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