]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-common.c
arm.c (thumb_expand_movstrqi): Rewrite to support SSA form.
[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 24#include "tree.h"
b30f223b 25#include "flags.h"
5f6da302 26#include "toplev.h"
d6f4ec51 27#include "output.h"
e2af664c 28#include "c-pragma.h"
3932261a 29#include "rtl.h"
1526a060 30#include "ggc.h"
c6991660 31#include "expr.h"
8f17b5c5 32#include "c-common.h"
7bdb32b9 33#include "tm_p.h"
6a357625 34#include "intl.h"
3f80e6ac 35#include "diagnostic.h"
ccd043a9 36
c8724862
DB
37#if USE_CPPLIB
38#include "cpplib.h"
39cpp_reader parse_in;
c8724862
DB
40#endif
41
12a39b12
JM
42#undef WCHAR_TYPE_SIZE
43#define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node)
fef610be 44
7f4edbcb 45/* The following symbols are subsumed in the c_global_trees array, and
d125d268 46 listed here individually for documentation purposes.
7f4edbcb
BS
47
48 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
49
50 tree short_integer_type_node;
51 tree long_integer_type_node;
52 tree long_long_integer_type_node;
53
54 tree short_unsigned_type_node;
55 tree long_unsigned_type_node;
56 tree long_long_unsigned_type_node;
57
58 tree boolean_type_node;
59 tree boolean_false_node;
60 tree boolean_true_node;
61
62 tree ptrdiff_type_node;
63
64 tree unsigned_char_type_node;
65 tree signed_char_type_node;
66 tree wchar_type_node;
67 tree signed_wchar_type_node;
68 tree unsigned_wchar_type_node;
69
70 tree float_type_node;
71 tree double_type_node;
72 tree long_double_type_node;
73
74 tree complex_integer_type_node;
75 tree complex_float_type_node;
76 tree complex_double_type_node;
77 tree complex_long_double_type_node;
78
79 tree intQI_type_node;
80 tree intHI_type_node;
81 tree intSI_type_node;
82 tree intDI_type_node;
83 tree intTI_type_node;
84
85 tree unsigned_intQI_type_node;
86 tree unsigned_intHI_type_node;
87 tree unsigned_intSI_type_node;
88 tree unsigned_intDI_type_node;
89 tree unsigned_intTI_type_node;
90
91 tree widest_integer_literal_type_node;
92 tree widest_unsigned_literal_type_node;
93
94 Nodes for types `void *' and `const void *'.
95
96 tree ptr_type_node, const_ptr_type_node;
97
98 Nodes for types `char *' and `const char *'.
99
100 tree string_type_node, const_string_type_node;
101
102 Type `char[SOMENUMBER]'.
103 Used when an array of char is needed and the size is irrelevant.
104
105 tree char_array_type_node;
106
107 Type `int[SOMENUMBER]' or something like it.
108 Used when an array of int needed and the size is irrelevant.
109
110 tree int_array_type_node;
111
112 Type `wchar_t[SOMENUMBER]' or something like it.
113 Used when a wide string literal is created.
114
115 tree wchar_array_type_node;
116
117 Type `int ()' -- used for implicit declaration of functions.
118
119 tree default_function_type;
120
121 Function types `int (int)', etc.
122
123 tree int_ftype_int;
124 tree void_ftype;
125 tree void_ftype_ptr;
126 tree int_ftype_int;
127 tree ptr_ftype_sizetype;
128
129 A VOID_TYPE node, packaged in a TREE_LIST.
130
131 tree void_list_node;
132
63ad61ed
ZW
133 The identifiers __FUNCTION__, __PRETTY_FUNCTION__, and __func__.
134
135 tree function_id_node;
136 tree pretty_function_id_node;
137 tree func_id_node;
138
7f4edbcb
BS
139*/
140
141tree c_global_trees[CTI_MAX];
0b73773c 142
f09f1de5
MM
143/* The elements of `ridpointers' are identifier nodes for the reserved
144 type names and storage classes. It is indexed by a RID_... value. */
145tree *ridpointers;
146
2ce07e2d
NS
147tree (*make_fname_decl) PARAMS ((tree, const char *, int));
148
ae499cce
MM
149/* If non-NULL, the address of a language-specific function that
150 returns 1 for language-specific statement codes. */
151int (*lang_statement_code_p) PARAMS ((enum tree_code));
152
8f17b5c5
MM
153/* If non-NULL, the address of a language-specific function that takes
154 any action required right before expand_function_end is called. */
155void (*lang_expand_function_end) PARAMS ((void));
156
e78a3b42
RK
157/* Nonzero means the expression being parsed will never be evaluated.
158 This is a count, since unevaluated expressions can nest. */
159int skip_evaluation;
160
2786cbad 161enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
7d384cc0 162 A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
bbb1ae01 163 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
a157febd 164 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS, A_MALLOC,
2a8f6b90 165 A_NO_LIMIT_STACK, A_PURE};
53065596 166
bb72a084
PE
167enum format_type { printf_format_type, scanf_format_type,
168 strftime_format_type };
169
6e090c76
KG
170static void add_attribute PARAMS ((enum attrs, const char *,
171 int, int, int));
172static void init_attributes PARAMS ((void));
173static void record_function_format PARAMS ((tree, tree, enum format_type,
174 int, int));
175static void record_international_format PARAMS ((tree, tree, int));
6e090c76 176static int default_valid_lang_attribute PARAMS ((tree, tree, tree, tree));
4724b3de 177
0a7ed33c
BS
178/* Keep a stack of if statements. We record the number of compound
179 statements seen up to the if keyword, as well as the line number
180 and file of the if. If a potentially ambiguous else is seen, that
181 fact is recorded; the warning is issued when we can be sure that
182 the enclosing if statement does not have an else branch. */
183typedef struct
184{
185 int compstmt_count;
186 int line;
dff01034 187 const char *file;
0a7ed33c 188 int needs_warning;
8f17b5c5 189 tree if_stmt;
0a7ed33c
BS
190} if_elt;
191
192static if_elt *if_stack;
6d819282
MK
193
194/* Amount of space in the if statement stack. */
195static int if_stack_space = 0;
196
197/* Stack pointer. */
198static int if_stack_pointer = 0;
199
8f17b5c5 200/* Record the start of an if-then, and record the start of it
0a7ed33c
BS
201 for ambiguous else detection. */
202
6d819282 203void
8f17b5c5 204c_expand_start_cond (cond, compstmt_count)
6d819282 205 tree cond;
6d819282
MK
206 int compstmt_count;
207{
8f17b5c5
MM
208 tree if_stmt;
209
6d819282
MK
210 /* Make sure there is enough space on the stack. */
211 if (if_stack_space == 0)
212 {
213 if_stack_space = 10;
0a7ed33c 214 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
6d819282
MK
215 }
216 else if (if_stack_space == if_stack_pointer)
217 {
218 if_stack_space += 10;
0a7ed33c 219 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
6d819282 220 }
0a7ed33c 221
8f17b5c5
MM
222 if_stmt = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
223 IF_COND (if_stmt) = cond;
224 add_stmt (if_stmt);
225
6d819282 226 /* Record this if statement. */
0a7ed33c
BS
227 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
228 if_stack[if_stack_pointer].file = input_filename;
229 if_stack[if_stack_pointer].line = lineno;
230 if_stack[if_stack_pointer].needs_warning = 0;
8f17b5c5 231 if_stack[if_stack_pointer].if_stmt = if_stmt;
0a7ed33c 232 if_stack_pointer++;
8f17b5c5 233}
6d819282 234
8f17b5c5
MM
235/* Called after the then-clause for an if-statement is processed. */
236
237void
238c_finish_then ()
239{
240 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
241 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
6d819282
MK
242}
243
8f17b5c5 244/* Record the end of an if-then. Optionally warn if a nested
0a7ed33c
BS
245 if statement had an ambiguous else clause. */
246
6d819282
MK
247void
248c_expand_end_cond ()
249{
250 if_stack_pointer--;
0a7ed33c
BS
251 if (if_stack[if_stack_pointer].needs_warning)
252 warning_with_file_and_line (if_stack[if_stack_pointer].file,
253 if_stack[if_stack_pointer].line,
254 "suggest explicit braces to avoid ambiguous `else'");
8f17b5c5 255 last_expr_type = NULL_TREE;
6d819282
MK
256}
257
8f17b5c5 258/* Called between the then-clause and the else-clause
0a7ed33c
BS
259 of an if-then-else. */
260
6d819282
MK
261void
262c_expand_start_else ()
263{
0a7ed33c
BS
264 /* An ambiguous else warning must be generated for the enclosing if
265 statement, unless we see an else branch for that one, too. */
6d819282
MK
266 if (warn_parentheses
267 && if_stack_pointer > 1
0a7ed33c
BS
268 && (if_stack[if_stack_pointer - 1].compstmt_count
269 == if_stack[if_stack_pointer - 2].compstmt_count))
270 if_stack[if_stack_pointer - 2].needs_warning = 1;
271
272 /* Even if a nested if statement had an else branch, it can't be
273 ambiguous if this one also has an else. So don't warn in that
274 case. Also don't warn for any if statements nested in this else. */
275 if_stack[if_stack_pointer - 1].needs_warning = 0;
276 if_stack[if_stack_pointer - 1].compstmt_count--;
8f17b5c5
MM
277}
278
279/* Called after the else-clause for an if-statement is processed. */
6d819282 280
8f17b5c5
MM
281void
282c_finish_else ()
283{
284 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
285 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
6d819282
MK
286}
287
6f4d7222 288/* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__. */
7da551a2
RS
289
290void
291declare_function_name ()
292{
dff01034 293 const char *name, *printable_name;
7da551a2
RS
294
295 if (current_function_decl == NULL)
296 {
297 name = "";
298 printable_name = "top level";
299 }
300 else
301 {
6152f876
RS
302 /* Allow functions to be nameless (such as artificial ones). */
303 if (DECL_NAME (current_function_decl))
304 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
305 else
306 name = "";
a1d7ffe3 307 printable_name = (*decl_printable_name) (current_function_decl, 2);
63ad61ed
ZW
308
309 /* ISO C99 defines __func__, which is a variable, not a string
310 constant, and which is not a defined symbol at file scope. */
311 (*make_fname_decl) (func_id_node, name, 0);
7da551a2 312 }
2ce07e2d 313
63ad61ed
ZW
314 (*make_fname_decl) (function_id_node, name, 0);
315 (*make_fname_decl) (pretty_function_id_node, printable_name, 1);
7da551a2
RS
316}
317
b30f223b
RS
318/* Given a chain of STRING_CST nodes,
319 concatenate them into one STRING_CST
320 and give it a suitable array-of-chars data type. */
321
322tree
323combine_strings (strings)
324 tree strings;
325{
326 register tree value, t;
327 register int length = 1;
328 int wide_length = 0;
329 int wide_flag = 0;
330 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
331 int nchars;
1326a48b 332 const int nchars_max = flag_isoc99 ? 4095 : 509;
b30f223b
RS
333
334 if (TREE_CHAIN (strings))
335 {
336 /* More than one in the chain, so concatenate. */
337 register char *p, *q;
338
339 /* Don't include the \0 at the end of each substring,
340 except for the last one.
341 Count wide strings and ordinary strings separately. */
342 for (t = strings; t; t = TREE_CHAIN (t))
343 {
344 if (TREE_TYPE (t) == wchar_array_type_node)
345 {
346 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
347 wide_flag = 1;
348 }
349 else
350 length += (TREE_STRING_LENGTH (t) - 1);
351 }
352
353 /* If anything is wide, the non-wides will be converted,
354 which makes them take more space. */
355 if (wide_flag)
356 length = length * wchar_bytes + wide_length;
357
4dd7201e 358 p = ggc_alloc_string (NULL, length);
b30f223b
RS
359
360 /* Copy the individual strings into the new combined string.
361 If the combined string is wide, convert the chars to ints
362 for any individual strings that are not wide. */
363
364 q = p;
365 for (t = strings; t; t = TREE_CHAIN (t))
366 {
367 int len = (TREE_STRING_LENGTH (t)
368 - ((TREE_TYPE (t) == wchar_array_type_node)
369 ? wchar_bytes : 1));
370 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
371 {
7e2231e7 372 memcpy (q, TREE_STRING_POINTER (t), len);
b30f223b
RS
373 q += len;
374 }
375 else
376 {
377 int i;
378 for (i = 0; i < len; i++)
41bbd14e
JW
379 {
380 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
381 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
382 else
383 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
384 }
b30f223b
RS
385 q += len * wchar_bytes;
386 }
387 }
388 if (wide_flag)
389 {
390 int i;
391 for (i = 0; i < wchar_bytes; i++)
392 *q++ = 0;
393 }
394 else
395 *q = 0;
396
397 value = make_node (STRING_CST);
398 TREE_STRING_POINTER (value) = p;
399 TREE_STRING_LENGTH (value) = length;
b30f223b
RS
400 }
401 else
402 {
403 value = strings;
404 length = TREE_STRING_LENGTH (value);
405 if (TREE_TYPE (value) == wchar_array_type_node)
406 wide_flag = 1;
407 }
408
b57062ca 409 /* Compute the number of elements, for the array type. */
b30f223b
RS
410 nchars = wide_flag ? length / wchar_bytes : length;
411
1326a48b 412 if (pedantic && nchars > nchars_max)
89abf8d1
JM
413 pedwarn ("string length `%d' is greater than the minimum length `%d' ISO C%d is required to support",
414 nchars, nchars_max, flag_isoc99 ? 99 : 89);
1326a48b 415
b30f223b
RS
416 /* Create the array type for the string constant.
417 -Wwrite-strings says make the string constant an array of const char
d9cf7c82
JM
418 so that copying it to a non-const pointer will get a warning.
419 For C++, this is the standard behavior. */
420 if (flag_const_strings
b30f223b
RS
421 && (! flag_traditional && ! flag_writable_strings))
422 {
423 tree elements
424 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
425 1, 0);
426 TREE_TYPE (value)
427 = build_array_type (elements,
428 build_index_type (build_int_2 (nchars - 1, 0)));
429 }
430 else
431 TREE_TYPE (value)
432 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
433 build_index_type (build_int_2 (nchars - 1, 0)));
d9cf7c82 434
ccd4c832
JM
435 TREE_CONSTANT (value) = 1;
436 TREE_READONLY (value) = ! flag_writable_strings;
b30f223b
RS
437 TREE_STATIC (value) = 1;
438 return value;
439}
440\f
53065596
RK
441/* To speed up processing of attributes, we maintain an array of
442 IDENTIFIER_NODES and the corresponding attribute types. */
443
444/* Array to hold attribute information. */
445
446static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
447
448static int attrtab_idx = 0;
449
450/* Add an entry to the attribute table above. */
451
452static void
453add_attribute (id, string, min_len, max_len, decl_req)
454 enum attrs id;
dff01034 455 const char *string;
53065596
RK
456 int min_len, max_len;
457 int decl_req;
458{
459 char buf[100];
460
461 attrtab[attrtab_idx].id = id;
462 attrtab[attrtab_idx].name = get_identifier (string);
463 attrtab[attrtab_idx].min = min_len;
464 attrtab[attrtab_idx].max = max_len;
465 attrtab[attrtab_idx++].decl_req = decl_req;
466
467 sprintf (buf, "__%s__", string);
468
469 attrtab[attrtab_idx].id = id;
470 attrtab[attrtab_idx].name = get_identifier (buf);
471 attrtab[attrtab_idx].min = min_len;
472 attrtab[attrtab_idx].max = max_len;
473 attrtab[attrtab_idx++].decl_req = decl_req;
474}
475
476/* Initialize attribute table. */
477
478static void
479init_attributes ()
480{
a89ca5c6 481 add_attribute (A_PACKED, "packed", 0, 0, 0);
bbb1ae01 482 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
2786cbad 483 add_attribute (A_COMMON, "common", 0, 0, 1);
53065596
RK
484 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
485 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
6d819282 486 add_attribute (A_UNUSED, "unused", 0, 0, 0);
53065596
RK
487 add_attribute (A_CONST, "const", 0, 0, 1);
488 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
489 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
490 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
491 add_attribute (A_MODE, "mode", 1, 1, 1);
492 add_attribute (A_SECTION, "section", 1, 1, 1);
493 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
d161fae4 494 add_attribute (A_FORMAT, "format", 3, 3, 1);
0161e8da 495 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
4b8af8d9
JM
496 add_attribute (A_WEAK, "weak", 0, 0, 1);
497 add_attribute (A_ALIAS, "alias", 1, 1, 1);
07417085 498 add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
7d384cc0 499 add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
140592a0 500 add_attribute (A_MALLOC, "malloc", 0, 0, 1);
a157febd 501 add_attribute (A_NO_LIMIT_STACK, "no_stack_limit", 0, 0, 1);
2a8f6b90 502 add_attribute (A_PURE, "pure", 0, 0, 1);
53065596
RK
503}
504\f
adfaf194
JM
505/* Default implementation of valid_lang_attribute, below. By default, there
506 are no language-specific attributes. */
507
508static int
509default_valid_lang_attribute (attr_name, attr_args, decl, type)
510 tree attr_name ATTRIBUTE_UNUSED;
511 tree attr_args ATTRIBUTE_UNUSED;
512 tree decl ATTRIBUTE_UNUSED;
513 tree type ATTRIBUTE_UNUSED;
514{
515 return 0;
516}
517
518/* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific
519 attribute for either declaration DECL or type TYPE and 0 otherwise. */
520
6e090c76 521int (*valid_lang_attribute) PARAMS ((tree, tree, tree, tree))
adfaf194
JM
522 = default_valid_lang_attribute;
523
1228e2a6 524/* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
53065596
RK
525 and install them in NODE, which is either a DECL (including a TYPE_DECL)
526 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
0f41302f 527 and declaration modifiers but before the declaration proper. */
b30f223b
RS
528
529void
53065596
RK
530decl_attributes (node, attributes, prefix_attributes)
531 tree node, attributes, prefix_attributes;
b30f223b 532{
a16b4c9c
JW
533 tree decl = 0, type = 0;
534 int is_type = 0;
53065596
RK
535 tree a;
536
537 if (attrtab_idx == 0)
538 init_attributes ();
539
2f939d94 540 if (DECL_P (node))
53065596
RK
541 {
542 decl = node;
543 type = TREE_TYPE (decl);
544 is_type = TREE_CODE (node) == TYPE_DECL;
545 }
2f939d94 546 else if (TYPE_P (node))
53065596 547 type = node, is_type = 1;
a2cd7b45 548
e2af664c
NC
549#ifdef PRAGMA_INSERT_ATTRIBUTES
550 /* If the code in c-pragma.c wants to insert some attributes then
551 allow it to do so. Do this before allowing machine back ends to
552 insert attributes, so that they have the opportunity to override
553 anything done here. */
554 PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
555#endif
d125d268 556
f09db6e0
NC
557#ifdef INSERT_ATTRIBUTES
558 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
559#endif
d125d268 560
eed32833 561 attributes = chainon (prefix_attributes, attributes);
5289b665 562
b30f223b 563 for (a = attributes; a; a = TREE_CHAIN (a))
53065596
RK
564 {
565 tree name = TREE_PURPOSE (a);
566 tree args = TREE_VALUE (a);
567 int i;
568 enum attrs id;
b57062ca 569
53065596
RK
570 for (i = 0; i < attrtab_idx; i++)
571 if (attrtab[i].name == name)
572 break;
573
6eaba4a7 574 if (i == attrtab_idx)
53065596 575 {
adfaf194
JM
576 if (! valid_machine_attribute (name, args, decl, type)
577 && ! (* valid_lang_attribute) (name, args, decl, type))
6eaba4a7
DE
578 warning ("`%s' attribute directive ignored",
579 IDENTIFIER_POINTER (name));
17c1a44f
SC
580 else if (decl != 0)
581 type = TREE_TYPE (decl);
53065596
RK
582 continue;
583 }
584 else if (attrtab[i].decl_req && decl == 0)
585 {
586 warning ("`%s' attribute does not apply to types",
587 IDENTIFIER_POINTER (name));
588 continue;
589 }
590 else if (list_length (args) < attrtab[i].min
591 || list_length (args) > attrtab[i].max)
592 {
593 error ("wrong number of arguments specified for `%s' attribute",
594 IDENTIFIER_POINTER (name));
595 continue;
596 }
597
598 id = attrtab[i].id;
599 switch (id)
600 {
601 case A_PACKED:
1bcf5b08 602 if (is_type)
a89ca5c6
RK
603 TYPE_PACKED (type) = 1;
604 else if (TREE_CODE (decl) == FIELD_DECL)
53065596
RK
605 DECL_PACKED (decl) = 1;
606 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
607 used for DECL_REGISTER. It wouldn't mean anything anyway. */
608 else
609 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
610 break;
611
bbb1ae01
RK
612 case A_NOCOMMON:
613 if (TREE_CODE (decl) == VAR_DECL)
614 DECL_COMMON (decl) = 0;
615 else
616 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
617 break;
618
2786cbad
JM
619 case A_COMMON:
620 if (TREE_CODE (decl) == VAR_DECL)
621 DECL_COMMON (decl) = 1;
622 else
623 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
624 break;
625
53065596
RK
626 case A_NORETURN:
627 if (TREE_CODE (decl) == FUNCTION_DECL)
628 TREE_THIS_VOLATILE (decl) = 1;
629 else if (TREE_CODE (type) == POINTER_TYPE
630 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
b57062ca 631 TREE_TYPE (decl) = type
53065596
RK
632 = build_pointer_type
633 (build_type_variant (TREE_TYPE (type),
634 TREE_READONLY (TREE_TYPE (type)), 1));
635 else
636 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
637 break;
638
140592a0
AG
639 case A_MALLOC:
640 if (TREE_CODE (decl) == FUNCTION_DECL)
641 DECL_IS_MALLOC (decl) = 1;
2a8f6b90 642 /* ??? TODO: Support types. */
140592a0
AG
643 else
644 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
645 break;
646
1c4fadec 647 case A_UNUSED:
6d819282 648 if (is_type)
d20a70b4
CM
649 if (decl)
650 TREE_USED (decl) = 1;
651 else
652 TREE_USED (type) = 1;
b57062ca 653 else if (TREE_CODE (decl) == PARM_DECL
6d819282 654 || TREE_CODE (decl) == VAR_DECL
736b02fd
KG
655 || TREE_CODE (decl) == FUNCTION_DECL
656 || TREE_CODE (decl) == LABEL_DECL)
1c4fadec
RK
657 TREE_USED (decl) = 1;
658 else
659 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
660 break;
661
53065596
RK
662 case A_CONST:
663 if (TREE_CODE (decl) == FUNCTION_DECL)
664 TREE_READONLY (decl) = 1;
665 else if (TREE_CODE (type) == POINTER_TYPE
666 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
667 TREE_TYPE (decl) = type
668 = build_pointer_type
669 (build_type_variant (TREE_TYPE (type), 1,
670 TREE_THIS_VOLATILE (TREE_TYPE (type))));
671 else
672 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
673 break;
674
2a8f6b90
JH
675 case A_PURE:
676 if (TREE_CODE (decl) == FUNCTION_DECL)
677 DECL_IS_PURE (decl) = 1;
678 /* ??? TODO: Support types. */
679 else
680 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
681 break;
682
683
53065596 684 case A_T_UNION:
1bcf5b08 685 if (is_type
53065596 686 && TREE_CODE (type) == UNION_TYPE
1bcf5b08 687 && (decl == 0
62b1077c
RK
688 || (TYPE_FIELDS (type) != 0
689 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
1bcf5b08
RK
690 TYPE_TRANSPARENT_UNION (type) = 1;
691 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
53065596
RK
692 && TREE_CODE (type) == UNION_TYPE
693 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
1bcf5b08 694 DECL_TRANSPARENT_UNION (decl) = 1;
53065596
RK
695 else
696 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
697 break;
698
699 case A_CONSTRUCTOR:
700 if (TREE_CODE (decl) == FUNCTION_DECL
701 && TREE_CODE (type) == FUNCTION_TYPE
702 && decl_function_context (decl) == 0)
1a16a053
RK
703 {
704 DECL_STATIC_CONSTRUCTOR (decl) = 1;
705 TREE_USED (decl) = 1;
706 }
53065596
RK
707 else
708 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
709 break;
710
711 case A_DESTRUCTOR:
712 if (TREE_CODE (decl) == FUNCTION_DECL
713 && TREE_CODE (type) == FUNCTION_TYPE
714 && decl_function_context (decl) == 0)
1a16a053
RK
715 {
716 DECL_STATIC_DESTRUCTOR (decl) = 1;
717 TREE_USED (decl) = 1;
718 }
53065596
RK
719 else
720 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
721 break;
722
723 case A_MODE:
724 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
725 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
726 else
727 {
728 int j;
dff01034 729 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
53065596
RK
730 int len = strlen (p);
731 enum machine_mode mode = VOIDmode;
732 tree typefm;
733
734 if (len > 4 && p[0] == '_' && p[1] == '_'
735 && p[len - 1] == '_' && p[len - 2] == '_')
736 {
3438dff9 737 char *newp = (char *) alloca (len - 1);
53065596
RK
738
739 strcpy (newp, &p[2]);
740 newp[len - 4] = '\0';
741 p = newp;
742 }
743
744 /* Give this decl a type with the specified mode.
745 First check for the special modes. */
746 if (! strcmp (p, "byte"))
747 mode = byte_mode;
748 else if (!strcmp (p, "word"))
749 mode = word_mode;
750 else if (! strcmp (p, "pointer"))
751 mode = ptr_mode;
752 else
753 for (j = 0; j < NUM_MACHINE_MODES; j++)
754 if (!strcmp (p, GET_MODE_NAME (j)))
755 mode = (enum machine_mode) j;
756
757 if (mode == VOIDmode)
758 error ("unknown machine mode `%s'", p);
759 else if (0 == (typefm = type_for_mode (mode,
760 TREE_UNSIGNED (type))))
761 error ("no data type for mode `%s'", p);
762 else
763 {
b15ad712
JM
764 if (TYPE_PRECISION (typefm) > (TREE_UNSIGNED (type)
765 ? TYPE_PRECISION(uintmax_type_node)
766 : TYPE_PRECISION(intmax_type_node))
767 && pedantic)
768 pedwarn ("type with more precision than %s",
769 TREE_UNSIGNED (type) ? "uintmax_t" : "intmax_t");
53065596 770 TREE_TYPE (decl) = type = typefm;
06ceef4e 771 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
53065596
RK
772 layout_decl (decl, 0);
773 }
774 }
775 break;
776
777 case A_SECTION:
5289b665 778#ifdef ASM_OUTPUT_SECTION_NAME
53065596
RK
779 if ((TREE_CODE (decl) == FUNCTION_DECL
780 || TREE_CODE (decl) == VAR_DECL)
781 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
782 {
b57062ca 783 if (TREE_CODE (decl) == VAR_DECL
f4ca236c
RK
784 && current_function_decl != NULL_TREE
785 && ! TREE_STATIC (decl))
53065596
RK
786 error_with_decl (decl,
787 "section attribute cannot be specified for local variables");
788 /* The decl may have already been given a section attribute from
789 a previous declaration. Ensure they match. */
790 else if (DECL_SECTION_NAME (decl) != NULL_TREE
791 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
792 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
793 error_with_decl (node,
794 "section of `%s' conflicts with previous declaration");
795 else
796 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
797 }
798 else
799 error_with_decl (node,
5289b665
DE
800 "section attribute not allowed for `%s'");
801#else
53065596
RK
802 error_with_decl (node,
803 "section attributes are not supported for this target");
5289b665 804#endif
53065596
RK
805 break;
806
807 case A_ALIGNED:
6f38f669 808 {
53065596 809 tree align_expr
54630035
RK
810 = (args ? TREE_VALUE (args)
811 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
05bccae2 812 int i;
d125d268 813
53065596
RK
814 /* Strip any NOPs of any kind. */
815 while (TREE_CODE (align_expr) == NOP_EXPR
816 || TREE_CODE (align_expr) == CONVERT_EXPR
817 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
818 align_expr = TREE_OPERAND (align_expr, 0);
b57062ca 819
53065596
RK
820 if (TREE_CODE (align_expr) != INTEGER_CST)
821 {
822 error ("requested alignment is not a constant");
823 continue;
824 }
825
05bccae2 826 if ((i = tree_log2 (align_expr)) == -1)
53065596 827 error ("requested alignment is not a power of 2");
05bccae2
RK
828 else if (i > HOST_BITS_PER_INT - 2)
829 error ("requested alignment is too large");
53065596 830 else if (is_type)
11cf4d18 831 {
aee3c6b0
JW
832 /* If we have a TYPE_DECL, then copy the type, so that we
833 don't accidentally modify a builtin type. See pushdecl. */
834 if (decl && TREE_TYPE (decl) != error_mark_node
835 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
af80d489 836 {
aee3c6b0
JW
837 tree tt = TREE_TYPE (decl);
838 DECL_ORIGINAL_TYPE (decl) = tt;
839 tt = build_type_copy (tt);
840 TYPE_NAME (tt) = decl;
841 TREE_USED (tt) = TREE_USED (decl);
842 TREE_TYPE (decl) = tt;
843 type = tt;
af80d489 844 }
aee3c6b0
JW
845
846 TYPE_ALIGN (type) = (1 << i) * BITS_PER_UNIT;
847 TYPE_USER_ALIGN (type) = 1;
11cf4d18 848 }
53065596
RK
849 else if (TREE_CODE (decl) != VAR_DECL
850 && TREE_CODE (decl) != FIELD_DECL)
851 error_with_decl (decl,
852 "alignment may not be specified for `%s'");
853 else
11cf4d18
JJ
854 {
855 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
856 DECL_USER_ALIGN (decl) = 1;
857 }
6f38f669 858 }
53065596 859 break;
6f38f669 860
53065596 861 case A_FORMAT:
b30f223b 862 {
bb72a084 863 tree format_type_id = TREE_VALUE (args);
53065596
RK
864 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
865 tree first_arg_num_expr
866 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
05bccae2 867 unsigned HOST_WIDE_INT format_num, first_arg_num;
bb72a084 868 enum format_type format_type;
53065596 869 tree argument;
05bccae2 870 unsigned int arg_num;
b57062ca 871
53065596
RK
872 if (TREE_CODE (decl) != FUNCTION_DECL)
873 {
874 error_with_decl (decl,
875 "argument format specified for non-function `%s'");
876 continue;
877 }
d125d268 878
bb72a084 879 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
0161e8da 880 {
bb72a084 881 error ("unrecognized format specifier");
0161e8da
RK
882 continue;
883 }
53065596
RK
884 else
885 {
dff01034 886 const char *p = IDENTIFIER_POINTER (format_type_id);
d125d268 887
bb72a084
PE
888 if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
889 format_type = printf_format_type;
890 else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
891 format_type = scanf_format_type;
892 else if (!strcmp (p, "strftime")
893 || !strcmp (p, "__strftime__"))
894 format_type = strftime_format_type;
895 else
896 {
b27d2bd5 897 warning ("`%s' is an unrecognized format function type", p);
bb72a084
PE
898 continue;
899 }
53065596 900 }
6f38f669 901
53065596
RK
902 /* Strip any conversions from the string index and first arg number
903 and verify they are constants. */
904 while (TREE_CODE (format_num_expr) == NOP_EXPR
905 || TREE_CODE (format_num_expr) == CONVERT_EXPR
906 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
907 format_num_expr = TREE_OPERAND (format_num_expr, 0);
677ff441 908
53065596
RK
909 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
910 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
911 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
912 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
913
914 if (TREE_CODE (format_num_expr) != INTEGER_CST
05bccae2
RK
915 || TREE_INT_CST_HIGH (format_num_expr) != 0
916 || TREE_CODE (first_arg_num_expr) != INTEGER_CST
917 || TREE_INT_CST_HIGH (first_arg_num_expr) != 0)
09e3dd72 918 {
05bccae2 919 error ("format string has invalid operand number");
53065596 920 continue;
09e3dd72 921 }
53065596
RK
922
923 format_num = TREE_INT_CST_LOW (format_num_expr);
924 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
925 if (first_arg_num != 0 && first_arg_num <= format_num)
68a91d8d 926 {
53065596 927 error ("format string arg follows the args to be formatted");
6f38f669 928 continue;
68a91d8d 929 }
53065596
RK
930
931 /* If a parameter list is specified, verify that the format_num
932 argument is actually a string, in case the format attribute
933 is in error. */
934 argument = TYPE_ARG_TYPES (type);
935 if (argument)
09e3dd72 936 {
05bccae2
RK
937 for (arg_num = 1; argument != 0 && arg_num != format_num;
938 ++arg_num, argument = TREE_CHAIN (argument))
939 ;
940
53065596
RK
941 if (! argument
942 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
943 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
944 != char_type_node))
945 {
946 error ("format string arg not a string type");
09e3dd72
RK
947 continue;
948 }
05bccae2
RK
949
950 else if (first_arg_num != 0)
53065596
RK
951 {
952 /* Verify that first_arg_num points to the last arg,
0f41302f 953 the ... */
53065596
RK
954 while (argument)
955 arg_num++, argument = TREE_CHAIN (argument);
05bccae2
RK
956
957 if (arg_num != first_arg_num)
958 {
959 error ("args to be formatted is not '...'");
960 continue;
961 }
53065596 962 }
09e3dd72 963 }
a2cd7b45 964
53065596
RK
965 record_function_format (DECL_NAME (decl),
966 DECL_ASSEMBLER_NAME (decl),
bb72a084 967 format_type, format_num, first_arg_num);
53065596
RK
968 break;
969 }
4b8af8d9 970
0161e8da
RK
971 case A_FORMAT_ARG:
972 {
973 tree format_num_expr = TREE_VALUE (args);
05bccae2
RK
974 unsigned HOST_WIDE_INT format_num;
975 unsigned int arg_num;
0161e8da 976 tree argument;
b57062ca 977
0161e8da
RK
978 if (TREE_CODE (decl) != FUNCTION_DECL)
979 {
980 error_with_decl (decl,
981 "argument format specified for non-function `%s'");
982 continue;
983 }
b57062ca 984
0161e8da
RK
985 /* Strip any conversions from the first arg number and verify it
986 is a constant. */
987 while (TREE_CODE (format_num_expr) == NOP_EXPR
988 || TREE_CODE (format_num_expr) == CONVERT_EXPR
989 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
990 format_num_expr = TREE_OPERAND (format_num_expr, 0);
991
05bccae2
RK
992 if (TREE_CODE (format_num_expr) != INTEGER_CST
993 || TREE_INT_CST_HIGH (format_num_expr) != 0)
0161e8da 994 {
05bccae2 995 error ("format string has invalid operand number");
0161e8da
RK
996 continue;
997 }
998
999 format_num = TREE_INT_CST_LOW (format_num_expr);
1000
1001 /* If a parameter list is specified, verify that the format_num
1002 argument is actually a string, in case the format attribute
1003 is in error. */
1004 argument = TYPE_ARG_TYPES (type);
1005 if (argument)
1006 {
05bccae2
RK
1007 for (arg_num = 1; argument != 0 && arg_num != format_num;
1008 ++arg_num, argument = TREE_CHAIN (argument))
1009 ;
1010
0161e8da
RK
1011 if (! argument
1012 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
1013 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
1014 != char_type_node))
1015 {
1016 error ("format string arg not a string type");
1017 continue;
1018 }
1019 }
1020
1021 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
1022 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
1023 != char_type_node))
1024 {
1025 error ("function does not return string type");
1026 continue;
1027 }
1028
1029 record_international_format (DECL_NAME (decl),
1030 DECL_ASSEMBLER_NAME (decl),
1031 format_num);
1032 break;
1033 }
1034
4b8af8d9
JM
1035 case A_WEAK:
1036 declare_weak (decl);
1037 break;
1038
1039 case A_ALIAS:
1040 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
5d9dd5a5 1041 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
4b8af8d9
JM
1042 error_with_decl (decl,
1043 "`%s' defined both normally and as an alias");
1044 else if (decl_function_context (decl) == 0)
1045 {
20045452
RH
1046 tree id;
1047
1048 id = TREE_VALUE (args);
1049 if (TREE_CODE (id) != STRING_CST)
1050 {
1051 error ("alias arg not a string");
1052 break;
1053 }
1054 id = get_identifier (TREE_STRING_POINTER (id));
06a50fff
ZW
1055 /* This counts as a use of the object pointed to. */
1056 TREE_USED (id) = 1;
20045452 1057
4b8af8d9
JM
1058 if (TREE_CODE (decl) == FUNCTION_DECL)
1059 DECL_INITIAL (decl) = error_mark_node;
1060 else
1061 DECL_EXTERNAL (decl) = 0;
1062 assemble_alias (decl, id);
1063 }
1064 else
1065 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1066 break;
07417085 1067
7d384cc0
KR
1068 case A_NO_CHECK_MEMORY_USAGE:
1069 if (TREE_CODE (decl) != FUNCTION_DECL)
1070 {
1071 error_with_decl (decl,
1072 "`%s' attribute applies only to functions",
1073 IDENTIFIER_POINTER (name));
1074 }
1075 else if (DECL_INITIAL (decl))
1076 {
1077 error_with_decl (decl,
1078 "can't set `%s' attribute after definition",
1079 IDENTIFIER_POINTER (name));
1080 }
1081 else
1082 DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
1083 break;
1084
07417085
KR
1085 case A_NO_INSTRUMENT_FUNCTION:
1086 if (TREE_CODE (decl) != FUNCTION_DECL)
1087 {
1088 error_with_decl (decl,
1089 "`%s' attribute applies only to functions",
1090 IDENTIFIER_POINTER (name));
1091 }
1092 else if (DECL_INITIAL (decl))
1093 {
1094 error_with_decl (decl,
1095 "can't set `%s' attribute after definition",
1096 IDENTIFIER_POINTER (name));
1097 }
1098 else
1099 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1100 break;
a157febd
GK
1101
1102 case A_NO_LIMIT_STACK:
1103 if (TREE_CODE (decl) != FUNCTION_DECL)
1104 {
1105 error_with_decl (decl,
1106 "`%s' attribute applies only to functions",
1107 IDENTIFIER_POINTER (name));
1108 }
1109 else if (DECL_INITIAL (decl))
1110 {
1111 error_with_decl (decl,
1112 "can't set `%s' attribute after definition",
1113 IDENTIFIER_POINTER (name));
1114 }
1115 else
1116 DECL_NO_LIMIT_STACK (decl) = 1;
1117 break;
53065596
RK
1118 }
1119 }
b30f223b 1120}
800f4153
RK
1121
1122/* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1123 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1124
1125 The head of the declspec list is stored in DECLSPECS.
1126 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1127
1128 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1129 the list elements. We drop the containing TREE_LIST nodes and link the
1130 resulting attributes together the way decl_attributes expects them. */
1131
1132void
1133split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1134 tree specs_attrs;
1135 tree *declspecs, *prefix_attributes;
1136{
1137 tree t, s, a, next, specs, attrs;
1138
101e59f4
ML
1139 /* This can happen after an __extension__ in pedantic mode. */
1140 if (specs_attrs != NULL_TREE
1141 && TREE_CODE (specs_attrs) == INTEGER_CST)
1142 {
1143 *declspecs = NULL_TREE;
1144 *prefix_attributes = NULL_TREE;
1145 return;
1146 }
1147
800f4153
RK
1148 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1149 if (specs_attrs != NULL_TREE
1150 && TREE_CODE (specs_attrs) != TREE_LIST)
1151 {
1152 *declspecs = specs_attrs;
1153 *prefix_attributes = NULL_TREE;
1154 return;
1155 }
1156
1157 /* Remember to keep the lists in the same order, element-wise. */
1158
1159 specs = s = NULL_TREE;
1160 attrs = a = NULL_TREE;
1161 for (t = specs_attrs; t; t = next)
1162 {
1163 next = TREE_CHAIN (t);
1164 /* Declspecs have a non-NULL TREE_VALUE. */
1165 if (TREE_VALUE (t) != NULL_TREE)
1166 {
1167 if (specs == NULL_TREE)
1168 specs = s = t;
1169 else
1170 {
1171 TREE_CHAIN (s) = t;
1172 s = t;
1173 }
1174 }
1175 else
1176 {
1177 if (attrs == NULL_TREE)
1178 attrs = a = TREE_PURPOSE (t);
1179 else
1180 {
1181 TREE_CHAIN (a) = TREE_PURPOSE (t);
1182 a = TREE_PURPOSE (t);
1183 }
1184 /* More attrs can be linked here, move A to the end. */
1185 while (TREE_CHAIN (a) != NULL_TREE)
1186 a = TREE_CHAIN (a);
1187 }
1188 }
1189
1190 /* Terminate the lists. */
1191 if (s != NULL_TREE)
1192 TREE_CHAIN (s) = NULL_TREE;
1193 if (a != NULL_TREE)
1194 TREE_CHAIN (a) = NULL_TREE;
1195
1196 /* All done. */
1197 *declspecs = specs;
1198 *prefix_attributes = attrs;
1199}
d9525bec
BK
1200
1201/* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1202 This function is used by the parser when a rule will accept attributes
1203 in a particular position, but we don't want to support that just yet.
1204
1205 A warning is issued for every ignored attribute. */
1206
1207tree
1208strip_attrs (specs_attrs)
1209 tree specs_attrs;
1210{
1211 tree specs, attrs;
1212
1213 split_specs_attrs (specs_attrs, &specs, &attrs);
1214
1215 while (attrs)
1216 {
1217 warning ("`%s' attribute ignored",
1218 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1219 attrs = TREE_CHAIN (attrs);
1220 }
1221
1222 return specs;
1223}
b30f223b 1224\f
1ccf251f
RK
1225/* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1226 a parameter list. */
1227
3c786c69
JM
1228/* The meaningfully distinct length modifiers for format checking recognised
1229 by GCC. */
1230enum format_lengths
1231{
1232 FMT_LEN_none,
1233 FMT_LEN_hh,
1234 FMT_LEN_h,
1235 FMT_LEN_l,
1236 FMT_LEN_ll,
1237 FMT_LEN_L,
1238 FMT_LEN_z,
1239 FMT_LEN_t,
1240 FMT_LEN_j,
1241 FMT_LEN_MAX
1242};
1243
1244
1245/* The standard versions in which various format features appeared. */
1246enum format_std_version
1247{
1248 STD_C89,
1249 STD_C94,
1250 STD_C99,
1251 STD_EXT
1252};
1253
1254
2588e44e
JM
1255/* Flags that may apply to a particular kind of format checked by GCC. */
1256enum
1257{
1258 /* This format converts arguments of types determined by the
1259 format string. */
1260 FMT_FLAG_ARG_CONVERT = 1,
1261 /* The scanf allocation 'a' kludge applies to this format kind. */
1262 FMT_FLAG_SCANF_A_KLUDGE = 2,
1263 /* A % during parsing a specifier is allowed to be a modified % rather
1264 that indicating the format is broken and we are out-of-sync. */
1265 FMT_FLAG_FANCY_PERCENT_OK = 4
1266 /* Not included here: details of whether width or precision may occur
1267 (controlled by width_char and precision_char); details of whether
1268 '*' can be used for these (width_type and precision_type); details
1269 of whether length modifiers can occur (length_char_specs); details
1270 of when $ operand numbers are allowed (always, for the formats
1271 supported, if arguments are converted). */
1272};
1273
1274
3c786c69
JM
1275/* Structure describing a length modifier supported in format checking, and
1276 possibly a doubled version such as "hh". */
1277typedef struct
1278{
1279 /* Name of the single-character length modifier. */
1280 const char *name;
1281 /* Index into a format_char_info.types array. */
1282 enum format_lengths index;
1283 /* Standard version this length appears in. */
1284 enum format_std_version std;
1285 /* Same, if the modifier can be repeated, or NULL if it can't. */
1286 const char *double_name;
1287 enum format_lengths double_index;
1288 enum format_std_version double_std;
1289} format_length_info;
1290
1291
1292/* Structure desribing the combination of a conversion specifier
1293 (or a set of specifiers which act identically) and a length modifier. */
1294typedef struct
1295{
1296 /* The standard version this combination of length and type appeared in.
1297 This is only relevant if greater than those for length and type
1298 individually; otherwise it is ignored. */
1299 enum format_std_version std;
1300 /* The name to use for the type, if different from that generated internally
1301 (e.g., "signed size_t"). */
1302 const char *name;
1303 /* The type itself. */
1304 tree *type;
1305} format_type_detail;
1306
1307
1308/* Macros to fill out tables of these. */
1309#define BADLEN { 0, NULL, NULL }
1310#define NOLENGTHS { BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }
1311
1312
1313/* Structure desribing a format conversion specifier (or a set of specifiers
1314 which act identically), and the length modifiers used with it. */
1315typedef struct
1316{
1317 const char *format_chars;
1318 int pointer_count;
1319 enum format_std_version std;
1320 /* Types accepted for each length modifier. */
1321 format_type_detail types[FMT_LEN_MAX];
0e00b68b 1322 /* List of other modifier characters allowed with these specifiers.
3c786c69 1323 This lists flags, and additionally "w" for width, "p" for precision,
0e00b68b
JM
1324 "a" for scanf "a" allocation extension (not applicable in C99 mode),
1325 "*" for scanf suppression, and "E" and "O" for those strftime
1326 modifiers. */
3c786c69 1327 const char *flag_chars;
0e00b68b
JM
1328 /* List of additional flags describing these conversion specifiers.
1329 "c" for generic character pointers being allowed, "2" for strftime
1330 two digit year formats, "3" for strftime formats giving two digit
2588e44e
JM
1331 years in some locales, "4" for "2" which becomes "3" with an "E" modifier,
1332 "o" if use of strftime "O" is a GNU extension beyond C99,
1333 "W" if the argument is a pointer which is dereferenced and written into,
1334 "i" for printf integer formats where the '0' flag is ignored with
1335 precision, and "[" for the starting character of a scanf scanset. */
0e00b68b 1336 const char *flags2;
3c786c69
JM
1337} format_char_info;
1338
1339
2588e44e
JM
1340/* Structure describing a flag accepted by some kind of format. */
1341typedef struct
1342{
1343 /* The flag character in question (0 for end of array). */
1344 int flag_char;
1345 /* Zero if this entry describes the flag character in general, or a
1346 non-zero character that may be found in flags2 if it describes the
1347 flag when used with certain formats only. If the latter, only
1348 the first such entry found that applies to the current conversion
1349 specifier is used; the values of `name' and `long_name' it supplies
1350 will be used, if non-NULL and the standard version is higher than
1351 the unpredicated one, for any pedantic warning. For example, 'o'
1352 for strftime formats (meaning 'O' is an extension over C99). */
1353 int predicate;
1354 /* The name to use for this flag in diagnostic messages. For example,
1355 N_("`0' flag"), N_("field width"). */
1356 const char *name;
1357 /* Long name for this flag in diagnostic messages; currently only used for
1358 "ISO C does not support ...". For example, N_("the `I' printf flag"). */
1359 const char *long_name;
1360 /* The standard version in which it appeared. */
1361 enum format_std_version std;
1362} format_flag_spec;
1363
1364
1365/* Structure describing a combination of flags that is bad for some kind
1366 of format. */
1367typedef struct
1368{
1369 /* The first flag character in question (0 for end of array). */
1370 int flag_char1;
1371 /* The second flag character. */
1372 int flag_char2;
1373 /* Non-zero if the message should say that the first flag is ignored with
1374 the second, zero if the combination should simply be objected to. */
1375 int ignored;
1376 /* Zero if this entry applies whenever this flag combination occurs,
1377 a non-zero character from flags2 if it only applies in some
1378 circumstances (e.g. 'i' for printf formats ignoring 0 with precision). */
1379 int predicate;
1380} format_flag_pair;
1381
1382
3c786c69
JM
1383/* Structure describing a particular kind of format processed by GCC. */
1384typedef struct
1385{
1386 /* The name of this kind of format, for use in diagnostics. */
1387 const char *name;
1388 /* Specifications of the length modifiers accepted; possibly NULL. */
1389 const format_length_info *length_char_specs;
1390 /* Details of the conversion specification characters accepted. */
1391 const format_char_info *conversion_specs;
2588e44e
JM
1392 /* String listing the flag characters that are accepted. */
1393 const char *flag_chars;
1394 /* String listing modifier characters (strftime) accepted. May be NULL. */
1395 const char *modifier_chars;
1396 /* Details of the flag characters, including pseudo-flags. */
1397 const format_flag_spec *flag_specs;
1398 /* Details of bad combinations of flags. */
1399 const format_flag_pair *bad_flag_pairs;
1400 /* Flags applicable to this kind of format. */
1401 int flags;
1402 /* Flag character to treat a width as, or 0 if width not used. */
1403 int width_char;
1404 /* Flag character to treat a precision as, or 0 if precision not used. */
1405 int precision_char;
1406 /* If a flag character has the effect of suppressing the conversion of
1407 an argument ('*' in scanf), that flag character, otherwise 0. */
1408 int suppression_char;
1409 /* Flag character to treat a length modifier as (ignored if length
1410 modifiers not used). Need not be placed in flag_chars for conversion
1411 specifiers, but is used to check for bad combinations such as length
1412 modifier with assignment suppression in scanf. */
1413 int length_code_char;
1414 /* Pointer to type of argument expected if '*' is used for a width,
1415 or NULL if '*' not used for widths. */
1416 tree *width_type;
1417 /* Pointer to type of argument expected if '*' is used for a precision,
1418 or NULL if '*' not used for precisions. */
1419 tree *precision_type;
3c786c69
JM
1420} format_kind_info;
1421
1422
6a357625
JM
1423/* Structure describing details of a type expected in format checking,
1424 and the type to check against it. */
1425typedef struct format_wanted_type
1426{
1427 /* The type wanted. */
1428 tree wanted_type;
1429 /* The name of this type to use in diagnostics. */
1430 const char *wanted_type_name;
1431 /* The level of indirection through pointers at which this type occurs. */
1432 int pointer_count;
1433 /* Whether, when pointer_count is 1, to allow any character type when
1434 pedantic, rather than just the character or void type specified. */
1435 int char_lenient_flag;
1436 /* Whether the argument, dereferenced once, is written into and so the
1437 argument must not be a pointer to a const-qualified type. */
1438 int writing_in_flag;
1439 /* If warnings should be of the form "field precision is not type int",
1440 the name to use (in this case "field precision"), otherwise NULL,
1441 for "%s format, %s arg" type messages. If (in an extension), this
1442 is a pointer type, wanted_type_name should be set to include the
1443 terminating '*' characters of the type name to give a correct
1444 message. */
1445 const char *name;
1446 /* The actual parameter to check against the wanted type. */
1447 tree param;
1448 /* The argument number of that parameter. */
1449 int arg_num;
1450 /* The next type to check for this format conversion, or NULL if none. */
1451 struct format_wanted_type *next;
1452} format_wanted_type;
1453
1454
3c786c69
JM
1455static const format_length_info printf_length_specs[] =
1456{
1457 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
1458 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C99 },
1459 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
1460 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
1461 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
1462 { "Z", FMT_LEN_z, STD_EXT, NULL, 0, 0 },
1463 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
1464 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
1465 { NULL, 0, 0, NULL, 0, 0 }
1466};
1467
1468
1469/* This differs from printf_length_specs only in that "Z" is not accepted. */
1470static const format_length_info scanf_length_specs[] =
1471{
1472 { "h", FMT_LEN_h, STD_C89, "hh", FMT_LEN_hh, STD_C99 },
1473 { "l", FMT_LEN_l, STD_C89, "ll", FMT_LEN_ll, STD_C99 },
1474 { "q", FMT_LEN_ll, STD_EXT, NULL, 0, 0 },
1475 { "L", FMT_LEN_L, STD_C89, NULL, 0, 0 },
1476 { "z", FMT_LEN_z, STD_C99, NULL, 0, 0 },
1477 { "t", FMT_LEN_t, STD_C99, NULL, 0, 0 },
1478 { "j", FMT_LEN_j, STD_C99, NULL, 0, 0 },
1479 { NULL, 0, 0, NULL, 0, 0 }
1480};
1481
1482
2588e44e
JM
1483static const format_flag_spec printf_flag_specs[] =
1484{
1485 { ' ', 0, N_("` ' flag"), N_("the ` ' printf flag"), STD_C89 },
1486 { '+', 0, N_("`+' flag"), N_("the `+' printf flag"), STD_C89 },
1487 { '#', 0, N_("`#' flag"), N_("the `#' printf flag"), STD_C89 },
1488 { '0', 0, N_("`0' flag"), N_("the `0' printf flag"), STD_C89 },
1489 { '-', 0, N_("`-' flag"), N_("the `-' printf flag"), STD_C89 },
1490 { '\'', 0, N_("`'' flag"), N_("the `'' printf flag"), STD_EXT },
1491 { 'I', 0, N_("`I' flag"), N_("the `I' printf flag"), STD_EXT },
1492 { 'w', 0, N_("field width"), N_("field width in printf format"), STD_C89 },
1493 { 'p', 0, N_("precision"), N_("precision in printf format"), STD_C89 },
1494 { 'L', 0, N_("length modifier"), N_("length modifier in printf format"), STD_C89 },
1495 { 0, 0, NULL, NULL, 0 }
1496};
1497
1498
1499static const format_flag_pair printf_flag_pairs[] =
1500{
1501 { ' ', '+', 1, 0 },
1502 { '0', '-', 1, 0 },
1503 { '0', 'p', 1, 'i' },
1504 { 0, 0, 0, 0 }
1505};
1506
1507
1508static const format_flag_spec scanf_flag_specs[] =
1509{
1510 { '*', 0, N_("assignment suppression"), N_("assignment suppression"), STD_C89 },
1511 { 'a', 0, N_("`a' flag"), N_("the `a' scanf flag"), STD_EXT },
1512 { 'w', 0, N_("field width"), N_("field width in scanf format"), STD_C89 },
1513 { 'L', 0, N_("length modifier"), N_("length modifier in scanf format"), STD_C89 },
1514 { 0, 0, NULL, NULL, 0 }
1515};
1516
1517
1518static const format_flag_pair scanf_flag_pairs[] =
1519{
1520 { '*', 'L', 0, 0 },
1521 { 0, 0, 0, 0 }
1522};
1523
1524
1525static const format_flag_spec strftime_flag_specs[] =
1526{
1527 { '_', 0, N_("`_' flag"), N_("the `_' strftime flag"), STD_EXT },
1528 { '-', 0, N_("`-' flag"), N_("the `-' strftime flag"), STD_EXT },
1529 { '0', 0, N_("`0' flag"), N_("the `0' strftime flag"), STD_EXT },
1530 { '^', 0, N_("`^' flag"), N_("the `^' strftime flag"), STD_EXT },
1531 { '#', 0, N_("`#' flag"), N_("the `#' strftime flag"), STD_EXT },
1532 { 'w', 0, N_("field width"), N_("field width in strftime format"), STD_EXT },
1533 { 'E', 0, N_("`E' modifier"), N_("the `E' strftime modifier"), STD_C99 },
1534 { 'O', 0, N_("`O' modifier"), N_("the `O' strftime modifier"), STD_C99 },
1535 { 'O', 'o', NULL, N_("the `O' modifier"), STD_EXT },
1536 { 0, 0, NULL, NULL, 0 }
1537};
1538
1539
1540static const format_flag_pair strftime_flag_pairs[] =
1541{
1542 { 'E', 'O', 0, 0 },
1543 { 0, 0, 0, 0 }
1544};
1545
1546
1ccf251f 1547#define T_I &integer_type_node
3c786c69
JM
1548#define T89_I { STD_C89, NULL, T_I }
1549#define T99_I { STD_C99, NULL, T_I }
1ccf251f 1550#define T_L &long_integer_type_node
3c786c69 1551#define T89_L { STD_C89, NULL, T_L }
2314fb70 1552#define T_LL &long_long_integer_type_node
3c786c69
JM
1553#define T99_LL { STD_C99, NULL, T_LL }
1554#define TEX_LL { STD_EXT, NULL, T_LL }
1ccf251f 1555#define T_S &short_integer_type_node
3c786c69 1556#define T89_S { STD_C89, NULL, T_S }
1ccf251f 1557#define T_UI &unsigned_type_node
3c786c69
JM
1558#define T89_UI { STD_C89, NULL, T_UI }
1559#define T99_UI { STD_C99, NULL, T_UI }
1ccf251f 1560#define T_UL &long_unsigned_type_node
3c786c69 1561#define T89_UL { STD_C89, NULL, T_UL }
2314fb70 1562#define T_ULL &long_long_unsigned_type_node
3c786c69
JM
1563#define T99_ULL { STD_C99, NULL, T_ULL }
1564#define TEX_ULL { STD_EXT, NULL, T_ULL }
1ccf251f 1565#define T_US &short_unsigned_type_node
3c786c69 1566#define T89_US { STD_C89, NULL, T_US }
1ccf251f 1567#define T_F &float_type_node
3c786c69
JM
1568#define T89_F { STD_C89, NULL, T_F }
1569#define T99_F { STD_C99, NULL, T_F }
1ccf251f 1570#define T_D &double_type_node
3c786c69
JM
1571#define T89_D { STD_C89, NULL, T_D }
1572#define T99_D { STD_C99, NULL, T_D }
1ccf251f 1573#define T_LD &long_double_type_node
3c786c69
JM
1574#define T89_LD { STD_C89, NULL, T_LD }
1575#define T99_LD { STD_C99, NULL, T_LD }
1ccf251f 1576#define T_C &char_type_node
3c786c69 1577#define T89_C { STD_C89, NULL, T_C }
cd732418 1578#define T_SC &signed_char_type_node
3c786c69 1579#define T99_SC { STD_C99, NULL, T_SC }
b57062ca 1580#define T_UC &unsigned_char_type_node
3c786c69 1581#define T99_UC { STD_C99, NULL, T_UC }
1ccf251f 1582#define T_V &void_type_node
3c786c69 1583#define T89_V { STD_C89, NULL, T_V }
1ccf251f 1584#define T_W &wchar_type_node
3c786c69
JM
1585#define T94_W { STD_C94, "wchar_t", T_W }
1586#define TEX_W { STD_EXT, "wchar_t", T_W }
c5ab7f91 1587#define T_WI &wint_type_node
3c786c69
JM
1588#define T94_WI { STD_C94, "wint_t", T_WI }
1589#define TEX_WI { STD_EXT, "wint_t", T_WI }
1590#define T_ST &c_size_type_node
1591#define T99_ST { STD_C99, "size_t", T_ST }
cd732418 1592#define T_SST &signed_size_type_node
3c786c69 1593#define T99_SST { STD_C99, "signed size_t", T_SST }
b8458e3e 1594#define T_PD &ptrdiff_type_node
3c786c69 1595#define T99_PD { STD_C99, "ptrdiff_t", T_PD }
cd732418 1596#define T_UPD &unsigned_ptrdiff_type_node
3c786c69 1597#define T99_UPD { STD_C99, "unsigned ptrdiff_t", T_UPD }
b15ad712 1598#define T_IM &intmax_type_node
3c786c69 1599#define T99_IM { STD_C99, "intmax_t", T_IM }
b15ad712 1600#define T_UIM &uintmax_type_node
3c786c69 1601#define T99_UIM { STD_C99, "uintmax_t", T_UIM }
1ccf251f 1602
3c786c69
JM
1603static const format_char_info print_char_table[] =
1604{
1605 /* C89 conversion specifiers. */
0e00b68b
JM
1606 { "di", 0, STD_C89, { T89_I, T99_I, T89_I, T89_L, T99_LL, TEX_LL, T99_SST, T99_PD, T99_IM }, "-wp0 +'I", "i" },
1607 { "oxX", 0, STD_C89, { T89_UI, T99_UI, T89_UI, T89_UL, T99_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "-wp0#", "i" },
1608 { "u", 0, STD_C89, { T89_UI, T99_UI, T89_UI, T89_UL, T99_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "-wp0'I", "i" },
1609 { "fgG", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'", "" },
1610 { "eE", 0, STD_C89, { T89_D, BADLEN, BADLEN, T99_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "" },
1611 { "c", 0, STD_C89, { T89_I, BADLEN, BADLEN, T94_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "" },
1612 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "c" },
1613 { "p", 1, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "c" },
1614 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T99_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W" },
3c786c69 1615 /* C99 conversion specifiers. */
0e00b68b
JM
1616 { "F", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#'", "" },
1617 { "aA", 0, STD_C99, { T99_D, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "-wp0 +#", "" },
3c786c69 1618 /* X/Open conversion specifiers. */
0e00b68b
JM
1619 { "C", 0, STD_EXT, { TEX_WI, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-w", "" },
1620 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "" },
3c786c69 1621 /* GNU conversion specifiers. */
0e00b68b
JM
1622 { "m", 0, STD_EXT, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "-wp", "" },
1623 { NULL, 0, 0, NOLENGTHS, NULL, NULL }
3c786c69 1624};
1ccf251f 1625
3c786c69
JM
1626static const format_char_info scan_char_table[] =
1627{
1628 /* C89 conversion specifiers. */
0e00b68b
JM
1629 { "di", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T99_LL, TEX_LL, T99_SST, T99_PD, T99_IM }, "*w", "W" },
1630 { "ouxX", 1, STD_C89, { T89_UI, T99_UC, T89_US, T89_UL, T99_ULL, TEX_ULL, T99_ST, T99_UPD, T99_UIM }, "*w", "W" },
1631 { "efgEG", 1, STD_C89, { T89_F, BADLEN, BADLEN, T89_D, BADLEN, T89_LD, BADLEN, BADLEN, BADLEN }, "*w", "W" },
1632 { "c", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "cW" },
1633 { "s", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW" },
1634 { "[", 1, STD_C89, { T89_C, BADLEN, BADLEN, T94_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "cW[" },
1635 { "p", 2, STD_C89, { T89_V, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W" },
1636 { "n", 1, STD_C89, { T89_I, T99_SC, T89_S, T89_L, T99_LL, BADLEN, T99_SST, T99_PD, T99_IM }, "", "W" },
3c786c69 1637 /* C99 conversion specifiers. */
0e00b68b 1638 { "FaA", 1, STD_C99, { T99_F, BADLEN, BADLEN, T99_D, BADLEN, T99_LD, BADLEN, BADLEN, BADLEN }, "*w", "W" },
3c786c69 1639 /* X/Open conversion specifiers. */
0e00b68b
JM
1640 { "C", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*w", "W" },
1641 { "S", 1, STD_EXT, { TEX_W, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN, BADLEN }, "*aw", "W" },
1642 { NULL, 0, 0, NOLENGTHS, NULL, NULL }
1ccf251f
RK
1643};
1644
3c786c69
JM
1645static format_char_info time_char_table[] =
1646{
1647 /* C89 conversion specifiers. */
0e00b68b
JM
1648 { "ABZab", 0, STD_C89, NOLENGTHS, "^#", "" },
1649 { "cx", 0, STD_C89, NOLENGTHS, "E", "3" },
1650 { "HIMSUWdmw", 0, STD_C89, NOLENGTHS, "-_0Ow", "" },
1651 { "j", 0, STD_C89, NOLENGTHS, "-_0Ow", "o" },
1652 { "p", 0, STD_C89, NOLENGTHS, "#", "" },
1653 { "X", 0, STD_C89, NOLENGTHS, "E", "" },
2588e44e 1654 { "y", 0, STD_C89, NOLENGTHS, "EO-_0w", "4" },
0e00b68b
JM
1655 { "Y", 0, STD_C89, NOLENGTHS, "-_0EOw", "o" },
1656 { "%", 0, STD_C89, NOLENGTHS, "", "" },
3c786c69 1657 /* C99 conversion specifiers. */
0e00b68b
JM
1658 { "C", 0, STD_C99, NOLENGTHS, "-_0EOw", "o" },
1659 { "D", 0, STD_C99, NOLENGTHS, "", "2" },
1660 { "eVu", 0, STD_C99, NOLENGTHS, "-_0Ow", "" },
1661 { "FRTnrt", 0, STD_C99, NOLENGTHS, "", "" },
1662 { "g", 0, STD_C99, NOLENGTHS, "O-_0w", "2o" },
1663 { "G", 0, STD_C99, NOLENGTHS, "-_0Ow", "o" },
1664 { "h", 0, STD_C99, NOLENGTHS, "^#", "" },
1665 { "z", 0, STD_C99, NOLENGTHS, "O", "o" },
3c786c69 1666 /* GNU conversion specifiers. */
0e00b68b
JM
1667 { "kls", 0, STD_EXT, NOLENGTHS, "-_0Ow", "" },
1668 { "P", 0, STD_EXT, NOLENGTHS, "", "" },
1669 { NULL, 0, 0, NOLENGTHS, NULL, NULL }
1ccf251f
RK
1670};
1671
3c786c69
JM
1672
1673/* This must be in the same order as enum format_type. */
1674static const format_kind_info format_types[] =
1675{
2588e44e
JM
1676 { "printf", printf_length_specs, print_char_table, " +#0-'I", NULL,
1677 printf_flag_specs, printf_flag_pairs,
1678 FMT_FLAG_ARG_CONVERT, 'w', 'p', 0, 'L',
1679 &integer_type_node, &integer_type_node
1680 },
1681 { "scanf", scanf_length_specs, scan_char_table, "*", NULL,
1682 scanf_flag_specs, scanf_flag_pairs,
1683 FMT_FLAG_ARG_CONVERT|FMT_FLAG_SCANF_A_KLUDGE, 'w', 0, '*', 'L',
1684 NULL, NULL
1685 },
1686 { "strftime", NULL, time_char_table, "_-0^#", "EO",
1687 strftime_flag_specs, strftime_flag_pairs,
1688 FMT_FLAG_FANCY_PERCENT_OK, 'w', 0, 0, 0,
1689 NULL, NULL
1690 }
bb72a084
PE
1691};
1692
3c786c69 1693
0161e8da
RK
1694typedef struct function_format_info
1695{
1ccf251f
RK
1696 struct function_format_info *next; /* next structure on the list */
1697 tree name; /* identifier such as "printf" */
1698 tree assembler_name; /* optional mangled identifier (for C++) */
bb72a084 1699 enum format_type format_type; /* type of format (printf, scanf, etc.) */
1ccf251f
RK
1700 int format_num; /* number of format argument */
1701 int first_arg_num; /* number of first arg (zero for varargs) */
1702} function_format_info;
1703
1704static function_format_info *function_format_list = NULL;
1705
0161e8da
RK
1706typedef struct international_format_info
1707{
1708 struct international_format_info *next; /* next structure on the list */
1709 tree name; /* identifier such as "gettext" */
1710 tree assembler_name; /* optional mangled identifier (for C++) */
1711 int format_num; /* number of format argument */
1712} international_format_info;
1713
1714static international_format_info *international_format_list = NULL;
1715
3f80e6ac
KG
1716static void check_format_info PARAMS ((int *, function_format_info *, tree));
1717static void status_warning PARAMS ((int *, const char *, ...))
1718 ATTRIBUTE_PRINTF_2;
1ccf251f 1719
5804a754 1720static void init_dollar_format_checking PARAMS ((int, tree));
3f80e6ac 1721static int maybe_read_dollar_number PARAMS ((int *, const char **, int,
5804a754 1722 tree, tree *));
3f80e6ac 1723static void finish_dollar_format_checking PARAMS ((int *));
5804a754 1724
2588e44e
JM
1725static const format_flag_spec *get_flag_spec PARAMS ((const format_flag_spec *,
1726 int, const char *));
1727
3f80e6ac 1728static void check_format_types PARAMS ((int *, format_wanted_type *));
c70eaeaf
KG
1729static int is_valid_printf_arglist PARAMS ((tree));
1730static rtx c_expand_builtin (tree, rtx, enum machine_mode, enum expand_modifier);
1731static rtx c_expand_builtin_printf PARAMS ((tree, rtx, enum machine_mode,
1732 enum expand_modifier, int));
6a357625 1733
1ccf251f 1734/* Initialize the table of functions to perform format checking on.
93e2382f 1735 The ISO C functions are always checked (whether <stdio.h> is
1ccf251f
RK
1736 included or not), since it is common to call printf without
1737 including <stdio.h>. There shouldn't be a problem with this,
93e2382f
JM
1738 since ISO C reserves these function names whether you include the
1739 header file or not. In any case, the checking is harmless. With
1740 -ffreestanding, these default attributes are disabled, and must be
1741 specified manually if desired.
0161e8da
RK
1742
1743 Also initialize the name of function that modify the format string for
1744 internationalization purposes. */
1ccf251f
RK
1745
1746void
1747init_function_format_info ()
1748{
93e2382f
JM
1749 if (flag_hosted)
1750 {
1751 /* Functions from ISO/IEC 9899:1990. */
1752 record_function_format (get_identifier ("printf"), NULL_TREE,
1753 printf_format_type, 1, 2);
ad3fd36f
KG
1754 record_function_format (get_identifier ("__builtin_printf"), NULL_TREE,
1755 printf_format_type, 1, 2);
93e2382f
JM
1756 record_function_format (get_identifier ("fprintf"), NULL_TREE,
1757 printf_format_type, 2, 3);
1758 record_function_format (get_identifier ("sprintf"), NULL_TREE,
1759 printf_format_type, 2, 3);
1760 record_function_format (get_identifier ("scanf"), NULL_TREE,
1761 scanf_format_type, 1, 2);
1762 record_function_format (get_identifier ("fscanf"), NULL_TREE,
1763 scanf_format_type, 2, 3);
1764 record_function_format (get_identifier ("sscanf"), NULL_TREE,
1765 scanf_format_type, 2, 3);
1766 record_function_format (get_identifier ("vprintf"), NULL_TREE,
1767 printf_format_type, 1, 0);
1768 record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1769 printf_format_type, 2, 0);
1770 record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1771 printf_format_type, 2, 0);
1772 record_function_format (get_identifier ("strftime"), NULL_TREE,
1773 strftime_format_type, 3, 0);
1774 }
1775
1776 if (flag_hosted && flag_isoc99)
980d86e9
JM
1777 {
1778 /* ISO C99 adds the snprintf and vscanf family functions. */
1779 record_function_format (get_identifier ("snprintf"), NULL_TREE,
1780 printf_format_type, 3, 4);
1781 record_function_format (get_identifier ("vsnprintf"), NULL_TREE,
1782 printf_format_type, 3, 0);
1783 record_function_format (get_identifier ("vscanf"), NULL_TREE,
1784 scanf_format_type, 1, 0);
1785 record_function_format (get_identifier ("vfscanf"), NULL_TREE,
1786 scanf_format_type, 2, 0);
1787 record_function_format (get_identifier ("vsscanf"), NULL_TREE,
1788 scanf_format_type, 2, 0);
1789 }
1790
93e2382f
JM
1791 if (flag_hosted && flag_noniso_default_format_attributes)
1792 {
1793 /* Uniforum/GNU gettext functions, not in ISO C. */
1794 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1795 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1796 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1797 }
1ccf251f
RK
1798}
1799
1800/* Record information for argument format checking. FUNCTION_IDENT is
1801 the identifier node for the name of the function to check (its decl
bb72a084
PE
1802 need not exist yet).
1803 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
1ccf251f
RK
1804 of the argument which is the format control string (starting from 1).
1805 FIRST_ARG_NUM is the number of the first actual argument to check
0161e8da 1806 against the format string, or zero if no checking is not be done
1ccf251f
RK
1807 (e.g. for varargs such as vfprintf). */
1808
bb72a084
PE
1809static void
1810record_function_format (name, assembler_name, format_type,
1ccf251f
RK
1811 format_num, first_arg_num)
1812 tree name;
1813 tree assembler_name;
bb72a084 1814 enum format_type format_type;
1ccf251f
RK
1815 int format_num;
1816 int first_arg_num;
1817{
1818 function_format_info *info;
1819
1820 /* Re-use existing structure if it's there. */
1821
1822 for (info = function_format_list; info; info = info->next)
1823 {
1824 if (info->name == name && info->assembler_name == assembler_name)
1825 break;
1826 }
1827 if (! info)
1828 {
1829 info = (function_format_info *) xmalloc (sizeof (function_format_info));
1830 info->next = function_format_list;
1831 function_format_list = info;
1832
1833 info->name = name;
1834 info->assembler_name = assembler_name;
1835 }
1836
bb72a084 1837 info->format_type = format_type;
1ccf251f
RK
1838 info->format_num = format_num;
1839 info->first_arg_num = first_arg_num;
1840}
1841
0161e8da
RK
1842/* Record information for the names of function that modify the format
1843 argument to format functions. FUNCTION_IDENT is the identifier node for
1844 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1845 the number of the argument which is the format control string (starting
1846 from 1). */
1847
31837ce2 1848static void
0161e8da
RK
1849record_international_format (name, assembler_name, format_num)
1850 tree name;
1851 tree assembler_name;
1852 int format_num;
1853{
1854 international_format_info *info;
1855
1856 /* Re-use existing structure if it's there. */
1857
1858 for (info = international_format_list; info; info = info->next)
1859 {
1860 if (info->name == name && info->assembler_name == assembler_name)
1861 break;
1862 }
1863
1864 if (! info)
1865 {
1866 info
1867 = (international_format_info *)
1868 xmalloc (sizeof (international_format_info));
1869 info->next = international_format_list;
1870 international_format_list = info;
1871
1872 info->name = name;
1873 info->assembler_name = assembler_name;
1874 }
1875
1876 info->format_num = format_num;
1877}
1ccf251f
RK
1878\f
1879/* Check the argument list of a call to printf, scanf, etc.
1880 NAME is the function identifier.
1881 ASSEMBLER_NAME is the function's assembler identifier.
1882 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1883 PARAMS is the list of argument values. */
1884
1885void
3f80e6ac
KG
1886check_function_format (status, name, assembler_name, params)
1887 int *status;
1ccf251f
RK
1888 tree name;
1889 tree assembler_name;
1890 tree params;
1891{
1892 function_format_info *info;
1893
1894 /* See if this function is a format function. */
1895 for (info = function_format_list; info; info = info->next)
1896 {
95216dec 1897 if (info->assembler_name
1ccf251f
RK
1898 ? (info->assembler_name == assembler_name)
1899 : (info->name == name))
1900 {
1901 /* Yup; check it. */
3f80e6ac 1902 check_format_info (status, info, params);
1ccf251f
RK
1903 break;
1904 }
1905 }
1906}
1907
3f80e6ac
KG
1908/* This function replaces `warning' inside the printf format checking
1909 functions. If the `status' parameter is non-NULL, then it is
1910 dereferenced and set to 1 whenever a warning is caught. Otherwise
1911 it warns as usual by replicating the innards of the warning
1912 function from diagnostic.c. */
1913static void
1914status_warning VPARAMS ((int *status, const char *msgid, ...))
1915{
1916#ifndef ANSI_PROTOTYPES
1917 int *status;
1918 const char *msgid;
1919#endif
1920 va_list ap;
1921 diagnostic_context dc;
1922
1923 if (status)
1924 *status = 1;
1925 else
1926 {
1927 VA_START (ap, msgid);
1928
1929#ifndef ANSI_PROTOTYPES
1930 status = va_arg (ap, int *);
1931 msgid = va_arg (ap, const char *);
1932#endif
1933
1934 /* This duplicates the warning function behavior. */
1935 set_diagnostic_context
1936 (&dc, msgid, &ap, input_filename, lineno, /* warn = */ 1);
1937 report_diagnostic (&dc);
1938
1939 va_end (ap);
1940 }
1941}
5804a754
JM
1942
1943/* Variables used by the checking of $ operand number formats. */
1944static char *dollar_arguments_used = NULL;
1945static int dollar_arguments_alloc = 0;
1946static int dollar_arguments_count;
1947static int dollar_first_arg_num;
1948static int dollar_max_arg_used;
1949static int dollar_format_warned;
1950
1951/* Initialize the checking for a format string that may contain $
1952 parameter number specifications; we will need to keep track of whether
1953 each parameter has been used. FIRST_ARG_NUM is the number of the first
1954 argument that is a parameter to the format, or 0 for a vprintf-style
1955 function; PARAMS is the list of arguments starting at this argument. */
1956
1957static void
1958init_dollar_format_checking (first_arg_num, params)
1959 int first_arg_num;
1960 tree params;
1961{
1962 dollar_first_arg_num = first_arg_num;
1963 dollar_arguments_count = 0;
1964 dollar_max_arg_used = 0;
1965 dollar_format_warned = 0;
1966 if (first_arg_num > 0)
1967 {
1968 while (params)
1969 {
1970 dollar_arguments_count++;
1971 params = TREE_CHAIN (params);
1972 }
1973 }
1974 if (dollar_arguments_alloc < dollar_arguments_count)
1975 {
1976 if (dollar_arguments_used)
1977 free (dollar_arguments_used);
1978 dollar_arguments_alloc = dollar_arguments_count;
1979 dollar_arguments_used = xmalloc (dollar_arguments_alloc);
1980 }
1981 if (dollar_arguments_alloc)
1982 memset (dollar_arguments_used, 0, dollar_arguments_alloc);
1983}
1984
1985
1986/* Look for a decimal number followed by a $ in *FORMAT. If DOLLAR_NEEDED
1987 is set, it is an error if one is not found; otherwise, it is OK. If
1988 such a number is found, check whether it is within range and mark that
1989 numbered operand as being used for later checking. Returns the operand
1990 number if found and within range, zero if no such number was found and
1991 this is OK, or -1 on error. PARAMS points to the first operand of the
1992 format; PARAM_PTR is made to point to the parameter referred to. If
1993 a $ format is found, *FORMAT is updated to point just after it. */
1994
1995static int
3f80e6ac
KG
1996maybe_read_dollar_number (status, format, dollar_needed, params, param_ptr)
1997 int *status;
5804a754
JM
1998 const char **format;
1999 int dollar_needed;
2000 tree params;
2001 tree *param_ptr;
2002{
2003 int argnum;
2004 int overflow_flag;
2005 const char *fcp = *format;
2006 if (*fcp < '0' || *fcp > '9')
2007 {
2008 if (dollar_needed)
2009 {
3f80e6ac 2010 status_warning (status, "missing $ operand number in format");
5804a754
JM
2011 return -1;
2012 }
2013 else
2014 return 0;
2015 }
2016 argnum = 0;
2017 overflow_flag = 0;
2018 while (*fcp >= '0' && *fcp <= '9')
2019 {
2020 int nargnum;
2021 nargnum = 10 * argnum + (*fcp - '0');
2022 if (nargnum < 0 || nargnum / 10 != argnum)
2023 overflow_flag = 1;
2024 argnum = nargnum;
2025 fcp++;
2026 }
2027 if (*fcp != '$')
2028 {
2029 if (dollar_needed)
2030 {
3f80e6ac 2031 status_warning (status, "missing $ operand number in format");
5804a754
JM
2032 return -1;
2033 }
2034 else
2035 return 0;
2036 }
2037 *format = fcp + 1;
2038 if (pedantic && !dollar_format_warned)
2039 {
3f80e6ac 2040 status_warning (status, "ISO C does not support %%n$ operand number formats");
5804a754
JM
2041 dollar_format_warned = 1;
2042 }
2043 if (overflow_flag || argnum == 0
2044 || (dollar_first_arg_num && argnum > dollar_arguments_count))
2045 {
3f80e6ac 2046 status_warning (status, "operand number out of range in format");
5804a754
JM
2047 return -1;
2048 }
2049 if (argnum > dollar_max_arg_used)
2050 dollar_max_arg_used = argnum;
2051 /* For vprintf-style functions we may need to allocate more memory to
2052 track which arguments are used. */
2053 while (dollar_arguments_alloc < dollar_max_arg_used)
2054 {
2055 int nalloc;
2056 nalloc = 2 * dollar_arguments_alloc + 16;
2057 dollar_arguments_used = xrealloc (dollar_arguments_used, nalloc);
2058 memset (dollar_arguments_used + dollar_arguments_alloc, 0,
2059 nalloc - dollar_arguments_alloc);
2060 dollar_arguments_alloc = nalloc;
2061 }
2062 dollar_arguments_used[argnum - 1] = 1;
2063 if (dollar_first_arg_num)
2064 {
2065 int i;
2066 *param_ptr = params;
2067 for (i = 1; i < argnum && *param_ptr != 0; i++)
2068 *param_ptr = TREE_CHAIN (*param_ptr);
2069
2070 if (*param_ptr == 0)
2071 {
2072 /* This case shouldn't be caught here. */
2073 abort ();
2074 }
2075 }
2076 else
2077 *param_ptr = 0;
2078 return argnum;
2079}
2080
2081
2082/* Finish the checking for a format string that used $ operand number formats
2083 instead of non-$ formats. We check for unused operands before used ones
2084 (a serious error, since the implementation of the format function
2085 can't know what types to pass to va_arg to find the later arguments).
2086 and for unused operands at the end of the format (if we know how many
2087 arguments the format had, so not for vprintf). If there were operand
2088 numbers out of range on a non-vprintf-style format, we won't have reached
2089 here. */
2090
2091static void
3f80e6ac
KG
2092finish_dollar_format_checking (status)
2093 int *status;
5804a754
JM
2094{
2095 int i;
2096 for (i = 0; i < dollar_max_arg_used; i++)
2097 {
2098 if (!dollar_arguments_used[i])
3f80e6ac 2099 status_warning (status, "format argument %d unused before used argument %d in $-style format",
5804a754
JM
2100 i + 1, dollar_max_arg_used);
2101 }
2102 if (dollar_first_arg_num && dollar_max_arg_used < dollar_arguments_count)
3f80e6ac 2103 status_warning (status, "unused arguments in $-style format");
5804a754
JM
2104}
2105
2106
2588e44e
JM
2107/* Retrieve the specification for a format flag. SPEC contains the
2108 specifications for format flags for the applicable kind of format.
2109 FLAG is the flag in question. If PREDICATES is NULL, the basic
2110 spec for that flag must be retrieved and this function aborts if
2111 it cannot be found. If PREDICATES is not NULL, it is a string listing
2112 possible predicates for the spec entry; if an entry predicated on any
2113 of these is found, it is returned, otherwise NULL is returned. */
2114
2115static const format_flag_spec *
2116get_flag_spec (spec, flag, predicates)
2117 const format_flag_spec *spec;
2118 int flag;
2119 const char *predicates;
2120{
2121 int i;
2122 for (i = 0; spec[i].flag_char != 0; i++)
2123 {
2124 if (spec[i].flag_char != flag)
2125 continue;
2126 if (predicates != NULL)
2127 {
2128 if (spec[i].predicate != 0
2129 && index (predicates, spec[i].predicate) != 0)
2130 return &spec[i];
2131 }
2132 else if (spec[i].predicate == 0)
2133 return &spec[i];
2134 }
2135 if (predicates == NULL)
2136 abort ();
2137 else
2138 return NULL;
2139}
2140
2141
1ccf251f
RK
2142/* Check the argument list of a call to printf, scanf, etc.
2143 INFO points to the function_format_info structure.
2144 PARAMS is the list of argument values. */
2145
2146static void
3f80e6ac
KG
2147check_format_info (status, info, params)
2148 int *status;
1ccf251f
RK
2149 function_format_info *info;
2150 tree params;
2151{
2152 int i;
2153 int arg_num;
2588e44e 2154 int suppressed;
3c786c69
JM
2155 const char *length_chars = NULL;
2156 enum format_lengths length_chars_val = FMT_LEN_none;
2157 enum format_std_version length_chars_std = STD_C89;
1ccf251f
RK
2158 int format_char;
2159 int format_length;
2160 tree format_tree;
2161 tree cur_param;
1ccf251f 2162 tree wanted_type;
fc6df7e1
JM
2163 int main_arg_num;
2164 tree main_arg_params;
3c786c69
JM
2165 enum format_std_version wanted_type_std;
2166 const char *wanted_type_name;
6a357625
JM
2167 format_wanted_type width_wanted_type;
2168 format_wanted_type precision_wanted_type;
2169 format_wanted_type main_wanted_type;
2170 format_wanted_type *first_wanted_type;
2171 format_wanted_type *last_wanted_type;
9b69f523 2172 tree first_fillin_param;
dff01034 2173 const char *format_chars;
3c786c69 2174 const format_kind_info *fki = NULL;
2588e44e
JM
2175 const format_flag_spec *flag_specs = NULL;
2176 const format_flag_pair *bad_flag_pairs = NULL;
3c786c69
JM
2177 const format_length_info *fli = NULL;
2178 const format_char_info *fci = NULL;
2588e44e 2179 char flag_chars[256];
5804a754
JM
2180 /* -1 if no conversions taking an operand have been found; 0 if one has
2181 and it didn't use $; 1 if $ formats are in use. */
2182 int has_operand_number = -1;
1ccf251f
RK
2183
2184 /* Skip to format argument. If the argument isn't available, there's
2185 no work for us to do; prototype checking will catch the problem. */
2186 for (arg_num = 1; ; ++arg_num)
2187 {
2188 if (params == 0)
2189 return;
2190 if (arg_num == info->format_num)
2191 break;
2192 params = TREE_CHAIN (params);
2193 }
2194 format_tree = TREE_VALUE (params);
2195 params = TREE_CHAIN (params);
2196 if (format_tree == 0)
2197 return;
0161e8da 2198
1ccf251f
RK
2199 /* We can only check the format if it's a string constant. */
2200 while (TREE_CODE (format_tree) == NOP_EXPR)
2201 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
0161e8da
RK
2202
2203 if (TREE_CODE (format_tree) == CALL_EXPR
2204 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
2205 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
2206 == FUNCTION_DECL))
2207 {
2208 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
2209
2210 /* See if this is a call to a known internationalization function
2211 that modifies the format arg. */
2212 international_format_info *info;
2213
2214 for (info = international_format_list; info; info = info->next)
2215 if (info->assembler_name
2216 ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
2217 : (info->name == DECL_NAME (function)))
2218 {
2219 tree inner_args;
2220 int i;
2221
2222 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
2223 inner_args != 0;
2224 inner_args = TREE_CHAIN (inner_args), i++)
2225 if (i == info->format_num)
2226 {
2227 format_tree = TREE_VALUE (inner_args);
2228
2229 while (TREE_CODE (format_tree) == NOP_EXPR)
2230 format_tree = TREE_OPERAND (format_tree, 0);
2231 }
2232 }
2233 }
2234
39b751ce 2235 if (integer_zerop (format_tree))
1ccf251f 2236 {
3f80e6ac 2237 status_warning (status, "null format string");
1ccf251f
RK
2238 return;
2239 }
2240 if (TREE_CODE (format_tree) != ADDR_EXPR)
ba806745
KR
2241 {
2242 /* The user may get multiple warnings if the supplied argument
2243 isn't even a string pointer. */
30145215
KG
2244 /* Functions taking a va_list normally pass a non-literal format
2245 string. These functions typically are declared with
2246 first_arg_num == 0, so avoid warning in those cases. */
1d682cca 2247 if (info->first_arg_num != 0 && warn_format > 1)
3f80e6ac 2248 status_warning (status, "format not a string literal, argument types not checked");
ba806745
KR
2249 return;
2250 }
1ccf251f
RK
2251 format_tree = TREE_OPERAND (format_tree, 0);
2252 if (TREE_CODE (format_tree) != STRING_CST)
ba806745
KR
2253 {
2254 /* The user may get multiple warnings if the supplied argument
2255 isn't even a string pointer. */
30145215
KG
2256 /* Functions taking a va_list normally pass a non-literal format
2257 string. These functions typically are declared with
2258 first_arg_num == 0, so avoid warning in those cases. */
1d682cca 2259 if (info->first_arg_num != 0 && warn_format > 1)
3f80e6ac 2260 status_warning (status, "format not a string literal, argument types not checked");
ba806745
KR
2261 return;
2262 }
c5412f06
JM
2263 if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
2264 {
2265 status_warning (status, "format is a wide character string");
2266 return;
2267 }
1ccf251f
RK
2268 format_chars = TREE_STRING_POINTER (format_tree);
2269 format_length = TREE_STRING_LENGTH (format_tree);
2270 if (format_length <= 1)
3f80e6ac 2271 status_warning (status, "zero-length format string");
1ccf251f
RK
2272 if (format_chars[--format_length] != 0)
2273 {
3f80e6ac 2274 status_warning (status, "unterminated format string");
1ccf251f
RK
2275 return;
2276 }
2277 /* Skip to first argument to check. */
2278 while (arg_num + 1 < info->first_arg_num)
2279 {
2280 if (params == 0)
2281 return;
2282 params = TREE_CHAIN (params);
2283 ++arg_num;
2284 }
9b69f523
RK
2285
2286 first_fillin_param = params;
5804a754 2287 init_dollar_format_checking (info->first_arg_num, first_fillin_param);
3c786c69 2288 fki = &format_types[info->format_type];
2588e44e
JM
2289 flag_specs = fki->flag_specs;
2290 bad_flag_pairs = fki->bad_flag_pairs;
1ccf251f
RK
2291 while (1)
2292 {
af3c5588 2293 int aflag;
6a357625
JM
2294 first_wanted_type = NULL;
2295 last_wanted_type = NULL;
1ccf251f
RK
2296 if (*format_chars == 0)
2297 {
2298 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
3f80e6ac 2299 status_warning (status, "embedded `\\0' in format");
5804a754
JM
2300 if (info->first_arg_num != 0 && params != 0
2301 && has_operand_number <= 0)
3f80e6ac 2302 status_warning (status, "too many arguments for format");
5804a754 2303 if (has_operand_number > 0)
3f80e6ac 2304 finish_dollar_format_checking (status);
1ccf251f
RK
2305 return;
2306 }
2307 if (*format_chars++ != '%')
2308 continue;
2309 if (*format_chars == 0)
2310 {
3f80e6ac 2311 status_warning (status, "spurious trailing `%%' in format");
1ccf251f
RK
2312 continue;
2313 }
2314 if (*format_chars == '%')
2315 {
2316 ++format_chars;
2317 continue;
2318 }
2319 flag_chars[0] = 0;
2588e44e 2320 suppressed = FALSE;
fc6df7e1
JM
2321 main_arg_num = 0;
2322 main_arg_params = 0;
2588e44e
JM
2323
2324 if ((fki->flags & FMT_FLAG_ARG_CONVERT) && has_operand_number != 0)
1ccf251f 2325 {
2588e44e
JM
2326 /* Possibly read a $ operand number at the start of the format.
2327 If one was previously used, one is required here. If one
2328 is not used here, we can't immediately conclude this is a
2329 format without them, since it could be printf %m or scanf %*. */
2330 int opnum;
2331 opnum = maybe_read_dollar_number (status, &format_chars, 0,
2332 first_fillin_param,
2333 &main_arg_params);
2334 if (opnum == -1)
2335 return;
2336 else if (opnum > 0)
5804a754 2337 {
2588e44e
JM
2338 has_operand_number = 1;
2339 main_arg_num = opnum + info->first_arg_num - 1;
5804a754 2340 }
1ccf251f 2341 }
2588e44e
JM
2342
2343 /* Read any format flags, but do not yet validate them beyond removing
2344 duplicates, since in general validation depends on the rest of
2345 the format. */
2346 while (*format_chars != 0 && index (fki->flag_chars, *format_chars) != 0)
2347 {
2348 if (index (flag_chars, *format_chars) != 0)
bb72a084 2349 {
2588e44e
JM
2350 const format_flag_spec *s = get_flag_spec (flag_specs,
2351 *format_chars, NULL);
2352 status_warning (status, "repeated %s in format", _(s->name));
bb72a084 2353 }
2588e44e 2354 else
bb72a084
PE
2355 {
2356 i = strlen (flag_chars);
2588e44e 2357 flag_chars[i++] = *format_chars;
bb72a084 2358 flag_chars[i] = 0;
bb72a084 2359 }
2588e44e 2360 ++format_chars;
bb72a084 2361 }
9b69f523 2362
2588e44e
JM
2363 /* Read any format width, possibly * or *m$. */
2364 if (fki->width_char != 0)
2365 {
2366 if (fki->width_type != NULL && *format_chars == '*')
1ccf251f 2367 {
2588e44e
JM
2368 i = strlen (flag_chars);
2369 flag_chars[i++] = fki->width_char;
2370 flag_chars[i] = 0;
1ccf251f
RK
2371 /* "...a field width...may be indicated by an asterisk.
2372 In this case, an int argument supplies the field width..." */
2373 ++format_chars;
2374 if (params == 0)
2375 {
3f80e6ac 2376 status_warning (status, "too few arguments for format");
1ccf251f
RK
2377 return;
2378 }
fc6df7e1 2379 if (has_operand_number != 0)
5804a754
JM
2380 {
2381 int opnum;
3f80e6ac 2382 opnum = maybe_read_dollar_number (status, &format_chars,
fc6df7e1 2383 has_operand_number == 1,
5804a754
JM
2384 first_fillin_param,
2385 &params);
fc6df7e1 2386 if (opnum == -1)
5804a754 2387 return;
fc6df7e1
JM
2388 else if (opnum > 0)
2389 {
2390 has_operand_number = 1;
2391 arg_num = opnum + info->first_arg_num - 1;
2392 }
5804a754 2393 else
fc6df7e1 2394 has_operand_number = 0;
5804a754 2395 }
1ccf251f
RK
2396 if (info->first_arg_num != 0)
2397 {
2398 cur_param = TREE_VALUE (params);
5804a754
JM
2399 if (has_operand_number <= 0)
2400 {
2401 params = TREE_CHAIN (params);
2402 ++arg_num;
2403 }
2588e44e 2404 width_wanted_type.wanted_type = *fki->width_type;
6a357625
JM
2405 width_wanted_type.wanted_type_name = NULL;
2406 width_wanted_type.pointer_count = 0;
2407 width_wanted_type.char_lenient_flag = 0;
2408 width_wanted_type.writing_in_flag = 0;
2409 width_wanted_type.name = _("field width");
2410 width_wanted_type.param = cur_param;
2411 width_wanted_type.arg_num = arg_num;
2412 width_wanted_type.next = NULL;
2413 if (last_wanted_type != 0)
2414 last_wanted_type->next = &width_wanted_type;
2415 if (first_wanted_type == 0)
2416 first_wanted_type = &width_wanted_type;
2417 last_wanted_type = &width_wanted_type;
1ccf251f
RK
2418 }
2419 }
2420 else
2421 {
2588e44e
JM
2422 /* Possibly read a numeric width. If the width is zero,
2423 we complain; for scanf this is bad according to the
2424 standard, and for printf and strftime it cannot occur
2425 because 0 is a flag. */
2426 int non_zero_width_char = FALSE;
2427 int found_width = FALSE;
e9a780ec 2428 while (ISDIGIT (*format_chars))
1ccf251f 2429 {
2588e44e
JM
2430 found_width = TRUE;
2431 if (*format_chars != '0')
2432 non_zero_width_char = TRUE;
1ccf251f
RK
2433 ++format_chars;
2434 }
2588e44e
JM
2435 if (found_width && !non_zero_width_char)
2436 status_warning (status, "zero width in scanf format");
2437 if (found_width)
2438 {
2439 i = strlen (flag_chars);
2440 flag_chars[i++] = fki->width_char;
2441 flag_chars[i] = 0;
2442 }
1ccf251f 2443 }
2588e44e
JM
2444 }
2445
2446 /* Read any format precision, possibly * or *m$. */
2447 if (fki->precision_char != 0 && *format_chars == '.')
2448 {
2449 ++format_chars;
2450 i = strlen (flag_chars);
2451 flag_chars[i++] = fki->precision_char;
2452 flag_chars[i] = 0;
2453 if (fki->precision_type != NULL && *format_chars == '*')
1ccf251f 2454 {
1ccf251f
RK
2455 /* "...a...precision...may be indicated by an asterisk.
2456 In this case, an int argument supplies the...precision." */
2588e44e
JM
2457 ++format_chars;
2458 if (has_operand_number != 0)
1ccf251f 2459 {
2588e44e
JM
2460 int opnum;
2461 opnum = maybe_read_dollar_number (status, &format_chars,
2462 has_operand_number == 1,
2463 first_fillin_param,
2464 &params);
2465 if (opnum == -1)
2466 return;
2467 else if (opnum > 0)
1ccf251f 2468 {
2588e44e
JM
2469 has_operand_number = 1;
2470 arg_num = opnum + info->first_arg_num - 1;
1ccf251f 2471 }
2588e44e
JM
2472 else
2473 has_operand_number = 0;
1ccf251f 2474 }
2588e44e 2475 if (info->first_arg_num != 0)
1ccf251f 2476 {
2588e44e
JM
2477 if (params == 0)
2478 {
2479 status_warning (status, "too few arguments for format");
2480 return;
2481 }
2482 cur_param = TREE_VALUE (params);
2483 if (has_operand_number <= 0)
2484 {
2485 params = TREE_CHAIN (params);
2486 ++arg_num;
2487 }
2488 precision_wanted_type.wanted_type = *fki->precision_type;
2489 precision_wanted_type.wanted_type_name = NULL;
2490 precision_wanted_type.pointer_count = 0;
2491 precision_wanted_type.char_lenient_flag = 0;
2492 precision_wanted_type.writing_in_flag = 0;
2493 precision_wanted_type.name = _("field precision");
2494 precision_wanted_type.param = cur_param;
2495 precision_wanted_type.arg_num = arg_num;
2496 precision_wanted_type.next = NULL;
2497 if (last_wanted_type != 0)
2498 last_wanted_type->next = &precision_wanted_type;
2499 if (first_wanted_type == 0)
2500 first_wanted_type = &precision_wanted_type;
2501 last_wanted_type = &precision_wanted_type;
1ccf251f
RK
2502 }
2503 }
2588e44e
JM
2504 else
2505 {
2506 while (ISDIGIT (*format_chars))
2507 ++format_chars;
2508 }
1ccf251f 2509 }
bb72a084 2510
2588e44e 2511 /* Read any length modifier, if this kind of format has them. */
3c786c69 2512 fli = fki->length_char_specs;
2588e44e
JM
2513 length_chars = NULL;
2514 length_chars_val = FMT_LEN_none;
2515 length_chars_std = STD_C89;
3c786c69 2516 if (fli)
af3c5588 2517 {
3c786c69
JM
2518 while (fli->name != 0 && fli->name[0] != *format_chars)
2519 fli++;
2520 if (fli->name != 0)
bb72a084 2521 {
3c786c69
JM
2522 format_chars++;
2523 if (fli->double_name != 0 && fli->name[0] == *format_chars)
89abf8d1 2524 {
3c786c69
JM
2525 format_chars++;
2526 length_chars = fli->double_name;
2527 length_chars_val = fli->double_index;
2528 length_chars_std = fli->double_std;
2529 }
2530 else
2531 {
2532 length_chars = fli->name;
2533 length_chars_val = fli->index;
2534 length_chars_std = fli->std;
89abf8d1 2535 }
2588e44e
JM
2536 i = strlen (flag_chars);
2537 flag_chars[i++] = fki->length_code_char;
2538 flag_chars[i] = 0;
bb72a084 2539 }
3c786c69 2540 if (pedantic)
bc516719 2541 {
3c786c69
JM
2542 /* Warn if the length modifier is non-standard. */
2543 if (length_chars_std == STD_EXT)
3f80e6ac 2544 status_warning (status, "ISO C does not support the `%s' %s length modifier",
3c786c69
JM
2545 length_chars, fki->name);
2546 else if ((length_chars_std == STD_C99 && !flag_isoc99)
2547 || (length_chars_std == STD_C94 && !flag_isoc94))
3f80e6ac 2548 status_warning (status, "ISO C89 does not support the `%s' %s length modifier",
3c786c69 2549 length_chars, fki->name);
bc516719 2550 }
2588e44e
JM
2551 }
2552
2553 /* Read any modifier (strftime E/O). */
2554 if (fki->modifier_chars != NULL)
2555 {
2556 while (*format_chars != 0
2557 && index (fki->modifier_chars, *format_chars) != 0)
2558 {
2559 if (index (flag_chars, *format_chars) != 0)
2560 {
2561 const format_flag_spec *s = get_flag_spec (flag_specs,
2562 *format_chars, NULL);
2563 status_warning (status, "repeated %s in format", _(s->name));
2564 }
2565 else
2566 {
2567 i = strlen (flag_chars);
2568 flag_chars[i++] = *format_chars;
2569 flag_chars[i] = 0;
2570 }
2571 ++format_chars;
2572 }
2573 }
2574
2575 /* Handle the scanf allocation kludge. */
2576 if (fki->flags & FMT_FLAG_SCANF_A_KLUDGE)
2577 {
2578 if (*format_chars == 'a' && !flag_isoc99)
bb72a084
PE
2579 {
2580 if (format_chars[1] == 's' || format_chars[1] == 'S'
2581 || format_chars[1] == '[')
2582 {
2583 /* `a' is used as a flag. */
2588e44e
JM
2584 i = strlen (flag_chars);
2585 flag_chars[i++] = 'a';
2586 flag_chars[i] = 0;
bb72a084
PE
2587 format_chars++;
2588 }
2589 }
1ccf251f 2590 }
2588e44e 2591
1ccf251f 2592 format_char = *format_chars;
bb72a084 2593 if (format_char == 0
2588e44e 2594 || (!(fki->flags & FMT_FLAG_FANCY_PERCENT_OK) && format_char == '%'))
1ccf251f 2595 {
3f80e6ac 2596 status_warning (status, "conversion lacks type at end of format");
1ccf251f
RK
2597 continue;
2598 }
2599 format_chars++;
3c786c69 2600 fci = fki->conversion_specs;
1ccf251f
RK
2601 while (fci->format_chars != 0
2602 && index (fci->format_chars, format_char) == 0)
2603 ++fci;
2604 if (fci->format_chars == 0)
2605 {
f3ad1f9c 2606 if (ISGRAPH(format_char))
3f80e6ac 2607 status_warning (status, "unknown conversion type character `%c' in format",
1ccf251f
RK
2608 format_char);
2609 else
3f80e6ac 2610 status_warning (status, "unknown conversion type character 0x%x in format",
1ccf251f 2611 format_char);
1ccf251f
RK
2612 continue;
2613 }
bb72a084 2614 if (pedantic)
1ccf251f 2615 {
3c786c69 2616 if (fci->std == STD_EXT)
3f80e6ac 2617 status_warning (status, "ISO C does not support the `%%%c' %s format",
3c786c69
JM
2618 format_char, fki->name);
2619 else if ((fci->std == STD_C99 && !flag_isoc99)
2620 || (fci->std == STD_C94 && !flag_isoc94))
3f80e6ac 2621 status_warning (status, "ISO C89 does not support the `%%%c' %s format",
3c786c69 2622 format_char, fki->name);
1ccf251f 2623 }
2588e44e
JM
2624
2625 /* Validate the individual flags used, removing any that are invalid. */
2626 {
2627 int d = 0;
2628 for (i = 0; flag_chars[i] != 0; i++)
2629 {
2630 const format_flag_spec *s = get_flag_spec (flag_specs,
2631 flag_chars[i], NULL);
2632 flag_chars[i - d] = flag_chars[i];
2633 if (flag_chars[i] == fki->length_code_char)
2634 continue;
2635 if (index (fci->flag_chars, flag_chars[i]) == 0)
2636 {
2637 status_warning (status, "%s used with `%%%c' %s format",
2638 _(s->name), format_char, fki->name);
2639 d++;
2640 continue;
2641 }
2642 if (pedantic)
2643 {
2644 const format_flag_spec *t;
2645 if (s->std == STD_EXT)
2646 status_warning (status, "ISO C does not support %s",
2647 _(s->long_name));
2648 else if ((s->std == STD_C99 && !flag_isoc99)
2649 || (s->std == STD_C94 && !flag_isoc94))
2650 status_warning (status, "ISO C89 does not support %s",
2651 _(s->long_name));
2652 t = get_flag_spec (flag_specs, flag_chars[i], fci->flags2);
2653 if (t != NULL && t->std > s->std)
2654 {
2655 const char *long_name = (t->long_name != NULL
2656 ? t->long_name
2657 : s->long_name);
2658 if (t->std == STD_EXT)
2659 status_warning (status, "ISO C does not support %s with the `%%%c' %s format",
2660 _(long_name), format_char, fki->name);
2661 else if ((t->std == STD_C99 && !flag_isoc99)
2662 || (t->std == STD_C94 && !flag_isoc94))
2663 status_warning (status, "ISO C89 does not support %s with the `%%%c' %s format",
2664 _(long_name), format_char, fki->name);
2665 }
2666 }
2667 }
2668 flag_chars[i - d] = 0;
2669 }
2670
2671 aflag = 0;
2672 if ((fki->flags & FMT_FLAG_SCANF_A_KLUDGE)
2673 && index (flag_chars, 'a') != 0)
2674 aflag = 1;
2675
2676 if (fki->suppression_char
2677 && index (flag_chars, fki->suppression_char) != 0)
2678 suppressed = 1;
2679
2680 /* Validate the pairs of flags used. */
2681 for (i = 0; bad_flag_pairs[i].flag_char1 != 0; i++)
af3c5588 2682 {
2588e44e
JM
2683 const format_flag_spec *s, *t;
2684 if (index (flag_chars, bad_flag_pairs[i].flag_char1) == 0)
2685 continue;
2686 if (index (flag_chars, bad_flag_pairs[i].flag_char2) == 0)
2687 continue;
2688 if (bad_flag_pairs[i].predicate != 0
2689 && index (fci->flags2, bad_flag_pairs[i].predicate) == 0)
2690 continue;
2691 s = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char1, NULL);
2692 t = get_flag_spec (flag_specs, bad_flag_pairs[i].flag_char2, NULL);
2693 if (bad_flag_pairs[i].ignored)
2694 {
2695 if (bad_flag_pairs[i].predicate != 0)
2696 status_warning (status, "%s ignored with %s and `%%%c' %s format",
2697 _(s->name), _(t->name), format_char,
2698 fki->name);
2699 else
2700 status_warning (status, "%s ignored with %s in %s format",
2701 _(s->name), _(t->name), fki->name);
2702 }
2703 else
2704 {
2705 if (bad_flag_pairs[i].predicate != 0)
2706 status_warning (status, "use of %s and %s together with `%%%c' %s format",
2707 _(s->name), _(t->name), format_char,
2708 fki->name);
2709 else
2710 status_warning (status, "use of %s and %s together in %s format",
2711 _(s->name), _(t->name), fki->name);
2712 }
1ccf251f 2713 }
2588e44e
JM
2714
2715 /* Give Y2K warnings. */
2716 {
2717 int y2k_level = 0;
2718 if (index (fci->flags2, '4') != 0)
2719 if (index (flag_chars, 'E') != 0)
2720 y2k_level = 3;
2721 else
2722 y2k_level = 2;
2723 else if (index (fci->flags2, '3') != 0)
2724 y2k_level = 3;
2725 else if (index (fci->flags2, '2') != 0)
2726 y2k_level = 2;
2727 if (y2k_level == 3)
2728 status_warning (status, "`%%%c' yields only last 2 digits of year in some locales",
2729 format_char);
2730 else if (y2k_level == 2)
2731 status_warning (status, "`%%%c' yields only last 2 digits of year", format_char);
2732 }
2733
0e00b68b 2734 if (index (fci->flags2, '[') != 0)
1ccf251f
RK
2735 {
2736 /* Skip over scan set, in case it happens to have '%' in it. */
2737 if (*format_chars == '^')
2738 ++format_chars;
2739 /* Find closing bracket; if one is hit immediately, then
2740 it's part of the scan set rather than a terminator. */
2741 if (*format_chars == ']')
2742 ++format_chars;
2743 while (*format_chars && *format_chars != ']')
2744 ++format_chars;
2745 if (*format_chars != ']')
bb72a084 2746 /* The end of the format string was reached. */
3f80e6ac 2747 status_warning (status, "no closing `]' for `%%[' format");
1ccf251f 2748 }
2588e44e
JM
2749
2750 if (fki->flags & FMT_FLAG_ARG_CONVERT)
6a357625 2751 {
2588e44e
JM
2752 wanted_type = (fci->types[length_chars_val].type
2753 ? *fci->types[length_chars_val].type : 0);
2754 wanted_type_name = fci->types[length_chars_val].name;
2755 wanted_type_std = fci->types[length_chars_val].std;
2756 if (wanted_type == 0)
2757 {
2758 status_warning (status, "use of `%s' length modifier with `%c' type character",
2759 length_chars, format_char);
2760 /* Heuristic: skip one argument when an invalid length/type
2761 combination is encountered. */
2762 arg_num++;
2763 if (params == 0)
2764 {
2765 status_warning (status, "too few arguments for format");
2766 return;
2767 }
2768 params = TREE_CHAIN (params);
2769 continue;
2770 }
2771 else if (pedantic
2772 /* Warn if non-standard, provided it is more non-standard
2773 than the length and type characters that may already
2774 have been warned for. */
2775 && wanted_type_std > length_chars_std
2776 && wanted_type_std > fci->std)
6a357625 2777 {
2588e44e
JM
2778 if (wanted_type_std == STD_EXT)
2779 status_warning (status, "ISO C does not support the `%%%s%c' %s format",
2780 length_chars, format_char, fki->name);
2781 else if ((wanted_type_std == STD_C99 && !flag_isoc99)
2782 || (wanted_type_std == STD_C94 && !flag_isoc94))
2783 status_warning (status, "ISO C89 does not support the `%%%s%c' %s format",
2784 length_chars, format_char, fki->name);
6a357625 2785 }
1ccf251f 2786 }
1ccf251f 2787
1ccf251f
RK
2788 /* Finally. . .check type of argument against desired type! */
2789 if (info->first_arg_num == 0)
2790 continue;
2588e44e
JM
2791 if ((fci->pointer_count == 0 && wanted_type == void_type_node)
2792 || suppressed)
1ccf251f 2793 {
fc6df7e1 2794 if (main_arg_num != 0)
2588e44e
JM
2795 {
2796 if (suppressed)
2797 status_warning (status, "operand number specified with suppressed assignment");
2798 else
2799 status_warning (status, "operand number specified for format taking no argument");
2800 }
fc6df7e1
JM
2801 }
2802 else
2803 {
2804 if (main_arg_num != 0)
6a357625 2805 {
fc6df7e1
JM
2806 arg_num = main_arg_num;
2807 params = main_arg_params;
2808 }
2809 else
2810 {
2811 ++arg_num;
2812 if (has_operand_number > 0)
2813 {
3f80e6ac 2814 status_warning (status, "missing $ operand number in format");
fc6df7e1
JM
2815 return;
2816 }
2817 else
2818 has_operand_number = 0;
2819 if (params == 0)
2820 {
3f80e6ac 2821 status_warning (status, "too few arguments for format");
fc6df7e1
JM
2822 return;
2823 }
6a357625
JM
2824 }
2825 cur_param = TREE_VALUE (params);
2826 params = TREE_CHAIN (params);
6a357625
JM
2827 main_wanted_type.wanted_type = wanted_type;
2828 main_wanted_type.wanted_type_name = wanted_type_name;
2829 main_wanted_type.pointer_count = fci->pointer_count + aflag;
2830 main_wanted_type.char_lenient_flag = 0;
0e00b68b 2831 if (index (fci->flags2, 'c') != 0)
6a357625
JM
2832 main_wanted_type.char_lenient_flag = 1;
2833 main_wanted_type.writing_in_flag = 0;
0e00b68b 2834 if (index (fci->flags2, 'W') != 0)
6a357625
JM
2835 main_wanted_type.writing_in_flag = 1;
2836 main_wanted_type.name = NULL;
2837 main_wanted_type.param = cur_param;
2838 main_wanted_type.arg_num = arg_num;
2839 main_wanted_type.next = NULL;
2840 if (last_wanted_type != 0)
2841 last_wanted_type->next = &main_wanted_type;
2842 if (first_wanted_type == 0)
2843 first_wanted_type = &main_wanted_type;
2844 last_wanted_type = &main_wanted_type;
1ccf251f 2845 }
6a357625
JM
2846
2847 if (first_wanted_type != 0)
3f80e6ac 2848 check_format_types (status, first_wanted_type);
6a357625
JM
2849
2850 }
2851}
2852
2853
2854/* Check the argument types from a single format conversion (possibly
2855 including width and precision arguments). */
2856static void
3f80e6ac
KG
2857check_format_types (status, types)
2858 int *status;
6a357625
JM
2859 format_wanted_type *types;
2860{
2861 for (; types != 0; types = types->next)
2862 {
2863 tree cur_param;
2864 tree cur_type;
99303ae4 2865 tree orig_cur_type;
6a357625
JM
2866 tree wanted_type;
2867 int arg_num;
2868 int i;
2869 int char_type_flag;
2870 cur_param = types->param;
1ccf251f 2871 cur_type = TREE_TYPE (cur_param);
99303ae4
JM
2872 if (TREE_CODE (cur_type) == ERROR_MARK)
2873 continue;
6a357625
JM
2874 char_type_flag = 0;
2875 wanted_type = types->wanted_type;
2876 arg_num = types->arg_num;
1ccf251f 2877
99303ae4
JM
2878 /* The following should not occur here. */
2879 if (wanted_type == 0)
2880 abort ();
2881 if (wanted_type == void_type_node && types->pointer_count == 0)
2882 abort ();
2883
6d819282
MK
2884 STRIP_NOPS (cur_param);
2885
1ccf251f
RK
2886 /* Check the types of any additional pointer arguments
2887 that precede the "real" argument. */
6a357625 2888 for (i = 0; i < types->pointer_count; ++i)
1ccf251f
RK
2889 {
2890 if (TREE_CODE (cur_type) == POINTER_TYPE)
2891 {
2892 cur_type = TREE_TYPE (cur_type);
99303ae4
JM
2893 if (TREE_CODE (cur_type) == ERROR_MARK)
2894 break;
6d819282 2895
f5963e61 2896 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
6d819282
MK
2897 cur_param = TREE_OPERAND (cur_param, 0);
2898 else
2899 cur_param = 0;
2900
f3d360aa
JM
2901 /* See if this is an attempt to write into a const type with
2902 scanf or with printf "%n". Note: the writing in happens
2903 at the first indirection only, if for example
2904 void * const * is passed to scanf %p; passing
2905 const void ** is simply passing an incompatible type. */
6a357625 2906 if (types->writing_in_flag
f3d360aa 2907 && i == 0
f3d360aa
JM
2908 && (TYPE_READONLY (cur_type)
2909 || (cur_param != 0
2910 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
2911 || (DECL_P (cur_param)
2912 && TREE_READONLY (cur_param))))))
3f80e6ac 2913 status_warning (status, "writing into constant object (arg %d)", arg_num);
f3d360aa
JM
2914
2915 /* If there are extra type qualifiers beyond the first
2916 indirection, then this makes the types technically
2917 incompatible. */
2918 if (i > 0
2919 && pedantic
f3d360aa
JM
2920 && (TYPE_READONLY (cur_type)
2921 || TYPE_VOLATILE (cur_type)
2922 || TYPE_RESTRICT (cur_type)))
3f80e6ac 2923 status_warning (status, "extra type qualifiers in format argument (arg %d)",
f3d360aa
JM
2924 arg_num);
2925
1ccf251f 2926 }
99303ae4 2927 else
913d0833 2928 {
6a357625 2929 if (types->pointer_count == 1)
3f80e6ac 2930 status_warning (status, "format argument is not a pointer (arg %d)", arg_num);
913d0833 2931 else
3f80e6ac 2932 status_warning (status, "format argument is not a pointer to a pointer (arg %d)", arg_num);
99303ae4 2933 break;
913d0833 2934 }
1ccf251f
RK
2935 }
2936
99303ae4
JM
2937 if (i < types->pointer_count)
2938 continue;
2939
2940 orig_cur_type = cur_type;
2941 cur_type = TYPE_MAIN_VARIANT (cur_type);
2942
cd732418
JM
2943 /* Check whether the argument type is a character type. This leniency
2944 only applies to certain formats, flagged with 'c'.
2945 */
99303ae4
JM
2946 if (types->char_lenient_flag)
2947 char_type_flag = (cur_type == char_type_node
2948 || cur_type == signed_char_type_node
2949 || cur_type == unsigned_char_type_node);
b8458e3e 2950
1ccf251f 2951 /* Check the type of the "real" argument, if there's a type we want. */
99303ae4
JM
2952 if (wanted_type == cur_type)
2953 continue;
2954 /* If we want `void *', allow any pointer type.
2955 (Anything else would already have got a warning.)
2956 With -pedantic, only allow pointers to void and to character
2957 types. */
2958 if (wanted_type == void_type_node
2959 && (!pedantic || (i == 1 && char_type_flag)))
2960 continue;
2961 /* Don't warn about differences merely in signedness, unless
2962 -pedantic. With -pedantic, warn if the type is a pointer
2963 target and not a character type, and for character types at
2964 a second level of indirection. */
2965 if (TREE_CODE (wanted_type) == INTEGER_TYPE
2966 && TREE_CODE (cur_type) == INTEGER_TYPE
2967 && (! pedantic || i == 0 || (i == 1 && char_type_flag))
2968 && (TREE_UNSIGNED (wanted_type)
2969 ? wanted_type == unsigned_type (cur_type)
2970 : wanted_type == signed_type (cur_type)))
2971 continue;
2972 /* Likewise, "signed char", "unsigned char" and "char" are
2973 equivalent but the above test won't consider them equivalent. */
2974 if (wanted_type == char_type_node
2975 && (! pedantic || i < 2)
2976 && char_type_flag)
2977 continue;
2978 /* Now we have a type mismatch. */
2979 {
2980 register const char *this;
2981 register const char *that;
2982
2983 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
2984 that = 0;
2985 if (TYPE_NAME (orig_cur_type) != 0
2986 && TREE_CODE (orig_cur_type) != INTEGER_TYPE
2987 && !(TREE_CODE (orig_cur_type) == POINTER_TYPE
2988 && TREE_CODE (TREE_TYPE (orig_cur_type)) == INTEGER_TYPE))
2989 {
2990 if (TREE_CODE (TYPE_NAME (orig_cur_type)) == TYPE_DECL
2991 && DECL_NAME (TYPE_NAME (orig_cur_type)) != 0)
2992 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type)));
2993 else
2994 that = IDENTIFIER_POINTER (TYPE_NAME (orig_cur_type));
2995 }
1ccf251f 2996
99303ae4
JM
2997 /* A nameless type can't possibly match what the format wants.
2998 So there will be a warning for it.
2999 Make up a string to describe vaguely what it is. */
3000 if (that == 0)
3001 {
3002 if (TREE_CODE (orig_cur_type) == POINTER_TYPE)
3003 that = "pointer";
3004 else
3005 that = "different type";
3006 }
1ccf251f 3007
99303ae4
JM
3008 /* Make the warning better in case of mismatch of int vs long. */
3009 if (TREE_CODE (orig_cur_type) == INTEGER_TYPE
3010 && TREE_CODE (wanted_type) == INTEGER_TYPE
3011 && TYPE_PRECISION (orig_cur_type) == TYPE_PRECISION (wanted_type)
3012 && TYPE_NAME (orig_cur_type) != 0
3013 && TREE_CODE (TYPE_NAME (orig_cur_type)) == TYPE_DECL)
3014 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (orig_cur_type)));
309ffab6 3015
99303ae4
JM
3016 if (strcmp (this, that) != 0)
3017 {
3018 /* There may be a better name for the format, e.g. size_t,
3019 but we should allow for programs with a perverse typedef
3020 making size_t something other than what the compiler
3021 thinks. */
3022 if (types->wanted_type_name != 0
3023 && strcmp (types->wanted_type_name, that) != 0)
3024 this = types->wanted_type_name;
3025 if (types->name != 0)
3f80e6ac 3026 status_warning (status, "%s is not type %s (arg %d)", types->name, this,
99303ae4
JM
3027 arg_num);
3028 else
3f80e6ac 3029 status_warning (status, "%s format, %s arg (arg %d)", this, that, arg_num);
99303ae4
JM
3030 }
3031 }
1ccf251f
RK
3032 }
3033}
3034\f
d74154d5
RS
3035/* Print a warning if a constant expression had overflow in folding.
3036 Invoke this function on every expression that the language
3037 requires to be a constant expression.
3038 Note the ANSI C standard says it is erroneous for a
3039 constant expression to overflow. */
96571883
BK
3040
3041void
3042constant_expression_warning (value)
3043 tree value;
3044{
c05f751c
RK
3045 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
3046 || TREE_CODE (value) == COMPLEX_CST)
3047 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
3048 pedwarn ("overflow in constant expression");
d74154d5
RS
3049}
3050
3051/* Print a warning if an expression had overflow in folding.
3052 Invoke this function on every expression that
3053 (1) appears in the source code, and
3054 (2) might be a constant expression that overflowed, and
3055 (3) is not already checked by convert_and_check;
3056 however, do not invoke this function on operands of explicit casts. */
3057
3058void
3059overflow_warning (value)
3060 tree value;
3061{
c05f751c
RK
3062 if ((TREE_CODE (value) == INTEGER_CST
3063 || (TREE_CODE (value) == COMPLEX_CST
3064 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
3065 && TREE_OVERFLOW (value))
d74154d5 3066 {
7193bce2 3067 TREE_OVERFLOW (value) = 0;
e78a3b42
RK
3068 if (skip_evaluation == 0)
3069 warning ("integer overflow in expression");
d74154d5 3070 }
c05f751c
RK
3071 else if ((TREE_CODE (value) == REAL_CST
3072 || (TREE_CODE (value) == COMPLEX_CST
3073 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
3074 && TREE_OVERFLOW (value))
3075 {
3076 TREE_OVERFLOW (value) = 0;
e78a3b42
RK
3077 if (skip_evaluation == 0)
3078 warning ("floating point overflow in expression");
c05f751c 3079 }
d74154d5
RS
3080}
3081
3082/* Print a warning if a large constant is truncated to unsigned,
3083 or if -Wconversion is used and a constant < 0 is converted to unsigned.
3084 Invoke this function on every expression that might be implicitly
3085 converted to an unsigned type. */
3086
3087void
3088unsigned_conversion_warning (result, operand)
3089 tree result, operand;
3090{
3091 if (TREE_CODE (operand) == INTEGER_CST
3092 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
3093 && TREE_UNSIGNED (TREE_TYPE (result))
e78a3b42 3094 && skip_evaluation == 0
d74154d5
RS
3095 && !int_fits_type_p (operand, TREE_TYPE (result)))
3096 {
3097 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
3098 /* This detects cases like converting -129 or 256 to unsigned char. */
90c939d4 3099 warning ("large integer implicitly truncated to unsigned type");
d74154d5 3100 else if (warn_conversion)
90c939d4 3101 warning ("negative integer implicitly converted to unsigned type");
d74154d5
RS
3102 }
3103}
3104
3105/* Convert EXPR to TYPE, warning about conversion problems with constants.
3106 Invoke this function on every expression that is converted implicitly,
3107 i.e. because of language rules and not because of an explicit cast. */
3108
3109tree
3110convert_and_check (type, expr)
3111 tree type, expr;
3112{
3113 tree t = convert (type, expr);
3114 if (TREE_CODE (t) == INTEGER_CST)
3115 {
7193bce2 3116 if (TREE_OVERFLOW (t))
d74154d5 3117 {
7193bce2
PE
3118 TREE_OVERFLOW (t) = 0;
3119
868fc750
RK
3120 /* Do not diagnose overflow in a constant expression merely
3121 because a conversion overflowed. */
3122 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
3123
7193bce2
PE
3124 /* No warning for converting 0x80000000 to int. */
3125 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
3126 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
3127 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
22ba338b
RS
3128 /* If EXPR fits in the unsigned version of TYPE,
3129 don't warn unless pedantic. */
e78a3b42
RK
3130 if ((pedantic
3131 || TREE_UNSIGNED (type)
3132 || ! int_fits_type_p (expr, unsigned_type (type)))
3133 && skip_evaluation == 0)
bb72a084 3134 warning ("overflow in implicit constant conversion");
d74154d5
RS
3135 }
3136 else
3137 unsigned_conversion_warning (t, expr);
3138 }
3139 return t;
96571883
BK
3140}
3141\f
b30f223b
RS
3142void
3143c_expand_expr_stmt (expr)
3144 tree expr;
3145{
3146 /* Do default conversion if safe and possibly important,
3147 in case within ({...}). */
3148 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
3149 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
3150 expr = default_conversion (expr);
3151
3152 if (TREE_TYPE (expr) != error_mark_node
b8de2d02 3153 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
b30f223b
RS
3154 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
3155 error ("expression statement has incomplete type");
3156
8f17b5c5
MM
3157 last_expr_type = TREE_TYPE (expr);
3158 add_stmt (build_stmt (EXPR_STMT, expr));
b30f223b
RS
3159}
3160\f
3161/* Validate the expression after `case' and apply default promotions. */
3162
3163tree
3164check_case_value (value)
3165 tree value;
3166{
3167 if (value == NULL_TREE)
3168 return value;
3169
3170 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8493738b 3171 STRIP_TYPE_NOPS (value);
56cb9733
MM
3172 /* In C++, the following is allowed:
3173
3174 const int i = 3;
3175 switch (...) { case i: ... }
3176
3177 So, we try to reduce the VALUE to a constant that way. */
3178 if (c_language == clk_cplusplus)
3179 {
3180 value = decl_constant_value (value);
3181 STRIP_TYPE_NOPS (value);
3182 value = fold (value);
3183 }
b30f223b
RS
3184
3185 if (TREE_CODE (value) != INTEGER_CST
3186 && value != error_mark_node)
3187 {
3188 error ("case label does not reduce to an integer constant");
3189 value = error_mark_node;
3190 }
3191 else
3192 /* Promote char or short to int. */
3193 value = default_conversion (value);
3194
bc690db1
RS
3195 constant_expression_warning (value);
3196
b30f223b
RS
3197 return value;
3198}
3199\f
3200/* Return an integer type with BITS bits of precision,
3201 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3202
3203tree
3204type_for_size (bits, unsignedp)
3205 unsigned bits;
3206 int unsignedp;
3207{
a311b52c
JM
3208 if (bits == TYPE_PRECISION (integer_type_node))
3209 return unsignedp ? unsigned_type_node : integer_type_node;
3210
3fc7e390 3211 if (bits == TYPE_PRECISION (signed_char_type_node))
b30f223b
RS
3212 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3213
3fc7e390 3214 if (bits == TYPE_PRECISION (short_integer_type_node))
b30f223b
RS
3215 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3216
3fc7e390 3217 if (bits == TYPE_PRECISION (long_integer_type_node))
b30f223b
RS
3218 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3219
3fc7e390 3220 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b30f223b
RS
3221 return (unsignedp ? long_long_unsigned_type_node
3222 : long_long_integer_type_node);
3223
835f9b4d
GRK
3224 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3225 return (unsignedp ? widest_unsigned_literal_type_node
3226 : widest_integer_literal_type_node);
3227
3fc7e390
RS
3228 if (bits <= TYPE_PRECISION (intQI_type_node))
3229 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3230
3231 if (bits <= TYPE_PRECISION (intHI_type_node))
3232 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3233
3234 if (bits <= TYPE_PRECISION (intSI_type_node))
3235 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3236
3237 if (bits <= TYPE_PRECISION (intDI_type_node))
3238 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3239
b30f223b
RS
3240 return 0;
3241}
3242
3243/* Return a data type that has machine mode MODE.
3244 If the mode is an integer,
3245 then UNSIGNEDP selects between signed and unsigned types. */
3246
3247tree
3248type_for_mode (mode, unsignedp)
3249 enum machine_mode mode;
3250 int unsignedp;
3251{
a311b52c
JM
3252 if (mode == TYPE_MODE (integer_type_node))
3253 return unsignedp ? unsigned_type_node : integer_type_node;
3254
b30f223b
RS
3255 if (mode == TYPE_MODE (signed_char_type_node))
3256 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3257
3258 if (mode == TYPE_MODE (short_integer_type_node))
3259 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3260
b30f223b
RS
3261 if (mode == TYPE_MODE (long_integer_type_node))
3262 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3263
3264 if (mode == TYPE_MODE (long_long_integer_type_node))
3265 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3266
835f9b4d 3267 if (mode == TYPE_MODE (widest_integer_literal_type_node))
d125d268 3268 return unsignedp ? widest_unsigned_literal_type_node
835f9b4d
GRK
3269 : widest_integer_literal_type_node;
3270
3fc7e390
RS
3271 if (mode == TYPE_MODE (intQI_type_node))
3272 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3273
3274 if (mode == TYPE_MODE (intHI_type_node))
3275 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3276
3277 if (mode == TYPE_MODE (intSI_type_node))
3278 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3279
3280 if (mode == TYPE_MODE (intDI_type_node))
3281 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3282
21a9616b 3283#if HOST_BITS_PER_WIDE_INT >= 64
a6d7e156
JL
3284 if (mode == TYPE_MODE (intTI_type_node))
3285 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
21a9616b 3286#endif
a6d7e156 3287
b30f223b
RS
3288 if (mode == TYPE_MODE (float_type_node))
3289 return float_type_node;
3290
3291 if (mode == TYPE_MODE (double_type_node))
3292 return double_type_node;
3293
3294 if (mode == TYPE_MODE (long_double_type_node))
3295 return long_double_type_node;
3296
3297 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
3298 return build_pointer_type (char_type_node);
3299
3300 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
3301 return build_pointer_type (integer_type_node);
3302
4061f623
BS
3303#ifdef VECTOR_MODE_SUPPORTED_P
3304 if (mode == TYPE_MODE (V4SF_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
3305 return V4SF_type_node;
3306 if (mode == TYPE_MODE (V4SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
3307 return V4SI_type_node;
3308 if (mode == TYPE_MODE (V2SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
3309 return V2SI_type_node;
3310 if (mode == TYPE_MODE (V4HI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
3311 return V4HI_type_node;
3312 if (mode == TYPE_MODE (V8QI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
3313 return V8QI_type_node;
3314#endif
3315
b30f223b
RS
3316 return 0;
3317}
693a6128
GRK
3318
3319/* Return an unsigned type the same as TYPE in other respects. */
3320tree
3321unsigned_type (type)
3322 tree type;
3323{
3324 tree type1 = TYPE_MAIN_VARIANT (type);
3325 if (type1 == signed_char_type_node || type1 == char_type_node)
3326 return unsigned_char_type_node;
3327 if (type1 == integer_type_node)
3328 return unsigned_type_node;
3329 if (type1 == short_integer_type_node)
3330 return short_unsigned_type_node;
3331 if (type1 == long_integer_type_node)
3332 return long_unsigned_type_node;
3333 if (type1 == long_long_integer_type_node)
3334 return long_long_unsigned_type_node;
3335 if (type1 == widest_integer_literal_type_node)
3336 return widest_unsigned_literal_type_node;
3337#if HOST_BITS_PER_WIDE_INT >= 64
3338 if (type1 == intTI_type_node)
3339 return unsigned_intTI_type_node;
3340#endif
3341 if (type1 == intDI_type_node)
3342 return unsigned_intDI_type_node;
3343 if (type1 == intSI_type_node)
3344 return unsigned_intSI_type_node;
3345 if (type1 == intHI_type_node)
3346 return unsigned_intHI_type_node;
3347 if (type1 == intQI_type_node)
3348 return unsigned_intQI_type_node;
3349
3350 return signed_or_unsigned_type (1, type);
3351}
3352
3353/* Return a signed type the same as TYPE in other respects. */
3354
3355tree
3356signed_type (type)
3357 tree type;
3358{
3359 tree type1 = TYPE_MAIN_VARIANT (type);
3360 if (type1 == unsigned_char_type_node || type1 == char_type_node)
3361 return signed_char_type_node;
3362 if (type1 == unsigned_type_node)
3363 return integer_type_node;
3364 if (type1 == short_unsigned_type_node)
3365 return short_integer_type_node;
3366 if (type1 == long_unsigned_type_node)
3367 return long_integer_type_node;
3368 if (type1 == long_long_unsigned_type_node)
3369 return long_long_integer_type_node;
3370 if (type1 == widest_unsigned_literal_type_node)
3371 return widest_integer_literal_type_node;
3372#if HOST_BITS_PER_WIDE_INT >= 64
3373 if (type1 == unsigned_intTI_type_node)
3374 return intTI_type_node;
3375#endif
3376 if (type1 == unsigned_intDI_type_node)
3377 return intDI_type_node;
3378 if (type1 == unsigned_intSI_type_node)
3379 return intSI_type_node;
3380 if (type1 == unsigned_intHI_type_node)
3381 return intHI_type_node;
3382 if (type1 == unsigned_intQI_type_node)
3383 return intQI_type_node;
3384
3385 return signed_or_unsigned_type (0, type);
3386}
3387
3388/* Return a type the same as TYPE except unsigned or
3389 signed according to UNSIGNEDP. */
3390
3391tree
3392signed_or_unsigned_type (unsignedp, type)
3393 int unsignedp;
3394 tree type;
3395{
3396 if (! INTEGRAL_TYPE_P (type)
3397 || TREE_UNSIGNED (type) == unsignedp)
3398 return type;
3399
3400 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
3401 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
d125d268 3402 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
693a6128 3403 return unsignedp ? unsigned_type_node : integer_type_node;
d125d268 3404 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
693a6128 3405 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
d125d268 3406 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
693a6128 3407 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
d125d268 3408 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
693a6128
GRK
3409 return (unsignedp ? long_long_unsigned_type_node
3410 : long_long_integer_type_node);
d125d268 3411 if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
693a6128
GRK
3412 return (unsignedp ? widest_unsigned_literal_type_node
3413 : widest_integer_literal_type_node);
3414 return type;
3415}
b30f223b 3416\f
6acfe908
JM
3417/* Return the minimum number of bits needed to represent VALUE in a
3418 signed or unsigned type, UNSIGNEDP says which. */
3419
05bccae2 3420unsigned int
6acfe908
JM
3421min_precision (value, unsignedp)
3422 tree value;
3423 int unsignedp;
3424{
3425 int log;
3426
3427 /* If the value is negative, compute its negative minus 1. The latter
3428 adjustment is because the absolute value of the largest negative value
3429 is one larger than the largest positive value. This is equivalent to
3430 a bit-wise negation, so use that operation instead. */
3431
3432 if (tree_int_cst_sgn (value) < 0)
3433 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
3434
3435 /* Return the number of bits needed, taking into account the fact
3436 that we need one more bit for a signed than unsigned type. */
3437
3438 if (integer_zerop (value))
3439 log = 0;
6acfe908 3440 else
05bccae2 3441 log = tree_floor_log2 (value);
6acfe908
JM
3442
3443 return log + 1 + ! unsignedp;
3444}
3445\f
b30f223b
RS
3446/* Print an error message for invalid operands to arith operation CODE.
3447 NOP_EXPR is used as a special case (see truthvalue_conversion). */
3448
3449void
3450binary_op_error (code)
3451 enum tree_code code;
3452{
dff01034 3453 register const char *opname;
89c78d7d 3454
b30f223b
RS
3455 switch (code)
3456 {
3457 case NOP_EXPR:
3458 error ("invalid truth-value expression");
3459 return;
3460
3461 case PLUS_EXPR:
3462 opname = "+"; break;
3463 case MINUS_EXPR:
3464 opname = "-"; break;
3465 case MULT_EXPR:
3466 opname = "*"; break;
3467 case MAX_EXPR:
3468 opname = "max"; break;
3469 case MIN_EXPR:
3470 opname = "min"; break;
3471 case EQ_EXPR:
3472 opname = "=="; break;
3473 case NE_EXPR:
3474 opname = "!="; break;
3475 case LE_EXPR:
3476 opname = "<="; break;
3477 case GE_EXPR:
3478 opname = ">="; break;
3479 case LT_EXPR:
3480 opname = "<"; break;
3481 case GT_EXPR:
3482 opname = ">"; break;
3483 case LSHIFT_EXPR:
3484 opname = "<<"; break;
3485 case RSHIFT_EXPR:
3486 opname = ">>"; break;
3487 case TRUNC_MOD_EXPR:
047de90b 3488 case FLOOR_MOD_EXPR:
b30f223b
RS
3489 opname = "%"; break;
3490 case TRUNC_DIV_EXPR:
047de90b 3491 case FLOOR_DIV_EXPR:
b30f223b
RS
3492 opname = "/"; break;
3493 case BIT_AND_EXPR:
3494 opname = "&"; break;
3495 case BIT_IOR_EXPR:
3496 opname = "|"; break;
3497 case TRUTH_ANDIF_EXPR:
3498 opname = "&&"; break;
3499 case TRUTH_ORIF_EXPR:
3500 opname = "||"; break;
3501 case BIT_XOR_EXPR:
3502 opname = "^"; break;
047de90b
RS
3503 case LROTATE_EXPR:
3504 case RROTATE_EXPR:
3505 opname = "rotate"; break;
6d819282
MK
3506 default:
3507 opname = "unknown"; break;
b30f223b
RS
3508 }
3509 error ("invalid operands to binary %s", opname);
3510}
3511\f
3512/* Subroutine of build_binary_op, used for comparison operations.
3513 See if the operands have both been converted from subword integer types
3514 and, if so, perhaps change them both back to their original type.
94dccd9d
RS
3515 This function is also responsible for converting the two operands
3516 to the proper common type for comparison.
b30f223b
RS
3517
3518 The arguments of this function are all pointers to local variables
3519 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3520 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3521
3522 If this function returns nonzero, it means that the comparison has
3523 a constant value. What this function returns is an expression for
3524 that value. */
3525
3526tree
3527shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
3528 tree *op0_ptr, *op1_ptr;
3529 tree *restype_ptr;
3530 enum tree_code *rescode_ptr;
3531{
3532 register tree type;
3533 tree op0 = *op0_ptr;
3534 tree op1 = *op1_ptr;
3535 int unsignedp0, unsignedp1;
3536 int real1, real2;
3537 tree primop0, primop1;
3538 enum tree_code code = *rescode_ptr;
3539
3540 /* Throw away any conversions to wider types
3541 already present in the operands. */
3542
3543 primop0 = get_narrower (op0, &unsignedp0);
3544 primop1 = get_narrower (op1, &unsignedp1);
3545
3546 /* Handle the case that OP0 does not *contain* a conversion
3547 but it *requires* conversion to FINAL_TYPE. */
3548
3549 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3550 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
3551 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3552 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
3553
3554 /* If one of the operands must be floated, we cannot optimize. */
3555 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3556 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
3557
3558 /* If first arg is constant, swap the args (changing operation
5af6001b
RK
3559 so value is preserved), for canonicalization. Don't do this if
3560 the second arg is 0. */
b30f223b 3561
5af6001b
RK
3562 if (TREE_CONSTANT (primop0)
3563 && ! integer_zerop (primop1) && ! real_zerop (primop1))
b30f223b
RS
3564 {
3565 register tree tem = primop0;
3566 register int temi = unsignedp0;
3567 primop0 = primop1;
3568 primop1 = tem;
3569 tem = op0;
3570 op0 = op1;
3571 op1 = tem;
3572 *op0_ptr = op0;
3573 *op1_ptr = op1;
3574 unsignedp0 = unsignedp1;
3575 unsignedp1 = temi;
3576 temi = real1;
3577 real1 = real2;
3578 real2 = temi;
3579
3580 switch (code)
3581 {
3582 case LT_EXPR:
3583 code = GT_EXPR;
3584 break;
3585 case GT_EXPR:
3586 code = LT_EXPR;
3587 break;
3588 case LE_EXPR:
3589 code = GE_EXPR;
3590 break;
3591 case GE_EXPR:
3592 code = LE_EXPR;
3593 break;
6d819282
MK
3594 default:
3595 break;
b30f223b
RS
3596 }
3597 *rescode_ptr = code;
3598 }
3599
3600 /* If comparing an integer against a constant more bits wide,
3601 maybe we can deduce a value of 1 or 0 independent of the data.
3602 Or else truncate the constant now
3603 rather than extend the variable at run time.
3604
3605 This is only interesting if the constant is the wider arg.
3606 Also, it is not safe if the constant is unsigned and the
3607 variable arg is signed, since in this case the variable
3608 would be sign-extended and then regarded as unsigned.
3609 Our technique fails in this case because the lowest/highest
3610 possible unsigned results don't follow naturally from the
3611 lowest/highest possible values of the variable operand.
3612 For just EQ_EXPR and NE_EXPR there is another technique that
3613 could be used: see if the constant can be faithfully represented
3614 in the other operand's type, by truncating it and reextending it
3615 and see if that preserves the constant's value. */
3616
3617 if (!real1 && !real2
3618 && TREE_CODE (primop1) == INTEGER_CST
3619 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3620 {
3621 int min_gt, max_gt, min_lt, max_lt;
3622 tree maxval, minval;
3623 /* 1 if comparison is nominally unsigned. */
3624 int unsignedp = TREE_UNSIGNED (*restype_ptr);
3625 tree val;
3626
3627 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
8bbd5685
CW
3628
3629 /* If TYPE is an enumeration, then we need to get its min/max
3630 values from it's underlying integral type, not the enumerated
3631 type itself. */
3632 if (TREE_CODE (type) == ENUMERAL_TYPE)
3633 type = type_for_size (TYPE_PRECISION (type), unsignedp0);
b30f223b
RS
3634
3635 maxval = TYPE_MAX_VALUE (type);
3636 minval = TYPE_MIN_VALUE (type);
3637
3638 if (unsignedp && !unsignedp0)
3639 *restype_ptr = signed_type (*restype_ptr);
3640
3641 if (TREE_TYPE (primop1) != *restype_ptr)
3642 primop1 = convert (*restype_ptr, primop1);
3643 if (type != *restype_ptr)
3644 {
3645 minval = convert (*restype_ptr, minval);
3646 maxval = convert (*restype_ptr, maxval);
3647 }
3648
3649 if (unsignedp && unsignedp0)
3650 {
3651 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
3652 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
3653 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
3654 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
3655 }
3656 else
3657 {
3658 min_gt = INT_CST_LT (primop1, minval);
3659 max_gt = INT_CST_LT (primop1, maxval);
3660 min_lt = INT_CST_LT (minval, primop1);
3661 max_lt = INT_CST_LT (maxval, primop1);
3662 }
3663
3664 val = 0;
3665 /* This used to be a switch, but Genix compiler can't handle that. */
3666 if (code == NE_EXPR)
3667 {
3668 if (max_lt || min_gt)
a360da3a 3669 val = boolean_true_node;
b30f223b
RS
3670 }
3671 else if (code == EQ_EXPR)
3672 {
3673 if (max_lt || min_gt)
a360da3a 3674 val = boolean_false_node;
b30f223b
RS
3675 }
3676 else if (code == LT_EXPR)
3677 {
3678 if (max_lt)
a360da3a 3679 val = boolean_true_node;
b30f223b 3680 if (!min_lt)
a360da3a 3681 val = boolean_false_node;
b30f223b
RS
3682 }
3683 else if (code == GT_EXPR)
3684 {
3685 if (min_gt)
a360da3a 3686 val = boolean_true_node;
b30f223b 3687 if (!max_gt)
a360da3a 3688 val = boolean_false_node;
b30f223b
RS
3689 }
3690 else if (code == LE_EXPR)
3691 {
3692 if (!max_gt)
a360da3a 3693 val = boolean_true_node;
b30f223b 3694 if (min_gt)
a360da3a 3695 val = boolean_false_node;
b30f223b
RS
3696 }
3697 else if (code == GE_EXPR)
3698 {
3699 if (!min_lt)
a360da3a 3700 val = boolean_true_node;
b30f223b 3701 if (max_lt)
a360da3a 3702 val = boolean_false_node;
b30f223b
RS
3703 }
3704
3705 /* If primop0 was sign-extended and unsigned comparison specd,
3706 we did a signed comparison above using the signed type bounds.
3707 But the comparison we output must be unsigned.
3708
3709 Also, for inequalities, VAL is no good; but if the signed
3710 comparison had *any* fixed result, it follows that the
3711 unsigned comparison just tests the sign in reverse
3712 (positive values are LE, negative ones GE).
3713 So we can generate an unsigned comparison
3714 against an extreme value of the signed type. */
3715
3716 if (unsignedp && !unsignedp0)
3717 {
3718 if (val != 0)
3719 switch (code)
3720 {
3721 case LT_EXPR:
3722 case GE_EXPR:
3723 primop1 = TYPE_MIN_VALUE (type);
3724 val = 0;
3725 break;
3726
3727 case LE_EXPR:
3728 case GT_EXPR:
3729 primop1 = TYPE_MAX_VALUE (type);
3730 val = 0;
3731 break;
6d819282
MK
3732
3733 default:
3734 break;
b30f223b
RS
3735 }
3736 type = unsigned_type (type);
3737 }
3738
b7c9c707 3739 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
b30f223b
RS
3740 {
3741 /* This is the case of (char)x >?< 0x80, which people used to use
3742 expecting old C compilers to change the 0x80 into -0x80. */
a360da3a 3743 if (val == boolean_false_node)
07be2a23 3744 warning ("comparison is always false due to limited range of data type");
a360da3a 3745 if (val == boolean_true_node)
07be2a23 3746 warning ("comparison is always true due to limited range of data type");
b30f223b
RS
3747 }
3748
b7c9c707 3749 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
b30f223b 3750 {
1e276c4a 3751 /* This is the case of (unsigned char)x >?< -1 or < 0. */
a360da3a 3752 if (val == boolean_false_node)
07be2a23 3753 warning ("comparison is always false due to limited range of data type");
a360da3a 3754 if (val == boolean_true_node)
07be2a23 3755 warning ("comparison is always true due to limited range of data type");
b30f223b
RS
3756 }
3757
3758 if (val != 0)
3759 {
3760 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3761 if (TREE_SIDE_EFFECTS (primop0))
3762 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
3763 return val;
3764 }
3765
3766 /* Value is not predetermined, but do the comparison
3767 in the type of the operand that is not constant.
3768 TYPE is already properly set. */
3769 }
3770 else if (real1 && real2
766f6c30
RS
3771 && (TYPE_PRECISION (TREE_TYPE (primop0))
3772 == TYPE_PRECISION (TREE_TYPE (primop1))))
b30f223b
RS
3773 type = TREE_TYPE (primop0);
3774
3775 /* If args' natural types are both narrower than nominal type
3776 and both extend in the same manner, compare them
3777 in the type of the wider arg.
3778 Otherwise must actually extend both to the nominal
3779 common type lest different ways of extending
3780 alter the result.
3781 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
3782
3783 else if (unsignedp0 == unsignedp1 && real1 == real2
3784 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
3785 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
3786 {
3787 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3788 type = signed_or_unsigned_type (unsignedp0
3789 || TREE_UNSIGNED (*restype_ptr),
3790 type);
3791 /* Make sure shorter operand is extended the right way
3792 to match the longer operand. */
3793 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
3794 primop0);
3795 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
3796 primop1);
3797 }
3798 else
3799 {
3800 /* Here we must do the comparison on the nominal type
3801 using the args exactly as we received them. */
3802 type = *restype_ptr;
3803 primop0 = op0;
3804 primop1 = op1;
3805
3806 if (!real1 && !real2 && integer_zerop (primop1)
597681f6 3807 && TREE_UNSIGNED (*restype_ptr))
b30f223b
RS
3808 {
3809 tree value = 0;
3810 switch (code)
3811 {
3812 case GE_EXPR:
5af6001b
RK
3813 /* All unsigned values are >= 0, so we warn if extra warnings
3814 are requested. However, if OP0 is a constant that is
3815 >= 0, the signedness of the comparison isn't an issue,
3816 so suppress the warning. */
2c492eef 3817 if (extra_warnings && !in_system_header
5af6001b
RK
3818 && ! (TREE_CODE (primop0) == INTEGER_CST
3819 && ! TREE_OVERFLOW (convert (signed_type (type),
3820 primop0))))
07be2a23 3821 warning ("comparison of unsigned expression >= 0 is always true");
a360da3a 3822 value = boolean_true_node;
b30f223b
RS
3823 break;
3824
3825 case LT_EXPR:
2c492eef 3826 if (extra_warnings && !in_system_header
5af6001b
RK
3827 && ! (TREE_CODE (primop0) == INTEGER_CST
3828 && ! TREE_OVERFLOW (convert (signed_type (type),
3829 primop0))))
07be2a23 3830 warning ("comparison of unsigned expression < 0 is always false");
a360da3a 3831 value = boolean_false_node;
6d819282
MK
3832 break;
3833
3834 default:
3835 break;
b30f223b
RS
3836 }
3837
3838 if (value != 0)
3839 {
3840 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3841 if (TREE_SIDE_EFFECTS (primop0))
3842 return build (COMPOUND_EXPR, TREE_TYPE (value),
3843 primop0, value);
3844 return value;
3845 }
3846 }
3847 }
3848
3849 *op0_ptr = convert (type, primop0);
3850 *op1_ptr = convert (type, primop1);
3851
a360da3a 3852 *restype_ptr = boolean_type_node;
b30f223b
RS
3853
3854 return 0;
3855}
3856\f
3857/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3858 or validate its data type for an `if' or `while' statement or ?..: exp.
3859
3860 This preparation consists of taking the ordinary
3861 representation of an expression expr and producing a valid tree
3862 boolean expression describing whether expr is nonzero. We could
a360da3a 3863 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
b30f223b
RS
3864 but we optimize comparisons, &&, ||, and !.
3865
a360da3a 3866 The resulting type should always be `boolean_type_node'. */
b30f223b
RS
3867
3868tree
3869truthvalue_conversion (expr)
3870 tree expr;
3871{
257e61ed
RS
3872 if (TREE_CODE (expr) == ERROR_MARK)
3873 return expr;
3874
d7c83727 3875#if 0 /* This appears to be wrong for C++. */
257e61ed
RS
3876 /* These really should return error_mark_node after 2.4 is stable.
3877 But not all callers handle ERROR_MARK properly. */
3878 switch (TREE_CODE (TREE_TYPE (expr)))
3879 {
3880 case RECORD_TYPE:
3881 error ("struct type value used where scalar is required");
a360da3a 3882 return boolean_false_node;
257e61ed
RS
3883
3884 case UNION_TYPE:
3885 error ("union type value used where scalar is required");
a360da3a 3886 return boolean_false_node;
257e61ed
RS
3887
3888 case ARRAY_TYPE:
3889 error ("array type value used where scalar is required");
a360da3a 3890 return boolean_false_node;
257e61ed
RS
3891
3892 default:
3893 break;
3894 }
d7c83727 3895#endif /* 0 */
257e61ed 3896
b30f223b
RS
3897 switch (TREE_CODE (expr))
3898 {
b30f223b 3899 case EQ_EXPR:
b30f223b
RS
3900 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3901 case TRUTH_ANDIF_EXPR:
3902 case TRUTH_ORIF_EXPR:
3903 case TRUTH_AND_EXPR:
3904 case TRUTH_OR_EXPR:
9379fac9 3905 case TRUTH_XOR_EXPR:
1180eb10 3906 case TRUTH_NOT_EXPR:
a360da3a
JM
3907 TREE_TYPE (expr) = boolean_type_node;
3908 return expr;
18c0f675 3909
b30f223b
RS
3910 case ERROR_MARK:
3911 return expr;
3912
3913 case INTEGER_CST:
a360da3a 3914 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
b30f223b
RS
3915
3916 case REAL_CST:
a360da3a 3917 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
b30f223b
RS
3918
3919 case ADDR_EXPR:
fc0c675f
RK
3920 /* If we are taking the address of a external decl, it might be zero
3921 if it is weak, so we cannot optimize. */
2f939d94 3922 if (DECL_P (TREE_OPERAND (expr, 0))
fc0c675f
RK
3923 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
3924 break;
3925
b30f223b 3926 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
a360da3a
JM
3927 return build (COMPOUND_EXPR, boolean_type_node,
3928 TREE_OPERAND (expr, 0), boolean_true_node);
b30f223b 3929 else
a360da3a 3930 return boolean_true_node;
b30f223b 3931
766f6c30 3932 case COMPLEX_EXPR:
f0b996c5 3933 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
b839fb3f 3934 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
f0b996c5
RS
3935 truthvalue_conversion (TREE_OPERAND (expr, 0)),
3936 truthvalue_conversion (TREE_OPERAND (expr, 1)),
766f6c30
RS
3937 0);
3938
b30f223b
RS
3939 case NEGATE_EXPR:
3940 case ABS_EXPR:
3941 case FLOAT_EXPR:
3942 case FFS_EXPR:
3943 /* These don't change whether an object is non-zero or zero. */
3944 return truthvalue_conversion (TREE_OPERAND (expr, 0));
3945
3946 case LROTATE_EXPR:
3947 case RROTATE_EXPR:
3948 /* These don't change whether an object is zero or non-zero, but
3949 we can't ignore them if their second arg has side-effects. */
3950 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
a360da3a 3951 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
b30f223b
RS
3952 truthvalue_conversion (TREE_OPERAND (expr, 0)));
3953 else
3954 return truthvalue_conversion (TREE_OPERAND (expr, 0));
b57062ca 3955
b30f223b
RS
3956 case COND_EXPR:
3957 /* Distribute the conversion into the arms of a COND_EXPR. */
a360da3a 3958 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
b30f223b
RS
3959 truthvalue_conversion (TREE_OPERAND (expr, 1)),
3960 truthvalue_conversion (TREE_OPERAND (expr, 2))));
3961
3962 case CONVERT_EXPR:
3963 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3964 since that affects how `default_conversion' will behave. */
3965 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
3966 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
3967 break;
0f41302f 3968 /* fall through... */
b30f223b
RS
3969 case NOP_EXPR:
3970 /* If this is widening the argument, we can ignore it. */
3971 if (TYPE_PRECISION (TREE_TYPE (expr))
3972 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
3973 return truthvalue_conversion (TREE_OPERAND (expr, 0));
3974 break;
3975
b30f223b 3976 case MINUS_EXPR:
f87550e0
JW
3977 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
3978 this case. */
3979 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
3980 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
3981 break;
0f41302f 3982 /* fall through... */
f87550e0 3983 case BIT_XOR_EXPR:
d7c83727 3984 /* This and MINUS_EXPR can be changed into a comparison of the
f87550e0 3985 two objects. */
b30f223b
RS
3986 if (TREE_TYPE (TREE_OPERAND (expr, 0))
3987 == TREE_TYPE (TREE_OPERAND (expr, 1)))
3988 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
3989 TREE_OPERAND (expr, 1), 1);
3990 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
3991 fold (build1 (NOP_EXPR,
3992 TREE_TYPE (TREE_OPERAND (expr, 0)),
3993 TREE_OPERAND (expr, 1))), 1);
e2aab13d 3994
fb48b1f0 3995 case BIT_AND_EXPR:
58cee643
RK
3996 if (integer_onep (TREE_OPERAND (expr, 1))
3997 && TREE_TYPE (expr) != boolean_type_node)
3998 /* Using convert here would cause infinite recursion. */
3999 return build1 (NOP_EXPR, boolean_type_node, expr);
4000 break;
fb48b1f0 4001
e2aab13d
RS
4002 case MODIFY_EXPR:
4003 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
4004 warning ("suggest parentheses around assignment used as truth value");
4005 break;
b57062ca 4006
6d819282
MK
4007 default:
4008 break;
b30f223b
RS
4009 }
4010
f0b996c5 4011 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
f0b8d9aa
AS
4012 {
4013 tree tem = save_expr (expr);
4014 return (build_binary_op
4015 ((TREE_SIDE_EFFECTS (expr)
4016 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
4017 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
4018 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
4019 0));
4020 }
f0b996c5 4021
b30f223b
RS
4022 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
4023}
4024\f
0e5921e8 4025#if !USE_CPPLIB
b30f223b
RS
4026/* Read the rest of a #-directive from input stream FINPUT.
4027 In normal use, the directive name and the white space after it
4028 have already been read, so they won't be included in the result.
4029 We allow for the fact that the directive line may contain
4030 a newline embedded within a character or string literal which forms
4031 a part of the directive.
4032
4033 The value is a string in a reusable buffer. It remains valid
05a81fe5
DE
4034 only until the next time this function is called.
4035
4036 The terminating character ('\n' or EOF) is left in FINPUT for the
4037 caller to re-read. */
b30f223b
RS
4038
4039char *
4040get_directive_line (finput)
4041 register FILE *finput;
4042{
4043 static char *directive_buffer = NULL;
4044 static unsigned buffer_length = 0;
4045 register char *p;
4046 register char *buffer_limit;
4047 register int looking_for = 0;
4048 register int char_escaped = 0;
4049
4050 if (buffer_length == 0)
4051 {
4052 directive_buffer = (char *)xmalloc (128);
4053 buffer_length = 128;
4054 }
4055
4056 buffer_limit = &directive_buffer[buffer_length];
4057
4058 for (p = directive_buffer; ; )
4059 {
4060 int c;
4061
4062 /* Make buffer bigger if it is full. */
4063 if (p >= buffer_limit)
4064 {
4065 register unsigned bytes_used = (p - directive_buffer);
4066
4067 buffer_length *= 2;
4068 directive_buffer
4069 = (char *)xrealloc (directive_buffer, buffer_length);
4070 p = &directive_buffer[bytes_used];
4071 buffer_limit = &directive_buffer[buffer_length];
4072 }
4073
4074 c = getc (finput);
4075
4076 /* Discard initial whitespace. */
4077 if ((c == ' ' || c == '\t') && p == directive_buffer)
4078 continue;
4079
4080 /* Detect the end of the directive. */
05a81fe5
DE
4081 if (looking_for == 0
4082 && (c == '\n' || c == EOF))
b30f223b
RS
4083 {
4084 ungetc (c, finput);
4085 c = '\0';
4086 }
4087
4088 *p++ = c;
4089
4090 if (c == 0)
4091 return directive_buffer;
4092
4093 /* Handle string and character constant syntax. */
4094 if (looking_for)
4095 {
4096 if (looking_for == c && !char_escaped)
4097 looking_for = 0; /* Found terminator... stop looking. */
4098 }
4099 else
4100 if (c == '\'' || c == '"')
4101 looking_for = c; /* Don't stop buffering until we see another
38e01259 4102 one of these (or an EOF). */
b30f223b
RS
4103
4104 /* Handle backslash. */
4105 char_escaped = (c == '\\' && ! char_escaped);
4106 }
4107}
0e5921e8 4108#endif /* USE_CPPLIB */
0b73773c
NH
4109\f
4110/* Make a variant type in the proper way for C/C++, propagating qualifiers
4111 down to the element type of an array. */
4112
4113tree
3932261a 4114c_build_qualified_type (type, type_quals)
0b73773c 4115 tree type;
3932261a 4116 int type_quals;
0b73773c 4117{
3932261a
MM
4118 /* A restrict-qualified pointer type must be a pointer to object or
4119 incomplete type. Note that the use of POINTER_TYPE_P also allows
4120 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
4121 the C++ front-end also use POINTER_TYPE for pointer-to-member
4122 values, so even though it should be illegal to use `restrict'
4123 with such an entity we don't flag that here. Thus, special case
4124 code for that case is required in the C++ front-end. */
4125 if ((type_quals & TYPE_QUAL_RESTRICT)
4126 && (!POINTER_TYPE_P (type)
4127 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
4128 {
4129 error ("invalid use of `restrict'");
4130 type_quals &= ~TYPE_QUAL_RESTRICT;
4131 }
4132
0b73773c 4133 if (TREE_CODE (type) == ARRAY_TYPE)
3932261a
MM
4134 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
4135 type_quals),
3ab1999b 4136 TYPE_DOMAIN (type));
3932261a
MM
4137 return build_qualified_type (type, type_quals);
4138}
4139
4140/* Apply the TYPE_QUALS to the new DECL. */
4141
4142void
4143c_apply_type_quals_to_decl (type_quals, decl)
4144 int type_quals;
4145 tree decl;
4146{
a6496605
MM
4147 if ((type_quals & TYPE_QUAL_CONST)
4148 || (TREE_TYPE (decl)
4149 && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE))
3932261a
MM
4150 TREE_READONLY (decl) = 1;
4151 if (type_quals & TYPE_QUAL_VOLATILE)
4152 {
4153 TREE_SIDE_EFFECTS (decl) = 1;
4154 TREE_THIS_VOLATILE (decl) = 1;
4155 }
6946bc60 4156 if (type_quals & TYPE_QUAL_RESTRICT)
3932261a 4157 {
6946bc60
MM
4158 if (!TREE_TYPE (decl)
4159 || !POINTER_TYPE_P (TREE_TYPE (decl))
4160 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
4161 error ("invalid use of `restrict'");
4162 else if (flag_strict_aliasing)
3932261a 4163 {
6946bc60
MM
4164 /* No two restricted pointers can point at the same thing.
4165 However, a restricted pointer can point at the same thing
4166 as an unrestricted pointer, if that unrestricted pointer
4167 is based on the restricted pointer. So, we make the
4168 alias set for the restricted pointer a subset of the
4169 alias set for the type pointed to by the type of the
4170 decl. */
4171
3bdf5ad1 4172 HOST_WIDE_INT pointed_to_alias_set
6946bc60 4173 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
d125d268 4174
3bdf5ad1 4175 if (pointed_to_alias_set == 0)
6946bc60
MM
4176 /* It's not legal to make a subset of alias set zero. */
4177 ;
4178 else
4179 {
4180 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
4181 record_alias_subset (pointed_to_alias_set,
4182 DECL_POINTER_ALIAS_SET (decl));
4183 }
3932261a
MM
4184 }
4185 }
4186}
4187
41472af8
MM
4188
4189/* Return the typed-based alias set for T, which may be an expression
3bdf5ad1 4190 or a type. Return -1 if we don't do anything special. */
41472af8 4191
3bdf5ad1 4192HOST_WIDE_INT
2e761e49 4193lang_get_alias_set (t)
41472af8
MM
4194 tree t;
4195{
08bc2431 4196 tree u;
41472af8 4197
08bc2431
MM
4198 /* Permit type-punning when accessing a union, provided the access
4199 is directly through the union. For example, this code does not
4200 permit taking the address of a union member and then storing
4201 through it. Even the type-punning allowed here is a GCC
4202 extension, albeit a common and useful one; the C standard says
4203 that such accesses have implementation-defined behavior. */
4204 for (u = t;
4205 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4206 u = TREE_OPERAND (u, 0))
4207 if (TREE_CODE (u) == COMPONENT_REF
4208 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4209 return 0;
ece32014 4210
3bdf5ad1 4211 /* If this is a char *, the ANSI C standard says it can alias
f824e5c3
RK
4212 anything. Note that all references need do this. */
4213 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'r'
4214 && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
4215 && TYPE_PRECISION (TREE_TYPE (t)) == TYPE_PRECISION (char_type_node))
3bdf5ad1 4216 return 0;
3932261a 4217
3bdf5ad1
RK
4218 /* That's all the expressions we handle specially. */
4219 if (! TYPE_P (t))
4220 return -1;
41472af8 4221
f824e5c3
RK
4222 /* The C standard specifically allows aliasing between signed and
4223 unsigned variants of the same type. We treat the signed
4224 variant as canonical. */
4225 if (TREE_CODE (t) == INTEGER_TYPE && TREE_UNSIGNED (t))
8f215dce
JJ
4226 {
4227 tree t1 = signed_type (t);
f824e5c3 4228
8f215dce
JJ
4229 /* t1 == t can happen for boolean nodes which are always unsigned. */
4230 if (t1 != t)
4231 return get_alias_set (t1);
4232 }
3bdf5ad1 4233 else if (POINTER_TYPE_P (t))
02af3af6 4234 {
3bdf5ad1 4235 tree t1;
02af3af6
MS
4236
4237 /* Unfortunately, there is no canonical form of a pointer type.
4238 In particular, if we have `typedef int I', then `int *', and
4239 `I *' are different types. So, we have to pick a canonical
4240 representative. We do this below.
d125d268 4241
b61148dd
MM
4242 Technically, this approach is actually more conservative that
4243 it needs to be. In particular, `const int *' and `int *'
4244 chould be in different alias sets, according to the C and C++
4245 standard, since their types are not the same, and so,
4246 technically, an `int **' and `const int **' cannot point at
4247 the same thing.
4248
4249 But, the standard is wrong. In particular, this code is
4250 legal C++:
4251
4252 int *ip;
4253 int **ipp = &ip;
4254 const int* const* cipp = &ip;
4255
4256 And, it doesn't make sense for that to be legal unless you
4257 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
4258 the pointed-to types. This issue has been reported to the
4259 C++ committee. */
3bdf5ad1
RK
4260 t1 = TYPE_MAIN_VARIANT (TREE_TYPE (t));
4261 t1 = ((TREE_CODE (t) == POINTER_TYPE)
4262 ? build_pointer_type (t1) : build_reference_type (t1));
4263 if (t1 != t)
4264 return get_alias_set (t1);
02af3af6 4265 }
5b2abab8
MM
4266 /* It's not yet safe to use alias sets for classes in C++ because
4267 the TYPE_FIELDs list for a class doesn't mention base classes. */
f6f8291a 4268 else if (c_language == clk_cplusplus && AGGREGATE_TYPE_P (t))
5b2abab8 4269 return 0;
ece32014 4270
3bdf5ad1 4271 return -1;
41472af8 4272}
7f4edbcb
BS
4273
4274/* Build tree nodes and builtin functions common to both C and C++ language
d125d268 4275 frontends.
7f4edbcb 4276 CPLUS_MODE is nonzero if we are called from the C++ frontend, we generate
d125d268 4277 some stricter prototypes in that case.
7f4edbcb
BS
4278 NO_BUILTINS and NO_NONANSI_BUILTINS contain the respective values of
4279 the language frontend flags flag_no_builtin and
4280 flag_no_nonansi_builtin. */
3bdf5ad1 4281
7f4edbcb
BS
4282void
4283c_common_nodes_and_builtins (cplus_mode, no_builtins, no_nonansi_builtins)
4284 int cplus_mode, no_builtins, no_nonansi_builtins;
4285{
4286 tree temp;
4287 tree memcpy_ftype, memset_ftype, strlen_ftype;
ad3fd36f 4288 tree bzero_ftype, bcmp_ftype, puts_ftype, printf_ftype;
7f4edbcb
BS
4289 tree endlink, int_endlink, double_endlink, unsigned_endlink;
4290 tree sizetype_endlink;
4291 tree ptr_ftype, ptr_ftype_unsigned;
07328167 4292 tree void_ftype_any, void_ftype_int, int_ftype_any, sizet_ftype_any;
7f4edbcb
BS
4293 tree double_ftype_double, double_ftype_double_double;
4294 tree float_ftype_float, ldouble_ftype_ldouble;
4295 tree int_ftype_cptr_cptr_sizet;
4296 tree int_ftype_string_string, string_ftype_ptr_ptr;
4297 tree long_ftype_long;
1c44c45d 4298 tree longlong_ftype_longlong;
7f4edbcb
BS
4299 /* Either char* or void*. */
4300 tree traditional_ptr_type_node;
4b2a62db
KG
4301 /* Either const char* or const void*. */
4302 tree traditional_cptr_type_node;
4303 tree traditional_len_type_node;
4304 tree traditional_len_endlink;
9f720c3e 4305 tree va_list_ref_type_node;
daf68dd7 4306 tree va_list_arg_type_node;
d3707adb 4307
d3707adb
RH
4308 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
4309 va_list_type_node));
daf68dd7 4310
29ae8f10
GRK
4311 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"),
4312 ptrdiff_type_node));
4313
4314 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"),
4315 sizetype));
4316
daf68dd7 4317 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
9f720c3e
GK
4318 {
4319 va_list_arg_type_node = va_list_ref_type_node =
4320 build_pointer_type (TREE_TYPE (va_list_type_node));
4321 }
daf68dd7 4322 else
9f720c3e
GK
4323 {
4324 va_list_arg_type_node = va_list_type_node;
4325 va_list_ref_type_node = build_reference_type (va_list_type_node);
4326 }
4327
7f4edbcb
BS
4328 endlink = void_list_node;
4329 int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
4330 double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
4331 unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
4332
4333 ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
4334 ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
21318741 4335 sizetype_endlink = tree_cons (NULL_TREE, TYPE_DOMAIN (sizetype), endlink);
7f4edbcb
BS
4336 /* We realloc here because sizetype could be int or unsigned. S'ok. */
4337 ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
4338
07328167 4339 sizet_ftype_any = build_function_type (sizetype, NULL_TREE);
4b2a62db 4340 int_ftype_any = build_function_type (integer_type_node, NULL_TREE);
7f4edbcb
BS
4341 void_ftype_any = build_function_type (void_type_node, NULL_TREE);
4342 void_ftype = build_function_type (void_type_node, endlink);
4343 void_ftype_int = build_function_type (void_type_node, int_endlink);
4344 void_ftype_ptr
4345 = build_function_type (void_type_node,
4346 tree_cons (NULL_TREE, ptr_type_node, endlink));
4347
4348 float_ftype_float
4349 = build_function_type (float_type_node,
4350 tree_cons (NULL_TREE, float_type_node, endlink));
4351
4352 double_ftype_double
4353 = build_function_type (double_type_node, double_endlink);
4354
4355 ldouble_ftype_ldouble
4356 = build_function_type (long_double_type_node,
4357 tree_cons (NULL_TREE, long_double_type_node,
4358 endlink));
4359
4360 double_ftype_double_double
4361 = build_function_type (double_type_node,
4362 tree_cons (NULL_TREE, double_type_node,
4363 double_endlink));
4364
4365 int_ftype_int
4366 = build_function_type (integer_type_node, int_endlink);
4367
4368 long_ftype_long
4369 = build_function_type (long_integer_type_node,
4370 tree_cons (NULL_TREE, long_integer_type_node,
4371 endlink));
4372
1c44c45d
JM
4373 longlong_ftype_longlong
4374 = build_function_type (long_long_integer_type_node,
4375 tree_cons (NULL_TREE, long_long_integer_type_node,
4376 endlink));
4377
7f4edbcb
BS
4378 int_ftype_cptr_cptr_sizet
4379 = build_function_type (integer_type_node,
4380 tree_cons (NULL_TREE, const_ptr_type_node,
4381 tree_cons (NULL_TREE, const_ptr_type_node,
4382 tree_cons (NULL_TREE,
4383 sizetype,
4384 endlink))));
4385
ae499cce
MM
4386 void_zero_node = build_int_2 (0, 0);
4387 TREE_TYPE (void_zero_node) = void_type_node;
4388
7f4edbcb
BS
4389 /* Prototype for strcpy. */
4390 string_ftype_ptr_ptr
4391 = build_function_type (string_type_node,
4392 tree_cons (NULL_TREE, string_type_node,
4393 tree_cons (NULL_TREE,
4394 const_string_type_node,
4395 endlink)));
4396
4b2a62db
KG
4397 traditional_len_type_node = (flag_traditional && ! cplus_mode
4398 ? integer_type_node : sizetype);
4399 traditional_len_endlink = tree_cons (NULL_TREE, traditional_len_type_node,
4400 endlink);
4401
7f4edbcb
BS
4402 /* Prototype for strcmp. */
4403 int_ftype_string_string
4404 = build_function_type (integer_type_node,
4405 tree_cons (NULL_TREE, const_string_type_node,
4406 tree_cons (NULL_TREE,
4407 const_string_type_node,
4408 endlink)));
4409
4410 /* Prototype for strlen. */
4411 strlen_ftype
4b2a62db 4412 = build_function_type (traditional_len_type_node,
7f4edbcb
BS
4413 tree_cons (NULL_TREE, const_string_type_node,
4414 endlink));
4415
4416 traditional_ptr_type_node = (flag_traditional && ! cplus_mode
4417 ? string_type_node : ptr_type_node);
4b2a62db
KG
4418 traditional_cptr_type_node = (flag_traditional && ! cplus_mode
4419 ? const_string_type_node : const_ptr_type_node);
7f4edbcb
BS
4420
4421 /* Prototype for memcpy. */
4422 memcpy_ftype
4423 = build_function_type (traditional_ptr_type_node,
4424 tree_cons (NULL_TREE, ptr_type_node,
4425 tree_cons (NULL_TREE, const_ptr_type_node,
4426 sizetype_endlink)));
4427
4428 /* Prototype for memset. */
4429 memset_ftype
4430 = build_function_type (traditional_ptr_type_node,
4431 tree_cons (NULL_TREE, ptr_type_node,
4432 tree_cons (NULL_TREE, integer_type_node,
4433 tree_cons (NULL_TREE,
4434 sizetype,
4435 endlink))));
4436
e3a709be
KG
4437 /* Prototype for bzero. */
4438 bzero_ftype
4439 = build_function_type (void_type_node,
4440 tree_cons (NULL_TREE, traditional_ptr_type_node,
4441 traditional_len_endlink));
4442
4b2a62db
KG
4443 /* Prototype for bcmp. */
4444 bcmp_ftype
4445 = build_function_type (integer_type_node,
4446 tree_cons (NULL_TREE, traditional_cptr_type_node,
4447 tree_cons (NULL_TREE,
4448 traditional_cptr_type_node,
4449 traditional_len_endlink)));
4450
ad3fd36f
KG
4451 /* Prototype for puts. */
4452 puts_ftype
4453 = build_function_type (integer_type_node,
4454 tree_cons (NULL_TREE, const_string_type_node,
4455 endlink));
4456
4457 /* Prototype for printf. */
4458 printf_ftype
4459 = build_function_type (integer_type_node,
4460 tree_cons (NULL_TREE, const_string_type_node,
4461 NULL_TREE));
4462
7f4edbcb 4463 builtin_function ("__builtin_constant_p", default_function_type,
26db82d8 4464 BUILT_IN_CONSTANT_P, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
4465
4466 builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
26db82d8 4467 BUILT_IN_RETURN_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
4468
4469 builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
26db82d8 4470 BUILT_IN_FRAME_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
4471
4472 builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
26db82d8
BS
4473 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, "alloca");
4474 builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS,
4475 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
4476 /* Define alloca, ffs as builtins.
4477 Declare _exit just to mark it as volatile. */
4478 if (! no_builtins && ! no_nonansi_builtins)
4479 {
512b62fb 4480#ifndef SMALL_STACK
7f4edbcb 4481 temp = builtin_function ("alloca", ptr_ftype_sizetype,
26db82d8 4482 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
4483 /* Suppress error if redefined as a non-function. */
4484 DECL_BUILT_IN_NONANSI (temp) = 1;
512b62fb 4485#endif
26db82d8
BS
4486 temp = builtin_function ("ffs", int_ftype_int, BUILT_IN_FFS,
4487 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
4488 /* Suppress error if redefined as a non-function. */
4489 DECL_BUILT_IN_NONANSI (temp) = 1;
4490 temp = builtin_function ("_exit", void_ftype_int,
26db82d8 4491 0, NOT_BUILT_IN, NULL_PTR);
7f4edbcb
BS
4492 TREE_THIS_VOLATILE (temp) = 1;
4493 TREE_SIDE_EFFECTS (temp) = 1;
4494 /* Suppress error if redefined as a non-function. */
4495 DECL_BUILT_IN_NONANSI (temp) = 1;
4b2a62db 4496
dc1c86cb
KG
4497 /* The system prototypes for these functions have many
4498 variations, so don't specify parameters to avoid conflicts.
4499 The expand_* functions check the argument types anyway. */
4500 temp = builtin_function ("bzero", void_ftype_any,
e3a709be
KG
4501 BUILT_IN_BZERO, BUILT_IN_NORMAL, NULL_PTR);
4502 DECL_BUILT_IN_NONANSI (temp) = 1;
dc1c86cb 4503 temp = builtin_function ("bcmp", int_ftype_any,
4b2a62db
KG
4504 BUILT_IN_BCMP, BUILT_IN_NORMAL, NULL_PTR);
4505 DECL_BUILT_IN_NONANSI (temp) = 1;
7f4edbcb
BS
4506 }
4507
26db82d8
BS
4508 builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS,
4509 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4510 builtin_function ("__builtin_fabsf", float_ftype_float, BUILT_IN_FABS,
26db82d8 4511 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4512 builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS,
26db82d8 4513 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4514 builtin_function ("__builtin_fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
26db82d8 4515 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4516 builtin_function ("__builtin_labs", long_ftype_long, BUILT_IN_LABS,
26db82d8 4517 BUILT_IN_NORMAL, NULL_PTR);
1c44c45d
JM
4518 builtin_function ("__builtin_llabs", longlong_ftype_longlong, BUILT_IN_LLABS,
4519 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4520 builtin_function ("__builtin_saveregs", ptr_ftype, BUILT_IN_SAVEREGS,
26db82d8 4521 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4522 builtin_function ("__builtin_classify_type", default_function_type,
26db82d8 4523 BUILT_IN_CLASSIFY_TYPE, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4524 builtin_function ("__builtin_next_arg", ptr_ftype, BUILT_IN_NEXT_ARG,
26db82d8 4525 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4526 builtin_function ("__builtin_args_info", int_ftype_int, BUILT_IN_ARGS_INFO,
26db82d8 4527 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
4528 builtin_function ("__builtin_setjmp",
4529 build_function_type (integer_type_node,
4530 tree_cons (NULL_TREE, ptr_type_node,
4531 endlink)),
26db82d8 4532 BUILT_IN_SETJMP, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
4533 builtin_function ("__builtin_longjmp",
4534 build_function_type (void_type_node,
4535 tree_cons (NULL_TREE, ptr_type_node,
4536 tree_cons (NULL_TREE,
4537 integer_type_node,
4538 endlink))),
26db82d8
BS
4539 BUILT_IN_LONGJMP, BUILT_IN_NORMAL, NULL_PTR);
4540 builtin_function ("__builtin_trap", void_ftype, BUILT_IN_TRAP,
4541 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4542
1eb8759b
RH
4543 /* ISO C99 IEEE Unordered compares. */
4544 builtin_function ("__builtin_isgreater", default_function_type,
4545 BUILT_IN_ISGREATER, BUILT_IN_NORMAL, NULL_PTR);
4546 builtin_function ("__builtin_isgreaterequal", default_function_type,
4547 BUILT_IN_ISGREATEREQUAL, BUILT_IN_NORMAL, NULL_PTR);
4548 builtin_function ("__builtin_isless", default_function_type,
4549 BUILT_IN_ISLESS, BUILT_IN_NORMAL, NULL_PTR);
4550 builtin_function ("__builtin_islessequal", default_function_type,
4551 BUILT_IN_ISLESSEQUAL, BUILT_IN_NORMAL, NULL_PTR);
4552 builtin_function ("__builtin_islessgreater", default_function_type,
4553 BUILT_IN_ISLESSGREATER, BUILT_IN_NORMAL, NULL_PTR);
4554 builtin_function ("__builtin_isunordered", default_function_type,
4555 BUILT_IN_ISUNORDERED, BUILT_IN_NORMAL, NULL_PTR);
4556
7f4edbcb
BS
4557 /* Untyped call and return. */
4558 builtin_function ("__builtin_apply_args", ptr_ftype,
26db82d8 4559 BUILT_IN_APPLY_ARGS, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
4560
4561 temp = tree_cons (NULL_TREE,
4562 build_pointer_type (build_function_type (void_type_node,
4563 NULL_TREE)),
4564 tree_cons (NULL_TREE,
4565 ptr_type_node,
4566 tree_cons (NULL_TREE,
4567 sizetype,
4568 endlink)));
4569 builtin_function ("__builtin_apply",
4570 build_function_type (ptr_type_node, temp),
26db82d8 4571 BUILT_IN_APPLY, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4572 builtin_function ("__builtin_return", void_ftype_ptr,
26db82d8 4573 BUILT_IN_RETURN, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4574
d3707adb
RH
4575 /* Support for varargs.h and stdarg.h. */
4576 builtin_function ("__builtin_varargs_start",
4577 build_function_type (void_type_node,
4578 tree_cons (NULL_TREE,
9f720c3e 4579 va_list_ref_type_node,
d3707adb 4580 endlink)),
26db82d8 4581 BUILT_IN_VARARGS_START, BUILT_IN_NORMAL, NULL_PTR);
d3707adb
RH
4582
4583 builtin_function ("__builtin_stdarg_start",
4584 build_function_type (void_type_node,
4585 tree_cons (NULL_TREE,
9f720c3e 4586 va_list_ref_type_node,
d3707adb 4587 NULL_TREE)),
26db82d8 4588 BUILT_IN_STDARG_START, BUILT_IN_NORMAL, NULL_PTR);
d3707adb
RH
4589
4590 builtin_function ("__builtin_va_end",
4591 build_function_type (void_type_node,
4592 tree_cons (NULL_TREE,
9f720c3e 4593 va_list_ref_type_node,
d3707adb 4594 endlink)),
26db82d8 4595 BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL_PTR);
d3707adb
RH
4596
4597 builtin_function ("__builtin_va_copy",
4598 build_function_type (void_type_node,
4599 tree_cons (NULL_TREE,
9f720c3e 4600 va_list_ref_type_node,
d3707adb 4601 tree_cons (NULL_TREE,
daf68dd7 4602 va_list_arg_type_node,
d3707adb 4603 endlink))),
26db82d8 4604 BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL_PTR);
d3707adb 4605
994a57cd
RH
4606 /* ??? Ought to be `T __builtin_expect(T, T)' for any type T. */
4607 builtin_function ("__builtin_expect",
4608 build_function_type (long_integer_type_node,
4609 tree_cons (NULL_TREE,
4610 long_integer_type_node,
4611 tree_cons (NULL_TREE,
4612 long_integer_type_node,
4613 endlink))),
4614 BUILT_IN_EXPECT, BUILT_IN_NORMAL, NULL_PTR);
4615
7f4edbcb
BS
4616 /* Currently under experimentation. */
4617 builtin_function ("__builtin_memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
26db82d8 4618 BUILT_IN_NORMAL, "memcpy");
7f4edbcb 4619 builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
26db82d8 4620 BUILT_IN_MEMCMP, BUILT_IN_NORMAL, "memcmp");
e3a709be
KG
4621 builtin_function ("__builtin_memset", memset_ftype,
4622 BUILT_IN_MEMSET, BUILT_IN_NORMAL, "memset");
4623 builtin_function ("__builtin_bzero", bzero_ftype,
4624 BUILT_IN_BZERO, BUILT_IN_NORMAL, "bzero");
4b2a62db
KG
4625 builtin_function ("__builtin_bcmp", bcmp_ftype,
4626 BUILT_IN_BCMP, BUILT_IN_NORMAL, "bcmp");
7f4edbcb 4627 builtin_function ("__builtin_strcmp", int_ftype_string_string,
26db82d8 4628 BUILT_IN_STRCMP, BUILT_IN_NORMAL, "strcmp");
7f4edbcb 4629 builtin_function ("__builtin_strcpy", string_ftype_ptr_ptr,
26db82d8 4630 BUILT_IN_STRCPY, BUILT_IN_NORMAL, "strcpy");
7f4edbcb 4631 builtin_function ("__builtin_strlen", strlen_ftype,
26db82d8 4632 BUILT_IN_STRLEN, BUILT_IN_NORMAL, "strlen");
d125d268 4633 builtin_function ("__builtin_sqrtf", float_ftype_float,
26db82d8 4634 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtf");
7f4edbcb 4635 builtin_function ("__builtin_fsqrt", double_ftype_double,
26db82d8 4636 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrt");
7f4edbcb 4637 builtin_function ("__builtin_sqrtl", ldouble_ftype_ldouble,
26db82d8 4638 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtl");
d125d268 4639 builtin_function ("__builtin_sinf", float_ftype_float,
26db82d8 4640 BUILT_IN_SIN, BUILT_IN_NORMAL, "sinf");
d125d268 4641 builtin_function ("__builtin_sin", double_ftype_double,
26db82d8 4642 BUILT_IN_SIN, BUILT_IN_NORMAL, "sin");
d125d268 4643 builtin_function ("__builtin_sinl", ldouble_ftype_ldouble,
26db82d8 4644 BUILT_IN_SIN, BUILT_IN_NORMAL, "sinl");
d125d268 4645 builtin_function ("__builtin_cosf", float_ftype_float,
26db82d8 4646 BUILT_IN_COS, BUILT_IN_NORMAL, "cosf");
d125d268 4647 builtin_function ("__builtin_cos", double_ftype_double,
26db82d8 4648 BUILT_IN_COS, BUILT_IN_NORMAL, "cos");
d125d268 4649 builtin_function ("__builtin_cosl", ldouble_ftype_ldouble,
26db82d8 4650 BUILT_IN_COS, BUILT_IN_NORMAL, "cosl");
ad3fd36f
KG
4651 built_in_decls[BUILT_IN_PUTCHAR] =
4652 builtin_function ("__builtin_putchar", int_ftype_int,
4653 BUILT_IN_PUTCHAR, BUILT_IN_NORMAL, "putchar");
4654 built_in_decls[BUILT_IN_PUTS] =
4655 builtin_function ("__builtin_puts", puts_ftype,
4656 BUILT_IN_PUTS, BUILT_IN_NORMAL, "puts");
4657 builtin_function ("__builtin_printf", printf_ftype,
c70eaeaf 4658 BUILT_IN_PRINTF, BUILT_IN_FRONTEND, "printf");
3ff5f682
KG
4659 /* We declare these without argument so that the initial declaration
4660 for these identifiers is a builtin. That allows us to redeclare
4661 them later with argument without worrying about the explicit
4662 declarations in stdio.h being taken as the initial declaration.
4663 Also, save the _DECL for these so we can use them later. */
07328167
KG
4664 built_in_decls[BUILT_IN_FWRITE] =
4665 builtin_function ("__builtin_fwrite", sizet_ftype_any,
4666 BUILT_IN_FWRITE, BUILT_IN_NORMAL, "fwrite");
3ff5f682
KG
4667 built_in_decls[BUILT_IN_FPUTC] =
4668 builtin_function ("__builtin_fputc", int_ftype_any,
4669 BUILT_IN_FPUTC, BUILT_IN_NORMAL, "fputc");
4670 built_in_decls[BUILT_IN_FPUTS] =
4671 builtin_function ("__builtin_fputs", int_ftype_any,
4672 BUILT_IN_FPUTS, BUILT_IN_NORMAL, "fputs");
7f4edbcb
BS
4673
4674 if (! no_builtins)
4675 {
26db82d8
BS
4676 builtin_function ("abs", int_ftype_int, BUILT_IN_ABS,
4677 BUILT_IN_NORMAL, NULL_PTR);
4678 builtin_function ("fabsf", float_ftype_float, BUILT_IN_FABS,
4679 BUILT_IN_NORMAL, NULL_PTR);
4680 builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS,
4681 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4682 builtin_function ("fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
26db82d8
BS
4683 BUILT_IN_NORMAL, NULL_PTR);
4684 builtin_function ("labs", long_ftype_long, BUILT_IN_LABS,
4685 BUILT_IN_NORMAL, NULL_PTR);
1c44c45d
JM
4686 if (flag_isoc99 || ! no_nonansi_builtins)
4687 builtin_function ("llabs", longlong_ftype_longlong, BUILT_IN_LLABS,
4688 BUILT_IN_NORMAL, NULL_PTR);
26db82d8
BS
4689 builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
4690 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4691 builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP,
26db82d8
BS
4692 BUILT_IN_NORMAL, NULL_PTR);
4693 builtin_function ("memset", memset_ftype, BUILT_IN_MEMSET,
4694 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4695 builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP,
26db82d8 4696 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4697 builtin_function ("strcpy", string_ftype_ptr_ptr, BUILT_IN_STRCPY,
26db82d8
BS
4698 BUILT_IN_NORMAL, NULL_PTR);
4699 builtin_function ("strlen", strlen_ftype, BUILT_IN_STRLEN,
4700 BUILT_IN_NORMAL, NULL_PTR);
4701 builtin_function ("sqrtf", float_ftype_float, BUILT_IN_FSQRT,
4702 BUILT_IN_NORMAL, NULL_PTR);
4703 builtin_function ("sqrt", double_ftype_double, BUILT_IN_FSQRT,
4704 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4705 builtin_function ("sqrtl", ldouble_ftype_ldouble, BUILT_IN_FSQRT,
26db82d8
BS
4706 BUILT_IN_NORMAL, NULL_PTR);
4707 builtin_function ("sinf", float_ftype_float, BUILT_IN_SIN,
4708 BUILT_IN_NORMAL, NULL_PTR);
4709 builtin_function ("sin", double_ftype_double, BUILT_IN_SIN,
4710 BUILT_IN_NORMAL, NULL_PTR);
4711 builtin_function ("sinl", ldouble_ftype_ldouble, BUILT_IN_SIN,
4712 BUILT_IN_NORMAL, NULL_PTR);
4713 builtin_function ("cosf", float_ftype_float, BUILT_IN_COS,
4714 BUILT_IN_NORMAL, NULL_PTR);
4715 builtin_function ("cos", double_ftype_double, BUILT_IN_COS,
4716 BUILT_IN_NORMAL, NULL_PTR);
4717 builtin_function ("cosl", ldouble_ftype_ldouble, BUILT_IN_COS,
4718 BUILT_IN_NORMAL, NULL_PTR);
ad3fd36f 4719 builtin_function ("printf", printf_ftype, BUILT_IN_PRINTF,
c70eaeaf 4720 BUILT_IN_FRONTEND, NULL_PTR);
3ff5f682
KG
4721 /* We declare these without argument so that the initial
4722 declaration for these identifiers is a builtin. That allows
4723 us to redeclare them later with argument without worrying
4724 about the explicit declarations in stdio.h being taken as the
4725 initial declaration. */
4726 builtin_function ("fputc", int_ftype_any, BUILT_IN_FPUTC,
4727 BUILT_IN_NORMAL, NULL_PTR);
4728 builtin_function ("fputs", int_ftype_any, BUILT_IN_FPUTS,
4729 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
4730
4731 /* Declare these functions volatile
4732 to avoid spurious "control drops through" warnings. */
4733 temp = builtin_function ("abort", cplus_mode ? void_ftype : void_ftype_any,
26db82d8 4734 0, NOT_BUILT_IN, NULL_PTR);
7f4edbcb
BS
4735 TREE_THIS_VOLATILE (temp) = 1;
4736 TREE_SIDE_EFFECTS (temp) = 1;
4737
4738#if 0 /* ??? The C++ frontend used to do this. */
4739 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
4740 them... */
4741 DECL_BUILT_IN_NONANSI (temp) = 1;
4742#endif
4743 temp = builtin_function ("exit",
4744 cplus_mode ? void_ftype_int : void_ftype_any,
26db82d8 4745 0, NOT_BUILT_IN, NULL_PTR);
7f4edbcb
BS
4746 TREE_THIS_VOLATILE (temp) = 1;
4747 TREE_SIDE_EFFECTS (temp) = 1;
4748
4749#if 0 /* ??? The C++ frontend used to do this. */
4750 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
4751 them... */
4752 DECL_BUILT_IN_NONANSI (temp) = 1;
4753#endif
4754 }
4755
4756#if 0
4757 /* Support for these has not been written in either expand_builtin
4758 or build_function_call. */
26db82d8
BS
4759 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV,
4760 BUILT_IN_NORMAL, NULL_PTR);
4761 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV,
4762 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4763 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
26db82d8 4764 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4765 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
26db82d8 4766 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4767 builtin_function ("__builtin_fmod", double_ftype_double_double,
26db82d8 4768 BUILT_IN_FMOD, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4769 builtin_function ("__builtin_frem", double_ftype_double_double,
26db82d8 4770 BUILT_IN_FREM, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4771 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
26db82d8 4772 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4773 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
26db82d8 4774 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 4775#endif
c530479e 4776
5b47282c
GM
4777 main_identifier_node = get_identifier ("main");
4778
c530479e
RH
4779 /* ??? Perhaps there's a better place to do this. But it is related
4780 to __builtin_va_arg, so it isn't that off-the-wall. */
4781 lang_type_promotes_to = simple_type_promotes_to;
7f4edbcb 4782}
d3707adb
RH
4783
4784tree
4785build_va_arg (expr, type)
4786 tree expr, type;
4787{
4788 return build1 (VA_ARG_EXPR, type, expr);
4789}
c530479e
RH
4790\f
4791/* Given a type, apply default promotions wrt unnamed function arguments
4792 and return the new type. Return NULL_TREE if no change. */
d125d268 4793/* ??? There is a function of the same name in the C++ front end that
c530479e 4794 does something similar, but is more thorough and does not return NULL
d125d268 4795 if no change. We could perhaps share code, but it would make the
c530479e
RH
4796 self_promoting_type property harder to identify. */
4797
4798tree
4799simple_type_promotes_to (type)
4800 tree type;
4801{
4802 if (TYPE_MAIN_VARIANT (type) == float_type_node)
4803 return double_type_node;
4804
4805 if (C_PROMOTING_INTEGER_TYPE_P (type))
4806 {
4807 /* Traditionally, unsignedness is preserved in default promotions.
4808 Also preserve unsignedness if not really getting any wider. */
4809 if (TREE_UNSIGNED (type)
4810 && (flag_traditional
4811 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
4812 return unsigned_type_node;
4813 return integer_type_node;
4814 }
4815
4816 return NULL_TREE;
4817}
4818
4819/* Return 1 if PARMS specifies a fixed number of parameters
4820 and none of their types is affected by default promotions. */
4821
4822int
4823self_promoting_args_p (parms)
4824 tree parms;
4825{
4826 register tree t;
4827 for (t = parms; t; t = TREE_CHAIN (t))
4828 {
4829 register tree type = TREE_VALUE (t);
7e8176d7 4830
c530479e
RH
4831 if (TREE_CHAIN (t) == 0 && type != void_type_node)
4832 return 0;
4833
4834 if (type == 0)
4835 return 0;
4836
4837 if (TYPE_MAIN_VARIANT (type) == float_type_node)
4838 return 0;
4839
4840 if (C_PROMOTING_INTEGER_TYPE_P (type))
4841 return 0;
4842 }
4843 return 1;
4844}
5eda3d66
RH
4845
4846/* Recognize certain built-in functions so we can make tree-codes
4847 other than CALL_EXPR. We do this when it enables fold-const.c
4848 to do something useful. */
4849/* ??? By rights this should go in builtins.c, but only C and C++
4850 implement build_{binary,unary}_op. Not exactly sure what bits
4851 of functionality are actually needed from those functions, or
4852 where the similar functionality exists in the other front ends. */
4853
4854tree
4855expand_tree_builtin (function, params, coerced_params)
4856 tree function, params, coerced_params;
4857{
4858 enum tree_code code;
4859
4860 if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL)
4861 return NULL_TREE;
4862
4863 switch (DECL_FUNCTION_CODE (function))
4864 {
4865 case BUILT_IN_ABS:
4866 case BUILT_IN_LABS:
1c44c45d 4867 case BUILT_IN_LLABS:
5eda3d66
RH
4868 case BUILT_IN_FABS:
4869 if (coerced_params == 0)
4870 return integer_zero_node;
4871 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
4872
4873 case BUILT_IN_ISGREATER:
4874 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4875 code = UNLE_EXPR;
4876 else
4877 code = LE_EXPR;
4878 goto unordered_cmp;
4879
4880 case BUILT_IN_ISGREATEREQUAL:
4881 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4882 code = UNLT_EXPR;
4883 else
4884 code = LT_EXPR;
4885 goto unordered_cmp;
4886
4887 case BUILT_IN_ISLESS:
4888 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4889 code = UNGE_EXPR;
4890 else
4891 code = GE_EXPR;
4892 goto unordered_cmp;
4893
4894 case BUILT_IN_ISLESSEQUAL:
4895 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4896 code = UNGT_EXPR;
4897 else
4898 code = GT_EXPR;
4899 goto unordered_cmp;
4900
4901 case BUILT_IN_ISLESSGREATER:
4902 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4903 code = UNEQ_EXPR;
4904 else
4905 code = EQ_EXPR;
4906 goto unordered_cmp;
4907
4908 case BUILT_IN_ISUNORDERED:
4909 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
4910 return integer_zero_node;
4911 code = UNORDERED_EXPR;
4912 goto unordered_cmp;
4913
4914 unordered_cmp:
4915 {
4916 tree arg0, arg1;
4917
4918 if (params == 0
4919 || TREE_CHAIN (params) == 0)
4920 {
4921 error ("too few arguments to function `%s'",
4922 IDENTIFIER_POINTER (DECL_NAME (function)));
4923 return error_mark_node;
4924 }
4925 else if (TREE_CHAIN (TREE_CHAIN (params)) != 0)
4926 {
4927 error ("too many arguments to function `%s'",
4928 IDENTIFIER_POINTER (DECL_NAME (function)));
4929 return error_mark_node;
4930 }
4931
4932 arg0 = TREE_VALUE (params);
4933 arg1 = TREE_VALUE (TREE_CHAIN (params));
4934 arg0 = build_binary_op (code, arg0, arg1, 0);
4935 if (code != UNORDERED_EXPR)
4936 arg0 = build_unary_op (TRUTH_NOT_EXPR, arg0, 0);
4937 return arg0;
4938 }
4939 break;
4940
4941 default:
4942 break;
4943 }
4944
4945 return NULL_TREE;
4946}
c7d87c0a 4947
ae499cce
MM
4948/* Returns non-zero if CODE is the code for a statement. */
4949
4950int
4951statement_code_p (code)
4952 enum tree_code code;
4953{
4954 switch (code)
4955 {
4956 case EXPR_STMT:
4957 case COMPOUND_STMT:
4958 case DECL_STMT:
4959 case IF_STMT:
4960 case FOR_STMT:
4961 case WHILE_STMT:
4962 case DO_STMT:
4963 case RETURN_STMT:
4964 case BREAK_STMT:
4965 case CONTINUE_STMT:
8f17b5c5 4966 case SCOPE_STMT:
ae499cce
MM
4967 case SWITCH_STMT:
4968 case GOTO_STMT:
4969 case LABEL_STMT:
4970 case ASM_STMT:
4971 case CASE_LABEL:
4972 return 1;
4973
4974 default:
4975 if (lang_statement_code_p)
4976 return (*lang_statement_code_p) (code);
4977 return 0;
4978 }
4979}
4980
4981/* Walk the statemen tree, rooted at *tp. Apply FUNC to all the
4982 sub-trees of *TP in a pre-order traversal. FUNC is called with the
4983 DATA and the address of each sub-tree. If FUNC returns a non-NULL
4984 value, the traversal is aborted, and the value returned by FUNC is
4985 returned. If FUNC sets WALK_SUBTREES to zero, then the subtrees of
4986 the node being visited are not walked.
4987
4988 We don't need a without_duplicates variant of this one because the
4989 statement tree is a tree, not a graph. */
4990
4991tree
4992walk_stmt_tree (tp, func, data)
4993 tree *tp;
4994 walk_tree_fn func;
4995 void *data;
4996{
4997 enum tree_code code;
4998 int walk_subtrees;
4999 tree result;
5000 int i, len;
5001
5002#define WALK_SUBTREE(NODE) \
5003 do \
5004 { \
5005 result = walk_stmt_tree (&(NODE), func, data); \
5006 if (result) \
5007 return result; \
5008 } \
5009 while (0)
5010
5011 /* Skip empty subtrees. */
5012 if (!*tp)
5013 return NULL_TREE;
5014
5015 /* Skip subtrees below non-statement nodes. */
5016 if (!statement_code_p (TREE_CODE (*tp)))
5017 return NULL_TREE;
5018
5019 /* Call the function. */
5020 walk_subtrees = 1;
5021 result = (*func) (tp, &walk_subtrees, data);
5022
5023 /* If we found something, return it. */
5024 if (result)
5025 return result;
5026
5027 /* Even if we didn't, FUNC may have decided that there was nothing
5028 interesting below this point in the tree. */
5029 if (!walk_subtrees)
5030 return NULL_TREE;
5031
5032 /* FUNC may have modified the tree, recheck that we're looking at a
5033 statement node. */
5034 code = TREE_CODE (*tp);
5035 if (!statement_code_p (code))
5036 return NULL_TREE;
5037
5038 /* Walk over all the sub-trees of this operand. Statement nodes never
5039 contain RTL, and we needn't worry about TARGET_EXPRs. */
5040 len = TREE_CODE_LENGTH (code);
5041
5042 /* Go through the subtrees. We need to do this in forward order so
5043 that the scope of a FOR_EXPR is handled properly. */
5044 for (i = 0; i < len; ++i)
5045 WALK_SUBTREE (TREE_OPERAND (*tp, i));
5046
5047 /* Finally visit the chain. This can be tail-recursion optimized if
5048 we write it this way. */
5049 return walk_stmt_tree (&TREE_CHAIN (*tp), func, data);
5050
5051#undef WALK_SUBTREE
5052}
5053
8f17b5c5
MM
5054/* Used to compare case labels. K1 and K2 are actually tree nodes
5055 representing case labels, or NULL_TREE for a `default' label.
5056 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5057 K2, and 0 if K1 and K2 are equal. */
5058
5059int
5060case_compare (k1, k2)
5061 splay_tree_key k1;
5062 splay_tree_key k2;
5063{
5064 /* Consider a NULL key (such as arises with a `default' label) to be
5065 smaller than anything else. */
5066 if (!k1)
5067 return k2 ? -1 : 0;
5068 else if (!k2)
5069 return k1 ? 1 : 0;
5070
5071 return tree_int_cst_compare ((tree) k1, (tree) k2);
5072}
5073
5074/* Process a case label for the range LOW_VALUE ... HIGH_VALUE. If
5075 LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is
5076 actually a `default' label. If only HIGH_VALUE is NULL_TREE, then
5077 case label was declared using the usual C/C++ syntax, rather than
5078 the GNU case range extension. CASES is a tree containing all the
5079 case ranges processed so far; COND is the condition for the
5080 switch-statement itself. Returns the CASE_LABEL created, or
5081 ERROR_MARK_NODE if no CASE_LABEL is created. */
5082
5083tree
5084c_add_case_label (cases, cond, low_value, high_value)
5085 splay_tree cases;
5086 tree cond;
5087 tree low_value;
5088 tree high_value;
5089{
5090 tree type;
5091 tree label;
5092 tree case_label;
5093 splay_tree_node node;
5094
5095 /* Create the LABEL_DECL itself. */
5096 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5097 DECL_CONTEXT (label) = current_function_decl;
5098
5099 /* If there was an error processing the switch condition, bail now
5100 before we get more confused. */
5101 if (!cond || cond == error_mark_node)
5102 {
5103 /* Add a label anyhow so that the back-end doesn't think that
5104 the beginning of the switch is unreachable. */
5105 if (!cases->root)
5106 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
5107 return error_mark_node;
5108 }
5109
5110 if ((low_value && TREE_TYPE (low_value)
5111 && POINTER_TYPE_P (TREE_TYPE (low_value)))
5112 || (high_value && TREE_TYPE (high_value)
5113 && POINTER_TYPE_P (TREE_TYPE (high_value))))
5114 error ("pointers are not permitted as case values");
5115
5116 /* Case ranges are a GNU extension. */
5117 if (high_value && pedantic)
5118 {
5119 if (c_language == clk_cplusplus)
5120 pedwarn ("ISO C++ forbids range expressions in switch statements");
5121 else
5122 pedwarn ("ISO C forbids range expressions in switch statements");
5123 }
5124
5125 type = TREE_TYPE (cond);
5126 if (low_value)
5127 {
5128 low_value = check_case_value (low_value);
5129 low_value = convert_and_check (type, low_value);
5130 }
5131 if (high_value)
5132 {
5133 high_value = check_case_value (high_value);
5134 high_value = convert_and_check (type, high_value);
5135 }
5136
5137 /* If an error has occurred, bail out now. */
5138 if (low_value == error_mark_node || high_value == error_mark_node)
5139 {
5140 if (!cases->root)
5141 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
5142 return error_mark_node;
5143 }
5144
5145 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5146 really a case range, even though it was written that way. Remove
5147 the HIGH_VALUE to simplify later processing. */
5148 if (tree_int_cst_equal (low_value, high_value))
5149 high_value = NULL_TREE;
5150 if (low_value && high_value
5151 && !tree_int_cst_lt (low_value, high_value))
5152 warning ("empty range specified");
5153
5154 /* Look up the LOW_VALUE in the table of case labels we already
5155 have. */
5156 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5157 /* If there was not an exact match, check for overlapping ranges.
5158 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5159 that's a `default' label and the only overlap is an exact match. */
5160 if (!node && (low_value || high_value))
5161 {
5162 splay_tree_node low_bound;
5163 splay_tree_node high_bound;
5164
5165 /* Even though there wasn't an exact match, there might be an
5166 overlap between this case range and another case range.
5167 Since we've (inductively) not allowed any overlapping case
5168 ranges, we simply need to find the greatest low case label
5169 that is smaller that LOW_VALUE, and the smallest low case
5170 label that is greater than LOW_VALUE. If there is an overlap
5171 it will occur in one of these two ranges. */
5172 low_bound = splay_tree_predecessor (cases,
5173 (splay_tree_key) low_value);
5174 high_bound = splay_tree_successor (cases,
5175 (splay_tree_key) low_value);
5176
5177 /* Check to see if the LOW_BOUND overlaps. It is smaller than
5178 the LOW_VALUE, so there is no need to check unless the
5179 LOW_BOUND is in fact itself a case range. */
5180 if (low_bound
5181 && CASE_HIGH ((tree) low_bound->value)
5182 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5183 low_value) >= 0)
5184 node = low_bound;
5185 /* Check to see if the HIGH_BOUND overlaps. The low end of that
5186 range is bigger than the low end of the current range, so we
5187 are only interested if the current range is a real range, and
5188 not an ordinary case label. */
5189 else if (high_bound
5190 && high_value
5191 && (tree_int_cst_compare ((tree) high_bound->key,
5192 high_value)
5193 <= 0))
5194 node = high_bound;
5195 }
5196 /* If there was an overlap, issue an error. */
5197 if (node)
5198 {
5199 tree duplicate = CASE_LABEL_DECL ((tree) node->value);
5200
5201 if (high_value)
5202 {
5203 error ("duplicate (or overlapping) case value");
5204 error_with_decl (duplicate,
5205 "this is the first entry overlapping that value");
5206 }
5207 else if (low_value)
5208 {
5209 error ("duplicate case value") ;
5210 error_with_decl (duplicate, "previously used here");
5211 }
5212 else
5213 {
5214 error ("multiple default labels in one switch");
5215 error_with_decl (duplicate, "this is the first default label");
5216 }
5217 if (!cases->root)
5218 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
5219 }
5220
5221 /* Add a CASE_LABEL to the statement-tree. */
5222 case_label = add_stmt (build_case_label (low_value, high_value, label));
5223 /* Register this case label in the splay tree. */
5224 splay_tree_insert (cases,
5225 (splay_tree_key) low_value,
5226 (splay_tree_value) case_label);
5227
5228 return case_label;
5229}
5230
5231/* Mark P (a stmt_tree) for GC. The use of a `void *' for the
5232 parameter allows this function to be used as a GC-marking
5233 function. */
5234
5235void
5236mark_stmt_tree (p)
5237 void *p;
5238{
5239 stmt_tree st = (stmt_tree) p;
5240
5241 ggc_mark_tree (st->x_last_stmt);
5242 ggc_mark_tree (st->x_last_expr_type);
5243}
5244
5245/* Mark LD for GC. */
5246
5247void
5248c_mark_lang_decl (c)
5249 struct c_lang_decl *c;
5250{
5251 ggc_mark_tree (c->saved_tree);
5252}
5253
5254/* Mark F for GC. */
5255
5256void
5257mark_c_language_function (f)
5258 struct language_function *f;
5259{
5260 if (!f)
5261 return;
5262
5263 mark_stmt_tree (&f->x_stmt_tree);
5264 ggc_mark_tree (f->x_scope_stmt_stack);
5265}
5266
5267/* Hook used by expand_expr to expand language-specific tree codes. */
5268
5269rtx
5270c_expand_expr (exp, target, tmode, modifier)
5271 tree exp;
5272 rtx target;
5273 enum machine_mode tmode;
5274 enum expand_modifier modifier;
5275{
5276 switch (TREE_CODE (exp))
5277 {
5278 case STMT_EXPR:
5279 {
5280 tree rtl_expr;
5281 rtx result;
5282
5283 /* Since expand_expr_stmt calls free_temp_slots after every
5284 expression statement, we must call push_temp_slots here.
5285 Otherwise, any temporaries in use now would be considered
5286 out-of-scope after the first EXPR_STMT from within the
5287 STMT_EXPR. */
5288 push_temp_slots ();
5289 rtl_expr = expand_start_stmt_expr ();
5290 expand_stmt (STMT_EXPR_STMT (exp));
5291 expand_end_stmt_expr (rtl_expr);
5292 result = expand_expr (rtl_expr, target, tmode, modifier);
5293 pop_temp_slots ();
5294 return result;
5295 }
5296 break;
5297
c70eaeaf
KG
5298 case CALL_EXPR:
5299 {
5300 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5301 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5302 == FUNCTION_DECL)
5303 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5304 && (DECL_BUILT_IN_CLASS (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5305 == BUILT_IN_FRONTEND))
5306 return c_expand_builtin (exp, target, tmode, modifier);
5307 else
5308 abort();
5309 }
5310 break;
5311
8f17b5c5
MM
5312 default:
5313 abort ();
5314 }
5315
5316 abort ();
5317 return NULL;
5318}
5319
5320/* Hook used by safe_from_p to handle language-specific tree codes. */
5321
5322int
5323c_safe_from_p (target, exp)
5324 rtx target;
5325 tree exp;
5326{
5327 /* We can see statements here when processing the body of a
5328 statement-expression. For a declaration statement declaring a
5329 variable, look at the variable's initializer. */
5330 if (TREE_CODE (exp) == DECL_STMT)
5331 {
5332 tree decl = DECL_STMT_DECL (exp);
5333
5334 if (TREE_CODE (decl) == VAR_DECL
5335 && DECL_INITIAL (decl)
5336 && !safe_from_p (target, DECL_INITIAL (decl), /*top_p=*/0))
5337 return 0;
5338 }
5339
5340 /* For any statement, we must follow the statement-chain. */
5341 if (statement_code_p (TREE_CODE (exp)) && TREE_CHAIN (exp))
5342 return safe_from_p (target, TREE_CHAIN (exp), /*top_p=*/0);
5343
5344 /* Assume everything else is safe. */
5345 return 1;
5346}
5347
c7d87c0a
BC
5348/* Tree code classes. */
5349
5350#define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
5351
5352static char c_tree_code_type[] = {
5353 'x',
5354#include "c-common.def"
5355};
5356#undef DEFTREECODE
5357
5358/* Table indexed by tree code giving number of expression
5359 operands beyond the fixed part of the node structure.
5360 Not used for types or decls. */
5361
5362#define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
5363
5364static int c_tree_code_length[] = {
5365 0,
5366#include "c-common.def"
5367};
5368#undef DEFTREECODE
5369
5370/* Names of tree components.
5371 Used for printing out the tree and error messages. */
5372#define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
5373
5374static const char *c_tree_code_name[] = {
5375 "@@dummy",
5376#include "c-common.def"
5377};
5378#undef DEFTREECODE
5379
5380/* Adds the tree codes specific to the C front end to the list of all
5381 tree codes. */
5382
5383void
5538e30f 5384add_c_tree_codes ()
c7d87c0a
BC
5385{
5386 memcpy (tree_code_type + (int) LAST_AND_UNUSED_TREE_CODE,
5387 c_tree_code_type,
5388 (int)LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE);
5389 memcpy (tree_code_length + (int) LAST_AND_UNUSED_TREE_CODE,
5390 c_tree_code_length,
5391 (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (int));
5392 memcpy (tree_code_name + (int) LAST_AND_UNUSED_TREE_CODE,
5393 c_tree_code_name,
5394 (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (char *));
5395}
c70eaeaf
KG
5396
5397#define CALLED_AS_BUILT_IN(NODE) \
5398 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
5399
5400static rtx
5401c_expand_builtin (exp, target, tmode, modifier)
5402 tree exp;
5403 rtx target;
5404 enum machine_mode tmode;
5405 enum expand_modifier modifier;
5406{
5407 tree type = TREE_TYPE (exp);
5408 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5409 tree arglist = TREE_OPERAND (exp, 1);
5410 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
5411 enum tree_code code = TREE_CODE (exp);
5412 const int ignore = (target == const0_rtx
5413 || ((code == NON_LVALUE_EXPR || code == NOP_EXPR
5414 || code == CONVERT_EXPR || code == REFERENCE_EXPR
5415 || code == COND_EXPR)
5416 && TREE_CODE (type) == VOID_TYPE));
5417
5418 if (! optimize && ! CALLED_AS_BUILT_IN (fndecl))
5419 return expand_call (exp, target, ignore);
5420
5421 switch (fcode)
5422 {
5423 case BUILT_IN_PRINTF:
5424 target = c_expand_builtin_printf (arglist, target, tmode,
5425 modifier, ignore);
5426 if (target)
5427 return target;
5428 break;
5429
5430 default: /* just do library call, if unknown builtin */
5431 error ("built-in function `%s' not currently supported",
5432 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
5433 }
5434
5435 /* The switch statement above can drop through to cause the function
5436 to be called normally. */
5437 return expand_call (exp, target, ignore);
5438}
5439
5440/* Check an arglist to *printf for problems. The arglist should start
5441 at the format specifier, with the remaining arguments immediately
5442 following it. */
5443static int
5444is_valid_printf_arglist (arglist)
5445 tree arglist;
5446{
5447 /* Save this value so we can restore it later. */
5448 const int SAVE_pedantic = pedantic;
5449 int diagnostic_occurred = 0;
5450
5451 /* Set this to a known value so the user setting won't affect code
5452 generation. */
5453 pedantic = 1;
5454 /* Check to make sure there are no format specifier errors. */
5455 check_function_format (&diagnostic_occurred,
5456 maybe_get_identifier("printf"),
5457 NULL_TREE, arglist);
5458
5459 /* Restore the value of `pedantic'. */
5460 pedantic = SAVE_pedantic;
5461
5462 /* If calling `check_function_format_ptr' produces a warning, we
5463 return false, otherwise we return true. */
5464 return ! diagnostic_occurred;
5465}
5466
5467/* If the arguments passed to printf are suitable for optimizations,
5468 we attempt to transform the call. */
5469static rtx
5470c_expand_builtin_printf (arglist, target, tmode, modifier, ignore)
5471 tree arglist;
5472 rtx target;
5473 enum machine_mode tmode;
5474 enum expand_modifier modifier;
5475 int ignore;
5476{
5477 tree fn_putchar = built_in_decls[BUILT_IN_PUTCHAR],
5478 fn_puts = built_in_decls[BUILT_IN_PUTS];
5479 tree fn, format_arg, stripped_string;
5480
5481 /* If the return value is used, or the replacement _DECL isn't
5482 initialized, don't do the transformation. */
5483 if (!ignore || !fn_putchar || !fn_puts)
5484 return 0;
5485
5486 /* Verify the required arguments in the original call. */
5487 if (arglist == 0
5488 || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE))
5489 return 0;
5490
5491 /* Check the specifier vs. the parameters. */
5492 if (!is_valid_printf_arglist (arglist))
5493 return 0;
5494
5495 format_arg = TREE_VALUE (arglist);
5496 stripped_string = format_arg;
5497 STRIP_NOPS (stripped_string);
5498 if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
5499 stripped_string = TREE_OPERAND (stripped_string, 0);
5500
5501 /* If the format specifier isn't a STRING_CST, punt. */
5502 if (TREE_CODE (stripped_string) != STRING_CST)
5503 return 0;
5504
5505 /* OK! We can attempt optimization. */
5506
5507 /* If the format specifier was "%s\n", call __builtin_puts(arg2). */
5508 if (strcmp (TREE_STRING_POINTER (stripped_string), "%s\n") == 0)
5509 {
5510 arglist = TREE_CHAIN (arglist);
5511 fn = fn_puts;
5512 }
5513 /* If the format specifier was "%c", call __builtin_putchar (arg2). */
5514 else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0)
5515 {
5516 arglist = TREE_CHAIN (arglist);
5517 fn = fn_putchar;
5518 }
5519 else
5520 {
5521 /* We can't handle anything else with % args or %% ... yet. */
5522 if (strchr (TREE_STRING_POINTER (stripped_string), '%'))
5523 return 0;
5524
5525 /* If the resulting constant string has a length of 1, call
5526 putchar. Note, TREE_STRING_LENGTH includes the terminating
5527 NULL in its count. */
5528 if (TREE_STRING_LENGTH (stripped_string) == 2)
5529 {
5530 /* Given printf("c"), (where c is any one character,)
5531 convert "c"[0] to an int and pass that to the replacement
5532 function. */
5533 arglist = build_int_2 (TREE_STRING_POINTER (stripped_string)[0], 0);
5534 arglist = build_tree_list (NULL_TREE, arglist);
5535
5536 fn = fn_putchar;
5537 }
5538 /* If the resulting constant was "string\n", call
5539 __builtin_puts("string"). Ensure "string" has at least one
5540 character besides the trailing \n. Note, TREE_STRING_LENGTH
5541 includes the terminating NULL in its count. */
5542 else if (TREE_STRING_LENGTH (stripped_string) > 2
5543 && TREE_STRING_POINTER (stripped_string)
5544 [TREE_STRING_LENGTH (stripped_string) - 2] == '\n')
5545 {
5546 /* Create a NULL-terminated string that's one char shorter
5547 than the original, stripping off the trailing '\n'. */
5548 const int newlen = TREE_STRING_LENGTH (stripped_string) - 1;
5549 char *newstr = (char *) alloca (newlen);
5550 memcpy (newstr, TREE_STRING_POINTER (stripped_string), newlen - 1);
5551 newstr[newlen - 1] = 0;
5552
5553 arglist = build_string (newlen, newstr);
5554 TREE_TYPE (arglist) =
5555 build_type_variant (char_array_type_node, 1, 0);
5556 arglist = build_tree_list (NULL_TREE, arglist);
5557 fn = fn_puts;
5558 }
5559 else
5560 /* We'd like to arrange to call fputs(string) here, but we
5561 need stdout and don't have a way to get it ... yet. */
5562 return 0;
5563 }
5564
5565 return expand_expr (build_function_call (fn, arglist),
5566 (ignore ? const0_rtx : target),
5567 tmode, modifier);
5568}