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
,
60 enum format_type
{ printf_format_type
, scanf_format_type
,
61 strftime_format_type
};
63 static void declare_hidden_char_array
PROTO((char *, char *));
64 static void add_attribute
PROTO((enum attrs
, char *,
66 static void init_attributes
PROTO((void));
67 static void record_function_format
PROTO((tree
, tree
, enum format_type
,
69 static void record_international_format
PROTO((tree
, tree
, int));
70 static tree c_find_base_decl
PROTO((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. */
85 static if_elt
*if_stack
;
87 /* Amount of space in the if statement stack. */
88 static int if_stack_space
= 0;
91 static int if_stack_pointer
= 0;
93 /* Generate RTL for the start of an if-then, and record the start of it
94 for ambiguous else detection. */
96 /* A list of objects which have constructors or destructors which
97 reside in the global scope, and have an init_priority attribute
98 associated with them. The decl is stored in the TREE_VALUE slot
99 and the priority number is stored in the TREE_PURPOSE slot. */
100 tree static_aggregates_initp
;
103 c_expand_start_cond (cond
, exitflag
, compstmt_count
)
108 /* Make sure there is enough space on the stack. */
109 if (if_stack_space
== 0)
112 if_stack
= (if_elt
*)xmalloc (10 * sizeof (if_elt
));
114 else if (if_stack_space
== if_stack_pointer
)
116 if_stack_space
+= 10;
117 if_stack
= (if_elt
*)xrealloc (if_stack
, if_stack_space
* sizeof (if_elt
));
120 /* Record this if statement. */
121 if_stack
[if_stack_pointer
].compstmt_count
= compstmt_count
;
122 if_stack
[if_stack_pointer
].file
= input_filename
;
123 if_stack
[if_stack_pointer
].line
= lineno
;
124 if_stack
[if_stack_pointer
].needs_warning
= 0;
127 expand_start_cond (cond
, exitflag
);
130 /* Generate RTL for the end of an if-then. Optionally warn if a nested
131 if statement had an ambiguous else clause. */
137 if (if_stack
[if_stack_pointer
].needs_warning
)
138 warning_with_file_and_line (if_stack
[if_stack_pointer
].file
,
139 if_stack
[if_stack_pointer
].line
,
140 "suggest explicit braces to avoid ambiguous `else'");
144 /* Generate RTL between the then-clause and the else-clause
145 of an if-then-else. */
148 c_expand_start_else ()
150 /* An ambiguous else warning must be generated for the enclosing if
151 statement, unless we see an else branch for that one, too. */
153 && if_stack_pointer
> 1
154 && (if_stack
[if_stack_pointer
- 1].compstmt_count
155 == if_stack
[if_stack_pointer
- 2].compstmt_count
))
156 if_stack
[if_stack_pointer
- 2].needs_warning
= 1;
158 /* Even if a nested if statement had an else branch, it can't be
159 ambiguous if this one also has an else. So don't warn in that
160 case. Also don't warn for any if statements nested in this else. */
161 if_stack
[if_stack_pointer
- 1].needs_warning
= 0;
162 if_stack
[if_stack_pointer
- 1].compstmt_count
--;
164 expand_start_else ();
167 /* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__. */
170 declare_function_name ()
172 char *name
, *printable_name
;
174 if (current_function_decl
== NULL
)
177 printable_name
= "top level";
181 /* Allow functions to be nameless (such as artificial ones). */
182 if (DECL_NAME (current_function_decl
))
183 name
= IDENTIFIER_POINTER (DECL_NAME (current_function_decl
));
186 printable_name
= (*decl_printable_name
) (current_function_decl
, 2);
189 declare_hidden_char_array ("__FUNCTION__", name
);
190 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name
);
191 /* The ISO C people "of course" couldn't use __FUNCTION__ in the
192 ISO C 9x standard; instead a new variable is invented. */
193 declare_hidden_char_array ("__func__", name
);
197 declare_hidden_char_array (name
, value
)
200 tree decl
, type
, init
;
203 /* If the default size of char arrays isn't big enough for the name,
204 or if we want to give warnings for large objects, make a bigger one. */
205 vlen
= strlen (value
) + 1;
206 type
= char_array_type_node
;
207 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))) < vlen
209 type
= build_array_type (char_type_node
,
210 build_index_type (build_int_2 (vlen
, 0)));
211 push_obstacks_nochange ();
212 decl
= build_decl (VAR_DECL
, get_identifier (name
), type
);
213 TREE_STATIC (decl
) = 1;
214 TREE_READONLY (decl
) = 1;
215 TREE_ASM_WRITTEN (decl
) = 1;
216 DECL_SOURCE_LINE (decl
) = 0;
217 DECL_ARTIFICIAL (decl
) = 1;
218 DECL_IN_SYSTEM_HEADER (decl
) = 1;
219 DECL_IGNORED_P (decl
) = 1;
220 init
= build_string (vlen
, value
);
221 TREE_TYPE (init
) = type
;
222 DECL_INITIAL (decl
) = init
;
223 finish_decl (pushdecl (decl
), init
, NULL_TREE
);
226 /* Given a chain of STRING_CST nodes,
227 concatenate them into one STRING_CST
228 and give it a suitable array-of-chars data type. */
231 combine_strings (strings
)
234 register tree value
, t
;
235 register int length
= 1;
238 int wchar_bytes
= TYPE_PRECISION (wchar_type_node
) / BITS_PER_UNIT
;
241 if (TREE_CHAIN (strings
))
243 /* More than one in the chain, so concatenate. */
244 register char *p
, *q
;
246 /* Don't include the \0 at the end of each substring,
247 except for the last one.
248 Count wide strings and ordinary strings separately. */
249 for (t
= strings
; t
; t
= TREE_CHAIN (t
))
251 if (TREE_TYPE (t
) == wchar_array_type_node
)
253 wide_length
+= (TREE_STRING_LENGTH (t
) - wchar_bytes
);
257 length
+= (TREE_STRING_LENGTH (t
) - 1);
260 /* If anything is wide, the non-wides will be converted,
261 which makes them take more space. */
263 length
= length
* wchar_bytes
+ wide_length
;
265 p
= savealloc (length
);
267 /* Copy the individual strings into the new combined string.
268 If the combined string is wide, convert the chars to ints
269 for any individual strings that are not wide. */
272 for (t
= strings
; t
; t
= TREE_CHAIN (t
))
274 int len
= (TREE_STRING_LENGTH (t
)
275 - ((TREE_TYPE (t
) == wchar_array_type_node
)
277 if ((TREE_TYPE (t
) == wchar_array_type_node
) == wide_flag
)
279 memcpy (q
, TREE_STRING_POINTER (t
), len
);
285 for (i
= 0; i
< len
; i
++)
287 if (WCHAR_TYPE_SIZE
== HOST_BITS_PER_SHORT
)
288 ((short *) q
)[i
] = TREE_STRING_POINTER (t
)[i
];
290 ((int *) q
)[i
] = TREE_STRING_POINTER (t
)[i
];
292 q
+= len
* wchar_bytes
;
298 for (i
= 0; i
< wchar_bytes
; i
++)
304 value
= make_node (STRING_CST
);
305 TREE_STRING_POINTER (value
) = p
;
306 TREE_STRING_LENGTH (value
) = length
;
311 length
= TREE_STRING_LENGTH (value
);
312 if (TREE_TYPE (value
) == wchar_array_type_node
)
316 /* Compute the number of elements, for the array type. */
317 nchars
= wide_flag
? length
/ wchar_bytes
: length
;
319 /* Create the array type for the string constant.
320 -Wwrite-strings says make the string constant an array of const char
321 so that copying it to a non-const pointer will get a warning.
322 For C++, this is the standard behavior. */
323 if (flag_const_strings
324 && (! flag_traditional
&& ! flag_writable_strings
))
327 = build_type_variant (wide_flag
? wchar_type_node
: char_type_node
,
330 = build_array_type (elements
,
331 build_index_type (build_int_2 (nchars
- 1, 0)));
335 = build_array_type (wide_flag
? wchar_type_node
: char_type_node
,
336 build_index_type (build_int_2 (nchars
- 1, 0)));
338 TREE_READONLY (value
) = TREE_CONSTANT (value
) = ! flag_writable_strings
;
339 TREE_STATIC (value
) = 1;
343 /* To speed up processing of attributes, we maintain an array of
344 IDENTIFIER_NODES and the corresponding attribute types. */
346 /* Array to hold attribute information. */
348 static struct {enum attrs id
; tree name
; int min
, max
, decl_req
;} attrtab
[50];
350 static int attrtab_idx
= 0;
352 /* Add an entry to the attribute table above. */
355 add_attribute (id
, string
, min_len
, max_len
, decl_req
)
358 int min_len
, max_len
;
363 attrtab
[attrtab_idx
].id
= id
;
364 attrtab
[attrtab_idx
].name
= get_identifier (string
);
365 attrtab
[attrtab_idx
].min
= min_len
;
366 attrtab
[attrtab_idx
].max
= max_len
;
367 attrtab
[attrtab_idx
++].decl_req
= decl_req
;
369 sprintf (buf
, "__%s__", string
);
371 attrtab
[attrtab_idx
].id
= id
;
372 attrtab
[attrtab_idx
].name
= get_identifier (buf
);
373 attrtab
[attrtab_idx
].min
= min_len
;
374 attrtab
[attrtab_idx
].max
= max_len
;
375 attrtab
[attrtab_idx
++].decl_req
= decl_req
;
378 /* Initialize attribute table. */
383 add_attribute (A_PACKED
, "packed", 0, 0, 0);
384 add_attribute (A_NOCOMMON
, "nocommon", 0, 0, 1);
385 add_attribute (A_COMMON
, "common", 0, 0, 1);
386 add_attribute (A_NORETURN
, "noreturn", 0, 0, 1);
387 add_attribute (A_NORETURN
, "volatile", 0, 0, 1);
388 add_attribute (A_UNUSED
, "unused", 0, 0, 0);
389 add_attribute (A_CONST
, "const", 0, 0, 1);
390 add_attribute (A_T_UNION
, "transparent_union", 0, 0, 0);
391 add_attribute (A_CONSTRUCTOR
, "constructor", 0, 0, 1);
392 add_attribute (A_DESTRUCTOR
, "destructor", 0, 0, 1);
393 add_attribute (A_MODE
, "mode", 1, 1, 1);
394 add_attribute (A_SECTION
, "section", 1, 1, 1);
395 add_attribute (A_ALIGNED
, "aligned", 0, 1, 0);
396 add_attribute (A_FORMAT
, "format", 3, 3, 1);
397 add_attribute (A_FORMAT_ARG
, "format_arg", 1, 1, 1);
398 add_attribute (A_WEAK
, "weak", 0, 0, 1);
399 add_attribute (A_ALIAS
, "alias", 1, 1, 1);
400 add_attribute (A_INIT_PRIORITY
, "init_priority", 0, 1, 0);
401 add_attribute (A_NO_INSTRUMENT_FUNCTION
, "no_instrument_function", 0, 0, 1);
402 add_attribute (A_NO_CHECK_MEMORY_USAGE
, "no_check_memory_usage", 0, 0, 1);
405 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
406 and install them in NODE, which is either a DECL (including a TYPE_DECL)
407 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
408 and declaration modifiers but before the declaration proper. */
411 decl_attributes (node
, attributes
, prefix_attributes
)
412 tree node
, attributes
, prefix_attributes
;
414 tree decl
= 0, type
= 0;
418 if (attrtab_idx
== 0)
421 if (TREE_CODE_CLASS (TREE_CODE (node
)) == 'd')
424 type
= TREE_TYPE (decl
);
425 is_type
= TREE_CODE (node
) == TYPE_DECL
;
427 else if (TREE_CODE_CLASS (TREE_CODE (node
)) == 't')
428 type
= node
, is_type
= 1;
430 #ifdef PRAGMA_INSERT_ATTRIBUTES
431 /* If the code in c-pragma.c wants to insert some attributes then
432 allow it to do so. Do this before allowing machine back ends to
433 insert attributes, so that they have the opportunity to override
434 anything done here. */
435 PRAGMA_INSERT_ATTRIBUTES (node
, & attributes
, & prefix_attributes
);
438 #ifdef INSERT_ATTRIBUTES
439 INSERT_ATTRIBUTES (node
, & attributes
, & prefix_attributes
);
442 attributes
= chainon (prefix_attributes
, attributes
);
444 for (a
= attributes
; a
; a
= TREE_CHAIN (a
))
446 tree name
= TREE_PURPOSE (a
);
447 tree args
= TREE_VALUE (a
);
451 for (i
= 0; i
< attrtab_idx
; i
++)
452 if (attrtab
[i
].name
== name
)
455 if (i
== attrtab_idx
)
457 if (! valid_machine_attribute (name
, args
, decl
, type
))
458 warning ("`%s' attribute directive ignored",
459 IDENTIFIER_POINTER (name
));
461 type
= TREE_TYPE (decl
);
464 else if (attrtab
[i
].decl_req
&& decl
== 0)
466 warning ("`%s' attribute does not apply to types",
467 IDENTIFIER_POINTER (name
));
470 else if (list_length (args
) < attrtab
[i
].min
471 || list_length (args
) > attrtab
[i
].max
)
473 error ("wrong number of arguments specified for `%s' attribute",
474 IDENTIFIER_POINTER (name
));
483 TYPE_PACKED (type
) = 1;
484 else if (TREE_CODE (decl
) == FIELD_DECL
)
485 DECL_PACKED (decl
) = 1;
486 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
487 used for DECL_REGISTER. It wouldn't mean anything anyway. */
489 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
493 if (TREE_CODE (decl
) == VAR_DECL
)
494 DECL_COMMON (decl
) = 0;
496 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
500 if (TREE_CODE (decl
) == VAR_DECL
)
501 DECL_COMMON (decl
) = 1;
503 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
507 if (TREE_CODE (decl
) == FUNCTION_DECL
)
508 TREE_THIS_VOLATILE (decl
) = 1;
509 else if (TREE_CODE (type
) == POINTER_TYPE
510 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
511 TREE_TYPE (decl
) = type
513 (build_type_variant (TREE_TYPE (type
),
514 TREE_READONLY (TREE_TYPE (type
)), 1));
516 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
521 TREE_USED (type
) = 1;
522 else if (TREE_CODE (decl
) == PARM_DECL
523 || TREE_CODE (decl
) == VAR_DECL
524 || TREE_CODE (decl
) == FUNCTION_DECL
525 || TREE_CODE (decl
) == LABEL_DECL
)
526 TREE_USED (decl
) = 1;
528 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
532 if (TREE_CODE (decl
) == FUNCTION_DECL
)
533 TREE_READONLY (decl
) = 1;
534 else if (TREE_CODE (type
) == POINTER_TYPE
535 && TREE_CODE (TREE_TYPE (type
)) == FUNCTION_TYPE
)
536 TREE_TYPE (decl
) = type
538 (build_type_variant (TREE_TYPE (type
), 1,
539 TREE_THIS_VOLATILE (TREE_TYPE (type
))));
541 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name
));
546 && TREE_CODE (type
) == UNION_TYPE
548 || (TYPE_FIELDS (type
) != 0
549 && TYPE_MODE (type
) == DECL_MODE (TYPE_FIELDS (type
)))))
550 TYPE_TRANSPARENT_UNION (type
) = 1;
551 else if (decl
!= 0 && TREE_CODE (decl
) == PARM_DECL
552 && TREE_CODE (type
) == UNION_TYPE
553 && TYPE_MODE (type
) == DECL_MODE (TYPE_FIELDS (type
)))
554 DECL_TRANSPARENT_UNION (decl
) = 1;
556 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
560 if (TREE_CODE (decl
) == FUNCTION_DECL
561 && TREE_CODE (type
) == FUNCTION_TYPE
562 && decl_function_context (decl
) == 0)
564 DECL_STATIC_CONSTRUCTOR (decl
) = 1;
565 TREE_USED (decl
) = 1;
568 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
572 if (TREE_CODE (decl
) == FUNCTION_DECL
573 && TREE_CODE (type
) == FUNCTION_TYPE
574 && decl_function_context (decl
) == 0)
576 DECL_STATIC_DESTRUCTOR (decl
) = 1;
577 TREE_USED (decl
) = 1;
580 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
584 if (TREE_CODE (TREE_VALUE (args
)) != IDENTIFIER_NODE
)
585 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
589 char *p
= IDENTIFIER_POINTER (TREE_VALUE (args
));
590 int len
= strlen (p
);
591 enum machine_mode mode
= VOIDmode
;
594 if (len
> 4 && p
[0] == '_' && p
[1] == '_'
595 && p
[len
- 1] == '_' && p
[len
- 2] == '_')
597 char *newp
= (char *) alloca (len
- 1);
599 strcpy (newp
, &p
[2]);
600 newp
[len
- 4] = '\0';
604 /* Give this decl a type with the specified mode.
605 First check for the special modes. */
606 if (! strcmp (p
, "byte"))
608 else if (!strcmp (p
, "word"))
610 else if (! strcmp (p
, "pointer"))
613 for (j
= 0; j
< NUM_MACHINE_MODES
; j
++)
614 if (!strcmp (p
, GET_MODE_NAME (j
)))
615 mode
= (enum machine_mode
) j
;
617 if (mode
== VOIDmode
)
618 error ("unknown machine mode `%s'", p
);
619 else if (0 == (typefm
= type_for_mode (mode
,
620 TREE_UNSIGNED (type
))))
621 error ("no data type for mode `%s'", p
);
624 TREE_TYPE (decl
) = type
= typefm
;
625 DECL_SIZE (decl
) = 0;
626 layout_decl (decl
, 0);
632 #ifdef ASM_OUTPUT_SECTION_NAME
633 if ((TREE_CODE (decl
) == FUNCTION_DECL
634 || TREE_CODE (decl
) == VAR_DECL
)
635 && TREE_CODE (TREE_VALUE (args
)) == STRING_CST
)
637 if (TREE_CODE (decl
) == VAR_DECL
638 && current_function_decl
!= NULL_TREE
639 && ! TREE_STATIC (decl
))
640 error_with_decl (decl
,
641 "section attribute cannot be specified for local variables");
642 /* The decl may have already been given a section attribute from
643 a previous declaration. Ensure they match. */
644 else if (DECL_SECTION_NAME (decl
) != NULL_TREE
645 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl
)),
646 TREE_STRING_POINTER (TREE_VALUE (args
))) != 0)
647 error_with_decl (node
,
648 "section of `%s' conflicts with previous declaration");
650 DECL_SECTION_NAME (decl
) = TREE_VALUE (args
);
653 error_with_decl (node
,
654 "section attribute not allowed for `%s'");
656 error_with_decl (node
,
657 "section attributes are not supported for this target");
664 = (args
? TREE_VALUE (args
)
665 : size_int (BIGGEST_ALIGNMENT
/ BITS_PER_UNIT
));
668 /* Strip any NOPs of any kind. */
669 while (TREE_CODE (align_expr
) == NOP_EXPR
670 || TREE_CODE (align_expr
) == CONVERT_EXPR
671 || TREE_CODE (align_expr
) == NON_LVALUE_EXPR
)
672 align_expr
= TREE_OPERAND (align_expr
, 0);
674 if (TREE_CODE (align_expr
) != INTEGER_CST
)
676 error ("requested alignment is not a constant");
680 align
= TREE_INT_CST_LOW (align_expr
) * BITS_PER_UNIT
;
682 if (exact_log2 (align
) == -1)
683 error ("requested alignment is not a power of 2");
685 TYPE_ALIGN (type
) = align
;
686 else if (TREE_CODE (decl
) != VAR_DECL
687 && TREE_CODE (decl
) != FIELD_DECL
)
688 error_with_decl (decl
,
689 "alignment may not be specified for `%s'");
691 DECL_ALIGN (decl
) = align
;
697 tree format_type_id
= TREE_VALUE (args
);
698 tree format_num_expr
= TREE_VALUE (TREE_CHAIN (args
));
699 tree first_arg_num_expr
700 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args
)));
703 enum format_type format_type
;
707 if (TREE_CODE (decl
) != FUNCTION_DECL
)
709 error_with_decl (decl
,
710 "argument format specified for non-function `%s'");
714 if (TREE_CODE (format_type_id
) != IDENTIFIER_NODE
)
716 error ("unrecognized format specifier");
721 char *p
= IDENTIFIER_POINTER (format_type_id
);
723 if (!strcmp (p
, "printf") || !strcmp (p
, "__printf__"))
724 format_type
= printf_format_type
;
725 else if (!strcmp (p
, "scanf") || !strcmp (p
, "__scanf__"))
726 format_type
= scanf_format_type
;
727 else if (!strcmp (p
, "strftime")
728 || !strcmp (p
, "__strftime__"))
729 format_type
= strftime_format_type
;
732 error ("`%s' is an unrecognized format function type", p
);
737 /* Strip any conversions from the string index and first arg number
738 and verify they are constants. */
739 while (TREE_CODE (format_num_expr
) == NOP_EXPR
740 || TREE_CODE (format_num_expr
) == CONVERT_EXPR
741 || TREE_CODE (format_num_expr
) == NON_LVALUE_EXPR
)
742 format_num_expr
= TREE_OPERAND (format_num_expr
, 0);
744 while (TREE_CODE (first_arg_num_expr
) == NOP_EXPR
745 || TREE_CODE (first_arg_num_expr
) == CONVERT_EXPR
746 || TREE_CODE (first_arg_num_expr
) == NON_LVALUE_EXPR
)
747 first_arg_num_expr
= TREE_OPERAND (first_arg_num_expr
, 0);
749 if (TREE_CODE (format_num_expr
) != INTEGER_CST
750 || TREE_CODE (first_arg_num_expr
) != INTEGER_CST
)
752 error ("format string has non-constant operand number");
756 format_num
= TREE_INT_CST_LOW (format_num_expr
);
757 first_arg_num
= TREE_INT_CST_LOW (first_arg_num_expr
);
758 if (first_arg_num
!= 0 && first_arg_num
<= format_num
)
760 error ("format string arg follows the args to be formatted");
764 /* If a parameter list is specified, verify that the format_num
765 argument is actually a string, in case the format attribute
767 argument
= TYPE_ARG_TYPES (type
);
770 for (arg_num
= 1; ; ++arg_num
)
772 if (argument
== 0 || arg_num
== format_num
)
774 argument
= TREE_CHAIN (argument
);
777 || TREE_CODE (TREE_VALUE (argument
)) != POINTER_TYPE
778 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument
)))
781 error ("format string arg not a string type");
784 if (first_arg_num
!= 0)
786 /* Verify that first_arg_num points to the last arg,
789 arg_num
++, argument
= TREE_CHAIN (argument
);
790 if (arg_num
!= first_arg_num
)
792 error ("args to be formatted is not ...");
798 record_function_format (DECL_NAME (decl
),
799 DECL_ASSEMBLER_NAME (decl
),
800 format_type
, format_num
, first_arg_num
);
806 tree format_num_expr
= TREE_VALUE (args
);
807 int format_num
, arg_num
;
810 if (TREE_CODE (decl
) != FUNCTION_DECL
)
812 error_with_decl (decl
,
813 "argument format specified for non-function `%s'");
817 /* Strip any conversions from the first arg number and verify it
819 while (TREE_CODE (format_num_expr
) == NOP_EXPR
820 || TREE_CODE (format_num_expr
) == CONVERT_EXPR
821 || TREE_CODE (format_num_expr
) == NON_LVALUE_EXPR
)
822 format_num_expr
= TREE_OPERAND (format_num_expr
, 0);
824 if (TREE_CODE (format_num_expr
) != INTEGER_CST
)
826 error ("format string has non-constant operand number");
830 format_num
= TREE_INT_CST_LOW (format_num_expr
);
832 /* If a parameter list is specified, verify that the format_num
833 argument is actually a string, in case the format attribute
835 argument
= TYPE_ARG_TYPES (type
);
838 for (arg_num
= 1; ; ++arg_num
)
840 if (argument
== 0 || arg_num
== format_num
)
842 argument
= TREE_CHAIN (argument
);
845 || TREE_CODE (TREE_VALUE (argument
)) != POINTER_TYPE
846 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument
)))
849 error ("format string arg not a string type");
854 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl
))) != POINTER_TYPE
855 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl
))))
858 error ("function does not return string type");
862 record_international_format (DECL_NAME (decl
),
863 DECL_ASSEMBLER_NAME (decl
),
873 if ((TREE_CODE (decl
) == FUNCTION_DECL
&& DECL_INITIAL (decl
))
874 || (TREE_CODE (decl
) != FUNCTION_DECL
&& ! DECL_EXTERNAL (decl
)))
875 error_with_decl (decl
,
876 "`%s' defined both normally and as an alias");
877 else if (decl_function_context (decl
) == 0)
881 id
= TREE_VALUE (args
);
882 if (TREE_CODE (id
) != STRING_CST
)
884 error ("alias arg not a string");
887 id
= get_identifier (TREE_STRING_POINTER (id
));
889 if (TREE_CODE (decl
) == FUNCTION_DECL
)
890 DECL_INITIAL (decl
) = error_mark_node
;
892 DECL_EXTERNAL (decl
) = 0;
893 assemble_alias (decl
, id
);
896 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name
));
899 case A_NO_CHECK_MEMORY_USAGE
:
900 if (TREE_CODE (decl
) != FUNCTION_DECL
)
902 error_with_decl (decl
,
903 "`%s' attribute applies only to functions",
904 IDENTIFIER_POINTER (name
));
906 else if (DECL_INITIAL (decl
))
908 error_with_decl (decl
,
909 "can't set `%s' attribute after definition",
910 IDENTIFIER_POINTER (name
));
913 DECL_NO_CHECK_MEMORY_USAGE (decl
) = 1;
916 case A_INIT_PRIORITY
:
918 tree initp_expr
= (args
? TREE_VALUE (args
): NULL_TREE
);
922 STRIP_NOPS (initp_expr
);
924 if (!initp_expr
|| TREE_CODE (initp_expr
) != INTEGER_CST
)
926 error ("requested init_priority is not an integer constant");
930 pri
= TREE_INT_CST_LOW (initp_expr
);
932 if (is_type
|| TREE_CODE (decl
) != VAR_DECL
933 || ! TREE_STATIC (decl
)
934 || DECL_EXTERNAL (decl
)
935 || (TREE_CODE (TREE_TYPE (decl
)) != RECORD_TYPE
936 && TREE_CODE (TREE_TYPE (decl
)) != UNION_TYPE
)
937 /* Static objects in functions are initialized the
938 first time control passes through that
939 function. This is not precise enough to pin down an
940 init_priority value, so don't allow it. */
941 || current_function_decl
)
943 error ("can only use init_priority attribute on file-scope definitions of objects of class type");
947 /* Check for init_priorities that are reserved for
948 implementation. Reserved for language and runtime
949 support implementations.*/
950 if ((10 <= pri
&& pri
<= 99)
951 /* Reserved for standard library implementations. */
952 || (500 <= pri
&& pri
<= 999)
953 /* Reserved for objects with no attributes. */
954 || pri
> (MAX_INIT_PRIORITY
- 50))
957 ("requested init_priority is reserved for internal use");
961 if (pri
> MAX_INIT_PRIORITY
|| pri
<= 0)
963 error ("requested init_priority is out of range");
967 static_aggregates_initp
968 = perm_tree_cons (initp_expr
, decl
, static_aggregates_initp
);
972 case A_NO_INSTRUMENT_FUNCTION
:
973 if (TREE_CODE (decl
) != FUNCTION_DECL
)
975 error_with_decl (decl
,
976 "`%s' attribute applies only to functions",
977 IDENTIFIER_POINTER (name
));
979 else if (DECL_INITIAL (decl
))
981 error_with_decl (decl
,
982 "can't set `%s' attribute after definition",
983 IDENTIFIER_POINTER (name
));
986 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl
) = 1;
992 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
993 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
995 The head of the declspec list is stored in DECLSPECS.
996 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
998 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
999 the list elements. We drop the containing TREE_LIST nodes and link the
1000 resulting attributes together the way decl_attributes expects them. */
1003 split_specs_attrs (specs_attrs
, declspecs
, prefix_attributes
)
1005 tree
*declspecs
, *prefix_attributes
;
1007 tree t
, s
, a
, next
, specs
, attrs
;
1009 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1010 if (specs_attrs
!= NULL_TREE
1011 && TREE_CODE (specs_attrs
) != TREE_LIST
)
1013 *declspecs
= specs_attrs
;
1014 *prefix_attributes
= NULL_TREE
;
1018 /* Remember to keep the lists in the same order, element-wise. */
1020 specs
= s
= NULL_TREE
;
1021 attrs
= a
= NULL_TREE
;
1022 for (t
= specs_attrs
; t
; t
= next
)
1024 next
= TREE_CHAIN (t
);
1025 /* Declspecs have a non-NULL TREE_VALUE. */
1026 if (TREE_VALUE (t
) != NULL_TREE
)
1028 if (specs
== NULL_TREE
)
1038 if (attrs
== NULL_TREE
)
1039 attrs
= a
= TREE_PURPOSE (t
);
1042 TREE_CHAIN (a
) = TREE_PURPOSE (t
);
1043 a
= TREE_PURPOSE (t
);
1045 /* More attrs can be linked here, move A to the end. */
1046 while (TREE_CHAIN (a
) != NULL_TREE
)
1051 /* Terminate the lists. */
1053 TREE_CHAIN (s
) = NULL_TREE
;
1055 TREE_CHAIN (a
) = NULL_TREE
;
1059 *prefix_attributes
= attrs
;
1062 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1063 This function is used by the parser when a rule will accept attributes
1064 in a particular position, but we don't want to support that just yet.
1066 A warning is issued for every ignored attribute. */
1069 strip_attrs (specs_attrs
)
1074 split_specs_attrs (specs_attrs
, &specs
, &attrs
);
1078 warning ("`%s' attribute ignored",
1079 IDENTIFIER_POINTER (TREE_PURPOSE (attrs
)));
1080 attrs
= TREE_CHAIN (attrs
);
1086 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1087 a parameter list. */
1089 #define T_I &integer_type_node
1090 #define T_L &long_integer_type_node
1091 #define T_LL &long_long_integer_type_node
1092 #define T_S &short_integer_type_node
1093 #define T_UI &unsigned_type_node
1094 #define T_UL &long_unsigned_type_node
1095 #define T_ULL &long_long_unsigned_type_node
1096 #define T_US &short_unsigned_type_node
1097 #define T_F &float_type_node
1098 #define T_D &double_type_node
1099 #define T_LD &long_double_type_node
1100 #define T_C &char_type_node
1101 #define T_UC &unsigned_char_type_node
1102 #define T_V &void_type_node
1103 #define T_W &wchar_type_node
1104 #define T_ST &sizetype
1109 /* Type of argument if no length modifier is used. */
1111 /* Type of argument if length modifier for shortening to byte is used.
1112 If NULL, then this modifier is not allowed. */
1114 /* Type of argument if length modifier for shortening is used.
1115 If NULL, then this modifier is not allowed. */
1117 /* Type of argument if length modifier `l' is used.
1118 If NULL, then this modifier is not allowed. */
1120 /* Type of argument if length modifier `q' or `ll' is used.
1121 If NULL, then this modifier is not allowed. */
1123 /* Type of argument if length modifier `L' is used.
1124 If NULL, then this modifier is not allowed. */
1126 /* Type of argument if length modifier `Z' is used.
1127 If NULL, then this modifier is not allowed. */
1129 /* List of other modifier characters allowed with these options. */
1133 static format_char_info print_char_table
[] = {
1134 { "di", 0, T_I
, T_I
, T_I
, T_L
, T_LL
, T_LL
, T_ST
, "-wp0 +" },
1135 { "oxX", 0, T_UI
, T_UI
, T_UI
, T_UL
, T_ULL
, T_ULL
, T_ST
, "-wp0#" },
1136 { "u", 0, T_UI
, T_UI
, T_UI
, T_UL
, T_ULL
, T_ULL
, T_ST
, "-wp0" },
1137 /* A GNU extension. */
1138 { "m", 0, T_V
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-wp" },
1139 { "feEgGaA", 0, T_D
, NULL
, NULL
, NULL
, NULL
, T_LD
, NULL
, "-wp0 +#" },
1140 { "c", 0, T_I
, NULL
, NULL
, T_W
, NULL
, NULL
, NULL
, "-w" },
1141 { "C", 0, T_W
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-w" },
1142 { "s", 1, T_C
, NULL
, NULL
, T_W
, NULL
, NULL
, NULL
, "-wp" },
1143 { "S", 1, T_W
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-wp" },
1144 { "p", 1, T_V
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-w" },
1145 { "n", 1, T_I
, NULL
, T_S
, T_L
, T_LL
, NULL
, NULL
, "" },
1146 { NULL
, 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
}
1149 static format_char_info scan_char_table
[] = {
1150 { "di", 1, T_I
, T_C
, T_S
, T_L
, T_LL
, T_LL
, NULL
, "*" },
1151 { "ouxX", 1, T_UI
, T_UC
, T_US
, T_UL
, T_ULL
, T_ULL
, NULL
, "*" },
1152 { "efgEGaA", 1, T_F
, NULL
, NULL
, T_D
, NULL
, T_LD
, NULL
, "*" },
1153 { "c", 1, T_C
, NULL
, NULL
, T_W
, NULL
, NULL
, NULL
, "*" },
1154 { "s", 1, T_C
, NULL
, NULL
, T_W
, NULL
, NULL
, NULL
, "*a" },
1155 { "[", 1, T_C
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "*a" },
1156 { "C", 1, T_W
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "*" },
1157 { "S", 1, T_W
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "*a" },
1158 { "p", 2, T_V
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "*" },
1159 { "n", 1, T_I
, T_C
, T_S
, T_L
, T_LL
, NULL
, NULL
, "" },
1160 { NULL
, 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
}
1163 /* Handle format characters recognized by glibc's strftime.c.
1164 '2' - MUST do years as only two digits
1165 '3' - MAY do years as only two digits (depending on locale)
1166 'E' - E modifier is acceptable
1167 'O' - O modifier is acceptable to Standard C
1168 'o' - O modifier is acceptable as a GNU extension
1169 'G' - other GNU extensions */
1171 static format_char_info time_char_table
[] = {
1172 { "y", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "2EO-_0w" },
1173 { "D", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "2" },
1174 { "g", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "2O-_0w" },
1175 { "cx", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "3E" },
1176 { "%RTXnrt", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "" },
1177 { "P", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "G" },
1178 { "HIMSUWdemw", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-_0Ow" },
1179 { "Vju", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-_0Oow" },
1180 { "Gklsz", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-_0OGw" },
1181 { "ABZa", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "^#" },
1182 { "p", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "#" },
1183 { "bh", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "^" },
1184 { "CY", 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, "-_0EOw" },
1185 { NULL
, 0, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
}
1188 typedef struct function_format_info
1190 struct function_format_info
*next
; /* next structure on the list */
1191 tree name
; /* identifier such as "printf" */
1192 tree assembler_name
; /* optional mangled identifier (for C++) */
1193 enum format_type format_type
; /* type of format (printf, scanf, etc.) */
1194 int format_num
; /* number of format argument */
1195 int first_arg_num
; /* number of first arg (zero for varargs) */
1196 } function_format_info
;
1198 static function_format_info
*function_format_list
= NULL
;
1200 typedef struct international_format_info
1202 struct international_format_info
*next
; /* next structure on the list */
1203 tree name
; /* identifier such as "gettext" */
1204 tree assembler_name
; /* optional mangled identifier (for C++) */
1205 int format_num
; /* number of format argument */
1206 } international_format_info
;
1208 static international_format_info
*international_format_list
= NULL
;
1210 static void check_format_info
PROTO((function_format_info
*, tree
));
1212 /* Initialize the table of functions to perform format checking on.
1213 The ANSI functions are always checked (whether <stdio.h> is
1214 included or not), since it is common to call printf without
1215 including <stdio.h>. There shouldn't be a problem with this,
1216 since ANSI reserves these function names whether you include the
1217 header file or not. In any case, the checking is harmless.
1219 Also initialize the name of function that modify the format string for
1220 internationalization purposes. */
1223 init_function_format_info ()
1225 record_function_format (get_identifier ("printf"), NULL_TREE
,
1226 printf_format_type
, 1, 2);
1227 record_function_format (get_identifier ("fprintf"), NULL_TREE
,
1228 printf_format_type
, 2, 3);
1229 record_function_format (get_identifier ("sprintf"), NULL_TREE
,
1230 printf_format_type
, 2, 3);
1231 record_function_format (get_identifier ("scanf"), NULL_TREE
,
1232 scanf_format_type
, 1, 2);
1233 record_function_format (get_identifier ("fscanf"), NULL_TREE
,
1234 scanf_format_type
, 2, 3);
1235 record_function_format (get_identifier ("sscanf"), NULL_TREE
,
1236 scanf_format_type
, 2, 3);
1237 record_function_format (get_identifier ("vprintf"), NULL_TREE
,
1238 printf_format_type
, 1, 0);
1239 record_function_format (get_identifier ("vfprintf"), NULL_TREE
,
1240 printf_format_type
, 2, 0);
1241 record_function_format (get_identifier ("vsprintf"), NULL_TREE
,
1242 printf_format_type
, 2, 0);
1243 record_function_format (get_identifier ("strftime"), NULL_TREE
,
1244 strftime_format_type
, 3, 0);
1246 record_international_format (get_identifier ("gettext"), NULL_TREE
, 1);
1247 record_international_format (get_identifier ("dgettext"), NULL_TREE
, 2);
1248 record_international_format (get_identifier ("dcgettext"), NULL_TREE
, 2);
1251 /* Record information for argument format checking. FUNCTION_IDENT is
1252 the identifier node for the name of the function to check (its decl
1253 need not exist yet).
1254 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
1255 of the argument which is the format control string (starting from 1).
1256 FIRST_ARG_NUM is the number of the first actual argument to check
1257 against the format string, or zero if no checking is not be done
1258 (e.g. for varargs such as vfprintf). */
1261 record_function_format (name
, assembler_name
, format_type
,
1262 format_num
, first_arg_num
)
1264 tree assembler_name
;
1265 enum format_type format_type
;
1269 function_format_info
*info
;
1271 /* Re-use existing structure if it's there. */
1273 for (info
= function_format_list
; info
; info
= info
->next
)
1275 if (info
->name
== name
&& info
->assembler_name
== assembler_name
)
1280 info
= (function_format_info
*) xmalloc (sizeof (function_format_info
));
1281 info
->next
= function_format_list
;
1282 function_format_list
= info
;
1285 info
->assembler_name
= assembler_name
;
1288 info
->format_type
= format_type
;
1289 info
->format_num
= format_num
;
1290 info
->first_arg_num
= first_arg_num
;
1293 /* Record information for the names of function that modify the format
1294 argument to format functions. FUNCTION_IDENT is the identifier node for
1295 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1296 the number of the argument which is the format control string (starting
1300 record_international_format (name
, assembler_name
, format_num
)
1302 tree assembler_name
;
1305 international_format_info
*info
;
1307 /* Re-use existing structure if it's there. */
1309 for (info
= international_format_list
; info
; info
= info
->next
)
1311 if (info
->name
== name
&& info
->assembler_name
== assembler_name
)
1318 = (international_format_info
*)
1319 xmalloc (sizeof (international_format_info
));
1320 info
->next
= international_format_list
;
1321 international_format_list
= info
;
1324 info
->assembler_name
= assembler_name
;
1327 info
->format_num
= format_num
;
1330 static char tfaff
[] = "too few arguments for format";
1332 /* Check the argument list of a call to printf, scanf, etc.
1333 NAME is the function identifier.
1334 ASSEMBLER_NAME is the function's assembler identifier.
1335 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1336 PARAMS is the list of argument values. */
1339 check_function_format (name
, assembler_name
, params
)
1341 tree assembler_name
;
1344 function_format_info
*info
;
1346 /* See if this function is a format function. */
1347 for (info
= function_format_list
; info
; info
= info
->next
)
1349 if (info
->assembler_name
1350 ? (info
->assembler_name
== assembler_name
)
1351 : (info
->name
== name
))
1353 /* Yup; check it. */
1354 check_format_info (info
, params
);
1360 /* Check the argument list of a call to printf, scanf, etc.
1361 INFO points to the function_format_info structure.
1362 PARAMS is the list of argument values. */
1365 check_format_info (info
, params
)
1366 function_format_info
*info
;
1371 int suppressed
, wide
, precise
;
1372 int length_char
= 0;
1379 tree first_fillin_param
;
1381 format_char_info
*fci
= NULL
;
1383 int has_operand_number
= 0;
1385 /* Skip to format argument. If the argument isn't available, there's
1386 no work for us to do; prototype checking will catch the problem. */
1387 for (arg_num
= 1; ; ++arg_num
)
1391 if (arg_num
== info
->format_num
)
1393 params
= TREE_CHAIN (params
);
1395 format_tree
= TREE_VALUE (params
);
1396 params
= TREE_CHAIN (params
);
1397 if (format_tree
== 0)
1400 /* We can only check the format if it's a string constant. */
1401 while (TREE_CODE (format_tree
) == NOP_EXPR
)
1402 format_tree
= TREE_OPERAND (format_tree
, 0); /* strip coercion */
1404 if (TREE_CODE (format_tree
) == CALL_EXPR
1405 && TREE_CODE (TREE_OPERAND (format_tree
, 0)) == ADDR_EXPR
1406 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree
, 0), 0))
1409 tree function
= TREE_OPERAND (TREE_OPERAND (format_tree
, 0), 0);
1411 /* See if this is a call to a known internationalization function
1412 that modifies the format arg. */
1413 international_format_info
*info
;
1415 for (info
= international_format_list
; info
; info
= info
->next
)
1416 if (info
->assembler_name
1417 ? (info
->assembler_name
== DECL_ASSEMBLER_NAME (function
))
1418 : (info
->name
== DECL_NAME (function
)))
1423 for (inner_args
= TREE_OPERAND (format_tree
, 1), i
= 1;
1425 inner_args
= TREE_CHAIN (inner_args
), i
++)
1426 if (i
== info
->format_num
)
1428 format_tree
= TREE_VALUE (inner_args
);
1430 while (TREE_CODE (format_tree
) == NOP_EXPR
)
1431 format_tree
= TREE_OPERAND (format_tree
, 0);
1436 if (integer_zerop (format_tree
))
1438 warning ("null format string");
1441 if (TREE_CODE (format_tree
) != ADDR_EXPR
)
1443 format_tree
= TREE_OPERAND (format_tree
, 0);
1444 if (TREE_CODE (format_tree
) != STRING_CST
)
1446 format_chars
= TREE_STRING_POINTER (format_tree
);
1447 format_length
= TREE_STRING_LENGTH (format_tree
);
1448 if (format_length
<= 1)
1449 warning ("zero-length format string");
1450 if (format_chars
[--format_length
] != 0)
1452 warning ("unterminated format string");
1455 /* Skip to first argument to check. */
1456 while (arg_num
+ 1 < info
->first_arg_num
)
1460 params
= TREE_CHAIN (params
);
1464 first_fillin_param
= params
;
1468 if (*format_chars
== 0)
1470 if (format_chars
- TREE_STRING_POINTER (format_tree
) != format_length
)
1471 warning ("embedded `\\0' in format");
1472 if (info
->first_arg_num
!= 0 && params
!= 0 && ! has_operand_number
)
1473 warning ("too many arguments for format");
1476 if (*format_chars
++ != '%')
1478 if (*format_chars
== 0)
1480 warning ("spurious trailing `%%' in format");
1483 if (*format_chars
== '%')
1489 suppressed
= wide
= precise
= FALSE
;
1490 if (info
->format_type
== scanf_format_type
)
1492 suppressed
= *format_chars
== '*';
1495 while (ISDIGIT (*format_chars
))
1498 else if (info
->format_type
== strftime_format_type
)
1500 while (*format_chars
!= 0 && index ("_-0^#", *format_chars
) != 0)
1503 warning ("ANSI C does not support the strftime `%c' flag",
1505 if (index (flag_chars
, *format_chars
) != 0)
1507 warning ("repeated `%c' flag in format",
1513 i
= strlen (flag_chars
);
1514 flag_chars
[i
++] = *format_chars
++;
1518 while (ISDIGIT ((unsigned char) *format_chars
))
1523 if (wide
&& pedantic
)
1524 warning ("ANSI C does not support strftime format width");
1525 if (*format_chars
== 'E' || *format_chars
== 'O')
1527 i
= strlen (flag_chars
);
1528 flag_chars
[i
++] = *format_chars
++;
1530 if (*format_chars
== 'E' || *format_chars
== 'O')
1532 warning ("multiple E/O modifiers in format");
1533 while (*format_chars
== 'E' || *format_chars
== 'O')
1538 else if (info
->format_type
== printf_format_type
)
1540 /* See if we have a number followed by a dollar sign. If we do,
1541 it is an operand number, so set PARAMS to that operand. */
1542 if (*format_chars
>= '0' && *format_chars
<= '9')
1544 char *p
= format_chars
;
1546 while (*p
>= '0' && *p
++ <= '9')
1551 int opnum
= atoi (format_chars
);
1553 params
= first_fillin_param
;
1554 format_chars
= p
+ 1;
1555 has_operand_number
= 1;
1557 for (i
= 1; i
< opnum
&& params
!= 0; i
++)
1558 params
= TREE_CHAIN (params
);
1560 if (opnum
== 0 || params
== 0)
1562 warning ("operand number out of range in format");
1568 while (*format_chars
!= 0 && index (" +#0-", *format_chars
) != 0)
1570 if (index (flag_chars
, *format_chars
) != 0)
1571 warning ("repeated `%c' flag in format", *format_chars
++);
1574 i
= strlen (flag_chars
);
1575 flag_chars
[i
++] = *format_chars
++;
1579 /* "If the space and + flags both appear,
1580 the space flag will be ignored." */
1581 if (index (flag_chars
, ' ') != 0
1582 && index (flag_chars
, '+') != 0)
1583 warning ("use of both ` ' and `+' flags in format");
1584 /* "If the 0 and - flags both appear,
1585 the 0 flag will be ignored." */
1586 if (index (flag_chars
, '0') != 0
1587 && index (flag_chars
, '-') != 0)
1588 warning ("use of both `0' and `-' flags in format");
1589 if (*format_chars
== '*')
1592 /* "...a field width...may be indicated by an asterisk.
1593 In this case, an int argument supplies the field width..." */
1600 if (info
->first_arg_num
!= 0)
1602 cur_param
= TREE_VALUE (params
);
1603 params
= TREE_CHAIN (params
);
1605 /* size_t is generally not valid here.
1606 It will work on most machines, because size_t and int
1607 have the same mode. But might as well warn anyway,
1608 since it will fail on other machines. */
1609 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1610 != integer_type_node
)
1612 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1613 != unsigned_type_node
))
1614 warning ("field width is not type int (arg %d)", arg_num
);
1619 while (ISDIGIT (*format_chars
))
1625 if (*format_chars
== '.')
1629 if (*format_chars
!= '*' && !ISDIGIT (*format_chars
))
1630 warning ("`.' not followed by `*' or digit in format");
1631 /* "...a...precision...may be indicated by an asterisk.
1632 In this case, an int argument supplies the...precision." */
1633 if (*format_chars
== '*')
1635 if (info
->first_arg_num
!= 0)
1643 cur_param
= TREE_VALUE (params
);
1644 params
= TREE_CHAIN (params
);
1646 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param
))
1647 != integer_type_node
)
1648 warning ("field width is not type int (arg %d)",
1654 while (ISDIGIT (*format_chars
))
1662 if (info
->format_type
!= strftime_format_type
)
1664 if (*format_chars
== 'h' || *format_chars
== 'l')
1665 length_char
= *format_chars
++;
1666 else if (*format_chars
== 'q' || *format_chars
== 'L')
1668 length_char
= *format_chars
++;
1670 warning ("ANSI C does not support the `%c' length modifier",
1673 else if (*format_chars
== 'Z')
1675 length_char
= *format_chars
++;
1677 warning ("ANSI C does not support the `Z' length modifier");
1681 if (length_char
== 'l' && *format_chars
== 'l')
1683 length_char
= 'q', format_chars
++;
1684 /* FIXME: Is allowed in ISO C 9x. */
1686 warning ("ANSI C does not support the `ll' length modifier");
1688 else if (length_char
== 'h' && *format_chars
== 'h')
1690 length_char
= 'H', format_chars
++;
1691 /* FIXME: Is allowed in ISO C 9x. */
1693 warning ("ANSI C does not support the `hh' length modifier");
1695 if (*format_chars
== 'a' && info
->format_type
== scanf_format_type
)
1697 if (format_chars
[1] == 's' || format_chars
[1] == 'S'
1698 || format_chars
[1] == '[')
1700 /* `a' is used as a flag. */
1705 if (suppressed
&& length_char
!= 0)
1706 warning ("use of `*' and `%c' together in format", length_char
);
1708 format_char
= *format_chars
;
1709 if (format_char
== 0
1710 || (info
->format_type
!= strftime_format_type
&& format_char
== '%'))
1712 warning ("conversion lacks type at end of format");
1715 /* The m, C, and S formats are GNU extensions. */
1716 if (pedantic
&& info
->format_type
!= strftime_format_type
1717 && (format_char
== 'm' || format_char
== 'C' || format_char
== 'S'))
1718 warning ("ANSI C does not support the `%c' format", format_char
);
1719 /* ??? The a and A formats are C9X extensions, and should be allowed
1720 when a C9X option is added. */
1721 if (pedantic
&& info
->format_type
!= strftime_format_type
1722 && (format_char
== 'a' || format_char
== 'A'))
1723 warning ("ANSI C does not support the `%c' format", format_char
);
1725 switch (info
->format_type
)
1727 case printf_format_type
:
1728 fci
= print_char_table
;
1730 case scanf_format_type
:
1731 fci
= scan_char_table
;
1733 case strftime_format_type
:
1734 fci
= time_char_table
;
1739 while (fci
->format_chars
!= 0
1740 && index (fci
->format_chars
, format_char
) == 0)
1742 if (fci
->format_chars
== 0)
1744 if (format_char
>= 040 && format_char
< 0177)
1745 warning ("unknown conversion type character `%c' in format",
1748 warning ("unknown conversion type character 0x%x in format",
1754 if (index (fci
->flag_chars
, 'G') != 0)
1755 warning ("ANSI C does not support `%%%c'", format_char
);
1756 if (index (fci
->flag_chars
, 'o') != 0
1757 && index (flag_chars
, 'O') != 0)
1758 warning ("ANSI C does not support `%%O%c'", format_char
);
1760 if (wide
&& index (fci
->flag_chars
, 'w') == 0)
1761 warning ("width used with `%c' format", format_char
);
1762 if (index (fci
->flag_chars
, '2') != 0)
1763 warning ("`%%%c' yields only last 2 digits of year", format_char
);
1764 else if (index (fci
->flag_chars
, '3') != 0)
1765 warning ("`%%%c' yields only last 2 digits of year in some locales",
1767 if (precise
&& index (fci
->flag_chars
, 'p') == 0)
1768 warning ("precision used with `%c' format", format_char
);
1769 if (aflag
&& index (fci
->flag_chars
, 'a') == 0)
1771 warning ("`a' flag used with `%c' format", format_char
);
1772 /* To simplify the following code. */
1775 /* The a flag is a GNU extension. */
1776 else if (pedantic
&& aflag
)
1777 warning ("ANSI C does not support the `a' flag");
1778 if (info
->format_type
== scanf_format_type
&& format_char
== '[')
1780 /* Skip over scan set, in case it happens to have '%' in it. */
1781 if (*format_chars
== '^')
1783 /* Find closing bracket; if one is hit immediately, then
1784 it's part of the scan set rather than a terminator. */
1785 if (*format_chars
== ']')
1787 while (*format_chars
&& *format_chars
!= ']')
1789 if (*format_chars
!= ']')
1790 /* The end of the format string was reached. */
1791 warning ("no closing `]' for `%%[' format");
1795 if (index (fci
->flag_chars
, '*') == 0)
1796 warning ("suppression of `%c' conversion in format", format_char
);
1799 for (i
= 0; flag_chars
[i
] != 0; ++i
)
1801 if (index (fci
->flag_chars
, flag_chars
[i
]) == 0)
1802 warning ("flag `%c' used with type `%c'",
1803 flag_chars
[i
], format_char
);
1805 if (info
->format_type
== strftime_format_type
)
1807 if (precise
&& index (flag_chars
, '0') != 0
1808 && (format_char
== 'd' || format_char
== 'i'
1809 || format_char
== 'o' || format_char
== 'u'
1810 || format_char
== 'x' || format_char
== 'X'))
1811 warning ("`0' flag ignored with precision specifier and `%c' format",
1813 switch (length_char
)
1815 default: wanted_type
= fci
->nolen
? *(fci
->nolen
) : 0; break;
1816 case 'H': wanted_type
= fci
->hhlen
? *(fci
->hhlen
) : 0; break;
1817 case 'h': wanted_type
= fci
->hlen
? *(fci
->hlen
) : 0; break;
1818 case 'l': wanted_type
= fci
->llen
? *(fci
->llen
) : 0; break;
1819 case 'q': wanted_type
= fci
->qlen
? *(fci
->qlen
) : 0; break;
1820 case 'L': wanted_type
= fci
->bigllen
? *(fci
->bigllen
) : 0; break;
1821 case 'Z': wanted_type
= fci
->zlen
? *fci
->zlen
: 0; break;
1823 if (wanted_type
== 0)
1824 warning ("use of `%c' length character with `%c' type character",
1825 length_char
, format_char
);
1827 /* Finally. . .check type of argument against desired type! */
1828 if (info
->first_arg_num
== 0)
1830 if (fci
->pointer_count
== 0 && wanted_type
== void_type_node
)
1831 /* This specifier takes no argument. */
1838 cur_param
= TREE_VALUE (params
);
1839 params
= TREE_CHAIN (params
);
1841 cur_type
= TREE_TYPE (cur_param
);
1843 STRIP_NOPS (cur_param
);
1845 /* Check the types of any additional pointer arguments
1846 that precede the "real" argument. */
1847 for (i
= 0; i
< fci
->pointer_count
+ aflag
; ++i
)
1849 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
1851 cur_type
= TREE_TYPE (cur_type
);
1853 if (cur_param
!= 0 && TREE_CODE (cur_param
) == ADDR_EXPR
)
1854 cur_param
= TREE_OPERAND (cur_param
, 0);
1860 if (TREE_CODE (cur_type
) != ERROR_MARK
)
1861 warning ("format argument is not a %s (arg %d)",
1862 ((fci
->pointer_count
+ aflag
== 1)
1863 ? "pointer" : "pointer to a pointer"),
1868 /* See if this is an attempt to write into a const type with
1869 scanf or with printf "%n". */
1870 if ((info
->format_type
== scanf_format_type
1871 || (info
->format_type
== printf_format_type
1872 && format_char
== 'n'))
1873 && i
== fci
->pointer_count
+ aflag
1875 && TREE_CODE (cur_type
) != ERROR_MARK
1876 && (TYPE_READONLY (cur_type
)
1878 && (TREE_CODE_CLASS (TREE_CODE (cur_param
)) == 'c'
1879 || (TREE_CODE_CLASS (TREE_CODE (cur_param
)) == 'd'
1880 && TREE_READONLY (cur_param
))))))
1881 warning ("writing into constant object (arg %d)", arg_num
);
1883 /* Check the type of the "real" argument, if there's a type we want. */
1884 if (i
== fci
->pointer_count
+ aflag
&& wanted_type
!= 0
1885 && TREE_CODE (cur_type
) != ERROR_MARK
1886 && wanted_type
!= TYPE_MAIN_VARIANT (cur_type
)
1887 /* If we want `void *', allow any pointer type.
1888 (Anything else would already have got a warning.) */
1889 && ! (wanted_type
== void_type_node
1890 && fci
->pointer_count
> 0)
1891 /* Don't warn about differences merely in signedness. */
1892 && !(TREE_CODE (wanted_type
) == INTEGER_TYPE
1893 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type
)) == INTEGER_TYPE
1894 && (TREE_UNSIGNED (wanted_type
)
1895 ? wanted_type
== (cur_type
= unsigned_type (cur_type
))
1896 : wanted_type
== (cur_type
= signed_type (cur_type
))))
1897 /* Likewise, "signed char", "unsigned char" and "char" are
1898 equivalent but the above test won't consider them equivalent. */
1899 && ! (wanted_type
== char_type_node
1900 && (TYPE_MAIN_VARIANT (cur_type
) == signed_char_type_node
1901 || TYPE_MAIN_VARIANT (cur_type
) == unsigned_char_type_node
)))
1903 register char *this;
1904 register char *that
;
1906 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type
)));
1908 if (TREE_CODE (cur_type
) != ERROR_MARK
1909 && TYPE_NAME (cur_type
) != 0
1910 && TREE_CODE (cur_type
) != INTEGER_TYPE
1911 && !(TREE_CODE (cur_type
) == POINTER_TYPE
1912 && TREE_CODE (TREE_TYPE (cur_type
)) == INTEGER_TYPE
))
1914 if (TREE_CODE (TYPE_NAME (cur_type
)) == TYPE_DECL
1915 && DECL_NAME (TYPE_NAME (cur_type
)) != 0)
1916 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type
)));
1918 that
= IDENTIFIER_POINTER (TYPE_NAME (cur_type
));
1921 /* A nameless type can't possibly match what the format wants.
1922 So there will be a warning for it.
1923 Make up a string to describe vaguely what it is. */
1926 if (TREE_CODE (cur_type
) == POINTER_TYPE
)
1929 that
= "different type";
1932 /* Make the warning better in case of mismatch of int vs long. */
1933 if (TREE_CODE (cur_type
) == INTEGER_TYPE
1934 && TREE_CODE (wanted_type
) == INTEGER_TYPE
1935 && TYPE_PRECISION (cur_type
) == TYPE_PRECISION (wanted_type
)
1936 && TYPE_NAME (cur_type
) != 0
1937 && TREE_CODE (TYPE_NAME (cur_type
)) == TYPE_DECL
)
1938 that
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type
)));
1940 if (strcmp (this, that
) != 0)
1941 warning ("%s format, %s arg (arg %d)", this, that
, arg_num
);
1946 /* Print a warning if a constant expression had overflow in folding.
1947 Invoke this function on every expression that the language
1948 requires to be a constant expression.
1949 Note the ANSI C standard says it is erroneous for a
1950 constant expression to overflow. */
1953 constant_expression_warning (value
)
1956 if ((TREE_CODE (value
) == INTEGER_CST
|| TREE_CODE (value
) == REAL_CST
1957 || TREE_CODE (value
) == COMPLEX_CST
)
1958 && TREE_CONSTANT_OVERFLOW (value
) && pedantic
)
1959 pedwarn ("overflow in constant expression");
1962 /* Print a warning if an expression had overflow in folding.
1963 Invoke this function on every expression that
1964 (1) appears in the source code, and
1965 (2) might be a constant expression that overflowed, and
1966 (3) is not already checked by convert_and_check;
1967 however, do not invoke this function on operands of explicit casts. */
1970 overflow_warning (value
)
1973 if ((TREE_CODE (value
) == INTEGER_CST
1974 || (TREE_CODE (value
) == COMPLEX_CST
1975 && TREE_CODE (TREE_REALPART (value
)) == INTEGER_CST
))
1976 && TREE_OVERFLOW (value
))
1978 TREE_OVERFLOW (value
) = 0;
1979 if (skip_evaluation
== 0)
1980 warning ("integer overflow in expression");
1982 else if ((TREE_CODE (value
) == REAL_CST
1983 || (TREE_CODE (value
) == COMPLEX_CST
1984 && TREE_CODE (TREE_REALPART (value
)) == REAL_CST
))
1985 && TREE_OVERFLOW (value
))
1987 TREE_OVERFLOW (value
) = 0;
1988 if (skip_evaluation
== 0)
1989 warning ("floating point overflow in expression");
1993 /* Print a warning if a large constant is truncated to unsigned,
1994 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1995 Invoke this function on every expression that might be implicitly
1996 converted to an unsigned type. */
1999 unsigned_conversion_warning (result
, operand
)
2000 tree result
, operand
;
2002 if (TREE_CODE (operand
) == INTEGER_CST
2003 && TREE_CODE (TREE_TYPE (result
)) == INTEGER_TYPE
2004 && TREE_UNSIGNED (TREE_TYPE (result
))
2005 && skip_evaluation
== 0
2006 && !int_fits_type_p (operand
, TREE_TYPE (result
)))
2008 if (!int_fits_type_p (operand
, signed_type (TREE_TYPE (result
))))
2009 /* This detects cases like converting -129 or 256 to unsigned char. */
2010 warning ("large integer implicitly truncated to unsigned type");
2011 else if (warn_conversion
)
2012 warning ("negative integer implicitly converted to unsigned type");
2016 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2017 Invoke this function on every expression that is converted implicitly,
2018 i.e. because of language rules and not because of an explicit cast. */
2021 convert_and_check (type
, expr
)
2024 tree t
= convert (type
, expr
);
2025 if (TREE_CODE (t
) == INTEGER_CST
)
2027 if (TREE_OVERFLOW (t
))
2029 TREE_OVERFLOW (t
) = 0;
2031 /* Do not diagnose overflow in a constant expression merely
2032 because a conversion overflowed. */
2033 TREE_CONSTANT_OVERFLOW (t
) = TREE_CONSTANT_OVERFLOW (expr
);
2035 /* No warning for converting 0x80000000 to int. */
2036 if (!(TREE_UNSIGNED (type
) < TREE_UNSIGNED (TREE_TYPE (expr
))
2037 && TREE_CODE (TREE_TYPE (expr
)) == INTEGER_TYPE
2038 && TYPE_PRECISION (type
) == TYPE_PRECISION (TREE_TYPE (expr
))))
2039 /* If EXPR fits in the unsigned version of TYPE,
2040 don't warn unless pedantic. */
2042 || TREE_UNSIGNED (type
)
2043 || ! int_fits_type_p (expr
, unsigned_type (type
)))
2044 && skip_evaluation
== 0)
2045 warning ("overflow in implicit constant conversion");
2048 unsigned_conversion_warning (t
, expr
);
2054 c_expand_expr_stmt (expr
)
2057 /* Do default conversion if safe and possibly important,
2058 in case within ({...}). */
2059 if ((TREE_CODE (TREE_TYPE (expr
)) == ARRAY_TYPE
&& lvalue_p (expr
))
2060 || TREE_CODE (TREE_TYPE (expr
)) == FUNCTION_TYPE
)
2061 expr
= default_conversion (expr
);
2063 if (TREE_TYPE (expr
) != error_mark_node
2064 && TYPE_SIZE (TREE_TYPE (expr
)) == 0
2065 && TREE_CODE (TREE_TYPE (expr
)) != ARRAY_TYPE
)
2066 error ("expression statement has incomplete type");
2068 expand_expr_stmt (expr
);
2071 /* Validate the expression after `case' and apply default promotions. */
2074 check_case_value (value
)
2077 if (value
== NULL_TREE
)
2080 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2081 STRIP_TYPE_NOPS (value
);
2083 if (TREE_CODE (value
) != INTEGER_CST
2084 && value
!= error_mark_node
)
2086 error ("case label does not reduce to an integer constant");
2087 value
= error_mark_node
;
2090 /* Promote char or short to int. */
2091 value
= default_conversion (value
);
2093 constant_expression_warning (value
);
2098 /* Return an integer type with BITS bits of precision,
2099 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2102 type_for_size (bits
, unsignedp
)
2106 if (bits
== TYPE_PRECISION (integer_type_node
))
2107 return unsignedp
? unsigned_type_node
: integer_type_node
;
2109 if (bits
== TYPE_PRECISION (signed_char_type_node
))
2110 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
2112 if (bits
== TYPE_PRECISION (short_integer_type_node
))
2113 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
2115 if (bits
== TYPE_PRECISION (long_integer_type_node
))
2116 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
2118 if (bits
== TYPE_PRECISION (long_long_integer_type_node
))
2119 return (unsignedp
? long_long_unsigned_type_node
2120 : long_long_integer_type_node
);
2122 if (bits
<= TYPE_PRECISION (intQI_type_node
))
2123 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
2125 if (bits
<= TYPE_PRECISION (intHI_type_node
))
2126 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
2128 if (bits
<= TYPE_PRECISION (intSI_type_node
))
2129 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
2131 if (bits
<= TYPE_PRECISION (intDI_type_node
))
2132 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
2137 /* Return a data type that has machine mode MODE.
2138 If the mode is an integer,
2139 then UNSIGNEDP selects between signed and unsigned types. */
2142 type_for_mode (mode
, unsignedp
)
2143 enum machine_mode mode
;
2146 if (mode
== TYPE_MODE (integer_type_node
))
2147 return unsignedp
? unsigned_type_node
: integer_type_node
;
2149 if (mode
== TYPE_MODE (signed_char_type_node
))
2150 return unsignedp
? unsigned_char_type_node
: signed_char_type_node
;
2152 if (mode
== TYPE_MODE (short_integer_type_node
))
2153 return unsignedp
? short_unsigned_type_node
: short_integer_type_node
;
2155 if (mode
== TYPE_MODE (long_integer_type_node
))
2156 return unsignedp
? long_unsigned_type_node
: long_integer_type_node
;
2158 if (mode
== TYPE_MODE (long_long_integer_type_node
))
2159 return unsignedp
? long_long_unsigned_type_node
: long_long_integer_type_node
;
2161 if (mode
== TYPE_MODE (intQI_type_node
))
2162 return unsignedp
? unsigned_intQI_type_node
: intQI_type_node
;
2164 if (mode
== TYPE_MODE (intHI_type_node
))
2165 return unsignedp
? unsigned_intHI_type_node
: intHI_type_node
;
2167 if (mode
== TYPE_MODE (intSI_type_node
))
2168 return unsignedp
? unsigned_intSI_type_node
: intSI_type_node
;
2170 if (mode
== TYPE_MODE (intDI_type_node
))
2171 return unsignedp
? unsigned_intDI_type_node
: intDI_type_node
;
2173 #if HOST_BITS_PER_WIDE_INT >= 64
2174 if (mode
== TYPE_MODE (intTI_type_node
))
2175 return unsignedp
? unsigned_intTI_type_node
: intTI_type_node
;
2178 if (mode
== TYPE_MODE (float_type_node
))
2179 return float_type_node
;
2181 if (mode
== TYPE_MODE (double_type_node
))
2182 return double_type_node
;
2184 if (mode
== TYPE_MODE (long_double_type_node
))
2185 return long_double_type_node
;
2187 if (mode
== TYPE_MODE (build_pointer_type (char_type_node
)))
2188 return build_pointer_type (char_type_node
);
2190 if (mode
== TYPE_MODE (build_pointer_type (integer_type_node
)))
2191 return build_pointer_type (integer_type_node
);
2196 /* Return the minimum number of bits needed to represent VALUE in a
2197 signed or unsigned type, UNSIGNEDP says which. */
2200 min_precision (value
, unsignedp
)
2206 /* If the value is negative, compute its negative minus 1. The latter
2207 adjustment is because the absolute value of the largest negative value
2208 is one larger than the largest positive value. This is equivalent to
2209 a bit-wise negation, so use that operation instead. */
2211 if (tree_int_cst_sgn (value
) < 0)
2212 value
= fold (build1 (BIT_NOT_EXPR
, TREE_TYPE (value
), value
));
2214 /* Return the number of bits needed, taking into account the fact
2215 that we need one more bit for a signed than unsigned type. */
2217 if (integer_zerop (value
))
2219 else if (TREE_INT_CST_HIGH (value
) != 0)
2220 log
= HOST_BITS_PER_WIDE_INT
+ floor_log2 (TREE_INT_CST_HIGH (value
));
2222 log
= floor_log2 (TREE_INT_CST_LOW (value
));
2224 return log
+ 1 + ! unsignedp
;
2227 /* Print an error message for invalid operands to arith operation CODE.
2228 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2231 binary_op_error (code
)
2232 enum tree_code code
;
2234 register char *opname
;
2239 error ("invalid truth-value expression");
2243 opname
= "+"; break;
2245 opname
= "-"; break;
2247 opname
= "*"; break;
2249 opname
= "max"; break;
2251 opname
= "min"; break;
2253 opname
= "=="; break;
2255 opname
= "!="; break;
2257 opname
= "<="; break;
2259 opname
= ">="; break;
2261 opname
= "<"; break;
2263 opname
= ">"; break;
2265 opname
= "<<"; break;
2267 opname
= ">>"; break;
2268 case TRUNC_MOD_EXPR
:
2269 case FLOOR_MOD_EXPR
:
2270 opname
= "%"; break;
2271 case TRUNC_DIV_EXPR
:
2272 case FLOOR_DIV_EXPR
:
2273 opname
= "/"; break;
2275 opname
= "&"; break;
2277 opname
= "|"; break;
2278 case TRUTH_ANDIF_EXPR
:
2279 opname
= "&&"; break;
2280 case TRUTH_ORIF_EXPR
:
2281 opname
= "||"; break;
2283 opname
= "^"; break;
2286 opname
= "rotate"; break;
2288 opname
= "unknown"; break;
2290 error ("invalid operands to binary %s", opname
);
2293 /* Subroutine of build_binary_op, used for comparison operations.
2294 See if the operands have both been converted from subword integer types
2295 and, if so, perhaps change them both back to their original type.
2296 This function is also responsible for converting the two operands
2297 to the proper common type for comparison.
2299 The arguments of this function are all pointers to local variables
2300 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2301 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2303 If this function returns nonzero, it means that the comparison has
2304 a constant value. What this function returns is an expression for
2308 shorten_compare (op0_ptr
, op1_ptr
, restype_ptr
, rescode_ptr
)
2309 tree
*op0_ptr
, *op1_ptr
;
2311 enum tree_code
*rescode_ptr
;
2314 tree op0
= *op0_ptr
;
2315 tree op1
= *op1_ptr
;
2316 int unsignedp0
, unsignedp1
;
2318 tree primop0
, primop1
;
2319 enum tree_code code
= *rescode_ptr
;
2321 /* Throw away any conversions to wider types
2322 already present in the operands. */
2324 primop0
= get_narrower (op0
, &unsignedp0
);
2325 primop1
= get_narrower (op1
, &unsignedp1
);
2327 /* Handle the case that OP0 does not *contain* a conversion
2328 but it *requires* conversion to FINAL_TYPE. */
2330 if (op0
== primop0
&& TREE_TYPE (op0
) != *restype_ptr
)
2331 unsignedp0
= TREE_UNSIGNED (TREE_TYPE (op0
));
2332 if (op1
== primop1
&& TREE_TYPE (op1
) != *restype_ptr
)
2333 unsignedp1
= TREE_UNSIGNED (TREE_TYPE (op1
));
2335 /* If one of the operands must be floated, we cannot optimize. */
2336 real1
= TREE_CODE (TREE_TYPE (primop0
)) == REAL_TYPE
;
2337 real2
= TREE_CODE (TREE_TYPE (primop1
)) == REAL_TYPE
;
2339 /* If first arg is constant, swap the args (changing operation
2340 so value is preserved), for canonicalization. Don't do this if
2341 the second arg is 0. */
2343 if (TREE_CONSTANT (primop0
)
2344 && ! integer_zerop (primop1
) && ! real_zerop (primop1
))
2346 register tree tem
= primop0
;
2347 register int temi
= unsignedp0
;
2355 unsignedp0
= unsignedp1
;
2378 *rescode_ptr
= code
;
2381 /* If comparing an integer against a constant more bits wide,
2382 maybe we can deduce a value of 1 or 0 independent of the data.
2383 Or else truncate the constant now
2384 rather than extend the variable at run time.
2386 This is only interesting if the constant is the wider arg.
2387 Also, it is not safe if the constant is unsigned and the
2388 variable arg is signed, since in this case the variable
2389 would be sign-extended and then regarded as unsigned.
2390 Our technique fails in this case because the lowest/highest
2391 possible unsigned results don't follow naturally from the
2392 lowest/highest possible values of the variable operand.
2393 For just EQ_EXPR and NE_EXPR there is another technique that
2394 could be used: see if the constant can be faithfully represented
2395 in the other operand's type, by truncating it and reextending it
2396 and see if that preserves the constant's value. */
2398 if (!real1
&& !real2
2399 && TREE_CODE (primop1
) == INTEGER_CST
2400 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
))
2402 int min_gt
, max_gt
, min_lt
, max_lt
;
2403 tree maxval
, minval
;
2404 /* 1 if comparison is nominally unsigned. */
2405 int unsignedp
= TREE_UNSIGNED (*restype_ptr
);
2408 type
= signed_or_unsigned_type (unsignedp0
, TREE_TYPE (primop0
));
2410 maxval
= TYPE_MAX_VALUE (type
);
2411 minval
= TYPE_MIN_VALUE (type
);
2413 if (unsignedp
&& !unsignedp0
)
2414 *restype_ptr
= signed_type (*restype_ptr
);
2416 if (TREE_TYPE (primop1
) != *restype_ptr
)
2417 primop1
= convert (*restype_ptr
, primop1
);
2418 if (type
!= *restype_ptr
)
2420 minval
= convert (*restype_ptr
, minval
);
2421 maxval
= convert (*restype_ptr
, maxval
);
2424 if (unsignedp
&& unsignedp0
)
2426 min_gt
= INT_CST_LT_UNSIGNED (primop1
, minval
);
2427 max_gt
= INT_CST_LT_UNSIGNED (primop1
, maxval
);
2428 min_lt
= INT_CST_LT_UNSIGNED (minval
, primop1
);
2429 max_lt
= INT_CST_LT_UNSIGNED (maxval
, primop1
);
2433 min_gt
= INT_CST_LT (primop1
, minval
);
2434 max_gt
= INT_CST_LT (primop1
, maxval
);
2435 min_lt
= INT_CST_LT (minval
, primop1
);
2436 max_lt
= INT_CST_LT (maxval
, primop1
);
2440 /* This used to be a switch, but Genix compiler can't handle that. */
2441 if (code
== NE_EXPR
)
2443 if (max_lt
|| min_gt
)
2444 val
= boolean_true_node
;
2446 else if (code
== EQ_EXPR
)
2448 if (max_lt
|| min_gt
)
2449 val
= boolean_false_node
;
2451 else if (code
== LT_EXPR
)
2454 val
= boolean_true_node
;
2456 val
= boolean_false_node
;
2458 else if (code
== GT_EXPR
)
2461 val
= boolean_true_node
;
2463 val
= boolean_false_node
;
2465 else if (code
== LE_EXPR
)
2468 val
= boolean_true_node
;
2470 val
= boolean_false_node
;
2472 else if (code
== GE_EXPR
)
2475 val
= boolean_true_node
;
2477 val
= boolean_false_node
;
2480 /* If primop0 was sign-extended and unsigned comparison specd,
2481 we did a signed comparison above using the signed type bounds.
2482 But the comparison we output must be unsigned.
2484 Also, for inequalities, VAL is no good; but if the signed
2485 comparison had *any* fixed result, it follows that the
2486 unsigned comparison just tests the sign in reverse
2487 (positive values are LE, negative ones GE).
2488 So we can generate an unsigned comparison
2489 against an extreme value of the signed type. */
2491 if (unsignedp
&& !unsignedp0
)
2498 primop1
= TYPE_MIN_VALUE (type
);
2504 primop1
= TYPE_MAX_VALUE (type
);
2511 type
= unsigned_type (type
);
2514 if (!max_gt
&& !unsignedp0
&& TREE_CODE (primop0
) != INTEGER_CST
)
2516 /* This is the case of (char)x >?< 0x80, which people used to use
2517 expecting old C compilers to change the 0x80 into -0x80. */
2518 if (val
== boolean_false_node
)
2519 warning ("comparison is always false due to limited range of data type");
2520 if (val
== boolean_true_node
)
2521 warning ("comparison is always true due to limited range of data type");
2524 if (!min_lt
&& unsignedp0
&& TREE_CODE (primop0
) != INTEGER_CST
)
2526 /* This is the case of (unsigned char)x >?< -1 or < 0. */
2527 if (val
== boolean_false_node
)
2528 warning ("comparison is always false due to limited range of data type");
2529 if (val
== boolean_true_node
)
2530 warning ("comparison is always true due to limited range of data type");
2535 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2536 if (TREE_SIDE_EFFECTS (primop0
))
2537 return build (COMPOUND_EXPR
, TREE_TYPE (val
), primop0
, val
);
2541 /* Value is not predetermined, but do the comparison
2542 in the type of the operand that is not constant.
2543 TYPE is already properly set. */
2545 else if (real1
&& real2
2546 && (TYPE_PRECISION (TREE_TYPE (primop0
))
2547 == TYPE_PRECISION (TREE_TYPE (primop1
))))
2548 type
= TREE_TYPE (primop0
);
2550 /* If args' natural types are both narrower than nominal type
2551 and both extend in the same manner, compare them
2552 in the type of the wider arg.
2553 Otherwise must actually extend both to the nominal
2554 common type lest different ways of extending
2556 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2558 else if (unsignedp0
== unsignedp1
&& real1
== real2
2559 && TYPE_PRECISION (TREE_TYPE (primop0
)) < TYPE_PRECISION (*restype_ptr
)
2560 && TYPE_PRECISION (TREE_TYPE (primop1
)) < TYPE_PRECISION (*restype_ptr
))
2562 type
= common_type (TREE_TYPE (primop0
), TREE_TYPE (primop1
));
2563 type
= signed_or_unsigned_type (unsignedp0
2564 || TREE_UNSIGNED (*restype_ptr
),
2566 /* Make sure shorter operand is extended the right way
2567 to match the longer operand. */
2568 primop0
= convert (signed_or_unsigned_type (unsignedp0
, TREE_TYPE (primop0
)),
2570 primop1
= convert (signed_or_unsigned_type (unsignedp1
, TREE_TYPE (primop1
)),
2575 /* Here we must do the comparison on the nominal type
2576 using the args exactly as we received them. */
2577 type
= *restype_ptr
;
2581 if (!real1
&& !real2
&& integer_zerop (primop1
)
2582 && TREE_UNSIGNED (*restype_ptr
))
2588 /* All unsigned values are >= 0, so we warn if extra warnings
2589 are requested. However, if OP0 is a constant that is
2590 >= 0, the signedness of the comparison isn't an issue,
2591 so suppress the warning. */
2593 && ! (TREE_CODE (primop0
) == INTEGER_CST
2594 && ! TREE_OVERFLOW (convert (signed_type (type
),
2596 warning ("comparison of unsigned expression >= 0 is always true");
2597 value
= boolean_true_node
;
2602 && ! (TREE_CODE (primop0
) == INTEGER_CST
2603 && ! TREE_OVERFLOW (convert (signed_type (type
),
2605 warning ("comparison of unsigned expression < 0 is always false");
2606 value
= boolean_false_node
;
2615 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2616 if (TREE_SIDE_EFFECTS (primop0
))
2617 return build (COMPOUND_EXPR
, TREE_TYPE (value
),
2624 *op0_ptr
= convert (type
, primop0
);
2625 *op1_ptr
= convert (type
, primop1
);
2627 *restype_ptr
= boolean_type_node
;
2632 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2633 or validate its data type for an `if' or `while' statement or ?..: exp.
2635 This preparation consists of taking the ordinary
2636 representation of an expression expr and producing a valid tree
2637 boolean expression describing whether expr is nonzero. We could
2638 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2639 but we optimize comparisons, &&, ||, and !.
2641 The resulting type should always be `boolean_type_node'. */
2644 truthvalue_conversion (expr
)
2647 if (TREE_CODE (expr
) == ERROR_MARK
)
2650 #if 0 /* This appears to be wrong for C++. */
2651 /* These really should return error_mark_node after 2.4 is stable.
2652 But not all callers handle ERROR_MARK properly. */
2653 switch (TREE_CODE (TREE_TYPE (expr
)))
2656 error ("struct type value used where scalar is required");
2657 return boolean_false_node
;
2660 error ("union type value used where scalar is required");
2661 return boolean_false_node
;
2664 error ("array type value used where scalar is required");
2665 return boolean_false_node
;
2672 switch (TREE_CODE (expr
))
2674 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2675 or comparison expressions as truth values at this level. */
2678 /* A one-bit unsigned bit-field is already acceptable. */
2679 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr
, 1)))
2680 && TREE_UNSIGNED (TREE_OPERAND (expr
, 1)))
2686 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2687 or comparison expressions as truth values at this level. */
2689 if (integer_zerop (TREE_OPERAND (expr
, 1)))
2690 return build_unary_op (TRUTH_NOT_EXPR
, TREE_OPERAND (expr
, 0), 0);
2692 case NE_EXPR
: case LE_EXPR
: case GE_EXPR
: case LT_EXPR
: case GT_EXPR
:
2693 case TRUTH_ANDIF_EXPR
:
2694 case TRUTH_ORIF_EXPR
:
2695 case TRUTH_AND_EXPR
:
2697 case TRUTH_XOR_EXPR
:
2698 case TRUTH_NOT_EXPR
:
2699 TREE_TYPE (expr
) = boolean_type_node
;
2706 return integer_zerop (expr
) ? boolean_false_node
: boolean_true_node
;
2709 return real_zerop (expr
) ? boolean_false_node
: boolean_true_node
;
2712 /* If we are taking the address of a external decl, it might be zero
2713 if it is weak, so we cannot optimize. */
2714 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr
, 0))) == 'd'
2715 && DECL_EXTERNAL (TREE_OPERAND (expr
, 0)))
2718 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 0)))
2719 return build (COMPOUND_EXPR
, boolean_type_node
,
2720 TREE_OPERAND (expr
, 0), boolean_true_node
);
2722 return boolean_true_node
;
2725 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1))
2726 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
2727 truthvalue_conversion (TREE_OPERAND (expr
, 0)),
2728 truthvalue_conversion (TREE_OPERAND (expr
, 1)),
2735 /* These don't change whether an object is non-zero or zero. */
2736 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
2740 /* These don't change whether an object is zero or non-zero, but
2741 we can't ignore them if their second arg has side-effects. */
2742 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1)))
2743 return build (COMPOUND_EXPR
, boolean_type_node
, TREE_OPERAND (expr
, 1),
2744 truthvalue_conversion (TREE_OPERAND (expr
, 0)));
2746 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
2749 /* Distribute the conversion into the arms of a COND_EXPR. */
2750 return fold (build (COND_EXPR
, boolean_type_node
, TREE_OPERAND (expr
, 0),
2751 truthvalue_conversion (TREE_OPERAND (expr
, 1)),
2752 truthvalue_conversion (TREE_OPERAND (expr
, 2))));
2755 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2756 since that affects how `default_conversion' will behave. */
2757 if (TREE_CODE (TREE_TYPE (expr
)) == REFERENCE_TYPE
2758 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr
, 0))) == REFERENCE_TYPE
)
2760 /* fall through... */
2762 /* If this is widening the argument, we can ignore it. */
2763 if (TYPE_PRECISION (TREE_TYPE (expr
))
2764 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr
, 0))))
2765 return truthvalue_conversion (TREE_OPERAND (expr
, 0));
2769 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2771 if (TARGET_FLOAT_FORMAT
== IEEE_FLOAT_FORMAT
2772 && TREE_CODE (TREE_TYPE (expr
)) == REAL_TYPE
)
2774 /* fall through... */
2776 /* This and MINUS_EXPR can be changed into a comparison of the
2778 if (TREE_TYPE (TREE_OPERAND (expr
, 0))
2779 == TREE_TYPE (TREE_OPERAND (expr
, 1)))
2780 return build_binary_op (NE_EXPR
, TREE_OPERAND (expr
, 0),
2781 TREE_OPERAND (expr
, 1), 1);
2782 return build_binary_op (NE_EXPR
, TREE_OPERAND (expr
, 0),
2783 fold (build1 (NOP_EXPR
,
2784 TREE_TYPE (TREE_OPERAND (expr
, 0)),
2785 TREE_OPERAND (expr
, 1))), 1);
2788 if (integer_onep (TREE_OPERAND (expr
, 1))
2789 && TREE_TYPE (expr
) != boolean_type_node
)
2790 /* Using convert here would cause infinite recursion. */
2791 return build1 (NOP_EXPR
, boolean_type_node
, expr
);
2795 if (warn_parentheses
&& C_EXP_ORIGINAL_CODE (expr
) == MODIFY_EXPR
)
2796 warning ("suggest parentheses around assignment used as truth value");
2803 if (TREE_CODE (TREE_TYPE (expr
)) == COMPLEX_TYPE
)
2805 tree tem
= save_expr (expr
);
2806 return (build_binary_op
2807 ((TREE_SIDE_EFFECTS (expr
)
2808 ? TRUTH_OR_EXPR
: TRUTH_ORIF_EXPR
),
2809 truthvalue_conversion (build_unary_op (REALPART_EXPR
, tem
, 0)),
2810 truthvalue_conversion (build_unary_op (IMAGPART_EXPR
, tem
, 0)),
2814 return build_binary_op (NE_EXPR
, expr
, integer_zero_node
, 1);
2818 /* Read the rest of a #-directive from input stream FINPUT.
2819 In normal use, the directive name and the white space after it
2820 have already been read, so they won't be included in the result.
2821 We allow for the fact that the directive line may contain
2822 a newline embedded within a character or string literal which forms
2823 a part of the directive.
2825 The value is a string in a reusable buffer. It remains valid
2826 only until the next time this function is called. */
2827 unsigned char *yy_cur
, *yy_lim
;
2829 #define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
2830 #define UNGETC(c) ((c), yy_cur--)
2837 parse_in
.limit
= parse_in
.token_buffer
;
2838 cpp_token
= cpp_get_token (&parse_in
);
2839 if (cpp_token
== CPP_EOF
)
2841 yy_lim
= CPP_PWRITTEN (&parse_in
);
2842 yy_cur
= parse_in
.token_buffer
;
2843 if (yy_cur
< yy_lim
)
2849 get_directive_line ()
2851 static char *directive_buffer
= NULL
;
2852 static unsigned buffer_length
= 0;
2854 register char *buffer_limit
;
2855 register int looking_for
= 0;
2856 register int char_escaped
= 0;
2858 if (buffer_length
== 0)
2860 directive_buffer
= (char *)xmalloc (128);
2861 buffer_length
= 128;
2864 buffer_limit
= &directive_buffer
[buffer_length
];
2866 for (p
= directive_buffer
; ; )
2870 /* Make buffer bigger if it is full. */
2871 if (p
>= buffer_limit
)
2873 register unsigned bytes_used
= (p
- directive_buffer
);
2877 = (char *)xrealloc (directive_buffer
, buffer_length
);
2878 p
= &directive_buffer
[bytes_used
];
2879 buffer_limit
= &directive_buffer
[buffer_length
];
2884 /* Discard initial whitespace. */
2885 if ((c
== ' ' || c
== '\t') && p
== directive_buffer
)
2888 /* Detect the end of the directive. */
2889 if (c
== '\n' && looking_for
== 0)
2898 return directive_buffer
;
2900 /* Handle string and character constant syntax. */
2903 if (looking_for
== c
&& !char_escaped
)
2904 looking_for
= 0; /* Found terminator... stop looking. */
2907 if (c
== '\'' || c
== '"')
2908 looking_for
= c
; /* Don't stop buffering until we see another
2909 another one of these (or an EOF). */
2911 /* Handle backslash. */
2912 char_escaped
= (c
== '\\' && ! char_escaped
);
2916 /* Read the rest of a #-directive from input stream FINPUT.
2917 In normal use, the directive name and the white space after it
2918 have already been read, so they won't be included in the result.
2919 We allow for the fact that the directive line may contain
2920 a newline embedded within a character or string literal which forms
2921 a part of the directive.
2923 The value is a string in a reusable buffer. It remains valid
2924 only until the next time this function is called.
2926 The terminating character ('\n' or EOF) is left in FINPUT for the
2927 caller to re-read. */
2930 get_directive_line (finput
)
2931 register FILE *finput
;
2933 static char *directive_buffer
= NULL
;
2934 static unsigned buffer_length
= 0;
2936 register char *buffer_limit
;
2937 register int looking_for
= 0;
2938 register int char_escaped
= 0;
2940 if (buffer_length
== 0)
2942 directive_buffer
= (char *)xmalloc (128);
2943 buffer_length
= 128;
2946 buffer_limit
= &directive_buffer
[buffer_length
];
2948 for (p
= directive_buffer
; ; )
2952 /* Make buffer bigger if it is full. */
2953 if (p
>= buffer_limit
)
2955 register unsigned bytes_used
= (p
- directive_buffer
);
2959 = (char *)xrealloc (directive_buffer
, buffer_length
);
2960 p
= &directive_buffer
[bytes_used
];
2961 buffer_limit
= &directive_buffer
[buffer_length
];
2966 /* Discard initial whitespace. */
2967 if ((c
== ' ' || c
== '\t') && p
== directive_buffer
)
2970 /* Detect the end of the directive. */
2971 if (looking_for
== 0
2972 && (c
== '\n' || c
== EOF
))
2981 return directive_buffer
;
2983 /* Handle string and character constant syntax. */
2986 if (looking_for
== c
&& !char_escaped
)
2987 looking_for
= 0; /* Found terminator... stop looking. */
2990 if (c
== '\'' || c
== '"')
2991 looking_for
= c
; /* Don't stop buffering until we see another
2992 one of these (or an EOF). */
2994 /* Handle backslash. */
2995 char_escaped
= (c
== '\\' && ! char_escaped
);
2998 #endif /* !USE_CPPLIB */
3000 /* Make a variant type in the proper way for C/C++, propagating qualifiers
3001 down to the element type of an array. */
3004 c_build_qualified_type (type
, type_quals
)
3008 /* A restrict-qualified pointer type must be a pointer to object or
3009 incomplete type. Note that the use of POINTER_TYPE_P also allows
3010 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
3011 the C++ front-end also use POINTER_TYPE for pointer-to-member
3012 values, so even though it should be illegal to use `restrict'
3013 with such an entity we don't flag that here. Thus, special case
3014 code for that case is required in the C++ front-end. */
3015 if ((type_quals
& TYPE_QUAL_RESTRICT
)
3016 && (!POINTER_TYPE_P (type
)
3017 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type
))))
3019 error ("invalid use of `restrict'");
3020 type_quals
&= ~TYPE_QUAL_RESTRICT
;
3023 if (TREE_CODE (type
) == ARRAY_TYPE
)
3024 return build_array_type (c_build_qualified_type (TREE_TYPE (type
),
3026 TYPE_DOMAIN (type
));
3027 return build_qualified_type (type
, type_quals
);
3030 /* Apply the TYPE_QUALS to the new DECL. */
3033 c_apply_type_quals_to_decl (type_quals
, decl
)
3037 if (type_quals
& TYPE_QUAL_CONST
)
3038 TREE_READONLY (decl
) = 1;
3039 if (type_quals
& TYPE_QUAL_VOLATILE
)
3041 TREE_SIDE_EFFECTS (decl
) = 1;
3042 TREE_THIS_VOLATILE (decl
) = 1;
3044 if (type_quals
& TYPE_QUAL_RESTRICT
)
3046 if (!TREE_TYPE (decl
)
3047 || !POINTER_TYPE_P (TREE_TYPE (decl
))
3048 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl
))))
3049 error ("invalid use of `restrict'");
3050 else if (flag_strict_aliasing
)
3052 /* No two restricted pointers can point at the same thing.
3053 However, a restricted pointer can point at the same thing
3054 as an unrestricted pointer, if that unrestricted pointer
3055 is based on the restricted pointer. So, we make the
3056 alias set for the restricted pointer a subset of the
3057 alias set for the type pointed to by the type of the
3060 int pointed_to_alias_set
3061 = get_alias_set (TREE_TYPE (TREE_TYPE (decl
)));
3063 if (!pointed_to_alias_set
)
3064 /* It's not legal to make a subset of alias set zero. */
3068 DECL_POINTER_ALIAS_SET (decl
) = new_alias_set ();
3069 record_alias_subset (pointed_to_alias_set
,
3070 DECL_POINTER_ALIAS_SET (decl
));
3076 /* T is an expression with pointer type. Find the DECL on which this
3077 expression is based. (For example, in `a[i]' this would be `a'.)
3078 If there is no such DECL, or a unique decl cannot be determined,
3079 NULL_TREE is retured. */
3082 c_find_base_decl (t
)
3088 if (t
== NULL_TREE
|| t
== error_mark_node
)
3091 if (!POINTER_TYPE_P (TREE_TYPE (t
)))
3096 if (TREE_CODE (t
) == FIELD_DECL
3097 || TREE_CODE (t
) == PARM_DECL
3098 || TREE_CODE (t
) == VAR_DECL
)
3099 /* Aha, we found a pointer-typed declaration. */
3102 /* It would be nice to deal with COMPONENT_REFs here. If we could
3103 tell that `a' and `b' were the same, then `a->f' and `b->f' are
3106 /* Handle general expressions. */
3107 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
3112 for (i
= tree_code_length
[(int) TREE_CODE (t
)]; --i
>= 0;)
3114 tree d
= c_find_base_decl (TREE_OPERAND (t
, i
));
3119 else if (d
&& d
!= decl
)
3120 /* Two different declarations. That's confusing; let's
3121 just assume we don't know what's going on. */
3134 /* Return the typed-based alias set for T, which may be an expression
3144 if (t
== error_mark_node
)
3147 type
= (TREE_CODE_CLASS (TREE_CODE (t
)) == 't')
3148 ? t
: TREE_TYPE (t
);
3150 if (type
== error_mark_node
)
3153 /* Deal with special cases first; for certain kinds of references
3154 we're interested in more than just the type. */
3156 if (TREE_CODE (t
) == BIT_FIELD_REF
)
3157 /* Perhaps reads and writes to this piece of data alias fields
3158 neighboring the bitfield. Perhaps that's impossible. For now,
3159 let's just assume that bitfields can alias everything, which is
3160 the conservative assumption. */
3163 /* Permit type-punning when accessing a union, provided the access
3164 is directly through the union. For example, this code does not
3165 permit taking the address of a union member and then storing
3166 through it. Even the type-punning allowed here is a GCC
3167 extension, albeit a common and useful one; the C standard says
3168 that such accesses have implementation-defined behavior. */
3170 TREE_CODE (u
) == COMPONENT_REF
|| TREE_CODE (u
) == ARRAY_REF
;
3171 u
= TREE_OPERAND (u
, 0))
3172 if (TREE_CODE (u
) == COMPONENT_REF
3173 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u
, 0))) == UNION_TYPE
)
3176 if (TREE_CODE (t
) == INDIRECT_REF
)
3178 /* Check for accesses through restrict-qualified pointers. */
3179 tree decl
= c_find_base_decl (TREE_OPERAND (t
, 0));
3181 if (decl
&& DECL_POINTER_ALIAS_SET_KNOWN_P (decl
))
3182 /* We use the alias set indicated in the declaration. */
3183 return DECL_POINTER_ALIAS_SET (decl
);
3186 /* From here on, only the type matters. */
3188 if (TREE_CODE (t
) == COMPONENT_REF
3189 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (t
, 1)))
3190 /* Since build_modify_expr calls get_unwidened for stores to
3191 component references, the type of a bit field can be changed
3192 from (say) `unsigned int : 16' to `unsigned short' or from
3193 `enum E : 16' to `short'. We want the real type of the
3194 bit-field in this case, not some the integral equivalent. */
3195 type
= DECL_BIT_FIELD_TYPE (TREE_OPERAND (t
, 1));
3197 if (TYPE_ALIAS_SET_KNOWN_P (type
))
3198 /* If we've already calculated the value, just return it. */
3199 return TYPE_ALIAS_SET (type
);
3200 else if (TYPE_MAIN_VARIANT (type
) != type
)
3201 /* The C standard specifically allows aliasing between
3202 cv-qualified variants of types. */
3203 TYPE_ALIAS_SET (type
) = c_get_alias_set (TYPE_MAIN_VARIANT (type
));
3204 else if (TREE_CODE (type
) == INTEGER_TYPE
)
3206 tree signed_variant
;
3208 /* The C standard specifically allows aliasing between signed and
3209 unsigned variants of the same type. We treat the signed
3210 variant as canonical. */
3211 signed_variant
= signed_type (type
);
3213 if (signed_variant
!= type
)
3214 TYPE_ALIAS_SET (type
) = c_get_alias_set (signed_variant
);
3215 else if (signed_variant
== signed_char_type_node
)
3216 /* The C standard guarantess that any object may be accessed
3217 via an lvalue that has character type. We don't have to
3218 check for unsigned_char_type_node or char_type_node because
3219 we are specifically looking at the signed variant. */
3220 TYPE_ALIAS_SET (type
) = 0;
3222 else if (TREE_CODE (type
) == ARRAY_TYPE
)
3223 /* Anything that can alias one of the array elements can alias
3224 the entire array as well. */
3225 TYPE_ALIAS_SET (type
) = c_get_alias_set (TREE_TYPE (type
));
3226 else if (TREE_CODE (type
) == FUNCTION_TYPE
)
3227 /* There are no objects of FUNCTION_TYPE, so there's no point in
3228 using up an alias set for them. (There are, of course,
3229 pointers and references to functions, but that's
3231 TYPE_ALIAS_SET (type
) = 0;
3232 else if (TREE_CODE (type
) == RECORD_TYPE
3233 || TREE_CODE (type
) == UNION_TYPE
)
3234 /* If TYPE is a struct or union type then we're reading or
3235 writing an entire struct. Thus, we don't know anything about
3236 aliasing. (In theory, such an access can only alias objects
3237 whose type is the same as one of the fields, recursively, but
3238 we don't yet make any use of that information.) */
3239 TYPE_ALIAS_SET (type
) = 0;
3241 if (!TYPE_ALIAS_SET_KNOWN_P (type
))
3242 /* TYPE is something we haven't seen before. Put it in a new
3244 TYPE_ALIAS_SET (type
) = new_alias_set ();
3246 return TYPE_ALIAS_SET (type
);