* config/i386/netbsd-elf.h (LINK_SPEC): Define as
[official-gcc.git] / gcc / c-common.c
blobeaa812d587fdde270ad41c7a095cd1947438d5cf
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "tree.h"
25 #include "real.h"
26 #include "flags.h"
27 #include "toplev.h"
28 #include "output.h"
29 #include "c-pragma.h"
30 #include "rtl.h"
31 #include "ggc.h"
32 #include "expr.h"
33 #include "c-common.h"
34 #include "tree-inline.h"
35 #include "diagnostic.h"
36 #include "tm_p.h"
37 #include "obstack.h"
38 #include "cpplib.h"
39 #include "target.h"
40 #include "langhooks.h"
41 #include "except.h" /* For USING_SJLJ_EXCEPTIONS. */
43 cpp_reader *parse_in; /* Declared in c-pragma.h. */
45 /* We let tm.h override the types used here, to handle trivial differences
46 such as the choice of unsigned int or long unsigned int for size_t.
47 When machines start needing nontrivial differences in the size type,
48 it would be best to do something here to figure out automatically
49 from other information what type to use. */
51 #ifndef SIZE_TYPE
52 #define SIZE_TYPE "long unsigned int"
53 #endif
55 #ifndef WCHAR_TYPE
56 #define WCHAR_TYPE "int"
57 #endif
59 /* WCHAR_TYPE gets overridden by -fshort-wchar. */
60 #define MODIFIED_WCHAR_TYPE \
61 (flag_short_wchar ? "short unsigned int" : WCHAR_TYPE)
63 #ifndef PTRDIFF_TYPE
64 #define PTRDIFF_TYPE "long int"
65 #endif
67 #ifndef WINT_TYPE
68 #define WINT_TYPE "unsigned int"
69 #endif
71 #ifndef INTMAX_TYPE
72 #define INTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
73 ? "int" \
74 : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
75 ? "long int" \
76 : "long long int"))
77 #endif
79 #ifndef UINTMAX_TYPE
80 #define UINTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
81 ? "unsigned int" \
82 : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
83 ? "long unsigned int" \
84 : "long long unsigned int"))
85 #endif
87 #ifndef STDC_0_IN_SYSTEM_HEADERS
88 #define STDC_0_IN_SYSTEM_HEADERS 0
89 #endif
91 #ifndef REGISTER_PREFIX
92 #define REGISTER_PREFIX ""
93 #endif
95 /* The variant of the C language being processed. */
97 enum c_language_kind c_language;
99 /* The following symbols are subsumed in the c_global_trees array, and
100 listed here individually for documentation purposes.
102 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
104 tree short_integer_type_node;
105 tree long_integer_type_node;
106 tree long_long_integer_type_node;
108 tree short_unsigned_type_node;
109 tree long_unsigned_type_node;
110 tree long_long_unsigned_type_node;
112 tree boolean_type_node;
113 tree boolean_false_node;
114 tree boolean_true_node;
116 tree ptrdiff_type_node;
118 tree unsigned_char_type_node;
119 tree signed_char_type_node;
120 tree wchar_type_node;
121 tree signed_wchar_type_node;
122 tree unsigned_wchar_type_node;
124 tree float_type_node;
125 tree double_type_node;
126 tree long_double_type_node;
128 tree complex_integer_type_node;
129 tree complex_float_type_node;
130 tree complex_double_type_node;
131 tree complex_long_double_type_node;
133 tree intQI_type_node;
134 tree intHI_type_node;
135 tree intSI_type_node;
136 tree intDI_type_node;
137 tree intTI_type_node;
139 tree unsigned_intQI_type_node;
140 tree unsigned_intHI_type_node;
141 tree unsigned_intSI_type_node;
142 tree unsigned_intDI_type_node;
143 tree unsigned_intTI_type_node;
145 tree widest_integer_literal_type_node;
146 tree widest_unsigned_literal_type_node;
148 Nodes for types `void *' and `const void *'.
150 tree ptr_type_node, const_ptr_type_node;
152 Nodes for types `char *' and `const char *'.
154 tree string_type_node, const_string_type_node;
156 Type `char[SOMENUMBER]'.
157 Used when an array of char is needed and the size is irrelevant.
159 tree char_array_type_node;
161 Type `int[SOMENUMBER]' or something like it.
162 Used when an array of int needed and the size is irrelevant.
164 tree int_array_type_node;
166 Type `wchar_t[SOMENUMBER]' or something like it.
167 Used when a wide string literal is created.
169 tree wchar_array_type_node;
171 Type `int ()' -- used for implicit declaration of functions.
173 tree default_function_type;
175 A VOID_TYPE node, packaged in a TREE_LIST.
177 tree void_list_node;
179 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
180 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
181 VAR_DECLS, but C++ does.)
183 tree function_name_decl_node;
184 tree pretty_function_name_decl_node;
185 tree c99_function_name_decl_node;
187 Stack of nested function name VAR_DECLs.
189 tree saved_function_name_decls;
193 tree c_global_trees[CTI_MAX];
195 /* Nonzero if prepreprocessing only. */
196 int flag_preprocess_only;
198 /* Nonzero if an ISO standard was selected. It rejects macros in the
199 user's namespace. */
200 int flag_iso;
202 /* Nonzero if -undef was given. It suppresses target built-in macros
203 and assertions. */
204 int flag_undef;
206 /* Nonzero means don't recognize the non-ANSI builtin functions. */
208 int flag_no_builtin;
210 /* Nonzero means don't recognize the non-ANSI builtin functions.
211 -ansi sets this. */
213 int flag_no_nonansi_builtin;
215 /* Nonzero means give `double' the same size as `float'. */
217 int flag_short_double;
219 /* Nonzero means give `wchar_t' the same size as `short'. */
221 int flag_short_wchar;
223 /* Nonzero means warn about use of multicharacter literals. */
225 int warn_multichar = 1;
227 /* Nonzero means warn about possible violations of sequence point rules. */
229 int warn_sequence_point;
231 /* Nonzero means to warn about compile-time division by zero. */
232 int warn_div_by_zero = 1;
234 /* Warn about NULL being passed to argument slots marked as requiring
235 non-NULL. */
237 int warn_nonnull;
239 /* The elements of `ridpointers' are identifier nodes for the reserved
240 type names and storage classes. It is indexed by a RID_... value. */
241 tree *ridpointers;
243 tree (*make_fname_decl) PARAMS ((tree, int));
245 /* If non-NULL, the address of a language-specific function that
246 returns 1 for language-specific statement codes. */
247 int (*lang_statement_code_p) PARAMS ((enum tree_code));
249 /* If non-NULL, the address of a language-specific function that takes
250 any action required right before expand_function_end is called. */
251 void (*lang_expand_function_end) PARAMS ((void));
253 /* Nonzero means the expression being parsed will never be evaluated.
254 This is a count, since unevaluated expressions can nest. */
255 int skip_evaluation;
257 /* Information about how a function name is generated. */
258 struct fname_var_t
260 tree *const decl; /* pointer to the VAR_DECL. */
261 const unsigned rid; /* RID number for the identifier. */
262 const int pretty; /* How pretty is it? */
265 /* The three ways of getting then name of the current function. */
267 const struct fname_var_t fname_vars[] =
269 /* C99 compliant __func__, must be first. */
270 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
271 /* GCC __FUNCTION__ compliant. */
272 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
273 /* GCC __PRETTY_FUNCTION__ compliant. */
274 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
275 {NULL, 0, 0},
278 static int constant_fits_type_p PARAMS ((tree, tree));
280 /* Keep a stack of if statements. We record the number of compound
281 statements seen up to the if keyword, as well as the line number
282 and file of the if. If a potentially ambiguous else is seen, that
283 fact is recorded; the warning is issued when we can be sure that
284 the enclosing if statement does not have an else branch. */
285 typedef struct
287 int compstmt_count;
288 int line;
289 const char *file;
290 int needs_warning;
291 tree if_stmt;
292 } if_elt;
294 static if_elt *if_stack;
296 /* Amount of space in the if statement stack. */
297 static int if_stack_space = 0;
299 /* Stack pointer. */
300 static int if_stack_pointer = 0;
302 static void cb_register_builtins PARAMS ((cpp_reader *));
304 static tree handle_packed_attribute PARAMS ((tree *, tree, tree, int,
305 bool *));
306 static tree handle_nocommon_attribute PARAMS ((tree *, tree, tree, int,
307 bool *));
308 static tree handle_common_attribute PARAMS ((tree *, tree, tree, int,
309 bool *));
310 static tree handle_noreturn_attribute PARAMS ((tree *, tree, tree, int,
311 bool *));
312 static tree handle_noinline_attribute PARAMS ((tree *, tree, tree, int,
313 bool *));
314 static tree handle_always_inline_attribute PARAMS ((tree *, tree, tree, int,
315 bool *));
316 static tree handle_used_attribute PARAMS ((tree *, tree, tree, int,
317 bool *));
318 static tree handle_unused_attribute PARAMS ((tree *, tree, tree, int,
319 bool *));
320 static tree handle_const_attribute PARAMS ((tree *, tree, tree, int,
321 bool *));
322 static tree handle_transparent_union_attribute PARAMS ((tree *, tree, tree,
323 int, bool *));
324 static tree handle_constructor_attribute PARAMS ((tree *, tree, tree, int,
325 bool *));
326 static tree handle_destructor_attribute PARAMS ((tree *, tree, tree, int,
327 bool *));
328 static tree handle_mode_attribute PARAMS ((tree *, tree, tree, int,
329 bool *));
330 static tree handle_section_attribute PARAMS ((tree *, tree, tree, int,
331 bool *));
332 static tree handle_aligned_attribute PARAMS ((tree *, tree, tree, int,
333 bool *));
334 static tree handle_weak_attribute PARAMS ((tree *, tree, tree, int,
335 bool *));
336 static tree handle_alias_attribute PARAMS ((tree *, tree, tree, int,
337 bool *));
338 static tree handle_visibility_attribute PARAMS ((tree *, tree, tree, int,
339 bool *));
340 static tree handle_no_instrument_function_attribute PARAMS ((tree *, tree,
341 tree, int,
342 bool *));
343 static tree handle_malloc_attribute PARAMS ((tree *, tree, tree, int,
344 bool *));
345 static tree handle_no_limit_stack_attribute PARAMS ((tree *, tree, tree, int,
346 bool *));
347 static tree handle_pure_attribute PARAMS ((tree *, tree, tree, int,
348 bool *));
349 static tree handle_deprecated_attribute PARAMS ((tree *, tree, tree, int,
350 bool *));
351 static tree handle_vector_size_attribute PARAMS ((tree *, tree, tree, int,
352 bool *));
353 static tree handle_nonnull_attribute PARAMS ((tree *, tree, tree, int,
354 bool *));
355 static tree handle_nothrow_attribute PARAMS ((tree *, tree, tree, int,
356 bool *));
357 static tree vector_size_helper PARAMS ((tree, tree));
359 static void check_function_nonnull PARAMS ((tree, tree));
360 static void check_nonnull_arg PARAMS ((void *, tree,
361 unsigned HOST_WIDE_INT));
362 static bool nonnull_check_p PARAMS ((tree, unsigned HOST_WIDE_INT));
363 static bool get_nonnull_operand PARAMS ((tree,
364 unsigned HOST_WIDE_INT *));
365 void builtin_define_std PARAMS ((const char *));
366 static void builtin_define_with_value PARAMS ((const char *, const char *,
367 int));
369 /* Table of machine-independent attributes common to all C-like languages. */
370 const struct attribute_spec c_common_attribute_table[] =
372 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
373 { "packed", 0, 0, false, false, false,
374 handle_packed_attribute },
375 { "nocommon", 0, 0, true, false, false,
376 handle_nocommon_attribute },
377 { "common", 0, 0, true, false, false,
378 handle_common_attribute },
379 /* FIXME: logically, noreturn attributes should be listed as
380 "false, true, true" and apply to function types. But implementing this
381 would require all the places in the compiler that use TREE_THIS_VOLATILE
382 on a decl to identify non-returning functions to be located and fixed
383 to check the function type instead. */
384 { "noreturn", 0, 0, true, false, false,
385 handle_noreturn_attribute },
386 { "volatile", 0, 0, true, false, false,
387 handle_noreturn_attribute },
388 { "noinline", 0, 0, true, false, false,
389 handle_noinline_attribute },
390 { "always_inline", 0, 0, true, false, false,
391 handle_always_inline_attribute },
392 { "used", 0, 0, true, false, false,
393 handle_used_attribute },
394 { "unused", 0, 0, false, false, false,
395 handle_unused_attribute },
396 /* The same comments as for noreturn attributes apply to const ones. */
397 { "const", 0, 0, true, false, false,
398 handle_const_attribute },
399 { "transparent_union", 0, 0, false, false, false,
400 handle_transparent_union_attribute },
401 { "constructor", 0, 0, true, false, false,
402 handle_constructor_attribute },
403 { "destructor", 0, 0, true, false, false,
404 handle_destructor_attribute },
405 { "mode", 1, 1, false, true, false,
406 handle_mode_attribute },
407 { "section", 1, 1, true, false, false,
408 handle_section_attribute },
409 { "aligned", 0, 1, false, false, false,
410 handle_aligned_attribute },
411 { "weak", 0, 0, true, false, false,
412 handle_weak_attribute },
413 { "alias", 1, 1, true, false, false,
414 handle_alias_attribute },
415 { "no_instrument_function", 0, 0, true, false, false,
416 handle_no_instrument_function_attribute },
417 { "malloc", 0, 0, true, false, false,
418 handle_malloc_attribute },
419 { "no_stack_limit", 0, 0, true, false, false,
420 handle_no_limit_stack_attribute },
421 { "pure", 0, 0, true, false, false,
422 handle_pure_attribute },
423 { "deprecated", 0, 0, false, false, false,
424 handle_deprecated_attribute },
425 { "vector_size", 1, 1, false, true, false,
426 handle_vector_size_attribute },
427 { "visibility", 1, 1, true, false, false,
428 handle_visibility_attribute },
429 { "nonnull", 0, -1, false, true, true,
430 handle_nonnull_attribute },
431 { "nothrow", 0, 0, true, false, false,
432 handle_nothrow_attribute },
433 { "may_alias", 0, 0, false, true, false, NULL },
434 { NULL, 0, 0, false, false, false, NULL }
437 /* Give the specifications for the format attributes, used by C and all
438 descendents. */
440 const struct attribute_spec c_common_format_attribute_table[] =
442 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
443 { "format", 3, 3, false, true, true,
444 handle_format_attribute },
445 { "format_arg", 1, 1, false, true, true,
446 handle_format_arg_attribute },
447 { NULL, 0, 0, false, false, false, NULL }
450 /* Record the start of an if-then, and record the start of it
451 for ambiguous else detection.
453 COND is the condition for the if-then statement.
455 IF_STMT is the statement node that has already been created for
456 this if-then statement. It is created before parsing the
457 condition to keep line number information accurate. */
459 void
460 c_expand_start_cond (cond, compstmt_count, if_stmt)
461 tree cond;
462 int compstmt_count;
463 tree if_stmt;
465 /* Make sure there is enough space on the stack. */
466 if (if_stack_space == 0)
468 if_stack_space = 10;
469 if_stack = (if_elt *) xmalloc (10 * sizeof (if_elt));
471 else if (if_stack_space == if_stack_pointer)
473 if_stack_space += 10;
474 if_stack = (if_elt *) xrealloc (if_stack, if_stack_space * sizeof (if_elt));
477 IF_COND (if_stmt) = cond;
478 add_stmt (if_stmt);
480 /* Record this if statement. */
481 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
482 if_stack[if_stack_pointer].file = input_filename;
483 if_stack[if_stack_pointer].line = lineno;
484 if_stack[if_stack_pointer].needs_warning = 0;
485 if_stack[if_stack_pointer].if_stmt = if_stmt;
486 if_stack_pointer++;
489 /* Called after the then-clause for an if-statement is processed. */
491 void
492 c_finish_then ()
494 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
495 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
498 /* Record the end of an if-then. Optionally warn if a nested
499 if statement had an ambiguous else clause. */
501 void
502 c_expand_end_cond ()
504 if_stack_pointer--;
505 if (if_stack[if_stack_pointer].needs_warning)
506 warning_with_file_and_line (if_stack[if_stack_pointer].file,
507 if_stack[if_stack_pointer].line,
508 "suggest explicit braces to avoid ambiguous `else'");
509 last_expr_type = NULL_TREE;
512 /* Called between the then-clause and the else-clause
513 of an if-then-else. */
515 void
516 c_expand_start_else ()
518 /* An ambiguous else warning must be generated for the enclosing if
519 statement, unless we see an else branch for that one, too. */
520 if (warn_parentheses
521 && if_stack_pointer > 1
522 && (if_stack[if_stack_pointer - 1].compstmt_count
523 == if_stack[if_stack_pointer - 2].compstmt_count))
524 if_stack[if_stack_pointer - 2].needs_warning = 1;
526 /* Even if a nested if statement had an else branch, it can't be
527 ambiguous if this one also has an else. So don't warn in that
528 case. Also don't warn for any if statements nested in this else. */
529 if_stack[if_stack_pointer - 1].needs_warning = 0;
530 if_stack[if_stack_pointer - 1].compstmt_count--;
533 /* Called after the else-clause for an if-statement is processed. */
535 void
536 c_finish_else ()
538 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
539 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
542 /* Begin an if-statement. Returns a newly created IF_STMT if
543 appropriate.
545 Unlike the C++ front-end, we do not call add_stmt here; it is
546 probably safe to do so, but I am not very familiar with this
547 code so I am being extra careful not to change its behavior
548 beyond what is strictly necessary for correctness. */
550 tree
551 c_begin_if_stmt ()
553 tree r;
554 r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
555 return r;
558 /* Begin a while statement. Returns a newly created WHILE_STMT if
559 appropriate.
561 Unlike the C++ front-end, we do not call add_stmt here; it is
562 probably safe to do so, but I am not very familiar with this
563 code so I am being extra careful not to change its behavior
564 beyond what is strictly necessary for correctness. */
566 tree
567 c_begin_while_stmt ()
569 tree r;
570 r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
571 return r;
574 void
575 c_finish_while_stmt_cond (cond, while_stmt)
576 tree while_stmt;
577 tree cond;
579 WHILE_COND (while_stmt) = cond;
582 /* Push current bindings for the function name VAR_DECLS. */
584 void
585 start_fname_decls ()
587 unsigned ix;
588 tree saved = NULL_TREE;
590 for (ix = 0; fname_vars[ix].decl; ix++)
592 tree decl = *fname_vars[ix].decl;
594 if (decl)
596 saved = tree_cons (decl, build_int_2 (ix, 0), saved);
597 *fname_vars[ix].decl = NULL_TREE;
600 if (saved || saved_function_name_decls)
601 /* Normally they'll have been NULL, so only push if we've got a
602 stack, or they are non-NULL. */
603 saved_function_name_decls = tree_cons (saved, NULL_TREE,
604 saved_function_name_decls);
607 /* Finish up the current bindings, adding them into the
608 current function's statement tree. This is done by wrapping the
609 function's body in a COMPOUND_STMT containing these decls too. This
610 must be done _before_ finish_stmt_tree is called. If there is no
611 current function, we must be at file scope and no statements are
612 involved. Pop the previous bindings. */
614 void
615 finish_fname_decls ()
617 unsigned ix;
618 tree body = NULL_TREE;
619 tree stack = saved_function_name_decls;
621 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
622 body = chainon (TREE_VALUE (stack), body);
624 if (body)
626 /* They were called into existence, so add to statement tree. */
627 body = chainon (body,
628 TREE_CHAIN (DECL_SAVED_TREE (current_function_decl)));
629 body = build_stmt (COMPOUND_STMT, body);
631 COMPOUND_STMT_NO_SCOPE (body) = 1;
632 TREE_CHAIN (DECL_SAVED_TREE (current_function_decl)) = body;
635 for (ix = 0; fname_vars[ix].decl; ix++)
636 *fname_vars[ix].decl = NULL_TREE;
638 if (stack)
640 /* We had saved values, restore them. */
641 tree saved;
643 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
645 tree decl = TREE_PURPOSE (saved);
646 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
648 *fname_vars[ix].decl = decl;
650 stack = TREE_CHAIN (stack);
652 saved_function_name_decls = stack;
655 /* Return the text name of the current function, suitable prettified
656 by PRETTY_P. */
658 const char *
659 fname_as_string (pretty_p)
660 int pretty_p;
662 const char *name = NULL;
664 if (pretty_p)
665 name = (current_function_decl
666 ? (*lang_hooks.decl_printable_name) (current_function_decl, 2)
667 : "top level");
668 else if (current_function_decl && DECL_NAME (current_function_decl))
669 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
670 else
671 name = "";
672 return name;
675 /* Return the text name of the current function, formatted as
676 required by the supplied RID value. */
678 const char *
679 fname_string (rid)
680 unsigned rid;
682 unsigned ix;
684 for (ix = 0; fname_vars[ix].decl; ix++)
685 if (fname_vars[ix].rid == rid)
686 break;
687 return fname_as_string (fname_vars[ix].pretty);
690 /* Return the VAR_DECL for a const char array naming the current
691 function. If the VAR_DECL has not yet been created, create it
692 now. RID indicates how it should be formatted and IDENTIFIER_NODE
693 ID is its name (unfortunately C and C++ hold the RID values of
694 keywords in different places, so we can't derive RID from ID in
695 this language independent code. */
697 tree
698 fname_decl (rid, id)
699 unsigned rid;
700 tree id;
702 unsigned ix;
703 tree decl = NULL_TREE;
705 for (ix = 0; fname_vars[ix].decl; ix++)
706 if (fname_vars[ix].rid == rid)
707 break;
709 decl = *fname_vars[ix].decl;
710 if (!decl)
712 tree saved_last_tree = last_tree;
714 decl = (*make_fname_decl) (id, fname_vars[ix].pretty);
715 if (last_tree != saved_last_tree)
717 /* We created some statement tree for the decl. This belongs
718 at the start of the function, so remove it now and reinsert
719 it after the function is complete. */
720 tree stmts = TREE_CHAIN (saved_last_tree);
722 TREE_CHAIN (saved_last_tree) = NULL_TREE;
723 last_tree = saved_last_tree;
724 saved_function_name_decls = tree_cons (decl, stmts,
725 saved_function_name_decls);
727 *fname_vars[ix].decl = decl;
729 if (!ix && !current_function_decl)
730 pedwarn_with_decl (decl, "`%s' is not defined outside of function scope");
732 return decl;
735 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
737 tree
738 fix_string_type (value)
739 tree value;
741 const int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
742 const int wide_flag = TREE_TYPE (value) == wchar_array_type_node;
743 const int nchars_max = flag_isoc99 ? 4095 : 509;
744 int length = TREE_STRING_LENGTH (value);
745 int nchars;
747 /* Compute the number of elements, for the array type. */
748 nchars = wide_flag ? length / wchar_bytes : length;
750 if (pedantic && nchars - 1 > nchars_max && c_language == clk_c)
751 pedwarn ("string length `%d' is greater than the length `%d' ISO C%d compilers are required to support",
752 nchars - 1, nchars_max, flag_isoc99 ? 99 : 89);
754 /* Create the array type for the string constant.
755 -Wwrite-strings says make the string constant an array of const char
756 so that copying it to a non-const pointer will get a warning.
757 For C++, this is the standard behavior. */
758 if (flag_const_strings && ! flag_writable_strings)
760 tree elements
761 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
762 1, 0);
763 TREE_TYPE (value)
764 = build_array_type (elements,
765 build_index_type (build_int_2 (nchars - 1, 0)));
767 else
768 TREE_TYPE (value)
769 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
770 build_index_type (build_int_2 (nchars - 1, 0)));
772 TREE_CONSTANT (value) = 1;
773 TREE_READONLY (value) = ! flag_writable_strings;
774 TREE_STATIC (value) = 1;
775 return value;
778 /* Given a VARRAY of STRING_CST nodes, concatenate them into one
779 STRING_CST. */
781 tree
782 combine_strings (strings)
783 varray_type strings;
785 const int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
786 const int nstrings = VARRAY_ACTIVE_SIZE (strings);
787 tree value, t;
788 int length = 1;
789 int wide_length = 0;
790 int wide_flag = 0;
791 int i;
792 char *p, *q;
794 /* Don't include the \0 at the end of each substring. Count wide
795 strings and ordinary strings separately. */
796 for (i = 0; i < nstrings; ++i)
798 t = VARRAY_TREE (strings, i);
800 if (TREE_TYPE (t) == wchar_array_type_node)
802 wide_length += TREE_STRING_LENGTH (t) - wchar_bytes;
803 wide_flag = 1;
805 else
807 length += (TREE_STRING_LENGTH (t) - 1);
808 if (C_ARTIFICIAL_STRING_P (t) && !in_system_header)
809 warning ("concatenation of string literals with __FUNCTION__ is deprecated");
813 /* If anything is wide, the non-wides will be converted,
814 which makes them take more space. */
815 if (wide_flag)
816 length = length * wchar_bytes + wide_length;
818 p = xmalloc (length);
820 /* Copy the individual strings into the new combined string.
821 If the combined string is wide, convert the chars to ints
822 for any individual strings that are not wide. */
824 q = p;
825 for (i = 0; i < nstrings; ++i)
827 int len, this_wide;
829 t = VARRAY_TREE (strings, i);
830 this_wide = TREE_TYPE (t) == wchar_array_type_node;
831 len = TREE_STRING_LENGTH (t) - (this_wide ? wchar_bytes : 1);
832 if (this_wide == wide_flag)
834 memcpy (q, TREE_STRING_POINTER (t), len);
835 q += len;
837 else
839 const int nzeros = (TYPE_PRECISION (wchar_type_node)
840 / BITS_PER_UNIT) - 1;
841 int j, k;
843 if (BYTES_BIG_ENDIAN)
845 for (k = 0; k < len; k++)
847 for (j = 0; j < nzeros; j++)
848 *q++ = 0;
849 *q++ = TREE_STRING_POINTER (t)[k];
852 else
854 for (k = 0; k < len; k++)
856 *q++ = TREE_STRING_POINTER (t)[k];
857 for (j = 0; j < nzeros; j++)
858 *q++ = 0;
864 /* Nul terminate the string. */
865 if (wide_flag)
867 for (i = 0; i < wchar_bytes; i++)
868 *q++ = 0;
870 else
871 *q = 0;
873 value = build_string (length, p);
874 free (p);
876 if (wide_flag)
877 TREE_TYPE (value) = wchar_array_type_node;
878 else
879 TREE_TYPE (value) = char_array_type_node;
881 return value;
884 static int is_valid_printf_arglist PARAMS ((tree));
885 static rtx c_expand_builtin PARAMS ((tree, rtx, enum machine_mode, enum expand_modifier));
886 static rtx c_expand_builtin_printf PARAMS ((tree, rtx, enum machine_mode,
887 enum expand_modifier, int, int));
888 static rtx c_expand_builtin_fprintf PARAMS ((tree, rtx, enum machine_mode,
889 enum expand_modifier, int, int));
891 /* Print a warning if a constant expression had overflow in folding.
892 Invoke this function on every expression that the language
893 requires to be a constant expression.
894 Note the ANSI C standard says it is erroneous for a
895 constant expression to overflow. */
897 void
898 constant_expression_warning (value)
899 tree value;
901 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
902 || TREE_CODE (value) == VECTOR_CST
903 || TREE_CODE (value) == COMPLEX_CST)
904 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
905 pedwarn ("overflow in constant expression");
908 /* Print a warning if an expression had overflow in folding.
909 Invoke this function on every expression that
910 (1) appears in the source code, and
911 (2) might be a constant expression that overflowed, and
912 (3) is not already checked by convert_and_check;
913 however, do not invoke this function on operands of explicit casts. */
915 void
916 overflow_warning (value)
917 tree value;
919 if ((TREE_CODE (value) == INTEGER_CST
920 || (TREE_CODE (value) == COMPLEX_CST
921 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
922 && TREE_OVERFLOW (value))
924 TREE_OVERFLOW (value) = 0;
925 if (skip_evaluation == 0)
926 warning ("integer overflow in expression");
928 else if ((TREE_CODE (value) == REAL_CST
929 || (TREE_CODE (value) == COMPLEX_CST
930 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
931 && TREE_OVERFLOW (value))
933 TREE_OVERFLOW (value) = 0;
934 if (skip_evaluation == 0)
935 warning ("floating point overflow in expression");
937 else if (TREE_CODE (value) == VECTOR_CST && TREE_OVERFLOW (value))
939 TREE_OVERFLOW (value) = 0;
940 if (skip_evaluation == 0)
941 warning ("vector overflow in expression");
945 /* Print a warning if a large constant is truncated to unsigned,
946 or if -Wconversion is used and a constant < 0 is converted to unsigned.
947 Invoke this function on every expression that might be implicitly
948 converted to an unsigned type. */
950 void
951 unsigned_conversion_warning (result, operand)
952 tree result, operand;
954 tree type = TREE_TYPE (result);
956 if (TREE_CODE (operand) == INTEGER_CST
957 && TREE_CODE (type) == INTEGER_TYPE
958 && TREE_UNSIGNED (type)
959 && skip_evaluation == 0
960 && !int_fits_type_p (operand, type))
962 if (!int_fits_type_p (operand, c_common_signed_type (type)))
963 /* This detects cases like converting -129 or 256 to unsigned char. */
964 warning ("large integer implicitly truncated to unsigned type");
965 else if (warn_conversion)
966 warning ("negative integer implicitly converted to unsigned type");
970 /* Nonzero if constant C has a value that is permissible
971 for type TYPE (an INTEGER_TYPE). */
973 static int
974 constant_fits_type_p (c, type)
975 tree c, type;
977 if (TREE_CODE (c) == INTEGER_CST)
978 return int_fits_type_p (c, type);
980 c = convert (type, c);
981 return !TREE_OVERFLOW (c);
984 /* Convert EXPR to TYPE, warning about conversion problems with constants.
985 Invoke this function on every expression that is converted implicitly,
986 i.e. because of language rules and not because of an explicit cast. */
988 tree
989 convert_and_check (type, expr)
990 tree type, expr;
992 tree t = convert (type, expr);
993 if (TREE_CODE (t) == INTEGER_CST)
995 if (TREE_OVERFLOW (t))
997 TREE_OVERFLOW (t) = 0;
999 /* Do not diagnose overflow in a constant expression merely
1000 because a conversion overflowed. */
1001 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
1003 /* No warning for converting 0x80000000 to int. */
1004 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
1005 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1006 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
1007 /* If EXPR fits in the unsigned version of TYPE,
1008 don't warn unless pedantic. */
1009 if ((pedantic
1010 || TREE_UNSIGNED (type)
1011 || ! constant_fits_type_p (expr,
1012 c_common_unsigned_type (type)))
1013 && skip_evaluation == 0)
1014 warning ("overflow in implicit constant conversion");
1016 else
1017 unsigned_conversion_warning (t, expr);
1019 return t;
1022 /* A node in a list that describes references to variables (EXPR), which are
1023 either read accesses if WRITER is zero, or write accesses, in which case
1024 WRITER is the parent of EXPR. */
1025 struct tlist
1027 struct tlist *next;
1028 tree expr, writer;
1031 /* Used to implement a cache the results of a call to verify_tree. We only
1032 use this for SAVE_EXPRs. */
1033 struct tlist_cache
1035 struct tlist_cache *next;
1036 struct tlist *cache_before_sp;
1037 struct tlist *cache_after_sp;
1038 tree expr;
1041 /* Obstack to use when allocating tlist structures, and corresponding
1042 firstobj. */
1043 static struct obstack tlist_obstack;
1044 static char *tlist_firstobj = 0;
1046 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
1047 warnings. */
1048 static struct tlist *warned_ids;
1049 /* SAVE_EXPRs need special treatment. We process them only once and then
1050 cache the results. */
1051 static struct tlist_cache *save_expr_cache;
1053 static void add_tlist PARAMS ((struct tlist **, struct tlist *, tree, int));
1054 static void merge_tlist PARAMS ((struct tlist **, struct tlist *, int));
1055 static void verify_tree PARAMS ((tree, struct tlist **, struct tlist **, tree));
1056 static int warning_candidate_p PARAMS ((tree));
1057 static void warn_for_collisions PARAMS ((struct tlist *));
1058 static void warn_for_collisions_1 PARAMS ((tree, tree, struct tlist *, int));
1059 static struct tlist *new_tlist PARAMS ((struct tlist *, tree, tree));
1060 static void verify_sequence_points PARAMS ((tree));
1062 /* Create a new struct tlist and fill in its fields. */
1063 static struct tlist *
1064 new_tlist (next, t, writer)
1065 struct tlist *next;
1066 tree t;
1067 tree writer;
1069 struct tlist *l;
1070 l = (struct tlist *) obstack_alloc (&tlist_obstack, sizeof *l);
1071 l->next = next;
1072 l->expr = t;
1073 l->writer = writer;
1074 return l;
1077 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1078 is nonnull, we ignore any node we find which has a writer equal to it. */
1080 static void
1081 add_tlist (to, add, exclude_writer, copy)
1082 struct tlist **to;
1083 struct tlist *add;
1084 tree exclude_writer;
1085 int copy;
1087 while (add)
1089 struct tlist *next = add->next;
1090 if (! copy)
1091 add->next = *to;
1092 if (! exclude_writer || add->writer != exclude_writer)
1093 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1094 add = next;
1098 /* Merge the nodes of ADD into TO. This merging process is done so that for
1099 each variable that already exists in TO, no new node is added; however if
1100 there is a write access recorded in ADD, and an occurrence on TO is only
1101 a read access, then the occurrence in TO will be modified to record the
1102 write. */
1104 static void
1105 merge_tlist (to, add, copy)
1106 struct tlist **to;
1107 struct tlist *add;
1108 int copy;
1110 struct tlist **end = to;
1112 while (*end)
1113 end = &(*end)->next;
1115 while (add)
1117 int found = 0;
1118 struct tlist *tmp2;
1119 struct tlist *next = add->next;
1121 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1122 if (tmp2->expr == add->expr)
1124 found = 1;
1125 if (! tmp2->writer)
1126 tmp2->writer = add->writer;
1128 if (! found)
1130 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
1131 end = &(*end)->next;
1132 *end = 0;
1134 add = next;
1138 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1139 references in list LIST conflict with it, excluding reads if ONLY writers
1140 is nonzero. */
1142 static void
1143 warn_for_collisions_1 (written, writer, list, only_writes)
1144 tree written, writer;
1145 struct tlist *list;
1146 int only_writes;
1148 struct tlist *tmp;
1150 /* Avoid duplicate warnings. */
1151 for (tmp = warned_ids; tmp; tmp = tmp->next)
1152 if (tmp->expr == written)
1153 return;
1155 while (list)
1157 if (list->expr == written
1158 && list->writer != writer
1159 && (! only_writes || list->writer))
1161 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
1162 warning ("operation on `%s' may be undefined",
1163 IDENTIFIER_POINTER (DECL_NAME (list->expr)));
1165 list = list->next;
1169 /* Given a list LIST of references to variables, find whether any of these
1170 can cause conflicts due to missing sequence points. */
1172 static void
1173 warn_for_collisions (list)
1174 struct tlist *list;
1176 struct tlist *tmp;
1178 for (tmp = list; tmp; tmp = tmp->next)
1180 if (tmp->writer)
1181 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1185 /* Return nonzero if X is a tree that can be verified by the sequence point
1186 warnings. */
1187 static int
1188 warning_candidate_p (x)
1189 tree x;
1191 return TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == PARM_DECL;
1194 /* Walk the tree X, and record accesses to variables. If X is written by the
1195 parent tree, WRITER is the parent.
1196 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
1197 expression or its only operand forces a sequence point, then everything up
1198 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
1199 in PNO_SP.
1200 Once we return, we will have emitted warnings if any subexpression before
1201 such a sequence point could be undefined. On a higher level, however, the
1202 sequence point may not be relevant, and we'll merge the two lists.
1204 Example: (b++, a) + b;
1205 The call that processes the COMPOUND_EXPR will store the increment of B
1206 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
1207 processes the PLUS_EXPR will need to merge the two lists so that
1208 eventually, all accesses end up on the same list (and we'll warn about the
1209 unordered subexpressions b++ and b.
1211 A note on merging. If we modify the former example so that our expression
1212 becomes
1213 (b++, b) + a
1214 care must be taken not simply to add all three expressions into the final
1215 PNO_SP list. The function merge_tlist takes care of that by merging the
1216 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
1217 way, so that no more than one access to B is recorded. */
1219 static void
1220 verify_tree (x, pbefore_sp, pno_sp, writer)
1221 tree x;
1222 struct tlist **pbefore_sp, **pno_sp;
1223 tree writer;
1225 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
1226 enum tree_code code;
1227 char class;
1229 /* X may be NULL if it is the operand of an empty statement expression
1230 ({ }). */
1231 if (x == NULL)
1232 return;
1234 restart:
1235 code = TREE_CODE (x);
1236 class = TREE_CODE_CLASS (code);
1238 if (warning_candidate_p (x))
1240 *pno_sp = new_tlist (*pno_sp, x, writer);
1241 return;
1244 switch (code)
1246 case CONSTRUCTOR:
1247 return;
1249 case COMPOUND_EXPR:
1250 case TRUTH_ANDIF_EXPR:
1251 case TRUTH_ORIF_EXPR:
1252 tmp_before = tmp_nosp = tmp_list3 = 0;
1253 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1254 warn_for_collisions (tmp_nosp);
1255 merge_tlist (pbefore_sp, tmp_before, 0);
1256 merge_tlist (pbefore_sp, tmp_nosp, 0);
1257 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
1258 merge_tlist (pbefore_sp, tmp_list3, 0);
1259 return;
1261 case COND_EXPR:
1262 tmp_before = tmp_list2 = 0;
1263 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
1264 warn_for_collisions (tmp_list2);
1265 merge_tlist (pbefore_sp, tmp_before, 0);
1266 merge_tlist (pbefore_sp, tmp_list2, 1);
1268 tmp_list3 = tmp_nosp = 0;
1269 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
1270 warn_for_collisions (tmp_nosp);
1271 merge_tlist (pbefore_sp, tmp_list3, 0);
1273 tmp_list3 = tmp_list2 = 0;
1274 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
1275 warn_for_collisions (tmp_list2);
1276 merge_tlist (pbefore_sp, tmp_list3, 0);
1277 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1278 two first, to avoid warning for (a ? b++ : b++). */
1279 merge_tlist (&tmp_nosp, tmp_list2, 0);
1280 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1281 return;
1283 case PREDECREMENT_EXPR:
1284 case PREINCREMENT_EXPR:
1285 case POSTDECREMENT_EXPR:
1286 case POSTINCREMENT_EXPR:
1287 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
1288 return;
1290 case MODIFY_EXPR:
1291 tmp_before = tmp_nosp = tmp_list3 = 0;
1292 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
1293 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
1294 /* Expressions inside the LHS are not ordered wrt. the sequence points
1295 in the RHS. Example:
1296 *a = (a++, 2)
1297 Despite the fact that the modification of "a" is in the before_sp
1298 list (tmp_before), it conflicts with the use of "a" in the LHS.
1299 We can handle this by adding the contents of tmp_list3
1300 to those of tmp_before, and redoing the collision warnings for that
1301 list. */
1302 add_tlist (&tmp_before, tmp_list3, x, 1);
1303 warn_for_collisions (tmp_before);
1304 /* Exclude the LHS itself here; we first have to merge it into the
1305 tmp_nosp list. This is done to avoid warning for "a = a"; if we
1306 didn't exclude the LHS, we'd get it twice, once as a read and once
1307 as a write. */
1308 add_tlist (pno_sp, tmp_list3, x, 0);
1309 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
1311 merge_tlist (pbefore_sp, tmp_before, 0);
1312 if (warning_candidate_p (TREE_OPERAND (x, 0)))
1313 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
1314 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
1315 return;
1317 case CALL_EXPR:
1318 /* We need to warn about conflicts among arguments and conflicts between
1319 args and the function address. Side effects of the function address,
1320 however, are not ordered by the sequence point of the call. */
1321 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
1322 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1323 if (TREE_OPERAND (x, 1))
1324 verify_tree (TREE_OPERAND (x, 1), &tmp_list2, &tmp_list3, NULL_TREE);
1325 merge_tlist (&tmp_list3, tmp_list2, 0);
1326 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
1327 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
1328 warn_for_collisions (tmp_before);
1329 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
1330 return;
1332 case TREE_LIST:
1333 /* Scan all the list, e.g. indices of multi dimensional array. */
1334 while (x)
1336 tmp_before = tmp_nosp = 0;
1337 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
1338 merge_tlist (&tmp_nosp, tmp_before, 0);
1339 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1340 x = TREE_CHAIN (x);
1342 return;
1344 case SAVE_EXPR:
1346 struct tlist_cache *t;
1347 for (t = save_expr_cache; t; t = t->next)
1348 if (t->expr == x)
1349 break;
1351 if (! t)
1353 t = (struct tlist_cache *) obstack_alloc (&tlist_obstack,
1354 sizeof *t);
1355 t->next = save_expr_cache;
1356 t->expr = x;
1357 save_expr_cache = t;
1359 tmp_before = tmp_nosp = 0;
1360 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1361 warn_for_collisions (tmp_nosp);
1363 tmp_list3 = 0;
1364 while (tmp_nosp)
1366 struct tlist *t = tmp_nosp;
1367 tmp_nosp = t->next;
1368 merge_tlist (&tmp_list3, t, 0);
1370 t->cache_before_sp = tmp_before;
1371 t->cache_after_sp = tmp_list3;
1373 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
1374 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
1375 return;
1377 default:
1378 break;
1381 if (class == '1')
1383 if (first_rtl_op (code) == 0)
1384 return;
1385 x = TREE_OPERAND (x, 0);
1386 writer = 0;
1387 goto restart;
1390 switch (class)
1392 case 'r':
1393 case '<':
1394 case '2':
1395 case 'b':
1396 case 'e':
1397 case 's':
1398 case 'x':
1400 int lp;
1401 int max = first_rtl_op (TREE_CODE (x));
1402 for (lp = 0; lp < max; lp++)
1404 tmp_before = tmp_nosp = 0;
1405 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, NULL_TREE);
1406 merge_tlist (&tmp_nosp, tmp_before, 0);
1407 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1409 break;
1414 /* Try to warn for undefined behaviour in EXPR due to missing sequence
1415 points. */
1417 static void
1418 verify_sequence_points (expr)
1419 tree expr;
1421 struct tlist *before_sp = 0, *after_sp = 0;
1423 warned_ids = 0;
1424 save_expr_cache = 0;
1425 if (tlist_firstobj == 0)
1427 gcc_obstack_init (&tlist_obstack);
1428 tlist_firstobj = obstack_alloc (&tlist_obstack, 0);
1431 verify_tree (expr, &before_sp, &after_sp, 0);
1432 warn_for_collisions (after_sp);
1433 obstack_free (&tlist_obstack, tlist_firstobj);
1436 tree
1437 c_expand_expr_stmt (expr)
1438 tree expr;
1440 /* Do default conversion if safe and possibly important,
1441 in case within ({...}). */
1442 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
1443 && (flag_isoc99 || lvalue_p (expr)))
1444 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1445 expr = default_conversion (expr);
1447 if (warn_sequence_point)
1448 verify_sequence_points (expr);
1450 if (TREE_TYPE (expr) != error_mark_node
1451 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
1452 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1453 error ("expression statement has incomplete type");
1455 last_expr_type = TREE_TYPE (expr);
1456 return add_stmt (build_stmt (EXPR_STMT, expr));
1459 /* Validate the expression after `case' and apply default promotions. */
1461 tree
1462 check_case_value (value)
1463 tree value;
1465 if (value == NULL_TREE)
1466 return value;
1468 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1469 STRIP_TYPE_NOPS (value);
1470 /* In C++, the following is allowed:
1472 const int i = 3;
1473 switch (...) { case i: ... }
1475 So, we try to reduce the VALUE to a constant that way. */
1476 if (c_language == clk_cplusplus)
1478 value = decl_constant_value (value);
1479 STRIP_TYPE_NOPS (value);
1480 value = fold (value);
1483 if (TREE_CODE (value) != INTEGER_CST
1484 && value != error_mark_node)
1486 error ("case label does not reduce to an integer constant");
1487 value = error_mark_node;
1489 else
1490 /* Promote char or short to int. */
1491 value = default_conversion (value);
1493 constant_expression_warning (value);
1495 return value;
1498 /* Return an integer type with BITS bits of precision,
1499 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1501 tree
1502 c_common_type_for_size (bits, unsignedp)
1503 unsigned bits;
1504 int unsignedp;
1506 if (bits == TYPE_PRECISION (integer_type_node))
1507 return unsignedp ? unsigned_type_node : integer_type_node;
1509 if (bits == TYPE_PRECISION (signed_char_type_node))
1510 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1512 if (bits == TYPE_PRECISION (short_integer_type_node))
1513 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1515 if (bits == TYPE_PRECISION (long_integer_type_node))
1516 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1518 if (bits == TYPE_PRECISION (long_long_integer_type_node))
1519 return (unsignedp ? long_long_unsigned_type_node
1520 : long_long_integer_type_node);
1522 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
1523 return (unsignedp ? widest_unsigned_literal_type_node
1524 : widest_integer_literal_type_node);
1526 if (bits <= TYPE_PRECISION (intQI_type_node))
1527 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1529 if (bits <= TYPE_PRECISION (intHI_type_node))
1530 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1532 if (bits <= TYPE_PRECISION (intSI_type_node))
1533 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1535 if (bits <= TYPE_PRECISION (intDI_type_node))
1536 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1538 return 0;
1541 /* Return a data type that has machine mode MODE.
1542 If the mode is an integer,
1543 then UNSIGNEDP selects between signed and unsigned types. */
1545 tree
1546 c_common_type_for_mode (mode, unsignedp)
1547 enum machine_mode mode;
1548 int unsignedp;
1550 if (mode == TYPE_MODE (integer_type_node))
1551 return unsignedp ? unsigned_type_node : integer_type_node;
1553 if (mode == TYPE_MODE (signed_char_type_node))
1554 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1556 if (mode == TYPE_MODE (short_integer_type_node))
1557 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1559 if (mode == TYPE_MODE (long_integer_type_node))
1560 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1562 if (mode == TYPE_MODE (long_long_integer_type_node))
1563 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
1565 if (mode == TYPE_MODE (widest_integer_literal_type_node))
1566 return unsignedp ? widest_unsigned_literal_type_node
1567 : widest_integer_literal_type_node;
1569 if (mode == QImode)
1570 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1572 if (mode == HImode)
1573 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1575 if (mode == SImode)
1576 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1578 if (mode == DImode)
1579 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1581 #if HOST_BITS_PER_WIDE_INT >= 64
1582 if (mode == TYPE_MODE (intTI_type_node))
1583 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
1584 #endif
1586 if (mode == TYPE_MODE (float_type_node))
1587 return float_type_node;
1589 if (mode == TYPE_MODE (double_type_node))
1590 return double_type_node;
1592 if (mode == TYPE_MODE (long_double_type_node))
1593 return long_double_type_node;
1595 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
1596 return build_pointer_type (char_type_node);
1598 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
1599 return build_pointer_type (integer_type_node);
1601 #ifdef VECTOR_MODE_SUPPORTED_P
1602 if (VECTOR_MODE_SUPPORTED_P (mode))
1604 switch (mode)
1606 case V16QImode:
1607 return unsignedp ? unsigned_V16QI_type_node : V16QI_type_node;
1608 case V8HImode:
1609 return unsignedp ? unsigned_V8HI_type_node : V8HI_type_node;
1610 case V4SImode:
1611 return unsignedp ? unsigned_V4SI_type_node : V4SI_type_node;
1612 case V2DImode:
1613 return unsignedp ? unsigned_V2DI_type_node : V2DI_type_node;
1614 case V2SImode:
1615 return unsignedp ? unsigned_V2SI_type_node : V2SI_type_node;
1616 case V4HImode:
1617 return unsignedp ? unsigned_V4HI_type_node : V4HI_type_node;
1618 case V8QImode:
1619 return unsignedp ? unsigned_V8QI_type_node : V8QI_type_node;
1620 case V16SFmode:
1621 return V16SF_type_node;
1622 case V4SFmode:
1623 return V4SF_type_node;
1624 case V2SFmode:
1625 return V2SF_type_node;
1626 case V2DFmode:
1627 return V2DF_type_node;
1628 default:
1629 break;
1632 #endif
1634 return 0;
1637 /* Return an unsigned type the same as TYPE in other respects. */
1638 tree
1639 c_common_unsigned_type (type)
1640 tree type;
1642 tree type1 = TYPE_MAIN_VARIANT (type);
1643 if (type1 == signed_char_type_node || type1 == char_type_node)
1644 return unsigned_char_type_node;
1645 if (type1 == integer_type_node)
1646 return unsigned_type_node;
1647 if (type1 == short_integer_type_node)
1648 return short_unsigned_type_node;
1649 if (type1 == long_integer_type_node)
1650 return long_unsigned_type_node;
1651 if (type1 == long_long_integer_type_node)
1652 return long_long_unsigned_type_node;
1653 if (type1 == widest_integer_literal_type_node)
1654 return widest_unsigned_literal_type_node;
1655 #if HOST_BITS_PER_WIDE_INT >= 64
1656 if (type1 == intTI_type_node)
1657 return unsigned_intTI_type_node;
1658 #endif
1659 if (type1 == intDI_type_node)
1660 return unsigned_intDI_type_node;
1661 if (type1 == intSI_type_node)
1662 return unsigned_intSI_type_node;
1663 if (type1 == intHI_type_node)
1664 return unsigned_intHI_type_node;
1665 if (type1 == intQI_type_node)
1666 return unsigned_intQI_type_node;
1668 return c_common_signed_or_unsigned_type (1, type);
1671 /* Return a signed type the same as TYPE in other respects. */
1673 tree
1674 c_common_signed_type (type)
1675 tree type;
1677 tree type1 = TYPE_MAIN_VARIANT (type);
1678 if (type1 == unsigned_char_type_node || type1 == char_type_node)
1679 return signed_char_type_node;
1680 if (type1 == unsigned_type_node)
1681 return integer_type_node;
1682 if (type1 == short_unsigned_type_node)
1683 return short_integer_type_node;
1684 if (type1 == long_unsigned_type_node)
1685 return long_integer_type_node;
1686 if (type1 == long_long_unsigned_type_node)
1687 return long_long_integer_type_node;
1688 if (type1 == widest_unsigned_literal_type_node)
1689 return widest_integer_literal_type_node;
1690 #if HOST_BITS_PER_WIDE_INT >= 64
1691 if (type1 == unsigned_intTI_type_node)
1692 return intTI_type_node;
1693 #endif
1694 if (type1 == unsigned_intDI_type_node)
1695 return intDI_type_node;
1696 if (type1 == unsigned_intSI_type_node)
1697 return intSI_type_node;
1698 if (type1 == unsigned_intHI_type_node)
1699 return intHI_type_node;
1700 if (type1 == unsigned_intQI_type_node)
1701 return intQI_type_node;
1703 return c_common_signed_or_unsigned_type (0, type);
1706 /* Return a type the same as TYPE except unsigned or
1707 signed according to UNSIGNEDP. */
1709 tree
1710 c_common_signed_or_unsigned_type (unsignedp, type)
1711 int unsignedp;
1712 tree type;
1714 if (! INTEGRAL_TYPE_P (type)
1715 || TREE_UNSIGNED (type) == unsignedp)
1716 return type;
1718 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
1719 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1720 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
1721 return unsignedp ? unsigned_type_node : integer_type_node;
1722 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
1723 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1724 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
1725 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1726 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
1727 return (unsignedp ? long_long_unsigned_type_node
1728 : long_long_integer_type_node);
1729 if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
1730 return (unsignedp ? widest_unsigned_literal_type_node
1731 : widest_integer_literal_type_node);
1733 #if HOST_BITS_PER_WIDE_INT >= 64
1734 if (TYPE_PRECISION (type) == TYPE_PRECISION (intTI_type_node))
1735 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
1736 #endif
1737 if (TYPE_PRECISION (type) == TYPE_PRECISION (intDI_type_node))
1738 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1739 if (TYPE_PRECISION (type) == TYPE_PRECISION (intSI_type_node))
1740 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1741 if (TYPE_PRECISION (type) == TYPE_PRECISION (intHI_type_node))
1742 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1743 if (TYPE_PRECISION (type) == TYPE_PRECISION (intQI_type_node))
1744 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1746 return type;
1749 /* Return the minimum number of bits needed to represent VALUE in a
1750 signed or unsigned type, UNSIGNEDP says which. */
1752 unsigned int
1753 min_precision (value, unsignedp)
1754 tree value;
1755 int unsignedp;
1757 int log;
1759 /* If the value is negative, compute its negative minus 1. The latter
1760 adjustment is because the absolute value of the largest negative value
1761 is one larger than the largest positive value. This is equivalent to
1762 a bit-wise negation, so use that operation instead. */
1764 if (tree_int_cst_sgn (value) < 0)
1765 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
1767 /* Return the number of bits needed, taking into account the fact
1768 that we need one more bit for a signed than unsigned type. */
1770 if (integer_zerop (value))
1771 log = 0;
1772 else
1773 log = tree_floor_log2 (value);
1775 return log + 1 + ! unsignedp;
1778 /* Print an error message for invalid operands to arith operation
1779 CODE. NOP_EXPR is used as a special case (see
1780 c_common_truthvalue_conversion). */
1782 void
1783 binary_op_error (code)
1784 enum tree_code code;
1786 const char *opname;
1788 switch (code)
1790 case NOP_EXPR:
1791 error ("invalid truth-value expression");
1792 return;
1794 case PLUS_EXPR:
1795 opname = "+"; break;
1796 case MINUS_EXPR:
1797 opname = "-"; break;
1798 case MULT_EXPR:
1799 opname = "*"; break;
1800 case MAX_EXPR:
1801 opname = "max"; break;
1802 case MIN_EXPR:
1803 opname = "min"; break;
1804 case EQ_EXPR:
1805 opname = "=="; break;
1806 case NE_EXPR:
1807 opname = "!="; break;
1808 case LE_EXPR:
1809 opname = "<="; break;
1810 case GE_EXPR:
1811 opname = ">="; break;
1812 case LT_EXPR:
1813 opname = "<"; break;
1814 case GT_EXPR:
1815 opname = ">"; break;
1816 case LSHIFT_EXPR:
1817 opname = "<<"; break;
1818 case RSHIFT_EXPR:
1819 opname = ">>"; break;
1820 case TRUNC_MOD_EXPR:
1821 case FLOOR_MOD_EXPR:
1822 opname = "%"; break;
1823 case TRUNC_DIV_EXPR:
1824 case FLOOR_DIV_EXPR:
1825 opname = "/"; break;
1826 case BIT_AND_EXPR:
1827 opname = "&"; break;
1828 case BIT_IOR_EXPR:
1829 opname = "|"; break;
1830 case TRUTH_ANDIF_EXPR:
1831 opname = "&&"; break;
1832 case TRUTH_ORIF_EXPR:
1833 opname = "||"; break;
1834 case BIT_XOR_EXPR:
1835 opname = "^"; break;
1836 case LROTATE_EXPR:
1837 case RROTATE_EXPR:
1838 opname = "rotate"; break;
1839 default:
1840 opname = "unknown"; break;
1842 error ("invalid operands to binary %s", opname);
1845 /* Subroutine of build_binary_op, used for comparison operations.
1846 See if the operands have both been converted from subword integer types
1847 and, if so, perhaps change them both back to their original type.
1848 This function is also responsible for converting the two operands
1849 to the proper common type for comparison.
1851 The arguments of this function are all pointers to local variables
1852 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
1853 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
1855 If this function returns nonzero, it means that the comparison has
1856 a constant value. What this function returns is an expression for
1857 that value. */
1859 tree
1860 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
1861 tree *op0_ptr, *op1_ptr;
1862 tree *restype_ptr;
1863 enum tree_code *rescode_ptr;
1865 tree type;
1866 tree op0 = *op0_ptr;
1867 tree op1 = *op1_ptr;
1868 int unsignedp0, unsignedp1;
1869 int real1, real2;
1870 tree primop0, primop1;
1871 enum tree_code code = *rescode_ptr;
1873 /* Throw away any conversions to wider types
1874 already present in the operands. */
1876 primop0 = get_narrower (op0, &unsignedp0);
1877 primop1 = get_narrower (op1, &unsignedp1);
1879 /* Handle the case that OP0 does not *contain* a conversion
1880 but it *requires* conversion to FINAL_TYPE. */
1882 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
1883 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
1884 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
1885 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
1887 /* If one of the operands must be floated, we cannot optimize. */
1888 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
1889 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
1891 /* If first arg is constant, swap the args (changing operation
1892 so value is preserved), for canonicalization. Don't do this if
1893 the second arg is 0. */
1895 if (TREE_CONSTANT (primop0)
1896 && ! integer_zerop (primop1) && ! real_zerop (primop1))
1898 tree tem = primop0;
1899 int temi = unsignedp0;
1900 primop0 = primop1;
1901 primop1 = tem;
1902 tem = op0;
1903 op0 = op1;
1904 op1 = tem;
1905 *op0_ptr = op0;
1906 *op1_ptr = op1;
1907 unsignedp0 = unsignedp1;
1908 unsignedp1 = temi;
1909 temi = real1;
1910 real1 = real2;
1911 real2 = temi;
1913 switch (code)
1915 case LT_EXPR:
1916 code = GT_EXPR;
1917 break;
1918 case GT_EXPR:
1919 code = LT_EXPR;
1920 break;
1921 case LE_EXPR:
1922 code = GE_EXPR;
1923 break;
1924 case GE_EXPR:
1925 code = LE_EXPR;
1926 break;
1927 default:
1928 break;
1930 *rescode_ptr = code;
1933 /* If comparing an integer against a constant more bits wide,
1934 maybe we can deduce a value of 1 or 0 independent of the data.
1935 Or else truncate the constant now
1936 rather than extend the variable at run time.
1938 This is only interesting if the constant is the wider arg.
1939 Also, it is not safe if the constant is unsigned and the
1940 variable arg is signed, since in this case the variable
1941 would be sign-extended and then regarded as unsigned.
1942 Our technique fails in this case because the lowest/highest
1943 possible unsigned results don't follow naturally from the
1944 lowest/highest possible values of the variable operand.
1945 For just EQ_EXPR and NE_EXPR there is another technique that
1946 could be used: see if the constant can be faithfully represented
1947 in the other operand's type, by truncating it and reextending it
1948 and see if that preserves the constant's value. */
1950 if (!real1 && !real2
1951 && TREE_CODE (primop1) == INTEGER_CST
1952 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
1954 int min_gt, max_gt, min_lt, max_lt;
1955 tree maxval, minval;
1956 /* 1 if comparison is nominally unsigned. */
1957 int unsignedp = TREE_UNSIGNED (*restype_ptr);
1958 tree val;
1960 type = c_common_signed_or_unsigned_type (unsignedp0,
1961 TREE_TYPE (primop0));
1963 /* If TYPE is an enumeration, then we need to get its min/max
1964 values from it's underlying integral type, not the enumerated
1965 type itself. */
1966 if (TREE_CODE (type) == ENUMERAL_TYPE)
1967 type = c_common_type_for_size (TYPE_PRECISION (type), unsignedp0);
1969 maxval = TYPE_MAX_VALUE (type);
1970 minval = TYPE_MIN_VALUE (type);
1972 if (unsignedp && !unsignedp0)
1973 *restype_ptr = c_common_signed_type (*restype_ptr);
1975 if (TREE_TYPE (primop1) != *restype_ptr)
1976 primop1 = convert (*restype_ptr, primop1);
1977 if (type != *restype_ptr)
1979 minval = convert (*restype_ptr, minval);
1980 maxval = convert (*restype_ptr, maxval);
1983 if (unsignedp && unsignedp0)
1985 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
1986 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
1987 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
1988 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
1990 else
1992 min_gt = INT_CST_LT (primop1, minval);
1993 max_gt = INT_CST_LT (primop1, maxval);
1994 min_lt = INT_CST_LT (minval, primop1);
1995 max_lt = INT_CST_LT (maxval, primop1);
1998 val = 0;
1999 /* This used to be a switch, but Genix compiler can't handle that. */
2000 if (code == NE_EXPR)
2002 if (max_lt || min_gt)
2003 val = boolean_true_node;
2005 else if (code == EQ_EXPR)
2007 if (max_lt || min_gt)
2008 val = boolean_false_node;
2010 else if (code == LT_EXPR)
2012 if (max_lt)
2013 val = boolean_true_node;
2014 if (!min_lt)
2015 val = boolean_false_node;
2017 else if (code == GT_EXPR)
2019 if (min_gt)
2020 val = boolean_true_node;
2021 if (!max_gt)
2022 val = boolean_false_node;
2024 else if (code == LE_EXPR)
2026 if (!max_gt)
2027 val = boolean_true_node;
2028 if (min_gt)
2029 val = boolean_false_node;
2031 else if (code == GE_EXPR)
2033 if (!min_lt)
2034 val = boolean_true_node;
2035 if (max_lt)
2036 val = boolean_false_node;
2039 /* If primop0 was sign-extended and unsigned comparison specd,
2040 we did a signed comparison above using the signed type bounds.
2041 But the comparison we output must be unsigned.
2043 Also, for inequalities, VAL is no good; but if the signed
2044 comparison had *any* fixed result, it follows that the
2045 unsigned comparison just tests the sign in reverse
2046 (positive values are LE, negative ones GE).
2047 So we can generate an unsigned comparison
2048 against an extreme value of the signed type. */
2050 if (unsignedp && !unsignedp0)
2052 if (val != 0)
2053 switch (code)
2055 case LT_EXPR:
2056 case GE_EXPR:
2057 primop1 = TYPE_MIN_VALUE (type);
2058 val = 0;
2059 break;
2061 case LE_EXPR:
2062 case GT_EXPR:
2063 primop1 = TYPE_MAX_VALUE (type);
2064 val = 0;
2065 break;
2067 default:
2068 break;
2070 type = c_common_unsigned_type (type);
2073 if (TREE_CODE (primop0) != INTEGER_CST)
2075 if (val == boolean_false_node)
2076 warning ("comparison is always false due to limited range of data type");
2077 if (val == boolean_true_node)
2078 warning ("comparison is always true due to limited range of data type");
2081 if (val != 0)
2083 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2084 if (TREE_SIDE_EFFECTS (primop0))
2085 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2086 return val;
2089 /* Value is not predetermined, but do the comparison
2090 in the type of the operand that is not constant.
2091 TYPE is already properly set. */
2093 else if (real1 && real2
2094 && (TYPE_PRECISION (TREE_TYPE (primop0))
2095 == TYPE_PRECISION (TREE_TYPE (primop1))))
2096 type = TREE_TYPE (primop0);
2098 /* If args' natural types are both narrower than nominal type
2099 and both extend in the same manner, compare them
2100 in the type of the wider arg.
2101 Otherwise must actually extend both to the nominal
2102 common type lest different ways of extending
2103 alter the result.
2104 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2106 else if (unsignedp0 == unsignedp1 && real1 == real2
2107 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2108 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2110 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2111 type = c_common_signed_or_unsigned_type (unsignedp0
2112 || TREE_UNSIGNED (*restype_ptr),
2113 type);
2114 /* Make sure shorter operand is extended the right way
2115 to match the longer operand. */
2116 primop0
2117 = convert (c_common_signed_or_unsigned_type (unsignedp0,
2118 TREE_TYPE (primop0)),
2119 primop0);
2120 primop1
2121 = convert (c_common_signed_or_unsigned_type (unsignedp1,
2122 TREE_TYPE (primop1)),
2123 primop1);
2125 else
2127 /* Here we must do the comparison on the nominal type
2128 using the args exactly as we received them. */
2129 type = *restype_ptr;
2130 primop0 = op0;
2131 primop1 = op1;
2133 if (!real1 && !real2 && integer_zerop (primop1)
2134 && TREE_UNSIGNED (*restype_ptr))
2136 tree value = 0;
2137 switch (code)
2139 case GE_EXPR:
2140 /* All unsigned values are >= 0, so we warn if extra warnings
2141 are requested. However, if OP0 is a constant that is
2142 >= 0, the signedness of the comparison isn't an issue,
2143 so suppress the warning. */
2144 if (extra_warnings && !in_system_header
2145 && ! (TREE_CODE (primop0) == INTEGER_CST
2146 && ! TREE_OVERFLOW (convert (c_common_signed_type (type),
2147 primop0))))
2148 warning ("comparison of unsigned expression >= 0 is always true");
2149 value = boolean_true_node;
2150 break;
2152 case LT_EXPR:
2153 if (extra_warnings && !in_system_header
2154 && ! (TREE_CODE (primop0) == INTEGER_CST
2155 && ! TREE_OVERFLOW (convert (c_common_signed_type (type),
2156 primop0))))
2157 warning ("comparison of unsigned expression < 0 is always false");
2158 value = boolean_false_node;
2159 break;
2161 default:
2162 break;
2165 if (value != 0)
2167 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2168 if (TREE_SIDE_EFFECTS (primop0))
2169 return build (COMPOUND_EXPR, TREE_TYPE (value),
2170 primop0, value);
2171 return value;
2176 *op0_ptr = convert (type, primop0);
2177 *op1_ptr = convert (type, primop1);
2179 *restype_ptr = boolean_type_node;
2181 return 0;
2184 /* Return a tree for the sum or difference (RESULTCODE says which)
2185 of pointer PTROP and integer INTOP. */
2187 tree
2188 pointer_int_sum (resultcode, ptrop, intop)
2189 enum tree_code resultcode;
2190 tree ptrop, intop;
2192 tree size_exp;
2194 tree result;
2195 tree folded;
2197 /* The result is a pointer of the same type that is being added. */
2199 tree result_type = TREE_TYPE (ptrop);
2201 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2203 if (pedantic || warn_pointer_arith)
2204 pedwarn ("pointer of type `void *' used in arithmetic");
2205 size_exp = integer_one_node;
2207 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2209 if (pedantic || warn_pointer_arith)
2210 pedwarn ("pointer to a function used in arithmetic");
2211 size_exp = integer_one_node;
2213 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
2215 if (pedantic || warn_pointer_arith)
2216 pedwarn ("pointer to member function used in arithmetic");
2217 size_exp = integer_one_node;
2219 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
2221 if (pedantic || warn_pointer_arith)
2222 pedwarn ("pointer to a member used in arithmetic");
2223 size_exp = integer_one_node;
2225 else
2226 size_exp = size_in_bytes (TREE_TYPE (result_type));
2228 /* If what we are about to multiply by the size of the elements
2229 contains a constant term, apply distributive law
2230 and multiply that constant term separately.
2231 This helps produce common subexpressions. */
2233 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2234 && ! TREE_CONSTANT (intop)
2235 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2236 && TREE_CONSTANT (size_exp)
2237 /* If the constant comes from pointer subtraction,
2238 skip this optimization--it would cause an error. */
2239 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2240 /* If the constant is unsigned, and smaller than the pointer size,
2241 then we must skip this optimization. This is because it could cause
2242 an overflow error if the constant is negative but INTOP is not. */
2243 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2244 || (TYPE_PRECISION (TREE_TYPE (intop))
2245 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2247 enum tree_code subcode = resultcode;
2248 tree int_type = TREE_TYPE (intop);
2249 if (TREE_CODE (intop) == MINUS_EXPR)
2250 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2251 /* Convert both subexpression types to the type of intop,
2252 because weird cases involving pointer arithmetic
2253 can result in a sum or difference with different type args. */
2254 ptrop = build_binary_op (subcode, ptrop,
2255 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2256 intop = convert (int_type, TREE_OPERAND (intop, 0));
2259 /* Convert the integer argument to a type the same size as sizetype
2260 so the multiply won't overflow spuriously. */
2262 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
2263 || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype))
2264 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
2265 TREE_UNSIGNED (sizetype)), intop);
2267 /* Replace the integer argument with a suitable product by the object size.
2268 Do this multiplication as signed, then convert to the appropriate
2269 pointer type (actually unsigned integral). */
2271 intop = convert (result_type,
2272 build_binary_op (MULT_EXPR, intop,
2273 convert (TREE_TYPE (intop), size_exp), 1));
2275 /* Create the sum or difference. */
2277 result = build (resultcode, result_type, ptrop, intop);
2279 folded = fold (result);
2280 if (folded == result)
2281 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2282 return folded;
2285 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2286 or validate its data type for an `if' or `while' statement or ?..: exp.
2288 This preparation consists of taking the ordinary
2289 representation of an expression expr and producing a valid tree
2290 boolean expression describing whether expr is nonzero. We could
2291 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2292 but we optimize comparisons, &&, ||, and !.
2294 The resulting type should always be `boolean_type_node'. */
2296 tree
2297 c_common_truthvalue_conversion (expr)
2298 tree expr;
2300 if (TREE_CODE (expr) == ERROR_MARK)
2301 return expr;
2303 #if 0 /* This appears to be wrong for C++. */
2304 /* These really should return error_mark_node after 2.4 is stable.
2305 But not all callers handle ERROR_MARK properly. */
2306 switch (TREE_CODE (TREE_TYPE (expr)))
2308 case RECORD_TYPE:
2309 error ("struct type value used where scalar is required");
2310 return boolean_false_node;
2312 case UNION_TYPE:
2313 error ("union type value used where scalar is required");
2314 return boolean_false_node;
2316 case ARRAY_TYPE:
2317 error ("array type value used where scalar is required");
2318 return boolean_false_node;
2320 default:
2321 break;
2323 #endif /* 0 */
2325 switch (TREE_CODE (expr))
2327 case EQ_EXPR:
2328 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2329 case TRUTH_ANDIF_EXPR:
2330 case TRUTH_ORIF_EXPR:
2331 case TRUTH_AND_EXPR:
2332 case TRUTH_OR_EXPR:
2333 case TRUTH_XOR_EXPR:
2334 case TRUTH_NOT_EXPR:
2335 TREE_TYPE (expr) = boolean_type_node;
2336 return expr;
2338 case ERROR_MARK:
2339 return expr;
2341 case INTEGER_CST:
2342 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2344 case REAL_CST:
2345 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2347 case ADDR_EXPR:
2348 /* If we are taking the address of an external decl, it might be zero
2349 if it is weak, so we cannot optimize. */
2350 if (DECL_P (TREE_OPERAND (expr, 0))
2351 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2352 break;
2354 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2355 return build (COMPOUND_EXPR, boolean_type_node,
2356 TREE_OPERAND (expr, 0), boolean_true_node);
2357 else
2358 return boolean_true_node;
2360 case COMPLEX_EXPR:
2361 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2362 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2363 c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)),
2364 c_common_truthvalue_conversion (TREE_OPERAND (expr, 1)),
2367 case NEGATE_EXPR:
2368 case ABS_EXPR:
2369 case FLOAT_EXPR:
2370 case FFS_EXPR:
2371 /* These don't change whether an object is non-zero or zero. */
2372 return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0));
2374 case LROTATE_EXPR:
2375 case RROTATE_EXPR:
2376 /* These don't change whether an object is zero or non-zero, but
2377 we can't ignore them if their second arg has side-effects. */
2378 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2379 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2380 c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)));
2381 else
2382 return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0));
2384 case COND_EXPR:
2385 /* Distribute the conversion into the arms of a COND_EXPR. */
2386 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2387 c_common_truthvalue_conversion (TREE_OPERAND (expr, 1)),
2388 c_common_truthvalue_conversion (TREE_OPERAND (expr, 2))));
2390 case CONVERT_EXPR:
2391 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2392 since that affects how `default_conversion' will behave. */
2393 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2394 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2395 break;
2396 /* fall through... */
2397 case NOP_EXPR:
2398 /* If this is widening the argument, we can ignore it. */
2399 if (TYPE_PRECISION (TREE_TYPE (expr))
2400 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2401 return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0));
2402 break;
2404 case MINUS_EXPR:
2405 /* Perhaps reduce (x - y) != 0 to (x != y). The expressions
2406 aren't guaranteed to the be same for modes that can represent
2407 infinity, since if x and y are both +infinity, or both
2408 -infinity, then x - y is not a number.
2410 Note that this transformation is safe when x or y is NaN.
2411 (x - y) is then NaN, and both (x - y) != 0 and x != y will
2412 be false. */
2413 if (HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0)))))
2414 break;
2415 /* fall through... */
2416 case BIT_XOR_EXPR:
2417 /* This and MINUS_EXPR can be changed into a comparison of the
2418 two objects. */
2419 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2420 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2421 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2422 TREE_OPERAND (expr, 1), 1);
2423 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2424 fold (build1 (NOP_EXPR,
2425 TREE_TYPE (TREE_OPERAND (expr, 0)),
2426 TREE_OPERAND (expr, 1))), 1);
2428 case BIT_AND_EXPR:
2429 if (integer_onep (TREE_OPERAND (expr, 1))
2430 && TREE_TYPE (expr) != boolean_type_node)
2431 /* Using convert here would cause infinite recursion. */
2432 return build1 (NOP_EXPR, boolean_type_node, expr);
2433 break;
2435 case MODIFY_EXPR:
2436 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2437 warning ("suggest parentheses around assignment used as truth value");
2438 break;
2440 default:
2441 break;
2444 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2446 tree t = save_expr (expr);
2447 return (build_binary_op
2448 ((TREE_SIDE_EFFECTS (expr)
2449 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2450 c_common_truthvalue_conversion (build_unary_op (REALPART_EXPR, t, 0)),
2451 c_common_truthvalue_conversion (build_unary_op (IMAGPART_EXPR, t, 0)),
2452 0));
2455 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2458 static tree builtin_function_2 PARAMS ((const char *, const char *, tree, tree,
2459 int, enum built_in_class, int, int,
2460 int));
2462 /* Make a variant type in the proper way for C/C++, propagating qualifiers
2463 down to the element type of an array. */
2465 tree
2466 c_build_qualified_type (type, type_quals)
2467 tree type;
2468 int type_quals;
2470 /* A restrict-qualified pointer type must be a pointer to object or
2471 incomplete type. Note that the use of POINTER_TYPE_P also allows
2472 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
2473 the C++ front-end also use POINTER_TYPE for pointer-to-member
2474 values, so even though it should be illegal to use `restrict'
2475 with such an entity we don't flag that here. Thus, special case
2476 code for that case is required in the C++ front-end. */
2477 if ((type_quals & TYPE_QUAL_RESTRICT)
2478 && (!POINTER_TYPE_P (type)
2479 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
2481 error ("invalid use of `restrict'");
2482 type_quals &= ~TYPE_QUAL_RESTRICT;
2485 if (TREE_CODE (type) == ARRAY_TYPE)
2486 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
2487 type_quals),
2488 TYPE_DOMAIN (type));
2489 return build_qualified_type (type, type_quals);
2492 /* Apply the TYPE_QUALS to the new DECL. */
2494 void
2495 c_apply_type_quals_to_decl (type_quals, decl)
2496 int type_quals;
2497 tree decl;
2499 if ((type_quals & TYPE_QUAL_CONST)
2500 || (TREE_TYPE (decl)
2501 && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE))
2502 TREE_READONLY (decl) = 1;
2503 if (type_quals & TYPE_QUAL_VOLATILE)
2505 TREE_SIDE_EFFECTS (decl) = 1;
2506 TREE_THIS_VOLATILE (decl) = 1;
2508 if (type_quals & TYPE_QUAL_RESTRICT)
2510 if (!TREE_TYPE (decl)
2511 || !POINTER_TYPE_P (TREE_TYPE (decl))
2512 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
2513 error ("invalid use of `restrict'");
2514 else if (flag_strict_aliasing)
2515 /* Indicate we need to make a unique alias set for this pointer.
2516 We can't do it here because it might be pointing to an
2517 incomplete type. */
2518 DECL_POINTER_ALIAS_SET (decl) = -2;
2522 /* Return the typed-based alias set for T, which may be an expression
2523 or a type. Return -1 if we don't do anything special. */
2525 HOST_WIDE_INT
2526 c_common_get_alias_set (t)
2527 tree t;
2529 tree u;
2531 /* We know nothing about vector types */
2532 if (TREE_CODE (t) == VECTOR_TYPE)
2533 return 0;
2535 /* Permit type-punning when accessing a union, provided the access
2536 is directly through the union. For example, this code does not
2537 permit taking the address of a union member and then storing
2538 through it. Even the type-punning allowed here is a GCC
2539 extension, albeit a common and useful one; the C standard says
2540 that such accesses have implementation-defined behavior. */
2541 for (u = t;
2542 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
2543 u = TREE_OPERAND (u, 0))
2544 if (TREE_CODE (u) == COMPONENT_REF
2545 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
2546 return 0;
2548 /* If this is a char *, the ANSI C standard says it can alias
2549 anything. Note that all references need do this. */
2550 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'r'
2551 && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
2552 && TYPE_PRECISION (TREE_TYPE (t)) == TYPE_PRECISION (char_type_node))
2553 return 0;
2555 /* If it has the may_alias attribute, it can alias anything. */
2556 if (TYPE_P (t) && lookup_attribute ("may_alias", TYPE_ATTRIBUTES (t)))
2557 return 0;
2559 /* That's all the expressions we handle specially. */
2560 if (! TYPE_P (t))
2561 return -1;
2563 /* The C standard specifically allows aliasing between signed and
2564 unsigned variants of the same type. We treat the signed
2565 variant as canonical. */
2566 if (TREE_CODE (t) == INTEGER_TYPE && TREE_UNSIGNED (t))
2568 tree t1 = c_common_signed_type (t);
2570 /* t1 == t can happen for boolean nodes which are always unsigned. */
2571 if (t1 != t)
2572 return get_alias_set (t1);
2574 else if (POINTER_TYPE_P (t))
2576 tree t1;
2578 /* Unfortunately, there is no canonical form of a pointer type.
2579 In particular, if we have `typedef int I', then `int *', and
2580 `I *' are different types. So, we have to pick a canonical
2581 representative. We do this below.
2583 Technically, this approach is actually more conservative that
2584 it needs to be. In particular, `const int *' and `int *'
2585 should be in different alias sets, according to the C and C++
2586 standard, since their types are not the same, and so,
2587 technically, an `int **' and `const int **' cannot point at
2588 the same thing.
2590 But, the standard is wrong. In particular, this code is
2591 legal C++:
2593 int *ip;
2594 int **ipp = &ip;
2595 const int* const* cipp = &ipp;
2597 And, it doesn't make sense for that to be legal unless you
2598 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
2599 the pointed-to types. This issue has been reported to the
2600 C++ committee. */
2601 t1 = build_type_no_quals (t);
2602 if (t1 != t)
2603 return get_alias_set (t1);
2606 return -1;
2609 /* Implement the __alignof keyword: Return the minimum required
2610 alignment of TYPE, measured in bytes. */
2612 tree
2613 c_alignof (type)
2614 tree type;
2616 enum tree_code code = TREE_CODE (type);
2617 tree t;
2619 /* In C++, sizeof applies to the referent. Handle alignof the same way. */
2620 if (code == REFERENCE_TYPE)
2622 type = TREE_TYPE (type);
2623 code = TREE_CODE (type);
2626 if (code == FUNCTION_TYPE)
2627 t = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
2628 else if (code == VOID_TYPE || code == ERROR_MARK)
2629 t = size_one_node;
2630 else if (!COMPLETE_TYPE_P (type))
2632 error ("__alignof__ applied to an incomplete type");
2633 t = size_zero_node;
2635 else
2636 t = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
2638 return fold (build1 (NOP_EXPR, c_size_type_node, t));
2641 /* Implement the __alignof keyword: Return the minimum required
2642 alignment of EXPR, measured in bytes. For VAR_DECL's and
2643 FIELD_DECL's return DECL_ALIGN (which can be set from an
2644 "aligned" __attribute__ specification). */
2646 tree
2647 c_alignof_expr (expr)
2648 tree expr;
2650 tree t;
2652 if (TREE_CODE (expr) == VAR_DECL)
2653 t = size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
2655 else if (TREE_CODE (expr) == COMPONENT_REF
2656 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2658 error ("`__alignof' applied to a bit-field");
2659 t = size_one_node;
2661 else if (TREE_CODE (expr) == COMPONENT_REF
2662 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
2663 t = size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
2665 else if (TREE_CODE (expr) == INDIRECT_REF)
2667 tree t = TREE_OPERAND (expr, 0);
2668 tree best = t;
2669 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
2671 while (TREE_CODE (t) == NOP_EXPR
2672 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
2674 int thisalign;
2676 t = TREE_OPERAND (t, 0);
2677 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
2678 if (thisalign > bestalign)
2679 best = t, bestalign = thisalign;
2681 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
2683 else
2684 return c_alignof (TREE_TYPE (expr));
2686 return fold (build1 (NOP_EXPR, c_size_type_node, t));
2689 /* Build tree nodes and builtin functions common to both C and C++ language
2690 frontends. */
2692 void
2693 c_common_nodes_and_builtins ()
2695 enum builtin_type
2697 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
2698 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
2699 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
2700 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
2701 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
2702 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
2703 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
2704 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
2705 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
2706 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
2707 #include "builtin-types.def"
2708 #undef DEF_PRIMITIVE_TYPE
2709 #undef DEF_FUNCTION_TYPE_0
2710 #undef DEF_FUNCTION_TYPE_1
2711 #undef DEF_FUNCTION_TYPE_2
2712 #undef DEF_FUNCTION_TYPE_3
2713 #undef DEF_FUNCTION_TYPE_4
2714 #undef DEF_FUNCTION_TYPE_VAR_0
2715 #undef DEF_FUNCTION_TYPE_VAR_1
2716 #undef DEF_FUNCTION_TYPE_VAR_2
2717 #undef DEF_POINTER_TYPE
2718 BT_LAST
2721 typedef enum builtin_type builtin_type;
2723 tree builtin_types[(int) BT_LAST];
2724 int wchar_type_size;
2725 tree array_domain_type;
2726 tree va_list_ref_type_node;
2727 tree va_list_arg_type_node;
2729 /* Define `int' and `char' first so that dbx will output them first. */
2730 record_builtin_type (RID_INT, NULL, integer_type_node);
2731 record_builtin_type (RID_CHAR, "char", char_type_node);
2733 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
2734 "unsigned long", "long long unsigned" and "unsigned short" were in C++
2735 but not C. Are the conditionals here needed? */
2736 if (c_language == clk_cplusplus)
2737 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
2738 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
2739 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
2740 record_builtin_type (RID_MAX, "long unsigned int",
2741 long_unsigned_type_node);
2742 if (c_language == clk_cplusplus)
2743 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
2744 record_builtin_type (RID_MAX, "long long int",
2745 long_long_integer_type_node);
2746 record_builtin_type (RID_MAX, "long long unsigned int",
2747 long_long_unsigned_type_node);
2748 if (c_language == clk_cplusplus)
2749 record_builtin_type (RID_MAX, "long long unsigned",
2750 long_long_unsigned_type_node);
2751 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
2752 record_builtin_type (RID_MAX, "short unsigned int",
2753 short_unsigned_type_node);
2754 if (c_language == clk_cplusplus)
2755 record_builtin_type (RID_MAX, "unsigned short",
2756 short_unsigned_type_node);
2758 /* Define both `signed char' and `unsigned char'. */
2759 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
2760 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
2762 /* These are types that c_common_type_for_size and
2763 c_common_type_for_mode use. */
2764 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
2765 intQI_type_node));
2766 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
2767 intHI_type_node));
2768 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
2769 intSI_type_node));
2770 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
2771 intDI_type_node));
2772 #if HOST_BITS_PER_WIDE_INT >= 64
2773 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
2774 get_identifier ("__int128_t"),
2775 intTI_type_node));
2776 #endif
2777 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
2778 unsigned_intQI_type_node));
2779 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
2780 unsigned_intHI_type_node));
2781 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
2782 unsigned_intSI_type_node));
2783 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
2784 unsigned_intDI_type_node));
2785 #if HOST_BITS_PER_WIDE_INT >= 64
2786 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
2787 get_identifier ("__uint128_t"),
2788 unsigned_intTI_type_node));
2789 #endif
2791 /* Create the widest literal types. */
2792 widest_integer_literal_type_node
2793 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
2794 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
2795 widest_integer_literal_type_node));
2797 widest_unsigned_literal_type_node
2798 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
2799 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
2800 widest_unsigned_literal_type_node));
2802 /* `unsigned long' is the standard type for sizeof.
2803 Note that stddef.h uses `unsigned long',
2804 and this must agree, even if long and int are the same size. */
2805 c_size_type_node =
2806 TREE_TYPE (identifier_global_value (get_identifier (SIZE_TYPE)));
2807 signed_size_type_node = c_common_signed_type (c_size_type_node);
2808 set_sizetype (c_size_type_node);
2810 build_common_tree_nodes_2 (flag_short_double);
2812 record_builtin_type (RID_FLOAT, NULL, float_type_node);
2813 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
2814 record_builtin_type (RID_MAX, "long double", long_double_type_node);
2816 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
2817 get_identifier ("complex int"),
2818 complex_integer_type_node));
2819 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
2820 get_identifier ("complex float"),
2821 complex_float_type_node));
2822 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
2823 get_identifier ("complex double"),
2824 complex_double_type_node));
2825 (*lang_hooks.decls.pushdecl)
2826 (build_decl (TYPE_DECL, get_identifier ("complex long double"),
2827 complex_long_double_type_node));
2829 /* Types which are common to the fortran compiler and libf2c. When
2830 changing these, you also need to be concerned with f/com.h. */
2832 if (TYPE_PRECISION (float_type_node)
2833 == TYPE_PRECISION (long_integer_type_node))
2835 g77_integer_type_node = long_integer_type_node;
2836 g77_uinteger_type_node = long_unsigned_type_node;
2838 else if (TYPE_PRECISION (float_type_node)
2839 == TYPE_PRECISION (integer_type_node))
2841 g77_integer_type_node = integer_type_node;
2842 g77_uinteger_type_node = unsigned_type_node;
2844 else
2845 g77_integer_type_node = g77_uinteger_type_node = NULL_TREE;
2847 if (g77_integer_type_node != NULL_TREE)
2849 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
2850 get_identifier ("__g77_integer"),
2851 g77_integer_type_node));
2852 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
2853 get_identifier ("__g77_uinteger"),
2854 g77_uinteger_type_node));
2857 if (TYPE_PRECISION (float_type_node) * 2
2858 == TYPE_PRECISION (long_integer_type_node))
2860 g77_longint_type_node = long_integer_type_node;
2861 g77_ulongint_type_node = long_unsigned_type_node;
2863 else if (TYPE_PRECISION (float_type_node) * 2
2864 == TYPE_PRECISION (long_long_integer_type_node))
2866 g77_longint_type_node = long_long_integer_type_node;
2867 g77_ulongint_type_node = long_long_unsigned_type_node;
2869 else
2870 g77_longint_type_node = g77_ulongint_type_node = NULL_TREE;
2872 if (g77_longint_type_node != NULL_TREE)
2874 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
2875 get_identifier ("__g77_longint"),
2876 g77_longint_type_node));
2877 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
2878 get_identifier ("__g77_ulongint"),
2879 g77_ulongint_type_node));
2882 record_builtin_type (RID_VOID, NULL, void_type_node);
2884 void_zero_node = build_int_2 (0, 0);
2885 TREE_TYPE (void_zero_node) = void_type_node;
2887 void_list_node = build_void_list_node ();
2889 /* Make a type to be the domain of a few array types
2890 whose domains don't really matter.
2891 200 is small enough that it always fits in size_t
2892 and large enough that it can hold most function names for the
2893 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
2894 array_domain_type = build_index_type (size_int (200));
2896 /* Make a type for arrays of characters.
2897 With luck nothing will ever really depend on the length of this
2898 array type. */
2899 char_array_type_node
2900 = build_array_type (char_type_node, array_domain_type);
2902 /* Likewise for arrays of ints. */
2903 int_array_type_node
2904 = build_array_type (integer_type_node, array_domain_type);
2906 string_type_node = build_pointer_type (char_type_node);
2907 const_string_type_node
2908 = build_pointer_type (build_qualified_type
2909 (char_type_node, TYPE_QUAL_CONST));
2911 (*targetm.init_builtins) ();
2913 /* This is special for C++ so functions can be overloaded. */
2914 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
2915 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
2916 wchar_type_size = TYPE_PRECISION (wchar_type_node);
2917 if (c_language == clk_cplusplus)
2919 if (TREE_UNSIGNED (wchar_type_node))
2920 wchar_type_node = make_unsigned_type (wchar_type_size);
2921 else
2922 wchar_type_node = make_signed_type (wchar_type_size);
2923 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
2925 else
2927 signed_wchar_type_node = c_common_signed_type (wchar_type_node);
2928 unsigned_wchar_type_node = c_common_unsigned_type (wchar_type_node);
2931 /* This is for wide string constants. */
2932 wchar_array_type_node
2933 = build_array_type (wchar_type_node, array_domain_type);
2935 wint_type_node =
2936 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
2938 intmax_type_node =
2939 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
2940 uintmax_type_node =
2941 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
2943 default_function_type = build_function_type (integer_type_node, NULL_TREE);
2944 ptrdiff_type_node
2945 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
2946 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
2948 (*lang_hooks.decls.pushdecl)
2949 (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
2950 va_list_type_node));
2952 (*lang_hooks.decls.pushdecl)
2953 (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"),
2954 ptrdiff_type_node));
2956 (*lang_hooks.decls.pushdecl)
2957 (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"),
2958 sizetype));
2960 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
2962 va_list_arg_type_node = va_list_ref_type_node =
2963 build_pointer_type (TREE_TYPE (va_list_type_node));
2965 else
2967 va_list_arg_type_node = va_list_type_node;
2968 va_list_ref_type_node = build_reference_type (va_list_type_node);
2971 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
2972 builtin_types[(int) ENUM] = VALUE;
2973 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
2974 builtin_types[(int) ENUM] \
2975 = build_function_type (builtin_types[(int) RETURN], \
2976 void_list_node);
2977 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
2978 builtin_types[(int) ENUM] \
2979 = build_function_type (builtin_types[(int) RETURN], \
2980 tree_cons (NULL_TREE, \
2981 builtin_types[(int) ARG1], \
2982 void_list_node));
2983 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
2984 builtin_types[(int) ENUM] \
2985 = build_function_type \
2986 (builtin_types[(int) RETURN], \
2987 tree_cons (NULL_TREE, \
2988 builtin_types[(int) ARG1], \
2989 tree_cons (NULL_TREE, \
2990 builtin_types[(int) ARG2], \
2991 void_list_node)));
2992 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
2993 builtin_types[(int) ENUM] \
2994 = build_function_type \
2995 (builtin_types[(int) RETURN], \
2996 tree_cons (NULL_TREE, \
2997 builtin_types[(int) ARG1], \
2998 tree_cons (NULL_TREE, \
2999 builtin_types[(int) ARG2], \
3000 tree_cons (NULL_TREE, \
3001 builtin_types[(int) ARG3], \
3002 void_list_node))));
3003 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3004 builtin_types[(int) ENUM] \
3005 = build_function_type \
3006 (builtin_types[(int) RETURN], \
3007 tree_cons (NULL_TREE, \
3008 builtin_types[(int) ARG1], \
3009 tree_cons (NULL_TREE, \
3010 builtin_types[(int) ARG2], \
3011 tree_cons \
3012 (NULL_TREE, \
3013 builtin_types[(int) ARG3], \
3014 tree_cons (NULL_TREE, \
3015 builtin_types[(int) ARG4], \
3016 void_list_node)))));
3017 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
3018 builtin_types[(int) ENUM] \
3019 = build_function_type (builtin_types[(int) RETURN], NULL_TREE);
3020 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
3021 builtin_types[(int) ENUM] \
3022 = build_function_type (builtin_types[(int) RETURN], \
3023 tree_cons (NULL_TREE, \
3024 builtin_types[(int) ARG1], \
3025 NULL_TREE));
3027 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
3028 builtin_types[(int) ENUM] \
3029 = build_function_type \
3030 (builtin_types[(int) RETURN], \
3031 tree_cons (NULL_TREE, \
3032 builtin_types[(int) ARG1], \
3033 tree_cons (NULL_TREE, \
3034 builtin_types[(int) ARG2], \
3035 NULL_TREE)));
3036 #define DEF_POINTER_TYPE(ENUM, TYPE) \
3037 builtin_types[(int) ENUM] \
3038 = build_pointer_type (builtin_types[(int) TYPE]);
3039 #include "builtin-types.def"
3040 #undef DEF_PRIMITIVE_TYPE
3041 #undef DEF_FUNCTION_TYPE_1
3042 #undef DEF_FUNCTION_TYPE_2
3043 #undef DEF_FUNCTION_TYPE_3
3044 #undef DEF_FUNCTION_TYPE_4
3045 #undef DEF_FUNCTION_TYPE_VAR_0
3046 #undef DEF_FUNCTION_TYPE_VAR_1
3047 #undef DEF_POINTER_TYPE
3049 #define DEF_BUILTIN(ENUM, NAME, CLASS, \
3050 TYPE, LIBTYPE, BOTH_P, FALLBACK_P, NONANSI_P) \
3051 if (NAME) \
3053 tree decl; \
3055 if (strncmp (NAME, "__builtin_", strlen ("__builtin_")) != 0) \
3056 abort (); \
3058 if (!BOTH_P) \
3059 decl = builtin_function (NAME, builtin_types[TYPE], ENUM, \
3060 CLASS, \
3061 (FALLBACK_P \
3062 ? (NAME + strlen ("__builtin_")) \
3063 : NULL)); \
3064 else \
3065 decl = builtin_function_2 (NAME, \
3066 NAME + strlen ("__builtin_"), \
3067 builtin_types[TYPE], \
3068 builtin_types[LIBTYPE], \
3069 ENUM, \
3070 CLASS, \
3071 FALLBACK_P, \
3072 NONANSI_P, \
3073 /*noreturn_p=*/0); \
3075 built_in_decls[(int) ENUM] = decl; \
3077 #include "builtins.def"
3078 #undef DEF_BUILTIN
3080 /* Declare _exit and _Exit just to mark them as non-returning. */
3081 builtin_function_2 (NULL, "_exit", NULL_TREE,
3082 builtin_types[BT_FN_VOID_INT],
3083 0, NOT_BUILT_IN, 0, 1, 1);
3084 builtin_function_2 (NULL, "_Exit", NULL_TREE,
3085 builtin_types[BT_FN_VOID_INT],
3086 0, NOT_BUILT_IN, 0, !flag_isoc99, 1);
3088 /* Declare these functions non-returning
3089 to avoid spurious "control drops through" warnings. */
3090 builtin_function_2 (NULL, "abort",
3091 NULL_TREE, ((c_language == clk_cplusplus)
3092 ? builtin_types[BT_FN_VOID]
3093 : builtin_types[BT_FN_VOID_VAR]),
3094 0, NOT_BUILT_IN, 0, 0, 1);
3096 builtin_function_2 (NULL, "exit",
3097 NULL_TREE, ((c_language == clk_cplusplus)
3098 ? builtin_types[BT_FN_VOID_INT]
3099 : builtin_types[BT_FN_VOID_VAR]),
3100 0, NOT_BUILT_IN, 0, 0, 1);
3102 main_identifier_node = get_identifier ("main");
3105 tree
3106 build_va_arg (expr, type)
3107 tree expr, type;
3109 return build1 (VA_ARG_EXPR, type, expr);
3113 /* Linked list of disabled built-in functions. */
3115 typedef struct disabled_builtin
3117 const char *name;
3118 struct disabled_builtin *next;
3119 } disabled_builtin;
3120 static disabled_builtin *disabled_builtins = NULL;
3122 static bool builtin_function_disabled_p PARAMS ((const char *));
3124 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
3125 begins with "__builtin_", give an error. */
3127 void
3128 disable_builtin_function (name)
3129 const char *name;
3131 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
3132 error ("cannot disable built-in function `%s'", name);
3133 else
3135 disabled_builtin *new = xmalloc (sizeof (disabled_builtin));
3136 new->name = name;
3137 new->next = disabled_builtins;
3138 disabled_builtins = new;
3143 /* Return true if the built-in function NAME has been disabled, false
3144 otherwise. */
3146 static bool
3147 builtin_function_disabled_p (name)
3148 const char *name;
3150 disabled_builtin *p;
3151 for (p = disabled_builtins; p != NULL; p = p->next)
3153 if (strcmp (name, p->name) == 0)
3154 return true;
3156 return false;
3160 /* Possibly define a builtin function with one or two names. BUILTIN_NAME
3161 is an __builtin_-prefixed name; NAME is the ordinary name; one or both
3162 of these may be NULL (though both being NULL is useless).
3163 BUILTIN_TYPE is the type of the __builtin_-prefixed function;
3164 TYPE is the type of the function with the ordinary name. These
3165 may differ if the ordinary name is declared with a looser type to avoid
3166 conflicts with headers. FUNCTION_CODE and CLASS are as for
3167 builtin_function. If LIBRARY_NAME_P is nonzero, NAME is passed as
3168 the LIBRARY_NAME parameter to builtin_function when declaring BUILTIN_NAME.
3169 If NONANSI_P is nonzero, the name NAME is treated as a non-ANSI name; if
3170 NORETURN_P is nonzero, the function is marked as non-returning.
3171 Returns the declaration of BUILTIN_NAME, if any, otherwise
3172 the declaration of NAME. Does not declare NAME if flag_no_builtin,
3173 or if NONANSI_P and flag_no_nonansi_builtin. */
3175 static tree
3176 builtin_function_2 (builtin_name, name, builtin_type, type, function_code,
3177 class, library_name_p, nonansi_p, noreturn_p)
3178 const char *builtin_name;
3179 const char *name;
3180 tree builtin_type;
3181 tree type;
3182 int function_code;
3183 enum built_in_class class;
3184 int library_name_p;
3185 int nonansi_p;
3186 int noreturn_p;
3188 tree bdecl = NULL_TREE;
3189 tree decl = NULL_TREE;
3190 if (builtin_name != 0)
3192 bdecl = builtin_function (builtin_name, builtin_type, function_code,
3193 class, library_name_p ? name : NULL);
3194 if (noreturn_p)
3196 TREE_THIS_VOLATILE (bdecl) = 1;
3197 TREE_SIDE_EFFECTS (bdecl) = 1;
3200 if (name != 0 && !flag_no_builtin && !builtin_function_disabled_p (name)
3201 && !(nonansi_p && flag_no_nonansi_builtin))
3203 decl = builtin_function (name, type, function_code, class, NULL);
3204 if (nonansi_p)
3205 DECL_BUILT_IN_NONANSI (decl) = 1;
3206 if (noreturn_p)
3208 TREE_THIS_VOLATILE (decl) = 1;
3209 TREE_SIDE_EFFECTS (decl) = 1;
3212 return (bdecl != 0 ? bdecl : decl);
3215 /* Nonzero if the type T promotes to int. This is (nearly) the
3216 integral promotions defined in ISO C99 6.3.1.1/2. */
3218 bool
3219 c_promoting_integer_type_p (t)
3220 tree t;
3222 switch (TREE_CODE (t))
3224 case INTEGER_TYPE:
3225 return (TYPE_MAIN_VARIANT (t) == char_type_node
3226 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
3227 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
3228 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
3229 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
3230 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
3232 case ENUMERAL_TYPE:
3233 /* ??? Technically all enumerations not larger than an int
3234 promote to an int. But this is used along code paths
3235 that only want to notice a size change. */
3236 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
3238 case BOOLEAN_TYPE:
3239 return 1;
3241 default:
3242 return 0;
3246 /* Return 1 if PARMS specifies a fixed number of parameters
3247 and none of their types is affected by default promotions. */
3250 self_promoting_args_p (parms)
3251 tree parms;
3253 tree t;
3254 for (t = parms; t; t = TREE_CHAIN (t))
3256 tree type = TREE_VALUE (t);
3258 if (TREE_CHAIN (t) == 0 && type != void_type_node)
3259 return 0;
3261 if (type == 0)
3262 return 0;
3264 if (TYPE_MAIN_VARIANT (type) == float_type_node)
3265 return 0;
3267 if (c_promoting_integer_type_p (type))
3268 return 0;
3270 return 1;
3273 /* Recursively examines the array elements of TYPE, until a non-array
3274 element type is found. */
3276 tree
3277 strip_array_types (type)
3278 tree type;
3280 while (TREE_CODE (type) == ARRAY_TYPE)
3281 type = TREE_TYPE (type);
3283 return type;
3286 static tree expand_unordered_cmp PARAMS ((tree, tree, enum tree_code,
3287 enum tree_code));
3289 /* Expand a call to an unordered comparison function such as
3290 __builtin_isgreater(). FUNCTION is the function's declaration and
3291 PARAMS a list of the values passed. For __builtin_isunordered(),
3292 UNORDERED_CODE is UNORDERED_EXPR and ORDERED_CODE is NOP_EXPR. In
3293 other cases, UNORDERED_CODE and ORDERED_CODE are comparison codes
3294 that give the opposite of the desired result. UNORDERED_CODE is
3295 used for modes that can hold NaNs and ORDERED_CODE is used for the
3296 rest. */
3298 static tree
3299 expand_unordered_cmp (function, params, unordered_code, ordered_code)
3300 tree function, params;
3301 enum tree_code unordered_code, ordered_code;
3303 tree arg0, arg1, type;
3304 enum tree_code code0, code1;
3306 /* Check that we have exactly two arguments. */
3307 if (params == 0 || TREE_CHAIN (params) == 0)
3309 error ("too few arguments to function `%s'",
3310 IDENTIFIER_POINTER (DECL_NAME (function)));
3311 return error_mark_node;
3313 else if (TREE_CHAIN (TREE_CHAIN (params)) != 0)
3315 error ("too many arguments to function `%s'",
3316 IDENTIFIER_POINTER (DECL_NAME (function)));
3317 return error_mark_node;
3320 arg0 = TREE_VALUE (params);
3321 arg1 = TREE_VALUE (TREE_CHAIN (params));
3323 code0 = TREE_CODE (TREE_TYPE (arg0));
3324 code1 = TREE_CODE (TREE_TYPE (arg1));
3326 /* Make sure that the arguments have a common type of REAL. */
3327 type = 0;
3328 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3329 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3330 type = common_type (TREE_TYPE (arg0), TREE_TYPE (arg1));
3332 if (type == 0 || TREE_CODE (type) != REAL_TYPE)
3334 error ("non-floating-point argument to function `%s'",
3335 IDENTIFIER_POINTER (DECL_NAME (function)));
3336 return error_mark_node;
3339 if (unordered_code == UNORDERED_EXPR)
3341 if (MODE_HAS_NANS (TYPE_MODE (type)))
3342 return build_binary_op (unordered_code,
3343 convert (type, arg0),
3344 convert (type, arg1),
3346 else
3347 return integer_zero_node;
3350 return build_unary_op (TRUTH_NOT_EXPR,
3351 build_binary_op (MODE_HAS_NANS (TYPE_MODE (type))
3352 ? unordered_code
3353 : ordered_code,
3354 convert (type, arg0),
3355 convert (type, arg1),
3361 /* Recognize certain built-in functions so we can make tree-codes
3362 other than CALL_EXPR. We do this when it enables fold-const.c
3363 to do something useful. */
3364 /* ??? By rights this should go in builtins.c, but only C and C++
3365 implement build_{binary,unary}_op. Not exactly sure what bits
3366 of functionality are actually needed from those functions, or
3367 where the similar functionality exists in the other front ends. */
3369 tree
3370 expand_tree_builtin (function, params, coerced_params)
3371 tree function, params, coerced_params;
3373 if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL)
3374 return NULL_TREE;
3376 switch (DECL_FUNCTION_CODE (function))
3378 case BUILT_IN_ABS:
3379 case BUILT_IN_LABS:
3380 case BUILT_IN_LLABS:
3381 case BUILT_IN_IMAXABS:
3382 case BUILT_IN_FABS:
3383 case BUILT_IN_FABSL:
3384 case BUILT_IN_FABSF:
3385 if (coerced_params == 0)
3386 return integer_zero_node;
3387 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
3389 case BUILT_IN_CONJ:
3390 case BUILT_IN_CONJF:
3391 case BUILT_IN_CONJL:
3392 if (coerced_params == 0)
3393 return integer_zero_node;
3394 return build_unary_op (CONJ_EXPR, TREE_VALUE (coerced_params), 0);
3396 case BUILT_IN_CREAL:
3397 case BUILT_IN_CREALF:
3398 case BUILT_IN_CREALL:
3399 if (coerced_params == 0)
3400 return integer_zero_node;
3401 return build_unary_op (REALPART_EXPR, TREE_VALUE (coerced_params), 0);
3403 case BUILT_IN_CIMAG:
3404 case BUILT_IN_CIMAGF:
3405 case BUILT_IN_CIMAGL:
3406 if (coerced_params == 0)
3407 return integer_zero_node;
3408 return build_unary_op (IMAGPART_EXPR, TREE_VALUE (coerced_params), 0);
3410 case BUILT_IN_ISGREATER:
3411 return expand_unordered_cmp (function, params, UNLE_EXPR, LE_EXPR);
3413 case BUILT_IN_ISGREATEREQUAL:
3414 return expand_unordered_cmp (function, params, UNLT_EXPR, LT_EXPR);
3416 case BUILT_IN_ISLESS:
3417 return expand_unordered_cmp (function, params, UNGE_EXPR, GE_EXPR);
3419 case BUILT_IN_ISLESSEQUAL:
3420 return expand_unordered_cmp (function, params, UNGT_EXPR, GT_EXPR);
3422 case BUILT_IN_ISLESSGREATER:
3423 return expand_unordered_cmp (function, params, UNEQ_EXPR, EQ_EXPR);
3425 case BUILT_IN_ISUNORDERED:
3426 return expand_unordered_cmp (function, params, UNORDERED_EXPR, NOP_EXPR);
3428 default:
3429 break;
3432 return NULL_TREE;
3435 /* Returns non-zero if CODE is the code for a statement. */
3438 statement_code_p (code)
3439 enum tree_code code;
3441 switch (code)
3443 case CLEANUP_STMT:
3444 case EXPR_STMT:
3445 case COMPOUND_STMT:
3446 case DECL_STMT:
3447 case IF_STMT:
3448 case FOR_STMT:
3449 case WHILE_STMT:
3450 case DO_STMT:
3451 case RETURN_STMT:
3452 case BREAK_STMT:
3453 case CONTINUE_STMT:
3454 case SCOPE_STMT:
3455 case SWITCH_STMT:
3456 case GOTO_STMT:
3457 case LABEL_STMT:
3458 case ASM_STMT:
3459 case FILE_STMT:
3460 case CASE_LABEL:
3461 return 1;
3463 default:
3464 if (lang_statement_code_p)
3465 return (*lang_statement_code_p) (code);
3466 return 0;
3470 /* Walk the statement tree, rooted at *tp. Apply FUNC to all the
3471 sub-trees of *TP in a pre-order traversal. FUNC is called with the
3472 DATA and the address of each sub-tree. If FUNC returns a non-NULL
3473 value, the traversal is aborted, and the value returned by FUNC is
3474 returned. If FUNC sets WALK_SUBTREES to zero, then the subtrees of
3475 the node being visited are not walked.
3477 We don't need a without_duplicates variant of this one because the
3478 statement tree is a tree, not a graph. */
3480 tree
3481 walk_stmt_tree (tp, func, data)
3482 tree *tp;
3483 walk_tree_fn func;
3484 void *data;
3486 enum tree_code code;
3487 int walk_subtrees;
3488 tree result;
3489 int i, len;
3491 #define WALK_SUBTREE(NODE) \
3492 do \
3494 result = walk_stmt_tree (&(NODE), func, data); \
3495 if (result) \
3496 return result; \
3498 while (0)
3500 /* Skip empty subtrees. */
3501 if (!*tp)
3502 return NULL_TREE;
3504 /* Skip subtrees below non-statement nodes. */
3505 if (!statement_code_p (TREE_CODE (*tp)))
3506 return NULL_TREE;
3508 /* Call the function. */
3509 walk_subtrees = 1;
3510 result = (*func) (tp, &walk_subtrees, data);
3512 /* If we found something, return it. */
3513 if (result)
3514 return result;
3516 /* FUNC may have modified the tree, recheck that we're looking at a
3517 statement node. */
3518 code = TREE_CODE (*tp);
3519 if (!statement_code_p (code))
3520 return NULL_TREE;
3522 /* Visit the subtrees unless FUNC decided that there was nothing
3523 interesting below this point in the tree. */
3524 if (walk_subtrees)
3526 /* Walk over all the sub-trees of this operand. Statement nodes
3527 never contain RTL, and we needn't worry about TARGET_EXPRs. */
3528 len = TREE_CODE_LENGTH (code);
3530 /* Go through the subtrees. We need to do this in forward order so
3531 that the scope of a FOR_EXPR is handled properly. */
3532 for (i = 0; i < len; ++i)
3533 WALK_SUBTREE (TREE_OPERAND (*tp, i));
3536 /* Finally visit the chain. This can be tail-recursion optimized if
3537 we write it this way. */
3538 return walk_stmt_tree (&TREE_CHAIN (*tp), func, data);
3540 #undef WALK_SUBTREE
3543 /* Used to compare case labels. K1 and K2 are actually tree nodes
3544 representing case labels, or NULL_TREE for a `default' label.
3545 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
3546 K2, and 0 if K1 and K2 are equal. */
3549 case_compare (k1, k2)
3550 splay_tree_key k1;
3551 splay_tree_key k2;
3553 /* Consider a NULL key (such as arises with a `default' label) to be
3554 smaller than anything else. */
3555 if (!k1)
3556 return k2 ? -1 : 0;
3557 else if (!k2)
3558 return k1 ? 1 : 0;
3560 return tree_int_cst_compare ((tree) k1, (tree) k2);
3563 /* Process a case label for the range LOW_VALUE ... HIGH_VALUE. If
3564 LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is
3565 actually a `default' label. If only HIGH_VALUE is NULL_TREE, then
3566 case label was declared using the usual C/C++ syntax, rather than
3567 the GNU case range extension. CASES is a tree containing all the
3568 case ranges processed so far; COND is the condition for the
3569 switch-statement itself. Returns the CASE_LABEL created, or
3570 ERROR_MARK_NODE if no CASE_LABEL is created. */
3572 tree
3573 c_add_case_label (cases, cond, low_value, high_value)
3574 splay_tree cases;
3575 tree cond;
3576 tree low_value;
3577 tree high_value;
3579 tree type;
3580 tree label;
3581 tree case_label;
3582 splay_tree_node node;
3584 /* Create the LABEL_DECL itself. */
3585 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
3586 DECL_CONTEXT (label) = current_function_decl;
3588 /* If there was an error processing the switch condition, bail now
3589 before we get more confused. */
3590 if (!cond || cond == error_mark_node)
3592 /* Add a label anyhow so that the back-end doesn't think that
3593 the beginning of the switch is unreachable. */
3594 if (!cases->root)
3595 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
3596 return error_mark_node;
3599 if ((low_value && TREE_TYPE (low_value)
3600 && POINTER_TYPE_P (TREE_TYPE (low_value)))
3601 || (high_value && TREE_TYPE (high_value)
3602 && POINTER_TYPE_P (TREE_TYPE (high_value))))
3603 error ("pointers are not permitted as case values");
3605 /* Case ranges are a GNU extension. */
3606 if (high_value && pedantic)
3608 if (c_language == clk_cplusplus)
3609 pedwarn ("ISO C++ forbids range expressions in switch statements");
3610 else
3611 pedwarn ("ISO C forbids range expressions in switch statements");
3614 type = TREE_TYPE (cond);
3615 if (low_value)
3617 low_value = check_case_value (low_value);
3618 low_value = convert_and_check (type, low_value);
3620 if (high_value)
3622 high_value = check_case_value (high_value);
3623 high_value = convert_and_check (type, high_value);
3626 /* If an error has occurred, bail out now. */
3627 if (low_value == error_mark_node || high_value == error_mark_node)
3629 if (!cases->root)
3630 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
3631 return error_mark_node;
3634 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
3635 really a case range, even though it was written that way. Remove
3636 the HIGH_VALUE to simplify later processing. */
3637 if (tree_int_cst_equal (low_value, high_value))
3638 high_value = NULL_TREE;
3639 if (low_value && high_value
3640 && !tree_int_cst_lt (low_value, high_value))
3641 warning ("empty range specified");
3643 /* Look up the LOW_VALUE in the table of case labels we already
3644 have. */
3645 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
3646 /* If there was not an exact match, check for overlapping ranges.
3647 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
3648 that's a `default' label and the only overlap is an exact match. */
3649 if (!node && (low_value || high_value))
3651 splay_tree_node low_bound;
3652 splay_tree_node high_bound;
3654 /* Even though there wasn't an exact match, there might be an
3655 overlap between this case range and another case range.
3656 Since we've (inductively) not allowed any overlapping case
3657 ranges, we simply need to find the greatest low case label
3658 that is smaller that LOW_VALUE, and the smallest low case
3659 label that is greater than LOW_VALUE. If there is an overlap
3660 it will occur in one of these two ranges. */
3661 low_bound = splay_tree_predecessor (cases,
3662 (splay_tree_key) low_value);
3663 high_bound = splay_tree_successor (cases,
3664 (splay_tree_key) low_value);
3666 /* Check to see if the LOW_BOUND overlaps. It is smaller than
3667 the LOW_VALUE, so there is no need to check unless the
3668 LOW_BOUND is in fact itself a case range. */
3669 if (low_bound
3670 && CASE_HIGH ((tree) low_bound->value)
3671 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
3672 low_value) >= 0)
3673 node = low_bound;
3674 /* Check to see if the HIGH_BOUND overlaps. The low end of that
3675 range is bigger than the low end of the current range, so we
3676 are only interested if the current range is a real range, and
3677 not an ordinary case label. */
3678 else if (high_bound
3679 && high_value
3680 && (tree_int_cst_compare ((tree) high_bound->key,
3681 high_value)
3682 <= 0))
3683 node = high_bound;
3685 /* If there was an overlap, issue an error. */
3686 if (node)
3688 tree duplicate = CASE_LABEL_DECL ((tree) node->value);
3690 if (high_value)
3692 error ("duplicate (or overlapping) case value");
3693 error_with_decl (duplicate,
3694 "this is the first entry overlapping that value");
3696 else if (low_value)
3698 error ("duplicate case value") ;
3699 error_with_decl (duplicate, "previously used here");
3701 else
3703 error ("multiple default labels in one switch");
3704 error_with_decl (duplicate, "this is the first default label");
3706 if (!cases->root)
3707 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
3710 /* Add a CASE_LABEL to the statement-tree. */
3711 case_label = add_stmt (build_case_label (low_value, high_value, label));
3712 /* Register this case label in the splay tree. */
3713 splay_tree_insert (cases,
3714 (splay_tree_key) low_value,
3715 (splay_tree_value) case_label);
3717 return case_label;
3720 /* Finish an expression taking the address of LABEL. Returns an
3721 expression for the address. */
3723 tree
3724 finish_label_address_expr (label)
3725 tree label;
3727 tree result;
3729 if (pedantic)
3731 if (c_language == clk_cplusplus)
3732 pedwarn ("ISO C++ forbids taking the address of a label");
3733 else
3734 pedwarn ("ISO C forbids taking the address of a label");
3737 label = lookup_label (label);
3738 if (label == NULL_TREE)
3739 result = null_pointer_node;
3740 else
3742 TREE_USED (label) = 1;
3743 result = build1 (ADDR_EXPR, ptr_type_node, label);
3744 TREE_CONSTANT (result) = 1;
3745 /* The current function in not necessarily uninlinable.
3746 Computed gotos are incompatible with inlining, but the value
3747 here could be used only in a diagnostic, for example. */
3750 return result;
3753 /* Hook used by expand_expr to expand language-specific tree codes. */
3756 c_expand_expr (exp, target, tmode, modifier)
3757 tree exp;
3758 rtx target;
3759 enum machine_mode tmode;
3760 int modifier; /* Actually enum_modifier. */
3762 switch (TREE_CODE (exp))
3764 case STMT_EXPR:
3766 tree rtl_expr;
3767 rtx result;
3768 bool preserve_result = false;
3770 /* Since expand_expr_stmt calls free_temp_slots after every
3771 expression statement, we must call push_temp_slots here.
3772 Otherwise, any temporaries in use now would be considered
3773 out-of-scope after the first EXPR_STMT from within the
3774 STMT_EXPR. */
3775 push_temp_slots ();
3776 rtl_expr = expand_start_stmt_expr (!STMT_EXPR_NO_SCOPE (exp));
3778 /* If we want the result of this expression, find the last
3779 EXPR_STMT in the COMPOUND_STMT and mark it as addressable. */
3780 if (target != const0_rtx
3781 && TREE_CODE (STMT_EXPR_STMT (exp)) == COMPOUND_STMT
3782 && TREE_CODE (COMPOUND_BODY (STMT_EXPR_STMT (exp))) == SCOPE_STMT)
3784 tree expr = COMPOUND_BODY (STMT_EXPR_STMT (exp));
3785 tree last = TREE_CHAIN (expr);
3787 while (TREE_CHAIN (last))
3789 expr = last;
3790 last = TREE_CHAIN (last);
3793 if (TREE_CODE (last) == SCOPE_STMT
3794 && TREE_CODE (expr) == EXPR_STMT)
3796 TREE_ADDRESSABLE (expr) = 1;
3797 preserve_result = true;
3801 expand_stmt (STMT_EXPR_STMT (exp));
3802 expand_end_stmt_expr (rtl_expr);
3804 result = expand_expr (rtl_expr, target, tmode, modifier);
3805 if (preserve_result && GET_CODE (result) == MEM)
3807 if (GET_MODE (result) != BLKmode)
3808 result = copy_to_reg (result);
3809 else
3810 preserve_temp_slots (result);
3813 /* If the statment-expression does not have a scope, then the
3814 new temporaries we created within it must live beyond the
3815 statement-expression. */
3816 if (STMT_EXPR_NO_SCOPE (exp))
3817 preserve_temp_slots (NULL_RTX);
3819 pop_temp_slots ();
3820 return result;
3822 break;
3824 case CALL_EXPR:
3826 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
3827 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
3828 == FUNCTION_DECL)
3829 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
3830 && (DECL_BUILT_IN_CLASS (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
3831 == BUILT_IN_FRONTEND))
3832 return c_expand_builtin (exp, target, tmode, modifier);
3833 else
3834 abort ();
3836 break;
3838 case COMPOUND_LITERAL_EXPR:
3840 /* Initialize the anonymous variable declared in the compound
3841 literal, then return the variable. */
3842 tree decl = COMPOUND_LITERAL_EXPR_DECL (exp);
3843 emit_local_var (decl);
3844 return expand_expr (decl, target, tmode, modifier);
3847 default:
3848 abort ();
3851 abort ();
3852 return NULL;
3855 /* Hook used by safe_from_p to handle language-specific tree codes. */
3858 c_safe_from_p (target, exp)
3859 rtx target;
3860 tree exp;
3862 /* We can see statements here when processing the body of a
3863 statement-expression. For a declaration statement declaring a
3864 variable, look at the variable's initializer. */
3865 if (TREE_CODE (exp) == DECL_STMT)
3867 tree decl = DECL_STMT_DECL (exp);
3869 if (TREE_CODE (decl) == VAR_DECL
3870 && DECL_INITIAL (decl)
3871 && !safe_from_p (target, DECL_INITIAL (decl), /*top_p=*/0))
3872 return 0;
3875 /* For any statement, we must follow the statement-chain. */
3876 if (statement_code_p (TREE_CODE (exp)) && TREE_CHAIN (exp))
3877 return safe_from_p (target, TREE_CHAIN (exp), /*top_p=*/0);
3879 /* Assume everything else is safe. */
3880 return 1;
3883 /* Hook used by unsafe_for_reeval to handle language-specific tree codes. */
3886 c_common_unsafe_for_reeval (exp)
3887 tree exp;
3889 /* Statement expressions may not be reevaluated, likewise compound
3890 literals. */
3891 if (TREE_CODE (exp) == STMT_EXPR
3892 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
3893 return 2;
3895 /* Walk all other expressions. */
3896 return -1;
3899 /* Hook used by staticp to handle language-specific tree codes. */
3902 c_staticp (exp)
3903 tree exp;
3905 if (TREE_CODE (exp) == COMPOUND_LITERAL_EXPR
3906 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
3907 return 1;
3908 return 0;
3911 #define CALLED_AS_BUILT_IN(NODE) \
3912 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
3914 static rtx
3915 c_expand_builtin (exp, target, tmode, modifier)
3916 tree exp;
3917 rtx target;
3918 enum machine_mode tmode;
3919 enum expand_modifier modifier;
3921 tree type = TREE_TYPE (exp);
3922 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
3923 tree arglist = TREE_OPERAND (exp, 1);
3924 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
3925 enum tree_code code = TREE_CODE (exp);
3926 const int ignore = (target == const0_rtx
3927 || ((code == NON_LVALUE_EXPR || code == NOP_EXPR
3928 || code == CONVERT_EXPR || code == REFERENCE_EXPR
3929 || code == COND_EXPR)
3930 && TREE_CODE (type) == VOID_TYPE));
3932 if (! optimize && ! CALLED_AS_BUILT_IN (fndecl))
3933 return expand_call (exp, target, ignore);
3935 switch (fcode)
3937 case BUILT_IN_PRINTF:
3938 target = c_expand_builtin_printf (arglist, target, tmode,
3939 modifier, ignore, /*unlocked=*/ 0);
3940 if (target)
3941 return target;
3942 break;
3944 case BUILT_IN_PRINTF_UNLOCKED:
3945 target = c_expand_builtin_printf (arglist, target, tmode,
3946 modifier, ignore, /*unlocked=*/ 1);
3947 if (target)
3948 return target;
3949 break;
3951 case BUILT_IN_FPRINTF:
3952 target = c_expand_builtin_fprintf (arglist, target, tmode,
3953 modifier, ignore, /*unlocked=*/ 0);
3954 if (target)
3955 return target;
3956 break;
3958 case BUILT_IN_FPRINTF_UNLOCKED:
3959 target = c_expand_builtin_fprintf (arglist, target, tmode,
3960 modifier, ignore, /*unlocked=*/ 1);
3961 if (target)
3962 return target;
3963 break;
3965 default: /* just do library call, if unknown builtin */
3966 error ("built-in function `%s' not currently supported",
3967 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
3970 /* The switch statement above can drop through to cause the function
3971 to be called normally. */
3972 return expand_call (exp, target, ignore);
3975 /* Check an arglist to *printf for problems. The arglist should start
3976 at the format specifier, with the remaining arguments immediately
3977 following it. */
3978 static int
3979 is_valid_printf_arglist (arglist)
3980 tree arglist;
3982 /* Save this value so we can restore it later. */
3983 const int SAVE_pedantic = pedantic;
3984 int diagnostic_occurred = 0;
3985 tree attrs;
3987 /* Set this to a known value so the user setting won't affect code
3988 generation. */
3989 pedantic = 1;
3990 /* Check to make sure there are no format specifier errors. */
3991 attrs = tree_cons (get_identifier ("format"),
3992 tree_cons (NULL_TREE,
3993 get_identifier ("printf"),
3994 tree_cons (NULL_TREE,
3995 integer_one_node,
3996 tree_cons (NULL_TREE,
3997 build_int_2 (2, 0),
3998 NULL_TREE))),
3999 NULL_TREE);
4000 check_function_format (&diagnostic_occurred, attrs, arglist);
4002 /* Restore the value of `pedantic'. */
4003 pedantic = SAVE_pedantic;
4005 /* If calling `check_function_format_ptr' produces a warning, we
4006 return false, otherwise we return true. */
4007 return ! diagnostic_occurred;
4010 /* If the arguments passed to printf are suitable for optimizations,
4011 we attempt to transform the call. */
4012 static rtx
4013 c_expand_builtin_printf (arglist, target, tmode, modifier, ignore, unlocked)
4014 tree arglist;
4015 rtx target;
4016 enum machine_mode tmode;
4017 enum expand_modifier modifier;
4018 int ignore;
4019 int unlocked;
4021 tree fn_putchar = unlocked ?
4022 built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED] : built_in_decls[BUILT_IN_PUTCHAR];
4023 tree fn_puts = unlocked ?
4024 built_in_decls[BUILT_IN_PUTS_UNLOCKED] : built_in_decls[BUILT_IN_PUTS];
4025 tree fn, format_arg, stripped_string;
4027 /* If the return value is used, or the replacement _DECL isn't
4028 initialized, don't do the transformation. */
4029 if (!ignore || !fn_putchar || !fn_puts)
4030 return 0;
4032 /* Verify the required arguments in the original call. */
4033 if (arglist == 0
4034 || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE))
4035 return 0;
4037 /* Check the specifier vs. the parameters. */
4038 if (!is_valid_printf_arglist (arglist))
4039 return 0;
4041 format_arg = TREE_VALUE (arglist);
4042 stripped_string = format_arg;
4043 STRIP_NOPS (stripped_string);
4044 if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
4045 stripped_string = TREE_OPERAND (stripped_string, 0);
4047 /* If the format specifier isn't a STRING_CST, punt. */
4048 if (TREE_CODE (stripped_string) != STRING_CST)
4049 return 0;
4051 /* OK! We can attempt optimization. */
4053 /* If the format specifier was "%s\n", call __builtin_puts(arg2). */
4054 if (strcmp (TREE_STRING_POINTER (stripped_string), "%s\n") == 0)
4056 arglist = TREE_CHAIN (arglist);
4057 fn = fn_puts;
4059 /* If the format specifier was "%c", call __builtin_putchar (arg2). */
4060 else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0)
4062 arglist = TREE_CHAIN (arglist);
4063 fn = fn_putchar;
4065 else
4067 /* We can't handle anything else with % args or %% ... yet. */
4068 if (strchr (TREE_STRING_POINTER (stripped_string), '%'))
4069 return 0;
4071 /* If the resulting constant string has a length of 1, call
4072 putchar. Note, TREE_STRING_LENGTH includes the terminating
4073 NULL in its count. */
4074 if (TREE_STRING_LENGTH (stripped_string) == 2)
4076 /* Given printf("c"), (where c is any one character,)
4077 convert "c"[0] to an int and pass that to the replacement
4078 function. */
4079 arglist = build_int_2 (TREE_STRING_POINTER (stripped_string)[0], 0);
4080 arglist = build_tree_list (NULL_TREE, arglist);
4082 fn = fn_putchar;
4084 /* If the resulting constant was "string\n", call
4085 __builtin_puts("string"). Ensure "string" has at least one
4086 character besides the trailing \n. Note, TREE_STRING_LENGTH
4087 includes the terminating NULL in its count. */
4088 else if (TREE_STRING_LENGTH (stripped_string) > 2
4089 && TREE_STRING_POINTER (stripped_string)
4090 [TREE_STRING_LENGTH (stripped_string) - 2] == '\n')
4092 /* Create a NULL-terminated string that's one char shorter
4093 than the original, stripping off the trailing '\n'. */
4094 const int newlen = TREE_STRING_LENGTH (stripped_string) - 1;
4095 char *newstr = (char *) alloca (newlen);
4096 memcpy (newstr, TREE_STRING_POINTER (stripped_string), newlen - 1);
4097 newstr[newlen - 1] = 0;
4099 arglist = fix_string_type (build_string (newlen, newstr));
4100 arglist = build_tree_list (NULL_TREE, arglist);
4101 fn = fn_puts;
4103 else
4104 /* We'd like to arrange to call fputs(string) here, but we
4105 need stdout and don't have a way to get it ... yet. */
4106 return 0;
4109 return expand_expr (build_function_call (fn, arglist),
4110 (ignore ? const0_rtx : target),
4111 tmode, modifier);
4114 /* If the arguments passed to fprintf are suitable for optimizations,
4115 we attempt to transform the call. */
4116 static rtx
4117 c_expand_builtin_fprintf (arglist, target, tmode, modifier, ignore, unlocked)
4118 tree arglist;
4119 rtx target;
4120 enum machine_mode tmode;
4121 enum expand_modifier modifier;
4122 int ignore;
4123 int unlocked;
4125 tree fn_fputc = unlocked ?
4126 built_in_decls[BUILT_IN_FPUTC_UNLOCKED] : built_in_decls[BUILT_IN_FPUTC];
4127 tree fn_fputs = unlocked ?
4128 built_in_decls[BUILT_IN_FPUTS_UNLOCKED] : built_in_decls[BUILT_IN_FPUTS];
4129 tree fn, format_arg, stripped_string;
4131 /* If the return value is used, or the replacement _DECL isn't
4132 initialized, don't do the transformation. */
4133 if (!ignore || !fn_fputc || !fn_fputs)
4134 return 0;
4136 /* Verify the required arguments in the original call. */
4137 if (arglist == 0
4138 || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE)
4139 || (TREE_CHAIN (arglist) == 0)
4140 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist)))) !=
4141 POINTER_TYPE))
4142 return 0;
4144 /* Check the specifier vs. the parameters. */
4145 if (!is_valid_printf_arglist (TREE_CHAIN (arglist)))
4146 return 0;
4148 format_arg = TREE_VALUE (TREE_CHAIN (arglist));
4149 stripped_string = format_arg;
4150 STRIP_NOPS (stripped_string);
4151 if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
4152 stripped_string = TREE_OPERAND (stripped_string, 0);
4154 /* If the format specifier isn't a STRING_CST, punt. */
4155 if (TREE_CODE (stripped_string) != STRING_CST)
4156 return 0;
4158 /* OK! We can attempt optimization. */
4160 /* If the format specifier was "%s", call __builtin_fputs(arg3, arg1). */
4161 if (strcmp (TREE_STRING_POINTER (stripped_string), "%s") == 0)
4163 tree newarglist = build_tree_list (NULL_TREE, TREE_VALUE (arglist));
4164 arglist = tree_cons (NULL_TREE,
4165 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))),
4166 newarglist);
4167 fn = fn_fputs;
4169 /* If the format specifier was "%c", call __builtin_fputc (arg3, arg1). */
4170 else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0)
4172 tree newarglist = build_tree_list (NULL_TREE, TREE_VALUE (arglist));
4173 arglist = tree_cons (NULL_TREE,
4174 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))),
4175 newarglist);
4176 fn = fn_fputc;
4178 else
4180 /* We can't handle anything else with % args or %% ... yet. */
4181 if (strchr (TREE_STRING_POINTER (stripped_string), '%'))
4182 return 0;
4184 /* When "string" doesn't contain %, replace all cases of
4185 fprintf(stream,string) with fputs(string,stream). The fputs
4186 builtin will take take of special cases like length==1. */
4187 arglist = tree_cons (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)),
4188 build_tree_list (NULL_TREE, TREE_VALUE (arglist)));
4189 fn = fn_fputs;
4192 return expand_expr (build_function_call (fn, arglist),
4193 (ignore ? const0_rtx : target),
4194 tmode, modifier);
4198 /* Given a boolean expression ARG, return a tree representing an increment
4199 or decrement (as indicated by CODE) of ARG. The front end must check for
4200 invalid cases (e.g., decrement in C++). */
4201 tree
4202 boolean_increment (code, arg)
4203 enum tree_code code;
4204 tree arg;
4206 tree val;
4207 tree true_res = (c_language == clk_cplusplus
4208 ? boolean_true_node
4209 : c_bool_true_node);
4210 arg = stabilize_reference (arg);
4211 switch (code)
4213 case PREINCREMENT_EXPR:
4214 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4215 break;
4216 case POSTINCREMENT_EXPR:
4217 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4218 arg = save_expr (arg);
4219 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4220 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4221 break;
4222 case PREDECREMENT_EXPR:
4223 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
4224 break;
4225 case POSTDECREMENT_EXPR:
4226 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
4227 arg = save_expr (arg);
4228 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4229 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4230 break;
4231 default:
4232 abort ();
4234 TREE_SIDE_EFFECTS (val) = 1;
4235 return val;
4238 /* Handle C and C++ default attributes. */
4240 enum built_in_attribute
4242 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4243 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
4244 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4245 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
4246 #define DEF_FN_ATTR(NAME, ATTRS, PREDICATE) /* No entry needed in enum. */
4247 #include "builtin-attrs.def"
4248 #undef DEF_ATTR_NULL_TREE
4249 #undef DEF_ATTR_INT
4250 #undef DEF_ATTR_IDENT
4251 #undef DEF_ATTR_TREE_LIST
4252 #undef DEF_FN_ATTR
4253 ATTR_LAST
4256 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4258 static bool c_attrs_initialized = false;
4260 static void c_init_attributes PARAMS ((void));
4262 /* Common initialization before parsing options. */
4263 void
4264 c_common_init_options (lang)
4265 enum c_language_kind lang;
4267 c_language = lang;
4268 parse_in = cpp_create_reader (lang == clk_c || lang == clk_objective_c
4269 ? CLK_GNUC89 : CLK_GNUCXX);
4270 if (lang == clk_objective_c)
4271 cpp_get_options (parse_in)->objc = 1;
4273 /* Mark as "unspecified" (see c_common_post_options). */
4274 flag_bounds_check = -1;
4277 /* Post-switch processing. */
4278 void
4279 c_common_post_options ()
4281 cpp_post_options (parse_in);
4283 flag_inline_trees = 1;
4285 /* Use tree inlining if possible. Function instrumentation is only
4286 done in the RTL level, so we disable tree inlining. */
4287 if (! flag_instrument_function_entry_exit)
4289 if (!flag_no_inline)
4290 flag_no_inline = 1;
4291 if (flag_inline_functions)
4293 flag_inline_trees = 2;
4294 flag_inline_functions = 0;
4298 /* If still "unspecified", make it match -fbounded-pointers. */
4299 if (flag_bounds_check == -1)
4300 flag_bounds_check = flag_bounded_pointers;
4302 /* Special format checking options don't work without -Wformat; warn if
4303 they are used. */
4304 if (warn_format_y2k && !warn_format)
4305 warning ("-Wformat-y2k ignored without -Wformat");
4306 if (warn_format_extra_args && !warn_format)
4307 warning ("-Wformat-extra-args ignored without -Wformat");
4308 if (warn_format_zero_length && !warn_format)
4309 warning ("-Wformat-zero-length ignored without -Wformat");
4310 if (warn_format_nonliteral && !warn_format)
4311 warning ("-Wformat-nonliteral ignored without -Wformat");
4312 if (warn_format_security && !warn_format)
4313 warning ("-Wformat-security ignored without -Wformat");
4314 if (warn_missing_format_attribute && !warn_format)
4315 warning ("-Wmissing-format-attribute ignored without -Wformat");
4317 /* If an error has occurred in cpplib, note it so we fail
4318 immediately. */
4319 errorcount += cpp_errors (parse_in);
4322 /* Hook that registers front end and target-specific built-ins. */
4323 static void
4324 cb_register_builtins (pfile)
4325 cpp_reader *pfile;
4327 /* -undef turns off target-specific built-ins. */
4328 if (flag_undef)
4329 return;
4331 if (c_language == clk_cplusplus)
4333 if (SUPPORTS_ONE_ONLY)
4334 cpp_define (pfile, "__GXX_WEAK__=1");
4335 else
4336 cpp_define (pfile, "__GXX_WEAK__=0");
4339 /* libgcc needs to know this. */
4340 if (USING_SJLJ_EXCEPTIONS)
4341 cpp_define (pfile, "__USING_SJLJ_EXCEPTIONS__");
4343 /* stddef.h needs to know these. */
4344 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
4345 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
4346 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
4347 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
4349 /* For use in assembly language. */
4350 builtin_define_with_value ("__REGISTER_PREFIX__", REGISTER_PREFIX, 0);
4351 builtin_define_with_value ("__USER_LABEL_PREFIX__", user_label_prefix, 0);
4353 /* Misc. */
4354 builtin_define_with_value ("__VERSION__", version_string, 1);
4356 /* Other target-independent built-ins determined by command-line
4357 options. */
4358 if (optimize_size)
4359 cpp_define (pfile, "__OPTIMIZE_SIZE__");
4360 if (optimize)
4361 cpp_define (pfile, "__OPTIMIZE__");
4363 if (flag_hosted)
4364 cpp_define (pfile, "__STDC_HOSTED__=1");
4365 else
4366 cpp_define (pfile, "__STDC_HOSTED__=0");
4368 if (fast_math_flags_set_p ())
4369 cpp_define (pfile, "__FAST_MATH__");
4370 if (flag_no_inline)
4371 cpp_define (pfile, "__NO_INLINE__");
4373 if (flag_iso)
4374 cpp_define (pfile, "__STRICT_ANSI__");
4376 if (!flag_signed_char)
4377 cpp_define (pfile, "__CHAR_UNSIGNED__");
4379 /* A straightforward target hook doesn't work, because of problems
4380 linking that hook's body when part of non-C front ends. */
4381 # define preprocessing_asm_p() (cpp_get_options (pfile)->lang == CLK_ASM)
4382 # define builtin_define(TXT) cpp_define (pfile, TXT)
4383 # define builtin_assert(TXT) cpp_assert (pfile, TXT)
4384 TARGET_CPU_CPP_BUILTINS ();
4385 TARGET_OS_CPP_BUILTINS ();
4388 /* Pass an object-like macro. If it doesn't lie in the user's
4389 namespace, defines it unconditionally. Otherwise define a version
4390 with two leading underscores, and another version with two leading
4391 and trailing underscores, and define the original only if an ISO
4392 standard was not nominated.
4394 e.g. passing "unix" defines "__unix", "__unix__" and possibly
4395 "unix". Passing "_mips" defines "__mips", "__mips__" and possibly
4396 "_mips". */
4397 void
4398 builtin_define_std (macro)
4399 const char *macro;
4401 size_t len = strlen (macro);
4402 char *buff = alloca (len + 5);
4403 char *p = buff + 2;
4404 char *q = p + len;
4406 /* prepend __ (or maybe just _) if in user's namespace. */
4407 memcpy (p, macro, len + 1);
4408 if (!( *p == '_' && (p[1] == '_' || ISUPPER (p[1]))))
4410 if (*p != '_')
4411 *--p = '_';
4412 if (p[1] != '_')
4413 *--p = '_';
4415 cpp_define (parse_in, p);
4417 /* If it was in user's namespace... */
4418 if (p != buff + 2)
4420 /* Define the macro with leading and following __. */
4421 if (q[-1] != '_')
4422 *q++ = '_';
4423 if (q[-2] != '_')
4424 *q++ = '_';
4425 *q = '\0';
4426 cpp_define (parse_in, p);
4428 /* Finally, define the original macro if permitted. */
4429 if (!flag_iso)
4430 cpp_define (parse_in, macro);
4434 /* Pass an object-like macro and a value to define it to. The third
4435 parameter says whether or not to turn the value into a string
4436 constant. */
4437 static void
4438 builtin_define_with_value (macro, expansion, is_str)
4439 const char *macro;
4440 const char *expansion;
4441 int is_str;
4443 char *buf;
4444 size_t mlen = strlen (macro);
4445 size_t elen = strlen (expansion);
4446 size_t extra = 2; /* space for an = and a NUL */
4448 if (is_str)
4449 extra += 2; /* space for two quote marks */
4451 buf = alloca (mlen + elen + extra);
4452 if (is_str)
4453 sprintf (buf, "%s=\"%s\"", macro, expansion);
4454 else
4455 sprintf (buf, "%s=%s", macro, expansion);
4457 cpp_define (parse_in, buf);
4460 /* Front end initialization common to C, ObjC and C++. */
4461 const char *
4462 c_common_init (filename)
4463 const char *filename;
4465 cpp_options *options = cpp_get_options (parse_in);
4467 /* Set up preprocessor arithmetic. Must be done after call to
4468 c_common_nodes_and_builtins for wchar_type_node to be good. */
4469 options->precision = TYPE_PRECISION (intmax_type_node);
4470 options->char_precision = TYPE_PRECISION (char_type_node);
4471 options->int_precision = TYPE_PRECISION (integer_type_node);
4472 options->wchar_precision = TYPE_PRECISION (wchar_type_node);
4473 options->unsigned_wchar = TREE_UNSIGNED (wchar_type_node);
4474 options->unsigned_char = !flag_signed_char;
4475 options->warn_multichar = warn_multichar;
4476 options->stdc_0_in_system_headers = STDC_0_IN_SYSTEM_HEADERS;
4478 /* We want -Wno-long-long to override -pedantic -std=non-c99
4479 whatever the ordering. */
4480 options->warn_long_long = warn_long_long && !flag_isoc99 && pedantic;
4482 /* Register preprocessor built-ins before calls to
4483 cpp_main_file. */
4484 cpp_get_callbacks (parse_in)->register_builtins = cb_register_builtins;
4486 /* NULL is passed up to toplev.c and we exit quickly. */
4487 if (flag_preprocess_only)
4489 cpp_preprocess_file (parse_in);
4490 return NULL;
4493 /* Do this before initializing pragmas, as then cpplib's hash table
4494 has been set up. */
4495 filename = init_c_lex (filename);
4497 init_pragma ();
4499 if (!c_attrs_initialized)
4500 c_init_attributes ();
4502 return filename;
4505 /* Common finish hook for the C, ObjC and C++ front ends. */
4506 void
4507 c_common_finish ()
4509 cpp_finish (parse_in);
4511 /* For performance, avoid tearing down cpplib's internal structures.
4512 Call cpp_errors () instead of cpp_destroy (). */
4513 errorcount += cpp_errors (parse_in);
4516 static void
4517 c_init_attributes ()
4519 /* Fill in the built_in_attributes array. */
4520 #define DEF_ATTR_NULL_TREE(ENUM) \
4521 built_in_attributes[(int) ENUM] = NULL_TREE;
4522 #define DEF_ATTR_INT(ENUM, VALUE) \
4523 built_in_attributes[(int) ENUM] = build_int_2 (VALUE, VALUE < 0 ? -1 : 0);
4524 #define DEF_ATTR_IDENT(ENUM, STRING) \
4525 built_in_attributes[(int) ENUM] = get_identifier (STRING);
4526 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
4527 built_in_attributes[(int) ENUM] \
4528 = tree_cons (built_in_attributes[(int) PURPOSE], \
4529 built_in_attributes[(int) VALUE], \
4530 built_in_attributes[(int) CHAIN]);
4531 #define DEF_FN_ATTR(NAME, ATTRS, PREDICATE) /* No initialization needed. */
4532 #include "builtin-attrs.def"
4533 #undef DEF_ATTR_NULL_TREE
4534 #undef DEF_ATTR_INT
4535 #undef DEF_ATTR_IDENT
4536 #undef DEF_ATTR_TREE_LIST
4537 #undef DEF_FN_ATTR
4538 c_attrs_initialized = true;
4541 /* Depending on the name of DECL, apply default attributes to it. */
4543 void
4544 c_common_insert_default_attributes (decl)
4545 tree decl;
4547 tree name = DECL_NAME (decl);
4549 if (!c_attrs_initialized)
4550 c_init_attributes ();
4552 #define DEF_ATTR_NULL_TREE(ENUM) /* Nothing needed after initialization. */
4553 #define DEF_ATTR_INT(ENUM, VALUE)
4554 #define DEF_ATTR_IDENT(ENUM, STRING)
4555 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN)
4556 #define DEF_FN_ATTR(NAME, ATTRS, PREDICATE) \
4557 if ((PREDICATE) && name == built_in_attributes[(int) NAME]) \
4558 decl_attributes (&decl, built_in_attributes[(int) ATTRS], \
4559 ATTR_FLAG_BUILT_IN);
4560 #include "builtin-attrs.def"
4561 #undef DEF_ATTR_NULL_TREE
4562 #undef DEF_ATTR_INT
4563 #undef DEF_ATTR_IDENT
4564 #undef DEF_ATTR_TREE_LIST
4565 #undef DEF_FN_ATTR
4568 /* Output a -Wshadow warning MSGID about NAME, an IDENTIFIER_NODE, and
4569 additionally give the location of the previous declaration DECL. */
4570 void
4571 shadow_warning (msgid, name, decl)
4572 const char *msgid;
4573 tree name, decl;
4575 warning ("declaration of `%s' shadows %s", IDENTIFIER_POINTER (name), msgid);
4576 warning_with_file_and_line (DECL_SOURCE_FILE (decl),
4577 DECL_SOURCE_LINE (decl),
4578 "shadowed declaration is here");
4581 /* Attribute handlers common to C front ends. */
4583 /* Handle a "packed" attribute; arguments as in
4584 struct attribute_spec.handler. */
4586 static tree
4587 handle_packed_attribute (node, name, args, flags, no_add_attrs)
4588 tree *node;
4589 tree name;
4590 tree args ATTRIBUTE_UNUSED;
4591 int flags;
4592 bool *no_add_attrs;
4594 tree *type = NULL;
4595 if (DECL_P (*node))
4597 if (TREE_CODE (*node) == TYPE_DECL)
4598 type = &TREE_TYPE (*node);
4600 else
4601 type = node;
4603 if (type)
4605 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
4606 *type = build_type_copy (*type);
4607 TYPE_PACKED (*type) = 1;
4609 else if (TREE_CODE (*node) == FIELD_DECL)
4610 DECL_PACKED (*node) = 1;
4611 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
4612 used for DECL_REGISTER. It wouldn't mean anything anyway. */
4613 else
4615 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4616 *no_add_attrs = true;
4619 return NULL_TREE;
4622 /* Handle a "nocommon" attribute; arguments as in
4623 struct attribute_spec.handler. */
4625 static tree
4626 handle_nocommon_attribute (node, name, args, flags, no_add_attrs)
4627 tree *node;
4628 tree name;
4629 tree args ATTRIBUTE_UNUSED;
4630 int flags ATTRIBUTE_UNUSED;
4631 bool *no_add_attrs;
4633 if (TREE_CODE (*node) == VAR_DECL)
4634 DECL_COMMON (*node) = 0;
4635 else
4637 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4638 *no_add_attrs = true;
4641 return NULL_TREE;
4644 /* Handle a "common" attribute; arguments as in
4645 struct attribute_spec.handler. */
4647 static tree
4648 handle_common_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;
4655 if (TREE_CODE (*node) == VAR_DECL)
4656 DECL_COMMON (*node) = 1;
4657 else
4659 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4660 *no_add_attrs = true;
4663 return NULL_TREE;
4666 /* Handle a "noreturn" attribute; arguments as in
4667 struct attribute_spec.handler. */
4669 static tree
4670 handle_noreturn_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;
4677 tree type = TREE_TYPE (*node);
4679 /* See FIXME comment in c_common_attribute_table. */
4680 if (TREE_CODE (*node) == FUNCTION_DECL)
4681 TREE_THIS_VOLATILE (*node) = 1;
4682 else if (TREE_CODE (type) == POINTER_TYPE
4683 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
4684 TREE_TYPE (*node)
4685 = build_pointer_type
4686 (build_type_variant (TREE_TYPE (type),
4687 TREE_READONLY (TREE_TYPE (type)), 1));
4688 else
4690 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4691 *no_add_attrs = true;
4694 return NULL_TREE;
4697 /* Handle a "noinline" attribute; arguments as in
4698 struct attribute_spec.handler. */
4700 static tree
4701 handle_noinline_attribute (node, name, args, flags, no_add_attrs)
4702 tree *node;
4703 tree name;
4704 tree args ATTRIBUTE_UNUSED;
4705 int flags ATTRIBUTE_UNUSED;
4706 bool *no_add_attrs;
4708 if (TREE_CODE (*node) == FUNCTION_DECL)
4709 DECL_UNINLINABLE (*node) = 1;
4710 else
4712 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4713 *no_add_attrs = true;
4716 return NULL_TREE;
4719 /* Handle a "always_inline" attribute; arguments as in
4720 struct attribute_spec.handler. */
4722 static tree
4723 handle_always_inline_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;
4730 if (TREE_CODE (*node) == FUNCTION_DECL)
4732 /* Do nothing else, just set the attribute. We'll get at
4733 it later with lookup_attribute. */
4735 else
4737 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4738 *no_add_attrs = true;
4741 return NULL_TREE;
4744 /* Handle a "used" attribute; arguments as in
4745 struct attribute_spec.handler. */
4747 static tree
4748 handle_used_attribute (node, name, args, flags, no_add_attrs)
4749 tree *node;
4750 tree name;
4751 tree args ATTRIBUTE_UNUSED;
4752 int flags ATTRIBUTE_UNUSED;
4753 bool *no_add_attrs;
4755 if (TREE_CODE (*node) == FUNCTION_DECL)
4756 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (*node))
4757 = TREE_USED (*node) = 1;
4758 else
4760 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4761 *no_add_attrs = true;
4764 return NULL_TREE;
4767 /* Handle a "unused" attribute; arguments as in
4768 struct attribute_spec.handler. */
4770 static tree
4771 handle_unused_attribute (node, name, args, flags, no_add_attrs)
4772 tree *node;
4773 tree name;
4774 tree args ATTRIBUTE_UNUSED;
4775 int flags;
4776 bool *no_add_attrs;
4778 if (DECL_P (*node))
4780 tree decl = *node;
4782 if (TREE_CODE (decl) == PARM_DECL
4783 || TREE_CODE (decl) == VAR_DECL
4784 || TREE_CODE (decl) == FUNCTION_DECL
4785 || TREE_CODE (decl) == LABEL_DECL
4786 || TREE_CODE (decl) == TYPE_DECL)
4787 TREE_USED (decl) = 1;
4788 else
4790 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4791 *no_add_attrs = true;
4794 else
4796 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
4797 *node = build_type_copy (*node);
4798 TREE_USED (*node) = 1;
4801 return NULL_TREE;
4804 /* Handle a "const" attribute; arguments as in
4805 struct attribute_spec.handler. */
4807 static tree
4808 handle_const_attribute (node, name, args, flags, no_add_attrs)
4809 tree *node;
4810 tree name;
4811 tree args ATTRIBUTE_UNUSED;
4812 int flags ATTRIBUTE_UNUSED;
4813 bool *no_add_attrs;
4815 tree type = TREE_TYPE (*node);
4817 /* See FIXME comment on noreturn in c_common_attribute_table. */
4818 if (TREE_CODE (*node) == FUNCTION_DECL)
4819 TREE_READONLY (*node) = 1;
4820 else if (TREE_CODE (type) == POINTER_TYPE
4821 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
4822 TREE_TYPE (*node)
4823 = build_pointer_type
4824 (build_type_variant (TREE_TYPE (type), 1,
4825 TREE_THIS_VOLATILE (TREE_TYPE (type))));
4826 else
4828 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4829 *no_add_attrs = true;
4832 return NULL_TREE;
4835 /* Handle a "transparent_union" attribute; arguments as in
4836 struct attribute_spec.handler. */
4838 static tree
4839 handle_transparent_union_attribute (node, name, args, flags, no_add_attrs)
4840 tree *node;
4841 tree name;
4842 tree args ATTRIBUTE_UNUSED;
4843 int flags;
4844 bool *no_add_attrs;
4846 tree decl = NULL_TREE;
4847 tree *type = NULL;
4848 int is_type = 0;
4850 if (DECL_P (*node))
4852 decl = *node;
4853 type = &TREE_TYPE (decl);
4854 is_type = TREE_CODE (*node) == TYPE_DECL;
4856 else if (TYPE_P (*node))
4857 type = node, is_type = 1;
4859 if (is_type
4860 && TREE_CODE (*type) == UNION_TYPE
4861 && (decl == 0
4862 || (TYPE_FIELDS (*type) != 0
4863 && TYPE_MODE (*type) == DECL_MODE (TYPE_FIELDS (*type)))))
4865 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
4866 *type = build_type_copy (*type);
4867 TYPE_TRANSPARENT_UNION (*type) = 1;
4869 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
4870 && TREE_CODE (*type) == UNION_TYPE
4871 && TYPE_MODE (*type) == DECL_MODE (TYPE_FIELDS (*type)))
4872 DECL_TRANSPARENT_UNION (decl) = 1;
4873 else
4875 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4876 *no_add_attrs = true;
4879 return NULL_TREE;
4882 /* Handle a "constructor" attribute; arguments as in
4883 struct attribute_spec.handler. */
4885 static tree
4886 handle_constructor_attribute (node, name, args, flags, no_add_attrs)
4887 tree *node;
4888 tree name;
4889 tree args ATTRIBUTE_UNUSED;
4890 int flags ATTRIBUTE_UNUSED;
4891 bool *no_add_attrs;
4893 tree decl = *node;
4894 tree type = TREE_TYPE (decl);
4896 if (TREE_CODE (decl) == FUNCTION_DECL
4897 && TREE_CODE (type) == FUNCTION_TYPE
4898 && decl_function_context (decl) == 0)
4900 DECL_STATIC_CONSTRUCTOR (decl) = 1;
4901 TREE_USED (decl) = 1;
4903 else
4905 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4906 *no_add_attrs = true;
4909 return NULL_TREE;
4912 /* Handle a "destructor" attribute; arguments as in
4913 struct attribute_spec.handler. */
4915 static tree
4916 handle_destructor_attribute (node, name, args, flags, no_add_attrs)
4917 tree *node;
4918 tree name;
4919 tree args ATTRIBUTE_UNUSED;
4920 int flags ATTRIBUTE_UNUSED;
4921 bool *no_add_attrs;
4923 tree decl = *node;
4924 tree type = TREE_TYPE (decl);
4926 if (TREE_CODE (decl) == FUNCTION_DECL
4927 && TREE_CODE (type) == FUNCTION_TYPE
4928 && decl_function_context (decl) == 0)
4930 DECL_STATIC_DESTRUCTOR (decl) = 1;
4931 TREE_USED (decl) = 1;
4933 else
4935 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4936 *no_add_attrs = true;
4939 return NULL_TREE;
4942 /* Handle a "mode" attribute; arguments as in
4943 struct attribute_spec.handler. */
4945 static tree
4946 handle_mode_attribute (node, name, args, flags, no_add_attrs)
4947 tree *node;
4948 tree name;
4949 tree args;
4950 int flags ATTRIBUTE_UNUSED;
4951 bool *no_add_attrs;
4953 tree type = *node;
4955 *no_add_attrs = true;
4957 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
4958 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
4959 else
4961 int j;
4962 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
4963 int len = strlen (p);
4964 enum machine_mode mode = VOIDmode;
4965 tree typefm;
4967 if (len > 4 && p[0] == '_' && p[1] == '_'
4968 && p[len - 1] == '_' && p[len - 2] == '_')
4970 char *newp = (char *) alloca (len - 1);
4972 strcpy (newp, &p[2]);
4973 newp[len - 4] = '\0';
4974 p = newp;
4977 /* Change this type to have a type with the specified mode.
4978 First check for the special modes. */
4979 if (! strcmp (p, "byte"))
4980 mode = byte_mode;
4981 else if (!strcmp (p, "word"))
4982 mode = word_mode;
4983 else if (! strcmp (p, "pointer"))
4984 mode = ptr_mode;
4985 else
4986 for (j = 0; j < NUM_MACHINE_MODES; j++)
4987 if (!strcmp (p, GET_MODE_NAME (j)))
4988 mode = (enum machine_mode) j;
4990 if (mode == VOIDmode)
4991 error ("unknown machine mode `%s'", p);
4992 else if (0 == (typefm = (*lang_hooks.types.type_for_mode)
4993 (mode, TREE_UNSIGNED (type))))
4994 error ("no data type for mode `%s'", p);
4995 else
4996 *node = typefm;
4997 /* No need to layout the type here. The caller should do this. */
5000 return NULL_TREE;
5003 /* Handle a "section" attribute; arguments as in
5004 struct attribute_spec.handler. */
5006 static tree
5007 handle_section_attribute (node, name, args, flags, no_add_attrs)
5008 tree *node;
5009 tree name ATTRIBUTE_UNUSED;
5010 tree args;
5011 int flags ATTRIBUTE_UNUSED;
5012 bool *no_add_attrs;
5014 tree decl = *node;
5016 if (targetm.have_named_sections)
5018 if ((TREE_CODE (decl) == FUNCTION_DECL
5019 || TREE_CODE (decl) == VAR_DECL)
5020 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
5022 if (TREE_CODE (decl) == VAR_DECL
5023 && current_function_decl != NULL_TREE
5024 && ! TREE_STATIC (decl))
5026 error_with_decl (decl,
5027 "section attribute cannot be specified for local variables");
5028 *no_add_attrs = true;
5031 /* The decl may have already been given a section attribute
5032 from a previous declaration. Ensure they match. */
5033 else if (DECL_SECTION_NAME (decl) != NULL_TREE
5034 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
5035 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
5037 error_with_decl (*node,
5038 "section of `%s' conflicts with previous declaration");
5039 *no_add_attrs = true;
5041 else
5042 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
5044 else
5046 error_with_decl (*node,
5047 "section attribute not allowed for `%s'");
5048 *no_add_attrs = true;
5051 else
5053 error_with_decl (*node,
5054 "section attributes are not supported for this target");
5055 *no_add_attrs = true;
5058 return NULL_TREE;
5061 /* Handle a "aligned" attribute; arguments as in
5062 struct attribute_spec.handler. */
5064 static tree
5065 handle_aligned_attribute (node, name, args, flags, no_add_attrs)
5066 tree *node;
5067 tree name ATTRIBUTE_UNUSED;
5068 tree args;
5069 int flags;
5070 bool *no_add_attrs;
5072 tree decl = NULL_TREE;
5073 tree *type = NULL;
5074 int is_type = 0;
5075 tree align_expr = (args ? TREE_VALUE (args)
5076 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
5077 int i;
5079 if (DECL_P (*node))
5081 decl = *node;
5082 type = &TREE_TYPE (decl);
5083 is_type = TREE_CODE (*node) == TYPE_DECL;
5085 else if (TYPE_P (*node))
5086 type = node, is_type = 1;
5088 /* Strip any NOPs of any kind. */
5089 while (TREE_CODE (align_expr) == NOP_EXPR
5090 || TREE_CODE (align_expr) == CONVERT_EXPR
5091 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
5092 align_expr = TREE_OPERAND (align_expr, 0);
5094 if (TREE_CODE (align_expr) != INTEGER_CST)
5096 error ("requested alignment is not a constant");
5097 *no_add_attrs = true;
5099 else if ((i = tree_log2 (align_expr)) == -1)
5101 error ("requested alignment is not a power of 2");
5102 *no_add_attrs = true;
5104 else if (i > HOST_BITS_PER_INT - 2)
5106 error ("requested alignment is too large");
5107 *no_add_attrs = true;
5109 else if (is_type)
5111 /* If we have a TYPE_DECL, then copy the type, so that we
5112 don't accidentally modify a builtin type. See pushdecl. */
5113 if (decl && TREE_TYPE (decl) != error_mark_node
5114 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
5116 tree tt = TREE_TYPE (decl);
5117 *type = build_type_copy (*type);
5118 DECL_ORIGINAL_TYPE (decl) = tt;
5119 TYPE_NAME (*type) = decl;
5120 TREE_USED (*type) = TREE_USED (decl);
5121 TREE_TYPE (decl) = *type;
5123 else if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
5124 *type = build_type_copy (*type);
5126 TYPE_ALIGN (*type) = (1 << i) * BITS_PER_UNIT;
5127 TYPE_USER_ALIGN (*type) = 1;
5129 else if (TREE_CODE (decl) != VAR_DECL
5130 && TREE_CODE (decl) != FIELD_DECL)
5132 error_with_decl (decl,
5133 "alignment may not be specified for `%s'");
5134 *no_add_attrs = true;
5136 else
5138 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
5139 DECL_USER_ALIGN (decl) = 1;
5142 return NULL_TREE;
5145 /* Handle a "weak" attribute; arguments as in
5146 struct attribute_spec.handler. */
5148 static tree
5149 handle_weak_attribute (node, name, args, flags, no_add_attrs)
5150 tree *node;
5151 tree name ATTRIBUTE_UNUSED;
5152 tree args ATTRIBUTE_UNUSED;
5153 int flags ATTRIBUTE_UNUSED;
5154 bool *no_add_attrs ATTRIBUTE_UNUSED;
5156 declare_weak (*node);
5158 return NULL_TREE;
5161 /* Handle an "alias" attribute; arguments as in
5162 struct attribute_spec.handler. */
5164 static tree
5165 handle_alias_attribute (node, name, args, flags, no_add_attrs)
5166 tree *node;
5167 tree name;
5168 tree args;
5169 int flags ATTRIBUTE_UNUSED;
5170 bool *no_add_attrs;
5172 tree decl = *node;
5174 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
5175 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
5177 error_with_decl (decl,
5178 "`%s' defined both normally and as an alias");
5179 *no_add_attrs = true;
5181 else if (decl_function_context (decl) == 0)
5183 tree id;
5185 id = TREE_VALUE (args);
5186 if (TREE_CODE (id) != STRING_CST)
5188 error ("alias arg not a string");
5189 *no_add_attrs = true;
5190 return NULL_TREE;
5192 id = get_identifier (TREE_STRING_POINTER (id));
5193 /* This counts as a use of the object pointed to. */
5194 TREE_USED (id) = 1;
5196 if (TREE_CODE (decl) == FUNCTION_DECL)
5197 DECL_INITIAL (decl) = error_mark_node;
5198 else
5199 DECL_EXTERNAL (decl) = 0;
5201 else
5203 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5204 *no_add_attrs = true;
5207 return NULL_TREE;
5210 /* Handle an "visibility" attribute; arguments as in
5211 struct attribute_spec.handler. */
5213 static tree
5214 handle_visibility_attribute (node, name, args, flags, no_add_attrs)
5215 tree *node;
5216 tree name;
5217 tree args;
5218 int flags ATTRIBUTE_UNUSED;
5219 bool *no_add_attrs;
5221 tree decl = *node;
5223 if (decl_function_context (decl) != 0 || ! TREE_PUBLIC (decl))
5225 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5226 *no_add_attrs = true;
5228 else
5230 tree id;
5232 id = TREE_VALUE (args);
5233 if (TREE_CODE (id) != STRING_CST)
5235 error ("visibility arg not a string");
5236 *no_add_attrs = true;
5237 return NULL_TREE;
5239 if (strcmp (TREE_STRING_POINTER (id), "hidden")
5240 && strcmp (TREE_STRING_POINTER (id), "protected")
5241 && strcmp (TREE_STRING_POINTER (id), "internal"))
5243 error ("visibility arg must be one of \"hidden\", \"protected\" or \"internal\"");
5244 *no_add_attrs = true;
5245 return NULL_TREE;
5249 return NULL_TREE;
5252 /* Handle a "no_instrument_function" attribute; arguments as in
5253 struct attribute_spec.handler. */
5255 static tree
5256 handle_no_instrument_function_attribute (node, name, args, flags, no_add_attrs)
5257 tree *node;
5258 tree name;
5259 tree args ATTRIBUTE_UNUSED;
5260 int flags ATTRIBUTE_UNUSED;
5261 bool *no_add_attrs;
5263 tree decl = *node;
5265 if (TREE_CODE (decl) != FUNCTION_DECL)
5267 error_with_decl (decl,
5268 "`%s' attribute applies only to functions",
5269 IDENTIFIER_POINTER (name));
5270 *no_add_attrs = true;
5272 else if (DECL_INITIAL (decl))
5274 error_with_decl (decl,
5275 "can't set `%s' attribute after definition",
5276 IDENTIFIER_POINTER (name));
5277 *no_add_attrs = true;
5279 else
5280 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
5282 return NULL_TREE;
5285 /* Handle a "malloc" attribute; arguments as in
5286 struct attribute_spec.handler. */
5288 static tree
5289 handle_malloc_attribute (node, name, args, flags, no_add_attrs)
5290 tree *node;
5291 tree name;
5292 tree args ATTRIBUTE_UNUSED;
5293 int flags ATTRIBUTE_UNUSED;
5294 bool *no_add_attrs;
5296 if (TREE_CODE (*node) == FUNCTION_DECL)
5297 DECL_IS_MALLOC (*node) = 1;
5298 /* ??? TODO: Support types. */
5299 else
5301 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5302 *no_add_attrs = true;
5305 return NULL_TREE;
5308 /* Handle a "no_limit_stack" attribute; arguments as in
5309 struct attribute_spec.handler. */
5311 static tree
5312 handle_no_limit_stack_attribute (node, name, args, flags, no_add_attrs)
5313 tree *node;
5314 tree name;
5315 tree args ATTRIBUTE_UNUSED;
5316 int flags ATTRIBUTE_UNUSED;
5317 bool *no_add_attrs;
5319 tree decl = *node;
5321 if (TREE_CODE (decl) != FUNCTION_DECL)
5323 error_with_decl (decl,
5324 "`%s' attribute applies only to functions",
5325 IDENTIFIER_POINTER (name));
5326 *no_add_attrs = true;
5328 else if (DECL_INITIAL (decl))
5330 error_with_decl (decl,
5331 "can't set `%s' attribute after definition",
5332 IDENTIFIER_POINTER (name));
5333 *no_add_attrs = true;
5335 else
5336 DECL_NO_LIMIT_STACK (decl) = 1;
5338 return NULL_TREE;
5341 /* Handle a "pure" attribute; arguments as in
5342 struct attribute_spec.handler. */
5344 static tree
5345 handle_pure_attribute (node, name, args, flags, no_add_attrs)
5346 tree *node;
5347 tree name;
5348 tree args ATTRIBUTE_UNUSED;
5349 int flags ATTRIBUTE_UNUSED;
5350 bool *no_add_attrs;
5352 if (TREE_CODE (*node) == FUNCTION_DECL)
5353 DECL_IS_PURE (*node) = 1;
5354 /* ??? TODO: Support types. */
5355 else
5357 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5358 *no_add_attrs = true;
5361 return NULL_TREE;
5364 /* Handle a "deprecated" attribute; arguments as in
5365 struct attribute_spec.handler. */
5367 static tree
5368 handle_deprecated_attribute (node, name, args, flags, no_add_attrs)
5369 tree *node;
5370 tree name;
5371 tree args ATTRIBUTE_UNUSED;
5372 int flags;
5373 bool *no_add_attrs;
5375 tree type = NULL_TREE;
5376 int warn = 0;
5377 const char *what = NULL;
5379 if (DECL_P (*node))
5381 tree decl = *node;
5382 type = TREE_TYPE (decl);
5384 if (TREE_CODE (decl) == TYPE_DECL
5385 || TREE_CODE (decl) == PARM_DECL
5386 || TREE_CODE (decl) == VAR_DECL
5387 || TREE_CODE (decl) == FUNCTION_DECL
5388 || TREE_CODE (decl) == FIELD_DECL)
5389 TREE_DEPRECATED (decl) = 1;
5390 else
5391 warn = 1;
5393 else if (TYPE_P (*node))
5395 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
5396 *node = build_type_copy (*node);
5397 TREE_DEPRECATED (*node) = 1;
5398 type = *node;
5400 else
5401 warn = 1;
5403 if (warn)
5405 *no_add_attrs = true;
5406 if (type && TYPE_NAME (type))
5408 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
5409 what = IDENTIFIER_POINTER (TYPE_NAME (*node));
5410 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
5411 && DECL_NAME (TYPE_NAME (type)))
5412 what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
5414 if (what)
5415 warning ("`%s' attribute ignored for `%s'",
5416 IDENTIFIER_POINTER (name), what);
5417 else
5418 warning ("`%s' attribute ignored",
5419 IDENTIFIER_POINTER (name));
5422 return NULL_TREE;
5425 /* Keep a list of vector type nodes we created in handle_vector_size_attribute,
5426 to prevent us from duplicating type nodes unnecessarily.
5427 The normal mechanism to prevent duplicates is to use type_hash_canon, but
5428 since we want to distinguish types that are essentially identical (except
5429 for their debug representation), we use a local list here. */
5430 static tree vector_type_node_list = 0;
5432 /* Handle a "vector_size" attribute; arguments as in
5433 struct attribute_spec.handler. */
5435 static tree
5436 handle_vector_size_attribute (node, name, args, flags, no_add_attrs)
5437 tree *node;
5438 tree name;
5439 tree args;
5440 int flags ATTRIBUTE_UNUSED;
5441 bool *no_add_attrs;
5443 unsigned HOST_WIDE_INT vecsize, nunits;
5444 enum machine_mode mode, orig_mode, new_mode;
5445 tree type = *node, new_type = NULL_TREE;
5446 tree type_list_node;
5448 *no_add_attrs = true;
5450 if (! host_integerp (TREE_VALUE (args), 1))
5452 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5453 return NULL_TREE;
5456 /* Get the vector size (in bytes). */
5457 vecsize = tree_low_cst (TREE_VALUE (args), 1);
5459 /* We need to provide for vector pointers, vector arrays, and
5460 functions returning vectors. For example:
5462 __attribute__((vector_size(16))) short *foo;
5464 In this case, the mode is SI, but the type being modified is
5465 HI, so we need to look further. */
5467 while (POINTER_TYPE_P (type)
5468 || TREE_CODE (type) == FUNCTION_TYPE
5469 || TREE_CODE (type) == ARRAY_TYPE)
5470 type = TREE_TYPE (type);
5472 /* Get the mode of the type being modified. */
5473 orig_mode = TYPE_MODE (type);
5475 if (TREE_CODE (type) == RECORD_TYPE
5476 || (GET_MODE_CLASS (orig_mode) != MODE_FLOAT
5477 && GET_MODE_CLASS (orig_mode) != MODE_INT)
5478 || ! host_integerp (TYPE_SIZE_UNIT (type), 1))
5480 error ("invalid vector type for attribute `%s'",
5481 IDENTIFIER_POINTER (name));
5482 return NULL_TREE;
5485 /* Calculate how many units fit in the vector. */
5486 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
5488 /* Find a suitably sized vector. */
5489 new_mode = VOIDmode;
5490 for (mode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_mode) == MODE_INT
5491 ? MODE_VECTOR_INT
5492 : MODE_VECTOR_FLOAT);
5493 mode != VOIDmode;
5494 mode = GET_MODE_WIDER_MODE (mode))
5495 if (vecsize == GET_MODE_SIZE (mode)
5496 && nunits == (unsigned HOST_WIDE_INT) GET_MODE_NUNITS (mode))
5498 new_mode = mode;
5499 break;
5502 if (new_mode == VOIDmode)
5504 error ("no vector mode with the size and type specified could be found");
5505 return NULL_TREE;
5508 for (type_list_node = vector_type_node_list; type_list_node;
5509 type_list_node = TREE_CHAIN (type_list_node))
5511 tree other_type = TREE_VALUE (type_list_node);
5512 tree record = TYPE_DEBUG_REPRESENTATION_TYPE (other_type);
5513 tree fields = TYPE_FIELDS (record);
5514 tree field_type = TREE_TYPE (fields);
5515 tree array_type = TREE_TYPE (field_type);
5516 if (TREE_CODE (fields) != FIELD_DECL
5517 || TREE_CODE (field_type) != ARRAY_TYPE)
5518 abort ();
5520 if (TYPE_MODE (other_type) == mode && type == array_type)
5522 new_type = other_type;
5523 break;
5527 if (new_type == NULL_TREE)
5529 tree index, array, rt, list_node;
5531 new_type = (*lang_hooks.types.type_for_mode) (new_mode,
5532 TREE_UNSIGNED (type));
5534 if (!new_type)
5536 error ("no vector mode with the size and type specified could be found");
5537 return NULL_TREE;
5540 new_type = build_type_copy (new_type);
5542 /* Set the debug information here, because this is the only
5543 place where we know the underlying type for a vector made
5544 with vector_size. For debugging purposes we pretend a vector
5545 is an array within a structure. */
5546 index = build_int_2 (TYPE_VECTOR_SUBPARTS (new_type) - 1, 0);
5547 array = build_array_type (type, build_index_type (index));
5548 rt = make_node (RECORD_TYPE);
5550 TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
5551 DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
5552 layout_type (rt);
5553 TYPE_DEBUG_REPRESENTATION_TYPE (new_type) = rt;
5555 list_node = build_tree_list (NULL, new_type);
5556 TREE_CHAIN (list_node) = vector_type_node_list;
5557 vector_type_node_list = list_node;
5560 /* Build back pointers if needed. */
5561 *node = vector_size_helper (*node, new_type);
5563 return NULL_TREE;
5566 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
5567 better way.
5569 If we requested a pointer to a vector, build up the pointers that
5570 we stripped off while looking for the inner type. Similarly for
5571 return values from functions.
5573 The argument "type" is the top of the chain, and "bottom" is the
5574 new type which we will point to. */
5576 static tree
5577 vector_size_helper (type, bottom)
5578 tree type, bottom;
5580 tree inner, outer;
5582 if (POINTER_TYPE_P (type))
5584 inner = vector_size_helper (TREE_TYPE (type), bottom);
5585 outer = build_pointer_type (inner);
5587 else if (TREE_CODE (type) == ARRAY_TYPE)
5589 inner = vector_size_helper (TREE_TYPE (type), bottom);
5590 outer = build_array_type (inner, TYPE_VALUES (type));
5592 else if (TREE_CODE (type) == FUNCTION_TYPE)
5594 inner = vector_size_helper (TREE_TYPE (type), bottom);
5595 outer = build_function_type (inner, TYPE_VALUES (type));
5597 else
5598 return bottom;
5600 TREE_READONLY (outer) = TREE_READONLY (type);
5601 TREE_THIS_VOLATILE (outer) = TREE_THIS_VOLATILE (type);
5603 return outer;
5606 /* Handle the "nonnull" attribute. */
5607 static tree
5608 handle_nonnull_attribute (node, name, args, flags, no_add_attrs)
5609 tree *node;
5610 tree name ATTRIBUTE_UNUSED;
5611 tree args;
5612 int flags ATTRIBUTE_UNUSED;
5613 bool *no_add_attrs;
5615 tree type = *node;
5616 unsigned HOST_WIDE_INT attr_arg_num;
5618 /* If no arguments are specified, all pointer arguments should be
5619 non-null. Veryify a full prototype is given so that the arguments
5620 will have the correct types when we actually check them later. */
5621 if (! args)
5623 if (! TYPE_ARG_TYPES (type))
5625 error ("nonnull attribute without arguments on a non-prototype");
5626 *no_add_attrs = true;
5628 return NULL_TREE;
5631 /* Argument list specified. Verify that each argument number references
5632 a pointer argument. */
5633 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
5635 tree argument;
5636 unsigned HOST_WIDE_INT arg_num, ck_num;
5638 if (! get_nonnull_operand (TREE_VALUE (args), &arg_num))
5640 error ("nonnull argument has invalid operand number (arg %lu)",
5641 (unsigned long) attr_arg_num);
5642 *no_add_attrs = true;
5643 return NULL_TREE;
5646 argument = TYPE_ARG_TYPES (type);
5647 if (argument)
5649 for (ck_num = 1; ; ck_num++)
5651 if (! argument || ck_num == arg_num)
5652 break;
5653 argument = TREE_CHAIN (argument);
5656 if (! argument
5657 || TREE_CODE (TREE_VALUE (argument)) == VOID_TYPE)
5659 error ("nonnull argument with out-of-range operand number (arg %lu, operand %lu)",
5660 (unsigned long) attr_arg_num, (unsigned long) arg_num);
5661 *no_add_attrs = true;
5662 return NULL_TREE;
5665 if (TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE)
5667 error ("nonnull argument references non-pointer operand (arg %lu, operand %lu)",
5668 (unsigned long) attr_arg_num, (unsigned long) arg_num);
5669 *no_add_attrs = true;
5670 return NULL_TREE;
5675 return NULL_TREE;
5678 /* Check the argument list of a function call for null in argument slots
5679 that are marked as requiring a non-null pointer argument. */
5681 static void
5682 check_function_nonnull (attrs, params)
5683 tree attrs;
5684 tree params;
5686 tree a, args, param;
5687 int param_num;
5689 for (a = attrs; a; a = TREE_CHAIN (a))
5691 if (is_attribute_p ("nonnull", TREE_PURPOSE (a)))
5693 args = TREE_VALUE (a);
5695 /* Walk the argument list. If we encounter an argument number we
5696 should check for non-null, do it. If the attribute has no args,
5697 then every pointer argument is checked (in which case the check
5698 for pointer type is done in check_nonnull_arg). */
5699 for (param = params, param_num = 1; ;
5700 param_num++, param = TREE_CHAIN (param))
5702 if (! param)
5703 break;
5704 if (! args || nonnull_check_p (args, param_num))
5705 check_function_arguments_recurse (check_nonnull_arg, NULL,
5706 TREE_VALUE (param),
5707 param_num);
5713 /* Helper for check_function_nonnull; given a list of operands which
5714 must be non-null in ARGS, determine if operand PARAM_NUM should be
5715 checked. */
5717 static bool
5718 nonnull_check_p (args, param_num)
5719 tree args;
5720 unsigned HOST_WIDE_INT param_num;
5722 unsigned HOST_WIDE_INT arg_num;
5724 for (; args; args = TREE_CHAIN (args))
5726 if (! get_nonnull_operand (TREE_VALUE (args), &arg_num))
5727 abort ();
5729 if (arg_num == param_num)
5730 return true;
5732 return false;
5735 /* Check that the function argument PARAM (which is operand number
5736 PARAM_NUM) is non-null. This is called by check_function_nonnull
5737 via check_function_arguments_recurse. */
5739 static void
5740 check_nonnull_arg (ctx, param, param_num)
5741 void *ctx ATTRIBUTE_UNUSED;
5742 tree param;
5743 unsigned HOST_WIDE_INT param_num;
5745 /* Just skip checking the argument if it's not a pointer. This can
5746 happen if the "nonnull" attribute was given without an operand
5747 list (which means to check every pointer argument). */
5749 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
5750 return;
5752 if (integer_zerop (param))
5753 warning ("null argument where non-null required (arg %lu)",
5754 (unsigned long) param_num);
5757 /* Helper for nonnull attribute handling; fetch the operand number
5758 from the attribute argument list. */
5760 static bool
5761 get_nonnull_operand (arg_num_expr, valp)
5762 tree arg_num_expr;
5763 unsigned HOST_WIDE_INT *valp;
5765 /* Strip any conversions from the arg number and verify they
5766 are constants. */
5767 while (TREE_CODE (arg_num_expr) == NOP_EXPR
5768 || TREE_CODE (arg_num_expr) == CONVERT_EXPR
5769 || TREE_CODE (arg_num_expr) == NON_LVALUE_EXPR)
5770 arg_num_expr = TREE_OPERAND (arg_num_expr, 0);
5772 if (TREE_CODE (arg_num_expr) != INTEGER_CST
5773 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
5774 return false;
5776 *valp = TREE_INT_CST_LOW (arg_num_expr);
5777 return true;
5780 /* Handle a "nothrow" attribute; arguments as in
5781 struct attribute_spec.handler. */
5783 static tree
5784 handle_nothrow_attribute (node, name, args, flags, no_add_attrs)
5785 tree *node;
5786 tree name;
5787 tree args ATTRIBUTE_UNUSED;
5788 int flags ATTRIBUTE_UNUSED;
5789 bool *no_add_attrs;
5791 if (TREE_CODE (*node) == FUNCTION_DECL)
5792 TREE_NOTHROW (*node) = 1;
5793 /* ??? TODO: Support types. */
5794 else
5796 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5797 *no_add_attrs = true;
5800 return NULL_TREE;
5803 /* Check for valid arguments being passed to a function. */
5804 void
5805 check_function_arguments (attrs, params)
5806 tree attrs;
5807 tree params;
5809 /* Check for null being passed in a pointer argument that must be
5810 non-null. We also need to do this if format checking is enabled. */
5812 if (warn_nonnull)
5813 check_function_nonnull (attrs, params);
5815 /* Check for errors in format strings. */
5817 if (warn_format)
5818 check_function_format (NULL, attrs, params);
5821 /* Generic argument checking recursion routine. PARAM is the argument to
5822 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
5823 once the argument is resolved. CTX is context for the callback. */
5824 void
5825 check_function_arguments_recurse (callback, ctx, param, param_num)
5826 void (*callback) PARAMS ((void *, tree, unsigned HOST_WIDE_INT));
5827 void *ctx;
5828 tree param;
5829 unsigned HOST_WIDE_INT param_num;
5831 if (TREE_CODE (param) == NOP_EXPR)
5833 /* Strip coercion. */
5834 check_function_arguments_recurse (callback, ctx,
5835 TREE_OPERAND (param, 0), param_num);
5836 return;
5839 if (TREE_CODE (param) == CALL_EXPR)
5841 tree type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (param, 0)));
5842 tree attrs;
5843 bool found_format_arg = false;
5845 /* See if this is a call to a known internationalization function
5846 that modifies a format arg. Such a function may have multiple
5847 format_arg attributes (for example, ngettext). */
5849 for (attrs = TYPE_ATTRIBUTES (type);
5850 attrs;
5851 attrs = TREE_CHAIN (attrs))
5852 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
5854 tree inner_args;
5855 tree format_num_expr;
5856 int format_num;
5857 int i;
5859 /* Extract the argument number, which was previously checked
5860 to be valid. */
5861 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
5862 while (TREE_CODE (format_num_expr) == NOP_EXPR
5863 || TREE_CODE (format_num_expr) == CONVERT_EXPR
5864 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
5865 format_num_expr = TREE_OPERAND (format_num_expr, 0);
5867 if (TREE_CODE (format_num_expr) != INTEGER_CST
5868 || TREE_INT_CST_HIGH (format_num_expr) != 0)
5869 abort ();
5871 format_num = TREE_INT_CST_LOW (format_num_expr);
5873 for (inner_args = TREE_OPERAND (param, 1), i = 1;
5874 inner_args != 0;
5875 inner_args = TREE_CHAIN (inner_args), i++)
5876 if (i == format_num)
5878 check_function_arguments_recurse (callback, ctx,
5879 TREE_VALUE (inner_args),
5880 param_num);
5881 found_format_arg = true;
5882 break;
5886 /* If we found a format_arg attribute and did a recursive check,
5887 we are done with checking this argument. Otherwise, we continue
5888 and this will be considered a non-literal. */
5889 if (found_format_arg)
5890 return;
5893 if (TREE_CODE (param) == COND_EXPR)
5895 /* Check both halves of the conditional expression. */
5896 check_function_arguments_recurse (callback, ctx,
5897 TREE_OPERAND (param, 1), param_num);
5898 check_function_arguments_recurse (callback, ctx,
5899 TREE_OPERAND (param, 2), param_num);
5900 return;
5903 (*callback) (ctx, param, param_num);
5906 #include "gt-c-common.h"