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