* arm.c (arm_split_constant): Don't try to force a constant to
[official-gcc.git] / gcc / c-common.c
blob8ea14899a259bf72564bbd2979f107c5ce99199c
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,
58 A_INIT_PRIORITY};
60 enum format_type { printf_format_type, scanf_format_type,
61 strftime_format_type };
63 static void declare_hidden_char_array PROTO((const char *, const char *));
64 static void add_attribute PROTO((enum attrs, const char *,
65 int, int, int));
66 static void init_attributes PROTO((void));
67 static void record_function_format PROTO((tree, tree, enum format_type,
68 int, int));
69 static void record_international_format PROTO((tree, tree, int));
70 static tree c_find_base_decl PROTO((tree));
72 /* Keep a stack of if statements. We record the number of compound
73 statements seen up to the if keyword, as well as the line number
74 and file of the if. If a potentially ambiguous else is seen, that
75 fact is recorded; the warning is issued when we can be sure that
76 the enclosing if statement does not have an else branch. */
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 /* A list of objects which have constructors or destructors which
98 reside in the global scope, and have an init_priority attribute
99 associated with them. The decl is stored in the TREE_VALUE slot
100 and the priority number is stored in the TREE_PURPOSE slot. */
101 tree static_aggregates_initp;
103 void
104 c_expand_start_cond (cond, exitflag, compstmt_count)
105 tree cond;
106 int exitflag;
107 int compstmt_count;
109 /* Make sure there is enough space on the stack. */
110 if (if_stack_space == 0)
112 if_stack_space = 10;
113 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
115 else if (if_stack_space == if_stack_pointer)
117 if_stack_space += 10;
118 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
121 /* Record this if statement. */
122 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
123 if_stack[if_stack_pointer].file = input_filename;
124 if_stack[if_stack_pointer].line = lineno;
125 if_stack[if_stack_pointer].needs_warning = 0;
126 if_stack_pointer++;
128 expand_start_cond (cond, exitflag);
131 /* Generate RTL for the end of an if-then. Optionally warn if a nested
132 if statement had an ambiguous else clause. */
134 void
135 c_expand_end_cond ()
137 if_stack_pointer--;
138 if (if_stack[if_stack_pointer].needs_warning)
139 warning_with_file_and_line (if_stack[if_stack_pointer].file,
140 if_stack[if_stack_pointer].line,
141 "suggest explicit braces to avoid ambiguous `else'");
142 expand_end_cond ();
145 /* Generate RTL between the then-clause and the else-clause
146 of an if-then-else. */
148 void
149 c_expand_start_else ()
151 /* An ambiguous else warning must be generated for the enclosing if
152 statement, unless we see an else branch for that one, too. */
153 if (warn_parentheses
154 && if_stack_pointer > 1
155 && (if_stack[if_stack_pointer - 1].compstmt_count
156 == if_stack[if_stack_pointer - 2].compstmt_count))
157 if_stack[if_stack_pointer - 2].needs_warning = 1;
159 /* Even if a nested if statement had an else branch, it can't be
160 ambiguous if this one also has an else. So don't warn in that
161 case. Also don't warn for any if statements nested in this else. */
162 if_stack[if_stack_pointer - 1].needs_warning = 0;
163 if_stack[if_stack_pointer - 1].compstmt_count--;
165 expand_start_else ();
168 /* Make bindings for __FUNCTION__, __PRETTY_FUNCTION__, and __func__. */
170 void
171 declare_function_name ()
173 const char *name, *printable_name;
175 if (current_function_decl == NULL)
177 name = "";
178 printable_name = "top level";
180 else
182 /* Allow functions to be nameless (such as artificial ones). */
183 if (DECL_NAME (current_function_decl))
184 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
185 else
186 name = "";
187 printable_name = (*decl_printable_name) (current_function_decl, 2);
190 declare_hidden_char_array ("__FUNCTION__", name);
191 declare_hidden_char_array ("__PRETTY_FUNCTION__", printable_name);
192 /* The ISO C people "of course" couldn't use __FUNCTION__ in the
193 ISO C 9x standard; instead a new variable is invented. */
194 declare_hidden_char_array ("__func__", name);
197 static void
198 declare_hidden_char_array (name, value)
199 const char *name, *value;
201 tree decl, type, init;
202 int vlen;
204 /* If the default size of char arrays isn't big enough for the name,
205 or if we want to give warnings for large objects, make a bigger one. */
206 vlen = strlen (value) + 1;
207 type = char_array_type_node;
208 if (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) < vlen
209 || warn_larger_than)
210 type = build_array_type (char_type_node,
211 build_index_type (build_int_2 (vlen, 0)));
212 push_obstacks_nochange ();
213 decl = build_decl (VAR_DECL, get_identifier (name), type);
214 TREE_STATIC (decl) = 1;
215 TREE_READONLY (decl) = 1;
216 TREE_ASM_WRITTEN (decl) = 1;
217 DECL_SOURCE_LINE (decl) = 0;
218 DECL_ARTIFICIAL (decl) = 1;
219 DECL_IN_SYSTEM_HEADER (decl) = 1;
220 DECL_IGNORED_P (decl) = 1;
221 init = build_string (vlen, value);
222 TREE_TYPE (init) = type;
223 DECL_INITIAL (decl) = init;
224 finish_decl (pushdecl (decl), init, NULL_TREE);
227 /* Given a chain of STRING_CST nodes,
228 concatenate them into one STRING_CST
229 and give it a suitable array-of-chars data type. */
231 tree
232 combine_strings (strings)
233 tree strings;
235 register tree value, t;
236 register int length = 1;
237 int wide_length = 0;
238 int wide_flag = 0;
239 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
240 int nchars;
242 if (TREE_CHAIN (strings))
244 /* More than one in the chain, so concatenate. */
245 register char *p, *q;
247 /* Don't include the \0 at the end of each substring,
248 except for the last one.
249 Count wide strings and ordinary strings separately. */
250 for (t = strings; t; t = TREE_CHAIN (t))
252 if (TREE_TYPE (t) == wchar_array_type_node)
254 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
255 wide_flag = 1;
257 else
258 length += (TREE_STRING_LENGTH (t) - 1);
261 /* If anything is wide, the non-wides will be converted,
262 which makes them take more space. */
263 if (wide_flag)
264 length = length * wchar_bytes + wide_length;
266 p = savealloc (length);
268 /* Copy the individual strings into the new combined string.
269 If the combined string is wide, convert the chars to ints
270 for any individual strings that are not wide. */
272 q = p;
273 for (t = strings; t; t = TREE_CHAIN (t))
275 int len = (TREE_STRING_LENGTH (t)
276 - ((TREE_TYPE (t) == wchar_array_type_node)
277 ? wchar_bytes : 1));
278 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
280 memcpy (q, TREE_STRING_POINTER (t), len);
281 q += len;
283 else
285 int i;
286 for (i = 0; i < len; i++)
288 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
289 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
290 else
291 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
293 q += len * wchar_bytes;
296 if (wide_flag)
298 int i;
299 for (i = 0; i < wchar_bytes; i++)
300 *q++ = 0;
302 else
303 *q = 0;
305 value = make_node (STRING_CST);
306 TREE_STRING_POINTER (value) = p;
307 TREE_STRING_LENGTH (value) = length;
309 else
311 value = strings;
312 length = TREE_STRING_LENGTH (value);
313 if (TREE_TYPE (value) == wchar_array_type_node)
314 wide_flag = 1;
317 /* Compute the number of elements, for the array type. */
318 nchars = wide_flag ? length / wchar_bytes : length;
320 /* Create the array type for the string constant.
321 -Wwrite-strings says make the string constant an array of const char
322 so that copying it to a non-const pointer will get a warning.
323 For C++, this is the standard behavior. */
324 if (flag_const_strings
325 && (! flag_traditional && ! flag_writable_strings))
327 tree elements
328 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
329 1, 0);
330 TREE_TYPE (value)
331 = build_array_type (elements,
332 build_index_type (build_int_2 (nchars - 1, 0)));
334 else
335 TREE_TYPE (value)
336 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
337 build_index_type (build_int_2 (nchars - 1, 0)));
339 TREE_READONLY (value) = TREE_CONSTANT (value) = ! flag_writable_strings;
340 TREE_STATIC (value) = 1;
341 return value;
344 /* To speed up processing of attributes, we maintain an array of
345 IDENTIFIER_NODES and the corresponding attribute types. */
347 /* Array to hold attribute information. */
349 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
351 static int attrtab_idx = 0;
353 /* Add an entry to the attribute table above. */
355 static void
356 add_attribute (id, string, min_len, max_len, decl_req)
357 enum attrs id;
358 const char *string;
359 int min_len, max_len;
360 int decl_req;
362 char buf[100];
364 attrtab[attrtab_idx].id = id;
365 attrtab[attrtab_idx].name = get_identifier (string);
366 attrtab[attrtab_idx].min = min_len;
367 attrtab[attrtab_idx].max = max_len;
368 attrtab[attrtab_idx++].decl_req = decl_req;
370 sprintf (buf, "__%s__", string);
372 attrtab[attrtab_idx].id = id;
373 attrtab[attrtab_idx].name = get_identifier (buf);
374 attrtab[attrtab_idx].min = min_len;
375 attrtab[attrtab_idx].max = max_len;
376 attrtab[attrtab_idx++].decl_req = decl_req;
379 /* Initialize attribute table. */
381 static void
382 init_attributes ()
384 add_attribute (A_PACKED, "packed", 0, 0, 0);
385 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
386 add_attribute (A_COMMON, "common", 0, 0, 1);
387 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
388 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
389 add_attribute (A_UNUSED, "unused", 0, 0, 0);
390 add_attribute (A_CONST, "const", 0, 0, 1);
391 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
392 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
393 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
394 add_attribute (A_MODE, "mode", 1, 1, 1);
395 add_attribute (A_SECTION, "section", 1, 1, 1);
396 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
397 add_attribute (A_FORMAT, "format", 3, 3, 1);
398 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
399 add_attribute (A_WEAK, "weak", 0, 0, 1);
400 add_attribute (A_ALIAS, "alias", 1, 1, 1);
401 add_attribute (A_INIT_PRIORITY, "init_priority", 0, 1, 0);
402 add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
403 add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
406 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
407 and install them in NODE, which is either a DECL (including a TYPE_DECL)
408 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
409 and declaration modifiers but before the declaration proper. */
411 void
412 decl_attributes (node, attributes, prefix_attributes)
413 tree node, attributes, prefix_attributes;
415 tree decl = 0, type = 0;
416 int is_type = 0;
417 tree a;
419 if (attrtab_idx == 0)
420 init_attributes ();
422 if (TREE_CODE_CLASS (TREE_CODE (node)) == 'd')
424 decl = node;
425 type = TREE_TYPE (decl);
426 is_type = TREE_CODE (node) == TYPE_DECL;
428 else if (TREE_CODE_CLASS (TREE_CODE (node)) == 't')
429 type = node, is_type = 1;
431 #ifdef PRAGMA_INSERT_ATTRIBUTES
432 /* If the code in c-pragma.c wants to insert some attributes then
433 allow it to do so. Do this before allowing machine back ends to
434 insert attributes, so that they have the opportunity to override
435 anything done here. */
436 PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
437 #endif
439 #ifdef INSERT_ATTRIBUTES
440 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
441 #endif
443 attributes = chainon (prefix_attributes, attributes);
445 for (a = attributes; a; a = TREE_CHAIN (a))
447 tree name = TREE_PURPOSE (a);
448 tree args = TREE_VALUE (a);
449 int i;
450 enum attrs id;
452 for (i = 0; i < attrtab_idx; i++)
453 if (attrtab[i].name == name)
454 break;
456 if (i == attrtab_idx)
458 if (! valid_machine_attribute (name, args, decl, type))
459 warning ("`%s' attribute directive ignored",
460 IDENTIFIER_POINTER (name));
461 else if (decl != 0)
462 type = TREE_TYPE (decl);
463 continue;
465 else if (attrtab[i].decl_req && decl == 0)
467 warning ("`%s' attribute does not apply to types",
468 IDENTIFIER_POINTER (name));
469 continue;
471 else if (list_length (args) < attrtab[i].min
472 || list_length (args) > attrtab[i].max)
474 error ("wrong number of arguments specified for `%s' attribute",
475 IDENTIFIER_POINTER (name));
476 continue;
479 id = attrtab[i].id;
480 switch (id)
482 case A_PACKED:
483 if (is_type)
484 TYPE_PACKED (type) = 1;
485 else if (TREE_CODE (decl) == FIELD_DECL)
486 DECL_PACKED (decl) = 1;
487 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
488 used for DECL_REGISTER. It wouldn't mean anything anyway. */
489 else
490 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
491 break;
493 case A_NOCOMMON:
494 if (TREE_CODE (decl) == VAR_DECL)
495 DECL_COMMON (decl) = 0;
496 else
497 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
498 break;
500 case A_COMMON:
501 if (TREE_CODE (decl) == VAR_DECL)
502 DECL_COMMON (decl) = 1;
503 else
504 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
505 break;
507 case A_NORETURN:
508 if (TREE_CODE (decl) == FUNCTION_DECL)
509 TREE_THIS_VOLATILE (decl) = 1;
510 else if (TREE_CODE (type) == POINTER_TYPE
511 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
512 TREE_TYPE (decl) = type
513 = build_pointer_type
514 (build_type_variant (TREE_TYPE (type),
515 TREE_READONLY (TREE_TYPE (type)), 1));
516 else
517 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
518 break;
520 case A_UNUSED:
521 if (is_type)
522 TREE_USED (type) = 1;
523 else if (TREE_CODE (decl) == PARM_DECL
524 || TREE_CODE (decl) == VAR_DECL
525 || TREE_CODE (decl) == FUNCTION_DECL
526 || TREE_CODE (decl) == LABEL_DECL)
527 TREE_USED (decl) = 1;
528 else
529 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
530 break;
532 case A_CONST:
533 if (TREE_CODE (decl) == FUNCTION_DECL)
534 TREE_READONLY (decl) = 1;
535 else if (TREE_CODE (type) == POINTER_TYPE
536 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
537 TREE_TYPE (decl) = type
538 = build_pointer_type
539 (build_type_variant (TREE_TYPE (type), 1,
540 TREE_THIS_VOLATILE (TREE_TYPE (type))));
541 else
542 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
543 break;
545 case A_T_UNION:
546 if (is_type
547 && TREE_CODE (type) == UNION_TYPE
548 && (decl == 0
549 || (TYPE_FIELDS (type) != 0
550 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
551 TYPE_TRANSPARENT_UNION (type) = 1;
552 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
553 && TREE_CODE (type) == UNION_TYPE
554 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
555 DECL_TRANSPARENT_UNION (decl) = 1;
556 else
557 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
558 break;
560 case A_CONSTRUCTOR:
561 if (TREE_CODE (decl) == FUNCTION_DECL
562 && TREE_CODE (type) == FUNCTION_TYPE
563 && decl_function_context (decl) == 0)
565 DECL_STATIC_CONSTRUCTOR (decl) = 1;
566 TREE_USED (decl) = 1;
568 else
569 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
570 break;
572 case A_DESTRUCTOR:
573 if (TREE_CODE (decl) == FUNCTION_DECL
574 && TREE_CODE (type) == FUNCTION_TYPE
575 && decl_function_context (decl) == 0)
577 DECL_STATIC_DESTRUCTOR (decl) = 1;
578 TREE_USED (decl) = 1;
580 else
581 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
582 break;
584 case A_MODE:
585 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
586 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
587 else
589 int j;
590 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
591 int len = strlen (p);
592 enum machine_mode mode = VOIDmode;
593 tree typefm;
595 if (len > 4 && p[0] == '_' && p[1] == '_'
596 && p[len - 1] == '_' && p[len - 2] == '_')
598 char *newp = (char *) alloca (len - 1);
600 strcpy (newp, &p[2]);
601 newp[len - 4] = '\0';
602 p = newp;
605 /* Give this decl a type with the specified mode.
606 First check for the special modes. */
607 if (! strcmp (p, "byte"))
608 mode = byte_mode;
609 else if (!strcmp (p, "word"))
610 mode = word_mode;
611 else if (! strcmp (p, "pointer"))
612 mode = ptr_mode;
613 else
614 for (j = 0; j < NUM_MACHINE_MODES; j++)
615 if (!strcmp (p, GET_MODE_NAME (j)))
616 mode = (enum machine_mode) j;
618 if (mode == VOIDmode)
619 error ("unknown machine mode `%s'", p);
620 else if (0 == (typefm = type_for_mode (mode,
621 TREE_UNSIGNED (type))))
622 error ("no data type for mode `%s'", p);
623 else
625 TREE_TYPE (decl) = type = typefm;
626 DECL_SIZE (decl) = 0;
627 layout_decl (decl, 0);
630 break;
632 case A_SECTION:
633 #ifdef ASM_OUTPUT_SECTION_NAME
634 if ((TREE_CODE (decl) == FUNCTION_DECL
635 || TREE_CODE (decl) == VAR_DECL)
636 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
638 if (TREE_CODE (decl) == VAR_DECL
639 && current_function_decl != NULL_TREE
640 && ! TREE_STATIC (decl))
641 error_with_decl (decl,
642 "section attribute cannot be specified for local variables");
643 /* The decl may have already been given a section attribute from
644 a previous declaration. Ensure they match. */
645 else if (DECL_SECTION_NAME (decl) != NULL_TREE
646 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
647 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
648 error_with_decl (node,
649 "section of `%s' conflicts with previous declaration");
650 else
651 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
653 else
654 error_with_decl (node,
655 "section attribute not allowed for `%s'");
656 #else
657 error_with_decl (node,
658 "section attributes are not supported for this target");
659 #endif
660 break;
662 case A_ALIGNED:
664 tree align_expr
665 = (args ? TREE_VALUE (args)
666 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
667 int align;
669 /* Strip any NOPs of any kind. */
670 while (TREE_CODE (align_expr) == NOP_EXPR
671 || TREE_CODE (align_expr) == CONVERT_EXPR
672 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
673 align_expr = TREE_OPERAND (align_expr, 0);
675 if (TREE_CODE (align_expr) != INTEGER_CST)
677 error ("requested alignment is not a constant");
678 continue;
681 align = TREE_INT_CST_LOW (align_expr) * BITS_PER_UNIT;
683 if (exact_log2 (align) == -1)
684 error ("requested alignment is not a power of 2");
685 else if (is_type)
686 TYPE_ALIGN (type) = align;
687 else if (TREE_CODE (decl) != VAR_DECL
688 && TREE_CODE (decl) != FIELD_DECL)
689 error_with_decl (decl,
690 "alignment may not be specified for `%s'");
691 else
692 DECL_ALIGN (decl) = align;
694 break;
696 case A_FORMAT:
698 tree format_type_id = TREE_VALUE (args);
699 tree format_num_expr = TREE_VALUE (TREE_CHAIN (args));
700 tree first_arg_num_expr
701 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
702 int format_num;
703 int first_arg_num;
704 enum format_type format_type;
705 tree argument;
706 int arg_num;
708 if (TREE_CODE (decl) != FUNCTION_DECL)
710 error_with_decl (decl,
711 "argument format specified for non-function `%s'");
712 continue;
715 if (TREE_CODE (format_type_id) != IDENTIFIER_NODE)
717 error ("unrecognized format specifier");
718 continue;
720 else
722 const char *p = IDENTIFIER_POINTER (format_type_id);
724 if (!strcmp (p, "printf") || !strcmp (p, "__printf__"))
725 format_type = printf_format_type;
726 else if (!strcmp (p, "scanf") || !strcmp (p, "__scanf__"))
727 format_type = scanf_format_type;
728 else if (!strcmp (p, "strftime")
729 || !strcmp (p, "__strftime__"))
730 format_type = strftime_format_type;
731 else
733 warning ("`%s' is an unrecognized format function type", p);
734 continue;
738 /* Strip any conversions from the string index and first arg number
739 and verify they are constants. */
740 while (TREE_CODE (format_num_expr) == NOP_EXPR
741 || TREE_CODE (format_num_expr) == CONVERT_EXPR
742 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
743 format_num_expr = TREE_OPERAND (format_num_expr, 0);
745 while (TREE_CODE (first_arg_num_expr) == NOP_EXPR
746 || TREE_CODE (first_arg_num_expr) == CONVERT_EXPR
747 || TREE_CODE (first_arg_num_expr) == NON_LVALUE_EXPR)
748 first_arg_num_expr = TREE_OPERAND (first_arg_num_expr, 0);
750 if (TREE_CODE (format_num_expr) != INTEGER_CST
751 || TREE_CODE (first_arg_num_expr) != INTEGER_CST)
753 error ("format string has non-constant operand number");
754 continue;
757 format_num = TREE_INT_CST_LOW (format_num_expr);
758 first_arg_num = TREE_INT_CST_LOW (first_arg_num_expr);
759 if (first_arg_num != 0 && first_arg_num <= format_num)
761 error ("format string arg follows the args to be formatted");
762 continue;
765 /* If a parameter list is specified, verify that the format_num
766 argument is actually a string, in case the format attribute
767 is in error. */
768 argument = TYPE_ARG_TYPES (type);
769 if (argument)
771 for (arg_num = 1; ; ++arg_num)
773 if (argument == 0 || arg_num == format_num)
774 break;
775 argument = TREE_CHAIN (argument);
777 if (! argument
778 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
779 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
780 != char_type_node))
782 error ("format string arg not a string type");
783 continue;
785 if (first_arg_num != 0)
787 /* Verify that first_arg_num points to the last arg,
788 the ... */
789 while (argument)
790 arg_num++, argument = TREE_CHAIN (argument);
791 if (arg_num != first_arg_num)
793 error ("args to be formatted is not ...");
794 continue;
799 record_function_format (DECL_NAME (decl),
800 DECL_ASSEMBLER_NAME (decl),
801 format_type, format_num, first_arg_num);
802 break;
805 case A_FORMAT_ARG:
807 tree format_num_expr = TREE_VALUE (args);
808 int format_num, arg_num;
809 tree argument;
811 if (TREE_CODE (decl) != FUNCTION_DECL)
813 error_with_decl (decl,
814 "argument format specified for non-function `%s'");
815 continue;
818 /* Strip any conversions from the first arg number and verify it
819 is a constant. */
820 while (TREE_CODE (format_num_expr) == NOP_EXPR
821 || TREE_CODE (format_num_expr) == CONVERT_EXPR
822 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
823 format_num_expr = TREE_OPERAND (format_num_expr, 0);
825 if (TREE_CODE (format_num_expr) != INTEGER_CST)
827 error ("format string has non-constant operand number");
828 continue;
831 format_num = TREE_INT_CST_LOW (format_num_expr);
833 /* If a parameter list is specified, verify that the format_num
834 argument is actually a string, in case the format attribute
835 is in error. */
836 argument = TYPE_ARG_TYPES (type);
837 if (argument)
839 for (arg_num = 1; ; ++arg_num)
841 if (argument == 0 || arg_num == format_num)
842 break;
843 argument = TREE_CHAIN (argument);
845 if (! argument
846 || TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE
847 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (argument)))
848 != char_type_node))
850 error ("format string arg not a string type");
851 continue;
855 if (TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) != POINTER_TYPE
856 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (TREE_TYPE (decl))))
857 != char_type_node))
859 error ("function does not return string type");
860 continue;
863 record_international_format (DECL_NAME (decl),
864 DECL_ASSEMBLER_NAME (decl),
865 format_num);
866 break;
869 case A_WEAK:
870 declare_weak (decl);
871 break;
873 case A_ALIAS:
874 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
875 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
876 error_with_decl (decl,
877 "`%s' defined both normally and as an alias");
878 else if (decl_function_context (decl) == 0)
880 tree id;
882 id = TREE_VALUE (args);
883 if (TREE_CODE (id) != STRING_CST)
885 error ("alias arg not a string");
886 break;
888 id = get_identifier (TREE_STRING_POINTER (id));
890 if (TREE_CODE (decl) == FUNCTION_DECL)
891 DECL_INITIAL (decl) = error_mark_node;
892 else
893 DECL_EXTERNAL (decl) = 0;
894 assemble_alias (decl, id);
896 else
897 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
898 break;
900 case A_NO_CHECK_MEMORY_USAGE:
901 if (TREE_CODE (decl) != FUNCTION_DECL)
903 error_with_decl (decl,
904 "`%s' attribute applies only to functions",
905 IDENTIFIER_POINTER (name));
907 else if (DECL_INITIAL (decl))
909 error_with_decl (decl,
910 "can't set `%s' attribute after definition",
911 IDENTIFIER_POINTER (name));
913 else
914 DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
915 break;
917 case A_INIT_PRIORITY:
919 tree initp_expr = (args ? TREE_VALUE (args): NULL_TREE);
920 int pri;
922 if (initp_expr)
923 STRIP_NOPS (initp_expr);
925 if (!initp_expr || TREE_CODE (initp_expr) != INTEGER_CST)
927 error ("requested init_priority is not an integer constant");
928 continue;
931 pri = TREE_INT_CST_LOW (initp_expr);
933 if (is_type || TREE_CODE (decl) != VAR_DECL
934 || ! TREE_STATIC (decl)
935 || DECL_EXTERNAL (decl)
936 || (TREE_CODE (TREE_TYPE (decl)) != RECORD_TYPE
937 && TREE_CODE (TREE_TYPE (decl)) != UNION_TYPE)
938 /* Static objects in functions are initialized the
939 first time control passes through that
940 function. This is not precise enough to pin down an
941 init_priority value, so don't allow it. */
942 || current_function_decl)
944 error ("can only use init_priority attribute on file-scope definitions of objects of class type");
945 continue;
948 if (pri > MAX_INIT_PRIORITY || pri <= 0)
950 error ("requested init_priority is out of range");
951 continue;
954 /* Check for init_priorities that are reserved for
955 language and runtime support implementations.*/
956 if (pri <= MAX_RESERVED_INIT_PRIORITY)
958 warning
959 ("requested init_priority is reserved for internal use");
962 static_aggregates_initp
963 = perm_tree_cons (initp_expr, decl, static_aggregates_initp);
964 break;
967 case A_NO_INSTRUMENT_FUNCTION:
968 if (TREE_CODE (decl) != FUNCTION_DECL)
970 error_with_decl (decl,
971 "`%s' attribute applies only to functions",
972 IDENTIFIER_POINTER (name));
974 else if (DECL_INITIAL (decl))
976 error_with_decl (decl,
977 "can't set `%s' attribute after definition",
978 IDENTIFIER_POINTER (name));
980 else
981 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
982 break;
987 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
988 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
990 The head of the declspec list is stored in DECLSPECS.
991 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
993 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
994 the list elements. We drop the containing TREE_LIST nodes and link the
995 resulting attributes together the way decl_attributes expects them. */
997 void
998 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
999 tree specs_attrs;
1000 tree *declspecs, *prefix_attributes;
1002 tree t, s, a, next, specs, attrs;
1004 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1005 if (specs_attrs != NULL_TREE
1006 && TREE_CODE (specs_attrs) != TREE_LIST)
1008 *declspecs = specs_attrs;
1009 *prefix_attributes = NULL_TREE;
1010 return;
1013 /* Remember to keep the lists in the same order, element-wise. */
1015 specs = s = NULL_TREE;
1016 attrs = a = NULL_TREE;
1017 for (t = specs_attrs; t; t = next)
1019 next = TREE_CHAIN (t);
1020 /* Declspecs have a non-NULL TREE_VALUE. */
1021 if (TREE_VALUE (t) != NULL_TREE)
1023 if (specs == NULL_TREE)
1024 specs = s = t;
1025 else
1027 TREE_CHAIN (s) = t;
1028 s = t;
1031 else
1033 if (attrs == NULL_TREE)
1034 attrs = a = TREE_PURPOSE (t);
1035 else
1037 TREE_CHAIN (a) = TREE_PURPOSE (t);
1038 a = TREE_PURPOSE (t);
1040 /* More attrs can be linked here, move A to the end. */
1041 while (TREE_CHAIN (a) != NULL_TREE)
1042 a = TREE_CHAIN (a);
1046 /* Terminate the lists. */
1047 if (s != NULL_TREE)
1048 TREE_CHAIN (s) = NULL_TREE;
1049 if (a != NULL_TREE)
1050 TREE_CHAIN (a) = NULL_TREE;
1052 /* All done. */
1053 *declspecs = specs;
1054 *prefix_attributes = attrs;
1057 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1058 This function is used by the parser when a rule will accept attributes
1059 in a particular position, but we don't want to support that just yet.
1061 A warning is issued for every ignored attribute. */
1063 tree
1064 strip_attrs (specs_attrs)
1065 tree specs_attrs;
1067 tree specs, attrs;
1069 split_specs_attrs (specs_attrs, &specs, &attrs);
1071 while (attrs)
1073 warning ("`%s' attribute ignored",
1074 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1075 attrs = TREE_CHAIN (attrs);
1078 return specs;
1081 /* Check a printf/fprintf/sprintf/scanf/fscanf/sscanf format against
1082 a parameter list. */
1084 #define T_I &integer_type_node
1085 #define T_L &long_integer_type_node
1086 #define T_LL &long_long_integer_type_node
1087 #define T_S &short_integer_type_node
1088 #define T_UI &unsigned_type_node
1089 #define T_UL &long_unsigned_type_node
1090 #define T_ULL &long_long_unsigned_type_node
1091 #define T_US &short_unsigned_type_node
1092 #define T_F &float_type_node
1093 #define T_D &double_type_node
1094 #define T_LD &long_double_type_node
1095 #define T_C &char_type_node
1096 #define T_UC &unsigned_char_type_node
1097 #define T_V &void_type_node
1098 #define T_W &wchar_type_node
1099 #define T_ST &sizetype
1101 typedef struct {
1102 const char *format_chars;
1103 int pointer_count;
1104 /* Type of argument if no length modifier is used. */
1105 tree *nolen;
1106 /* Type of argument if length modifier for shortening to byte is used.
1107 If NULL, then this modifier is not allowed. */
1108 tree *hhlen;
1109 /* Type of argument if length modifier for shortening is used.
1110 If NULL, then this modifier is not allowed. */
1111 tree *hlen;
1112 /* Type of argument if length modifier `l' is used.
1113 If NULL, then this modifier is not allowed. */
1114 tree *llen;
1115 /* Type of argument if length modifier `q' or `ll' is used.
1116 If NULL, then this modifier is not allowed. */
1117 tree *qlen;
1118 /* Type of argument if length modifier `L' is used.
1119 If NULL, then this modifier is not allowed. */
1120 tree *bigllen;
1121 /* Type of argument if length modifier `Z' is used.
1122 If NULL, then this modifier is not allowed. */
1123 tree *zlen;
1124 /* List of other modifier characters allowed with these options. */
1125 const char *flag_chars;
1126 } format_char_info;
1128 static format_char_info print_char_table[] = {
1129 { "di", 0, T_I, T_I, T_I, T_L, T_LL, T_LL, T_ST, "-wp0 +" },
1130 { "oxX", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0#" },
1131 { "u", 0, T_UI, T_UI, T_UI, T_UL, T_ULL, T_ULL, T_ST, "-wp0" },
1132 /* A GNU extension. */
1133 { "m", 0, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1134 { "feEgGaA", 0, T_D, NULL, NULL, NULL, NULL, T_LD, NULL, "-wp0 +#" },
1135 { "c", 0, T_I, NULL, NULL, T_W, NULL, NULL, NULL, "-w" },
1136 { "C", 0, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1137 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "-wp" },
1138 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "-wp" },
1139 { "p", 1, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "-w" },
1140 { "n", 1, T_I, NULL, T_S, T_L, T_LL, NULL, NULL, "" },
1141 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1144 static format_char_info scan_char_table[] = {
1145 { "di", 1, T_I, T_C, T_S, T_L, T_LL, T_LL, NULL, "*" },
1146 { "ouxX", 1, T_UI, T_UC, T_US, T_UL, T_ULL, T_ULL, NULL, "*" },
1147 { "efgEGaA", 1, T_F, NULL, NULL, T_D, NULL, T_LD, NULL, "*" },
1148 { "c", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*" },
1149 { "s", 1, T_C, NULL, NULL, T_W, NULL, NULL, NULL, "*a" },
1150 { "[", 1, T_C, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1151 { "C", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1152 { "S", 1, T_W, NULL, NULL, NULL, NULL, NULL, NULL, "*a" },
1153 { "p", 2, T_V, NULL, NULL, NULL, NULL, NULL, NULL, "*" },
1154 { "n", 1, T_I, T_C, T_S, T_L, T_LL, NULL, NULL, "" },
1155 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1158 /* Handle format characters recognized by glibc's strftime.c.
1159 '2' - MUST do years as only two digits
1160 '3' - MAY do years as only two digits (depending on locale)
1161 'E' - E modifier is acceptable
1162 'O' - O modifier is acceptable to Standard C
1163 'o' - O modifier is acceptable as a GNU extension
1164 'G' - other GNU extensions */
1166 static format_char_info time_char_table[] = {
1167 { "y", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2EO-_0w" },
1168 { "D", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2" },
1169 { "g", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "2O-_0w" },
1170 { "cx", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "3E" },
1171 { "%RTXnrt", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "" },
1172 { "P", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "G" },
1173 { "HIMSUWdemw", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Ow" },
1174 { "Vju", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0Oow" },
1175 { "Gklsz", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0OGw" },
1176 { "ABZa", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^#" },
1177 { "p", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "#" },
1178 { "bh", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "^" },
1179 { "CY", 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "-_0EOw" },
1180 { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
1183 typedef struct function_format_info
1185 struct function_format_info *next; /* next structure on the list */
1186 tree name; /* identifier such as "printf" */
1187 tree assembler_name; /* optional mangled identifier (for C++) */
1188 enum format_type format_type; /* type of format (printf, scanf, etc.) */
1189 int format_num; /* number of format argument */
1190 int first_arg_num; /* number of first arg (zero for varargs) */
1191 } function_format_info;
1193 static function_format_info *function_format_list = NULL;
1195 typedef struct international_format_info
1197 struct international_format_info *next; /* next structure on the list */
1198 tree name; /* identifier such as "gettext" */
1199 tree assembler_name; /* optional mangled identifier (for C++) */
1200 int format_num; /* number of format argument */
1201 } international_format_info;
1203 static international_format_info *international_format_list = NULL;
1205 static void check_format_info PROTO((function_format_info *, tree));
1207 /* Initialize the table of functions to perform format checking on.
1208 The ANSI functions are always checked (whether <stdio.h> is
1209 included or not), since it is common to call printf without
1210 including <stdio.h>. There shouldn't be a problem with this,
1211 since ANSI reserves these function names whether you include the
1212 header file or not. In any case, the checking is harmless.
1214 Also initialize the name of function that modify the format string for
1215 internationalization purposes. */
1217 void
1218 init_function_format_info ()
1220 record_function_format (get_identifier ("printf"), NULL_TREE,
1221 printf_format_type, 1, 2);
1222 record_function_format (get_identifier ("fprintf"), NULL_TREE,
1223 printf_format_type, 2, 3);
1224 record_function_format (get_identifier ("sprintf"), NULL_TREE,
1225 printf_format_type, 2, 3);
1226 record_function_format (get_identifier ("scanf"), NULL_TREE,
1227 scanf_format_type, 1, 2);
1228 record_function_format (get_identifier ("fscanf"), NULL_TREE,
1229 scanf_format_type, 2, 3);
1230 record_function_format (get_identifier ("sscanf"), NULL_TREE,
1231 scanf_format_type, 2, 3);
1232 record_function_format (get_identifier ("vprintf"), NULL_TREE,
1233 printf_format_type, 1, 0);
1234 record_function_format (get_identifier ("vfprintf"), NULL_TREE,
1235 printf_format_type, 2, 0);
1236 record_function_format (get_identifier ("vsprintf"), NULL_TREE,
1237 printf_format_type, 2, 0);
1238 record_function_format (get_identifier ("strftime"), NULL_TREE,
1239 strftime_format_type, 3, 0);
1241 record_international_format (get_identifier ("gettext"), NULL_TREE, 1);
1242 record_international_format (get_identifier ("dgettext"), NULL_TREE, 2);
1243 record_international_format (get_identifier ("dcgettext"), NULL_TREE, 2);
1246 /* Record information for argument format checking. FUNCTION_IDENT is
1247 the identifier node for the name of the function to check (its decl
1248 need not exist yet).
1249 FORMAT_TYPE specifies the type of format checking. FORMAT_NUM is the number
1250 of the argument which is the format control string (starting from 1).
1251 FIRST_ARG_NUM is the number of the first actual argument to check
1252 against the format string, or zero if no checking is not be done
1253 (e.g. for varargs such as vfprintf). */
1255 static void
1256 record_function_format (name, assembler_name, format_type,
1257 format_num, first_arg_num)
1258 tree name;
1259 tree assembler_name;
1260 enum format_type format_type;
1261 int format_num;
1262 int first_arg_num;
1264 function_format_info *info;
1266 /* Re-use existing structure if it's there. */
1268 for (info = function_format_list; info; info = info->next)
1270 if (info->name == name && info->assembler_name == assembler_name)
1271 break;
1273 if (! info)
1275 info = (function_format_info *) xmalloc (sizeof (function_format_info));
1276 info->next = function_format_list;
1277 function_format_list = info;
1279 info->name = name;
1280 info->assembler_name = assembler_name;
1283 info->format_type = format_type;
1284 info->format_num = format_num;
1285 info->first_arg_num = first_arg_num;
1288 /* Record information for the names of function that modify the format
1289 argument to format functions. FUNCTION_IDENT is the identifier node for
1290 the name of the function (its decl need not exist yet) and FORMAT_NUM is
1291 the number of the argument which is the format control string (starting
1292 from 1). */
1294 static void
1295 record_international_format (name, assembler_name, format_num)
1296 tree name;
1297 tree assembler_name;
1298 int format_num;
1300 international_format_info *info;
1302 /* Re-use existing structure if it's there. */
1304 for (info = international_format_list; info; info = info->next)
1306 if (info->name == name && info->assembler_name == assembler_name)
1307 break;
1310 if (! info)
1312 info
1313 = (international_format_info *)
1314 xmalloc (sizeof (international_format_info));
1315 info->next = international_format_list;
1316 international_format_list = info;
1318 info->name = name;
1319 info->assembler_name = assembler_name;
1322 info->format_num = format_num;
1325 static void
1326 tfaff ()
1328 warning ("too few arguments for format");
1331 /* Check the argument list of a call to printf, scanf, etc.
1332 NAME is the function identifier.
1333 ASSEMBLER_NAME is the function's assembler identifier.
1334 (Either NAME or ASSEMBLER_NAME, but not both, may be NULL_TREE.)
1335 PARAMS is the list of argument values. */
1337 void
1338 check_function_format (name, assembler_name, params)
1339 tree name;
1340 tree assembler_name;
1341 tree params;
1343 function_format_info *info;
1345 /* See if this function is a format function. */
1346 for (info = function_format_list; info; info = info->next)
1348 if (info->assembler_name
1349 ? (info->assembler_name == assembler_name)
1350 : (info->name == name))
1352 /* Yup; check it. */
1353 check_format_info (info, params);
1354 break;
1359 /* Check the argument list of a call to printf, scanf, etc.
1360 INFO points to the function_format_info structure.
1361 PARAMS is the list of argument values. */
1363 static void
1364 check_format_info (info, params)
1365 function_format_info *info;
1366 tree params;
1368 int i;
1369 int arg_num;
1370 int suppressed, wide, precise;
1371 int length_char = 0;
1372 int format_char;
1373 int format_length;
1374 tree format_tree;
1375 tree cur_param;
1376 tree cur_type;
1377 tree wanted_type;
1378 tree first_fillin_param;
1379 const char *format_chars;
1380 format_char_info *fci = NULL;
1381 char flag_chars[8];
1382 int has_operand_number = 0;
1384 /* Skip to format argument. If the argument isn't available, there's
1385 no work for us to do; prototype checking will catch the problem. */
1386 for (arg_num = 1; ; ++arg_num)
1388 if (params == 0)
1389 return;
1390 if (arg_num == info->format_num)
1391 break;
1392 params = TREE_CHAIN (params);
1394 format_tree = TREE_VALUE (params);
1395 params = TREE_CHAIN (params);
1396 if (format_tree == 0)
1397 return;
1399 /* We can only check the format if it's a string constant. */
1400 while (TREE_CODE (format_tree) == NOP_EXPR)
1401 format_tree = TREE_OPERAND (format_tree, 0); /* strip coercion */
1403 if (TREE_CODE (format_tree) == CALL_EXPR
1404 && TREE_CODE (TREE_OPERAND (format_tree, 0)) == ADDR_EXPR
1405 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0))
1406 == FUNCTION_DECL))
1408 tree function = TREE_OPERAND (TREE_OPERAND (format_tree, 0), 0);
1410 /* See if this is a call to a known internationalization function
1411 that modifies the format arg. */
1412 international_format_info *info;
1414 for (info = international_format_list; info; info = info->next)
1415 if (info->assembler_name
1416 ? (info->assembler_name == DECL_ASSEMBLER_NAME (function))
1417 : (info->name == DECL_NAME (function)))
1419 tree inner_args;
1420 int i;
1422 for (inner_args = TREE_OPERAND (format_tree, 1), i = 1;
1423 inner_args != 0;
1424 inner_args = TREE_CHAIN (inner_args), i++)
1425 if (i == info->format_num)
1427 format_tree = TREE_VALUE (inner_args);
1429 while (TREE_CODE (format_tree) == NOP_EXPR)
1430 format_tree = TREE_OPERAND (format_tree, 0);
1435 if (integer_zerop (format_tree))
1437 warning ("null format string");
1438 return;
1440 if (TREE_CODE (format_tree) != ADDR_EXPR)
1441 return;
1442 format_tree = TREE_OPERAND (format_tree, 0);
1443 if (TREE_CODE (format_tree) != STRING_CST)
1444 return;
1445 format_chars = TREE_STRING_POINTER (format_tree);
1446 format_length = TREE_STRING_LENGTH (format_tree);
1447 if (format_length <= 1)
1448 warning ("zero-length format string");
1449 if (format_chars[--format_length] != 0)
1451 warning ("unterminated format string");
1452 return;
1454 /* Skip to first argument to check. */
1455 while (arg_num + 1 < info->first_arg_num)
1457 if (params == 0)
1458 return;
1459 params = TREE_CHAIN (params);
1460 ++arg_num;
1463 first_fillin_param = params;
1464 while (1)
1466 int aflag;
1467 if (*format_chars == 0)
1469 if (format_chars - TREE_STRING_POINTER (format_tree) != format_length)
1470 warning ("embedded `\\0' in format");
1471 if (info->first_arg_num != 0 && params != 0 && ! has_operand_number)
1472 warning ("too many arguments for format");
1473 return;
1475 if (*format_chars++ != '%')
1476 continue;
1477 if (*format_chars == 0)
1479 warning ("spurious trailing `%%' in format");
1480 continue;
1482 if (*format_chars == '%')
1484 ++format_chars;
1485 continue;
1487 flag_chars[0] = 0;
1488 suppressed = wide = precise = FALSE;
1489 if (info->format_type == scanf_format_type)
1491 suppressed = *format_chars == '*';
1492 if (suppressed)
1493 ++format_chars;
1494 while (ISDIGIT (*format_chars))
1495 ++format_chars;
1497 else if (info->format_type == strftime_format_type)
1499 while (*format_chars != 0 && index ("_-0^#", *format_chars) != 0)
1501 if (pedantic)
1502 warning ("ANSI C does not support the strftime `%c' flag",
1503 *format_chars);
1504 if (index (flag_chars, *format_chars) != 0)
1506 warning ("repeated `%c' flag in format",
1507 *format_chars);
1508 ++format_chars;
1510 else
1512 i = strlen (flag_chars);
1513 flag_chars[i++] = *format_chars++;
1514 flag_chars[i] = 0;
1517 while (ISDIGIT ((unsigned char) *format_chars))
1519 wide = TRUE;
1520 ++format_chars;
1522 if (wide && pedantic)
1523 warning ("ANSI C does not support strftime format width");
1524 if (*format_chars == 'E' || *format_chars == 'O')
1526 i = strlen (flag_chars);
1527 flag_chars[i++] = *format_chars++;
1528 flag_chars[i] = 0;
1529 if (*format_chars == 'E' || *format_chars == 'O')
1531 warning ("multiple E/O modifiers in format");
1532 while (*format_chars == 'E' || *format_chars == 'O')
1533 ++format_chars;
1537 else if (info->format_type == printf_format_type)
1539 /* See if we have a number followed by a dollar sign. If we do,
1540 it is an operand number, so set PARAMS to that operand. */
1541 if (*format_chars >= '0' && *format_chars <= '9')
1543 const char *p = format_chars;
1545 while (*p >= '0' && *p++ <= '9')
1548 if (*p == '$')
1550 int opnum = atoi (format_chars);
1552 params = first_fillin_param;
1553 format_chars = p + 1;
1554 has_operand_number = 1;
1556 for (i = 1; i < opnum && params != 0; i++)
1557 params = TREE_CHAIN (params);
1559 if (opnum == 0 || params == 0)
1561 warning ("operand number out of range in format");
1562 return;
1567 while (*format_chars != 0 && index (" +#0-", *format_chars) != 0)
1569 if (index (flag_chars, *format_chars) != 0)
1570 warning ("repeated `%c' flag in format", *format_chars++);
1571 else
1573 i = strlen (flag_chars);
1574 flag_chars[i++] = *format_chars++;
1575 flag_chars[i] = 0;
1578 /* "If the space and + flags both appear,
1579 the space flag will be ignored." */
1580 if (index (flag_chars, ' ') != 0
1581 && index (flag_chars, '+') != 0)
1582 warning ("use of both ` ' and `+' flags in format");
1583 /* "If the 0 and - flags both appear,
1584 the 0 flag will be ignored." */
1585 if (index (flag_chars, '0') != 0
1586 && index (flag_chars, '-') != 0)
1587 warning ("use of both `0' and `-' flags in format");
1588 if (*format_chars == '*')
1590 wide = TRUE;
1591 /* "...a field width...may be indicated by an asterisk.
1592 In this case, an int argument supplies the field width..." */
1593 ++format_chars;
1594 if (params == 0)
1596 tfaff ();
1597 return;
1599 if (info->first_arg_num != 0)
1601 cur_param = TREE_VALUE (params);
1602 params = TREE_CHAIN (params);
1603 ++arg_num;
1604 /* size_t is generally not valid here.
1605 It will work on most machines, because size_t and int
1606 have the same mode. But might as well warn anyway,
1607 since it will fail on other machines. */
1608 if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1609 != integer_type_node)
1611 (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1612 != unsigned_type_node))
1613 warning ("field width is not type int (arg %d)", arg_num);
1616 else
1618 while (ISDIGIT (*format_chars))
1620 wide = TRUE;
1621 ++format_chars;
1624 if (*format_chars == '.')
1626 precise = TRUE;
1627 ++format_chars;
1628 if (*format_chars != '*' && !ISDIGIT (*format_chars))
1629 warning ("`.' not followed by `*' or digit in format");
1630 /* "...a...precision...may be indicated by an asterisk.
1631 In this case, an int argument supplies the...precision." */
1632 if (*format_chars == '*')
1634 if (info->first_arg_num != 0)
1636 ++format_chars;
1637 if (params == 0)
1639 tfaff ();
1640 return;
1642 cur_param = TREE_VALUE (params);
1643 params = TREE_CHAIN (params);
1644 ++arg_num;
1645 if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
1646 != integer_type_node)
1647 warning ("field width is not type int (arg %d)",
1648 arg_num);
1651 else
1653 while (ISDIGIT (*format_chars))
1654 ++format_chars;
1659 aflag = 0;
1661 if (info->format_type != strftime_format_type)
1663 if (*format_chars == 'h' || *format_chars == 'l')
1664 length_char = *format_chars++;
1665 else if (*format_chars == 'q' || *format_chars == 'L')
1667 length_char = *format_chars++;
1668 if (pedantic)
1669 warning ("ANSI C does not support the `%c' length modifier",
1670 length_char);
1672 else if (*format_chars == 'Z')
1674 length_char = *format_chars++;
1675 if (pedantic)
1676 warning ("ANSI C does not support the `Z' length modifier");
1678 else
1679 length_char = 0;
1680 if (length_char == 'l' && *format_chars == 'l')
1682 length_char = 'q', format_chars++;
1683 /* FIXME: Is allowed in ISO C 9x. */
1684 if (pedantic)
1685 warning ("ANSI C does not support the `ll' length modifier");
1687 else if (length_char == 'h' && *format_chars == 'h')
1689 length_char = 'H', format_chars++;
1690 /* FIXME: Is allowed in ISO C 9x. */
1691 if (pedantic)
1692 warning ("ANSI C does not support the `hh' length modifier");
1694 if (*format_chars == 'a' && info->format_type == scanf_format_type)
1696 if (format_chars[1] == 's' || format_chars[1] == 'S'
1697 || format_chars[1] == '[')
1699 /* `a' is used as a flag. */
1700 aflag = 1;
1701 format_chars++;
1704 if (suppressed && length_char != 0)
1705 warning ("use of `*' and `%c' together in format", length_char);
1707 format_char = *format_chars;
1708 if (format_char == 0
1709 || (info->format_type != strftime_format_type && format_char == '%'))
1711 warning ("conversion lacks type at end of format");
1712 continue;
1714 /* The m, C, and S formats are GNU extensions. */
1715 if (pedantic && info->format_type != strftime_format_type
1716 && (format_char == 'm' || format_char == 'C' || format_char == 'S'))
1717 warning ("ANSI C does not support the `%c' format", format_char);
1718 /* ??? The a and A formats are C9X extensions, and should be allowed
1719 when a C9X option is added. */
1720 if (pedantic && info->format_type != strftime_format_type
1721 && (format_char == 'a' || format_char == 'A'))
1722 warning ("ANSI C does not support the `%c' format", format_char);
1723 format_chars++;
1724 switch (info->format_type)
1726 case printf_format_type:
1727 fci = print_char_table;
1728 break;
1729 case scanf_format_type:
1730 fci = scan_char_table;
1731 break;
1732 case strftime_format_type:
1733 fci = time_char_table;
1734 break;
1735 default:
1736 abort ();
1738 while (fci->format_chars != 0
1739 && index (fci->format_chars, format_char) == 0)
1740 ++fci;
1741 if (fci->format_chars == 0)
1743 if (format_char >= 040 && format_char < 0177)
1744 warning ("unknown conversion type character `%c' in format",
1745 format_char);
1746 else
1747 warning ("unknown conversion type character 0x%x in format",
1748 format_char);
1749 continue;
1751 if (pedantic)
1753 if (index (fci->flag_chars, 'G') != 0)
1754 warning ("ANSI C does not support `%%%c'", format_char);
1755 if (index (fci->flag_chars, 'o') != 0
1756 && index (flag_chars, 'O') != 0)
1757 warning ("ANSI C does not support `%%O%c'", format_char);
1759 if (wide && index (fci->flag_chars, 'w') == 0)
1760 warning ("width used with `%c' format", format_char);
1761 if (index (fci->flag_chars, '2') != 0)
1762 warning ("`%%%c' yields only last 2 digits of year", format_char);
1763 else if (index (fci->flag_chars, '3') != 0)
1764 warning ("`%%%c' yields only last 2 digits of year in some locales",
1765 format_char);
1766 if (precise && index (fci->flag_chars, 'p') == 0)
1767 warning ("precision used with `%c' format", format_char);
1768 if (aflag && index (fci->flag_chars, 'a') == 0)
1770 warning ("`a' flag used with `%c' format", format_char);
1771 /* To simplify the following code. */
1772 aflag = 0;
1774 /* The a flag is a GNU extension. */
1775 else if (pedantic && aflag)
1776 warning ("ANSI C does not support the `a' flag");
1777 if (info->format_type == scanf_format_type && format_char == '[')
1779 /* Skip over scan set, in case it happens to have '%' in it. */
1780 if (*format_chars == '^')
1781 ++format_chars;
1782 /* Find closing bracket; if one is hit immediately, then
1783 it's part of the scan set rather than a terminator. */
1784 if (*format_chars == ']')
1785 ++format_chars;
1786 while (*format_chars && *format_chars != ']')
1787 ++format_chars;
1788 if (*format_chars != ']')
1789 /* The end of the format string was reached. */
1790 warning ("no closing `]' for `%%[' format");
1792 if (suppressed)
1794 if (index (fci->flag_chars, '*') == 0)
1795 warning ("suppression of `%c' conversion in format", format_char);
1796 continue;
1798 for (i = 0; flag_chars[i] != 0; ++i)
1800 if (index (fci->flag_chars, flag_chars[i]) == 0)
1801 warning ("flag `%c' used with type `%c'",
1802 flag_chars[i], format_char);
1804 if (info->format_type == strftime_format_type)
1805 continue;
1806 if (precise && index (flag_chars, '0') != 0
1807 && (format_char == 'd' || format_char == 'i'
1808 || format_char == 'o' || format_char == 'u'
1809 || format_char == 'x' || format_char == 'X'))
1810 warning ("`0' flag ignored with precision specifier and `%c' format",
1811 format_char);
1812 switch (length_char)
1814 default: wanted_type = fci->nolen ? *(fci->nolen) : 0; break;
1815 case 'H': wanted_type = fci->hhlen ? *(fci->hhlen) : 0; break;
1816 case 'h': wanted_type = fci->hlen ? *(fci->hlen) : 0; break;
1817 case 'l': wanted_type = fci->llen ? *(fci->llen) : 0; break;
1818 case 'q': wanted_type = fci->qlen ? *(fci->qlen) : 0; break;
1819 case 'L': wanted_type = fci->bigllen ? *(fci->bigllen) : 0; break;
1820 case 'Z': wanted_type = fci->zlen ? *fci->zlen : 0; break;
1822 if (wanted_type == 0)
1823 warning ("use of `%c' length character with `%c' type character",
1824 length_char, format_char);
1826 /* Finally. . .check type of argument against desired type! */
1827 if (info->first_arg_num == 0)
1828 continue;
1829 if (fci->pointer_count == 0 && wanted_type == void_type_node)
1830 /* This specifier takes no argument. */
1831 continue;
1832 if (params == 0)
1834 tfaff ();
1835 return;
1837 cur_param = TREE_VALUE (params);
1838 params = TREE_CHAIN (params);
1839 ++arg_num;
1840 cur_type = TREE_TYPE (cur_param);
1842 STRIP_NOPS (cur_param);
1844 /* Check the types of any additional pointer arguments
1845 that precede the "real" argument. */
1846 for (i = 0; i < fci->pointer_count + aflag; ++i)
1848 if (TREE_CODE (cur_type) == POINTER_TYPE)
1850 cur_type = TREE_TYPE (cur_type);
1852 if (cur_param != 0 && TREE_CODE (cur_param) == ADDR_EXPR)
1853 cur_param = TREE_OPERAND (cur_param, 0);
1854 else
1855 cur_param = 0;
1857 continue;
1859 if (TREE_CODE (cur_type) != ERROR_MARK)
1860 warning ((fci->pointer_count + aflag == 1
1861 ? "format argument is not a pointer (arg %d)"
1862 : "format argument is not a pointer to a pointer (arg %d)"),
1863 arg_num);
1864 break;
1867 /* See if this is an attempt to write into a const type with
1868 scanf or with printf "%n". */
1869 if ((info->format_type == scanf_format_type
1870 || (info->format_type == printf_format_type
1871 && format_char == 'n'))
1872 && i == fci->pointer_count + aflag
1873 && wanted_type != 0
1874 && TREE_CODE (cur_type) != ERROR_MARK
1875 && (TYPE_READONLY (cur_type)
1876 || (cur_param != 0
1877 && (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'c'
1878 || (TREE_CODE_CLASS (TREE_CODE (cur_param)) == 'd'
1879 && TREE_READONLY (cur_param))))))
1880 warning ("writing into constant object (arg %d)", arg_num);
1882 /* Check the type of the "real" argument, if there's a type we want. */
1883 if (i == fci->pointer_count + aflag && wanted_type != 0
1884 && TREE_CODE (cur_type) != ERROR_MARK
1885 && wanted_type != TYPE_MAIN_VARIANT (cur_type)
1886 /* If we want `void *', allow any pointer type.
1887 (Anything else would already have got a warning.) */
1888 && ! (wanted_type == void_type_node
1889 && fci->pointer_count > 0)
1890 /* Don't warn about differences merely in signedness. */
1891 && !(TREE_CODE (wanted_type) == INTEGER_TYPE
1892 && TREE_CODE (TYPE_MAIN_VARIANT (cur_type)) == INTEGER_TYPE
1893 && (TREE_UNSIGNED (wanted_type)
1894 ? wanted_type == (cur_type = unsigned_type (cur_type))
1895 : wanted_type == (cur_type = signed_type (cur_type))))
1896 /* Likewise, "signed char", "unsigned char" and "char" are
1897 equivalent but the above test won't consider them equivalent. */
1898 && ! (wanted_type == char_type_node
1899 && (TYPE_MAIN_VARIANT (cur_type) == signed_char_type_node
1900 || TYPE_MAIN_VARIANT (cur_type) == unsigned_char_type_node)))
1902 register const char *this;
1903 register const char *that;
1905 this = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (wanted_type)));
1906 that = 0;
1907 if (TREE_CODE (cur_type) != ERROR_MARK
1908 && TYPE_NAME (cur_type) != 0
1909 && TREE_CODE (cur_type) != INTEGER_TYPE
1910 && !(TREE_CODE (cur_type) == POINTER_TYPE
1911 && TREE_CODE (TREE_TYPE (cur_type)) == INTEGER_TYPE))
1913 if (TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL
1914 && DECL_NAME (TYPE_NAME (cur_type)) != 0)
1915 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1916 else
1917 that = IDENTIFIER_POINTER (TYPE_NAME (cur_type));
1920 /* A nameless type can't possibly match what the format wants.
1921 So there will be a warning for it.
1922 Make up a string to describe vaguely what it is. */
1923 if (that == 0)
1925 if (TREE_CODE (cur_type) == POINTER_TYPE)
1926 that = "pointer";
1927 else
1928 that = "different type";
1931 /* Make the warning better in case of mismatch of int vs long. */
1932 if (TREE_CODE (cur_type) == INTEGER_TYPE
1933 && TREE_CODE (wanted_type) == INTEGER_TYPE
1934 && TYPE_PRECISION (cur_type) == TYPE_PRECISION (wanted_type)
1935 && TYPE_NAME (cur_type) != 0
1936 && TREE_CODE (TYPE_NAME (cur_type)) == TYPE_DECL)
1937 that = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (cur_type)));
1939 if (strcmp (this, that) != 0)
1940 warning ("%s format, %s arg (arg %d)", this, that, arg_num);
1945 /* Print a warning if a constant expression had overflow in folding.
1946 Invoke this function on every expression that the language
1947 requires to be a constant expression.
1948 Note the ANSI C standard says it is erroneous for a
1949 constant expression to overflow. */
1951 void
1952 constant_expression_warning (value)
1953 tree value;
1955 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1956 || TREE_CODE (value) == COMPLEX_CST)
1957 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1958 pedwarn ("overflow in constant expression");
1961 /* Print a warning if an expression had overflow in folding.
1962 Invoke this function on every expression that
1963 (1) appears in the source code, and
1964 (2) might be a constant expression that overflowed, and
1965 (3) is not already checked by convert_and_check;
1966 however, do not invoke this function on operands of explicit casts. */
1968 void
1969 overflow_warning (value)
1970 tree value;
1972 if ((TREE_CODE (value) == INTEGER_CST
1973 || (TREE_CODE (value) == COMPLEX_CST
1974 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1975 && TREE_OVERFLOW (value))
1977 TREE_OVERFLOW (value) = 0;
1978 if (skip_evaluation == 0)
1979 warning ("integer overflow in expression");
1981 else if ((TREE_CODE (value) == REAL_CST
1982 || (TREE_CODE (value) == COMPLEX_CST
1983 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1984 && TREE_OVERFLOW (value))
1986 TREE_OVERFLOW (value) = 0;
1987 if (skip_evaluation == 0)
1988 warning ("floating point overflow in expression");
1992 /* Print a warning if a large constant is truncated to unsigned,
1993 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1994 Invoke this function on every expression that might be implicitly
1995 converted to an unsigned type. */
1997 void
1998 unsigned_conversion_warning (result, operand)
1999 tree result, operand;
2001 if (TREE_CODE (operand) == INTEGER_CST
2002 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
2003 && TREE_UNSIGNED (TREE_TYPE (result))
2004 && skip_evaluation == 0
2005 && !int_fits_type_p (operand, TREE_TYPE (result)))
2007 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
2008 /* This detects cases like converting -129 or 256 to unsigned char. */
2009 warning ("large integer implicitly truncated to unsigned type");
2010 else if (warn_conversion)
2011 warning ("negative integer implicitly converted to unsigned type");
2015 /* Convert EXPR to TYPE, warning about conversion problems with constants.
2016 Invoke this function on every expression that is converted implicitly,
2017 i.e. because of language rules and not because of an explicit cast. */
2019 tree
2020 convert_and_check (type, expr)
2021 tree type, expr;
2023 tree t = convert (type, expr);
2024 if (TREE_CODE (t) == INTEGER_CST)
2026 if (TREE_OVERFLOW (t))
2028 TREE_OVERFLOW (t) = 0;
2030 /* Do not diagnose overflow in a constant expression merely
2031 because a conversion overflowed. */
2032 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
2034 /* No warning for converting 0x80000000 to int. */
2035 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
2036 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
2037 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
2038 /* If EXPR fits in the unsigned version of TYPE,
2039 don't warn unless pedantic. */
2040 if ((pedantic
2041 || TREE_UNSIGNED (type)
2042 || ! int_fits_type_p (expr, unsigned_type (type)))
2043 && skip_evaluation == 0)
2044 warning ("overflow in implicit constant conversion");
2046 else
2047 unsigned_conversion_warning (t, expr);
2049 return t;
2052 void
2053 c_expand_expr_stmt (expr)
2054 tree expr;
2056 /* Do default conversion if safe and possibly important,
2057 in case within ({...}). */
2058 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
2059 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
2060 expr = default_conversion (expr);
2062 if (TREE_TYPE (expr) != error_mark_node
2063 && TYPE_SIZE (TREE_TYPE (expr)) == 0
2064 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
2065 error ("expression statement has incomplete type");
2067 expand_expr_stmt (expr);
2070 /* Validate the expression after `case' and apply default promotions. */
2072 tree
2073 check_case_value (value)
2074 tree value;
2076 if (value == NULL_TREE)
2077 return value;
2079 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
2080 STRIP_TYPE_NOPS (value);
2082 if (TREE_CODE (value) != INTEGER_CST
2083 && value != error_mark_node)
2085 error ("case label does not reduce to an integer constant");
2086 value = error_mark_node;
2088 else
2089 /* Promote char or short to int. */
2090 value = default_conversion (value);
2092 constant_expression_warning (value);
2094 return value;
2097 /* Return an integer type with BITS bits of precision,
2098 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2100 tree
2101 type_for_size (bits, unsignedp)
2102 unsigned bits;
2103 int unsignedp;
2105 if (bits == TYPE_PRECISION (integer_type_node))
2106 return unsignedp ? unsigned_type_node : integer_type_node;
2108 if (bits == TYPE_PRECISION (signed_char_type_node))
2109 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2111 if (bits == TYPE_PRECISION (short_integer_type_node))
2112 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2114 if (bits == TYPE_PRECISION (long_integer_type_node))
2115 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2117 if (bits == TYPE_PRECISION (long_long_integer_type_node))
2118 return (unsignedp ? long_long_unsigned_type_node
2119 : long_long_integer_type_node);
2121 if (bits <= TYPE_PRECISION (intQI_type_node))
2122 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2124 if (bits <= TYPE_PRECISION (intHI_type_node))
2125 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2127 if (bits <= TYPE_PRECISION (intSI_type_node))
2128 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2130 if (bits <= TYPE_PRECISION (intDI_type_node))
2131 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2133 return 0;
2136 /* Return a data type that has machine mode MODE.
2137 If the mode is an integer,
2138 then UNSIGNEDP selects between signed and unsigned types. */
2140 tree
2141 type_for_mode (mode, unsignedp)
2142 enum machine_mode mode;
2143 int unsignedp;
2145 if (mode == TYPE_MODE (integer_type_node))
2146 return unsignedp ? unsigned_type_node : integer_type_node;
2148 if (mode == TYPE_MODE (signed_char_type_node))
2149 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2151 if (mode == TYPE_MODE (short_integer_type_node))
2152 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2154 if (mode == TYPE_MODE (long_integer_type_node))
2155 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2157 if (mode == TYPE_MODE (long_long_integer_type_node))
2158 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2160 if (mode == TYPE_MODE (intQI_type_node))
2161 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2163 if (mode == TYPE_MODE (intHI_type_node))
2164 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2166 if (mode == TYPE_MODE (intSI_type_node))
2167 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2169 if (mode == TYPE_MODE (intDI_type_node))
2170 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2172 #if HOST_BITS_PER_WIDE_INT >= 64
2173 if (mode == TYPE_MODE (intTI_type_node))
2174 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2175 #endif
2177 if (mode == TYPE_MODE (float_type_node))
2178 return float_type_node;
2180 if (mode == TYPE_MODE (double_type_node))
2181 return double_type_node;
2183 if (mode == TYPE_MODE (long_double_type_node))
2184 return long_double_type_node;
2186 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2187 return build_pointer_type (char_type_node);
2189 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2190 return build_pointer_type (integer_type_node);
2192 return 0;
2195 /* Return the minimum number of bits needed to represent VALUE in a
2196 signed or unsigned type, UNSIGNEDP says which. */
2199 min_precision (value, unsignedp)
2200 tree value;
2201 int unsignedp;
2203 int log;
2205 /* If the value is negative, compute its negative minus 1. The latter
2206 adjustment is because the absolute value of the largest negative value
2207 is one larger than the largest positive value. This is equivalent to
2208 a bit-wise negation, so use that operation instead. */
2210 if (tree_int_cst_sgn (value) < 0)
2211 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2213 /* Return the number of bits needed, taking into account the fact
2214 that we need one more bit for a signed than unsigned type. */
2216 if (integer_zerop (value))
2217 log = 0;
2218 else if (TREE_INT_CST_HIGH (value) != 0)
2219 log = HOST_BITS_PER_WIDE_INT + floor_log2 (TREE_INT_CST_HIGH (value));
2220 else
2221 log = floor_log2 (TREE_INT_CST_LOW (value));
2223 return log + 1 + ! unsignedp;
2226 /* Print an error message for invalid operands to arith operation CODE.
2227 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2229 void
2230 binary_op_error (code)
2231 enum tree_code code;
2233 register const char *opname;
2235 switch (code)
2237 case NOP_EXPR:
2238 error ("invalid truth-value expression");
2239 return;
2241 case PLUS_EXPR:
2242 opname = "+"; break;
2243 case MINUS_EXPR:
2244 opname = "-"; break;
2245 case MULT_EXPR:
2246 opname = "*"; break;
2247 case MAX_EXPR:
2248 opname = "max"; break;
2249 case MIN_EXPR:
2250 opname = "min"; break;
2251 case EQ_EXPR:
2252 opname = "=="; break;
2253 case NE_EXPR:
2254 opname = "!="; break;
2255 case LE_EXPR:
2256 opname = "<="; break;
2257 case GE_EXPR:
2258 opname = ">="; break;
2259 case LT_EXPR:
2260 opname = "<"; break;
2261 case GT_EXPR:
2262 opname = ">"; break;
2263 case LSHIFT_EXPR:
2264 opname = "<<"; break;
2265 case RSHIFT_EXPR:
2266 opname = ">>"; break;
2267 case TRUNC_MOD_EXPR:
2268 case FLOOR_MOD_EXPR:
2269 opname = "%"; break;
2270 case TRUNC_DIV_EXPR:
2271 case FLOOR_DIV_EXPR:
2272 opname = "/"; break;
2273 case BIT_AND_EXPR:
2274 opname = "&"; break;
2275 case BIT_IOR_EXPR:
2276 opname = "|"; break;
2277 case TRUTH_ANDIF_EXPR:
2278 opname = "&&"; break;
2279 case TRUTH_ORIF_EXPR:
2280 opname = "||"; break;
2281 case BIT_XOR_EXPR:
2282 opname = "^"; break;
2283 case LROTATE_EXPR:
2284 case RROTATE_EXPR:
2285 opname = "rotate"; break;
2286 default:
2287 opname = "unknown"; break;
2289 error ("invalid operands to binary %s", opname);
2292 /* Subroutine of build_binary_op, used for comparison operations.
2293 See if the operands have both been converted from subword integer types
2294 and, if so, perhaps change them both back to their original type.
2295 This function is also responsible for converting the two operands
2296 to the proper common type for comparison.
2298 The arguments of this function are all pointers to local variables
2299 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2300 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2302 If this function returns nonzero, it means that the comparison has
2303 a constant value. What this function returns is an expression for
2304 that value. */
2306 tree
2307 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2308 tree *op0_ptr, *op1_ptr;
2309 tree *restype_ptr;
2310 enum tree_code *rescode_ptr;
2312 register tree type;
2313 tree op0 = *op0_ptr;
2314 tree op1 = *op1_ptr;
2315 int unsignedp0, unsignedp1;
2316 int real1, real2;
2317 tree primop0, primop1;
2318 enum tree_code code = *rescode_ptr;
2320 /* Throw away any conversions to wider types
2321 already present in the operands. */
2323 primop0 = get_narrower (op0, &unsignedp0);
2324 primop1 = get_narrower (op1, &unsignedp1);
2326 /* Handle the case that OP0 does not *contain* a conversion
2327 but it *requires* conversion to FINAL_TYPE. */
2329 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2330 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2331 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2332 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2334 /* If one of the operands must be floated, we cannot optimize. */
2335 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2336 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2338 /* If first arg is constant, swap the args (changing operation
2339 so value is preserved), for canonicalization. Don't do this if
2340 the second arg is 0. */
2342 if (TREE_CONSTANT (primop0)
2343 && ! integer_zerop (primop1) && ! real_zerop (primop1))
2345 register tree tem = primop0;
2346 register int temi = unsignedp0;
2347 primop0 = primop1;
2348 primop1 = tem;
2349 tem = op0;
2350 op0 = op1;
2351 op1 = tem;
2352 *op0_ptr = op0;
2353 *op1_ptr = op1;
2354 unsignedp0 = unsignedp1;
2355 unsignedp1 = temi;
2356 temi = real1;
2357 real1 = real2;
2358 real2 = temi;
2360 switch (code)
2362 case LT_EXPR:
2363 code = GT_EXPR;
2364 break;
2365 case GT_EXPR:
2366 code = LT_EXPR;
2367 break;
2368 case LE_EXPR:
2369 code = GE_EXPR;
2370 break;
2371 case GE_EXPR:
2372 code = LE_EXPR;
2373 break;
2374 default:
2375 break;
2377 *rescode_ptr = code;
2380 /* If comparing an integer against a constant more bits wide,
2381 maybe we can deduce a value of 1 or 0 independent of the data.
2382 Or else truncate the constant now
2383 rather than extend the variable at run time.
2385 This is only interesting if the constant is the wider arg.
2386 Also, it is not safe if the constant is unsigned and the
2387 variable arg is signed, since in this case the variable
2388 would be sign-extended and then regarded as unsigned.
2389 Our technique fails in this case because the lowest/highest
2390 possible unsigned results don't follow naturally from the
2391 lowest/highest possible values of the variable operand.
2392 For just EQ_EXPR and NE_EXPR there is another technique that
2393 could be used: see if the constant can be faithfully represented
2394 in the other operand's type, by truncating it and reextending it
2395 and see if that preserves the constant's value. */
2397 if (!real1 && !real2
2398 && TREE_CODE (primop1) == INTEGER_CST
2399 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2401 int min_gt, max_gt, min_lt, max_lt;
2402 tree maxval, minval;
2403 /* 1 if comparison is nominally unsigned. */
2404 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2405 tree val;
2407 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2409 /* If TYPE is an enumeration, then we need to get its min/max
2410 values from it's underlying integral type, not the enumerated
2411 type itself. */
2412 if (TREE_CODE (type) == ENUMERAL_TYPE)
2413 type = type_for_size (TYPE_PRECISION (type), unsignedp0);
2415 maxval = TYPE_MAX_VALUE (type);
2416 minval = TYPE_MIN_VALUE (type);
2418 if (unsignedp && !unsignedp0)
2419 *restype_ptr = signed_type (*restype_ptr);
2421 if (TREE_TYPE (primop1) != *restype_ptr)
2422 primop1 = convert (*restype_ptr, primop1);
2423 if (type != *restype_ptr)
2425 minval = convert (*restype_ptr, minval);
2426 maxval = convert (*restype_ptr, maxval);
2429 if (unsignedp && unsignedp0)
2431 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2432 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2433 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2434 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2436 else
2438 min_gt = INT_CST_LT (primop1, minval);
2439 max_gt = INT_CST_LT (primop1, maxval);
2440 min_lt = INT_CST_LT (minval, primop1);
2441 max_lt = INT_CST_LT (maxval, primop1);
2444 val = 0;
2445 /* This used to be a switch, but Genix compiler can't handle that. */
2446 if (code == NE_EXPR)
2448 if (max_lt || min_gt)
2449 val = boolean_true_node;
2451 else if (code == EQ_EXPR)
2453 if (max_lt || min_gt)
2454 val = boolean_false_node;
2456 else if (code == LT_EXPR)
2458 if (max_lt)
2459 val = boolean_true_node;
2460 if (!min_lt)
2461 val = boolean_false_node;
2463 else if (code == GT_EXPR)
2465 if (min_gt)
2466 val = boolean_true_node;
2467 if (!max_gt)
2468 val = boolean_false_node;
2470 else if (code == LE_EXPR)
2472 if (!max_gt)
2473 val = boolean_true_node;
2474 if (min_gt)
2475 val = boolean_false_node;
2477 else if (code == GE_EXPR)
2479 if (!min_lt)
2480 val = boolean_true_node;
2481 if (max_lt)
2482 val = boolean_false_node;
2485 /* If primop0 was sign-extended and unsigned comparison specd,
2486 we did a signed comparison above using the signed type bounds.
2487 But the comparison we output must be unsigned.
2489 Also, for inequalities, VAL is no good; but if the signed
2490 comparison had *any* fixed result, it follows that the
2491 unsigned comparison just tests the sign in reverse
2492 (positive values are LE, negative ones GE).
2493 So we can generate an unsigned comparison
2494 against an extreme value of the signed type. */
2496 if (unsignedp && !unsignedp0)
2498 if (val != 0)
2499 switch (code)
2501 case LT_EXPR:
2502 case GE_EXPR:
2503 primop1 = TYPE_MIN_VALUE (type);
2504 val = 0;
2505 break;
2507 case LE_EXPR:
2508 case GT_EXPR:
2509 primop1 = TYPE_MAX_VALUE (type);
2510 val = 0;
2511 break;
2513 default:
2514 break;
2516 type = unsigned_type (type);
2519 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2521 /* This is the case of (char)x >?< 0x80, which people used to use
2522 expecting old C compilers to change the 0x80 into -0x80. */
2523 if (val == boolean_false_node)
2524 warning ("comparison is always false due to limited range of data type");
2525 if (val == boolean_true_node)
2526 warning ("comparison is always true due to limited range of data type");
2529 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2531 /* This is the case of (unsigned char)x >?< -1 or < 0. */
2532 if (val == boolean_false_node)
2533 warning ("comparison is always false due to limited range of data type");
2534 if (val == boolean_true_node)
2535 warning ("comparison is always true due to limited range of data type");
2538 if (val != 0)
2540 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2541 if (TREE_SIDE_EFFECTS (primop0))
2542 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2543 return val;
2546 /* Value is not predetermined, but do the comparison
2547 in the type of the operand that is not constant.
2548 TYPE is already properly set. */
2550 else if (real1 && real2
2551 && (TYPE_PRECISION (TREE_TYPE (primop0))
2552 == TYPE_PRECISION (TREE_TYPE (primop1))))
2553 type = TREE_TYPE (primop0);
2555 /* If args' natural types are both narrower than nominal type
2556 and both extend in the same manner, compare them
2557 in the type of the wider arg.
2558 Otherwise must actually extend both to the nominal
2559 common type lest different ways of extending
2560 alter the result.
2561 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2563 else if (unsignedp0 == unsignedp1 && real1 == real2
2564 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2565 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2567 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2568 type = signed_or_unsigned_type (unsignedp0
2569 || TREE_UNSIGNED (*restype_ptr),
2570 type);
2571 /* Make sure shorter operand is extended the right way
2572 to match the longer operand. */
2573 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2574 primop0);
2575 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2576 primop1);
2578 else
2580 /* Here we must do the comparison on the nominal type
2581 using the args exactly as we received them. */
2582 type = *restype_ptr;
2583 primop0 = op0;
2584 primop1 = op1;
2586 if (!real1 && !real2 && integer_zerop (primop1)
2587 && TREE_UNSIGNED (*restype_ptr))
2589 tree value = 0;
2590 switch (code)
2592 case GE_EXPR:
2593 /* All unsigned values are >= 0, so we warn if extra warnings
2594 are requested. However, if OP0 is a constant that is
2595 >= 0, the signedness of the comparison isn't an issue,
2596 so suppress the warning. */
2597 if (extra_warnings
2598 && ! (TREE_CODE (primop0) == INTEGER_CST
2599 && ! TREE_OVERFLOW (convert (signed_type (type),
2600 primop0))))
2601 warning ("comparison of unsigned expression >= 0 is always true");
2602 value = boolean_true_node;
2603 break;
2605 case LT_EXPR:
2606 if (extra_warnings
2607 && ! (TREE_CODE (primop0) == INTEGER_CST
2608 && ! TREE_OVERFLOW (convert (signed_type (type),
2609 primop0))))
2610 warning ("comparison of unsigned expression < 0 is always false");
2611 value = boolean_false_node;
2612 break;
2614 default:
2615 break;
2618 if (value != 0)
2620 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2621 if (TREE_SIDE_EFFECTS (primop0))
2622 return build (COMPOUND_EXPR, TREE_TYPE (value),
2623 primop0, value);
2624 return value;
2629 *op0_ptr = convert (type, primop0);
2630 *op1_ptr = convert (type, primop1);
2632 *restype_ptr = boolean_type_node;
2634 return 0;
2637 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2638 or validate its data type for an `if' or `while' statement or ?..: exp.
2640 This preparation consists of taking the ordinary
2641 representation of an expression expr and producing a valid tree
2642 boolean expression describing whether expr is nonzero. We could
2643 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2644 but we optimize comparisons, &&, ||, and !.
2646 The resulting type should always be `boolean_type_node'. */
2648 tree
2649 truthvalue_conversion (expr)
2650 tree expr;
2652 if (TREE_CODE (expr) == ERROR_MARK)
2653 return expr;
2655 #if 0 /* This appears to be wrong for C++. */
2656 /* These really should return error_mark_node after 2.4 is stable.
2657 But not all callers handle ERROR_MARK properly. */
2658 switch (TREE_CODE (TREE_TYPE (expr)))
2660 case RECORD_TYPE:
2661 error ("struct type value used where scalar is required");
2662 return boolean_false_node;
2664 case UNION_TYPE:
2665 error ("union type value used where scalar is required");
2666 return boolean_false_node;
2668 case ARRAY_TYPE:
2669 error ("array type value used where scalar is required");
2670 return boolean_false_node;
2672 default:
2673 break;
2675 #endif /* 0 */
2677 switch (TREE_CODE (expr))
2679 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2680 or comparison expressions as truth values at this level. */
2681 #if 0
2682 case COMPONENT_REF:
2683 /* A one-bit unsigned bit-field is already acceptable. */
2684 if (1 == TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (expr, 1)))
2685 && TREE_UNSIGNED (TREE_OPERAND (expr, 1)))
2686 return expr;
2687 break;
2688 #endif
2690 case EQ_EXPR:
2691 /* It is simpler and generates better code to have only TRUTH_*_EXPR
2692 or comparison expressions as truth values at this level. */
2693 #if 0
2694 if (integer_zerop (TREE_OPERAND (expr, 1)))
2695 return build_unary_op (TRUTH_NOT_EXPR, TREE_OPERAND (expr, 0), 0);
2696 #endif
2697 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2698 case TRUTH_ANDIF_EXPR:
2699 case TRUTH_ORIF_EXPR:
2700 case TRUTH_AND_EXPR:
2701 case TRUTH_OR_EXPR:
2702 case TRUTH_XOR_EXPR:
2703 case TRUTH_NOT_EXPR:
2704 TREE_TYPE (expr) = boolean_type_node;
2705 return expr;
2707 case ERROR_MARK:
2708 return expr;
2710 case INTEGER_CST:
2711 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2713 case REAL_CST:
2714 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2716 case ADDR_EXPR:
2717 /* If we are taking the address of a external decl, it might be zero
2718 if it is weak, so we cannot optimize. */
2719 if (TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (expr, 0))) == 'd'
2720 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2721 break;
2723 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2724 return build (COMPOUND_EXPR, boolean_type_node,
2725 TREE_OPERAND (expr, 0), boolean_true_node);
2726 else
2727 return boolean_true_node;
2729 case COMPLEX_EXPR:
2730 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2731 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2732 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2733 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2736 case NEGATE_EXPR:
2737 case ABS_EXPR:
2738 case FLOAT_EXPR:
2739 case FFS_EXPR:
2740 /* These don't change whether an object is non-zero or zero. */
2741 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2743 case LROTATE_EXPR:
2744 case RROTATE_EXPR:
2745 /* These don't change whether an object is zero or non-zero, but
2746 we can't ignore them if their second arg has side-effects. */
2747 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2748 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2749 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2750 else
2751 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2753 case COND_EXPR:
2754 /* Distribute the conversion into the arms of a COND_EXPR. */
2755 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2756 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2757 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2759 case CONVERT_EXPR:
2760 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2761 since that affects how `default_conversion' will behave. */
2762 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2763 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2764 break;
2765 /* fall through... */
2766 case NOP_EXPR:
2767 /* If this is widening the argument, we can ignore it. */
2768 if (TYPE_PRECISION (TREE_TYPE (expr))
2769 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2770 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2771 break;
2773 case MINUS_EXPR:
2774 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2775 this case. */
2776 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2777 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2778 break;
2779 /* fall through... */
2780 case BIT_XOR_EXPR:
2781 /* This and MINUS_EXPR can be changed into a comparison of the
2782 two objects. */
2783 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2784 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2785 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2786 TREE_OPERAND (expr, 1), 1);
2787 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2788 fold (build1 (NOP_EXPR,
2789 TREE_TYPE (TREE_OPERAND (expr, 0)),
2790 TREE_OPERAND (expr, 1))), 1);
2792 case BIT_AND_EXPR:
2793 if (integer_onep (TREE_OPERAND (expr, 1))
2794 && TREE_TYPE (expr) != boolean_type_node)
2795 /* Using convert here would cause infinite recursion. */
2796 return build1 (NOP_EXPR, boolean_type_node, expr);
2797 break;
2799 case MODIFY_EXPR:
2800 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2801 warning ("suggest parentheses around assignment used as truth value");
2802 break;
2804 default:
2805 break;
2808 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2810 tree tem = save_expr (expr);
2811 return (build_binary_op
2812 ((TREE_SIDE_EFFECTS (expr)
2813 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2814 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
2815 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
2816 0));
2819 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2822 #if USE_CPPLIB
2823 /* Read the rest of a #-directive from input stream FINPUT.
2824 In normal use, the directive name and the white space after it
2825 have already been read, so they won't be included in the result.
2826 We allow for the fact that the directive line may contain
2827 a newline embedded within a character or string literal which forms
2828 a part of the directive.
2830 The value is a string in a reusable buffer. It remains valid
2831 only until the next time this function is called. */
2832 unsigned char *yy_cur, *yy_lim;
2834 #define GETC() (yy_cur < yy_lim ? *yy_cur++ : yy_get_token ())
2835 #define UNGETC(c) ((c) == EOF ? 0 : yy_cur--)
2838 yy_get_token ()
2840 for (;;)
2842 parse_in.limit = parse_in.token_buffer;
2843 cpp_token = cpp_get_token (&parse_in);
2844 if (cpp_token == CPP_EOF)
2845 return -1;
2846 yy_lim = CPP_PWRITTEN (&parse_in);
2847 yy_cur = parse_in.token_buffer;
2848 if (yy_cur < yy_lim)
2849 return *yy_cur++;
2853 char *
2854 get_directive_line ()
2856 static char *directive_buffer = NULL;
2857 static unsigned buffer_length = 0;
2858 register char *p;
2859 register char *buffer_limit;
2860 register int looking_for = 0;
2861 register int char_escaped = 0;
2863 if (buffer_length == 0)
2865 directive_buffer = (char *)xmalloc (128);
2866 buffer_length = 128;
2869 buffer_limit = &directive_buffer[buffer_length];
2871 for (p = directive_buffer; ; )
2873 int c;
2875 /* Make buffer bigger if it is full. */
2876 if (p >= buffer_limit)
2878 register unsigned bytes_used = (p - directive_buffer);
2880 buffer_length *= 2;
2881 directive_buffer
2882 = (char *)xrealloc (directive_buffer, buffer_length);
2883 p = &directive_buffer[bytes_used];
2884 buffer_limit = &directive_buffer[buffer_length];
2887 c = GETC ();
2889 /* Discard initial whitespace. */
2890 if ((c == ' ' || c == '\t') && p == directive_buffer)
2891 continue;
2893 /* Detect the end of the directive. */
2894 if (c == '\n' && looking_for == 0)
2896 UNGETC (c);
2897 c = '\0';
2900 *p++ = c;
2902 if (c == 0)
2903 return directive_buffer;
2905 /* Handle string and character constant syntax. */
2906 if (looking_for)
2908 if (looking_for == c && !char_escaped)
2909 looking_for = 0; /* Found terminator... stop looking. */
2911 else
2912 if (c == '\'' || c == '"')
2913 looking_for = c; /* Don't stop buffering until we see another
2914 another one of these (or an EOF). */
2916 /* Handle backslash. */
2917 char_escaped = (c == '\\' && ! char_escaped);
2920 #else
2921 /* Read the rest of a #-directive from input stream FINPUT.
2922 In normal use, the directive name and the white space after it
2923 have already been read, so they won't be included in the result.
2924 We allow for the fact that the directive line may contain
2925 a newline embedded within a character or string literal which forms
2926 a part of the directive.
2928 The value is a string in a reusable buffer. It remains valid
2929 only until the next time this function is called.
2931 The terminating character ('\n' or EOF) is left in FINPUT for the
2932 caller to re-read. */
2934 char *
2935 get_directive_line (finput)
2936 register FILE *finput;
2938 static char *directive_buffer = NULL;
2939 static unsigned buffer_length = 0;
2940 register char *p;
2941 register char *buffer_limit;
2942 register int looking_for = 0;
2943 register int char_escaped = 0;
2945 if (buffer_length == 0)
2947 directive_buffer = (char *)xmalloc (128);
2948 buffer_length = 128;
2951 buffer_limit = &directive_buffer[buffer_length];
2953 for (p = directive_buffer; ; )
2955 int c;
2957 /* Make buffer bigger if it is full. */
2958 if (p >= buffer_limit)
2960 register unsigned bytes_used = (p - directive_buffer);
2962 buffer_length *= 2;
2963 directive_buffer
2964 = (char *)xrealloc (directive_buffer, buffer_length);
2965 p = &directive_buffer[bytes_used];
2966 buffer_limit = &directive_buffer[buffer_length];
2969 c = getc (finput);
2971 /* Discard initial whitespace. */
2972 if ((c == ' ' || c == '\t') && p == directive_buffer)
2973 continue;
2975 /* Detect the end of the directive. */
2976 if (looking_for == 0
2977 && (c == '\n' || c == EOF))
2979 ungetc (c, finput);
2980 c = '\0';
2983 *p++ = c;
2985 if (c == 0)
2986 return directive_buffer;
2988 /* Handle string and character constant syntax. */
2989 if (looking_for)
2991 if (looking_for == c && !char_escaped)
2992 looking_for = 0; /* Found terminator... stop looking. */
2994 else
2995 if (c == '\'' || c == '"')
2996 looking_for = c; /* Don't stop buffering until we see another
2997 one of these (or an EOF). */
2999 /* Handle backslash. */
3000 char_escaped = (c == '\\' && ! char_escaped);
3003 #endif /* !USE_CPPLIB */
3005 /* Make a variant type in the proper way for C/C++, propagating qualifiers
3006 down to the element type of an array. */
3008 tree
3009 c_build_qualified_type (type, type_quals)
3010 tree type;
3011 int type_quals;
3013 /* A restrict-qualified pointer type must be a pointer to object or
3014 incomplete type. Note that the use of POINTER_TYPE_P also allows
3015 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
3016 the C++ front-end also use POINTER_TYPE for pointer-to-member
3017 values, so even though it should be illegal to use `restrict'
3018 with such an entity we don't flag that here. Thus, special case
3019 code for that case is required in the C++ front-end. */
3020 if ((type_quals & TYPE_QUAL_RESTRICT)
3021 && (!POINTER_TYPE_P (type)
3022 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
3024 error ("invalid use of `restrict'");
3025 type_quals &= ~TYPE_QUAL_RESTRICT;
3028 if (TREE_CODE (type) == ARRAY_TYPE)
3029 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
3030 type_quals),
3031 TYPE_DOMAIN (type));
3032 return build_qualified_type (type, type_quals);
3035 /* Apply the TYPE_QUALS to the new DECL. */
3037 void
3038 c_apply_type_quals_to_decl (type_quals, decl)
3039 int type_quals;
3040 tree decl;
3042 if (type_quals & TYPE_QUAL_CONST)
3043 TREE_READONLY (decl) = 1;
3044 if (type_quals & TYPE_QUAL_VOLATILE)
3046 TREE_SIDE_EFFECTS (decl) = 1;
3047 TREE_THIS_VOLATILE (decl) = 1;
3049 if (type_quals & TYPE_QUAL_RESTRICT)
3051 if (!TREE_TYPE (decl)
3052 || !POINTER_TYPE_P (TREE_TYPE (decl))
3053 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
3054 error ("invalid use of `restrict'");
3055 else if (flag_strict_aliasing)
3057 /* No two restricted pointers can point at the same thing.
3058 However, a restricted pointer can point at the same thing
3059 as an unrestricted pointer, if that unrestricted pointer
3060 is based on the restricted pointer. So, we make the
3061 alias set for the restricted pointer a subset of the
3062 alias set for the type pointed to by the type of the
3063 decl. */
3065 int pointed_to_alias_set
3066 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
3068 if (!pointed_to_alias_set)
3069 /* It's not legal to make a subset of alias set zero. */
3071 else
3073 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
3074 record_alias_subset (pointed_to_alias_set,
3075 DECL_POINTER_ALIAS_SET (decl));
3081 /* T is an expression with pointer type. Find the DECL on which this
3082 expression is based. (For example, in `a[i]' this would be `a'.)
3083 If there is no such DECL, or a unique decl cannot be determined,
3084 NULL_TREE is retured. */
3086 static tree
3087 c_find_base_decl (t)
3088 tree t;
3090 int i;
3091 tree decl;
3093 if (t == NULL_TREE || t == error_mark_node)
3094 return NULL_TREE;
3096 if (!POINTER_TYPE_P (TREE_TYPE (t)))
3097 return NULL_TREE;
3099 decl = NULL_TREE;
3101 if (TREE_CODE (t) == FIELD_DECL
3102 || TREE_CODE (t) == PARM_DECL
3103 || TREE_CODE (t) == VAR_DECL)
3104 /* Aha, we found a pointer-typed declaration. */
3105 return t;
3107 /* It would be nice to deal with COMPONENT_REFs here. If we could
3108 tell that `a' and `b' were the same, then `a->f' and `b->f' are
3109 also the same. */
3111 /* Handle general expressions. */
3112 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3114 case '1':
3115 case '2':
3116 case '3':
3117 for (i = tree_code_length [(int) TREE_CODE (t)]; --i >= 0;)
3119 tree d = c_find_base_decl (TREE_OPERAND (t, i));
3120 if (d)
3122 if (!decl)
3123 decl = d;
3124 else if (d && d != decl)
3125 /* Two different declarations. That's confusing; let's
3126 just assume we don't know what's going on. */
3127 decl = NULL_TREE;
3130 break;
3132 default:
3133 break;
3136 return decl;
3139 /* Return the typed-based alias set for T, which may be an expression
3140 or a type. */
3143 c_get_alias_set (t)
3144 tree t;
3146 tree type;
3147 tree u;
3149 if (t == error_mark_node)
3150 return 0;
3152 type = (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
3153 ? t : TREE_TYPE (t);
3155 if (type == error_mark_node)
3156 return 0;
3158 /* Deal with special cases first; for certain kinds of references
3159 we're interested in more than just the type. */
3161 if (TREE_CODE (t) == BIT_FIELD_REF)
3162 /* Perhaps reads and writes to this piece of data alias fields
3163 neighboring the bitfield. Perhaps that's impossible. For now,
3164 let's just assume that bitfields can alias everything, which is
3165 the conservative assumption. */
3166 return 0;
3168 /* Permit type-punning when accessing a union, provided the access
3169 is directly through the union. For example, this code does not
3170 permit taking the address of a union member and then storing
3171 through it. Even the type-punning allowed here is a GCC
3172 extension, albeit a common and useful one; the C standard says
3173 that such accesses have implementation-defined behavior. */
3174 for (u = t;
3175 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3176 u = TREE_OPERAND (u, 0))
3177 if (TREE_CODE (u) == COMPONENT_REF
3178 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3179 return 0;
3181 if (TREE_CODE (t) == INDIRECT_REF)
3183 /* Check for accesses through restrict-qualified pointers. */
3184 tree decl = c_find_base_decl (TREE_OPERAND (t, 0));
3186 if (decl && DECL_POINTER_ALIAS_SET_KNOWN_P (decl))
3187 /* We use the alias set indicated in the declaration. */
3188 return DECL_POINTER_ALIAS_SET (decl);
3191 /* From here on, only the type matters. */
3193 if (TREE_CODE (t) == COMPONENT_REF
3194 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1)))
3195 /* Since build_modify_expr calls get_unwidened for stores to
3196 component references, the type of a bit field can be changed
3197 from (say) `unsigned int : 16' to `unsigned short' or from
3198 `enum E : 16' to `short'. We want the real type of the
3199 bit-field in this case, not some the integral equivalent. */
3200 type = DECL_BIT_FIELD_TYPE (TREE_OPERAND (t, 1));
3202 if (TYPE_ALIAS_SET_KNOWN_P (type))
3203 /* If we've already calculated the value, just return it. */
3204 return TYPE_ALIAS_SET (type);
3205 else if (TYPE_MAIN_VARIANT (type) != type)
3206 /* The C standard specifically allows aliasing between
3207 cv-qualified variants of types. */
3208 TYPE_ALIAS_SET (type) = c_get_alias_set (TYPE_MAIN_VARIANT (type));
3209 else if (TREE_CODE (type) == INTEGER_TYPE)
3211 tree signed_variant;
3213 /* The C standard specifically allows aliasing between signed and
3214 unsigned variants of the same type. We treat the signed
3215 variant as canonical. */
3216 signed_variant = signed_type (type);
3218 if (signed_variant != type)
3219 TYPE_ALIAS_SET (type) = c_get_alias_set (signed_variant);
3220 else if (signed_variant == signed_char_type_node)
3221 /* The C standard guarantess that any object may be accessed
3222 via an lvalue that has character type. We don't have to
3223 check for unsigned_char_type_node or char_type_node because
3224 we are specifically looking at the signed variant. */
3225 TYPE_ALIAS_SET (type) = 0;
3227 else if (TREE_CODE (type) == ARRAY_TYPE)
3228 /* Anything that can alias one of the array elements can alias
3229 the entire array as well. */
3230 TYPE_ALIAS_SET (type) = c_get_alias_set (TREE_TYPE (type));
3231 else if (TREE_CODE (type) == FUNCTION_TYPE)
3232 /* There are no objects of FUNCTION_TYPE, so there's no point in
3233 using up an alias set for them. (There are, of course,
3234 pointers and references to functions, but that's
3235 different.) */
3236 TYPE_ALIAS_SET (type) = 0;
3237 else if (TREE_CODE (type) == RECORD_TYPE
3238 || TREE_CODE (type) == UNION_TYPE)
3239 /* If TYPE is a struct or union type then we're reading or
3240 writing an entire struct. Thus, we don't know anything about
3241 aliasing. (In theory, such an access can only alias objects
3242 whose type is the same as one of the fields, recursively, but
3243 we don't yet make any use of that information.) */
3244 TYPE_ALIAS_SET (type) = 0;
3246 if (!TYPE_ALIAS_SET_KNOWN_P (type))
3247 /* TYPE is something we haven't seen before. Put it in a new
3248 alias set. */
3249 TYPE_ALIAS_SET (type) = new_alias_set ();
3251 return TYPE_ALIAS_SET (type);