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