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