Remove some out of date comments.
[official-gcc.git] / gcc / c-common.c
blobd921661578e55a8b5bfeabbfe3b03b005ae026ed
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996 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);
1400 if (info->is_scan && format_char == '[')
1402 /* Skip over scan set, in case it happens to have '%' in it. */
1403 if (*format_chars == '^')
1404 ++format_chars;
1405 /* Find closing bracket; if one is hit immediately, then
1406 it's part of the scan set rather than a terminator. */
1407 if (*format_chars == ']')
1408 ++format_chars;
1409 while (*format_chars && *format_chars != ']')
1410 ++format_chars;
1411 if (*format_chars != ']')
1412 /* The end of the format string was reached. */
1413 warning ("no closing `]' for `%%[' format");
1415 if (suppressed)
1417 if (index (fci->flag_chars, '*') == 0)
1419 sprintf (message,
1420 "suppression of `%c' conversion in format",
1421 format_char);
1422 warning (message);
1424 continue;
1426 for (i = 0; flag_chars[i] != 0; ++i)
1428 if (index (fci->flag_chars, flag_chars[i]) == 0)
1430 sprintf (message, "flag `%c' used with type `%c'",
1431 flag_chars[i], format_char);
1432 warning (message);
1435 if (precise && index (flag_chars, '0') != 0
1436 && (format_char == 'd' || format_char == 'i'
1437 || format_char == 'o' || format_char == 'u'
1438 || format_char == 'x' || format_char == 'x'))
1440 sprintf (message,
1441 "`0' flag ignored with precision specifier and `%c' format",
1442 format_char);
1443 warning (message);
1445 switch (length_char)
1447 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
1448 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1449 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
1450 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1451 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
1453 if (wanted_type == 0)
1455 sprintf (message,
1456 "use of `%c' length character with `%c' type character",
1457 length_char, format_char);
1458 warning (message);
1462 ** XXX -- should kvetch about stuff such as
1463 ** {
1464 ** const int i;
1466 ** scanf ("%d", &i);
1467 ** }
1470 /* Finally. . .check type of argument against desired type! */
1471 if (info->first_arg_num == 0)
1472 continue;
1473 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1474 /* This specifier takes no argument. */
1475 continue;
1476 if (params == 0)
1478 warning (tfaff);
1479 return;
1481 cur_param = TREE_VALUE (params);
1482 params = TREE_CHAIN (params);
1483 ++arg_num;
1484 cur_type = TREE_TYPE (cur_param);
1486 /* Check the types of any additional pointer arguments
1487 that precede the "real" argument. */
1488 for (i = 0; i < fci->pointer_count; ++i)
1490 if (TREE_CODE (cur_type) == POINTER_TYPE)
1492 cur_type = TREE_TYPE (cur_type);
1493 continue;
1495 if (TREE_CODE (cur_type) != ERROR_MARK)
1497 sprintf (message,
1498 "format argument is not a %s (arg %d)",
1499 ((fci->pointer_count == 1) ? "pointer" : "pointer to a pointer"),
1500 arg_num);
1501 warning (message);
1503 break;
1506 /* Check the type of the "real" argument, if there's a type we want. */
1507 if (i == fci->pointer_count && wanted_type != 0
1508 && TREE_CODE (cur_type) != ERROR_MARK
1509 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1510 /* If we want `void *', allow any pointer type.
1511 (Anything else would already have got a warning.) */
1512 && ! (wanted_type == void_type_node
1513 && fci->pointer_count > 0)
1514 /* Don't warn about differences merely in signedness. */
1515 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
1516 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
1517 && (TREE_UNSIGNED (wanted_type)
1518 ? wanted_type == (cur_type = unsigned_type (cur_type))
1519 : wanted_type == (cur_type = signed_type (cur_type))))
1520 /* Likewise, "signed char", "unsigned char" and "char" are
1521 equivalent but the above test won't consider them equivalent. */
1522 && ! (wanted_type == char_type_node
1523 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
1524 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
1526 register char *this;
1527 register char *that;
1529 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
1530 that = 0;
1531 if (TREE_CODE (cur_type) != ERROR_MARK
1532 && TYPE_NAME (cur_type) != 0
1533 && TREE_CODE (cur_type) != INTEGER_TYPE
1534 && !(TREE_CODE (cur_type) == POINTER_TYPE
1535 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
1537 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
1538 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
1539 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1540 else
1541 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
1544 /* A nameless type can't possibly match what the format wants.
1545 So there will be a warning for it.
1546 Make up a string to describe vaguely what it is. */
1547 if (that == 0)
1549 if (TREE_CODE (cur_type) == POINTER_TYPE)
1550 that = "pointer";
1551 else
1552 that = "different type";
1555 /* Make the warning better in case of mismatch of int vs long. */
1556 if (TREE_CODE (cur_type) == INTEGER_TYPE
1557 && TREE_CODE (wanted_type) == INTEGER_TYPE
1558 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
1559 && TYPE_NAME (cur_type) != 0
1560 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
1561 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1563 if (strcmp (this, that) != 0)
1565 sprintf (message, "%s format, %s arg (arg %d)",
1566 this, that, arg_num);
1567 warning (message);
1573 /* Print a warning if a constant expression had overflow in folding.
1574 Invoke this function on every expression that the language
1575 requires to be a constant expression.
1576 Note the ANSI C standard says it is erroneous for a
1577 constant expression to overflow. */
1579 void
1580 constant_expression_warning (value)
1581 tree value;
1583 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1584 || TREE_CODE (value) == COMPLEX_CST)
1585 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1586 pedwarn ("overflow in constant expression");
1589 /* Print a warning if an expression had overflow in folding.
1590 Invoke this function on every expression that
1591 (1) appears in the source code, and
1592 (2) might be a constant expression that overflowed, and
1593 (3) is not already checked by convert_and_check;
1594 however, do not invoke this function on operands of explicit casts. */
1596 void
1597 overflow_warning (value)
1598 tree value;
1600 if ((TREE_CODE (value) == INTEGER_CST
1601 || (TREE_CODE (value) == COMPLEX_CST
1602 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1603 && TREE_OVERFLOW (value))
1605 TREE_OVERFLOW (value) = 0;
1606 if (skip_evaluation == 0)
1607 warning ("integer overflow in expression");
1609 else if ((TREE_CODE (value) == REAL_CST
1610 || (TREE_CODE (value) == COMPLEX_CST
1611 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1612 && TREE_OVERFLOW (value))
1614 TREE_OVERFLOW (value) = 0;
1615 if (skip_evaluation == 0)
1616 warning ("floating point overflow in expression");
1620 /* Print a warning if a large constant is truncated to unsigned,
1621 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1622 Invoke this function on every expression that might be implicitly
1623 converted to an unsigned type. */
1625 void
1626 unsigned_conversion_warning (result, operand)
1627 tree result, operand;
1629 if (TREE_CODE (operand) == INTEGER_CST
1630 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
1631 && TREE_UNSIGNED (TREE_TYPE (result))
1632 && skip_evaluation == 0
1633 && !int_fits_type_p (operand, TREE_TYPE (result)))
1635 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
1636 /* This detects cases like converting -129 or 256 to unsigned char. */
1637 warning ("large integer implicitly truncated to unsigned type");
1638 else if (warn_conversion)
1639 warning ("negative integer implicitly converted to unsigned type");
1643 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1644 Invoke this function on every expression that is converted implicitly,
1645 i.e. because of language rules and not because of an explicit cast. */
1647 tree
1648 convert_and_check (type, expr)
1649 tree type, expr;
1651 tree t = convert (type, expr);
1652 if (TREE_CODE (t) == INTEGER_CST)
1654 if (TREE_OVERFLOW (t))
1656 TREE_OVERFLOW (t) = 0;
1658 /* Do not diagnose overflow in a constant expression merely
1659 because a conversion overflowed. */
1660 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
1662 /* No warning for converting 0x80000000 to int. */
1663 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
1664 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1665 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
1666 /* If EXPR fits in the unsigned version of TYPE,
1667 don't warn unless pedantic. */
1668 if ((pedantic
1669 || TREE_UNSIGNED (type)
1670 || ! int_fits_type_p (expr, unsigned_type (type)))
1671 && skip_evaluation == 0)
1672 warning ("overflow in implicit constant conversion");
1674 else
1675 unsigned_conversion_warning (t, expr);
1677 return t;
1680 void
1681 c_expand_expr_stmt (expr)
1682 tree expr;
1684 /* Do default conversion if safe and possibly important,
1685 in case within ({...}). */
1686 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
1687 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1688 expr = default_conversion (expr);
1690 if (TREE_TYPE (expr) != error_mark_node
1691 && TYPE_SIZE (TREE_TYPE (expr)) == 0
1692 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1693 error ("expression statement has incomplete type");
1695 expand_expr_stmt (expr);
1698 /* Validate the expression after `case' and apply default promotions. */
1700 tree
1701 check_case_value (value)
1702 tree value;
1704 if (value == NULL_TREE)
1705 return value;
1707 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1708 STRIP_TYPE_NOPS (value);
1710 if (TREE_CODE (value) != INTEGER_CST
1711 && value != error_mark_node)
1713 error ("case label does not reduce to an integer constant");
1714 value = error_mark_node;
1716 else
1717 /* Promote char or short to int. */
1718 value = default_conversion (value);
1720 constant_expression_warning (value);
1722 return value;
1725 /* Return an integer type with BITS bits of precision,
1726 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1728 tree
1729 type_for_size (bits, unsignedp)
1730 unsigned bits;
1731 int unsignedp;
1733 if (bits == TYPE_PRECISION (integer_type_node))
1734 return unsignedp ? unsigned_type_node : integer_type_node;
1736 if (bits == TYPE_PRECISION (signed_char_type_node))
1737 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1739 if (bits == TYPE_PRECISION (short_integer_type_node))
1740 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1742 if (bits == TYPE_PRECISION (long_integer_type_node))
1743 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1745 if (bits == TYPE_PRECISION (long_long_integer_type_node))
1746 return (unsignedp ? long_long_unsigned_type_node
1747 : long_long_integer_type_node);
1749 if (bits <= TYPE_PRECISION (intQI_type_node))
1750 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1752 if (bits <= TYPE_PRECISION (intHI_type_node))
1753 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1755 if (bits <= TYPE_PRECISION (intSI_type_node))
1756 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1758 if (bits <= TYPE_PRECISION (intDI_type_node))
1759 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1761 return 0;
1764 /* Return a data type that has machine mode MODE.
1765 If the mode is an integer,
1766 then UNSIGNEDP selects between signed and unsigned types. */
1768 tree
1769 type_for_mode (mode, unsignedp)
1770 enum machine_mode mode;
1771 int unsignedp;
1773 if (mode == TYPE_MODE (integer_type_node))
1774 return unsignedp ? unsigned_type_node : integer_type_node;
1776 if (mode == TYPE_MODE (signed_char_type_node))
1777 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1779 if (mode == TYPE_MODE (short_integer_type_node))
1780 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1782 if (mode == TYPE_MODE (long_integer_type_node))
1783 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1785 if (mode == TYPE_MODE (long_long_integer_type_node))
1786 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
1788 if (mode == TYPE_MODE (intQI_type_node))
1789 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1791 if (mode == TYPE_MODE (intHI_type_node))
1792 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1794 if (mode == TYPE_MODE (intSI_type_node))
1795 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1797 if (mode == TYPE_MODE (intDI_type_node))
1798 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1800 if (mode == TYPE_MODE (float_type_node))
1801 return float_type_node;
1803 if (mode == TYPE_MODE (double_type_node))
1804 return double_type_node;
1806 if (mode == TYPE_MODE (long_double_type_node))
1807 return long_double_type_node;
1809 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
1810 return build_pointer_type (char_type_node);
1812 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
1813 return build_pointer_type (integer_type_node);
1815 return 0;
1818 /* Return the minimum number of bits needed to represent VALUE in a
1819 signed or unsigned type, UNSIGNEDP says which. */
1822 min_precision (value, unsignedp)
1823 tree value;
1824 int unsignedp;
1826 int log;
1828 /* If the value is negative, compute its negative minus 1. The latter
1829 adjustment is because the absolute value of the largest negative value
1830 is one larger than the largest positive value. This is equivalent to
1831 a bit-wise negation, so use that operation instead. */
1833 if (tree_int_cst_sgn (value) < 0)
1834 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
1836 /* Return the number of bits needed, taking into account the fact
1837 that we need one more bit for a signed than unsigned type. */
1839 if (integer_zerop (value))
1840 log = 0;
1841 else if (TREE_INT_CST_HIGH (value) != 0)
1842 log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
1843 else
1844 log = floor_log2 (TREE_INT_CST_LOW (value));
1846 return log + 1 + ! unsignedp;
1849 /* Print an error message for invalid operands to arith operation CODE.
1850 NOP_EXPR is used as a special case (see truthvalue_conversion). */
1852 void
1853 binary_op_error (code)
1854 enum tree_code code;
1856 register char *opname = "unknown";
1858 switch (code)
1860 case NOP_EXPR:
1861 error ("invalid truth-value expression");
1862 return;
1864 case PLUS_EXPR:
1865 opname = "+"; break;
1866 case MINUS_EXPR:
1867 opname = "-"; break;
1868 case MULT_EXPR:
1869 opname = "*"; break;
1870 case MAX_EXPR:
1871 opname = "max"; break;
1872 case MIN_EXPR:
1873 opname = "min"; break;
1874 case EQ_EXPR:
1875 opname = "=="; break;
1876 case NE_EXPR:
1877 opname = "!="; break;
1878 case LE_EXPR:
1879 opname = "<="; break;
1880 case GE_EXPR:
1881 opname = ">="; break;
1882 case LT_EXPR:
1883 opname = "<"; break;
1884 case GT_EXPR:
1885 opname = ">"; break;
1886 case LSHIFT_EXPR:
1887 opname = "<<"; break;
1888 case RSHIFT_EXPR:
1889 opname = ">>"; break;
1890 case TRUNC_MOD_EXPR:
1891 case FLOOR_MOD_EXPR:
1892 opname = "%"; break;
1893 case TRUNC_DIV_EXPR:
1894 case FLOOR_DIV_EXPR:
1895 opname = "/"; break;
1896 case BIT_AND_EXPR:
1897 opname = "&"; break;
1898 case BIT_IOR_EXPR:
1899 opname = "|"; break;
1900 case TRUTH_ANDIF_EXPR:
1901 opname = "&&"; break;
1902 case TRUTH_ORIF_EXPR:
1903 opname = "||"; break;
1904 case BIT_XOR_EXPR:
1905 opname = "^"; break;
1906 case LROTATE_EXPR:
1907 case RROTATE_EXPR:
1908 opname = "rotate"; break;
1910 error ("invalid operands to binary %s", opname);
1913 /* Subroutine of build_binary_op, used for comparison operations.
1914 See if the operands have both been converted from subword integer types
1915 and, if so, perhaps change them both back to their original type.
1916 This function is also responsible for converting the two operands
1917 to the proper common type for comparison.
1919 The arguments of this function are all pointers to local variables
1920 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
1921 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
1923 If this function returns nonzero, it means that the comparison has
1924 a constant value. What this function returns is an expression for
1925 that value. */
1927 tree
1928 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
1929 tree *op0_ptr, *op1_ptr;
1930 tree *restype_ptr;
1931 enum tree_code *rescode_ptr;
1933 register tree type;
1934 tree op0 = *op0_ptr;
1935 tree op1 = *op1_ptr;
1936 int unsignedp0, unsignedp1;
1937 int real1, real2;
1938 tree primop0, primop1;
1939 enum tree_code code = *rescode_ptr;
1941 /* Throw away any conversions to wider types
1942 already present in the operands. */
1944 primop0 = get_narrower (op0, &unsignedp0);
1945 primop1 = get_narrower (op1, &unsignedp1);
1947 /* Handle the case that OP0 does not *contain* a conversion
1948 but it *requires* conversion to FINAL_TYPE. */
1950 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
1951 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
1952 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
1953 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
1955 /* If one of the operands must be floated, we cannot optimize. */
1956 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
1957 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
1959 /* If first arg is constant, swap the args (changing operation
1960 so value is preserved), for canonicalization. Don't do this if
1961 the second arg is 0. */
1963 if (TREE_CONSTANT (primop0)
1964 && ! integer_zerop (primop1) && ! real_zerop (primop1))
1966 register tree tem = primop0;
1967 register int temi = unsignedp0;
1968 primop0 = primop1;
1969 primop1 = tem;
1970 tem = op0;
1971 op0 = op1;
1972 op1 = tem;
1973 *op0_ptr = op0;
1974 *op1_ptr = op1;
1975 unsignedp0 = unsignedp1;
1976 unsignedp1 = temi;
1977 temi = real1;
1978 real1 = real2;
1979 real2 = temi;
1981 switch (code)
1983 case LT_EXPR:
1984 code = GT_EXPR;
1985 break;
1986 case GT_EXPR:
1987 code = LT_EXPR;
1988 break;
1989 case LE_EXPR:
1990 code = GE_EXPR;
1991 break;
1992 case GE_EXPR:
1993 code = LE_EXPR;
1994 break;
1996 *rescode_ptr = code;
1999 /* If comparing an integer against a constant more bits wide,
2000 maybe we can deduce a value of 1 or 0 independent of the data.
2001 Or else truncate the constant now
2002 rather than extend the variable at run time.
2004 This is only interesting if the constant is the wider arg.
2005 Also, it is not safe if the constant is unsigned and the
2006 variable arg is signed, since in this case the variable
2007 would be sign-extended and then regarded as unsigned.
2008 Our technique fails in this case because the lowest/highest
2009 possible unsigned results don't follow naturally from the
2010 lowest/highest possible values of the variable operand.
2011 For just EQ_EXPR and NE_EXPR there is another technique that
2012 could be used: see if the constant can be faithfully represented
2013 in the other operand's type, by truncating it and reextending it
2014 and see if that preserves the constant's value. */
2016 if (!real1 && !real2
2017 && TREE_CODE (primop1) == INTEGER_CST
2018 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2020 int min_gt, max_gt, min_lt, max_lt;
2021 tree maxval, minval;
2022 /* 1 if comparison is nominally unsigned. */
2023 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2024 tree val;
2026 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2028 maxval = TYPE_MAX_VALUE (type);
2029 minval = TYPE_MIN_VALUE (type);
2031 if (unsignedp && !unsignedp0)
2032 *restype_ptr = signed_type (*restype_ptr);
2034 if (TREE_TYPE (primop1) != *restype_ptr)
2035 primop1 = convert (*restype_ptr, primop1);
2036 if (type != *restype_ptr)
2038 minval = convert (*restype_ptr, minval);
2039 maxval = convert (*restype_ptr, maxval);
2042 if (unsignedp && unsignedp0)
2044 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2045 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2046 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2047 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2049 else
2051 min_gt = INT_CST_LT (primop1, minval);
2052 max_gt = INT_CST_LT (primop1, maxval);
2053 min_lt = INT_CST_LT (minval, primop1);
2054 max_lt = INT_CST_LT (maxval, primop1);
2057 val = 0;
2058 /* This used to be a switch, but Genix compiler can't handle that. */
2059 if (code == NE_EXPR)
2061 if (max_lt || min_gt)
2062 val = boolean_true_node;
2064 else if (code == EQ_EXPR)
2066 if (max_lt || min_gt)
2067 val = boolean_false_node;
2069 else if (code == LT_EXPR)
2071 if (max_lt)
2072 val = boolean_true_node;
2073 if (!min_lt)
2074 val = boolean_false_node;
2076 else if (code == GT_EXPR)
2078 if (min_gt)
2079 val = boolean_true_node;
2080 if (!max_gt)
2081 val = boolean_false_node;
2083 else if (code == LE_EXPR)
2085 if (!max_gt)
2086 val = boolean_true_node;
2087 if (min_gt)
2088 val = boolean_false_node;
2090 else if (code == GE_EXPR)
2092 if (!min_lt)
2093 val = boolean_true_node;
2094 if (max_lt)
2095 val = boolean_false_node;
2098 /* If primop0 was sign-extended and unsigned comparison specd,
2099 we did a signed comparison above using the signed type bounds.
2100 But the comparison we output must be unsigned.
2102 Also, for inequalities, VAL is no good; but if the signed
2103 comparison had *any* fixed result, it follows that the
2104 unsigned comparison just tests the sign in reverse
2105 (positive values are LE, negative ones GE).
2106 So we can generate an unsigned comparison
2107 against an extreme value of the signed type. */
2109 if (unsignedp && !unsignedp0)
2111 if (val != 0)
2112 switch (code)
2114 case LT_EXPR:
2115 case GE_EXPR:
2116 primop1 = TYPE_MIN_VALUE (type);
2117 val = 0;
2118 break;
2120 case LE_EXPR:
2121 case GT_EXPR:
2122 primop1 = TYPE_MAX_VALUE (type);
2123 val = 0;
2124 break;
2126 type = unsigned_type (type);
2129 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2131 /* This is the case of (char)x >?< 0x80, which people used to use
2132 expecting old C compilers to change the 0x80 into -0x80. */
2133 if (val == boolean_false_node)
2134 warning ("comparison is always 0 due to limited range of data type");
2135 if (val == boolean_true_node)
2136 warning ("comparison is always 1 due to limited range of data type");
2139 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2141 /* This is the case of (unsigned char)x >?< -1 or < 0. */
2142 if (val == boolean_false_node)
2143 warning ("comparison is always 0 due to limited range of data type");
2144 if (val == boolean_true_node)
2145 warning ("comparison is always 1 due to limited range of data type");
2148 if (val != 0)
2150 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2151 if (TREE_SIDE_EFFECTS (primop0))
2152 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2153 return val;
2156 /* Value is not predetermined, but do the comparison
2157 in the type of the operand that is not constant.
2158 TYPE is already properly set. */
2160 else if (real1 && real2
2161 && (TYPE_PRECISION (TREE_TYPE (primop0))
2162 == TYPE_PRECISION (TREE_TYPE (primop1))))
2163 type = TREE_TYPE (primop0);
2165 /* If args' natural types are both narrower than nominal type
2166 and both extend in the same manner, compare them
2167 in the type of the wider arg.
2168 Otherwise must actually extend both to the nominal
2169 common type lest different ways of extending
2170 alter the result.
2171 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2173 else if (unsignedp0 == unsignedp1 && real1 == real2
2174 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2175 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2177 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2178 type = signed_or_unsigned_type (unsignedp0
2179 || TREE_UNSIGNED (*restype_ptr),
2180 type);
2181 /* Make sure shorter operand is extended the right way
2182 to match the longer operand. */
2183 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2184 primop0);
2185 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2186 primop1);
2188 else
2190 /* Here we must do the comparison on the nominal type
2191 using the args exactly as we received them. */
2192 type = *restype_ptr;
2193 primop0 = op0;
2194 primop1 = op1;
2196 if (!real1 && !real2 && integer_zerop (primop1)
2197 && TREE_UNSIGNED (*restype_ptr))
2199 tree value = 0;
2200 switch (code)
2202 case GE_EXPR:
2203 /* All unsigned values are >= 0, so we warn if extra warnings
2204 are requested. However, if OP0 is a constant that is
2205 >= 0, the signedness of the comparison isn't an issue,
2206 so suppress the warning. */
2207 if (extra_warnings
2208 && ! (TREE_CODE (primop0) == INTEGER_CST
2209 && ! TREE_OVERFLOW (convert (signed_type (type),
2210 primop0))))
2211 warning ("unsigned value >= 0 is always 1");
2212 value = boolean_true_node;
2213 break;
2215 case LT_EXPR:
2216 if (extra_warnings
2217 && ! (TREE_CODE (primop0) == INTEGER_CST
2218 && ! TREE_OVERFLOW (convert (signed_type (type),
2219 primop0))))
2220 warning ("unsigned value < 0 is always 0");
2221 value = boolean_false_node;
2224 if (value != 0)
2226 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2227 if (TREE_SIDE_EFFECTS (primop0))
2228 return build (COMPOUND_EXPR, TREE_TYPE (value),
2229 primop0, value);
2230 return value;
2235 *op0_ptr = convert (type, primop0);
2236 *op1_ptr = convert (type, primop1);
2238 *restype_ptr = boolean_type_node;
2240 return 0;
2243 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2244 or validate its data type for an `if' or `while' statement or ?..: exp.
2246 This preparation consists of taking the ordinary
2247 representation of an expression expr and producing a valid tree
2248 boolean expression describing whether expr is nonzero. We could
2249 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2250 but we optimize comparisons, &&, ||, and !.
2252 The resulting type should always be `boolean_type_node'. */
2254 tree
2255 truthvalue_conversion (expr)
2256 tree expr;
2258 if (TREE_CODE (expr) == ERROR_MARK)
2259 return expr;
2261 #if 0 /* This appears to be wrong for C++. */
2262 /* These really should return error_mark_node after 2.4 is stable.
2263 But not all callers handle ERROR_MARK properly. */
2264 switch (TREE_CODE (TREE_TYPE (expr)))
2266 case RECORD_TYPE:
2267 error ("struct type value used where scalar is required");
2268 return boolean_false_node;
2270 case UNION_TYPE:
2271 error ("union type value used where scalar is required");
2272 return boolean_false_node;
2274 case ARRAY_TYPE:
2275 error ("array type value used where scalar is required");
2276 return boolean_false_node;
2278 default:
2279 break;
2281 #endif /* 0 */
2283 switch (TREE_CODE (expr))
2285 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2286 or comparison expressions as truth values at this level. */
2287 #if 0
2288 case COMPONENT_REF:
2289 /* A one-bit unsigned bit-field is already acceptable. */
2290 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
2291 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
2292 return expr;
2293 break;
2294 #endif
2296 case EQ_EXPR:
2297 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2298 or comparison expressions as truth values at this level. */
2299 #if 0
2300 if (integer_zerop (TREE_OPERAND (expr, 1)))
2301 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
2302 #endif
2303 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2304 case TRUTH_ANDIF_EXPR:
2305 case TRUTH_ORIF_EXPR:
2306 case TRUTH_AND_EXPR:
2307 case TRUTH_OR_EXPR:
2308 case TRUTH_XOR_EXPR:
2309 case TRUTH_NOT_EXPR:
2310 TREE_TYPE (expr) = boolean_type_node;
2311 return expr;
2313 case ERROR_MARK:
2314 return expr;
2316 case INTEGER_CST:
2317 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2319 case REAL_CST:
2320 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2322 case ADDR_EXPR:
2323 /* If we are taking the address of a external decl, it might be zero
2324 if it is weak, so we cannot optimize. */
2325 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr, 0))) == 'd'
2326 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2327 break;
2329 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2330 return build (COMPOUND_EXPR, boolean_type_node,
2331 TREE_OPERAND (expr, 0), boolean_true_node);
2332 else
2333 return boolean_true_node;
2335 case COMPLEX_EXPR:
2336 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2337 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2338 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2339 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2342 case NEGATE_EXPR:
2343 case ABS_EXPR:
2344 case FLOAT_EXPR:
2345 case FFS_EXPR:
2346 /* These don't change whether an object is non-zero or zero. */
2347 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2349 case LROTATE_EXPR:
2350 case RROTATE_EXPR:
2351 /* These don't change whether an object is zero or non-zero, but
2352 we can't ignore them if their second arg has side-effects. */
2353 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2354 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2355 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2356 else
2357 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2359 case COND_EXPR:
2360 /* Distribute the conversion into the arms of a COND_EXPR. */
2361 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2362 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2363 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2365 case CONVERT_EXPR:
2366 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2367 since that affects how `default_conversion' will behave. */
2368 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2369 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2370 break;
2371 /* fall through... */
2372 case NOP_EXPR:
2373 /* If this is widening the argument, we can ignore it. */
2374 if (TYPE_PRECISION (TREE_TYPE (expr))
2375 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2376 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2377 break;
2379 case MINUS_EXPR:
2380 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2381 this case. */
2382 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2383 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2384 break;
2385 /* fall through... */
2386 case BIT_XOR_EXPR:
2387 /* This and MINUS_EXPR can be changed into a comparison of the
2388 two objects. */
2389 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2390 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2391 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2392 TREE_OPERAND (expr, 1), 1);
2393 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2394 fold (build1 (NOP_EXPR,
2395 TREE_TYPE (TREE_OPERAND (expr, 0)),
2396 TREE_OPERAND (expr, 1))), 1);
2398 case BIT_AND_EXPR:
2399 if (integer_onep (TREE_OPERAND (expr, 1))
2400 && TREE_TYPE (expr) != boolean_type_node)
2401 /* Using convert here would cause infinite recursion. */
2402 return build1 (NOP_EXPR, boolean_type_node, expr);
2403 break;
2405 case MODIFY_EXPR:
2406 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2407 warning ("suggest parentheses around assignment used as truth value");
2408 break;
2411 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2412 return (build_binary_op
2413 ((TREE_SIDE_EFFECTS (expr)
2414 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2415 truthvalue_conversion (build_unary_op (REALPART_EXPR, expr, 0)),
2416 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, expr, 0)),
2417 0));
2419 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2422 /* Read the rest of a #-directive from input stream FINPUT.
2423 In normal use, the directive name and the white space after it
2424 have already been read, so they won't be included in the result.
2425 We allow for the fact that the directive line may contain
2426 a newline embedded within a character or string literal which forms
2427 a part of the directive.
2429 The value is a string in a reusable buffer. It remains valid
2430 only until the next time this function is called.
2432 The terminating character ('\n' or EOF) is left in FINPUT for the
2433 caller to re-read. */
2435 char *
2436 get_directive_line (finput)
2437 register FILE *finput;
2439 static char *directive_buffer = NULL;
2440 static unsigned buffer_length = 0;
2441 register char *p;
2442 register char *buffer_limit;
2443 register int looking_for = 0;
2444 register int char_escaped = 0;
2446 if (buffer_length == 0)
2448 directive_buffer = (char *)xmalloc (128);
2449 buffer_length = 128;
2452 buffer_limit = &directive_buffer[buffer_length];
2454 for (p = directive_buffer; ; )
2456 int c;
2458 /* Make buffer bigger if it is full. */
2459 if (p >= buffer_limit)
2461 register unsigned bytes_used = (p - directive_buffer);
2463 buffer_length *= 2;
2464 directive_buffer
2465 = (char *)xrealloc (directive_buffer, buffer_length);
2466 p = &directive_buffer[bytes_used];
2467 buffer_limit = &directive_buffer[buffer_length];
2470 c = getc (finput);
2472 /* Discard initial whitespace. */
2473 if ((c == ' ' || c == '\t') && p == directive_buffer)
2474 continue;
2476 /* Detect the end of the directive. */
2477 if (looking_for == 0
2478 && (c == '\n' || c == EOF))
2480 ungetc (c, finput);
2481 c = '\0';
2484 *p++ = c;
2486 if (c == 0)
2487 return directive_buffer;
2489 /* Handle string and character constant syntax. */
2490 if (looking_for)
2492 if (looking_for == c && !char_escaped)
2493 looking_for = 0; /* Found terminator... stop looking. */
2495 else
2496 if (c == '\'' || c == '"')
2497 looking_for = c; /* Don't stop buffering until we see another
2498 another one of these (or an EOF). */
2500 /* Handle backslash. */
2501 char_escaped = (c == '\\' && ! char_escaped);
2505 /* Make a variant type in the proper way for C/C++, propagating qualifiers
2506 down to the element type of an array. */
2508 tree
2509 c_build_type_variant (type, constp, volatilep)
2510 tree type;
2511 int constp, volatilep;
2513 if (TREE_CODE (type) == ARRAY_TYPE)
2515 tree real_main_variant = TYPE_MAIN_VARIANT (type);
2517 push_obstacks (TYPE_OBSTACK (real_main_variant),
2518 TYPE_OBSTACK (real_main_variant));
2519 type = build_array_type (c_build_type_variant (TREE_TYPE (type),
2520 constp, volatilep),
2521 TYPE_DOMAIN (type));
2523 /* TYPE must be on same obstack as REAL_MAIN_VARIANT. If not,
2524 make a copy. (TYPE might have come from the hash table and
2525 REAL_MAIN_VARIANT might be in some function's obstack.) */
2527 if (TYPE_OBSTACK (type) != TYPE_OBSTACK (real_main_variant))
2529 type = copy_node (type);
2530 TYPE_POINTER_TO (type) = TYPE_REFERENCE_TO (type) = 0;
2533 TYPE_MAIN_VARIANT (type) = real_main_variant;
2534 pop_obstacks ();
2536 return build_type_variant (type, constp, volatilep);