Import final gcc2 snapshot (990109)
[official-gcc.git] / gcc / c-common.c
blob5432305b51df0556a6422014e4ebc7baf4066a11
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 93, 94, 95, 96, 97, 1998 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 "system.h"
23 #include "tree.h"
24 #include "c-lex.h"
25 #include "c-tree.h"
26 #include "flags.h"
27 #include "obstack.h"
29 #ifndef WCHAR_TYPE_SIZE
30 #ifdef INT_TYPE_SIZE
31 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
32 #else
33 #define WCHAR_TYPE_SIZE BITS_PER_WORD
34 #endif
35 #endif
37 extern struct obstack permanent_obstack;
39 /* Nonzero means the expression being parsed will never be evaluated.
40 This is a count, since unevaluated expressions can nest. */
41 int skip_evaluation;
43 enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
44 A_NO_CHECK_MEMORY_USAGE, A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE,
45 A_SECTION, A_ALIGNED, A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK,
46 A_ALIAS};
48 enum format_type { printf_format_type, scanf_format_type,
49 strftime_format_type };
51 static void declare_hidden_char_array PROTO((char *, char *));
52 static void add_attribute PROTO((enum attrs, char *,
53 int, int, int));
54 static void init_attributes PROTO((void));
55 static void record_function_format PROTO((tree, tree, enum format_type,
56 int, int));
57 static void record_international_format PROTO((tree, tree, int));
58 static void tfaff PROTO((void));
60 /* Keep a stack of if statements. The value recorded is the number of
61 compound statements seen up to the if keyword. */
62 static int *if_stack;
64 /* Amount of space in the if statement stack. */
65 static int if_stack_space = 0;
67 /* Stack pointer. */
68 static int if_stack_pointer = 0;
70 void
71 c_expand_start_cond (cond, exitflag, compstmt_count)
72 tree cond;
73 int exitflag;
74 int compstmt_count;
76 /* Make sure there is enough space on the stack. */
77 if (if_stack_space == 0)
79 if_stack_space = 10;
80 if_stack = (int *)xmalloc (10 * sizeof (int));
82 else if (if_stack_space == if_stack_pointer)
84 if_stack_space += 10;
85 if_stack = (int *)xrealloc (if_stack, if_stack_space * sizeof (int));
88 /* Record this if statement. */
89 if_stack[if_stack_pointer++] = compstmt_count;
91 expand_start_cond (cond, exitflag);
94 void
95 c_expand_end_cond ()
97 if_stack_pointer--;
98 expand_end_cond ();
101 void
102 c_expand_start_else ()
104 if (warn_parentheses
105 && if_stack_pointer > 1
106 && if_stack[if_stack_pointer - 1] == if_stack[if_stack_pointer - 2])
107 warning ("suggest explicit braces to avoid ambiguous `else'");
109 /* This if statement can no longer cause a dangling else. */
110 if_stack[if_stack_pointer - 1]--;
112 expand_start_else ();
115 /* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
117 void
118 declare_function_name ()
120 char *name, *printable_name;
122 if (current_function_decl == NULL)
124 name = "";
125 printable_name = "top level";
127 else
129 /* Allow functions to be nameless (such as artificial ones). */
130 if (DECL_NAME (current_function_decl))
131 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
132 else
133 name = "";
134 printable_name = (*decl_printable_name) (current_function_decl, 2);
137 declare_hidden_char_array ("__FUNCTION__", name);
138 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
141 static void
142 declare_hidden_char_array (name, value)
143 char *name, *value;
145 tree decl, type, init;
146 int vlen;
148 /* If the default size of char arrays isn't big enough for the name,
149 or if we want to give warnings for large objects, make a bigger one. */
150 vlen = strlen (value) + 1;
151 type = char_array_type_node;
152 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TREE_TYPE (type))) < vlen
153 || warn_larger_than)
154 type = build_array_type (char_type_node,
155 build_index_type (build_int_2 (vlen, 0)));
156 push_obstacks_nochange ();
157 decl = build_decl (VAR_DECL, get_identifier (name), type);
158 TREE_STATIC (decl) = 1;
159 TREE_READONLY (decl) = 1;
160 TREE_ASM_WRITTEN (decl) = 1;
161 DECL_SOURCE_LINE (decl) = 0;
162 DECL_ARTIFICIAL (decl) = 1;
163 DECL_IN_SYSTEM_HEADER (decl) = 1;
164 DECL_IGNORED_P (decl) = 1;
165 init = build_string (vlen, value);
166 TREE_TYPE (init) = type;
167 DECL_INITIAL (decl) = init;
168 finish_decl (pushdecl (decl), init, NULL_TREE);
171 /* Given a chain of STRING_CST nodes,
172 concatenate them into one STRING_CST
173 and give it a suitable array-of-chars data type. */
175 tree
176 combine_strings (strings)
177 tree strings;
179 register tree value, t;
180 register int length = 1;
181 int wide_length = 0;
182 int wide_flag = 0;
183 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
184 int nchars;
186 if (TREE_CHAIN (strings))
188 /* More than one in the chain, so concatenate. */
189 register char *p, *q;
191 /* Don't include the \0 at the end of each substring,
192 except for the last one.
193 Count wide strings and ordinary strings separately. */
194 for (t = strings; t; t = TREE_CHAIN (t))
196 if (TREE_TYPE (t) == wchar_array_type_node)
198 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
199 wide_flag = 1;
201 else
202 length += (TREE_STRING_LENGTH (t) - 1);
205 /* If anything is wide, the non-wides will be converted,
206 which makes them take more space. */
207 if (wide_flag)
208 length = length * wchar_bytes + wide_length;
210 p = savealloc (length);
212 /* Copy the individual strings into the new combined string.
213 If the combined string is wide, convert the chars to ints
214 for any individual strings that are not wide. */
216 q = p;
217 for (t = strings; t; t = TREE_CHAIN (t))
219 int len = (TREE_STRING_LENGTH (t)
220 - ((TREE_TYPE (t) == wchar_array_type_node)
221 ? wchar_bytes : 1));
222 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
224 bcopy (TREE_STRING_POINTER (t), q, len);
225 q += len;
227 else
229 int i;
230 for (i = 0; i < len; i++)
232 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
233 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
234 else
235 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
237 q += len * wchar_bytes;
240 if (wide_flag)
242 int i;
243 for (i = 0; i < wchar_bytes; i++)
244 *q++ = 0;
246 else
247 *q = 0;
249 value = make_node (STRING_CST);
250 TREE_STRING_POINTER (value) = p;
251 TREE_STRING_LENGTH (value) = length;
252 TREE_CONSTANT (value) = 1;
254 else
256 value = strings;
257 length = TREE_STRING_LENGTH (value);
258 if (TREE_TYPE (value) == wchar_array_type_node)
259 wide_flag = 1;
262 /* Compute the number of elements, for the array type. */
263 nchars = wide_flag ? length / wchar_bytes : length;
265 /* Create the array type for the string constant.
266 -Wwrite-strings says make the string constant an array of const char
267 so that copying it to a non-const pointer will get a warning. */
268 if (warn_write_strings
269 && (! flag_traditional && ! flag_writable_strings))
271 tree elements
272 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
273 1, 0);
274 TREE_TYPE (value)
275 = build_array_type (elements,
276 build_index_type (build_int_2 (nchars - 1, 0)));
278 else
279 TREE_TYPE (value)
280 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
281 build_index_type (build_int_2 (nchars - 1, 0)));
282 TREE_CONSTANT (value) = 1;
283 TREE_STATIC (value) = 1;
284 return value;
287 /* To speed up processing of attributes, we maintain an array of
288 IDENTIFIER_NODES and the corresponding attribute types. */
290 /* Array to hold attribute information. */
292 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
294 static int attrtab_idx = 0;
296 /* Add an entry to the attribute table above. */
298 static void
299 add_attribute (id, string, min_len, max_len, decl_req)
300 enum attrs id;
301 char *string;
302 int min_len, max_len;
303 int decl_req;
305 char buf[100];
307 attrtab[attrtab_idx].id = id;
308 attrtab[attrtab_idx].name = get_identifier (string);
309 attrtab[attrtab_idx].min = min_len;
310 attrtab[attrtab_idx].max = max_len;
311 attrtab[attrtab_idx++].decl_req = decl_req;
313 sprintf (buf, "__%s__", string);
315 attrtab[attrtab_idx].id = id;
316 attrtab[attrtab_idx].name = get_identifier (buf);
317 attrtab[attrtab_idx].min = min_len;
318 attrtab[attrtab_idx].max = max_len;
319 attrtab[attrtab_idx++].decl_req = decl_req;
322 /* Initialize attribute table. */
324 static void
325 init_attributes ()
327 add_attribute (A_PACKED, "packed", 0, 0, 0);
328 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
329 add_attribute (A_COMMON, "common", 0, 0, 1);
330 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
331 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
332 add_attribute (A_UNUSED, "unused", 0, 0, 0);
333 add_attribute (A_CONST, "const", 0, 0, 1);
334 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
335 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
336 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
337 add_attribute (A_MODE, "mode", 1, 1, 1);
338 add_attribute (A_SECTION, "section", 1, 1, 1);
339 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
340 add_attribute (A_FORMAT, "format", 3, 3, 1);
341 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
342 add_attribute (A_WEAK, "weak", 0, 0, 1);
343 add_attribute (A_ALIAS, "alias", 1, 1, 1);
344 add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
347 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
348 and install them in NODE, which is either a DECL (including a TYPE_DECL)
349 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
350 and declaration modifiers but before the declaration proper. */
352 void
353 decl_attributes (node, attributes, prefix_attributes)
354 tree node, attributes, prefix_attributes;
356 tree decl = 0, type;
357 int is_type;
358 tree a;
360 if (attrtab_idx == 0)
361 init_attributes ();
363 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
365 decl = node;
366 type = TREE_TYPE (decl);
367 is_type = TREE_CODE (node) == TYPE_DECL;
369 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't')
370 type = node, is_type = 1;
372 attributes = chainon (prefix_attributes, attributes);
374 for (a = attributes; a; a = TREE_CHAIN (a))
376 tree name = TREE_PURPOSE (a);
377 tree args = TREE_VALUE (a);
378 int i;
379 enum attrs id;
381 for (i = 0; i < attrtab_idx; i++)
382 if (attrtab[i].name == name)
383 break;
385 if (i == attrtab_idx)
387 if (! valid_machine_attribute (name, args, decl, type))
388 warning ("`%s' attribute directive ignored",
389 IDENTIFIER_POINTER (name));
390 else if (decl != 0)
391 type = TREE_TYPE (decl);
392 continue;
394 else if (attrtab[i].decl_req && decl == 0)
396 warning ("`%s' attribute does not apply to types",
397 IDENTIFIER_POINTER (name));
398 continue;
400 else if (list_length (args) < attrtab[i].min
401 || list_length (args) > attrtab[i].max)
403 error ("wrong number of arguments specified for `%s' attribute",
404 IDENTIFIER_POINTER (name));
405 continue;
408 id = attrtab[i].id;
409 switch (id)
411 case A_PACKED:
412 if (is_type)
413 TYPE_PACKED (type) = 1;
414 else if (TREE_CODE (decl) == FIELD_DECL)
415 DECL_PACKED (decl) = 1;
416 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
417 used for DECL_REGISTER. It wouldn't mean anything anyway. */
418 else
419 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
420 break;
422 case A_NOCOMMON:
423 if (TREE_CODE (decl) == VAR_DECL)
424 DECL_COMMON (decl) = 0;
425 else
426 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
427 break;
429 case A_COMMON:
430 if (TREE_CODE (decl) == VAR_DECL)
431 DECL_COMMON (decl) = 1;
432 else
433 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
434 break;
436 case A_NORETURN:
437 if (TREE_CODE (decl) == FUNCTION_DECL)
438 TREE_THIS_VOLATILE (decl) = 1;
439 else if (TREE_CODE (type) == POINTER_TYPE
440 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
441 TREE_TYPE (decl) = type
442 = build_pointer_type
443 (build_type_variant (TREE_TYPE (type),
444 TREE_READONLY (TREE_TYPE (type)), 1));
445 else
446 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
447 break;
449 case A_UNUSED:
450 if (is_type)
451 TREE_USED (type) = 1;
452 else if (TREE_CODE (decl) == PARM_DECL
453 || TREE_CODE (decl) == VAR_DECL
454 || TREE_CODE (decl) == FUNCTION_DECL)
455 TREE_USED (decl) = 1;
456 else
457 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
458 break;
460 case A_CONST:
461 if (TREE_CODE (decl) == FUNCTION_DECL)
462 TREE_READONLY (decl) = 1;
463 else if (TREE_CODE (type) == POINTER_TYPE
464 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
465 TREE_TYPE (decl) = type
466 = build_pointer_type
467 (build_type_variant (TREE_TYPE (type), 1,
468 TREE_THIS_VOLATILE (TREE_TYPE (type))));
469 else
470 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
471 break;
473 case A_T_UNION:
474 if (is_type
475 && TREE_CODE (type) == UNION_TYPE
476 && (decl == 0
477 || (TYPE_FIELDS (type) != 0
478 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
479 TYPE_TRANSPARENT_UNION (type) = 1;
480 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
481 && TREE_CODE (type) == UNION_TYPE
482 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
483 DECL_TRANSPARENT_UNION (decl) = 1;
484 else
485 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
486 break;
488 case A_CONSTRUCTOR:
489 if (TREE_CODE (decl) == FUNCTION_DECL
490 && TREE_CODE (type) == FUNCTION_TYPE
491 && decl_function_context (decl) == 0)
493 DECL_STATIC_CONSTRUCTOR (decl) = 1;
494 TREE_USED (decl) = 1;
496 else
497 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
498 break;
500 case A_DESTRUCTOR:
501 if (TREE_CODE (decl) == FUNCTION_DECL
502 && TREE_CODE (type) == FUNCTION_TYPE
503 && decl_function_context (decl) == 0)
505 DECL_STATIC_DESTRUCTOR (decl) = 1;
506 TREE_USED (decl) = 1;
508 else
509 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
510 break;
512 case A_MODE:
513 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
514 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
515 else
517 int j;
518 char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
519 int len = strlen (p);
520 enum machine_mode mode = VOIDmode;
521 tree typefm;
523 if (len > 4 && p[0] == '_' && p[1] == '_'
524 && p[len - 1] == '_' && p[len - 2] == '_')
526 char *newp = (char *) alloca (len - 1);
528 strcpy (newp, &p[2]);
529 newp[len - 4] = '\0';
530 p = newp;
533 /* Give this decl a type with the specified mode.
534 First check for the special modes. */
535 if (! strcmp (p, "byte"))
536 mode = byte_mode;
537 else if (!strcmp (p, "word"))
538 mode = word_mode;
539 else if (! strcmp (p, "pointer"))
540 mode = ptr_mode;
541 else
542 for (j = 0; j < NUM_MACHINE_MODES; j++)
543 if (!strcmp (p, GET_MODE_NAME (j)))
544 mode = (enum machine_mode) j;
546 if (mode == VOIDmode)
547 error ("unknown machine mode `%s'", p);
548 else if (0 == (typefm = type_for_mode (mode,
549 TREE_UNSIGNED (type))))
550 error ("no data type for mode `%s'", p);
551 else
553 TREE_TYPE (decl) = type = typefm;
554 DECL_SIZE (decl) = 0;
555 layout_decl (decl, 0);
558 break;
560 case A_SECTION:
561 #ifdef ASM_OUTPUT_SECTION_NAME
562 if ((TREE_CODE (decl) == FUNCTION_DECL
563 || TREE_CODE (decl) == VAR_DECL)
564 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
566 if (TREE_CODE (decl) == VAR_DECL
567 && current_function_decl != NULL_TREE
568 && ! TREE_STATIC (decl))
569 error_with_decl (decl,
570 "section attribute cannot be specified for local variables");
571 /* The decl may have already been given a section attribute from
572 a previous declaration. Ensure they match. */
573 else if (DECL_SECTION_NAME (decl) != NULL_TREE
574 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
575 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
576 error_with_decl (node,
577 "section of `%s' conflicts with previous declaration");
578 else
579 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
581 else
582 error_with_decl (node,
583 "section attribute not allowed for `%s'");
584 #else
585 error_with_decl (node,
586 "section attributes are not supported for this target");
587 #endif
588 break;
590 case A_ALIGNED:
592 tree align_expr
593 = (args ? TREE_VALUE (args)
594 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
595 int align;
597 /* Strip any NOPs of any kind. */
598 while (TREE_CODE (align_expr) == NOP_EXPR
599 || TREE_CODE (align_expr) == CONVERT_EXPR
600 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
601 align_expr = TREE_OPERAND (align_expr, 0);
603 if (TREE_CODE (align_expr) != INTEGER_CST)
605 error ("requested alignment is not a constant");
606 continue;
609 align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
611 if (exact_log2 (align) == -1)
612 error ("requested alignment is not a power of 2");
613 else if (is_type)
614 TYPE_ALIGN (type) = align;
615 else if (TREE_CODE (decl) != VAR_DECL
616 && TREE_CODE (decl) != FIELD_DECL)
617 error_with_decl (decl,
618 "alignment may not be specified for `%s'");
619 else
620 DECL_ALIGN (decl) = align;
622 break;
624 case A_FORMAT:
626 tree format_type_id = TREE_VALUE (args);
627 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
628 tree first_arg_num_expr
629 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
630 int format_num;
631 int first_arg_num;
632 enum format_type format_type;
633 tree argument;
634 int arg_num;
636 if (TREE_CODE (decl) != FUNCTION_DECL)
638 error_with_decl (decl,
639 "argument format specified for non-function `%s'");
640 continue;
643 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
645 error ("unrecognized format specifier");
646 continue;
648 else
650 char *p = IDENTIFIER_POINTER (format_type_id);
652 if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
653 format_type = printf_format_type;
654 else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
655 format_type = scanf_format_type;
656 else if (!strcmp (p, "strftime")
657 || !strcmp (p, "__strftime__"))
658 format_type = strftime_format_type;
659 else
661 error ("`%s' is an unrecognized format function type", p);
662 continue;
666 /* Strip any conversions from the string index and first arg number
667 and verify they are constants. */
668 while (TREE_CODE (format_num_expr) == NOP_EXPR
669 || TREE_CODE (format_num_expr) == CONVERT_EXPR
670 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
671 format_num_expr = TREE_OPERAND (format_num_expr, 0);
673 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
674 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
675 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
676 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
678 if (TREE_CODE (format_num_expr) != INTEGER_CST
679 || TREE_CODE (first_arg_num_expr) != INTEGER_CST)
681 error ("format string has non-constant operand number");
682 continue;
685 format_num = TREE_INT_CST_LOW (format_num_expr);
686 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
687 if (first_arg_num != 0 && first_arg_num <= format_num)
689 error ("format string arg follows the args to be formatted");
690 continue;
693 /* If a parameter list is specified, verify that the format_num
694 argument is actually a string, in case the format attribute
695 is in error. */
696 argument = TYPE_ARG_TYPES (type);
697 if (argument)
699 for (arg_num = 1; ; ++arg_num)
701 if (argument == 0 || arg_num == format_num)
702 break;
703 argument = TREE_CHAIN (argument);
705 if (! argument
706 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
707 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
708 != char_type_node))
710 error ("format string arg not a string type");
711 continue;
713 if (first_arg_num != 0)
715 /* Verify that first_arg_num points to the last arg,
716 the ... */
717 while (argument)
718 arg_num++, argument = TREE_CHAIN (argument);
719 if (arg_num != first_arg_num)
721 error ("args to be formatted is not ...");
722 continue;
727 record_function_format (DECL_NAME (decl),
728 DECL_ASSEMBLER_NAME (decl),
729 format_type, format_num, first_arg_num);
730 break;
733 case A_FORMAT_ARG:
735 tree format_num_expr = TREE_VALUE (args);
736 int format_num, arg_num;
737 tree argument;
739 if (TREE_CODE (decl) != FUNCTION_DECL)
741 error_with_decl (decl,
742 "argument format specified for non-function `%s'");
743 continue;
746 /* Strip any conversions from the first arg number and verify it
747 is a constant. */
748 while (TREE_CODE (format_num_expr) == NOP_EXPR
749 || TREE_CODE (format_num_expr) == CONVERT_EXPR
750 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
751 format_num_expr = TREE_OPERAND (format_num_expr, 0);
753 if (TREE_CODE (format_num_expr) != INTEGER_CST)
755 error ("format string has non-constant operand number");
756 continue;
759 format_num = TREE_INT_CST_LOW (format_num_expr);
761 /* If a parameter list is specified, verify that the format_num
762 argument is actually a string, in case the format attribute
763 is in error. */
764 argument = TYPE_ARG_TYPES (type);
765 if (argument)
767 for (arg_num = 1; ; ++arg_num)
769 if (argument == 0 || arg_num == format_num)
770 break;
771 argument = TREE_CHAIN (argument);
773 if (! argument
774 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
775 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
776 != char_type_node))
778 error ("format string arg not a string type");
779 continue;
783 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
784 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
785 != char_type_node))
787 error ("function does not return string type");
788 continue;
791 record_international_format (DECL_NAME (decl),
792 DECL_ASSEMBLER_NAME (decl),
793 format_num);
794 break;
797 case A_WEAK:
798 declare_weak (decl);
799 break;
801 case A_ALIAS:
802 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
803 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
804 error_with_decl (decl,
805 "`%s' defined both normally and as an alias");
806 else if (decl_function_context (decl) == 0)
808 tree id = get_identifier (TREE_STRING_POINTER
809 (TREE_VALUE (args)));
810 if (TREE_CODE (decl) == FUNCTION_DECL)
811 DECL_INITIAL (decl) = error_mark_node;
812 else
813 DECL_EXTERNAL (decl) = 0;
814 assemble_alias (decl, id);
816 else
817 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
818 break;
820 case A_NO_CHECK_MEMORY_USAGE:
821 if (TREE_CODE (decl) != FUNCTION_DECL)
822 error_with_decl (decl,
823 "`%s' attribute applies only to functions",
824 IDENTIFIER_POINTER (name));
825 else if (DECL_INITIAL (decl))
826 error_with_decl (decl,
827 "can't set `%s' attribute after definition",
828 IDENTIFIER_POINTER (name));
829 else
830 DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
831 break;
836 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
837 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
839 The head of the declspec list is stored in DECLSPECS.
840 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
842 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
843 the list elements. We drop the containing TREE_LIST nodes and link the
844 resulting attributes together the way decl_attributes expects them. */
846 void
847 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
848 tree specs_attrs;
849 tree *declspecs, *prefix_attributes;
851 tree t, s, a, next, specs, attrs;
853 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
854 if (specs_attrs != NULL_TREE
855 && TREE_CODE (specs_attrs) != TREE_LIST)
857 *declspecs = specs_attrs;
858 *prefix_attributes = NULL_TREE;
859 return;
862 /* Remember to keep the lists in the same order, element-wise. */
864 specs = s = NULL_TREE;
865 attrs = a = NULL_TREE;
866 for (t = specs_attrs; t; t = next)
868 next = TREE_CHAIN (t);
869 /* Declspecs have a non-NULL TREE_VALUE. */
870 if (TREE_VALUE (t) != NULL_TREE)
872 if (specs == NULL_TREE)
873 specs = s = t;
874 else
876 TREE_CHAIN (s) = t;
877 s = t;
880 else
882 if (attrs == NULL_TREE)
883 attrs = a = TREE_PURPOSE (t);
884 else
886 TREE_CHAIN (a) = TREE_PURPOSE (t);
887 a = TREE_PURPOSE (t);
889 /* More attrs can be linked here, move A to the end. */
890 while (TREE_CHAIN (a) != NULL_TREE)
891 a = TREE_CHAIN (a);
895 /* Terminate the lists. */
896 if (s != NULL_TREE)
897 TREE_CHAIN (s) = NULL_TREE;
898 if (a != NULL_TREE)
899 TREE_CHAIN (a) = NULL_TREE;
901 /* All done. */
902 *declspecs = specs;
903 *prefix_attributes = attrs;
906 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
907 a parameter list. */
909 #define T_I &integer_type_node
910 #define T_L &long_integer_type_node
911 #define T_LL &long_long_integer_type_node
912 #define T_S &short_integer_type_node
913 #define T_UI &unsigned_type_node
914 #define T_UL &long_unsigned_type_node
915 #define T_ULL &long_long_unsigned_type_node
916 #define T_US &short_unsigned_type_node
917 #define T_F &float_type_node
918 #define T_D &double_type_node
919 #define T_LD &long_double_type_node
920 #define T_C &char_type_node
921 #define T_V &void_type_node
922 #define T_W &wchar_type_node
923 #define T_ST &sizetype
925 typedef struct {
926 char *format_chars;
927 int pointer_count;
928 /* Type of argument if no length modifier is used. */
929 tree *nolen;
930 /* Type of argument if length modifier for shortening is used.
931 If NULL, then this modifier is not allowed. */
932 tree *hlen;
933 /* Type of argument if length modifier `l' is used.
934 If NULL, then this modifier is not allowed. */
935 tree *llen;
936 /* Type of argument if length modifier `q' or `ll' is used.
937 If NULL, then this modifier is not allowed. */
938 tree *qlen;
939 /* Type of argument if length modifier `L' is used.
940 If NULL, then this modifier is not allowed. */
941 tree *bigllen;
942 /* Type of argument if length modifier `Z' is used.
943 If NULL, then this modifier is not allowed. */
944 tree *zlen;
945 /* List of other modifier characters allowed with these options. */
946 char *flag_chars;
947 } format_char_info;
949 static format_char_info print_char_table[] = {
950 { "di", 0, T_I, T_I, T_L, T_LL, T_LL, T_ST, "-wp0 +" },
951 { "oxX", 0, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0#" },
952 { "u", 0, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0" },
953 /* A GNU extension. */
954 { "m", 0, T_V, NULL, NULL, NULL, NULL, NULL, "-wp" },
955 { "feEgGaA", 0, T_D, NULL, NULL, NULL, T_LD, NULL, "-wp0 +#" },
956 { "c", 0, T_I, NULL, T_W, NULL, NULL, NULL, "-w" },
957 { "C", 0, T_W, NULL, NULL, NULL, NULL, NULL, "-w" },
958 { "s", 1, T_C, NULL, T_W, NULL, NULL, NULL, "-wp" },
959 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, "-wp" },
960 { "p", 1, T_V, NULL, NULL, NULL, NULL, NULL, "-w" },
961 { "n", 1, T_I, T_S, T_L, T_LL, NULL, NULL, "" },
962 { NULL }
965 static format_char_info scan_char_table[] = {
966 { "di", 1, T_I, T_S, T_L, T_LL, T_LL, NULL, "*" },
967 { "ouxX", 1, T_UI, T_US, T_UL, T_ULL, T_ULL, NULL, "*" },
968 { "efgEGaA", 1, T_F, NULL, T_D, NULL, T_LD, NULL, "*" },
969 { "sc", 1, T_C, NULL, T_W, NULL, NULL, NULL, "*a" },
970 { "[", 1, T_C, NULL, NULL, NULL, NULL, NULL, "*a" },
971 { "C", 1, T_W, NULL, NULL, NULL, NULL, NULL, "*" },
972 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, "*" },
973 { "p", 2, T_V, NULL, NULL, NULL, NULL, NULL, "*" },
974 { "n", 1, T_I, T_S, T_L, T_LL, NULL, NULL, "" },
975 { NULL }
978 /* Handle format characters recognized by glibc's strftime.c.
979 '2' - MUST do years as only two digits
980 '3' - MAY do years as only two digits (depending on locale)
981 'E' - E modifier is acceptable
982 'O' - O modifier is acceptable to Standard C
983 'o' - O modifier is acceptable as a GNU extension
984 'G' - other GNU extensions */
986 static format_char_info time_char_table[] = {
987 { "y", 0, NULL, NULL, NULL, NULL, NULL, NULL, "2EO-_0w" },
988 { "D", 0, NULL, NULL, NULL, NULL, NULL, NULL, "2" },
989 { "g", 0, NULL, NULL, NULL, NULL, NULL, NULL, "2O-_0w" },
990 { "cx", 0, NULL, NULL, NULL, NULL, NULL, NULL, "3E" },
991 { "%RTXnrt", 0, NULL, NULL, NULL, NULL, NULL, NULL, "" },
992 { "P", 0, NULL, NULL, NULL, NULL, NULL, NULL, "G" },
993 { "HIMSUWdemw", 0, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Ow" },
994 { "Vju", 0, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Oow" },
995 { "Gklsz", 0, NULL, NULL, NULL, NULL, NULL, NULL, "-_0OGw" },
996 { "ABZa", 0, NULL, NULL, NULL, NULL, NULL, NULL, "^#" },
997 { "p", 0, NULL, NULL, NULL, NULL, NULL, NULL, "#" },
998 { "bh", 0, NULL, NULL, NULL, NULL, NULL, NULL, "^" },
999 { "CY", 0, NULL, NULL, NULL, NULL, NULL, NULL, "-_0EOw" },
1000 { NULL }
1003 typedef struct function_format_info
1005 struct function_format_info *next; /* next structure on the list */
1006 tree name; /* identifier such as "printf" */
1007 tree assembler_name; /* optional mangled identifier (for C++) */
1008 enum format_type format_type; /* type of format (printf, scanf, etc.) */
1009 int format_num; /* number of format argument */
1010 int first_arg_num; /* number of first arg (zero for varargs) */
1011 } function_format_info;
1013 static function_format_info *function_format_list = NULL;
1015 typedef struct international_format_info
1017 struct international_format_info *next; /* next structure on the list */
1018 tree name; /* identifier such as "gettext" */
1019 tree assembler_name; /* optional mangled identifier (for C++) */
1020 int format_num; /* number of format argument */
1021 } international_format_info;
1023 static international_format_info *international_format_list = NULL;
1025 static void check_format_info PROTO((function_format_info *, tree));
1027 /* Initialize the table of functions to perform format checking on.
1028 The ANSI functions are always checked (whether <stdio.h> is
1029 included or not), since it is common to call printf without
1030 including <stdio.h>. There shouldn't be a problem with this,
1031 since ANSI reserves these function names whether you include the
1032 header file or not. In any case, the checking is harmless.
1034 Also initialize the name of function that modify the format string for
1035 internationalization purposes. */
1037 void
1038 init_function_format_info ()
1040 record_function_format (get_identifier ("printf"), NULL_TREE,
1041 printf_format_type, 1, 2);
1042 record_function_format (get_identifier ("fprintf"), NULL_TREE,
1043 printf_format_type, 2, 3);
1044 record_function_format (get_identifier ("sprintf"), NULL_TREE,
1045 printf_format_type, 2, 3);
1046 record_function_format (get_identifier ("scanf"), NULL_TREE,
1047 scanf_format_type, 1, 2);
1048 record_function_format (get_identifier ("fscanf"), NULL_TREE,
1049 scanf_format_type, 2, 3);
1050 record_function_format (get_identifier ("sscanf"), NULL_TREE,
1051 scanf_format_type, 2, 3);
1052 record_function_format (get_identifier ("vprintf"), NULL_TREE,
1053 printf_format_type, 1, 0);
1054 record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1055 printf_format_type, 2, 0);
1056 record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1057 printf_format_type, 2, 0);
1058 record_function_format (get_identifier ("strftime"), NULL_TREE,
1059 strftime_format_type, 3, 0);
1061 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1062 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1063 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1066 /* Record information for argument format checking. FUNCTION_IDENT is
1067 the identifier node for the name of the function to check (its decl
1068 need not exist yet).
1069 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
1070 of the argument which is the format control string (starting from 1).
1071 FIRST_ARG_NUM is the number of the first actual argument to check
1072 against the format string, or zero if no checking is not be done
1073 (e.g. for varargs such as vfprintf). */
1075 static void
1076 record_function_format (name, assembler_name, format_type,
1077 format_num, first_arg_num)
1078 tree name;
1079 tree assembler_name;
1080 enum format_type format_type;
1081 int format_num;
1082 int first_arg_num;
1084 function_format_info *info;
1086 /* Re-use existing structure if it's there. */
1088 for (info = function_format_list; info; info = info->next)
1090 if (info->name == name && info->assembler_name == assembler_name)
1091 break;
1093 if (! info)
1095 info = (function_format_info *) xmalloc (sizeof (function_format_info));
1096 info->next = function_format_list;
1097 function_format_list = info;
1099 info->name = name;
1100 info->assembler_name = assembler_name;
1103 info->format_type = format_type;
1104 info->format_num = format_num;
1105 info->first_arg_num = first_arg_num;
1108 /* Record information for the names of function that modify the format
1109 argument to format functions. FUNCTION_IDENT is the identifier node for
1110 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1111 the number of the argument which is the format control string (starting
1112 from 1). */
1114 static void
1115 record_international_format (name, assembler_name, format_num)
1116 tree name;
1117 tree assembler_name;
1118 int format_num;
1120 international_format_info *info;
1122 /* Re-use existing structure if it's there. */
1124 for (info = international_format_list; info; info = info->next)
1126 if (info->name == name && info->assembler_name == assembler_name)
1127 break;
1130 if (! info)
1132 info
1133 = (international_format_info *)
1134 xmalloc (sizeof (international_format_info));
1135 info->next = international_format_list;
1136 international_format_list = info;
1138 info->name = name;
1139 info->assembler_name = assembler_name;
1142 info->format_num = format_num;
1145 static void
1146 tfaff ()
1148 warning ("too few arguments for format");
1151 /* Check the argument list of a call to printf, scanf, etc.
1152 NAME is the function identifier.
1153 ASSEMBLER_NAME is the function's assembler identifier.
1154 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1155 PARAMS is the list of argument values. */
1157 void
1158 check_function_format (name, assembler_name, params)
1159 tree name;
1160 tree assembler_name;
1161 tree params;
1163 function_format_info *info;
1165 /* See if this function is a format function. */
1166 for (info = function_format_list; info; info = info->next)
1168 if (info->assembler_name
1169 ? (info->assembler_name == assembler_name)
1170 : (info->name == name))
1172 /* Yup; check it. */
1173 check_format_info (info, params);
1174 break;
1179 /* Check the argument list of a call to printf, scanf, etc.
1180 INFO points to the function_format_info structure.
1181 PARAMS is the list of argument values. */
1183 static void
1184 check_format_info (info, params)
1185 function_format_info *info;
1186 tree params;
1188 int i;
1189 int arg_num;
1190 int suppressed, wide, precise;
1191 int length_char;
1192 int format_char;
1193 int format_length;
1194 tree format_tree;
1195 tree cur_param;
1196 tree cur_type;
1197 tree wanted_type;
1198 tree first_fillin_param;
1199 char *format_chars;
1200 format_char_info *fci;
1201 char flag_chars[8];
1202 int has_operand_number = 0;
1204 /* Skip to format argument. If the argument isn't available, there's
1205 no work for us to do; prototype checking will catch the problem. */
1206 for (arg_num = 1; ; ++arg_num)
1208 if (params == 0)
1209 return;
1210 if (arg_num == info->format_num)
1211 break;
1212 params = TREE_CHAIN (params);
1214 format_tree = TREE_VALUE (params);
1215 params = TREE_CHAIN (params);
1216 if (format_tree == 0)
1217 return;
1219 /* We can only check the format if it's a string constant. */
1220 while (TREE_CODE (format_tree) == NOP_EXPR)
1221 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
1223 if (TREE_CODE (format_tree) == CALL_EXPR
1224 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1225 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1226 == FUNCTION_DECL))
1228 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1230 /* See if this is a call to a known internationalization function
1231 that modifies the format arg. */
1232 international_format_info *info;
1234 for (info = international_format_list; info; info = info->next)
1235 if (info->assembler_name
1236 ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
1237 : (info->name == DECL_NAME (function)))
1239 tree inner_args;
1240 int i;
1242 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
1243 inner_args != 0;
1244 inner_args = TREE_CHAIN (inner_args), i++)
1245 if (i == info->format_num)
1247 format_tree = TREE_VALUE (inner_args);
1249 while (TREE_CODE (format_tree) == NOP_EXPR)
1250 format_tree = TREE_OPERAND (format_tree, 0);
1255 if (integer_zerop (format_tree))
1257 warning ("null format string");
1258 return;
1260 if (TREE_CODE (format_tree) != ADDR_EXPR)
1261 return;
1262 format_tree = TREE_OPERAND (format_tree, 0);
1263 if (TREE_CODE (format_tree) != STRING_CST)
1264 return;
1265 format_chars = TREE_STRING_POINTER (format_tree);
1266 format_length = TREE_STRING_LENGTH (format_tree);
1267 if (format_length <= 1)
1268 warning ("zero-length format string");
1269 if (format_chars[--format_length] != 0)
1271 warning ("unterminated format string");
1272 return;
1274 /* Skip to first argument to check. */
1275 while (arg_num + 1 < info->first_arg_num)
1277 if (params == 0)
1278 return;
1279 params = TREE_CHAIN (params);
1280 ++arg_num;
1283 first_fillin_param = params;
1284 while (1)
1286 int aflag;
1287 if (*format_chars == 0)
1289 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
1290 warning ("embedded `\\0' in format");
1291 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
1292 warning ("too many arguments for format");
1293 return;
1295 if (*format_chars++ != '%')
1296 continue;
1297 if (*format_chars == 0)
1299 warning ("spurious trailing `%%' in format");
1300 continue;
1302 if (*format_chars == '%')
1304 ++format_chars;
1305 continue;
1307 flag_chars[0] = 0;
1308 suppressed = wide = precise = FALSE;
1309 if (info->format_type == scanf_format_type)
1311 suppressed = *format_chars == '*';
1312 if (suppressed)
1313 ++format_chars;
1314 while (is_C_digit (*format_chars))
1315 ++format_chars;
1317 else if (info->format_type == strftime_format_type)
1319 while (*format_chars != 0 && index ("_-0^#", *format_chars) != 0)
1321 if (pedantic)
1322 warning ("ANSI C does not support the strftime `%c' flag",
1323 *format_chars);
1324 if (index (flag_chars, *format_chars) != 0)
1326 warning ("repeated `%c' flag in format",
1327 *format_chars);
1328 ++format_chars;
1330 else
1332 i = strlen (flag_chars);
1333 flag_chars[i++] = *format_chars++;
1334 flag_chars[i] = 0;
1337 while (is_C_digit ((unsigned char) *format_chars))
1339 wide = TRUE;
1340 ++format_chars;
1342 if (wide && pedantic)
1343 warning ("ANSI C does not support strftime format width");
1344 if (*format_chars == 'E' || *format_chars == 'O')
1346 i = strlen (flag_chars);
1347 flag_chars[i++] = *format_chars++;
1348 flag_chars[i] = 0;
1349 if (*format_chars == 'E' || *format_chars == 'O')
1351 warning ("multiple E/O modifiers in format");
1352 while (*format_chars == 'E' || *format_chars == 'O')
1353 ++format_chars;
1357 else if (info->format_type == printf_format_type)
1359 /* See if we have a number followed by a dollar sign. If we do,
1360 it is an operand number, so set PARAMS to that operand. */
1361 if (*format_chars >= '0' && *format_chars <= '9')
1363 char *p = format_chars;
1365 while (*p >= '0' && *p++ <= '9')
1368 if (*p == '$')
1370 int opnum = atoi (format_chars);
1372 params = first_fillin_param;
1373 format_chars = p + 1;
1374 has_operand_number = 1;
1376 for (i = 1; i < opnum && params != 0; i++)
1377 params = TREE_CHAIN (params);
1379 if (opnum == 0 || params == 0)
1381 warning ("operand number out of range in format");
1382 return;
1387 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
1389 if (index (flag_chars, *format_chars) != 0)
1390 warning ("repeated `%c' flag in format", *format_chars++);
1391 else
1393 i = strlen (flag_chars);
1394 flag_chars[i++] = *format_chars++;
1395 flag_chars[i] = 0;
1398 /* "If the space and + flags both appear,
1399 the space flag will be ignored." */
1400 if (index (flag_chars, ' ') != 0
1401 && index (flag_chars, '+') != 0)
1402 warning ("use of both ` ' and `+' flags in format");
1403 /* "If the 0 and - flags both appear,
1404 the 0 flag will be ignored." */
1405 if (index (flag_chars, '0') != 0
1406 && index (flag_chars, '-') != 0)
1407 warning ("use of both `0' and `-' flags in format");
1408 if (*format_chars == '*')
1410 wide = TRUE;
1411 /* "...a field width...may be indicated by an asterisk.
1412 In this case, an int argument supplies the field width..." */
1413 ++format_chars;
1414 if (params == 0)
1416 tfaff ();
1417 return;
1419 if (info->first_arg_num != 0)
1421 cur_param = TREE_VALUE (params);
1422 params = TREE_CHAIN (params);
1423 ++arg_num;
1424 /* size_t is generally not valid here.
1425 It will work on most machines, because size_t and int
1426 have the same mode. But might as well warn anyway,
1427 since it will fail on other machines. */
1428 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1429 != integer_type_node)
1431 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1432 != unsigned_type_node))
1433 warning ("field width is not type int (arg %d)", arg_num);
1436 else
1438 while (is_C_digit (*format_chars))
1440 wide = TRUE;
1441 ++format_chars;
1444 if (*format_chars == '.')
1446 precise = TRUE;
1447 ++format_chars;
1448 if (*format_chars != '*' && !is_C_digit (*format_chars))
1449 warning ("`.' not followed by `*' or digit in format");
1450 /* "...a...precision...may be indicated by an asterisk.
1451 In this case, an int argument supplies the...precision." */
1452 if (*format_chars == '*')
1454 if (info->first_arg_num != 0)
1456 ++format_chars;
1457 if (params == 0)
1459 tfaff ();
1460 return;
1462 cur_param = TREE_VALUE (params);
1463 params = TREE_CHAIN (params);
1464 ++arg_num;
1465 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1466 != integer_type_node)
1467 warning ("field width is not type int (arg %d)",
1468 arg_num);
1471 else
1473 while (is_C_digit (*format_chars))
1474 ++format_chars;
1479 aflag = 0;
1481 if (info->format_type != strftime_format_type)
1483 if (*format_chars == 'h' || *format_chars == 'l')
1484 length_char = *format_chars++;
1485 else if (*format_chars == 'q' || *format_chars == 'L')
1487 length_char = *format_chars++;
1488 if (pedantic)
1489 warning ("ANSI C does not support the `%c' length modifier",
1490 length_char);
1492 else if (*format_chars == 'Z')
1494 length_char = *format_chars++;
1495 if (pedantic)
1496 warning ("ANSI C does not support the `Z' length modifier");
1498 else
1499 length_char = 0;
1500 if (length_char == 'l' && *format_chars == 'l')
1502 length_char = 'q', format_chars++;
1503 if (pedantic)
1504 warning ("ANSI C does not support the `ll' length modifier");
1506 if (*format_chars == 'a' && info->format_type == scanf_format_type)
1508 if (format_chars[1] == 's' || format_chars[1] == 'S'
1509 || format_chars[1] == '[')
1511 /* `a' is used as a flag. */
1512 aflag = 1;
1513 format_chars++;
1516 if (suppressed && length_char != 0)
1517 warning ("use of `*' and `%c' together in format", length_char);
1519 format_char = *format_chars;
1520 if (format_char == 0
1521 || (info->format_type != strftime_format_type && format_char == '%'))
1523 warning ("conversion lacks type at end of format");
1524 continue;
1526 format_chars++;
1527 switch (info->format_type)
1529 case printf_format_type:
1530 fci = print_char_table;
1531 break;
1532 case scanf_format_type:
1533 fci = scan_char_table;
1534 break;
1535 case strftime_format_type:
1536 fci = time_char_table;
1537 break;
1538 default:
1539 abort ();
1541 while (fci->format_chars != 0
1542 && index (fci->format_chars, format_char) == 0)
1543 ++fci;
1544 if (fci->format_chars == 0)
1546 if (format_char >= 040 && format_char < 0177)
1547 warning ("unknown conversion type character `%c' in format",
1548 format_char);
1549 else
1550 warning ("unknown conversion type character 0x%x in format",
1551 format_char);
1552 continue;
1554 if (pedantic)
1556 if (index (fci->flag_chars, 'G') != 0)
1557 warning ("ANSI C does not support `%%%c'", format_char);
1558 if (index (fci->flag_chars, 'o') != 0
1559 && index (flag_chars, 'O') != 0)
1560 warning ("ANSI C does not support `%%O%c'", format_char);
1562 if (wide && index (fci->flag_chars, 'w') == 0)
1563 warning ("width used with `%c' format", format_char);
1564 if (index (fci->flag_chars, '2') != 0)
1565 warning ("`%%%c' yields only last 2 digits of year", format_char);
1566 else if (index (fci->flag_chars, '3') != 0)
1567 warning ("`%%%c' yields only last 2 digits of year in some locales",
1568 format_char);
1569 if (precise && index (fci->flag_chars, 'p') == 0)
1570 warning ("precision used with `%c' format", format_char);
1571 if (aflag && index (fci->flag_chars, 'a') == 0)
1573 warning ("`a' flag used with `%c' format", format_char);
1574 /* To simplify the following code. */
1575 aflag = 0;
1577 if (info->format_type == scanf_format_type && format_char == '[')
1579 /* Skip over scan set, in case it happens to have '%' in it. */
1580 if (*format_chars == '^')
1581 ++format_chars;
1582 /* Find closing bracket; if one is hit immediately, then
1583 it's part of the scan set rather than a terminator. */
1584 if (*format_chars == ']')
1585 ++format_chars;
1586 while (*format_chars && *format_chars != ']')
1587 ++format_chars;
1588 if (*format_chars != ']')
1589 /* The end of the format string was reached. */
1590 warning ("no closing `]' for `%%[' format");
1592 if (suppressed)
1594 if (index (fci->flag_chars, '*') == 0)
1595 warning ("suppression of `%c' conversion in format", format_char);
1596 continue;
1598 for (i = 0; flag_chars[i] != 0; ++i)
1600 if (index (fci->flag_chars, flag_chars[i]) == 0)
1601 warning ("flag `%c' used with type `%c'",
1602 flag_chars[i], format_char);
1604 if (info->format_type == strftime_format_type)
1605 continue;
1606 if (precise && index (flag_chars, '0') != 0
1607 && (format_char == 'd' || format_char == 'i'
1608 || format_char == 'o' || format_char == 'u'
1609 || format_char == 'x' || format_char == 'x'))
1610 warning ("`0' flag ignored with precision specifier and `%c' format",
1611 format_char);
1612 switch (length_char)
1614 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
1615 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1616 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
1617 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1618 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
1619 case 'Z': wanted_type = fci->zlen ? *fci->zlen : 0; break;
1621 if (wanted_type == 0)
1622 warning ("use of `%c' length character with `%c' type character",
1623 length_char, format_char);
1626 ** XXX -- should kvetch about stuff such as
1627 ** {
1628 ** const int i;
1630 ** scanf ("%d", &i);
1631 ** }
1634 /* Finally. . .check type of argument against desired type! */
1635 if (info->first_arg_num == 0)
1636 continue;
1637 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1638 /* This specifier takes no argument. */
1639 continue;
1640 if (params == 0)
1642 tfaff ();
1643 return;
1645 cur_param = TREE_VALUE (params);
1646 params = TREE_CHAIN (params);
1647 ++arg_num;
1648 cur_type = TREE_TYPE (cur_param);
1650 STRIP_NOPS (cur_param);
1652 /* Check the types of any additional pointer arguments
1653 that precede the "real" argument. */
1654 for (i = 0; i < fci->pointer_count + aflag; ++i)
1656 if (TREE_CODE (cur_type) == POINTER_TYPE)
1658 cur_type = TREE_TYPE (cur_type);
1660 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
1661 cur_param = TREE_OPERAND (cur_param, 0);
1662 else
1663 cur_param = 0;
1665 continue;
1667 if (TREE_CODE (cur_type) != ERROR_MARK)
1668 warning ((fci->pointer_count + aflag == 1
1669 ? "format argument is not a pointer (arg %d)"
1670 : "format argument is not a pointer to a pointer (arg %d)"),
1671 arg_num);
1672 break;
1675 /* See if this is an attempt to write into a const type with
1676 scanf. */
1677 if (info->format_type == scanf_format_type
1678 && i == fci->pointer_count + aflag
1679 && wanted_type != 0
1680 && TREE_CODE (cur_type) != ERROR_MARK
1681 && (TYPE_READONLY (cur_type)
1682 || (cur_param != 0
1683 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
1684 || (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'd'
1685 && TREE_READONLY (cur_param))))))
1686 warning ("writing into constant object (arg %d)", arg_num);
1688 /* Check the type of the "real" argument, if there's a type we want. */
1689 if (i == fci->pointer_count + aflag && wanted_type != 0
1690 && TREE_CODE (cur_type) != ERROR_MARK
1691 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1692 /* If we want `void *', allow any pointer type.
1693 (Anything else would already have got a warning.) */
1694 && ! (wanted_type == void_type_node
1695 && fci->pointer_count > 0)
1696 /* Don't warn about differences merely in signedness. */
1697 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
1698 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
1699 && (TREE_UNSIGNED (wanted_type)
1700 ? wanted_type == (cur_type = unsigned_type (cur_type))
1701 : wanted_type == (cur_type = signed_type (cur_type))))
1702 /* Likewise, "signed char", "unsigned char" and "char" are
1703 equivalent but the above test won't consider them equivalent. */
1704 && ! (wanted_type == char_type_node
1705 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
1706 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
1708 register char *this;
1709 register char *that;
1711 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
1712 that = 0;
1713 if (TREE_CODE (cur_type) != ERROR_MARK
1714 && TYPE_NAME (cur_type) != 0
1715 && TREE_CODE (cur_type) != INTEGER_TYPE
1716 && !(TREE_CODE (cur_type) == POINTER_TYPE
1717 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
1719 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
1720 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
1721 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1722 else
1723 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
1726 /* A nameless type can't possibly match what the format wants.
1727 So there will be a warning for it.
1728 Make up a string to describe vaguely what it is. */
1729 if (that == 0)
1731 if (TREE_CODE (cur_type) == POINTER_TYPE)
1732 that = "pointer";
1733 else
1734 that = "different type";
1737 /* Make the warning better in case of mismatch of int vs long. */
1738 if (TREE_CODE (cur_type) == INTEGER_TYPE
1739 && TREE_CODE (wanted_type) == INTEGER_TYPE
1740 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
1741 && TYPE_NAME (cur_type) != 0
1742 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
1743 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1745 if (strcmp (this, that) != 0)
1746 warning ("%s format, %s arg (arg %d)", this, that, arg_num);
1751 /* Print a warning if a constant expression had overflow in folding.
1752 Invoke this function on every expression that the language
1753 requires to be a constant expression.
1754 Note the ANSI C standard says it is erroneous for a
1755 constant expression to overflow. */
1757 void
1758 constant_expression_warning (value)
1759 tree value;
1761 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1762 || TREE_CODE (value) == COMPLEX_CST)
1763 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1764 pedwarn ("overflow in constant expression");
1767 /* Print a warning if an expression had overflow in folding.
1768 Invoke this function on every expression that
1769 (1) appears in the source code, and
1770 (2) might be a constant expression that overflowed, and
1771 (3) is not already checked by convert_and_check;
1772 however, do not invoke this function on operands of explicit casts. */
1774 void
1775 overflow_warning (value)
1776 tree value;
1778 if ((TREE_CODE (value) == INTEGER_CST
1779 || (TREE_CODE (value) == COMPLEX_CST
1780 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1781 && TREE_OVERFLOW (value))
1783 TREE_OVERFLOW (value) = 0;
1784 if (skip_evaluation == 0)
1785 warning ("integer overflow in expression");
1787 else if ((TREE_CODE (value) == REAL_CST
1788 || (TREE_CODE (value) == COMPLEX_CST
1789 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1790 && TREE_OVERFLOW (value))
1792 TREE_OVERFLOW (value) = 0;
1793 if (skip_evaluation == 0)
1794 warning ("floating point overflow in expression");
1798 /* Print a warning if a large constant is truncated to unsigned,
1799 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1800 Invoke this function on every expression that might be implicitly
1801 converted to an unsigned type. */
1803 void
1804 unsigned_conversion_warning (result, operand)
1805 tree result, operand;
1807 if (TREE_CODE (operand) == INTEGER_CST
1808 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
1809 && TREE_UNSIGNED (TREE_TYPE (result))
1810 && skip_evaluation == 0
1811 && !int_fits_type_p (operand, TREE_TYPE (result)))
1813 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
1814 /* This detects cases like converting -129 or 256 to unsigned char. */
1815 warning ("large integer implicitly truncated to unsigned type");
1816 else if (warn_conversion)
1817 warning ("negative integer implicitly converted to unsigned type");
1821 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1822 Invoke this function on every expression that is converted implicitly,
1823 i.e. because of language rules and not because of an explicit cast. */
1825 tree
1826 convert_and_check (type, expr)
1827 tree type, expr;
1829 tree t = convert (type, expr);
1830 if (TREE_CODE (t) == INTEGER_CST)
1832 if (TREE_OVERFLOW (t))
1834 TREE_OVERFLOW (t) = 0;
1836 /* Do not diagnose overflow in a constant expression merely
1837 because a conversion overflowed. */
1838 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
1840 /* No warning for converting 0x80000000 to int. */
1841 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
1842 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1843 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
1844 /* If EXPR fits in the unsigned version of TYPE,
1845 don't warn unless pedantic. */
1846 if ((pedantic
1847 || TREE_UNSIGNED (type)
1848 || ! int_fits_type_p (expr, unsigned_type (type)))
1849 && skip_evaluation == 0)
1850 warning ("overflow in implicit constant conversion");
1852 else
1853 unsigned_conversion_warning (t, expr);
1855 return t;
1858 void
1859 c_expand_expr_stmt (expr)
1860 tree expr;
1862 /* Do default conversion if safe and possibly important,
1863 in case within ({...}). */
1864 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
1865 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1866 expr = default_conversion (expr);
1868 if (TREE_TYPE (expr) != error_mark_node
1869 && TYPE_SIZE (TREE_TYPE (expr)) == 0
1870 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1871 error ("expression statement has incomplete type");
1873 expand_expr_stmt (expr);
1876 /* Validate the expression after `case' and apply default promotions. */
1878 tree
1879 check_case_value (value)
1880 tree value;
1882 if (value == NULL_TREE)
1883 return value;
1885 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1886 STRIP_TYPE_NOPS (value);
1888 if (TREE_CODE (value) != INTEGER_CST
1889 && value != error_mark_node)
1891 error ("case label does not reduce to an integer constant");
1892 value = error_mark_node;
1894 else
1895 /* Promote char or short to int. */
1896 value = default_conversion (value);
1898 constant_expression_warning (value);
1900 return value;
1903 /* Return an integer type with BITS bits of precision,
1904 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1906 tree
1907 type_for_size (bits, unsignedp)
1908 unsigned bits;
1909 int unsignedp;
1911 if (bits == TYPE_PRECISION (integer_type_node))
1912 return unsignedp ? unsigned_type_node : integer_type_node;
1914 if (bits == TYPE_PRECISION (signed_char_type_node))
1915 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1917 if (bits == TYPE_PRECISION (short_integer_type_node))
1918 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1920 if (bits == TYPE_PRECISION (long_integer_type_node))
1921 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1923 if (bits == TYPE_PRECISION (long_long_integer_type_node))
1924 return (unsignedp ? long_long_unsigned_type_node
1925 : long_long_integer_type_node);
1927 if (bits <= TYPE_PRECISION (intQI_type_node))
1928 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1930 if (bits <= TYPE_PRECISION (intHI_type_node))
1931 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1933 if (bits <= TYPE_PRECISION (intSI_type_node))
1934 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1936 if (bits <= TYPE_PRECISION (intDI_type_node))
1937 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1939 return 0;
1942 /* Return a data type that has machine mode MODE.
1943 If the mode is an integer,
1944 then UNSIGNEDP selects between signed and unsigned types. */
1946 tree
1947 type_for_mode (mode, unsignedp)
1948 enum machine_mode mode;
1949 int unsignedp;
1951 if (mode == TYPE_MODE (integer_type_node))
1952 return unsignedp ? unsigned_type_node : integer_type_node;
1954 if (mode == TYPE_MODE (signed_char_type_node))
1955 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1957 if (mode == TYPE_MODE (short_integer_type_node))
1958 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1960 if (mode == TYPE_MODE (long_integer_type_node))
1961 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1963 if (mode == TYPE_MODE (long_long_integer_type_node))
1964 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
1966 if (mode == TYPE_MODE (intQI_type_node))
1967 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1969 if (mode == TYPE_MODE (intHI_type_node))
1970 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1972 if (mode == TYPE_MODE (intSI_type_node))
1973 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1975 if (mode == TYPE_MODE (intDI_type_node))
1976 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1978 if (mode == TYPE_MODE (float_type_node))
1979 return float_type_node;
1981 if (mode == TYPE_MODE (double_type_node))
1982 return double_type_node;
1984 if (mode == TYPE_MODE (long_double_type_node))
1985 return long_double_type_node;
1987 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
1988 return build_pointer_type (char_type_node);
1990 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
1991 return build_pointer_type (integer_type_node);
1993 return 0;
1996 /* Return the minimum number of bits needed to represent VALUE in a
1997 signed or unsigned type, UNSIGNEDP says which. */
2000 min_precision (value, unsignedp)
2001 tree value;
2002 int unsignedp;
2004 int log;
2006 /* If the value is negative, compute its negative minus 1. The latter
2007 adjustment is because the absolute value of the largest negative value
2008 is one larger than the largest positive value. This is equivalent to
2009 a bit-wise negation, so use that operation instead. */
2011 if (tree_int_cst_sgn (value) < 0)
2012 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2014 /* Return the number of bits needed, taking into account the fact
2015 that we need one more bit for a signed than unsigned type. */
2017 if (integer_zerop (value))
2018 log = 0;
2019 else if (TREE_INT_CST_HIGH (value) != 0)
2020 log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
2021 else
2022 log = floor_log2 (TREE_INT_CST_LOW (value));
2024 return log + 1 + ! unsignedp;
2027 /* Print an error message for invalid operands to arith operation CODE.
2028 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2030 void
2031 binary_op_error (code)
2032 enum tree_code code;
2034 register char *opname;
2036 switch (code)
2038 case NOP_EXPR:
2039 error ("invalid truth-value expression");
2040 return;
2042 case PLUS_EXPR:
2043 opname = "+"; break;
2044 case MINUS_EXPR:
2045 opname = "-"; break;
2046 case MULT_EXPR:
2047 opname = "*"; break;
2048 case MAX_EXPR:
2049 opname = "max"; break;
2050 case MIN_EXPR:
2051 opname = "min"; break;
2052 case EQ_EXPR:
2053 opname = "=="; break;
2054 case NE_EXPR:
2055 opname = "!="; break;
2056 case LE_EXPR:
2057 opname = "<="; break;
2058 case GE_EXPR:
2059 opname = ">="; break;
2060 case LT_EXPR:
2061 opname = "<"; break;
2062 case GT_EXPR:
2063 opname = ">"; break;
2064 case LSHIFT_EXPR:
2065 opname = "<<"; break;
2066 case RSHIFT_EXPR:
2067 opname = ">>"; break;
2068 case TRUNC_MOD_EXPR:
2069 case FLOOR_MOD_EXPR:
2070 opname = "%"; break;
2071 case TRUNC_DIV_EXPR:
2072 case FLOOR_DIV_EXPR:
2073 opname = "/"; break;
2074 case BIT_AND_EXPR:
2075 opname = "&"; break;
2076 case BIT_IOR_EXPR:
2077 opname = "|"; break;
2078 case TRUTH_ANDIF_EXPR:
2079 opname = "&&"; break;
2080 case TRUTH_ORIF_EXPR:
2081 opname = "||"; break;
2082 case BIT_XOR_EXPR:
2083 opname = "^"; break;
2084 case LROTATE_EXPR:
2085 case RROTATE_EXPR:
2086 opname = "rotate"; break;
2087 default:
2088 opname = "unknown"; break;
2090 error ("invalid operands to binary %s", opname);
2093 /* Subroutine of build_binary_op, used for comparison operations.
2094 See if the operands have both been converted from subword integer types
2095 and, if so, perhaps change them both back to their original type.
2096 This function is also responsible for converting the two operands
2097 to the proper common type for comparison.
2099 The arguments of this function are all pointers to local variables
2100 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2101 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2103 If this function returns nonzero, it means that the comparison has
2104 a constant value. What this function returns is an expression for
2105 that value. */
2107 tree
2108 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2109 tree *op0_ptr, *op1_ptr;
2110 tree *restype_ptr;
2111 enum tree_code *rescode_ptr;
2113 register tree type;
2114 tree op0 = *op0_ptr;
2115 tree op1 = *op1_ptr;
2116 int unsignedp0, unsignedp1;
2117 int real1, real2;
2118 tree primop0, primop1;
2119 enum tree_code code = *rescode_ptr;
2121 /* Throw away any conversions to wider types
2122 already present in the operands. */
2124 primop0 = get_narrower (op0, &unsignedp0);
2125 primop1 = get_narrower (op1, &unsignedp1);
2127 /* Handle the case that OP0 does not *contain* a conversion
2128 but it *requires* conversion to FINAL_TYPE. */
2130 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2131 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2132 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2133 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2135 /* If one of the operands must be floated, we cannot optimize. */
2136 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2137 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2139 /* If first arg is constant, swap the args (changing operation
2140 so value is preserved), for canonicalization. Don't do this if
2141 the second arg is 0. */
2143 if (TREE_CONSTANT (primop0)
2144 && ! integer_zerop (primop1) && ! real_zerop (primop1))
2146 register tree tem = primop0;
2147 register int temi = unsignedp0;
2148 primop0 = primop1;
2149 primop1 = tem;
2150 tem = op0;
2151 op0 = op1;
2152 op1 = tem;
2153 *op0_ptr = op0;
2154 *op1_ptr = op1;
2155 unsignedp0 = unsignedp1;
2156 unsignedp1 = temi;
2157 temi = real1;
2158 real1 = real2;
2159 real2 = temi;
2161 switch (code)
2163 case LT_EXPR:
2164 code = GT_EXPR;
2165 break;
2166 case GT_EXPR:
2167 code = LT_EXPR;
2168 break;
2169 case LE_EXPR:
2170 code = GE_EXPR;
2171 break;
2172 case GE_EXPR:
2173 code = LE_EXPR;
2174 break;
2175 default:
2176 break;
2178 *rescode_ptr = code;
2181 /* If comparing an integer against a constant more bits wide,
2182 maybe we can deduce a value of 1 or 0 independent of the data.
2183 Or else truncate the constant now
2184 rather than extend the variable at run time.
2186 This is only interesting if the constant is the wider arg.
2187 Also, it is not safe if the constant is unsigned and the
2188 variable arg is signed, since in this case the variable
2189 would be sign-extended and then regarded as unsigned.
2190 Our technique fails in this case because the lowest/highest
2191 possible unsigned results don't follow naturally from the
2192 lowest/highest possible values of the variable operand.
2193 For just EQ_EXPR and NE_EXPR there is another technique that
2194 could be used: see if the constant can be faithfully represented
2195 in the other operand's type, by truncating it and reextending it
2196 and see if that preserves the constant's value. */
2198 if (!real1 && !real2
2199 && TREE_CODE (primop1) == INTEGER_CST
2200 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2202 int min_gt, max_gt, min_lt, max_lt;
2203 tree maxval, minval;
2204 /* 1 if comparison is nominally unsigned. */
2205 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2206 tree val;
2208 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2210 maxval = TYPE_MAX_VALUE (type);
2211 minval = TYPE_MIN_VALUE (type);
2213 if (unsignedp && !unsignedp0)
2214 *restype_ptr = signed_type (*restype_ptr);
2216 if (TREE_TYPE (primop1) != *restype_ptr)
2217 primop1 = convert (*restype_ptr, primop1);
2218 if (type != *restype_ptr)
2220 minval = convert (*restype_ptr, minval);
2221 maxval = convert (*restype_ptr, maxval);
2224 if (unsignedp && unsignedp0)
2226 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2227 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2228 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2229 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2231 else
2233 min_gt = INT_CST_LT (primop1, minval);
2234 max_gt = INT_CST_LT (primop1, maxval);
2235 min_lt = INT_CST_LT (minval, primop1);
2236 max_lt = INT_CST_LT (maxval, primop1);
2239 val = 0;
2240 /* This used to be a switch, but Genix compiler can't handle that. */
2241 if (code == NE_EXPR)
2243 if (max_lt || min_gt)
2244 val = boolean_true_node;
2246 else if (code == EQ_EXPR)
2248 if (max_lt || min_gt)
2249 val = boolean_false_node;
2251 else if (code == LT_EXPR)
2253 if (max_lt)
2254 val = boolean_true_node;
2255 if (!min_lt)
2256 val = boolean_false_node;
2258 else if (code == GT_EXPR)
2260 if (min_gt)
2261 val = boolean_true_node;
2262 if (!max_gt)
2263 val = boolean_false_node;
2265 else if (code == LE_EXPR)
2267 if (!max_gt)
2268 val = boolean_true_node;
2269 if (min_gt)
2270 val = boolean_false_node;
2272 else if (code == GE_EXPR)
2274 if (!min_lt)
2275 val = boolean_true_node;
2276 if (max_lt)
2277 val = boolean_false_node;
2280 /* If primop0 was sign-extended and unsigned comparison specd,
2281 we did a signed comparison above using the signed type bounds.
2282 But the comparison we output must be unsigned.
2284 Also, for inequalities, VAL is no good; but if the signed
2285 comparison had *any* fixed result, it follows that the
2286 unsigned comparison just tests the sign in reverse
2287 (positive values are LE, negative ones GE).
2288 So we can generate an unsigned comparison
2289 against an extreme value of the signed type. */
2291 if (unsignedp && !unsignedp0)
2293 if (val != 0)
2294 switch (code)
2296 case LT_EXPR:
2297 case GE_EXPR:
2298 primop1 = TYPE_MIN_VALUE (type);
2299 val = 0;
2300 break;
2302 case LE_EXPR:
2303 case GT_EXPR:
2304 primop1 = TYPE_MAX_VALUE (type);
2305 val = 0;
2306 break;
2308 default:
2309 break;
2311 type = unsigned_type (type);
2314 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2316 /* This is the case of (char)x >?< 0x80, which people used to use
2317 expecting old C compilers to change the 0x80 into -0x80. */
2318 if (val == boolean_false_node)
2319 warning ("comparison is always 0 due to limited range of data type");
2320 if (val == boolean_true_node)
2321 warning ("comparison is always 1 due to limited range of data type");
2324 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2326 /* This is the case of (unsigned char)x >?< -1 or < 0. */
2327 if (val == boolean_false_node)
2328 warning ("comparison is always 0 due to limited range of data type");
2329 if (val == boolean_true_node)
2330 warning ("comparison is always 1 due to limited range of data type");
2333 if (val != 0)
2335 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2336 if (TREE_SIDE_EFFECTS (primop0))
2337 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2338 return val;
2341 /* Value is not predetermined, but do the comparison
2342 in the type of the operand that is not constant.
2343 TYPE is already properly set. */
2345 else if (real1 && real2
2346 && (TYPE_PRECISION (TREE_TYPE (primop0))
2347 == TYPE_PRECISION (TREE_TYPE (primop1))))
2348 type = TREE_TYPE (primop0);
2350 /* If args' natural types are both narrower than nominal type
2351 and both extend in the same manner, compare them
2352 in the type of the wider arg.
2353 Otherwise must actually extend both to the nominal
2354 common type lest different ways of extending
2355 alter the result.
2356 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2358 else if (unsignedp0 == unsignedp1 && real1 == real2
2359 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2360 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2362 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2363 type = signed_or_unsigned_type (unsignedp0
2364 || TREE_UNSIGNED (*restype_ptr),
2365 type);
2366 /* Make sure shorter operand is extended the right way
2367 to match the longer operand. */
2368 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2369 primop0);
2370 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2371 primop1);
2373 else
2375 /* Here we must do the comparison on the nominal type
2376 using the args exactly as we received them. */
2377 type = *restype_ptr;
2378 primop0 = op0;
2379 primop1 = op1;
2381 if (!real1 && !real2 && integer_zerop (primop1)
2382 && TREE_UNSIGNED (*restype_ptr))
2384 tree value = 0;
2385 switch (code)
2387 case GE_EXPR:
2388 /* All unsigned values are >= 0, so we warn if extra warnings
2389 are requested. However, if OP0 is a constant that is
2390 >= 0, the signedness of the comparison isn't an issue,
2391 so suppress the warning. */
2392 if (extra_warnings
2393 && ! (TREE_CODE (primop0) == INTEGER_CST
2394 && ! TREE_OVERFLOW (convert (signed_type (type),
2395 primop0))))
2396 warning ("unsigned value >= 0 is always 1");
2397 value = boolean_true_node;
2398 break;
2400 case LT_EXPR:
2401 if (extra_warnings
2402 && ! (TREE_CODE (primop0) == INTEGER_CST
2403 && ! TREE_OVERFLOW (convert (signed_type (type),
2404 primop0))))
2405 warning ("unsigned value < 0 is always 0");
2406 value = boolean_false_node;
2407 break;
2409 default:
2410 break;
2413 if (value != 0)
2415 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2416 if (TREE_SIDE_EFFECTS (primop0))
2417 return build (COMPOUND_EXPR, TREE_TYPE (value),
2418 primop0, value);
2419 return value;
2424 *op0_ptr = convert (type, primop0);
2425 *op1_ptr = convert (type, primop1);
2427 *restype_ptr = boolean_type_node;
2429 return 0;
2432 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2433 or validate its data type for an `if' or `while' statement or ?..: exp.
2435 This preparation consists of taking the ordinary
2436 representation of an expression expr and producing a valid tree
2437 boolean expression describing whether expr is nonzero. We could
2438 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2439 but we optimize comparisons, &&, ||, and !.
2441 The resulting type should always be `boolean_type_node'. */
2443 tree
2444 truthvalue_conversion (expr)
2445 tree expr;
2447 if (TREE_CODE (expr) == ERROR_MARK)
2448 return expr;
2450 #if 0 /* This appears to be wrong for C++. */
2451 /* These really should return error_mark_node after 2.4 is stable.
2452 But not all callers handle ERROR_MARK properly. */
2453 switch (TREE_CODE (TREE_TYPE (expr)))
2455 case RECORD_TYPE:
2456 error ("struct type value used where scalar is required");
2457 return boolean_false_node;
2459 case UNION_TYPE:
2460 error ("union type value used where scalar is required");
2461 return boolean_false_node;
2463 case ARRAY_TYPE:
2464 error ("array type value used where scalar is required");
2465 return boolean_false_node;
2467 default:
2468 break;
2470 #endif /* 0 */
2472 switch (TREE_CODE (expr))
2474 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2475 or comparison expressions as truth values at this level. */
2476 #if 0
2477 case COMPONENT_REF:
2478 /* A one-bit unsigned bit-field is already acceptable. */
2479 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
2480 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
2481 return expr;
2482 break;
2483 #endif
2485 case EQ_EXPR:
2486 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2487 or comparison expressions as truth values at this level. */
2488 #if 0
2489 if (integer_zerop (TREE_OPERAND (expr, 1)))
2490 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
2491 #endif
2492 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2493 case TRUTH_ANDIF_EXPR:
2494 case TRUTH_ORIF_EXPR:
2495 case TRUTH_AND_EXPR:
2496 case TRUTH_OR_EXPR:
2497 case TRUTH_XOR_EXPR:
2498 case TRUTH_NOT_EXPR:
2499 TREE_TYPE (expr) = boolean_type_node;
2500 return expr;
2502 case ERROR_MARK:
2503 return expr;
2505 case INTEGER_CST:
2506 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2508 case REAL_CST:
2509 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2511 case ADDR_EXPR:
2512 /* If we are taking the address of a external decl, it might be zero
2513 if it is weak, so we cannot optimize. */
2514 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr, 0))) == 'd'
2515 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2516 break;
2518 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2519 return build (COMPOUND_EXPR, boolean_type_node,
2520 TREE_OPERAND (expr, 0), boolean_true_node);
2521 else
2522 return boolean_true_node;
2524 case COMPLEX_EXPR:
2525 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2526 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2527 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2528 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2531 case NEGATE_EXPR:
2532 case ABS_EXPR:
2533 case FLOAT_EXPR:
2534 case FFS_EXPR:
2535 /* These don't change whether an object is non-zero or zero. */
2536 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2538 case LROTATE_EXPR:
2539 case RROTATE_EXPR:
2540 /* These don't change whether an object is zero or non-zero, but
2541 we can't ignore them if their second arg has side-effects. */
2542 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2543 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2544 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2545 else
2546 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2548 case COND_EXPR:
2549 /* Distribute the conversion into the arms of a COND_EXPR. */
2550 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2551 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2552 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2554 case CONVERT_EXPR:
2555 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2556 since that affects how `default_conversion' will behave. */
2557 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2558 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2559 break;
2560 /* fall through... */
2561 case NOP_EXPR:
2562 /* If this is widening the argument, we can ignore it. */
2563 if (TYPE_PRECISION (TREE_TYPE (expr))
2564 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2565 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2566 break;
2568 case MINUS_EXPR:
2569 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2570 this case. */
2571 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2572 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2573 break;
2574 /* fall through... */
2575 case BIT_XOR_EXPR:
2576 /* This and MINUS_EXPR can be changed into a comparison of the
2577 two objects. */
2578 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2579 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2580 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2581 TREE_OPERAND (expr, 1), 1);
2582 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2583 fold (build1 (NOP_EXPR,
2584 TREE_TYPE (TREE_OPERAND (expr, 0)),
2585 TREE_OPERAND (expr, 1))), 1);
2587 case BIT_AND_EXPR:
2588 if (integer_onep (TREE_OPERAND (expr, 1))
2589 && TREE_TYPE (expr) != boolean_type_node)
2590 /* Using convert here would cause infinite recursion. */
2591 return build1 (NOP_EXPR, boolean_type_node, expr);
2592 break;
2594 case MODIFY_EXPR:
2595 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2596 warning ("suggest parentheses around assignment used as truth value");
2597 break;
2599 default:
2600 break;
2603 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2605 tree tem = save_expr (expr);
2606 return (build_binary_op
2607 ((TREE_SIDE_EFFECTS (expr)
2608 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2609 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
2610 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
2611 0));
2614 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2617 /* Read the rest of a #-directive from input stream FINPUT.
2618 In normal use, the directive name and the white space after it
2619 have already been read, so they won't be included in the result.
2620 We allow for the fact that the directive line may contain
2621 a newline embedded within a character or string literal which forms
2622 a part of the directive.
2624 The value is a string in a reusable buffer. It remains valid
2625 only until the next time this function is called.
2627 The terminating character ('\n' or EOF) is left in FINPUT for the
2628 caller to re-read. */
2630 char *
2631 get_directive_line (finput)
2632 register FILE *finput;
2634 static char *directive_buffer = NULL;
2635 static unsigned buffer_length = 0;
2636 register char *p;
2637 register char *buffer_limit;
2638 register int looking_for = 0;
2639 register int char_escaped = 0;
2641 if (buffer_length == 0)
2643 directive_buffer = (char *)xmalloc (128);
2644 buffer_length = 128;
2647 buffer_limit = &directive_buffer[buffer_length];
2649 for (p = directive_buffer; ; )
2651 int c;
2653 /* Make buffer bigger if it is full. */
2654 if (p >= buffer_limit)
2656 register unsigned bytes_used = (p - directive_buffer);
2658 buffer_length *= 2;
2659 directive_buffer
2660 = (char *)xrealloc (directive_buffer, buffer_length);
2661 p = &directive_buffer[bytes_used];
2662 buffer_limit = &directive_buffer[buffer_length];
2665 c = getc (finput);
2667 /* Discard initial whitespace. */
2668 if ((c == ' ' || c == '\t') && p == directive_buffer)
2669 continue;
2671 /* Detect the end of the directive. */
2672 if (looking_for == 0
2673 && (c == '\n' || c == EOF))
2675 ungetc (c, finput);
2676 c = '\0';
2679 *p++ = c;
2681 if (c == 0)
2682 return directive_buffer;
2684 /* Handle string and character constant syntax. */
2685 if (looking_for)
2687 if (looking_for == c && !char_escaped)
2688 looking_for = 0; /* Found terminator... stop looking. */
2690 else
2691 if (c == '\'' || c == '"')
2692 looking_for = c; /* Don't stop buffering until we see another
2693 another one of these (or an EOF). */
2695 /* Handle backslash. */
2696 char_escaped = (c == '\\' && ! char_escaped);
2700 /* Make a variant type in the proper way for C/C++, propagating qualifiers
2701 down to the element type of an array. */
2703 tree
2704 c_build_type_variant (type, constp, volatilep)
2705 tree type;
2706 int constp, volatilep;
2708 if (TREE_CODE (type) == ARRAY_TYPE)
2709 return build_array_type (c_build_type_variant (TREE_TYPE (type),
2710 constp, volatilep),
2711 TYPE_DOMAIN (type));
2712 return build_type_variant (type, constp, volatilep);