1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 93-98, 1999 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
36 cpp_options parse_options
;
37 static enum cpp_token cpp_token
;
40 #ifndef WCHAR_TYPE_SIZE
42 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
44 #define WCHAR_TYPE_SIZE BITS_PER_WORD
48 extern struct obstack permanent_obstack
;
50 /* Nonzero means the expression being parsed will never be evaluated.
51 This is a count, since unevaluated expressions can nest. */
54 enum attrs
{A_PACKED
, A_NOCOMMON
, A_COMMON
, A_NORETURN
, A_CONST
, A_T_UNION
,
55 A_NO_CHECK_MEMORY_USAGE
, A_NO_INSTRUMENT_FUNCTION
,
56 A_CONSTRUCTOR
, A_DESTRUCTOR
, A_MODE
, A_SECTION
, A_ALIGNED
,
57 A_UNUSED
, A_FORMAT
, A_FORMAT_ARG
, A_WEAK
, A_ALIAS
};
59 enum format_type
{ printf_format_type
, scanf_format_type
,
60 strftime_format_type
};
62 static void declare_hidden_char_array
PROTO((const char *, const char *));
63 static void add_attribute
PROTO((enum attrs
, const char *,
65 static void init_attributes
PROTO((void));
66 static void record_function_format
PROTO((tree
, tree
, enum format_type
,
68 static void record_international_format
PROTO((tree
, tree
, int));
69 static tree c_find_base_decl
PROTO((tree
));
70 static int default_valid_lang_attribute
PROTO ((tree
, tree
, tree
, tree
));
72 /* Keep a stack of if statements. We record the number of compound
73 statements seen up to the if keyword, as well as the line number
74 and file of the if. If a potentially ambiguous else is seen, that
75 fact is recorded; the warning is issued when we can be sure that
76 the enclosing if statement does not have an else branch. */
84 static void tfaff
PROTO((void));
86 static if_elt
*if_stack
;
88 /* Amount of space in the if statement stack. */
89 static int if_stack_space
= 0;
92 static int if_stack_pointer
= 0;
94 /* Generate RTL for the start of an if-then, and record the start of it
95 for ambiguous else detection. */
98 c_expand_start_cond (cond
, exitflag
, compstmt_count
)
103 /* Make sure there is enough space on the stack. */
104 if (if_stack_space
== 0)
107 if_stack
= (if_elt
*)xmalloc (10 * sizeof (if_elt
));
109 else if (if_stack_space
== if_stack_pointer
)
111 if_stack_space
+= 10;
112 if_stack
= (if_elt
*)xrealloc (if_stack
, if_stack_space
* sizeof (if_elt
));
115 /* Record this if statement. */
116 if_stack
[if_stack_pointer
].compstmt_count
= compstmt_count
;
117 if_stack
[if_stack_pointer
].file
= input_filename
;
118 if_stack
[if_stack_pointer
].line
= lineno
;
119 if_stack
[if_stack_pointer
].needs_warning
= 0;
122 expand_start_cond (cond
, exitflag
);
125 /* Generate RTL for the end of an if-then. Optionally warn if a nested
126 if statement had an ambiguous else clause. */
132 if (if_stack
[if_stack_pointer
].needs_warning
)
133 warning_with_file_and_line (if_stack
[if_stack_pointer
].file
,
134 if_stack
[if_stack_pointer
].line
,
135 "suggest explicit braces to avoid ambiguous `else'");
139 /* Generate RTL between the then-clause and the else-clause
140 of an if-then-else. */
143 c_expand_start_else ()
145 /* An ambiguous else warning must be generated for the enclosing if
146 statement, unless we see an else branch for that one, too. */
148 && if_stack_pointer
> 1
149 && (if_stack
[if_stack_pointer
- 1].compstmt_count
150 == if_stack
[if_stack_pointer
- 2].compstmt_count
))
151 if_stack
[if_stack_pointer
- 2].needs_warning
= 1;
153 /* Even if a nested if statement had an else branch, it can't be
154 ambiguous if this one also has an else. So don't warn in that
155 case. Also don't warn for any if statements nested in this else. */
156 if_stack
[if_stack_pointer
- 1].needs_warning
= 0;
157 if_stack
[if_stack_pointer
- 1].compstmt_count
--;
159 expand_start_else ();
162 /* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__. */
165 declare_function_name ()
167 const char *name
, *printable_name
;
169 if (current_function_decl
== NULL
)
172 printable_name
= "top level";
176 /* Allow functions to be nameless (such as artificial ones). */
177 if (DECL_NAME (current_function_decl
))
178 name
= IDENTIFIER_POINTER (DECL_NAME (current_function_decl
));
181 printable_name
= (*decl_printable_name
) (current_function_decl
, 2);
184 declare_hidden_char_array ("__FUNCTION__", name
);
185 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name
);
186 /* The ISO C people "of course" couldn't use __FUNCTION__ in the
187 ISO C 9x standard; instead a new variable is invented. */
188 declare_hidden_char_array ("__func__", name
);
192 declare_hidden_char_array (name
, value
)
193 const char *name
, *value
;
195 tree decl
, type
, init
;
198 /* If the default size of char arrays isn't big enough for the name,
199 or if we want to give warnings for large objects, make a bigger one. */
200 vlen
= strlen (value
) + 1;
201 type
= char_array_type_node
;
202 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))) < vlen
204 type
= build_array_type (char_type_node
,
205 build_index_type (build_int_2 (vlen
, 0)));
206 push_obstacks_nochange ();
207 decl
= build_decl (VAR_DECL
, get_identifier (name
), type
);
208 TREE_STATIC (decl
) = 1;
209 TREE_READONLY (decl
) = 1;
210 TREE_ASM_WRITTEN (decl
) = 1;
211 DECL_SOURCE_LINE (decl
) = 0;
212 DECL_ARTIFICIAL (decl
) = 1;
213 DECL_IN_SYSTEM_HEADER (decl
) = 1;
214 DECL_IGNORED_P (decl
) = 1;
215 init
= build_string (vlen
, value
);
216 TREE_TYPE (init
) = type
;
217 DECL_INITIAL (decl
) = init
;
218 finish_decl (pushdecl (decl
), init
, NULL_TREE
);
221 /* Given a chain of STRING_CST nodes,
222 concatenate them into one STRING_CST
223 and give it a suitable array-of-chars data type. */
226 combine_strings (strings
)
229 register tree value
, t
;
230 register int length
= 1;
233 int wchar_bytes
= TYPE_PRECISION (wchar_type_node
) / BITS_PER_UNIT
;
236 if (TREE_CHAIN (strings
))
238 /* More than one in the chain, so concatenate. */
239 register char *p
, *q
;
241 /* Don't include the \0 at the end of each substring,
242 except for the last one.
243 Count wide strings and ordinary strings separately. */
244 for (t
= strings
; t
; t
= TREE_CHAIN (t
))
246 if (TREE_TYPE (t
) == wchar_array_type_node
)
248 wide_length
+= (TREE_STRING_LENGTH (t
) - wchar_bytes
);
252 length
+= (TREE_STRING_LENGTH (t
) - 1);
255 /* If anything is wide, the non-wides will be converted,
256 which makes them take more space. */
258 length
= length
* wchar_bytes
+ wide_length
;
260 p
= savealloc (length
);
262 /* Copy the individual strings into the new combined string.
263 If the combined string is wide, convert the chars to ints
264 for any individual strings that are not wide. */
267 for (t
= strings
; t
; t
= TREE_CHAIN (t
))
269 int len
= (TREE_STRING_LENGTH (t
)
270 - ((TREE_TYPE (t
) == wchar_array_type_node
)
272 if ((TREE_TYPE (t
) == wchar_array_type_node
) == wide_flag
)
274 memcpy (q
, TREE_STRING_POINTER (t
), len
);
280 for (i
= 0; i
< len
; i
++)
282 if (WCHAR_TYPE_SIZE
== HOST_BITS_PER_SHORT
)
283 ((short *) q
)[i
] = TREE_STRING_POINTER (t
)[i
];
285 ((int *) q
)[i
] = TREE_STRING_POINTER (t
)[i
];
287 q
+= len
* wchar_bytes
;
293 for (i
= 0; i
< wchar_bytes
; i
++)
299 value
= make_node (STRING_CST
);
300 TREE_STRING_POINTER (value
) = p
;
301 TREE_STRING_LENGTH (value
) = length
;
306 length
= TREE_STRING_LENGTH (value
);
307 if (TREE_TYPE (value
) == wchar_array_type_node
)
311 /* Compute the number of elements, for the array type. */
312 nchars
= wide_flag
? length
/ wchar_bytes
: length
;
314 /* Create the array type for the string constant.
315 -Wwrite-strings says make the string constant an array of const char
316 so that copying it to a non-const pointer will get a warning.
317 For C++, this is the standard behavior. */
318 if (flag_const_strings
319 && (! flag_traditional
&& ! flag_writable_strings
))
322 = build_type_variant (wide_flag
? wchar_type_node
: char_type_node
,
325 = build_array_type (elements
,
326 build_index_type (build_int_2 (nchars
- 1, 0)));
330 = build_array_type (wide_flag
? wchar_type_node
: char_type_node
,
331 build_index_type (build_int_2 (nchars
- 1, 0)));
333 TREE_READONLY (value
) = TREE_CONSTANT (value
) = ! flag_writable_strings
;
334 TREE_STATIC (value
) = 1;
338 /* To speed up processing of attributes, we maintain an array of
339 IDENTIFIER_NODES and the corresponding attribute types. */
341 /* Array to hold attribute information. */
343 static struct {enum attrs id
; tree name
; int min
, max
, decl_req
;} attrtab
[50];
345 static int attrtab_idx
= 0;
347 /* Add an entry to the attribute table above. */
350 add_attribute (id
, string
, min_len
, max_len
, decl_req
)
353 int min_len
, max_len
;
358 attrtab
[attrtab_idx
].id
= id
;
359 attrtab
[attrtab_idx
].name
= get_identifier (string
);
360 attrtab
[attrtab_idx
].min
= min_len
;
361 attrtab
[attrtab_idx
].max
= max_len
;
362 attrtab
[attrtab_idx
++].decl_req
= decl_req
;
364 sprintf (buf
, "__%s__", string
);
366 attrtab
[attrtab_idx
].id
= id
;
367 attrtab
[attrtab_idx
].name
= get_identifier (buf
);
368 attrtab
[attrtab_idx
].min
= min_len
;
369 attrtab
[attrtab_idx
].max
= max_len
;
370 attrtab
[attrtab_idx
++].decl_req
= decl_req
;
373 /* Initialize attribute table. */
378 add_attribute (A_PACKED
, "packed", 0, 0, 0);
379 add_attribute (A_NOCOMMON
, "nocommon", 0, 0, 1);
380 add_attribute (A_COMMON
, "common", 0, 0, 1);
381 add_attribute (A_NORETURN
, "noreturn", 0, 0, 1);
382 add_attribute (A_NORETURN
, "volatile", 0, 0, 1);
383 add_attribute (A_UNUSED
, "unused", 0, 0, 0);
384 add_attribute (A_CONST
, "const", 0, 0, 1);
385 add_attribute (A_T_UNION
, "transparent_union", 0, 0, 0);
386 add_attribute (A_CONSTRUCTOR
, "constructor", 0, 0, 1);
387 add_attribute (A_DESTRUCTOR
, "destructor", 0, 0, 1);
388 add_attribute (A_MODE
, "mode", 1, 1, 1);
389 add_attribute (A_SECTION
, "section", 1, 1, 1);
390 add_attribute (A_ALIGNED
, "aligned", 0, 1, 0);
391 add_attribute (A_FORMAT
, "format", 3, 3, 1);
392 add_attribute (A_FORMAT_ARG
, "format_arg", 1, 1, 1);
393 add_attribute (A_WEAK
, "weak", 0, 0, 1);
394 add_attribute (A_ALIAS
, "alias", 1, 1, 1);
395 add_attribute (A_NO_INSTRUMENT_FUNCTION
, "no_instrument_function", 0, 0, 1);
396 add_attribute (A_NO_CHECK_MEMORY_USAGE
, "no_check_memory_usage", 0, 0, 1);
399 /* Default implementation of valid_lang_attribute, below. By default, there
400 are no language-specific attributes. */
403 default_valid_lang_attribute (attr_name
, attr_args
, decl
, type
)
404 tree attr_name ATTRIBUTE_UNUSED
;
405 tree attr_args ATTRIBUTE_UNUSED
;
406 tree decl ATTRIBUTE_UNUSED
;
407 tree type ATTRIBUTE_UNUSED
;
412 /* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific
413 attribute for either declaration DECL or type TYPE and 0 otherwise. */
415 int (*valid_lang_attribute
) PROTO ((tree
, tree
, tree
, tree
))
416 = default_valid_lang_attribute
;
418 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
419 and install them in NODE, which is either a DECL (including a TYPE_DECL)
420 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
421 and declaration modifiers but before the declaration proper. */
424 decl_attributes (node
, attributes
, prefix_attributes
)
425 tree node
, attributes
, prefix_attributes
;
427 tree decl
= 0, type
= 0;
431 if (attrtab_idx
== 0)
434 if (TREE_CODE_CLASS (TREE_CODE (node
)) == 'd')
437 type
= TREE_TYPE (decl
);
438 is_type
= TREE_CODE (node
) == TYPE_DECL
;
440 else if (TREE_CODE_CLASS (TREE_CODE (node
)) == 't')
441 type
= node
, is_type
= 1;
443 #ifdef PRAGMA_INSERT_ATTRIBUTES
444 /* If the code in c-pragma.c wants to insert some attributes then
445 allow it to do so. Do this before allowing machine back ends to
446 insert attributes, so that they have the opportunity to override
447 anything done here. */
448 PRAGMA_INSERT_ATTRIBUTES (node
, & attributes
, & prefix_attributes
);
451 #ifdef INSERT_ATTRIBUTES
452 INSERT_ATTRIBUTES (node
, & attributes
, & prefix_attributes
);
455 attributes
= chainon (prefix_attributes
, attributes
);
457 for (a
= attributes
; a
; a
= TREE_CHAIN (a
))
459 tree name
= TREE_PURPOSE (a
);
460 tree args
= TREE_VALUE (a
);
464 for (i
= 0; i
< attrtab_idx
; i
++)
465 if (attrtab
[i
].name
== name
)
468 if (i
== attrtab_idx
)
470 if (! valid_machine_attribute (name
, args
, decl
, type
)
471 && ! (* valid_lang_attribute
) (name
, args
, decl
, type
))
472 warning ("`%s' attribute directive ignored",
473 IDENTIFIER_POINTER (name
));
475 type
= TREE_TYPE (decl
);
478 else if (attrtab
[i
].decl_req
&& decl
== 0)
480 warning ("`%s' attribute does not apply to types",
481 IDENTIFIER_POINTER (name
));
484 else if (list_length (args
) < attrtab
[i
].min
485 || list_length (args
) > attrtab
[i
].max
)
487 error ("wrong number of arguments specified for `%s' attribute",
488 IDENTIFIER_POINTER (name
));
497 TYPE_PACKED (type
) = 1;
498 else if (TREE_CODE (decl
) == FIELD_DECL
)
499 DECL_PACKED (decl
) = 1;
500 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
501 used for DECL_REGISTER. It wouldn't mean anything anyway. */
503 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
507 if (TREE_CODE (decl
) == VAR_DECL
)
508 DECL_COMMON (decl
) = 0;
510 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
514 if (TREE_CODE (decl
) == VAR_DECL
)
515 DECL_COMMON (decl
) = 1;
517 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
521 if (TREE_CODE (decl
) == FUNCTION_DECL
)
522 TREE_THIS_VOLATILE (decl
) = 1;
523 else if (TREE_CODE (type
) == POINTER_TYPE
524 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
525 TREE_TYPE (decl
) = type
527 (build_type_variant (TREE_TYPE (type
),
528 TREE_READONLY (TREE_TYPE (type
)), 1));
530 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
535 TREE_USED (type
) = 1;
536 else if (TREE_CODE (decl
) == PARM_DECL
537 || TREE_CODE (decl
) == VAR_DECL
538 || TREE_CODE (decl
) == FUNCTION_DECL
539 || TREE_CODE (decl
) == LABEL_DECL
)
540 TREE_USED (decl
) = 1;
542 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
546 if (TREE_CODE (decl
) == FUNCTION_DECL
)
547 TREE_READONLY (decl
) = 1;
548 else if (TREE_CODE (type
) == POINTER_TYPE
549 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
550 TREE_TYPE (decl
) = type
552 (build_type_variant (TREE_TYPE (type
), 1,
553 TREE_THIS_VOLATILE (TREE_TYPE (type
))));
555 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name
));
560 && TREE_CODE (type
) == UNION_TYPE
562 || (TYPE_FIELDS (type
) != 0
563 && TYPE_MODE (type
) == DECL_MODE (TYPE_FIELDS (type
)))))
564 TYPE_TRANSPARENT_UNION (type
) = 1;
565 else if (decl
!= 0 && TREE_CODE (decl
) == PARM_DECL
566 && TREE_CODE (type
) == UNION_TYPE
567 && TYPE_MODE (type
) == DECL_MODE (TYPE_FIELDS (type
)))
568 DECL_TRANSPARENT_UNION (decl
) = 1;
570 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
574 if (TREE_CODE (decl
) == FUNCTION_DECL
575 && TREE_CODE (type
) == FUNCTION_TYPE
576 && decl_function_context (decl
) == 0)
578 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
579 TREE_USED (decl
) = 1;
582 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
586 if (TREE_CODE (decl
) == FUNCTION_DECL
587 && TREE_CODE (type
) == FUNCTION_TYPE
588 && decl_function_context (decl
) == 0)
590 DECL_STATIC_DESTRUCTOR (decl
) = 1;
591 TREE_USED (decl
) = 1;
594 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
598 if (TREE_CODE (TREE_VALUE (args
)) != IDENTIFIER_NODE
)
599 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
603 const char *p
= IDENTIFIER_POINTER (TREE_VALUE (args
));
604 int len
= strlen (p
);
605 enum machine_mode mode
= VOIDmode
;
608 if (len
> 4 && p
[0] == '_' && p
[1] == '_'
609 && p
[len
- 1] == '_' && p
[len
- 2] == '_')
611 char *newp
= (char *) alloca (len
- 1);
613 strcpy (newp
, &p
[2]);
614 newp
[len
- 4] = '\0';
618 /* Give this decl a type with the specified mode.
619 First check for the special modes. */
620 if (! strcmp (p
, "byte"))
622 else if (!strcmp (p
, "word"))
624 else if (! strcmp (p
, "pointer"))
627 for (j
= 0; j
< NUM_MACHINE_MODES
; j
++)
628 if (!strcmp (p
, GET_MODE_NAME (j
)))
629 mode
= (enum machine_mode
) j
;
631 if (mode
== VOIDmode
)
632 error ("unknown machine mode `%s'", p
);
633 else if (0 == (typefm
= type_for_mode (mode
,
634 TREE_UNSIGNED (type
))))
635 error ("no data type for mode `%s'", p
);
638 TREE_TYPE (decl
) = type
= typefm
;
639 DECL_SIZE (decl
) = 0;
640 layout_decl (decl
, 0);
646 #ifdef ASM_OUTPUT_SECTION_NAME
647 if ((TREE_CODE (decl
) == FUNCTION_DECL
648 || TREE_CODE (decl
) == VAR_DECL
)
649 && TREE_CODE (TREE_VALUE (args
)) == STRING_CST
)
651 if (TREE_CODE (decl
) == VAR_DECL
652 && current_function_decl
!= NULL_TREE
653 && ! TREE_STATIC (decl
))
654 error_with_decl (decl
,
655 "section attribute cannot be specified for local variables");
656 /* The decl may have already been given a section attribute from
657 a previous declaration. Ensure they match. */
658 else if (DECL_SECTION_NAME (decl
) != NULL_TREE
659 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl
)),
660 TREE_STRING_POINTER (TREE_VALUE (args
))) != 0)
661 error_with_decl (node
,
662 "section of `%s' conflicts with previous declaration");
664 DECL_SECTION_NAME (decl
) = TREE_VALUE (args
);
667 error_with_decl (node
,
668 "section attribute not allowed for `%s'");
670 error_with_decl (node
,
671 "section attributes are not supported for this target");
678 = (args
? TREE_VALUE (args
)
679 : size_int (BIGGEST_ALIGNMENT
/ BITS_PER_UNIT
));
682 /* Strip any NOPs of any kind. */
683 while (TREE_CODE (align_expr
) == NOP_EXPR
684 || TREE_CODE (align_expr
) == CONVERT_EXPR
685 || TREE_CODE (align_expr
) == NON_LVALUE_EXPR
)
686 align_expr
= TREE_OPERAND (align_expr
, 0);
688 if (TREE_CODE (align_expr
) != INTEGER_CST
)
690 error ("requested alignment is not a constant");
694 align
= TREE_INT_CST_LOW (align_expr
) * BITS_PER_UNIT
;
696 if (exact_log2 (align
) == -1)
697 error ("requested alignment is not a power of 2");
699 TYPE_ALIGN (type
) = align
;
700 else if (TREE_CODE (decl
) != VAR_DECL
701 && TREE_CODE (decl
) != FIELD_DECL
)
702 error_with_decl (decl
,
703 "alignment may not be specified for `%s'");
705 DECL_ALIGN (decl
) = align
;
711 tree format_type_id
= TREE_VALUE (args
);
712 tree format_num_expr
= TREE_VALUE (TREE_CHAIN (args
));
713 tree first_arg_num_expr
714 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args
)));
717 enum format_type format_type
;
721 if (TREE_CODE (decl
) != FUNCTION_DECL
)
723 error_with_decl (decl
,
724 "argument format specified for non-function `%s'");
728 if (TREE_CODE (format_type_id
) != IDENTIFIER_NODE
)
730 error ("unrecognized format specifier");
735 const char *p
= IDENTIFIER_POINTER (format_type_id
);
737 if (!strcmp (p
, "printf") || !strcmp (p
, "__printf__"))
738 format_type
= printf_format_type
;
739 else if (!strcmp (p
, "scanf") || !strcmp (p
, "__scanf__"))
740 format_type
= scanf_format_type
;
741 else if (!strcmp (p
, "strftime")
742 || !strcmp (p
, "__strftime__"))
743 format_type
= strftime_format_type
;
746 warning ("`%s' is an unrecognized format function type", p
);
751 /* Strip any conversions from the string index and first arg number
752 and verify they are constants. */
753 while (TREE_CODE (format_num_expr
) == NOP_EXPR
754 || TREE_CODE (format_num_expr
) == CONVERT_EXPR
755 || TREE_CODE (format_num_expr
) == NON_LVALUE_EXPR
)
756 format_num_expr
= TREE_OPERAND (format_num_expr
, 0);
758 while (TREE_CODE (first_arg_num_expr
) == NOP_EXPR
759 || TREE_CODE (first_arg_num_expr
) == CONVERT_EXPR
760 || TREE_CODE (first_arg_num_expr
) == NON_LVALUE_EXPR
)
761 first_arg_num_expr
= TREE_OPERAND (first_arg_num_expr
, 0);
763 if (TREE_CODE (format_num_expr
) != INTEGER_CST
764 || TREE_CODE (first_arg_num_expr
) != INTEGER_CST
)
766 error ("format string has non-constant operand number");
770 format_num
= TREE_INT_CST_LOW (format_num_expr
);
771 first_arg_num
= TREE_INT_CST_LOW (first_arg_num_expr
);
772 if (first_arg_num
!= 0 && first_arg_num
<= format_num
)
774 error ("format string arg follows the args to be formatted");
778 /* If a parameter list is specified, verify that the format_num
779 argument is actually a string, in case the format attribute
781 argument
= TYPE_ARG_TYPES (type
);
784 for (arg_num
= 1; ; ++arg_num
)
786 if (argument
== 0 || arg_num
== format_num
)
788 argument
= TREE_CHAIN (argument
);
791 || TREE_CODE (TREE_VALUE (argument
)) != POINTER_TYPE
792 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument
)))
795 error ("format string arg not a string type");
798 if (first_arg_num
!= 0)
800 /* Verify that first_arg_num points to the last arg,
803 arg_num
++, argument
= TREE_CHAIN (argument
);
804 if (arg_num
!= first_arg_num
)
806 error ("args to be formatted is not ...");
812 record_function_format (DECL_NAME (decl
),
813 DECL_ASSEMBLER_NAME (decl
),
814 format_type
, format_num
, first_arg_num
);
820 tree format_num_expr
= TREE_VALUE (args
);
821 int format_num
, arg_num
;
824 if (TREE_CODE (decl
) != FUNCTION_DECL
)
826 error_with_decl (decl
,
827 "argument format specified for non-function `%s'");
831 /* Strip any conversions from the first arg number and verify it
833 while (TREE_CODE (format_num_expr
) == NOP_EXPR
834 || TREE_CODE (format_num_expr
) == CONVERT_EXPR
835 || TREE_CODE (format_num_expr
) == NON_LVALUE_EXPR
)
836 format_num_expr
= TREE_OPERAND (format_num_expr
, 0);
838 if (TREE_CODE (format_num_expr
) != INTEGER_CST
)
840 error ("format string has non-constant operand number");
844 format_num
= TREE_INT_CST_LOW (format_num_expr
);
846 /* If a parameter list is specified, verify that the format_num
847 argument is actually a string, in case the format attribute
849 argument
= TYPE_ARG_TYPES (type
);
852 for (arg_num
= 1; ; ++arg_num
)
854 if (argument
== 0 || arg_num
== format_num
)
856 argument
= TREE_CHAIN (argument
);
859 || TREE_CODE (TREE_VALUE (argument
)) != POINTER_TYPE
860 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument
)))
863 error ("format string arg not a string type");
868 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl
))) != POINTER_TYPE
869 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl
))))
872 error ("function does not return string type");
876 record_international_format (DECL_NAME (decl
),
877 DECL_ASSEMBLER_NAME (decl
),
887 if ((TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_INITIAL (decl
))
888 || (TREE_CODE (decl
) != FUNCTION_DECL
&& ! DECL_EXTERNAL (decl
)))
889 error_with_decl (decl
,
890 "`%s' defined both normally and as an alias");
891 else if (decl_function_context (decl
) == 0)
895 id
= TREE_VALUE (args
);
896 if (TREE_CODE (id
) != STRING_CST
)
898 error ("alias arg not a string");
901 id
= get_identifier (TREE_STRING_POINTER (id
));
903 if (TREE_CODE (decl
) == FUNCTION_DECL
)
904 DECL_INITIAL (decl
) = error_mark_node
;
906 DECL_EXTERNAL (decl
) = 0;
907 assemble_alias (decl
, id
);
910 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
913 case A_NO_CHECK_MEMORY_USAGE
:
914 if (TREE_CODE (decl
) != FUNCTION_DECL
)
916 error_with_decl (decl
,
917 "`%s' attribute applies only to functions",
918 IDENTIFIER_POINTER (name
));
920 else if (DECL_INITIAL (decl
))
922 error_with_decl (decl
,
923 "can't set `%s' attribute after definition",
924 IDENTIFIER_POINTER (name
));
927 DECL_NO_CHECK_MEMORY_USAGE (decl
) = 1;
930 case A_NO_INSTRUMENT_FUNCTION
:
931 if (TREE_CODE (decl
) != FUNCTION_DECL
)
933 error_with_decl (decl
,
934 "`%s' attribute applies only to functions",
935 IDENTIFIER_POINTER (name
));
937 else if (DECL_INITIAL (decl
))
939 error_with_decl (decl
,
940 "can't set `%s' attribute after definition",
941 IDENTIFIER_POINTER (name
));
944 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
950 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
951 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
953 The head of the declspec list is stored in DECLSPECS.
954 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
956 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
957 the list elements. We drop the containing TREE_LIST nodes and link the
958 resulting attributes together the way decl_attributes expects them. */
961 split_specs_attrs (specs_attrs
, declspecs
, prefix_attributes
)
963 tree
*declspecs
, *prefix_attributes
;
965 tree t
, s
, a
, next
, specs
, attrs
;
967 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
968 if (specs_attrs
!= NULL_TREE
969 && TREE_CODE (specs_attrs
) != TREE_LIST
)
971 *declspecs
= specs_attrs
;
972 *prefix_attributes
= NULL_TREE
;
976 /* Remember to keep the lists in the same order, element-wise. */
978 specs
= s
= NULL_TREE
;
979 attrs
= a
= NULL_TREE
;
980 for (t
= specs_attrs
; t
; t
= next
)
982 next
= TREE_CHAIN (t
);
983 /* Declspecs have a non-NULL TREE_VALUE. */
984 if (TREE_VALUE (t
) != NULL_TREE
)
986 if (specs
== NULL_TREE
)
996 if (attrs
== NULL_TREE
)
997 attrs
= a
= TREE_PURPOSE (t
);
1000 TREE_CHAIN (a
) = TREE_PURPOSE (t
);
1001 a
= TREE_PURPOSE (t
);
1003 /* More attrs can be linked here, move A to the end. */
1004 while (TREE_CHAIN (a
) != NULL_TREE
)
1009 /* Terminate the lists. */
1011 TREE_CHAIN (s
) = NULL_TREE
;
1013 TREE_CHAIN (a
) = NULL_TREE
;
1017 *prefix_attributes
= attrs
;
1020 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1021 This function is used by the parser when a rule will accept attributes
1022 in a particular position, but we don't want to support that just yet.
1024 A warning is issued for every ignored attribute. */
1027 strip_attrs (specs_attrs
)
1032 split_specs_attrs (specs_attrs
, &specs
, &attrs
);
1036 warning ("`%s' attribute ignored",
1037 IDENTIFIER_POINTER (TREE_PURPOSE (attrs
)));
1038 attrs
= TREE_CHAIN (attrs
);
1044 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1045 a parameter list. */
1047 #define T_I &integer_type_node
1048 #define T_L &long_integer_type_node
1049 #define T_LL &long_long_integer_type_node
1050 #define T_S &short_integer_type_node
1051 #define T_UI &unsigned_type_node
1052 #define T_UL &long_unsigned_type_node
1053 #define T_ULL &long_long_unsigned_type_node
1054 #define T_US &short_unsigned_type_node
1055 #define T_F &float_type_node
1056 #define T_D &double_type_node
1057 #define T_LD &long_double_type_node
1058 #define T_C &char_type_node
1059 #define T_UC &unsigned_char_type_node
1060 #define T_V &void_type_node
1061 #define T_W &wchar_type_node
1062 #define T_ST &sizetype
1065 const char *format_chars
;
1067 /* Type of argument if no length modifier is used. */
1069 /* Type of argument if length modifier for shortening to byte is used.
1070 If NULL, then this modifier is not allowed. */
1072 /* Type of argument if length modifier for shortening is used.
1073 If NULL, then this modifier is not allowed. */
1075 /* Type of argument if length modifier `l' is used.
1076 If NULL, then this modifier is not allowed. */
1078 /* Type of argument if length modifier `q' or `ll' is used.
1079 If NULL, then this modifier is not allowed. */
1081 /* Type of argument if length modifier `L' is used.
1082 If NULL, then this modifier is not allowed. */
1084 /* Type of argument if length modifier `Z' is used.
1085 If NULL, then this modifier is not allowed. */
1087 /* List of other modifier characters allowed with these options. */
1088 const char *flag_chars
;
1091 static format_char_info print_char_table
[] = {
1092 { "di", 0, T_I
, T_I
, T_I
, T_L
, T_LL
, T_LL
, T_ST
, "-wp0 +" },
1093 { "oxX", 0, T_UI
, T_UI
, T_UI
, T_UL
, T_ULL
, T_ULL
, T_ST
, "-wp0#" },
1094 { "u", 0, T_UI
, T_UI
, T_UI
, T_UL
, T_ULL
, T_ULL
, T_ST
, "-wp0" },
1095 /* A GNU extension. */
1096 { "m", 0, T_V
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-wp" },
1097 { "feEgGaA", 0, T_D
, NULL
, NULL
, NULL
, NULL
, T_LD
, NULL
, "-wp0 +#" },
1098 { "c", 0, T_I
, NULL
, NULL
, T_W
, NULL
, NULL
, NULL
, "-w" },
1099 { "C", 0, T_W
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-w" },
1100 { "s", 1, T_C
, NULL
, NULL
, T_W
, NULL
, NULL
, NULL
, "-wp" },
1101 { "S", 1, T_W
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-wp" },
1102 { "p", 1, T_V
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-w" },
1103 { "n", 1, T_I
, NULL
, T_S
, T_L
, T_LL
, NULL
, NULL
, "" },
1104 { NULL
, 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
}
1107 static format_char_info scan_char_table
[] = {
1108 { "di", 1, T_I
, T_C
, T_S
, T_L
, T_LL
, T_LL
, NULL
, "*" },
1109 { "ouxX", 1, T_UI
, T_UC
, T_US
, T_UL
, T_ULL
, T_ULL
, NULL
, "*" },
1110 { "efgEGaA", 1, T_F
, NULL
, NULL
, T_D
, NULL
, T_LD
, NULL
, "*" },
1111 { "c", 1, T_C
, NULL
, NULL
, T_W
, NULL
, NULL
, NULL
, "*" },
1112 { "s", 1, T_C
, NULL
, NULL
, T_W
, NULL
, NULL
, NULL
, "*a" },
1113 { "[", 1, T_C
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "*a" },
1114 { "C", 1, T_W
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "*" },
1115 { "S", 1, T_W
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "*a" },
1116 { "p", 2, T_V
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "*" },
1117 { "n", 1, T_I
, T_C
, T_S
, T_L
, T_LL
, NULL
, NULL
, "" },
1118 { NULL
, 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
}
1121 /* Handle format characters recognized by glibc's strftime.c.
1122 '2' - MUST do years as only two digits
1123 '3' - MAY do years as only two digits (depending on locale)
1124 'E' - E modifier is acceptable
1125 'O' - O modifier is acceptable to Standard C
1126 'o' - O modifier is acceptable as a GNU extension
1127 'G' - other GNU extensions */
1129 static format_char_info time_char_table
[] = {
1130 { "y", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "2EO-_0w" },
1131 { "D", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "2" },
1132 { "g", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "2O-_0w" },
1133 { "cx", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "3E" },
1134 { "%RTXnrt", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "" },
1135 { "P", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "G" },
1136 { "HIMSUWdemw", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-_0Ow" },
1137 { "Vju", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-_0Oow" },
1138 { "Gklsz", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-_0OGw" },
1139 { "ABZa", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "^#" },
1140 { "p", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "#" },
1141 { "bh", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "^" },
1142 { "CY", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-_0EOw" },
1143 { NULL
, 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
}
1146 typedef struct function_format_info
1148 struct function_format_info
*next
; /* next structure on the list */
1149 tree name
; /* identifier such as "printf" */
1150 tree assembler_name
; /* optional mangled identifier (for C++) */
1151 enum format_type format_type
; /* type of format (printf, scanf, etc.) */
1152 int format_num
; /* number of format argument */
1153 int first_arg_num
; /* number of first arg (zero for varargs) */
1154 } function_format_info
;
1156 static function_format_info
*function_format_list
= NULL
;
1158 typedef struct international_format_info
1160 struct international_format_info
*next
; /* next structure on the list */
1161 tree name
; /* identifier such as "gettext" */
1162 tree assembler_name
; /* optional mangled identifier (for C++) */
1163 int format_num
; /* number of format argument */
1164 } international_format_info
;
1166 static international_format_info
*international_format_list
= NULL
;
1168 static void check_format_info
PROTO((function_format_info
*, tree
));
1170 /* Initialize the table of functions to perform format checking on.
1171 The ANSI functions are always checked (whether <stdio.h> is
1172 included or not), since it is common to call printf without
1173 including <stdio.h>. There shouldn't be a problem with this,
1174 since ANSI reserves these function names whether you include the
1175 header file or not. In any case, the checking is harmless.
1177 Also initialize the name of function that modify the format string for
1178 internationalization purposes. */
1181 init_function_format_info ()
1183 record_function_format (get_identifier ("printf"), NULL_TREE
,
1184 printf_format_type
, 1, 2);
1185 record_function_format (get_identifier ("fprintf"), NULL_TREE
,
1186 printf_format_type
, 2, 3);
1187 record_function_format (get_identifier ("sprintf"), NULL_TREE
,
1188 printf_format_type
, 2, 3);
1189 record_function_format (get_identifier ("scanf"), NULL_TREE
,
1190 scanf_format_type
, 1, 2);
1191 record_function_format (get_identifier ("fscanf"), NULL_TREE
,
1192 scanf_format_type
, 2, 3);
1193 record_function_format (get_identifier ("sscanf"), NULL_TREE
,
1194 scanf_format_type
, 2, 3);
1195 record_function_format (get_identifier ("vprintf"), NULL_TREE
,
1196 printf_format_type
, 1, 0);
1197 record_function_format (get_identifier ("vfprintf"), NULL_TREE
,
1198 printf_format_type
, 2, 0);
1199 record_function_format (get_identifier ("vsprintf"), NULL_TREE
,
1200 printf_format_type
, 2, 0);
1201 record_function_format (get_identifier ("strftime"), NULL_TREE
,
1202 strftime_format_type
, 3, 0);
1204 record_international_format (get_identifier ("gettext"), NULL_TREE
, 1);
1205 record_international_format (get_identifier ("dgettext"), NULL_TREE
, 2);
1206 record_international_format (get_identifier ("dcgettext"), NULL_TREE
, 2);
1209 /* Record information for argument format checking. FUNCTION_IDENT is
1210 the identifier node for the name of the function to check (its decl
1211 need not exist yet).
1212 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
1213 of the argument which is the format control string (starting from 1).
1214 FIRST_ARG_NUM is the number of the first actual argument to check
1215 against the format string, or zero if no checking is not be done
1216 (e.g. for varargs such as vfprintf). */
1219 record_function_format (name
, assembler_name
, format_type
,
1220 format_num
, first_arg_num
)
1222 tree assembler_name
;
1223 enum format_type format_type
;
1227 function_format_info
*info
;
1229 /* Re-use existing structure if it's there. */
1231 for (info
= function_format_list
; info
; info
= info
->next
)
1233 if (info
->name
== name
&& info
->assembler_name
== assembler_name
)
1238 info
= (function_format_info
*) xmalloc (sizeof (function_format_info
));
1239 info
->next
= function_format_list
;
1240 function_format_list
= info
;
1243 info
->assembler_name
= assembler_name
;
1246 info
->format_type
= format_type
;
1247 info
->format_num
= format_num
;
1248 info
->first_arg_num
= first_arg_num
;
1251 /* Record information for the names of function that modify the format
1252 argument to format functions. FUNCTION_IDENT is the identifier node for
1253 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1254 the number of the argument which is the format control string (starting
1258 record_international_format (name
, assembler_name
, format_num
)
1260 tree assembler_name
;
1263 international_format_info
*info
;
1265 /* Re-use existing structure if it's there. */
1267 for (info
= international_format_list
; info
; info
= info
->next
)
1269 if (info
->name
== name
&& info
->assembler_name
== assembler_name
)
1276 = (international_format_info
*)
1277 xmalloc (sizeof (international_format_info
));
1278 info
->next
= international_format_list
;
1279 international_format_list
= info
;
1282 info
->assembler_name
= assembler_name
;
1285 info
->format_num
= format_num
;
1291 warning ("too few arguments for format");
1294 /* Check the argument list of a call to printf, scanf, etc.
1295 NAME is the function identifier.
1296 ASSEMBLER_NAME is the function's assembler identifier.
1297 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1298 PARAMS is the list of argument values. */
1301 check_function_format (name
, assembler_name
, params
)
1303 tree assembler_name
;
1306 function_format_info
*info
;
1308 /* See if this function is a format function. */
1309 for (info
= function_format_list
; info
; info
= info
->next
)
1311 if (info
->assembler_name
1312 ? (info
->assembler_name
== assembler_name
)
1313 : (info
->name
== name
))
1315 /* Yup; check it. */
1316 check_format_info (info
, params
);
1322 /* Check the argument list of a call to printf, scanf, etc.
1323 INFO points to the function_format_info structure.
1324 PARAMS is the list of argument values. */
1327 check_format_info (info
, params
)
1328 function_format_info
*info
;
1333 int suppressed
, wide
, precise
;
1334 int length_char
= 0;
1341 tree first_fillin_param
;
1342 const char *format_chars
;
1343 format_char_info
*fci
= NULL
;
1345 int has_operand_number
= 0;
1347 /* Skip to format argument. If the argument isn't available, there's
1348 no work for us to do; prototype checking will catch the problem. */
1349 for (arg_num
= 1; ; ++arg_num
)
1353 if (arg_num
== info
->format_num
)
1355 params
= TREE_CHAIN (params
);
1357 format_tree
= TREE_VALUE (params
);
1358 params
= TREE_CHAIN (params
);
1359 if (format_tree
== 0)
1362 /* We can only check the format if it's a string constant. */
1363 while (TREE_CODE (format_tree
) == NOP_EXPR
)
1364 format_tree
= TREE_OPERAND (format_tree
, 0); /* strip coercion */
1366 if (TREE_CODE (format_tree
) == CALL_EXPR
1367 && TREE_CODE (TREE_OPERAND (format_tree
, 0)) == ADDR_EXPR
1368 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree
, 0), 0))
1371 tree function
= TREE_OPERAND (TREE_OPERAND (format_tree
, 0), 0);
1373 /* See if this is a call to a known internationalization function
1374 that modifies the format arg. */
1375 international_format_info
*info
;
1377 for (info
= international_format_list
; info
; info
= info
->next
)
1378 if (info
->assembler_name
1379 ? (info
->assembler_name
== DECL_ASSEMBLER_NAME (function
))
1380 : (info
->name
== DECL_NAME (function
)))
1385 for (inner_args
= TREE_OPERAND (format_tree
, 1), i
= 1;
1387 inner_args
= TREE_CHAIN (inner_args
), i
++)
1388 if (i
== info
->format_num
)
1390 format_tree
= TREE_VALUE (inner_args
);
1392 while (TREE_CODE (format_tree
) == NOP_EXPR
)
1393 format_tree
= TREE_OPERAND (format_tree
, 0);
1398 if (integer_zerop (format_tree
))
1400 warning ("null format string");
1403 if (TREE_CODE (format_tree
) != ADDR_EXPR
)
1405 format_tree
= TREE_OPERAND (format_tree
, 0);
1406 if (TREE_CODE (format_tree
) != STRING_CST
)
1408 format_chars
= TREE_STRING_POINTER (format_tree
);
1409 format_length
= TREE_STRING_LENGTH (format_tree
);
1410 if (format_length
<= 1)
1411 warning ("zero-length format string");
1412 if (format_chars
[--format_length
] != 0)
1414 warning ("unterminated format string");
1417 /* Skip to first argument to check. */
1418 while (arg_num
+ 1 < info
->first_arg_num
)
1422 params
= TREE_CHAIN (params
);
1426 first_fillin_param
= params
;
1430 if (*format_chars
== 0)
1432 if (format_chars
- TREE_STRING_POINTER (format_tree
) != format_length
)
1433 warning ("embedded `\\0' in format");
1434 if (info
->first_arg_num
!= 0 && params
!= 0 && ! has_operand_number
)
1435 warning ("too many arguments for format");
1438 if (*format_chars
++ != '%')
1440 if (*format_chars
== 0)
1442 warning ("spurious trailing `%%' in format");
1445 if (*format_chars
== '%')
1451 suppressed
= wide
= precise
= FALSE
;
1452 if (info
->format_type
== scanf_format_type
)
1454 suppressed
= *format_chars
== '*';
1457 while (ISDIGIT (*format_chars
))
1460 else if (info
->format_type
== strftime_format_type
)
1462 while (*format_chars
!= 0 && index ("_-0^#", *format_chars
) != 0)
1465 warning ("ANSI C does not support the strftime `%c' flag",
1467 if (index (flag_chars
, *format_chars
) != 0)
1469 warning ("repeated `%c' flag in format",
1475 i
= strlen (flag_chars
);
1476 flag_chars
[i
++] = *format_chars
++;
1480 while (ISDIGIT ((unsigned char) *format_chars
))
1485 if (wide
&& pedantic
)
1486 warning ("ANSI C does not support strftime format width");
1487 if (*format_chars
== 'E' || *format_chars
== 'O')
1489 i
= strlen (flag_chars
);
1490 flag_chars
[i
++] = *format_chars
++;
1492 if (*format_chars
== 'E' || *format_chars
== 'O')
1494 warning ("multiple E/O modifiers in format");
1495 while (*format_chars
== 'E' || *format_chars
== 'O')
1500 else if (info
->format_type
== printf_format_type
)
1502 /* See if we have a number followed by a dollar sign. If we do,
1503 it is an operand number, so set PARAMS to that operand. */
1504 if (*format_chars
>= '0' && *format_chars
<= '9')
1506 const char *p
= format_chars
;
1508 while (*p
>= '0' && *p
++ <= '9')
1513 int opnum
= atoi (format_chars
);
1515 params
= first_fillin_param
;
1516 format_chars
= p
+ 1;
1517 has_operand_number
= 1;
1519 for (i
= 1; i
< opnum
&& params
!= 0; i
++)
1520 params
= TREE_CHAIN (params
);
1522 if (opnum
== 0 || params
== 0)
1524 warning ("operand number out of range in format");
1530 while (*format_chars
!= 0 && index (" +#0-", *format_chars
) != 0)
1532 if (index (flag_chars
, *format_chars
) != 0)
1533 warning ("repeated `%c' flag in format", *format_chars
++);
1536 i
= strlen (flag_chars
);
1537 flag_chars
[i
++] = *format_chars
++;
1541 /* "If the space and + flags both appear,
1542 the space flag will be ignored." */
1543 if (index (flag_chars
, ' ') != 0
1544 && index (flag_chars
, '+') != 0)
1545 warning ("use of both ` ' and `+' flags in format");
1546 /* "If the 0 and - flags both appear,
1547 the 0 flag will be ignored." */
1548 if (index (flag_chars
, '0') != 0
1549 && index (flag_chars
, '-') != 0)
1550 warning ("use of both `0' and `-' flags in format");
1551 if (*format_chars
== '*')
1554 /* "...a field width...may be indicated by an asterisk.
1555 In this case, an int argument supplies the field width..." */
1562 if (info
->first_arg_num
!= 0)
1564 cur_param
= TREE_VALUE (params
);
1565 params
= TREE_CHAIN (params
);
1567 /* size_t is generally not valid here.
1568 It will work on most machines, because size_t and int
1569 have the same mode. But might as well warn anyway,
1570 since it will fail on other machines. */
1571 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1572 != integer_type_node
)
1574 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1575 != unsigned_type_node
))
1576 warning ("field width is not type int (arg %d)", arg_num
);
1581 while (ISDIGIT (*format_chars
))
1587 if (*format_chars
== '.')
1591 if (*format_chars
!= '*' && !ISDIGIT (*format_chars
))
1592 warning ("`.' not followed by `*' or digit in format");
1593 /* "...a...precision...may be indicated by an asterisk.
1594 In this case, an int argument supplies the...precision." */
1595 if (*format_chars
== '*')
1597 if (info
->first_arg_num
!= 0)
1605 cur_param
= TREE_VALUE (params
);
1606 params
= TREE_CHAIN (params
);
1608 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1609 != integer_type_node
)
1610 warning ("field width is not type int (arg %d)",
1616 while (ISDIGIT (*format_chars
))
1624 if (info
->format_type
!= strftime_format_type
)
1626 if (*format_chars
== 'h' || *format_chars
== 'l')
1627 length_char
= *format_chars
++;
1628 else if (*format_chars
== 'q' || *format_chars
== 'L')
1630 length_char
= *format_chars
++;
1632 warning ("ANSI C does not support the `%c' length modifier",
1635 else if (*format_chars
== 'Z')
1637 length_char
= *format_chars
++;
1639 warning ("ANSI C does not support the `Z' length modifier");
1643 if (length_char
== 'l' && *format_chars
== 'l')
1645 length_char
= 'q', format_chars
++;
1646 /* FIXME: Is allowed in ISO C 9x. */
1648 warning ("ANSI C does not support the `ll' length modifier");
1650 else if (length_char
== 'h' && *format_chars
== 'h')
1652 length_char
= 'H', format_chars
++;
1653 /* FIXME: Is allowed in ISO C 9x. */
1655 warning ("ANSI C does not support the `hh' length modifier");
1657 if (*format_chars
== 'a' && info
->format_type
== scanf_format_type
)
1659 if (format_chars
[1] == 's' || format_chars
[1] == 'S'
1660 || format_chars
[1] == '[')
1662 /* `a' is used as a flag. */
1667 if (suppressed
&& length_char
!= 0)
1668 warning ("use of `*' and `%c' together in format", length_char
);
1670 format_char
= *format_chars
;
1671 if (format_char
== 0
1672 || (info
->format_type
!= strftime_format_type
&& format_char
== '%'))
1674 warning ("conversion lacks type at end of format");
1677 /* The m, C, and S formats are GNU extensions. */
1678 if (pedantic
&& info
->format_type
!= strftime_format_type
1679 && (format_char
== 'm' || format_char
== 'C' || format_char
== 'S'))
1680 warning ("ANSI C does not support the `%c' format", format_char
);
1681 /* ??? The a and A formats are C9X extensions, and should be allowed
1682 when a C9X option is added. */
1683 if (pedantic
&& info
->format_type
!= strftime_format_type
1684 && (format_char
== 'a' || format_char
== 'A'))
1685 warning ("ANSI C does not support the `%c' format", format_char
);
1687 switch (info
->format_type
)
1689 case printf_format_type
:
1690 fci
= print_char_table
;
1692 case scanf_format_type
:
1693 fci
= scan_char_table
;
1695 case strftime_format_type
:
1696 fci
= time_char_table
;
1701 while (fci
->format_chars
!= 0
1702 && index (fci
->format_chars
, format_char
) == 0)
1704 if (fci
->format_chars
== 0)
1706 if (format_char
>= 040 && format_char
< 0177)
1707 warning ("unknown conversion type character `%c' in format",
1710 warning ("unknown conversion type character 0x%x in format",
1716 if (index (fci
->flag_chars
, 'G') != 0)
1717 warning ("ANSI C does not support `%%%c'", format_char
);
1718 if (index (fci
->flag_chars
, 'o') != 0
1719 && index (flag_chars
, 'O') != 0)
1720 warning ("ANSI C does not support `%%O%c'", format_char
);
1722 if (wide
&& index (fci
->flag_chars
, 'w') == 0)
1723 warning ("width used with `%c' format", format_char
);
1724 if (index (fci
->flag_chars
, '2') != 0)
1725 warning ("`%%%c' yields only last 2 digits of year", format_char
);
1726 else if (index (fci
->flag_chars
, '3') != 0)
1727 warning ("`%%%c' yields only last 2 digits of year in some locales",
1729 if (precise
&& index (fci
->flag_chars
, 'p') == 0)
1730 warning ("precision used with `%c' format", format_char
);
1731 if (aflag
&& index (fci
->flag_chars
, 'a') == 0)
1733 warning ("`a' flag used with `%c' format", format_char
);
1734 /* To simplify the following code. */
1737 /* The a flag is a GNU extension. */
1738 else if (pedantic
&& aflag
)
1739 warning ("ANSI C does not support the `a' flag");
1740 if (info
->format_type
== scanf_format_type
&& format_char
== '[')
1742 /* Skip over scan set, in case it happens to have '%' in it. */
1743 if (*format_chars
== '^')
1745 /* Find closing bracket; if one is hit immediately, then
1746 it's part of the scan set rather than a terminator. */
1747 if (*format_chars
== ']')
1749 while (*format_chars
&& *format_chars
!= ']')
1751 if (*format_chars
!= ']')
1752 /* The end of the format string was reached. */
1753 warning ("no closing `]' for `%%[' format");
1757 if (index (fci
->flag_chars
, '*') == 0)
1758 warning ("suppression of `%c' conversion in format", format_char
);
1761 for (i
= 0; flag_chars
[i
] != 0; ++i
)
1763 if (index (fci
->flag_chars
, flag_chars
[i
]) == 0)
1764 warning ("flag `%c' used with type `%c'",
1765 flag_chars
[i
], format_char
);
1767 if (info
->format_type
== strftime_format_type
)
1769 if (precise
&& index (flag_chars
, '0') != 0
1770 && (format_char
== 'd' || format_char
== 'i'
1771 || format_char
== 'o' || format_char
== 'u'
1772 || format_char
== 'x' || format_char
== 'X'))
1773 warning ("`0' flag ignored with precision specifier and `%c' format",
1775 switch (length_char
)
1777 default: wanted_type
= fci
->nolen
? *(fci
->nolen
) : 0; break;
1778 case 'H': wanted_type
= fci
->hhlen
? *(fci
->hhlen
) : 0; break;
1779 case 'h': wanted_type
= fci
->hlen
? *(fci
->hlen
) : 0; break;
1780 case 'l': wanted_type
= fci
->llen
? *(fci
->llen
) : 0; break;
1781 case 'q': wanted_type
= fci
->qlen
? *(fci
->qlen
) : 0; break;
1782 case 'L': wanted_type
= fci
->bigllen
? *(fci
->bigllen
) : 0; break;
1783 case 'Z': wanted_type
= fci
->zlen
? *fci
->zlen
: 0; break;
1785 if (wanted_type
== 0)
1786 warning ("use of `%c' length character with `%c' type character",
1787 length_char
, format_char
);
1789 /* Finally. . .check type of argument against desired type! */
1790 if (info
->first_arg_num
== 0)
1792 if (fci
->pointer_count
== 0 && wanted_type
== void_type_node
)
1793 /* This specifier takes no argument. */
1800 cur_param
= TREE_VALUE (params
);
1801 params
= TREE_CHAIN (params
);
1803 cur_type
= TREE_TYPE (cur_param
);
1805 STRIP_NOPS (cur_param
);
1807 /* Check the types of any additional pointer arguments
1808 that precede the "real" argument. */
1809 for (i
= 0; i
< fci
->pointer_count
+ aflag
; ++i
)
1811 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
1813 cur_type
= TREE_TYPE (cur_type
);
1815 if (cur_param
!= 0 && TREE_CODE (cur_param
) == ADDR_EXPR
)
1816 cur_param
= TREE_OPERAND (cur_param
, 0);
1822 if (TREE_CODE (cur_type
) != ERROR_MARK
)
1823 warning ((fci
->pointer_count
+ aflag
== 1
1824 ? "format argument is not a pointer (arg %d)"
1825 : "format argument is not a pointer to a pointer (arg %d)"),
1830 /* See if this is an attempt to write into a const type with
1831 scanf or with printf "%n". */
1832 if ((info
->format_type
== scanf_format_type
1833 || (info
->format_type
== printf_format_type
1834 && format_char
== 'n'))
1835 && i
== fci
->pointer_count
+ aflag
1837 && TREE_CODE (cur_type
) != ERROR_MARK
1838 && (TYPE_READONLY (cur_type
)
1840 && (TREE_CODE_CLASS (TREE_CODE (cur_param
)) == 'c'
1841 || (TREE_CODE_CLASS (TREE_CODE (cur_param
)) == 'd'
1842 && TREE_READONLY (cur_param
))))))
1843 warning ("writing into constant object (arg %d)", arg_num
);
1845 /* Check the type of the "real" argument, if there's a type we want. */
1846 if (i
== fci
->pointer_count
+ aflag
&& wanted_type
!= 0
1847 && TREE_CODE (cur_type
) != ERROR_MARK
1848 && wanted_type
!= TYPE_MAIN_VARIANT (cur_type
)
1849 /* If we want `void *', allow any pointer type.
1850 (Anything else would already have got a warning.) */
1851 && ! (wanted_type
== void_type_node
1852 && fci
->pointer_count
> 0)
1853 /* Don't warn about differences merely in signedness. */
1854 && !(TREE_CODE (wanted_type
) == INTEGER_TYPE
1855 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type
)) == INTEGER_TYPE
1856 && (TREE_UNSIGNED (wanted_type
)
1857 ? wanted_type
== (cur_type
= unsigned_type (cur_type
))
1858 : wanted_type
== (cur_type
= signed_type (cur_type
))))
1859 /* Likewise, "signed char", "unsigned char" and "char" are
1860 equivalent but the above test won't consider them equivalent. */
1861 && ! (wanted_type
== char_type_node
1862 && (TYPE_MAIN_VARIANT (cur_type
) == signed_char_type_node
1863 || TYPE_MAIN_VARIANT (cur_type
) == unsigned_char_type_node
)))
1865 register const char *this;
1866 register const char *that
;
1868 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type
)));
1870 if (TREE_CODE (cur_type
) != ERROR_MARK
1871 && TYPE_NAME (cur_type
) != 0
1872 && TREE_CODE (cur_type
) != INTEGER_TYPE
1873 && !(TREE_CODE (cur_type
) == POINTER_TYPE
1874 && TREE_CODE (TREE_TYPE (cur_type
)) == INTEGER_TYPE
))
1876 if (TREE_CODE (TYPE_NAME (cur_type
)) == TYPE_DECL
1877 && DECL_NAME (TYPE_NAME (cur_type
)) != 0)
1878 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type
)));
1880 that
= IDENTIFIER_POINTER (TYPE_NAME (cur_type
));
1883 /* A nameless type can't possibly match what the format wants.
1884 So there will be a warning for it.
1885 Make up a string to describe vaguely what it is. */
1888 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
1891 that
= "different type";
1894 /* Make the warning better in case of mismatch of int vs long. */
1895 if (TREE_CODE (cur_type
) == INTEGER_TYPE
1896 && TREE_CODE (wanted_type
) == INTEGER_TYPE
1897 && TYPE_PRECISION (cur_type
) == TYPE_PRECISION (wanted_type
)
1898 && TYPE_NAME (cur_type
) != 0
1899 && TREE_CODE (TYPE_NAME (cur_type
)) == TYPE_DECL
)
1900 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type
)));
1902 if (strcmp (this, that
) != 0)
1903 warning ("%s format, %s arg (arg %d)", this, that
, arg_num
);
1908 /* Print a warning if a constant expression had overflow in folding.
1909 Invoke this function on every expression that the language
1910 requires to be a constant expression.
1911 Note the ANSI C standard says it is erroneous for a
1912 constant expression to overflow. */
1915 constant_expression_warning (value
)
1918 if ((TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
1919 || TREE_CODE (value
) == COMPLEX_CST
)
1920 && TREE_CONSTANT_OVERFLOW (value
) && pedantic
)
1921 pedwarn ("overflow in constant expression");
1924 /* Print a warning if an expression had overflow in folding.
1925 Invoke this function on every expression that
1926 (1) appears in the source code, and
1927 (2) might be a constant expression that overflowed, and
1928 (3) is not already checked by convert_and_check;
1929 however, do not invoke this function on operands of explicit casts. */
1932 overflow_warning (value
)
1935 if ((TREE_CODE (value
) == INTEGER_CST
1936 || (TREE_CODE (value
) == COMPLEX_CST
1937 && TREE_CODE (TREE_REALPART (value
)) == INTEGER_CST
))
1938 && TREE_OVERFLOW (value
))
1940 TREE_OVERFLOW (value
) = 0;
1941 if (skip_evaluation
== 0)
1942 warning ("integer overflow in expression");
1944 else if ((TREE_CODE (value
) == REAL_CST
1945 || (TREE_CODE (value
) == COMPLEX_CST
1946 && TREE_CODE (TREE_REALPART (value
)) == REAL_CST
))
1947 && TREE_OVERFLOW (value
))
1949 TREE_OVERFLOW (value
) = 0;
1950 if (skip_evaluation
== 0)
1951 warning ("floating point overflow in expression");
1955 /* Print a warning if a large constant is truncated to unsigned,
1956 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1957 Invoke this function on every expression that might be implicitly
1958 converted to an unsigned type. */
1961 unsigned_conversion_warning (result
, operand
)
1962 tree result
, operand
;
1964 if (TREE_CODE (operand
) == INTEGER_CST
1965 && TREE_CODE (TREE_TYPE (result
)) == INTEGER_TYPE
1966 && TREE_UNSIGNED (TREE_TYPE (result
))
1967 && skip_evaluation
== 0
1968 && !int_fits_type_p (operand
, TREE_TYPE (result
)))
1970 if (!int_fits_type_p (operand
, signed_type (TREE_TYPE (result
))))
1971 /* This detects cases like converting -129 or 256 to unsigned char. */
1972 warning ("large integer implicitly truncated to unsigned type");
1973 else if (warn_conversion
)
1974 warning ("negative integer implicitly converted to unsigned type");
1978 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1979 Invoke this function on every expression that is converted implicitly,
1980 i.e. because of language rules and not because of an explicit cast. */
1983 convert_and_check (type
, expr
)
1986 tree t
= convert (type
, expr
);
1987 if (TREE_CODE (t
) == INTEGER_CST
)
1989 if (TREE_OVERFLOW (t
))
1991 TREE_OVERFLOW (t
) = 0;
1993 /* Do not diagnose overflow in a constant expression merely
1994 because a conversion overflowed. */
1995 TREE_CONSTANT_OVERFLOW (t
) = TREE_CONSTANT_OVERFLOW (expr
);
1997 /* No warning for converting 0x80000000 to int. */
1998 if (!(TREE_UNSIGNED (type
) < TREE_UNSIGNED (TREE_TYPE (expr
))
1999 && TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
2000 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (expr
))))
2001 /* If EXPR fits in the unsigned version of TYPE,
2002 don't warn unless pedantic. */
2004 || TREE_UNSIGNED (type
)
2005 || ! int_fits_type_p (expr
, unsigned_type (type
)))
2006 && skip_evaluation
== 0)
2007 warning ("overflow in implicit constant conversion");
2010 unsigned_conversion_warning (t
, expr
);
2016 c_expand_expr_stmt (expr
)
2019 /* Do default conversion if safe and possibly important,
2020 in case within ({...}). */
2021 if ((TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
&& lvalue_p (expr
))
2022 || TREE_CODE (TREE_TYPE (expr
)) == FUNCTION_TYPE
)
2023 expr
= default_conversion (expr
);
2025 if (TREE_TYPE (expr
) != error_mark_node
2026 && TYPE_SIZE (TREE_TYPE (expr
)) == 0
2027 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
2028 error ("expression statement has incomplete type");
2030 expand_expr_stmt (expr
);
2033 /* Validate the expression after `case' and apply default promotions. */
2036 check_case_value (value
)
2039 if (value
== NULL_TREE
)
2042 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2043 STRIP_TYPE_NOPS (value
);
2045 if (TREE_CODE (value
) != INTEGER_CST
2046 && value
!= error_mark_node
)
2048 error ("case label does not reduce to an integer constant");
2049 value
= error_mark_node
;
2052 /* Promote char or short to int. */
2053 value
= default_conversion (value
);
2055 constant_expression_warning (value
);
2060 /* Return an integer type with BITS bits of precision,
2061 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2064 type_for_size (bits
, unsignedp
)
2068 if (bits
== TYPE_PRECISION (integer_type_node
))
2069 return unsignedp
? unsigned_type_node
: integer_type_node
;
2071 if (bits
== TYPE_PRECISION (signed_char_type_node
))
2072 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
2074 if (bits
== TYPE_PRECISION (short_integer_type_node
))
2075 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
2077 if (bits
== TYPE_PRECISION (long_integer_type_node
))
2078 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
2080 if (bits
== TYPE_PRECISION (long_long_integer_type_node
))
2081 return (unsignedp
? long_long_unsigned_type_node
2082 : long_long_integer_type_node
);
2084 if (bits
<= TYPE_PRECISION (intQI_type_node
))
2085 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
2087 if (bits
<= TYPE_PRECISION (intHI_type_node
))
2088 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
2090 if (bits
<= TYPE_PRECISION (intSI_type_node
))
2091 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
2093 if (bits
<= TYPE_PRECISION (intDI_type_node
))
2094 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
2099 /* Return a data type that has machine mode MODE.
2100 If the mode is an integer,
2101 then UNSIGNEDP selects between signed and unsigned types. */
2104 type_for_mode (mode
, unsignedp
)
2105 enum machine_mode mode
;
2108 if (mode
== TYPE_MODE (integer_type_node
))
2109 return unsignedp
? unsigned_type_node
: integer_type_node
;
2111 if (mode
== TYPE_MODE (signed_char_type_node
))
2112 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
2114 if (mode
== TYPE_MODE (short_integer_type_node
))
2115 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
2117 if (mode
== TYPE_MODE (long_integer_type_node
))
2118 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
2120 if (mode
== TYPE_MODE (long_long_integer_type_node
))
2121 return unsignedp
? long_long_unsigned_type_node
: long_long_integer_type_node
;
2123 if (mode
== TYPE_MODE (intQI_type_node
))
2124 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
2126 if (mode
== TYPE_MODE (intHI_type_node
))
2127 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
2129 if (mode
== TYPE_MODE (intSI_type_node
))
2130 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
2132 if (mode
== TYPE_MODE (intDI_type_node
))
2133 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
2135 #if HOST_BITS_PER_WIDE_INT >= 64
2136 if (mode
== TYPE_MODE (intTI_type_node
))
2137 return unsignedp
? unsigned_intTI_type_node
: intTI_type_node
;
2140 if (mode
== TYPE_MODE (float_type_node
))
2141 return float_type_node
;
2143 if (mode
== TYPE_MODE (double_type_node
))
2144 return double_type_node
;
2146 if (mode
== TYPE_MODE (long_double_type_node
))
2147 return long_double_type_node
;
2149 if (mode
== TYPE_MODE (build_pointer_type (char_type_node
)))
2150 return build_pointer_type (char_type_node
);
2152 if (mode
== TYPE_MODE (build_pointer_type (integer_type_node
)))
2153 return build_pointer_type (integer_type_node
);
2158 /* Return the minimum number of bits needed to represent VALUE in a
2159 signed or unsigned type, UNSIGNEDP says which. */
2162 min_precision (value
, unsignedp
)
2168 /* If the value is negative, compute its negative minus 1. The latter
2169 adjustment is because the absolute value of the largest negative value
2170 is one larger than the largest positive value. This is equivalent to
2171 a bit-wise negation, so use that operation instead. */
2173 if (tree_int_cst_sgn (value
) < 0)
2174 value
= fold (build1 (BIT_NOT_EXPR
, TREE_TYPE (value
), value
));
2176 /* Return the number of bits needed, taking into account the fact
2177 that we need one more bit for a signed than unsigned type. */
2179 if (integer_zerop (value
))
2181 else if (TREE_INT_CST_HIGH (value
) != 0)
2182 log
= HOST_BITS_PER_WIDE_INT
+ floor_log2 (TREE_INT_CST_HIGH (value
));
2184 log
= floor_log2 (TREE_INT_CST_LOW (value
));
2186 return log
+ 1 + ! unsignedp
;
2189 /* Print an error message for invalid operands to arith operation CODE.
2190 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2193 binary_op_error (code
)
2194 enum tree_code code
;
2196 register const char *opname
;
2201 error ("invalid truth-value expression");
2205 opname
= "+"; break;
2207 opname
= "-"; break;
2209 opname
= "*"; break;
2211 opname
= "max"; break;
2213 opname
= "min"; break;
2215 opname
= "=="; break;
2217 opname
= "!="; break;
2219 opname
= "<="; break;
2221 opname
= ">="; break;
2223 opname
= "<"; break;
2225 opname
= ">"; break;
2227 opname
= "<<"; break;
2229 opname
= ">>"; break;
2230 case TRUNC_MOD_EXPR
:
2231 case FLOOR_MOD_EXPR
:
2232 opname
= "%"; break;
2233 case TRUNC_DIV_EXPR
:
2234 case FLOOR_DIV_EXPR
:
2235 opname
= "/"; break;
2237 opname
= "&"; break;
2239 opname
= "|"; break;
2240 case TRUTH_ANDIF_EXPR
:
2241 opname
= "&&"; break;
2242 case TRUTH_ORIF_EXPR
:
2243 opname
= "||"; break;
2245 opname
= "^"; break;
2248 opname
= "rotate"; break;
2250 opname
= "unknown"; break;
2252 error ("invalid operands to binary %s", opname
);
2255 /* Subroutine of build_binary_op, used for comparison operations.
2256 See if the operands have both been converted from subword integer types
2257 and, if so, perhaps change them both back to their original type.
2258 This function is also responsible for converting the two operands
2259 to the proper common type for comparison.
2261 The arguments of this function are all pointers to local variables
2262 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2263 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2265 If this function returns nonzero, it means that the comparison has
2266 a constant value. What this function returns is an expression for
2270 shorten_compare (op0_ptr
, op1_ptr
, restype_ptr
, rescode_ptr
)
2271 tree
*op0_ptr
, *op1_ptr
;
2273 enum tree_code
*rescode_ptr
;
2276 tree op0
= *op0_ptr
;
2277 tree op1
= *op1_ptr
;
2278 int unsignedp0
, unsignedp1
;
2280 tree primop0
, primop1
;
2281 enum tree_code code
= *rescode_ptr
;
2283 /* Throw away any conversions to wider types
2284 already present in the operands. */
2286 primop0
= get_narrower (op0
, &unsignedp0
);
2287 primop1
= get_narrower (op1
, &unsignedp1
);
2289 /* Handle the case that OP0 does not *contain* a conversion
2290 but it *requires* conversion to FINAL_TYPE. */
2292 if (op0
== primop0
&& TREE_TYPE (op0
) != *restype_ptr
)
2293 unsignedp0
= TREE_UNSIGNED (TREE_TYPE (op0
));
2294 if (op1
== primop1
&& TREE_TYPE (op1
) != *restype_ptr
)
2295 unsignedp1
= TREE_UNSIGNED (TREE_TYPE (op1
));
2297 /* If one of the operands must be floated, we cannot optimize. */
2298 real1
= TREE_CODE (TREE_TYPE (primop0
)) == REAL_TYPE
;
2299 real2
= TREE_CODE (TREE_TYPE (primop1
)) == REAL_TYPE
;
2301 /* If first arg is constant, swap the args (changing operation
2302 so value is preserved), for canonicalization. Don't do this if
2303 the second arg is 0. */
2305 if (TREE_CONSTANT (primop0
)
2306 && ! integer_zerop (primop1
) && ! real_zerop (primop1
))
2308 register tree tem
= primop0
;
2309 register int temi
= unsignedp0
;
2317 unsignedp0
= unsignedp1
;
2340 *rescode_ptr
= code
;
2343 /* If comparing an integer against a constant more bits wide,
2344 maybe we can deduce a value of 1 or 0 independent of the data.
2345 Or else truncate the constant now
2346 rather than extend the variable at run time.
2348 This is only interesting if the constant is the wider arg.
2349 Also, it is not safe if the constant is unsigned and the
2350 variable arg is signed, since in this case the variable
2351 would be sign-extended and then regarded as unsigned.
2352 Our technique fails in this case because the lowest/highest
2353 possible unsigned results don't follow naturally from the
2354 lowest/highest possible values of the variable operand.
2355 For just EQ_EXPR and NE_EXPR there is another technique that
2356 could be used: see if the constant can be faithfully represented
2357 in the other operand's type, by truncating it and reextending it
2358 and see if that preserves the constant's value. */
2360 if (!real1
&& !real2
2361 && TREE_CODE (primop1
) == INTEGER_CST
2362 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
))
2364 int min_gt
, max_gt
, min_lt
, max_lt
;
2365 tree maxval
, minval
;
2366 /* 1 if comparison is nominally unsigned. */
2367 int unsignedp
= TREE_UNSIGNED (*restype_ptr
);
2370 type
= signed_or_unsigned_type (unsignedp0
, TREE_TYPE (primop0
));
2372 /* If TYPE is an enumeration, then we need to get its min/max
2373 values from it's underlying integral type, not the enumerated
2375 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
2376 type
= type_for_size (TYPE_PRECISION (type
), unsignedp0
);
2378 maxval
= TYPE_MAX_VALUE (type
);
2379 minval
= TYPE_MIN_VALUE (type
);
2381 if (unsignedp
&& !unsignedp0
)
2382 *restype_ptr
= signed_type (*restype_ptr
);
2384 if (TREE_TYPE (primop1
) != *restype_ptr
)
2385 primop1
= convert (*restype_ptr
, primop1
);
2386 if (type
!= *restype_ptr
)
2388 minval
= convert (*restype_ptr
, minval
);
2389 maxval
= convert (*restype_ptr
, maxval
);
2392 if (unsignedp
&& unsignedp0
)
2394 min_gt
= INT_CST_LT_UNSIGNED (primop1
, minval
);
2395 max_gt
= INT_CST_LT_UNSIGNED (primop1
, maxval
);
2396 min_lt
= INT_CST_LT_UNSIGNED (minval
, primop1
);
2397 max_lt
= INT_CST_LT_UNSIGNED (maxval
, primop1
);
2401 min_gt
= INT_CST_LT (primop1
, minval
);
2402 max_gt
= INT_CST_LT (primop1
, maxval
);
2403 min_lt
= INT_CST_LT (minval
, primop1
);
2404 max_lt
= INT_CST_LT (maxval
, primop1
);
2408 /* This used to be a switch, but Genix compiler can't handle that. */
2409 if (code
== NE_EXPR
)
2411 if (max_lt
|| min_gt
)
2412 val
= boolean_true_node
;
2414 else if (code
== EQ_EXPR
)
2416 if (max_lt
|| min_gt
)
2417 val
= boolean_false_node
;
2419 else if (code
== LT_EXPR
)
2422 val
= boolean_true_node
;
2424 val
= boolean_false_node
;
2426 else if (code
== GT_EXPR
)
2429 val
= boolean_true_node
;
2431 val
= boolean_false_node
;
2433 else if (code
== LE_EXPR
)
2436 val
= boolean_true_node
;
2438 val
= boolean_false_node
;
2440 else if (code
== GE_EXPR
)
2443 val
= boolean_true_node
;
2445 val
= boolean_false_node
;
2448 /* If primop0 was sign-extended and unsigned comparison specd,
2449 we did a signed comparison above using the signed type bounds.
2450 But the comparison we output must be unsigned.
2452 Also, for inequalities, VAL is no good; but if the signed
2453 comparison had *any* fixed result, it follows that the
2454 unsigned comparison just tests the sign in reverse
2455 (positive values are LE, negative ones GE).
2456 So we can generate an unsigned comparison
2457 against an extreme value of the signed type. */
2459 if (unsignedp
&& !unsignedp0
)
2466 primop1
= TYPE_MIN_VALUE (type
);
2472 primop1
= TYPE_MAX_VALUE (type
);
2479 type
= unsigned_type (type
);
2482 if (!max_gt
&& !unsignedp0
&& TREE_CODE (primop0
) != INTEGER_CST
)
2484 /* This is the case of (char)x >?< 0x80, which people used to use
2485 expecting old C compilers to change the 0x80 into -0x80. */
2486 if (val
== boolean_false_node
)
2487 warning ("comparison is always false due to limited range of data type");
2488 if (val
== boolean_true_node
)
2489 warning ("comparison is always true due to limited range of data type");
2492 if (!min_lt
&& unsignedp0
&& TREE_CODE (primop0
) != INTEGER_CST
)
2494 /* This is the case of (unsigned char)x >?< -1 or < 0. */
2495 if (val
== boolean_false_node
)
2496 warning ("comparison is always false due to limited range of data type");
2497 if (val
== boolean_true_node
)
2498 warning ("comparison is always true due to limited range of data type");
2503 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2504 if (TREE_SIDE_EFFECTS (primop0
))
2505 return build (COMPOUND_EXPR
, TREE_TYPE (val
), primop0
, val
);
2509 /* Value is not predetermined, but do the comparison
2510 in the type of the operand that is not constant.
2511 TYPE is already properly set. */
2513 else if (real1
&& real2
2514 && (TYPE_PRECISION (TREE_TYPE (primop0
))
2515 == TYPE_PRECISION (TREE_TYPE (primop1
))))
2516 type
= TREE_TYPE (primop0
);
2518 /* If args' natural types are both narrower than nominal type
2519 and both extend in the same manner, compare them
2520 in the type of the wider arg.
2521 Otherwise must actually extend both to the nominal
2522 common type lest different ways of extending
2524 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2526 else if (unsignedp0
== unsignedp1
&& real1
== real2
2527 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
)
2528 && TYPE_PRECISION (TREE_TYPE (primop1
)) < TYPE_PRECISION (*restype_ptr
))
2530 type
= common_type (TREE_TYPE (primop0
), TREE_TYPE (primop1
));
2531 type
= signed_or_unsigned_type (unsignedp0
2532 || TREE_UNSIGNED (*restype_ptr
),
2534 /* Make sure shorter operand is extended the right way
2535 to match the longer operand. */
2536 primop0
= convert (signed_or_unsigned_type (unsignedp0
, TREE_TYPE (primop0
)),
2538 primop1
= convert (signed_or_unsigned_type (unsignedp1
, TREE_TYPE (primop1
)),
2543 /* Here we must do the comparison on the nominal type
2544 using the args exactly as we received them. */
2545 type
= *restype_ptr
;
2549 if (!real1
&& !real2
&& integer_zerop (primop1
)
2550 && TREE_UNSIGNED (*restype_ptr
))
2556 /* All unsigned values are >= 0, so we warn if extra warnings
2557 are requested. However, if OP0 is a constant that is
2558 >= 0, the signedness of the comparison isn't an issue,
2559 so suppress the warning. */
2561 && ! (TREE_CODE (primop0
) == INTEGER_CST
2562 && ! TREE_OVERFLOW (convert (signed_type (type
),
2564 warning ("comparison of unsigned expression >= 0 is always true");
2565 value
= boolean_true_node
;
2570 && ! (TREE_CODE (primop0
) == INTEGER_CST
2571 && ! TREE_OVERFLOW (convert (signed_type (type
),
2573 warning ("comparison of unsigned expression < 0 is always false");
2574 value
= boolean_false_node
;
2583 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2584 if (TREE_SIDE_EFFECTS (primop0
))
2585 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
2592 *op0_ptr
= convert (type
, primop0
);
2593 *op1_ptr
= convert (type
, primop1
);
2595 *restype_ptr
= boolean_type_node
;
2600 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2601 or validate its data type for an `if' or `while' statement or ?..: exp.
2603 This preparation consists of taking the ordinary
2604 representation of an expression expr and producing a valid tree
2605 boolean expression describing whether expr is nonzero. We could
2606 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2607 but we optimize comparisons, &&, ||, and !.
2609 The resulting type should always be `boolean_type_node'. */
2612 truthvalue_conversion (expr
)
2615 if (TREE_CODE (expr
) == ERROR_MARK
)
2618 #if 0 /* This appears to be wrong for C++. */
2619 /* These really should return error_mark_node after 2.4 is stable.
2620 But not all callers handle ERROR_MARK properly. */
2621 switch (TREE_CODE (TREE_TYPE (expr
)))
2624 error ("struct type value used where scalar is required");
2625 return boolean_false_node
;
2628 error ("union type value used where scalar is required");
2629 return boolean_false_node
;
2632 error ("array type value used where scalar is required");
2633 return boolean_false_node
;
2640 switch (TREE_CODE (expr
))
2642 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2643 or comparison expressions as truth values at this level. */
2646 /* A one-bit unsigned bit-field is already acceptable. */
2647 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr
, 1)))
2648 && TREE_UNSIGNED (TREE_OPERAND (expr
, 1)))
2654 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2655 or comparison expressions as truth values at this level. */
2657 if (integer_zerop (TREE_OPERAND (expr
, 1)))
2658 return build_unary_op (TRUTH_NOT_EXPR
, TREE_OPERAND (expr
, 0), 0);
2660 case NE_EXPR
: case LE_EXPR
: case GE_EXPR
: case LT_EXPR
: case GT_EXPR
:
2661 case TRUTH_ANDIF_EXPR
:
2662 case TRUTH_ORIF_EXPR
:
2663 case TRUTH_AND_EXPR
:
2665 case TRUTH_XOR_EXPR
:
2666 case TRUTH_NOT_EXPR
:
2667 TREE_TYPE (expr
) = boolean_type_node
;
2674 return integer_zerop (expr
) ? boolean_false_node
: boolean_true_node
;
2677 return real_zerop (expr
) ? boolean_false_node
: boolean_true_node
;
2680 /* If we are taking the address of a external decl, it might be zero
2681 if it is weak, so we cannot optimize. */
2682 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr
, 0))) == 'd'
2683 && DECL_EXTERNAL (TREE_OPERAND (expr
, 0)))
2686 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 0)))
2687 return build (COMPOUND_EXPR
, boolean_type_node
,
2688 TREE_OPERAND (expr
, 0), boolean_true_node
);
2690 return boolean_true_node
;
2693 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1))
2694 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
2695 truthvalue_conversion (TREE_OPERAND (expr
, 0)),
2696 truthvalue_conversion (TREE_OPERAND (expr
, 1)),
2703 /* These don't change whether an object is non-zero or zero. */
2704 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
2708 /* These don't change whether an object is zero or non-zero, but
2709 we can't ignore them if their second arg has side-effects. */
2710 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1)))
2711 return build (COMPOUND_EXPR
, boolean_type_node
, TREE_OPERAND (expr
, 1),
2712 truthvalue_conversion (TREE_OPERAND (expr
, 0)));
2714 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
2717 /* Distribute the conversion into the arms of a COND_EXPR. */
2718 return fold (build (COND_EXPR
, boolean_type_node
, TREE_OPERAND (expr
, 0),
2719 truthvalue_conversion (TREE_OPERAND (expr
, 1)),
2720 truthvalue_conversion (TREE_OPERAND (expr
, 2))));
2723 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2724 since that affects how `default_conversion' will behave. */
2725 if (TREE_CODE (TREE_TYPE (expr
)) == REFERENCE_TYPE
2726 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == REFERENCE_TYPE
)
2728 /* fall through... */
2730 /* If this is widening the argument, we can ignore it. */
2731 if (TYPE_PRECISION (TREE_TYPE (expr
))
2732 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr
, 0))))
2733 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
2737 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2739 if (TARGET_FLOAT_FORMAT
== IEEE_FLOAT_FORMAT
2740 && TREE_CODE (TREE_TYPE (expr
)) == REAL_TYPE
)
2742 /* fall through... */
2744 /* This and MINUS_EXPR can be changed into a comparison of the
2746 if (TREE_TYPE (TREE_OPERAND (expr
, 0))
2747 == TREE_TYPE (TREE_OPERAND (expr
, 1)))
2748 return build_binary_op (NE_EXPR
, TREE_OPERAND (expr
, 0),
2749 TREE_OPERAND (expr
, 1), 1);
2750 return build_binary_op (NE_EXPR
, TREE_OPERAND (expr
, 0),
2751 fold (build1 (NOP_EXPR
,
2752 TREE_TYPE (TREE_OPERAND (expr
, 0)),
2753 TREE_OPERAND (expr
, 1))), 1);
2756 if (integer_onep (TREE_OPERAND (expr
, 1))
2757 && TREE_TYPE (expr
) != boolean_type_node
)
2758 /* Using convert here would cause infinite recursion. */
2759 return build1 (NOP_EXPR
, boolean_type_node
, expr
);
2763 if (warn_parentheses
&& C_EXP_ORIGINAL_CODE (expr
) == MODIFY_EXPR
)
2764 warning ("suggest parentheses around assignment used as truth value");
2771 if (TREE_CODE (TREE_TYPE (expr
)) == COMPLEX_TYPE
)
2773 tree tem
= save_expr (expr
);
2774 return (build_binary_op
2775 ((TREE_SIDE_EFFECTS (expr
)
2776 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
2777 truthvalue_conversion (build_unary_op (REALPART_EXPR
, tem
, 0)),
2778 truthvalue_conversion (build_unary_op (IMAGPART_EXPR
, tem
, 0)),
2782 return build_binary_op (NE_EXPR
, expr
, integer_zero_node
, 1);
2786 /* Read the rest of a #-directive from input stream FINPUT.
2787 In normal use, the directive name and the white space after it
2788 have already been read, so they won't be included in the result.
2789 We allow for the fact that the directive line may contain
2790 a newline embedded within a character or string literal which forms
2791 a part of the directive.
2793 The value is a string in a reusable buffer. It remains valid
2794 only until the next time this function is called. */
2795 unsigned char *yy_cur
, *yy_lim
;
2797 #define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
2798 #define UNGETC(c) ((c) == EOF ? 0 : yy_cur--)
2805 parse_in
.limit
= parse_in
.token_buffer
;
2806 cpp_token
= cpp_get_token (&parse_in
);
2807 if (cpp_token
== CPP_EOF
)
2809 yy_lim
= CPP_PWRITTEN (&parse_in
);
2810 yy_cur
= parse_in
.token_buffer
;
2811 if (yy_cur
< yy_lim
)
2817 get_directive_line ()
2819 static char *directive_buffer
= NULL
;
2820 static unsigned buffer_length
= 0;
2822 register char *buffer_limit
;
2823 register int looking_for
= 0;
2824 register int char_escaped
= 0;
2826 if (buffer_length
== 0)
2828 directive_buffer
= (char *)xmalloc (128);
2829 buffer_length
= 128;
2832 buffer_limit
= &directive_buffer
[buffer_length
];
2834 for (p
= directive_buffer
; ; )
2838 /* Make buffer bigger if it is full. */
2839 if (p
>= buffer_limit
)
2841 register unsigned bytes_used
= (p
- directive_buffer
);
2845 = (char *)xrealloc (directive_buffer
, buffer_length
);
2846 p
= &directive_buffer
[bytes_used
];
2847 buffer_limit
= &directive_buffer
[buffer_length
];
2852 /* Discard initial whitespace. */
2853 if ((c
== ' ' || c
== '\t') && p
== directive_buffer
)
2856 /* Detect the end of the directive. */
2857 if (c
== '\n' && looking_for
== 0)
2866 return directive_buffer
;
2868 /* Handle string and character constant syntax. */
2871 if (looking_for
== c
&& !char_escaped
)
2872 looking_for
= 0; /* Found terminator... stop looking. */
2875 if (c
== '\'' || c
== '"')
2876 looking_for
= c
; /* Don't stop buffering until we see another
2877 another one of these (or an EOF). */
2879 /* Handle backslash. */
2880 char_escaped
= (c
== '\\' && ! char_escaped
);
2884 /* Read the rest of a #-directive from input stream FINPUT.
2885 In normal use, the directive name and the white space after it
2886 have already been read, so they won't be included in the result.
2887 We allow for the fact that the directive line may contain
2888 a newline embedded within a character or string literal which forms
2889 a part of the directive.
2891 The value is a string in a reusable buffer. It remains valid
2892 only until the next time this function is called.
2894 The terminating character ('\n' or EOF) is left in FINPUT for the
2895 caller to re-read. */
2898 get_directive_line (finput
)
2899 register FILE *finput
;
2901 static char *directive_buffer
= NULL
;
2902 static unsigned buffer_length
= 0;
2904 register char *buffer_limit
;
2905 register int looking_for
= 0;
2906 register int char_escaped
= 0;
2908 if (buffer_length
== 0)
2910 directive_buffer
= (char *)xmalloc (128);
2911 buffer_length
= 128;
2914 buffer_limit
= &directive_buffer
[buffer_length
];
2916 for (p
= directive_buffer
; ; )
2920 /* Make buffer bigger if it is full. */
2921 if (p
>= buffer_limit
)
2923 register unsigned bytes_used
= (p
- directive_buffer
);
2927 = (char *)xrealloc (directive_buffer
, buffer_length
);
2928 p
= &directive_buffer
[bytes_used
];
2929 buffer_limit
= &directive_buffer
[buffer_length
];
2934 /* Discard initial whitespace. */
2935 if ((c
== ' ' || c
== '\t') && p
== directive_buffer
)
2938 /* Detect the end of the directive. */
2939 if (looking_for
== 0
2940 && (c
== '\n' || c
== EOF
))
2949 return directive_buffer
;
2951 /* Handle string and character constant syntax. */
2954 if (looking_for
== c
&& !char_escaped
)
2955 looking_for
= 0; /* Found terminator... stop looking. */
2958 if (c
== '\'' || c
== '"')
2959 looking_for
= c
; /* Don't stop buffering until we see another
2960 one of these (or an EOF). */
2962 /* Handle backslash. */
2963 char_escaped
= (c
== '\\' && ! char_escaped
);
2966 #endif /* !USE_CPPLIB */
2968 /* Make a variant type in the proper way for C/C++, propagating qualifiers
2969 down to the element type of an array. */
2972 c_build_qualified_type (type
, type_quals
)
2976 /* A restrict-qualified pointer type must be a pointer to object or
2977 incomplete type. Note that the use of POINTER_TYPE_P also allows
2978 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
2979 the C++ front-end also use POINTER_TYPE for pointer-to-member
2980 values, so even though it should be illegal to use `restrict'
2981 with such an entity we don't flag that here. Thus, special case
2982 code for that case is required in the C++ front-end. */
2983 if ((type_quals
& TYPE_QUAL_RESTRICT
)
2984 && (!POINTER_TYPE_P (type
)
2985 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
2987 error ("invalid use of `restrict'");
2988 type_quals
&= ~TYPE_QUAL_RESTRICT
;
2991 if (TREE_CODE (type
) == ARRAY_TYPE
)
2992 return build_array_type (c_build_qualified_type (TREE_TYPE (type
),
2994 TYPE_DOMAIN (type
));
2995 return build_qualified_type (type
, type_quals
);
2998 /* Apply the TYPE_QUALS to the new DECL. */
3001 c_apply_type_quals_to_decl (type_quals
, decl
)
3005 if (type_quals
& TYPE_QUAL_CONST
)
3006 TREE_READONLY (decl
) = 1;
3007 if (type_quals
& TYPE_QUAL_VOLATILE
)
3009 TREE_SIDE_EFFECTS (decl
) = 1;
3010 TREE_THIS_VOLATILE (decl
) = 1;
3012 if (type_quals
& TYPE_QUAL_RESTRICT
)
3014 if (!TREE_TYPE (decl
)
3015 || !POINTER_TYPE_P (TREE_TYPE (decl
))
3016 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl
))))
3017 error ("invalid use of `restrict'");
3018 else if (flag_strict_aliasing
)
3020 /* No two restricted pointers can point at the same thing.
3021 However, a restricted pointer can point at the same thing
3022 as an unrestricted pointer, if that unrestricted pointer
3023 is based on the restricted pointer. So, we make the
3024 alias set for the restricted pointer a subset of the
3025 alias set for the type pointed to by the type of the
3028 int pointed_to_alias_set
3029 = get_alias_set (TREE_TYPE (TREE_TYPE (decl
)));
3031 if (!pointed_to_alias_set
)
3032 /* It's not legal to make a subset of alias set zero. */
3036 DECL_POINTER_ALIAS_SET (decl
) = new_alias_set ();
3037 record_alias_subset (pointed_to_alias_set
,
3038 DECL_POINTER_ALIAS_SET (decl
));
3044 /* T is an expression with pointer type. Find the DECL on which this
3045 expression is based. (For example, in `a[i]' this would be `a'.)
3046 If there is no such DECL, or a unique decl cannot be determined,
3047 NULL_TREE is retured. */
3050 c_find_base_decl (t
)
3056 if (t
== NULL_TREE
|| t
== error_mark_node
)
3059 if (!POINTER_TYPE_P (TREE_TYPE (t
)))
3064 if (TREE_CODE (t
) == FIELD_DECL
3065 || TREE_CODE (t
) == PARM_DECL
3066 || TREE_CODE (t
) == VAR_DECL
)
3067 /* Aha, we found a pointer-typed declaration. */
3070 /* It would be nice to deal with COMPONENT_REFs here. If we could
3071 tell that `a' and `b' were the same, then `a->f' and `b->f' are
3074 /* Handle general expressions. */
3075 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
3080 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
3082 tree d
= c_find_base_decl (TREE_OPERAND (t
, i
));
3087 else if (d
&& d
!= decl
)
3088 /* Two different declarations. That's confusing; let's
3089 just assume we don't know what's going on. */
3102 /* Return the typed-based alias set for T, which may be an expression
3112 if (t
== error_mark_node
)
3115 type
= (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
3116 ? t
: TREE_TYPE (t
);
3118 if (type
== error_mark_node
)
3121 /* Deal with special cases first; for certain kinds of references
3122 we're interested in more than just the type. */
3124 if (TREE_CODE (t
) == BIT_FIELD_REF
)
3125 /* Perhaps reads and writes to this piece of data alias fields
3126 neighboring the bitfield. Perhaps that's impossible. For now,
3127 let's just assume that bitfields can alias everything, which is
3128 the conservative assumption. */
3131 /* Permit type-punning when accessing a union, provided the access
3132 is directly through the union. For example, this code does not
3133 permit taking the address of a union member and then storing
3134 through it. Even the type-punning allowed here is a GCC
3135 extension, albeit a common and useful one; the C standard says
3136 that such accesses have implementation-defined behavior. */
3138 TREE_CODE (u
) == COMPONENT_REF
|| TREE_CODE (u
) == ARRAY_REF
;
3139 u
= TREE_OPERAND (u
, 0))
3140 if (TREE_CODE (u
) == COMPONENT_REF
3141 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u
, 0))) == UNION_TYPE
)
3144 if (TREE_CODE (t
) == INDIRECT_REF
)
3146 /* Check for accesses through restrict-qualified pointers. */
3147 tree decl
= c_find_base_decl (TREE_OPERAND (t
, 0));
3149 if (decl
&& DECL_POINTER_ALIAS_SET_KNOWN_P (decl
))
3150 /* We use the alias set indicated in the declaration. */
3151 return DECL_POINTER_ALIAS_SET (decl
);
3154 /* From here on, only the type matters. */
3156 if (TREE_CODE (t
) == COMPONENT_REF
3157 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (t
, 1)))
3158 /* Since build_modify_expr calls get_unwidened for stores to
3159 component references, the type of a bit field can be changed
3160 from (say) `unsigned int : 16' to `unsigned short' or from
3161 `enum E : 16' to `short'. We want the real type of the
3162 bit-field in this case, not some the integral equivalent. */
3163 type
= DECL_BIT_FIELD_TYPE (TREE_OPERAND (t
, 1));
3165 if (TYPE_ALIAS_SET_KNOWN_P (type
))
3166 /* If we've already calculated the value, just return it. */
3167 return TYPE_ALIAS_SET (type
);
3168 else if (TYPE_MAIN_VARIANT (type
) != type
)
3169 /* The C standard specifically allows aliasing between
3170 cv-qualified variants of types. */
3171 TYPE_ALIAS_SET (type
) = c_get_alias_set (TYPE_MAIN_VARIANT (type
));
3172 else if (TREE_CODE (type
) == INTEGER_TYPE
)
3174 tree signed_variant
;
3176 /* The C standard specifically allows aliasing between signed and
3177 unsigned variants of the same type. We treat the signed
3178 variant as canonical. */
3179 signed_variant
= signed_type (type
);
3181 if (signed_variant
!= type
)
3182 TYPE_ALIAS_SET (type
) = c_get_alias_set (signed_variant
);
3183 else if (signed_variant
== signed_char_type_node
)
3184 /* The C standard guarantess that any object may be accessed
3185 via an lvalue that has character type. We don't have to
3186 check for unsigned_char_type_node or char_type_node because
3187 we are specifically looking at the signed variant. */
3188 TYPE_ALIAS_SET (type
) = 0;
3190 else if (TREE_CODE (type
) == ARRAY_TYPE
)
3191 /* Anything that can alias one of the array elements can alias
3192 the entire array as well. */
3193 TYPE_ALIAS_SET (type
) = c_get_alias_set (TREE_TYPE (type
));
3194 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
3195 /* There are no objects of FUNCTION_TYPE, so there's no point in
3196 using up an alias set for them. (There are, of course,
3197 pointers and references to functions, but that's
3199 TYPE_ALIAS_SET (type
) = 0;
3200 else if (TREE_CODE (type
) == RECORD_TYPE
3201 || TREE_CODE (type
) == UNION_TYPE
)
3202 /* If TYPE is a struct or union type then we're reading or
3203 writing an entire struct. Thus, we don't know anything about
3204 aliasing. (In theory, such an access can only alias objects
3205 whose type is the same as one of the fields, recursively, but
3206 we don't yet make any use of that information.) */
3207 TYPE_ALIAS_SET (type
) = 0;
3209 if (!TYPE_ALIAS_SET_KNOWN_P (type
))
3210 /* TYPE is something we haven't seen before. Put it in a new
3212 TYPE_ALIAS_SET (type
) = new_alias_set ();
3214 return TYPE_ALIAS_SET (type
);