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