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