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