]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-common.c
re GNATS gcj/129 (Static array length access bug in gcj)
[thirdparty/gcc.git] / gcc / c-common.c
CommitLineData
b30f223b 1/* Subroutines shared by all languages that are variants of C.
06ceef4e
RK
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
b30f223b
RS
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
940d9d63
RK
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
b30f223b
RS
21
22#include "config.h"
670ee920 23#include "system.h"
b30f223b
RS
24#include "tree.h"
25#include "c-lex.h"
26#include "c-tree.h"
27#include "flags.h"
5f6da302 28#include "toplev.h"
d6f4ec51 29#include "output.h"
e2af664c 30#include "c-pragma.h"
3932261a 31#include "rtl.h"
1526a060 32#include "ggc.h"
c6991660 33#include "expr.h"
7bdb32b9 34#include "tm_p.h"
ccd043a9 35
c8724862
DB
36#if USE_CPPLIB
37#include "cpplib.h"
38cpp_reader parse_in;
39cpp_options parse_options;
3773a46b 40enum cpp_token cpp_token;
c8724862
DB
41#endif
42
12a39b12
JM
43#undef WCHAR_TYPE_SIZE
44#define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node)
fef610be 45
7f4edbcb 46/* The following symbols are subsumed in the c_global_trees array, and
d125d268 47 listed here individually for documentation purposes.
7f4edbcb
BS
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
136tree c_global_trees[CTI_MAX];
0b73773c 137
2ce07e2d
NS
138tree (*make_fname_decl) PARAMS ((tree, const char *, int));
139
e78a3b42
RK
140/* Nonzero means the expression being parsed will never be evaluated.
141 This is a count, since unevaluated expressions can nest. */
142int skip_evaluation;
143
2786cbad 144enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
7d384cc0 145 A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
bbb1ae01 146 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
a157febd 147 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS, A_MALLOC,
2a8f6b90 148 A_NO_LIMIT_STACK, A_PURE};
53065596 149
bb72a084
PE
150enum format_type { printf_format_type, scanf_format_type,
151 strftime_format_type };
152
6e090c76
KG
153static void add_attribute PARAMS ((enum attrs, const char *,
154 int, int, int));
155static void init_attributes PARAMS ((void));
156static void record_function_format PARAMS ((tree, tree, enum format_type,
157 int, int));
158static void record_international_format PARAMS ((tree, tree, int));
6e090c76 159static int default_valid_lang_attribute PARAMS ((tree, tree, tree, tree));
4724b3de 160
0a7ed33c
BS
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. */
166typedef struct
167{
168 int compstmt_count;
169 int line;
dff01034 170 const char *file;
0a7ed33c
BS
171 int needs_warning;
172} if_elt;
6e090c76 173static void tfaff PARAMS ((void));
0a7ed33c
BS
174
175static if_elt *if_stack;
6d819282
MK
176
177/* Amount of space in the if statement stack. */
178static int if_stack_space = 0;
179
180/* Stack pointer. */
181static int if_stack_pointer = 0;
182
0a7ed33c
BS
183/* Generate RTL for the start of an if-then, and record the start of it
184 for ambiguous else detection. */
185
6d819282
MK
186void
187c_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;
0a7ed33c 196 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
6d819282
MK
197 }
198 else if (if_stack_space == if_stack_pointer)
199 {
200 if_stack_space += 10;
0a7ed33c 201 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
6d819282 202 }
0a7ed33c 203
6d819282 204 /* Record this if statement. */
0a7ed33c
BS
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++;
6d819282
MK
210
211 expand_start_cond (cond, exitflag);
212}
213
0a7ed33c
BS
214/* Generate RTL for the end of an if-then. Optionally warn if a nested
215 if statement had an ambiguous else clause. */
216
6d819282
MK
217void
218c_expand_end_cond ()
219{
220 if_stack_pointer--;
0a7ed33c
BS
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'");
6d819282
MK
225 expand_end_cond ();
226}
227
0a7ed33c
BS
228/* Generate RTL between the then-clause and the else-clause
229 of an if-then-else. */
230
6d819282
MK
231void
232c_expand_start_else ()
233{
0a7ed33c
BS
234 /* An ambiguous else warning must be generated for the enclosing if
235 statement, unless we see an else branch for that one, too. */
6d819282
MK
236 if (warn_parentheses
237 && if_stack_pointer > 1
0a7ed33c
BS
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--;
6d819282
MK
247
248 expand_start_else ();
249}
250
6f4d7222 251/* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__. */
7da551a2
RS
252
253void
254declare_function_name ()
255{
dff01034 256 const char *name, *printable_name;
7da551a2
RS
257
258 if (current_function_decl == NULL)
259 {
260 name = "";
261 printable_name = "top level";
262 }
263 else
264 {
6152f876
RS
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 = "";
a1d7ffe3 270 printable_name = (*decl_printable_name) (current_function_decl, 2);
7da551a2 271 }
2ce07e2d
NS
272
273 (*make_fname_decl) (get_identifier ("__FUNCTION__"), name, 0);
274 (*make_fname_decl) (get_identifier ("__PRETTY_FUNCTION__"), printable_name, 1);
6f4d7222 275 /* The ISO C people "of course" couldn't use __FUNCTION__ in the
31948547 276 ISO C 99 standard; instead a new variable is invented. */
2ce07e2d 277 (*make_fname_decl) (get_identifier ("__func__"), name, 0);
7da551a2
RS
278}
279
b30f223b
RS
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
284tree
285combine_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
4dd7201e 319 p = ggc_alloc_string (NULL, length);
b30f223b
RS
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 {
7e2231e7 333 memcpy (q, TREE_STRING_POINTER (t), len);
b30f223b
RS
334 q += len;
335 }
336 else
337 {
338 int i;
339 for (i = 0; i < len; i++)
41bbd14e
JW
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 }
b30f223b
RS
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;
b30f223b
RS
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
b57062ca 370 /* Compute the number of elements, for the array type. */
b30f223b
RS
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
d9cf7c82
JM
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
b30f223b
RS
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)));
d9cf7c82 391
ccd4c832
JM
392 TREE_CONSTANT (value) = 1;
393 TREE_READONLY (value) = ! flag_writable_strings;
b30f223b
RS
394 TREE_STATIC (value) = 1;
395 return value;
396}
397\f
53065596
RK
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
403static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
404
405static int attrtab_idx = 0;
406
407/* Add an entry to the attribute table above. */
408
409static void
410add_attribute (id, string, min_len, max_len, decl_req)
411 enum attrs id;
dff01034 412 const char *string;
53065596
RK
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
435static void
436init_attributes ()
437{
a89ca5c6 438 add_attribute (A_PACKED, "packed", 0, 0, 0);
bbb1ae01 439 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
2786cbad 440 add_attribute (A_COMMON, "common", 0, 0, 1);
53065596
RK
441 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
442 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
6d819282 443 add_attribute (A_UNUSED, "unused", 0, 0, 0);
53065596
RK
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);
d161fae4 451 add_attribute (A_FORMAT, "format", 3, 3, 1);
0161e8da 452 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
4b8af8d9
JM
453 add_attribute (A_WEAK, "weak", 0, 0, 1);
454 add_attribute (A_ALIAS, "alias", 1, 1, 1);
07417085 455 add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
7d384cc0 456 add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
140592a0 457 add_attribute (A_MALLOC, "malloc", 0, 0, 1);
a157febd 458 add_attribute (A_NO_LIMIT_STACK, "no_stack_limit", 0, 0, 1);
2a8f6b90 459 add_attribute (A_PURE, "pure", 0, 0, 1);
53065596
RK
460}
461\f
adfaf194
JM
462/* Default implementation of valid_lang_attribute, below. By default, there
463 are no language-specific attributes. */
464
465static int
466default_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
6e090c76 478int (*valid_lang_attribute) PARAMS ((tree, tree, tree, tree))
adfaf194
JM
479 = default_valid_lang_attribute;
480
1228e2a6 481/* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
53065596
RK
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
0f41302f 484 and declaration modifiers but before the declaration proper. */
b30f223b
RS
485
486void
53065596
RK
487decl_attributes (node, attributes, prefix_attributes)
488 tree node, attributes, prefix_attributes;
b30f223b 489{
a16b4c9c
JW
490 tree decl = 0, type = 0;
491 int is_type = 0;
53065596
RK
492 tree a;
493
494 if (attrtab_idx == 0)
495 init_attributes ();
496
2f939d94 497 if (DECL_P (node))
53065596
RK
498 {
499 decl = node;
500 type = TREE_TYPE (decl);
501 is_type = TREE_CODE (node) == TYPE_DECL;
502 }
2f939d94 503 else if (TYPE_P (node))
53065596 504 type = node, is_type = 1;
a2cd7b45 505
e2af664c
NC
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
d125d268 513
f09db6e0
NC
514#ifdef INSERT_ATTRIBUTES
515 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
516#endif
d125d268 517
eed32833 518 attributes = chainon (prefix_attributes, attributes);
5289b665 519
b30f223b 520 for (a = attributes; a; a = TREE_CHAIN (a))
53065596
RK
521 {
522 tree name = TREE_PURPOSE (a);
523 tree args = TREE_VALUE (a);
524 int i;
525 enum attrs id;
b57062ca 526
53065596
RK
527 for (i = 0; i < attrtab_idx; i++)
528 if (attrtab[i].name == name)
529 break;
530
6eaba4a7 531 if (i == attrtab_idx)
53065596 532 {
adfaf194
JM
533 if (! valid_machine_attribute (name, args, decl, type)
534 && ! (* valid_lang_attribute) (name, args, decl, type))
6eaba4a7
DE
535 warning ("`%s' attribute directive ignored",
536 IDENTIFIER_POINTER (name));
17c1a44f
SC
537 else if (decl != 0)
538 type = TREE_TYPE (decl);
53065596
RK
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:
1bcf5b08 559 if (is_type)
a89ca5c6
RK
560 TYPE_PACKED (type) = 1;
561 else if (TREE_CODE (decl) == FIELD_DECL)
53065596
RK
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
bbb1ae01
RK
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
2786cbad
JM
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
53065596
RK
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)
b57062ca 588 TREE_TYPE (decl) = type
53065596
RK
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
140592a0
AG
596 case A_MALLOC:
597 if (TREE_CODE (decl) == FUNCTION_DECL)
598 DECL_IS_MALLOC (decl) = 1;
2a8f6b90 599 /* ??? TODO: Support types. */
140592a0
AG
600 else
601 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
602 break;
603
1c4fadec 604 case A_UNUSED:
6d819282 605 if (is_type)
d20a70b4
CM
606 if (decl)
607 TREE_USED (decl) = 1;
608 else
609 TREE_USED (type) = 1;
b57062ca 610 else if (TREE_CODE (decl) == PARM_DECL
6d819282 611 || TREE_CODE (decl) == VAR_DECL
736b02fd
KG
612 || TREE_CODE (decl) == FUNCTION_DECL
613 || TREE_CODE (decl) == LABEL_DECL)
1c4fadec
RK
614 TREE_USED (decl) = 1;
615 else
616 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
617 break;
618
53065596
RK
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
2a8f6b90
JH
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
53065596 641 case A_T_UNION:
1bcf5b08 642 if (is_type
53065596 643 && TREE_CODE (type) == UNION_TYPE
1bcf5b08 644 && (decl == 0
62b1077c
RK
645 || (TYPE_FIELDS (type) != 0
646 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
1bcf5b08
RK
647 TYPE_TRANSPARENT_UNION (type) = 1;
648 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
53065596
RK
649 && TREE_CODE (type) == UNION_TYPE
650 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
1bcf5b08 651 DECL_TRANSPARENT_UNION (decl) = 1;
53065596
RK
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)
1a16a053
RK
660 {
661 DECL_STATIC_CONSTRUCTOR (decl) = 1;
662 TREE_USED (decl) = 1;
663 }
53065596
RK
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)
1a16a053
RK
672 {
673 DECL_STATIC_DESTRUCTOR (decl) = 1;
674 TREE_USED (decl) = 1;
675 }
53065596
RK
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;
dff01034 686 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
53065596
RK
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 {
3438dff9 694 char *newp = (char *) alloca (len - 1);
53065596
RK
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;
06ceef4e 722 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
53065596
RK
723 layout_decl (decl, 0);
724 }
725 }
726 break;
727
728 case A_SECTION:
5289b665 729#ifdef ASM_OUTPUT_SECTION_NAME
53065596
RK
730 if ((TREE_CODE (decl) == FUNCTION_DECL
731 || TREE_CODE (decl) == VAR_DECL)
732 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
733 {
b57062ca 734 if (TREE_CODE (decl) == VAR_DECL
f4ca236c
RK
735 && current_function_decl != NULL_TREE
736 && ! TREE_STATIC (decl))
53065596
RK
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,
5289b665
DE
751 "section attribute not allowed for `%s'");
752#else
53065596
RK
753 error_with_decl (node,
754 "section attributes are not supported for this target");
5289b665 755#endif
53065596
RK
756 break;
757
758 case A_ALIGNED:
6f38f669 759 {
53065596 760 tree align_expr
54630035
RK
761 = (args ? TREE_VALUE (args)
762 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
05bccae2 763 int i;
d125d268 764
53065596
RK
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);
b57062ca 770
53065596
RK
771 if (TREE_CODE (align_expr) != INTEGER_CST)
772 {
773 error ("requested alignment is not a constant");
774 continue;
775 }
776
05bccae2 777 if ((i = tree_log2 (align_expr)) == -1)
53065596 778 error ("requested alignment is not a power of 2");
05bccae2
RK
779 else if (i > HOST_BITS_PER_INT - 2)
780 error ("requested alignment is too large");
53065596 781 else if (is_type)
05bccae2 782 TYPE_ALIGN (type) = (1 << i) * BITS_PER_UNIT;
53065596
RK
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
05bccae2 788 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
6f38f669 789 }
53065596 790 break;
6f38f669 791
53065596 792 case A_FORMAT:
b30f223b 793 {
bb72a084 794 tree format_type_id = TREE_VALUE (args);
53065596
RK
795 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
796 tree first_arg_num_expr
797 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
05bccae2 798 unsigned HOST_WIDE_INT format_num, first_arg_num;
bb72a084 799 enum format_type format_type;
53065596 800 tree argument;
05bccae2 801 unsigned int arg_num;
b57062ca 802
53065596
RK
803 if (TREE_CODE (decl) != FUNCTION_DECL)
804 {
805 error_with_decl (decl,
806 "argument format specified for non-function `%s'");
807 continue;
808 }
d125d268 809
bb72a084 810 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
0161e8da 811 {
bb72a084 812 error ("unrecognized format specifier");
0161e8da
RK
813 continue;
814 }
53065596
RK
815 else
816 {
dff01034 817 const char *p = IDENTIFIER_POINTER (format_type_id);
d125d268 818
bb72a084
PE
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 {
b27d2bd5 828 warning ("`%s' is an unrecognized format function type", p);
bb72a084
PE
829 continue;
830 }
53065596 831 }
6f38f669 832
53065596
RK
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);
677ff441 839
53065596
RK
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
05bccae2
RK
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)
09e3dd72 849 {
05bccae2 850 error ("format string has invalid operand number");
53065596 851 continue;
09e3dd72 852 }
53065596
RK
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)
68a91d8d 857 {
53065596 858 error ("format string arg follows the args to be formatted");
6f38f669 859 continue;
68a91d8d 860 }
53065596
RK
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)
09e3dd72 867 {
05bccae2
RK
868 for (arg_num = 1; argument != 0 && arg_num != format_num;
869 ++arg_num, argument = TREE_CHAIN (argument))
870 ;
871
53065596
RK
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");
09e3dd72
RK
878 continue;
879 }
05bccae2
RK
880
881 else if (first_arg_num != 0)
53065596
RK
882 {
883 /* Verify that first_arg_num points to the last arg,
0f41302f 884 the ... */
53065596
RK
885 while (argument)
886 arg_num++, argument = TREE_CHAIN (argument);
05bccae2
RK
887
888 if (arg_num != first_arg_num)
889 {
890 error ("args to be formatted is not '...'");
891 continue;
892 }
53065596 893 }
09e3dd72 894 }
a2cd7b45 895
53065596
RK
896 record_function_format (DECL_NAME (decl),
897 DECL_ASSEMBLER_NAME (decl),
bb72a084 898 format_type, format_num, first_arg_num);
53065596
RK
899 break;
900 }
4b8af8d9 901
0161e8da
RK
902 case A_FORMAT_ARG:
903 {
904 tree format_num_expr = TREE_VALUE (args);
05bccae2
RK
905 unsigned HOST_WIDE_INT format_num;
906 unsigned int arg_num;
0161e8da 907 tree argument;
b57062ca 908
0161e8da
RK
909 if (TREE_CODE (decl) != FUNCTION_DECL)
910 {
911 error_with_decl (decl,
912 "argument format specified for non-function `%s'");
913 continue;
914 }
b57062ca 915
0161e8da
RK
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
05bccae2
RK
923 if (TREE_CODE (format_num_expr) != INTEGER_CST
924 || TREE_INT_CST_HIGH (format_num_expr) != 0)
0161e8da 925 {
05bccae2 926 error ("format string has invalid operand number");
0161e8da
RK
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 {
05bccae2
RK
938 for (arg_num = 1; argument != 0 && arg_num != format_num;
939 ++arg_num, argument = TREE_CHAIN (argument))
940 ;
941
0161e8da
RK
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
4b8af8d9
JM
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))
5d9dd5a5 972 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
4b8af8d9
JM
973 error_with_decl (decl,
974 "`%s' defined both normally and as an alias");
975 else if (decl_function_context (decl) == 0)
976 {
20045452
RH
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));
06a50fff
ZW
986 /* This counts as a use of the object pointed to. */
987 TREE_USED (id) = 1;
20045452 988
4b8af8d9
JM
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;
07417085 998
7d384cc0
KR
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
07417085
KR
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;
a157febd
GK
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;
53065596
RK
1049 }
1050 }
b30f223b 1051}
800f4153
RK
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
1063void
1064split_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
101e59f4
ML
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
800f4153
RK
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}
d9525bec
BK
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
1138tree
1139strip_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}
b30f223b 1155\f
1ccf251f
RK
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
2314fb70 1161#define T_LL &long_long_integer_type_node
1ccf251f
RK
1162#define T_S &short_integer_type_node
1163#define T_UI &unsigned_type_node
1164#define T_UL &long_unsigned_type_node
2314fb70 1165#define T_ULL &long_long_unsigned_type_node
1ccf251f
RK
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
b57062ca 1171#define T_UC &unsigned_char_type_node
1ccf251f
RK
1172#define T_V &void_type_node
1173#define T_W &wchar_type_node
df8a401a 1174#define T_ST &sizetype
1ccf251f
RK
1175
1176typedef struct {
dff01034 1177 const char *format_chars;
1ccf251f
RK
1178 int pointer_count;
1179 /* Type of argument if no length modifier is used. */
1180 tree *nolen;
bc516719 1181 /* Type of argument if length modifier for shortening to byte is used.
1ccf251f 1182 If NULL, then this modifier is not allowed. */
b57062ca 1183 tree *hhlen;
bc516719 1184 /* Type of argument if length modifier for shortening is used.
b57062ca 1185 If NULL, then this modifier is not allowed. */
1ccf251f
RK
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;
2cedb812 1190 /* Type of argument if length modifier `q' or `ll' is used.
2314fb70
CH
1191 If NULL, then this modifier is not allowed. */
1192 tree *qlen;
1ccf251f
RK
1193 /* Type of argument if length modifier `L' is used.
1194 If NULL, then this modifier is not allowed. */
1195 tree *bigllen;
d125d268 1196 /* Type of argument if length modifiers 'z' or `Z' is used.
e5e809f4
JL
1197 If NULL, then this modifier is not allowed. */
1198 tree *zlen;
1ccf251f 1199 /* List of other modifier characters allowed with these options. */
dff01034 1200 const char *flag_chars;
1ccf251f
RK
1201} format_char_info;
1202
1203static format_char_info print_char_table[] = {
b57062ca
UD
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" },
e5e809f4 1207/* A GNU extension. */
b57062ca
UD
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, "" },
c84e2712 1216 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1ccf251f
RK
1217};
1218
1219static format_char_info scan_char_table[] = {
b57062ca
UD
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, "*" },
c6a9dea8
JW
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" },
b57062ca
UD
1225 { "[", 1, T_C, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1226 { "C", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
c6a9dea8 1227 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
b57062ca
UD
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, "" },
c84e2712 1230 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1ccf251f
RK
1231};
1232
bb72a084
PE
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
1241static format_char_info time_char_table[] = {
b57062ca
UD
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" },
e316d107 1251 { "ABZa", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^#" },
b57062ca
UD
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" },
c84e2712 1255 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
bb72a084
PE
1256};
1257
0161e8da
RK
1258typedef struct function_format_info
1259{
1ccf251f
RK
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++) */
bb72a084 1263 enum format_type format_type; /* type of format (printf, scanf, etc.) */
1ccf251f
RK
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
1268static function_format_info *function_format_list = NULL;
1269
0161e8da
RK
1270typedef 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
1278static international_format_info *international_format_list = NULL;
1279
6e090c76 1280static void check_format_info PARAMS ((function_format_info *, tree));
1ccf251f
RK
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
b57062ca 1287 header file or not. In any case, the checking is harmless.
0161e8da
RK
1288
1289 Also initialize the name of function that modify the format string for
1290 internationalization purposes. */
1ccf251f
RK
1291
1292void
1293init_function_format_info ()
1294{
bb72a084
PE
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);
0161e8da
RK
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);
1ccf251f
RK
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
bb72a084
PE
1323 need not exist yet).
1324 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
1ccf251f
RK
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
0161e8da 1327 against the format string, or zero if no checking is not be done
1ccf251f
RK
1328 (e.g. for varargs such as vfprintf). */
1329
bb72a084
PE
1330static void
1331record_function_format (name, assembler_name, format_type,
1ccf251f
RK
1332 format_num, first_arg_num)
1333 tree name;
1334 tree assembler_name;
bb72a084 1335 enum format_type format_type;
1ccf251f
RK
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
bb72a084 1358 info->format_type = format_type;
1ccf251f
RK
1359 info->format_num = format_num;
1360 info->first_arg_num = first_arg_num;
1361}
1362
0161e8da
RK
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
31837ce2 1369static void
0161e8da
RK
1370record_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
ab87f8c8
JL
1400static void
1401tfaff ()
1402{
1403 warning ("too few arguments for format");
1404}
1ccf251f
RK
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
1412void
1413check_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 {
95216dec 1423 if (info->assembler_name
1ccf251f
RK
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
1438static void
1439check_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;
f67aab2c 1446 int length_char = 0;
1ccf251f
RK
1447 int format_char;
1448 int format_length;
1449 tree format_tree;
1450 tree cur_param;
1451 tree cur_type;
1452 tree wanted_type;
9b69f523 1453 tree first_fillin_param;
dff01034 1454 const char *format_chars;
f67aab2c 1455 format_char_info *fci = NULL;
1ccf251f 1456 char flag_chars[8];
9b69f523 1457 int has_operand_number = 0;
1ccf251f
RK
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;
0161e8da 1473
1ccf251f
RK
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 */
0161e8da
RK
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
39b751ce 1510 if (integer_zerop (format_tree))
1ccf251f
RK
1511 {
1512 warning ("null format string");
1513 return;
1514 }
1515 if (TREE_CODE (format_tree) != ADDR_EXPR)
ba806745
KR
1516 {
1517 /* The user may get multiple warnings if the supplied argument
1518 isn't even a string pointer. */
30145215
KG
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. */
1d682cca 1522 if (info->first_arg_num != 0 && warn_format > 1)
30145215 1523 warning ("format not a string literal, argument types not checked");
ba806745
KR
1524 return;
1525 }
1ccf251f
RK
1526 format_tree = TREE_OPERAND (format_tree, 0);
1527 if (TREE_CODE (format_tree) != STRING_CST)
ba806745
KR
1528 {
1529 /* The user may get multiple warnings if the supplied argument
1530 isn't even a string pointer. */
30145215
KG
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. */
1d682cca 1534 if (info->first_arg_num != 0 && warn_format > 1)
30145215 1535 warning ("format not a string literal, argument types not checked");
ba806745
KR
1536 return;
1537 }
1ccf251f
RK
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 }
9b69f523
RK
1555
1556 first_fillin_param = params;
1ccf251f
RK
1557 while (1)
1558 {
af3c5588 1559 int aflag;
1ccf251f
RK
1560 if (*format_chars == 0)
1561 {
1562 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
1563 warning ("embedded `\\0' in format");
9b69f523 1564 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
1ccf251f
RK
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;
bb72a084 1582 if (info->format_type == scanf_format_type)
1ccf251f
RK
1583 {
1584 suppressed = *format_chars == '*';
1585 if (suppressed)
1586 ++format_chars;
e9a780ec 1587 while (ISDIGIT (*format_chars))
1ccf251f
RK
1588 ++format_chars;
1589 }
bb72a084
PE
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 }
e9a780ec 1610 while (ISDIGIT ((unsigned char) *format_chars))
bb72a084
PE
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)
1ccf251f 1631 {
9b69f523
RK
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 {
dff01034 1636 const char *p = format_chars;
9b69f523
RK
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
1ccf251f
RK
1660 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
1661 {
1662 if (index (flag_chars, *format_chars) != 0)
bb72a084 1663 warning ("repeated `%c' flag in format", *format_chars++);
6d819282
MK
1664 else
1665 {
1666 i = strlen (flag_chars);
1667 flag_chars[i++] = *format_chars++;
1668 flag_chars[i] = 0;
1669 }
1ccf251f 1670 }
b57062ca 1671 /* "If the space and + flags both appear,
1ccf251f
RK
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 {
ab87f8c8 1689 tfaff ();
1ccf251f
RK
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. */
309ffab6
RS
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))
bb72a084 1706 warning ("field width is not type int (arg %d)", arg_num);
1ccf251f
RK
1707 }
1708 }
1709 else
1710 {
e9a780ec 1711 while (ISDIGIT (*format_chars))
1ccf251f
RK
1712 {
1713 wide = TRUE;
1714 ++format_chars;
1715 }
1716 }
1717 if (*format_chars == '.')
1718 {
1719 precise = TRUE;
1720 ++format_chars;
e9a780ec 1721 if (*format_chars != '*' && !ISDIGIT (*format_chars))
1ccf251f
RK
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 {
ab87f8c8 1732 tfaff ();
1ccf251f
RK
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)
bb72a084
PE
1740 warning ("field width is not type int (arg %d)",
1741 arg_num);
1ccf251f
RK
1742 }
1743 }
1744 else
1745 {
e9a780ec 1746 while (ISDIGIT (*format_chars))
1ccf251f
RK
1747 ++format_chars;
1748 }
1749 }
1750 }
bb72a084 1751
af3c5588 1752 aflag = 0;
bb72a084
PE
1753
1754 if (info->format_type != strftime_format_type)
af3c5588 1755 {
bb72a084
PE
1756 if (*format_chars == 'h' || *format_chars == 'l')
1757 length_char = *format_chars++;
1758 else if (*format_chars == 'q' || *format_chars == 'L')
6d819282 1759 {
bb72a084 1760 length_char = *format_chars++;
f5963e61 1761 if (pedantic)
bb72a084
PE
1762 warning ("ANSI C does not support the `%c' length modifier",
1763 length_char);
6d819282 1764 }
d125d268 1765 else if (*format_chars == 'Z' || *format_chars == 'z')
bb72a084
PE
1766 {
1767 length_char = *format_chars++;
31948547 1768 if (pedantic && (length_char == 'Z' || !flag_isoc99))
d125d268
UD
1769 warning ("ANSI C does not support the `%c' length modifier",
1770 length_char);
bb72a084
PE
1771 }
1772 else
1773 length_char = 0;
1774 if (length_char == 'l' && *format_chars == 'l')
1775 {
1776 length_char = 'q', format_chars++;
31948547 1777 if (pedantic && !flag_isoc99)
bb72a084
PE
1778 warning ("ANSI C does not support the `ll' length modifier");
1779 }
bc516719
AS
1780 else if (length_char == 'h' && *format_chars == 'h')
1781 {
1782 length_char = 'H', format_chars++;
31948547 1783 if (pedantic && !flag_isoc99)
bc516719
AS
1784 warning ("ANSI C does not support the `hh' length modifier");
1785 }
bb72a084
PE
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);
1ccf251f
RK
1798 }
1799 format_char = *format_chars;
bb72a084
PE
1800 if (format_char == 0
1801 || (info->format_type != strftime_format_type && format_char == '%'))
1ccf251f
RK
1802 {
1803 warning ("conversion lacks type at end of format");
1804 continue;
1805 }
c6a9dea8
JW
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);
d125d268 1810 /* The a and A formats are C99 extensions. */
c6a9dea8 1811 if (pedantic && info->format_type != strftime_format_type
d125d268 1812 && (format_char == 'a' || format_char == 'A')
31948547 1813 && !flag_isoc99)
c6a9dea8 1814 warning ("ANSI C does not support the `%c' format", format_char);
1ccf251f 1815 format_chars++;
bb72a084
PE
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 }
1ccf251f
RK
1830 while (fci->format_chars != 0
1831 && index (fci->format_chars, format_char) == 0)
1832 ++fci;
1833 if (fci->format_chars == 0)
1834 {
f3ad1f9c 1835 if (ISGRAPH(format_char))
bb72a084 1836 warning ("unknown conversion type character `%c' in format",
1ccf251f
RK
1837 format_char);
1838 else
bb72a084 1839 warning ("unknown conversion type character 0x%x in format",
1ccf251f 1840 format_char);
1ccf251f
RK
1841 continue;
1842 }
bb72a084 1843 if (pedantic)
1ccf251f 1844 {
bb72a084
PE
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);
1ccf251f 1850 }
bb72a084
PE
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);
1ccf251f 1858 if (precise && index (fci->flag_chars, 'p') == 0)
bb72a084 1859 warning ("precision used with `%c' format", format_char);
af3c5588
RK
1860 if (aflag && index (fci->flag_chars, 'a') == 0)
1861 {
bb72a084 1862 warning ("`a' flag used with `%c' format", format_char);
f9dcab52
RK
1863 /* To simplify the following code. */
1864 aflag = 0;
1ccf251f 1865 }
c6a9dea8
JW
1866 /* The a flag is a GNU extension. */
1867 else if (pedantic && aflag)
1868 warning ("ANSI C does not support the `a' flag");
bb72a084 1869 if (info->format_type == scanf_format_type && format_char == '[')
1ccf251f
RK
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 != ']')
bb72a084
PE
1881 /* The end of the format string was reached. */
1882 warning ("no closing `]' for `%%[' format");
1ccf251f
RK
1883 }
1884 if (suppressed)
1885 {
1886 if (index (fci->flag_chars, '*') == 0)
bb72a084 1887 warning ("suppression of `%c' conversion in format", format_char);
1ccf251f
RK
1888 continue;
1889 }
1890 for (i = 0; flag_chars[i] != 0; ++i)
1891 {
1892 if (index (fci->flag_chars, flag_chars[i]) == 0)
bb72a084
PE
1893 warning ("flag `%c' used with type `%c'",
1894 flag_chars[i], format_char);
1ccf251f 1895 }
bb72a084
PE
1896 if (info->format_type == strftime_format_type)
1897 continue;
f5963e61
JL
1898 if (precise && index (flag_chars, '0') != 0
1899 && (format_char == 'd' || format_char == 'i'
1900 || format_char == 'o' || format_char == 'u'
bf39dde3 1901 || format_char == 'x' || format_char == 'X'))
bb72a084
PE
1902 warning ("`0' flag ignored with precision specifier and `%c' format",
1903 format_char);
1ccf251f
RK
1904 switch (length_char)
1905 {
1906 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
b57062ca 1907 case 'H': wanted_type = fci->hhlen ? *(fci->hhlen) : 0; break;
1ccf251f
RK
1908 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1909 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
2314fb70 1910 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1ccf251f 1911 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
d125d268 1912 case 'z': case 'Z': wanted_type = fci->zlen ? *fci->zlen : 0; break;
1ccf251f 1913 }
f5963e61 1914 if (wanted_type == 0)
72f54004
JM
1915 warning ("use of `%c' length character with `%c' type character",
1916 length_char, format_char);
1ccf251f 1917
1ccf251f
RK
1918 /* Finally. . .check type of argument against desired type! */
1919 if (info->first_arg_num == 0)
1920 continue;
2a13575e
RK
1921 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1922 /* This specifier takes no argument. */
1923 continue;
1ccf251f
RK
1924 if (params == 0)
1925 {
ab87f8c8 1926 tfaff ();
1ccf251f
RK
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
6d819282
MK
1934 STRIP_NOPS (cur_param);
1935
1ccf251f
RK
1936 /* Check the types of any additional pointer arguments
1937 that precede the "real" argument. */
f9dcab52 1938 for (i = 0; i < fci->pointer_count + aflag; ++i)
1ccf251f
RK
1939 {
1940 if (TREE_CODE (cur_type) == POINTER_TYPE)
1941 {
1942 cur_type = TREE_TYPE (cur_type);
6d819282 1943
f5963e61 1944 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
6d819282
MK
1945 cur_param = TREE_OPERAND (cur_param, 0);
1946 else
1947 cur_param = 0;
1948
1ccf251f
RK
1949 continue;
1950 }
87416640 1951 if (TREE_CODE (cur_type) != ERROR_MARK)
913d0833
KG
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 }
1ccf251f
RK
1958 break;
1959 }
1960
6d819282 1961 /* See if this is an attempt to write into a const type with
bc516719
AS
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'))
bb72a084 1966 && i == fci->pointer_count + aflag
6d819282
MK
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'
2f939d94 1972 || (DECL_P (cur_param) && TREE_READONLY (cur_param))))))
bb72a084 1973 warning ("writing into constant object (arg %d)", arg_num);
6d819282 1974
1ccf251f 1975 /* Check the type of the "real" argument, if there's a type we want. */
f9dcab52 1976 if (i == fci->pointer_count + aflag && wanted_type != 0
87416640 1977 && TREE_CODE (cur_type) != ERROR_MARK
1ccf251f
RK
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
b7c9c707 1985 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
4215e498 1986 && (TREE_UNSIGNED (wanted_type)
f5775325
RK
1987 ? wanted_type == (cur_type = unsigned_type (cur_type))
1988 : wanted_type == (cur_type = signed_type (cur_type))))
60e02b1e
RK
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
b7c9c707
RS
1992 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
1993 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
1ccf251f 1994 {
dff01034
KG
1995 register const char *this;
1996 register const char *that;
b57062ca 1997
1ccf251f
RK
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
309ffab6
RS
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
1ccf251f 2032 if (strcmp (this, that) != 0)
bb72a084 2033 warning ("%s format, %s arg (arg %d)", this, that, arg_num);
1ccf251f
RK
2034 }
2035 }
2036}
2037\f
d74154d5
RS
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. */
96571883
BK
2043
2044void
2045constant_expression_warning (value)
2046 tree value;
2047{
c05f751c
RK
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");
d74154d5
RS
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
2061void
2062overflow_warning (value)
2063 tree value;
2064{
c05f751c
RK
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))
d74154d5 2069 {
7193bce2 2070 TREE_OVERFLOW (value) = 0;
e78a3b42
RK
2071 if (skip_evaluation == 0)
2072 warning ("integer overflow in expression");
d74154d5 2073 }
c05f751c
RK
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;
e78a3b42
RK
2080 if (skip_evaluation == 0)
2081 warning ("floating point overflow in expression");
c05f751c 2082 }
d74154d5
RS
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
2090void
2091unsigned_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))
e78a3b42 2097 && skip_evaluation == 0
d74154d5
RS
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. */
90c939d4 2102 warning ("large integer implicitly truncated to unsigned type");
d74154d5 2103 else if (warn_conversion)
90c939d4 2104 warning ("negative integer implicitly converted to unsigned type");
d74154d5
RS
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
2112tree
2113convert_and_check (type, expr)
2114 tree type, expr;
2115{
2116 tree t = convert (type, expr);
2117 if (TREE_CODE (t) == INTEGER_CST)
2118 {
7193bce2 2119 if (TREE_OVERFLOW (t))
d74154d5 2120 {
7193bce2
PE
2121 TREE_OVERFLOW (t) = 0;
2122
868fc750
RK
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
7193bce2
PE
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))))
22ba338b
RS
2131 /* If EXPR fits in the unsigned version of TYPE,
2132 don't warn unless pedantic. */
e78a3b42
RK
2133 if ((pedantic
2134 || TREE_UNSIGNED (type)
2135 || ! int_fits_type_p (expr, unsigned_type (type)))
2136 && skip_evaluation == 0)
bb72a084 2137 warning ("overflow in implicit constant conversion");
d74154d5
RS
2138 }
2139 else
2140 unsigned_conversion_warning (t, expr);
2141 }
2142 return t;
96571883
BK
2143}
2144\f
b30f223b
RS
2145void
2146c_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
b8de2d02 2156 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
b30f223b
RS
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
2165tree
2166check_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. */
8493738b 2173 STRIP_TYPE_NOPS (value);
b30f223b
RS
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
bc690db1
RS
2185 constant_expression_warning (value);
2186
b30f223b
RS
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
2193tree
2194type_for_size (bits, unsignedp)
2195 unsigned bits;
2196 int unsignedp;
2197{
a311b52c
JM
2198 if (bits == TYPE_PRECISION (integer_type_node))
2199 return unsignedp ? unsigned_type_node : integer_type_node;
2200
3fc7e390 2201 if (bits == TYPE_PRECISION (signed_char_type_node))
b30f223b
RS
2202 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2203
3fc7e390 2204 if (bits == TYPE_PRECISION (short_integer_type_node))
b30f223b
RS
2205 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2206
3fc7e390 2207 if (bits == TYPE_PRECISION (long_integer_type_node))
b30f223b
RS
2208 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2209
3fc7e390 2210 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b30f223b
RS
2211 return (unsignedp ? long_long_unsigned_type_node
2212 : long_long_integer_type_node);
2213
835f9b4d
GRK
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
3fc7e390
RS
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
b30f223b
RS
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
2237tree
2238type_for_mode (mode, unsignedp)
2239 enum machine_mode mode;
2240 int unsignedp;
2241{
a311b52c
JM
2242 if (mode == TYPE_MODE (integer_type_node))
2243 return unsignedp ? unsigned_type_node : integer_type_node;
2244
b30f223b
RS
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
b30f223b
RS
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
835f9b4d 2257 if (mode == TYPE_MODE (widest_integer_literal_type_node))
d125d268 2258 return unsignedp ? widest_unsigned_literal_type_node
835f9b4d
GRK
2259 : widest_integer_literal_type_node;
2260
3fc7e390
RS
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
21a9616b 2273#if HOST_BITS_PER_WIDE_INT >= 64
a6d7e156
JL
2274 if (mode == TYPE_MODE (intTI_type_node))
2275 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
21a9616b 2276#endif
a6d7e156 2277
b30f223b
RS
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}
693a6128
GRK
2295
2296/* Return an unsigned type the same as TYPE in other respects. */
2297tree
2298unsigned_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
2332tree
2333signed_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
2368tree
2369signed_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;
d125d268 2379 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
693a6128 2380 return unsignedp ? unsigned_type_node : integer_type_node;
d125d268 2381 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
693a6128 2382 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
d125d268 2383 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
693a6128 2384 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
d125d268 2385 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
693a6128
GRK
2386 return (unsignedp ? long_long_unsigned_type_node
2387 : long_long_integer_type_node);
d125d268 2388 if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
693a6128
GRK
2389 return (unsignedp ? widest_unsigned_literal_type_node
2390 : widest_integer_literal_type_node);
2391 return type;
2392}
b30f223b 2393\f
6acfe908
JM
2394/* Return the minimum number of bits needed to represent VALUE in a
2395 signed or unsigned type, UNSIGNEDP says which. */
2396
05bccae2 2397unsigned int
6acfe908
JM
2398min_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;
6acfe908 2417 else
05bccae2 2418 log = tree_floor_log2 (value);
6acfe908
JM
2419
2420 return log + 1 + ! unsignedp;
2421}
2422\f
b30f223b
RS
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
2426void
2427binary_op_error (code)
2428 enum tree_code code;
2429{
dff01034 2430 register const char *opname;
89c78d7d 2431
b30f223b
RS
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:
047de90b 2465 case FLOOR_MOD_EXPR:
b30f223b
RS
2466 opname = "%"; break;
2467 case TRUNC_DIV_EXPR:
047de90b 2468 case FLOOR_DIV_EXPR:
b30f223b
RS
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;
047de90b
RS
2480 case LROTATE_EXPR:
2481 case RROTATE_EXPR:
2482 opname = "rotate"; break;
6d819282
MK
2483 default:
2484 opname = "unknown"; break;
b30f223b
RS
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.
94dccd9d
RS
2492 This function is also responsible for converting the two operands
2493 to the proper common type for comparison.
b30f223b
RS
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
2503tree
2504shorten_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
5af6001b
RK
2536 so value is preserved), for canonicalization. Don't do this if
2537 the second arg is 0. */
b30f223b 2538
5af6001b
RK
2539 if (TREE_CONSTANT (primop0)
2540 && ! integer_zerop (primop1) && ! real_zerop (primop1))
b30f223b
RS
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;
6d819282
MK
2571 default:
2572 break;
b30f223b
RS
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));
8bbd5685
CW
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);
b30f223b
RS
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)
a360da3a 2646 val = boolean_true_node;
b30f223b
RS
2647 }
2648 else if (code == EQ_EXPR)
2649 {
2650 if (max_lt || min_gt)
a360da3a 2651 val = boolean_false_node;
b30f223b
RS
2652 }
2653 else if (code == LT_EXPR)
2654 {
2655 if (max_lt)
a360da3a 2656 val = boolean_true_node;
b30f223b 2657 if (!min_lt)
a360da3a 2658 val = boolean_false_node;
b30f223b
RS
2659 }
2660 else if (code == GT_EXPR)
2661 {
2662 if (min_gt)
a360da3a 2663 val = boolean_true_node;
b30f223b 2664 if (!max_gt)
a360da3a 2665 val = boolean_false_node;
b30f223b
RS
2666 }
2667 else if (code == LE_EXPR)
2668 {
2669 if (!max_gt)
a360da3a 2670 val = boolean_true_node;
b30f223b 2671 if (min_gt)
a360da3a 2672 val = boolean_false_node;
b30f223b
RS
2673 }
2674 else if (code == GE_EXPR)
2675 {
2676 if (!min_lt)
a360da3a 2677 val = boolean_true_node;
b30f223b 2678 if (max_lt)
a360da3a 2679 val = boolean_false_node;
b30f223b
RS
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;
6d819282
MK
2709
2710 default:
2711 break;
b30f223b
RS
2712 }
2713 type = unsigned_type (type);
2714 }
2715
b7c9c707 2716 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
b30f223b
RS
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. */
a360da3a 2720 if (val == boolean_false_node)
07be2a23 2721 warning ("comparison is always false due to limited range of data type");
a360da3a 2722 if (val == boolean_true_node)
07be2a23 2723 warning ("comparison is always true due to limited range of data type");
b30f223b
RS
2724 }
2725
b7c9c707 2726 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
b30f223b 2727 {
1e276c4a 2728 /* This is the case of (unsigned char)x >?< -1 or < 0. */
a360da3a 2729 if (val == boolean_false_node)
07be2a23 2730 warning ("comparison is always false due to limited range of data type");
a360da3a 2731 if (val == boolean_true_node)
07be2a23 2732 warning ("comparison is always true due to limited range of data type");
b30f223b
RS
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
766f6c30
RS
2748 && (TYPE_PRECISION (TREE_TYPE (primop0))
2749 == TYPE_PRECISION (TREE_TYPE (primop1))))
b30f223b
RS
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)
597681f6 2784 && TREE_UNSIGNED (*restype_ptr))
b30f223b
RS
2785 {
2786 tree value = 0;
2787 switch (code)
2788 {
2789 case GE_EXPR:
5af6001b
RK
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))))
07be2a23 2798 warning ("comparison of unsigned expression >= 0 is always true");
a360da3a 2799 value = boolean_true_node;
b30f223b
RS
2800 break;
2801
2802 case LT_EXPR:
5af6001b
RK
2803 if (extra_warnings
2804 && ! (TREE_CODE (primop0) == INTEGER_CST
2805 && ! TREE_OVERFLOW (convert (signed_type (type),
2806 primop0))))
07be2a23 2807 warning ("comparison of unsigned expression < 0 is always false");
a360da3a 2808 value = boolean_false_node;
6d819282
MK
2809 break;
2810
2811 default:
2812 break;
b30f223b
RS
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
a360da3a 2829 *restype_ptr = boolean_type_node;
b30f223b
RS
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
a360da3a 2840 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
b30f223b
RS
2841 but we optimize comparisons, &&, ||, and !.
2842
a360da3a 2843 The resulting type should always be `boolean_type_node'. */
b30f223b
RS
2844
2845tree
2846truthvalue_conversion (expr)
2847 tree expr;
2848{
257e61ed
RS
2849 if (TREE_CODE (expr) == ERROR_MARK)
2850 return expr;
2851
d7c83727 2852#if 0 /* This appears to be wrong for C++. */
257e61ed
RS
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");
a360da3a 2859 return boolean_false_node;
257e61ed
RS
2860
2861 case UNION_TYPE:
2862 error ("union type value used where scalar is required");
a360da3a 2863 return boolean_false_node;
257e61ed
RS
2864
2865 case ARRAY_TYPE:
2866 error ("array type value used where scalar is required");
a360da3a 2867 return boolean_false_node;
257e61ed
RS
2868
2869 default:
2870 break;
2871 }
d7c83727 2872#endif /* 0 */
257e61ed 2873
b30f223b
RS
2874 switch (TREE_CODE (expr))
2875 {
b30f223b 2876 case EQ_EXPR:
b30f223b
RS
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:
9379fac9 2882 case TRUTH_XOR_EXPR:
1180eb10 2883 case TRUTH_NOT_EXPR:
a360da3a
JM
2884 TREE_TYPE (expr) = boolean_type_node;
2885 return expr;
18c0f675 2886
b30f223b
RS
2887 case ERROR_MARK:
2888 return expr;
2889
2890 case INTEGER_CST:
a360da3a 2891 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
b30f223b
RS
2892
2893 case REAL_CST:
a360da3a 2894 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
b30f223b
RS
2895
2896 case ADDR_EXPR:
fc0c675f
RK
2897 /* If we are taking the address of a external decl, it might be zero
2898 if it is weak, so we cannot optimize. */
2f939d94 2899 if (DECL_P (TREE_OPERAND (expr, 0))
fc0c675f
RK
2900 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2901 break;
2902
b30f223b 2903 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
a360da3a
JM
2904 return build (COMPOUND_EXPR, boolean_type_node,
2905 TREE_OPERAND (expr, 0), boolean_true_node);
b30f223b 2906 else
a360da3a 2907 return boolean_true_node;
b30f223b 2908
766f6c30 2909 case COMPLEX_EXPR:
f0b996c5 2910 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
b839fb3f 2911 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
f0b996c5
RS
2912 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2913 truthvalue_conversion (TREE_OPERAND (expr, 1)),
766f6c30
RS
2914 0);
2915
b30f223b
RS
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)))
a360da3a 2928 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
b30f223b
RS
2929 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2930 else
2931 return truthvalue_conversion (TREE_OPERAND (expr, 0));
b57062ca 2932
b30f223b
RS
2933 case COND_EXPR:
2934 /* Distribute the conversion into the arms of a COND_EXPR. */
a360da3a 2935 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
b30f223b
RS
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;
0f41302f 2945 /* fall through... */
b30f223b
RS
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
b30f223b 2953 case MINUS_EXPR:
f87550e0
JW
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;
0f41302f 2959 /* fall through... */
f87550e0 2960 case BIT_XOR_EXPR:
d7c83727 2961 /* This and MINUS_EXPR can be changed into a comparison of the
f87550e0 2962 two objects. */
b30f223b
RS
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);
e2aab13d 2971
fb48b1f0 2972 case BIT_AND_EXPR:
58cee643
RK
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;
fb48b1f0 2978
e2aab13d
RS
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;
b57062ca 2983
6d819282
MK
2984 default:
2985 break;
b30f223b
RS
2986 }
2987
f0b996c5 2988 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
f0b8d9aa
AS
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 }
f0b996c5 2998
b30f223b
RS
2999 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
3000}
3001\f
c8724862
DB
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. */
3012unsigned char *yy_cur, *yy_lim;
3013
3014#define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
4d9a1b48 3015#define UNGETC(c) ((c) == EOF ? 0 : yy_cur--)
c8724862
DB
3016
3017int
3018yy_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
3033char *
3034get_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
b30f223b
RS
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
05a81fe5
DE
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. */
b30f223b
RS
3113
3114char *
3115get_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. */
05a81fe5
DE
3156 if (looking_for == 0
3157 && (c == '\n' || c == EOF))
b30f223b
RS
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
38e01259 3177 one of these (or an EOF). */
b30f223b
RS
3178
3179 /* Handle backslash. */
3180 char_escaped = (c == '\\' && ! char_escaped);
3181 }
3182}
c8724862 3183#endif /* !USE_CPPLIB */
0b73773c
NH
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
3188tree
3932261a 3189c_build_qualified_type (type, type_quals)
0b73773c 3190 tree type;
3932261a 3191 int type_quals;
0b73773c 3192{
3932261a
MM
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
0b73773c 3208 if (TREE_CODE (type) == ARRAY_TYPE)
3932261a
MM
3209 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
3210 type_quals),
3ab1999b 3211 TYPE_DOMAIN (type));
3932261a
MM
3212 return build_qualified_type (type, type_quals);
3213}
3214
3215/* Apply the TYPE_QUALS to the new DECL. */
3216
3217void
3218c_apply_type_quals_to_decl (type_quals, decl)
3219 int type_quals;
3220 tree decl;
3221{
a6496605
MM
3222 if ((type_quals & TYPE_QUAL_CONST)
3223 || (TREE_TYPE (decl)
3224 && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE))
3932261a
MM
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 }
6946bc60 3231 if (type_quals & TYPE_QUAL_RESTRICT)
3932261a 3232 {
6946bc60
MM
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)
3932261a 3238 {
6946bc60
MM
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
3bdf5ad1 3247 HOST_WIDE_INT pointed_to_alias_set
6946bc60 3248 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
d125d268 3249
3bdf5ad1 3250 if (pointed_to_alias_set == 0)
6946bc60
MM
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 }
3932261a
MM
3259 }
3260 }
3261}
3262
41472af8
MM
3263
3264/* Return the typed-based alias set for T, which may be an expression
3bdf5ad1 3265 or a type. Return -1 if we don't do anything special. */
41472af8 3266
3bdf5ad1 3267HOST_WIDE_INT
41472af8
MM
3268c_get_alias_set (t)
3269 tree t;
3270{
08bc2431 3271 tree u;
41472af8 3272
08bc2431
MM
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;
ece32014 3285
3bdf5ad1
RK
3286 /* If this is a char *, the ANSI C standard says it can alias
3287 anything. */
3288 else if (TREE_CODE (t) == INDIRECT_REF
3289 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == INTEGER_TYPE
3290 && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))
3291 == TYPE_PRECISION (char_type_node)))
3292 return 0;
3932261a 3293
3bdf5ad1
RK
3294 /* That's all the expressions we handle specially. */
3295 if (! TYPE_P (t))
3296 return -1;
41472af8 3297
3bdf5ad1
RK
3298 if (TREE_CODE (t) == INTEGER_TYPE)
3299 {
41472af8
MM
3300 /* The C standard specifically allows aliasing between signed and
3301 unsigned variants of the same type. We treat the signed
3302 variant as canonical. */
3bdf5ad1 3303 tree signed_variant = signed_type (t);
41472af8 3304
3bdf5ad1 3305 if (signed_variant == signed_char_type_node)
41472af8
MM
3306 /* The C standard guarantess that any object may be accessed
3307 via an lvalue that has character type. We don't have to
3308 check for unsigned_char_type_node or char_type_node because
3309 we are specifically looking at the signed variant. */
3bdf5ad1
RK
3310 return 0;
3311 else if (signed_variant != t)
3312 return get_alias_set (signed_variant);
41472af8 3313 }
3bdf5ad1 3314 else if (POINTER_TYPE_P (t))
02af3af6 3315 {
3bdf5ad1 3316 tree t1;
02af3af6
MS
3317
3318 /* Unfortunately, there is no canonical form of a pointer type.
3319 In particular, if we have `typedef int I', then `int *', and
3320 `I *' are different types. So, we have to pick a canonical
3321 representative. We do this below.
d125d268 3322
b61148dd
MM
3323 Technically, this approach is actually more conservative that
3324 it needs to be. In particular, `const int *' and `int *'
3325 chould be in different alias sets, according to the C and C++
3326 standard, since their types are not the same, and so,
3327 technically, an `int **' and `const int **' cannot point at
3328 the same thing.
3329
3330 But, the standard is wrong. In particular, this code is
3331 legal C++:
3332
3333 int *ip;
3334 int **ipp = &ip;
3335 const int* const* cipp = &ip;
3336
3337 And, it doesn't make sense for that to be legal unless you
3338 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
3339 the pointed-to types. This issue has been reported to the
3340 C++ committee. */
3bdf5ad1
RK
3341 t1 = TYPE_MAIN_VARIANT (TREE_TYPE (t));
3342 t1 = ((TREE_CODE (t) == POINTER_TYPE)
3343 ? build_pointer_type (t1) : build_reference_type (t1));
3344 if (t1 != t)
3345 return get_alias_set (t1);
02af3af6 3346 }
ece32014 3347
3bdf5ad1 3348 return -1;
41472af8 3349}
7f4edbcb
BS
3350
3351/* Build tree nodes and builtin functions common to both C and C++ language
d125d268 3352 frontends.
7f4edbcb 3353 CPLUS_MODE is nonzero if we are called from the C++ frontend, we generate
d125d268 3354 some stricter prototypes in that case.
7f4edbcb
BS
3355 NO_BUILTINS and NO_NONANSI_BUILTINS contain the respective values of
3356 the language frontend flags flag_no_builtin and
3357 flag_no_nonansi_builtin. */
3bdf5ad1 3358
7f4edbcb
BS
3359void
3360c_common_nodes_and_builtins (cplus_mode, no_builtins, no_nonansi_builtins)
3361 int cplus_mode, no_builtins, no_nonansi_builtins;
3362{
3363 tree temp;
3364 tree memcpy_ftype, memset_ftype, strlen_ftype;
e3a709be 3365 tree bzero_ftype, bcmp_ftype;
7f4edbcb
BS
3366 tree endlink, int_endlink, double_endlink, unsigned_endlink;
3367 tree sizetype_endlink;
3368 tree ptr_ftype, ptr_ftype_unsigned;
4b2a62db 3369 tree void_ftype_any, void_ftype_int, int_ftype_any;
7f4edbcb
BS
3370 tree double_ftype_double, double_ftype_double_double;
3371 tree float_ftype_float, ldouble_ftype_ldouble;
3372 tree int_ftype_cptr_cptr_sizet;
3373 tree int_ftype_string_string, string_ftype_ptr_ptr;
3374 tree long_ftype_long;
3375 /* Either char* or void*. */
3376 tree traditional_ptr_type_node;
4b2a62db
KG
3377 /* Either const char* or const void*. */
3378 tree traditional_cptr_type_node;
3379 tree traditional_len_type_node;
3380 tree traditional_len_endlink;
9f720c3e 3381 tree va_list_ref_type_node;
daf68dd7 3382 tree va_list_arg_type_node;
d3707adb 3383
d3707adb
RH
3384 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
3385 va_list_type_node));
daf68dd7 3386
29ae8f10
GRK
3387 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"),
3388 ptrdiff_type_node));
3389
3390 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"),
3391 sizetype));
3392
daf68dd7 3393 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
9f720c3e
GK
3394 {
3395 va_list_arg_type_node = va_list_ref_type_node =
3396 build_pointer_type (TREE_TYPE (va_list_type_node));
3397 }
daf68dd7 3398 else
9f720c3e
GK
3399 {
3400 va_list_arg_type_node = va_list_type_node;
3401 va_list_ref_type_node = build_reference_type (va_list_type_node);
3402 }
3403
7f4edbcb
BS
3404 endlink = void_list_node;
3405 int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
3406 double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
3407 unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
3408
3409 ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
3410 ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
21318741 3411 sizetype_endlink = tree_cons (NULL_TREE, TYPE_DOMAIN (sizetype), endlink);
7f4edbcb
BS
3412 /* We realloc here because sizetype could be int or unsigned. S'ok. */
3413 ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
3414
4b2a62db 3415 int_ftype_any = build_function_type (integer_type_node, NULL_TREE);
7f4edbcb
BS
3416 void_ftype_any = build_function_type (void_type_node, NULL_TREE);
3417 void_ftype = build_function_type (void_type_node, endlink);
3418 void_ftype_int = build_function_type (void_type_node, int_endlink);
3419 void_ftype_ptr
3420 = build_function_type (void_type_node,
3421 tree_cons (NULL_TREE, ptr_type_node, endlink));
3422
3423 float_ftype_float
3424 = build_function_type (float_type_node,
3425 tree_cons (NULL_TREE, float_type_node, endlink));
3426
3427 double_ftype_double
3428 = build_function_type (double_type_node, double_endlink);
3429
3430 ldouble_ftype_ldouble
3431 = build_function_type (long_double_type_node,
3432 tree_cons (NULL_TREE, long_double_type_node,
3433 endlink));
3434
3435 double_ftype_double_double
3436 = build_function_type (double_type_node,
3437 tree_cons (NULL_TREE, double_type_node,
3438 double_endlink));
3439
3440 int_ftype_int
3441 = build_function_type (integer_type_node, int_endlink);
3442
3443 long_ftype_long
3444 = build_function_type (long_integer_type_node,
3445 tree_cons (NULL_TREE, long_integer_type_node,
3446 endlink));
3447
3448 int_ftype_cptr_cptr_sizet
3449 = build_function_type (integer_type_node,
3450 tree_cons (NULL_TREE, const_ptr_type_node,
3451 tree_cons (NULL_TREE, const_ptr_type_node,
3452 tree_cons (NULL_TREE,
3453 sizetype,
3454 endlink))));
3455
3456 /* Prototype for strcpy. */
3457 string_ftype_ptr_ptr
3458 = build_function_type (string_type_node,
3459 tree_cons (NULL_TREE, string_type_node,
3460 tree_cons (NULL_TREE,
3461 const_string_type_node,
3462 endlink)));
3463
4b2a62db
KG
3464 traditional_len_type_node = (flag_traditional && ! cplus_mode
3465 ? integer_type_node : sizetype);
3466 traditional_len_endlink = tree_cons (NULL_TREE, traditional_len_type_node,
3467 endlink);
3468
7f4edbcb
BS
3469 /* Prototype for strcmp. */
3470 int_ftype_string_string
3471 = build_function_type (integer_type_node,
3472 tree_cons (NULL_TREE, const_string_type_node,
3473 tree_cons (NULL_TREE,
3474 const_string_type_node,
3475 endlink)));
3476
3477 /* Prototype for strlen. */
3478 strlen_ftype
4b2a62db 3479 = build_function_type (traditional_len_type_node,
7f4edbcb
BS
3480 tree_cons (NULL_TREE, const_string_type_node,
3481 endlink));
3482
3483 traditional_ptr_type_node = (flag_traditional && ! cplus_mode
3484 ? string_type_node : ptr_type_node);
4b2a62db
KG
3485 traditional_cptr_type_node = (flag_traditional && ! cplus_mode
3486 ? const_string_type_node : const_ptr_type_node);
7f4edbcb
BS
3487
3488 /* Prototype for memcpy. */
3489 memcpy_ftype
3490 = build_function_type (traditional_ptr_type_node,
3491 tree_cons (NULL_TREE, ptr_type_node,
3492 tree_cons (NULL_TREE, const_ptr_type_node,
3493 sizetype_endlink)));
3494
3495 /* Prototype for memset. */
3496 memset_ftype
3497 = build_function_type (traditional_ptr_type_node,
3498 tree_cons (NULL_TREE, ptr_type_node,
3499 tree_cons (NULL_TREE, integer_type_node,
3500 tree_cons (NULL_TREE,
3501 sizetype,
3502 endlink))));
3503
e3a709be
KG
3504 /* Prototype for bzero. */
3505 bzero_ftype
3506 = build_function_type (void_type_node,
3507 tree_cons (NULL_TREE, traditional_ptr_type_node,
3508 traditional_len_endlink));
3509
4b2a62db
KG
3510 /* Prototype for bcmp. */
3511 bcmp_ftype
3512 = build_function_type (integer_type_node,
3513 tree_cons (NULL_TREE, traditional_cptr_type_node,
3514 tree_cons (NULL_TREE,
3515 traditional_cptr_type_node,
3516 traditional_len_endlink)));
3517
7f4edbcb 3518 builtin_function ("__builtin_constant_p", default_function_type,
26db82d8 3519 BUILT_IN_CONSTANT_P, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
3520
3521 builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
26db82d8 3522 BUILT_IN_RETURN_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
3523
3524 builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
26db82d8 3525 BUILT_IN_FRAME_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
3526
3527 builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
26db82d8
BS
3528 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, "alloca");
3529 builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS,
3530 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
3531 /* Define alloca, ffs as builtins.
3532 Declare _exit just to mark it as volatile. */
3533 if (! no_builtins && ! no_nonansi_builtins)
3534 {
512b62fb 3535#ifndef SMALL_STACK
7f4edbcb 3536 temp = builtin_function ("alloca", ptr_ftype_sizetype,
26db82d8 3537 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
3538 /* Suppress error if redefined as a non-function. */
3539 DECL_BUILT_IN_NONANSI (temp) = 1;
512b62fb 3540#endif
26db82d8
BS
3541 temp = builtin_function ("ffs", int_ftype_int, BUILT_IN_FFS,
3542 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
3543 /* Suppress error if redefined as a non-function. */
3544 DECL_BUILT_IN_NONANSI (temp) = 1;
3545 temp = builtin_function ("_exit", void_ftype_int,
26db82d8 3546 0, NOT_BUILT_IN, NULL_PTR);
7f4edbcb
BS
3547 TREE_THIS_VOLATILE (temp) = 1;
3548 TREE_SIDE_EFFECTS (temp) = 1;
3549 /* Suppress error if redefined as a non-function. */
3550 DECL_BUILT_IN_NONANSI (temp) = 1;
4b2a62db 3551
dc1c86cb
KG
3552 /* The system prototypes for these functions have many
3553 variations, so don't specify parameters to avoid conflicts.
3554 The expand_* functions check the argument types anyway. */
3555 temp = builtin_function ("bzero", void_ftype_any,
e3a709be
KG
3556 BUILT_IN_BZERO, BUILT_IN_NORMAL, NULL_PTR);
3557 DECL_BUILT_IN_NONANSI (temp) = 1;
dc1c86cb 3558 temp = builtin_function ("bcmp", int_ftype_any,
4b2a62db
KG
3559 BUILT_IN_BCMP, BUILT_IN_NORMAL, NULL_PTR);
3560 DECL_BUILT_IN_NONANSI (temp) = 1;
7f4edbcb
BS
3561 }
3562
26db82d8
BS
3563 builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS,
3564 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3565 builtin_function ("__builtin_fabsf", float_ftype_float, BUILT_IN_FABS,
26db82d8 3566 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3567 builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS,
26db82d8 3568 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3569 builtin_function ("__builtin_fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
26db82d8 3570 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3571 builtin_function ("__builtin_labs", long_ftype_long, BUILT_IN_LABS,
26db82d8 3572 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3573 builtin_function ("__builtin_saveregs", ptr_ftype, BUILT_IN_SAVEREGS,
26db82d8 3574 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3575 builtin_function ("__builtin_classify_type", default_function_type,
26db82d8 3576 BUILT_IN_CLASSIFY_TYPE, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3577 builtin_function ("__builtin_next_arg", ptr_ftype, BUILT_IN_NEXT_ARG,
26db82d8 3578 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3579 builtin_function ("__builtin_args_info", int_ftype_int, BUILT_IN_ARGS_INFO,
26db82d8 3580 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
3581 builtin_function ("__builtin_setjmp",
3582 build_function_type (integer_type_node,
3583 tree_cons (NULL_TREE, ptr_type_node,
3584 endlink)),
26db82d8 3585 BUILT_IN_SETJMP, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
3586 builtin_function ("__builtin_longjmp",
3587 build_function_type (void_type_node,
3588 tree_cons (NULL_TREE, ptr_type_node,
3589 tree_cons (NULL_TREE,
3590 integer_type_node,
3591 endlink))),
26db82d8
BS
3592 BUILT_IN_LONGJMP, BUILT_IN_NORMAL, NULL_PTR);
3593 builtin_function ("__builtin_trap", void_ftype, BUILT_IN_TRAP,
3594 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3595
1eb8759b
RH
3596 /* ISO C99 IEEE Unordered compares. */
3597 builtin_function ("__builtin_isgreater", default_function_type,
3598 BUILT_IN_ISGREATER, BUILT_IN_NORMAL, NULL_PTR);
3599 builtin_function ("__builtin_isgreaterequal", default_function_type,
3600 BUILT_IN_ISGREATEREQUAL, BUILT_IN_NORMAL, NULL_PTR);
3601 builtin_function ("__builtin_isless", default_function_type,
3602 BUILT_IN_ISLESS, BUILT_IN_NORMAL, NULL_PTR);
3603 builtin_function ("__builtin_islessequal", default_function_type,
3604 BUILT_IN_ISLESSEQUAL, BUILT_IN_NORMAL, NULL_PTR);
3605 builtin_function ("__builtin_islessgreater", default_function_type,
3606 BUILT_IN_ISLESSGREATER, BUILT_IN_NORMAL, NULL_PTR);
3607 builtin_function ("__builtin_isunordered", default_function_type,
3608 BUILT_IN_ISUNORDERED, BUILT_IN_NORMAL, NULL_PTR);
3609
7f4edbcb
BS
3610 /* Untyped call and return. */
3611 builtin_function ("__builtin_apply_args", ptr_ftype,
26db82d8 3612 BUILT_IN_APPLY_ARGS, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
3613
3614 temp = tree_cons (NULL_TREE,
3615 build_pointer_type (build_function_type (void_type_node,
3616 NULL_TREE)),
3617 tree_cons (NULL_TREE,
3618 ptr_type_node,
3619 tree_cons (NULL_TREE,
3620 sizetype,
3621 endlink)));
3622 builtin_function ("__builtin_apply",
3623 build_function_type (ptr_type_node, temp),
26db82d8 3624 BUILT_IN_APPLY, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3625 builtin_function ("__builtin_return", void_ftype_ptr,
26db82d8 3626 BUILT_IN_RETURN, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3627
d3707adb
RH
3628 /* Support for varargs.h and stdarg.h. */
3629 builtin_function ("__builtin_varargs_start",
3630 build_function_type (void_type_node,
3631 tree_cons (NULL_TREE,
9f720c3e 3632 va_list_ref_type_node,
d3707adb 3633 endlink)),
26db82d8 3634 BUILT_IN_VARARGS_START, BUILT_IN_NORMAL, NULL_PTR);
d3707adb
RH
3635
3636 builtin_function ("__builtin_stdarg_start",
3637 build_function_type (void_type_node,
3638 tree_cons (NULL_TREE,
9f720c3e 3639 va_list_ref_type_node,
d3707adb 3640 NULL_TREE)),
26db82d8 3641 BUILT_IN_STDARG_START, BUILT_IN_NORMAL, NULL_PTR);
d3707adb
RH
3642
3643 builtin_function ("__builtin_va_end",
3644 build_function_type (void_type_node,
3645 tree_cons (NULL_TREE,
9f720c3e 3646 va_list_ref_type_node,
d3707adb 3647 endlink)),
26db82d8 3648 BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL_PTR);
d3707adb
RH
3649
3650 builtin_function ("__builtin_va_copy",
3651 build_function_type (void_type_node,
3652 tree_cons (NULL_TREE,
9f720c3e 3653 va_list_ref_type_node,
d3707adb 3654 tree_cons (NULL_TREE,
daf68dd7 3655 va_list_arg_type_node,
d3707adb 3656 endlink))),
26db82d8 3657 BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL_PTR);
d3707adb 3658
994a57cd
RH
3659 /* ??? Ought to be `T __builtin_expect(T, T)' for any type T. */
3660 builtin_function ("__builtin_expect",
3661 build_function_type (long_integer_type_node,
3662 tree_cons (NULL_TREE,
3663 long_integer_type_node,
3664 tree_cons (NULL_TREE,
3665 long_integer_type_node,
3666 endlink))),
3667 BUILT_IN_EXPECT, BUILT_IN_NORMAL, NULL_PTR);
3668
7f4edbcb
BS
3669 /* Currently under experimentation. */
3670 builtin_function ("__builtin_memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
26db82d8 3671 BUILT_IN_NORMAL, "memcpy");
7f4edbcb 3672 builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
26db82d8 3673 BUILT_IN_MEMCMP, BUILT_IN_NORMAL, "memcmp");
e3a709be
KG
3674 builtin_function ("__builtin_memset", memset_ftype,
3675 BUILT_IN_MEMSET, BUILT_IN_NORMAL, "memset");
3676 builtin_function ("__builtin_bzero", bzero_ftype,
3677 BUILT_IN_BZERO, BUILT_IN_NORMAL, "bzero");
4b2a62db
KG
3678 builtin_function ("__builtin_bcmp", bcmp_ftype,
3679 BUILT_IN_BCMP, BUILT_IN_NORMAL, "bcmp");
7f4edbcb 3680 builtin_function ("__builtin_strcmp", int_ftype_string_string,
26db82d8 3681 BUILT_IN_STRCMP, BUILT_IN_NORMAL, "strcmp");
7f4edbcb 3682 builtin_function ("__builtin_strcpy", string_ftype_ptr_ptr,
26db82d8 3683 BUILT_IN_STRCPY, BUILT_IN_NORMAL, "strcpy");
7f4edbcb 3684 builtin_function ("__builtin_strlen", strlen_ftype,
26db82d8 3685 BUILT_IN_STRLEN, BUILT_IN_NORMAL, "strlen");
d125d268 3686 builtin_function ("__builtin_sqrtf", float_ftype_float,
26db82d8 3687 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtf");
7f4edbcb 3688 builtin_function ("__builtin_fsqrt", double_ftype_double,
26db82d8 3689 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrt");
7f4edbcb 3690 builtin_function ("__builtin_sqrtl", ldouble_ftype_ldouble,
26db82d8 3691 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtl");
d125d268 3692 builtin_function ("__builtin_sinf", float_ftype_float,
26db82d8 3693 BUILT_IN_SIN, BUILT_IN_NORMAL, "sinf");
d125d268 3694 builtin_function ("__builtin_sin", double_ftype_double,
26db82d8 3695 BUILT_IN_SIN, BUILT_IN_NORMAL, "sin");
d125d268 3696 builtin_function ("__builtin_sinl", ldouble_ftype_ldouble,
26db82d8 3697 BUILT_IN_SIN, BUILT_IN_NORMAL, "sinl");
d125d268 3698 builtin_function ("__builtin_cosf", float_ftype_float,
26db82d8 3699 BUILT_IN_COS, BUILT_IN_NORMAL, "cosf");
d125d268 3700 builtin_function ("__builtin_cos", double_ftype_double,
26db82d8 3701 BUILT_IN_COS, BUILT_IN_NORMAL, "cos");
d125d268 3702 builtin_function ("__builtin_cosl", ldouble_ftype_ldouble,
26db82d8 3703 BUILT_IN_COS, BUILT_IN_NORMAL, "cosl");
7f4edbcb
BS
3704
3705 if (! no_builtins)
3706 {
26db82d8
BS
3707 builtin_function ("abs", int_ftype_int, BUILT_IN_ABS,
3708 BUILT_IN_NORMAL, NULL_PTR);
3709 builtin_function ("fabsf", float_ftype_float, BUILT_IN_FABS,
3710 BUILT_IN_NORMAL, NULL_PTR);
3711 builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS,
3712 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3713 builtin_function ("fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
26db82d8
BS
3714 BUILT_IN_NORMAL, NULL_PTR);
3715 builtin_function ("labs", long_ftype_long, BUILT_IN_LABS,
3716 BUILT_IN_NORMAL, NULL_PTR);
3717 builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
3718 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3719 builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP,
26db82d8
BS
3720 BUILT_IN_NORMAL, NULL_PTR);
3721 builtin_function ("memset", memset_ftype, BUILT_IN_MEMSET,
3722 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3723 builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP,
26db82d8 3724 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3725 builtin_function ("strcpy", string_ftype_ptr_ptr, BUILT_IN_STRCPY,
26db82d8
BS
3726 BUILT_IN_NORMAL, NULL_PTR);
3727 builtin_function ("strlen", strlen_ftype, BUILT_IN_STRLEN,
3728 BUILT_IN_NORMAL, NULL_PTR);
3729 builtin_function ("sqrtf", float_ftype_float, BUILT_IN_FSQRT,
3730 BUILT_IN_NORMAL, NULL_PTR);
3731 builtin_function ("sqrt", double_ftype_double, BUILT_IN_FSQRT,
3732 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3733 builtin_function ("sqrtl", ldouble_ftype_ldouble, BUILT_IN_FSQRT,
26db82d8
BS
3734 BUILT_IN_NORMAL, NULL_PTR);
3735 builtin_function ("sinf", float_ftype_float, BUILT_IN_SIN,
3736 BUILT_IN_NORMAL, NULL_PTR);
3737 builtin_function ("sin", double_ftype_double, BUILT_IN_SIN,
3738 BUILT_IN_NORMAL, NULL_PTR);
3739 builtin_function ("sinl", ldouble_ftype_ldouble, BUILT_IN_SIN,
3740 BUILT_IN_NORMAL, NULL_PTR);
3741 builtin_function ("cosf", float_ftype_float, BUILT_IN_COS,
3742 BUILT_IN_NORMAL, NULL_PTR);
3743 builtin_function ("cos", double_ftype_double, BUILT_IN_COS,
3744 BUILT_IN_NORMAL, NULL_PTR);
3745 builtin_function ("cosl", ldouble_ftype_ldouble, BUILT_IN_COS,
3746 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
3747
3748 /* Declare these functions volatile
3749 to avoid spurious "control drops through" warnings. */
3750 temp = builtin_function ("abort", cplus_mode ? void_ftype : void_ftype_any,
26db82d8 3751 0, NOT_BUILT_IN, NULL_PTR);
7f4edbcb
BS
3752 TREE_THIS_VOLATILE (temp) = 1;
3753 TREE_SIDE_EFFECTS (temp) = 1;
3754
3755#if 0 /* ??? The C++ frontend used to do this. */
3756 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
3757 them... */
3758 DECL_BUILT_IN_NONANSI (temp) = 1;
3759#endif
3760 temp = builtin_function ("exit",
3761 cplus_mode ? void_ftype_int : void_ftype_any,
26db82d8 3762 0, NOT_BUILT_IN, NULL_PTR);
7f4edbcb
BS
3763 TREE_THIS_VOLATILE (temp) = 1;
3764 TREE_SIDE_EFFECTS (temp) = 1;
3765
3766#if 0 /* ??? The C++ frontend used to do this. */
3767 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
3768 them... */
3769 DECL_BUILT_IN_NONANSI (temp) = 1;
3770#endif
3771 }
3772
3773#if 0
3774 /* Support for these has not been written in either expand_builtin
3775 or build_function_call. */
26db82d8
BS
3776 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV,
3777 BUILT_IN_NORMAL, NULL_PTR);
3778 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV,
3779 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3780 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
26db82d8 3781 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3782 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
26db82d8 3783 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3784 builtin_function ("__builtin_fmod", double_ftype_double_double,
26db82d8 3785 BUILT_IN_FMOD, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3786 builtin_function ("__builtin_frem", double_ftype_double_double,
26db82d8 3787 BUILT_IN_FREM, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3788 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
26db82d8 3789 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3790 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
26db82d8 3791 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3792#endif
c530479e
RH
3793
3794 /* ??? Perhaps there's a better place to do this. But it is related
3795 to __builtin_va_arg, so it isn't that off-the-wall. */
3796 lang_type_promotes_to = simple_type_promotes_to;
7f4edbcb 3797}
d3707adb
RH
3798
3799tree
3800build_va_arg (expr, type)
3801 tree expr, type;
3802{
3803 return build1 (VA_ARG_EXPR, type, expr);
3804}
c530479e
RH
3805\f
3806/* Given a type, apply default promotions wrt unnamed function arguments
3807 and return the new type. Return NULL_TREE if no change. */
d125d268 3808/* ??? There is a function of the same name in the C++ front end that
c530479e 3809 does something similar, but is more thorough and does not return NULL
d125d268 3810 if no change. We could perhaps share code, but it would make the
c530479e
RH
3811 self_promoting_type property harder to identify. */
3812
3813tree
3814simple_type_promotes_to (type)
3815 tree type;
3816{
3817 if (TYPE_MAIN_VARIANT (type) == float_type_node)
3818 return double_type_node;
3819
3820 if (C_PROMOTING_INTEGER_TYPE_P (type))
3821 {
3822 /* Traditionally, unsignedness is preserved in default promotions.
3823 Also preserve unsignedness if not really getting any wider. */
3824 if (TREE_UNSIGNED (type)
3825 && (flag_traditional
3826 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
3827 return unsigned_type_node;
3828 return integer_type_node;
3829 }
3830
3831 return NULL_TREE;
3832}
3833
3834/* Return 1 if PARMS specifies a fixed number of parameters
3835 and none of their types is affected by default promotions. */
3836
3837int
3838self_promoting_args_p (parms)
3839 tree parms;
3840{
3841 register tree t;
3842 for (t = parms; t; t = TREE_CHAIN (t))
3843 {
3844 register tree type = TREE_VALUE (t);
7e8176d7 3845
c530479e
RH
3846 if (TREE_CHAIN (t) == 0 && type != void_type_node)
3847 return 0;
3848
3849 if (type == 0)
3850 return 0;
3851
3852 if (TYPE_MAIN_VARIANT (type) == float_type_node)
3853 return 0;
3854
3855 if (C_PROMOTING_INTEGER_TYPE_P (type))
3856 return 0;
3857 }
3858 return 1;
3859}
5eda3d66
RH
3860
3861/* Recognize certain built-in functions so we can make tree-codes
3862 other than CALL_EXPR. We do this when it enables fold-const.c
3863 to do something useful. */
3864/* ??? By rights this should go in builtins.c, but only C and C++
3865 implement build_{binary,unary}_op. Not exactly sure what bits
3866 of functionality are actually needed from those functions, or
3867 where the similar functionality exists in the other front ends. */
3868
3869tree
3870expand_tree_builtin (function, params, coerced_params)
3871 tree function, params, coerced_params;
3872{
3873 enum tree_code code;
3874
3875 if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL)
3876 return NULL_TREE;
3877
3878 switch (DECL_FUNCTION_CODE (function))
3879 {
3880 case BUILT_IN_ABS:
3881 case BUILT_IN_LABS:
3882 case BUILT_IN_FABS:
3883 if (coerced_params == 0)
3884 return integer_zero_node;
3885 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
3886
3887 case BUILT_IN_ISGREATER:
3888 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3889 code = UNLE_EXPR;
3890 else
3891 code = LE_EXPR;
3892 goto unordered_cmp;
3893
3894 case BUILT_IN_ISGREATEREQUAL:
3895 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3896 code = UNLT_EXPR;
3897 else
3898 code = LT_EXPR;
3899 goto unordered_cmp;
3900
3901 case BUILT_IN_ISLESS:
3902 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3903 code = UNGE_EXPR;
3904 else
3905 code = GE_EXPR;
3906 goto unordered_cmp;
3907
3908 case BUILT_IN_ISLESSEQUAL:
3909 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3910 code = UNGT_EXPR;
3911 else
3912 code = GT_EXPR;
3913 goto unordered_cmp;
3914
3915 case BUILT_IN_ISLESSGREATER:
3916 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3917 code = UNEQ_EXPR;
3918 else
3919 code = EQ_EXPR;
3920 goto unordered_cmp;
3921
3922 case BUILT_IN_ISUNORDERED:
3923 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
3924 return integer_zero_node;
3925 code = UNORDERED_EXPR;
3926 goto unordered_cmp;
3927
3928 unordered_cmp:
3929 {
3930 tree arg0, arg1;
3931
3932 if (params == 0
3933 || TREE_CHAIN (params) == 0)
3934 {
3935 error ("too few arguments to function `%s'",
3936 IDENTIFIER_POINTER (DECL_NAME (function)));
3937 return error_mark_node;
3938 }
3939 else if (TREE_CHAIN (TREE_CHAIN (params)) != 0)
3940 {
3941 error ("too many arguments to function `%s'",
3942 IDENTIFIER_POINTER (DECL_NAME (function)));
3943 return error_mark_node;
3944 }
3945
3946 arg0 = TREE_VALUE (params);
3947 arg1 = TREE_VALUE (TREE_CHAIN (params));
3948 arg0 = build_binary_op (code, arg0, arg1, 0);
3949 if (code != UNORDERED_EXPR)
3950 arg0 = build_unary_op (TRUTH_NOT_EXPR, arg0, 0);
3951 return arg0;
3952 }
3953 break;
3954
3955 default:
3956 break;
3957 }
3958
3959 return NULL_TREE;
3960}