1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 1993, 1994, 1995 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)
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
29 extern struct obstack permanent_obstack
;
31 enum attrs
{A_PACKED
, A_NOCOMMON
, A_NORETURN
, A_CONST
, A_T_UNION
,
32 A_CONSTRUCTOR
, A_DESTRUCTOR
, A_MODE
, A_SECTION
, A_ALIGNED
,
33 A_UNUSED
, A_FORMAT
, A_WEAK
, A_ALIAS
};
35 static void declare_hidden_char_array
PROTO((char *, char *));
36 static void add_attribute
PROTO((enum attrs
, char *,
38 static void init_attributes
PROTO((void));
40 /* Make bindings for __FUNCTION__ and __PRETTY_FUNCTION__. */
43 declare_function_name ()
45 char *name
, *printable_name
;
47 if (current_function_decl
== NULL
)
50 printable_name
= "top level";
54 char *kind
= "function";
55 if (TREE_CODE (TREE_TYPE (current_function_decl
)) == METHOD_TYPE
)
57 /* Allow functions to be nameless (such as artificial ones). */
58 if (DECL_NAME (current_function_decl
))
59 name
= IDENTIFIER_POINTER (DECL_NAME (current_function_decl
));
62 printable_name
= (*decl_printable_name
) (current_function_decl
, &kind
);
65 declare_hidden_char_array ("__FUNCTION__", name
);
66 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name
);
70 declare_hidden_char_array (name
, value
)
73 tree decl
, type
, init
;
76 /* If the default size of char arrays isn't big enough for the name,
77 or if we want to give warnings for large objects, make a bigger one. */
78 vlen
= strlen (value
) + 1;
79 type
= char_array_type_node
;
80 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TREE_TYPE (type
))) < vlen
82 type
= build_array_type (char_type_node
,
83 build_index_type (build_int_2 (vlen
, 0)));
84 push_obstacks_nochange ();
85 decl
= build_decl (VAR_DECL
, get_identifier (name
), type
);
86 TREE_STATIC (decl
) = 1;
87 TREE_READONLY (decl
) = 1;
88 TREE_ASM_WRITTEN (decl
) = 1;
89 DECL_SOURCE_LINE (decl
) = 0;
90 DECL_ARTIFICIAL (decl
) = 1;
91 DECL_IN_SYSTEM_HEADER (decl
) = 1;
92 DECL_IGNORED_P (decl
) = 1;
93 init
= build_string (vlen
, value
);
94 TREE_TYPE (init
) = type
;
95 DECL_INITIAL (decl
) = init
;
96 finish_decl (pushdecl (decl
), init
, NULL_TREE
);
99 /* Given a chain of STRING_CST nodes,
100 concatenate them into one STRING_CST
101 and give it a suitable array-of-chars data type. */
104 combine_strings (strings
)
107 register tree value
, t
;
108 register int length
= 1;
111 int wchar_bytes
= TYPE_PRECISION (wchar_type_node
) / BITS_PER_UNIT
;
114 if (TREE_CHAIN (strings
))
116 /* More than one in the chain, so concatenate. */
117 register char *p
, *q
;
119 /* Don't include the \0 at the end of each substring,
120 except for the last one.
121 Count wide strings and ordinary strings separately. */
122 for (t
= strings
; t
; t
= TREE_CHAIN (t
))
124 if (TREE_TYPE (t
) == wchar_array_type_node
)
126 wide_length
+= (TREE_STRING_LENGTH (t
) - wchar_bytes
);
130 length
+= (TREE_STRING_LENGTH (t
) - 1);
133 /* If anything is wide, the non-wides will be converted,
134 which makes them take more space. */
136 length
= length
* wchar_bytes
+ wide_length
;
138 p
= savealloc (length
);
140 /* Copy the individual strings into the new combined string.
141 If the combined string is wide, convert the chars to ints
142 for any individual strings that are not wide. */
145 for (t
= strings
; t
; t
= TREE_CHAIN (t
))
147 int len
= (TREE_STRING_LENGTH (t
)
148 - ((TREE_TYPE (t
) == wchar_array_type_node
)
150 if ((TREE_TYPE (t
) == wchar_array_type_node
) == wide_flag
)
152 bcopy (TREE_STRING_POINTER (t
), q
, len
);
158 for (i
= 0; i
< len
; i
++)
159 ((int *) q
)[i
] = TREE_STRING_POINTER (t
)[i
];
160 q
+= len
* wchar_bytes
;
166 for (i
= 0; i
< wchar_bytes
; i
++)
172 value
= make_node (STRING_CST
);
173 TREE_STRING_POINTER (value
) = p
;
174 TREE_STRING_LENGTH (value
) = length
;
175 TREE_CONSTANT (value
) = 1;
180 length
= TREE_STRING_LENGTH (value
);
181 if (TREE_TYPE (value
) == wchar_array_type_node
)
185 /* Compute the number of elements, for the array type. */
186 nchars
= wide_flag
? length
/ wchar_bytes
: length
;
188 /* Create the array type for the string constant.
189 -Wwrite-strings says make the string constant an array of const char
190 so that copying it to a non-const pointer will get a warning. */
191 if (warn_write_strings
192 && (! flag_traditional
&& ! flag_writable_strings
))
195 = build_type_variant (wide_flag
? wchar_type_node
: char_type_node
,
198 = build_array_type (elements
,
199 build_index_type (build_int_2 (nchars
- 1, 0)));
203 = build_array_type (wide_flag
? wchar_type_node
: char_type_node
,
204 build_index_type (build_int_2 (nchars
- 1, 0)));
205 TREE_CONSTANT (value
) = 1;
206 TREE_STATIC (value
) = 1;
210 /* To speed up processing of attributes, we maintain an array of
211 IDENTIFIER_NODES and the corresponding attribute types. */
213 /* Array to hold attribute information. */
215 static struct {enum attrs id
; tree name
; int min
, max
, decl_req
;} attrtab
[50];
217 static int attrtab_idx
= 0;
219 /* Add an entry to the attribute table above. */
222 add_attribute (id
, string
, min_len
, max_len
, decl_req
)
225 int min_len
, max_len
;
230 attrtab
[attrtab_idx
].id
= id
;
231 attrtab
[attrtab_idx
].name
= get_identifier (string
);
232 attrtab
[attrtab_idx
].min
= min_len
;
233 attrtab
[attrtab_idx
].max
= max_len
;
234 attrtab
[attrtab_idx
++].decl_req
= decl_req
;
236 sprintf (buf
, "__%s__", string
);
238 attrtab
[attrtab_idx
].id
= id
;
239 attrtab
[attrtab_idx
].name
= get_identifier (buf
);
240 attrtab
[attrtab_idx
].min
= min_len
;
241 attrtab
[attrtab_idx
].max
= max_len
;
242 attrtab
[attrtab_idx
++].decl_req
= decl_req
;
245 /* Initialize attribute table. */
250 add_attribute (A_PACKED
, "packed", 0, 0, 0);
251 add_attribute (A_NOCOMMON
, "nocommon", 0, 0, 1);
252 add_attribute (A_NORETURN
, "noreturn", 0, 0, 1);
253 add_attribute (A_NORETURN
, "volatile", 0, 0, 1);
254 add_attribute (A_UNUSED
, "unused", 0, 0, 1);
255 add_attribute (A_CONST
, "const", 0, 0, 1);
256 add_attribute (A_T_UNION
, "transparent_union", 0, 0, 0);
257 add_attribute (A_CONSTRUCTOR
, "constructor", 0, 0, 1);
258 add_attribute (A_DESTRUCTOR
, "destructor", 0, 0, 1);
259 add_attribute (A_MODE
, "mode", 1, 1, 1);
260 add_attribute (A_SECTION
, "section", 1, 1, 1);
261 add_attribute (A_ALIGNED
, "aligned", 0, 1, 0);
262 add_attribute (A_FORMAT
, "format", 3, 3, 1);
263 add_attribute (A_WEAK
, "weak", 0, 0, 1);
264 add_attribute (A_ALIAS
, "alias", 1, 1, 1);
267 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
268 and install them in NODE, which is either a DECL (including a TYPE_DECL)
269 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
270 and declaration modifiers but before the declaration proper. */
273 decl_attributes (node
, attributes
, prefix_attributes
)
274 tree node
, attributes
, prefix_attributes
;
280 if (attrtab_idx
== 0)
283 if (TREE_CODE_CLASS (TREE_CODE (node
)) == 'd')
286 type
= TREE_TYPE (decl
);
287 is_type
= TREE_CODE (node
) == TYPE_DECL
;
289 else if (TREE_CODE_CLASS (TREE_CODE (node
)) == 't')
290 type
= node
, is_type
= 1;
292 attributes
= chainon (prefix_attributes
, attributes
);
294 for (a
= attributes
; a
; a
= TREE_CHAIN (a
))
296 tree name
= TREE_PURPOSE (a
);
297 tree args
= TREE_VALUE (a
);
301 for (i
= 0; i
< attrtab_idx
; i
++)
302 if (attrtab
[i
].name
== name
)
305 if (i
== attrtab_idx
)
307 if (! valid_machine_attribute (name
, args
, decl
, type
))
308 warning ("`%s' attribute directive ignored",
309 IDENTIFIER_POINTER (name
));
312 else if (attrtab
[i
].decl_req
&& decl
== 0)
314 warning ("`%s' attribute does not apply to types",
315 IDENTIFIER_POINTER (name
));
318 else if (list_length (args
) < attrtab
[i
].min
319 || list_length (args
) > attrtab
[i
].max
)
321 error ("wrong number of arguments specified for `%s' attribute",
322 IDENTIFIER_POINTER (name
));
331 TYPE_PACKED (type
) = 1;
332 else if (TREE_CODE (decl
) == FIELD_DECL
)
333 DECL_PACKED (decl
) = 1;
334 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
335 used for DECL_REGISTER. It wouldn't mean anything anyway. */
337 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
341 if (TREE_CODE (decl
) == VAR_DECL
)
342 DECL_COMMON (decl
) = 0;
344 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
348 if (TREE_CODE (decl
) == FUNCTION_DECL
)
349 TREE_THIS_VOLATILE (decl
) = 1;
350 else if (TREE_CODE (type
) == POINTER_TYPE
351 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
352 TREE_TYPE (decl
) = type
354 (build_type_variant (TREE_TYPE (type
),
355 TREE_READONLY (TREE_TYPE (type
)), 1));
357 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
361 if (TREE_CODE (decl
) == PARM_DECL
|| TREE_CODE (decl
) == VAR_DECL
362 || TREE_CODE (decl
) == FUNCTION_DECL
)
363 TREE_USED (decl
) = 1;
365 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
369 if (TREE_CODE (decl
) == FUNCTION_DECL
)
370 TREE_READONLY (decl
) = 1;
371 else if (TREE_CODE (type
) == POINTER_TYPE
372 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
373 TREE_TYPE (decl
) = type
375 (build_type_variant (TREE_TYPE (type
), 1,
376 TREE_THIS_VOLATILE (TREE_TYPE (type
))));
378 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name
));
382 if (decl
!= 0 && TREE_CODE (decl
) == PARM_DECL
383 && TREE_CODE (type
) == UNION_TYPE
384 && TYPE_MODE (type
) == DECL_MODE (TYPE_FIELDS (type
)))
385 DECL_TRANSPARENT_UNION (decl
) = 1;
387 && TREE_CODE (type
) == UNION_TYPE
388 && TYPE_MODE (type
) == DECL_MODE (TYPE_FIELDS (type
)))
389 TYPE_TRANSPARENT_UNION (type
) = 1;
391 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
395 if (TREE_CODE (decl
) == FUNCTION_DECL
396 && TREE_CODE (type
) == FUNCTION_TYPE
397 && decl_function_context (decl
) == 0)
398 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
400 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
404 if (TREE_CODE (decl
) == FUNCTION_DECL
405 && TREE_CODE (type
) == FUNCTION_TYPE
406 && decl_function_context (decl
) == 0)
407 DECL_STATIC_DESTRUCTOR (decl
) = 1;
409 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
413 if (TREE_CODE (TREE_VALUE (args
)) != IDENTIFIER_NODE
)
414 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
418 char *p
= IDENTIFIER_POINTER (TREE_VALUE (args
));
419 int len
= strlen (p
);
420 enum machine_mode mode
= VOIDmode
;
423 if (len
> 4 && p
[0] == '_' && p
[1] == '_'
424 && p
[len
- 1] == '_' && p
[len
- 2] == '_')
426 char *newp
= (char *) alloca (len
- 2);
428 strcpy (newp
, &p
[2]);
429 newp
[len
- 4] = '\0';
433 /* Give this decl a type with the specified mode.
434 First check for the special modes. */
435 if (! strcmp (p
, "byte"))
437 else if (!strcmp (p
, "word"))
439 else if (! strcmp (p
, "pointer"))
442 for (j
= 0; j
< NUM_MACHINE_MODES
; j
++)
443 if (!strcmp (p
, GET_MODE_NAME (j
)))
444 mode
= (enum machine_mode
) j
;
446 if (mode
== VOIDmode
)
447 error ("unknown machine mode `%s'", p
);
448 else if (0 == (typefm
= type_for_mode (mode
,
449 TREE_UNSIGNED (type
))))
450 error ("no data type for mode `%s'", p
);
453 TREE_TYPE (decl
) = type
= typefm
;
454 DECL_SIZE (decl
) = 0;
455 layout_decl (decl
, 0);
461 #ifdef ASM_OUTPUT_SECTION_NAME
462 if ((TREE_CODE (decl
) == FUNCTION_DECL
463 || TREE_CODE (decl
) == VAR_DECL
)
464 && TREE_CODE (TREE_VALUE (args
)) == STRING_CST
)
466 if (TREE_CODE (decl
) == VAR_DECL
467 && current_function_decl
!= NULL_TREE
)
468 error_with_decl (decl
,
469 "section attribute cannot be specified for local variables");
470 /* The decl may have already been given a section attribute from
471 a previous declaration. Ensure they match. */
472 else if (DECL_SECTION_NAME (decl
) != NULL_TREE
473 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl
)),
474 TREE_STRING_POINTER (TREE_VALUE (args
))) != 0)
475 error_with_decl (node
,
476 "section of `%s' conflicts with previous declaration");
478 DECL_SECTION_NAME (decl
) = TREE_VALUE (args
);
481 error_with_decl (node
,
482 "section attribute not allowed for `%s'");
484 error_with_decl (node
,
485 "section attributes are not supported for this target");
492 = args
? TREE_VALUE (args
) : size_int (BIGGEST_ALIGNMENT
);
495 /* Strip any NOPs of any kind. */
496 while (TREE_CODE (align_expr
) == NOP_EXPR
497 || TREE_CODE (align_expr
) == CONVERT_EXPR
498 || TREE_CODE (align_expr
) == NON_LVALUE_EXPR
)
499 align_expr
= TREE_OPERAND (align_expr
, 0);
501 if (TREE_CODE (align_expr
) != INTEGER_CST
)
503 error ("requested alignment is not a constant");
507 align
= TREE_INT_CST_LOW (align_expr
) * BITS_PER_UNIT
;
509 if (exact_log2 (align
) == -1)
510 error ("requested alignment is not a power of 2");
512 TYPE_ALIGN (TREE_TYPE (decl
)) = align
;
513 else if (TREE_CODE (decl
) != VAR_DECL
514 && TREE_CODE (decl
) != FIELD_DECL
)
515 error_with_decl (decl
,
516 "alignment may not be specified for `%s'");
518 DECL_ALIGN (decl
) = align
;
524 tree format_type
= TREE_VALUE (args
);
525 tree format_num_expr
= TREE_VALUE (TREE_CHAIN (args
));
526 tree first_arg_num_expr
527 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args
)));
534 if (TREE_CODE (decl
) != FUNCTION_DECL
)
536 error_with_decl (decl
,
537 "argument format specified for non-function `%s'");
541 if (TREE_CODE (format_type
) == IDENTIFIER_NODE
542 && (!strcmp (IDENTIFIER_POINTER (format_type
), "printf")
543 || !strcmp (IDENTIFIER_POINTER (format_type
),
546 else if (TREE_CODE (format_type
) == IDENTIFIER_NODE
547 && (!strcmp (IDENTIFIER_POINTER (format_type
), "scanf")
548 || !strcmp (IDENTIFIER_POINTER (format_type
),
553 error ("unrecognized format specifier for `%s'");
557 /* Strip any conversions from the string index and first arg number
558 and verify they are constants. */
559 while (TREE_CODE (format_num_expr
) == NOP_EXPR
560 || TREE_CODE (format_num_expr
) == CONVERT_EXPR
561 || TREE_CODE (format_num_expr
) == NON_LVALUE_EXPR
)
562 format_num_expr
= TREE_OPERAND (format_num_expr
, 0);
564 while (TREE_CODE (first_arg_num_expr
) == NOP_EXPR
565 || TREE_CODE (first_arg_num_expr
) == CONVERT_EXPR
566 || TREE_CODE (first_arg_num_expr
) == NON_LVALUE_EXPR
)
567 first_arg_num_expr
= TREE_OPERAND (first_arg_num_expr
, 0);
569 if (TREE_CODE (format_num_expr
) != INTEGER_CST
570 || TREE_CODE (first_arg_num_expr
) != INTEGER_CST
)
572 error ("format string has non-constant operand number");
576 format_num
= TREE_INT_CST_LOW (format_num_expr
);
577 first_arg_num
= TREE_INT_CST_LOW (first_arg_num_expr
);
578 if (first_arg_num
!= 0 && first_arg_num
<= format_num
)
580 error ("format string arg follows the args to be formatted");
584 /* If a parameter list is specified, verify that the format_num
585 argument is actually a string, in case the format attribute
587 argument
= TYPE_ARG_TYPES (type
);
590 for (arg_num
= 1; ; ++arg_num
)
592 if (argument
== 0 || arg_num
== format_num
)
594 argument
= TREE_CHAIN (argument
);
597 || TREE_CODE (TREE_VALUE (argument
)) != POINTER_TYPE
598 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument
)))
601 error ("format string arg not a string type");
604 if (first_arg_num
!= 0)
606 /* Verify that first_arg_num points to the last arg,
609 arg_num
++, argument
= TREE_CHAIN (argument
);
610 if (arg_num
!= first_arg_num
)
612 error ("args to be formatted is not ...");
618 record_function_format (DECL_NAME (decl
),
619 DECL_ASSEMBLER_NAME (decl
),
620 is_scan
, format_num
, first_arg_num
);
629 if ((TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_INITIAL (decl
))
630 || TREE_CODE (decl
) != FUNCTION_DECL
&& ! DECL_EXTERNAL (decl
))
631 error_with_decl (decl
,
632 "`%s' defined both normally and as an alias");
633 else if (decl_function_context (decl
) == 0)
635 tree id
= get_identifier (TREE_STRING_POINTER
636 (TREE_VALUE (args
)));
637 if (TREE_CODE (decl
) == FUNCTION_DECL
)
638 DECL_INITIAL (decl
) = error_mark_node
;
640 DECL_EXTERNAL (decl
) = 0;
641 assemble_alias (decl
, id
);
644 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
650 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
653 #define T_I &integer_type_node
654 #define T_L &long_integer_type_node
655 #define T_LL &long_long_integer_type_node
656 #define T_S &short_integer_type_node
657 #define T_UI &unsigned_type_node
658 #define T_UL &long_unsigned_type_node
659 #define T_ULL &long_long_unsigned_type_node
660 #define T_US &short_unsigned_type_node
661 #define T_F &float_type_node
662 #define T_D &double_type_node
663 #define T_LD &long_double_type_node
664 #define T_C &char_type_node
665 #define T_V &void_type_node
666 #define T_W &wchar_type_node
667 #define T_ST &sizetype
672 /* Type of argument if no length modifier is used. */
674 /* Type of argument if length modifier for shortening is used.
675 If NULL, then this modifier is not allowed. */
677 /* Type of argument if length modifier `l' is used.
678 If NULL, then this modifier is not allowed. */
680 /* Type of argument if length modifier `q' or `ll' is used.
681 If NULL, then this modifier is not allowed. */
683 /* Type of argument if length modifier `L' is used.
684 If NULL, then this modifier is not allowed. */
686 /* List of other modifier characters allowed with these options. */
690 static format_char_info print_char_table
[] = {
691 { "di", 0, T_I
, T_I
, T_L
, T_LL
, T_LL
, "-wp0 +" },
692 { "oxX", 0, T_UI
, T_UI
, T_UL
, T_ULL
, T_ULL
, "-wp0#" },
693 { "u", 0, T_UI
, T_UI
, T_UL
, T_ULL
, T_ULL
, "-wp0" },
694 /* Two GNU extensions. */
695 { "Z", 0, T_ST
, NULL
, NULL
, NULL
, NULL
, "-wp0" },
696 { "m", 0, T_UI
, T_UI
, T_UL
, NULL
, NULL
, "-wp" },
697 { "feEgG", 0, T_D
, NULL
, NULL
, NULL
, T_LD
, "-wp0 +#" },
698 { "c", 0, T_I
, NULL
, T_W
, NULL
, NULL
, "-w" },
699 { "C", 0, T_W
, NULL
, NULL
, NULL
, NULL
, "-w" },
700 { "s", 1, T_C
, NULL
, T_W
, NULL
, NULL
, "-wp" },
701 { "S", 1, T_W
, NULL
, NULL
, NULL
, NULL
, "-wp" },
702 { "p", 1, T_V
, NULL
, NULL
, NULL
, NULL
, "-w" },
703 { "n", 1, T_I
, T_S
, T_L
, T_LL
, NULL
, "" },
707 static format_char_info scan_char_table
[] = {
708 { "di", 1, T_I
, T_S
, T_L
, T_LL
, T_LL
, "*" },
709 { "ouxX", 1, T_UI
, T_US
, T_UL
, T_ULL
, T_ULL
, "*" },
710 { "efgEG", 1, T_F
, NULL
, T_D
, NULL
, T_LD
, "*" },
711 { "sc", 1, T_C
, NULL
, T_W
, NULL
, NULL
, "*a" },
712 { "[", 1, T_C
, NULL
, NULL
, NULL
, NULL
, "*a" },
713 { "C", 1, T_W
, NULL
, NULL
, NULL
, NULL
, "*" },
714 { "S", 1, T_W
, NULL
, NULL
, NULL
, NULL
, "*" },
715 { "p", 2, T_V
, NULL
, NULL
, NULL
, NULL
, "*" },
716 { "n", 1, T_I
, T_S
, T_L
, T_LL
, NULL
, "" },
720 typedef struct function_format_info
{
721 struct function_format_info
*next
; /* next structure on the list */
722 tree name
; /* identifier such as "printf" */
723 tree assembler_name
; /* optional mangled identifier (for C++) */
724 int is_scan
; /* TRUE if *scanf */
725 int format_num
; /* number of format argument */
726 int first_arg_num
; /* number of first arg (zero for varargs) */
727 } function_format_info
;
729 static function_format_info
*function_format_list
= NULL
;
731 static void check_format_info
PROTO((function_format_info
*, tree
));
733 /* Initialize the table of functions to perform format checking on.
734 The ANSI functions are always checked (whether <stdio.h> is
735 included or not), since it is common to call printf without
736 including <stdio.h>. There shouldn't be a problem with this,
737 since ANSI reserves these function names whether you include the
738 header file or not. In any case, the checking is harmless. */
741 init_function_format_info ()
743 record_function_format (get_identifier ("printf"), NULL_TREE
, 0, 1, 2);
744 record_function_format (get_identifier ("fprintf"), NULL_TREE
, 0, 2, 3);
745 record_function_format (get_identifier ("sprintf"), NULL_TREE
, 0, 2, 3);
746 record_function_format (get_identifier ("scanf"), NULL_TREE
, 1, 1, 2);
747 record_function_format (get_identifier ("fscanf"), NULL_TREE
, 1, 2, 3);
748 record_function_format (get_identifier ("sscanf"), NULL_TREE
, 1, 2, 3);
749 record_function_format (get_identifier ("vprintf"), NULL_TREE
, 0, 1, 0);
750 record_function_format (get_identifier ("vfprintf"), NULL_TREE
, 0, 2, 0);
751 record_function_format (get_identifier ("vsprintf"), NULL_TREE
, 0, 2, 0);
754 /* Record information for argument format checking. FUNCTION_IDENT is
755 the identifier node for the name of the function to check (its decl
756 need not exist yet). IS_SCAN is true for scanf-type format checking;
757 false indicates printf-style format checking. FORMAT_NUM is the number
758 of the argument which is the format control string (starting from 1).
759 FIRST_ARG_NUM is the number of the first actual argument to check
760 against teh format string, or zero if no checking is not be done
761 (e.g. for varargs such as vfprintf). */
764 record_function_format (name
, assembler_name
, is_scan
,
765 format_num
, first_arg_num
)
772 function_format_info
*info
;
774 /* Re-use existing structure if it's there. */
776 for (info
= function_format_list
; info
; info
= info
->next
)
778 if (info
->name
== name
&& info
->assembler_name
== assembler_name
)
783 info
= (function_format_info
*) xmalloc (sizeof (function_format_info
));
784 info
->next
= function_format_list
;
785 function_format_list
= info
;
788 info
->assembler_name
= assembler_name
;
791 info
->is_scan
= is_scan
;
792 info
->format_num
= format_num
;
793 info
->first_arg_num
= first_arg_num
;
796 static char tfaff
[] = "too few arguments for format";
798 /* Check the argument list of a call to printf, scanf, etc.
799 NAME is the function identifier.
800 ASSEMBLER_NAME is the function's assembler identifier.
801 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
802 PARAMS is the list of argument values. */
805 check_function_format (name
, assembler_name
, params
)
810 function_format_info
*info
;
812 /* See if this function is a format function. */
813 for (info
= function_format_list
; info
; info
= info
->next
)
815 if (info
->assembler_name
816 ? (info
->assembler_name
== assembler_name
)
817 : (info
->name
== name
))
820 check_format_info (info
, params
);
826 /* Check the argument list of a call to printf, scanf, etc.
827 INFO points to the function_format_info structure.
828 PARAMS is the list of argument values. */
831 check_format_info (info
, params
)
832 function_format_info
*info
;
837 int suppressed
, wide
, precise
;
845 tree first_fillin_param
;
847 format_char_info
*fci
;
848 static char message
[132];
850 int has_operand_number
= 0;
852 /* Skip to format argument. If the argument isn't available, there's
853 no work for us to do; prototype checking will catch the problem. */
854 for (arg_num
= 1; ; ++arg_num
)
858 if (arg_num
== info
->format_num
)
860 params
= TREE_CHAIN (params
);
862 format_tree
= TREE_VALUE (params
);
863 params
= TREE_CHAIN (params
);
864 if (format_tree
== 0)
866 /* We can only check the format if it's a string constant. */
867 while (TREE_CODE (format_tree
) == NOP_EXPR
)
868 format_tree
= TREE_OPERAND (format_tree
, 0); /* strip coercion */
869 if (format_tree
== null_pointer_node
)
871 warning ("null format string");
874 if (TREE_CODE (format_tree
) != ADDR_EXPR
)
876 format_tree
= TREE_OPERAND (format_tree
, 0);
877 if (TREE_CODE (format_tree
) != STRING_CST
)
879 format_chars
= TREE_STRING_POINTER (format_tree
);
880 format_length
= TREE_STRING_LENGTH (format_tree
);
881 if (format_length
<= 1)
882 warning ("zero-length format string");
883 if (format_chars
[--format_length
] != 0)
885 warning ("unterminated format string");
888 /* Skip to first argument to check. */
889 while (arg_num
+ 1 < info
->first_arg_num
)
893 params
= TREE_CHAIN (params
);
897 first_fillin_param
= params
;
901 if (*format_chars
== 0)
903 if (format_chars
- TREE_STRING_POINTER (format_tree
) != format_length
)
904 warning ("embedded `\\0' in format");
905 if (info
->first_arg_num
!= 0 && params
!= 0 && ! has_operand_number
)
906 warning ("too many arguments for format");
909 if (*format_chars
++ != '%')
911 if (*format_chars
== 0)
913 warning ("spurious trailing `%%' in format");
916 if (*format_chars
== '%')
922 suppressed
= wide
= precise
= FALSE
;
925 suppressed
= *format_chars
== '*';
928 while (isdigit (*format_chars
))
933 /* See if we have a number followed by a dollar sign. If we do,
934 it is an operand number, so set PARAMS to that operand. */
935 if (*format_chars
>= '0' && *format_chars
<= '9')
937 char *p
= format_chars
;
939 while (*p
>= '0' && *p
++ <= '9')
944 int opnum
= atoi (format_chars
);
946 params
= first_fillin_param
;
947 format_chars
= p
+ 1;
948 has_operand_number
= 1;
950 for (i
= 1; i
< opnum
&& params
!= 0; i
++)
951 params
= TREE_CHAIN (params
);
953 if (opnum
== 0 || params
== 0)
955 warning ("operand number out of range in format");
961 while (*format_chars
!= 0 && index (" +#0-", *format_chars
) != 0)
963 if (index (flag_chars
, *format_chars
) != 0)
965 sprintf (message
, "repeated `%c' flag in format",
969 i
= strlen (flag_chars
);
970 flag_chars
[i
++] = *format_chars
++;
973 /* "If the space and + flags both appear,
974 the space flag will be ignored." */
975 if (index (flag_chars
, ' ') != 0
976 && index (flag_chars
, '+') != 0)
977 warning ("use of both ` ' and `+' flags in format");
978 /* "If the 0 and - flags both appear,
979 the 0 flag will be ignored." */
980 if (index (flag_chars
, '0') != 0
981 && index (flag_chars
, '-') != 0)
982 warning ("use of both `0' and `-' flags in format");
983 if (*format_chars
== '*')
986 /* "...a field width...may be indicated by an asterisk.
987 In this case, an int argument supplies the field width..." */
994 if (info
->first_arg_num
!= 0)
996 cur_param
= TREE_VALUE (params
);
997 params
= TREE_CHAIN (params
);
999 /* size_t is generally not valid here.
1000 It will work on most machines, because size_t and int
1001 have the same mode. But might as well warn anyway,
1002 since it will fail on other machines. */
1003 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1004 != integer_type_node
)
1006 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1007 != unsigned_type_node
))
1010 "field width is not type int (arg %d)",
1018 while (isdigit (*format_chars
))
1024 if (*format_chars
== '.')
1028 if (*format_chars
!= '*' && !isdigit (*format_chars
))
1029 warning ("`.' not followed by `*' or digit in format");
1030 /* "...a...precision...may be indicated by an asterisk.
1031 In this case, an int argument supplies the...precision." */
1032 if (*format_chars
== '*')
1034 if (info
->first_arg_num
!= 0)
1042 cur_param
= TREE_VALUE (params
);
1043 params
= TREE_CHAIN (params
);
1045 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1046 != integer_type_node
)
1049 "field width is not type int (arg %d)",
1057 while (isdigit (*format_chars
))
1062 if (*format_chars
== 'h' || *format_chars
== 'l' || *format_chars
== 'q' ||
1063 *format_chars
== 'L')
1064 length_char
= *format_chars
++;
1067 if (length_char
== 'l' && *format_chars
== 'l')
1068 length_char
= 'q', format_chars
++;
1070 if (*format_chars
== 'a')
1075 if (suppressed
&& length_char
!= 0)
1078 "use of `*' and `%c' together in format",
1082 format_char
= *format_chars
;
1083 if (format_char
== 0)
1085 warning ("conversion lacks type at end of format");
1089 fci
= info
->is_scan
? scan_char_table
: print_char_table
;
1090 while (fci
->format_chars
!= 0
1091 && index (fci
->format_chars
, format_char
) == 0)
1093 if (fci
->format_chars
== 0)
1095 if (format_char
>= 040 && format_char
< 0177)
1097 "unknown conversion type character `%c' in format",
1101 "unknown conversion type character 0x%x in format",
1106 if (wide
&& index (fci
->flag_chars
, 'w') == 0)
1108 sprintf (message
, "width used with `%c' format",
1112 if (precise
&& index (fci
->flag_chars
, 'p') == 0)
1114 sprintf (message
, "precision used with `%c' format",
1118 if (aflag
&& index (fci
->flag_chars
, 'a') == 0)
1120 sprintf (message
, "`a' flag used with `%c' format",
1124 if (info
->is_scan
&& format_char
== '[')
1126 /* Skip over scan set, in case it happens to have '%' in it. */
1127 if (*format_chars
== '^')
1129 /* Find closing bracket; if one is hit immediately, then
1130 it's part of the scan set rather than a terminator. */
1131 if (*format_chars
== ']')
1133 while (*format_chars
&& *format_chars
!= ']')
1135 if (*format_chars
!= ']')
1136 /* The end of the format string was reached. */
1137 warning ("no closing `]' for `%%[' format");
1141 if (index (fci
->flag_chars
, '*') == 0)
1144 "suppression of `%c' conversion in format",
1150 for (i
= 0; flag_chars
[i
] != 0; ++i
)
1152 if (index (fci
->flag_chars
, flag_chars
[i
]) == 0)
1154 sprintf (message
, "flag `%c' used with type `%c'",
1155 flag_chars
[i
], format_char
);
1159 if (precise
&& index (flag_chars
, '0') != 0
1160 && (format_char
== 'd' || format_char
== 'i'
1161 || format_char
== 'o' || format_char
== 'u'
1162 || format_char
== 'x' || format_char
== 'x'))
1165 "precision and `0' flag not both allowed with `%c' format",
1169 switch (length_char
)
1171 default: wanted_type
= fci
->nolen
? *(fci
->nolen
) : 0; break;
1172 case 'h': wanted_type
= fci
->hlen
? *(fci
->hlen
) : 0; break;
1173 case 'l': wanted_type
= fci
->llen
? *(fci
->llen
) : 0; break;
1174 case 'q': wanted_type
= fci
->qlen
? *(fci
->qlen
) : 0; break;
1175 case 'L': wanted_type
= fci
->bigllen
? *(fci
->bigllen
) : 0; break;
1177 if (wanted_type
== 0)
1180 "use of `%c' length character with `%c' type character",
1181 length_char
, format_char
);
1186 ** XXX -- should kvetch about stuff such as
1190 ** scanf ("%d", &i);
1194 /* Finally. . .check type of argument against desired type! */
1195 if (info
->first_arg_num
== 0)
1202 cur_param
= TREE_VALUE (params
);
1203 params
= TREE_CHAIN (params
);
1205 cur_type
= TREE_TYPE (cur_param
);
1207 /* Check the types of any additional pointer arguments
1208 that precede the "real" argument. */
1209 for (i
= 0; i
< fci
->pointer_count
; ++i
)
1211 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
1213 cur_type
= TREE_TYPE (cur_type
);
1217 "format argument is not a %s (arg %d)",
1218 ((fci
->pointer_count
== 1) ? "pointer" : "pointer to a pointer"),
1224 /* Check the type of the "real" argument, if there's a type we want. */
1225 if (i
== fci
->pointer_count
&& wanted_type
!= 0
1226 && wanted_type
!= TYPE_MAIN_VARIANT (cur_type
)
1227 /* If we want `void *', allow any pointer type.
1228 (Anything else would already have got a warning.) */
1229 && ! (wanted_type
== void_type_node
1230 && fci
->pointer_count
> 0)
1231 /* Don't warn about differences merely in signedness. */
1232 && !(TREE_CODE (wanted_type
) == INTEGER_TYPE
1233 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type
)) == INTEGER_TYPE
1234 && (TREE_UNSIGNED (wanted_type
)
1235 ? wanted_type
== (cur_type
= unsigned_type (cur_type
))
1236 : wanted_type
== (cur_type
= signed_type (cur_type
))))
1237 /* Likewise, "signed char", "unsigned char" and "char" are
1238 equivalent but the above test won't consider them equivalent. */
1239 && ! (wanted_type
== char_type_node
1240 && (TYPE_MAIN_VARIANT (cur_type
) == signed_char_type_node
1241 || TYPE_MAIN_VARIANT (cur_type
) == unsigned_char_type_node
)))
1243 register char *this;
1244 register char *that
;
1246 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type
)));
1248 if (TREE_CODE (cur_type
) != ERROR_MARK
1249 && TYPE_NAME (cur_type
) != 0
1250 && TREE_CODE (cur_type
) != INTEGER_TYPE
1251 && !(TREE_CODE (cur_type
) == POINTER_TYPE
1252 && TREE_CODE (TREE_TYPE (cur_type
)) == INTEGER_TYPE
))
1254 if (TREE_CODE (TYPE_NAME (cur_type
)) == TYPE_DECL
1255 && DECL_NAME (TYPE_NAME (cur_type
)) != 0)
1256 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type
)));
1258 that
= IDENTIFIER_POINTER (TYPE_NAME (cur_type
));
1261 /* A nameless type can't possibly match what the format wants.
1262 So there will be a warning for it.
1263 Make up a string to describe vaguely what it is. */
1266 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
1269 that
= "different type";
1272 /* Make the warning better in case of mismatch of int vs long. */
1273 if (TREE_CODE (cur_type
) == INTEGER_TYPE
1274 && TREE_CODE (wanted_type
) == INTEGER_TYPE
1275 && TYPE_PRECISION (cur_type
) == TYPE_PRECISION (wanted_type
)
1276 && TYPE_NAME (cur_type
) != 0
1277 && TREE_CODE (TYPE_NAME (cur_type
)) == TYPE_DECL
)
1278 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type
)));
1280 if (strcmp (this, that
) != 0)
1282 sprintf (message
, "%s format, %s arg (arg %d)",
1283 this, that
, arg_num
);
1290 /* Print a warning if a constant expression had overflow in folding.
1291 Invoke this function on every expression that the language
1292 requires to be a constant expression.
1293 Note the ANSI C standard says it is erroneous for a
1294 constant expression to overflow. */
1297 constant_expression_warning (value
)
1300 if ((TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
1301 || TREE_CODE (value
) == COMPLEX_CST
)
1302 && TREE_CONSTANT_OVERFLOW (value
) && pedantic
)
1303 pedwarn ("overflow in constant expression");
1306 /* Print a warning if an expression had overflow in folding.
1307 Invoke this function on every expression that
1308 (1) appears in the source code, and
1309 (2) might be a constant expression that overflowed, and
1310 (3) is not already checked by convert_and_check;
1311 however, do not invoke this function on operands of explicit casts. */
1314 overflow_warning (value
)
1317 if ((TREE_CODE (value
) == INTEGER_CST
1318 || (TREE_CODE (value
) == COMPLEX_CST
1319 && TREE_CODE (TREE_REALPART (value
)) == INTEGER_CST
))
1320 && TREE_OVERFLOW (value
))
1322 TREE_OVERFLOW (value
) = 0;
1323 warning ("integer overflow in expression");
1325 else if ((TREE_CODE (value
) == REAL_CST
1326 || (TREE_CODE (value
) == COMPLEX_CST
1327 && TREE_CODE (TREE_REALPART (value
)) == REAL_CST
))
1328 && TREE_OVERFLOW (value
))
1330 TREE_OVERFLOW (value
) = 0;
1331 warning ("floating-pointer overflow in expression");
1335 /* Print a warning if a large constant is truncated to unsigned,
1336 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1337 Invoke this function on every expression that might be implicitly
1338 converted to an unsigned type. */
1341 unsigned_conversion_warning (result
, operand
)
1342 tree result
, operand
;
1344 if (TREE_CODE (operand
) == INTEGER_CST
1345 && TREE_CODE (TREE_TYPE (result
)) == INTEGER_TYPE
1346 && TREE_UNSIGNED (TREE_TYPE (result
))
1347 && !int_fits_type_p (operand
, TREE_TYPE (result
)))
1349 if (!int_fits_type_p (operand
, signed_type (TREE_TYPE (result
))))
1350 /* This detects cases like converting -129 or 256 to unsigned char. */
1351 warning ("large integer implicitly truncated to unsigned type");
1352 else if (warn_conversion
)
1353 warning ("negative integer implicitly converted to unsigned type");
1357 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1358 Invoke this function on every expression that is converted implicitly,
1359 i.e. because of language rules and not because of an explicit cast. */
1362 convert_and_check (type
, expr
)
1365 tree t
= convert (type
, expr
);
1366 if (TREE_CODE (t
) == INTEGER_CST
)
1368 if (TREE_OVERFLOW (t
))
1370 TREE_OVERFLOW (t
) = 0;
1372 /* Do not diagnose overflow in a constant expression merely
1373 because a conversion overflowed. */
1374 TREE_CONSTANT_OVERFLOW (t
) = TREE_CONSTANT_OVERFLOW (expr
);
1376 /* No warning for converting 0x80000000 to int. */
1377 if (!(TREE_UNSIGNED (type
) < TREE_UNSIGNED (TREE_TYPE (expr
))
1378 && TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
1379 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (expr
))))
1380 /* If EXPR fits in the unsigned version of TYPE,
1381 don't warn unless pedantic. */
1383 || TREE_UNSIGNED (type
)
1384 || ! int_fits_type_p (expr
, unsigned_type (type
)))
1385 warning ("overflow in implicit constant conversion");
1388 unsigned_conversion_warning (t
, expr
);
1394 c_expand_expr_stmt (expr
)
1397 /* Do default conversion if safe and possibly important,
1398 in case within ({...}). */
1399 if ((TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
&& lvalue_p (expr
))
1400 || TREE_CODE (TREE_TYPE (expr
)) == FUNCTION_TYPE
)
1401 expr
= default_conversion (expr
);
1403 if (TREE_TYPE (expr
) != error_mark_node
1404 && TYPE_SIZE (TREE_TYPE (expr
)) == 0
1405 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
1406 error ("expression statement has incomplete type");
1408 expand_expr_stmt (expr
);
1411 /* Validate the expression after `case' and apply default promotions. */
1414 check_case_value (value
)
1417 if (value
== NULL_TREE
)
1420 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1421 STRIP_TYPE_NOPS (value
);
1423 if (TREE_CODE (value
) != INTEGER_CST
1424 && value
!= error_mark_node
)
1426 error ("case label does not reduce to an integer constant");
1427 value
= error_mark_node
;
1430 /* Promote char or short to int. */
1431 value
= default_conversion (value
);
1433 constant_expression_warning (value
);
1438 /* Return an integer type with BITS bits of precision,
1439 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1442 type_for_size (bits
, unsignedp
)
1446 if (bits
== TYPE_PRECISION (integer_type_node
))
1447 return unsignedp
? unsigned_type_node
: integer_type_node
;
1449 if (bits
== TYPE_PRECISION (signed_char_type_node
))
1450 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
1452 if (bits
== TYPE_PRECISION (short_integer_type_node
))
1453 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
1455 if (bits
== TYPE_PRECISION (long_integer_type_node
))
1456 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
1458 if (bits
== TYPE_PRECISION (long_long_integer_type_node
))
1459 return (unsignedp
? long_long_unsigned_type_node
1460 : long_long_integer_type_node
);
1462 if (bits
<= TYPE_PRECISION (intQI_type_node
))
1463 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
1465 if (bits
<= TYPE_PRECISION (intHI_type_node
))
1466 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
1468 if (bits
<= TYPE_PRECISION (intSI_type_node
))
1469 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
1471 if (bits
<= TYPE_PRECISION (intDI_type_node
))
1472 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
1477 /* Return a data type that has machine mode MODE.
1478 If the mode is an integer,
1479 then UNSIGNEDP selects between signed and unsigned types. */
1482 type_for_mode (mode
, unsignedp
)
1483 enum machine_mode mode
;
1486 if (mode
== TYPE_MODE (integer_type_node
))
1487 return unsignedp
? unsigned_type_node
: integer_type_node
;
1489 if (mode
== TYPE_MODE (signed_char_type_node
))
1490 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
1492 if (mode
== TYPE_MODE (short_integer_type_node
))
1493 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
1495 if (mode
== TYPE_MODE (long_integer_type_node
))
1496 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
1498 if (mode
== TYPE_MODE (long_long_integer_type_node
))
1499 return unsignedp
? long_long_unsigned_type_node
: long_long_integer_type_node
;
1501 if (mode
== TYPE_MODE (intQI_type_node
))
1502 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
1504 if (mode
== TYPE_MODE (intHI_type_node
))
1505 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
1507 if (mode
== TYPE_MODE (intSI_type_node
))
1508 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
1510 if (mode
== TYPE_MODE (intDI_type_node
))
1511 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
1513 if (mode
== TYPE_MODE (float_type_node
))
1514 return float_type_node
;
1516 if (mode
== TYPE_MODE (double_type_node
))
1517 return double_type_node
;
1519 if (mode
== TYPE_MODE (long_double_type_node
))
1520 return long_double_type_node
;
1522 if (mode
== TYPE_MODE (build_pointer_type (char_type_node
)))
1523 return build_pointer_type (char_type_node
);
1525 if (mode
== TYPE_MODE (build_pointer_type (integer_type_node
)))
1526 return build_pointer_type (integer_type_node
);
1531 /* Return the minimum number of bits needed to represent VALUE in a
1532 signed or unsigned type, UNSIGNEDP says which. */
1535 min_precision (value
, unsignedp
)
1541 /* If the value is negative, compute its negative minus 1. The latter
1542 adjustment is because the absolute value of the largest negative value
1543 is one larger than the largest positive value. This is equivalent to
1544 a bit-wise negation, so use that operation instead. */
1546 if (tree_int_cst_sgn (value
) < 0)
1547 value
= fold (build1 (BIT_NOT_EXPR
, TREE_TYPE (value
), value
));
1549 /* Return the number of bits needed, taking into account the fact
1550 that we need one more bit for a signed than unsigned type. */
1552 if (integer_zerop (value
))
1554 else if (TREE_INT_CST_HIGH (value
) != 0)
1555 log
= HOST_BITS_PER_WIDE_INT
+ floor_log2 (TREE_INT_CST_HIGH (value
));
1557 log
= floor_log2 (TREE_INT_CST_LOW (value
));
1559 return log
+ 1 + ! unsignedp
;
1562 /* Print an error message for invalid operands to arith operation CODE.
1563 NOP_EXPR is used as a special case (see truthvalue_conversion). */
1566 binary_op_error (code
)
1567 enum tree_code code
;
1569 register char *opname
= "unknown";
1574 error ("invalid truth-value expression");
1578 opname
= "+"; break;
1580 opname
= "-"; break;
1582 opname
= "*"; break;
1584 opname
= "max"; break;
1586 opname
= "min"; break;
1588 opname
= "=="; break;
1590 opname
= "!="; break;
1592 opname
= "<="; break;
1594 opname
= ">="; break;
1596 opname
= "<"; break;
1598 opname
= ">"; break;
1600 opname
= "<<"; break;
1602 opname
= ">>"; break;
1603 case TRUNC_MOD_EXPR
:
1604 case FLOOR_MOD_EXPR
:
1605 opname
= "%"; break;
1606 case TRUNC_DIV_EXPR
:
1607 case FLOOR_DIV_EXPR
:
1608 opname
= "/"; break;
1610 opname
= "&"; break;
1612 opname
= "|"; break;
1613 case TRUTH_ANDIF_EXPR
:
1614 opname
= "&&"; break;
1615 case TRUTH_ORIF_EXPR
:
1616 opname
= "||"; break;
1618 opname
= "^"; break;
1621 opname
= "rotate"; break;
1623 error ("invalid operands to binary %s", opname
);
1626 /* Subroutine of build_binary_op, used for comparison operations.
1627 See if the operands have both been converted from subword integer types
1628 and, if so, perhaps change them both back to their original type.
1629 This function is also responsible for converting the two operands
1630 to the proper common type for comparison.
1632 The arguments of this function are all pointers to local variables
1633 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
1634 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
1636 If this function returns nonzero, it means that the comparison has
1637 a constant value. What this function returns is an expression for
1641 shorten_compare (op0_ptr
, op1_ptr
, restype_ptr
, rescode_ptr
)
1642 tree
*op0_ptr
, *op1_ptr
;
1644 enum tree_code
*rescode_ptr
;
1647 tree op0
= *op0_ptr
;
1648 tree op1
= *op1_ptr
;
1649 int unsignedp0
, unsignedp1
;
1651 tree primop0
, primop1
;
1652 enum tree_code code
= *rescode_ptr
;
1654 /* Throw away any conversions to wider types
1655 already present in the operands. */
1657 primop0
= get_narrower (op0
, &unsignedp0
);
1658 primop1
= get_narrower (op1
, &unsignedp1
);
1660 /* Handle the case that OP0 does not *contain* a conversion
1661 but it *requires* conversion to FINAL_TYPE. */
1663 if (op0
== primop0
&& TREE_TYPE (op0
) != *restype_ptr
)
1664 unsignedp0
= TREE_UNSIGNED (TREE_TYPE (op0
));
1665 if (op1
== primop1
&& TREE_TYPE (op1
) != *restype_ptr
)
1666 unsignedp1
= TREE_UNSIGNED (TREE_TYPE (op1
));
1668 /* If one of the operands must be floated, we cannot optimize. */
1669 real1
= TREE_CODE (TREE_TYPE (primop0
)) == REAL_TYPE
;
1670 real2
= TREE_CODE (TREE_TYPE (primop1
)) == REAL_TYPE
;
1672 /* If first arg is constant, swap the args (changing operation
1673 so value is preserved), for canonicalization. Don't do this if
1674 the second arg is 0. */
1676 if (TREE_CONSTANT (primop0
)
1677 && ! integer_zerop (primop1
) && ! real_zerop (primop1
))
1679 register tree tem
= primop0
;
1680 register int temi
= unsignedp0
;
1688 unsignedp0
= unsignedp1
;
1709 *rescode_ptr
= code
;
1712 /* If comparing an integer against a constant more bits wide,
1713 maybe we can deduce a value of 1 or 0 independent of the data.
1714 Or else truncate the constant now
1715 rather than extend the variable at run time.
1717 This is only interesting if the constant is the wider arg.
1718 Also, it is not safe if the constant is unsigned and the
1719 variable arg is signed, since in this case the variable
1720 would be sign-extended and then regarded as unsigned.
1721 Our technique fails in this case because the lowest/highest
1722 possible unsigned results don't follow naturally from the
1723 lowest/highest possible values of the variable operand.
1724 For just EQ_EXPR and NE_EXPR there is another technique that
1725 could be used: see if the constant can be faithfully represented
1726 in the other operand's type, by truncating it and reextending it
1727 and see if that preserves the constant's value. */
1729 if (!real1
&& !real2
1730 && TREE_CODE (primop1
) == INTEGER_CST
1731 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
))
1733 int min_gt
, max_gt
, min_lt
, max_lt
;
1734 tree maxval
, minval
;
1735 /* 1 if comparison is nominally unsigned. */
1736 int unsignedp
= TREE_UNSIGNED (*restype_ptr
);
1739 type
= signed_or_unsigned_type (unsignedp0
, TREE_TYPE (primop0
));
1741 maxval
= TYPE_MAX_VALUE (type
);
1742 minval
= TYPE_MIN_VALUE (type
);
1744 if (unsignedp
&& !unsignedp0
)
1745 *restype_ptr
= signed_type (*restype_ptr
);
1747 if (TREE_TYPE (primop1
) != *restype_ptr
)
1748 primop1
= convert (*restype_ptr
, primop1
);
1749 if (type
!= *restype_ptr
)
1751 minval
= convert (*restype_ptr
, minval
);
1752 maxval
= convert (*restype_ptr
, maxval
);
1755 if (unsignedp
&& unsignedp0
)
1757 min_gt
= INT_CST_LT_UNSIGNED (primop1
, minval
);
1758 max_gt
= INT_CST_LT_UNSIGNED (primop1
, maxval
);
1759 min_lt
= INT_CST_LT_UNSIGNED (minval
, primop1
);
1760 max_lt
= INT_CST_LT_UNSIGNED (maxval
, primop1
);
1764 min_gt
= INT_CST_LT (primop1
, minval
);
1765 max_gt
= INT_CST_LT (primop1
, maxval
);
1766 min_lt
= INT_CST_LT (minval
, primop1
);
1767 max_lt
= INT_CST_LT (maxval
, primop1
);
1771 /* This used to be a switch, but Genix compiler can't handle that. */
1772 if (code
== NE_EXPR
)
1774 if (max_lt
|| min_gt
)
1775 val
= boolean_true_node
;
1777 else if (code
== EQ_EXPR
)
1779 if (max_lt
|| min_gt
)
1780 val
= boolean_false_node
;
1782 else if (code
== LT_EXPR
)
1785 val
= boolean_true_node
;
1787 val
= boolean_false_node
;
1789 else if (code
== GT_EXPR
)
1792 val
= boolean_true_node
;
1794 val
= boolean_false_node
;
1796 else if (code
== LE_EXPR
)
1799 val
= boolean_true_node
;
1801 val
= boolean_false_node
;
1803 else if (code
== GE_EXPR
)
1806 val
= boolean_true_node
;
1808 val
= boolean_false_node
;
1811 /* If primop0 was sign-extended and unsigned comparison specd,
1812 we did a signed comparison above using the signed type bounds.
1813 But the comparison we output must be unsigned.
1815 Also, for inequalities, VAL is no good; but if the signed
1816 comparison had *any* fixed result, it follows that the
1817 unsigned comparison just tests the sign in reverse
1818 (positive values are LE, negative ones GE).
1819 So we can generate an unsigned comparison
1820 against an extreme value of the signed type. */
1822 if (unsignedp
&& !unsignedp0
)
1829 primop1
= TYPE_MIN_VALUE (type
);
1835 primop1
= TYPE_MAX_VALUE (type
);
1839 type
= unsigned_type (type
);
1842 if (!max_gt
&& !unsignedp0
&& TREE_CODE (primop0
) != INTEGER_CST
)
1844 /* This is the case of (char)x >?< 0x80, which people used to use
1845 expecting old C compilers to change the 0x80 into -0x80. */
1846 if (val
== boolean_false_node
)
1847 warning ("comparison is always 0 due to limited range of data type");
1848 if (val
== boolean_true_node
)
1849 warning ("comparison is always 1 due to limited range of data type");
1852 if (!min_lt
&& unsignedp0
&& TREE_CODE (primop0
) != INTEGER_CST
)
1854 /* This is the case of (unsigned char)x >?< -1 or < 0. */
1855 if (val
== boolean_false_node
)
1856 warning ("comparison is always 0 due to limited range of data type");
1857 if (val
== boolean_true_node
)
1858 warning ("comparison is always 1 due to limited range of data type");
1863 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
1864 if (TREE_SIDE_EFFECTS (primop0
))
1865 return build (COMPOUND_EXPR
, TREE_TYPE (val
), primop0
, val
);
1869 /* Value is not predetermined, but do the comparison
1870 in the type of the operand that is not constant.
1871 TYPE is already properly set. */
1873 else if (real1
&& real2
1874 && (TYPE_PRECISION (TREE_TYPE (primop0
))
1875 == TYPE_PRECISION (TREE_TYPE (primop1
))))
1876 type
= TREE_TYPE (primop0
);
1878 /* If args' natural types are both narrower than nominal type
1879 and both extend in the same manner, compare them
1880 in the type of the wider arg.
1881 Otherwise must actually extend both to the nominal
1882 common type lest different ways of extending
1884 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
1886 else if (unsignedp0
== unsignedp1
&& real1
== real2
1887 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
)
1888 && TYPE_PRECISION (TREE_TYPE (primop1
)) < TYPE_PRECISION (*restype_ptr
))
1890 type
= common_type (TREE_TYPE (primop0
), TREE_TYPE (primop1
));
1891 type
= signed_or_unsigned_type (unsignedp0
1892 || TREE_UNSIGNED (*restype_ptr
),
1894 /* Make sure shorter operand is extended the right way
1895 to match the longer operand. */
1896 primop0
= convert (signed_or_unsigned_type (unsignedp0
, TREE_TYPE (primop0
)),
1898 primop1
= convert (signed_or_unsigned_type (unsignedp1
, TREE_TYPE (primop1
)),
1903 /* Here we must do the comparison on the nominal type
1904 using the args exactly as we received them. */
1905 type
= *restype_ptr
;
1909 if (!real1
&& !real2
&& integer_zerop (primop1
)
1910 && TREE_UNSIGNED (*restype_ptr
))
1916 /* All unsigned values are >= 0, so we warn if extra warnings
1917 are requested. However, if OP0 is a constant that is
1918 >= 0, the signedness of the comparison isn't an issue,
1919 so suppress the warning. */
1921 && ! (TREE_CODE (primop0
) == INTEGER_CST
1922 && ! TREE_OVERFLOW (convert (signed_type (type
),
1924 warning ("unsigned value >= 0 is always 1");
1925 value
= boolean_true_node
;
1930 && ! (TREE_CODE (primop0
) == INTEGER_CST
1931 && ! TREE_OVERFLOW (convert (signed_type (type
),
1933 warning ("unsigned value < 0 is always 0");
1934 value
= boolean_false_node
;
1939 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
1940 if (TREE_SIDE_EFFECTS (primop0
))
1941 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
1948 *op0_ptr
= convert (type
, primop0
);
1949 *op1_ptr
= convert (type
, primop1
);
1951 *restype_ptr
= boolean_type_node
;
1956 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
1957 or validate its data type for an `if' or `while' statement or ?..: exp.
1959 This preparation consists of taking the ordinary
1960 representation of an expression expr and producing a valid tree
1961 boolean expression describing whether expr is nonzero. We could
1962 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
1963 but we optimize comparisons, &&, ||, and !.
1965 The resulting type should always be `boolean_type_node'. */
1968 truthvalue_conversion (expr
)
1971 if (TREE_CODE (expr
) == ERROR_MARK
)
1974 #if 0 /* This appears to be wrong for C++. */
1975 /* These really should return error_mark_node after 2.4 is stable.
1976 But not all callers handle ERROR_MARK properly. */
1977 switch (TREE_CODE (TREE_TYPE (expr
)))
1980 error ("struct type value used where scalar is required");
1981 return boolean_false_node
;
1984 error ("union type value used where scalar is required");
1985 return boolean_false_node
;
1988 error ("array type value used where scalar is required");
1989 return boolean_false_node
;
1996 switch (TREE_CODE (expr
))
1998 /* It is simpler and generates better code to have only TRUTH_*_EXPR
1999 or comparison expressions as truth values at this level. */
2002 /* A one-bit unsigned bit-field is already acceptable. */
2003 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr
, 1)))
2004 && TREE_UNSIGNED (TREE_OPERAND (expr
, 1)))
2010 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2011 or comparison expressions as truth values at this level. */
2013 if (integer_zerop (TREE_OPERAND (expr
, 1)))
2014 return build_unary_op (TRUTH_NOT_EXPR
, TREE_OPERAND (expr
, 0), 0);
2016 case NE_EXPR
: case LE_EXPR
: case GE_EXPR
: case LT_EXPR
: case GT_EXPR
:
2017 case TRUTH_ANDIF_EXPR
:
2018 case TRUTH_ORIF_EXPR
:
2019 case TRUTH_AND_EXPR
:
2021 case TRUTH_XOR_EXPR
:
2022 TREE_TYPE (expr
) = boolean_type_node
;
2029 return integer_zerop (expr
) ? boolean_false_node
: boolean_true_node
;
2032 return real_zerop (expr
) ? boolean_false_node
: boolean_true_node
;
2035 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 0)))
2036 return build (COMPOUND_EXPR
, boolean_type_node
,
2037 TREE_OPERAND (expr
, 0), boolean_true_node
);
2039 return boolean_true_node
;
2042 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1))
2043 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
2044 truthvalue_conversion (TREE_OPERAND (expr
, 0)),
2045 truthvalue_conversion (TREE_OPERAND (expr
, 1)),
2052 /* These don't change whether an object is non-zero or zero. */
2053 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
2057 /* These don't change whether an object is zero or non-zero, but
2058 we can't ignore them if their second arg has side-effects. */
2059 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1)))
2060 return build (COMPOUND_EXPR
, boolean_type_node
, TREE_OPERAND (expr
, 1),
2061 truthvalue_conversion (TREE_OPERAND (expr
, 0)));
2063 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
2066 /* Distribute the conversion into the arms of a COND_EXPR. */
2067 return fold (build (COND_EXPR
, boolean_type_node
, TREE_OPERAND (expr
, 0),
2068 truthvalue_conversion (TREE_OPERAND (expr
, 1)),
2069 truthvalue_conversion (TREE_OPERAND (expr
, 2))));
2072 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2073 since that affects how `default_conversion' will behave. */
2074 if (TREE_CODE (TREE_TYPE (expr
)) == REFERENCE_TYPE
2075 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == REFERENCE_TYPE
)
2077 /* fall through... */
2079 /* If this is widening the argument, we can ignore it. */
2080 if (TYPE_PRECISION (TREE_TYPE (expr
))
2081 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr
, 0))))
2082 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
2086 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2088 if (TARGET_FLOAT_FORMAT
== IEEE_FLOAT_FORMAT
2089 && TREE_CODE (TREE_TYPE (expr
)) == REAL_TYPE
)
2091 /* fall through... */
2093 /* This and MINUS_EXPR can be changed into a comparison of the
2095 if (TREE_TYPE (TREE_OPERAND (expr
, 0))
2096 == TREE_TYPE (TREE_OPERAND (expr
, 1)))
2097 return build_binary_op (NE_EXPR
, TREE_OPERAND (expr
, 0),
2098 TREE_OPERAND (expr
, 1), 1);
2099 return build_binary_op (NE_EXPR
, TREE_OPERAND (expr
, 0),
2100 fold (build1 (NOP_EXPR
,
2101 TREE_TYPE (TREE_OPERAND (expr
, 0)),
2102 TREE_OPERAND (expr
, 1))), 1);
2105 if (integer_onep (TREE_OPERAND (expr
, 1)))
2109 if (warn_parentheses
&& C_EXP_ORIGINAL_CODE (expr
) == MODIFY_EXPR
)
2110 warning ("suggest parentheses around assignment used as truth value");
2114 if (TREE_CODE (TREE_TYPE (expr
)) == COMPLEX_TYPE
)
2115 return (build_binary_op
2116 ((TREE_SIDE_EFFECTS (expr
)
2117 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
2118 truthvalue_conversion (build_unary_op (REALPART_EXPR
, expr
, 0)),
2119 truthvalue_conversion (build_unary_op (IMAGPART_EXPR
, expr
, 0)),
2122 return build_binary_op (NE_EXPR
, expr
, integer_zero_node
, 1);
2125 /* Read the rest of a #-directive from input stream FINPUT.
2126 In normal use, the directive name and the white space after it
2127 have already been read, so they won't be included in the result.
2128 We allow for the fact that the directive line may contain
2129 a newline embedded within a character or string literal which forms
2130 a part of the directive.
2132 The value is a string in a reusable buffer. It remains valid
2133 only until the next time this function is called. */
2136 get_directive_line (finput
)
2137 register FILE *finput
;
2139 static char *directive_buffer
= NULL
;
2140 static unsigned buffer_length
= 0;
2142 register char *buffer_limit
;
2143 register int looking_for
= 0;
2144 register int char_escaped
= 0;
2146 if (buffer_length
== 0)
2148 directive_buffer
= (char *)xmalloc (128);
2149 buffer_length
= 128;
2152 buffer_limit
= &directive_buffer
[buffer_length
];
2154 for (p
= directive_buffer
; ; )
2158 /* Make buffer bigger if it is full. */
2159 if (p
>= buffer_limit
)
2161 register unsigned bytes_used
= (p
- directive_buffer
);
2165 = (char *)xrealloc (directive_buffer
, buffer_length
);
2166 p
= &directive_buffer
[bytes_used
];
2167 buffer_limit
= &directive_buffer
[buffer_length
];
2172 /* Discard initial whitespace. */
2173 if ((c
== ' ' || c
== '\t') && p
== directive_buffer
)
2176 /* Detect the end of the directive. */
2177 if (c
== '\n' && looking_for
== 0)
2186 return directive_buffer
;
2188 /* Handle string and character constant syntax. */
2191 if (looking_for
== c
&& !char_escaped
)
2192 looking_for
= 0; /* Found terminator... stop looking. */
2195 if (c
== '\'' || c
== '"')
2196 looking_for
= c
; /* Don't stop buffering until we see another
2197 another one of these (or an EOF). */
2199 /* Handle backslash. */
2200 char_escaped
= (c
== '\\' && ! char_escaped
);
2204 /* Make a variant type in the proper way for C/C++, propagating qualifiers
2205 down to the element type of an array. */
2208 c_build_type_variant (type
, constp
, volatilep
)
2210 int constp
, volatilep
;
2212 if (TREE_CODE (type
) == ARRAY_TYPE
)
2214 tree real_main_variant
= TYPE_MAIN_VARIANT (type
);
2216 push_obstacks (TYPE_OBSTACK (real_main_variant
),
2217 TYPE_OBSTACK (real_main_variant
));
2218 type
= build_array_type (c_build_type_variant (TREE_TYPE (type
),
2220 TYPE_DOMAIN (type
));
2222 /* TYPE must be on same obstack as REAL_MAIN_VARIANT. If not,
2223 make a copy. (TYPE might have come from the hash table and
2224 REAL_MAIN_VARIANT might be in some function's obstack.) */
2226 if (TYPE_OBSTACK (type
) != TYPE_OBSTACK (real_main_variant
))
2228 type
= copy_node (type
);
2229 TYPE_POINTER_TO (type
) = TYPE_REFERENCE_TO (type
) = 0;
2232 TYPE_MAIN_VARIANT (type
) = real_main_variant
;
2235 return build_type_variant (type
, constp
, volatilep
);