* dwarfout.c (field_byte_offset): Correctly compute the object's
[official-gcc.git] / gcc / c-common.c
blob1ec3842010b4a7153015275c3e37e9a424aa9c4a
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)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "config.h"
22 #include "system.h"
23 #include "tree.h"
24 #include "c-lex.h"
25 #include "c-tree.h"
26 #include "flags.h"
27 #include "obstack.h"
28 #include "toplev.h"
29 #include "output.h"
30 #include "c-pragma.h"
31 #include "rtl.h"
33 #if USE_CPPLIB
34 #include "cpplib.h"
35 cpp_reader parse_in;
36 cpp_options parse_options;
37 static enum cpp_token cpp_token;
38 #endif
40 #ifndef WCHAR_TYPE_SIZE
41 #ifdef INT_TYPE_SIZE
42 #define WCHAR_TYPE_SIZE INT_TYPE_SIZE
43 #else
44 #define WCHAR_TYPE_SIZE BITS_PER_WORD
45 #endif
46 #endif
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. */
52 int skip_evaluation;
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 *,
64 int, int, int));
65 static void init_attributes PROTO((void));
66 static void record_function_format PROTO((tree, tree, enum format_type,
67 int, int));
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. */
77 typedef struct
79 int compstmt_count;
80 int line;
81 const char *file;
82 int needs_warning;
83 } if_elt;
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;
91 /* Stack pointer. */
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. */
97 void
98 c_expand_start_cond (cond, exitflag, compstmt_count)
99 tree cond;
100 int exitflag;
101 int compstmt_count;
103 /* Make sure there is enough space on the stack. */
104 if (if_stack_space == 0)
106 if_stack_space = 10;
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;
120 if_stack_pointer++;
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. */
128 void
129 c_expand_end_cond ()
131 if_stack_pointer--;
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'");
136 expand_end_cond ();
139 /* Generate RTL between the then-clause and the else-clause
140 of an if-then-else. */
142 void
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. */
147 if (warn_parentheses
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__. */
164 void
165 declare_function_name ()
167 const char *name, *printable_name;
169 if (current_function_decl == NULL)
171 name = "";
172 printable_name = "top level";
174 else
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));
179 else
180 name = "";
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);
191 static void
192 declare_hidden_char_array (name, value)
193 const char *name, *value;
195 tree decl, type, init;
196 int vlen;
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
203 || warn_larger_than)
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. */
225 tree
226 combine_strings (strings)
227 tree strings;
229 register tree value, t;
230 register int length = 1;
231 int wide_length = 0;
232 int wide_flag = 0;
233 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
234 int nchars;
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);
249 wide_flag = 1;
251 else
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. */
257 if (wide_flag)
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. */
266 q = p;
267 for (t = strings; t; t = TREE_CHAIN (t))
269 int len = (TREE_STRING_LENGTH (t)
270 - ((TREE_TYPE (t) == wchar_array_type_node)
271 ? wchar_bytes : 1));
272 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
274 memcpy (q, TREE_STRING_POINTER (t), len);
275 q += len;
277 else
279 int i;
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];
284 else
285 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
287 q += len * wchar_bytes;
290 if (wide_flag)
292 int i;
293 for (i = 0; i < wchar_bytes; i++)
294 *q++ = 0;
296 else
297 *q = 0;
299 value = make_node (STRING_CST);
300 TREE_STRING_POINTER (value) = p;
301 TREE_STRING_LENGTH (value) = length;
303 else
305 value = strings;
306 length = TREE_STRING_LENGTH (value);
307 if (TREE_TYPE (value) == wchar_array_type_node)
308 wide_flag = 1;
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))
321 tree elements
322 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
323 1, 0);
324 TREE_TYPE (value)
325 = build_array_type (elements,
326 build_index_type (build_int_2 (nchars - 1, 0)));
328 else
329 TREE_TYPE (value)
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;
335 return value;
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. */
349 static void
350 add_attribute (id, string, min_len, max_len, decl_req)
351 enum attrs id;
352 const char *string;
353 int min_len, max_len;
354 int decl_req;
356 char buf[100];
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. */
375 static void
376 init_attributes ()
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. */
402 static int
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;
409 return 0;
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. */
423 void
424 decl_attributes (node, attributes, prefix_attributes)
425 tree node, attributes, prefix_attributes;
427 tree decl = 0, type = 0;
428 int is_type = 0;
429 tree a;
431 if (attrtab_idx == 0)
432 init_attributes ();
434 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
436 decl = node;
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);
449 #endif
451 #ifdef INSERT_ATTRIBUTES
452 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
453 #endif
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);
461 int i;
462 enum attrs id;
464 for (i = 0; i < attrtab_idx; i++)
465 if (attrtab[i].name == name)
466 break;
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));
474 else if (decl != 0)
475 type = TREE_TYPE (decl);
476 continue;
478 else if (attrtab[i].decl_req && decl == 0)
480 warning ("`%s' attribute does not apply to types",
481 IDENTIFIER_POINTER (name));
482 continue;
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));
489 continue;
492 id = attrtab[i].id;
493 switch (id)
495 case A_PACKED:
496 if (is_type)
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. */
502 else
503 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
504 break;
506 case A_NOCOMMON:
507 if (TREE_CODE (decl) == VAR_DECL)
508 DECL_COMMON (decl) = 0;
509 else
510 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
511 break;
513 case A_COMMON:
514 if (TREE_CODE (decl) == VAR_DECL)
515 DECL_COMMON (decl) = 1;
516 else
517 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
518 break;
520 case A_NORETURN:
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
526 = build_pointer_type
527 (build_type_variant (TREE_TYPE (type),
528 TREE_READONLY (TREE_TYPE (type)), 1));
529 else
530 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
531 break;
533 case A_UNUSED:
534 if (is_type)
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;
541 else
542 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
543 break;
545 case A_CONST:
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
551 = build_pointer_type
552 (build_type_variant (TREE_TYPE (type), 1,
553 TREE_THIS_VOLATILE (TREE_TYPE (type))));
554 else
555 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
556 break;
558 case A_T_UNION:
559 if (is_type
560 && TREE_CODE (type) == UNION_TYPE
561 && (decl == 0
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;
569 else
570 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
571 break;
573 case A_CONSTRUCTOR:
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;
581 else
582 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
583 break;
585 case A_DESTRUCTOR:
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;
593 else
594 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
595 break;
597 case A_MODE:
598 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
599 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
600 else
602 int j;
603 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
604 int len = strlen (p);
605 enum machine_mode mode = VOIDmode;
606 tree typefm;
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';
615 p = newp;
618 /* Give this decl a type with the specified mode.
619 First check for the special modes. */
620 if (! strcmp (p, "byte"))
621 mode = byte_mode;
622 else if (!strcmp (p, "word"))
623 mode = word_mode;
624 else if (! strcmp (p, "pointer"))
625 mode = ptr_mode;
626 else
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);
636 else
638 TREE_TYPE (decl) = type = typefm;
639 DECL_SIZE (decl) = 0;
640 layout_decl (decl, 0);
643 break;
645 case A_SECTION:
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");
663 else
664 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
666 else
667 error_with_decl (node,
668 "section attribute not allowed for `%s'");
669 #else
670 error_with_decl (node,
671 "section attributes are not supported for this target");
672 #endif
673 break;
675 case A_ALIGNED:
677 tree align_expr
678 = (args ? TREE_VALUE (args)
679 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
680 int align;
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");
691 continue;
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");
698 else if (is_type)
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'");
704 else
705 DECL_ALIGN (decl) = align;
707 break;
709 case A_FORMAT:
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)));
715 int format_num;
716 int first_arg_num;
717 enum format_type format_type;
718 tree argument;
719 int arg_num;
721 if (TREE_CODE (decl) != FUNCTION_DECL)
723 error_with_decl (decl,
724 "argument format specified for non-function `%s'");
725 continue;
728 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
730 error ("unrecognized format specifier");
731 continue;
733 else
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;
744 else
746 warning ("`%s' is an unrecognized format function type", p);
747 continue;
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");
767 continue;
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");
775 continue;
778 /* If a parameter list is specified, verify that the format_num
779 argument is actually a string, in case the format attribute
780 is in error. */
781 argument = TYPE_ARG_TYPES (type);
782 if (argument)
784 for (arg_num = 1; ; ++arg_num)
786 if (argument == 0 || arg_num == format_num)
787 break;
788 argument = TREE_CHAIN (argument);
790 if (! argument
791 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
792 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
793 != char_type_node))
795 error ("format string arg not a string type");
796 continue;
798 if (first_arg_num != 0)
800 /* Verify that first_arg_num points to the last arg,
801 the ... */
802 while (argument)
803 arg_num++, argument = TREE_CHAIN (argument);
804 if (arg_num != first_arg_num)
806 error ("args to be formatted is not ...");
807 continue;
812 record_function_format (DECL_NAME (decl),
813 DECL_ASSEMBLER_NAME (decl),
814 format_type, format_num, first_arg_num);
815 break;
818 case A_FORMAT_ARG:
820 tree format_num_expr = TREE_VALUE (args);
821 int format_num, arg_num;
822 tree argument;
824 if (TREE_CODE (decl) != FUNCTION_DECL)
826 error_with_decl (decl,
827 "argument format specified for non-function `%s'");
828 continue;
831 /* Strip any conversions from the first arg number and verify it
832 is a constant. */
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");
841 continue;
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
848 is in error. */
849 argument = TYPE_ARG_TYPES (type);
850 if (argument)
852 for (arg_num = 1; ; ++arg_num)
854 if (argument == 0 || arg_num == format_num)
855 break;
856 argument = TREE_CHAIN (argument);
858 if (! argument
859 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
860 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
861 != char_type_node))
863 error ("format string arg not a string type");
864 continue;
868 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
869 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
870 != char_type_node))
872 error ("function does not return string type");
873 continue;
876 record_international_format (DECL_NAME (decl),
877 DECL_ASSEMBLER_NAME (decl),
878 format_num);
879 break;
882 case A_WEAK:
883 declare_weak (decl);
884 break;
886 case A_ALIAS:
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)
893 tree id;
895 id = TREE_VALUE (args);
896 if (TREE_CODE (id) != STRING_CST)
898 error ("alias arg not a string");
899 break;
901 id = get_identifier (TREE_STRING_POINTER (id));
903 if (TREE_CODE (decl) == FUNCTION_DECL)
904 DECL_INITIAL (decl) = error_mark_node;
905 else
906 DECL_EXTERNAL (decl) = 0;
907 assemble_alias (decl, id);
909 else
910 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
911 break;
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));
926 else
927 DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
928 break;
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));
943 else
944 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
945 break;
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. */
960 void
961 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
962 tree specs_attrs;
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;
973 return;
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)
987 specs = s = t;
988 else
990 TREE_CHAIN (s) = t;
991 s = t;
994 else
996 if (attrs == NULL_TREE)
997 attrs = a = TREE_PURPOSE (t);
998 else
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)
1005 a = TREE_CHAIN (a);
1009 /* Terminate the lists. */
1010 if (s != NULL_TREE)
1011 TREE_CHAIN (s) = NULL_TREE;
1012 if (a != NULL_TREE)
1013 TREE_CHAIN (a) = NULL_TREE;
1015 /* All done. */
1016 *declspecs = specs;
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. */
1026 tree
1027 strip_attrs (specs_attrs)
1028 tree specs_attrs;
1030 tree specs, attrs;
1032 split_specs_attrs (specs_attrs, &specs, &attrs);
1034 while (attrs)
1036 warning ("`%s' attribute ignored",
1037 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1038 attrs = TREE_CHAIN (attrs);
1041 return specs;
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
1064 typedef struct {
1065 const char *format_chars;
1066 int pointer_count;
1067 /* Type of argument if no length modifier is used. */
1068 tree *nolen;
1069 /* Type of argument if length modifier for shortening to byte is used.
1070 If NULL, then this modifier is not allowed. */
1071 tree *hhlen;
1072 /* Type of argument if length modifier for shortening is used.
1073 If NULL, then this modifier is not allowed. */
1074 tree *hlen;
1075 /* Type of argument if length modifier `l' is used.
1076 If NULL, then this modifier is not allowed. */
1077 tree *llen;
1078 /* Type of argument if length modifier `q' or `ll' is used.
1079 If NULL, then this modifier is not allowed. */
1080 tree *qlen;
1081 /* Type of argument if length modifier `L' is used.
1082 If NULL, then this modifier is not allowed. */
1083 tree *bigllen;
1084 /* Type of argument if length modifier `Z' is used.
1085 If NULL, then this modifier is not allowed. */
1086 tree *zlen;
1087 /* List of other modifier characters allowed with these options. */
1088 const char *flag_chars;
1089 } format_char_info;
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. */
1180 void
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). */
1218 static void
1219 record_function_format (name, assembler_name, format_type,
1220 format_num, first_arg_num)
1221 tree name;
1222 tree assembler_name;
1223 enum format_type format_type;
1224 int format_num;
1225 int first_arg_num;
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)
1234 break;
1236 if (! info)
1238 info = (function_format_info *) xmalloc (sizeof (function_format_info));
1239 info->next = function_format_list;
1240 function_format_list = info;
1242 info->name = name;
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
1255 from 1). */
1257 static void
1258 record_international_format (name, assembler_name, format_num)
1259 tree name;
1260 tree assembler_name;
1261 int format_num;
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)
1270 break;
1273 if (! info)
1275 info
1276 = (international_format_info *)
1277 xmalloc (sizeof (international_format_info));
1278 info->next = international_format_list;
1279 international_format_list = info;
1281 info->name = name;
1282 info->assembler_name = assembler_name;
1285 info->format_num = format_num;
1288 static void
1289 tfaff ()
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. */
1300 void
1301 check_function_format (name, assembler_name, params)
1302 tree name;
1303 tree assembler_name;
1304 tree params;
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);
1317 break;
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. */
1326 static void
1327 check_format_info (info, params)
1328 function_format_info *info;
1329 tree params;
1331 int i;
1332 int arg_num;
1333 int suppressed, wide, precise;
1334 int length_char = 0;
1335 int format_char;
1336 int format_length;
1337 tree format_tree;
1338 tree cur_param;
1339 tree cur_type;
1340 tree wanted_type;
1341 tree first_fillin_param;
1342 const char *format_chars;
1343 format_char_info *fci = NULL;
1344 char flag_chars[8];
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)
1351 if (params == 0)
1352 return;
1353 if (arg_num == info->format_num)
1354 break;
1355 params = TREE_CHAIN (params);
1357 format_tree = TREE_VALUE (params);
1358 params = TREE_CHAIN (params);
1359 if (format_tree == 0)
1360 return;
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))
1369 == FUNCTION_DECL))
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)))
1382 tree inner_args;
1383 int i;
1385 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
1386 inner_args != 0;
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");
1401 return;
1403 if (TREE_CODE (format_tree) != ADDR_EXPR)
1404 return;
1405 format_tree = TREE_OPERAND (format_tree, 0);
1406 if (TREE_CODE (format_tree) != STRING_CST)
1407 return;
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");
1415 return;
1417 /* Skip to first argument to check. */
1418 while (arg_num + 1 < info->first_arg_num)
1420 if (params == 0)
1421 return;
1422 params = TREE_CHAIN (params);
1423 ++arg_num;
1426 first_fillin_param = params;
1427 while (1)
1429 int aflag;
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");
1436 return;
1438 if (*format_chars++ != '%')
1439 continue;
1440 if (*format_chars == 0)
1442 warning ("spurious trailing `%%' in format");
1443 continue;
1445 if (*format_chars == '%')
1447 ++format_chars;
1448 continue;
1450 flag_chars[0] = 0;
1451 suppressed = wide = precise = FALSE;
1452 if (info->format_type == scanf_format_type)
1454 suppressed = *format_chars == '*';
1455 if (suppressed)
1456 ++format_chars;
1457 while (ISDIGIT (*format_chars))
1458 ++format_chars;
1460 else if (info->format_type == strftime_format_type)
1462 while (*format_chars != 0 && index ("_-0^#", *format_chars) != 0)
1464 if (pedantic)
1465 warning ("ANSI C does not support the strftime `%c' flag",
1466 *format_chars);
1467 if (index (flag_chars, *format_chars) != 0)
1469 warning ("repeated `%c' flag in format",
1470 *format_chars);
1471 ++format_chars;
1473 else
1475 i = strlen (flag_chars);
1476 flag_chars[i++] = *format_chars++;
1477 flag_chars[i] = 0;
1480 while (ISDIGIT ((unsigned char) *format_chars))
1482 wide = TRUE;
1483 ++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++;
1491 flag_chars[i] = 0;
1492 if (*format_chars == 'E' || *format_chars == 'O')
1494 warning ("multiple E/O modifiers in format");
1495 while (*format_chars == 'E' || *format_chars == 'O')
1496 ++format_chars;
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')
1511 if (*p == '$')
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");
1525 return;
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++);
1534 else
1536 i = strlen (flag_chars);
1537 flag_chars[i++] = *format_chars++;
1538 flag_chars[i] = 0;
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 == '*')
1553 wide = TRUE;
1554 /* "...a field width...may be indicated by an asterisk.
1555 In this case, an int argument supplies the field width..." */
1556 ++format_chars;
1557 if (params == 0)
1559 tfaff ();
1560 return;
1562 if (info->first_arg_num != 0)
1564 cur_param = TREE_VALUE (params);
1565 params = TREE_CHAIN (params);
1566 ++arg_num;
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);
1579 else
1581 while (ISDIGIT (*format_chars))
1583 wide = TRUE;
1584 ++format_chars;
1587 if (*format_chars == '.')
1589 precise = TRUE;
1590 ++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)
1599 ++format_chars;
1600 if (params == 0)
1602 tfaff ();
1603 return;
1605 cur_param = TREE_VALUE (params);
1606 params = TREE_CHAIN (params);
1607 ++arg_num;
1608 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1609 != integer_type_node)
1610 warning ("field width is not type int (arg %d)",
1611 arg_num);
1614 else
1616 while (ISDIGIT (*format_chars))
1617 ++format_chars;
1622 aflag = 0;
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++;
1631 if (pedantic)
1632 warning ("ANSI C does not support the `%c' length modifier",
1633 length_char);
1635 else if (*format_chars == 'Z')
1637 length_char = *format_chars++;
1638 if (pedantic)
1639 warning ("ANSI C does not support the `Z' length modifier");
1641 else
1642 length_char = 0;
1643 if (length_char == 'l' && *format_chars == 'l')
1645 length_char = 'q', format_chars++;
1646 /* FIXME: Is allowed in ISO C 9x. */
1647 if (pedantic)
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. */
1654 if (pedantic)
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. */
1663 aflag = 1;
1664 format_chars++;
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");
1675 continue;
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);
1686 format_chars++;
1687 switch (info->format_type)
1689 case printf_format_type:
1690 fci = print_char_table;
1691 break;
1692 case scanf_format_type:
1693 fci = scan_char_table;
1694 break;
1695 case strftime_format_type:
1696 fci = time_char_table;
1697 break;
1698 default:
1699 abort ();
1701 while (fci->format_chars != 0
1702 && index (fci->format_chars, format_char) == 0)
1703 ++fci;
1704 if (fci->format_chars == 0)
1706 if (format_char >= 040 && format_char < 0177)
1707 warning ("unknown conversion type character `%c' in format",
1708 format_char);
1709 else
1710 warning ("unknown conversion type character 0x%x in format",
1711 format_char);
1712 continue;
1714 if (pedantic)
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",
1728 format_char);
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. */
1735 aflag = 0;
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 == '^')
1744 ++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 == ']')
1748 ++format_chars;
1749 while (*format_chars && *format_chars != ']')
1750 ++format_chars;
1751 if (*format_chars != ']')
1752 /* The end of the format string was reached. */
1753 warning ("no closing `]' for `%%[' format");
1755 if (suppressed)
1757 if (index (fci->flag_chars, '*') == 0)
1758 warning ("suppression of `%c' conversion in format", format_char);
1759 continue;
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)
1768 continue;
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",
1774 format_char);
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)
1791 continue;
1792 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1793 /* This specifier takes no argument. */
1794 continue;
1795 if (params == 0)
1797 tfaff ();
1798 return;
1800 cur_param = TREE_VALUE (params);
1801 params = TREE_CHAIN (params);
1802 ++arg_num;
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);
1817 else
1818 cur_param = 0;
1820 continue;
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)"),
1826 arg_num);
1827 break;
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
1836 && wanted_type != 0
1837 && TREE_CODE (cur_type) != ERROR_MARK
1838 && (TYPE_READONLY (cur_type)
1839 || (cur_param != 0
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)));
1869 that = 0;
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)));
1879 else
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. */
1886 if (that == 0)
1888 if (TREE_CODE (cur_type) == POINTER_TYPE)
1889 that = "pointer";
1890 else
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. */
1914 void
1915 constant_expression_warning (value)
1916 tree 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. */
1931 void
1932 overflow_warning (value)
1933 tree 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. */
1960 void
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. */
1982 tree
1983 convert_and_check (type, expr)
1984 tree 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. */
2003 if ((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");
2009 else
2010 unsigned_conversion_warning (t, expr);
2012 return t;
2015 void
2016 c_expand_expr_stmt (expr)
2017 tree 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. */
2035 tree
2036 check_case_value (value)
2037 tree value;
2039 if (value == NULL_TREE)
2040 return value;
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;
2051 else
2052 /* Promote char or short to int. */
2053 value = default_conversion (value);
2055 constant_expression_warning (value);
2057 return value;
2060 /* Return an integer type with BITS bits of precision,
2061 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2063 tree
2064 type_for_size (bits, unsignedp)
2065 unsigned bits;
2066 int 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;
2096 return 0;
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. */
2103 tree
2104 type_for_mode (mode, unsignedp)
2105 enum machine_mode mode;
2106 int unsignedp;
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;
2138 #endif
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);
2155 return 0;
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)
2163 tree value;
2164 int unsignedp;
2166 int log;
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))
2180 log = 0;
2181 else if (TREE_INT_CST_HIGH (value) != 0)
2182 log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
2183 else
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). */
2192 void
2193 binary_op_error (code)
2194 enum tree_code code;
2196 register const char *opname;
2198 switch (code)
2200 case NOP_EXPR:
2201 error ("invalid truth-value expression");
2202 return;
2204 case PLUS_EXPR:
2205 opname = "+"; break;
2206 case MINUS_EXPR:
2207 opname = "-"; break;
2208 case MULT_EXPR:
2209 opname = "*"; break;
2210 case MAX_EXPR:
2211 opname = "max"; break;
2212 case MIN_EXPR:
2213 opname = "min"; break;
2214 case EQ_EXPR:
2215 opname = "=="; break;
2216 case NE_EXPR:
2217 opname = "!="; break;
2218 case LE_EXPR:
2219 opname = "<="; break;
2220 case GE_EXPR:
2221 opname = ">="; break;
2222 case LT_EXPR:
2223 opname = "<"; break;
2224 case GT_EXPR:
2225 opname = ">"; break;
2226 case LSHIFT_EXPR:
2227 opname = "<<"; break;
2228 case RSHIFT_EXPR:
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;
2236 case BIT_AND_EXPR:
2237 opname = "&"; break;
2238 case BIT_IOR_EXPR:
2239 opname = "|"; break;
2240 case TRUTH_ANDIF_EXPR:
2241 opname = "&&"; break;
2242 case TRUTH_ORIF_EXPR:
2243 opname = "||"; break;
2244 case BIT_XOR_EXPR:
2245 opname = "^"; break;
2246 case LROTATE_EXPR:
2247 case RROTATE_EXPR:
2248 opname = "rotate"; break;
2249 default:
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
2267 that value. */
2269 tree
2270 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2271 tree *op0_ptr, *op1_ptr;
2272 tree *restype_ptr;
2273 enum tree_code *rescode_ptr;
2275 register tree type;
2276 tree op0 = *op0_ptr;
2277 tree op1 = *op1_ptr;
2278 int unsignedp0, unsignedp1;
2279 int real1, real2;
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;
2310 primop0 = primop1;
2311 primop1 = tem;
2312 tem = op0;
2313 op0 = op1;
2314 op1 = tem;
2315 *op0_ptr = op0;
2316 *op1_ptr = op1;
2317 unsignedp0 = unsignedp1;
2318 unsignedp1 = temi;
2319 temi = real1;
2320 real1 = real2;
2321 real2 = temi;
2323 switch (code)
2325 case LT_EXPR:
2326 code = GT_EXPR;
2327 break;
2328 case GT_EXPR:
2329 code = LT_EXPR;
2330 break;
2331 case LE_EXPR:
2332 code = GE_EXPR;
2333 break;
2334 case GE_EXPR:
2335 code = LE_EXPR;
2336 break;
2337 default:
2338 break;
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);
2368 tree val;
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
2374 type itself. */
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);
2399 else
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);
2407 val = 0;
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)
2421 if (max_lt)
2422 val = boolean_true_node;
2423 if (!min_lt)
2424 val = boolean_false_node;
2426 else if (code == GT_EXPR)
2428 if (min_gt)
2429 val = boolean_true_node;
2430 if (!max_gt)
2431 val = boolean_false_node;
2433 else if (code == LE_EXPR)
2435 if (!max_gt)
2436 val = boolean_true_node;
2437 if (min_gt)
2438 val = boolean_false_node;
2440 else if (code == GE_EXPR)
2442 if (!min_lt)
2443 val = boolean_true_node;
2444 if (max_lt)
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)
2461 if (val != 0)
2462 switch (code)
2464 case LT_EXPR:
2465 case GE_EXPR:
2466 primop1 = TYPE_MIN_VALUE (type);
2467 val = 0;
2468 break;
2470 case LE_EXPR:
2471 case GT_EXPR:
2472 primop1 = TYPE_MAX_VALUE (type);
2473 val = 0;
2474 break;
2476 default:
2477 break;
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");
2501 if (val != 0)
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);
2506 return 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
2523 alter the result.
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),
2533 type);
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)),
2537 primop0);
2538 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2539 primop1);
2541 else
2543 /* Here we must do the comparison on the nominal type
2544 using the args exactly as we received them. */
2545 type = *restype_ptr;
2546 primop0 = op0;
2547 primop1 = op1;
2549 if (!real1 && !real2 && integer_zerop (primop1)
2550 && TREE_UNSIGNED (*restype_ptr))
2552 tree value = 0;
2553 switch (code)
2555 case GE_EXPR:
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. */
2560 if (extra_warnings
2561 && ! (TREE_CODE (primop0) == INTEGER_CST
2562 && ! TREE_OVERFLOW (convert (signed_type (type),
2563 primop0))))
2564 warning ("comparison of unsigned expression >= 0 is always true");
2565 value = boolean_true_node;
2566 break;
2568 case LT_EXPR:
2569 if (extra_warnings
2570 && ! (TREE_CODE (primop0) == INTEGER_CST
2571 && ! TREE_OVERFLOW (convert (signed_type (type),
2572 primop0))))
2573 warning ("comparison of unsigned expression < 0 is always false");
2574 value = boolean_false_node;
2575 break;
2577 default:
2578 break;
2581 if (value != 0)
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),
2586 primop0, value);
2587 return value;
2592 *op0_ptr = convert (type, primop0);
2593 *op1_ptr = convert (type, primop1);
2595 *restype_ptr = boolean_type_node;
2597 return 0;
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'. */
2611 tree
2612 truthvalue_conversion (expr)
2613 tree expr;
2615 if (TREE_CODE (expr) == ERROR_MARK)
2616 return expr;
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)))
2623 case RECORD_TYPE:
2624 error ("struct type value used where scalar is required");
2625 return boolean_false_node;
2627 case UNION_TYPE:
2628 error ("union type value used where scalar is required");
2629 return boolean_false_node;
2631 case ARRAY_TYPE:
2632 error ("array type value used where scalar is required");
2633 return boolean_false_node;
2635 default:
2636 break;
2638 #endif /* 0 */
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. */
2644 #if 0
2645 case COMPONENT_REF:
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)))
2649 return expr;
2650 break;
2651 #endif
2653 case EQ_EXPR:
2654 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2655 or comparison expressions as truth values at this level. */
2656 #if 0
2657 if (integer_zerop (TREE_OPERAND (expr, 1)))
2658 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
2659 #endif
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:
2664 case TRUTH_OR_EXPR:
2665 case TRUTH_XOR_EXPR:
2666 case TRUTH_NOT_EXPR:
2667 TREE_TYPE (expr) = boolean_type_node;
2668 return expr;
2670 case ERROR_MARK:
2671 return expr;
2673 case INTEGER_CST:
2674 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2676 case REAL_CST:
2677 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2679 case ADDR_EXPR:
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)))
2684 break;
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);
2689 else
2690 return boolean_true_node;
2692 case COMPLEX_EXPR:
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)),
2699 case NEGATE_EXPR:
2700 case ABS_EXPR:
2701 case FLOAT_EXPR:
2702 case FFS_EXPR:
2703 /* These don't change whether an object is non-zero or zero. */
2704 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2706 case LROTATE_EXPR:
2707 case RROTATE_EXPR:
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)));
2713 else
2714 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2716 case COND_EXPR:
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))));
2722 case CONVERT_EXPR:
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)
2727 break;
2728 /* fall through... */
2729 case NOP_EXPR:
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));
2734 break;
2736 case MINUS_EXPR:
2737 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2738 this case. */
2739 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2740 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2741 break;
2742 /* fall through... */
2743 case BIT_XOR_EXPR:
2744 /* This and MINUS_EXPR can be changed into a comparison of the
2745 two objects. */
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);
2755 case BIT_AND_EXPR:
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);
2760 break;
2762 case MODIFY_EXPR:
2763 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2764 warning ("suggest parentheses around assignment used as truth value");
2765 break;
2767 default:
2768 break;
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)),
2779 0));
2782 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2785 #if USE_CPPLIB
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--)
2801 yy_get_token ()
2803 for (;;)
2805 parse_in.limit = parse_in.token_buffer;
2806 cpp_token = cpp_get_token (&parse_in);
2807 if (cpp_token == CPP_EOF)
2808 return -1;
2809 yy_lim = CPP_PWRITTEN (&parse_in);
2810 yy_cur = parse_in.token_buffer;
2811 if (yy_cur < yy_lim)
2812 return *yy_cur++;
2816 char *
2817 get_directive_line ()
2819 static char *directive_buffer = NULL;
2820 static unsigned buffer_length = 0;
2821 register char *p;
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; ; )
2836 int c;
2838 /* Make buffer bigger if it is full. */
2839 if (p >= buffer_limit)
2841 register unsigned bytes_used = (p - directive_buffer);
2843 buffer_length *= 2;
2844 directive_buffer
2845 = (char *)xrealloc (directive_buffer, buffer_length);
2846 p = &directive_buffer[bytes_used];
2847 buffer_limit = &directive_buffer[buffer_length];
2850 c = GETC ();
2852 /* Discard initial whitespace. */
2853 if ((c == ' ' || c == '\t') && p == directive_buffer)
2854 continue;
2856 /* Detect the end of the directive. */
2857 if (c == '\n' && looking_for == 0)
2859 UNGETC (c);
2860 c = '\0';
2863 *p++ = c;
2865 if (c == 0)
2866 return directive_buffer;
2868 /* Handle string and character constant syntax. */
2869 if (looking_for)
2871 if (looking_for == c && !char_escaped)
2872 looking_for = 0; /* Found terminator... stop looking. */
2874 else
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);
2883 #else
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. */
2897 char *
2898 get_directive_line (finput)
2899 register FILE *finput;
2901 static char *directive_buffer = NULL;
2902 static unsigned buffer_length = 0;
2903 register char *p;
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; ; )
2918 int c;
2920 /* Make buffer bigger if it is full. */
2921 if (p >= buffer_limit)
2923 register unsigned bytes_used = (p - directive_buffer);
2925 buffer_length *= 2;
2926 directive_buffer
2927 = (char *)xrealloc (directive_buffer, buffer_length);
2928 p = &directive_buffer[bytes_used];
2929 buffer_limit = &directive_buffer[buffer_length];
2932 c = getc (finput);
2934 /* Discard initial whitespace. */
2935 if ((c == ' ' || c == '\t') && p == directive_buffer)
2936 continue;
2938 /* Detect the end of the directive. */
2939 if (looking_for == 0
2940 && (c == '\n' || c == EOF))
2942 ungetc (c, finput);
2943 c = '\0';
2946 *p++ = c;
2948 if (c == 0)
2949 return directive_buffer;
2951 /* Handle string and character constant syntax. */
2952 if (looking_for)
2954 if (looking_for == c && !char_escaped)
2955 looking_for = 0; /* Found terminator... stop looking. */
2957 else
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. */
2971 tree
2972 c_build_qualified_type (type, type_quals)
2973 tree type;
2974 int 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),
2993 type_quals),
2994 TYPE_DOMAIN (type));
2995 return build_qualified_type (type, type_quals);
2998 /* Apply the TYPE_QUALS to the new DECL. */
3000 void
3001 c_apply_type_quals_to_decl (type_quals, decl)
3002 int type_quals;
3003 tree 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
3026 decl. */
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. */
3034 else
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. */
3049 static tree
3050 c_find_base_decl (t)
3051 tree t;
3053 int i;
3054 tree decl;
3056 if (t == NULL_TREE || t == error_mark_node)
3057 return NULL_TREE;
3059 if (!POINTER_TYPE_P (TREE_TYPE (t)))
3060 return NULL_TREE;
3062 decl = NULL_TREE;
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. */
3068 return t;
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
3072 also the same. */
3074 /* Handle general expressions. */
3075 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3077 case '1':
3078 case '2':
3079 case '3':
3080 for (i = tree_code_length [(int) TREE_CODE (t)]; --i >= 0;)
3082 tree d = c_find_base_decl (TREE_OPERAND (t, i));
3083 if (d)
3085 if (!decl)
3086 decl = d;
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. */
3090 decl = NULL_TREE;
3093 break;
3095 default:
3096 break;
3099 return decl;
3102 /* Return the typed-based alias set for T, which may be an expression
3103 or a type. */
3106 c_get_alias_set (t)
3107 tree t;
3109 tree type;
3110 tree u;
3112 if (t == error_mark_node)
3113 return 0;
3115 type = (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
3116 ? t : TREE_TYPE (t);
3118 if (type == error_mark_node)
3119 return 0;
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. */
3129 return 0;
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. */
3137 for (u = t;
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)
3142 return 0;
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
3198 different.) */
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
3211 alias set. */
3212 TYPE_ALIAS_SET (type) = new_alias_set ();
3214 return TYPE_ALIAS_SET (type);