]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-common.c
Fix warnings from Kaveh R. Ghazi
[thirdparty/gcc.git] / gcc / c-common.c
CommitLineData
b30f223b 1/* Subroutines shared by all languages that are variants of C.
f9dcab52 2 Copyright (C) 1992, 93, 94, 95, 96, 1997 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"
ccd043a9 28
fef610be
JW
29#ifndef WCHAR_TYPE_SIZE
30#ifdef INT_TYPE_SIZE
31#define WCHAR_TYPE_SIZE INT_TYPE_SIZE
32#else
33#define WCHAR_TYPE_SIZE BITS_PER_WORD
34#endif
35#endif
36
0b73773c
NH
37extern struct obstack permanent_obstack;
38
e78a3b42
RK
39/* Nonzero means the expression being parsed will never be evaluated.
40 This is a count, since unevaluated expressions can nest. */
41int skip_evaluation;
42
2786cbad 43enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
bbb1ae01 44 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
0161e8da 45 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS};
53065596
RK
46
47static void declare_hidden_char_array PROTO((char *, char *));
48static void add_attribute PROTO((enum attrs, char *,
49 int, int, int));
50static void init_attributes PROTO((void));
0161e8da 51static void record_international_format PROTO((tree, tree, int));
4724b3de 52
0a7ed33c
BS
53/* Keep a stack of if statements. We record the number of compound
54 statements seen up to the if keyword, as well as the line number
55 and file of the if. If a potentially ambiguous else is seen, that
56 fact is recorded; the warning is issued when we can be sure that
57 the enclosing if statement does not have an else branch. */
58typedef struct
59{
60 int compstmt_count;
61 int line;
62 char *file;
63 int needs_warning;
64} if_elt;
65
66static if_elt *if_stack;
6d819282
MK
67
68/* Amount of space in the if statement stack. */
69static int if_stack_space = 0;
70
71/* Stack pointer. */
72static int if_stack_pointer = 0;
73
0a7ed33c
BS
74/* Generate RTL for the start of an if-then, and record the start of it
75 for ambiguous else detection. */
76
6d819282
MK
77void
78c_expand_start_cond (cond, exitflag, compstmt_count)
79 tree cond;
80 int exitflag;
81 int compstmt_count;
82{
83 /* Make sure there is enough space on the stack. */
84 if (if_stack_space == 0)
85 {
86 if_stack_space = 10;
0a7ed33c 87 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
6d819282
MK
88 }
89 else if (if_stack_space == if_stack_pointer)
90 {
91 if_stack_space += 10;
0a7ed33c 92 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
6d819282 93 }
0a7ed33c 94
6d819282 95 /* Record this if statement. */
0a7ed33c
BS
96 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
97 if_stack[if_stack_pointer].file = input_filename;
98 if_stack[if_stack_pointer].line = lineno;
99 if_stack[if_stack_pointer].needs_warning = 0;
100 if_stack_pointer++;
6d819282
MK
101
102 expand_start_cond (cond, exitflag);
103}
104
0a7ed33c
BS
105/* Generate RTL for the end of an if-then. Optionally warn if a nested
106 if statement had an ambiguous else clause. */
107
6d819282
MK
108void
109c_expand_end_cond ()
110{
111 if_stack_pointer--;
0a7ed33c
BS
112 if (if_stack[if_stack_pointer].needs_warning)
113 warning_with_file_and_line (if_stack[if_stack_pointer].file,
114 if_stack[if_stack_pointer].line,
115 "suggest explicit braces to avoid ambiguous `else'");
6d819282
MK
116 expand_end_cond ();
117}
118
0a7ed33c
BS
119/* Generate RTL between the then-clause and the else-clause
120 of an if-then-else. */
121
6d819282
MK
122void
123c_expand_start_else ()
124{
0a7ed33c
BS
125 /* An ambiguous else warning must be generated for the enclosing if
126 statement, unless we see an else branch for that one, too. */
6d819282
MK
127 if (warn_parentheses
128 && if_stack_pointer > 1
0a7ed33c
BS
129 && (if_stack[if_stack_pointer - 1].compstmt_count
130 == if_stack[if_stack_pointer - 2].compstmt_count))
131 if_stack[if_stack_pointer - 2].needs_warning = 1;
132
133 /* Even if a nested if statement had an else branch, it can't be
134 ambiguous if this one also has an else. So don't warn in that
135 case. Also don't warn for any if statements nested in this else. */
136 if_stack[if_stack_pointer - 1].needs_warning = 0;
137 if_stack[if_stack_pointer - 1].compstmt_count--;
6d819282
MK
138
139 expand_start_else ();
140}
141
b032c74c 142/* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
7da551a2
RS
143
144void
145declare_function_name ()
146{
7da551a2
RS
147 char *name, *printable_name;
148
149 if (current_function_decl == NULL)
150 {
151 name = "";
152 printable_name = "top level";
153 }
154 else
155 {
6152f876
RS
156 /* Allow functions to be nameless (such as artificial ones). */
157 if (DECL_NAME (current_function_decl))
158 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
159 else
160 name = "";
a1d7ffe3 161 printable_name = (*decl_printable_name) (current_function_decl, 2);
7da551a2
RS
162 }
163
4724b3de
RS
164 declare_hidden_char_array ("__FUNCTION__", name);
165 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
166}
97d17ac2 167
4724b3de
RS
168static void
169declare_hidden_char_array (name, value)
170 char *name, *value;
171{
172 tree decl, type, init;
173 int vlen;
7da551a2 174
4724b3de 175 /* If the default size of char arrays isn't big enough for the name,
700942a0 176 or if we want to give warnings for large objects, make a bigger one. */
4724b3de 177 vlen = strlen (value) + 1;
97d17ac2 178 type = char_array_type_node;
700942a0
RK
179 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TREE_TYPE (type))) < vlen
180 || warn_larger_than)
97d17ac2 181 type = build_array_type (char_type_node,
4724b3de 182 build_index_type (build_int_2 (vlen, 0)));
7da551a2 183 push_obstacks_nochange ();
4724b3de 184 decl = build_decl (VAR_DECL, get_identifier (name), type);
7da551a2
RS
185 TREE_STATIC (decl) = 1;
186 TREE_READONLY (decl) = 1;
4724b3de 187 TREE_ASM_WRITTEN (decl) = 1;
b9a24ad4 188 DECL_SOURCE_LINE (decl) = 0;
d2eb0876 189 DECL_ARTIFICIAL (decl) = 1;
176e81eb 190 DECL_IN_SYSTEM_HEADER (decl) = 1;
7da551a2 191 DECL_IGNORED_P (decl) = 1;
4724b3de 192 init = build_string (vlen, value);
97d17ac2 193 TREE_TYPE (init) = type;
7da551a2
RS
194 DECL_INITIAL (decl) = init;
195 finish_decl (pushdecl (decl), init, NULL_TREE);
196}
197
b30f223b
RS
198/* Given a chain of STRING_CST nodes,
199 concatenate them into one STRING_CST
200 and give it a suitable array-of-chars data type. */
201
202tree
203combine_strings (strings)
204 tree strings;
205{
206 register tree value, t;
207 register int length = 1;
208 int wide_length = 0;
209 int wide_flag = 0;
210 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
211 int nchars;
212
213 if (TREE_CHAIN (strings))
214 {
215 /* More than one in the chain, so concatenate. */
216 register char *p, *q;
217
218 /* Don't include the \0 at the end of each substring,
219 except for the last one.
220 Count wide strings and ordinary strings separately. */
221 for (t = strings; t; t = TREE_CHAIN (t))
222 {
223 if (TREE_TYPE (t) == wchar_array_type_node)
224 {
225 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
226 wide_flag = 1;
227 }
228 else
229 length += (TREE_STRING_LENGTH (t) - 1);
230 }
231
232 /* If anything is wide, the non-wides will be converted,
233 which makes them take more space. */
234 if (wide_flag)
235 length = length * wchar_bytes + wide_length;
236
237 p = savealloc (length);
238
239 /* Copy the individual strings into the new combined string.
240 If the combined string is wide, convert the chars to ints
241 for any individual strings that are not wide. */
242
243 q = p;
244 for (t = strings; t; t = TREE_CHAIN (t))
245 {
246 int len = (TREE_STRING_LENGTH (t)
247 - ((TREE_TYPE (t) == wchar_array_type_node)
248 ? wchar_bytes : 1));
249 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
250 {
251 bcopy (TREE_STRING_POINTER (t), q, len);
252 q += len;
253 }
254 else
255 {
256 int i;
257 for (i = 0; i < len; i++)
41bbd14e
JW
258 {
259 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
260 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
261 else
262 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
263 }
b30f223b
RS
264 q += len * wchar_bytes;
265 }
266 }
267 if (wide_flag)
268 {
269 int i;
270 for (i = 0; i < wchar_bytes; i++)
271 *q++ = 0;
272 }
273 else
274 *q = 0;
275
276 value = make_node (STRING_CST);
277 TREE_STRING_POINTER (value) = p;
278 TREE_STRING_LENGTH (value) = length;
279 TREE_CONSTANT (value) = 1;
280 }
281 else
282 {
283 value = strings;
284 length = TREE_STRING_LENGTH (value);
285 if (TREE_TYPE (value) == wchar_array_type_node)
286 wide_flag = 1;
287 }
288
289 /* Compute the number of elements, for the array type. */
290 nchars = wide_flag ? length / wchar_bytes : length;
291
292 /* Create the array type for the string constant.
293 -Wwrite-strings says make the string constant an array of const char
294 so that copying it to a non-const pointer will get a warning. */
295 if (warn_write_strings
296 && (! flag_traditional && ! flag_writable_strings))
297 {
298 tree elements
299 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
300 1, 0);
301 TREE_TYPE (value)
302 = build_array_type (elements,
303 build_index_type (build_int_2 (nchars - 1, 0)));
304 }
305 else
306 TREE_TYPE (value)
307 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
308 build_index_type (build_int_2 (nchars - 1, 0)));
309 TREE_CONSTANT (value) = 1;
310 TREE_STATIC (value) = 1;
311 return value;
312}
313\f
53065596
RK
314/* To speed up processing of attributes, we maintain an array of
315 IDENTIFIER_NODES and the corresponding attribute types. */
316
317/* Array to hold attribute information. */
318
319static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
320
321static int attrtab_idx = 0;
322
323/* Add an entry to the attribute table above. */
324
325static void
326add_attribute (id, string, min_len, max_len, decl_req)
327 enum attrs id;
328 char *string;
329 int min_len, max_len;
330 int decl_req;
331{
332 char buf[100];
333
334 attrtab[attrtab_idx].id = id;
335 attrtab[attrtab_idx].name = get_identifier (string);
336 attrtab[attrtab_idx].min = min_len;
337 attrtab[attrtab_idx].max = max_len;
338 attrtab[attrtab_idx++].decl_req = decl_req;
339
340 sprintf (buf, "__%s__", string);
341
342 attrtab[attrtab_idx].id = id;
343 attrtab[attrtab_idx].name = get_identifier (buf);
344 attrtab[attrtab_idx].min = min_len;
345 attrtab[attrtab_idx].max = max_len;
346 attrtab[attrtab_idx++].decl_req = decl_req;
347}
348
349/* Initialize attribute table. */
350
351static void
352init_attributes ()
353{
a89ca5c6 354 add_attribute (A_PACKED, "packed", 0, 0, 0);
bbb1ae01 355 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
2786cbad 356 add_attribute (A_COMMON, "common", 0, 0, 1);
53065596
RK
357 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
358 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
6d819282 359 add_attribute (A_UNUSED, "unused", 0, 0, 0);
53065596
RK
360 add_attribute (A_CONST, "const", 0, 0, 1);
361 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
362 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
363 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
364 add_attribute (A_MODE, "mode", 1, 1, 1);
365 add_attribute (A_SECTION, "section", 1, 1, 1);
366 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
d161fae4 367 add_attribute (A_FORMAT, "format", 3, 3, 1);
0161e8da 368 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
4b8af8d9
JM
369 add_attribute (A_WEAK, "weak", 0, 0, 1);
370 add_attribute (A_ALIAS, "alias", 1, 1, 1);
53065596
RK
371}
372\f
1228e2a6 373/* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
53065596
RK
374 and install them in NODE, which is either a DECL (including a TYPE_DECL)
375 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
0f41302f 376 and declaration modifiers but before the declaration proper. */
b30f223b
RS
377
378void
53065596
RK
379decl_attributes (node, attributes, prefix_attributes)
380 tree node, attributes, prefix_attributes;
b30f223b 381{
53065596
RK
382 tree decl = 0, type;
383 int is_type;
384 tree a;
385
386 if (attrtab_idx == 0)
387 init_attributes ();
388
389 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
390 {
391 decl = node;
392 type = TREE_TYPE (decl);
393 is_type = TREE_CODE (node) == TYPE_DECL;
394 }
395 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't')
396 type = node, is_type = 1;
a2cd7b45 397
eed32833 398 attributes = chainon (prefix_attributes, attributes);
5289b665 399
b30f223b 400 for (a = attributes; a; a = TREE_CHAIN (a))
53065596
RK
401 {
402 tree name = TREE_PURPOSE (a);
403 tree args = TREE_VALUE (a);
404 int i;
405 enum attrs id;
406
407 for (i = 0; i < attrtab_idx; i++)
408 if (attrtab[i].name == name)
409 break;
410
6eaba4a7 411 if (i == attrtab_idx)
53065596 412 {
6eaba4a7
DE
413 if (! valid_machine_attribute (name, args, decl, type))
414 warning ("`%s' attribute directive ignored",
415 IDENTIFIER_POINTER (name));
17c1a44f
SC
416 else if (decl != 0)
417 type = TREE_TYPE (decl);
53065596
RK
418 continue;
419 }
420 else if (attrtab[i].decl_req && decl == 0)
421 {
422 warning ("`%s' attribute does not apply to types",
423 IDENTIFIER_POINTER (name));
424 continue;
425 }
426 else if (list_length (args) < attrtab[i].min
427 || list_length (args) > attrtab[i].max)
428 {
429 error ("wrong number of arguments specified for `%s' attribute",
430 IDENTIFIER_POINTER (name));
431 continue;
432 }
433
434 id = attrtab[i].id;
435 switch (id)
436 {
437 case A_PACKED:
1bcf5b08 438 if (is_type)
a89ca5c6
RK
439 TYPE_PACKED (type) = 1;
440 else if (TREE_CODE (decl) == FIELD_DECL)
53065596
RK
441 DECL_PACKED (decl) = 1;
442 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
443 used for DECL_REGISTER. It wouldn't mean anything anyway. */
444 else
445 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
446 break;
447
bbb1ae01
RK
448 case A_NOCOMMON:
449 if (TREE_CODE (decl) == VAR_DECL)
450 DECL_COMMON (decl) = 0;
451 else
452 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
453 break;
454
2786cbad
JM
455 case A_COMMON:
456 if (TREE_CODE (decl) == VAR_DECL)
457 DECL_COMMON (decl) = 1;
458 else
459 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
460 break;
461
53065596
RK
462 case A_NORETURN:
463 if (TREE_CODE (decl) == FUNCTION_DECL)
464 TREE_THIS_VOLATILE (decl) = 1;
465 else if (TREE_CODE (type) == POINTER_TYPE
466 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
467 TREE_TYPE (decl) = type
468 = build_pointer_type
469 (build_type_variant (TREE_TYPE (type),
470 TREE_READONLY (TREE_TYPE (type)), 1));
471 else
472 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
473 break;
474
1c4fadec 475 case A_UNUSED:
6d819282
MK
476 if (is_type)
477 TREE_USED (type) = 1;
478 else if (TREE_CODE (decl) == PARM_DECL
479 || TREE_CODE (decl) == VAR_DECL
480 || TREE_CODE (decl) == FUNCTION_DECL)
1c4fadec
RK
481 TREE_USED (decl) = 1;
482 else
483 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
484 break;
485
53065596
RK
486 case A_CONST:
487 if (TREE_CODE (decl) == FUNCTION_DECL)
488 TREE_READONLY (decl) = 1;
489 else if (TREE_CODE (type) == POINTER_TYPE
490 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
491 TREE_TYPE (decl) = type
492 = build_pointer_type
493 (build_type_variant (TREE_TYPE (type), 1,
494 TREE_THIS_VOLATILE (TREE_TYPE (type))));
495 else
496 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
497 break;
498
499 case A_T_UNION:
1bcf5b08 500 if (is_type
53065596 501 && TREE_CODE (type) == UNION_TYPE
1bcf5b08 502 && (decl == 0
62b1077c
RK
503 || (TYPE_FIELDS (type) != 0
504 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
1bcf5b08
RK
505 TYPE_TRANSPARENT_UNION (type) = 1;
506 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
53065596
RK
507 && TREE_CODE (type) == UNION_TYPE
508 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
1bcf5b08 509 DECL_TRANSPARENT_UNION (decl) = 1;
53065596
RK
510 else
511 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
512 break;
513
514 case A_CONSTRUCTOR:
515 if (TREE_CODE (decl) == FUNCTION_DECL
516 && TREE_CODE (type) == FUNCTION_TYPE
517 && decl_function_context (decl) == 0)
1a16a053
RK
518 {
519 DECL_STATIC_CONSTRUCTOR (decl) = 1;
520 TREE_USED (decl) = 1;
521 }
53065596
RK
522 else
523 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
524 break;
525
526 case A_DESTRUCTOR:
527 if (TREE_CODE (decl) == FUNCTION_DECL
528 && TREE_CODE (type) == FUNCTION_TYPE
529 && decl_function_context (decl) == 0)
1a16a053
RK
530 {
531 DECL_STATIC_DESTRUCTOR (decl) = 1;
532 TREE_USED (decl) = 1;
533 }
53065596
RK
534 else
535 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
536 break;
537
538 case A_MODE:
539 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
540 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
541 else
542 {
543 int j;
544 char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
545 int len = strlen (p);
546 enum machine_mode mode = VOIDmode;
547 tree typefm;
548
549 if (len > 4 && p[0] == '_' && p[1] == '_'
550 && p[len - 1] == '_' && p[len - 2] == '_')
551 {
3438dff9 552 char *newp = (char *) alloca (len - 1);
53065596
RK
553
554 strcpy (newp, &p[2]);
555 newp[len - 4] = '\0';
556 p = newp;
557 }
558
559 /* Give this decl a type with the specified mode.
560 First check for the special modes. */
561 if (! strcmp (p, "byte"))
562 mode = byte_mode;
563 else if (!strcmp (p, "word"))
564 mode = word_mode;
565 else if (! strcmp (p, "pointer"))
566 mode = ptr_mode;
567 else
568 for (j = 0; j < NUM_MACHINE_MODES; j++)
569 if (!strcmp (p, GET_MODE_NAME (j)))
570 mode = (enum machine_mode) j;
571
572 if (mode == VOIDmode)
573 error ("unknown machine mode `%s'", p);
574 else if (0 == (typefm = type_for_mode (mode,
575 TREE_UNSIGNED (type))))
576 error ("no data type for mode `%s'", p);
577 else
578 {
579 TREE_TYPE (decl) = type = typefm;
580 DECL_SIZE (decl) = 0;
581 layout_decl (decl, 0);
582 }
583 }
584 break;
585
586 case A_SECTION:
5289b665 587#ifdef ASM_OUTPUT_SECTION_NAME
53065596
RK
588 if ((TREE_CODE (decl) == FUNCTION_DECL
589 || TREE_CODE (decl) == VAR_DECL)
590 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
591 {
592 if (TREE_CODE (decl) == VAR_DECL
f4ca236c
RK
593 && current_function_decl != NULL_TREE
594 && ! TREE_STATIC (decl))
53065596
RK
595 error_with_decl (decl,
596 "section attribute cannot be specified for local variables");
597 /* The decl may have already been given a section attribute from
598 a previous declaration. Ensure they match. */
599 else if (DECL_SECTION_NAME (decl) != NULL_TREE
600 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
601 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
602 error_with_decl (node,
603 "section of `%s' conflicts with previous declaration");
604 else
605 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
606 }
607 else
608 error_with_decl (node,
5289b665
DE
609 "section attribute not allowed for `%s'");
610#else
53065596
RK
611 error_with_decl (node,
612 "section attributes are not supported for this target");
5289b665 613#endif
53065596
RK
614 break;
615
616 case A_ALIGNED:
6f38f669 617 {
53065596 618 tree align_expr
54630035
RK
619 = (args ? TREE_VALUE (args)
620 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
53065596
RK
621 int align;
622
623 /* Strip any NOPs of any kind. */
624 while (TREE_CODE (align_expr) == NOP_EXPR
625 || TREE_CODE (align_expr) == CONVERT_EXPR
626 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
627 align_expr = TREE_OPERAND (align_expr, 0);
628
629 if (TREE_CODE (align_expr) != INTEGER_CST)
630 {
631 error ("requested alignment is not a constant");
632 continue;
633 }
634
635 align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
636
637 if (exact_log2 (align) == -1)
638 error ("requested alignment is not a power of 2");
639 else if (is_type)
06aa52de 640 TYPE_ALIGN (type) = align;
53065596
RK
641 else if (TREE_CODE (decl) != VAR_DECL
642 && TREE_CODE (decl) != FIELD_DECL)
643 error_with_decl (decl,
644 "alignment may not be specified for `%s'");
645 else
646 DECL_ALIGN (decl) = align;
6f38f669 647 }
53065596 648 break;
6f38f669 649
53065596 650 case A_FORMAT:
b30f223b 651 {
53065596
RK
652 tree format_type = TREE_VALUE (args);
653 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
654 tree first_arg_num_expr
655 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
656 int format_num;
657 int first_arg_num;
658 int is_scan;
659 tree argument;
660 int arg_num;
b30f223b 661
53065596
RK
662 if (TREE_CODE (decl) != FUNCTION_DECL)
663 {
664 error_with_decl (decl,
665 "argument format specified for non-function `%s'");
666 continue;
667 }
b30f223b 668
53065596
RK
669 if (TREE_CODE (format_type) == IDENTIFIER_NODE
670 && (!strcmp (IDENTIFIER_POINTER (format_type), "printf")
671 || !strcmp (IDENTIFIER_POINTER (format_type),
672 "__printf__")))
673 is_scan = 0;
674 else if (TREE_CODE (format_type) == IDENTIFIER_NODE
675 && (!strcmp (IDENTIFIER_POINTER (format_type), "scanf")
676 || !strcmp (IDENTIFIER_POINTER (format_type),
677 "__scanf__")))
678 is_scan = 1;
0161e8da
RK
679 else if (TREE_CODE (format_type) == IDENTIFIER_NODE)
680 {
6d819282
MK
681 warning ("`%s' is an unrecognized format function type",
682 IDENTIFIER_POINTER (format_type));
0161e8da
RK
683 continue;
684 }
53065596
RK
685 else
686 {
0161e8da 687 error ("unrecognized format specifier");
53065596
RK
688 continue;
689 }
6f38f669 690
53065596
RK
691 /* Strip any conversions from the string index and first arg number
692 and verify they are constants. */
693 while (TREE_CODE (format_num_expr) == NOP_EXPR
694 || TREE_CODE (format_num_expr) == CONVERT_EXPR
695 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
696 format_num_expr = TREE_OPERAND (format_num_expr, 0);
677ff441 697
53065596
RK
698 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
699 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
700 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
701 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
702
703 if (TREE_CODE (format_num_expr) != INTEGER_CST
704 || TREE_CODE (first_arg_num_expr) != INTEGER_CST)
09e3dd72 705 {
53065596
RK
706 error ("format string has non-constant operand number");
707 continue;
09e3dd72 708 }
53065596
RK
709
710 format_num = TREE_INT_CST_LOW (format_num_expr);
711 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
712 if (first_arg_num != 0 && first_arg_num <= format_num)
68a91d8d 713 {
53065596 714 error ("format string arg follows the args to be formatted");
6f38f669 715 continue;
68a91d8d 716 }
53065596
RK
717
718 /* If a parameter list is specified, verify that the format_num
719 argument is actually a string, in case the format attribute
720 is in error. */
721 argument = TYPE_ARG_TYPES (type);
722 if (argument)
09e3dd72 723 {
53065596 724 for (arg_num = 1; ; ++arg_num)
09e3dd72 725 {
53065596
RK
726 if (argument == 0 || arg_num == format_num)
727 break;
728 argument = TREE_CHAIN (argument);
729 }
730 if (! argument
731 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
732 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
733 != char_type_node))
734 {
735 error ("format string arg not a string type");
09e3dd72
RK
736 continue;
737 }
53065596
RK
738 if (first_arg_num != 0)
739 {
740 /* Verify that first_arg_num points to the last arg,
0f41302f 741 the ... */
53065596
RK
742 while (argument)
743 arg_num++, argument = TREE_CHAIN (argument);
744 if (arg_num != first_arg_num)
745 {
746 error ("args to be formatted is not ...");
747 continue;
748 }
749 }
09e3dd72 750 }
a2cd7b45 751
53065596
RK
752 record_function_format (DECL_NAME (decl),
753 DECL_ASSEMBLER_NAME (decl),
754 is_scan, format_num, first_arg_num);
755 break;
756 }
4b8af8d9 757
0161e8da
RK
758 case A_FORMAT_ARG:
759 {
760 tree format_num_expr = TREE_VALUE (args);
761 int format_num, arg_num;
762 tree argument;
763
764 if (TREE_CODE (decl) != FUNCTION_DECL)
765 {
766 error_with_decl (decl,
767 "argument format specified for non-function `%s'");
768 continue;
769 }
770
771 /* Strip any conversions from the first arg number and verify it
772 is a constant. */
773 while (TREE_CODE (format_num_expr) == NOP_EXPR
774 || TREE_CODE (format_num_expr) == CONVERT_EXPR
775 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
776 format_num_expr = TREE_OPERAND (format_num_expr, 0);
777
778 if (TREE_CODE (format_num_expr) != INTEGER_CST)
779 {
780 error ("format string has non-constant operand number");
781 continue;
782 }
783
784 format_num = TREE_INT_CST_LOW (format_num_expr);
785
786 /* If a parameter list is specified, verify that the format_num
787 argument is actually a string, in case the format attribute
788 is in error. */
789 argument = TYPE_ARG_TYPES (type);
790 if (argument)
791 {
792 for (arg_num = 1; ; ++arg_num)
793 {
794 if (argument == 0 || arg_num == format_num)
795 break;
796 argument = TREE_CHAIN (argument);
797 }
798 if (! argument
799 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
800 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
801 != char_type_node))
802 {
803 error ("format string arg not a string type");
804 continue;
805 }
806 }
807
808 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
809 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
810 != char_type_node))
811 {
812 error ("function does not return string type");
813 continue;
814 }
815
816 record_international_format (DECL_NAME (decl),
817 DECL_ASSEMBLER_NAME (decl),
818 format_num);
819 break;
820 }
821
4b8af8d9
JM
822 case A_WEAK:
823 declare_weak (decl);
824 break;
825
826 case A_ALIAS:
827 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
5d9dd5a5 828 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
4b8af8d9
JM
829 error_with_decl (decl,
830 "`%s' defined both normally and as an alias");
831 else if (decl_function_context (decl) == 0)
832 {
833 tree id = get_identifier (TREE_STRING_POINTER
834 (TREE_VALUE (args)));
835 if (TREE_CODE (decl) == FUNCTION_DECL)
836 DECL_INITIAL (decl) = error_mark_node;
837 else
838 DECL_EXTERNAL (decl) = 0;
839 assemble_alias (decl, id);
840 }
841 else
842 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
843 break;
53065596
RK
844 }
845 }
b30f223b 846}
800f4153
RK
847
848/* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
849 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
850
851 The head of the declspec list is stored in DECLSPECS.
852 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
853
854 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
855 the list elements. We drop the containing TREE_LIST nodes and link the
856 resulting attributes together the way decl_attributes expects them. */
857
858void
859split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
860 tree specs_attrs;
861 tree *declspecs, *prefix_attributes;
862{
863 tree t, s, a, next, specs, attrs;
864
865 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
866 if (specs_attrs != NULL_TREE
867 && TREE_CODE (specs_attrs) != TREE_LIST)
868 {
869 *declspecs = specs_attrs;
870 *prefix_attributes = NULL_TREE;
871 return;
872 }
873
874 /* Remember to keep the lists in the same order, element-wise. */
875
876 specs = s = NULL_TREE;
877 attrs = a = NULL_TREE;
878 for (t = specs_attrs; t; t = next)
879 {
880 next = TREE_CHAIN (t);
881 /* Declspecs have a non-NULL TREE_VALUE. */
882 if (TREE_VALUE (t) != NULL_TREE)
883 {
884 if (specs == NULL_TREE)
885 specs = s = t;
886 else
887 {
888 TREE_CHAIN (s) = t;
889 s = t;
890 }
891 }
892 else
893 {
894 if (attrs == NULL_TREE)
895 attrs = a = TREE_PURPOSE (t);
896 else
897 {
898 TREE_CHAIN (a) = TREE_PURPOSE (t);
899 a = TREE_PURPOSE (t);
900 }
901 /* More attrs can be linked here, move A to the end. */
902 while (TREE_CHAIN (a) != NULL_TREE)
903 a = TREE_CHAIN (a);
904 }
905 }
906
907 /* Terminate the lists. */
908 if (s != NULL_TREE)
909 TREE_CHAIN (s) = NULL_TREE;
910 if (a != NULL_TREE)
911 TREE_CHAIN (a) = NULL_TREE;
912
913 /* All done. */
914 *declspecs = specs;
915 *prefix_attributes = attrs;
916}
d9525bec
BK
917
918/* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
919 This function is used by the parser when a rule will accept attributes
920 in a particular position, but we don't want to support that just yet.
921
922 A warning is issued for every ignored attribute. */
923
924tree
925strip_attrs (specs_attrs)
926 tree specs_attrs;
927{
928 tree specs, attrs;
929
930 split_specs_attrs (specs_attrs, &specs, &attrs);
931
932 while (attrs)
933 {
934 warning ("`%s' attribute ignored",
935 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
936 attrs = TREE_CHAIN (attrs);
937 }
938
939 return specs;
940}
b30f223b 941\f
1ccf251f
RK
942/* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
943 a parameter list. */
944
945#define T_I &integer_type_node
946#define T_L &long_integer_type_node
2314fb70 947#define T_LL &long_long_integer_type_node
1ccf251f
RK
948#define T_S &short_integer_type_node
949#define T_UI &unsigned_type_node
950#define T_UL &long_unsigned_type_node
2314fb70 951#define T_ULL &long_long_unsigned_type_node
1ccf251f
RK
952#define T_US &short_unsigned_type_node
953#define T_F &float_type_node
954#define T_D &double_type_node
955#define T_LD &long_double_type_node
956#define T_C &char_type_node
957#define T_V &void_type_node
958#define T_W &wchar_type_node
df8a401a 959#define T_ST &sizetype
1ccf251f
RK
960
961typedef struct {
962 char *format_chars;
963 int pointer_count;
964 /* Type of argument if no length modifier is used. */
965 tree *nolen;
966 /* Type of argument if length modifier for shortening is used.
967 If NULL, then this modifier is not allowed. */
968 tree *hlen;
969 /* Type of argument if length modifier `l' is used.
970 If NULL, then this modifier is not allowed. */
971 tree *llen;
2cedb812 972 /* Type of argument if length modifier `q' or `ll' is used.
2314fb70
CH
973 If NULL, then this modifier is not allowed. */
974 tree *qlen;
1ccf251f
RK
975 /* Type of argument if length modifier `L' is used.
976 If NULL, then this modifier is not allowed. */
977 tree *bigllen;
978 /* List of other modifier characters allowed with these options. */
979 char *flag_chars;
980} format_char_info;
981
982static format_char_info print_char_table[] = {
2cedb812
RK
983 { "di", 0, T_I, T_I, T_L, T_LL, T_LL, "-wp0 +" },
984 { "oxX", 0, T_UI, T_UI, T_UL, T_ULL, T_ULL, "-wp0#" },
4cc00c5a 985 { "u", 0, T_UI, T_UI, T_UL, T_ULL, T_ULL, "-wp0" },
af3c5588 986/* Two GNU extensions. */
2314fb70 987 { "Z", 0, T_ST, NULL, NULL, NULL, NULL, "-wp0" },
2a13575e 988 { "m", 0, T_V, NULL, NULL, NULL, NULL, "-wp" },
6d819282 989 { "feEgGaA", 0, T_D, NULL, NULL, NULL, T_LD, "-wp0 +#" },
2314fb70
CH
990 { "c", 0, T_I, NULL, T_W, NULL, NULL, "-w" },
991 { "C", 0, T_W, NULL, NULL, NULL, NULL, "-w" },
992 { "s", 1, T_C, NULL, T_W, NULL, NULL, "-wp" },
993 { "S", 1, T_W, NULL, NULL, NULL, NULL, "-wp" },
994 { "p", 1, T_V, NULL, NULL, NULL, NULL, "-w" },
995 { "n", 1, T_I, T_S, T_L, T_LL, NULL, "" },
1ccf251f
RK
996 { NULL }
997};
998
999static format_char_info scan_char_table[] = {
2cedb812
RK
1000 { "di", 1, T_I, T_S, T_L, T_LL, T_LL, "*" },
1001 { "ouxX", 1, T_UI, T_US, T_UL, T_ULL, T_ULL, "*" },
6d819282 1002 { "efgEGaA", 1, T_F, NULL, T_D, NULL, T_LD, "*" },
2314fb70
CH
1003 { "sc", 1, T_C, NULL, T_W, NULL, NULL, "*a" },
1004 { "[", 1, T_C, NULL, NULL, NULL, NULL, "*a" },
1005 { "C", 1, T_W, NULL, NULL, NULL, NULL, "*" },
1006 { "S", 1, T_W, NULL, NULL, NULL, NULL, "*" },
1007 { "p", 2, T_V, NULL, NULL, NULL, NULL, "*" },
1008 { "n", 1, T_I, T_S, T_L, T_LL, NULL, "" },
1ccf251f
RK
1009 { NULL }
1010};
1011
0161e8da
RK
1012typedef struct function_format_info
1013{
1ccf251f
RK
1014 struct function_format_info *next; /* next structure on the list */
1015 tree name; /* identifier such as "printf" */
1016 tree assembler_name; /* optional mangled identifier (for C++) */
1017 int is_scan; /* TRUE if *scanf */
1018 int format_num; /* number of format argument */
1019 int first_arg_num; /* number of first arg (zero for varargs) */
1020} function_format_info;
1021
1022static function_format_info *function_format_list = NULL;
1023
0161e8da
RK
1024typedef struct international_format_info
1025{
1026 struct international_format_info *next; /* next structure on the list */
1027 tree name; /* identifier such as "gettext" */
1028 tree assembler_name; /* optional mangled identifier (for C++) */
1029 int format_num; /* number of format argument */
1030} international_format_info;
1031
1032static international_format_info *international_format_list = NULL;
1033
1034static void check_format_info PROTO((function_format_info *, tree));
1ccf251f
RK
1035
1036/* Initialize the table of functions to perform format checking on.
1037 The ANSI functions are always checked (whether <stdio.h> is
1038 included or not), since it is common to call printf without
1039 including <stdio.h>. There shouldn't be a problem with this,
1040 since ANSI reserves these function names whether you include the
0161e8da
RK
1041 header file or not. In any case, the checking is harmless.
1042
1043 Also initialize the name of function that modify the format string for
1044 internationalization purposes. */
1ccf251f
RK
1045
1046void
1047init_function_format_info ()
1048{
1049 record_function_format (get_identifier ("printf"), NULL_TREE, 0, 1, 2);
1050 record_function_format (get_identifier ("fprintf"), NULL_TREE, 0, 2, 3);
1051 record_function_format (get_identifier ("sprintf"), NULL_TREE, 0, 2, 3);
1052 record_function_format (get_identifier ("scanf"), NULL_TREE, 1, 1, 2);
1053 record_function_format (get_identifier ("fscanf"), NULL_TREE, 1, 2, 3);
1054 record_function_format (get_identifier ("sscanf"), NULL_TREE, 1, 2, 3);
1055 record_function_format (get_identifier ("vprintf"), NULL_TREE, 0, 1, 0);
1056 record_function_format (get_identifier ("vfprintf"), NULL_TREE, 0, 2, 0);
1057 record_function_format (get_identifier ("vsprintf"), NULL_TREE, 0, 2, 0);
0161e8da
RK
1058
1059 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1060 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1061 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1ccf251f
RK
1062}
1063
1064/* Record information for argument format checking. FUNCTION_IDENT is
1065 the identifier node for the name of the function to check (its decl
1066 need not exist yet). IS_SCAN is true for scanf-type format checking;
1067 false indicates printf-style format checking. FORMAT_NUM is the number
1068 of the argument which is the format control string (starting from 1).
1069 FIRST_ARG_NUM is the number of the first actual argument to check
0161e8da 1070 against the format string, or zero if no checking is not be done
1ccf251f
RK
1071 (e.g. for varargs such as vfprintf). */
1072
1073void
1074record_function_format (name, assembler_name, is_scan,
1075 format_num, first_arg_num)
1076 tree name;
1077 tree assembler_name;
1078 int is_scan;
1079 int format_num;
1080 int first_arg_num;
1081{
1082 function_format_info *info;
1083
1084 /* Re-use existing structure if it's there. */
1085
1086 for (info = function_format_list; info; info = info->next)
1087 {
1088 if (info->name == name && info->assembler_name == assembler_name)
1089 break;
1090 }
1091 if (! info)
1092 {
1093 info = (function_format_info *) xmalloc (sizeof (function_format_info));
1094 info->next = function_format_list;
1095 function_format_list = info;
1096
1097 info->name = name;
1098 info->assembler_name = assembler_name;
1099 }
1100
1101 info->is_scan = is_scan;
1102 info->format_num = format_num;
1103 info->first_arg_num = first_arg_num;
1104}
1105
0161e8da
RK
1106/* Record information for the names of function that modify the format
1107 argument to format functions. FUNCTION_IDENT is the identifier node for
1108 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1109 the number of the argument which is the format control string (starting
1110 from 1). */
1111
31837ce2 1112static void
0161e8da
RK
1113record_international_format (name, assembler_name, format_num)
1114 tree name;
1115 tree assembler_name;
1116 int format_num;
1117{
1118 international_format_info *info;
1119
1120 /* Re-use existing structure if it's there. */
1121
1122 for (info = international_format_list; info; info = info->next)
1123 {
1124 if (info->name == name && info->assembler_name == assembler_name)
1125 break;
1126 }
1127
1128 if (! info)
1129 {
1130 info
1131 = (international_format_info *)
1132 xmalloc (sizeof (international_format_info));
1133 info->next = international_format_list;
1134 international_format_list = info;
1135
1136 info->name = name;
1137 info->assembler_name = assembler_name;
1138 }
1139
1140 info->format_num = format_num;
1141}
1142
1ccf251f
RK
1143static char tfaff[] = "too few arguments for format";
1144\f
1145/* Check the argument list of a call to printf, scanf, etc.
1146 NAME is the function identifier.
1147 ASSEMBLER_NAME is the function's assembler identifier.
1148 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1149 PARAMS is the list of argument values. */
1150
1151void
1152check_function_format (name, assembler_name, params)
1153 tree name;
1154 tree assembler_name;
1155 tree params;
1156{
1157 function_format_info *info;
1158
1159 /* See if this function is a format function. */
1160 for (info = function_format_list; info; info = info->next)
1161 {
95216dec 1162 if (info->assembler_name
1ccf251f
RK
1163 ? (info->assembler_name == assembler_name)
1164 : (info->name == name))
1165 {
1166 /* Yup; check it. */
1167 check_format_info (info, params);
1168 break;
1169 }
1170 }
1171}
1172
1173/* Check the argument list of a call to printf, scanf, etc.
1174 INFO points to the function_format_info structure.
1175 PARAMS is the list of argument values. */
1176
1177static void
1178check_format_info (info, params)
1179 function_format_info *info;
1180 tree params;
1181{
1182 int i;
1183 int arg_num;
1184 int suppressed, wide, precise;
1185 int length_char;
1186 int format_char;
1187 int format_length;
1188 tree format_tree;
1189 tree cur_param;
1190 tree cur_type;
1191 tree wanted_type;
9b69f523 1192 tree first_fillin_param;
1ccf251f
RK
1193 char *format_chars;
1194 format_char_info *fci;
1195 static char message[132];
1196 char flag_chars[8];
9b69f523 1197 int has_operand_number = 0;
1ccf251f
RK
1198
1199 /* Skip to format argument. If the argument isn't available, there's
1200 no work for us to do; prototype checking will catch the problem. */
1201 for (arg_num = 1; ; ++arg_num)
1202 {
1203 if (params == 0)
1204 return;
1205 if (arg_num == info->format_num)
1206 break;
1207 params = TREE_CHAIN (params);
1208 }
1209 format_tree = TREE_VALUE (params);
1210 params = TREE_CHAIN (params);
1211 if (format_tree == 0)
1212 return;
0161e8da 1213
1ccf251f
RK
1214 /* We can only check the format if it's a string constant. */
1215 while (TREE_CODE (format_tree) == NOP_EXPR)
1216 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
0161e8da
RK
1217
1218 if (TREE_CODE (format_tree) == CALL_EXPR
1219 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1220 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1221 == FUNCTION_DECL))
1222 {
1223 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1224
1225 /* See if this is a call to a known internationalization function
1226 that modifies the format arg. */
1227 international_format_info *info;
1228
1229 for (info = international_format_list; info; info = info->next)
1230 if (info->assembler_name
1231 ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
1232 : (info->name == DECL_NAME (function)))
1233 {
1234 tree inner_args;
1235 int i;
1236
1237 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
1238 inner_args != 0;
1239 inner_args = TREE_CHAIN (inner_args), i++)
1240 if (i == info->format_num)
1241 {
1242 format_tree = TREE_VALUE (inner_args);
1243
1244 while (TREE_CODE (format_tree) == NOP_EXPR)
1245 format_tree = TREE_OPERAND (format_tree, 0);
1246 }
1247 }
1248 }
1249
39b751ce 1250 if (integer_zerop (format_tree))
1ccf251f
RK
1251 {
1252 warning ("null format string");
1253 return;
1254 }
1255 if (TREE_CODE (format_tree) != ADDR_EXPR)
1256 return;
1257 format_tree = TREE_OPERAND (format_tree, 0);
1258 if (TREE_CODE (format_tree) != STRING_CST)
1259 return;
1260 format_chars = TREE_STRING_POINTER (format_tree);
1261 format_length = TREE_STRING_LENGTH (format_tree);
1262 if (format_length <= 1)
1263 warning ("zero-length format string");
1264 if (format_chars[--format_length] != 0)
1265 {
1266 warning ("unterminated format string");
1267 return;
1268 }
1269 /* Skip to first argument to check. */
1270 while (arg_num + 1 < info->first_arg_num)
1271 {
1272 if (params == 0)
1273 return;
1274 params = TREE_CHAIN (params);
1275 ++arg_num;
1276 }
9b69f523
RK
1277
1278 first_fillin_param = params;
1ccf251f
RK
1279 while (1)
1280 {
af3c5588 1281 int aflag;
1ccf251f
RK
1282 if (*format_chars == 0)
1283 {
1284 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
1285 warning ("embedded `\\0' in format");
9b69f523 1286 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
1ccf251f
RK
1287 warning ("too many arguments for format");
1288 return;
1289 }
1290 if (*format_chars++ != '%')
1291 continue;
1292 if (*format_chars == 0)
1293 {
1294 warning ("spurious trailing `%%' in format");
1295 continue;
1296 }
1297 if (*format_chars == '%')
1298 {
1299 ++format_chars;
1300 continue;
1301 }
1302 flag_chars[0] = 0;
1303 suppressed = wide = precise = FALSE;
1304 if (info->is_scan)
1305 {
1306 suppressed = *format_chars == '*';
1307 if (suppressed)
1308 ++format_chars;
1309 while (isdigit (*format_chars))
1310 ++format_chars;
1311 }
1312 else
1313 {
9b69f523
RK
1314 /* See if we have a number followed by a dollar sign. If we do,
1315 it is an operand number, so set PARAMS to that operand. */
1316 if (*format_chars >= '0' && *format_chars <= '9')
1317 {
1318 char *p = format_chars;
1319
1320 while (*p >= '0' && *p++ <= '9')
1321 ;
1322
1323 if (*p == '$')
1324 {
1325 int opnum = atoi (format_chars);
1326
1327 params = first_fillin_param;
1328 format_chars = p + 1;
1329 has_operand_number = 1;
1330
1331 for (i = 1; i < opnum && params != 0; i++)
1332 params = TREE_CHAIN (params);
1333
1334 if (opnum == 0 || params == 0)
1335 {
1336 warning ("operand number out of range in format");
1337 return;
1338 }
1339 }
1340 }
1341
1ccf251f
RK
1342 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
1343 {
1344 if (index (flag_chars, *format_chars) != 0)
1345 {
1346 sprintf (message, "repeated `%c' flag in format",
1347 *format_chars);
1348 warning (message);
1349 }
6d819282
MK
1350 else
1351 {
1352 i = strlen (flag_chars);
1353 flag_chars[i++] = *format_chars++;
1354 flag_chars[i] = 0;
1355 }
1ccf251f
RK
1356 }
1357 /* "If the space and + flags both appear,
1358 the space flag will be ignored." */
1359 if (index (flag_chars, ' ') != 0
1360 && index (flag_chars, '+') != 0)
1361 warning ("use of both ` ' and `+' flags in format");
1362 /* "If the 0 and - flags both appear,
1363 the 0 flag will be ignored." */
1364 if (index (flag_chars, '0') != 0
1365 && index (flag_chars, '-') != 0)
1366 warning ("use of both `0' and `-' flags in format");
1367 if (*format_chars == '*')
1368 {
1369 wide = TRUE;
1370 /* "...a field width...may be indicated by an asterisk.
1371 In this case, an int argument supplies the field width..." */
1372 ++format_chars;
1373 if (params == 0)
1374 {
1375 warning (tfaff);
1376 return;
1377 }
1378 if (info->first_arg_num != 0)
1379 {
1380 cur_param = TREE_VALUE (params);
1381 params = TREE_CHAIN (params);
1382 ++arg_num;
1383 /* size_t is generally not valid here.
1384 It will work on most machines, because size_t and int
1385 have the same mode. But might as well warn anyway,
1386 since it will fail on other machines. */
309ffab6
RS
1387 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1388 != integer_type_node)
1389 &&
1390 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1391 != unsigned_type_node))
1ccf251f
RK
1392 {
1393 sprintf (message,
1394 "field width is not type int (arg %d)",
1395 arg_num);
1396 warning (message);
1397 }
1398 }
1399 }
1400 else
1401 {
1402 while (isdigit (*format_chars))
1403 {
1404 wide = TRUE;
1405 ++format_chars;
1406 }
1407 }
1408 if (*format_chars == '.')
1409 {
1410 precise = TRUE;
1411 ++format_chars;
1412 if (*format_chars != '*' && !isdigit (*format_chars))
1413 warning ("`.' not followed by `*' or digit in format");
1414 /* "...a...precision...may be indicated by an asterisk.
1415 In this case, an int argument supplies the...precision." */
1416 if (*format_chars == '*')
1417 {
1418 if (info->first_arg_num != 0)
1419 {
1420 ++format_chars;
1421 if (params == 0)
1422 {
1423 warning (tfaff);
1424 return;
1425 }
1426 cur_param = TREE_VALUE (params);
1427 params = TREE_CHAIN (params);
1428 ++arg_num;
1429 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1430 != integer_type_node)
1431 {
1432 sprintf (message,
1433 "field width is not type int (arg %d)",
1434 arg_num);
1435 warning (message);
1436 }
1437 }
1438 }
1439 else
1440 {
1441 while (isdigit (*format_chars))
1442 ++format_chars;
1443 }
1444 }
1445 }
80d390a2
RK
1446 if (*format_chars == 'h' || *format_chars == 'l')
1447 length_char = *format_chars++;
1448 else if (*format_chars == 'q' || *format_chars == 'L')
15cf7c83
RK
1449 {
1450 length_char = *format_chars++;
1451 if (pedantic)
80d390a2
RK
1452 pedwarn ("ANSI C does not support the `%c' length modifier",
1453 length_char);
15cf7c83 1454 }
1ccf251f
RK
1455 else
1456 length_char = 0;
2cedb812 1457 if (length_char == 'l' && *format_chars == 'l')
15cf7c83
RK
1458 {
1459 length_char = 'q', format_chars++;
1460 if (pedantic)
1461 pedwarn ("ANSI C does not support the `ll' length modifier");
1462 }
af3c5588 1463 aflag = 0;
6d819282 1464 if (*format_chars == 'a' && info->is_scan)
af3c5588 1465 {
6d819282
MK
1466 if (format_chars[1] == 's' || format_chars[1] == 'S'
1467 || format_chars[1] == '[')
1468 {
1469 /* `a' is used as a flag. */
1470 aflag = 1;
1471 format_chars++;
1472 }
af3c5588 1473 }
1ccf251f
RK
1474 if (suppressed && length_char != 0)
1475 {
1476 sprintf (message,
1477 "use of `*' and `%c' together in format",
1478 length_char);
1479 warning (message);
1480 }
1481 format_char = *format_chars;
69c3716d 1482 if (format_char == 0 || format_char == '%')
1ccf251f
RK
1483 {
1484 warning ("conversion lacks type at end of format");
1485 continue;
1486 }
1487 format_chars++;
1488 fci = info->is_scan ? scan_char_table : print_char_table;
1489 while (fci->format_chars != 0
1490 && index (fci->format_chars, format_char) == 0)
1491 ++fci;
1492 if (fci->format_chars == 0)
1493 {
1494 if (format_char >= 040 && format_char < 0177)
1495 sprintf (message,
1496 "unknown conversion type character `%c' in format",
1497 format_char);
1498 else
1499 sprintf (message,
1500 "unknown conversion type character 0x%x in format",
1501 format_char);
1502 warning (message);
1503 continue;
1504 }
1505 if (wide && index (fci->flag_chars, 'w') == 0)
1506 {
1507 sprintf (message, "width used with `%c' format",
1508 format_char);
1509 warning (message);
1510 }
1511 if (precise && index (fci->flag_chars, 'p') == 0)
1512 {
1513 sprintf (message, "precision used with `%c' format",
1514 format_char);
1515 warning (message);
af3c5588
RK
1516 }
1517 if (aflag && index (fci->flag_chars, 'a') == 0)
1518 {
1519 sprintf (message, "`a' flag used with `%c' format",
1520 format_char);
1521 warning (message);
f9dcab52
RK
1522 /* To simplify the following code. */
1523 aflag = 0;
1ccf251f
RK
1524 }
1525 if (info->is_scan && format_char == '[')
1526 {
1527 /* Skip over scan set, in case it happens to have '%' in it. */
1528 if (*format_chars == '^')
1529 ++format_chars;
1530 /* Find closing bracket; if one is hit immediately, then
1531 it's part of the scan set rather than a terminator. */
1532 if (*format_chars == ']')
1533 ++format_chars;
1534 while (*format_chars && *format_chars != ']')
1535 ++format_chars;
1536 if (*format_chars != ']')
1537 /* The end of the format string was reached. */
1538 warning ("no closing `]' for `%%[' format");
1539 }
1540 if (suppressed)
1541 {
1542 if (index (fci->flag_chars, '*') == 0)
1543 {
1544 sprintf (message,
1545 "suppression of `%c' conversion in format",
1546 format_char);
1547 warning (message);
1548 }
1549 continue;
1550 }
1551 for (i = 0; flag_chars[i] != 0; ++i)
1552 {
1553 if (index (fci->flag_chars, flag_chars[i]) == 0)
1554 {
1555 sprintf (message, "flag `%c' used with type `%c'",
1556 flag_chars[i], format_char);
1557 warning (message);
1558 }
1559 }
1560 if (precise && index (flag_chars, '0') != 0
1561 && (format_char == 'd' || format_char == 'i'
1562 || format_char == 'o' || format_char == 'u'
1563 || format_char == 'x' || format_char == 'x'))
1564 {
1565 sprintf (message,
e5ee0060 1566 "`0' flag ignored with precision specifier and `%c' format",
1ccf251f
RK
1567 format_char);
1568 warning (message);
1569 }
1570 switch (length_char)
1571 {
1572 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
1573 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1574 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
2314fb70 1575 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1ccf251f
RK
1576 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
1577 }
1578 if (wanted_type == 0)
1579 {
1580 sprintf (message,
1581 "use of `%c' length character with `%c' type character",
1582 length_char, format_char);
1583 warning (message);
1584 }
1585
1586 /*
1587 ** XXX -- should kvetch about stuff such as
1588 ** {
1589 ** const int i;
1590 **
1591 ** scanf ("%d", &i);
1592 ** }
1593 */
1594
1595 /* Finally. . .check type of argument against desired type! */
1596 if (info->first_arg_num == 0)
1597 continue;
2a13575e
RK
1598 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1599 /* This specifier takes no argument. */
1600 continue;
1ccf251f
RK
1601 if (params == 0)
1602 {
1603 warning (tfaff);
1604 return;
1605 }
1606 cur_param = TREE_VALUE (params);
1607 params = TREE_CHAIN (params);
1608 ++arg_num;
1609 cur_type = TREE_TYPE (cur_param);
1610
6d819282
MK
1611 STRIP_NOPS (cur_param);
1612
1ccf251f
RK
1613 /* Check the types of any additional pointer arguments
1614 that precede the "real" argument. */
f9dcab52 1615 for (i = 0; i < fci->pointer_count + aflag; ++i)
1ccf251f
RK
1616 {
1617 if (TREE_CODE (cur_type) == POINTER_TYPE)
1618 {
1619 cur_type = TREE_TYPE (cur_type);
6d819282
MK
1620
1621 if (TREE_CODE (cur_param) == ADDR_EXPR)
1622 cur_param = TREE_OPERAND (cur_param, 0);
1623 else
1624 cur_param = 0;
1625
1ccf251f
RK
1626 continue;
1627 }
87416640
RK
1628 if (TREE_CODE (cur_type) != ERROR_MARK)
1629 {
1630 sprintf (message,
1631 "format argument is not a %s (arg %d)",
f9dcab52
RK
1632 ((fci->pointer_count + aflag == 1)
1633 ? "pointer" : "pointer to a pointer"),
87416640
RK
1634 arg_num);
1635 warning (message);
1636 }
1ccf251f
RK
1637 break;
1638 }
1639
6d819282
MK
1640 /* See if this is an attempt to write into a const type with
1641 scanf. */
1642 if (info->is_scan && i == fci->pointer_count + aflag
1643 && wanted_type != 0
1644 && TREE_CODE (cur_type) != ERROR_MARK
1645 && (TYPE_READONLY (cur_type)
1646 || (cur_param != 0
1647 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
1648 || (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'd'
1649 && TREE_READONLY (cur_param))))))
1650 {
1651 sprintf (message, "writing into constant object (arg %d)", arg_num);
1652 warning (message);
1653 }
1654
1ccf251f 1655 /* Check the type of the "real" argument, if there's a type we want. */
f9dcab52 1656 if (i == fci->pointer_count + aflag && wanted_type != 0
87416640 1657 && TREE_CODE (cur_type) != ERROR_MARK
1ccf251f
RK
1658 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1659 /* If we want `void *', allow any pointer type.
1660 (Anything else would already have got a warning.) */
1661 && ! (wanted_type == void_type_node
1662 && fci->pointer_count > 0)
1663 /* Don't warn about differences merely in signedness. */
1664 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
b7c9c707 1665 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
4215e498 1666 && (TREE_UNSIGNED (wanted_type)
f5775325
RK
1667 ? wanted_type == (cur_type = unsigned_type (cur_type))
1668 : wanted_type == (cur_type = signed_type (cur_type))))
60e02b1e
RK
1669 /* Likewise, "signed char", "unsigned char" and "char" are
1670 equivalent but the above test won't consider them equivalent. */
1671 && ! (wanted_type == char_type_node
b7c9c707
RS
1672 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
1673 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
1ccf251f
RK
1674 {
1675 register char *this;
1676 register char *that;
1677
1678 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
1679 that = 0;
1680 if (TREE_CODE (cur_type) != ERROR_MARK
1681 && TYPE_NAME (cur_type) != 0
1682 && TREE_CODE (cur_type) != INTEGER_TYPE
1683 && !(TREE_CODE (cur_type) == POINTER_TYPE
1684 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
1685 {
1686 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
1687 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
1688 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1689 else
1690 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
1691 }
1692
1693 /* A nameless type can't possibly match what the format wants.
1694 So there will be a warning for it.
1695 Make up a string to describe vaguely what it is. */
1696 if (that == 0)
1697 {
1698 if (TREE_CODE (cur_type) == POINTER_TYPE)
1699 that = "pointer";
1700 else
1701 that = "different type";
1702 }
1703
309ffab6
RS
1704 /* Make the warning better in case of mismatch of int vs long. */
1705 if (TREE_CODE (cur_type) == INTEGER_TYPE
1706 && TREE_CODE (wanted_type) == INTEGER_TYPE
1707 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
1708 && TYPE_NAME (cur_type) != 0
1709 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
1710 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1711
1ccf251f
RK
1712 if (strcmp (this, that) != 0)
1713 {
1714 sprintf (message, "%s format, %s arg (arg %d)",
1715 this, that, arg_num);
1716 warning (message);
1717 }
1718 }
1719 }
1720}
1721\f
d74154d5
RS
1722/* Print a warning if a constant expression had overflow in folding.
1723 Invoke this function on every expression that the language
1724 requires to be a constant expression.
1725 Note the ANSI C standard says it is erroneous for a
1726 constant expression to overflow. */
96571883
BK
1727
1728void
1729constant_expression_warning (value)
1730 tree value;
1731{
c05f751c
RK
1732 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1733 || TREE_CODE (value) == COMPLEX_CST)
1734 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1735 pedwarn ("overflow in constant expression");
d74154d5
RS
1736}
1737
1738/* Print a warning if an expression had overflow in folding.
1739 Invoke this function on every expression that
1740 (1) appears in the source code, and
1741 (2) might be a constant expression that overflowed, and
1742 (3) is not already checked by convert_and_check;
1743 however, do not invoke this function on operands of explicit casts. */
1744
1745void
1746overflow_warning (value)
1747 tree value;
1748{
c05f751c
RK
1749 if ((TREE_CODE (value) == INTEGER_CST
1750 || (TREE_CODE (value) == COMPLEX_CST
1751 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1752 && TREE_OVERFLOW (value))
d74154d5 1753 {
7193bce2 1754 TREE_OVERFLOW (value) = 0;
e78a3b42
RK
1755 if (skip_evaluation == 0)
1756 warning ("integer overflow in expression");
d74154d5 1757 }
c05f751c
RK
1758 else if ((TREE_CODE (value) == REAL_CST
1759 || (TREE_CODE (value) == COMPLEX_CST
1760 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1761 && TREE_OVERFLOW (value))
1762 {
1763 TREE_OVERFLOW (value) = 0;
e78a3b42
RK
1764 if (skip_evaluation == 0)
1765 warning ("floating point overflow in expression");
c05f751c 1766 }
d74154d5
RS
1767}
1768
1769/* Print a warning if a large constant is truncated to unsigned,
1770 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1771 Invoke this function on every expression that might be implicitly
1772 converted to an unsigned type. */
1773
1774void
1775unsigned_conversion_warning (result, operand)
1776 tree result, operand;
1777{
1778 if (TREE_CODE (operand) == INTEGER_CST
1779 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
1780 && TREE_UNSIGNED (TREE_TYPE (result))
e78a3b42 1781 && skip_evaluation == 0
d74154d5
RS
1782 && !int_fits_type_p (operand, TREE_TYPE (result)))
1783 {
1784 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
1785 /* This detects cases like converting -129 or 256 to unsigned char. */
90c939d4 1786 warning ("large integer implicitly truncated to unsigned type");
d74154d5 1787 else if (warn_conversion)
90c939d4 1788 warning ("negative integer implicitly converted to unsigned type");
d74154d5
RS
1789 }
1790}
1791
1792/* Convert EXPR to TYPE, warning about conversion problems with constants.
1793 Invoke this function on every expression that is converted implicitly,
1794 i.e. because of language rules and not because of an explicit cast. */
1795
1796tree
1797convert_and_check (type, expr)
1798 tree type, expr;
1799{
1800 tree t = convert (type, expr);
1801 if (TREE_CODE (t) == INTEGER_CST)
1802 {
7193bce2 1803 if (TREE_OVERFLOW (t))
d74154d5 1804 {
7193bce2
PE
1805 TREE_OVERFLOW (t) = 0;
1806
868fc750
RK
1807 /* Do not diagnose overflow in a constant expression merely
1808 because a conversion overflowed. */
1809 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
1810
7193bce2
PE
1811 /* No warning for converting 0x80000000 to int. */
1812 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
1813 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1814 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
22ba338b
RS
1815 /* If EXPR fits in the unsigned version of TYPE,
1816 don't warn unless pedantic. */
e78a3b42
RK
1817 if ((pedantic
1818 || TREE_UNSIGNED (type)
1819 || ! int_fits_type_p (expr, unsigned_type (type)))
1820 && skip_evaluation == 0)
1821 warning ("overflow in implicit constant conversion");
d74154d5
RS
1822 }
1823 else
1824 unsigned_conversion_warning (t, expr);
1825 }
1826 return t;
96571883
BK
1827}
1828\f
b30f223b
RS
1829void
1830c_expand_expr_stmt (expr)
1831 tree expr;
1832{
1833 /* Do default conversion if safe and possibly important,
1834 in case within ({...}). */
1835 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
1836 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1837 expr = default_conversion (expr);
1838
1839 if (TREE_TYPE (expr) != error_mark_node
1840 && TYPE_SIZE (TREE_TYPE (expr)) == 0
1841 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1842 error ("expression statement has incomplete type");
1843
1844 expand_expr_stmt (expr);
1845}
1846\f
1847/* Validate the expression after `case' and apply default promotions. */
1848
1849tree
1850check_case_value (value)
1851 tree value;
1852{
1853 if (value == NULL_TREE)
1854 return value;
1855
1856 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8493738b 1857 STRIP_TYPE_NOPS (value);
b30f223b
RS
1858
1859 if (TREE_CODE (value) != INTEGER_CST
1860 && value != error_mark_node)
1861 {
1862 error ("case label does not reduce to an integer constant");
1863 value = error_mark_node;
1864 }
1865 else
1866 /* Promote char or short to int. */
1867 value = default_conversion (value);
1868
bc690db1
RS
1869 constant_expression_warning (value);
1870
b30f223b
RS
1871 return value;
1872}
1873\f
1874/* Return an integer type with BITS bits of precision,
1875 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1876
1877tree
1878type_for_size (bits, unsignedp)
1879 unsigned bits;
1880 int unsignedp;
1881{
a311b52c
JM
1882 if (bits == TYPE_PRECISION (integer_type_node))
1883 return unsignedp ? unsigned_type_node : integer_type_node;
1884
3fc7e390 1885 if (bits == TYPE_PRECISION (signed_char_type_node))
b30f223b
RS
1886 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1887
3fc7e390 1888 if (bits == TYPE_PRECISION (short_integer_type_node))
b30f223b
RS
1889 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1890
3fc7e390 1891 if (bits == TYPE_PRECISION (long_integer_type_node))
b30f223b
RS
1892 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1893
3fc7e390 1894 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b30f223b
RS
1895 return (unsignedp ? long_long_unsigned_type_node
1896 : long_long_integer_type_node);
1897
3fc7e390
RS
1898 if (bits <= TYPE_PRECISION (intQI_type_node))
1899 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1900
1901 if (bits <= TYPE_PRECISION (intHI_type_node))
1902 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1903
1904 if (bits <= TYPE_PRECISION (intSI_type_node))
1905 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1906
1907 if (bits <= TYPE_PRECISION (intDI_type_node))
1908 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1909
b30f223b
RS
1910 return 0;
1911}
1912
1913/* Return a data type that has machine mode MODE.
1914 If the mode is an integer,
1915 then UNSIGNEDP selects between signed and unsigned types. */
1916
1917tree
1918type_for_mode (mode, unsignedp)
1919 enum machine_mode mode;
1920 int unsignedp;
1921{
a311b52c
JM
1922 if (mode == TYPE_MODE (integer_type_node))
1923 return unsignedp ? unsigned_type_node : integer_type_node;
1924
b30f223b
RS
1925 if (mode == TYPE_MODE (signed_char_type_node))
1926 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1927
1928 if (mode == TYPE_MODE (short_integer_type_node))
1929 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1930
b30f223b
RS
1931 if (mode == TYPE_MODE (long_integer_type_node))
1932 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1933
1934 if (mode == TYPE_MODE (long_long_integer_type_node))
1935 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
1936
3fc7e390
RS
1937 if (mode == TYPE_MODE (intQI_type_node))
1938 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1939
1940 if (mode == TYPE_MODE (intHI_type_node))
1941 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1942
1943 if (mode == TYPE_MODE (intSI_type_node))
1944 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1945
1946 if (mode == TYPE_MODE (intDI_type_node))
1947 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1948
b30f223b
RS
1949 if (mode == TYPE_MODE (float_type_node))
1950 return float_type_node;
1951
1952 if (mode == TYPE_MODE (double_type_node))
1953 return double_type_node;
1954
1955 if (mode == TYPE_MODE (long_double_type_node))
1956 return long_double_type_node;
1957
1958 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
1959 return build_pointer_type (char_type_node);
1960
1961 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
1962 return build_pointer_type (integer_type_node);
1963
1964 return 0;
1965}
1966\f
6acfe908
JM
1967/* Return the minimum number of bits needed to represent VALUE in a
1968 signed or unsigned type, UNSIGNEDP says which. */
1969
1970int
1971min_precision (value, unsignedp)
1972 tree value;
1973 int unsignedp;
1974{
1975 int log;
1976
1977 /* If the value is negative, compute its negative minus 1. The latter
1978 adjustment is because the absolute value of the largest negative value
1979 is one larger than the largest positive value. This is equivalent to
1980 a bit-wise negation, so use that operation instead. */
1981
1982 if (tree_int_cst_sgn (value) < 0)
1983 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
1984
1985 /* Return the number of bits needed, taking into account the fact
1986 that we need one more bit for a signed than unsigned type. */
1987
1988 if (integer_zerop (value))
1989 log = 0;
1990 else if (TREE_INT_CST_HIGH (value) != 0)
1991 log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
1992 else
1993 log = floor_log2 (TREE_INT_CST_LOW (value));
1994
1995 return log + 1 + ! unsignedp;
1996}
1997\f
b30f223b
RS
1998/* Print an error message for invalid operands to arith operation CODE.
1999 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2000
2001void
2002binary_op_error (code)
2003 enum tree_code code;
2004{
6d819282 2005 register char *opname;
89c78d7d 2006
b30f223b
RS
2007 switch (code)
2008 {
2009 case NOP_EXPR:
2010 error ("invalid truth-value expression");
2011 return;
2012
2013 case PLUS_EXPR:
2014 opname = "+"; break;
2015 case MINUS_EXPR:
2016 opname = "-"; break;
2017 case MULT_EXPR:
2018 opname = "*"; break;
2019 case MAX_EXPR:
2020 opname = "max"; break;
2021 case MIN_EXPR:
2022 opname = "min"; break;
2023 case EQ_EXPR:
2024 opname = "=="; break;
2025 case NE_EXPR:
2026 opname = "!="; break;
2027 case LE_EXPR:
2028 opname = "<="; break;
2029 case GE_EXPR:
2030 opname = ">="; break;
2031 case LT_EXPR:
2032 opname = "<"; break;
2033 case GT_EXPR:
2034 opname = ">"; break;
2035 case LSHIFT_EXPR:
2036 opname = "<<"; break;
2037 case RSHIFT_EXPR:
2038 opname = ">>"; break;
2039 case TRUNC_MOD_EXPR:
047de90b 2040 case FLOOR_MOD_EXPR:
b30f223b
RS
2041 opname = "%"; break;
2042 case TRUNC_DIV_EXPR:
047de90b 2043 case FLOOR_DIV_EXPR:
b30f223b
RS
2044 opname = "/"; break;
2045 case BIT_AND_EXPR:
2046 opname = "&"; break;
2047 case BIT_IOR_EXPR:
2048 opname = "|"; break;
2049 case TRUTH_ANDIF_EXPR:
2050 opname = "&&"; break;
2051 case TRUTH_ORIF_EXPR:
2052 opname = "||"; break;
2053 case BIT_XOR_EXPR:
2054 opname = "^"; break;
047de90b
RS
2055 case LROTATE_EXPR:
2056 case RROTATE_EXPR:
2057 opname = "rotate"; break;
6d819282
MK
2058 default:
2059 opname = "unknown"; break;
b30f223b
RS
2060 }
2061 error ("invalid operands to binary %s", opname);
2062}
2063\f
2064/* Subroutine of build_binary_op, used for comparison operations.
2065 See if the operands have both been converted from subword integer types
2066 and, if so, perhaps change them both back to their original type.
94dccd9d
RS
2067 This function is also responsible for converting the two operands
2068 to the proper common type for comparison.
b30f223b
RS
2069
2070 The arguments of this function are all pointers to local variables
2071 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2072 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2073
2074 If this function returns nonzero, it means that the comparison has
2075 a constant value. What this function returns is an expression for
2076 that value. */
2077
2078tree
2079shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2080 tree *op0_ptr, *op1_ptr;
2081 tree *restype_ptr;
2082 enum tree_code *rescode_ptr;
2083{
2084 register tree type;
2085 tree op0 = *op0_ptr;
2086 tree op1 = *op1_ptr;
2087 int unsignedp0, unsignedp1;
2088 int real1, real2;
2089 tree primop0, primop1;
2090 enum tree_code code = *rescode_ptr;
2091
2092 /* Throw away any conversions to wider types
2093 already present in the operands. */
2094
2095 primop0 = get_narrower (op0, &unsignedp0);
2096 primop1 = get_narrower (op1, &unsignedp1);
2097
2098 /* Handle the case that OP0 does not *contain* a conversion
2099 but it *requires* conversion to FINAL_TYPE. */
2100
2101 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2102 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2103 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2104 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2105
2106 /* If one of the operands must be floated, we cannot optimize. */
2107 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2108 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2109
2110 /* If first arg is constant, swap the args (changing operation
5af6001b
RK
2111 so value is preserved), for canonicalization. Don't do this if
2112 the second arg is 0. */
b30f223b 2113
5af6001b
RK
2114 if (TREE_CONSTANT (primop0)
2115 && ! integer_zerop (primop1) && ! real_zerop (primop1))
b30f223b
RS
2116 {
2117 register tree tem = primop0;
2118 register int temi = unsignedp0;
2119 primop0 = primop1;
2120 primop1 = tem;
2121 tem = op0;
2122 op0 = op1;
2123 op1 = tem;
2124 *op0_ptr = op0;
2125 *op1_ptr = op1;
2126 unsignedp0 = unsignedp1;
2127 unsignedp1 = temi;
2128 temi = real1;
2129 real1 = real2;
2130 real2 = temi;
2131
2132 switch (code)
2133 {
2134 case LT_EXPR:
2135 code = GT_EXPR;
2136 break;
2137 case GT_EXPR:
2138 code = LT_EXPR;
2139 break;
2140 case LE_EXPR:
2141 code = GE_EXPR;
2142 break;
2143 case GE_EXPR:
2144 code = LE_EXPR;
2145 break;
6d819282
MK
2146 default:
2147 break;
b30f223b
RS
2148 }
2149 *rescode_ptr = code;
2150 }
2151
2152 /* If comparing an integer against a constant more bits wide,
2153 maybe we can deduce a value of 1 or 0 independent of the data.
2154 Or else truncate the constant now
2155 rather than extend the variable at run time.
2156
2157 This is only interesting if the constant is the wider arg.
2158 Also, it is not safe if the constant is unsigned and the
2159 variable arg is signed, since in this case the variable
2160 would be sign-extended and then regarded as unsigned.
2161 Our technique fails in this case because the lowest/highest
2162 possible unsigned results don't follow naturally from the
2163 lowest/highest possible values of the variable operand.
2164 For just EQ_EXPR and NE_EXPR there is another technique that
2165 could be used: see if the constant can be faithfully represented
2166 in the other operand's type, by truncating it and reextending it
2167 and see if that preserves the constant's value. */
2168
2169 if (!real1 && !real2
2170 && TREE_CODE (primop1) == INTEGER_CST
2171 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2172 {
2173 int min_gt, max_gt, min_lt, max_lt;
2174 tree maxval, minval;
2175 /* 1 if comparison is nominally unsigned. */
2176 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2177 tree val;
2178
2179 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2180
2181 maxval = TYPE_MAX_VALUE (type);
2182 minval = TYPE_MIN_VALUE (type);
2183
2184 if (unsignedp && !unsignedp0)
2185 *restype_ptr = signed_type (*restype_ptr);
2186
2187 if (TREE_TYPE (primop1) != *restype_ptr)
2188 primop1 = convert (*restype_ptr, primop1);
2189 if (type != *restype_ptr)
2190 {
2191 minval = convert (*restype_ptr, minval);
2192 maxval = convert (*restype_ptr, maxval);
2193 }
2194
2195 if (unsignedp && unsignedp0)
2196 {
2197 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2198 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2199 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2200 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2201 }
2202 else
2203 {
2204 min_gt = INT_CST_LT (primop1, minval);
2205 max_gt = INT_CST_LT (primop1, maxval);
2206 min_lt = INT_CST_LT (minval, primop1);
2207 max_lt = INT_CST_LT (maxval, primop1);
2208 }
2209
2210 val = 0;
2211 /* This used to be a switch, but Genix compiler can't handle that. */
2212 if (code == NE_EXPR)
2213 {
2214 if (max_lt || min_gt)
a360da3a 2215 val = boolean_true_node;
b30f223b
RS
2216 }
2217 else if (code == EQ_EXPR)
2218 {
2219 if (max_lt || min_gt)
a360da3a 2220 val = boolean_false_node;
b30f223b
RS
2221 }
2222 else if (code == LT_EXPR)
2223 {
2224 if (max_lt)
a360da3a 2225 val = boolean_true_node;
b30f223b 2226 if (!min_lt)
a360da3a 2227 val = boolean_false_node;
b30f223b
RS
2228 }
2229 else if (code == GT_EXPR)
2230 {
2231 if (min_gt)
a360da3a 2232 val = boolean_true_node;
b30f223b 2233 if (!max_gt)
a360da3a 2234 val = boolean_false_node;
b30f223b
RS
2235 }
2236 else if (code == LE_EXPR)
2237 {
2238 if (!max_gt)
a360da3a 2239 val = boolean_true_node;
b30f223b 2240 if (min_gt)
a360da3a 2241 val = boolean_false_node;
b30f223b
RS
2242 }
2243 else if (code == GE_EXPR)
2244 {
2245 if (!min_lt)
a360da3a 2246 val = boolean_true_node;
b30f223b 2247 if (max_lt)
a360da3a 2248 val = boolean_false_node;
b30f223b
RS
2249 }
2250
2251 /* If primop0 was sign-extended and unsigned comparison specd,
2252 we did a signed comparison above using the signed type bounds.
2253 But the comparison we output must be unsigned.
2254
2255 Also, for inequalities, VAL is no good; but if the signed
2256 comparison had *any* fixed result, it follows that the
2257 unsigned comparison just tests the sign in reverse
2258 (positive values are LE, negative ones GE).
2259 So we can generate an unsigned comparison
2260 against an extreme value of the signed type. */
2261
2262 if (unsignedp && !unsignedp0)
2263 {
2264 if (val != 0)
2265 switch (code)
2266 {
2267 case LT_EXPR:
2268 case GE_EXPR:
2269 primop1 = TYPE_MIN_VALUE (type);
2270 val = 0;
2271 break;
2272
2273 case LE_EXPR:
2274 case GT_EXPR:
2275 primop1 = TYPE_MAX_VALUE (type);
2276 val = 0;
2277 break;
6d819282
MK
2278
2279 default:
2280 break;
b30f223b
RS
2281 }
2282 type = unsigned_type (type);
2283 }
2284
b7c9c707 2285 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
b30f223b
RS
2286 {
2287 /* This is the case of (char)x >?< 0x80, which people used to use
2288 expecting old C compilers to change the 0x80 into -0x80. */
a360da3a 2289 if (val == boolean_false_node)
b30f223b 2290 warning ("comparison is always 0 due to limited range of data type");
a360da3a 2291 if (val == boolean_true_node)
b30f223b
RS
2292 warning ("comparison is always 1 due to limited range of data type");
2293 }
2294
b7c9c707 2295 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
b30f223b 2296 {
1e276c4a 2297 /* This is the case of (unsigned char)x >?< -1 or < 0. */
a360da3a 2298 if (val == boolean_false_node)
b30f223b 2299 warning ("comparison is always 0 due to limited range of data type");
a360da3a 2300 if (val == boolean_true_node)
b30f223b
RS
2301 warning ("comparison is always 1 due to limited range of data type");
2302 }
2303
2304 if (val != 0)
2305 {
2306 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2307 if (TREE_SIDE_EFFECTS (primop0))
2308 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2309 return val;
2310 }
2311
2312 /* Value is not predetermined, but do the comparison
2313 in the type of the operand that is not constant.
2314 TYPE is already properly set. */
2315 }
2316 else if (real1 && real2
766f6c30
RS
2317 && (TYPE_PRECISION (TREE_TYPE (primop0))
2318 == TYPE_PRECISION (TREE_TYPE (primop1))))
b30f223b
RS
2319 type = TREE_TYPE (primop0);
2320
2321 /* If args' natural types are both narrower than nominal type
2322 and both extend in the same manner, compare them
2323 in the type of the wider arg.
2324 Otherwise must actually extend both to the nominal
2325 common type lest different ways of extending
2326 alter the result.
2327 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2328
2329 else if (unsignedp0 == unsignedp1 && real1 == real2
2330 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2331 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2332 {
2333 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2334 type = signed_or_unsigned_type (unsignedp0
2335 || TREE_UNSIGNED (*restype_ptr),
2336 type);
2337 /* Make sure shorter operand is extended the right way
2338 to match the longer operand. */
2339 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2340 primop0);
2341 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2342 primop1);
2343 }
2344 else
2345 {
2346 /* Here we must do the comparison on the nominal type
2347 using the args exactly as we received them. */
2348 type = *restype_ptr;
2349 primop0 = op0;
2350 primop1 = op1;
2351
2352 if (!real1 && !real2 && integer_zerop (primop1)
597681f6 2353 && TREE_UNSIGNED (*restype_ptr))
b30f223b
RS
2354 {
2355 tree value = 0;
2356 switch (code)
2357 {
2358 case GE_EXPR:
5af6001b
RK
2359 /* All unsigned values are >= 0, so we warn if extra warnings
2360 are requested. However, if OP0 is a constant that is
2361 >= 0, the signedness of the comparison isn't an issue,
2362 so suppress the warning. */
2363 if (extra_warnings
2364 && ! (TREE_CODE (primop0) == INTEGER_CST
2365 && ! TREE_OVERFLOW (convert (signed_type (type),
2366 primop0))))
b30f223b 2367 warning ("unsigned value >= 0 is always 1");
a360da3a 2368 value = boolean_true_node;
b30f223b
RS
2369 break;
2370
2371 case LT_EXPR:
5af6001b
RK
2372 if (extra_warnings
2373 && ! (TREE_CODE (primop0) == INTEGER_CST
2374 && ! TREE_OVERFLOW (convert (signed_type (type),
2375 primop0))))
b30f223b 2376 warning ("unsigned value < 0 is always 0");
a360da3a 2377 value = boolean_false_node;
6d819282
MK
2378 break;
2379
2380 default:
2381 break;
b30f223b
RS
2382 }
2383
2384 if (value != 0)
2385 {
2386 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2387 if (TREE_SIDE_EFFECTS (primop0))
2388 return build (COMPOUND_EXPR, TREE_TYPE (value),
2389 primop0, value);
2390 return value;
2391 }
2392 }
2393 }
2394
2395 *op0_ptr = convert (type, primop0);
2396 *op1_ptr = convert (type, primop1);
2397
a360da3a 2398 *restype_ptr = boolean_type_node;
b30f223b
RS
2399
2400 return 0;
2401}
2402\f
2403/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2404 or validate its data type for an `if' or `while' statement or ?..: exp.
2405
2406 This preparation consists of taking the ordinary
2407 representation of an expression expr and producing a valid tree
2408 boolean expression describing whether expr is nonzero. We could
a360da3a 2409 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
b30f223b
RS
2410 but we optimize comparisons, &&, ||, and !.
2411
a360da3a 2412 The resulting type should always be `boolean_type_node'. */
b30f223b
RS
2413
2414tree
2415truthvalue_conversion (expr)
2416 tree expr;
2417{
257e61ed
RS
2418 if (TREE_CODE (expr) == ERROR_MARK)
2419 return expr;
2420
d7c83727 2421#if 0 /* This appears to be wrong for C++. */
257e61ed
RS
2422 /* These really should return error_mark_node after 2.4 is stable.
2423 But not all callers handle ERROR_MARK properly. */
2424 switch (TREE_CODE (TREE_TYPE (expr)))
2425 {
2426 case RECORD_TYPE:
2427 error ("struct type value used where scalar is required");
a360da3a 2428 return boolean_false_node;
257e61ed
RS
2429
2430 case UNION_TYPE:
2431 error ("union type value used where scalar is required");
a360da3a 2432 return boolean_false_node;
257e61ed
RS
2433
2434 case ARRAY_TYPE:
2435 error ("array type value used where scalar is required");
a360da3a 2436 return boolean_false_node;
257e61ed
RS
2437
2438 default:
2439 break;
2440 }
d7c83727 2441#endif /* 0 */
257e61ed 2442
b30f223b
RS
2443 switch (TREE_CODE (expr))
2444 {
2445 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2446 or comparison expressions as truth values at this level. */
2447#if 0
2448 case COMPONENT_REF:
2449 /* A one-bit unsigned bit-field is already acceptable. */
2450 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
2451 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
2452 return expr;
2453 break;
2454#endif
2455
2456 case EQ_EXPR:
2457 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2458 or comparison expressions as truth values at this level. */
2459#if 0
2460 if (integer_zerop (TREE_OPERAND (expr, 1)))
2461 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
2462#endif
2463 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2464 case TRUTH_ANDIF_EXPR:
2465 case TRUTH_ORIF_EXPR:
2466 case TRUTH_AND_EXPR:
2467 case TRUTH_OR_EXPR:
9379fac9 2468 case TRUTH_XOR_EXPR:
1180eb10 2469 case TRUTH_NOT_EXPR:
a360da3a
JM
2470 TREE_TYPE (expr) = boolean_type_node;
2471 return expr;
18c0f675 2472
b30f223b
RS
2473 case ERROR_MARK:
2474 return expr;
2475
2476 case INTEGER_CST:
a360da3a 2477 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
b30f223b
RS
2478
2479 case REAL_CST:
a360da3a 2480 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
b30f223b
RS
2481
2482 case ADDR_EXPR:
fc0c675f
RK
2483 /* If we are taking the address of a external decl, it might be zero
2484 if it is weak, so we cannot optimize. */
2485 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr, 0))) == 'd'
2486 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2487 break;
2488
b30f223b 2489 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
a360da3a
JM
2490 return build (COMPOUND_EXPR, boolean_type_node,
2491 TREE_OPERAND (expr, 0), boolean_true_node);
b30f223b 2492 else
a360da3a 2493 return boolean_true_node;
b30f223b 2494
766f6c30 2495 case COMPLEX_EXPR:
f0b996c5 2496 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
b839fb3f 2497 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
f0b996c5
RS
2498 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2499 truthvalue_conversion (TREE_OPERAND (expr, 1)),
766f6c30
RS
2500 0);
2501
b30f223b
RS
2502 case NEGATE_EXPR:
2503 case ABS_EXPR:
2504 case FLOAT_EXPR:
2505 case FFS_EXPR:
2506 /* These don't change whether an object is non-zero or zero. */
2507 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2508
2509 case LROTATE_EXPR:
2510 case RROTATE_EXPR:
2511 /* These don't change whether an object is zero or non-zero, but
2512 we can't ignore them if their second arg has side-effects. */
2513 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
a360da3a 2514 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
b30f223b
RS
2515 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2516 else
2517 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2518
2519 case COND_EXPR:
2520 /* Distribute the conversion into the arms of a COND_EXPR. */
a360da3a 2521 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
b30f223b
RS
2522 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2523 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2524
2525 case CONVERT_EXPR:
2526 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2527 since that affects how `default_conversion' will behave. */
2528 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2529 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2530 break;
0f41302f 2531 /* fall through... */
b30f223b
RS
2532 case NOP_EXPR:
2533 /* If this is widening the argument, we can ignore it. */
2534 if (TYPE_PRECISION (TREE_TYPE (expr))
2535 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2536 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2537 break;
2538
b30f223b 2539 case MINUS_EXPR:
f87550e0
JW
2540 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2541 this case. */
2542 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2543 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2544 break;
0f41302f 2545 /* fall through... */
f87550e0 2546 case BIT_XOR_EXPR:
d7c83727 2547 /* This and MINUS_EXPR can be changed into a comparison of the
f87550e0 2548 two objects. */
b30f223b
RS
2549 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2550 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2551 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2552 TREE_OPERAND (expr, 1), 1);
2553 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2554 fold (build1 (NOP_EXPR,
2555 TREE_TYPE (TREE_OPERAND (expr, 0)),
2556 TREE_OPERAND (expr, 1))), 1);
e2aab13d 2557
fb48b1f0 2558 case BIT_AND_EXPR:
58cee643
RK
2559 if (integer_onep (TREE_OPERAND (expr, 1))
2560 && TREE_TYPE (expr) != boolean_type_node)
2561 /* Using convert here would cause infinite recursion. */
2562 return build1 (NOP_EXPR, boolean_type_node, expr);
2563 break;
fb48b1f0 2564
e2aab13d
RS
2565 case MODIFY_EXPR:
2566 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2567 warning ("suggest parentheses around assignment used as truth value");
2568 break;
6d819282
MK
2569
2570 default:
2571 break;
b30f223b
RS
2572 }
2573
f0b996c5
RS
2574 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2575 return (build_binary_op
2576 ((TREE_SIDE_EFFECTS (expr)
b839fb3f 2577 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
f0b996c5
RS
2578 truthvalue_conversion (build_unary_op (REALPART_EXPR, expr, 0)),
2579 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, expr, 0)),
2580 0));
2581
b30f223b
RS
2582 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2583}
2584\f
2585/* Read the rest of a #-directive from input stream FINPUT.
2586 In normal use, the directive name and the white space after it
2587 have already been read, so they won't be included in the result.
2588 We allow for the fact that the directive line may contain
2589 a newline embedded within a character or string literal which forms
2590 a part of the directive.
2591
2592 The value is a string in a reusable buffer. It remains valid
05a81fe5
DE
2593 only until the next time this function is called.
2594
2595 The terminating character ('\n' or EOF) is left in FINPUT for the
2596 caller to re-read. */
b30f223b
RS
2597
2598char *
2599get_directive_line (finput)
2600 register FILE *finput;
2601{
2602 static char *directive_buffer = NULL;
2603 static unsigned buffer_length = 0;
2604 register char *p;
2605 register char *buffer_limit;
2606 register int looking_for = 0;
2607 register int char_escaped = 0;
2608
2609 if (buffer_length == 0)
2610 {
2611 directive_buffer = (char *)xmalloc (128);
2612 buffer_length = 128;
2613 }
2614
2615 buffer_limit = &directive_buffer[buffer_length];
2616
2617 for (p = directive_buffer; ; )
2618 {
2619 int c;
2620
2621 /* Make buffer bigger if it is full. */
2622 if (p >= buffer_limit)
2623 {
2624 register unsigned bytes_used = (p - directive_buffer);
2625
2626 buffer_length *= 2;
2627 directive_buffer
2628 = (char *)xrealloc (directive_buffer, buffer_length);
2629 p = &directive_buffer[bytes_used];
2630 buffer_limit = &directive_buffer[buffer_length];
2631 }
2632
2633 c = getc (finput);
2634
2635 /* Discard initial whitespace. */
2636 if ((c == ' ' || c == '\t') && p == directive_buffer)
2637 continue;
2638
2639 /* Detect the end of the directive. */
05a81fe5
DE
2640 if (looking_for == 0
2641 && (c == '\n' || c == EOF))
b30f223b
RS
2642 {
2643 ungetc (c, finput);
2644 c = '\0';
2645 }
2646
2647 *p++ = c;
2648
2649 if (c == 0)
2650 return directive_buffer;
2651
2652 /* Handle string and character constant syntax. */
2653 if (looking_for)
2654 {
2655 if (looking_for == c && !char_escaped)
2656 looking_for = 0; /* Found terminator... stop looking. */
2657 }
2658 else
2659 if (c == '\'' || c == '"')
2660 looking_for = c; /* Don't stop buffering until we see another
2661 another one of these (or an EOF). */
2662
2663 /* Handle backslash. */
2664 char_escaped = (c == '\\' && ! char_escaped);
2665 }
2666}
0b73773c
NH
2667\f
2668/* Make a variant type in the proper way for C/C++, propagating qualifiers
2669 down to the element type of an array. */
2670
2671tree
2672c_build_type_variant (type, constp, volatilep)
2673 tree type;
2674 int constp, volatilep;
2675{
2676 if (TREE_CODE (type) == ARRAY_TYPE)
3ab1999b
RK
2677 return build_array_type (c_build_type_variant (TREE_TYPE (type),
2678 constp, volatilep),
2679 TYPE_DOMAIN (type));
0b73773c
NH
2680 return build_type_variant (type, constp, volatilep);
2681}