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