]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-common.c
[PATCH][PR tree-optimization/68198] Avoid CFG explosion due to threading
[thirdparty/gcc.git] / gcc / c-family / c-common.c
CommitLineData
b0fc3e72 1/* Subroutines shared by all languages that are variants of C.
d353bf18 2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
b0fc3e72 3
f12b58b3 4This file is part of GCC.
b0fc3e72 5
f12b58b3 6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8c4c00c1 8Software Foundation; either version 3, or (at your option) any later
f12b58b3 9version.
b0fc3e72 10
f12b58b3 11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
b0fc3e72 15
16You should have received a copy of the GNU General Public License
8c4c00c1 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
b0fc3e72 19
b20a8bb4 20#define GCC_C_COMMON_C
21
b0fc3e72 22#include "config.h"
405711de 23#include "system.h"
805e22b2 24#include "coretypes.h"
4cba6f60 25#include "target.h"
26#include "function.h"
4cba6f60 27#include "tree.h"
7ff8db31 28#include "c-common.h"
4cba6f60 29#include "gimple-expr.h"
30#include "tm_p.h"
31#include "stringpool.h"
32#include "cgraph.h"
33#include "diagnostic.h"
e48d0f41 34#include "intl.h"
9ed99284 35#include "stor-layout.h"
36#include "calls.h"
9ed99284 37#include "attribs.h"
38#include "varasm.h"
39#include "trans-mem.h"
6c536c4f 40#include "c-objc.h"
218e3e4e 41#include "common/common-target.h"
96554925 42#include "langhooks.h"
f3dde807 43#include "tree-inline.h"
69579044 44#include "toplev.h"
4ee9c684 45#include "tree-iterator.h"
e08bd2f4 46#include "opts.h"
a8783bee 47#include "gimplify.h"
fd6f6435 48
90cc7820 49cpp_reader *parse_in; /* Declared in c-pragma.h. */
a654e028 50
de801c28 51/* Mode used to build pointers (VOIDmode means ptr_mode). */
52
53machine_mode c_default_pointer_mode = VOIDmode;
54
72040e7e 55/* The following symbols are subsumed in the c_global_trees array, and
44e9fa65 56 listed here individually for documentation purposes.
72040e7e 57
58 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
59
60 tree short_integer_type_node;
61 tree long_integer_type_node;
62 tree long_long_integer_type_node;
63
64 tree short_unsigned_type_node;
65 tree long_unsigned_type_node;
66 tree long_long_unsigned_type_node;
67
3c2239cf 68 tree truthvalue_type_node;
69 tree truthvalue_false_node;
70 tree truthvalue_true_node;
72040e7e 71
72 tree ptrdiff_type_node;
73
74 tree unsigned_char_type_node;
75 tree signed_char_type_node;
76 tree wchar_type_node;
72040e7e 77
924bbf02 78 tree char16_type_node;
79 tree char32_type_node;
80
72040e7e 81 tree float_type_node;
82 tree double_type_node;
83 tree long_double_type_node;
84
85 tree complex_integer_type_node;
86 tree complex_float_type_node;
87 tree complex_double_type_node;
88 tree complex_long_double_type_node;
89
c4503c0a 90 tree dfloat32_type_node;
91 tree dfloat64_type_node;
92 tree_dfloat128_type_node;
93
72040e7e 94 tree intQI_type_node;
95 tree intHI_type_node;
96 tree intSI_type_node;
97 tree intDI_type_node;
98 tree intTI_type_node;
99
100 tree unsigned_intQI_type_node;
101 tree unsigned_intHI_type_node;
102 tree unsigned_intSI_type_node;
103 tree unsigned_intDI_type_node;
104 tree unsigned_intTI_type_node;
105
106 tree widest_integer_literal_type_node;
107 tree widest_unsigned_literal_type_node;
108
109 Nodes for types `void *' and `const void *'.
110
111 tree ptr_type_node, const_ptr_type_node;
112
113 Nodes for types `char *' and `const char *'.
114
115 tree string_type_node, const_string_type_node;
116
117 Type `char[SOMENUMBER]'.
118 Used when an array of char is needed and the size is irrelevant.
119
120 tree char_array_type_node;
121
72040e7e 122 Type `wchar_t[SOMENUMBER]' or something like it.
123 Used when a wide string literal is created.
124
125 tree wchar_array_type_node;
126
924bbf02 127 Type `char16_t[SOMENUMBER]' or something like it.
128 Used when a UTF-16 string literal is created.
129
130 tree char16_array_type_node;
131
132 Type `char32_t[SOMENUMBER]' or something like it.
133 Used when a UTF-32 string literal is created.
134
135 tree char32_array_type_node;
136
72040e7e 137 Type `int ()' -- used for implicit declaration of functions.
138
139 tree default_function_type;
140
72040e7e 141 A VOID_TYPE node, packaged in a TREE_LIST.
142
143 tree void_list_node;
144
734c98be 145 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
65b7f83f 146 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
147 VAR_DECLS, but C++ does.)
71d9fc9b 148
65b7f83f 149 tree function_name_decl_node;
734c98be 150 tree pretty_function_name_decl_node;
65b7f83f 151 tree c99_function_name_decl_node;
152
153 Stack of nested function name VAR_DECLs.
1cae46be 154
65b7f83f 155 tree saved_function_name_decls;
71d9fc9b 156
72040e7e 157*/
158
159tree c_global_trees[CTI_MAX];
573aba85 160\f
574a6990 161/* Switches common to the C front ends. */
162
f7070933 163/* Nonzero means don't output line number information. */
164
165char flag_no_line_commands;
166
167/* Nonzero causes -E output not to be done, but directives such as
168 #define that have side effects are still obeyed. */
169
170char flag_no_output;
171
172/* Nonzero means dump macros in some fashion. */
173
174char flag_dump_macros;
175
176/* Nonzero means pass #include lines through to the output. */
177
178char flag_dump_includes;
179
d718b525 180/* Nonzero means process PCH files while preprocessing. */
181
182bool flag_pch_preprocess;
183
573aba85 184/* The file name to which we should write a precompiled header, or
185 NULL if no header will be written in this compile. */
186
187const char *pch_file;
188
1ed9d5f5 189/* Nonzero if an ISO standard was selected. It rejects macros in the
190 user's namespace. */
191int flag_iso;
192
574a6990 193/* C/ObjC language option variables. */
194
195
574a6990 196/* Nonzero means allow type mismatches in conditional expressions;
197 just make their values `void'. */
198
199int flag_cond_mismatch;
200
201/* Nonzero means enable C89 Amendment 1 features. */
202
203int flag_isoc94;
204
32074525 205/* Nonzero means use the ISO C99 (or C11) dialect of C. */
574a6990 206
207int flag_isoc99;
208
32074525 209/* Nonzero means use the ISO C11 dialect of C. */
39012afb 210
32074525 211int flag_isoc11;
39012afb 212
8b332087 213/* Nonzero means that we have builtin functions, and main is an int. */
574a6990 214
215int flag_hosted = 1;
216
574a6990 217
218/* ObjC language option variables. */
219
220
574a6990 221/* Tells the compiler that this is a special run. Do not perform any
222 compiling, instead we are to test some platform dependent features
223 and output a C header file with appropriate definitions. */
224
225int print_struct_values;
226
f0b5f617 227/* Tells the compiler what is the constant string class for ObjC. */
574a6990 228
229const char *constant_string_class_name;
230
574a6990 231
232/* C++ language option variables. */
233
229a58b1 234/* The reference version of the ABI for -Wabi. */
235
236int warn_abi_version = -1;
574a6990 237
574a6990 238/* Nonzero means generate separate instantiation control files and
239 juggle them at link time. */
240
241int flag_use_repository;
242
d875b9d2 243/* The C++ dialect being used. Default set in c_common_post_options. */
0fe6eeac 244
d875b9d2 245enum cxx_dialect cxx_dialect = cxx_unset;
0fe6eeac 246
9dcd0d49 247/* Maximum template instantiation depth. This limit exists to limit the
8ce59854 248 time it takes to notice excessively recursive template instantiations.
574a6990 249
8ce59854 250 The default is lower than the 1024 recommended by the C++0x standard
251 because G++ runs out of stack before 1024 with highly recursive template
252 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
253
254int max_tinst_depth = 900;
574a6990 255
988fc1d1 256/* The elements of `ridpointers' are identifier nodes for the reserved
257 type names and storage classes. It is indexed by a RID_... value. */
258tree *ridpointers;
259
e60a6f7b 260tree (*make_fname_decl) (location_t, tree, int);
9e5a737d 261
48d94ede 262/* Nonzero means don't warn about problems that occur when the code is
263 executed. */
264int c_inhibit_evaluation_warnings;
e78703c1 265
93be21c0 266/* Whether we are building a boolean conversion inside
267 convert_for_assignment, or some other late binary operation. If
268 build_binary_op is called for C (from code shared by C and C++) in
269 this case, then the operands have already been folded and the
270 result will not be folded again, so C_MAYBE_CONST_EXPR should not
271 be generated. */
272bool in_late_binary_op;
273
7f5f3953 274/* Whether lexing has been completed, so subsequent preprocessor
275 errors should use the compiler's input_location. */
276bool done_lexing = false;
277
2c0e001b 278/* Information about how a function name is generated. */
65b7f83f 279struct fname_var_t
280{
e99c3a1d 281 tree *const decl; /* pointer to the VAR_DECL. */
282 const unsigned rid; /* RID number for the identifier. */
283 const int pretty; /* How pretty is it? */
65b7f83f 284};
285
2c0e001b 286/* The three ways of getting then name of the current function. */
65b7f83f 287
288const struct fname_var_t fname_vars[] =
289{
2c0e001b 290 /* C99 compliant __func__, must be first. */
65b7f83f 291 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
2c0e001b 292 /* GCC __FUNCTION__ compliant. */
65b7f83f 293 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
2c0e001b 294 /* GCC __PRETTY_FUNCTION__ compliant. */
65b7f83f 295 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
296 {NULL, 0, 0},
297};
298
79396169 299/* Global visibility options. */
300struct visibility_flags visibility_options;
301
aac24642 302static tree c_fully_fold_internal (tree expr, bool, bool *, bool *, bool);
2d2f6a15 303static tree check_case_value (location_t, tree);
be23b16f 304static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
305 bool *);
be43ff5a 306
1cae46be 307static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
308static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
309static tree handle_common_attribute (tree *, tree, tree, int, bool *);
310static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
5de92639 311static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
312static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
a9196da9 313static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
314 int, bool *);
d413ffdd 315static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
316 int, bool *);
05f893e1 317static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
318 bool *);
947aa916 319static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
1cae46be 320static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
bdb1f0d1 321static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
85fbea97 322static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
7bd95dfd 323static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
1cae46be 324static tree handle_always_inline_attribute (tree *, tree, tree, int,
325 bool *);
1b16fc45 326static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
327static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
0cdd9887 328static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
10fc867f 329static tree handle_error_attribute (tree *, tree, tree, int, bool *);
1cae46be 330static tree handle_used_attribute (tree *, tree, tree, int, bool *);
331static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
62eec3b4 332static tree handle_externally_visible_attribute (tree *, tree, tree, int,
333 bool *);
6b722052 334static tree handle_no_reorder_attribute (tree *, tree, tree, int,
335 bool *);
1cae46be 336static tree handle_const_attribute (tree *, tree, tree, int, bool *);
337static tree handle_transparent_union_attribute (tree *, tree, tree,
338 int, bool *);
292237f3 339static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
340 int, bool *);
1cae46be 341static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
342static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
343static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
344static tree handle_section_attribute (tree *, tree, tree, int, bool *);
345static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
346static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
4641882f 347static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
85c0a25c 348static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
349static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
1cae46be 350static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
f4a30bd7 351static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
1cae46be 352static tree handle_visibility_attribute (tree *, tree, tree, int,
353 bool *);
354static tree handle_tls_model_attribute (tree *, tree, tree, int,
355 bool *);
356static tree handle_no_instrument_function_attribute (tree *, tree,
357 tree, int, bool *);
358static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
26d1c5ff 359static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
1cae46be 360static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
361 bool *);
362static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
4c0315d0 363static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
364static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
fc09b200 365static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
1cae46be 366static tree handle_deprecated_attribute (tree *, tree, tree, int,
367 bool *);
368static tree handle_vector_size_attribute (tree *, tree, tree, int,
369 bool *);
370static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
371static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
372static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
8a8cdb8d 373static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
374 bool *);
bf6c8de0 375static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
b5c26b42 376static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
4a29c97c 377static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
237e78b1 378static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
379static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
24470055 380static tree handle_target_attribute (tree *, tree, tree, int, bool *);
ab50af2a 381static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
46f8e3b0 382static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
4c0315d0 383static tree ignore_attribute (tree *, tree, tree, int, bool *);
48b14f50 384static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
8ce86007 385static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
a96c3cc1 386static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
d7dcba40 387static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
bc7bff74 388static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
389 bool *);
c58a4cfd 390static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
bc7bff74 391static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
392 bool *);
74691f46 393static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
058a1b7a 394static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
395static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
396static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
1cae46be 397
d01f58f9 398static void check_function_nonnull (tree, int, tree *);
1cae46be 399static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
400static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
401static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
860251be 402static int resort_field_decl_cmp (const void *, const void *);
dbf6c367 403
5c6e5756 404/* Reserved words. The third field is a mask: keywords are disabled
405 if they match the mask.
406
407 Masks for languages:
408 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
409 C --std=c99: D_CXXONLY | D_OBJC
410 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
411 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
412 C++ --std=c0x: D_CONLY | D_OBJC
413 ObjC++ is like C++ except that D_OBJC is not set
414
415 If -fno-asm is used, D_ASM is added to the mask. If
416 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
417 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
b27e241e 418 In C with -Wc++-compat, we warn if D_CXXWARN is set.
5c6e5756 419
b27e241e 420 Note the complication of the D_CXX_OBJC keywords. These are
421 reserved words such as 'class'. In C++, 'class' is a reserved
422 word. In Objective-C++ it is too. In Objective-C, it is a
423 reserved word too, but only if it follows an '@' sign.
424*/
5c6e5756 425const struct c_common_resword c_common_reswords[] =
426{
83e25171 427 { "_Alignas", RID_ALIGNAS, D_CONLY },
428 { "_Alignof", RID_ALIGNOF, D_CONLY },
b560fabd 429 { "_Atomic", RID_ATOMIC, D_CONLY },
5c6e5756 430 { "_Bool", RID_BOOL, D_CONLY },
431 { "_Complex", RID_COMPLEX, 0 },
d037099f 432 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
433 { "_Cilk_sync", RID_CILK_SYNC, 0 },
40750995 434 { "_Cilk_for", RID_CILK_FOR, 0 },
c1800156 435 { "_Imaginary", RID_IMAGINARY, D_CONLY },
5c6e5756 436 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
437 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
438 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
439 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
440 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
441 { "_Sat", RID_SAT, D_CONLY | D_EXT },
f80e7755 442 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
985c6e3a 443 { "_Noreturn", RID_NORETURN, D_CONLY },
7aa04c8d 444 { "_Generic", RID_GENERIC, D_CONLY },
d184e0c0 445 { "_Thread_local", RID_THREAD, D_CONLY },
5c6e5756 446 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
447 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
448 { "__alignof", RID_ALIGNOF, 0 },
449 { "__alignof__", RID_ALIGNOF, 0 },
450 { "__asm", RID_ASM, 0 },
451 { "__asm__", RID_ASM, 0 },
452 { "__attribute", RID_ATTRIBUTE, 0 },
453 { "__attribute__", RID_ATTRIBUTE, 0 },
4fba5eb9 454 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
e6014a82 455 { "__bases", RID_BASES, D_CXXONLY },
6f58cf06 456 { "__builtin_call_with_static_chain",
457 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
5c6e5756 458 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
bff4ad11 459 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
bf0cb017 460 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
5c6e5756 461 { "__builtin_offsetof", RID_OFFSETOF, 0 },
462 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
463 { "__builtin_va_arg", RID_VA_ARG, 0 },
464 { "__complex", RID_COMPLEX, 0 },
465 { "__complex__", RID_COMPLEX, 0 },
466 { "__const", RID_CONST, 0 },
467 { "__const__", RID_CONST, 0 },
468 { "__decltype", RID_DECLTYPE, D_CXXONLY },
e6014a82 469 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
5c6e5756 470 { "__extension__", RID_EXTENSION, 0 },
471 { "__func__", RID_C99_FUNCTION_NAME, 0 },
472 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
473 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
474 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
475 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
476 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
477 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
478 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
479 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
23407dc9 480 { "__imag", RID_IMAGPART, 0 },
481 { "__imag__", RID_IMAGPART, 0 },
482 { "__inline", RID_INLINE, 0 },
483 { "__inline__", RID_INLINE, 0 },
5c6e5756 484 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
485 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
486 { "__is_class", RID_IS_CLASS, D_CXXONLY },
5c6e5756 487 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
488 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
aa4313eb 489 { "__is_final", RID_IS_FINAL, D_CXXONLY },
23407dc9 490 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
5c6e5756 491 { "__is_pod", RID_IS_POD, D_CXXONLY },
492 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
56c12fd4 493 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
c1c67b4f 494 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
495 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
f76a9aa8 496 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
497 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
717e52f9 498 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
5c6e5756 499 { "__is_union", RID_IS_UNION, D_CXXONLY },
5c6e5756 500 { "__label__", RID_LABEL, 0 },
501 { "__null", RID_NULL, 0 },
502 { "__real", RID_REALPART, 0 },
503 { "__real__", RID_REALPART, 0 },
504 { "__restrict", RID_RESTRICT, 0 },
505 { "__restrict__", RID_RESTRICT, 0 },
506 { "__signed", RID_SIGNED, 0 },
507 { "__signed__", RID_SIGNED, 0 },
508 { "__thread", RID_THREAD, 0 },
4c0315d0 509 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
510 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
511 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
5c6e5756 512 { "__typeof", RID_TYPEOF, 0 },
513 { "__typeof__", RID_TYPEOF, 0 },
23407dc9 514 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
5c6e5756 515 { "__volatile", RID_VOLATILE, 0 },
516 { "__volatile__", RID_VOLATILE, 0 },
fa769cc5 517 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
518 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 519 { "asm", RID_ASM, D_ASM },
520 { "auto", RID_AUTO, 0 },
dbd982c9 521 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
5c6e5756 522 { "break", RID_BREAK, 0 },
523 { "case", RID_CASE, 0 },
51030405 524 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
5c6e5756 525 { "char", RID_CHAR, 0 },
fa769cc5 526 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
527 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
51030405 528 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
5c6e5756 529 { "const", RID_CONST, 0 },
fa769cc5 530 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 531 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
532 { "continue", RID_CONTINUE, 0 },
fa769cc5 533 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 534 { "default", RID_DEFAULT, 0 },
51030405 535 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
5c6e5756 536 { "do", RID_DO, 0 },
537 { "double", RID_DOUBLE, 0 },
538 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
539 { "else", RID_ELSE, 0 },
540 { "enum", RID_ENUM, 0 },
51030405 541 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
542 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
5c6e5756 543 { "extern", RID_EXTERN, 0 },
51030405 544 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
5c6e5756 545 { "float", RID_FLOAT, 0 },
546 { "for", RID_FOR, 0 },
51030405 547 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
5c6e5756 548 { "goto", RID_GOTO, 0 },
549 { "if", RID_IF, 0 },
550 { "inline", RID_INLINE, D_EXT89 },
551 { "int", RID_INT, 0 },
552 { "long", RID_LONG, 0 },
553 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
51030405 554 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
555 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
fa769cc5 556 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
557 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
51030405 558 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
559 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
560 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
561 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
5c6e5756 562 { "register", RID_REGISTER, 0 },
563 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
564 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
565 { "return", RID_RETURN, 0 },
566 { "short", RID_SHORT, 0 },
567 { "signed", RID_SIGNED, 0 },
568 { "sizeof", RID_SIZEOF, 0 },
569 { "static", RID_STATIC, 0 },
fa769cc5 570 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 571 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
572 { "struct", RID_STRUCT, 0 },
573 { "switch", RID_SWITCH, 0 },
51030405 574 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
575 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
fa769cc5 576 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
51030405 577 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
578 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
579 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
5c6e5756 580 { "typedef", RID_TYPEDEF, 0 },
51030405 581 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
582 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
5c6e5756 583 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
584 { "union", RID_UNION, 0 },
585 { "unsigned", RID_UNSIGNED, 0 },
51030405 586 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
587 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
5c6e5756 588 { "void", RID_VOID, 0 },
589 { "volatile", RID_VOLATILE, 0 },
590 { "wchar_t", RID_WCHAR, D_CXXONLY },
591 { "while", RID_WHILE, 0 },
56c12fd4 592
6d02e6b2 593 /* C++ transactional memory. */
594 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
595 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
596 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
597 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
598
56c12fd4 599 /* Concepts-related keywords */
600 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
601 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
602
5c6e5756 603 /* These Objective-C keywords are recognized only immediately after
604 an '@'. */
605 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
606 { "defs", RID_AT_DEFS, D_OBJC },
607 { "encode", RID_AT_ENCODE, D_OBJC },
608 { "end", RID_AT_END, D_OBJC },
609 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
610 { "interface", RID_AT_INTERFACE, D_OBJC },
611 { "protocol", RID_AT_PROTOCOL, D_OBJC },
612 { "selector", RID_AT_SELECTOR, D_OBJC },
613 { "finally", RID_AT_FINALLY, D_OBJC },
069761fb 614 { "optional", RID_AT_OPTIONAL, D_OBJC },
615 { "required", RID_AT_REQUIRED, D_OBJC },
86c110ac 616 { "property", RID_AT_PROPERTY, D_OBJC },
4a8875ed 617 { "package", RID_AT_PACKAGE, D_OBJC },
e1f293c0 618 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
619 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
5c6e5756 620 /* These are recognized only in protocol-qualifier context
621 (see above) */
622 { "bycopy", RID_BYCOPY, D_OBJC },
623 { "byref", RID_BYREF, D_OBJC },
624 { "in", RID_IN, D_OBJC },
625 { "inout", RID_INOUT, D_OBJC },
626 { "oneway", RID_ONEWAY, D_OBJC },
627 { "out", RID_OUT, D_OBJC },
86c110ac 628 /* These are recognized inside a property attribute list */
7590f0e5 629 { "assign", RID_ASSIGN, D_OBJC },
630 { "copy", RID_COPY, D_OBJC },
86c110ac 631 { "getter", RID_GETTER, D_OBJC },
7590f0e5 632 { "nonatomic", RID_NONATOMIC, D_OBJC },
633 { "readonly", RID_READONLY, D_OBJC },
634 { "readwrite", RID_READWRITE, D_OBJC },
635 { "retain", RID_RETAIN, D_OBJC },
86c110ac 636 { "setter", RID_SETTER, D_OBJC },
5c6e5756 637};
638
639const unsigned int num_c_common_reswords =
640 sizeof c_common_reswords / sizeof (struct c_common_resword);
641
058a1b7a 642/* Table of machine-independent attributes common to all C-like languages.
643
644 All attributes referencing arguments should be additionally processed
645 in chkp_copy_function_type_adding_bounds for correct instrumentation
646 by Pointer Bounds Checker.
647 Current list of processed common attributes: nonnull. */
f8e93a2e 648const struct attribute_spec c_common_attribute_table[] =
649{
ac86af5d 650 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
651 affects_type_identity } */
f8e93a2e 652 { "packed", 0, 0, false, false, false,
ac86af5d 653 handle_packed_attribute , false},
f8e93a2e 654 { "nocommon", 0, 0, true, false, false,
ac86af5d 655 handle_nocommon_attribute, false},
f8e93a2e 656 { "common", 0, 0, true, false, false,
ac86af5d 657 handle_common_attribute, false },
f8e93a2e 658 /* FIXME: logically, noreturn attributes should be listed as
659 "false, true, true" and apply to function types. But implementing this
660 would require all the places in the compiler that use TREE_THIS_VOLATILE
661 on a decl to identify non-returning functions to be located and fixed
662 to check the function type instead. */
663 { "noreturn", 0, 0, true, false, false,
ac86af5d 664 handle_noreturn_attribute, false },
f8e93a2e 665 { "volatile", 0, 0, true, false, false,
ac86af5d 666 handle_noreturn_attribute, false },
947aa916 667 { "stack_protect", 0, 0, true, false, false,
668 handle_stack_protect_attribute, false },
f8e93a2e 669 { "noinline", 0, 0, true, false, false,
ac86af5d 670 handle_noinline_attribute, false },
bdb1f0d1 671 { "noclone", 0, 0, true, false, false,
ac86af5d 672 handle_noclone_attribute, false },
85fbea97 673 { "no_icf", 0, 0, true, false, false,
674 handle_noicf_attribute, false },
7bd95dfd 675 { "leaf", 0, 0, true, false, false,
ac86af5d 676 handle_leaf_attribute, false },
f8e93a2e 677 { "always_inline", 0, 0, true, false, false,
ac86af5d 678 handle_always_inline_attribute, false },
541e4101 679 { "gnu_inline", 0, 0, true, false, false,
ac86af5d 680 handle_gnu_inline_attribute, false },
1b16fc45 681 { "artificial", 0, 0, true, false, false,
ac86af5d 682 handle_artificial_attribute, false },
0cdd9887 683 { "flatten", 0, 0, true, false, false,
ac86af5d 684 handle_flatten_attribute, false },
f8e93a2e 685 { "used", 0, 0, true, false, false,
ac86af5d 686 handle_used_attribute, false },
f8e93a2e 687 { "unused", 0, 0, false, false, false,
ac86af5d 688 handle_unused_attribute, false },
62eec3b4 689 { "externally_visible", 0, 0, true, false, false,
ac86af5d 690 handle_externally_visible_attribute, false },
6b722052 691 { "no_reorder", 0, 0, true, false, false,
692 handle_no_reorder_attribute, false },
f8e93a2e 693 /* The same comments as for noreturn attributes apply to const ones. */
694 { "const", 0, 0, true, false, false,
ac86af5d 695 handle_const_attribute, false },
292237f3 696 { "scalar_storage_order", 1, 1, false, false, false,
697 handle_scalar_storage_order_attribute, false },
f8e93a2e 698 { "transparent_union", 0, 0, false, false, false,
ac86af5d 699 handle_transparent_union_attribute, false },
9af7fd5b 700 { "constructor", 0, 1, true, false, false,
ac86af5d 701 handle_constructor_attribute, false },
9af7fd5b 702 { "destructor", 0, 1, true, false, false,
ac86af5d 703 handle_destructor_attribute, false },
f8e93a2e 704 { "mode", 1, 1, false, true, false,
ac86af5d 705 handle_mode_attribute, false },
f8e93a2e 706 { "section", 1, 1, true, false, false,
ac86af5d 707 handle_section_attribute, false },
f8e93a2e 708 { "aligned", 0, 1, false, false, false,
ac86af5d 709 handle_aligned_attribute, false },
f8e93a2e 710 { "weak", 0, 0, true, false, false,
ac86af5d 711 handle_weak_attribute, false },
4641882f 712 { "noplt", 0, 0, true, false, false,
713 handle_noplt_attribute, false },
85c0a25c 714 { "ifunc", 1, 1, true, false, false,
ac86af5d 715 handle_ifunc_attribute, false },
f8e93a2e 716 { "alias", 1, 1, true, false, false,
ac86af5d 717 handle_alias_attribute, false },
f4a30bd7 718 { "weakref", 0, 1, true, false, false,
ac86af5d 719 handle_weakref_attribute, false },
f8e93a2e 720 { "no_instrument_function", 0, 0, true, false, false,
ac86af5d 721 handle_no_instrument_function_attribute,
722 false },
f8e93a2e 723 { "malloc", 0, 0, true, false, false,
ac86af5d 724 handle_malloc_attribute, false },
26d1c5ff 725 { "returns_twice", 0, 0, true, false, false,
ac86af5d 726 handle_returns_twice_attribute, false },
f8e93a2e 727 { "no_stack_limit", 0, 0, true, false, false,
ac86af5d 728 handle_no_limit_stack_attribute, false },
f8e93a2e 729 { "pure", 0, 0, true, false, false,
ac86af5d 730 handle_pure_attribute, false },
4c0315d0 731 { "transaction_callable", 0, 0, false, true, false,
732 handle_tm_attribute, false },
733 { "transaction_unsafe", 0, 0, false, true, false,
6d02e6b2 734 handle_tm_attribute, true },
4c0315d0 735 { "transaction_safe", 0, 0, false, true, false,
6d02e6b2 736 handle_tm_attribute, true },
737 { "transaction_safe_dynamic", 0, 0, true, false, false,
4c0315d0 738 handle_tm_attribute, false },
739 { "transaction_may_cancel_outer", 0, 0, false, true, false,
740 handle_tm_attribute, false },
741 /* ??? These two attributes didn't make the transition from the
742 Intel language document to the multi-vendor language document. */
743 { "transaction_pure", 0, 0, false, true, false,
744 handle_tm_attribute, false },
745 { "transaction_wrap", 1, 1, true, false, false,
746 handle_tm_wrap_attribute, false },
fc09b200 747 /* For internal use (marking of builtins) only. The name contains space
748 to prevent its usage in source code. */
749 { "no vops", 0, 0, true, false, false,
ac86af5d 750 handle_novops_attribute, false },
45c4e798 751 { "deprecated", 0, 1, false, false, false,
ac86af5d 752 handle_deprecated_attribute, false },
f8e93a2e 753 { "vector_size", 1, 1, false, true, false,
ac86af5d 754 handle_vector_size_attribute, false },
b212f378 755 { "visibility", 1, 1, false, false, false,
ac86af5d 756 handle_visibility_attribute, false },
24dfead4 757 { "tls_model", 1, 1, true, false, false,
ac86af5d 758 handle_tls_model_attribute, false },
dbf6c367 759 { "nonnull", 0, -1, false, true, true,
ac86af5d 760 handle_nonnull_attribute, false },
fa987697 761 { "nothrow", 0, 0, true, false, false,
ac86af5d 762 handle_nothrow_attribute, false },
763 { "may_alias", 0, 0, false, true, false, NULL, false },
7acb29a3 764 { "cleanup", 1, 1, true, false, false,
ac86af5d 765 handle_cleanup_attribute, false },
8a8cdb8d 766 { "warn_unused_result", 0, 0, false, true, true,
ac86af5d 767 handle_warn_unused_result_attribute, false },
50ca527f 768 { "sentinel", 0, 1, false, true, true,
ac86af5d 769 handle_sentinel_attribute, false },
b5c26b42 770 /* For internal use (marking of builtins) only. The name contains space
771 to prevent its usage in source code. */
772 { "type generic", 0, 0, false, true, true,
ac86af5d 773 handle_type_generic_attribute, false },
4a29c97c 774 { "alloc_size", 1, 2, false, true, true,
ac86af5d 775 handle_alloc_size_attribute, false },
5de92639 776 { "cold", 0, 0, true, false, false,
ac86af5d 777 handle_cold_attribute, false },
5de92639 778 { "hot", 0, 0, true, false, false,
ac86af5d 779 handle_hot_attribute, false },
d413ffdd 780 { "no_address_safety_analysis",
781 0, 0, true, false, false,
782 handle_no_address_safety_analysis_attribute,
783 false },
a9196da9 784 { "no_sanitize_address", 0, 0, true, false, false,
785 handle_no_sanitize_address_attribute,
786 false },
d1e96383 787 { "no_sanitize_thread", 0, 0, true, false, false,
788 handle_no_sanitize_address_attribute,
789 false },
05f893e1 790 { "no_sanitize_undefined", 0, 0, true, false, false,
791 handle_no_sanitize_undefined_attribute,
792 false },
10fc867f 793 { "warning", 1, 1, true, false, false,
ac86af5d 794 handle_error_attribute, false },
10fc867f 795 { "error", 1, 1, true, false, false,
ac86af5d 796 handle_error_attribute, false },
24470055 797 { "target", 1, -1, true, false, false,
ac86af5d 798 handle_target_attribute, false },
ab50af2a 799 { "target_clones", 1, -1, true, false, false,
800 handle_target_clones_attribute, false },
46f8e3b0 801 { "optimize", 1, -1, true, false, false,
ac86af5d 802 handle_optimize_attribute, false },
4c0315d0 803 /* For internal use only. The leading '*' both prevents its usage in
804 source code and signals that it may be overridden by machine tables. */
805 { "*tm regparm", 0, 0, false, true, true,
806 ignore_attribute, false },
48b14f50 807 { "no_split_stack", 0, 0, true, false, false,
ac86af5d 808 handle_no_split_stack_attribute, false },
8ce86007 809 /* For internal use (marking of builtins and runtime functions) only.
810 The name contains space to prevent its usage in source code. */
811 { "fn spec", 1, 1, false, true, true,
ac86af5d 812 handle_fnspec_attribute, false },
a96c3cc1 813 { "warn_unused", 0, 0, false, false, false,
814 handle_warn_unused_attribute, false },
d7dcba40 815 { "returns_nonnull", 0, 0, false, true, true,
816 handle_returns_nonnull_attribute, false },
bc7bff74 817 { "omp declare simd", 0, -1, true, false, false,
818 handle_omp_declare_simd_attribute, false },
74acc703 819 { "cilk simd function", 0, -1, true, false, false,
820 handle_omp_declare_simd_attribute, false },
c58a4cfd 821 { "simd", 0, 0, true, false, false,
822 handle_simd_attribute, false },
bc7bff74 823 { "omp declare target", 0, 0, true, false, false,
824 handle_omp_declare_target_attribute, false },
237e78b1 825 { "alloc_align", 1, 1, false, true, true,
826 handle_alloc_align_attribute, false },
827 { "assume_aligned", 1, 2, false, true, true,
828 handle_assume_aligned_attribute, false },
74691f46 829 { "designated_init", 0, 0, false, true, false,
830 handle_designated_init_attribute, false },
058a1b7a 831 { "bnd_variable_size", 0, 0, true, false, false,
832 handle_bnd_variable_size_attribute, false },
833 { "bnd_legacy", 0, 0, true, false, false,
834 handle_bnd_legacy, false },
835 { "bnd_instrument", 0, 0, true, false, false,
836 handle_bnd_instrument, false },
ac86af5d 837 { NULL, 0, 0, false, false, false, NULL, false }
f8e93a2e 838};
839
840/* Give the specifications for the format attributes, used by C and all
058a1b7a 841 descendants.
f8e93a2e 842
058a1b7a 843 All attributes referencing arguments should be additionally processed
844 in chkp_copy_function_type_adding_bounds for correct instrumentation
845 by Pointer Bounds Checker.
846 Current list of processed format attributes: format, format_arg. */
f8e93a2e 847const struct attribute_spec c_common_format_attribute_table[] =
848{
ac86af5d 849 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
850 affects_type_identity } */
f8e93a2e 851 { "format", 3, 3, false, true, true,
ac86af5d 852 handle_format_attribute, false },
f8e93a2e 853 { "format_arg", 1, 1, false, true, true,
ac86af5d 854 handle_format_arg_attribute, false },
855 { NULL, 0, 0, false, false, false, NULL, false }
f8e93a2e 856};
857
6d5d708e 858/* Return identifier for address space AS. */
34208e18 859
6d5d708e 860const char *
861c_addr_space_name (addr_space_t as)
862{
34208e18 863 int rid = RID_FIRST_ADDR_SPACE + as;
864 gcc_assert (ridpointers [rid]);
865 return IDENTIFIER_POINTER (ridpointers [rid]);
6d5d708e 866}
867
2c0e001b 868/* Push current bindings for the function name VAR_DECLS. */
f4e3c278 869
870void
1cae46be 871start_fname_decls (void)
f4e3c278 872{
65b7f83f 873 unsigned ix;
874 tree saved = NULL_TREE;
1cae46be 875
65b7f83f 876 for (ix = 0; fname_vars[ix].decl; ix++)
877 {
878 tree decl = *fname_vars[ix].decl;
f4e3c278 879
65b7f83f 880 if (decl)
881 {
ceb7b692 882 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
883 saved);
65b7f83f 884 *fname_vars[ix].decl = NULL_TREE;
885 }
886 }
887 if (saved || saved_function_name_decls)
888 /* Normally they'll have been NULL, so only push if we've got a
889 stack, or they are non-NULL. */
890 saved_function_name_decls = tree_cons (saved, NULL_TREE,
891 saved_function_name_decls);
892}
893
2363ef00 894/* Finish up the current bindings, adding them into the current function's
895 statement tree. This must be done _before_ finish_stmt_tree is called.
896 If there is no current function, we must be at file scope and no statements
897 are involved. Pop the previous bindings. */
65b7f83f 898
899void
1cae46be 900finish_fname_decls (void)
65b7f83f 901{
902 unsigned ix;
2363ef00 903 tree stmts = NULL_TREE;
65b7f83f 904 tree stack = saved_function_name_decls;
905
906 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
2363ef00 907 append_to_statement_list (TREE_VALUE (stack), &stmts);
1cae46be 908
2363ef00 909 if (stmts)
65b7f83f 910 {
2363ef00 911 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
5c423bd6 912
2363ef00 913 if (TREE_CODE (*bodyp) == BIND_EXPR)
914 bodyp = &BIND_EXPR_BODY (*bodyp);
81010c97 915
bc2b76e0 916 append_to_statement_list_force (*bodyp, &stmts);
2363ef00 917 *bodyp = stmts;
65b7f83f 918 }
1cae46be 919
65b7f83f 920 for (ix = 0; fname_vars[ix].decl; ix++)
921 *fname_vars[ix].decl = NULL_TREE;
1cae46be 922
65b7f83f 923 if (stack)
f4e3c278 924 {
2c0e001b 925 /* We had saved values, restore them. */
65b7f83f 926 tree saved;
927
928 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
929 {
930 tree decl = TREE_PURPOSE (saved);
f9ae6f95 931 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
1cae46be 932
65b7f83f 933 *fname_vars[ix].decl = decl;
934 }
935 stack = TREE_CHAIN (stack);
f4e3c278 936 }
65b7f83f 937 saved_function_name_decls = stack;
938}
939
81010c97 940/* Return the text name of the current function, suitably prettified
5fc7fa69 941 by PRETTY_P. Return string must be freed by caller. */
65b7f83f 942
943const char *
1cae46be 944fname_as_string (int pretty_p)
65b7f83f 945{
9ad4bb1e 946 const char *name = "top level";
5fc7fa69 947 char *namep;
8115b8be 948 int vrb = 2, len;
949 cpp_string cstr = { 0, 0 }, strname;
9ad4bb1e 950
84166705 951 if (!pretty_p)
9ad4bb1e 952 {
953 name = "";
954 vrb = 0;
955 }
956
957 if (current_function_decl)
dc24ddbd 958 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
9ad4bb1e 959
8115b8be 960 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
5fc7fa69 961
8115b8be 962 namep = XNEWVEC (char, len);
963 snprintf (namep, len, "\"%s\"", name);
964 strname.text = (unsigned char *) namep;
965 strname.len = len - 1;
5fc7fa69 966
924bbf02 967 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
8115b8be 968 {
969 XDELETEVEC (namep);
970 return (const char *) cstr.text;
5fc7fa69 971 }
5fc7fa69 972
973 return namep;
65b7f83f 974}
975
65b7f83f 976/* Return the VAR_DECL for a const char array naming the current
977 function. If the VAR_DECL has not yet been created, create it
978 now. RID indicates how it should be formatted and IDENTIFIER_NODE
979 ID is its name (unfortunately C and C++ hold the RID values of
980 keywords in different places, so we can't derive RID from ID in
e3b80d49 981 this language independent code. LOC is the location of the
982 function. */
65b7f83f 983
984tree
e3b80d49 985fname_decl (location_t loc, unsigned int rid, tree id)
65b7f83f 986{
987 unsigned ix;
988 tree decl = NULL_TREE;
989
990 for (ix = 0; fname_vars[ix].decl; ix++)
991 if (fname_vars[ix].rid == rid)
992 break;
993
994 decl = *fname_vars[ix].decl;
995 if (!decl)
f4e3c278 996 {
2222b3c6 997 /* If a tree is built here, it would normally have the lineno of
998 the current statement. Later this tree will be moved to the
999 beginning of the function and this line number will be wrong.
1000 To avoid this problem set the lineno to 0 here; that prevents
7299020b 1001 it from appearing in the RTL. */
2363ef00 1002 tree stmts;
9a6486a6 1003 location_t saved_location = input_location;
9a6486a6 1004 input_location = UNKNOWN_LOCATION;
1cae46be 1005
2363ef00 1006 stmts = push_stmt_list ();
e60a6f7b 1007 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
2363ef00 1008 stmts = pop_stmt_list (stmts);
1009 if (!IS_EMPTY_STMT (stmts))
1010 saved_function_name_decls
1011 = tree_cons (decl, stmts, saved_function_name_decls);
65b7f83f 1012 *fname_vars[ix].decl = decl;
9a6486a6 1013 input_location = saved_location;
f4e3c278 1014 }
65b7f83f 1015 if (!ix && !current_function_decl)
e3b80d49 1016 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
81010c97 1017
65b7f83f 1018 return decl;
f4e3c278 1019}
1020
070236f0 1021/* Given a STRING_CST, give it a suitable array-of-chars data type. */
b0fc3e72 1022
1023tree
1cae46be 1024fix_string_type (tree value)
b0fc3e72 1025{
070236f0 1026 int length = TREE_STRING_LENGTH (value);
1027 int nchars;
00d26680 1028 tree e_type, i_type, a_type;
1029
73be5127 1030 /* Compute the number of elements, for the array type. */
924bbf02 1031 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1032 {
1033 nchars = length;
1034 e_type = char_type_node;
1035 }
1036 else if (TREE_TYPE (value) == char16_array_type_node)
1037 {
1038 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1039 e_type = char16_type_node;
1040 }
1041 else if (TREE_TYPE (value) == char32_array_type_node)
1042 {
1043 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1044 e_type = char32_type_node;
1045 }
1046 else
1047 {
1048 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1049 e_type = wchar_type_node;
1050 }
b0fc3e72 1051
1d752508 1052 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1053 limit in C++98 Annex B is very large (65536) and is not normative,
1054 so we do not diagnose it (warn_overlength_strings is forced off
1055 in c_common_post_options). */
1056 if (warn_overlength_strings)
1057 {
1058 const int nchars_max = flag_isoc99 ? 4095 : 509;
1059 const int relevant_std = flag_isoc99 ? 99 : 90;
1060 if (nchars - 1 > nchars_max)
1061 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1062 separate the %d from the 'C'. 'ISO' should not be
1063 translated, but it may be moved after 'C%d' in languages
1064 where modifiers follow nouns. */
21ca8540 1065 pedwarn (input_location, OPT_Woverlength_strings,
8864917d 1066 "string length %qd is greater than the length %qd "
1d752508 1067 "ISO C%d compilers are required to support",
1068 nchars - 1, nchars_max, relevant_std);
1069 }
82cfc7f7 1070
390be14e 1071 /* Create the array type for the string constant. The ISO C++
1072 standard says that a string literal has type `const char[N]' or
1073 `const wchar_t[N]'. We use the same logic when invoked as a C
1074 front-end with -Wwrite-strings.
1075 ??? We should change the type of an expression depending on the
1076 state of a warning flag. We should just be warning -- see how
1077 this is handled in the C++ front-end for the deprecated implicit
1078 conversion from string literals to `char*' or `wchar_t*'.
00d26680 1079
1080 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1081 array type being the unqualified version of that type.
1082 Therefore, if we are constructing an array of const char, we must
1083 construct the matching unqualified array type first. The C front
1084 end does not require this, but it does no harm, so we do it
1085 unconditionally. */
ceb7b692 1086 i_type = build_index_type (size_int (nchars - 1));
00d26680 1087 a_type = build_array_type (e_type, i_type);
390be14e 1088 if (c_dialect_cxx() || warn_write_strings)
aebc8537 1089 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
3a10ba35 1090
00d26680 1091 TREE_TYPE (value) = a_type;
b8e3b7ad 1092 TREE_CONSTANT (value) = 1;
a814bad5 1093 TREE_READONLY (value) = 1;
b0fc3e72 1094 TREE_STATIC (value) = 1;
1095 return value;
1096}
1097\f
fc501191 1098/* If DISABLE is true, stop issuing warnings. This is used when
1099 parsing code that we know will not be executed. This function may
1100 be called multiple times, and works as a stack. */
1101
1102static void
1103c_disable_warnings (bool disable)
1104{
1105 if (disable)
1106 {
1107 ++c_inhibit_evaluation_warnings;
1108 fold_defer_overflow_warnings ();
1109 }
1110}
1111
1112/* If ENABLE is true, reenable issuing warnings. */
1113
1114static void
1115c_enable_warnings (bool enable)
1116{
1117 if (enable)
1118 {
1119 --c_inhibit_evaluation_warnings;
1120 fold_undefer_and_ignore_overflow_warnings ();
1121 }
1122}
1123
a75b1c71 1124/* Fully fold EXPR, an expression that was not folded (beyond integer
1125 constant expressions and null pointer constants) when being built
1126 up. If IN_INIT, this is in a static initializer and certain
1127 changes are made to the folding done. Clear *MAYBE_CONST if
1128 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1129 expression because it contains an evaluated operator (in C99) or an
1130 operator outside of sizeof returning an integer constant (in C90)
1131 not permitted in constant expressions, or because it contains an
1132 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1133 set to true by callers before calling this function.) Return the
1134 folded expression. Function arguments have already been folded
1135 before calling this function, as have the contents of SAVE_EXPR,
1136 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1137 C_MAYBE_CONST_EXPR. */
1138
1139tree
1140c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1141{
1142 tree ret;
c6418a4e 1143 tree eptype = NULL_TREE;
a75b1c71 1144 bool dummy = true;
1145 bool maybe_const_itself = true;
389dd41b 1146 location_t loc = EXPR_LOCATION (expr);
a75b1c71 1147
1148 /* This function is not relevant to C++ because C++ folds while
1149 parsing, and may need changes to be correct for C++ when C++
1150 stops folding while parsing. */
1151 if (c_dialect_cxx ())
1152 gcc_unreachable ();
1153
1154 if (!maybe_const)
1155 maybe_const = &dummy;
c6418a4e 1156 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1157 {
1158 eptype = TREE_TYPE (expr);
1159 expr = TREE_OPERAND (expr, 0);
1160 }
a75b1c71 1161 ret = c_fully_fold_internal (expr, in_init, maybe_const,
aac24642 1162 &maybe_const_itself, false);
c6418a4e 1163 if (eptype)
389dd41b 1164 ret = fold_convert_loc (loc, eptype, ret);
a75b1c71 1165 *maybe_const &= maybe_const_itself;
1166 return ret;
1167}
1168
1169/* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1170 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1171 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1172 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1173 both evaluated and unevaluated subexpressions while
1174 *MAYBE_CONST_ITSELF is carried from only evaluated
aac24642 1175 subexpressions). FOR_INT_CONST indicates if EXPR is an expression
1176 with integer constant operands, and if any of the operands doesn't
1177 get folded to an integer constant, don't fold the expression itself. */
a75b1c71 1178
1179static tree
1180c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
aac24642 1181 bool *maybe_const_itself, bool for_int_const)
a75b1c71 1182{
1183 tree ret = expr;
1184 enum tree_code code = TREE_CODE (expr);
1185 enum tree_code_class kind = TREE_CODE_CLASS (code);
1186 location_t loc = EXPR_LOCATION (expr);
1187 tree op0, op1, op2, op3;
1188 tree orig_op0, orig_op1, orig_op2;
1189 bool op0_const = true, op1_const = true, op2_const = true;
1190 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1191 bool nowarning = TREE_NO_WARNING (expr);
fc501191 1192 bool unused_p;
a96cefb2 1193 source_range old_range;
a75b1c71 1194
1195 /* This function is not relevant to C++ because C++ folds while
1196 parsing, and may need changes to be correct for C++ when C++
1197 stops folding while parsing. */
1198 if (c_dialect_cxx ())
1199 gcc_unreachable ();
1200
1201 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1202 anything else not counted as an expression cannot usefully be
1203 folded further at this point. */
1204 if (!IS_EXPR_CODE_CLASS (kind)
1205 || kind == tcc_statement
1206 || code == SAVE_EXPR)
1207 return expr;
1208
a96cefb2 1209 if (IS_EXPR_CODE_CLASS (kind))
1210 old_range = EXPR_LOCATION_RANGE (expr);
1211
a75b1c71 1212 /* Operands of variable-length expressions (function calls) have
1213 already been folded, as have __builtin_* function calls, and such
1214 expressions cannot occur in constant expressions. */
1215 if (kind == tcc_vl_exp)
1216 {
1217 *maybe_const_operands = false;
1218 ret = fold (expr);
1219 goto out;
1220 }
1221
1222 if (code == C_MAYBE_CONST_EXPR)
1223 {
1224 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1225 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1226 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1227 *maybe_const_operands = false;
1228 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
aac24642 1229 {
1230 *maybe_const_itself = false;
1231 inner = c_fully_fold_internal (inner, in_init, maybe_const_operands,
1232 maybe_const_itself, true);
1233 }
a75b1c71 1234 if (pre && !in_init)
1235 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1236 else
1237 ret = inner;
1238 goto out;
1239 }
1240
1241 /* Assignment, increment, decrement, function call and comma
1242 operators, and statement expressions, cannot occur in constant
1243 expressions if evaluated / outside of sizeof. (Function calls
1244 were handled above, though VA_ARG_EXPR is treated like a function
1245 call here, and statement expressions are handled through
1246 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1247 switch (code)
1248 {
1249 case MODIFY_EXPR:
1250 case PREDECREMENT_EXPR:
1251 case PREINCREMENT_EXPR:
1252 case POSTDECREMENT_EXPR:
1253 case POSTINCREMENT_EXPR:
1254 case COMPOUND_EXPR:
1255 *maybe_const_operands = false;
1256 break;
1257
1258 case VA_ARG_EXPR:
1259 case TARGET_EXPR:
1260 case BIND_EXPR:
1261 case OBJ_TYPE_REF:
1262 *maybe_const_operands = false;
1263 ret = fold (expr);
1264 goto out;
1265
1266 default:
1267 break;
1268 }
1269
1270 /* Fold individual tree codes as appropriate. */
1271 switch (code)
1272 {
1273 case COMPOUND_LITERAL_EXPR:
1274 /* Any non-constancy will have been marked in a containing
1275 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1276 goto out;
1277
1278 case COMPONENT_REF:
1279 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1280 op1 = TREE_OPERAND (expr, 1);
1281 op2 = TREE_OPERAND (expr, 2);
1282 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1283 maybe_const_itself, for_int_const);
f59e3889 1284 STRIP_TYPE_NOPS (op0);
a75b1c71 1285 if (op0 != orig_op0)
1286 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1287 if (ret != expr)
1288 {
1289 TREE_READONLY (ret) = TREE_READONLY (expr);
1290 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1291 }
1292 goto out;
1293
1294 case ARRAY_REF:
1295 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1296 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1297 op2 = TREE_OPERAND (expr, 2);
1298 op3 = TREE_OPERAND (expr, 3);
1299 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1300 maybe_const_itself, for_int_const);
f59e3889 1301 STRIP_TYPE_NOPS (op0);
a75b1c71 1302 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
aac24642 1303 maybe_const_itself, for_int_const);
f59e3889 1304 STRIP_TYPE_NOPS (op1);
a75b1c71 1305 op1 = decl_constant_value_for_optimization (op1);
1306 if (op0 != orig_op0 || op1 != orig_op1)
1307 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1308 if (ret != expr)
1309 {
1310 TREE_READONLY (ret) = TREE_READONLY (expr);
1311 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1312 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1313 }
1314 ret = fold (ret);
1315 goto out;
1316
1317 case COMPOUND_EXPR:
1318 case MODIFY_EXPR:
1319 case PREDECREMENT_EXPR:
1320 case PREINCREMENT_EXPR:
1321 case POSTDECREMENT_EXPR:
1322 case POSTINCREMENT_EXPR:
1323 case PLUS_EXPR:
1324 case MINUS_EXPR:
1325 case MULT_EXPR:
1326 case POINTER_PLUS_EXPR:
1327 case TRUNC_DIV_EXPR:
1328 case CEIL_DIV_EXPR:
1329 case FLOOR_DIV_EXPR:
1330 case TRUNC_MOD_EXPR:
1331 case RDIV_EXPR:
1332 case EXACT_DIV_EXPR:
1333 case LSHIFT_EXPR:
1334 case RSHIFT_EXPR:
1335 case BIT_IOR_EXPR:
1336 case BIT_XOR_EXPR:
1337 case BIT_AND_EXPR:
1338 case LT_EXPR:
1339 case LE_EXPR:
1340 case GT_EXPR:
1341 case GE_EXPR:
1342 case EQ_EXPR:
1343 case NE_EXPR:
1344 case COMPLEX_EXPR:
1345 case TRUTH_AND_EXPR:
1346 case TRUTH_OR_EXPR:
1347 case TRUTH_XOR_EXPR:
1348 case UNORDERED_EXPR:
1349 case ORDERED_EXPR:
1350 case UNLT_EXPR:
1351 case UNLE_EXPR:
1352 case UNGT_EXPR:
1353 case UNGE_EXPR:
1354 case UNEQ_EXPR:
1355 /* Binary operations evaluating both arguments (increment and
1356 decrement are binary internally in GCC). */
1357 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1358 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1359 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1360 maybe_const_itself, for_int_const);
f59e3889 1361 STRIP_TYPE_NOPS (op0);
a75b1c71 1362 if (code != MODIFY_EXPR
1363 && code != PREDECREMENT_EXPR
1364 && code != PREINCREMENT_EXPR
1365 && code != POSTDECREMENT_EXPR
1366 && code != POSTINCREMENT_EXPR)
1367 op0 = decl_constant_value_for_optimization (op0);
1368 /* The RHS of a MODIFY_EXPR was fully folded when building that
1369 expression for the sake of conversion warnings. */
1370 if (code != MODIFY_EXPR)
1371 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
aac24642 1372 maybe_const_itself, for_int_const);
f59e3889 1373 STRIP_TYPE_NOPS (op1);
a75b1c71 1374 op1 = decl_constant_value_for_optimization (op1);
aac24642 1375
1376 if (for_int_const && (TREE_CODE (op0) != INTEGER_CST
1377 || TREE_CODE (op1) != INTEGER_CST))
1378 goto out;
1379
a75b1c71 1380 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1381 ret = in_init
389dd41b 1382 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1383 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
a75b1c71 1384 else
1385 ret = fold (expr);
672d914b 1386 if (TREE_OVERFLOW_P (ret)
1387 && !TREE_OVERFLOW_P (op0)
1388 && !TREE_OVERFLOW_P (op1))
8d669e79 1389 overflow_warning (EXPR_LOC_OR_LOC (expr, input_location), ret);
cdc64059 1390 if (code == LSHIFT_EXPR
1391 && TREE_CODE (orig_op0) != INTEGER_CST
1392 && TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1393 && TREE_CODE (op0) == INTEGER_CST
1394 && c_inhibit_evaluation_warnings == 0
1395 && tree_int_cst_sgn (op0) < 0)
1396 warning_at (loc, OPT_Wshift_negative_value,
1397 "left shift of negative value");
7c834436 1398 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1399 && TREE_CODE (orig_op1) != INTEGER_CST
1400 && TREE_CODE (op1) == INTEGER_CST
1401 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1402 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1403 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1404 && c_inhibit_evaluation_warnings == 0)
1405 {
1406 if (tree_int_cst_sgn (op1) < 0)
f11bdffb 1407 warning_at (loc, OPT_Wshift_count_negative,
1408 (code == LSHIFT_EXPR
1409 ? G_("left shift count is negative")
1410 : G_("right shift count is negative")));
7c834436 1411 else if (compare_tree_int (op1,
1412 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1413 >= 0)
f11bdffb 1414 warning_at (loc, OPT_Wshift_count_overflow,
1415 (code == LSHIFT_EXPR
1416 ? G_("left shift count >= width of type")
1417 : G_("right shift count >= width of type")));
7c834436 1418 }
8d669e79 1419 if (code == LSHIFT_EXPR
1420 /* If either OP0 has been folded to INTEGER_CST... */
1421 && ((TREE_CODE (orig_op0) != INTEGER_CST
1422 && TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1423 && TREE_CODE (op0) == INTEGER_CST)
1424 /* ...or if OP1 has been folded to INTEGER_CST... */
1425 || (TREE_CODE (orig_op1) != INTEGER_CST
1426 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1427 && TREE_CODE (op1) == INTEGER_CST))
1428 && c_inhibit_evaluation_warnings == 0)
1429 /* ...then maybe we can detect an overflow. */
1430 maybe_warn_shift_overflow (loc, op0, op1);
b57910fa 1431 if ((code == TRUNC_DIV_EXPR
1432 || code == CEIL_DIV_EXPR
1433 || code == FLOOR_DIV_EXPR
1434 || code == EXACT_DIV_EXPR
1435 || code == TRUNC_MOD_EXPR)
1436 && TREE_CODE (orig_op1) != INTEGER_CST
1437 && TREE_CODE (op1) == INTEGER_CST
1438 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1439 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1440 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE)
1441 warn_for_div_by_zero (loc, op1);
a75b1c71 1442 goto out;
1443
1444 case INDIRECT_REF:
1445 case FIX_TRUNC_EXPR:
1446 case FLOAT_EXPR:
1447 CASE_CONVERT:
e66325ea 1448 case ADDR_SPACE_CONVERT_EXPR:
2b19dfe4 1449 case VIEW_CONVERT_EXPR:
a75b1c71 1450 case NON_LVALUE_EXPR:
1451 case NEGATE_EXPR:
1452 case BIT_NOT_EXPR:
1453 case TRUTH_NOT_EXPR:
1454 case ADDR_EXPR:
1455 case CONJ_EXPR:
1456 case REALPART_EXPR:
1457 case IMAGPART_EXPR:
1458 /* Unary operations. */
1459 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1460 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1461 maybe_const_itself, for_int_const);
f59e3889 1462 STRIP_TYPE_NOPS (op0);
a75b1c71 1463 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1464 op0 = decl_constant_value_for_optimization (op0);
aac24642 1465
1466 if (for_int_const && TREE_CODE (op0) != INTEGER_CST)
1467 goto out;
1468
737a23cc 1469 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1470 not prepared to deal with them if they occur in initializers. */
1471 if (op0 != orig_op0
1472 && code == ADDR_EXPR
1473 && (op1 = get_base_address (op0)) != NULL_TREE
aa3e402e 1474 && INDIRECT_REF_P (op1)
737a23cc 1475 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
7549df0d 1476 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
737a23cc 1477 else if (op0 != orig_op0 || in_init)
a75b1c71 1478 ret = in_init
389dd41b 1479 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1480 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
a75b1c71 1481 else
1482 ret = fold (expr);
1483 if (code == INDIRECT_REF
1484 && ret != expr
aa3e402e 1485 && INDIRECT_REF_P (ret))
a75b1c71 1486 {
1487 TREE_READONLY (ret) = TREE_READONLY (expr);
1488 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1489 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1490 }
672d914b 1491 switch (code)
1492 {
1493 case FIX_TRUNC_EXPR:
1494 case FLOAT_EXPR:
1495 CASE_CONVERT:
1496 /* Don't warn about explicit conversions. We will already
1497 have warned about suspect implicit conversions. */
1498 break;
1499
1500 default:
1501 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1502 overflow_warning (EXPR_LOCATION (expr), ret);
1503 break;
1504 }
a75b1c71 1505 goto out;
1506
1507 case TRUTH_ANDIF_EXPR:
1508 case TRUTH_ORIF_EXPR:
1509 /* Binary operations not necessarily evaluating both
1510 arguments. */
1511 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1512 orig_op1 = op1 = TREE_OPERAND (expr, 1);
aac24642 1513 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1514 for_int_const);
f59e3889 1515 STRIP_TYPE_NOPS (op0);
672d914b 1516
1517 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1518 ? truthvalue_false_node
1519 : truthvalue_true_node));
fc501191 1520 c_disable_warnings (unused_p);
aac24642 1521 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1522 for_int_const);
f59e3889 1523 STRIP_TYPE_NOPS (op1);
fc501191 1524 c_enable_warnings (unused_p);
672d914b 1525
aac24642 1526 if (for_int_const
1527 && (TREE_CODE (op0) != INTEGER_CST
1528 /* Require OP1 be an INTEGER_CST only if it's evaluated. */
1529 || (!unused_p && TREE_CODE (op1) != INTEGER_CST)))
1530 goto out;
1531
a75b1c71 1532 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1533 ret = in_init
389dd41b 1534 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1535 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
a75b1c71 1536 else
1537 ret = fold (expr);
1538 *maybe_const_operands &= op0_const;
1539 *maybe_const_itself &= op0_const_self;
1540 if (!(flag_isoc99
1541 && op0_const
1542 && op0_const_self
1543 && (code == TRUTH_ANDIF_EXPR
1544 ? op0 == truthvalue_false_node
1545 : op0 == truthvalue_true_node)))
1546 *maybe_const_operands &= op1_const;
1547 if (!(op0_const
1548 && op0_const_self
1549 && (code == TRUTH_ANDIF_EXPR
1550 ? op0 == truthvalue_false_node
1551 : op0 == truthvalue_true_node)))
1552 *maybe_const_itself &= op1_const_self;
1553 goto out;
1554
1555 case COND_EXPR:
1556 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1557 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1558 orig_op2 = op2 = TREE_OPERAND (expr, 2);
aac24642 1559 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1560 for_int_const);
672d914b 1561
f59e3889 1562 STRIP_TYPE_NOPS (op0);
fc501191 1563 c_disable_warnings (op0 == truthvalue_false_node);
aac24642 1564 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1565 for_int_const);
f59e3889 1566 STRIP_TYPE_NOPS (op1);
fc501191 1567 c_enable_warnings (op0 == truthvalue_false_node);
672d914b 1568
fc501191 1569 c_disable_warnings (op0 == truthvalue_true_node);
aac24642 1570 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self,
1571 for_int_const);
f59e3889 1572 STRIP_TYPE_NOPS (op2);
fc501191 1573 c_enable_warnings (op0 == truthvalue_true_node);
672d914b 1574
aac24642 1575 if (for_int_const
1576 && (TREE_CODE (op0) != INTEGER_CST
1577 /* Only the evaluated operand must be an INTEGER_CST. */
1578 || (op0 == truthvalue_true_node
1579 ? TREE_CODE (op1) != INTEGER_CST
1580 : TREE_CODE (op2) != INTEGER_CST)))
1581 goto out;
1582
a75b1c71 1583 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
389dd41b 1584 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
a75b1c71 1585 else
1586 ret = fold (expr);
1587 *maybe_const_operands &= op0_const;
1588 *maybe_const_itself &= op0_const_self;
1589 if (!(flag_isoc99
1590 && op0_const
1591 && op0_const_self
1592 && op0 == truthvalue_false_node))
1593 *maybe_const_operands &= op1_const;
1594 if (!(op0_const
1595 && op0_const_self
1596 && op0 == truthvalue_false_node))
1597 *maybe_const_itself &= op1_const_self;
1598 if (!(flag_isoc99
1599 && op0_const
1600 && op0_const_self
1601 && op0 == truthvalue_true_node))
1602 *maybe_const_operands &= op2_const;
1603 if (!(op0_const
1604 && op0_const_self
1605 && op0 == truthvalue_true_node))
1606 *maybe_const_itself &= op2_const_self;
1607 goto out;
1608
c6418a4e 1609 case EXCESS_PRECISION_EXPR:
1610 /* Each case where an operand with excess precision may be
1611 encountered must remove the EXCESS_PRECISION_EXPR around
1612 inner operands and possibly put one around the whole
1613 expression or possibly convert to the semantic type (which
1614 c_fully_fold does); we cannot tell at this stage which is
1615 appropriate in any particular case. */
1616 gcc_unreachable ();
1617
a75b1c71 1618 default:
1619 /* Various codes may appear through folding built-in functions
1620 and their arguments. */
1621 goto out;
1622 }
1623
1624 out:
1625 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1626 have been done by this point, so remove them again. */
1627 nowarning |= TREE_NO_WARNING (ret);
1628 STRIP_TYPE_NOPS (ret);
1629 if (nowarning && !TREE_NO_WARNING (ret))
1630 {
1631 if (!CAN_HAVE_LOCATION_P (ret))
1632 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1633 TREE_NO_WARNING (ret) = 1;
1634 }
1635 if (ret != expr)
a96cefb2 1636 {
1637 protected_set_expr_location (ret, loc);
1638 if (IS_EXPR_CODE_CLASS (kind))
1639 set_source_range (ret, old_range.m_start, old_range.m_finish);
1640 }
a75b1c71 1641 return ret;
1642}
1643
1644/* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1645 return EXP. Otherwise, return either EXP or its known constant
1646 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1647 Is the BLKmode test appropriate? */
1648
1649tree
1650decl_constant_value_for_optimization (tree exp)
1651{
1652 tree ret;
1653
1654 /* This function is only used by C, for c_fully_fold and other
1655 optimization, and may not be correct for C++. */
1656 if (c_dialect_cxx ())
1657 gcc_unreachable ();
1658
1659 if (!optimize
f48c7f4a 1660 || !VAR_P (exp)
a75b1c71 1661 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1662 || DECL_MODE (exp) == BLKmode)
1663 return exp;
1664
1665 ret = decl_constant_value (exp);
1666 /* Avoid unwanted tree sharing between the initializer and current
1667 function's body where the tree can be modified e.g. by the
1668 gimplifier. */
1669 if (ret != exp && TREE_STATIC (exp))
1670 ret = unshare_expr (ret);
1671 return ret;
1672}
1673
2a1736ed 1674/* Print a warning if a constant expression had overflow in folding.
1675 Invoke this function on every expression that the language
1676 requires to be a constant expression.
1677 Note the ANSI C standard says it is erroneous for a
1678 constant expression to overflow. */
b2806639 1679
1680void
1cae46be 1681constant_expression_warning (tree value)
07317e69 1682{
48e1416a 1683 if (warn_overflow && pedantic
07317e69 1684 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1685 || TREE_CODE (value) == FIXED_CST
1686 || TREE_CODE (value) == VECTOR_CST
1687 || TREE_CODE (value) == COMPLEX_CST)
1688 && TREE_OVERFLOW (value))
21ca8540 1689 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
07317e69 1690}
1691
1692/* The same as above but print an unconditional error. */
1693void
1694constant_expression_error (tree value)
b2806639 1695{
837e1122 1696 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
9421ebb9 1697 || TREE_CODE (value) == FIXED_CST
886cfd4f 1698 || TREE_CODE (value) == VECTOR_CST
837e1122 1699 || TREE_CODE (value) == COMPLEX_CST)
07317e69 1700 && TREE_OVERFLOW (value))
1701 error ("overflow in constant expression");
2a1736ed 1702}
1703
f170d67f 1704/* Print a warning if an expression had overflow in folding and its
1705 operands hadn't.
1706
2a1736ed 1707 Invoke this function on every expression that
1708 (1) appears in the source code, and
f170d67f 1709 (2) is a constant expression that overflowed, and
2a1736ed 1710 (3) is not already checked by convert_and_check;
f170d67f 1711 however, do not invoke this function on operands of explicit casts
1712 or when the expression is the result of an operator and any operand
1713 already overflowed. */
2a1736ed 1714
1715void
e60a6f7b 1716overflow_warning (location_t loc, tree value)
2a1736ed 1717{
48d94ede 1718 if (c_inhibit_evaluation_warnings != 0)
1719 return;
f170d67f 1720
1721 switch (TREE_CODE (value))
886cfd4f 1722 {
f170d67f 1723 case INTEGER_CST:
e60a6f7b 1724 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
f170d67f 1725 break;
48e1416a 1726
f170d67f 1727 case REAL_CST:
e60a6f7b 1728 warning_at (loc, OPT_Woverflow,
1729 "floating point overflow in expression");
f170d67f 1730 break;
48e1416a 1731
9421ebb9 1732 case FIXED_CST:
e60a6f7b 1733 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
9421ebb9 1734 break;
1735
f170d67f 1736 case VECTOR_CST:
e60a6f7b 1737 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
f170d67f 1738 break;
48e1416a 1739
f170d67f 1740 case COMPLEX_CST:
1741 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
e60a6f7b 1742 warning_at (loc, OPT_Woverflow,
1743 "complex integer overflow in expression");
f170d67f 1744 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
e60a6f7b 1745 warning_at (loc, OPT_Woverflow,
1746 "complex floating point overflow in expression");
f170d67f 1747 break;
1748
1749 default:
1750 break;
886cfd4f 1751 }
2a1736ed 1752}
1753
03033af4 1754/* Warn about uses of logical || / && operator in a context where it
1755 is likely that the bitwise equivalent was intended by the
1756 programmer. We have seen an expression in which CODE is a binary
9c20c4fc 1757 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1758 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
b13d1547 1759void
9c20c4fc 1760warn_logical_operator (location_t location, enum tree_code code, tree type,
48e1416a 1761 enum tree_code code_left, tree op_left,
03033af4 1762 enum tree_code ARG_UNUSED (code_right), tree op_right)
b13d1547 1763{
9c20c4fc 1764 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1765 int in0_p, in1_p, in_p;
1766 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1767 bool strict_overflow_p = false;
1768
03033af4 1769 if (code != TRUTH_ANDIF_EXPR
1770 && code != TRUTH_AND_EXPR
1771 && code != TRUTH_ORIF_EXPR
1772 && code != TRUTH_OR_EXPR)
1773 return;
1774
439606a9 1775 /* We don't want to warn if either operand comes from a macro
1776 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1777 see PR61534. */
1778 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1779 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1780 return;
1781
03033af4 1782 /* Warn if &&/|| are being used in a context where it is
1783 likely that the bitwise equivalent was intended by the
1784 programmer. That is, an expression such as op && MASK
1785 where op should not be any boolean expression, nor a
1786 constant, and mask seems to be a non-boolean integer constant. */
1787 if (!truth_value_p (code_left)
1788 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1789 && !CONSTANT_CLASS_P (op_left)
1790 && !TREE_NO_WARNING (op_left)
1791 && TREE_CODE (op_right) == INTEGER_CST
1792 && !integer_zerop (op_right)
1793 && !integer_onep (op_right))
b13d1547 1794 {
9c20c4fc 1795 if (or_op)
03033af4 1796 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1797 " applied to non-boolean constant");
1798 else
1799 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1800 " applied to non-boolean constant");
1801 TREE_NO_WARNING (op_left) = true;
9c20c4fc 1802 return;
1803 }
1804
1805 /* We do not warn for constants because they are typical of macro
1806 expansions that test for features. */
1807 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1808 return;
1809
1810 /* This warning only makes sense with logical operands. */
1811 if (!(truth_value_p (TREE_CODE (op_left))
1812 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1813 || !(truth_value_p (TREE_CODE (op_right))
1814 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1815 return;
1816
5e84569c 1817 /* The range computations only work with scalars. */
1818 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1819 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1820 return;
9c20c4fc 1821
686369e8 1822 /* We first test whether either side separately is trivially true
1823 (with OR) or trivially false (with AND). If so, do not warn.
1824 This is a common idiom for testing ranges of data types in
1825 portable code. */
1826 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1827 if (!lhs)
1828 return;
1829 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
9c20c4fc 1830 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1831
686369e8 1832 /* If this is an OR operation, invert both sides; now, the result
1833 should be always false to get a warning. */
1834 if (or_op)
1835 in0_p = !in0_p;
1836
1837 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
d42e7c5a 1838 if (tem && integer_zerop (tem))
686369e8 1839 return;
1840
1841 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1842 if (!rhs)
1843 return;
1844 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
9c20c4fc 1845 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
48e1416a 1846
686369e8 1847 /* If this is an OR operation, invert both sides; now, the result
1848 should be always false to get a warning. */
9c20c4fc 1849 if (or_op)
686369e8 1850 in1_p = !in1_p;
48e1416a 1851
686369e8 1852 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
d42e7c5a 1853 if (tem && integer_zerop (tem))
686369e8 1854 return;
1855
1856 /* If both expressions have the same operand, if we can merge the
485f6b9c 1857 ranges, ... */
686369e8 1858 if (operand_equal_p (lhs, rhs, 0)
9c20c4fc 1859 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
485f6b9c 1860 in1_p, low1, high1))
9c20c4fc 1861 {
485f6b9c 1862 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1863 /* ... and if the range test is always false, then warn. */
1864 if (tem && integer_zerop (tem))
1865 {
1866 if (or_op)
1867 warning_at (location, OPT_Wlogical_op,
1868 "logical %<or%> of collectively exhaustive tests is "
1869 "always true");
1870 else
1871 warning_at (location, OPT_Wlogical_op,
1872 "logical %<and%> of mutually exclusive tests is "
1873 "always false");
1874 }
1875 /* Or warn if the operands have exactly the same range, e.g.
1876 A > 0 && A > 0. */
0200602e 1877 else if (tree_int_cst_equal (low0, low1)
1878 && tree_int_cst_equal (high0, high1))
485f6b9c 1879 {
1880 if (or_op)
1881 warning_at (location, OPT_Wlogical_op,
1882 "logical %<or%> of equal expressions");
1883 else
1884 warning_at (location, OPT_Wlogical_op,
1885 "logical %<and%> of equal expressions");
1886 }
b13d1547 1887 }
1888}
1889
6784a472 1890/* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
1891 with constant indices. */
1892
1893static tree
1894find_array_ref_with_const_idx_r (tree *expr_p, int *walk_subtrees, void *data)
1895{
1896 tree expr = *expr_p;
1897
1898 if ((TREE_CODE (expr) == ARRAY_REF
1899 || TREE_CODE (expr) == ARRAY_RANGE_REF)
1900 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
1901 {
1902 *(bool *) data = true;
1903 *walk_subtrees = 0;
1904 }
1905
1906 return NULL_TREE;
1907}
1908
1909/* Warn if a self-comparison always evaluates to true or false. LOC
1910 is the location of the comparison with code CODE, LHS and RHS are
1911 operands of the comparison. */
1912
1913void
1914warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
1915{
1916 if (TREE_CODE_CLASS (code) != tcc_comparison)
1917 return;
1918
d9e4fe27 1919 /* Don't warn for various macro expansions. */
1920 if (from_macro_expansion_at (loc)
1921 || from_macro_expansion_at (EXPR_LOCATION (lhs))
1922 || from_macro_expansion_at (EXPR_LOCATION (rhs)))
1923 return;
1924
6784a472 1925 /* We do not warn for constants because they are typical of macro
1926 expansions that test for features, sizeof, and similar. */
7991eeee 1927 if (CONSTANT_CLASS_P (fold (lhs)) || CONSTANT_CLASS_P (fold (rhs)))
6784a472 1928 return;
1929
1930 /* Don't warn for e.g.
1931 HOST_WIDE_INT n;
1932 ...
1933 if (n == (long) n) ...
1934 */
1935 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
1936 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
1937 return;
1938
f58dd84b 1939 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
1940 It could be a NaN, and NaN never compares equal to anything, even
1941 itself. */
1942 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
1943 return;
1944
6784a472 1945 if (operand_equal_p (lhs, rhs, 0))
1946 {
1947 /* Don't warn about array references with constant indices;
1948 these are likely to come from a macro. */
1949 bool found = false;
1950 walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
1951 &found);
1952 if (found)
1953 return;
1954 const bool always_true = (code == EQ_EXPR || code == LE_EXPR
1955 || code == GE_EXPR || code == UNLE_EXPR
1956 || code == UNGE_EXPR || code == UNEQ_EXPR);
1957 if (always_true)
1958 warning_at (loc, OPT_Wtautological_compare,
1959 "self-comparison always evaluates to true");
1960 else
1961 warning_at (loc, OPT_Wtautological_compare,
1962 "self-comparison always evaluates to false");
1963 }
1964}
1965
32dc1512 1966/* Warn about logical not used on the left hand side operand of a comparison.
1967 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
dc6229e8 1968 Do not warn if RHS is of a boolean type. */
32dc1512 1969
1970void
1971warn_logical_not_parentheses (location_t location, enum tree_code code,
dc6229e8 1972 tree rhs)
32dc1512 1973{
dc6229e8 1974 if (TREE_CODE_CLASS (code) != tcc_comparison
1975 || TREE_TYPE (rhs) == NULL_TREE
1976 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
32dc1512 1977 return;
1978
16f958b3 1979 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1980 !(x == 0) or !(y != 0). */
1981 if ((code == EQ_EXPR || code == NE_EXPR)
1982 && integer_zerop (rhs))
1983 return;
1984
32dc1512 1985 warning_at (location, OPT_Wlogical_not_parentheses,
1986 "logical not is only applied to the left hand side of "
1987 "comparison");
1988}
b13d1547 1989
3d177e8c 1990/* Warn if EXP contains any computations whose results are not used.
1991 Return true if a warning is printed; false otherwise. LOCUS is the
1992 (potential) location of the expression. */
1993
1994bool
1995warn_if_unused_value (const_tree exp, location_t locus)
1996{
1997 restart:
1998 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1999 return false;
2000
2001 /* Don't warn about void constructs. This includes casting to void,
2002 void function calls, and statement expressions with a final cast
2003 to void. */
2004 if (VOID_TYPE_P (TREE_TYPE (exp)))
2005 return false;
2006
2007 if (EXPR_HAS_LOCATION (exp))
2008 locus = EXPR_LOCATION (exp);
2009
2010 switch (TREE_CODE (exp))
2011 {
2012 case PREINCREMENT_EXPR:
2013 case POSTINCREMENT_EXPR:
2014 case PREDECREMENT_EXPR:
2015 case POSTDECREMENT_EXPR:
2016 case MODIFY_EXPR:
2017 case INIT_EXPR:
2018 case TARGET_EXPR:
2019 case CALL_EXPR:
2020 case TRY_CATCH_EXPR:
2021 case WITH_CLEANUP_EXPR:
2022 case EXIT_EXPR:
2023 case VA_ARG_EXPR:
2024 return false;
2025
2026 case BIND_EXPR:
2027 /* For a binding, warn if no side effect within it. */
2028 exp = BIND_EXPR_BODY (exp);
2029 goto restart;
2030
2031 case SAVE_EXPR:
2032 case NON_LVALUE_EXPR:
d85dbdb3 2033 case NOP_EXPR:
3d177e8c 2034 exp = TREE_OPERAND (exp, 0);
2035 goto restart;
2036
2037 case TRUTH_ORIF_EXPR:
2038 case TRUTH_ANDIF_EXPR:
2039 /* In && or ||, warn if 2nd operand has no side effect. */
2040 exp = TREE_OPERAND (exp, 1);
2041 goto restart;
2042
2043 case COMPOUND_EXPR:
2044 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
2045 return true;
2046 /* Let people do `(foo (), 0)' without a warning. */
2047 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
2048 return false;
2049 exp = TREE_OPERAND (exp, 1);
2050 goto restart;
2051
2052 case COND_EXPR:
2053 /* If this is an expression with side effects, don't warn; this
2054 case commonly appears in macro expansions. */
2055 if (TREE_SIDE_EFFECTS (exp))
2056 return false;
2057 goto warn;
2058
2059 case INDIRECT_REF:
2060 /* Don't warn about automatic dereferencing of references, since
2061 the user cannot control it. */
2062 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
2063 {
2064 exp = TREE_OPERAND (exp, 0);
2065 goto restart;
2066 }
2067 /* Fall through. */
2068
2069 default:
2070 /* Referencing a volatile value is a side effect, so don't warn. */
2071 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
2072 && TREE_THIS_VOLATILE (exp))
2073 return false;
2074
2075 /* If this is an expression which has no operands, there is no value
2076 to be unused. There are no such language-independent codes,
2077 but front ends may define such. */
2078 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
2079 return false;
2080
2081 warn:
2082 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
2083 }
2084}
2085
2086
bcf22371 2087/* Print a warning about casts that might indicate violation
2088 of strict aliasing rules if -Wstrict-aliasing is used and
1e31ff37 2089 strict aliasing mode is in effect. OTYPE is the original
2090 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
bcf22371 2091
e6fa0ea6 2092bool
1e31ff37 2093strict_aliasing_warning (tree otype, tree type, tree expr)
bcf22371 2094{
45bb3afb 2095 /* Strip pointer conversion chains and get to the correct original type. */
2096 STRIP_NOPS (expr);
2097 otype = TREE_TYPE (expr);
2098
f06537f2 2099 if (!(flag_strict_aliasing
2100 && POINTER_TYPE_P (type)
2101 && POINTER_TYPE_P (otype)
2102 && !VOID_TYPE_P (TREE_TYPE (type)))
2103 /* If the type we are casting to is a ref-all pointer
2104 dereferencing it is always valid. */
2105 || TYPE_REF_CAN_ALIAS_ALL (type))
e6fa0ea6 2106 return false;
2107
2108 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
bcf22371 2109 && (DECL_P (TREE_OPERAND (expr, 0))
e6fa0ea6 2110 || handled_component_p (TREE_OPERAND (expr, 0))))
bcf22371 2111 {
2112 /* Casting the address of an object to non void pointer. Warn
2113 if the cast breaks type based aliasing. */
e6fa0ea6 2114 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
2115 {
2116 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
2117 "might break strict-aliasing rules");
2118 return true;
2119 }
bcf22371 2120 else
2121 {
48e1416a 2122 /* warn_strict_aliasing >= 3. This includes the default (3).
e6fa0ea6 2123 Only warn if the cast is dereferenced immediately. */
32c2fdea 2124 alias_set_type set1 =
e6fa0ea6 2125 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
32c2fdea 2126 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
bcf22371 2127
62d823d0 2128 if (set1 != set2 && set2 != 0
2129 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
e6fa0ea6 2130 {
2131 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2132 "pointer will break strict-aliasing rules");
2133 return true;
2134 }
2135 else if (warn_strict_aliasing == 2
879f881c 2136 && !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 2137 {
2138 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2139 "pointer might break strict-aliasing rules");
2140 return true;
2141 }
bcf22371 2142 }
2143 }
e6fa0ea6 2144 else
2145 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
2146 {
2147 /* At this level, warn for any conversions, even if an address is
2148 not taken in the same statement. This will likely produce many
2149 false positives, but could be useful to pinpoint problems that
2150 are not revealed at higher levels. */
32c2fdea 2151 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
2152 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
2153 if (!COMPLETE_TYPE_P (type)
879f881c 2154 || !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 2155 {
2156 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2157 "pointer might break strict-aliasing rules");
2158 return true;
2159 }
2160 }
2161
2162 return false;
bcf22371 2163}
2164
f003f5dc 2165/* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
2166 sizeof as last operand of certain builtins. */
2167
2168void
57f872a2 2169sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
f1f41a6c 2170 vec<tree, va_gc> *params, tree *sizeof_arg,
f003f5dc 2171 bool (*comp_types) (tree, tree))
2172{
2173 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
57f872a2 2174 bool strop = false, cmp = false;
2175 unsigned int idx = ~0;
2176 location_t loc;
f003f5dc 2177
2178 if (TREE_CODE (callee) != FUNCTION_DECL
2179 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
f1f41a6c 2180 || vec_safe_length (params) <= 1)
f003f5dc 2181 return;
2182
f003f5dc 2183 switch (DECL_FUNCTION_CODE (callee))
2184 {
2185 case BUILT_IN_STRNCMP:
2186 case BUILT_IN_STRNCASECMP:
57f872a2 2187 cmp = true;
2188 /* FALLTHRU */
f003f5dc 2189 case BUILT_IN_STRNCPY:
57f872a2 2190 case BUILT_IN_STRNCPY_CHK:
f003f5dc 2191 case BUILT_IN_STRNCAT:
57f872a2 2192 case BUILT_IN_STRNCAT_CHK:
2193 case BUILT_IN_STPNCPY:
2194 case BUILT_IN_STPNCPY_CHK:
f003f5dc 2195 strop = true;
2196 /* FALLTHRU */
2197 case BUILT_IN_MEMCPY:
57f872a2 2198 case BUILT_IN_MEMCPY_CHK:
f003f5dc 2199 case BUILT_IN_MEMMOVE:
57f872a2 2200 case BUILT_IN_MEMMOVE_CHK:
f1f41a6c 2201 if (params->length () < 3)
57f872a2 2202 return;
f1f41a6c 2203 src = (*params)[1];
2204 dest = (*params)[0];
57f872a2 2205 idx = 2;
2206 break;
2207 case BUILT_IN_BCOPY:
f1f41a6c 2208 if (params->length () < 3)
57f872a2 2209 return;
f1f41a6c 2210 src = (*params)[0];
2211 dest = (*params)[1];
57f872a2 2212 idx = 2;
2213 break;
f003f5dc 2214 case BUILT_IN_MEMCMP:
57f872a2 2215 case BUILT_IN_BCMP:
f1f41a6c 2216 if (params->length () < 3)
f003f5dc 2217 return;
f1f41a6c 2218 src = (*params)[1];
2219 dest = (*params)[0];
57f872a2 2220 idx = 2;
2221 cmp = true;
f003f5dc 2222 break;
2223 case BUILT_IN_MEMSET:
57f872a2 2224 case BUILT_IN_MEMSET_CHK:
f1f41a6c 2225 if (params->length () < 3)
f003f5dc 2226 return;
f1f41a6c 2227 dest = (*params)[0];
57f872a2 2228 idx = 2;
2229 break;
2230 case BUILT_IN_BZERO:
f1f41a6c 2231 dest = (*params)[0];
57f872a2 2232 idx = 1;
f003f5dc 2233 break;
2234 case BUILT_IN_STRNDUP:
f1f41a6c 2235 src = (*params)[0];
f003f5dc 2236 strop = true;
57f872a2 2237 idx = 1;
2238 break;
2239 case BUILT_IN_MEMCHR:
f1f41a6c 2240 if (params->length () < 3)
57f872a2 2241 return;
f1f41a6c 2242 src = (*params)[0];
57f872a2 2243 idx = 2;
2244 break;
2245 case BUILT_IN_SNPRINTF:
2246 case BUILT_IN_SNPRINTF_CHK:
2247 case BUILT_IN_VSNPRINTF:
2248 case BUILT_IN_VSNPRINTF_CHK:
f1f41a6c 2249 dest = (*params)[0];
57f872a2 2250 idx = 1;
2251 strop = true;
f003f5dc 2252 break;
2253 default:
2254 break;
2255 }
2256
57f872a2 2257 if (idx >= 3)
2258 return;
2259
2260 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2261 return;
2262
2263 type = TYPE_P (sizeof_arg[idx])
2264 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2265 if (!POINTER_TYPE_P (type))
2266 return;
2267
f003f5dc 2268 if (dest
2269 && (tem = tree_strip_nop_conversions (dest))
2270 && POINTER_TYPE_P (TREE_TYPE (tem))
2271 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2272 return;
2273
2274 if (src
2275 && (tem = tree_strip_nop_conversions (src))
2276 && POINTER_TYPE_P (TREE_TYPE (tem))
2277 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2278 return;
2279
57f872a2 2280 loc = sizeof_arg_loc[idx];
2281
2282 if (dest && !cmp)
f003f5dc 2283 {
57f872a2 2284 if (!TYPE_P (sizeof_arg[idx])
2285 && operand_equal_p (dest, sizeof_arg[idx], 0)
f003f5dc 2286 && comp_types (TREE_TYPE (dest), type))
2287 {
57f872a2 2288 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
f003f5dc 2289 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2290 "argument to %<sizeof%> in %qD call is the same "
2291 "expression as the destination; did you mean to "
2292 "remove the addressof?", callee);
2293 else if ((TYPE_PRECISION (TREE_TYPE (type))
2294 == TYPE_PRECISION (char_type_node))
2295 || strop)
2296 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2297 "argument to %<sizeof%> in %qD call is the same "
2298 "expression as the destination; did you mean to "
2299 "provide an explicit length?", callee);
2300 else
2301 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2302 "argument to %<sizeof%> in %qD call is the same "
2303 "expression as the destination; did you mean to "
2304 "dereference it?", callee);
2305 return;
2306 }
2307
2308 if (POINTER_TYPE_P (TREE_TYPE (dest))
2309 && !strop
2310 && comp_types (TREE_TYPE (dest), type)
2311 && !VOID_TYPE_P (TREE_TYPE (type)))
2312 {
2313 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2314 "argument to %<sizeof%> in %qD call is the same "
2315 "pointer type %qT as the destination; expected %qT "
2316 "or an explicit length", callee, TREE_TYPE (dest),
2317 TREE_TYPE (TREE_TYPE (dest)));
2318 return;
2319 }
2320 }
2321
57f872a2 2322 if (src && !cmp)
f003f5dc 2323 {
57f872a2 2324 if (!TYPE_P (sizeof_arg[idx])
2325 && operand_equal_p (src, sizeof_arg[idx], 0)
f003f5dc 2326 && comp_types (TREE_TYPE (src), type))
2327 {
57f872a2 2328 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
f003f5dc 2329 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2330 "argument to %<sizeof%> in %qD call is the same "
2331 "expression as the source; did you mean to "
2332 "remove the addressof?", callee);
2333 else if ((TYPE_PRECISION (TREE_TYPE (type))
2334 == TYPE_PRECISION (char_type_node))
2335 || strop)
2336 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2337 "argument to %<sizeof%> in %qD call is the same "
2338 "expression as the source; did you mean to "
2339 "provide an explicit length?", callee);
2340 else
2341 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2342 "argument to %<sizeof%> in %qD call is the same "
2343 "expression as the source; did you mean to "
2344 "dereference it?", callee);
2345 return;
2346 }
2347
2348 if (POINTER_TYPE_P (TREE_TYPE (src))
2349 && !strop
2350 && comp_types (TREE_TYPE (src), type)
2351 && !VOID_TYPE_P (TREE_TYPE (type)))
2352 {
2353 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2354 "argument to %<sizeof%> in %qD call is the same "
2355 "pointer type %qT as the source; expected %qT "
2356 "or an explicit length", callee, TREE_TYPE (src),
2357 TREE_TYPE (TREE_TYPE (src)));
2358 return;
2359 }
2360 }
57f872a2 2361
2362 if (dest)
2363 {
2364 if (!TYPE_P (sizeof_arg[idx])
2365 && operand_equal_p (dest, sizeof_arg[idx], 0)
2366 && comp_types (TREE_TYPE (dest), type))
2367 {
2368 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2369 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2370 "argument to %<sizeof%> in %qD call is the same "
2371 "expression as the first source; did you mean to "
2372 "remove the addressof?", callee);
2373 else if ((TYPE_PRECISION (TREE_TYPE (type))
2374 == TYPE_PRECISION (char_type_node))
2375 || strop)
2376 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2377 "argument to %<sizeof%> in %qD call is the same "
2378 "expression as the first source; did you mean to "
2379 "provide an explicit length?", callee);
2380 else
2381 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2382 "argument to %<sizeof%> in %qD call is the same "
2383 "expression as the first source; did you mean to "
2384 "dereference it?", callee);
2385 return;
2386 }
2387
2388 if (POINTER_TYPE_P (TREE_TYPE (dest))
2389 && !strop
2390 && comp_types (TREE_TYPE (dest), type)
2391 && !VOID_TYPE_P (TREE_TYPE (type)))
2392 {
2393 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2394 "argument to %<sizeof%> in %qD call is the same "
2395 "pointer type %qT as the first source; expected %qT "
2396 "or an explicit length", callee, TREE_TYPE (dest),
2397 TREE_TYPE (TREE_TYPE (dest)));
2398 return;
2399 }
2400 }
2401
2402 if (src)
2403 {
2404 if (!TYPE_P (sizeof_arg[idx])
2405 && operand_equal_p (src, sizeof_arg[idx], 0)
2406 && comp_types (TREE_TYPE (src), type))
2407 {
2408 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2409 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2410 "argument to %<sizeof%> in %qD call is the same "
2411 "expression as the second source; did you mean to "
2412 "remove the addressof?", callee);
2413 else if ((TYPE_PRECISION (TREE_TYPE (type))
2414 == TYPE_PRECISION (char_type_node))
2415 || strop)
2416 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2417 "argument to %<sizeof%> in %qD call is the same "
2418 "expression as the second source; did you mean to "
2419 "provide an explicit length?", callee);
2420 else
2421 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2422 "argument to %<sizeof%> in %qD call is the same "
2423 "expression as the second source; did you mean to "
2424 "dereference it?", callee);
2425 return;
2426 }
2427
2428 if (POINTER_TYPE_P (TREE_TYPE (src))
2429 && !strop
2430 && comp_types (TREE_TYPE (src), type)
2431 && !VOID_TYPE_P (TREE_TYPE (type)))
2432 {
2433 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2434 "argument to %<sizeof%> in %qD call is the same "
2435 "pointer type %qT as the second source; expected %qT "
2436 "or an explicit length", callee, TREE_TYPE (src),
2437 TREE_TYPE (TREE_TYPE (src)));
2438 return;
2439 }
2440 }
2441
f003f5dc 2442}
2443
3f08e399 2444/* Warn for unlikely, improbable, or stupid DECL declarations
2445 of `main'. */
2446
2447void
2448check_main_parameter_types (tree decl)
2449{
d0af78c5 2450 function_args_iterator iter;
2451 tree type;
3f08e399 2452 int argct = 0;
2453
d0af78c5 2454 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2455 {
2456 /* XXX void_type_node belies the abstraction. */
2457 if (type == void_type_node || type == error_mark_node )
2458 break;
2459
2026249a 2460 tree t = type;
2461 if (TYPE_ATOMIC (t))
2462 pedwarn (input_location, OPT_Wmain,
2463 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2464 type, decl);
2465 while (POINTER_TYPE_P (t))
2466 {
2467 t = TREE_TYPE (t);
2468 if (TYPE_ATOMIC (t))
2469 pedwarn (input_location, OPT_Wmain,
2470 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2471 type, decl);
2472 }
2473
d0af78c5 2474 ++argct;
2475 switch (argct)
2476 {
2477 case 1:
2478 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2479 pedwarn (input_location, OPT_Wmain,
2480 "first argument of %q+D should be %<int%>", decl);
2481 break;
2482
2483 case 2:
2484 if (TREE_CODE (type) != POINTER_TYPE
2485 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2486 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2487 != char_type_node))
2488 pedwarn (input_location, OPT_Wmain,
2489 "second argument of %q+D should be %<char **%>", decl);
2490 break;
2491
2492 case 3:
2493 if (TREE_CODE (type) != POINTER_TYPE
2494 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2495 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2496 != char_type_node))
2497 pedwarn (input_location, OPT_Wmain,
2498 "third argument of %q+D should probably be "
2499 "%<char **%>", decl);
2500 break;
2501 }
2502 }
3f08e399 2503
2504 /* It is intentional that this message does not mention the third
2505 argument because it's only mentioned in an appendix of the
2506 standard. */
2507 if (argct > 0 && (argct < 2 || argct > 3))
d0af78c5 2508 pedwarn (input_location, OPT_Wmain,
2509 "%q+D takes only zero or two arguments", decl);
90e645fa 2510
2511 if (stdarg_p (TREE_TYPE (decl)))
2512 pedwarn (input_location, OPT_Wmain,
2513 "%q+D declared as variadic function", decl);
3f08e399 2514}
2515
73437615 2516/* vector_targets_convertible_p is used for vector pointer types. The
2517 callers perform various checks that the qualifiers are satisfactory,
2518 while OTOH vector_targets_convertible_p ignores the number of elements
2519 in the vectors. That's fine with vector pointers as we can consider,
2520 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2521 and that does not require and conversion of the pointer values.
2522 In contrast, vector_types_convertible_p and
2523 vector_types_compatible_elements_p are used for vector value types. */
ed7c4e62 2524/* True if pointers to distinct types T1 and T2 can be converted to
2525 each other without an explicit cast. Only returns true for opaque
2526 vector types. */
2527bool
2528vector_targets_convertible_p (const_tree t1, const_tree t2)
2529{
6290f0db 2530 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
8d125f7d 2531 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ed7c4e62 2532 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2533 return true;
2534
2535 return false;
2536}
2537
73437615 2538/* vector_types_convertible_p is used for vector value types.
2539 It could in principle call vector_targets_convertible_p as a subroutine,
2540 but then the check for vector type would be duplicated with its callers,
2541 and also the purpose of vector_targets_convertible_p would become
2542 muddled.
2543 Where vector_types_convertible_p returns true, a conversion might still be
2544 needed to make the types match.
2545 In contrast, vector_targets_convertible_p is used for vector pointer
2546 values, and vector_types_compatible_elements_p is used specifically
2547 in the context for binary operators, as a check if use is possible without
2548 conversion. */
546c4794 2549/* True if vector types T1 and T2 can be converted to each other
2550 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2551 can only be converted with -flax-vector-conversions yet that is not
2552 in effect, emit a note telling the user about that option if such
2553 a note has not previously been emitted. */
2554bool
9f627b1a 2555vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
8b4b9810 2556{
546c4794 2557 static bool emitted_lax_note = false;
ae6db8ab 2558 bool convertible_lax;
2559
8d125f7d 2560 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ae6db8ab 2561 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2562 return true;
2563
2564 convertible_lax =
2565 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2566 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
432dd330 2567 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
ae6db8ab 2568 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2569 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
546c4794 2570
2571 if (!convertible_lax || flag_lax_vector_conversions)
2572 return convertible_lax;
2573
2574 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
29f51994 2575 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
546c4794 2576 return true;
2577
2578 if (emit_lax_note && !emitted_lax_note)
2579 {
2580 emitted_lax_note = true;
5bcc316e 2581 inform (input_location, "use -flax-vector-conversions to permit "
546c4794 2582 "conversions between vectors with differing "
2583 "element types or numbers of subparts");
2584 }
2585
2586 return false;
8b4b9810 2587}
2588
bf0cb017 2589/* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2590 and have vector types, V0 has the same type as V1, and the number of
2591 elements of V0, V1, MASK is the same.
2592
2593 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2594 called with two arguments. In this case implementation passes the
2595 first argument twice in order to share the same tree code. This fact
2596 could enable the mask-values being twice the vector length. This is
2597 an implementation accident and this semantics is not guaranteed to
2598 the user. */
2599tree
68ea4406 2600c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2601 bool complain)
bf0cb017 2602{
2603 tree ret;
2604 bool wrap = true;
2605 bool maybe_const = false;
2606 bool two_arguments = false;
2607
2608 if (v1 == NULL_TREE)
2609 {
2610 two_arguments = true;
2611 v1 = v0;
2612 }
2613
2614 if (v0 == error_mark_node || v1 == error_mark_node
2615 || mask == error_mark_node)
2616 return error_mark_node;
2617
6290f0db 2618 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
bf0cb017 2619 {
68ea4406 2620 if (complain)
2621 error_at (loc, "__builtin_shuffle last argument must "
2622 "be an integer vector");
bf0cb017 2623 return error_mark_node;
2624 }
2625
6290f0db 2626 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
2627 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
bf0cb017 2628 {
68ea4406 2629 if (complain)
2630 error_at (loc, "__builtin_shuffle arguments must be vectors");
bf0cb017 2631 return error_mark_node;
2632 }
2633
2634 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2635 {
68ea4406 2636 if (complain)
2637 error_at (loc, "__builtin_shuffle argument vectors must be of "
2638 "the same type");
bf0cb017 2639 return error_mark_node;
2640 }
2641
2642 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2643 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2644 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2645 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2646 {
68ea4406 2647 if (complain)
2648 error_at (loc, "__builtin_shuffle number of elements of the "
2649 "argument vector(s) and the mask vector should "
2650 "be the same");
bf0cb017 2651 return error_mark_node;
2652 }
2653
2654 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2655 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2656 {
68ea4406 2657 if (complain)
2658 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2659 "must have the same size as inner type of the mask");
bf0cb017 2660 return error_mark_node;
2661 }
2662
2663 if (!c_dialect_cxx ())
2664 {
2665 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2666 v0 = c_fully_fold (v0, false, &maybe_const);
2667 wrap &= maybe_const;
2668
2669 if (two_arguments)
2670 v1 = v0 = save_expr (v0);
2671 else
2672 {
2673 v1 = c_fully_fold (v1, false, &maybe_const);
2674 wrap &= maybe_const;
2675 }
2676
2677 mask = c_fully_fold (mask, false, &maybe_const);
2678 wrap &= maybe_const;
2679 }
68ea4406 2680 else if (two_arguments)
2681 v1 = v0 = save_expr (v0);
bf0cb017 2682
2683 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2684
2685 if (!c_dialect_cxx () && !wrap)
2686 ret = c_wrap_maybe_const (ret, true);
2687
2688 return ret;
2689}
2690
7f506bca 2691/* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2692 to integral type. */
2693
2694static tree
2695c_common_get_narrower (tree op, int *unsignedp_ptr)
2696{
2697 op = get_narrower (op, unsignedp_ptr);
2698
2699 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2700 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2701 {
2702 /* C++0x scoped enumerations don't implicitly convert to integral
2703 type; if we stripped an explicit conversion to a larger type we
2704 need to replace it so common_type will still work. */
a51edb4c 2705 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2706 TYPE_UNSIGNED (TREE_TYPE (op)));
7f506bca 2707 op = fold_convert (type, op);
2708 }
2709 return op;
2710}
2711
2561cea2 2712/* This is a helper function of build_binary_op.
2713
2714 For certain operations if both args were extended from the same
2715 smaller type, do the arithmetic in that type and then extend.
2716
2717 BITWISE indicates a bitwise operation.
2718 For them, this optimization is safe only if
2719 both args are zero-extended or both are sign-extended.
2720 Otherwise, we might change the result.
2721 Eg, (short)-1 | (unsigned short)-1 is (int)-1
48e1416a 2722 but calculated in (unsigned short) it would be (unsigned short)-1.
2561cea2 2723*/
7f506bca 2724tree
2725shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2561cea2 2726{
2727 int unsigned0, unsigned1;
2728 tree arg0, arg1;
2729 int uns;
2730 tree type;
2731
2732 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2733 excessive narrowing when we call get_narrower below. For
2734 example, suppose that OP0 is of unsigned int extended
2735 from signed char and that RESULT_TYPE is long long int.
2736 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2737 like
48e1416a 2738
2561cea2 2739 (long long int) (unsigned int) signed_char
2740
2741 which get_narrower would narrow down to
48e1416a 2742
2561cea2 2743 (unsigned int) signed char
48e1416a 2744
2561cea2 2745 If we do not cast OP0 first, get_narrower would return
2746 signed_char, which is inconsistent with the case of the
2747 explicit cast. */
2748 op0 = convert (result_type, op0);
2749 op1 = convert (result_type, op1);
2750
7f506bca 2751 arg0 = c_common_get_narrower (op0, &unsigned0);
2752 arg1 = c_common_get_narrower (op1, &unsigned1);
ab2c1de8 2753
2561cea2 2754 /* UNS is 1 if the operation to be done is an unsigned one. */
2755 uns = TYPE_UNSIGNED (result_type);
2756
2757 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2758 but it *requires* conversion to FINAL_TYPE. */
48e1416a 2759
2561cea2 2760 if ((TYPE_PRECISION (TREE_TYPE (op0))
2761 == TYPE_PRECISION (TREE_TYPE (arg0)))
2762 && TREE_TYPE (op0) != result_type)
2763 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2764 if ((TYPE_PRECISION (TREE_TYPE (op1))
2765 == TYPE_PRECISION (TREE_TYPE (arg1)))
2766 && TREE_TYPE (op1) != result_type)
2767 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
48e1416a 2768
2561cea2 2769 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
48e1416a 2770
2561cea2 2771 /* For bitwise operations, signedness of nominal type
2772 does not matter. Consider only how operands were extended. */
2773 if (bitwise)
2774 uns = unsigned0;
48e1416a 2775
2561cea2 2776 /* Note that in all three cases below we refrain from optimizing
2777 an unsigned operation on sign-extended args.
2778 That would not be valid. */
48e1416a 2779
2561cea2 2780 /* Both args variable: if both extended in same way
2781 from same width, do it in that width.
2782 Do it unsigned if args were zero-extended. */
2783 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2784 < TYPE_PRECISION (result_type))
2785 && (TYPE_PRECISION (TREE_TYPE (arg1))
2786 == TYPE_PRECISION (TREE_TYPE (arg0)))
2787 && unsigned0 == unsigned1
2788 && (unsigned0 || !uns))
2789 return c_common_signed_or_unsigned_type
2790 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2791
2792 else if (TREE_CODE (arg0) == INTEGER_CST
2793 && (unsigned1 || !uns)
2794 && (TYPE_PRECISION (TREE_TYPE (arg1))
2795 < TYPE_PRECISION (result_type))
2796 && (type
2797 = c_common_signed_or_unsigned_type (unsigned1,
2798 TREE_TYPE (arg1)))
2799 && !POINTER_TYPE_P (type)
2800 && int_fits_type_p (arg0, type))
2801 return type;
2802
2803 else if (TREE_CODE (arg1) == INTEGER_CST
2804 && (unsigned0 || !uns)
2805 && (TYPE_PRECISION (TREE_TYPE (arg0))
2806 < TYPE_PRECISION (result_type))
2807 && (type
2808 = c_common_signed_or_unsigned_type (unsigned0,
2809 TREE_TYPE (arg0)))
2810 && !POINTER_TYPE_P (type)
2811 && int_fits_type_p (arg1, type))
2812 return type;
2813
2814 return result_type;
2815}
2816
e53013a8 2817/* Returns true iff any integer value of type FROM_TYPE can be represented as
2818 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2819
2820static bool
2821int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2822{
2823 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2824 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2825 REAL_VALUE_TYPE real_low_bound =
2826 real_value_from_int_cst (0, type_low_bound);
2827 REAL_VALUE_TYPE real_high_bound =
2828 real_value_from_int_cst (0, type_high_bound);
2829
2830 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2831 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2832}
2833
2834/* Checks if expression EXPR of complex/real/integer type cannot be converted
2835 to the complex/real/integer type TYPE. Function returns non-zero when:
22a75734 2836 * EXPR is a constant which cannot be exactly converted to TYPE.
2837 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
e53013a8 2838 for EXPR type and TYPE being both integers or both real, or both
2839 complex.
2840 * EXPR is not a constant of complex type and TYPE is a real or
2841 an integer.
22a75734 2842 * EXPR is not a constant of real type and TYPE is an integer.
2843 * EXPR is not a constant of integer type which cannot be
2844 exactly converted to real type.
e53013a8 2845
7dfa155b 2846 Function allows conversions between types of different signedness and
ca9d7d74 2847 can return SAFE_CONVERSION (zero) in that case. Function can produce
e53013a8 2848 signedness warnings if PRODUCE_WARNS is true.
2849
2850 Function allows conversions from complex constants to non-complex types,
2851 provided that imaginary part is zero and real part can be safely converted
2852 to TYPE. */
22a75734 2853
ca9d7d74 2854enum conversion_safety
22a75734 2855unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
d31d55f0 2856{
ca9d7d74 2857 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
27259707 2858 tree expr_type = TREE_TYPE (expr);
22a75734 2859 loc = expansion_point_location_if_in_system_header (loc);
d31d55f0 2860
7dfa155b 2861 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
5b16c152 2862 {
e53013a8 2863 /* If type is complex, we are interested in compatibility with
2864 underlying type. */
2865 if (TREE_CODE (type) == COMPLEX_TYPE)
2866 type = TREE_TYPE (type);
2867
d31d55f0 2868 /* Warn for real constant that is not an exact integer converted
7dfa155b 2869 to integer type. */
27259707 2870 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2871 && TREE_CODE (type) == INTEGER_TYPE)
2872 {
2873 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
ca9d7d74 2874 give_warning = UNSAFE_REAL;
7dfa155b 2875 }
da1fb07b 2876 /* Warn for an integer constant that does not fit into integer type. */
27259707 2877 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2878 && TREE_CODE (type) == INTEGER_TYPE
2879 && !int_fits_type_p (expr, type))
2880 {
2881 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
27259707 2882 && tree_int_cst_sgn (expr) < 0)
7dfa155b 2883 {
2884 if (produce_warns)
2885 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2886 " implicitly converted to unsigned type");
2887 }
2888 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2889 {
2890 if (produce_warns)
2891 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2892 " constant value to negative integer");
2893 }
7ee0d227 2894 else
ca9d7d74 2895 give_warning = UNSAFE_OTHER;
7dfa155b 2896 }
d31d55f0 2897 else if (TREE_CODE (type) == REAL_TYPE)
7dfa155b 2898 {
2899 /* Warn for an integer constant that does not fit into real type. */
2900 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2901 {
2902 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2903 if (!exact_real_truncate (TYPE_MODE (type), &a))
ca9d7d74 2904 give_warning = UNSAFE_REAL;
7dfa155b 2905 }
2906 /* Warn for a real constant that does not fit into a smaller
2907 real type. */
2908 else if (TREE_CODE (expr_type) == REAL_TYPE
2909 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2910 {
2911 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2912 if (!exact_real_truncate (TYPE_MODE (type), &a))
ca9d7d74 2913 give_warning = UNSAFE_REAL;
7dfa155b 2914 }
2915 }
2916 }
e53013a8 2917
2918 else if (TREE_CODE (expr) == COMPLEX_CST)
2919 {
2920 tree imag_part = TREE_IMAGPART (expr);
2921 /* Conversion from complex constant with zero imaginary part,
2922 perform check for conversion of real part. */
2923 if ((TREE_CODE (imag_part) == REAL_CST
2924 && real_zerop (imag_part))
2925 || (TREE_CODE (imag_part) == INTEGER_CST
2926 && integer_zerop (imag_part)))
2927 /* Note: in this branch we use recursive call to unsafe_conversion_p
2928 with different type of EXPR, but it is still safe, because when EXPR
2929 is a constant, it's type is not used in text of generated warnings
2930 (otherwise they could sound misleading). */
2931 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2932 produce_warns);
2933 /* Conversion from complex constant with non-zero imaginary part. */
2934 else
2935 {
2936 /* Conversion to complex type.
2937 Perform checks for both real and imaginary parts. */
2938 if (TREE_CODE (type) == COMPLEX_TYPE)
2939 {
2940 /* Unfortunately, produce_warns must be false in two subsequent
2941 calls of unsafe_conversion_p, because otherwise we could
2942 produce strange "double" warnings, if both real and imaginary
2943 parts have conversion problems related to signedness.
2944
2945 For example:
2946 int32_t _Complex a = 0x80000000 + 0x80000000i;
2947
2948 Possible solution: add a separate function for checking
2949 constants and combine result of two calls appropriately. */
2950 enum conversion_safety re_safety =
2951 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2952 enum conversion_safety im_safety =
2953 unsafe_conversion_p (loc, type, imag_part, false);
2954
2955 /* Merge the results into appropriate single warning. */
2956
2957 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2958 if (re_safety == im_safety)
2959 give_warning = re_safety;
2960 else if (!re_safety && im_safety)
2961 give_warning = im_safety;
2962 else if (re_safety && !im_safety)
2963 give_warning = re_safety;
2964 else
2965 give_warning = UNSAFE_OTHER;
2966 }
2967 /* Warn about conversion from complex to real or integer type. */
2968 else
2969 give_warning = UNSAFE_IMAGINARY;
2970 }
2971 }
2972
2973 /* Checks for remaining case: EXPR is not constant. */
7dfa155b 2974 else
2975 {
d31d55f0 2976 /* Warn for real types converted to integer types. */
2561cea2 2977 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2978 && TREE_CODE (type) == INTEGER_TYPE)
ca9d7d74 2979 give_warning = UNSAFE_REAL;
d31d55f0 2980
2561cea2 2981 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2982 && TREE_CODE (type) == INTEGER_TYPE)
2983 {
69609004 2984 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
f9d856a4 2985 expr = get_unwidened (expr, 0);
2561cea2 2986 expr_type = TREE_TYPE (expr);
69609004 2987
2561cea2 2988 /* Don't warn for short y; short x = ((int)y & 0xff); */
48e1416a 2989 if (TREE_CODE (expr) == BIT_AND_EXPR
7dfa155b 2990 || TREE_CODE (expr) == BIT_IOR_EXPR
2561cea2 2991 || TREE_CODE (expr) == BIT_XOR_EXPR)
2992 {
27259707 2993 /* If both args were extended from a shortest type,
2994 use that type if that is safe. */
48e1416a 2995 expr_type = shorten_binary_op (expr_type,
2996 TREE_OPERAND (expr, 0),
2997 TREE_OPERAND (expr, 1),
2561cea2 2998 /* bitwise */1);
2999
2561cea2 3000 if (TREE_CODE (expr) == BIT_AND_EXPR)
3001 {
3002 tree op0 = TREE_OPERAND (expr, 0);
3003 tree op1 = TREE_OPERAND (expr, 1);
30de145b 3004 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3005 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3006
3007 /* If one of the operands is a non-negative constant
3008 that fits in the target type, then the type of the
3009 other operand does not matter. */
2561cea2 3010 if ((TREE_CODE (op0) == INTEGER_CST
3011 && int_fits_type_p (op0, c_common_signed_type (type))
3012 && int_fits_type_p (op0, c_common_unsigned_type (type)))
3013 || (TREE_CODE (op1) == INTEGER_CST
27259707 3014 && int_fits_type_p (op1, c_common_signed_type (type))
48e1416a 3015 && int_fits_type_p (op1,
27259707 3016 c_common_unsigned_type (type))))
ca9d7d74 3017 return SAFE_CONVERSION;
30de145b 3018 /* If constant is unsigned and fits in the target
3019 type, then the result will also fit. */
3020 else if ((TREE_CODE (op0) == INTEGER_CST
48e1416a 3021 && unsigned0
30de145b 3022 && int_fits_type_p (op0, type))
3023 || (TREE_CODE (op1) == INTEGER_CST
3024 && unsigned1
3025 && int_fits_type_p (op1, type)))
ca9d7d74 3026 return SAFE_CONVERSION;
2561cea2 3027 }
3028 }
7dfa155b 3029 /* Warn for integer types converted to smaller integer types. */
48e1416a 3030 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
ca9d7d74 3031 give_warning = UNSAFE_OTHER;
7ee0d227 3032
3033 /* When they are the same width but different signedness,
3034 then the value may change. */
7dfa155b 3035 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2561cea2 3036 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
7ee0d227 3037 /* Even when converted to a bigger type, if the type is
3038 unsigned but expr is signed, then negative values
3039 will be changed. */
7dfa155b 3040 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
3041 && produce_warns)
200dd99c 3042 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
3043 "may change the sign of the result",
3044 type, expr_type);
7dfa155b 3045 }
d31d55f0 3046
3047 /* Warn for integer types converted to real types if and only if
7dfa155b 3048 all the range of values of the integer type cannot be
3049 represented by the real type. */
2561cea2 3050 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 3051 && TREE_CODE (type) == REAL_TYPE)
3052 {
4c2cfa81 3053 /* Don't warn about char y = 0xff; float x = (int) y; */
3054 expr = get_unwidened (expr, 0);
3055 expr_type = TREE_TYPE (expr);
3056
e53013a8 3057 if (!int_safely_convertible_to_real_p (expr_type, type))
ca9d7d74 3058 give_warning = UNSAFE_OTHER;
7dfa155b 3059 }
d31d55f0 3060
3061 /* Warn for real types converted to smaller real types. */
2561cea2 3062 else if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 3063 && TREE_CODE (type) == REAL_TYPE
3064 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
ca9d7d74 3065 give_warning = UNSAFE_REAL;
e53013a8 3066
3067 /* Check conversion between two complex types. */
3068 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
3069 && TREE_CODE (type) == COMPLEX_TYPE)
3070 {
3071 /* Extract underlying types (i.e., type of real and imaginary
3072 parts) of expr_type and type. */
3073 tree from_type = TREE_TYPE (expr_type);
3074 tree to_type = TREE_TYPE (type);
3075
3076 /* Warn for real types converted to integer types. */
3077 if (TREE_CODE (from_type) == REAL_TYPE
3078 && TREE_CODE (to_type) == INTEGER_TYPE)
3079 give_warning = UNSAFE_REAL;
3080
3081 /* Warn for real types converted to smaller real types. */
3082 else if (TREE_CODE (from_type) == REAL_TYPE
3083 && TREE_CODE (to_type) == REAL_TYPE
3084 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
3085 give_warning = UNSAFE_REAL;
3086
3087 /* Check conversion for complex integer types. Here implementation
3088 is simpler than for real-domain integers because it does not
3089 involve sophisticated cases, such as bitmasks, casts, etc. */
3090 else if (TREE_CODE (from_type) == INTEGER_TYPE
3091 && TREE_CODE (to_type) == INTEGER_TYPE)
3092 {
3093 /* Warn for integer types converted to smaller integer types. */
3094 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
3095 give_warning = UNSAFE_OTHER;
3096
3097 /* Check for different signedness, see case for real-domain
3098 integers (above) for a more detailed comment. */
3099 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
3100 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
3101 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
3102 && produce_warns)
3103 warning_at (loc, OPT_Wsign_conversion,
3104 "conversion to %qT from %qT "
3105 "may change the sign of the result",
3106 type, expr_type);
3107 }
3108 else if (TREE_CODE (from_type) == INTEGER_TYPE
3109 && TREE_CODE (to_type) == REAL_TYPE
3110 && !int_safely_convertible_to_real_p (from_type, to_type))
3111 give_warning = UNSAFE_OTHER;
3112 }
3113
3114 /* Warn for complex types converted to real or integer types. */
3115 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
3116 && TREE_CODE (type) != COMPLEX_TYPE)
3117 give_warning = UNSAFE_IMAGINARY;
7dfa155b 3118 }
3119
3120 return give_warning;
3121}
3122
3123/* Warns if the conversion of EXPR to TYPE may alter a value.
3124 This is a helper function for warnings_for_convert_and_check. */
3125
3126static void
22a75734 3127conversion_warning (location_t loc, tree type, tree expr)
7dfa155b 3128{
7dfa155b 3129 tree expr_type = TREE_TYPE (expr);
ca9d7d74 3130 enum conversion_safety conversion_kind;
d31d55f0 3131
ca9d7d74 3132 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
7dfa155b 3133 return;
d31d55f0 3134
ec704957 3135 /* This may happen, because for LHS op= RHS we preevaluate
3136 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
3137 means we could no longer see the code of the EXPR. */
3138 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
3139 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
3140 if (TREE_CODE (expr) == SAVE_EXPR)
3141 expr = TREE_OPERAND (expr, 0);
3142
7dfa155b 3143 switch (TREE_CODE (expr))
3144 {
3145 case EQ_EXPR:
3146 case NE_EXPR:
3147 case LE_EXPR:
3148 case GE_EXPR:
3149 case LT_EXPR:
3150 case GT_EXPR:
3151 case TRUTH_ANDIF_EXPR:
3152 case TRUTH_ORIF_EXPR:
3153 case TRUTH_AND_EXPR:
3154 case TRUTH_OR_EXPR:
3155 case TRUTH_XOR_EXPR:
3156 case TRUTH_NOT_EXPR:
3157 /* Conversion from boolean to a signed:1 bit-field (which only
3158 can hold the values 0 and -1) doesn't lose information - but
3159 it does change the value. */
3160 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
3161 warning_at (loc, OPT_Wconversion,
3162 "conversion to %qT from boolean expression", type);
3163 return;
3164
3165 case REAL_CST:
3166 case INTEGER_CST:
e53013a8 3167 case COMPLEX_CST:
22a75734 3168 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
ca9d7d74 3169 if (conversion_kind == UNSAFE_REAL)
3170 warning_at (loc, OPT_Wfloat_conversion,
3171 "conversion to %qT alters %qT constant value",
3172 type, expr_type);
3173 else if (conversion_kind)
7dfa155b 3174 warning_at (loc, OPT_Wconversion,
3175 "conversion to %qT alters %qT constant value",
3176 type, expr_type);
3177 return;
3178
3179 case COND_EXPR:
3180 {
0e4e775a 3181 /* In case of COND_EXPR, we do not care about the type of
3182 COND_EXPR, only about the conversion of each operand. */
3183 tree op1 = TREE_OPERAND (expr, 1);
3184 tree op2 = TREE_OPERAND (expr, 2);
3185
22a75734 3186 conversion_warning (loc, type, op1);
3187 conversion_warning (loc, type, op2);
0e4e775a 3188 return;
7dfa155b 3189 }
3190
3191 default: /* 'expr' is not a constant. */
22a75734 3192 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
ca9d7d74 3193 if (conversion_kind == UNSAFE_REAL)
3194 warning_at (loc, OPT_Wfloat_conversion,
3195 "conversion to %qT from %qT may alter its value",
3196 type, expr_type);
e53013a8 3197 else if (conversion_kind == UNSAFE_IMAGINARY)
3198 warning_at (loc, OPT_Wconversion,
3199 "conversion to %qT from %qT discards imaginary component",
3200 type, expr_type);
ca9d7d74 3201 else if (conversion_kind)
7dfa155b 3202 warning_at (loc, OPT_Wconversion,
200dd99c 3203 "conversion to %qT from %qT may alter its value",
3204 type, expr_type);
d31d55f0 3205 }
3206}
3207
59dd8856 3208/* Produce warnings after a conversion. RESULT is the result of
3209 converting EXPR to TYPE. This is a helper function for
3210 convert_and_check and cp_convert_and_check. */
2a1736ed 3211
59dd8856 3212void
22a75734 3213warnings_for_convert_and_check (location_t loc, tree type, tree expr,
3214 tree result)
2a1736ed 3215{
22a75734 3216 loc = expansion_point_location_if_in_system_header (loc);
61f69bc9 3217
da1fb07b 3218 if (TREE_CODE (expr) == INTEGER_CST
3219 && (TREE_CODE (type) == INTEGER_TYPE
3220 || TREE_CODE (type) == ENUMERAL_TYPE)
3221 && !int_fits_type_p (expr, type))
3222 {
d31d55f0 3223 /* Do not diagnose overflow in a constant expression merely
3224 because a conversion overflowed. */
da1fb07b 3225 if (TREE_OVERFLOW (result))
eddad94a 3226 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
3227
da1fb07b 3228 if (TYPE_UNSIGNED (type))
d31d55f0 3229 {
da1fb07b 3230 /* This detects cases like converting -129 or 256 to
3231 unsigned char. */
3232 if (!int_fits_type_p (expr, c_common_signed_type (type)))
61f69bc9 3233 warning_at (loc, OPT_Woverflow,
3234 "large integer implicitly truncated to unsigned type");
7ee0d227 3235 else
22a75734 3236 conversion_warning (loc, type, expr);
da1fb07b 3237 }
48e1416a 3238 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
22a75734 3239 warning_at (loc, OPT_Woverflow,
e0913805 3240 "overflow in implicit constant conversion");
3241 /* No warning for converting 0x80000000 to int. */
3242 else if (pedantic
3243 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
3244 || TYPE_PRECISION (TREE_TYPE (expr))
3245 != TYPE_PRECISION (type)))
61f69bc9 3246 warning_at (loc, OPT_Woverflow,
3247 "overflow in implicit constant conversion");
e0913805 3248
7ee0d227 3249 else
22a75734 3250 conversion_warning (loc, type, expr);
2a1736ed 3251 }
9421ebb9 3252 else if ((TREE_CODE (result) == INTEGER_CST
3253 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
61f69bc9 3254 warning_at (loc, OPT_Woverflow,
3255 "overflow in implicit constant conversion");
7ee0d227 3256 else
22a75734 3257 conversion_warning (loc, type, expr);
59dd8856 3258}
3259
3260
3261/* Convert EXPR to TYPE, warning about conversion problems with constants.
3262 Invoke this function on every expression that is converted implicitly,
3263 i.e. because of language rules and not because of an explicit cast. */
3264
3265tree
22a75734 3266convert_and_check (location_t loc, tree type, tree expr)
59dd8856 3267{
3268 tree result;
c6418a4e 3269 tree expr_for_warning;
3270
3271 /* Convert from a value with possible excess precision rather than
3272 via the semantic type, but do not warn about values not fitting
3273 exactly in the semantic type. */
3274 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
3275 {
3276 tree orig_type = TREE_TYPE (expr);
3277 expr = TREE_OPERAND (expr, 0);
3278 expr_for_warning = convert (orig_type, expr);
3279 if (orig_type == type)
3280 return expr_for_warning;
3281 }
3282 else
3283 expr_for_warning = expr;
59dd8856 3284
3285 if (TREE_TYPE (expr) == type)
3286 return expr;
48e1416a 3287
59dd8856 3288 result = convert (type, expr);
3289
48d94ede 3290 if (c_inhibit_evaluation_warnings == 0
3291 && !TREE_OVERFLOW_P (expr)
3292 && result != error_mark_node)
22a75734 3293 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
59dd8856 3294
da1fb07b 3295 return result;
b2806639 3296}
3297\f
4e91a871 3298/* A node in a list that describes references to variables (EXPR), which are
3299 either read accesses if WRITER is zero, or write accesses, in which case
3300 WRITER is the parent of EXPR. */
3301struct tlist
3302{
3303 struct tlist *next;
3304 tree expr, writer;
3305};
3306
3307/* Used to implement a cache the results of a call to verify_tree. We only
3308 use this for SAVE_EXPRs. */
3309struct tlist_cache
3310{
3311 struct tlist_cache *next;
3312 struct tlist *cache_before_sp;
3313 struct tlist *cache_after_sp;
3314 tree expr;
481c6ce6 3315};
3316
4e91a871 3317/* Obstack to use when allocating tlist structures, and corresponding
3318 firstobj. */
3319static struct obstack tlist_obstack;
3320static char *tlist_firstobj = 0;
3321
3322/* Keep track of the identifiers we've warned about, so we can avoid duplicate
3323 warnings. */
3324static struct tlist *warned_ids;
3325/* SAVE_EXPRs need special treatment. We process them only once and then
3326 cache the results. */
3327static struct tlist_cache *save_expr_cache;
3328
1cae46be 3329static void add_tlist (struct tlist **, struct tlist *, tree, int);
3330static void merge_tlist (struct tlist **, struct tlist *, int);
3331static void verify_tree (tree, struct tlist **, struct tlist **, tree);
3332static int warning_candidate_p (tree);
79973b57 3333static bool candidate_equal_p (const_tree, const_tree);
1cae46be 3334static void warn_for_collisions (struct tlist *);
3335static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
3336static struct tlist *new_tlist (struct tlist *, tree, tree);
481c6ce6 3337
4e91a871 3338/* Create a new struct tlist and fill in its fields. */
3339static struct tlist *
1cae46be 3340new_tlist (struct tlist *next, tree t, tree writer)
4e91a871 3341{
3342 struct tlist *l;
9318f22c 3343 l = XOBNEW (&tlist_obstack, struct tlist);
4e91a871 3344 l->next = next;
3345 l->expr = t;
3346 l->writer = writer;
3347 return l;
3348}
3349
3350/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3351 is nonnull, we ignore any node we find which has a writer equal to it. */
3352
3353static void
1cae46be 3354add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
4e91a871 3355{
3356 while (add)
3357 {
3358 struct tlist *next = add->next;
84166705 3359 if (!copy)
4e91a871 3360 add->next = *to;
79973b57 3361 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
4e91a871 3362 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3363 add = next;
3364 }
3365}
3366
3367/* Merge the nodes of ADD into TO. This merging process is done so that for
3368 each variable that already exists in TO, no new node is added; however if
3369 there is a write access recorded in ADD, and an occurrence on TO is only
3370 a read access, then the occurrence in TO will be modified to record the
3371 write. */
481c6ce6 3372
3373static void
1cae46be 3374merge_tlist (struct tlist **to, struct tlist *add, int copy)
4e91a871 3375{
3376 struct tlist **end = to;
3377
3378 while (*end)
3379 end = &(*end)->next;
3380
3381 while (add)
3382 {
3383 int found = 0;
3384 struct tlist *tmp2;
3385 struct tlist *next = add->next;
3386
3387 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
79973b57 3388 if (candidate_equal_p (tmp2->expr, add->expr))
4e91a871 3389 {
3390 found = 1;
84166705 3391 if (!tmp2->writer)
4e91a871 3392 tmp2->writer = add->writer;
3393 }
84166705 3394 if (!found)
4e91a871 3395 {
312243bb 3396 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
4e91a871 3397 end = &(*end)->next;
3398 *end = 0;
3399 }
3400 add = next;
3401 }
3402}
3403
3404/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3405 references in list LIST conflict with it, excluding reads if ONLY writers
3406 is nonzero. */
3407
3408static void
1cae46be 3409warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3410 int only_writes)
4e91a871 3411{
3412 struct tlist *tmp;
3413
3414 /* Avoid duplicate warnings. */
3415 for (tmp = warned_ids; tmp; tmp = tmp->next)
79973b57 3416 if (candidate_equal_p (tmp->expr, written))
4e91a871 3417 return;
3418
3419 while (list)
3420 {
79973b57 3421 if (candidate_equal_p (list->expr, written)
3422 && !candidate_equal_p (list->writer, writer)
3423 && (!only_writes || list->writer))
4e91a871 3424 {
3425 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3df42822 3426 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
6513b50d 3427 OPT_Wsequence_point, "operation on %qE may be undefined",
3428 list->expr);
4e91a871 3429 }
3430 list = list->next;
3431 }
3432}
3433
3434/* Given a list LIST of references to variables, find whether any of these
3435 can cause conflicts due to missing sequence points. */
3436
3437static void
1cae46be 3438warn_for_collisions (struct tlist *list)
4e91a871 3439{
3440 struct tlist *tmp;
1cae46be 3441
4e91a871 3442 for (tmp = list; tmp; tmp = tmp->next)
3443 {
3444 if (tmp->writer)
3445 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3446 }
3447}
3448
734c98be 3449/* Return nonzero if X is a tree that can be verified by the sequence point
4e91a871 3450 warnings. */
3451static int
1cae46be 3452warning_candidate_p (tree x)
481c6ce6 3453{
6ef8d12f 3454 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3455 return 0;
3456
027fc6ef 3457 if (TREE_CODE (x) == BLOCK)
3458 return 0;
3459
6ef8d12f 3460 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
79973b57 3461 (lvalue_p) crash on TRY/CATCH. */
6ef8d12f 3462 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3463 return 0;
3464
3465 if (!lvalue_p (x))
3466 return 0;
3467
3468 /* No point to track non-const calls, they will never satisfy
3469 operand_equal_p. */
3470 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3471 return 0;
3472
3473 if (TREE_CODE (x) == STRING_CST)
3474 return 0;
3475
3476 return 1;
79973b57 3477}
3478
3479/* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3480static bool
3481candidate_equal_p (const_tree x, const_tree y)
3482{
3483 return (x == y) || (x && y && operand_equal_p (x, y, 0));
4e91a871 3484}
481c6ce6 3485
4e91a871 3486/* Walk the tree X, and record accesses to variables. If X is written by the
3487 parent tree, WRITER is the parent.
3488 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3489 expression or its only operand forces a sequence point, then everything up
3490 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3491 in PNO_SP.
3492 Once we return, we will have emitted warnings if any subexpression before
3493 such a sequence point could be undefined. On a higher level, however, the
3494 sequence point may not be relevant, and we'll merge the two lists.
3495
3496 Example: (b++, a) + b;
3497 The call that processes the COMPOUND_EXPR will store the increment of B
3498 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3499 processes the PLUS_EXPR will need to merge the two lists so that
3500 eventually, all accesses end up on the same list (and we'll warn about the
3501 unordered subexpressions b++ and b.
3502
3503 A note on merging. If we modify the former example so that our expression
3504 becomes
3505 (b++, b) + a
3506 care must be taken not simply to add all three expressions into the final
3507 PNO_SP list. The function merge_tlist takes care of that by merging the
3508 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3509 way, so that no more than one access to B is recorded. */
481c6ce6 3510
4e91a871 3511static void
1cae46be 3512verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3513 tree writer)
4e91a871 3514{
3515 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3516 enum tree_code code;
ce45a448 3517 enum tree_code_class cl;
481c6ce6 3518
e5b75768 3519 /* X may be NULL if it is the operand of an empty statement expression
3520 ({ }). */
3521 if (x == NULL)
3522 return;
3523
4e91a871 3524 restart:
3525 code = TREE_CODE (x);
e916c70c 3526 cl = TREE_CODE_CLASS (code);
481c6ce6 3527
4e91a871 3528 if (warning_candidate_p (x))
79973b57 3529 *pno_sp = new_tlist (*pno_sp, x, writer);
4e91a871 3530
3531 switch (code)
3532 {
67b28e3e 3533 case CONSTRUCTOR:
8e71dad2 3534 case SIZEOF_EXPR:
67b28e3e 3535 return;
3536
4e91a871 3537 case COMPOUND_EXPR:
3538 case TRUTH_ANDIF_EXPR:
3539 case TRUTH_ORIF_EXPR:
3540 tmp_before = tmp_nosp = tmp_list3 = 0;
3541 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3542 warn_for_collisions (tmp_nosp);
3543 merge_tlist (pbefore_sp, tmp_before, 0);
3544 merge_tlist (pbefore_sp, tmp_nosp, 0);
3545 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3546 merge_tlist (pbefore_sp, tmp_list3, 0);
3547 return;
3548
3549 case COND_EXPR:
3550 tmp_before = tmp_list2 = 0;
3551 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3552 warn_for_collisions (tmp_list2);
3553 merge_tlist (pbefore_sp, tmp_before, 0);
312243bb 3554 merge_tlist (pbefore_sp, tmp_list2, 0);
4e91a871 3555
3556 tmp_list3 = tmp_nosp = 0;
3557 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3558 warn_for_collisions (tmp_nosp);
3559 merge_tlist (pbefore_sp, tmp_list3, 0);
3560
3561 tmp_list3 = tmp_list2 = 0;
3562 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3563 warn_for_collisions (tmp_list2);
3564 merge_tlist (pbefore_sp, tmp_list3, 0);
3565 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3566 two first, to avoid warning for (a ? b++ : b++). */
3567 merge_tlist (&tmp_nosp, tmp_list2, 0);
3568 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3569 return;
3570
481c6ce6 3571 case PREDECREMENT_EXPR:
3572 case PREINCREMENT_EXPR:
3573 case POSTDECREMENT_EXPR:
3574 case POSTINCREMENT_EXPR:
4e91a871 3575 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3576 return;
3577
3578 case MODIFY_EXPR:
3579 tmp_before = tmp_nosp = tmp_list3 = 0;
3580 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3581 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3582 /* Expressions inside the LHS are not ordered wrt. the sequence points
3583 in the RHS. Example:
3584 *a = (a++, 2)
3585 Despite the fact that the modification of "a" is in the before_sp
3586 list (tmp_before), it conflicts with the use of "a" in the LHS.
3587 We can handle this by adding the contents of tmp_list3
3588 to those of tmp_before, and redoing the collision warnings for that
3589 list. */
3590 add_tlist (&tmp_before, tmp_list3, x, 1);
3591 warn_for_collisions (tmp_before);
3592 /* Exclude the LHS itself here; we first have to merge it into the
3593 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3594 didn't exclude the LHS, we'd get it twice, once as a read and once
3595 as a write. */
3596 add_tlist (pno_sp, tmp_list3, x, 0);
3597 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3598
3599 merge_tlist (pbefore_sp, tmp_before, 0);
3600 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3601 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3602 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3603 return;
481c6ce6 3604
3605 case CALL_EXPR:
4e91a871 3606 /* We need to warn about conflicts among arguments and conflicts between
3607 args and the function address. Side effects of the function address,
3608 however, are not ordered by the sequence point of the call. */
c2f47e15 3609 {
3610 call_expr_arg_iterator iter;
3611 tree arg;
48e1416a 3612 tmp_before = tmp_nosp = 0;
c2f47e15 3613 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3614 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3615 {
3616 tmp_list2 = tmp_list3 = 0;
3617 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3618 merge_tlist (&tmp_list3, tmp_list2, 0);
3619 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3620 }
3621 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3622 warn_for_collisions (tmp_before);
3623 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3624 return;
3625 }
481c6ce6 3626
3627 case TREE_LIST:
3628 /* Scan all the list, e.g. indices of multi dimensional array. */
3629 while (x)
3630 {
4e91a871 3631 tmp_before = tmp_nosp = 0;
3632 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3633 merge_tlist (&tmp_nosp, tmp_before, 0);
3634 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
481c6ce6 3635 x = TREE_CHAIN (x);
3636 }
4e91a871 3637 return;
481c6ce6 3638
4e91a871 3639 case SAVE_EXPR:
3640 {
3641 struct tlist_cache *t;
3642 for (t = save_expr_cache; t; t = t->next)
79973b57 3643 if (candidate_equal_p (t->expr, x))
4e91a871 3644 break;
481c6ce6 3645
84166705 3646 if (!t)
481c6ce6 3647 {
9318f22c 3648 t = XOBNEW (&tlist_obstack, struct tlist_cache);
4e91a871 3649 t->next = save_expr_cache;
3650 t->expr = x;
3651 save_expr_cache = t;
3652
3653 tmp_before = tmp_nosp = 0;
3654 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3655 warn_for_collisions (tmp_nosp);
3656
3657 tmp_list3 = 0;
312243bb 3658 merge_tlist (&tmp_list3, tmp_nosp, 0);
4e91a871 3659 t->cache_before_sp = tmp_before;
3660 t->cache_after_sp = tmp_list3;
481c6ce6 3661 }
4e91a871 3662 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3663 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3664 return;
3665 }
481c6ce6 3666
012916cb 3667 case ADDR_EXPR:
3668 x = TREE_OPERAND (x, 0);
3669 if (DECL_P (x))
3670 return;
3671 writer = 0;
3672 goto restart;
3673
ce45a448 3674 default:
3675 /* For other expressions, simply recurse on their operands.
a0c938f0 3676 Manual tail recursion for unary expressions.
ce45a448 3677 Other non-expressions need not be processed. */
3678 if (cl == tcc_unary)
3679 {
ce45a448 3680 x = TREE_OPERAND (x, 0);
3681 writer = 0;
3682 goto restart;
3683 }
3684 else if (IS_EXPR_CODE_CLASS (cl))
3685 {
3686 int lp;
c2f47e15 3687 int max = TREE_OPERAND_LENGTH (x);
ce45a448 3688 for (lp = 0; lp < max; lp++)
3689 {
3690 tmp_before = tmp_nosp = 0;
3691 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3692 merge_tlist (&tmp_nosp, tmp_before, 0);
3693 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3694 }
3695 }
3696 return;
481c6ce6 3697 }
481c6ce6 3698}
3699
974e2c0c 3700/* Try to warn for undefined behavior in EXPR due to missing sequence
481c6ce6 3701 points. */
3702
4b987fac 3703DEBUG_FUNCTION void
1cae46be 3704verify_sequence_points (tree expr)
481c6ce6 3705{
4e91a871 3706 struct tlist *before_sp = 0, *after_sp = 0;
481c6ce6 3707
4e91a871 3708 warned_ids = 0;
3709 save_expr_cache = 0;
3710 if (tlist_firstobj == 0)
481c6ce6 3711 {
4e91a871 3712 gcc_obstack_init (&tlist_obstack);
4fd61bc6 3713 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
481c6ce6 3714 }
3715
4e91a871 3716 verify_tree (expr, &before_sp, &after_sp, 0);
3717 warn_for_collisions (after_sp);
3718 obstack_free (&tlist_obstack, tlist_firstobj);
481c6ce6 3719}
b0fc3e72 3720\f
3721/* Validate the expression after `case' and apply default promotions. */
3722
2ca392fd 3723static tree
2d2f6a15 3724check_case_value (location_t loc, tree value)
b0fc3e72 3725{
3726 if (value == NULL_TREE)
3727 return value;
3728
b96dc121 3729 if (TREE_CODE (value) == INTEGER_CST)
3730 /* Promote char or short to int. */
3731 value = perform_integral_promotions (value);
3732 else if (value != error_mark_node)
b0fc3e72 3733 {
2d2f6a15 3734 error_at (loc, "case label does not reduce to an integer constant");
b0fc3e72 3735 value = error_mark_node;
3736 }
b0fc3e72 3737
6433f1c2 3738 constant_expression_warning (value);
3739
b0fc3e72 3740 return value;
3741}
3742\f
2ca392fd 3743/* See if the case values LOW and HIGH are in the range of the original
5c9dae64 3744 type (i.e. before the default conversion to int) of the switch testing
2ca392fd 3745 expression.
3746 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
91275768 3747 the type before promoting it. CASE_LOW_P is a pointer to the lower
2ca392fd 3748 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3749 if the case is not a case range.
3750 The caller has to make sure that we are not called with NULL for
be23b16f 3751 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
3752 was a case value that doesn't fit into the range of the ORIG_TYPE.
442e3cb9 3753 Returns true if the case label is in range of ORIG_TYPE (saturated or
2ca392fd 3754 untouched) or false if the label is out of range. */
3755
3756static bool
f61a9bc2 3757check_case_bounds (location_t loc, tree type, tree orig_type,
be23b16f 3758 tree *case_low_p, tree *case_high_p,
3759 bool *outside_range_p)
2ca392fd 3760{
3761 tree min_value, max_value;
3762 tree case_low = *case_low_p;
3763 tree case_high = case_high_p ? *case_high_p : case_low;
3764
3765 /* If there was a problem with the original type, do nothing. */
3766 if (orig_type == error_mark_node)
3767 return true;
3768
3769 min_value = TYPE_MIN_VALUE (orig_type);
3770 max_value = TYPE_MAX_VALUE (orig_type);
3771
73f67931 3772 /* We'll really need integer constants here. */
3773 case_low = fold (case_low);
3774 case_high = fold (case_high);
3775
2ca392fd 3776 /* Case label is less than minimum for type. */
3777 if (tree_int_cst_compare (case_low, min_value) < 0
3778 && tree_int_cst_compare (case_high, min_value) < 0)
3779 {
f61a9bc2 3780 warning_at (loc, 0, "case label value is less than minimum value "
3781 "for type");
be23b16f 3782 *outside_range_p = true;
2ca392fd 3783 return false;
3784 }
b27ac6b5 3785
2ca392fd 3786 /* Case value is greater than maximum for type. */
3787 if (tree_int_cst_compare (case_low, max_value) > 0
3788 && tree_int_cst_compare (case_high, max_value) > 0)
3789 {
f61a9bc2 3790 warning_at (loc, 0, "case label value exceeds maximum value for type");
be23b16f 3791 *outside_range_p = true;
2ca392fd 3792 return false;
3793 }
3794
3795 /* Saturate lower case label value to minimum. */
3796 if (tree_int_cst_compare (case_high, min_value) >= 0
3797 && tree_int_cst_compare (case_low, min_value) < 0)
3798 {
f61a9bc2 3799 warning_at (loc, 0, "lower value in case label range"
3800 " less than minimum value for type");
be23b16f 3801 *outside_range_p = true;
2ca392fd 3802 case_low = min_value;
3803 }
b27ac6b5 3804
2ca392fd 3805 /* Saturate upper case label value to maximum. */
3806 if (tree_int_cst_compare (case_low, max_value) <= 0
3807 && tree_int_cst_compare (case_high, max_value) > 0)
3808 {
f61a9bc2 3809 warning_at (loc, 0, "upper value in case label range"
3810 " exceeds maximum value for type");
be23b16f 3811 *outside_range_p = true;
2ca392fd 3812 case_high = max_value;
3813 }
3814
3815 if (*case_low_p != case_low)
3816 *case_low_p = convert (type, case_low);
3817 if (case_high_p && *case_high_p != case_high)
3818 *case_high_p = convert (type, case_high);
3819
3820 return true;
3821}
3822\f
b0fc3e72 3823/* Return an integer type with BITS bits of precision,
3824 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3825
3826tree
1cae46be 3827c_common_type_for_size (unsigned int bits, int unsignedp)
b0fc3e72 3828{
9f75f026 3829 int i;
3830
46375237 3831 if (bits == TYPE_PRECISION (integer_type_node))
3832 return unsignedp ? unsigned_type_node : integer_type_node;
3833
bacde65a 3834 if (bits == TYPE_PRECISION (signed_char_type_node))
b0fc3e72 3835 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3836
bacde65a 3837 if (bits == TYPE_PRECISION (short_integer_type_node))
b0fc3e72 3838 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3839
bacde65a 3840 if (bits == TYPE_PRECISION (long_integer_type_node))
b0fc3e72 3841 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3842
bacde65a 3843 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b0fc3e72 3844 return (unsignedp ? long_long_unsigned_type_node
3845 : long_long_integer_type_node);
3846
9f75f026 3847 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3848 if (int_n_enabled_p[i]
3849 && bits == int_n_data[i].bitsize)
3850 return (unsignedp ? int_n_trees[i].unsigned_type
3851 : int_n_trees[i].signed_type);
6388cfe2 3852
f57fa2ea 3853 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3854 return (unsignedp ? widest_unsigned_literal_type_node
3855 : widest_integer_literal_type_node);
3856
bacde65a 3857 if (bits <= TYPE_PRECISION (intQI_type_node))
3858 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3859
3860 if (bits <= TYPE_PRECISION (intHI_type_node))
3861 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3862
3863 if (bits <= TYPE_PRECISION (intSI_type_node))
3864 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3865
3866 if (bits <= TYPE_PRECISION (intDI_type_node))
3867 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3868
b0fc3e72 3869 return 0;
3870}
3871
9421ebb9 3872/* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3873 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3874 and saturating if SATP is nonzero, otherwise not saturating. */
3875
3876tree
3877c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3878 int unsignedp, int satp)
3879{
3754d046 3880 machine_mode mode;
9421ebb9 3881 if (ibit == 0)
3882 mode = unsignedp ? UQQmode : QQmode;
3883 else
3884 mode = unsignedp ? UHAmode : HAmode;
3885
3886 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3887 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3888 break;
3889
3890 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3891 {
3892 sorry ("GCC cannot support operators with integer types and "
3893 "fixed-point types that have too many integral and "
3894 "fractional bits together");
3895 return 0;
3896 }
3897
3898 return c_common_type_for_mode (mode, satp);
3899}
3900
5b247e9f 3901/* Used for communication between c_common_type_for_mode and
3902 c_register_builtin_type. */
c1917557 3903tree registered_builtin_types;
5b247e9f 3904
b0fc3e72 3905/* Return a data type that has machine mode MODE.
3906 If the mode is an integer,
9421ebb9 3907 then UNSIGNEDP selects between signed and unsigned types.
3908 If the mode is a fixed-point mode,
3909 then UNSIGNEDP selects between saturating and nonsaturating types. */
b0fc3e72 3910
3911tree
3754d046 3912c_common_type_for_mode (machine_mode mode, int unsignedp)
b0fc3e72 3913{
5b247e9f 3914 tree t;
9f75f026 3915 int i;
5b247e9f 3916
46375237 3917 if (mode == TYPE_MODE (integer_type_node))
3918 return unsignedp ? unsigned_type_node : integer_type_node;
3919
b0fc3e72 3920 if (mode == TYPE_MODE (signed_char_type_node))
3921 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3922
3923 if (mode == TYPE_MODE (short_integer_type_node))
3924 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3925
b0fc3e72 3926 if (mode == TYPE_MODE (long_integer_type_node))
3927 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3928
3929 if (mode == TYPE_MODE (long_long_integer_type_node))
3930 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3931
9f75f026 3932 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3933 if (int_n_enabled_p[i]
3934 && mode == int_n_data[i].m)
3935 return (unsignedp ? int_n_trees[i].unsigned_type
3936 : int_n_trees[i].signed_type);
6388cfe2 3937
f57fa2ea 3938 if (mode == TYPE_MODE (widest_integer_literal_type_node))
44e9fa65 3939 return unsignedp ? widest_unsigned_literal_type_node
4ee9c684 3940 : widest_integer_literal_type_node;
f57fa2ea 3941
88ae7f04 3942 if (mode == QImode)
bacde65a 3943 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3944
88ae7f04 3945 if (mode == HImode)
bacde65a 3946 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3947
88ae7f04 3948 if (mode == SImode)
bacde65a 3949 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3950
88ae7f04 3951 if (mode == DImode)
bacde65a 3952 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
ab2c1de8 3953
cc1cc1c7 3954#if HOST_BITS_PER_WIDE_INT >= 64
6274009c 3955 if (mode == TYPE_MODE (intTI_type_node))
3956 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
cc1cc1c7 3957#endif
6274009c 3958
b0fc3e72 3959 if (mode == TYPE_MODE (float_type_node))
3960 return float_type_node;
3961
3962 if (mode == TYPE_MODE (double_type_node))
3963 return double_type_node;
3964
3965 if (mode == TYPE_MODE (long_double_type_node))
3966 return long_double_type_node;
3967
545c2bde 3968 if (mode == TYPE_MODE (void_type_node))
3969 return void_type_node;
b27ac6b5 3970
b0fc3e72 3971 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
61b9b73c 3972 return (unsignedp
3973 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3974 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3975
3976 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
61b9b73c 3977 return (unsignedp
3978 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3979 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3980
0dfc45b5 3981 if (COMPLEX_MODE_P (mode))
3982 {
3754d046 3983 machine_mode inner_mode;
0dfc45b5 3984 tree inner_type;
3985
3986 if (mode == TYPE_MODE (complex_float_type_node))
3987 return complex_float_type_node;
3988 if (mode == TYPE_MODE (complex_double_type_node))
3989 return complex_double_type_node;
3990 if (mode == TYPE_MODE (complex_long_double_type_node))
3991 return complex_long_double_type_node;
3992
3993 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3994 return complex_integer_type_node;
3995
3996 inner_mode = GET_MODE_INNER (mode);
3997 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3998 if (inner_type != NULL_TREE)
3999 return build_complex_type (inner_type);
4000 }
4001 else if (VECTOR_MODE_P (mode))
4917c376 4002 {
3754d046 4003 machine_mode inner_mode = GET_MODE_INNER (mode);
4917c376 4004 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
4005 if (inner_type != NULL_TREE)
4006 return build_vector_type_for_mode (inner_type, mode);
88ae7f04 4007 }
e2ea7e3a 4008
c4503c0a 4009 if (mode == TYPE_MODE (dfloat32_type_node))
4010 return dfloat32_type_node;
4011 if (mode == TYPE_MODE (dfloat64_type_node))
4012 return dfloat64_type_node;
4013 if (mode == TYPE_MODE (dfloat128_type_node))
4014 return dfloat128_type_node;
4015
9421ebb9 4016 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
4017 {
4018 if (mode == TYPE_MODE (short_fract_type_node))
4019 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
4020 if (mode == TYPE_MODE (fract_type_node))
4021 return unsignedp ? sat_fract_type_node : fract_type_node;
4022 if (mode == TYPE_MODE (long_fract_type_node))
4023 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
4024 if (mode == TYPE_MODE (long_long_fract_type_node))
4025 return unsignedp ? sat_long_long_fract_type_node
4026 : long_long_fract_type_node;
4027
4028 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
4029 return unsignedp ? sat_unsigned_short_fract_type_node
4030 : unsigned_short_fract_type_node;
4031 if (mode == TYPE_MODE (unsigned_fract_type_node))
4032 return unsignedp ? sat_unsigned_fract_type_node
4033 : unsigned_fract_type_node;
4034 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
4035 return unsignedp ? sat_unsigned_long_fract_type_node
4036 : unsigned_long_fract_type_node;
4037 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
4038 return unsignedp ? sat_unsigned_long_long_fract_type_node
4039 : unsigned_long_long_fract_type_node;
4040
4041 if (mode == TYPE_MODE (short_accum_type_node))
4042 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
4043 if (mode == TYPE_MODE (accum_type_node))
4044 return unsignedp ? sat_accum_type_node : accum_type_node;
4045 if (mode == TYPE_MODE (long_accum_type_node))
4046 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
4047 if (mode == TYPE_MODE (long_long_accum_type_node))
4048 return unsignedp ? sat_long_long_accum_type_node
4049 : long_long_accum_type_node;
4050
4051 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
4052 return unsignedp ? sat_unsigned_short_accum_type_node
4053 : unsigned_short_accum_type_node;
4054 if (mode == TYPE_MODE (unsigned_accum_type_node))
4055 return unsignedp ? sat_unsigned_accum_type_node
4056 : unsigned_accum_type_node;
4057 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
4058 return unsignedp ? sat_unsigned_long_accum_type_node
4059 : unsigned_long_accum_type_node;
4060 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
4061 return unsignedp ? sat_unsigned_long_long_accum_type_node
4062 : unsigned_long_long_accum_type_node;
4063
4064 if (mode == QQmode)
4065 return unsignedp ? sat_qq_type_node : qq_type_node;
4066 if (mode == HQmode)
4067 return unsignedp ? sat_hq_type_node : hq_type_node;
4068 if (mode == SQmode)
4069 return unsignedp ? sat_sq_type_node : sq_type_node;
4070 if (mode == DQmode)
4071 return unsignedp ? sat_dq_type_node : dq_type_node;
4072 if (mode == TQmode)
4073 return unsignedp ? sat_tq_type_node : tq_type_node;
4074
4075 if (mode == UQQmode)
4076 return unsignedp ? sat_uqq_type_node : uqq_type_node;
4077 if (mode == UHQmode)
4078 return unsignedp ? sat_uhq_type_node : uhq_type_node;
4079 if (mode == USQmode)
4080 return unsignedp ? sat_usq_type_node : usq_type_node;
4081 if (mode == UDQmode)
4082 return unsignedp ? sat_udq_type_node : udq_type_node;
4083 if (mode == UTQmode)
4084 return unsignedp ? sat_utq_type_node : utq_type_node;
4085
4086 if (mode == HAmode)
4087 return unsignedp ? sat_ha_type_node : ha_type_node;
4088 if (mode == SAmode)
4089 return unsignedp ? sat_sa_type_node : sa_type_node;
4090 if (mode == DAmode)
4091 return unsignedp ? sat_da_type_node : da_type_node;
4092 if (mode == TAmode)
4093 return unsignedp ? sat_ta_type_node : ta_type_node;
4094
4095 if (mode == UHAmode)
4096 return unsignedp ? sat_uha_type_node : uha_type_node;
4097 if (mode == USAmode)
4098 return unsignedp ? sat_usa_type_node : usa_type_node;
4099 if (mode == UDAmode)
4100 return unsignedp ? sat_uda_type_node : uda_type_node;
4101 if (mode == UTAmode)
4102 return unsignedp ? sat_uta_type_node : uta_type_node;
4103 }
4104
5b247e9f 4105 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
baec58e1 4106 if (TYPE_MODE (TREE_VALUE (t)) == mode
4107 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
5b247e9f 4108 return TREE_VALUE (t);
4109
b0fc3e72 4110 return 0;
4111}
20d39783 4112
11773141 4113tree
4114c_common_unsigned_type (tree type)
4115{
4116 return c_common_signed_or_unsigned_type (1, type);
4117}
4118
20d39783 4119/* Return a signed type the same as TYPE in other respects. */
4120
4121tree
1cae46be 4122c_common_signed_type (tree type)
20d39783 4123{
4070745f 4124 return c_common_signed_or_unsigned_type (0, type);
20d39783 4125}
4126
4127/* Return a type the same as TYPE except unsigned or
4128 signed according to UNSIGNEDP. */
4129
4130tree
1cae46be 4131c_common_signed_or_unsigned_type (int unsignedp, tree type)
20d39783 4132{
7a91101f 4133 tree type1;
9f75f026 4134 int i;
20d39783 4135
7a91101f 4136 /* This block of code emulates the behavior of the old
4137 c_common_unsigned_type. In particular, it returns
4138 long_unsigned_type_node if passed a long, even when a int would
4139 have the same size. This is necessary for warnings to work
4140 correctly in archs where sizeof(int) == sizeof(long) */
4141
4142 type1 = TYPE_MAIN_VARIANT (type);
4143 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
4144 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4145 if (type1 == integer_type_node || type1 == unsigned_type_node)
4146 return unsignedp ? unsigned_type_node : integer_type_node;
4147 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
4148 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4149 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
4150 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4151 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
4152 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
9f75f026 4153
4154 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4155 if (int_n_enabled_p[i]
4156 && (type1 == int_n_trees[i].unsigned_type
4157 || type1 == int_n_trees[i].signed_type))
4158 return (unsignedp ? int_n_trees[i].unsigned_type
4159 : int_n_trees[i].signed_type);
4160
7a91101f 4161 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
4162 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
4163#if HOST_BITS_PER_WIDE_INT >= 64
4164 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
4165 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4166#endif
4167 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
4168 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4169 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
4170 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4171 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
4172 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4173 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
4174 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4175
9f92e1a2 4176#define C_COMMON_FIXED_TYPES(NAME) \
4177 if (type1 == short_ ## NAME ## _type_node \
4178 || type1 == unsigned_short_ ## NAME ## _type_node) \
4179 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
4180 : short_ ## NAME ## _type_node; \
4181 if (type1 == NAME ## _type_node \
4182 || type1 == unsigned_ ## NAME ## _type_node) \
4183 return unsignedp ? unsigned_ ## NAME ## _type_node \
4184 : NAME ## _type_node; \
4185 if (type1 == long_ ## NAME ## _type_node \
4186 || type1 == unsigned_long_ ## NAME ## _type_node) \
4187 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
4188 : long_ ## NAME ## _type_node; \
4189 if (type1 == long_long_ ## NAME ## _type_node \
4190 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
4191 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
4192 : long_long_ ## NAME ## _type_node;
4193
4194#define C_COMMON_FIXED_MODE_TYPES(NAME) \
4195 if (type1 == NAME ## _type_node \
4196 || type1 == u ## NAME ## _type_node) \
4197 return unsignedp ? u ## NAME ## _type_node \
4198 : NAME ## _type_node;
4199
4200#define C_COMMON_FIXED_TYPES_SAT(NAME) \
4201 if (type1 == sat_ ## short_ ## NAME ## _type_node \
4202 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
4203 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
4204 : sat_ ## short_ ## NAME ## _type_node; \
4205 if (type1 == sat_ ## NAME ## _type_node \
4206 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
4207 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
4208 : sat_ ## NAME ## _type_node; \
4209 if (type1 == sat_ ## long_ ## NAME ## _type_node \
4210 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
4211 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
4212 : sat_ ## long_ ## NAME ## _type_node; \
4213 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
4214 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
4215 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
4216 : sat_ ## long_long_ ## NAME ## _type_node;
4217
4218#define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
4219 if (type1 == sat_ ## NAME ## _type_node \
4220 || type1 == sat_ ## u ## NAME ## _type_node) \
4221 return unsignedp ? sat_ ## u ## NAME ## _type_node \
4222 : sat_ ## NAME ## _type_node;
4223
4224 C_COMMON_FIXED_TYPES (fract);
4225 C_COMMON_FIXED_TYPES_SAT (fract);
4226 C_COMMON_FIXED_TYPES (accum);
4227 C_COMMON_FIXED_TYPES_SAT (accum);
4228
4229 C_COMMON_FIXED_MODE_TYPES (qq);
4230 C_COMMON_FIXED_MODE_TYPES (hq);
4231 C_COMMON_FIXED_MODE_TYPES (sq);
4232 C_COMMON_FIXED_MODE_TYPES (dq);
4233 C_COMMON_FIXED_MODE_TYPES (tq);
4234 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
4235 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
4236 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
4237 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
4238 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
4239 C_COMMON_FIXED_MODE_TYPES (ha);
4240 C_COMMON_FIXED_MODE_TYPES (sa);
4241 C_COMMON_FIXED_MODE_TYPES (da);
4242 C_COMMON_FIXED_MODE_TYPES (ta);
4243 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
4244 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
4245 C_COMMON_FIXED_MODE_TYPES_SAT (da);
4246 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
9421ebb9 4247
4f7f7efd 4248 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
4249 the precision; they have precision set to match their range, but
4250 may use a wider mode to match an ABI. If we change modes, we may
4251 wind up with bad conversions. For INTEGER_TYPEs in C, must check
4252 the precision as well, so as to yield correct results for
4253 bit-field types. C++ does not have these separate bit-field
4254 types, and producing a signed or unsigned variant of an
4255 ENUMERAL_TYPE may cause other problems as well. */
4256
ac265864 4257 if (!INTEGRAL_TYPE_P (type)
4258 || TYPE_UNSIGNED (type) == unsignedp)
4259 return type;
4260
4f7f7efd 4261#define TYPE_OK(node) \
4262 (TYPE_MODE (type) == TYPE_MODE (node) \
0c4abe5b 4263 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
4f7f7efd 4264 if (TYPE_OK (signed_char_type_node))
20d39783 4265 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4f7f7efd 4266 if (TYPE_OK (integer_type_node))
20d39783 4267 return unsignedp ? unsigned_type_node : integer_type_node;
4f7f7efd 4268 if (TYPE_OK (short_integer_type_node))
20d39783 4269 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4f7f7efd 4270 if (TYPE_OK (long_integer_type_node))
20d39783 4271 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4f7f7efd 4272 if (TYPE_OK (long_long_integer_type_node))
20d39783 4273 return (unsignedp ? long_long_unsigned_type_node
4274 : long_long_integer_type_node);
9f75f026 4275
4276 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4277 if (int_n_enabled_p[i]
4278 && TYPE_MODE (type) == int_n_data[i].m
4279 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
4280 return (unsignedp ? int_n_trees[i].unsigned_type
4281 : int_n_trees[i].signed_type);
4282
4f7f7efd 4283 if (TYPE_OK (widest_integer_literal_type_node))
20d39783 4284 return (unsignedp ? widest_unsigned_literal_type_node
4285 : widest_integer_literal_type_node);
ef11801e 4286
4287#if HOST_BITS_PER_WIDE_INT >= 64
4f7f7efd 4288 if (TYPE_OK (intTI_type_node))
ef11801e 4289 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4290#endif
4f7f7efd 4291 if (TYPE_OK (intDI_type_node))
ef11801e 4292 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4f7f7efd 4293 if (TYPE_OK (intSI_type_node))
ef11801e 4294 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4f7f7efd 4295 if (TYPE_OK (intHI_type_node))
ef11801e 4296 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4f7f7efd 4297 if (TYPE_OK (intQI_type_node))
ef11801e 4298 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4f7f7efd 4299#undef TYPE_OK
ef11801e 4300
0c4abe5b 4301 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
20d39783 4302}
b268e47e 4303
c0e47fd4 4304/* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
4305
4306tree
4307c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
4308{
9f75f026 4309 int i;
4310
c0e47fd4 4311 /* Extended integer types of the same width as a standard type have
4312 lesser rank, so those of the same width as int promote to int or
4313 unsigned int and are valid for printf formats expecting int or
4314 unsigned int. To avoid such special cases, avoid creating
4315 extended integer types for bit-fields if a standard integer type
4316 is available. */
4317 if (width == TYPE_PRECISION (integer_type_node))
4318 return unsignedp ? unsigned_type_node : integer_type_node;
4319 if (width == TYPE_PRECISION (signed_char_type_node))
4320 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4321 if (width == TYPE_PRECISION (short_integer_type_node))
4322 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4323 if (width == TYPE_PRECISION (long_integer_type_node))
4324 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4325 if (width == TYPE_PRECISION (long_long_integer_type_node))
4326 return (unsignedp ? long_long_unsigned_type_node
4327 : long_long_integer_type_node);
9f75f026 4328 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4329 if (int_n_enabled_p[i]
4330 && width == int_n_data[i].bitsize)
4331 return (unsignedp ? int_n_trees[i].unsigned_type
4332 : int_n_trees[i].signed_type);
c0e47fd4 4333 return build_nonstandard_integer_type (width, unsignedp);
4334}
4335
b268e47e 4336/* The C version of the register_builtin_type langhook. */
4337
4338void
4339c_register_builtin_type (tree type, const char* name)
4340{
4341 tree decl;
4342
e60a6f7b 4343 decl = build_decl (UNKNOWN_LOCATION,
4344 TYPE_DECL, get_identifier (name), type);
b268e47e 4345 DECL_ARTIFICIAL (decl) = 1;
4346 if (!TYPE_NAME (type))
4347 TYPE_NAME (type) = decl;
4348 pushdecl (decl);
5b247e9f 4349
4350 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
b268e47e 4351}
a9b9d10c 4352\f
aff9e656 4353/* Print an error message for invalid operands to arith operation
8e70fb09 4354 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4355 LOCATION is the location of the message. */
b0fc3e72 4356
4357void
8e70fb09 4358binary_op_error (location_t location, enum tree_code code,
4359 tree type0, tree type1)
b0fc3e72 4360{
19cb6b50 4361 const char *opname;
f03946e4 4362
b0fc3e72 4363 switch (code)
4364 {
b0fc3e72 4365 case PLUS_EXPR:
4366 opname = "+"; break;
4367 case MINUS_EXPR:
4368 opname = "-"; break;
4369 case MULT_EXPR:
4370 opname = "*"; break;
4371 case MAX_EXPR:
4372 opname = "max"; break;
4373 case MIN_EXPR:
4374 opname = "min"; break;
4375 case EQ_EXPR:
4376 opname = "=="; break;
4377 case NE_EXPR:
4378 opname = "!="; break;
4379 case LE_EXPR:
4380 opname = "<="; break;
4381 case GE_EXPR:
4382 opname = ">="; break;
4383 case LT_EXPR:
4384 opname = "<"; break;
4385 case GT_EXPR:
4386 opname = ">"; break;
4387 case LSHIFT_EXPR:
4388 opname = "<<"; break;
4389 case RSHIFT_EXPR:
4390 opname = ">>"; break;
4391 case TRUNC_MOD_EXPR:
66618a1e 4392 case FLOOR_MOD_EXPR:
b0fc3e72 4393 opname = "%"; break;
4394 case TRUNC_DIV_EXPR:
66618a1e 4395 case FLOOR_DIV_EXPR:
b0fc3e72 4396 opname = "/"; break;
4397 case BIT_AND_EXPR:
4398 opname = "&"; break;
4399 case BIT_IOR_EXPR:
4400 opname = "|"; break;
4401 case TRUTH_ANDIF_EXPR:
4402 opname = "&&"; break;
4403 case TRUTH_ORIF_EXPR:
4404 opname = "||"; break;
4405 case BIT_XOR_EXPR:
4406 opname = "^"; break;
31f820d2 4407 default:
315ba355 4408 gcc_unreachable ();
b0fc3e72 4409 }
8e70fb09 4410 error_at (location,
4411 "invalid operands to binary %s (have %qT and %qT)", opname,
4412 type0, type1);
b0fc3e72 4413}
4414\f
03fe1dc2 4415/* Given an expression as a tree, return its original type. Do this
4416 by stripping any conversion that preserves the sign and precision. */
4417static tree
4418expr_original_type (tree expr)
4419{
4420 STRIP_SIGN_NOPS (expr);
4421 return TREE_TYPE (expr);
4422}
4423
b0fc3e72 4424/* Subroutine of build_binary_op, used for comparison operations.
4425 See if the operands have both been converted from subword integer types
4426 and, if so, perhaps change them both back to their original type.
5b511807 4427 This function is also responsible for converting the two operands
4428 to the proper common type for comparison.
b0fc3e72 4429
4430 The arguments of this function are all pointers to local variables
4431 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4432 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4433
2623625f 4434 LOC is the location of the comparison.
4435
b0fc3e72 4436 If this function returns nonzero, it means that the comparison has
4437 a constant value. What this function returns is an expression for
4438 that value. */
4439
4440tree
2623625f 4441shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4442 tree *restype_ptr, enum tree_code *rescode_ptr)
b0fc3e72 4443{
19cb6b50 4444 tree type;
b0fc3e72 4445 tree op0 = *op0_ptr;
4446 tree op1 = *op1_ptr;
4447 int unsignedp0, unsignedp1;
4448 int real1, real2;
4449 tree primop0, primop1;
4450 enum tree_code code = *rescode_ptr;
4451
4452 /* Throw away any conversions to wider types
4453 already present in the operands. */
4454
7f506bca 4455 primop0 = c_common_get_narrower (op0, &unsignedp0);
4456 primop1 = c_common_get_narrower (op1, &unsignedp1);
b0fc3e72 4457
119d06b2 4458 /* If primopN is first sign-extended from primopN's precision to opN's
4459 precision, then zero-extended from opN's precision to
4460 *restype_ptr precision, shortenings might be invalid. */
4461 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4462 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4463 && !unsignedp0
4464 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4465 primop0 = op0;
4466 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4467 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4468 && !unsignedp1
4469 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4470 primop1 = op1;
4471
b0fc3e72 4472 /* Handle the case that OP0 does not *contain* a conversion
4473 but it *requires* conversion to FINAL_TYPE. */
4474
4475 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
78a8ed03 4476 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
b0fc3e72 4477 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
78a8ed03 4478 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b0fc3e72 4479
4480 /* If one of the operands must be floated, we cannot optimize. */
4481 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4482 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
ab2c1de8 4483
b0fc3e72 4484 /* If first arg is constant, swap the args (changing operation
2bd278cc 4485 so value is preserved), for canonicalization. Don't do this if
4486 the second arg is 0. */
b0fc3e72 4487
2bd278cc 4488 if (TREE_CONSTANT (primop0)
9421ebb9 4489 && !integer_zerop (primop1) && !real_zerop (primop1)
4490 && !fixed_zerop (primop1))
b0fc3e72 4491 {
dfcf26a5 4492 std::swap (primop0, primop1);
4493 std::swap (op0, op1);
b0fc3e72 4494 *op0_ptr = op0;
4495 *op1_ptr = op1;
dfcf26a5 4496 std::swap (unsignedp0, unsignedp1);
4497 std::swap (real1, real2);
b0fc3e72 4498
4499 switch (code)
4500 {
4501 case LT_EXPR:
4502 code = GT_EXPR;
4503 break;
4504 case GT_EXPR:
4505 code = LT_EXPR;
4506 break;
4507 case LE_EXPR:
4508 code = GE_EXPR;
4509 break;
4510 case GE_EXPR:
4511 code = LE_EXPR;
4512 break;
31f820d2 4513 default:
4514 break;
b0fc3e72 4515 }
4516 *rescode_ptr = code;
4517 }
4518
4519 /* If comparing an integer against a constant more bits wide,
4520 maybe we can deduce a value of 1 or 0 independent of the data.
4521 Or else truncate the constant now
4522 rather than extend the variable at run time.
4523
4524 This is only interesting if the constant is the wider arg.
4525 Also, it is not safe if the constant is unsigned and the
4526 variable arg is signed, since in this case the variable
4527 would be sign-extended and then regarded as unsigned.
4528 Our technique fails in this case because the lowest/highest
4529 possible unsigned results don't follow naturally from the
4530 lowest/highest possible values of the variable operand.
4531 For just EQ_EXPR and NE_EXPR there is another technique that
4532 could be used: see if the constant can be faithfully represented
4533 in the other operand's type, by truncating it and reextending it
4534 and see if that preserves the constant's value. */
4535
4536 if (!real1 && !real2
9421ebb9 4537 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
b0fc3e72 4538 && TREE_CODE (primop1) == INTEGER_CST
4539 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4540 {
4541 int min_gt, max_gt, min_lt, max_lt;
4542 tree maxval, minval;
4543 /* 1 if comparison is nominally unsigned. */
78a8ed03 4544 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
b0fc3e72 4545 tree val;
4546
4070745f 4547 type = c_common_signed_or_unsigned_type (unsignedp0,
4548 TREE_TYPE (primop0));
cda09c61 4549
b0fc3e72 4550 maxval = TYPE_MAX_VALUE (type);
4551 minval = TYPE_MIN_VALUE (type);
4552
4553 if (unsignedp && !unsignedp0)
4070745f 4554 *restype_ptr = c_common_signed_type (*restype_ptr);
b0fc3e72 4555
4556 if (TREE_TYPE (primop1) != *restype_ptr)
18dbec6f 4557 {
9a5e8086 4558 /* Convert primop1 to target type, but do not introduce
4559 additional overflow. We know primop1 is an int_cst. */
e913b5cd 4560 primop1 = force_fit_type (*restype_ptr,
796b6678 4561 wide_int::from
4562 (primop1,
4563 TYPE_PRECISION (*restype_ptr),
4564 TYPE_SIGN (TREE_TYPE (primop1))),
e913b5cd 4565 0, TREE_OVERFLOW (primop1));
18dbec6f 4566 }
b0fc3e72 4567 if (type != *restype_ptr)
4568 {
4569 minval = convert (*restype_ptr, minval);
4570 maxval = convert (*restype_ptr, maxval);
4571 }
4572
d99d10ca 4573 min_gt = tree_int_cst_lt (primop1, minval);
4574 max_gt = tree_int_cst_lt (primop1, maxval);
4575 min_lt = tree_int_cst_lt (minval, primop1);
4576 max_lt = tree_int_cst_lt (maxval, primop1);
b0fc3e72 4577
4578 val = 0;
4579 /* This used to be a switch, but Genix compiler can't handle that. */
4580 if (code == NE_EXPR)
4581 {
4582 if (max_lt || min_gt)
3c2239cf 4583 val = truthvalue_true_node;
b0fc3e72 4584 }
4585 else if (code == EQ_EXPR)
4586 {
4587 if (max_lt || min_gt)
3c2239cf 4588 val = truthvalue_false_node;
b0fc3e72 4589 }
4590 else if (code == LT_EXPR)
4591 {
4592 if (max_lt)
3c2239cf 4593 val = truthvalue_true_node;
b0fc3e72 4594 if (!min_lt)
3c2239cf 4595 val = truthvalue_false_node;
b0fc3e72 4596 }
4597 else if (code == GT_EXPR)
4598 {
4599 if (min_gt)
3c2239cf 4600 val = truthvalue_true_node;
b0fc3e72 4601 if (!max_gt)
3c2239cf 4602 val = truthvalue_false_node;
b0fc3e72 4603 }
4604 else if (code == LE_EXPR)
4605 {
4606 if (!max_gt)
3c2239cf 4607 val = truthvalue_true_node;
b0fc3e72 4608 if (min_gt)
3c2239cf 4609 val = truthvalue_false_node;
b0fc3e72 4610 }
4611 else if (code == GE_EXPR)
4612 {
4613 if (!min_lt)
3c2239cf 4614 val = truthvalue_true_node;
b0fc3e72 4615 if (max_lt)
3c2239cf 4616 val = truthvalue_false_node;
b0fc3e72 4617 }
4618
4619 /* If primop0 was sign-extended and unsigned comparison specd,
4620 we did a signed comparison above using the signed type bounds.
4621 But the comparison we output must be unsigned.
4622
4623 Also, for inequalities, VAL is no good; but if the signed
4624 comparison had *any* fixed result, it follows that the
4625 unsigned comparison just tests the sign in reverse
4626 (positive values are LE, negative ones GE).
4627 So we can generate an unsigned comparison
4628 against an extreme value of the signed type. */
4629
4630 if (unsignedp && !unsignedp0)
4631 {
4632 if (val != 0)
4633 switch (code)
4634 {
4635 case LT_EXPR:
4636 case GE_EXPR:
4637 primop1 = TYPE_MIN_VALUE (type);
4638 val = 0;
4639 break;
4640
4641 case LE_EXPR:
4642 case GT_EXPR:
4643 primop1 = TYPE_MAX_VALUE (type);
4644 val = 0;
4645 break;
31f820d2 4646
4647 default:
4648 break;
b0fc3e72 4649 }
11773141 4650 type = c_common_unsigned_type (type);
b0fc3e72 4651 }
4652
734ec290 4653 if (TREE_CODE (primop0) != INTEGER_CST)
b0fc3e72 4654 {
3c2239cf 4655 if (val == truthvalue_false_node)
03fe1dc2 4656 warning_at (loc, OPT_Wtype_limits,
4657 "comparison is always false due to limited range of data type");
3c2239cf 4658 if (val == truthvalue_true_node)
03fe1dc2 4659 warning_at (loc, OPT_Wtype_limits,
4660 "comparison is always true due to limited range of data type");
b0fc3e72 4661 }
4662
4663 if (val != 0)
4664 {
4665 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4666 if (TREE_SIDE_EFFECTS (primop0))
14ae0310 4667 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
b0fc3e72 4668 return val;
4669 }
4670
4671 /* Value is not predetermined, but do the comparison
4672 in the type of the operand that is not constant.
4673 TYPE is already properly set. */
4674 }
c4503c0a 4675
4676 /* If either arg is decimal float and the other is float, find the
4677 proper common type to use for comparison. */
7fd22aae 4678 else if (real1 && real2
4679 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4680 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4681 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4682
4683 /* If either arg is decimal float and the other is float, fail. */
c4503c0a 4684 else if (real1 && real2
4685 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4686 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
7fd22aae 4687 return 0;
c4503c0a 4688
b0fc3e72 4689 else if (real1 && real2
2203bd5c 4690 && (TYPE_PRECISION (TREE_TYPE (primop0))
4691 == TYPE_PRECISION (TREE_TYPE (primop1))))
b0fc3e72 4692 type = TREE_TYPE (primop0);
4693
4694 /* If args' natural types are both narrower than nominal type
4695 and both extend in the same manner, compare them
4696 in the type of the wider arg.
4697 Otherwise must actually extend both to the nominal
4698 common type lest different ways of extending
4699 alter the result.
4700 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4701
4702 else if (unsignedp0 == unsignedp1 && real1 == real2
4703 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4704 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4705 {
4706 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4070745f 4707 type = c_common_signed_or_unsigned_type (unsignedp0
78a8ed03 4708 || TYPE_UNSIGNED (*restype_ptr),
4070745f 4709 type);
b0fc3e72 4710 /* Make sure shorter operand is extended the right way
4711 to match the longer operand. */
4070745f 4712 primop0
4713 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4714 TREE_TYPE (primop0)),
4715 primop0);
4716 primop1
4717 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4718 TREE_TYPE (primop1)),
4719 primop1);
b0fc3e72 4720 }
4721 else
4722 {
4723 /* Here we must do the comparison on the nominal type
4724 using the args exactly as we received them. */
4725 type = *restype_ptr;
4726 primop0 = op0;
4727 primop1 = op1;
4728
4729 if (!real1 && !real2 && integer_zerop (primop1)
78a8ed03 4730 && TYPE_UNSIGNED (*restype_ptr))
b0fc3e72 4731 {
4732 tree value = 0;
03fe1dc2 4733 /* All unsigned values are >= 0, so we warn. However,
4734 if OP0 is a constant that is >= 0, the signedness of
4735 the comparison isn't an issue, so suppress the
4736 warning. */
4737 bool warn =
3df42822 4738 warn_type_limits && !in_system_header_at (loc)
03fe1dc2 4739 && !(TREE_CODE (primop0) == INTEGER_CST
4740 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4741 primop0)))
4742 /* Do not warn for enumeration types. */
4743 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4744
b0fc3e72 4745 switch (code)
4746 {
4747 case GE_EXPR:
03fe1dc2 4748 if (warn)
4749 warning_at (loc, OPT_Wtype_limits,
4750 "comparison of unsigned expression >= 0 is always true");
3c2239cf 4751 value = truthvalue_true_node;
b0fc3e72 4752 break;
4753
4754 case LT_EXPR:
03fe1dc2 4755 if (warn)
4756 warning_at (loc, OPT_Wtype_limits,
4757 "comparison of unsigned expression < 0 is always false");
3c2239cf 4758 value = truthvalue_false_node;
31f820d2 4759 break;
4760
4761 default:
4762 break;
b0fc3e72 4763 }
4764
4765 if (value != 0)
4766 {
4767 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4768 if (TREE_SIDE_EFFECTS (primop0))
14ae0310 4769 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4770 primop0, value);
b0fc3e72 4771 return value;
4772 }
4773 }
4774 }
4775
4776 *op0_ptr = convert (type, primop0);
4777 *op1_ptr = convert (type, primop1);
4778
3c2239cf 4779 *restype_ptr = truthvalue_type_node;
b0fc3e72 4780
4781 return 0;
4782}
4783\f
1c26100f 4784/* Return a tree for the sum or difference (RESULTCODE says which)
4785 of pointer PTROP and integer INTOP. */
4786
4787tree
389dd41b 4788pointer_int_sum (location_t loc, enum tree_code resultcode,
42f9a786 4789 tree ptrop, tree intop, bool complain)
1c26100f 4790{
add6ee5e 4791 tree size_exp, ret;
1c26100f 4792
1c26100f 4793 /* The result is a pointer of the same type that is being added. */
1c26100f 4794 tree result_type = TREE_TYPE (ptrop);
4795
4796 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4797 {
42f9a786 4798 if (complain && warn_pointer_arith)
4799 pedwarn (loc, OPT_Wpointer_arith,
4800 "pointer of type %<void *%> used in arithmetic");
4801 else if (!complain)
4802 return error_mark_node;
1c26100f 4803 size_exp = integer_one_node;
4804 }
4805 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4806 {
42f9a786 4807 if (complain && warn_pointer_arith)
4808 pedwarn (loc, OPT_Wpointer_arith,
4809 "pointer to a function used in arithmetic");
4810 else if (!complain)
4811 return error_mark_node;
1c26100f 4812 size_exp = integer_one_node;
4813 }
1c26100f 4814 else
4815 size_exp = size_in_bytes (TREE_TYPE (result_type));
4816
add6ee5e 4817 /* We are manipulating pointer values, so we don't need to warn
4818 about relying on undefined signed overflow. We disable the
4819 warning here because we use integer types so fold won't know that
4820 they are really pointers. */
4821 fold_defer_overflow_warnings ();
4822
1c26100f 4823 /* If what we are about to multiply by the size of the elements
4824 contains a constant term, apply distributive law
4825 and multiply that constant term separately.
4826 This helps produce common subexpressions. */
1c26100f 4827 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
84166705 4828 && !TREE_CONSTANT (intop)
1c26100f 4829 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4830 && TREE_CONSTANT (size_exp)
4831 /* If the constant comes from pointer subtraction,
4832 skip this optimization--it would cause an error. */
4833 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4834 /* If the constant is unsigned, and smaller than the pointer size,
4835 then we must skip this optimization. This is because it could cause
4836 an overflow error if the constant is negative but INTOP is not. */
84166705 4837 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
1c26100f 4838 || (TYPE_PRECISION (TREE_TYPE (intop))
4839 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4840 {
4841 enum tree_code subcode = resultcode;
4842 tree int_type = TREE_TYPE (intop);
4843 if (TREE_CODE (intop) == MINUS_EXPR)
4844 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4845 /* Convert both subexpression types to the type of intop,
4846 because weird cases involving pointer arithmetic
4847 can result in a sum or difference with different type args. */
8e70fb09 4848 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4849 subcode, ptrop,
1c26100f 4850 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4851 intop = convert (int_type, TREE_OPERAND (intop, 0));
4852 }
4853
4854 /* Convert the integer argument to a type the same size as sizetype
4855 so the multiply won't overflow spuriously. */
1c26100f 4856 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
78a8ed03 4857 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
1cae46be 4858 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
78a8ed03 4859 TYPE_UNSIGNED (sizetype)), intop);
1c26100f 4860
4861 /* Replace the integer argument with a suitable product by the object size.
c6feb9f1 4862 Do this multiplication as signed, then convert to the appropriate type
8032877c 4863 for the pointer operation and disregard an overflow that occurred only
c6feb9f1 4864 because of the sign-extension change in the latter conversion. */
4865 {
f576a2e4 4866 tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
4867 convert (TREE_TYPE (intop), size_exp));
c6feb9f1 4868 intop = convert (sizetype, t);
4869 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
e913b5cd 4870 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
c6feb9f1 4871 }
0de36bdb 4872
499e523f 4873 /* Create the sum or difference. */
0de36bdb 4874 if (resultcode == MINUS_EXPR)
389dd41b 4875 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
1c26100f 4876
2cc66f2a 4877 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
add6ee5e 4878
4879 fold_undefer_and_ignore_overflow_warnings ();
4880
4881 return ret;
1c26100f 4882}
4883\f
f59e3889 4884/* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4885 and if NON_CONST is known not to be permitted in an evaluated part
4886 of a constant expression. */
4887
4888tree
4889c_wrap_maybe_const (tree expr, bool non_const)
4890{
4891 bool nowarning = TREE_NO_WARNING (expr);
4892 location_t loc = EXPR_LOCATION (expr);
4893
4894 /* This should never be called for C++. */
4895 if (c_dialect_cxx ())
4896 gcc_unreachable ();
4897
4898 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4899 STRIP_TYPE_NOPS (expr);
4900 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4901 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4902 if (nowarning)
4903 TREE_NO_WARNING (expr) = 1;
4904 protected_set_expr_location (expr, loc);
4905
4906 return expr;
4907}
4908
a75b1c71 4909/* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4910 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4911 around the SAVE_EXPR if needed so that c_fully_fold does not need
4912 to look inside SAVE_EXPRs. */
4913
4914tree
4915c_save_expr (tree expr)
4916{
4917 bool maybe_const = true;
4918 if (c_dialect_cxx ())
4919 return save_expr (expr);
4920 expr = c_fully_fold (expr, false, &maybe_const);
4921 expr = save_expr (expr);
4922 if (!maybe_const)
f59e3889 4923 expr = c_wrap_maybe_const (expr, true);
a75b1c71 4924 return expr;
4925}
4926
6b68e71a 4927/* Return whether EXPR is a declaration whose address can never be
4928 NULL. */
4929
4930bool
9f627b1a 4931decl_with_nonnull_addr_p (const_tree expr)
6b68e71a 4932{
4933 return (DECL_P (expr)
4934 && (TREE_CODE (expr) == PARM_DECL
4935 || TREE_CODE (expr) == LABEL_DECL
4936 || !DECL_WEAK (expr)));
4937}
4938
b0fc3e72 4939/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
45a78cc0 4940 or for an `if' or `while' statement or ?..: exp. It should already
4941 have been validated to be of suitable type; otherwise, a bad
4942 diagnostic may result.
b0fc3e72 4943
8e70fb09 4944 The EXPR is located at LOCATION.
4945
b0fc3e72 4946 This preparation consists of taking the ordinary
4947 representation of an expression expr and producing a valid tree
4948 boolean expression describing whether expr is nonzero. We could
3c2239cf 4949 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
b0fc3e72 4950 but we optimize comparisons, &&, ||, and !.
4951
3c2239cf 4952 The resulting type should always be `truthvalue_type_node'. */
b0fc3e72 4953
4954tree
8e70fb09 4955c_common_truthvalue_conversion (location_t location, tree expr)
b0fc3e72 4956{
b0fc3e72 4957 switch (TREE_CODE (expr))
4958 {
318a728f 4959 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
e1232ce2 4960 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4961 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4962 case ORDERED_EXPR: case UNORDERED_EXPR:
ce04dcdc 4963 if (TREE_TYPE (expr) == truthvalue_type_node)
4964 return expr;
e60a6f7b 4965 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
ce04dcdc 4966 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
e60a6f7b 4967 goto ret;
ce04dcdc 4968
b0fc3e72 4969 case TRUTH_ANDIF_EXPR:
4970 case TRUTH_ORIF_EXPR:
4971 case TRUTH_AND_EXPR:
4972 case TRUTH_OR_EXPR:
31f6e93c 4973 case TRUTH_XOR_EXPR:
ce04dcdc 4974 if (TREE_TYPE (expr) == truthvalue_type_node)
4975 return expr;
e60a6f7b 4976 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
48e1416a 4977 c_common_truthvalue_conversion (location,
e60a6f7b 4978 TREE_OPERAND (expr, 0)),
4979 c_common_truthvalue_conversion (location,
4980 TREE_OPERAND (expr, 1)));
4981 goto ret;
3e851b85 4982
f6e28f72 4983 case TRUTH_NOT_EXPR:
ce04dcdc 4984 if (TREE_TYPE (expr) == truthvalue_type_node)
4985 return expr;
e60a6f7b 4986 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4987 c_common_truthvalue_conversion (location,
4988 TREE_OPERAND (expr, 0)));
4989 goto ret;
f6e28f72 4990
b0fc3e72 4991 case ERROR_MARK:
4992 return expr;
ab2c1de8 4993
b0fc3e72 4994 case INTEGER_CST:
eddad94a 4995 return integer_zerop (expr) ? truthvalue_false_node
4996 : truthvalue_true_node;
b0fc3e72 4997
4998 case REAL_CST:
5000e21c 4999 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
5000 ? truthvalue_true_node
5001 : truthvalue_false_node;
b0fc3e72 5002
9421ebb9 5003 case FIXED_CST:
5004 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
5005 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
5006 ? truthvalue_true_node
5007 : truthvalue_false_node;
5008
ce04dcdc 5009 case FUNCTION_DECL:
b6889cb0 5010 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
ce04dcdc 5011 /* Fall through. */
5012
b0fc3e72 5013 case ADDR_EXPR:
ce871053 5014 {
ee7d4d6a 5015 tree inner = TREE_OPERAND (expr, 0);
6b68e71a 5016 if (decl_with_nonnull_addr_p (inner))
ce871053 5017 {
6b68e71a 5018 /* Common Ada/Pascal programmer's mistake. */
8e70fb09 5019 warning_at (location,
5020 OPT_Waddress,
5021 "the address of %qD will always evaluate as %<true%>",
5022 inner);
ce871053 5023 return truthvalue_true_node;
5024 }
d473d901 5025 break;
ce871053 5026 }
b0fc3e72 5027
2203bd5c 5028 case COMPLEX_EXPR:
e60a6f7b 5029 expr = build_binary_op (EXPR_LOCATION (expr),
8e70fb09 5030 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
95809de4 5031 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
8e70fb09 5032 c_common_truthvalue_conversion (location,
5033 TREE_OPERAND (expr, 0)),
5034 c_common_truthvalue_conversion (location,
5035 TREE_OPERAND (expr, 1)),
2203bd5c 5036 0);
e60a6f7b 5037 goto ret;
2203bd5c 5038
b0fc3e72 5039 case NEGATE_EXPR:
5040 case ABS_EXPR:
5041 case FLOAT_EXPR:
c6418a4e 5042 case EXCESS_PRECISION_EXPR:
d10cfa8d 5043 /* These don't change whether an object is nonzero or zero. */
8e70fb09 5044 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
b0fc3e72 5045
5046 case LROTATE_EXPR:
5047 case RROTATE_EXPR:
d10cfa8d 5048 /* These don't change whether an object is zero or nonzero, but
b0fc3e72 5049 we can't ignore them if their second arg has side-effects. */
5050 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
e60a6f7b 5051 {
5052 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
5053 TREE_OPERAND (expr, 1),
48e1416a 5054 c_common_truthvalue_conversion
e60a6f7b 5055 (location, TREE_OPERAND (expr, 0)));
5056 goto ret;
5057 }
b0fc3e72 5058 else
8e70fb09 5059 return c_common_truthvalue_conversion (location,
5060 TREE_OPERAND (expr, 0));
73be5127 5061
b0fc3e72 5062 case COND_EXPR:
5063 /* Distribute the conversion into the arms of a COND_EXPR. */
a75b1c71 5064 if (c_dialect_cxx ())
e60a6f7b 5065 {
d0389adc 5066 tree op1 = TREE_OPERAND (expr, 1);
5067 tree op2 = TREE_OPERAND (expr, 2);
5068 /* In C++ one of the arms might have void type if it is throw. */
5069 if (!VOID_TYPE_P (TREE_TYPE (op1)))
5070 op1 = c_common_truthvalue_conversion (location, op1);
5071 if (!VOID_TYPE_P (TREE_TYPE (op2)))
5072 op2 = c_common_truthvalue_conversion (location, op2);
389dd41b 5073 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
d0389adc 5074 TREE_OPERAND (expr, 0), op1, op2);
e60a6f7b 5075 goto ret;
5076 }
a75b1c71 5077 else
e60a6f7b 5078 {
5079 /* Folding will happen later for C. */
5080 expr = build3 (COND_EXPR, truthvalue_type_node,
5081 TREE_OPERAND (expr, 0),
5082 c_common_truthvalue_conversion (location,
5083 TREE_OPERAND (expr, 1)),
5084 c_common_truthvalue_conversion (location,
5085 TREE_OPERAND (expr, 2)));
5086 goto ret;
5087 }
b0fc3e72 5088
72dd6141 5089 CASE_CONVERT:
f8913d47 5090 {
5091 tree totype = TREE_TYPE (expr);
5092 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
5093
db3d1ffc 5094 if (POINTER_TYPE_P (totype)
5095 && TREE_CODE (fromtype) == REFERENCE_TYPE)
5096 {
5097 tree inner = expr;
5098 STRIP_NOPS (inner);
5099
5100 if (DECL_P (inner))
5101 warning_at (location,
5102 OPT_Waddress,
5103 "the compiler can assume that the address of "
5104 "%qD will always evaluate to %<true%>",
5105 inner);
5106 }
5107
f8913d47 5108 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
5109 since that affects how `default_conversion' will behave. */
5110 if (TREE_CODE (totype) == REFERENCE_TYPE
5111 || TREE_CODE (fromtype) == REFERENCE_TYPE)
5112 break;
5113 /* Don't strip a conversion from C++0x scoped enum, since they
5114 don't implicitly convert to other types. */
5115 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
5116 && ENUM_IS_SCOPED (fromtype))
5117 break;
5118 /* If this isn't narrowing the argument, we can ignore it. */
5119 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
5120 return c_common_truthvalue_conversion (location,
5121 TREE_OPERAND (expr, 0));
5122 }
b0fc3e72 5123 break;
5124
16837b18 5125 case MODIFY_EXPR:
60a0513e 5126 if (!TREE_NO_WARNING (expr)
5127 && warn_parentheses)
5128 {
5129 warning (OPT_Wparentheses,
5130 "suggest parentheses around assignment used as truth value");
5131 TREE_NO_WARNING (expr) = 1;
5132 }
16837b18 5133 break;
73be5127 5134
31f820d2 5135 default:
5136 break;
b0fc3e72 5137 }
5138
2ba726d2 5139 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
a0748b7d 5140 {
93be21c0 5141 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
e60a6f7b 5142 expr = (build_binary_op
8e70fb09 5143 (EXPR_LOCATION (expr),
5144 (TREE_SIDE_EFFECTS (expr)
a0748b7d 5145 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
b6889cb0 5146 c_common_truthvalue_conversion
5147 (location,
5148 build_unary_op (location, REALPART_EXPR, t, 0)),
5149 c_common_truthvalue_conversion
5150 (location,
5151 build_unary_op (location, IMAGPART_EXPR, t, 0)),
a0748b7d 5152 0));
e60a6f7b 5153 goto ret;
a0748b7d 5154 }
2ba726d2 5155
9421ebb9 5156 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
5157 {
5158 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
5159 FCONST0 (TYPE_MODE
5160 (TREE_TYPE (expr))));
43158006 5161 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
9421ebb9 5162 }
e60a6f7b 5163 else
5164 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
9421ebb9 5165
e60a6f7b 5166 ret:
5167 protected_set_expr_location (expr, location);
5168 return expr;
b0fc3e72 5169}
5170\f
3237155d 5171static void def_builtin_1 (enum built_in_function fncode,
5172 const char *name,
5173 enum built_in_class fnclass,
5174 tree fntype, tree libtype,
5175 bool both_p, bool fallback_p, bool nonansi_p,
5176 tree fnattrs, bool implicit_p);
0d4238dc 5177
a5b1863e 5178
5179/* Apply the TYPE_QUALS to the new DECL. */
5180
5181void
1cae46be 5182c_apply_type_quals_to_decl (int type_quals, tree decl)
a5b1863e 5183{
adfb367f 5184 tree type = TREE_TYPE (decl);
b27ac6b5 5185
e4eabbe4 5186 if (type == error_mark_node)
5187 return;
adfb367f 5188
98a33d9f 5189 if ((type_quals & TYPE_QUAL_CONST)
5190 || (type && TREE_CODE (type) == REFERENCE_TYPE))
5191 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
5192 constructor can produce constant init, so rely on cp_finish_decl to
5193 clear TREE_READONLY if the variable has non-constant init. */
a5b1863e 5194 TREE_READONLY (decl) = 1;
5195 if (type_quals & TYPE_QUAL_VOLATILE)
5196 {
5197 TREE_SIDE_EFFECTS (decl) = 1;
5198 TREE_THIS_VOLATILE (decl) = 1;
5199 }
d91a20bc 5200 if (type_quals & TYPE_QUAL_RESTRICT)
a5b1863e 5201 {
adfb367f 5202 while (type && TREE_CODE (type) == ARRAY_TYPE)
5203 /* Allow 'restrict' on arrays of pointers.
5204 FIXME currently we just ignore it. */
5205 type = TREE_TYPE (type);
5206 if (!type
5207 || !POINTER_TYPE_P (type)
5208 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
b0b1af64 5209 error ("invalid use of %<restrict%>");
a5b1863e 5210 }
5211}
5212
b594087e 5213struct c_type_hasher : ggc_ptr_hash<tree_node>
2ef51f0e 5214{
5215 static hashval_t hash (tree);
5216 static bool equal (tree, tree);
5217};
5218
4ee9c684 5219/* Hash function for the problem of multiple type definitions in
5220 different files. This must hash all types that will compare
5221 equal via comptypes to the same value. In practice it hashes
0bed3869 5222 on some of the simple stuff and leaves the details to comptypes. */
4ee9c684 5223
2ef51f0e 5224hashval_t
5225c_type_hasher::hash (tree t)
4ee9c684 5226{
ecf2703d 5227 int n_elements;
4ee9c684 5228 int shift, size;
4ee9c684 5229 tree t2;
5230 switch (TREE_CODE (t))
5231 {
fbf0afd1 5232 /* For pointers, hash on pointee type plus some swizzling. */
2363ef00 5233 case POINTER_TYPE:
2ef51f0e 5234 return hash (TREE_TYPE (t)) ^ 0x3003003;
2363ef00 5235 /* Hash on number of elements and total size. */
5236 case ENUMERAL_TYPE:
5237 shift = 3;
5238 t2 = TYPE_VALUES (t);
5239 break;
5240 case RECORD_TYPE:
5241 shift = 0;
5242 t2 = TYPE_FIELDS (t);
5243 break;
5244 case QUAL_UNION_TYPE:
5245 shift = 1;
5246 t2 = TYPE_FIELDS (t);
5247 break;
5248 case UNION_TYPE:
5249 shift = 2;
5250 t2 = TYPE_FIELDS (t);
5251 break;
5252 default:
231bd014 5253 gcc_unreachable ();
4ee9c684 5254 }
ecf2703d 5255 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
5256 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
5257 n_elements = list_length (t2);
78c2e180 5258 /* We might have a VLA here. */
5259 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
5260 size = 0;
5261 else
f9ae6f95 5262 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
ecf2703d 5263 return ((size << 24) | (n_elements << shift));
4ee9c684 5264}
5265
2ef51f0e 5266bool
5267c_type_hasher::equal (tree t1, tree t2)
5268{
5269 return lang_hooks.types_compatible_p (t1, t2);
5270}
5271
5272static GTY(()) hash_table<c_type_hasher> *type_hash_table;
1ecd4018 5273
b5ba9f3a 5274/* Return the typed-based alias set for T, which may be an expression
f7c44134 5275 or a type. Return -1 if we don't do anything special. */
ab2c1de8 5276
32c2fdea 5277alias_set_type
1cae46be 5278c_common_get_alias_set (tree t)
b5ba9f3a 5279{
be4f2de7 5280 tree u;
1cae46be 5281
e58c17e7 5282 /* For VLAs, use the alias set of the element type rather than the
5283 default of alias set 0 for types compared structurally. */
5284 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
5285 {
5286 if (TREE_CODE (t) == ARRAY_TYPE)
5287 return get_alias_set (TREE_TYPE (t));
5288 return -1;
5289 }
5290
be4f2de7 5291 /* Permit type-punning when accessing a union, provided the access
5292 is directly through the union. For example, this code does not
5293 permit taking the address of a union member and then storing
5294 through it. Even the type-punning allowed here is a GCC
5295 extension, albeit a common and useful one; the C standard says
5296 that such accesses have implementation-defined behavior. */
5297 for (u = t;
5298 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
5299 u = TREE_OPERAND (u, 0))
5300 if (TREE_CODE (u) == COMPONENT_REF
5301 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
5302 return 0;
1e2513d9 5303
9fcc3e54 5304 /* That's all the expressions we handle specially. */
84166705 5305 if (!TYPE_P (t))
9fcc3e54 5306 return -1;
5307
d716ce75 5308 /* The C standard guarantees that any object may be accessed via an
9fcc3e54 5309 lvalue that has character type. */
5310 if (t == char_type_node
5311 || t == signed_char_type_node
5312 || t == unsigned_char_type_node)
f7c44134 5313 return 0;
a5b1863e 5314
1607663f 5315 /* The C standard specifically allows aliasing between signed and
5316 unsigned variants of the same type. We treat the signed
5317 variant as canonical. */
78a8ed03 5318 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
a8868e19 5319 {
4070745f 5320 tree t1 = c_common_signed_type (t);
1607663f 5321
a8868e19 5322 /* t1 == t can happen for boolean nodes which are always unsigned. */
5323 if (t1 != t)
5324 return get_alias_set (t1);
5325 }
1e2513d9 5326
4ee9c684 5327 /* Handle the case of multiple type nodes referring to "the same" type,
5328 which occurs with IMA. These share an alias set. FIXME: Currently only
5329 C90 is handled. (In C99 type compatibility is not transitive, which
5330 complicates things mightily. The alias set splay trees can theoretically
5331 represent this, but insertion is tricky when you consider all the
5332 different orders things might arrive in.) */
5333
5334 if (c_language != clk_c || flag_isoc99)
5335 return -1;
5336
0bed3869 5337 /* Save time if there's only one input file. */
e08bd2f4 5338 if (num_in_fnames == 1)
4ee9c684 5339 return -1;
5340
5341 /* Pointers need special handling if they point to any type that
5342 needs special handling (below). */
5343 if (TREE_CODE (t) == POINTER_TYPE)
5344 {
5345 tree t2;
5346 /* Find bottom type under any nested POINTERs. */
b27ac6b5 5347 for (t2 = TREE_TYPE (t);
af592f67 5348 TREE_CODE (t2) == POINTER_TYPE;
5349 t2 = TREE_TYPE (t2))
5350 ;
8e539fdc 5351 if (!RECORD_OR_UNION_TYPE_P (t2)
5352 && TREE_CODE (t2) != ENUMERAL_TYPE)
af592f67 5353 return -1;
4ee9c684 5354 if (TYPE_SIZE (t2) == 0)
af592f67 5355 return -1;
4ee9c684 5356 }
5357 /* These are the only cases that need special handling. */
8e539fdc 5358 if (!RECORD_OR_UNION_TYPE_P (t)
4ee9c684 5359 && TREE_CODE (t) != ENUMERAL_TYPE
4ee9c684 5360 && TREE_CODE (t) != POINTER_TYPE)
5361 return -1;
5362 /* Undefined? */
5363 if (TYPE_SIZE (t) == 0)
5364 return -1;
5365
b27ac6b5 5366 /* Look up t in hash table. Only one of the compatible types within each
4ee9c684 5367 alias set is recorded in the table. */
5368 if (!type_hash_table)
2ef51f0e 5369 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
5370 tree *slot = type_hash_table->find_slot (t, INSERT);
4ee9c684 5371 if (*slot != NULL)
ad16cb2c 5372 {
5373 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5374 return TYPE_ALIAS_SET ((tree)*slot);
5375 }
4ee9c684 5376 else
5377 /* Our caller will assign and record (in t) a new alias set; all we need
5378 to do is remember t in the hash table. */
5379 *slot = t;
5380
f7c44134 5381 return -1;
b5ba9f3a 5382}
902b4e01 5383\f
e60a6f7b 5384/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
a179a7dc 5385 the IS_SIZEOF parameter indicates which operator is being applied.
e60a6f7b 5386 The COMPLAIN flag controls whether we should diagnose possibly
5387 ill-formed constructs or not. LOC is the location of the SIZEOF or
a179a7dc 5388 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5389 a type in any context should be returned, rather than the normal
5390 alignment for that type. */
908c697e 5391
902b4e01 5392tree
e60a6f7b 5393c_sizeof_or_alignof_type (location_t loc,
a179a7dc 5394 tree type, bool is_sizeof, bool min_alignof,
5395 int complain)
902b4e01 5396{
d4c4d95c 5397 const char *op_name;
5398 tree value = NULL;
5399 enum tree_code type_code = TREE_CODE (type);
1cae46be 5400
908c697e 5401 op_name = is_sizeof ? "sizeof" : "__alignof__";
1cae46be 5402
d4c4d95c 5403 if (type_code == FUNCTION_TYPE)
902b4e01 5404 {
908c697e 5405 if (is_sizeof)
d4c4d95c 5406 {
9205a6cc 5407 if (complain && warn_pointer_arith)
5408 pedwarn (loc, OPT_Wpointer_arith,
8864917d 5409 "invalid application of %<sizeof%> to a function type");
ebd21de4 5410 else if (!complain)
5411 return error_mark_node;
d4c4d95c 5412 value = size_one_node;
5413 }
5414 else
83e25171 5415 {
5416 if (complain)
5417 {
5418 if (c_dialect_cxx ())
29438999 5419 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
83e25171 5420 "%<alignof%> applied to a function type");
5421 else
29438999 5422 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
83e25171 5423 "%<_Alignof%> applied to a function type");
5424 }
5425 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5426 }
d4c4d95c 5427 }
5428 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5429 {
1cae46be 5430 if (type_code == VOID_TYPE
9205a6cc 5431 && complain && warn_pointer_arith)
5432 pedwarn (loc, OPT_Wpointer_arith,
8864917d 5433 "invalid application of %qs to a void type", op_name);
ebd21de4 5434 else if (!complain)
5435 return error_mark_node;
d4c4d95c 5436 value = size_one_node;
902b4e01 5437 }
3df19e1b 5438 else if (!COMPLETE_TYPE_P (type)
5439 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
902b4e01 5440 {
d3a4d008 5441 if (complain)
3df19e1b 5442 error_at (loc, "invalid application of %qs to incomplete type %qT",
e60a6f7b 5443 op_name, type);
9c719c74 5444 return error_mark_node;
902b4e01 5445 }
3df19e1b 5446 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5447 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5448 {
5449 if (complain)
5450 error_at (loc, "invalid application of %qs to array type %qT of "
5451 "incomplete element type", op_name, type);
5452 return error_mark_node;
5453 }
902b4e01 5454 else
d4c4d95c 5455 {
908c697e 5456 if (is_sizeof)
d4c4d95c 5457 /* Convert in case a char is more than one unit. */
389dd41b 5458 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5459 size_int (TYPE_PRECISION (char_type_node)
5460 / BITS_PER_UNIT));
a179a7dc 5461 else if (min_alignof)
c2c4ae8d 5462 value = size_int (min_align_of_type (type));
d4c4d95c 5463 else
d37625c0 5464 value = size_int (TYPE_ALIGN_UNIT (type));
d4c4d95c 5465 }
902b4e01 5466
5a1fe2db 5467 /* VALUE will have the middle-end integer type sizetype.
5468 However, we should really return a value of type `size_t',
5469 which is just a typedef for an ordinary integer type. */
389dd41b 5470 value = fold_convert_loc (loc, size_type_node, value);
1cae46be 5471
d4c4d95c 5472 return value;
902b4e01 5473}
5474
5475/* Implement the __alignof keyword: Return the minimum required
097b5c8b 5476 alignment of EXPR, measured in bytes. For VAR_DECLs,
5477 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
e60a6f7b 5478 from an "aligned" __attribute__ specification). LOC is the
5479 location of the ALIGNOF operator. */
72040e7e 5480
902b4e01 5481tree
e60a6f7b 5482c_alignof_expr (location_t loc, tree expr)
902b4e01 5483{
5484 tree t;
5485
097b5c8b 5486 if (VAR_OR_FUNCTION_DECL_P (expr))
d37625c0 5487 t = size_int (DECL_ALIGN_UNIT (expr));
1cae46be 5488
902b4e01 5489 else if (TREE_CODE (expr) == COMPONENT_REF
5490 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5491 {
e60a6f7b 5492 error_at (loc, "%<__alignof%> applied to a bit-field");
902b4e01 5493 t = size_one_node;
5494 }
5495 else if (TREE_CODE (expr) == COMPONENT_REF
7cc7e163 5496 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
d37625c0 5497 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
1cae46be 5498
aa3e402e 5499 else if (INDIRECT_REF_P (expr))
902b4e01 5500 {
5501 tree t = TREE_OPERAND (expr, 0);
5502 tree best = t;
5503 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1cae46be 5504
72dd6141 5505 while (CONVERT_EXPR_P (t)
7cc7e163 5506 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
902b4e01 5507 {
5508 int thisalign;
5509
5510 t = TREE_OPERAND (t, 0);
5511 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5512 if (thisalign > bestalign)
5513 best = t, bestalign = thisalign;
5514 }
e60a6f7b 5515 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
902b4e01 5516 }
5517 else
e60a6f7b 5518 return c_alignof (loc, TREE_TYPE (expr));
902b4e01 5519
389dd41b 5520 return fold_convert_loc (loc, size_type_node, t);
902b4e01 5521}
5522\f
8fe4a266 5523/* Handle C and C++ default attributes. */
5524
5525enum built_in_attribute
5526{
5527#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5528#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
c8010b80 5529#define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
8fe4a266 5530#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5531#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
8fe4a266 5532#include "builtin-attrs.def"
5533#undef DEF_ATTR_NULL_TREE
5534#undef DEF_ATTR_INT
c8010b80 5535#undef DEF_ATTR_STRING
8fe4a266 5536#undef DEF_ATTR_IDENT
5537#undef DEF_ATTR_TREE_LIST
8fe4a266 5538 ATTR_LAST
5539};
5540
5541static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5542
1cae46be 5543static void c_init_attributes (void);
8fe4a266 5544
27213ba3 5545enum c_builtin_type
72040e7e 5546{
d2d4bdde 5547#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5548#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5549#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5550#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5551#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5552#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
0a39fd54 5553#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
3c77ca67 5554#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5555 ARG6) NAME,
5556#define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5557 ARG6, ARG7) NAME,
5558#define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5559 ARG6, ARG7, ARG8) NAME,
43895be5 5560#define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5561 ARG6, ARG7, ARG8, ARG9) NAME,
5562#define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5563 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
5564#define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5565 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
d2d4bdde 5566#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5567#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5568#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
03901330 5569#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
0a39fd54 5570#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3c77ca67 5571#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
6349b8cc 5572 NAME,
e561d5e1 5573#define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5574 ARG6) NAME,
6349b8cc 5575#define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5576 ARG6, ARG7) NAME,
d2d4bdde 5577#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5578#include "builtin-types.def"
5579#undef DEF_PRIMITIVE_TYPE
5580#undef DEF_FUNCTION_TYPE_0
5581#undef DEF_FUNCTION_TYPE_1
5582#undef DEF_FUNCTION_TYPE_2
5583#undef DEF_FUNCTION_TYPE_3
5584#undef DEF_FUNCTION_TYPE_4
0a39fd54 5585#undef DEF_FUNCTION_TYPE_5
5586#undef DEF_FUNCTION_TYPE_6
27213ba3 5587#undef DEF_FUNCTION_TYPE_7
bc7bff74 5588#undef DEF_FUNCTION_TYPE_8
43895be5 5589#undef DEF_FUNCTION_TYPE_9
5590#undef DEF_FUNCTION_TYPE_10
5591#undef DEF_FUNCTION_TYPE_11
d2d4bdde 5592#undef DEF_FUNCTION_TYPE_VAR_0
5593#undef DEF_FUNCTION_TYPE_VAR_1
5594#undef DEF_FUNCTION_TYPE_VAR_2
03901330 5595#undef DEF_FUNCTION_TYPE_VAR_3
0a39fd54 5596#undef DEF_FUNCTION_TYPE_VAR_4
5597#undef DEF_FUNCTION_TYPE_VAR_5
e561d5e1 5598#undef DEF_FUNCTION_TYPE_VAR_6
6349b8cc 5599#undef DEF_FUNCTION_TYPE_VAR_7
d2d4bdde 5600#undef DEF_POINTER_TYPE
27213ba3 5601 BT_LAST
5602};
5603
5604typedef enum c_builtin_type builtin_type;
d2d4bdde 5605
27213ba3 5606/* A temporary array for c_common_nodes_and_builtins. Used in
5607 communication with def_fn_type. */
5608static tree builtin_types[(int) BT_LAST + 1];
d2d4bdde 5609
27213ba3 5610/* A helper function for c_common_nodes_and_builtins. Build function type
5611 for DEF with return type RET and N arguments. If VAR is true, then the
5612 function should be variadic after those N arguments.
5613
5614 Takes special care not to ICE if any of the types involved are
5615 error_mark_node, which indicates that said type is not in fact available
5616 (see builtin_type_for_size). In which case the function type as a whole
5617 should be error_mark_node. */
5618
5619static void
5620def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5621{
3a939d12 5622 tree t;
5623 tree *args = XALLOCAVEC (tree, n);
27213ba3 5624 va_list list;
5625 int i;
5626
5627 va_start (list, n);
5628 for (i = 0; i < n; ++i)
5629 {
7d339f93 5630 builtin_type a = (builtin_type) va_arg (list, int);
27213ba3 5631 t = builtin_types[a];
5632 if (t == error_mark_node)
5633 goto egress;
3a939d12 5634 args[i] = t;
27213ba3 5635 }
27213ba3 5636
27213ba3 5637 t = builtin_types[ret];
5638 if (t == error_mark_node)
5639 goto egress;
3a939d12 5640 if (var)
5641 t = build_varargs_function_type_array (t, n, args);
5642 else
5643 t = build_function_type_array (t, n, args);
27213ba3 5644
5645 egress:
5646 builtin_types[def] = t;
451c8e2f 5647 va_end (list);
27213ba3 5648}
5649
dce22712 5650/* Build builtin functions common to both C and C++ language
5651 frontends. */
5652
5653static void
5654c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5655{
5656#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5657 builtin_types[ENUM] = VALUE;
5658#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5659 def_fn_type (ENUM, RETURN, 0, 0);
5660#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5661 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5662#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5663 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5664#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5665 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5666#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5667 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5668#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5669 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5670#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5671 ARG6) \
5672 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5673#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5674 ARG6, ARG7) \
5675 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
bc7bff74 5676#define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5677 ARG6, ARG7, ARG8) \
5678 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5679 ARG7, ARG8);
43895be5 5680#define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5681 ARG6, ARG7, ARG8, ARG9) \
5682 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5683 ARG7, ARG8, ARG9);
5684#define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5685 ARG6, ARG7, ARG8, ARG9, ARG10) \
5686 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5687 ARG7, ARG8, ARG9, ARG10);
5688#define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5689 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5690 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5691 ARG7, ARG8, ARG9, ARG10, ARG11);
dce22712 5692#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5693 def_fn_type (ENUM, RETURN, 1, 0);
5694#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5695 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5696#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5697 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5698#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5699 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5700#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5701 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5702#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5703 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
e561d5e1 5704#define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5705 ARG6) \
5706 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
6349b8cc 5707#define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5708 ARG6, ARG7) \
5709 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
dce22712 5710#define DEF_POINTER_TYPE(ENUM, TYPE) \
5711 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5712
5713#include "builtin-types.def"
5714
5715#undef DEF_PRIMITIVE_TYPE
3c77ca67 5716#undef DEF_FUNCTION_TYPE_0
dce22712 5717#undef DEF_FUNCTION_TYPE_1
5718#undef DEF_FUNCTION_TYPE_2
5719#undef DEF_FUNCTION_TYPE_3
5720#undef DEF_FUNCTION_TYPE_4
5721#undef DEF_FUNCTION_TYPE_5
5722#undef DEF_FUNCTION_TYPE_6
3c77ca67 5723#undef DEF_FUNCTION_TYPE_7
5724#undef DEF_FUNCTION_TYPE_8
43895be5 5725#undef DEF_FUNCTION_TYPE_9
5726#undef DEF_FUNCTION_TYPE_10
5727#undef DEF_FUNCTION_TYPE_11
dce22712 5728#undef DEF_FUNCTION_TYPE_VAR_0
5729#undef DEF_FUNCTION_TYPE_VAR_1
5730#undef DEF_FUNCTION_TYPE_VAR_2
5731#undef DEF_FUNCTION_TYPE_VAR_3
5732#undef DEF_FUNCTION_TYPE_VAR_4
5733#undef DEF_FUNCTION_TYPE_VAR_5
e561d5e1 5734#undef DEF_FUNCTION_TYPE_VAR_6
6349b8cc 5735#undef DEF_FUNCTION_TYPE_VAR_7
dce22712 5736#undef DEF_POINTER_TYPE
5737 builtin_types[(int) BT_LAST] = NULL_TREE;
5738
5739 c_init_attributes ();
5740
5741#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5742 NONANSI_P, ATTRS, IMPLICIT, COND) \
5743 if (NAME && COND) \
5744 def_builtin_1 (ENUM, NAME, CLASS, \
5745 builtin_types[(int) TYPE], \
5746 builtin_types[(int) LIBTYPE], \
5747 BOTH_P, FALLBACK_P, NONANSI_P, \
5748 built_in_attributes[(int) ATTRS], IMPLICIT);
5749#include "builtins.def"
dce22712 5750
87eb1c28 5751 targetm.init_builtins ();
5752
471eff36 5753 build_common_builtin_nodes ();
dce22712 5754
a89e6c15 5755 if (flag_cilkplus)
d037099f 5756 cilk_init_builtins ();
dce22712 5757}
5758
9e6687c8 5759/* Like get_identifier, but avoid warnings about null arguments when
5760 the argument may be NULL for targets where GCC lacks stdint.h type
5761 information. */
5762
5763static inline tree
5764c_get_ident (const char *id)
5765{
5766 return get_identifier (id);
5767}
5768
27213ba3 5769/* Build tree nodes and builtin functions common to both C and C++ language
5770 frontends. */
5771
5772void
5773c_common_nodes_and_builtins (void)
5774{
924bbf02 5775 int char16_type_size;
5776 int char32_type_size;
174fcc61 5777 int wchar_type_size;
5778 tree array_domain_type;
2d47cc32 5779 tree va_list_ref_type_node;
8a15c04a 5780 tree va_list_arg_type_node;
9f75f026 5781 int i;
a66c9326 5782
c38a75b7 5783 build_common_tree_nodes (flag_signed_char, flag_short_double);
e593356b 5784
174fcc61 5785 /* Define `int' and `char' first so that dbx will output them first. */
d946ea19 5786 record_builtin_type (RID_INT, NULL, integer_type_node);
174fcc61 5787 record_builtin_type (RID_CHAR, "char", char_type_node);
5788
5789 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5790 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5791 but not C. Are the conditionals here needed? */
c0f19401 5792 if (c_dialect_cxx ())
d946ea19 5793 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
174fcc61 5794 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5795 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5796 record_builtin_type (RID_MAX, "long unsigned int",
5797 long_unsigned_type_node);
9f75f026 5798
5799 for (i = 0; i < NUM_INT_N_ENTS; i ++)
6388cfe2 5800 {
9f75f026 5801 char name[25];
5802
5803 sprintf (name, "__int%d", int_n_data[i].bitsize);
76738f56 5804 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
9f75f026 5805 int_n_trees[i].signed_type);
5806 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
76738f56 5807 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
6388cfe2 5808 }
9f75f026 5809
c0f19401 5810 if (c_dialect_cxx ())
174fcc61 5811 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5812 record_builtin_type (RID_MAX, "long long int",
5813 long_long_integer_type_node);
5814 record_builtin_type (RID_MAX, "long long unsigned int",
5815 long_long_unsigned_type_node);
c0f19401 5816 if (c_dialect_cxx ())
174fcc61 5817 record_builtin_type (RID_MAX, "long long unsigned",
5818 long_long_unsigned_type_node);
5819 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5820 record_builtin_type (RID_MAX, "short unsigned int",
5821 short_unsigned_type_node);
c0f19401 5822 if (c_dialect_cxx ())
174fcc61 5823 record_builtin_type (RID_MAX, "unsigned short",
5824 short_unsigned_type_node);
5825
5826 /* Define both `signed char' and `unsigned char'. */
5827 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5828 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5829
771d21fa 5830 /* These are types that c_common_type_for_size and
5831 c_common_type_for_mode use. */
e60a6f7b 5832 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5833 TYPE_DECL, NULL_TREE,
dc24ddbd 5834 intQI_type_node));
e60a6f7b 5835 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5836 TYPE_DECL, NULL_TREE,
dc24ddbd 5837 intHI_type_node));
e60a6f7b 5838 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5839 TYPE_DECL, NULL_TREE,
dc24ddbd 5840 intSI_type_node));
e60a6f7b 5841 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5842 TYPE_DECL, NULL_TREE,
dc24ddbd 5843 intDI_type_node));
174fcc61 5844#if HOST_BITS_PER_WIDE_INT >= 64
9f75f026 5845 /* Note that this is different than the __int128 type that's part of
5846 the generic __intN support. */
f1515a39 5847 if (targetm.scalar_mode_supported_p (TImode))
e60a6f7b 5848 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5849 TYPE_DECL,
f1515a39 5850 get_identifier ("__int128_t"),
5851 intTI_type_node));
174fcc61 5852#endif
e60a6f7b 5853 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5854 TYPE_DECL, NULL_TREE,
dc24ddbd 5855 unsigned_intQI_type_node));
e60a6f7b 5856 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5857 TYPE_DECL, NULL_TREE,
dc24ddbd 5858 unsigned_intHI_type_node));
e60a6f7b 5859 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5860 TYPE_DECL, NULL_TREE,
dc24ddbd 5861 unsigned_intSI_type_node));
e60a6f7b 5862 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5863 TYPE_DECL, NULL_TREE,
dc24ddbd 5864 unsigned_intDI_type_node));
174fcc61 5865#if HOST_BITS_PER_WIDE_INT >= 64
f1515a39 5866 if (targetm.scalar_mode_supported_p (TImode))
e60a6f7b 5867 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5868 TYPE_DECL,
f1515a39 5869 get_identifier ("__uint128_t"),
5870 unsigned_intTI_type_node));
174fcc61 5871#endif
5872
5873 /* Create the widest literal types. */
5874 widest_integer_literal_type_node
5875 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
e60a6f7b 5876 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5877 TYPE_DECL, NULL_TREE,
dc24ddbd 5878 widest_integer_literal_type_node));
174fcc61 5879
5880 widest_unsigned_literal_type_node
5881 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
e60a6f7b 5882 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5883 TYPE_DECL, NULL_TREE,
dc24ddbd 5884 widest_unsigned_literal_type_node));
174fcc61 5885
654ef926 5886 signed_size_type_node = c_common_signed_type (size_type_node);
174fcc61 5887
73673831 5888 pid_type_node =
5889 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5890
d946ea19 5891 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5892 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
174fcc61 5893 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5894
c4503c0a 5895 /* Only supported decimal floating point extension if the target
5896 actually supports underlying modes. */
48e1416a 5897 if (targetm.scalar_mode_supported_p (SDmode)
c4503c0a 5898 && targetm.scalar_mode_supported_p (DDmode)
5899 && targetm.scalar_mode_supported_p (TDmode))
5900 {
5901 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5902 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5903 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5904 }
5905
9421ebb9 5906 if (targetm.fixed_point_supported_p ())
5907 {
5908 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5909 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5910 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5911 record_builtin_type (RID_MAX, "long long _Fract",
5912 long_long_fract_type_node);
5913 record_builtin_type (RID_MAX, "unsigned short _Fract",
5914 unsigned_short_fract_type_node);
5915 record_builtin_type (RID_MAX, "unsigned _Fract",
5916 unsigned_fract_type_node);
5917 record_builtin_type (RID_MAX, "unsigned long _Fract",
5918 unsigned_long_fract_type_node);
5919 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5920 unsigned_long_long_fract_type_node);
5921 record_builtin_type (RID_MAX, "_Sat short _Fract",
5922 sat_short_fract_type_node);
5923 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5924 record_builtin_type (RID_MAX, "_Sat long _Fract",
5925 sat_long_fract_type_node);
5926 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5927 sat_long_long_fract_type_node);
5928 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5929 sat_unsigned_short_fract_type_node);
5930 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5931 sat_unsigned_fract_type_node);
5932 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5933 sat_unsigned_long_fract_type_node);
5934 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5935 sat_unsigned_long_long_fract_type_node);
5936 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5937 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5938 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5939 record_builtin_type (RID_MAX, "long long _Accum",
5940 long_long_accum_type_node);
5941 record_builtin_type (RID_MAX, "unsigned short _Accum",
5942 unsigned_short_accum_type_node);
5943 record_builtin_type (RID_MAX, "unsigned _Accum",
5944 unsigned_accum_type_node);
5945 record_builtin_type (RID_MAX, "unsigned long _Accum",
5946 unsigned_long_accum_type_node);
5947 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5948 unsigned_long_long_accum_type_node);
5949 record_builtin_type (RID_MAX, "_Sat short _Accum",
5950 sat_short_accum_type_node);
5951 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5952 record_builtin_type (RID_MAX, "_Sat long _Accum",
5953 sat_long_accum_type_node);
5954 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5955 sat_long_long_accum_type_node);
5956 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5957 sat_unsigned_short_accum_type_node);
5958 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5959 sat_unsigned_accum_type_node);
5960 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5961 sat_unsigned_long_accum_type_node);
5962 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5963 sat_unsigned_long_long_accum_type_node);
5964
5965 }
5966
e60a6f7b 5967 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5968 TYPE_DECL,
dc24ddbd 5969 get_identifier ("complex int"),
5970 complex_integer_type_node));
e60a6f7b 5971 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5972 TYPE_DECL,
dc24ddbd 5973 get_identifier ("complex float"),
5974 complex_float_type_node));
e60a6f7b 5975 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5976 TYPE_DECL,
dc24ddbd 5977 get_identifier ("complex double"),
5978 complex_double_type_node));
5979 lang_hooks.decls.pushdecl
e60a6f7b 5980 (build_decl (UNKNOWN_LOCATION,
5981 TYPE_DECL, get_identifier ("complex long double"),
20325f61 5982 complex_long_double_type_node));
174fcc61 5983
e256d445 5984 if (c_dialect_cxx ())
5985 /* For C++, make fileptr_type_node a distinct void * type until
5986 FILE type is defined. */
e086912e 5987 fileptr_type_node = build_variant_type_copy (ptr_type_node);
e256d445 5988
d946ea19 5989 record_builtin_type (RID_VOID, NULL, void_type_node);
174fcc61 5990
6753bca0 5991 /* Set the TYPE_NAME for any variants that were built before
5992 record_builtin_type gave names to the built-in types. */
5993 {
5994 tree void_name = TYPE_NAME (void_type_node);
5995 TYPE_NAME (void_type_node) = NULL_TREE;
5996 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5997 = void_name;
5998 TYPE_NAME (void_type_node) = void_name;
5999 }
6000
174fcc61 6001 void_list_node = build_void_list_node ();
6002
6003 /* Make a type to be the domain of a few array types
6004 whose domains don't really matter.
6005 200 is small enough that it always fits in size_t
6006 and large enough that it can hold most function names for the
6007 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
6008 array_domain_type = build_index_type (size_int (200));
6009
6010 /* Make a type for arrays of characters.
6011 With luck nothing will ever really depend on the length of this
6012 array type. */
6013 char_array_type_node
6014 = build_array_type (char_type_node, array_domain_type);
6015
d2d4bdde 6016 string_type_node = build_pointer_type (char_type_node);
6017 const_string_type_node
6018 = build_pointer_type (build_qualified_type
6019 (char_type_node, TYPE_QUAL_CONST));
6020
174fcc61 6021 /* This is special for C++ so functions can be overloaded. */
18ef7ac2 6022 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
174fcc61 6023 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
6024 wchar_type_size = TYPE_PRECISION (wchar_type_node);
f3449a3c 6025 underlying_wchar_type_node = wchar_type_node;
c0f19401 6026 if (c_dialect_cxx ())
174fcc61 6027 {
78a8ed03 6028 if (TYPE_UNSIGNED (wchar_type_node))
174fcc61 6029 wchar_type_node = make_unsigned_type (wchar_type_size);
6030 else
6031 wchar_type_node = make_signed_type (wchar_type_size);
6032 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
6033 }
174fcc61 6034
6035 /* This is for wide string constants. */
6036 wchar_array_type_node
6037 = build_array_type (wchar_type_node, array_domain_type);
6038
924bbf02 6039 /* Define 'char16_t'. */
6040 char16_type_node = get_identifier (CHAR16_TYPE);
6041 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
6042 char16_type_size = TYPE_PRECISION (char16_type_node);
6043 if (c_dialect_cxx ())
6044 {
6045 char16_type_node = make_unsigned_type (char16_type_size);
6046
60777f69 6047 if (cxx_dialect >= cxx11)
924bbf02 6048 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
6049 }
6050
6051 /* This is for UTF-16 string constants. */
6052 char16_array_type_node
6053 = build_array_type (char16_type_node, array_domain_type);
6054
6055 /* Define 'char32_t'. */
6056 char32_type_node = get_identifier (CHAR32_TYPE);
6057 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
6058 char32_type_size = TYPE_PRECISION (char32_type_node);
6059 if (c_dialect_cxx ())
6060 {
6061 char32_type_node = make_unsigned_type (char32_type_size);
6062
60777f69 6063 if (cxx_dialect >= cxx11)
924bbf02 6064 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
6065 }
6066
6067 /* This is for UTF-32 string constants. */
6068 char32_array_type_node
6069 = build_array_type (char32_type_node, array_domain_type);
6070
6bf5ed8d 6071 wint_type_node =
6072 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
6073
6074 intmax_type_node =
6075 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
6076 uintmax_type_node =
6077 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
6078
f3449a3c 6079 if (SIG_ATOMIC_TYPE)
6080 sig_atomic_type_node =
9e6687c8 6081 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
f3449a3c 6082 if (INT8_TYPE)
6083 int8_type_node =
9e6687c8 6084 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
f3449a3c 6085 if (INT16_TYPE)
6086 int16_type_node =
9e6687c8 6087 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
f3449a3c 6088 if (INT32_TYPE)
6089 int32_type_node =
9e6687c8 6090 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
f3449a3c 6091 if (INT64_TYPE)
6092 int64_type_node =
9e6687c8 6093 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
f3449a3c 6094 if (UINT8_TYPE)
6095 uint8_type_node =
9e6687c8 6096 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
f3449a3c 6097 if (UINT16_TYPE)
d1081017 6098 c_uint16_type_node = uint16_type_node =
9e6687c8 6099 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
f3449a3c 6100 if (UINT32_TYPE)
d1081017 6101 c_uint32_type_node = uint32_type_node =
9e6687c8 6102 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
f3449a3c 6103 if (UINT64_TYPE)
d1081017 6104 c_uint64_type_node = uint64_type_node =
9e6687c8 6105 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
f3449a3c 6106 if (INT_LEAST8_TYPE)
6107 int_least8_type_node =
9e6687c8 6108 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
f3449a3c 6109 if (INT_LEAST16_TYPE)
6110 int_least16_type_node =
9e6687c8 6111 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
f3449a3c 6112 if (INT_LEAST32_TYPE)
6113 int_least32_type_node =
9e6687c8 6114 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
f3449a3c 6115 if (INT_LEAST64_TYPE)
6116 int_least64_type_node =
9e6687c8 6117 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
f3449a3c 6118 if (UINT_LEAST8_TYPE)
6119 uint_least8_type_node =
9e6687c8 6120 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
f3449a3c 6121 if (UINT_LEAST16_TYPE)
6122 uint_least16_type_node =
9e6687c8 6123 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
f3449a3c 6124 if (UINT_LEAST32_TYPE)
6125 uint_least32_type_node =
9e6687c8 6126 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
f3449a3c 6127 if (UINT_LEAST64_TYPE)
6128 uint_least64_type_node =
9e6687c8 6129 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
f3449a3c 6130 if (INT_FAST8_TYPE)
6131 int_fast8_type_node =
9e6687c8 6132 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
f3449a3c 6133 if (INT_FAST16_TYPE)
6134 int_fast16_type_node =
9e6687c8 6135 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
f3449a3c 6136 if (INT_FAST32_TYPE)
6137 int_fast32_type_node =
9e6687c8 6138 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
f3449a3c 6139 if (INT_FAST64_TYPE)
6140 int_fast64_type_node =
9e6687c8 6141 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
f3449a3c 6142 if (UINT_FAST8_TYPE)
6143 uint_fast8_type_node =
9e6687c8 6144 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
f3449a3c 6145 if (UINT_FAST16_TYPE)
6146 uint_fast16_type_node =
9e6687c8 6147 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
f3449a3c 6148 if (UINT_FAST32_TYPE)
6149 uint_fast32_type_node =
9e6687c8 6150 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
f3449a3c 6151 if (UINT_FAST64_TYPE)
6152 uint_fast64_type_node =
9e6687c8 6153 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
f3449a3c 6154 if (INTPTR_TYPE)
6155 intptr_type_node =
9e6687c8 6156 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
f3449a3c 6157 if (UINTPTR_TYPE)
6158 uintptr_type_node =
9e6687c8 6159 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
f3449a3c 6160
3a939d12 6161 default_function_type
6162 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
6bf5ed8d 6163 ptrdiff_type_node
6164 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
11773141 6165 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
6bf5ed8d 6166
dc24ddbd 6167 lang_hooks.decls.pushdecl
e60a6f7b 6168 (build_decl (UNKNOWN_LOCATION,
6169 TYPE_DECL, get_identifier ("__builtin_va_list"),
20325f61 6170 va_list_type_node));
202d6e5f 6171 if (targetm.enum_va_list_p)
acd6f472 6172 {
6173 int l;
6174 const char *pname;
6175 tree ptype;
5f57a8b1 6176
202d6e5f 6177 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
acd6f472 6178 {
6179 lang_hooks.decls.pushdecl
6180 (build_decl (UNKNOWN_LOCATION,
6181 TYPE_DECL, get_identifier (pname),
6182 ptype));
ab2c1de8 6183
acd6f472 6184 }
6185 }
8a15c04a 6186
8a15c04a 6187 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
2d47cc32 6188 {
6189 va_list_arg_type_node = va_list_ref_type_node =
6190 build_pointer_type (TREE_TYPE (va_list_type_node));
6191 }
8a15c04a 6192 else
2d47cc32 6193 {
6194 va_list_arg_type_node = va_list_type_node;
6195 va_list_ref_type_node = build_reference_type (va_list_type_node);
6196 }
1cae46be 6197
dce22712 6198 if (!flag_preprocess_only)
6199 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
ffa8918b 6200
5c62f199 6201 main_identifier_node = get_identifier ("main");
ae84079f 6202
6203 /* Create the built-in __null node. It is important that this is
6204 not shared. */
271e739a 6205 null_node = make_int_cst (1, 1);
ae84079f 6206 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
27213ba3 6207
6208 /* Since builtin_types isn't gc'ed, don't export these nodes. */
6209 memset (builtin_types, 0, sizeof (builtin_types));
72040e7e 6210}
a66c9326 6211
79b01846 6212/* The number of named compound-literals generated thus far. */
6213static GTY(()) int compound_literal_number;
6214
6215/* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
6216
6217void
6218set_compound_literal_name (tree decl)
6219{
6220 char *name;
6221 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
6222 compound_literal_number);
6223 compound_literal_number++;
6224 DECL_NAME (decl) = get_identifier (name);
6225}
6226
2e474820 6227/* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
6228 TYPE and operand OP. */
6229
6230static tree
6231build_va_arg_1 (location_t loc, tree type, tree op)
6232{
6233 tree expr = build1 (VA_ARG_EXPR, type, op);
6234 SET_EXPR_LOCATION (expr, loc);
6235 return expr;
6236}
6237
6238/* Return a VA_ARG_EXPR corresponding to a source-level expression
6239 va_arg (EXPR, TYPE) at source location LOC. */
6240
a66c9326 6241tree
e60a6f7b 6242build_va_arg (location_t loc, tree expr, tree type)
a66c9326 6243{
c37be9ec 6244 tree va_type = TREE_TYPE (expr);
6245 tree canon_va_type = (va_type == error_mark_node
6246 ? NULL_TREE
6247 : targetm.canonical_va_list_type (va_type));
6248
2e474820 6249 if (va_type == error_mark_node
6250 || canon_va_type == NULL_TREE)
c37be9ec 6251 {
2e474820 6252 /* Let's handle things neutrallly, if expr:
6253 - has undeclared type, or
6254 - is not an va_list type. */
6255 return build_va_arg_1 (loc, type, expr);
c37be9ec 6256 }
f7fec1c7 6257
2e474820 6258 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
6259 {
6260 /* Case 1: Not an array type. */
6261
6262 /* Take the address, to get '&ap'. */
6263 mark_addressable (expr);
6264 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
6265
6266 /* Verify that &ap is still recognized as having va_list type. */
6267 tree canon_expr_type
6268 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6269 gcc_assert (canon_expr_type != NULL_TREE);
6270
6271 return build_va_arg_1 (loc, type, expr);
6272 }
6273
6274 /* Case 2: Array type.
6275
6276 Background:
6277
6278 For contrast, let's start with the simple case (case 1). If
6279 canon_va_type is not an array type, but say a char *, then when
6280 passing-by-value a va_list, the type of the va_list param decl is
6281 the same as for another va_list decl (all ap's are char *):
6282
6283 f2_1 (char * ap)
6284 D.1815 = VA_ARG (&ap, 0B, 1);
6285 return D.1815;
6286
6287 f2 (int i)
6288 char * ap.0;
6289 char * ap;
6290 __builtin_va_start (&ap, 0);
6291 ap.0 = ap;
6292 res = f2_1 (ap.0);
6293 __builtin_va_end (&ap);
6294 D.1812 = res;
6295 return D.1812;
6296
6297 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
6298 va_list the type of the va_list param decl (case 2b, struct * ap) is not
6299 the same as for another va_list decl (case 2a, struct ap[1]).
6300
6301 f2_1 (struct * ap)
6302 D.1844 = VA_ARG (ap, 0B, 0);
6303 return D.1844;
6304
6305 f2 (int i)
6306 struct ap[1];
6307 __builtin_va_start (&ap, 0);
6308 res = f2_1 (&ap);
6309 __builtin_va_end (&ap);
6310 D.1841 = res;
6311 return D.1841;
6312
6313 Case 2b is different because:
6314 - on the callee side, the parm decl has declared type va_list, but
6315 grokdeclarator changes the type of the parm decl to a pointer to the
6316 array elem type.
6317 - on the caller side, the pass-by-value uses &ap.
6318
6319 We unify these two cases (case 2a: va_list is array type,
6320 case 2b: va_list is pointer to array elem type), by adding '&' for the
6321 array type case, such that we have a pointer to array elem in both
6322 cases. */
6323
6324 if (TREE_CODE (va_type) == ARRAY_TYPE)
6325 {
6326 /* Case 2a: va_list is array type. */
6327
6328 /* Take the address, to get '&ap'. Make sure it's a pointer to array
6329 elem type. */
6330 mark_addressable (expr);
6331 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
6332 expr);
6333
6334 /* Verify that &ap is still recognized as having va_list type. */
6335 tree canon_expr_type
6336 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6337 gcc_assert (canon_expr_type != NULL_TREE);
6338 }
6339 else
6340 {
6341 /* Case 2b: va_list is pointer to array elem type. */
6342 gcc_assert (POINTER_TYPE_P (va_type));
6343 gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type));
6344
6345 /* Don't take the address. We've already got '&ap'. */
6346 ;
6347 }
6348
6349 return build_va_arg_1 (loc, type, expr);
a66c9326 6350}
0d4238dc 6351
6352
dd878098 6353/* Linked list of disabled built-in functions. */
6354
6dc50383 6355struct disabled_builtin
dd878098 6356{
6357 const char *name;
6358 struct disabled_builtin *next;
6dc50383 6359};
dd878098 6360static disabled_builtin *disabled_builtins = NULL;
6361
1cae46be 6362static bool builtin_function_disabled_p (const char *);
dd878098 6363
6364/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
6365 begins with "__builtin_", give an error. */
6366
6367void
1cae46be 6368disable_builtin_function (const char *name)
dd878098 6369{
6370 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
b0b1af64 6371 error ("cannot disable built-in function %qs", name);
dd878098 6372 else
6373 {
e85905e5 6374 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
6375 new_disabled_builtin->name = name;
6376 new_disabled_builtin->next = disabled_builtins;
6377 disabled_builtins = new_disabled_builtin;
dd878098 6378 }
6379}
6380
6381
6382/* Return true if the built-in function NAME has been disabled, false
6383 otherwise. */
6384
6385static bool
1cae46be 6386builtin_function_disabled_p (const char *name)
dd878098 6387{
6388 disabled_builtin *p;
6389 for (p = disabled_builtins; p != NULL; p = p->next)
6390 {
6391 if (strcmp (name, p->name) == 0)
6392 return true;
6393 }
6394 return false;
6395}
6396
6397
3237155d 6398/* Worker for DEF_BUILTIN.
6399 Possibly define a builtin function with one or two names.
6400 Does not declare a non-__builtin_ function if flag_no_builtin, or if
6401 nonansi_p and flag_no_nonansi_builtin. */
0d4238dc 6402
3237155d 6403static void
6404def_builtin_1 (enum built_in_function fncode,
6405 const char *name,
6406 enum built_in_class fnclass,
6407 tree fntype, tree libtype,
6408 bool both_p, bool fallback_p, bool nonansi_p,
6409 tree fnattrs, bool implicit_p)
0d4238dc 6410{
3237155d 6411 tree decl;
6412 const char *libname;
6413
27213ba3 6414 if (fntype == error_mark_node)
6415 return;
6416
3237155d 6417 gcc_assert ((!both_p && !fallback_p)
6418 || !strncmp (name, "__builtin_",
6419 strlen ("__builtin_")));
6420
6421 libname = name + strlen ("__builtin_");
54be5d7e 6422 decl = add_builtin_function (name, fntype, fncode, fnclass,
6423 (fallback_p ? libname : NULL),
6424 fnattrs);
b9a16870 6425
6426 set_builtin_decl (fncode, decl, implicit_p);
6427
3237155d 6428 if (both_p
6429 && !flag_no_builtin && !builtin_function_disabled_p (libname)
dd878098 6430 && !(nonansi_p && flag_no_nonansi_builtin))
54be5d7e 6431 add_builtin_function (libname, libtype, fncode, fnclass,
6432 NULL, fnattrs);
0d4238dc 6433}
e94026da 6434\f
d7aeef06 6435/* Nonzero if the type T promotes to int. This is (nearly) the
6436 integral promotions defined in ISO C99 6.3.1.1/2. */
6437
6438bool
9f627b1a 6439c_promoting_integer_type_p (const_tree t)
d7aeef06 6440{
6441 switch (TREE_CODE (t))
6442 {
6443 case INTEGER_TYPE:
6444 return (TYPE_MAIN_VARIANT (t) == char_type_node
6445 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
6446 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
6447 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
7aa1e6eb 6448 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
6449 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d7aeef06 6450
6451 case ENUMERAL_TYPE:
6452 /* ??? Technically all enumerations not larger than an int
6453 promote to an int. But this is used along code paths
6454 that only want to notice a size change. */
6455 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6456
6457 case BOOLEAN_TYPE:
6458 return 1;
6459
6460 default:
6461 return 0;
6462 }
6463}
6464
e94026da 6465/* Return 1 if PARMS specifies a fixed number of parameters
6466 and none of their types is affected by default promotions. */
6467
6468int
9f627b1a 6469self_promoting_args_p (const_tree parms)
e94026da 6470{
9f627b1a 6471 const_tree t;
e94026da 6472 for (t = parms; t; t = TREE_CHAIN (t))
6473 {
19cb6b50 6474 tree type = TREE_VALUE (t);
43f74bc4 6475
e1d8e198 6476 if (type == error_mark_node)
6477 continue;
6478
e94026da 6479 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6480 return 0;
6481
6482 if (type == 0)
6483 return 0;
6484
6485 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6486 return 0;
6487
d7aeef06 6488 if (c_promoting_integer_type_p (type))
e94026da 6489 return 0;
6490 }
6491 return 1;
6492}
605fb01e 6493
c10de5e7 6494/* Recursively remove any '*' or '&' operator from TYPE. */
6495tree
6496strip_pointer_operator (tree t)
6497{
6498 while (POINTER_TYPE_P (t))
6499 t = TREE_TYPE (t);
6500 return t;
6501}
6502
57a0ed23 6503/* Recursively remove pointer or array type from TYPE. */
6504tree
6505strip_pointer_or_array_types (tree t)
6506{
6507 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6508 t = TREE_TYPE (t);
6509 return t;
6510}
6511
e41f0d80 6512/* Used to compare case labels. K1 and K2 are actually tree nodes
6513 representing case labels, or NULL_TREE for a `default' label.
6514 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6515 K2, and 0 if K1 and K2 are equal. */
6516
6517int
1cae46be 6518case_compare (splay_tree_key k1, splay_tree_key k2)
e41f0d80 6519{
6520 /* Consider a NULL key (such as arises with a `default' label) to be
6521 smaller than anything else. */
6522 if (!k1)
6523 return k2 ? -1 : 0;
6524 else if (!k2)
6525 return k1 ? 1 : 0;
6526
6527 return tree_int_cst_compare ((tree) k1, (tree) k2);
6528}
6529
e60a6f7b 6530/* Process a case label, located at LOC, for the range LOW_VALUE
6531 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6532 then this case label is actually a `default' label. If only
6533 HIGH_VALUE is NULL_TREE, then case label was declared using the
6534 usual C/C++ syntax, rather than the GNU case range extension.
6535 CASES is a tree containing all the case ranges processed so far;
be23b16f 6536 COND is the condition for the switch-statement itself.
6537 OUTSIDE_RANGE_P says whether there was a case value that doesn't
6538 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
6539 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
e41f0d80 6540
6541tree
e60a6f7b 6542c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
be23b16f 6543 tree low_value, tree high_value, bool *outside_range_p)
e41f0d80 6544{
6545 tree type;
6546 tree label;
6547 tree case_label;
6548 splay_tree_node node;
6549
6550 /* Create the LABEL_DECL itself. */
e60a6f7b 6551 label = create_artificial_label (loc);
e41f0d80 6552
6553 /* If there was an error processing the switch condition, bail now
6554 before we get more confused. */
6555 if (!cond || cond == error_mark_node)
4ee9c684 6556 goto error_out;
e41f0d80 6557
1cae46be 6558 if ((low_value && TREE_TYPE (low_value)
6559 && POINTER_TYPE_P (TREE_TYPE (low_value)))
e41f0d80 6560 || (high_value && TREE_TYPE (high_value)
6561 && POINTER_TYPE_P (TREE_TYPE (high_value))))
b96dc121 6562 {
e60a6f7b 6563 error_at (loc, "pointers are not permitted as case values");
b96dc121 6564 goto error_out;
6565 }
e41f0d80 6566
6567 /* Case ranges are a GNU extension. */
8864917d 6568 if (high_value)
29438999 6569 pedwarn (loc, OPT_Wpedantic,
8864917d 6570 "range expressions in switch statements are non-standard");
e41f0d80 6571
6572 type = TREE_TYPE (cond);
6573 if (low_value)
6574 {
2d2f6a15 6575 low_value = check_case_value (loc, low_value);
22a75734 6576 low_value = convert_and_check (loc, type, low_value);
96722196 6577 if (low_value == error_mark_node)
6578 goto error_out;
e41f0d80 6579 }
6580 if (high_value)
6581 {
2d2f6a15 6582 high_value = check_case_value (loc, high_value);
22a75734 6583 high_value = convert_and_check (loc, type, high_value);
96722196 6584 if (high_value == error_mark_node)
6585 goto error_out;
e41f0d80 6586 }
6587
96722196 6588 if (low_value && high_value)
6589 {
6590 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
a0c938f0 6591 really a case range, even though it was written that way.
6592 Remove the HIGH_VALUE to simplify later processing. */
96722196 6593 if (tree_int_cst_equal (low_value, high_value))
6594 high_value = NULL_TREE;
6595 else if (!tree_int_cst_lt (low_value, high_value))
e60a6f7b 6596 warning_at (loc, 0, "empty range specified");
96722196 6597 }
e41f0d80 6598
2ca392fd 6599 /* See if the case is in range of the type of the original testing
6600 expression. If both low_value and high_value are out of range,
6601 don't insert the case label and return NULL_TREE. */
6602 if (low_value
f61a9bc2 6603 && !check_case_bounds (loc, type, orig_type,
be23b16f 6604 &low_value, high_value ? &high_value : NULL,
6605 outside_range_p))
2ca392fd 6606 return NULL_TREE;
6607
e41f0d80 6608 /* Look up the LOW_VALUE in the table of case labels we already
6609 have. */
6610 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6611 /* If there was not an exact match, check for overlapping ranges.
6612 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6613 that's a `default' label and the only overlap is an exact match. */
6614 if (!node && (low_value || high_value))
6615 {
6616 splay_tree_node low_bound;
6617 splay_tree_node high_bound;
6618
6619 /* Even though there wasn't an exact match, there might be an
6620 overlap between this case range and another case range.
6621 Since we've (inductively) not allowed any overlapping case
6622 ranges, we simply need to find the greatest low case label
6623 that is smaller that LOW_VALUE, and the smallest low case
6624 label that is greater than LOW_VALUE. If there is an overlap
6625 it will occur in one of these two ranges. */
6626 low_bound = splay_tree_predecessor (cases,
6627 (splay_tree_key) low_value);
6628 high_bound = splay_tree_successor (cases,
6629 (splay_tree_key) low_value);
6630
6631 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6632 the LOW_VALUE, so there is no need to check unless the
6633 LOW_BOUND is in fact itself a case range. */
6634 if (low_bound
6635 && CASE_HIGH ((tree) low_bound->value)
6636 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6637 low_value) >= 0)
6638 node = low_bound;
6639 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6640 range is bigger than the low end of the current range, so we
6641 are only interested if the current range is a real range, and
6642 not an ordinary case label. */
1cae46be 6643 else if (high_bound
e41f0d80 6644 && high_value
6645 && (tree_int_cst_compare ((tree) high_bound->key,
6646 high_value)
6647 <= 0))
6648 node = high_bound;
6649 }
6650 /* If there was an overlap, issue an error. */
6651 if (node)
6652 {
eaae3b75 6653 tree duplicate = CASE_LABEL ((tree) node->value);
e41f0d80 6654
6655 if (high_value)
6656 {
e60a6f7b 6657 error_at (loc, "duplicate (or overlapping) case value");
6658 error_at (DECL_SOURCE_LOCATION (duplicate),
6659 "this is the first entry overlapping that value");
e41f0d80 6660 }
6661 else if (low_value)
6662 {
e60a6f7b 6663 error_at (loc, "duplicate case value") ;
6664 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
e41f0d80 6665 }
6666 else
6667 {
e60a6f7b 6668 error_at (loc, "multiple default labels in one switch");
6669 error_at (DECL_SOURCE_LOCATION (duplicate),
6670 "this is the first default label");
e41f0d80 6671 }
4ee9c684 6672 goto error_out;
e41f0d80 6673 }
6674
6675 /* Add a CASE_LABEL to the statement-tree. */
b6e3dd65 6676 case_label = add_stmt (build_case_label (low_value, high_value, label));
e41f0d80 6677 /* Register this case label in the splay tree. */
1cae46be 6678 splay_tree_insert (cases,
e41f0d80 6679 (splay_tree_key) low_value,
6680 (splay_tree_value) case_label);
6681
6682 return case_label;
4ee9c684 6683
6684 error_out:
daf6dff5 6685 /* Add a label so that the back-end doesn't think that the beginning of
4ee9c684 6686 the switch is unreachable. Note that we do not add a case label, as
a53ff4c1 6687 that just leads to duplicates and thence to failure later on. */
4ee9c684 6688 if (!cases->root)
6689 {
e60a6f7b 6690 tree t = create_artificial_label (loc);
6691 add_stmt (build_stmt (loc, LABEL_EXPR, t));
4ee9c684 6692 }
6693 return error_mark_node;
6694}
6695
6696/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6697 Used to verify that case values match up with enumerator values. */
6698
6699static void
6700match_case_to_enum_1 (tree key, tree type, tree label)
6701{
e913b5cd 6702 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6703
dd76621f 6704 if (tree_fits_uhwi_p (key))
6705 print_dec (key, buf, UNSIGNED);
6706 else if (tree_fits_shwi_p (key))
6707 print_dec (key, buf, SIGNED);
4ee9c684 6708 else
e913b5cd 6709 print_hex (key, buf);
4ee9c684 6710
6711 if (TYPE_NAME (type) == 0)
712d2297 6712 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6713 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6714 "case value %qs not in enumerated type",
6715 buf);
4ee9c684 6716 else
712d2297 6717 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6718 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6719 "case value %qs not in enumerated type %qT",
6720 buf, type);
4ee9c684 6721}
6722
359d87c6 6723/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6724 Used to verify that case values match up with enumerator values. */
6725
4ee9c684 6726static int
6727match_case_to_enum (splay_tree_node node, void *data)
6728{
6729 tree label = (tree) node->value;
4fd61bc6 6730 tree type = (tree) data;
4ee9c684 6731
6732 /* Skip default case. */
6733 if (!CASE_LOW (label))
6734 return 0;
6735
359d87c6 6736 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
4ee9c684 6737 when we did our enum->case scan. Reset our scratch bit after. */
359d87c6 6738 if (!CASE_LOW_SEEN (label))
4ee9c684 6739 match_case_to_enum_1 (CASE_LOW (label), type, label);
6740 else
359d87c6 6741 CASE_LOW_SEEN (label) = 0;
4ee9c684 6742
359d87c6 6743 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6744 not set, that means that CASE_HIGH did not appear when we did our
6745 enum->case scan. Reset our scratch bit after. */
4ee9c684 6746 if (CASE_HIGH (label))
6747 {
359d87c6 6748 if (!CASE_HIGH_SEEN (label))
6749 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6750 else
6751 CASE_HIGH_SEEN (label) = 0;
4ee9c684 6752 }
6753
6754 return 0;
6755}
6756
e7911019 6757/* Handle -Wswitch*. Called from the front end after parsing the
6758 switch construct. */
6759/* ??? Should probably be somewhere generic, since other languages
6760 besides C and C++ would want this. At the moment, however, C/C++
6761 are the only tree-ssa languages that support enumerations at all,
6762 so the point is moot. */
4ee9c684 6763
e7911019 6764void
6765c_do_switch_warnings (splay_tree cases, location_t switch_location,
be23b16f 6766 tree type, tree cond, bool bool_cond_p,
6767 bool outside_range_p)
4ee9c684 6768{
b27ac6b5 6769 splay_tree_node default_node;
359d87c6 6770 splay_tree_node node;
6771 tree chain;
4ee9c684 6772
be23b16f 6773 if (!warn_switch && !warn_switch_enum && !warn_switch_default
6774 && !warn_switch_bool)
4ee9c684 6775 return;
6776
4ee9c684 6777 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
8b6866af 6778 if (!default_node)
5fb6a912 6779 warning_at (switch_location, OPT_Wswitch_default,
6780 "switch missing default case");
4ee9c684 6781
be23b16f 6782 /* There are certain cases where -Wswitch-bool warnings aren't
6783 desirable, such as
6784 switch (boolean)
6785 {
6786 case true: ...
6787 case false: ...
6788 }
6789 so be careful here. */
6790 if (warn_switch_bool && bool_cond_p)
6791 {
6792 splay_tree_node min_node;
6793 /* If there's a default node, it's also the value with the minimal
6794 key. So look at the penultimate key (if any). */
6795 if (default_node)
6796 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
6797 else
6798 min_node = splay_tree_min (cases);
6799 tree min = min_node ? (tree) min_node->key : NULL_TREE;
6800
6801 splay_tree_node max_node = splay_tree_max (cases);
6802 /* This might be a case range, so look at the value with the
6803 maximal key and then check CASE_HIGH. */
6804 tree max = max_node ? (tree) max_node->value : NULL_TREE;
6805 if (max)
6806 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
6807
6808 /* If there's a case value > 1 or < 0, that is outside bool
6809 range, warn. */
6810 if (outside_range_p
6811 || (max && wi::gts_p (max, 1))
6812 || (min && wi::lts_p (min, 0))
6813 /* And handle the
6814 switch (boolean)
6815 {
6816 case true: ...
6817 case false: ...
6818 default: ...
6819 }
6820 case, where we want to warn. */
6821 || (default_node
6822 && max && wi::eq_p (max, 1)
6823 && min && wi::eq_p (min, 0)))
6824 warning_at (switch_location, OPT_Wswitch_bool,
6825 "switch condition has boolean value");
6826 }
6827
47ae02b7 6828 /* From here on, we only care about enumerated types. */
359d87c6 6829 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6830 return;
6831
561017b5 6832 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6833 if (!warn_switch_enum && !warn_switch)
359d87c6 6834 return;
6835
561017b5 6836 /* Check the cases. Warn about case values which are not members of
6837 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6838 there is no default case, check that exactly all enumeration
6839 literals are covered by the cases. */
6840
359d87c6 6841 /* Clearing COND if it is not an integer constant simplifies
6842 the tests inside the loop below. */
6843 if (TREE_CODE (cond) != INTEGER_CST)
6844 cond = NULL_TREE;
6845
6846 /* The time complexity here is O(N*lg(N)) worst case, but for the
6847 common case of monotonically increasing enumerators, it is
6848 O(N), since the nature of the splay tree will keep the next
6849 element adjacent to the root at all times. */
4ee9c684 6850
359d87c6 6851 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6852 {
6853 tree value = TREE_VALUE (chain);
3f00a6c0 6854 if (TREE_CODE (value) == CONST_DECL)
6855 value = DECL_INITIAL (value);
359d87c6 6856 node = splay_tree_lookup (cases, (splay_tree_key) value);
6857 if (node)
4ee9c684 6858 {
359d87c6 6859 /* Mark the CASE_LOW part of the case entry as seen. */
6860 tree label = (tree) node->value;
6861 CASE_LOW_SEEN (label) = 1;
6862 continue;
6863 }
6864
6865 /* Even though there wasn't an exact match, there might be a
f0b5f617 6866 case range which includes the enumerator's value. */
359d87c6 6867 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6868 if (node && CASE_HIGH ((tree) node->value))
6869 {
6870 tree label = (tree) node->value;
6871 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6872 if (cmp >= 0)
4ee9c684 6873 {
359d87c6 6874 /* If we match the upper bound exactly, mark the CASE_HIGH
6875 part of the case entry as seen. */
6876 if (cmp == 0)
6877 CASE_HIGH_SEEN (label) = 1;
6878 continue;
4ee9c684 6879 }
6880 }
6881
359d87c6 6882 /* We've now determined that this enumerated literal isn't
6883 handled by the case labels of the switch statement. */
4ee9c684 6884
359d87c6 6885 /* If the switch expression is a constant, we only really care
6886 about whether that constant is handled by the switch. */
6887 if (cond && tree_int_cst_compare (cond, value))
6888 continue;
4ee9c684 6889
6cbbbc89 6890 /* If there is a default_node, the only relevant option is
561017b5 6891 Wswitch-enum. Otherwise, if both are enabled then we prefer
6cbbbc89 6892 to warn using -Wswitch because -Wswitch is enabled by -Wall
6893 while -Wswitch-enum is explicit. */
561017b5 6894 warning_at (switch_location,
6895 (default_node || !warn_switch
6896 ? OPT_Wswitch_enum
6897 : OPT_Wswitch),
6898 "enumeration value %qE not handled in switch",
6899 TREE_PURPOSE (chain));
4ee9c684 6900 }
359d87c6 6901
6902 /* Warn if there are case expressions that don't correspond to
6903 enumerators. This can occur since C and C++ don't enforce
6904 type-checking of assignments to enumeration variables.
6905
6906 The time complexity here is now always O(N) worst case, since
6907 we should have marked both the lower bound and upper bound of
6908 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6909 above. This scan also resets those fields. */
6cbbbc89 6910
359d87c6 6911 splay_tree_foreach (cases, match_case_to_enum, type);
e41f0d80 6912}
6913
9dd48740 6914/* Finish an expression taking the address of LABEL (an
dda49785 6915 IDENTIFIER_NODE). Returns an expression for the address.
6916
6917 LOC is the location for the expression returned. */
d0a47c8d 6918
1cae46be 6919tree
dda49785 6920finish_label_address_expr (tree label, location_t loc)
d0a47c8d 6921{
6922 tree result;
6923
29438999 6924 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
d0a47c8d 6925
9dd48740 6926 if (label == error_mark_node)
6927 return error_mark_node;
6928
d0a47c8d 6929 label = lookup_label (label);
6930 if (label == NULL_TREE)
6931 result = null_pointer_node;
6932 else
6933 {
6934 TREE_USED (label) = 1;
6935 result = build1 (ADDR_EXPR, ptr_type_node, label);
344cd9b2 6936 /* The current function is not necessarily uninlinable.
d0a47c8d 6937 Computed gotos are incompatible with inlining, but the value
6938 here could be used only in a diagnostic, for example. */
dda49785 6939 protected_set_expr_location (result, loc);
d0a47c8d 6940 }
6941
6942 return result;
6943}
4f9a1c9b 6944\f
6945
6946/* Given a boolean expression ARG, return a tree representing an increment
6947 or decrement (as indicated by CODE) of ARG. The front end must check for
6948 invalid cases (e.g., decrement in C++). */
6949tree
1cae46be 6950boolean_increment (enum tree_code code, tree arg)
4f9a1c9b 6951{
6952 tree val;
69db191c 6953 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
c0f19401 6954
4f9a1c9b 6955 arg = stabilize_reference (arg);
6956 switch (code)
6957 {
6958 case PREINCREMENT_EXPR:
14ae0310 6959 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6960 break;
6961 case POSTINCREMENT_EXPR:
14ae0310 6962 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6963 arg = save_expr (arg);
14ae0310 6964 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6965 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6966 break;
6967 case PREDECREMENT_EXPR:
14ae0310 6968 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6969 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6970 break;
6971 case POSTDECREMENT_EXPR:
14ae0310 6972 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6973 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6974 arg = save_expr (arg);
14ae0310 6975 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6976 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6977 break;
6978 default:
231bd014 6979 gcc_unreachable ();
4f9a1c9b 6980 }
6981 TREE_SIDE_EFFECTS (val) = 1;
6982 return val;
6983}
76a6e674 6984\f
f3449a3c 6985/* Built-in macros for stddef.h and stdint.h, that require macros
6986 defined in this file. */
79cf3ec1 6987void
1cae46be 6988c_stddef_cpp_builtins(void)
1ed9d5f5 6989{
63994318 6990 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6991 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6992 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6993 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
36bccbfc 6994 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6995 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
b0726616 6996 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6997 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
f3449a3c 6998 if (SIG_ATOMIC_TYPE)
6999 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
7000 if (INT8_TYPE)
7001 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
7002 if (INT16_TYPE)
7003 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
7004 if (INT32_TYPE)
7005 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
7006 if (INT64_TYPE)
7007 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
7008 if (UINT8_TYPE)
7009 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
7010 if (UINT16_TYPE)
7011 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
7012 if (UINT32_TYPE)
7013 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
7014 if (UINT64_TYPE)
7015 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
7016 if (INT_LEAST8_TYPE)
7017 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
7018 if (INT_LEAST16_TYPE)
7019 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
7020 if (INT_LEAST32_TYPE)
7021 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
7022 if (INT_LEAST64_TYPE)
7023 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
7024 if (UINT_LEAST8_TYPE)
7025 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
7026 if (UINT_LEAST16_TYPE)
7027 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
7028 if (UINT_LEAST32_TYPE)
7029 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
7030 if (UINT_LEAST64_TYPE)
7031 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
7032 if (INT_FAST8_TYPE)
7033 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
7034 if (INT_FAST16_TYPE)
7035 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
7036 if (INT_FAST32_TYPE)
7037 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
7038 if (INT_FAST64_TYPE)
7039 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
7040 if (UINT_FAST8_TYPE)
7041 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
7042 if (UINT_FAST16_TYPE)
7043 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
7044 if (UINT_FAST32_TYPE)
7045 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
7046 if (UINT_FAST64_TYPE)
7047 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
7048 if (INTPTR_TYPE)
7049 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
7050 if (UINTPTR_TYPE)
7051 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
574006c3 7052}
7053
7d3b509a 7054static void
1cae46be 7055c_init_attributes (void)
7d3b509a 7056{
7057 /* Fill in the built_in_attributes array. */
7c446c95 7058#define DEF_ATTR_NULL_TREE(ENUM) \
7d3b509a 7059 built_in_attributes[(int) ENUM] = NULL_TREE;
7c446c95 7060#define DEF_ATTR_INT(ENUM, VALUE) \
ceb7b692 7061 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
c8010b80 7062#define DEF_ATTR_STRING(ENUM, VALUE) \
7063 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
7d3b509a 7064#define DEF_ATTR_IDENT(ENUM, STRING) \
7065 built_in_attributes[(int) ENUM] = get_identifier (STRING);
7066#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
7067 built_in_attributes[(int) ENUM] \
7068 = tree_cons (built_in_attributes[(int) PURPOSE], \
7069 built_in_attributes[(int) VALUE], \
7070 built_in_attributes[(int) CHAIN]);
7d3b509a 7071#include "builtin-attrs.def"
7072#undef DEF_ATTR_NULL_TREE
7073#undef DEF_ATTR_INT
7074#undef DEF_ATTR_IDENT
7075#undef DEF_ATTR_TREE_LIST
76a6e674 7076}
5f3cead1 7077
33199a81 7078/* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
7079 identifier as an argument, so the front end shouldn't look it up. */
7080
7081bool
47b19b94 7082attribute_takes_identifier_p (const_tree attr_id)
33199a81 7083{
9bf1c74e 7084 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
02cb1060 7085 if (spec == NULL)
7086 /* Unknown attribute that we'll end up ignoring, return true so we
7087 don't complain about an identifier argument. */
7088 return true;
7089 else if (!strcmp ("mode", spec->name)
7090 || !strcmp ("format", spec->name)
7091 || !strcmp ("cleanup", spec->name))
47b19b94 7092 return true;
7093 else
7094 return targetm.attribute_takes_identifier_p (attr_id);
33199a81 7095}
7096
f8e93a2e 7097/* Attribute handlers common to C front ends. */
7098
7099/* Handle a "packed" attribute; arguments as in
7100 struct attribute_spec.handler. */
7101
7102static tree
9a03a746 7103handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
09347743 7104 int flags, bool *no_add_attrs)
f8e93a2e 7105{
f40175cb 7106 if (TYPE_P (*node))
f8e93a2e 7107 {
7108 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 7109 *node = build_variant_type_copy (*node);
f40175cb 7110 TYPE_PACKED (*node) = 1;
f8e93a2e 7111 }
7112 else if (TREE_CODE (*node) == FIELD_DECL)
c2ab04f9 7113 {
9fd767c5 7114 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
7115 /* Still pack bitfields. */
7116 && ! DECL_INITIAL (*node))
c2ab04f9 7117 warning (OPT_Wattributes,
7118 "%qE attribute ignored for field of type %qT",
7119 name, TREE_TYPE (*node));
7120 else
7121 DECL_PACKED (*node) = 1;
7122 }
f8e93a2e 7123 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
f40175cb 7124 used for DECL_REGISTER. It wouldn't mean anything anyway.
7125 We can't set DECL_PACKED on the type of a TYPE_DECL, because
7126 that changes what the typedef is typing. */
f8e93a2e 7127 else
7128 {
9b2d6d13 7129 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7130 *no_add_attrs = true;
7131 }
7132
7133 return NULL_TREE;
7134}
7135
7136/* Handle a "nocommon" attribute; arguments as in
7137 struct attribute_spec.handler. */
7138
7139static tree
1cae46be 7140handle_nocommon_attribute (tree *node, tree name,
9a03a746 7141 tree ARG_UNUSED (args),
7142 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7143{
f48c7f4a 7144 if (VAR_P (*node))
f8e93a2e 7145 DECL_COMMON (*node) = 0;
7146 else
7147 {
9b2d6d13 7148 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7149 *no_add_attrs = true;
7150 }
7151
7152 return NULL_TREE;
7153}
7154
7155/* Handle a "common" attribute; arguments as in
7156 struct attribute_spec.handler. */
7157
7158static tree
9a03a746 7159handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7160 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7161{
f48c7f4a 7162 if (VAR_P (*node))
f8e93a2e 7163 DECL_COMMON (*node) = 1;
7164 else
7165 {
9b2d6d13 7166 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7167 *no_add_attrs = true;
7168 }
7169
7170 return NULL_TREE;
7171}
7172
7173/* Handle a "noreturn" attribute; arguments as in
7174 struct attribute_spec.handler. */
7175
7176static tree
9a03a746 7177handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7178 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7179{
7180 tree type = TREE_TYPE (*node);
7181
7182 /* See FIXME comment in c_common_attribute_table. */
8c582e4f 7183 if (TREE_CODE (*node) == FUNCTION_DECL
7184 || objc_method_decl (TREE_CODE (*node)))
f8e93a2e 7185 TREE_THIS_VOLATILE (*node) = 1;
7186 else if (TREE_CODE (type) == POINTER_TYPE
7187 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7188 TREE_TYPE (*node)
9d4eeb52 7189 = (build_qualified_type
7190 (build_pointer_type
7191 (build_type_variant (TREE_TYPE (type),
7192 TYPE_READONLY (TREE_TYPE (type)), 1)),
7193 TYPE_QUALS (type)));
f8e93a2e 7194 else
7195 {
9b2d6d13 7196 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7197 *no_add_attrs = true;
7198 }
7199
7200 return NULL_TREE;
7201}
7202
5de92639 7203/* Handle a "hot" and attribute; arguments as in
7204 struct attribute_spec.handler. */
7205
7206static tree
7207handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
46f8e3b0 7208 int ARG_UNUSED (flags), bool *no_add_attrs)
5de92639 7209{
758a38ab 7210 if (TREE_CODE (*node) == FUNCTION_DECL
7211 || TREE_CODE (*node) == LABEL_DECL)
5de92639 7212 {
7213 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
7214 {
4a026b48 7215 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7216 "with attribute %qs", name, "cold");
5de92639 7217 *no_add_attrs = true;
7218 }
24470055 7219 /* Most of the rest of the hot processing is done later with
7220 lookup_attribute. */
5de92639 7221 }
7222 else
7223 {
7224 warning (OPT_Wattributes, "%qE attribute ignored", name);
7225 *no_add_attrs = true;
7226 }
7227
7228 return NULL_TREE;
7229}
758a38ab 7230
5de92639 7231/* Handle a "cold" and attribute; arguments as in
7232 struct attribute_spec.handler. */
7233
7234static tree
7235handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7236 int ARG_UNUSED (flags), bool *no_add_attrs)
7237{
758a38ab 7238 if (TREE_CODE (*node) == FUNCTION_DECL
7239 || TREE_CODE (*node) == LABEL_DECL)
5de92639 7240 {
7241 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
7242 {
4a026b48 7243 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7244 "with attribute %qs", name, "hot");
5de92639 7245 *no_add_attrs = true;
7246 }
24470055 7247 /* Most of the rest of the cold processing is done later with
7248 lookup_attribute. */
5de92639 7249 }
7250 else
7251 {
7252 warning (OPT_Wattributes, "%qE attribute ignored", name);
7253 *no_add_attrs = true;
7254 }
7255
7256 return NULL_TREE;
7257}
7258
a9196da9 7259/* Handle a "no_sanitize_address" attribute; arguments as in
d413ffdd 7260 struct attribute_spec.handler. */
7261
7262static tree
a9196da9 7263handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
7264 bool *no_add_attrs)
d413ffdd 7265{
7266 if (TREE_CODE (*node) != FUNCTION_DECL)
7267 {
7268 warning (OPT_Wattributes, "%qE attribute ignored", name);
7269 *no_add_attrs = true;
7270 }
7271
7272 return NULL_TREE;
7273}
7274
a9196da9 7275/* Handle a "no_address_safety_analysis" attribute; arguments as in
7276 struct attribute_spec.handler. */
7277
7278static tree
7279handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
7280 bool *no_add_attrs)
7281{
7282 if (TREE_CODE (*node) != FUNCTION_DECL)
7283 warning (OPT_Wattributes, "%qE attribute ignored", name);
7284 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
7285 DECL_ATTRIBUTES (*node)
7286 = tree_cons (get_identifier ("no_sanitize_address"),
7287 NULL_TREE, DECL_ATTRIBUTES (*node));
7288 *no_add_attrs = true;
7289 return NULL_TREE;
7290}
7291
05f893e1 7292/* Handle a "no_sanitize_undefined" attribute; arguments as in
7293 struct attribute_spec.handler. */
7294
7295static tree
7296handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
7297 bool *no_add_attrs)
7298{
7299 if (TREE_CODE (*node) != FUNCTION_DECL)
7300 {
7301 warning (OPT_Wattributes, "%qE attribute ignored", name);
7302 *no_add_attrs = true;
7303 }
7304
7305 return NULL_TREE;
7306}
7307
947aa916 7308/* Handle a "stack_protect" attribute; arguments as in
7309 struct attribute_spec.handler. */
7310static tree
7311handle_stack_protect_attribute (tree *node, tree name, tree, int,
7312 bool *no_add_attrs)
7313{
7314 if (TREE_CODE (*node) != FUNCTION_DECL)
7315 {
7316 warning (OPT_Wattributes, "%qE attribute ignored", name);
7317 *no_add_attrs = true;
7318 }
7319 else
7320 DECL_ATTRIBUTES (*node)
7321 = tree_cons (get_identifier ("stack_protect"),
7322 NULL_TREE, DECL_ATTRIBUTES (*node));
7323
7324 return NULL_TREE;
7325}
7326
f8e93a2e 7327/* Handle a "noinline" attribute; arguments as in
7328 struct attribute_spec.handler. */
7329
7330static tree
1cae46be 7331handle_noinline_attribute (tree *node, tree name,
9a03a746 7332 tree ARG_UNUSED (args),
7333 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7334{
7335 if (TREE_CODE (*node) == FUNCTION_DECL)
4a026b48 7336 {
7337 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
7338 {
7339 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7340 "with attribute %qs", name, "always_inline");
7341 *no_add_attrs = true;
7342 }
7343 else
7344 DECL_UNINLINABLE (*node) = 1;
7345 }
f8e93a2e 7346 else
7347 {
9b2d6d13 7348 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7349 *no_add_attrs = true;
7350 }
7351
7352 return NULL_TREE;
7353}
7354
bdb1f0d1 7355/* Handle a "noclone" attribute; arguments as in
7356 struct attribute_spec.handler. */
7357
7358static tree
7359handle_noclone_attribute (tree *node, tree name,
7360 tree ARG_UNUSED (args),
7361 int ARG_UNUSED (flags), bool *no_add_attrs)
7362{
7363 if (TREE_CODE (*node) != FUNCTION_DECL)
7364 {
7365 warning (OPT_Wattributes, "%qE attribute ignored", name);
7366 *no_add_attrs = true;
7367 }
7368
7369 return NULL_TREE;
7370}
7371
85fbea97 7372/* Handle a "no_icf" attribute; arguments as in
7373 struct attribute_spec.handler. */
7374
7375static tree
7376handle_noicf_attribute (tree *node, tree name,
7377 tree ARG_UNUSED (args),
7378 int ARG_UNUSED (flags), bool *no_add_attrs)
7379{
7380 if (TREE_CODE (*node) != FUNCTION_DECL)
7381 {
7382 warning (OPT_Wattributes, "%qE attribute ignored", name);
7383 *no_add_attrs = true;
7384 }
7385
7386 return NULL_TREE;
7387}
7388
7389
f8e93a2e 7390/* Handle a "always_inline" attribute; arguments as in
7391 struct attribute_spec.handler. */
7392
7393static tree
1cae46be 7394handle_always_inline_attribute (tree *node, tree name,
9a03a746 7395 tree ARG_UNUSED (args),
7396 int ARG_UNUSED (flags),
09347743 7397 bool *no_add_attrs)
f8e93a2e 7398{
7399 if (TREE_CODE (*node) == FUNCTION_DECL)
7400 {
4a026b48 7401 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
7402 {
7403 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7404 "with %qs attribute", name, "noinline");
7405 *no_add_attrs = true;
7406 }
ab50af2a 7407 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
7408 {
7409 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7410 "with %qs attribute", name, "target_clones");
7411 *no_add_attrs = true;
7412 }
4a026b48 7413 else
7414 /* Set the attribute and mark it for disregarding inline
7415 limits. */
7416 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
f8e93a2e 7417 }
7418 else
7419 {
9b2d6d13 7420 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7421 *no_add_attrs = true;
541e4101 7422 }
7423
7424 return NULL_TREE;
7425}
7426
7427/* Handle a "gnu_inline" attribute; arguments as in
7428 struct attribute_spec.handler. */
7429
7430static tree
7431handle_gnu_inline_attribute (tree *node, tree name,
7432 tree ARG_UNUSED (args),
7433 int ARG_UNUSED (flags),
7434 bool *no_add_attrs)
7435{
7436 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7437 {
7438 /* Do nothing else, just set the attribute. We'll get at
7439 it later with lookup_attribute. */
7440 }
7441 else
7442 {
7443 warning (OPT_Wattributes, "%qE attribute ignored", name);
7444 *no_add_attrs = true;
7bd95dfd 7445 }
7446
7447 return NULL_TREE;
7448}
7449
7450/* Handle a "leaf" attribute; arguments as in
7451 struct attribute_spec.handler. */
7452
7453static tree
7454handle_leaf_attribute (tree *node, tree name,
7455 tree ARG_UNUSED (args),
7456 int ARG_UNUSED (flags), bool *no_add_attrs)
7457{
7458 if (TREE_CODE (*node) != FUNCTION_DECL)
7459 {
7460 warning (OPT_Wattributes, "%qE attribute ignored", name);
7461 *no_add_attrs = true;
7462 }
7463 if (!TREE_PUBLIC (*node))
7464 {
7465 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
7466 *no_add_attrs = true;
f8e93a2e 7467 }
7468
7469 return NULL_TREE;
7470}
7471
1b16fc45 7472/* Handle an "artificial" attribute; arguments as in
7473 struct attribute_spec.handler. */
7474
7475static tree
7476handle_artificial_attribute (tree *node, tree name,
7477 tree ARG_UNUSED (args),
7478 int ARG_UNUSED (flags),
7479 bool *no_add_attrs)
7480{
7481 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7482 {
7483 /* Do nothing else, just set the attribute. We'll get at
7484 it later with lookup_attribute. */
7485 }
7486 else
7487 {
7488 warning (OPT_Wattributes, "%qE attribute ignored", name);
7489 *no_add_attrs = true;
7490 }
7491
7492 return NULL_TREE;
7493}
7494
0cdd9887 7495/* Handle a "flatten" attribute; arguments as in
7496 struct attribute_spec.handler. */
7497
7498static tree
7499handle_flatten_attribute (tree *node, tree name,
a0c938f0 7500 tree args ATTRIBUTE_UNUSED,
7501 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
0cdd9887 7502{
7503 if (TREE_CODE (*node) == FUNCTION_DECL)
7504 /* Do nothing else, just set the attribute. We'll get at
7505 it later with lookup_attribute. */
7506 ;
7507 else
7508 {
7509 warning (OPT_Wattributes, "%qE attribute ignored", name);
7510 *no_add_attrs = true;
7511 }
7512
7513 return NULL_TREE;
7514}
7515
10fc867f 7516/* Handle a "warning" or "error" attribute; arguments as in
7517 struct attribute_spec.handler. */
7518
7519static tree
7520handle_error_attribute (tree *node, tree name, tree args,
7521 int ARG_UNUSED (flags), bool *no_add_attrs)
7522{
7523 if (TREE_CODE (*node) == FUNCTION_DECL
df936998 7524 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
10fc867f 7525 /* Do nothing else, just set the attribute. We'll get at
7526 it later with lookup_attribute. */
7527 ;
7528 else
7529 {
7530 warning (OPT_Wattributes, "%qE attribute ignored", name);
7531 *no_add_attrs = true;
7532 }
7533
7534 return NULL_TREE;
7535}
0cdd9887 7536
f8e93a2e 7537/* Handle a "used" attribute; arguments as in
7538 struct attribute_spec.handler. */
7539
7540static tree
9a03a746 7541handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7542 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7543{
d0a31bd8 7544 tree node = *pnode;
7545
7546 if (TREE_CODE (node) == FUNCTION_DECL
f48c7f4a 7547 || (VAR_P (node) && TREE_STATIC (node))
a4e3ffad 7548 || (TREE_CODE (node) == TYPE_DECL))
f54ed8bc 7549 {
f54ed8bc 7550 TREE_USED (node) = 1;
9423c9b7 7551 DECL_PRESERVE_P (node) = 1;
f48c7f4a 7552 if (VAR_P (node))
abc6c64f 7553 DECL_READ_P (node) = 1;
f54ed8bc 7554 }
f8e93a2e 7555 else
7556 {
9b2d6d13 7557 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7558 *no_add_attrs = true;
7559 }
7560
7561 return NULL_TREE;
7562}
7563
7564/* Handle a "unused" attribute; arguments as in
7565 struct attribute_spec.handler. */
7566
7567static tree
9a03a746 7568handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7569 int flags, bool *no_add_attrs)
f8e93a2e 7570{
7571 if (DECL_P (*node))
7572 {
7573 tree decl = *node;
7574
7575 if (TREE_CODE (decl) == PARM_DECL
4e81b384 7576 || VAR_OR_FUNCTION_DECL_P (decl)
f8e93a2e 7577 || TREE_CODE (decl) == LABEL_DECL
7578 || TREE_CODE (decl) == TYPE_DECL)
abc6c64f 7579 {
7580 TREE_USED (decl) = 1;
f48c7f4a 7581 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
abc6c64f 7582 DECL_READ_P (decl) = 1;
7583 }
f8e93a2e 7584 else
7585 {
9b2d6d13 7586 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7587 *no_add_attrs = true;
7588 }
7589 }
7590 else
7591 {
7592 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 7593 *node = build_variant_type_copy (*node);
f8e93a2e 7594 TREE_USED (*node) = 1;
7595 }
7596
7597 return NULL_TREE;
7598}
7599
62eec3b4 7600/* Handle a "externally_visible" attribute; arguments as in
7601 struct attribute_spec.handler. */
7602
7603static tree
7604handle_externally_visible_attribute (tree *pnode, tree name,
7605 tree ARG_UNUSED (args),
7606 int ARG_UNUSED (flags),
7607 bool *no_add_attrs)
7608{
7609 tree node = *pnode;
7610
b443c459 7611 if (VAR_OR_FUNCTION_DECL_P (node))
62eec3b4 7612 {
ba12ea31 7613 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7614 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7615 {
7616 warning (OPT_Wattributes,
7617 "%qE attribute have effect only on public objects", name);
7618 *no_add_attrs = true;
7619 }
62eec3b4 7620 }
62eec3b4 7621 else
7622 {
7623 warning (OPT_Wattributes, "%qE attribute ignored", name);
7624 *no_add_attrs = true;
7625 }
7626
7627 return NULL_TREE;
7628}
7629
6b722052 7630/* Handle the "no_reorder" attribute. Arguments as in
7631 struct attribute_spec.handler. */
7632
7633static tree
7634handle_no_reorder_attribute (tree *pnode,
7635 tree name,
7636 tree,
7637 int,
7638 bool *no_add_attrs)
7639{
7640 tree node = *pnode;
7641
b443c459 7642 if (!VAR_OR_FUNCTION_DECL_P (node)
6b722052 7643 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7644 {
7645 warning (OPT_Wattributes,
7646 "%qE attribute only affects top level objects",
7647 name);
7648 *no_add_attrs = true;
7649 }
7650
7651 return NULL_TREE;
7652}
7653
f8e93a2e 7654/* Handle a "const" attribute; arguments as in
7655 struct attribute_spec.handler. */
7656
7657static tree
9a03a746 7658handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7659 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7660{
7661 tree type = TREE_TYPE (*node);
7662
7663 /* See FIXME comment on noreturn in c_common_attribute_table. */
7664 if (TREE_CODE (*node) == FUNCTION_DECL)
7665 TREE_READONLY (*node) = 1;
7666 else if (TREE_CODE (type) == POINTER_TYPE
7667 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7668 TREE_TYPE (*node)
9d4eeb52 7669 = (build_qualified_type
7670 (build_pointer_type
7671 (build_type_variant (TREE_TYPE (type), 1,
7672 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7673 TYPE_QUALS (type)));
f8e93a2e 7674 else
7675 {
9b2d6d13 7676 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7677 *no_add_attrs = true;
7678 }
7679
7680 return NULL_TREE;
7681}
7682
292237f3 7683/* Handle a "scalar_storage_order" attribute; arguments as in
7684 struct attribute_spec.handler. */
7685
7686static tree
7687handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
7688 int flags, bool *no_add_attrs)
7689{
7690 tree id = TREE_VALUE (args);
7691 tree type;
7692
7693 if (TREE_CODE (*node) == TYPE_DECL
7694 && ! (flags & ATTR_FLAG_CXX11))
7695 node = &TREE_TYPE (*node);
7696 type = *node;
7697
7698 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
7699 {
7700 error ("scalar_storage_order is not supported because endianness "
7701 "is not uniform");
7702 return NULL_TREE;
7703 }
7704
7705 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
7706 {
7707 bool reverse = false;
7708
7709 if (TREE_CODE (id) == STRING_CST
7710 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
7711 reverse = !BYTES_BIG_ENDIAN;
7712 else if (TREE_CODE (id) == STRING_CST
7713 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
7714 reverse = BYTES_BIG_ENDIAN;
7715 else
7716 {
7717 error ("scalar_storage_order argument must be one of \"big-endian\""
7718 " or \"little-endian\"");
7719 return NULL_TREE;
7720 }
7721
7722 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7723 {
7724 if (reverse)
7725 /* A type variant isn't good enough, since we don't want a cast
7726 to such a type to be removed as a no-op. */
7727 *node = type = build_duplicate_type (type);
7728 }
7729
7730 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
7731 return NULL_TREE;
7732 }
7733
7734 warning (OPT_Wattributes, "%qE attribute ignored", name);
7735 *no_add_attrs = true;
7736 return NULL_TREE;
7737}
7738
f8e93a2e 7739/* Handle a "transparent_union" attribute; arguments as in
7740 struct attribute_spec.handler. */
7741
7742static tree
1cae46be 7743handle_transparent_union_attribute (tree *node, tree name,
9a03a746 7744 tree ARG_UNUSED (args), int flags,
09347743 7745 bool *no_add_attrs)
f8e93a2e 7746{
881eb642 7747 tree type;
03908818 7748
7749 *no_add_attrs = true;
f8e93a2e 7750
ffcdbf9c 7751 if (TREE_CODE (*node) == TYPE_DECL
7752 && ! (flags & ATTR_FLAG_CXX11))
881eb642 7753 node = &TREE_TYPE (*node);
7754 type = *node;
f8e93a2e 7755
03908818 7756 if (TREE_CODE (type) == UNION_TYPE)
f8e93a2e 7757 {
fca86134 7758 /* Make sure that the first field will work for a transparent union.
7759 If the type isn't complete yet, leave the check to the code in
7760 finish_struct. */
7761 if (TYPE_SIZE (type))
7762 {
7763 tree first = first_field (type);
7764 if (first == NULL_TREE
7765 || DECL_ARTIFICIAL (first)
7766 || TYPE_MODE (type) != DECL_MODE (first))
7767 goto ignored;
7768 }
7769
f8e93a2e 7770 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
03908818 7771 {
fca86134 7772 /* If the type isn't complete yet, setting the flag
7773 on a variant wouldn't ever be checked. */
7774 if (!TYPE_SIZE (type))
7775 goto ignored;
7776
7777 /* build_duplicate_type doesn't work for C++. */
7778 if (c_dialect_cxx ())
03908818 7779 goto ignored;
7780
292237f3 7781 /* A type variant isn't good enough, since we don't want a cast
7782 to such a type to be removed as a no-op. */
03908818 7783 *node = type = build_duplicate_type (type);
7784 }
7785
8df5a43d 7786 TYPE_TRANSPARENT_AGGR (type) = 1;
03908818 7787 return NULL_TREE;
f8e93a2e 7788 }
7789
03908818 7790 ignored:
7791 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7792 return NULL_TREE;
7793}
7794
9af7fd5b 7795/* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7796 get the requested priority for a constructor or destructor,
7797 possibly issuing diagnostics for invalid or reserved
7798 priorities. */
7799
7800static priority_type
7801get_priority (tree args, bool is_destructor)
7802{
7803 HOST_WIDE_INT pri;
6c181a06 7804 tree arg;
9af7fd5b 7805
7806 if (!args)
7807 return DEFAULT_INIT_PRIORITY;
48e1416a 7808
28fbc04f 7809 if (!SUPPORTS_INIT_PRIORITY)
7810 {
7811 if (is_destructor)
7812 error ("destructor priorities are not supported");
7813 else
7814 error ("constructor priorities are not supported");
7815 return DEFAULT_INIT_PRIORITY;
7816 }
7817
6c181a06 7818 arg = TREE_VALUE (args);
253e1cae 7819 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7820 goto invalid;
7821 if (arg == error_mark_node)
7822 return DEFAULT_INIT_PRIORITY;
c28ddc97 7823 arg = default_conversion (arg);
e913b5cd 7824 if (!tree_fits_shwi_p (arg)
6c181a06 7825 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
9af7fd5b 7826 goto invalid;
7827
e913b5cd 7828 pri = tree_to_shwi (arg);
9af7fd5b 7829 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7830 goto invalid;
7831
7832 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7833 {
7834 if (is_destructor)
7835 warning (0,
7836 "destructor priorities from 0 to %d are reserved "
48e1416a 7837 "for the implementation",
9af7fd5b 7838 MAX_RESERVED_INIT_PRIORITY);
7839 else
7840 warning (0,
7841 "constructor priorities from 0 to %d are reserved "
48e1416a 7842 "for the implementation",
9af7fd5b 7843 MAX_RESERVED_INIT_PRIORITY);
7844 }
7845 return pri;
7846
7847 invalid:
7848 if (is_destructor)
7849 error ("destructor priorities must be integers from 0 to %d inclusive",
7850 MAX_INIT_PRIORITY);
7851 else
7852 error ("constructor priorities must be integers from 0 to %d inclusive",
7853 MAX_INIT_PRIORITY);
7854 return DEFAULT_INIT_PRIORITY;
7855}
7856
f8e93a2e 7857/* Handle a "constructor" attribute; arguments as in
7858 struct attribute_spec.handler. */
7859
7860static tree
9af7fd5b 7861handle_constructor_attribute (tree *node, tree name, tree args,
9a03a746 7862 int ARG_UNUSED (flags),
09347743 7863 bool *no_add_attrs)
f8e93a2e 7864{
7865 tree decl = *node;
7866 tree type = TREE_TYPE (decl);
7867
7868 if (TREE_CODE (decl) == FUNCTION_DECL
7869 && TREE_CODE (type) == FUNCTION_TYPE
7870 && decl_function_context (decl) == 0)
7871 {
9af7fd5b 7872 priority_type priority;
f8e93a2e 7873 DECL_STATIC_CONSTRUCTOR (decl) = 1;
9af7fd5b 7874 priority = get_priority (args, /*is_destructor=*/false);
7875 SET_DECL_INIT_PRIORITY (decl, priority);
f8e93a2e 7876 TREE_USED (decl) = 1;
7877 }
7878 else
7879 {
9b2d6d13 7880 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7881 *no_add_attrs = true;
7882 }
7883
7884 return NULL_TREE;
7885}
7886
7887/* Handle a "destructor" attribute; arguments as in
7888 struct attribute_spec.handler. */
7889
7890static tree
9af7fd5b 7891handle_destructor_attribute (tree *node, tree name, tree args,
9a03a746 7892 int ARG_UNUSED (flags),
09347743 7893 bool *no_add_attrs)
f8e93a2e 7894{
7895 tree decl = *node;
7896 tree type = TREE_TYPE (decl);
7897
7898 if (TREE_CODE (decl) == FUNCTION_DECL
7899 && TREE_CODE (type) == FUNCTION_TYPE
7900 && decl_function_context (decl) == 0)
7901 {
9af7fd5b 7902 priority_type priority;
f8e93a2e 7903 DECL_STATIC_DESTRUCTOR (decl) = 1;
9af7fd5b 7904 priority = get_priority (args, /*is_destructor=*/true);
7905 SET_DECL_FINI_PRIORITY (decl, priority);
f8e93a2e 7906 TREE_USED (decl) = 1;
7907 }
7908 else
7909 {
9b2d6d13 7910 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7911 *no_add_attrs = true;
7912 }
7913
7914 return NULL_TREE;
7915}
7916
1c58e3f1 7917/* Nonzero if the mode is a valid vector mode for this architecture.
7918 This returns nonzero even if there is no hardware support for the
7919 vector mode, but we can emulate with narrower modes. */
7920
7921static int
3754d046 7922vector_mode_valid_p (machine_mode mode)
1c58e3f1 7923{
7924 enum mode_class mclass = GET_MODE_CLASS (mode);
3754d046 7925 machine_mode innermode;
1c58e3f1 7926
7927 /* Doh! What's going on? */
7928 if (mclass != MODE_VECTOR_INT
7929 && mclass != MODE_VECTOR_FLOAT
7930 && mclass != MODE_VECTOR_FRACT
7931 && mclass != MODE_VECTOR_UFRACT
7932 && mclass != MODE_VECTOR_ACCUM
7933 && mclass != MODE_VECTOR_UACCUM)
7934 return 0;
7935
7936 /* Hardware support. Woo hoo! */
7937 if (targetm.vector_mode_supported_p (mode))
7938 return 1;
7939
7940 innermode = GET_MODE_INNER (mode);
7941
7942 /* We should probably return 1 if requesting V4DI and we have no DI,
7943 but we have V2DI, but this is probably very unlikely. */
7944
7945 /* If we have support for the inner mode, we can safely emulate it.
7946 We may not have V2DI, but me can emulate with a pair of DIs. */
7947 return targetm.scalar_mode_supported_p (innermode);
7948}
7949
7950
f8e93a2e 7951/* Handle a "mode" attribute; arguments as in
7952 struct attribute_spec.handler. */
7953
7954static tree
9a03a746 7955handle_mode_attribute (tree *node, tree name, tree args,
7956 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7957{
7958 tree type = *node;
d1dd9ac0 7959 tree ident = TREE_VALUE (args);
f8e93a2e 7960
7961 *no_add_attrs = true;
ab2c1de8 7962
d1dd9ac0 7963 if (TREE_CODE (ident) != IDENTIFIER_NODE)
9b2d6d13 7964 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7965 else
7966 {
7967 int j;
d1dd9ac0 7968 const char *p = IDENTIFIER_POINTER (ident);
f8e93a2e 7969 int len = strlen (p);
3754d046 7970 machine_mode mode = VOIDmode;
f8e93a2e 7971 tree typefm;
b2aef146 7972 bool valid_mode;
f8e93a2e 7973
7974 if (len > 4 && p[0] == '_' && p[1] == '_'
7975 && p[len - 1] == '_' && p[len - 2] == '_')
7976 {
4fd61bc6 7977 char *newp = (char *) alloca (len - 1);
f8e93a2e 7978
7979 strcpy (newp, &p[2]);
7980 newp[len - 4] = '\0';
7981 p = newp;
7982 }
7983
7984 /* Change this type to have a type with the specified mode.
7985 First check for the special modes. */
84166705 7986 if (!strcmp (p, "byte"))
f8e93a2e 7987 mode = byte_mode;
7988 else if (!strcmp (p, "word"))
7989 mode = word_mode;
84166705 7990 else if (!strcmp (p, "pointer"))
f8e93a2e 7991 mode = ptr_mode;
0ef89dfd 7992 else if (!strcmp (p, "libgcc_cmp_return"))
7993 mode = targetm.libgcc_cmp_return_mode ();
7994 else if (!strcmp (p, "libgcc_shift_count"))
7995 mode = targetm.libgcc_shift_count_mode ();
1bd43494 7996 else if (!strcmp (p, "unwind_word"))
7997 mode = targetm.unwind_word_mode ();
f8e93a2e 7998 else
7999 for (j = 0; j < NUM_MACHINE_MODES; j++)
8000 if (!strcmp (p, GET_MODE_NAME (j)))
743a6f47 8001 {
3754d046 8002 mode = (machine_mode) j;
743a6f47 8003 break;
8004 }
f8e93a2e 8005
8006 if (mode == VOIDmode)
4917c376 8007 {
d1dd9ac0 8008 error ("unknown machine mode %qE", ident);
4917c376 8009 return NULL_TREE;
8010 }
8011
b2aef146 8012 valid_mode = false;
8013 switch (GET_MODE_CLASS (mode))
4917c376 8014 {
b2aef146 8015 case MODE_INT:
8016 case MODE_PARTIAL_INT:
8017 case MODE_FLOAT:
c4503c0a 8018 case MODE_DECIMAL_FLOAT:
9421ebb9 8019 case MODE_FRACT:
8020 case MODE_UFRACT:
8021 case MODE_ACCUM:
8022 case MODE_UACCUM:
b2aef146 8023 valid_mode = targetm.scalar_mode_supported_p (mode);
8024 break;
8025
8026 case MODE_COMPLEX_INT:
8027 case MODE_COMPLEX_FLOAT:
8028 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
8029 break;
8030
8031 case MODE_VECTOR_INT:
8032 case MODE_VECTOR_FLOAT:
9421ebb9 8033 case MODE_VECTOR_FRACT:
8034 case MODE_VECTOR_UFRACT:
8035 case MODE_VECTOR_ACCUM:
8036 case MODE_VECTOR_UACCUM:
9b2d6d13 8037 warning (OPT_Wattributes, "specifying vector types with "
8038 "__attribute__ ((mode)) is deprecated");
8039 warning (OPT_Wattributes,
8040 "use __attribute__ ((vector_size)) instead");
b2aef146 8041 valid_mode = vector_mode_valid_p (mode);
8042 break;
4917c376 8043
b2aef146 8044 default:
8045 break;
8046 }
8047 if (!valid_mode)
8048 {
1e5fcbe2 8049 error ("unable to emulate %qs", p);
b2aef146 8050 return NULL_TREE;
8051 }
4917c376 8052
b2aef146 8053 if (POINTER_TYPE_P (type))
ead34f59 8054 {
6d5d708e 8055 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
3754d046 8056 tree (*fn)(tree, machine_mode, bool);
b2aef146 8057
6d5d708e 8058 if (!targetm.addr_space.valid_pointer_mode (mode, as))
ead34f59 8059 {
1e5fcbe2 8060 error ("invalid pointer mode %qs", p);
ead34f59 8061 return NULL_TREE;
8062 }
8063
a0c938f0 8064 if (TREE_CODE (type) == POINTER_TYPE)
b2aef146 8065 fn = build_pointer_type_for_mode;
805e22b2 8066 else
b2aef146 8067 fn = build_reference_type_for_mode;
8068 typefm = fn (TREE_TYPE (type), mode, false);
ead34f59 8069 }
b2aef146 8070 else
9421ebb9 8071 {
8072 /* For fixed-point modes, we need to test if the signness of type
8073 and the machine mode are consistent. */
8074 if (ALL_FIXED_POINT_MODE_P (mode)
8075 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
8076 {
bf776685 8077 error ("signedness of type and machine mode %qs don%'t match", p);
9421ebb9 8078 return NULL_TREE;
8079 }
8080 /* For fixed-point modes, we need to pass saturating info. */
8081 typefm = lang_hooks.types.type_for_mode (mode,
8082 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
8083 : TYPE_UNSIGNED (type));
8084 }
3a648ab9 8085
b2aef146 8086 if (typefm == NULL_TREE)
8087 {
743a6f47 8088 error ("no data type for mode %qs", p);
b2aef146 8089 return NULL_TREE;
8090 }
3a648ab9 8091 else if (TREE_CODE (type) == ENUMERAL_TYPE)
8092 {
8093 /* For enumeral types, copy the precision from the integer
8094 type returned above. If not an INTEGER_TYPE, we can't use
8095 this mode for this type. */
8096 if (TREE_CODE (typefm) != INTEGER_TYPE)
8097 {
743a6f47 8098 error ("cannot use mode %qs for enumeral types", p);
3a648ab9 8099 return NULL_TREE;
8100 }
8101
10080eac 8102 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
8103 {
8104 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
8105 typefm = type;
8106 }
8107 else
8108 {
8109 /* We cannot build a type variant, as there's code that assumes
8110 that TYPE_MAIN_VARIANT has the same mode. This includes the
8111 debug generators. Instead, create a subrange type. This
8112 results in all of the enumeral values being emitted only once
8113 in the original, and the subtype gets them by reference. */
8114 if (TYPE_UNSIGNED (type))
8115 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
8116 else
8117 typefm = make_signed_type (TYPE_PRECISION (typefm));
8118 TREE_TYPE (typefm) = type;
8119 }
3a648ab9 8120 }
4bf450a1 8121 else if (VECTOR_MODE_P (mode)
8122 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
8123 : TREE_CODE (type) != TREE_CODE (typefm))
743a6f47 8124 {
8125 error ("mode %qs applied to inappropriate type", p);
8126 return NULL_TREE;
8127 }
8128
b2aef146 8129 *node = typefm;
f8e93a2e 8130 }
8131
8132 return NULL_TREE;
8133}
8134
8135/* Handle a "section" attribute; arguments as in
8136 struct attribute_spec.handler. */
8137
8138static tree
9a03a746 8139handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8140 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8141{
8142 tree decl = *node;
8143
9866562d 8144 if (!targetm_common.have_named_sections)
f8e93a2e 8145 {
9866562d 8146 error_at (DECL_SOURCE_LOCATION (*node),
8147 "section attributes are not supported for this target");
8148 goto fail;
8149 }
065efcb1 8150
9866562d 8151 user_defined_section_attribute = true;
f8e93a2e 8152
b443c459 8153 if (!VAR_OR_FUNCTION_DECL_P (decl))
9866562d 8154 {
8155 error ("section attribute not allowed for %q+D", *node);
8156 goto fail;
f8e93a2e 8157 }
9866562d 8158
8159 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
f8e93a2e 8160 {
9866562d 8161 error ("section attribute argument not a string constant");
8162 goto fail;
f8e93a2e 8163 }
8164
f48c7f4a 8165 if (VAR_P (decl)
9866562d 8166 && current_function_decl != NULL_TREE
8167 && !TREE_STATIC (decl))
8168 {
8169 error_at (DECL_SOURCE_LOCATION (decl),
8170 "section attribute cannot be specified for local variables");
8171 goto fail;
8172 }
8173
8174 /* The decl may have already been given a section attribute
8175 from a previous declaration. Ensure they match. */
8176 if (DECL_SECTION_NAME (decl) != NULL
8177 && strcmp (DECL_SECTION_NAME (decl),
8178 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
8179 {
8180 error ("section of %q+D conflicts with previous declaration", *node);
8181 goto fail;
8182 }
8183
f48c7f4a 8184 if (VAR_P (decl)
9866562d 8185 && !targetm.have_tls && targetm.emutls.tmpl_section
8186 && DECL_THREAD_LOCAL_P (decl))
8187 {
8188 error ("section of %q+D cannot be overridden", *node);
8189 goto fail;
8190 }
8191
8192 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
8193 return NULL_TREE;
8194
8195fail:
8196 *no_add_attrs = true;
f8e93a2e 8197 return NULL_TREE;
8198}
8199
83e25171 8200/* Check whether ALIGN is a valid user-specified alignment. If so,
8201 return its base-2 log; if not, output an error and return -1. If
8202 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
8203 no error. */
8204int
8205check_user_alignment (const_tree align, bool allow_zero)
8206{
8207 int i;
8208
3e5a8b00 8209 if (error_operand_p (align))
8210 return -1;
5abaa10a 8211 if (TREE_CODE (align) != INTEGER_CST
8212 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
83e25171 8213 {
8214 error ("requested alignment is not an integer constant");
8215 return -1;
8216 }
8217 else if (allow_zero && integer_zerop (align))
8218 return -1;
1a087624 8219 else if (tree_int_cst_sgn (align) == -1
8220 || (i = tree_log2 (align)) == -1)
83e25171 8221 {
1a087624 8222 error ("requested alignment is not a positive power of 2");
83e25171 8223 return -1;
8224 }
8225 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
8226 {
8227 error ("requested alignment is too large");
8228 return -1;
8229 }
8230 return i;
8231}
8232
ffcdbf9c 8233/*
8234 If in c++-11, check if the c++-11 alignment constraint with respect
8235 to fundamental alignment (in [dcl.align]) are satisfied. If not in
8236 c++-11 mode, does nothing.
8237
8238 [dcl.align]2/ says:
8239
8240 [* if the constant expression evaluates to a fundamental alignment,
8241 the alignment requirement of the declared entity shall be the
8242 specified fundamental alignment.
8243
8244 * if the constant expression evaluates to an extended alignment
8245 and the implementation supports that alignment in the context
8246 of the declaration, the alignment of the declared entity shall
8247 be that alignment
8248
8249 * if the constant expression evaluates to an extended alignment
8250 and the implementation does not support that alignment in the
8251 context of the declaration, the program is ill-formed]. */
8252
8253static bool
8254check_cxx_fundamental_alignment_constraints (tree node,
8255 unsigned align_log,
8256 int flags)
8257{
8258 bool alignment_too_large_p = false;
8259 unsigned requested_alignment = 1U << align_log;
8260 unsigned max_align = 0;
8261
8262 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
8263 || (node == NULL_TREE || node == error_mark_node))
8264 return true;
8265
8266 if (cxx_fundamental_alignment_p (requested_alignment))
8267 return true;
8268
8269 if (DECL_P (node))
8270 {
8271 if (TREE_STATIC (node))
8272 {
8273 /* For file scope variables and static members, the target
8274 supports alignments that are at most
8275 MAX_OFILE_ALIGNMENT. */
8276 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
8277 alignment_too_large_p = true;
8278 }
8279 else
8280 {
8281#ifdef BIGGEST_FIELD_ALIGNMENT
8282#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
8283#else
8284#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
8285#endif
8286 /* For non-static members, the target supports either
8287 alignments that at most either BIGGEST_FIELD_ALIGNMENT
8288 if it is defined or BIGGEST_ALIGNMENT. */
8289 max_align = MAX_TARGET_FIELD_ALIGNMENT;
8290 if (TREE_CODE (node) == FIELD_DECL
8291 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
8292 alignment_too_large_p = true;
8293#undef MAX_TARGET_FIELD_ALIGNMENT
8294 /* For stack variables, the target supports at most
8295 MAX_STACK_ALIGNMENT. */
8296 else if (decl_function_context (node) != NULL
8297 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
8298 alignment_too_large_p = true;
8299 }
8300 }
8301 else if (TYPE_P (node))
8302 {
8303 /* Let's be liberal for types. */
8304 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
8305 alignment_too_large_p = true;
8306 }
8307
8308 if (alignment_too_large_p)
8309 pedwarn (input_location, OPT_Wattributes,
8310 "requested alignment %d is larger than %d",
8311 requested_alignment, max_align);
8312
8313 return !alignment_too_large_p;
8314}
8315
f8e93a2e 8316/* Handle a "aligned" attribute; arguments as in
8317 struct attribute_spec.handler. */
8318
8319static tree
9a03a746 8320handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
09347743 8321 int flags, bool *no_add_attrs)
f8e93a2e 8322{
8323 tree decl = NULL_TREE;
8324 tree *type = NULL;
8325 int is_type = 0;
caf62483 8326 tree align_expr;
f8e93a2e 8327 int i;
8328
caf62483 8329 if (args)
8330 {
8331 align_expr = TREE_VALUE (args);
3e5a8b00 8332 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
8333 && TREE_CODE (align_expr) != FUNCTION_DECL)
caf62483 8334 align_expr = default_conversion (align_expr);
8335 }
8336 else
8337 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
8338
f8e93a2e 8339 if (DECL_P (*node))
8340 {
8341 decl = *node;
8342 type = &TREE_TYPE (decl);
8343 is_type = TREE_CODE (*node) == TYPE_DECL;
8344 }
8345 else if (TYPE_P (*node))
8346 type = node, is_type = 1;
8347
ffcdbf9c 8348 if ((i = check_user_alignment (align_expr, false)) == -1
8349 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
83e25171 8350 *no_add_attrs = true;
f8e93a2e 8351 else if (is_type)
8352 {
2ec3af9c 8353 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8354 /* OK, modify the type in place. */;
f8e93a2e 8355 /* If we have a TYPE_DECL, then copy the type, so that we
8356 don't accidentally modify a builtin type. See pushdecl. */
2ec3af9c 8357 else if (decl && TREE_TYPE (decl) != error_mark_node
8358 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
f8e93a2e 8359 {
8360 tree tt = TREE_TYPE (decl);
e086912e 8361 *type = build_variant_type_copy (*type);
f8e93a2e 8362 DECL_ORIGINAL_TYPE (decl) = tt;
8363 TYPE_NAME (*type) = decl;
8364 TREE_USED (*type) = TREE_USED (decl);
8365 TREE_TYPE (decl) = *type;
8366 }
2ec3af9c 8367 else
e086912e 8368 *type = build_variant_type_copy (*type);
f8e93a2e 8369
7cfdc2f0 8370 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
f8e93a2e 8371 TYPE_USER_ALIGN (*type) = 1;
8372 }
097b5c8b 8373 else if (! VAR_OR_FUNCTION_DECL_P (decl)
f8e93a2e 8374 && TREE_CODE (decl) != FIELD_DECL)
8375 {
3cf8b391 8376 error ("alignment may not be specified for %q+D", decl);
f8e93a2e 8377 *no_add_attrs = true;
8378 }
ffcdbf9c 8379 else if (DECL_USER_ALIGN (decl)
8380 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
8381 /* C++-11 [dcl.align/4]:
8382
8383 When multiple alignment-specifiers are specified for an
8384 entity, the alignment requirement shall be set to the
8385 strictest specified alignment.
8386
8387 This formally comes from the c++11 specification but we are
8388 doing it for the GNU attribute syntax as well. */
8389 *no_add_attrs = true;
097b5c8b 8390 else if (TREE_CODE (decl) == FUNCTION_DECL
7cfdc2f0 8391 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
097b5c8b 8392 {
8393 if (DECL_USER_ALIGN (decl))
8394 error ("alignment for %q+D was previously specified as %d "
8395 "and may not be decreased", decl,
8396 DECL_ALIGN (decl) / BITS_PER_UNIT);
8397 else
8398 error ("alignment for %q+D must be at least %d", decl,
8399 DECL_ALIGN (decl) / BITS_PER_UNIT);
8400 *no_add_attrs = true;
8401 }
f8e93a2e 8402 else
8403 {
7cfdc2f0 8404 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
f8e93a2e 8405 DECL_USER_ALIGN (decl) = 1;
8406 }
8407
8408 return NULL_TREE;
8409}
8410
8411/* Handle a "weak" attribute; arguments as in
8412 struct attribute_spec.handler. */
8413
8414static tree
f948b309 8415handle_weak_attribute (tree *node, tree name,
9a03a746 8416 tree ARG_UNUSED (args),
8417 int ARG_UNUSED (flags),
8418 bool * ARG_UNUSED (no_add_attrs))
f8e93a2e 8419{
f948b309 8420 if (TREE_CODE (*node) == FUNCTION_DECL
059a60f3 8421 && DECL_DECLARED_INLINE_P (*node))
8422 {
0725e25c 8423 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
059a60f3 8424 *no_add_attrs = true;
8425 }
85c0a25c 8426 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8427 {
8428 error ("indirect function %q+D cannot be declared weak", *node);
8429 *no_add_attrs = true;
8430 return NULL_TREE;
8431 }
b443c459 8432 else if (VAR_OR_FUNCTION_DECL_P (*node))
9e6bcade 8433 declare_weak (*node);
f948b309 8434 else
8435 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8436
8437 return NULL_TREE;
8438}
8439
4641882f 8440/* Handle a "noplt" attribute; arguments as in
8441 struct attribute_spec.handler. */
8442
8443static tree
8444handle_noplt_attribute (tree *node, tree name,
8445 tree ARG_UNUSED (args),
8446 int ARG_UNUSED (flags),
8447 bool * ARG_UNUSED (no_add_attrs))
8448{
8449 if (TREE_CODE (*node) != FUNCTION_DECL)
8450 {
8451 warning (OPT_Wattributes,
8452 "%qE attribute is only applicable on functions", name);
8453 *no_add_attrs = true;
8454 return NULL_TREE;
8455 }
8456 return NULL_TREE;
8457}
8458
85c0a25c 8459/* Handle an "alias" or "ifunc" attribute; arguments as in
8460 struct attribute_spec.handler, except that IS_ALIAS tells us
8461 whether this is an alias as opposed to ifunc attribute. */
f8e93a2e 8462
8463static tree
85c0a25c 8464handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
8465 bool *no_add_attrs)
f8e93a2e 8466{
8467 tree decl = *node;
8468
85c0a25c 8469 if (TREE_CODE (decl) != FUNCTION_DECL
f48c7f4a 8470 && (!is_alias || !VAR_P (decl)))
9e830260 8471 {
8472 warning (OPT_Wattributes, "%qE attribute ignored", name);
8473 *no_add_attrs = true;
8474 }
8475 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
48e1416a 8476 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 8477 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
8478 /* A static variable declaration is always a tentative definition,
8479 but the alias is a non-tentative definition which overrides. */
48e1416a 8480 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 8481 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
f8e93a2e 8482 {
85c0a25c 8483 error ("%q+D defined both normally and as %qE attribute", decl, name);
f8e93a2e 8484 *no_add_attrs = true;
85c0a25c 8485 return NULL_TREE;
f8e93a2e 8486 }
85c0a25c 8487 else if (!is_alias
8488 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
8489 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
8490 {
8491 error ("weak %q+D cannot be defined %qE", decl, name);
8492 *no_add_attrs = true;
8493 return NULL_TREE;
8494 }
8c42f0d9 8495
8496 /* Note that the very first time we process a nested declaration,
8497 decl_function_context will not be set. Indeed, *would* never
8498 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8499 we do below. After such frobbery, pushdecl would set the context.
8500 In any case, this is never what we want. */
8501 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
f8e93a2e 8502 {
8503 tree id;
8504
8505 id = TREE_VALUE (args);
8506 if (TREE_CODE (id) != STRING_CST)
8507 {
85c0a25c 8508 error ("attribute %qE argument not a string", name);
f8e93a2e 8509 *no_add_attrs = true;
8510 return NULL_TREE;
8511 }
8512 id = get_identifier (TREE_STRING_POINTER (id));
8513 /* This counts as a use of the object pointed to. */
8514 TREE_USED (id) = 1;
8515
8516 if (TREE_CODE (decl) == FUNCTION_DECL)
8517 DECL_INITIAL (decl) = error_mark_node;
8518 else
f2526cce 8519 TREE_STATIC (decl) = 1;
85c0a25c 8520
8521 if (!is_alias)
8522 /* ifuncs are also aliases, so set that attribute too. */
8523 DECL_ATTRIBUTES (decl)
8524 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
f8e93a2e 8525 }
8526 else
8527 {
9b2d6d13 8528 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8529 *no_add_attrs = true;
8530 }
8531
8e857c41 8532 if (decl_in_symtab_p (*node))
8533 {
8534 struct symtab_node *n = symtab_node::get (decl);
8535 if (n && n->refuse_visibility_changes)
8536 {
8537 if (is_alias)
8538 error ("%+D declared alias after being used", decl);
8539 else
8540 error ("%+D declared ifunc after being used", decl);
8541 }
8542 }
8543
8544
f8e93a2e 8545 return NULL_TREE;
8546}
8547
85c0a25c 8548/* Handle an "alias" or "ifunc" attribute; arguments as in
8549 struct attribute_spec.handler. */
8550
8551static tree
8552handle_ifunc_attribute (tree *node, tree name, tree args,
8553 int ARG_UNUSED (flags), bool *no_add_attrs)
8554{
8555 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8556}
8557
8558/* Handle an "alias" or "ifunc" attribute; arguments as in
8559 struct attribute_spec.handler. */
8560
8561static tree
8562handle_alias_attribute (tree *node, tree name, tree args,
8563 int ARG_UNUSED (flags), bool *no_add_attrs)
8564{
8565 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8566}
8567
f4a30bd7 8568/* Handle a "weakref" attribute; arguments as in struct
8569 attribute_spec.handler. */
8570
8571static tree
8572handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8573 int flags, bool *no_add_attrs)
8574{
8575 tree attr = NULL_TREE;
8576
83852912 8577 /* We must ignore the attribute when it is associated with
8578 local-scoped decls, since attribute alias is ignored and many
8579 such symbols do not even have a DECL_WEAK field. */
64a7bd81 8580 if (decl_function_context (*node)
8581 || current_function_decl
b443c459 8582 || !VAR_OR_FUNCTION_DECL_P (*node))
83852912 8583 {
8584 warning (OPT_Wattributes, "%qE attribute ignored", name);
8585 *no_add_attrs = true;
8586 return NULL_TREE;
8587 }
8588
85c0a25c 8589 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8590 {
8591 error ("indirect function %q+D cannot be declared weakref", *node);
8592 *no_add_attrs = true;
8593 return NULL_TREE;
8594 }
8595
f4a30bd7 8596 /* The idea here is that `weakref("name")' mutates into `weakref,
8597 alias("name")', and weakref without arguments, in turn,
8598 implicitly adds weak. */
8599
8600 if (args)
8601 {
8602 attr = tree_cons (get_identifier ("alias"), args, attr);
8603 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8604
8605 *no_add_attrs = true;
0a3ecdc1 8606
8607 decl_attributes (node, attr, flags);
f4a30bd7 8608 }
8609 else
8610 {
8611 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
712d2297 8612 error_at (DECL_SOURCE_LOCATION (*node),
8613 "weakref attribute must appear before alias attribute");
f4a30bd7 8614
0a3ecdc1 8615 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8616 and that isn't supported; and because it wants to add it to
8617 the list of weak decls, which isn't helpful. */
8618 DECL_WEAK (*node) = 1;
f4a30bd7 8619 }
8620
8e857c41 8621 if (decl_in_symtab_p (*node))
8622 {
8623 struct symtab_node *n = symtab_node::get (*node);
8624 if (n && n->refuse_visibility_changes)
8625 error ("%+D declared weakref after being used", *node);
8626 }
8627
f4a30bd7 8628 return NULL_TREE;
8629}
8630
f8e93a2e 8631/* Handle an "visibility" attribute; arguments as in
8632 struct attribute_spec.handler. */
8633
8634static tree
1cae46be 8635handle_visibility_attribute (tree *node, tree name, tree args,
9a03a746 8636 int ARG_UNUSED (flags),
4a2849cb 8637 bool *ARG_UNUSED (no_add_attrs))
f8e93a2e 8638{
8639 tree decl = *node;
9c40570a 8640 tree id = TREE_VALUE (args);
4a2849cb 8641 enum symbol_visibility vis;
f8e93a2e 8642
b212f378 8643 if (TYPE_P (*node))
8644 {
4a2849cb 8645 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8646 /* OK */;
8e539fdc 8647 else if (!RECORD_OR_UNION_TYPE_P (*node))
4a2849cb 8648 {
8649 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8650 name);
8651 return NULL_TREE;
8652 }
8653 else if (TYPE_FIELDS (*node))
8654 {
8655 error ("%qE attribute ignored because %qT is already defined",
8656 name, *node);
8657 return NULL_TREE;
8658 }
b212f378 8659 }
84166705 8660 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
f8e93a2e 8661 {
9b2d6d13 8662 warning (OPT_Wattributes, "%qE attribute ignored", name);
9c40570a 8663 return NULL_TREE;
f8e93a2e 8664 }
f8e93a2e 8665
9c40570a 8666 if (TREE_CODE (id) != STRING_CST)
8667 {
07e3a3d2 8668 error ("visibility argument not a string");
9c40570a 8669 return NULL_TREE;
f8e93a2e 8670 }
b27ac6b5 8671
b212f378 8672 /* If this is a type, set the visibility on the type decl. */
8673 if (TYPE_P (decl))
8674 {
8675 decl = TYPE_NAME (decl);
84166705 8676 if (!decl)
a0c938f0 8677 return NULL_TREE;
e147aab3 8678 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8679 {
9b2d6d13 8680 warning (OPT_Wattributes, "%qE attribute ignored on types",
e147aab3 8681 name);
8682 return NULL_TREE;
8683 }
b212f378 8684 }
f8e93a2e 8685
9c40570a 8686 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
4a2849cb 8687 vis = VISIBILITY_DEFAULT;
9c40570a 8688 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
4a2849cb 8689 vis = VISIBILITY_INTERNAL;
9c40570a 8690 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
4a2849cb 8691 vis = VISIBILITY_HIDDEN;
9c40570a 8692 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
4a2849cb 8693 vis = VISIBILITY_PROTECTED;
9c40570a 8694 else
4a2849cb 8695 {
8696 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8697 vis = VISIBILITY_DEFAULT;
8698 }
8699
8700 if (DECL_VISIBILITY_SPECIFIED (decl)
098a01e7 8701 && vis != DECL_VISIBILITY (decl))
8702 {
8703 tree attributes = (TYPE_P (*node)
8704 ? TYPE_ATTRIBUTES (*node)
8705 : DECL_ATTRIBUTES (decl));
8706 if (lookup_attribute ("visibility", attributes))
8707 error ("%qD redeclared with different visibility", decl);
8708 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8709 && lookup_attribute ("dllimport", attributes))
8710 error ("%qD was declared %qs which implies default visibility",
8711 decl, "dllimport");
8712 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8713 && lookup_attribute ("dllexport", attributes))
8714 error ("%qD was declared %qs which implies default visibility",
8715 decl, "dllexport");
8716 }
4a2849cb 8717
8718 DECL_VISIBILITY (decl) = vis;
b212f378 8719 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8720
4a2849cb 8721 /* Go ahead and attach the attribute to the node as well. This is needed
8722 so we can determine whether we have VISIBILITY_DEFAULT because the
8723 visibility was not specified, or because it was explicitly overridden
8724 from the containing scope. */
9c40570a 8725
f8e93a2e 8726 return NULL_TREE;
8727}
8728
3aa0c315 8729/* Determine the ELF symbol visibility for DECL, which is either a
8730 variable or a function. It is an error to use this function if a
8731 definition of DECL is not available in this translation unit.
8732 Returns true if the final visibility has been determined by this
8733 function; false if the caller is free to make additional
8734 modifications. */
8735
8736bool
8737c_determine_visibility (tree decl)
8738{
b443c459 8739 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
3aa0c315 8740
8741 /* If the user explicitly specified the visibility with an
8742 attribute, honor that. DECL_VISIBILITY will have been set during
920f5a70 8743 the processing of the attribute. We check for an explicit
8744 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8745 to distinguish the use of an attribute from the use of a "#pragma
8746 GCC visibility push(...)"; in the latter case we still want other
8747 considerations to be able to overrule the #pragma. */
8748 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8749 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8750 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8751 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
3aa0c315 8752 return true;
8753
4a2849cb 8754 /* Set default visibility to whatever the user supplied with
8755 visibility_specified depending on #pragma GCC visibility. */
8756 if (!DECL_VISIBILITY_SPECIFIED (decl))
8757 {
2d9d8740 8758 if (visibility_options.inpragma
8759 || DECL_VISIBILITY (decl) != default_visibility)
8760 {
8761 DECL_VISIBILITY (decl) = default_visibility;
8762 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8763 /* If visibility changed and DECL already has DECL_RTL, ensure
8764 symbol flags are updated. */
f48c7f4a 8765 if (((VAR_P (decl) && TREE_STATIC (decl))
2d9d8740 8766 || TREE_CODE (decl) == FUNCTION_DECL)
8767 && DECL_RTL_SET_P (decl))
8768 make_decl_rtl (decl);
8769 }
4a2849cb 8770 }
3aa0c315 8771 return false;
8772}
8773
24dfead4 8774/* Handle an "tls_model" attribute; arguments as in
8775 struct attribute_spec.handler. */
8776
8777static tree
1cae46be 8778handle_tls_model_attribute (tree *node, tree name, tree args,
9a03a746 8779 int ARG_UNUSED (flags), bool *no_add_attrs)
24dfead4 8780{
1b53eb20 8781 tree id;
24dfead4 8782 tree decl = *node;
1b53eb20 8783 enum tls_model kind;
24dfead4 8784
1b53eb20 8785 *no_add_attrs = true;
8786
f48c7f4a 8787 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
24dfead4 8788 {
9b2d6d13 8789 warning (OPT_Wattributes, "%qE attribute ignored", name);
1b53eb20 8790 return NULL_TREE;
24dfead4 8791 }
24dfead4 8792
1b53eb20 8793 kind = DECL_TLS_MODEL (decl);
8794 id = TREE_VALUE (args);
8795 if (TREE_CODE (id) != STRING_CST)
8796 {
8797 error ("tls_model argument not a string");
8798 return NULL_TREE;
24dfead4 8799 }
8800
1b53eb20 8801 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8802 kind = TLS_MODEL_LOCAL_EXEC;
8803 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8804 kind = TLS_MODEL_INITIAL_EXEC;
8805 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8806 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8807 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8808 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8809 else
8810 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8811
5e68df57 8812 set_decl_tls_model (decl, kind);
24dfead4 8813 return NULL_TREE;
8814}
8815
f8e93a2e 8816/* Handle a "no_instrument_function" attribute; arguments as in
8817 struct attribute_spec.handler. */
8818
8819static tree
1cae46be 8820handle_no_instrument_function_attribute (tree *node, tree name,
9a03a746 8821 tree ARG_UNUSED (args),
8822 int ARG_UNUSED (flags),
09347743 8823 bool *no_add_attrs)
f8e93a2e 8824{
8825 tree decl = *node;
8826
8827 if (TREE_CODE (decl) != FUNCTION_DECL)
8828 {
712d2297 8829 error_at (DECL_SOURCE_LOCATION (decl),
8830 "%qE attribute applies only to functions", name);
f8e93a2e 8831 *no_add_attrs = true;
8832 }
f8e93a2e 8833 else
8834 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8835
8836 return NULL_TREE;
8837}
8838
8839/* Handle a "malloc" attribute; arguments as in
8840 struct attribute_spec.handler. */
8841
8842static tree
9a03a746 8843handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8844 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8845{
a5147fca 8846 if (TREE_CODE (*node) == FUNCTION_DECL
8847 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
f8e93a2e 8848 DECL_IS_MALLOC (*node) = 1;
f8e93a2e 8849 else
8850 {
9b2d6d13 8851 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8852 *no_add_attrs = true;
8853 }
8854
8855 return NULL_TREE;
8856}
8857
4a29c97c 8858/* Handle a "alloc_size" attribute; arguments as in
8859 struct attribute_spec.handler. */
8860
8861static tree
8862handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8863 int ARG_UNUSED (flags), bool *no_add_attrs)
8864{
2802826e 8865 unsigned arg_count = type_num_arguments (*node);
4a29c97c 8866 for (; args; args = TREE_CHAIN (args))
8867 {
8868 tree position = TREE_VALUE (args);
caf62483 8869 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8870 && TREE_CODE (position) != FUNCTION_DECL)
8871 position = default_conversion (position);
4a29c97c 8872
237e78b1 8873 if (!tree_fits_uhwi_p (position)
8874 || !arg_count
8875 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
4a29c97c 8876 {
48e1416a 8877 warning (OPT_Wattributes,
4a29c97c 8878 "alloc_size parameter outside range");
8879 *no_add_attrs = true;
8880 return NULL_TREE;
8881 }
8882 }
8883 return NULL_TREE;
8884}
8885
237e78b1 8886/* Handle a "alloc_align" attribute; arguments as in
8887 struct attribute_spec.handler. */
8888
8889static tree
8890handle_alloc_align_attribute (tree *node, tree, tree args, int,
8891 bool *no_add_attrs)
8892{
8893 unsigned arg_count = type_num_arguments (*node);
8894 tree position = TREE_VALUE (args);
8895 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8896 position = default_conversion (position);
8897
8898 if (!tree_fits_uhwi_p (position)
8899 || !arg_count
8900 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8901 {
8902 warning (OPT_Wattributes,
8903 "alloc_align parameter outside range");
8904 *no_add_attrs = true;
8905 return NULL_TREE;
8906 }
8907 return NULL_TREE;
8908}
8909
8910/* Handle a "assume_aligned" attribute; arguments as in
8911 struct attribute_spec.handler. */
8912
8913static tree
8914handle_assume_aligned_attribute (tree *, tree, tree args, int,
8915 bool *no_add_attrs)
8916{
8917 for (; args; args = TREE_CHAIN (args))
8918 {
8919 tree position = TREE_VALUE (args);
8920 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8921 && TREE_CODE (position) != FUNCTION_DECL)
8922 position = default_conversion (position);
8923
8924 if (TREE_CODE (position) != INTEGER_CST)
8925 {
8926 warning (OPT_Wattributes,
8927 "assume_aligned parameter not integer constant");
8928 *no_add_attrs = true;
8929 return NULL_TREE;
8930 }
8931 }
8932 return NULL_TREE;
8933}
8934
8ce86007 8935/* Handle a "fn spec" attribute; arguments as in
8936 struct attribute_spec.handler. */
8937
8938static tree
8939handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8940 tree args, int ARG_UNUSED (flags),
8941 bool *no_add_attrs ATTRIBUTE_UNUSED)
8942{
8943 gcc_assert (args
8944 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8945 && !TREE_CHAIN (args));
8946 return NULL_TREE;
8947}
8948
058a1b7a 8949/* Handle a "bnd_variable_size" attribute; arguments as in
8950 struct attribute_spec.handler. */
8951
8952static tree
8953handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8954 int ARG_UNUSED (flags), bool *no_add_attrs)
8955{
8956 if (TREE_CODE (*node) != FIELD_DECL)
8957 {
8958 warning (OPT_Wattributes, "%qE attribute ignored", name);
8959 *no_add_attrs = true;
8960 }
8961
8962 return NULL_TREE;
8963}
8964
8965/* Handle a "bnd_legacy" attribute; arguments as in
8966 struct attribute_spec.handler. */
8967
8968static tree
8969handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8970 int ARG_UNUSED (flags), bool *no_add_attrs)
8971{
8972 if (TREE_CODE (*node) != FUNCTION_DECL)
8973 {
8974 warning (OPT_Wattributes, "%qE attribute ignored", name);
8975 *no_add_attrs = true;
8976 }
8977
8978 return NULL_TREE;
8979}
8980
8981/* Handle a "bnd_instrument" attribute; arguments as in
8982 struct attribute_spec.handler. */
8983
8984static tree
8985handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8986 int ARG_UNUSED (flags), bool *no_add_attrs)
8987{
8988 if (TREE_CODE (*node) != FUNCTION_DECL)
8989 {
8990 warning (OPT_Wattributes, "%qE attribute ignored", name);
8991 *no_add_attrs = true;
8992 }
8993
8994 return NULL_TREE;
8995}
8996
a96c3cc1 8997/* Handle a "warn_unused" attribute; arguments as in
8998 struct attribute_spec.handler. */
8999
9000static tree
9001handle_warn_unused_attribute (tree *node, tree name,
9002 tree args ATTRIBUTE_UNUSED,
9003 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
9004{
9005 if (TYPE_P (*node))
9006 /* Do nothing else, just set the attribute. We'll get at
9007 it later with lookup_attribute. */
9008 ;
9009 else
9010 {
9011 warning (OPT_Wattributes, "%qE attribute ignored", name);
9012 *no_add_attrs = true;
9013 }
9014
9015 return NULL_TREE;
9016}
9017
bc7bff74 9018/* Handle an "omp declare simd" attribute; arguments as in
9019 struct attribute_spec.handler. */
9020
9021static tree
9022handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
9023{
9024 return NULL_TREE;
9025}
9026
c58a4cfd 9027/* Handle a "simd" attribute. */
9028
9029static tree
9030handle_simd_attribute (tree *node, tree name, tree, int, bool *no_add_attrs)
9031{
9032 if (TREE_CODE (*node) == FUNCTION_DECL)
9033 {
9034 if (lookup_attribute ("cilk simd function",
9035 DECL_ATTRIBUTES (*node)) != NULL)
9036 {
9037 error_at (DECL_SOURCE_LOCATION (*node),
9038 "%<__simd__%> attribute cannot be used in the same "
9039 "function marked as a Cilk Plus SIMD-enabled function");
9040 *no_add_attrs = true;
9041 }
9042 else
9043 DECL_ATTRIBUTES (*node)
9044 = tree_cons (get_identifier ("omp declare simd"),
9045 NULL_TREE, DECL_ATTRIBUTES (*node));
9046 }
9047 else
9048 {
9049 warning (OPT_Wattributes, "%qE attribute ignored", name);
9050 *no_add_attrs = true;
9051 }
9052
9053 return NULL_TREE;
9054}
9055
bc7bff74 9056/* Handle an "omp declare target" attribute; arguments as in
9057 struct attribute_spec.handler. */
9058
9059static tree
9060handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
9061{
9062 return NULL_TREE;
9063}
9064
26d1c5ff 9065/* Handle a "returns_twice" attribute; arguments as in
9066 struct attribute_spec.handler. */
9067
9068static tree
9069handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9070 int ARG_UNUSED (flags), bool *no_add_attrs)
9071{
9072 if (TREE_CODE (*node) == FUNCTION_DECL)
9073 DECL_IS_RETURNS_TWICE (*node) = 1;
9074 else
9075 {
9b2d6d13 9076 warning (OPT_Wattributes, "%qE attribute ignored", name);
26d1c5ff 9077 *no_add_attrs = true;
9078 }
9079
9080 return NULL_TREE;
9081}
9082
f8e93a2e 9083/* Handle a "no_limit_stack" attribute; arguments as in
9084 struct attribute_spec.handler. */
9085
9086static tree
1cae46be 9087handle_no_limit_stack_attribute (tree *node, tree name,
9a03a746 9088 tree ARG_UNUSED (args),
9089 int ARG_UNUSED (flags),
09347743 9090 bool *no_add_attrs)
f8e93a2e 9091{
9092 tree decl = *node;
9093
9094 if (TREE_CODE (decl) != FUNCTION_DECL)
9095 {
712d2297 9096 error_at (DECL_SOURCE_LOCATION (decl),
9097 "%qE attribute applies only to functions", name);
f8e93a2e 9098 *no_add_attrs = true;
9099 }
9100 else if (DECL_INITIAL (decl))
9101 {
712d2297 9102 error_at (DECL_SOURCE_LOCATION (decl),
9103 "can%'t set %qE attribute after definition", name);
f8e93a2e 9104 *no_add_attrs = true;
9105 }
9106 else
9107 DECL_NO_LIMIT_STACK (decl) = 1;
9108
9109 return NULL_TREE;
9110}
9111
9112/* Handle a "pure" attribute; arguments as in
9113 struct attribute_spec.handler. */
9114
9115static tree
9a03a746 9116handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9117 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 9118{
9119 if (TREE_CODE (*node) == FUNCTION_DECL)
9c2a0c05 9120 DECL_PURE_P (*node) = 1;
f8e93a2e 9121 /* ??? TODO: Support types. */
9122 else
9123 {
9b2d6d13 9124 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 9125 *no_add_attrs = true;
9126 }
9127
9128 return NULL_TREE;
9129}
9130
4c0315d0 9131/* Digest an attribute list destined for a transactional memory statement.
9132 ALLOWED is the set of attributes that are allowed for this statement;
9133 return the attribute we parsed. Multiple attributes are never allowed. */
9134
9135int
9136parse_tm_stmt_attr (tree attrs, int allowed)
9137{
9138 tree a_seen = NULL;
9139 int m_seen = 0;
9140
9141 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
9142 {
9143 tree a = TREE_PURPOSE (attrs);
9144 int m = 0;
9145
9146 if (is_attribute_p ("outer", a))
9147 m = TM_STMT_ATTR_OUTER;
9148
9149 if ((m & allowed) == 0)
9150 {
9151 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
9152 continue;
9153 }
9154
9155 if (m_seen == 0)
9156 {
9157 a_seen = a;
9158 m_seen = m;
9159 }
9160 else if (m_seen == m)
9161 warning (OPT_Wattributes, "%qE attribute duplicated", a);
9162 else
9163 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
9164 }
9165
9166 return m_seen;
9167}
9168
9169/* Transform a TM attribute name into a maskable integer and back.
9170 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
9171 to how the lack of an attribute is treated. */
9172
9173int
9174tm_attr_to_mask (tree attr)
9175{
9176 if (attr == NULL)
9177 return 0;
9178 if (is_attribute_p ("transaction_safe", attr))
9179 return TM_ATTR_SAFE;
9180 if (is_attribute_p ("transaction_callable", attr))
9181 return TM_ATTR_CALLABLE;
9182 if (is_attribute_p ("transaction_pure", attr))
9183 return TM_ATTR_PURE;
9184 if (is_attribute_p ("transaction_unsafe", attr))
9185 return TM_ATTR_IRREVOCABLE;
9186 if (is_attribute_p ("transaction_may_cancel_outer", attr))
9187 return TM_ATTR_MAY_CANCEL_OUTER;
9188 return 0;
9189}
9190
9191tree
9192tm_mask_to_attr (int mask)
9193{
9194 const char *str;
9195 switch (mask)
9196 {
9197 case TM_ATTR_SAFE:
9198 str = "transaction_safe";
9199 break;
9200 case TM_ATTR_CALLABLE:
9201 str = "transaction_callable";
9202 break;
9203 case TM_ATTR_PURE:
9204 str = "transaction_pure";
9205 break;
9206 case TM_ATTR_IRREVOCABLE:
9207 str = "transaction_unsafe";
9208 break;
9209 case TM_ATTR_MAY_CANCEL_OUTER:
9210 str = "transaction_may_cancel_outer";
9211 break;
9212 default:
9213 gcc_unreachable ();
9214 }
9215 return get_identifier (str);
9216}
9217
9218/* Return the first TM attribute seen in LIST. */
9219
9220tree
9221find_tm_attribute (tree list)
9222{
9223 for (; list ; list = TREE_CHAIN (list))
9224 {
9225 tree name = TREE_PURPOSE (list);
9226 if (tm_attr_to_mask (name) != 0)
9227 return name;
9228 }
9229 return NULL_TREE;
9230}
9231
9232/* Handle the TM attributes; arguments as in struct attribute_spec.handler.
9233 Here we accept only function types, and verify that none of the other
9234 function TM attributes are also applied. */
9235/* ??? We need to accept class types for C++, but not C. This greatly
9236 complicates this function, since we can no longer rely on the extra
9237 processing given by function_type_required. */
9238
9239static tree
9240handle_tm_attribute (tree *node, tree name, tree args,
9241 int flags, bool *no_add_attrs)
9242{
9243 /* Only one path adds the attribute; others don't. */
9244 *no_add_attrs = true;
9245
9246 switch (TREE_CODE (*node))
9247 {
9248 case RECORD_TYPE:
9249 case UNION_TYPE:
9250 /* Only tm_callable and tm_safe apply to classes. */
9251 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
9252 goto ignored;
9253 /* FALLTHRU */
9254
9255 case FUNCTION_TYPE:
9256 case METHOD_TYPE:
9257 {
9258 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
9259 if (old_name == name)
9260 ;
9261 else if (old_name != NULL_TREE)
9262 error ("type was previously declared %qE", old_name);
9263 else
9264 *no_add_attrs = false;
9265 }
9266 break;
9267
6d02e6b2 9268 case FUNCTION_DECL:
9269 {
9270 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
9271 want to set transaction_safe on the type. */
9272 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
9273 if (!TYPE_P (DECL_CONTEXT (*node)))
9274 error_at (DECL_SOURCE_LOCATION (*node),
9275 "%<transaction_safe_dynamic%> may only be specified for "
9276 "a virtual function");
9277 *no_add_attrs = false;
9278 decl_attributes (&TREE_TYPE (*node),
9279 build_tree_list (get_identifier ("transaction_safe"),
9280 NULL_TREE),
9281 0);
9282 break;
9283 }
9284
4c0315d0 9285 case POINTER_TYPE:
9286 {
9287 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
9288 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
9289 {
9290 tree fn_tmp = TREE_TYPE (*node);
9291 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
9292 *node = build_pointer_type (fn_tmp);
9293 break;
9294 }
9295 }
9296 /* FALLTHRU */
9297
9298 default:
9299 /* If a function is next, pass it on to be tried next. */
9300 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
9301 return tree_cons (name, args, NULL);
9302
9303 ignored:
9304 warning (OPT_Wattributes, "%qE attribute ignored", name);
9305 break;
9306 }
9307
9308 return NULL_TREE;
9309}
9310
9311/* Handle the TM_WRAP attribute; arguments as in
9312 struct attribute_spec.handler. */
9313
9314static tree
9315handle_tm_wrap_attribute (tree *node, tree name, tree args,
9316 int ARG_UNUSED (flags), bool *no_add_attrs)
9317{
9318 tree decl = *node;
9319
9320 /* We don't need the attribute even on success, since we
9321 record the entry in an external table. */
9322 *no_add_attrs = true;
9323
9324 if (TREE_CODE (decl) != FUNCTION_DECL)
9325 warning (OPT_Wattributes, "%qE attribute ignored", name);
9326 else
9327 {
9328 tree wrap_decl = TREE_VALUE (args);
3e5a8b00 9329 if (error_operand_p (wrap_decl))
9330 ;
9331 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
b443c459 9332 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
4c0315d0 9333 error ("%qE argument not an identifier", name);
9334 else
9335 {
9336 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
9337 wrap_decl = lookup_name (wrap_decl);
9338 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
9339 {
9340 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
9341 TREE_TYPE (wrap_decl)))
9342 record_tm_replacement (wrap_decl, decl);
9343 else
9344 error ("%qD is not compatible with %qD", wrap_decl, decl);
9345 }
9346 else
cdf34fca 9347 error ("%qE argument is not a function", name);
4c0315d0 9348 }
9349 }
9350
9351 return NULL_TREE;
9352}
9353
9354/* Ignore the given attribute. Used when this attribute may be usefully
9355 overridden by the target, but is not used generically. */
9356
9357static tree
9358ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
9359 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9360 bool *no_add_attrs)
9361{
9362 *no_add_attrs = true;
9363 return NULL_TREE;
9364}
9365
fc09b200 9366/* Handle a "no vops" attribute; arguments as in
9367 struct attribute_spec.handler. */
9368
9369static tree
9370handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
9371 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9372 bool *ARG_UNUSED (no_add_attrs))
9373{
9374 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
9375 DECL_IS_NOVOPS (*node) = 1;
9376 return NULL_TREE;
9377}
9378
f8e93a2e 9379/* Handle a "deprecated" attribute; arguments as in
9380 struct attribute_spec.handler. */
1cae46be 9381
f8e93a2e 9382static tree
1cae46be 9383handle_deprecated_attribute (tree *node, tree name,
45c4e798 9384 tree args, int flags,
09347743 9385 bool *no_add_attrs)
f8e93a2e 9386{
9387 tree type = NULL_TREE;
9388 int warn = 0;
782858b8 9389 tree what = NULL_TREE;
1cae46be 9390
45c4e798 9391 if (!args)
9392 *no_add_attrs = true;
9393 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
9394 {
9395 error ("deprecated message is not a string");
9396 *no_add_attrs = true;
9397 }
9398
f8e93a2e 9399 if (DECL_P (*node))
9400 {
9401 tree decl = *node;
9402 type = TREE_TYPE (decl);
1cae46be 9403
f8e93a2e 9404 if (TREE_CODE (decl) == TYPE_DECL
9405 || TREE_CODE (decl) == PARM_DECL
b443c459 9406 || VAR_OR_FUNCTION_DECL_P (decl)
40c8d1dd 9407 || TREE_CODE (decl) == FIELD_DECL
5a4c69dd 9408 || TREE_CODE (decl) == CONST_DECL
40c8d1dd 9409 || objc_method_decl (TREE_CODE (decl)))
f8e93a2e 9410 TREE_DEPRECATED (decl) = 1;
9411 else
9412 warn = 1;
9413 }
9414 else if (TYPE_P (*node))
9415 {
9416 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 9417 *node = build_variant_type_copy (*node);
f8e93a2e 9418 TREE_DEPRECATED (*node) = 1;
9419 type = *node;
9420 }
9421 else
9422 warn = 1;
1cae46be 9423
f8e93a2e 9424 if (warn)
9425 {
9426 *no_add_attrs = true;
9427 if (type && TYPE_NAME (type))
9428 {
9429 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
782858b8 9430 what = TYPE_NAME (*node);
f8e93a2e 9431 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9432 && DECL_NAME (TYPE_NAME (type)))
782858b8 9433 what = DECL_NAME (TYPE_NAME (type));
f8e93a2e 9434 }
9435 if (what)
9b2d6d13 9436 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
f8e93a2e 9437 else
9b2d6d13 9438 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 9439 }
9440
9441 return NULL_TREE;
9442}
9443
f8e93a2e 9444/* Handle a "vector_size" attribute; arguments as in
9445 struct attribute_spec.handler. */
9446
9447static tree
1cae46be 9448handle_vector_size_attribute (tree *node, tree name, tree args,
9a03a746 9449 int ARG_UNUSED (flags),
09347743 9450 bool *no_add_attrs)
f8e93a2e 9451{
9452 unsigned HOST_WIDE_INT vecsize, nunits;
3754d046 9453 machine_mode orig_mode;
4917c376 9454 tree type = *node, new_type, size;
f8e93a2e 9455
9456 *no_add_attrs = true;
9457
4917c376 9458 size = TREE_VALUE (args);
3e5a8b00 9459 if (size && TREE_CODE (size) != IDENTIFIER_NODE
9460 && TREE_CODE (size) != FUNCTION_DECL)
caf62483 9461 size = default_conversion (size);
4917c376 9462
e913b5cd 9463 if (!tree_fits_uhwi_p (size))
f8e93a2e 9464 {
9b2d6d13 9465 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 9466 return NULL_TREE;
9467 }
9468
9469 /* Get the vector size (in bytes). */
e913b5cd 9470 vecsize = tree_to_uhwi (size);
f8e93a2e 9471
9472 /* We need to provide for vector pointers, vector arrays, and
9473 functions returning vectors. For example:
9474
9475 __attribute__((vector_size(16))) short *foo;
9476
9477 In this case, the mode is SI, but the type being modified is
9478 HI, so we need to look further. */
9479
9480 while (POINTER_TYPE_P (type)
9481 || TREE_CODE (type) == FUNCTION_TYPE
5bfb0742 9482 || TREE_CODE (type) == METHOD_TYPE
2cb0e5d9 9483 || TREE_CODE (type) == ARRAY_TYPE
9484 || TREE_CODE (type) == OFFSET_TYPE)
f8e93a2e 9485 type = TREE_TYPE (type);
9486
9487 /* Get the mode of the type being modified. */
9488 orig_mode = TYPE_MODE (type);
9489
2cb0e5d9 9490 if ((!INTEGRAL_TYPE_P (type)
9491 && !SCALAR_FLOAT_TYPE_P (type)
9492 && !FIXED_POINT_TYPE_P (type))
cee7491d 9493 || (!SCALAR_FLOAT_MODE_P (orig_mode)
9421ebb9 9494 && GET_MODE_CLASS (orig_mode) != MODE_INT
9495 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
e913b5cd 9496 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7ec31215 9497 || TREE_CODE (type) == BOOLEAN_TYPE)
f8e93a2e 9498 {
782858b8 9499 error ("invalid vector type for attribute %qE", name);
f8e93a2e 9500 return NULL_TREE;
9501 }
9502
e913b5cd 9503 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
39cc3e6d 9504 {
9505 error ("vector size not an integral multiple of component size");
9506 return NULL;
9507 }
9508
9509 if (vecsize == 0)
9510 {
9511 error ("zero vector size");
9512 return NULL;
9513 }
9514
f8e93a2e 9515 /* Calculate how many units fit in the vector. */
e913b5cd 9516 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
83e2a11b 9517 if (nunits & (nunits - 1))
f8e93a2e 9518 {
83e2a11b 9519 error ("number of components of the vector not a power of two");
f8e93a2e 9520 return NULL_TREE;
9521 }
9522
83e2a11b 9523 new_type = build_vector_type (type, nunits);
f8e93a2e 9524
9525 /* Build back pointers if needed. */
d991e6e8 9526 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
f8e93a2e 9527
9528 return NULL_TREE;
9529}
9530
dbf6c367 9531/* Handle the "nonnull" attribute. */
9532static tree
9a03a746 9533handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9534 tree args, int ARG_UNUSED (flags),
09347743 9535 bool *no_add_attrs)
dbf6c367 9536{
9537 tree type = *node;
9538 unsigned HOST_WIDE_INT attr_arg_num;
9539
9540 /* If no arguments are specified, all pointer arguments should be
d716ce75 9541 non-null. Verify a full prototype is given so that the arguments
dbf6c367 9542 will have the correct types when we actually check them later. */
84166705 9543 if (!args)
dbf6c367 9544 {
a36cf284 9545 if (!prototype_p (type))
dbf6c367 9546 {
9547 error ("nonnull attribute without arguments on a non-prototype");
4ee9c684 9548 *no_add_attrs = true;
dbf6c367 9549 }
9550 return NULL_TREE;
9551 }
9552
9553 /* Argument list specified. Verify that each argument number references
9554 a pointer argument. */
caf62483 9555 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
dbf6c367 9556 {
4ee9c684 9557 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
dbf6c367 9558
caf62483 9559 tree arg = TREE_VALUE (args);
9560 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9561 && TREE_CODE (arg) != FUNCTION_DECL)
9562 arg = default_conversion (arg);
9563
9564 if (!get_nonnull_operand (arg, &arg_num))
dbf6c367 9565 {
07e3a3d2 9566 error ("nonnull argument has invalid operand number (argument %lu)",
dbf6c367 9567 (unsigned long) attr_arg_num);
9568 *no_add_attrs = true;
9569 return NULL_TREE;
9570 }
9571
d0af78c5 9572 if (prototype_p (type))
dbf6c367 9573 {
d0af78c5 9574 function_args_iterator iter;
9575 tree argument;
9576
9577 function_args_iter_init (&iter, type);
9578 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
dbf6c367 9579 {
d0af78c5 9580 argument = function_args_iter_cond (&iter);
9581 if (argument == NULL_TREE || ck_num == arg_num)
dbf6c367 9582 break;
dbf6c367 9583 }
9584
84166705 9585 if (!argument
d0af78c5 9586 || TREE_CODE (argument) == VOID_TYPE)
dbf6c367 9587 {
07e3a3d2 9588 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
dbf6c367 9589 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9590 *no_add_attrs = true;
9591 return NULL_TREE;
9592 }
9593
d0af78c5 9594 if (TREE_CODE (argument) != POINTER_TYPE)
dbf6c367 9595 {
07e3a3d2 9596 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
dbf6c367 9597 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9598 *no_add_attrs = true;
9599 return NULL_TREE;
9600 }
9601 }
9602 }
9603
9604 return NULL_TREE;
9605}
9606
9607/* Check the argument list of a function call for null in argument slots
d01f58f9 9608 that are marked as requiring a non-null pointer argument. The NARGS
9609 arguments are passed in the array ARGARRAY.
9610*/
dbf6c367 9611
9612static void
d01f58f9 9613check_function_nonnull (tree attrs, int nargs, tree *argarray)
dbf6c367 9614{
9ca77b08 9615 tree a;
d01f58f9 9616 int i;
dbf6c367 9617
9ca77b08 9618 attrs = lookup_attribute ("nonnull", attrs);
9619 if (attrs == NULL_TREE)
9620 return;
9621
9622 a = attrs;
9623 /* See if any of the nonnull attributes has no arguments. If so,
9624 then every pointer argument is checked (in which case the check
9625 for pointer type is done in check_nonnull_arg). */
9626 if (TREE_VALUE (a) != NULL_TREE)
9627 do
9628 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9629 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9630
9631 if (a != NULL_TREE)
9632 for (i = 0; i < nargs; i++)
9633 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9634 i + 1);
9635 else
dbf6c367 9636 {
9ca77b08 9637 /* Walk the argument list. If we encounter an argument number we
9638 should check for non-null, do it. */
9639 for (i = 0; i < nargs; i++)
dbf6c367 9640 {
9ca77b08 9641 for (a = attrs; ; a = TREE_CHAIN (a))
4ee9c684 9642 {
9ca77b08 9643 a = lookup_attribute ("nonnull", a);
9644 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9645 break;
4ee9c684 9646 }
9ca77b08 9647
9648 if (a != NULL_TREE)
9649 check_function_arguments_recurse (check_nonnull_arg, NULL,
9650 argarray[i], i + 1);
dbf6c367 9651 }
9652 }
9653}
9654
50ca527f 9655/* Check that the Nth argument of a function call (counting backwards
d01f58f9 9656 from the end) is a (pointer)0. The NARGS arguments are passed in the
9657 array ARGARRAY. */
bf6c8de0 9658
9659static void
774e9d58 9660check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
bf6c8de0 9661{
774e9d58 9662 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
bf6c8de0 9663
9664 if (attr)
9665 {
d01f58f9 9666 int len = 0;
9667 int pos = 0;
9668 tree sentinel;
774e9d58 9669 function_args_iterator iter;
9670 tree t;
a0c938f0 9671
d01f58f9 9672 /* Skip over the named arguments. */
774e9d58 9673 FOREACH_FUNCTION_ARGS (fntype, t, iter)
a0c938f0 9674 {
774e9d58 9675 if (len == nargs)
9676 break;
d01f58f9 9677 len++;
9678 }
50ca527f 9679
d01f58f9 9680 if (TREE_VALUE (attr))
9681 {
9682 tree p = TREE_VALUE (TREE_VALUE (attr));
f9ae6f95 9683 pos = TREE_INT_CST_LOW (p);
d01f58f9 9684 }
50ca527f 9685
d01f58f9 9686 /* The sentinel must be one of the varargs, i.e.
9687 in position >= the number of fixed arguments. */
9688 if ((nargs - 1 - pos) < len)
9689 {
77a357e3 9690 warning (OPT_Wformat_,
d01f58f9 9691 "not enough variable arguments to fit a sentinel");
9692 return;
bf6c8de0 9693 }
d01f58f9 9694
9695 /* Validate the sentinel. */
9696 sentinel = argarray[nargs - 1 - pos];
9697 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9698 || !integer_zerop (sentinel))
9699 /* Although __null (in C++) is only an integer we allow it
9700 nevertheless, as we are guaranteed that it's exactly
9701 as wide as a pointer, and we don't want to force
9702 users to cast the NULL they have written there.
9703 We warn with -Wstrict-null-sentinel, though. */
9704 && (warn_strict_null_sentinel || null_node != sentinel))
77a357e3 9705 warning (OPT_Wformat_, "missing sentinel in function call");
bf6c8de0 9706 }
9707}
9708
dbf6c367 9709/* Helper for check_function_nonnull; given a list of operands which
9710 must be non-null in ARGS, determine if operand PARAM_NUM should be
9711 checked. */
9712
9713static bool
1cae46be 9714nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
dbf6c367 9715{
4ee9c684 9716 unsigned HOST_WIDE_INT arg_num = 0;
dbf6c367 9717
9718 for (; args; args = TREE_CHAIN (args))
9719 {
231bd014 9720 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9721
9722 gcc_assert (found);
dbf6c367 9723
9724 if (arg_num == param_num)
9725 return true;
9726 }
9727 return false;
9728}
9729
9730/* Check that the function argument PARAM (which is operand number
9731 PARAM_NUM) is non-null. This is called by check_function_nonnull
9732 via check_function_arguments_recurse. */
9733
9734static void
9a03a746 9735check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
1cae46be 9736 unsigned HOST_WIDE_INT param_num)
dbf6c367 9737{
9738 /* Just skip checking the argument if it's not a pointer. This can
9739 happen if the "nonnull" attribute was given without an operand
9740 list (which means to check every pointer argument). */
9741
9742 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9743 return;
9744
9745 if (integer_zerop (param))
155b601b 9746 warning (OPT_Wnonnull, "null argument where non-null required "
9747 "(argument %lu)", (unsigned long) param_num);
dbf6c367 9748}
9749
9750/* Helper for nonnull attribute handling; fetch the operand number
9751 from the attribute argument list. */
9752
9753static bool
1cae46be 9754get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
dbf6c367 9755{
e913b5cd 9756 /* Verify the arg number is a small constant. */
e1d65c9f 9757 if (tree_fits_uhwi_p (arg_num_expr))
e913b5cd 9758 {
f9ae6f95 9759 *valp = TREE_INT_CST_LOW (arg_num_expr);
e913b5cd 9760 return true;
9761 }
9762 else
dbf6c367 9763 return false;
dbf6c367 9764}
fa987697 9765
9766/* Handle a "nothrow" attribute; arguments as in
9767 struct attribute_spec.handler. */
9768
9769static tree
9a03a746 9770handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9771 int ARG_UNUSED (flags), bool *no_add_attrs)
fa987697 9772{
9773 if (TREE_CODE (*node) == FUNCTION_DECL)
9774 TREE_NOTHROW (*node) = 1;
9775 /* ??? TODO: Support types. */
9776 else
9777 {
9b2d6d13 9778 warning (OPT_Wattributes, "%qE attribute ignored", name);
fa987697 9779 *no_add_attrs = true;
9780 }
9781
9782 return NULL_TREE;
9783}
7acb29a3 9784
9785/* Handle a "cleanup" attribute; arguments as in
9786 struct attribute_spec.handler. */
9787
9788static tree
1cae46be 9789handle_cleanup_attribute (tree *node, tree name, tree args,
9a03a746 9790 int ARG_UNUSED (flags), bool *no_add_attrs)
7acb29a3 9791{
9792 tree decl = *node;
9793 tree cleanup_id, cleanup_decl;
9794
9795 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9796 for global destructors in C++. This requires infrastructure that
9797 we don't have generically at the moment. It's also not a feature
9798 we'd be missing too much, since we do have attribute constructor. */
f48c7f4a 9799 if (!VAR_P (decl) || TREE_STATIC (decl))
7acb29a3 9800 {
9b2d6d13 9801 warning (OPT_Wattributes, "%qE attribute ignored", name);
7acb29a3 9802 *no_add_attrs = true;
9803 return NULL_TREE;
9804 }
9805
9806 /* Verify that the argument is a function in scope. */
9807 /* ??? We could support pointers to functions here as well, if
9808 that was considered desirable. */
9809 cleanup_id = TREE_VALUE (args);
9810 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9811 {
07e3a3d2 9812 error ("cleanup argument not an identifier");
7acb29a3 9813 *no_add_attrs = true;
9814 return NULL_TREE;
9815 }
d1c41717 9816 cleanup_decl = lookup_name (cleanup_id);
7acb29a3 9817 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9818 {
07e3a3d2 9819 error ("cleanup argument not a function");
7acb29a3 9820 *no_add_attrs = true;
9821 return NULL_TREE;
9822 }
9823
1cae46be 9824 /* That the function has proper type is checked with the
7acb29a3 9825 eventual call to build_function_call. */
9826
9827 return NULL_TREE;
9828}
8a8cdb8d 9829
9830/* Handle a "warn_unused_result" attribute. No special handling. */
9831
9832static tree
9833handle_warn_unused_result_attribute (tree *node, tree name,
9a03a746 9834 tree ARG_UNUSED (args),
9835 int ARG_UNUSED (flags), bool *no_add_attrs)
8a8cdb8d 9836{
9837 /* Ignore the attribute for functions not returning any value. */
9838 if (VOID_TYPE_P (TREE_TYPE (*node)))
9839 {
9b2d6d13 9840 warning (OPT_Wattributes, "%qE attribute ignored", name);
8a8cdb8d 9841 *no_add_attrs = true;
9842 }
9843
9844 return NULL_TREE;
9845}
bf6c8de0 9846
9847/* Handle a "sentinel" attribute. */
9848
9849static tree
50ca527f 9850handle_sentinel_attribute (tree *node, tree name, tree args,
bf6c8de0 9851 int ARG_UNUSED (flags), bool *no_add_attrs)
9852{
a36cf284 9853 if (!prototype_p (*node))
bf6c8de0 9854 {
9b2d6d13 9855 warning (OPT_Wattributes,
9856 "%qE attribute requires prototypes with named arguments", name);
bf6c8de0 9857 *no_add_attrs = true;
bf6c8de0 9858 }
50ca527f 9859 else
9860 {
c33080b9 9861 if (!stdarg_p (*node))
a0c938f0 9862 {
9b2d6d13 9863 warning (OPT_Wattributes,
9864 "%qE attribute only applies to variadic functions", name);
50ca527f 9865 *no_add_attrs = true;
9866 }
9867 }
a0c938f0 9868
50ca527f 9869 if (args)
bf6c8de0 9870 {
50ca527f 9871 tree position = TREE_VALUE (args);
3e5a8b00 9872 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9873 && TREE_CODE (position) != FUNCTION_DECL)
9874 position = default_conversion (position);
50ca527f 9875
3e5a8b00 9876 if (TREE_CODE (position) != INTEGER_CST
9877 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
a0c938f0 9878 {
48e1416a 9879 warning (OPT_Wattributes,
01b54db5 9880 "requested position is not an integer constant");
50ca527f 9881 *no_add_attrs = true;
9882 }
9883 else
a0c938f0 9884 {
50ca527f 9885 if (tree_int_cst_lt (position, integer_zero_node))
9886 {
01b54db5 9887 warning (OPT_Wattributes,
9888 "requested position is less than zero");
50ca527f 9889 *no_add_attrs = true;
9890 }
9891 }
bf6c8de0 9892 }
a0c938f0 9893
bf6c8de0 9894 return NULL_TREE;
9895}
b5c26b42 9896
9897/* Handle a "type_generic" attribute. */
9898
9899static tree
9900handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9901 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9902 bool * ARG_UNUSED (no_add_attrs))
9903{
19fbe3a4 9904 /* Ensure we have a function type. */
9905 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
48e1416a 9906
19fbe3a4 9907 /* Ensure we have a variadic function. */
c33080b9 9908 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
b5c26b42 9909
9910 return NULL_TREE;
9911}
46f8e3b0 9912
24470055 9913/* Handle a "target" attribute. */
46f8e3b0 9914
9915static tree
24470055 9916handle_target_attribute (tree *node, tree name, tree args, int flags,
46f8e3b0 9917 bool *no_add_attrs)
9918{
9919 /* Ensure we have a function type. */
9920 if (TREE_CODE (*node) != FUNCTION_DECL)
9921 {
9922 warning (OPT_Wattributes, "%qE attribute ignored", name);
9923 *no_add_attrs = true;
9924 }
ab50af2a 9925 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
9926 {
9927 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9928 "with %qs attribute", name, "target_clones");
9929 *no_add_attrs = true;
9930 }
46f8e3b0 9931 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
ae0c3984 9932 flags))
46f8e3b0 9933 *no_add_attrs = true;
9934
9935 return NULL_TREE;
9936}
9937
ab50af2a 9938/* Handle a "target_clones" attribute. */
9939
9940static tree
9941handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9942 int ARG_UNUSED (flags), bool *no_add_attrs)
9943{
9944 /* Ensure we have a function type. */
9945 if (TREE_CODE (*node) == FUNCTION_DECL)
9946 {
9947 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
9948 {
9949 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9950 "with %qs attribute", name, "always_inline");
9951 *no_add_attrs = true;
9952 }
9953 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
9954 {
9955 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9956 "with %qs attribute", name, "target");
9957 *no_add_attrs = true;
9958 }
9959 else
9960 /* Do not inline functions with multiple clone targets. */
9961 DECL_UNINLINABLE (*node) = 1;
9962 }
9963 else
9964 {
9965 warning (OPT_Wattributes, "%qE attribute ignored", name);
9966 *no_add_attrs = true;
9967 }
9968 return NULL_TREE;
9969}
9970
46f8e3b0 9971/* Arguments being collected for optimization. */
9972typedef const char *const_char_p; /* For DEF_VEC_P. */
f1f41a6c 9973static GTY(()) vec<const_char_p, va_gc> *optimize_args;
46f8e3b0 9974
9975
9976/* Inner function to convert a TREE_LIST to argv string to parse the optimize
9977 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9978 false for #pragma GCC optimize. */
9979
9980bool
9981parse_optimize_options (tree args, bool attr_p)
9982{
9983 bool ret = true;
9984 unsigned opt_argc;
9985 unsigned i;
2becf397 9986 int saved_flag_strict_aliasing;
46f8e3b0 9987 const char **opt_argv;
615ef0bb 9988 struct cl_decoded_option *decoded_options;
9989 unsigned int decoded_options_count;
46f8e3b0 9990 tree ap;
9991
9992 /* Build up argv vector. Just in case the string is stored away, use garbage
9993 collected strings. */
f1f41a6c 9994 vec_safe_truncate (optimize_args, 0);
9995 vec_safe_push (optimize_args, (const char *) NULL);
46f8e3b0 9996
9997 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9998 {
9999 tree value = TREE_VALUE (ap);
10000
10001 if (TREE_CODE (value) == INTEGER_CST)
10002 {
10003 char buffer[20];
f9ae6f95 10004 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
f1f41a6c 10005 vec_safe_push (optimize_args, ggc_strdup (buffer));
46f8e3b0 10006 }
10007
10008 else if (TREE_CODE (value) == STRING_CST)
10009 {
10010 /* Split string into multiple substrings. */
10011 size_t len = TREE_STRING_LENGTH (value);
10012 char *p = ASTRDUP (TREE_STRING_POINTER (value));
10013 char *end = p + len;
10014 char *comma;
10015 char *next_p = p;
10016
10017 while (next_p != NULL)
10018 {
10019 size_t len2;
10020 char *q, *r;
10021
10022 p = next_p;
10023 comma = strchr (p, ',');
10024 if (comma)
10025 {
10026 len2 = comma - p;
10027 *comma = '\0';
10028 next_p = comma+1;
10029 }
10030 else
10031 {
10032 len2 = end - p;
10033 next_p = NULL;
10034 }
10035
ba72912a 10036 r = q = (char *) ggc_alloc_atomic (len2 + 3);
46f8e3b0 10037
10038 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
10039 options. */
10040 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
10041 {
10042 ret = false;
10043 if (attr_p)
10044 warning (OPT_Wattributes,
ebd7c4c1 10045 "bad option %s to optimize attribute", p);
46f8e3b0 10046 else
10047 warning (OPT_Wpragmas,
e44b0a1f 10048 "bad option %s to pragma attribute", p);
46f8e3b0 10049 continue;
10050 }
10051
10052 if (*p != '-')
10053 {
10054 *r++ = '-';
10055
10056 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
10057 itself is -Os, and any other switch begins with a -f. */
10058 if ((*p >= '0' && *p <= '9')
10059 || (p[0] == 's' && p[1] == '\0'))
10060 *r++ = 'O';
10061 else if (*p != 'O')
10062 *r++ = 'f';
10063 }
10064
10065 memcpy (r, p, len2);
10066 r[len2] = '\0';
f1f41a6c 10067 vec_safe_push (optimize_args, (const char *) q);
46f8e3b0 10068 }
10069
10070 }
10071 }
10072
f1f41a6c 10073 opt_argc = optimize_args->length ();
46f8e3b0 10074 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
10075
10076 for (i = 1; i < opt_argc; i++)
f1f41a6c 10077 opt_argv[i] = (*optimize_args)[i];
46f8e3b0 10078
2becf397 10079 saved_flag_strict_aliasing = flag_strict_aliasing;
10080
46f8e3b0 10081 /* Now parse the options. */
f3f006ad 10082 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
10083 &decoded_options,
10084 &decoded_options_count);
10085 decode_options (&global_options, &global_options_set,
3c6c0e40 10086 decoded_options, decoded_options_count,
10087 input_location, global_dc);
46f8e3b0 10088
4bec06b3 10089 targetm.override_options_after_change();
10090
2becf397 10091 /* Don't allow changing -fstrict-aliasing. */
10092 flag_strict_aliasing = saved_flag_strict_aliasing;
10093
f1f41a6c 10094 optimize_args->truncate (0);
46f8e3b0 10095 return ret;
10096}
10097
10098/* For handling "optimize" attribute. arguments as in
10099 struct attribute_spec.handler. */
10100
10101static tree
10102handle_optimize_attribute (tree *node, tree name, tree args,
10103 int ARG_UNUSED (flags), bool *no_add_attrs)
10104{
10105 /* Ensure we have a function type. */
10106 if (TREE_CODE (*node) != FUNCTION_DECL)
10107 {
10108 warning (OPT_Wattributes, "%qE attribute ignored", name);
10109 *no_add_attrs = true;
10110 }
10111 else
10112 {
10113 struct cl_optimization cur_opts;
10114 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
10115
10116 /* Save current options. */
2c5d2e39 10117 cl_optimization_save (&cur_opts, &global_options);
46f8e3b0 10118
10119 /* If we previously had some optimization options, use them as the
10120 default. */
10121 if (old_opts)
2c5d2e39 10122 cl_optimization_restore (&global_options,
10123 TREE_OPTIMIZATION (old_opts));
46f8e3b0 10124
10125 /* Parse options, and update the vector. */
10126 parse_optimize_options (args, true);
10127 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
55310327 10128 = build_optimization_node (&global_options);
46f8e3b0 10129
10130 /* Restore current options. */
2c5d2e39 10131 cl_optimization_restore (&global_options, &cur_opts);
46f8e3b0 10132 }
10133
10134 return NULL_TREE;
10135}
48b14f50 10136
10137/* Handle a "no_split_stack" attribute. */
10138
10139static tree
10140handle_no_split_stack_attribute (tree *node, tree name,
10141 tree ARG_UNUSED (args),
10142 int ARG_UNUSED (flags),
10143 bool *no_add_attrs)
10144{
10145 tree decl = *node;
10146
10147 if (TREE_CODE (decl) != FUNCTION_DECL)
10148 {
10149 error_at (DECL_SOURCE_LOCATION (decl),
10150 "%qE attribute applies only to functions", name);
10151 *no_add_attrs = true;
10152 }
10153 else if (DECL_INITIAL (decl))
10154 {
10155 error_at (DECL_SOURCE_LOCATION (decl),
10156 "can%'t set %qE attribute after definition", name);
10157 *no_add_attrs = true;
10158 }
10159
10160 return NULL_TREE;
10161}
d7dcba40 10162
10163/* Handle a "returns_nonnull" attribute; arguments as in
10164 struct attribute_spec.handler. */
10165
10166static tree
10167handle_returns_nonnull_attribute (tree *node, tree, tree, int,
10168 bool *no_add_attrs)
10169{
10170 // Even without a prototype we still have a return type we can check.
10171 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
10172 {
10173 error ("returns_nonnull attribute on a function not returning a pointer");
10174 *no_add_attrs = true;
10175 }
10176 return NULL_TREE;
10177}
10178
74691f46 10179/* Handle a "designated_init" attribute; arguments as in
10180 struct attribute_spec.handler. */
10181
10182static tree
10183handle_designated_init_attribute (tree *node, tree name, tree, int,
10184 bool *no_add_attrs)
10185{
10186 if (TREE_CODE (*node) != RECORD_TYPE)
10187 {
10188 error ("%qE attribute is only valid on %<struct%> type", name);
10189 *no_add_attrs = true;
10190 }
10191 return NULL_TREE;
10192}
10193
dbf6c367 10194\f
774e9d58 10195/* Check for valid arguments being passed to a function with FNTYPE.
10196 There are NARGS arguments in the array ARGARRAY. */
dbf6c367 10197void
774e9d58 10198check_function_arguments (const_tree fntype, int nargs, tree *argarray)
dbf6c367 10199{
10200 /* Check for null being passed in a pointer argument that must be
10201 non-null. We also need to do this if format checking is enabled. */
10202
10203 if (warn_nonnull)
774e9d58 10204 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
dbf6c367 10205
10206 /* Check for errors in format strings. */
10207
068bea1e 10208 if (warn_format || warn_suggest_attribute_format)
774e9d58 10209 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
95c90e04 10210
10211 if (warn_format)
774e9d58 10212 check_function_sentinel (fntype, nargs, argarray);
dbf6c367 10213}
10214
10215/* Generic argument checking recursion routine. PARAM is the argument to
10216 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
10217 once the argument is resolved. CTX is context for the callback. */
10218void
1cae46be 10219check_function_arguments_recurse (void (*callback)
10220 (void *, tree, unsigned HOST_WIDE_INT),
10221 void *ctx, tree param,
10222 unsigned HOST_WIDE_INT param_num)
dbf6c367 10223{
72dd6141 10224 if (CONVERT_EXPR_P (param)
c44afe23 10225 && (TYPE_PRECISION (TREE_TYPE (param))
10226 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
dbf6c367 10227 {
10228 /* Strip coercion. */
10229 check_function_arguments_recurse (callback, ctx,
4ee9c684 10230 TREE_OPERAND (param, 0), param_num);
dbf6c367 10231 return;
10232 }
10233
10234 if (TREE_CODE (param) == CALL_EXPR)
10235 {
c2f47e15 10236 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
dbf6c367 10237 tree attrs;
10238 bool found_format_arg = false;
10239
10240 /* See if this is a call to a known internationalization function
10241 that modifies a format arg. Such a function may have multiple
10242 format_arg attributes (for example, ngettext). */
10243
10244 for (attrs = TYPE_ATTRIBUTES (type);
10245 attrs;
10246 attrs = TREE_CHAIN (attrs))
10247 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
10248 {
c2f47e15 10249 tree inner_arg;
dbf6c367 10250 tree format_num_expr;
10251 int format_num;
10252 int i;
c2f47e15 10253 call_expr_arg_iterator iter;
dbf6c367 10254
10255 /* Extract the argument number, which was previously checked
10256 to be valid. */
10257 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
ddb1be65 10258
e913b5cd 10259 format_num = tree_to_uhwi (format_num_expr);
dbf6c367 10260
c2f47e15 10261 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
10262 inner_arg != 0;
10263 inner_arg = next_call_expr_arg (&iter), i++)
dbf6c367 10264 if (i == format_num)
10265 {
10266 check_function_arguments_recurse (callback, ctx,
c2f47e15 10267 inner_arg, param_num);
dbf6c367 10268 found_format_arg = true;
10269 break;
10270 }
10271 }
10272
10273 /* If we found a format_arg attribute and did a recursive check,
10274 we are done with checking this argument. Otherwise, we continue
10275 and this will be considered a non-literal. */
10276 if (found_format_arg)
10277 return;
10278 }
10279
10280 if (TREE_CODE (param) == COND_EXPR)
10281 {
10282 /* Check both halves of the conditional expression. */
10283 check_function_arguments_recurse (callback, ctx,
4ee9c684 10284 TREE_OPERAND (param, 1), param_num);
dbf6c367 10285 check_function_arguments_recurse (callback, ctx,
4ee9c684 10286 TREE_OPERAND (param, 2), param_num);
dbf6c367 10287 return;
10288 }
10289
10290 (*callback) (ctx, param, param_num);
10291}
1f3233d1 10292
60cce472 10293/* Checks for a builtin function FNDECL that the number of arguments
10294 NARGS against the required number REQUIRED and issues an error if
10295 there is a mismatch. Returns true if the number of arguments is
10296 correct, otherwise false. */
d43cee80 10297
10298static bool
60cce472 10299builtin_function_validate_nargs (tree fndecl, int nargs, int required)
d43cee80 10300{
10301 if (nargs < required)
10302 {
60cce472 10303 error_at (input_location,
10304 "not enough arguments to function %qE", fndecl);
d43cee80 10305 return false;
10306 }
10307 else if (nargs > required)
10308 {
60cce472 10309 error_at (input_location,
10310 "too many arguments to function %qE", fndecl);
d43cee80 10311 return false;
10312 }
10313 return true;
10314}
10315
10316/* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
10317 Returns false if there was an error, otherwise true. */
10318
10319bool
10320check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
10321{
10322 if (!DECL_BUILT_IN (fndecl)
10323 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
10324 return true;
10325
10326 switch (DECL_FUNCTION_CODE (fndecl))
10327 {
10328 case BUILT_IN_CONSTANT_P:
60cce472 10329 return builtin_function_validate_nargs (fndecl, nargs, 1);
d43cee80 10330
10331 case BUILT_IN_ISFINITE:
10332 case BUILT_IN_ISINF:
c319d56a 10333 case BUILT_IN_ISINF_SIGN:
d43cee80 10334 case BUILT_IN_ISNAN:
10335 case BUILT_IN_ISNORMAL:
10902624 10336 case BUILT_IN_SIGNBIT:
60cce472 10337 if (builtin_function_validate_nargs (fndecl, nargs, 1))
d43cee80 10338 {
10339 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
10340 {
10341 error ("non-floating-point argument in call to "
10342 "function %qE", fndecl);
10343 return false;
10344 }
10345 return true;
10346 }
10347 return false;
10348
10349 case BUILT_IN_ISGREATER:
10350 case BUILT_IN_ISGREATEREQUAL:
10351 case BUILT_IN_ISLESS:
10352 case BUILT_IN_ISLESSEQUAL:
10353 case BUILT_IN_ISLESSGREATER:
10354 case BUILT_IN_ISUNORDERED:
60cce472 10355 if (builtin_function_validate_nargs (fndecl, nargs, 2))
d43cee80 10356 {
10357 enum tree_code code0, code1;
10358 code0 = TREE_CODE (TREE_TYPE (args[0]));
10359 code1 = TREE_CODE (TREE_TYPE (args[1]));
10360 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
10361 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10362 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
10363 {
10364 error ("non-floating-point arguments in call to "
10365 "function %qE", fndecl);
10366 return false;
10367 }
10368 return true;
10369 }
10370 return false;
10371
19fbe3a4 10372 case BUILT_IN_FPCLASSIFY:
60cce472 10373 if (builtin_function_validate_nargs (fndecl, nargs, 6))
19fbe3a4 10374 {
10375 unsigned i;
48e1416a 10376
19fbe3a4 10377 for (i=0; i<5; i++)
10378 if (TREE_CODE (args[i]) != INTEGER_CST)
10379 {
10380 error ("non-const integer argument %u in call to function %qE",
10381 i+1, fndecl);
10382 return false;
10383 }
10384
10385 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
10386 {
10387 error ("non-floating-point argument in call to function %qE",
10388 fndecl);
10389 return false;
10390 }
10391 return true;
10392 }
10393 return false;
10394
fca0886c 10395 case BUILT_IN_ASSUME_ALIGNED:
10396 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
10397 {
10398 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
10399 {
10400 error ("non-integer argument 3 in call to function %qE", fndecl);
10401 return false;
10402 }
10403 return true;
10404 }
10405 return false;
10406
0c93c8a9 10407 case BUILT_IN_ADD_OVERFLOW:
10408 case BUILT_IN_SUB_OVERFLOW:
10409 case BUILT_IN_MUL_OVERFLOW:
10410 if (builtin_function_validate_nargs (fndecl, nargs, 3))
10411 {
10412 unsigned i;
10413 for (i = 0; i < 2; i++)
10414 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
10415 {
10416 error ("argument %u in call to function %qE does not have "
10417 "integral type", i + 1, fndecl);
10418 return false;
10419 }
10420 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
10421 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
10422 {
10423 error ("argument 3 in call to function %qE does not have "
10424 "pointer to integer type", fndecl);
10425 return false;
10426 }
10427 return true;
10428 }
10429 return false;
10430
d43cee80 10431 default:
10432 return true;
10433 }
10434}
10435
860251be 10436/* Function to help qsort sort FIELD_DECLs by name order. */
10437
10438int
10439field_decl_cmp (const void *x_p, const void *y_p)
10440{
4fd61bc6 10441 const tree *const x = (const tree *const) x_p;
10442 const tree *const y = (const tree *const) y_p;
10443
860251be 10444 if (DECL_NAME (*x) == DECL_NAME (*y))
10445 /* A nontype is "greater" than a type. */
10446 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10447 if (DECL_NAME (*x) == NULL_TREE)
10448 return -1;
10449 if (DECL_NAME (*y) == NULL_TREE)
10450 return 1;
10451 if (DECL_NAME (*x) < DECL_NAME (*y))
10452 return -1;
10453 return 1;
10454}
10455
10456static struct {
10457 gt_pointer_operator new_value;
10458 void *cookie;
10459} resort_data;
10460
10461/* This routine compares two fields like field_decl_cmp but using the
10462pointer operator in resort_data. */
10463
10464static int
10465resort_field_decl_cmp (const void *x_p, const void *y_p)
10466{
4fd61bc6 10467 const tree *const x = (const tree *const) x_p;
10468 const tree *const y = (const tree *const) y_p;
860251be 10469
10470 if (DECL_NAME (*x) == DECL_NAME (*y))
10471 /* A nontype is "greater" than a type. */
10472 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10473 if (DECL_NAME (*x) == NULL_TREE)
10474 return -1;
10475 if (DECL_NAME (*y) == NULL_TREE)
10476 return 1;
10477 {
10478 tree d1 = DECL_NAME (*x);
10479 tree d2 = DECL_NAME (*y);
10480 resort_data.new_value (&d1, resort_data.cookie);
10481 resort_data.new_value (&d2, resort_data.cookie);
10482 if (d1 < d2)
10483 return -1;
10484 }
10485 return 1;
10486}
10487
10488/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10489
10490void
10491resort_sorted_fields (void *obj,
9a03a746 10492 void * ARG_UNUSED (orig_obj),
4ee9c684 10493 gt_pointer_operator new_value,
10494 void *cookie)
860251be 10495{
9a03a746 10496 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
860251be 10497 resort_data.new_value = new_value;
10498 resort_data.cookie = cookie;
10499 qsort (&sf->elts[0], sf->len, sizeof (tree),
4ee9c684 10500 resort_field_decl_cmp);
860251be 10501}
10502
209c9752 10503/* Subroutine of c_parse_error.
10504 Return the result of concatenating LHS and RHS. RHS is really
10505 a string literal, its first character is indicated by RHS_START and
cfee01e3 10506 RHS_SIZE is its length (including the terminating NUL character).
209c9752 10507
10508 The caller is responsible for deleting the returned pointer. */
10509
10510static char *
10511catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10512{
10513 const int lhs_size = strlen (lhs);
10514 char *result = XNEWVEC (char, lhs_size + rhs_size);
10515 strncpy (result, lhs, lhs_size);
10516 strncpy (result + lhs_size, rhs_start, rhs_size);
10517 return result;
10518}
10519
380c6697 10520/* Issue the error given by GMSGID, indicating that it occurred before
92b128ed 10521 TOKEN, which had the associated VALUE. */
10522
10523void
48e1416a 10524c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
ba99525e 10525 tree value, unsigned char token_flags)
92b128ed 10526{
209c9752 10527#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10528
10529 char *message = NULL;
92b128ed 10530
ba99525e 10531 if (token_type == CPP_EOF)
380c6697 10532 message = catenate_messages (gmsgid, " at end of input");
48e1416a 10533 else if (token_type == CPP_CHAR
10534 || token_type == CPP_WCHAR
ba99525e 10535 || token_type == CPP_CHAR16
30b1ba42 10536 || token_type == CPP_CHAR32
10537 || token_type == CPP_UTF8CHAR)
92b128ed 10538 {
f9ae6f95 10539 unsigned int val = TREE_INT_CST_LOW (value);
924bbf02 10540 const char *prefix;
10541
ba99525e 10542 switch (token_type)
924bbf02 10543 {
10544 default:
10545 prefix = "";
10546 break;
10547 case CPP_WCHAR:
10548 prefix = "L";
10549 break;
10550 case CPP_CHAR16:
10551 prefix = "u";
10552 break;
10553 case CPP_CHAR32:
10554 prefix = "U";
10555 break;
30b1ba42 10556 case CPP_UTF8CHAR:
10557 prefix = "u8";
10558 break;
924bbf02 10559 }
10560
92b128ed 10561 if (val <= UCHAR_MAX && ISGRAPH (val))
a0c938f0 10562 message = catenate_messages (gmsgid, " before %s'%c'");
92b128ed 10563 else
a0c938f0 10564 message = catenate_messages (gmsgid, " before %s'\\x%x'");
209c9752 10565
924bbf02 10566 error (message, prefix, val);
209c9752 10567 free (message);
10568 message = NULL;
92b128ed 10569 }
1898176c 10570 else if (token_type == CPP_CHAR_USERDEF
10571 || token_type == CPP_WCHAR_USERDEF
10572 || token_type == CPP_CHAR16_USERDEF
30b1ba42 10573 || token_type == CPP_CHAR32_USERDEF
10574 || token_type == CPP_UTF8CHAR_USERDEF)
1898176c 10575 message = catenate_messages (gmsgid,
10576 " before user-defined character literal");
10577 else if (token_type == CPP_STRING_USERDEF
10578 || token_type == CPP_WSTRING_USERDEF
10579 || token_type == CPP_STRING16_USERDEF
10580 || token_type == CPP_STRING32_USERDEF
10581 || token_type == CPP_UTF8STRING_USERDEF)
10582 message = catenate_messages (gmsgid, " before user-defined string literal");
48e1416a 10583 else if (token_type == CPP_STRING
10584 || token_type == CPP_WSTRING
ba99525e 10585 || token_type == CPP_STRING16
538ba11a 10586 || token_type == CPP_STRING32
10587 || token_type == CPP_UTF8STRING)
380c6697 10588 message = catenate_messages (gmsgid, " before string constant");
ba99525e 10589 else if (token_type == CPP_NUMBER)
380c6697 10590 message = catenate_messages (gmsgid, " before numeric constant");
ba99525e 10591 else if (token_type == CPP_NAME)
209c9752 10592 {
380c6697 10593 message = catenate_messages (gmsgid, " before %qE");
782858b8 10594 error (message, value);
209c9752 10595 free (message);
10596 message = NULL;
10597 }
ba99525e 10598 else if (token_type == CPP_PRAGMA)
b75b98aa 10599 message = catenate_messages (gmsgid, " before %<#pragma%>");
ba99525e 10600 else if (token_type == CPP_PRAGMA_EOL)
b75b98aa 10601 message = catenate_messages (gmsgid, " before end of line");
07b8f133 10602 else if (token_type == CPP_DECLTYPE)
10603 message = catenate_messages (gmsgid, " before %<decltype%>");
ba99525e 10604 else if (token_type < N_TTYPES)
209c9752 10605 {
380c6697 10606 message = catenate_messages (gmsgid, " before %qs token");
ba99525e 10607 error (message, cpp_type2name (token_type, token_flags));
209c9752 10608 free (message);
10609 message = NULL;
10610 }
92b128ed 10611 else
380c6697 10612 error (gmsgid);
209c9752 10613
10614 if (message)
10615 {
10616 error (message);
10617 free (message);
10618 }
a0c938f0 10619#undef catenate_messages
92b128ed 10620}
10621
3a79f5da 10622/* Return the gcc option code associated with the reason for a cpp
10623 message, or 0 if none. */
10624
10625static int
10626c_option_controlling_cpp_error (int reason)
10627{
7ff8db31 10628 const struct cpp_reason_option_codes_t *entry;
3a79f5da 10629
7ff8db31 10630 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
3a79f5da 10631 {
10632 if (entry->reason == reason)
10633 return entry->option_code;
10634 }
10635 return 0;
10636}
10637
7f5f3953 10638/* Callback from cpp_error for PFILE to print diagnostics from the
3a79f5da 10639 preprocessor. The diagnostic is of type LEVEL, with REASON set
10640 to the reason code if LEVEL is represents a warning, at location
f0479000 10641 RICHLOC unless this is after lexing and the compiler's location
10642 should be used instead; MSG is the translated message and AP
7f5f3953 10643 the arguments. Returns true if a diagnostic was emitted, false
10644 otherwise. */
10645
10646bool
3a79f5da 10647c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
f0479000 10648 rich_location *richloc,
7f5f3953 10649 const char *msg, va_list *ap)
10650{
10651 diagnostic_info diagnostic;
10652 diagnostic_t dlevel;
5ae82d58 10653 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
7f5f3953 10654 bool ret;
10655
10656 switch (level)
10657 {
10658 case CPP_DL_WARNING_SYSHDR:
10659 if (flag_no_output)
10660 return false;
5ae82d58 10661 global_dc->dc_warn_system_headers = 1;
7f5f3953 10662 /* Fall through. */
10663 case CPP_DL_WARNING:
10664 if (flag_no_output)
10665 return false;
10666 dlevel = DK_WARNING;
10667 break;
10668 case CPP_DL_PEDWARN:
10669 if (flag_no_output && !flag_pedantic_errors)
10670 return false;
10671 dlevel = DK_PEDWARN;
10672 break;
10673 case CPP_DL_ERROR:
10674 dlevel = DK_ERROR;
10675 break;
10676 case CPP_DL_ICE:
10677 dlevel = DK_ICE;
10678 break;
10679 case CPP_DL_NOTE:
10680 dlevel = DK_NOTE;
10681 break;
ff903809 10682 case CPP_DL_FATAL:
10683 dlevel = DK_FATAL;
10684 break;
7f5f3953 10685 default:
10686 gcc_unreachable ();
10687 }
10688 if (done_lexing)
f0479000 10689 richloc->set_range (0,
10690 source_range::from_location (input_location),
10691 true, true);
7f5f3953 10692 diagnostic_set_info_translated (&diagnostic, msg, ap,
f0479000 10693 richloc, dlevel);
3a79f5da 10694 diagnostic_override_option_index (&diagnostic,
10695 c_option_controlling_cpp_error (reason));
7f5f3953 10696 ret = report_diagnostic (&diagnostic);
10697 if (level == CPP_DL_WARNING_SYSHDR)
5ae82d58 10698 global_dc->dc_warn_system_headers = save_warn_system_headers;
7f5f3953 10699 return ret;
10700}
10701
624d37a6 10702/* Convert a character from the host to the target execution character
10703 set. cpplib handles this, mostly. */
10704
10705HOST_WIDE_INT
10706c_common_to_target_charset (HOST_WIDE_INT c)
10707{
10708 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10709 zero-extended under -fno-signed-char. cpplib insists that characters
10710 and character constants are always unsigned. Hence we must convert
10711 back and forth. */
10712 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10713
10714 uc = cpp_host_to_exec_charset (parse_in, uc);
10715
10716 if (flag_signed_char)
10717 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10718 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10719 else
10720 return uc;
10721}
10722
7549df0d 10723/* Fold an offsetof-like expression. EXPR is a nested sequence of component
10724 references with an INDIRECT_REF of a constant at the bottom; much like the
10725 traditional rendering of offsetof as a macro. Return the folded result. */
af28855b 10726
7549df0d 10727tree
aeaccb75 10728fold_offsetof_1 (tree expr, enum tree_code ctx)
af28855b 10729{
af28855b 10730 tree base, off, t;
aeaccb75 10731 tree_code code = TREE_CODE (expr);
10732 switch (code)
af28855b 10733 {
10734 case ERROR_MARK:
10735 return expr;
10736
6b11d2e3 10737 case VAR_DECL:
10738 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10739 return error_mark_node;
10740
d897f7c2 10741 case CALL_EXPR:
cf1a89a3 10742 case TARGET_EXPR:
d897f7c2 10743 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10744 return error_mark_node;
10745
d897f7c2 10746 case NOP_EXPR:
10747 case INDIRECT_REF:
7549df0d 10748 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
64ed018c 10749 {
10750 error ("cannot apply %<offsetof%> to a non constant address");
10751 return error_mark_node;
10752 }
7549df0d 10753 return TREE_OPERAND (expr, 0);
d897f7c2 10754
af28855b 10755 case COMPONENT_REF:
aeaccb75 10756 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
af28855b 10757 if (base == error_mark_node)
10758 return base;
10759
10760 t = TREE_OPERAND (expr, 1);
10761 if (DECL_C_BIT_FIELD (t))
10762 {
10763 error ("attempt to take address of bit-field structure "
782858b8 10764 "member %qD", t);
af28855b 10765 return error_mark_node;
10766 }
389dd41b 10767 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
e913b5cd 10768 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
389dd41b 10769 / BITS_PER_UNIT));
af28855b 10770 break;
10771
10772 case ARRAY_REF:
aeaccb75 10773 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
af28855b 10774 if (base == error_mark_node)
10775 return base;
10776
10777 t = TREE_OPERAND (expr, 1);
64ed018c 10778
10779 /* Check if the offset goes beyond the upper bound of the array. */
7549df0d 10780 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
e0559d69 10781 {
10782 tree upbound = array_ref_up_bound (expr);
10783 if (upbound != NULL_TREE
10784 && TREE_CODE (upbound) == INTEGER_CST
10785 && !tree_int_cst_equal (upbound,
10786 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10787 {
aeaccb75 10788 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
10789 upbound = size_binop (PLUS_EXPR, upbound,
10790 build_int_cst (TREE_TYPE (upbound), 1));
e0559d69 10791 if (tree_int_cst_lt (upbound, t))
10792 {
10793 tree v;
10794
10795 for (v = TREE_OPERAND (expr, 0);
10796 TREE_CODE (v) == COMPONENT_REF;
10797 v = TREE_OPERAND (v, 0))
10798 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10799 == RECORD_TYPE)
10800 {
1767a056 10801 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10802 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
e0559d69 10803 if (TREE_CODE (fld_chain) == FIELD_DECL)
10804 break;
10805
10806 if (fld_chain)
10807 break;
10808 }
10809 /* Don't warn if the array might be considered a poor
10810 man's flexible array member with a very permissive
10811 definition thereof. */
10812 if (TREE_CODE (v) == ARRAY_REF
10813 || TREE_CODE (v) == COMPONENT_REF)
10814 warning (OPT_Warray_bounds,
10815 "index %E denotes an offset "
10816 "greater than size of %qT",
10817 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10818 }
10819 }
10820 }
7549df0d 10821
10822 t = convert (sizetype, t);
10823 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
af28855b 10824 break;
10825
ede90cc2 10826 case COMPOUND_EXPR:
10827 /* Handle static members of volatile structs. */
10828 t = TREE_OPERAND (expr, 1);
f48c7f4a 10829 gcc_assert (VAR_P (t));
7549df0d 10830 return fold_offsetof_1 (t);
ede90cc2 10831
af28855b 10832 default:
231bd014 10833 gcc_unreachable ();
af28855b 10834 }
10835
7549df0d 10836 return fold_build_pointer_plus (base, off);
af28855b 10837}
10838
7549df0d 10839/* Likewise, but convert it to the return type of offsetof. */
10840
af28855b 10841tree
7549df0d 10842fold_offsetof (tree expr)
af28855b 10843{
7549df0d 10844 return convert (size_type_node, fold_offsetof_1 (expr));
af28855b 10845}
10846
b9bdfa0b 10847/* Warn for A ?: C expressions (with B omitted) where A is a boolean
10848 expression, because B will always be true. */
10849
10850void
10851warn_for_omitted_condop (location_t location, tree cond)
10852{
10853 if (truth_value_p (TREE_CODE (cond)))
10854 warning_at (location, OPT_Wparentheses,
10855 "the omitted middle operand in ?: will always be %<true%>, "
10856 "suggest explicit middle operand");
10857}
10858
a1f90215 10859/* Give an error for storing into ARG, which is 'const'. USE indicates
10860 how ARG was being used. */
10861
10862void
f2697631 10863readonly_error (location_t loc, tree arg, enum lvalue_use use)
a1f90215 10864{
10865 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10866 || use == lv_asm);
10867 /* Using this macro rather than (for example) arrays of messages
10868 ensures that all the format strings are checked at compile
10869 time. */
10870#define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10871 : (use == lv_increment ? (I) \
10872 : (use == lv_decrement ? (D) : (AS))))
10873 if (TREE_CODE (arg) == COMPONENT_REF)
10874 {
10875 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
f2697631 10876 error_at (loc, READONLY_MSG (G_("assignment of member "
10877 "%qD in read-only object"),
10878 G_("increment of member "
10879 "%qD in read-only object"),
10880 G_("decrement of member "
10881 "%qD in read-only object"),
10882 G_("member %qD in read-only object "
10883 "used as %<asm%> output")),
10884 TREE_OPERAND (arg, 1));
a1f90215 10885 else
f2697631 10886 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10887 G_("increment of read-only member %qD"),
10888 G_("decrement of read-only member %qD"),
10889 G_("read-only member %qD used as %<asm%> output")),
10890 TREE_OPERAND (arg, 1));
a1f90215 10891 }
f48c7f4a 10892 else if (VAR_P (arg))
f2697631 10893 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10894 G_("increment of read-only variable %qD"),
10895 G_("decrement of read-only variable %qD"),
10896 G_("read-only variable %qD used as %<asm%> output")),
10897 arg);
a1f90215 10898 else if (TREE_CODE (arg) == PARM_DECL)
f2697631 10899 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10900 G_("increment of read-only parameter %qD"),
10901 G_("decrement of read-only parameter %qD"),
10902 G_("read-only parameter %qD use as %<asm%> output")),
10903 arg);
a1f90215 10904 else if (TREE_CODE (arg) == RESULT_DECL)
10905 {
10906 gcc_assert (c_dialect_cxx ());
f2697631 10907 error_at (loc, READONLY_MSG (G_("assignment of "
10908 "read-only named return value %qD"),
10909 G_("increment of "
10910 "read-only named return value %qD"),
10911 G_("decrement of "
10912 "read-only named return value %qD"),
10913 G_("read-only named return value %qD "
10914 "used as %<asm%>output")),
10915 arg);
a1f90215 10916 }
10917 else if (TREE_CODE (arg) == FUNCTION_DECL)
f2697631 10918 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10919 G_("increment of function %qD"),
10920 G_("decrement of function %qD"),
10921 G_("function %qD used as %<asm%> output")),
10922 arg);
a1f90215 10923 else
f2697631 10924 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10925 G_("increment of read-only location %qE"),
10926 G_("decrement of read-only location %qE"),
10927 G_("read-only location %qE used as %<asm%> output")),
10928 arg);
a1f90215 10929}
10930
e35976b1 10931/* Print an error message for an invalid lvalue. USE says
fdd84b77 10932 how the lvalue is being used and so selects the error message. LOC
10933 is the location for the error. */
ab6bb714 10934
e35976b1 10935void
fdd84b77 10936lvalue_error (location_t loc, enum lvalue_use use)
ab6bb714 10937{
e35976b1 10938 switch (use)
ab6bb714 10939 {
e35976b1 10940 case lv_assign:
fdd84b77 10941 error_at (loc, "lvalue required as left operand of assignment");
e35976b1 10942 break;
10943 case lv_increment:
fdd84b77 10944 error_at (loc, "lvalue required as increment operand");
e35976b1 10945 break;
10946 case lv_decrement:
fdd84b77 10947 error_at (loc, "lvalue required as decrement operand");
e35976b1 10948 break;
10949 case lv_addressof:
fdd84b77 10950 error_at (loc, "lvalue required as unary %<&%> operand");
e35976b1 10951 break;
10952 case lv_asm:
fdd84b77 10953 error_at (loc, "lvalue required in asm statement");
e35976b1 10954 break;
10955 default:
10956 gcc_unreachable ();
ab6bb714 10957 }
ab6bb714 10958}
b1bbc8e5 10959
10960/* Print an error message for an invalid indirection of type TYPE.
10961 ERRSTRING is the name of the operator for the indirection. */
10962
10963void
10964invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10965{
10966 switch (errstring)
10967 {
10968 case RO_NULL:
10969 gcc_assert (c_dialect_cxx ());
10970 error_at (loc, "invalid type argument (have %qT)", type);
10971 break;
10972 case RO_ARRAY_INDEXING:
10973 error_at (loc,
10974 "invalid type argument of array indexing (have %qT)",
10975 type);
10976 break;
10977 case RO_UNARY_STAR:
10978 error_at (loc,
10979 "invalid type argument of unary %<*%> (have %qT)",
10980 type);
10981 break;
10982 case RO_ARROW:
10983 error_at (loc,
10984 "invalid type argument of %<->%> (have %qT)",
10985 type);
10986 break;
7354a89b 10987 case RO_ARROW_STAR:
10988 error_at (loc,
10989 "invalid type argument of %<->*%> (have %qT)",
10990 type);
10991 break;
b1bbc8e5 10992 case RO_IMPLICIT_CONVERSION:
10993 error_at (loc,
10994 "invalid type argument of implicit conversion (have %qT)",
10995 type);
10996 break;
10997 default:
10998 gcc_unreachable ();
10999 }
11000}
c271bdb2 11001\f
11002/* *PTYPE is an incomplete array. Complete it with a domain based on
11003 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
11004 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
11005 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
11006
11007int
11008complete_array_type (tree *ptype, tree initial_value, bool do_default)
11009{
11010 tree maxindex, type, main_type, elt, unqual_elt;
11011 int failure = 0, quals;
6753bca0 11012 hashval_t hashcode = 0;
f5298614 11013 bool overflow_p = false;
c271bdb2 11014
11015 maxindex = size_zero_node;
11016 if (initial_value)
11017 {
11018 if (TREE_CODE (initial_value) == STRING_CST)
11019 {
11020 int eltsize
11021 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
11022 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
11023 }
11024 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
11025 {
f1f41a6c 11026 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
c271bdb2 11027
f1f41a6c 11028 if (vec_safe_is_empty (v))
c271bdb2 11029 {
11030 if (pedantic)
11031 failure = 3;
7542c3b4 11032 maxindex = ssize_int (-1);
c271bdb2 11033 }
11034 else
11035 {
11036 tree curindex;
c75b4594 11037 unsigned HOST_WIDE_INT cnt;
11038 constructor_elt *ce;
cee43f7e 11039 bool fold_p = false;
c271bdb2 11040
f1f41a6c 11041 if ((*v)[0].index)
f5298614 11042 maxindex = (*v)[0].index, fold_p = true;
11043
c271bdb2 11044 curindex = maxindex;
11045
f1f41a6c 11046 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
c271bdb2 11047 {
cee43f7e 11048 bool curfold_p = false;
c75b4594 11049 if (ce->index)
cee43f7e 11050 curindex = ce->index, curfold_p = true;
c271bdb2 11051 else
cee43f7e 11052 {
11053 if (fold_p)
f5298614 11054 {
11055 /* Since we treat size types now as ordinary
11056 unsigned types, we need an explicit overflow
11057 check. */
11058 tree orig = curindex;
11059 curindex = fold_convert (sizetype, curindex);
11060 overflow_p |= tree_int_cst_lt (curindex, orig);
11061 }
389dd41b 11062 curindex = size_binop (PLUS_EXPR, curindex,
11063 size_one_node);
cee43f7e 11064 }
c271bdb2 11065 if (tree_int_cst_lt (maxindex, curindex))
cee43f7e 11066 maxindex = curindex, fold_p = curfold_p;
c271bdb2 11067 }
f5298614 11068 if (fold_p)
11069 {
11070 tree orig = maxindex;
11071 maxindex = fold_convert (sizetype, maxindex);
11072 overflow_p |= tree_int_cst_lt (maxindex, orig);
11073 }
c271bdb2 11074 }
11075 }
11076 else
11077 {
11078 /* Make an error message unless that happened already. */
11079 if (initial_value != error_mark_node)
11080 failure = 1;
11081 }
11082 }
11083 else
11084 {
11085 failure = 2;
11086 if (!do_default)
11087 return failure;
11088 }
11089
11090 type = *ptype;
11091 elt = TREE_TYPE (type);
11092 quals = TYPE_QUALS (strip_array_types (elt));
11093 if (quals == 0)
11094 unqual_elt = elt;
11095 else
6d5d708e 11096 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
c271bdb2 11097
11098 /* Using build_distinct_type_copy and modifying things afterward instead
11099 of using build_array_type to create a new type preserves all of the
11100 TYPE_LANG_FLAG_? bits that the front end may have set. */
11101 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
11102 TREE_TYPE (main_type) = unqual_elt;
783bb57e 11103 TYPE_DOMAIN (main_type)
11104 = build_range_type (TREE_TYPE (maxindex),
11105 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
c271bdb2 11106 layout_type (main_type);
11107
6753bca0 11108 /* Make sure we have the canonical MAIN_TYPE. */
11109 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
48e1416a 11110 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
6753bca0 11111 hashcode);
11112 main_type = type_hash_canon (hashcode, main_type);
11113
796735dc 11114 /* Fix the canonical type. */
11115 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
11116 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
11117 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
11118 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
11119 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
11120 != TYPE_DOMAIN (main_type)))
48e1416a 11121 TYPE_CANONICAL (main_type)
796735dc 11122 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
11123 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
11124 else
11125 TYPE_CANONICAL (main_type) = main_type;
11126
c271bdb2 11127 if (quals == 0)
11128 type = main_type;
11129 else
11130 type = c_build_qualified_type (main_type, quals);
11131
4f5b8f2a 11132 if (COMPLETE_TYPE_P (type)
11133 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
f5298614 11134 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
4f5b8f2a 11135 {
11136 error ("size of array is too large");
11137 /* If we proceed with the array type as it is, we'll eventually
08f817b3 11138 crash in tree_to_[su]hwi(). */
4f5b8f2a 11139 type = error_mark_node;
11140 }
11141
c271bdb2 11142 *ptype = type;
11143 return failure;
11144}
ab6bb714 11145
93426222 11146/* Like c_mark_addressable but don't check register qualifier. */
11147void
11148c_common_mark_addressable_vec (tree t)
11149{
11150 while (handled_component_p (t))
11151 t = TREE_OPERAND (t, 0);
f48c7f4a 11152 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
93426222 11153 return;
11154 TREE_ADDRESSABLE (t) = 1;
11155}
11156
11157
b6a5fc45 11158\f
11159/* Used to help initialize the builtin-types.def table. When a type of
11160 the correct size doesn't exist, use error_mark_node instead of NULL.
11161 The later results in segfaults even when a decl using the type doesn't
11162 get invoked. */
11163
11164tree
11165builtin_type_for_size (int size, bool unsignedp)
11166{
a51edb4c 11167 tree type = c_common_type_for_size (size, unsignedp);
b6a5fc45 11168 return type ? type : error_mark_node;
11169}
11170
11171/* A helper function for resolve_overloaded_builtin in resolving the
11172 overloaded __sync_ builtins. Returns a positive power of 2 if the
11173 first operand of PARAMS is a pointer to a supported data type.
11174 Returns 0 if an error is encountered. */
11175
11176static int
f1f41a6c 11177sync_resolve_size (tree function, vec<tree, va_gc> *params)
b6a5fc45 11178{
11179 tree type;
11180 int size;
11181
f1f41a6c 11182 if (!params)
b6a5fc45 11183 {
11184 error ("too few arguments to function %qE", function);
11185 return 0;
11186 }
11187
f1f41a6c 11188 type = TREE_TYPE ((*params)[0]);
0d284870 11189 if (TREE_CODE (type) == ARRAY_TYPE)
11190 {
11191 /* Force array-to-pointer decay for C++. */
11192 gcc_assert (c_dialect_cxx());
11193 (*params)[0] = default_conversion ((*params)[0]);
11194 type = TREE_TYPE ((*params)[0]);
11195 }
b6a5fc45 11196 if (TREE_CODE (type) != POINTER_TYPE)
11197 goto incompatible;
11198
11199 type = TREE_TYPE (type);
11200 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
11201 goto incompatible;
11202
e913b5cd 11203 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
27213ba3 11204 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
b6a5fc45 11205 return size;
11206
11207 incompatible:
11208 error ("incompatible type for argument %d of %qE", 1, function);
11209 return 0;
11210}
11211
a0c938f0 11212/* A helper function for resolve_overloaded_builtin. Adds casts to
b6a5fc45 11213 PARAMS to make arguments match up with those of FUNCTION. Drops
11214 the variadic arguments at the end. Returns false if some error
11215 was encountered; true on success. */
11216
11217static bool
1cd6e20d 11218sync_resolve_params (location_t loc, tree orig_function, tree function,
f1f41a6c 11219 vec<tree, va_gc> *params, bool orig_format)
b6a5fc45 11220{
d0af78c5 11221 function_args_iterator iter;
b6a5fc45 11222 tree ptype;
b9c74b4d 11223 unsigned int parmnum;
b6a5fc45 11224
d0af78c5 11225 function_args_iter_init (&iter, TREE_TYPE (function));
b6a5fc45 11226 /* We've declared the implementation functions to use "volatile void *"
11227 as the pointer parameter, so we shouldn't get any complaints from the
11228 call to check_function_arguments what ever type the user used. */
d0af78c5 11229 function_args_iter_next (&iter);
f1f41a6c 11230 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
b560fabd 11231 ptype = TYPE_MAIN_VARIANT (ptype);
b6a5fc45 11232
11233 /* For the rest of the values, we need to cast these to FTYPE, so that we
11234 don't get warnings for passing pointer types, etc. */
b9c74b4d 11235 parmnum = 0;
d0af78c5 11236 while (1)
b6a5fc45 11237 {
d0af78c5 11238 tree val, arg_type;
11239
11240 arg_type = function_args_iter_cond (&iter);
11241 /* XXX void_type_node belies the abstraction. */
11242 if (arg_type == void_type_node)
11243 break;
b6a5fc45 11244
b9c74b4d 11245 ++parmnum;
f1f41a6c 11246 if (params->length () <= parmnum)
b6a5fc45 11247 {
1cd6e20d 11248 error_at (loc, "too few arguments to function %qE", orig_function);
b6a5fc45 11249 return false;
11250 }
11251
0f6a7cb7 11252 /* Only convert parameters if arg_type is unsigned integer type with
11253 new format sync routines, i.e. don't attempt to convert pointer
11254 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
11255 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
11256 kinds). */
11257 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
1cd6e20d 11258 {
11259 /* Ideally for the first conversion we'd use convert_for_assignment
11260 so that we get warnings for anything that doesn't match the pointer
11261 type. This isn't portable across the C and C++ front ends atm. */
f1f41a6c 11262 val = (*params)[parmnum];
1cd6e20d 11263 val = convert (ptype, val);
11264 val = convert (arg_type, val);
f1f41a6c 11265 (*params)[parmnum] = val;
1cd6e20d 11266 }
b6a5fc45 11267
d0af78c5 11268 function_args_iter_next (&iter);
b6a5fc45 11269 }
11270
1cd6e20d 11271 /* __atomic routines are not variadic. */
f1f41a6c 11272 if (!orig_format && params->length () != parmnum + 1)
1cd6e20d 11273 {
11274 error_at (loc, "too many arguments to function %qE", orig_function);
11275 return false;
11276 }
11277
b6a5fc45 11278 /* The definition of these primitives is variadic, with the remaining
11279 being "an optional list of variables protected by the memory barrier".
11280 No clue what that's supposed to mean, precisely, but we consider all
11281 call-clobbered variables to be protected so we're safe. */
f1f41a6c 11282 params->truncate (parmnum + 1);
b6a5fc45 11283
11284 return true;
11285}
11286
a0c938f0 11287/* A helper function for resolve_overloaded_builtin. Adds a cast to
b6a5fc45 11288 RESULT to make it match the type of the first pointer argument in
11289 PARAMS. */
11290
11291static tree
1cd6e20d 11292sync_resolve_return (tree first_param, tree result, bool orig_format)
b6a5fc45 11293{
b9c74b4d 11294 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
1cd6e20d 11295 tree rtype = TREE_TYPE (result);
10080eac 11296 ptype = TYPE_MAIN_VARIANT (ptype);
1cd6e20d 11297
11298 /* New format doesn't require casting unless the types are the same size. */
11299 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
11300 return convert (ptype, result);
11301 else
11302 return result;
11303}
11304
11305/* This function verifies the PARAMS to generic atomic FUNCTION.
11306 It returns the size if all the parameters are the same size, otherwise
11307 0 is returned if the parameters are invalid. */
11308
11309static int
f1f41a6c 11310get_atomic_generic_size (location_t loc, tree function,
11311 vec<tree, va_gc> *params)
1cd6e20d 11312{
11313 unsigned int n_param;
11314 unsigned int n_model;
11315 unsigned int x;
11316 int size_0;
11317 tree type_0;
11318
11319 /* Determine the parameter makeup. */
11320 switch (DECL_FUNCTION_CODE (function))
11321 {
11322 case BUILT_IN_ATOMIC_EXCHANGE:
11323 n_param = 4;
11324 n_model = 1;
11325 break;
11326 case BUILT_IN_ATOMIC_LOAD:
11327 case BUILT_IN_ATOMIC_STORE:
11328 n_param = 3;
11329 n_model = 1;
11330 break;
11331 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11332 n_param = 6;
11333 n_model = 2;
11334 break;
11335 default:
1d581089 11336 gcc_unreachable ();
1cd6e20d 11337 }
11338
f1f41a6c 11339 if (vec_safe_length (params) != n_param)
1cd6e20d 11340 {
11341 error_at (loc, "incorrect number of arguments to function %qE", function);
11342 return 0;
11343 }
11344
11345 /* Get type of first parameter, and determine its size. */
f1f41a6c 11346 type_0 = TREE_TYPE ((*params)[0]);
0d284870 11347 if (TREE_CODE (type_0) == ARRAY_TYPE)
11348 {
11349 /* Force array-to-pointer decay for C++. */
11350 gcc_assert (c_dialect_cxx());
11351 (*params)[0] = default_conversion ((*params)[0]);
11352 type_0 = TREE_TYPE ((*params)[0]);
11353 }
1d581089 11354 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
11355 {
11356 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
11357 function);
11358 return 0;
11359 }
11360
11361 /* Types must be compile time constant sizes. */
11362 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
1cd6e20d 11363 {
1d581089 11364 error_at (loc,
11365 "argument 1 of %qE must be a pointer to a constant size type",
11366 function);
1cd6e20d 11367 return 0;
11368 }
1d581089 11369
e913b5cd 11370 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
1cd6e20d 11371
1d581089 11372 /* Zero size objects are not allowed. */
11373 if (size_0 == 0)
11374 {
11375 error_at (loc,
11376 "argument 1 of %qE must be a pointer to a nonzero size object",
11377 function);
11378 return 0;
11379 }
11380
1cd6e20d 11381 /* Check each other parameter is a pointer and the same size. */
11382 for (x = 0; x < n_param - n_model; x++)
11383 {
11384 int size;
f1f41a6c 11385 tree type = TREE_TYPE ((*params)[x]);
a04e8d62 11386 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
1cd6e20d 11387 if (n_param == 6 && x == 3)
11388 continue;
11389 if (!POINTER_TYPE_P (type))
11390 {
11391 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
11392 function);
11393 return 0;
11394 }
1f6be080 11395 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
11396 size = type_size ? tree_to_uhwi (type_size) : 0;
1cd6e20d 11397 if (size != size_0)
11398 {
11399 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
11400 function);
11401 return 0;
11402 }
11403 }
11404
11405 /* Check memory model parameters for validity. */
11406 for (x = n_param - n_model ; x < n_param; x++)
11407 {
f1f41a6c 11408 tree p = (*params)[x];
1cd6e20d 11409 if (TREE_CODE (p) == INTEGER_CST)
11410 {
e913b5cd 11411 int i = tree_to_uhwi (p);
a372f7ca 11412 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
1cd6e20d 11413 {
11414 warning_at (loc, OPT_Winvalid_memory_model,
11415 "invalid memory model argument %d of %qE", x + 1,
11416 function);
1cd6e20d 11417 }
11418 }
11419 else
11420 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
11421 {
11422 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
11423 function);
11424 return 0;
11425 }
11426 }
11427
11428 return size_0;
11429}
11430
11431
11432/* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
11433 at the beginning of the parameter list PARAMS representing the size of the
11434 objects. This is to match the library ABI requirement. LOC is the location
11435 of the function call.
11436 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
11437 returned to allow the external call to be constructed. */
11438
11439static tree
11440add_atomic_size_parameter (unsigned n, location_t loc, tree function,
f1f41a6c 11441 vec<tree, va_gc> *params)
1cd6e20d 11442{
11443 tree size_node;
11444
11445 /* Insert a SIZE_T parameter as the first param. If there isn't
11446 enough space, allocate a new vector and recursively re-build with that. */
f1f41a6c 11447 if (!params->space (1))
1cd6e20d 11448 {
11449 unsigned int z, len;
f1f41a6c 11450 vec<tree, va_gc> *v;
1cd6e20d 11451 tree f;
11452
f1f41a6c 11453 len = params->length ();
11454 vec_alloc (v, len + 1);
5a672e62 11455 v->quick_push (build_int_cst (size_type_node, n));
1cd6e20d 11456 for (z = 0; z < len; z++)
f1f41a6c 11457 v->quick_push ((*params)[z]);
ec761d5a 11458 f = build_function_call_vec (loc, vNULL, function, v, NULL);
f1f41a6c 11459 vec_free (v);
1cd6e20d 11460 return f;
11461 }
11462
11463 /* Add the size parameter and leave as a function call for processing. */
11464 size_node = build_int_cst (size_type_node, n);
f1f41a6c 11465 params->quick_insert (0, size_node);
1cd6e20d 11466 return NULL_TREE;
11467}
11468
11469
a056826c 11470/* Return whether atomic operations for naturally aligned N-byte
11471 arguments are supported, whether inline or through libatomic. */
11472static bool
11473atomic_size_supported_p (int n)
11474{
11475 switch (n)
11476 {
11477 case 1:
11478 case 2:
11479 case 4:
11480 case 8:
11481 return true;
11482
11483 case 16:
11484 return targetm.scalar_mode_supported_p (TImode);
11485
11486 default:
11487 return false;
11488 }
11489}
11490
1cd6e20d 11491/* This will process an __atomic_exchange function call, determine whether it
11492 needs to be mapped to the _N variation, or turned into a library call.
11493 LOC is the location of the builtin call.
11494 FUNCTION is the DECL that has been invoked;
11495 PARAMS is the argument list for the call. The return value is non-null
11496 TRUE is returned if it is translated into the proper format for a call to the
11497 external library, and NEW_RETURN is set the tree for that function.
11498 FALSE is returned if processing for the _N variation is required, and
47ae02b7 11499 NEW_RETURN is set to the return value the result is copied into. */
1cd6e20d 11500static bool
11501resolve_overloaded_atomic_exchange (location_t loc, tree function,
f1f41a6c 11502 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11503{
11504 tree p0, p1, p2, p3;
11505 tree I_type, I_type_ptr;
11506 int n = get_atomic_generic_size (loc, function, params);
11507
1d581089 11508 /* Size of 0 is an error condition. */
11509 if (n == 0)
11510 {
11511 *new_return = error_mark_node;
11512 return true;
11513 }
11514
1cd6e20d 11515 /* If not a lock-free size, change to the library generic format. */
a056826c 11516 if (!atomic_size_supported_p (n))
1cd6e20d 11517 {
11518 *new_return = add_atomic_size_parameter (n, loc, function, params);
11519 return true;
11520 }
11521
11522 /* Otherwise there is a lockfree match, transform the call from:
11523 void fn(T* mem, T* desired, T* return, model)
11524 into
11525 *return = (T) (fn (In* mem, (In) *desired, model)) */
11526
f1f41a6c 11527 p0 = (*params)[0];
11528 p1 = (*params)[1];
11529 p2 = (*params)[2];
11530 p3 = (*params)[3];
1cd6e20d 11531
11532 /* Create pointer to appropriate size. */
11533 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11534 I_type_ptr = build_pointer_type (I_type);
11535
11536 /* Convert object pointer to required type. */
11537 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11538 (*params)[0] = p0;
1cd6e20d 11539 /* Convert new value to required type, and dereference it. */
11540 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11541 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 11542 (*params)[1] = p1;
1cd6e20d 11543
11544 /* Move memory model to the 3rd position, and end param list. */
f1f41a6c 11545 (*params)[2] = p3;
11546 params->truncate (3);
1cd6e20d 11547
11548 /* Convert return pointer and dereference it for later assignment. */
11549 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11550
11551 return false;
b6a5fc45 11552}
11553
1cd6e20d 11554
11555/* This will process an __atomic_compare_exchange function call, determine
11556 whether it needs to be mapped to the _N variation, or turned into a lib call.
11557 LOC is the location of the builtin call.
11558 FUNCTION is the DECL that has been invoked;
11559 PARAMS is the argument list for the call. The return value is non-null
11560 TRUE is returned if it is translated into the proper format for a call to the
11561 external library, and NEW_RETURN is set the tree for that function.
11562 FALSE is returned if processing for the _N variation is required. */
11563
11564static bool
11565resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
f1f41a6c 11566 vec<tree, va_gc> *params,
1cd6e20d 11567 tree *new_return)
11568{
11569 tree p0, p1, p2;
11570 tree I_type, I_type_ptr;
11571 int n = get_atomic_generic_size (loc, function, params);
11572
1d581089 11573 /* Size of 0 is an error condition. */
11574 if (n == 0)
11575 {
11576 *new_return = error_mark_node;
11577 return true;
11578 }
11579
1cd6e20d 11580 /* If not a lock-free size, change to the library generic format. */
a056826c 11581 if (!atomic_size_supported_p (n))
1cd6e20d 11582 {
11583 /* The library generic format does not have the weak parameter, so
11584 remove it from the param list. Since a parameter has been removed,
11585 we can be sure that there is room for the SIZE_T parameter, meaning
11586 there will not be a recursive rebuilding of the parameter list, so
11587 there is no danger this will be done twice. */
11588 if (n > 0)
11589 {
f1f41a6c 11590 (*params)[3] = (*params)[4];
11591 (*params)[4] = (*params)[5];
11592 params->truncate (5);
1cd6e20d 11593 }
11594 *new_return = add_atomic_size_parameter (n, loc, function, params);
11595 return true;
11596 }
11597
11598 /* Otherwise, there is a match, so the call needs to be transformed from:
11599 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11600 into
11601 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
ab2c1de8 11602
f1f41a6c 11603 p0 = (*params)[0];
11604 p1 = (*params)[1];
11605 p2 = (*params)[2];
1cd6e20d 11606
11607 /* Create pointer to appropriate size. */
11608 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11609 I_type_ptr = build_pointer_type (I_type);
11610
11611 /* Convert object pointer to required type. */
11612 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11613 (*params)[0] = p0;
1cd6e20d 11614
11615 /* Convert expected pointer to required type. */
11616 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
f1f41a6c 11617 (*params)[1] = p1;
1cd6e20d 11618
11619 /* Convert desired value to required type, and dereference it. */
11620 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11621 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
f1f41a6c 11622 (*params)[2] = p2;
1cd6e20d 11623
11624 /* The rest of the parameters are fine. NULL means no special return value
11625 processing.*/
11626 *new_return = NULL;
11627 return false;
11628}
11629
11630
11631/* This will process an __atomic_load function call, determine whether it
11632 needs to be mapped to the _N variation, or turned into a library call.
11633 LOC is the location of the builtin call.
11634 FUNCTION is the DECL that has been invoked;
11635 PARAMS is the argument list for the call. The return value is non-null
11636 TRUE is returned if it is translated into the proper format for a call to the
11637 external library, and NEW_RETURN is set the tree for that function.
11638 FALSE is returned if processing for the _N variation is required, and
47ae02b7 11639 NEW_RETURN is set to the return value the result is copied into. */
1cd6e20d 11640
11641static bool
11642resolve_overloaded_atomic_load (location_t loc, tree function,
f1f41a6c 11643 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11644{
11645 tree p0, p1, p2;
11646 tree I_type, I_type_ptr;
11647 int n = get_atomic_generic_size (loc, function, params);
11648
1d581089 11649 /* Size of 0 is an error condition. */
11650 if (n == 0)
11651 {
11652 *new_return = error_mark_node;
11653 return true;
11654 }
11655
1cd6e20d 11656 /* If not a lock-free size, change to the library generic format. */
a056826c 11657 if (!atomic_size_supported_p (n))
1cd6e20d 11658 {
11659 *new_return = add_atomic_size_parameter (n, loc, function, params);
11660 return true;
11661 }
11662
11663 /* Otherwise, there is a match, so the call needs to be transformed from:
11664 void fn(T* mem, T* return, model)
11665 into
11666 *return = (T) (fn ((In *) mem, model)) */
11667
f1f41a6c 11668 p0 = (*params)[0];
11669 p1 = (*params)[1];
11670 p2 = (*params)[2];
1cd6e20d 11671
11672 /* Create pointer to appropriate size. */
11673 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11674 I_type_ptr = build_pointer_type (I_type);
11675
11676 /* Convert object pointer to required type. */
11677 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11678 (*params)[0] = p0;
1cd6e20d 11679
11680 /* Move memory model to the 2nd position, and end param list. */
f1f41a6c 11681 (*params)[1] = p2;
11682 params->truncate (2);
1cd6e20d 11683
11684 /* Convert return pointer and dereference it for later assignment. */
11685 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11686
11687 return false;
11688}
11689
11690
11691/* This will process an __atomic_store function call, determine whether it
11692 needs to be mapped to the _N variation, or turned into a library call.
11693 LOC is the location of the builtin call.
11694 FUNCTION is the DECL that has been invoked;
11695 PARAMS is the argument list for the call. The return value is non-null
11696 TRUE is returned if it is translated into the proper format for a call to the
11697 external library, and NEW_RETURN is set the tree for that function.
11698 FALSE is returned if processing for the _N variation is required, and
47ae02b7 11699 NEW_RETURN is set to the return value the result is copied into. */
1cd6e20d 11700
11701static bool
11702resolve_overloaded_atomic_store (location_t loc, tree function,
f1f41a6c 11703 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11704{
11705 tree p0, p1;
11706 tree I_type, I_type_ptr;
11707 int n = get_atomic_generic_size (loc, function, params);
11708
1d581089 11709 /* Size of 0 is an error condition. */
11710 if (n == 0)
11711 {
11712 *new_return = error_mark_node;
11713 return true;
11714 }
11715
1cd6e20d 11716 /* If not a lock-free size, change to the library generic format. */
a056826c 11717 if (!atomic_size_supported_p (n))
1cd6e20d 11718 {
11719 *new_return = add_atomic_size_parameter (n, loc, function, params);
11720 return true;
11721 }
11722
11723 /* Otherwise, there is a match, so the call needs to be transformed from:
11724 void fn(T* mem, T* value, model)
11725 into
11726 fn ((In *) mem, (In) *value, model) */
11727
f1f41a6c 11728 p0 = (*params)[0];
11729 p1 = (*params)[1];
1cd6e20d 11730
11731 /* Create pointer to appropriate size. */
11732 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11733 I_type_ptr = build_pointer_type (I_type);
11734
11735 /* Convert object pointer to required type. */
11736 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11737 (*params)[0] = p0;
1cd6e20d 11738
11739 /* Convert new value to required type, and dereference it. */
11740 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11741 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 11742 (*params)[1] = p1;
1cd6e20d 11743
11744 /* The memory model is in the right spot already. Return is void. */
11745 *new_return = NULL_TREE;
11746
11747 return false;
11748}
11749
11750
b6a5fc45 11751/* Some builtin functions are placeholders for other expressions. This
11752 function should be called immediately after parsing the call expression
11753 before surrounding code has committed to the type of the expression.
11754
e60a6f7b 11755 LOC is the location of the builtin call.
11756
b6a5fc45 11757 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11758 PARAMS is the argument list for the call. The return value is non-null
11759 when expansion is complete, and null if normal processing should
11760 continue. */
11761
11762tree
f1f41a6c 11763resolve_overloaded_builtin (location_t loc, tree function,
11764 vec<tree, va_gc> *params)
b6a5fc45 11765{
11766 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
1cd6e20d 11767 bool orig_format = true;
11768 tree new_return = NULL_TREE;
11769
65441f6f 11770 switch (DECL_BUILT_IN_CLASS (function))
11771 {
11772 case BUILT_IN_NORMAL:
11773 break;
11774 case BUILT_IN_MD:
11775 if (targetm.resolve_overloaded_builtin)
e60a6f7b 11776 return targetm.resolve_overloaded_builtin (loc, function, params);
65441f6f 11777 else
a0c938f0 11778 return NULL_TREE;
65441f6f 11779 default:
11780 return NULL_TREE;
11781 }
a0c938f0 11782
65441f6f 11783 /* Handle BUILT_IN_NORMAL here. */
b6a5fc45 11784 switch (orig_code)
11785 {
1cd6e20d 11786 case BUILT_IN_ATOMIC_EXCHANGE:
11787 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11788 case BUILT_IN_ATOMIC_LOAD:
11789 case BUILT_IN_ATOMIC_STORE:
11790 {
11791 /* Handle these 4 together so that they can fall through to the next
11792 case if the call is transformed to an _N variant. */
11793 switch (orig_code)
11794 {
11795 case BUILT_IN_ATOMIC_EXCHANGE:
11796 {
11797 if (resolve_overloaded_atomic_exchange (loc, function, params,
11798 &new_return))
11799 return new_return;
11800 /* Change to the _N variant. */
11801 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11802 break;
11803 }
11804
11805 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11806 {
11807 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11808 params,
11809 &new_return))
11810 return new_return;
11811 /* Change to the _N variant. */
11812 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11813 break;
11814 }
11815 case BUILT_IN_ATOMIC_LOAD:
11816 {
11817 if (resolve_overloaded_atomic_load (loc, function, params,
11818 &new_return))
11819 return new_return;
11820 /* Change to the _N variant. */
11821 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11822 break;
11823 }
11824 case BUILT_IN_ATOMIC_STORE:
11825 {
11826 if (resolve_overloaded_atomic_store (loc, function, params,
11827 &new_return))
11828 return new_return;
11829 /* Change to the _N variant. */
11830 orig_code = BUILT_IN_ATOMIC_STORE_N;
11831 break;
11832 }
11833 default:
11834 gcc_unreachable ();
11835 }
11836 /* Fallthrough to the normal processing. */
11837 }
11838 case BUILT_IN_ATOMIC_EXCHANGE_N:
11839 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11840 case BUILT_IN_ATOMIC_LOAD_N:
11841 case BUILT_IN_ATOMIC_STORE_N:
11842 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11843 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11844 case BUILT_IN_ATOMIC_AND_FETCH_N:
11845 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11846 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11847 case BUILT_IN_ATOMIC_OR_FETCH_N:
11848 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11849 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11850 case BUILT_IN_ATOMIC_FETCH_AND_N:
11851 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11852 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11853 case BUILT_IN_ATOMIC_FETCH_OR_N:
11854 {
11855 orig_format = false;
11856 /* Fallthru for parameter processing. */
11857 }
2797f13a 11858 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11859 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11860 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11861 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11862 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11863 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11864 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11865 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11866 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11867 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11868 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11869 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11870 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11871 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11872 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11873 case BUILT_IN_SYNC_LOCK_RELEASE_N:
b6a5fc45 11874 {
11875 int n = sync_resolve_size (function, params);
b9c74b4d 11876 tree new_function, first_param, result;
b9a16870 11877 enum built_in_function fncode;
b6a5fc45 11878
11879 if (n == 0)
11880 return error_mark_node;
11881
b9a16870 11882 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11883 new_function = builtin_decl_explicit (fncode);
1cd6e20d 11884 if (!sync_resolve_params (loc, function, new_function, params,
11885 orig_format))
b6a5fc45 11886 return error_mark_node;
11887
f1f41a6c 11888 first_param = (*params)[0];
ec761d5a 11889 result = build_function_call_vec (loc, vNULL, new_function, params,
11890 NULL);
1cd6e20d 11891 if (result == error_mark_node)
11892 return result;
2797f13a 11893 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
1cd6e20d 11894 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11895 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11896 result = sync_resolve_return (first_param, result, orig_format);
b6a5fc45 11897
1cd6e20d 11898 /* If new_return is set, assign function to that expr and cast the
11899 result to void since the generic interface returned void. */
11900 if (new_return)
11901 {
11902 /* Cast function result from I{1,2,4,8,16} to the required type. */
11903 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11904 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11905 result);
11906 TREE_SIDE_EFFECTS (result) = 1;
11907 protected_set_expr_location (result, loc);
11908 result = convert (void_type_node, result);
11909 }
b6a5fc45 11910 return result;
11911 }
11912
11913 default:
65441f6f 11914 return NULL_TREE;
b6a5fc45 11915 }
11916}
11917
73437615 11918/* vector_types_compatible_elements_p is used in type checks of vectors
11919 values used as operands of binary operators. Where it returns true, and
11920 the other checks of the caller succeed (being vector types in he first
11921 place, and matching number of elements), we can just treat the types
11922 as essentially the same.
11923 Contrast with vector_targets_convertible_p, which is used for vector
11924 pointer types, and vector_types_convertible_p, which will allow
11925 language-specific matches under the control of flag_lax_vector_conversions,
11926 and might still require a conversion. */
11927/* True if vector types T1 and T2 can be inputs to the same binary
11928 operator without conversion.
11929 We don't check the overall vector size here because some of our callers
11930 want to give different error messages when the vectors are compatible
11931 except for the element count. */
11932
491255f5 11933bool
73437615 11934vector_types_compatible_elements_p (tree t1, tree t2)
491255f5 11935{
73437615 11936 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11937 t1 = TREE_TYPE (t1);
11938 t2 = TREE_TYPE (t2);
11939
491255f5 11940 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11941
9421ebb9 11942 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11943 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11944 || c2 == FIXED_POINT_TYPE));
491255f5 11945
73437615 11946 t1 = c_common_signed_type (t1);
11947 t2 = c_common_signed_type (t2);
491255f5 11948 /* Equality works here because c_common_signed_type uses
11949 TYPE_MAIN_VARIANT. */
73437615 11950 if (t1 == t2)
11951 return true;
11952 if (opaque && c1 == c2
11953 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11954 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11955 return true;
11956 return false;
491255f5 11957}
11958
be7350e7 11959/* Check for missing format attributes on function pointers. LTYPE is
11960 the new type or left-hand side type. RTYPE is the old type or
11961 right-hand side type. Returns TRUE if LTYPE is missing the desired
11962 attribute. */
11963
11964bool
11965check_missing_format_attribute (tree ltype, tree rtype)
11966{
11967 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11968 tree ra;
11969
11970 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11971 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11972 break;
11973 if (ra)
11974 {
11975 tree la;
11976 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11977 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11978 break;
11979 return !la;
11980 }
11981 else
11982 return false;
11983}
11984
2840aae4 11985/* Subscripting with type char is likely to lose on a machine where
11986 chars are signed. So warn on any machine, but optionally. Don't
11987 warn for unsigned char since that type is safe. Don't warn for
11988 signed char because anyone who uses that must have done so
11989 deliberately. Furthermore, we reduce the false positive load by
11990 warning only for non-constant value of type char. */
11991
11992void
92b63884 11993warn_array_subscript_with_type_char (location_t loc, tree index)
2840aae4 11994{
11995 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11996 && TREE_CODE (index) != INTEGER_CST)
92b63884 11997 warning_at (loc, OPT_Wchar_subscripts,
11998 "array subscript has type %<char%>");
2840aae4 11999}
12000
e534436e 12001/* Implement -Wparentheses for the unexpected C precedence rules, to
12002 cover cases like x + y << z which readers are likely to
12003 misinterpret. We have seen an expression in which CODE is a binary
82012ffe 12004 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
12005 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
12006 CODE_RIGHT may be ERROR_MARK, which means that that side of the
12007 expression was not formed using a binary or unary operator, or it
12008 was enclosed in parentheses. */
e534436e 12009
12010void
b0e7825e 12011warn_about_parentheses (location_t loc, enum tree_code code,
269f7979 12012 enum tree_code code_left, tree arg_left,
82012ffe 12013 enum tree_code code_right, tree arg_right)
e534436e 12014{
12015 if (!warn_parentheses)
12016 return;
12017
82012ffe 12018 /* This macro tests that the expression ARG with original tree code
12019 CODE appears to be a boolean expression. or the result of folding a
12020 boolean expression. */
12021#define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
12022 (truth_value_p (TREE_CODE (ARG)) \
12023 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
12024 /* Folding may create 0 or 1 integers from other expressions. */ \
12025 || ((CODE) != INTEGER_CST \
12026 && (integer_onep (ARG) || integer_zerop (ARG))))
12027
48e1416a 12028 switch (code)
e534436e 12029 {
82012ffe 12030 case LSHIFT_EXPR:
b0e7825e 12031 if (code_left == PLUS_EXPR)
12032 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12033 "suggest parentheses around %<+%> inside %<<<%>");
12034 else if (code_right == PLUS_EXPR)
12035 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
12036 "suggest parentheses around %<+%> inside %<<<%>");
12037 else if (code_left == MINUS_EXPR)
12038 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12039 "suggest parentheses around %<-%> inside %<<<%>");
12040 else if (code_right == MINUS_EXPR)
12041 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
12042 "suggest parentheses around %<-%> inside %<<<%>");
82012ffe 12043 return;
e534436e 12044
82012ffe 12045 case RSHIFT_EXPR:
b0e7825e 12046 if (code_left == PLUS_EXPR)
12047 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12048 "suggest parentheses around %<+%> inside %<>>%>");
12049 else if (code_right == PLUS_EXPR)
12050 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
12051 "suggest parentheses around %<+%> inside %<>>%>");
12052 else if (code_left == MINUS_EXPR)
12053 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12054 "suggest parentheses around %<-%> inside %<>>%>");
12055 else if (code_right == MINUS_EXPR)
12056 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
12057 "suggest parentheses around %<-%> inside %<>>%>");
82012ffe 12058 return;
e534436e 12059
82012ffe 12060 case TRUTH_ORIF_EXPR:
b0e7825e 12061 if (code_left == TRUTH_ANDIF_EXPR)
12062 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12063 "suggest parentheses around %<&&%> within %<||%>");
12064 else if (code_right == TRUTH_ANDIF_EXPR)
12065 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
12066 "suggest parentheses around %<&&%> within %<||%>");
82012ffe 12067 return;
12068
12069 case BIT_IOR_EXPR:
e534436e 12070 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
b0e7825e 12071 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
12072 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12073 "suggest parentheses around arithmetic in operand of %<|%>");
12074 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
12075 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
12076 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 12077 "suggest parentheses around arithmetic in operand of %<|%>");
e534436e 12078 /* Check cases like x|y==z */
b0e7825e 12079 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
12080 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12081 "suggest parentheses around comparison in operand of %<|%>");
12082 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
12083 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 12084 "suggest parentheses around comparison in operand of %<|%>");
12085 /* Check cases like !x | y */
12086 else if (code_left == TRUTH_NOT_EXPR
12087 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 12088 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12089 "suggest parentheses around operand of "
12090 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
82012ffe 12091 return;
e534436e 12092
82012ffe 12093 case BIT_XOR_EXPR:
e534436e 12094 if (code_left == BIT_AND_EXPR
b0e7825e 12095 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
12096 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12097 "suggest parentheses around arithmetic in operand of %<^%>");
12098 else if (code_right == BIT_AND_EXPR
12099 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
12100 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 12101 "suggest parentheses around arithmetic in operand of %<^%>");
e534436e 12102 /* Check cases like x^y==z */
b0e7825e 12103 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
12104 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12105 "suggest parentheses around comparison in operand of %<^%>");
12106 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
12107 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 12108 "suggest parentheses around comparison in operand of %<^%>");
12109 return;
e534436e 12110
82012ffe 12111 case BIT_AND_EXPR:
b0e7825e 12112 if (code_left == PLUS_EXPR)
12113 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12114 "suggest parentheses around %<+%> in operand of %<&%>");
12115 else if (code_right == PLUS_EXPR)
12116 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 12117 "suggest parentheses around %<+%> in operand of %<&%>");
b0e7825e 12118 else if (code_left == MINUS_EXPR)
12119 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12120 "suggest parentheses around %<-%> in operand of %<&%>");
12121 else if (code_right == MINUS_EXPR)
12122 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 12123 "suggest parentheses around %<-%> in operand of %<&%>");
e534436e 12124 /* Check cases like x&y==z */
b0e7825e 12125 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
12126 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12127 "suggest parentheses around comparison in operand of %<&%>");
12128 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
12129 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 12130 "suggest parentheses around comparison in operand of %<&%>");
12131 /* Check cases like !x & y */
12132 else if (code_left == TRUTH_NOT_EXPR
12133 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 12134 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12135 "suggest parentheses around operand of "
12136 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
82012ffe 12137 return;
e534436e 12138
82012ffe 12139 case EQ_EXPR:
b0e7825e 12140 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
12141 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12142 "suggest parentheses around comparison in operand of %<==%>");
12143 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
12144 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 12145 "suggest parentheses around comparison in operand of %<==%>");
12146 return;
12147 case NE_EXPR:
b0e7825e 12148 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
12149 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12150 "suggest parentheses around comparison in operand of %<!=%>");
12151 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
12152 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 12153 "suggest parentheses around comparison in operand of %<!=%>");
12154 return;
12155
12156 default:
b0e7825e 12157 if (TREE_CODE_CLASS (code) == tcc_comparison)
12158 {
12159 if (TREE_CODE_CLASS (code_left) == tcc_comparison
269f7979 12160 && code_left != NE_EXPR && code_left != EQ_EXPR
12161 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
b0e7825e 12162 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12163 "comparisons like %<X<=Y<=Z%> do not "
12164 "have their mathematical meaning");
12165 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
269f7979 12166 && code_right != NE_EXPR && code_right != EQ_EXPR
b0e7825e 12167 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
12168 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
12169 "comparisons like %<X<=Y<=Z%> do not "
12170 "have their mathematical meaning");
12171 }
82012ffe 12172 return;
6ce0c450 12173 }
82012ffe 12174#undef NOT_A_BOOLEAN_EXPR_P
e534436e 12175}
12176
92fccaaa 12177/* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
12178
12179void
12180warn_for_unused_label (tree label)
12181{
12182 if (!TREE_USED (label))
12183 {
12184 if (DECL_INITIAL (label))
12185 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
12186 else
12187 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
12188 }
12189}
2840aae4 12190
b6889cb0 12191/* Warn for division by zero according to the value of DIVISOR. LOC
12192 is the location of the division operator. */
f092582b 12193
12194void
b6889cb0 12195warn_for_div_by_zero (location_t loc, tree divisor)
f092582b 12196{
9421ebb9 12197 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
12198 about division by zero. Do not issue a warning if DIVISOR has a
f092582b 12199 floating-point type, since we consider 0.0/0.0 a valid way of
12200 generating a NaN. */
48d94ede 12201 if (c_inhibit_evaluation_warnings == 0
9421ebb9 12202 && (integer_zerop (divisor) || fixed_zerop (divisor)))
b6889cb0 12203 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
f092582b 12204}
12205
13869a99 12206/* Subroutine of build_binary_op. Give warnings for comparisons
12207 between signed and unsigned quantities that may fail. Do the
12208 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
12209 so that casts will be considered, but default promotions won't
8e70fb09 12210 be.
12211
12212 LOCATION is the location of the comparison operator.
13869a99 12213
12214 The arguments of this function map directly to local variables
12215 of build_binary_op. */
12216
48e1416a 12217void
8e70fb09 12218warn_for_sign_compare (location_t location,
48e1416a 12219 tree orig_op0, tree orig_op1,
12220 tree op0, tree op1,
13869a99 12221 tree result_type, enum tree_code resultcode)
12222{
12223 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
12224 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
12225 int unsignedp0, unsignedp1;
48e1416a 12226
13869a99 12227 /* In C++, check for comparison of different enum types. */
12228 if (c_dialect_cxx()
12229 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
12230 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
12231 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
895b662f 12232 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
13869a99 12233 {
8e70fb09 12234 warning_at (location,
12235 OPT_Wsign_compare, "comparison between types %qT and %qT",
12236 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
13869a99 12237 }
12238
12239 /* Do not warn if the comparison is being done in a signed type,
12240 since the signed type will only be chosen if it can represent
12241 all the values of the unsigned type. */
12242 if (!TYPE_UNSIGNED (result_type))
12243 /* OK */;
12244 /* Do not warn if both operands are unsigned. */
12245 else if (op0_signed == op1_signed)
12246 /* OK */;
12247 else
12248 {
895b662f 12249 tree sop, uop, base_type;
13869a99 12250 bool ovf;
895b662f 12251
13869a99 12252 if (op0_signed)
12253 sop = orig_op0, uop = orig_op1;
48e1416a 12254 else
13869a99 12255 sop = orig_op1, uop = orig_op0;
12256
48e1416a 12257 STRIP_TYPE_NOPS (sop);
13869a99 12258 STRIP_TYPE_NOPS (uop);
895b662f 12259 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
12260 ? TREE_TYPE (result_type) : result_type);
13869a99 12261
12262 /* Do not warn if the signed quantity is an unsuffixed integer
12263 literal (or some static constant expression involving such
12264 literals or a conditional expression involving such literals)
12265 and it is non-negative. */
12266 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
12267 /* OK */;
12268 /* Do not warn if the comparison is an equality operation, the
12269 unsigned quantity is an integral constant, and it would fit
12270 in the result if the result were signed. */
12271 else if (TREE_CODE (uop) == INTEGER_CST
12272 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
895b662f 12273 && int_fits_type_p (uop, c_common_signed_type (base_type)))
13869a99 12274 /* OK */;
12275 /* In C, do not warn if the unsigned quantity is an enumeration
12276 constant and its maximum value would fit in the result if the
12277 result were signed. */
12278 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
12279 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
12280 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
895b662f 12281 c_common_signed_type (base_type)))
13869a99 12282 /* OK */;
48e1416a 12283 else
8e70fb09 12284 warning_at (location,
48e1416a 12285 OPT_Wsign_compare,
8e70fb09 12286 "comparison between signed and unsigned integer expressions");
13869a99 12287 }
48e1416a 12288
13869a99 12289 /* Warn if two unsigned values are being compared in a size larger
12290 than their original size, and one (and only one) is the result of
12291 a `~' operator. This comparison will always fail.
48e1416a 12292
13869a99 12293 Also warn if one operand is a constant, and the constant does not
12294 have all bits set that are set in the ~ operand when it is
12295 extended. */
12296
7f506bca 12297 op0 = c_common_get_narrower (op0, &unsignedp0);
12298 op1 = c_common_get_narrower (op1, &unsignedp1);
48e1416a 12299
13869a99 12300 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
12301 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
12302 {
12303 if (TREE_CODE (op0) == BIT_NOT_EXPR)
7f506bca 12304 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
13869a99 12305 if (TREE_CODE (op1) == BIT_NOT_EXPR)
7f506bca 12306 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
13869a99 12307
e913b5cd 12308 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
13869a99 12309 {
12310 tree primop;
12311 HOST_WIDE_INT constant, mask;
12312 int unsignedp;
12313 unsigned int bits;
48e1416a 12314
e913b5cd 12315 if (tree_fits_shwi_p (op0))
13869a99 12316 {
12317 primop = op1;
12318 unsignedp = unsignedp1;
e913b5cd 12319 constant = tree_to_shwi (op0);
13869a99 12320 }
12321 else
12322 {
12323 primop = op0;
12324 unsignedp = unsignedp0;
e913b5cd 12325 constant = tree_to_shwi (op1);
13869a99 12326 }
48e1416a 12327
13869a99 12328 bits = TYPE_PRECISION (TREE_TYPE (primop));
12329 if (bits < TYPE_PRECISION (result_type)
12330 && bits < HOST_BITS_PER_LONG && unsignedp)
12331 {
85f5e2ee 12332 mask = (~ (unsigned HOST_WIDE_INT) 0) << bits;
13869a99 12333 if ((mask & constant) != mask)
12334 {
12335 if (constant == 0)
76fdceeb 12336 warning_at (location, OPT_Wsign_compare,
12337 "promoted ~unsigned is always non-zero");
13869a99 12338 else
48e1416a 12339 warning_at (location, OPT_Wsign_compare,
8e70fb09 12340 "comparison of promoted ~unsigned with constant");
13869a99 12341 }
12342 }
12343 }
12344 else if (unsignedp0 && unsignedp1
12345 && (TYPE_PRECISION (TREE_TYPE (op0))
12346 < TYPE_PRECISION (result_type))
12347 && (TYPE_PRECISION (TREE_TYPE (op1))
12348 < TYPE_PRECISION (result_type)))
8e70fb09 12349 warning_at (location, OPT_Wsign_compare,
13869a99 12350 "comparison of promoted ~unsigned with unsigned");
12351 }
12352}
12353
5ba33bf4 12354/* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
12355 type via c_common_type. If -Wdouble-promotion is in use, and the
12356 conditions for warning have been met, issue a warning. GMSGID is
12357 the warning message. It must have two %T specifiers for the type
12358 that was converted (generally "float") and the type to which it was
12359 converted (generally "double), respectively. LOC is the location
12360 to which the awrning should refer. */
12361
12362void
12363do_warn_double_promotion (tree result_type, tree type1, tree type2,
12364 const char *gmsgid, location_t loc)
12365{
12366 tree source_type;
12367
12368 if (!warn_double_promotion)
12369 return;
12370 /* If the conversion will not occur at run-time, there is no need to
12371 warn about it. */
12372 if (c_inhibit_evaluation_warnings)
12373 return;
12374 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
12375 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
12376 return;
12377 if (TYPE_MAIN_VARIANT (type1) == float_type_node
12378 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
12379 source_type = type1;
12380 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
12381 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
12382 source_type = type2;
12383 else
12384 return;
12385 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
12386}
12387
0949f227 12388/* Possibly warn about unused parameters. */
12389
12390void
12391do_warn_unused_parameter (tree fn)
12392{
12393 tree decl;
12394
12395 for (decl = DECL_ARGUMENTS (fn);
12396 decl; decl = DECL_CHAIN (decl))
12397 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
12398 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
12399 && !TREE_NO_WARNING (decl))
12400 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
12401 "unused parameter %qD", decl);
12402}
12403
12404
41771881 12405/* Setup a TYPE_DECL node as a typedef representation.
12406
12407 X is a TYPE_DECL for a typedef statement. Create a brand new
12408 ..._TYPE node (which will be just a variant of the existing
12409 ..._TYPE node with identical properties) and then install X
12410 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
12411
12412 The whole point here is to end up with a situation where each
12413 and every ..._TYPE node the compiler creates will be uniquely
12414 associated with AT MOST one node representing a typedef name.
12415 This way, even though the compiler substitutes corresponding
12416 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
12417 early on, later parts of the compiler can always do the reverse
12418 translation and get back the corresponding typedef name. For
12419 example, given:
ab2c1de8 12420
41771881 12421 typedef struct S MY_TYPE;
12422 MY_TYPE object;
12423
12424 Later parts of the compiler might only know that `object' was of
12425 type `struct S' if it were not for code just below. With this
12426 code however, later parts of the compiler see something like:
12427
12428 struct S' == struct S
12429 typedef struct S' MY_TYPE;
12430 struct S' object;
12431
12432 And they can then deduce (from the node for type struct S') that
12433 the original object declaration was:
12434
12435 MY_TYPE object;
12436
12437 Being able to do this is important for proper support of protoize,
12438 and also for generating precise symbolic debugging information
12439 which takes full account of the programmer's (typedef) vocabulary.
12440
12441 Obviously, we don't want to generate a duplicate ..._TYPE node if
12442 the TYPE_DECL node that we are now processing really represents a
12443 standard built-in type. */
12444
12445void
12446set_underlying_type (tree x)
12447{
12448 if (x == error_mark_node)
12449 return;
12450 if (DECL_IS_BUILTIN (x))
12451 {
12452 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12453 TYPE_NAME (TREE_TYPE (x)) = x;
12454 }
12455 else if (TREE_TYPE (x) != error_mark_node
12456 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12457 {
12458 tree tt = TREE_TYPE (x);
12459 DECL_ORIGINAL_TYPE (x) = tt;
12460 tt = build_variant_type_copy (tt);
12461 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12462 TYPE_NAME (tt) = x;
12463 TREE_USED (tt) = TREE_USED (x);
12464 TREE_TYPE (x) = tt;
12465 }
12466}
12467
1a4c44c5 12468/* Record the types used by the current global variable declaration
12469 being parsed, so that we can decide later to emit their debug info.
12470 Those types are in types_used_by_cur_var_decl, and we are going to
12471 store them in the types_used_by_vars_hash hash table.
12472 DECL is the declaration of the global variable that has been parsed. */
12473
12474void
12475record_types_used_by_current_var_decl (tree decl)
12476{
12477 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12478
f1f41a6c 12479 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
1a4c44c5 12480 {
f1f41a6c 12481 tree type = types_used_by_cur_var_decl->pop ();
aef48c9a 12482 types_used_by_var_decl_insert (type, decl);
1a4c44c5 12483 }
12484}
12485
a4e3ffad 12486/* If DECL is a typedef that is declared in the current function,
12487 record it for the purpose of -Wunused-local-typedefs. */
12488
12489void
12490record_locally_defined_typedef (tree decl)
12491{
12492 struct c_language_function *l;
12493
12494 if (!warn_unused_local_typedefs
12495 || cfun == NULL
12496 /* if this is not a locally defined typedef then we are not
12497 interested. */
12498 || !is_typedef_decl (decl)
12499 || !decl_function_context (decl))
12500 return;
12501
12502 l = (struct c_language_function *) cfun->language;
f1f41a6c 12503 vec_safe_push (l->local_typedefs, decl);
a4e3ffad 12504}
12505
12506/* If T is a TYPE_DECL declared locally, mark it as used. */
12507
12508void
12509maybe_record_typedef_use (tree t)
12510{
12511 if (!is_typedef_decl (t))
12512 return;
12513
12514 TREE_USED (t) = true;
12515}
12516
12517/* Warn if there are some unused locally defined typedefs in the
12518 current function. */
12519
12520void
12521maybe_warn_unused_local_typedefs (void)
12522{
12523 int i;
12524 tree decl;
12525 /* The number of times we have emitted -Wunused-local-typedefs
12526 warnings. If this is different from errorcount, that means some
12527 unrelated errors have been issued. In which case, we'll avoid
12528 emitting "unused-local-typedefs" warnings. */
12529 static int unused_local_typedefs_warn_count;
12530 struct c_language_function *l;
12531
12532 if (cfun == NULL)
12533 return;
12534
12535 if ((l = (struct c_language_function *) cfun->language) == NULL)
12536 return;
12537
12538 if (warn_unused_local_typedefs
12539 && errorcount == unused_local_typedefs_warn_count)
12540 {
f1f41a6c 12541 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
a4e3ffad 12542 if (!TREE_USED (decl))
12543 warning_at (DECL_SOURCE_LOCATION (decl),
12544 OPT_Wunused_local_typedefs,
12545 "typedef %qD locally defined but not used", decl);
12546 unused_local_typedefs_warn_count = errorcount;
12547 }
12548
f1f41a6c 12549 vec_free (l->local_typedefs);
a4e3ffad 12550}
12551
78bf4156 12552/* Warn about boolean expression compared with an integer value different
12553 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12554 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12555 are the operands of the comparison. The caller must ensure that
12556 either operand is a boolean expression. */
12557
12558void
12559maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12560 tree op1)
12561{
12562 if (TREE_CODE_CLASS (code) != tcc_comparison)
12563 return;
12564
12565 tree cst = (TREE_CODE (op0) == INTEGER_CST)
12566 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
12567 if (!cst)
12568 return;
12569
12570 if (!integer_zerop (cst) && !integer_onep (cst))
12571 {
a720ab1c 12572 int sign = (TREE_CODE (op0) == INTEGER_CST
12573 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
78bf4156 12574 if (code == EQ_EXPR
12575 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12576 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12577 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12578 "with boolean expression is always false", cst);
12579 else
12580 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12581 "with boolean expression is always true", cst);
12582 }
a720ab1c 12583 else if (integer_zerop (cst) || integer_onep (cst))
12584 {
14744a16 12585 /* If the non-constant operand isn't of a boolean type, we
12586 don't want to warn here. */
12587 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12588 /* Handle booleans promoted to integers. */
12589 if (CONVERT_EXPR_P (noncst)
12590 && TREE_TYPE (noncst) == integer_type_node
12591 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
12592 /* Warn. */;
12593 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12594 && !truth_value_p (TREE_CODE (noncst)))
12595 return;
a720ab1c 12596 /* Do some magic to get the right diagnostics. */
12597 bool flag = TREE_CODE (op0) == INTEGER_CST;
12598 flag = integer_zerop (cst) ? flag : !flag;
12599 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12600 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12601 "with boolean expression is always true", cst);
12602 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12603 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12604 "with boolean expression is always false", cst);
12605 }
78bf4156 12606}
12607
8d669e79 12608/* Warn if signed left shift overflows. We don't warn
12609 about left-shifting 1 into the sign bit in C++14; cf.
12610 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
12611 LOC is a location of the shift; OP0 and OP1 are the operands.
12612 Return true if an overflow is detected, false otherwise. */
12613
12614bool
12615maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
12616{
12617 if (TREE_CODE (op0) != INTEGER_CST
12618 || TREE_CODE (op1) != INTEGER_CST)
12619 return false;
12620
12621 tree type0 = TREE_TYPE (op0);
12622 unsigned int prec0 = TYPE_PRECISION (type0);
12623
12624 /* Left-hand operand must be signed. */
12625 if (TYPE_UNSIGNED (type0))
12626 return false;
12627
f4809955 12628 unsigned int min_prec = (wi::min_precision (op0, SIGNED)
12629 + TREE_INT_CST_LOW (op1));
8d669e79 12630 /* Handle the left-shifting 1 into the sign bit case. */
f4809955 12631 if (min_prec == prec0 + 1)
8d669e79 12632 {
12633 /* Never warn for C++14 onwards. */
12634 if (cxx_dialect >= cxx14)
12635 return false;
12636 /* Otherwise only if -Wshift-overflow=2. But return
12637 true to signal an overflow for the sake of integer
12638 constant expressions. */
12639 if (warn_shift_overflow < 2)
12640 return true;
12641 }
12642
8d669e79 12643 bool overflowed = min_prec > prec0;
12644 if (overflowed && c_inhibit_evaluation_warnings == 0)
12645 warning_at (loc, OPT_Wshift_overflow_,
12646 "result of %qE requires %u bits to represent, "
12647 "but %qT only has %u bits",
12648 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
12649 min_prec, type0, prec0);
12650
12651 return overflowed;
12652}
12653
f352a3fb 12654/* The C and C++ parsers both use vectors to hold function arguments.
12655 For efficiency, we keep a cache of unused vectors. This is the
12656 cache. */
12657
f1f41a6c 12658typedef vec<tree, va_gc> *tree_gc_vec;
12659static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
f352a3fb 12660
12661/* Return a new vector from the cache. If the cache is empty,
12662 allocate a new vector. These vectors are GC'ed, so it is OK if the
12663 pointer is not released.. */
12664
f1f41a6c 12665vec<tree, va_gc> *
f352a3fb 12666make_tree_vector (void)
12667{
f1f41a6c 12668 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12669 return tree_vector_cache->pop ();
f352a3fb 12670 else
12671 {
f1f41a6c 12672 /* Passing 0 to vec::alloc returns NULL, and our callers require
f352a3fb 12673 that we always return a non-NULL value. The vector code uses
12674 4 when growing a NULL vector, so we do too. */
f1f41a6c 12675 vec<tree, va_gc> *v;
12676 vec_alloc (v, 4);
12677 return v;
f352a3fb 12678 }
12679}
12680
12681/* Release a vector of trees back to the cache. */
12682
12683void
f1f41a6c 12684release_tree_vector (vec<tree, va_gc> *vec)
f352a3fb 12685{
12686 if (vec != NULL)
12687 {
f1f41a6c 12688 vec->truncate (0);
12689 vec_safe_push (tree_vector_cache, vec);
f352a3fb 12690 }
12691}
12692
12693/* Get a new tree vector holding a single tree. */
12694
f1f41a6c 12695vec<tree, va_gc> *
f352a3fb 12696make_tree_vector_single (tree t)
12697{
f1f41a6c 12698 vec<tree, va_gc> *ret = make_tree_vector ();
12699 ret->quick_push (t);
f352a3fb 12700 return ret;
12701}
12702
c66c81be 12703/* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12704
f1f41a6c 12705vec<tree, va_gc> *
c66c81be 12706make_tree_vector_from_list (tree list)
12707{
f1f41a6c 12708 vec<tree, va_gc> *ret = make_tree_vector ();
c66c81be 12709 for (; list; list = TREE_CHAIN (list))
f1f41a6c 12710 vec_safe_push (ret, TREE_VALUE (list));
c66c81be 12711 return ret;
12712}
12713
f352a3fb 12714/* Get a new tree vector which is a copy of an existing one. */
12715
f1f41a6c 12716vec<tree, va_gc> *
12717make_tree_vector_copy (const vec<tree, va_gc> *orig)
f352a3fb 12718{
f1f41a6c 12719 vec<tree, va_gc> *ret;
f352a3fb 12720 unsigned int ix;
12721 tree t;
12722
12723 ret = make_tree_vector ();
f1f41a6c 12724 vec_safe_reserve (ret, vec_safe_length (orig));
12725 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12726 ret->quick_push (t);
f352a3fb 12727 return ret;
12728}
12729
a9ffdd35 12730/* Return true if KEYWORD starts a type specifier. */
12731
12732bool
12733keyword_begins_type_specifier (enum rid keyword)
12734{
12735 switch (keyword)
12736 {
4fba5eb9 12737 case RID_AUTO_TYPE:
a9ffdd35 12738 case RID_INT:
12739 case RID_CHAR:
12740 case RID_FLOAT:
12741 case RID_DOUBLE:
12742 case RID_VOID:
a9ffdd35 12743 case RID_UNSIGNED:
12744 case RID_LONG:
12745 case RID_SHORT:
12746 case RID_SIGNED:
12747 case RID_DFLOAT32:
12748 case RID_DFLOAT64:
12749 case RID_DFLOAT128:
12750 case RID_FRACT:
12751 case RID_ACCUM:
12752 case RID_BOOL:
12753 case RID_WCHAR:
12754 case RID_CHAR16:
12755 case RID_CHAR32:
12756 case RID_SAT:
12757 case RID_COMPLEX:
12758 case RID_TYPEOF:
12759 case RID_STRUCT:
12760 case RID_CLASS:
12761 case RID_UNION:
12762 case RID_ENUM:
12763 return true;
12764 default:
9f75f026 12765 if (keyword >= RID_FIRST_INT_N
12766 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12767 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12768 return true;
a9ffdd35 12769 return false;
12770 }
12771}
12772
12773/* Return true if KEYWORD names a type qualifier. */
12774
12775bool
12776keyword_is_type_qualifier (enum rid keyword)
12777{
12778 switch (keyword)
12779 {
12780 case RID_CONST:
12781 case RID_VOLATILE:
12782 case RID_RESTRICT:
b560fabd 12783 case RID_ATOMIC:
a9ffdd35 12784 return true;
12785 default:
12786 return false;
12787 }
12788}
12789
12790/* Return true if KEYWORD names a storage class specifier.
12791
12792 RID_TYPEDEF is not included in this list despite `typedef' being
12793 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12794 such for syntactic convenience only. */
12795
12796bool
12797keyword_is_storage_class_specifier (enum rid keyword)
12798{
12799 switch (keyword)
12800 {
12801 case RID_STATIC:
12802 case RID_EXTERN:
12803 case RID_REGISTER:
12804 case RID_AUTO:
12805 case RID_MUTABLE:
12806 case RID_THREAD:
12807 return true;
12808 default:
12809 return false;
12810 }
12811}
12812
fad3f658 12813/* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12814
12815static bool
12816keyword_is_function_specifier (enum rid keyword)
12817{
12818 switch (keyword)
12819 {
12820 case RID_INLINE:
985c6e3a 12821 case RID_NORETURN:
fad3f658 12822 case RID_VIRTUAL:
12823 case RID_EXPLICIT:
12824 return true;
12825 default:
12826 return false;
12827 }
12828}
12829
12830/* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12831 declaration-specifier (C99 6.7). */
12832
12833bool
12834keyword_is_decl_specifier (enum rid keyword)
12835{
12836 if (keyword_is_storage_class_specifier (keyword)
12837 || keyword_is_type_qualifier (keyword)
12838 || keyword_is_function_specifier (keyword))
12839 return true;
12840
12841 switch (keyword)
12842 {
12843 case RID_TYPEDEF:
12844 case RID_FRIEND:
12845 case RID_CONSTEXPR:
12846 return true;
12847 default:
12848 return false;
12849 }
12850}
12851
9b88d08d 12852/* Initialize language-specific-bits of tree_contains_struct. */
12853
12854void
12855c_common_init_ts (void)
12856{
12857 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12858 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
3c6d4197 12859 MARK_TS_TYPED (ARRAY_NOTATION_REF);
9b88d08d 12860}
12861
244db24d 12862/* Build a user-defined numeric literal out of an integer constant type VALUE
12863 with identifier SUFFIX. */
12864
12865tree
324ca377 12866build_userdef_literal (tree suffix_id, tree value,
12867 enum overflow_type overflow, tree num_string)
244db24d 12868{
12869 tree literal = make_node (USERDEF_LITERAL);
12870 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12871 USERDEF_LITERAL_VALUE (literal) = value;
324ca377 12872 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
244db24d 12873 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12874 return literal;
12875}
12876
7059d45d 12877/* For vector[index], convert the vector to a
c61ef207 12878 pointer of the underlying type. Return true if the resulting
12879 ARRAY_REF should not be an lvalue. */
12880
12881bool
7059d45d 12882convert_vector_to_pointer_for_subscript (location_t loc,
c61ef207 12883 tree *vecp, tree index)
7059d45d 12884{
c61ef207 12885 bool ret = false;
6290f0db 12886 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
7059d45d 12887 {
12888 tree type = TREE_TYPE (*vecp);
12889 tree type1;
12890
c61ef207 12891 ret = !lvalue_p (*vecp);
7059d45d 12892 if (TREE_CODE (index) == INTEGER_CST)
e913b5cd 12893 if (!tree_fits_uhwi_p (index)
aa59f000 12894 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7059d45d 12895 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12896
c61ef207 12897 if (ret)
12898 {
f9e245b2 12899 tree tmp = create_tmp_var_raw (type);
c61ef207 12900 DECL_SOURCE_LOCATION (tmp) = loc;
12901 *vecp = c_save_expr (*vecp);
12902 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12903 {
12904 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12905 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12906 *vecp
12907 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12908 *vecp, NULL_TREE, NULL_TREE),
12909 non_const);
12910 }
12911 else
12912 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12913 NULL_TREE, NULL_TREE);
12914 SET_EXPR_LOCATION (*vecp, loc);
12915 c_common_mark_addressable_vec (tmp);
12916 }
12917 else
12918 c_common_mark_addressable_vec (*vecp);
7059d45d 12919 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7059d45d 12920 type1 = build_pointer_type (TREE_TYPE (*vecp));
b2ca6510 12921 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12922 if (!ref_all
12923 && !DECL_P (*vecp))
12924 {
12925 /* If the original vector isn't declared may_alias and it
12926 isn't a bare vector look if the subscripting would
12927 alias the vector we subscript, and if not, force ref-all. */
12928 alias_set_type vecset = get_alias_set (*vecp);
12929 alias_set_type sset = get_alias_set (type);
12930 if (!alias_sets_must_conflict_p (sset, vecset)
12931 && !alias_set_subset_of (sset, vecset))
12932 ref_all = true;
12933 }
12934 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
7059d45d 12935 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12936 *vecp = convert (type, *vecp);
12937 }
c61ef207 12938 return ret;
7059d45d 12939}
12940
41ed701a 12941/* Determine which of the operands, if any, is a scalar that needs to be
12942 converted to a vector, for the range of operations. */
12943enum stv_conv
12944scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12945 bool complain)
12946{
12947 tree type0 = TREE_TYPE (op0);
12948 tree type1 = TREE_TYPE (op1);
12949 bool integer_only_op = false;
12950 enum stv_conv ret = stv_firstarg;
12951
6290f0db 12952 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
41ed701a 12953 switch (code)
12954 {
12955 /* Most GENERIC binary expressions require homogeneous arguments.
12956 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12957 argument that is a vector and a second one that is a scalar, so
12958 we never return stv_secondarg for them. */
12959 case RSHIFT_EXPR:
12960 case LSHIFT_EXPR:
12961 if (TREE_CODE (type0) == INTEGER_TYPE
12962 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12963 {
22a75734 12964 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12965 {
12966 if (complain)
12967 error_at (loc, "conversion of scalar %qT to vector %qT "
12968 "involves truncation", type0, type1);
12969 return stv_error;
12970 }
12971 else
12972 return stv_firstarg;
12973 }
12974 break;
12975
12976 case BIT_IOR_EXPR:
12977 case BIT_XOR_EXPR:
12978 case BIT_AND_EXPR:
12979 integer_only_op = true;
12980 /* ... fall through ... */
12981
7b463b19 12982 case VEC_COND_EXPR:
12983
41ed701a 12984 case PLUS_EXPR:
12985 case MINUS_EXPR:
12986 case MULT_EXPR:
12987 case TRUNC_DIV_EXPR:
12988 case CEIL_DIV_EXPR:
12989 case FLOOR_DIV_EXPR:
12990 case ROUND_DIV_EXPR:
12991 case EXACT_DIV_EXPR:
12992 case TRUNC_MOD_EXPR:
12993 case FLOOR_MOD_EXPR:
12994 case RDIV_EXPR:
12995 case EQ_EXPR:
12996 case NE_EXPR:
12997 case LE_EXPR:
12998 case GE_EXPR:
12999 case LT_EXPR:
13000 case GT_EXPR:
13001 /* What about UNLT_EXPR? */
6290f0db 13002 if (VECTOR_TYPE_P (type0))
41ed701a 13003 {
41ed701a 13004 ret = stv_secondarg;
a4f59596 13005 std::swap (type0, type1);
13006 std::swap (op0, op1);
41ed701a 13007 }
13008
13009 if (TREE_CODE (type0) == INTEGER_TYPE
13010 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
13011 {
22a75734 13012 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 13013 {
13014 if (complain)
13015 error_at (loc, "conversion of scalar %qT to vector %qT "
13016 "involves truncation", type0, type1);
13017 return stv_error;
13018 }
13019 return ret;
13020 }
13021 else if (!integer_only_op
13022 /* Allow integer --> real conversion if safe. */
13023 && (TREE_CODE (type0) == REAL_TYPE
13024 || TREE_CODE (type0) == INTEGER_TYPE)
13025 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
13026 {
22a75734 13027 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 13028 {
13029 if (complain)
13030 error_at (loc, "conversion of scalar %qT to vector %qT "
13031 "involves truncation", type0, type1);
13032 return stv_error;
13033 }
13034 return ret;
13035 }
13036 default:
13037 break;
13038 }
13039
13040 return stv_nothing;
13041}
13042
ffcdbf9c 13043/* Return true iff ALIGN is an integral constant that is a fundamental
13044 alignment, as defined by [basic.align] in the c++-11
13045 specifications.
13046
13047 That is:
13048
13049 [A fundamental alignment is represented by an alignment less than or
13050 equal to the greatest alignment supported by the implementation
13051 in all contexts, which is equal to
13052 alignof(max_align_t)]. */
13053
13054bool
13055cxx_fundamental_alignment_p (unsigned align)
13056{
13057 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
13058 TYPE_ALIGN (long_double_type_node)));
13059}
13060
46da3601 13061/* Return true if T is a pointer to a zero-sized aggregate. */
13062
13063bool
13064pointer_to_zero_sized_aggr_p (tree t)
13065{
13066 if (!POINTER_TYPE_P (t))
13067 return false;
13068 t = TREE_TYPE (t);
13069 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
13070}
13071
547c6b1f 13072/* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
13073 with no library fallback or for an ADDR_EXPR whose operand is such type
13074 issues an error pointing to the location LOC.
13075 Returns true when the expression has been diagnosed and false
13076 otherwise. */
13077bool
13078reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
13079{
13080 if (TREE_CODE (expr) == ADDR_EXPR)
13081 expr = TREE_OPERAND (expr, 0);
13082
13083 if (TREE_TYPE (expr)
13084 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
13085 && DECL_P (expr)
13086 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
13087 false positives for user-declared built-ins such as abs or
13088 strlen, and for C++ operators new and delete.
13089 The c_decl_implicit() test avoids false positives for implicitly
13090 declared built-ins with library fallbacks (such as abs). */
13091 && DECL_BUILT_IN (expr)
13092 && DECL_IS_BUILTIN (expr)
13093 && !c_decl_implicit (expr)
13094 && !DECL_ASSEMBLER_NAME_SET_P (expr))
13095 {
13096 if (loc == UNKNOWN_LOCATION)
13097 loc = EXPR_LOC_OR_LOC (expr, input_location);
13098
13099 /* Reject arguments that are built-in functions with
13100 no library fallback. */
13101 error_at (loc, "built-in function %qE must be directly called", expr);
13102
13103 return true;
13104 }
13105
13106 return false;
13107}
13108
ef17a71a 13109/* If we're creating an if-else-if condition chain, first see if we
13110 already have this COND in the CHAIN. If so, warn and don't add COND
13111 into the vector, otherwise add the COND there. LOC is the location
13112 of COND. */
13113
13114void
13115warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
13116{
13117 /* No chain has been created yet. Do nothing. */
13118 if (*chain == NULL)
13119 return;
13120
13121 if (TREE_SIDE_EFFECTS (cond))
13122 {
13123 /* Uh-oh! This condition has a side-effect, thus invalidates
13124 the whole chain. */
13125 delete *chain;
13126 *chain = NULL;
13127 return;
13128 }
13129
13130 unsigned int ix;
13131 tree t;
13132 bool found = false;
13133 FOR_EACH_VEC_ELT (**chain, ix, t)
13134 if (operand_equal_p (cond, t, 0))
13135 {
13136 if (warning_at (loc, OPT_Wduplicated_cond,
13137 "duplicated %<if%> condition"))
13138 inform (EXPR_LOCATION (t), "previously used here");
13139 found = true;
13140 break;
13141 }
13142
13143 if (!found
13144 && !CONSTANT_CLASS_P (cond)
13145 /* Don't infinitely grow the chain. */
13146 && (*chain)->length () < 512)
13147 (*chain)->safe_push (cond);
13148}
13149
81b1b2a8 13150/* Check if array size calculations overflow or if the array covers more
13151 than half of the address space. Return true if the size of the array
13152 is valid, false otherwise. TYPE is the type of the array and NAME is
13153 the name of the array, or NULL_TREE for unnamed arrays. */
13154
13155bool
13156valid_array_size_p (location_t loc, tree type, tree name)
13157{
13158 if (type != error_mark_node
13159 && COMPLETE_TYPE_P (type)
13160 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
13161 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
13162 {
13163 if (name)
13164 error_at (loc, "size of array %qE is too large", name);
13165 else
13166 error_at (loc, "size of unnamed array is too large");
13167 return false;
13168 }
13169 return true;
13170}
13171
7bedc3a0 13172#include "gt-c-family-c-common.h"