]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-common.c
Revert last change.
[thirdparty/gcc.git] / gcc / c-common.c
CommitLineData
b30f223b 1/* Subroutines shared by all languages that are variants of C.
09f89307 2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
06ceef4e 3 Free Software Foundation, Inc.
b30f223b
RS
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
940d9d63
RK
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
b30f223b
RS
21
22#include "config.h"
670ee920 23#include "system.h"
b30f223b 24#include "tree.h"
b30f223b 25#include "flags.h"
5f6da302 26#include "toplev.h"
d6f4ec51 27#include "output.h"
e2af664c 28#include "c-pragma.h"
3932261a 29#include "rtl.h"
1526a060 30#include "ggc.h"
c6991660 31#include "expr.h"
8f17b5c5 32#include "c-common.h"
7bdb32b9 33#include "tm_p.h"
235cfbc4 34#include "obstack.h"
c8724862 35#include "cpplib.h"
9ba2e1ef 36cpp_reader *parse_in; /* Declared in c-lex.h. */
c8724862 37
12a39b12
JM
38#undef WCHAR_TYPE_SIZE
39#define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node)
fef610be 40
eaa7c03f
JM
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. */
46
47#ifndef SIZE_TYPE
48#define SIZE_TYPE "long unsigned int"
49#endif
50
51#ifndef WCHAR_TYPE
52#define WCHAR_TYPE "int"
53#endif
54
0884b60c
BS
55#ifndef PTRDIFF_TYPE
56#define PTRDIFF_TYPE "long int"
57#endif
58
5fd8e536
JM
59#ifndef WINT_TYPE
60#define WINT_TYPE "unsigned int"
61#endif
62
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
70
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
78
7f4edbcb 79/* The following symbols are subsumed in the c_global_trees array, and
d125d268 80 listed here individually for documentation purposes.
7f4edbcb
BS
81
82 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
83
84 tree short_integer_type_node;
85 tree long_integer_type_node;
86 tree long_long_integer_type_node;
87
88 tree short_unsigned_type_node;
89 tree long_unsigned_type_node;
90 tree long_long_unsigned_type_node;
91
92 tree boolean_type_node;
93 tree boolean_false_node;
94 tree boolean_true_node;
95
96 tree ptrdiff_type_node;
97
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;
103
104 tree float_type_node;
105 tree double_type_node;
106 tree long_double_type_node;
107
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;
112
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;
118
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;
124
125 tree widest_integer_literal_type_node;
126 tree widest_unsigned_literal_type_node;
127
128 Nodes for types `void *' and `const void *'.
129
130 tree ptr_type_node, const_ptr_type_node;
131
132 Nodes for types `char *' and `const char *'.
133
134 tree string_type_node, const_string_type_node;
135
136 Type `char[SOMENUMBER]'.
137 Used when an array of char is needed and the size is irrelevant.
138
139 tree char_array_type_node;
140
141 Type `int[SOMENUMBER]' or something like it.
142 Used when an array of int needed and the size is irrelevant.
143
144 tree int_array_type_node;
145
146 Type `wchar_t[SOMENUMBER]' or something like it.
147 Used when a wide string literal is created.
148
149 tree wchar_array_type_node;
150
151 Type `int ()' -- used for implicit declaration of functions.
152
153 tree default_function_type;
154
155 Function types `int (int)', etc.
156
157 tree int_ftype_int;
158 tree void_ftype;
159 tree void_ftype_ptr;
160 tree int_ftype_int;
161 tree ptr_ftype_sizetype;
162
163 A VOID_TYPE node, packaged in a TREE_LIST.
164
165 tree void_list_node;
166
0ba8a114
NS
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.)
63ad61ed 170
0ba8a114
NS
171 tree function_name_decl_node;
172 tree pretty_function_name_declnode;
173 tree c99_function_name_decl_node;
174
175 Stack of nested function name VAR_DECLs.
176
177 tree saved_function_name_decls;
63ad61ed 178
7f4edbcb
BS
179*/
180
181tree c_global_trees[CTI_MAX];
0b73773c 182
6bcedb4e
MM
183/* Nonzero means don't recognize the non-ANSI builtin functions. */
184
185int flag_no_builtin;
186
187/* Nonzero means don't recognize the non-ANSI builtin functions.
188 -ansi sets this. */
189
190int flag_no_nonansi_builtin;
191
eaa7c03f
JM
192/* Nonzero means give `double' the same size as `float'. */
193
194int flag_short_double;
195
196/* Nonzero means give `wchar_t' the same size as `short'. */
197
198int flag_short_wchar;
199
0a7394bc
MM
200/* If non-NULL, dump the tree structure for the entire translation
201 unit to this file. */
202
203const char *flag_dump_translation_unit;
204
2683ed8d
BS
205/* Nonzero means warn about possible violations of sequence point rules. */
206
207int warn_sequence_point;
208
f09f1de5
MM
209/* The elements of `ridpointers' are identifier nodes for the reserved
210 type names and storage classes. It is indexed by a RID_... value. */
211tree *ridpointers;
212
0ba8a114 213tree (*make_fname_decl) PARAMS ((tree, int));
2ce07e2d 214
ae499cce
MM
215/* If non-NULL, the address of a language-specific function that
216 returns 1 for language-specific statement codes. */
217int (*lang_statement_code_p) PARAMS ((enum tree_code));
218
8f17b5c5
MM
219/* If non-NULL, the address of a language-specific function that takes
220 any action required right before expand_function_end is called. */
221void (*lang_expand_function_end) PARAMS ((void));
222
67673f5c
MM
223/* If this variable is defined to a non-NULL value, it will be called
224 after the file has been completely parsed. */
225void (*back_end_hook) PARAMS ((tree));
226
e78a3b42
RK
227/* Nonzero means the expression being parsed will never be evaluated.
228 This is a count, since unevaluated expressions can nest. */
229int skip_evaluation;
230
2786cbad 231enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
7d384cc0 232 A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
bbb1ae01 233 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
a157febd 234 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS, A_MALLOC,
2a8f6b90 235 A_NO_LIMIT_STACK, A_PURE};
53065596 236
0ba8a114
NS
237/* Information about how a function name is generated. */
238struct fname_var_t
239{
240 tree *decl; /* pointer to the VAR_DECL. */
241 unsigned rid; /* RID number for the identifier. */
242 int pretty; /* How pretty is it? */
243};
244
245/* The three ways of getting then name of the current function. */
246
247const struct fname_var_t fname_vars[] =
248{
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},
256};
257
6e090c76
KG
258static void add_attribute PARAMS ((enum attrs, const char *,
259 int, int, int));
260static void init_attributes PARAMS ((void));
6e090c76 261static int default_valid_lang_attribute PARAMS ((tree, tree, tree, tree));
d02b54f6 262static int constant_fits_type_p PARAMS ((tree, tree));
4724b3de 263
0a7ed33c
BS
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. */
269typedef struct
270{
271 int compstmt_count;
272 int line;
dff01034 273 const char *file;
0a7ed33c 274 int needs_warning;
8f17b5c5 275 tree if_stmt;
0a7ed33c
BS
276} if_elt;
277
278static if_elt *if_stack;
6d819282
MK
279
280/* Amount of space in the if statement stack. */
281static int if_stack_space = 0;
282
283/* Stack pointer. */
284static int if_stack_pointer = 0;
285
8f17b5c5 286/* Record the start of an if-then, and record the start of it
0a7ed33c
BS
287 for ambiguous else detection. */
288
6d819282 289void
8f17b5c5 290c_expand_start_cond (cond, compstmt_count)
6d819282 291 tree cond;
6d819282
MK
292 int compstmt_count;
293{
8f17b5c5
MM
294 tree if_stmt;
295
6d819282
MK
296 /* Make sure there is enough space on the stack. */
297 if (if_stack_space == 0)
298 {
299 if_stack_space = 10;
0a7ed33c 300 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
6d819282
MK
301 }
302 else if (if_stack_space == if_stack_pointer)
303 {
304 if_stack_space += 10;
0a7ed33c 305 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
6d819282 306 }
0a7ed33c 307
8f17b5c5
MM
308 if_stmt = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
309 IF_COND (if_stmt) = cond;
310 add_stmt (if_stmt);
311
6d819282 312 /* Record this if statement. */
0a7ed33c
BS
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;
8f17b5c5 317 if_stack[if_stack_pointer].if_stmt = if_stmt;
0a7ed33c 318 if_stack_pointer++;
8f17b5c5 319}
6d819282 320
8f17b5c5
MM
321/* Called after the then-clause for an if-statement is processed. */
322
323void
324c_finish_then ()
325{
326 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
327 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
6d819282
MK
328}
329
8f17b5c5 330/* Record the end of an if-then. Optionally warn if a nested
0a7ed33c
BS
331 if statement had an ambiguous else clause. */
332
6d819282
MK
333void
334c_expand_end_cond ()
335{
336 if_stack_pointer--;
0a7ed33c
BS
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'");
8f17b5c5 341 last_expr_type = NULL_TREE;
6d819282
MK
342}
343
8f17b5c5 344/* Called between the then-clause and the else-clause
0a7ed33c
BS
345 of an if-then-else. */
346
6d819282
MK
347void
348c_expand_start_else ()
349{
0a7ed33c
BS
350 /* An ambiguous else warning must be generated for the enclosing if
351 statement, unless we see an else branch for that one, too. */
6d819282
MK
352 if (warn_parentheses
353 && if_stack_pointer > 1
0a7ed33c
BS
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;
357
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--;
8f17b5c5
MM
363}
364
365/* Called after the else-clause for an if-statement is processed. */
6d819282 366
8f17b5c5
MM
367void
368c_finish_else ()
369{
370 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
371 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
6d819282
MK
372}
373
0ba8a114 374/* Push current bindings for the function name VAR_DECLS. */
7da551a2
RS
375
376void
0ba8a114 377start_fname_decls ()
7da551a2 378{
0ba8a114
NS
379 unsigned ix;
380 tree saved = NULL_TREE;
381
382 for (ix = 0; fname_vars[ix].decl; ix++)
383 {
384 tree decl = *fname_vars[ix].decl;
7da551a2 385
0ba8a114
NS
386 if (decl)
387 {
388 saved = tree_cons (decl, build_int_2 (ix, 0), saved);
389 *fname_vars[ix].decl = NULL_TREE;
390 }
391 }
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);
397}
398
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. */
405
406void
407finish_fname_decls ()
408{
409 unsigned ix;
410 tree body = NULL_TREE;
411 tree stack = saved_function_name_decls;
412
413 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
414 body = chainon (TREE_VALUE (stack), body);
415
416 if (body)
417 {
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);
422
423 COMPOUND_STMT_NO_SCOPE (body) = 1;
424 TREE_CHAIN (DECL_SAVED_TREE (current_function_decl)) = body;
425 }
426
427 for (ix = 0; fname_vars[ix].decl; ix++)
428 *fname_vars[ix].decl = NULL_TREE;
429
430 if (stack)
7da551a2 431 {
0ba8a114
NS
432 /* We had saved values, restore them. */
433 tree saved;
434
435 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
436 {
437 tree decl = TREE_PURPOSE (saved);
438 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
439
440 *fname_vars[ix].decl = decl;
441 }
442 stack = TREE_CHAIN (stack);
7da551a2 443 }
0ba8a114
NS
444 saved_function_name_decls = stack;
445}
446
447/* Return the text name of the current function, suitable prettified
448 by PRETTY_P. */
449
450const char *
451fname_as_string (pretty_p)
452 int pretty_p;
453{
454 const char *name = NULL;
455
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));
7da551a2 462 else
0ba8a114
NS
463 name = "";
464 return name;
465}
466
467/* Return the text name of the current function, formatted as
468 required by the supplied RID value. */
469
470const char *
471fname_string (rid)
472 unsigned rid;
473{
474 unsigned ix;
475
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);
480}
481
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. */
488
489tree
490fname_decl (rid, id)
491 unsigned rid;
492 tree id;
493{
494 unsigned ix;
495 tree decl = NULL_TREE;
496
497 for (ix = 0; fname_vars[ix].decl; ix++)
498 if (fname_vars[ix].rid == rid)
499 break;
500
501 decl = *fname_vars[ix].decl;
502 if (!decl)
7da551a2 503 {
0ba8a114
NS
504 tree saved_last_tree = last_tree;
505
506 decl = (*make_fname_decl) (id, fname_vars[ix].pretty);
507 if (last_tree != saved_last_tree)
508 {
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);
513
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);
518 }
519 *fname_vars[ix].decl = decl;
7da551a2 520 }
0ba8a114
NS
521 if (!ix && !current_function_decl)
522 pedwarn_with_decl (decl, "`%s' is not defined outside of function scope");
2ce07e2d 523
0ba8a114 524 return decl;
7da551a2
RS
525}
526
b30f223b
RS
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. */
530
531tree
532combine_strings (strings)
533 tree strings;
534{
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;
1326a48b 541 const int nchars_max = flag_isoc99 ? 4095 : 509;
b30f223b
RS
542
543 if (TREE_CHAIN (strings))
544 {
545 /* More than one in the chain, so concatenate. */
546 register char *p, *q;
547
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))
552 {
553 if (TREE_TYPE (t) == wchar_array_type_node)
554 {
555 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
556 wide_flag = 1;
557 }
558 else
559 length += (TREE_STRING_LENGTH (t) - 1);
560 }
561
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;
566
520a57c8 567 p = alloca (length);
b30f223b
RS
568
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. */
572
573 q = p;
574 for (t = strings; t; t = TREE_CHAIN (t))
575 {
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)
580 {
7e2231e7 581 memcpy (q, TREE_STRING_POINTER (t), len);
b30f223b
RS
582 q += len;
583 }
584 else
585 {
586 int i;
587 for (i = 0; i < len; i++)
41bbd14e
JW
588 {
589 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
590 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
591 else
592 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
593 }
b30f223b
RS
594 q += len * wchar_bytes;
595 }
596 }
597 if (wide_flag)
598 {
599 int i;
600 for (i = 0; i < wchar_bytes; i++)
601 *q++ = 0;
602 }
603 else
604 *q = 0;
605
520a57c8 606 value = build_string (length, p);
b30f223b
RS
607 }
608 else
609 {
610 value = strings;
611 length = TREE_STRING_LENGTH (value);
612 if (TREE_TYPE (value) == wchar_array_type_node)
613 wide_flag = 1;
614 }
615
b57062ca 616 /* Compute the number of elements, for the array type. */
b30f223b
RS
617 nchars = wide_flag ? length / wchar_bytes : length;
618
690c96c8 619 if (pedantic && nchars - 1 > nchars_max && c_language == clk_c)
3220116f 620 pedwarn ("string length `%d' is greater than the length `%d' ISO C%d compilers are required to support",
690c96c8 621 nchars - 1, nchars_max, flag_isoc99 ? 99 : 89);
1326a48b 622
b30f223b
RS
623 /* Create the array type for the string constant.
624 -Wwrite-strings says make the string constant an array of const char
d9cf7c82
JM
625 so that copying it to a non-const pointer will get a warning.
626 For C++, this is the standard behavior. */
627 if (flag_const_strings
b30f223b
RS
628 && (! flag_traditional && ! flag_writable_strings))
629 {
630 tree elements
631 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
632 1, 0);
633 TREE_TYPE (value)
634 = build_array_type (elements,
635 build_index_type (build_int_2 (nchars - 1, 0)));
636 }
637 else
638 TREE_TYPE (value)
639 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
640 build_index_type (build_int_2 (nchars - 1, 0)));
d9cf7c82 641
ccd4c832
JM
642 TREE_CONSTANT (value) = 1;
643 TREE_READONLY (value) = ! flag_writable_strings;
b30f223b
RS
644 TREE_STATIC (value) = 1;
645 return value;
646}
647\f
53065596
RK
648/* To speed up processing of attributes, we maintain an array of
649 IDENTIFIER_NODES and the corresponding attribute types. */
650
651/* Array to hold attribute information. */
652
653static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
654
655static int attrtab_idx = 0;
656
657/* Add an entry to the attribute table above. */
658
659static void
660add_attribute (id, string, min_len, max_len, decl_req)
661 enum attrs id;
dff01034 662 const char *string;
53065596
RK
663 int min_len, max_len;
664 int decl_req;
665{
666 char buf[100];
667
668 attrtab[attrtab_idx].id = id;
669 attrtab[attrtab_idx].name = get_identifier (string);
670 attrtab[attrtab_idx].min = min_len;
671 attrtab[attrtab_idx].max = max_len;
672 attrtab[attrtab_idx++].decl_req = decl_req;
673
674 sprintf (buf, "__%s__", string);
675
676 attrtab[attrtab_idx].id = id;
677 attrtab[attrtab_idx].name = get_identifier (buf);
678 attrtab[attrtab_idx].min = min_len;
679 attrtab[attrtab_idx].max = max_len;
680 attrtab[attrtab_idx++].decl_req = decl_req;
681}
682
683/* Initialize attribute table. */
684
685static void
686init_attributes ()
687{
a89ca5c6 688 add_attribute (A_PACKED, "packed", 0, 0, 0);
bbb1ae01 689 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
2786cbad 690 add_attribute (A_COMMON, "common", 0, 0, 1);
53065596
RK
691 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
692 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
6d819282 693 add_attribute (A_UNUSED, "unused", 0, 0, 0);
53065596
RK
694 add_attribute (A_CONST, "const", 0, 0, 1);
695 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
696 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
697 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
698 add_attribute (A_MODE, "mode", 1, 1, 1);
699 add_attribute (A_SECTION, "section", 1, 1, 1);
700 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
d161fae4 701 add_attribute (A_FORMAT, "format", 3, 3, 1);
0161e8da 702 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
4b8af8d9
JM
703 add_attribute (A_WEAK, "weak", 0, 0, 1);
704 add_attribute (A_ALIAS, "alias", 1, 1, 1);
07417085 705 add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
7d384cc0 706 add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
140592a0 707 add_attribute (A_MALLOC, "malloc", 0, 0, 1);
a157febd 708 add_attribute (A_NO_LIMIT_STACK, "no_stack_limit", 0, 0, 1);
2a8f6b90 709 add_attribute (A_PURE, "pure", 0, 0, 1);
53065596
RK
710}
711\f
adfaf194
JM
712/* Default implementation of valid_lang_attribute, below. By default, there
713 are no language-specific attributes. */
714
715static int
716default_valid_lang_attribute (attr_name, attr_args, decl, type)
717 tree attr_name ATTRIBUTE_UNUSED;
718 tree attr_args ATTRIBUTE_UNUSED;
719 tree decl ATTRIBUTE_UNUSED;
720 tree type ATTRIBUTE_UNUSED;
721{
722 return 0;
723}
724
725/* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific
726 attribute for either declaration DECL or type TYPE and 0 otherwise. */
727
6e090c76 728int (*valid_lang_attribute) PARAMS ((tree, tree, tree, tree))
adfaf194
JM
729 = default_valid_lang_attribute;
730
1228e2a6 731/* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
53065596
RK
732 and install them in NODE, which is either a DECL (including a TYPE_DECL)
733 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
0f41302f 734 and declaration modifiers but before the declaration proper. */
b30f223b
RS
735
736void
53065596
RK
737decl_attributes (node, attributes, prefix_attributes)
738 tree node, attributes, prefix_attributes;
b30f223b 739{
a16b4c9c
JW
740 tree decl = 0, type = 0;
741 int is_type = 0;
53065596
RK
742 tree a;
743
744 if (attrtab_idx == 0)
745 init_attributes ();
746
2f939d94 747 if (DECL_P (node))
53065596
RK
748 {
749 decl = node;
750 type = TREE_TYPE (decl);
751 is_type = TREE_CODE (node) == TYPE_DECL;
752 }
2f939d94 753 else if (TYPE_P (node))
53065596 754 type = node, is_type = 1;
a2cd7b45 755
e2af664c
NC
756#ifdef PRAGMA_INSERT_ATTRIBUTES
757 /* If the code in c-pragma.c wants to insert some attributes then
758 allow it to do so. Do this before allowing machine back ends to
759 insert attributes, so that they have the opportunity to override
760 anything done here. */
761 PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
762#endif
d125d268 763
f09db6e0
NC
764#ifdef INSERT_ATTRIBUTES
765 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
766#endif
d125d268 767
eed32833 768 attributes = chainon (prefix_attributes, attributes);
5289b665 769
b30f223b 770 for (a = attributes; a; a = TREE_CHAIN (a))
53065596
RK
771 {
772 tree name = TREE_PURPOSE (a);
773 tree args = TREE_VALUE (a);
774 int i;
775 enum attrs id;
b57062ca 776
53065596
RK
777 for (i = 0; i < attrtab_idx; i++)
778 if (attrtab[i].name == name)
779 break;
780
6eaba4a7 781 if (i == attrtab_idx)
53065596 782 {
adfaf194
JM
783 if (! valid_machine_attribute (name, args, decl, type)
784 && ! (* valid_lang_attribute) (name, args, decl, type))
6eaba4a7
DE
785 warning ("`%s' attribute directive ignored",
786 IDENTIFIER_POINTER (name));
17c1a44f
SC
787 else if (decl != 0)
788 type = TREE_TYPE (decl);
53065596
RK
789 continue;
790 }
791 else if (attrtab[i].decl_req && decl == 0)
792 {
793 warning ("`%s' attribute does not apply to types",
794 IDENTIFIER_POINTER (name));
795 continue;
796 }
797 else if (list_length (args) < attrtab[i].min
798 || list_length (args) > attrtab[i].max)
799 {
800 error ("wrong number of arguments specified for `%s' attribute",
801 IDENTIFIER_POINTER (name));
802 continue;
803 }
804
805 id = attrtab[i].id;
806 switch (id)
807 {
808 case A_PACKED:
1bcf5b08 809 if (is_type)
a89ca5c6
RK
810 TYPE_PACKED (type) = 1;
811 else if (TREE_CODE (decl) == FIELD_DECL)
53065596
RK
812 DECL_PACKED (decl) = 1;
813 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
814 used for DECL_REGISTER. It wouldn't mean anything anyway. */
815 else
816 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
817 break;
818
bbb1ae01
RK
819 case A_NOCOMMON:
820 if (TREE_CODE (decl) == VAR_DECL)
821 DECL_COMMON (decl) = 0;
822 else
823 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
824 break;
825
2786cbad
JM
826 case A_COMMON:
827 if (TREE_CODE (decl) == VAR_DECL)
828 DECL_COMMON (decl) = 1;
829 else
830 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
831 break;
832
53065596
RK
833 case A_NORETURN:
834 if (TREE_CODE (decl) == FUNCTION_DECL)
835 TREE_THIS_VOLATILE (decl) = 1;
836 else if (TREE_CODE (type) == POINTER_TYPE
837 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
b57062ca 838 TREE_TYPE (decl) = type
53065596
RK
839 = build_pointer_type
840 (build_type_variant (TREE_TYPE (type),
841 TREE_READONLY (TREE_TYPE (type)), 1));
842 else
843 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
844 break;
845
140592a0
AG
846 case A_MALLOC:
847 if (TREE_CODE (decl) == FUNCTION_DECL)
848 DECL_IS_MALLOC (decl) = 1;
2a8f6b90 849 /* ??? TODO: Support types. */
140592a0
AG
850 else
851 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
852 break;
853
1c4fadec 854 case A_UNUSED:
6d819282 855 if (is_type)
d20a70b4
CM
856 if (decl)
857 TREE_USED (decl) = 1;
858 else
859 TREE_USED (type) = 1;
b57062ca 860 else if (TREE_CODE (decl) == PARM_DECL
6d819282 861 || TREE_CODE (decl) == VAR_DECL
736b02fd
KG
862 || TREE_CODE (decl) == FUNCTION_DECL
863 || TREE_CODE (decl) == LABEL_DECL)
1c4fadec
RK
864 TREE_USED (decl) = 1;
865 else
866 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
867 break;
868
53065596
RK
869 case A_CONST:
870 if (TREE_CODE (decl) == FUNCTION_DECL)
871 TREE_READONLY (decl) = 1;
872 else if (TREE_CODE (type) == POINTER_TYPE
873 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
874 TREE_TYPE (decl) = type
875 = build_pointer_type
876 (build_type_variant (TREE_TYPE (type), 1,
877 TREE_THIS_VOLATILE (TREE_TYPE (type))));
878 else
879 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
880 break;
881
2a8f6b90
JH
882 case A_PURE:
883 if (TREE_CODE (decl) == FUNCTION_DECL)
884 DECL_IS_PURE (decl) = 1;
885 /* ??? TODO: Support types. */
886 else
887 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
888 break;
889
890
53065596 891 case A_T_UNION:
1bcf5b08 892 if (is_type
53065596 893 && TREE_CODE (type) == UNION_TYPE
1bcf5b08 894 && (decl == 0
62b1077c
RK
895 || (TYPE_FIELDS (type) != 0
896 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
1bcf5b08
RK
897 TYPE_TRANSPARENT_UNION (type) = 1;
898 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
53065596
RK
899 && TREE_CODE (type) == UNION_TYPE
900 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
1bcf5b08 901 DECL_TRANSPARENT_UNION (decl) = 1;
53065596
RK
902 else
903 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
904 break;
905
906 case A_CONSTRUCTOR:
907 if (TREE_CODE (decl) == FUNCTION_DECL
908 && TREE_CODE (type) == FUNCTION_TYPE
909 && decl_function_context (decl) == 0)
1a16a053
RK
910 {
911 DECL_STATIC_CONSTRUCTOR (decl) = 1;
912 TREE_USED (decl) = 1;
913 }
53065596
RK
914 else
915 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
916 break;
917
918 case A_DESTRUCTOR:
919 if (TREE_CODE (decl) == FUNCTION_DECL
920 && TREE_CODE (type) == FUNCTION_TYPE
921 && decl_function_context (decl) == 0)
1a16a053
RK
922 {
923 DECL_STATIC_DESTRUCTOR (decl) = 1;
924 TREE_USED (decl) = 1;
925 }
53065596
RK
926 else
927 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
928 break;
929
930 case A_MODE:
931 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
932 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
933 else
934 {
935 int j;
dff01034 936 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
53065596
RK
937 int len = strlen (p);
938 enum machine_mode mode = VOIDmode;
939 tree typefm;
940
941 if (len > 4 && p[0] == '_' && p[1] == '_'
942 && p[len - 1] == '_' && p[len - 2] == '_')
943 {
3438dff9 944 char *newp = (char *) alloca (len - 1);
53065596
RK
945
946 strcpy (newp, &p[2]);
947 newp[len - 4] = '\0';
948 p = newp;
949 }
950
951 /* Give this decl a type with the specified mode.
952 First check for the special modes. */
953 if (! strcmp (p, "byte"))
954 mode = byte_mode;
955 else if (!strcmp (p, "word"))
956 mode = word_mode;
957 else if (! strcmp (p, "pointer"))
958 mode = ptr_mode;
959 else
960 for (j = 0; j < NUM_MACHINE_MODES; j++)
961 if (!strcmp (p, GET_MODE_NAME (j)))
962 mode = (enum machine_mode) j;
963
964 if (mode == VOIDmode)
965 error ("unknown machine mode `%s'", p);
966 else if (0 == (typefm = type_for_mode (mode,
967 TREE_UNSIGNED (type))))
968 error ("no data type for mode `%s'", p);
969 else
970 {
b15ad712
JM
971 if (TYPE_PRECISION (typefm) > (TREE_UNSIGNED (type)
972 ? TYPE_PRECISION(uintmax_type_node)
973 : TYPE_PRECISION(intmax_type_node))
974 && pedantic)
975 pedwarn ("type with more precision than %s",
976 TREE_UNSIGNED (type) ? "uintmax_t" : "intmax_t");
53065596 977 TREE_TYPE (decl) = type = typefm;
06ceef4e 978 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
53065596
RK
979 layout_decl (decl, 0);
980 }
981 }
982 break;
983
984 case A_SECTION:
5289b665 985#ifdef ASM_OUTPUT_SECTION_NAME
53065596
RK
986 if ((TREE_CODE (decl) == FUNCTION_DECL
987 || TREE_CODE (decl) == VAR_DECL)
988 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
989 {
b57062ca 990 if (TREE_CODE (decl) == VAR_DECL
f4ca236c
RK
991 && current_function_decl != NULL_TREE
992 && ! TREE_STATIC (decl))
53065596
RK
993 error_with_decl (decl,
994 "section attribute cannot be specified for local variables");
995 /* The decl may have already been given a section attribute from
996 a previous declaration. Ensure they match. */
997 else if (DECL_SECTION_NAME (decl) != NULL_TREE
998 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
999 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
1000 error_with_decl (node,
1001 "section of `%s' conflicts with previous declaration");
1002 else
1003 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
1004 }
1005 else
1006 error_with_decl (node,
5289b665
DE
1007 "section attribute not allowed for `%s'");
1008#else
53065596
RK
1009 error_with_decl (node,
1010 "section attributes are not supported for this target");
5289b665 1011#endif
53065596
RK
1012 break;
1013
1014 case A_ALIGNED:
6f38f669 1015 {
53065596 1016 tree align_expr
54630035
RK
1017 = (args ? TREE_VALUE (args)
1018 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
05bccae2 1019 int i;
d125d268 1020
53065596
RK
1021 /* Strip any NOPs of any kind. */
1022 while (TREE_CODE (align_expr) == NOP_EXPR
1023 || TREE_CODE (align_expr) == CONVERT_EXPR
1024 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
1025 align_expr = TREE_OPERAND (align_expr, 0);
b57062ca 1026
53065596
RK
1027 if (TREE_CODE (align_expr) != INTEGER_CST)
1028 {
1029 error ("requested alignment is not a constant");
1030 continue;
1031 }
1032
05bccae2 1033 if ((i = tree_log2 (align_expr)) == -1)
53065596 1034 error ("requested alignment is not a power of 2");
05bccae2
RK
1035 else if (i > HOST_BITS_PER_INT - 2)
1036 error ("requested alignment is too large");
53065596 1037 else if (is_type)
11cf4d18 1038 {
aee3c6b0
JW
1039 /* If we have a TYPE_DECL, then copy the type, so that we
1040 don't accidentally modify a builtin type. See pushdecl. */
1041 if (decl && TREE_TYPE (decl) != error_mark_node
1042 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
af80d489 1043 {
aee3c6b0
JW
1044 tree tt = TREE_TYPE (decl);
1045 DECL_ORIGINAL_TYPE (decl) = tt;
1046 tt = build_type_copy (tt);
1047 TYPE_NAME (tt) = decl;
1048 TREE_USED (tt) = TREE_USED (decl);
1049 TREE_TYPE (decl) = tt;
1050 type = tt;
af80d489 1051 }
aee3c6b0
JW
1052
1053 TYPE_ALIGN (type) = (1 << i) * BITS_PER_UNIT;
1054 TYPE_USER_ALIGN (type) = 1;
11cf4d18 1055 }
53065596
RK
1056 else if (TREE_CODE (decl) != VAR_DECL
1057 && TREE_CODE (decl) != FIELD_DECL)
1058 error_with_decl (decl,
1059 "alignment may not be specified for `%s'");
1060 else
11cf4d18
JJ
1061 {
1062 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
1063 DECL_USER_ALIGN (decl) = 1;
1064 }
6f38f669 1065 }
53065596 1066 break;
6f38f669 1067
53065596 1068 case A_FORMAT:
1312c143
JM
1069 decl_handle_format_attribute (decl, args);
1070 break;
4b8af8d9 1071
0161e8da 1072 case A_FORMAT_ARG:
1312c143
JM
1073 decl_handle_format_arg_attribute (decl, args);
1074 break;
0161e8da 1075
4b8af8d9
JM
1076 case A_WEAK:
1077 declare_weak (decl);
1078 break;
1079
1080 case A_ALIAS:
1081 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
5d9dd5a5 1082 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
4b8af8d9
JM
1083 error_with_decl (decl,
1084 "`%s' defined both normally and as an alias");
1085 else if (decl_function_context (decl) == 0)
1086 {
20045452
RH
1087 tree id;
1088
1089 id = TREE_VALUE (args);
1090 if (TREE_CODE (id) != STRING_CST)
1091 {
1092 error ("alias arg not a string");
1093 break;
1094 }
1095 id = get_identifier (TREE_STRING_POINTER (id));
06a50fff
ZW
1096 /* This counts as a use of the object pointed to. */
1097 TREE_USED (id) = 1;
20045452 1098
4b8af8d9
JM
1099 if (TREE_CODE (decl) == FUNCTION_DECL)
1100 DECL_INITIAL (decl) = error_mark_node;
1101 else
1102 DECL_EXTERNAL (decl) = 0;
1103 assemble_alias (decl, id);
1104 }
1105 else
1106 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1107 break;
07417085 1108
7d384cc0
KR
1109 case A_NO_CHECK_MEMORY_USAGE:
1110 if (TREE_CODE (decl) != FUNCTION_DECL)
1111 {
1112 error_with_decl (decl,
1113 "`%s' attribute applies only to functions",
1114 IDENTIFIER_POINTER (name));
1115 }
1116 else if (DECL_INITIAL (decl))
1117 {
1118 error_with_decl (decl,
1119 "can't set `%s' attribute after definition",
1120 IDENTIFIER_POINTER (name));
1121 }
1122 else
1123 DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
1124 break;
1125
07417085
KR
1126 case A_NO_INSTRUMENT_FUNCTION:
1127 if (TREE_CODE (decl) != FUNCTION_DECL)
1128 {
1129 error_with_decl (decl,
1130 "`%s' attribute applies only to functions",
1131 IDENTIFIER_POINTER (name));
1132 }
1133 else if (DECL_INITIAL (decl))
1134 {
1135 error_with_decl (decl,
1136 "can't set `%s' attribute after definition",
1137 IDENTIFIER_POINTER (name));
1138 }
1139 else
1140 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1141 break;
a157febd
GK
1142
1143 case A_NO_LIMIT_STACK:
1144 if (TREE_CODE (decl) != FUNCTION_DECL)
1145 {
1146 error_with_decl (decl,
1147 "`%s' attribute applies only to functions",
1148 IDENTIFIER_POINTER (name));
1149 }
1150 else if (DECL_INITIAL (decl))
1151 {
1152 error_with_decl (decl,
1153 "can't set `%s' attribute after definition",
1154 IDENTIFIER_POINTER (name));
1155 }
1156 else
1157 DECL_NO_LIMIT_STACK (decl) = 1;
1158 break;
53065596
RK
1159 }
1160 }
b30f223b 1161}
800f4153
RK
1162
1163/* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1164 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1165
1166 The head of the declspec list is stored in DECLSPECS.
1167 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1168
1169 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1170 the list elements. We drop the containing TREE_LIST nodes and link the
1171 resulting attributes together the way decl_attributes expects them. */
1172
1173void
1174split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1175 tree specs_attrs;
1176 tree *declspecs, *prefix_attributes;
1177{
1178 tree t, s, a, next, specs, attrs;
1179
101e59f4
ML
1180 /* This can happen after an __extension__ in pedantic mode. */
1181 if (specs_attrs != NULL_TREE
1182 && TREE_CODE (specs_attrs) == INTEGER_CST)
1183 {
1184 *declspecs = NULL_TREE;
1185 *prefix_attributes = NULL_TREE;
1186 return;
1187 }
1188
800f4153
RK
1189 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1190 if (specs_attrs != NULL_TREE
1191 && TREE_CODE (specs_attrs) != TREE_LIST)
1192 {
1193 *declspecs = specs_attrs;
1194 *prefix_attributes = NULL_TREE;
1195 return;
1196 }
1197
1198 /* Remember to keep the lists in the same order, element-wise. */
1199
1200 specs = s = NULL_TREE;
1201 attrs = a = NULL_TREE;
1202 for (t = specs_attrs; t; t = next)
1203 {
1204 next = TREE_CHAIN (t);
1205 /* Declspecs have a non-NULL TREE_VALUE. */
1206 if (TREE_VALUE (t) != NULL_TREE)
1207 {
1208 if (specs == NULL_TREE)
1209 specs = s = t;
1210 else
1211 {
1212 TREE_CHAIN (s) = t;
1213 s = t;
1214 }
1215 }
1216 else
1217 {
1218 if (attrs == NULL_TREE)
1219 attrs = a = TREE_PURPOSE (t);
1220 else
1221 {
1222 TREE_CHAIN (a) = TREE_PURPOSE (t);
1223 a = TREE_PURPOSE (t);
1224 }
1225 /* More attrs can be linked here, move A to the end. */
1226 while (TREE_CHAIN (a) != NULL_TREE)
1227 a = TREE_CHAIN (a);
1228 }
1229 }
1230
1231 /* Terminate the lists. */
1232 if (s != NULL_TREE)
1233 TREE_CHAIN (s) = NULL_TREE;
1234 if (a != NULL_TREE)
1235 TREE_CHAIN (a) = NULL_TREE;
1236
1237 /* All done. */
1238 *declspecs = specs;
1239 *prefix_attributes = attrs;
1240}
d9525bec
BK
1241
1242/* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1243 This function is used by the parser when a rule will accept attributes
1244 in a particular position, but we don't want to support that just yet.
1245
1246 A warning is issued for every ignored attribute. */
1247
1248tree
1249strip_attrs (specs_attrs)
1250 tree specs_attrs;
1251{
1252 tree specs, attrs;
1253
1254 split_specs_attrs (specs_attrs, &specs, &attrs);
1255
1256 while (attrs)
1257 {
1258 warning ("`%s' attribute ignored",
1259 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1260 attrs = TREE_CHAIN (attrs);
1261 }
1262
1263 return specs;
1264}
b30f223b 1265\f
c70eaeaf 1266static int is_valid_printf_arglist PARAMS ((tree));
f58e0b0c 1267static rtx c_expand_builtin PARAMS ((tree, rtx, enum machine_mode, enum expand_modifier));
c70eaeaf
KG
1268static rtx c_expand_builtin_printf PARAMS ((tree, rtx, enum machine_mode,
1269 enum expand_modifier, int));
18f988a0
KG
1270static rtx c_expand_builtin_fprintf PARAMS ((tree, rtx, enum machine_mode,
1271 enum expand_modifier, int));
1ccf251f 1272\f
d74154d5
RS
1273/* Print a warning if a constant expression had overflow in folding.
1274 Invoke this function on every expression that the language
1275 requires to be a constant expression.
1276 Note the ANSI C standard says it is erroneous for a
1277 constant expression to overflow. */
96571883
BK
1278
1279void
1280constant_expression_warning (value)
1281 tree value;
1282{
c05f751c
RK
1283 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1284 || TREE_CODE (value) == COMPLEX_CST)
1285 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1286 pedwarn ("overflow in constant expression");
d74154d5
RS
1287}
1288
1289/* Print a warning if an expression had overflow in folding.
1290 Invoke this function on every expression that
1291 (1) appears in the source code, and
1292 (2) might be a constant expression that overflowed, and
1293 (3) is not already checked by convert_and_check;
1294 however, do not invoke this function on operands of explicit casts. */
1295
1296void
1297overflow_warning (value)
1298 tree value;
1299{
c05f751c
RK
1300 if ((TREE_CODE (value) == INTEGER_CST
1301 || (TREE_CODE (value) == COMPLEX_CST
1302 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1303 && TREE_OVERFLOW (value))
d74154d5 1304 {
7193bce2 1305 TREE_OVERFLOW (value) = 0;
e78a3b42
RK
1306 if (skip_evaluation == 0)
1307 warning ("integer overflow in expression");
d74154d5 1308 }
c05f751c
RK
1309 else if ((TREE_CODE (value) == REAL_CST
1310 || (TREE_CODE (value) == COMPLEX_CST
1311 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1312 && TREE_OVERFLOW (value))
1313 {
1314 TREE_OVERFLOW (value) = 0;
e78a3b42
RK
1315 if (skip_evaluation == 0)
1316 warning ("floating point overflow in expression");
c05f751c 1317 }
d74154d5
RS
1318}
1319
1320/* Print a warning if a large constant is truncated to unsigned,
1321 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1322 Invoke this function on every expression that might be implicitly
1323 converted to an unsigned type. */
1324
1325void
1326unsigned_conversion_warning (result, operand)
1327 tree result, operand;
1328{
1329 if (TREE_CODE (operand) == INTEGER_CST
1330 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
1331 && TREE_UNSIGNED (TREE_TYPE (result))
e78a3b42 1332 && skip_evaluation == 0
d74154d5
RS
1333 && !int_fits_type_p (operand, TREE_TYPE (result)))
1334 {
1335 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
1336 /* This detects cases like converting -129 or 256 to unsigned char. */
90c939d4 1337 warning ("large integer implicitly truncated to unsigned type");
d74154d5 1338 else if (warn_conversion)
90c939d4 1339 warning ("negative integer implicitly converted to unsigned type");
d74154d5
RS
1340 }
1341}
1342
d02b54f6
JJ
1343/* Nonzero if constant C has a value that is permissible
1344 for type TYPE (an INTEGER_TYPE). */
1345
1346static int
1347constant_fits_type_p (c, type)
1348 tree c, type;
1349{
1350 if (TREE_CODE (c) == INTEGER_CST)
1351 return int_fits_type_p (c, type);
1352
1353 c = convert (type, c);
1354 return !TREE_OVERFLOW (c);
1355}
1356
d74154d5
RS
1357/* Convert EXPR to TYPE, warning about conversion problems with constants.
1358 Invoke this function on every expression that is converted implicitly,
1359 i.e. because of language rules and not because of an explicit cast. */
1360
1361tree
1362convert_and_check (type, expr)
1363 tree type, expr;
1364{
1365 tree t = convert (type, expr);
1366 if (TREE_CODE (t) == INTEGER_CST)
1367 {
7193bce2 1368 if (TREE_OVERFLOW (t))
d74154d5 1369 {
7193bce2
PE
1370 TREE_OVERFLOW (t) = 0;
1371
868fc750
RK
1372 /* Do not diagnose overflow in a constant expression merely
1373 because a conversion overflowed. */
1374 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
1375
7193bce2
PE
1376 /* No warning for converting 0x80000000 to int. */
1377 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
1378 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1379 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
22ba338b
RS
1380 /* If EXPR fits in the unsigned version of TYPE,
1381 don't warn unless pedantic. */
e78a3b42
RK
1382 if ((pedantic
1383 || TREE_UNSIGNED (type)
d02b54f6 1384 || ! constant_fits_type_p (expr, unsigned_type (type)))
e78a3b42 1385 && skip_evaluation == 0)
bb72a084 1386 warning ("overflow in implicit constant conversion");
d74154d5
RS
1387 }
1388 else
1389 unsigned_conversion_warning (t, expr);
1390 }
1391 return t;
96571883
BK
1392}
1393\f
235cfbc4
BS
1394/* A node in a list that describes references to variables (EXPR), which are
1395 either read accesses if WRITER is zero, or write accesses, in which case
1396 WRITER is the parent of EXPR. */
1397struct tlist
1398{
1399 struct tlist *next;
1400 tree expr, writer;
1401};
1402
1403/* Used to implement a cache the results of a call to verify_tree. We only
1404 use this for SAVE_EXPRs. */
1405struct tlist_cache
1406{
1407 struct tlist_cache *next;
1408 struct tlist *cache_before_sp;
1409 struct tlist *cache_after_sp;
1410 tree expr;
2683ed8d
BS
1411};
1412
235cfbc4
BS
1413/* Obstack to use when allocating tlist structures, and corresponding
1414 firstobj. */
1415static struct obstack tlist_obstack;
1416static char *tlist_firstobj = 0;
1417
1418/* Keep track of the identifiers we've warned about, so we can avoid duplicate
1419 warnings. */
1420static struct tlist *warned_ids;
1421/* SAVE_EXPRs need special treatment. We process them only once and then
1422 cache the results. */
1423static struct tlist_cache *save_expr_cache;
1424
1425static void add_tlist PARAMS ((struct tlist **, struct tlist *, tree, int));
1426static void merge_tlist PARAMS ((struct tlist **, struct tlist *, int));
1427static void verify_tree PARAMS ((tree, struct tlist **, struct tlist **, tree));
1428static int warning_candidate_p PARAMS ((tree));
1429static void warn_for_collisions PARAMS ((struct tlist *));
1430static void warn_for_collisions_1 PARAMS ((tree, tree, struct tlist *, int));
1431static struct tlist *new_tlist PARAMS ((struct tlist *, tree, tree));
2683ed8d
BS
1432static void verify_sequence_points PARAMS ((tree));
1433
235cfbc4
BS
1434/* Create a new struct tlist and fill in its fields. */
1435static struct tlist *
1436new_tlist (next, t, writer)
1437 struct tlist *next;
1438 tree t;
1439 tree writer;
1440{
1441 struct tlist *l;
1442 l = (struct tlist *) obstack_alloc (&tlist_obstack, sizeof *l);
1443 l->next = next;
1444 l->expr = t;
1445 l->writer = writer;
1446 return l;
1447}
1448
1449/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1450 is nonnull, we ignore any node we find which has a writer equal to it. */
1451
1452static void
1453add_tlist (to, add, exclude_writer, copy)
1454 struct tlist **to;
1455 struct tlist *add;
1456 tree exclude_writer;
1457 int copy;
1458{
1459 while (add)
1460 {
1461 struct tlist *next = add->next;
1462 if (! copy)
1463 add->next = *to;
1464 if (! exclude_writer || add->writer != exclude_writer)
1465 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1466 add = next;
1467 }
1468}
1469
1470/* Merge the nodes of ADD into TO. This merging process is done so that for
1471 each variable that already exists in TO, no new node is added; however if
1472 there is a write access recorded in ADD, and an occurrence on TO is only
1473 a read access, then the occurrence in TO will be modified to record the
1474 write. */
2683ed8d
BS
1475
1476static void
235cfbc4
BS
1477merge_tlist (to, add, copy)
1478 struct tlist **to;
1479 struct tlist *add;
1480 int copy;
1481{
1482 struct tlist **end = to;
1483
1484 while (*end)
1485 end = &(*end)->next;
1486
1487 while (add)
1488 {
1489 int found = 0;
1490 struct tlist *tmp2;
1491 struct tlist *next = add->next;
1492
1493 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1494 if (tmp2->expr == add->expr)
1495 {
1496 found = 1;
1497 if (! tmp2->writer)
1498 tmp2->writer = add->writer;
1499 }
1500 if (! found)
1501 {
1502 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
1503 end = &(*end)->next;
1504 *end = 0;
1505 }
1506 add = next;
1507 }
1508}
1509
1510/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1511 references in list LIST conflict with it, excluding reads if ONLY writers
1512 is nonzero. */
1513
1514static void
1515warn_for_collisions_1 (written, writer, list, only_writes)
1516 tree written, writer;
1517 struct tlist *list;
1518 int only_writes;
1519{
1520 struct tlist *tmp;
1521
1522 /* Avoid duplicate warnings. */
1523 for (tmp = warned_ids; tmp; tmp = tmp->next)
1524 if (tmp->expr == written)
1525 return;
1526
1527 while (list)
1528 {
1529 if (list->expr == written
1530 && list->writer != writer
1531 && (! only_writes || list->writer))
1532 {
1533 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
1534 warning ("operation on `%s' may be undefined",
1535 IDENTIFIER_POINTER (DECL_NAME (list->expr)));
1536 }
1537 list = list->next;
1538 }
1539}
1540
1541/* Given a list LIST of references to variables, find whether any of these
1542 can cause conflicts due to missing sequence points. */
1543
1544static void
1545warn_for_collisions (list)
1546 struct tlist *list;
1547{
1548 struct tlist *tmp;
1549
1550 for (tmp = list; tmp; tmp = tmp->next)
1551 {
1552 if (tmp->writer)
1553 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1554 }
1555}
1556
1557/* Return nonzero if X is a tree that can be verified by the sequence poitn
1558 warnings. */
1559static int
1560warning_candidate_p (x)
2683ed8d 1561 tree x;
2683ed8d 1562{
235cfbc4
BS
1563 return TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == PARM_DECL;
1564}
2683ed8d 1565
235cfbc4
BS
1566/* Walk the tree X, and record accesses to variables. If X is written by the
1567 parent tree, WRITER is the parent.
1568 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
1569 expression or its only operand forces a sequence point, then everything up
1570 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
1571 in PNO_SP.
1572 Once we return, we will have emitted warnings if any subexpression before
1573 such a sequence point could be undefined. On a higher level, however, the
1574 sequence point may not be relevant, and we'll merge the two lists.
1575
1576 Example: (b++, a) + b;
1577 The call that processes the COMPOUND_EXPR will store the increment of B
1578 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
1579 processes the PLUS_EXPR will need to merge the two lists so that
1580 eventually, all accesses end up on the same list (and we'll warn about the
1581 unordered subexpressions b++ and b.
1582
1583 A note on merging. If we modify the former example so that our expression
1584 becomes
1585 (b++, b) + a
1586 care must be taken not simply to add all three expressions into the final
1587 PNO_SP list. The function merge_tlist takes care of that by merging the
1588 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
1589 way, so that no more than one access to B is recorded. */
2683ed8d 1590
235cfbc4
BS
1591static void
1592verify_tree (x, pbefore_sp, pno_sp, writer)
1593 tree x;
1594 struct tlist **pbefore_sp, **pno_sp;
1595 tree writer;
1596{
1597 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
1598 enum tree_code code;
1599 char class;
2683ed8d 1600
235cfbc4
BS
1601 restart:
1602 code = TREE_CODE (x);
1603 class = TREE_CODE_CLASS (code);
2683ed8d 1604
235cfbc4 1605 if (warning_candidate_p (x))
2683ed8d 1606 {
235cfbc4
BS
1607 *pno_sp = new_tlist (*pno_sp, x, writer);
1608 return;
1609 }
1610
1611 switch (code)
1612 {
52a84e42
BS
1613 case CONSTRUCTOR:
1614 return;
1615
235cfbc4
BS
1616 case COMPOUND_EXPR:
1617 case TRUTH_ANDIF_EXPR:
1618 case TRUTH_ORIF_EXPR:
1619 tmp_before = tmp_nosp = tmp_list3 = 0;
1620 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1621 warn_for_collisions (tmp_nosp);
1622 merge_tlist (pbefore_sp, tmp_before, 0);
1623 merge_tlist (pbefore_sp, tmp_nosp, 0);
1624 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
1625 merge_tlist (pbefore_sp, tmp_list3, 0);
1626 return;
1627
1628 case COND_EXPR:
1629 tmp_before = tmp_list2 = 0;
1630 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
1631 warn_for_collisions (tmp_list2);
1632 merge_tlist (pbefore_sp, tmp_before, 0);
1633 merge_tlist (pbefore_sp, tmp_list2, 1);
1634
1635 tmp_list3 = tmp_nosp = 0;
1636 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
1637 warn_for_collisions (tmp_nosp);
1638 merge_tlist (pbefore_sp, tmp_list3, 0);
1639
1640 tmp_list3 = tmp_list2 = 0;
1641 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
1642 warn_for_collisions (tmp_list2);
1643 merge_tlist (pbefore_sp, tmp_list3, 0);
1644 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1645 two first, to avoid warning for (a ? b++ : b++). */
1646 merge_tlist (&tmp_nosp, tmp_list2, 0);
1647 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1648 return;
1649
2683ed8d
BS
1650 case PREDECREMENT_EXPR:
1651 case PREINCREMENT_EXPR:
1652 case POSTDECREMENT_EXPR:
1653 case POSTINCREMENT_EXPR:
235cfbc4
BS
1654 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
1655 return;
1656
1657 case MODIFY_EXPR:
1658 tmp_before = tmp_nosp = tmp_list3 = 0;
1659 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
1660 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
1661 /* Expressions inside the LHS are not ordered wrt. the sequence points
1662 in the RHS. Example:
1663 *a = (a++, 2)
1664 Despite the fact that the modification of "a" is in the before_sp
1665 list (tmp_before), it conflicts with the use of "a" in the LHS.
1666 We can handle this by adding the contents of tmp_list3
1667 to those of tmp_before, and redoing the collision warnings for that
1668 list. */
1669 add_tlist (&tmp_before, tmp_list3, x, 1);
1670 warn_for_collisions (tmp_before);
1671 /* Exclude the LHS itself here; we first have to merge it into the
1672 tmp_nosp list. This is done to avoid warning for "a = a"; if we
1673 didn't exclude the LHS, we'd get it twice, once as a read and once
1674 as a write. */
1675 add_tlist (pno_sp, tmp_list3, x, 0);
1676 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
1677
1678 merge_tlist (pbefore_sp, tmp_before, 0);
1679 if (warning_candidate_p (TREE_OPERAND (x, 0)))
1680 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
1681 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
1682 return;
2683ed8d
BS
1683
1684 case CALL_EXPR:
235cfbc4
BS
1685 /* We need to warn about conflicts among arguments and conflicts between
1686 args and the function address. Side effects of the function address,
1687 however, are not ordered by the sequence point of the call. */
1688 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
1689 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1690 if (TREE_OPERAND (x, 1))
1691 verify_tree (TREE_OPERAND (x, 1), &tmp_list2, &tmp_list3, NULL_TREE);
1692 merge_tlist (&tmp_list3, tmp_list2, 0);
1693 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
1694 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
1695 warn_for_collisions (tmp_before);
1696 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
1697 return;
2683ed8d
BS
1698
1699 case TREE_LIST:
1700 /* Scan all the list, e.g. indices of multi dimensional array. */
1701 while (x)
1702 {
235cfbc4
BS
1703 tmp_before = tmp_nosp = 0;
1704 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
1705 merge_tlist (&tmp_nosp, tmp_before, 0);
1706 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2683ed8d
BS
1707 x = TREE_CHAIN (x);
1708 }
235cfbc4 1709 return;
2683ed8d 1710
235cfbc4
BS
1711 case SAVE_EXPR:
1712 {
1713 struct tlist_cache *t;
1714 for (t = save_expr_cache; t; t = t->next)
1715 if (t->expr == x)
1716 break;
2683ed8d 1717
235cfbc4 1718 if (! t)
2683ed8d 1719 {
235cfbc4
BS
1720 t = (struct tlist_cache *) obstack_alloc (&tlist_obstack,
1721 sizeof *t);
1722 t->next = save_expr_cache;
1723 t->expr = x;
1724 save_expr_cache = t;
1725
1726 tmp_before = tmp_nosp = 0;
1727 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1728 warn_for_collisions (tmp_nosp);
1729
1730 tmp_list3 = 0;
1731 while (tmp_nosp)
1732 {
1733 struct tlist *t = tmp_nosp;
1734 tmp_nosp = t->next;
1735 merge_tlist (&tmp_list3, t, 0);
1736 }
1737 t->cache_before_sp = tmp_before;
1738 t->cache_after_sp = tmp_list3;
2683ed8d 1739 }
235cfbc4
BS
1740 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
1741 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
1742 return;
1743 }
1744 default:
2683ed8d
BS
1745 break;
1746 }
2683ed8d 1747
235cfbc4 1748 if (class == '1')
2683ed8d 1749 {
235cfbc4
BS
1750 if (first_rtl_op (code) == 0)
1751 return;
1752 x = TREE_OPERAND (x, 0);
1753 writer = 0;
1754 goto restart;
2683ed8d
BS
1755 }
1756
235cfbc4 1757 switch (class)
2683ed8d 1758 {
235cfbc4
BS
1759 case 'r':
1760 case '<':
1761 case '2':
1762 case 'b':
1763 case 'e':
1764 case 's':
1765 case 'x':
1766 {
1767 int lp;
1768 int max = first_rtl_op (TREE_CODE (x));
1769 for (lp = 0; lp < max; lp++)
1770 {
1771 tmp_before = tmp_nosp = 0;
1772 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, NULL_TREE);
1773 merge_tlist (&tmp_nosp, tmp_before, 0);
1774 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1775 }
1776 break;
1777 }
2683ed8d 1778 }
2683ed8d
BS
1779}
1780
1781/* Try to warn for undefined behaviour in EXPR due to missing sequence
1782 points. */
1783
1784static void
1785verify_sequence_points (expr)
1786 tree expr;
1787{
235cfbc4 1788 struct tlist *before_sp = 0, *after_sp = 0;
2683ed8d 1789
235cfbc4
BS
1790 warned_ids = 0;
1791 save_expr_cache = 0;
1792 if (tlist_firstobj == 0)
2683ed8d 1793 {
235cfbc4
BS
1794 gcc_obstack_init (&tlist_obstack);
1795 tlist_firstobj = obstack_alloc (&tlist_obstack, 0);
2683ed8d
BS
1796 }
1797
235cfbc4
BS
1798 verify_tree (expr, &before_sp, &after_sp, 0);
1799 warn_for_collisions (after_sp);
1800 obstack_free (&tlist_obstack, tlist_firstobj);
2683ed8d
BS
1801}
1802
64094f6a 1803tree
b30f223b
RS
1804c_expand_expr_stmt (expr)
1805 tree expr;
1806{
1807 /* Do default conversion if safe and possibly important,
1808 in case within ({...}). */
1809 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
1810 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1811 expr = default_conversion (expr);
1812
2683ed8d
BS
1813 if (warn_sequence_point)
1814 verify_sequence_points (expr);
1815
b30f223b 1816 if (TREE_TYPE (expr) != error_mark_node
b8de2d02 1817 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
b30f223b
RS
1818 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1819 error ("expression statement has incomplete type");
1820
8f17b5c5 1821 last_expr_type = TREE_TYPE (expr);
64094f6a 1822 return add_stmt (build_stmt (EXPR_STMT, expr));
b30f223b
RS
1823}
1824\f
1825/* Validate the expression after `case' and apply default promotions. */
1826
1827tree
1828check_case_value (value)
1829 tree value;
1830{
1831 if (value == NULL_TREE)
1832 return value;
1833
1834 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8493738b 1835 STRIP_TYPE_NOPS (value);
56cb9733
MM
1836 /* In C++, the following is allowed:
1837
1838 const int i = 3;
1839 switch (...) { case i: ... }
1840
1841 So, we try to reduce the VALUE to a constant that way. */
1842 if (c_language == clk_cplusplus)
1843 {
1844 value = decl_constant_value (value);
1845 STRIP_TYPE_NOPS (value);
1846 value = fold (value);
1847 }
b30f223b
RS
1848
1849 if (TREE_CODE (value) != INTEGER_CST
1850 && value != error_mark_node)
1851 {
1852 error ("case label does not reduce to an integer constant");
1853 value = error_mark_node;
1854 }
1855 else
1856 /* Promote char or short to int. */
1857 value = default_conversion (value);
1858
bc690db1
RS
1859 constant_expression_warning (value);
1860
b30f223b
RS
1861 return value;
1862}
1863\f
1864/* Return an integer type with BITS bits of precision,
1865 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1866
1867tree
1868type_for_size (bits, unsignedp)
1869 unsigned bits;
1870 int unsignedp;
1871{
a311b52c
JM
1872 if (bits == TYPE_PRECISION (integer_type_node))
1873 return unsignedp ? unsigned_type_node : integer_type_node;
1874
3fc7e390 1875 if (bits == TYPE_PRECISION (signed_char_type_node))
b30f223b
RS
1876 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1877
3fc7e390 1878 if (bits == TYPE_PRECISION (short_integer_type_node))
b30f223b
RS
1879 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1880
3fc7e390 1881 if (bits == TYPE_PRECISION (long_integer_type_node))
b30f223b
RS
1882 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1883
3fc7e390 1884 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b30f223b
RS
1885 return (unsignedp ? long_long_unsigned_type_node
1886 : long_long_integer_type_node);
1887
835f9b4d
GRK
1888 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
1889 return (unsignedp ? widest_unsigned_literal_type_node
1890 : widest_integer_literal_type_node);
1891
3fc7e390
RS
1892 if (bits <= TYPE_PRECISION (intQI_type_node))
1893 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1894
1895 if (bits <= TYPE_PRECISION (intHI_type_node))
1896 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1897
1898 if (bits <= TYPE_PRECISION (intSI_type_node))
1899 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1900
1901 if (bits <= TYPE_PRECISION (intDI_type_node))
1902 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1903
b30f223b
RS
1904 return 0;
1905}
1906
1907/* Return a data type that has machine mode MODE.
1908 If the mode is an integer,
1909 then UNSIGNEDP selects between signed and unsigned types. */
1910
1911tree
1912type_for_mode (mode, unsignedp)
1913 enum machine_mode mode;
1914 int unsignedp;
1915{
a311b52c
JM
1916 if (mode == TYPE_MODE (integer_type_node))
1917 return unsignedp ? unsigned_type_node : integer_type_node;
1918
b30f223b
RS
1919 if (mode == TYPE_MODE (signed_char_type_node))
1920 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1921
1922 if (mode == TYPE_MODE (short_integer_type_node))
1923 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1924
b30f223b
RS
1925 if (mode == TYPE_MODE (long_integer_type_node))
1926 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1927
1928 if (mode == TYPE_MODE (long_long_integer_type_node))
1929 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
1930
835f9b4d 1931 if (mode == TYPE_MODE (widest_integer_literal_type_node))
d125d268 1932 return unsignedp ? widest_unsigned_literal_type_node
835f9b4d
GRK
1933 : widest_integer_literal_type_node;
1934
3fc7e390
RS
1935 if (mode == TYPE_MODE (intQI_type_node))
1936 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1937
1938 if (mode == TYPE_MODE (intHI_type_node))
1939 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1940
1941 if (mode == TYPE_MODE (intSI_type_node))
1942 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1943
1944 if (mode == TYPE_MODE (intDI_type_node))
1945 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1946
21a9616b 1947#if HOST_BITS_PER_WIDE_INT >= 64
a6d7e156
JL
1948 if (mode == TYPE_MODE (intTI_type_node))
1949 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
21a9616b 1950#endif
a6d7e156 1951
b30f223b
RS
1952 if (mode == TYPE_MODE (float_type_node))
1953 return float_type_node;
1954
1955 if (mode == TYPE_MODE (double_type_node))
1956 return double_type_node;
1957
1958 if (mode == TYPE_MODE (long_double_type_node))
1959 return long_double_type_node;
1960
1961 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
1962 return build_pointer_type (char_type_node);
1963
1964 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
1965 return build_pointer_type (integer_type_node);
1966
4061f623
BS
1967#ifdef VECTOR_MODE_SUPPORTED_P
1968 if (mode == TYPE_MODE (V4SF_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1969 return V4SF_type_node;
1970 if (mode == TYPE_MODE (V4SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1971 return V4SI_type_node;
1972 if (mode == TYPE_MODE (V2SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1973 return V2SI_type_node;
1974 if (mode == TYPE_MODE (V4HI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1975 return V4HI_type_node;
1976 if (mode == TYPE_MODE (V8QI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1977 return V8QI_type_node;
1978#endif
1979
b30f223b
RS
1980 return 0;
1981}
693a6128
GRK
1982
1983/* Return an unsigned type the same as TYPE in other respects. */
1984tree
1985unsigned_type (type)
1986 tree type;
1987{
1988 tree type1 = TYPE_MAIN_VARIANT (type);
1989 if (type1 == signed_char_type_node || type1 == char_type_node)
1990 return unsigned_char_type_node;
1991 if (type1 == integer_type_node)
1992 return unsigned_type_node;
1993 if (type1 == short_integer_type_node)
1994 return short_unsigned_type_node;
1995 if (type1 == long_integer_type_node)
1996 return long_unsigned_type_node;
1997 if (type1 == long_long_integer_type_node)
1998 return long_long_unsigned_type_node;
1999 if (type1 == widest_integer_literal_type_node)
2000 return widest_unsigned_literal_type_node;
2001#if HOST_BITS_PER_WIDE_INT >= 64
2002 if (type1 == intTI_type_node)
2003 return unsigned_intTI_type_node;
2004#endif
2005 if (type1 == intDI_type_node)
2006 return unsigned_intDI_type_node;
2007 if (type1 == intSI_type_node)
2008 return unsigned_intSI_type_node;
2009 if (type1 == intHI_type_node)
2010 return unsigned_intHI_type_node;
2011 if (type1 == intQI_type_node)
2012 return unsigned_intQI_type_node;
2013
2014 return signed_or_unsigned_type (1, type);
2015}
2016
2017/* Return a signed type the same as TYPE in other respects. */
2018
2019tree
2020signed_type (type)
2021 tree type;
2022{
2023 tree type1 = TYPE_MAIN_VARIANT (type);
2024 if (type1 == unsigned_char_type_node || type1 == char_type_node)
2025 return signed_char_type_node;
2026 if (type1 == unsigned_type_node)
2027 return integer_type_node;
2028 if (type1 == short_unsigned_type_node)
2029 return short_integer_type_node;
2030 if (type1 == long_unsigned_type_node)
2031 return long_integer_type_node;
2032 if (type1 == long_long_unsigned_type_node)
2033 return long_long_integer_type_node;
2034 if (type1 == widest_unsigned_literal_type_node)
2035 return widest_integer_literal_type_node;
2036#if HOST_BITS_PER_WIDE_INT >= 64
2037 if (type1 == unsigned_intTI_type_node)
2038 return intTI_type_node;
2039#endif
2040 if (type1 == unsigned_intDI_type_node)
2041 return intDI_type_node;
2042 if (type1 == unsigned_intSI_type_node)
2043 return intSI_type_node;
2044 if (type1 == unsigned_intHI_type_node)
2045 return intHI_type_node;
2046 if (type1 == unsigned_intQI_type_node)
2047 return intQI_type_node;
2048
2049 return signed_or_unsigned_type (0, type);
2050}
2051
2052/* Return a type the same as TYPE except unsigned or
2053 signed according to UNSIGNEDP. */
2054
2055tree
2056signed_or_unsigned_type (unsignedp, type)
2057 int unsignedp;
2058 tree type;
2059{
2060 if (! INTEGRAL_TYPE_P (type)
2061 || TREE_UNSIGNED (type) == unsignedp)
2062 return type;
2063
2064 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
2065 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
d125d268 2066 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
693a6128 2067 return unsignedp ? unsigned_type_node : integer_type_node;
d125d268 2068 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
693a6128 2069 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
d125d268 2070 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
693a6128 2071 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
d125d268 2072 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
693a6128
GRK
2073 return (unsignedp ? long_long_unsigned_type_node
2074 : long_long_integer_type_node);
d125d268 2075 if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
693a6128
GRK
2076 return (unsignedp ? widest_unsigned_literal_type_node
2077 : widest_integer_literal_type_node);
2078 return type;
2079}
b30f223b 2080\f
6acfe908
JM
2081/* Return the minimum number of bits needed to represent VALUE in a
2082 signed or unsigned type, UNSIGNEDP says which. */
2083
05bccae2 2084unsigned int
6acfe908
JM
2085min_precision (value, unsignedp)
2086 tree value;
2087 int unsignedp;
2088{
2089 int log;
2090
2091 /* If the value is negative, compute its negative minus 1. The latter
2092 adjustment is because the absolute value of the largest negative value
2093 is one larger than the largest positive value. This is equivalent to
2094 a bit-wise negation, so use that operation instead. */
2095
2096 if (tree_int_cst_sgn (value) < 0)
2097 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2098
2099 /* Return the number of bits needed, taking into account the fact
2100 that we need one more bit for a signed than unsigned type. */
2101
2102 if (integer_zerop (value))
2103 log = 0;
6acfe908 2104 else
05bccae2 2105 log = tree_floor_log2 (value);
6acfe908
JM
2106
2107 return log + 1 + ! unsignedp;
2108}
2109\f
b30f223b
RS
2110/* Print an error message for invalid operands to arith operation CODE.
2111 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2112
2113void
2114binary_op_error (code)
2115 enum tree_code code;
2116{
dff01034 2117 register const char *opname;
89c78d7d 2118
b30f223b
RS
2119 switch (code)
2120 {
2121 case NOP_EXPR:
2122 error ("invalid truth-value expression");
2123 return;
2124
2125 case PLUS_EXPR:
2126 opname = "+"; break;
2127 case MINUS_EXPR:
2128 opname = "-"; break;
2129 case MULT_EXPR:
2130 opname = "*"; break;
2131 case MAX_EXPR:
2132 opname = "max"; break;
2133 case MIN_EXPR:
2134 opname = "min"; break;
2135 case EQ_EXPR:
2136 opname = "=="; break;
2137 case NE_EXPR:
2138 opname = "!="; break;
2139 case LE_EXPR:
2140 opname = "<="; break;
2141 case GE_EXPR:
2142 opname = ">="; break;
2143 case LT_EXPR:
2144 opname = "<"; break;
2145 case GT_EXPR:
2146 opname = ">"; break;
2147 case LSHIFT_EXPR:
2148 opname = "<<"; break;
2149 case RSHIFT_EXPR:
2150 opname = ">>"; break;
2151 case TRUNC_MOD_EXPR:
047de90b 2152 case FLOOR_MOD_EXPR:
b30f223b
RS
2153 opname = "%"; break;
2154 case TRUNC_DIV_EXPR:
047de90b 2155 case FLOOR_DIV_EXPR:
b30f223b
RS
2156 opname = "/"; break;
2157 case BIT_AND_EXPR:
2158 opname = "&"; break;
2159 case BIT_IOR_EXPR:
2160 opname = "|"; break;
2161 case TRUTH_ANDIF_EXPR:
2162 opname = "&&"; break;
2163 case TRUTH_ORIF_EXPR:
2164 opname = "||"; break;
2165 case BIT_XOR_EXPR:
2166 opname = "^"; break;
047de90b
RS
2167 case LROTATE_EXPR:
2168 case RROTATE_EXPR:
2169 opname = "rotate"; break;
6d819282
MK
2170 default:
2171 opname = "unknown"; break;
b30f223b
RS
2172 }
2173 error ("invalid operands to binary %s", opname);
2174}
2175\f
2176/* Subroutine of build_binary_op, used for comparison operations.
2177 See if the operands have both been converted from subword integer types
2178 and, if so, perhaps change them both back to their original type.
94dccd9d
RS
2179 This function is also responsible for converting the two operands
2180 to the proper common type for comparison.
b30f223b
RS
2181
2182 The arguments of this function are all pointers to local variables
2183 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2184 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2185
2186 If this function returns nonzero, it means that the comparison has
2187 a constant value. What this function returns is an expression for
2188 that value. */
2189
2190tree
2191shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2192 tree *op0_ptr, *op1_ptr;
2193 tree *restype_ptr;
2194 enum tree_code *rescode_ptr;
2195{
2196 register tree type;
2197 tree op0 = *op0_ptr;
2198 tree op1 = *op1_ptr;
2199 int unsignedp0, unsignedp1;
2200 int real1, real2;
2201 tree primop0, primop1;
2202 enum tree_code code = *rescode_ptr;
2203
2204 /* Throw away any conversions to wider types
2205 already present in the operands. */
2206
2207 primop0 = get_narrower (op0, &unsignedp0);
2208 primop1 = get_narrower (op1, &unsignedp1);
2209
2210 /* Handle the case that OP0 does not *contain* a conversion
2211 but it *requires* conversion to FINAL_TYPE. */
2212
2213 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2214 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2215 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2216 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2217
2218 /* If one of the operands must be floated, we cannot optimize. */
2219 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2220 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2221
2222 /* If first arg is constant, swap the args (changing operation
5af6001b
RK
2223 so value is preserved), for canonicalization. Don't do this if
2224 the second arg is 0. */
b30f223b 2225
5af6001b
RK
2226 if (TREE_CONSTANT (primop0)
2227 && ! integer_zerop (primop1) && ! real_zerop (primop1))
b30f223b
RS
2228 {
2229 register tree tem = primop0;
2230 register int temi = unsignedp0;
2231 primop0 = primop1;
2232 primop1 = tem;
2233 tem = op0;
2234 op0 = op1;
2235 op1 = tem;
2236 *op0_ptr = op0;
2237 *op1_ptr = op1;
2238 unsignedp0 = unsignedp1;
2239 unsignedp1 = temi;
2240 temi = real1;
2241 real1 = real2;
2242 real2 = temi;
2243
2244 switch (code)
2245 {
2246 case LT_EXPR:
2247 code = GT_EXPR;
2248 break;
2249 case GT_EXPR:
2250 code = LT_EXPR;
2251 break;
2252 case LE_EXPR:
2253 code = GE_EXPR;
2254 break;
2255 case GE_EXPR:
2256 code = LE_EXPR;
2257 break;
6d819282
MK
2258 default:
2259 break;
b30f223b
RS
2260 }
2261 *rescode_ptr = code;
2262 }
2263
2264 /* If comparing an integer against a constant more bits wide,
2265 maybe we can deduce a value of 1 or 0 independent of the data.
2266 Or else truncate the constant now
2267 rather than extend the variable at run time.
2268
2269 This is only interesting if the constant is the wider arg.
2270 Also, it is not safe if the constant is unsigned and the
2271 variable arg is signed, since in this case the variable
2272 would be sign-extended and then regarded as unsigned.
2273 Our technique fails in this case because the lowest/highest
2274 possible unsigned results don't follow naturally from the
2275 lowest/highest possible values of the variable operand.
2276 For just EQ_EXPR and NE_EXPR there is another technique that
2277 could be used: see if the constant can be faithfully represented
2278 in the other operand's type, by truncating it and reextending it
2279 and see if that preserves the constant's value. */
2280
2281 if (!real1 && !real2
2282 && TREE_CODE (primop1) == INTEGER_CST
2283 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2284 {
2285 int min_gt, max_gt, min_lt, max_lt;
2286 tree maxval, minval;
2287 /* 1 if comparison is nominally unsigned. */
2288 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2289 tree val;
2290
2291 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
8bbd5685
CW
2292
2293 /* If TYPE is an enumeration, then we need to get its min/max
2294 values from it's underlying integral type, not the enumerated
2295 type itself. */
2296 if (TREE_CODE (type) == ENUMERAL_TYPE)
2297 type = type_for_size (TYPE_PRECISION (type), unsignedp0);
b30f223b
RS
2298
2299 maxval = TYPE_MAX_VALUE (type);
2300 minval = TYPE_MIN_VALUE (type);
2301
2302 if (unsignedp && !unsignedp0)
2303 *restype_ptr = signed_type (*restype_ptr);
2304
2305 if (TREE_TYPE (primop1) != *restype_ptr)
2306 primop1 = convert (*restype_ptr, primop1);
2307 if (type != *restype_ptr)
2308 {
2309 minval = convert (*restype_ptr, minval);
2310 maxval = convert (*restype_ptr, maxval);
2311 }
2312
2313 if (unsignedp && unsignedp0)
2314 {
2315 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2316 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2317 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2318 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2319 }
2320 else
2321 {
2322 min_gt = INT_CST_LT (primop1, minval);
2323 max_gt = INT_CST_LT (primop1, maxval);
2324 min_lt = INT_CST_LT (minval, primop1);
2325 max_lt = INT_CST_LT (maxval, primop1);
2326 }
2327
2328 val = 0;
2329 /* This used to be a switch, but Genix compiler can't handle that. */
2330 if (code == NE_EXPR)
2331 {
2332 if (max_lt || min_gt)
a360da3a 2333 val = boolean_true_node;
b30f223b
RS
2334 }
2335 else if (code == EQ_EXPR)
2336 {
2337 if (max_lt || min_gt)
a360da3a 2338 val = boolean_false_node;
b30f223b
RS
2339 }
2340 else if (code == LT_EXPR)
2341 {
2342 if (max_lt)
a360da3a 2343 val = boolean_true_node;
b30f223b 2344 if (!min_lt)
a360da3a 2345 val = boolean_false_node;
b30f223b
RS
2346 }
2347 else if (code == GT_EXPR)
2348 {
2349 if (min_gt)
a360da3a 2350 val = boolean_true_node;
b30f223b 2351 if (!max_gt)
a360da3a 2352 val = boolean_false_node;
b30f223b
RS
2353 }
2354 else if (code == LE_EXPR)
2355 {
2356 if (!max_gt)
a360da3a 2357 val = boolean_true_node;
b30f223b 2358 if (min_gt)
a360da3a 2359 val = boolean_false_node;
b30f223b
RS
2360 }
2361 else if (code == GE_EXPR)
2362 {
2363 if (!min_lt)
a360da3a 2364 val = boolean_true_node;
b30f223b 2365 if (max_lt)
a360da3a 2366 val = boolean_false_node;
b30f223b
RS
2367 }
2368
2369 /* If primop0 was sign-extended and unsigned comparison specd,
2370 we did a signed comparison above using the signed type bounds.
2371 But the comparison we output must be unsigned.
2372
2373 Also, for inequalities, VAL is no good; but if the signed
2374 comparison had *any* fixed result, it follows that the
2375 unsigned comparison just tests the sign in reverse
2376 (positive values are LE, negative ones GE).
2377 So we can generate an unsigned comparison
2378 against an extreme value of the signed type. */
2379
2380 if (unsignedp && !unsignedp0)
2381 {
2382 if (val != 0)
2383 switch (code)
2384 {
2385 case LT_EXPR:
2386 case GE_EXPR:
2387 primop1 = TYPE_MIN_VALUE (type);
2388 val = 0;
2389 break;
2390
2391 case LE_EXPR:
2392 case GT_EXPR:
2393 primop1 = TYPE_MAX_VALUE (type);
2394 val = 0;
2395 break;
6d819282
MK
2396
2397 default:
2398 break;
b30f223b
RS
2399 }
2400 type = unsigned_type (type);
2401 }
2402
b7c9c707 2403 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
b30f223b
RS
2404 {
2405 /* This is the case of (char)x >?< 0x80, which people used to use
2406 expecting old C compilers to change the 0x80 into -0x80. */
a360da3a 2407 if (val == boolean_false_node)
07be2a23 2408 warning ("comparison is always false due to limited range of data type");
a360da3a 2409 if (val == boolean_true_node)
07be2a23 2410 warning ("comparison is always true due to limited range of data type");
b30f223b
RS
2411 }
2412
b7c9c707 2413 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
b30f223b 2414 {
1e276c4a 2415 /* This is the case of (unsigned char)x >?< -1 or < 0. */
a360da3a 2416 if (val == boolean_false_node)
07be2a23 2417 warning ("comparison is always false due to limited range of data type");
a360da3a 2418 if (val == boolean_true_node)
07be2a23 2419 warning ("comparison is always true due to limited range of data type");
b30f223b
RS
2420 }
2421
2422 if (val != 0)
2423 {
2424 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2425 if (TREE_SIDE_EFFECTS (primop0))
2426 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2427 return val;
2428 }
2429
2430 /* Value is not predetermined, but do the comparison
2431 in the type of the operand that is not constant.
2432 TYPE is already properly set. */
2433 }
2434 else if (real1 && real2
766f6c30
RS
2435 && (TYPE_PRECISION (TREE_TYPE (primop0))
2436 == TYPE_PRECISION (TREE_TYPE (primop1))))
b30f223b
RS
2437 type = TREE_TYPE (primop0);
2438
2439 /* If args' natural types are both narrower than nominal type
2440 and both extend in the same manner, compare them
2441 in the type of the wider arg.
2442 Otherwise must actually extend both to the nominal
2443 common type lest different ways of extending
2444 alter the result.
2445 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2446
2447 else if (unsignedp0 == unsignedp1 && real1 == real2
2448 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2449 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2450 {
2451 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2452 type = signed_or_unsigned_type (unsignedp0
2453 || TREE_UNSIGNED (*restype_ptr),
2454 type);
2455 /* Make sure shorter operand is extended the right way
2456 to match the longer operand. */
2457 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2458 primop0);
2459 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2460 primop1);
2461 }
2462 else
2463 {
2464 /* Here we must do the comparison on the nominal type
2465 using the args exactly as we received them. */
2466 type = *restype_ptr;
2467 primop0 = op0;
2468 primop1 = op1;
2469
2470 if (!real1 && !real2 && integer_zerop (primop1)
597681f6 2471 && TREE_UNSIGNED (*restype_ptr))
b30f223b
RS
2472 {
2473 tree value = 0;
2474 switch (code)
2475 {
2476 case GE_EXPR:
5af6001b
RK
2477 /* All unsigned values are >= 0, so we warn if extra warnings
2478 are requested. However, if OP0 is a constant that is
2479 >= 0, the signedness of the comparison isn't an issue,
2480 so suppress the warning. */
2c492eef 2481 if (extra_warnings && !in_system_header
5af6001b
RK
2482 && ! (TREE_CODE (primop0) == INTEGER_CST
2483 && ! TREE_OVERFLOW (convert (signed_type (type),
2484 primop0))))
07be2a23 2485 warning ("comparison of unsigned expression >= 0 is always true");
a360da3a 2486 value = boolean_true_node;
b30f223b
RS
2487 break;
2488
2489 case LT_EXPR:
2c492eef 2490 if (extra_warnings && !in_system_header
5af6001b
RK
2491 && ! (TREE_CODE (primop0) == INTEGER_CST
2492 && ! TREE_OVERFLOW (convert (signed_type (type),
2493 primop0))))
07be2a23 2494 warning ("comparison of unsigned expression < 0 is always false");
a360da3a 2495 value = boolean_false_node;
6d819282
MK
2496 break;
2497
2498 default:
2499 break;
b30f223b
RS
2500 }
2501
2502 if (value != 0)
2503 {
2504 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2505 if (TREE_SIDE_EFFECTS (primop0))
2506 return build (COMPOUND_EXPR, TREE_TYPE (value),
2507 primop0, value);
2508 return value;
2509 }
2510 }
2511 }
2512
2513 *op0_ptr = convert (type, primop0);
2514 *op1_ptr = convert (type, primop1);
2515
a360da3a 2516 *restype_ptr = boolean_type_node;
b30f223b
RS
2517
2518 return 0;
2519}
2520\f
2521/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2522 or validate its data type for an `if' or `while' statement or ?..: exp.
2523
2524 This preparation consists of taking the ordinary
2525 representation of an expression expr and producing a valid tree
2526 boolean expression describing whether expr is nonzero. We could
a360da3a 2527 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
b30f223b
RS
2528 but we optimize comparisons, &&, ||, and !.
2529
a360da3a 2530 The resulting type should always be `boolean_type_node'. */
b30f223b
RS
2531
2532tree
2533truthvalue_conversion (expr)
2534 tree expr;
2535{
257e61ed
RS
2536 if (TREE_CODE (expr) == ERROR_MARK)
2537 return expr;
2538
d7c83727 2539#if 0 /* This appears to be wrong for C++. */
257e61ed
RS
2540 /* These really should return error_mark_node after 2.4 is stable.
2541 But not all callers handle ERROR_MARK properly. */
2542 switch (TREE_CODE (TREE_TYPE (expr)))
2543 {
2544 case RECORD_TYPE:
2545 error ("struct type value used where scalar is required");
a360da3a 2546 return boolean_false_node;
257e61ed
RS
2547
2548 case UNION_TYPE:
2549 error ("union type value used where scalar is required");
a360da3a 2550 return boolean_false_node;
257e61ed
RS
2551
2552 case ARRAY_TYPE:
2553 error ("array type value used where scalar is required");
a360da3a 2554 return boolean_false_node;
257e61ed
RS
2555
2556 default:
2557 break;
2558 }
d7c83727 2559#endif /* 0 */
257e61ed 2560
b30f223b
RS
2561 switch (TREE_CODE (expr))
2562 {
b30f223b 2563 case EQ_EXPR:
b30f223b
RS
2564 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2565 case TRUTH_ANDIF_EXPR:
2566 case TRUTH_ORIF_EXPR:
2567 case TRUTH_AND_EXPR:
2568 case TRUTH_OR_EXPR:
9379fac9 2569 case TRUTH_XOR_EXPR:
1180eb10 2570 case TRUTH_NOT_EXPR:
a360da3a
JM
2571 TREE_TYPE (expr) = boolean_type_node;
2572 return expr;
18c0f675 2573
b30f223b
RS
2574 case ERROR_MARK:
2575 return expr;
2576
2577 case INTEGER_CST:
a360da3a 2578 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
b30f223b
RS
2579
2580 case REAL_CST:
a360da3a 2581 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
b30f223b
RS
2582
2583 case ADDR_EXPR:
fc0c675f
RK
2584 /* If we are taking the address of a external decl, it might be zero
2585 if it is weak, so we cannot optimize. */
2f939d94 2586 if (DECL_P (TREE_OPERAND (expr, 0))
fc0c675f
RK
2587 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2588 break;
2589
b30f223b 2590 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
a360da3a
JM
2591 return build (COMPOUND_EXPR, boolean_type_node,
2592 TREE_OPERAND (expr, 0), boolean_true_node);
b30f223b 2593 else
a360da3a 2594 return boolean_true_node;
b30f223b 2595
766f6c30 2596 case COMPLEX_EXPR:
f0b996c5 2597 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
b839fb3f 2598 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
f0b996c5
RS
2599 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2600 truthvalue_conversion (TREE_OPERAND (expr, 1)),
766f6c30
RS
2601 0);
2602
b30f223b
RS
2603 case NEGATE_EXPR:
2604 case ABS_EXPR:
2605 case FLOAT_EXPR:
2606 case FFS_EXPR:
2607 /* These don't change whether an object is non-zero or zero. */
2608 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2609
2610 case LROTATE_EXPR:
2611 case RROTATE_EXPR:
2612 /* These don't change whether an object is zero or non-zero, but
2613 we can't ignore them if their second arg has side-effects. */
2614 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
a360da3a 2615 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
b30f223b
RS
2616 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2617 else
2618 return truthvalue_conversion (TREE_OPERAND (expr, 0));
b57062ca 2619
b30f223b
RS
2620 case COND_EXPR:
2621 /* Distribute the conversion into the arms of a COND_EXPR. */
a360da3a 2622 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
b30f223b
RS
2623 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2624 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2625
2626 case CONVERT_EXPR:
2627 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2628 since that affects how `default_conversion' will behave. */
2629 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2630 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2631 break;
0f41302f 2632 /* fall through... */
b30f223b
RS
2633 case NOP_EXPR:
2634 /* If this is widening the argument, we can ignore it. */
2635 if (TYPE_PRECISION (TREE_TYPE (expr))
2636 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2637 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2638 break;
2639
b30f223b 2640 case MINUS_EXPR:
f87550e0
JW
2641 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2642 this case. */
2643 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2644 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2645 break;
0f41302f 2646 /* fall through... */
f87550e0 2647 case BIT_XOR_EXPR:
d7c83727 2648 /* This and MINUS_EXPR can be changed into a comparison of the
f87550e0 2649 two objects. */
b30f223b
RS
2650 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2651 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2652 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2653 TREE_OPERAND (expr, 1), 1);
2654 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2655 fold (build1 (NOP_EXPR,
2656 TREE_TYPE (TREE_OPERAND (expr, 0)),
2657 TREE_OPERAND (expr, 1))), 1);
e2aab13d 2658
fb48b1f0 2659 case BIT_AND_EXPR:
58cee643
RK
2660 if (integer_onep (TREE_OPERAND (expr, 1))
2661 && TREE_TYPE (expr) != boolean_type_node)
2662 /* Using convert here would cause infinite recursion. */
2663 return build1 (NOP_EXPR, boolean_type_node, expr);
2664 break;
fb48b1f0 2665
e2aab13d
RS
2666 case MODIFY_EXPR:
2667 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2668 warning ("suggest parentheses around assignment used as truth value");
2669 break;
b57062ca 2670
6d819282
MK
2671 default:
2672 break;
b30f223b
RS
2673 }
2674
f0b996c5 2675 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
f0b8d9aa
AS
2676 {
2677 tree tem = save_expr (expr);
2678 return (build_binary_op
2679 ((TREE_SIDE_EFFECTS (expr)
2680 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2681 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
2682 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
2683 0));
2684 }
f0b996c5 2685
b30f223b
RS
2686 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2687}
2688\f
fc2aaf30
JM
2689static tree builtin_function_2 PARAMS ((const char *, const char *, tree, tree,
2690 int, enum built_in_class, int, int,
2691 int));
2692
0b73773c
NH
2693/* Make a variant type in the proper way for C/C++, propagating qualifiers
2694 down to the element type of an array. */
2695
2696tree
3932261a 2697c_build_qualified_type (type, type_quals)
0b73773c 2698 tree type;
3932261a 2699 int type_quals;
0b73773c 2700{
3932261a
MM
2701 /* A restrict-qualified pointer type must be a pointer to object or
2702 incomplete type. Note that the use of POINTER_TYPE_P also allows
2703 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
2704 the C++ front-end also use POINTER_TYPE for pointer-to-member
2705 values, so even though it should be illegal to use `restrict'
2706 with such an entity we don't flag that here. Thus, special case
2707 code for that case is required in the C++ front-end. */
2708 if ((type_quals & TYPE_QUAL_RESTRICT)
2709 && (!POINTER_TYPE_P (type)
2710 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
2711 {
2712 error ("invalid use of `restrict'");
2713 type_quals &= ~TYPE_QUAL_RESTRICT;
2714 }
2715
0b73773c 2716 if (TREE_CODE (type) == ARRAY_TYPE)
3932261a
MM
2717 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
2718 type_quals),
3ab1999b 2719 TYPE_DOMAIN (type));
3932261a
MM
2720 return build_qualified_type (type, type_quals);
2721}
2722
2723/* Apply the TYPE_QUALS to the new DECL. */
2724
2725void
2726c_apply_type_quals_to_decl (type_quals, decl)
2727 int type_quals;
2728 tree decl;
2729{
a6496605
MM
2730 if ((type_quals & TYPE_QUAL_CONST)
2731 || (TREE_TYPE (decl)
2732 && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE))
3932261a
MM
2733 TREE_READONLY (decl) = 1;
2734 if (type_quals & TYPE_QUAL_VOLATILE)
2735 {
2736 TREE_SIDE_EFFECTS (decl) = 1;
2737 TREE_THIS_VOLATILE (decl) = 1;
2738 }
6946bc60 2739 if (type_quals & TYPE_QUAL_RESTRICT)
3932261a 2740 {
6946bc60
MM
2741 if (!TREE_TYPE (decl)
2742 || !POINTER_TYPE_P (TREE_TYPE (decl))
2743 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
2744 error ("invalid use of `restrict'");
2745 else if (flag_strict_aliasing)
3932261a 2746 {
6946bc60
MM
2747 /* No two restricted pointers can point at the same thing.
2748 However, a restricted pointer can point at the same thing
2749 as an unrestricted pointer, if that unrestricted pointer
2750 is based on the restricted pointer. So, we make the
2751 alias set for the restricted pointer a subset of the
2752 alias set for the type pointed to by the type of the
2753 decl. */
2754
3bdf5ad1 2755 HOST_WIDE_INT pointed_to_alias_set
6946bc60 2756 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
d125d268 2757
3bdf5ad1 2758 if (pointed_to_alias_set == 0)
6946bc60
MM
2759 /* It's not legal to make a subset of alias set zero. */
2760 ;
2761 else
2762 {
2763 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
2764 record_alias_subset (pointed_to_alias_set,
2765 DECL_POINTER_ALIAS_SET (decl));
2766 }
3932261a
MM
2767 }
2768 }
2769}
2770
41472af8
MM
2771
2772/* Return the typed-based alias set for T, which may be an expression
3bdf5ad1 2773 or a type. Return -1 if we don't do anything special. */
41472af8 2774
3bdf5ad1 2775HOST_WIDE_INT
2e761e49 2776lang_get_alias_set (t)
41472af8
MM
2777 tree t;
2778{
08bc2431 2779 tree u;
604bb87d
DB
2780
2781 /* We know nothing about vector types */
2782 if (TREE_CODE (t) == VECTOR_TYPE)
2783 return 0;
2784
08bc2431
MM
2785 /* Permit type-punning when accessing a union, provided the access
2786 is directly through the union. For example, this code does not
2787 permit taking the address of a union member and then storing
2788 through it. Even the type-punning allowed here is a GCC
2789 extension, albeit a common and useful one; the C standard says
2790 that such accesses have implementation-defined behavior. */
2791 for (u = t;
2792 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
2793 u = TREE_OPERAND (u, 0))
2794 if (TREE_CODE (u) == COMPONENT_REF
2795 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
2796 return 0;
ece32014 2797
3bdf5ad1 2798 /* If this is a char *, the ANSI C standard says it can alias
f824e5c3
RK
2799 anything. Note that all references need do this. */
2800 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'r'
2801 && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
2802 && TYPE_PRECISION (TREE_TYPE (t)) == TYPE_PRECISION (char_type_node))
3bdf5ad1 2803 return 0;
3932261a 2804
3bdf5ad1
RK
2805 /* That's all the expressions we handle specially. */
2806 if (! TYPE_P (t))
2807 return -1;
41472af8 2808
f824e5c3
RK
2809 /* The C standard specifically allows aliasing between signed and
2810 unsigned variants of the same type. We treat the signed
2811 variant as canonical. */
2812 if (TREE_CODE (t) == INTEGER_TYPE && TREE_UNSIGNED (t))
8f215dce
JJ
2813 {
2814 tree t1 = signed_type (t);
f824e5c3 2815
8f215dce
JJ
2816 /* t1 == t can happen for boolean nodes which are always unsigned. */
2817 if (t1 != t)
2818 return get_alias_set (t1);
2819 }
3bdf5ad1 2820 else if (POINTER_TYPE_P (t))
02af3af6 2821 {
3bdf5ad1 2822 tree t1;
02af3af6
MS
2823
2824 /* Unfortunately, there is no canonical form of a pointer type.
2825 In particular, if we have `typedef int I', then `int *', and
2826 `I *' are different types. So, we have to pick a canonical
2827 representative. We do this below.
d125d268 2828
b61148dd
MM
2829 Technically, this approach is actually more conservative that
2830 it needs to be. In particular, `const int *' and `int *'
2831 chould be in different alias sets, according to the C and C++
2832 standard, since their types are not the same, and so,
2833 technically, an `int **' and `const int **' cannot point at
2834 the same thing.
2835
2836 But, the standard is wrong. In particular, this code is
2837 legal C++:
2838
2839 int *ip;
2840 int **ipp = &ip;
2841 const int* const* cipp = &ip;
2842
2843 And, it doesn't make sense for that to be legal unless you
2844 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
2845 the pointed-to types. This issue has been reported to the
2846 C++ committee. */
12e1243e 2847 t1 = build_type_no_quals (t);
3bdf5ad1
RK
2848 if (t1 != t)
2849 return get_alias_set (t1);
02af3af6 2850 }
5b2abab8
MM
2851 /* It's not yet safe to use alias sets for classes in C++ because
2852 the TYPE_FIELDs list for a class doesn't mention base classes. */
f6f8291a 2853 else if (c_language == clk_cplusplus && AGGREGATE_TYPE_P (t))
5b2abab8 2854 return 0;
ece32014 2855
3bdf5ad1 2856 return -1;
41472af8 2857}
7f4edbcb
BS
2858
2859/* Build tree nodes and builtin functions common to both C and C++ language
6bcedb4e 2860 frontends. */
3bdf5ad1 2861
7f4edbcb 2862void
6bcedb4e 2863c_common_nodes_and_builtins ()
7f4edbcb 2864{
eaa7c03f
JM
2865 int wchar_type_size;
2866 tree array_domain_type;
7f4edbcb
BS
2867 tree temp;
2868 tree memcpy_ftype, memset_ftype, strlen_ftype;
ad3fd36f 2869 tree bzero_ftype, bcmp_ftype, puts_ftype, printf_ftype;
18f988a0 2870 tree fputs_ftype, fputc_ftype, fwrite_ftype, fprintf_ftype;
7f4edbcb 2871 tree endlink, int_endlink, double_endlink, unsigned_endlink;
60291474 2872 tree cstring_endlink, sizetype_endlink;
7f4edbcb 2873 tree ptr_ftype, ptr_ftype_unsigned;
c2e42aab 2874 tree void_ftype_any, void_ftype_int, int_ftype_any;
7f4edbcb
BS
2875 tree double_ftype_double, double_ftype_double_double;
2876 tree float_ftype_float, ldouble_ftype_ldouble;
341e3d11
JM
2877 tree cfloat_ftype_cfloat, cdouble_ftype_cdouble, cldouble_ftype_cldouble;
2878 tree float_ftype_cfloat, double_ftype_cdouble, ldouble_ftype_cldouble;
d118937d 2879 tree int_ftype_cptr_cptr_sizet, sizet_ftype_cstring_cstring;
60291474
KG
2880 tree int_ftype_cstring_cstring, string_ftype_string_cstring;
2881 tree string_ftype_cstring_int, string_ftype_cstring_cstring;
da9e9f08 2882 tree string_ftype_string_cstring_sizet, int_ftype_cstring_cstring_sizet;
7f4edbcb 2883 tree long_ftype_long;
1c44c45d 2884 tree longlong_ftype_longlong;
e78f4a97 2885 tree intmax_ftype_intmax;
7f4edbcb
BS
2886 /* Either char* or void*. */
2887 tree traditional_ptr_type_node;
4b2a62db
KG
2888 /* Either const char* or const void*. */
2889 tree traditional_cptr_type_node;
2890 tree traditional_len_type_node;
2891 tree traditional_len_endlink;
9f720c3e 2892 tree va_list_ref_type_node;
daf68dd7 2893 tree va_list_arg_type_node;
d3707adb 2894
eaa7c03f
JM
2895 /* Define `int' and `char' first so that dbx will output them first. */
2896 record_builtin_type (RID_INT, NULL_PTR, integer_type_node);
2897 record_builtin_type (RID_CHAR, "char", char_type_node);
2898
2899 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
2900 "unsigned long", "long long unsigned" and "unsigned short" were in C++
2901 but not C. Are the conditionals here needed? */
2902 if (c_language == clk_cplusplus)
2903 record_builtin_type (RID_SIGNED, NULL_PTR, integer_type_node);
2904 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
2905 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
2906 record_builtin_type (RID_MAX, "long unsigned int",
2907 long_unsigned_type_node);
2908 if (c_language == clk_cplusplus)
2909 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
2910 record_builtin_type (RID_MAX, "long long int",
2911 long_long_integer_type_node);
2912 record_builtin_type (RID_MAX, "long long unsigned int",
2913 long_long_unsigned_type_node);
2914 if (c_language == clk_cplusplus)
2915 record_builtin_type (RID_MAX, "long long unsigned",
2916 long_long_unsigned_type_node);
2917 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
2918 record_builtin_type (RID_MAX, "short unsigned int",
2919 short_unsigned_type_node);
2920 if (c_language == clk_cplusplus)
2921 record_builtin_type (RID_MAX, "unsigned short",
2922 short_unsigned_type_node);
2923
2924 /* Define both `signed char' and `unsigned char'. */
2925 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
2926 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
2927
2928 /* These are types that type_for_size and type_for_mode use. */
2929 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intQI_type_node));
2930 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intHI_type_node));
2931 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intSI_type_node));
2932 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intDI_type_node));
2933#if HOST_BITS_PER_WIDE_INT >= 64
2934 pushdecl (build_decl (TYPE_DECL, get_identifier ("__int128_t"), intTI_type_node));
2935#endif
2936 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intQI_type_node));
2937 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intHI_type_node));
2938 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intSI_type_node));
2939 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intDI_type_node));
2940#if HOST_BITS_PER_WIDE_INT >= 64
2941 pushdecl (build_decl (TYPE_DECL, get_identifier ("__uint128_t"), unsigned_intTI_type_node));
2942#endif
2943
2944 /* Create the widest literal types. */
2945 widest_integer_literal_type_node
2946 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
2947 pushdecl (build_decl (TYPE_DECL, NULL_TREE,
2948 widest_integer_literal_type_node));
2949
2950 widest_unsigned_literal_type_node
2951 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
2952 pushdecl (build_decl (TYPE_DECL, NULL_TREE,
2953 widest_unsigned_literal_type_node));
2954
2955 /* `unsigned long' is the standard type for sizeof.
2956 Note that stddef.h uses `unsigned long',
2957 and this must agree, even if long and int are the same size. */
2958 c_size_type_node =
2959 TREE_TYPE (identifier_global_value (get_identifier (SIZE_TYPE)));
2960 signed_size_type_node = signed_type (c_size_type_node);
2961 if (flag_traditional)
2962 c_size_type_node = signed_size_type_node;
2963 set_sizetype (c_size_type_node);
2964
2965 build_common_tree_nodes_2 (flag_short_double);
2966
2967 record_builtin_type (RID_FLOAT, NULL_PTR, float_type_node);
2968 record_builtin_type (RID_DOUBLE, NULL_PTR, double_type_node);
2969 record_builtin_type (RID_MAX, "long double", long_double_type_node);
2970
2971 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex int"),
2972 complex_integer_type_node));
2973 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex float"),
2974 complex_float_type_node));
2975 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex double"),
2976 complex_double_type_node));
2977 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex long double"),
2978 complex_long_double_type_node));
2979
2980 record_builtin_type (RID_VOID, NULL_PTR, void_type_node);
2981
2982 void_list_node = build_void_list_node ();
2983
2984 /* Make a type to be the domain of a few array types
2985 whose domains don't really matter.
2986 200 is small enough that it always fits in size_t
2987 and large enough that it can hold most function names for the
2988 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
2989 array_domain_type = build_index_type (size_int (200));
2990
2991 /* Make a type for arrays of characters.
2992 With luck nothing will ever really depend on the length of this
2993 array type. */
2994 char_array_type_node
2995 = build_array_type (char_type_node, array_domain_type);
2996
2997 /* Likewise for arrays of ints. */
2998 int_array_type_node
2999 = build_array_type (integer_type_node, array_domain_type);
3000
3001#ifdef MD_INIT_BUILTINS
3002 MD_INIT_BUILTINS;
3003#endif
3004
3005 /* This is special for C++ so functions can be overloaded. */
3006 wchar_type_node = get_identifier (flag_short_wchar
3007 ? "short unsigned int"
3008 : WCHAR_TYPE);
3009 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
3010 wchar_type_size = TYPE_PRECISION (wchar_type_node);
3011 if (c_language == clk_cplusplus)
3012 {
3013 if (TREE_UNSIGNED (wchar_type_node))
3014 wchar_type_node = make_unsigned_type (wchar_type_size);
3015 else
3016 wchar_type_node = make_signed_type (wchar_type_size);
3017 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
3018 }
3019 else
3020 {
3021 signed_wchar_type_node = signed_type (wchar_type_node);
3022 unsigned_wchar_type_node = unsigned_type (wchar_type_node);
3023 }
3024
3025 /* This is for wide string constants. */
3026 wchar_array_type_node
3027 = build_array_type (wchar_type_node, array_domain_type);
3028
5fd8e536
JM
3029 string_type_node = build_pointer_type (char_type_node);
3030 const_string_type_node
3031 = build_pointer_type (build_type_variant (char_type_node, 1, 0));
3032
3033 wint_type_node =
3034 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
3035
3036 intmax_type_node =
3037 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
3038 uintmax_type_node =
3039 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
3040
3041 default_function_type = build_function_type (integer_type_node, NULL_TREE);
3042 ptrdiff_type_node
3043 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
3044 unsigned_ptrdiff_type_node = unsigned_type (ptrdiff_type_node);
3045
d3707adb
RH
3046 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
3047 va_list_type_node));
daf68dd7 3048
29ae8f10
GRK
3049 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"),
3050 ptrdiff_type_node));
3051
3052 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"),
3053 sizetype));
3054
daf68dd7 3055 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
9f720c3e
GK
3056 {
3057 va_list_arg_type_node = va_list_ref_type_node =
3058 build_pointer_type (TREE_TYPE (va_list_type_node));
3059 }
daf68dd7 3060 else
9f720c3e
GK
3061 {
3062 va_list_arg_type_node = va_list_type_node;
3063 va_list_ref_type_node = build_reference_type (va_list_type_node);
3064 }
3065
7f4edbcb
BS
3066 endlink = void_list_node;
3067 int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
3068 double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
3069 unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
60291474 3070 cstring_endlink = tree_cons (NULL_TREE, const_string_type_node, endlink);
7f4edbcb
BS
3071
3072 ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
3073 ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
21318741 3074 sizetype_endlink = tree_cons (NULL_TREE, TYPE_DOMAIN (sizetype), endlink);
7f4edbcb
BS
3075 /* We realloc here because sizetype could be int or unsigned. S'ok. */
3076 ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
3077
4b2a62db 3078 int_ftype_any = build_function_type (integer_type_node, NULL_TREE);
7f4edbcb
BS
3079 void_ftype_any = build_function_type (void_type_node, NULL_TREE);
3080 void_ftype = build_function_type (void_type_node, endlink);
3081 void_ftype_int = build_function_type (void_type_node, int_endlink);
3082 void_ftype_ptr
3083 = build_function_type (void_type_node,
3084 tree_cons (NULL_TREE, ptr_type_node, endlink));
3085
3086 float_ftype_float
3087 = build_function_type (float_type_node,
3088 tree_cons (NULL_TREE, float_type_node, endlink));
3089
3090 double_ftype_double
3091 = build_function_type (double_type_node, double_endlink);
3092
3093 ldouble_ftype_ldouble
3094 = build_function_type (long_double_type_node,
3095 tree_cons (NULL_TREE, long_double_type_node,
3096 endlink));
3097
3098 double_ftype_double_double
3099 = build_function_type (double_type_node,
3100 tree_cons (NULL_TREE, double_type_node,
3101 double_endlink));
3102
341e3d11
JM
3103 cfloat_ftype_cfloat
3104 = build_function_type (complex_float_type_node,
3105 tree_cons (NULL_TREE, complex_float_type_node,
3106 endlink));
3107 cdouble_ftype_cdouble
3108 = build_function_type (complex_double_type_node,
3109 tree_cons (NULL_TREE, complex_double_type_node,
3110 endlink));
3111 cldouble_ftype_cldouble
3112 = build_function_type (complex_long_double_type_node,
3113 tree_cons (NULL_TREE, complex_long_double_type_node,
3114 endlink));
3115
3116 float_ftype_cfloat
3117 = build_function_type (float_type_node,
3118 tree_cons (NULL_TREE, complex_float_type_node,
3119 endlink));
3120 double_ftype_cdouble
3121 = build_function_type (double_type_node,
3122 tree_cons (NULL_TREE, complex_double_type_node,
3123 endlink));
3124 ldouble_ftype_cldouble
3125 = build_function_type (long_double_type_node,
3126 tree_cons (NULL_TREE, complex_long_double_type_node,
3127 endlink));
3128
7f4edbcb
BS
3129 int_ftype_int
3130 = build_function_type (integer_type_node, int_endlink);
3131
3132 long_ftype_long
3133 = build_function_type (long_integer_type_node,
3134 tree_cons (NULL_TREE, long_integer_type_node,
3135 endlink));
3136
1c44c45d
JM
3137 longlong_ftype_longlong
3138 = build_function_type (long_long_integer_type_node,
3139 tree_cons (NULL_TREE, long_long_integer_type_node,
3140 endlink));
3141
e78f4a97
JM
3142 intmax_ftype_intmax
3143 = build_function_type (intmax_type_node,
3144 tree_cons (NULL_TREE, intmax_type_node,
3145 endlink));
3146
7f4edbcb
BS
3147 int_ftype_cptr_cptr_sizet
3148 = build_function_type (integer_type_node,
3149 tree_cons (NULL_TREE, const_ptr_type_node,
60291474
KG
3150 tree_cons (NULL_TREE,
3151 const_ptr_type_node,
3152 sizetype_endlink)));
7f4edbcb 3153
ae499cce
MM
3154 void_zero_node = build_int_2 (0, 0);
3155 TREE_TYPE (void_zero_node) = void_type_node;
3156
d118937d
KG
3157 /* Prototype for strcpy/strcat. */
3158 string_ftype_string_cstring
7f4edbcb
BS
3159 = build_function_type (string_type_node,
3160 tree_cons (NULL_TREE, string_type_node,
60291474 3161 cstring_endlink));
7f4edbcb 3162
d118937d 3163 /* Prototype for strncpy/strncat. */
da9e9f08
KG
3164 string_ftype_string_cstring_sizet
3165 = build_function_type (string_type_node,
3166 tree_cons (NULL_TREE, string_type_node,
3167 tree_cons (NULL_TREE,
3168 const_string_type_node,
3169 sizetype_endlink)));
3170
6bcedb4e
MM
3171 traditional_len_type_node = ((flag_traditional &&
3172 c_language != clk_cplusplus)
4b2a62db
KG
3173 ? integer_type_node : sizetype);
3174 traditional_len_endlink = tree_cons (NULL_TREE, traditional_len_type_node,
3175 endlink);
3176
7f4edbcb 3177 /* Prototype for strcmp. */
60291474 3178 int_ftype_cstring_cstring
7f4edbcb
BS
3179 = build_function_type (integer_type_node,
3180 tree_cons (NULL_TREE, const_string_type_node,
60291474 3181 cstring_endlink));
7f4edbcb 3182
d118937d
KG
3183 /* Prototype for strspn/strcspn. */
3184 sizet_ftype_cstring_cstring
3185 = build_function_type (c_size_type_node,
3186 tree_cons (NULL_TREE, const_string_type_node,
60291474 3187 cstring_endlink));
d118937d 3188
da9e9f08
KG
3189 /* Prototype for strncmp. */
3190 int_ftype_cstring_cstring_sizet
3191 = build_function_type (integer_type_node,
3192 tree_cons (NULL_TREE, const_string_type_node,
3193 tree_cons (NULL_TREE,
3194 const_string_type_node,
3195 sizetype_endlink)));
3196
26a87cab 3197 /* Prototype for strstr, strpbrk, etc. */
60291474 3198 string_ftype_cstring_cstring
78e7629e
KG
3199 = build_function_type (string_type_node,
3200 tree_cons (NULL_TREE, const_string_type_node,
60291474 3201 cstring_endlink));
78e7629e
KG
3202
3203 /* Prototype for strchr. */
60291474 3204 string_ftype_cstring_int
78e7629e
KG
3205 = build_function_type (string_type_node,
3206 tree_cons (NULL_TREE, const_string_type_node,
60291474 3207 int_endlink));
78e7629e 3208
7f4edbcb
BS
3209 /* Prototype for strlen. */
3210 strlen_ftype
60291474 3211 = build_function_type (traditional_len_type_node, cstring_endlink);
7f4edbcb 3212
6bcedb4e
MM
3213 traditional_ptr_type_node = ((flag_traditional &&
3214 c_language != clk_cplusplus)
7f4edbcb 3215 ? string_type_node : ptr_type_node);
6bcedb4e
MM
3216 traditional_cptr_type_node = ((flag_traditional &&
3217 c_language != clk_cplusplus)
4b2a62db 3218 ? const_string_type_node : const_ptr_type_node);
7f4edbcb
BS
3219
3220 /* Prototype for memcpy. */
3221 memcpy_ftype
3222 = build_function_type (traditional_ptr_type_node,
3223 tree_cons (NULL_TREE, ptr_type_node,
3224 tree_cons (NULL_TREE, const_ptr_type_node,
3225 sizetype_endlink)));
3226
3227 /* Prototype for memset. */
3228 memset_ftype
3229 = build_function_type (traditional_ptr_type_node,
3230 tree_cons (NULL_TREE, ptr_type_node,
3231 tree_cons (NULL_TREE, integer_type_node,
60291474 3232 sizetype_endlink)));
7f4edbcb 3233
e3a709be
KG
3234 /* Prototype for bzero. */
3235 bzero_ftype
3236 = build_function_type (void_type_node,
3237 tree_cons (NULL_TREE, traditional_ptr_type_node,
3238 traditional_len_endlink));
3239
4b2a62db
KG
3240 /* Prototype for bcmp. */
3241 bcmp_ftype
3242 = build_function_type (integer_type_node,
3243 tree_cons (NULL_TREE, traditional_cptr_type_node,
3244 tree_cons (NULL_TREE,
3245 traditional_cptr_type_node,
3246 traditional_len_endlink)));
3247
ad3fd36f
KG
3248 /* Prototype for puts. */
3249 puts_ftype
60291474 3250 = build_function_type (integer_type_node, cstring_endlink);
ad3fd36f
KG
3251
3252 /* Prototype for printf. */
3253 printf_ftype
3254 = build_function_type (integer_type_node,
3255 tree_cons (NULL_TREE, const_string_type_node,
3256 NULL_TREE));
3257
c2e42aab
KG
3258 /* These stdio prototypes are declared using void* in place of
3259 FILE*. They are only used for __builtin_ style calls, regular
3260 style builtin prototypes omit the arguments and merge those
3261 provided by stdio.h. */
3262 /* Prototype for fwrite. */
3263 fwrite_ftype
3264 = build_function_type (c_size_type_node,
3265 tree_cons (NULL_TREE, const_ptr_type_node,
3266 tree_cons (NULL_TREE, c_size_type_node,
3267 tree_cons (NULL_TREE, c_size_type_node,
3268 tree_cons (NULL_TREE, ptr_type_node, endlink)))));
3269
3270 /* Prototype for fputc. */
3271 fputc_ftype
3272 = build_function_type (integer_type_node,
3273 tree_cons (NULL_TREE, integer_type_node,
3274 tree_cons (NULL_TREE, ptr_type_node, endlink)));
3275
3276 /* Prototype for fputs. */
3277 fputs_ftype
3278 = build_function_type (integer_type_node,
3279 tree_cons (NULL_TREE, const_string_type_node,
3280 tree_cons (NULL_TREE, ptr_type_node, endlink)));
3281
18f988a0
KG
3282 /* Prototype for fprintf. */
3283 fprintf_ftype
3284 = build_function_type (integer_type_node,
3285 tree_cons (NULL_TREE, ptr_type_node,
3286 tree_cons (NULL_TREE,
3287 const_string_type_node,
3288 NULL_TREE)));
3289
7f4edbcb 3290 builtin_function ("__builtin_constant_p", default_function_type,
26db82d8 3291 BUILT_IN_CONSTANT_P, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
3292
3293 builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
26db82d8 3294 BUILT_IN_RETURN_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
3295
3296 builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
26db82d8 3297 BUILT_IN_FRAME_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3298
52a11cbf
RH
3299#ifdef EH_RETURN_DATA_REGNO
3300 builtin_function ("__builtin_eh_return_data_regno", int_ftype_int,
3301 BUILT_IN_EH_RETURN_DATA_REGNO, BUILT_IN_NORMAL, NULL_PTR);
3302#endif
3303
7f4edbcb 3304 builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
26db82d8 3305 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, "alloca");
fc2aaf30
JM
3306 builtin_function_2 ("__builtin_ffs", "ffs",
3307 int_ftype_int, int_ftype_int,
3308 BUILT_IN_FFS, BUILT_IN_NORMAL, 0, 1, 0);
3309 /* Define alloca as builtin, unless SMALL_STACK. */
512b62fb 3310#ifndef SMALL_STACK
fc2aaf30
JM
3311 builtin_function_2 (NULL_PTR, "alloca", NULL_TREE, ptr_ftype_sizetype,
3312 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, 0, 1, 0);
512b62fb 3313#endif
796cdb65 3314 /* Declare _exit and _Exit just to mark them as non-returning. */
fc2aaf30
JM
3315 builtin_function_2 (NULL_PTR, "_exit", NULL_TREE, void_ftype_int,
3316 0, NOT_BUILT_IN, 0, 1, 1);
796cdb65
JM
3317 builtin_function_2 (NULL_PTR, "_Exit", NULL_TREE, void_ftype_int,
3318 0, NOT_BUILT_IN, 0, !flag_isoc99, 1);
fc2aaf30
JM
3319
3320 builtin_function_2 ("__builtin_index", "index",
3321 string_ftype_cstring_int, string_ftype_cstring_int,
3322 BUILT_IN_INDEX, BUILT_IN_NORMAL, 1, 1, 0);
3323 builtin_function_2 ("__builtin_rindex", "rindex",
3324 string_ftype_cstring_int, string_ftype_cstring_int,
3325 BUILT_IN_RINDEX, BUILT_IN_NORMAL, 1, 1, 0);
3326
3327 /* The system prototypes for these functions have many
3328 variations, so don't specify parameters to avoid conflicts.
3329 The expand_* functions check the argument types anyway. */
3330 builtin_function_2 ("__builtin_bzero", "bzero",
3331 bzero_ftype, void_ftype_any,
3332 BUILT_IN_BZERO, BUILT_IN_NORMAL, 1, 1, 0);
3333 builtin_function_2 ("__builtin_bcmp", "bcmp",
3334 bcmp_ftype, int_ftype_any,
3335 BUILT_IN_BCMP, BUILT_IN_NORMAL, 1, 1, 0);
3336
3337 builtin_function_2 ("__builtin_abs", "abs",
3338 int_ftype_int, int_ftype_int,
3339 BUILT_IN_ABS, BUILT_IN_NORMAL, 0, 0, 0);
3340 builtin_function_2 ("__builtin_fabsf", "fabsf",
3341 float_ftype_float, float_ftype_float,
3342 BUILT_IN_FABS, BUILT_IN_NORMAL, 0, 0, 0);
3343 builtin_function_2 ("__builtin_fabs", "fabs",
3344 double_ftype_double, double_ftype_double,
3345 BUILT_IN_FABS, BUILT_IN_NORMAL, 0, 0, 0);
3346 builtin_function_2 ("__builtin_fabsl", "fabsl",
3347 ldouble_ftype_ldouble, ldouble_ftype_ldouble,
3348 BUILT_IN_FABS, BUILT_IN_NORMAL, 0, 0, 0);
3349 builtin_function_2 ("__builtin_labs", "labs",
3350 long_ftype_long, long_ftype_long,
29b5ca41 3351 BUILT_IN_ABS, BUILT_IN_NORMAL, 0, 0, 0);
fc2aaf30
JM
3352 builtin_function_2 ("__builtin_llabs", "llabs",
3353 longlong_ftype_longlong, longlong_ftype_longlong,
29b5ca41 3354 BUILT_IN_ABS, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
fc2aaf30
JM
3355 builtin_function_2 ("__builtin_imaxabs", "imaxabs",
3356 intmax_ftype_intmax, intmax_ftype_intmax,
29b5ca41 3357 BUILT_IN_ABS, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
7f4edbcb 3358
7f4edbcb 3359 builtin_function ("__builtin_saveregs", ptr_ftype, BUILT_IN_SAVEREGS,
26db82d8 3360 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3361 builtin_function ("__builtin_classify_type", default_function_type,
26db82d8 3362 BUILT_IN_CLASSIFY_TYPE, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3363 builtin_function ("__builtin_next_arg", ptr_ftype, BUILT_IN_NEXT_ARG,
26db82d8 3364 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3365 builtin_function ("__builtin_args_info", int_ftype_int, BUILT_IN_ARGS_INFO,
26db82d8 3366 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
3367 builtin_function ("__builtin_setjmp",
3368 build_function_type (integer_type_node,
3369 tree_cons (NULL_TREE, ptr_type_node,
3370 endlink)),
26db82d8 3371 BUILT_IN_SETJMP, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
3372 builtin_function ("__builtin_longjmp",
3373 build_function_type (void_type_node,
3374 tree_cons (NULL_TREE, ptr_type_node,
60291474 3375 int_endlink)),
26db82d8
BS
3376 BUILT_IN_LONGJMP, BUILT_IN_NORMAL, NULL_PTR);
3377 builtin_function ("__builtin_trap", void_ftype, BUILT_IN_TRAP,
3378 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3379
1eb8759b
RH
3380 /* ISO C99 IEEE Unordered compares. */
3381 builtin_function ("__builtin_isgreater", default_function_type,
3382 BUILT_IN_ISGREATER, BUILT_IN_NORMAL, NULL_PTR);
3383 builtin_function ("__builtin_isgreaterequal", default_function_type,
3384 BUILT_IN_ISGREATEREQUAL, BUILT_IN_NORMAL, NULL_PTR);
3385 builtin_function ("__builtin_isless", default_function_type,
3386 BUILT_IN_ISLESS, BUILT_IN_NORMAL, NULL_PTR);
3387 builtin_function ("__builtin_islessequal", default_function_type,
3388 BUILT_IN_ISLESSEQUAL, BUILT_IN_NORMAL, NULL_PTR);
3389 builtin_function ("__builtin_islessgreater", default_function_type,
3390 BUILT_IN_ISLESSGREATER, BUILT_IN_NORMAL, NULL_PTR);
3391 builtin_function ("__builtin_isunordered", default_function_type,
3392 BUILT_IN_ISUNORDERED, BUILT_IN_NORMAL, NULL_PTR);
3393
7f4edbcb
BS
3394 /* Untyped call and return. */
3395 builtin_function ("__builtin_apply_args", ptr_ftype,
26db82d8 3396 BUILT_IN_APPLY_ARGS, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb
BS
3397
3398 temp = tree_cons (NULL_TREE,
3399 build_pointer_type (build_function_type (void_type_node,
3400 NULL_TREE)),
60291474 3401 tree_cons (NULL_TREE, ptr_type_node, sizetype_endlink));
7f4edbcb
BS
3402 builtin_function ("__builtin_apply",
3403 build_function_type (ptr_type_node, temp),
26db82d8 3404 BUILT_IN_APPLY, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3405 builtin_function ("__builtin_return", void_ftype_ptr,
26db82d8 3406 BUILT_IN_RETURN, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3407
d3707adb
RH
3408 /* Support for varargs.h and stdarg.h. */
3409 builtin_function ("__builtin_varargs_start",
3410 build_function_type (void_type_node,
3411 tree_cons (NULL_TREE,
9f720c3e 3412 va_list_ref_type_node,
d3707adb 3413 endlink)),
26db82d8 3414 BUILT_IN_VARARGS_START, BUILT_IN_NORMAL, NULL_PTR);
d3707adb
RH
3415
3416 builtin_function ("__builtin_stdarg_start",
3417 build_function_type (void_type_node,
3418 tree_cons (NULL_TREE,
9f720c3e 3419 va_list_ref_type_node,
d3707adb 3420 NULL_TREE)),
26db82d8 3421 BUILT_IN_STDARG_START, BUILT_IN_NORMAL, NULL_PTR);
d3707adb
RH
3422
3423 builtin_function ("__builtin_va_end",
3424 build_function_type (void_type_node,
3425 tree_cons (NULL_TREE,
9f720c3e 3426 va_list_ref_type_node,
d3707adb 3427 endlink)),
26db82d8 3428 BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL_PTR);
d3707adb
RH
3429
3430 builtin_function ("__builtin_va_copy",
3431 build_function_type (void_type_node,
3432 tree_cons (NULL_TREE,
9f720c3e 3433 va_list_ref_type_node,
d3707adb 3434 tree_cons (NULL_TREE,
daf68dd7 3435 va_list_arg_type_node,
d3707adb 3436 endlink))),
26db82d8 3437 BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL_PTR);
d3707adb 3438
994a57cd
RH
3439 /* ??? Ought to be `T __builtin_expect(T, T)' for any type T. */
3440 builtin_function ("__builtin_expect",
3441 build_function_type (long_integer_type_node,
3442 tree_cons (NULL_TREE,
3443 long_integer_type_node,
3444 tree_cons (NULL_TREE,
3445 long_integer_type_node,
3446 endlink))),
3447 BUILT_IN_EXPECT, BUILT_IN_NORMAL, NULL_PTR);
3448
7f4edbcb 3449 /* Currently under experimentation. */
fc2aaf30
JM
3450 builtin_function_2 ("__builtin_memcpy", "memcpy",
3451 memcpy_ftype, memcpy_ftype,
3452 BUILT_IN_MEMCPY, BUILT_IN_NORMAL, 1, 0, 0);
3453 builtin_function_2 ("__builtin_memcmp", "memcmp",
3454 int_ftype_cptr_cptr_sizet, int_ftype_cptr_cptr_sizet,
3455 BUILT_IN_MEMCMP, BUILT_IN_NORMAL, 1, 0, 0);
3456 builtin_function_2 ("__builtin_memset", "memset",
3457 memset_ftype, memset_ftype,
3458 BUILT_IN_MEMSET, BUILT_IN_NORMAL, 1, 0, 0);
da9e9f08 3459 built_in_decls[BUILT_IN_STRCMP] =
fc2aaf30
JM
3460 builtin_function_2 ("__builtin_strcmp", "strcmp",
3461 int_ftype_cstring_cstring, int_ftype_cstring_cstring,
3462 BUILT_IN_STRCMP, BUILT_IN_NORMAL, 1, 0, 0);
3463 builtin_function_2 ("__builtin_strncmp", "strncmp",
3464 int_ftype_cstring_cstring_sizet,
3465 int_ftype_cstring_cstring_sizet,
3466 BUILT_IN_STRNCMP, BUILT_IN_NORMAL, 1, 0, 0);
3467 builtin_function_2 ("__builtin_strstr", "strstr",
3468 string_ftype_cstring_cstring, string_ftype_cstring_cstring,
3469 BUILT_IN_STRSTR, BUILT_IN_NORMAL, 1, 0, 0);
3470 builtin_function_2 ("__builtin_strpbrk", "strpbrk",
3471 string_ftype_cstring_cstring, string_ftype_cstring_cstring,
3472 BUILT_IN_STRPBRK, BUILT_IN_NORMAL, 1, 0, 0);
78e7629e 3473 built_in_decls[BUILT_IN_STRCHR] =
fc2aaf30
JM
3474 builtin_function_2 ("__builtin_strchr", "strchr",
3475 string_ftype_cstring_int, string_ftype_cstring_int,
3476 BUILT_IN_STRCHR, BUILT_IN_NORMAL, 1, 0, 0);
3477 builtin_function_2 ("__builtin_strrchr", "strrchr",
3478 string_ftype_cstring_int, string_ftype_cstring_int,
3479 BUILT_IN_STRRCHR, BUILT_IN_NORMAL, 1, 0, 0);
3480 builtin_function_2 ("__builtin_strcpy", "strcpy",
3481 string_ftype_string_cstring, string_ftype_string_cstring,
3482 BUILT_IN_STRCPY, BUILT_IN_NORMAL, 1, 0, 0);
3483 builtin_function_2 ("__builtin_strncpy", "strncpy",
3484 string_ftype_string_cstring_sizet,
3485 string_ftype_string_cstring_sizet,
3486 BUILT_IN_STRNCPY, BUILT_IN_NORMAL, 1, 0, 0);
d118937d 3487 built_in_decls[BUILT_IN_STRCAT] =
fc2aaf30
JM
3488 builtin_function_2 ("__builtin_strcat", "strcat",
3489 string_ftype_string_cstring,
3490 string_ftype_string_cstring,
3491 BUILT_IN_STRCAT, BUILT_IN_NORMAL, 1, 0, 0);
3492 builtin_function_2 ("__builtin_strncat", "strncat",
3493 string_ftype_string_cstring_sizet,
3494 string_ftype_string_cstring_sizet,
3495 BUILT_IN_STRNCAT, BUILT_IN_NORMAL, 1, 0, 0);
3496 builtin_function_2 ("__builtin_strspn", "strspn",
3497 sizet_ftype_cstring_cstring, sizet_ftype_cstring_cstring,
3498 BUILT_IN_STRSPN, BUILT_IN_NORMAL, 1, 0, 0);
3499 builtin_function_2 ("__builtin_strcspn", "strcspn",
3500 sizet_ftype_cstring_cstring, sizet_ftype_cstring_cstring,
3501 BUILT_IN_STRCSPN, BUILT_IN_NORMAL, 1, 0, 0);
d118937d 3502 built_in_decls[BUILT_IN_STRLEN] =
fc2aaf30
JM
3503 builtin_function_2 ("__builtin_strlen", "strlen",
3504 strlen_ftype, strlen_ftype,
3505 BUILT_IN_STRLEN, BUILT_IN_NORMAL, 1, 0, 0);
3506
3507 builtin_function_2 ("__builtin_sqrtf", "sqrtf",
3508 float_ftype_float, float_ftype_float,
3509 BUILT_IN_FSQRT, BUILT_IN_NORMAL, 1, 0, 0);
3510 builtin_function_2 ("__builtin_fsqrt", "sqrt",
3511 double_ftype_double, double_ftype_double,
3512 BUILT_IN_FSQRT, BUILT_IN_NORMAL, 1, 0, 0);
3513 builtin_function_2 ("__builtin_sqrtl", "sqrtl",
3514 ldouble_ftype_ldouble, ldouble_ftype_ldouble,
3515 BUILT_IN_FSQRT, BUILT_IN_NORMAL, 1, 0, 0);
3516 builtin_function_2 ("__builtin_sinf", "sinf",
3517 float_ftype_float, float_ftype_float,
3518 BUILT_IN_SIN, BUILT_IN_NORMAL, 1, 0, 0);
3519 builtin_function_2 ("__builtin_sin", "sin",
3520 double_ftype_double, double_ftype_double,
3521 BUILT_IN_SIN, BUILT_IN_NORMAL, 1, 0, 0);
3522 builtin_function_2 ("__builtin_sinl", "sinl",
3523 ldouble_ftype_ldouble, ldouble_ftype_ldouble,
3524 BUILT_IN_SIN, BUILT_IN_NORMAL, 1, 0, 0);
3525 builtin_function_2 ("__builtin_cosf", "cosf",
3526 float_ftype_float, float_ftype_float,
3527 BUILT_IN_COS, BUILT_IN_NORMAL, 1, 0, 0);
3528 builtin_function_2 ("__builtin_cos", "cos",
3529 double_ftype_double, double_ftype_double,
3530 BUILT_IN_COS, BUILT_IN_NORMAL, 1, 0, 0);
3531 builtin_function_2 ("__builtin_cosl", "cosl",
3532 ldouble_ftype_ldouble, ldouble_ftype_ldouble,
3533 BUILT_IN_COS, BUILT_IN_NORMAL, 1, 0, 0);
341e3d11
JM
3534
3535 /* ISO C99 complex arithmetic functions. */
3536 builtin_function_2 ("__builtin_conjf", "conjf",
3537 cfloat_ftype_cfloat, cfloat_ftype_cfloat,
3538 BUILT_IN_CONJ, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3539 builtin_function_2 ("__builtin_conj", "conj",
3540 cdouble_ftype_cdouble, cdouble_ftype_cdouble,
3541 BUILT_IN_CONJ, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3542 builtin_function_2 ("__builtin_conjl", "conjl",
3543 cldouble_ftype_cldouble, cldouble_ftype_cldouble,
3544 BUILT_IN_CONJ, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3545 builtin_function_2 ("__builtin_crealf", "crealf",
3546 float_ftype_cfloat, float_ftype_cfloat,
3547 BUILT_IN_CREAL, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3548 builtin_function_2 ("__builtin_creal", "creal",
3549 double_ftype_cdouble, double_ftype_cdouble,
3550 BUILT_IN_CREAL, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3551 builtin_function_2 ("__builtin_creall", "creall",
3552 ldouble_ftype_cldouble, ldouble_ftype_cldouble,
3553 BUILT_IN_CREAL, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3554 builtin_function_2 ("__builtin_cimagf", "cimagf",
3555 float_ftype_cfloat, float_ftype_cfloat,
3556 BUILT_IN_CIMAG, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3557 builtin_function_2 ("__builtin_cimag", "cimag",
3558 double_ftype_cdouble, double_ftype_cdouble,
3559 BUILT_IN_CIMAG, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3560 builtin_function_2 ("__builtin_cimagl", "cimagl",
3561 ldouble_ftype_cldouble, ldouble_ftype_cldouble,
3562 BUILT_IN_CIMAG, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3563
ad3fd36f
KG
3564 built_in_decls[BUILT_IN_PUTCHAR] =
3565 builtin_function ("__builtin_putchar", int_ftype_int,
3566 BUILT_IN_PUTCHAR, BUILT_IN_NORMAL, "putchar");
3567 built_in_decls[BUILT_IN_PUTS] =
3568 builtin_function ("__builtin_puts", puts_ftype,
3569 BUILT_IN_PUTS, BUILT_IN_NORMAL, "puts");
fc2aaf30
JM
3570 builtin_function_2 ("__builtin_printf", "printf",
3571 printf_ftype, printf_ftype,
3572 BUILT_IN_PRINTF, BUILT_IN_FRONTEND, 1, 0, 0);
18f988a0
KG
3573 builtin_function_2 ("__builtin_fprintf", "fprintf",
3574 fprintf_ftype, fprintf_ftype,
3575 BUILT_IN_FPRINTF, BUILT_IN_FRONTEND, 1, 0, 0);
07328167 3576 built_in_decls[BUILT_IN_FWRITE] =
c2e42aab 3577 builtin_function ("__builtin_fwrite", fwrite_ftype,
07328167 3578 BUILT_IN_FWRITE, BUILT_IN_NORMAL, "fwrite");
3ff5f682 3579 built_in_decls[BUILT_IN_FPUTC] =
c2e42aab
KG
3580 builtin_function ("__builtin_fputc", fputc_ftype,
3581 BUILT_IN_FPUTC, BUILT_IN_NORMAL, "fputc");
3582 /* Declare the __builtin_ style with arguments and the regular style
3583 without them. We rely on stdio.h to supply the arguments for the
3584 regular style declaration since we had to use void* instead of
3585 FILE* in the __builtin_ prototype supplied here. */
3ff5f682 3586 built_in_decls[BUILT_IN_FPUTS] =
fc2aaf30 3587 builtin_function_2 ("__builtin_fputs", "fputs",
c2e42aab 3588 fputs_ftype, int_ftype_any,
fc2aaf30
JM
3589 BUILT_IN_FPUTS, BUILT_IN_NORMAL, 1, 0, 0);
3590
3591 /* Declare these functions non-returning
3592 to avoid spurious "control drops through" warnings. */
3593 builtin_function_2 (NULL_PTR, "abort",
3594 NULL_TREE, ((c_language == clk_cplusplus)
3595 ? void_ftype : void_ftype_any),
3596 0, NOT_BUILT_IN, 0, 0, 1);
3597
3598 builtin_function_2 (NULL_PTR, "exit",
3599 NULL_TREE, ((c_language == clk_cplusplus)
3600 ? void_ftype_int : void_ftype_any),
3601 0, NOT_BUILT_IN, 0, 0, 1);
7f4edbcb
BS
3602
3603#if 0
3604 /* Support for these has not been written in either expand_builtin
3605 or build_function_call. */
26db82d8
BS
3606 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV,
3607 BUILT_IN_NORMAL, NULL_PTR);
3608 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV,
3609 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3610 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
26db82d8 3611 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3612 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
26db82d8 3613 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3614 builtin_function ("__builtin_fmod", double_ftype_double_double,
26db82d8 3615 BUILT_IN_FMOD, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3616 builtin_function ("__builtin_frem", double_ftype_double_double,
26db82d8 3617 BUILT_IN_FREM, BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3618 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
26db82d8 3619 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3620 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
26db82d8 3621 BUILT_IN_NORMAL, NULL_PTR);
7f4edbcb 3622#endif
c530479e 3623
5b47282c
GM
3624 main_identifier_node = get_identifier ("main");
3625
c530479e
RH
3626 /* ??? Perhaps there's a better place to do this. But it is related
3627 to __builtin_va_arg, so it isn't that off-the-wall. */
3628 lang_type_promotes_to = simple_type_promotes_to;
7f4edbcb 3629}
d3707adb
RH
3630
3631tree
3632build_va_arg (expr, type)
3633 tree expr, type;
3634{
3635 return build1 (VA_ARG_EXPR, type, expr);
3636}
fc2aaf30
JM
3637
3638
3639/* Possibly define a builtin function with one or two names. BUILTIN_NAME
3640 is an __builtin_-prefixed name; NAME is the ordinary name; one or both
3641 of these may be NULL (though both being NULL is useless).
3642 BUILTIN_TYPE is the type of the __builtin_-prefixed function;
3643 TYPE is the type of the function with the ordinary name. These
3644 may differ if the ordinary name is declared with a looser type to avoid
3645 conflicts with headers. FUNCTION_CODE and CLASS are as for
3646 builtin_function. If LIBRARY_NAME_P is nonzero, NAME is passed as
3647 the LIBRARY_NAME parameter to builtin_function when declaring BUILTIN_NAME.
3648 If NONANSI_P is nonzero, the name NAME is treated as a non-ANSI name; if
3649 NORETURN_P is nonzero, the function is marked as non-returning.
3650 Returns the declaration of BUILTIN_NAME, if any, otherwise
3651 the declaration of NAME. Does not declare NAME if flag_no_builtin,
3652 or if NONANSI_P and flag_no_nonansi_builtin. */
3653
3654static tree
3655builtin_function_2 (builtin_name, name, builtin_type, type, function_code,
3656 class, library_name_p, nonansi_p, noreturn_p)
3657 const char *builtin_name;
3658 const char *name;
3659 tree builtin_type;
3660 tree type;
3661 int function_code;
3662 enum built_in_class class;
3663 int library_name_p;
3664 int nonansi_p;
3665 int noreturn_p;
3666{
3667 tree bdecl = NULL_TREE;
3668 tree decl = NULL_TREE;
3669 if (builtin_name != 0)
3670 {
3671 bdecl = builtin_function (builtin_name, builtin_type, function_code,
3672 class, library_name_p ? name : NULL_PTR);
3673 if (noreturn_p)
3674 {
3675 TREE_THIS_VOLATILE (bdecl) = 1;
3676 TREE_SIDE_EFFECTS (bdecl) = 1;
3677 }
3678 }
3679 if (name != 0 && !flag_no_builtin && !(nonansi_p && flag_no_nonansi_builtin))
3680 {
3681 decl = builtin_function (name, type, function_code, class, NULL_PTR);
3682 if (nonansi_p)
3683 DECL_BUILT_IN_NONANSI (decl) = 1;
3684 if (noreturn_p)
3685 {
3686 TREE_THIS_VOLATILE (decl) = 1;
3687 TREE_SIDE_EFFECTS (decl) = 1;
3688 }
3689 }
3690 return (bdecl != 0 ? bdecl : decl);
3691}
c530479e
RH
3692\f
3693/* Given a type, apply default promotions wrt unnamed function arguments
3694 and return the new type. Return NULL_TREE if no change. */
d125d268 3695/* ??? There is a function of the same name in the C++ front end that
c530479e 3696 does something similar, but is more thorough and does not return NULL
d125d268 3697 if no change. We could perhaps share code, but it would make the
c530479e
RH
3698 self_promoting_type property harder to identify. */
3699
3700tree
3701simple_type_promotes_to (type)
3702 tree type;
3703{
3704 if (TYPE_MAIN_VARIANT (type) == float_type_node)
3705 return double_type_node;
3706
3707 if (C_PROMOTING_INTEGER_TYPE_P (type))
3708 {
3709 /* Traditionally, unsignedness is preserved in default promotions.
3710 Also preserve unsignedness if not really getting any wider. */
3711 if (TREE_UNSIGNED (type)
3712 && (flag_traditional
3713 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
3714 return unsigned_type_node;
3715 return integer_type_node;
3716 }
3717
3718 return NULL_TREE;
3719}
3720
3721/* Return 1 if PARMS specifies a fixed number of parameters
3722 and none of their types is affected by default promotions. */
3723
3724int
3725self_promoting_args_p (parms)
3726 tree parms;
3727{
3728 register tree t;
3729 for (t = parms; t; t = TREE_CHAIN (t))
3730 {
3731 register tree type = TREE_VALUE (t);
7e8176d7 3732
c530479e
RH
3733 if (TREE_CHAIN (t) == 0 && type != void_type_node)
3734 return 0;
3735
3736 if (type == 0)
3737 return 0;
3738
3739 if (TYPE_MAIN_VARIANT (type) == float_type_node)
3740 return 0;
3741
3742 if (C_PROMOTING_INTEGER_TYPE_P (type))
3743 return 0;
3744 }
3745 return 1;
3746}
5eda3d66 3747
0a7394bc
MM
3748/* Recursively examines the array elements of TYPE, until a non-array
3749 element type is found. */
3750
3751tree
3752strip_array_types (type)
3753 tree type;
3754{
3755 while (TREE_CODE (type) == ARRAY_TYPE)
3756 type = TREE_TYPE (type);
3757
3758 return type;
3759}
3760
5eda3d66
RH
3761/* Recognize certain built-in functions so we can make tree-codes
3762 other than CALL_EXPR. We do this when it enables fold-const.c
3763 to do something useful. */
3764/* ??? By rights this should go in builtins.c, but only C and C++
3765 implement build_{binary,unary}_op. Not exactly sure what bits
3766 of functionality are actually needed from those functions, or
3767 where the similar functionality exists in the other front ends. */
3768
3769tree
3770expand_tree_builtin (function, params, coerced_params)
3771 tree function, params, coerced_params;
3772{
3773 enum tree_code code;
3774
3775 if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL)
3776 return NULL_TREE;
3777
3778 switch (DECL_FUNCTION_CODE (function))
3779 {
3780 case BUILT_IN_ABS:
5eda3d66
RH
3781 case BUILT_IN_FABS:
3782 if (coerced_params == 0)
3783 return integer_zero_node;
3784 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
3785
341e3d11
JM
3786 case BUILT_IN_CONJ:
3787 if (coerced_params == 0)
3788 return integer_zero_node;
3789 return build_unary_op (CONJ_EXPR, TREE_VALUE (coerced_params), 0);
3790
3791 case BUILT_IN_CREAL:
3792 if (coerced_params == 0)
3793 return integer_zero_node;
3794 return build_unary_op (REALPART_EXPR, TREE_VALUE (coerced_params), 0);
3795
3796 case BUILT_IN_CIMAG:
3797 if (coerced_params == 0)
3798 return integer_zero_node;
3799 return build_unary_op (IMAGPART_EXPR, TREE_VALUE (coerced_params), 0);
3800
5eda3d66
RH
3801 case BUILT_IN_ISGREATER:
3802 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3803 code = UNLE_EXPR;
3804 else
3805 code = LE_EXPR;
3806 goto unordered_cmp;
3807
3808 case BUILT_IN_ISGREATEREQUAL:
3809 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3810 code = UNLT_EXPR;
3811 else
3812 code = LT_EXPR;
3813 goto unordered_cmp;
3814
3815 case BUILT_IN_ISLESS:
3816 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3817 code = UNGE_EXPR;
3818 else
3819 code = GE_EXPR;
3820 goto unordered_cmp;
3821
3822 case BUILT_IN_ISLESSEQUAL:
3823 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3824 code = UNGT_EXPR;
3825 else
3826 code = GT_EXPR;
3827 goto unordered_cmp;
3828
3829 case BUILT_IN_ISLESSGREATER:
3830 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3831 code = UNEQ_EXPR;
3832 else
3833 code = EQ_EXPR;
3834 goto unordered_cmp;
3835
3836 case BUILT_IN_ISUNORDERED:
3837 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
3838 return integer_zero_node;
3839 code = UNORDERED_EXPR;
3840 goto unordered_cmp;
3841
3842 unordered_cmp:
3843 {
3844 tree arg0, arg1;
3845
3846 if (params == 0
3847 || TREE_CHAIN (params) == 0)
3848 {
3849 error ("too few arguments to function `%s'",
3850 IDENTIFIER_POINTER (DECL_NAME (function)));
3851 return error_mark_node;
3852 }
3853 else if (TREE_CHAIN (TREE_CHAIN (params)) != 0)
3854 {
3855 error ("too many arguments to function `%s'",
3856 IDENTIFIER_POINTER (DECL_NAME (function)));
3857 return error_mark_node;
3858 }
3859
3860 arg0 = TREE_VALUE (params);
3861 arg1 = TREE_VALUE (TREE_CHAIN (params));
3862 arg0 = build_binary_op (code, arg0, arg1, 0);
3863 if (code != UNORDERED_EXPR)
3864 arg0 = build_unary_op (TRUTH_NOT_EXPR, arg0, 0);
3865 return arg0;
3866 }
3867 break;
3868
3869 default:
3870 break;
3871 }
3872
3873 return NULL_TREE;
3874}
c7d87c0a 3875
ae499cce
MM
3876/* Returns non-zero if CODE is the code for a statement. */
3877
3878int
3879statement_code_p (code)
3880 enum tree_code code;
3881{
3882 switch (code)
3883 {
3884 case EXPR_STMT:
3885 case COMPOUND_STMT:
3886 case DECL_STMT:
3887 case IF_STMT:
3888 case FOR_STMT:
3889 case WHILE_STMT:
3890 case DO_STMT:
3891 case RETURN_STMT:
3892 case BREAK_STMT:
3893 case CONTINUE_STMT:
8f17b5c5 3894 case SCOPE_STMT:
ae499cce
MM
3895 case SWITCH_STMT:
3896 case GOTO_STMT:
3897 case LABEL_STMT:
3898 case ASM_STMT:
3899 case CASE_LABEL:
3900 return 1;
3901
3902 default:
3903 if (lang_statement_code_p)
3904 return (*lang_statement_code_p) (code);
3905 return 0;
3906 }
3907}
3908
86306c8e 3909/* Walk the statement tree, rooted at *tp. Apply FUNC to all the
ae499cce
MM
3910 sub-trees of *TP in a pre-order traversal. FUNC is called with the
3911 DATA and the address of each sub-tree. If FUNC returns a non-NULL
3912 value, the traversal is aborted, and the value returned by FUNC is
3913 returned. If FUNC sets WALK_SUBTREES to zero, then the subtrees of
3914 the node being visited are not walked.
3915
3916 We don't need a without_duplicates variant of this one because the
3917 statement tree is a tree, not a graph. */
3918
3919tree
3920walk_stmt_tree (tp, func, data)
3921 tree *tp;
3922 walk_tree_fn func;
3923 void *data;
3924{
3925 enum tree_code code;
3926 int walk_subtrees;
3927 tree result;
3928 int i, len;
3929
3930#define WALK_SUBTREE(NODE) \
3931 do \
3932 { \
3933 result = walk_stmt_tree (&(NODE), func, data); \
3934 if (result) \
3935 return result; \
3936 } \
3937 while (0)
3938
3939 /* Skip empty subtrees. */
3940 if (!*tp)
3941 return NULL_TREE;
3942
3943 /* Skip subtrees below non-statement nodes. */
3944 if (!statement_code_p (TREE_CODE (*tp)))
3945 return NULL_TREE;
3946
3947 /* Call the function. */
3948 walk_subtrees = 1;
3949 result = (*func) (tp, &walk_subtrees, data);
3950
3951 /* If we found something, return it. */
3952 if (result)
3953 return result;
3954
ae499cce
MM
3955 /* FUNC may have modified the tree, recheck that we're looking at a
3956 statement node. */
3957 code = TREE_CODE (*tp);
3958 if (!statement_code_p (code))
3959 return NULL_TREE;
3960
87aee676
DN
3961 /* Visit the subtrees unless FUNC decided that there was nothing
3962 interesting below this point in the tree. */
3963 if (walk_subtrees)
3964 {
3965 /* Walk over all the sub-trees of this operand. Statement nodes
3966 never contain RTL, and we needn't worry about TARGET_EXPRs. */
3967 len = TREE_CODE_LENGTH (code);
3968
3969 /* Go through the subtrees. We need to do this in forward order so
3970 that the scope of a FOR_EXPR is handled properly. */
3971 for (i = 0; i < len; ++i)
3972 WALK_SUBTREE (TREE_OPERAND (*tp, i));
3973 }
ae499cce
MM
3974
3975 /* Finally visit the chain. This can be tail-recursion optimized if
3976 we write it this way. */
3977 return walk_stmt_tree (&TREE_CHAIN (*tp), func, data);
3978
3979#undef WALK_SUBTREE
3980}
3981
8f17b5c5
MM
3982/* Used to compare case labels. K1 and K2 are actually tree nodes
3983 representing case labels, or NULL_TREE for a `default' label.
3984 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
3985 K2, and 0 if K1 and K2 are equal. */
3986
3987int
3988case_compare (k1, k2)
3989 splay_tree_key k1;
3990 splay_tree_key k2;
3991{
3992 /* Consider a NULL key (such as arises with a `default' label) to be
3993 smaller than anything else. */
3994 if (!k1)
3995 return k2 ? -1 : 0;
3996 else if (!k2)
3997 return k1 ? 1 : 0;
3998
3999 return tree_int_cst_compare ((tree) k1, (tree) k2);
4000}
4001
4002/* Process a case label for the range LOW_VALUE ... HIGH_VALUE. If
4003 LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is
4004 actually a `default' label. If only HIGH_VALUE is NULL_TREE, then
4005 case label was declared using the usual C/C++ syntax, rather than
4006 the GNU case range extension. CASES is a tree containing all the
4007 case ranges processed so far; COND is the condition for the
4008 switch-statement itself. Returns the CASE_LABEL created, or
4009 ERROR_MARK_NODE if no CASE_LABEL is created. */
4010
4011tree
4012c_add_case_label (cases, cond, low_value, high_value)
4013 splay_tree cases;
4014 tree cond;
4015 tree low_value;
4016 tree high_value;
4017{
4018 tree type;
4019 tree label;
4020 tree case_label;
4021 splay_tree_node node;
4022
4023 /* Create the LABEL_DECL itself. */
4024 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
4025 DECL_CONTEXT (label) = current_function_decl;
4026
4027 /* If there was an error processing the switch condition, bail now
4028 before we get more confused. */
4029 if (!cond || cond == error_mark_node)
4030 {
4031 /* Add a label anyhow so that the back-end doesn't think that
4032 the beginning of the switch is unreachable. */
4033 if (!cases->root)
4034 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
4035 return error_mark_node;
4036 }
4037
4038 if ((low_value && TREE_TYPE (low_value)
4039 && POINTER_TYPE_P (TREE_TYPE (low_value)))
4040 || (high_value && TREE_TYPE (high_value)
4041 && POINTER_TYPE_P (TREE_TYPE (high_value))))
4042 error ("pointers are not permitted as case values");
4043
4044 /* Case ranges are a GNU extension. */
4045 if (high_value && pedantic)
4046 {
4047 if (c_language == clk_cplusplus)
4048 pedwarn ("ISO C++ forbids range expressions in switch statements");
4049 else
4050 pedwarn ("ISO C forbids range expressions in switch statements");
4051 }
4052
4053 type = TREE_TYPE (cond);
4054 if (low_value)
4055 {
4056 low_value = check_case_value (low_value);
4057 low_value = convert_and_check (type, low_value);
4058 }
4059 if (high_value)
4060 {
4061 high_value = check_case_value (high_value);
4062 high_value = convert_and_check (type, high_value);
4063 }
4064
4065 /* If an error has occurred, bail out now. */
4066 if (low_value == error_mark_node || high_value == error_mark_node)
4067 {
4068 if (!cases->root)
4069 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
4070 return error_mark_node;
4071 }
4072
4073 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
4074 really a case range, even though it was written that way. Remove
4075 the HIGH_VALUE to simplify later processing. */
4076 if (tree_int_cst_equal (low_value, high_value))
4077 high_value = NULL_TREE;
4078 if (low_value && high_value
4079 && !tree_int_cst_lt (low_value, high_value))
4080 warning ("empty range specified");
4081
4082 /* Look up the LOW_VALUE in the table of case labels we already
4083 have. */
4084 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
4085 /* If there was not an exact match, check for overlapping ranges.
4086 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
4087 that's a `default' label and the only overlap is an exact match. */
4088 if (!node && (low_value || high_value))
4089 {
4090 splay_tree_node low_bound;
4091 splay_tree_node high_bound;
4092
4093 /* Even though there wasn't an exact match, there might be an
4094 overlap between this case range and another case range.
4095 Since we've (inductively) not allowed any overlapping case
4096 ranges, we simply need to find the greatest low case label
4097 that is smaller that LOW_VALUE, and the smallest low case
4098 label that is greater than LOW_VALUE. If there is an overlap
4099 it will occur in one of these two ranges. */
4100 low_bound = splay_tree_predecessor (cases,
4101 (splay_tree_key) low_value);
4102 high_bound = splay_tree_successor (cases,
4103 (splay_tree_key) low_value);
4104
4105 /* Check to see if the LOW_BOUND overlaps. It is smaller than
4106 the LOW_VALUE, so there is no need to check unless the
4107 LOW_BOUND is in fact itself a case range. */
4108 if (low_bound
4109 && CASE_HIGH ((tree) low_bound->value)
4110 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
4111 low_value) >= 0)
4112 node = low_bound;
4113 /* Check to see if the HIGH_BOUND overlaps. The low end of that
4114 range is bigger than the low end of the current range, so we
4115 are only interested if the current range is a real range, and
4116 not an ordinary case label. */
4117 else if (high_bound
4118 && high_value
4119 && (tree_int_cst_compare ((tree) high_bound->key,
4120 high_value)
4121 <= 0))
4122 node = high_bound;
4123 }
4124 /* If there was an overlap, issue an error. */
4125 if (node)
4126 {
4127 tree duplicate = CASE_LABEL_DECL ((tree) node->value);
4128
4129 if (high_value)
4130 {
4131 error ("duplicate (or overlapping) case value");
4132 error_with_decl (duplicate,
4133 "this is the first entry overlapping that value");
4134 }
4135 else if (low_value)
4136 {
4137 error ("duplicate case value") ;
4138 error_with_decl (duplicate, "previously used here");
4139 }
4140 else
4141 {
4142 error ("multiple default labels in one switch");
4143 error_with_decl (duplicate, "this is the first default label");
4144 }
4145 if (!cases->root)
4146 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
4147 }
4148
4149 /* Add a CASE_LABEL to the statement-tree. */
4150 case_label = add_stmt (build_case_label (low_value, high_value, label));
4151 /* Register this case label in the splay tree. */
4152 splay_tree_insert (cases,
4153 (splay_tree_key) low_value,
4154 (splay_tree_value) case_label);
4155
4156 return case_label;
4157}
4158
4159/* Mark P (a stmt_tree) for GC. The use of a `void *' for the
4160 parameter allows this function to be used as a GC-marking
4161 function. */
4162
4163void
4164mark_stmt_tree (p)
4165 void *p;
4166{
4167 stmt_tree st = (stmt_tree) p;
4168
4169 ggc_mark_tree (st->x_last_stmt);
4170 ggc_mark_tree (st->x_last_expr_type);
4171}
4172
4173/* Mark LD for GC. */
4174
4175void
4176c_mark_lang_decl (c)
4177 struct c_lang_decl *c;
4178{
4179 ggc_mark_tree (c->saved_tree);
4180}
4181
4182/* Mark F for GC. */
4183
4184void
4185mark_c_language_function (f)
4186 struct language_function *f;
4187{
4188 if (!f)
4189 return;
4190
4191 mark_stmt_tree (&f->x_stmt_tree);
4192 ggc_mark_tree (f->x_scope_stmt_stack);
4193}
4194
4195/* Hook used by expand_expr to expand language-specific tree codes. */
4196
4197rtx
4198c_expand_expr (exp, target, tmode, modifier)
4199 tree exp;
4200 rtx target;
4201 enum machine_mode tmode;
4202 enum expand_modifier modifier;
4203{
4204 switch (TREE_CODE (exp))
4205 {
4206 case STMT_EXPR:
4207 {
4208 tree rtl_expr;
4209 rtx result;
4210
4211 /* Since expand_expr_stmt calls free_temp_slots after every
4212 expression statement, we must call push_temp_slots here.
4213 Otherwise, any temporaries in use now would be considered
4214 out-of-scope after the first EXPR_STMT from within the
4215 STMT_EXPR. */
4216 push_temp_slots ();
4217 rtl_expr = expand_start_stmt_expr ();
4218 expand_stmt (STMT_EXPR_STMT (exp));
4219 expand_end_stmt_expr (rtl_expr);
4220 result = expand_expr (rtl_expr, target, tmode, modifier);
4221 pop_temp_slots ();
4222 return result;
4223 }
4224 break;
4225
c70eaeaf
KG
4226 case CALL_EXPR:
4227 {
4228 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
4229 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4230 == FUNCTION_DECL)
4231 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4232 && (DECL_BUILT_IN_CLASS (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4233 == BUILT_IN_FRONTEND))
4234 return c_expand_builtin (exp, target, tmode, modifier);
4235 else
4236 abort();
4237 }
4238 break;
4239
8f17b5c5
MM
4240 default:
4241 abort ();
4242 }
4243
4244 abort ();
4245 return NULL;
4246}
4247
4248/* Hook used by safe_from_p to handle language-specific tree codes. */
4249
4250int
4251c_safe_from_p (target, exp)
4252 rtx target;
4253 tree exp;
4254{
4255 /* We can see statements here when processing the body of a
4256 statement-expression. For a declaration statement declaring a
4257 variable, look at the variable's initializer. */
4258 if (TREE_CODE (exp) == DECL_STMT)
4259 {
4260 tree decl = DECL_STMT_DECL (exp);
4261
4262 if (TREE_CODE (decl) == VAR_DECL
4263 && DECL_INITIAL (decl)
4264 && !safe_from_p (target, DECL_INITIAL (decl), /*top_p=*/0))
4265 return 0;
4266 }
4267
4268 /* For any statement, we must follow the statement-chain. */
4269 if (statement_code_p (TREE_CODE (exp)) && TREE_CHAIN (exp))
4270 return safe_from_p (target, TREE_CHAIN (exp), /*top_p=*/0);
4271
4272 /* Assume everything else is safe. */
4273 return 1;
4274}
4275
3fe30ff6
RH
4276/* Hook used by unsafe_for_reeval to handle language-specific tree codes. */
4277
4278int
4279c_unsafe_for_reeval (exp)
4280 tree exp;
4281{
4282 /* Statement expressions may not be reevaluated. */
4283 if (TREE_CODE (exp) == STMT_EXPR)
4284 return 2;
4285
4286 /* Walk all other expressions. */
4287 return -1;
4288}
4289
c7d87c0a
BC
4290/* Tree code classes. */
4291
4292#define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
4293
4294static char c_tree_code_type[] = {
4295 'x',
4296#include "c-common.def"
4297};
4298#undef DEFTREECODE
4299
4300/* Table indexed by tree code giving number of expression
4301 operands beyond the fixed part of the node structure.
4302 Not used for types or decls. */
4303
4304#define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
4305
4306static int c_tree_code_length[] = {
4307 0,
4308#include "c-common.def"
4309};
4310#undef DEFTREECODE
4311
4312/* Names of tree components.
4313 Used for printing out the tree and error messages. */
4314#define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
4315
4316static const char *c_tree_code_name[] = {
4317 "@@dummy",
4318#include "c-common.def"
4319};
4320#undef DEFTREECODE
4321
4322/* Adds the tree codes specific to the C front end to the list of all
4323 tree codes. */
4324
4325void
5538e30f 4326add_c_tree_codes ()
c7d87c0a
BC
4327{
4328 memcpy (tree_code_type + (int) LAST_AND_UNUSED_TREE_CODE,
4329 c_tree_code_type,
4330 (int)LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE);
4331 memcpy (tree_code_length + (int) LAST_AND_UNUSED_TREE_CODE,
4332 c_tree_code_length,
4333 (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (int));
4334 memcpy (tree_code_name + (int) LAST_AND_UNUSED_TREE_CODE,
4335 c_tree_code_name,
4336 (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (char *));
3fe30ff6 4337 lang_unsafe_for_reeval = c_unsafe_for_reeval;
c7d87c0a 4338}
c70eaeaf
KG
4339
4340#define CALLED_AS_BUILT_IN(NODE) \
4341 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
4342
4343static rtx
4344c_expand_builtin (exp, target, tmode, modifier)
4345 tree exp;
4346 rtx target;
4347 enum machine_mode tmode;
4348 enum expand_modifier modifier;
4349{
4350 tree type = TREE_TYPE (exp);
4351 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4352 tree arglist = TREE_OPERAND (exp, 1);
4353 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
4354 enum tree_code code = TREE_CODE (exp);
4355 const int ignore = (target == const0_rtx
4356 || ((code == NON_LVALUE_EXPR || code == NOP_EXPR
4357 || code == CONVERT_EXPR || code == REFERENCE_EXPR
4358 || code == COND_EXPR)
4359 && TREE_CODE (type) == VOID_TYPE));
4360
4361 if (! optimize && ! CALLED_AS_BUILT_IN (fndecl))
4362 return expand_call (exp, target, ignore);
4363
4364 switch (fcode)
4365 {
4366 case BUILT_IN_PRINTF:
4367 target = c_expand_builtin_printf (arglist, target, tmode,
4368 modifier, ignore);
4369 if (target)
4370 return target;
4371 break;
4372
18f988a0
KG
4373 case BUILT_IN_FPRINTF:
4374 target = c_expand_builtin_fprintf (arglist, target, tmode,
4375 modifier, ignore);
4376 if (target)
4377 return target;
4378 break;
4379
c70eaeaf
KG
4380 default: /* just do library call, if unknown builtin */
4381 error ("built-in function `%s' not currently supported",
4382 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
4383 }
4384
4385 /* The switch statement above can drop through to cause the function
4386 to be called normally. */
4387 return expand_call (exp, target, ignore);
4388}
4389
4390/* Check an arglist to *printf for problems. The arglist should start
4391 at the format specifier, with the remaining arguments immediately
4392 following it. */
4393static int
4394is_valid_printf_arglist (arglist)
4395 tree arglist;
4396{
4397 /* Save this value so we can restore it later. */
4398 const int SAVE_pedantic = pedantic;
4399 int diagnostic_occurred = 0;
4400
4401 /* Set this to a known value so the user setting won't affect code
4402 generation. */
4403 pedantic = 1;
4404 /* Check to make sure there are no format specifier errors. */
4405 check_function_format (&diagnostic_occurred,
4406 maybe_get_identifier("printf"),
4407 NULL_TREE, arglist);
4408
4409 /* Restore the value of `pedantic'. */
4410 pedantic = SAVE_pedantic;
4411
4412 /* If calling `check_function_format_ptr' produces a warning, we
4413 return false, otherwise we return true. */
4414 return ! diagnostic_occurred;
4415}
4416
4417/* If the arguments passed to printf are suitable for optimizations,
4418 we attempt to transform the call. */
4419static rtx
4420c_expand_builtin_printf (arglist, target, tmode, modifier, ignore)
4421 tree arglist;
4422 rtx target;
4423 enum machine_mode tmode;
4424 enum expand_modifier modifier;
4425 int ignore;
4426{
4427 tree fn_putchar = built_in_decls[BUILT_IN_PUTCHAR],
4428 fn_puts = built_in_decls[BUILT_IN_PUTS];
4429 tree fn, format_arg, stripped_string;
4430
4431 /* If the return value is used, or the replacement _DECL isn't
4432 initialized, don't do the transformation. */
4433 if (!ignore || !fn_putchar || !fn_puts)
4434 return 0;
4435
4436 /* Verify the required arguments in the original call. */
4437 if (arglist == 0
4438 || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE))
4439 return 0;
4440
4441 /* Check the specifier vs. the parameters. */
4442 if (!is_valid_printf_arglist (arglist))
4443 return 0;
4444
4445 format_arg = TREE_VALUE (arglist);
4446 stripped_string = format_arg;
4447 STRIP_NOPS (stripped_string);
4448 if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
4449 stripped_string = TREE_OPERAND (stripped_string, 0);
4450
4451 /* If the format specifier isn't a STRING_CST, punt. */
4452 if (TREE_CODE (stripped_string) != STRING_CST)
4453 return 0;
4454
4455 /* OK! We can attempt optimization. */
4456
4457 /* If the format specifier was "%s\n", call __builtin_puts(arg2). */
4458 if (strcmp (TREE_STRING_POINTER (stripped_string), "%s\n") == 0)
4459 {
4460 arglist = TREE_CHAIN (arglist);
4461 fn = fn_puts;
4462 }
4463 /* If the format specifier was "%c", call __builtin_putchar (arg2). */
4464 else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0)
4465 {
4466 arglist = TREE_CHAIN (arglist);
4467 fn = fn_putchar;
4468 }
4469 else
4470 {
4471 /* We can't handle anything else with % args or %% ... yet. */
4472 if (strchr (TREE_STRING_POINTER (stripped_string), '%'))
4473 return 0;
4474
4475 /* If the resulting constant string has a length of 1, call
4476 putchar. Note, TREE_STRING_LENGTH includes the terminating
4477 NULL in its count. */
4478 if (TREE_STRING_LENGTH (stripped_string) == 2)
4479 {
4480 /* Given printf("c"), (where c is any one character,)
4481 convert "c"[0] to an int and pass that to the replacement
4482 function. */
4483 arglist = build_int_2 (TREE_STRING_POINTER (stripped_string)[0], 0);
4484 arglist = build_tree_list (NULL_TREE, arglist);
4485
4486 fn = fn_putchar;
4487 }
4488 /* If the resulting constant was "string\n", call
4489 __builtin_puts("string"). Ensure "string" has at least one
4490 character besides the trailing \n. Note, TREE_STRING_LENGTH
4491 includes the terminating NULL in its count. */
4492 else if (TREE_STRING_LENGTH (stripped_string) > 2
4493 && TREE_STRING_POINTER (stripped_string)
4494 [TREE_STRING_LENGTH (stripped_string) - 2] == '\n')
4495 {
4496 /* Create a NULL-terminated string that's one char shorter
4497 than the original, stripping off the trailing '\n'. */
4498 const int newlen = TREE_STRING_LENGTH (stripped_string) - 1;
4499 char *newstr = (char *) alloca (newlen);
4500 memcpy (newstr, TREE_STRING_POINTER (stripped_string), newlen - 1);
4501 newstr[newlen - 1] = 0;
4502
1092710d 4503 arglist = combine_strings (build_string (newlen, newstr));
c70eaeaf
KG
4504 arglist = build_tree_list (NULL_TREE, arglist);
4505 fn = fn_puts;
4506 }
4507 else
4508 /* We'd like to arrange to call fputs(string) here, but we
4509 need stdout and don't have a way to get it ... yet. */
4510 return 0;
4511 }
4512
4513 return expand_expr (build_function_call (fn, arglist),
4514 (ignore ? const0_rtx : target),
4515 tmode, modifier);
4516}
18f988a0
KG
4517
4518/* If the arguments passed to fprintf are suitable for optimizations,
4519 we attempt to transform the call. */
4520static rtx
4521c_expand_builtin_fprintf (arglist, target, tmode, modifier, ignore)
4522 tree arglist;
4523 rtx target;
4524 enum machine_mode tmode;
4525 enum expand_modifier modifier;
4526 int ignore;
4527{
4528 tree fn_fputc = built_in_decls[BUILT_IN_FPUTC],
4529 fn_fputs = built_in_decls[BUILT_IN_FPUTS];
4530 tree fn, format_arg, stripped_string;
4531
4532 /* If the return value is used, or the replacement _DECL isn't
4533 initialized, don't do the transformation. */
4534 if (!ignore || !fn_fputc || !fn_fputs)
4535 return 0;
4536
4537 /* Verify the required arguments in the original call. */
4538 if (arglist == 0
4539 || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE)
4540 || (TREE_CHAIN (arglist) == 0)
4541 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist)))) !=
4542 POINTER_TYPE))
4543 return 0;
4544
4545 /* Check the specifier vs. the parameters. */
4546 if (!is_valid_printf_arglist (TREE_CHAIN (arglist)))
4547 return 0;
4548
4549 format_arg = TREE_VALUE (TREE_CHAIN (arglist));
4550 stripped_string = format_arg;
4551 STRIP_NOPS (stripped_string);
4552 if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
4553 stripped_string = TREE_OPERAND (stripped_string, 0);
4554
4555 /* If the format specifier isn't a STRING_CST, punt. */
4556 if (TREE_CODE (stripped_string) != STRING_CST)
4557 return 0;
4558
4559 /* OK! We can attempt optimization. */
4560
4561 /* If the format specifier was "%s", call __builtin_fputs(arg3, arg1). */
4562 if (strcmp (TREE_STRING_POINTER (stripped_string), "%s") == 0)
4563 {
4564 tree newarglist = build_tree_list (NULL_TREE, TREE_VALUE (arglist));
4565 arglist = tree_cons (NULL_TREE,
4566 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))),
4567 newarglist);
4568 fn = fn_fputs;
4569 }
4570 /* If the format specifier was "%c", call __builtin_fputc (arg3, arg1). */
4571 else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0)
4572 {
4573 tree newarglist = build_tree_list (NULL_TREE, TREE_VALUE (arglist));
4574 arglist = tree_cons (NULL_TREE,
4575 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))),
4576 newarglist);
4577 fn = fn_fputc;
4578 }
4579 else
4580 {
4581 /* We can't handle anything else with % args or %% ... yet. */
4582 if (strchr (TREE_STRING_POINTER (stripped_string), '%'))
4583 return 0;
4584
4585 /* When "string" doesn't contain %, replace all cases of
4586 fprintf(stream,string) with fputs(string,stream). The fputs
4587 builtin will take take of special cases like length==1. */
4588 arglist = tree_cons (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)),
4589 build_tree_list (NULL_TREE, TREE_VALUE (arglist)));
4590 fn = fn_fputs;
4591 }
4592
4593 return expand_expr (build_function_call (fn, arglist),
4594 (ignore ? const0_rtx : target),
4595 tmode, modifier);
4596}
19552aa5
JM
4597\f
4598
4599/* Given a boolean expression ARG, return a tree representing an increment
4600 or decrement (as indicated by CODE) of ARG. The front end must check for
4601 invalid cases (e.g., decrement in C++). */
4602tree
4603boolean_increment (code, arg)
4604 enum tree_code code;
4605 tree arg;
4606{
4607 tree val;
4608 tree true_res = (c_language == clk_cplusplus
4609 ? boolean_true_node
4610 : c_bool_true_node);
4611 arg = stabilize_reference (arg);
4612 switch (code)
4613 {
4614 case PREINCREMENT_EXPR:
4615 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4616 break;
4617 case POSTINCREMENT_EXPR:
4618 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4619 arg = save_expr (arg);
4620 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4621 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4622 break;
4623 case PREDECREMENT_EXPR:
4624 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
4625 break;
4626 case POSTDECREMENT_EXPR:
4627 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
4628 arg = save_expr (arg);
4629 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4630 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4631 break;
4632 default:
4633 abort ();
4634 }
4635 TREE_SIDE_EFFECTS (val) = 1;
4636 return val;
4637}
03dc0325
JM
4638\f
4639
4640/* Do the parts of lang_init common to C and C++. */
4641void
4642c_common_lang_init ()
4643{
4644 /* If still "unspecified", make it match -fbounded-pointers. */
4645 if (flag_bounds_check < 0)
4646 flag_bounds_check = flag_bounded_pointers;
4647
4648 /* Special format checking options don't work without -Wformat; warn if
4649 they are used. */
4650 if (warn_format_y2k && !warn_format)
4651 warning ("-Wformat-y2k ignored without -Wformat");
4652 if (warn_format_extra_args && !warn_format)
4653 warning ("-Wformat-extra-args ignored without -Wformat");
4654 if (warn_format_nonliteral && !warn_format)
4655 warning ("-Wformat-nonliteral ignored without -Wformat");
4656 if (warn_format_security && !warn_format)
4657 warning ("-Wformat-security ignored without -Wformat");
4658 if (warn_missing_format_attribute && !warn_format)
4659 warning ("-Wmissing-format-attribute ignored without -Wformat");
4660}