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