]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-common.c
(fndef, nested_function): Pass prefix_attributes to start_function.
[thirdparty/gcc.git] / gcc / c-common.c
CommitLineData
b30f223b 1/* Subroutines shared by all languages that are variants of C.
767880cd 2 Copyright (C) 1992, 1993, 1994, 1995 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
18the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "config.h"
21#include "tree.h"
22#include "c-lex.h"
23#include "c-tree.h"
24#include "flags.h"
1bb8e5b1 25#include "obstack.h"
b30f223b 26#include <stdio.h>
1ccf251f 27#include <ctype.h>
b30f223b 28
0b73773c
NH
29extern struct obstack permanent_obstack;
30
4724b3de
RS
31static void declare_hidden_char_array PROTO((char *, char *));
32
b032c74c 33/* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
7da551a2
RS
34
35void
36declare_function_name ()
37{
7da551a2
RS
38 char *name, *printable_name;
39
40 if (current_function_decl == NULL)
41 {
42 name = "";
43 printable_name = "top level";
44 }
45 else
46 {
47 char *kind = "function";
48 if (TREE_CODE (TREE_TYPE (current_function_decl)) == METHOD_TYPE)
49 kind = "method";
6152f876
RS
50 /* Allow functions to be nameless (such as artificial ones). */
51 if (DECL_NAME (current_function_decl))
52 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
53 else
54 name = "";
7da551a2
RS
55 printable_name = (*decl_printable_name) (current_function_decl, &kind);
56 }
57
4724b3de
RS
58 declare_hidden_char_array ("__FUNCTION__", name);
59 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
60}
97d17ac2 61
4724b3de
RS
62static void
63declare_hidden_char_array (name, value)
64 char *name, *value;
65{
66 tree decl, type, init;
67 int vlen;
7da551a2 68
4724b3de 69 /* If the default size of char arrays isn't big enough for the name,
700942a0 70 or if we want to give warnings for large objects, make a bigger one. */
4724b3de 71 vlen = strlen (value) + 1;
97d17ac2 72 type = char_array_type_node;
700942a0
RK
73 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TREE_TYPE (type))) < vlen
74 || warn_larger_than)
97d17ac2 75 type = build_array_type (char_type_node,
4724b3de 76 build_index_type (build_int_2 (vlen, 0)));
7da551a2 77 push_obstacks_nochange ();
4724b3de 78 decl = build_decl (VAR_DECL, get_identifier (name), type);
7da551a2
RS
79 TREE_STATIC (decl) = 1;
80 TREE_READONLY (decl) = 1;
4724b3de 81 TREE_ASM_WRITTEN (decl) = 1;
b9a24ad4 82 DECL_SOURCE_LINE (decl) = 0;
176e81eb 83 DECL_IN_SYSTEM_HEADER (decl) = 1;
7da551a2 84 DECL_IGNORED_P (decl) = 1;
4724b3de 85 init = build_string (vlen, value);
97d17ac2 86 TREE_TYPE (init) = type;
7da551a2
RS
87 DECL_INITIAL (decl) = init;
88 finish_decl (pushdecl (decl), init, NULL_TREE);
89}
90
b30f223b
RS
91/* Given a chain of STRING_CST nodes,
92 concatenate them into one STRING_CST
93 and give it a suitable array-of-chars data type. */
94
95tree
96combine_strings (strings)
97 tree strings;
98{
99 register tree value, t;
100 register int length = 1;
101 int wide_length = 0;
102 int wide_flag = 0;
103 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
104 int nchars;
105
106 if (TREE_CHAIN (strings))
107 {
108 /* More than one in the chain, so concatenate. */
109 register char *p, *q;
110
111 /* Don't include the \0 at the end of each substring,
112 except for the last one.
113 Count wide strings and ordinary strings separately. */
114 for (t = strings; t; t = TREE_CHAIN (t))
115 {
116 if (TREE_TYPE (t) == wchar_array_type_node)
117 {
118 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
119 wide_flag = 1;
120 }
121 else
122 length += (TREE_STRING_LENGTH (t) - 1);
123 }
124
125 /* If anything is wide, the non-wides will be converted,
126 which makes them take more space. */
127 if (wide_flag)
128 length = length * wchar_bytes + wide_length;
129
130 p = savealloc (length);
131
132 /* Copy the individual strings into the new combined string.
133 If the combined string is wide, convert the chars to ints
134 for any individual strings that are not wide. */
135
136 q = p;
137 for (t = strings; t; t = TREE_CHAIN (t))
138 {
139 int len = (TREE_STRING_LENGTH (t)
140 - ((TREE_TYPE (t) == wchar_array_type_node)
141 ? wchar_bytes : 1));
142 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
143 {
144 bcopy (TREE_STRING_POINTER (t), q, len);
145 q += len;
146 }
147 else
148 {
149 int i;
150 for (i = 0; i < len; i++)
151 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
152 q += len * wchar_bytes;
153 }
154 }
155 if (wide_flag)
156 {
157 int i;
158 for (i = 0; i < wchar_bytes; i++)
159 *q++ = 0;
160 }
161 else
162 *q = 0;
163
164 value = make_node (STRING_CST);
165 TREE_STRING_POINTER (value) = p;
166 TREE_STRING_LENGTH (value) = length;
167 TREE_CONSTANT (value) = 1;
168 }
169 else
170 {
171 value = strings;
172 length = TREE_STRING_LENGTH (value);
173 if (TREE_TYPE (value) == wchar_array_type_node)
174 wide_flag = 1;
175 }
176
177 /* Compute the number of elements, for the array type. */
178 nchars = wide_flag ? length / wchar_bytes : length;
179
180 /* Create the array type for the string constant.
181 -Wwrite-strings says make the string constant an array of const char
182 so that copying it to a non-const pointer will get a warning. */
183 if (warn_write_strings
184 && (! flag_traditional && ! flag_writable_strings))
185 {
186 tree elements
187 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
188 1, 0);
189 TREE_TYPE (value)
190 = build_array_type (elements,
191 build_index_type (build_int_2 (nchars - 1, 0)));
192 }
193 else
194 TREE_TYPE (value)
195 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
196 build_index_type (build_int_2 (nchars - 1, 0)));
197 TREE_CONSTANT (value) = 1;
198 TREE_STATIC (value) = 1;
199 return value;
200}
201\f
1228e2a6
RK
202/* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
203 and install them in DECL. PREFIX_ATTRIBUTES can appear after the
204 declaration specifiers and declaration modifiers but before the
205 declaration proper. */
b30f223b
RS
206
207void
1228e2a6
RK
208decl_attributes (decl, attributes, prefix_attributes)
209 tree decl, attributes, prefix_attributes;
b30f223b 210{
1228e2a6 211 tree a, name, args, type;
a2cd7b45
RK
212
213 type = TREE_TYPE (decl);
214
1228e2a6
RK
215 for (a = prefix_attributes; a; a = TREE_CHAIN (a))
216 if (!(name = TREE_VALUE (a)))
217 continue;
218 else if (valid_machine_attribute (name, decl, type))
219 ;
220 else
221 warning ("`%s' attribute directive ignored",
222 IDENTIFIER_POINTER (name));
5289b665 223
b30f223b 224 for (a = attributes; a; a = TREE_CHAIN (a))
a2cd7b45
RK
225 if (!(name = TREE_VALUE (a)))
226 continue;
55960f52
RK
227 else if (name == get_identifier ("packed")
228 || name == get_identifier ("__packed__"))
2525c782
RS
229 {
230 if (TREE_CODE (decl) == FIELD_DECL)
231 DECL_PACKED (decl) = 1;
9b4e97b0 232 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
1394aabd 233 used for DECL_REGISTER. It wouldn't mean anything anyway. */
a0b25e45 234 else
23c97802 235 warning_with_decl (decl, "`packed' attribute ignored");
2525c782 236 }
23c97802
DE
237 else if (name == get_identifier ("noreturn")
238 || name == get_identifier ("__noreturn__")
239 || name == get_identifier ("volatile")
240 || name == get_identifier ("__volatile__"))
3550617c
RS
241 {
242 if (TREE_CODE (decl) == FUNCTION_DECL)
243 TREE_THIS_VOLATILE (decl) = 1;
a0b25e45
RK
244 else if (TREE_CODE (type) == POINTER_TYPE
245 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
20250c07 246 TREE_TYPE (decl) = type
a0b25e45
RK
247 = build_pointer_type
248 (build_type_variant (TREE_TYPE (type),
249 TREE_READONLY (TREE_TYPE (type)), 1));
250 else
23c97802
DE
251 warning_with_decl (decl,
252 (IDENTIFIER_POINTER (name)[0] == 'n'
253 || IDENTIFIER_POINTER (name)[2] == 'n')
254 ? "`noreturn' attribute ignored"
255 : "`volatile' attribute ignored");
3550617c 256 }
23c97802
DE
257 else if (name == get_identifier ("const")
258 || name == get_identifier ("__const__"))
3550617c
RS
259 {
260 if (TREE_CODE (decl) == FUNCTION_DECL)
261 TREE_READONLY (decl) = 1;
a0b25e45
RK
262 else if (TREE_CODE (type) == POINTER_TYPE
263 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
20250c07 264 TREE_TYPE (decl) = type
a0b25e45
RK
265 = build_pointer_type
266 (build_type_variant (TREE_TYPE (type), 1,
267 TREE_THIS_VOLATILE (TREE_TYPE (type))));
268 else
269 warning_with_decl (decl, "`const' attribute ignored");
3550617c 270 }
23c97802
DE
271 else if (name == get_identifier ("transparent_union")
272 || name == get_identifier ("__transparent_union__"))
ec83f2b3
RK
273 {
274 if (TREE_CODE (decl) == PARM_DECL
275 && TREE_CODE (type) == UNION_TYPE
276 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
277 DECL_TRANSPARENT_UNION (decl) = 1;
278 else if (TREE_CODE (decl) == TYPE_DECL
279 && TREE_CODE (type) == UNION_TYPE
280 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
281 TYPE_TRANSPARENT_UNION (type) = 1;
282 else
283 warning_with_decl (decl, "`transparent_union' attribute ignored");
284 }
1228e2a6
RK
285 else if (name == get_identifier ("constructor")
286 || name == get_identifier ("__constructor__"))
2c5f4139
JM
287 {
288 if (TREE_CODE (decl) != FUNCTION_DECL
289 || TREE_CODE (TREE_TYPE (decl)) != FUNCTION_TYPE
290 || decl_function_context (decl))
291 {
292 error_with_decl (decl,
293 "`constructor' attribute meaningless for non-function %s");
294 continue;
295 }
296 DECL_STATIC_CONSTRUCTOR (decl) = 1;
297 }
1228e2a6
RK
298 else if (name == get_identifier ("destructor")
299 || name == get_identifier ("__destructor__"))
2c5f4139
JM
300 {
301 if (TREE_CODE (decl) != FUNCTION_DECL
302 || TREE_CODE (TREE_TYPE (decl)) != FUNCTION_TYPE
303 || decl_function_context (decl))
304 {
305 error_with_decl (decl,
306 "`destructor' attribute meaningless for non-function %s");
307 continue;
308 }
309 DECL_STATIC_DESTRUCTOR (decl) = 1;
310 }
23c97802 311 else if ( args = TREE_CHAIN (name),
55960f52
RK
312 (!strcmp (IDENTIFIER_POINTER (name = TREE_PURPOSE (name)), "mode")
313 || !strcmp (IDENTIFIER_POINTER (name), "__mode__"))
a2cd7b45 314 && list_length (args) == 1
389da362 315 && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE)
2525c782
RS
316 {
317 int i;
caab5771
RK
318 char *specified_name = IDENTIFIER_POINTER (TREE_VALUE (args));
319 enum machine_mode mode = VOIDmode;
320 tree typefm;
321
322 /* Give this decl a type with the specified mode.
323 First check for the special modes. */
324 if (! strcmp (specified_name, "byte")
325 || ! strcmp (specified_name, "__byte__"))
326 mode = byte_mode;
327 else if (!strcmp (specified_name, "word")
328 || ! strcmp (specified_name, "__word__"))
329 mode = word_mode;
330 else if (! strcmp (specified_name, "pointer")
331 || !strcmp (specified_name, "__pointer__"))
332 mode = ptr_mode;
333 else
334 for (i = 0; i < NUM_MACHINE_MODES; i++)
335 if (!strcmp (specified_name, GET_MODE_NAME (i)))
336 mode = (enum machine_mode) i;
2525c782 337
caab5771 338 if (mode == VOIDmode)
23c97802 339 error ("unknown machine mode `%s'", specified_name);
caab5771 340 else if ((typefm = type_for_mode (mode, TREE_UNSIGNED (type))) == 0)
23c97802 341 error ("no data type for mode `%s'", specified_name);
caab5771
RK
342 else
343 {
344 TREE_TYPE (decl) = type = typefm;
345 DECL_SIZE (decl) = 0;
346 layout_decl (decl, 0);
347 }
2525c782 348 }
55960f52
RK
349 else if ((!strcmp (IDENTIFIER_POINTER (name), "section")
350 || !strcmp (IDENTIFIER_POINTER (name), "__section__"))
a2cd7b45
RK
351 && list_length (args) == 1
352 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
5289b665
DE
353 {
354#ifdef ASM_OUTPUT_SECTION_NAME
355 if (TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
356 {
1228e2a6
RK
357 if (TREE_CODE (decl) == VAR_DECL
358 && current_function_decl != NULL_TREE)
a0b25e45
RK
359 error_with_decl (decl,
360 "section attribute cannot be specified for local variables");
5289b665
DE
361 /* The decl may have already been given a section attribute from
362 a previous declaration. Ensure they match. */
363 else if (DECL_SECTION_NAME (decl) != NULL_TREE
364 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
f58e9815 365 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
5289b665
DE
366 error_with_decl (decl,
367 "section of `%s' conflicts with previous declaration");
368 else
f58e9815 369 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
5289b665
DE
370 }
371 else
372 error_with_decl (decl,
373 "section attribute not allowed for `%s'");
374#else
375 error_with_decl (decl, "section attributes are not supported for this target");
376#endif
377 }
55960f52
RK
378 else if ((!strcmp (IDENTIFIER_POINTER (name), "aligned")
379 || !strcmp (IDENTIFIER_POINTER (name), "__aligned__"))
a2cd7b45
RK
380 && list_length (args) == 1
381 && TREE_CODE (TREE_VALUE (args)) == INTEGER_CST)
b30f223b 382 {
a2cd7b45 383 tree align_expr = TREE_VALUE (args);
6f38f669
RK
384 int align;
385
386 /* Strip any NOPs of any kind. */
387 while (TREE_CODE (align_expr) == NOP_EXPR
388 || TREE_CODE (align_expr) == CONVERT_EXPR
389 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
390 align_expr = TREE_OPERAND (align_expr, 0);
391
392 if (TREE_CODE (align_expr) != INTEGER_CST)
393 {
394 error_with_decl (decl,
395 "requested alignment of `%s' is not a constant");
396 continue;
397 }
398
399 align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
b30f223b
RS
400
401 if (exact_log2 (align) == -1)
9a631e8e
RS
402 error_with_decl (decl,
403 "requested alignment of `%s' is not a power of 2");
b30f223b
RS
404 else if (TREE_CODE (decl) != VAR_DECL
405 && TREE_CODE (decl) != FIELD_DECL)
9a631e8e 406 error_with_decl (decl,
1e307bd8 407 "alignment specified for `%s'");
b30f223b
RS
408 else
409 DECL_ALIGN (decl) = align;
410 }
55960f52
RK
411 else if ((!strcmp (IDENTIFIER_POINTER (name), "format")
412 || !strcmp (IDENTIFIER_POINTER (name), "__format__"))
a2cd7b45
RK
413 && list_length (args) == 3
414 && TREE_CODE (TREE_VALUE (args)) == IDENTIFIER_NODE
415 && TREE_CODE (TREE_VALUE (TREE_CHAIN (args))) == INTEGER_CST
1228e2a6
RK
416 && TREE_CODE (TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args))))
417 == INTEGER_CST )
b30f223b 418 {
a2cd7b45
RK
419 tree format_type = TREE_VALUE (args);
420 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
421 tree first_arg_num_expr = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
6f38f669
RK
422 int format_num;
423 int first_arg_num;
b30f223b 424 int is_scan;
677ff441
JW
425 tree argument;
426 int arg_num;
b30f223b
RS
427
428 if (TREE_CODE (decl) != FUNCTION_DECL)
429 {
9a631e8e
RS
430 error_with_decl (decl,
431 "argument format specified for non-function `%s'");
6f38f669 432 continue;
b30f223b
RS
433 }
434
ca854c05
RK
435 if (!strcmp (IDENTIFIER_POINTER (format_type), "printf")
436 || !strcmp (IDENTIFIER_POINTER (format_type), "__printf__"))
b30f223b 437 is_scan = 0;
ca854c05
RK
438 else if (!strcmp (IDENTIFIER_POINTER (format_type), "scanf")
439 || !strcmp (IDENTIFIER_POINTER (format_type), "__scanf__"))
b30f223b
RS
440 is_scan = 1;
441 else
442 {
9a631e8e 443 error_with_decl (decl, "unrecognized format specifier for `%s'");
6f38f669 444 continue;
b30f223b
RS
445 }
446
6f38f669
RK
447 /* Strip any conversions from the string index and first arg number
448 and verify they are constants. */
449 while (TREE_CODE (format_num_expr) == NOP_EXPR
450 || TREE_CODE (format_num_expr) == CONVERT_EXPR
451 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
452 format_num_expr = TREE_OPERAND (format_num_expr, 0);
453
454 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
455 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
456 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
457 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
458
459 if (TREE_CODE (format_num_expr) != INTEGER_CST
460 || TREE_CODE (first_arg_num_expr) != INTEGER_CST)
461 {
462 error_with_decl (decl,
463 "format string for `%s' has non-constant operand number");
464 continue;
465 }
466
467 format_num = TREE_INT_CST_LOW (format_num_expr);
468 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
b30f223b
RS
469 if (first_arg_num != 0 && first_arg_num <= format_num)
470 {
9a631e8e 471 error_with_decl (decl,
6f38f669
RK
472 "format string arg follows the args to be formatted, for `%s'");
473 continue;
b30f223b 474 }
677ff441 475
09e3dd72
RK
476 /* If a parameter list is specified, verify that the format_num
477 argument is actually a string, in case the format attribute
478 is in error. */
a2cd7b45 479 argument = TYPE_ARG_TYPES (type);
09e3dd72 480 if (argument)
677ff441 481 {
09e3dd72
RK
482 for (arg_num = 1; ; ++arg_num)
483 {
484 if (argument == 0 || arg_num == format_num)
485 break;
486 argument = TREE_CHAIN (argument);
487 }
488 if (! argument
489 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
490 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
491 != char_type_node))
68a91d8d
RS
492 {
493 error_with_decl (decl,
09e3dd72 494 "format string arg not a string type, for `%s'");
6f38f669 495 continue;
68a91d8d 496 }
09e3dd72
RK
497 if (first_arg_num != 0)
498 {
499 /* Verify that first_arg_num points to the last arg, the ... */
500 while (argument)
501 arg_num++, argument = TREE_CHAIN (argument);
502 if (arg_num != first_arg_num)
503 {
504 error_with_decl (decl,
505 "args to be formatted is not ..., for `%s'");
506 continue;
507 }
508 }
677ff441 509 }
1ccf251f
RK
510
511 record_function_format (DECL_NAME (decl), DECL_ASSEMBLER_NAME (decl),
512 is_scan, format_num, first_arg_num);
b30f223b 513 }
1228e2a6
RK
514 else if (valid_machine_attribute (name, decl, type))
515 ;
a2cd7b45 516 else
23c97802
DE
517 warning ("`%s' attribute directive ignored",
518 IDENTIFIER_POINTER (name));
a2cd7b45 519
b30f223b
RS
520}
521\f
1ccf251f
RK
522/* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
523 a parameter list. */
524
525#define T_I &integer_type_node
526#define T_L &long_integer_type_node
2314fb70 527#define T_LL &long_long_integer_type_node
1ccf251f
RK
528#define T_S &short_integer_type_node
529#define T_UI &unsigned_type_node
530#define T_UL &long_unsigned_type_node
2314fb70 531#define T_ULL &long_long_unsigned_type_node
1ccf251f
RK
532#define T_US &short_unsigned_type_node
533#define T_F &float_type_node
534#define T_D &double_type_node
535#define T_LD &long_double_type_node
536#define T_C &char_type_node
537#define T_V &void_type_node
538#define T_W &wchar_type_node
df8a401a 539#define T_ST &sizetype
1ccf251f
RK
540
541typedef struct {
542 char *format_chars;
543 int pointer_count;
544 /* Type of argument if no length modifier is used. */
545 tree *nolen;
546 /* Type of argument if length modifier for shortening is used.
547 If NULL, then this modifier is not allowed. */
548 tree *hlen;
549 /* Type of argument if length modifier `l' is used.
550 If NULL, then this modifier is not allowed. */
551 tree *llen;
2cedb812 552 /* Type of argument if length modifier `q' or `ll' is used.
2314fb70
CH
553 If NULL, then this modifier is not allowed. */
554 tree *qlen;
1ccf251f
RK
555 /* Type of argument if length modifier `L' is used.
556 If NULL, then this modifier is not allowed. */
557 tree *bigllen;
558 /* List of other modifier characters allowed with these options. */
559 char *flag_chars;
560} format_char_info;
561
562static format_char_info print_char_table[] = {
2cedb812
RK
563 { "di", 0, T_I, T_I, T_L, T_LL, T_LL, "-wp0 +" },
564 { "oxX", 0, T_UI, T_UI, T_UL, T_ULL, T_ULL, "-wp0#" },
4cc00c5a 565 { "u", 0, T_UI, T_UI, T_UL, T_ULL, T_ULL, "-wp0" },
af3c5588 566/* Two GNU extensions. */
2314fb70
CH
567 { "Z", 0, T_ST, NULL, NULL, NULL, NULL, "-wp0" },
568 { "m", 0, T_UI, T_UI, T_UL, NULL, NULL, "-wp" },
569 { "feEgG", 0, T_D, NULL, NULL, NULL, T_LD, "-wp0 +#" },
570 { "c", 0, T_I, NULL, T_W, NULL, NULL, "-w" },
571 { "C", 0, T_W, NULL, NULL, NULL, NULL, "-w" },
572 { "s", 1, T_C, NULL, T_W, NULL, NULL, "-wp" },
573 { "S", 1, T_W, NULL, NULL, NULL, NULL, "-wp" },
574 { "p", 1, T_V, NULL, NULL, NULL, NULL, "-w" },
575 { "n", 1, T_I, T_S, T_L, T_LL, NULL, "" },
1ccf251f
RK
576 { NULL }
577};
578
579static format_char_info scan_char_table[] = {
2cedb812
RK
580 { "di", 1, T_I, T_S, T_L, T_LL, T_LL, "*" },
581 { "ouxX", 1, T_UI, T_US, T_UL, T_ULL, T_ULL, "*" },
2314fb70
CH
582 { "efgEG", 1, T_F, NULL, T_D, NULL, T_LD, "*" },
583 { "sc", 1, T_C, NULL, T_W, NULL, NULL, "*a" },
584 { "[", 1, T_C, NULL, NULL, NULL, NULL, "*a" },
585 { "C", 1, T_W, NULL, NULL, NULL, NULL, "*" },
586 { "S", 1, T_W, NULL, NULL, NULL, NULL, "*" },
587 { "p", 2, T_V, NULL, NULL, NULL, NULL, "*" },
588 { "n", 1, T_I, T_S, T_L, T_LL, NULL, "" },
1ccf251f
RK
589 { NULL }
590};
591
592typedef struct function_format_info {
593 struct function_format_info *next; /* next structure on the list */
594 tree name; /* identifier such as "printf" */
595 tree assembler_name; /* optional mangled identifier (for C++) */
596 int is_scan; /* TRUE if *scanf */
597 int format_num; /* number of format argument */
598 int first_arg_num; /* number of first arg (zero for varargs) */
599} function_format_info;
600
601static function_format_info *function_format_list = NULL;
602
603static void check_format_info PROTO((function_format_info *, tree));
604
605/* Initialize the table of functions to perform format checking on.
606 The ANSI functions are always checked (whether <stdio.h> is
607 included or not), since it is common to call printf without
608 including <stdio.h>. There shouldn't be a problem with this,
609 since ANSI reserves these function names whether you include the
610 header file or not. In any case, the checking is harmless. */
611
612void
613init_function_format_info ()
614{
615 record_function_format (get_identifier ("printf"), NULL_TREE, 0, 1, 2);
616 record_function_format (get_identifier ("fprintf"), NULL_TREE, 0, 2, 3);
617 record_function_format (get_identifier ("sprintf"), NULL_TREE, 0, 2, 3);
618 record_function_format (get_identifier ("scanf"), NULL_TREE, 1, 1, 2);
619 record_function_format (get_identifier ("fscanf"), NULL_TREE, 1, 2, 3);
620 record_function_format (get_identifier ("sscanf"), NULL_TREE, 1, 2, 3);
621 record_function_format (get_identifier ("vprintf"), NULL_TREE, 0, 1, 0);
622 record_function_format (get_identifier ("vfprintf"), NULL_TREE, 0, 2, 0);
623 record_function_format (get_identifier ("vsprintf"), NULL_TREE, 0, 2, 0);
624}
625
626/* Record information for argument format checking. FUNCTION_IDENT is
627 the identifier node for the name of the function to check (its decl
628 need not exist yet). IS_SCAN is true for scanf-type format checking;
629 false indicates printf-style format checking. FORMAT_NUM is the number
630 of the argument which is the format control string (starting from 1).
631 FIRST_ARG_NUM is the number of the first actual argument to check
632 against teh format string, or zero if no checking is not be done
633 (e.g. for varargs such as vfprintf). */
634
635void
636record_function_format (name, assembler_name, is_scan,
637 format_num, first_arg_num)
638 tree name;
639 tree assembler_name;
640 int is_scan;
641 int format_num;
642 int first_arg_num;
643{
644 function_format_info *info;
645
646 /* Re-use existing structure if it's there. */
647
648 for (info = function_format_list; info; info = info->next)
649 {
650 if (info->name == name && info->assembler_name == assembler_name)
651 break;
652 }
653 if (! info)
654 {
655 info = (function_format_info *) xmalloc (sizeof (function_format_info));
656 info->next = function_format_list;
657 function_format_list = info;
658
659 info->name = name;
660 info->assembler_name = assembler_name;
661 }
662
663 info->is_scan = is_scan;
664 info->format_num = format_num;
665 info->first_arg_num = first_arg_num;
666}
667
668static char tfaff[] = "too few arguments for format";
669\f
670/* Check the argument list of a call to printf, scanf, etc.
671 NAME is the function identifier.
672 ASSEMBLER_NAME is the function's assembler identifier.
673 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
674 PARAMS is the list of argument values. */
675
676void
677check_function_format (name, assembler_name, params)
678 tree name;
679 tree assembler_name;
680 tree params;
681{
682 function_format_info *info;
683
684 /* See if this function is a format function. */
685 for (info = function_format_list; info; info = info->next)
686 {
95216dec 687 if (info->assembler_name
1ccf251f
RK
688 ? (info->assembler_name == assembler_name)
689 : (info->name == name))
690 {
691 /* Yup; check it. */
692 check_format_info (info, params);
693 break;
694 }
695 }
696}
697
698/* Check the argument list of a call to printf, scanf, etc.
699 INFO points to the function_format_info structure.
700 PARAMS is the list of argument values. */
701
702static void
703check_format_info (info, params)
704 function_format_info *info;
705 tree params;
706{
707 int i;
708 int arg_num;
709 int suppressed, wide, precise;
710 int length_char;
711 int format_char;
712 int format_length;
713 tree format_tree;
714 tree cur_param;
715 tree cur_type;
716 tree wanted_type;
9b69f523 717 tree first_fillin_param;
1ccf251f
RK
718 char *format_chars;
719 format_char_info *fci;
720 static char message[132];
721 char flag_chars[8];
9b69f523 722 int has_operand_number = 0;
1ccf251f
RK
723
724 /* Skip to format argument. If the argument isn't available, there's
725 no work for us to do; prototype checking will catch the problem. */
726 for (arg_num = 1; ; ++arg_num)
727 {
728 if (params == 0)
729 return;
730 if (arg_num == info->format_num)
731 break;
732 params = TREE_CHAIN (params);
733 }
734 format_tree = TREE_VALUE (params);
735 params = TREE_CHAIN (params);
736 if (format_tree == 0)
737 return;
738 /* We can only check the format if it's a string constant. */
739 while (TREE_CODE (format_tree) == NOP_EXPR)
740 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
741 if (format_tree == null_pointer_node)
742 {
743 warning ("null format string");
744 return;
745 }
746 if (TREE_CODE (format_tree) != ADDR_EXPR)
747 return;
748 format_tree = TREE_OPERAND (format_tree, 0);
749 if (TREE_CODE (format_tree) != STRING_CST)
750 return;
751 format_chars = TREE_STRING_POINTER (format_tree);
752 format_length = TREE_STRING_LENGTH (format_tree);
753 if (format_length <= 1)
754 warning ("zero-length format string");
755 if (format_chars[--format_length] != 0)
756 {
757 warning ("unterminated format string");
758 return;
759 }
760 /* Skip to first argument to check. */
761 while (arg_num + 1 < info->first_arg_num)
762 {
763 if (params == 0)
764 return;
765 params = TREE_CHAIN (params);
766 ++arg_num;
767 }
9b69f523
RK
768
769 first_fillin_param = params;
1ccf251f
RK
770 while (1)
771 {
af3c5588 772 int aflag;
1ccf251f
RK
773 if (*format_chars == 0)
774 {
775 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
776 warning ("embedded `\\0' in format");
9b69f523 777 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
1ccf251f
RK
778 warning ("too many arguments for format");
779 return;
780 }
781 if (*format_chars++ != '%')
782 continue;
783 if (*format_chars == 0)
784 {
785 warning ("spurious trailing `%%' in format");
786 continue;
787 }
788 if (*format_chars == '%')
789 {
790 ++format_chars;
791 continue;
792 }
793 flag_chars[0] = 0;
794 suppressed = wide = precise = FALSE;
795 if (info->is_scan)
796 {
797 suppressed = *format_chars == '*';
798 if (suppressed)
799 ++format_chars;
800 while (isdigit (*format_chars))
801 ++format_chars;
802 }
803 else
804 {
9b69f523
RK
805 /* See if we have a number followed by a dollar sign. If we do,
806 it is an operand number, so set PARAMS to that operand. */
807 if (*format_chars >= '0' && *format_chars <= '9')
808 {
809 char *p = format_chars;
810
811 while (*p >= '0' && *p++ <= '9')
812 ;
813
814 if (*p == '$')
815 {
816 int opnum = atoi (format_chars);
817
818 params = first_fillin_param;
819 format_chars = p + 1;
820 has_operand_number = 1;
821
822 for (i = 1; i < opnum && params != 0; i++)
823 params = TREE_CHAIN (params);
824
825 if (opnum == 0 || params == 0)
826 {
827 warning ("operand number out of range in format");
828 return;
829 }
830 }
831 }
832
1ccf251f
RK
833 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
834 {
835 if (index (flag_chars, *format_chars) != 0)
836 {
837 sprintf (message, "repeated `%c' flag in format",
838 *format_chars);
839 warning (message);
840 }
841 i = strlen (flag_chars);
842 flag_chars[i++] = *format_chars++;
843 flag_chars[i] = 0;
844 }
845 /* "If the space and + flags both appear,
846 the space flag will be ignored." */
847 if (index (flag_chars, ' ') != 0
848 && index (flag_chars, '+') != 0)
849 warning ("use of both ` ' and `+' flags in format");
850 /* "If the 0 and - flags both appear,
851 the 0 flag will be ignored." */
852 if (index (flag_chars, '0') != 0
853 && index (flag_chars, '-') != 0)
854 warning ("use of both `0' and `-' flags in format");
855 if (*format_chars == '*')
856 {
857 wide = TRUE;
858 /* "...a field width...may be indicated by an asterisk.
859 In this case, an int argument supplies the field width..." */
860 ++format_chars;
861 if (params == 0)
862 {
863 warning (tfaff);
864 return;
865 }
866 if (info->first_arg_num != 0)
867 {
868 cur_param = TREE_VALUE (params);
869 params = TREE_CHAIN (params);
870 ++arg_num;
871 /* size_t is generally not valid here.
872 It will work on most machines, because size_t and int
873 have the same mode. But might as well warn anyway,
874 since it will fail on other machines. */
309ffab6
RS
875 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
876 != integer_type_node)
877 &&
878 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
879 != unsigned_type_node))
1ccf251f
RK
880 {
881 sprintf (message,
882 "field width is not type int (arg %d)",
883 arg_num);
884 warning (message);
885 }
886 }
887 }
888 else
889 {
890 while (isdigit (*format_chars))
891 {
892 wide = TRUE;
893 ++format_chars;
894 }
895 }
896 if (*format_chars == '.')
897 {
898 precise = TRUE;
899 ++format_chars;
900 if (*format_chars != '*' && !isdigit (*format_chars))
901 warning ("`.' not followed by `*' or digit in format");
902 /* "...a...precision...may be indicated by an asterisk.
903 In this case, an int argument supplies the...precision." */
904 if (*format_chars == '*')
905 {
906 if (info->first_arg_num != 0)
907 {
908 ++format_chars;
909 if (params == 0)
910 {
911 warning (tfaff);
912 return;
913 }
914 cur_param = TREE_VALUE (params);
915 params = TREE_CHAIN (params);
916 ++arg_num;
917 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
918 != integer_type_node)
919 {
920 sprintf (message,
921 "field width is not type int (arg %d)",
922 arg_num);
923 warning (message);
924 }
925 }
926 }
927 else
928 {
929 while (isdigit (*format_chars))
930 ++format_chars;
931 }
932 }
933 }
2314fb70
CH
934 if (*format_chars == 'h' || *format_chars == 'l' || *format_chars == 'q' ||
935 *format_chars == 'L')
1ccf251f
RK
936 length_char = *format_chars++;
937 else
938 length_char = 0;
2cedb812
RK
939 if (length_char == 'l' && *format_chars == 'l')
940 length_char = 'q', format_chars++;
af3c5588
RK
941 aflag = 0;
942 if (*format_chars == 'a')
943 {
944 aflag = 1;
945 format_chars++;
946 }
1ccf251f
RK
947 if (suppressed && length_char != 0)
948 {
949 sprintf (message,
950 "use of `*' and `%c' together in format",
951 length_char);
952 warning (message);
953 }
954 format_char = *format_chars;
955 if (format_char == 0)
956 {
957 warning ("conversion lacks type at end of format");
958 continue;
959 }
960 format_chars++;
961 fci = info->is_scan ? scan_char_table : print_char_table;
962 while (fci->format_chars != 0
963 && index (fci->format_chars, format_char) == 0)
964 ++fci;
965 if (fci->format_chars == 0)
966 {
967 if (format_char >= 040 && format_char < 0177)
968 sprintf (message,
969 "unknown conversion type character `%c' in format",
970 format_char);
971 else
972 sprintf (message,
973 "unknown conversion type character 0x%x in format",
974 format_char);
975 warning (message);
976 continue;
977 }
978 if (wide && index (fci->flag_chars, 'w') == 0)
979 {
980 sprintf (message, "width used with `%c' format",
981 format_char);
982 warning (message);
983 }
984 if (precise && index (fci->flag_chars, 'p') == 0)
985 {
986 sprintf (message, "precision used with `%c' format",
987 format_char);
988 warning (message);
af3c5588
RK
989 }
990 if (aflag && index (fci->flag_chars, 'a') == 0)
991 {
992 sprintf (message, "`a' flag used with `%c' format",
993 format_char);
994 warning (message);
1ccf251f
RK
995 }
996 if (info->is_scan && format_char == '[')
997 {
998 /* Skip over scan set, in case it happens to have '%' in it. */
999 if (*format_chars == '^')
1000 ++format_chars;
1001 /* Find closing bracket; if one is hit immediately, then
1002 it's part of the scan set rather than a terminator. */
1003 if (*format_chars == ']')
1004 ++format_chars;
1005 while (*format_chars && *format_chars != ']')
1006 ++format_chars;
1007 if (*format_chars != ']')
1008 /* The end of the format string was reached. */
1009 warning ("no closing `]' for `%%[' format");
1010 }
1011 if (suppressed)
1012 {
1013 if (index (fci->flag_chars, '*') == 0)
1014 {
1015 sprintf (message,
1016 "suppression of `%c' conversion in format",
1017 format_char);
1018 warning (message);
1019 }
1020 continue;
1021 }
1022 for (i = 0; flag_chars[i] != 0; ++i)
1023 {
1024 if (index (fci->flag_chars, flag_chars[i]) == 0)
1025 {
1026 sprintf (message, "flag `%c' used with type `%c'",
1027 flag_chars[i], format_char);
1028 warning (message);
1029 }
1030 }
1031 if (precise && index (flag_chars, '0') != 0
1032 && (format_char == 'd' || format_char == 'i'
1033 || format_char == 'o' || format_char == 'u'
1034 || format_char == 'x' || format_char == 'x'))
1035 {
1036 sprintf (message,
1037 "precision and `0' flag not both allowed with `%c' format",
1038 format_char);
1039 warning (message);
1040 }
1041 switch (length_char)
1042 {
1043 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
1044 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1045 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
2314fb70 1046 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1ccf251f
RK
1047 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
1048 }
1049 if (wanted_type == 0)
1050 {
1051 sprintf (message,
1052 "use of `%c' length character with `%c' type character",
1053 length_char, format_char);
1054 warning (message);
1055 }
1056
1057 /*
1058 ** XXX -- should kvetch about stuff such as
1059 ** {
1060 ** const int i;
1061 **
1062 ** scanf ("%d", &i);
1063 ** }
1064 */
1065
1066 /* Finally. . .check type of argument against desired type! */
1067 if (info->first_arg_num == 0)
1068 continue;
1069 if (params == 0)
1070 {
1071 warning (tfaff);
1072 return;
1073 }
1074 cur_param = TREE_VALUE (params);
1075 params = TREE_CHAIN (params);
1076 ++arg_num;
1077 cur_type = TREE_TYPE (cur_param);
1078
1079 /* Check the types of any additional pointer arguments
1080 that precede the "real" argument. */
1081 for (i = 0; i < fci->pointer_count; ++i)
1082 {
1083 if (TREE_CODE (cur_type) == POINTER_TYPE)
1084 {
1085 cur_type = TREE_TYPE (cur_type);
1086 continue;
1087 }
1088 sprintf (message,
1089 "format argument is not a %s (arg %d)",
1090 ((fci->pointer_count == 1) ? "pointer" : "pointer to a pointer"),
1091 arg_num);
1092 warning (message);
1093 break;
1094 }
1095
1096 /* Check the type of the "real" argument, if there's a type we want. */
1097 if (i == fci->pointer_count && wanted_type != 0
1098 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1099 /* If we want `void *', allow any pointer type.
1100 (Anything else would already have got a warning.) */
1101 && ! (wanted_type == void_type_node
1102 && fci->pointer_count > 0)
1103 /* Don't warn about differences merely in signedness. */
1104 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
b7c9c707 1105 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
4215e498 1106 && (TREE_UNSIGNED (wanted_type)
f5775325
RK
1107 ? wanted_type == (cur_type = unsigned_type (cur_type))
1108 : wanted_type == (cur_type = signed_type (cur_type))))
60e02b1e
RK
1109 /* Likewise, "signed char", "unsigned char" and "char" are
1110 equivalent but the above test won't consider them equivalent. */
1111 && ! (wanted_type == char_type_node
b7c9c707
RS
1112 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
1113 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
1ccf251f
RK
1114 {
1115 register char *this;
1116 register char *that;
1117
1118 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
1119 that = 0;
1120 if (TREE_CODE (cur_type) != ERROR_MARK
1121 && TYPE_NAME (cur_type) != 0
1122 && TREE_CODE (cur_type) != INTEGER_TYPE
1123 && !(TREE_CODE (cur_type) == POINTER_TYPE
1124 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
1125 {
1126 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
1127 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
1128 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1129 else
1130 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
1131 }
1132
1133 /* A nameless type can't possibly match what the format wants.
1134 So there will be a warning for it.
1135 Make up a string to describe vaguely what it is. */
1136 if (that == 0)
1137 {
1138 if (TREE_CODE (cur_type) == POINTER_TYPE)
1139 that = "pointer";
1140 else
1141 that = "different type";
1142 }
1143
309ffab6
RS
1144 /* Make the warning better in case of mismatch of int vs long. */
1145 if (TREE_CODE (cur_type) == INTEGER_TYPE
1146 && TREE_CODE (wanted_type) == INTEGER_TYPE
1147 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
1148 && TYPE_NAME (cur_type) != 0
1149 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
1150 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1151
1ccf251f
RK
1152 if (strcmp (this, that) != 0)
1153 {
1154 sprintf (message, "%s format, %s arg (arg %d)",
1155 this, that, arg_num);
1156 warning (message);
1157 }
1158 }
1159 }
1160}
1161\f
d74154d5
RS
1162/* Print a warning if a constant expression had overflow in folding.
1163 Invoke this function on every expression that the language
1164 requires to be a constant expression.
1165 Note the ANSI C standard says it is erroneous for a
1166 constant expression to overflow. */
96571883
BK
1167
1168void
1169constant_expression_warning (value)
1170 tree value;
1171{
c05f751c
RK
1172 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1173 || TREE_CODE (value) == COMPLEX_CST)
1174 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1175 pedwarn ("overflow in constant expression");
d74154d5
RS
1176}
1177
1178/* Print a warning if an expression had overflow in folding.
1179 Invoke this function on every expression that
1180 (1) appears in the source code, and
1181 (2) might be a constant expression that overflowed, and
1182 (3) is not already checked by convert_and_check;
1183 however, do not invoke this function on operands of explicit casts. */
1184
1185void
1186overflow_warning (value)
1187 tree value;
1188{
c05f751c
RK
1189 if ((TREE_CODE (value) == INTEGER_CST
1190 || (TREE_CODE (value) == COMPLEX_CST
1191 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1192 && TREE_OVERFLOW (value))
d74154d5 1193 {
7193bce2 1194 TREE_OVERFLOW (value) = 0;
14aadfe8 1195 warning ("integer overflow in expression");
d74154d5 1196 }
c05f751c
RK
1197 else if ((TREE_CODE (value) == REAL_CST
1198 || (TREE_CODE (value) == COMPLEX_CST
1199 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1200 && TREE_OVERFLOW (value))
1201 {
1202 TREE_OVERFLOW (value) = 0;
1203 warning ("floating-pointer overflow in expression");
1204 }
d74154d5
RS
1205}
1206
1207/* Print a warning if a large constant is truncated to unsigned,
1208 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1209 Invoke this function on every expression that might be implicitly
1210 converted to an unsigned type. */
1211
1212void
1213unsigned_conversion_warning (result, operand)
1214 tree result, operand;
1215{
1216 if (TREE_CODE (operand) == INTEGER_CST
1217 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
1218 && TREE_UNSIGNED (TREE_TYPE (result))
1219 && !int_fits_type_p (operand, TREE_TYPE (result)))
1220 {
1221 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
1222 /* This detects cases like converting -129 or 256 to unsigned char. */
90c939d4 1223 warning ("large integer implicitly truncated to unsigned type");
d74154d5 1224 else if (warn_conversion)
90c939d4 1225 warning ("negative integer implicitly converted to unsigned type");
d74154d5
RS
1226 }
1227}
1228
1229/* Convert EXPR to TYPE, warning about conversion problems with constants.
1230 Invoke this function on every expression that is converted implicitly,
1231 i.e. because of language rules and not because of an explicit cast. */
1232
1233tree
1234convert_and_check (type, expr)
1235 tree type, expr;
1236{
1237 tree t = convert (type, expr);
1238 if (TREE_CODE (t) == INTEGER_CST)
1239 {
7193bce2 1240 if (TREE_OVERFLOW (t))
d74154d5 1241 {
7193bce2
PE
1242 TREE_OVERFLOW (t) = 0;
1243
1244 /* No warning for converting 0x80000000 to int. */
1245 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
1246 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1247 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
22ba338b
RS
1248 /* If EXPR fits in the unsigned version of TYPE,
1249 don't warn unless pedantic. */
1250 if (pedantic
1251 || TREE_UNSIGNED (type)
1252 || ! int_fits_type_p (expr, unsigned_type (type)))
1253 warning ("overflow in implicit constant conversion");
d74154d5
RS
1254 }
1255 else
1256 unsigned_conversion_warning (t, expr);
1257 }
1258 return t;
96571883
BK
1259}
1260\f
b30f223b
RS
1261void
1262c_expand_expr_stmt (expr)
1263 tree expr;
1264{
1265 /* Do default conversion if safe and possibly important,
1266 in case within ({...}). */
1267 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
1268 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1269 expr = default_conversion (expr);
1270
1271 if (TREE_TYPE (expr) != error_mark_node
1272 && TYPE_SIZE (TREE_TYPE (expr)) == 0
1273 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1274 error ("expression statement has incomplete type");
1275
1276 expand_expr_stmt (expr);
1277}
1278\f
1279/* Validate the expression after `case' and apply default promotions. */
1280
1281tree
1282check_case_value (value)
1283 tree value;
1284{
1285 if (value == NULL_TREE)
1286 return value;
1287
1288 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8493738b 1289 STRIP_TYPE_NOPS (value);
b30f223b
RS
1290
1291 if (TREE_CODE (value) != INTEGER_CST
1292 && value != error_mark_node)
1293 {
1294 error ("case label does not reduce to an integer constant");
1295 value = error_mark_node;
1296 }
1297 else
1298 /* Promote char or short to int. */
1299 value = default_conversion (value);
1300
bc690db1
RS
1301 constant_expression_warning (value);
1302
b30f223b
RS
1303 return value;
1304}
1305\f
1306/* Return an integer type with BITS bits of precision,
1307 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1308
1309tree
1310type_for_size (bits, unsignedp)
1311 unsigned bits;
1312 int unsignedp;
1313{
a311b52c
JM
1314 if (bits == TYPE_PRECISION (integer_type_node))
1315 return unsignedp ? unsigned_type_node : integer_type_node;
1316
3fc7e390 1317 if (bits == TYPE_PRECISION (signed_char_type_node))
b30f223b
RS
1318 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1319
3fc7e390 1320 if (bits == TYPE_PRECISION (short_integer_type_node))
b30f223b
RS
1321 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1322
3fc7e390 1323 if (bits == TYPE_PRECISION (long_integer_type_node))
b30f223b
RS
1324 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1325
3fc7e390 1326 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b30f223b
RS
1327 return (unsignedp ? long_long_unsigned_type_node
1328 : long_long_integer_type_node);
1329
3fc7e390
RS
1330 if (bits <= TYPE_PRECISION (intQI_type_node))
1331 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1332
1333 if (bits <= TYPE_PRECISION (intHI_type_node))
1334 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1335
1336 if (bits <= TYPE_PRECISION (intSI_type_node))
1337 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1338
1339 if (bits <= TYPE_PRECISION (intDI_type_node))
1340 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1341
b30f223b
RS
1342 return 0;
1343}
1344
1345/* Return a data type that has machine mode MODE.
1346 If the mode is an integer,
1347 then UNSIGNEDP selects between signed and unsigned types. */
1348
1349tree
1350type_for_mode (mode, unsignedp)
1351 enum machine_mode mode;
1352 int unsignedp;
1353{
a311b52c
JM
1354 if (mode == TYPE_MODE (integer_type_node))
1355 return unsignedp ? unsigned_type_node : integer_type_node;
1356
b30f223b
RS
1357 if (mode == TYPE_MODE (signed_char_type_node))
1358 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1359
1360 if (mode == TYPE_MODE (short_integer_type_node))
1361 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1362
b30f223b
RS
1363 if (mode == TYPE_MODE (long_integer_type_node))
1364 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1365
1366 if (mode == TYPE_MODE (long_long_integer_type_node))
1367 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
1368
3fc7e390
RS
1369 if (mode == TYPE_MODE (intQI_type_node))
1370 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1371
1372 if (mode == TYPE_MODE (intHI_type_node))
1373 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1374
1375 if (mode == TYPE_MODE (intSI_type_node))
1376 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1377
1378 if (mode == TYPE_MODE (intDI_type_node))
1379 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1380
b30f223b
RS
1381 if (mode == TYPE_MODE (float_type_node))
1382 return float_type_node;
1383
1384 if (mode == TYPE_MODE (double_type_node))
1385 return double_type_node;
1386
1387 if (mode == TYPE_MODE (long_double_type_node))
1388 return long_double_type_node;
1389
1390 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
1391 return build_pointer_type (char_type_node);
1392
1393 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
1394 return build_pointer_type (integer_type_node);
1395
1396 return 0;
1397}
1398\f
6acfe908
JM
1399/* Return the minimum number of bits needed to represent VALUE in a
1400 signed or unsigned type, UNSIGNEDP says which. */
1401
1402int
1403min_precision (value, unsignedp)
1404 tree value;
1405 int unsignedp;
1406{
1407 int log;
1408
1409 /* If the value is negative, compute its negative minus 1. The latter
1410 adjustment is because the absolute value of the largest negative value
1411 is one larger than the largest positive value. This is equivalent to
1412 a bit-wise negation, so use that operation instead. */
1413
1414 if (tree_int_cst_sgn (value) < 0)
1415 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
1416
1417 /* Return the number of bits needed, taking into account the fact
1418 that we need one more bit for a signed than unsigned type. */
1419
1420 if (integer_zerop (value))
1421 log = 0;
1422 else if (TREE_INT_CST_HIGH (value) != 0)
1423 log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
1424 else
1425 log = floor_log2 (TREE_INT_CST_LOW (value));
1426
1427 return log + 1 + ! unsignedp;
1428}
1429\f
b30f223b
RS
1430/* Print an error message for invalid operands to arith operation CODE.
1431 NOP_EXPR is used as a special case (see truthvalue_conversion). */
1432
1433void
1434binary_op_error (code)
1435 enum tree_code code;
1436{
89c78d7d
RK
1437 register char *opname = "unknown";
1438
b30f223b
RS
1439 switch (code)
1440 {
1441 case NOP_EXPR:
1442 error ("invalid truth-value expression");
1443 return;
1444
1445 case PLUS_EXPR:
1446 opname = "+"; break;
1447 case MINUS_EXPR:
1448 opname = "-"; break;
1449 case MULT_EXPR:
1450 opname = "*"; break;
1451 case MAX_EXPR:
1452 opname = "max"; break;
1453 case MIN_EXPR:
1454 opname = "min"; break;
1455 case EQ_EXPR:
1456 opname = "=="; break;
1457 case NE_EXPR:
1458 opname = "!="; break;
1459 case LE_EXPR:
1460 opname = "<="; break;
1461 case GE_EXPR:
1462 opname = ">="; break;
1463 case LT_EXPR:
1464 opname = "<"; break;
1465 case GT_EXPR:
1466 opname = ">"; break;
1467 case LSHIFT_EXPR:
1468 opname = "<<"; break;
1469 case RSHIFT_EXPR:
1470 opname = ">>"; break;
1471 case TRUNC_MOD_EXPR:
047de90b 1472 case FLOOR_MOD_EXPR:
b30f223b
RS
1473 opname = "%"; break;
1474 case TRUNC_DIV_EXPR:
047de90b 1475 case FLOOR_DIV_EXPR:
b30f223b
RS
1476 opname = "/"; break;
1477 case BIT_AND_EXPR:
1478 opname = "&"; break;
1479 case BIT_IOR_EXPR:
1480 opname = "|"; break;
1481 case TRUTH_ANDIF_EXPR:
1482 opname = "&&"; break;
1483 case TRUTH_ORIF_EXPR:
1484 opname = "||"; break;
1485 case BIT_XOR_EXPR:
1486 opname = "^"; break;
047de90b
RS
1487 case LROTATE_EXPR:
1488 case RROTATE_EXPR:
1489 opname = "rotate"; break;
b30f223b
RS
1490 }
1491 error ("invalid operands to binary %s", opname);
1492}
1493\f
1494/* Subroutine of build_binary_op, used for comparison operations.
1495 See if the operands have both been converted from subword integer types
1496 and, if so, perhaps change them both back to their original type.
94dccd9d
RS
1497 This function is also responsible for converting the two operands
1498 to the proper common type for comparison.
b30f223b
RS
1499
1500 The arguments of this function are all pointers to local variables
1501 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
1502 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
1503
1504 If this function returns nonzero, it means that the comparison has
1505 a constant value. What this function returns is an expression for
1506 that value. */
1507
1508tree
1509shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
1510 tree *op0_ptr, *op1_ptr;
1511 tree *restype_ptr;
1512 enum tree_code *rescode_ptr;
1513{
1514 register tree type;
1515 tree op0 = *op0_ptr;
1516 tree op1 = *op1_ptr;
1517 int unsignedp0, unsignedp1;
1518 int real1, real2;
1519 tree primop0, primop1;
1520 enum tree_code code = *rescode_ptr;
1521
1522 /* Throw away any conversions to wider types
1523 already present in the operands. */
1524
1525 primop0 = get_narrower (op0, &unsignedp0);
1526 primop1 = get_narrower (op1, &unsignedp1);
1527
1528 /* Handle the case that OP0 does not *contain* a conversion
1529 but it *requires* conversion to FINAL_TYPE. */
1530
1531 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
1532 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
1533 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
1534 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
1535
1536 /* If one of the operands must be floated, we cannot optimize. */
1537 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
1538 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
1539
1540 /* If first arg is constant, swap the args (changing operation
5af6001b
RK
1541 so value is preserved), for canonicalization. Don't do this if
1542 the second arg is 0. */
b30f223b 1543
5af6001b
RK
1544 if (TREE_CONSTANT (primop0)
1545 && ! integer_zerop (primop1) && ! real_zerop (primop1))
b30f223b
RS
1546 {
1547 register tree tem = primop0;
1548 register int temi = unsignedp0;
1549 primop0 = primop1;
1550 primop1 = tem;
1551 tem = op0;
1552 op0 = op1;
1553 op1 = tem;
1554 *op0_ptr = op0;
1555 *op1_ptr = op1;
1556 unsignedp0 = unsignedp1;
1557 unsignedp1 = temi;
1558 temi = real1;
1559 real1 = real2;
1560 real2 = temi;
1561
1562 switch (code)
1563 {
1564 case LT_EXPR:
1565 code = GT_EXPR;
1566 break;
1567 case GT_EXPR:
1568 code = LT_EXPR;
1569 break;
1570 case LE_EXPR:
1571 code = GE_EXPR;
1572 break;
1573 case GE_EXPR:
1574 code = LE_EXPR;
1575 break;
1576 }
1577 *rescode_ptr = code;
1578 }
1579
1580 /* If comparing an integer against a constant more bits wide,
1581 maybe we can deduce a value of 1 or 0 independent of the data.
1582 Or else truncate the constant now
1583 rather than extend the variable at run time.
1584
1585 This is only interesting if the constant is the wider arg.
1586 Also, it is not safe if the constant is unsigned and the
1587 variable arg is signed, since in this case the variable
1588 would be sign-extended and then regarded as unsigned.
1589 Our technique fails in this case because the lowest/highest
1590 possible unsigned results don't follow naturally from the
1591 lowest/highest possible values of the variable operand.
1592 For just EQ_EXPR and NE_EXPR there is another technique that
1593 could be used: see if the constant can be faithfully represented
1594 in the other operand's type, by truncating it and reextending it
1595 and see if that preserves the constant's value. */
1596
1597 if (!real1 && !real2
1598 && TREE_CODE (primop1) == INTEGER_CST
1599 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
1600 {
1601 int min_gt, max_gt, min_lt, max_lt;
1602 tree maxval, minval;
1603 /* 1 if comparison is nominally unsigned. */
1604 int unsignedp = TREE_UNSIGNED (*restype_ptr);
1605 tree val;
1606
1607 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
1608
1609 maxval = TYPE_MAX_VALUE (type);
1610 minval = TYPE_MIN_VALUE (type);
1611
1612 if (unsignedp && !unsignedp0)
1613 *restype_ptr = signed_type (*restype_ptr);
1614
1615 if (TREE_TYPE (primop1) != *restype_ptr)
1616 primop1 = convert (*restype_ptr, primop1);
1617 if (type != *restype_ptr)
1618 {
1619 minval = convert (*restype_ptr, minval);
1620 maxval = convert (*restype_ptr, maxval);
1621 }
1622
1623 if (unsignedp && unsignedp0)
1624 {
1625 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
1626 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
1627 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
1628 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
1629 }
1630 else
1631 {
1632 min_gt = INT_CST_LT (primop1, minval);
1633 max_gt = INT_CST_LT (primop1, maxval);
1634 min_lt = INT_CST_LT (minval, primop1);
1635 max_lt = INT_CST_LT (maxval, primop1);
1636 }
1637
1638 val = 0;
1639 /* This used to be a switch, but Genix compiler can't handle that. */
1640 if (code == NE_EXPR)
1641 {
1642 if (max_lt || min_gt)
a360da3a 1643 val = boolean_true_node;
b30f223b
RS
1644 }
1645 else if (code == EQ_EXPR)
1646 {
1647 if (max_lt || min_gt)
a360da3a 1648 val = boolean_false_node;
b30f223b
RS
1649 }
1650 else if (code == LT_EXPR)
1651 {
1652 if (max_lt)
a360da3a 1653 val = boolean_true_node;
b30f223b 1654 if (!min_lt)
a360da3a 1655 val = boolean_false_node;
b30f223b
RS
1656 }
1657 else if (code == GT_EXPR)
1658 {
1659 if (min_gt)
a360da3a 1660 val = boolean_true_node;
b30f223b 1661 if (!max_gt)
a360da3a 1662 val = boolean_false_node;
b30f223b
RS
1663 }
1664 else if (code == LE_EXPR)
1665 {
1666 if (!max_gt)
a360da3a 1667 val = boolean_true_node;
b30f223b 1668 if (min_gt)
a360da3a 1669 val = boolean_false_node;
b30f223b
RS
1670 }
1671 else if (code == GE_EXPR)
1672 {
1673 if (!min_lt)
a360da3a 1674 val = boolean_true_node;
b30f223b 1675 if (max_lt)
a360da3a 1676 val = boolean_false_node;
b30f223b
RS
1677 }
1678
1679 /* If primop0 was sign-extended and unsigned comparison specd,
1680 we did a signed comparison above using the signed type bounds.
1681 But the comparison we output must be unsigned.
1682
1683 Also, for inequalities, VAL is no good; but if the signed
1684 comparison had *any* fixed result, it follows that the
1685 unsigned comparison just tests the sign in reverse
1686 (positive values are LE, negative ones GE).
1687 So we can generate an unsigned comparison
1688 against an extreme value of the signed type. */
1689
1690 if (unsignedp && !unsignedp0)
1691 {
1692 if (val != 0)
1693 switch (code)
1694 {
1695 case LT_EXPR:
1696 case GE_EXPR:
1697 primop1 = TYPE_MIN_VALUE (type);
1698 val = 0;
1699 break;
1700
1701 case LE_EXPR:
1702 case GT_EXPR:
1703 primop1 = TYPE_MAX_VALUE (type);
1704 val = 0;
1705 break;
1706 }
1707 type = unsigned_type (type);
1708 }
1709
b7c9c707 1710 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
b30f223b
RS
1711 {
1712 /* This is the case of (char)x >?< 0x80, which people used to use
1713 expecting old C compilers to change the 0x80 into -0x80. */
a360da3a 1714 if (val == boolean_false_node)
b30f223b 1715 warning ("comparison is always 0 due to limited range of data type");
a360da3a 1716 if (val == boolean_true_node)
b30f223b
RS
1717 warning ("comparison is always 1 due to limited range of data type");
1718 }
1719
b7c9c707 1720 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
b30f223b 1721 {
1e276c4a 1722 /* This is the case of (unsigned char)x >?< -1 or < 0. */
a360da3a 1723 if (val == boolean_false_node)
b30f223b 1724 warning ("comparison is always 0 due to limited range of data type");
a360da3a 1725 if (val == boolean_true_node)
b30f223b
RS
1726 warning ("comparison is always 1 due to limited range of data type");
1727 }
1728
1729 if (val != 0)
1730 {
1731 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
1732 if (TREE_SIDE_EFFECTS (primop0))
1733 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
1734 return val;
1735 }
1736
1737 /* Value is not predetermined, but do the comparison
1738 in the type of the operand that is not constant.
1739 TYPE is already properly set. */
1740 }
1741 else if (real1 && real2
766f6c30
RS
1742 && (TYPE_PRECISION (TREE_TYPE (primop0))
1743 == TYPE_PRECISION (TREE_TYPE (primop1))))
b30f223b
RS
1744 type = TREE_TYPE (primop0);
1745
1746 /* If args' natural types are both narrower than nominal type
1747 and both extend in the same manner, compare them
1748 in the type of the wider arg.
1749 Otherwise must actually extend both to the nominal
1750 common type lest different ways of extending
1751 alter the result.
1752 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
1753
1754 else if (unsignedp0 == unsignedp1 && real1 == real2
1755 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
1756 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
1757 {
1758 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
1759 type = signed_or_unsigned_type (unsignedp0
1760 || TREE_UNSIGNED (*restype_ptr),
1761 type);
1762 /* Make sure shorter operand is extended the right way
1763 to match the longer operand. */
1764 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
1765 primop0);
1766 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
1767 primop1);
1768 }
1769 else
1770 {
1771 /* Here we must do the comparison on the nominal type
1772 using the args exactly as we received them. */
1773 type = *restype_ptr;
1774 primop0 = op0;
1775 primop1 = op1;
1776
1777 if (!real1 && !real2 && integer_zerop (primop1)
597681f6 1778 && TREE_UNSIGNED (*restype_ptr))
b30f223b
RS
1779 {
1780 tree value = 0;
1781 switch (code)
1782 {
1783 case GE_EXPR:
5af6001b
RK
1784 /* All unsigned values are >= 0, so we warn if extra warnings
1785 are requested. However, if OP0 is a constant that is
1786 >= 0, the signedness of the comparison isn't an issue,
1787 so suppress the warning. */
1788 if (extra_warnings
1789 && ! (TREE_CODE (primop0) == INTEGER_CST
1790 && ! TREE_OVERFLOW (convert (signed_type (type),
1791 primop0))))
b30f223b 1792 warning ("unsigned value >= 0 is always 1");
a360da3a 1793 value = boolean_true_node;
b30f223b
RS
1794 break;
1795
1796 case LT_EXPR:
5af6001b
RK
1797 if (extra_warnings
1798 && ! (TREE_CODE (primop0) == INTEGER_CST
1799 && ! TREE_OVERFLOW (convert (signed_type (type),
1800 primop0))))
b30f223b 1801 warning ("unsigned value < 0 is always 0");
a360da3a 1802 value = boolean_false_node;
b30f223b
RS
1803 }
1804
1805 if (value != 0)
1806 {
1807 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
1808 if (TREE_SIDE_EFFECTS (primop0))
1809 return build (COMPOUND_EXPR, TREE_TYPE (value),
1810 primop0, value);
1811 return value;
1812 }
1813 }
1814 }
1815
1816 *op0_ptr = convert (type, primop0);
1817 *op1_ptr = convert (type, primop1);
1818
a360da3a 1819 *restype_ptr = boolean_type_node;
b30f223b
RS
1820
1821 return 0;
1822}
1823\f
1824/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
1825 or validate its data type for an `if' or `while' statement or ?..: exp.
1826
1827 This preparation consists of taking the ordinary
1828 representation of an expression expr and producing a valid tree
1829 boolean expression describing whether expr is nonzero. We could
a360da3a 1830 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
b30f223b
RS
1831 but we optimize comparisons, &&, ||, and !.
1832
a360da3a 1833 The resulting type should always be `boolean_type_node'. */
b30f223b
RS
1834
1835tree
1836truthvalue_conversion (expr)
1837 tree expr;
1838{
257e61ed
RS
1839 if (TREE_CODE (expr) == ERROR_MARK)
1840 return expr;
1841
d7c83727 1842#if 0 /* This appears to be wrong for C++. */
257e61ed
RS
1843 /* These really should return error_mark_node after 2.4 is stable.
1844 But not all callers handle ERROR_MARK properly. */
1845 switch (TREE_CODE (TREE_TYPE (expr)))
1846 {
1847 case RECORD_TYPE:
1848 error ("struct type value used where scalar is required");
a360da3a 1849 return boolean_false_node;
257e61ed
RS
1850
1851 case UNION_TYPE:
1852 error ("union type value used where scalar is required");
a360da3a 1853 return boolean_false_node;
257e61ed
RS
1854
1855 case ARRAY_TYPE:
1856 error ("array type value used where scalar is required");
a360da3a 1857 return boolean_false_node;
257e61ed
RS
1858
1859 default:
1860 break;
1861 }
d7c83727 1862#endif /* 0 */
257e61ed 1863
b30f223b
RS
1864 switch (TREE_CODE (expr))
1865 {
1866 /* It is simpler and generates better code to have only TRUTH_*_EXPR
1867 or comparison expressions as truth values at this level. */
1868#if 0
1869 case COMPONENT_REF:
1870 /* A one-bit unsigned bit-field is already acceptable. */
1871 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
1872 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
1873 return expr;
1874 break;
1875#endif
1876
1877 case EQ_EXPR:
1878 /* It is simpler and generates better code to have only TRUTH_*_EXPR
1879 or comparison expressions as truth values at this level. */
1880#if 0
1881 if (integer_zerop (TREE_OPERAND (expr, 1)))
1882 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
1883#endif
1884 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
1885 case TRUTH_ANDIF_EXPR:
1886 case TRUTH_ORIF_EXPR:
1887 case TRUTH_AND_EXPR:
1888 case TRUTH_OR_EXPR:
9379fac9 1889 case TRUTH_XOR_EXPR:
a360da3a
JM
1890 TREE_TYPE (expr) = boolean_type_node;
1891 return expr;
18c0f675 1892
b30f223b
RS
1893 case ERROR_MARK:
1894 return expr;
1895
1896 case INTEGER_CST:
a360da3a 1897 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
b30f223b
RS
1898
1899 case REAL_CST:
a360da3a 1900 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
b30f223b
RS
1901
1902 case ADDR_EXPR:
1903 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
a360da3a
JM
1904 return build (COMPOUND_EXPR, boolean_type_node,
1905 TREE_OPERAND (expr, 0), boolean_true_node);
b30f223b 1906 else
a360da3a 1907 return boolean_true_node;
b30f223b 1908
766f6c30 1909 case COMPLEX_EXPR:
f0b996c5 1910 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
b839fb3f 1911 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
f0b996c5
RS
1912 truthvalue_conversion (TREE_OPERAND (expr, 0)),
1913 truthvalue_conversion (TREE_OPERAND (expr, 1)),
766f6c30
RS
1914 0);
1915
b30f223b
RS
1916 case NEGATE_EXPR:
1917 case ABS_EXPR:
1918 case FLOAT_EXPR:
1919 case FFS_EXPR:
1920 /* These don't change whether an object is non-zero or zero. */
1921 return truthvalue_conversion (TREE_OPERAND (expr, 0));
1922
1923 case LROTATE_EXPR:
1924 case RROTATE_EXPR:
1925 /* These don't change whether an object is zero or non-zero, but
1926 we can't ignore them if their second arg has side-effects. */
1927 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
a360da3a 1928 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
b30f223b
RS
1929 truthvalue_conversion (TREE_OPERAND (expr, 0)));
1930 else
1931 return truthvalue_conversion (TREE_OPERAND (expr, 0));
1932
1933 case COND_EXPR:
1934 /* Distribute the conversion into the arms of a COND_EXPR. */
a360da3a 1935 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
b30f223b
RS
1936 truthvalue_conversion (TREE_OPERAND (expr, 1)),
1937 truthvalue_conversion (TREE_OPERAND (expr, 2))));
1938
1939 case CONVERT_EXPR:
1940 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
1941 since that affects how `default_conversion' will behave. */
1942 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
1943 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
1944 break;
1945 /* fall through... */
1946 case NOP_EXPR:
1947 /* If this is widening the argument, we can ignore it. */
1948 if (TYPE_PRECISION (TREE_TYPE (expr))
1949 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
1950 return truthvalue_conversion (TREE_OPERAND (expr, 0));
1951 break;
1952
b30f223b 1953 case MINUS_EXPR:
f87550e0
JW
1954 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
1955 this case. */
1956 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1957 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
1958 break;
1959 /* fall through... */
1960 case BIT_XOR_EXPR:
d7c83727 1961 /* This and MINUS_EXPR can be changed into a comparison of the
f87550e0 1962 two objects. */
b30f223b
RS
1963 if (TREE_TYPE (TREE_OPERAND (expr, 0))
1964 == TREE_TYPE (TREE_OPERAND (expr, 1)))
1965 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
1966 TREE_OPERAND (expr, 1), 1);
1967 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
1968 fold (build1 (NOP_EXPR,
1969 TREE_TYPE (TREE_OPERAND (expr, 0)),
1970 TREE_OPERAND (expr, 1))), 1);
e2aab13d 1971
fb48b1f0
JM
1972 case BIT_AND_EXPR:
1973 if (integer_onep (TREE_OPERAND (expr, 1)))
1974 return expr;
1975
e2aab13d
RS
1976 case MODIFY_EXPR:
1977 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
1978 warning ("suggest parentheses around assignment used as truth value");
1979 break;
b30f223b
RS
1980 }
1981
f0b996c5
RS
1982 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
1983 return (build_binary_op
1984 ((TREE_SIDE_EFFECTS (expr)
b839fb3f 1985 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
f0b996c5
RS
1986 truthvalue_conversion (build_unary_op (REALPART_EXPR, expr, 0)),
1987 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, expr, 0)),
1988 0));
1989
b30f223b
RS
1990 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
1991}
1992\f
1993/* Read the rest of a #-directive from input stream FINPUT.
1994 In normal use, the directive name and the white space after it
1995 have already been read, so they won't be included in the result.
1996 We allow for the fact that the directive line may contain
1997 a newline embedded within a character or string literal which forms
1998 a part of the directive.
1999
2000 The value is a string in a reusable buffer. It remains valid
2001 only until the next time this function is called. */
2002
2003char *
2004get_directive_line (finput)
2005 register FILE *finput;
2006{
2007 static char *directive_buffer = NULL;
2008 static unsigned buffer_length = 0;
2009 register char *p;
2010 register char *buffer_limit;
2011 register int looking_for = 0;
2012 register int char_escaped = 0;
2013
2014 if (buffer_length == 0)
2015 {
2016 directive_buffer = (char *)xmalloc (128);
2017 buffer_length = 128;
2018 }
2019
2020 buffer_limit = &directive_buffer[buffer_length];
2021
2022 for (p = directive_buffer; ; )
2023 {
2024 int c;
2025
2026 /* Make buffer bigger if it is full. */
2027 if (p >= buffer_limit)
2028 {
2029 register unsigned bytes_used = (p - directive_buffer);
2030
2031 buffer_length *= 2;
2032 directive_buffer
2033 = (char *)xrealloc (directive_buffer, buffer_length);
2034 p = &directive_buffer[bytes_used];
2035 buffer_limit = &directive_buffer[buffer_length];
2036 }
2037
2038 c = getc (finput);
2039
2040 /* Discard initial whitespace. */
2041 if ((c == ' ' || c == '\t') && p == directive_buffer)
2042 continue;
2043
2044 /* Detect the end of the directive. */
2045 if (c == '\n' && looking_for == 0)
2046 {
2047 ungetc (c, finput);
2048 c = '\0';
2049 }
2050
2051 *p++ = c;
2052
2053 if (c == 0)
2054 return directive_buffer;
2055
2056 /* Handle string and character constant syntax. */
2057 if (looking_for)
2058 {
2059 if (looking_for == c && !char_escaped)
2060 looking_for = 0; /* Found terminator... stop looking. */
2061 }
2062 else
2063 if (c == '\'' || c == '"')
2064 looking_for = c; /* Don't stop buffering until we see another
2065 another one of these (or an EOF). */
2066
2067 /* Handle backslash. */
2068 char_escaped = (c == '\\' && ! char_escaped);
2069 }
2070}
0b73773c
NH
2071\f
2072/* Make a variant type in the proper way for C/C++, propagating qualifiers
2073 down to the element type of an array. */
2074
2075tree
2076c_build_type_variant (type, constp, volatilep)
2077 tree type;
2078 int constp, volatilep;
2079{
2080 if (TREE_CODE (type) == ARRAY_TYPE)
2081 {
084b6d7b
RK
2082 tree real_main_variant = TYPE_MAIN_VARIANT (type);
2083
2084 push_obstacks (TYPE_OBSTACK (real_main_variant),
2085 TYPE_OBSTACK (real_main_variant));
0b73773c
NH
2086 type = build_array_type (c_build_type_variant (TREE_TYPE (type),
2087 constp, volatilep),
2088 TYPE_DOMAIN (type));
ba5ce70d 2089
084b6d7b
RK
2090 /* TYPE must be on same obstack as REAL_MAIN_VARIANT. If not,
2091 make a copy. (TYPE might have come from the hash table and
2092 REAL_MAIN_VARIANT might be in some function's obstack.) */
2093
2094 if (TYPE_OBSTACK (type) != TYPE_OBSTACK (real_main_variant))
2095 {
2096 type = copy_node (type);
2097 TYPE_POINTER_TO (type) = TYPE_REFERENCE_TO (type) = 0;
2098 }
2099
2100 TYPE_MAIN_VARIANT (type) = real_main_variant;
2101 pop_obstacks ();
0b73773c
NH
2102 }
2103 return build_type_variant (type, constp, volatilep);
2104}