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