]> gcc.gnu.org Git - gcc.git/blame - gcc/c-common.c
Daily bump.
[gcc.git] / gcc / c-common.c
CommitLineData
b30f223b 1/* Subroutines shared by all languages that are variants of C.
1574ef13
AO
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002 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"
4d6baafa 33#include "tree-inline.h"
22703ccc 34#include "diagnostic.h"
7bdb32b9 35#include "tm_p.h"
235cfbc4 36#include "obstack.h"
c8724862 37#include "cpplib.h"
12a68f1f 38#include "target.h"
7afff7cf 39#include "langhooks.h"
12a41c22 40#include "except.h" /* For USING_SJLJ_EXCEPTIONS. */
81a75f0f
NB
41
42cpp_reader *parse_in; /* Declared in c-pragma.h. */
c8724862 43
eaa7c03f
JM
44/* We let tm.h override the types used here, to handle trivial differences
45 such as the choice of unsigned int or long unsigned int for size_t.
46 When machines start needing nontrivial differences in the size type,
47 it would be best to do something here to figure out automatically
48 from other information what type to use. */
49
50#ifndef SIZE_TYPE
51#define SIZE_TYPE "long unsigned int"
52#endif
53
54#ifndef WCHAR_TYPE
55#define WCHAR_TYPE "int"
56#endif
57
a11eba95
ZW
58/* WCHAR_TYPE gets overridden by -fshort-wchar. */
59#define MODIFIED_WCHAR_TYPE \
60 (flag_short_wchar ? "short unsigned int" : WCHAR_TYPE)
61
0884b60c
BS
62#ifndef PTRDIFF_TYPE
63#define PTRDIFF_TYPE "long int"
64#endif
65
5fd8e536
JM
66#ifndef WINT_TYPE
67#define WINT_TYPE "unsigned int"
68#endif
69
70#ifndef INTMAX_TYPE
71#define INTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
72 ? "int" \
73 : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
74 ? "long int" \
75 : "long long int"))
76#endif
77
78#ifndef UINTMAX_TYPE
79#define UINTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
80 ? "unsigned int" \
81 : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
82 ? "long unsigned int" \
83 : "long long unsigned int"))
84#endif
85
5279d739
ZW
86#ifndef STDC_0_IN_SYSTEM_HEADERS
87#define STDC_0_IN_SYSTEM_HEADERS 0
88#endif
89
90#ifndef REGISTER_PREFIX
91#define REGISTER_PREFIX ""
92#endif
93
4d6baafa
NB
94/* The variant of the C language being processed. */
95
96enum c_language_kind c_language;
97
7f4edbcb 98/* The following symbols are subsumed in the c_global_trees array, and
d125d268 99 listed here individually for documentation purposes.
7f4edbcb
BS
100
101 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
102
103 tree short_integer_type_node;
104 tree long_integer_type_node;
105 tree long_long_integer_type_node;
106
107 tree short_unsigned_type_node;
108 tree long_unsigned_type_node;
109 tree long_long_unsigned_type_node;
110
111 tree boolean_type_node;
112 tree boolean_false_node;
113 tree boolean_true_node;
114
115 tree ptrdiff_type_node;
116
117 tree unsigned_char_type_node;
118 tree signed_char_type_node;
119 tree wchar_type_node;
120 tree signed_wchar_type_node;
121 tree unsigned_wchar_type_node;
122
123 tree float_type_node;
124 tree double_type_node;
125 tree long_double_type_node;
126
127 tree complex_integer_type_node;
128 tree complex_float_type_node;
129 tree complex_double_type_node;
130 tree complex_long_double_type_node;
131
132 tree intQI_type_node;
133 tree intHI_type_node;
134 tree intSI_type_node;
135 tree intDI_type_node;
136 tree intTI_type_node;
137
138 tree unsigned_intQI_type_node;
139 tree unsigned_intHI_type_node;
140 tree unsigned_intSI_type_node;
141 tree unsigned_intDI_type_node;
142 tree unsigned_intTI_type_node;
143
144 tree widest_integer_literal_type_node;
145 tree widest_unsigned_literal_type_node;
146
147 Nodes for types `void *' and `const void *'.
148
149 tree ptr_type_node, const_ptr_type_node;
150
151 Nodes for types `char *' and `const char *'.
152
153 tree string_type_node, const_string_type_node;
154
155 Type `char[SOMENUMBER]'.
156 Used when an array of char is needed and the size is irrelevant.
157
158 tree char_array_type_node;
159
160 Type `int[SOMENUMBER]' or something like it.
161 Used when an array of int needed and the size is irrelevant.
162
163 tree int_array_type_node;
164
165 Type `wchar_t[SOMENUMBER]' or something like it.
166 Used when a wide string literal is created.
167
168 tree wchar_array_type_node;
169
170 Type `int ()' -- used for implicit declaration of functions.
171
172 tree default_function_type;
173
7f4edbcb
BS
174 A VOID_TYPE node, packaged in a TREE_LIST.
175
176 tree void_list_node;
177
684d9f3b 178 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
0ba8a114
NS
179 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
180 VAR_DECLS, but C++ does.)
63ad61ed 181
0ba8a114 182 tree function_name_decl_node;
684d9f3b 183 tree pretty_function_name_decl_node;
0ba8a114
NS
184 tree c99_function_name_decl_node;
185
186 Stack of nested function name VAR_DECLs.
187
188 tree saved_function_name_decls;
63ad61ed 189
7f4edbcb
BS
190*/
191
192tree c_global_trees[CTI_MAX];
0b73773c 193
aaf93206
NB
194/* Nonzero if prepreprocessing only. */
195int flag_preprocess_only;
196
3df89291
NB
197/* Nonzero if an ISO standard was selected. It rejects macros in the
198 user's namespace. */
199int flag_iso;
200
201/* Nonzero if -undef was given. It suppresses target built-in macros
202 and assertions. */
203int flag_undef;
204
6bcedb4e
MM
205/* Nonzero means don't recognize the non-ANSI builtin functions. */
206
207int flag_no_builtin;
208
209/* Nonzero means don't recognize the non-ANSI builtin functions.
210 -ansi sets this. */
211
212int flag_no_nonansi_builtin;
213
eaa7c03f
JM
214/* Nonzero means give `double' the same size as `float'. */
215
216int flag_short_double;
217
218/* Nonzero means give `wchar_t' the same size as `short'. */
219
220int flag_short_wchar;
221
a5a49440
NB
222/* Nonzero means warn about use of multicharacter literals. */
223
224int warn_multichar = 1;
225
2683ed8d
BS
226/* Nonzero means warn about possible violations of sequence point rules. */
227
228int warn_sequence_point;
229
6c36d76b
NB
230/* Nonzero means to warn about compile-time division by zero. */
231int warn_div_by_zero = 1;
232
b34c7881
JT
233/* Warn about NULL being passed to argument slots marked as requiring
234 non-NULL. */
235
236int warn_nonnull;
237
f09f1de5
MM
238/* The elements of `ridpointers' are identifier nodes for the reserved
239 type names and storage classes. It is indexed by a RID_... value. */
240tree *ridpointers;
241
0ba8a114 242tree (*make_fname_decl) PARAMS ((tree, int));
2ce07e2d 243
ae499cce
MM
244/* If non-NULL, the address of a language-specific function that
245 returns 1 for language-specific statement codes. */
246int (*lang_statement_code_p) PARAMS ((enum tree_code));
247
8f17b5c5
MM
248/* If non-NULL, the address of a language-specific function that takes
249 any action required right before expand_function_end is called. */
250void (*lang_expand_function_end) PARAMS ((void));
251
e78a3b42
RK
252/* Nonzero means the expression being parsed will never be evaluated.
253 This is a count, since unevaluated expressions can nest. */
254int skip_evaluation;
255
ec5c56db 256/* Information about how a function name is generated. */
0ba8a114
NS
257struct fname_var_t
258{
8b60264b
KG
259 tree *const decl; /* pointer to the VAR_DECL. */
260 const unsigned rid; /* RID number for the identifier. */
261 const int pretty; /* How pretty is it? */
0ba8a114
NS
262};
263
ec5c56db 264/* The three ways of getting then name of the current function. */
0ba8a114
NS
265
266const struct fname_var_t fname_vars[] =
267{
ec5c56db 268 /* C99 compliant __func__, must be first. */
0ba8a114 269 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
ec5c56db 270 /* GCC __FUNCTION__ compliant. */
0ba8a114 271 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
ec5c56db 272 /* GCC __PRETTY_FUNCTION__ compliant. */
0ba8a114
NS
273 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
274 {NULL, 0, 0},
275};
276
d02b54f6 277static int constant_fits_type_p PARAMS ((tree, tree));
4724b3de 278
0a7ed33c
BS
279/* Keep a stack of if statements. We record the number of compound
280 statements seen up to the if keyword, as well as the line number
281 and file of the if. If a potentially ambiguous else is seen, that
282 fact is recorded; the warning is issued when we can be sure that
283 the enclosing if statement does not have an else branch. */
284typedef struct
285{
286 int compstmt_count;
287 int line;
dff01034 288 const char *file;
0a7ed33c 289 int needs_warning;
8f17b5c5 290 tree if_stmt;
0a7ed33c
BS
291} if_elt;
292
293static if_elt *if_stack;
6d819282
MK
294
295/* Amount of space in the if statement stack. */
296static int if_stack_space = 0;
297
298/* Stack pointer. */
299static int if_stack_pointer = 0;
300
3df89291
NB
301static void cb_register_builtins PARAMS ((cpp_reader *));
302
349ae713
NB
303static tree handle_packed_attribute PARAMS ((tree *, tree, tree, int,
304 bool *));
305static tree handle_nocommon_attribute PARAMS ((tree *, tree, tree, int,
306 bool *));
307static tree handle_common_attribute PARAMS ((tree *, tree, tree, int,
308 bool *));
309static tree handle_noreturn_attribute PARAMS ((tree *, tree, tree, int,
310 bool *));
311static tree handle_noinline_attribute PARAMS ((tree *, tree, tree, int,
312 bool *));
313static tree handle_always_inline_attribute PARAMS ((tree *, tree, tree, int,
314 bool *));
315static tree handle_used_attribute PARAMS ((tree *, tree, tree, int,
316 bool *));
317static tree handle_unused_attribute PARAMS ((tree *, tree, tree, int,
318 bool *));
319static tree handle_const_attribute PARAMS ((tree *, tree, tree, int,
320 bool *));
321static tree handle_transparent_union_attribute PARAMS ((tree *, tree, tree,
322 int, bool *));
323static tree handle_constructor_attribute PARAMS ((tree *, tree, tree, int,
324 bool *));
325static tree handle_destructor_attribute PARAMS ((tree *, tree, tree, int,
326 bool *));
327static tree handle_mode_attribute PARAMS ((tree *, tree, tree, int,
328 bool *));
329static tree handle_section_attribute PARAMS ((tree *, tree, tree, int,
330 bool *));
331static tree handle_aligned_attribute PARAMS ((tree *, tree, tree, int,
332 bool *));
333static tree handle_weak_attribute PARAMS ((tree *, tree, tree, int,
334 bool *));
335static tree handle_alias_attribute PARAMS ((tree *, tree, tree, int,
336 bool *));
337static tree handle_visibility_attribute PARAMS ((tree *, tree, tree, int,
338 bool *));
339static tree handle_no_instrument_function_attribute PARAMS ((tree *, tree,
340 tree, int,
341 bool *));
342static tree handle_malloc_attribute PARAMS ((tree *, tree, tree, int,
343 bool *));
344static tree handle_no_limit_stack_attribute PARAMS ((tree *, tree, tree, int,
345 bool *));
346static tree handle_pure_attribute PARAMS ((tree *, tree, tree, int,
347 bool *));
348static tree handle_deprecated_attribute PARAMS ((tree *, tree, tree, int,
349 bool *));
350static tree handle_vector_size_attribute PARAMS ((tree *, tree, tree, int,
351 bool *));
b34c7881
JT
352static tree handle_nonnull_attribute PARAMS ((tree *, tree, tree, int,
353 bool *));
349ae713
NB
354static tree vector_size_helper PARAMS ((tree, tree));
355
b34c7881
JT
356static void check_function_nonnull PARAMS ((tree, tree));
357static void check_nonnull_arg PARAMS ((void *, tree,
358 unsigned HOST_WIDE_INT));
359static bool nonnull_check_p PARAMS ((tree, unsigned HOST_WIDE_INT));
360static bool get_nonnull_operand PARAMS ((tree,
361 unsigned HOST_WIDE_INT *));
81a75f0f
NB
362void builtin_define_std PARAMS ((const char *));
363static void builtin_define_with_value PARAMS ((const char *, const char *,
364 int));
b34c7881 365
349ae713
NB
366/* Table of machine-independent attributes common to all C-like languages. */
367const struct attribute_spec c_common_attribute_table[] =
368{
369 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
370 { "packed", 0, 0, false, false, false,
371 handle_packed_attribute },
372 { "nocommon", 0, 0, true, false, false,
373 handle_nocommon_attribute },
374 { "common", 0, 0, true, false, false,
375 handle_common_attribute },
376 /* FIXME: logically, noreturn attributes should be listed as
377 "false, true, true" and apply to function types. But implementing this
378 would require all the places in the compiler that use TREE_THIS_VOLATILE
379 on a decl to identify non-returning functions to be located and fixed
380 to check the function type instead. */
381 { "noreturn", 0, 0, true, false, false,
382 handle_noreturn_attribute },
383 { "volatile", 0, 0, true, false, false,
384 handle_noreturn_attribute },
385 { "noinline", 0, 0, true, false, false,
386 handle_noinline_attribute },
387 { "always_inline", 0, 0, true, false, false,
388 handle_always_inline_attribute },
389 { "used", 0, 0, true, false, false,
390 handle_used_attribute },
391 { "unused", 0, 0, false, false, false,
392 handle_unused_attribute },
393 /* The same comments as for noreturn attributes apply to const ones. */
394 { "const", 0, 0, true, false, false,
395 handle_const_attribute },
396 { "transparent_union", 0, 0, false, false, false,
397 handle_transparent_union_attribute },
398 { "constructor", 0, 0, true, false, false,
399 handle_constructor_attribute },
400 { "destructor", 0, 0, true, false, false,
401 handle_destructor_attribute },
402 { "mode", 1, 1, false, true, false,
403 handle_mode_attribute },
404 { "section", 1, 1, true, false, false,
405 handle_section_attribute },
406 { "aligned", 0, 1, false, false, false,
407 handle_aligned_attribute },
408 { "weak", 0, 0, true, false, false,
409 handle_weak_attribute },
410 { "alias", 1, 1, true, false, false,
411 handle_alias_attribute },
412 { "no_instrument_function", 0, 0, true, false, false,
413 handle_no_instrument_function_attribute },
414 { "malloc", 0, 0, true, false, false,
415 handle_malloc_attribute },
416 { "no_stack_limit", 0, 0, true, false, false,
417 handle_no_limit_stack_attribute },
418 { "pure", 0, 0, true, false, false,
419 handle_pure_attribute },
420 { "deprecated", 0, 0, false, false, false,
421 handle_deprecated_attribute },
422 { "vector_size", 1, 1, false, true, false,
423 handle_vector_size_attribute },
424 { "visibility", 1, 1, true, false, false,
425 handle_visibility_attribute },
b34c7881
JT
426 { "nonnull", 0, -1, false, true, true,
427 handle_nonnull_attribute },
349ae713
NB
428 { NULL, 0, 0, false, false, false, NULL }
429};
430
431/* Give the specifications for the format attributes, used by C and all
432 descendents. */
433
434const struct attribute_spec c_common_format_attribute_table[] =
435{
436 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
437 { "format", 3, 3, false, true, true,
438 handle_format_attribute },
439 { "format_arg", 1, 1, false, true, true,
440 handle_format_arg_attribute },
441 { NULL, 0, 0, false, false, false, NULL }
442};
443
8f17b5c5 444/* Record the start of an if-then, and record the start of it
c1e14513
JL
445 for ambiguous else detection.
446
447 COND is the condition for the if-then statement.
448
449 IF_STMT is the statement node that has already been created for
450 this if-then statement. It is created before parsing the
451 condition to keep line number information accurate. */
0a7ed33c 452
6d819282 453void
c1e14513 454c_expand_start_cond (cond, compstmt_count, if_stmt)
6d819282 455 tree cond;
6d819282 456 int compstmt_count;
c1e14513 457 tree if_stmt;
6d819282
MK
458{
459 /* Make sure there is enough space on the stack. */
460 if (if_stack_space == 0)
461 {
462 if_stack_space = 10;
173bf5be 463 if_stack = (if_elt *) xmalloc (10 * sizeof (if_elt));
6d819282
MK
464 }
465 else if (if_stack_space == if_stack_pointer)
466 {
467 if_stack_space += 10;
173bf5be 468 if_stack = (if_elt *) xrealloc (if_stack, if_stack_space * sizeof (if_elt));
6d819282 469 }
0a7ed33c 470
8f17b5c5
MM
471 IF_COND (if_stmt) = cond;
472 add_stmt (if_stmt);
473
6d819282 474 /* Record this if statement. */
0a7ed33c
BS
475 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
476 if_stack[if_stack_pointer].file = input_filename;
477 if_stack[if_stack_pointer].line = lineno;
478 if_stack[if_stack_pointer].needs_warning = 0;
8f17b5c5 479 if_stack[if_stack_pointer].if_stmt = if_stmt;
0a7ed33c 480 if_stack_pointer++;
8f17b5c5 481}
6d819282 482
8f17b5c5
MM
483/* Called after the then-clause for an if-statement is processed. */
484
485void
486c_finish_then ()
487{
488 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
489 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
6d819282
MK
490}
491
8f17b5c5 492/* Record the end of an if-then. Optionally warn if a nested
0a7ed33c
BS
493 if statement had an ambiguous else clause. */
494
6d819282
MK
495void
496c_expand_end_cond ()
497{
498 if_stack_pointer--;
0a7ed33c
BS
499 if (if_stack[if_stack_pointer].needs_warning)
500 warning_with_file_and_line (if_stack[if_stack_pointer].file,
501 if_stack[if_stack_pointer].line,
502 "suggest explicit braces to avoid ambiguous `else'");
8f17b5c5 503 last_expr_type = NULL_TREE;
6d819282
MK
504}
505
8f17b5c5 506/* Called between the then-clause and the else-clause
0a7ed33c
BS
507 of an if-then-else. */
508
6d819282
MK
509void
510c_expand_start_else ()
511{
0a7ed33c
BS
512 /* An ambiguous else warning must be generated for the enclosing if
513 statement, unless we see an else branch for that one, too. */
6d819282
MK
514 if (warn_parentheses
515 && if_stack_pointer > 1
0a7ed33c
BS
516 && (if_stack[if_stack_pointer - 1].compstmt_count
517 == if_stack[if_stack_pointer - 2].compstmt_count))
518 if_stack[if_stack_pointer - 2].needs_warning = 1;
519
520 /* Even if a nested if statement had an else branch, it can't be
521 ambiguous if this one also has an else. So don't warn in that
522 case. Also don't warn for any if statements nested in this else. */
523 if_stack[if_stack_pointer - 1].needs_warning = 0;
524 if_stack[if_stack_pointer - 1].compstmt_count--;
8f17b5c5
MM
525}
526
527/* Called after the else-clause for an if-statement is processed. */
6d819282 528
8f17b5c5
MM
529void
530c_finish_else ()
531{
532 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
533 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
6d819282
MK
534}
535
c1e14513
JL
536/* Begin an if-statement. Returns a newly created IF_STMT if
537 appropriate.
538
539 Unlike the C++ front-end, we do not call add_stmt here; it is
540 probably safe to do so, but I am not very familiar with this
541 code so I am being extra careful not to change its behavior
542 beyond what is strictly necessary for correctness. */
543
544tree
545c_begin_if_stmt ()
546{
547 tree r;
548 r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
549 return r;
550}
551
552/* Begin a while statement. Returns a newly created WHILE_STMT if
553 appropriate.
554
555 Unlike the C++ front-end, we do not call add_stmt here; it is
556 probably safe to do so, but I am not very familiar with this
557 code so I am being extra careful not to change its behavior
558 beyond what is strictly necessary for correctness. */
559
560tree
561c_begin_while_stmt ()
562{
563 tree r;
564 r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
565 return r;
566}
567
568void
569c_finish_while_stmt_cond (cond, while_stmt)
570 tree while_stmt;
571 tree cond;
572{
573 WHILE_COND (while_stmt) = cond;
574}
575
ec5c56db 576/* Push current bindings for the function name VAR_DECLS. */
7da551a2
RS
577
578void
0ba8a114 579start_fname_decls ()
7da551a2 580{
0ba8a114
NS
581 unsigned ix;
582 tree saved = NULL_TREE;
583
584 for (ix = 0; fname_vars[ix].decl; ix++)
585 {
586 tree decl = *fname_vars[ix].decl;
7da551a2 587
0ba8a114
NS
588 if (decl)
589 {
590 saved = tree_cons (decl, build_int_2 (ix, 0), saved);
591 *fname_vars[ix].decl = NULL_TREE;
592 }
593 }
594 if (saved || saved_function_name_decls)
595 /* Normally they'll have been NULL, so only push if we've got a
596 stack, or they are non-NULL. */
597 saved_function_name_decls = tree_cons (saved, NULL_TREE,
598 saved_function_name_decls);
599}
600
601/* Finish up the current bindings, adding them into the
602 current function's statement tree. This is done by wrapping the
603 function's body in a COMPOUND_STMT containing these decls too. This
604 must be done _before_ finish_stmt_tree is called. If there is no
605 current function, we must be at file scope and no statements are
ec5c56db 606 involved. Pop the previous bindings. */
0ba8a114
NS
607
608void
609finish_fname_decls ()
610{
611 unsigned ix;
612 tree body = NULL_TREE;
613 tree stack = saved_function_name_decls;
614
615 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
616 body = chainon (TREE_VALUE (stack), body);
617
618 if (body)
619 {
ff7cc307 620 /* They were called into existence, so add to statement tree. */
0ba8a114
NS
621 body = chainon (body,
622 TREE_CHAIN (DECL_SAVED_TREE (current_function_decl)));
623 body = build_stmt (COMPOUND_STMT, body);
624
625 COMPOUND_STMT_NO_SCOPE (body) = 1;
626 TREE_CHAIN (DECL_SAVED_TREE (current_function_decl)) = body;
627 }
628
629 for (ix = 0; fname_vars[ix].decl; ix++)
630 *fname_vars[ix].decl = NULL_TREE;
631
632 if (stack)
7da551a2 633 {
ec5c56db 634 /* We had saved values, restore them. */
0ba8a114
NS
635 tree saved;
636
637 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
638 {
639 tree decl = TREE_PURPOSE (saved);
640 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
641
642 *fname_vars[ix].decl = decl;
643 }
644 stack = TREE_CHAIN (stack);
7da551a2 645 }
0ba8a114
NS
646 saved_function_name_decls = stack;
647}
648
649/* Return the text name of the current function, suitable prettified
ec5c56db 650 by PRETTY_P. */
0ba8a114
NS
651
652const char *
653fname_as_string (pretty_p)
654 int pretty_p;
655{
656 const char *name = NULL;
657
658 if (pretty_p)
659 name = (current_function_decl
7afff7cf 660 ? (*lang_hooks.decl_printable_name) (current_function_decl, 2)
0ba8a114
NS
661 : "top level");
662 else if (current_function_decl && DECL_NAME (current_function_decl))
663 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
7da551a2 664 else
0ba8a114
NS
665 name = "";
666 return name;
667}
668
669/* Return the text name of the current function, formatted as
670 required by the supplied RID value. */
671
672const char *
673fname_string (rid)
674 unsigned rid;
675{
676 unsigned ix;
677
678 for (ix = 0; fname_vars[ix].decl; ix++)
679 if (fname_vars[ix].rid == rid)
680 break;
681 return fname_as_string (fname_vars[ix].pretty);
682}
683
684/* Return the VAR_DECL for a const char array naming the current
685 function. If the VAR_DECL has not yet been created, create it
686 now. RID indicates how it should be formatted and IDENTIFIER_NODE
687 ID is its name (unfortunately C and C++ hold the RID values of
688 keywords in different places, so we can't derive RID from ID in
f63d1bf7 689 this language independent code. */
0ba8a114
NS
690
691tree
692fname_decl (rid, id)
693 unsigned rid;
694 tree id;
695{
696 unsigned ix;
697 tree decl = NULL_TREE;
698
699 for (ix = 0; fname_vars[ix].decl; ix++)
700 if (fname_vars[ix].rid == rid)
701 break;
702
703 decl = *fname_vars[ix].decl;
704 if (!decl)
7da551a2 705 {
0ba8a114
NS
706 tree saved_last_tree = last_tree;
707
708 decl = (*make_fname_decl) (id, fname_vars[ix].pretty);
709 if (last_tree != saved_last_tree)
710 {
711 /* We created some statement tree for the decl. This belongs
712 at the start of the function, so remove it now and reinsert
ec5c56db 713 it after the function is complete. */
0ba8a114
NS
714 tree stmts = TREE_CHAIN (saved_last_tree);
715
716 TREE_CHAIN (saved_last_tree) = NULL_TREE;
717 last_tree = saved_last_tree;
718 saved_function_name_decls = tree_cons (decl, stmts,
719 saved_function_name_decls);
720 }
721 *fname_vars[ix].decl = decl;
7da551a2 722 }
0ba8a114
NS
723 if (!ix && !current_function_decl)
724 pedwarn_with_decl (decl, "`%s' is not defined outside of function scope");
2ce07e2d 725
0ba8a114 726 return decl;
7da551a2
RS
727}
728
b84a3874 729/* Given a STRING_CST, give it a suitable array-of-chars data type. */
b30f223b
RS
730
731tree
b84a3874
RH
732fix_string_type (value)
733 tree value;
b30f223b 734{
b84a3874
RH
735 const int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
736 const int wide_flag = TREE_TYPE (value) == wchar_array_type_node;
1326a48b 737 const int nchars_max = flag_isoc99 ? 4095 : 509;
b84a3874
RH
738 int length = TREE_STRING_LENGTH (value);
739 int nchars;
b30f223b 740
b57062ca 741 /* Compute the number of elements, for the array type. */
b30f223b
RS
742 nchars = wide_flag ? length / wchar_bytes : length;
743
690c96c8 744 if (pedantic && nchars - 1 > nchars_max && c_language == clk_c)
3220116f 745 pedwarn ("string length `%d' is greater than the length `%d' ISO C%d compilers are required to support",
690c96c8 746 nchars - 1, nchars_max, flag_isoc99 ? 99 : 89);
1326a48b 747
b30f223b
RS
748 /* Create the array type for the string constant.
749 -Wwrite-strings says make the string constant an array of const char
d9cf7c82
JM
750 so that copying it to a non-const pointer will get a warning.
751 For C++, this is the standard behavior. */
f458d1d5 752 if (flag_const_strings && ! flag_writable_strings)
b30f223b
RS
753 {
754 tree elements
755 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
756 1, 0);
757 TREE_TYPE (value)
758 = build_array_type (elements,
759 build_index_type (build_int_2 (nchars - 1, 0)));
760 }
761 else
762 TREE_TYPE (value)
763 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
764 build_index_type (build_int_2 (nchars - 1, 0)));
d9cf7c82 765
ccd4c832
JM
766 TREE_CONSTANT (value) = 1;
767 TREE_READONLY (value) = ! flag_writable_strings;
b30f223b
RS
768 TREE_STATIC (value) = 1;
769 return value;
770}
b84a3874
RH
771
772/* Given a VARRAY of STRING_CST nodes, concatenate them into one
773 STRING_CST. */
774
775tree
776combine_strings (strings)
777 varray_type strings;
778{
779 const int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
780 const int nstrings = VARRAY_ACTIVE_SIZE (strings);
781 tree value, t;
782 int length = 1;
783 int wide_length = 0;
784 int wide_flag = 0;
785 int i;
786 char *p, *q;
787
788 /* Don't include the \0 at the end of each substring. Count wide
789 strings and ordinary strings separately. */
790 for (i = 0; i < nstrings; ++i)
791 {
792 t = VARRAY_TREE (strings, i);
793
794 if (TREE_TYPE (t) == wchar_array_type_node)
795 {
796 wide_length += TREE_STRING_LENGTH (t) - wchar_bytes;
797 wide_flag = 1;
798 }
799 else
800 {
801 length += (TREE_STRING_LENGTH (t) - 1);
802 if (C_ARTIFICIAL_STRING_P (t) && !in_system_header)
803 warning ("concatenation of string literals with __FUNCTION__ is deprecated");
804 }
805 }
806
807 /* If anything is wide, the non-wides will be converted,
808 which makes them take more space. */
809 if (wide_flag)
810 length = length * wchar_bytes + wide_length;
811
812 p = xmalloc (length);
813
814 /* Copy the individual strings into the new combined string.
815 If the combined string is wide, convert the chars to ints
816 for any individual strings that are not wide. */
817
818 q = p;
819 for (i = 0; i < nstrings; ++i)
820 {
821 int len, this_wide;
822
823 t = VARRAY_TREE (strings, i);
824 this_wide = TREE_TYPE (t) == wchar_array_type_node;
825 len = TREE_STRING_LENGTH (t) - (this_wide ? wchar_bytes : 1);
826 if (this_wide == wide_flag)
827 {
828 memcpy (q, TREE_STRING_POINTER (t), len);
829 q += len;
830 }
831 else
832 {
a11eba95
ZW
833 const int nzeros = (TYPE_PRECISION (wchar_type_node)
834 / BITS_PER_UNIT) - 1;
b84a3874
RH
835 int j, k;
836
837 if (BYTES_BIG_ENDIAN)
838 {
839 for (k = 0; k < len; k++)
840 {
841 for (j = 0; j < nzeros; j++)
842 *q++ = 0;
843 *q++ = TREE_STRING_POINTER (t)[k];
844 }
845 }
846 else
847 {
848 for (k = 0; k < len; k++)
849 {
850 *q++ = TREE_STRING_POINTER (t)[k];
851 for (j = 0; j < nzeros; j++)
852 *q++ = 0;
853 }
854 }
855 }
856 }
857
858 /* Nul terminate the string. */
859 if (wide_flag)
860 {
861 for (i = 0; i < wchar_bytes; i++)
862 *q++ = 0;
863 }
864 else
865 *q = 0;
866
867 value = build_string (length, p);
868 free (p);
869
870 if (wide_flag)
871 TREE_TYPE (value) = wchar_array_type_node;
872 else
873 TREE_TYPE (value) = char_array_type_node;
874
875 return value;
876}
b30f223b 877\f
c70eaeaf 878static int is_valid_printf_arglist PARAMS ((tree));
f58e0b0c 879static rtx c_expand_builtin PARAMS ((tree, rtx, enum machine_mode, enum expand_modifier));
c70eaeaf 880static rtx c_expand_builtin_printf PARAMS ((tree, rtx, enum machine_mode,
b4c984fb 881 enum expand_modifier, int, int));
18f988a0 882static rtx c_expand_builtin_fprintf PARAMS ((tree, rtx, enum machine_mode,
b4c984fb 883 enum expand_modifier, int, int));
1ccf251f 884\f
d74154d5
RS
885/* Print a warning if a constant expression had overflow in folding.
886 Invoke this function on every expression that the language
887 requires to be a constant expression.
888 Note the ANSI C standard says it is erroneous for a
889 constant expression to overflow. */
96571883
BK
890
891void
892constant_expression_warning (value)
893 tree value;
894{
c05f751c 895 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
69ef87e2 896 || TREE_CODE (value) == VECTOR_CST
c05f751c
RK
897 || TREE_CODE (value) == COMPLEX_CST)
898 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
899 pedwarn ("overflow in constant expression");
d74154d5
RS
900}
901
902/* Print a warning if an expression had overflow in folding.
903 Invoke this function on every expression that
904 (1) appears in the source code, and
905 (2) might be a constant expression that overflowed, and
906 (3) is not already checked by convert_and_check;
907 however, do not invoke this function on operands of explicit casts. */
908
909void
910overflow_warning (value)
911 tree value;
912{
c05f751c
RK
913 if ((TREE_CODE (value) == INTEGER_CST
914 || (TREE_CODE (value) == COMPLEX_CST
915 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
916 && TREE_OVERFLOW (value))
d74154d5 917 {
7193bce2 918 TREE_OVERFLOW (value) = 0;
e78a3b42
RK
919 if (skip_evaluation == 0)
920 warning ("integer overflow in expression");
d74154d5 921 }
c05f751c
RK
922 else if ((TREE_CODE (value) == REAL_CST
923 || (TREE_CODE (value) == COMPLEX_CST
924 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
925 && TREE_OVERFLOW (value))
926 {
927 TREE_OVERFLOW (value) = 0;
e78a3b42
RK
928 if (skip_evaluation == 0)
929 warning ("floating point overflow in expression");
c05f751c 930 }
69ef87e2
AH
931 else if (TREE_CODE (value) == VECTOR_CST && TREE_OVERFLOW (value))
932 {
933 TREE_OVERFLOW (value) = 0;
934 if (skip_evaluation == 0)
935 warning ("vector overflow in expression");
936 }
d74154d5
RS
937}
938
939/* Print a warning if a large constant is truncated to unsigned,
940 or if -Wconversion is used and a constant < 0 is converted to unsigned.
941 Invoke this function on every expression that might be implicitly
942 converted to an unsigned type. */
943
944void
945unsigned_conversion_warning (result, operand)
946 tree result, operand;
947{
ceef8ce4
NB
948 tree type = TREE_TYPE (result);
949
d74154d5 950 if (TREE_CODE (operand) == INTEGER_CST
ceef8ce4
NB
951 && TREE_CODE (type) == INTEGER_TYPE
952 && TREE_UNSIGNED (type)
e78a3b42 953 && skip_evaluation == 0
ceef8ce4 954 && !int_fits_type_p (operand, type))
d74154d5 955 {
ceef8ce4 956 if (!int_fits_type_p (operand, c_common_signed_type (type)))
d74154d5 957 /* This detects cases like converting -129 or 256 to unsigned char. */
90c939d4 958 warning ("large integer implicitly truncated to unsigned type");
d74154d5 959 else if (warn_conversion)
90c939d4 960 warning ("negative integer implicitly converted to unsigned type");
d74154d5
RS
961 }
962}
963
d02b54f6
JJ
964/* Nonzero if constant C has a value that is permissible
965 for type TYPE (an INTEGER_TYPE). */
966
967static int
968constant_fits_type_p (c, type)
969 tree c, type;
970{
971 if (TREE_CODE (c) == INTEGER_CST)
972 return int_fits_type_p (c, type);
973
974 c = convert (type, c);
975 return !TREE_OVERFLOW (c);
976}
977
d74154d5
RS
978/* Convert EXPR to TYPE, warning about conversion problems with constants.
979 Invoke this function on every expression that is converted implicitly,
980 i.e. because of language rules and not because of an explicit cast. */
981
982tree
983convert_and_check (type, expr)
984 tree type, expr;
985{
986 tree t = convert (type, expr);
987 if (TREE_CODE (t) == INTEGER_CST)
988 {
7193bce2 989 if (TREE_OVERFLOW (t))
d74154d5 990 {
7193bce2
PE
991 TREE_OVERFLOW (t) = 0;
992
868fc750
RK
993 /* Do not diagnose overflow in a constant expression merely
994 because a conversion overflowed. */
995 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
996
7193bce2
PE
997 /* No warning for converting 0x80000000 to int. */
998 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
999 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1000 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
22ba338b
RS
1001 /* If EXPR fits in the unsigned version of TYPE,
1002 don't warn unless pedantic. */
e78a3b42
RK
1003 if ((pedantic
1004 || TREE_UNSIGNED (type)
ceef8ce4
NB
1005 || ! constant_fits_type_p (expr,
1006 c_common_unsigned_type (type)))
e78a3b42 1007 && skip_evaluation == 0)
bb72a084 1008 warning ("overflow in implicit constant conversion");
d74154d5
RS
1009 }
1010 else
1011 unsigned_conversion_warning (t, expr);
1012 }
1013 return t;
96571883
BK
1014}
1015\f
235cfbc4
BS
1016/* A node in a list that describes references to variables (EXPR), which are
1017 either read accesses if WRITER is zero, or write accesses, in which case
1018 WRITER is the parent of EXPR. */
1019struct tlist
1020{
1021 struct tlist *next;
1022 tree expr, writer;
1023};
1024
1025/* Used to implement a cache the results of a call to verify_tree. We only
1026 use this for SAVE_EXPRs. */
1027struct tlist_cache
1028{
1029 struct tlist_cache *next;
1030 struct tlist *cache_before_sp;
1031 struct tlist *cache_after_sp;
1032 tree expr;
2683ed8d
BS
1033};
1034
235cfbc4
BS
1035/* Obstack to use when allocating tlist structures, and corresponding
1036 firstobj. */
1037static struct obstack tlist_obstack;
1038static char *tlist_firstobj = 0;
1039
1040/* Keep track of the identifiers we've warned about, so we can avoid duplicate
1041 warnings. */
1042static struct tlist *warned_ids;
1043/* SAVE_EXPRs need special treatment. We process them only once and then
1044 cache the results. */
1045static struct tlist_cache *save_expr_cache;
1046
1047static void add_tlist PARAMS ((struct tlist **, struct tlist *, tree, int));
1048static void merge_tlist PARAMS ((struct tlist **, struct tlist *, int));
1049static void verify_tree PARAMS ((tree, struct tlist **, struct tlist **, tree));
1050static int warning_candidate_p PARAMS ((tree));
1051static void warn_for_collisions PARAMS ((struct tlist *));
1052static void warn_for_collisions_1 PARAMS ((tree, tree, struct tlist *, int));
1053static struct tlist *new_tlist PARAMS ((struct tlist *, tree, tree));
2683ed8d
BS
1054static void verify_sequence_points PARAMS ((tree));
1055
235cfbc4
BS
1056/* Create a new struct tlist and fill in its fields. */
1057static struct tlist *
1058new_tlist (next, t, writer)
1059 struct tlist *next;
1060 tree t;
1061 tree writer;
1062{
1063 struct tlist *l;
1064 l = (struct tlist *) obstack_alloc (&tlist_obstack, sizeof *l);
1065 l->next = next;
1066 l->expr = t;
1067 l->writer = writer;
1068 return l;
1069}
1070
1071/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1072 is nonnull, we ignore any node we find which has a writer equal to it. */
1073
1074static void
1075add_tlist (to, add, exclude_writer, copy)
1076 struct tlist **to;
1077 struct tlist *add;
1078 tree exclude_writer;
1079 int copy;
1080{
1081 while (add)
1082 {
1083 struct tlist *next = add->next;
1084 if (! copy)
1085 add->next = *to;
1086 if (! exclude_writer || add->writer != exclude_writer)
1087 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1088 add = next;
1089 }
1090}
1091
1092/* Merge the nodes of ADD into TO. This merging process is done so that for
1093 each variable that already exists in TO, no new node is added; however if
1094 there is a write access recorded in ADD, and an occurrence on TO is only
1095 a read access, then the occurrence in TO will be modified to record the
1096 write. */
2683ed8d
BS
1097
1098static void
235cfbc4
BS
1099merge_tlist (to, add, copy)
1100 struct tlist **to;
1101 struct tlist *add;
1102 int copy;
1103{
1104 struct tlist **end = to;
1105
1106 while (*end)
1107 end = &(*end)->next;
1108
1109 while (add)
1110 {
1111 int found = 0;
1112 struct tlist *tmp2;
1113 struct tlist *next = add->next;
1114
1115 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1116 if (tmp2->expr == add->expr)
1117 {
1118 found = 1;
1119 if (! tmp2->writer)
1120 tmp2->writer = add->writer;
1121 }
1122 if (! found)
1123 {
1124 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
1125 end = &(*end)->next;
1126 *end = 0;
1127 }
1128 add = next;
1129 }
1130}
1131
1132/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1133 references in list LIST conflict with it, excluding reads if ONLY writers
1134 is nonzero. */
1135
1136static void
1137warn_for_collisions_1 (written, writer, list, only_writes)
1138 tree written, writer;
1139 struct tlist *list;
1140 int only_writes;
1141{
1142 struct tlist *tmp;
1143
1144 /* Avoid duplicate warnings. */
1145 for (tmp = warned_ids; tmp; tmp = tmp->next)
1146 if (tmp->expr == written)
1147 return;
1148
1149 while (list)
1150 {
1151 if (list->expr == written
1152 && list->writer != writer
1153 && (! only_writes || list->writer))
1154 {
1155 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
1156 warning ("operation on `%s' may be undefined",
1157 IDENTIFIER_POINTER (DECL_NAME (list->expr)));
1158 }
1159 list = list->next;
1160 }
1161}
1162
1163/* Given a list LIST of references to variables, find whether any of these
1164 can cause conflicts due to missing sequence points. */
1165
1166static void
1167warn_for_collisions (list)
1168 struct tlist *list;
1169{
1170 struct tlist *tmp;
1171
1172 for (tmp = list; tmp; tmp = tmp->next)
1173 {
1174 if (tmp->writer)
1175 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1176 }
1177}
1178
684d9f3b 1179/* Return nonzero if X is a tree that can be verified by the sequence point
235cfbc4
BS
1180 warnings. */
1181static int
1182warning_candidate_p (x)
2683ed8d 1183 tree x;
2683ed8d 1184{
235cfbc4
BS
1185 return TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == PARM_DECL;
1186}
2683ed8d 1187
235cfbc4
BS
1188/* Walk the tree X, and record accesses to variables. If X is written by the
1189 parent tree, WRITER is the parent.
1190 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
1191 expression or its only operand forces a sequence point, then everything up
1192 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
1193 in PNO_SP.
1194 Once we return, we will have emitted warnings if any subexpression before
1195 such a sequence point could be undefined. On a higher level, however, the
1196 sequence point may not be relevant, and we'll merge the two lists.
1197
1198 Example: (b++, a) + b;
1199 The call that processes the COMPOUND_EXPR will store the increment of B
1200 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
1201 processes the PLUS_EXPR will need to merge the two lists so that
1202 eventually, all accesses end up on the same list (and we'll warn about the
1203 unordered subexpressions b++ and b.
1204
1205 A note on merging. If we modify the former example so that our expression
1206 becomes
1207 (b++, b) + a
1208 care must be taken not simply to add all three expressions into the final
1209 PNO_SP list. The function merge_tlist takes care of that by merging the
1210 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
1211 way, so that no more than one access to B is recorded. */
2683ed8d 1212
235cfbc4
BS
1213static void
1214verify_tree (x, pbefore_sp, pno_sp, writer)
1215 tree x;
1216 struct tlist **pbefore_sp, **pno_sp;
1217 tree writer;
1218{
1219 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
1220 enum tree_code code;
1221 char class;
2683ed8d 1222
f9e1917e
JM
1223 /* X may be NULL if it is the operand of an empty statement expression
1224 ({ }). */
1225 if (x == NULL)
1226 return;
1227
235cfbc4
BS
1228 restart:
1229 code = TREE_CODE (x);
1230 class = TREE_CODE_CLASS (code);
2683ed8d 1231
235cfbc4 1232 if (warning_candidate_p (x))
2683ed8d 1233 {
235cfbc4
BS
1234 *pno_sp = new_tlist (*pno_sp, x, writer);
1235 return;
1236 }
1237
1238 switch (code)
1239 {
52a84e42
BS
1240 case CONSTRUCTOR:
1241 return;
1242
235cfbc4
BS
1243 case COMPOUND_EXPR:
1244 case TRUTH_ANDIF_EXPR:
1245 case TRUTH_ORIF_EXPR:
1246 tmp_before = tmp_nosp = tmp_list3 = 0;
1247 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1248 warn_for_collisions (tmp_nosp);
1249 merge_tlist (pbefore_sp, tmp_before, 0);
1250 merge_tlist (pbefore_sp, tmp_nosp, 0);
1251 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
1252 merge_tlist (pbefore_sp, tmp_list3, 0);
1253 return;
1254
1255 case COND_EXPR:
1256 tmp_before = tmp_list2 = 0;
1257 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
1258 warn_for_collisions (tmp_list2);
1259 merge_tlist (pbefore_sp, tmp_before, 0);
1260 merge_tlist (pbefore_sp, tmp_list2, 1);
1261
1262 tmp_list3 = tmp_nosp = 0;
1263 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
1264 warn_for_collisions (tmp_nosp);
1265 merge_tlist (pbefore_sp, tmp_list3, 0);
1266
1267 tmp_list3 = tmp_list2 = 0;
1268 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
1269 warn_for_collisions (tmp_list2);
1270 merge_tlist (pbefore_sp, tmp_list3, 0);
1271 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1272 two first, to avoid warning for (a ? b++ : b++). */
1273 merge_tlist (&tmp_nosp, tmp_list2, 0);
1274 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1275 return;
1276
2683ed8d
BS
1277 case PREDECREMENT_EXPR:
1278 case PREINCREMENT_EXPR:
1279 case POSTDECREMENT_EXPR:
1280 case POSTINCREMENT_EXPR:
235cfbc4
BS
1281 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
1282 return;
1283
1284 case MODIFY_EXPR:
1285 tmp_before = tmp_nosp = tmp_list3 = 0;
1286 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
1287 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
1288 /* Expressions inside the LHS are not ordered wrt. the sequence points
1289 in the RHS. Example:
1290 *a = (a++, 2)
1291 Despite the fact that the modification of "a" is in the before_sp
1292 list (tmp_before), it conflicts with the use of "a" in the LHS.
1293 We can handle this by adding the contents of tmp_list3
1294 to those of tmp_before, and redoing the collision warnings for that
1295 list. */
1296 add_tlist (&tmp_before, tmp_list3, x, 1);
1297 warn_for_collisions (tmp_before);
1298 /* Exclude the LHS itself here; we first have to merge it into the
1299 tmp_nosp list. This is done to avoid warning for "a = a"; if we
1300 didn't exclude the LHS, we'd get it twice, once as a read and once
1301 as a write. */
1302 add_tlist (pno_sp, tmp_list3, x, 0);
1303 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
1304
1305 merge_tlist (pbefore_sp, tmp_before, 0);
1306 if (warning_candidate_p (TREE_OPERAND (x, 0)))
1307 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
1308 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
1309 return;
2683ed8d
BS
1310
1311 case CALL_EXPR:
235cfbc4
BS
1312 /* We need to warn about conflicts among arguments and conflicts between
1313 args and the function address. Side effects of the function address,
1314 however, are not ordered by the sequence point of the call. */
1315 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
1316 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1317 if (TREE_OPERAND (x, 1))
1318 verify_tree (TREE_OPERAND (x, 1), &tmp_list2, &tmp_list3, NULL_TREE);
1319 merge_tlist (&tmp_list3, tmp_list2, 0);
1320 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
1321 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
1322 warn_for_collisions (tmp_before);
1323 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
1324 return;
2683ed8d
BS
1325
1326 case TREE_LIST:
1327 /* Scan all the list, e.g. indices of multi dimensional array. */
1328 while (x)
1329 {
235cfbc4
BS
1330 tmp_before = tmp_nosp = 0;
1331 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
1332 merge_tlist (&tmp_nosp, tmp_before, 0);
1333 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2683ed8d
BS
1334 x = TREE_CHAIN (x);
1335 }
235cfbc4 1336 return;
2683ed8d 1337
235cfbc4
BS
1338 case SAVE_EXPR:
1339 {
1340 struct tlist_cache *t;
1341 for (t = save_expr_cache; t; t = t->next)
1342 if (t->expr == x)
1343 break;
2683ed8d 1344
235cfbc4 1345 if (! t)
2683ed8d 1346 {
235cfbc4
BS
1347 t = (struct tlist_cache *) obstack_alloc (&tlist_obstack,
1348 sizeof *t);
1349 t->next = save_expr_cache;
1350 t->expr = x;
1351 save_expr_cache = t;
1352
1353 tmp_before = tmp_nosp = 0;
1354 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1355 warn_for_collisions (tmp_nosp);
1356
1357 tmp_list3 = 0;
1358 while (tmp_nosp)
1359 {
1360 struct tlist *t = tmp_nosp;
1361 tmp_nosp = t->next;
1362 merge_tlist (&tmp_list3, t, 0);
1363 }
1364 t->cache_before_sp = tmp_before;
1365 t->cache_after_sp = tmp_list3;
2683ed8d 1366 }
235cfbc4
BS
1367 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
1368 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
1369 return;
1370 }
1371 default:
2683ed8d
BS
1372 break;
1373 }
2683ed8d 1374
235cfbc4 1375 if (class == '1')
2683ed8d 1376 {
235cfbc4
BS
1377 if (first_rtl_op (code) == 0)
1378 return;
1379 x = TREE_OPERAND (x, 0);
1380 writer = 0;
1381 goto restart;
2683ed8d
BS
1382 }
1383
235cfbc4 1384 switch (class)
2683ed8d 1385 {
235cfbc4
BS
1386 case 'r':
1387 case '<':
1388 case '2':
1389 case 'b':
1390 case 'e':
1391 case 's':
1392 case 'x':
1393 {
1394 int lp;
1395 int max = first_rtl_op (TREE_CODE (x));
1396 for (lp = 0; lp < max; lp++)
1397 {
1398 tmp_before = tmp_nosp = 0;
1399 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, NULL_TREE);
1400 merge_tlist (&tmp_nosp, tmp_before, 0);
1401 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1402 }
1403 break;
1404 }
2683ed8d 1405 }
2683ed8d
BS
1406}
1407
1408/* Try to warn for undefined behaviour in EXPR due to missing sequence
1409 points. */
1410
1411static void
1412verify_sequence_points (expr)
1413 tree expr;
1414{
235cfbc4 1415 struct tlist *before_sp = 0, *after_sp = 0;
2683ed8d 1416
235cfbc4
BS
1417 warned_ids = 0;
1418 save_expr_cache = 0;
1419 if (tlist_firstobj == 0)
2683ed8d 1420 {
235cfbc4
BS
1421 gcc_obstack_init (&tlist_obstack);
1422 tlist_firstobj = obstack_alloc (&tlist_obstack, 0);
2683ed8d
BS
1423 }
1424
235cfbc4
BS
1425 verify_tree (expr, &before_sp, &after_sp, 0);
1426 warn_for_collisions (after_sp);
1427 obstack_free (&tlist_obstack, tlist_firstobj);
2683ed8d
BS
1428}
1429
64094f6a 1430tree
b30f223b
RS
1431c_expand_expr_stmt (expr)
1432 tree expr;
1433{
1434 /* Do default conversion if safe and possibly important,
1435 in case within ({...}). */
207bf485
JM
1436 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
1437 && (flag_isoc99 || lvalue_p (expr)))
b30f223b
RS
1438 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1439 expr = default_conversion (expr);
1440
2683ed8d
BS
1441 if (warn_sequence_point)
1442 verify_sequence_points (expr);
1443
b30f223b 1444 if (TREE_TYPE (expr) != error_mark_node
b8de2d02 1445 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
b30f223b
RS
1446 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1447 error ("expression statement has incomplete type");
1448
8f17b5c5 1449 last_expr_type = TREE_TYPE (expr);
64094f6a 1450 return add_stmt (build_stmt (EXPR_STMT, expr));
b30f223b
RS
1451}
1452\f
1453/* Validate the expression after `case' and apply default promotions. */
1454
1455tree
1456check_case_value (value)
1457 tree value;
1458{
1459 if (value == NULL_TREE)
1460 return value;
1461
1462 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
8493738b 1463 STRIP_TYPE_NOPS (value);
56cb9733
MM
1464 /* In C++, the following is allowed:
1465
1466 const int i = 3;
1467 switch (...) { case i: ... }
1468
1469 So, we try to reduce the VALUE to a constant that way. */
1470 if (c_language == clk_cplusplus)
1471 {
1472 value = decl_constant_value (value);
1473 STRIP_TYPE_NOPS (value);
1474 value = fold (value);
1475 }
b30f223b
RS
1476
1477 if (TREE_CODE (value) != INTEGER_CST
1478 && value != error_mark_node)
1479 {
1480 error ("case label does not reduce to an integer constant");
1481 value = error_mark_node;
1482 }
1483 else
1484 /* Promote char or short to int. */
1485 value = default_conversion (value);
1486
bc690db1
RS
1487 constant_expression_warning (value);
1488
b30f223b
RS
1489 return value;
1490}
1491\f
1492/* Return an integer type with BITS bits of precision,
1493 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1494
1495tree
b0c48229 1496c_common_type_for_size (bits, unsignedp)
b30f223b
RS
1497 unsigned bits;
1498 int unsignedp;
1499{
a311b52c
JM
1500 if (bits == TYPE_PRECISION (integer_type_node))
1501 return unsignedp ? unsigned_type_node : integer_type_node;
1502
3fc7e390 1503 if (bits == TYPE_PRECISION (signed_char_type_node))
b30f223b
RS
1504 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1505
3fc7e390 1506 if (bits == TYPE_PRECISION (short_integer_type_node))
b30f223b
RS
1507 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1508
3fc7e390 1509 if (bits == TYPE_PRECISION (long_integer_type_node))
b30f223b
RS
1510 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1511
3fc7e390 1512 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b30f223b
RS
1513 return (unsignedp ? long_long_unsigned_type_node
1514 : long_long_integer_type_node);
1515
835f9b4d
GRK
1516 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
1517 return (unsignedp ? widest_unsigned_literal_type_node
1518 : widest_integer_literal_type_node);
1519
3fc7e390
RS
1520 if (bits <= TYPE_PRECISION (intQI_type_node))
1521 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1522
1523 if (bits <= TYPE_PRECISION (intHI_type_node))
1524 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1525
1526 if (bits <= TYPE_PRECISION (intSI_type_node))
1527 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1528
1529 if (bits <= TYPE_PRECISION (intDI_type_node))
1530 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1531
b30f223b
RS
1532 return 0;
1533}
1534
1535/* Return a data type that has machine mode MODE.
1536 If the mode is an integer,
1537 then UNSIGNEDP selects between signed and unsigned types. */
1538
1539tree
b0c48229 1540c_common_type_for_mode (mode, unsignedp)
b30f223b
RS
1541 enum machine_mode mode;
1542 int unsignedp;
1543{
a311b52c
JM
1544 if (mode == TYPE_MODE (integer_type_node))
1545 return unsignedp ? unsigned_type_node : integer_type_node;
1546
b30f223b
RS
1547 if (mode == TYPE_MODE (signed_char_type_node))
1548 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1549
1550 if (mode == TYPE_MODE (short_integer_type_node))
1551 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1552
b30f223b
RS
1553 if (mode == TYPE_MODE (long_integer_type_node))
1554 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1555
1556 if (mode == TYPE_MODE (long_long_integer_type_node))
1557 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
1558
835f9b4d 1559 if (mode == TYPE_MODE (widest_integer_literal_type_node))
d125d268 1560 return unsignedp ? widest_unsigned_literal_type_node
835f9b4d
GRK
1561 : widest_integer_literal_type_node;
1562
0afeef64 1563 if (mode == QImode)
3fc7e390
RS
1564 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1565
0afeef64 1566 if (mode == HImode)
3fc7e390
RS
1567 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1568
0afeef64 1569 if (mode == SImode)
3fc7e390
RS
1570 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1571
0afeef64 1572 if (mode == DImode)
3fc7e390
RS
1573 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1574
21a9616b 1575#if HOST_BITS_PER_WIDE_INT >= 64
a6d7e156
JL
1576 if (mode == TYPE_MODE (intTI_type_node))
1577 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
21a9616b 1578#endif
a6d7e156 1579
b30f223b
RS
1580 if (mode == TYPE_MODE (float_type_node))
1581 return float_type_node;
1582
1583 if (mode == TYPE_MODE (double_type_node))
1584 return double_type_node;
1585
1586 if (mode == TYPE_MODE (long_double_type_node))
1587 return long_double_type_node;
1588
1589 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
1590 return build_pointer_type (char_type_node);
1591
1592 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
1593 return build_pointer_type (integer_type_node);
1594
4061f623 1595#ifdef VECTOR_MODE_SUPPORTED_P
0afeef64
AH
1596 if (VECTOR_MODE_SUPPORTED_P (mode))
1597 {
1598 switch (mode)
1599 {
1600 case V16QImode:
1601 return unsignedp ? unsigned_V16QI_type_node : V16QI_type_node;
1602 case V8HImode:
1603 return unsignedp ? unsigned_V8HI_type_node : V8HI_type_node;
1604 case V4SImode:
1605 return unsignedp ? unsigned_V4SI_type_node : V4SI_type_node;
fbe5eb6d
BS
1606 case V2DImode:
1607 return unsignedp ? unsigned_V2DI_type_node : V2DI_type_node;
0afeef64
AH
1608 case V2SImode:
1609 return unsignedp ? unsigned_V2SI_type_node : V2SI_type_node;
1610 case V4HImode:
1611 return unsignedp ? unsigned_V4HI_type_node : V4HI_type_node;
1612 case V8QImode:
1613 return unsignedp ? unsigned_V8QI_type_node : V8QI_type_node;
fa5322fa
AO
1614 case V16SFmode:
1615 return V16SF_type_node;
0afeef64
AH
1616 case V4SFmode:
1617 return V4SF_type_node;
1618 case V2SFmode:
1619 return V2SF_type_node;
fbe5eb6d
BS
1620 case V2DFmode:
1621 return V2DF_type_node;
0afeef64
AH
1622 default:
1623 break;
1624 }
1625 }
4061f623
BS
1626#endif
1627
b30f223b
RS
1628 return 0;
1629}
693a6128 1630
ec5c56db 1631/* Return an unsigned type the same as TYPE in other respects. */
693a6128 1632tree
ceef8ce4 1633c_common_unsigned_type (type)
693a6128
GRK
1634 tree type;
1635{
1636 tree type1 = TYPE_MAIN_VARIANT (type);
1637 if (type1 == signed_char_type_node || type1 == char_type_node)
1638 return unsigned_char_type_node;
1639 if (type1 == integer_type_node)
1640 return unsigned_type_node;
1641 if (type1 == short_integer_type_node)
1642 return short_unsigned_type_node;
1643 if (type1 == long_integer_type_node)
1644 return long_unsigned_type_node;
1645 if (type1 == long_long_integer_type_node)
1646 return long_long_unsigned_type_node;
1647 if (type1 == widest_integer_literal_type_node)
1648 return widest_unsigned_literal_type_node;
1649#if HOST_BITS_PER_WIDE_INT >= 64
1650 if (type1 == intTI_type_node)
1651 return unsigned_intTI_type_node;
1652#endif
1653 if (type1 == intDI_type_node)
1654 return unsigned_intDI_type_node;
1655 if (type1 == intSI_type_node)
1656 return unsigned_intSI_type_node;
1657 if (type1 == intHI_type_node)
1658 return unsigned_intHI_type_node;
1659 if (type1 == intQI_type_node)
1660 return unsigned_intQI_type_node;
1661
ceef8ce4 1662 return c_common_signed_or_unsigned_type (1, type);
693a6128
GRK
1663}
1664
1665/* Return a signed type the same as TYPE in other respects. */
1666
1667tree
ceef8ce4 1668c_common_signed_type (type)
693a6128
GRK
1669 tree type;
1670{
1671 tree type1 = TYPE_MAIN_VARIANT (type);
1672 if (type1 == unsigned_char_type_node || type1 == char_type_node)
1673 return signed_char_type_node;
1674 if (type1 == unsigned_type_node)
1675 return integer_type_node;
1676 if (type1 == short_unsigned_type_node)
1677 return short_integer_type_node;
1678 if (type1 == long_unsigned_type_node)
1679 return long_integer_type_node;
1680 if (type1 == long_long_unsigned_type_node)
1681 return long_long_integer_type_node;
1682 if (type1 == widest_unsigned_literal_type_node)
1683 return widest_integer_literal_type_node;
1684#if HOST_BITS_PER_WIDE_INT >= 64
1685 if (type1 == unsigned_intTI_type_node)
1686 return intTI_type_node;
1687#endif
1688 if (type1 == unsigned_intDI_type_node)
1689 return intDI_type_node;
1690 if (type1 == unsigned_intSI_type_node)
1691 return intSI_type_node;
1692 if (type1 == unsigned_intHI_type_node)
1693 return intHI_type_node;
1694 if (type1 == unsigned_intQI_type_node)
1695 return intQI_type_node;
1696
ceef8ce4 1697 return c_common_signed_or_unsigned_type (0, type);
693a6128
GRK
1698}
1699
1700/* Return a type the same as TYPE except unsigned or
1701 signed according to UNSIGNEDP. */
1702
1703tree
ceef8ce4 1704c_common_signed_or_unsigned_type (unsignedp, type)
693a6128
GRK
1705 int unsignedp;
1706 tree type;
1707{
1708 if (! INTEGRAL_TYPE_P (type)
1709 || TREE_UNSIGNED (type) == unsignedp)
1710 return type;
1711
1712 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
1713 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
d125d268 1714 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
693a6128 1715 return unsignedp ? unsigned_type_node : integer_type_node;
d125d268 1716 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
693a6128 1717 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
d125d268 1718 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
693a6128 1719 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
d125d268 1720 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
693a6128
GRK
1721 return (unsignedp ? long_long_unsigned_type_node
1722 : long_long_integer_type_node);
d125d268 1723 if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
693a6128
GRK
1724 return (unsignedp ? widest_unsigned_literal_type_node
1725 : widest_integer_literal_type_node);
4a063bec
RH
1726
1727#if HOST_BITS_PER_WIDE_INT >= 64
1728 if (TYPE_PRECISION (type) == TYPE_PRECISION (intTI_type_node))
1729 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
1730#endif
1731 if (TYPE_PRECISION (type) == TYPE_PRECISION (intDI_type_node))
1732 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1733 if (TYPE_PRECISION (type) == TYPE_PRECISION (intSI_type_node))
1734 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1735 if (TYPE_PRECISION (type) == TYPE_PRECISION (intHI_type_node))
1736 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1737 if (TYPE_PRECISION (type) == TYPE_PRECISION (intQI_type_node))
1738 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1739
693a6128
GRK
1740 return type;
1741}
b30f223b 1742\f
6acfe908
JM
1743/* Return the minimum number of bits needed to represent VALUE in a
1744 signed or unsigned type, UNSIGNEDP says which. */
1745
05bccae2 1746unsigned int
6acfe908
JM
1747min_precision (value, unsignedp)
1748 tree value;
1749 int unsignedp;
1750{
1751 int log;
1752
1753 /* If the value is negative, compute its negative minus 1. The latter
1754 adjustment is because the absolute value of the largest negative value
1755 is one larger than the largest positive value. This is equivalent to
1756 a bit-wise negation, so use that operation instead. */
1757
1758 if (tree_int_cst_sgn (value) < 0)
1759 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
1760
1761 /* Return the number of bits needed, taking into account the fact
1762 that we need one more bit for a signed than unsigned type. */
1763
1764 if (integer_zerop (value))
1765 log = 0;
6acfe908 1766 else
05bccae2 1767 log = tree_floor_log2 (value);
6acfe908
JM
1768
1769 return log + 1 + ! unsignedp;
1770}
1771\f
78ef5b89
NB
1772/* Print an error message for invalid operands to arith operation
1773 CODE. NOP_EXPR is used as a special case (see
1774 c_common_truthvalue_conversion). */
b30f223b
RS
1775
1776void
1777binary_op_error (code)
1778 enum tree_code code;
1779{
b3694847 1780 const char *opname;
89c78d7d 1781
b30f223b
RS
1782 switch (code)
1783 {
1784 case NOP_EXPR:
1785 error ("invalid truth-value expression");
1786 return;
1787
1788 case PLUS_EXPR:
1789 opname = "+"; break;
1790 case MINUS_EXPR:
1791 opname = "-"; break;
1792 case MULT_EXPR:
1793 opname = "*"; break;
1794 case MAX_EXPR:
1795 opname = "max"; break;
1796 case MIN_EXPR:
1797 opname = "min"; break;
1798 case EQ_EXPR:
1799 opname = "=="; break;
1800 case NE_EXPR:
1801 opname = "!="; break;
1802 case LE_EXPR:
1803 opname = "<="; break;
1804 case GE_EXPR:
1805 opname = ">="; break;
1806 case LT_EXPR:
1807 opname = "<"; break;
1808 case GT_EXPR:
1809 opname = ">"; break;
1810 case LSHIFT_EXPR:
1811 opname = "<<"; break;
1812 case RSHIFT_EXPR:
1813 opname = ">>"; break;
1814 case TRUNC_MOD_EXPR:
047de90b 1815 case FLOOR_MOD_EXPR:
b30f223b
RS
1816 opname = "%"; break;
1817 case TRUNC_DIV_EXPR:
047de90b 1818 case FLOOR_DIV_EXPR:
b30f223b
RS
1819 opname = "/"; break;
1820 case BIT_AND_EXPR:
1821 opname = "&"; break;
1822 case BIT_IOR_EXPR:
1823 opname = "|"; break;
1824 case TRUTH_ANDIF_EXPR:
1825 opname = "&&"; break;
1826 case TRUTH_ORIF_EXPR:
1827 opname = "||"; break;
1828 case BIT_XOR_EXPR:
1829 opname = "^"; break;
047de90b
RS
1830 case LROTATE_EXPR:
1831 case RROTATE_EXPR:
1832 opname = "rotate"; break;
6d819282
MK
1833 default:
1834 opname = "unknown"; break;
b30f223b
RS
1835 }
1836 error ("invalid operands to binary %s", opname);
1837}
1838\f
1839/* Subroutine of build_binary_op, used for comparison operations.
1840 See if the operands have both been converted from subword integer types
1841 and, if so, perhaps change them both back to their original type.
94dccd9d
RS
1842 This function is also responsible for converting the two operands
1843 to the proper common type for comparison.
b30f223b
RS
1844
1845 The arguments of this function are all pointers to local variables
1846 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
1847 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
1848
1849 If this function returns nonzero, it means that the comparison has
1850 a constant value. What this function returns is an expression for
1851 that value. */
1852
1853tree
1854shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
1855 tree *op0_ptr, *op1_ptr;
1856 tree *restype_ptr;
1857 enum tree_code *rescode_ptr;
1858{
b3694847 1859 tree type;
b30f223b
RS
1860 tree op0 = *op0_ptr;
1861 tree op1 = *op1_ptr;
1862 int unsignedp0, unsignedp1;
1863 int real1, real2;
1864 tree primop0, primop1;
1865 enum tree_code code = *rescode_ptr;
1866
1867 /* Throw away any conversions to wider types
1868 already present in the operands. */
1869
1870 primop0 = get_narrower (op0, &unsignedp0);
1871 primop1 = get_narrower (op1, &unsignedp1);
1872
1873 /* Handle the case that OP0 does not *contain* a conversion
1874 but it *requires* conversion to FINAL_TYPE. */
1875
1876 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
1877 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
1878 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
1879 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
1880
1881 /* If one of the operands must be floated, we cannot optimize. */
1882 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
1883 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
1884
1885 /* If first arg is constant, swap the args (changing operation
5af6001b
RK
1886 so value is preserved), for canonicalization. Don't do this if
1887 the second arg is 0. */
b30f223b 1888
5af6001b
RK
1889 if (TREE_CONSTANT (primop0)
1890 && ! integer_zerop (primop1) && ! real_zerop (primop1))
b30f223b 1891 {
b3694847
SS
1892 tree tem = primop0;
1893 int temi = unsignedp0;
b30f223b
RS
1894 primop0 = primop1;
1895 primop1 = tem;
1896 tem = op0;
1897 op0 = op1;
1898 op1 = tem;
1899 *op0_ptr = op0;
1900 *op1_ptr = op1;
1901 unsignedp0 = unsignedp1;
1902 unsignedp1 = temi;
1903 temi = real1;
1904 real1 = real2;
1905 real2 = temi;
1906
1907 switch (code)
1908 {
1909 case LT_EXPR:
1910 code = GT_EXPR;
1911 break;
1912 case GT_EXPR:
1913 code = LT_EXPR;
1914 break;
1915 case LE_EXPR:
1916 code = GE_EXPR;
1917 break;
1918 case GE_EXPR:
1919 code = LE_EXPR;
1920 break;
6d819282
MK
1921 default:
1922 break;
b30f223b
RS
1923 }
1924 *rescode_ptr = code;
1925 }
1926
1927 /* If comparing an integer against a constant more bits wide,
1928 maybe we can deduce a value of 1 or 0 independent of the data.
1929 Or else truncate the constant now
1930 rather than extend the variable at run time.
1931
1932 This is only interesting if the constant is the wider arg.
1933 Also, it is not safe if the constant is unsigned and the
1934 variable arg is signed, since in this case the variable
1935 would be sign-extended and then regarded as unsigned.
1936 Our technique fails in this case because the lowest/highest
1937 possible unsigned results don't follow naturally from the
1938 lowest/highest possible values of the variable operand.
1939 For just EQ_EXPR and NE_EXPR there is another technique that
1940 could be used: see if the constant can be faithfully represented
1941 in the other operand's type, by truncating it and reextending it
1942 and see if that preserves the constant's value. */
1943
1944 if (!real1 && !real2
1945 && TREE_CODE (primop1) == INTEGER_CST
1946 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
1947 {
1948 int min_gt, max_gt, min_lt, max_lt;
1949 tree maxval, minval;
1950 /* 1 if comparison is nominally unsigned. */
1951 int unsignedp = TREE_UNSIGNED (*restype_ptr);
1952 tree val;
1953
ceef8ce4
NB
1954 type = c_common_signed_or_unsigned_type (unsignedp0,
1955 TREE_TYPE (primop0));
8bbd5685
CW
1956
1957 /* If TYPE is an enumeration, then we need to get its min/max
1958 values from it's underlying integral type, not the enumerated
1959 type itself. */
1960 if (TREE_CODE (type) == ENUMERAL_TYPE)
b0c48229 1961 type = c_common_type_for_size (TYPE_PRECISION (type), unsignedp0);
b30f223b
RS
1962
1963 maxval = TYPE_MAX_VALUE (type);
1964 minval = TYPE_MIN_VALUE (type);
1965
1966 if (unsignedp && !unsignedp0)
ceef8ce4 1967 *restype_ptr = c_common_signed_type (*restype_ptr);
b30f223b
RS
1968
1969 if (TREE_TYPE (primop1) != *restype_ptr)
1970 primop1 = convert (*restype_ptr, primop1);
1971 if (type != *restype_ptr)
1972 {
1973 minval = convert (*restype_ptr, minval);
1974 maxval = convert (*restype_ptr, maxval);
1975 }
1976
1977 if (unsignedp && unsignedp0)
1978 {
1979 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
1980 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
1981 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
1982 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
1983 }
1984 else
1985 {
1986 min_gt = INT_CST_LT (primop1, minval);
1987 max_gt = INT_CST_LT (primop1, maxval);
1988 min_lt = INT_CST_LT (minval, primop1);
1989 max_lt = INT_CST_LT (maxval, primop1);
1990 }
1991
1992 val = 0;
1993 /* This used to be a switch, but Genix compiler can't handle that. */
1994 if (code == NE_EXPR)
1995 {
1996 if (max_lt || min_gt)
a360da3a 1997 val = boolean_true_node;
b30f223b
RS
1998 }
1999 else if (code == EQ_EXPR)
2000 {
2001 if (max_lt || min_gt)
a360da3a 2002 val = boolean_false_node;
b30f223b
RS
2003 }
2004 else if (code == LT_EXPR)
2005 {
2006 if (max_lt)
a360da3a 2007 val = boolean_true_node;
b30f223b 2008 if (!min_lt)
a360da3a 2009 val = boolean_false_node;
b30f223b
RS
2010 }
2011 else if (code == GT_EXPR)
2012 {
2013 if (min_gt)
a360da3a 2014 val = boolean_true_node;
b30f223b 2015 if (!max_gt)
a360da3a 2016 val = boolean_false_node;
b30f223b
RS
2017 }
2018 else if (code == LE_EXPR)
2019 {
2020 if (!max_gt)
a360da3a 2021 val = boolean_true_node;
b30f223b 2022 if (min_gt)
a360da3a 2023 val = boolean_false_node;
b30f223b
RS
2024 }
2025 else if (code == GE_EXPR)
2026 {
2027 if (!min_lt)
a360da3a 2028 val = boolean_true_node;
b30f223b 2029 if (max_lt)
a360da3a 2030 val = boolean_false_node;
b30f223b
RS
2031 }
2032
2033 /* If primop0 was sign-extended and unsigned comparison specd,
2034 we did a signed comparison above using the signed type bounds.
2035 But the comparison we output must be unsigned.
2036
2037 Also, for inequalities, VAL is no good; but if the signed
2038 comparison had *any* fixed result, it follows that the
2039 unsigned comparison just tests the sign in reverse
2040 (positive values are LE, negative ones GE).
2041 So we can generate an unsigned comparison
2042 against an extreme value of the signed type. */
2043
2044 if (unsignedp && !unsignedp0)
2045 {
2046 if (val != 0)
2047 switch (code)
2048 {
2049 case LT_EXPR:
2050 case GE_EXPR:
2051 primop1 = TYPE_MIN_VALUE (type);
2052 val = 0;
2053 break;
2054
2055 case LE_EXPR:
2056 case GT_EXPR:
2057 primop1 = TYPE_MAX_VALUE (type);
2058 val = 0;
2059 break;
6d819282
MK
2060
2061 default:
2062 break;
b30f223b 2063 }
ceef8ce4 2064 type = c_common_unsigned_type (type);
b30f223b
RS
2065 }
2066
5c5b2283 2067 if (TREE_CODE (primop0) != INTEGER_CST)
b30f223b 2068 {
a360da3a 2069 if (val == boolean_false_node)
07be2a23 2070 warning ("comparison is always false due to limited range of data type");
a360da3a 2071 if (val == boolean_true_node)
07be2a23 2072 warning ("comparison is always true due to limited range of data type");
b30f223b
RS
2073 }
2074
2075 if (val != 0)
2076 {
2077 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2078 if (TREE_SIDE_EFFECTS (primop0))
2079 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2080 return val;
2081 }
2082
2083 /* Value is not predetermined, but do the comparison
2084 in the type of the operand that is not constant.
2085 TYPE is already properly set. */
2086 }
2087 else if (real1 && real2
766f6c30
RS
2088 && (TYPE_PRECISION (TREE_TYPE (primop0))
2089 == TYPE_PRECISION (TREE_TYPE (primop1))))
b30f223b
RS
2090 type = TREE_TYPE (primop0);
2091
2092 /* If args' natural types are both narrower than nominal type
2093 and both extend in the same manner, compare them
2094 in the type of the wider arg.
2095 Otherwise must actually extend both to the nominal
2096 common type lest different ways of extending
2097 alter the result.
2098 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2099
2100 else if (unsignedp0 == unsignedp1 && real1 == real2
2101 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2102 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2103 {
2104 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
ceef8ce4
NB
2105 type = c_common_signed_or_unsigned_type (unsignedp0
2106 || TREE_UNSIGNED (*restype_ptr),
2107 type);
b30f223b
RS
2108 /* Make sure shorter operand is extended the right way
2109 to match the longer operand. */
ceef8ce4
NB
2110 primop0
2111 = convert (c_common_signed_or_unsigned_type (unsignedp0,
2112 TREE_TYPE (primop0)),
2113 primop0);
2114 primop1
2115 = convert (c_common_signed_or_unsigned_type (unsignedp1,
2116 TREE_TYPE (primop1)),
2117 primop1);
b30f223b
RS
2118 }
2119 else
2120 {
2121 /* Here we must do the comparison on the nominal type
2122 using the args exactly as we received them. */
2123 type = *restype_ptr;
2124 primop0 = op0;
2125 primop1 = op1;
2126
2127 if (!real1 && !real2 && integer_zerop (primop1)
597681f6 2128 && TREE_UNSIGNED (*restype_ptr))
b30f223b
RS
2129 {
2130 tree value = 0;
2131 switch (code)
2132 {
2133 case GE_EXPR:
5af6001b
RK
2134 /* All unsigned values are >= 0, so we warn if extra warnings
2135 are requested. However, if OP0 is a constant that is
2136 >= 0, the signedness of the comparison isn't an issue,
2137 so suppress the warning. */
2c492eef 2138 if (extra_warnings && !in_system_header
5af6001b 2139 && ! (TREE_CODE (primop0) == INTEGER_CST
ceef8ce4 2140 && ! TREE_OVERFLOW (convert (c_common_signed_type (type),
5af6001b 2141 primop0))))
07be2a23 2142 warning ("comparison of unsigned expression >= 0 is always true");
a360da3a 2143 value = boolean_true_node;
b30f223b
RS
2144 break;
2145
2146 case LT_EXPR:
2c492eef 2147 if (extra_warnings && !in_system_header
5af6001b 2148 && ! (TREE_CODE (primop0) == INTEGER_CST
ceef8ce4 2149 && ! TREE_OVERFLOW (convert (c_common_signed_type (type),
5af6001b 2150 primop0))))
07be2a23 2151 warning ("comparison of unsigned expression < 0 is always false");
a360da3a 2152 value = boolean_false_node;
6d819282
MK
2153 break;
2154
2155 default:
2156 break;
b30f223b
RS
2157 }
2158
2159 if (value != 0)
2160 {
2161 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2162 if (TREE_SIDE_EFFECTS (primop0))
2163 return build (COMPOUND_EXPR, TREE_TYPE (value),
2164 primop0, value);
2165 return value;
2166 }
2167 }
2168 }
2169
2170 *op0_ptr = convert (type, primop0);
2171 *op1_ptr = convert (type, primop1);
2172
a360da3a 2173 *restype_ptr = boolean_type_node;
b30f223b
RS
2174
2175 return 0;
2176}
2177\f
7552da58
JJ
2178/* Return a tree for the sum or difference (RESULTCODE says which)
2179 of pointer PTROP and integer INTOP. */
2180
2181tree
2182pointer_int_sum (resultcode, ptrop, intop)
2183 enum tree_code resultcode;
2184 tree ptrop, intop;
2185{
2186 tree size_exp;
2187
2188 tree result;
2189 tree folded;
2190
2191 /* The result is a pointer of the same type that is being added. */
2192
2193 tree result_type = TREE_TYPE (ptrop);
2194
2195 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2196 {
2197 if (pedantic || warn_pointer_arith)
2198 pedwarn ("pointer of type `void *' used in arithmetic");
2199 size_exp = integer_one_node;
2200 }
2201 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2202 {
2203 if (pedantic || warn_pointer_arith)
2204 pedwarn ("pointer to a function used in arithmetic");
2205 size_exp = integer_one_node;
2206 }
2207 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
2208 {
2209 if (pedantic || warn_pointer_arith)
2210 pedwarn ("pointer to member function used in arithmetic");
2211 size_exp = integer_one_node;
2212 }
2213 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
2214 {
2215 if (pedantic || warn_pointer_arith)
2216 pedwarn ("pointer to a member used in arithmetic");
2217 size_exp = integer_one_node;
2218 }
2219 else
2220 size_exp = size_in_bytes (TREE_TYPE (result_type));
2221
2222 /* If what we are about to multiply by the size of the elements
2223 contains a constant term, apply distributive law
2224 and multiply that constant term separately.
2225 This helps produce common subexpressions. */
2226
2227 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2228 && ! TREE_CONSTANT (intop)
2229 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2230 && TREE_CONSTANT (size_exp)
2231 /* If the constant comes from pointer subtraction,
2232 skip this optimization--it would cause an error. */
2233 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2234 /* If the constant is unsigned, and smaller than the pointer size,
2235 then we must skip this optimization. This is because it could cause
2236 an overflow error if the constant is negative but INTOP is not. */
2237 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2238 || (TYPE_PRECISION (TREE_TYPE (intop))
2239 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2240 {
2241 enum tree_code subcode = resultcode;
2242 tree int_type = TREE_TYPE (intop);
2243 if (TREE_CODE (intop) == MINUS_EXPR)
2244 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2245 /* Convert both subexpression types to the type of intop,
2246 because weird cases involving pointer arithmetic
2247 can result in a sum or difference with different type args. */
2248 ptrop = build_binary_op (subcode, ptrop,
2249 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2250 intop = convert (int_type, TREE_OPERAND (intop, 0));
2251 }
2252
2253 /* Convert the integer argument to a type the same size as sizetype
2254 so the multiply won't overflow spuriously. */
2255
2256 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
2257 || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype))
b0c48229
NB
2258 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
2259 TREE_UNSIGNED (sizetype)), intop);
7552da58
JJ
2260
2261 /* Replace the integer argument with a suitable product by the object size.
2262 Do this multiplication as signed, then convert to the appropriate
2263 pointer type (actually unsigned integral). */
2264
2265 intop = convert (result_type,
2266 build_binary_op (MULT_EXPR, intop,
2267 convert (TREE_TYPE (intop), size_exp), 1));
2268
2269 /* Create the sum or difference. */
2270
2271 result = build (resultcode, result_type, ptrop, intop);
2272
2273 folded = fold (result);
2274 if (folded == result)
2275 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2276 return folded;
2277}
2278\f
b30f223b
RS
2279/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2280 or validate its data type for an `if' or `while' statement or ?..: exp.
2281
2282 This preparation consists of taking the ordinary
2283 representation of an expression expr and producing a valid tree
2284 boolean expression describing whether expr is nonzero. We could
a360da3a 2285 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
b30f223b
RS
2286 but we optimize comparisons, &&, ||, and !.
2287
a360da3a 2288 The resulting type should always be `boolean_type_node'. */
b30f223b
RS
2289
2290tree
78ef5b89 2291c_common_truthvalue_conversion (expr)
b30f223b
RS
2292 tree expr;
2293{
257e61ed
RS
2294 if (TREE_CODE (expr) == ERROR_MARK)
2295 return expr;
2296
d7c83727 2297#if 0 /* This appears to be wrong for C++. */
257e61ed
RS
2298 /* These really should return error_mark_node after 2.4 is stable.
2299 But not all callers handle ERROR_MARK properly. */
2300 switch (TREE_CODE (TREE_TYPE (expr)))
2301 {
2302 case RECORD_TYPE:
2303 error ("struct type value used where scalar is required");
a360da3a 2304 return boolean_false_node;
257e61ed
RS
2305
2306 case UNION_TYPE:
2307 error ("union type value used where scalar is required");
a360da3a 2308 return boolean_false_node;
257e61ed
RS
2309
2310 case ARRAY_TYPE:
2311 error ("array type value used where scalar is required");
a360da3a 2312 return boolean_false_node;
257e61ed
RS
2313
2314 default:
2315 break;
2316 }
d7c83727 2317#endif /* 0 */
257e61ed 2318
b30f223b
RS
2319 switch (TREE_CODE (expr))
2320 {
b30f223b 2321 case EQ_EXPR:
b30f223b
RS
2322 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2323 case TRUTH_ANDIF_EXPR:
2324 case TRUTH_ORIF_EXPR:
2325 case TRUTH_AND_EXPR:
2326 case TRUTH_OR_EXPR:
9379fac9 2327 case TRUTH_XOR_EXPR:
1180eb10 2328 case TRUTH_NOT_EXPR:
a360da3a
JM
2329 TREE_TYPE (expr) = boolean_type_node;
2330 return expr;
18c0f675 2331
b30f223b
RS
2332 case ERROR_MARK:
2333 return expr;
2334
2335 case INTEGER_CST:
a360da3a 2336 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
b30f223b
RS
2337
2338 case REAL_CST:
a360da3a 2339 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
b30f223b
RS
2340
2341 case ADDR_EXPR:
4fe9b91c 2342 /* If we are taking the address of an external decl, it might be zero
fc0c675f 2343 if it is weak, so we cannot optimize. */
2f939d94 2344 if (DECL_P (TREE_OPERAND (expr, 0))
fc0c675f
RK
2345 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2346 break;
2347
b30f223b 2348 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
a360da3a
JM
2349 return build (COMPOUND_EXPR, boolean_type_node,
2350 TREE_OPERAND (expr, 0), boolean_true_node);
b30f223b 2351 else
a360da3a 2352 return boolean_true_node;
b30f223b 2353
766f6c30 2354 case COMPLEX_EXPR:
f0b996c5 2355 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
b839fb3f 2356 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
78ef5b89
NB
2357 c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)),
2358 c_common_truthvalue_conversion (TREE_OPERAND (expr, 1)),
766f6c30
RS
2359 0);
2360
b30f223b
RS
2361 case NEGATE_EXPR:
2362 case ABS_EXPR:
2363 case FLOAT_EXPR:
2364 case FFS_EXPR:
2365 /* These don't change whether an object is non-zero or zero. */
78ef5b89 2366 return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0));
b30f223b
RS
2367
2368 case LROTATE_EXPR:
2369 case RROTATE_EXPR:
2370 /* These don't change whether an object is zero or non-zero, but
2371 we can't ignore them if their second arg has side-effects. */
2372 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
a360da3a 2373 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
78ef5b89 2374 c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)));
b30f223b 2375 else
78ef5b89 2376 return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0));
b57062ca 2377
b30f223b
RS
2378 case COND_EXPR:
2379 /* Distribute the conversion into the arms of a COND_EXPR. */
a360da3a 2380 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
78ef5b89
NB
2381 c_common_truthvalue_conversion (TREE_OPERAND (expr, 1)),
2382 c_common_truthvalue_conversion (TREE_OPERAND (expr, 2))));
b30f223b
RS
2383
2384 case CONVERT_EXPR:
2385 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2386 since that affects how `default_conversion' will behave. */
2387 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2388 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2389 break;
0f41302f 2390 /* fall through... */
b30f223b
RS
2391 case NOP_EXPR:
2392 /* If this is widening the argument, we can ignore it. */
2393 if (TYPE_PRECISION (TREE_TYPE (expr))
2394 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
78ef5b89 2395 return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0));
b30f223b
RS
2396 break;
2397
b30f223b 2398 case MINUS_EXPR:
71925bc0
RS
2399 /* Perhaps reduce (x - y) != 0 to (x != y). The expressions
2400 aren't guaranteed to the be same for modes that can represent
2401 infinity, since if x and y are both +infinity, or both
2402 -infinity, then x - y is not a number.
2403
2404 Note that this transformation is safe when x or y is NaN.
2405 (x - y) is then NaN, and both (x - y) != 0 and x != y will
2406 be false. */
2407 if (HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0)))))
f87550e0 2408 break;
0f41302f 2409 /* fall through... */
f87550e0 2410 case BIT_XOR_EXPR:
d7c83727 2411 /* This and MINUS_EXPR can be changed into a comparison of the
f87550e0 2412 two objects. */
b30f223b
RS
2413 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2414 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2415 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2416 TREE_OPERAND (expr, 1), 1);
2417 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2418 fold (build1 (NOP_EXPR,
2419 TREE_TYPE (TREE_OPERAND (expr, 0)),
2420 TREE_OPERAND (expr, 1))), 1);
e2aab13d 2421
fb48b1f0 2422 case BIT_AND_EXPR:
58cee643
RK
2423 if (integer_onep (TREE_OPERAND (expr, 1))
2424 && TREE_TYPE (expr) != boolean_type_node)
2425 /* Using convert here would cause infinite recursion. */
2426 return build1 (NOP_EXPR, boolean_type_node, expr);
2427 break;
fb48b1f0 2428
e2aab13d
RS
2429 case MODIFY_EXPR:
2430 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2431 warning ("suggest parentheses around assignment used as truth value");
2432 break;
b57062ca 2433
6d819282
MK
2434 default:
2435 break;
b30f223b
RS
2436 }
2437
f0b996c5 2438 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
f0b8d9aa 2439 {
78ef5b89 2440 tree t = save_expr (expr);
f0b8d9aa
AS
2441 return (build_binary_op
2442 ((TREE_SIDE_EFFECTS (expr)
2443 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
78ef5b89
NB
2444 c_common_truthvalue_conversion (build_unary_op (REALPART_EXPR, t, 0)),
2445 c_common_truthvalue_conversion (build_unary_op (IMAGPART_EXPR, t, 0)),
f0b8d9aa
AS
2446 0));
2447 }
f0b996c5 2448
b30f223b
RS
2449 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2450}
2451\f
fc2aaf30
JM
2452static tree builtin_function_2 PARAMS ((const char *, const char *, tree, tree,
2453 int, enum built_in_class, int, int,
2454 int));
2455
0b73773c
NH
2456/* Make a variant type in the proper way for C/C++, propagating qualifiers
2457 down to the element type of an array. */
2458
2459tree
3932261a 2460c_build_qualified_type (type, type_quals)
0b73773c 2461 tree type;
3932261a 2462 int type_quals;
0b73773c 2463{
3932261a
MM
2464 /* A restrict-qualified pointer type must be a pointer to object or
2465 incomplete type. Note that the use of POINTER_TYPE_P also allows
2466 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
2467 the C++ front-end also use POINTER_TYPE for pointer-to-member
2468 values, so even though it should be illegal to use `restrict'
2469 with such an entity we don't flag that here. Thus, special case
2470 code for that case is required in the C++ front-end. */
2471 if ((type_quals & TYPE_QUAL_RESTRICT)
2472 && (!POINTER_TYPE_P (type)
2473 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
2474 {
2475 error ("invalid use of `restrict'");
2476 type_quals &= ~TYPE_QUAL_RESTRICT;
2477 }
2478
0b73773c 2479 if (TREE_CODE (type) == ARRAY_TYPE)
3932261a
MM
2480 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
2481 type_quals),
3ab1999b 2482 TYPE_DOMAIN (type));
3932261a
MM
2483 return build_qualified_type (type, type_quals);
2484}
2485
2486/* Apply the TYPE_QUALS to the new DECL. */
2487
2488void
2489c_apply_type_quals_to_decl (type_quals, decl)
2490 int type_quals;
2491 tree decl;
2492{
a6496605
MM
2493 if ((type_quals & TYPE_QUAL_CONST)
2494 || (TREE_TYPE (decl)
2495 && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE))
3932261a
MM
2496 TREE_READONLY (decl) = 1;
2497 if (type_quals & TYPE_QUAL_VOLATILE)
2498 {
2499 TREE_SIDE_EFFECTS (decl) = 1;
2500 TREE_THIS_VOLATILE (decl) = 1;
2501 }
6946bc60 2502 if (type_quals & TYPE_QUAL_RESTRICT)
3932261a 2503 {
6946bc60
MM
2504 if (!TREE_TYPE (decl)
2505 || !POINTER_TYPE_P (TREE_TYPE (decl))
2506 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
2507 error ("invalid use of `restrict'");
2508 else if (flag_strict_aliasing)
3c5ad1ff
RK
2509 /* Indicate we need to make a unique alias set for this pointer.
2510 We can't do it here because it might be pointing to an
2511 incomplete type. */
2512 DECL_POINTER_ALIAS_SET (decl) = -2;
3932261a
MM
2513 }
2514}
2515
41472af8 2516/* Return the typed-based alias set for T, which may be an expression
3bdf5ad1 2517 or a type. Return -1 if we don't do anything special. */
41472af8 2518
3bdf5ad1 2519HOST_WIDE_INT
8ac61af7 2520c_common_get_alias_set (t)
41472af8
MM
2521 tree t;
2522{
08bc2431 2523 tree u;
604bb87d
DB
2524
2525 /* We know nothing about vector types */
2526 if (TREE_CODE (t) == VECTOR_TYPE)
2527 return 0;
2528
08bc2431
MM
2529 /* Permit type-punning when accessing a union, provided the access
2530 is directly through the union. For example, this code does not
2531 permit taking the address of a union member and then storing
2532 through it. Even the type-punning allowed here is a GCC
2533 extension, albeit a common and useful one; the C standard says
2534 that such accesses have implementation-defined behavior. */
2535 for (u = t;
2536 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
2537 u = TREE_OPERAND (u, 0))
2538 if (TREE_CODE (u) == COMPONENT_REF
2539 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
2540 return 0;
ece32014 2541
3bdf5ad1 2542 /* If this is a char *, the ANSI C standard says it can alias
f824e5c3
RK
2543 anything. Note that all references need do this. */
2544 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'r'
2545 && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
2546 && TYPE_PRECISION (TREE_TYPE (t)) == TYPE_PRECISION (char_type_node))
3bdf5ad1 2547 return 0;
3932261a 2548
3bdf5ad1
RK
2549 /* That's all the expressions we handle specially. */
2550 if (! TYPE_P (t))
2551 return -1;
41472af8 2552
f824e5c3
RK
2553 /* The C standard specifically allows aliasing between signed and
2554 unsigned variants of the same type. We treat the signed
2555 variant as canonical. */
2556 if (TREE_CODE (t) == INTEGER_TYPE && TREE_UNSIGNED (t))
8f215dce 2557 {
ceef8ce4 2558 tree t1 = c_common_signed_type (t);
f824e5c3 2559
8f215dce
JJ
2560 /* t1 == t can happen for boolean nodes which are always unsigned. */
2561 if (t1 != t)
2562 return get_alias_set (t1);
2563 }
3bdf5ad1 2564 else if (POINTER_TYPE_P (t))
02af3af6 2565 {
3bdf5ad1 2566 tree t1;
02af3af6
MS
2567
2568 /* Unfortunately, there is no canonical form of a pointer type.
2569 In particular, if we have `typedef int I', then `int *', and
2570 `I *' are different types. So, we have to pick a canonical
2571 representative. We do this below.
d125d268 2572
b61148dd
MM
2573 Technically, this approach is actually more conservative that
2574 it needs to be. In particular, `const int *' and `int *'
684d9f3b 2575 should be in different alias sets, according to the C and C++
b61148dd
MM
2576 standard, since their types are not the same, and so,
2577 technically, an `int **' and `const int **' cannot point at
2578 the same thing.
2579
2580 But, the standard is wrong. In particular, this code is
2581 legal C++:
2582
2583 int *ip;
2584 int **ipp = &ip;
68981e3a 2585 const int* const* cipp = &ipp;
b61148dd
MM
2586
2587 And, it doesn't make sense for that to be legal unless you
2588 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
2589 the pointed-to types. This issue has been reported to the
2590 C++ committee. */
12e1243e 2591 t1 = build_type_no_quals (t);
3bdf5ad1
RK
2592 if (t1 != t)
2593 return get_alias_set (t1);
02af3af6 2594 }
ece32014 2595
3bdf5ad1 2596 return -1;
41472af8 2597}
0213a355
JM
2598\f
2599/* Implement the __alignof keyword: Return the minimum required
2600 alignment of TYPE, measured in bytes. */
2601
2602tree
2603c_alignof (type)
2604 tree type;
2605{
2606 enum tree_code code = TREE_CODE (type);
2607 tree t;
2608
2609 /* In C++, sizeof applies to the referent. Handle alignof the same way. */
2610 if (code == REFERENCE_TYPE)
2611 {
2612 type = TREE_TYPE (type);
2613 code = TREE_CODE (type);
2614 }
2615
2616 if (code == FUNCTION_TYPE)
2617 t = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
2618 else if (code == VOID_TYPE || code == ERROR_MARK)
2619 t = size_one_node;
2620 else if (!COMPLETE_TYPE_P (type))
2621 {
2622 error ("__alignof__ applied to an incomplete type");
2623 t = size_zero_node;
2624 }
2625 else
2626 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
2627
2628 return fold (build1 (NOP_EXPR, c_size_type_node, t));
2629}
2630
2631/* Implement the __alignof keyword: Return the minimum required
2632 alignment of EXPR, measured in bytes. For VAR_DECL's and
2633 FIELD_DECL's return DECL_ALIGN (which can be set from an
2634 "aligned" __attribute__ specification). */
7f4edbcb 2635
0213a355
JM
2636tree
2637c_alignof_expr (expr)
2638 tree expr;
2639{
2640 tree t;
2641
2642 if (TREE_CODE (expr) == VAR_DECL)
2643 t = size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
2644
2645 else if (TREE_CODE (expr) == COMPONENT_REF
2646 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2647 {
2648 error ("`__alignof' applied to a bit-field");
2649 t = size_one_node;
2650 }
2651 else if (TREE_CODE (expr) == COMPONENT_REF
173bf5be 2652 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
0213a355
JM
2653 t = size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
2654
2655 else if (TREE_CODE (expr) == INDIRECT_REF)
2656 {
2657 tree t = TREE_OPERAND (expr, 0);
2658 tree best = t;
2659 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
2660
2661 while (TREE_CODE (t) == NOP_EXPR
173bf5be 2662 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
0213a355
JM
2663 {
2664 int thisalign;
2665
2666 t = TREE_OPERAND (t, 0);
2667 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
2668 if (thisalign > bestalign)
2669 best = t, bestalign = thisalign;
2670 }
2671 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
2672 }
2673 else
2674 return c_alignof (TREE_TYPE (expr));
2675
2676 return fold (build1 (NOP_EXPR, c_size_type_node, t));
2677}
2678\f
7f4edbcb 2679/* Build tree nodes and builtin functions common to both C and C++ language
6bcedb4e 2680 frontends. */
3bdf5ad1 2681
7f4edbcb 2682void
6bcedb4e 2683c_common_nodes_and_builtins ()
7f4edbcb 2684{
10841285
MM
2685 enum builtin_type
2686 {
2687#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
2688#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
2689#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
2690#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
2691#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
2692#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
2693#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
2694#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
2695#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
2696#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
2697#include "builtin-types.def"
2698#undef DEF_PRIMITIVE_TYPE
2699#undef DEF_FUNCTION_TYPE_0
2700#undef DEF_FUNCTION_TYPE_1
2701#undef DEF_FUNCTION_TYPE_2
2702#undef DEF_FUNCTION_TYPE_3
2703#undef DEF_FUNCTION_TYPE_4
2704#undef DEF_FUNCTION_TYPE_VAR_0
2705#undef DEF_FUNCTION_TYPE_VAR_1
2706#undef DEF_FUNCTION_TYPE_VAR_2
2707#undef DEF_POINTER_TYPE
2708 BT_LAST
2709 };
2710
2711 typedef enum builtin_type builtin_type;
2712
173bf5be 2713 tree builtin_types[(int) BT_LAST];
eaa7c03f
JM
2714 int wchar_type_size;
2715 tree array_domain_type;
9f720c3e 2716 tree va_list_ref_type_node;
daf68dd7 2717 tree va_list_arg_type_node;
d3707adb 2718
eaa7c03f 2719 /* Define `int' and `char' first so that dbx will output them first. */
6496a589 2720 record_builtin_type (RID_INT, NULL, integer_type_node);
eaa7c03f
JM
2721 record_builtin_type (RID_CHAR, "char", char_type_node);
2722
2723 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
2724 "unsigned long", "long long unsigned" and "unsigned short" were in C++
2725 but not C. Are the conditionals here needed? */
2726 if (c_language == clk_cplusplus)
6496a589 2727 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
eaa7c03f
JM
2728 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
2729 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
2730 record_builtin_type (RID_MAX, "long unsigned int",
2731 long_unsigned_type_node);
2732 if (c_language == clk_cplusplus)
2733 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
2734 record_builtin_type (RID_MAX, "long long int",
2735 long_long_integer_type_node);
2736 record_builtin_type (RID_MAX, "long long unsigned int",
2737 long_long_unsigned_type_node);
2738 if (c_language == clk_cplusplus)
2739 record_builtin_type (RID_MAX, "long long unsigned",
2740 long_long_unsigned_type_node);
2741 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
2742 record_builtin_type (RID_MAX, "short unsigned int",
2743 short_unsigned_type_node);
2744 if (c_language == clk_cplusplus)
2745 record_builtin_type (RID_MAX, "unsigned short",
2746 short_unsigned_type_node);
2747
2748 /* Define both `signed char' and `unsigned char'. */
2749 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
2750 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
2751
b0c48229
NB
2752 /* These are types that c_common_type_for_size and
2753 c_common_type_for_mode use. */
43577e6b
NB
2754 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
2755 intQI_type_node));
2756 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
2757 intHI_type_node));
2758 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
2759 intSI_type_node));
2760 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
2761 intDI_type_node));
eaa7c03f 2762#if HOST_BITS_PER_WIDE_INT >= 64
43577e6b
NB
2763 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
2764 get_identifier ("__int128_t"),
2765 intTI_type_node));
eaa7c03f 2766#endif
43577e6b
NB
2767 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
2768 unsigned_intQI_type_node));
2769 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
2770 unsigned_intHI_type_node));
2771 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
2772 unsigned_intSI_type_node));
2773 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
2774 unsigned_intDI_type_node));
eaa7c03f 2775#if HOST_BITS_PER_WIDE_INT >= 64
43577e6b
NB
2776 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
2777 get_identifier ("__uint128_t"),
2778 unsigned_intTI_type_node));
eaa7c03f
JM
2779#endif
2780
2781 /* Create the widest literal types. */
2782 widest_integer_literal_type_node
2783 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
43577e6b
NB
2784 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
2785 widest_integer_literal_type_node));
eaa7c03f
JM
2786
2787 widest_unsigned_literal_type_node
2788 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
43577e6b
NB
2789 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
2790 widest_unsigned_literal_type_node));
eaa7c03f
JM
2791
2792 /* `unsigned long' is the standard type for sizeof.
2793 Note that stddef.h uses `unsigned long',
2794 and this must agree, even if long and int are the same size. */
2795 c_size_type_node =
2796 TREE_TYPE (identifier_global_value (get_identifier (SIZE_TYPE)));
ceef8ce4 2797 signed_size_type_node = c_common_signed_type (c_size_type_node);
eaa7c03f
JM
2798 set_sizetype (c_size_type_node);
2799
2800 build_common_tree_nodes_2 (flag_short_double);
2801
6496a589
KG
2802 record_builtin_type (RID_FLOAT, NULL, float_type_node);
2803 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
eaa7c03f
JM
2804 record_builtin_type (RID_MAX, "long double", long_double_type_node);
2805
43577e6b
NB
2806 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
2807 get_identifier ("complex int"),
2808 complex_integer_type_node));
2809 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
2810 get_identifier ("complex float"),
2811 complex_float_type_node));
2812 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
2813 get_identifier ("complex double"),
2814 complex_double_type_node));
2815 (*lang_hooks.decls.pushdecl)
2816 (build_decl (TYPE_DECL, get_identifier ("complex long double"),
2817 complex_long_double_type_node));
eaa7c03f 2818
03f10472
TM
2819 /* Types which are common to the fortran compiler and libf2c. When
2820 changing these, you also need to be concerned with f/com.h. */
2821
2822 if (TYPE_PRECISION (float_type_node)
2823 == TYPE_PRECISION (long_integer_type_node))
2824 {
2825 g77_integer_type_node = long_integer_type_node;
2826 g77_uinteger_type_node = long_unsigned_type_node;
2827 }
2828 else if (TYPE_PRECISION (float_type_node)
2829 == TYPE_PRECISION (integer_type_node))
2830 {
2831 g77_integer_type_node = integer_type_node;
2832 g77_uinteger_type_node = unsigned_type_node;
2833 }
2834 else
2835 g77_integer_type_node = g77_uinteger_type_node = NULL_TREE;
2836
2837 if (g77_integer_type_node != NULL_TREE)
2838 {
2839 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
2840 get_identifier ("__g77_integer"),
2841 g77_integer_type_node));
2842 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
2843 get_identifier ("__g77_uinteger"),
2844 g77_uinteger_type_node));
2845 }
2846
2847 if (TYPE_PRECISION (float_type_node) * 2
2848 == TYPE_PRECISION (long_integer_type_node))
2849 {
2850 g77_longint_type_node = long_integer_type_node;
2851 g77_ulongint_type_node = long_unsigned_type_node;
2852 }
2853 else if (TYPE_PRECISION (float_type_node) * 2
2854 == TYPE_PRECISION (long_long_integer_type_node))
2855 {
2856 g77_longint_type_node = long_long_integer_type_node;
2857 g77_ulongint_type_node = long_long_unsigned_type_node;
2858 }
2859 else
2860 g77_longint_type_node = g77_ulongint_type_node = NULL_TREE;
2861
2862 if (g77_longint_type_node != NULL_TREE)
2863 {
2864 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
2865 get_identifier ("__g77_longint"),
2866 g77_longint_type_node));
2867 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
2868 get_identifier ("__g77_ulongint"),
2869 g77_ulongint_type_node));
2870 }
2871
6496a589 2872 record_builtin_type (RID_VOID, NULL, void_type_node);
eaa7c03f 2873
10841285
MM
2874 void_zero_node = build_int_2 (0, 0);
2875 TREE_TYPE (void_zero_node) = void_type_node;
2876
eaa7c03f
JM
2877 void_list_node = build_void_list_node ();
2878
2879 /* Make a type to be the domain of a few array types
2880 whose domains don't really matter.
2881 200 is small enough that it always fits in size_t
2882 and large enough that it can hold most function names for the
2883 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
2884 array_domain_type = build_index_type (size_int (200));
2885
2886 /* Make a type for arrays of characters.
2887 With luck nothing will ever really depend on the length of this
2888 array type. */
2889 char_array_type_node
2890 = build_array_type (char_type_node, array_domain_type);
2891
2892 /* Likewise for arrays of ints. */
2893 int_array_type_node
2894 = build_array_type (integer_type_node, array_domain_type);
2895
10841285
MM
2896 string_type_node = build_pointer_type (char_type_node);
2897 const_string_type_node
2898 = build_pointer_type (build_qualified_type
2899 (char_type_node, TYPE_QUAL_CONST));
2900
f6155fda 2901 (*targetm.init_builtins) ();
eaa7c03f
JM
2902
2903 /* This is special for C++ so functions can be overloaded. */
a11eba95 2904 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
eaa7c03f
JM
2905 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
2906 wchar_type_size = TYPE_PRECISION (wchar_type_node);
2907 if (c_language == clk_cplusplus)
2908 {
2909 if (TREE_UNSIGNED (wchar_type_node))
2910 wchar_type_node = make_unsigned_type (wchar_type_size);
2911 else
2912 wchar_type_node = make_signed_type (wchar_type_size);
2913 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
2914 }
2915 else
2916 {
ceef8ce4
NB
2917 signed_wchar_type_node = c_common_signed_type (wchar_type_node);
2918 unsigned_wchar_type_node = c_common_unsigned_type (wchar_type_node);
eaa7c03f
JM
2919 }
2920
2921 /* This is for wide string constants. */
2922 wchar_array_type_node
2923 = build_array_type (wchar_type_node, array_domain_type);
2924
5fd8e536
JM
2925 wint_type_node =
2926 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
2927
2928 intmax_type_node =
2929 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
2930 uintmax_type_node =
2931 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
2932
2933 default_function_type = build_function_type (integer_type_node, NULL_TREE);
2934 ptrdiff_type_node
2935 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
ceef8ce4 2936 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5fd8e536 2937
43577e6b
NB
2938 (*lang_hooks.decls.pushdecl)
2939 (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
2940 va_list_type_node));
daf68dd7 2941
43577e6b
NB
2942 (*lang_hooks.decls.pushdecl)
2943 (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"),
2944 ptrdiff_type_node));
29ae8f10 2945
43577e6b
NB
2946 (*lang_hooks.decls.pushdecl)
2947 (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"),
2948 sizetype));
29ae8f10 2949
daf68dd7 2950 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
9f720c3e
GK
2951 {
2952 va_list_arg_type_node = va_list_ref_type_node =
2953 build_pointer_type (TREE_TYPE (va_list_type_node));
2954 }
daf68dd7 2955 else
9f720c3e
GK
2956 {
2957 va_list_arg_type_node = va_list_type_node;
2958 va_list_ref_type_node = build_reference_type (va_list_type_node);
2959 }
2960
10841285
MM
2961#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
2962 builtin_types[(int) ENUM] = VALUE;
2963#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
2964 builtin_types[(int) ENUM] \
2965 = build_function_type (builtin_types[(int) RETURN], \
2966 void_list_node);
2967#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
2968 builtin_types[(int) ENUM] \
2969 = build_function_type (builtin_types[(int) RETURN], \
2970 tree_cons (NULL_TREE, \
2971 builtin_types[(int) ARG1], \
2972 void_list_node));
2973#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
2974 builtin_types[(int) ENUM] \
2975 = build_function_type \
2976 (builtin_types[(int) RETURN], \
2977 tree_cons (NULL_TREE, \
2978 builtin_types[(int) ARG1], \
2979 tree_cons (NULL_TREE, \
2980 builtin_types[(int) ARG2], \
2981 void_list_node)));
2982#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
2983 builtin_types[(int) ENUM] \
2984 = build_function_type \
2985 (builtin_types[(int) RETURN], \
2986 tree_cons (NULL_TREE, \
2987 builtin_types[(int) ARG1], \
2988 tree_cons (NULL_TREE, \
2989 builtin_types[(int) ARG2], \
2990 tree_cons (NULL_TREE, \
2991 builtin_types[(int) ARG3], \
2992 void_list_node))));
2993#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
2994 builtin_types[(int) ENUM] \
2995 = build_function_type \
2996 (builtin_types[(int) RETURN], \
2997 tree_cons (NULL_TREE, \
2998 builtin_types[(int) ARG1], \
2999 tree_cons (NULL_TREE, \
3000 builtin_types[(int) ARG2], \
3001 tree_cons \
3002 (NULL_TREE, \
3003 builtin_types[(int) ARG3], \
3004 tree_cons (NULL_TREE, \
3005 builtin_types[(int) ARG4], \
3006 void_list_node)))));
3007#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
3008 builtin_types[(int) ENUM] \
3009 = build_function_type (builtin_types[(int) RETURN], NULL_TREE);
3010#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
3011 builtin_types[(int) ENUM] \
3012 = build_function_type (builtin_types[(int) RETURN], \
3013 tree_cons (NULL_TREE, \
3014 builtin_types[(int) ARG1], \
ad3fd36f
KG
3015 NULL_TREE));
3016
10841285
MM
3017#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
3018 builtin_types[(int) ENUM] \
3019 = build_function_type \
3020 (builtin_types[(int) RETURN], \
3021 tree_cons (NULL_TREE, \
3022 builtin_types[(int) ARG1], \
3023 tree_cons (NULL_TREE, \
3024 builtin_types[(int) ARG2], \
3025 NULL_TREE)));
3026#define DEF_POINTER_TYPE(ENUM, TYPE) \
3027 builtin_types[(int) ENUM] \
3028 = build_pointer_type (builtin_types[(int) TYPE]);
3029#include "builtin-types.def"
3030#undef DEF_PRIMITIVE_TYPE
3031#undef DEF_FUNCTION_TYPE_1
3032#undef DEF_FUNCTION_TYPE_2
3033#undef DEF_FUNCTION_TYPE_3
3034#undef DEF_FUNCTION_TYPE_4
3035#undef DEF_FUNCTION_TYPE_VAR_0
3036#undef DEF_FUNCTION_TYPE_VAR_1
3037#undef DEF_POINTER_TYPE
3038
3039#define DEF_BUILTIN(ENUM, NAME, CLASS, \
3040 TYPE, LIBTYPE, BOTH_P, FALLBACK_P, NONANSI_P) \
3041 if (NAME) \
3042 { \
3043 tree decl; \
3044 \
3045 if (strncmp (NAME, "__builtin_", strlen ("__builtin_")) != 0) \
3046 abort (); \
3047 \
3048 if (!BOTH_P) \
3049 decl = builtin_function (NAME, builtin_types[TYPE], ENUM, \
3050 CLASS, \
3051 (FALLBACK_P \
3052 ? (NAME + strlen ("__builtin_")) \
3053 : NULL)); \
3054 else \
3055 decl = builtin_function_2 (NAME, \
3056 NAME + strlen ("__builtin_"), \
3057 builtin_types[TYPE], \
3058 builtin_types[LIBTYPE], \
3059 ENUM, \
3060 CLASS, \
3061 FALLBACK_P, \
3062 NONANSI_P, \
3063 /*noreturn_p=*/0); \
3064 \
3065 built_in_decls[(int) ENUM] = decl; \
3066 }
3067#include "builtins.def"
3068#undef DEF_BUILTIN
52a11cbf 3069
796cdb65 3070 /* Declare _exit and _Exit just to mark them as non-returning. */
10841285
MM
3071 builtin_function_2 (NULL, "_exit", NULL_TREE,
3072 builtin_types[BT_FN_VOID_INT],
fc2aaf30 3073 0, NOT_BUILT_IN, 0, 1, 1);
10841285
MM
3074 builtin_function_2 (NULL, "_Exit", NULL_TREE,
3075 builtin_types[BT_FN_VOID_INT],
796cdb65 3076 0, NOT_BUILT_IN, 0, !flag_isoc99, 1);
fc2aaf30 3077
fc2aaf30
JM
3078 /* Declare these functions non-returning
3079 to avoid spurious "control drops through" warnings. */
6496a589 3080 builtin_function_2 (NULL, "abort",
fc2aaf30 3081 NULL_TREE, ((c_language == clk_cplusplus)
10841285
MM
3082 ? builtin_types[BT_FN_VOID]
3083 : builtin_types[BT_FN_VOID_VAR]),
fc2aaf30
JM
3084 0, NOT_BUILT_IN, 0, 0, 1);
3085
6496a589 3086 builtin_function_2 (NULL, "exit",
fc2aaf30 3087 NULL_TREE, ((c_language == clk_cplusplus)
10841285
MM
3088 ? builtin_types[BT_FN_VOID_INT]
3089 : builtin_types[BT_FN_VOID_VAR]),
fc2aaf30 3090 0, NOT_BUILT_IN, 0, 0, 1);
7f4edbcb 3091
5b47282c 3092 main_identifier_node = get_identifier ("main");
7f4edbcb 3093}
d3707adb
RH
3094
3095tree
3096build_va_arg (expr, type)
3097 tree expr, type;
3098{
3099 return build1 (VA_ARG_EXPR, type, expr);
3100}
fc2aaf30
JM
3101
3102
7d14c755
JM
3103/* Linked list of disabled built-in functions. */
3104
3105typedef struct disabled_builtin
3106{
3107 const char *name;
3108 struct disabled_builtin *next;
3109} disabled_builtin;
3110static disabled_builtin *disabled_builtins = NULL;
3111
3112static bool builtin_function_disabled_p PARAMS ((const char *));
3113
3114/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
3115 begins with "__builtin_", give an error. */
3116
3117void
3118disable_builtin_function (name)
3119 const char *name;
3120{
3121 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
3122 error ("cannot disable built-in function `%s'", name);
3123 else
3124 {
3125 disabled_builtin *new = xmalloc (sizeof (disabled_builtin));
3126 new->name = name;
3127 new->next = disabled_builtins;
3128 disabled_builtins = new;
3129 }
3130}
3131
3132
3133/* Return true if the built-in function NAME has been disabled, false
3134 otherwise. */
3135
3136static bool
3137builtin_function_disabled_p (name)
3138 const char *name;
3139{
3140 disabled_builtin *p;
3141 for (p = disabled_builtins; p != NULL; p = p->next)
3142 {
3143 if (strcmp (name, p->name) == 0)
3144 return true;
3145 }
3146 return false;
3147}
3148
3149
fc2aaf30
JM
3150/* Possibly define a builtin function with one or two names. BUILTIN_NAME
3151 is an __builtin_-prefixed name; NAME is the ordinary name; one or both
3152 of these may be NULL (though both being NULL is useless).
3153 BUILTIN_TYPE is the type of the __builtin_-prefixed function;
3154 TYPE is the type of the function with the ordinary name. These
3155 may differ if the ordinary name is declared with a looser type to avoid
3156 conflicts with headers. FUNCTION_CODE and CLASS are as for
3157 builtin_function. If LIBRARY_NAME_P is nonzero, NAME is passed as
3158 the LIBRARY_NAME parameter to builtin_function when declaring BUILTIN_NAME.
3159 If NONANSI_P is nonzero, the name NAME is treated as a non-ANSI name; if
3160 NORETURN_P is nonzero, the function is marked as non-returning.
3161 Returns the declaration of BUILTIN_NAME, if any, otherwise
3162 the declaration of NAME. Does not declare NAME if flag_no_builtin,
3163 or if NONANSI_P and flag_no_nonansi_builtin. */
3164
3165static tree
3166builtin_function_2 (builtin_name, name, builtin_type, type, function_code,
3167 class, library_name_p, nonansi_p, noreturn_p)
3168 const char *builtin_name;
3169 const char *name;
3170 tree builtin_type;
3171 tree type;
3172 int function_code;
3173 enum built_in_class class;
3174 int library_name_p;
3175 int nonansi_p;
3176 int noreturn_p;
3177{
3178 tree bdecl = NULL_TREE;
3179 tree decl = NULL_TREE;
3180 if (builtin_name != 0)
3181 {
3182 bdecl = builtin_function (builtin_name, builtin_type, function_code,
6496a589 3183 class, library_name_p ? name : NULL);
fc2aaf30
JM
3184 if (noreturn_p)
3185 {
3186 TREE_THIS_VOLATILE (bdecl) = 1;
3187 TREE_SIDE_EFFECTS (bdecl) = 1;
3188 }
3189 }
7d14c755
JM
3190 if (name != 0 && !flag_no_builtin && !builtin_function_disabled_p (name)
3191 && !(nonansi_p && flag_no_nonansi_builtin))
fc2aaf30 3192 {
6496a589 3193 decl = builtin_function (name, type, function_code, class, NULL);
fc2aaf30
JM
3194 if (nonansi_p)
3195 DECL_BUILT_IN_NONANSI (decl) = 1;
3196 if (noreturn_p)
3197 {
3198 TREE_THIS_VOLATILE (decl) = 1;
3199 TREE_SIDE_EFFECTS (decl) = 1;
3200 }
3201 }
3202 return (bdecl != 0 ? bdecl : decl);
3203}
c530479e 3204\f
d72040f5
RH
3205/* Nonzero if the type T promotes to int. This is (nearly) the
3206 integral promotions defined in ISO C99 6.3.1.1/2. */
3207
3208bool
3209c_promoting_integer_type_p (t)
3210 tree t;
3211{
3212 switch (TREE_CODE (t))
3213 {
3214 case INTEGER_TYPE:
3215 return (TYPE_MAIN_VARIANT (t) == char_type_node
3216 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
3217 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
3218 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
c6c04fca
RL
3219 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
3220 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d72040f5
RH
3221
3222 case ENUMERAL_TYPE:
3223 /* ??? Technically all enumerations not larger than an int
3224 promote to an int. But this is used along code paths
3225 that only want to notice a size change. */
3226 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
3227
3228 case BOOLEAN_TYPE:
3229 return 1;
3230
3231 default:
3232 return 0;
3233 }
3234}
3235
c530479e
RH
3236/* Return 1 if PARMS specifies a fixed number of parameters
3237 and none of their types is affected by default promotions. */
3238
3239int
3240self_promoting_args_p (parms)
3241 tree parms;
3242{
b3694847 3243 tree t;
c530479e
RH
3244 for (t = parms; t; t = TREE_CHAIN (t))
3245 {
b3694847 3246 tree type = TREE_VALUE (t);
7e8176d7 3247
c530479e
RH
3248 if (TREE_CHAIN (t) == 0 && type != void_type_node)
3249 return 0;
3250
3251 if (type == 0)
3252 return 0;
3253
3254 if (TYPE_MAIN_VARIANT (type) == float_type_node)
3255 return 0;
3256
d72040f5 3257 if (c_promoting_integer_type_p (type))
c530479e
RH
3258 return 0;
3259 }
3260 return 1;
3261}
5eda3d66 3262
0a7394bc
MM
3263/* Recursively examines the array elements of TYPE, until a non-array
3264 element type is found. */
3265
3266tree
3267strip_array_types (type)
3268 tree type;
3269{
3270 while (TREE_CODE (type) == ARRAY_TYPE)
3271 type = TREE_TYPE (type);
3272
3273 return type;
3274}
3275
71925bc0
RS
3276static tree expand_unordered_cmp PARAMS ((tree, tree, enum tree_code,
3277 enum tree_code));
3278
3279/* Expand a call to an unordered comparison function such as
3280 __builtin_isgreater(). FUNCTION is the function's declaration and
3281 PARAMS a list of the values passed. For __builtin_isunordered(),
3282 UNORDERED_CODE is UNORDERED_EXPR and ORDERED_CODE is NOP_EXPR. In
3283 other cases, UNORDERED_CODE and ORDERED_CODE are comparison codes
3284 that give the opposite of the desired result. UNORDERED_CODE is
3285 used for modes that can hold NaNs and ORDERED_CODE is used for the
3286 rest. */
3287
3288static tree
3289expand_unordered_cmp (function, params, unordered_code, ordered_code)
3290 tree function, params;
3291 enum tree_code unordered_code, ordered_code;
3292{
3293 tree arg0, arg1, type;
3294 enum tree_code code0, code1;
3295
3296 /* Check that we have exactly two arguments. */
3297 if (params == 0 || TREE_CHAIN (params) == 0)
3298 {
3299 error ("too few arguments to function `%s'",
3300 IDENTIFIER_POINTER (DECL_NAME (function)));
3301 return error_mark_node;
3302 }
3303 else if (TREE_CHAIN (TREE_CHAIN (params)) != 0)
3304 {
3305 error ("too many arguments to function `%s'",
3306 IDENTIFIER_POINTER (DECL_NAME (function)));
3307 return error_mark_node;
3308 }
3309
3310 arg0 = TREE_VALUE (params);
3311 arg1 = TREE_VALUE (TREE_CHAIN (params));
3312
3313 code0 = TREE_CODE (TREE_TYPE (arg0));
3314 code1 = TREE_CODE (TREE_TYPE (arg1));
3315
3316 /* Make sure that the arguments have a common type of REAL. */
3317 type = 0;
3318 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3319 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3320 type = common_type (TREE_TYPE (arg0), TREE_TYPE (arg1));
3321
3322 if (type == 0 || TREE_CODE (type) != REAL_TYPE)
3323 {
3324 error ("non-floating-point argument to function `%s'",
3325 IDENTIFIER_POINTER (DECL_NAME (function)));
3326 return error_mark_node;
3327 }
3328
3329 if (unordered_code == UNORDERED_EXPR)
3330 {
3331 if (MODE_HAS_NANS (TYPE_MODE (type)))
3332 return build_binary_op (unordered_code,
3333 convert (type, arg0),
3334 convert (type, arg1),
3335 0);
3336 else
3337 return integer_zero_node;
3338 }
3339
3340 return build_unary_op (TRUTH_NOT_EXPR,
3341 build_binary_op (MODE_HAS_NANS (TYPE_MODE (type))
3342 ? unordered_code
3343 : ordered_code,
3344 convert (type, arg0),
3345 convert (type, arg1),
3346 0),
3347 0);
3348}
3349
3350
5eda3d66
RH
3351/* Recognize certain built-in functions so we can make tree-codes
3352 other than CALL_EXPR. We do this when it enables fold-const.c
3353 to do something useful. */
3354/* ??? By rights this should go in builtins.c, but only C and C++
3355 implement build_{binary,unary}_op. Not exactly sure what bits
3356 of functionality are actually needed from those functions, or
3357 where the similar functionality exists in the other front ends. */
3358
3359tree
3360expand_tree_builtin (function, params, coerced_params)
3361 tree function, params, coerced_params;
3362{
5eda3d66
RH
3363 if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL)
3364 return NULL_TREE;
3365
3366 switch (DECL_FUNCTION_CODE (function))
3367 {
3368 case BUILT_IN_ABS:
10841285
MM
3369 case BUILT_IN_LABS:
3370 case BUILT_IN_LLABS:
3371 case BUILT_IN_IMAXABS:
5eda3d66 3372 case BUILT_IN_FABS:
10841285
MM
3373 case BUILT_IN_FABSL:
3374 case BUILT_IN_FABSF:
5eda3d66
RH
3375 if (coerced_params == 0)
3376 return integer_zero_node;
3377 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
3378
341e3d11 3379 case BUILT_IN_CONJ:
10841285
MM
3380 case BUILT_IN_CONJF:
3381 case BUILT_IN_CONJL:
341e3d11
JM
3382 if (coerced_params == 0)
3383 return integer_zero_node;
3384 return build_unary_op (CONJ_EXPR, TREE_VALUE (coerced_params), 0);
3385
3386 case BUILT_IN_CREAL:
10841285
MM
3387 case BUILT_IN_CREALF:
3388 case BUILT_IN_CREALL:
341e3d11
JM
3389 if (coerced_params == 0)
3390 return integer_zero_node;
3391 return build_unary_op (REALPART_EXPR, TREE_VALUE (coerced_params), 0);
3392
3393 case BUILT_IN_CIMAG:
10841285
MM
3394 case BUILT_IN_CIMAGF:
3395 case BUILT_IN_CIMAGL:
341e3d11
JM
3396 if (coerced_params == 0)
3397 return integer_zero_node;
3398 return build_unary_op (IMAGPART_EXPR, TREE_VALUE (coerced_params), 0);
3399
5eda3d66 3400 case BUILT_IN_ISGREATER:
71925bc0 3401 return expand_unordered_cmp (function, params, UNLE_EXPR, LE_EXPR);
5eda3d66
RH
3402
3403 case BUILT_IN_ISGREATEREQUAL:
71925bc0 3404 return expand_unordered_cmp (function, params, UNLT_EXPR, LT_EXPR);
5eda3d66
RH
3405
3406 case BUILT_IN_ISLESS:
71925bc0 3407 return expand_unordered_cmp (function, params, UNGE_EXPR, GE_EXPR);
5eda3d66
RH
3408
3409 case BUILT_IN_ISLESSEQUAL:
71925bc0 3410 return expand_unordered_cmp (function, params, UNGT_EXPR, GT_EXPR);
5eda3d66
RH
3411
3412 case BUILT_IN_ISLESSGREATER:
71925bc0 3413 return expand_unordered_cmp (function, params, UNEQ_EXPR, EQ_EXPR);
5eda3d66
RH
3414
3415 case BUILT_IN_ISUNORDERED:
71925bc0 3416 return expand_unordered_cmp (function, params, UNORDERED_EXPR, NOP_EXPR);
5eda3d66
RH
3417
3418 default:
3419 break;
3420 }
3421
3422 return NULL_TREE;
3423}
c7d87c0a 3424
ae499cce
MM
3425/* Returns non-zero if CODE is the code for a statement. */
3426
3427int
3428statement_code_p (code)
3429 enum tree_code code;
3430{
3431 switch (code)
3432 {
6e4ae815 3433 case CLEANUP_STMT:
ae499cce
MM
3434 case EXPR_STMT:
3435 case COMPOUND_STMT:
3436 case DECL_STMT:
3437 case IF_STMT:
3438 case FOR_STMT:
3439 case WHILE_STMT:
3440 case DO_STMT:
3441 case RETURN_STMT:
3442 case BREAK_STMT:
3443 case CONTINUE_STMT:
8f17b5c5 3444 case SCOPE_STMT:
ae499cce
MM
3445 case SWITCH_STMT:
3446 case GOTO_STMT:
3447 case LABEL_STMT:
3448 case ASM_STMT:
de097a2d 3449 case FILE_STMT:
ae499cce
MM
3450 case CASE_LABEL:
3451 return 1;
3452
3453 default:
3454 if (lang_statement_code_p)
3455 return (*lang_statement_code_p) (code);
3456 return 0;
3457 }
3458}
3459
86306c8e 3460/* Walk the statement tree, rooted at *tp. Apply FUNC to all the
ae499cce
MM
3461 sub-trees of *TP in a pre-order traversal. FUNC is called with the
3462 DATA and the address of each sub-tree. If FUNC returns a non-NULL
3463 value, the traversal is aborted, and the value returned by FUNC is
3464 returned. If FUNC sets WALK_SUBTREES to zero, then the subtrees of
3465 the node being visited are not walked.
3466
3467 We don't need a without_duplicates variant of this one because the
3468 statement tree is a tree, not a graph. */
3469
3470tree
3471walk_stmt_tree (tp, func, data)
3472 tree *tp;
3473 walk_tree_fn func;
3474 void *data;
3475{
3476 enum tree_code code;
3477 int walk_subtrees;
3478 tree result;
3479 int i, len;
3480
3481#define WALK_SUBTREE(NODE) \
3482 do \
3483 { \
3484 result = walk_stmt_tree (&(NODE), func, data); \
3485 if (result) \
3486 return result; \
3487 } \
3488 while (0)
3489
3490 /* Skip empty subtrees. */
3491 if (!*tp)
3492 return NULL_TREE;
3493
3494 /* Skip subtrees below non-statement nodes. */
3495 if (!statement_code_p (TREE_CODE (*tp)))
3496 return NULL_TREE;
3497
3498 /* Call the function. */
3499 walk_subtrees = 1;
3500 result = (*func) (tp, &walk_subtrees, data);
3501
3502 /* If we found something, return it. */
3503 if (result)
3504 return result;
3505
ae499cce
MM
3506 /* FUNC may have modified the tree, recheck that we're looking at a
3507 statement node. */
3508 code = TREE_CODE (*tp);
3509 if (!statement_code_p (code))
3510 return NULL_TREE;
3511
87aee676
DN
3512 /* Visit the subtrees unless FUNC decided that there was nothing
3513 interesting below this point in the tree. */
3514 if (walk_subtrees)
3515 {
3516 /* Walk over all the sub-trees of this operand. Statement nodes
3517 never contain RTL, and we needn't worry about TARGET_EXPRs. */
3518 len = TREE_CODE_LENGTH (code);
3519
3520 /* Go through the subtrees. We need to do this in forward order so
3521 that the scope of a FOR_EXPR is handled properly. */
3522 for (i = 0; i < len; ++i)
3523 WALK_SUBTREE (TREE_OPERAND (*tp, i));
3524 }
ae499cce
MM
3525
3526 /* Finally visit the chain. This can be tail-recursion optimized if
3527 we write it this way. */
3528 return walk_stmt_tree (&TREE_CHAIN (*tp), func, data);
3529
3530#undef WALK_SUBTREE
3531}
3532
8f17b5c5
MM
3533/* Used to compare case labels. K1 and K2 are actually tree nodes
3534 representing case labels, or NULL_TREE for a `default' label.
3535 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
3536 K2, and 0 if K1 and K2 are equal. */
3537
3538int
3539case_compare (k1, k2)
3540 splay_tree_key k1;
3541 splay_tree_key k2;
3542{
3543 /* Consider a NULL key (such as arises with a `default' label) to be
3544 smaller than anything else. */
3545 if (!k1)
3546 return k2 ? -1 : 0;
3547 else if (!k2)
3548 return k1 ? 1 : 0;
3549
3550 return tree_int_cst_compare ((tree) k1, (tree) k2);
3551}
3552
3553/* Process a case label for the range LOW_VALUE ... HIGH_VALUE. If
3554 LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is
3555 actually a `default' label. If only HIGH_VALUE is NULL_TREE, then
3556 case label was declared using the usual C/C++ syntax, rather than
3557 the GNU case range extension. CASES is a tree containing all the
3558 case ranges processed so far; COND is the condition for the
3559 switch-statement itself. Returns the CASE_LABEL created, or
3560 ERROR_MARK_NODE if no CASE_LABEL is created. */
3561
3562tree
3563c_add_case_label (cases, cond, low_value, high_value)
3564 splay_tree cases;
3565 tree cond;
3566 tree low_value;
3567 tree high_value;
3568{
3569 tree type;
3570 tree label;
3571 tree case_label;
3572 splay_tree_node node;
3573
3574 /* Create the LABEL_DECL itself. */
3575 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
3576 DECL_CONTEXT (label) = current_function_decl;
3577
3578 /* If there was an error processing the switch condition, bail now
3579 before we get more confused. */
3580 if (!cond || cond == error_mark_node)
3581 {
3582 /* Add a label anyhow so that the back-end doesn't think that
3583 the beginning of the switch is unreachable. */
3584 if (!cases->root)
3585 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
3586 return error_mark_node;
3587 }
3588
3589 if ((low_value && TREE_TYPE (low_value)
3590 && POINTER_TYPE_P (TREE_TYPE (low_value)))
3591 || (high_value && TREE_TYPE (high_value)
3592 && POINTER_TYPE_P (TREE_TYPE (high_value))))
3593 error ("pointers are not permitted as case values");
3594
3595 /* Case ranges are a GNU extension. */
3596 if (high_value && pedantic)
3597 {
3598 if (c_language == clk_cplusplus)
3599 pedwarn ("ISO C++ forbids range expressions in switch statements");
3600 else
3601 pedwarn ("ISO C forbids range expressions in switch statements");
3602 }
3603
3604 type = TREE_TYPE (cond);
3605 if (low_value)
3606 {
3607 low_value = check_case_value (low_value);
3608 low_value = convert_and_check (type, low_value);
3609 }
3610 if (high_value)
3611 {
3612 high_value = check_case_value (high_value);
3613 high_value = convert_and_check (type, high_value);
3614 }
3615
3616 /* If an error has occurred, bail out now. */
3617 if (low_value == error_mark_node || high_value == error_mark_node)
3618 {
3619 if (!cases->root)
3620 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
3621 return error_mark_node;
3622 }
3623
3624 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
3625 really a case range, even though it was written that way. Remove
3626 the HIGH_VALUE to simplify later processing. */
3627 if (tree_int_cst_equal (low_value, high_value))
3628 high_value = NULL_TREE;
3629 if (low_value && high_value
3630 && !tree_int_cst_lt (low_value, high_value))
3631 warning ("empty range specified");
3632
3633 /* Look up the LOW_VALUE in the table of case labels we already
3634 have. */
3635 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
3636 /* If there was not an exact match, check for overlapping ranges.
3637 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
3638 that's a `default' label and the only overlap is an exact match. */
3639 if (!node && (low_value || high_value))
3640 {
3641 splay_tree_node low_bound;
3642 splay_tree_node high_bound;
3643
3644 /* Even though there wasn't an exact match, there might be an
3645 overlap between this case range and another case range.
3646 Since we've (inductively) not allowed any overlapping case
3647 ranges, we simply need to find the greatest low case label
3648 that is smaller that LOW_VALUE, and the smallest low case
3649 label that is greater than LOW_VALUE. If there is an overlap
3650 it will occur in one of these two ranges. */
3651 low_bound = splay_tree_predecessor (cases,
3652 (splay_tree_key) low_value);
3653 high_bound = splay_tree_successor (cases,
3654 (splay_tree_key) low_value);
3655
3656 /* Check to see if the LOW_BOUND overlaps. It is smaller than
3657 the LOW_VALUE, so there is no need to check unless the
3658 LOW_BOUND is in fact itself a case range. */
3659 if (low_bound
3660 && CASE_HIGH ((tree) low_bound->value)
3661 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
3662 low_value) >= 0)
3663 node = low_bound;
3664 /* Check to see if the HIGH_BOUND overlaps. The low end of that
3665 range is bigger than the low end of the current range, so we
3666 are only interested if the current range is a real range, and
3667 not an ordinary case label. */
3668 else if (high_bound
3669 && high_value
3670 && (tree_int_cst_compare ((tree) high_bound->key,
3671 high_value)
3672 <= 0))
3673 node = high_bound;
3674 }
3675 /* If there was an overlap, issue an error. */
3676 if (node)
3677 {
3678 tree duplicate = CASE_LABEL_DECL ((tree) node->value);
3679
3680 if (high_value)
3681 {
3682 error ("duplicate (or overlapping) case value");
3683 error_with_decl (duplicate,
3684 "this is the first entry overlapping that value");
3685 }
3686 else if (low_value)
3687 {
3688 error ("duplicate case value") ;
3689 error_with_decl (duplicate, "previously used here");
3690 }
3691 else
3692 {
3693 error ("multiple default labels in one switch");
3694 error_with_decl (duplicate, "this is the first default label");
3695 }
3696 if (!cases->root)
3697 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
3698 }
3699
3700 /* Add a CASE_LABEL to the statement-tree. */
3701 case_label = add_stmt (build_case_label (low_value, high_value, label));
3702 /* Register this case label in the splay tree. */
3703 splay_tree_insert (cases,
3704 (splay_tree_key) low_value,
3705 (splay_tree_value) case_label);
3706
3707 return case_label;
3708}
3709
15b732b2
NB
3710/* Finish an expression taking the address of LABEL. Returns an
3711 expression for the address. */
3712
3713tree
3714finish_label_address_expr (label)
3715 tree label;
3716{
3717 tree result;
3718
3719 if (pedantic)
3720 {
3721 if (c_language == clk_cplusplus)
3722 pedwarn ("ISO C++ forbids taking the address of a label");
3723 else
3724 pedwarn ("ISO C forbids taking the address of a label");
3725 }
3726
3727 label = lookup_label (label);
3728 if (label == NULL_TREE)
3729 result = null_pointer_node;
3730 else
3731 {
3732 TREE_USED (label) = 1;
3733 result = build1 (ADDR_EXPR, ptr_type_node, label);
3734 TREE_CONSTANT (result) = 1;
3735 /* The current function in not necessarily uninlinable.
3736 Computed gotos are incompatible with inlining, but the value
3737 here could be used only in a diagnostic, for example. */
3738 }
3739
3740 return result;
3741}
3742
8f17b5c5
MM
3743/* Mark P (a stmt_tree) for GC. The use of a `void *' for the
3744 parameter allows this function to be used as a GC-marking
3745 function. */
3746
3747void
3748mark_stmt_tree (p)
3749 void *p;
3750{
3751 stmt_tree st = (stmt_tree) p;
3752
3753 ggc_mark_tree (st->x_last_stmt);
3754 ggc_mark_tree (st->x_last_expr_type);
3755}
3756
3757/* Mark LD for GC. */
3758
3759void
3760c_mark_lang_decl (c)
69dcadff 3761 struct c_lang_decl *c ATTRIBUTE_UNUSED;
8f17b5c5 3762{
8f17b5c5
MM
3763}
3764
3765/* Mark F for GC. */
3766
3767void
3768mark_c_language_function (f)
3769 struct language_function *f;
3770{
3771 if (!f)
3772 return;
3773
3774 mark_stmt_tree (&f->x_stmt_tree);
3775 ggc_mark_tree (f->x_scope_stmt_stack);
3776}
3777
3778/* Hook used by expand_expr to expand language-specific tree codes. */
3779
3780rtx
3781c_expand_expr (exp, target, tmode, modifier)
3782 tree exp;
3783 rtx target;
3784 enum machine_mode tmode;
c9d892a8 3785 int modifier; /* Actually enum_modifier. */
8f17b5c5
MM
3786{
3787 switch (TREE_CODE (exp))
3788 {
3789 case STMT_EXPR:
3790 {
3791 tree rtl_expr;
3792 rtx result;
312687cf 3793 bool preserve_result = false;
8f17b5c5
MM
3794
3795 /* Since expand_expr_stmt calls free_temp_slots after every
3796 expression statement, we must call push_temp_slots here.
3797 Otherwise, any temporaries in use now would be considered
3798 out-of-scope after the first EXPR_STMT from within the
3799 STMT_EXPR. */
3800 push_temp_slots ();
b2123dc0 3801 rtl_expr = expand_start_stmt_expr (!STMT_EXPR_NO_SCOPE (exp));
1574ef13
AO
3802
3803 /* If we want the result of this expression, find the last
3804 EXPR_STMT in the COMPOUND_STMT and mark it as addressable. */
1cf537c5
JJ
3805 if (target != const0_rtx
3806 && TREE_CODE (STMT_EXPR_STMT (exp)) == COMPOUND_STMT
3807 && TREE_CODE (COMPOUND_BODY (STMT_EXPR_STMT (exp))) == SCOPE_STMT)
1574ef13 3808 {
1cf537c5
JJ
3809 tree expr = COMPOUND_BODY (STMT_EXPR_STMT (exp));
3810 tree last = TREE_CHAIN (expr);
1574ef13 3811
1cf537c5 3812 while (TREE_CHAIN (last))
1574ef13 3813 {
1cf537c5
JJ
3814 expr = last;
3815 last = TREE_CHAIN (last);
1574ef13 3816 }
1cf537c5
JJ
3817
3818 if (TREE_CODE (last) == SCOPE_STMT
3819 && TREE_CODE (expr) == EXPR_STMT)
312687cf
EB
3820 {
3821 TREE_ADDRESSABLE (expr) = 1;
3822 preserve_result = true;
3823 }
1574ef13
AO
3824 }
3825
8f17b5c5
MM
3826 expand_stmt (STMT_EXPR_STMT (exp));
3827 expand_end_stmt_expr (rtl_expr);
312687cf 3828
8f17b5c5 3829 result = expand_expr (rtl_expr, target, tmode, modifier);
312687cf
EB
3830 if (preserve_result && GET_CODE (result) == MEM)
3831 {
3832 if (GET_MODE (result) != BLKmode)
3833 result = copy_to_reg (result);
3834 else
3835 preserve_temp_slots (result);
3836 }
3837
b2123dc0
MM
3838 /* If the statment-expression does not have a scope, then the
3839 new temporaries we created within it must live beyond the
3840 statement-expression. */
3841 if (STMT_EXPR_NO_SCOPE (exp))
3842 preserve_temp_slots (NULL_RTX);
3843
8f17b5c5
MM
3844 pop_temp_slots ();
3845 return result;
3846 }
3847 break;
3848
c70eaeaf
KG
3849 case CALL_EXPR:
3850 {
3851 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
3852 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
3853 == FUNCTION_DECL)
3854 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
3855 && (DECL_BUILT_IN_CLASS (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
3856 == BUILT_IN_FRONTEND))
3857 return c_expand_builtin (exp, target, tmode, modifier);
3858 else
173bf5be 3859 abort ();
c70eaeaf
KG
3860 }
3861 break;
3862
db3acfa5
JM
3863 case COMPOUND_LITERAL_EXPR:
3864 {
3865 /* Initialize the anonymous variable declared in the compound
3866 literal, then return the variable. */
3867 tree decl = COMPOUND_LITERAL_EXPR_DECL (exp);
3868 emit_local_var (decl);
3869 return expand_expr (decl, target, tmode, modifier);
3870 }
3871
8f17b5c5
MM
3872 default:
3873 abort ();
3874 }
3875
3876 abort ();
3877 return NULL;
3878}
3879
3880/* Hook used by safe_from_p to handle language-specific tree codes. */
3881
3882int
3883c_safe_from_p (target, exp)
3884 rtx target;
3885 tree exp;
3886{
3887 /* We can see statements here when processing the body of a
3888 statement-expression. For a declaration statement declaring a
3889 variable, look at the variable's initializer. */
3890 if (TREE_CODE (exp) == DECL_STMT)
3891 {
3892 tree decl = DECL_STMT_DECL (exp);
3893
3894 if (TREE_CODE (decl) == VAR_DECL
3895 && DECL_INITIAL (decl)
3896 && !safe_from_p (target, DECL_INITIAL (decl), /*top_p=*/0))
3897 return 0;
3898 }
3899
3900 /* For any statement, we must follow the statement-chain. */
3901 if (statement_code_p (TREE_CODE (exp)) && TREE_CHAIN (exp))
3902 return safe_from_p (target, TREE_CHAIN (exp), /*top_p=*/0);
3903
3904 /* Assume everything else is safe. */
3905 return 1;
3906}
3907
3fe30ff6
RH
3908/* Hook used by unsafe_for_reeval to handle language-specific tree codes. */
3909
3910int
48a7a235 3911c_common_unsafe_for_reeval (exp)
3fe30ff6
RH
3912 tree exp;
3913{
caaf2272
JJ
3914 /* Statement expressions may not be reevaluated, likewise compound
3915 literals. */
3916 if (TREE_CODE (exp) == STMT_EXPR
3917 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
3fe30ff6
RH
3918 return 2;
3919
3920 /* Walk all other expressions. */
3921 return -1;
3922}
3923
db3acfa5
JM
3924/* Hook used by staticp to handle language-specific tree codes. */
3925
3926int
3927c_staticp (exp)
3928 tree exp;
3929{
3930 if (TREE_CODE (exp) == COMPOUND_LITERAL_EXPR
3931 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
3932 return 1;
3933 return 0;
3934}
3935
c70eaeaf
KG
3936#define CALLED_AS_BUILT_IN(NODE) \
3937 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
3938
3939static rtx
3940c_expand_builtin (exp, target, tmode, modifier)
3941 tree exp;
3942 rtx target;
3943 enum machine_mode tmode;
3944 enum expand_modifier modifier;
3945{
3946 tree type = TREE_TYPE (exp);
3947 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
3948 tree arglist = TREE_OPERAND (exp, 1);
3949 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
3950 enum tree_code code = TREE_CODE (exp);
3951 const int ignore = (target == const0_rtx
3952 || ((code == NON_LVALUE_EXPR || code == NOP_EXPR
3953 || code == CONVERT_EXPR || code == REFERENCE_EXPR
3954 || code == COND_EXPR)
3955 && TREE_CODE (type) == VOID_TYPE));
3956
3957 if (! optimize && ! CALLED_AS_BUILT_IN (fndecl))
3958 return expand_call (exp, target, ignore);
3959
3960 switch (fcode)
3961 {
3962 case BUILT_IN_PRINTF:
3963 target = c_expand_builtin_printf (arglist, target, tmode,
173bf5be 3964 modifier, ignore, /*unlocked=*/ 0);
b4c984fb
KG
3965 if (target)
3966 return target;
3967 break;
3968
3969 case BUILT_IN_PRINTF_UNLOCKED:
3970 target = c_expand_builtin_printf (arglist, target, tmode,
173bf5be 3971 modifier, ignore, /*unlocked=*/ 1);
c70eaeaf
KG
3972 if (target)
3973 return target;
3974 break;
3975
18f988a0
KG
3976 case BUILT_IN_FPRINTF:
3977 target = c_expand_builtin_fprintf (arglist, target, tmode,
173bf5be 3978 modifier, ignore, /*unlocked=*/ 0);
b4c984fb
KG
3979 if (target)
3980 return target;
3981 break;
3982
3983 case BUILT_IN_FPRINTF_UNLOCKED:
3984 target = c_expand_builtin_fprintf (arglist, target, tmode,
173bf5be 3985 modifier, ignore, /*unlocked=*/ 1);
18f988a0
KG
3986 if (target)
3987 return target;
3988 break;
3989
c70eaeaf
KG
3990 default: /* just do library call, if unknown builtin */
3991 error ("built-in function `%s' not currently supported",
3992 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
3993 }
3994
3995 /* The switch statement above can drop through to cause the function
3996 to be called normally. */
3997 return expand_call (exp, target, ignore);
3998}
3999
4000/* Check an arglist to *printf for problems. The arglist should start
4001 at the format specifier, with the remaining arguments immediately
ec5c56db 4002 following it. */
c70eaeaf
KG
4003static int
4004is_valid_printf_arglist (arglist)
173bf5be 4005 tree arglist;
c70eaeaf 4006{
ec5c56db 4007 /* Save this value so we can restore it later. */
c70eaeaf
KG
4008 const int SAVE_pedantic = pedantic;
4009 int diagnostic_occurred = 0;
80a497e4 4010 tree attrs;
c70eaeaf
KG
4011
4012 /* Set this to a known value so the user setting won't affect code
4013 generation. */
4014 pedantic = 1;
ec5c56db 4015 /* Check to make sure there are no format specifier errors. */
80a497e4
JM
4016 attrs = tree_cons (get_identifier ("format"),
4017 tree_cons (NULL_TREE,
4018 get_identifier ("printf"),
4019 tree_cons (NULL_TREE,
4020 integer_one_node,
4021 tree_cons (NULL_TREE,
4022 build_int_2 (2, 0),
4023 NULL_TREE))),
4024 NULL_TREE);
4025 check_function_format (&diagnostic_occurred, attrs, arglist);
c70eaeaf 4026
ec5c56db 4027 /* Restore the value of `pedantic'. */
c70eaeaf
KG
4028 pedantic = SAVE_pedantic;
4029
4030 /* If calling `check_function_format_ptr' produces a warning, we
ec5c56db 4031 return false, otherwise we return true. */
c70eaeaf
KG
4032 return ! diagnostic_occurred;
4033}
4034
4035/* If the arguments passed to printf are suitable for optimizations,
ec5c56db 4036 we attempt to transform the call. */
c70eaeaf 4037static rtx
b4c984fb 4038c_expand_builtin_printf (arglist, target, tmode, modifier, ignore, unlocked)
c70eaeaf
KG
4039 tree arglist;
4040 rtx target;
4041 enum machine_mode tmode;
4042 enum expand_modifier modifier;
4043 int ignore;
b4c984fb 4044 int unlocked;
c70eaeaf 4045{
b4c984fb
KG
4046 tree fn_putchar = unlocked ?
4047 built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED] : built_in_decls[BUILT_IN_PUTCHAR];
4048 tree fn_puts = unlocked ?
4049 built_in_decls[BUILT_IN_PUTS_UNLOCKED] : built_in_decls[BUILT_IN_PUTS];
c70eaeaf
KG
4050 tree fn, format_arg, stripped_string;
4051
4052 /* If the return value is used, or the replacement _DECL isn't
ec5c56db 4053 initialized, don't do the transformation. */
c70eaeaf
KG
4054 if (!ignore || !fn_putchar || !fn_puts)
4055 return 0;
4056
ec5c56db 4057 /* Verify the required arguments in the original call. */
c70eaeaf
KG
4058 if (arglist == 0
4059 || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE))
4060 return 0;
4061
ec5c56db 4062 /* Check the specifier vs. the parameters. */
c70eaeaf
KG
4063 if (!is_valid_printf_arglist (arglist))
4064 return 0;
4065
4066 format_arg = TREE_VALUE (arglist);
4067 stripped_string = format_arg;
4068 STRIP_NOPS (stripped_string);
4069 if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
4070 stripped_string = TREE_OPERAND (stripped_string, 0);
4071
4072 /* If the format specifier isn't a STRING_CST, punt. */
4073 if (TREE_CODE (stripped_string) != STRING_CST)
4074 return 0;
4075
4076 /* OK! We can attempt optimization. */
4077
ec5c56db 4078 /* If the format specifier was "%s\n", call __builtin_puts(arg2). */
c70eaeaf
KG
4079 if (strcmp (TREE_STRING_POINTER (stripped_string), "%s\n") == 0)
4080 {
4081 arglist = TREE_CHAIN (arglist);
4082 fn = fn_puts;
4083 }
ec5c56db 4084 /* If the format specifier was "%c", call __builtin_putchar (arg2). */
c70eaeaf
KG
4085 else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0)
4086 {
4087 arglist = TREE_CHAIN (arglist);
4088 fn = fn_putchar;
4089 }
4090 else
4091 {
173bf5be 4092 /* We can't handle anything else with % args or %% ... yet. */
c70eaeaf
KG
4093 if (strchr (TREE_STRING_POINTER (stripped_string), '%'))
4094 return 0;
4095
4096 /* If the resulting constant string has a length of 1, call
4097 putchar. Note, TREE_STRING_LENGTH includes the terminating
4098 NULL in its count. */
4099 if (TREE_STRING_LENGTH (stripped_string) == 2)
4100 {
4101 /* Given printf("c"), (where c is any one character,)
4102 convert "c"[0] to an int and pass that to the replacement
ec5c56db 4103 function. */
c70eaeaf
KG
4104 arglist = build_int_2 (TREE_STRING_POINTER (stripped_string)[0], 0);
4105 arglist = build_tree_list (NULL_TREE, arglist);
4106
4107 fn = fn_putchar;
4108 }
4109 /* If the resulting constant was "string\n", call
4110 __builtin_puts("string"). Ensure "string" has at least one
4111 character besides the trailing \n. Note, TREE_STRING_LENGTH
4112 includes the terminating NULL in its count. */
4113 else if (TREE_STRING_LENGTH (stripped_string) > 2
4114 && TREE_STRING_POINTER (stripped_string)
4115 [TREE_STRING_LENGTH (stripped_string) - 2] == '\n')
4116 {
4117 /* Create a NULL-terminated string that's one char shorter
4118 than the original, stripping off the trailing '\n'. */
4119 const int newlen = TREE_STRING_LENGTH (stripped_string) - 1;
4120 char *newstr = (char *) alloca (newlen);
4121 memcpy (newstr, TREE_STRING_POINTER (stripped_string), newlen - 1);
4122 newstr[newlen - 1] = 0;
4123
b84a3874 4124 arglist = fix_string_type (build_string (newlen, newstr));
c70eaeaf
KG
4125 arglist = build_tree_list (NULL_TREE, arglist);
4126 fn = fn_puts;
4127 }
4128 else
4129 /* We'd like to arrange to call fputs(string) here, but we
4130 need stdout and don't have a way to get it ... yet. */
4131 return 0;
4132 }
4133
4134 return expand_expr (build_function_call (fn, arglist),
4135 (ignore ? const0_rtx : target),
4136 tmode, modifier);
4137}
18f988a0
KG
4138
4139/* If the arguments passed to fprintf are suitable for optimizations,
ec5c56db 4140 we attempt to transform the call. */
18f988a0 4141static rtx
b4c984fb 4142c_expand_builtin_fprintf (arglist, target, tmode, modifier, ignore, unlocked)
18f988a0
KG
4143 tree arglist;
4144 rtx target;
4145 enum machine_mode tmode;
4146 enum expand_modifier modifier;
4147 int ignore;
b4c984fb 4148 int unlocked;
18f988a0 4149{
b4c984fb
KG
4150 tree fn_fputc = unlocked ?
4151 built_in_decls[BUILT_IN_FPUTC_UNLOCKED] : built_in_decls[BUILT_IN_FPUTC];
4152 tree fn_fputs = unlocked ?
4153 built_in_decls[BUILT_IN_FPUTS_UNLOCKED] : built_in_decls[BUILT_IN_FPUTS];
18f988a0
KG
4154 tree fn, format_arg, stripped_string;
4155
4156 /* If the return value is used, or the replacement _DECL isn't
ec5c56db 4157 initialized, don't do the transformation. */
18f988a0
KG
4158 if (!ignore || !fn_fputc || !fn_fputs)
4159 return 0;
4160
ec5c56db 4161 /* Verify the required arguments in the original call. */
18f988a0
KG
4162 if (arglist == 0
4163 || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE)
4164 || (TREE_CHAIN (arglist) == 0)
4165 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist)))) !=
4166 POINTER_TYPE))
4167 return 0;
4168
ec5c56db 4169 /* Check the specifier vs. the parameters. */
18f988a0
KG
4170 if (!is_valid_printf_arglist (TREE_CHAIN (arglist)))
4171 return 0;
4172
4173 format_arg = TREE_VALUE (TREE_CHAIN (arglist));
4174 stripped_string = format_arg;
4175 STRIP_NOPS (stripped_string);
4176 if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
4177 stripped_string = TREE_OPERAND (stripped_string, 0);
4178
4179 /* If the format specifier isn't a STRING_CST, punt. */
4180 if (TREE_CODE (stripped_string) != STRING_CST)
4181 return 0;
4182
4183 /* OK! We can attempt optimization. */
4184
ec5c56db 4185 /* If the format specifier was "%s", call __builtin_fputs(arg3, arg1). */
18f988a0
KG
4186 if (strcmp (TREE_STRING_POINTER (stripped_string), "%s") == 0)
4187 {
4188 tree newarglist = build_tree_list (NULL_TREE, TREE_VALUE (arglist));
4189 arglist = tree_cons (NULL_TREE,
4190 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))),
4191 newarglist);
4192 fn = fn_fputs;
4193 }
ec5c56db 4194 /* If the format specifier was "%c", call __builtin_fputc (arg3, arg1). */
18f988a0
KG
4195 else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0)
4196 {
4197 tree newarglist = build_tree_list (NULL_TREE, TREE_VALUE (arglist));
4198 arglist = tree_cons (NULL_TREE,
4199 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))),
4200 newarglist);
4201 fn = fn_fputc;
4202 }
4203 else
4204 {
173bf5be 4205 /* We can't handle anything else with % args or %% ... yet. */
18f988a0
KG
4206 if (strchr (TREE_STRING_POINTER (stripped_string), '%'))
4207 return 0;
4208
4209 /* When "string" doesn't contain %, replace all cases of
4210 fprintf(stream,string) with fputs(string,stream). The fputs
4211 builtin will take take of special cases like length==1. */
4212 arglist = tree_cons (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)),
4213 build_tree_list (NULL_TREE, TREE_VALUE (arglist)));
4214 fn = fn_fputs;
4215 }
4216
4217 return expand_expr (build_function_call (fn, arglist),
4218 (ignore ? const0_rtx : target),
4219 tmode, modifier);
4220}
19552aa5
JM
4221\f
4222
4223/* Given a boolean expression ARG, return a tree representing an increment
4224 or decrement (as indicated by CODE) of ARG. The front end must check for
4225 invalid cases (e.g., decrement in C++). */
4226tree
4227boolean_increment (code, arg)
4228 enum tree_code code;
4229 tree arg;
4230{
4231 tree val;
4232 tree true_res = (c_language == clk_cplusplus
4233 ? boolean_true_node
4234 : c_bool_true_node);
4235 arg = stabilize_reference (arg);
4236 switch (code)
4237 {
4238 case PREINCREMENT_EXPR:
4239 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4240 break;
4241 case POSTINCREMENT_EXPR:
4242 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4243 arg = save_expr (arg);
4244 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4245 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4246 break;
4247 case PREDECREMENT_EXPR:
4248 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
4249 break;
4250 case POSTDECREMENT_EXPR:
4251 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
4252 arg = save_expr (arg);
4253 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4254 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4255 break;
4256 default:
4257 abort ();
4258 }
4259 TREE_SIDE_EFFECTS (val) = 1;
4260 return val;
4261}
03dc0325 4262\f
6431177a
JM
4263/* Handle C and C++ default attributes. */
4264
4265enum built_in_attribute
4266{
4267#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4268#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
4269#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4270#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
4271#define DEF_FN_ATTR(NAME, ATTRS, PREDICATE) /* No entry needed in enum. */
4272#include "builtin-attrs.def"
4273#undef DEF_ATTR_NULL_TREE
4274#undef DEF_ATTR_INT
4275#undef DEF_ATTR_IDENT
4276#undef DEF_ATTR_TREE_LIST
4277#undef DEF_FN_ATTR
4278 ATTR_LAST
bb9f8221
RK
4279};
4280
6431177a
JM
4281static tree built_in_attributes[(int) ATTR_LAST];
4282
4283static bool c_attrs_initialized = false;
4284
4285static void c_init_attributes PARAMS ((void));
4286
4d6baafa
NB
4287/* Common initialization before parsing options. */
4288void
4289c_common_init_options (lang)
4290 enum c_language_kind lang;
03dc0325 4291{
4d6baafa 4292 c_language = lang;
bdcae02b
NB
4293 parse_in = cpp_create_reader (lang == clk_c || lang == clk_objective_c
4294 ? CLK_GNUC89 : CLK_GNUCXX);
4295 if (lang == clk_objective_c)
4296 cpp_get_options (parse_in)->objc = 1;
f5e99456 4297
4d6baafa
NB
4298 /* Mark as "unspecified" (see c_common_post_options). */
4299 flag_bounds_check = -1;
4300}
4301
4302/* Post-switch processing. */
4303void
4304c_common_post_options ()
4305{
4306 cpp_post_options (parse_in);
4307
6aa77e6c
AH
4308 flag_inline_trees = 1;
4309
4d6baafa
NB
4310 /* Use tree inlining if possible. Function instrumentation is only
4311 done in the RTL level, so we disable tree inlining. */
4312 if (! flag_instrument_function_entry_exit)
4313 {
4314 if (!flag_no_inline)
6aa77e6c 4315 flag_no_inline = 1;
4d6baafa
NB
4316 if (flag_inline_functions)
4317 {
4318 flag_inline_trees = 2;
4319 flag_inline_functions = 0;
4320 }
4321 }
f5e99456 4322
03dc0325 4323 /* If still "unspecified", make it match -fbounded-pointers. */
4d6baafa 4324 if (flag_bounds_check == -1)
03dc0325
JM
4325 flag_bounds_check = flag_bounded_pointers;
4326
4327 /* Special format checking options don't work without -Wformat; warn if
4328 they are used. */
4329 if (warn_format_y2k && !warn_format)
4330 warning ("-Wformat-y2k ignored without -Wformat");
4331 if (warn_format_extra_args && !warn_format)
4332 warning ("-Wformat-extra-args ignored without -Wformat");
e964a556
JT
4333 if (warn_format_zero_length && !warn_format)
4334 warning ("-Wformat-zero-length ignored without -Wformat");
03dc0325
JM
4335 if (warn_format_nonliteral && !warn_format)
4336 warning ("-Wformat-nonliteral ignored without -Wformat");
4337 if (warn_format_security && !warn_format)
4338 warning ("-Wformat-security ignored without -Wformat");
4339 if (warn_missing_format_attribute && !warn_format)
4340 warning ("-Wmissing-format-attribute ignored without -Wformat");
e5f3b786
NB
4341
4342 /* If an error has occurred in cpplib, note it so we fail
4343 immediately. */
4344 errorcount += cpp_errors (parse_in);
4d6baafa
NB
4345}
4346
3df89291
NB
4347/* Hook that registers front end and target-specific built-ins. */
4348static void
4349cb_register_builtins (pfile)
4350 cpp_reader *pfile;
4351{
4352 /* -undef turns off target-specific built-ins. */
4353 if (flag_undef)
4354 return;
4355
4356 if (c_language == clk_cplusplus)
4357 {
4358 if (SUPPORTS_ONE_ONLY)
a11eba95 4359 cpp_define (pfile, "__GXX_WEAK__=1");
3df89291
NB
4360 else
4361 cpp_define (pfile, "__GXX_WEAK__=0");
4362 }
4363
12a41c22
NB
4364 /* libgcc needs to know this. */
4365 if (USING_SJLJ_EXCEPTIONS)
4366 cpp_define (pfile, "__USING_SJLJ_EXCEPTIONS__");
4367
a11eba95 4368 /* stddef.h needs to know these. */
5279d739
ZW
4369 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
4370 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
4371 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
4372 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
4373
4374 /* For use in assembly language. */
4375 builtin_define_with_value ("__REGISTER_PREFIX__", REGISTER_PREFIX, 0);
4376 builtin_define_with_value ("__USER_LABEL_PREFIX__", user_label_prefix, 0);
4377
4378 /* Misc. */
4379 builtin_define_with_value ("__VERSION__", version_string, 1);
a11eba95 4380
748d29c1
NB
4381 /* Other target-independent built-ins determined by command-line
4382 options. */
4383 if (optimize_size)
4384 cpp_define (pfile, "__OPTIMIZE_SIZE__");
4385 if (optimize)
4386 cpp_define (pfile, "__OPTIMIZE__");
4387
4388 if (flag_hosted)
4389 cpp_define (pfile, "__STDC_HOSTED__=1");
4390 else
4391 cpp_define (pfile, "__STDC_HOSTED__=0");
4392
4393 if (fast_math_flags_set_p ())
4394 cpp_define (pfile, "__FAST_MATH__");
4395 if (flag_no_inline)
4396 cpp_define (pfile, "__NO_INLINE__");
4397
2a1dc0d8
ZW
4398 if (flag_iso)
4399 cpp_define (pfile, "__STRICT_ANSI__");
4400
4401 if (!flag_signed_char)
4402 cpp_define (pfile, "__CHAR_UNSIGNED__");
4403
3df89291
NB
4404 /* A straightforward target hook doesn't work, because of problems
4405 linking that hook's body when part of non-C front ends. */
2727afb5 4406# define preprocessing_asm_p() (cpp_get_options (pfile)->lang == CLK_ASM)
81a75f0f
NB
4407# define builtin_define(TXT) cpp_define (pfile, TXT)
4408# define builtin_assert(TXT) cpp_assert (pfile, TXT)
12a41c22
NB
4409 TARGET_CPU_CPP_BUILTINS ();
4410 TARGET_OS_CPP_BUILTINS ();
3df89291
NB
4411}
4412
4413/* Pass an object-like macro. If it doesn't lie in the user's
4414 namespace, defines it unconditionally. Otherwise define a version
4415 with two leading underscores, and another version with two leading
4416 and trailing underscores, and define the original only if an ISO
4417 standard was not nominated.
4418
4419 e.g. passing "unix" defines "__unix", "__unix__" and possibly
4420 "unix". Passing "_mips" defines "__mips", "__mips__" and possibly
4421 "_mips". */
4422void
4423builtin_define_std (macro)
4424 const char *macro;
4425{
4426 size_t len = strlen (macro);
4427 char *buff = alloca (len + 5);
4428 char *p = buff + 2;
4429 char *q = p + len;
4430
4431 /* prepend __ (or maybe just _) if in user's namespace. */
4432 memcpy (p, macro, len + 1);
4433 if (*p != '_')
4434 *--p = '_';
4435 if (p[1] != '_' && !ISUPPER (p[1]))
4436 *--p = '_';
4437 cpp_define (parse_in, p);
4438
4439 /* If it was in user's namespace... */
4440 if (p != buff + 2)
4441 {
3df89291
NB
4442 /* Define the macro with leading and following __. */
4443 if (q[-1] != '_')
4444 *q++ = '_';
4445 if (q[-2] != '_')
4446 *q++ = '_';
4447 *q = '\0';
4448 cpp_define (parse_in, p);
e0322d5c
NB
4449
4450 /* Finally, define the original macro if permitted. */
4451 if (!flag_iso)
4452 cpp_define (parse_in, macro);
3df89291
NB
4453 }
4454}
4455
5279d739
ZW
4456/* Pass an object-like macro and a value to define it to. The third
4457 parameter says whether or not to turn the value into a string
4458 constant. */
81a75f0f 4459static void
5279d739 4460builtin_define_with_value (macro, expansion, is_str)
a11eba95
ZW
4461 const char *macro;
4462 const char *expansion;
5279d739 4463 int is_str;
a11eba95 4464{
5279d739 4465 char *buf;
a11eba95
ZW
4466 size_t mlen = strlen (macro);
4467 size_t elen = strlen (expansion);
5279d739 4468 size_t extra = 2; /* space for an = and a NUL */
a11eba95 4469
5279d739
ZW
4470 if (is_str)
4471 extra += 2; /* space for two quote marks */
4472
4473 buf = alloca (mlen + elen + extra);
4474 if (is_str)
4475 sprintf (buf, "%s=\"%s\"", macro, expansion);
4476 else
4477 sprintf (buf, "%s=%s", macro, expansion);
a11eba95
ZW
4478
4479 cpp_define (parse_in, buf);
4480}
4481
4d6baafa
NB
4482/* Front end initialization common to C, ObjC and C++. */
4483const char *
4484c_common_init (filename)
4485 const char *filename;
4486{
2443d4e1
NB
4487 cpp_options *options = cpp_get_options (parse_in);
4488
4489 /* Set up preprocessor arithmetic. Must be done after call to
4490 c_common_nodes_and_builtins for wchar_type_node to be good. */
c9220e3a
NB
4491 if (flag_isoc99)
4492 options->precision = TYPE_PRECISION (intmax_type_node);
4493 else
4494 options->precision = TYPE_PRECISION (long_integer_type_node);
2443d4e1
NB
4495 options->char_precision = TYPE_PRECISION (char_type_node);
4496 options->int_precision = TYPE_PRECISION (integer_type_node);
4497 options->wchar_precision = TYPE_PRECISION (wchar_type_node);
44a147ad 4498 options->unsigned_wchar = TREE_UNSIGNED (wchar_type_node);
2a1dc0d8 4499 options->unsigned_char = !flag_signed_char;
a5a49440 4500 options->warn_multichar = warn_multichar;
5279d739 4501 options->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS;
a5a49440 4502
3df89291
NB
4503 /* Register preprocessor built-ins before calls to
4504 cpp_main_file. */
4505 cpp_get_callbacks (parse_in)->register_builtins = cb_register_builtins;
4506
aaf93206
NB
4507 /* NULL is passed up to toplev.c and we exit quickly. */
4508 if (flag_preprocess_only)
4509 {
4510 cpp_preprocess_file (parse_in);
4511 return NULL;
4512 }
4513
4d6baafa
NB
4514 /* Do this before initializing pragmas, as then cpplib's hash table
4515 has been set up. */
4516 filename = init_c_lex (filename);
4517
4518 init_pragma ();
6431177a
JM
4519
4520 if (!c_attrs_initialized)
4521 c_init_attributes ();
f5e99456
NB
4522
4523 return filename;
6431177a
JM
4524}
4525
22703ccc
NB
4526/* Common finish hook for the C, ObjC and C++ front ends. */
4527void
4528c_common_finish ()
4529{
4530 cpp_finish (parse_in);
4531
4532 /* For performance, avoid tearing down cpplib's internal structures.
4533 Call cpp_errors () instead of cpp_destroy (). */
4534 errorcount += cpp_errors (parse_in);
4535}
4536
6431177a
JM
4537static void
4538c_init_attributes ()
4539{
4540 /* Fill in the built_in_attributes array. */
4541#define DEF_ATTR_NULL_TREE(ENUM) \
4542 built_in_attributes[(int) ENUM] = NULL_TREE;
4543#define DEF_ATTR_INT(ENUM, VALUE) \
4544 built_in_attributes[(int) ENUM] = build_int_2 (VALUE, VALUE < 0 ? -1 : 0);
4545#define DEF_ATTR_IDENT(ENUM, STRING) \
4546 built_in_attributes[(int) ENUM] = get_identifier (STRING);
4547#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
4548 built_in_attributes[(int) ENUM] \
4549 = tree_cons (built_in_attributes[(int) PURPOSE], \
4550 built_in_attributes[(int) VALUE], \
4551 built_in_attributes[(int) CHAIN]);
4552#define DEF_FN_ATTR(NAME, ATTRS, PREDICATE) /* No initialization needed. */
4553#include "builtin-attrs.def"
4554#undef DEF_ATTR_NULL_TREE
4555#undef DEF_ATTR_INT
4556#undef DEF_ATTR_IDENT
4557#undef DEF_ATTR_TREE_LIST
4558#undef DEF_FN_ATTR
4559 ggc_add_tree_root (built_in_attributes, (int) ATTR_LAST);
4560 c_attrs_initialized = true;
4561}
4562
4563/* Depending on the name of DECL, apply default attributes to it. */
4564
4565void
4566c_common_insert_default_attributes (decl)
4567 tree decl;
4568{
4569 tree name = DECL_NAME (decl);
4570
4571 if (!c_attrs_initialized)
4572 c_init_attributes ();
4573
4574#define DEF_ATTR_NULL_TREE(ENUM) /* Nothing needed after initialization. */
4575#define DEF_ATTR_INT(ENUM, VALUE)
4576#define DEF_ATTR_IDENT(ENUM, STRING)
4577#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN)
4578#define DEF_FN_ATTR(NAME, ATTRS, PREDICATE) \
4579 if ((PREDICATE) && name == built_in_attributes[(int) NAME]) \
4580 decl_attributes (&decl, built_in_attributes[(int) ATTRS], \
4581 ATTR_FLAG_BUILT_IN);
4582#include "builtin-attrs.def"
4583#undef DEF_ATTR_NULL_TREE
4584#undef DEF_ATTR_INT
4585#undef DEF_ATTR_IDENT
4586#undef DEF_ATTR_TREE_LIST
4587#undef DEF_FN_ATTR
03dc0325 4588}
26f943fd
NB
4589
4590/* Output a -Wshadow warning MSGID about NAME, an IDENTIFIER_NODE, and
4591 additionally give the location of the previous declaration DECL. */
4592void
4593shadow_warning (msgid, name, decl)
4594 const char *msgid;
4595 tree name, decl;
4596{
4597 warning ("declaration of `%s' shadows %s", IDENTIFIER_POINTER (name), msgid);
4598 warning_with_file_and_line (DECL_SOURCE_FILE (decl),
4599 DECL_SOURCE_LINE (decl),
4600 "shadowed declaration is here");
4601}
4602
349ae713
NB
4603/* Attribute handlers common to C front ends. */
4604
4605/* Handle a "packed" attribute; arguments as in
4606 struct attribute_spec.handler. */
4607
4608static tree
4609handle_packed_attribute (node, name, args, flags, no_add_attrs)
4610 tree *node;
4611 tree name;
4612 tree args ATTRIBUTE_UNUSED;
4613 int flags;
4614 bool *no_add_attrs;
4615{
4616 tree *type = NULL;
4617 if (DECL_P (*node))
4618 {
4619 if (TREE_CODE (*node) == TYPE_DECL)
4620 type = &TREE_TYPE (*node);
4621 }
4622 else
4623 type = node;
4624
4625 if (type)
4626 {
4627 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
4628 *type = build_type_copy (*type);
4629 TYPE_PACKED (*type) = 1;
4630 }
4631 else if (TREE_CODE (*node) == FIELD_DECL)
4632 DECL_PACKED (*node) = 1;
4633 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
4634 used for DECL_REGISTER. It wouldn't mean anything anyway. */
4635 else
4636 {
4637 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4638 *no_add_attrs = true;
4639 }
4640
4641 return NULL_TREE;
4642}
4643
4644/* Handle a "nocommon" attribute; arguments as in
4645 struct attribute_spec.handler. */
4646
4647static tree
4648handle_nocommon_attribute (node, name, args, flags, no_add_attrs)
4649 tree *node;
4650 tree name;
4651 tree args ATTRIBUTE_UNUSED;
4652 int flags ATTRIBUTE_UNUSED;
4653 bool *no_add_attrs;
4654{
4655 if (TREE_CODE (*node) == VAR_DECL)
4656 DECL_COMMON (*node) = 0;
4657 else
4658 {
4659 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4660 *no_add_attrs = true;
4661 }
4662
4663 return NULL_TREE;
4664}
4665
4666/* Handle a "common" attribute; arguments as in
4667 struct attribute_spec.handler. */
4668
4669static tree
4670handle_common_attribute (node, name, args, flags, no_add_attrs)
4671 tree *node;
4672 tree name;
4673 tree args ATTRIBUTE_UNUSED;
4674 int flags ATTRIBUTE_UNUSED;
4675 bool *no_add_attrs;
4676{
4677 if (TREE_CODE (*node) == VAR_DECL)
4678 DECL_COMMON (*node) = 1;
4679 else
4680 {
4681 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4682 *no_add_attrs = true;
4683 }
4684
4685 return NULL_TREE;
4686}
4687
4688/* Handle a "noreturn" attribute; arguments as in
4689 struct attribute_spec.handler. */
4690
4691static tree
4692handle_noreturn_attribute (node, name, args, flags, no_add_attrs)
4693 tree *node;
4694 tree name;
4695 tree args ATTRIBUTE_UNUSED;
4696 int flags ATTRIBUTE_UNUSED;
4697 bool *no_add_attrs;
4698{
4699 tree type = TREE_TYPE (*node);
4700
4701 /* See FIXME comment in c_common_attribute_table. */
4702 if (TREE_CODE (*node) == FUNCTION_DECL)
4703 TREE_THIS_VOLATILE (*node) = 1;
4704 else if (TREE_CODE (type) == POINTER_TYPE
4705 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
4706 TREE_TYPE (*node)
4707 = build_pointer_type
4708 (build_type_variant (TREE_TYPE (type),
4709 TREE_READONLY (TREE_TYPE (type)), 1));
4710 else
4711 {
4712 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4713 *no_add_attrs = true;
4714 }
4715
4716 return NULL_TREE;
4717}
4718
4719/* Handle a "noinline" attribute; arguments as in
4720 struct attribute_spec.handler. */
4721
4722static tree
4723handle_noinline_attribute (node, name, args, flags, no_add_attrs)
4724 tree *node;
4725 tree name;
4726 tree args ATTRIBUTE_UNUSED;
4727 int flags ATTRIBUTE_UNUSED;
4728 bool *no_add_attrs;
4729{
4730 if (TREE_CODE (*node) == FUNCTION_DECL)
4731 DECL_UNINLINABLE (*node) = 1;
4732 else
4733 {
4734 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4735 *no_add_attrs = true;
4736 }
4737
4738 return NULL_TREE;
4739}
4740
4741/* Handle a "always_inline" attribute; arguments as in
4742 struct attribute_spec.handler. */
4743
4744static tree
4745handle_always_inline_attribute (node, name, args, flags, no_add_attrs)
4746 tree *node;
4747 tree name;
4748 tree args ATTRIBUTE_UNUSED;
4749 int flags ATTRIBUTE_UNUSED;
4750 bool *no_add_attrs;
4751{
4752 if (TREE_CODE (*node) == FUNCTION_DECL)
4753 {
4754 /* Do nothing else, just set the attribute. We'll get at
4755 it later with lookup_attribute. */
4756 }
4757 else
4758 {
4759 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4760 *no_add_attrs = true;
4761 }
4762
4763 return NULL_TREE;
4764}
4765
4766/* Handle a "used" attribute; arguments as in
4767 struct attribute_spec.handler. */
4768
4769static tree
4770handle_used_attribute (node, name, args, flags, no_add_attrs)
4771 tree *node;
4772 tree name;
4773 tree args ATTRIBUTE_UNUSED;
4774 int flags ATTRIBUTE_UNUSED;
4775 bool *no_add_attrs;
4776{
4777 if (TREE_CODE (*node) == FUNCTION_DECL)
4778 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (*node))
4779 = TREE_USED (*node) = 1;
4780 else
4781 {
4782 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4783 *no_add_attrs = true;
4784 }
4785
4786 return NULL_TREE;
4787}
4788
4789/* Handle a "unused" attribute; arguments as in
4790 struct attribute_spec.handler. */
4791
4792static tree
4793handle_unused_attribute (node, name, args, flags, no_add_attrs)
4794 tree *node;
4795 tree name;
4796 tree args ATTRIBUTE_UNUSED;
4797 int flags;
4798 bool *no_add_attrs;
4799{
4800 if (DECL_P (*node))
4801 {
4802 tree decl = *node;
4803
4804 if (TREE_CODE (decl) == PARM_DECL
4805 || TREE_CODE (decl) == VAR_DECL
4806 || TREE_CODE (decl) == FUNCTION_DECL
4807 || TREE_CODE (decl) == LABEL_DECL
4808 || TREE_CODE (decl) == TYPE_DECL)
4809 TREE_USED (decl) = 1;
4810 else
4811 {
4812 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4813 *no_add_attrs = true;
4814 }
4815 }
4816 else
4817 {
4818 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
4819 *node = build_type_copy (*node);
4820 TREE_USED (*node) = 1;
4821 }
4822
4823 return NULL_TREE;
4824}
4825
4826/* Handle a "const" attribute; arguments as in
4827 struct attribute_spec.handler. */
4828
4829static tree
4830handle_const_attribute (node, name, args, flags, no_add_attrs)
4831 tree *node;
4832 tree name;
4833 tree args ATTRIBUTE_UNUSED;
4834 int flags ATTRIBUTE_UNUSED;
4835 bool *no_add_attrs;
4836{
4837 tree type = TREE_TYPE (*node);
4838
4839 /* See FIXME comment on noreturn in c_common_attribute_table. */
4840 if (TREE_CODE (*node) == FUNCTION_DECL)
4841 TREE_READONLY (*node) = 1;
4842 else if (TREE_CODE (type) == POINTER_TYPE
4843 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
4844 TREE_TYPE (*node)
4845 = build_pointer_type
4846 (build_type_variant (TREE_TYPE (type), 1,
4847 TREE_THIS_VOLATILE (TREE_TYPE (type))));
4848 else
4849 {
4850 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4851 *no_add_attrs = true;
4852 }
4853
4854 return NULL_TREE;
4855}
4856
4857/* Handle a "transparent_union" attribute; arguments as in
4858 struct attribute_spec.handler. */
4859
4860static tree
4861handle_transparent_union_attribute (node, name, args, flags, no_add_attrs)
4862 tree *node;
4863 tree name;
4864 tree args ATTRIBUTE_UNUSED;
4865 int flags;
4866 bool *no_add_attrs;
4867{
4868 tree decl = NULL_TREE;
4869 tree *type = NULL;
4870 int is_type = 0;
4871
4872 if (DECL_P (*node))
4873 {
4874 decl = *node;
4875 type = &TREE_TYPE (decl);
4876 is_type = TREE_CODE (*node) == TYPE_DECL;
4877 }
4878 else if (TYPE_P (*node))
4879 type = node, is_type = 1;
4880
4881 if (is_type
4882 && TREE_CODE (*type) == UNION_TYPE
4883 && (decl == 0
4884 || (TYPE_FIELDS (*type) != 0
4885 && TYPE_MODE (*type) == DECL_MODE (TYPE_FIELDS (*type)))))
4886 {
4887 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
4888 *type = build_type_copy (*type);
4889 TYPE_TRANSPARENT_UNION (*type) = 1;
4890 }
4891 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
4892 && TREE_CODE (*type) == UNION_TYPE
4893 && TYPE_MODE (*type) == DECL_MODE (TYPE_FIELDS (*type)))
4894 DECL_TRANSPARENT_UNION (decl) = 1;
4895 else
4896 {
4897 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4898 *no_add_attrs = true;
4899 }
4900
4901 return NULL_TREE;
4902}
4903
4904/* Handle a "constructor" attribute; arguments as in
4905 struct attribute_spec.handler. */
4906
4907static tree
4908handle_constructor_attribute (node, name, args, flags, no_add_attrs)
4909 tree *node;
4910 tree name;
4911 tree args ATTRIBUTE_UNUSED;
4912 int flags ATTRIBUTE_UNUSED;
4913 bool *no_add_attrs;
4914{
4915 tree decl = *node;
4916 tree type = TREE_TYPE (decl);
4917
4918 if (TREE_CODE (decl) == FUNCTION_DECL
4919 && TREE_CODE (type) == FUNCTION_TYPE
4920 && decl_function_context (decl) == 0)
4921 {
4922 DECL_STATIC_CONSTRUCTOR (decl) = 1;
4923 TREE_USED (decl) = 1;
4924 }
4925 else
4926 {
4927 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4928 *no_add_attrs = true;
4929 }
4930
4931 return NULL_TREE;
4932}
4933
4934/* Handle a "destructor" attribute; arguments as in
4935 struct attribute_spec.handler. */
4936
4937static tree
4938handle_destructor_attribute (node, name, args, flags, no_add_attrs)
4939 tree *node;
4940 tree name;
4941 tree args ATTRIBUTE_UNUSED;
4942 int flags ATTRIBUTE_UNUSED;
4943 bool *no_add_attrs;
4944{
4945 tree decl = *node;
4946 tree type = TREE_TYPE (decl);
4947
4948 if (TREE_CODE (decl) == FUNCTION_DECL
4949 && TREE_CODE (type) == FUNCTION_TYPE
4950 && decl_function_context (decl) == 0)
4951 {
4952 DECL_STATIC_DESTRUCTOR (decl) = 1;
4953 TREE_USED (decl) = 1;
4954 }
4955 else
4956 {
4957 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4958 *no_add_attrs = true;
4959 }
4960
4961 return NULL_TREE;
4962}
4963
4964/* Handle a "mode" attribute; arguments as in
4965 struct attribute_spec.handler. */
4966
4967static tree
4968handle_mode_attribute (node, name, args, flags, no_add_attrs)
4969 tree *node;
4970 tree name;
4971 tree args;
4972 int flags ATTRIBUTE_UNUSED;
4973 bool *no_add_attrs;
4974{
4975 tree type = *node;
4976
4977 *no_add_attrs = true;
4978
4979 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
4980 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4981 else
4982 {
4983 int j;
4984 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
4985 int len = strlen (p);
4986 enum machine_mode mode = VOIDmode;
4987 tree typefm;
4988
4989 if (len > 4 && p[0] == '_' && p[1] == '_'
4990 && p[len - 1] == '_' && p[len - 2] == '_')
4991 {
4992 char *newp = (char *) alloca (len - 1);
4993
4994 strcpy (newp, &p[2]);
4995 newp[len - 4] = '\0';
4996 p = newp;
4997 }
4998
4999 /* Change this type to have a type with the specified mode.
5000 First check for the special modes. */
5001 if (! strcmp (p, "byte"))
5002 mode = byte_mode;
5003 else if (!strcmp (p, "word"))
5004 mode = word_mode;
5005 else if (! strcmp (p, "pointer"))
5006 mode = ptr_mode;
5007 else
5008 for (j = 0; j < NUM_MACHINE_MODES; j++)
5009 if (!strcmp (p, GET_MODE_NAME (j)))
5010 mode = (enum machine_mode) j;
5011
5012 if (mode == VOIDmode)
5013 error ("unknown machine mode `%s'", p);
5014 else if (0 == (typefm = (*lang_hooks.types.type_for_mode)
5015 (mode, TREE_UNSIGNED (type))))
5016 error ("no data type for mode `%s'", p);
5017 else
5018 *node = typefm;
5019 /* No need to layout the type here. The caller should do this. */
5020 }
5021
5022 return NULL_TREE;
5023}
5024
5025/* Handle a "section" attribute; arguments as in
5026 struct attribute_spec.handler. */
5027
5028static tree
5029handle_section_attribute (node, name, args, flags, no_add_attrs)
5030 tree *node;
5031 tree name ATTRIBUTE_UNUSED;
5032 tree args;
5033 int flags ATTRIBUTE_UNUSED;
5034 bool *no_add_attrs;
5035{
5036 tree decl = *node;
5037
5038 if (targetm.have_named_sections)
5039 {
5040 if ((TREE_CODE (decl) == FUNCTION_DECL
5041 || TREE_CODE (decl) == VAR_DECL)
5042 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
5043 {
5044 if (TREE_CODE (decl) == VAR_DECL
5045 && current_function_decl != NULL_TREE
5046 && ! TREE_STATIC (decl))
5047 {
5048 error_with_decl (decl,
5049 "section attribute cannot be specified for local variables");
5050 *no_add_attrs = true;
5051 }
5052
5053 /* The decl may have already been given a section attribute
5054 from a previous declaration. Ensure they match. */
5055 else if (DECL_SECTION_NAME (decl) != NULL_TREE
5056 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
5057 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
5058 {
5059 error_with_decl (*node,
5060 "section of `%s' conflicts with previous declaration");
5061 *no_add_attrs = true;
5062 }
5063 else
5064 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
5065 }
5066 else
5067 {
5068 error_with_decl (*node,
5069 "section attribute not allowed for `%s'");
5070 *no_add_attrs = true;
5071 }
5072 }
5073 else
5074 {
5075 error_with_decl (*node,
5076 "section attributes are not supported for this target");
5077 *no_add_attrs = true;
5078 }
5079
5080 return NULL_TREE;
5081}
5082
5083/* Handle a "aligned" attribute; arguments as in
5084 struct attribute_spec.handler. */
5085
5086static tree
5087handle_aligned_attribute (node, name, args, flags, no_add_attrs)
5088 tree *node;
5089 tree name ATTRIBUTE_UNUSED;
5090 tree args;
5091 int flags;
5092 bool *no_add_attrs;
5093{
5094 tree decl = NULL_TREE;
5095 tree *type = NULL;
5096 int is_type = 0;
5097 tree align_expr = (args ? TREE_VALUE (args)
5098 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
5099 int i;
5100
5101 if (DECL_P (*node))
5102 {
5103 decl = *node;
5104 type = &TREE_TYPE (decl);
5105 is_type = TREE_CODE (*node) == TYPE_DECL;
5106 }
5107 else if (TYPE_P (*node))
5108 type = node, is_type = 1;
5109
5110 /* Strip any NOPs of any kind. */
5111 while (TREE_CODE (align_expr) == NOP_EXPR
5112 || TREE_CODE (align_expr) == CONVERT_EXPR
5113 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
5114 align_expr = TREE_OPERAND (align_expr, 0);
5115
5116 if (TREE_CODE (align_expr) != INTEGER_CST)
5117 {
5118 error ("requested alignment is not a constant");
5119 *no_add_attrs = true;
5120 }
5121 else if ((i = tree_log2 (align_expr)) == -1)
5122 {
5123 error ("requested alignment is not a power of 2");
5124 *no_add_attrs = true;
5125 }
5126 else if (i > HOST_BITS_PER_INT - 2)
5127 {
5128 error ("requested alignment is too large");
5129 *no_add_attrs = true;
5130 }
5131 else if (is_type)
5132 {
5133 /* If we have a TYPE_DECL, then copy the type, so that we
5134 don't accidentally modify a builtin type. See pushdecl. */
5135 if (decl && TREE_TYPE (decl) != error_mark_node
5136 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
5137 {
5138 tree tt = TREE_TYPE (decl);
5139 *type = build_type_copy (*type);
5140 DECL_ORIGINAL_TYPE (decl) = tt;
5141 TYPE_NAME (*type) = decl;
5142 TREE_USED (*type) = TREE_USED (decl);
5143 TREE_TYPE (decl) = *type;
5144 }
5145 else if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
5146 *type = build_type_copy (*type);
5147
5148 TYPE_ALIGN (*type) = (1 << i) * BITS_PER_UNIT;
5149 TYPE_USER_ALIGN (*type) = 1;
5150 }
5151 else if (TREE_CODE (decl) != VAR_DECL
5152 && TREE_CODE (decl) != FIELD_DECL)
5153 {
5154 error_with_decl (decl,
5155 "alignment may not be specified for `%s'");
5156 *no_add_attrs = true;
5157 }
5158 else
5159 {
5160 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
5161 DECL_USER_ALIGN (decl) = 1;
5162 }
5163
5164 return NULL_TREE;
5165}
5166
5167/* Handle a "weak" attribute; arguments as in
5168 struct attribute_spec.handler. */
5169
5170static tree
5171handle_weak_attribute (node, name, args, flags, no_add_attrs)
5172 tree *node;
5173 tree name ATTRIBUTE_UNUSED;
5174 tree args ATTRIBUTE_UNUSED;
5175 int flags ATTRIBUTE_UNUSED;
5176 bool *no_add_attrs ATTRIBUTE_UNUSED;
5177{
5178 declare_weak (*node);
5179
5180 return NULL_TREE;
5181}
5182
5183/* Handle an "alias" attribute; arguments as in
5184 struct attribute_spec.handler. */
5185
5186static tree
5187handle_alias_attribute (node, name, args, flags, no_add_attrs)
5188 tree *node;
5189 tree name;
5190 tree args;
5191 int flags ATTRIBUTE_UNUSED;
5192 bool *no_add_attrs;
5193{
5194 tree decl = *node;
5195
5196 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
5197 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
5198 {
5199 error_with_decl (decl,
5200 "`%s' defined both normally and as an alias");
5201 *no_add_attrs = true;
5202 }
5203 else if (decl_function_context (decl) == 0)
5204 {
5205 tree id;
5206
5207 id = TREE_VALUE (args);
5208 if (TREE_CODE (id) != STRING_CST)
5209 {
5210 error ("alias arg not a string");
5211 *no_add_attrs = true;
5212 return NULL_TREE;
5213 }
5214 id = get_identifier (TREE_STRING_POINTER (id));
5215 /* This counts as a use of the object pointed to. */
5216 TREE_USED (id) = 1;
5217
5218 if (TREE_CODE (decl) == FUNCTION_DECL)
5219 DECL_INITIAL (decl) = error_mark_node;
5220 else
5221 DECL_EXTERNAL (decl) = 0;
5222 }
5223 else
5224 {
5225 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5226 *no_add_attrs = true;
5227 }
5228
5229 return NULL_TREE;
5230}
5231
5232/* Handle an "visibility" attribute; arguments as in
5233 struct attribute_spec.handler. */
5234
5235static tree
5236handle_visibility_attribute (node, name, args, flags, no_add_attrs)
5237 tree *node;
5238 tree name;
5239 tree args;
5240 int flags ATTRIBUTE_UNUSED;
5241 bool *no_add_attrs;
5242{
5243 tree decl = *node;
5244
5245 if (decl_function_context (decl) != 0 || ! TREE_PUBLIC (decl))
5246 {
5247 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5248 *no_add_attrs = true;
5249 }
5250 else
5251 {
5252 tree id;
5253
5254 id = TREE_VALUE (args);
5255 if (TREE_CODE (id) != STRING_CST)
5256 {
5257 error ("visibility arg not a string");
5258 *no_add_attrs = true;
5259 return NULL_TREE;
5260 }
5261 if (strcmp (TREE_STRING_POINTER (id), "hidden")
5262 && strcmp (TREE_STRING_POINTER (id), "protected")
5263 && strcmp (TREE_STRING_POINTER (id), "internal"))
5264 {
5265 error ("visibility arg must be one of \"hidden\", \"protected\" or \"internal\"");
5266 *no_add_attrs = true;
5267 return NULL_TREE;
5268 }
5269 }
5270
5271 return NULL_TREE;
5272}
5273
5274/* Handle a "no_instrument_function" attribute; arguments as in
5275 struct attribute_spec.handler. */
5276
5277static tree
5278handle_no_instrument_function_attribute (node, name, args, flags, no_add_attrs)
5279 tree *node;
5280 tree name;
5281 tree args ATTRIBUTE_UNUSED;
5282 int flags ATTRIBUTE_UNUSED;
5283 bool *no_add_attrs;
5284{
5285 tree decl = *node;
5286
5287 if (TREE_CODE (decl) != FUNCTION_DECL)
5288 {
5289 error_with_decl (decl,
5290 "`%s' attribute applies only to functions",
5291 IDENTIFIER_POINTER (name));
5292 *no_add_attrs = true;
5293 }
5294 else if (DECL_INITIAL (decl))
5295 {
5296 error_with_decl (decl,
5297 "can't set `%s' attribute after definition",
5298 IDENTIFIER_POINTER (name));
5299 *no_add_attrs = true;
5300 }
5301 else
5302 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
5303
5304 return NULL_TREE;
5305}
5306
5307/* Handle a "malloc" attribute; arguments as in
5308 struct attribute_spec.handler. */
5309
5310static tree
5311handle_malloc_attribute (node, name, args, flags, no_add_attrs)
5312 tree *node;
5313 tree name;
5314 tree args ATTRIBUTE_UNUSED;
5315 int flags ATTRIBUTE_UNUSED;
5316 bool *no_add_attrs;
5317{
5318 if (TREE_CODE (*node) == FUNCTION_DECL)
5319 DECL_IS_MALLOC (*node) = 1;
5320 /* ??? TODO: Support types. */
5321 else
5322 {
5323 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5324 *no_add_attrs = true;
5325 }
5326
5327 return NULL_TREE;
5328}
5329
5330/* Handle a "no_limit_stack" attribute; arguments as in
5331 struct attribute_spec.handler. */
5332
5333static tree
5334handle_no_limit_stack_attribute (node, name, args, flags, no_add_attrs)
5335 tree *node;
5336 tree name;
5337 tree args ATTRIBUTE_UNUSED;
5338 int flags ATTRIBUTE_UNUSED;
5339 bool *no_add_attrs;
5340{
5341 tree decl = *node;
5342
5343 if (TREE_CODE (decl) != FUNCTION_DECL)
5344 {
5345 error_with_decl (decl,
5346 "`%s' attribute applies only to functions",
5347 IDENTIFIER_POINTER (name));
5348 *no_add_attrs = true;
5349 }
5350 else if (DECL_INITIAL (decl))
5351 {
5352 error_with_decl (decl,
5353 "can't set `%s' attribute after definition",
5354 IDENTIFIER_POINTER (name));
5355 *no_add_attrs = true;
5356 }
5357 else
5358 DECL_NO_LIMIT_STACK (decl) = 1;
5359
5360 return NULL_TREE;
5361}
5362
5363/* Handle a "pure" attribute; arguments as in
5364 struct attribute_spec.handler. */
5365
5366static tree
5367handle_pure_attribute (node, name, args, flags, no_add_attrs)
5368 tree *node;
5369 tree name;
5370 tree args ATTRIBUTE_UNUSED;
5371 int flags ATTRIBUTE_UNUSED;
5372 bool *no_add_attrs;
5373{
5374 if (TREE_CODE (*node) == FUNCTION_DECL)
5375 DECL_IS_PURE (*node) = 1;
5376 /* ??? TODO: Support types. */
5377 else
5378 {
5379 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5380 *no_add_attrs = true;
5381 }
5382
5383 return NULL_TREE;
5384}
5385
5386/* Handle a "deprecated" attribute; arguments as in
5387 struct attribute_spec.handler. */
5388
5389static tree
5390handle_deprecated_attribute (node, name, args, flags, no_add_attrs)
5391 tree *node;
5392 tree name;
5393 tree args ATTRIBUTE_UNUSED;
5394 int flags;
5395 bool *no_add_attrs;
5396{
5397 tree type = NULL_TREE;
5398 int warn = 0;
5399 const char *what = NULL;
5400
5401 if (DECL_P (*node))
5402 {
5403 tree decl = *node;
5404 type = TREE_TYPE (decl);
5405
5406 if (TREE_CODE (decl) == TYPE_DECL
5407 || TREE_CODE (decl) == PARM_DECL
5408 || TREE_CODE (decl) == VAR_DECL
5409 || TREE_CODE (decl) == FUNCTION_DECL
5410 || TREE_CODE (decl) == FIELD_DECL)
5411 TREE_DEPRECATED (decl) = 1;
5412 else
5413 warn = 1;
5414 }
5415 else if (TYPE_P (*node))
5416 {
5417 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
5418 *node = build_type_copy (*node);
5419 TREE_DEPRECATED (*node) = 1;
5420 type = *node;
5421 }
5422 else
5423 warn = 1;
5424
5425 if (warn)
5426 {
5427 *no_add_attrs = true;
5428 if (type && TYPE_NAME (type))
5429 {
5430 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
5431 what = IDENTIFIER_POINTER (TYPE_NAME (*node));
5432 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
5433 && DECL_NAME (TYPE_NAME (type)))
5434 what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
5435 }
5436 if (what)
5437 warning ("`%s' attribute ignored for `%s'",
5438 IDENTIFIER_POINTER (name), what);
5439 else
5440 warning ("`%s' attribute ignored",
5441 IDENTIFIER_POINTER (name));
5442 }
5443
5444 return NULL_TREE;
5445}
5446
5447/* Keep a list of vector type nodes we created in handle_vector_size_attribute,
5448 to prevent us from duplicating type nodes unnecessarily.
5449 The normal mechanism to prevent duplicates is to use type_hash_canon, but
5450 since we want to distinguish types that are essentially identical (except
5451 for their debug representation), we use a local list here. */
5452static tree vector_type_node_list = 0;
5453
5454/* Handle a "vector_size" attribute; arguments as in
5455 struct attribute_spec.handler. */
5456
5457static tree
5458handle_vector_size_attribute (node, name, args, flags, no_add_attrs)
5459 tree *node;
5460 tree name;
5461 tree args;
5462 int flags ATTRIBUTE_UNUSED;
5463 bool *no_add_attrs;
5464{
5465 unsigned HOST_WIDE_INT vecsize, nunits;
5466 enum machine_mode mode, orig_mode, new_mode;
5467 tree type = *node, new_type = NULL_TREE;
5468 tree type_list_node;
5469
5470 *no_add_attrs = true;
5471
5472 if (! host_integerp (TREE_VALUE (args), 1))
5473 {
5474 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5475 return NULL_TREE;
5476 }
5477
5478 /* Get the vector size (in bytes). */
5479 vecsize = tree_low_cst (TREE_VALUE (args), 1);
5480
5481 /* We need to provide for vector pointers, vector arrays, and
5482 functions returning vectors. For example:
5483
5484 __attribute__((vector_size(16))) short *foo;
5485
5486 In this case, the mode is SI, but the type being modified is
5487 HI, so we need to look further. */
5488
5489 while (POINTER_TYPE_P (type)
5490 || TREE_CODE (type) == FUNCTION_TYPE
5491 || TREE_CODE (type) == ARRAY_TYPE)
5492 type = TREE_TYPE (type);
5493
5494 /* Get the mode of the type being modified. */
5495 orig_mode = TYPE_MODE (type);
5496
5497 if (TREE_CODE (type) == RECORD_TYPE
5498 || (GET_MODE_CLASS (orig_mode) != MODE_FLOAT
5499 && GET_MODE_CLASS (orig_mode) != MODE_INT)
5500 || ! host_integerp (TYPE_SIZE_UNIT (type), 1))
5501 {
5502 error ("invalid vector type for attribute `%s'",
5503 IDENTIFIER_POINTER (name));
5504 return NULL_TREE;
5505 }
5506
5507 /* Calculate how many units fit in the vector. */
5508 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
5509
5510 /* Find a suitably sized vector. */
5511 new_mode = VOIDmode;
5512 for (mode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_mode) == MODE_INT
5513 ? MODE_VECTOR_INT
5514 : MODE_VECTOR_FLOAT);
5515 mode != VOIDmode;
5516 mode = GET_MODE_WIDER_MODE (mode))
5517 if (vecsize == GET_MODE_SIZE (mode)
5518 && nunits == (unsigned HOST_WIDE_INT) GET_MODE_NUNITS (mode))
5519 {
5520 new_mode = mode;
5521 break;
5522 }
5523
5524 if (new_mode == VOIDmode)
5525 {
5526 error ("no vector mode with the size and type specified could be found");
5527 return NULL_TREE;
5528 }
5529
5530 for (type_list_node = vector_type_node_list; type_list_node;
5531 type_list_node = TREE_CHAIN (type_list_node))
5532 {
5533 tree other_type = TREE_VALUE (type_list_node);
5534 tree record = TYPE_DEBUG_REPRESENTATION_TYPE (other_type);
5535 tree fields = TYPE_FIELDS (record);
5536 tree field_type = TREE_TYPE (fields);
5537 tree array_type = TREE_TYPE (field_type);
5538 if (TREE_CODE (fields) != FIELD_DECL
5539 || TREE_CODE (field_type) != ARRAY_TYPE)
5540 abort ();
5541
5542 if (TYPE_MODE (other_type) == mode && type == array_type)
5543 {
5544 new_type = other_type;
5545 break;
5546 }
5547 }
5548
5549 if (new_type == NULL_TREE)
5550 {
5551 tree index, array, rt, list_node;
5552
5553 new_type = (*lang_hooks.types.type_for_mode) (new_mode,
5554 TREE_UNSIGNED (type));
5555
5556 if (!new_type)
5557 {
5558 error ("no vector mode with the size and type specified could be found");
5559 return NULL_TREE;
5560 }
5561
5562 new_type = build_type_copy (new_type);
5563
5564 /* Set the debug information here, because this is the only
5565 place where we know the underlying type for a vector made
5566 with vector_size. For debugging purposes we pretend a vector
5567 is an array within a structure. */
5568 index = build_int_2 (TYPE_VECTOR_SUBPARTS (new_type) - 1, 0);
5569 array = build_array_type (type, build_index_type (index));
5570 rt = make_node (RECORD_TYPE);
5571
5572 TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
5573 DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
5574 layout_type (rt);
5575 TYPE_DEBUG_REPRESENTATION_TYPE (new_type) = rt;
5576
5577 list_node = build_tree_list (NULL, new_type);
5578 TREE_CHAIN (list_node) = vector_type_node_list;
5579 vector_type_node_list = list_node;
5580 }
5581
5582 /* Build back pointers if needed. */
5583 *node = vector_size_helper (*node, new_type);
5584
5585 return NULL_TREE;
5586}
5587
5588/* HACK. GROSS. This is absolutely disgusting. I wish there was a
5589 better way.
5590
5591 If we requested a pointer to a vector, build up the pointers that
5592 we stripped off while looking for the inner type. Similarly for
5593 return values from functions.
5594
5595 The argument "type" is the top of the chain, and "bottom" is the
5596 new type which we will point to. */
5597
5598static tree
5599vector_size_helper (type, bottom)
5600 tree type, bottom;
5601{
5602 tree inner, outer;
5603
5604 if (POINTER_TYPE_P (type))
5605 {
5606 inner = vector_size_helper (TREE_TYPE (type), bottom);
5607 outer = build_pointer_type (inner);
5608 }
5609 else if (TREE_CODE (type) == ARRAY_TYPE)
5610 {
5611 inner = vector_size_helper (TREE_TYPE (type), bottom);
5612 outer = build_array_type (inner, TYPE_VALUES (type));
5613 }
5614 else if (TREE_CODE (type) == FUNCTION_TYPE)
5615 {
5616 inner = vector_size_helper (TREE_TYPE (type), bottom);
5617 outer = build_function_type (inner, TYPE_VALUES (type));
5618 }
5619 else
5620 return bottom;
5621
5622 TREE_READONLY (outer) = TREE_READONLY (type);
5623 TREE_THIS_VOLATILE (outer) = TREE_THIS_VOLATILE (type);
5624
5625 return outer;
5626}
b34c7881
JT
5627
5628/* Handle the "nonnull" attribute. */
5629static tree
5630handle_nonnull_attribute (node, name, args, flags, no_add_attrs)
5631 tree *node;
5632 tree name ATTRIBUTE_UNUSED;
5633 tree args;
5634 int flags ATTRIBUTE_UNUSED;
5635 bool *no_add_attrs;
5636{
5637 tree type = *node;
5638 unsigned HOST_WIDE_INT attr_arg_num;
5639
5640 /* If no arguments are specified, all pointer arguments should be
5641 non-null. Veryify a full prototype is given so that the arguments
5642 will have the correct types when we actually check them later. */
5643 if (! args)
5644 {
5645 if (! TYPE_ARG_TYPES (type))
5646 {
5647 error ("nonnull attribute without arguments on a non-prototype");
5648 *no_add_attrs = true;
5649 }
5650 return NULL_TREE;
5651 }
5652
5653 /* Argument list specified. Verify that each argument number references
5654 a pointer argument. */
5655 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
5656 {
5657 tree argument;
5658 unsigned HOST_WIDE_INT arg_num, ck_num;
5659
5660 if (! get_nonnull_operand (TREE_VALUE (args), &arg_num))
5661 {
5662 error ("nonnull argument has invalid operand number (arg %lu)",
5663 (unsigned long) attr_arg_num);
5664 *no_add_attrs = true;
5665 return NULL_TREE;
5666 }
5667
5668 argument = TYPE_ARG_TYPES (type);
5669 if (argument)
5670 {
5671 for (ck_num = 1; ; ck_num++)
5672 {
5673 if (! argument || ck_num == arg_num)
5674 break;
5675 argument = TREE_CHAIN (argument);
5676 }
5677
5678 if (! argument
5679 || TREE_CODE (TREE_VALUE (argument)) == VOID_TYPE)
5680 {
5681 error ("nonnull argument with out-of-range operand number (arg %lu, operand %lu)",
5682 (unsigned long) attr_arg_num, (unsigned long) arg_num);
5683 *no_add_attrs = true;
5684 return NULL_TREE;
5685 }
5686
5687 if (TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE)
5688 {
5689 error ("nonnull argument references non-pointer operand (arg %lu, operand %lu)",
5690 (unsigned long) attr_arg_num, (unsigned long) arg_num);
5691 *no_add_attrs = true;
5692 return NULL_TREE;
5693 }
5694 }
5695 }
5696
5697 return NULL_TREE;
5698}
5699
5700/* Check the argument list of a function call for null in argument slots
5701 that are marked as requiring a non-null pointer argument. */
5702
5703static void
5704check_function_nonnull (attrs, params)
5705 tree attrs;
5706 tree params;
5707{
5708 tree a, args, param;
5709 int param_num;
5710
5711 for (a = attrs; a; a = TREE_CHAIN (a))
5712 {
5713 if (is_attribute_p ("nonnull", TREE_PURPOSE (a)))
5714 {
5715 args = TREE_VALUE (a);
5716
5717 /* Walk the argument list. If we encounter an argument number we
5718 should check for non-null, do it. If the attribute has no args,
5719 then every pointer argument is checked (in which case the check
5720 for pointer type is done in check_nonnull_arg). */
5721 for (param = params, param_num = 1; ;
5722 param_num++, param = TREE_CHAIN (param))
5723 {
5724 if (! param)
5725 break;
5726 if (! args || nonnull_check_p (args, param_num))
5727 check_function_arguments_recurse (check_nonnull_arg, NULL,
5728 TREE_VALUE (param),
5729 param_num);
5730 }
5731 }
5732 }
5733}
5734
5735/* Helper for check_function_nonnull; given a list of operands which
5736 must be non-null in ARGS, determine if operand PARAM_NUM should be
5737 checked. */
5738
5739static bool
5740nonnull_check_p (args, param_num)
5741 tree args;
5742 unsigned HOST_WIDE_INT param_num;
5743{
5744 unsigned HOST_WIDE_INT arg_num;
5745
5746 for (; args; args = TREE_CHAIN (args))
5747 {
5748 if (! get_nonnull_operand (TREE_VALUE (args), &arg_num))
5749 abort ();
5750
5751 if (arg_num == param_num)
5752 return true;
5753 }
5754 return false;
5755}
5756
5757/* Check that the function argument PARAM (which is operand number
5758 PARAM_NUM) is non-null. This is called by check_function_nonnull
5759 via check_function_arguments_recurse. */
5760
5761static void
5762check_nonnull_arg (ctx, param, param_num)
5763 void *ctx ATTRIBUTE_UNUSED;
5764 tree param;
5765 unsigned HOST_WIDE_INT param_num;
5766{
5767 /* Just skip checking the argument if it's not a pointer. This can
5768 happen if the "nonnull" attribute was given without an operand
5769 list (which means to check every pointer argument). */
5770
5771 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
5772 return;
5773
5774 if (integer_zerop (param))
5775 warning ("null argument where non-null required (arg %lu)",
5776 (unsigned long) param_num);
5777}
5778
5779/* Helper for nonnull attribute handling; fetch the operand number
5780 from the attribute argument list. */
5781
5782static bool
5783get_nonnull_operand (arg_num_expr, valp)
5784 tree arg_num_expr;
5785 unsigned HOST_WIDE_INT *valp;
5786{
5787 /* Strip any conversions from the arg number and verify they
5788 are constants. */
5789 while (TREE_CODE (arg_num_expr) == NOP_EXPR
5790 || TREE_CODE (arg_num_expr) == CONVERT_EXPR
5791 || TREE_CODE (arg_num_expr) == NON_LVALUE_EXPR)
5792 arg_num_expr = TREE_OPERAND (arg_num_expr, 0);
5793
5794 if (TREE_CODE (arg_num_expr) != INTEGER_CST
5795 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
5796 return false;
5797
5798 *valp = TREE_INT_CST_LOW (arg_num_expr);
5799 return true;
5800}
5801\f
5802/* Check for valid arguments being passed to a function. */
5803void
5804check_function_arguments (attrs, params)
5805 tree attrs;
5806 tree params;
5807{
5808 /* Check for null being passed in a pointer argument that must be
5809 non-null. We also need to do this if format checking is enabled. */
5810
5811 if (warn_nonnull)
5812 check_function_nonnull (attrs, params);
5813
5814 /* Check for errors in format strings. */
5815
5816 if (warn_format)
5817 check_function_format (NULL, attrs, params);
5818}
5819
5820/* Generic argument checking recursion routine. PARAM is the argument to
5821 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
5822 once the argument is resolved. CTX is context for the callback. */
5823void
5824check_function_arguments_recurse (callback, ctx, param, param_num)
5825 void (*callback) PARAMS ((void *, tree, unsigned HOST_WIDE_INT));
5826 void *ctx;
5827 tree param;
5828 unsigned HOST_WIDE_INT param_num;
5829{
5830 if (TREE_CODE (param) == NOP_EXPR)
5831 {
5832 /* Strip coercion. */
5833 check_function_arguments_recurse (callback, ctx,
5834 TREE_OPERAND (param, 0), param_num);
5835 return;
5836 }
5837
5838 if (TREE_CODE (param) == CALL_EXPR)
5839 {
5840 tree type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (param, 0)));
5841 tree attrs;
5842 bool found_format_arg = false;
5843
5844 /* See if this is a call to a known internationalization function
5845 that modifies a format arg. Such a function may have multiple
5846 format_arg attributes (for example, ngettext). */
5847
5848 for (attrs = TYPE_ATTRIBUTES (type);
5849 attrs;
5850 attrs = TREE_CHAIN (attrs))
5851 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
5852 {
5853 tree inner_args;
5854 tree format_num_expr;
5855 int format_num;
5856 int i;
5857
5858 /* Extract the argument number, which was previously checked
5859 to be valid. */
5860 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
5861 while (TREE_CODE (format_num_expr) == NOP_EXPR
5862 || TREE_CODE (format_num_expr) == CONVERT_EXPR
5863 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
5864 format_num_expr = TREE_OPERAND (format_num_expr, 0);
5865
5866 if (TREE_CODE (format_num_expr) != INTEGER_CST
5867 || TREE_INT_CST_HIGH (format_num_expr) != 0)
5868 abort ();
5869
5870 format_num = TREE_INT_CST_LOW (format_num_expr);
5871
5872 for (inner_args = TREE_OPERAND (param, 1), i = 1;
5873 inner_args != 0;
5874 inner_args = TREE_CHAIN (inner_args), i++)
5875 if (i == format_num)
5876 {
5877 check_function_arguments_recurse (callback, ctx,
5878 TREE_VALUE (inner_args),
5879 param_num);
5880 found_format_arg = true;
5881 break;
5882 }
5883 }
5884
5885 /* If we found a format_arg attribute and did a recursive check,
5886 we are done with checking this argument. Otherwise, we continue
5887 and this will be considered a non-literal. */
5888 if (found_format_arg)
5889 return;
5890 }
5891
5892 if (TREE_CODE (param) == COND_EXPR)
5893 {
5894 /* Check both halves of the conditional expression. */
5895 check_function_arguments_recurse (callback, ctx,
5896 TREE_OPERAND (param, 1), param_num);
5897 check_function_arguments_recurse (callback, ctx,
5898 TREE_OPERAND (param, 2), param_num);
5899 return;
5900 }
5901
5902 (*callback) (ctx, param, param_num);
5903}
This page took 1.754299 seconds and 5 git commands to generate.