Add x prefix to v850e case for handling --with-cpu=v850e.
[official-gcc.git] / gcc / c-common.c
blob960e4cfe75bd89c3edda63edb416fabbde766c67
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 "diagnostic.h"
35 #include "tm_p.h"
36 #include "obstack.h"
37 #include "cpplib.h"
38 #include "target.h"
39 #include "langhooks.h"
40 #include "except.h" /* For USING_SJLJ_EXCEPTIONS. */
41 #include "tree-inline.h"
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 REGISTER_PREFIX
88 #define REGISTER_PREFIX ""
89 #endif
91 /* The variant of the C language being processed. */
93 enum c_language_kind c_language;
95 /* The following symbols are subsumed in the c_global_trees array, and
96 listed here individually for documentation purposes.
98 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
100 tree short_integer_type_node;
101 tree long_integer_type_node;
102 tree long_long_integer_type_node;
104 tree short_unsigned_type_node;
105 tree long_unsigned_type_node;
106 tree long_long_unsigned_type_node;
108 tree boolean_type_node;
109 tree boolean_false_node;
110 tree boolean_true_node;
112 tree ptrdiff_type_node;
114 tree unsigned_char_type_node;
115 tree signed_char_type_node;
116 tree wchar_type_node;
117 tree signed_wchar_type_node;
118 tree unsigned_wchar_type_node;
120 tree float_type_node;
121 tree double_type_node;
122 tree long_double_type_node;
124 tree complex_integer_type_node;
125 tree complex_float_type_node;
126 tree complex_double_type_node;
127 tree complex_long_double_type_node;
129 tree intQI_type_node;
130 tree intHI_type_node;
131 tree intSI_type_node;
132 tree intDI_type_node;
133 tree intTI_type_node;
135 tree unsigned_intQI_type_node;
136 tree unsigned_intHI_type_node;
137 tree unsigned_intSI_type_node;
138 tree unsigned_intDI_type_node;
139 tree unsigned_intTI_type_node;
141 tree widest_integer_literal_type_node;
142 tree widest_unsigned_literal_type_node;
144 Nodes for types `void *' and `const void *'.
146 tree ptr_type_node, const_ptr_type_node;
148 Nodes for types `char *' and `const char *'.
150 tree string_type_node, const_string_type_node;
152 Type `char[SOMENUMBER]'.
153 Used when an array of char is needed and the size is irrelevant.
155 tree char_array_type_node;
157 Type `int[SOMENUMBER]' or something like it.
158 Used when an array of int needed and the size is irrelevant.
160 tree int_array_type_node;
162 Type `wchar_t[SOMENUMBER]' or something like it.
163 Used when a wide string literal is created.
165 tree wchar_array_type_node;
167 Type `int ()' -- used for implicit declaration of functions.
169 tree default_function_type;
171 A VOID_TYPE node, packaged in a TREE_LIST.
173 tree void_list_node;
175 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
176 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
177 VAR_DECLS, but C++ does.)
179 tree function_name_decl_node;
180 tree pretty_function_name_decl_node;
181 tree c99_function_name_decl_node;
183 Stack of nested function name VAR_DECLs.
185 tree saved_function_name_decls;
189 tree c_global_trees[CTI_MAX];
191 /* Switches common to the C front ends. */
193 /* Nonzero if prepreprocessing only. */
194 int flag_preprocess_only;
196 /* Nonzero if an ISO standard was selected. It rejects macros in the
197 user's namespace. */
198 int flag_iso;
200 /* Nonzero whenever Objective-C functionality is being used. */
201 int flag_objc;
203 /* Nonzero if -undef was given. It suppresses target built-in macros
204 and assertions. */
205 int flag_undef;
207 /* Nonzero means don't recognize the non-ANSI builtin functions. */
209 int flag_no_builtin;
211 /* Nonzero means don't recognize the non-ANSI builtin functions.
212 -ansi sets this. */
214 int flag_no_nonansi_builtin;
216 /* Nonzero means give `double' the same size as `float'. */
218 int flag_short_double;
220 /* Nonzero means give `wchar_t' the same size as `short'. */
222 int flag_short_wchar;
224 /* Nonzero means allow Microsoft extensions without warnings or errors. */
225 int flag_ms_extensions;
227 /* Nonzero means don't recognize the keyword `asm'. */
229 int flag_no_asm;
231 /* Nonzero means give string constants the type `const char *', as mandated
232 by the standard. */
234 int flag_const_strings;
236 /* Nonzero means `$' can be in an identifier. */
238 #ifndef DOLLARS_IN_IDENTIFIERS
239 #define DOLLARS_IN_IDENTIFIERS 1
240 #endif
241 int dollars_in_ident = DOLLARS_IN_IDENTIFIERS;
243 /* Nonzero means to treat bitfields as signed unless they say `unsigned'. */
245 int flag_signed_bitfields = 1;
246 int explicit_flag_signed_bitfields;
248 /* Nonzero means warn about pointer casts that can drop a type qualifier
249 from the pointer target type. */
251 int warn_cast_qual;
253 /* Warn about functions which might be candidates for format attributes. */
255 int warn_missing_format_attribute;
257 /* Nonzero means warn about sizeof(function) or addition/subtraction
258 of function pointers. */
260 int warn_pointer_arith;
262 /* Nonzero means warn for any global function def
263 without separate previous prototype decl. */
265 int warn_missing_prototypes;
267 /* Warn if adding () is suggested. */
269 int warn_parentheses;
271 /* Warn if initializer is not completely bracketed. */
273 int warn_missing_braces;
275 /* Warn about comparison of signed and unsigned values.
276 If -1, neither -Wsign-compare nor -Wno-sign-compare has been specified. */
278 int warn_sign_compare;
280 /* Nonzero means warn about usage of long long when `-pedantic'. */
282 int warn_long_long = 1;
284 /* Nonzero means warn about deprecated conversion from string constant to
285 `char *'. */
287 int warn_write_strings;
289 /* Nonzero means warn about multiple (redundant) decls for the same single
290 variable or function. */
292 int warn_redundant_decls;
294 /* Warn about testing equality of floating point numbers. */
296 int warn_float_equal;
298 /* Warn about a subscript that has type char. */
300 int warn_char_subscripts;
302 /* Warn if a type conversion is done that might have confusing results. */
304 int warn_conversion;
306 /* Warn about #pragma directives that are not recognized. */
308 int warn_unknown_pragmas; /* Tri state variable. */
310 /* Warn about format/argument anomalies in calls to formatted I/O functions
311 (*printf, *scanf, strftime, strfmon, etc.). */
313 int warn_format;
315 /* Warn about Y2K problems with strftime formats. */
317 int warn_format_y2k;
319 /* Warn about excess arguments to formats. */
321 int warn_format_extra_args;
323 /* Warn about zero-length formats. */
325 int warn_format_zero_length;
327 /* Warn about non-literal format arguments. */
329 int warn_format_nonliteral;
331 /* Warn about possible security problems with calls to format functions. */
333 int warn_format_security;
336 /* C/ObjC language option variables. */
339 /* Nonzero means message about use of implicit function declarations;
340 1 means warning; 2 means error. */
342 int mesg_implicit_function_declaration = -1;
344 /* Nonzero means allow type mismatches in conditional expressions;
345 just make their values `void'. */
347 int flag_cond_mismatch;
349 /* Nonzero means enable C89 Amendment 1 features. */
351 int flag_isoc94;
353 /* Nonzero means use the ISO C99 dialect of C. */
355 int flag_isoc99;
357 /* Nonzero means that we have builtin functions, and main is an int */
359 int flag_hosted = 1;
361 /* Nonzero means add default format_arg attributes for functions not
362 in ISO C. */
364 int flag_noniso_default_format_attributes = 1;
366 /* Nonzero means warn when casting a function call to a type that does
367 not match the return type (e.g. (float)sqrt() or (anything*)malloc()
368 when there is no previous declaration of sqrt or malloc. */
370 int warn_bad_function_cast;
372 /* Warn about traditional constructs whose meanings changed in ANSI C. */
374 int warn_traditional;
376 /* Nonzero means warn for non-prototype function decls
377 or non-prototyped defs without previous prototype. */
379 int warn_strict_prototypes;
381 /* Nonzero means warn for any global function def
382 without separate previous decl. */
384 int warn_missing_declarations;
386 /* Nonzero means warn about declarations of objects not at
387 file-scope level and about *all* declarations of functions (whether
388 or static) not at file-scope level. Note that we exclude
389 implicit function declarations. To get warnings about those, use
390 -Wimplicit. */
392 int warn_nested_externs;
394 /* Warn if main is suspicious. */
396 int warn_main;
398 /* Nonzero means warn about possible violations of sequence point rules. */
400 int warn_sequence_point;
402 /* Nonzero means to warn about compile-time division by zero. */
403 int warn_div_by_zero = 1;
405 /* Nonzero means warn about use of implicit int. */
407 int warn_implicit_int;
409 /* Warn about NULL being passed to argument slots marked as requiring
410 non-NULL. */
412 int warn_nonnull;
415 /* ObjC language option variables. */
418 /* Open and close the file for outputting class declarations, if
419 requested (ObjC). */
421 int flag_gen_declaration;
423 /* Generate code for GNU or NeXT runtime environment. */
425 #ifdef NEXT_OBJC_RUNTIME
426 int flag_next_runtime = 1;
427 #else
428 int flag_next_runtime = 0;
429 #endif
431 /* Tells the compiler that this is a special run. Do not perform any
432 compiling, instead we are to test some platform dependent features
433 and output a C header file with appropriate definitions. */
435 int print_struct_values;
437 /* ???. Undocumented. */
439 const char *constant_string_class_name;
441 /* Warn if multiple methods are seen for the same selector, but with
442 different argument types. Performs the check on the whole selector
443 table at the end of compilation. */
445 int warn_selector;
447 /* Warn if a @selector() is found, and no method with that selector
448 has been previously declared. The check is done on each
449 @selector() as soon as it is found - so it warns about forward
450 declarations. */
452 int warn_undeclared_selector;
454 /* Warn if methods required by a protocol are not implemented in the
455 class adopting it. When turned off, methods inherited to that
456 class are also considered implemented. */
458 int warn_protocol = 1;
461 /* C++ language option variables. */
464 /* Nonzero means don't recognize any extension keywords. */
466 int flag_no_gnu_keywords;
468 /* Nonzero means do emit exported implementations of functions even if
469 they can be inlined. */
471 int flag_implement_inlines = 1;
473 /* Nonzero means do emit exported implementations of templates, instead of
474 multiple static copies in each file that needs a definition. */
476 int flag_external_templates;
478 /* Nonzero means that the decision to emit or not emit the implementation of a
479 template depends on where the template is instantiated, rather than where
480 it is defined. */
482 int flag_alt_external_templates;
484 /* Nonzero means that implicit instantiations will be emitted if needed. */
486 int flag_implicit_templates = 1;
488 /* Nonzero means that implicit instantiations of inline templates will be
489 emitted if needed, even if instantiations of non-inline templates
490 aren't. */
492 int flag_implicit_inline_templates = 1;
494 /* Nonzero means generate separate instantiation control files and
495 juggle them at link time. */
497 int flag_use_repository;
499 /* Nonzero if we want to issue diagnostics that the standard says are not
500 required. */
502 int flag_optional_diags = 1;
504 /* Nonzero means we should attempt to elide constructors when possible. */
506 int flag_elide_constructors = 1;
508 /* Nonzero means that member functions defined in class scope are
509 inline by default. */
511 int flag_default_inline = 1;
513 /* Controls whether compiler generates 'type descriptor' that give
514 run-time type information. */
516 int flag_rtti = 1;
518 /* Nonzero if we want to conserve space in the .o files. We do this
519 by putting uninitialized data and runtime initialized data into
520 .common instead of .data at the expense of not flagging multiple
521 definitions. */
523 int flag_conserve_space;
525 /* Nonzero if we want to obey access control semantics. */
527 int flag_access_control = 1;
529 /* Nonzero if we want to check the return value of new and avoid calling
530 constructors if it is a null pointer. */
532 int flag_check_new;
534 /* Nonzero if we want the new ISO rules for pushing a new scope for `for'
535 initialization variables.
536 0: Old rules, set by -fno-for-scope.
537 2: New ISO rules, set by -ffor-scope.
538 1: Try to implement new ISO rules, but with backup compatibility
539 (and warnings). This is the default, for now. */
541 int flag_new_for_scope = 1;
543 /* Nonzero if we want to emit defined symbols with common-like linkage as
544 weak symbols where possible, in order to conform to C++ semantics.
545 Otherwise, emit them as local symbols. */
547 int flag_weak = 1;
549 /* Nonzero to use __cxa_atexit, rather than atexit, to register
550 destructors for local statics and global objects. */
552 int flag_use_cxa_atexit = DEFAULT_USE_CXA_ATEXIT;
554 /* Nonzero means output .vtable_{entry,inherit} for use in doing vtable gc. */
556 int flag_vtable_gc;
558 /* Nonzero means make the default pedwarns warnings instead of errors.
559 The value of this flag is ignored if -pedantic is specified. */
561 int flag_permissive;
563 /* Nonzero means to implement standard semantics for exception
564 specifications, calling unexpected if an exception is thrown that
565 doesn't match the specification. Zero means to treat them as
566 assertions and optimize accordingly, but not check them. */
568 int flag_enforce_eh_specs = 1;
570 /* The version of the C++ ABI in use. The following values are
571 allowed:
573 0: The version of the ABI believed most conformant with the
574 C++ ABI specification. This ABI may change as bugs are
575 discovered and fixed. Therefore, 0 will not necessarily
576 indicate the same ABI in different versions of G++.
578 1: The version of the ABI first used in G++ 3.2.
580 Additional positive integers will be assigned as new versions of
581 the ABI become the default version of the ABI. */
583 int flag_abi_version = 1;
585 /* Nonzero means warn about things that will change when compiling
586 with an ABI-compliant compiler. */
588 int warn_abi = 0;
590 /* Nonzero means warn about implicit declarations. */
592 int warn_implicit = 1;
594 /* Nonzero means warn when all ctors or dtors are private, and the class
595 has no friends. */
597 int warn_ctor_dtor_privacy = 1;
599 /* Nonzero means warn in function declared in derived class has the
600 same name as a virtual in the base class, but fails to match the
601 type signature of any virtual function in the base class. */
603 int warn_overloaded_virtual;
605 /* Nonzero means warn when declaring a class that has a non virtual
606 destructor, when it really ought to have a virtual one. */
608 int warn_nonvdtor;
610 /* Nonzero means warn when the compiler will reorder code. */
612 int warn_reorder;
614 /* Nonzero means warn when synthesis behavior differs from Cfront's. */
616 int warn_synth;
618 /* Nonzero means warn when we convert a pointer to member function
619 into a pointer to (void or function). */
621 int warn_pmf2ptr = 1;
623 /* Nonzero means warn about violation of some Effective C++ style rules. */
625 int warn_ecpp;
627 /* Nonzero means warn where overload resolution chooses a promotion from
628 unsigned to signed over a conversion to an unsigned of the same size. */
630 int warn_sign_promo;
632 /* Nonzero means warn when an old-style cast is used. */
634 int warn_old_style_cast;
636 /* Nonzero means warn when non-templatized friend functions are
637 declared within a template */
639 int warn_nontemplate_friend = 1;
641 /* Nonzero means complain about deprecated features. */
643 int warn_deprecated = 1;
645 /* Maximum template instantiation depth. This limit is rather
646 arbitrary, but it exists to limit the time it takes to notice
647 infinite template instantiations. */
649 int max_tinst_depth = 500;
653 /* The elements of `ridpointers' are identifier nodes for the reserved
654 type names and storage classes. It is indexed by a RID_... value. */
655 tree *ridpointers;
657 tree (*make_fname_decl) PARAMS ((tree, int));
659 /* If non-NULL, the address of a language-specific function that
660 returns 1 for language-specific statement codes. */
661 int (*lang_statement_code_p) PARAMS ((enum tree_code));
663 /* If non-NULL, the address of a language-specific function that takes
664 any action required right before expand_function_end is called. */
665 void (*lang_expand_function_end) PARAMS ((void));
667 /* Nonzero means the expression being parsed will never be evaluated.
668 This is a count, since unevaluated expressions can nest. */
669 int skip_evaluation;
671 /* Information about how a function name is generated. */
672 struct fname_var_t
674 tree *const decl; /* pointer to the VAR_DECL. */
675 const unsigned rid; /* RID number for the identifier. */
676 const int pretty; /* How pretty is it? */
679 /* The three ways of getting then name of the current function. */
681 const struct fname_var_t fname_vars[] =
683 /* C99 compliant __func__, must be first. */
684 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
685 /* GCC __FUNCTION__ compliant. */
686 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
687 /* GCC __PRETTY_FUNCTION__ compliant. */
688 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
689 {NULL, 0, 0},
692 static int constant_fits_type_p PARAMS ((tree, tree));
694 /* Keep a stack of if statements. We record the number of compound
695 statements seen up to the if keyword, as well as the line number
696 and file of the if. If a potentially ambiguous else is seen, that
697 fact is recorded; the warning is issued when we can be sure that
698 the enclosing if statement does not have an else branch. */
699 typedef struct
701 int compstmt_count;
702 int line;
703 const char *file;
704 int needs_warning;
705 tree if_stmt;
706 } if_elt;
708 static if_elt *if_stack;
710 /* Amount of space in the if statement stack. */
711 static int if_stack_space = 0;
713 /* Stack pointer. */
714 static int if_stack_pointer = 0;
716 static tree handle_packed_attribute PARAMS ((tree *, tree, tree, int,
717 bool *));
718 static tree handle_nocommon_attribute PARAMS ((tree *, tree, tree, int,
719 bool *));
720 static tree handle_common_attribute PARAMS ((tree *, tree, tree, int,
721 bool *));
722 static tree handle_noreturn_attribute PARAMS ((tree *, tree, tree, int,
723 bool *));
724 static tree handle_noinline_attribute PARAMS ((tree *, tree, tree, int,
725 bool *));
726 static tree handle_always_inline_attribute PARAMS ((tree *, tree, tree, int,
727 bool *));
728 static tree handle_used_attribute PARAMS ((tree *, tree, tree, int,
729 bool *));
730 static tree handle_unused_attribute PARAMS ((tree *, tree, tree, int,
731 bool *));
732 static tree handle_const_attribute PARAMS ((tree *, tree, tree, int,
733 bool *));
734 static tree handle_transparent_union_attribute PARAMS ((tree *, tree, tree,
735 int, bool *));
736 static tree handle_constructor_attribute PARAMS ((tree *, tree, tree, int,
737 bool *));
738 static tree handle_destructor_attribute PARAMS ((tree *, tree, tree, int,
739 bool *));
740 static tree handle_mode_attribute PARAMS ((tree *, tree, tree, int,
741 bool *));
742 static tree handle_section_attribute PARAMS ((tree *, tree, tree, int,
743 bool *));
744 static tree handle_aligned_attribute PARAMS ((tree *, tree, tree, int,
745 bool *));
746 static tree handle_weak_attribute PARAMS ((tree *, tree, tree, int,
747 bool *));
748 static tree handle_alias_attribute PARAMS ((tree *, tree, tree, int,
749 bool *));
750 static tree handle_visibility_attribute PARAMS ((tree *, tree, tree, int,
751 bool *));
752 static tree handle_no_instrument_function_attribute PARAMS ((tree *, tree,
753 tree, int,
754 bool *));
755 static tree handle_malloc_attribute PARAMS ((tree *, tree, tree, int,
756 bool *));
757 static tree handle_no_limit_stack_attribute PARAMS ((tree *, tree, tree, int,
758 bool *));
759 static tree handle_pure_attribute PARAMS ((tree *, tree, tree, int,
760 bool *));
761 static tree handle_deprecated_attribute PARAMS ((tree *, tree, tree, int,
762 bool *));
763 static tree handle_vector_size_attribute PARAMS ((tree *, tree, tree, int,
764 bool *));
765 static tree handle_nonnull_attribute PARAMS ((tree *, tree, tree, int,
766 bool *));
767 static tree handle_nothrow_attribute PARAMS ((tree *, tree, tree, int,
768 bool *));
769 static tree vector_size_helper PARAMS ((tree, tree));
771 static void check_function_nonnull PARAMS ((tree, tree));
772 static void check_nonnull_arg PARAMS ((void *, tree,
773 unsigned HOST_WIDE_INT));
774 static bool nonnull_check_p PARAMS ((tree, unsigned HOST_WIDE_INT));
775 static bool get_nonnull_operand PARAMS ((tree,
776 unsigned HOST_WIDE_INT *));
777 void builtin_define_std PARAMS ((const char *));
778 static void builtin_define_with_value PARAMS ((const char *, const char *,
779 int));
780 static void builtin_define_with_int_value PARAMS ((const char *,
781 HOST_WIDE_INT));
782 static void builtin_define_with_hex_fp_value PARAMS ((const char *, tree,
783 int, const char *,
784 const char *));
785 static void builtin_define_type_max PARAMS ((const char *, tree, int));
786 static void builtin_define_type_precision PARAMS ((const char *, tree));
787 static void builtin_define_float_constants PARAMS ((const char *,
788 const char *, tree));
790 /* Table of machine-independent attributes common to all C-like languages. */
791 const struct attribute_spec c_common_attribute_table[] =
793 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
794 { "packed", 0, 0, false, false, false,
795 handle_packed_attribute },
796 { "nocommon", 0, 0, true, false, false,
797 handle_nocommon_attribute },
798 { "common", 0, 0, true, false, false,
799 handle_common_attribute },
800 /* FIXME: logically, noreturn attributes should be listed as
801 "false, true, true" and apply to function types. But implementing this
802 would require all the places in the compiler that use TREE_THIS_VOLATILE
803 on a decl to identify non-returning functions to be located and fixed
804 to check the function type instead. */
805 { "noreturn", 0, 0, true, false, false,
806 handle_noreturn_attribute },
807 { "volatile", 0, 0, true, false, false,
808 handle_noreturn_attribute },
809 { "noinline", 0, 0, true, false, false,
810 handle_noinline_attribute },
811 { "always_inline", 0, 0, true, false, false,
812 handle_always_inline_attribute },
813 { "used", 0, 0, true, false, false,
814 handle_used_attribute },
815 { "unused", 0, 0, false, false, false,
816 handle_unused_attribute },
817 /* The same comments as for noreturn attributes apply to const ones. */
818 { "const", 0, 0, true, false, false,
819 handle_const_attribute },
820 { "transparent_union", 0, 0, false, false, false,
821 handle_transparent_union_attribute },
822 { "constructor", 0, 0, true, false, false,
823 handle_constructor_attribute },
824 { "destructor", 0, 0, true, false, false,
825 handle_destructor_attribute },
826 { "mode", 1, 1, false, true, false,
827 handle_mode_attribute },
828 { "section", 1, 1, true, false, false,
829 handle_section_attribute },
830 { "aligned", 0, 1, false, false, false,
831 handle_aligned_attribute },
832 { "weak", 0, 0, true, false, false,
833 handle_weak_attribute },
834 { "alias", 1, 1, true, false, false,
835 handle_alias_attribute },
836 { "no_instrument_function", 0, 0, true, false, false,
837 handle_no_instrument_function_attribute },
838 { "malloc", 0, 0, true, false, false,
839 handle_malloc_attribute },
840 { "no_stack_limit", 0, 0, true, false, false,
841 handle_no_limit_stack_attribute },
842 { "pure", 0, 0, true, false, false,
843 handle_pure_attribute },
844 { "deprecated", 0, 0, false, false, false,
845 handle_deprecated_attribute },
846 { "vector_size", 1, 1, false, true, false,
847 handle_vector_size_attribute },
848 { "visibility", 1, 1, true, false, false,
849 handle_visibility_attribute },
850 { "nonnull", 0, -1, false, true, true,
851 handle_nonnull_attribute },
852 { "nothrow", 0, 0, true, false, false,
853 handle_nothrow_attribute },
854 { "may_alias", 0, 0, false, true, false, NULL },
855 { NULL, 0, 0, false, false, false, NULL }
858 /* Give the specifications for the format attributes, used by C and all
859 descendents. */
861 const struct attribute_spec c_common_format_attribute_table[] =
863 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
864 { "format", 3, 3, false, true, true,
865 handle_format_attribute },
866 { "format_arg", 1, 1, false, true, true,
867 handle_format_arg_attribute },
868 { NULL, 0, 0, false, false, false, NULL }
871 /* Record the start of an if-then, and record the start of it
872 for ambiguous else detection.
874 COND is the condition for the if-then statement.
876 IF_STMT is the statement node that has already been created for
877 this if-then statement. It is created before parsing the
878 condition to keep line number information accurate. */
880 void
881 c_expand_start_cond (cond, compstmt_count, if_stmt)
882 tree cond;
883 int compstmt_count;
884 tree if_stmt;
886 /* Make sure there is enough space on the stack. */
887 if (if_stack_space == 0)
889 if_stack_space = 10;
890 if_stack = (if_elt *) xmalloc (10 * sizeof (if_elt));
892 else if (if_stack_space == if_stack_pointer)
894 if_stack_space += 10;
895 if_stack = (if_elt *) xrealloc (if_stack, if_stack_space * sizeof (if_elt));
898 IF_COND (if_stmt) = cond;
899 add_stmt (if_stmt);
901 /* Record this if statement. */
902 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
903 if_stack[if_stack_pointer].file = input_filename;
904 if_stack[if_stack_pointer].line = lineno;
905 if_stack[if_stack_pointer].needs_warning = 0;
906 if_stack[if_stack_pointer].if_stmt = if_stmt;
907 if_stack_pointer++;
910 /* Called after the then-clause for an if-statement is processed. */
912 void
913 c_finish_then ()
915 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
916 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
919 /* Record the end of an if-then. Optionally warn if a nested
920 if statement had an ambiguous else clause. */
922 void
923 c_expand_end_cond ()
925 if_stack_pointer--;
926 if (if_stack[if_stack_pointer].needs_warning)
927 warning_with_file_and_line (if_stack[if_stack_pointer].file,
928 if_stack[if_stack_pointer].line,
929 "suggest explicit braces to avoid ambiguous `else'");
930 last_expr_type = NULL_TREE;
933 /* Called between the then-clause and the else-clause
934 of an if-then-else. */
936 void
937 c_expand_start_else ()
939 /* An ambiguous else warning must be generated for the enclosing if
940 statement, unless we see an else branch for that one, too. */
941 if (warn_parentheses
942 && if_stack_pointer > 1
943 && (if_stack[if_stack_pointer - 1].compstmt_count
944 == if_stack[if_stack_pointer - 2].compstmt_count))
945 if_stack[if_stack_pointer - 2].needs_warning = 1;
947 /* Even if a nested if statement had an else branch, it can't be
948 ambiguous if this one also has an else. So don't warn in that
949 case. Also don't warn for any if statements nested in this else. */
950 if_stack[if_stack_pointer - 1].needs_warning = 0;
951 if_stack[if_stack_pointer - 1].compstmt_count--;
954 /* Called after the else-clause for an if-statement is processed. */
956 void
957 c_finish_else ()
959 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
960 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
963 /* Begin an if-statement. Returns a newly created IF_STMT if
964 appropriate.
966 Unlike the C++ front-end, we do not call add_stmt here; it is
967 probably safe to do so, but I am not very familiar with this
968 code so I am being extra careful not to change its behavior
969 beyond what is strictly necessary for correctness. */
971 tree
972 c_begin_if_stmt ()
974 tree r;
975 r = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
976 return r;
979 /* Begin a while statement. Returns a newly created WHILE_STMT if
980 appropriate.
982 Unlike the C++ front-end, we do not call add_stmt here; it is
983 probably safe to do so, but I am not very familiar with this
984 code so I am being extra careful not to change its behavior
985 beyond what is strictly necessary for correctness. */
987 tree
988 c_begin_while_stmt ()
990 tree r;
991 r = build_stmt (WHILE_STMT, NULL_TREE, NULL_TREE);
992 return r;
995 void
996 c_finish_while_stmt_cond (cond, while_stmt)
997 tree while_stmt;
998 tree cond;
1000 WHILE_COND (while_stmt) = cond;
1003 /* Push current bindings for the function name VAR_DECLS. */
1005 void
1006 start_fname_decls ()
1008 unsigned ix;
1009 tree saved = NULL_TREE;
1011 for (ix = 0; fname_vars[ix].decl; ix++)
1013 tree decl = *fname_vars[ix].decl;
1015 if (decl)
1017 saved = tree_cons (decl, build_int_2 (ix, 0), saved);
1018 *fname_vars[ix].decl = NULL_TREE;
1021 if (saved || saved_function_name_decls)
1022 /* Normally they'll have been NULL, so only push if we've got a
1023 stack, or they are non-NULL. */
1024 saved_function_name_decls = tree_cons (saved, NULL_TREE,
1025 saved_function_name_decls);
1028 /* Finish up the current bindings, adding them into the
1029 current function's statement tree. This is done by wrapping the
1030 function's body in a COMPOUND_STMT containing these decls too. This
1031 must be done _before_ finish_stmt_tree is called. If there is no
1032 current function, we must be at file scope and no statements are
1033 involved. Pop the previous bindings. */
1035 void
1036 finish_fname_decls ()
1038 unsigned ix;
1039 tree body = NULL_TREE;
1040 tree stack = saved_function_name_decls;
1042 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
1043 body = chainon (TREE_VALUE (stack), body);
1045 if (body)
1047 /* They were called into existence, so add to statement tree. */
1048 body = chainon (body,
1049 TREE_CHAIN (DECL_SAVED_TREE (current_function_decl)));
1050 body = build_stmt (COMPOUND_STMT, body);
1052 COMPOUND_STMT_NO_SCOPE (body) = 1;
1053 TREE_CHAIN (DECL_SAVED_TREE (current_function_decl)) = body;
1056 for (ix = 0; fname_vars[ix].decl; ix++)
1057 *fname_vars[ix].decl = NULL_TREE;
1059 if (stack)
1061 /* We had saved values, restore them. */
1062 tree saved;
1064 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
1066 tree decl = TREE_PURPOSE (saved);
1067 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
1069 *fname_vars[ix].decl = decl;
1071 stack = TREE_CHAIN (stack);
1073 saved_function_name_decls = stack;
1076 /* Return the text name of the current function, suitable prettified
1077 by PRETTY_P. */
1079 const char *
1080 fname_as_string (pretty_p)
1081 int pretty_p;
1083 const char *name = NULL;
1085 if (pretty_p)
1086 name = (current_function_decl
1087 ? (*lang_hooks.decl_printable_name) (current_function_decl, 2)
1088 : "top level");
1089 else if (current_function_decl && DECL_NAME (current_function_decl))
1090 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
1091 else
1092 name = "";
1093 return name;
1096 /* Return the text name of the current function, formatted as
1097 required by the supplied RID value. */
1099 const char *
1100 fname_string (rid)
1101 unsigned rid;
1103 unsigned ix;
1105 for (ix = 0; fname_vars[ix].decl; ix++)
1106 if (fname_vars[ix].rid == rid)
1107 break;
1108 return fname_as_string (fname_vars[ix].pretty);
1111 /* Return the VAR_DECL for a const char array naming the current
1112 function. If the VAR_DECL has not yet been created, create it
1113 now. RID indicates how it should be formatted and IDENTIFIER_NODE
1114 ID is its name (unfortunately C and C++ hold the RID values of
1115 keywords in different places, so we can't derive RID from ID in
1116 this language independent code. */
1118 tree
1119 fname_decl (rid, id)
1120 unsigned rid;
1121 tree id;
1123 unsigned ix;
1124 tree decl = NULL_TREE;
1126 for (ix = 0; fname_vars[ix].decl; ix++)
1127 if (fname_vars[ix].rid == rid)
1128 break;
1130 decl = *fname_vars[ix].decl;
1131 if (!decl)
1133 tree saved_last_tree = last_tree;
1134 /* If a tree is built here, it would normally have the lineno of
1135 the current statement. Later this tree will be moved to the
1136 beginning of the function and this line number will be wrong.
1137 To avoid this problem set the lineno to 0 here; that prevents
1138 it from appearing in the RTL. */
1139 int saved_lineno = lineno;
1140 lineno = 0;
1142 decl = (*make_fname_decl) (id, fname_vars[ix].pretty);
1143 if (last_tree != saved_last_tree)
1145 /* We created some statement tree for the decl. This belongs
1146 at the start of the function, so remove it now and reinsert
1147 it after the function is complete. */
1148 tree stmts = TREE_CHAIN (saved_last_tree);
1150 TREE_CHAIN (saved_last_tree) = NULL_TREE;
1151 last_tree = saved_last_tree;
1152 saved_function_name_decls = tree_cons (decl, stmts,
1153 saved_function_name_decls);
1155 *fname_vars[ix].decl = decl;
1156 lineno = saved_lineno;
1158 if (!ix && !current_function_decl)
1159 pedwarn_with_decl (decl, "`%s' is not defined outside of function scope");
1161 return decl;
1164 /* Given a STRING_CST, give it a suitable array-of-chars data type. */
1166 tree
1167 fix_string_type (value)
1168 tree value;
1170 const int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
1171 const int wide_flag = TREE_TYPE (value) == wchar_array_type_node;
1172 const int nchars_max = flag_isoc99 ? 4095 : 509;
1173 int length = TREE_STRING_LENGTH (value);
1174 int nchars;
1176 /* Compute the number of elements, for the array type. */
1177 nchars = wide_flag ? length / wchar_bytes : length;
1179 if (pedantic && nchars - 1 > nchars_max && c_language == clk_c)
1180 pedwarn ("string length `%d' is greater than the length `%d' ISO C%d compilers are required to support",
1181 nchars - 1, nchars_max, flag_isoc99 ? 99 : 89);
1183 /* Create the array type for the string constant.
1184 -Wwrite-strings says make the string constant an array of const char
1185 so that copying it to a non-const pointer will get a warning.
1186 For C++, this is the standard behavior. */
1187 if (flag_const_strings && ! flag_writable_strings)
1189 tree elements
1190 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
1191 1, 0);
1192 TREE_TYPE (value)
1193 = build_array_type (elements,
1194 build_index_type (build_int_2 (nchars - 1, 0)));
1196 else
1197 TREE_TYPE (value)
1198 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
1199 build_index_type (build_int_2 (nchars - 1, 0)));
1201 TREE_CONSTANT (value) = 1;
1202 TREE_READONLY (value) = ! flag_writable_strings;
1203 TREE_STATIC (value) = 1;
1204 return value;
1207 /* Given a VARRAY of STRING_CST nodes, concatenate them into one
1208 STRING_CST. */
1210 tree
1211 combine_strings (strings)
1212 varray_type strings;
1214 const int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
1215 const int nstrings = VARRAY_ACTIVE_SIZE (strings);
1216 tree value, t;
1217 int length = 1;
1218 int wide_length = 0;
1219 int wide_flag = 0;
1220 int i;
1221 char *p, *q;
1223 /* Don't include the \0 at the end of each substring. Count wide
1224 strings and ordinary strings separately. */
1225 for (i = 0; i < nstrings; ++i)
1227 t = VARRAY_TREE (strings, i);
1229 if (TREE_TYPE (t) == wchar_array_type_node)
1231 wide_length += TREE_STRING_LENGTH (t) - wchar_bytes;
1232 wide_flag = 1;
1234 else
1236 length += (TREE_STRING_LENGTH (t) - 1);
1237 if (C_ARTIFICIAL_STRING_P (t) && !in_system_header)
1238 warning ("concatenation of string literals with __FUNCTION__ is deprecated");
1242 /* If anything is wide, the non-wides will be converted,
1243 which makes them take more space. */
1244 if (wide_flag)
1245 length = length * wchar_bytes + wide_length;
1247 p = xmalloc (length);
1249 /* Copy the individual strings into the new combined string.
1250 If the combined string is wide, convert the chars to ints
1251 for any individual strings that are not wide. */
1253 q = p;
1254 for (i = 0; i < nstrings; ++i)
1256 int len, this_wide;
1258 t = VARRAY_TREE (strings, i);
1259 this_wide = TREE_TYPE (t) == wchar_array_type_node;
1260 len = TREE_STRING_LENGTH (t) - (this_wide ? wchar_bytes : 1);
1261 if (this_wide == wide_flag)
1263 memcpy (q, TREE_STRING_POINTER (t), len);
1264 q += len;
1266 else
1268 const int nzeros = (TYPE_PRECISION (wchar_type_node)
1269 / BITS_PER_UNIT) - 1;
1270 int j, k;
1272 if (BYTES_BIG_ENDIAN)
1274 for (k = 0; k < len; k++)
1276 for (j = 0; j < nzeros; j++)
1277 *q++ = 0;
1278 *q++ = TREE_STRING_POINTER (t)[k];
1281 else
1283 for (k = 0; k < len; k++)
1285 *q++ = TREE_STRING_POINTER (t)[k];
1286 for (j = 0; j < nzeros; j++)
1287 *q++ = 0;
1293 /* Nul terminate the string. */
1294 if (wide_flag)
1296 for (i = 0; i < wchar_bytes; i++)
1297 *q++ = 0;
1299 else
1300 *q = 0;
1302 value = build_string (length, p);
1303 free (p);
1305 if (wide_flag)
1306 TREE_TYPE (value) = wchar_array_type_node;
1307 else
1308 TREE_TYPE (value) = char_array_type_node;
1310 return value;
1313 static int is_valid_printf_arglist PARAMS ((tree));
1314 static rtx c_expand_builtin PARAMS ((tree, rtx, enum machine_mode, enum expand_modifier));
1315 static rtx c_expand_builtin_printf PARAMS ((tree, rtx, enum machine_mode,
1316 enum expand_modifier, int, int));
1317 static rtx c_expand_builtin_fprintf PARAMS ((tree, rtx, enum machine_mode,
1318 enum expand_modifier, int, int));
1320 /* Print a warning if a constant expression had overflow in folding.
1321 Invoke this function on every expression that the language
1322 requires to be a constant expression.
1323 Note the ANSI C standard says it is erroneous for a
1324 constant expression to overflow. */
1326 void
1327 constant_expression_warning (value)
1328 tree value;
1330 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1331 || TREE_CODE (value) == VECTOR_CST
1332 || TREE_CODE (value) == COMPLEX_CST)
1333 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1334 pedwarn ("overflow in constant expression");
1337 /* Print a warning if an expression had overflow in folding.
1338 Invoke this function on every expression that
1339 (1) appears in the source code, and
1340 (2) might be a constant expression that overflowed, and
1341 (3) is not already checked by convert_and_check;
1342 however, do not invoke this function on operands of explicit casts. */
1344 void
1345 overflow_warning (value)
1346 tree value;
1348 if ((TREE_CODE (value) == INTEGER_CST
1349 || (TREE_CODE (value) == COMPLEX_CST
1350 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1351 && TREE_OVERFLOW (value))
1353 TREE_OVERFLOW (value) = 0;
1354 if (skip_evaluation == 0)
1355 warning ("integer overflow in expression");
1357 else if ((TREE_CODE (value) == REAL_CST
1358 || (TREE_CODE (value) == COMPLEX_CST
1359 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1360 && TREE_OVERFLOW (value))
1362 TREE_OVERFLOW (value) = 0;
1363 if (skip_evaluation == 0)
1364 warning ("floating point overflow in expression");
1366 else if (TREE_CODE (value) == VECTOR_CST && TREE_OVERFLOW (value))
1368 TREE_OVERFLOW (value) = 0;
1369 if (skip_evaluation == 0)
1370 warning ("vector overflow in expression");
1374 /* Print a warning if a large constant is truncated to unsigned,
1375 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1376 Invoke this function on every expression that might be implicitly
1377 converted to an unsigned type. */
1379 void
1380 unsigned_conversion_warning (result, operand)
1381 tree result, operand;
1383 tree type = TREE_TYPE (result);
1385 if (TREE_CODE (operand) == INTEGER_CST
1386 && TREE_CODE (type) == INTEGER_TYPE
1387 && TREE_UNSIGNED (type)
1388 && skip_evaluation == 0
1389 && !int_fits_type_p (operand, type))
1391 if (!int_fits_type_p (operand, c_common_signed_type (type)))
1392 /* This detects cases like converting -129 or 256 to unsigned char. */
1393 warning ("large integer implicitly truncated to unsigned type");
1394 else if (warn_conversion)
1395 warning ("negative integer implicitly converted to unsigned type");
1399 /* Nonzero if constant C has a value that is permissible
1400 for type TYPE (an INTEGER_TYPE). */
1402 static int
1403 constant_fits_type_p (c, type)
1404 tree c, type;
1406 if (TREE_CODE (c) == INTEGER_CST)
1407 return int_fits_type_p (c, type);
1409 c = convert (type, c);
1410 return !TREE_OVERFLOW (c);
1413 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1414 Invoke this function on every expression that is converted implicitly,
1415 i.e. because of language rules and not because of an explicit cast. */
1417 tree
1418 convert_and_check (type, expr)
1419 tree type, expr;
1421 tree t = convert (type, expr);
1422 if (TREE_CODE (t) == INTEGER_CST)
1424 if (TREE_OVERFLOW (t))
1426 TREE_OVERFLOW (t) = 0;
1428 /* Do not diagnose overflow in a constant expression merely
1429 because a conversion overflowed. */
1430 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
1432 /* No warning for converting 0x80000000 to int. */
1433 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
1434 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1435 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
1436 /* If EXPR fits in the unsigned version of TYPE,
1437 don't warn unless pedantic. */
1438 if ((pedantic
1439 || TREE_UNSIGNED (type)
1440 || ! constant_fits_type_p (expr,
1441 c_common_unsigned_type (type)))
1442 && skip_evaluation == 0)
1443 warning ("overflow in implicit constant conversion");
1445 else
1446 unsigned_conversion_warning (t, expr);
1448 return t;
1451 /* A node in a list that describes references to variables (EXPR), which are
1452 either read accesses if WRITER is zero, or write accesses, in which case
1453 WRITER is the parent of EXPR. */
1454 struct tlist
1456 struct tlist *next;
1457 tree expr, writer;
1460 /* Used to implement a cache the results of a call to verify_tree. We only
1461 use this for SAVE_EXPRs. */
1462 struct tlist_cache
1464 struct tlist_cache *next;
1465 struct tlist *cache_before_sp;
1466 struct tlist *cache_after_sp;
1467 tree expr;
1470 /* Obstack to use when allocating tlist structures, and corresponding
1471 firstobj. */
1472 static struct obstack tlist_obstack;
1473 static char *tlist_firstobj = 0;
1475 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
1476 warnings. */
1477 static struct tlist *warned_ids;
1478 /* SAVE_EXPRs need special treatment. We process them only once and then
1479 cache the results. */
1480 static struct tlist_cache *save_expr_cache;
1482 static void add_tlist PARAMS ((struct tlist **, struct tlist *, tree, int));
1483 static void merge_tlist PARAMS ((struct tlist **, struct tlist *, int));
1484 static void verify_tree PARAMS ((tree, struct tlist **, struct tlist **, tree));
1485 static int warning_candidate_p PARAMS ((tree));
1486 static void warn_for_collisions PARAMS ((struct tlist *));
1487 static void warn_for_collisions_1 PARAMS ((tree, tree, struct tlist *, int));
1488 static struct tlist *new_tlist PARAMS ((struct tlist *, tree, tree));
1489 static void verify_sequence_points PARAMS ((tree));
1491 /* Create a new struct tlist and fill in its fields. */
1492 static struct tlist *
1493 new_tlist (next, t, writer)
1494 struct tlist *next;
1495 tree t;
1496 tree writer;
1498 struct tlist *l;
1499 l = (struct tlist *) obstack_alloc (&tlist_obstack, sizeof *l);
1500 l->next = next;
1501 l->expr = t;
1502 l->writer = writer;
1503 return l;
1506 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1507 is nonnull, we ignore any node we find which has a writer equal to it. */
1509 static void
1510 add_tlist (to, add, exclude_writer, copy)
1511 struct tlist **to;
1512 struct tlist *add;
1513 tree exclude_writer;
1514 int copy;
1516 while (add)
1518 struct tlist *next = add->next;
1519 if (! copy)
1520 add->next = *to;
1521 if (! exclude_writer || add->writer != exclude_writer)
1522 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1523 add = next;
1527 /* Merge the nodes of ADD into TO. This merging process is done so that for
1528 each variable that already exists in TO, no new node is added; however if
1529 there is a write access recorded in ADD, and an occurrence on TO is only
1530 a read access, then the occurrence in TO will be modified to record the
1531 write. */
1533 static void
1534 merge_tlist (to, add, copy)
1535 struct tlist **to;
1536 struct tlist *add;
1537 int copy;
1539 struct tlist **end = to;
1541 while (*end)
1542 end = &(*end)->next;
1544 while (add)
1546 int found = 0;
1547 struct tlist *tmp2;
1548 struct tlist *next = add->next;
1550 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1551 if (tmp2->expr == add->expr)
1553 found = 1;
1554 if (! tmp2->writer)
1555 tmp2->writer = add->writer;
1557 if (! found)
1559 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
1560 end = &(*end)->next;
1561 *end = 0;
1563 add = next;
1567 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1568 references in list LIST conflict with it, excluding reads if ONLY writers
1569 is nonzero. */
1571 static void
1572 warn_for_collisions_1 (written, writer, list, only_writes)
1573 tree written, writer;
1574 struct tlist *list;
1575 int only_writes;
1577 struct tlist *tmp;
1579 /* Avoid duplicate warnings. */
1580 for (tmp = warned_ids; tmp; tmp = tmp->next)
1581 if (tmp->expr == written)
1582 return;
1584 while (list)
1586 if (list->expr == written
1587 && list->writer != writer
1588 && (! only_writes || list->writer))
1590 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
1591 warning ("operation on `%s' may be undefined",
1592 IDENTIFIER_POINTER (DECL_NAME (list->expr)));
1594 list = list->next;
1598 /* Given a list LIST of references to variables, find whether any of these
1599 can cause conflicts due to missing sequence points. */
1601 static void
1602 warn_for_collisions (list)
1603 struct tlist *list;
1605 struct tlist *tmp;
1607 for (tmp = list; tmp; tmp = tmp->next)
1609 if (tmp->writer)
1610 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1614 /* Return nonzero if X is a tree that can be verified by the sequence point
1615 warnings. */
1616 static int
1617 warning_candidate_p (x)
1618 tree x;
1620 return TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == PARM_DECL;
1623 /* Walk the tree X, and record accesses to variables. If X is written by the
1624 parent tree, WRITER is the parent.
1625 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
1626 expression or its only operand forces a sequence point, then everything up
1627 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
1628 in PNO_SP.
1629 Once we return, we will have emitted warnings if any subexpression before
1630 such a sequence point could be undefined. On a higher level, however, the
1631 sequence point may not be relevant, and we'll merge the two lists.
1633 Example: (b++, a) + b;
1634 The call that processes the COMPOUND_EXPR will store the increment of B
1635 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
1636 processes the PLUS_EXPR will need to merge the two lists so that
1637 eventually, all accesses end up on the same list (and we'll warn about the
1638 unordered subexpressions b++ and b.
1640 A note on merging. If we modify the former example so that our expression
1641 becomes
1642 (b++, b) + a
1643 care must be taken not simply to add all three expressions into the final
1644 PNO_SP list. The function merge_tlist takes care of that by merging the
1645 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
1646 way, so that no more than one access to B is recorded. */
1648 static void
1649 verify_tree (x, pbefore_sp, pno_sp, writer)
1650 tree x;
1651 struct tlist **pbefore_sp, **pno_sp;
1652 tree writer;
1654 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
1655 enum tree_code code;
1656 char class;
1658 /* X may be NULL if it is the operand of an empty statement expression
1659 ({ }). */
1660 if (x == NULL)
1661 return;
1663 restart:
1664 code = TREE_CODE (x);
1665 class = TREE_CODE_CLASS (code);
1667 if (warning_candidate_p (x))
1669 *pno_sp = new_tlist (*pno_sp, x, writer);
1670 return;
1673 switch (code)
1675 case CONSTRUCTOR:
1676 return;
1678 case COMPOUND_EXPR:
1679 case TRUTH_ANDIF_EXPR:
1680 case TRUTH_ORIF_EXPR:
1681 tmp_before = tmp_nosp = tmp_list3 = 0;
1682 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1683 warn_for_collisions (tmp_nosp);
1684 merge_tlist (pbefore_sp, tmp_before, 0);
1685 merge_tlist (pbefore_sp, tmp_nosp, 0);
1686 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
1687 merge_tlist (pbefore_sp, tmp_list3, 0);
1688 return;
1690 case COND_EXPR:
1691 tmp_before = tmp_list2 = 0;
1692 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
1693 warn_for_collisions (tmp_list2);
1694 merge_tlist (pbefore_sp, tmp_before, 0);
1695 merge_tlist (pbefore_sp, tmp_list2, 1);
1697 tmp_list3 = tmp_nosp = 0;
1698 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
1699 warn_for_collisions (tmp_nosp);
1700 merge_tlist (pbefore_sp, tmp_list3, 0);
1702 tmp_list3 = tmp_list2 = 0;
1703 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
1704 warn_for_collisions (tmp_list2);
1705 merge_tlist (pbefore_sp, tmp_list3, 0);
1706 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1707 two first, to avoid warning for (a ? b++ : b++). */
1708 merge_tlist (&tmp_nosp, tmp_list2, 0);
1709 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1710 return;
1712 case PREDECREMENT_EXPR:
1713 case PREINCREMENT_EXPR:
1714 case POSTDECREMENT_EXPR:
1715 case POSTINCREMENT_EXPR:
1716 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
1717 return;
1719 case MODIFY_EXPR:
1720 tmp_before = tmp_nosp = tmp_list3 = 0;
1721 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
1722 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
1723 /* Expressions inside the LHS are not ordered wrt. the sequence points
1724 in the RHS. Example:
1725 *a = (a++, 2)
1726 Despite the fact that the modification of "a" is in the before_sp
1727 list (tmp_before), it conflicts with the use of "a" in the LHS.
1728 We can handle this by adding the contents of tmp_list3
1729 to those of tmp_before, and redoing the collision warnings for that
1730 list. */
1731 add_tlist (&tmp_before, tmp_list3, x, 1);
1732 warn_for_collisions (tmp_before);
1733 /* Exclude the LHS itself here; we first have to merge it into the
1734 tmp_nosp list. This is done to avoid warning for "a = a"; if we
1735 didn't exclude the LHS, we'd get it twice, once as a read and once
1736 as a write. */
1737 add_tlist (pno_sp, tmp_list3, x, 0);
1738 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
1740 merge_tlist (pbefore_sp, tmp_before, 0);
1741 if (warning_candidate_p (TREE_OPERAND (x, 0)))
1742 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
1743 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
1744 return;
1746 case CALL_EXPR:
1747 /* We need to warn about conflicts among arguments and conflicts between
1748 args and the function address. Side effects of the function address,
1749 however, are not ordered by the sequence point of the call. */
1750 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
1751 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1752 if (TREE_OPERAND (x, 1))
1753 verify_tree (TREE_OPERAND (x, 1), &tmp_list2, &tmp_list3, NULL_TREE);
1754 merge_tlist (&tmp_list3, tmp_list2, 0);
1755 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
1756 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
1757 warn_for_collisions (tmp_before);
1758 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
1759 return;
1761 case TREE_LIST:
1762 /* Scan all the list, e.g. indices of multi dimensional array. */
1763 while (x)
1765 tmp_before = tmp_nosp = 0;
1766 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
1767 merge_tlist (&tmp_nosp, tmp_before, 0);
1768 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1769 x = TREE_CHAIN (x);
1771 return;
1773 case SAVE_EXPR:
1775 struct tlist_cache *t;
1776 for (t = save_expr_cache; t; t = t->next)
1777 if (t->expr == x)
1778 break;
1780 if (! t)
1782 t = (struct tlist_cache *) obstack_alloc (&tlist_obstack,
1783 sizeof *t);
1784 t->next = save_expr_cache;
1785 t->expr = x;
1786 save_expr_cache = t;
1788 tmp_before = tmp_nosp = 0;
1789 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1790 warn_for_collisions (tmp_nosp);
1792 tmp_list3 = 0;
1793 while (tmp_nosp)
1795 struct tlist *t = tmp_nosp;
1796 tmp_nosp = t->next;
1797 merge_tlist (&tmp_list3, t, 0);
1799 t->cache_before_sp = tmp_before;
1800 t->cache_after_sp = tmp_list3;
1802 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
1803 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
1804 return;
1806 default:
1807 break;
1810 if (class == '1')
1812 if (first_rtl_op (code) == 0)
1813 return;
1814 x = TREE_OPERAND (x, 0);
1815 writer = 0;
1816 goto restart;
1819 switch (class)
1821 case 'r':
1822 case '<':
1823 case '2':
1824 case 'b':
1825 case 'e':
1826 case 's':
1827 case 'x':
1829 int lp;
1830 int max = first_rtl_op (TREE_CODE (x));
1831 for (lp = 0; lp < max; lp++)
1833 tmp_before = tmp_nosp = 0;
1834 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, NULL_TREE);
1835 merge_tlist (&tmp_nosp, tmp_before, 0);
1836 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1838 break;
1843 /* Try to warn for undefined behavior in EXPR due to missing sequence
1844 points. */
1846 static void
1847 verify_sequence_points (expr)
1848 tree expr;
1850 struct tlist *before_sp = 0, *after_sp = 0;
1852 warned_ids = 0;
1853 save_expr_cache = 0;
1854 if (tlist_firstobj == 0)
1856 gcc_obstack_init (&tlist_obstack);
1857 tlist_firstobj = obstack_alloc (&tlist_obstack, 0);
1860 verify_tree (expr, &before_sp, &after_sp, 0);
1861 warn_for_collisions (after_sp);
1862 obstack_free (&tlist_obstack, tlist_firstobj);
1865 tree
1866 c_expand_expr_stmt (expr)
1867 tree expr;
1869 /* Do default conversion if safe and possibly important,
1870 in case within ({...}). */
1871 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
1872 && (flag_isoc99 || lvalue_p (expr)))
1873 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1874 expr = default_conversion (expr);
1876 if (warn_sequence_point)
1877 verify_sequence_points (expr);
1879 if (TREE_TYPE (expr) != error_mark_node
1880 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
1881 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1882 error ("expression statement has incomplete type");
1884 last_expr_type = TREE_TYPE (expr);
1885 return add_stmt (build_stmt (EXPR_STMT, expr));
1888 /* Validate the expression after `case' and apply default promotions. */
1890 tree
1891 check_case_value (value)
1892 tree value;
1894 if (value == NULL_TREE)
1895 return value;
1897 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1898 STRIP_TYPE_NOPS (value);
1899 /* In C++, the following is allowed:
1901 const int i = 3;
1902 switch (...) { case i: ... }
1904 So, we try to reduce the VALUE to a constant that way. */
1905 if (c_language == clk_cplusplus)
1907 value = decl_constant_value (value);
1908 STRIP_TYPE_NOPS (value);
1909 value = fold (value);
1912 if (TREE_CODE (value) != INTEGER_CST
1913 && value != error_mark_node)
1915 error ("case label does not reduce to an integer constant");
1916 value = error_mark_node;
1918 else
1919 /* Promote char or short to int. */
1920 value = default_conversion (value);
1922 constant_expression_warning (value);
1924 return value;
1927 /* Return an integer type with BITS bits of precision,
1928 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1930 tree
1931 c_common_type_for_size (bits, unsignedp)
1932 unsigned bits;
1933 int unsignedp;
1935 if (bits == TYPE_PRECISION (integer_type_node))
1936 return unsignedp ? unsigned_type_node : integer_type_node;
1938 if (bits == TYPE_PRECISION (signed_char_type_node))
1939 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1941 if (bits == TYPE_PRECISION (short_integer_type_node))
1942 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1944 if (bits == TYPE_PRECISION (long_integer_type_node))
1945 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1947 if (bits == TYPE_PRECISION (long_long_integer_type_node))
1948 return (unsignedp ? long_long_unsigned_type_node
1949 : long_long_integer_type_node);
1951 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
1952 return (unsignedp ? widest_unsigned_literal_type_node
1953 : widest_integer_literal_type_node);
1955 if (bits <= TYPE_PRECISION (intQI_type_node))
1956 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1958 if (bits <= TYPE_PRECISION (intHI_type_node))
1959 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1961 if (bits <= TYPE_PRECISION (intSI_type_node))
1962 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1964 if (bits <= TYPE_PRECISION (intDI_type_node))
1965 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1967 return 0;
1970 /* Return a data type that has machine mode MODE.
1971 If the mode is an integer,
1972 then UNSIGNEDP selects between signed and unsigned types. */
1974 tree
1975 c_common_type_for_mode (mode, unsignedp)
1976 enum machine_mode mode;
1977 int unsignedp;
1979 if (mode == TYPE_MODE (integer_type_node))
1980 return unsignedp ? unsigned_type_node : integer_type_node;
1982 if (mode == TYPE_MODE (signed_char_type_node))
1983 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1985 if (mode == TYPE_MODE (short_integer_type_node))
1986 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1988 if (mode == TYPE_MODE (long_integer_type_node))
1989 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1991 if (mode == TYPE_MODE (long_long_integer_type_node))
1992 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
1994 if (mode == TYPE_MODE (widest_integer_literal_type_node))
1995 return unsignedp ? widest_unsigned_literal_type_node
1996 : widest_integer_literal_type_node;
1998 if (mode == QImode)
1999 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2001 if (mode == HImode)
2002 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2004 if (mode == SImode)
2005 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2007 if (mode == DImode)
2008 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2010 #if HOST_BITS_PER_WIDE_INT >= 64
2011 if (mode == TYPE_MODE (intTI_type_node))
2012 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2013 #endif
2015 if (mode == TYPE_MODE (float_type_node))
2016 return float_type_node;
2018 if (mode == TYPE_MODE (double_type_node))
2019 return double_type_node;
2021 if (mode == TYPE_MODE (long_double_type_node))
2022 return long_double_type_node;
2024 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
2025 return build_pointer_type (char_type_node);
2027 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2028 return build_pointer_type (integer_type_node);
2030 switch (mode)
2032 case V16QImode:
2033 return unsignedp ? unsigned_V16QI_type_node : V16QI_type_node;
2034 case V8HImode:
2035 return unsignedp ? unsigned_V8HI_type_node : V8HI_type_node;
2036 case V4SImode:
2037 return unsignedp ? unsigned_V4SI_type_node : V4SI_type_node;
2038 case V2DImode:
2039 return unsignedp ? unsigned_V2DI_type_node : V2DI_type_node;
2040 case V2SImode:
2041 return unsignedp ? unsigned_V2SI_type_node : V2SI_type_node;
2042 case V4HImode:
2043 return unsignedp ? unsigned_V4HI_type_node : V4HI_type_node;
2044 case V8QImode:
2045 return unsignedp ? unsigned_V8QI_type_node : V8QI_type_node;
2046 case V1DImode:
2047 return unsignedp ? unsigned_V1DI_type_node : V1DI_type_node;
2048 case V16SFmode:
2049 return V16SF_type_node;
2050 case V4SFmode:
2051 return V4SF_type_node;
2052 case V2SFmode:
2053 return V2SF_type_node;
2054 case V2DFmode:
2055 return V2DF_type_node;
2056 default:
2057 break;
2060 return 0;
2063 /* Return an unsigned type the same as TYPE in other respects. */
2064 tree
2065 c_common_unsigned_type (type)
2066 tree type;
2068 tree type1 = TYPE_MAIN_VARIANT (type);
2069 if (type1 == signed_char_type_node || type1 == char_type_node)
2070 return unsigned_char_type_node;
2071 if (type1 == integer_type_node)
2072 return unsigned_type_node;
2073 if (type1 == short_integer_type_node)
2074 return short_unsigned_type_node;
2075 if (type1 == long_integer_type_node)
2076 return long_unsigned_type_node;
2077 if (type1 == long_long_integer_type_node)
2078 return long_long_unsigned_type_node;
2079 if (type1 == widest_integer_literal_type_node)
2080 return widest_unsigned_literal_type_node;
2081 #if HOST_BITS_PER_WIDE_INT >= 64
2082 if (type1 == intTI_type_node)
2083 return unsigned_intTI_type_node;
2084 #endif
2085 if (type1 == intDI_type_node)
2086 return unsigned_intDI_type_node;
2087 if (type1 == intSI_type_node)
2088 return unsigned_intSI_type_node;
2089 if (type1 == intHI_type_node)
2090 return unsigned_intHI_type_node;
2091 if (type1 == intQI_type_node)
2092 return unsigned_intQI_type_node;
2094 return c_common_signed_or_unsigned_type (1, type);
2097 /* Return a signed type the same as TYPE in other respects. */
2099 tree
2100 c_common_signed_type (type)
2101 tree type;
2103 tree type1 = TYPE_MAIN_VARIANT (type);
2104 if (type1 == unsigned_char_type_node || type1 == char_type_node)
2105 return signed_char_type_node;
2106 if (type1 == unsigned_type_node)
2107 return integer_type_node;
2108 if (type1 == short_unsigned_type_node)
2109 return short_integer_type_node;
2110 if (type1 == long_unsigned_type_node)
2111 return long_integer_type_node;
2112 if (type1 == long_long_unsigned_type_node)
2113 return long_long_integer_type_node;
2114 if (type1 == widest_unsigned_literal_type_node)
2115 return widest_integer_literal_type_node;
2116 #if HOST_BITS_PER_WIDE_INT >= 64
2117 if (type1 == unsigned_intTI_type_node)
2118 return intTI_type_node;
2119 #endif
2120 if (type1 == unsigned_intDI_type_node)
2121 return intDI_type_node;
2122 if (type1 == unsigned_intSI_type_node)
2123 return intSI_type_node;
2124 if (type1 == unsigned_intHI_type_node)
2125 return intHI_type_node;
2126 if (type1 == unsigned_intQI_type_node)
2127 return intQI_type_node;
2129 return c_common_signed_or_unsigned_type (0, type);
2132 /* Return a type the same as TYPE except unsigned or
2133 signed according to UNSIGNEDP. */
2135 tree
2136 c_common_signed_or_unsigned_type (unsignedp, type)
2137 int unsignedp;
2138 tree type;
2140 if (! INTEGRAL_TYPE_P (type)
2141 || TREE_UNSIGNED (type) == unsignedp)
2142 return type;
2144 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
2145 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2146 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2147 return unsignedp ? unsigned_type_node : integer_type_node;
2148 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
2149 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2150 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
2151 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2152 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
2153 return (unsignedp ? long_long_unsigned_type_node
2154 : long_long_integer_type_node);
2155 if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
2156 return (unsignedp ? widest_unsigned_literal_type_node
2157 : widest_integer_literal_type_node);
2159 #if HOST_BITS_PER_WIDE_INT >= 64
2160 if (TYPE_PRECISION (type) == TYPE_PRECISION (intTI_type_node))
2161 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2162 #endif
2163 if (TYPE_PRECISION (type) == TYPE_PRECISION (intDI_type_node))
2164 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2165 if (TYPE_PRECISION (type) == TYPE_PRECISION (intSI_type_node))
2166 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2167 if (TYPE_PRECISION (type) == TYPE_PRECISION (intHI_type_node))
2168 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2169 if (TYPE_PRECISION (type) == TYPE_PRECISION (intQI_type_node))
2170 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2172 return type;
2175 /* Return the minimum number of bits needed to represent VALUE in a
2176 signed or unsigned type, UNSIGNEDP says which. */
2178 unsigned int
2179 min_precision (value, unsignedp)
2180 tree value;
2181 int unsignedp;
2183 int log;
2185 /* If the value is negative, compute its negative minus 1. The latter
2186 adjustment is because the absolute value of the largest negative value
2187 is one larger than the largest positive value. This is equivalent to
2188 a bit-wise negation, so use that operation instead. */
2190 if (tree_int_cst_sgn (value) < 0)
2191 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2193 /* Return the number of bits needed, taking into account the fact
2194 that we need one more bit for a signed than unsigned type. */
2196 if (integer_zerop (value))
2197 log = 0;
2198 else
2199 log = tree_floor_log2 (value);
2201 return log + 1 + ! unsignedp;
2204 /* Print an error message for invalid operands to arith operation
2205 CODE. NOP_EXPR is used as a special case (see
2206 c_common_truthvalue_conversion). */
2208 void
2209 binary_op_error (code)
2210 enum tree_code code;
2212 const char *opname;
2214 switch (code)
2216 case NOP_EXPR:
2217 error ("invalid truth-value expression");
2218 return;
2220 case PLUS_EXPR:
2221 opname = "+"; break;
2222 case MINUS_EXPR:
2223 opname = "-"; break;
2224 case MULT_EXPR:
2225 opname = "*"; break;
2226 case MAX_EXPR:
2227 opname = "max"; break;
2228 case MIN_EXPR:
2229 opname = "min"; break;
2230 case EQ_EXPR:
2231 opname = "=="; break;
2232 case NE_EXPR:
2233 opname = "!="; break;
2234 case LE_EXPR:
2235 opname = "<="; break;
2236 case GE_EXPR:
2237 opname = ">="; break;
2238 case LT_EXPR:
2239 opname = "<"; break;
2240 case GT_EXPR:
2241 opname = ">"; break;
2242 case LSHIFT_EXPR:
2243 opname = "<<"; break;
2244 case RSHIFT_EXPR:
2245 opname = ">>"; break;
2246 case TRUNC_MOD_EXPR:
2247 case FLOOR_MOD_EXPR:
2248 opname = "%"; break;
2249 case TRUNC_DIV_EXPR:
2250 case FLOOR_DIV_EXPR:
2251 opname = "/"; break;
2252 case BIT_AND_EXPR:
2253 opname = "&"; break;
2254 case BIT_IOR_EXPR:
2255 opname = "|"; break;
2256 case TRUTH_ANDIF_EXPR:
2257 opname = "&&"; break;
2258 case TRUTH_ORIF_EXPR:
2259 opname = "||"; break;
2260 case BIT_XOR_EXPR:
2261 opname = "^"; break;
2262 case LROTATE_EXPR:
2263 case RROTATE_EXPR:
2264 opname = "rotate"; break;
2265 default:
2266 opname = "unknown"; break;
2268 error ("invalid operands to binary %s", opname);
2271 /* Subroutine of build_binary_op, used for comparison operations.
2272 See if the operands have both been converted from subword integer types
2273 and, if so, perhaps change them both back to their original type.
2274 This function is also responsible for converting the two operands
2275 to the proper common type for comparison.
2277 The arguments of this function are all pointers to local variables
2278 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2279 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2281 If this function returns nonzero, it means that the comparison has
2282 a constant value. What this function returns is an expression for
2283 that value. */
2285 tree
2286 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2287 tree *op0_ptr, *op1_ptr;
2288 tree *restype_ptr;
2289 enum tree_code *rescode_ptr;
2291 tree type;
2292 tree op0 = *op0_ptr;
2293 tree op1 = *op1_ptr;
2294 int unsignedp0, unsignedp1;
2295 int real1, real2;
2296 tree primop0, primop1;
2297 enum tree_code code = *rescode_ptr;
2299 /* Throw away any conversions to wider types
2300 already present in the operands. */
2302 primop0 = get_narrower (op0, &unsignedp0);
2303 primop1 = get_narrower (op1, &unsignedp1);
2305 /* Handle the case that OP0 does not *contain* a conversion
2306 but it *requires* conversion to FINAL_TYPE. */
2308 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2309 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2310 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2311 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2313 /* If one of the operands must be floated, we cannot optimize. */
2314 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2315 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2317 /* If first arg is constant, swap the args (changing operation
2318 so value is preserved), for canonicalization. Don't do this if
2319 the second arg is 0. */
2321 if (TREE_CONSTANT (primop0)
2322 && ! integer_zerop (primop1) && ! real_zerop (primop1))
2324 tree tem = primop0;
2325 int temi = unsignedp0;
2326 primop0 = primop1;
2327 primop1 = tem;
2328 tem = op0;
2329 op0 = op1;
2330 op1 = tem;
2331 *op0_ptr = op0;
2332 *op1_ptr = op1;
2333 unsignedp0 = unsignedp1;
2334 unsignedp1 = temi;
2335 temi = real1;
2336 real1 = real2;
2337 real2 = temi;
2339 switch (code)
2341 case LT_EXPR:
2342 code = GT_EXPR;
2343 break;
2344 case GT_EXPR:
2345 code = LT_EXPR;
2346 break;
2347 case LE_EXPR:
2348 code = GE_EXPR;
2349 break;
2350 case GE_EXPR:
2351 code = LE_EXPR;
2352 break;
2353 default:
2354 break;
2356 *rescode_ptr = code;
2359 /* If comparing an integer against a constant more bits wide,
2360 maybe we can deduce a value of 1 or 0 independent of the data.
2361 Or else truncate the constant now
2362 rather than extend the variable at run time.
2364 This is only interesting if the constant is the wider arg.
2365 Also, it is not safe if the constant is unsigned and the
2366 variable arg is signed, since in this case the variable
2367 would be sign-extended and then regarded as unsigned.
2368 Our technique fails in this case because the lowest/highest
2369 possible unsigned results don't follow naturally from the
2370 lowest/highest possible values of the variable operand.
2371 For just EQ_EXPR and NE_EXPR there is another technique that
2372 could be used: see if the constant can be faithfully represented
2373 in the other operand's type, by truncating it and reextending it
2374 and see if that preserves the constant's value. */
2376 if (!real1 && !real2
2377 && TREE_CODE (primop1) == INTEGER_CST
2378 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2380 int min_gt, max_gt, min_lt, max_lt;
2381 tree maxval, minval;
2382 /* 1 if comparison is nominally unsigned. */
2383 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2384 tree val;
2386 type = c_common_signed_or_unsigned_type (unsignedp0,
2387 TREE_TYPE (primop0));
2389 /* If TYPE is an enumeration, then we need to get its min/max
2390 values from it's underlying integral type, not the enumerated
2391 type itself. */
2392 if (TREE_CODE (type) == ENUMERAL_TYPE)
2393 type = c_common_type_for_size (TYPE_PRECISION (type), unsignedp0);
2395 maxval = TYPE_MAX_VALUE (type);
2396 minval = TYPE_MIN_VALUE (type);
2398 if (unsignedp && !unsignedp0)
2399 *restype_ptr = c_common_signed_type (*restype_ptr);
2401 if (TREE_TYPE (primop1) != *restype_ptr)
2402 primop1 = convert (*restype_ptr, primop1);
2403 if (type != *restype_ptr)
2405 minval = convert (*restype_ptr, minval);
2406 maxval = convert (*restype_ptr, maxval);
2409 if (unsignedp && unsignedp0)
2411 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2412 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2413 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2414 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2416 else
2418 min_gt = INT_CST_LT (primop1, minval);
2419 max_gt = INT_CST_LT (primop1, maxval);
2420 min_lt = INT_CST_LT (minval, primop1);
2421 max_lt = INT_CST_LT (maxval, primop1);
2424 val = 0;
2425 /* This used to be a switch, but Genix compiler can't handle that. */
2426 if (code == NE_EXPR)
2428 if (max_lt || min_gt)
2429 val = boolean_true_node;
2431 else if (code == EQ_EXPR)
2433 if (max_lt || min_gt)
2434 val = boolean_false_node;
2436 else if (code == LT_EXPR)
2438 if (max_lt)
2439 val = boolean_true_node;
2440 if (!min_lt)
2441 val = boolean_false_node;
2443 else if (code == GT_EXPR)
2445 if (min_gt)
2446 val = boolean_true_node;
2447 if (!max_gt)
2448 val = boolean_false_node;
2450 else if (code == LE_EXPR)
2452 if (!max_gt)
2453 val = boolean_true_node;
2454 if (min_gt)
2455 val = boolean_false_node;
2457 else if (code == GE_EXPR)
2459 if (!min_lt)
2460 val = boolean_true_node;
2461 if (max_lt)
2462 val = boolean_false_node;
2465 /* If primop0 was sign-extended and unsigned comparison specd,
2466 we did a signed comparison above using the signed type bounds.
2467 But the comparison we output must be unsigned.
2469 Also, for inequalities, VAL is no good; but if the signed
2470 comparison had *any* fixed result, it follows that the
2471 unsigned comparison just tests the sign in reverse
2472 (positive values are LE, negative ones GE).
2473 So we can generate an unsigned comparison
2474 against an extreme value of the signed type. */
2476 if (unsignedp && !unsignedp0)
2478 if (val != 0)
2479 switch (code)
2481 case LT_EXPR:
2482 case GE_EXPR:
2483 primop1 = TYPE_MIN_VALUE (type);
2484 val = 0;
2485 break;
2487 case LE_EXPR:
2488 case GT_EXPR:
2489 primop1 = TYPE_MAX_VALUE (type);
2490 val = 0;
2491 break;
2493 default:
2494 break;
2496 type = c_common_unsigned_type (type);
2499 if (TREE_CODE (primop0) != INTEGER_CST)
2501 if (val == boolean_false_node)
2502 warning ("comparison is always false due to limited range of data type");
2503 if (val == boolean_true_node)
2504 warning ("comparison is always true due to limited range of data type");
2507 if (val != 0)
2509 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2510 if (TREE_SIDE_EFFECTS (primop0))
2511 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2512 return val;
2515 /* Value is not predetermined, but do the comparison
2516 in the type of the operand that is not constant.
2517 TYPE is already properly set. */
2519 else if (real1 && real2
2520 && (TYPE_PRECISION (TREE_TYPE (primop0))
2521 == TYPE_PRECISION (TREE_TYPE (primop1))))
2522 type = TREE_TYPE (primop0);
2524 /* If args' natural types are both narrower than nominal type
2525 and both extend in the same manner, compare them
2526 in the type of the wider arg.
2527 Otherwise must actually extend both to the nominal
2528 common type lest different ways of extending
2529 alter the result.
2530 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2532 else if (unsignedp0 == unsignedp1 && real1 == real2
2533 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2534 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2536 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2537 type = c_common_signed_or_unsigned_type (unsignedp0
2538 || TREE_UNSIGNED (*restype_ptr),
2539 type);
2540 /* Make sure shorter operand is extended the right way
2541 to match the longer operand. */
2542 primop0
2543 = convert (c_common_signed_or_unsigned_type (unsignedp0,
2544 TREE_TYPE (primop0)),
2545 primop0);
2546 primop1
2547 = convert (c_common_signed_or_unsigned_type (unsignedp1,
2548 TREE_TYPE (primop1)),
2549 primop1);
2551 else
2553 /* Here we must do the comparison on the nominal type
2554 using the args exactly as we received them. */
2555 type = *restype_ptr;
2556 primop0 = op0;
2557 primop1 = op1;
2559 if (!real1 && !real2 && integer_zerop (primop1)
2560 && TREE_UNSIGNED (*restype_ptr))
2562 tree value = 0;
2563 switch (code)
2565 case GE_EXPR:
2566 /* All unsigned values are >= 0, so we warn if extra warnings
2567 are requested. However, if OP0 is a constant that is
2568 >= 0, the signedness of the comparison isn't an issue,
2569 so suppress the warning. */
2570 if (extra_warnings && !in_system_header
2571 && ! (TREE_CODE (primop0) == INTEGER_CST
2572 && ! TREE_OVERFLOW (convert (c_common_signed_type (type),
2573 primop0))))
2574 warning ("comparison of unsigned expression >= 0 is always true");
2575 value = boolean_true_node;
2576 break;
2578 case LT_EXPR:
2579 if (extra_warnings && !in_system_header
2580 && ! (TREE_CODE (primop0) == INTEGER_CST
2581 && ! TREE_OVERFLOW (convert (c_common_signed_type (type),
2582 primop0))))
2583 warning ("comparison of unsigned expression < 0 is always false");
2584 value = boolean_false_node;
2585 break;
2587 default:
2588 break;
2591 if (value != 0)
2593 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2594 if (TREE_SIDE_EFFECTS (primop0))
2595 return build (COMPOUND_EXPR, TREE_TYPE (value),
2596 primop0, value);
2597 return value;
2602 *op0_ptr = convert (type, primop0);
2603 *op1_ptr = convert (type, primop1);
2605 *restype_ptr = boolean_type_node;
2607 return 0;
2610 /* Return a tree for the sum or difference (RESULTCODE says which)
2611 of pointer PTROP and integer INTOP. */
2613 tree
2614 pointer_int_sum (resultcode, ptrop, intop)
2615 enum tree_code resultcode;
2616 tree ptrop, intop;
2618 tree size_exp;
2620 tree result;
2621 tree folded;
2623 /* The result is a pointer of the same type that is being added. */
2625 tree result_type = TREE_TYPE (ptrop);
2627 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
2629 if (pedantic || warn_pointer_arith)
2630 pedwarn ("pointer of type `void *' used in arithmetic");
2631 size_exp = integer_one_node;
2633 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
2635 if (pedantic || warn_pointer_arith)
2636 pedwarn ("pointer to a function used in arithmetic");
2637 size_exp = integer_one_node;
2639 else if (TREE_CODE (TREE_TYPE (result_type)) == METHOD_TYPE)
2641 if (pedantic || warn_pointer_arith)
2642 pedwarn ("pointer to member function used in arithmetic");
2643 size_exp = integer_one_node;
2645 else if (TREE_CODE (TREE_TYPE (result_type)) == OFFSET_TYPE)
2647 if (pedantic || warn_pointer_arith)
2648 pedwarn ("pointer to a member used in arithmetic");
2649 size_exp = integer_one_node;
2651 else
2652 size_exp = size_in_bytes (TREE_TYPE (result_type));
2654 /* If what we are about to multiply by the size of the elements
2655 contains a constant term, apply distributive law
2656 and multiply that constant term separately.
2657 This helps produce common subexpressions. */
2659 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
2660 && ! TREE_CONSTANT (intop)
2661 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
2662 && TREE_CONSTANT (size_exp)
2663 /* If the constant comes from pointer subtraction,
2664 skip this optimization--it would cause an error. */
2665 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
2666 /* If the constant is unsigned, and smaller than the pointer size,
2667 then we must skip this optimization. This is because it could cause
2668 an overflow error if the constant is negative but INTOP is not. */
2669 && (! TREE_UNSIGNED (TREE_TYPE (intop))
2670 || (TYPE_PRECISION (TREE_TYPE (intop))
2671 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
2673 enum tree_code subcode = resultcode;
2674 tree int_type = TREE_TYPE (intop);
2675 if (TREE_CODE (intop) == MINUS_EXPR)
2676 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
2677 /* Convert both subexpression types to the type of intop,
2678 because weird cases involving pointer arithmetic
2679 can result in a sum or difference with different type args. */
2680 ptrop = build_binary_op (subcode, ptrop,
2681 convert (int_type, TREE_OPERAND (intop, 1)), 1);
2682 intop = convert (int_type, TREE_OPERAND (intop, 0));
2685 /* Convert the integer argument to a type the same size as sizetype
2686 so the multiply won't overflow spuriously. */
2688 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
2689 || TREE_UNSIGNED (TREE_TYPE (intop)) != TREE_UNSIGNED (sizetype))
2690 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
2691 TREE_UNSIGNED (sizetype)), intop);
2693 /* Replace the integer argument with a suitable product by the object size.
2694 Do this multiplication as signed, then convert to the appropriate
2695 pointer type (actually unsigned integral). */
2697 intop = convert (result_type,
2698 build_binary_op (MULT_EXPR, intop,
2699 convert (TREE_TYPE (intop), size_exp), 1));
2701 /* Create the sum or difference. */
2703 result = build (resultcode, result_type, ptrop, intop);
2705 folded = fold (result);
2706 if (folded == result)
2707 TREE_CONSTANT (folded) = TREE_CONSTANT (ptrop) & TREE_CONSTANT (intop);
2708 return folded;
2711 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2712 or validate its data type for an `if' or `while' statement or ?..: exp.
2714 This preparation consists of taking the ordinary
2715 representation of an expression expr and producing a valid tree
2716 boolean expression describing whether expr is nonzero. We could
2717 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2718 but we optimize comparisons, &&, ||, and !.
2720 The resulting type should always be `boolean_type_node'. */
2722 tree
2723 c_common_truthvalue_conversion (expr)
2724 tree expr;
2726 if (TREE_CODE (expr) == ERROR_MARK)
2727 return expr;
2729 #if 0 /* This appears to be wrong for C++. */
2730 /* These really should return error_mark_node after 2.4 is stable.
2731 But not all callers handle ERROR_MARK properly. */
2732 switch (TREE_CODE (TREE_TYPE (expr)))
2734 case RECORD_TYPE:
2735 error ("struct type value used where scalar is required");
2736 return boolean_false_node;
2738 case UNION_TYPE:
2739 error ("union type value used where scalar is required");
2740 return boolean_false_node;
2742 case ARRAY_TYPE:
2743 error ("array type value used where scalar is required");
2744 return boolean_false_node;
2746 default:
2747 break;
2749 #endif /* 0 */
2751 switch (TREE_CODE (expr))
2753 case EQ_EXPR:
2754 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2755 case TRUTH_ANDIF_EXPR:
2756 case TRUTH_ORIF_EXPR:
2757 case TRUTH_AND_EXPR:
2758 case TRUTH_OR_EXPR:
2759 case TRUTH_XOR_EXPR:
2760 case TRUTH_NOT_EXPR:
2761 TREE_TYPE (expr) = boolean_type_node;
2762 return expr;
2764 case ERROR_MARK:
2765 return expr;
2767 case INTEGER_CST:
2768 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2770 case REAL_CST:
2771 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2773 case ADDR_EXPR:
2774 /* If we are taking the address of an external decl, it might be zero
2775 if it is weak, so we cannot optimize. */
2776 if (DECL_P (TREE_OPERAND (expr, 0))
2777 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2778 break;
2780 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2781 return build (COMPOUND_EXPR, boolean_type_node,
2782 TREE_OPERAND (expr, 0), boolean_true_node);
2783 else
2784 return boolean_true_node;
2786 case COMPLEX_EXPR:
2787 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2788 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2789 c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)),
2790 c_common_truthvalue_conversion (TREE_OPERAND (expr, 1)),
2793 case NEGATE_EXPR:
2794 case ABS_EXPR:
2795 case FLOAT_EXPR:
2796 case FFS_EXPR:
2797 /* These don't change whether an object is nonzero or zero. */
2798 return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0));
2800 case LROTATE_EXPR:
2801 case RROTATE_EXPR:
2802 /* These don't change whether an object is zero or nonzero, but
2803 we can't ignore them if their second arg has side-effects. */
2804 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2805 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2806 c_common_truthvalue_conversion (TREE_OPERAND (expr, 0)));
2807 else
2808 return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0));
2810 case COND_EXPR:
2811 /* Distribute the conversion into the arms of a COND_EXPR. */
2812 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2813 c_common_truthvalue_conversion (TREE_OPERAND (expr, 1)),
2814 c_common_truthvalue_conversion (TREE_OPERAND (expr, 2))));
2816 case CONVERT_EXPR:
2817 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2818 since that affects how `default_conversion' will behave. */
2819 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2820 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2821 break;
2822 /* fall through... */
2823 case NOP_EXPR:
2824 /* If this is widening the argument, we can ignore it. */
2825 if (TYPE_PRECISION (TREE_TYPE (expr))
2826 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2827 return c_common_truthvalue_conversion (TREE_OPERAND (expr, 0));
2828 break;
2830 case MINUS_EXPR:
2831 /* Perhaps reduce (x - y) != 0 to (x != y). The expressions
2832 aren't guaranteed to the be same for modes that can represent
2833 infinity, since if x and y are both +infinity, or both
2834 -infinity, then x - y is not a number.
2836 Note that this transformation is safe when x or y is NaN.
2837 (x - y) is then NaN, and both (x - y) != 0 and x != y will
2838 be false. */
2839 if (HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (TREE_OPERAND (expr, 0)))))
2840 break;
2841 /* fall through... */
2842 case BIT_XOR_EXPR:
2843 /* This and MINUS_EXPR can be changed into a comparison of the
2844 two objects. */
2845 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2846 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2847 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2848 TREE_OPERAND (expr, 1), 1);
2849 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2850 fold (build1 (NOP_EXPR,
2851 TREE_TYPE (TREE_OPERAND (expr, 0)),
2852 TREE_OPERAND (expr, 1))), 1);
2854 case BIT_AND_EXPR:
2855 if (integer_onep (TREE_OPERAND (expr, 1))
2856 && TREE_TYPE (expr) != boolean_type_node)
2857 /* Using convert here would cause infinite recursion. */
2858 return build1 (NOP_EXPR, boolean_type_node, expr);
2859 break;
2861 case MODIFY_EXPR:
2862 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2863 warning ("suggest parentheses around assignment used as truth value");
2864 break;
2866 default:
2867 break;
2870 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2872 tree t = save_expr (expr);
2873 return (build_binary_op
2874 ((TREE_SIDE_EFFECTS (expr)
2875 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2876 c_common_truthvalue_conversion (build_unary_op (REALPART_EXPR, t, 0)),
2877 c_common_truthvalue_conversion (build_unary_op (IMAGPART_EXPR, t, 0)),
2878 0));
2881 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2884 static tree builtin_function_2 PARAMS ((const char *, const char *, tree, tree,
2885 int, enum built_in_class, int, int,
2886 tree));
2888 /* Make a variant type in the proper way for C/C++, propagating qualifiers
2889 down to the element type of an array. */
2891 tree
2892 c_build_qualified_type (type, type_quals)
2893 tree type;
2894 int type_quals;
2896 /* A restrict-qualified pointer type must be a pointer to object or
2897 incomplete type. Note that the use of POINTER_TYPE_P also allows
2898 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
2899 the C++ front-end also use POINTER_TYPE for pointer-to-member
2900 values, so even though it should be illegal to use `restrict'
2901 with such an entity we don't flag that here. Thus, special case
2902 code for that case is required in the C++ front-end. */
2903 if ((type_quals & TYPE_QUAL_RESTRICT)
2904 && (!POINTER_TYPE_P (type)
2905 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
2907 error ("invalid use of `restrict'");
2908 type_quals &= ~TYPE_QUAL_RESTRICT;
2911 if (TREE_CODE (type) == ARRAY_TYPE)
2912 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
2913 type_quals),
2914 TYPE_DOMAIN (type));
2915 return build_qualified_type (type, type_quals);
2918 /* Apply the TYPE_QUALS to the new DECL. */
2920 void
2921 c_apply_type_quals_to_decl (type_quals, decl)
2922 int type_quals;
2923 tree decl;
2925 if ((type_quals & TYPE_QUAL_CONST)
2926 || (TREE_TYPE (decl)
2927 && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE))
2928 TREE_READONLY (decl) = 1;
2929 if (type_quals & TYPE_QUAL_VOLATILE)
2931 TREE_SIDE_EFFECTS (decl) = 1;
2932 TREE_THIS_VOLATILE (decl) = 1;
2934 if (type_quals & TYPE_QUAL_RESTRICT)
2936 if (!TREE_TYPE (decl)
2937 || !POINTER_TYPE_P (TREE_TYPE (decl))
2938 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
2939 error ("invalid use of `restrict'");
2940 else if (flag_strict_aliasing)
2941 /* Indicate we need to make a unique alias set for this pointer.
2942 We can't do it here because it might be pointing to an
2943 incomplete type. */
2944 DECL_POINTER_ALIAS_SET (decl) = -2;
2948 /* Return the typed-based alias set for T, which may be an expression
2949 or a type. Return -1 if we don't do anything special. */
2951 HOST_WIDE_INT
2952 c_common_get_alias_set (t)
2953 tree t;
2955 tree u;
2957 /* Permit type-punning when accessing a union, provided the access
2958 is directly through the union. For example, this code does not
2959 permit taking the address of a union member and then storing
2960 through it. Even the type-punning allowed here is a GCC
2961 extension, albeit a common and useful one; the C standard says
2962 that such accesses have implementation-defined behavior. */
2963 for (u = t;
2964 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
2965 u = TREE_OPERAND (u, 0))
2966 if (TREE_CODE (u) == COMPONENT_REF
2967 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
2968 return 0;
2970 /* That's all the expressions we handle specially. */
2971 if (! TYPE_P (t))
2972 return -1;
2974 /* The C standard guarantess that any object may be accessed via an
2975 lvalue that has character type. */
2976 if (t == char_type_node
2977 || t == signed_char_type_node
2978 || t == unsigned_char_type_node)
2979 return 0;
2981 /* If it has the may_alias attribute, it can alias anything. */
2982 if (lookup_attribute ("may_alias", TYPE_ATTRIBUTES (t)))
2983 return 0;
2985 /* The C standard specifically allows aliasing between signed and
2986 unsigned variants of the same type. We treat the signed
2987 variant as canonical. */
2988 if (TREE_CODE (t) == INTEGER_TYPE && TREE_UNSIGNED (t))
2990 tree t1 = c_common_signed_type (t);
2992 /* t1 == t can happen for boolean nodes which are always unsigned. */
2993 if (t1 != t)
2994 return get_alias_set (t1);
2996 else if (POINTER_TYPE_P (t))
2998 tree t1;
3000 /* Unfortunately, there is no canonical form of a pointer type.
3001 In particular, if we have `typedef int I', then `int *', and
3002 `I *' are different types. So, we have to pick a canonical
3003 representative. We do this below.
3005 Technically, this approach is actually more conservative that
3006 it needs to be. In particular, `const int *' and `int *'
3007 should be in different alias sets, according to the C and C++
3008 standard, since their types are not the same, and so,
3009 technically, an `int **' and `const int **' cannot point at
3010 the same thing.
3012 But, the standard is wrong. In particular, this code is
3013 legal C++:
3015 int *ip;
3016 int **ipp = &ip;
3017 const int* const* cipp = &ipp;
3019 And, it doesn't make sense for that to be legal unless you
3020 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
3021 the pointed-to types. This issue has been reported to the
3022 C++ committee. */
3023 t1 = build_type_no_quals (t);
3024 if (t1 != t)
3025 return get_alias_set (t1);
3028 return -1;
3031 /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where the
3032 second parameter indicates which OPERATOR is being applied. The COMPLAIN
3033 flag controls whether we should diagnose possibly ill-formed
3034 constructs or not. */
3035 tree
3036 c_sizeof_or_alignof_type (type, op, complain)
3037 tree type;
3038 enum tree_code op;
3039 int complain;
3041 const char *op_name;
3042 tree value = NULL;
3043 enum tree_code type_code = TREE_CODE (type);
3045 my_friendly_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR, 20020720);
3046 op_name = op == SIZEOF_EXPR ? "sizeof" : "__alignof__";
3048 if (type_code == FUNCTION_TYPE)
3050 if (op == SIZEOF_EXPR)
3052 if (complain && (pedantic || warn_pointer_arith))
3053 pedwarn ("invalid application of `sizeof' to a function type");
3054 value = size_one_node;
3056 else
3057 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
3059 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
3061 if (type_code == VOID_TYPE
3062 && complain && (pedantic || warn_pointer_arith))
3063 pedwarn ("invalid application of `%s' to a void type", op_name);
3064 value = size_one_node;
3066 else if (!COMPLETE_TYPE_P (type))
3068 if (complain)
3069 error ("invalid application of `%s' to an incomplete type", op_name);
3070 value = size_zero_node;
3072 else
3074 if (op == SIZEOF_EXPR)
3075 /* Convert in case a char is more than one unit. */
3076 value = size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
3077 size_int (TYPE_PRECISION (char_type_node)
3078 / BITS_PER_UNIT));
3079 else
3080 value = size_int (TYPE_ALIGN (type) / BITS_PER_UNIT);
3083 /* VALUE will have an integer type with TYPE_IS_SIZETYPE set.
3084 TYPE_IS_SIZETYPE means that certain things (like overflow) will
3085 never happen. However, this node should really have type
3086 `size_t', which is just a typedef for an ordinary integer type. */
3087 value = fold (build1 (NOP_EXPR, c_size_type_node, value));
3088 my_friendly_assert (!TYPE_IS_SIZETYPE (TREE_TYPE (value)), 20001021);
3090 return value;
3093 /* Implement the __alignof keyword: Return the minimum required
3094 alignment of EXPR, measured in bytes. For VAR_DECL's and
3095 FIELD_DECL's return DECL_ALIGN (which can be set from an
3096 "aligned" __attribute__ specification). */
3098 tree
3099 c_alignof_expr (expr)
3100 tree expr;
3102 tree t;
3104 if (TREE_CODE (expr) == VAR_DECL)
3105 t = size_int (DECL_ALIGN (expr) / BITS_PER_UNIT);
3107 else if (TREE_CODE (expr) == COMPONENT_REF
3108 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
3110 error ("`__alignof' applied to a bit-field");
3111 t = size_one_node;
3113 else if (TREE_CODE (expr) == COMPONENT_REF
3114 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
3115 t = size_int (DECL_ALIGN (TREE_OPERAND (expr, 1)) / BITS_PER_UNIT);
3117 else if (TREE_CODE (expr) == INDIRECT_REF)
3119 tree t = TREE_OPERAND (expr, 0);
3120 tree best = t;
3121 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
3123 while (TREE_CODE (t) == NOP_EXPR
3124 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
3126 int thisalign;
3128 t = TREE_OPERAND (t, 0);
3129 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
3130 if (thisalign > bestalign)
3131 best = t, bestalign = thisalign;
3133 return c_alignof (TREE_TYPE (TREE_TYPE (best)));
3135 else
3136 return c_alignof (TREE_TYPE (expr));
3138 return fold (build1 (NOP_EXPR, c_size_type_node, t));
3141 /* Handle C and C++ default attributes. */
3143 enum built_in_attribute
3145 #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
3146 #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
3147 #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
3148 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
3149 #define DEF_FN_ATTR(NAME, ATTRS, PREDICATE) /* No entry needed in enum. */
3150 #include "builtin-attrs.def"
3151 #undef DEF_ATTR_NULL_TREE
3152 #undef DEF_ATTR_INT
3153 #undef DEF_ATTR_IDENT
3154 #undef DEF_ATTR_TREE_LIST
3155 #undef DEF_FN_ATTR
3156 ATTR_LAST
3159 static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
3161 static bool c_attrs_initialized = false;
3163 static void c_init_attributes PARAMS ((void));
3165 /* Build tree nodes and builtin functions common to both C and C++ language
3166 frontends. */
3168 void
3169 c_common_nodes_and_builtins ()
3171 enum builtin_type
3173 #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
3174 #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
3175 #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
3176 #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
3177 #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3178 #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3179 #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
3180 #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
3181 #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
3182 #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
3183 #include "builtin-types.def"
3184 #undef DEF_PRIMITIVE_TYPE
3185 #undef DEF_FUNCTION_TYPE_0
3186 #undef DEF_FUNCTION_TYPE_1
3187 #undef DEF_FUNCTION_TYPE_2
3188 #undef DEF_FUNCTION_TYPE_3
3189 #undef DEF_FUNCTION_TYPE_4
3190 #undef DEF_FUNCTION_TYPE_VAR_0
3191 #undef DEF_FUNCTION_TYPE_VAR_1
3192 #undef DEF_FUNCTION_TYPE_VAR_2
3193 #undef DEF_POINTER_TYPE
3194 BT_LAST
3197 typedef enum builtin_type builtin_type;
3199 tree builtin_types[(int) BT_LAST];
3200 int wchar_type_size;
3201 tree array_domain_type;
3202 tree va_list_ref_type_node;
3203 tree va_list_arg_type_node;
3205 /* Define `int' and `char' first so that dbx will output them first. */
3206 record_builtin_type (RID_INT, NULL, integer_type_node);
3207 record_builtin_type (RID_CHAR, "char", char_type_node);
3209 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
3210 "unsigned long", "long long unsigned" and "unsigned short" were in C++
3211 but not C. Are the conditionals here needed? */
3212 if (c_language == clk_cplusplus)
3213 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
3214 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
3215 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
3216 record_builtin_type (RID_MAX, "long unsigned int",
3217 long_unsigned_type_node);
3218 if (c_language == clk_cplusplus)
3219 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
3220 record_builtin_type (RID_MAX, "long long int",
3221 long_long_integer_type_node);
3222 record_builtin_type (RID_MAX, "long long unsigned int",
3223 long_long_unsigned_type_node);
3224 if (c_language == clk_cplusplus)
3225 record_builtin_type (RID_MAX, "long long unsigned",
3226 long_long_unsigned_type_node);
3227 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
3228 record_builtin_type (RID_MAX, "short unsigned int",
3229 short_unsigned_type_node);
3230 if (c_language == clk_cplusplus)
3231 record_builtin_type (RID_MAX, "unsigned short",
3232 short_unsigned_type_node);
3234 /* Define both `signed char' and `unsigned char'. */
3235 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
3236 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
3238 /* These are types that c_common_type_for_size and
3239 c_common_type_for_mode use. */
3240 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
3241 intQI_type_node));
3242 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
3243 intHI_type_node));
3244 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
3245 intSI_type_node));
3246 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
3247 intDI_type_node));
3248 #if HOST_BITS_PER_WIDE_INT >= 64
3249 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
3250 get_identifier ("__int128_t"),
3251 intTI_type_node));
3252 #endif
3253 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
3254 unsigned_intQI_type_node));
3255 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
3256 unsigned_intHI_type_node));
3257 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
3258 unsigned_intSI_type_node));
3259 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
3260 unsigned_intDI_type_node));
3261 #if HOST_BITS_PER_WIDE_INT >= 64
3262 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
3263 get_identifier ("__uint128_t"),
3264 unsigned_intTI_type_node));
3265 #endif
3267 /* Create the widest literal types. */
3268 widest_integer_literal_type_node
3269 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
3270 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
3271 widest_integer_literal_type_node));
3273 widest_unsigned_literal_type_node
3274 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
3275 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL, NULL_TREE,
3276 widest_unsigned_literal_type_node));
3278 /* `unsigned long' is the standard type for sizeof.
3279 Note that stddef.h uses `unsigned long',
3280 and this must agree, even if long and int are the same size. */
3281 c_size_type_node =
3282 TREE_TYPE (identifier_global_value (get_identifier (SIZE_TYPE)));
3283 signed_size_type_node = c_common_signed_type (c_size_type_node);
3284 set_sizetype (c_size_type_node);
3286 build_common_tree_nodes_2 (flag_short_double);
3288 record_builtin_type (RID_FLOAT, NULL, float_type_node);
3289 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
3290 record_builtin_type (RID_MAX, "long double", long_double_type_node);
3292 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
3293 get_identifier ("complex int"),
3294 complex_integer_type_node));
3295 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
3296 get_identifier ("complex float"),
3297 complex_float_type_node));
3298 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
3299 get_identifier ("complex double"),
3300 complex_double_type_node));
3301 (*lang_hooks.decls.pushdecl)
3302 (build_decl (TYPE_DECL, get_identifier ("complex long double"),
3303 complex_long_double_type_node));
3305 /* Types which are common to the fortran compiler and libf2c. When
3306 changing these, you also need to be concerned with f/com.h. */
3308 if (TYPE_PRECISION (float_type_node)
3309 == TYPE_PRECISION (long_integer_type_node))
3311 g77_integer_type_node = long_integer_type_node;
3312 g77_uinteger_type_node = long_unsigned_type_node;
3314 else if (TYPE_PRECISION (float_type_node)
3315 == TYPE_PRECISION (integer_type_node))
3317 g77_integer_type_node = integer_type_node;
3318 g77_uinteger_type_node = unsigned_type_node;
3320 else
3321 g77_integer_type_node = g77_uinteger_type_node = NULL_TREE;
3323 if (g77_integer_type_node != NULL_TREE)
3325 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
3326 get_identifier ("__g77_integer"),
3327 g77_integer_type_node));
3328 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
3329 get_identifier ("__g77_uinteger"),
3330 g77_uinteger_type_node));
3333 if (TYPE_PRECISION (float_type_node) * 2
3334 == TYPE_PRECISION (long_integer_type_node))
3336 g77_longint_type_node = long_integer_type_node;
3337 g77_ulongint_type_node = long_unsigned_type_node;
3339 else if (TYPE_PRECISION (float_type_node) * 2
3340 == TYPE_PRECISION (long_long_integer_type_node))
3342 g77_longint_type_node = long_long_integer_type_node;
3343 g77_ulongint_type_node = long_long_unsigned_type_node;
3345 else
3346 g77_longint_type_node = g77_ulongint_type_node = NULL_TREE;
3348 if (g77_longint_type_node != NULL_TREE)
3350 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
3351 get_identifier ("__g77_longint"),
3352 g77_longint_type_node));
3353 (*lang_hooks.decls.pushdecl) (build_decl (TYPE_DECL,
3354 get_identifier ("__g77_ulongint"),
3355 g77_ulongint_type_node));
3358 record_builtin_type (RID_VOID, NULL, void_type_node);
3360 void_zero_node = build_int_2 (0, 0);
3361 TREE_TYPE (void_zero_node) = void_type_node;
3363 void_list_node = build_void_list_node ();
3365 /* Make a type to be the domain of a few array types
3366 whose domains don't really matter.
3367 200 is small enough that it always fits in size_t
3368 and large enough that it can hold most function names for the
3369 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
3370 array_domain_type = build_index_type (size_int (200));
3372 /* Make a type for arrays of characters.
3373 With luck nothing will ever really depend on the length of this
3374 array type. */
3375 char_array_type_node
3376 = build_array_type (char_type_node, array_domain_type);
3378 /* Likewise for arrays of ints. */
3379 int_array_type_node
3380 = build_array_type (integer_type_node, array_domain_type);
3382 string_type_node = build_pointer_type (char_type_node);
3383 const_string_type_node
3384 = build_pointer_type (build_qualified_type
3385 (char_type_node, TYPE_QUAL_CONST));
3387 (*targetm.init_builtins) ();
3389 /* This is special for C++ so functions can be overloaded. */
3390 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
3391 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
3392 wchar_type_size = TYPE_PRECISION (wchar_type_node);
3393 if (c_language == clk_cplusplus)
3395 if (TREE_UNSIGNED (wchar_type_node))
3396 wchar_type_node = make_unsigned_type (wchar_type_size);
3397 else
3398 wchar_type_node = make_signed_type (wchar_type_size);
3399 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
3401 else
3403 signed_wchar_type_node = c_common_signed_type (wchar_type_node);
3404 unsigned_wchar_type_node = c_common_unsigned_type (wchar_type_node);
3407 /* This is for wide string constants. */
3408 wchar_array_type_node
3409 = build_array_type (wchar_type_node, array_domain_type);
3411 wint_type_node =
3412 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
3414 intmax_type_node =
3415 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
3416 uintmax_type_node =
3417 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
3419 default_function_type = build_function_type (integer_type_node, NULL_TREE);
3420 ptrdiff_type_node
3421 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
3422 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
3424 (*lang_hooks.decls.pushdecl)
3425 (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
3426 va_list_type_node));
3428 (*lang_hooks.decls.pushdecl)
3429 (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"),
3430 ptrdiff_type_node));
3432 (*lang_hooks.decls.pushdecl)
3433 (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"),
3434 sizetype));
3436 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
3438 va_list_arg_type_node = va_list_ref_type_node =
3439 build_pointer_type (TREE_TYPE (va_list_type_node));
3441 else
3443 va_list_arg_type_node = va_list_type_node;
3444 va_list_ref_type_node = build_reference_type (va_list_type_node);
3447 #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
3448 builtin_types[(int) ENUM] = VALUE;
3449 #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
3450 builtin_types[(int) ENUM] \
3451 = build_function_type (builtin_types[(int) RETURN], \
3452 void_list_node);
3453 #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
3454 builtin_types[(int) ENUM] \
3455 = build_function_type (builtin_types[(int) RETURN], \
3456 tree_cons (NULL_TREE, \
3457 builtin_types[(int) ARG1], \
3458 void_list_node));
3459 #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
3460 builtin_types[(int) ENUM] \
3461 = build_function_type \
3462 (builtin_types[(int) RETURN], \
3463 tree_cons (NULL_TREE, \
3464 builtin_types[(int) ARG1], \
3465 tree_cons (NULL_TREE, \
3466 builtin_types[(int) ARG2], \
3467 void_list_node)));
3468 #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3469 builtin_types[(int) ENUM] \
3470 = build_function_type \
3471 (builtin_types[(int) RETURN], \
3472 tree_cons (NULL_TREE, \
3473 builtin_types[(int) ARG1], \
3474 tree_cons (NULL_TREE, \
3475 builtin_types[(int) ARG2], \
3476 tree_cons (NULL_TREE, \
3477 builtin_types[(int) ARG3], \
3478 void_list_node))));
3479 #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3480 builtin_types[(int) ENUM] \
3481 = build_function_type \
3482 (builtin_types[(int) RETURN], \
3483 tree_cons (NULL_TREE, \
3484 builtin_types[(int) ARG1], \
3485 tree_cons (NULL_TREE, \
3486 builtin_types[(int) ARG2], \
3487 tree_cons \
3488 (NULL_TREE, \
3489 builtin_types[(int) ARG3], \
3490 tree_cons (NULL_TREE, \
3491 builtin_types[(int) ARG4], \
3492 void_list_node)))));
3493 #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
3494 builtin_types[(int) ENUM] \
3495 = build_function_type (builtin_types[(int) RETURN], NULL_TREE);
3496 #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
3497 builtin_types[(int) ENUM] \
3498 = build_function_type (builtin_types[(int) RETURN], \
3499 tree_cons (NULL_TREE, \
3500 builtin_types[(int) ARG1], \
3501 NULL_TREE));
3503 #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
3504 builtin_types[(int) ENUM] \
3505 = build_function_type \
3506 (builtin_types[(int) RETURN], \
3507 tree_cons (NULL_TREE, \
3508 builtin_types[(int) ARG1], \
3509 tree_cons (NULL_TREE, \
3510 builtin_types[(int) ARG2], \
3511 NULL_TREE)));
3512 #define DEF_POINTER_TYPE(ENUM, TYPE) \
3513 builtin_types[(int) ENUM] \
3514 = build_pointer_type (builtin_types[(int) TYPE]);
3515 #include "builtin-types.def"
3516 #undef DEF_PRIMITIVE_TYPE
3517 #undef DEF_FUNCTION_TYPE_1
3518 #undef DEF_FUNCTION_TYPE_2
3519 #undef DEF_FUNCTION_TYPE_3
3520 #undef DEF_FUNCTION_TYPE_4
3521 #undef DEF_FUNCTION_TYPE_VAR_0
3522 #undef DEF_FUNCTION_TYPE_VAR_1
3523 #undef DEF_POINTER_TYPE
3525 if (!c_attrs_initialized)
3526 c_init_attributes ();
3528 #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, \
3529 BOTH_P, FALLBACK_P, NONANSI_P, ATTRS) \
3530 if (NAME) \
3532 tree decl; \
3534 if (strncmp (NAME, "__builtin_", strlen ("__builtin_")) != 0) \
3535 abort (); \
3537 if (!BOTH_P) \
3538 decl = builtin_function (NAME, builtin_types[TYPE], ENUM, \
3539 CLASS, \
3540 (FALLBACK_P \
3541 ? (NAME + strlen ("__builtin_")) \
3542 : NULL), \
3543 built_in_attributes[(int) ATTRS]); \
3544 else \
3545 decl = builtin_function_2 (NAME, \
3546 NAME + strlen ("__builtin_"), \
3547 builtin_types[TYPE], \
3548 builtin_types[LIBTYPE], \
3549 ENUM, \
3550 CLASS, \
3551 FALLBACK_P, \
3552 NONANSI_P, \
3553 built_in_attributes[(int) ATTRS]); \
3555 built_in_decls[(int) ENUM] = decl; \
3557 #include "builtins.def"
3558 #undef DEF_BUILTIN
3560 main_identifier_node = get_identifier ("main");
3563 tree
3564 build_va_arg (expr, type)
3565 tree expr, type;
3567 return build1 (VA_ARG_EXPR, type, expr);
3571 /* Linked list of disabled built-in functions. */
3573 typedef struct disabled_builtin
3575 const char *name;
3576 struct disabled_builtin *next;
3577 } disabled_builtin;
3578 static disabled_builtin *disabled_builtins = NULL;
3580 static bool builtin_function_disabled_p PARAMS ((const char *));
3582 /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
3583 begins with "__builtin_", give an error. */
3585 void
3586 disable_builtin_function (name)
3587 const char *name;
3589 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
3590 error ("cannot disable built-in function `%s'", name);
3591 else
3593 disabled_builtin *new = xmalloc (sizeof (disabled_builtin));
3594 new->name = name;
3595 new->next = disabled_builtins;
3596 disabled_builtins = new;
3601 /* Return true if the built-in function NAME has been disabled, false
3602 otherwise. */
3604 static bool
3605 builtin_function_disabled_p (name)
3606 const char *name;
3608 disabled_builtin *p;
3609 for (p = disabled_builtins; p != NULL; p = p->next)
3611 if (strcmp (name, p->name) == 0)
3612 return true;
3614 return false;
3618 /* Possibly define a builtin function with one or two names. BUILTIN_NAME
3619 is an __builtin_-prefixed name; NAME is the ordinary name; one or both
3620 of these may be NULL (though both being NULL is useless).
3621 BUILTIN_TYPE is the type of the __builtin_-prefixed function;
3622 TYPE is the type of the function with the ordinary name. These
3623 may differ if the ordinary name is declared with a looser type to avoid
3624 conflicts with headers. FUNCTION_CODE and CLASS are as for
3625 builtin_function. If LIBRARY_NAME_P is nonzero, NAME is passed as
3626 the LIBRARY_NAME parameter to builtin_function when declaring BUILTIN_NAME.
3627 If NONANSI_P is nonzero, the name NAME is treated as a non-ANSI name;
3628 ATTRS is the tree list representing the builtin's function attributes.
3629 Returns the declaration of BUILTIN_NAME, if any, otherwise
3630 the declaration of NAME. Does not declare NAME if flag_no_builtin,
3631 or if NONANSI_P and flag_no_nonansi_builtin. */
3633 static tree
3634 builtin_function_2 (builtin_name, name, builtin_type, type, function_code,
3635 class, library_name_p, nonansi_p, attrs)
3636 const char *builtin_name;
3637 const char *name;
3638 tree builtin_type;
3639 tree type;
3640 int function_code;
3641 enum built_in_class class;
3642 int library_name_p;
3643 int nonansi_p;
3644 tree attrs;
3646 tree bdecl = NULL_TREE;
3647 tree decl = NULL_TREE;
3648 if (builtin_name != 0)
3650 bdecl = builtin_function (builtin_name, builtin_type, function_code,
3651 class, library_name_p ? name : NULL,
3652 attrs);
3654 if (name != 0 && !flag_no_builtin && !builtin_function_disabled_p (name)
3655 && !(nonansi_p && flag_no_nonansi_builtin))
3657 decl = builtin_function (name, type, function_code, class, NULL,
3658 attrs);
3659 if (nonansi_p)
3660 DECL_BUILT_IN_NONANSI (decl) = 1;
3662 return (bdecl != 0 ? bdecl : decl);
3665 /* Nonzero if the type T promotes to int. This is (nearly) the
3666 integral promotions defined in ISO C99 6.3.1.1/2. */
3668 bool
3669 c_promoting_integer_type_p (t)
3670 tree t;
3672 switch (TREE_CODE (t))
3674 case INTEGER_TYPE:
3675 return (TYPE_MAIN_VARIANT (t) == char_type_node
3676 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
3677 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
3678 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
3679 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
3680 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
3682 case ENUMERAL_TYPE:
3683 /* ??? Technically all enumerations not larger than an int
3684 promote to an int. But this is used along code paths
3685 that only want to notice a size change. */
3686 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
3688 case BOOLEAN_TYPE:
3689 return 1;
3691 default:
3692 return 0;
3696 /* Return 1 if PARMS specifies a fixed number of parameters
3697 and none of their types is affected by default promotions. */
3700 self_promoting_args_p (parms)
3701 tree parms;
3703 tree t;
3704 for (t = parms; t; t = TREE_CHAIN (t))
3706 tree type = TREE_VALUE (t);
3708 if (TREE_CHAIN (t) == 0 && type != void_type_node)
3709 return 0;
3711 if (type == 0)
3712 return 0;
3714 if (TYPE_MAIN_VARIANT (type) == float_type_node)
3715 return 0;
3717 if (c_promoting_integer_type_p (type))
3718 return 0;
3720 return 1;
3723 /* Recursively examines the array elements of TYPE, until a non-array
3724 element type is found. */
3726 tree
3727 strip_array_types (type)
3728 tree type;
3730 while (TREE_CODE (type) == ARRAY_TYPE)
3731 type = TREE_TYPE (type);
3733 return type;
3736 static tree expand_unordered_cmp PARAMS ((tree, tree, enum tree_code,
3737 enum tree_code));
3739 /* Expand a call to an unordered comparison function such as
3740 __builtin_isgreater(). FUNCTION is the function's declaration and
3741 PARAMS a list of the values passed. For __builtin_isunordered(),
3742 UNORDERED_CODE is UNORDERED_EXPR and ORDERED_CODE is NOP_EXPR. In
3743 other cases, UNORDERED_CODE and ORDERED_CODE are comparison codes
3744 that give the opposite of the desired result. UNORDERED_CODE is
3745 used for modes that can hold NaNs and ORDERED_CODE is used for the
3746 rest. */
3748 static tree
3749 expand_unordered_cmp (function, params, unordered_code, ordered_code)
3750 tree function, params;
3751 enum tree_code unordered_code, ordered_code;
3753 tree arg0, arg1, type;
3754 enum tree_code code0, code1;
3756 /* Check that we have exactly two arguments. */
3757 if (params == 0 || TREE_CHAIN (params) == 0)
3759 error ("too few arguments to function `%s'",
3760 IDENTIFIER_POINTER (DECL_NAME (function)));
3761 return error_mark_node;
3763 else if (TREE_CHAIN (TREE_CHAIN (params)) != 0)
3765 error ("too many arguments to function `%s'",
3766 IDENTIFIER_POINTER (DECL_NAME (function)));
3767 return error_mark_node;
3770 arg0 = TREE_VALUE (params);
3771 arg1 = TREE_VALUE (TREE_CHAIN (params));
3773 code0 = TREE_CODE (TREE_TYPE (arg0));
3774 code1 = TREE_CODE (TREE_TYPE (arg1));
3776 /* Make sure that the arguments have a common type of REAL. */
3777 type = 0;
3778 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
3779 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
3780 type = common_type (TREE_TYPE (arg0), TREE_TYPE (arg1));
3782 if (type == 0 || TREE_CODE (type) != REAL_TYPE)
3784 error ("non-floating-point argument to function `%s'",
3785 IDENTIFIER_POINTER (DECL_NAME (function)));
3786 return error_mark_node;
3789 if (unordered_code == UNORDERED_EXPR)
3791 if (MODE_HAS_NANS (TYPE_MODE (type)))
3792 return build_binary_op (unordered_code,
3793 convert (type, arg0),
3794 convert (type, arg1),
3796 else
3797 return integer_zero_node;
3800 return build_unary_op (TRUTH_NOT_EXPR,
3801 build_binary_op (MODE_HAS_NANS (TYPE_MODE (type))
3802 ? unordered_code
3803 : ordered_code,
3804 convert (type, arg0),
3805 convert (type, arg1),
3811 /* Recognize certain built-in functions so we can make tree-codes
3812 other than CALL_EXPR. We do this when it enables fold-const.c
3813 to do something useful. */
3814 /* ??? By rights this should go in builtins.c, but only C and C++
3815 implement build_{binary,unary}_op. Not exactly sure what bits
3816 of functionality are actually needed from those functions, or
3817 where the similar functionality exists in the other front ends. */
3819 tree
3820 expand_tree_builtin (function, params, coerced_params)
3821 tree function, params, coerced_params;
3823 if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL)
3824 return NULL_TREE;
3826 switch (DECL_FUNCTION_CODE (function))
3828 case BUILT_IN_ABS:
3829 case BUILT_IN_LABS:
3830 case BUILT_IN_LLABS:
3831 case BUILT_IN_IMAXABS:
3832 case BUILT_IN_FABS:
3833 case BUILT_IN_FABSL:
3834 case BUILT_IN_FABSF:
3835 if (coerced_params == 0)
3836 return integer_zero_node;
3837 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
3839 case BUILT_IN_CONJ:
3840 case BUILT_IN_CONJF:
3841 case BUILT_IN_CONJL:
3842 if (coerced_params == 0)
3843 return integer_zero_node;
3844 return build_unary_op (CONJ_EXPR, TREE_VALUE (coerced_params), 0);
3846 case BUILT_IN_CREAL:
3847 case BUILT_IN_CREALF:
3848 case BUILT_IN_CREALL:
3849 if (coerced_params == 0)
3850 return integer_zero_node;
3851 return build_unary_op (REALPART_EXPR, TREE_VALUE (coerced_params), 0);
3853 case BUILT_IN_CIMAG:
3854 case BUILT_IN_CIMAGF:
3855 case BUILT_IN_CIMAGL:
3856 if (coerced_params == 0)
3857 return integer_zero_node;
3858 return build_unary_op (IMAGPART_EXPR, TREE_VALUE (coerced_params), 0);
3860 case BUILT_IN_ISGREATER:
3861 return expand_unordered_cmp (function, params, UNLE_EXPR, LE_EXPR);
3863 case BUILT_IN_ISGREATEREQUAL:
3864 return expand_unordered_cmp (function, params, UNLT_EXPR, LT_EXPR);
3866 case BUILT_IN_ISLESS:
3867 return expand_unordered_cmp (function, params, UNGE_EXPR, GE_EXPR);
3869 case BUILT_IN_ISLESSEQUAL:
3870 return expand_unordered_cmp (function, params, UNGT_EXPR, GT_EXPR);
3872 case BUILT_IN_ISLESSGREATER:
3873 return expand_unordered_cmp (function, params, UNEQ_EXPR, EQ_EXPR);
3875 case BUILT_IN_ISUNORDERED:
3876 return expand_unordered_cmp (function, params, UNORDERED_EXPR, NOP_EXPR);
3878 default:
3879 break;
3882 return NULL_TREE;
3885 /* Returns nonzero if CODE is the code for a statement. */
3888 statement_code_p (code)
3889 enum tree_code code;
3891 switch (code)
3893 case CLEANUP_STMT:
3894 case EXPR_STMT:
3895 case COMPOUND_STMT:
3896 case DECL_STMT:
3897 case IF_STMT:
3898 case FOR_STMT:
3899 case WHILE_STMT:
3900 case DO_STMT:
3901 case RETURN_STMT:
3902 case BREAK_STMT:
3903 case CONTINUE_STMT:
3904 case SCOPE_STMT:
3905 case SWITCH_STMT:
3906 case GOTO_STMT:
3907 case LABEL_STMT:
3908 case ASM_STMT:
3909 case FILE_STMT:
3910 case CASE_LABEL:
3911 return 1;
3913 default:
3914 if (lang_statement_code_p)
3915 return (*lang_statement_code_p) (code);
3916 return 0;
3920 /* Walk the statement tree, rooted at *tp. Apply FUNC to all the
3921 sub-trees of *TP in a pre-order traversal. FUNC is called with the
3922 DATA and the address of each sub-tree. If FUNC returns a non-NULL
3923 value, the traversal is aborted, and the value returned by FUNC is
3924 returned. If FUNC sets WALK_SUBTREES to zero, then the subtrees of
3925 the node being visited are not walked.
3927 We don't need a without_duplicates variant of this one because the
3928 statement tree is a tree, not a graph. */
3930 tree
3931 walk_stmt_tree (tp, func, data)
3932 tree *tp;
3933 walk_tree_fn func;
3934 void *data;
3936 enum tree_code code;
3937 int walk_subtrees;
3938 tree result;
3939 int i, len;
3941 #define WALK_SUBTREE(NODE) \
3942 do \
3944 result = walk_stmt_tree (&(NODE), func, data); \
3945 if (result) \
3946 return result; \
3948 while (0)
3950 /* Skip empty subtrees. */
3951 if (!*tp)
3952 return NULL_TREE;
3954 /* Skip subtrees below non-statement nodes. */
3955 if (!statement_code_p (TREE_CODE (*tp)))
3956 return NULL_TREE;
3958 /* Call the function. */
3959 walk_subtrees = 1;
3960 result = (*func) (tp, &walk_subtrees, data);
3962 /* If we found something, return it. */
3963 if (result)
3964 return result;
3966 /* FUNC may have modified the tree, recheck that we're looking at a
3967 statement node. */
3968 code = TREE_CODE (*tp);
3969 if (!statement_code_p (code))
3970 return NULL_TREE;
3972 /* Visit the subtrees unless FUNC decided that there was nothing
3973 interesting below this point in the tree. */
3974 if (walk_subtrees)
3976 /* Walk over all the sub-trees of this operand. Statement nodes
3977 never contain RTL, and we needn't worry about TARGET_EXPRs. */
3978 len = TREE_CODE_LENGTH (code);
3980 /* Go through the subtrees. We need to do this in forward order so
3981 that the scope of a FOR_EXPR is handled properly. */
3982 for (i = 0; i < len; ++i)
3983 WALK_SUBTREE (TREE_OPERAND (*tp, i));
3986 /* Finally visit the chain. This can be tail-recursion optimized if
3987 we write it this way. */
3988 return walk_stmt_tree (&TREE_CHAIN (*tp), func, data);
3990 #undef WALK_SUBTREE
3993 /* Used to compare case labels. K1 and K2 are actually tree nodes
3994 representing case labels, or NULL_TREE for a `default' label.
3995 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
3996 K2, and 0 if K1 and K2 are equal. */
3999 case_compare (k1, k2)
4000 splay_tree_key k1;
4001 splay_tree_key k2;
4003 /* Consider a NULL key (such as arises with a `default' label) to be
4004 smaller than anything else. */
4005 if (!k1)
4006 return k2 ? -1 : 0;
4007 else if (!k2)
4008 return k1 ? 1 : 0;
4010 return tree_int_cst_compare ((tree) k1, (tree) k2);
4013 /* Process a case label for the range LOW_VALUE ... HIGH_VALUE. If
4014 LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is
4015 actually a `default' label. If only HIGH_VALUE is NULL_TREE, then
4016 case label was declared using the usual C/C++ syntax, rather than
4017 the GNU case range extension. CASES is a tree containing all the
4018 case ranges processed so far; COND is the condition for the
4019 switch-statement itself. Returns the CASE_LABEL created, or
4020 ERROR_MARK_NODE if no CASE_LABEL is created. */
4022 tree
4023 c_add_case_label (cases, cond, low_value, high_value)
4024 splay_tree cases;
4025 tree cond;
4026 tree low_value;
4027 tree high_value;
4029 tree type;
4030 tree label;
4031 tree case_label;
4032 splay_tree_node node;
4034 /* Create the LABEL_DECL itself. */
4035 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
4036 DECL_CONTEXT (label) = current_function_decl;
4038 /* If there was an error processing the switch condition, bail now
4039 before we get more confused. */
4040 if (!cond || cond == error_mark_node)
4042 /* Add a label anyhow so that the back-end doesn't think that
4043 the beginning of the switch is unreachable. */
4044 if (!cases->root)
4045 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
4046 return error_mark_node;
4049 if ((low_value && TREE_TYPE (low_value)
4050 && POINTER_TYPE_P (TREE_TYPE (low_value)))
4051 || (high_value && TREE_TYPE (high_value)
4052 && POINTER_TYPE_P (TREE_TYPE (high_value))))
4053 error ("pointers are not permitted as case values");
4055 /* Case ranges are a GNU extension. */
4056 if (high_value && pedantic)
4058 if (c_language == clk_cplusplus)
4059 pedwarn ("ISO C++ forbids range expressions in switch statements");
4060 else
4061 pedwarn ("ISO C forbids range expressions in switch statements");
4064 type = TREE_TYPE (cond);
4065 if (low_value)
4067 low_value = check_case_value (low_value);
4068 low_value = convert_and_check (type, low_value);
4070 if (high_value)
4072 high_value = check_case_value (high_value);
4073 high_value = convert_and_check (type, high_value);
4076 /* If an error has occurred, bail out now. */
4077 if (low_value == error_mark_node || high_value == error_mark_node)
4079 if (!cases->root)
4080 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
4081 return error_mark_node;
4084 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
4085 really a case range, even though it was written that way. Remove
4086 the HIGH_VALUE to simplify later processing. */
4087 if (tree_int_cst_equal (low_value, high_value))
4088 high_value = NULL_TREE;
4089 if (low_value && high_value
4090 && !tree_int_cst_lt (low_value, high_value))
4091 warning ("empty range specified");
4093 /* Look up the LOW_VALUE in the table of case labels we already
4094 have. */
4095 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
4096 /* If there was not an exact match, check for overlapping ranges.
4097 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
4098 that's a `default' label and the only overlap is an exact match. */
4099 if (!node && (low_value || high_value))
4101 splay_tree_node low_bound;
4102 splay_tree_node high_bound;
4104 /* Even though there wasn't an exact match, there might be an
4105 overlap between this case range and another case range.
4106 Since we've (inductively) not allowed any overlapping case
4107 ranges, we simply need to find the greatest low case label
4108 that is smaller that LOW_VALUE, and the smallest low case
4109 label that is greater than LOW_VALUE. If there is an overlap
4110 it will occur in one of these two ranges. */
4111 low_bound = splay_tree_predecessor (cases,
4112 (splay_tree_key) low_value);
4113 high_bound = splay_tree_successor (cases,
4114 (splay_tree_key) low_value);
4116 /* Check to see if the LOW_BOUND overlaps. It is smaller than
4117 the LOW_VALUE, so there is no need to check unless the
4118 LOW_BOUND is in fact itself a case range. */
4119 if (low_bound
4120 && CASE_HIGH ((tree) low_bound->value)
4121 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
4122 low_value) >= 0)
4123 node = low_bound;
4124 /* Check to see if the HIGH_BOUND overlaps. The low end of that
4125 range is bigger than the low end of the current range, so we
4126 are only interested if the current range is a real range, and
4127 not an ordinary case label. */
4128 else if (high_bound
4129 && high_value
4130 && (tree_int_cst_compare ((tree) high_bound->key,
4131 high_value)
4132 <= 0))
4133 node = high_bound;
4135 /* If there was an overlap, issue an error. */
4136 if (node)
4138 tree duplicate = CASE_LABEL_DECL ((tree) node->value);
4140 if (high_value)
4142 error ("duplicate (or overlapping) case value");
4143 error_with_decl (duplicate,
4144 "this is the first entry overlapping that value");
4146 else if (low_value)
4148 error ("duplicate case value") ;
4149 error_with_decl (duplicate, "previously used here");
4151 else
4153 error ("multiple default labels in one switch");
4154 error_with_decl (duplicate, "this is the first default label");
4156 if (!cases->root)
4157 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
4160 /* Add a CASE_LABEL to the statement-tree. */
4161 case_label = add_stmt (build_case_label (low_value, high_value, label));
4162 /* Register this case label in the splay tree. */
4163 splay_tree_insert (cases,
4164 (splay_tree_key) low_value,
4165 (splay_tree_value) case_label);
4167 return case_label;
4170 /* Finish an expression taking the address of LABEL. Returns an
4171 expression for the address. */
4173 tree
4174 finish_label_address_expr (label)
4175 tree label;
4177 tree result;
4179 if (pedantic)
4181 if (c_language == clk_cplusplus)
4182 pedwarn ("ISO C++ forbids taking the address of a label");
4183 else
4184 pedwarn ("ISO C forbids taking the address of a label");
4187 label = lookup_label (label);
4188 if (label == NULL_TREE)
4189 result = null_pointer_node;
4190 else
4192 TREE_USED (label) = 1;
4193 result = build1 (ADDR_EXPR, ptr_type_node, label);
4194 TREE_CONSTANT (result) = 1;
4195 /* The current function in not necessarily uninlinable.
4196 Computed gotos are incompatible with inlining, but the value
4197 here could be used only in a diagnostic, for example. */
4200 return result;
4203 /* Hook used by expand_expr to expand language-specific tree codes. */
4206 c_expand_expr (exp, target, tmode, modifier)
4207 tree exp;
4208 rtx target;
4209 enum machine_mode tmode;
4210 int modifier; /* Actually enum_modifier. */
4212 switch (TREE_CODE (exp))
4214 case STMT_EXPR:
4216 tree rtl_expr;
4217 rtx result;
4218 bool preserve_result = false;
4219 bool return_target = false;
4221 /* Since expand_expr_stmt calls free_temp_slots after every
4222 expression statement, we must call push_temp_slots here.
4223 Otherwise, any temporaries in use now would be considered
4224 out-of-scope after the first EXPR_STMT from within the
4225 STMT_EXPR. */
4226 push_temp_slots ();
4227 rtl_expr = expand_start_stmt_expr (!STMT_EXPR_NO_SCOPE (exp));
4229 /* If we want the result of this expression, find the last
4230 EXPR_STMT in the COMPOUND_STMT and mark it as addressable. */
4231 if (target != const0_rtx
4232 && TREE_CODE (STMT_EXPR_STMT (exp)) == COMPOUND_STMT
4233 && TREE_CODE (COMPOUND_BODY (STMT_EXPR_STMT (exp))) == SCOPE_STMT)
4235 tree expr = COMPOUND_BODY (STMT_EXPR_STMT (exp));
4236 tree last = TREE_CHAIN (expr);
4238 while (TREE_CHAIN (last))
4240 expr = last;
4241 last = TREE_CHAIN (last);
4244 if (TREE_CODE (last) == SCOPE_STMT
4245 && TREE_CODE (expr) == EXPR_STMT)
4247 if (target && TREE_CODE (EXPR_STMT_EXPR (expr)) == VAR_DECL
4248 && DECL_RTL_IF_SET (EXPR_STMT_EXPR (expr)) == target)
4249 /* If the last expression is a variable whose RTL is the
4250 same as our target, just return the target; if it
4251 isn't valid expanding the decl would produce different
4252 RTL, and store_expr would try to do a copy. */
4253 return_target = true;
4254 else
4256 /* Otherwise, note that we want the value from the last
4257 expression. */
4258 TREE_ADDRESSABLE (expr) = 1;
4259 preserve_result = true;
4264 expand_stmt (STMT_EXPR_STMT (exp));
4265 expand_end_stmt_expr (rtl_expr);
4267 result = expand_expr (rtl_expr, target, tmode, modifier);
4268 if (return_target)
4269 result = target;
4270 else if (preserve_result && GET_CODE (result) == MEM)
4272 if (GET_MODE (result) != BLKmode)
4273 result = copy_to_reg (result);
4274 else
4275 preserve_temp_slots (result);
4278 /* If the statment-expression does not have a scope, then the
4279 new temporaries we created within it must live beyond the
4280 statement-expression. */
4281 if (STMT_EXPR_NO_SCOPE (exp))
4282 preserve_temp_slots (NULL_RTX);
4284 pop_temp_slots ();
4285 return result;
4287 break;
4289 case CALL_EXPR:
4291 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
4292 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4293 == FUNCTION_DECL)
4294 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4295 && (DECL_BUILT_IN_CLASS (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4296 == BUILT_IN_FRONTEND))
4297 return c_expand_builtin (exp, target, tmode, modifier);
4298 else
4299 abort ();
4301 break;
4303 case COMPOUND_LITERAL_EXPR:
4305 /* Initialize the anonymous variable declared in the compound
4306 literal, then return the variable. */
4307 tree decl = COMPOUND_LITERAL_EXPR_DECL (exp);
4308 emit_local_var (decl);
4309 return expand_expr (decl, target, tmode, modifier);
4312 default:
4313 abort ();
4316 abort ();
4317 return NULL;
4320 /* Hook used by safe_from_p to handle language-specific tree codes. */
4323 c_safe_from_p (target, exp)
4324 rtx target;
4325 tree exp;
4327 /* We can see statements here when processing the body of a
4328 statement-expression. For a declaration statement declaring a
4329 variable, look at the variable's initializer. */
4330 if (TREE_CODE (exp) == DECL_STMT)
4332 tree decl = DECL_STMT_DECL (exp);
4334 if (TREE_CODE (decl) == VAR_DECL
4335 && DECL_INITIAL (decl)
4336 && !safe_from_p (target, DECL_INITIAL (decl), /*top_p=*/0))
4337 return 0;
4340 /* For any statement, we must follow the statement-chain. */
4341 if (statement_code_p (TREE_CODE (exp)) && TREE_CHAIN (exp))
4342 return safe_from_p (target, TREE_CHAIN (exp), /*top_p=*/0);
4344 /* Assume everything else is safe. */
4345 return 1;
4348 /* Hook used by unsafe_for_reeval to handle language-specific tree codes. */
4351 c_common_unsafe_for_reeval (exp)
4352 tree exp;
4354 /* Statement expressions may not be reevaluated, likewise compound
4355 literals. */
4356 if (TREE_CODE (exp) == STMT_EXPR
4357 || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
4358 return 2;
4360 /* Walk all other expressions. */
4361 return -1;
4364 /* Hook used by staticp to handle language-specific tree codes. */
4367 c_staticp (exp)
4368 tree exp;
4370 if (TREE_CODE (exp) == COMPOUND_LITERAL_EXPR
4371 && TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (exp)))
4372 return 1;
4373 return 0;
4376 #define CALLED_AS_BUILT_IN(NODE) \
4377 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
4379 static rtx
4380 c_expand_builtin (exp, target, tmode, modifier)
4381 tree exp;
4382 rtx target;
4383 enum machine_mode tmode;
4384 enum expand_modifier modifier;
4386 tree type = TREE_TYPE (exp);
4387 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4388 tree arglist = TREE_OPERAND (exp, 1);
4389 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
4390 enum tree_code code = TREE_CODE (exp);
4391 const int ignore = (target == const0_rtx
4392 || ((code == NON_LVALUE_EXPR || code == NOP_EXPR
4393 || code == CONVERT_EXPR || code == REFERENCE_EXPR
4394 || code == COND_EXPR)
4395 && TREE_CODE (type) == VOID_TYPE));
4397 if (! optimize && ! CALLED_AS_BUILT_IN (fndecl))
4398 return expand_call (exp, target, ignore);
4400 switch (fcode)
4402 case BUILT_IN_PRINTF:
4403 target = c_expand_builtin_printf (arglist, target, tmode,
4404 modifier, ignore, /*unlocked=*/ 0);
4405 if (target)
4406 return target;
4407 break;
4409 case BUILT_IN_PRINTF_UNLOCKED:
4410 target = c_expand_builtin_printf (arglist, target, tmode,
4411 modifier, ignore, /*unlocked=*/ 1);
4412 if (target)
4413 return target;
4414 break;
4416 case BUILT_IN_FPRINTF:
4417 target = c_expand_builtin_fprintf (arglist, target, tmode,
4418 modifier, ignore, /*unlocked=*/ 0);
4419 if (target)
4420 return target;
4421 break;
4423 case BUILT_IN_FPRINTF_UNLOCKED:
4424 target = c_expand_builtin_fprintf (arglist, target, tmode,
4425 modifier, ignore, /*unlocked=*/ 1);
4426 if (target)
4427 return target;
4428 break;
4430 default: /* just do library call, if unknown builtin */
4431 error ("built-in function `%s' not currently supported",
4432 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
4435 /* The switch statement above can drop through to cause the function
4436 to be called normally. */
4437 return expand_call (exp, target, ignore);
4440 /* Check an arglist to *printf for problems. The arglist should start
4441 at the format specifier, with the remaining arguments immediately
4442 following it. */
4443 static int
4444 is_valid_printf_arglist (arglist)
4445 tree arglist;
4447 /* Save this value so we can restore it later. */
4448 const int SAVE_pedantic = pedantic;
4449 int diagnostic_occurred = 0;
4450 tree attrs;
4452 /* Set this to a known value so the user setting won't affect code
4453 generation. */
4454 pedantic = 1;
4455 /* Check to make sure there are no format specifier errors. */
4456 attrs = tree_cons (get_identifier ("format"),
4457 tree_cons (NULL_TREE,
4458 get_identifier ("printf"),
4459 tree_cons (NULL_TREE,
4460 integer_one_node,
4461 tree_cons (NULL_TREE,
4462 build_int_2 (2, 0),
4463 NULL_TREE))),
4464 NULL_TREE);
4465 check_function_format (&diagnostic_occurred, attrs, arglist);
4467 /* Restore the value of `pedantic'. */
4468 pedantic = SAVE_pedantic;
4470 /* If calling `check_function_format_ptr' produces a warning, we
4471 return false, otherwise we return true. */
4472 return ! diagnostic_occurred;
4475 /* If the arguments passed to printf are suitable for optimizations,
4476 we attempt to transform the call. */
4477 static rtx
4478 c_expand_builtin_printf (arglist, target, tmode, modifier, ignore, unlocked)
4479 tree arglist;
4480 rtx target;
4481 enum machine_mode tmode;
4482 enum expand_modifier modifier;
4483 int ignore;
4484 int unlocked;
4486 tree fn_putchar = unlocked ?
4487 built_in_decls[BUILT_IN_PUTCHAR_UNLOCKED] : built_in_decls[BUILT_IN_PUTCHAR];
4488 tree fn_puts = unlocked ?
4489 built_in_decls[BUILT_IN_PUTS_UNLOCKED] : built_in_decls[BUILT_IN_PUTS];
4490 tree fn, format_arg, stripped_string;
4492 /* If the return value is used, or the replacement _DECL isn't
4493 initialized, don't do the transformation. */
4494 if (!ignore || !fn_putchar || !fn_puts)
4495 return 0;
4497 /* Verify the required arguments in the original call. */
4498 if (arglist == 0
4499 || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE))
4500 return 0;
4502 /* Check the specifier vs. the parameters. */
4503 if (!is_valid_printf_arglist (arglist))
4504 return 0;
4506 format_arg = TREE_VALUE (arglist);
4507 stripped_string = format_arg;
4508 STRIP_NOPS (stripped_string);
4509 if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
4510 stripped_string = TREE_OPERAND (stripped_string, 0);
4512 /* If the format specifier isn't a STRING_CST, punt. */
4513 if (TREE_CODE (stripped_string) != STRING_CST)
4514 return 0;
4516 /* OK! We can attempt optimization. */
4518 /* If the format specifier was "%s\n", call __builtin_puts(arg2). */
4519 if (strcmp (TREE_STRING_POINTER (stripped_string), "%s\n") == 0)
4521 arglist = TREE_CHAIN (arglist);
4522 fn = fn_puts;
4524 /* If the format specifier was "%c", call __builtin_putchar (arg2). */
4525 else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0)
4527 arglist = TREE_CHAIN (arglist);
4528 fn = fn_putchar;
4530 else
4532 /* We can't handle anything else with % args or %% ... yet. */
4533 if (strchr (TREE_STRING_POINTER (stripped_string), '%'))
4534 return 0;
4536 /* If the resulting constant string has a length of 1, call
4537 putchar. Note, TREE_STRING_LENGTH includes the terminating
4538 NULL in its count. */
4539 if (TREE_STRING_LENGTH (stripped_string) == 2)
4541 /* Given printf("c"), (where c is any one character,)
4542 convert "c"[0] to an int and pass that to the replacement
4543 function. */
4544 arglist = build_int_2 (TREE_STRING_POINTER (stripped_string)[0], 0);
4545 arglist = build_tree_list (NULL_TREE, arglist);
4547 fn = fn_putchar;
4549 /* If the resulting constant was "string\n", call
4550 __builtin_puts("string"). Ensure "string" has at least one
4551 character besides the trailing \n. Note, TREE_STRING_LENGTH
4552 includes the terminating NULL in its count. */
4553 else if (TREE_STRING_LENGTH (stripped_string) > 2
4554 && TREE_STRING_POINTER (stripped_string)
4555 [TREE_STRING_LENGTH (stripped_string) - 2] == '\n')
4557 /* Create a NULL-terminated string that's one char shorter
4558 than the original, stripping off the trailing '\n'. */
4559 const int newlen = TREE_STRING_LENGTH (stripped_string) - 1;
4560 char *newstr = (char *) alloca (newlen);
4561 memcpy (newstr, TREE_STRING_POINTER (stripped_string), newlen - 1);
4562 newstr[newlen - 1] = 0;
4564 arglist = fix_string_type (build_string (newlen, newstr));
4565 arglist = build_tree_list (NULL_TREE, arglist);
4566 fn = fn_puts;
4568 else
4569 /* We'd like to arrange to call fputs(string) here, but we
4570 need stdout and don't have a way to get it ... yet. */
4571 return 0;
4574 return expand_expr (build_function_call (fn, arglist),
4575 (ignore ? const0_rtx : target),
4576 tmode, modifier);
4579 /* If the arguments passed to fprintf are suitable for optimizations,
4580 we attempt to transform the call. */
4581 static rtx
4582 c_expand_builtin_fprintf (arglist, target, tmode, modifier, ignore, unlocked)
4583 tree arglist;
4584 rtx target;
4585 enum machine_mode tmode;
4586 enum expand_modifier modifier;
4587 int ignore;
4588 int unlocked;
4590 tree fn_fputc = unlocked ?
4591 built_in_decls[BUILT_IN_FPUTC_UNLOCKED] : built_in_decls[BUILT_IN_FPUTC];
4592 tree fn_fputs = unlocked ?
4593 built_in_decls[BUILT_IN_FPUTS_UNLOCKED] : built_in_decls[BUILT_IN_FPUTS];
4594 tree fn, format_arg, stripped_string;
4596 /* If the return value is used, or the replacement _DECL isn't
4597 initialized, don't do the transformation. */
4598 if (!ignore || !fn_fputc || !fn_fputs)
4599 return 0;
4601 /* Verify the required arguments in the original call. */
4602 if (arglist == 0
4603 || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE)
4604 || (TREE_CHAIN (arglist) == 0)
4605 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist)))) !=
4606 POINTER_TYPE))
4607 return 0;
4609 /* Check the specifier vs. the parameters. */
4610 if (!is_valid_printf_arglist (TREE_CHAIN (arglist)))
4611 return 0;
4613 format_arg = TREE_VALUE (TREE_CHAIN (arglist));
4614 stripped_string = format_arg;
4615 STRIP_NOPS (stripped_string);
4616 if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
4617 stripped_string = TREE_OPERAND (stripped_string, 0);
4619 /* If the format specifier isn't a STRING_CST, punt. */
4620 if (TREE_CODE (stripped_string) != STRING_CST)
4621 return 0;
4623 /* OK! We can attempt optimization. */
4625 /* If the format specifier was "%s", call __builtin_fputs(arg3, arg1). */
4626 if (strcmp (TREE_STRING_POINTER (stripped_string), "%s") == 0)
4628 tree newarglist = build_tree_list (NULL_TREE, TREE_VALUE (arglist));
4629 arglist = tree_cons (NULL_TREE,
4630 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))),
4631 newarglist);
4632 fn = fn_fputs;
4634 /* If the format specifier was "%c", call __builtin_fputc (arg3, arg1). */
4635 else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0)
4637 tree newarglist = build_tree_list (NULL_TREE, TREE_VALUE (arglist));
4638 arglist = tree_cons (NULL_TREE,
4639 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))),
4640 newarglist);
4641 fn = fn_fputc;
4643 else
4645 /* We can't handle anything else with % args or %% ... yet. */
4646 if (strchr (TREE_STRING_POINTER (stripped_string), '%'))
4647 return 0;
4649 /* When "string" doesn't contain %, replace all cases of
4650 fprintf(stream,string) with fputs(string,stream). The fputs
4651 builtin will take take of special cases like length==1. */
4652 arglist = tree_cons (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)),
4653 build_tree_list (NULL_TREE, TREE_VALUE (arglist)));
4654 fn = fn_fputs;
4657 return expand_expr (build_function_call (fn, arglist),
4658 (ignore ? const0_rtx : target),
4659 tmode, modifier);
4663 /* Given a boolean expression ARG, return a tree representing an increment
4664 or decrement (as indicated by CODE) of ARG. The front end must check for
4665 invalid cases (e.g., decrement in C++). */
4666 tree
4667 boolean_increment (code, arg)
4668 enum tree_code code;
4669 tree arg;
4671 tree val;
4672 tree true_res = (c_language == clk_cplusplus
4673 ? boolean_true_node
4674 : c_bool_true_node);
4675 arg = stabilize_reference (arg);
4676 switch (code)
4678 case PREINCREMENT_EXPR:
4679 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4680 break;
4681 case POSTINCREMENT_EXPR:
4682 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4683 arg = save_expr (arg);
4684 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4685 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4686 break;
4687 case PREDECREMENT_EXPR:
4688 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
4689 break;
4690 case POSTDECREMENT_EXPR:
4691 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
4692 arg = save_expr (arg);
4693 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4694 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4695 break;
4696 default:
4697 abort ();
4699 TREE_SIDE_EFFECTS (val) = 1;
4700 return val;
4703 /* Define NAME with value TYPE precision. */
4704 static void
4705 builtin_define_type_precision (name, type)
4706 const char *name;
4707 tree type;
4709 builtin_define_with_int_value (name, TYPE_PRECISION (type));
4712 /* Define the float.h constants for TYPE using NAME_PREFIX and FP_SUFFIX. */
4713 static void
4714 builtin_define_float_constants (name_prefix, fp_suffix, type)
4715 const char *name_prefix;
4716 const char *fp_suffix;
4717 tree type;
4719 /* Used to convert radix-based values to base 10 values in several cases.
4721 In the max_exp -> max_10_exp conversion for 128-bit IEEE, we need at
4722 least 6 significant digits for correct results. Using the fraction
4723 formed by (log(2)*1e6)/(log(10)*1e6) overflows a 32-bit integer as an
4724 intermediate; perhaps someone can find a better approximation, in the
4725 mean time, I suspect using doubles won't harm the bootstrap here. */
4727 const double log10_2 = .30102999566398119521;
4728 double log10_b;
4729 const struct real_format *fmt;
4731 char name[64], buf[128];
4732 int dig, min_10_exp, max_10_exp;
4733 int decimal_dig;
4735 fmt = real_format_for_mode[TYPE_MODE (type) - QFmode];
4737 /* The radix of the exponent representation. */
4738 if (type == float_type_node)
4739 builtin_define_with_int_value ("__FLT_RADIX__", fmt->b);
4740 log10_b = log10_2 * fmt->log2_b;
4742 /* The number of radix digits, p, in the floating-point significand. */
4743 sprintf (name, "__%s_MANT_DIG__", name_prefix);
4744 builtin_define_with_int_value (name, fmt->p);
4746 /* The number of decimal digits, q, such that any floating-point number
4747 with q decimal digits can be rounded into a floating-point number with
4748 p radix b digits and back again without change to the q decimal digits,
4750 p log10 b if b is a power of 10
4751 floor((p - 1) log10 b) otherwise
4753 dig = (fmt->p - 1) * log10_b;
4754 sprintf (name, "__%s_DIG__", name_prefix);
4755 builtin_define_with_int_value (name, dig);
4757 /* The minimum negative int x such that b**(x-1) is a normalized float. */
4758 sprintf (name, "__%s_MIN_EXP__", name_prefix);
4759 sprintf (buf, "(%d)", fmt->emin);
4760 builtin_define_with_value (name, buf, 0);
4762 /* The minimum negative int x such that 10**x is a normalized float,
4764 ceil (log10 (b ** (emin - 1)))
4765 = ceil (log10 (b) * (emin - 1))
4767 Recall that emin is negative, so the integer truncation calculates
4768 the ceiling, not the floor, in this case. */
4769 min_10_exp = (fmt->emin - 1) * log10_b;
4770 sprintf (name, "__%s_MIN_10_EXP__", name_prefix);
4771 sprintf (buf, "(%d)", min_10_exp);
4772 builtin_define_with_value (name, buf, 0);
4774 /* The maximum int x such that b**(x-1) is a representable float. */
4775 sprintf (name, "__%s_MAX_EXP__", name_prefix);
4776 builtin_define_with_int_value (name, fmt->emax);
4778 /* The maximum int x such that 10**x is in the range of representable
4779 finite floating-point numbers,
4781 floor (log10((1 - b**-p) * b**emax))
4782 = floor (log10(1 - b**-p) + log10(b**emax))
4783 = floor (log10(1 - b**-p) + log10(b)*emax)
4785 The safest thing to do here is to just compute this number. But since
4786 we don't link cc1 with libm, we cannot. We could implement log10 here
4787 a series expansion, but that seems too much effort because:
4789 Note that the first term, for all extant p, is a number exceedingly close
4790 to zero, but slightly negative. Note that the second term is an integer
4791 scaling an irrational number, and that because of the floor we are only
4792 interested in its integral portion.
4794 In order for the first term to have any effect on the integral portion
4795 of the second term, the second term has to be exceedingly close to an
4796 integer itself (e.g. 123.000000000001 or something). Getting a result
4797 that close to an integer requires that the irrational multiplicand have
4798 a long series of zeros in its expansion, which doesn't occur in the
4799 first 20 digits or so of log10(b).
4801 Hand-waving aside, crunching all of the sets of constants above by hand
4802 does not yield a case for which the first term is significant, which
4803 in the end is all that matters. */
4804 max_10_exp = fmt->emax * log10_b;
4805 sprintf (name, "__%s_MAX_10_EXP__", name_prefix);
4806 builtin_define_with_int_value (name, max_10_exp);
4808 /* The number of decimal digits, n, such that any floating-point number
4809 can be rounded to n decimal digits and back again without change to
4810 the value.
4812 p * log10(b) if b is a power of 10
4813 ceil(1 + p * log10(b)) otherwise
4815 The only macro we care about is this number for the widest supported
4816 floating type, but we want this value for rendering constants below. */
4818 double d_decimal_dig = 1 + fmt->p * log10_b;
4819 decimal_dig = d_decimal_dig;
4820 if (decimal_dig < d_decimal_dig)
4821 decimal_dig++;
4823 if (type == long_double_type_node)
4824 builtin_define_with_int_value ("__DECIMAL_DIG__", decimal_dig);
4826 /* Since, for the supported formats, B is always a power of 2, we
4827 construct the following numbers directly as a hexadecimal
4828 constants. */
4830 /* The maximum representable finite floating-point number,
4831 (1 - b**-p) * b**emax */
4833 int i, n;
4834 char *p;
4836 strcpy (buf, "0x0.");
4837 n = fmt->p * fmt->log2_b;
4838 for (i = 0, p = buf + 4; i + 3 < n; i += 4)
4839 *p++ = 'f';
4840 if (i < n)
4841 *p++ = "08ce"[n - i];
4842 sprintf (p, "p%d", fmt->emax * fmt->log2_b);
4844 sprintf (name, "__%s_MAX__", name_prefix);
4845 builtin_define_with_hex_fp_value (name, type, decimal_dig, buf, fp_suffix);
4847 /* The minimum normalized positive floating-point number,
4848 b**(emin-1). */
4849 sprintf (name, "__%s_MIN__", name_prefix);
4850 sprintf (buf, "0x1p%d", (fmt->emin - 1) * fmt->log2_b);
4851 builtin_define_with_hex_fp_value (name, type, decimal_dig, buf, fp_suffix);
4853 /* The difference between 1 and the least value greater than 1 that is
4854 representable in the given floating point type, b**(1-p). */
4855 sprintf (name, "__%s_EPSILON__", name_prefix);
4856 sprintf (buf, "0x1p%d", (1 - fmt->p) * fmt->log2_b);
4857 builtin_define_with_hex_fp_value (name, type, decimal_dig, buf, fp_suffix);
4859 /* For C++ std::numeric_limits<T>::denorm_min. The minimum denormalized
4860 positive floating-point number, b**(emin-p). Zero for formats that
4861 don't support denormals. */
4862 sprintf (name, "__%s_DENORM_MIN__", name_prefix);
4863 if (fmt->has_denorm)
4865 sprintf (buf, "0x1p%d", (fmt->emin - fmt->p) * fmt->log2_b);
4866 builtin_define_with_hex_fp_value (name, type, decimal_dig,
4867 buf, fp_suffix);
4869 else
4871 sprintf (buf, "0.0%s", fp_suffix);
4872 builtin_define_with_value (name, buf, 0);
4876 /* Hook that registers front end and target-specific built-ins. */
4877 void
4878 cb_register_builtins (pfile)
4879 cpp_reader *pfile;
4881 /* -undef turns off target-specific built-ins. */
4882 if (flag_undef)
4883 return;
4885 if (c_language == clk_cplusplus)
4887 if (SUPPORTS_ONE_ONLY)
4888 cpp_define (pfile, "__GXX_WEAK__=1");
4889 else
4890 cpp_define (pfile, "__GXX_WEAK__=0");
4891 if (flag_exceptions)
4892 cpp_define (pfile, "__EXCEPTIONS");
4893 if (warn_deprecated)
4894 cpp_define (pfile, "__DEPRECATED");
4897 /* represents the C++ ABI version, always defined so it can be used while
4898 preprocessing C and assembler. */
4899 cpp_define (pfile, "__GXX_ABI_VERSION=102");
4901 /* libgcc needs to know this. */
4902 if (USING_SJLJ_EXCEPTIONS)
4903 cpp_define (pfile, "__USING_SJLJ_EXCEPTIONS__");
4905 /* stddef.h needs to know these. */
4906 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
4907 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
4908 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
4909 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
4911 /* limits.h needs to know these. */
4912 builtin_define_type_max ("__SCHAR_MAX__", signed_char_type_node, 0);
4913 builtin_define_type_max ("__SHRT_MAX__", short_integer_type_node, 0);
4914 builtin_define_type_max ("__INT_MAX__", integer_type_node, 0);
4915 builtin_define_type_max ("__LONG_MAX__", long_integer_type_node, 1);
4916 builtin_define_type_max ("__LONG_LONG_MAX__", long_long_integer_type_node, 2);
4918 builtin_define_type_precision ("__CHAR_BIT__", char_type_node);
4920 /* float.h needs to know these. */
4922 builtin_define_with_int_value ("__FLT_EVAL_METHOD__",
4923 TARGET_FLT_EVAL_METHOD);
4925 builtin_define_float_constants ("FLT", "F", float_type_node);
4926 builtin_define_float_constants ("DBL", "", double_type_node);
4927 builtin_define_float_constants ("LDBL", "L", long_double_type_node);
4929 /* For use in assembly language. */
4930 builtin_define_with_value ("__REGISTER_PREFIX__", REGISTER_PREFIX, 0);
4931 builtin_define_with_value ("__USER_LABEL_PREFIX__", user_label_prefix, 0);
4933 /* Misc. */
4934 builtin_define_with_value ("__VERSION__", version_string, 1);
4936 /* Other target-independent built-ins determined by command-line
4937 options. */
4938 if (optimize_size)
4939 cpp_define (pfile, "__OPTIMIZE_SIZE__");
4940 if (optimize)
4941 cpp_define (pfile, "__OPTIMIZE__");
4943 if (flag_hosted)
4944 cpp_define (pfile, "__STDC_HOSTED__=1");
4945 else
4946 cpp_define (pfile, "__STDC_HOSTED__=0");
4948 if (fast_math_flags_set_p ())
4949 cpp_define (pfile, "__FAST_MATH__");
4950 if (flag_no_inline)
4951 cpp_define (pfile, "__NO_INLINE__");
4952 if (flag_signaling_nans)
4953 cpp_define (pfile, "__SUPPORT_SNAN__");
4954 if (flag_finite_math_only)
4955 cpp_define (pfile, "__FINITE_MATH_ONLY__=1");
4956 else
4957 cpp_define (pfile, "__FINITE_MATH_ONLY__=0");
4959 if (flag_iso)
4960 cpp_define (pfile, "__STRICT_ANSI__");
4962 if (!flag_signed_char)
4963 cpp_define (pfile, "__CHAR_UNSIGNED__");
4965 if (c_language == clk_cplusplus && TREE_UNSIGNED (wchar_type_node))
4966 cpp_define (pfile, "__WCHAR_UNSIGNED__");
4968 /* Make the choice of ObjC runtime visible to source code. */
4969 if (flag_objc && flag_next_runtime)
4970 cpp_define (pfile, "__NEXT_RUNTIME__");
4972 /* A straightforward target hook doesn't work, because of problems
4973 linking that hook's body when part of non-C front ends. */
4974 # define preprocessing_asm_p() (cpp_get_options (pfile)->lang == CLK_ASM)
4975 # define preprocessing_trad_p() (cpp_get_options (pfile)->traditional)
4976 # define builtin_define(TXT) cpp_define (pfile, TXT)
4977 # define builtin_assert(TXT) cpp_assert (pfile, TXT)
4978 TARGET_CPU_CPP_BUILTINS ();
4979 TARGET_OS_CPP_BUILTINS ();
4982 /* Pass an object-like macro. If it doesn't lie in the user's
4983 namespace, defines it unconditionally. Otherwise define a version
4984 with two leading underscores, and another version with two leading
4985 and trailing underscores, and define the original only if an ISO
4986 standard was not nominated.
4988 e.g. passing "unix" defines "__unix", "__unix__" and possibly
4989 "unix". Passing "_mips" defines "__mips", "__mips__" and possibly
4990 "_mips". */
4991 void
4992 builtin_define_std (macro)
4993 const char *macro;
4995 size_t len = strlen (macro);
4996 char *buff = alloca (len + 5);
4997 char *p = buff + 2;
4998 char *q = p + len;
5000 /* prepend __ (or maybe just _) if in user's namespace. */
5001 memcpy (p, macro, len + 1);
5002 if (!( *p == '_' && (p[1] == '_' || ISUPPER (p[1]))))
5004 if (*p != '_')
5005 *--p = '_';
5006 if (p[1] != '_')
5007 *--p = '_';
5009 cpp_define (parse_in, p);
5011 /* If it was in user's namespace... */
5012 if (p != buff + 2)
5014 /* Define the macro with leading and following __. */
5015 if (q[-1] != '_')
5016 *q++ = '_';
5017 if (q[-2] != '_')
5018 *q++ = '_';
5019 *q = '\0';
5020 cpp_define (parse_in, p);
5022 /* Finally, define the original macro if permitted. */
5023 if (!flag_iso)
5024 cpp_define (parse_in, macro);
5028 /* Pass an object-like macro and a value to define it to. The third
5029 parameter says whether or not to turn the value into a string
5030 constant. */
5031 static void
5032 builtin_define_with_value (macro, expansion, is_str)
5033 const char *macro;
5034 const char *expansion;
5035 int is_str;
5037 char *buf;
5038 size_t mlen = strlen (macro);
5039 size_t elen = strlen (expansion);
5040 size_t extra = 2; /* space for an = and a NUL */
5042 if (is_str)
5043 extra += 2; /* space for two quote marks */
5045 buf = alloca (mlen + elen + extra);
5046 if (is_str)
5047 sprintf (buf, "%s=\"%s\"", macro, expansion);
5048 else
5049 sprintf (buf, "%s=%s", macro, expansion);
5051 cpp_define (parse_in, buf);
5054 /* Pass an object-like macro and an integer value to define it to. */
5055 static void
5056 builtin_define_with_int_value (macro, value)
5057 const char *macro;
5058 HOST_WIDE_INT value;
5060 char *buf;
5061 size_t mlen = strlen (macro);
5062 size_t vlen = 18;
5063 size_t extra = 2; /* space for = and NUL. */
5065 buf = alloca (mlen + vlen + extra);
5066 memcpy (buf, macro, mlen);
5067 buf[mlen] = '=';
5068 sprintf (buf + mlen + 1, HOST_WIDE_INT_PRINT_DEC, value);
5070 cpp_define (parse_in, buf);
5073 /* Pass an object-like macro a hexadecimal floating-point value. */
5074 static void
5075 builtin_define_with_hex_fp_value (macro, type, digits, hex_str, fp_suffix)
5076 const char *macro;
5077 tree type ATTRIBUTE_UNUSED;
5078 int digits;
5079 const char *hex_str;
5080 const char *fp_suffix;
5082 REAL_VALUE_TYPE real;
5083 char dec_str[64], buf[256];
5085 /* Hex values are really cool and convenient, except that they're
5086 not supported in strict ISO C90 mode. First, the "p-" sequence
5087 is not valid as part of a preprocessor number. Second, we get a
5088 pedwarn from the preprocessor, which has no context, so we can't
5089 suppress the warning with __extension__.
5091 So instead what we do is construct the number in hex (because
5092 it's easy to get the exact correct value), parse it as a real,
5093 then print it back out as decimal. */
5095 real_from_string (&real, hex_str);
5096 real_to_decimal (dec_str, &real, digits);
5098 sprintf (buf, "%s=%s%s", macro, dec_str, fp_suffix);
5099 cpp_define (parse_in, buf);
5102 /* Define MAX for TYPE based on the precision of the type, which is assumed
5103 to be signed. IS_LONG is 1 for type "long" and 2 for "long long". */
5105 static void
5106 builtin_define_type_max (macro, type, is_long)
5107 const char *macro;
5108 tree type;
5109 int is_long;
5111 const char *value;
5112 char *buf;
5113 size_t mlen, vlen, extra;
5115 /* Pre-rendering the values mean we don't have to futz with printing a
5116 multi-word decimal value. There are also a very limited number of
5117 precisions that we support, so it's really a waste of time. */
5118 switch (TYPE_PRECISION (type))
5120 case 8:
5121 value = "127";
5122 break;
5123 case 16:
5124 value = "32767";
5125 break;
5126 case 32:
5127 value = "2147483647";
5128 break;
5129 case 64:
5130 value = "9223372036854775807";
5131 break;
5132 case 128:
5133 value = "170141183460469231731687303715884105727";
5134 break;
5135 default:
5136 abort ();
5139 mlen = strlen (macro);
5140 vlen = strlen (value);
5141 extra = 2 + is_long;
5142 buf = alloca (mlen + vlen + extra);
5144 sprintf (buf, "%s=%s%s", macro, value,
5145 (is_long == 1 ? "L" : is_long == 2 ? "LL" : ""));
5147 cpp_define (parse_in, buf);
5150 static void
5151 c_init_attributes ()
5153 /* Fill in the built_in_attributes array. */
5154 #define DEF_ATTR_NULL_TREE(ENUM) \
5155 built_in_attributes[(int) ENUM] = NULL_TREE;
5156 #define DEF_ATTR_INT(ENUM, VALUE) \
5157 built_in_attributes[(int) ENUM] = build_int_2 (VALUE, VALUE < 0 ? -1 : 0);
5158 #define DEF_ATTR_IDENT(ENUM, STRING) \
5159 built_in_attributes[(int) ENUM] = get_identifier (STRING);
5160 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5161 built_in_attributes[(int) ENUM] \
5162 = tree_cons (built_in_attributes[(int) PURPOSE], \
5163 built_in_attributes[(int) VALUE], \
5164 built_in_attributes[(int) CHAIN]);
5165 #define DEF_FN_ATTR(NAME, ATTRS, PREDICATE) /* No initialization needed. */
5166 #include "builtin-attrs.def"
5167 #undef DEF_ATTR_NULL_TREE
5168 #undef DEF_ATTR_INT
5169 #undef DEF_ATTR_IDENT
5170 #undef DEF_ATTR_TREE_LIST
5171 #undef DEF_FN_ATTR
5172 c_attrs_initialized = true;
5175 /* Depending on the name of DECL, apply default attributes to it. */
5177 void
5178 c_common_insert_default_attributes (decl)
5179 tree decl;
5181 tree name = DECL_NAME (decl);
5183 if (!c_attrs_initialized)
5184 c_init_attributes ();
5186 #define DEF_ATTR_NULL_TREE(ENUM) /* Nothing needed after initialization. */
5187 #define DEF_ATTR_INT(ENUM, VALUE)
5188 #define DEF_ATTR_IDENT(ENUM, STRING)
5189 #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN)
5190 #define DEF_FN_ATTR(NAME, ATTRS, PREDICATE) \
5191 if ((PREDICATE) && name == built_in_attributes[(int) NAME]) \
5192 decl_attributes (&decl, built_in_attributes[(int) ATTRS], \
5193 ATTR_FLAG_BUILT_IN);
5194 #include "builtin-attrs.def"
5195 #undef DEF_ATTR_NULL_TREE
5196 #undef DEF_ATTR_INT
5197 #undef DEF_ATTR_IDENT
5198 #undef DEF_ATTR_TREE_LIST
5199 #undef DEF_FN_ATTR
5202 /* Output a -Wshadow warning MSGID about NAME, an IDENTIFIER_NODE, and
5203 additionally give the location of the previous declaration DECL. */
5204 void
5205 shadow_warning (msgid, name, decl)
5206 const char *msgid;
5207 tree name, decl;
5209 warning ("declaration of `%s' shadows %s", IDENTIFIER_POINTER (name), msgid);
5210 warning_with_file_and_line (DECL_SOURCE_FILE (decl),
5211 DECL_SOURCE_LINE (decl),
5212 "shadowed declaration is here");
5215 /* Attribute handlers common to C front ends. */
5217 /* Handle a "packed" attribute; arguments as in
5218 struct attribute_spec.handler. */
5220 static tree
5221 handle_packed_attribute (node, name, args, flags, no_add_attrs)
5222 tree *node;
5223 tree name;
5224 tree args ATTRIBUTE_UNUSED;
5225 int flags;
5226 bool *no_add_attrs;
5228 tree *type = NULL;
5229 if (DECL_P (*node))
5231 if (TREE_CODE (*node) == TYPE_DECL)
5232 type = &TREE_TYPE (*node);
5234 else
5235 type = node;
5237 if (type)
5239 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
5240 *type = build_type_copy (*type);
5241 TYPE_PACKED (*type) = 1;
5243 else if (TREE_CODE (*node) == FIELD_DECL)
5244 DECL_PACKED (*node) = 1;
5245 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
5246 used for DECL_REGISTER. It wouldn't mean anything anyway. */
5247 else
5249 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5250 *no_add_attrs = true;
5253 return NULL_TREE;
5256 /* Handle a "nocommon" attribute; arguments as in
5257 struct attribute_spec.handler. */
5259 static tree
5260 handle_nocommon_attribute (node, name, args, flags, no_add_attrs)
5261 tree *node;
5262 tree name;
5263 tree args ATTRIBUTE_UNUSED;
5264 int flags ATTRIBUTE_UNUSED;
5265 bool *no_add_attrs;
5267 if (TREE_CODE (*node) == VAR_DECL)
5268 DECL_COMMON (*node) = 0;
5269 else
5271 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5272 *no_add_attrs = true;
5275 return NULL_TREE;
5278 /* Handle a "common" attribute; arguments as in
5279 struct attribute_spec.handler. */
5281 static tree
5282 handle_common_attribute (node, name, args, flags, no_add_attrs)
5283 tree *node;
5284 tree name;
5285 tree args ATTRIBUTE_UNUSED;
5286 int flags ATTRIBUTE_UNUSED;
5287 bool *no_add_attrs;
5289 if (TREE_CODE (*node) == VAR_DECL)
5290 DECL_COMMON (*node) = 1;
5291 else
5293 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5294 *no_add_attrs = true;
5297 return NULL_TREE;
5300 /* Handle a "noreturn" attribute; arguments as in
5301 struct attribute_spec.handler. */
5303 static tree
5304 handle_noreturn_attribute (node, name, args, flags, no_add_attrs)
5305 tree *node;
5306 tree name;
5307 tree args ATTRIBUTE_UNUSED;
5308 int flags ATTRIBUTE_UNUSED;
5309 bool *no_add_attrs;
5311 tree type = TREE_TYPE (*node);
5313 /* See FIXME comment in c_common_attribute_table. */
5314 if (TREE_CODE (*node) == FUNCTION_DECL)
5315 TREE_THIS_VOLATILE (*node) = 1;
5316 else if (TREE_CODE (type) == POINTER_TYPE
5317 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5318 TREE_TYPE (*node)
5319 = build_pointer_type
5320 (build_type_variant (TREE_TYPE (type),
5321 TREE_READONLY (TREE_TYPE (type)), 1));
5322 else
5324 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5325 *no_add_attrs = true;
5328 return NULL_TREE;
5331 /* Handle a "noinline" attribute; arguments as in
5332 struct attribute_spec.handler. */
5334 static tree
5335 handle_noinline_attribute (node, name, args, flags, no_add_attrs)
5336 tree *node;
5337 tree name;
5338 tree args ATTRIBUTE_UNUSED;
5339 int flags ATTRIBUTE_UNUSED;
5340 bool *no_add_attrs;
5342 if (TREE_CODE (*node) == FUNCTION_DECL)
5343 DECL_UNINLINABLE (*node) = 1;
5344 else
5346 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5347 *no_add_attrs = true;
5350 return NULL_TREE;
5353 /* Handle a "always_inline" attribute; arguments as in
5354 struct attribute_spec.handler. */
5356 static tree
5357 handle_always_inline_attribute (node, name, args, flags, no_add_attrs)
5358 tree *node;
5359 tree name;
5360 tree args ATTRIBUTE_UNUSED;
5361 int flags ATTRIBUTE_UNUSED;
5362 bool *no_add_attrs;
5364 if (TREE_CODE (*node) == FUNCTION_DECL)
5366 /* Do nothing else, just set the attribute. We'll get at
5367 it later with lookup_attribute. */
5369 else
5371 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5372 *no_add_attrs = true;
5375 return NULL_TREE;
5378 /* Handle a "used" attribute; arguments as in
5379 struct attribute_spec.handler. */
5381 static tree
5382 handle_used_attribute (node, name, args, flags, no_add_attrs)
5383 tree *node;
5384 tree name;
5385 tree args ATTRIBUTE_UNUSED;
5386 int flags ATTRIBUTE_UNUSED;
5387 bool *no_add_attrs;
5389 if (TREE_CODE (*node) == FUNCTION_DECL)
5390 TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (*node))
5391 = TREE_USED (*node) = 1;
5392 else
5394 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5395 *no_add_attrs = true;
5398 return NULL_TREE;
5401 /* Handle a "unused" attribute; arguments as in
5402 struct attribute_spec.handler. */
5404 static tree
5405 handle_unused_attribute (node, name, args, flags, no_add_attrs)
5406 tree *node;
5407 tree name;
5408 tree args ATTRIBUTE_UNUSED;
5409 int flags;
5410 bool *no_add_attrs;
5412 if (DECL_P (*node))
5414 tree decl = *node;
5416 if (TREE_CODE (decl) == PARM_DECL
5417 || TREE_CODE (decl) == VAR_DECL
5418 || TREE_CODE (decl) == FUNCTION_DECL
5419 || TREE_CODE (decl) == LABEL_DECL
5420 || TREE_CODE (decl) == TYPE_DECL)
5421 TREE_USED (decl) = 1;
5422 else
5424 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5425 *no_add_attrs = true;
5428 else
5430 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
5431 *node = build_type_copy (*node);
5432 TREE_USED (*node) = 1;
5435 return NULL_TREE;
5438 /* Handle a "const" attribute; arguments as in
5439 struct attribute_spec.handler. */
5441 static tree
5442 handle_const_attribute (node, name, args, flags, no_add_attrs)
5443 tree *node;
5444 tree name;
5445 tree args ATTRIBUTE_UNUSED;
5446 int flags ATTRIBUTE_UNUSED;
5447 bool *no_add_attrs;
5449 tree type = TREE_TYPE (*node);
5451 /* See FIXME comment on noreturn in c_common_attribute_table. */
5452 if (TREE_CODE (*node) == FUNCTION_DECL)
5453 TREE_READONLY (*node) = 1;
5454 else if (TREE_CODE (type) == POINTER_TYPE
5455 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
5456 TREE_TYPE (*node)
5457 = build_pointer_type
5458 (build_type_variant (TREE_TYPE (type), 1,
5459 TREE_THIS_VOLATILE (TREE_TYPE (type))));
5460 else
5462 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5463 *no_add_attrs = true;
5466 return NULL_TREE;
5469 /* Handle a "transparent_union" attribute; arguments as in
5470 struct attribute_spec.handler. */
5472 static tree
5473 handle_transparent_union_attribute (node, name, args, flags, no_add_attrs)
5474 tree *node;
5475 tree name;
5476 tree args ATTRIBUTE_UNUSED;
5477 int flags;
5478 bool *no_add_attrs;
5480 tree decl = NULL_TREE;
5481 tree *type = NULL;
5482 int is_type = 0;
5484 if (DECL_P (*node))
5486 decl = *node;
5487 type = &TREE_TYPE (decl);
5488 is_type = TREE_CODE (*node) == TYPE_DECL;
5490 else if (TYPE_P (*node))
5491 type = node, is_type = 1;
5493 if (is_type
5494 && TREE_CODE (*type) == UNION_TYPE
5495 && (decl == 0
5496 || (TYPE_FIELDS (*type) != 0
5497 && TYPE_MODE (*type) == DECL_MODE (TYPE_FIELDS (*type)))))
5499 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
5500 *type = build_type_copy (*type);
5501 TYPE_TRANSPARENT_UNION (*type) = 1;
5503 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
5504 && TREE_CODE (*type) == UNION_TYPE
5505 && TYPE_MODE (*type) == DECL_MODE (TYPE_FIELDS (*type)))
5506 DECL_TRANSPARENT_UNION (decl) = 1;
5507 else
5509 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5510 *no_add_attrs = true;
5513 return NULL_TREE;
5516 /* Handle a "constructor" attribute; arguments as in
5517 struct attribute_spec.handler. */
5519 static tree
5520 handle_constructor_attribute (node, name, args, flags, no_add_attrs)
5521 tree *node;
5522 tree name;
5523 tree args ATTRIBUTE_UNUSED;
5524 int flags ATTRIBUTE_UNUSED;
5525 bool *no_add_attrs;
5527 tree decl = *node;
5528 tree type = TREE_TYPE (decl);
5530 if (TREE_CODE (decl) == FUNCTION_DECL
5531 && TREE_CODE (type) == FUNCTION_TYPE
5532 && decl_function_context (decl) == 0)
5534 DECL_STATIC_CONSTRUCTOR (decl) = 1;
5535 TREE_USED (decl) = 1;
5537 else
5539 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5540 *no_add_attrs = true;
5543 return NULL_TREE;
5546 /* Handle a "destructor" attribute; arguments as in
5547 struct attribute_spec.handler. */
5549 static tree
5550 handle_destructor_attribute (node, name, args, flags, no_add_attrs)
5551 tree *node;
5552 tree name;
5553 tree args ATTRIBUTE_UNUSED;
5554 int flags ATTRIBUTE_UNUSED;
5555 bool *no_add_attrs;
5557 tree decl = *node;
5558 tree type = TREE_TYPE (decl);
5560 if (TREE_CODE (decl) == FUNCTION_DECL
5561 && TREE_CODE (type) == FUNCTION_TYPE
5562 && decl_function_context (decl) == 0)
5564 DECL_STATIC_DESTRUCTOR (decl) = 1;
5565 TREE_USED (decl) = 1;
5567 else
5569 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5570 *no_add_attrs = true;
5573 return NULL_TREE;
5576 /* Handle a "mode" attribute; arguments as in
5577 struct attribute_spec.handler. */
5579 static tree
5580 handle_mode_attribute (node, name, args, flags, no_add_attrs)
5581 tree *node;
5582 tree name;
5583 tree args;
5584 int flags ATTRIBUTE_UNUSED;
5585 bool *no_add_attrs;
5587 tree type = *node;
5589 *no_add_attrs = true;
5591 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
5592 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5593 else
5595 int j;
5596 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
5597 int len = strlen (p);
5598 enum machine_mode mode = VOIDmode;
5599 tree typefm;
5601 if (len > 4 && p[0] == '_' && p[1] == '_'
5602 && p[len - 1] == '_' && p[len - 2] == '_')
5604 char *newp = (char *) alloca (len - 1);
5606 strcpy (newp, &p[2]);
5607 newp[len - 4] = '\0';
5608 p = newp;
5611 /* Change this type to have a type with the specified mode.
5612 First check for the special modes. */
5613 if (! strcmp (p, "byte"))
5614 mode = byte_mode;
5615 else if (!strcmp (p, "word"))
5616 mode = word_mode;
5617 else if (! strcmp (p, "pointer"))
5618 mode = ptr_mode;
5619 else
5620 for (j = 0; j < NUM_MACHINE_MODES; j++)
5621 if (!strcmp (p, GET_MODE_NAME (j)))
5622 mode = (enum machine_mode) j;
5624 if (mode == VOIDmode)
5625 error ("unknown machine mode `%s'", p);
5626 else if (0 == (typefm = (*lang_hooks.types.type_for_mode)
5627 (mode, TREE_UNSIGNED (type))))
5628 error ("no data type for mode `%s'", p);
5629 else
5631 /* If this is a vector, make sure we either have hardware
5632 support, or we can emulate it. */
5633 if ((GET_MODE_CLASS (mode) == MODE_VECTOR_INT
5634 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
5635 && !vector_mode_valid_p (mode))
5637 error ("unable to emulate '%s'", GET_MODE_NAME (mode));
5638 return NULL_TREE;
5641 *node = typefm;
5642 /* No need to layout the type here. The caller should do this. */
5646 return NULL_TREE;
5649 /* Handle a "section" attribute; arguments as in
5650 struct attribute_spec.handler. */
5652 static tree
5653 handle_section_attribute (node, name, args, flags, no_add_attrs)
5654 tree *node;
5655 tree name ATTRIBUTE_UNUSED;
5656 tree args;
5657 int flags ATTRIBUTE_UNUSED;
5658 bool *no_add_attrs;
5660 tree decl = *node;
5662 if (targetm.have_named_sections)
5664 if ((TREE_CODE (decl) == FUNCTION_DECL
5665 || TREE_CODE (decl) == VAR_DECL)
5666 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
5668 if (TREE_CODE (decl) == VAR_DECL
5669 && current_function_decl != NULL_TREE
5670 && ! TREE_STATIC (decl))
5672 error_with_decl (decl,
5673 "section attribute cannot be specified for local variables");
5674 *no_add_attrs = true;
5677 /* The decl may have already been given a section attribute
5678 from a previous declaration. Ensure they match. */
5679 else if (DECL_SECTION_NAME (decl) != NULL_TREE
5680 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
5681 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
5683 error_with_decl (*node,
5684 "section of `%s' conflicts with previous declaration");
5685 *no_add_attrs = true;
5687 else
5688 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
5690 else
5692 error_with_decl (*node,
5693 "section attribute not allowed for `%s'");
5694 *no_add_attrs = true;
5697 else
5699 error_with_decl (*node,
5700 "section attributes are not supported for this target");
5701 *no_add_attrs = true;
5704 return NULL_TREE;
5707 /* Handle a "aligned" attribute; arguments as in
5708 struct attribute_spec.handler. */
5710 static tree
5711 handle_aligned_attribute (node, name, args, flags, no_add_attrs)
5712 tree *node;
5713 tree name ATTRIBUTE_UNUSED;
5714 tree args;
5715 int flags;
5716 bool *no_add_attrs;
5718 tree decl = NULL_TREE;
5719 tree *type = NULL;
5720 int is_type = 0;
5721 tree align_expr = (args ? TREE_VALUE (args)
5722 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
5723 int i;
5725 if (DECL_P (*node))
5727 decl = *node;
5728 type = &TREE_TYPE (decl);
5729 is_type = TREE_CODE (*node) == TYPE_DECL;
5731 else if (TYPE_P (*node))
5732 type = node, is_type = 1;
5734 /* Strip any NOPs of any kind. */
5735 while (TREE_CODE (align_expr) == NOP_EXPR
5736 || TREE_CODE (align_expr) == CONVERT_EXPR
5737 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
5738 align_expr = TREE_OPERAND (align_expr, 0);
5740 if (TREE_CODE (align_expr) != INTEGER_CST)
5742 error ("requested alignment is not a constant");
5743 *no_add_attrs = true;
5745 else if ((i = tree_log2 (align_expr)) == -1)
5747 error ("requested alignment is not a power of 2");
5748 *no_add_attrs = true;
5750 else if (i > HOST_BITS_PER_INT - 2)
5752 error ("requested alignment is too large");
5753 *no_add_attrs = true;
5755 else if (is_type)
5757 /* If we have a TYPE_DECL, then copy the type, so that we
5758 don't accidentally modify a builtin type. See pushdecl. */
5759 if (decl && TREE_TYPE (decl) != error_mark_node
5760 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
5762 tree tt = TREE_TYPE (decl);
5763 *type = build_type_copy (*type);
5764 DECL_ORIGINAL_TYPE (decl) = tt;
5765 TYPE_NAME (*type) = decl;
5766 TREE_USED (*type) = TREE_USED (decl);
5767 TREE_TYPE (decl) = *type;
5769 else if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
5770 *type = build_type_copy (*type);
5772 TYPE_ALIGN (*type) = (1 << i) * BITS_PER_UNIT;
5773 TYPE_USER_ALIGN (*type) = 1;
5775 else if (TREE_CODE (decl) != VAR_DECL
5776 && TREE_CODE (decl) != FIELD_DECL)
5778 error_with_decl (decl,
5779 "alignment may not be specified for `%s'");
5780 *no_add_attrs = true;
5782 else
5784 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
5785 DECL_USER_ALIGN (decl) = 1;
5788 return NULL_TREE;
5791 /* Handle a "weak" attribute; arguments as in
5792 struct attribute_spec.handler. */
5794 static tree
5795 handle_weak_attribute (node, name, args, flags, no_add_attrs)
5796 tree *node;
5797 tree name ATTRIBUTE_UNUSED;
5798 tree args ATTRIBUTE_UNUSED;
5799 int flags ATTRIBUTE_UNUSED;
5800 bool *no_add_attrs ATTRIBUTE_UNUSED;
5802 declare_weak (*node);
5804 return NULL_TREE;
5807 /* Handle an "alias" attribute; arguments as in
5808 struct attribute_spec.handler. */
5810 static tree
5811 handle_alias_attribute (node, name, args, flags, no_add_attrs)
5812 tree *node;
5813 tree name;
5814 tree args;
5815 int flags ATTRIBUTE_UNUSED;
5816 bool *no_add_attrs;
5818 tree decl = *node;
5820 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
5821 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
5823 error_with_decl (decl,
5824 "`%s' defined both normally and as an alias");
5825 *no_add_attrs = true;
5827 else if (decl_function_context (decl) == 0)
5829 tree id;
5831 id = TREE_VALUE (args);
5832 if (TREE_CODE (id) != STRING_CST)
5834 error ("alias arg not a string");
5835 *no_add_attrs = true;
5836 return NULL_TREE;
5838 id = get_identifier (TREE_STRING_POINTER (id));
5839 /* This counts as a use of the object pointed to. */
5840 TREE_USED (id) = 1;
5842 if (TREE_CODE (decl) == FUNCTION_DECL)
5843 DECL_INITIAL (decl) = error_mark_node;
5844 else
5845 DECL_EXTERNAL (decl) = 0;
5847 else
5849 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5850 *no_add_attrs = true;
5853 return NULL_TREE;
5856 /* Handle an "visibility" attribute; arguments as in
5857 struct attribute_spec.handler. */
5859 static tree
5860 handle_visibility_attribute (node, name, args, flags, no_add_attrs)
5861 tree *node;
5862 tree name;
5863 tree args;
5864 int flags ATTRIBUTE_UNUSED;
5865 bool *no_add_attrs;
5867 tree decl = *node;
5869 if (decl_function_context (decl) != 0 || ! TREE_PUBLIC (decl))
5871 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5872 *no_add_attrs = true;
5874 else
5876 tree id;
5878 id = TREE_VALUE (args);
5879 if (TREE_CODE (id) != STRING_CST)
5881 error ("visibility arg not a string");
5882 *no_add_attrs = true;
5883 return NULL_TREE;
5885 if (strcmp (TREE_STRING_POINTER (id), "hidden")
5886 && strcmp (TREE_STRING_POINTER (id), "protected")
5887 && strcmp (TREE_STRING_POINTER (id), "internal"))
5889 error ("visibility arg must be one of \"hidden\", \"protected\" or \"internal\"");
5890 *no_add_attrs = true;
5891 return NULL_TREE;
5895 return NULL_TREE;
5898 /* Handle a "no_instrument_function" attribute; arguments as in
5899 struct attribute_spec.handler. */
5901 static tree
5902 handle_no_instrument_function_attribute (node, name, args, flags, no_add_attrs)
5903 tree *node;
5904 tree name;
5905 tree args ATTRIBUTE_UNUSED;
5906 int flags ATTRIBUTE_UNUSED;
5907 bool *no_add_attrs;
5909 tree decl = *node;
5911 if (TREE_CODE (decl) != FUNCTION_DECL)
5913 error_with_decl (decl,
5914 "`%s' attribute applies only to functions",
5915 IDENTIFIER_POINTER (name));
5916 *no_add_attrs = true;
5918 else if (DECL_INITIAL (decl))
5920 error_with_decl (decl,
5921 "can't set `%s' attribute after definition",
5922 IDENTIFIER_POINTER (name));
5923 *no_add_attrs = true;
5925 else
5926 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
5928 return NULL_TREE;
5931 /* Handle a "malloc" attribute; arguments as in
5932 struct attribute_spec.handler. */
5934 static tree
5935 handle_malloc_attribute (node, name, args, flags, no_add_attrs)
5936 tree *node;
5937 tree name;
5938 tree args ATTRIBUTE_UNUSED;
5939 int flags ATTRIBUTE_UNUSED;
5940 bool *no_add_attrs;
5942 if (TREE_CODE (*node) == FUNCTION_DECL)
5943 DECL_IS_MALLOC (*node) = 1;
5944 /* ??? TODO: Support types. */
5945 else
5947 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
5948 *no_add_attrs = true;
5951 return NULL_TREE;
5954 /* Handle a "no_limit_stack" attribute; arguments as in
5955 struct attribute_spec.handler. */
5957 static tree
5958 handle_no_limit_stack_attribute (node, name, args, flags, no_add_attrs)
5959 tree *node;
5960 tree name;
5961 tree args ATTRIBUTE_UNUSED;
5962 int flags ATTRIBUTE_UNUSED;
5963 bool *no_add_attrs;
5965 tree decl = *node;
5967 if (TREE_CODE (decl) != FUNCTION_DECL)
5969 error_with_decl (decl,
5970 "`%s' attribute applies only to functions",
5971 IDENTIFIER_POINTER (name));
5972 *no_add_attrs = true;
5974 else if (DECL_INITIAL (decl))
5976 error_with_decl (decl,
5977 "can't set `%s' attribute after definition",
5978 IDENTIFIER_POINTER (name));
5979 *no_add_attrs = true;
5981 else
5982 DECL_NO_LIMIT_STACK (decl) = 1;
5984 return NULL_TREE;
5987 /* Handle a "pure" attribute; arguments as in
5988 struct attribute_spec.handler. */
5990 static tree
5991 handle_pure_attribute (node, name, args, flags, no_add_attrs)
5992 tree *node;
5993 tree name;
5994 tree args ATTRIBUTE_UNUSED;
5995 int flags ATTRIBUTE_UNUSED;
5996 bool *no_add_attrs;
5998 if (TREE_CODE (*node) == FUNCTION_DECL)
5999 DECL_IS_PURE (*node) = 1;
6000 /* ??? TODO: Support types. */
6001 else
6003 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
6004 *no_add_attrs = true;
6007 return NULL_TREE;
6010 /* Handle a "deprecated" attribute; arguments as in
6011 struct attribute_spec.handler. */
6013 static tree
6014 handle_deprecated_attribute (node, name, args, flags, no_add_attrs)
6015 tree *node;
6016 tree name;
6017 tree args ATTRIBUTE_UNUSED;
6018 int flags;
6019 bool *no_add_attrs;
6021 tree type = NULL_TREE;
6022 int warn = 0;
6023 const char *what = NULL;
6025 if (DECL_P (*node))
6027 tree decl = *node;
6028 type = TREE_TYPE (decl);
6030 if (TREE_CODE (decl) == TYPE_DECL
6031 || TREE_CODE (decl) == PARM_DECL
6032 || TREE_CODE (decl) == VAR_DECL
6033 || TREE_CODE (decl) == FUNCTION_DECL
6034 || TREE_CODE (decl) == FIELD_DECL)
6035 TREE_DEPRECATED (decl) = 1;
6036 else
6037 warn = 1;
6039 else if (TYPE_P (*node))
6041 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
6042 *node = build_type_copy (*node);
6043 TREE_DEPRECATED (*node) = 1;
6044 type = *node;
6046 else
6047 warn = 1;
6049 if (warn)
6051 *no_add_attrs = true;
6052 if (type && TYPE_NAME (type))
6054 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
6055 what = IDENTIFIER_POINTER (TYPE_NAME (*node));
6056 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
6057 && DECL_NAME (TYPE_NAME (type)))
6058 what = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
6060 if (what)
6061 warning ("`%s' attribute ignored for `%s'",
6062 IDENTIFIER_POINTER (name), what);
6063 else
6064 warning ("`%s' attribute ignored",
6065 IDENTIFIER_POINTER (name));
6068 return NULL_TREE;
6071 /* Keep a list of vector type nodes we created in handle_vector_size_attribute,
6072 to prevent us from duplicating type nodes unnecessarily.
6073 The normal mechanism to prevent duplicates is to use type_hash_canon, but
6074 since we want to distinguish types that are essentially identical (except
6075 for their debug representation), we use a local list here. */
6076 static tree vector_type_node_list = 0;
6078 /* Handle a "vector_size" attribute; arguments as in
6079 struct attribute_spec.handler. */
6081 static tree
6082 handle_vector_size_attribute (node, name, args, flags, no_add_attrs)
6083 tree *node;
6084 tree name;
6085 tree args;
6086 int flags ATTRIBUTE_UNUSED;
6087 bool *no_add_attrs;
6089 unsigned HOST_WIDE_INT vecsize, nunits;
6090 enum machine_mode mode, orig_mode, new_mode;
6091 tree type = *node, new_type = NULL_TREE;
6092 tree type_list_node;
6094 *no_add_attrs = true;
6096 if (! host_integerp (TREE_VALUE (args), 1))
6098 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
6099 return NULL_TREE;
6102 /* Get the vector size (in bytes). */
6103 vecsize = tree_low_cst (TREE_VALUE (args), 1);
6105 /* We need to provide for vector pointers, vector arrays, and
6106 functions returning vectors. For example:
6108 __attribute__((vector_size(16))) short *foo;
6110 In this case, the mode is SI, but the type being modified is
6111 HI, so we need to look further. */
6113 while (POINTER_TYPE_P (type)
6114 || TREE_CODE (type) == FUNCTION_TYPE
6115 || TREE_CODE (type) == ARRAY_TYPE)
6116 type = TREE_TYPE (type);
6118 /* Get the mode of the type being modified. */
6119 orig_mode = TYPE_MODE (type);
6121 if (TREE_CODE (type) == RECORD_TYPE
6122 || (GET_MODE_CLASS (orig_mode) != MODE_FLOAT
6123 && GET_MODE_CLASS (orig_mode) != MODE_INT)
6124 || ! host_integerp (TYPE_SIZE_UNIT (type), 1))
6126 error ("invalid vector type for attribute `%s'",
6127 IDENTIFIER_POINTER (name));
6128 return NULL_TREE;
6131 /* Calculate how many units fit in the vector. */
6132 nunits = vecsize / tree_low_cst (TYPE_SIZE_UNIT (type), 1);
6134 /* Find a suitably sized vector. */
6135 new_mode = VOIDmode;
6136 for (mode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_mode) == MODE_INT
6137 ? MODE_VECTOR_INT
6138 : MODE_VECTOR_FLOAT);
6139 mode != VOIDmode;
6140 mode = GET_MODE_WIDER_MODE (mode))
6141 if (vecsize == GET_MODE_SIZE (mode)
6142 && nunits == (unsigned HOST_WIDE_INT) GET_MODE_NUNITS (mode))
6144 new_mode = mode;
6145 break;
6148 if (new_mode == VOIDmode)
6150 error ("no vector mode with the size and type specified could be found");
6151 return NULL_TREE;
6154 for (type_list_node = vector_type_node_list; type_list_node;
6155 type_list_node = TREE_CHAIN (type_list_node))
6157 tree other_type = TREE_VALUE (type_list_node);
6158 tree record = TYPE_DEBUG_REPRESENTATION_TYPE (other_type);
6159 tree fields = TYPE_FIELDS (record);
6160 tree field_type = TREE_TYPE (fields);
6161 tree array_type = TREE_TYPE (field_type);
6162 if (TREE_CODE (fields) != FIELD_DECL
6163 || TREE_CODE (field_type) != ARRAY_TYPE)
6164 abort ();
6166 if (TYPE_MODE (other_type) == mode && type == array_type)
6168 new_type = other_type;
6169 break;
6173 if (new_type == NULL_TREE)
6175 tree index, array, rt, list_node;
6177 new_type = (*lang_hooks.types.type_for_mode) (new_mode,
6178 TREE_UNSIGNED (type));
6180 if (!new_type)
6182 error ("no vector mode with the size and type specified could be found");
6183 return NULL_TREE;
6186 new_type = build_type_copy (new_type);
6188 /* If this is a vector, make sure we either have hardware
6189 support, or we can emulate it. */
6190 if ((GET_MODE_CLASS (mode) == MODE_VECTOR_INT
6191 || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT)
6192 && !vector_mode_valid_p (mode))
6194 error ("unable to emulate '%s'", GET_MODE_NAME (mode));
6195 return NULL_TREE;
6198 /* Set the debug information here, because this is the only
6199 place where we know the underlying type for a vector made
6200 with vector_size. For debugging purposes we pretend a vector
6201 is an array within a structure. */
6202 index = build_int_2 (TYPE_VECTOR_SUBPARTS (new_type) - 1, 0);
6203 array = build_array_type (type, build_index_type (index));
6204 rt = make_node (RECORD_TYPE);
6206 TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
6207 DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
6208 layout_type (rt);
6209 TYPE_DEBUG_REPRESENTATION_TYPE (new_type) = rt;
6211 list_node = build_tree_list (NULL, new_type);
6212 TREE_CHAIN (list_node) = vector_type_node_list;
6213 vector_type_node_list = list_node;
6216 /* Build back pointers if needed. */
6217 *node = vector_size_helper (*node, new_type);
6219 return NULL_TREE;
6222 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
6223 better way.
6225 If we requested a pointer to a vector, build up the pointers that
6226 we stripped off while looking for the inner type. Similarly for
6227 return values from functions.
6229 The argument "type" is the top of the chain, and "bottom" is the
6230 new type which we will point to. */
6232 static tree
6233 vector_size_helper (type, bottom)
6234 tree type, bottom;
6236 tree inner, outer;
6238 if (POINTER_TYPE_P (type))
6240 inner = vector_size_helper (TREE_TYPE (type), bottom);
6241 outer = build_pointer_type (inner);
6243 else if (TREE_CODE (type) == ARRAY_TYPE)
6245 inner = vector_size_helper (TREE_TYPE (type), bottom);
6246 outer = build_array_type (inner, TYPE_VALUES (type));
6248 else if (TREE_CODE (type) == FUNCTION_TYPE)
6250 inner = vector_size_helper (TREE_TYPE (type), bottom);
6251 outer = build_function_type (inner, TYPE_VALUES (type));
6253 else
6254 return bottom;
6256 TREE_READONLY (outer) = TREE_READONLY (type);
6257 TREE_THIS_VOLATILE (outer) = TREE_THIS_VOLATILE (type);
6259 return outer;
6262 /* Handle the "nonnull" attribute. */
6263 static tree
6264 handle_nonnull_attribute (node, name, args, flags, no_add_attrs)
6265 tree *node;
6266 tree name ATTRIBUTE_UNUSED;
6267 tree args;
6268 int flags ATTRIBUTE_UNUSED;
6269 bool *no_add_attrs;
6271 tree type = *node;
6272 unsigned HOST_WIDE_INT attr_arg_num;
6274 /* If no arguments are specified, all pointer arguments should be
6275 non-null. Veryify a full prototype is given so that the arguments
6276 will have the correct types when we actually check them later. */
6277 if (! args)
6279 if (! TYPE_ARG_TYPES (type))
6281 error ("nonnull attribute without arguments on a non-prototype");
6282 *no_add_attrs = true;
6284 return NULL_TREE;
6287 /* Argument list specified. Verify that each argument number references
6288 a pointer argument. */
6289 for (attr_arg_num = 1; args; args = TREE_CHAIN (args))
6291 tree argument;
6292 unsigned HOST_WIDE_INT arg_num, ck_num;
6294 if (! get_nonnull_operand (TREE_VALUE (args), &arg_num))
6296 error ("nonnull argument has invalid operand number (arg %lu)",
6297 (unsigned long) attr_arg_num);
6298 *no_add_attrs = true;
6299 return NULL_TREE;
6302 argument = TYPE_ARG_TYPES (type);
6303 if (argument)
6305 for (ck_num = 1; ; ck_num++)
6307 if (! argument || ck_num == arg_num)
6308 break;
6309 argument = TREE_CHAIN (argument);
6312 if (! argument
6313 || TREE_CODE (TREE_VALUE (argument)) == VOID_TYPE)
6315 error ("nonnull argument with out-of-range operand number (arg %lu, operand %lu)",
6316 (unsigned long) attr_arg_num, (unsigned long) arg_num);
6317 *no_add_attrs = true;
6318 return NULL_TREE;
6321 if (TREE_CODE (TREE_VALUE (argument)) != POINTER_TYPE)
6323 error ("nonnull argument references non-pointer operand (arg %lu, operand %lu)",
6324 (unsigned long) attr_arg_num, (unsigned long) arg_num);
6325 *no_add_attrs = true;
6326 return NULL_TREE;
6331 return NULL_TREE;
6334 /* Check the argument list of a function call for null in argument slots
6335 that are marked as requiring a non-null pointer argument. */
6337 static void
6338 check_function_nonnull (attrs, params)
6339 tree attrs;
6340 tree params;
6342 tree a, args, param;
6343 int param_num;
6345 for (a = attrs; a; a = TREE_CHAIN (a))
6347 if (is_attribute_p ("nonnull", TREE_PURPOSE (a)))
6349 args = TREE_VALUE (a);
6351 /* Walk the argument list. If we encounter an argument number we
6352 should check for non-null, do it. If the attribute has no args,
6353 then every pointer argument is checked (in which case the check
6354 for pointer type is done in check_nonnull_arg). */
6355 for (param = params, param_num = 1; ;
6356 param_num++, param = TREE_CHAIN (param))
6358 if (! param)
6359 break;
6360 if (! args || nonnull_check_p (args, param_num))
6361 check_function_arguments_recurse (check_nonnull_arg, NULL,
6362 TREE_VALUE (param),
6363 param_num);
6369 /* Helper for check_function_nonnull; given a list of operands which
6370 must be non-null in ARGS, determine if operand PARAM_NUM should be
6371 checked. */
6373 static bool
6374 nonnull_check_p (args, param_num)
6375 tree args;
6376 unsigned HOST_WIDE_INT param_num;
6378 unsigned HOST_WIDE_INT arg_num;
6380 for (; args; args = TREE_CHAIN (args))
6382 if (! get_nonnull_operand (TREE_VALUE (args), &arg_num))
6383 abort ();
6385 if (arg_num == param_num)
6386 return true;
6388 return false;
6391 /* Check that the function argument PARAM (which is operand number
6392 PARAM_NUM) is non-null. This is called by check_function_nonnull
6393 via check_function_arguments_recurse. */
6395 static void
6396 check_nonnull_arg (ctx, param, param_num)
6397 void *ctx ATTRIBUTE_UNUSED;
6398 tree param;
6399 unsigned HOST_WIDE_INT param_num;
6401 /* Just skip checking the argument if it's not a pointer. This can
6402 happen if the "nonnull" attribute was given without an operand
6403 list (which means to check every pointer argument). */
6405 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
6406 return;
6408 if (integer_zerop (param))
6409 warning ("null argument where non-null required (arg %lu)",
6410 (unsigned long) param_num);
6413 /* Helper for nonnull attribute handling; fetch the operand number
6414 from the attribute argument list. */
6416 static bool
6417 get_nonnull_operand (arg_num_expr, valp)
6418 tree arg_num_expr;
6419 unsigned HOST_WIDE_INT *valp;
6421 /* Strip any conversions from the arg number and verify they
6422 are constants. */
6423 while (TREE_CODE (arg_num_expr) == NOP_EXPR
6424 || TREE_CODE (arg_num_expr) == CONVERT_EXPR
6425 || TREE_CODE (arg_num_expr) == NON_LVALUE_EXPR)
6426 arg_num_expr = TREE_OPERAND (arg_num_expr, 0);
6428 if (TREE_CODE (arg_num_expr) != INTEGER_CST
6429 || TREE_INT_CST_HIGH (arg_num_expr) != 0)
6430 return false;
6432 *valp = TREE_INT_CST_LOW (arg_num_expr);
6433 return true;
6436 /* Handle a "nothrow" attribute; arguments as in
6437 struct attribute_spec.handler. */
6439 static tree
6440 handle_nothrow_attribute (node, name, args, flags, no_add_attrs)
6441 tree *node;
6442 tree name;
6443 tree args ATTRIBUTE_UNUSED;
6444 int flags ATTRIBUTE_UNUSED;
6445 bool *no_add_attrs;
6447 if (TREE_CODE (*node) == FUNCTION_DECL)
6448 TREE_NOTHROW (*node) = 1;
6449 /* ??? TODO: Support types. */
6450 else
6452 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
6453 *no_add_attrs = true;
6456 return NULL_TREE;
6459 /* Check for valid arguments being passed to a function. */
6460 void
6461 check_function_arguments (attrs, params)
6462 tree attrs;
6463 tree params;
6465 /* Check for null being passed in a pointer argument that must be
6466 non-null. We also need to do this if format checking is enabled. */
6468 if (warn_nonnull)
6469 check_function_nonnull (attrs, params);
6471 /* Check for errors in format strings. */
6473 if (warn_format)
6474 check_function_format (NULL, attrs, params);
6477 /* Generic argument checking recursion routine. PARAM is the argument to
6478 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
6479 once the argument is resolved. CTX is context for the callback. */
6480 void
6481 check_function_arguments_recurse (callback, ctx, param, param_num)
6482 void (*callback) PARAMS ((void *, tree, unsigned HOST_WIDE_INT));
6483 void *ctx;
6484 tree param;
6485 unsigned HOST_WIDE_INT param_num;
6487 if (TREE_CODE (param) == NOP_EXPR)
6489 /* Strip coercion. */
6490 check_function_arguments_recurse (callback, ctx,
6491 TREE_OPERAND (param, 0), param_num);
6492 return;
6495 if (TREE_CODE (param) == CALL_EXPR)
6497 tree type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (param, 0)));
6498 tree attrs;
6499 bool found_format_arg = false;
6501 /* See if this is a call to a known internationalization function
6502 that modifies a format arg. Such a function may have multiple
6503 format_arg attributes (for example, ngettext). */
6505 for (attrs = TYPE_ATTRIBUTES (type);
6506 attrs;
6507 attrs = TREE_CHAIN (attrs))
6508 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
6510 tree inner_args;
6511 tree format_num_expr;
6512 int format_num;
6513 int i;
6515 /* Extract the argument number, which was previously checked
6516 to be valid. */
6517 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
6518 while (TREE_CODE (format_num_expr) == NOP_EXPR
6519 || TREE_CODE (format_num_expr) == CONVERT_EXPR
6520 || TREE_CODE (format_num_expr) == NON_LVALUE_EXPR)
6521 format_num_expr = TREE_OPERAND (format_num_expr, 0);
6523 if (TREE_CODE (format_num_expr) != INTEGER_CST
6524 || TREE_INT_CST_HIGH (format_num_expr) != 0)
6525 abort ();
6527 format_num = TREE_INT_CST_LOW (format_num_expr);
6529 for (inner_args = TREE_OPERAND (param, 1), i = 1;
6530 inner_args != 0;
6531 inner_args = TREE_CHAIN (inner_args), i++)
6532 if (i == format_num)
6534 check_function_arguments_recurse (callback, ctx,
6535 TREE_VALUE (inner_args),
6536 param_num);
6537 found_format_arg = true;
6538 break;
6542 /* If we found a format_arg attribute and did a recursive check,
6543 we are done with checking this argument. Otherwise, we continue
6544 and this will be considered a non-literal. */
6545 if (found_format_arg)
6546 return;
6549 if (TREE_CODE (param) == COND_EXPR)
6551 /* Check both halves of the conditional expression. */
6552 check_function_arguments_recurse (callback, ctx,
6553 TREE_OPERAND (param, 1), param_num);
6554 check_function_arguments_recurse (callback, ctx,
6555 TREE_OPERAND (param, 2), param_num);
6556 return;
6559 (*callback) (ctx, param, param_num);
6562 #include "gt-c-common.h"