* c-common.c (c_format_attribute_table): Make format and
[official-gcc.git] / gcc / c-common.c
blobe6c81c385c04b46efd6445d144e237c6a734a6a4
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "tree.h"
25 #include "flags.h"
26 #include "toplev.h"
27 #include "output.h"
28 #include "c-pragma.h"
29 #include "rtl.h"
30 #include "ggc.h"
31 #include "expr.h"
32 #include "c-common.h"
33 #include "tm_p.h"
34 #include "obstack.h"
35 #include "cpplib.h"
36 #include "target.h"
37 cpp_reader *parse_in; /* Declared in c-lex.h. */
39 #undef WCHAR_TYPE_SIZE
40 #define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node)
42 /* We let tm.h override the types used here, to handle trivial differences
43 such as the choice of unsigned int or long unsigned int for size_t.
44 When machines start needing nontrivial differences in the size type,
45 it would be best to do something here to figure out automatically
46 from other information what type to use. */
48 #ifndef SIZE_TYPE
49 #define SIZE_TYPE "long unsigned int"
50 #endif
52 #ifndef WCHAR_TYPE
53 #define WCHAR_TYPE "int"
54 #endif
56 #ifndef PTRDIFF_TYPE
57 #define PTRDIFF_TYPE "long int"
58 #endif
60 #ifndef WINT_TYPE
61 #define WINT_TYPE "unsigned int"
62 #endif
64 #ifndef INTMAX_TYPE
65 #define INTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
66 ? "int" \
67 : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
68 ? "long int" \
69 : "long long int"))
70 #endif
72 #ifndef UINTMAX_TYPE
73 #define UINTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
74 ? "unsigned int" \
75 : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
76 ? "long unsigned int" \
77 : "long long unsigned int"))
78 #endif
80 /* The following symbols are subsumed in the c_global_trees array, and
81 listed here individually for documentation purposes.
83 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
85 tree short_integer_type_node;
86 tree long_integer_type_node;
87 tree long_long_integer_type_node;
89 tree short_unsigned_type_node;
90 tree long_unsigned_type_node;
91 tree long_long_unsigned_type_node;
93 tree boolean_type_node;
94 tree boolean_false_node;
95 tree boolean_true_node;
97 tree ptrdiff_type_node;
99 tree unsigned_char_type_node;
100 tree signed_char_type_node;
101 tree wchar_type_node;
102 tree signed_wchar_type_node;
103 tree unsigned_wchar_type_node;
105 tree float_type_node;
106 tree double_type_node;
107 tree long_double_type_node;
109 tree complex_integer_type_node;
110 tree complex_float_type_node;
111 tree complex_double_type_node;
112 tree complex_long_double_type_node;
114 tree intQI_type_node;
115 tree intHI_type_node;
116 tree intSI_type_node;
117 tree intDI_type_node;
118 tree intTI_type_node;
120 tree unsigned_intQI_type_node;
121 tree unsigned_intHI_type_node;
122 tree unsigned_intSI_type_node;
123 tree unsigned_intDI_type_node;
124 tree unsigned_intTI_type_node;
126 tree widest_integer_literal_type_node;
127 tree widest_unsigned_literal_type_node;
129 Nodes for types `void *' and `const void *'.
131 tree ptr_type_node, const_ptr_type_node;
133 Nodes for types `char *' and `const char *'.
135 tree string_type_node, const_string_type_node;
137 Type `char[SOMENUMBER]'.
138 Used when an array of char is needed and the size is irrelevant.
140 tree char_array_type_node;
142 Type `int[SOMENUMBER]' or something like it.
143 Used when an array of int needed and the size is irrelevant.
145 tree int_array_type_node;
147 Type `wchar_t[SOMENUMBER]' or something like it.
148 Used when a wide string literal is created.
150 tree wchar_array_type_node;
152 Type `int ()' -- used for implicit declaration of functions.
154 tree default_function_type;
156 A VOID_TYPE node, packaged in a TREE_LIST.
158 tree void_list_node;
160 The lazily created VAR_DECLS for __FUNCTION__, __PRETTY_FUNCTION__,
161 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
162 VAR_DECLS, but C++ does.)
164 tree function_name_decl_node;
165 tree pretty_function_name_declnode;
166 tree c99_function_name_decl_node;
168 Stack of nested function name VAR_DECLs.
170 tree saved_function_name_decls;
174 tree c_global_trees[CTI_MAX];
176 /* Nonzero means don't recognize the non-ANSI builtin functions. */
178 int flag_no_builtin;
180 /* Nonzero means don't recognize the non-ANSI builtin functions.
181 -ansi sets this. */
183 int flag_no_nonansi_builtin;
185 /* Nonzero means give `double' the same size as `float'. */
187 int flag_short_double;
189 /* Nonzero means give `wchar_t' the same size as `short'. */
191 int flag_short_wchar;
193 /* Nonzero means warn about possible violations of sequence point rules. */
195 int warn_sequence_point;
197 /* The elements of `ridpointers' are identifier nodes for the reserved
198 type names and storage classes. It is indexed by a RID_... value. */
199 tree *ridpointers;
201 tree (*make_fname_decl) PARAMS ((tree, int));
203 /* If non-NULL, the address of a language-specific function that
204 returns 1 for language-specific statement codes. */
205 int (*lang_statement_code_p) PARAMS ((enum tree_code));
207 /* If non-NULL, the address of a language-specific function that takes
208 any action required right before expand_function_end is called. */
209 void (*lang_expand_function_end) PARAMS ((void));
211 /* If this variable is defined to a non-NULL value, it will be called
212 after the file has been completely parsed. */
213 void (*back_end_hook) PARAMS ((tree));
215 /* Nonzero means the expression being parsed will never be evaluated.
216 This is a count, since unevaluated expressions can nest. */
217 int skip_evaluation;
219 /* Information about how a function name is generated. */
220 struct fname_var_t
222 tree *decl; /* pointer to the VAR_DECL. */
223 unsigned rid; /* RID number for the identifier. */
224 int pretty; /* How pretty is it? */
227 /* The three ways of getting then name of the current function. */
229 const struct fname_var_t fname_vars[] =
231 /* C99 compliant __func__, must be first. */
232 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
233 /* GCC __FUNCTION__ compliant. */
234 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
235 /* GCC __PRETTY_FUNCTION__ compliant. */
236 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
237 {NULL, 0, 0},
240 static int constant_fits_type_p PARAMS ((tree, tree));
242 /* Keep a stack of if statements. We record the number of compound
243 statements seen up to the if keyword, as well as the line number
244 and file of the if. If a potentially ambiguous else is seen, that
245 fact is recorded; the warning is issued when we can be sure that
246 the enclosing if statement does not have an else branch. */
247 typedef struct
249 int compstmt_count;
250 int line;
251 const char *file;
252 int needs_warning;
253 tree if_stmt;
254 } if_elt;
256 static if_elt *if_stack;
258 /* Amount of space in the if statement stack. */
259 static int if_stack_space = 0;
261 /* Stack pointer. */
262 static int if_stack_pointer = 0;
264 /* Record the start of an if-then, and record the start of it
265 for ambiguous else detection. */
267 void
268 c_expand_start_cond (cond, compstmt_count)
269 tree cond;
270 int compstmt_count;
272 tree if_stmt;
274 /* Make sure there is enough space on the stack. */
275 if (if_stack_space == 0)
277 if_stack_space = 10;
278 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
280 else if (if_stack_space == if_stack_pointer)
282 if_stack_space += 10;
283 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
286 if_stmt = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
287 IF_COND (if_stmt) = cond;
288 add_stmt (if_stmt);
290 /* Record this if statement. */
291 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
292 if_stack[if_stack_pointer].file = input_filename;
293 if_stack[if_stack_pointer].line = lineno;
294 if_stack[if_stack_pointer].needs_warning = 0;
295 if_stack[if_stack_pointer].if_stmt = if_stmt;
296 if_stack_pointer++;
299 /* Called after the then-clause for an if-statement is processed. */
301 void
302 c_finish_then ()
304 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
305 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
308 /* Record the end of an if-then. Optionally warn if a nested
309 if statement had an ambiguous else clause. */
311 void
312 c_expand_end_cond ()
314 if_stack_pointer--;
315 if (if_stack[if_stack_pointer].needs_warning)
316 warning_with_file_and_line (if_stack[if_stack_pointer].file,
317 if_stack[if_stack_pointer].line,
318 "suggest explicit braces to avoid ambiguous `else'");
319 last_expr_type = NULL_TREE;
322 /* Called between the then-clause and the else-clause
323 of an if-then-else. */
325 void
326 c_expand_start_else ()
328 /* An ambiguous else warning must be generated for the enclosing if
329 statement, unless we see an else branch for that one, too. */
330 if (warn_parentheses
331 && if_stack_pointer > 1
332 && (if_stack[if_stack_pointer - 1].compstmt_count
333 == if_stack[if_stack_pointer - 2].compstmt_count))
334 if_stack[if_stack_pointer - 2].needs_warning = 1;
336 /* Even if a nested if statement had an else branch, it can't be
337 ambiguous if this one also has an else. So don't warn in that
338 case. Also don't warn for any if statements nested in this else. */
339 if_stack[if_stack_pointer - 1].needs_warning = 0;
340 if_stack[if_stack_pointer - 1].compstmt_count--;
343 /* Called after the else-clause for an if-statement is processed. */
345 void
346 c_finish_else ()
348 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
349 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
352 /* Push current bindings for the function name VAR_DECLS. */
354 void
355 start_fname_decls ()
357 unsigned ix;
358 tree saved = NULL_TREE;
360 for (ix = 0; fname_vars[ix].decl; ix++)
362 tree decl = *fname_vars[ix].decl;
364 if (decl)
366 saved = tree_cons (decl, build_int_2 (ix, 0), saved);
367 *fname_vars[ix].decl = NULL_TREE;
370 if (saved || saved_function_name_decls)
371 /* Normally they'll have been NULL, so only push if we've got a
372 stack, or they are non-NULL. */
373 saved_function_name_decls = tree_cons (saved, NULL_TREE,
374 saved_function_name_decls);
377 /* Finish up the current bindings, adding them into the
378 current function's statement tree. This is done by wrapping the
379 function's body in a COMPOUND_STMT containing these decls too. This
380 must be done _before_ finish_stmt_tree is called. If there is no
381 current function, we must be at file scope and no statements are
382 involved. Pop the previous bindings. */
384 void
385 finish_fname_decls ()
387 unsigned ix;
388 tree body = NULL_TREE;
389 tree stack = saved_function_name_decls;
391 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
392 body = chainon (TREE_VALUE (stack), body);
394 if (body)
396 /* They were called into existance, so add to statement tree. */
397 body = chainon (body,
398 TREE_CHAIN (DECL_SAVED_TREE (current_function_decl)));
399 body = build_stmt (COMPOUND_STMT, body);
401 COMPOUND_STMT_NO_SCOPE (body) = 1;
402 TREE_CHAIN (DECL_SAVED_TREE (current_function_decl)) = body;
405 for (ix = 0; fname_vars[ix].decl; ix++)
406 *fname_vars[ix].decl = NULL_TREE;
408 if (stack)
410 /* We had saved values, restore them. */
411 tree saved;
413 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
415 tree decl = TREE_PURPOSE (saved);
416 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
418 *fname_vars[ix].decl = decl;
420 stack = TREE_CHAIN (stack);
422 saved_function_name_decls = stack;
425 /* Return the text name of the current function, suitable prettified
426 by PRETTY_P. */
428 const char *
429 fname_as_string (pretty_p)
430 int pretty_p;
432 const char *name = NULL;
434 if (pretty_p)
435 name = (current_function_decl
436 ? (*decl_printable_name) (current_function_decl, 2)
437 : "top level");
438 else if (current_function_decl && DECL_NAME (current_function_decl))
439 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
440 else
441 name = "";
442 return name;
445 /* Return the text name of the current function, formatted as
446 required by the supplied RID value. */
448 const char *
449 fname_string (rid)
450 unsigned rid;
452 unsigned ix;
454 for (ix = 0; fname_vars[ix].decl; ix++)
455 if (fname_vars[ix].rid == rid)
456 break;
457 return fname_as_string (fname_vars[ix].pretty);
460 /* Return the VAR_DECL for a const char array naming the current
461 function. If the VAR_DECL has not yet been created, create it
462 now. RID indicates how it should be formatted and IDENTIFIER_NODE
463 ID is its name (unfortunately C and C++ hold the RID values of
464 keywords in different places, so we can't derive RID from ID in
465 this language independant code. */
467 tree
468 fname_decl (rid, id)
469 unsigned rid;
470 tree id;
472 unsigned ix;
473 tree decl = NULL_TREE;
475 for (ix = 0; fname_vars[ix].decl; ix++)
476 if (fname_vars[ix].rid == rid)
477 break;
479 decl = *fname_vars[ix].decl;
480 if (!decl)
482 tree saved_last_tree = last_tree;
484 decl = (*make_fname_decl) (id, fname_vars[ix].pretty);
485 if (last_tree != saved_last_tree)
487 /* We created some statement tree for the decl. This belongs
488 at the start of the function, so remove it now and reinsert
489 it after the function is complete. */
490 tree stmts = TREE_CHAIN (saved_last_tree);
492 TREE_CHAIN (saved_last_tree) = NULL_TREE;
493 last_tree = saved_last_tree;
494 saved_function_name_decls = tree_cons (decl, stmts,
495 saved_function_name_decls);
497 *fname_vars[ix].decl = decl;
499 if (!ix && !current_function_decl)
500 pedwarn_with_decl (decl, "`%s' is not defined outside of function scope");
502 return decl;
505 /* Given a chain of STRING_CST nodes,
506 concatenate them into one STRING_CST
507 and give it a suitable array-of-chars data type. */
509 tree
510 combine_strings (strings)
511 tree strings;
513 register tree value, t;
514 register int length = 1;
515 int wide_length = 0;
516 int wide_flag = 0;
517 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
518 int nchars;
519 const int nchars_max = flag_isoc99 ? 4095 : 509;
521 if (TREE_CHAIN (strings))
523 /* More than one in the chain, so concatenate. */
524 register char *p, *q;
526 /* Don't include the \0 at the end of each substring,
527 except for the last one.
528 Count wide strings and ordinary strings separately. */
529 for (t = strings; t; t = TREE_CHAIN (t))
531 if (TREE_TYPE (t) == wchar_array_type_node)
533 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
534 wide_flag = 1;
536 else
537 length += (TREE_STRING_LENGTH (t) - 1);
540 /* If anything is wide, the non-wides will be converted,
541 which makes them take more space. */
542 if (wide_flag)
543 length = length * wchar_bytes + wide_length;
545 p = alloca (length);
547 /* Copy the individual strings into the new combined string.
548 If the combined string is wide, convert the chars to ints
549 for any individual strings that are not wide. */
551 q = p;
552 for (t = strings; t; t = TREE_CHAIN (t))
554 int len = (TREE_STRING_LENGTH (t)
555 - ((TREE_TYPE (t) == wchar_array_type_node)
556 ? wchar_bytes : 1));
557 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
559 memcpy (q, TREE_STRING_POINTER (t), len);
560 q += len;
562 else
564 int i, j;
565 for (i = 0; i < len; i++)
567 if (BYTES_BIG_ENDIAN)
569 for (j=0; j<(WCHAR_TYPE_SIZE / BITS_PER_UNIT)-1; j++)
570 *q++ = 0;
571 *q++ = TREE_STRING_POINTER (t)[i];
573 else
575 *q++ = TREE_STRING_POINTER (t)[i];
576 for (j=0; j<(WCHAR_TYPE_SIZE / BITS_PER_UNIT)-1; j++)
577 *q++ = 0;
582 if (wide_flag)
584 int i;
585 for (i = 0; i < wchar_bytes; i++)
586 *q++ = 0;
588 else
589 *q = 0;
591 value = build_string (length, p);
593 else
595 value = strings;
596 length = TREE_STRING_LENGTH (value);
597 if (TREE_TYPE (value) == wchar_array_type_node)
598 wide_flag = 1;
601 /* Compute the number of elements, for the array type. */
602 nchars = wide_flag ? length / wchar_bytes : length;
604 if (pedantic && nchars - 1 > nchars_max && c_language == clk_c)
605 pedwarn ("string length `%d' is greater than the length `%d' ISO C%d compilers are required to support",
606 nchars - 1, nchars_max, flag_isoc99 ? 99 : 89);
608 /* Create the array type for the string constant.
609 -Wwrite-strings says make the string constant an array of const char
610 so that copying it to a non-const pointer will get a warning.
611 For C++, this is the standard behavior. */
612 if (flag_const_strings
613 && (! flag_traditional && ! flag_writable_strings))
615 tree elements
616 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
617 1, 0);
618 TREE_TYPE (value)
619 = build_array_type (elements,
620 build_index_type (build_int_2 (nchars - 1, 0)));
622 else
623 TREE_TYPE (value)
624 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
625 build_index_type (build_int_2 (nchars - 1, 0)));
627 TREE_CONSTANT (value) = 1;
628 TREE_READONLY (value) = ! flag_writable_strings;
629 TREE_STATIC (value) = 1;
630 return value;
633 static int is_valid_printf_arglist PARAMS ((tree));
634 static rtx c_expand_builtin PARAMS ((tree, rtx, enum machine_mode, enum expand_modifier));
635 static rtx c_expand_builtin_printf PARAMS ((tree, rtx, enum machine_mode,
636 enum expand_modifier, int));
637 static rtx c_expand_builtin_fprintf PARAMS ((tree, rtx, enum machine_mode,
638 enum expand_modifier, int));
640 /* Print a warning if a constant expression had overflow in folding.
641 Invoke this function on every expression that the language
642 requires to be a constant expression.
643 Note the ANSI C standard says it is erroneous for a
644 constant expression to overflow. */
646 void
647 constant_expression_warning (value)
648 tree value;
650 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
651 || TREE_CODE (value) == COMPLEX_CST)
652 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
653 pedwarn ("overflow in constant expression");
656 /* Print a warning if an expression had overflow in folding.
657 Invoke this function on every expression that
658 (1) appears in the source code, and
659 (2) might be a constant expression that overflowed, and
660 (3) is not already checked by convert_and_check;
661 however, do not invoke this function on operands of explicit casts. */
663 void
664 overflow_warning (value)
665 tree value;
667 if ((TREE_CODE (value) == INTEGER_CST
668 || (TREE_CODE (value) == COMPLEX_CST
669 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
670 && TREE_OVERFLOW (value))
672 TREE_OVERFLOW (value) = 0;
673 if (skip_evaluation == 0)
674 warning ("integer overflow in expression");
676 else if ((TREE_CODE (value) == REAL_CST
677 || (TREE_CODE (value) == COMPLEX_CST
678 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
679 && TREE_OVERFLOW (value))
681 TREE_OVERFLOW (value) = 0;
682 if (skip_evaluation == 0)
683 warning ("floating point overflow in expression");
687 /* Print a warning if a large constant is truncated to unsigned,
688 or if -Wconversion is used and a constant < 0 is converted to unsigned.
689 Invoke this function on every expression that might be implicitly
690 converted to an unsigned type. */
692 void
693 unsigned_conversion_warning (result, operand)
694 tree result, operand;
696 if (TREE_CODE (operand) == INTEGER_CST
697 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
698 && TREE_UNSIGNED (TREE_TYPE (result))
699 && skip_evaluation == 0
700 && !int_fits_type_p (operand, TREE_TYPE (result)))
702 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
703 /* This detects cases like converting -129 or 256 to unsigned char. */
704 warning ("large integer implicitly truncated to unsigned type");
705 else if (warn_conversion)
706 warning ("negative integer implicitly converted to unsigned type");
710 /* Nonzero if constant C has a value that is permissible
711 for type TYPE (an INTEGER_TYPE). */
713 static int
714 constant_fits_type_p (c, type)
715 tree c, type;
717 if (TREE_CODE (c) == INTEGER_CST)
718 return int_fits_type_p (c, type);
720 c = convert (type, c);
721 return !TREE_OVERFLOW (c);
724 /* Convert EXPR to TYPE, warning about conversion problems with constants.
725 Invoke this function on every expression that is converted implicitly,
726 i.e. because of language rules and not because of an explicit cast. */
728 tree
729 convert_and_check (type, expr)
730 tree type, expr;
732 tree t = convert (type, expr);
733 if (TREE_CODE (t) == INTEGER_CST)
735 if (TREE_OVERFLOW (t))
737 TREE_OVERFLOW (t) = 0;
739 /* Do not diagnose overflow in a constant expression merely
740 because a conversion overflowed. */
741 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
743 /* No warning for converting 0x80000000 to int. */
744 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
745 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
746 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
747 /* If EXPR fits in the unsigned version of TYPE,
748 don't warn unless pedantic. */
749 if ((pedantic
750 || TREE_UNSIGNED (type)
751 || ! constant_fits_type_p (expr, unsigned_type (type)))
752 && skip_evaluation == 0)
753 warning ("overflow in implicit constant conversion");
755 else
756 unsigned_conversion_warning (t, expr);
758 return t;
761 /* A node in a list that describes references to variables (EXPR), which are
762 either read accesses if WRITER is zero, or write accesses, in which case
763 WRITER is the parent of EXPR. */
764 struct tlist
766 struct tlist *next;
767 tree expr, writer;
770 /* Used to implement a cache the results of a call to verify_tree. We only
771 use this for SAVE_EXPRs. */
772 struct tlist_cache
774 struct tlist_cache *next;
775 struct tlist *cache_before_sp;
776 struct tlist *cache_after_sp;
777 tree expr;
780 /* Obstack to use when allocating tlist structures, and corresponding
781 firstobj. */
782 static struct obstack tlist_obstack;
783 static char *tlist_firstobj = 0;
785 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
786 warnings. */
787 static struct tlist *warned_ids;
788 /* SAVE_EXPRs need special treatment. We process them only once and then
789 cache the results. */
790 static struct tlist_cache *save_expr_cache;
792 static void add_tlist PARAMS ((struct tlist **, struct tlist *, tree, int));
793 static void merge_tlist PARAMS ((struct tlist **, struct tlist *, int));
794 static void verify_tree PARAMS ((tree, struct tlist **, struct tlist **, tree));
795 static int warning_candidate_p PARAMS ((tree));
796 static void warn_for_collisions PARAMS ((struct tlist *));
797 static void warn_for_collisions_1 PARAMS ((tree, tree, struct tlist *, int));
798 static struct tlist *new_tlist PARAMS ((struct tlist *, tree, tree));
799 static void verify_sequence_points PARAMS ((tree));
801 /* Create a new struct tlist and fill in its fields. */
802 static struct tlist *
803 new_tlist (next, t, writer)
804 struct tlist *next;
805 tree t;
806 tree writer;
808 struct tlist *l;
809 l = (struct tlist *) obstack_alloc (&tlist_obstack, sizeof *l);
810 l->next = next;
811 l->expr = t;
812 l->writer = writer;
813 return l;
816 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
817 is nonnull, we ignore any node we find which has a writer equal to it. */
819 static void
820 add_tlist (to, add, exclude_writer, copy)
821 struct tlist **to;
822 struct tlist *add;
823 tree exclude_writer;
824 int copy;
826 while (add)
828 struct tlist *next = add->next;
829 if (! copy)
830 add->next = *to;
831 if (! exclude_writer || add->writer != exclude_writer)
832 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
833 add = next;
837 /* Merge the nodes of ADD into TO. This merging process is done so that for
838 each variable that already exists in TO, no new node is added; however if
839 there is a write access recorded in ADD, and an occurrence on TO is only
840 a read access, then the occurrence in TO will be modified to record the
841 write. */
843 static void
844 merge_tlist (to, add, copy)
845 struct tlist **to;
846 struct tlist *add;
847 int copy;
849 struct tlist **end = to;
851 while (*end)
852 end = &(*end)->next;
854 while (add)
856 int found = 0;
857 struct tlist *tmp2;
858 struct tlist *next = add->next;
860 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
861 if (tmp2->expr == add->expr)
863 found = 1;
864 if (! tmp2->writer)
865 tmp2->writer = add->writer;
867 if (! found)
869 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
870 end = &(*end)->next;
871 *end = 0;
873 add = next;
877 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
878 references in list LIST conflict with it, excluding reads if ONLY writers
879 is nonzero. */
881 static void
882 warn_for_collisions_1 (written, writer, list, only_writes)
883 tree written, writer;
884 struct tlist *list;
885 int only_writes;
887 struct tlist *tmp;
889 /* Avoid duplicate warnings. */
890 for (tmp = warned_ids; tmp; tmp = tmp->next)
891 if (tmp->expr == written)
892 return;
894 while (list)
896 if (list->expr == written
897 && list->writer != writer
898 && (! only_writes || list->writer))
900 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
901 warning ("operation on `%s' may be undefined",
902 IDENTIFIER_POINTER (DECL_NAME (list->expr)));
904 list = list->next;
908 /* Given a list LIST of references to variables, find whether any of these
909 can cause conflicts due to missing sequence points. */
911 static void
912 warn_for_collisions (list)
913 struct tlist *list;
915 struct tlist *tmp;
917 for (tmp = list; tmp; tmp = tmp->next)
919 if (tmp->writer)
920 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
924 /* Return nonzero if X is a tree that can be verified by the sequence poitn
925 warnings. */
926 static int
927 warning_candidate_p (x)
928 tree x;
930 return TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == PARM_DECL;
933 /* Walk the tree X, and record accesses to variables. If X is written by the
934 parent tree, WRITER is the parent.
935 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
936 expression or its only operand forces a sequence point, then everything up
937 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
938 in PNO_SP.
939 Once we return, we will have emitted warnings if any subexpression before
940 such a sequence point could be undefined. On a higher level, however, the
941 sequence point may not be relevant, and we'll merge the two lists.
943 Example: (b++, a) + b;
944 The call that processes the COMPOUND_EXPR will store the increment of B
945 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
946 processes the PLUS_EXPR will need to merge the two lists so that
947 eventually, all accesses end up on the same list (and we'll warn about the
948 unordered subexpressions b++ and b.
950 A note on merging. If we modify the former example so that our expression
951 becomes
952 (b++, b) + a
953 care must be taken not simply to add all three expressions into the final
954 PNO_SP list. The function merge_tlist takes care of that by merging the
955 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
956 way, so that no more than one access to B is recorded. */
958 static void
959 verify_tree (x, pbefore_sp, pno_sp, writer)
960 tree x;
961 struct tlist **pbefore_sp, **pno_sp;
962 tree writer;
964 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
965 enum tree_code code;
966 char class;
968 /* X may be NULL if it is the operand of an empty statement expression
969 ({ }). */
970 if (x == NULL)
971 return;
973 restart:
974 code = TREE_CODE (x);
975 class = TREE_CODE_CLASS (code);
977 if (warning_candidate_p (x))
979 *pno_sp = new_tlist (*pno_sp, x, writer);
980 return;
983 switch (code)
985 case CONSTRUCTOR:
986 return;
988 case COMPOUND_EXPR:
989 case TRUTH_ANDIF_EXPR:
990 case TRUTH_ORIF_EXPR:
991 tmp_before = tmp_nosp = tmp_list3 = 0;
992 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
993 warn_for_collisions (tmp_nosp);
994 merge_tlist (pbefore_sp, tmp_before, 0);
995 merge_tlist (pbefore_sp, tmp_nosp, 0);
996 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
997 merge_tlist (pbefore_sp, tmp_list3, 0);
998 return;
1000 case COND_EXPR:
1001 tmp_before = tmp_list2 = 0;
1002 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
1003 warn_for_collisions (tmp_list2);
1004 merge_tlist (pbefore_sp, tmp_before, 0);
1005 merge_tlist (pbefore_sp, tmp_list2, 1);
1007 tmp_list3 = tmp_nosp = 0;
1008 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
1009 warn_for_collisions (tmp_nosp);
1010 merge_tlist (pbefore_sp, tmp_list3, 0);
1012 tmp_list3 = tmp_list2 = 0;
1013 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
1014 warn_for_collisions (tmp_list2);
1015 merge_tlist (pbefore_sp, tmp_list3, 0);
1016 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1017 two first, to avoid warning for (a ? b++ : b++). */
1018 merge_tlist (&tmp_nosp, tmp_list2, 0);
1019 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1020 return;
1022 case PREDECREMENT_EXPR:
1023 case PREINCREMENT_EXPR:
1024 case POSTDECREMENT_EXPR:
1025 case POSTINCREMENT_EXPR:
1026 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
1027 return;
1029 case MODIFY_EXPR:
1030 tmp_before = tmp_nosp = tmp_list3 = 0;
1031 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
1032 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
1033 /* Expressions inside the LHS are not ordered wrt. the sequence points
1034 in the RHS. Example:
1035 *a = (a++, 2)
1036 Despite the fact that the modification of "a" is in the before_sp
1037 list (tmp_before), it conflicts with the use of "a" in the LHS.
1038 We can handle this by adding the contents of tmp_list3
1039 to those of tmp_before, and redoing the collision warnings for that
1040 list. */
1041 add_tlist (&tmp_before, tmp_list3, x, 1);
1042 warn_for_collisions (tmp_before);
1043 /* Exclude the LHS itself here; we first have to merge it into the
1044 tmp_nosp list. This is done to avoid warning for "a = a"; if we
1045 didn't exclude the LHS, we'd get it twice, once as a read and once
1046 as a write. */
1047 add_tlist (pno_sp, tmp_list3, x, 0);
1048 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
1050 merge_tlist (pbefore_sp, tmp_before, 0);
1051 if (warning_candidate_p (TREE_OPERAND (x, 0)))
1052 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
1053 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
1054 return;
1056 case CALL_EXPR:
1057 /* We need to warn about conflicts among arguments and conflicts between
1058 args and the function address. Side effects of the function address,
1059 however, are not ordered by the sequence point of the call. */
1060 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
1061 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1062 if (TREE_OPERAND (x, 1))
1063 verify_tree (TREE_OPERAND (x, 1), &tmp_list2, &tmp_list3, NULL_TREE);
1064 merge_tlist (&tmp_list3, tmp_list2, 0);
1065 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
1066 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
1067 warn_for_collisions (tmp_before);
1068 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
1069 return;
1071 case TREE_LIST:
1072 /* Scan all the list, e.g. indices of multi dimensional array. */
1073 while (x)
1075 tmp_before = tmp_nosp = 0;
1076 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
1077 merge_tlist (&tmp_nosp, tmp_before, 0);
1078 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1079 x = TREE_CHAIN (x);
1081 return;
1083 case SAVE_EXPR:
1085 struct tlist_cache *t;
1086 for (t = save_expr_cache; t; t = t->next)
1087 if (t->expr == x)
1088 break;
1090 if (! t)
1092 t = (struct tlist_cache *) obstack_alloc (&tlist_obstack,
1093 sizeof *t);
1094 t->next = save_expr_cache;
1095 t->expr = x;
1096 save_expr_cache = t;
1098 tmp_before = tmp_nosp = 0;
1099 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1100 warn_for_collisions (tmp_nosp);
1102 tmp_list3 = 0;
1103 while (tmp_nosp)
1105 struct tlist *t = tmp_nosp;
1106 tmp_nosp = t->next;
1107 merge_tlist (&tmp_list3, t, 0);
1109 t->cache_before_sp = tmp_before;
1110 t->cache_after_sp = tmp_list3;
1112 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
1113 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
1114 return;
1116 default:
1117 break;
1120 if (class == '1')
1122 if (first_rtl_op (code) == 0)
1123 return;
1124 x = TREE_OPERAND (x, 0);
1125 writer = 0;
1126 goto restart;
1129 switch (class)
1131 case 'r':
1132 case '<':
1133 case '2':
1134 case 'b':
1135 case 'e':
1136 case 's':
1137 case 'x':
1139 int lp;
1140 int max = first_rtl_op (TREE_CODE (x));
1141 for (lp = 0; lp < max; lp++)
1143 tmp_before = tmp_nosp = 0;
1144 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, NULL_TREE);
1145 merge_tlist (&tmp_nosp, tmp_before, 0);
1146 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1148 break;
1153 /* Try to warn for undefined behaviour in EXPR due to missing sequence
1154 points. */
1156 static void
1157 verify_sequence_points (expr)
1158 tree expr;
1160 struct tlist *before_sp = 0, *after_sp = 0;
1162 warned_ids = 0;
1163 save_expr_cache = 0;
1164 if (tlist_firstobj == 0)
1166 gcc_obstack_init (&tlist_obstack);
1167 tlist_firstobj = obstack_alloc (&tlist_obstack, 0);
1170 verify_tree (expr, &before_sp, &after_sp, 0);
1171 warn_for_collisions (after_sp);
1172 obstack_free (&tlist_obstack, tlist_firstobj);
1175 tree
1176 c_expand_expr_stmt (expr)
1177 tree expr;
1179 /* Do default conversion if safe and possibly important,
1180 in case within ({...}). */
1181 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
1182 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1183 expr = default_conversion (expr);
1185 if (warn_sequence_point)
1186 verify_sequence_points (expr);
1188 if (TREE_TYPE (expr) != error_mark_node
1189 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
1190 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1191 error ("expression statement has incomplete type");
1193 last_expr_type = TREE_TYPE (expr);
1194 return add_stmt (build_stmt (EXPR_STMT, expr));
1197 /* Validate the expression after `case' and apply default promotions. */
1199 tree
1200 check_case_value (value)
1201 tree value;
1203 if (value == NULL_TREE)
1204 return value;
1206 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1207 STRIP_TYPE_NOPS (value);
1208 /* In C++, the following is allowed:
1210 const int i = 3;
1211 switch (...) { case i: ... }
1213 So, we try to reduce the VALUE to a constant that way. */
1214 if (c_language == clk_cplusplus)
1216 value = decl_constant_value (value);
1217 STRIP_TYPE_NOPS (value);
1218 value = fold (value);
1221 if (TREE_CODE (value) != INTEGER_CST
1222 && value != error_mark_node)
1224 error ("case label does not reduce to an integer constant");
1225 value = error_mark_node;
1227 else
1228 /* Promote char or short to int. */
1229 value = default_conversion (value);
1231 constant_expression_warning (value);
1233 return value;
1236 /* Return an integer type with BITS bits of precision,
1237 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1239 tree
1240 type_for_size (bits, unsignedp)
1241 unsigned bits;
1242 int unsignedp;
1244 if (bits == TYPE_PRECISION (integer_type_node))
1245 return unsignedp ? unsigned_type_node : integer_type_node;
1247 if (bits == TYPE_PRECISION (signed_char_type_node))
1248 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1250 if (bits == TYPE_PRECISION (short_integer_type_node))
1251 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1253 if (bits == TYPE_PRECISION (long_integer_type_node))
1254 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1256 if (bits == TYPE_PRECISION (long_long_integer_type_node))
1257 return (unsignedp ? long_long_unsigned_type_node
1258 : long_long_integer_type_node);
1260 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
1261 return (unsignedp ? widest_unsigned_literal_type_node
1262 : widest_integer_literal_type_node);
1264 if (bits <= TYPE_PRECISION (intQI_type_node))
1265 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1267 if (bits <= TYPE_PRECISION (intHI_type_node))
1268 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1270 if (bits <= TYPE_PRECISION (intSI_type_node))
1271 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1273 if (bits <= TYPE_PRECISION (intDI_type_node))
1274 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1276 return 0;
1279 /* Return a data type that has machine mode MODE.
1280 If the mode is an integer,
1281 then UNSIGNEDP selects between signed and unsigned types. */
1283 tree
1284 type_for_mode (mode, unsignedp)
1285 enum machine_mode mode;
1286 int unsignedp;
1288 if (mode == TYPE_MODE (integer_type_node))
1289 return unsignedp ? unsigned_type_node : integer_type_node;
1291 if (mode == TYPE_MODE (signed_char_type_node))
1292 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1294 if (mode == TYPE_MODE (short_integer_type_node))
1295 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1297 if (mode == TYPE_MODE (long_integer_type_node))
1298 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1300 if (mode == TYPE_MODE (long_long_integer_type_node))
1301 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
1303 if (mode == TYPE_MODE (widest_integer_literal_type_node))
1304 return unsignedp ? widest_unsigned_literal_type_node
1305 : widest_integer_literal_type_node;
1307 if (mode == TYPE_MODE (intQI_type_node))
1308 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1310 if (mode == TYPE_MODE (intHI_type_node))
1311 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1313 if (mode == TYPE_MODE (intSI_type_node))
1314 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1316 if (mode == TYPE_MODE (intDI_type_node))
1317 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1319 #if HOST_BITS_PER_WIDE_INT >= 64
1320 if (mode == TYPE_MODE (intTI_type_node))
1321 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
1322 #endif
1324 if (mode == TYPE_MODE (float_type_node))
1325 return float_type_node;
1327 if (mode == TYPE_MODE (double_type_node))
1328 return double_type_node;
1330 if (mode == TYPE_MODE (long_double_type_node))
1331 return long_double_type_node;
1333 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
1334 return build_pointer_type (char_type_node);
1336 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
1337 return build_pointer_type (integer_type_node);
1339 #ifdef VECTOR_MODE_SUPPORTED_P
1340 if (mode == TYPE_MODE (V4SF_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1341 return V4SF_type_node;
1342 if (mode == TYPE_MODE (V4SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1343 return V4SI_type_node;
1344 if (mode == TYPE_MODE (V2SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1345 return V2SI_type_node;
1346 if (mode == TYPE_MODE (V4HI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1347 return V4HI_type_node;
1348 if (mode == TYPE_MODE (V8QI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1349 return V8QI_type_node;
1350 if (mode == TYPE_MODE (V2SF_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1351 return V2SF_type_node;
1352 #endif
1354 return 0;
1357 /* Return an unsigned type the same as TYPE in other respects. */
1358 tree
1359 unsigned_type (type)
1360 tree type;
1362 tree type1 = TYPE_MAIN_VARIANT (type);
1363 if (type1 == signed_char_type_node || type1 == char_type_node)
1364 return unsigned_char_type_node;
1365 if (type1 == integer_type_node)
1366 return unsigned_type_node;
1367 if (type1 == short_integer_type_node)
1368 return short_unsigned_type_node;
1369 if (type1 == long_integer_type_node)
1370 return long_unsigned_type_node;
1371 if (type1 == long_long_integer_type_node)
1372 return long_long_unsigned_type_node;
1373 if (type1 == widest_integer_literal_type_node)
1374 return widest_unsigned_literal_type_node;
1375 #if HOST_BITS_PER_WIDE_INT >= 64
1376 if (type1 == intTI_type_node)
1377 return unsigned_intTI_type_node;
1378 #endif
1379 if (type1 == intDI_type_node)
1380 return unsigned_intDI_type_node;
1381 if (type1 == intSI_type_node)
1382 return unsigned_intSI_type_node;
1383 if (type1 == intHI_type_node)
1384 return unsigned_intHI_type_node;
1385 if (type1 == intQI_type_node)
1386 return unsigned_intQI_type_node;
1388 return signed_or_unsigned_type (1, type);
1391 /* Return a signed type the same as TYPE in other respects. */
1393 tree
1394 signed_type (type)
1395 tree type;
1397 tree type1 = TYPE_MAIN_VARIANT (type);
1398 if (type1 == unsigned_char_type_node || type1 == char_type_node)
1399 return signed_char_type_node;
1400 if (type1 == unsigned_type_node)
1401 return integer_type_node;
1402 if (type1 == short_unsigned_type_node)
1403 return short_integer_type_node;
1404 if (type1 == long_unsigned_type_node)
1405 return long_integer_type_node;
1406 if (type1 == long_long_unsigned_type_node)
1407 return long_long_integer_type_node;
1408 if (type1 == widest_unsigned_literal_type_node)
1409 return widest_integer_literal_type_node;
1410 #if HOST_BITS_PER_WIDE_INT >= 64
1411 if (type1 == unsigned_intTI_type_node)
1412 return intTI_type_node;
1413 #endif
1414 if (type1 == unsigned_intDI_type_node)
1415 return intDI_type_node;
1416 if (type1 == unsigned_intSI_type_node)
1417 return intSI_type_node;
1418 if (type1 == unsigned_intHI_type_node)
1419 return intHI_type_node;
1420 if (type1 == unsigned_intQI_type_node)
1421 return intQI_type_node;
1423 return signed_or_unsigned_type (0, type);
1426 /* Return a type the same as TYPE except unsigned or
1427 signed according to UNSIGNEDP. */
1429 tree
1430 signed_or_unsigned_type (unsignedp, type)
1431 int unsignedp;
1432 tree type;
1434 if (! INTEGRAL_TYPE_P (type)
1435 || TREE_UNSIGNED (type) == unsignedp)
1436 return type;
1438 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
1439 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1440 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1441 return unsignedp ? unsigned_type_node : integer_type_node;
1442 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
1443 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1444 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
1445 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1446 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
1447 return (unsignedp ? long_long_unsigned_type_node
1448 : long_long_integer_type_node);
1449 if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
1450 return (unsignedp ? widest_unsigned_literal_type_node
1451 : widest_integer_literal_type_node);
1453 #if HOST_BITS_PER_WIDE_INT >= 64
1454 if (TYPE_PRECISION (type) == TYPE_PRECISION (intTI_type_node))
1455 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
1456 #endif
1457 if (TYPE_PRECISION (type) == TYPE_PRECISION (intDI_type_node))
1458 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1459 if (TYPE_PRECISION (type) == TYPE_PRECISION (intSI_type_node))
1460 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1461 if (TYPE_PRECISION (type) == TYPE_PRECISION (intHI_type_node))
1462 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1463 if (TYPE_PRECISION (type) == TYPE_PRECISION (intQI_type_node))
1464 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1466 return type;
1469 /* Return the minimum number of bits needed to represent VALUE in a
1470 signed or unsigned type, UNSIGNEDP says which. */
1472 unsigned int
1473 min_precision (value, unsignedp)
1474 tree value;
1475 int unsignedp;
1477 int log;
1479 /* If the value is negative, compute its negative minus 1. The latter
1480 adjustment is because the absolute value of the largest negative value
1481 is one larger than the largest positive value. This is equivalent to
1482 a bit-wise negation, so use that operation instead. */
1484 if (tree_int_cst_sgn (value) < 0)
1485 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
1487 /* Return the number of bits needed, taking into account the fact
1488 that we need one more bit for a signed than unsigned type. */
1490 if (integer_zerop (value))
1491 log = 0;
1492 else
1493 log = tree_floor_log2 (value);
1495 return log + 1 + ! unsignedp;
1498 /* Print an error message for invalid operands to arith operation CODE.
1499 NOP_EXPR is used as a special case (see truthvalue_conversion). */
1501 void
1502 binary_op_error (code)
1503 enum tree_code code;
1505 register const char *opname;
1507 switch (code)
1509 case NOP_EXPR:
1510 error ("invalid truth-value expression");
1511 return;
1513 case PLUS_EXPR:
1514 opname = "+"; break;
1515 case MINUS_EXPR:
1516 opname = "-"; break;
1517 case MULT_EXPR:
1518 opname = "*"; break;
1519 case MAX_EXPR:
1520 opname = "max"; break;
1521 case MIN_EXPR:
1522 opname = "min"; break;
1523 case EQ_EXPR:
1524 opname = "=="; break;
1525 case NE_EXPR:
1526 opname = "!="; break;
1527 case LE_EXPR:
1528 opname = "<="; break;
1529 case GE_EXPR:
1530 opname = ">="; break;
1531 case LT_EXPR:
1532 opname = "<"; break;
1533 case GT_EXPR:
1534 opname = ">"; break;
1535 case LSHIFT_EXPR:
1536 opname = "<<"; break;
1537 case RSHIFT_EXPR:
1538 opname = ">>"; break;
1539 case TRUNC_MOD_EXPR:
1540 case FLOOR_MOD_EXPR:
1541 opname = "%"; break;
1542 case TRUNC_DIV_EXPR:
1543 case FLOOR_DIV_EXPR:
1544 opname = "/"; break;
1545 case BIT_AND_EXPR:
1546 opname = "&"; break;
1547 case BIT_IOR_EXPR:
1548 opname = "|"; break;
1549 case TRUTH_ANDIF_EXPR:
1550 opname = "&&"; break;
1551 case TRUTH_ORIF_EXPR:
1552 opname = "||"; break;
1553 case BIT_XOR_EXPR:
1554 opname = "^"; break;
1555 case LROTATE_EXPR:
1556 case RROTATE_EXPR:
1557 opname = "rotate"; break;
1558 default:
1559 opname = "unknown"; break;
1561 error ("invalid operands to binary %s", opname);
1564 /* Subroutine of build_binary_op, used for comparison operations.
1565 See if the operands have both been converted from subword integer types
1566 and, if so, perhaps change them both back to their original type.
1567 This function is also responsible for converting the two operands
1568 to the proper common type for comparison.
1570 The arguments of this function are all pointers to local variables
1571 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
1572 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
1574 If this function returns nonzero, it means that the comparison has
1575 a constant value. What this function returns is an expression for
1576 that value. */
1578 tree
1579 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
1580 tree *op0_ptr, *op1_ptr;
1581 tree *restype_ptr;
1582 enum tree_code *rescode_ptr;
1584 register tree type;
1585 tree op0 = *op0_ptr;
1586 tree op1 = *op1_ptr;
1587 int unsignedp0, unsignedp1;
1588 int real1, real2;
1589 tree primop0, primop1;
1590 enum tree_code code = *rescode_ptr;
1592 /* Throw away any conversions to wider types
1593 already present in the operands. */
1595 primop0 = get_narrower (op0, &unsignedp0);
1596 primop1 = get_narrower (op1, &unsignedp1);
1598 /* Handle the case that OP0 does not *contain* a conversion
1599 but it *requires* conversion to FINAL_TYPE. */
1601 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
1602 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
1603 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
1604 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
1606 /* If one of the operands must be floated, we cannot optimize. */
1607 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
1608 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
1610 /* If first arg is constant, swap the args (changing operation
1611 so value is preserved), for canonicalization. Don't do this if
1612 the second arg is 0. */
1614 if (TREE_CONSTANT (primop0)
1615 && ! integer_zerop (primop1) && ! real_zerop (primop1))
1617 register tree tem = primop0;
1618 register int temi = unsignedp0;
1619 primop0 = primop1;
1620 primop1 = tem;
1621 tem = op0;
1622 op0 = op1;
1623 op1 = tem;
1624 *op0_ptr = op0;
1625 *op1_ptr = op1;
1626 unsignedp0 = unsignedp1;
1627 unsignedp1 = temi;
1628 temi = real1;
1629 real1 = real2;
1630 real2 = temi;
1632 switch (code)
1634 case LT_EXPR:
1635 code = GT_EXPR;
1636 break;
1637 case GT_EXPR:
1638 code = LT_EXPR;
1639 break;
1640 case LE_EXPR:
1641 code = GE_EXPR;
1642 break;
1643 case GE_EXPR:
1644 code = LE_EXPR;
1645 break;
1646 default:
1647 break;
1649 *rescode_ptr = code;
1652 /* If comparing an integer against a constant more bits wide,
1653 maybe we can deduce a value of 1 or 0 independent of the data.
1654 Or else truncate the constant now
1655 rather than extend the variable at run time.
1657 This is only interesting if the constant is the wider arg.
1658 Also, it is not safe if the constant is unsigned and the
1659 variable arg is signed, since in this case the variable
1660 would be sign-extended and then regarded as unsigned.
1661 Our technique fails in this case because the lowest/highest
1662 possible unsigned results don't follow naturally from the
1663 lowest/highest possible values of the variable operand.
1664 For just EQ_EXPR and NE_EXPR there is another technique that
1665 could be used: see if the constant can be faithfully represented
1666 in the other operand's type, by truncating it and reextending it
1667 and see if that preserves the constant's value. */
1669 if (!real1 && !real2
1670 && TREE_CODE (primop1) == INTEGER_CST
1671 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
1673 int min_gt, max_gt, min_lt, max_lt;
1674 tree maxval, minval;
1675 /* 1 if comparison is nominally unsigned. */
1676 int unsignedp = TREE_UNSIGNED (*restype_ptr);
1677 tree val;
1679 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
1681 /* If TYPE is an enumeration, then we need to get its min/max
1682 values from it's underlying integral type, not the enumerated
1683 type itself. */
1684 if (TREE_CODE (type) == ENUMERAL_TYPE)
1685 type = type_for_size (TYPE_PRECISION (type), unsignedp0);
1687 maxval = TYPE_MAX_VALUE (type);
1688 minval = TYPE_MIN_VALUE (type);
1690 if (unsignedp && !unsignedp0)
1691 *restype_ptr = signed_type (*restype_ptr);
1693 if (TREE_TYPE (primop1) != *restype_ptr)
1694 primop1 = convert (*restype_ptr, primop1);
1695 if (type != *restype_ptr)
1697 minval = convert (*restype_ptr, minval);
1698 maxval = convert (*restype_ptr, maxval);
1701 if (unsignedp && unsignedp0)
1703 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
1704 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
1705 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
1706 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
1708 else
1710 min_gt = INT_CST_LT (primop1, minval);
1711 max_gt = INT_CST_LT (primop1, maxval);
1712 min_lt = INT_CST_LT (minval, primop1);
1713 max_lt = INT_CST_LT (maxval, primop1);
1716 val = 0;
1717 /* This used to be a switch, but Genix compiler can't handle that. */
1718 if (code == NE_EXPR)
1720 if (max_lt || min_gt)
1721 val = boolean_true_node;
1723 else if (code == EQ_EXPR)
1725 if (max_lt || min_gt)
1726 val = boolean_false_node;
1728 else if (code == LT_EXPR)
1730 if (max_lt)
1731 val = boolean_true_node;
1732 if (!min_lt)
1733 val = boolean_false_node;
1735 else if (code == GT_EXPR)
1737 if (min_gt)
1738 val = boolean_true_node;
1739 if (!max_gt)
1740 val = boolean_false_node;
1742 else if (code == LE_EXPR)
1744 if (!max_gt)
1745 val = boolean_true_node;
1746 if (min_gt)
1747 val = boolean_false_node;
1749 else if (code == GE_EXPR)
1751 if (!min_lt)
1752 val = boolean_true_node;
1753 if (max_lt)
1754 val = boolean_false_node;
1757 /* If primop0 was sign-extended and unsigned comparison specd,
1758 we did a signed comparison above using the signed type bounds.
1759 But the comparison we output must be unsigned.
1761 Also, for inequalities, VAL is no good; but if the signed
1762 comparison had *any* fixed result, it follows that the
1763 unsigned comparison just tests the sign in reverse
1764 (positive values are LE, negative ones GE).
1765 So we can generate an unsigned comparison
1766 against an extreme value of the signed type. */
1768 if (unsignedp && !unsignedp0)
1770 if (val != 0)
1771 switch (code)
1773 case LT_EXPR:
1774 case GE_EXPR:
1775 primop1 = TYPE_MIN_VALUE (type);
1776 val = 0;
1777 break;
1779 case LE_EXPR:
1780 case GT_EXPR:
1781 primop1 = TYPE_MAX_VALUE (type);
1782 val = 0;
1783 break;
1785 default:
1786 break;
1788 type = unsigned_type (type);
1791 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
1793 /* This is the case of (char)x >?< 0x80, which people used to use
1794 expecting old C compilers to change the 0x80 into -0x80. */
1795 if (val == boolean_false_node)
1796 warning ("comparison is always false due to limited range of data type");
1797 if (val == boolean_true_node)
1798 warning ("comparison is always true due to limited range of data type");
1801 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
1803 /* This is the case of (unsigned char)x >?< -1 or < 0. */
1804 if (val == boolean_false_node)
1805 warning ("comparison is always false due to limited range of data type");
1806 if (val == boolean_true_node)
1807 warning ("comparison is always true due to limited range of data type");
1810 if (val != 0)
1812 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
1813 if (TREE_SIDE_EFFECTS (primop0))
1814 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
1815 return val;
1818 /* Value is not predetermined, but do the comparison
1819 in the type of the operand that is not constant.
1820 TYPE is already properly set. */
1822 else if (real1 && real2
1823 && (TYPE_PRECISION (TREE_TYPE (primop0))
1824 == TYPE_PRECISION (TREE_TYPE (primop1))))
1825 type = TREE_TYPE (primop0);
1827 /* If args' natural types are both narrower than nominal type
1828 and both extend in the same manner, compare them
1829 in the type of the wider arg.
1830 Otherwise must actually extend both to the nominal
1831 common type lest different ways of extending
1832 alter the result.
1833 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
1835 else if (unsignedp0 == unsignedp1 && real1 == real2
1836 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
1837 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
1839 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
1840 type = signed_or_unsigned_type (unsignedp0
1841 || TREE_UNSIGNED (*restype_ptr),
1842 type);
1843 /* Make sure shorter operand is extended the right way
1844 to match the longer operand. */
1845 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
1846 primop0);
1847 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
1848 primop1);
1850 else
1852 /* Here we must do the comparison on the nominal type
1853 using the args exactly as we received them. */
1854 type = *restype_ptr;
1855 primop0 = op0;
1856 primop1 = op1;
1858 if (!real1 && !real2 && integer_zerop (primop1)
1859 && TREE_UNSIGNED (*restype_ptr))
1861 tree value = 0;
1862 switch (code)
1864 case GE_EXPR:
1865 /* All unsigned values are >= 0, so we warn if extra warnings
1866 are requested. However, if OP0 is a constant that is
1867 >= 0, the signedness of the comparison isn't an issue,
1868 so suppress the warning. */
1869 if (extra_warnings && !in_system_header
1870 && ! (TREE_CODE (primop0) == INTEGER_CST
1871 && ! TREE_OVERFLOW (convert (signed_type (type),
1872 primop0))))
1873 warning ("comparison of unsigned expression >= 0 is always true");
1874 value = boolean_true_node;
1875 break;
1877 case LT_EXPR:
1878 if (extra_warnings && !in_system_header
1879 && ! (TREE_CODE (primop0) == INTEGER_CST
1880 && ! TREE_OVERFLOW (convert (signed_type (type),
1881 primop0))))
1882 warning ("comparison of unsigned expression < 0 is always false");
1883 value = boolean_false_node;
1884 break;
1886 default:
1887 break;
1890 if (value != 0)
1892 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
1893 if (TREE_SIDE_EFFECTS (primop0))
1894 return build (COMPOUND_EXPR, TREE_TYPE (value),
1895 primop0, value);
1896 return value;
1901 *op0_ptr = convert (type, primop0);
1902 *op1_ptr = convert (type, primop1);
1904 *restype_ptr = boolean_type_node;
1906 return 0;
1909 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
1910 or validate its data type for an `if' or `while' statement or ?..: exp.
1912 This preparation consists of taking the ordinary
1913 representation of an expression expr and producing a valid tree
1914 boolean expression describing whether expr is nonzero. We could
1915 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
1916 but we optimize comparisons, &&, ||, and !.
1918 The resulting type should always be `boolean_type_node'. */
1920 tree
1921 truthvalue_conversion (expr)
1922 tree expr;
1924 if (TREE_CODE (expr) == ERROR_MARK)
1925 return expr;
1927 #if 0 /* This appears to be wrong for C++. */
1928 /* These really should return error_mark_node after 2.4 is stable.
1929 But not all callers handle ERROR_MARK properly. */
1930 switch (TREE_CODE (TREE_TYPE (expr)))
1932 case RECORD_TYPE:
1933 error ("struct type value used where scalar is required");
1934 return boolean_false_node;
1936 case UNION_TYPE:
1937 error ("union type value used where scalar is required");
1938 return boolean_false_node;
1940 case ARRAY_TYPE:
1941 error ("array type value used where scalar is required");
1942 return boolean_false_node;
1944 default:
1945 break;
1947 #endif /* 0 */
1949 switch (TREE_CODE (expr))
1951 case EQ_EXPR:
1952 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
1953 case TRUTH_ANDIF_EXPR:
1954 case TRUTH_ORIF_EXPR:
1955 case TRUTH_AND_EXPR:
1956 case TRUTH_OR_EXPR:
1957 case TRUTH_XOR_EXPR:
1958 case TRUTH_NOT_EXPR:
1959 TREE_TYPE (expr) = boolean_type_node;
1960 return expr;
1962 case ERROR_MARK:
1963 return expr;
1965 case INTEGER_CST:
1966 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
1968 case REAL_CST:
1969 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
1971 case ADDR_EXPR:
1972 /* If we are taking the address of a external decl, it might be zero
1973 if it is weak, so we cannot optimize. */
1974 if (DECL_P (TREE_OPERAND (expr, 0))
1975 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
1976 break;
1978 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
1979 return build (COMPOUND_EXPR, boolean_type_node,
1980 TREE_OPERAND (expr, 0), boolean_true_node);
1981 else
1982 return boolean_true_node;
1984 case COMPLEX_EXPR:
1985 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
1986 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
1987 truthvalue_conversion (TREE_OPERAND (expr, 0)),
1988 truthvalue_conversion (TREE_OPERAND (expr, 1)),
1991 case NEGATE_EXPR:
1992 case ABS_EXPR:
1993 case FLOAT_EXPR:
1994 case FFS_EXPR:
1995 /* These don't change whether an object is non-zero or zero. */
1996 return truthvalue_conversion (TREE_OPERAND (expr, 0));
1998 case LROTATE_EXPR:
1999 case RROTATE_EXPR:
2000 /* These don't change whether an object is zero or non-zero, but
2001 we can't ignore them if their second arg has side-effects. */
2002 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2003 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2004 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2005 else
2006 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2008 case COND_EXPR:
2009 /* Distribute the conversion into the arms of a COND_EXPR. */
2010 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2011 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2012 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2014 case CONVERT_EXPR:
2015 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2016 since that affects how `default_conversion' will behave. */
2017 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2018 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2019 break;
2020 /* fall through... */
2021 case NOP_EXPR:
2022 /* If this is widening the argument, we can ignore it. */
2023 if (TYPE_PRECISION (TREE_TYPE (expr))
2024 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2025 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2026 break;
2028 case MINUS_EXPR:
2029 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2030 this case. */
2031 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2032 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2033 break;
2034 /* fall through... */
2035 case BIT_XOR_EXPR:
2036 /* This and MINUS_EXPR can be changed into a comparison of the
2037 two objects. */
2038 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2039 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2040 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2041 TREE_OPERAND (expr, 1), 1);
2042 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2043 fold (build1 (NOP_EXPR,
2044 TREE_TYPE (TREE_OPERAND (expr, 0)),
2045 TREE_OPERAND (expr, 1))), 1);
2047 case BIT_AND_EXPR:
2048 if (integer_onep (TREE_OPERAND (expr, 1))
2049 && TREE_TYPE (expr) != boolean_type_node)
2050 /* Using convert here would cause infinite recursion. */
2051 return build1 (NOP_EXPR, boolean_type_node, expr);
2052 break;
2054 case MODIFY_EXPR:
2055 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2056 warning ("suggest parentheses around assignment used as truth value");
2057 break;
2059 default:
2060 break;
2063 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2065 tree tem = save_expr (expr);
2066 return (build_binary_op
2067 ((TREE_SIDE_EFFECTS (expr)
2068 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2069 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
2070 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
2071 0));
2074 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2077 static tree builtin_function_2 PARAMS ((const char *, const char *, tree, tree,
2078 int, enum built_in_class, int, int,
2079 int));
2081 /* Make a variant type in the proper way for C/C++, propagating qualifiers
2082 down to the element type of an array. */
2084 tree
2085 c_build_qualified_type (type, type_quals)
2086 tree type;
2087 int type_quals;
2089 /* A restrict-qualified pointer type must be a pointer to object or
2090 incomplete type. Note that the use of POINTER_TYPE_P also allows
2091 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
2092 the C++ front-end also use POINTER_TYPE for pointer-to-member
2093 values, so even though it should be illegal to use `restrict'
2094 with such an entity we don't flag that here. Thus, special case
2095 code for that case is required in the C++ front-end. */
2096 if ((type_quals & TYPE_QUAL_RESTRICT)
2097 && (!POINTER_TYPE_P (type)
2098 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
2100 error ("invalid use of `restrict'");
2101 type_quals &= ~TYPE_QUAL_RESTRICT;
2104 if (TREE_CODE (type) == ARRAY_TYPE)
2105 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
2106 type_quals),
2107 TYPE_DOMAIN (type));
2108 return build_qualified_type (type, type_quals);
2111 /* Apply the TYPE_QUALS to the new DECL. */
2113 void
2114 c_apply_type_quals_to_decl (type_quals, decl)
2115 int type_quals;
2116 tree decl;
2118 if ((type_quals & TYPE_QUAL_CONST)
2119 || (TREE_TYPE (decl)
2120 && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE))
2121 TREE_READONLY (decl) = 1;
2122 if (type_quals & TYPE_QUAL_VOLATILE)
2124 TREE_SIDE_EFFECTS (decl) = 1;
2125 TREE_THIS_VOLATILE (decl) = 1;
2127 if (type_quals & TYPE_QUAL_RESTRICT)
2129 if (!TREE_TYPE (decl)
2130 || !POINTER_TYPE_P (TREE_TYPE (decl))
2131 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
2132 error ("invalid use of `restrict'");
2133 else if (flag_strict_aliasing)
2135 /* No two restricted pointers can point at the same thing.
2136 However, a restricted pointer can point at the same thing
2137 as an unrestricted pointer, if that unrestricted pointer
2138 is based on the restricted pointer. So, we make the
2139 alias set for the restricted pointer a subset of the
2140 alias set for the type pointed to by the type of the
2141 decl. */
2143 HOST_WIDE_INT pointed_to_alias_set
2144 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
2146 if (pointed_to_alias_set == 0)
2147 /* It's not legal to make a subset of alias set zero. */
2149 else
2151 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
2152 record_alias_subset (pointed_to_alias_set,
2153 DECL_POINTER_ALIAS_SET (decl));
2160 /* Return the typed-based alias set for T, which may be an expression
2161 or a type. Return -1 if we don't do anything special. */
2163 HOST_WIDE_INT
2164 lang_get_alias_set (t)
2165 tree t;
2167 tree u;
2169 /* We know nothing about vector types */
2170 if (TREE_CODE (t) == VECTOR_TYPE)
2171 return 0;
2173 /* Permit type-punning when accessing a union, provided the access
2174 is directly through the union. For example, this code does not
2175 permit taking the address of a union member and then storing
2176 through it. Even the type-punning allowed here is a GCC
2177 extension, albeit a common and useful one; the C standard says
2178 that such accesses have implementation-defined behavior. */
2179 for (u = t;
2180 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
2181 u = TREE_OPERAND (u, 0))
2182 if (TREE_CODE (u) == COMPONENT_REF
2183 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
2184 return 0;
2186 /* If this is a char *, the ANSI C standard says it can alias
2187 anything. Note that all references need do this. */
2188 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'r'
2189 && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
2190 && TYPE_PRECISION (TREE_TYPE (t)) == TYPE_PRECISION (char_type_node))
2191 return 0;
2193 /* That's all the expressions we handle specially. */
2194 if (! TYPE_P (t))
2195 return -1;
2197 /* The C standard specifically allows aliasing between signed and
2198 unsigned variants of the same type. We treat the signed
2199 variant as canonical. */
2200 if (TREE_CODE (t) == INTEGER_TYPE && TREE_UNSIGNED (t))
2202 tree t1 = signed_type (t);
2204 /* t1 == t can happen for boolean nodes which are always unsigned. */
2205 if (t1 != t)
2206 return get_alias_set (t1);
2208 else if (POINTER_TYPE_P (t))
2210 tree t1;
2212 /* Unfortunately, there is no canonical form of a pointer type.
2213 In particular, if we have `typedef int I', then `int *', and
2214 `I *' are different types. So, we have to pick a canonical
2215 representative. We do this below.
2217 Technically, this approach is actually more conservative that
2218 it needs to be. In particular, `const int *' and `int *'
2219 chould be in different alias sets, according to the C and C++
2220 standard, since their types are not the same, and so,
2221 technically, an `int **' and `const int **' cannot point at
2222 the same thing.
2224 But, the standard is wrong. In particular, this code is
2225 legal C++:
2227 int *ip;
2228 int **ipp = &ip;
2229 const int* const* cipp = &ipp;
2231 And, it doesn't make sense for that to be legal unless you
2232 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
2233 the pointed-to types. This issue has been reported to the
2234 C++ committee. */
2235 t1 = build_type_no_quals (t);
2236 if (t1 != t)
2237 return get_alias_set (t1);
2239 /* It's not yet safe to use alias sets for classes in C++ because
2240 the TYPE_FIELDs list for a class doesn't mention base classes. */
2241 else if (c_language == clk_cplusplus && AGGREGATE_TYPE_P (t))
2242 return 0;
2244 return -1;
2247 /* Implement the __alignof keyword: Return the minimum required
2248 alignment of TYPE, measured in bytes. */
2250 tree
2251 c_alignof (type)
2252 tree type;
2254 enum tree_code code = TREE_CODE (type);
2255 tree t;
2257 /* In C++, sizeof applies to the referent. Handle alignof the same way. */
2258 if (code == REFERENCE_TYPE)
2260 type = TREE_TYPE (type);
2261 code = TREE_CODE (type);
2264 if (code == FUNCTION_TYPE)
2265 t = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
2266 else if (code == VOID_TYPE || code == ERROR_MARK)
2267 t = size_one_node;
2268 else if (!COMPLETE_TYPE_P (type))
2270 error ("__alignof__ applied to an incomplete type");
2271 t = size_zero_node;
2273 else
2274 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
2276 return fold (build1 (NOP_EXPR, c_size_type_node, t));
2279 /* Implement the __alignof keyword: Return the minimum required
2280 alignment of EXPR, measured in bytes. For VAR_DECL's and
2281 FIELD_DECL's return DECL_ALIGN (which can be set from an
2282 "aligned" __attribute__ specification). */
2284 tree
2285 c_alignof_expr (expr)
2286 tree expr;
2288 tree t;
2290 if (TREE_CODE (expr) == VAR_DECL)
2291 t = size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
2293 else if (TREE_CODE (expr) == COMPONENT_REF
2294 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2296 error ("`__alignof' applied to a bit-field");
2297 t = size_one_node;
2299 else if (TREE_CODE (expr) == COMPONENT_REF
2300 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
2301 t = size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
2303 else if (TREE_CODE (expr) == INDIRECT_REF)
2305 tree t = TREE_OPERAND (expr, 0);
2306 tree best = t;
2307 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
2309 while (TREE_CODE (t) == NOP_EXPR
2310 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
2312 int thisalign;
2314 t = TREE_OPERAND (t, 0);
2315 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
2316 if (thisalign > bestalign)
2317 best = t, bestalign = thisalign;
2319 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
2321 else
2322 return c_alignof (TREE_TYPE (expr));
2324 return fold (build1 (NOP_EXPR, c_size_type_node, t));
2327 /* Give the specifications for the format attributes, used by C and all
2328 descendents. */
2330 static const struct attribute_spec c_format_attribute_table[] =
2332 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2333 { "format", 3, 3, false, true, true,
2334 handle_format_attribute },
2335 { "format_arg", 1, 1, false, true, true,
2336 handle_format_arg_attribute },
2337 { NULL, 0, 0, false, false, false, NULL }
2340 /* Build tree nodes and builtin functions common to both C and C++ language
2341 frontends. */
2343 void
2344 c_common_nodes_and_builtins ()
2346 enum builtin_type
2348 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
2349 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
2350 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
2351 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
2352 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
2353 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
2354 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
2355 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
2356 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
2357 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
2358 #include "builtin-types.def"
2359 #undef DEF_PRIMITIVE_TYPE
2360 #undef DEF_FUNCTION_TYPE_0
2361 #undef DEF_FUNCTION_TYPE_1
2362 #undef DEF_FUNCTION_TYPE_2
2363 #undef DEF_FUNCTION_TYPE_3
2364 #undef DEF_FUNCTION_TYPE_4
2365 #undef DEF_FUNCTION_TYPE_VAR_0
2366 #undef DEF_FUNCTION_TYPE_VAR_1
2367 #undef DEF_FUNCTION_TYPE_VAR_2
2368 #undef DEF_POINTER_TYPE
2369 BT_LAST
2372 typedef enum builtin_type builtin_type;
2374 tree builtin_types[(int)BT_LAST];
2375 int wchar_type_size;
2376 tree array_domain_type;
2377 /* Either char* or void*. */
2378 tree traditional_ptr_type_node;
2379 /* Either const char* or const void*. */
2380 tree traditional_cptr_type_node;
2381 tree traditional_len_type_node;
2382 tree va_list_ref_type_node;
2383 tree va_list_arg_type_node;
2385 /* We must initialize this before any builtin functions (which might have
2386 attributes) are declared. (c_common_lang_init is too late.) */
2387 format_attribute_table = c_format_attribute_table;
2389 /* Define `int' and `char' first so that dbx will output them first. */
2390 record_builtin_type (RID_INT, NULL, integer_type_node);
2391 record_builtin_type (RID_CHAR, "char", char_type_node);
2393 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
2394 "unsigned long", "long long unsigned" and "unsigned short" were in C++
2395 but not C. Are the conditionals here needed? */
2396 if (c_language == clk_cplusplus)
2397 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
2398 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
2399 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
2400 record_builtin_type (RID_MAX, "long unsigned int",
2401 long_unsigned_type_node);
2402 if (c_language == clk_cplusplus)
2403 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
2404 record_builtin_type (RID_MAX, "long long int",
2405 long_long_integer_type_node);
2406 record_builtin_type (RID_MAX, "long long unsigned int",
2407 long_long_unsigned_type_node);
2408 if (c_language == clk_cplusplus)
2409 record_builtin_type (RID_MAX, "long long unsigned",
2410 long_long_unsigned_type_node);
2411 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
2412 record_builtin_type (RID_MAX, "short unsigned int",
2413 short_unsigned_type_node);
2414 if (c_language == clk_cplusplus)
2415 record_builtin_type (RID_MAX, "unsigned short",
2416 short_unsigned_type_node);
2418 /* Define both `signed char' and `unsigned char'. */
2419 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
2420 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
2422 /* These are types that type_for_size and type_for_mode use. */
2423 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intQI_type_node));
2424 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intHI_type_node));
2425 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intSI_type_node));
2426 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intDI_type_node));
2427 #if HOST_BITS_PER_WIDE_INT >= 64
2428 pushdecl (build_decl (TYPE_DECL, get_identifier ("__int128_t"), intTI_type_node));
2429 #endif
2430 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intQI_type_node));
2431 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intHI_type_node));
2432 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intSI_type_node));
2433 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intDI_type_node));
2434 #if HOST_BITS_PER_WIDE_INT >= 64
2435 pushdecl (build_decl (TYPE_DECL, get_identifier ("__uint128_t"), unsigned_intTI_type_node));
2436 #endif
2438 /* Create the widest literal types. */
2439 widest_integer_literal_type_node
2440 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
2441 pushdecl (build_decl (TYPE_DECL, NULL_TREE,
2442 widest_integer_literal_type_node));
2444 widest_unsigned_literal_type_node
2445 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
2446 pushdecl (build_decl (TYPE_DECL, NULL_TREE,
2447 widest_unsigned_literal_type_node));
2449 /* `unsigned long' is the standard type for sizeof.
2450 Note that stddef.h uses `unsigned long',
2451 and this must agree, even if long and int are the same size. */
2452 c_size_type_node =
2453 TREE_TYPE (identifier_global_value (get_identifier (SIZE_TYPE)));
2454 signed_size_type_node = signed_type (c_size_type_node);
2455 if (flag_traditional)
2456 c_size_type_node = signed_size_type_node;
2457 set_sizetype (c_size_type_node);
2459 build_common_tree_nodes_2 (flag_short_double);
2461 record_builtin_type (RID_FLOAT, NULL, float_type_node);
2462 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
2463 record_builtin_type (RID_MAX, "long double", long_double_type_node);
2465 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex int"),
2466 complex_integer_type_node));
2467 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex float"),
2468 complex_float_type_node));
2469 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex double"),
2470 complex_double_type_node));
2471 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex long double"),
2472 complex_long_double_type_node));
2474 record_builtin_type (RID_VOID, NULL, void_type_node);
2476 void_zero_node = build_int_2 (0, 0);
2477 TREE_TYPE (void_zero_node) = void_type_node;
2479 void_list_node = build_void_list_node ();
2481 /* Make a type to be the domain of a few array types
2482 whose domains don't really matter.
2483 200 is small enough that it always fits in size_t
2484 and large enough that it can hold most function names for the
2485 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
2486 array_domain_type = build_index_type (size_int (200));
2488 /* Make a type for arrays of characters.
2489 With luck nothing will ever really depend on the length of this
2490 array type. */
2491 char_array_type_node
2492 = build_array_type (char_type_node, array_domain_type);
2494 /* Likewise for arrays of ints. */
2495 int_array_type_node
2496 = build_array_type (integer_type_node, array_domain_type);
2498 string_type_node = build_pointer_type (char_type_node);
2499 const_string_type_node
2500 = build_pointer_type (build_qualified_type
2501 (char_type_node, TYPE_QUAL_CONST));
2503 traditional_ptr_type_node = ((flag_traditional &&
2504 c_language != clk_cplusplus)
2505 ? string_type_node : ptr_type_node);
2506 traditional_cptr_type_node = ((flag_traditional &&
2507 c_language != clk_cplusplus)
2508 ? const_string_type_node : const_ptr_type_node);
2510 (*targetm.init_builtins) ();
2512 /* This is special for C++ so functions can be overloaded. */
2513 wchar_type_node = get_identifier (flag_short_wchar
2514 ? "short unsigned int"
2515 : WCHAR_TYPE);
2516 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
2517 wchar_type_size = TYPE_PRECISION (wchar_type_node);
2518 if (c_language == clk_cplusplus)
2520 if (TREE_UNSIGNED (wchar_type_node))
2521 wchar_type_node = make_unsigned_type (wchar_type_size);
2522 else
2523 wchar_type_node = make_signed_type (wchar_type_size);
2524 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
2526 else
2528 signed_wchar_type_node = signed_type (wchar_type_node);
2529 unsigned_wchar_type_node = unsigned_type (wchar_type_node);
2532 /* This is for wide string constants. */
2533 wchar_array_type_node
2534 = build_array_type (wchar_type_node, array_domain_type);
2536 wint_type_node =
2537 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
2539 intmax_type_node =
2540 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
2541 uintmax_type_node =
2542 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
2544 default_function_type = build_function_type (integer_type_node, NULL_TREE);
2545 ptrdiff_type_node
2546 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
2547 unsigned_ptrdiff_type_node = unsigned_type (ptrdiff_type_node);
2549 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
2550 va_list_type_node));
2552 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"),
2553 ptrdiff_type_node));
2555 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"),
2556 sizetype));
2558 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
2560 va_list_arg_type_node = va_list_ref_type_node =
2561 build_pointer_type (TREE_TYPE (va_list_type_node));
2563 else
2565 va_list_arg_type_node = va_list_type_node;
2566 va_list_ref_type_node = build_reference_type (va_list_type_node);
2569 traditional_len_type_node = ((flag_traditional &&
2570 c_language != clk_cplusplus)
2571 ? integer_type_node : sizetype);
2573 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
2574 builtin_types[(int) ENUM] = VALUE;
2575 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
2576 builtin_types[(int) ENUM] \
2577 = build_function_type (builtin_types[(int) RETURN], \
2578 void_list_node);
2579 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
2580 builtin_types[(int) ENUM] \
2581 = build_function_type (builtin_types[(int) RETURN], \
2582 tree_cons (NULL_TREE, \
2583 builtin_types[(int) ARG1], \
2584 void_list_node));
2585 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
2586 builtin_types[(int) ENUM] \
2587 = build_function_type \
2588 (builtin_types[(int) RETURN], \
2589 tree_cons (NULL_TREE, \
2590 builtin_types[(int) ARG1], \
2591 tree_cons (NULL_TREE, \
2592 builtin_types[(int) ARG2], \
2593 void_list_node)));
2594 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
2595 builtin_types[(int) ENUM] \
2596 = build_function_type \
2597 (builtin_types[(int) RETURN], \
2598 tree_cons (NULL_TREE, \
2599 builtin_types[(int) ARG1], \
2600 tree_cons (NULL_TREE, \
2601 builtin_types[(int) ARG2], \
2602 tree_cons (NULL_TREE, \
2603 builtin_types[(int) ARG3], \
2604 void_list_node))));
2605 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
2606 builtin_types[(int) ENUM] \
2607 = build_function_type \
2608 (builtin_types[(int) RETURN], \
2609 tree_cons (NULL_TREE, \
2610 builtin_types[(int) ARG1], \
2611 tree_cons (NULL_TREE, \
2612 builtin_types[(int) ARG2], \
2613 tree_cons \
2614 (NULL_TREE, \
2615 builtin_types[(int) ARG3], \
2616 tree_cons (NULL_TREE, \
2617 builtin_types[(int) ARG4], \
2618 void_list_node)))));
2619 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
2620 builtin_types[(int) ENUM] \
2621 = build_function_type (builtin_types[(int) RETURN], NULL_TREE);
2622 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
2623 builtin_types[(int) ENUM] \
2624 = build_function_type (builtin_types[(int) RETURN], \
2625 tree_cons (NULL_TREE, \
2626 builtin_types[(int) ARG1], \
2627 NULL_TREE));
2629 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
2630 builtin_types[(int) ENUM] \
2631 = build_function_type \
2632 (builtin_types[(int) RETURN], \
2633 tree_cons (NULL_TREE, \
2634 builtin_types[(int) ARG1], \
2635 tree_cons (NULL_TREE, \
2636 builtin_types[(int) ARG2], \
2637 NULL_TREE)));
2638 #define DEF_POINTER_TYPE(ENUM, TYPE) \
2639 builtin_types[(int) ENUM] \
2640 = build_pointer_type (builtin_types[(int) TYPE]);
2641 #include "builtin-types.def"
2642 #undef DEF_PRIMITIVE_TYPE
2643 #undef DEF_FUNCTION_TYPE_1
2644 #undef DEF_FUNCTION_TYPE_2
2645 #undef DEF_FUNCTION_TYPE_3
2646 #undef DEF_FUNCTION_TYPE_4
2647 #undef DEF_FUNCTION_TYPE_VAR_0
2648 #undef DEF_FUNCTION_TYPE_VAR_1
2649 #undef DEF_POINTER_TYPE
2651 #define DEF_BUILTIN(ENUM, NAME, CLASS, \
2652 TYPE, LIBTYPE, BOTH_P, FALLBACK_P, NONANSI_P) \
2653 if (NAME) \
2655 tree decl; \
2657 if (strncmp (NAME, "__builtin_", strlen ("__builtin_")) != 0) \
2658 abort (); \
2660 if (!BOTH_P) \
2661 decl = builtin_function (NAME, builtin_types[TYPE], ENUM, \
2662 CLASS, \
2663 (FALLBACK_P \
2664 ? (NAME + strlen ("__builtin_")) \
2665 : NULL)); \
2666 else \
2667 decl = builtin_function_2 (NAME, \
2668 NAME + strlen ("__builtin_"), \
2669 builtin_types[TYPE], \
2670 builtin_types[LIBTYPE], \
2671 ENUM, \
2672 CLASS, \
2673 FALLBACK_P, \
2674 NONANSI_P, \
2675 /*noreturn_p=*/0); \
2677 built_in_decls[(int) ENUM] = decl; \
2679 #include "builtins.def"
2680 #undef DEF_BUILTIN
2682 /* Declare _exit and _Exit just to mark them as non-returning. */
2683 builtin_function_2 (NULL, "_exit", NULL_TREE,
2684 builtin_types[BT_FN_VOID_INT],
2685 0, NOT_BUILT_IN, 0, 1, 1);
2686 builtin_function_2 (NULL, "_Exit", NULL_TREE,
2687 builtin_types[BT_FN_VOID_INT],
2688 0, NOT_BUILT_IN, 0, !flag_isoc99, 1);
2690 /* Declare these functions non-returning
2691 to avoid spurious "control drops through" warnings. */
2692 builtin_function_2 (NULL, "abort",
2693 NULL_TREE, ((c_language == clk_cplusplus)
2694 ? builtin_types[BT_FN_VOID]
2695 : builtin_types[BT_FN_VOID_VAR]),
2696 0, NOT_BUILT_IN, 0, 0, 1);
2698 builtin_function_2 (NULL, "exit",
2699 NULL_TREE, ((c_language == clk_cplusplus)
2700 ? builtin_types[BT_FN_VOID_INT]
2701 : builtin_types[BT_FN_VOID_VAR]),
2702 0, NOT_BUILT_IN, 0, 0, 1);
2704 main_identifier_node = get_identifier ("main");
2706 /* ??? Perhaps there's a better place to do this. But it is related
2707 to __builtin_va_arg, so it isn't that off-the-wall. */
2708 lang_type_promotes_to = simple_type_promotes_to;
2711 tree
2712 build_va_arg (expr, type)
2713 tree expr, type;
2715 return build1 (VA_ARG_EXPR, type, expr);
2719 /* Possibly define a builtin function with one or two names. BUILTIN_NAME
2720 is an __builtin_-prefixed name; NAME is the ordinary name; one or both
2721 of these may be NULL (though both being NULL is useless).
2722 BUILTIN_TYPE is the type of the __builtin_-prefixed function;
2723 TYPE is the type of the function with the ordinary name. These
2724 may differ if the ordinary name is declared with a looser type to avoid
2725 conflicts with headers. FUNCTION_CODE and CLASS are as for
2726 builtin_function. If LIBRARY_NAME_P is nonzero, NAME is passed as
2727 the LIBRARY_NAME parameter to builtin_function when declaring BUILTIN_NAME.
2728 If NONANSI_P is nonzero, the name NAME is treated as a non-ANSI name; if
2729 NORETURN_P is nonzero, the function is marked as non-returning.
2730 Returns the declaration of BUILTIN_NAME, if any, otherwise
2731 the declaration of NAME. Does not declare NAME if flag_no_builtin,
2732 or if NONANSI_P and flag_no_nonansi_builtin. */
2734 static tree
2735 builtin_function_2 (builtin_name, name, builtin_type, type, function_code,
2736 class, library_name_p, nonansi_p, noreturn_p)
2737 const char *builtin_name;
2738 const char *name;
2739 tree builtin_type;
2740 tree type;
2741 int function_code;
2742 enum built_in_class class;
2743 int library_name_p;
2744 int nonansi_p;
2745 int noreturn_p;
2747 tree bdecl = NULL_TREE;
2748 tree decl = NULL_TREE;
2749 if (builtin_name != 0)
2751 bdecl = builtin_function (builtin_name, builtin_type, function_code,
2752 class, library_name_p ? name : NULL);
2753 if (noreturn_p)
2755 TREE_THIS_VOLATILE (bdecl) = 1;
2756 TREE_SIDE_EFFECTS (bdecl) = 1;
2759 if (name != 0 && !flag_no_builtin && !(nonansi_p && flag_no_nonansi_builtin))
2761 decl = builtin_function (name, type, function_code, class, NULL);
2762 if (nonansi_p)
2763 DECL_BUILT_IN_NONANSI (decl) = 1;
2764 if (noreturn_p)
2766 TREE_THIS_VOLATILE (decl) = 1;
2767 TREE_SIDE_EFFECTS (decl) = 1;
2770 return (bdecl != 0 ? bdecl : decl);
2773 /* Nonzero if the type T promotes to int. This is (nearly) the
2774 integral promotions defined in ISO C99 6.3.1.1/2. */
2776 bool
2777 c_promoting_integer_type_p (t)
2778 tree t;
2780 switch (TREE_CODE (t))
2782 case INTEGER_TYPE:
2783 return (TYPE_MAIN_VARIANT (t) == char_type_node
2784 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
2785 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
2786 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
2787 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
2788 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
2790 case ENUMERAL_TYPE:
2791 /* ??? Technically all enumerations not larger than an int
2792 promote to an int. But this is used along code paths
2793 that only want to notice a size change. */
2794 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
2796 case BOOLEAN_TYPE:
2797 return 1;
2799 default:
2800 return 0;
2804 /* Given a type, apply default promotions wrt unnamed function arguments
2805 and return the new type. Return NULL_TREE if no change. */
2806 /* ??? There is a function of the same name in the C++ front end that
2807 does something similar, but is more thorough and does not return NULL
2808 if no change. We could perhaps share code, but it would make the
2809 self_promoting_type property harder to identify. */
2811 tree
2812 simple_type_promotes_to (type)
2813 tree type;
2815 if (TYPE_MAIN_VARIANT (type) == float_type_node)
2816 return double_type_node;
2818 if (c_promoting_integer_type_p (type))
2820 /* Traditionally, unsignedness is preserved in default promotions.
2821 Also preserve unsignedness if not really getting any wider. */
2822 if (TREE_UNSIGNED (type)
2823 && (flag_traditional
2824 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
2825 return unsigned_type_node;
2826 return integer_type_node;
2829 return NULL_TREE;
2832 /* Return 1 if PARMS specifies a fixed number of parameters
2833 and none of their types is affected by default promotions. */
2836 self_promoting_args_p (parms)
2837 tree parms;
2839 register tree t;
2840 for (t = parms; t; t = TREE_CHAIN (t))
2842 register tree type = TREE_VALUE (t);
2844 if (TREE_CHAIN (t) == 0 && type != void_type_node)
2845 return 0;
2847 if (type == 0)
2848 return 0;
2850 if (TYPE_MAIN_VARIANT (type) == float_type_node)
2851 return 0;
2853 if (c_promoting_integer_type_p (type))
2854 return 0;
2856 return 1;
2859 /* Recursively examines the array elements of TYPE, until a non-array
2860 element type is found. */
2862 tree
2863 strip_array_types (type)
2864 tree type;
2866 while (TREE_CODE (type) == ARRAY_TYPE)
2867 type = TREE_TYPE (type);
2869 return type;
2872 /* Recognize certain built-in functions so we can make tree-codes
2873 other than CALL_EXPR. We do this when it enables fold-const.c
2874 to do something useful. */
2875 /* ??? By rights this should go in builtins.c, but only C and C++
2876 implement build_{binary,unary}_op. Not exactly sure what bits
2877 of functionality are actually needed from those functions, or
2878 where the similar functionality exists in the other front ends. */
2880 tree
2881 expand_tree_builtin (function, params, coerced_params)
2882 tree function, params, coerced_params;
2884 enum tree_code code;
2886 if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL)
2887 return NULL_TREE;
2889 switch (DECL_FUNCTION_CODE (function))
2891 case BUILT_IN_ABS:
2892 case BUILT_IN_LABS:
2893 case BUILT_IN_LLABS:
2894 case BUILT_IN_IMAXABS:
2895 case BUILT_IN_FABS:
2896 case BUILT_IN_FABSL:
2897 case BUILT_IN_FABSF:
2898 if (coerced_params == 0)
2899 return integer_zero_node;
2900 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
2902 case BUILT_IN_CONJ:
2903 case BUILT_IN_CONJF:
2904 case BUILT_IN_CONJL:
2905 if (coerced_params == 0)
2906 return integer_zero_node;
2907 return build_unary_op (CONJ_EXPR, TREE_VALUE (coerced_params), 0);
2909 case BUILT_IN_CREAL:
2910 case BUILT_IN_CREALF:
2911 case BUILT_IN_CREALL:
2912 if (coerced_params == 0)
2913 return integer_zero_node;
2914 return build_unary_op (REALPART_EXPR, TREE_VALUE (coerced_params), 0);
2916 case BUILT_IN_CIMAG:
2917 case BUILT_IN_CIMAGF:
2918 case BUILT_IN_CIMAGL:
2919 if (coerced_params == 0)
2920 return integer_zero_node;
2921 return build_unary_op (IMAGPART_EXPR, TREE_VALUE (coerced_params), 0);
2923 case BUILT_IN_ISGREATER:
2924 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
2925 code = UNLE_EXPR;
2926 else
2927 code = LE_EXPR;
2928 goto unordered_cmp;
2930 case BUILT_IN_ISGREATEREQUAL:
2931 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
2932 code = UNLT_EXPR;
2933 else
2934 code = LT_EXPR;
2935 goto unordered_cmp;
2937 case BUILT_IN_ISLESS:
2938 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
2939 code = UNGE_EXPR;
2940 else
2941 code = GE_EXPR;
2942 goto unordered_cmp;
2944 case BUILT_IN_ISLESSEQUAL:
2945 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
2946 code = UNGT_EXPR;
2947 else
2948 code = GT_EXPR;
2949 goto unordered_cmp;
2951 case BUILT_IN_ISLESSGREATER:
2952 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
2953 code = UNEQ_EXPR;
2954 else
2955 code = EQ_EXPR;
2956 goto unordered_cmp;
2958 case BUILT_IN_ISUNORDERED:
2959 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
2960 return integer_zero_node;
2961 code = UNORDERED_EXPR;
2962 goto unordered_cmp;
2964 unordered_cmp:
2966 tree arg0, arg1;
2968 if (params == 0
2969 || TREE_CHAIN (params) == 0)
2971 error ("too few arguments to function `%s'",
2972 IDENTIFIER_POINTER (DECL_NAME (function)));
2973 return error_mark_node;
2975 else if (TREE_CHAIN (TREE_CHAIN (params)) != 0)
2977 error ("too many arguments to function `%s'",
2978 IDENTIFIER_POINTER (DECL_NAME (function)));
2979 return error_mark_node;
2982 arg0 = TREE_VALUE (params);
2983 arg1 = TREE_VALUE (TREE_CHAIN (params));
2984 arg0 = build_binary_op (code, arg0, arg1, 0);
2985 if (code != UNORDERED_EXPR)
2986 arg0 = build_unary_op (TRUTH_NOT_EXPR, arg0, 0);
2987 return arg0;
2989 break;
2991 default:
2992 break;
2995 return NULL_TREE;
2998 /* Returns non-zero if CODE is the code for a statement. */
3001 statement_code_p (code)
3002 enum tree_code code;
3004 switch (code)
3006 case EXPR_STMT:
3007 case COMPOUND_STMT:
3008 case DECL_STMT:
3009 case IF_STMT:
3010 case FOR_STMT:
3011 case WHILE_STMT:
3012 case DO_STMT:
3013 case RETURN_STMT:
3014 case BREAK_STMT:
3015 case CONTINUE_STMT:
3016 case SCOPE_STMT:
3017 case SWITCH_STMT:
3018 case GOTO_STMT:
3019 case LABEL_STMT:
3020 case ASM_STMT:
3021 case CASE_LABEL:
3022 return 1;
3024 default:
3025 if (lang_statement_code_p)
3026 return (*lang_statement_code_p) (code);
3027 return 0;
3031 /* Walk the statement tree, rooted at *tp. Apply FUNC to all the
3032 sub-trees of *TP in a pre-order traversal. FUNC is called with the
3033 DATA and the address of each sub-tree. If FUNC returns a non-NULL
3034 value, the traversal is aborted, and the value returned by FUNC is
3035 returned. If FUNC sets WALK_SUBTREES to zero, then the subtrees of
3036 the node being visited are not walked.
3038 We don't need a without_duplicates variant of this one because the
3039 statement tree is a tree, not a graph. */
3041 tree
3042 walk_stmt_tree (tp, func, data)
3043 tree *tp;
3044 walk_tree_fn func;
3045 void *data;
3047 enum tree_code code;
3048 int walk_subtrees;
3049 tree result;
3050 int i, len;
3052 #define WALK_SUBTREE(NODE) \
3053 do \
3055 result = walk_stmt_tree (&(NODE), func, data); \
3056 if (result) \
3057 return result; \
3059 while (0)
3061 /* Skip empty subtrees. */
3062 if (!*tp)
3063 return NULL_TREE;
3065 /* Skip subtrees below non-statement nodes. */
3066 if (!statement_code_p (TREE_CODE (*tp)))
3067 return NULL_TREE;
3069 /* Call the function. */
3070 walk_subtrees = 1;
3071 result = (*func) (tp, &walk_subtrees, data);
3073 /* If we found something, return it. */
3074 if (result)
3075 return result;
3077 /* FUNC may have modified the tree, recheck that we're looking at a
3078 statement node. */
3079 code = TREE_CODE (*tp);
3080 if (!statement_code_p (code))
3081 return NULL_TREE;
3083 /* Visit the subtrees unless FUNC decided that there was nothing
3084 interesting below this point in the tree. */
3085 if (walk_subtrees)
3087 /* Walk over all the sub-trees of this operand. Statement nodes
3088 never contain RTL, and we needn't worry about TARGET_EXPRs. */
3089 len = TREE_CODE_LENGTH (code);
3091 /* Go through the subtrees. We need to do this in forward order so
3092 that the scope of a FOR_EXPR is handled properly. */
3093 for (i = 0; i < len; ++i)
3094 WALK_SUBTREE (TREE_OPERAND (*tp, i));
3097 /* Finally visit the chain. This can be tail-recursion optimized if
3098 we write it this way. */
3099 return walk_stmt_tree (&TREE_CHAIN (*tp), func, data);
3101 #undef WALK_SUBTREE
3104 /* Used to compare case labels. K1 and K2 are actually tree nodes
3105 representing case labels, or NULL_TREE for a `default' label.
3106 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
3107 K2, and 0 if K1 and K2 are equal. */
3110 case_compare (k1, k2)
3111 splay_tree_key k1;
3112 splay_tree_key k2;
3114 /* Consider a NULL key (such as arises with a `default' label) to be
3115 smaller than anything else. */
3116 if (!k1)
3117 return k2 ? -1 : 0;
3118 else if (!k2)
3119 return k1 ? 1 : 0;
3121 return tree_int_cst_compare ((tree) k1, (tree) k2);
3124 /* Process a case label for the range LOW_VALUE ... HIGH_VALUE. If
3125 LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is
3126 actually a `default' label. If only HIGH_VALUE is NULL_TREE, then
3127 case label was declared using the usual C/C++ syntax, rather than
3128 the GNU case range extension. CASES is a tree containing all the
3129 case ranges processed so far; COND is the condition for the
3130 switch-statement itself. Returns the CASE_LABEL created, or
3131 ERROR_MARK_NODE if no CASE_LABEL is created. */
3133 tree
3134 c_add_case_label (cases, cond, low_value, high_value)
3135 splay_tree cases;
3136 tree cond;
3137 tree low_value;
3138 tree high_value;
3140 tree type;
3141 tree label;
3142 tree case_label;
3143 splay_tree_node node;
3145 /* Create the LABEL_DECL itself. */
3146 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
3147 DECL_CONTEXT (label) = current_function_decl;
3149 /* If there was an error processing the switch condition, bail now
3150 before we get more confused. */
3151 if (!cond || cond == error_mark_node)
3153 /* Add a label anyhow so that the back-end doesn't think that
3154 the beginning of the switch is unreachable. */
3155 if (!cases->root)
3156 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
3157 return error_mark_node;
3160 if ((low_value && TREE_TYPE (low_value)
3161 && POINTER_TYPE_P (TREE_TYPE (low_value)))
3162 || (high_value && TREE_TYPE (high_value)
3163 && POINTER_TYPE_P (TREE_TYPE (high_value))))
3164 error ("pointers are not permitted as case values");
3166 /* Case ranges are a GNU extension. */
3167 if (high_value && pedantic)
3169 if (c_language == clk_cplusplus)
3170 pedwarn ("ISO C++ forbids range expressions in switch statements");
3171 else
3172 pedwarn ("ISO C forbids range expressions in switch statements");
3175 type = TREE_TYPE (cond);
3176 if (low_value)
3178 low_value = check_case_value (low_value);
3179 low_value = convert_and_check (type, low_value);
3181 if (high_value)
3183 high_value = check_case_value (high_value);
3184 high_value = convert_and_check (type, high_value);
3187 /* If an error has occurred, bail out now. */
3188 if (low_value == error_mark_node || high_value == error_mark_node)
3190 if (!cases->root)
3191 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
3192 return error_mark_node;
3195 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
3196 really a case range, even though it was written that way. Remove
3197 the HIGH_VALUE to simplify later processing. */
3198 if (tree_int_cst_equal (low_value, high_value))
3199 high_value = NULL_TREE;
3200 if (low_value && high_value
3201 && !tree_int_cst_lt (low_value, high_value))
3202 warning ("empty range specified");
3204 /* Look up the LOW_VALUE in the table of case labels we already
3205 have. */
3206 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
3207 /* If there was not an exact match, check for overlapping ranges.
3208 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
3209 that's a `default' label and the only overlap is an exact match. */
3210 if (!node && (low_value || high_value))
3212 splay_tree_node low_bound;
3213 splay_tree_node high_bound;
3215 /* Even though there wasn't an exact match, there might be an
3216 overlap between this case range and another case range.
3217 Since we've (inductively) not allowed any overlapping case
3218 ranges, we simply need to find the greatest low case label
3219 that is smaller that LOW_VALUE, and the smallest low case
3220 label that is greater than LOW_VALUE. If there is an overlap
3221 it will occur in one of these two ranges. */
3222 low_bound = splay_tree_predecessor (cases,
3223 (splay_tree_key) low_value);
3224 high_bound = splay_tree_successor (cases,
3225 (splay_tree_key) low_value);
3227 /* Check to see if the LOW_BOUND overlaps. It is smaller than
3228 the LOW_VALUE, so there is no need to check unless the
3229 LOW_BOUND is in fact itself a case range. */
3230 if (low_bound
3231 && CASE_HIGH ((tree) low_bound->value)
3232 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
3233 low_value) >= 0)
3234 node = low_bound;
3235 /* Check to see if the HIGH_BOUND overlaps. The low end of that
3236 range is bigger than the low end of the current range, so we
3237 are only interested if the current range is a real range, and
3238 not an ordinary case label. */
3239 else if (high_bound
3240 && high_value
3241 && (tree_int_cst_compare ((tree) high_bound->key,
3242 high_value)
3243 <= 0))
3244 node = high_bound;
3246 /* If there was an overlap, issue an error. */
3247 if (node)
3249 tree duplicate = CASE_LABEL_DECL ((tree) node->value);
3251 if (high_value)
3253 error ("duplicate (or overlapping) case value");
3254 error_with_decl (duplicate,
3255 "this is the first entry overlapping that value");
3257 else if (low_value)
3259 error ("duplicate case value") ;
3260 error_with_decl (duplicate, "previously used here");
3262 else
3264 error ("multiple default labels in one switch");
3265 error_with_decl (duplicate, "this is the first default label");
3267 if (!cases->root)
3268 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
3271 /* Add a CASE_LABEL to the statement-tree. */
3272 case_label = add_stmt (build_case_label (low_value, high_value, label));
3273 /* Register this case label in the splay tree. */
3274 splay_tree_insert (cases,
3275 (splay_tree_key) low_value,
3276 (splay_tree_value) case_label);
3278 return case_label;
3281 /* Finish an expression taking the address of LABEL. Returns an
3282 expression for the address. */
3284 tree
3285 finish_label_address_expr (label)
3286 tree label;
3288 tree result;
3290 if (pedantic)
3292 if (c_language == clk_cplusplus)
3293 pedwarn ("ISO C++ forbids taking the address of a label");
3294 else
3295 pedwarn ("ISO C forbids taking the address of a label");
3298 label = lookup_label (label);
3299 if (label == NULL_TREE)
3300 result = null_pointer_node;
3301 else
3303 TREE_USED (label) = 1;
3304 result = build1 (ADDR_EXPR, ptr_type_node, label);
3305 TREE_CONSTANT (result) = 1;
3306 /* The current function in not necessarily uninlinable.
3307 Computed gotos are incompatible with inlining, but the value
3308 here could be used only in a diagnostic, for example. */
3311 return result;
3314 /* Mark P (a stmt_tree) for GC. The use of a `void *' for the
3315 parameter allows this function to be used as a GC-marking
3316 function. */
3318 void
3319 mark_stmt_tree (p)
3320 void *p;
3322 stmt_tree st = (stmt_tree) p;
3324 ggc_mark_tree (st->x_last_stmt);
3325 ggc_mark_tree (st->x_last_expr_type);
3328 /* Mark LD for GC. */
3330 void
3331 c_mark_lang_decl (c)
3332 struct c_lang_decl *c;
3334 ggc_mark_tree (c->saved_tree);
3337 /* Mark F for GC. */
3339 void
3340 mark_c_language_function (f)
3341 struct language_function *f;
3343 if (!f)
3344 return;
3346 mark_stmt_tree (&f->x_stmt_tree);
3347 ggc_mark_tree (f->x_scope_stmt_stack);
3350 /* Hook used by expand_expr to expand language-specific tree codes. */
3353 c_expand_expr (exp, target, tmode, modifier)
3354 tree exp;
3355 rtx target;
3356 enum machine_mode tmode;
3357 enum expand_modifier modifier;
3359 switch (TREE_CODE (exp))
3361 case STMT_EXPR:
3363 tree rtl_expr;
3364 rtx result;
3366 /* Since expand_expr_stmt calls free_temp_slots after every
3367 expression statement, we must call push_temp_slots here.
3368 Otherwise, any temporaries in use now would be considered
3369 out-of-scope after the first EXPR_STMT from within the
3370 STMT_EXPR. */
3371 push_temp_slots ();
3372 rtl_expr = expand_start_stmt_expr ();
3373 expand_stmt (STMT_EXPR_STMT (exp));
3374 expand_end_stmt_expr (rtl_expr);
3375 result = expand_expr (rtl_expr, target, tmode, modifier);
3376 pop_temp_slots ();
3377 return result;
3379 break;
3381 case CALL_EXPR:
3383 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
3384 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
3385 == FUNCTION_DECL)
3386 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
3387 && (DECL_BUILT_IN_CLASS (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
3388 == BUILT_IN_FRONTEND))
3389 return c_expand_builtin (exp, target, tmode, modifier);
3390 else
3391 abort();
3393 break;
3395 default:
3396 abort ();
3399 abort ();
3400 return NULL;
3403 /* Hook used by safe_from_p to handle language-specific tree codes. */
3406 c_safe_from_p (target, exp)
3407 rtx target;
3408 tree exp;
3410 /* We can see statements here when processing the body of a
3411 statement-expression. For a declaration statement declaring a
3412 variable, look at the variable's initializer. */
3413 if (TREE_CODE (exp) == DECL_STMT)
3415 tree decl = DECL_STMT_DECL (exp);
3417 if (TREE_CODE (decl) == VAR_DECL
3418 && DECL_INITIAL (decl)
3419 && !safe_from_p (target, DECL_INITIAL (decl), /*top_p=*/0))
3420 return 0;
3423 /* For any statement, we must follow the statement-chain. */
3424 if (statement_code_p (TREE_CODE (exp)) && TREE_CHAIN (exp))
3425 return safe_from_p (target, TREE_CHAIN (exp), /*top_p=*/0);
3427 /* Assume everything else is safe. */
3428 return 1;
3431 /* Hook used by unsafe_for_reeval to handle language-specific tree codes. */
3434 c_unsafe_for_reeval (exp)
3435 tree exp;
3437 /* Statement expressions may not be reevaluated. */
3438 if (TREE_CODE (exp) == STMT_EXPR)
3439 return 2;
3441 /* Walk all other expressions. */
3442 return -1;
3445 /* Tree code classes. */
3447 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
3449 static char c_tree_code_type[] = {
3450 'x',
3451 #include "c-common.def"
3453 #undef DEFTREECODE
3455 /* Table indexed by tree code giving number of expression
3456 operands beyond the fixed part of the node structure.
3457 Not used for types or decls. */
3459 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
3461 static int c_tree_code_length[] = {
3463 #include "c-common.def"
3465 #undef DEFTREECODE
3467 /* Names of tree components.
3468 Used for printing out the tree and error messages. */
3469 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
3471 static const char *const c_tree_code_name[] = {
3472 "@@dummy",
3473 #include "c-common.def"
3475 #undef DEFTREECODE
3477 /* Adds the tree codes specific to the C front end to the list of all
3478 tree codes. */
3480 void
3481 add_c_tree_codes ()
3483 memcpy (tree_code_type + (int) LAST_AND_UNUSED_TREE_CODE,
3484 c_tree_code_type,
3485 (int)LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE);
3486 memcpy (tree_code_length + (int) LAST_AND_UNUSED_TREE_CODE,
3487 c_tree_code_length,
3488 (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (int));
3489 memcpy (tree_code_name + (int) LAST_AND_UNUSED_TREE_CODE,
3490 c_tree_code_name,
3491 (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (char *));
3492 lang_unsafe_for_reeval = c_unsafe_for_reeval;
3495 #define CALLED_AS_BUILT_IN(NODE) \
3496 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
3498 static rtx
3499 c_expand_builtin (exp, target, tmode, modifier)
3500 tree exp;
3501 rtx target;
3502 enum machine_mode tmode;
3503 enum expand_modifier modifier;
3505 tree type = TREE_TYPE (exp);
3506 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
3507 tree arglist = TREE_OPERAND (exp, 1);
3508 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
3509 enum tree_code code = TREE_CODE (exp);
3510 const int ignore = (target == const0_rtx
3511 || ((code == NON_LVALUE_EXPR || code == NOP_EXPR
3512 || code == CONVERT_EXPR || code == REFERENCE_EXPR
3513 || code == COND_EXPR)
3514 && TREE_CODE (type) == VOID_TYPE));
3516 if (! optimize && ! CALLED_AS_BUILT_IN (fndecl))
3517 return expand_call (exp, target, ignore);
3519 switch (fcode)
3521 case BUILT_IN_PRINTF:
3522 target = c_expand_builtin_printf (arglist, target, tmode,
3523 modifier, ignore);
3524 if (target)
3525 return target;
3526 break;
3528 case BUILT_IN_FPRINTF:
3529 target = c_expand_builtin_fprintf (arglist, target, tmode,
3530 modifier, ignore);
3531 if (target)
3532 return target;
3533 break;
3535 default: /* just do library call, if unknown builtin */
3536 error ("built-in function `%s' not currently supported",
3537 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
3540 /* The switch statement above can drop through to cause the function
3541 to be called normally. */
3542 return expand_call (exp, target, ignore);
3545 /* Check an arglist to *printf for problems. The arglist should start
3546 at the format specifier, with the remaining arguments immediately
3547 following it. */
3548 static int
3549 is_valid_printf_arglist (arglist)
3550 tree arglist;
3552 /* Save this value so we can restore it later. */
3553 const int SAVE_pedantic = pedantic;
3554 int diagnostic_occurred = 0;
3555 tree attrs;
3557 /* Set this to a known value so the user setting won't affect code
3558 generation. */
3559 pedantic = 1;
3560 /* Check to make sure there are no format specifier errors. */
3561 attrs = tree_cons (get_identifier ("format"),
3562 tree_cons (NULL_TREE,
3563 get_identifier ("printf"),
3564 tree_cons (NULL_TREE,
3565 integer_one_node,
3566 tree_cons (NULL_TREE,
3567 build_int_2 (2, 0),
3568 NULL_TREE))),
3569 NULL_TREE);
3570 check_function_format (&diagnostic_occurred, attrs, arglist);
3572 /* Restore the value of `pedantic'. */
3573 pedantic = SAVE_pedantic;
3575 /* If calling `check_function_format_ptr' produces a warning, we
3576 return false, otherwise we return true. */
3577 return ! diagnostic_occurred;
3580 /* If the arguments passed to printf are suitable for optimizations,
3581 we attempt to transform the call. */
3582 static rtx
3583 c_expand_builtin_printf (arglist, target, tmode, modifier, ignore)
3584 tree arglist;
3585 rtx target;
3586 enum machine_mode tmode;
3587 enum expand_modifier modifier;
3588 int ignore;
3590 tree fn_putchar = built_in_decls[BUILT_IN_PUTCHAR],
3591 fn_puts = built_in_decls[BUILT_IN_PUTS];
3592 tree fn, format_arg, stripped_string;
3594 /* If the return value is used, or the replacement _DECL isn't
3595 initialized, don't do the transformation. */
3596 if (!ignore || !fn_putchar || !fn_puts)
3597 return 0;
3599 /* Verify the required arguments in the original call. */
3600 if (arglist == 0
3601 || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE))
3602 return 0;
3604 /* Check the specifier vs. the parameters. */
3605 if (!is_valid_printf_arglist (arglist))
3606 return 0;
3608 format_arg = TREE_VALUE (arglist);
3609 stripped_string = format_arg;
3610 STRIP_NOPS (stripped_string);
3611 if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
3612 stripped_string = TREE_OPERAND (stripped_string, 0);
3614 /* If the format specifier isn't a STRING_CST, punt. */
3615 if (TREE_CODE (stripped_string) != STRING_CST)
3616 return 0;
3618 /* OK! We can attempt optimization. */
3620 /* If the format specifier was "%s\n", call __builtin_puts(arg2). */
3621 if (strcmp (TREE_STRING_POINTER (stripped_string), "%s\n") == 0)
3623 arglist = TREE_CHAIN (arglist);
3624 fn = fn_puts;
3626 /* If the format specifier was "%c", call __builtin_putchar (arg2). */
3627 else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0)
3629 arglist = TREE_CHAIN (arglist);
3630 fn = fn_putchar;
3632 else
3634 /* We can't handle anything else with % args or %% ... yet. */
3635 if (strchr (TREE_STRING_POINTER (stripped_string), '%'))
3636 return 0;
3638 /* If the resulting constant string has a length of 1, call
3639 putchar. Note, TREE_STRING_LENGTH includes the terminating
3640 NULL in its count. */
3641 if (TREE_STRING_LENGTH (stripped_string) == 2)
3643 /* Given printf("c"), (where c is any one character,)
3644 convert "c"[0] to an int and pass that to the replacement
3645 function. */
3646 arglist = build_int_2 (TREE_STRING_POINTER (stripped_string)[0], 0);
3647 arglist = build_tree_list (NULL_TREE, arglist);
3649 fn = fn_putchar;
3651 /* If the resulting constant was "string\n", call
3652 __builtin_puts("string"). Ensure "string" has at least one
3653 character besides the trailing \n. Note, TREE_STRING_LENGTH
3654 includes the terminating NULL in its count. */
3655 else if (TREE_STRING_LENGTH (stripped_string) > 2
3656 && TREE_STRING_POINTER (stripped_string)
3657 [TREE_STRING_LENGTH (stripped_string) - 2] == '\n')
3659 /* Create a NULL-terminated string that's one char shorter
3660 than the original, stripping off the trailing '\n'. */
3661 const int newlen = TREE_STRING_LENGTH (stripped_string) - 1;
3662 char *newstr = (char *) alloca (newlen);
3663 memcpy (newstr, TREE_STRING_POINTER (stripped_string), newlen - 1);
3664 newstr[newlen - 1] = 0;
3666 arglist = combine_strings (build_string (newlen, newstr));
3667 arglist = build_tree_list (NULL_TREE, arglist);
3668 fn = fn_puts;
3670 else
3671 /* We'd like to arrange to call fputs(string) here, but we
3672 need stdout and don't have a way to get it ... yet. */
3673 return 0;
3676 return expand_expr (build_function_call (fn, arglist),
3677 (ignore ? const0_rtx : target),
3678 tmode, modifier);
3681 /* If the arguments passed to fprintf are suitable for optimizations,
3682 we attempt to transform the call. */
3683 static rtx
3684 c_expand_builtin_fprintf (arglist, target, tmode, modifier, ignore)
3685 tree arglist;
3686 rtx target;
3687 enum machine_mode tmode;
3688 enum expand_modifier modifier;
3689 int ignore;
3691 tree fn_fputc = built_in_decls[BUILT_IN_FPUTC],
3692 fn_fputs = built_in_decls[BUILT_IN_FPUTS];
3693 tree fn, format_arg, stripped_string;
3695 /* If the return value is used, or the replacement _DECL isn't
3696 initialized, don't do the transformation. */
3697 if (!ignore || !fn_fputc || !fn_fputs)
3698 return 0;
3700 /* Verify the required arguments in the original call. */
3701 if (arglist == 0
3702 || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE)
3703 || (TREE_CHAIN (arglist) == 0)
3704 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist)))) !=
3705 POINTER_TYPE))
3706 return 0;
3708 /* Check the specifier vs. the parameters. */
3709 if (!is_valid_printf_arglist (TREE_CHAIN (arglist)))
3710 return 0;
3712 format_arg = TREE_VALUE (TREE_CHAIN (arglist));
3713 stripped_string = format_arg;
3714 STRIP_NOPS (stripped_string);
3715 if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
3716 stripped_string = TREE_OPERAND (stripped_string, 0);
3718 /* If the format specifier isn't a STRING_CST, punt. */
3719 if (TREE_CODE (stripped_string) != STRING_CST)
3720 return 0;
3722 /* OK! We can attempt optimization. */
3724 /* If the format specifier was "%s", call __builtin_fputs(arg3, arg1). */
3725 if (strcmp (TREE_STRING_POINTER (stripped_string), "%s") == 0)
3727 tree newarglist = build_tree_list (NULL_TREE, TREE_VALUE (arglist));
3728 arglist = tree_cons (NULL_TREE,
3729 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))),
3730 newarglist);
3731 fn = fn_fputs;
3733 /* If the format specifier was "%c", call __builtin_fputc (arg3, arg1). */
3734 else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0)
3736 tree newarglist = build_tree_list (NULL_TREE, TREE_VALUE (arglist));
3737 arglist = tree_cons (NULL_TREE,
3738 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))),
3739 newarglist);
3740 fn = fn_fputc;
3742 else
3744 /* We can't handle anything else with % args or %% ... yet. */
3745 if (strchr (TREE_STRING_POINTER (stripped_string), '%'))
3746 return 0;
3748 /* When "string" doesn't contain %, replace all cases of
3749 fprintf(stream,string) with fputs(string,stream). The fputs
3750 builtin will take take of special cases like length==1. */
3751 arglist = tree_cons (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)),
3752 build_tree_list (NULL_TREE, TREE_VALUE (arglist)));
3753 fn = fn_fputs;
3756 return expand_expr (build_function_call (fn, arglist),
3757 (ignore ? const0_rtx : target),
3758 tmode, modifier);
3762 /* Given a boolean expression ARG, return a tree representing an increment
3763 or decrement (as indicated by CODE) of ARG. The front end must check for
3764 invalid cases (e.g., decrement in C++). */
3765 tree
3766 boolean_increment (code, arg)
3767 enum tree_code code;
3768 tree arg;
3770 tree val;
3771 tree true_res = (c_language == clk_cplusplus
3772 ? boolean_true_node
3773 : c_bool_true_node);
3774 arg = stabilize_reference (arg);
3775 switch (code)
3777 case PREINCREMENT_EXPR:
3778 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
3779 break;
3780 case POSTINCREMENT_EXPR:
3781 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
3782 arg = save_expr (arg);
3783 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
3784 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
3785 break;
3786 case PREDECREMENT_EXPR:
3787 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
3788 break;
3789 case POSTDECREMENT_EXPR:
3790 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
3791 arg = save_expr (arg);
3792 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
3793 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
3794 break;
3795 default:
3796 abort ();
3798 TREE_SIDE_EFFECTS (val) = 1;
3799 return val;
3802 /* Handle C and C++ default attributes. */
3804 enum built_in_attribute
3806 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
3807 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
3808 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
3809 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
3810 #define DEF_FN_ATTR(NAME, ATTRS, PREDICATE) /* No entry needed in enum. */
3811 #include "builtin-attrs.def"
3812 #undef DEF_ATTR_NULL_TREE
3813 #undef DEF_ATTR_INT
3814 #undef DEF_ATTR_IDENT
3815 #undef DEF_ATTR_TREE_LIST
3816 #undef DEF_FN_ATTR
3817 ATTR_LAST
3820 static tree built_in_attributes[(int) ATTR_LAST];
3822 static bool c_attrs_initialized = false;
3824 static void c_init_attributes PARAMS ((void));
3826 /* Do the parts of lang_init common to C and C++. */
3827 void
3828 c_common_lang_init ()
3830 /* If still "unspecified", make it match -fbounded-pointers. */
3831 if (flag_bounds_check < 0)
3832 flag_bounds_check = flag_bounded_pointers;
3834 /* Special format checking options don't work without -Wformat; warn if
3835 they are used. */
3836 if (warn_format_y2k && !warn_format)
3837 warning ("-Wformat-y2k ignored without -Wformat");
3838 if (warn_format_extra_args && !warn_format)
3839 warning ("-Wformat-extra-args ignored without -Wformat");
3840 if (warn_format_nonliteral && !warn_format)
3841 warning ("-Wformat-nonliteral ignored without -Wformat");
3842 if (warn_format_security && !warn_format)
3843 warning ("-Wformat-security ignored without -Wformat");
3844 if (warn_missing_format_attribute && !warn_format)
3845 warning ("-Wmissing-format-attribute ignored without -Wformat");
3847 if (!c_attrs_initialized)
3848 c_init_attributes ();
3851 static void
3852 c_init_attributes ()
3854 /* Fill in the built_in_attributes array. */
3855 #define DEF_ATTR_NULL_TREE(ENUM) \
3856 built_in_attributes[(int) ENUM] = NULL_TREE;
3857 #define DEF_ATTR_INT(ENUM, VALUE) \
3858 built_in_attributes[(int) ENUM] = build_int_2 (VALUE, VALUE < 0 ? -1 : 0);
3859 #define DEF_ATTR_IDENT(ENUM, STRING) \
3860 built_in_attributes[(int) ENUM] = get_identifier (STRING);
3861 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
3862 built_in_attributes[(int) ENUM] \
3863 = tree_cons (built_in_attributes[(int) PURPOSE], \
3864 built_in_attributes[(int) VALUE], \
3865 built_in_attributes[(int) CHAIN]);
3866 #define DEF_FN_ATTR(NAME, ATTRS, PREDICATE) /* No initialization needed. */
3867 #include "builtin-attrs.def"
3868 #undef DEF_ATTR_NULL_TREE
3869 #undef DEF_ATTR_INT
3870 #undef DEF_ATTR_IDENT
3871 #undef DEF_ATTR_TREE_LIST
3872 #undef DEF_FN_ATTR
3873 ggc_add_tree_root (built_in_attributes, (int) ATTR_LAST);
3874 c_attrs_initialized = true;
3877 /* Depending on the name of DECL, apply default attributes to it. */
3879 void
3880 c_common_insert_default_attributes (decl)
3881 tree decl;
3883 tree name = DECL_NAME (decl);
3885 if (!c_attrs_initialized)
3886 c_init_attributes ();
3888 #define DEF_ATTR_NULL_TREE(ENUM) /* Nothing needed after initialization. */
3889 #define DEF_ATTR_INT(ENUM, VALUE)
3890 #define DEF_ATTR_IDENT(ENUM, STRING)
3891 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN)
3892 #define DEF_FN_ATTR(NAME, ATTRS, PREDICATE) \
3893 if ((PREDICATE) && name == built_in_attributes[(int) NAME]) \
3894 decl_attributes (&decl, built_in_attributes[(int) ATTRS], \
3895 ATTR_FLAG_BUILT_IN);
3896 #include "builtin-attrs.def"
3897 #undef DEF_ATTR_NULL_TREE
3898 #undef DEF_ATTR_INT
3899 #undef DEF_ATTR_IDENT
3900 #undef DEF_ATTR_TREE_LIST
3901 #undef DEF_FN_ATTR