Makefile.in (endfile.o): Add dependency on config.h.
[official-gcc.git] / gcc / c-common.c
blobf037ecc9ae32d17d4e8e2fd8c370d3ad16368875
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 GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 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 cpp_reader *parse_in; /* Declared in c-lex.h. */
38 #undef WCHAR_TYPE_SIZE
39 #define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node)
41 /* We let tm.h override the types used here, to handle trivial differences
42 such as the choice of unsigned int or long unsigned int for size_t.
43 When machines start needing nontrivial differences in the size type,
44 it would be best to do something here to figure out automatically
45 from other information what type to use. */
47 #ifndef SIZE_TYPE
48 #define SIZE_TYPE "long unsigned int"
49 #endif
51 #ifndef WCHAR_TYPE
52 #define WCHAR_TYPE "int"
53 #endif
55 #ifndef PTRDIFF_TYPE
56 #define PTRDIFF_TYPE "long int"
57 #endif
59 #ifndef WINT_TYPE
60 #define WINT_TYPE "unsigned int"
61 #endif
63 #ifndef INTMAX_TYPE
64 #define INTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
65 ? "int" \
66 : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
67 ? "long int" \
68 : "long long int"))
69 #endif
71 #ifndef UINTMAX_TYPE
72 #define UINTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
73 ? "unsigned int" \
74 : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
75 ? "long unsigned int" \
76 : "long long unsigned int"))
77 #endif
79 /* The following symbols are subsumed in the c_global_trees array, and
80 listed here individually for documentation purposes.
82 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
84 tree short_integer_type_node;
85 tree long_integer_type_node;
86 tree long_long_integer_type_node;
88 tree short_unsigned_type_node;
89 tree long_unsigned_type_node;
90 tree long_long_unsigned_type_node;
92 tree boolean_type_node;
93 tree boolean_false_node;
94 tree boolean_true_node;
96 tree ptrdiff_type_node;
98 tree unsigned_char_type_node;
99 tree signed_char_type_node;
100 tree wchar_type_node;
101 tree signed_wchar_type_node;
102 tree unsigned_wchar_type_node;
104 tree float_type_node;
105 tree double_type_node;
106 tree long_double_type_node;
108 tree complex_integer_type_node;
109 tree complex_float_type_node;
110 tree complex_double_type_node;
111 tree complex_long_double_type_node;
113 tree intQI_type_node;
114 tree intHI_type_node;
115 tree intSI_type_node;
116 tree intDI_type_node;
117 tree intTI_type_node;
119 tree unsigned_intQI_type_node;
120 tree unsigned_intHI_type_node;
121 tree unsigned_intSI_type_node;
122 tree unsigned_intDI_type_node;
123 tree unsigned_intTI_type_node;
125 tree widest_integer_literal_type_node;
126 tree widest_unsigned_literal_type_node;
128 Nodes for types `void *' and `const void *'.
130 tree ptr_type_node, const_ptr_type_node;
132 Nodes for types `char *' and `const char *'.
134 tree string_type_node, const_string_type_node;
136 Type `char[SOMENUMBER]'.
137 Used when an array of char is needed and the size is irrelevant.
139 tree char_array_type_node;
141 Type `int[SOMENUMBER]' or something like it.
142 Used when an array of int needed and the size is irrelevant.
144 tree int_array_type_node;
146 Type `wchar_t[SOMENUMBER]' or something like it.
147 Used when a wide string literal is created.
149 tree wchar_array_type_node;
151 Type `int ()' -- used for implicit declaration of functions.
153 tree default_function_type;
155 Function types `int (int)', etc.
157 tree int_ftype_int;
158 tree void_ftype;
159 tree void_ftype_ptr;
160 tree int_ftype_int;
161 tree ptr_ftype_sizetype;
163 A VOID_TYPE node, packaged in a TREE_LIST.
165 tree void_list_node;
167 The lazily created VAR_DECLS for __FUNCTION__, __PRETTY_FUNCTION__,
168 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
169 VAR_DECLS, but C++ does.)
171 tree function_name_decl_node;
172 tree pretty_function_name_declnode;
173 tree c99_function_name_decl_node;
175 Stack of nested function name VAR_DECLs.
177 tree saved_function_name_decls;
181 tree c_global_trees[CTI_MAX];
183 /* Nonzero means don't recognize the non-ANSI builtin functions. */
185 int flag_no_builtin;
187 /* Nonzero means don't recognize the non-ANSI builtin functions.
188 -ansi sets this. */
190 int flag_no_nonansi_builtin;
192 /* Nonzero means give `double' the same size as `float'. */
194 int flag_short_double;
196 /* Nonzero means give `wchar_t' the same size as `short'. */
198 int flag_short_wchar;
200 /* If non-NULL, dump the tree structure for the entire translation
201 unit to this file. */
203 const char *flag_dump_translation_unit;
205 /* Nonzero means warn about possible violations of sequence point rules. */
207 int warn_sequence_point;
209 /* The elements of `ridpointers' are identifier nodes for the reserved
210 type names and storage classes. It is indexed by a RID_... value. */
211 tree *ridpointers;
213 tree (*make_fname_decl) PARAMS ((tree, int));
215 /* If non-NULL, the address of a language-specific function that
216 returns 1 for language-specific statement codes. */
217 int (*lang_statement_code_p) PARAMS ((enum tree_code));
219 /* If non-NULL, the address of a language-specific function that takes
220 any action required right before expand_function_end is called. */
221 void (*lang_expand_function_end) PARAMS ((void));
223 /* If this variable is defined to a non-NULL value, it will be called
224 after the file has been completely parsed. */
225 void (*back_end_hook) PARAMS ((tree));
227 /* Nonzero means the expression being parsed will never be evaluated.
228 This is a count, since unevaluated expressions can nest. */
229 int skip_evaluation;
231 enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
232 A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
233 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
234 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS, A_MALLOC,
235 A_NO_LIMIT_STACK, A_PURE};
237 /* Information about how a function name is generated. */
238 struct fname_var_t
240 tree *decl; /* pointer to the VAR_DECL. */
241 unsigned rid; /* RID number for the identifier. */
242 int pretty; /* How pretty is it? */
245 /* The three ways of getting then name of the current function. */
247 const struct fname_var_t fname_vars[] =
249 /* C99 compliant __func__, must be first. */
250 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
251 /* GCC __FUNCTION__ compliant. */
252 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
253 /* GCC __PRETTY_FUNCTION__ compliant. */
254 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
255 {NULL, 0, 0},
258 static void add_attribute PARAMS ((enum attrs, const char *,
259 int, int, int));
260 static void init_attributes PARAMS ((void));
261 static int default_valid_lang_attribute PARAMS ((tree, tree, tree, tree));
262 static int constant_fits_type_p PARAMS ((tree, tree));
264 /* Keep a stack of if statements. We record the number of compound
265 statements seen up to the if keyword, as well as the line number
266 and file of the if. If a potentially ambiguous else is seen, that
267 fact is recorded; the warning is issued when we can be sure that
268 the enclosing if statement does not have an else branch. */
269 typedef struct
271 int compstmt_count;
272 int line;
273 const char *file;
274 int needs_warning;
275 tree if_stmt;
276 } if_elt;
278 static if_elt *if_stack;
280 /* Amount of space in the if statement stack. */
281 static int if_stack_space = 0;
283 /* Stack pointer. */
284 static int if_stack_pointer = 0;
286 /* Record the start of an if-then, and record the start of it
287 for ambiguous else detection. */
289 void
290 c_expand_start_cond (cond, compstmt_count)
291 tree cond;
292 int compstmt_count;
294 tree if_stmt;
296 /* Make sure there is enough space on the stack. */
297 if (if_stack_space == 0)
299 if_stack_space = 10;
300 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
302 else if (if_stack_space == if_stack_pointer)
304 if_stack_space += 10;
305 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
308 if_stmt = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
309 IF_COND (if_stmt) = cond;
310 add_stmt (if_stmt);
312 /* Record this if statement. */
313 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
314 if_stack[if_stack_pointer].file = input_filename;
315 if_stack[if_stack_pointer].line = lineno;
316 if_stack[if_stack_pointer].needs_warning = 0;
317 if_stack[if_stack_pointer].if_stmt = if_stmt;
318 if_stack_pointer++;
321 /* Called after the then-clause for an if-statement is processed. */
323 void
324 c_finish_then ()
326 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
327 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
330 /* Record the end of an if-then. Optionally warn if a nested
331 if statement had an ambiguous else clause. */
333 void
334 c_expand_end_cond ()
336 if_stack_pointer--;
337 if (if_stack[if_stack_pointer].needs_warning)
338 warning_with_file_and_line (if_stack[if_stack_pointer].file,
339 if_stack[if_stack_pointer].line,
340 "suggest explicit braces to avoid ambiguous `else'");
341 last_expr_type = NULL_TREE;
344 /* Called between the then-clause and the else-clause
345 of an if-then-else. */
347 void
348 c_expand_start_else ()
350 /* An ambiguous else warning must be generated for the enclosing if
351 statement, unless we see an else branch for that one, too. */
352 if (warn_parentheses
353 && if_stack_pointer > 1
354 && (if_stack[if_stack_pointer - 1].compstmt_count
355 == if_stack[if_stack_pointer - 2].compstmt_count))
356 if_stack[if_stack_pointer - 2].needs_warning = 1;
358 /* Even if a nested if statement had an else branch, it can't be
359 ambiguous if this one also has an else. So don't warn in that
360 case. Also don't warn for any if statements nested in this else. */
361 if_stack[if_stack_pointer - 1].needs_warning = 0;
362 if_stack[if_stack_pointer - 1].compstmt_count--;
365 /* Called after the else-clause for an if-statement is processed. */
367 void
368 c_finish_else ()
370 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
371 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
374 /* Push current bindings for the function name VAR_DECLS. */
376 void
377 start_fname_decls ()
379 unsigned ix;
380 tree saved = NULL_TREE;
382 for (ix = 0; fname_vars[ix].decl; ix++)
384 tree decl = *fname_vars[ix].decl;
386 if (decl)
388 saved = tree_cons (decl, build_int_2 (ix, 0), saved);
389 *fname_vars[ix].decl = NULL_TREE;
392 if (saved || saved_function_name_decls)
393 /* Normally they'll have been NULL, so only push if we've got a
394 stack, or they are non-NULL. */
395 saved_function_name_decls = tree_cons (saved, NULL_TREE,
396 saved_function_name_decls);
399 /* Finish up the current bindings, adding them into the
400 current function's statement tree. This is done by wrapping the
401 function's body in a COMPOUND_STMT containing these decls too. This
402 must be done _before_ finish_stmt_tree is called. If there is no
403 current function, we must be at file scope and no statements are
404 involved. Pop the previous bindings. */
406 void
407 finish_fname_decls ()
409 unsigned ix;
410 tree body = NULL_TREE;
411 tree stack = saved_function_name_decls;
413 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
414 body = chainon (TREE_VALUE (stack), body);
416 if (body)
418 /* They were called into existance, so add to statement tree. */
419 body = chainon (body,
420 TREE_CHAIN (DECL_SAVED_TREE (current_function_decl)));
421 body = build_stmt (COMPOUND_STMT, body);
423 COMPOUND_STMT_NO_SCOPE (body) = 1;
424 TREE_CHAIN (DECL_SAVED_TREE (current_function_decl)) = body;
427 for (ix = 0; fname_vars[ix].decl; ix++)
428 *fname_vars[ix].decl = NULL_TREE;
430 if (stack)
432 /* We had saved values, restore them. */
433 tree saved;
435 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
437 tree decl = TREE_PURPOSE (saved);
438 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
440 *fname_vars[ix].decl = decl;
442 stack = TREE_CHAIN (stack);
444 saved_function_name_decls = stack;
447 /* Return the text name of the current function, suitable prettified
448 by PRETTY_P. */
450 const char *
451 fname_as_string (pretty_p)
452 int pretty_p;
454 const char *name = NULL;
456 if (pretty_p)
457 name = (current_function_decl
458 ? (*decl_printable_name) (current_function_decl, 2)
459 : "top level");
460 else if (current_function_decl && DECL_NAME (current_function_decl))
461 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
462 else
463 name = "";
464 return name;
467 /* Return the text name of the current function, formatted as
468 required by the supplied RID value. */
470 const char *
471 fname_string (rid)
472 unsigned rid;
474 unsigned ix;
476 for (ix = 0; fname_vars[ix].decl; ix++)
477 if (fname_vars[ix].rid == rid)
478 break;
479 return fname_as_string (fname_vars[ix].pretty);
482 /* Return the VAR_DECL for a const char array naming the current
483 function. If the VAR_DECL has not yet been created, create it
484 now. RID indicates how it should be formatted and IDENTIFIER_NODE
485 ID is its name (unfortunately C and C++ hold the RID values of
486 keywords in different places, so we can't derive RID from ID in
487 this language independant code. */
489 tree
490 fname_decl (rid, id)
491 unsigned rid;
492 tree id;
494 unsigned ix;
495 tree decl = NULL_TREE;
497 for (ix = 0; fname_vars[ix].decl; ix++)
498 if (fname_vars[ix].rid == rid)
499 break;
501 decl = *fname_vars[ix].decl;
502 if (!decl)
504 tree saved_last_tree = last_tree;
506 decl = (*make_fname_decl) (id, fname_vars[ix].pretty);
507 if (last_tree != saved_last_tree)
509 /* We created some statement tree for the decl. This belongs
510 at the start of the function, so remove it now and reinsert
511 it after the function is complete. */
512 tree stmts = TREE_CHAIN (saved_last_tree);
514 TREE_CHAIN (saved_last_tree) = NULL_TREE;
515 last_tree = saved_last_tree;
516 saved_function_name_decls = tree_cons (decl, stmts,
517 saved_function_name_decls);
519 *fname_vars[ix].decl = decl;
521 if (!ix && !current_function_decl)
522 pedwarn_with_decl (decl, "`%s' is not defined outside of function scope");
524 return decl;
527 /* Given a chain of STRING_CST nodes,
528 concatenate them into one STRING_CST
529 and give it a suitable array-of-chars data type. */
531 tree
532 combine_strings (strings)
533 tree strings;
535 register tree value, t;
536 register int length = 1;
537 int wide_length = 0;
538 int wide_flag = 0;
539 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
540 int nchars;
541 const int nchars_max = flag_isoc99 ? 4095 : 509;
543 if (TREE_CHAIN (strings))
545 /* More than one in the chain, so concatenate. */
546 register char *p, *q;
548 /* Don't include the \0 at the end of each substring,
549 except for the last one.
550 Count wide strings and ordinary strings separately. */
551 for (t = strings; t; t = TREE_CHAIN (t))
553 if (TREE_TYPE (t) == wchar_array_type_node)
555 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
556 wide_flag = 1;
558 else
559 length += (TREE_STRING_LENGTH (t) - 1);
562 /* If anything is wide, the non-wides will be converted,
563 which makes them take more space. */
564 if (wide_flag)
565 length = length * wchar_bytes + wide_length;
567 p = alloca (length);
569 /* Copy the individual strings into the new combined string.
570 If the combined string is wide, convert the chars to ints
571 for any individual strings that are not wide. */
573 q = p;
574 for (t = strings; t; t = TREE_CHAIN (t))
576 int len = (TREE_STRING_LENGTH (t)
577 - ((TREE_TYPE (t) == wchar_array_type_node)
578 ? wchar_bytes : 1));
579 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
581 memcpy (q, TREE_STRING_POINTER (t), len);
582 q += len;
584 else
586 int i, j;
587 for (i = 0; i < len; i++)
589 if (BYTES_BIG_ENDIAN)
591 for (j=0; j<(WCHAR_TYPE_SIZE / BITS_PER_UNIT)-1; j++)
592 *q++ = 0;
593 *q++ = TREE_STRING_POINTER (t)[i];
595 else
597 *q++ = TREE_STRING_POINTER (t)[i];
598 for (j=0; j<(WCHAR_TYPE_SIZE / BITS_PER_UNIT)-1; j++)
599 *q++ = 0;
604 if (wide_flag)
606 int i;
607 for (i = 0; i < wchar_bytes; i++)
608 *q++ = 0;
610 else
611 *q = 0;
613 value = build_string (length, p);
615 else
617 value = strings;
618 length = TREE_STRING_LENGTH (value);
619 if (TREE_TYPE (value) == wchar_array_type_node)
620 wide_flag = 1;
623 /* Compute the number of elements, for the array type. */
624 nchars = wide_flag ? length / wchar_bytes : length;
626 if (pedantic && nchars - 1 > nchars_max && c_language == clk_c)
627 pedwarn ("string length `%d' is greater than the length `%d' ISO C%d compilers are required to support",
628 nchars - 1, nchars_max, flag_isoc99 ? 99 : 89);
630 /* Create the array type for the string constant.
631 -Wwrite-strings says make the string constant an array of const char
632 so that copying it to a non-const pointer will get a warning.
633 For C++, this is the standard behavior. */
634 if (flag_const_strings
635 && (! flag_traditional && ! flag_writable_strings))
637 tree elements
638 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
639 1, 0);
640 TREE_TYPE (value)
641 = build_array_type (elements,
642 build_index_type (build_int_2 (nchars - 1, 0)));
644 else
645 TREE_TYPE (value)
646 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
647 build_index_type (build_int_2 (nchars - 1, 0)));
649 TREE_CONSTANT (value) = 1;
650 TREE_READONLY (value) = ! flag_writable_strings;
651 TREE_STATIC (value) = 1;
652 return value;
655 /* To speed up processing of attributes, we maintain an array of
656 IDENTIFIER_NODES and the corresponding attribute types. */
658 /* Array to hold attribute information. */
660 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
662 static int attrtab_idx = 0;
664 /* Add an entry to the attribute table above. */
666 static void
667 add_attribute (id, string, min_len, max_len, decl_req)
668 enum attrs id;
669 const char *string;
670 int min_len, max_len;
671 int decl_req;
673 char buf[100];
675 attrtab[attrtab_idx].id = id;
676 attrtab[attrtab_idx].name = get_identifier (string);
677 attrtab[attrtab_idx].min = min_len;
678 attrtab[attrtab_idx].max = max_len;
679 attrtab[attrtab_idx++].decl_req = decl_req;
681 sprintf (buf, "__%s__", string);
683 attrtab[attrtab_idx].id = id;
684 attrtab[attrtab_idx].name = get_identifier (buf);
685 attrtab[attrtab_idx].min = min_len;
686 attrtab[attrtab_idx].max = max_len;
687 attrtab[attrtab_idx++].decl_req = decl_req;
690 /* Initialize attribute table. */
692 static void
693 init_attributes ()
695 add_attribute (A_PACKED, "packed", 0, 0, 0);
696 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
697 add_attribute (A_COMMON, "common", 0, 0, 1);
698 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
699 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
700 add_attribute (A_UNUSED, "unused", 0, 0, 0);
701 add_attribute (A_CONST, "const", 0, 0, 1);
702 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
703 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
704 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
705 add_attribute (A_MODE, "mode", 1, 1, 1);
706 add_attribute (A_SECTION, "section", 1, 1, 1);
707 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
708 add_attribute (A_FORMAT, "format", 3, 3, 1);
709 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
710 add_attribute (A_WEAK, "weak", 0, 0, 1);
711 add_attribute (A_ALIAS, "alias", 1, 1, 1);
712 add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
713 add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
714 add_attribute (A_MALLOC, "malloc", 0, 0, 1);
715 add_attribute (A_NO_LIMIT_STACK, "no_stack_limit", 0, 0, 1);
716 add_attribute (A_PURE, "pure", 0, 0, 1);
719 /* Default implementation of valid_lang_attribute, below. By default, there
720 are no language-specific attributes. */
722 static int
723 default_valid_lang_attribute (attr_name, attr_args, decl, type)
724 tree attr_name ATTRIBUTE_UNUSED;
725 tree attr_args ATTRIBUTE_UNUSED;
726 tree decl ATTRIBUTE_UNUSED;
727 tree type ATTRIBUTE_UNUSED;
729 return 0;
732 /* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific
733 attribute for either declaration DECL or type TYPE and 0 otherwise. */
735 int (*valid_lang_attribute) PARAMS ((tree, tree, tree, tree))
736 = default_valid_lang_attribute;
738 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
739 and install them in NODE, which is either a DECL (including a TYPE_DECL)
740 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
741 and declaration modifiers but before the declaration proper. */
743 void
744 decl_attributes (node, attributes, prefix_attributes)
745 tree node, attributes, prefix_attributes;
747 tree decl = 0, type = 0;
748 int is_type = 0;
749 tree a;
751 if (attrtab_idx == 0)
752 init_attributes ();
754 if (DECL_P (node))
756 decl = node;
757 type = TREE_TYPE (decl);
758 is_type = TREE_CODE (node) == TYPE_DECL;
760 else if (TYPE_P (node))
761 type = node, is_type = 1;
763 #ifdef PRAGMA_INSERT_ATTRIBUTES
764 /* If the code in c-pragma.c wants to insert some attributes then
765 allow it to do so. Do this before allowing machine back ends to
766 insert attributes, so that they have the opportunity to override
767 anything done here. */
768 PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
769 #endif
771 #ifdef INSERT_ATTRIBUTES
772 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
773 #endif
775 attributes = chainon (prefix_attributes, attributes);
777 for (a = attributes; a; a = TREE_CHAIN (a))
779 tree name = TREE_PURPOSE (a);
780 tree args = TREE_VALUE (a);
781 int i;
782 enum attrs id;
784 for (i = 0; i < attrtab_idx; i++)
785 if (attrtab[i].name == name)
786 break;
788 if (i == attrtab_idx)
790 if (! valid_machine_attribute (name, args, decl, type)
791 && ! (* valid_lang_attribute) (name, args, decl, type))
792 warning ("`%s' attribute directive ignored",
793 IDENTIFIER_POINTER (name));
794 else if (decl != 0)
795 type = TREE_TYPE (decl);
796 continue;
798 else if (attrtab[i].decl_req && decl == 0)
800 warning ("`%s' attribute does not apply to types",
801 IDENTIFIER_POINTER (name));
802 continue;
804 else if (list_length (args) < attrtab[i].min
805 || list_length (args) > attrtab[i].max)
807 error ("wrong number of arguments specified for `%s' attribute",
808 IDENTIFIER_POINTER (name));
809 continue;
812 id = attrtab[i].id;
813 switch (id)
815 case A_PACKED:
816 if (is_type)
817 TYPE_PACKED (type) = 1;
818 else if (TREE_CODE (decl) == FIELD_DECL)
819 DECL_PACKED (decl) = 1;
820 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
821 used for DECL_REGISTER. It wouldn't mean anything anyway. */
822 else
823 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
824 break;
826 case A_NOCOMMON:
827 if (TREE_CODE (decl) == VAR_DECL)
828 DECL_COMMON (decl) = 0;
829 else
830 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
831 break;
833 case A_COMMON:
834 if (TREE_CODE (decl) == VAR_DECL)
835 DECL_COMMON (decl) = 1;
836 else
837 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
838 break;
840 case A_NORETURN:
841 if (TREE_CODE (decl) == FUNCTION_DECL)
842 TREE_THIS_VOLATILE (decl) = 1;
843 else if (TREE_CODE (type) == POINTER_TYPE
844 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
845 TREE_TYPE (decl) = type
846 = build_pointer_type
847 (build_type_variant (TREE_TYPE (type),
848 TREE_READONLY (TREE_TYPE (type)), 1));
849 else
850 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
851 break;
853 case A_MALLOC:
854 if (TREE_CODE (decl) == FUNCTION_DECL)
855 DECL_IS_MALLOC (decl) = 1;
856 /* ??? TODO: Support types. */
857 else
858 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
859 break;
861 case A_UNUSED:
862 if (is_type)
863 if (decl)
864 TREE_USED (decl) = 1;
865 else
866 TREE_USED (type) = 1;
867 else if (TREE_CODE (decl) == PARM_DECL
868 || TREE_CODE (decl) == VAR_DECL
869 || TREE_CODE (decl) == FUNCTION_DECL
870 || TREE_CODE (decl) == LABEL_DECL)
871 TREE_USED (decl) = 1;
872 else
873 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
874 break;
876 case A_CONST:
877 if (TREE_CODE (decl) == FUNCTION_DECL)
878 TREE_READONLY (decl) = 1;
879 else if (TREE_CODE (type) == POINTER_TYPE
880 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
881 TREE_TYPE (decl) = type
882 = build_pointer_type
883 (build_type_variant (TREE_TYPE (type), 1,
884 TREE_THIS_VOLATILE (TREE_TYPE (type))));
885 else
886 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
887 break;
889 case A_PURE:
890 if (TREE_CODE (decl) == FUNCTION_DECL)
891 DECL_IS_PURE (decl) = 1;
892 /* ??? TODO: Support types. */
893 else
894 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
895 break;
898 case A_T_UNION:
899 if (is_type
900 && TREE_CODE (type) == UNION_TYPE
901 && (decl == 0
902 || (TYPE_FIELDS (type) != 0
903 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
904 TYPE_TRANSPARENT_UNION (type) = 1;
905 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
906 && TREE_CODE (type) == UNION_TYPE
907 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
908 DECL_TRANSPARENT_UNION (decl) = 1;
909 else
910 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
911 break;
913 case A_CONSTRUCTOR:
914 if (TREE_CODE (decl) == FUNCTION_DECL
915 && TREE_CODE (type) == FUNCTION_TYPE
916 && decl_function_context (decl) == 0)
918 DECL_STATIC_CONSTRUCTOR (decl) = 1;
919 TREE_USED (decl) = 1;
921 else
922 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
923 break;
925 case A_DESTRUCTOR:
926 if (TREE_CODE (decl) == FUNCTION_DECL
927 && TREE_CODE (type) == FUNCTION_TYPE
928 && decl_function_context (decl) == 0)
930 DECL_STATIC_DESTRUCTOR (decl) = 1;
931 TREE_USED (decl) = 1;
933 else
934 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
935 break;
937 case A_MODE:
938 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
939 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
940 else
942 int j;
943 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
944 int len = strlen (p);
945 enum machine_mode mode = VOIDmode;
946 tree typefm;
948 if (len > 4 && p[0] == '_' && p[1] == '_'
949 && p[len - 1] == '_' && p[len - 2] == '_')
951 char *newp = (char *) alloca (len - 1);
953 strcpy (newp, &p[2]);
954 newp[len - 4] = '\0';
955 p = newp;
958 /* Give this decl a type with the specified mode.
959 First check for the special modes. */
960 if (! strcmp (p, "byte"))
961 mode = byte_mode;
962 else if (!strcmp (p, "word"))
963 mode = word_mode;
964 else if (! strcmp (p, "pointer"))
965 mode = ptr_mode;
966 else
967 for (j = 0; j < NUM_MACHINE_MODES; j++)
968 if (!strcmp (p, GET_MODE_NAME (j)))
969 mode = (enum machine_mode) j;
971 if (mode == VOIDmode)
972 error ("unknown machine mode `%s'", p);
973 else if (0 == (typefm = type_for_mode (mode,
974 TREE_UNSIGNED (type))))
975 error ("no data type for mode `%s'", p);
976 else
978 if (TYPE_PRECISION (typefm) > (TREE_UNSIGNED (type)
979 ? TYPE_PRECISION(uintmax_type_node)
980 : TYPE_PRECISION(intmax_type_node))
981 && pedantic)
982 pedwarn ("type with more precision than %s",
983 TREE_UNSIGNED (type) ? "uintmax_t" : "intmax_t");
984 TREE_TYPE (decl) = type = typefm;
985 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
986 layout_decl (decl, 0);
989 break;
991 case A_SECTION:
992 #ifdef ASM_OUTPUT_SECTION_NAME
993 if ((TREE_CODE (decl) == FUNCTION_DECL
994 || TREE_CODE (decl) == VAR_DECL)
995 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
997 if (TREE_CODE (decl) == VAR_DECL
998 && current_function_decl != NULL_TREE
999 && ! TREE_STATIC (decl))
1000 error_with_decl (decl,
1001 "section attribute cannot be specified for local variables");
1002 /* The decl may have already been given a section attribute from
1003 a previous declaration. Ensure they match. */
1004 else if (DECL_SECTION_NAME (decl) != NULL_TREE
1005 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
1006 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
1007 error_with_decl (node,
1008 "section of `%s' conflicts with previous declaration");
1009 else
1010 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
1012 else
1013 error_with_decl (node,
1014 "section attribute not allowed for `%s'");
1015 #else
1016 error_with_decl (node,
1017 "section attributes are not supported for this target");
1018 #endif
1019 break;
1021 case A_ALIGNED:
1023 tree align_expr
1024 = (args ? TREE_VALUE (args)
1025 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1026 int i;
1028 /* Strip any NOPs of any kind. */
1029 while (TREE_CODE (align_expr) == NOP_EXPR
1030 || TREE_CODE (align_expr) == CONVERT_EXPR
1031 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
1032 align_expr = TREE_OPERAND (align_expr, 0);
1034 if (TREE_CODE (align_expr) != INTEGER_CST)
1036 error ("requested alignment is not a constant");
1037 continue;
1040 if ((i = tree_log2 (align_expr)) == -1)
1041 error ("requested alignment is not a power of 2");
1042 else if (i > HOST_BITS_PER_INT - 2)
1043 error ("requested alignment is too large");
1044 else if (is_type)
1046 /* If we have a TYPE_DECL, then copy the type, so that we
1047 don't accidentally modify a builtin type. See pushdecl. */
1048 if (decl && TREE_TYPE (decl) != error_mark_node
1049 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
1051 tree tt = TREE_TYPE (decl);
1052 DECL_ORIGINAL_TYPE (decl) = tt;
1053 tt = build_type_copy (tt);
1054 TYPE_NAME (tt) = decl;
1055 TREE_USED (tt) = TREE_USED (decl);
1056 TREE_TYPE (decl) = tt;
1057 type = tt;
1060 TYPE_ALIGN (type) = (1 << i) * BITS_PER_UNIT;
1061 TYPE_USER_ALIGN (type) = 1;
1063 else if (TREE_CODE (decl) != VAR_DECL
1064 && TREE_CODE (decl) != FIELD_DECL)
1065 error_with_decl (decl,
1066 "alignment may not be specified for `%s'");
1067 else
1069 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
1070 DECL_USER_ALIGN (decl) = 1;
1073 break;
1075 case A_FORMAT:
1076 decl_handle_format_attribute (decl, args);
1077 break;
1079 case A_FORMAT_ARG:
1080 decl_handle_format_arg_attribute (decl, args);
1081 break;
1083 case A_WEAK:
1084 declare_weak (decl);
1085 break;
1087 case A_ALIAS:
1088 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
1089 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
1090 error_with_decl (decl,
1091 "`%s' defined both normally and as an alias");
1092 else if (decl_function_context (decl) == 0)
1094 tree id;
1096 id = TREE_VALUE (args);
1097 if (TREE_CODE (id) != STRING_CST)
1099 error ("alias arg not a string");
1100 break;
1102 id = get_identifier (TREE_STRING_POINTER (id));
1103 /* This counts as a use of the object pointed to. */
1104 TREE_USED (id) = 1;
1106 if (TREE_CODE (decl) == FUNCTION_DECL)
1107 DECL_INITIAL (decl) = error_mark_node;
1108 else
1109 DECL_EXTERNAL (decl) = 0;
1110 assemble_alias (decl, id);
1112 else
1113 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1114 break;
1116 case A_NO_CHECK_MEMORY_USAGE:
1117 if (TREE_CODE (decl) != FUNCTION_DECL)
1119 error_with_decl (decl,
1120 "`%s' attribute applies only to functions",
1121 IDENTIFIER_POINTER (name));
1123 else if (DECL_INITIAL (decl))
1125 error_with_decl (decl,
1126 "can't set `%s' attribute after definition",
1127 IDENTIFIER_POINTER (name));
1129 else
1130 DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
1131 break;
1133 case A_NO_INSTRUMENT_FUNCTION:
1134 if (TREE_CODE (decl) != FUNCTION_DECL)
1136 error_with_decl (decl,
1137 "`%s' attribute applies only to functions",
1138 IDENTIFIER_POINTER (name));
1140 else if (DECL_INITIAL (decl))
1142 error_with_decl (decl,
1143 "can't set `%s' attribute after definition",
1144 IDENTIFIER_POINTER (name));
1146 else
1147 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1148 break;
1150 case A_NO_LIMIT_STACK:
1151 if (TREE_CODE (decl) != FUNCTION_DECL)
1153 error_with_decl (decl,
1154 "`%s' attribute applies only to functions",
1155 IDENTIFIER_POINTER (name));
1157 else if (DECL_INITIAL (decl))
1159 error_with_decl (decl,
1160 "can't set `%s' attribute after definition",
1161 IDENTIFIER_POINTER (name));
1163 else
1164 DECL_NO_LIMIT_STACK (decl) = 1;
1165 break;
1170 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1171 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1173 The head of the declspec list is stored in DECLSPECS.
1174 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1176 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1177 the list elements. We drop the containing TREE_LIST nodes and link the
1178 resulting attributes together the way decl_attributes expects them. */
1180 void
1181 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1182 tree specs_attrs;
1183 tree *declspecs, *prefix_attributes;
1185 tree t, s, a, next, specs, attrs;
1187 /* This can happen after an __extension__ in pedantic mode. */
1188 if (specs_attrs != NULL_TREE
1189 && TREE_CODE (specs_attrs) == INTEGER_CST)
1191 *declspecs = NULL_TREE;
1192 *prefix_attributes = NULL_TREE;
1193 return;
1196 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1197 if (specs_attrs != NULL_TREE
1198 && TREE_CODE (specs_attrs) != TREE_LIST)
1200 *declspecs = specs_attrs;
1201 *prefix_attributes = NULL_TREE;
1202 return;
1205 /* Remember to keep the lists in the same order, element-wise. */
1207 specs = s = NULL_TREE;
1208 attrs = a = NULL_TREE;
1209 for (t = specs_attrs; t; t = next)
1211 next = TREE_CHAIN (t);
1212 /* Declspecs have a non-NULL TREE_VALUE. */
1213 if (TREE_VALUE (t) != NULL_TREE)
1215 if (specs == NULL_TREE)
1216 specs = s = t;
1217 else
1219 TREE_CHAIN (s) = t;
1220 s = t;
1223 else
1225 if (attrs == NULL_TREE)
1226 attrs = a = TREE_PURPOSE (t);
1227 else
1229 TREE_CHAIN (a) = TREE_PURPOSE (t);
1230 a = TREE_PURPOSE (t);
1232 /* More attrs can be linked here, move A to the end. */
1233 while (TREE_CHAIN (a) != NULL_TREE)
1234 a = TREE_CHAIN (a);
1238 /* Terminate the lists. */
1239 if (s != NULL_TREE)
1240 TREE_CHAIN (s) = NULL_TREE;
1241 if (a != NULL_TREE)
1242 TREE_CHAIN (a) = NULL_TREE;
1244 /* All done. */
1245 *declspecs = specs;
1246 *prefix_attributes = attrs;
1249 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1250 This function is used by the parser when a rule will accept attributes
1251 in a particular position, but we don't want to support that just yet.
1253 A warning is issued for every ignored attribute. */
1255 tree
1256 strip_attrs (specs_attrs)
1257 tree specs_attrs;
1259 tree specs, attrs;
1261 split_specs_attrs (specs_attrs, &specs, &attrs);
1263 while (attrs)
1265 warning ("`%s' attribute ignored",
1266 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1267 attrs = TREE_CHAIN (attrs);
1270 return specs;
1273 static int is_valid_printf_arglist PARAMS ((tree));
1274 static rtx c_expand_builtin PARAMS ((tree, rtx, enum machine_mode, enum expand_modifier));
1275 static rtx c_expand_builtin_printf PARAMS ((tree, rtx, enum machine_mode,
1276 enum expand_modifier, int));
1277 static rtx c_expand_builtin_fprintf PARAMS ((tree, rtx, enum machine_mode,
1278 enum expand_modifier, int));
1280 /* Print a warning if a constant expression had overflow in folding.
1281 Invoke this function on every expression that the language
1282 requires to be a constant expression.
1283 Note the ANSI C standard says it is erroneous for a
1284 constant expression to overflow. */
1286 void
1287 constant_expression_warning (value)
1288 tree value;
1290 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1291 || TREE_CODE (value) == COMPLEX_CST)
1292 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1293 pedwarn ("overflow in constant expression");
1296 /* Print a warning if an expression had overflow in folding.
1297 Invoke this function on every expression that
1298 (1) appears in the source code, and
1299 (2) might be a constant expression that overflowed, and
1300 (3) is not already checked by convert_and_check;
1301 however, do not invoke this function on operands of explicit casts. */
1303 void
1304 overflow_warning (value)
1305 tree value;
1307 if ((TREE_CODE (value) == INTEGER_CST
1308 || (TREE_CODE (value) == COMPLEX_CST
1309 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1310 && TREE_OVERFLOW (value))
1312 TREE_OVERFLOW (value) = 0;
1313 if (skip_evaluation == 0)
1314 warning ("integer overflow in expression");
1316 else if ((TREE_CODE (value) == REAL_CST
1317 || (TREE_CODE (value) == COMPLEX_CST
1318 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1319 && TREE_OVERFLOW (value))
1321 TREE_OVERFLOW (value) = 0;
1322 if (skip_evaluation == 0)
1323 warning ("floating point overflow in expression");
1327 /* Print a warning if a large constant is truncated to unsigned,
1328 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1329 Invoke this function on every expression that might be implicitly
1330 converted to an unsigned type. */
1332 void
1333 unsigned_conversion_warning (result, operand)
1334 tree result, operand;
1336 if (TREE_CODE (operand) == INTEGER_CST
1337 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
1338 && TREE_UNSIGNED (TREE_TYPE (result))
1339 && skip_evaluation == 0
1340 && !int_fits_type_p (operand, TREE_TYPE (result)))
1342 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
1343 /* This detects cases like converting -129 or 256 to unsigned char. */
1344 warning ("large integer implicitly truncated to unsigned type");
1345 else if (warn_conversion)
1346 warning ("negative integer implicitly converted to unsigned type");
1350 /* Nonzero if constant C has a value that is permissible
1351 for type TYPE (an INTEGER_TYPE). */
1353 static int
1354 constant_fits_type_p (c, type)
1355 tree c, type;
1357 if (TREE_CODE (c) == INTEGER_CST)
1358 return int_fits_type_p (c, type);
1360 c = convert (type, c);
1361 return !TREE_OVERFLOW (c);
1364 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1365 Invoke this function on every expression that is converted implicitly,
1366 i.e. because of language rules and not because of an explicit cast. */
1368 tree
1369 convert_and_check (type, expr)
1370 tree type, expr;
1372 tree t = convert (type, expr);
1373 if (TREE_CODE (t) == INTEGER_CST)
1375 if (TREE_OVERFLOW (t))
1377 TREE_OVERFLOW (t) = 0;
1379 /* Do not diagnose overflow in a constant expression merely
1380 because a conversion overflowed. */
1381 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
1383 /* No warning for converting 0x80000000 to int. */
1384 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
1385 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1386 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
1387 /* If EXPR fits in the unsigned version of TYPE,
1388 don't warn unless pedantic. */
1389 if ((pedantic
1390 || TREE_UNSIGNED (type)
1391 || ! constant_fits_type_p (expr, unsigned_type (type)))
1392 && skip_evaluation == 0)
1393 warning ("overflow in implicit constant conversion");
1395 else
1396 unsigned_conversion_warning (t, expr);
1398 return t;
1401 /* A node in a list that describes references to variables (EXPR), which are
1402 either read accesses if WRITER is zero, or write accesses, in which case
1403 WRITER is the parent of EXPR. */
1404 struct tlist
1406 struct tlist *next;
1407 tree expr, writer;
1410 /* Used to implement a cache the results of a call to verify_tree. We only
1411 use this for SAVE_EXPRs. */
1412 struct tlist_cache
1414 struct tlist_cache *next;
1415 struct tlist *cache_before_sp;
1416 struct tlist *cache_after_sp;
1417 tree expr;
1420 /* Obstack to use when allocating tlist structures, and corresponding
1421 firstobj. */
1422 static struct obstack tlist_obstack;
1423 static char *tlist_firstobj = 0;
1425 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
1426 warnings. */
1427 static struct tlist *warned_ids;
1428 /* SAVE_EXPRs need special treatment. We process them only once and then
1429 cache the results. */
1430 static struct tlist_cache *save_expr_cache;
1432 static void add_tlist PARAMS ((struct tlist **, struct tlist *, tree, int));
1433 static void merge_tlist PARAMS ((struct tlist **, struct tlist *, int));
1434 static void verify_tree PARAMS ((tree, struct tlist **, struct tlist **, tree));
1435 static int warning_candidate_p PARAMS ((tree));
1436 static void warn_for_collisions PARAMS ((struct tlist *));
1437 static void warn_for_collisions_1 PARAMS ((tree, tree, struct tlist *, int));
1438 static struct tlist *new_tlist PARAMS ((struct tlist *, tree, tree));
1439 static void verify_sequence_points PARAMS ((tree));
1441 /* Create a new struct tlist and fill in its fields. */
1442 static struct tlist *
1443 new_tlist (next, t, writer)
1444 struct tlist *next;
1445 tree t;
1446 tree writer;
1448 struct tlist *l;
1449 l = (struct tlist *) obstack_alloc (&tlist_obstack, sizeof *l);
1450 l->next = next;
1451 l->expr = t;
1452 l->writer = writer;
1453 return l;
1456 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1457 is nonnull, we ignore any node we find which has a writer equal to it. */
1459 static void
1460 add_tlist (to, add, exclude_writer, copy)
1461 struct tlist **to;
1462 struct tlist *add;
1463 tree exclude_writer;
1464 int copy;
1466 while (add)
1468 struct tlist *next = add->next;
1469 if (! copy)
1470 add->next = *to;
1471 if (! exclude_writer || add->writer != exclude_writer)
1472 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1473 add = next;
1477 /* Merge the nodes of ADD into TO. This merging process is done so that for
1478 each variable that already exists in TO, no new node is added; however if
1479 there is a write access recorded in ADD, and an occurrence on TO is only
1480 a read access, then the occurrence in TO will be modified to record the
1481 write. */
1483 static void
1484 merge_tlist (to, add, copy)
1485 struct tlist **to;
1486 struct tlist *add;
1487 int copy;
1489 struct tlist **end = to;
1491 while (*end)
1492 end = &(*end)->next;
1494 while (add)
1496 int found = 0;
1497 struct tlist *tmp2;
1498 struct tlist *next = add->next;
1500 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1501 if (tmp2->expr == add->expr)
1503 found = 1;
1504 if (! tmp2->writer)
1505 tmp2->writer = add->writer;
1507 if (! found)
1509 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
1510 end = &(*end)->next;
1511 *end = 0;
1513 add = next;
1517 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1518 references in list LIST conflict with it, excluding reads if ONLY writers
1519 is nonzero. */
1521 static void
1522 warn_for_collisions_1 (written, writer, list, only_writes)
1523 tree written, writer;
1524 struct tlist *list;
1525 int only_writes;
1527 struct tlist *tmp;
1529 /* Avoid duplicate warnings. */
1530 for (tmp = warned_ids; tmp; tmp = tmp->next)
1531 if (tmp->expr == written)
1532 return;
1534 while (list)
1536 if (list->expr == written
1537 && list->writer != writer
1538 && (! only_writes || list->writer))
1540 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
1541 warning ("operation on `%s' may be undefined",
1542 IDENTIFIER_POINTER (DECL_NAME (list->expr)));
1544 list = list->next;
1548 /* Given a list LIST of references to variables, find whether any of these
1549 can cause conflicts due to missing sequence points. */
1551 static void
1552 warn_for_collisions (list)
1553 struct tlist *list;
1555 struct tlist *tmp;
1557 for (tmp = list; tmp; tmp = tmp->next)
1559 if (tmp->writer)
1560 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1564 /* Return nonzero if X is a tree that can be verified by the sequence poitn
1565 warnings. */
1566 static int
1567 warning_candidate_p (x)
1568 tree x;
1570 return TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == PARM_DECL;
1573 /* Walk the tree X, and record accesses to variables. If X is written by the
1574 parent tree, WRITER is the parent.
1575 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
1576 expression or its only operand forces a sequence point, then everything up
1577 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
1578 in PNO_SP.
1579 Once we return, we will have emitted warnings if any subexpression before
1580 such a sequence point could be undefined. On a higher level, however, the
1581 sequence point may not be relevant, and we'll merge the two lists.
1583 Example: (b++, a) + b;
1584 The call that processes the COMPOUND_EXPR will store the increment of B
1585 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
1586 processes the PLUS_EXPR will need to merge the two lists so that
1587 eventually, all accesses end up on the same list (and we'll warn about the
1588 unordered subexpressions b++ and b.
1590 A note on merging. If we modify the former example so that our expression
1591 becomes
1592 (b++, b) + a
1593 care must be taken not simply to add all three expressions into the final
1594 PNO_SP list. The function merge_tlist takes care of that by merging the
1595 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
1596 way, so that no more than one access to B is recorded. */
1598 static void
1599 verify_tree (x, pbefore_sp, pno_sp, writer)
1600 tree x;
1601 struct tlist **pbefore_sp, **pno_sp;
1602 tree writer;
1604 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
1605 enum tree_code code;
1606 char class;
1608 restart:
1609 code = TREE_CODE (x);
1610 class = TREE_CODE_CLASS (code);
1612 if (warning_candidate_p (x))
1614 *pno_sp = new_tlist (*pno_sp, x, writer);
1615 return;
1618 switch (code)
1620 case CONSTRUCTOR:
1621 return;
1623 case COMPOUND_EXPR:
1624 case TRUTH_ANDIF_EXPR:
1625 case TRUTH_ORIF_EXPR:
1626 tmp_before = tmp_nosp = tmp_list3 = 0;
1627 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1628 warn_for_collisions (tmp_nosp);
1629 merge_tlist (pbefore_sp, tmp_before, 0);
1630 merge_tlist (pbefore_sp, tmp_nosp, 0);
1631 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
1632 merge_tlist (pbefore_sp, tmp_list3, 0);
1633 return;
1635 case COND_EXPR:
1636 tmp_before = tmp_list2 = 0;
1637 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
1638 warn_for_collisions (tmp_list2);
1639 merge_tlist (pbefore_sp, tmp_before, 0);
1640 merge_tlist (pbefore_sp, tmp_list2, 1);
1642 tmp_list3 = tmp_nosp = 0;
1643 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
1644 warn_for_collisions (tmp_nosp);
1645 merge_tlist (pbefore_sp, tmp_list3, 0);
1647 tmp_list3 = tmp_list2 = 0;
1648 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
1649 warn_for_collisions (tmp_list2);
1650 merge_tlist (pbefore_sp, tmp_list3, 0);
1651 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1652 two first, to avoid warning for (a ? b++ : b++). */
1653 merge_tlist (&tmp_nosp, tmp_list2, 0);
1654 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1655 return;
1657 case PREDECREMENT_EXPR:
1658 case PREINCREMENT_EXPR:
1659 case POSTDECREMENT_EXPR:
1660 case POSTINCREMENT_EXPR:
1661 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
1662 return;
1664 case MODIFY_EXPR:
1665 tmp_before = tmp_nosp = tmp_list3 = 0;
1666 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
1667 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
1668 /* Expressions inside the LHS are not ordered wrt. the sequence points
1669 in the RHS. Example:
1670 *a = (a++, 2)
1671 Despite the fact that the modification of "a" is in the before_sp
1672 list (tmp_before), it conflicts with the use of "a" in the LHS.
1673 We can handle this by adding the contents of tmp_list3
1674 to those of tmp_before, and redoing the collision warnings for that
1675 list. */
1676 add_tlist (&tmp_before, tmp_list3, x, 1);
1677 warn_for_collisions (tmp_before);
1678 /* Exclude the LHS itself here; we first have to merge it into the
1679 tmp_nosp list. This is done to avoid warning for "a = a"; if we
1680 didn't exclude the LHS, we'd get it twice, once as a read and once
1681 as a write. */
1682 add_tlist (pno_sp, tmp_list3, x, 0);
1683 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
1685 merge_tlist (pbefore_sp, tmp_before, 0);
1686 if (warning_candidate_p (TREE_OPERAND (x, 0)))
1687 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
1688 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
1689 return;
1691 case CALL_EXPR:
1692 /* We need to warn about conflicts among arguments and conflicts between
1693 args and the function address. Side effects of the function address,
1694 however, are not ordered by the sequence point of the call. */
1695 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
1696 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1697 if (TREE_OPERAND (x, 1))
1698 verify_tree (TREE_OPERAND (x, 1), &tmp_list2, &tmp_list3, NULL_TREE);
1699 merge_tlist (&tmp_list3, tmp_list2, 0);
1700 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
1701 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
1702 warn_for_collisions (tmp_before);
1703 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
1704 return;
1706 case TREE_LIST:
1707 /* Scan all the list, e.g. indices of multi dimensional array. */
1708 while (x)
1710 tmp_before = tmp_nosp = 0;
1711 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
1712 merge_tlist (&tmp_nosp, tmp_before, 0);
1713 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1714 x = TREE_CHAIN (x);
1716 return;
1718 case SAVE_EXPR:
1720 struct tlist_cache *t;
1721 for (t = save_expr_cache; t; t = t->next)
1722 if (t->expr == x)
1723 break;
1725 if (! t)
1727 t = (struct tlist_cache *) obstack_alloc (&tlist_obstack,
1728 sizeof *t);
1729 t->next = save_expr_cache;
1730 t->expr = x;
1731 save_expr_cache = t;
1733 tmp_before = tmp_nosp = 0;
1734 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1735 warn_for_collisions (tmp_nosp);
1737 tmp_list3 = 0;
1738 while (tmp_nosp)
1740 struct tlist *t = tmp_nosp;
1741 tmp_nosp = t->next;
1742 merge_tlist (&tmp_list3, t, 0);
1744 t->cache_before_sp = tmp_before;
1745 t->cache_after_sp = tmp_list3;
1747 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
1748 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
1749 return;
1751 default:
1752 break;
1755 if (class == '1')
1757 if (first_rtl_op (code) == 0)
1758 return;
1759 x = TREE_OPERAND (x, 0);
1760 writer = 0;
1761 goto restart;
1764 switch (class)
1766 case 'r':
1767 case '<':
1768 case '2':
1769 case 'b':
1770 case 'e':
1771 case 's':
1772 case 'x':
1774 int lp;
1775 int max = first_rtl_op (TREE_CODE (x));
1776 for (lp = 0; lp < max; lp++)
1778 tmp_before = tmp_nosp = 0;
1779 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, NULL_TREE);
1780 merge_tlist (&tmp_nosp, tmp_before, 0);
1781 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1783 break;
1788 /* Try to warn for undefined behaviour in EXPR due to missing sequence
1789 points. */
1791 static void
1792 verify_sequence_points (expr)
1793 tree expr;
1795 struct tlist *before_sp = 0, *after_sp = 0;
1797 warned_ids = 0;
1798 save_expr_cache = 0;
1799 if (tlist_firstobj == 0)
1801 gcc_obstack_init (&tlist_obstack);
1802 tlist_firstobj = obstack_alloc (&tlist_obstack, 0);
1805 verify_tree (expr, &before_sp, &after_sp, 0);
1806 warn_for_collisions (after_sp);
1807 obstack_free (&tlist_obstack, tlist_firstobj);
1810 tree
1811 c_expand_expr_stmt (expr)
1812 tree expr;
1814 /* Do default conversion if safe and possibly important,
1815 in case within ({...}). */
1816 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
1817 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1818 expr = default_conversion (expr);
1820 if (warn_sequence_point)
1821 verify_sequence_points (expr);
1823 if (TREE_TYPE (expr) != error_mark_node
1824 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
1825 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1826 error ("expression statement has incomplete type");
1828 last_expr_type = TREE_TYPE (expr);
1829 return add_stmt (build_stmt (EXPR_STMT, expr));
1832 /* Validate the expression after `case' and apply default promotions. */
1834 tree
1835 check_case_value (value)
1836 tree value;
1838 if (value == NULL_TREE)
1839 return value;
1841 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1842 STRIP_TYPE_NOPS (value);
1843 /* In C++, the following is allowed:
1845 const int i = 3;
1846 switch (...) { case i: ... }
1848 So, we try to reduce the VALUE to a constant that way. */
1849 if (c_language == clk_cplusplus)
1851 value = decl_constant_value (value);
1852 STRIP_TYPE_NOPS (value);
1853 value = fold (value);
1856 if (TREE_CODE (value) != INTEGER_CST
1857 && value != error_mark_node)
1859 error ("case label does not reduce to an integer constant");
1860 value = error_mark_node;
1862 else
1863 /* Promote char or short to int. */
1864 value = default_conversion (value);
1866 constant_expression_warning (value);
1868 return value;
1871 /* Return an integer type with BITS bits of precision,
1872 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1874 tree
1875 type_for_size (bits, unsignedp)
1876 unsigned bits;
1877 int unsignedp;
1879 if (bits == TYPE_PRECISION (integer_type_node))
1880 return unsignedp ? unsigned_type_node : integer_type_node;
1882 if (bits == TYPE_PRECISION (signed_char_type_node))
1883 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1885 if (bits == TYPE_PRECISION (short_integer_type_node))
1886 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1888 if (bits == TYPE_PRECISION (long_integer_type_node))
1889 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1891 if (bits == TYPE_PRECISION (long_long_integer_type_node))
1892 return (unsignedp ? long_long_unsigned_type_node
1893 : long_long_integer_type_node);
1895 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
1896 return (unsignedp ? widest_unsigned_literal_type_node
1897 : widest_integer_literal_type_node);
1899 if (bits <= TYPE_PRECISION (intQI_type_node))
1900 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1902 if (bits <= TYPE_PRECISION (intHI_type_node))
1903 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1905 if (bits <= TYPE_PRECISION (intSI_type_node))
1906 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1908 if (bits <= TYPE_PRECISION (intDI_type_node))
1909 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1911 return 0;
1914 /* Return a data type that has machine mode MODE.
1915 If the mode is an integer,
1916 then UNSIGNEDP selects between signed and unsigned types. */
1918 tree
1919 type_for_mode (mode, unsignedp)
1920 enum machine_mode mode;
1921 int unsignedp;
1923 if (mode == TYPE_MODE (integer_type_node))
1924 return unsignedp ? unsigned_type_node : integer_type_node;
1926 if (mode == TYPE_MODE (signed_char_type_node))
1927 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1929 if (mode == TYPE_MODE (short_integer_type_node))
1930 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1932 if (mode == TYPE_MODE (long_integer_type_node))
1933 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1935 if (mode == TYPE_MODE (long_long_integer_type_node))
1936 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
1938 if (mode == TYPE_MODE (widest_integer_literal_type_node))
1939 return unsignedp ? widest_unsigned_literal_type_node
1940 : widest_integer_literal_type_node;
1942 if (mode == TYPE_MODE (intQI_type_node))
1943 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1945 if (mode == TYPE_MODE (intHI_type_node))
1946 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1948 if (mode == TYPE_MODE (intSI_type_node))
1949 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1951 if (mode == TYPE_MODE (intDI_type_node))
1952 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1954 #if HOST_BITS_PER_WIDE_INT >= 64
1955 if (mode == TYPE_MODE (intTI_type_node))
1956 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
1957 #endif
1959 if (mode == TYPE_MODE (float_type_node))
1960 return float_type_node;
1962 if (mode == TYPE_MODE (double_type_node))
1963 return double_type_node;
1965 if (mode == TYPE_MODE (long_double_type_node))
1966 return long_double_type_node;
1968 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
1969 return build_pointer_type (char_type_node);
1971 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
1972 return build_pointer_type (integer_type_node);
1974 #ifdef VECTOR_MODE_SUPPORTED_P
1975 if (mode == TYPE_MODE (V4SF_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1976 return V4SF_type_node;
1977 if (mode == TYPE_MODE (V4SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1978 return V4SI_type_node;
1979 if (mode == TYPE_MODE (V2SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1980 return V2SI_type_node;
1981 if (mode == TYPE_MODE (V4HI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1982 return V4HI_type_node;
1983 if (mode == TYPE_MODE (V8QI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1984 return V8QI_type_node;
1985 #endif
1987 return 0;
1990 /* Return an unsigned type the same as TYPE in other respects. */
1991 tree
1992 unsigned_type (type)
1993 tree type;
1995 tree type1 = TYPE_MAIN_VARIANT (type);
1996 if (type1 == signed_char_type_node || type1 == char_type_node)
1997 return unsigned_char_type_node;
1998 if (type1 == integer_type_node)
1999 return unsigned_type_node;
2000 if (type1 == short_integer_type_node)
2001 return short_unsigned_type_node;
2002 if (type1 == long_integer_type_node)
2003 return long_unsigned_type_node;
2004 if (type1 == long_long_integer_type_node)
2005 return long_long_unsigned_type_node;
2006 if (type1 == widest_integer_literal_type_node)
2007 return widest_unsigned_literal_type_node;
2008 #if HOST_BITS_PER_WIDE_INT >= 64
2009 if (type1 == intTI_type_node)
2010 return unsigned_intTI_type_node;
2011 #endif
2012 if (type1 == intDI_type_node)
2013 return unsigned_intDI_type_node;
2014 if (type1 == intSI_type_node)
2015 return unsigned_intSI_type_node;
2016 if (type1 == intHI_type_node)
2017 return unsigned_intHI_type_node;
2018 if (type1 == intQI_type_node)
2019 return unsigned_intQI_type_node;
2021 return signed_or_unsigned_type (1, type);
2024 /* Return a signed type the same as TYPE in other respects. */
2026 tree
2027 signed_type (type)
2028 tree type;
2030 tree type1 = TYPE_MAIN_VARIANT (type);
2031 if (type1 == unsigned_char_type_node || type1 == char_type_node)
2032 return signed_char_type_node;
2033 if (type1 == unsigned_type_node)
2034 return integer_type_node;
2035 if (type1 == short_unsigned_type_node)
2036 return short_integer_type_node;
2037 if (type1 == long_unsigned_type_node)
2038 return long_integer_type_node;
2039 if (type1 == long_long_unsigned_type_node)
2040 return long_long_integer_type_node;
2041 if (type1 == widest_unsigned_literal_type_node)
2042 return widest_integer_literal_type_node;
2043 #if HOST_BITS_PER_WIDE_INT >= 64
2044 if (type1 == unsigned_intTI_type_node)
2045 return intTI_type_node;
2046 #endif
2047 if (type1 == unsigned_intDI_type_node)
2048 return intDI_type_node;
2049 if (type1 == unsigned_intSI_type_node)
2050 return intSI_type_node;
2051 if (type1 == unsigned_intHI_type_node)
2052 return intHI_type_node;
2053 if (type1 == unsigned_intQI_type_node)
2054 return intQI_type_node;
2056 return signed_or_unsigned_type (0, type);
2059 /* Return a type the same as TYPE except unsigned or
2060 signed according to UNSIGNEDP. */
2062 tree
2063 signed_or_unsigned_type (unsignedp, type)
2064 int unsignedp;
2065 tree type;
2067 if (! INTEGRAL_TYPE_P (type)
2068 || TREE_UNSIGNED (type) == unsignedp)
2069 return type;
2071 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
2072 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2073 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2074 return unsignedp ? unsigned_type_node : integer_type_node;
2075 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
2076 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2077 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
2078 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2079 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
2080 return (unsignedp ? long_long_unsigned_type_node
2081 : long_long_integer_type_node);
2082 if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
2083 return (unsignedp ? widest_unsigned_literal_type_node
2084 : widest_integer_literal_type_node);
2085 return type;
2088 /* Return the minimum number of bits needed to represent VALUE in a
2089 signed or unsigned type, UNSIGNEDP says which. */
2091 unsigned int
2092 min_precision (value, unsignedp)
2093 tree value;
2094 int unsignedp;
2096 int log;
2098 /* If the value is negative, compute its negative minus 1. The latter
2099 adjustment is because the absolute value of the largest negative value
2100 is one larger than the largest positive value. This is equivalent to
2101 a bit-wise negation, so use that operation instead. */
2103 if (tree_int_cst_sgn (value) < 0)
2104 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2106 /* Return the number of bits needed, taking into account the fact
2107 that we need one more bit for a signed than unsigned type. */
2109 if (integer_zerop (value))
2110 log = 0;
2111 else
2112 log = tree_floor_log2 (value);
2114 return log + 1 + ! unsignedp;
2117 /* Print an error message for invalid operands to arith operation CODE.
2118 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2120 void
2121 binary_op_error (code)
2122 enum tree_code code;
2124 register const char *opname;
2126 switch (code)
2128 case NOP_EXPR:
2129 error ("invalid truth-value expression");
2130 return;
2132 case PLUS_EXPR:
2133 opname = "+"; break;
2134 case MINUS_EXPR:
2135 opname = "-"; break;
2136 case MULT_EXPR:
2137 opname = "*"; break;
2138 case MAX_EXPR:
2139 opname = "max"; break;
2140 case MIN_EXPR:
2141 opname = "min"; break;
2142 case EQ_EXPR:
2143 opname = "=="; break;
2144 case NE_EXPR:
2145 opname = "!="; break;
2146 case LE_EXPR:
2147 opname = "<="; break;
2148 case GE_EXPR:
2149 opname = ">="; break;
2150 case LT_EXPR:
2151 opname = "<"; break;
2152 case GT_EXPR:
2153 opname = ">"; break;
2154 case LSHIFT_EXPR:
2155 opname = "<<"; break;
2156 case RSHIFT_EXPR:
2157 opname = ">>"; break;
2158 case TRUNC_MOD_EXPR:
2159 case FLOOR_MOD_EXPR:
2160 opname = "%"; break;
2161 case TRUNC_DIV_EXPR:
2162 case FLOOR_DIV_EXPR:
2163 opname = "/"; break;
2164 case BIT_AND_EXPR:
2165 opname = "&"; break;
2166 case BIT_IOR_EXPR:
2167 opname = "|"; break;
2168 case TRUTH_ANDIF_EXPR:
2169 opname = "&&"; break;
2170 case TRUTH_ORIF_EXPR:
2171 opname = "||"; break;
2172 case BIT_XOR_EXPR:
2173 opname = "^"; break;
2174 case LROTATE_EXPR:
2175 case RROTATE_EXPR:
2176 opname = "rotate"; break;
2177 default:
2178 opname = "unknown"; break;
2180 error ("invalid operands to binary %s", opname);
2183 /* Subroutine of build_binary_op, used for comparison operations.
2184 See if the operands have both been converted from subword integer types
2185 and, if so, perhaps change them both back to their original type.
2186 This function is also responsible for converting the two operands
2187 to the proper common type for comparison.
2189 The arguments of this function are all pointers to local variables
2190 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2191 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2193 If this function returns nonzero, it means that the comparison has
2194 a constant value. What this function returns is an expression for
2195 that value. */
2197 tree
2198 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2199 tree *op0_ptr, *op1_ptr;
2200 tree *restype_ptr;
2201 enum tree_code *rescode_ptr;
2203 register tree type;
2204 tree op0 = *op0_ptr;
2205 tree op1 = *op1_ptr;
2206 int unsignedp0, unsignedp1;
2207 int real1, real2;
2208 tree primop0, primop1;
2209 enum tree_code code = *rescode_ptr;
2211 /* Throw away any conversions to wider types
2212 already present in the operands. */
2214 primop0 = get_narrower (op0, &unsignedp0);
2215 primop1 = get_narrower (op1, &unsignedp1);
2217 /* Handle the case that OP0 does not *contain* a conversion
2218 but it *requires* conversion to FINAL_TYPE. */
2220 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2221 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2222 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2223 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2225 /* If one of the operands must be floated, we cannot optimize. */
2226 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2227 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2229 /* If first arg is constant, swap the args (changing operation
2230 so value is preserved), for canonicalization. Don't do this if
2231 the second arg is 0. */
2233 if (TREE_CONSTANT (primop0)
2234 && ! integer_zerop (primop1) && ! real_zerop (primop1))
2236 register tree tem = primop0;
2237 register int temi = unsignedp0;
2238 primop0 = primop1;
2239 primop1 = tem;
2240 tem = op0;
2241 op0 = op1;
2242 op1 = tem;
2243 *op0_ptr = op0;
2244 *op1_ptr = op1;
2245 unsignedp0 = unsignedp1;
2246 unsignedp1 = temi;
2247 temi = real1;
2248 real1 = real2;
2249 real2 = temi;
2251 switch (code)
2253 case LT_EXPR:
2254 code = GT_EXPR;
2255 break;
2256 case GT_EXPR:
2257 code = LT_EXPR;
2258 break;
2259 case LE_EXPR:
2260 code = GE_EXPR;
2261 break;
2262 case GE_EXPR:
2263 code = LE_EXPR;
2264 break;
2265 default:
2266 break;
2268 *rescode_ptr = code;
2271 /* If comparing an integer against a constant more bits wide,
2272 maybe we can deduce a value of 1 or 0 independent of the data.
2273 Or else truncate the constant now
2274 rather than extend the variable at run time.
2276 This is only interesting if the constant is the wider arg.
2277 Also, it is not safe if the constant is unsigned and the
2278 variable arg is signed, since in this case the variable
2279 would be sign-extended and then regarded as unsigned.
2280 Our technique fails in this case because the lowest/highest
2281 possible unsigned results don't follow naturally from the
2282 lowest/highest possible values of the variable operand.
2283 For just EQ_EXPR and NE_EXPR there is another technique that
2284 could be used: see if the constant can be faithfully represented
2285 in the other operand's type, by truncating it and reextending it
2286 and see if that preserves the constant's value. */
2288 if (!real1 && !real2
2289 && TREE_CODE (primop1) == INTEGER_CST
2290 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2292 int min_gt, max_gt, min_lt, max_lt;
2293 tree maxval, minval;
2294 /* 1 if comparison is nominally unsigned. */
2295 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2296 tree val;
2298 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2300 /* If TYPE is an enumeration, then we need to get its min/max
2301 values from it's underlying integral type, not the enumerated
2302 type itself. */
2303 if (TREE_CODE (type) == ENUMERAL_TYPE)
2304 type = type_for_size (TYPE_PRECISION (type), unsignedp0);
2306 maxval = TYPE_MAX_VALUE (type);
2307 minval = TYPE_MIN_VALUE (type);
2309 if (unsignedp && !unsignedp0)
2310 *restype_ptr = signed_type (*restype_ptr);
2312 if (TREE_TYPE (primop1) != *restype_ptr)
2313 primop1 = convert (*restype_ptr, primop1);
2314 if (type != *restype_ptr)
2316 minval = convert (*restype_ptr, minval);
2317 maxval = convert (*restype_ptr, maxval);
2320 if (unsignedp && unsignedp0)
2322 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2323 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2324 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2325 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2327 else
2329 min_gt = INT_CST_LT (primop1, minval);
2330 max_gt = INT_CST_LT (primop1, maxval);
2331 min_lt = INT_CST_LT (minval, primop1);
2332 max_lt = INT_CST_LT (maxval, primop1);
2335 val = 0;
2336 /* This used to be a switch, but Genix compiler can't handle that. */
2337 if (code == NE_EXPR)
2339 if (max_lt || min_gt)
2340 val = boolean_true_node;
2342 else if (code == EQ_EXPR)
2344 if (max_lt || min_gt)
2345 val = boolean_false_node;
2347 else if (code == LT_EXPR)
2349 if (max_lt)
2350 val = boolean_true_node;
2351 if (!min_lt)
2352 val = boolean_false_node;
2354 else if (code == GT_EXPR)
2356 if (min_gt)
2357 val = boolean_true_node;
2358 if (!max_gt)
2359 val = boolean_false_node;
2361 else if (code == LE_EXPR)
2363 if (!max_gt)
2364 val = boolean_true_node;
2365 if (min_gt)
2366 val = boolean_false_node;
2368 else if (code == GE_EXPR)
2370 if (!min_lt)
2371 val = boolean_true_node;
2372 if (max_lt)
2373 val = boolean_false_node;
2376 /* If primop0 was sign-extended and unsigned comparison specd,
2377 we did a signed comparison above using the signed type bounds.
2378 But the comparison we output must be unsigned.
2380 Also, for inequalities, VAL is no good; but if the signed
2381 comparison had *any* fixed result, it follows that the
2382 unsigned comparison just tests the sign in reverse
2383 (positive values are LE, negative ones GE).
2384 So we can generate an unsigned comparison
2385 against an extreme value of the signed type. */
2387 if (unsignedp && !unsignedp0)
2389 if (val != 0)
2390 switch (code)
2392 case LT_EXPR:
2393 case GE_EXPR:
2394 primop1 = TYPE_MIN_VALUE (type);
2395 val = 0;
2396 break;
2398 case LE_EXPR:
2399 case GT_EXPR:
2400 primop1 = TYPE_MAX_VALUE (type);
2401 val = 0;
2402 break;
2404 default:
2405 break;
2407 type = unsigned_type (type);
2410 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2412 /* This is the case of (char)x >?< 0x80, which people used to use
2413 expecting old C compilers to change the 0x80 into -0x80. */
2414 if (val == boolean_false_node)
2415 warning ("comparison is always false due to limited range of data type");
2416 if (val == boolean_true_node)
2417 warning ("comparison is always true due to limited range of data type");
2420 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2422 /* This is the case of (unsigned char)x >?< -1 or < 0. */
2423 if (val == boolean_false_node)
2424 warning ("comparison is always false due to limited range of data type");
2425 if (val == boolean_true_node)
2426 warning ("comparison is always true due to limited range of data type");
2429 if (val != 0)
2431 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2432 if (TREE_SIDE_EFFECTS (primop0))
2433 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2434 return val;
2437 /* Value is not predetermined, but do the comparison
2438 in the type of the operand that is not constant.
2439 TYPE is already properly set. */
2441 else if (real1 && real2
2442 && (TYPE_PRECISION (TREE_TYPE (primop0))
2443 == TYPE_PRECISION (TREE_TYPE (primop1))))
2444 type = TREE_TYPE (primop0);
2446 /* If args' natural types are both narrower than nominal type
2447 and both extend in the same manner, compare them
2448 in the type of the wider arg.
2449 Otherwise must actually extend both to the nominal
2450 common type lest different ways of extending
2451 alter the result.
2452 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2454 else if (unsignedp0 == unsignedp1 && real1 == real2
2455 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2456 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2458 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2459 type = signed_or_unsigned_type (unsignedp0
2460 || TREE_UNSIGNED (*restype_ptr),
2461 type);
2462 /* Make sure shorter operand is extended the right way
2463 to match the longer operand. */
2464 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2465 primop0);
2466 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2467 primop1);
2469 else
2471 /* Here we must do the comparison on the nominal type
2472 using the args exactly as we received them. */
2473 type = *restype_ptr;
2474 primop0 = op0;
2475 primop1 = op1;
2477 if (!real1 && !real2 && integer_zerop (primop1)
2478 && TREE_UNSIGNED (*restype_ptr))
2480 tree value = 0;
2481 switch (code)
2483 case GE_EXPR:
2484 /* All unsigned values are >= 0, so we warn if extra warnings
2485 are requested. However, if OP0 is a constant that is
2486 >= 0, the signedness of the comparison isn't an issue,
2487 so suppress the warning. */
2488 if (extra_warnings && !in_system_header
2489 && ! (TREE_CODE (primop0) == INTEGER_CST
2490 && ! TREE_OVERFLOW (convert (signed_type (type),
2491 primop0))))
2492 warning ("comparison of unsigned expression >= 0 is always true");
2493 value = boolean_true_node;
2494 break;
2496 case LT_EXPR:
2497 if (extra_warnings && !in_system_header
2498 && ! (TREE_CODE (primop0) == INTEGER_CST
2499 && ! TREE_OVERFLOW (convert (signed_type (type),
2500 primop0))))
2501 warning ("comparison of unsigned expression < 0 is always false");
2502 value = boolean_false_node;
2503 break;
2505 default:
2506 break;
2509 if (value != 0)
2511 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2512 if (TREE_SIDE_EFFECTS (primop0))
2513 return build (COMPOUND_EXPR, TREE_TYPE (value),
2514 primop0, value);
2515 return value;
2520 *op0_ptr = convert (type, primop0);
2521 *op1_ptr = convert (type, primop1);
2523 *restype_ptr = boolean_type_node;
2525 return 0;
2528 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2529 or validate its data type for an `if' or `while' statement or ?..: exp.
2531 This preparation consists of taking the ordinary
2532 representation of an expression expr and producing a valid tree
2533 boolean expression describing whether expr is nonzero. We could
2534 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2535 but we optimize comparisons, &&, ||, and !.
2537 The resulting type should always be `boolean_type_node'. */
2539 tree
2540 truthvalue_conversion (expr)
2541 tree expr;
2543 if (TREE_CODE (expr) == ERROR_MARK)
2544 return expr;
2546 #if 0 /* This appears to be wrong for C++. */
2547 /* These really should return error_mark_node after 2.4 is stable.
2548 But not all callers handle ERROR_MARK properly. */
2549 switch (TREE_CODE (TREE_TYPE (expr)))
2551 case RECORD_TYPE:
2552 error ("struct type value used where scalar is required");
2553 return boolean_false_node;
2555 case UNION_TYPE:
2556 error ("union type value used where scalar is required");
2557 return boolean_false_node;
2559 case ARRAY_TYPE:
2560 error ("array type value used where scalar is required");
2561 return boolean_false_node;
2563 default:
2564 break;
2566 #endif /* 0 */
2568 switch (TREE_CODE (expr))
2570 case EQ_EXPR:
2571 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2572 case TRUTH_ANDIF_EXPR:
2573 case TRUTH_ORIF_EXPR:
2574 case TRUTH_AND_EXPR:
2575 case TRUTH_OR_EXPR:
2576 case TRUTH_XOR_EXPR:
2577 case TRUTH_NOT_EXPR:
2578 TREE_TYPE (expr) = boolean_type_node;
2579 return expr;
2581 case ERROR_MARK:
2582 return expr;
2584 case INTEGER_CST:
2585 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2587 case REAL_CST:
2588 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2590 case ADDR_EXPR:
2591 /* If we are taking the address of a external decl, it might be zero
2592 if it is weak, so we cannot optimize. */
2593 if (DECL_P (TREE_OPERAND (expr, 0))
2594 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2595 break;
2597 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2598 return build (COMPOUND_EXPR, boolean_type_node,
2599 TREE_OPERAND (expr, 0), boolean_true_node);
2600 else
2601 return boolean_true_node;
2603 case COMPLEX_EXPR:
2604 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2605 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2606 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2607 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2610 case NEGATE_EXPR:
2611 case ABS_EXPR:
2612 case FLOAT_EXPR:
2613 case FFS_EXPR:
2614 /* These don't change whether an object is non-zero or zero. */
2615 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2617 case LROTATE_EXPR:
2618 case RROTATE_EXPR:
2619 /* These don't change whether an object is zero or non-zero, but
2620 we can't ignore them if their second arg has side-effects. */
2621 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2622 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2623 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2624 else
2625 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2627 case COND_EXPR:
2628 /* Distribute the conversion into the arms of a COND_EXPR. */
2629 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2630 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2631 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2633 case CONVERT_EXPR:
2634 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2635 since that affects how `default_conversion' will behave. */
2636 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2637 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2638 break;
2639 /* fall through... */
2640 case NOP_EXPR:
2641 /* If this is widening the argument, we can ignore it. */
2642 if (TYPE_PRECISION (TREE_TYPE (expr))
2643 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2644 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2645 break;
2647 case MINUS_EXPR:
2648 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2649 this case. */
2650 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2651 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2652 break;
2653 /* fall through... */
2654 case BIT_XOR_EXPR:
2655 /* This and MINUS_EXPR can be changed into a comparison of the
2656 two objects. */
2657 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2658 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2659 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2660 TREE_OPERAND (expr, 1), 1);
2661 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2662 fold (build1 (NOP_EXPR,
2663 TREE_TYPE (TREE_OPERAND (expr, 0)),
2664 TREE_OPERAND (expr, 1))), 1);
2666 case BIT_AND_EXPR:
2667 if (integer_onep (TREE_OPERAND (expr, 1))
2668 && TREE_TYPE (expr) != boolean_type_node)
2669 /* Using convert here would cause infinite recursion. */
2670 return build1 (NOP_EXPR, boolean_type_node, expr);
2671 break;
2673 case MODIFY_EXPR:
2674 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2675 warning ("suggest parentheses around assignment used as truth value");
2676 break;
2678 default:
2679 break;
2682 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2684 tree tem = save_expr (expr);
2685 return (build_binary_op
2686 ((TREE_SIDE_EFFECTS (expr)
2687 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2688 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
2689 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
2690 0));
2693 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2696 static tree builtin_function_2 PARAMS ((const char *, const char *, tree, tree,
2697 int, enum built_in_class, int, int,
2698 int));
2700 /* Make a variant type in the proper way for C/C++, propagating qualifiers
2701 down to the element type of an array. */
2703 tree
2704 c_build_qualified_type (type, type_quals)
2705 tree type;
2706 int type_quals;
2708 /* A restrict-qualified pointer type must be a pointer to object or
2709 incomplete type. Note that the use of POINTER_TYPE_P also allows
2710 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
2711 the C++ front-end also use POINTER_TYPE for pointer-to-member
2712 values, so even though it should be illegal to use `restrict'
2713 with such an entity we don't flag that here. Thus, special case
2714 code for that case is required in the C++ front-end. */
2715 if ((type_quals & TYPE_QUAL_RESTRICT)
2716 && (!POINTER_TYPE_P (type)
2717 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
2719 error ("invalid use of `restrict'");
2720 type_quals &= ~TYPE_QUAL_RESTRICT;
2723 if (TREE_CODE (type) == ARRAY_TYPE)
2724 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
2725 type_quals),
2726 TYPE_DOMAIN (type));
2727 return build_qualified_type (type, type_quals);
2730 /* Apply the TYPE_QUALS to the new DECL. */
2732 void
2733 c_apply_type_quals_to_decl (type_quals, decl)
2734 int type_quals;
2735 tree decl;
2737 if ((type_quals & TYPE_QUAL_CONST)
2738 || (TREE_TYPE (decl)
2739 && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE))
2740 TREE_READONLY (decl) = 1;
2741 if (type_quals & TYPE_QUAL_VOLATILE)
2743 TREE_SIDE_EFFECTS (decl) = 1;
2744 TREE_THIS_VOLATILE (decl) = 1;
2746 if (type_quals & TYPE_QUAL_RESTRICT)
2748 if (!TREE_TYPE (decl)
2749 || !POINTER_TYPE_P (TREE_TYPE (decl))
2750 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
2751 error ("invalid use of `restrict'");
2752 else if (flag_strict_aliasing)
2754 /* No two restricted pointers can point at the same thing.
2755 However, a restricted pointer can point at the same thing
2756 as an unrestricted pointer, if that unrestricted pointer
2757 is based on the restricted pointer. So, we make the
2758 alias set for the restricted pointer a subset of the
2759 alias set for the type pointed to by the type of the
2760 decl. */
2762 HOST_WIDE_INT pointed_to_alias_set
2763 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
2765 if (pointed_to_alias_set == 0)
2766 /* It's not legal to make a subset of alias set zero. */
2768 else
2770 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
2771 record_alias_subset (pointed_to_alias_set,
2772 DECL_POINTER_ALIAS_SET (decl));
2779 /* Return the typed-based alias set for T, which may be an expression
2780 or a type. Return -1 if we don't do anything special. */
2782 HOST_WIDE_INT
2783 lang_get_alias_set (t)
2784 tree t;
2786 tree u;
2788 /* We know nothing about vector types */
2789 if (TREE_CODE (t) == VECTOR_TYPE)
2790 return 0;
2792 /* Permit type-punning when accessing a union, provided the access
2793 is directly through the union. For example, this code does not
2794 permit taking the address of a union member and then storing
2795 through it. Even the type-punning allowed here is a GCC
2796 extension, albeit a common and useful one; the C standard says
2797 that such accesses have implementation-defined behavior. */
2798 for (u = t;
2799 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
2800 u = TREE_OPERAND (u, 0))
2801 if (TREE_CODE (u) == COMPONENT_REF
2802 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
2803 return 0;
2805 /* If this is a char *, the ANSI C standard says it can alias
2806 anything. Note that all references need do this. */
2807 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'r'
2808 && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
2809 && TYPE_PRECISION (TREE_TYPE (t)) == TYPE_PRECISION (char_type_node))
2810 return 0;
2812 /* That's all the expressions we handle specially. */
2813 if (! TYPE_P (t))
2814 return -1;
2816 /* The C standard specifically allows aliasing between signed and
2817 unsigned variants of the same type. We treat the signed
2818 variant as canonical. */
2819 if (TREE_CODE (t) == INTEGER_TYPE && TREE_UNSIGNED (t))
2821 tree t1 = signed_type (t);
2823 /* t1 == t can happen for boolean nodes which are always unsigned. */
2824 if (t1 != t)
2825 return get_alias_set (t1);
2827 else if (POINTER_TYPE_P (t))
2829 tree t1;
2831 /* Unfortunately, there is no canonical form of a pointer type.
2832 In particular, if we have `typedef int I', then `int *', and
2833 `I *' are different types. So, we have to pick a canonical
2834 representative. We do this below.
2836 Technically, this approach is actually more conservative that
2837 it needs to be. In particular, `const int *' and `int *'
2838 chould be in different alias sets, according to the C and C++
2839 standard, since their types are not the same, and so,
2840 technically, an `int **' and `const int **' cannot point at
2841 the same thing.
2843 But, the standard is wrong. In particular, this code is
2844 legal C++:
2846 int *ip;
2847 int **ipp = &ip;
2848 const int* const* cipp = &ip;
2850 And, it doesn't make sense for that to be legal unless you
2851 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
2852 the pointed-to types. This issue has been reported to the
2853 C++ committee. */
2854 t1 = build_type_no_quals (t);
2855 if (t1 != t)
2856 return get_alias_set (t1);
2858 /* It's not yet safe to use alias sets for classes in C++ because
2859 the TYPE_FIELDs list for a class doesn't mention base classes. */
2860 else if (c_language == clk_cplusplus && AGGREGATE_TYPE_P (t))
2861 return 0;
2863 return -1;
2866 /* Build tree nodes and builtin functions common to both C and C++ language
2867 frontends. */
2869 void
2870 c_common_nodes_and_builtins ()
2872 int wchar_type_size;
2873 tree array_domain_type;
2874 tree temp;
2875 tree memcpy_ftype, memset_ftype, strlen_ftype;
2876 tree bzero_ftype, bcmp_ftype, puts_ftype, printf_ftype;
2877 tree fputs_ftype, fputc_ftype, fwrite_ftype, fprintf_ftype;
2878 tree endlink, int_endlink, double_endlink, unsigned_endlink;
2879 tree cstring_endlink, sizetype_endlink;
2880 tree ptr_ftype, ptr_ftype_unsigned;
2881 tree void_ftype_any, void_ftype_int, int_ftype_any;
2882 tree double_ftype_double, double_ftype_double_double;
2883 tree float_ftype_float, ldouble_ftype_ldouble;
2884 tree cfloat_ftype_cfloat, cdouble_ftype_cdouble, cldouble_ftype_cldouble;
2885 tree float_ftype_cfloat, double_ftype_cdouble, ldouble_ftype_cldouble;
2886 tree int_ftype_cptr_cptr_sizet, sizet_ftype_cstring_cstring;
2887 tree int_ftype_cstring_cstring, string_ftype_string_cstring;
2888 tree string_ftype_cstring_int, string_ftype_cstring_cstring;
2889 tree string_ftype_string_cstring_sizet, int_ftype_cstring_cstring_sizet;
2890 tree long_ftype_long;
2891 tree longlong_ftype_longlong;
2892 tree intmax_ftype_intmax;
2893 /* Either char* or void*. */
2894 tree traditional_ptr_type_node;
2895 /* Either const char* or const void*. */
2896 tree traditional_cptr_type_node;
2897 tree traditional_len_type_node;
2898 tree traditional_len_endlink;
2899 tree va_list_ref_type_node;
2900 tree va_list_arg_type_node;
2902 /* Define `int' and `char' first so that dbx will output them first. */
2903 record_builtin_type (RID_INT, NULL, integer_type_node);
2904 record_builtin_type (RID_CHAR, "char", char_type_node);
2906 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
2907 "unsigned long", "long long unsigned" and "unsigned short" were in C++
2908 but not C. Are the conditionals here needed? */
2909 if (c_language == clk_cplusplus)
2910 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
2911 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
2912 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
2913 record_builtin_type (RID_MAX, "long unsigned int",
2914 long_unsigned_type_node);
2915 if (c_language == clk_cplusplus)
2916 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
2917 record_builtin_type (RID_MAX, "long long int",
2918 long_long_integer_type_node);
2919 record_builtin_type (RID_MAX, "long long unsigned int",
2920 long_long_unsigned_type_node);
2921 if (c_language == clk_cplusplus)
2922 record_builtin_type (RID_MAX, "long long unsigned",
2923 long_long_unsigned_type_node);
2924 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
2925 record_builtin_type (RID_MAX, "short unsigned int",
2926 short_unsigned_type_node);
2927 if (c_language == clk_cplusplus)
2928 record_builtin_type (RID_MAX, "unsigned short",
2929 short_unsigned_type_node);
2931 /* Define both `signed char' and `unsigned char'. */
2932 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
2933 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
2935 /* These are types that type_for_size and type_for_mode use. */
2936 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intQI_type_node));
2937 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intHI_type_node));
2938 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intSI_type_node));
2939 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intDI_type_node));
2940 #if HOST_BITS_PER_WIDE_INT >= 64
2941 pushdecl (build_decl (TYPE_DECL, get_identifier ("__int128_t"), intTI_type_node));
2942 #endif
2943 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intQI_type_node));
2944 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intHI_type_node));
2945 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intSI_type_node));
2946 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intDI_type_node));
2947 #if HOST_BITS_PER_WIDE_INT >= 64
2948 pushdecl (build_decl (TYPE_DECL, get_identifier ("__uint128_t"), unsigned_intTI_type_node));
2949 #endif
2951 /* Create the widest literal types. */
2952 widest_integer_literal_type_node
2953 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
2954 pushdecl (build_decl (TYPE_DECL, NULL_TREE,
2955 widest_integer_literal_type_node));
2957 widest_unsigned_literal_type_node
2958 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
2959 pushdecl (build_decl (TYPE_DECL, NULL_TREE,
2960 widest_unsigned_literal_type_node));
2962 /* `unsigned long' is the standard type for sizeof.
2963 Note that stddef.h uses `unsigned long',
2964 and this must agree, even if long and int are the same size. */
2965 c_size_type_node =
2966 TREE_TYPE (identifier_global_value (get_identifier (SIZE_TYPE)));
2967 signed_size_type_node = signed_type (c_size_type_node);
2968 if (flag_traditional)
2969 c_size_type_node = signed_size_type_node;
2970 set_sizetype (c_size_type_node);
2972 build_common_tree_nodes_2 (flag_short_double);
2974 record_builtin_type (RID_FLOAT, NULL, float_type_node);
2975 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
2976 record_builtin_type (RID_MAX, "long double", long_double_type_node);
2978 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex int"),
2979 complex_integer_type_node));
2980 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex float"),
2981 complex_float_type_node));
2982 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex double"),
2983 complex_double_type_node));
2984 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex long double"),
2985 complex_long_double_type_node));
2987 record_builtin_type (RID_VOID, NULL, void_type_node);
2989 void_list_node = build_void_list_node ();
2991 /* Make a type to be the domain of a few array types
2992 whose domains don't really matter.
2993 200 is small enough that it always fits in size_t
2994 and large enough that it can hold most function names for the
2995 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
2996 array_domain_type = build_index_type (size_int (200));
2998 /* Make a type for arrays of characters.
2999 With luck nothing will ever really depend on the length of this
3000 array type. */
3001 char_array_type_node
3002 = build_array_type (char_type_node, array_domain_type);
3004 /* Likewise for arrays of ints. */
3005 int_array_type_node
3006 = build_array_type (integer_type_node, array_domain_type);
3008 #ifdef MD_INIT_BUILTINS
3009 MD_INIT_BUILTINS;
3010 #endif
3012 /* This is special for C++ so functions can be overloaded. */
3013 wchar_type_node = get_identifier (flag_short_wchar
3014 ? "short unsigned int"
3015 : WCHAR_TYPE);
3016 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
3017 wchar_type_size = TYPE_PRECISION (wchar_type_node);
3018 if (c_language == clk_cplusplus)
3020 if (TREE_UNSIGNED (wchar_type_node))
3021 wchar_type_node = make_unsigned_type (wchar_type_size);
3022 else
3023 wchar_type_node = make_signed_type (wchar_type_size);
3024 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
3026 else
3028 signed_wchar_type_node = signed_type (wchar_type_node);
3029 unsigned_wchar_type_node = unsigned_type (wchar_type_node);
3032 /* This is for wide string constants. */
3033 wchar_array_type_node
3034 = build_array_type (wchar_type_node, array_domain_type);
3036 string_type_node = build_pointer_type (char_type_node);
3037 const_string_type_node
3038 = build_pointer_type (build_type_variant (char_type_node, 1, 0));
3040 wint_type_node =
3041 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
3043 intmax_type_node =
3044 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
3045 uintmax_type_node =
3046 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
3048 default_function_type = build_function_type (integer_type_node, NULL_TREE);
3049 ptrdiff_type_node
3050 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
3051 unsigned_ptrdiff_type_node = unsigned_type (ptrdiff_type_node);
3053 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
3054 va_list_type_node));
3056 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"),
3057 ptrdiff_type_node));
3059 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"),
3060 sizetype));
3062 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
3064 va_list_arg_type_node = va_list_ref_type_node =
3065 build_pointer_type (TREE_TYPE (va_list_type_node));
3067 else
3069 va_list_arg_type_node = va_list_type_node;
3070 va_list_ref_type_node = build_reference_type (va_list_type_node);
3073 endlink = void_list_node;
3074 int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
3075 double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
3076 unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
3077 cstring_endlink = tree_cons (NULL_TREE, const_string_type_node, endlink);
3079 ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
3080 ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
3081 sizetype_endlink = tree_cons (NULL_TREE, TYPE_DOMAIN (sizetype), endlink);
3082 /* We realloc here because sizetype could be int or unsigned. S'ok. */
3083 ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
3085 int_ftype_any = build_function_type (integer_type_node, NULL_TREE);
3086 void_ftype_any = build_function_type (void_type_node, NULL_TREE);
3087 void_ftype = build_function_type (void_type_node, endlink);
3088 void_ftype_int = build_function_type (void_type_node, int_endlink);
3089 void_ftype_ptr
3090 = build_function_type (void_type_node,
3091 tree_cons (NULL_TREE, ptr_type_node, endlink));
3093 float_ftype_float
3094 = build_function_type (float_type_node,
3095 tree_cons (NULL_TREE, float_type_node, endlink));
3097 double_ftype_double
3098 = build_function_type (double_type_node, double_endlink);
3100 ldouble_ftype_ldouble
3101 = build_function_type (long_double_type_node,
3102 tree_cons (NULL_TREE, long_double_type_node,
3103 endlink));
3105 double_ftype_double_double
3106 = build_function_type (double_type_node,
3107 tree_cons (NULL_TREE, double_type_node,
3108 double_endlink));
3110 cfloat_ftype_cfloat
3111 = build_function_type (complex_float_type_node,
3112 tree_cons (NULL_TREE, complex_float_type_node,
3113 endlink));
3114 cdouble_ftype_cdouble
3115 = build_function_type (complex_double_type_node,
3116 tree_cons (NULL_TREE, complex_double_type_node,
3117 endlink));
3118 cldouble_ftype_cldouble
3119 = build_function_type (complex_long_double_type_node,
3120 tree_cons (NULL_TREE, complex_long_double_type_node,
3121 endlink));
3123 float_ftype_cfloat
3124 = build_function_type (float_type_node,
3125 tree_cons (NULL_TREE, complex_float_type_node,
3126 endlink));
3127 double_ftype_cdouble
3128 = build_function_type (double_type_node,
3129 tree_cons (NULL_TREE, complex_double_type_node,
3130 endlink));
3131 ldouble_ftype_cldouble
3132 = build_function_type (long_double_type_node,
3133 tree_cons (NULL_TREE, complex_long_double_type_node,
3134 endlink));
3136 int_ftype_int
3137 = build_function_type (integer_type_node, int_endlink);
3139 long_ftype_long
3140 = build_function_type (long_integer_type_node,
3141 tree_cons (NULL_TREE, long_integer_type_node,
3142 endlink));
3144 longlong_ftype_longlong
3145 = build_function_type (long_long_integer_type_node,
3146 tree_cons (NULL_TREE, long_long_integer_type_node,
3147 endlink));
3149 intmax_ftype_intmax
3150 = build_function_type (intmax_type_node,
3151 tree_cons (NULL_TREE, intmax_type_node,
3152 endlink));
3154 int_ftype_cptr_cptr_sizet
3155 = build_function_type (integer_type_node,
3156 tree_cons (NULL_TREE, const_ptr_type_node,
3157 tree_cons (NULL_TREE,
3158 const_ptr_type_node,
3159 sizetype_endlink)));
3161 void_zero_node = build_int_2 (0, 0);
3162 TREE_TYPE (void_zero_node) = void_type_node;
3164 /* Prototype for strcpy/strcat. */
3165 string_ftype_string_cstring
3166 = build_function_type (string_type_node,
3167 tree_cons (NULL_TREE, string_type_node,
3168 cstring_endlink));
3170 /* Prototype for strncpy/strncat. */
3171 string_ftype_string_cstring_sizet
3172 = build_function_type (string_type_node,
3173 tree_cons (NULL_TREE, string_type_node,
3174 tree_cons (NULL_TREE,
3175 const_string_type_node,
3176 sizetype_endlink)));
3178 traditional_len_type_node = ((flag_traditional &&
3179 c_language != clk_cplusplus)
3180 ? integer_type_node : sizetype);
3181 traditional_len_endlink = tree_cons (NULL_TREE, traditional_len_type_node,
3182 endlink);
3184 /* Prototype for strcmp. */
3185 int_ftype_cstring_cstring
3186 = build_function_type (integer_type_node,
3187 tree_cons (NULL_TREE, const_string_type_node,
3188 cstring_endlink));
3190 /* Prototype for strspn/strcspn. */
3191 sizet_ftype_cstring_cstring
3192 = build_function_type (c_size_type_node,
3193 tree_cons (NULL_TREE, const_string_type_node,
3194 cstring_endlink));
3196 /* Prototype for strncmp. */
3197 int_ftype_cstring_cstring_sizet
3198 = build_function_type (integer_type_node,
3199 tree_cons (NULL_TREE, const_string_type_node,
3200 tree_cons (NULL_TREE,
3201 const_string_type_node,
3202 sizetype_endlink)));
3204 /* Prototype for strstr, strpbrk, etc. */
3205 string_ftype_cstring_cstring
3206 = build_function_type (string_type_node,
3207 tree_cons (NULL_TREE, const_string_type_node,
3208 cstring_endlink));
3210 /* Prototype for strchr. */
3211 string_ftype_cstring_int
3212 = build_function_type (string_type_node,
3213 tree_cons (NULL_TREE, const_string_type_node,
3214 int_endlink));
3216 /* Prototype for strlen. */
3217 strlen_ftype
3218 = build_function_type (traditional_len_type_node, cstring_endlink);
3220 traditional_ptr_type_node = ((flag_traditional &&
3221 c_language != clk_cplusplus)
3222 ? string_type_node : ptr_type_node);
3223 traditional_cptr_type_node = ((flag_traditional &&
3224 c_language != clk_cplusplus)
3225 ? const_string_type_node : const_ptr_type_node);
3227 /* Prototype for memcpy. */
3228 memcpy_ftype
3229 = build_function_type (traditional_ptr_type_node,
3230 tree_cons (NULL_TREE, ptr_type_node,
3231 tree_cons (NULL_TREE, const_ptr_type_node,
3232 sizetype_endlink)));
3234 /* Prototype for memset. */
3235 memset_ftype
3236 = build_function_type (traditional_ptr_type_node,
3237 tree_cons (NULL_TREE, ptr_type_node,
3238 tree_cons (NULL_TREE, integer_type_node,
3239 sizetype_endlink)));
3241 /* Prototype for bzero. */
3242 bzero_ftype
3243 = build_function_type (void_type_node,
3244 tree_cons (NULL_TREE, traditional_ptr_type_node,
3245 traditional_len_endlink));
3247 /* Prototype for bcmp. */
3248 bcmp_ftype
3249 = build_function_type (integer_type_node,
3250 tree_cons (NULL_TREE, traditional_cptr_type_node,
3251 tree_cons (NULL_TREE,
3252 traditional_cptr_type_node,
3253 traditional_len_endlink)));
3255 /* Prototype for puts. */
3256 puts_ftype
3257 = build_function_type (integer_type_node, cstring_endlink);
3259 /* Prototype for printf. */
3260 printf_ftype
3261 = build_function_type (integer_type_node,
3262 tree_cons (NULL_TREE, const_string_type_node,
3263 NULL_TREE));
3265 /* These stdio prototypes are declared using void* in place of
3266 FILE*. They are only used for __builtin_ style calls, regular
3267 style builtin prototypes omit the arguments and merge those
3268 provided by stdio.h. */
3269 /* Prototype for fwrite. */
3270 fwrite_ftype
3271 = build_function_type (c_size_type_node,
3272 tree_cons (NULL_TREE, const_ptr_type_node,
3273 tree_cons (NULL_TREE, c_size_type_node,
3274 tree_cons (NULL_TREE, c_size_type_node,
3275 tree_cons (NULL_TREE, ptr_type_node, endlink)))));
3277 /* Prototype for fputc. */
3278 fputc_ftype
3279 = build_function_type (integer_type_node,
3280 tree_cons (NULL_TREE, integer_type_node,
3281 tree_cons (NULL_TREE, ptr_type_node, endlink)));
3283 /* Prototype for fputs. */
3284 fputs_ftype
3285 = build_function_type (integer_type_node,
3286 tree_cons (NULL_TREE, const_string_type_node,
3287 tree_cons (NULL_TREE, ptr_type_node, endlink)));
3289 /* Prototype for fprintf. */
3290 fprintf_ftype
3291 = build_function_type (integer_type_node,
3292 tree_cons (NULL_TREE, ptr_type_node,
3293 tree_cons (NULL_TREE,
3294 const_string_type_node,
3295 NULL_TREE)));
3297 builtin_function ("__builtin_constant_p", default_function_type,
3298 BUILT_IN_CONSTANT_P, BUILT_IN_NORMAL, NULL);
3300 builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
3301 BUILT_IN_RETURN_ADDRESS, BUILT_IN_NORMAL, NULL);
3303 builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
3304 BUILT_IN_FRAME_ADDRESS, BUILT_IN_NORMAL, NULL);
3306 #ifdef EH_RETURN_DATA_REGNO
3307 builtin_function ("__builtin_eh_return_data_regno", int_ftype_int,
3308 BUILT_IN_EH_RETURN_DATA_REGNO, BUILT_IN_NORMAL, NULL);
3309 #endif
3311 builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
3312 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, "alloca");
3313 builtin_function_2 ("__builtin_ffs", "ffs",
3314 int_ftype_int, int_ftype_int,
3315 BUILT_IN_FFS, BUILT_IN_NORMAL, 0, 1, 0);
3316 /* Define alloca as builtin, unless SMALL_STACK. */
3317 #ifndef SMALL_STACK
3318 builtin_function_2 (NULL, "alloca", NULL_TREE, ptr_ftype_sizetype,
3319 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, 0, 1, 0);
3320 #endif
3321 /* Declare _exit and _Exit just to mark them as non-returning. */
3322 builtin_function_2 (NULL, "_exit", NULL_TREE, void_ftype_int,
3323 0, NOT_BUILT_IN, 0, 1, 1);
3324 builtin_function_2 (NULL, "_Exit", NULL_TREE, void_ftype_int,
3325 0, NOT_BUILT_IN, 0, !flag_isoc99, 1);
3327 builtin_function_2 ("__builtin_index", "index",
3328 string_ftype_cstring_int, string_ftype_cstring_int,
3329 BUILT_IN_INDEX, BUILT_IN_NORMAL, 1, 1, 0);
3330 builtin_function_2 ("__builtin_rindex", "rindex",
3331 string_ftype_cstring_int, string_ftype_cstring_int,
3332 BUILT_IN_RINDEX, BUILT_IN_NORMAL, 1, 1, 0);
3334 /* The system prototypes for these functions have many
3335 variations, so don't specify parameters to avoid conflicts.
3336 The expand_* functions check the argument types anyway. */
3337 builtin_function_2 ("__builtin_bzero", "bzero",
3338 bzero_ftype, void_ftype_any,
3339 BUILT_IN_BZERO, BUILT_IN_NORMAL, 1, 1, 0);
3340 builtin_function_2 ("__builtin_bcmp", "bcmp",
3341 bcmp_ftype, int_ftype_any,
3342 BUILT_IN_BCMP, BUILT_IN_NORMAL, 1, 1, 0);
3344 builtin_function_2 ("__builtin_abs", "abs",
3345 int_ftype_int, int_ftype_int,
3346 BUILT_IN_ABS, BUILT_IN_NORMAL, 0, 0, 0);
3347 builtin_function_2 ("__builtin_fabsf", "fabsf",
3348 float_ftype_float, float_ftype_float,
3349 BUILT_IN_FABS, BUILT_IN_NORMAL, 0, 0, 0);
3350 builtin_function_2 ("__builtin_fabs", "fabs",
3351 double_ftype_double, double_ftype_double,
3352 BUILT_IN_FABS, BUILT_IN_NORMAL, 0, 0, 0);
3353 builtin_function_2 ("__builtin_fabsl", "fabsl",
3354 ldouble_ftype_ldouble, ldouble_ftype_ldouble,
3355 BUILT_IN_FABS, BUILT_IN_NORMAL, 0, 0, 0);
3356 builtin_function_2 ("__builtin_labs", "labs",
3357 long_ftype_long, long_ftype_long,
3358 BUILT_IN_ABS, BUILT_IN_NORMAL, 0, 0, 0);
3359 builtin_function_2 ("__builtin_llabs", "llabs",
3360 longlong_ftype_longlong, longlong_ftype_longlong,
3361 BUILT_IN_ABS, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3362 builtin_function_2 ("__builtin_imaxabs", "imaxabs",
3363 intmax_ftype_intmax, intmax_ftype_intmax,
3364 BUILT_IN_ABS, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3366 builtin_function ("__builtin_saveregs", ptr_ftype, BUILT_IN_SAVEREGS,
3367 BUILT_IN_NORMAL, NULL);
3368 builtin_function ("__builtin_classify_type", default_function_type,
3369 BUILT_IN_CLASSIFY_TYPE, BUILT_IN_NORMAL, NULL);
3370 builtin_function ("__builtin_next_arg", ptr_ftype, BUILT_IN_NEXT_ARG,
3371 BUILT_IN_NORMAL, NULL);
3372 builtin_function ("__builtin_args_info", int_ftype_int, BUILT_IN_ARGS_INFO,
3373 BUILT_IN_NORMAL, NULL);
3374 builtin_function ("__builtin_setjmp",
3375 build_function_type (integer_type_node,
3376 tree_cons (NULL_TREE, ptr_type_node,
3377 endlink)),
3378 BUILT_IN_SETJMP, BUILT_IN_NORMAL, NULL);
3379 builtin_function ("__builtin_longjmp",
3380 build_function_type (void_type_node,
3381 tree_cons (NULL_TREE, ptr_type_node,
3382 int_endlink)),
3383 BUILT_IN_LONGJMP, BUILT_IN_NORMAL, NULL);
3384 builtin_function ("__builtin_trap", void_ftype, BUILT_IN_TRAP,
3385 BUILT_IN_NORMAL, NULL);
3387 /* ISO C99 IEEE Unordered compares. */
3388 builtin_function ("__builtin_isgreater", default_function_type,
3389 BUILT_IN_ISGREATER, BUILT_IN_NORMAL, NULL);
3390 builtin_function ("__builtin_isgreaterequal", default_function_type,
3391 BUILT_IN_ISGREATEREQUAL, BUILT_IN_NORMAL, NULL);
3392 builtin_function ("__builtin_isless", default_function_type,
3393 BUILT_IN_ISLESS, BUILT_IN_NORMAL, NULL);
3394 builtin_function ("__builtin_islessequal", default_function_type,
3395 BUILT_IN_ISLESSEQUAL, BUILT_IN_NORMAL, NULL);
3396 builtin_function ("__builtin_islessgreater", default_function_type,
3397 BUILT_IN_ISLESSGREATER, BUILT_IN_NORMAL, NULL);
3398 builtin_function ("__builtin_isunordered", default_function_type,
3399 BUILT_IN_ISUNORDERED, BUILT_IN_NORMAL, NULL);
3401 /* Untyped call and return. */
3402 builtin_function ("__builtin_apply_args", ptr_ftype,
3403 BUILT_IN_APPLY_ARGS, BUILT_IN_NORMAL, NULL);
3405 temp = tree_cons (NULL_TREE,
3406 build_pointer_type (build_function_type (void_type_node,
3407 NULL_TREE)),
3408 tree_cons (NULL_TREE, ptr_type_node, sizetype_endlink));
3409 builtin_function ("__builtin_apply",
3410 build_function_type (ptr_type_node, temp),
3411 BUILT_IN_APPLY, BUILT_IN_NORMAL, NULL);
3412 builtin_function ("__builtin_return", void_ftype_ptr,
3413 BUILT_IN_RETURN, BUILT_IN_NORMAL, NULL);
3415 /* Support for varargs.h and stdarg.h. */
3416 builtin_function ("__builtin_varargs_start",
3417 build_function_type (void_type_node,
3418 tree_cons (NULL_TREE,
3419 va_list_ref_type_node,
3420 endlink)),
3421 BUILT_IN_VARARGS_START, BUILT_IN_NORMAL, NULL);
3423 builtin_function ("__builtin_stdarg_start",
3424 build_function_type (void_type_node,
3425 tree_cons (NULL_TREE,
3426 va_list_ref_type_node,
3427 NULL_TREE)),
3428 BUILT_IN_STDARG_START, BUILT_IN_NORMAL, NULL);
3430 builtin_function ("__builtin_va_end",
3431 build_function_type (void_type_node,
3432 tree_cons (NULL_TREE,
3433 va_list_ref_type_node,
3434 endlink)),
3435 BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL);
3437 builtin_function ("__builtin_va_copy",
3438 build_function_type (void_type_node,
3439 tree_cons (NULL_TREE,
3440 va_list_ref_type_node,
3441 tree_cons (NULL_TREE,
3442 va_list_arg_type_node,
3443 endlink))),
3444 BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL);
3446 /* ??? Ought to be `T __builtin_expect(T, T)' for any type T. */
3447 builtin_function ("__builtin_expect",
3448 build_function_type (long_integer_type_node,
3449 tree_cons (NULL_TREE,
3450 long_integer_type_node,
3451 tree_cons (NULL_TREE,
3452 long_integer_type_node,
3453 endlink))),
3454 BUILT_IN_EXPECT, BUILT_IN_NORMAL, NULL);
3456 /* Currently under experimentation. */
3457 builtin_function_2 ("__builtin_memcpy", "memcpy",
3458 memcpy_ftype, memcpy_ftype,
3459 BUILT_IN_MEMCPY, BUILT_IN_NORMAL, 1, 0, 0);
3460 builtin_function_2 ("__builtin_memcmp", "memcmp",
3461 int_ftype_cptr_cptr_sizet, int_ftype_cptr_cptr_sizet,
3462 BUILT_IN_MEMCMP, BUILT_IN_NORMAL, 1, 0, 0);
3463 builtin_function_2 ("__builtin_memset", "memset",
3464 memset_ftype, memset_ftype,
3465 BUILT_IN_MEMSET, BUILT_IN_NORMAL, 1, 0, 0);
3466 built_in_decls[BUILT_IN_STRCMP] =
3467 builtin_function_2 ("__builtin_strcmp", "strcmp",
3468 int_ftype_cstring_cstring, int_ftype_cstring_cstring,
3469 BUILT_IN_STRCMP, BUILT_IN_NORMAL, 1, 0, 0);
3470 builtin_function_2 ("__builtin_strncmp", "strncmp",
3471 int_ftype_cstring_cstring_sizet,
3472 int_ftype_cstring_cstring_sizet,
3473 BUILT_IN_STRNCMP, BUILT_IN_NORMAL, 1, 0, 0);
3474 builtin_function_2 ("__builtin_strstr", "strstr",
3475 string_ftype_cstring_cstring, string_ftype_cstring_cstring,
3476 BUILT_IN_STRSTR, BUILT_IN_NORMAL, 1, 0, 0);
3477 builtin_function_2 ("__builtin_strpbrk", "strpbrk",
3478 string_ftype_cstring_cstring, string_ftype_cstring_cstring,
3479 BUILT_IN_STRPBRK, BUILT_IN_NORMAL, 1, 0, 0);
3480 built_in_decls[BUILT_IN_STRCHR] =
3481 builtin_function_2 ("__builtin_strchr", "strchr",
3482 string_ftype_cstring_int, string_ftype_cstring_int,
3483 BUILT_IN_STRCHR, BUILT_IN_NORMAL, 1, 0, 0);
3484 builtin_function_2 ("__builtin_strrchr", "strrchr",
3485 string_ftype_cstring_int, string_ftype_cstring_int,
3486 BUILT_IN_STRRCHR, BUILT_IN_NORMAL, 1, 0, 0);
3487 builtin_function_2 ("__builtin_strcpy", "strcpy",
3488 string_ftype_string_cstring, string_ftype_string_cstring,
3489 BUILT_IN_STRCPY, BUILT_IN_NORMAL, 1, 0, 0);
3490 builtin_function_2 ("__builtin_strncpy", "strncpy",
3491 string_ftype_string_cstring_sizet,
3492 string_ftype_string_cstring_sizet,
3493 BUILT_IN_STRNCPY, BUILT_IN_NORMAL, 1, 0, 0);
3494 built_in_decls[BUILT_IN_STRCAT] =
3495 builtin_function_2 ("__builtin_strcat", "strcat",
3496 string_ftype_string_cstring,
3497 string_ftype_string_cstring,
3498 BUILT_IN_STRCAT, BUILT_IN_NORMAL, 1, 0, 0);
3499 builtin_function_2 ("__builtin_strncat", "strncat",
3500 string_ftype_string_cstring_sizet,
3501 string_ftype_string_cstring_sizet,
3502 BUILT_IN_STRNCAT, BUILT_IN_NORMAL, 1, 0, 0);
3503 builtin_function_2 ("__builtin_strspn", "strspn",
3504 sizet_ftype_cstring_cstring, sizet_ftype_cstring_cstring,
3505 BUILT_IN_STRSPN, BUILT_IN_NORMAL, 1, 0, 0);
3506 builtin_function_2 ("__builtin_strcspn", "strcspn",
3507 sizet_ftype_cstring_cstring, sizet_ftype_cstring_cstring,
3508 BUILT_IN_STRCSPN, BUILT_IN_NORMAL, 1, 0, 0);
3509 built_in_decls[BUILT_IN_STRLEN] =
3510 builtin_function_2 ("__builtin_strlen", "strlen",
3511 strlen_ftype, strlen_ftype,
3512 BUILT_IN_STRLEN, BUILT_IN_NORMAL, 1, 0, 0);
3514 builtin_function_2 ("__builtin_sqrtf", "sqrtf",
3515 float_ftype_float, float_ftype_float,
3516 BUILT_IN_FSQRT, BUILT_IN_NORMAL, 1, 0, 0);
3517 builtin_function_2 ("__builtin_fsqrt", "sqrt",
3518 double_ftype_double, double_ftype_double,
3519 BUILT_IN_FSQRT, BUILT_IN_NORMAL, 1, 0, 0);
3520 builtin_function_2 ("__builtin_sqrtl", "sqrtl",
3521 ldouble_ftype_ldouble, ldouble_ftype_ldouble,
3522 BUILT_IN_FSQRT, BUILT_IN_NORMAL, 1, 0, 0);
3523 builtin_function_2 ("__builtin_sinf", "sinf",
3524 float_ftype_float, float_ftype_float,
3525 BUILT_IN_SIN, BUILT_IN_NORMAL, 1, 0, 0);
3526 builtin_function_2 ("__builtin_sin", "sin",
3527 double_ftype_double, double_ftype_double,
3528 BUILT_IN_SIN, BUILT_IN_NORMAL, 1, 0, 0);
3529 builtin_function_2 ("__builtin_sinl", "sinl",
3530 ldouble_ftype_ldouble, ldouble_ftype_ldouble,
3531 BUILT_IN_SIN, BUILT_IN_NORMAL, 1, 0, 0);
3532 builtin_function_2 ("__builtin_cosf", "cosf",
3533 float_ftype_float, float_ftype_float,
3534 BUILT_IN_COS, BUILT_IN_NORMAL, 1, 0, 0);
3535 builtin_function_2 ("__builtin_cos", "cos",
3536 double_ftype_double, double_ftype_double,
3537 BUILT_IN_COS, BUILT_IN_NORMAL, 1, 0, 0);
3538 builtin_function_2 ("__builtin_cosl", "cosl",
3539 ldouble_ftype_ldouble, ldouble_ftype_ldouble,
3540 BUILT_IN_COS, BUILT_IN_NORMAL, 1, 0, 0);
3542 /* ISO C99 complex arithmetic functions. */
3543 builtin_function_2 ("__builtin_conjf", "conjf",
3544 cfloat_ftype_cfloat, cfloat_ftype_cfloat,
3545 BUILT_IN_CONJ, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3546 builtin_function_2 ("__builtin_conj", "conj",
3547 cdouble_ftype_cdouble, cdouble_ftype_cdouble,
3548 BUILT_IN_CONJ, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3549 builtin_function_2 ("__builtin_conjl", "conjl",
3550 cldouble_ftype_cldouble, cldouble_ftype_cldouble,
3551 BUILT_IN_CONJ, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3552 builtin_function_2 ("__builtin_crealf", "crealf",
3553 float_ftype_cfloat, float_ftype_cfloat,
3554 BUILT_IN_CREAL, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3555 builtin_function_2 ("__builtin_creal", "creal",
3556 double_ftype_cdouble, double_ftype_cdouble,
3557 BUILT_IN_CREAL, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3558 builtin_function_2 ("__builtin_creall", "creall",
3559 ldouble_ftype_cldouble, ldouble_ftype_cldouble,
3560 BUILT_IN_CREAL, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3561 builtin_function_2 ("__builtin_cimagf", "cimagf",
3562 float_ftype_cfloat, float_ftype_cfloat,
3563 BUILT_IN_CIMAG, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3564 builtin_function_2 ("__builtin_cimag", "cimag",
3565 double_ftype_cdouble, double_ftype_cdouble,
3566 BUILT_IN_CIMAG, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3567 builtin_function_2 ("__builtin_cimagl", "cimagl",
3568 ldouble_ftype_cldouble, ldouble_ftype_cldouble,
3569 BUILT_IN_CIMAG, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3571 built_in_decls[BUILT_IN_PUTCHAR] =
3572 builtin_function ("__builtin_putchar", int_ftype_int,
3573 BUILT_IN_PUTCHAR, BUILT_IN_NORMAL, "putchar");
3574 built_in_decls[BUILT_IN_PUTS] =
3575 builtin_function ("__builtin_puts", puts_ftype,
3576 BUILT_IN_PUTS, BUILT_IN_NORMAL, "puts");
3577 builtin_function_2 ("__builtin_printf", "printf",
3578 printf_ftype, printf_ftype,
3579 BUILT_IN_PRINTF, BUILT_IN_FRONTEND, 1, 0, 0);
3580 builtin_function_2 ("__builtin_fprintf", "fprintf",
3581 fprintf_ftype, fprintf_ftype,
3582 BUILT_IN_FPRINTF, BUILT_IN_FRONTEND, 1, 0, 0);
3583 built_in_decls[BUILT_IN_FWRITE] =
3584 builtin_function ("__builtin_fwrite", fwrite_ftype,
3585 BUILT_IN_FWRITE, BUILT_IN_NORMAL, "fwrite");
3586 built_in_decls[BUILT_IN_FPUTC] =
3587 builtin_function ("__builtin_fputc", fputc_ftype,
3588 BUILT_IN_FPUTC, BUILT_IN_NORMAL, "fputc");
3589 /* Declare the __builtin_ style with arguments and the regular style
3590 without them. We rely on stdio.h to supply the arguments for the
3591 regular style declaration since we had to use void* instead of
3592 FILE* in the __builtin_ prototype supplied here. */
3593 built_in_decls[BUILT_IN_FPUTS] =
3594 builtin_function_2 ("__builtin_fputs", "fputs",
3595 fputs_ftype, int_ftype_any,
3596 BUILT_IN_FPUTS, BUILT_IN_NORMAL, 1, 0, 0);
3598 /* Declare these functions non-returning
3599 to avoid spurious "control drops through" warnings. */
3600 builtin_function_2 (NULL, "abort",
3601 NULL_TREE, ((c_language == clk_cplusplus)
3602 ? void_ftype : void_ftype_any),
3603 0, NOT_BUILT_IN, 0, 0, 1);
3605 builtin_function_2 (NULL, "exit",
3606 NULL_TREE, ((c_language == clk_cplusplus)
3607 ? void_ftype_int : void_ftype_any),
3608 0, NOT_BUILT_IN, 0, 0, 1);
3610 #if 0
3611 /* Support for these has not been written in either expand_builtin
3612 or build_function_call. */
3613 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV,
3614 BUILT_IN_NORMAL, NULL);
3615 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV,
3616 BUILT_IN_NORMAL, NULL);
3617 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
3618 BUILT_IN_NORMAL, NULL);
3619 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
3620 BUILT_IN_NORMAL, NULL);
3621 builtin_function ("__builtin_fmod", double_ftype_double_double,
3622 BUILT_IN_FMOD, BUILT_IN_NORMAL, NULL);
3623 builtin_function ("__builtin_frem", double_ftype_double_double,
3624 BUILT_IN_FREM, BUILT_IN_NORMAL, NULL);
3625 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
3626 BUILT_IN_NORMAL, NULL);
3627 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
3628 BUILT_IN_NORMAL, NULL);
3629 #endif
3631 main_identifier_node = get_identifier ("main");
3633 /* ??? Perhaps there's a better place to do this. But it is related
3634 to __builtin_va_arg, so it isn't that off-the-wall. */
3635 lang_type_promotes_to = simple_type_promotes_to;
3638 tree
3639 build_va_arg (expr, type)
3640 tree expr, type;
3642 return build1 (VA_ARG_EXPR, type, expr);
3646 /* Possibly define a builtin function with one or two names. BUILTIN_NAME
3647 is an __builtin_-prefixed name; NAME is the ordinary name; one or both
3648 of these may be NULL (though both being NULL is useless).
3649 BUILTIN_TYPE is the type of the __builtin_-prefixed function;
3650 TYPE is the type of the function with the ordinary name. These
3651 may differ if the ordinary name is declared with a looser type to avoid
3652 conflicts with headers. FUNCTION_CODE and CLASS are as for
3653 builtin_function. If LIBRARY_NAME_P is nonzero, NAME is passed as
3654 the LIBRARY_NAME parameter to builtin_function when declaring BUILTIN_NAME.
3655 If NONANSI_P is nonzero, the name NAME is treated as a non-ANSI name; if
3656 NORETURN_P is nonzero, the function is marked as non-returning.
3657 Returns the declaration of BUILTIN_NAME, if any, otherwise
3658 the declaration of NAME. Does not declare NAME if flag_no_builtin,
3659 or if NONANSI_P and flag_no_nonansi_builtin. */
3661 static tree
3662 builtin_function_2 (builtin_name, name, builtin_type, type, function_code,
3663 class, library_name_p, nonansi_p, noreturn_p)
3664 const char *builtin_name;
3665 const char *name;
3666 tree builtin_type;
3667 tree type;
3668 int function_code;
3669 enum built_in_class class;
3670 int library_name_p;
3671 int nonansi_p;
3672 int noreturn_p;
3674 tree bdecl = NULL_TREE;
3675 tree decl = NULL_TREE;
3676 if (builtin_name != 0)
3678 bdecl = builtin_function (builtin_name, builtin_type, function_code,
3679 class, library_name_p ? name : NULL);
3680 if (noreturn_p)
3682 TREE_THIS_VOLATILE (bdecl) = 1;
3683 TREE_SIDE_EFFECTS (bdecl) = 1;
3686 if (name != 0 && !flag_no_builtin && !(nonansi_p && flag_no_nonansi_builtin))
3688 decl = builtin_function (name, type, function_code, class, NULL);
3689 if (nonansi_p)
3690 DECL_BUILT_IN_NONANSI (decl) = 1;
3691 if (noreturn_p)
3693 TREE_THIS_VOLATILE (decl) = 1;
3694 TREE_SIDE_EFFECTS (decl) = 1;
3697 return (bdecl != 0 ? bdecl : decl);
3700 /* Nonzero if the type T promotes to int. This is (nearly) the
3701 integral promotions defined in ISO C99 6.3.1.1/2. */
3703 bool
3704 c_promoting_integer_type_p (t)
3705 tree t;
3707 switch (TREE_CODE (t))
3709 case INTEGER_TYPE:
3710 return (TYPE_MAIN_VARIANT (t) == char_type_node
3711 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
3712 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
3713 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
3714 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node);
3716 case ENUMERAL_TYPE:
3717 /* ??? Technically all enumerations not larger than an int
3718 promote to an int. But this is used along code paths
3719 that only want to notice a size change. */
3720 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
3722 case BOOLEAN_TYPE:
3723 return 1;
3725 default:
3726 return 0;
3730 /* Given a type, apply default promotions wrt unnamed function arguments
3731 and return the new type. Return NULL_TREE if no change. */
3732 /* ??? There is a function of the same name in the C++ front end that
3733 does something similar, but is more thorough and does not return NULL
3734 if no change. We could perhaps share code, but it would make the
3735 self_promoting_type property harder to identify. */
3737 tree
3738 simple_type_promotes_to (type)
3739 tree type;
3741 if (TYPE_MAIN_VARIANT (type) == float_type_node)
3742 return double_type_node;
3744 if (c_promoting_integer_type_p (type))
3746 /* Traditionally, unsignedness is preserved in default promotions.
3747 Also preserve unsignedness if not really getting any wider. */
3748 if (TREE_UNSIGNED (type)
3749 && (flag_traditional
3750 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
3751 return unsigned_type_node;
3752 return integer_type_node;
3755 return NULL_TREE;
3758 /* Return 1 if PARMS specifies a fixed number of parameters
3759 and none of their types is affected by default promotions. */
3762 self_promoting_args_p (parms)
3763 tree parms;
3765 register tree t;
3766 for (t = parms; t; t = TREE_CHAIN (t))
3768 register tree type = TREE_VALUE (t);
3770 if (TREE_CHAIN (t) == 0 && type != void_type_node)
3771 return 0;
3773 if (type == 0)
3774 return 0;
3776 if (TYPE_MAIN_VARIANT (type) == float_type_node)
3777 return 0;
3779 if (c_promoting_integer_type_p (type))
3780 return 0;
3782 return 1;
3785 /* Recursively examines the array elements of TYPE, until a non-array
3786 element type is found. */
3788 tree
3789 strip_array_types (type)
3790 tree type;
3792 while (TREE_CODE (type) == ARRAY_TYPE)
3793 type = TREE_TYPE (type);
3795 return type;
3798 /* Recognize certain built-in functions so we can make tree-codes
3799 other than CALL_EXPR. We do this when it enables fold-const.c
3800 to do something useful. */
3801 /* ??? By rights this should go in builtins.c, but only C and C++
3802 implement build_{binary,unary}_op. Not exactly sure what bits
3803 of functionality are actually needed from those functions, or
3804 where the similar functionality exists in the other front ends. */
3806 tree
3807 expand_tree_builtin (function, params, coerced_params)
3808 tree function, params, coerced_params;
3810 enum tree_code code;
3812 if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL)
3813 return NULL_TREE;
3815 switch (DECL_FUNCTION_CODE (function))
3817 case BUILT_IN_ABS:
3818 case BUILT_IN_FABS:
3819 if (coerced_params == 0)
3820 return integer_zero_node;
3821 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
3823 case BUILT_IN_CONJ:
3824 if (coerced_params == 0)
3825 return integer_zero_node;
3826 return build_unary_op (CONJ_EXPR, TREE_VALUE (coerced_params), 0);
3828 case BUILT_IN_CREAL:
3829 if (coerced_params == 0)
3830 return integer_zero_node;
3831 return build_unary_op (REALPART_EXPR, TREE_VALUE (coerced_params), 0);
3833 case BUILT_IN_CIMAG:
3834 if (coerced_params == 0)
3835 return integer_zero_node;
3836 return build_unary_op (IMAGPART_EXPR, TREE_VALUE (coerced_params), 0);
3838 case BUILT_IN_ISGREATER:
3839 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3840 code = UNLE_EXPR;
3841 else
3842 code = LE_EXPR;
3843 goto unordered_cmp;
3845 case BUILT_IN_ISGREATEREQUAL:
3846 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3847 code = UNLT_EXPR;
3848 else
3849 code = LT_EXPR;
3850 goto unordered_cmp;
3852 case BUILT_IN_ISLESS:
3853 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3854 code = UNGE_EXPR;
3855 else
3856 code = GE_EXPR;
3857 goto unordered_cmp;
3859 case BUILT_IN_ISLESSEQUAL:
3860 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3861 code = UNGT_EXPR;
3862 else
3863 code = GT_EXPR;
3864 goto unordered_cmp;
3866 case BUILT_IN_ISLESSGREATER:
3867 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3868 code = UNEQ_EXPR;
3869 else
3870 code = EQ_EXPR;
3871 goto unordered_cmp;
3873 case BUILT_IN_ISUNORDERED:
3874 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
3875 return integer_zero_node;
3876 code = UNORDERED_EXPR;
3877 goto unordered_cmp;
3879 unordered_cmp:
3881 tree arg0, arg1;
3883 if (params == 0
3884 || TREE_CHAIN (params) == 0)
3886 error ("too few arguments to function `%s'",
3887 IDENTIFIER_POINTER (DECL_NAME (function)));
3888 return error_mark_node;
3890 else if (TREE_CHAIN (TREE_CHAIN (params)) != 0)
3892 error ("too many arguments to function `%s'",
3893 IDENTIFIER_POINTER (DECL_NAME (function)));
3894 return error_mark_node;
3897 arg0 = TREE_VALUE (params);
3898 arg1 = TREE_VALUE (TREE_CHAIN (params));
3899 arg0 = build_binary_op (code, arg0, arg1, 0);
3900 if (code != UNORDERED_EXPR)
3901 arg0 = build_unary_op (TRUTH_NOT_EXPR, arg0, 0);
3902 return arg0;
3904 break;
3906 default:
3907 break;
3910 return NULL_TREE;
3913 /* Returns non-zero if CODE is the code for a statement. */
3916 statement_code_p (code)
3917 enum tree_code code;
3919 switch (code)
3921 case EXPR_STMT:
3922 case COMPOUND_STMT:
3923 case DECL_STMT:
3924 case IF_STMT:
3925 case FOR_STMT:
3926 case WHILE_STMT:
3927 case DO_STMT:
3928 case RETURN_STMT:
3929 case BREAK_STMT:
3930 case CONTINUE_STMT:
3931 case SCOPE_STMT:
3932 case SWITCH_STMT:
3933 case GOTO_STMT:
3934 case LABEL_STMT:
3935 case ASM_STMT:
3936 case CASE_LABEL:
3937 return 1;
3939 default:
3940 if (lang_statement_code_p)
3941 return (*lang_statement_code_p) (code);
3942 return 0;
3946 /* Walk the statement tree, rooted at *tp. Apply FUNC to all the
3947 sub-trees of *TP in a pre-order traversal. FUNC is called with the
3948 DATA and the address of each sub-tree. If FUNC returns a non-NULL
3949 value, the traversal is aborted, and the value returned by FUNC is
3950 returned. If FUNC sets WALK_SUBTREES to zero, then the subtrees of
3951 the node being visited are not walked.
3953 We don't need a without_duplicates variant of this one because the
3954 statement tree is a tree, not a graph. */
3956 tree
3957 walk_stmt_tree (tp, func, data)
3958 tree *tp;
3959 walk_tree_fn func;
3960 void *data;
3962 enum tree_code code;
3963 int walk_subtrees;
3964 tree result;
3965 int i, len;
3967 #define WALK_SUBTREE(NODE) \
3968 do \
3970 result = walk_stmt_tree (&(NODE), func, data); \
3971 if (result) \
3972 return result; \
3974 while (0)
3976 /* Skip empty subtrees. */
3977 if (!*tp)
3978 return NULL_TREE;
3980 /* Skip subtrees below non-statement nodes. */
3981 if (!statement_code_p (TREE_CODE (*tp)))
3982 return NULL_TREE;
3984 /* Call the function. */
3985 walk_subtrees = 1;
3986 result = (*func) (tp, &walk_subtrees, data);
3988 /* If we found something, return it. */
3989 if (result)
3990 return result;
3992 /* FUNC may have modified the tree, recheck that we're looking at a
3993 statement node. */
3994 code = TREE_CODE (*tp);
3995 if (!statement_code_p (code))
3996 return NULL_TREE;
3998 /* Visit the subtrees unless FUNC decided that there was nothing
3999 interesting below this point in the tree. */
4000 if (walk_subtrees)
4002 /* Walk over all the sub-trees of this operand. Statement nodes
4003 never contain RTL, and we needn't worry about TARGET_EXPRs. */
4004 len = TREE_CODE_LENGTH (code);
4006 /* Go through the subtrees. We need to do this in forward order so
4007 that the scope of a FOR_EXPR is handled properly. */
4008 for (i = 0; i < len; ++i)
4009 WALK_SUBTREE (TREE_OPERAND (*tp, i));
4012 /* Finally visit the chain. This can be tail-recursion optimized if
4013 we write it this way. */
4014 return walk_stmt_tree (&TREE_CHAIN (*tp), func, data);
4016 #undef WALK_SUBTREE
4019 /* Used to compare case labels. K1 and K2 are actually tree nodes
4020 representing case labels, or NULL_TREE for a `default' label.
4021 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
4022 K2, and 0 if K1 and K2 are equal. */
4025 case_compare (k1, k2)
4026 splay_tree_key k1;
4027 splay_tree_key k2;
4029 /* Consider a NULL key (such as arises with a `default' label) to be
4030 smaller than anything else. */
4031 if (!k1)
4032 return k2 ? -1 : 0;
4033 else if (!k2)
4034 return k1 ? 1 : 0;
4036 return tree_int_cst_compare ((tree) k1, (tree) k2);
4039 /* Process a case label for the range LOW_VALUE ... HIGH_VALUE. If
4040 LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is
4041 actually a `default' label. If only HIGH_VALUE is NULL_TREE, then
4042 case label was declared using the usual C/C++ syntax, rather than
4043 the GNU case range extension. CASES is a tree containing all the
4044 case ranges processed so far; COND is the condition for the
4045 switch-statement itself. Returns the CASE_LABEL created, or
4046 ERROR_MARK_NODE if no CASE_LABEL is created. */
4048 tree
4049 c_add_case_label (cases, cond, low_value, high_value)
4050 splay_tree cases;
4051 tree cond;
4052 tree low_value;
4053 tree high_value;
4055 tree type;
4056 tree label;
4057 tree case_label;
4058 splay_tree_node node;
4060 /* Create the LABEL_DECL itself. */
4061 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
4062 DECL_CONTEXT (label) = current_function_decl;
4064 /* If there was an error processing the switch condition, bail now
4065 before we get more confused. */
4066 if (!cond || cond == error_mark_node)
4068 /* Add a label anyhow so that the back-end doesn't think that
4069 the beginning of the switch is unreachable. */
4070 if (!cases->root)
4071 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
4072 return error_mark_node;
4075 if ((low_value && TREE_TYPE (low_value)
4076 && POINTER_TYPE_P (TREE_TYPE (low_value)))
4077 || (high_value && TREE_TYPE (high_value)
4078 && POINTER_TYPE_P (TREE_TYPE (high_value))))
4079 error ("pointers are not permitted as case values");
4081 /* Case ranges are a GNU extension. */
4082 if (high_value && pedantic)
4084 if (c_language == clk_cplusplus)
4085 pedwarn ("ISO C++ forbids range expressions in switch statements");
4086 else
4087 pedwarn ("ISO C forbids range expressions in switch statements");
4090 type = TREE_TYPE (cond);
4091 if (low_value)
4093 low_value = check_case_value (low_value);
4094 low_value = convert_and_check (type, low_value);
4096 if (high_value)
4098 high_value = check_case_value (high_value);
4099 high_value = convert_and_check (type, high_value);
4102 /* If an error has occurred, bail out now. */
4103 if (low_value == error_mark_node || high_value == error_mark_node)
4105 if (!cases->root)
4106 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
4107 return error_mark_node;
4110 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
4111 really a case range, even though it was written that way. Remove
4112 the HIGH_VALUE to simplify later processing. */
4113 if (tree_int_cst_equal (low_value, high_value))
4114 high_value = NULL_TREE;
4115 if (low_value && high_value
4116 && !tree_int_cst_lt (low_value, high_value))
4117 warning ("empty range specified");
4119 /* Look up the LOW_VALUE in the table of case labels we already
4120 have. */
4121 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
4122 /* If there was not an exact match, check for overlapping ranges.
4123 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
4124 that's a `default' label and the only overlap is an exact match. */
4125 if (!node && (low_value || high_value))
4127 splay_tree_node low_bound;
4128 splay_tree_node high_bound;
4130 /* Even though there wasn't an exact match, there might be an
4131 overlap between this case range and another case range.
4132 Since we've (inductively) not allowed any overlapping case
4133 ranges, we simply need to find the greatest low case label
4134 that is smaller that LOW_VALUE, and the smallest low case
4135 label that is greater than LOW_VALUE. If there is an overlap
4136 it will occur in one of these two ranges. */
4137 low_bound = splay_tree_predecessor (cases,
4138 (splay_tree_key) low_value);
4139 high_bound = splay_tree_successor (cases,
4140 (splay_tree_key) low_value);
4142 /* Check to see if the LOW_BOUND overlaps. It is smaller than
4143 the LOW_VALUE, so there is no need to check unless the
4144 LOW_BOUND is in fact itself a case range. */
4145 if (low_bound
4146 && CASE_HIGH ((tree) low_bound->value)
4147 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
4148 low_value) >= 0)
4149 node = low_bound;
4150 /* Check to see if the HIGH_BOUND overlaps. The low end of that
4151 range is bigger than the low end of the current range, so we
4152 are only interested if the current range is a real range, and
4153 not an ordinary case label. */
4154 else if (high_bound
4155 && high_value
4156 && (tree_int_cst_compare ((tree) high_bound->key,
4157 high_value)
4158 <= 0))
4159 node = high_bound;
4161 /* If there was an overlap, issue an error. */
4162 if (node)
4164 tree duplicate = CASE_LABEL_DECL ((tree) node->value);
4166 if (high_value)
4168 error ("duplicate (or overlapping) case value");
4169 error_with_decl (duplicate,
4170 "this is the first entry overlapping that value");
4172 else if (low_value)
4174 error ("duplicate case value") ;
4175 error_with_decl (duplicate, "previously used here");
4177 else
4179 error ("multiple default labels in one switch");
4180 error_with_decl (duplicate, "this is the first default label");
4182 if (!cases->root)
4183 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
4186 /* Add a CASE_LABEL to the statement-tree. */
4187 case_label = add_stmt (build_case_label (low_value, high_value, label));
4188 /* Register this case label in the splay tree. */
4189 splay_tree_insert (cases,
4190 (splay_tree_key) low_value,
4191 (splay_tree_value) case_label);
4193 return case_label;
4196 /* Finish an expression taking the address of LABEL. Returns an
4197 expression for the address. */
4199 tree
4200 finish_label_address_expr (label)
4201 tree label;
4203 tree result;
4205 if (pedantic)
4207 if (c_language == clk_cplusplus)
4208 pedwarn ("ISO C++ forbids taking the address of a label");
4209 else
4210 pedwarn ("ISO C forbids taking the address of a label");
4213 label = lookup_label (label);
4214 if (label == NULL_TREE)
4215 result = null_pointer_node;
4216 else
4218 TREE_USED (label) = 1;
4219 result = build1 (ADDR_EXPR, ptr_type_node, label);
4220 TREE_CONSTANT (result) = 1;
4221 /* The current function in not necessarily uninlinable.
4222 Computed gotos are incompatible with inlining, but the value
4223 here could be used only in a diagnostic, for example. */
4226 return result;
4229 /* Mark P (a stmt_tree) for GC. The use of a `void *' for the
4230 parameter allows this function to be used as a GC-marking
4231 function. */
4233 void
4234 mark_stmt_tree (p)
4235 void *p;
4237 stmt_tree st = (stmt_tree) p;
4239 ggc_mark_tree (st->x_last_stmt);
4240 ggc_mark_tree (st->x_last_expr_type);
4243 /* Mark LD for GC. */
4245 void
4246 c_mark_lang_decl (c)
4247 struct c_lang_decl *c;
4249 ggc_mark_tree (c->saved_tree);
4252 /* Mark F for GC. */
4254 void
4255 mark_c_language_function (f)
4256 struct language_function *f;
4258 if (!f)
4259 return;
4261 mark_stmt_tree (&f->x_stmt_tree);
4262 ggc_mark_tree (f->x_scope_stmt_stack);
4265 /* Hook used by expand_expr to expand language-specific tree codes. */
4268 c_expand_expr (exp, target, tmode, modifier)
4269 tree exp;
4270 rtx target;
4271 enum machine_mode tmode;
4272 enum expand_modifier modifier;
4274 switch (TREE_CODE (exp))
4276 case STMT_EXPR:
4278 tree rtl_expr;
4279 rtx result;
4281 /* Since expand_expr_stmt calls free_temp_slots after every
4282 expression statement, we must call push_temp_slots here.
4283 Otherwise, any temporaries in use now would be considered
4284 out-of-scope after the first EXPR_STMT from within the
4285 STMT_EXPR. */
4286 push_temp_slots ();
4287 rtl_expr = expand_start_stmt_expr ();
4288 expand_stmt (STMT_EXPR_STMT (exp));
4289 expand_end_stmt_expr (rtl_expr);
4290 result = expand_expr (rtl_expr, target, tmode, modifier);
4291 pop_temp_slots ();
4292 return result;
4294 break;
4296 case CALL_EXPR:
4298 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
4299 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4300 == FUNCTION_DECL)
4301 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4302 && (DECL_BUILT_IN_CLASS (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4303 == BUILT_IN_FRONTEND))
4304 return c_expand_builtin (exp, target, tmode, modifier);
4305 else
4306 abort();
4308 break;
4310 default:
4311 abort ();
4314 abort ();
4315 return NULL;
4318 /* Hook used by safe_from_p to handle language-specific tree codes. */
4321 c_safe_from_p (target, exp)
4322 rtx target;
4323 tree exp;
4325 /* We can see statements here when processing the body of a
4326 statement-expression. For a declaration statement declaring a
4327 variable, look at the variable's initializer. */
4328 if (TREE_CODE (exp) == DECL_STMT)
4330 tree decl = DECL_STMT_DECL (exp);
4332 if (TREE_CODE (decl) == VAR_DECL
4333 && DECL_INITIAL (decl)
4334 && !safe_from_p (target, DECL_INITIAL (decl), /*top_p=*/0))
4335 return 0;
4338 /* For any statement, we must follow the statement-chain. */
4339 if (statement_code_p (TREE_CODE (exp)) && TREE_CHAIN (exp))
4340 return safe_from_p (target, TREE_CHAIN (exp), /*top_p=*/0);
4342 /* Assume everything else is safe. */
4343 return 1;
4346 /* Hook used by unsafe_for_reeval to handle language-specific tree codes. */
4349 c_unsafe_for_reeval (exp)
4350 tree exp;
4352 /* Statement expressions may not be reevaluated. */
4353 if (TREE_CODE (exp) == STMT_EXPR)
4354 return 2;
4356 /* Walk all other expressions. */
4357 return -1;
4360 /* Tree code classes. */
4362 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
4364 static char c_tree_code_type[] = {
4365 'x',
4366 #include "c-common.def"
4368 #undef DEFTREECODE
4370 /* Table indexed by tree code giving number of expression
4371 operands beyond the fixed part of the node structure.
4372 Not used for types or decls. */
4374 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
4376 static int c_tree_code_length[] = {
4378 #include "c-common.def"
4380 #undef DEFTREECODE
4382 /* Names of tree components.
4383 Used for printing out the tree and error messages. */
4384 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
4386 static const char *c_tree_code_name[] = {
4387 "@@dummy",
4388 #include "c-common.def"
4390 #undef DEFTREECODE
4392 /* Adds the tree codes specific to the C front end to the list of all
4393 tree codes. */
4395 void
4396 add_c_tree_codes ()
4398 memcpy (tree_code_type + (int) LAST_AND_UNUSED_TREE_CODE,
4399 c_tree_code_type,
4400 (int)LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE);
4401 memcpy (tree_code_length + (int) LAST_AND_UNUSED_TREE_CODE,
4402 c_tree_code_length,
4403 (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (int));
4404 memcpy (tree_code_name + (int) LAST_AND_UNUSED_TREE_CODE,
4405 c_tree_code_name,
4406 (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (char *));
4407 lang_unsafe_for_reeval = c_unsafe_for_reeval;
4410 #define CALLED_AS_BUILT_IN(NODE) \
4411 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
4413 static rtx
4414 c_expand_builtin (exp, target, tmode, modifier)
4415 tree exp;
4416 rtx target;
4417 enum machine_mode tmode;
4418 enum expand_modifier modifier;
4420 tree type = TREE_TYPE (exp);
4421 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4422 tree arglist = TREE_OPERAND (exp, 1);
4423 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
4424 enum tree_code code = TREE_CODE (exp);
4425 const int ignore = (target == const0_rtx
4426 || ((code == NON_LVALUE_EXPR || code == NOP_EXPR
4427 || code == CONVERT_EXPR || code == REFERENCE_EXPR
4428 || code == COND_EXPR)
4429 && TREE_CODE (type) == VOID_TYPE));
4431 if (! optimize && ! CALLED_AS_BUILT_IN (fndecl))
4432 return expand_call (exp, target, ignore);
4434 switch (fcode)
4436 case BUILT_IN_PRINTF:
4437 target = c_expand_builtin_printf (arglist, target, tmode,
4438 modifier, ignore);
4439 if (target)
4440 return target;
4441 break;
4443 case BUILT_IN_FPRINTF:
4444 target = c_expand_builtin_fprintf (arglist, target, tmode,
4445 modifier, ignore);
4446 if (target)
4447 return target;
4448 break;
4450 default: /* just do library call, if unknown builtin */
4451 error ("built-in function `%s' not currently supported",
4452 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
4455 /* The switch statement above can drop through to cause the function
4456 to be called normally. */
4457 return expand_call (exp, target, ignore);
4460 /* Check an arglist to *printf for problems. The arglist should start
4461 at the format specifier, with the remaining arguments immediately
4462 following it. */
4463 static int
4464 is_valid_printf_arglist (arglist)
4465 tree arglist;
4467 /* Save this value so we can restore it later. */
4468 const int SAVE_pedantic = pedantic;
4469 int diagnostic_occurred = 0;
4471 /* Set this to a known value so the user setting won't affect code
4472 generation. */
4473 pedantic = 1;
4474 /* Check to make sure there are no format specifier errors. */
4475 check_function_format (&diagnostic_occurred,
4476 maybe_get_identifier("printf"),
4477 NULL_TREE, arglist);
4479 /* Restore the value of `pedantic'. */
4480 pedantic = SAVE_pedantic;
4482 /* If calling `check_function_format_ptr' produces a warning, we
4483 return false, otherwise we return true. */
4484 return ! diagnostic_occurred;
4487 /* If the arguments passed to printf are suitable for optimizations,
4488 we attempt to transform the call. */
4489 static rtx
4490 c_expand_builtin_printf (arglist, target, tmode, modifier, ignore)
4491 tree arglist;
4492 rtx target;
4493 enum machine_mode tmode;
4494 enum expand_modifier modifier;
4495 int ignore;
4497 tree fn_putchar = built_in_decls[BUILT_IN_PUTCHAR],
4498 fn_puts = built_in_decls[BUILT_IN_PUTS];
4499 tree fn, format_arg, stripped_string;
4501 /* If the return value is used, or the replacement _DECL isn't
4502 initialized, don't do the transformation. */
4503 if (!ignore || !fn_putchar || !fn_puts)
4504 return 0;
4506 /* Verify the required arguments in the original call. */
4507 if (arglist == 0
4508 || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE))
4509 return 0;
4511 /* Check the specifier vs. the parameters. */
4512 if (!is_valid_printf_arglist (arglist))
4513 return 0;
4515 format_arg = TREE_VALUE (arglist);
4516 stripped_string = format_arg;
4517 STRIP_NOPS (stripped_string);
4518 if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
4519 stripped_string = TREE_OPERAND (stripped_string, 0);
4521 /* If the format specifier isn't a STRING_CST, punt. */
4522 if (TREE_CODE (stripped_string) != STRING_CST)
4523 return 0;
4525 /* OK! We can attempt optimization. */
4527 /* If the format specifier was "%s\n", call __builtin_puts(arg2). */
4528 if (strcmp (TREE_STRING_POINTER (stripped_string), "%s\n") == 0)
4530 arglist = TREE_CHAIN (arglist);
4531 fn = fn_puts;
4533 /* If the format specifier was "%c", call __builtin_putchar (arg2). */
4534 else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0)
4536 arglist = TREE_CHAIN (arglist);
4537 fn = fn_putchar;
4539 else
4541 /* We can't handle anything else with % args or %% ... yet. */
4542 if (strchr (TREE_STRING_POINTER (stripped_string), '%'))
4543 return 0;
4545 /* If the resulting constant string has a length of 1, call
4546 putchar. Note, TREE_STRING_LENGTH includes the terminating
4547 NULL in its count. */
4548 if (TREE_STRING_LENGTH (stripped_string) == 2)
4550 /* Given printf("c"), (where c is any one character,)
4551 convert "c"[0] to an int and pass that to the replacement
4552 function. */
4553 arglist = build_int_2 (TREE_STRING_POINTER (stripped_string)[0], 0);
4554 arglist = build_tree_list (NULL_TREE, arglist);
4556 fn = fn_putchar;
4558 /* If the resulting constant was "string\n", call
4559 __builtin_puts("string"). Ensure "string" has at least one
4560 character besides the trailing \n. Note, TREE_STRING_LENGTH
4561 includes the terminating NULL in its count. */
4562 else if (TREE_STRING_LENGTH (stripped_string) > 2
4563 && TREE_STRING_POINTER (stripped_string)
4564 [TREE_STRING_LENGTH (stripped_string) - 2] == '\n')
4566 /* Create a NULL-terminated string that's one char shorter
4567 than the original, stripping off the trailing '\n'. */
4568 const int newlen = TREE_STRING_LENGTH (stripped_string) - 1;
4569 char *newstr = (char *) alloca (newlen);
4570 memcpy (newstr, TREE_STRING_POINTER (stripped_string), newlen - 1);
4571 newstr[newlen - 1] = 0;
4573 arglist = combine_strings (build_string (newlen, newstr));
4574 arglist = build_tree_list (NULL_TREE, arglist);
4575 fn = fn_puts;
4577 else
4578 /* We'd like to arrange to call fputs(string) here, but we
4579 need stdout and don't have a way to get it ... yet. */
4580 return 0;
4583 return expand_expr (build_function_call (fn, arglist),
4584 (ignore ? const0_rtx : target),
4585 tmode, modifier);
4588 /* If the arguments passed to fprintf are suitable for optimizations,
4589 we attempt to transform the call. */
4590 static rtx
4591 c_expand_builtin_fprintf (arglist, target, tmode, modifier, ignore)
4592 tree arglist;
4593 rtx target;
4594 enum machine_mode tmode;
4595 enum expand_modifier modifier;
4596 int ignore;
4598 tree fn_fputc = built_in_decls[BUILT_IN_FPUTC],
4599 fn_fputs = built_in_decls[BUILT_IN_FPUTS];
4600 tree fn, format_arg, stripped_string;
4602 /* If the return value is used, or the replacement _DECL isn't
4603 initialized, don't do the transformation. */
4604 if (!ignore || !fn_fputc || !fn_fputs)
4605 return 0;
4607 /* Verify the required arguments in the original call. */
4608 if (arglist == 0
4609 || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE)
4610 || (TREE_CHAIN (arglist) == 0)
4611 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist)))) !=
4612 POINTER_TYPE))
4613 return 0;
4615 /* Check the specifier vs. the parameters. */
4616 if (!is_valid_printf_arglist (TREE_CHAIN (arglist)))
4617 return 0;
4619 format_arg = TREE_VALUE (TREE_CHAIN (arglist));
4620 stripped_string = format_arg;
4621 STRIP_NOPS (stripped_string);
4622 if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
4623 stripped_string = TREE_OPERAND (stripped_string, 0);
4625 /* If the format specifier isn't a STRING_CST, punt. */
4626 if (TREE_CODE (stripped_string) != STRING_CST)
4627 return 0;
4629 /* OK! We can attempt optimization. */
4631 /* If the format specifier was "%s", call __builtin_fputs(arg3, arg1). */
4632 if (strcmp (TREE_STRING_POINTER (stripped_string), "%s") == 0)
4634 tree newarglist = build_tree_list (NULL_TREE, TREE_VALUE (arglist));
4635 arglist = tree_cons (NULL_TREE,
4636 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))),
4637 newarglist);
4638 fn = fn_fputs;
4640 /* If the format specifier was "%c", call __builtin_fputc (arg3, arg1). */
4641 else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0)
4643 tree newarglist = build_tree_list (NULL_TREE, TREE_VALUE (arglist));
4644 arglist = tree_cons (NULL_TREE,
4645 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))),
4646 newarglist);
4647 fn = fn_fputc;
4649 else
4651 /* We can't handle anything else with % args or %% ... yet. */
4652 if (strchr (TREE_STRING_POINTER (stripped_string), '%'))
4653 return 0;
4655 /* When "string" doesn't contain %, replace all cases of
4656 fprintf(stream,string) with fputs(string,stream). The fputs
4657 builtin will take take of special cases like length==1. */
4658 arglist = tree_cons (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)),
4659 build_tree_list (NULL_TREE, TREE_VALUE (arglist)));
4660 fn = fn_fputs;
4663 return expand_expr (build_function_call (fn, arglist),
4664 (ignore ? const0_rtx : target),
4665 tmode, modifier);
4669 /* Given a boolean expression ARG, return a tree representing an increment
4670 or decrement (as indicated by CODE) of ARG. The front end must check for
4671 invalid cases (e.g., decrement in C++). */
4672 tree
4673 boolean_increment (code, arg)
4674 enum tree_code code;
4675 tree arg;
4677 tree val;
4678 tree true_res = (c_language == clk_cplusplus
4679 ? boolean_true_node
4680 : c_bool_true_node);
4681 arg = stabilize_reference (arg);
4682 switch (code)
4684 case PREINCREMENT_EXPR:
4685 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4686 break;
4687 case POSTINCREMENT_EXPR:
4688 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4689 arg = save_expr (arg);
4690 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4691 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4692 break;
4693 case PREDECREMENT_EXPR:
4694 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
4695 break;
4696 case POSTDECREMENT_EXPR:
4697 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
4698 arg = save_expr (arg);
4699 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4700 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4701 break;
4702 default:
4703 abort ();
4705 TREE_SIDE_EFFECTS (val) = 1;
4706 return val;
4710 /* Do the parts of lang_init common to C and C++. */
4711 void
4712 c_common_lang_init ()
4714 /* If still "unspecified", make it match -fbounded-pointers. */
4715 if (flag_bounds_check < 0)
4716 flag_bounds_check = flag_bounded_pointers;
4718 /* Special format checking options don't work without -Wformat; warn if
4719 they are used. */
4720 if (warn_format_y2k && !warn_format)
4721 warning ("-Wformat-y2k ignored without -Wformat");
4722 if (warn_format_extra_args && !warn_format)
4723 warning ("-Wformat-extra-args ignored without -Wformat");
4724 if (warn_format_nonliteral && !warn_format)
4725 warning ("-Wformat-nonliteral ignored without -Wformat");
4726 if (warn_format_security && !warn_format)
4727 warning ("-Wformat-security ignored without -Wformat");
4728 if (warn_missing_format_attribute && !warn_format)
4729 warning ("-Wmissing-format-attribute ignored without -Wformat");