]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-common.c
* calls.c (expand_call) Do not attempt to combine stack adjustments
[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"
25#include "c-lex.h"
26#include "c-tree.h"
27#include "flags.h"
9cdfa0b0 28#include "toplev.h"
cd03a192 29#include "output.h"
a3fa7feb 30#include "c-pragma.h"
a5b1863e 31#include "rtl.h"
dc12af01 32#include "ggc.h"
74647769 33#include "expr.h"
d8c9779c 34#include "tm_p.h"
3ef9782d 35
a654e028 36#if USE_CPPLIB
37#include "cpplib.h"
38cpp_reader parse_in;
39cpp_options parse_options;
4070d785 40enum cpp_token cpp_token;
a654e028 41#endif
42
be37a9cb 43#undef WCHAR_TYPE_SIZE
44#define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node)
c83bcd28 45
72040e7e 46/* The following symbols are subsumed in the c_global_trees array, and
44e9fa65 47 listed here individually for documentation purposes.
72040e7e 48
49 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
50
51 tree short_integer_type_node;
52 tree long_integer_type_node;
53 tree long_long_integer_type_node;
54
55 tree short_unsigned_type_node;
56 tree long_unsigned_type_node;
57 tree long_long_unsigned_type_node;
58
59 tree boolean_type_node;
60 tree boolean_false_node;
61 tree boolean_true_node;
62
63 tree ptrdiff_type_node;
64
65 tree unsigned_char_type_node;
66 tree signed_char_type_node;
67 tree wchar_type_node;
68 tree signed_wchar_type_node;
69 tree unsigned_wchar_type_node;
70
71 tree float_type_node;
72 tree double_type_node;
73 tree long_double_type_node;
74
75 tree complex_integer_type_node;
76 tree complex_float_type_node;
77 tree complex_double_type_node;
78 tree complex_long_double_type_node;
79
80 tree intQI_type_node;
81 tree intHI_type_node;
82 tree intSI_type_node;
83 tree intDI_type_node;
84 tree intTI_type_node;
85
86 tree unsigned_intQI_type_node;
87 tree unsigned_intHI_type_node;
88 tree unsigned_intSI_type_node;
89 tree unsigned_intDI_type_node;
90 tree unsigned_intTI_type_node;
91
92 tree widest_integer_literal_type_node;
93 tree widest_unsigned_literal_type_node;
94
95 Nodes for types `void *' and `const void *'.
96
97 tree ptr_type_node, const_ptr_type_node;
98
99 Nodes for types `char *' and `const char *'.
100
101 tree string_type_node, const_string_type_node;
102
103 Type `char[SOMENUMBER]'.
104 Used when an array of char is needed and the size is irrelevant.
105
106 tree char_array_type_node;
107
108 Type `int[SOMENUMBER]' or something like it.
109 Used when an array of int needed and the size is irrelevant.
110
111 tree int_array_type_node;
112
113 Type `wchar_t[SOMENUMBER]' or something like it.
114 Used when a wide string literal is created.
115
116 tree wchar_array_type_node;
117
118 Type `int ()' -- used for implicit declaration of functions.
119
120 tree default_function_type;
121
122 Function types `int (int)', etc.
123
124 tree int_ftype_int;
125 tree void_ftype;
126 tree void_ftype_ptr;
127 tree int_ftype_int;
128 tree ptr_ftype_sizetype;
129
130 A VOID_TYPE node, packaged in a TREE_LIST.
131
132 tree void_list_node;
133
134*/
135
136tree c_global_trees[CTI_MAX];
ceee5ef4 137
e78703c1 138/* Nonzero means the expression being parsed will never be evaluated.
139 This is a count, since unevaluated expressions can nest. */
140int skip_evaluation;
141
9493f142 142enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
efea460c 143 A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
1f57d1e5 144 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
8f8ac140 145 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS, A_MALLOC,
146 A_NO_LIMIT_STACK};
550e135c 147
d1f11193 148enum format_type { printf_format_type, scanf_format_type,
149 strftime_format_type };
150
6a9a24a5 151static void declare_hidden_char_array PARAMS ((const char *, const char *));
152static void add_attribute PARAMS ((enum attrs, const char *,
153 int, int, int));
154static void init_attributes PARAMS ((void));
155static void record_function_format PARAMS ((tree, tree, enum format_type,
156 int, int));
157static void record_international_format PARAMS ((tree, tree, int));
158static tree c_find_base_decl PARAMS ((tree));
159static int default_valid_lang_attribute PARAMS ((tree, tree, tree, tree));
be43ff5a 160
9fc84d48 161/* Keep a stack of if statements. We record the number of compound
162 statements seen up to the if keyword, as well as the line number
163 and file of the if. If a potentially ambiguous else is seen, that
164 fact is recorded; the warning is issued when we can be sure that
165 the enclosing if statement does not have an else branch. */
166typedef struct
167{
168 int compstmt_count;
169 int line;
3eee82c5 170 const char *file;
9fc84d48 171 int needs_warning;
172} if_elt;
6a9a24a5 173static void tfaff PARAMS ((void));
9fc84d48 174
175static if_elt *if_stack;
31f820d2 176
177/* Amount of space in the if statement stack. */
178static int if_stack_space = 0;
179
180/* Stack pointer. */
181static int if_stack_pointer = 0;
182
9fc84d48 183/* Generate RTL for the start of an if-then, and record the start of it
184 for ambiguous else detection. */
185
31f820d2 186void
187c_expand_start_cond (cond, exitflag, compstmt_count)
188 tree cond;
189 int exitflag;
190 int compstmt_count;
191{
192 /* Make sure there is enough space on the stack. */
193 if (if_stack_space == 0)
194 {
195 if_stack_space = 10;
9fc84d48 196 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
31f820d2 197 }
198 else if (if_stack_space == if_stack_pointer)
199 {
200 if_stack_space += 10;
9fc84d48 201 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
31f820d2 202 }
9fc84d48 203
31f820d2 204 /* Record this if statement. */
9fc84d48 205 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
206 if_stack[if_stack_pointer].file = input_filename;
207 if_stack[if_stack_pointer].line = lineno;
208 if_stack[if_stack_pointer].needs_warning = 0;
209 if_stack_pointer++;
31f820d2 210
211 expand_start_cond (cond, exitflag);
212}
213
9fc84d48 214/* Generate RTL for the end of an if-then. Optionally warn if a nested
215 if statement had an ambiguous else clause. */
216
31f820d2 217void
218c_expand_end_cond ()
219{
220 if_stack_pointer--;
9fc84d48 221 if (if_stack[if_stack_pointer].needs_warning)
222 warning_with_file_and_line (if_stack[if_stack_pointer].file,
223 if_stack[if_stack_pointer].line,
224 "suggest explicit braces to avoid ambiguous `else'");
31f820d2 225 expand_end_cond ();
226}
227
9fc84d48 228/* Generate RTL between the then-clause and the else-clause
229 of an if-then-else. */
230
31f820d2 231void
232c_expand_start_else ()
233{
9fc84d48 234 /* An ambiguous else warning must be generated for the enclosing if
235 statement, unless we see an else branch for that one, too. */
31f820d2 236 if (warn_parentheses
237 && if_stack_pointer > 1
9fc84d48 238 && (if_stack[if_stack_pointer - 1].compstmt_count
239 == if_stack[if_stack_pointer - 2].compstmt_count))
240 if_stack[if_stack_pointer - 2].needs_warning = 1;
241
242 /* Even if a nested if statement had an else branch, it can't be
243 ambiguous if this one also has an else. So don't warn in that
244 case. Also don't warn for any if statements nested in this else. */
245 if_stack[if_stack_pointer - 1].needs_warning = 0;
246 if_stack[if_stack_pointer - 1].compstmt_count--;
31f820d2 247
248 expand_start_else ();
249}
250
75bd5ada 251/* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__. */
f4e3c278 252
253void
254declare_function_name ()
255{
3eee82c5 256 const char *name, *printable_name;
f4e3c278 257
258 if (current_function_decl == NULL)
259 {
260 name = "";
261 printable_name = "top level";
262 }
263 else
264 {
3f3680c7 265 /* Allow functions to be nameless (such as artificial ones). */
266 if (DECL_NAME (current_function_decl))
267 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
268 else
269 name = "";
59561872 270 printable_name = (*decl_printable_name) (current_function_decl, 2);
f4e3c278 271 }
272
be43ff5a 273 declare_hidden_char_array ("__FUNCTION__", name);
274 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
75bd5ada 275 /* The ISO C people "of course" couldn't use __FUNCTION__ in the
85fc7e91 276 ISO C 99 standard; instead a new variable is invented. */
75bd5ada 277 declare_hidden_char_array ("__func__", name);
be43ff5a 278}
f4c9036d 279
be43ff5a 280static void
281declare_hidden_char_array (name, value)
3eee82c5 282 const char *name, *value;
be43ff5a 283{
284 tree decl, type, init;
285 int vlen;
f4e3c278 286
be43ff5a 287 /* If the default size of char arrays isn't big enough for the name,
3227fe11 288 or if we want to give warnings for large objects, make a bigger one. */
be43ff5a 289 vlen = strlen (value) + 1;
f4c9036d 290 type = char_array_type_node;
2695dec0 291 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < vlen
3227fe11 292 || warn_larger_than)
f4c9036d 293 type = build_array_type (char_type_node,
be43ff5a 294 build_index_type (build_int_2 (vlen, 0)));
be43ff5a 295 decl = build_decl (VAR_DECL, get_identifier (name), type);
f4e3c278 296 TREE_STATIC (decl) = 1;
297 TREE_READONLY (decl) = 1;
be43ff5a 298 TREE_ASM_WRITTEN (decl) = 1;
8658b58d 299 DECL_SOURCE_LINE (decl) = 0;
f5d94a98 300 DECL_ARTIFICIAL (decl) = 1;
776899d6 301 DECL_IN_SYSTEM_HEADER (decl) = 1;
f4e3c278 302 DECL_IGNORED_P (decl) = 1;
be43ff5a 303 init = build_string (vlen, value);
f4c9036d 304 TREE_TYPE (init) = type;
f4e3c278 305 DECL_INITIAL (decl) = init;
306 finish_decl (pushdecl (decl), init, NULL_TREE);
307}
308
b0fc3e72 309/* Given a chain of STRING_CST nodes,
310 concatenate them into one STRING_CST
311 and give it a suitable array-of-chars data type. */
312
313tree
314combine_strings (strings)
315 tree strings;
316{
317 register tree value, t;
318 register int length = 1;
319 int wide_length = 0;
320 int wide_flag = 0;
321 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
322 int nchars;
323
324 if (TREE_CHAIN (strings))
325 {
326 /* More than one in the chain, so concatenate. */
327 register char *p, *q;
328
329 /* Don't include the \0 at the end of each substring,
330 except for the last one.
331 Count wide strings and ordinary strings separately. */
332 for (t = strings; t; t = TREE_CHAIN (t))
333 {
334 if (TREE_TYPE (t) == wchar_array_type_node)
335 {
336 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
337 wide_flag = 1;
338 }
339 else
340 length += (TREE_STRING_LENGTH (t) - 1);
341 }
342
343 /* If anything is wide, the non-wides will be converted,
344 which makes them take more space. */
345 if (wide_flag)
346 length = length * wchar_bytes + wide_length;
347
f8b540f2 348 p = ggc_alloc_string (NULL, length);
b0fc3e72 349
350 /* Copy the individual strings into the new combined string.
351 If the combined string is wide, convert the chars to ints
352 for any individual strings that are not wide. */
353
354 q = p;
355 for (t = strings; t; t = TREE_CHAIN (t))
356 {
357 int len = (TREE_STRING_LENGTH (t)
358 - ((TREE_TYPE (t) == wchar_array_type_node)
359 ? wchar_bytes : 1));
360 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
361 {
d7e36a01 362 memcpy (q, TREE_STRING_POINTER (t), len);
b0fc3e72 363 q += len;
364 }
365 else
366 {
367 int i;
368 for (i = 0; i < len; i++)
c9dcb798 369 {
370 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
371 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
372 else
373 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
374 }
b0fc3e72 375 q += len * wchar_bytes;
376 }
377 }
378 if (wide_flag)
379 {
380 int i;
381 for (i = 0; i < wchar_bytes; i++)
382 *q++ = 0;
383 }
384 else
385 *q = 0;
386
387 value = make_node (STRING_CST);
388 TREE_STRING_POINTER (value) = p;
389 TREE_STRING_LENGTH (value) = length;
b0fc3e72 390 }
391 else
392 {
393 value = strings;
394 length = TREE_STRING_LENGTH (value);
395 if (TREE_TYPE (value) == wchar_array_type_node)
396 wide_flag = 1;
397 }
398
73be5127 399 /* Compute the number of elements, for the array type. */
b0fc3e72 400 nchars = wide_flag ? length / wchar_bytes : length;
401
402 /* Create the array type for the string constant.
403 -Wwrite-strings says make the string constant an array of const char
3a10ba35 404 so that copying it to a non-const pointer will get a warning.
405 For C++, this is the standard behavior. */
406 if (flag_const_strings
b0fc3e72 407 && (! flag_traditional && ! flag_writable_strings))
408 {
409 tree elements
410 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
411 1, 0);
412 TREE_TYPE (value)
413 = build_array_type (elements,
414 build_index_type (build_int_2 (nchars - 1, 0)));
415 }
416 else
417 TREE_TYPE (value)
418 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
419 build_index_type (build_int_2 (nchars - 1, 0)));
3a10ba35 420
b8e3b7ad 421 TREE_CONSTANT (value) = 1;
422 TREE_READONLY (value) = ! flag_writable_strings;
b0fc3e72 423 TREE_STATIC (value) = 1;
424 return value;
425}
426\f
550e135c 427/* To speed up processing of attributes, we maintain an array of
428 IDENTIFIER_NODES and the corresponding attribute types. */
429
430/* Array to hold attribute information. */
431
432static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
433
434static int attrtab_idx = 0;
435
436/* Add an entry to the attribute table above. */
437
438static void
439add_attribute (id, string, min_len, max_len, decl_req)
440 enum attrs id;
3eee82c5 441 const char *string;
550e135c 442 int min_len, max_len;
443 int decl_req;
444{
445 char buf[100];
446
447 attrtab[attrtab_idx].id = id;
448 attrtab[attrtab_idx].name = get_identifier (string);
449 attrtab[attrtab_idx].min = min_len;
450 attrtab[attrtab_idx].max = max_len;
451 attrtab[attrtab_idx++].decl_req = decl_req;
452
453 sprintf (buf, "__%s__", string);
454
455 attrtab[attrtab_idx].id = id;
456 attrtab[attrtab_idx].name = get_identifier (buf);
457 attrtab[attrtab_idx].min = min_len;
458 attrtab[attrtab_idx].max = max_len;
459 attrtab[attrtab_idx++].decl_req = decl_req;
460}
461
462/* Initialize attribute table. */
463
464static void
465init_attributes ()
466{
cf333338 467 add_attribute (A_PACKED, "packed", 0, 0, 0);
1f57d1e5 468 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
9493f142 469 add_attribute (A_COMMON, "common", 0, 0, 1);
550e135c 470 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
471 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
31f820d2 472 add_attribute (A_UNUSED, "unused", 0, 0, 0);
550e135c 473 add_attribute (A_CONST, "const", 0, 0, 1);
474 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
475 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
476 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
477 add_attribute (A_MODE, "mode", 1, 1, 1);
478 add_attribute (A_SECTION, "section", 1, 1, 1);
479 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
24f0e20d 480 add_attribute (A_FORMAT, "format", 3, 3, 1);
b91b108f 481 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
df1c8607 482 add_attribute (A_WEAK, "weak", 0, 0, 1);
483 add_attribute (A_ALIAS, "alias", 1, 1, 1);
abd28cef 484 add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
efea460c 485 add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
7259f3f8 486 add_attribute (A_MALLOC, "malloc", 0, 0, 1);
8f8ac140 487 add_attribute (A_NO_LIMIT_STACK, "no_stack_limit", 0, 0, 1);
550e135c 488}
489\f
695d6519 490/* Default implementation of valid_lang_attribute, below. By default, there
491 are no language-specific attributes. */
492
493static int
494default_valid_lang_attribute (attr_name, attr_args, decl, type)
495 tree attr_name ATTRIBUTE_UNUSED;
496 tree attr_args ATTRIBUTE_UNUSED;
497 tree decl ATTRIBUTE_UNUSED;
498 tree type ATTRIBUTE_UNUSED;
499{
500 return 0;
501}
502
503/* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific
504 attribute for either declaration DECL or type TYPE and 0 otherwise. */
505
6a9a24a5 506int (*valid_lang_attribute) PARAMS ((tree, tree, tree, tree))
695d6519 507 = default_valid_lang_attribute;
508
528adc5a 509/* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
550e135c 510 and install them in NODE, which is either a DECL (including a TYPE_DECL)
511 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
a92771b8 512 and declaration modifiers but before the declaration proper. */
b0fc3e72 513
514void
550e135c 515decl_attributes (node, attributes, prefix_attributes)
516 tree node, attributes, prefix_attributes;
b0fc3e72 517{
7ad3893d 518 tree decl = 0, type = 0;
519 int is_type = 0;
550e135c 520 tree a;
521
522 if (attrtab_idx == 0)
523 init_attributes ();
524
525 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
526 {
527 decl = node;
528 type = TREE_TYPE (decl);
529 is_type = TREE_CODE (node) == TYPE_DECL;
530 }
531 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't')
532 type = node, is_type = 1;
252b13bb 533
a3fa7feb 534#ifdef PRAGMA_INSERT_ATTRIBUTES
535 /* If the code in c-pragma.c wants to insert some attributes then
536 allow it to do so. Do this before allowing machine back ends to
537 insert attributes, so that they have the opportunity to override
538 anything done here. */
539 PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
540#endif
44e9fa65 541
2f491b41 542#ifdef INSERT_ATTRIBUTES
543 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
544#endif
44e9fa65 545
d9df9e36 546 attributes = chainon (prefix_attributes, attributes);
e532650d 547
b0fc3e72 548 for (a = attributes; a; a = TREE_CHAIN (a))
550e135c 549 {
550 tree name = TREE_PURPOSE (a);
551 tree args = TREE_VALUE (a);
552 int i;
553 enum attrs id;
73be5127 554
550e135c 555 for (i = 0; i < attrtab_idx; i++)
556 if (attrtab[i].name == name)
557 break;
558
606926f8 559 if (i == attrtab_idx)
550e135c 560 {
695d6519 561 if (! valid_machine_attribute (name, args, decl, type)
562 && ! (* valid_lang_attribute) (name, args, decl, type))
606926f8 563 warning ("`%s' attribute directive ignored",
564 IDENTIFIER_POINTER (name));
4b708a9e 565 else if (decl != 0)
566 type = TREE_TYPE (decl);
550e135c 567 continue;
568 }
569 else if (attrtab[i].decl_req && decl == 0)
570 {
571 warning ("`%s' attribute does not apply to types",
572 IDENTIFIER_POINTER (name));
573 continue;
574 }
575 else if (list_length (args) < attrtab[i].min
576 || list_length (args) > attrtab[i].max)
577 {
578 error ("wrong number of arguments specified for `%s' attribute",
579 IDENTIFIER_POINTER (name));
580 continue;
581 }
582
583 id = attrtab[i].id;
584 switch (id)
585 {
586 case A_PACKED:
4f21bc40 587 if (is_type)
cf333338 588 TYPE_PACKED (type) = 1;
589 else if (TREE_CODE (decl) == FIELD_DECL)
550e135c 590 DECL_PACKED (decl) = 1;
591 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
592 used for DECL_REGISTER. It wouldn't mean anything anyway. */
593 else
594 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
595 break;
596
1f57d1e5 597 case A_NOCOMMON:
598 if (TREE_CODE (decl) == VAR_DECL)
599 DECL_COMMON (decl) = 0;
600 else
601 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
602 break;
603
9493f142 604 case A_COMMON:
605 if (TREE_CODE (decl) == VAR_DECL)
606 DECL_COMMON (decl) = 1;
607 else
608 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
609 break;
610
550e135c 611 case A_NORETURN:
612 if (TREE_CODE (decl) == FUNCTION_DECL)
613 TREE_THIS_VOLATILE (decl) = 1;
614 else if (TREE_CODE (type) == POINTER_TYPE
615 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
73be5127 616 TREE_TYPE (decl) = type
550e135c 617 = build_pointer_type
618 (build_type_variant (TREE_TYPE (type),
619 TREE_READONLY (TREE_TYPE (type)), 1));
620 else
621 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
622 break;
623
7259f3f8 624 case A_MALLOC:
625 if (TREE_CODE (decl) == FUNCTION_DECL)
626 DECL_IS_MALLOC (decl) = 1;
627 else
628 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
629 break;
630
3cf4cf22 631 case A_UNUSED:
31f820d2 632 if (is_type)
633 TREE_USED (type) = 1;
73be5127 634 else if (TREE_CODE (decl) == PARM_DECL
31f820d2 635 || TREE_CODE (decl) == VAR_DECL
41e80e4b 636 || TREE_CODE (decl) == FUNCTION_DECL
637 || TREE_CODE (decl) == LABEL_DECL)
3cf4cf22 638 TREE_USED (decl) = 1;
639 else
640 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
641 break;
642
550e135c 643 case A_CONST:
644 if (TREE_CODE (decl) == FUNCTION_DECL)
645 TREE_READONLY (decl) = 1;
646 else if (TREE_CODE (type) == POINTER_TYPE
647 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
648 TREE_TYPE (decl) = type
649 = build_pointer_type
650 (build_type_variant (TREE_TYPE (type), 1,
651 TREE_THIS_VOLATILE (TREE_TYPE (type))));
652 else
653 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
654 break;
655
656 case A_T_UNION:
4f21bc40 657 if (is_type
550e135c 658 && TREE_CODE (type) == UNION_TYPE
4f21bc40 659 && (decl == 0
207bece5 660 || (TYPE_FIELDS (type) != 0
661 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
4f21bc40 662 TYPE_TRANSPARENT_UNION (type) = 1;
663 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
550e135c 664 && TREE_CODE (type) == UNION_TYPE
665 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
4f21bc40 666 DECL_TRANSPARENT_UNION (decl) = 1;
550e135c 667 else
668 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
669 break;
670
671 case A_CONSTRUCTOR:
672 if (TREE_CODE (decl) == FUNCTION_DECL
673 && TREE_CODE (type) == FUNCTION_TYPE
674 && decl_function_context (decl) == 0)
6d3e85e7 675 {
676 DECL_STATIC_CONSTRUCTOR (decl) = 1;
677 TREE_USED (decl) = 1;
678 }
550e135c 679 else
680 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
681 break;
682
683 case A_DESTRUCTOR:
684 if (TREE_CODE (decl) == FUNCTION_DECL
685 && TREE_CODE (type) == FUNCTION_TYPE
686 && decl_function_context (decl) == 0)
6d3e85e7 687 {
688 DECL_STATIC_DESTRUCTOR (decl) = 1;
689 TREE_USED (decl) = 1;
690 }
550e135c 691 else
692 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
693 break;
694
695 case A_MODE:
696 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
697 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
698 else
699 {
700 int j;
3eee82c5 701 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
550e135c 702 int len = strlen (p);
703 enum machine_mode mode = VOIDmode;
704 tree typefm;
705
706 if (len > 4 && p[0] == '_' && p[1] == '_'
707 && p[len - 1] == '_' && p[len - 2] == '_')
708 {
22573d05 709 char *newp = (char *) alloca (len - 1);
550e135c 710
711 strcpy (newp, &p[2]);
712 newp[len - 4] = '\0';
713 p = newp;
714 }
715
716 /* Give this decl a type with the specified mode.
717 First check for the special modes. */
718 if (! strcmp (p, "byte"))
719 mode = byte_mode;
720 else if (!strcmp (p, "word"))
721 mode = word_mode;
722 else if (! strcmp (p, "pointer"))
723 mode = ptr_mode;
724 else
725 for (j = 0; j < NUM_MACHINE_MODES; j++)
726 if (!strcmp (p, GET_MODE_NAME (j)))
727 mode = (enum machine_mode) j;
728
729 if (mode == VOIDmode)
730 error ("unknown machine mode `%s'", p);
731 else if (0 == (typefm = type_for_mode (mode,
732 TREE_UNSIGNED (type))))
733 error ("no data type for mode `%s'", p);
734 else
735 {
736 TREE_TYPE (decl) = type = typefm;
b278476e 737 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
550e135c 738 layout_decl (decl, 0);
739 }
740 }
741 break;
742
743 case A_SECTION:
e532650d 744#ifdef ASM_OUTPUT_SECTION_NAME
550e135c 745 if ((TREE_CODE (decl) == FUNCTION_DECL
746 || TREE_CODE (decl) == VAR_DECL)
747 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
748 {
73be5127 749 if (TREE_CODE (decl) == VAR_DECL
28f4825b 750 && current_function_decl != NULL_TREE
751 && ! TREE_STATIC (decl))
550e135c 752 error_with_decl (decl,
753 "section attribute cannot be specified for local variables");
754 /* The decl may have already been given a section attribute from
755 a previous declaration. Ensure they match. */
756 else if (DECL_SECTION_NAME (decl) != NULL_TREE
757 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
758 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
759 error_with_decl (node,
760 "section of `%s' conflicts with previous declaration");
761 else
762 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
763 }
764 else
765 error_with_decl (node,
e532650d 766 "section attribute not allowed for `%s'");
767#else
550e135c 768 error_with_decl (node,
769 "section attributes are not supported for this target");
e532650d 770#endif
550e135c 771 break;
772
773 case A_ALIGNED:
245de75a 774 {
550e135c 775 tree align_expr
21e3a120 776 = (args ? TREE_VALUE (args)
777 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
550e135c 778 int align;
44e9fa65 779
550e135c 780 /* Strip any NOPs of any kind. */
781 while (TREE_CODE (align_expr) == NOP_EXPR
782 || TREE_CODE (align_expr) == CONVERT_EXPR
783 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
784 align_expr = TREE_OPERAND (align_expr, 0);
73be5127 785
550e135c 786 if (TREE_CODE (align_expr) != INTEGER_CST)
787 {
788 error ("requested alignment is not a constant");
789 continue;
790 }
791
792 align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
793
794 if (exact_log2 (align) == -1)
795 error ("requested alignment is not a power of 2");
796 else if (is_type)
482cae09 797 TYPE_ALIGN (type) = align;
550e135c 798 else if (TREE_CODE (decl) != VAR_DECL
799 && TREE_CODE (decl) != FIELD_DECL)
800 error_with_decl (decl,
801 "alignment may not be specified for `%s'");
802 else
803 DECL_ALIGN (decl) = align;
245de75a 804 }
550e135c 805 break;
245de75a 806
550e135c 807 case A_FORMAT:
b0fc3e72 808 {
d1f11193 809 tree format_type_id = TREE_VALUE (args);
550e135c 810 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
811 tree first_arg_num_expr
812 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
813 int format_num;
814 int first_arg_num;
d1f11193 815 enum format_type format_type;
550e135c 816 tree argument;
817 int arg_num;
73be5127 818
550e135c 819 if (TREE_CODE (decl) != FUNCTION_DECL)
820 {
821 error_with_decl (decl,
822 "argument format specified for non-function `%s'");
823 continue;
824 }
44e9fa65 825
d1f11193 826 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
b91b108f 827 {
d1f11193 828 error ("unrecognized format specifier");
b91b108f 829 continue;
830 }
550e135c 831 else
832 {
3eee82c5 833 const char *p = IDENTIFIER_POINTER (format_type_id);
44e9fa65 834
d1f11193 835 if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
836 format_type = printf_format_type;
837 else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
838 format_type = scanf_format_type;
839 else if (!strcmp (p, "strftime")
840 || !strcmp (p, "__strftime__"))
841 format_type = strftime_format_type;
842 else
843 {
f1959ea2 844 warning ("`%s' is an unrecognized format function type", p);
d1f11193 845 continue;
846 }
550e135c 847 }
245de75a 848
550e135c 849 /* Strip any conversions from the string index and first arg number
850 and verify they are constants. */
851 while (TREE_CODE (format_num_expr) == NOP_EXPR
852 || TREE_CODE (format_num_expr) == CONVERT_EXPR
853 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
854 format_num_expr = TREE_OPERAND (format_num_expr, 0);
d6cd1111 855
550e135c 856 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
857 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
858 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
859 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
860
861 if (TREE_CODE (format_num_expr) != INTEGER_CST
862 || TREE_CODE (first_arg_num_expr) != INTEGER_CST)
557b092e 863 {
550e135c 864 error ("format string has non-constant operand number");
865 continue;
557b092e 866 }
550e135c 867
868 format_num = TREE_INT_CST_LOW (format_num_expr);
869 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
870 if (first_arg_num != 0 && first_arg_num <= format_num)
5d730828 871 {
550e135c 872 error ("format string arg follows the args to be formatted");
245de75a 873 continue;
5d730828 874 }
550e135c 875
876 /* If a parameter list is specified, verify that the format_num
877 argument is actually a string, in case the format attribute
878 is in error. */
879 argument = TYPE_ARG_TYPES (type);
880 if (argument)
557b092e 881 {
550e135c 882 for (arg_num = 1; ; ++arg_num)
557b092e 883 {
550e135c 884 if (argument == 0 || arg_num == format_num)
885 break;
886 argument = TREE_CHAIN (argument);
887 }
888 if (! argument
889 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
890 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
891 != char_type_node))
892 {
893 error ("format string arg not a string type");
557b092e 894 continue;
895 }
550e135c 896 if (first_arg_num != 0)
897 {
898 /* Verify that first_arg_num points to the last arg,
a92771b8 899 the ... */
550e135c 900 while (argument)
901 arg_num++, argument = TREE_CHAIN (argument);
902 if (arg_num != first_arg_num)
903 {
904 error ("args to be formatted is not ...");
905 continue;
906 }
907 }
557b092e 908 }
252b13bb 909
550e135c 910 record_function_format (DECL_NAME (decl),
911 DECL_ASSEMBLER_NAME (decl),
d1f11193 912 format_type, format_num, first_arg_num);
550e135c 913 break;
914 }
df1c8607 915
b91b108f 916 case A_FORMAT_ARG:
917 {
918 tree format_num_expr = TREE_VALUE (args);
919 int format_num, arg_num;
920 tree argument;
73be5127 921
b91b108f 922 if (TREE_CODE (decl) != FUNCTION_DECL)
923 {
924 error_with_decl (decl,
925 "argument format specified for non-function `%s'");
926 continue;
927 }
73be5127 928
b91b108f 929 /* Strip any conversions from the first arg number and verify it
930 is a constant. */
931 while (TREE_CODE (format_num_expr) == NOP_EXPR
932 || TREE_CODE (format_num_expr) == CONVERT_EXPR
933 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
934 format_num_expr = TREE_OPERAND (format_num_expr, 0);
935
936 if (TREE_CODE (format_num_expr) != INTEGER_CST)
937 {
938 error ("format string has non-constant operand number");
939 continue;
940 }
941
942 format_num = TREE_INT_CST_LOW (format_num_expr);
943
944 /* If a parameter list is specified, verify that the format_num
945 argument is actually a string, in case the format attribute
946 is in error. */
947 argument = TYPE_ARG_TYPES (type);
948 if (argument)
949 {
950 for (arg_num = 1; ; ++arg_num)
951 {
952 if (argument == 0 || arg_num == format_num)
953 break;
954 argument = TREE_CHAIN (argument);
955 }
956 if (! argument
957 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
958 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
959 != char_type_node))
960 {
961 error ("format string arg not a string type");
962 continue;
963 }
964 }
965
966 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
967 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
968 != char_type_node))
969 {
970 error ("function does not return string type");
971 continue;
972 }
973
974 record_international_format (DECL_NAME (decl),
975 DECL_ASSEMBLER_NAME (decl),
976 format_num);
977 break;
978 }
979
df1c8607 980 case A_WEAK:
981 declare_weak (decl);
982 break;
983
984 case A_ALIAS:
985 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
65dab9aa 986 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
df1c8607 987 error_with_decl (decl,
988 "`%s' defined both normally and as an alias");
989 else if (decl_function_context (decl) == 0)
990 {
17971af5 991 tree id;
992
993 id = TREE_VALUE (args);
994 if (TREE_CODE (id) != STRING_CST)
995 {
996 error ("alias arg not a string");
997 break;
998 }
999 id = get_identifier (TREE_STRING_POINTER (id));
1000
df1c8607 1001 if (TREE_CODE (decl) == FUNCTION_DECL)
1002 DECL_INITIAL (decl) = error_mark_node;
1003 else
1004 DECL_EXTERNAL (decl) = 0;
1005 assemble_alias (decl, id);
1006 }
1007 else
1008 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1009 break;
abd28cef 1010
efea460c 1011 case A_NO_CHECK_MEMORY_USAGE:
1012 if (TREE_CODE (decl) != FUNCTION_DECL)
1013 {
1014 error_with_decl (decl,
1015 "`%s' attribute applies only to functions",
1016 IDENTIFIER_POINTER (name));
1017 }
1018 else if (DECL_INITIAL (decl))
1019 {
1020 error_with_decl (decl,
1021 "can't set `%s' attribute after definition",
1022 IDENTIFIER_POINTER (name));
1023 }
1024 else
1025 DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
1026 break;
1027
abd28cef 1028 case A_NO_INSTRUMENT_FUNCTION:
1029 if (TREE_CODE (decl) != FUNCTION_DECL)
1030 {
1031 error_with_decl (decl,
1032 "`%s' attribute applies only to functions",
1033 IDENTIFIER_POINTER (name));
1034 }
1035 else if (DECL_INITIAL (decl))
1036 {
1037 error_with_decl (decl,
1038 "can't set `%s' attribute after definition",
1039 IDENTIFIER_POINTER (name));
1040 }
1041 else
1042 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1043 break;
8f8ac140 1044
1045 case A_NO_LIMIT_STACK:
1046 if (TREE_CODE (decl) != FUNCTION_DECL)
1047 {
1048 error_with_decl (decl,
1049 "`%s' attribute applies only to functions",
1050 IDENTIFIER_POINTER (name));
1051 }
1052 else if (DECL_INITIAL (decl))
1053 {
1054 error_with_decl (decl,
1055 "can't set `%s' attribute after definition",
1056 IDENTIFIER_POINTER (name));
1057 }
1058 else
1059 DECL_NO_LIMIT_STACK (decl) = 1;
1060 break;
550e135c 1061 }
1062 }
b0fc3e72 1063}
d53e9dc0 1064
1065/* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1066 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1067
1068 The head of the declspec list is stored in DECLSPECS.
1069 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1070
1071 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1072 the list elements. We drop the containing TREE_LIST nodes and link the
1073 resulting attributes together the way decl_attributes expects them. */
1074
1075void
1076split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1077 tree specs_attrs;
1078 tree *declspecs, *prefix_attributes;
1079{
1080 tree t, s, a, next, specs, attrs;
1081
ece7cbbe 1082 /* This can happen after an __extension__ in pedantic mode. */
1083 if (specs_attrs != NULL_TREE
1084 && TREE_CODE (specs_attrs) == INTEGER_CST)
1085 {
1086 *declspecs = NULL_TREE;
1087 *prefix_attributes = NULL_TREE;
1088 return;
1089 }
1090
d53e9dc0 1091 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1092 if (specs_attrs != NULL_TREE
1093 && TREE_CODE (specs_attrs) != TREE_LIST)
1094 {
1095 *declspecs = specs_attrs;
1096 *prefix_attributes = NULL_TREE;
1097 return;
1098 }
1099
1100 /* Remember to keep the lists in the same order, element-wise. */
1101
1102 specs = s = NULL_TREE;
1103 attrs = a = NULL_TREE;
1104 for (t = specs_attrs; t; t = next)
1105 {
1106 next = TREE_CHAIN (t);
1107 /* Declspecs have a non-NULL TREE_VALUE. */
1108 if (TREE_VALUE (t) != NULL_TREE)
1109 {
1110 if (specs == NULL_TREE)
1111 specs = s = t;
1112 else
1113 {
1114 TREE_CHAIN (s) = t;
1115 s = t;
1116 }
1117 }
1118 else
1119 {
1120 if (attrs == NULL_TREE)
1121 attrs = a = TREE_PURPOSE (t);
1122 else
1123 {
1124 TREE_CHAIN (a) = TREE_PURPOSE (t);
1125 a = TREE_PURPOSE (t);
1126 }
1127 /* More attrs can be linked here, move A to the end. */
1128 while (TREE_CHAIN (a) != NULL_TREE)
1129 a = TREE_CHAIN (a);
1130 }
1131 }
1132
1133 /* Terminate the lists. */
1134 if (s != NULL_TREE)
1135 TREE_CHAIN (s) = NULL_TREE;
1136 if (a != NULL_TREE)
1137 TREE_CHAIN (a) = NULL_TREE;
1138
1139 /* All done. */
1140 *declspecs = specs;
1141 *prefix_attributes = attrs;
1142}
0bf60c2b 1143
1144/* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1145 This function is used by the parser when a rule will accept attributes
1146 in a particular position, but we don't want to support that just yet.
1147
1148 A warning is issued for every ignored attribute. */
1149
1150tree
1151strip_attrs (specs_attrs)
1152 tree specs_attrs;
1153{
1154 tree specs, attrs;
1155
1156 split_specs_attrs (specs_attrs, &specs, &attrs);
1157
1158 while (attrs)
1159 {
1160 warning ("`%s' attribute ignored",
1161 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1162 attrs = TREE_CHAIN (attrs);
1163 }
1164
1165 return specs;
1166}
b0fc3e72 1167\f
fce9faf9 1168/* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1169 a parameter list. */
1170
1171#define T_I &integer_type_node
1172#define T_L &long_integer_type_node
93fccc0a 1173#define T_LL &long_long_integer_type_node
fce9faf9 1174#define T_S &short_integer_type_node
1175#define T_UI &unsigned_type_node
1176#define T_UL &long_unsigned_type_node
93fccc0a 1177#define T_ULL &long_long_unsigned_type_node
fce9faf9 1178#define T_US &short_unsigned_type_node
1179#define T_F &float_type_node
1180#define T_D &double_type_node
1181#define T_LD &long_double_type_node
1182#define T_C &char_type_node
73be5127 1183#define T_UC &unsigned_char_type_node
fce9faf9 1184#define T_V &void_type_node
1185#define T_W &wchar_type_node
7710f895 1186#define T_ST &sizetype
fce9faf9 1187
1188typedef struct {
3eee82c5 1189 const char *format_chars;
fce9faf9 1190 int pointer_count;
1191 /* Type of argument if no length modifier is used. */
1192 tree *nolen;
33b8b501 1193 /* Type of argument if length modifier for shortening to byte is used.
fce9faf9 1194 If NULL, then this modifier is not allowed. */
73be5127 1195 tree *hhlen;
33b8b501 1196 /* Type of argument if length modifier for shortening is used.
73be5127 1197 If NULL, then this modifier is not allowed. */
fce9faf9 1198 tree *hlen;
1199 /* Type of argument if length modifier `l' is used.
1200 If NULL, then this modifier is not allowed. */
1201 tree *llen;
4a1640ed 1202 /* Type of argument if length modifier `q' or `ll' is used.
93fccc0a 1203 If NULL, then this modifier is not allowed. */
1204 tree *qlen;
fce9faf9 1205 /* Type of argument if length modifier `L' is used.
1206 If NULL, then this modifier is not allowed. */
1207 tree *bigllen;
44e9fa65 1208 /* Type of argument if length modifiers 'z' or `Z' is used.
997d68fe 1209 If NULL, then this modifier is not allowed. */
1210 tree *zlen;
fce9faf9 1211 /* List of other modifier characters allowed with these options. */
3eee82c5 1212 const char *flag_chars;
fce9faf9 1213} format_char_info;
1214
1215static format_char_info print_char_table[] = {
73be5127 1216 { "di", 0, T_I, T_I, T_I, T_L, T_LL, T_LL, T_ST, "-wp0 +" },
1217 { "oxX", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0#" },
1218 { "u", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0" },
997d68fe 1219/* A GNU extension. */
73be5127 1220 { "m", 0, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1221 { "feEgGaA", 0, T_D, NULL, NULL, NULL, NULL, T_LD, NULL, "-wp0 +#" },
1222 { "c", 0, T_I, NULL, NULL, T_W, NULL, NULL, NULL, "-w" },
1223 { "C", 0, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1224 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "-wp" },
1225 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1226 { "p", 1, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1227 { "n", 1, T_I, NULL, T_S, T_L, T_LL, NULL, NULL, "" },
57380eb2 1228 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
fce9faf9 1229};
1230
1231static format_char_info scan_char_table[] = {
73be5127 1232 { "di", 1, T_I, T_C, T_S, T_L, T_LL, T_LL, NULL, "*" },
1233 { "ouxX", 1, T_UI, T_UC, T_US, T_UL, T_ULL, T_ULL, NULL, "*" },
1234 { "efgEGaA", 1, T_F, NULL, NULL, T_D, NULL, T_LD, NULL, "*" },
2d47efb6 1235 { "c", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*" },
1236 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*a" },
73be5127 1237 { "[", 1, T_C, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1238 { "C", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
2d47efb6 1239 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
73be5127 1240 { "p", 2, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1241 { "n", 1, T_I, T_C, T_S, T_L, T_LL, NULL, NULL, "" },
57380eb2 1242 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
fce9faf9 1243};
1244
d1f11193 1245/* Handle format characters recognized by glibc's strftime.c.
1246 '2' - MUST do years as only two digits
1247 '3' - MAY do years as only two digits (depending on locale)
1248 'E' - E modifier is acceptable
1249 'O' - O modifier is acceptable to Standard C
1250 'o' - O modifier is acceptable as a GNU extension
1251 'G' - other GNU extensions */
1252
1253static format_char_info time_char_table[] = {
73be5127 1254 { "y", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2EO-_0w" },
1255 { "D", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2" },
1256 { "g", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2O-_0w" },
1257 { "cx", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "3E" },
1258 { "%RTXnrt", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "" },
1259 { "P", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "G" },
1260 { "HIMSUWdemw", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Ow" },
1261 { "Vju", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Oow" },
1262 { "Gklsz", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0OGw" },
236aaf54 1263 { "ABZa", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^#" },
73be5127 1264 { "p", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "#" },
1265 { "bh", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^" },
1266 { "CY", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0EOw" },
57380eb2 1267 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
d1f11193 1268};
1269
b91b108f 1270typedef struct function_format_info
1271{
fce9faf9 1272 struct function_format_info *next; /* next structure on the list */
1273 tree name; /* identifier such as "printf" */
1274 tree assembler_name; /* optional mangled identifier (for C++) */
d1f11193 1275 enum format_type format_type; /* type of format (printf, scanf, etc.) */
fce9faf9 1276 int format_num; /* number of format argument */
1277 int first_arg_num; /* number of first arg (zero for varargs) */
1278} function_format_info;
1279
1280static function_format_info *function_format_list = NULL;
1281
b91b108f 1282typedef struct international_format_info
1283{
1284 struct international_format_info *next; /* next structure on the list */
1285 tree name; /* identifier such as "gettext" */
1286 tree assembler_name; /* optional mangled identifier (for C++) */
1287 int format_num; /* number of format argument */
1288} international_format_info;
1289
1290static international_format_info *international_format_list = NULL;
1291
6a9a24a5 1292static void check_format_info PARAMS ((function_format_info *, tree));
fce9faf9 1293
1294/* Initialize the table of functions to perform format checking on.
1295 The ANSI functions are always checked (whether <stdio.h> is
1296 included or not), since it is common to call printf without
1297 including <stdio.h>. There shouldn't be a problem with this,
1298 since ANSI reserves these function names whether you include the
73be5127 1299 header file or not. In any case, the checking is harmless.
b91b108f 1300
1301 Also initialize the name of function that modify the format string for
1302 internationalization purposes. */
fce9faf9 1303
1304void
1305init_function_format_info ()
1306{
d1f11193 1307 record_function_format (get_identifier ("printf"), NULL_TREE,
1308 printf_format_type, 1, 2);
1309 record_function_format (get_identifier ("fprintf"), NULL_TREE,
1310 printf_format_type, 2, 3);
1311 record_function_format (get_identifier ("sprintf"), NULL_TREE,
1312 printf_format_type, 2, 3);
1313 record_function_format (get_identifier ("scanf"), NULL_TREE,
1314 scanf_format_type, 1, 2);
1315 record_function_format (get_identifier ("fscanf"), NULL_TREE,
1316 scanf_format_type, 2, 3);
1317 record_function_format (get_identifier ("sscanf"), NULL_TREE,
1318 scanf_format_type, 2, 3);
1319 record_function_format (get_identifier ("vprintf"), NULL_TREE,
1320 printf_format_type, 1, 0);
1321 record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1322 printf_format_type, 2, 0);
1323 record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1324 printf_format_type, 2, 0);
1325 record_function_format (get_identifier ("strftime"), NULL_TREE,
1326 strftime_format_type, 3, 0);
b91b108f 1327
1328 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1329 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1330 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
fce9faf9 1331}
1332
1333/* Record information for argument format checking. FUNCTION_IDENT is
1334 the identifier node for the name of the function to check (its decl
d1f11193 1335 need not exist yet).
1336 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
fce9faf9 1337 of the argument which is the format control string (starting from 1).
1338 FIRST_ARG_NUM is the number of the first actual argument to check
b91b108f 1339 against the format string, or zero if no checking is not be done
fce9faf9 1340 (e.g. for varargs such as vfprintf). */
1341
d1f11193 1342static void
1343record_function_format (name, assembler_name, format_type,
fce9faf9 1344 format_num, first_arg_num)
1345 tree name;
1346 tree assembler_name;
d1f11193 1347 enum format_type format_type;
fce9faf9 1348 int format_num;
1349 int first_arg_num;
1350{
1351 function_format_info *info;
1352
1353 /* Re-use existing structure if it's there. */
1354
1355 for (info = function_format_list; info; info = info->next)
1356 {
1357 if (info->name == name && info->assembler_name == assembler_name)
1358 break;
1359 }
1360 if (! info)
1361 {
1362 info = (function_format_info *) xmalloc (sizeof (function_format_info));
1363 info->next = function_format_list;
1364 function_format_list = info;
1365
1366 info->name = name;
1367 info->assembler_name = assembler_name;
1368 }
1369
d1f11193 1370 info->format_type = format_type;
fce9faf9 1371 info->format_num = format_num;
1372 info->first_arg_num = first_arg_num;
1373}
1374
b91b108f 1375/* Record information for the names of function that modify the format
1376 argument to format functions. FUNCTION_IDENT is the identifier node for
1377 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1378 the number of the argument which is the format control string (starting
1379 from 1). */
1380
a5c74c6a 1381static void
b91b108f 1382record_international_format (name, assembler_name, format_num)
1383 tree name;
1384 tree assembler_name;
1385 int format_num;
1386{
1387 international_format_info *info;
1388
1389 /* Re-use existing structure if it's there. */
1390
1391 for (info = international_format_list; info; info = info->next)
1392 {
1393 if (info->name == name && info->assembler_name == assembler_name)
1394 break;
1395 }
1396
1397 if (! info)
1398 {
1399 info
1400 = (international_format_info *)
1401 xmalloc (sizeof (international_format_info));
1402 info->next = international_format_list;
1403 international_format_list = info;
1404
1405 info->name = name;
1406 info->assembler_name = assembler_name;
1407 }
1408
1409 info->format_num = format_num;
1410}
1411
be2828ce 1412static void
1413tfaff ()
1414{
1415 warning ("too few arguments for format");
1416}
fce9faf9 1417\f
1418/* Check the argument list of a call to printf, scanf, etc.
1419 NAME is the function identifier.
1420 ASSEMBLER_NAME is the function's assembler identifier.
1421 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1422 PARAMS is the list of argument values. */
1423
1424void
1425check_function_format (name, assembler_name, params)
1426 tree name;
1427 tree assembler_name;
1428 tree params;
1429{
1430 function_format_info *info;
1431
1432 /* See if this function is a format function. */
1433 for (info = function_format_list; info; info = info->next)
1434 {
236236da 1435 if (info->assembler_name
fce9faf9 1436 ? (info->assembler_name == assembler_name)
1437 : (info->name == name))
1438 {
1439 /* Yup; check it. */
1440 check_format_info (info, params);
1441 break;
1442 }
1443 }
1444}
1445
1446/* Check the argument list of a call to printf, scanf, etc.
1447 INFO points to the function_format_info structure.
1448 PARAMS is the list of argument values. */
1449
1450static void
1451check_format_info (info, params)
1452 function_format_info *info;
1453 tree params;
1454{
1455 int i;
1456 int arg_num;
1457 int suppressed, wide, precise;
6c00ca1e 1458 int length_char = 0;
fce9faf9 1459 int format_char;
1460 int format_length;
1461 tree format_tree;
1462 tree cur_param;
1463 tree cur_type;
1464 tree wanted_type;
f19cab4a 1465 tree first_fillin_param;
3eee82c5 1466 const char *format_chars;
6c00ca1e 1467 format_char_info *fci = NULL;
fce9faf9 1468 char flag_chars[8];
f19cab4a 1469 int has_operand_number = 0;
fce9faf9 1470
1471 /* Skip to format argument. If the argument isn't available, there's
1472 no work for us to do; prototype checking will catch the problem. */
1473 for (arg_num = 1; ; ++arg_num)
1474 {
1475 if (params == 0)
1476 return;
1477 if (arg_num == info->format_num)
1478 break;
1479 params = TREE_CHAIN (params);
1480 }
1481 format_tree = TREE_VALUE (params);
1482 params = TREE_CHAIN (params);
1483 if (format_tree == 0)
1484 return;
b91b108f 1485
fce9faf9 1486 /* We can only check the format if it's a string constant. */
1487 while (TREE_CODE (format_tree) == NOP_EXPR)
1488 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
b91b108f 1489
1490 if (TREE_CODE (format_tree) == CALL_EXPR
1491 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1492 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1493 == FUNCTION_DECL))
1494 {
1495 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1496
1497 /* See if this is a call to a known internationalization function
1498 that modifies the format arg. */
1499 international_format_info *info;
1500
1501 for (info = international_format_list; info; info = info->next)
1502 if (info->assembler_name
1503 ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
1504 : (info->name == DECL_NAME (function)))
1505 {
1506 tree inner_args;
1507 int i;
1508
1509 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
1510 inner_args != 0;
1511 inner_args = TREE_CHAIN (inner_args), i++)
1512 if (i == info->format_num)
1513 {
1514 format_tree = TREE_VALUE (inner_args);
1515
1516 while (TREE_CODE (format_tree) == NOP_EXPR)
1517 format_tree = TREE_OPERAND (format_tree, 0);
1518 }
1519 }
1520 }
1521
a8a713e9 1522 if (integer_zerop (format_tree))
fce9faf9 1523 {
1524 warning ("null format string");
1525 return;
1526 }
1527 if (TREE_CODE (format_tree) != ADDR_EXPR)
b7e644ba 1528 {
1529 /* The user may get multiple warnings if the supplied argument
1530 isn't even a string pointer. */
75da12e2 1531 /* Functions taking a va_list normally pass a non-literal format
1532 string. These functions typically are declared with
1533 first_arg_num == 0, so avoid warning in those cases. */
70f6ee86 1534 if (info->first_arg_num != 0 && warn_format > 1)
75da12e2 1535 warning ("format not a string literal, argument types not checked");
b7e644ba 1536 return;
1537 }
fce9faf9 1538 format_tree = TREE_OPERAND (format_tree, 0);
1539 if (TREE_CODE (format_tree) != STRING_CST)
b7e644ba 1540 {
1541 /* The user may get multiple warnings if the supplied argument
1542 isn't even a string pointer. */
75da12e2 1543 /* Functions taking a va_list normally pass a non-literal format
1544 string. These functions typically are declared with
1545 first_arg_num == 0, so avoid warning in those cases. */
70f6ee86 1546 if (info->first_arg_num != 0 && warn_format > 1)
75da12e2 1547 warning ("format not a string literal, argument types not checked");
b7e644ba 1548 return;
1549 }
fce9faf9 1550 format_chars = TREE_STRING_POINTER (format_tree);
1551 format_length = TREE_STRING_LENGTH (format_tree);
1552 if (format_length <= 1)
1553 warning ("zero-length format string");
1554 if (format_chars[--format_length] != 0)
1555 {
1556 warning ("unterminated format string");
1557 return;
1558 }
1559 /* Skip to first argument to check. */
1560 while (arg_num + 1 < info->first_arg_num)
1561 {
1562 if (params == 0)
1563 return;
1564 params = TREE_CHAIN (params);
1565 ++arg_num;
1566 }
f19cab4a 1567
1568 first_fillin_param = params;
fce9faf9 1569 while (1)
1570 {
63b97047 1571 int aflag;
fce9faf9 1572 if (*format_chars == 0)
1573 {
1574 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
1575 warning ("embedded `\\0' in format");
f19cab4a 1576 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
fce9faf9 1577 warning ("too many arguments for format");
1578 return;
1579 }
1580 if (*format_chars++ != '%')
1581 continue;
1582 if (*format_chars == 0)
1583 {
1584 warning ("spurious trailing `%%' in format");
1585 continue;
1586 }
1587 if (*format_chars == '%')
1588 {
1589 ++format_chars;
1590 continue;
1591 }
1592 flag_chars[0] = 0;
1593 suppressed = wide = precise = FALSE;
d1f11193 1594 if (info->format_type == scanf_format_type)
fce9faf9 1595 {
1596 suppressed = *format_chars == '*';
1597 if (suppressed)
1598 ++format_chars;
cc404a47 1599 while (ISDIGIT (*format_chars))
fce9faf9 1600 ++format_chars;
1601 }
d1f11193 1602 else if (info->format_type == strftime_format_type)
1603 {
1604 while (*format_chars != 0 && index ("_-0^#", *format_chars) != 0)
1605 {
1606 if (pedantic)
1607 warning ("ANSI C does not support the strftime `%c' flag",
1608 *format_chars);
1609 if (index (flag_chars, *format_chars) != 0)
1610 {
1611 warning ("repeated `%c' flag in format",
1612 *format_chars);
1613 ++format_chars;
1614 }
1615 else
1616 {
1617 i = strlen (flag_chars);
1618 flag_chars[i++] = *format_chars++;
1619 flag_chars[i] = 0;
1620 }
1621 }
cc404a47 1622 while (ISDIGIT ((unsigned char) *format_chars))
d1f11193 1623 {
1624 wide = TRUE;
1625 ++format_chars;
1626 }
1627 if (wide && pedantic)
1628 warning ("ANSI C does not support strftime format width");
1629 if (*format_chars == 'E' || *format_chars == 'O')
1630 {
1631 i = strlen (flag_chars);
1632 flag_chars[i++] = *format_chars++;
1633 flag_chars[i] = 0;
1634 if (*format_chars == 'E' || *format_chars == 'O')
1635 {
1636 warning ("multiple E/O modifiers in format");
1637 while (*format_chars == 'E' || *format_chars == 'O')
1638 ++format_chars;
1639 }
1640 }
1641 }
1642 else if (info->format_type == printf_format_type)
fce9faf9 1643 {
f19cab4a 1644 /* See if we have a number followed by a dollar sign. If we do,
1645 it is an operand number, so set PARAMS to that operand. */
1646 if (*format_chars >= '0' && *format_chars <= '9')
1647 {
3eee82c5 1648 const char *p = format_chars;
f19cab4a 1649
1650 while (*p >= '0' && *p++ <= '9')
1651 ;
1652
1653 if (*p == '$')
1654 {
1655 int opnum = atoi (format_chars);
1656
1657 params = first_fillin_param;
1658 format_chars = p + 1;
1659 has_operand_number = 1;
1660
1661 for (i = 1; i < opnum && params != 0; i++)
1662 params = TREE_CHAIN (params);
1663
1664 if (opnum == 0 || params == 0)
1665 {
1666 warning ("operand number out of range in format");
1667 return;
1668 }
1669 }
1670 }
1671
fce9faf9 1672 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
1673 {
1674 if (index (flag_chars, *format_chars) != 0)
d1f11193 1675 warning ("repeated `%c' flag in format", *format_chars++);
31f820d2 1676 else
1677 {
1678 i = strlen (flag_chars);
1679 flag_chars[i++] = *format_chars++;
1680 flag_chars[i] = 0;
1681 }
fce9faf9 1682 }
73be5127 1683 /* "If the space and + flags both appear,
fce9faf9 1684 the space flag will be ignored." */
1685 if (index (flag_chars, ' ') != 0
1686 && index (flag_chars, '+') != 0)
1687 warning ("use of both ` ' and `+' flags in format");
1688 /* "If the 0 and - flags both appear,
1689 the 0 flag will be ignored." */
1690 if (index (flag_chars, '0') != 0
1691 && index (flag_chars, '-') != 0)
1692 warning ("use of both `0' and `-' flags in format");
1693 if (*format_chars == '*')
1694 {
1695 wide = TRUE;
1696 /* "...a field width...may be indicated by an asterisk.
1697 In this case, an int argument supplies the field width..." */
1698 ++format_chars;
1699 if (params == 0)
1700 {
be2828ce 1701 tfaff ();
fce9faf9 1702 return;
1703 }
1704 if (info->first_arg_num != 0)
1705 {
1706 cur_param = TREE_VALUE (params);
1707 params = TREE_CHAIN (params);
1708 ++arg_num;
1709 /* size_t is generally not valid here.
1710 It will work on most machines, because size_t and int
1711 have the same mode. But might as well warn anyway,
1712 since it will fail on other machines. */
cbc5b7d9 1713 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1714 != integer_type_node)
1715 &&
1716 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1717 != unsigned_type_node))
d1f11193 1718 warning ("field width is not type int (arg %d)", arg_num);
fce9faf9 1719 }
1720 }
1721 else
1722 {
cc404a47 1723 while (ISDIGIT (*format_chars))
fce9faf9 1724 {
1725 wide = TRUE;
1726 ++format_chars;
1727 }
1728 }
1729 if (*format_chars == '.')
1730 {
1731 precise = TRUE;
1732 ++format_chars;
cc404a47 1733 if (*format_chars != '*' && !ISDIGIT (*format_chars))
fce9faf9 1734 warning ("`.' not followed by `*' or digit in format");
1735 /* "...a...precision...may be indicated by an asterisk.
1736 In this case, an int argument supplies the...precision." */
1737 if (*format_chars == '*')
1738 {
1739 if (info->first_arg_num != 0)
1740 {
1741 ++format_chars;
1742 if (params == 0)
1743 {
be2828ce 1744 tfaff ();
fce9faf9 1745 return;
1746 }
1747 cur_param = TREE_VALUE (params);
1748 params = TREE_CHAIN (params);
1749 ++arg_num;
1750 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1751 != integer_type_node)
d1f11193 1752 warning ("field width is not type int (arg %d)",
1753 arg_num);
fce9faf9 1754 }
1755 }
1756 else
1757 {
cc404a47 1758 while (ISDIGIT (*format_chars))
fce9faf9 1759 ++format_chars;
1760 }
1761 }
1762 }
d1f11193 1763
63b97047 1764 aflag = 0;
d1f11193 1765
1766 if (info->format_type != strftime_format_type)
63b97047 1767 {
d1f11193 1768 if (*format_chars == 'h' || *format_chars == 'l')
1769 length_char = *format_chars++;
1770 else if (*format_chars == 'q' || *format_chars == 'L')
31f820d2 1771 {
d1f11193 1772 length_char = *format_chars++;
c5aa1e92 1773 if (pedantic)
d1f11193 1774 warning ("ANSI C does not support the `%c' length modifier",
1775 length_char);
31f820d2 1776 }
44e9fa65 1777 else if (*format_chars == 'Z' || *format_chars == 'z')
d1f11193 1778 {
1779 length_char = *format_chars++;
85fc7e91 1780 if (pedantic && (length_char == 'Z' || !flag_isoc99))
44e9fa65 1781 warning ("ANSI C does not support the `%c' length modifier",
1782 length_char);
d1f11193 1783 }
1784 else
1785 length_char = 0;
1786 if (length_char == 'l' && *format_chars == 'l')
1787 {
1788 length_char = 'q', format_chars++;
85fc7e91 1789 if (pedantic && !flag_isoc99)
d1f11193 1790 warning ("ANSI C does not support the `ll' length modifier");
1791 }
33b8b501 1792 else if (length_char == 'h' && *format_chars == 'h')
1793 {
1794 length_char = 'H', format_chars++;
85fc7e91 1795 if (pedantic && !flag_isoc99)
33b8b501 1796 warning ("ANSI C does not support the `hh' length modifier");
1797 }
d1f11193 1798 if (*format_chars == 'a' && info->format_type == scanf_format_type)
1799 {
1800 if (format_chars[1] == 's' || format_chars[1] == 'S'
1801 || format_chars[1] == '[')
1802 {
1803 /* `a' is used as a flag. */
1804 aflag = 1;
1805 format_chars++;
1806 }
1807 }
1808 if (suppressed && length_char != 0)
1809 warning ("use of `*' and `%c' together in format", length_char);
fce9faf9 1810 }
1811 format_char = *format_chars;
d1f11193 1812 if (format_char == 0
1813 || (info->format_type != strftime_format_type && format_char == '%'))
fce9faf9 1814 {
1815 warning ("conversion lacks type at end of format");
1816 continue;
1817 }
2d47efb6 1818 /* The m, C, and S formats are GNU extensions. */
1819 if (pedantic && info->format_type != strftime_format_type
1820 && (format_char == 'm' || format_char == 'C' || format_char == 'S'))
1821 warning ("ANSI C does not support the `%c' format", format_char);
44e9fa65 1822 /* The a and A formats are C99 extensions. */
2d47efb6 1823 if (pedantic && info->format_type != strftime_format_type
44e9fa65 1824 && (format_char == 'a' || format_char == 'A')
85fc7e91 1825 && !flag_isoc99)
2d47efb6 1826 warning ("ANSI C does not support the `%c' format", format_char);
fce9faf9 1827 format_chars++;
d1f11193 1828 switch (info->format_type)
1829 {
1830 case printf_format_type:
1831 fci = print_char_table;
1832 break;
1833 case scanf_format_type:
1834 fci = scan_char_table;
1835 break;
1836 case strftime_format_type:
1837 fci = time_char_table;
1838 break;
1839 default:
1840 abort ();
1841 }
fce9faf9 1842 while (fci->format_chars != 0
1843 && index (fci->format_chars, format_char) == 0)
1844 ++fci;
1845 if (fci->format_chars == 0)
1846 {
38330f30 1847 if (ISGRAPH(format_char))
d1f11193 1848 warning ("unknown conversion type character `%c' in format",
fce9faf9 1849 format_char);
1850 else
d1f11193 1851 warning ("unknown conversion type character 0x%x in format",
fce9faf9 1852 format_char);
fce9faf9 1853 continue;
1854 }
d1f11193 1855 if (pedantic)
fce9faf9 1856 {
d1f11193 1857 if (index (fci->flag_chars, 'G') != 0)
1858 warning ("ANSI C does not support `%%%c'", format_char);
1859 if (index (fci->flag_chars, 'o') != 0
1860 && index (flag_chars, 'O') != 0)
1861 warning ("ANSI C does not support `%%O%c'", format_char);
fce9faf9 1862 }
d1f11193 1863 if (wide && index (fci->flag_chars, 'w') == 0)
1864 warning ("width used with `%c' format", format_char);
1865 if (index (fci->flag_chars, '2') != 0)
1866 warning ("`%%%c' yields only last 2 digits of year", format_char);
1867 else if (index (fci->flag_chars, '3') != 0)
1868 warning ("`%%%c' yields only last 2 digits of year in some locales",
1869 format_char);
fce9faf9 1870 if (precise && index (fci->flag_chars, 'p') == 0)
d1f11193 1871 warning ("precision used with `%c' format", format_char);
63b97047 1872 if (aflag && index (fci->flag_chars, 'a') == 0)
1873 {
d1f11193 1874 warning ("`a' flag used with `%c' format", format_char);
d6abc4a6 1875 /* To simplify the following code. */
1876 aflag = 0;
fce9faf9 1877 }
2d47efb6 1878 /* The a flag is a GNU extension. */
1879 else if (pedantic && aflag)
1880 warning ("ANSI C does not support the `a' flag");
d1f11193 1881 if (info->format_type == scanf_format_type && format_char == '[')
fce9faf9 1882 {
1883 /* Skip over scan set, in case it happens to have '%' in it. */
1884 if (*format_chars == '^')
1885 ++format_chars;
1886 /* Find closing bracket; if one is hit immediately, then
1887 it's part of the scan set rather than a terminator. */
1888 if (*format_chars == ']')
1889 ++format_chars;
1890 while (*format_chars && *format_chars != ']')
1891 ++format_chars;
1892 if (*format_chars != ']')
d1f11193 1893 /* The end of the format string was reached. */
1894 warning ("no closing `]' for `%%[' format");
fce9faf9 1895 }
1896 if (suppressed)
1897 {
1898 if (index (fci->flag_chars, '*') == 0)
d1f11193 1899 warning ("suppression of `%c' conversion in format", format_char);
fce9faf9 1900 continue;
1901 }
1902 for (i = 0; flag_chars[i] != 0; ++i)
1903 {
1904 if (index (fci->flag_chars, flag_chars[i]) == 0)
d1f11193 1905 warning ("flag `%c' used with type `%c'",
1906 flag_chars[i], format_char);
fce9faf9 1907 }
d1f11193 1908 if (info->format_type == strftime_format_type)
1909 continue;
c5aa1e92 1910 if (precise && index (flag_chars, '0') != 0
1911 && (format_char == 'd' || format_char == 'i'
1912 || format_char == 'o' || format_char == 'u'
c7bdb2f6 1913 || format_char == 'x' || format_char == 'X'))
d1f11193 1914 warning ("`0' flag ignored with precision specifier and `%c' format",
1915 format_char);
fce9faf9 1916 switch (length_char)
1917 {
1918 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
73be5127 1919 case 'H': wanted_type = fci->hhlen ? *(fci->hhlen) : 0; break;
fce9faf9 1920 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1921 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
93fccc0a 1922 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
fce9faf9 1923 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
44e9fa65 1924 case 'z': case 'Z': wanted_type = fci->zlen ? *fci->zlen : 0; break;
fce9faf9 1925 }
c5aa1e92 1926 if (wanted_type == 0)
047a721c 1927 warning ("use of `%c' length character with `%c' type character",
1928 length_char, format_char);
fce9faf9 1929
fce9faf9 1930 /* Finally. . .check type of argument against desired type! */
1931 if (info->first_arg_num == 0)
1932 continue;
2a287891 1933 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1934 /* This specifier takes no argument. */
1935 continue;
fce9faf9 1936 if (params == 0)
1937 {
be2828ce 1938 tfaff ();
fce9faf9 1939 return;
1940 }
1941 cur_param = TREE_VALUE (params);
1942 params = TREE_CHAIN (params);
1943 ++arg_num;
1944 cur_type = TREE_TYPE (cur_param);
1945
31f820d2 1946 STRIP_NOPS (cur_param);
1947
fce9faf9 1948 /* Check the types of any additional pointer arguments
1949 that precede the "real" argument. */
d6abc4a6 1950 for (i = 0; i < fci->pointer_count + aflag; ++i)
fce9faf9 1951 {
1952 if (TREE_CODE (cur_type) == POINTER_TYPE)
1953 {
1954 cur_type = TREE_TYPE (cur_type);
31f820d2 1955
c5aa1e92 1956 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
31f820d2 1957 cur_param = TREE_OPERAND (cur_param, 0);
1958 else
1959 cur_param = 0;
1960
fce9faf9 1961 continue;
1962 }
309f04fa 1963 if (TREE_CODE (cur_type) != ERROR_MARK)
e4ba8ded 1964 {
1965 if (fci->pointer_count + aflag == 1)
1966 warning ("format argument is not a pointer (arg %d)", arg_num);
1967 else
1968 warning ("format argument is not a pointer to a pointer (arg %d)", arg_num);
1969 }
fce9faf9 1970 break;
1971 }
1972
31f820d2 1973 /* See if this is an attempt to write into a const type with
33b8b501 1974 scanf or with printf "%n". */
1975 if ((info->format_type == scanf_format_type
1976 || (info->format_type == printf_format_type
1977 && format_char == 'n'))
d1f11193 1978 && i == fci->pointer_count + aflag
31f820d2 1979 && wanted_type != 0
1980 && TREE_CODE (cur_type) != ERROR_MARK
1981 && (TYPE_READONLY (cur_type)
1982 || (cur_param != 0
1983 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
1984 || (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'd'
1985 && TREE_READONLY (cur_param))))))
d1f11193 1986 warning ("writing into constant object (arg %d)", arg_num);
31f820d2 1987
fce9faf9 1988 /* Check the type of the "real" argument, if there's a type we want. */
d6abc4a6 1989 if (i == fci->pointer_count + aflag && wanted_type != 0
309f04fa 1990 && TREE_CODE (cur_type) != ERROR_MARK
fce9faf9 1991 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1992 /* If we want `void *', allow any pointer type.
1993 (Anything else would already have got a warning.) */
1994 && ! (wanted_type == void_type_node
1995 && fci->pointer_count > 0)
1996 /* Don't warn about differences merely in signedness. */
1997 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
78dafd61 1998 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
8200cbb2 1999 && (TREE_UNSIGNED (wanted_type)
4ef20085 2000 ? wanted_type == (cur_type = unsigned_type (cur_type))
2001 : wanted_type == (cur_type = signed_type (cur_type))))
641dd458 2002 /* Likewise, "signed char", "unsigned char" and "char" are
2003 equivalent but the above test won't consider them equivalent. */
2004 && ! (wanted_type == char_type_node
78dafd61 2005 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
2006 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
fce9faf9 2007 {
3eee82c5 2008 register const char *this;
2009 register const char *that;
73be5127 2010
fce9faf9 2011 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
2012 that = 0;
2013 if (TREE_CODE (cur_type) != ERROR_MARK
2014 && TYPE_NAME (cur_type) != 0
2015 && TREE_CODE (cur_type) != INTEGER_TYPE
2016 && !(TREE_CODE (cur_type) == POINTER_TYPE
2017 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
2018 {
2019 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
2020 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
2021 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
2022 else
2023 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
2024 }
2025
2026 /* A nameless type can't possibly match what the format wants.
2027 So there will be a warning for it.
2028 Make up a string to describe vaguely what it is. */
2029 if (that == 0)
2030 {
2031 if (TREE_CODE (cur_type) == POINTER_TYPE)
2032 that = "pointer";
2033 else
2034 that = "different type";
2035 }
2036
cbc5b7d9 2037 /* Make the warning better in case of mismatch of int vs long. */
2038 if (TREE_CODE (cur_type) == INTEGER_TYPE
2039 && TREE_CODE (wanted_type) == INTEGER_TYPE
2040 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
2041 && TYPE_NAME (cur_type) != 0
2042 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
2043 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
2044
fce9faf9 2045 if (strcmp (this, that) != 0)
d1f11193 2046 warning ("%s format, %s arg (arg %d)", this, that, arg_num);
fce9faf9 2047 }
2048 }
2049}
2050\f
2a1736ed 2051/* Print a warning if a constant expression had overflow in folding.
2052 Invoke this function on every expression that the language
2053 requires to be a constant expression.
2054 Note the ANSI C standard says it is erroneous for a
2055 constant expression to overflow. */
b2806639 2056
2057void
2058constant_expression_warning (value)
2059 tree value;
2060{
837e1122 2061 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
2062 || TREE_CODE (value) == COMPLEX_CST)
2063 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
2064 pedwarn ("overflow in constant expression");
2a1736ed 2065}
2066
2067/* Print a warning if an expression had overflow in folding.
2068 Invoke this function on every expression that
2069 (1) appears in the source code, and
2070 (2) might be a constant expression that overflowed, and
2071 (3) is not already checked by convert_and_check;
2072 however, do not invoke this function on operands of explicit casts. */
2073
2074void
2075overflow_warning (value)
2076 tree value;
2077{
837e1122 2078 if ((TREE_CODE (value) == INTEGER_CST
2079 || (TREE_CODE (value) == COMPLEX_CST
2080 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
2081 && TREE_OVERFLOW (value))
2a1736ed 2082 {
b04da9b5 2083 TREE_OVERFLOW (value) = 0;
e78703c1 2084 if (skip_evaluation == 0)
2085 warning ("integer overflow in expression");
2a1736ed 2086 }
837e1122 2087 else if ((TREE_CODE (value) == REAL_CST
2088 || (TREE_CODE (value) == COMPLEX_CST
2089 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
2090 && TREE_OVERFLOW (value))
2091 {
2092 TREE_OVERFLOW (value) = 0;
e78703c1 2093 if (skip_evaluation == 0)
2094 warning ("floating point overflow in expression");
837e1122 2095 }
2a1736ed 2096}
2097
2098/* Print a warning if a large constant is truncated to unsigned,
2099 or if -Wconversion is used and a constant < 0 is converted to unsigned.
2100 Invoke this function on every expression that might be implicitly
2101 converted to an unsigned type. */
2102
2103void
2104unsigned_conversion_warning (result, operand)
2105 tree result, operand;
2106{
2107 if (TREE_CODE (operand) == INTEGER_CST
2108 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
2109 && TREE_UNSIGNED (TREE_TYPE (result))
e78703c1 2110 && skip_evaluation == 0
2a1736ed 2111 && !int_fits_type_p (operand, TREE_TYPE (result)))
2112 {
2113 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
2114 /* This detects cases like converting -129 or 256 to unsigned char. */
454b5afb 2115 warning ("large integer implicitly truncated to unsigned type");
2a1736ed 2116 else if (warn_conversion)
454b5afb 2117 warning ("negative integer implicitly converted to unsigned type");
2a1736ed 2118 }
2119}
2120
2121/* Convert EXPR to TYPE, warning about conversion problems with constants.
2122 Invoke this function on every expression that is converted implicitly,
2123 i.e. because of language rules and not because of an explicit cast. */
2124
2125tree
2126convert_and_check (type, expr)
2127 tree type, expr;
2128{
2129 tree t = convert (type, expr);
2130 if (TREE_CODE (t) == INTEGER_CST)
2131 {
b04da9b5 2132 if (TREE_OVERFLOW (t))
2a1736ed 2133 {
b04da9b5 2134 TREE_OVERFLOW (t) = 0;
2135
2c4232f8 2136 /* Do not diagnose overflow in a constant expression merely
2137 because a conversion overflowed. */
2138 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
2139
b04da9b5 2140 /* No warning for converting 0x80000000 to int. */
2141 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
2142 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
2143 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
96730c20 2144 /* If EXPR fits in the unsigned version of TYPE,
2145 don't warn unless pedantic. */
e78703c1 2146 if ((pedantic
2147 || TREE_UNSIGNED (type)
2148 || ! int_fits_type_p (expr, unsigned_type (type)))
2149 && skip_evaluation == 0)
d1f11193 2150 warning ("overflow in implicit constant conversion");
2a1736ed 2151 }
2152 else
2153 unsigned_conversion_warning (t, expr);
2154 }
2155 return t;
b2806639 2156}
2157\f
b0fc3e72 2158void
2159c_expand_expr_stmt (expr)
2160 tree expr;
2161{
2162 /* Do default conversion if safe and possibly important,
2163 in case within ({...}). */
2164 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
2165 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
2166 expr = default_conversion (expr);
2167
2168 if (TREE_TYPE (expr) != error_mark_node
2169 && TYPE_SIZE (TREE_TYPE (expr)) == 0
2170 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
2171 error ("expression statement has incomplete type");
2172
2173 expand_expr_stmt (expr);
2174}
2175\f
2176/* Validate the expression after `case' and apply default promotions. */
2177
2178tree
2179check_case_value (value)
2180 tree value;
2181{
2182 if (value == NULL_TREE)
2183 return value;
2184
2185 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
fce1d6af 2186 STRIP_TYPE_NOPS (value);
b0fc3e72 2187
2188 if (TREE_CODE (value) != INTEGER_CST
2189 && value != error_mark_node)
2190 {
2191 error ("case label does not reduce to an integer constant");
2192 value = error_mark_node;
2193 }
2194 else
2195 /* Promote char or short to int. */
2196 value = default_conversion (value);
2197
6433f1c2 2198 constant_expression_warning (value);
2199
b0fc3e72 2200 return value;
2201}
2202\f
2203/* Return an integer type with BITS bits of precision,
2204 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2205
2206tree
2207type_for_size (bits, unsignedp)
2208 unsigned bits;
2209 int unsignedp;
2210{
46375237 2211 if (bits == TYPE_PRECISION (integer_type_node))
2212 return unsignedp ? unsigned_type_node : integer_type_node;
2213
bacde65a 2214 if (bits == TYPE_PRECISION (signed_char_type_node))
b0fc3e72 2215 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2216
bacde65a 2217 if (bits == TYPE_PRECISION (short_integer_type_node))
b0fc3e72 2218 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2219
bacde65a 2220 if (bits == TYPE_PRECISION (long_integer_type_node))
b0fc3e72 2221 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2222
bacde65a 2223 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b0fc3e72 2224 return (unsignedp ? long_long_unsigned_type_node
2225 : long_long_integer_type_node);
2226
f57fa2ea 2227 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2228 return (unsignedp ? widest_unsigned_literal_type_node
2229 : widest_integer_literal_type_node);
2230
bacde65a 2231 if (bits <= TYPE_PRECISION (intQI_type_node))
2232 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2233
2234 if (bits <= TYPE_PRECISION (intHI_type_node))
2235 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2236
2237 if (bits <= TYPE_PRECISION (intSI_type_node))
2238 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2239
2240 if (bits <= TYPE_PRECISION (intDI_type_node))
2241 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2242
b0fc3e72 2243 return 0;
2244}
2245
2246/* Return a data type that has machine mode MODE.
2247 If the mode is an integer,
2248 then UNSIGNEDP selects between signed and unsigned types. */
2249
2250tree
2251type_for_mode (mode, unsignedp)
2252 enum machine_mode mode;
2253 int unsignedp;
2254{
46375237 2255 if (mode == TYPE_MODE (integer_type_node))
2256 return unsignedp ? unsigned_type_node : integer_type_node;
2257
b0fc3e72 2258 if (mode == TYPE_MODE (signed_char_type_node))
2259 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2260
2261 if (mode == TYPE_MODE (short_integer_type_node))
2262 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2263
b0fc3e72 2264 if (mode == TYPE_MODE (long_integer_type_node))
2265 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2266
2267 if (mode == TYPE_MODE (long_long_integer_type_node))
2268 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2269
f57fa2ea 2270 if (mode == TYPE_MODE (widest_integer_literal_type_node))
44e9fa65 2271 return unsignedp ? widest_unsigned_literal_type_node
f57fa2ea 2272 : widest_integer_literal_type_node;
2273
bacde65a 2274 if (mode == TYPE_MODE (intQI_type_node))
2275 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2276
2277 if (mode == TYPE_MODE (intHI_type_node))
2278 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2279
2280 if (mode == TYPE_MODE (intSI_type_node))
2281 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2282
2283 if (mode == TYPE_MODE (intDI_type_node))
2284 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2285
cc1cc1c7 2286#if HOST_BITS_PER_WIDE_INT >= 64
6274009c 2287 if (mode == TYPE_MODE (intTI_type_node))
2288 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
cc1cc1c7 2289#endif
6274009c 2290
b0fc3e72 2291 if (mode == TYPE_MODE (float_type_node))
2292 return float_type_node;
2293
2294 if (mode == TYPE_MODE (double_type_node))
2295 return double_type_node;
2296
2297 if (mode == TYPE_MODE (long_double_type_node))
2298 return long_double_type_node;
2299
2300 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2301 return build_pointer_type (char_type_node);
2302
2303 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2304 return build_pointer_type (integer_type_node);
2305
2306 return 0;
2307}
20d39783 2308
2309/* Return an unsigned type the same as TYPE in other respects. */
2310tree
2311unsigned_type (type)
2312 tree type;
2313{
2314 tree type1 = TYPE_MAIN_VARIANT (type);
2315 if (type1 == signed_char_type_node || type1 == char_type_node)
2316 return unsigned_char_type_node;
2317 if (type1 == integer_type_node)
2318 return unsigned_type_node;
2319 if (type1 == short_integer_type_node)
2320 return short_unsigned_type_node;
2321 if (type1 == long_integer_type_node)
2322 return long_unsigned_type_node;
2323 if (type1 == long_long_integer_type_node)
2324 return long_long_unsigned_type_node;
2325 if (type1 == widest_integer_literal_type_node)
2326 return widest_unsigned_literal_type_node;
2327#if HOST_BITS_PER_WIDE_INT >= 64
2328 if (type1 == intTI_type_node)
2329 return unsigned_intTI_type_node;
2330#endif
2331 if (type1 == intDI_type_node)
2332 return unsigned_intDI_type_node;
2333 if (type1 == intSI_type_node)
2334 return unsigned_intSI_type_node;
2335 if (type1 == intHI_type_node)
2336 return unsigned_intHI_type_node;
2337 if (type1 == intQI_type_node)
2338 return unsigned_intQI_type_node;
2339
2340 return signed_or_unsigned_type (1, type);
2341}
2342
2343/* Return a signed type the same as TYPE in other respects. */
2344
2345tree
2346signed_type (type)
2347 tree type;
2348{
2349 tree type1 = TYPE_MAIN_VARIANT (type);
2350 if (type1 == unsigned_char_type_node || type1 == char_type_node)
2351 return signed_char_type_node;
2352 if (type1 == unsigned_type_node)
2353 return integer_type_node;
2354 if (type1 == short_unsigned_type_node)
2355 return short_integer_type_node;
2356 if (type1 == long_unsigned_type_node)
2357 return long_integer_type_node;
2358 if (type1 == long_long_unsigned_type_node)
2359 return long_long_integer_type_node;
2360 if (type1 == widest_unsigned_literal_type_node)
2361 return widest_integer_literal_type_node;
2362#if HOST_BITS_PER_WIDE_INT >= 64
2363 if (type1 == unsigned_intTI_type_node)
2364 return intTI_type_node;
2365#endif
2366 if (type1 == unsigned_intDI_type_node)
2367 return intDI_type_node;
2368 if (type1 == unsigned_intSI_type_node)
2369 return intSI_type_node;
2370 if (type1 == unsigned_intHI_type_node)
2371 return intHI_type_node;
2372 if (type1 == unsigned_intQI_type_node)
2373 return intQI_type_node;
2374
2375 return signed_or_unsigned_type (0, type);
2376}
2377
2378/* Return a type the same as TYPE except unsigned or
2379 signed according to UNSIGNEDP. */
2380
2381tree
2382signed_or_unsigned_type (unsignedp, type)
2383 int unsignedp;
2384 tree type;
2385{
2386 if (! INTEGRAL_TYPE_P (type)
2387 || TREE_UNSIGNED (type) == unsignedp)
2388 return type;
2389
2390 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
2391 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
44e9fa65 2392 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
20d39783 2393 return unsignedp ? unsigned_type_node : integer_type_node;
44e9fa65 2394 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
20d39783 2395 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
44e9fa65 2396 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
20d39783 2397 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
44e9fa65 2398 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
20d39783 2399 return (unsignedp ? long_long_unsigned_type_node
2400 : long_long_integer_type_node);
44e9fa65 2401 if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
20d39783 2402 return (unsignedp ? widest_unsigned_literal_type_node
2403 : widest_integer_literal_type_node);
2404 return type;
2405}
b0fc3e72 2406\f
a9b9d10c 2407/* Return the minimum number of bits needed to represent VALUE in a
2408 signed or unsigned type, UNSIGNEDP says which. */
2409
2410int
2411min_precision (value, unsignedp)
2412 tree value;
2413 int unsignedp;
2414{
2415 int log;
2416
2417 /* If the value is negative, compute its negative minus 1. The latter
2418 adjustment is because the absolute value of the largest negative value
2419 is one larger than the largest positive value. This is equivalent to
2420 a bit-wise negation, so use that operation instead. */
2421
2422 if (tree_int_cst_sgn (value) < 0)
2423 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2424
2425 /* Return the number of bits needed, taking into account the fact
2426 that we need one more bit for a signed than unsigned type. */
2427
2428 if (integer_zerop (value))
2429 log = 0;
2430 else if (TREE_INT_CST_HIGH (value) != 0)
2431 log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
2432 else
2433 log = floor_log2 (TREE_INT_CST_LOW (value));
2434
2435 return log + 1 + ! unsignedp;
2436}
2437\f
b0fc3e72 2438/* Print an error message for invalid operands to arith operation CODE.
2439 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2440
2441void
2442binary_op_error (code)
2443 enum tree_code code;
2444{
3eee82c5 2445 register const char *opname;
f03946e4 2446
b0fc3e72 2447 switch (code)
2448 {
2449 case NOP_EXPR:
2450 error ("invalid truth-value expression");
2451 return;
2452
2453 case PLUS_EXPR:
2454 opname = "+"; break;
2455 case MINUS_EXPR:
2456 opname = "-"; break;
2457 case MULT_EXPR:
2458 opname = "*"; break;
2459 case MAX_EXPR:
2460 opname = "max"; break;
2461 case MIN_EXPR:
2462 opname = "min"; break;
2463 case EQ_EXPR:
2464 opname = "=="; break;
2465 case NE_EXPR:
2466 opname = "!="; break;
2467 case LE_EXPR:
2468 opname = "<="; break;
2469 case GE_EXPR:
2470 opname = ">="; break;
2471 case LT_EXPR:
2472 opname = "<"; break;
2473 case GT_EXPR:
2474 opname = ">"; break;
2475 case LSHIFT_EXPR:
2476 opname = "<<"; break;
2477 case RSHIFT_EXPR:
2478 opname = ">>"; break;
2479 case TRUNC_MOD_EXPR:
66618a1e 2480 case FLOOR_MOD_EXPR:
b0fc3e72 2481 opname = "%"; break;
2482 case TRUNC_DIV_EXPR:
66618a1e 2483 case FLOOR_DIV_EXPR:
b0fc3e72 2484 opname = "/"; break;
2485 case BIT_AND_EXPR:
2486 opname = "&"; break;
2487 case BIT_IOR_EXPR:
2488 opname = "|"; break;
2489 case TRUTH_ANDIF_EXPR:
2490 opname = "&&"; break;
2491 case TRUTH_ORIF_EXPR:
2492 opname = "||"; break;
2493 case BIT_XOR_EXPR:
2494 opname = "^"; break;
66618a1e 2495 case LROTATE_EXPR:
2496 case RROTATE_EXPR:
2497 opname = "rotate"; break;
31f820d2 2498 default:
2499 opname = "unknown"; break;
b0fc3e72 2500 }
2501 error ("invalid operands to binary %s", opname);
2502}
2503\f
2504/* Subroutine of build_binary_op, used for comparison operations.
2505 See if the operands have both been converted from subword integer types
2506 and, if so, perhaps change them both back to their original type.
5b511807 2507 This function is also responsible for converting the two operands
2508 to the proper common type for comparison.
b0fc3e72 2509
2510 The arguments of this function are all pointers to local variables
2511 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2512 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2513
2514 If this function returns nonzero, it means that the comparison has
2515 a constant value. What this function returns is an expression for
2516 that value. */
2517
2518tree
2519shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2520 tree *op0_ptr, *op1_ptr;
2521 tree *restype_ptr;
2522 enum tree_code *rescode_ptr;
2523{
2524 register tree type;
2525 tree op0 = *op0_ptr;
2526 tree op1 = *op1_ptr;
2527 int unsignedp0, unsignedp1;
2528 int real1, real2;
2529 tree primop0, primop1;
2530 enum tree_code code = *rescode_ptr;
2531
2532 /* Throw away any conversions to wider types
2533 already present in the operands. */
2534
2535 primop0 = get_narrower (op0, &unsignedp0);
2536 primop1 = get_narrower (op1, &unsignedp1);
2537
2538 /* Handle the case that OP0 does not *contain* a conversion
2539 but it *requires* conversion to FINAL_TYPE. */
2540
2541 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2542 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2543 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2544 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2545
2546 /* If one of the operands must be floated, we cannot optimize. */
2547 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2548 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2549
2550 /* If first arg is constant, swap the args (changing operation
2bd278cc 2551 so value is preserved), for canonicalization. Don't do this if
2552 the second arg is 0. */
b0fc3e72 2553
2bd278cc 2554 if (TREE_CONSTANT (primop0)
2555 && ! integer_zerop (primop1) && ! real_zerop (primop1))
b0fc3e72 2556 {
2557 register tree tem = primop0;
2558 register int temi = unsignedp0;
2559 primop0 = primop1;
2560 primop1 = tem;
2561 tem = op0;
2562 op0 = op1;
2563 op1 = tem;
2564 *op0_ptr = op0;
2565 *op1_ptr = op1;
2566 unsignedp0 = unsignedp1;
2567 unsignedp1 = temi;
2568 temi = real1;
2569 real1 = real2;
2570 real2 = temi;
2571
2572 switch (code)
2573 {
2574 case LT_EXPR:
2575 code = GT_EXPR;
2576 break;
2577 case GT_EXPR:
2578 code = LT_EXPR;
2579 break;
2580 case LE_EXPR:
2581 code = GE_EXPR;
2582 break;
2583 case GE_EXPR:
2584 code = LE_EXPR;
2585 break;
31f820d2 2586 default:
2587 break;
b0fc3e72 2588 }
2589 *rescode_ptr = code;
2590 }
2591
2592 /* If comparing an integer against a constant more bits wide,
2593 maybe we can deduce a value of 1 or 0 independent of the data.
2594 Or else truncate the constant now
2595 rather than extend the variable at run time.
2596
2597 This is only interesting if the constant is the wider arg.
2598 Also, it is not safe if the constant is unsigned and the
2599 variable arg is signed, since in this case the variable
2600 would be sign-extended and then regarded as unsigned.
2601 Our technique fails in this case because the lowest/highest
2602 possible unsigned results don't follow naturally from the
2603 lowest/highest possible values of the variable operand.
2604 For just EQ_EXPR and NE_EXPR there is another technique that
2605 could be used: see if the constant can be faithfully represented
2606 in the other operand's type, by truncating it and reextending it
2607 and see if that preserves the constant's value. */
2608
2609 if (!real1 && !real2
2610 && TREE_CODE (primop1) == INTEGER_CST
2611 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2612 {
2613 int min_gt, max_gt, min_lt, max_lt;
2614 tree maxval, minval;
2615 /* 1 if comparison is nominally unsigned. */
2616 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2617 tree val;
2618
2619 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
cda09c61 2620
2621 /* If TYPE is an enumeration, then we need to get its min/max
2622 values from it's underlying integral type, not the enumerated
2623 type itself. */
2624 if (TREE_CODE (type) == ENUMERAL_TYPE)
2625 type = type_for_size (TYPE_PRECISION (type), unsignedp0);
b0fc3e72 2626
2627 maxval = TYPE_MAX_VALUE (type);
2628 minval = TYPE_MIN_VALUE (type);
2629
2630 if (unsignedp && !unsignedp0)
2631 *restype_ptr = signed_type (*restype_ptr);
2632
2633 if (TREE_TYPE (primop1) != *restype_ptr)
2634 primop1 = convert (*restype_ptr, primop1);
2635 if (type != *restype_ptr)
2636 {
2637 minval = convert (*restype_ptr, minval);
2638 maxval = convert (*restype_ptr, maxval);
2639 }
2640
2641 if (unsignedp && unsignedp0)
2642 {
2643 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2644 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2645 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2646 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2647 }
2648 else
2649 {
2650 min_gt = INT_CST_LT (primop1, minval);
2651 max_gt = INT_CST_LT (primop1, maxval);
2652 min_lt = INT_CST_LT (minval, primop1);
2653 max_lt = INT_CST_LT (maxval, primop1);
2654 }
2655
2656 val = 0;
2657 /* This used to be a switch, but Genix compiler can't handle that. */
2658 if (code == NE_EXPR)
2659 {
2660 if (max_lt || min_gt)
8a7e5d0d 2661 val = boolean_true_node;
b0fc3e72 2662 }
2663 else if (code == EQ_EXPR)
2664 {
2665 if (max_lt || min_gt)
8a7e5d0d 2666 val = boolean_false_node;
b0fc3e72 2667 }
2668 else if (code == LT_EXPR)
2669 {
2670 if (max_lt)
8a7e5d0d 2671 val = boolean_true_node;
b0fc3e72 2672 if (!min_lt)
8a7e5d0d 2673 val = boolean_false_node;
b0fc3e72 2674 }
2675 else if (code == GT_EXPR)
2676 {
2677 if (min_gt)
8a7e5d0d 2678 val = boolean_true_node;
b0fc3e72 2679 if (!max_gt)
8a7e5d0d 2680 val = boolean_false_node;
b0fc3e72 2681 }
2682 else if (code == LE_EXPR)
2683 {
2684 if (!max_gt)
8a7e5d0d 2685 val = boolean_true_node;
b0fc3e72 2686 if (min_gt)
8a7e5d0d 2687 val = boolean_false_node;
b0fc3e72 2688 }
2689 else if (code == GE_EXPR)
2690 {
2691 if (!min_lt)
8a7e5d0d 2692 val = boolean_true_node;
b0fc3e72 2693 if (max_lt)
8a7e5d0d 2694 val = boolean_false_node;
b0fc3e72 2695 }
2696
2697 /* If primop0 was sign-extended and unsigned comparison specd,
2698 we did a signed comparison above using the signed type bounds.
2699 But the comparison we output must be unsigned.
2700
2701 Also, for inequalities, VAL is no good; but if the signed
2702 comparison had *any* fixed result, it follows that the
2703 unsigned comparison just tests the sign in reverse
2704 (positive values are LE, negative ones GE).
2705 So we can generate an unsigned comparison
2706 against an extreme value of the signed type. */
2707
2708 if (unsignedp && !unsignedp0)
2709 {
2710 if (val != 0)
2711 switch (code)
2712 {
2713 case LT_EXPR:
2714 case GE_EXPR:
2715 primop1 = TYPE_MIN_VALUE (type);
2716 val = 0;
2717 break;
2718
2719 case LE_EXPR:
2720 case GT_EXPR:
2721 primop1 = TYPE_MAX_VALUE (type);
2722 val = 0;
2723 break;
31f820d2 2724
2725 default:
2726 break;
b0fc3e72 2727 }
2728 type = unsigned_type (type);
2729 }
2730
78dafd61 2731 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
b0fc3e72 2732 {
2733 /* This is the case of (char)x >?< 0x80, which people used to use
2734 expecting old C compilers to change the 0x80 into -0x80. */
8a7e5d0d 2735 if (val == boolean_false_node)
9a64a879 2736 warning ("comparison is always false due to limited range of data type");
8a7e5d0d 2737 if (val == boolean_true_node)
9a64a879 2738 warning ("comparison is always true due to limited range of data type");
b0fc3e72 2739 }
2740
78dafd61 2741 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
b0fc3e72 2742 {
a693ee7d 2743 /* This is the case of (unsigned char)x >?< -1 or < 0. */
8a7e5d0d 2744 if (val == boolean_false_node)
9a64a879 2745 warning ("comparison is always false due to limited range of data type");
8a7e5d0d 2746 if (val == boolean_true_node)
9a64a879 2747 warning ("comparison is always true due to limited range of data type");
b0fc3e72 2748 }
2749
2750 if (val != 0)
2751 {
2752 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2753 if (TREE_SIDE_EFFECTS (primop0))
2754 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2755 return val;
2756 }
2757
2758 /* Value is not predetermined, but do the comparison
2759 in the type of the operand that is not constant.
2760 TYPE is already properly set. */
2761 }
2762 else if (real1 && real2
2203bd5c 2763 && (TYPE_PRECISION (TREE_TYPE (primop0))
2764 == TYPE_PRECISION (TREE_TYPE (primop1))))
b0fc3e72 2765 type = TREE_TYPE (primop0);
2766
2767 /* If args' natural types are both narrower than nominal type
2768 and both extend in the same manner, compare them
2769 in the type of the wider arg.
2770 Otherwise must actually extend both to the nominal
2771 common type lest different ways of extending
2772 alter the result.
2773 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2774
2775 else if (unsignedp0 == unsignedp1 && real1 == real2
2776 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2777 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2778 {
2779 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2780 type = signed_or_unsigned_type (unsignedp0
2781 || TREE_UNSIGNED (*restype_ptr),
2782 type);
2783 /* Make sure shorter operand is extended the right way
2784 to match the longer operand. */
2785 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2786 primop0);
2787 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2788 primop1);
2789 }
2790 else
2791 {
2792 /* Here we must do the comparison on the nominal type
2793 using the args exactly as we received them. */
2794 type = *restype_ptr;
2795 primop0 = op0;
2796 primop1 = op1;
2797
2798 if (!real1 && !real2 && integer_zerop (primop1)
32ab5039 2799 && TREE_UNSIGNED (*restype_ptr))
b0fc3e72 2800 {
2801 tree value = 0;
2802 switch (code)
2803 {
2804 case GE_EXPR:
2bd278cc 2805 /* All unsigned values are >= 0, so we warn if extra warnings
2806 are requested. However, if OP0 is a constant that is
2807 >= 0, the signedness of the comparison isn't an issue,
2808 so suppress the warning. */
2809 if (extra_warnings
2810 && ! (TREE_CODE (primop0) == INTEGER_CST
2811 && ! TREE_OVERFLOW (convert (signed_type (type),
2812 primop0))))
9a64a879 2813 warning ("comparison of unsigned expression >= 0 is always true");
8a7e5d0d 2814 value = boolean_true_node;
b0fc3e72 2815 break;
2816
2817 case LT_EXPR:
2bd278cc 2818 if (extra_warnings
2819 && ! (TREE_CODE (primop0) == INTEGER_CST
2820 && ! TREE_OVERFLOW (convert (signed_type (type),
2821 primop0))))
9a64a879 2822 warning ("comparison of unsigned expression < 0 is always false");
8a7e5d0d 2823 value = boolean_false_node;
31f820d2 2824 break;
2825
2826 default:
2827 break;
b0fc3e72 2828 }
2829
2830 if (value != 0)
2831 {
2832 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2833 if (TREE_SIDE_EFFECTS (primop0))
2834 return build (COMPOUND_EXPR, TREE_TYPE (value),
2835 primop0, value);
2836 return value;
2837 }
2838 }
2839 }
2840
2841 *op0_ptr = convert (type, primop0);
2842 *op1_ptr = convert (type, primop1);
2843
8a7e5d0d 2844 *restype_ptr = boolean_type_node;
b0fc3e72 2845
2846 return 0;
2847}
2848\f
2849/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2850 or validate its data type for an `if' or `while' statement or ?..: exp.
2851
2852 This preparation consists of taking the ordinary
2853 representation of an expression expr and producing a valid tree
2854 boolean expression describing whether expr is nonzero. We could
8a7e5d0d 2855 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
b0fc3e72 2856 but we optimize comparisons, &&, ||, and !.
2857
8a7e5d0d 2858 The resulting type should always be `boolean_type_node'. */
b0fc3e72 2859
2860tree
2861truthvalue_conversion (expr)
2862 tree expr;
2863{
baa1f4f5 2864 if (TREE_CODE (expr) == ERROR_MARK)
2865 return expr;
2866
a70adbe5 2867#if 0 /* This appears to be wrong for C++. */
baa1f4f5 2868 /* These really should return error_mark_node after 2.4 is stable.
2869 But not all callers handle ERROR_MARK properly. */
2870 switch (TREE_CODE (TREE_TYPE (expr)))
2871 {
2872 case RECORD_TYPE:
2873 error ("struct type value used where scalar is required");
8a7e5d0d 2874 return boolean_false_node;
baa1f4f5 2875
2876 case UNION_TYPE:
2877 error ("union type value used where scalar is required");
8a7e5d0d 2878 return boolean_false_node;
baa1f4f5 2879
2880 case ARRAY_TYPE:
2881 error ("array type value used where scalar is required");
8a7e5d0d 2882 return boolean_false_node;
baa1f4f5 2883
2884 default:
2885 break;
2886 }
a70adbe5 2887#endif /* 0 */
baa1f4f5 2888
b0fc3e72 2889 switch (TREE_CODE (expr))
2890 {
2891 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2892 or comparison expressions as truth values at this level. */
2893#if 0
2894 case COMPONENT_REF:
2895 /* A one-bit unsigned bit-field is already acceptable. */
2896 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
2897 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
2898 return expr;
2899 break;
2900#endif
2901
2902 case EQ_EXPR:
2903 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2904 or comparison expressions as truth values at this level. */
2905#if 0
2906 if (integer_zerop (TREE_OPERAND (expr, 1)))
2907 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
2908#endif
2909 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2910 case TRUTH_ANDIF_EXPR:
2911 case TRUTH_ORIF_EXPR:
2912 case TRUTH_AND_EXPR:
2913 case TRUTH_OR_EXPR:
31f6e93c 2914 case TRUTH_XOR_EXPR:
7bbc42b5 2915 case TRUTH_NOT_EXPR:
8a7e5d0d 2916 TREE_TYPE (expr) = boolean_type_node;
2917 return expr;
3e851b85 2918
b0fc3e72 2919 case ERROR_MARK:
2920 return expr;
2921
2922 case INTEGER_CST:
8a7e5d0d 2923 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
b0fc3e72 2924
2925 case REAL_CST:
8a7e5d0d 2926 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
b0fc3e72 2927
2928 case ADDR_EXPR:
65b5e6a6 2929 /* If we are taking the address of a external decl, it might be zero
2930 if it is weak, so we cannot optimize. */
2931 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr, 0))) == 'd'
2932 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2933 break;
2934
b0fc3e72 2935 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
8a7e5d0d 2936 return build (COMPOUND_EXPR, boolean_type_node,
2937 TREE_OPERAND (expr, 0), boolean_true_node);
b0fc3e72 2938 else
8a7e5d0d 2939 return boolean_true_node;
b0fc3e72 2940
2203bd5c 2941 case COMPLEX_EXPR:
2ba726d2 2942 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
95809de4 2943 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2ba726d2 2944 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2945 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2203bd5c 2946 0);
2947
b0fc3e72 2948 case NEGATE_EXPR:
2949 case ABS_EXPR:
2950 case FLOAT_EXPR:
2951 case FFS_EXPR:
2952 /* These don't change whether an object is non-zero or zero. */
2953 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2954
2955 case LROTATE_EXPR:
2956 case RROTATE_EXPR:
2957 /* These don't change whether an object is zero or non-zero, but
2958 we can't ignore them if their second arg has side-effects. */
2959 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
8a7e5d0d 2960 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
b0fc3e72 2961 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2962 else
2963 return truthvalue_conversion (TREE_OPERAND (expr, 0));
73be5127 2964
b0fc3e72 2965 case COND_EXPR:
2966 /* Distribute the conversion into the arms of a COND_EXPR. */
8a7e5d0d 2967 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
b0fc3e72 2968 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2969 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2970
2971 case CONVERT_EXPR:
2972 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2973 since that affects how `default_conversion' will behave. */
2974 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2975 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2976 break;
a92771b8 2977 /* fall through... */
b0fc3e72 2978 case NOP_EXPR:
2979 /* If this is widening the argument, we can ignore it. */
2980 if (TYPE_PRECISION (TREE_TYPE (expr))
2981 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2982 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2983 break;
2984
b0fc3e72 2985 case MINUS_EXPR:
fe0a0255 2986 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2987 this case. */
2988 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2989 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2990 break;
a92771b8 2991 /* fall through... */
fe0a0255 2992 case BIT_XOR_EXPR:
a70adbe5 2993 /* This and MINUS_EXPR can be changed into a comparison of the
fe0a0255 2994 two objects. */
b0fc3e72 2995 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2996 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2997 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2998 TREE_OPERAND (expr, 1), 1);
2999 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
3000 fold (build1 (NOP_EXPR,
3001 TREE_TYPE (TREE_OPERAND (expr, 0)),
3002 TREE_OPERAND (expr, 1))), 1);
16837b18 3003
c2edbca4 3004 case BIT_AND_EXPR:
cc4c7eaf 3005 if (integer_onep (TREE_OPERAND (expr, 1))
3006 && TREE_TYPE (expr) != boolean_type_node)
3007 /* Using convert here would cause infinite recursion. */
3008 return build1 (NOP_EXPR, boolean_type_node, expr);
3009 break;
c2edbca4 3010
16837b18 3011 case MODIFY_EXPR:
3012 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
3013 warning ("suggest parentheses around assignment used as truth value");
3014 break;
73be5127 3015
31f820d2 3016 default:
3017 break;
b0fc3e72 3018 }
3019
2ba726d2 3020 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
a0748b7d 3021 {
3022 tree tem = save_expr (expr);
3023 return (build_binary_op
3024 ((TREE_SIDE_EFFECTS (expr)
3025 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3026 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
3027 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
3028 0));
3029 }
2ba726d2 3030
b0fc3e72 3031 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
3032}
3033\f
a654e028 3034#if USE_CPPLIB
3035/* Read the rest of a #-directive from input stream FINPUT.
3036 In normal use, the directive name and the white space after it
3037 have already been read, so they won't be included in the result.
3038 We allow for the fact that the directive line may contain
3039 a newline embedded within a character or string literal which forms
3040 a part of the directive.
3041
3042 The value is a string in a reusable buffer. It remains valid
3043 only until the next time this function is called. */
3044unsigned char *yy_cur, *yy_lim;
3045
3046#define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
c84cd4c4 3047#define UNGETC(c) ((c) == EOF ? 0 : yy_cur--)
a654e028 3048
3049int
3050yy_get_token ()
3051{
3052 for (;;)
3053 {
3054 parse_in.limit = parse_in.token_buffer;
3055 cpp_token = cpp_get_token (&parse_in);
3056 if (cpp_token == CPP_EOF)
3057 return -1;
3058 yy_lim = CPP_PWRITTEN (&parse_in);
3059 yy_cur = parse_in.token_buffer;
3060 if (yy_cur < yy_lim)
3061 return *yy_cur++;
3062 }
3063}
3064
3065char *
3066get_directive_line ()
3067{
3068 static char *directive_buffer = NULL;
3069 static unsigned buffer_length = 0;
3070 register char *p;
3071 register char *buffer_limit;
3072 register int looking_for = 0;
3073 register int char_escaped = 0;
3074
3075 if (buffer_length == 0)
3076 {
3077 directive_buffer = (char *)xmalloc (128);
3078 buffer_length = 128;
3079 }
3080
3081 buffer_limit = &directive_buffer[buffer_length];
3082
3083 for (p = directive_buffer; ; )
3084 {
3085 int c;
3086
3087 /* Make buffer bigger if it is full. */
3088 if (p >= buffer_limit)
3089 {
3090 register unsigned bytes_used = (p - directive_buffer);
3091
3092 buffer_length *= 2;
3093 directive_buffer
3094 = (char *)xrealloc (directive_buffer, buffer_length);
3095 p = &directive_buffer[bytes_used];
3096 buffer_limit = &directive_buffer[buffer_length];
3097 }
3098
3099 c = GETC ();
3100
3101 /* Discard initial whitespace. */
3102 if ((c == ' ' || c == '\t') && p == directive_buffer)
3103 continue;
3104
3105 /* Detect the end of the directive. */
3106 if (c == '\n' && looking_for == 0)
3107 {
3108 UNGETC (c);
3109 c = '\0';
3110 }
3111
3112 *p++ = c;
3113
3114 if (c == 0)
3115 return directive_buffer;
3116
3117 /* Handle string and character constant syntax. */
3118 if (looking_for)
3119 {
3120 if (looking_for == c && !char_escaped)
3121 looking_for = 0; /* Found terminator... stop looking. */
3122 }
3123 else
3124 if (c == '\'' || c == '"')
3125 looking_for = c; /* Don't stop buffering until we see another
3126 another one of these (or an EOF). */
3127
3128 /* Handle backslash. */
3129 char_escaped = (c == '\\' && ! char_escaped);
3130 }
3131}
3132#else
b0fc3e72 3133/* Read the rest of a #-directive from input stream FINPUT.
3134 In normal use, the directive name and the white space after it
3135 have already been read, so they won't be included in the result.
3136 We allow for the fact that the directive line may contain
3137 a newline embedded within a character or string literal which forms
3138 a part of the directive.
3139
3140 The value is a string in a reusable buffer. It remains valid
b97b38c0 3141 only until the next time this function is called.
3142
3143 The terminating character ('\n' or EOF) is left in FINPUT for the
3144 caller to re-read. */
b0fc3e72 3145
3146char *
3147get_directive_line (finput)
3148 register FILE *finput;
3149{
3150 static char *directive_buffer = NULL;
3151 static unsigned buffer_length = 0;
3152 register char *p;
3153 register char *buffer_limit;
3154 register int looking_for = 0;
3155 register int char_escaped = 0;
3156
3157 if (buffer_length == 0)
3158 {
3159 directive_buffer = (char *)xmalloc (128);
3160 buffer_length = 128;
3161 }
3162
3163 buffer_limit = &directive_buffer[buffer_length];
3164
3165 for (p = directive_buffer; ; )
3166 {
3167 int c;
3168
3169 /* Make buffer bigger if it is full. */
3170 if (p >= buffer_limit)
3171 {
3172 register unsigned bytes_used = (p - directive_buffer);
3173
3174 buffer_length *= 2;
3175 directive_buffer
3176 = (char *)xrealloc (directive_buffer, buffer_length);
3177 p = &directive_buffer[bytes_used];
3178 buffer_limit = &directive_buffer[buffer_length];
3179 }
3180
3181 c = getc (finput);
3182
3183 /* Discard initial whitespace. */
3184 if ((c == ' ' || c == '\t') && p == directive_buffer)
3185 continue;
3186
3187 /* Detect the end of the directive. */
b97b38c0 3188 if (looking_for == 0
3189 && (c == '\n' || c == EOF))
b0fc3e72 3190 {
3191 ungetc (c, finput);
3192 c = '\0';
3193 }
3194
3195 *p++ = c;
3196
3197 if (c == 0)
3198 return directive_buffer;
3199
3200 /* Handle string and character constant syntax. */
3201 if (looking_for)
3202 {
3203 if (looking_for == c && !char_escaped)
3204 looking_for = 0; /* Found terminator... stop looking. */
3205 }
3206 else
3207 if (c == '\'' || c == '"')
3208 looking_for = c; /* Don't stop buffering until we see another
3398e91d 3209 one of these (or an EOF). */
b0fc3e72 3210
3211 /* Handle backslash. */
3212 char_escaped = (c == '\\' && ! char_escaped);
3213 }
3214}
a654e028 3215#endif /* !USE_CPPLIB */
ceee5ef4 3216\f
3217/* Make a variant type in the proper way for C/C++, propagating qualifiers
3218 down to the element type of an array. */
3219
3220tree
a5b1863e 3221c_build_qualified_type (type, type_quals)
ceee5ef4 3222 tree type;
a5b1863e 3223 int type_quals;
ceee5ef4 3224{
a5b1863e 3225 /* A restrict-qualified pointer type must be a pointer to object or
3226 incomplete type. Note that the use of POINTER_TYPE_P also allows
3227 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
3228 the C++ front-end also use POINTER_TYPE for pointer-to-member
3229 values, so even though it should be illegal to use `restrict'
3230 with such an entity we don't flag that here. Thus, special case
3231 code for that case is required in the C++ front-end. */
3232 if ((type_quals & TYPE_QUAL_RESTRICT)
3233 && (!POINTER_TYPE_P (type)
3234 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
3235 {
3236 error ("invalid use of `restrict'");
3237 type_quals &= ~TYPE_QUAL_RESTRICT;
3238 }
3239
ceee5ef4 3240 if (TREE_CODE (type) == ARRAY_TYPE)
a5b1863e 3241 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
3242 type_quals),
754d8048 3243 TYPE_DOMAIN (type));
a5b1863e 3244 return build_qualified_type (type, type_quals);
3245}
3246
3247/* Apply the TYPE_QUALS to the new DECL. */
3248
3249void
3250c_apply_type_quals_to_decl (type_quals, decl)
3251 int type_quals;
3252 tree decl;
3253{
3254 if (type_quals & TYPE_QUAL_CONST)
3255 TREE_READONLY (decl) = 1;
3256 if (type_quals & TYPE_QUAL_VOLATILE)
3257 {
3258 TREE_SIDE_EFFECTS (decl) = 1;
3259 TREE_THIS_VOLATILE (decl) = 1;
3260 }
d91a20bc 3261 if (type_quals & TYPE_QUAL_RESTRICT)
a5b1863e 3262 {
d91a20bc 3263 if (!TREE_TYPE (decl)
3264 || !POINTER_TYPE_P (TREE_TYPE (decl))
3265 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
3266 error ("invalid use of `restrict'");
3267 else if (flag_strict_aliasing)
a5b1863e 3268 {
d91a20bc 3269 /* No two restricted pointers can point at the same thing.
3270 However, a restricted pointer can point at the same thing
3271 as an unrestricted pointer, if that unrestricted pointer
3272 is based on the restricted pointer. So, we make the
3273 alias set for the restricted pointer a subset of the
3274 alias set for the type pointed to by the type of the
3275 decl. */
3276
44e9fa65 3277 int pointed_to_alias_set
d91a20bc 3278 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
44e9fa65 3279
d91a20bc 3280 if (!pointed_to_alias_set)
3281 /* It's not legal to make a subset of alias set zero. */
3282 ;
3283 else
3284 {
3285 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
3286 record_alias_subset (pointed_to_alias_set,
3287 DECL_POINTER_ALIAS_SET (decl));
3288 }
a5b1863e 3289 }
3290 }
3291}
3292
3293/* T is an expression with pointer type. Find the DECL on which this
3294 expression is based. (For example, in `a[i]' this would be `a'.)
3295 If there is no such DECL, or a unique decl cannot be determined,
3296 NULL_TREE is retured. */
3297
3298static tree
3299c_find_base_decl (t)
3300 tree t;
3301{
3302 int i;
3303 tree decl;
3304
3305 if (t == NULL_TREE || t == error_mark_node)
3306 return NULL_TREE;
3307
3308 if (!POINTER_TYPE_P (TREE_TYPE (t)))
3309 return NULL_TREE;
3310
3311 decl = NULL_TREE;
3312
44e9fa65 3313 if (TREE_CODE (t) == FIELD_DECL
a5b1863e 3314 || TREE_CODE (t) == PARM_DECL
3315 || TREE_CODE (t) == VAR_DECL)
3316 /* Aha, we found a pointer-typed declaration. */
3317 return t;
3318
3319 /* It would be nice to deal with COMPONENT_REFs here. If we could
3320 tell that `a' and `b' were the same, then `a->f' and `b->f' are
3321 also the same. */
3322
3323 /* Handle general expressions. */
3324 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3325 {
3326 case '1':
3327 case '2':
3328 case '3':
3329 for (i = tree_code_length [(int) TREE_CODE (t)]; --i >= 0;)
3330 {
3331 tree d = c_find_base_decl (TREE_OPERAND (t, i));
3332 if (d)
3333 {
3334 if (!decl)
3335 decl = d;
3336 else if (d && d != decl)
3337 /* Two different declarations. That's confusing; let's
3338 just assume we don't know what's going on. */
3339 decl = NULL_TREE;
3340 }
3341 }
3342 break;
3343
3344 default:
3345 break;
3346 }
3347
3348 return decl;
ceee5ef4 3349}
b5ba9f3a 3350
3351/* Return the typed-based alias set for T, which may be an expression
3352 or a type. */
3353
3354int
3355c_get_alias_set (t)
3356 tree t;
3357{
b5ba9f3a 3358 tree type;
be4f2de7 3359 tree u;
b5ba9f3a 3360
3361 if (t == error_mark_node)
3362 return 0;
3363
3364 type = (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
1e2513d9 3365 ? t : TREE_TYPE (t);
b5ba9f3a 3366
3367 if (type == error_mark_node)
3368 return 0;
3369
1e2513d9 3370 /* Deal with special cases first; for certain kinds of references
3371 we're interested in more than just the type. */
b5ba9f3a 3372
3373 if (TREE_CODE (t) == BIT_FIELD_REF)
3374 /* Perhaps reads and writes to this piece of data alias fields
3375 neighboring the bitfield. Perhaps that's impossible. For now,
3376 let's just assume that bitfields can alias everything, which is
3377 the conservative assumption. */
3378 return 0;
1e2513d9 3379
be4f2de7 3380 /* Permit type-punning when accessing a union, provided the access
3381 is directly through the union. For example, this code does not
3382 permit taking the address of a union member and then storing
3383 through it. Even the type-punning allowed here is a GCC
3384 extension, albeit a common and useful one; the C standard says
3385 that such accesses have implementation-defined behavior. */
3386 for (u = t;
3387 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3388 u = TREE_OPERAND (u, 0))
3389 if (TREE_CODE (u) == COMPONENT_REF
3390 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3391 return 0;
1e2513d9 3392
a5b1863e 3393 if (TREE_CODE (t) == INDIRECT_REF)
3394 {
3395 /* Check for accesses through restrict-qualified pointers. */
3396 tree decl = c_find_base_decl (TREE_OPERAND (t, 0));
3397
3398 if (decl && DECL_POINTER_ALIAS_SET_KNOWN_P (decl))
3399 /* We use the alias set indicated in the declaration. */
3400 return DECL_POINTER_ALIAS_SET (decl);
3401 }
3402
1e2513d9 3403 /* From here on, only the type matters. */
3404
be4f2de7 3405 if (TREE_CODE (t) == COMPONENT_REF
3406 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1)))
3407 /* Since build_modify_expr calls get_unwidened for stores to
3408 component references, the type of a bit field can be changed
44e9fa65 3409 from (say) `unsigned int : 16' to `unsigned short' or from
be4f2de7 3410 `enum E : 16' to `short'. We want the real type of the
3411 bit-field in this case, not some the integral equivalent. */
3412 type = DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1));
3413
1e2513d9 3414 if (TYPE_ALIAS_SET_KNOWN_P (type))
3415 /* If we've already calculated the value, just return it. */
3416 return TYPE_ALIAS_SET (type);
b5ba9f3a 3417 else if (TYPE_MAIN_VARIANT (type) != type)
1e2513d9 3418 /* The C standard specifically allows aliasing between
3419 cv-qualified variants of types. */
3420 TYPE_ALIAS_SET (type) = c_get_alias_set (TYPE_MAIN_VARIANT (type));
b5ba9f3a 3421 else if (TREE_CODE (type) == INTEGER_TYPE)
3422 {
3423 tree signed_variant;
3424
3425 /* The C standard specifically allows aliasing between signed and
3426 unsigned variants of the same type. We treat the signed
3427 variant as canonical. */
3428 signed_variant = signed_type (type);
3429
3430 if (signed_variant != type)
1e2513d9 3431 TYPE_ALIAS_SET (type) = c_get_alias_set (signed_variant);
b5ba9f3a 3432 else if (signed_variant == signed_char_type_node)
3433 /* The C standard guarantess that any object may be accessed
3434 via an lvalue that has character type. We don't have to
3435 check for unsigned_char_type_node or char_type_node because
3436 we are specifically looking at the signed variant. */
1e2513d9 3437 TYPE_ALIAS_SET (type) = 0;
b5ba9f3a 3438 }
1e2513d9 3439 else if (TREE_CODE (type) == ARRAY_TYPE)
3440 /* Anything that can alias one of the array elements can alias
3441 the entire array as well. */
3442 TYPE_ALIAS_SET (type) = c_get_alias_set (TREE_TYPE (type));
3443 else if (TREE_CODE (type) == FUNCTION_TYPE)
3444 /* There are no objects of FUNCTION_TYPE, so there's no point in
3445 using up an alias set for them. (There are, of course,
44e9fa65 3446 pointers and references to functions, but that's
1e2513d9 3447 different.) */
3448 TYPE_ALIAS_SET (type) = 0;
b5ba9f3a 3449 else if (TREE_CODE (type) == RECORD_TYPE
3450 || TREE_CODE (type) == UNION_TYPE)
1e2513d9 3451 /* If TYPE is a struct or union type then we're reading or
3452 writing an entire struct. Thus, we don't know anything about
3453 aliasing. (In theory, such an access can only alias objects
3454 whose type is the same as one of the fields, recursively, but
3455 we don't yet make any use of that information.) */
3456 TYPE_ALIAS_SET (type) = 0;
87d2d17e 3457 else if (TREE_CODE (type) == POINTER_TYPE
3458 || TREE_CODE (type) == REFERENCE_TYPE)
3459 {
3460 tree t;
3461
3462 /* Unfortunately, there is no canonical form of a pointer type.
3463 In particular, if we have `typedef int I', then `int *', and
3464 `I *' are different types. So, we have to pick a canonical
3465 representative. We do this below.
44e9fa65 3466
40bdc593 3467 Technically, this approach is actually more conservative that
3468 it needs to be. In particular, `const int *' and `int *'
3469 chould be in different alias sets, according to the C and C++
3470 standard, since their types are not the same, and so,
3471 technically, an `int **' and `const int **' cannot point at
3472 the same thing.
3473
3474 But, the standard is wrong. In particular, this code is
3475 legal C++:
3476
3477 int *ip;
3478 int **ipp = &ip;
3479 const int* const* cipp = &ip;
3480
3481 And, it doesn't make sense for that to be legal unless you
3482 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
3483 the pointed-to types. This issue has been reported to the
3484 C++ committee. */
87d2d17e 3485 t = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3486 t = ((TREE_CODE (type) == POINTER_TYPE)
3487 ? build_pointer_type (t) : build_reference_type (t));
3488 if (t != type)
3489 TYPE_ALIAS_SET (type) = c_get_alias_set (t);
3490 }
1e2513d9 3491
44e9fa65 3492 if (!TYPE_ALIAS_SET_KNOWN_P (type))
4b4db818 3493 /* TYPE is something we haven't seen before. Put it in a new
3494 alias set. */
3495 TYPE_ALIAS_SET (type) = new_alias_set ();
b5ba9f3a 3496
b5ba9f3a 3497 return TYPE_ALIAS_SET (type);
3498}
72040e7e 3499
3500/* Build tree nodes and builtin functions common to both C and C++ language
44e9fa65 3501 frontends.
72040e7e 3502 CPLUS_MODE is nonzero if we are called from the C++ frontend, we generate
44e9fa65 3503 some stricter prototypes in that case.
72040e7e 3504 NO_BUILTINS and NO_NONANSI_BUILTINS contain the respective values of
3505 the language frontend flags flag_no_builtin and
3506 flag_no_nonansi_builtin. */
3507void
3508c_common_nodes_and_builtins (cplus_mode, no_builtins, no_nonansi_builtins)
3509 int cplus_mode, no_builtins, no_nonansi_builtins;
3510{
3511 tree temp;
3512 tree memcpy_ftype, memset_ftype, strlen_ftype;
3513 tree endlink, int_endlink, double_endlink, unsigned_endlink;
3514 tree sizetype_endlink;
3515 tree ptr_ftype, ptr_ftype_unsigned;
3516 tree void_ftype_any, void_ftype_int;
3517 tree double_ftype_double, double_ftype_double_double;
3518 tree float_ftype_float, ldouble_ftype_ldouble;
3519 tree int_ftype_cptr_cptr_sizet;
3520 tree int_ftype_string_string, string_ftype_ptr_ptr;
3521 tree long_ftype_long;
3522 /* Either char* or void*. */
3523 tree traditional_ptr_type_node;
a66c9326 3524 tree va_list_ptr_type_node;
8a15c04a 3525 tree va_list_arg_type_node;
a66c9326 3526
a66c9326 3527 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
3528 va_list_type_node));
8a15c04a 3529
ee702940 3530 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"),
3531 ptrdiff_type_node));
3532
3533 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"),
3534 sizetype));
3535
a66c9326 3536 va_list_ptr_type_node = build_pointer_type (va_list_type_node);
72040e7e 3537
8a15c04a 3538 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
3539 va_list_arg_type_node = build_pointer_type (TREE_TYPE (va_list_type_node));
3540 else
3541 va_list_arg_type_node = va_list_type_node;
3542
72040e7e 3543 endlink = void_list_node;
3544 int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
3545 double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
3546 unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
3547
3548 ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
3549 ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
3550 sizetype_endlink = tree_cons (NULL_TREE, sizetype, endlink);
3551 /* We realloc here because sizetype could be int or unsigned. S'ok. */
3552 ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
3553
3554 void_ftype_any = build_function_type (void_type_node, NULL_TREE);
3555 void_ftype = build_function_type (void_type_node, endlink);
3556 void_ftype_int = build_function_type (void_type_node, int_endlink);
3557 void_ftype_ptr
3558 = build_function_type (void_type_node,
3559 tree_cons (NULL_TREE, ptr_type_node, endlink));
3560
3561 float_ftype_float
3562 = build_function_type (float_type_node,
3563 tree_cons (NULL_TREE, float_type_node, endlink));
3564
3565 double_ftype_double
3566 = build_function_type (double_type_node, double_endlink);
3567
3568 ldouble_ftype_ldouble
3569 = build_function_type (long_double_type_node,
3570 tree_cons (NULL_TREE, long_double_type_node,
3571 endlink));
3572
3573 double_ftype_double_double
3574 = build_function_type (double_type_node,
3575 tree_cons (NULL_TREE, double_type_node,
3576 double_endlink));
3577
3578 int_ftype_int
3579 = build_function_type (integer_type_node, int_endlink);
3580
3581 long_ftype_long
3582 = build_function_type (long_integer_type_node,
3583 tree_cons (NULL_TREE, long_integer_type_node,
3584 endlink));
3585
3586 int_ftype_cptr_cptr_sizet
3587 = build_function_type (integer_type_node,
3588 tree_cons (NULL_TREE, const_ptr_type_node,
3589 tree_cons (NULL_TREE, const_ptr_type_node,
3590 tree_cons (NULL_TREE,
3591 sizetype,
3592 endlink))));
3593
3594 /* Prototype for strcpy. */
3595 string_ftype_ptr_ptr
3596 = build_function_type (string_type_node,
3597 tree_cons (NULL_TREE, string_type_node,
3598 tree_cons (NULL_TREE,
3599 const_string_type_node,
3600 endlink)));
3601
3602 /* Prototype for strcmp. */
3603 int_ftype_string_string
3604 = build_function_type (integer_type_node,
3605 tree_cons (NULL_TREE, const_string_type_node,
3606 tree_cons (NULL_TREE,
3607 const_string_type_node,
3608 endlink)));
3609
3610 /* Prototype for strlen. */
3611 strlen_ftype
3612 = build_function_type ((flag_traditional && ! cplus_mode
3613 ? integer_type_node : sizetype),
3614 tree_cons (NULL_TREE, const_string_type_node,
3615 endlink));
3616
3617 traditional_ptr_type_node = (flag_traditional && ! cplus_mode
3618 ? string_type_node : ptr_type_node);
3619
3620 /* Prototype for memcpy. */
3621 memcpy_ftype
3622 = build_function_type (traditional_ptr_type_node,
3623 tree_cons (NULL_TREE, ptr_type_node,
3624 tree_cons (NULL_TREE, const_ptr_type_node,
3625 sizetype_endlink)));
3626
3627 /* Prototype for memset. */
3628 memset_ftype
3629 = build_function_type (traditional_ptr_type_node,
3630 tree_cons (NULL_TREE, ptr_type_node,
3631 tree_cons (NULL_TREE, integer_type_node,
3632 tree_cons (NULL_TREE,
3633 sizetype,
3634 endlink))));
3635
3636 builtin_function ("__builtin_constant_p", default_function_type,
8305149e 3637 BUILT_IN_CONSTANT_P, BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3638
3639 builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
8305149e 3640 BUILT_IN_RETURN_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3641
3642 builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
8305149e 3643 BUILT_IN_FRAME_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3644
3645 builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
8305149e 3646 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, "alloca");
3647 builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS,
3648 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3649 /* Define alloca, ffs as builtins.
3650 Declare _exit just to mark it as volatile. */
3651 if (! no_builtins && ! no_nonansi_builtins)
3652 {
397f1574 3653#ifndef SMALL_STACK
72040e7e 3654 temp = builtin_function ("alloca", ptr_ftype_sizetype,
8305149e 3655 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3656 /* Suppress error if redefined as a non-function. */
3657 DECL_BUILT_IN_NONANSI (temp) = 1;
397f1574 3658#endif
8305149e 3659 temp = builtin_function ("ffs", int_ftype_int, BUILT_IN_FFS,
3660 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3661 /* Suppress error if redefined as a non-function. */
3662 DECL_BUILT_IN_NONANSI (temp) = 1;
3663 temp = builtin_function ("_exit", void_ftype_int,
8305149e 3664 0, NOT_BUILT_IN, NULL_PTR);
72040e7e 3665 TREE_THIS_VOLATILE (temp) = 1;
3666 TREE_SIDE_EFFECTS (temp) = 1;
3667 /* Suppress error if redefined as a non-function. */
3668 DECL_BUILT_IN_NONANSI (temp) = 1;
3669 }
3670
8305149e 3671 builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS,
3672 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3673 builtin_function ("__builtin_fabsf", float_ftype_float, BUILT_IN_FABS,
8305149e 3674 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3675 builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS,
8305149e 3676 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3677 builtin_function ("__builtin_fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
8305149e 3678 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3679 builtin_function ("__builtin_labs", long_ftype_long, BUILT_IN_LABS,
8305149e 3680 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3681 builtin_function ("__builtin_saveregs", ptr_ftype, BUILT_IN_SAVEREGS,
8305149e 3682 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3683 builtin_function ("__builtin_classify_type", default_function_type,
8305149e 3684 BUILT_IN_CLASSIFY_TYPE, BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3685 builtin_function ("__builtin_next_arg", ptr_ftype, BUILT_IN_NEXT_ARG,
8305149e 3686 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3687 builtin_function ("__builtin_args_info", int_ftype_int, BUILT_IN_ARGS_INFO,
8305149e 3688 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3689 builtin_function ("__builtin_setjmp",
3690 build_function_type (integer_type_node,
3691 tree_cons (NULL_TREE, ptr_type_node,
3692 endlink)),
8305149e 3693 BUILT_IN_SETJMP, BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3694 builtin_function ("__builtin_longjmp",
3695 build_function_type (void_type_node,
3696 tree_cons (NULL_TREE, ptr_type_node,
3697 tree_cons (NULL_TREE,
3698 integer_type_node,
3699 endlink))),
8305149e 3700 BUILT_IN_LONGJMP, BUILT_IN_NORMAL, NULL_PTR);
3701 builtin_function ("__builtin_trap", void_ftype, BUILT_IN_TRAP,
3702 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3703
a4110d9a 3704 /* ISO C99 IEEE Unordered compares. */
3705 builtin_function ("__builtin_isgreater", default_function_type,
3706 BUILT_IN_ISGREATER, BUILT_IN_NORMAL, NULL_PTR);
3707 builtin_function ("__builtin_isgreaterequal", default_function_type,
3708 BUILT_IN_ISGREATEREQUAL, BUILT_IN_NORMAL, NULL_PTR);
3709 builtin_function ("__builtin_isless", default_function_type,
3710 BUILT_IN_ISLESS, BUILT_IN_NORMAL, NULL_PTR);
3711 builtin_function ("__builtin_islessequal", default_function_type,
3712 BUILT_IN_ISLESSEQUAL, BUILT_IN_NORMAL, NULL_PTR);
3713 builtin_function ("__builtin_islessgreater", default_function_type,
3714 BUILT_IN_ISLESSGREATER, BUILT_IN_NORMAL, NULL_PTR);
3715 builtin_function ("__builtin_isunordered", default_function_type,
3716 BUILT_IN_ISUNORDERED, BUILT_IN_NORMAL, NULL_PTR);
3717
72040e7e 3718 /* Untyped call and return. */
3719 builtin_function ("__builtin_apply_args", ptr_ftype,
8305149e 3720 BUILT_IN_APPLY_ARGS, BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3721
3722 temp = tree_cons (NULL_TREE,
3723 build_pointer_type (build_function_type (void_type_node,
3724 NULL_TREE)),
3725 tree_cons (NULL_TREE,
3726 ptr_type_node,
3727 tree_cons (NULL_TREE,
3728 sizetype,
3729 endlink)));
3730 builtin_function ("__builtin_apply",
3731 build_function_type (ptr_type_node, temp),
8305149e 3732 BUILT_IN_APPLY, BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3733 builtin_function ("__builtin_return", void_ftype_ptr,
8305149e 3734 BUILT_IN_RETURN, BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3735
a66c9326 3736 /* Support for varargs.h and stdarg.h. */
3737 builtin_function ("__builtin_varargs_start",
3738 build_function_type (void_type_node,
3739 tree_cons (NULL_TREE,
3740 va_list_ptr_type_node,
3741 endlink)),
8305149e 3742 BUILT_IN_VARARGS_START, BUILT_IN_NORMAL, NULL_PTR);
a66c9326 3743
3744 builtin_function ("__builtin_stdarg_start",
3745 build_function_type (void_type_node,
3746 tree_cons (NULL_TREE,
3747 va_list_ptr_type_node,
3748 NULL_TREE)),
8305149e 3749 BUILT_IN_STDARG_START, BUILT_IN_NORMAL, NULL_PTR);
a66c9326 3750
3751 builtin_function ("__builtin_va_end",
3752 build_function_type (void_type_node,
3753 tree_cons (NULL_TREE,
8a15c04a 3754 va_list_arg_type_node,
a66c9326 3755 endlink)),
8305149e 3756 BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL_PTR);
a66c9326 3757
3758 builtin_function ("__builtin_va_copy",
3759 build_function_type (void_type_node,
3760 tree_cons (NULL_TREE,
3761 va_list_ptr_type_node,
3762 tree_cons (NULL_TREE,
8a15c04a 3763 va_list_arg_type_node,
a66c9326 3764 endlink))),
8305149e 3765 BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL_PTR);
a66c9326 3766
72040e7e 3767 /* Currently under experimentation. */
3768 builtin_function ("__builtin_memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
8305149e 3769 BUILT_IN_NORMAL, "memcpy");
72040e7e 3770 builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
8305149e 3771 BUILT_IN_MEMCMP, BUILT_IN_NORMAL, "memcmp");
72040e7e 3772 builtin_function ("__builtin_memset", memset_ftype, BUILT_IN_MEMSET,
8305149e 3773 BUILT_IN_NORMAL, "memset");
72040e7e 3774 builtin_function ("__builtin_strcmp", int_ftype_string_string,
8305149e 3775 BUILT_IN_STRCMP, BUILT_IN_NORMAL, "strcmp");
72040e7e 3776 builtin_function ("__builtin_strcpy", string_ftype_ptr_ptr,
8305149e 3777 BUILT_IN_STRCPY, BUILT_IN_NORMAL, "strcpy");
72040e7e 3778 builtin_function ("__builtin_strlen", strlen_ftype,
8305149e 3779 BUILT_IN_STRLEN, BUILT_IN_NORMAL, "strlen");
44e9fa65 3780 builtin_function ("__builtin_sqrtf", float_ftype_float,
8305149e 3781 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtf");
72040e7e 3782 builtin_function ("__builtin_fsqrt", double_ftype_double,
8305149e 3783 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrt");
72040e7e 3784 builtin_function ("__builtin_sqrtl", ldouble_ftype_ldouble,
8305149e 3785 BUILT_IN_FSQRT, BUILT_IN_NORMAL, "sqrtl");
44e9fa65 3786 builtin_function ("__builtin_sinf", float_ftype_float,
8305149e 3787 BUILT_IN_SIN, BUILT_IN_NORMAL, "sinf");
44e9fa65 3788 builtin_function ("__builtin_sin", double_ftype_double,
8305149e 3789 BUILT_IN_SIN, BUILT_IN_NORMAL, "sin");
44e9fa65 3790 builtin_function ("__builtin_sinl", ldouble_ftype_ldouble,
8305149e 3791 BUILT_IN_SIN, BUILT_IN_NORMAL, "sinl");
44e9fa65 3792 builtin_function ("__builtin_cosf", float_ftype_float,
8305149e 3793 BUILT_IN_COS, BUILT_IN_NORMAL, "cosf");
44e9fa65 3794 builtin_function ("__builtin_cos", double_ftype_double,
8305149e 3795 BUILT_IN_COS, BUILT_IN_NORMAL, "cos");
44e9fa65 3796 builtin_function ("__builtin_cosl", ldouble_ftype_ldouble,
8305149e 3797 BUILT_IN_COS, BUILT_IN_NORMAL, "cosl");
72040e7e 3798
3799 if (! no_builtins)
3800 {
8305149e 3801 builtin_function ("abs", int_ftype_int, BUILT_IN_ABS,
3802 BUILT_IN_NORMAL, NULL_PTR);
3803 builtin_function ("fabsf", float_ftype_float, BUILT_IN_FABS,
3804 BUILT_IN_NORMAL, NULL_PTR);
3805 builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS,
3806 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3807 builtin_function ("fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
8305149e 3808 BUILT_IN_NORMAL, NULL_PTR);
3809 builtin_function ("labs", long_ftype_long, BUILT_IN_LABS,
3810 BUILT_IN_NORMAL, NULL_PTR);
3811 builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY,
3812 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3813 builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP,
8305149e 3814 BUILT_IN_NORMAL, NULL_PTR);
3815 builtin_function ("memset", memset_ftype, BUILT_IN_MEMSET,
3816 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3817 builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP,
8305149e 3818 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3819 builtin_function ("strcpy", string_ftype_ptr_ptr, BUILT_IN_STRCPY,
8305149e 3820 BUILT_IN_NORMAL, NULL_PTR);
3821 builtin_function ("strlen", strlen_ftype, BUILT_IN_STRLEN,
3822 BUILT_IN_NORMAL, NULL_PTR);
3823 builtin_function ("sqrtf", float_ftype_float, BUILT_IN_FSQRT,
3824 BUILT_IN_NORMAL, NULL_PTR);
3825 builtin_function ("sqrt", double_ftype_double, BUILT_IN_FSQRT,
3826 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3827 builtin_function ("sqrtl", ldouble_ftype_ldouble, BUILT_IN_FSQRT,
8305149e 3828 BUILT_IN_NORMAL, NULL_PTR);
3829 builtin_function ("sinf", float_ftype_float, BUILT_IN_SIN,
3830 BUILT_IN_NORMAL, NULL_PTR);
3831 builtin_function ("sin", double_ftype_double, BUILT_IN_SIN,
3832 BUILT_IN_NORMAL, NULL_PTR);
3833 builtin_function ("sinl", ldouble_ftype_ldouble, BUILT_IN_SIN,
3834 BUILT_IN_NORMAL, NULL_PTR);
3835 builtin_function ("cosf", float_ftype_float, BUILT_IN_COS,
3836 BUILT_IN_NORMAL, NULL_PTR);
3837 builtin_function ("cos", double_ftype_double, BUILT_IN_COS,
3838 BUILT_IN_NORMAL, NULL_PTR);
3839 builtin_function ("cosl", ldouble_ftype_ldouble, BUILT_IN_COS,
3840 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3841
3842 /* Declare these functions volatile
3843 to avoid spurious "control drops through" warnings. */
3844 temp = builtin_function ("abort", cplus_mode ? void_ftype : void_ftype_any,
8305149e 3845 0, NOT_BUILT_IN, NULL_PTR);
72040e7e 3846 TREE_THIS_VOLATILE (temp) = 1;
3847 TREE_SIDE_EFFECTS (temp) = 1;
3848
3849#if 0 /* ??? The C++ frontend used to do this. */
3850 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
3851 them... */
3852 DECL_BUILT_IN_NONANSI (temp) = 1;
3853#endif
3854 temp = builtin_function ("exit",
3855 cplus_mode ? void_ftype_int : void_ftype_any,
8305149e 3856 0, NOT_BUILT_IN, NULL_PTR);
72040e7e 3857 TREE_THIS_VOLATILE (temp) = 1;
3858 TREE_SIDE_EFFECTS (temp) = 1;
3859
3860#if 0 /* ??? The C++ frontend used to do this. */
3861 /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
3862 them... */
3863 DECL_BUILT_IN_NONANSI (temp) = 1;
3864#endif
3865 }
3866
3867#if 0
3868 /* Support for these has not been written in either expand_builtin
3869 or build_function_call. */
8305149e 3870 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV,
3871 BUILT_IN_NORMAL, NULL_PTR);
3872 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV,
3873 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3874 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
8305149e 3875 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3876 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
8305149e 3877 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3878 builtin_function ("__builtin_fmod", double_ftype_double_double,
8305149e 3879 BUILT_IN_FMOD, BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3880 builtin_function ("__builtin_frem", double_ftype_double_double,
8305149e 3881 BUILT_IN_FREM, BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3882 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
8305149e 3883 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3884 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
8305149e 3885 BUILT_IN_NORMAL, NULL_PTR);
72040e7e 3886#endif
e94026da 3887
3888 /* ??? Perhaps there's a better place to do this. But it is related
3889 to __builtin_va_arg, so it isn't that off-the-wall. */
3890 lang_type_promotes_to = simple_type_promotes_to;
72040e7e 3891}
a66c9326 3892
3893tree
3894build_va_arg (expr, type)
3895 tree expr, type;
3896{
3897 return build1 (VA_ARG_EXPR, type, expr);
3898}
e94026da 3899\f
3900/* Given a type, apply default promotions wrt unnamed function arguments
3901 and return the new type. Return NULL_TREE if no change. */
44e9fa65 3902/* ??? There is a function of the same name in the C++ front end that
e94026da 3903 does something similar, but is more thorough and does not return NULL
44e9fa65 3904 if no change. We could perhaps share code, but it would make the
e94026da 3905 self_promoting_type property harder to identify. */
3906
3907tree
3908simple_type_promotes_to (type)
3909 tree type;
3910{
3911 if (TYPE_MAIN_VARIANT (type) == float_type_node)
3912 return double_type_node;
3913
3914 if (C_PROMOTING_INTEGER_TYPE_P (type))
3915 {
3916 /* Traditionally, unsignedness is preserved in default promotions.
3917 Also preserve unsignedness if not really getting any wider. */
3918 if (TREE_UNSIGNED (type)
3919 && (flag_traditional
3920 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
3921 return unsigned_type_node;
3922 return integer_type_node;
3923 }
3924
3925 return NULL_TREE;
3926}
3927
3928/* Return 1 if PARMS specifies a fixed number of parameters
3929 and none of their types is affected by default promotions. */
3930
3931int
3932self_promoting_args_p (parms)
3933 tree parms;
3934{
3935 register tree t;
3936 for (t = parms; t; t = TREE_CHAIN (t))
3937 {
3938 register tree type = TREE_VALUE (t);
43f74bc4 3939
e94026da 3940 if (TREE_CHAIN (t) == 0 && type != void_type_node)
3941 return 0;
3942
3943 if (type == 0)
3944 return 0;
3945
3946 if (TYPE_MAIN_VARIANT (type) == float_type_node)
3947 return 0;
3948
3949 if (C_PROMOTING_INTEGER_TYPE_P (type))
3950 return 0;
3951 }
3952 return 1;
3953}
605fb01e 3954
3955/* Recognize certain built-in functions so we can make tree-codes
3956 other than CALL_EXPR. We do this when it enables fold-const.c
3957 to do something useful. */
3958/* ??? By rights this should go in builtins.c, but only C and C++
3959 implement build_{binary,unary}_op. Not exactly sure what bits
3960 of functionality are actually needed from those functions, or
3961 where the similar functionality exists in the other front ends. */
3962
3963tree
3964expand_tree_builtin (function, params, coerced_params)
3965 tree function, params, coerced_params;
3966{
3967 enum tree_code code;
3968
3969 if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL)
3970 return NULL_TREE;
3971
3972 switch (DECL_FUNCTION_CODE (function))
3973 {
3974 case BUILT_IN_ABS:
3975 case BUILT_IN_LABS:
3976 case BUILT_IN_FABS:
3977 if (coerced_params == 0)
3978 return integer_zero_node;
3979 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
3980
3981 case BUILT_IN_ISGREATER:
3982 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3983 code = UNLE_EXPR;
3984 else
3985 code = LE_EXPR;
3986 goto unordered_cmp;
3987
3988 case BUILT_IN_ISGREATEREQUAL:
3989 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3990 code = UNLT_EXPR;
3991 else
3992 code = LT_EXPR;
3993 goto unordered_cmp;
3994
3995 case BUILT_IN_ISLESS:
3996 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3997 code = UNGE_EXPR;
3998 else
3999 code = GE_EXPR;
4000 goto unordered_cmp;
4001
4002 case BUILT_IN_ISLESSEQUAL:
4003 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4004 code = UNGT_EXPR;
4005 else
4006 code = GT_EXPR;
4007 goto unordered_cmp;
4008
4009 case BUILT_IN_ISLESSGREATER:
4010 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
4011 code = UNEQ_EXPR;
4012 else
4013 code = EQ_EXPR;
4014 goto unordered_cmp;
4015
4016 case BUILT_IN_ISUNORDERED:
4017 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
4018 return integer_zero_node;
4019 code = UNORDERED_EXPR;
4020 goto unordered_cmp;
4021
4022 unordered_cmp:
4023 {
4024 tree arg0, arg1;
4025
4026 if (params == 0
4027 || TREE_CHAIN (params) == 0)
4028 {
4029 error ("too few arguments to function `%s'",
4030 IDENTIFIER_POINTER (DECL_NAME (function)));
4031 return error_mark_node;
4032 }
4033 else if (TREE_CHAIN (TREE_CHAIN (params)) != 0)
4034 {
4035 error ("too many arguments to function `%s'",
4036 IDENTIFIER_POINTER (DECL_NAME (function)));
4037 return error_mark_node;
4038 }
4039
4040 arg0 = TREE_VALUE (params);
4041 arg1 = TREE_VALUE (TREE_CHAIN (params));
4042 arg0 = build_binary_op (code, arg0, arg1, 0);
4043 if (code != UNORDERED_EXPR)
4044 arg0 = build_unary_op (TRUTH_NOT_EXPR, arg0, 0);
4045 return arg0;
4046 }
4047 break;
4048
4049 default:
4050 break;
4051 }
4052
4053 return NULL_TREE;
4054}