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