* except.c (expand_start_catch_block): We only need the rethrow
[official-gcc.git] / gcc / c-common.c
blobc282f16663e13e68c20ec9ec64135f259669ae9d
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
4 This file is part of GNU CC.
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.
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.
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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "config.h"
22 #include "tree.h"
23 #include "c-lex.h"
24 #include "c-tree.h"
25 #include "flags.h"
26 #include "obstack.h"
27 #include <stdio.h>
28 #include <ctype.h>
30 #ifndef WCHAR_TYPE_SIZE
31 #ifdef INT_TYPE_SIZE
32 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
33 #else
34 #define WCHAR_TYPE_SIZE BITS_PER_WORD
35 #endif
36 #endif
38 extern struct obstack permanent_obstack;
40 /* Nonzero means the expression being parsed will never be evaluated.
41 This is a count, since unevaluated expressions can nest. */
42 int skip_evaluation;
44 enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
45 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
46 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS};
48 static void declare_hidden_char_array PROTO((char *, char *));
49 static void add_attribute PROTO((enum attrs, char *,
50 int, int, int));
51 static void init_attributes PROTO((void));
52 static void record_international_format PROTO((tree, tree, int));
54 /* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
56 void
57 declare_function_name ()
59 char *name, *printable_name;
61 if (current_function_decl == NULL)
63 name = "";
64 printable_name = "top level";
66 else
68 /* Allow functions to be nameless (such as artificial ones). */
69 if (DECL_NAME (current_function_decl))
70 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
71 else
72 name = "";
73 printable_name = (*decl_printable_name) (current_function_decl, 2);
76 declare_hidden_char_array ("__FUNCTION__", name);
77 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
80 static void
81 declare_hidden_char_array (name, value)
82 char *name, *value;
84 tree decl, type, init;
85 int vlen;
87 /* If the default size of char arrays isn't big enough for the name,
88 or if we want to give warnings for large objects, make a bigger one. */
89 vlen = strlen (value) + 1;
90 type = char_array_type_node;
91 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TREE_TYPE (type))) < vlen
92 || warn_larger_than)
93 type = build_array_type (char_type_node,
94 build_index_type (build_int_2 (vlen, 0)));
95 push_obstacks_nochange ();
96 decl = build_decl (VAR_DECL, get_identifier (name), type);
97 TREE_STATIC (decl) = 1;
98 TREE_READONLY (decl) = 1;
99 TREE_ASM_WRITTEN (decl) = 1;
100 DECL_SOURCE_LINE (decl) = 0;
101 DECL_ARTIFICIAL (decl) = 1;
102 DECL_IN_SYSTEM_HEADER (decl) = 1;
103 DECL_IGNORED_P (decl) = 1;
104 init = build_string (vlen, value);
105 TREE_TYPE (init) = type;
106 DECL_INITIAL (decl) = init;
107 finish_decl (pushdecl (decl), init, NULL_TREE);
110 /* Given a chain of STRING_CST nodes,
111 concatenate them into one STRING_CST
112 and give it a suitable array-of-chars data type. */
114 tree
115 combine_strings (strings)
116 tree strings;
118 register tree value, t;
119 register int length = 1;
120 int wide_length = 0;
121 int wide_flag = 0;
122 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
123 int nchars;
125 if (TREE_CHAIN (strings))
127 /* More than one in the chain, so concatenate. */
128 register char *p, *q;
130 /* Don't include the \0 at the end of each substring,
131 except for the last one.
132 Count wide strings and ordinary strings separately. */
133 for (t = strings; t; t = TREE_CHAIN (t))
135 if (TREE_TYPE (t) == wchar_array_type_node)
137 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
138 wide_flag = 1;
140 else
141 length += (TREE_STRING_LENGTH (t) - 1);
144 /* If anything is wide, the non-wides will be converted,
145 which makes them take more space. */
146 if (wide_flag)
147 length = length * wchar_bytes + wide_length;
149 p = savealloc (length);
151 /* Copy the individual strings into the new combined string.
152 If the combined string is wide, convert the chars to ints
153 for any individual strings that are not wide. */
155 q = p;
156 for (t = strings; t; t = TREE_CHAIN (t))
158 int len = (TREE_STRING_LENGTH (t)
159 - ((TREE_TYPE (t) == wchar_array_type_node)
160 ? wchar_bytes : 1));
161 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
163 bcopy (TREE_STRING_POINTER (t), q, len);
164 q += len;
166 else
168 int i;
169 for (i = 0; i < len; i++)
171 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
172 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
173 else
174 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
176 q += len * wchar_bytes;
179 if (wide_flag)
181 int i;
182 for (i = 0; i < wchar_bytes; i++)
183 *q++ = 0;
185 else
186 *q = 0;
188 value = make_node (STRING_CST);
189 TREE_STRING_POINTER (value) = p;
190 TREE_STRING_LENGTH (value) = length;
191 TREE_CONSTANT (value) = 1;
193 else
195 value = strings;
196 length = TREE_STRING_LENGTH (value);
197 if (TREE_TYPE (value) == wchar_array_type_node)
198 wide_flag = 1;
201 /* Compute the number of elements, for the array type. */
202 nchars = wide_flag ? length / wchar_bytes : length;
204 /* Create the array type for the string constant.
205 -Wwrite-strings says make the string constant an array of const char
206 so that copying it to a non-const pointer will get a warning. */
207 if (warn_write_strings
208 && (! flag_traditional && ! flag_writable_strings))
210 tree elements
211 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
212 1, 0);
213 TREE_TYPE (value)
214 = build_array_type (elements,
215 build_index_type (build_int_2 (nchars - 1, 0)));
217 else
218 TREE_TYPE (value)
219 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
220 build_index_type (build_int_2 (nchars - 1, 0)));
221 TREE_CONSTANT (value) = 1;
222 TREE_STATIC (value) = 1;
223 return value;
226 /* To speed up processing of attributes, we maintain an array of
227 IDENTIFIER_NODES and the corresponding attribute types. */
229 /* Array to hold attribute information. */
231 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
233 static int attrtab_idx = 0;
235 /* Add an entry to the attribute table above. */
237 static void
238 add_attribute (id, string, min_len, max_len, decl_req)
239 enum attrs id;
240 char *string;
241 int min_len, max_len;
242 int decl_req;
244 char buf[100];
246 attrtab[attrtab_idx].id = id;
247 attrtab[attrtab_idx].name = get_identifier (string);
248 attrtab[attrtab_idx].min = min_len;
249 attrtab[attrtab_idx].max = max_len;
250 attrtab[attrtab_idx++].decl_req = decl_req;
252 sprintf (buf, "__%s__", string);
254 attrtab[attrtab_idx].id = id;
255 attrtab[attrtab_idx].name = get_identifier (buf);
256 attrtab[attrtab_idx].min = min_len;
257 attrtab[attrtab_idx].max = max_len;
258 attrtab[attrtab_idx++].decl_req = decl_req;
261 /* Initialize attribute table. */
263 static void
264 init_attributes ()
266 add_attribute (A_PACKED, "packed", 0, 0, 0);
267 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
268 add_attribute (A_COMMON, "common", 0, 0, 1);
269 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
270 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
271 add_attribute (A_UNUSED, "unused", 0, 0, 1);
272 add_attribute (A_CONST, "const", 0, 0, 1);
273 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
274 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
275 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
276 add_attribute (A_MODE, "mode", 1, 1, 1);
277 add_attribute (A_SECTION, "section", 1, 1, 1);
278 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
279 add_attribute (A_FORMAT, "format", 3, 3, 1);
280 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
281 add_attribute (A_WEAK, "weak", 0, 0, 1);
282 add_attribute (A_ALIAS, "alias", 1, 1, 1);
285 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
286 and install them in NODE, which is either a DECL (including a TYPE_DECL)
287 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
288 and declaration modifiers but before the declaration proper. */
290 void
291 decl_attributes (node, attributes, prefix_attributes)
292 tree node, attributes, prefix_attributes;
294 tree decl = 0, type;
295 int is_type;
296 tree a;
298 if (attrtab_idx == 0)
299 init_attributes ();
301 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
303 decl = node;
304 type = TREE_TYPE (decl);
305 is_type = TREE_CODE (node) == TYPE_DECL;
307 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't')
308 type = node, is_type = 1;
310 attributes = chainon (prefix_attributes, attributes);
312 for (a = attributes; a; a = TREE_CHAIN (a))
314 tree name = TREE_PURPOSE (a);
315 tree args = TREE_VALUE (a);
316 int i;
317 enum attrs id;
319 for (i = 0; i < attrtab_idx; i++)
320 if (attrtab[i].name == name)
321 break;
323 if (i == attrtab_idx)
325 if (! valid_machine_attribute (name, args, decl, type))
326 warning ("`%s' attribute directive ignored",
327 IDENTIFIER_POINTER (name));
328 else if (decl != 0)
329 type = TREE_TYPE (decl);
330 continue;
332 else if (attrtab[i].decl_req && decl == 0)
334 warning ("`%s' attribute does not apply to types",
335 IDENTIFIER_POINTER (name));
336 continue;
338 else if (list_length (args) < attrtab[i].min
339 || list_length (args) > attrtab[i].max)
341 error ("wrong number of arguments specified for `%s' attribute",
342 IDENTIFIER_POINTER (name));
343 continue;
346 id = attrtab[i].id;
347 switch (id)
349 case A_PACKED:
350 if (is_type)
351 TYPE_PACKED (type) = 1;
352 else if (TREE_CODE (decl) == FIELD_DECL)
353 DECL_PACKED (decl) = 1;
354 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
355 used for DECL_REGISTER. It wouldn't mean anything anyway. */
356 else
357 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
358 break;
360 case A_NOCOMMON:
361 if (TREE_CODE (decl) == VAR_DECL)
362 DECL_COMMON (decl) = 0;
363 else
364 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
365 break;
367 case A_COMMON:
368 if (TREE_CODE (decl) == VAR_DECL)
369 DECL_COMMON (decl) = 1;
370 else
371 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
372 break;
374 case A_NORETURN:
375 if (TREE_CODE (decl) == FUNCTION_DECL)
376 TREE_THIS_VOLATILE (decl) = 1;
377 else if (TREE_CODE (type) == POINTER_TYPE
378 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
379 TREE_TYPE (decl) = type
380 = build_pointer_type
381 (build_type_variant (TREE_TYPE (type),
382 TREE_READONLY (TREE_TYPE (type)), 1));
383 else
384 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
385 break;
387 case A_UNUSED:
388 if (TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == VAR_DECL
389 || TREE_CODE (decl) == FUNCTION_DECL)
390 TREE_USED (decl) = 1;
391 else
392 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
393 break;
395 case A_CONST:
396 if (TREE_CODE (decl) == FUNCTION_DECL)
397 TREE_READONLY (decl) = 1;
398 else if (TREE_CODE (type) == POINTER_TYPE
399 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
400 TREE_TYPE (decl) = type
401 = build_pointer_type
402 (build_type_variant (TREE_TYPE (type), 1,
403 TREE_THIS_VOLATILE (TREE_TYPE (type))));
404 else
405 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
406 break;
408 case A_T_UNION:
409 if (is_type
410 && TREE_CODE (type) == UNION_TYPE
411 && (decl == 0
412 || (TYPE_FIELDS (type) != 0
413 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
414 TYPE_TRANSPARENT_UNION (type) = 1;
415 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
416 && TREE_CODE (type) == UNION_TYPE
417 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
418 DECL_TRANSPARENT_UNION (decl) = 1;
419 else
420 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
421 break;
423 case A_CONSTRUCTOR:
424 if (TREE_CODE (decl) == FUNCTION_DECL
425 && TREE_CODE (type) == FUNCTION_TYPE
426 && decl_function_context (decl) == 0)
428 DECL_STATIC_CONSTRUCTOR (decl) = 1;
429 TREE_USED (decl) = 1;
431 else
432 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
433 break;
435 case A_DESTRUCTOR:
436 if (TREE_CODE (decl) == FUNCTION_DECL
437 && TREE_CODE (type) == FUNCTION_TYPE
438 && decl_function_context (decl) == 0)
440 DECL_STATIC_DESTRUCTOR (decl) = 1;
441 TREE_USED (decl) = 1;
443 else
444 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
445 break;
447 case A_MODE:
448 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
449 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
450 else
452 int j;
453 char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
454 int len = strlen (p);
455 enum machine_mode mode = VOIDmode;
456 tree typefm;
458 if (len > 4 && p[0] == '_' && p[1] == '_'
459 && p[len - 1] == '_' && p[len - 2] == '_')
461 char *newp = (char *) alloca (len - 1);
463 strcpy (newp, &p[2]);
464 newp[len - 4] = '\0';
465 p = newp;
468 /* Give this decl a type with the specified mode.
469 First check for the special modes. */
470 if (! strcmp (p, "byte"))
471 mode = byte_mode;
472 else if (!strcmp (p, "word"))
473 mode = word_mode;
474 else if (! strcmp (p, "pointer"))
475 mode = ptr_mode;
476 else
477 for (j = 0; j < NUM_MACHINE_MODES; j++)
478 if (!strcmp (p, GET_MODE_NAME (j)))
479 mode = (enum machine_mode) j;
481 if (mode == VOIDmode)
482 error ("unknown machine mode `%s'", p);
483 else if (0 == (typefm = type_for_mode (mode,
484 TREE_UNSIGNED (type))))
485 error ("no data type for mode `%s'", p);
486 else
488 TREE_TYPE (decl) = type = typefm;
489 DECL_SIZE (decl) = 0;
490 layout_decl (decl, 0);
493 break;
495 case A_SECTION:
496 #ifdef ASM_OUTPUT_SECTION_NAME
497 if ((TREE_CODE (decl) == FUNCTION_DECL
498 || TREE_CODE (decl) == VAR_DECL)
499 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
501 if (TREE_CODE (decl) == VAR_DECL
502 && current_function_decl != NULL_TREE
503 && ! TREE_STATIC (decl))
504 error_with_decl (decl,
505 "section attribute cannot be specified for local variables");
506 /* The decl may have already been given a section attribute from
507 a previous declaration. Ensure they match. */
508 else if (DECL_SECTION_NAME (decl) != NULL_TREE
509 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
510 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
511 error_with_decl (node,
512 "section of `%s' conflicts with previous declaration");
513 else
514 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
516 else
517 error_with_decl (node,
518 "section attribute not allowed for `%s'");
519 #else
520 error_with_decl (node,
521 "section attributes are not supported for this target");
522 #endif
523 break;
525 case A_ALIGNED:
527 tree align_expr
528 = (args ? TREE_VALUE (args)
529 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
530 int align;
532 /* Strip any NOPs of any kind. */
533 while (TREE_CODE (align_expr) == NOP_EXPR
534 || TREE_CODE (align_expr) == CONVERT_EXPR
535 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
536 align_expr = TREE_OPERAND (align_expr, 0);
538 if (TREE_CODE (align_expr) != INTEGER_CST)
540 error ("requested alignment is not a constant");
541 continue;
544 align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
546 if (exact_log2 (align) == -1)
547 error ("requested alignment is not a power of 2");
548 else if (is_type)
549 TYPE_ALIGN (type) = align;
550 else if (TREE_CODE (decl) != VAR_DECL
551 && TREE_CODE (decl) != FIELD_DECL)
552 error_with_decl (decl,
553 "alignment may not be specified for `%s'");
554 else
555 DECL_ALIGN (decl) = align;
557 break;
559 case A_FORMAT:
561 tree format_type = TREE_VALUE (args);
562 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
563 tree first_arg_num_expr
564 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
565 int format_num;
566 int first_arg_num;
567 int is_scan;
568 tree argument;
569 int arg_num;
571 if (TREE_CODE (decl) != FUNCTION_DECL)
573 error_with_decl (decl,
574 "argument format specified for non-function `%s'");
575 continue;
578 if (TREE_CODE (format_type) == IDENTIFIER_NODE
579 && (!strcmp (IDENTIFIER_POINTER (format_type), "printf")
580 || !strcmp (IDENTIFIER_POINTER (format_type),
581 "__printf__")))
582 is_scan = 0;
583 else if (TREE_CODE (format_type) == IDENTIFIER_NODE
584 && (!strcmp (IDENTIFIER_POINTER (format_type), "scanf")
585 || !strcmp (IDENTIFIER_POINTER (format_type),
586 "__scanf__")))
587 is_scan = 1;
588 else if (TREE_CODE (format_type) == IDENTIFIER_NODE)
590 error ("`%s' is an unrecognized format function type",
591 IDENTIFIER_POINTER (format_type));
592 continue;
594 else
596 error ("unrecognized format specifier");
597 continue;
600 /* Strip any conversions from the string index and first arg number
601 and verify they are constants. */
602 while (TREE_CODE (format_num_expr) == NOP_EXPR
603 || TREE_CODE (format_num_expr) == CONVERT_EXPR
604 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
605 format_num_expr = TREE_OPERAND (format_num_expr, 0);
607 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
608 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
609 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
610 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
612 if (TREE_CODE (format_num_expr) != INTEGER_CST
613 || TREE_CODE (first_arg_num_expr) != INTEGER_CST)
615 error ("format string has non-constant operand number");
616 continue;
619 format_num = TREE_INT_CST_LOW (format_num_expr);
620 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
621 if (first_arg_num != 0 && first_arg_num <= format_num)
623 error ("format string arg follows the args to be formatted");
624 continue;
627 /* If a parameter list is specified, verify that the format_num
628 argument is actually a string, in case the format attribute
629 is in error. */
630 argument = TYPE_ARG_TYPES (type);
631 if (argument)
633 for (arg_num = 1; ; ++arg_num)
635 if (argument == 0 || arg_num == format_num)
636 break;
637 argument = TREE_CHAIN (argument);
639 if (! argument
640 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
641 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
642 != char_type_node))
644 error ("format string arg not a string type");
645 continue;
647 if (first_arg_num != 0)
649 /* Verify that first_arg_num points to the last arg,
650 the ... */
651 while (argument)
652 arg_num++, argument = TREE_CHAIN (argument);
653 if (arg_num != first_arg_num)
655 error ("args to be formatted is not ...");
656 continue;
661 record_function_format (DECL_NAME (decl),
662 DECL_ASSEMBLER_NAME (decl),
663 is_scan, format_num, first_arg_num);
664 break;
667 case A_FORMAT_ARG:
669 tree format_num_expr = TREE_VALUE (args);
670 int format_num, arg_num;
671 tree argument;
673 if (TREE_CODE (decl) != FUNCTION_DECL)
675 error_with_decl (decl,
676 "argument format specified for non-function `%s'");
677 continue;
680 /* Strip any conversions from the first arg number and verify it
681 is a constant. */
682 while (TREE_CODE (format_num_expr) == NOP_EXPR
683 || TREE_CODE (format_num_expr) == CONVERT_EXPR
684 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
685 format_num_expr = TREE_OPERAND (format_num_expr, 0);
687 if (TREE_CODE (format_num_expr) != INTEGER_CST)
689 error ("format string has non-constant operand number");
690 continue;
693 format_num = TREE_INT_CST_LOW (format_num_expr);
695 /* If a parameter list is specified, verify that the format_num
696 argument is actually a string, in case the format attribute
697 is in error. */
698 argument = TYPE_ARG_TYPES (type);
699 if (argument)
701 for (arg_num = 1; ; ++arg_num)
703 if (argument == 0 || arg_num == format_num)
704 break;
705 argument = TREE_CHAIN (argument);
707 if (! argument
708 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
709 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
710 != char_type_node))
712 error ("format string arg not a string type");
713 continue;
717 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
718 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
719 != char_type_node))
721 error ("function does not return string type");
722 continue;
725 record_international_format (DECL_NAME (decl),
726 DECL_ASSEMBLER_NAME (decl),
727 format_num);
728 break;
731 case A_WEAK:
732 declare_weak (decl);
733 break;
735 case A_ALIAS:
736 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
737 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
738 error_with_decl (decl,
739 "`%s' defined both normally and as an alias");
740 else if (decl_function_context (decl) == 0)
742 tree id = get_identifier (TREE_STRING_POINTER
743 (TREE_VALUE (args)));
744 if (TREE_CODE (decl) == FUNCTION_DECL)
745 DECL_INITIAL (decl) = error_mark_node;
746 else
747 DECL_EXTERNAL (decl) = 0;
748 assemble_alias (decl, id);
750 else
751 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
752 break;
757 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
758 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
760 The head of the declspec list is stored in DECLSPECS.
761 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
763 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
764 the list elements. We drop the containing TREE_LIST nodes and link the
765 resulting attributes together the way decl_attributes expects them. */
767 void
768 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
769 tree specs_attrs;
770 tree *declspecs, *prefix_attributes;
772 tree t, s, a, next, specs, attrs;
774 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
775 if (specs_attrs != NULL_TREE
776 && TREE_CODE (specs_attrs) != TREE_LIST)
778 *declspecs = specs_attrs;
779 *prefix_attributes = NULL_TREE;
780 return;
783 /* Remember to keep the lists in the same order, element-wise. */
785 specs = s = NULL_TREE;
786 attrs = a = NULL_TREE;
787 for (t = specs_attrs; t; t = next)
789 next = TREE_CHAIN (t);
790 /* Declspecs have a non-NULL TREE_VALUE. */
791 if (TREE_VALUE (t) != NULL_TREE)
793 if (specs == NULL_TREE)
794 specs = s = t;
795 else
797 TREE_CHAIN (s) = t;
798 s = t;
801 else
803 if (attrs == NULL_TREE)
804 attrs = a = TREE_PURPOSE (t);
805 else
807 TREE_CHAIN (a) = TREE_PURPOSE (t);
808 a = TREE_PURPOSE (t);
810 /* More attrs can be linked here, move A to the end. */
811 while (TREE_CHAIN (a) != NULL_TREE)
812 a = TREE_CHAIN (a);
816 /* Terminate the lists. */
817 if (s != NULL_TREE)
818 TREE_CHAIN (s) = NULL_TREE;
819 if (a != NULL_TREE)
820 TREE_CHAIN (a) = NULL_TREE;
822 /* All done. */
823 *declspecs = specs;
824 *prefix_attributes = attrs;
827 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
828 a parameter list. */
830 #define T_I &integer_type_node
831 #define T_L &long_integer_type_node
832 #define T_LL &long_long_integer_type_node
833 #define T_S &short_integer_type_node
834 #define T_UI &unsigned_type_node
835 #define T_UL &long_unsigned_type_node
836 #define T_ULL &long_long_unsigned_type_node
837 #define T_US &short_unsigned_type_node
838 #define T_F &float_type_node
839 #define T_D &double_type_node
840 #define T_LD &long_double_type_node
841 #define T_C &char_type_node
842 #define T_V &void_type_node
843 #define T_W &wchar_type_node
844 #define T_ST &sizetype
846 typedef struct {
847 char *format_chars;
848 int pointer_count;
849 /* Type of argument if no length modifier is used. */
850 tree *nolen;
851 /* Type of argument if length modifier for shortening is used.
852 If NULL, then this modifier is not allowed. */
853 tree *hlen;
854 /* Type of argument if length modifier `l' is used.
855 If NULL, then this modifier is not allowed. */
856 tree *llen;
857 /* Type of argument if length modifier `q' or `ll' is used.
858 If NULL, then this modifier is not allowed. */
859 tree *qlen;
860 /* Type of argument if length modifier `L' is used.
861 If NULL, then this modifier is not allowed. */
862 tree *bigllen;
863 /* List of other modifier characters allowed with these options. */
864 char *flag_chars;
865 } format_char_info;
867 static format_char_info print_char_table[] = {
868 { "di", 0, T_I, T_I, T_L, T_LL, T_LL, "-wp0 +" },
869 { "oxX", 0, T_UI, T_UI, T_UL, T_ULL, T_ULL, "-wp0#" },
870 { "u", 0, T_UI, T_UI, T_UL, T_ULL, T_ULL, "-wp0" },
871 /* Two GNU extensions. */
872 { "Z", 0, T_ST, NULL, NULL, NULL, NULL, "-wp0" },
873 { "m", 0, T_V, NULL, NULL, NULL, NULL, "-wp" },
874 { "feEgG", 0, T_D, NULL, NULL, NULL, T_LD, "-wp0 +#" },
875 { "c", 0, T_I, NULL, T_W, NULL, NULL, "-w" },
876 { "C", 0, T_W, NULL, NULL, NULL, NULL, "-w" },
877 { "s", 1, T_C, NULL, T_W, NULL, NULL, "-wp" },
878 { "S", 1, T_W, NULL, NULL, NULL, NULL, "-wp" },
879 { "p", 1, T_V, NULL, NULL, NULL, NULL, "-w" },
880 { "n", 1, T_I, T_S, T_L, T_LL, NULL, "" },
881 { NULL }
884 static format_char_info scan_char_table[] = {
885 { "di", 1, T_I, T_S, T_L, T_LL, T_LL, "*" },
886 { "ouxX", 1, T_UI, T_US, T_UL, T_ULL, T_ULL, "*" },
887 { "efgEG", 1, T_F, NULL, T_D, NULL, T_LD, "*" },
888 { "sc", 1, T_C, NULL, T_W, NULL, NULL, "*a" },
889 { "[", 1, T_C, NULL, NULL, NULL, NULL, "*a" },
890 { "C", 1, T_W, NULL, NULL, NULL, NULL, "*" },
891 { "S", 1, T_W, NULL, NULL, NULL, NULL, "*" },
892 { "p", 2, T_V, NULL, NULL, NULL, NULL, "*" },
893 { "n", 1, T_I, T_S, T_L, T_LL, NULL, "" },
894 { NULL }
897 typedef struct function_format_info
899 struct function_format_info *next; /* next structure on the list */
900 tree name; /* identifier such as "printf" */
901 tree assembler_name; /* optional mangled identifier (for C++) */
902 int is_scan; /* TRUE if *scanf */
903 int format_num; /* number of format argument */
904 int first_arg_num; /* number of first arg (zero for varargs) */
905 } function_format_info;
907 static function_format_info *function_format_list = NULL;
909 typedef struct international_format_info
911 struct international_format_info *next; /* next structure on the list */
912 tree name; /* identifier such as "gettext" */
913 tree assembler_name; /* optional mangled identifier (for C++) */
914 int format_num; /* number of format argument */
915 } international_format_info;
917 static international_format_info *international_format_list = NULL;
919 static void check_format_info PROTO((function_format_info *, tree));
921 /* Initialize the table of functions to perform format checking on.
922 The ANSI functions are always checked (whether <stdio.h> is
923 included or not), since it is common to call printf without
924 including <stdio.h>. There shouldn't be a problem with this,
925 since ANSI reserves these function names whether you include the
926 header file or not. In any case, the checking is harmless.
928 Also initialize the name of function that modify the format string for
929 internationalization purposes. */
931 void
932 init_function_format_info ()
934 record_function_format (get_identifier ("printf"), NULL_TREE, 0, 1, 2);
935 record_function_format (get_identifier ("fprintf"), NULL_TREE, 0, 2, 3);
936 record_function_format (get_identifier ("sprintf"), NULL_TREE, 0, 2, 3);
937 record_function_format (get_identifier ("scanf"), NULL_TREE, 1, 1, 2);
938 record_function_format (get_identifier ("fscanf"), NULL_TREE, 1, 2, 3);
939 record_function_format (get_identifier ("sscanf"), NULL_TREE, 1, 2, 3);
940 record_function_format (get_identifier ("vprintf"), NULL_TREE, 0, 1, 0);
941 record_function_format (get_identifier ("vfprintf"), NULL_TREE, 0, 2, 0);
942 record_function_format (get_identifier ("vsprintf"), NULL_TREE, 0, 2, 0);
944 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
945 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
946 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
949 /* Record information for argument format checking. FUNCTION_IDENT is
950 the identifier node for the name of the function to check (its decl
951 need not exist yet). IS_SCAN is true for scanf-type format checking;
952 false indicates printf-style format checking. FORMAT_NUM is the number
953 of the argument which is the format control string (starting from 1).
954 FIRST_ARG_NUM is the number of the first actual argument to check
955 against the format string, or zero if no checking is not be done
956 (e.g. for varargs such as vfprintf). */
958 void
959 record_function_format (name, assembler_name, is_scan,
960 format_num, first_arg_num)
961 tree name;
962 tree assembler_name;
963 int is_scan;
964 int format_num;
965 int first_arg_num;
967 function_format_info *info;
969 /* Re-use existing structure if it's there. */
971 for (info = function_format_list; info; info = info->next)
973 if (info->name == name && info->assembler_name == assembler_name)
974 break;
976 if (! info)
978 info = (function_format_info *) xmalloc (sizeof (function_format_info));
979 info->next = function_format_list;
980 function_format_list = info;
982 info->name = name;
983 info->assembler_name = assembler_name;
986 info->is_scan = is_scan;
987 info->format_num = format_num;
988 info->first_arg_num = first_arg_num;
991 /* Record information for the names of function that modify the format
992 argument to format functions. FUNCTION_IDENT is the identifier node for
993 the name of the function (its decl need not exist yet) and FORMAT_NUM is
994 the number of the argument which is the format control string (starting
995 from 1). */
997 static void
998 record_international_format (name, assembler_name, format_num)
999 tree name;
1000 tree assembler_name;
1001 int format_num;
1003 international_format_info *info;
1005 /* Re-use existing structure if it's there. */
1007 for (info = international_format_list; info; info = info->next)
1009 if (info->name == name && info->assembler_name == assembler_name)
1010 break;
1013 if (! info)
1015 info
1016 = (international_format_info *)
1017 xmalloc (sizeof (international_format_info));
1018 info->next = international_format_list;
1019 international_format_list = info;
1021 info->name = name;
1022 info->assembler_name = assembler_name;
1025 info->format_num = format_num;
1028 static char tfaff[] = "too few arguments for format";
1030 /* Check the argument list of a call to printf, scanf, etc.
1031 NAME is the function identifier.
1032 ASSEMBLER_NAME is the function's assembler identifier.
1033 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1034 PARAMS is the list of argument values. */
1036 void
1037 check_function_format (name, assembler_name, params)
1038 tree name;
1039 tree assembler_name;
1040 tree params;
1042 function_format_info *info;
1044 /* See if this function is a format function. */
1045 for (info = function_format_list; info; info = info->next)
1047 if (info->assembler_name
1048 ? (info->assembler_name == assembler_name)
1049 : (info->name == name))
1051 /* Yup; check it. */
1052 check_format_info (info, params);
1053 break;
1058 /* Check the argument list of a call to printf, scanf, etc.
1059 INFO points to the function_format_info structure.
1060 PARAMS is the list of argument values. */
1062 static void
1063 check_format_info (info, params)
1064 function_format_info *info;
1065 tree params;
1067 int i;
1068 int arg_num;
1069 int suppressed, wide, precise;
1070 int length_char;
1071 int format_char;
1072 int format_length;
1073 tree format_tree;
1074 tree cur_param;
1075 tree cur_type;
1076 tree wanted_type;
1077 tree first_fillin_param;
1078 char *format_chars;
1079 format_char_info *fci;
1080 static char message[132];
1081 char flag_chars[8];
1082 int has_operand_number = 0;
1084 /* Skip to format argument. If the argument isn't available, there's
1085 no work for us to do; prototype checking will catch the problem. */
1086 for (arg_num = 1; ; ++arg_num)
1088 if (params == 0)
1089 return;
1090 if (arg_num == info->format_num)
1091 break;
1092 params = TREE_CHAIN (params);
1094 format_tree = TREE_VALUE (params);
1095 params = TREE_CHAIN (params);
1096 if (format_tree == 0)
1097 return;
1099 /* We can only check the format if it's a string constant. */
1100 while (TREE_CODE (format_tree) == NOP_EXPR)
1101 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
1103 if (TREE_CODE (format_tree) == CALL_EXPR
1104 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1105 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1106 == FUNCTION_DECL))
1108 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1110 /* See if this is a call to a known internationalization function
1111 that modifies the format arg. */
1112 international_format_info *info;
1114 for (info = international_format_list; info; info = info->next)
1115 if (info->assembler_name
1116 ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
1117 : (info->name == DECL_NAME (function)))
1119 tree inner_args;
1120 int i;
1122 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
1123 inner_args != 0;
1124 inner_args = TREE_CHAIN (inner_args), i++)
1125 if (i == info->format_num)
1127 format_tree = TREE_VALUE (inner_args);
1129 while (TREE_CODE (format_tree) == NOP_EXPR)
1130 format_tree = TREE_OPERAND (format_tree, 0);
1135 if (integer_zerop (format_tree))
1137 warning ("null format string");
1138 return;
1140 if (TREE_CODE (format_tree) != ADDR_EXPR)
1141 return;
1142 format_tree = TREE_OPERAND (format_tree, 0);
1143 if (TREE_CODE (format_tree) != STRING_CST)
1144 return;
1145 format_chars = TREE_STRING_POINTER (format_tree);
1146 format_length = TREE_STRING_LENGTH (format_tree);
1147 if (format_length <= 1)
1148 warning ("zero-length format string");
1149 if (format_chars[--format_length] != 0)
1151 warning ("unterminated format string");
1152 return;
1154 /* Skip to first argument to check. */
1155 while (arg_num + 1 < info->first_arg_num)
1157 if (params == 0)
1158 return;
1159 params = TREE_CHAIN (params);
1160 ++arg_num;
1163 first_fillin_param = params;
1164 while (1)
1166 int aflag;
1167 if (*format_chars == 0)
1169 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
1170 warning ("embedded `\\0' in format");
1171 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
1172 warning ("too many arguments for format");
1173 return;
1175 if (*format_chars++ != '%')
1176 continue;
1177 if (*format_chars == 0)
1179 warning ("spurious trailing `%%' in format");
1180 continue;
1182 if (*format_chars == '%')
1184 ++format_chars;
1185 continue;
1187 flag_chars[0] = 0;
1188 suppressed = wide = precise = FALSE;
1189 if (info->is_scan)
1191 suppressed = *format_chars == '*';
1192 if (suppressed)
1193 ++format_chars;
1194 while (isdigit (*format_chars))
1195 ++format_chars;
1197 else
1199 /* See if we have a number followed by a dollar sign. If we do,
1200 it is an operand number, so set PARAMS to that operand. */
1201 if (*format_chars >= '0' && *format_chars <= '9')
1203 char *p = format_chars;
1205 while (*p >= '0' && *p++ <= '9')
1208 if (*p == '$')
1210 int opnum = atoi (format_chars);
1212 params = first_fillin_param;
1213 format_chars = p + 1;
1214 has_operand_number = 1;
1216 for (i = 1; i < opnum && params != 0; i++)
1217 params = TREE_CHAIN (params);
1219 if (opnum == 0 || params == 0)
1221 warning ("operand number out of range in format");
1222 return;
1227 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
1229 if (index (flag_chars, *format_chars) != 0)
1231 sprintf (message, "repeated `%c' flag in format",
1232 *format_chars);
1233 warning (message);
1235 i = strlen (flag_chars);
1236 flag_chars[i++] = *format_chars++;
1237 flag_chars[i] = 0;
1239 /* "If the space and + flags both appear,
1240 the space flag will be ignored." */
1241 if (index (flag_chars, ' ') != 0
1242 && index (flag_chars, '+') != 0)
1243 warning ("use of both ` ' and `+' flags in format");
1244 /* "If the 0 and - flags both appear,
1245 the 0 flag will be ignored." */
1246 if (index (flag_chars, '0') != 0
1247 && index (flag_chars, '-') != 0)
1248 warning ("use of both `0' and `-' flags in format");
1249 if (*format_chars == '*')
1251 wide = TRUE;
1252 /* "...a field width...may be indicated by an asterisk.
1253 In this case, an int argument supplies the field width..." */
1254 ++format_chars;
1255 if (params == 0)
1257 warning (tfaff);
1258 return;
1260 if (info->first_arg_num != 0)
1262 cur_param = TREE_VALUE (params);
1263 params = TREE_CHAIN (params);
1264 ++arg_num;
1265 /* size_t is generally not valid here.
1266 It will work on most machines, because size_t and int
1267 have the same mode. But might as well warn anyway,
1268 since it will fail on other machines. */
1269 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1270 != integer_type_node)
1272 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1273 != unsigned_type_node))
1275 sprintf (message,
1276 "field width is not type int (arg %d)",
1277 arg_num);
1278 warning (message);
1282 else
1284 while (isdigit (*format_chars))
1286 wide = TRUE;
1287 ++format_chars;
1290 if (*format_chars == '.')
1292 precise = TRUE;
1293 ++format_chars;
1294 if (*format_chars != '*' && !isdigit (*format_chars))
1295 warning ("`.' not followed by `*' or digit in format");
1296 /* "...a...precision...may be indicated by an asterisk.
1297 In this case, an int argument supplies the...precision." */
1298 if (*format_chars == '*')
1300 if (info->first_arg_num != 0)
1302 ++format_chars;
1303 if (params == 0)
1305 warning (tfaff);
1306 return;
1308 cur_param = TREE_VALUE (params);
1309 params = TREE_CHAIN (params);
1310 ++arg_num;
1311 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1312 != integer_type_node)
1314 sprintf (message,
1315 "field width is not type int (arg %d)",
1316 arg_num);
1317 warning (message);
1321 else
1323 while (isdigit (*format_chars))
1324 ++format_chars;
1328 if (*format_chars == 'h' || *format_chars == 'l')
1329 length_char = *format_chars++;
1330 else if (*format_chars == 'q' || *format_chars == 'L')
1332 length_char = *format_chars++;
1333 if (pedantic)
1334 pedwarn ("ANSI C does not support the `%c' length modifier",
1335 length_char);
1337 else
1338 length_char = 0;
1339 if (length_char == 'l' && *format_chars == 'l')
1341 length_char = 'q', format_chars++;
1342 if (pedantic)
1343 pedwarn ("ANSI C does not support the `ll' length modifier");
1345 aflag = 0;
1346 if (*format_chars == 'a')
1348 aflag = 1;
1349 format_chars++;
1351 if (suppressed && length_char != 0)
1353 sprintf (message,
1354 "use of `*' and `%c' together in format",
1355 length_char);
1356 warning (message);
1358 format_char = *format_chars;
1359 if (format_char == 0 || format_char == '%')
1361 warning ("conversion lacks type at end of format");
1362 continue;
1364 format_chars++;
1365 fci = info->is_scan ? scan_char_table : print_char_table;
1366 while (fci->format_chars != 0
1367 && index (fci->format_chars, format_char) == 0)
1368 ++fci;
1369 if (fci->format_chars == 0)
1371 if (format_char >= 040 && format_char < 0177)
1372 sprintf (message,
1373 "unknown conversion type character `%c' in format",
1374 format_char);
1375 else
1376 sprintf (message,
1377 "unknown conversion type character 0x%x in format",
1378 format_char);
1379 warning (message);
1380 continue;
1382 if (wide && index (fci->flag_chars, 'w') == 0)
1384 sprintf (message, "width used with `%c' format",
1385 format_char);
1386 warning (message);
1388 if (precise && index (fci->flag_chars, 'p') == 0)
1390 sprintf (message, "precision used with `%c' format",
1391 format_char);
1392 warning (message);
1394 if (aflag && index (fci->flag_chars, 'a') == 0)
1396 sprintf (message, "`a' flag used with `%c' format",
1397 format_char);
1398 warning (message);
1399 /* To simplify the following code. */
1400 aflag = 0;
1402 if (info->is_scan && format_char == '[')
1404 /* Skip over scan set, in case it happens to have '%' in it. */
1405 if (*format_chars == '^')
1406 ++format_chars;
1407 /* Find closing bracket; if one is hit immediately, then
1408 it's part of the scan set rather than a terminator. */
1409 if (*format_chars == ']')
1410 ++format_chars;
1411 while (*format_chars && *format_chars != ']')
1412 ++format_chars;
1413 if (*format_chars != ']')
1414 /* The end of the format string was reached. */
1415 warning ("no closing `]' for `%%[' format");
1417 if (suppressed)
1419 if (index (fci->flag_chars, '*') == 0)
1421 sprintf (message,
1422 "suppression of `%c' conversion in format",
1423 format_char);
1424 warning (message);
1426 continue;
1428 for (i = 0; flag_chars[i] != 0; ++i)
1430 if (index (fci->flag_chars, flag_chars[i]) == 0)
1432 sprintf (message, "flag `%c' used with type `%c'",
1433 flag_chars[i], format_char);
1434 warning (message);
1437 if (precise && index (flag_chars, '0') != 0
1438 && (format_char == 'd' || format_char == 'i'
1439 || format_char == 'o' || format_char == 'u'
1440 || format_char == 'x' || format_char == 'x'))
1442 sprintf (message,
1443 "`0' flag ignored with precision specifier and `%c' format",
1444 format_char);
1445 warning (message);
1447 switch (length_char)
1449 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
1450 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1451 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
1452 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1453 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
1455 if (wanted_type == 0)
1457 sprintf (message,
1458 "use of `%c' length character with `%c' type character",
1459 length_char, format_char);
1460 warning (message);
1464 ** XXX -- should kvetch about stuff such as
1465 ** {
1466 ** const int i;
1468 ** scanf ("%d", &i);
1469 ** }
1472 /* Finally. . .check type of argument against desired type! */
1473 if (info->first_arg_num == 0)
1474 continue;
1475 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1476 /* This specifier takes no argument. */
1477 continue;
1478 if (params == 0)
1480 warning (tfaff);
1481 return;
1483 cur_param = TREE_VALUE (params);
1484 params = TREE_CHAIN (params);
1485 ++arg_num;
1486 cur_type = TREE_TYPE (cur_param);
1488 /* Check the types of any additional pointer arguments
1489 that precede the "real" argument. */
1490 for (i = 0; i < fci->pointer_count + aflag; ++i)
1492 if (TREE_CODE (cur_type) == POINTER_TYPE)
1494 cur_type = TREE_TYPE (cur_type);
1495 continue;
1497 if (TREE_CODE (cur_type) != ERROR_MARK)
1499 sprintf (message,
1500 "format argument is not a %s (arg %d)",
1501 ((fci->pointer_count + aflag == 1)
1502 ? "pointer" : "pointer to a pointer"),
1503 arg_num);
1504 warning (message);
1506 break;
1509 /* Check the type of the "real" argument, if there's a type we want. */
1510 if (i == fci->pointer_count + aflag && wanted_type != 0
1511 && TREE_CODE (cur_type) != ERROR_MARK
1512 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1513 /* If we want `void *', allow any pointer type.
1514 (Anything else would already have got a warning.) */
1515 && ! (wanted_type == void_type_node
1516 && fci->pointer_count > 0)
1517 /* Don't warn about differences merely in signedness. */
1518 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
1519 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
1520 && (TREE_UNSIGNED (wanted_type)
1521 ? wanted_type == (cur_type = unsigned_type (cur_type))
1522 : wanted_type == (cur_type = signed_type (cur_type))))
1523 /* Likewise, "signed char", "unsigned char" and "char" are
1524 equivalent but the above test won't consider them equivalent. */
1525 && ! (wanted_type == char_type_node
1526 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
1527 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
1529 register char *this;
1530 register char *that;
1532 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
1533 that = 0;
1534 if (TREE_CODE (cur_type) != ERROR_MARK
1535 && TYPE_NAME (cur_type) != 0
1536 && TREE_CODE (cur_type) != INTEGER_TYPE
1537 && !(TREE_CODE (cur_type) == POINTER_TYPE
1538 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
1540 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
1541 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
1542 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1543 else
1544 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
1547 /* A nameless type can't possibly match what the format wants.
1548 So there will be a warning for it.
1549 Make up a string to describe vaguely what it is. */
1550 if (that == 0)
1552 if (TREE_CODE (cur_type) == POINTER_TYPE)
1553 that = "pointer";
1554 else
1555 that = "different type";
1558 /* Make the warning better in case of mismatch of int vs long. */
1559 if (TREE_CODE (cur_type) == INTEGER_TYPE
1560 && TREE_CODE (wanted_type) == INTEGER_TYPE
1561 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
1562 && TYPE_NAME (cur_type) != 0
1563 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
1564 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1566 if (strcmp (this, that) != 0)
1568 sprintf (message, "%s format, %s arg (arg %d)",
1569 this, that, arg_num);
1570 warning (message);
1576 /* Print a warning if a constant expression had overflow in folding.
1577 Invoke this function on every expression that the language
1578 requires to be a constant expression.
1579 Note the ANSI C standard says it is erroneous for a
1580 constant expression to overflow. */
1582 void
1583 constant_expression_warning (value)
1584 tree value;
1586 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1587 || TREE_CODE (value) == COMPLEX_CST)
1588 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1589 pedwarn ("overflow in constant expression");
1592 /* Print a warning if an expression had overflow in folding.
1593 Invoke this function on every expression that
1594 (1) appears in the source code, and
1595 (2) might be a constant expression that overflowed, and
1596 (3) is not already checked by convert_and_check;
1597 however, do not invoke this function on operands of explicit casts. */
1599 void
1600 overflow_warning (value)
1601 tree value;
1603 if ((TREE_CODE (value) == INTEGER_CST
1604 || (TREE_CODE (value) == COMPLEX_CST
1605 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1606 && TREE_OVERFLOW (value))
1608 TREE_OVERFLOW (value) = 0;
1609 if (skip_evaluation == 0)
1610 warning ("integer overflow in expression");
1612 else if ((TREE_CODE (value) == REAL_CST
1613 || (TREE_CODE (value) == COMPLEX_CST
1614 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1615 && TREE_OVERFLOW (value))
1617 TREE_OVERFLOW (value) = 0;
1618 if (skip_evaluation == 0)
1619 warning ("floating point overflow in expression");
1623 /* Print a warning if a large constant is truncated to unsigned,
1624 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1625 Invoke this function on every expression that might be implicitly
1626 converted to an unsigned type. */
1628 void
1629 unsigned_conversion_warning (result, operand)
1630 tree result, operand;
1632 if (TREE_CODE (operand) == INTEGER_CST
1633 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
1634 && TREE_UNSIGNED (TREE_TYPE (result))
1635 && skip_evaluation == 0
1636 && !int_fits_type_p (operand, TREE_TYPE (result)))
1638 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
1639 /* This detects cases like converting -129 or 256 to unsigned char. */
1640 warning ("large integer implicitly truncated to unsigned type");
1641 else if (warn_conversion)
1642 warning ("negative integer implicitly converted to unsigned type");
1646 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1647 Invoke this function on every expression that is converted implicitly,
1648 i.e. because of language rules and not because of an explicit cast. */
1650 tree
1651 convert_and_check (type, expr)
1652 tree type, expr;
1654 tree t = convert (type, expr);
1655 if (TREE_CODE (t) == INTEGER_CST)
1657 if (TREE_OVERFLOW (t))
1659 TREE_OVERFLOW (t) = 0;
1661 /* Do not diagnose overflow in a constant expression merely
1662 because a conversion overflowed. */
1663 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
1665 /* No warning for converting 0x80000000 to int. */
1666 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
1667 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1668 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
1669 /* If EXPR fits in the unsigned version of TYPE,
1670 don't warn unless pedantic. */
1671 if ((pedantic
1672 || TREE_UNSIGNED (type)
1673 || ! int_fits_type_p (expr, unsigned_type (type)))
1674 && skip_evaluation == 0)
1675 warning ("overflow in implicit constant conversion");
1677 else
1678 unsigned_conversion_warning (t, expr);
1680 return t;
1683 void
1684 c_expand_expr_stmt (expr)
1685 tree expr;
1687 /* Do default conversion if safe and possibly important,
1688 in case within ({...}). */
1689 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
1690 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1691 expr = default_conversion (expr);
1693 if (TREE_TYPE (expr) != error_mark_node
1694 && TYPE_SIZE (TREE_TYPE (expr)) == 0
1695 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1696 error ("expression statement has incomplete type");
1698 expand_expr_stmt (expr);
1701 /* Validate the expression after `case' and apply default promotions. */
1703 tree
1704 check_case_value (value)
1705 tree value;
1707 if (value == NULL_TREE)
1708 return value;
1710 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1711 STRIP_TYPE_NOPS (value);
1713 if (TREE_CODE (value) != INTEGER_CST
1714 && value != error_mark_node)
1716 error ("case label does not reduce to an integer constant");
1717 value = error_mark_node;
1719 else
1720 /* Promote char or short to int. */
1721 value = default_conversion (value);
1723 constant_expression_warning (value);
1725 return value;
1728 /* Return an integer type with BITS bits of precision,
1729 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1731 tree
1732 type_for_size (bits, unsignedp)
1733 unsigned bits;
1734 int unsignedp;
1736 if (bits == TYPE_PRECISION (integer_type_node))
1737 return unsignedp ? unsigned_type_node : integer_type_node;
1739 if (bits == TYPE_PRECISION (signed_char_type_node))
1740 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1742 if (bits == TYPE_PRECISION (short_integer_type_node))
1743 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1745 if (bits == TYPE_PRECISION (long_integer_type_node))
1746 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1748 if (bits == TYPE_PRECISION (long_long_integer_type_node))
1749 return (unsignedp ? long_long_unsigned_type_node
1750 : long_long_integer_type_node);
1752 if (bits <= TYPE_PRECISION (intQI_type_node))
1753 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1755 if (bits <= TYPE_PRECISION (intHI_type_node))
1756 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1758 if (bits <= TYPE_PRECISION (intSI_type_node))
1759 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1761 if (bits <= TYPE_PRECISION (intDI_type_node))
1762 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1764 return 0;
1767 /* Return a data type that has machine mode MODE.
1768 If the mode is an integer,
1769 then UNSIGNEDP selects between signed and unsigned types. */
1771 tree
1772 type_for_mode (mode, unsignedp)
1773 enum machine_mode mode;
1774 int unsignedp;
1776 if (mode == TYPE_MODE (integer_type_node))
1777 return unsignedp ? unsigned_type_node : integer_type_node;
1779 if (mode == TYPE_MODE (signed_char_type_node))
1780 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1782 if (mode == TYPE_MODE (short_integer_type_node))
1783 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1785 if (mode == TYPE_MODE (long_integer_type_node))
1786 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1788 if (mode == TYPE_MODE (long_long_integer_type_node))
1789 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
1791 if (mode == TYPE_MODE (intQI_type_node))
1792 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1794 if (mode == TYPE_MODE (intHI_type_node))
1795 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1797 if (mode == TYPE_MODE (intSI_type_node))
1798 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1800 if (mode == TYPE_MODE (intDI_type_node))
1801 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1803 if (mode == TYPE_MODE (float_type_node))
1804 return float_type_node;
1806 if (mode == TYPE_MODE (double_type_node))
1807 return double_type_node;
1809 if (mode == TYPE_MODE (long_double_type_node))
1810 return long_double_type_node;
1812 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
1813 return build_pointer_type (char_type_node);
1815 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
1816 return build_pointer_type (integer_type_node);
1818 return 0;
1821 /* Return the minimum number of bits needed to represent VALUE in a
1822 signed or unsigned type, UNSIGNEDP says which. */
1825 min_precision (value, unsignedp)
1826 tree value;
1827 int unsignedp;
1829 int log;
1831 /* If the value is negative, compute its negative minus 1. The latter
1832 adjustment is because the absolute value of the largest negative value
1833 is one larger than the largest positive value. This is equivalent to
1834 a bit-wise negation, so use that operation instead. */
1836 if (tree_int_cst_sgn (value) < 0)
1837 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
1839 /* Return the number of bits needed, taking into account the fact
1840 that we need one more bit for a signed than unsigned type. */
1842 if (integer_zerop (value))
1843 log = 0;
1844 else if (TREE_INT_CST_HIGH (value) != 0)
1845 log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
1846 else
1847 log = floor_log2 (TREE_INT_CST_LOW (value));
1849 return log + 1 + ! unsignedp;
1852 /* Print an error message for invalid operands to arith operation CODE.
1853 NOP_EXPR is used as a special case (see truthvalue_conversion). */
1855 void
1856 binary_op_error (code)
1857 enum tree_code code;
1859 register char *opname = "unknown";
1861 switch (code)
1863 case NOP_EXPR:
1864 error ("invalid truth-value expression");
1865 return;
1867 case PLUS_EXPR:
1868 opname = "+"; break;
1869 case MINUS_EXPR:
1870 opname = "-"; break;
1871 case MULT_EXPR:
1872 opname = "*"; break;
1873 case MAX_EXPR:
1874 opname = "max"; break;
1875 case MIN_EXPR:
1876 opname = "min"; break;
1877 case EQ_EXPR:
1878 opname = "=="; break;
1879 case NE_EXPR:
1880 opname = "!="; break;
1881 case LE_EXPR:
1882 opname = "<="; break;
1883 case GE_EXPR:
1884 opname = ">="; break;
1885 case LT_EXPR:
1886 opname = "<"; break;
1887 case GT_EXPR:
1888 opname = ">"; break;
1889 case LSHIFT_EXPR:
1890 opname = "<<"; break;
1891 case RSHIFT_EXPR:
1892 opname = ">>"; break;
1893 case TRUNC_MOD_EXPR:
1894 case FLOOR_MOD_EXPR:
1895 opname = "%"; break;
1896 case TRUNC_DIV_EXPR:
1897 case FLOOR_DIV_EXPR:
1898 opname = "/"; break;
1899 case BIT_AND_EXPR:
1900 opname = "&"; break;
1901 case BIT_IOR_EXPR:
1902 opname = "|"; break;
1903 case TRUTH_ANDIF_EXPR:
1904 opname = "&&"; break;
1905 case TRUTH_ORIF_EXPR:
1906 opname = "||"; break;
1907 case BIT_XOR_EXPR:
1908 opname = "^"; break;
1909 case LROTATE_EXPR:
1910 case RROTATE_EXPR:
1911 opname = "rotate"; break;
1913 error ("invalid operands to binary %s", opname);
1916 /* Subroutine of build_binary_op, used for comparison operations.
1917 See if the operands have both been converted from subword integer types
1918 and, if so, perhaps change them both back to their original type.
1919 This function is also responsible for converting the two operands
1920 to the proper common type for comparison.
1922 The arguments of this function are all pointers to local variables
1923 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
1924 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
1926 If this function returns nonzero, it means that the comparison has
1927 a constant value. What this function returns is an expression for
1928 that value. */
1930 tree
1931 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
1932 tree *op0_ptr, *op1_ptr;
1933 tree *restype_ptr;
1934 enum tree_code *rescode_ptr;
1936 register tree type;
1937 tree op0 = *op0_ptr;
1938 tree op1 = *op1_ptr;
1939 int unsignedp0, unsignedp1;
1940 int real1, real2;
1941 tree primop0, primop1;
1942 enum tree_code code = *rescode_ptr;
1944 /* Throw away any conversions to wider types
1945 already present in the operands. */
1947 primop0 = get_narrower (op0, &unsignedp0);
1948 primop1 = get_narrower (op1, &unsignedp1);
1950 /* Handle the case that OP0 does not *contain* a conversion
1951 but it *requires* conversion to FINAL_TYPE. */
1953 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
1954 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
1955 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
1956 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
1958 /* If one of the operands must be floated, we cannot optimize. */
1959 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
1960 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
1962 /* If first arg is constant, swap the args (changing operation
1963 so value is preserved), for canonicalization. Don't do this if
1964 the second arg is 0. */
1966 if (TREE_CONSTANT (primop0)
1967 && ! integer_zerop (primop1) && ! real_zerop (primop1))
1969 register tree tem = primop0;
1970 register int temi = unsignedp0;
1971 primop0 = primop1;
1972 primop1 = tem;
1973 tem = op0;
1974 op0 = op1;
1975 op1 = tem;
1976 *op0_ptr = op0;
1977 *op1_ptr = op1;
1978 unsignedp0 = unsignedp1;
1979 unsignedp1 = temi;
1980 temi = real1;
1981 real1 = real2;
1982 real2 = temi;
1984 switch (code)
1986 case LT_EXPR:
1987 code = GT_EXPR;
1988 break;
1989 case GT_EXPR:
1990 code = LT_EXPR;
1991 break;
1992 case LE_EXPR:
1993 code = GE_EXPR;
1994 break;
1995 case GE_EXPR:
1996 code = LE_EXPR;
1997 break;
1999 *rescode_ptr = code;
2002 /* If comparing an integer against a constant more bits wide,
2003 maybe we can deduce a value of 1 or 0 independent of the data.
2004 Or else truncate the constant now
2005 rather than extend the variable at run time.
2007 This is only interesting if the constant is the wider arg.
2008 Also, it is not safe if the constant is unsigned and the
2009 variable arg is signed, since in this case the variable
2010 would be sign-extended and then regarded as unsigned.
2011 Our technique fails in this case because the lowest/highest
2012 possible unsigned results don't follow naturally from the
2013 lowest/highest possible values of the variable operand.
2014 For just EQ_EXPR and NE_EXPR there is another technique that
2015 could be used: see if the constant can be faithfully represented
2016 in the other operand's type, by truncating it and reextending it
2017 and see if that preserves the constant's value. */
2019 if (!real1 && !real2
2020 && TREE_CODE (primop1) == INTEGER_CST
2021 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2023 int min_gt, max_gt, min_lt, max_lt;
2024 tree maxval, minval;
2025 /* 1 if comparison is nominally unsigned. */
2026 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2027 tree val;
2029 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2031 maxval = TYPE_MAX_VALUE (type);
2032 minval = TYPE_MIN_VALUE (type);
2034 if (unsignedp && !unsignedp0)
2035 *restype_ptr = signed_type (*restype_ptr);
2037 if (TREE_TYPE (primop1) != *restype_ptr)
2038 primop1 = convert (*restype_ptr, primop1);
2039 if (type != *restype_ptr)
2041 minval = convert (*restype_ptr, minval);
2042 maxval = convert (*restype_ptr, maxval);
2045 if (unsignedp && unsignedp0)
2047 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2048 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2049 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2050 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2052 else
2054 min_gt = INT_CST_LT (primop1, minval);
2055 max_gt = INT_CST_LT (primop1, maxval);
2056 min_lt = INT_CST_LT (minval, primop1);
2057 max_lt = INT_CST_LT (maxval, primop1);
2060 val = 0;
2061 /* This used to be a switch, but Genix compiler can't handle that. */
2062 if (code == NE_EXPR)
2064 if (max_lt || min_gt)
2065 val = boolean_true_node;
2067 else if (code == EQ_EXPR)
2069 if (max_lt || min_gt)
2070 val = boolean_false_node;
2072 else if (code == LT_EXPR)
2074 if (max_lt)
2075 val = boolean_true_node;
2076 if (!min_lt)
2077 val = boolean_false_node;
2079 else if (code == GT_EXPR)
2081 if (min_gt)
2082 val = boolean_true_node;
2083 if (!max_gt)
2084 val = boolean_false_node;
2086 else if (code == LE_EXPR)
2088 if (!max_gt)
2089 val = boolean_true_node;
2090 if (min_gt)
2091 val = boolean_false_node;
2093 else if (code == GE_EXPR)
2095 if (!min_lt)
2096 val = boolean_true_node;
2097 if (max_lt)
2098 val = boolean_false_node;
2101 /* If primop0 was sign-extended and unsigned comparison specd,
2102 we did a signed comparison above using the signed type bounds.
2103 But the comparison we output must be unsigned.
2105 Also, for inequalities, VAL is no good; but if the signed
2106 comparison had *any* fixed result, it follows that the
2107 unsigned comparison just tests the sign in reverse
2108 (positive values are LE, negative ones GE).
2109 So we can generate an unsigned comparison
2110 against an extreme value of the signed type. */
2112 if (unsignedp && !unsignedp0)
2114 if (val != 0)
2115 switch (code)
2117 case LT_EXPR:
2118 case GE_EXPR:
2119 primop1 = TYPE_MIN_VALUE (type);
2120 val = 0;
2121 break;
2123 case LE_EXPR:
2124 case GT_EXPR:
2125 primop1 = TYPE_MAX_VALUE (type);
2126 val = 0;
2127 break;
2129 type = unsigned_type (type);
2132 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2134 /* This is the case of (char)x >?< 0x80, which people used to use
2135 expecting old C compilers to change the 0x80 into -0x80. */
2136 if (val == boolean_false_node)
2137 warning ("comparison is always 0 due to limited range of data type");
2138 if (val == boolean_true_node)
2139 warning ("comparison is always 1 due to limited range of data type");
2142 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2144 /* This is the case of (unsigned char)x >?< -1 or < 0. */
2145 if (val == boolean_false_node)
2146 warning ("comparison is always 0 due to limited range of data type");
2147 if (val == boolean_true_node)
2148 warning ("comparison is always 1 due to limited range of data type");
2151 if (val != 0)
2153 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2154 if (TREE_SIDE_EFFECTS (primop0))
2155 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2156 return val;
2159 /* Value is not predetermined, but do the comparison
2160 in the type of the operand that is not constant.
2161 TYPE is already properly set. */
2163 else if (real1 && real2
2164 && (TYPE_PRECISION (TREE_TYPE (primop0))
2165 == TYPE_PRECISION (TREE_TYPE (primop1))))
2166 type = TREE_TYPE (primop0);
2168 /* If args' natural types are both narrower than nominal type
2169 and both extend in the same manner, compare them
2170 in the type of the wider arg.
2171 Otherwise must actually extend both to the nominal
2172 common type lest different ways of extending
2173 alter the result.
2174 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2176 else if (unsignedp0 == unsignedp1 && real1 == real2
2177 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2178 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2180 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2181 type = signed_or_unsigned_type (unsignedp0
2182 || TREE_UNSIGNED (*restype_ptr),
2183 type);
2184 /* Make sure shorter operand is extended the right way
2185 to match the longer operand. */
2186 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2187 primop0);
2188 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2189 primop1);
2191 else
2193 /* Here we must do the comparison on the nominal type
2194 using the args exactly as we received them. */
2195 type = *restype_ptr;
2196 primop0 = op0;
2197 primop1 = op1;
2199 if (!real1 && !real2 && integer_zerop (primop1)
2200 && TREE_UNSIGNED (*restype_ptr))
2202 tree value = 0;
2203 switch (code)
2205 case GE_EXPR:
2206 /* All unsigned values are >= 0, so we warn if extra warnings
2207 are requested. However, if OP0 is a constant that is
2208 >= 0, the signedness of the comparison isn't an issue,
2209 so suppress the warning. */
2210 if (extra_warnings
2211 && ! (TREE_CODE (primop0) == INTEGER_CST
2212 && ! TREE_OVERFLOW (convert (signed_type (type),
2213 primop0))))
2214 warning ("unsigned value >= 0 is always 1");
2215 value = boolean_true_node;
2216 break;
2218 case LT_EXPR:
2219 if (extra_warnings
2220 && ! (TREE_CODE (primop0) == INTEGER_CST
2221 && ! TREE_OVERFLOW (convert (signed_type (type),
2222 primop0))))
2223 warning ("unsigned value < 0 is always 0");
2224 value = boolean_false_node;
2227 if (value != 0)
2229 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2230 if (TREE_SIDE_EFFECTS (primop0))
2231 return build (COMPOUND_EXPR, TREE_TYPE (value),
2232 primop0, value);
2233 return value;
2238 *op0_ptr = convert (type, primop0);
2239 *op1_ptr = convert (type, primop1);
2241 *restype_ptr = boolean_type_node;
2243 return 0;
2246 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2247 or validate its data type for an `if' or `while' statement or ?..: exp.
2249 This preparation consists of taking the ordinary
2250 representation of an expression expr and producing a valid tree
2251 boolean expression describing whether expr is nonzero. We could
2252 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2253 but we optimize comparisons, &&, ||, and !.
2255 The resulting type should always be `boolean_type_node'. */
2257 tree
2258 truthvalue_conversion (expr)
2259 tree expr;
2261 if (TREE_CODE (expr) == ERROR_MARK)
2262 return expr;
2264 #if 0 /* This appears to be wrong for C++. */
2265 /* These really should return error_mark_node after 2.4 is stable.
2266 But not all callers handle ERROR_MARK properly. */
2267 switch (TREE_CODE (TREE_TYPE (expr)))
2269 case RECORD_TYPE:
2270 error ("struct type value used where scalar is required");
2271 return boolean_false_node;
2273 case UNION_TYPE:
2274 error ("union type value used where scalar is required");
2275 return boolean_false_node;
2277 case ARRAY_TYPE:
2278 error ("array type value used where scalar is required");
2279 return boolean_false_node;
2281 default:
2282 break;
2284 #endif /* 0 */
2286 switch (TREE_CODE (expr))
2288 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2289 or comparison expressions as truth values at this level. */
2290 #if 0
2291 case COMPONENT_REF:
2292 /* A one-bit unsigned bit-field is already acceptable. */
2293 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
2294 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
2295 return expr;
2296 break;
2297 #endif
2299 case EQ_EXPR:
2300 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2301 or comparison expressions as truth values at this level. */
2302 #if 0
2303 if (integer_zerop (TREE_OPERAND (expr, 1)))
2304 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
2305 #endif
2306 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2307 case TRUTH_ANDIF_EXPR:
2308 case TRUTH_ORIF_EXPR:
2309 case TRUTH_AND_EXPR:
2310 case TRUTH_OR_EXPR:
2311 case TRUTH_XOR_EXPR:
2312 case TRUTH_NOT_EXPR:
2313 TREE_TYPE (expr) = boolean_type_node;
2314 return expr;
2316 case ERROR_MARK:
2317 return expr;
2319 case INTEGER_CST:
2320 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2322 case REAL_CST:
2323 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2325 case ADDR_EXPR:
2326 /* If we are taking the address of a external decl, it might be zero
2327 if it is weak, so we cannot optimize. */
2328 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr, 0))) == 'd'
2329 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2330 break;
2332 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2333 return build (COMPOUND_EXPR, boolean_type_node,
2334 TREE_OPERAND (expr, 0), boolean_true_node);
2335 else
2336 return boolean_true_node;
2338 case COMPLEX_EXPR:
2339 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2340 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2341 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2342 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2345 case NEGATE_EXPR:
2346 case ABS_EXPR:
2347 case FLOAT_EXPR:
2348 case FFS_EXPR:
2349 /* These don't change whether an object is non-zero or zero. */
2350 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2352 case LROTATE_EXPR:
2353 case RROTATE_EXPR:
2354 /* These don't change whether an object is zero or non-zero, but
2355 we can't ignore them if their second arg has side-effects. */
2356 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2357 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2358 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2359 else
2360 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2362 case COND_EXPR:
2363 /* Distribute the conversion into the arms of a COND_EXPR. */
2364 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2365 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2366 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2368 case CONVERT_EXPR:
2369 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2370 since that affects how `default_conversion' will behave. */
2371 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2372 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2373 break;
2374 /* fall through... */
2375 case NOP_EXPR:
2376 /* If this is widening the argument, we can ignore it. */
2377 if (TYPE_PRECISION (TREE_TYPE (expr))
2378 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2379 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2380 break;
2382 case MINUS_EXPR:
2383 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2384 this case. */
2385 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2386 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2387 break;
2388 /* fall through... */
2389 case BIT_XOR_EXPR:
2390 /* This and MINUS_EXPR can be changed into a comparison of the
2391 two objects. */
2392 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2393 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2394 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2395 TREE_OPERAND (expr, 1), 1);
2396 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2397 fold (build1 (NOP_EXPR,
2398 TREE_TYPE (TREE_OPERAND (expr, 0)),
2399 TREE_OPERAND (expr, 1))), 1);
2401 case BIT_AND_EXPR:
2402 if (integer_onep (TREE_OPERAND (expr, 1))
2403 && TREE_TYPE (expr) != boolean_type_node)
2404 /* Using convert here would cause infinite recursion. */
2405 return build1 (NOP_EXPR, boolean_type_node, expr);
2406 break;
2408 case MODIFY_EXPR:
2409 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2410 warning ("suggest parentheses around assignment used as truth value");
2411 break;
2414 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2415 return (build_binary_op
2416 ((TREE_SIDE_EFFECTS (expr)
2417 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2418 truthvalue_conversion (build_unary_op (REALPART_EXPR, expr, 0)),
2419 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, expr, 0)),
2420 0));
2422 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2425 /* Read the rest of a #-directive from input stream FINPUT.
2426 In normal use, the directive name and the white space after it
2427 have already been read, so they won't be included in the result.
2428 We allow for the fact that the directive line may contain
2429 a newline embedded within a character or string literal which forms
2430 a part of the directive.
2432 The value is a string in a reusable buffer. It remains valid
2433 only until the next time this function is called.
2435 The terminating character ('\n' or EOF) is left in FINPUT for the
2436 caller to re-read. */
2438 char *
2439 get_directive_line (finput)
2440 register FILE *finput;
2442 static char *directive_buffer = NULL;
2443 static unsigned buffer_length = 0;
2444 register char *p;
2445 register char *buffer_limit;
2446 register int looking_for = 0;
2447 register int char_escaped = 0;
2449 if (buffer_length == 0)
2451 directive_buffer = (char *)xmalloc (128);
2452 buffer_length = 128;
2455 buffer_limit = &directive_buffer[buffer_length];
2457 for (p = directive_buffer; ; )
2459 int c;
2461 /* Make buffer bigger if it is full. */
2462 if (p >= buffer_limit)
2464 register unsigned bytes_used = (p - directive_buffer);
2466 buffer_length *= 2;
2467 directive_buffer
2468 = (char *)xrealloc (directive_buffer, buffer_length);
2469 p = &directive_buffer[bytes_used];
2470 buffer_limit = &directive_buffer[buffer_length];
2473 c = getc (finput);
2475 /* Discard initial whitespace. */
2476 if ((c == ' ' || c == '\t') && p == directive_buffer)
2477 continue;
2479 /* Detect the end of the directive. */
2480 if (looking_for == 0
2481 && (c == '\n' || c == EOF))
2483 ungetc (c, finput);
2484 c = '\0';
2487 *p++ = c;
2489 if (c == 0)
2490 return directive_buffer;
2492 /* Handle string and character constant syntax. */
2493 if (looking_for)
2495 if (looking_for == c && !char_escaped)
2496 looking_for = 0; /* Found terminator... stop looking. */
2498 else
2499 if (c == '\'' || c == '"')
2500 looking_for = c; /* Don't stop buffering until we see another
2501 another one of these (or an EOF). */
2503 /* Handle backslash. */
2504 char_escaped = (c == '\\' && ! char_escaped);
2508 /* Make a variant type in the proper way for C/C++, propagating qualifiers
2509 down to the element type of an array. */
2511 tree
2512 c_build_type_variant (type, constp, volatilep)
2513 tree type;
2514 int constp, volatilep;
2516 if (TREE_CODE (type) == ARRAY_TYPE)
2517 return build_array_type (c_build_type_variant (TREE_TYPE (type),
2518 constp, volatilep),
2519 TYPE_DOMAIN (type));
2520 return build_type_variant (type, constp, volatilep);