]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-common.c
[AArch64, 6/6] Reimplement vpadd intrinsics & extend rtl patterns to all modes
[thirdparty/gcc.git] / gcc / c-family / c-common.c
CommitLineData
b0fc3e72 1/* Subroutines shared by all languages that are variants of C.
f1717362 2 Copyright (C) 1992-2016 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
2d2f6a15 302static tree check_case_value (location_t, tree);
be23b16f 303static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
304 bool *);
be43ff5a 305
1cae46be 306static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
307static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
308static tree handle_common_attribute (tree *, tree, tree, int, bool *);
309static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
5de92639 310static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
311static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
a9196da9 312static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
313 int, bool *);
d413ffdd 314static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
315 int, bool *);
05f893e1 316static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
317 bool *);
947aa916 318static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
1cae46be 319static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
bdb1f0d1 320static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
85fbea97 321static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
7bd95dfd 322static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
1cae46be 323static tree handle_always_inline_attribute (tree *, tree, tree, int,
324 bool *);
1b16fc45 325static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
326static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
0cdd9887 327static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
10fc867f 328static tree handle_error_attribute (tree *, tree, tree, int, bool *);
1cae46be 329static tree handle_used_attribute (tree *, tree, tree, int, bool *);
62eec3b4 330static tree handle_externally_visible_attribute (tree *, tree, tree, int,
331 bool *);
6b722052 332static tree handle_no_reorder_attribute (tree *, tree, tree, int,
333 bool *);
1cae46be 334static tree handle_const_attribute (tree *, tree, tree, int, bool *);
335static tree handle_transparent_union_attribute (tree *, tree, tree,
336 int, bool *);
292237f3 337static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
338 int, bool *);
1cae46be 339static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
340static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
341static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
342static tree handle_section_attribute (tree *, tree, tree, int, bool *);
343static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
344static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
4641882f 345static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
85c0a25c 346static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
347static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
1cae46be 348static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
f4a30bd7 349static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
1cae46be 350static tree handle_visibility_attribute (tree *, tree, tree, int,
351 bool *);
352static tree handle_tls_model_attribute (tree *, tree, tree, int,
353 bool *);
354static tree handle_no_instrument_function_attribute (tree *, tree,
355 tree, int, bool *);
356static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
26d1c5ff 357static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
1cae46be 358static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
359 bool *);
360static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
4c0315d0 361static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
362static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
fc09b200 363static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
1cae46be 364static tree handle_deprecated_attribute (tree *, tree, tree, int,
365 bool *);
366static tree handle_vector_size_attribute (tree *, tree, tree, int,
367 bool *);
368static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
369static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
370static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
8a8cdb8d 371static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
372 bool *);
bf6c8de0 373static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
b5c26b42 374static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
4a29c97c 375static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
237e78b1 376static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
377static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
24470055 378static tree handle_target_attribute (tree *, tree, tree, int, bool *);
ab50af2a 379static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
46f8e3b0 380static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
4c0315d0 381static tree ignore_attribute (tree *, tree, tree, int, bool *);
48b14f50 382static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
8ce86007 383static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
a96c3cc1 384static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
d7dcba40 385static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
bc7bff74 386static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
387 bool *);
c58a4cfd 388static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
bc7bff74 389static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
390 bool *);
74691f46 391static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
058a1b7a 392static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
393static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
394static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
1cae46be 395
1cae46be 396static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
397static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
398static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
860251be 399static int resort_field_decl_cmp (const void *, const void *);
dbf6c367 400
5c6e5756 401/* Reserved words. The third field is a mask: keywords are disabled
402 if they match the mask.
403
404 Masks for languages:
405 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
406 C --std=c99: D_CXXONLY | D_OBJC
407 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
408 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
409 C++ --std=c0x: D_CONLY | D_OBJC
410 ObjC++ is like C++ except that D_OBJC is not set
411
412 If -fno-asm is used, D_ASM is added to the mask. If
413 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
414 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
b27e241e 415 In C with -Wc++-compat, we warn if D_CXXWARN is set.
5c6e5756 416
b27e241e 417 Note the complication of the D_CXX_OBJC keywords. These are
418 reserved words such as 'class'. In C++, 'class' is a reserved
419 word. In Objective-C++ it is too. In Objective-C, it is a
420 reserved word too, but only if it follows an '@' sign.
421*/
5c6e5756 422const struct c_common_resword c_common_reswords[] =
423{
83e25171 424 { "_Alignas", RID_ALIGNAS, D_CONLY },
425 { "_Alignof", RID_ALIGNOF, D_CONLY },
b560fabd 426 { "_Atomic", RID_ATOMIC, D_CONLY },
5c6e5756 427 { "_Bool", RID_BOOL, D_CONLY },
428 { "_Complex", RID_COMPLEX, 0 },
d037099f 429 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
430 { "_Cilk_sync", RID_CILK_SYNC, 0 },
40750995 431 { "_Cilk_for", RID_CILK_FOR, 0 },
c1800156 432 { "_Imaginary", RID_IMAGINARY, D_CONLY },
5c6e5756 433 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
434 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
435 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
436 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
437 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
438 { "_Sat", RID_SAT, D_CONLY | D_EXT },
f80e7755 439 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
985c6e3a 440 { "_Noreturn", RID_NORETURN, D_CONLY },
7aa04c8d 441 { "_Generic", RID_GENERIC, D_CONLY },
d184e0c0 442 { "_Thread_local", RID_THREAD, D_CONLY },
5c6e5756 443 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
444 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
445 { "__alignof", RID_ALIGNOF, 0 },
446 { "__alignof__", RID_ALIGNOF, 0 },
447 { "__asm", RID_ASM, 0 },
448 { "__asm__", RID_ASM, 0 },
449 { "__attribute", RID_ATTRIBUTE, 0 },
450 { "__attribute__", RID_ATTRIBUTE, 0 },
4fba5eb9 451 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
e6014a82 452 { "__bases", RID_BASES, D_CXXONLY },
6f58cf06 453 { "__builtin_call_with_static_chain",
454 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
5c6e5756 455 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
bff4ad11 456 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
bf0cb017 457 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
5c6e5756 458 { "__builtin_offsetof", RID_OFFSETOF, 0 },
459 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
460 { "__builtin_va_arg", RID_VA_ARG, 0 },
461 { "__complex", RID_COMPLEX, 0 },
462 { "__complex__", RID_COMPLEX, 0 },
463 { "__const", RID_CONST, 0 },
464 { "__const__", RID_CONST, 0 },
465 { "__decltype", RID_DECLTYPE, D_CXXONLY },
e6014a82 466 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
5c6e5756 467 { "__extension__", RID_EXTENSION, 0 },
468 { "__func__", RID_C99_FUNCTION_NAME, 0 },
469 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
470 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
471 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
472 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
473 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
474 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
475 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
476 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
23407dc9 477 { "__imag", RID_IMAGPART, 0 },
478 { "__imag__", RID_IMAGPART, 0 },
479 { "__inline", RID_INLINE, 0 },
480 { "__inline__", RID_INLINE, 0 },
5c6e5756 481 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
482 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
483 { "__is_class", RID_IS_CLASS, D_CXXONLY },
5c6e5756 484 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
485 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
aa4313eb 486 { "__is_final", RID_IS_FINAL, D_CXXONLY },
23407dc9 487 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
5c6e5756 488 { "__is_pod", RID_IS_POD, D_CXXONLY },
489 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
56c12fd4 490 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
c1c67b4f 491 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
492 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
f76a9aa8 493 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
494 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
717e52f9 495 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
5c6e5756 496 { "__is_union", RID_IS_UNION, D_CXXONLY },
5c6e5756 497 { "__label__", RID_LABEL, 0 },
498 { "__null", RID_NULL, 0 },
499 { "__real", RID_REALPART, 0 },
500 { "__real__", RID_REALPART, 0 },
501 { "__restrict", RID_RESTRICT, 0 },
502 { "__restrict__", RID_RESTRICT, 0 },
503 { "__signed", RID_SIGNED, 0 },
504 { "__signed__", RID_SIGNED, 0 },
505 { "__thread", RID_THREAD, 0 },
4c0315d0 506 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
507 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
508 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
5c6e5756 509 { "__typeof", RID_TYPEOF, 0 },
510 { "__typeof__", RID_TYPEOF, 0 },
23407dc9 511 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
5c6e5756 512 { "__volatile", RID_VOLATILE, 0 },
513 { "__volatile__", RID_VOLATILE, 0 },
fa769cc5 514 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
515 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 516 { "asm", RID_ASM, D_ASM },
517 { "auto", RID_AUTO, 0 },
dbd982c9 518 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
5c6e5756 519 { "break", RID_BREAK, 0 },
520 { "case", RID_CASE, 0 },
51030405 521 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
5c6e5756 522 { "char", RID_CHAR, 0 },
fa769cc5 523 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
524 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
51030405 525 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
5c6e5756 526 { "const", RID_CONST, 0 },
fa769cc5 527 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 528 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
529 { "continue", RID_CONTINUE, 0 },
fa769cc5 530 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 531 { "default", RID_DEFAULT, 0 },
51030405 532 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
5c6e5756 533 { "do", RID_DO, 0 },
534 { "double", RID_DOUBLE, 0 },
535 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
536 { "else", RID_ELSE, 0 },
537 { "enum", RID_ENUM, 0 },
51030405 538 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
539 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
5c6e5756 540 { "extern", RID_EXTERN, 0 },
51030405 541 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
5c6e5756 542 { "float", RID_FLOAT, 0 },
543 { "for", RID_FOR, 0 },
51030405 544 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
5c6e5756 545 { "goto", RID_GOTO, 0 },
546 { "if", RID_IF, 0 },
547 { "inline", RID_INLINE, D_EXT89 },
548 { "int", RID_INT, 0 },
549 { "long", RID_LONG, 0 },
550 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
51030405 551 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
552 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
fa769cc5 553 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
554 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
51030405 555 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
556 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
557 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
558 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
5c6e5756 559 { "register", RID_REGISTER, 0 },
560 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
561 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
562 { "return", RID_RETURN, 0 },
563 { "short", RID_SHORT, 0 },
564 { "signed", RID_SIGNED, 0 },
565 { "sizeof", RID_SIZEOF, 0 },
566 { "static", RID_STATIC, 0 },
fa769cc5 567 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 568 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
569 { "struct", RID_STRUCT, 0 },
570 { "switch", RID_SWITCH, 0 },
51030405 571 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
572 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
fa769cc5 573 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
51030405 574 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
575 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
576 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
5c6e5756 577 { "typedef", RID_TYPEDEF, 0 },
51030405 578 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
579 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
5c6e5756 580 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
581 { "union", RID_UNION, 0 },
582 { "unsigned", RID_UNSIGNED, 0 },
51030405 583 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
584 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
5c6e5756 585 { "void", RID_VOID, 0 },
586 { "volatile", RID_VOLATILE, 0 },
587 { "wchar_t", RID_WCHAR, D_CXXONLY },
588 { "while", RID_WHILE, 0 },
56c12fd4 589
6d02e6b2 590 /* C++ transactional memory. */
591 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
592 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
593 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
594 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
595
56c12fd4 596 /* Concepts-related keywords */
597 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
598 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
599
5c6e5756 600 /* These Objective-C keywords are recognized only immediately after
601 an '@'. */
602 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
603 { "defs", RID_AT_DEFS, D_OBJC },
604 { "encode", RID_AT_ENCODE, D_OBJC },
605 { "end", RID_AT_END, D_OBJC },
606 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
607 { "interface", RID_AT_INTERFACE, D_OBJC },
608 { "protocol", RID_AT_PROTOCOL, D_OBJC },
609 { "selector", RID_AT_SELECTOR, D_OBJC },
610 { "finally", RID_AT_FINALLY, D_OBJC },
069761fb 611 { "optional", RID_AT_OPTIONAL, D_OBJC },
612 { "required", RID_AT_REQUIRED, D_OBJC },
86c110ac 613 { "property", RID_AT_PROPERTY, D_OBJC },
4a8875ed 614 { "package", RID_AT_PACKAGE, D_OBJC },
e1f293c0 615 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
616 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
5c6e5756 617 /* These are recognized only in protocol-qualifier context
618 (see above) */
619 { "bycopy", RID_BYCOPY, D_OBJC },
620 { "byref", RID_BYREF, D_OBJC },
621 { "in", RID_IN, D_OBJC },
622 { "inout", RID_INOUT, D_OBJC },
623 { "oneway", RID_ONEWAY, D_OBJC },
624 { "out", RID_OUT, D_OBJC },
86c110ac 625 /* These are recognized inside a property attribute list */
7590f0e5 626 { "assign", RID_ASSIGN, D_OBJC },
627 { "copy", RID_COPY, D_OBJC },
86c110ac 628 { "getter", RID_GETTER, D_OBJC },
7590f0e5 629 { "nonatomic", RID_NONATOMIC, D_OBJC },
630 { "readonly", RID_READONLY, D_OBJC },
631 { "readwrite", RID_READWRITE, D_OBJC },
632 { "retain", RID_RETAIN, D_OBJC },
86c110ac 633 { "setter", RID_SETTER, D_OBJC },
5c6e5756 634};
635
636const unsigned int num_c_common_reswords =
637 sizeof c_common_reswords / sizeof (struct c_common_resword);
638
058a1b7a 639/* Table of machine-independent attributes common to all C-like languages.
640
641 All attributes referencing arguments should be additionally processed
642 in chkp_copy_function_type_adding_bounds for correct instrumentation
643 by Pointer Bounds Checker.
644 Current list of processed common attributes: nonnull. */
f8e93a2e 645const struct attribute_spec c_common_attribute_table[] =
646{
ac86af5d 647 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
648 affects_type_identity } */
f8e93a2e 649 { "packed", 0, 0, false, false, false,
ac86af5d 650 handle_packed_attribute , false},
f8e93a2e 651 { "nocommon", 0, 0, true, false, false,
ac86af5d 652 handle_nocommon_attribute, false},
f8e93a2e 653 { "common", 0, 0, true, false, false,
ac86af5d 654 handle_common_attribute, false },
f8e93a2e 655 /* FIXME: logically, noreturn attributes should be listed as
656 "false, true, true" and apply to function types. But implementing this
657 would require all the places in the compiler that use TREE_THIS_VOLATILE
658 on a decl to identify non-returning functions to be located and fixed
659 to check the function type instead. */
660 { "noreturn", 0, 0, true, false, false,
ac86af5d 661 handle_noreturn_attribute, false },
f8e93a2e 662 { "volatile", 0, 0, true, false, false,
ac86af5d 663 handle_noreturn_attribute, false },
947aa916 664 { "stack_protect", 0, 0, true, false, false,
665 handle_stack_protect_attribute, false },
f8e93a2e 666 { "noinline", 0, 0, true, false, false,
ac86af5d 667 handle_noinline_attribute, false },
bdb1f0d1 668 { "noclone", 0, 0, true, false, false,
ac86af5d 669 handle_noclone_attribute, false },
85fbea97 670 { "no_icf", 0, 0, true, false, false,
671 handle_noicf_attribute, false },
7bd95dfd 672 { "leaf", 0, 0, true, false, false,
ac86af5d 673 handle_leaf_attribute, false },
f8e93a2e 674 { "always_inline", 0, 0, true, false, false,
ac86af5d 675 handle_always_inline_attribute, false },
541e4101 676 { "gnu_inline", 0, 0, true, false, false,
ac86af5d 677 handle_gnu_inline_attribute, false },
1b16fc45 678 { "artificial", 0, 0, true, false, false,
ac86af5d 679 handle_artificial_attribute, false },
0cdd9887 680 { "flatten", 0, 0, true, false, false,
ac86af5d 681 handle_flatten_attribute, false },
f8e93a2e 682 { "used", 0, 0, true, false, false,
ac86af5d 683 handle_used_attribute, false },
f8e93a2e 684 { "unused", 0, 0, false, false, false,
ac86af5d 685 handle_unused_attribute, false },
62eec3b4 686 { "externally_visible", 0, 0, true, false, false,
ac86af5d 687 handle_externally_visible_attribute, false },
6b722052 688 { "no_reorder", 0, 0, true, false, false,
689 handle_no_reorder_attribute, false },
f8e93a2e 690 /* The same comments as for noreturn attributes apply to const ones. */
691 { "const", 0, 0, true, false, false,
ac86af5d 692 handle_const_attribute, false },
292237f3 693 { "scalar_storage_order", 1, 1, false, false, false,
694 handle_scalar_storage_order_attribute, false },
f8e93a2e 695 { "transparent_union", 0, 0, false, false, false,
ac86af5d 696 handle_transparent_union_attribute, false },
9af7fd5b 697 { "constructor", 0, 1, true, false, false,
ac86af5d 698 handle_constructor_attribute, false },
9af7fd5b 699 { "destructor", 0, 1, true, false, false,
ac86af5d 700 handle_destructor_attribute, false },
f8e93a2e 701 { "mode", 1, 1, false, true, false,
ac86af5d 702 handle_mode_attribute, false },
f8e93a2e 703 { "section", 1, 1, true, false, false,
ac86af5d 704 handle_section_attribute, false },
f8e93a2e 705 { "aligned", 0, 1, false, false, false,
ac86af5d 706 handle_aligned_attribute, false },
f8e93a2e 707 { "weak", 0, 0, true, false, false,
ac86af5d 708 handle_weak_attribute, false },
4641882f 709 { "noplt", 0, 0, true, false, false,
710 handle_noplt_attribute, false },
85c0a25c 711 { "ifunc", 1, 1, true, false, false,
ac86af5d 712 handle_ifunc_attribute, false },
f8e93a2e 713 { "alias", 1, 1, true, false, false,
ac86af5d 714 handle_alias_attribute, false },
f4a30bd7 715 { "weakref", 0, 1, true, false, false,
ac86af5d 716 handle_weakref_attribute, false },
f8e93a2e 717 { "no_instrument_function", 0, 0, true, false, false,
ac86af5d 718 handle_no_instrument_function_attribute,
719 false },
f8e93a2e 720 { "malloc", 0, 0, true, false, false,
ac86af5d 721 handle_malloc_attribute, false },
26d1c5ff 722 { "returns_twice", 0, 0, true, false, false,
ac86af5d 723 handle_returns_twice_attribute, false },
f8e93a2e 724 { "no_stack_limit", 0, 0, true, false, false,
ac86af5d 725 handle_no_limit_stack_attribute, false },
f8e93a2e 726 { "pure", 0, 0, true, false, false,
ac86af5d 727 handle_pure_attribute, false },
4c0315d0 728 { "transaction_callable", 0, 0, false, true, false,
729 handle_tm_attribute, false },
730 { "transaction_unsafe", 0, 0, false, true, false,
6d02e6b2 731 handle_tm_attribute, true },
4c0315d0 732 { "transaction_safe", 0, 0, false, true, false,
6d02e6b2 733 handle_tm_attribute, true },
734 { "transaction_safe_dynamic", 0, 0, true, false, false,
4c0315d0 735 handle_tm_attribute, false },
736 { "transaction_may_cancel_outer", 0, 0, false, true, false,
737 handle_tm_attribute, false },
738 /* ??? These two attributes didn't make the transition from the
739 Intel language document to the multi-vendor language document. */
740 { "transaction_pure", 0, 0, false, true, false,
741 handle_tm_attribute, false },
742 { "transaction_wrap", 1, 1, true, false, false,
743 handle_tm_wrap_attribute, false },
fc09b200 744 /* For internal use (marking of builtins) only. The name contains space
745 to prevent its usage in source code. */
746 { "no vops", 0, 0, true, false, false,
ac86af5d 747 handle_novops_attribute, false },
45c4e798 748 { "deprecated", 0, 1, false, false, false,
ac86af5d 749 handle_deprecated_attribute, false },
f8e93a2e 750 { "vector_size", 1, 1, false, true, false,
ac86af5d 751 handle_vector_size_attribute, false },
b212f378 752 { "visibility", 1, 1, false, false, false,
ac86af5d 753 handle_visibility_attribute, false },
24dfead4 754 { "tls_model", 1, 1, true, false, false,
ac86af5d 755 handle_tls_model_attribute, false },
dbf6c367 756 { "nonnull", 0, -1, false, true, true,
ac86af5d 757 handle_nonnull_attribute, false },
fa987697 758 { "nothrow", 0, 0, true, false, false,
ac86af5d 759 handle_nothrow_attribute, false },
760 { "may_alias", 0, 0, false, true, false, NULL, false },
7acb29a3 761 { "cleanup", 1, 1, true, false, false,
ac86af5d 762 handle_cleanup_attribute, false },
8a8cdb8d 763 { "warn_unused_result", 0, 0, false, true, true,
ac86af5d 764 handle_warn_unused_result_attribute, false },
50ca527f 765 { "sentinel", 0, 1, false, true, true,
ac86af5d 766 handle_sentinel_attribute, false },
b5c26b42 767 /* For internal use (marking of builtins) only. The name contains space
768 to prevent its usage in source code. */
769 { "type generic", 0, 0, false, true, true,
ac86af5d 770 handle_type_generic_attribute, false },
4a29c97c 771 { "alloc_size", 1, 2, false, true, true,
ac86af5d 772 handle_alloc_size_attribute, false },
5de92639 773 { "cold", 0, 0, true, false, false,
ac86af5d 774 handle_cold_attribute, false },
5de92639 775 { "hot", 0, 0, true, false, false,
ac86af5d 776 handle_hot_attribute, false },
d413ffdd 777 { "no_address_safety_analysis",
778 0, 0, true, false, false,
779 handle_no_address_safety_analysis_attribute,
780 false },
a9196da9 781 { "no_sanitize_address", 0, 0, true, false, false,
782 handle_no_sanitize_address_attribute,
783 false },
d1e96383 784 { "no_sanitize_thread", 0, 0, true, false, false,
785 handle_no_sanitize_address_attribute,
786 false },
05f893e1 787 { "no_sanitize_undefined", 0, 0, true, false, false,
788 handle_no_sanitize_undefined_attribute,
789 false },
10fc867f 790 { "warning", 1, 1, true, false, false,
ac86af5d 791 handle_error_attribute, false },
10fc867f 792 { "error", 1, 1, true, false, false,
ac86af5d 793 handle_error_attribute, false },
24470055 794 { "target", 1, -1, true, false, false,
ac86af5d 795 handle_target_attribute, false },
ab50af2a 796 { "target_clones", 1, -1, true, false, false,
797 handle_target_clones_attribute, false },
46f8e3b0 798 { "optimize", 1, -1, true, false, false,
ac86af5d 799 handle_optimize_attribute, false },
4c0315d0 800 /* For internal use only. The leading '*' both prevents its usage in
801 source code and signals that it may be overridden by machine tables. */
802 { "*tm regparm", 0, 0, false, true, true,
803 ignore_attribute, false },
48b14f50 804 { "no_split_stack", 0, 0, true, false, false,
ac86af5d 805 handle_no_split_stack_attribute, false },
8ce86007 806 /* For internal use (marking of builtins and runtime functions) only.
807 The name contains space to prevent its usage in source code. */
808 { "fn spec", 1, 1, false, true, true,
ac86af5d 809 handle_fnspec_attribute, false },
a96c3cc1 810 { "warn_unused", 0, 0, false, false, false,
811 handle_warn_unused_attribute, false },
d7dcba40 812 { "returns_nonnull", 0, 0, false, true, true,
813 handle_returns_nonnull_attribute, false },
bc7bff74 814 { "omp declare simd", 0, -1, true, false, false,
815 handle_omp_declare_simd_attribute, false },
74acc703 816 { "cilk simd function", 0, -1, true, false, false,
817 handle_omp_declare_simd_attribute, false },
9841c6ee 818 { "simd", 0, 1, true, false, false,
c58a4cfd 819 handle_simd_attribute, false },
bc7bff74 820 { "omp declare target", 0, 0, true, false, false,
821 handle_omp_declare_target_attribute, false },
c0998828 822 { "omp declare target link", 0, 0, true, false, false,
823 handle_omp_declare_target_attribute, false },
237e78b1 824 { "alloc_align", 1, 1, false, true, true,
825 handle_alloc_align_attribute, false },
826 { "assume_aligned", 1, 2, false, true, true,
827 handle_assume_aligned_attribute, false },
74691f46 828 { "designated_init", 0, 0, false, true, false,
829 handle_designated_init_attribute, false },
058a1b7a 830 { "bnd_variable_size", 0, 0, true, false, false,
831 handle_bnd_variable_size_attribute, false },
832 { "bnd_legacy", 0, 0, true, false, false,
833 handle_bnd_legacy, false },
834 { "bnd_instrument", 0, 0, true, false, false,
835 handle_bnd_instrument, false },
ac86af5d 836 { NULL, 0, 0, false, false, false, NULL, false }
f8e93a2e 837};
838
839/* Give the specifications for the format attributes, used by C and all
058a1b7a 840 descendants.
f8e93a2e 841
058a1b7a 842 All attributes referencing arguments should be additionally processed
843 in chkp_copy_function_type_adding_bounds for correct instrumentation
844 by Pointer Bounds Checker.
845 Current list of processed format attributes: format, format_arg. */
f8e93a2e 846const struct attribute_spec c_common_format_attribute_table[] =
847{
ac86af5d 848 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
849 affects_type_identity } */
f8e93a2e 850 { "format", 3, 3, false, true, true,
ac86af5d 851 handle_format_attribute, false },
f8e93a2e 852 { "format_arg", 1, 1, false, true, true,
ac86af5d 853 handle_format_arg_attribute, false },
854 { NULL, 0, 0, false, false, false, NULL, false }
f8e93a2e 855};
856
6d5d708e 857/* Return identifier for address space AS. */
34208e18 858
6d5d708e 859const char *
860c_addr_space_name (addr_space_t as)
861{
34208e18 862 int rid = RID_FIRST_ADDR_SPACE + as;
863 gcc_assert (ridpointers [rid]);
864 return IDENTIFIER_POINTER (ridpointers [rid]);
6d5d708e 865}
866
2c0e001b 867/* Push current bindings for the function name VAR_DECLS. */
f4e3c278 868
869void
1cae46be 870start_fname_decls (void)
f4e3c278 871{
65b7f83f 872 unsigned ix;
873 tree saved = NULL_TREE;
1cae46be 874
65b7f83f 875 for (ix = 0; fname_vars[ix].decl; ix++)
876 {
877 tree decl = *fname_vars[ix].decl;
f4e3c278 878
65b7f83f 879 if (decl)
880 {
ceb7b692 881 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
882 saved);
65b7f83f 883 *fname_vars[ix].decl = NULL_TREE;
884 }
885 }
886 if (saved || saved_function_name_decls)
887 /* Normally they'll have been NULL, so only push if we've got a
888 stack, or they are non-NULL. */
889 saved_function_name_decls = tree_cons (saved, NULL_TREE,
890 saved_function_name_decls);
891}
892
2363ef00 893/* Finish up the current bindings, adding them into the current function's
894 statement tree. This must be done _before_ finish_stmt_tree is called.
895 If there is no current function, we must be at file scope and no statements
896 are involved. Pop the previous bindings. */
65b7f83f 897
898void
1cae46be 899finish_fname_decls (void)
65b7f83f 900{
901 unsigned ix;
2363ef00 902 tree stmts = NULL_TREE;
65b7f83f 903 tree stack = saved_function_name_decls;
904
905 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
2363ef00 906 append_to_statement_list (TREE_VALUE (stack), &stmts);
1cae46be 907
2363ef00 908 if (stmts)
65b7f83f 909 {
2363ef00 910 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
5c423bd6 911
2363ef00 912 if (TREE_CODE (*bodyp) == BIND_EXPR)
913 bodyp = &BIND_EXPR_BODY (*bodyp);
81010c97 914
bc2b76e0 915 append_to_statement_list_force (*bodyp, &stmts);
2363ef00 916 *bodyp = stmts;
65b7f83f 917 }
1cae46be 918
65b7f83f 919 for (ix = 0; fname_vars[ix].decl; ix++)
920 *fname_vars[ix].decl = NULL_TREE;
1cae46be 921
65b7f83f 922 if (stack)
f4e3c278 923 {
2c0e001b 924 /* We had saved values, restore them. */
65b7f83f 925 tree saved;
926
927 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
928 {
929 tree decl = TREE_PURPOSE (saved);
f9ae6f95 930 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
1cae46be 931
65b7f83f 932 *fname_vars[ix].decl = decl;
933 }
934 stack = TREE_CHAIN (stack);
f4e3c278 935 }
65b7f83f 936 saved_function_name_decls = stack;
937}
938
81010c97 939/* Return the text name of the current function, suitably prettified
5fc7fa69 940 by PRETTY_P. Return string must be freed by caller. */
65b7f83f 941
942const char *
1cae46be 943fname_as_string (int pretty_p)
65b7f83f 944{
9ad4bb1e 945 const char *name = "top level";
5fc7fa69 946 char *namep;
8115b8be 947 int vrb = 2, len;
948 cpp_string cstr = { 0, 0 }, strname;
9ad4bb1e 949
84166705 950 if (!pretty_p)
9ad4bb1e 951 {
952 name = "";
953 vrb = 0;
954 }
955
956 if (current_function_decl)
dc24ddbd 957 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
9ad4bb1e 958
8115b8be 959 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
5fc7fa69 960
8115b8be 961 namep = XNEWVEC (char, len);
962 snprintf (namep, len, "\"%s\"", name);
963 strname.text = (unsigned char *) namep;
964 strname.len = len - 1;
5fc7fa69 965
924bbf02 966 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
8115b8be 967 {
968 XDELETEVEC (namep);
969 return (const char *) cstr.text;
5fc7fa69 970 }
5fc7fa69 971
972 return namep;
65b7f83f 973}
974
65b7f83f 975/* Return the VAR_DECL for a const char array naming the current
976 function. If the VAR_DECL has not yet been created, create it
977 now. RID indicates how it should be formatted and IDENTIFIER_NODE
978 ID is its name (unfortunately C and C++ hold the RID values of
979 keywords in different places, so we can't derive RID from ID in
e3b80d49 980 this language independent code. LOC is the location of the
981 function. */
65b7f83f 982
983tree
e3b80d49 984fname_decl (location_t loc, unsigned int rid, tree id)
65b7f83f 985{
986 unsigned ix;
987 tree decl = NULL_TREE;
988
989 for (ix = 0; fname_vars[ix].decl; ix++)
990 if (fname_vars[ix].rid == rid)
991 break;
992
993 decl = *fname_vars[ix].decl;
994 if (!decl)
f4e3c278 995 {
2222b3c6 996 /* If a tree is built here, it would normally have the lineno of
997 the current statement. Later this tree will be moved to the
998 beginning of the function and this line number will be wrong.
999 To avoid this problem set the lineno to 0 here; that prevents
7299020b 1000 it from appearing in the RTL. */
2363ef00 1001 tree stmts;
9a6486a6 1002 location_t saved_location = input_location;
9a6486a6 1003 input_location = UNKNOWN_LOCATION;
1cae46be 1004
2363ef00 1005 stmts = push_stmt_list ();
e60a6f7b 1006 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
2363ef00 1007 stmts = pop_stmt_list (stmts);
1008 if (!IS_EMPTY_STMT (stmts))
1009 saved_function_name_decls
1010 = tree_cons (decl, stmts, saved_function_name_decls);
65b7f83f 1011 *fname_vars[ix].decl = decl;
9a6486a6 1012 input_location = saved_location;
f4e3c278 1013 }
65b7f83f 1014 if (!ix && !current_function_decl)
e3b80d49 1015 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
81010c97 1016
65b7f83f 1017 return decl;
f4e3c278 1018}
1019
070236f0 1020/* Given a STRING_CST, give it a suitable array-of-chars data type. */
b0fc3e72 1021
1022tree
1cae46be 1023fix_string_type (tree value)
b0fc3e72 1024{
070236f0 1025 int length = TREE_STRING_LENGTH (value);
1026 int nchars;
00d26680 1027 tree e_type, i_type, a_type;
1028
73be5127 1029 /* Compute the number of elements, for the array type. */
924bbf02 1030 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1031 {
1032 nchars = length;
1033 e_type = char_type_node;
1034 }
1035 else if (TREE_TYPE (value) == char16_array_type_node)
1036 {
1037 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1038 e_type = char16_type_node;
1039 }
1040 else if (TREE_TYPE (value) == char32_array_type_node)
1041 {
1042 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1043 e_type = char32_type_node;
1044 }
1045 else
1046 {
1047 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1048 e_type = wchar_type_node;
1049 }
b0fc3e72 1050
1d752508 1051 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1052 limit in C++98 Annex B is very large (65536) and is not normative,
1053 so we do not diagnose it (warn_overlength_strings is forced off
1054 in c_common_post_options). */
1055 if (warn_overlength_strings)
1056 {
1057 const int nchars_max = flag_isoc99 ? 4095 : 509;
1058 const int relevant_std = flag_isoc99 ? 99 : 90;
1059 if (nchars - 1 > nchars_max)
1060 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1061 separate the %d from the 'C'. 'ISO' should not be
1062 translated, but it may be moved after 'C%d' in languages
1063 where modifiers follow nouns. */
21ca8540 1064 pedwarn (input_location, OPT_Woverlength_strings,
8864917d 1065 "string length %qd is greater than the length %qd "
1d752508 1066 "ISO C%d compilers are required to support",
1067 nchars - 1, nchars_max, relevant_std);
1068 }
82cfc7f7 1069
390be14e 1070 /* Create the array type for the string constant. The ISO C++
1071 standard says that a string literal has type `const char[N]' or
1072 `const wchar_t[N]'. We use the same logic when invoked as a C
1073 front-end with -Wwrite-strings.
1074 ??? We should change the type of an expression depending on the
1075 state of a warning flag. We should just be warning -- see how
1076 this is handled in the C++ front-end for the deprecated implicit
1077 conversion from string literals to `char*' or `wchar_t*'.
00d26680 1078
1079 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1080 array type being the unqualified version of that type.
1081 Therefore, if we are constructing an array of const char, we must
1082 construct the matching unqualified array type first. The C front
1083 end does not require this, but it does no harm, so we do it
1084 unconditionally. */
ceb7b692 1085 i_type = build_index_type (size_int (nchars - 1));
00d26680 1086 a_type = build_array_type (e_type, i_type);
390be14e 1087 if (c_dialect_cxx() || warn_write_strings)
aebc8537 1088 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
3a10ba35 1089
00d26680 1090 TREE_TYPE (value) = a_type;
b8e3b7ad 1091 TREE_CONSTANT (value) = 1;
a814bad5 1092 TREE_READONLY (value) = 1;
b0fc3e72 1093 TREE_STATIC (value) = 1;
1094 return value;
1095}
1096\f
9c691dbd 1097/* Fold X for consideration by one of the warning functions when checking
1098 whether an expression has a constant value. */
1099
1100static tree
1101fold_for_warn (tree x)
1102{
1103 if (c_dialect_cxx ())
1104 return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
1105 else
1106 /* The C front-end has already folded X appropriately. */
1107 return x;
1108}
1109
2a1736ed 1110/* Print a warning if a constant expression had overflow in folding.
1111 Invoke this function on every expression that the language
1112 requires to be a constant expression.
1113 Note the ANSI C standard says it is erroneous for a
1114 constant expression to overflow. */
b2806639 1115
1116void
1cae46be 1117constant_expression_warning (tree value)
07317e69 1118{
48e1416a 1119 if (warn_overflow && pedantic
07317e69 1120 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1121 || TREE_CODE (value) == FIXED_CST
1122 || TREE_CODE (value) == VECTOR_CST
1123 || TREE_CODE (value) == COMPLEX_CST)
1124 && TREE_OVERFLOW (value))
21ca8540 1125 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
07317e69 1126}
1127
1128/* The same as above but print an unconditional error. */
1129void
1130constant_expression_error (tree value)
b2806639 1131{
837e1122 1132 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
9421ebb9 1133 || TREE_CODE (value) == FIXED_CST
886cfd4f 1134 || TREE_CODE (value) == VECTOR_CST
837e1122 1135 || TREE_CODE (value) == COMPLEX_CST)
07317e69 1136 && TREE_OVERFLOW (value))
1137 error ("overflow in constant expression");
2a1736ed 1138}
1139
f170d67f 1140/* Print a warning if an expression had overflow in folding and its
1141 operands hadn't.
1142
2a1736ed 1143 Invoke this function on every expression that
1144 (1) appears in the source code, and
f170d67f 1145 (2) is a constant expression that overflowed, and
2a1736ed 1146 (3) is not already checked by convert_and_check;
f170d67f 1147 however, do not invoke this function on operands of explicit casts
1148 or when the expression is the result of an operator and any operand
1149 already overflowed. */
2a1736ed 1150
1151void
e60a6f7b 1152overflow_warning (location_t loc, tree value)
2a1736ed 1153{
48d94ede 1154 if (c_inhibit_evaluation_warnings != 0)
1155 return;
f170d67f 1156
1157 switch (TREE_CODE (value))
886cfd4f 1158 {
f170d67f 1159 case INTEGER_CST:
e60a6f7b 1160 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
f170d67f 1161 break;
48e1416a 1162
f170d67f 1163 case REAL_CST:
e60a6f7b 1164 warning_at (loc, OPT_Woverflow,
1165 "floating point overflow in expression");
f170d67f 1166 break;
48e1416a 1167
9421ebb9 1168 case FIXED_CST:
e60a6f7b 1169 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
9421ebb9 1170 break;
1171
f170d67f 1172 case VECTOR_CST:
e60a6f7b 1173 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
f170d67f 1174 break;
48e1416a 1175
f170d67f 1176 case COMPLEX_CST:
1177 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
e60a6f7b 1178 warning_at (loc, OPT_Woverflow,
1179 "complex integer overflow in expression");
f170d67f 1180 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
e60a6f7b 1181 warning_at (loc, OPT_Woverflow,
1182 "complex floating point overflow in expression");
f170d67f 1183 break;
1184
1185 default:
1186 break;
886cfd4f 1187 }
2a1736ed 1188}
1189
03033af4 1190/* Warn about uses of logical || / && operator in a context where it
1191 is likely that the bitwise equivalent was intended by the
1192 programmer. We have seen an expression in which CODE is a binary
9c20c4fc 1193 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1194 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
b13d1547 1195void
9c20c4fc 1196warn_logical_operator (location_t location, enum tree_code code, tree type,
48e1416a 1197 enum tree_code code_left, tree op_left,
03033af4 1198 enum tree_code ARG_UNUSED (code_right), tree op_right)
b13d1547 1199{
9c20c4fc 1200 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1201 int in0_p, in1_p, in_p;
1202 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1203 bool strict_overflow_p = false;
1204
03033af4 1205 if (code != TRUTH_ANDIF_EXPR
1206 && code != TRUTH_AND_EXPR
1207 && code != TRUTH_ORIF_EXPR
1208 && code != TRUTH_OR_EXPR)
1209 return;
1210
439606a9 1211 /* We don't want to warn if either operand comes from a macro
1212 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1213 see PR61534. */
1214 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1215 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1216 return;
1217
03033af4 1218 /* Warn if &&/|| are being used in a context where it is
1219 likely that the bitwise equivalent was intended by the
1220 programmer. That is, an expression such as op && MASK
1221 where op should not be any boolean expression, nor a
1222 constant, and mask seems to be a non-boolean integer constant. */
9c691dbd 1223 if (TREE_CODE (op_right) == CONST_DECL)
1224 /* An enumerator counts as a constant. */
1225 op_right = DECL_INITIAL (op_right);
03033af4 1226 if (!truth_value_p (code_left)
1227 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1228 && !CONSTANT_CLASS_P (op_left)
1229 && !TREE_NO_WARNING (op_left)
1230 && TREE_CODE (op_right) == INTEGER_CST
1231 && !integer_zerop (op_right)
1232 && !integer_onep (op_right))
b13d1547 1233 {
9c20c4fc 1234 if (or_op)
03033af4 1235 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1236 " applied to non-boolean constant");
1237 else
1238 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1239 " applied to non-boolean constant");
1240 TREE_NO_WARNING (op_left) = true;
9c20c4fc 1241 return;
1242 }
1243
1244 /* We do not warn for constants because they are typical of macro
1245 expansions that test for features. */
9c691dbd 1246 if (CONSTANT_CLASS_P (fold_for_warn (op_left))
1247 || CONSTANT_CLASS_P (fold_for_warn (op_right)))
9c20c4fc 1248 return;
1249
1250 /* This warning only makes sense with logical operands. */
1251 if (!(truth_value_p (TREE_CODE (op_left))
1252 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1253 || !(truth_value_p (TREE_CODE (op_right))
1254 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1255 return;
1256
5e84569c 1257 /* The range computations only work with scalars. */
1258 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1259 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1260 return;
9c20c4fc 1261
686369e8 1262 /* We first test whether either side separately is trivially true
1263 (with OR) or trivially false (with AND). If so, do not warn.
1264 This is a common idiom for testing ranges of data types in
1265 portable code. */
1266 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1267 if (!lhs)
1268 return;
1269 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
9c20c4fc 1270 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1271
686369e8 1272 /* If this is an OR operation, invert both sides; now, the result
1273 should be always false to get a warning. */
1274 if (or_op)
1275 in0_p = !in0_p;
1276
1277 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
d42e7c5a 1278 if (tem && integer_zerop (tem))
686369e8 1279 return;
1280
1281 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1282 if (!rhs)
1283 return;
1284 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
9c20c4fc 1285 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
48e1416a 1286
686369e8 1287 /* If this is an OR operation, invert both sides; now, the result
1288 should be always false to get a warning. */
9c20c4fc 1289 if (or_op)
686369e8 1290 in1_p = !in1_p;
48e1416a 1291
686369e8 1292 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
d42e7c5a 1293 if (tem && integer_zerop (tem))
686369e8 1294 return;
1295
1296 /* If both expressions have the same operand, if we can merge the
485f6b9c 1297 ranges, ... */
686369e8 1298 if (operand_equal_p (lhs, rhs, 0)
9c20c4fc 1299 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
485f6b9c 1300 in1_p, low1, high1))
9c20c4fc 1301 {
485f6b9c 1302 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1303 /* ... and if the range test is always false, then warn. */
1304 if (tem && integer_zerop (tem))
1305 {
1306 if (or_op)
1307 warning_at (location, OPT_Wlogical_op,
1308 "logical %<or%> of collectively exhaustive tests is "
1309 "always true");
1310 else
1311 warning_at (location, OPT_Wlogical_op,
1312 "logical %<and%> of mutually exclusive tests is "
1313 "always false");
1314 }
1315 /* Or warn if the operands have exactly the same range, e.g.
1316 A > 0 && A > 0. */
0200602e 1317 else if (tree_int_cst_equal (low0, low1)
1318 && tree_int_cst_equal (high0, high1))
485f6b9c 1319 {
1320 if (or_op)
1321 warning_at (location, OPT_Wlogical_op,
1322 "logical %<or%> of equal expressions");
1323 else
1324 warning_at (location, OPT_Wlogical_op,
1325 "logical %<and%> of equal expressions");
1326 }
b13d1547 1327 }
1328}
1329
6784a472 1330/* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
1331 with constant indices. */
1332
1333static tree
1334find_array_ref_with_const_idx_r (tree *expr_p, int *walk_subtrees, void *data)
1335{
1336 tree expr = *expr_p;
1337
1338 if ((TREE_CODE (expr) == ARRAY_REF
1339 || TREE_CODE (expr) == ARRAY_RANGE_REF)
1340 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
1341 {
1342 *(bool *) data = true;
1343 *walk_subtrees = 0;
1344 }
1345
1346 return NULL_TREE;
1347}
1348
1349/* Warn if a self-comparison always evaluates to true or false. LOC
1350 is the location of the comparison with code CODE, LHS and RHS are
1351 operands of the comparison. */
1352
1353void
1354warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
1355{
1356 if (TREE_CODE_CLASS (code) != tcc_comparison)
1357 return;
1358
d9e4fe27 1359 /* Don't warn for various macro expansions. */
1360 if (from_macro_expansion_at (loc)
1361 || from_macro_expansion_at (EXPR_LOCATION (lhs))
1362 || from_macro_expansion_at (EXPR_LOCATION (rhs)))
1363 return;
1364
6784a472 1365 /* We do not warn for constants because they are typical of macro
1366 expansions that test for features, sizeof, and similar. */
9c691dbd 1367 if (CONSTANT_CLASS_P (fold_for_warn (lhs))
1368 || CONSTANT_CLASS_P (fold_for_warn (rhs)))
6784a472 1369 return;
1370
1371 /* Don't warn for e.g.
1372 HOST_WIDE_INT n;
1373 ...
1374 if (n == (long) n) ...
1375 */
1376 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
1377 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
1378 return;
1379
f58dd84b 1380 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
1381 It could be a NaN, and NaN never compares equal to anything, even
1382 itself. */
1383 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
1384 return;
1385
6784a472 1386 if (operand_equal_p (lhs, rhs, 0))
1387 {
1388 /* Don't warn about array references with constant indices;
1389 these are likely to come from a macro. */
1390 bool found = false;
1391 walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
1392 &found);
1393 if (found)
1394 return;
1395 const bool always_true = (code == EQ_EXPR || code == LE_EXPR
1396 || code == GE_EXPR || code == UNLE_EXPR
1397 || code == UNGE_EXPR || code == UNEQ_EXPR);
1398 if (always_true)
1399 warning_at (loc, OPT_Wtautological_compare,
1400 "self-comparison always evaluates to true");
1401 else
1402 warning_at (loc, OPT_Wtautological_compare,
1403 "self-comparison always evaluates to false");
1404 }
1405}
1406
32dc1512 1407/* Warn about logical not used on the left hand side operand of a comparison.
1408 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
dc6229e8 1409 Do not warn if RHS is of a boolean type. */
32dc1512 1410
1411void
1412warn_logical_not_parentheses (location_t location, enum tree_code code,
dc6229e8 1413 tree rhs)
32dc1512 1414{
dc6229e8 1415 if (TREE_CODE_CLASS (code) != tcc_comparison
1416 || TREE_TYPE (rhs) == NULL_TREE
1417 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
32dc1512 1418 return;
1419
16f958b3 1420 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1421 !(x == 0) or !(y != 0). */
1422 if ((code == EQ_EXPR || code == NE_EXPR)
1423 && integer_zerop (rhs))
1424 return;
1425
32dc1512 1426 warning_at (location, OPT_Wlogical_not_parentheses,
1427 "logical not is only applied to the left hand side of "
1428 "comparison");
1429}
b13d1547 1430
3d177e8c 1431/* Warn if EXP contains any computations whose results are not used.
1432 Return true if a warning is printed; false otherwise. LOCUS is the
1433 (potential) location of the expression. */
1434
1435bool
1436warn_if_unused_value (const_tree exp, location_t locus)
1437{
1438 restart:
1439 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1440 return false;
1441
1442 /* Don't warn about void constructs. This includes casting to void,
1443 void function calls, and statement expressions with a final cast
1444 to void. */
1445 if (VOID_TYPE_P (TREE_TYPE (exp)))
1446 return false;
1447
1448 if (EXPR_HAS_LOCATION (exp))
1449 locus = EXPR_LOCATION (exp);
1450
1451 switch (TREE_CODE (exp))
1452 {
1453 case PREINCREMENT_EXPR:
1454 case POSTINCREMENT_EXPR:
1455 case PREDECREMENT_EXPR:
1456 case POSTDECREMENT_EXPR:
1457 case MODIFY_EXPR:
1458 case INIT_EXPR:
1459 case TARGET_EXPR:
1460 case CALL_EXPR:
1461 case TRY_CATCH_EXPR:
1462 case WITH_CLEANUP_EXPR:
1463 case EXIT_EXPR:
1464 case VA_ARG_EXPR:
1465 return false;
1466
1467 case BIND_EXPR:
1468 /* For a binding, warn if no side effect within it. */
1469 exp = BIND_EXPR_BODY (exp);
1470 goto restart;
1471
1472 case SAVE_EXPR:
1473 case NON_LVALUE_EXPR:
d85dbdb3 1474 case NOP_EXPR:
3d177e8c 1475 exp = TREE_OPERAND (exp, 0);
1476 goto restart;
1477
1478 case TRUTH_ORIF_EXPR:
1479 case TRUTH_ANDIF_EXPR:
1480 /* In && or ||, warn if 2nd operand has no side effect. */
1481 exp = TREE_OPERAND (exp, 1);
1482 goto restart;
1483
1484 case COMPOUND_EXPR:
1485 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1486 return true;
1487 /* Let people do `(foo (), 0)' without a warning. */
1488 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1489 return false;
1490 exp = TREE_OPERAND (exp, 1);
1491 goto restart;
1492
1493 case COND_EXPR:
1494 /* If this is an expression with side effects, don't warn; this
1495 case commonly appears in macro expansions. */
1496 if (TREE_SIDE_EFFECTS (exp))
1497 return false;
1498 goto warn;
1499
1500 case INDIRECT_REF:
1501 /* Don't warn about automatic dereferencing of references, since
1502 the user cannot control it. */
1503 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1504 {
1505 exp = TREE_OPERAND (exp, 0);
1506 goto restart;
1507 }
1508 /* Fall through. */
1509
1510 default:
1511 /* Referencing a volatile value is a side effect, so don't warn. */
1512 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1513 && TREE_THIS_VOLATILE (exp))
1514 return false;
1515
1516 /* If this is an expression which has no operands, there is no value
1517 to be unused. There are no such language-independent codes,
1518 but front ends may define such. */
1519 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1520 return false;
1521
1522 warn:
1523 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1524 }
1525}
1526
1527
bcf22371 1528/* Print a warning about casts that might indicate violation
1529 of strict aliasing rules if -Wstrict-aliasing is used and
1e31ff37 1530 strict aliasing mode is in effect. OTYPE is the original
1531 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
bcf22371 1532
e6fa0ea6 1533bool
1e31ff37 1534strict_aliasing_warning (tree otype, tree type, tree expr)
bcf22371 1535{
45bb3afb 1536 /* Strip pointer conversion chains and get to the correct original type. */
1537 STRIP_NOPS (expr);
1538 otype = TREE_TYPE (expr);
1539
f06537f2 1540 if (!(flag_strict_aliasing
1541 && POINTER_TYPE_P (type)
1542 && POINTER_TYPE_P (otype)
1543 && !VOID_TYPE_P (TREE_TYPE (type)))
1544 /* If the type we are casting to is a ref-all pointer
1545 dereferencing it is always valid. */
1546 || TYPE_REF_CAN_ALIAS_ALL (type))
e6fa0ea6 1547 return false;
1548
1549 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
bcf22371 1550 && (DECL_P (TREE_OPERAND (expr, 0))
e6fa0ea6 1551 || handled_component_p (TREE_OPERAND (expr, 0))))
bcf22371 1552 {
1553 /* Casting the address of an object to non void pointer. Warn
1554 if the cast breaks type based aliasing. */
e6fa0ea6 1555 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1556 {
1557 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1558 "might break strict-aliasing rules");
1559 return true;
1560 }
bcf22371 1561 else
1562 {
48e1416a 1563 /* warn_strict_aliasing >= 3. This includes the default (3).
e6fa0ea6 1564 Only warn if the cast is dereferenced immediately. */
32c2fdea 1565 alias_set_type set1 =
e6fa0ea6 1566 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
32c2fdea 1567 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
bcf22371 1568
62d823d0 1569 if (set1 != set2 && set2 != 0
42bb3bfb 1570 && (set1 == 0
1571 || (!alias_set_subset_of (set2, set1)
1572 && !alias_sets_conflict_p (set1, set2))))
e6fa0ea6 1573 {
1574 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1575 "pointer will break strict-aliasing rules");
1576 return true;
1577 }
1578 else if (warn_strict_aliasing == 2
879f881c 1579 && !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 1580 {
1581 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1582 "pointer might break strict-aliasing rules");
1583 return true;
1584 }
bcf22371 1585 }
1586 }
e6fa0ea6 1587 else
1588 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1589 {
1590 /* At this level, warn for any conversions, even if an address is
1591 not taken in the same statement. This will likely produce many
1592 false positives, but could be useful to pinpoint problems that
1593 are not revealed at higher levels. */
32c2fdea 1594 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1595 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1596 if (!COMPLETE_TYPE_P (type)
879f881c 1597 || !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 1598 {
1599 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1600 "pointer might break strict-aliasing rules");
1601 return true;
1602 }
1603 }
1604
1605 return false;
bcf22371 1606}
1607
f003f5dc 1608/* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1609 sizeof as last operand of certain builtins. */
1610
1611void
57f872a2 1612sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
f1f41a6c 1613 vec<tree, va_gc> *params, tree *sizeof_arg,
f003f5dc 1614 bool (*comp_types) (tree, tree))
1615{
1616 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
57f872a2 1617 bool strop = false, cmp = false;
1618 unsigned int idx = ~0;
1619 location_t loc;
f003f5dc 1620
1621 if (TREE_CODE (callee) != FUNCTION_DECL
1622 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
f1f41a6c 1623 || vec_safe_length (params) <= 1)
f003f5dc 1624 return;
1625
f003f5dc 1626 switch (DECL_FUNCTION_CODE (callee))
1627 {
1628 case BUILT_IN_STRNCMP:
1629 case BUILT_IN_STRNCASECMP:
57f872a2 1630 cmp = true;
1631 /* FALLTHRU */
f003f5dc 1632 case BUILT_IN_STRNCPY:
57f872a2 1633 case BUILT_IN_STRNCPY_CHK:
f003f5dc 1634 case BUILT_IN_STRNCAT:
57f872a2 1635 case BUILT_IN_STRNCAT_CHK:
1636 case BUILT_IN_STPNCPY:
1637 case BUILT_IN_STPNCPY_CHK:
f003f5dc 1638 strop = true;
1639 /* FALLTHRU */
1640 case BUILT_IN_MEMCPY:
57f872a2 1641 case BUILT_IN_MEMCPY_CHK:
f003f5dc 1642 case BUILT_IN_MEMMOVE:
57f872a2 1643 case BUILT_IN_MEMMOVE_CHK:
f1f41a6c 1644 if (params->length () < 3)
57f872a2 1645 return;
f1f41a6c 1646 src = (*params)[1];
1647 dest = (*params)[0];
57f872a2 1648 idx = 2;
1649 break;
1650 case BUILT_IN_BCOPY:
f1f41a6c 1651 if (params->length () < 3)
57f872a2 1652 return;
f1f41a6c 1653 src = (*params)[0];
1654 dest = (*params)[1];
57f872a2 1655 idx = 2;
1656 break;
f003f5dc 1657 case BUILT_IN_MEMCMP:
57f872a2 1658 case BUILT_IN_BCMP:
f1f41a6c 1659 if (params->length () < 3)
f003f5dc 1660 return;
f1f41a6c 1661 src = (*params)[1];
1662 dest = (*params)[0];
57f872a2 1663 idx = 2;
1664 cmp = true;
f003f5dc 1665 break;
1666 case BUILT_IN_MEMSET:
57f872a2 1667 case BUILT_IN_MEMSET_CHK:
f1f41a6c 1668 if (params->length () < 3)
f003f5dc 1669 return;
f1f41a6c 1670 dest = (*params)[0];
57f872a2 1671 idx = 2;
1672 break;
1673 case BUILT_IN_BZERO:
f1f41a6c 1674 dest = (*params)[0];
57f872a2 1675 idx = 1;
f003f5dc 1676 break;
1677 case BUILT_IN_STRNDUP:
f1f41a6c 1678 src = (*params)[0];
f003f5dc 1679 strop = true;
57f872a2 1680 idx = 1;
1681 break;
1682 case BUILT_IN_MEMCHR:
f1f41a6c 1683 if (params->length () < 3)
57f872a2 1684 return;
f1f41a6c 1685 src = (*params)[0];
57f872a2 1686 idx = 2;
1687 break;
1688 case BUILT_IN_SNPRINTF:
1689 case BUILT_IN_SNPRINTF_CHK:
1690 case BUILT_IN_VSNPRINTF:
1691 case BUILT_IN_VSNPRINTF_CHK:
f1f41a6c 1692 dest = (*params)[0];
57f872a2 1693 idx = 1;
1694 strop = true;
f003f5dc 1695 break;
1696 default:
1697 break;
1698 }
1699
57f872a2 1700 if (idx >= 3)
1701 return;
1702
1703 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1704 return;
1705
1706 type = TYPE_P (sizeof_arg[idx])
1707 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1708 if (!POINTER_TYPE_P (type))
1709 return;
1710
f003f5dc 1711 if (dest
1712 && (tem = tree_strip_nop_conversions (dest))
1713 && POINTER_TYPE_P (TREE_TYPE (tem))
1714 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1715 return;
1716
1717 if (src
1718 && (tem = tree_strip_nop_conversions (src))
1719 && POINTER_TYPE_P (TREE_TYPE (tem))
1720 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1721 return;
1722
57f872a2 1723 loc = sizeof_arg_loc[idx];
1724
1725 if (dest && !cmp)
f003f5dc 1726 {
57f872a2 1727 if (!TYPE_P (sizeof_arg[idx])
1728 && operand_equal_p (dest, sizeof_arg[idx], 0)
f003f5dc 1729 && comp_types (TREE_TYPE (dest), type))
1730 {
57f872a2 1731 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
f003f5dc 1732 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1733 "argument to %<sizeof%> in %qD call is the same "
1734 "expression as the destination; did you mean to "
1735 "remove the addressof?", callee);
1736 else if ((TYPE_PRECISION (TREE_TYPE (type))
1737 == TYPE_PRECISION (char_type_node))
1738 || strop)
1739 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1740 "argument to %<sizeof%> in %qD call is the same "
1741 "expression as the destination; did you mean to "
1742 "provide an explicit length?", callee);
1743 else
1744 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1745 "argument to %<sizeof%> in %qD call is the same "
1746 "expression as the destination; did you mean to "
1747 "dereference it?", callee);
1748 return;
1749 }
1750
1751 if (POINTER_TYPE_P (TREE_TYPE (dest))
1752 && !strop
1753 && comp_types (TREE_TYPE (dest), type)
1754 && !VOID_TYPE_P (TREE_TYPE (type)))
1755 {
1756 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1757 "argument to %<sizeof%> in %qD call is the same "
1758 "pointer type %qT as the destination; expected %qT "
1759 "or an explicit length", callee, TREE_TYPE (dest),
1760 TREE_TYPE (TREE_TYPE (dest)));
1761 return;
1762 }
1763 }
1764
57f872a2 1765 if (src && !cmp)
f003f5dc 1766 {
57f872a2 1767 if (!TYPE_P (sizeof_arg[idx])
1768 && operand_equal_p (src, sizeof_arg[idx], 0)
f003f5dc 1769 && comp_types (TREE_TYPE (src), type))
1770 {
57f872a2 1771 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
f003f5dc 1772 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1773 "argument to %<sizeof%> in %qD call is the same "
1774 "expression as the source; did you mean to "
1775 "remove the addressof?", callee);
1776 else if ((TYPE_PRECISION (TREE_TYPE (type))
1777 == TYPE_PRECISION (char_type_node))
1778 || strop)
1779 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1780 "argument to %<sizeof%> in %qD call is the same "
1781 "expression as the source; did you mean to "
1782 "provide an explicit length?", callee);
1783 else
1784 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1785 "argument to %<sizeof%> in %qD call is the same "
1786 "expression as the source; did you mean to "
1787 "dereference it?", callee);
1788 return;
1789 }
1790
1791 if (POINTER_TYPE_P (TREE_TYPE (src))
1792 && !strop
1793 && comp_types (TREE_TYPE (src), type)
1794 && !VOID_TYPE_P (TREE_TYPE (type)))
1795 {
1796 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1797 "argument to %<sizeof%> in %qD call is the same "
1798 "pointer type %qT as the source; expected %qT "
1799 "or an explicit length", callee, TREE_TYPE (src),
1800 TREE_TYPE (TREE_TYPE (src)));
1801 return;
1802 }
1803 }
57f872a2 1804
1805 if (dest)
1806 {
1807 if (!TYPE_P (sizeof_arg[idx])
1808 && operand_equal_p (dest, sizeof_arg[idx], 0)
1809 && comp_types (TREE_TYPE (dest), type))
1810 {
1811 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1812 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1813 "argument to %<sizeof%> in %qD call is the same "
1814 "expression as the first source; did you mean to "
1815 "remove the addressof?", callee);
1816 else if ((TYPE_PRECISION (TREE_TYPE (type))
1817 == TYPE_PRECISION (char_type_node))
1818 || strop)
1819 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1820 "argument to %<sizeof%> in %qD call is the same "
1821 "expression as the first source; did you mean to "
1822 "provide an explicit length?", callee);
1823 else
1824 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1825 "argument to %<sizeof%> in %qD call is the same "
1826 "expression as the first source; did you mean to "
1827 "dereference it?", callee);
1828 return;
1829 }
1830
1831 if (POINTER_TYPE_P (TREE_TYPE (dest))
1832 && !strop
1833 && comp_types (TREE_TYPE (dest), type)
1834 && !VOID_TYPE_P (TREE_TYPE (type)))
1835 {
1836 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1837 "argument to %<sizeof%> in %qD call is the same "
1838 "pointer type %qT as the first source; expected %qT "
1839 "or an explicit length", callee, TREE_TYPE (dest),
1840 TREE_TYPE (TREE_TYPE (dest)));
1841 return;
1842 }
1843 }
1844
1845 if (src)
1846 {
1847 if (!TYPE_P (sizeof_arg[idx])
1848 && operand_equal_p (src, sizeof_arg[idx], 0)
1849 && comp_types (TREE_TYPE (src), type))
1850 {
1851 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1852 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1853 "argument to %<sizeof%> in %qD call is the same "
1854 "expression as the second source; did you mean to "
1855 "remove the addressof?", callee);
1856 else if ((TYPE_PRECISION (TREE_TYPE (type))
1857 == TYPE_PRECISION (char_type_node))
1858 || strop)
1859 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1860 "argument to %<sizeof%> in %qD call is the same "
1861 "expression as the second source; did you mean to "
1862 "provide an explicit length?", callee);
1863 else
1864 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1865 "argument to %<sizeof%> in %qD call is the same "
1866 "expression as the second source; did you mean to "
1867 "dereference it?", callee);
1868 return;
1869 }
1870
1871 if (POINTER_TYPE_P (TREE_TYPE (src))
1872 && !strop
1873 && comp_types (TREE_TYPE (src), type)
1874 && !VOID_TYPE_P (TREE_TYPE (type)))
1875 {
1876 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1877 "argument to %<sizeof%> in %qD call is the same "
1878 "pointer type %qT as the second source; expected %qT "
1879 "or an explicit length", callee, TREE_TYPE (src),
1880 TREE_TYPE (TREE_TYPE (src)));
1881 return;
1882 }
1883 }
1884
f003f5dc 1885}
1886
3f08e399 1887/* Warn for unlikely, improbable, or stupid DECL declarations
1888 of `main'. */
1889
1890void
1891check_main_parameter_types (tree decl)
1892{
d0af78c5 1893 function_args_iterator iter;
1894 tree type;
3f08e399 1895 int argct = 0;
1896
d0af78c5 1897 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
1898 {
1899 /* XXX void_type_node belies the abstraction. */
1900 if (type == void_type_node || type == error_mark_node )
1901 break;
1902
2026249a 1903 tree t = type;
1904 if (TYPE_ATOMIC (t))
1905 pedwarn (input_location, OPT_Wmain,
1906 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1907 type, decl);
1908 while (POINTER_TYPE_P (t))
1909 {
1910 t = TREE_TYPE (t);
1911 if (TYPE_ATOMIC (t))
1912 pedwarn (input_location, OPT_Wmain,
1913 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1914 type, decl);
1915 }
1916
d0af78c5 1917 ++argct;
1918 switch (argct)
1919 {
1920 case 1:
1921 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1922 pedwarn (input_location, OPT_Wmain,
1923 "first argument of %q+D should be %<int%>", decl);
1924 break;
1925
1926 case 2:
1927 if (TREE_CODE (type) != POINTER_TYPE
1928 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1929 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1930 != char_type_node))
1931 pedwarn (input_location, OPT_Wmain,
1932 "second argument of %q+D should be %<char **%>", decl);
1933 break;
1934
1935 case 3:
1936 if (TREE_CODE (type) != POINTER_TYPE
1937 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1938 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1939 != char_type_node))
1940 pedwarn (input_location, OPT_Wmain,
1941 "third argument of %q+D should probably be "
1942 "%<char **%>", decl);
1943 break;
1944 }
1945 }
3f08e399 1946
1947 /* It is intentional that this message does not mention the third
1948 argument because it's only mentioned in an appendix of the
1949 standard. */
1950 if (argct > 0 && (argct < 2 || argct > 3))
d0af78c5 1951 pedwarn (input_location, OPT_Wmain,
1952 "%q+D takes only zero or two arguments", decl);
90e645fa 1953
1954 if (stdarg_p (TREE_TYPE (decl)))
1955 pedwarn (input_location, OPT_Wmain,
1956 "%q+D declared as variadic function", decl);
3f08e399 1957}
1958
73437615 1959/* vector_targets_convertible_p is used for vector pointer types. The
1960 callers perform various checks that the qualifiers are satisfactory,
1961 while OTOH vector_targets_convertible_p ignores the number of elements
1962 in the vectors. That's fine with vector pointers as we can consider,
1963 say, a vector of 8 elements as two consecutive vectors of 4 elements,
1964 and that does not require and conversion of the pointer values.
1965 In contrast, vector_types_convertible_p and
1966 vector_types_compatible_elements_p are used for vector value types. */
ed7c4e62 1967/* True if pointers to distinct types T1 and T2 can be converted to
1968 each other without an explicit cast. Only returns true for opaque
1969 vector types. */
1970bool
1971vector_targets_convertible_p (const_tree t1, const_tree t2)
1972{
6290f0db 1973 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
8d125f7d 1974 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ed7c4e62 1975 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1976 return true;
1977
1978 return false;
1979}
1980
73437615 1981/* vector_types_convertible_p is used for vector value types.
1982 It could in principle call vector_targets_convertible_p as a subroutine,
1983 but then the check for vector type would be duplicated with its callers,
1984 and also the purpose of vector_targets_convertible_p would become
1985 muddled.
1986 Where vector_types_convertible_p returns true, a conversion might still be
1987 needed to make the types match.
1988 In contrast, vector_targets_convertible_p is used for vector pointer
1989 values, and vector_types_compatible_elements_p is used specifically
1990 in the context for binary operators, as a check if use is possible without
1991 conversion. */
546c4794 1992/* True if vector types T1 and T2 can be converted to each other
1993 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
1994 can only be converted with -flax-vector-conversions yet that is not
1995 in effect, emit a note telling the user about that option if such
1996 a note has not previously been emitted. */
1997bool
9f627b1a 1998vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
8b4b9810 1999{
546c4794 2000 static bool emitted_lax_note = false;
ae6db8ab 2001 bool convertible_lax;
2002
8d125f7d 2003 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ae6db8ab 2004 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2005 return true;
2006
2007 convertible_lax =
2008 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2009 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
432dd330 2010 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
ae6db8ab 2011 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2012 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
546c4794 2013
2014 if (!convertible_lax || flag_lax_vector_conversions)
2015 return convertible_lax;
2016
2017 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
29f51994 2018 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
546c4794 2019 return true;
2020
2021 if (emit_lax_note && !emitted_lax_note)
2022 {
2023 emitted_lax_note = true;
5bcc316e 2024 inform (input_location, "use -flax-vector-conversions to permit "
546c4794 2025 "conversions between vectors with differing "
2026 "element types or numbers of subparts");
2027 }
2028
2029 return false;
8b4b9810 2030}
2031
bf0cb017 2032/* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2033 and have vector types, V0 has the same type as V1, and the number of
2034 elements of V0, V1, MASK is the same.
2035
2036 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2037 called with two arguments. In this case implementation passes the
2038 first argument twice in order to share the same tree code. This fact
2039 could enable the mask-values being twice the vector length. This is
2040 an implementation accident and this semantics is not guaranteed to
2041 the user. */
2042tree
68ea4406 2043c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2044 bool complain)
bf0cb017 2045{
2046 tree ret;
2047 bool wrap = true;
2048 bool maybe_const = false;
2049 bool two_arguments = false;
2050
2051 if (v1 == NULL_TREE)
2052 {
2053 two_arguments = true;
2054 v1 = v0;
2055 }
2056
2057 if (v0 == error_mark_node || v1 == error_mark_node
2058 || mask == error_mark_node)
2059 return error_mark_node;
2060
6290f0db 2061 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
bf0cb017 2062 {
68ea4406 2063 if (complain)
2064 error_at (loc, "__builtin_shuffle last argument must "
2065 "be an integer vector");
bf0cb017 2066 return error_mark_node;
2067 }
2068
6290f0db 2069 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
2070 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
bf0cb017 2071 {
68ea4406 2072 if (complain)
2073 error_at (loc, "__builtin_shuffle arguments must be vectors");
bf0cb017 2074 return error_mark_node;
2075 }
2076
2077 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2078 {
68ea4406 2079 if (complain)
2080 error_at (loc, "__builtin_shuffle argument vectors must be of "
2081 "the same type");
bf0cb017 2082 return error_mark_node;
2083 }
2084
2085 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2086 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2087 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2088 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2089 {
68ea4406 2090 if (complain)
2091 error_at (loc, "__builtin_shuffle number of elements of the "
2092 "argument vector(s) and the mask vector should "
2093 "be the same");
bf0cb017 2094 return error_mark_node;
2095 }
2096
2097 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2098 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2099 {
68ea4406 2100 if (complain)
2101 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2102 "must have the same size as inner type of the mask");
bf0cb017 2103 return error_mark_node;
2104 }
2105
2106 if (!c_dialect_cxx ())
2107 {
2108 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2109 v0 = c_fully_fold (v0, false, &maybe_const);
2110 wrap &= maybe_const;
2111
2112 if (two_arguments)
2113 v1 = v0 = save_expr (v0);
2114 else
2115 {
2116 v1 = c_fully_fold (v1, false, &maybe_const);
2117 wrap &= maybe_const;
2118 }
2119
2120 mask = c_fully_fold (mask, false, &maybe_const);
2121 wrap &= maybe_const;
2122 }
68ea4406 2123 else if (two_arguments)
2124 v1 = v0 = save_expr (v0);
bf0cb017 2125
2126 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2127
2128 if (!c_dialect_cxx () && !wrap)
2129 ret = c_wrap_maybe_const (ret, true);
2130
2131 return ret;
2132}
2133
7f506bca 2134/* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2135 to integral type. */
2136
2137static tree
2138c_common_get_narrower (tree op, int *unsignedp_ptr)
2139{
2140 op = get_narrower (op, unsignedp_ptr);
2141
2142 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2143 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2144 {
2145 /* C++0x scoped enumerations don't implicitly convert to integral
2146 type; if we stripped an explicit conversion to a larger type we
2147 need to replace it so common_type will still work. */
a51edb4c 2148 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2149 TYPE_UNSIGNED (TREE_TYPE (op)));
7f506bca 2150 op = fold_convert (type, op);
2151 }
2152 return op;
2153}
2154
2561cea2 2155/* This is a helper function of build_binary_op.
2156
2157 For certain operations if both args were extended from the same
2158 smaller type, do the arithmetic in that type and then extend.
2159
2160 BITWISE indicates a bitwise operation.
2161 For them, this optimization is safe only if
2162 both args are zero-extended or both are sign-extended.
2163 Otherwise, we might change the result.
2164 Eg, (short)-1 | (unsigned short)-1 is (int)-1
48e1416a 2165 but calculated in (unsigned short) it would be (unsigned short)-1.
2561cea2 2166*/
7f506bca 2167tree
2168shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2561cea2 2169{
2170 int unsigned0, unsigned1;
2171 tree arg0, arg1;
2172 int uns;
2173 tree type;
2174
2175 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2176 excessive narrowing when we call get_narrower below. For
2177 example, suppose that OP0 is of unsigned int extended
2178 from signed char and that RESULT_TYPE is long long int.
2179 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2180 like
48e1416a 2181
2561cea2 2182 (long long int) (unsigned int) signed_char
2183
2184 which get_narrower would narrow down to
48e1416a 2185
2561cea2 2186 (unsigned int) signed char
48e1416a 2187
2561cea2 2188 If we do not cast OP0 first, get_narrower would return
2189 signed_char, which is inconsistent with the case of the
2190 explicit cast. */
2191 op0 = convert (result_type, op0);
2192 op1 = convert (result_type, op1);
2193
7f506bca 2194 arg0 = c_common_get_narrower (op0, &unsigned0);
2195 arg1 = c_common_get_narrower (op1, &unsigned1);
ab2c1de8 2196
2561cea2 2197 /* UNS is 1 if the operation to be done is an unsigned one. */
2198 uns = TYPE_UNSIGNED (result_type);
2199
2200 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2201 but it *requires* conversion to FINAL_TYPE. */
48e1416a 2202
2561cea2 2203 if ((TYPE_PRECISION (TREE_TYPE (op0))
2204 == TYPE_PRECISION (TREE_TYPE (arg0)))
2205 && TREE_TYPE (op0) != result_type)
2206 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2207 if ((TYPE_PRECISION (TREE_TYPE (op1))
2208 == TYPE_PRECISION (TREE_TYPE (arg1)))
2209 && TREE_TYPE (op1) != result_type)
2210 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
48e1416a 2211
2561cea2 2212 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
48e1416a 2213
2561cea2 2214 /* For bitwise operations, signedness of nominal type
2215 does not matter. Consider only how operands were extended. */
2216 if (bitwise)
2217 uns = unsigned0;
48e1416a 2218
2561cea2 2219 /* Note that in all three cases below we refrain from optimizing
2220 an unsigned operation on sign-extended args.
2221 That would not be valid. */
48e1416a 2222
2561cea2 2223 /* Both args variable: if both extended in same way
2224 from same width, do it in that width.
2225 Do it unsigned if args were zero-extended. */
2226 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2227 < TYPE_PRECISION (result_type))
2228 && (TYPE_PRECISION (TREE_TYPE (arg1))
2229 == TYPE_PRECISION (TREE_TYPE (arg0)))
2230 && unsigned0 == unsigned1
2231 && (unsigned0 || !uns))
2232 return c_common_signed_or_unsigned_type
2233 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2234
2235 else if (TREE_CODE (arg0) == INTEGER_CST
2236 && (unsigned1 || !uns)
2237 && (TYPE_PRECISION (TREE_TYPE (arg1))
2238 < TYPE_PRECISION (result_type))
2239 && (type
2240 = c_common_signed_or_unsigned_type (unsigned1,
2241 TREE_TYPE (arg1)))
2242 && !POINTER_TYPE_P (type)
2243 && int_fits_type_p (arg0, type))
2244 return type;
2245
2246 else if (TREE_CODE (arg1) == INTEGER_CST
2247 && (unsigned0 || !uns)
2248 && (TYPE_PRECISION (TREE_TYPE (arg0))
2249 < TYPE_PRECISION (result_type))
2250 && (type
2251 = c_common_signed_or_unsigned_type (unsigned0,
2252 TREE_TYPE (arg0)))
2253 && !POINTER_TYPE_P (type)
2254 && int_fits_type_p (arg1, type))
2255 return type;
2256
2257 return result_type;
2258}
2259
e53013a8 2260/* Returns true iff any integer value of type FROM_TYPE can be represented as
2261 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2262
2263static bool
2264int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2265{
2266 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2267 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2268 REAL_VALUE_TYPE real_low_bound =
2269 real_value_from_int_cst (0, type_low_bound);
2270 REAL_VALUE_TYPE real_high_bound =
2271 real_value_from_int_cst (0, type_high_bound);
2272
2273 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2274 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2275}
2276
2277/* Checks if expression EXPR of complex/real/integer type cannot be converted
2278 to the complex/real/integer type TYPE. Function returns non-zero when:
22a75734 2279 * EXPR is a constant which cannot be exactly converted to TYPE.
2280 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
e53013a8 2281 for EXPR type and TYPE being both integers or both real, or both
2282 complex.
2283 * EXPR is not a constant of complex type and TYPE is a real or
2284 an integer.
22a75734 2285 * EXPR is not a constant of real type and TYPE is an integer.
2286 * EXPR is not a constant of integer type which cannot be
2287 exactly converted to real type.
e53013a8 2288
7dfa155b 2289 Function allows conversions between types of different signedness and
ca9d7d74 2290 can return SAFE_CONVERSION (zero) in that case. Function can produce
e53013a8 2291 signedness warnings if PRODUCE_WARNS is true.
2292
2293 Function allows conversions from complex constants to non-complex types,
2294 provided that imaginary part is zero and real part can be safely converted
2295 to TYPE. */
22a75734 2296
ca9d7d74 2297enum conversion_safety
22a75734 2298unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
d31d55f0 2299{
ca9d7d74 2300 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
27259707 2301 tree expr_type = TREE_TYPE (expr);
22a75734 2302 loc = expansion_point_location_if_in_system_header (loc);
d31d55f0 2303
7dfa155b 2304 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
5b16c152 2305 {
e53013a8 2306 /* If type is complex, we are interested in compatibility with
2307 underlying type. */
2308 if (TREE_CODE (type) == COMPLEX_TYPE)
2309 type = TREE_TYPE (type);
2310
d31d55f0 2311 /* Warn for real constant that is not an exact integer converted
7dfa155b 2312 to integer type. */
27259707 2313 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2314 && TREE_CODE (type) == INTEGER_TYPE)
2315 {
2316 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
ca9d7d74 2317 give_warning = UNSAFE_REAL;
7dfa155b 2318 }
da1fb07b 2319 /* Warn for an integer constant that does not fit into integer type. */
27259707 2320 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2321 && TREE_CODE (type) == INTEGER_TYPE
2322 && !int_fits_type_p (expr, type))
2323 {
2324 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
27259707 2325 && tree_int_cst_sgn (expr) < 0)
7dfa155b 2326 {
2327 if (produce_warns)
2328 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2329 " implicitly converted to unsigned type");
2330 }
2331 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2332 {
2333 if (produce_warns)
2334 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2335 " constant value to negative integer");
2336 }
7ee0d227 2337 else
ca9d7d74 2338 give_warning = UNSAFE_OTHER;
7dfa155b 2339 }
d31d55f0 2340 else if (TREE_CODE (type) == REAL_TYPE)
7dfa155b 2341 {
2342 /* Warn for an integer constant that does not fit into real type. */
2343 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2344 {
2345 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2346 if (!exact_real_truncate (TYPE_MODE (type), &a))
ca9d7d74 2347 give_warning = UNSAFE_REAL;
7dfa155b 2348 }
2349 /* Warn for a real constant that does not fit into a smaller
2350 real type. */
2351 else if (TREE_CODE (expr_type) == REAL_TYPE
2352 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2353 {
2354 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2355 if (!exact_real_truncate (TYPE_MODE (type), &a))
ca9d7d74 2356 give_warning = UNSAFE_REAL;
7dfa155b 2357 }
2358 }
2359 }
e53013a8 2360
2361 else if (TREE_CODE (expr) == COMPLEX_CST)
2362 {
2363 tree imag_part = TREE_IMAGPART (expr);
2364 /* Conversion from complex constant with zero imaginary part,
2365 perform check for conversion of real part. */
2366 if ((TREE_CODE (imag_part) == REAL_CST
2367 && real_zerop (imag_part))
2368 || (TREE_CODE (imag_part) == INTEGER_CST
2369 && integer_zerop (imag_part)))
2370 /* Note: in this branch we use recursive call to unsafe_conversion_p
2371 with different type of EXPR, but it is still safe, because when EXPR
2372 is a constant, it's type is not used in text of generated warnings
2373 (otherwise they could sound misleading). */
2374 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2375 produce_warns);
2376 /* Conversion from complex constant with non-zero imaginary part. */
2377 else
2378 {
2379 /* Conversion to complex type.
2380 Perform checks for both real and imaginary parts. */
2381 if (TREE_CODE (type) == COMPLEX_TYPE)
2382 {
2383 /* Unfortunately, produce_warns must be false in two subsequent
2384 calls of unsafe_conversion_p, because otherwise we could
2385 produce strange "double" warnings, if both real and imaginary
2386 parts have conversion problems related to signedness.
2387
2388 For example:
2389 int32_t _Complex a = 0x80000000 + 0x80000000i;
2390
2391 Possible solution: add a separate function for checking
2392 constants and combine result of two calls appropriately. */
2393 enum conversion_safety re_safety =
2394 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2395 enum conversion_safety im_safety =
2396 unsafe_conversion_p (loc, type, imag_part, false);
2397
2398 /* Merge the results into appropriate single warning. */
2399
2400 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2401 if (re_safety == im_safety)
2402 give_warning = re_safety;
2403 else if (!re_safety && im_safety)
2404 give_warning = im_safety;
2405 else if (re_safety && !im_safety)
2406 give_warning = re_safety;
2407 else
2408 give_warning = UNSAFE_OTHER;
2409 }
2410 /* Warn about conversion from complex to real or integer type. */
2411 else
2412 give_warning = UNSAFE_IMAGINARY;
2413 }
2414 }
2415
2416 /* Checks for remaining case: EXPR is not constant. */
7dfa155b 2417 else
2418 {
d31d55f0 2419 /* Warn for real types converted to integer types. */
2561cea2 2420 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2421 && TREE_CODE (type) == INTEGER_TYPE)
ca9d7d74 2422 give_warning = UNSAFE_REAL;
d31d55f0 2423
2561cea2 2424 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2425 && TREE_CODE (type) == INTEGER_TYPE)
2426 {
69609004 2427 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
f9d856a4 2428 expr = get_unwidened (expr, 0);
2561cea2 2429 expr_type = TREE_TYPE (expr);
69609004 2430
2561cea2 2431 /* Don't warn for short y; short x = ((int)y & 0xff); */
48e1416a 2432 if (TREE_CODE (expr) == BIT_AND_EXPR
7dfa155b 2433 || TREE_CODE (expr) == BIT_IOR_EXPR
2561cea2 2434 || TREE_CODE (expr) == BIT_XOR_EXPR)
2435 {
27259707 2436 /* If both args were extended from a shortest type,
2437 use that type if that is safe. */
48e1416a 2438 expr_type = shorten_binary_op (expr_type,
2439 TREE_OPERAND (expr, 0),
2440 TREE_OPERAND (expr, 1),
2561cea2 2441 /* bitwise */1);
2442
2561cea2 2443 if (TREE_CODE (expr) == BIT_AND_EXPR)
2444 {
2445 tree op0 = TREE_OPERAND (expr, 0);
2446 tree op1 = TREE_OPERAND (expr, 1);
30de145b 2447 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2448 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2449
2450 /* If one of the operands is a non-negative constant
2451 that fits in the target type, then the type of the
2452 other operand does not matter. */
2561cea2 2453 if ((TREE_CODE (op0) == INTEGER_CST
2454 && int_fits_type_p (op0, c_common_signed_type (type))
2455 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2456 || (TREE_CODE (op1) == INTEGER_CST
27259707 2457 && int_fits_type_p (op1, c_common_signed_type (type))
48e1416a 2458 && int_fits_type_p (op1,
27259707 2459 c_common_unsigned_type (type))))
ca9d7d74 2460 return SAFE_CONVERSION;
30de145b 2461 /* If constant is unsigned and fits in the target
2462 type, then the result will also fit. */
2463 else if ((TREE_CODE (op0) == INTEGER_CST
48e1416a 2464 && unsigned0
30de145b 2465 && int_fits_type_p (op0, type))
2466 || (TREE_CODE (op1) == INTEGER_CST
2467 && unsigned1
2468 && int_fits_type_p (op1, type)))
ca9d7d74 2469 return SAFE_CONVERSION;
2561cea2 2470 }
2471 }
7dfa155b 2472 /* Warn for integer types converted to smaller integer types. */
48e1416a 2473 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
ca9d7d74 2474 give_warning = UNSAFE_OTHER;
7ee0d227 2475
2476 /* When they are the same width but different signedness,
2477 then the value may change. */
7dfa155b 2478 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2561cea2 2479 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
7ee0d227 2480 /* Even when converted to a bigger type, if the type is
2481 unsigned but expr is signed, then negative values
2482 will be changed. */
7dfa155b 2483 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2484 && produce_warns)
200dd99c 2485 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2486 "may change the sign of the result",
2487 type, expr_type);
7dfa155b 2488 }
d31d55f0 2489
2490 /* Warn for integer types converted to real types if and only if
7dfa155b 2491 all the range of values of the integer type cannot be
2492 represented by the real type. */
2561cea2 2493 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2494 && TREE_CODE (type) == REAL_TYPE)
2495 {
4c2cfa81 2496 /* Don't warn about char y = 0xff; float x = (int) y; */
2497 expr = get_unwidened (expr, 0);
2498 expr_type = TREE_TYPE (expr);
2499
e53013a8 2500 if (!int_safely_convertible_to_real_p (expr_type, type))
ca9d7d74 2501 give_warning = UNSAFE_OTHER;
7dfa155b 2502 }
d31d55f0 2503
2504 /* Warn for real types converted to smaller real types. */
2561cea2 2505 else if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2506 && TREE_CODE (type) == REAL_TYPE
2507 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
ca9d7d74 2508 give_warning = UNSAFE_REAL;
e53013a8 2509
2510 /* Check conversion between two complex types. */
2511 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2512 && TREE_CODE (type) == COMPLEX_TYPE)
2513 {
2514 /* Extract underlying types (i.e., type of real and imaginary
2515 parts) of expr_type and type. */
2516 tree from_type = TREE_TYPE (expr_type);
2517 tree to_type = TREE_TYPE (type);
2518
2519 /* Warn for real types converted to integer types. */
2520 if (TREE_CODE (from_type) == REAL_TYPE
2521 && TREE_CODE (to_type) == INTEGER_TYPE)
2522 give_warning = UNSAFE_REAL;
2523
2524 /* Warn for real types converted to smaller real types. */
2525 else if (TREE_CODE (from_type) == REAL_TYPE
2526 && TREE_CODE (to_type) == REAL_TYPE
2527 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2528 give_warning = UNSAFE_REAL;
2529
2530 /* Check conversion for complex integer types. Here implementation
2531 is simpler than for real-domain integers because it does not
2532 involve sophisticated cases, such as bitmasks, casts, etc. */
2533 else if (TREE_CODE (from_type) == INTEGER_TYPE
2534 && TREE_CODE (to_type) == INTEGER_TYPE)
2535 {
2536 /* Warn for integer types converted to smaller integer types. */
2537 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2538 give_warning = UNSAFE_OTHER;
2539
2540 /* Check for different signedness, see case for real-domain
2541 integers (above) for a more detailed comment. */
2542 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
2543 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
2544 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
2545 && produce_warns)
2546 warning_at (loc, OPT_Wsign_conversion,
2547 "conversion to %qT from %qT "
2548 "may change the sign of the result",
2549 type, expr_type);
2550 }
2551 else if (TREE_CODE (from_type) == INTEGER_TYPE
2552 && TREE_CODE (to_type) == REAL_TYPE
2553 && !int_safely_convertible_to_real_p (from_type, to_type))
2554 give_warning = UNSAFE_OTHER;
2555 }
2556
2557 /* Warn for complex types converted to real or integer types. */
2558 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2559 && TREE_CODE (type) != COMPLEX_TYPE)
2560 give_warning = UNSAFE_IMAGINARY;
7dfa155b 2561 }
2562
2563 return give_warning;
2564}
2565
2566/* Warns if the conversion of EXPR to TYPE may alter a value.
2567 This is a helper function for warnings_for_convert_and_check. */
2568
2569static void
22a75734 2570conversion_warning (location_t loc, tree type, tree expr)
7dfa155b 2571{
7dfa155b 2572 tree expr_type = TREE_TYPE (expr);
ca9d7d74 2573 enum conversion_safety conversion_kind;
d31d55f0 2574
ca9d7d74 2575 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
7dfa155b 2576 return;
d31d55f0 2577
ec704957 2578 /* This may happen, because for LHS op= RHS we preevaluate
2579 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2580 means we could no longer see the code of the EXPR. */
2581 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2582 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2583 if (TREE_CODE (expr) == SAVE_EXPR)
2584 expr = TREE_OPERAND (expr, 0);
2585
7dfa155b 2586 switch (TREE_CODE (expr))
2587 {
2588 case EQ_EXPR:
2589 case NE_EXPR:
2590 case LE_EXPR:
2591 case GE_EXPR:
2592 case LT_EXPR:
2593 case GT_EXPR:
2594 case TRUTH_ANDIF_EXPR:
2595 case TRUTH_ORIF_EXPR:
2596 case TRUTH_AND_EXPR:
2597 case TRUTH_OR_EXPR:
2598 case TRUTH_XOR_EXPR:
2599 case TRUTH_NOT_EXPR:
2600 /* Conversion from boolean to a signed:1 bit-field (which only
2601 can hold the values 0 and -1) doesn't lose information - but
2602 it does change the value. */
2603 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2604 warning_at (loc, OPT_Wconversion,
2605 "conversion to %qT from boolean expression", type);
2606 return;
2607
2608 case REAL_CST:
2609 case INTEGER_CST:
e53013a8 2610 case COMPLEX_CST:
22a75734 2611 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
ca9d7d74 2612 if (conversion_kind == UNSAFE_REAL)
2613 warning_at (loc, OPT_Wfloat_conversion,
2614 "conversion to %qT alters %qT constant value",
2615 type, expr_type);
2616 else if (conversion_kind)
7dfa155b 2617 warning_at (loc, OPT_Wconversion,
2618 "conversion to %qT alters %qT constant value",
2619 type, expr_type);
2620 return;
2621
2622 case COND_EXPR:
2623 {
0e4e775a 2624 /* In case of COND_EXPR, we do not care about the type of
2625 COND_EXPR, only about the conversion of each operand. */
2626 tree op1 = TREE_OPERAND (expr, 1);
2627 tree op2 = TREE_OPERAND (expr, 2);
2628
22a75734 2629 conversion_warning (loc, type, op1);
2630 conversion_warning (loc, type, op2);
0e4e775a 2631 return;
7dfa155b 2632 }
2633
2634 default: /* 'expr' is not a constant. */
22a75734 2635 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
ca9d7d74 2636 if (conversion_kind == UNSAFE_REAL)
2637 warning_at (loc, OPT_Wfloat_conversion,
2638 "conversion to %qT from %qT may alter its value",
2639 type, expr_type);
e53013a8 2640 else if (conversion_kind == UNSAFE_IMAGINARY)
2641 warning_at (loc, OPT_Wconversion,
2642 "conversion to %qT from %qT discards imaginary component",
2643 type, expr_type);
ca9d7d74 2644 else if (conversion_kind)
7dfa155b 2645 warning_at (loc, OPT_Wconversion,
200dd99c 2646 "conversion to %qT from %qT may alter its value",
2647 type, expr_type);
d31d55f0 2648 }
2649}
2650
59dd8856 2651/* Produce warnings after a conversion. RESULT is the result of
2652 converting EXPR to TYPE. This is a helper function for
2653 convert_and_check and cp_convert_and_check. */
2a1736ed 2654
59dd8856 2655void
22a75734 2656warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2657 tree result)
2a1736ed 2658{
22a75734 2659 loc = expansion_point_location_if_in_system_header (loc);
61f69bc9 2660
da1fb07b 2661 if (TREE_CODE (expr) == INTEGER_CST
2662 && (TREE_CODE (type) == INTEGER_TYPE
2663 || TREE_CODE (type) == ENUMERAL_TYPE)
2664 && !int_fits_type_p (expr, type))
2665 {
d31d55f0 2666 /* Do not diagnose overflow in a constant expression merely
2667 because a conversion overflowed. */
da1fb07b 2668 if (TREE_OVERFLOW (result))
eddad94a 2669 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2670
da1fb07b 2671 if (TYPE_UNSIGNED (type))
d31d55f0 2672 {
da1fb07b 2673 /* This detects cases like converting -129 or 256 to
2674 unsigned char. */
2675 if (!int_fits_type_p (expr, c_common_signed_type (type)))
61f69bc9 2676 warning_at (loc, OPT_Woverflow,
2677 "large integer implicitly truncated to unsigned type");
7ee0d227 2678 else
22a75734 2679 conversion_warning (loc, type, expr);
da1fb07b 2680 }
48e1416a 2681 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
22a75734 2682 warning_at (loc, OPT_Woverflow,
e0913805 2683 "overflow in implicit constant conversion");
2684 /* No warning for converting 0x80000000 to int. */
2685 else if (pedantic
2686 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2687 || TYPE_PRECISION (TREE_TYPE (expr))
2688 != TYPE_PRECISION (type)))
61f69bc9 2689 warning_at (loc, OPT_Woverflow,
2690 "overflow in implicit constant conversion");
e0913805 2691
7ee0d227 2692 else
22a75734 2693 conversion_warning (loc, type, expr);
2a1736ed 2694 }
9421ebb9 2695 else if ((TREE_CODE (result) == INTEGER_CST
2696 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
61f69bc9 2697 warning_at (loc, OPT_Woverflow,
2698 "overflow in implicit constant conversion");
7ee0d227 2699 else
22a75734 2700 conversion_warning (loc, type, expr);
59dd8856 2701}
2702
2703
2704/* Convert EXPR to TYPE, warning about conversion problems with constants.
2705 Invoke this function on every expression that is converted implicitly,
2706 i.e. because of language rules and not because of an explicit cast. */
2707
2708tree
22a75734 2709convert_and_check (location_t loc, tree type, tree expr)
59dd8856 2710{
2711 tree result;
c6418a4e 2712 tree expr_for_warning;
2713
2714 /* Convert from a value with possible excess precision rather than
2715 via the semantic type, but do not warn about values not fitting
2716 exactly in the semantic type. */
2717 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2718 {
2719 tree orig_type = TREE_TYPE (expr);
2720 expr = TREE_OPERAND (expr, 0);
2721 expr_for_warning = convert (orig_type, expr);
2722 if (orig_type == type)
2723 return expr_for_warning;
2724 }
2725 else
2726 expr_for_warning = expr;
59dd8856 2727
2728 if (TREE_TYPE (expr) == type)
2729 return expr;
48e1416a 2730
59dd8856 2731 result = convert (type, expr);
2732
48d94ede 2733 if (c_inhibit_evaluation_warnings == 0
2734 && !TREE_OVERFLOW_P (expr)
2735 && result != error_mark_node)
22a75734 2736 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
59dd8856 2737
da1fb07b 2738 return result;
b2806639 2739}
2740\f
4e91a871 2741/* A node in a list that describes references to variables (EXPR), which are
2742 either read accesses if WRITER is zero, or write accesses, in which case
2743 WRITER is the parent of EXPR. */
2744struct tlist
2745{
2746 struct tlist *next;
2747 tree expr, writer;
2748};
2749
2750/* Used to implement a cache the results of a call to verify_tree. We only
2751 use this for SAVE_EXPRs. */
2752struct tlist_cache
2753{
2754 struct tlist_cache *next;
2755 struct tlist *cache_before_sp;
2756 struct tlist *cache_after_sp;
2757 tree expr;
481c6ce6 2758};
2759
4e91a871 2760/* Obstack to use when allocating tlist structures, and corresponding
2761 firstobj. */
2762static struct obstack tlist_obstack;
2763static char *tlist_firstobj = 0;
2764
2765/* Keep track of the identifiers we've warned about, so we can avoid duplicate
2766 warnings. */
2767static struct tlist *warned_ids;
2768/* SAVE_EXPRs need special treatment. We process them only once and then
2769 cache the results. */
2770static struct tlist_cache *save_expr_cache;
2771
1cae46be 2772static void add_tlist (struct tlist **, struct tlist *, tree, int);
2773static void merge_tlist (struct tlist **, struct tlist *, int);
2774static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2775static int warning_candidate_p (tree);
79973b57 2776static bool candidate_equal_p (const_tree, const_tree);
1cae46be 2777static void warn_for_collisions (struct tlist *);
2778static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2779static struct tlist *new_tlist (struct tlist *, tree, tree);
481c6ce6 2780
4e91a871 2781/* Create a new struct tlist and fill in its fields. */
2782static struct tlist *
1cae46be 2783new_tlist (struct tlist *next, tree t, tree writer)
4e91a871 2784{
2785 struct tlist *l;
9318f22c 2786 l = XOBNEW (&tlist_obstack, struct tlist);
4e91a871 2787 l->next = next;
2788 l->expr = t;
2789 l->writer = writer;
2790 return l;
2791}
2792
2793/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2794 is nonnull, we ignore any node we find which has a writer equal to it. */
2795
2796static void
1cae46be 2797add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
4e91a871 2798{
2799 while (add)
2800 {
2801 struct tlist *next = add->next;
84166705 2802 if (!copy)
4e91a871 2803 add->next = *to;
79973b57 2804 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
4e91a871 2805 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2806 add = next;
2807 }
2808}
2809
2810/* Merge the nodes of ADD into TO. This merging process is done so that for
2811 each variable that already exists in TO, no new node is added; however if
2812 there is a write access recorded in ADD, and an occurrence on TO is only
2813 a read access, then the occurrence in TO will be modified to record the
2814 write. */
481c6ce6 2815
2816static void
1cae46be 2817merge_tlist (struct tlist **to, struct tlist *add, int copy)
4e91a871 2818{
2819 struct tlist **end = to;
2820
2821 while (*end)
2822 end = &(*end)->next;
2823
2824 while (add)
2825 {
2826 int found = 0;
2827 struct tlist *tmp2;
2828 struct tlist *next = add->next;
2829
2830 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
79973b57 2831 if (candidate_equal_p (tmp2->expr, add->expr))
4e91a871 2832 {
2833 found = 1;
84166705 2834 if (!tmp2->writer)
4e91a871 2835 tmp2->writer = add->writer;
2836 }
84166705 2837 if (!found)
4e91a871 2838 {
312243bb 2839 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
4e91a871 2840 end = &(*end)->next;
2841 *end = 0;
2842 }
2843 add = next;
2844 }
2845}
2846
2847/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2848 references in list LIST conflict with it, excluding reads if ONLY writers
2849 is nonzero. */
2850
2851static void
1cae46be 2852warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2853 int only_writes)
4e91a871 2854{
2855 struct tlist *tmp;
2856
2857 /* Avoid duplicate warnings. */
2858 for (tmp = warned_ids; tmp; tmp = tmp->next)
79973b57 2859 if (candidate_equal_p (tmp->expr, written))
4e91a871 2860 return;
2861
2862 while (list)
2863 {
79973b57 2864 if (candidate_equal_p (list->expr, written)
2865 && !candidate_equal_p (list->writer, writer)
2866 && (!only_writes || list->writer))
4e91a871 2867 {
2868 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3df42822 2869 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
6513b50d 2870 OPT_Wsequence_point, "operation on %qE may be undefined",
2871 list->expr);
4e91a871 2872 }
2873 list = list->next;
2874 }
2875}
2876
2877/* Given a list LIST of references to variables, find whether any of these
2878 can cause conflicts due to missing sequence points. */
2879
2880static void
1cae46be 2881warn_for_collisions (struct tlist *list)
4e91a871 2882{
2883 struct tlist *tmp;
1cae46be 2884
4e91a871 2885 for (tmp = list; tmp; tmp = tmp->next)
2886 {
2887 if (tmp->writer)
2888 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2889 }
2890}
2891
734c98be 2892/* Return nonzero if X is a tree that can be verified by the sequence point
4e91a871 2893 warnings. */
2894static int
1cae46be 2895warning_candidate_p (tree x)
481c6ce6 2896{
6ef8d12f 2897 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2898 return 0;
2899
027fc6ef 2900 if (TREE_CODE (x) == BLOCK)
2901 return 0;
2902
6ef8d12f 2903 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
79973b57 2904 (lvalue_p) crash on TRY/CATCH. */
6ef8d12f 2905 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2906 return 0;
2907
2908 if (!lvalue_p (x))
2909 return 0;
2910
2911 /* No point to track non-const calls, they will never satisfy
2912 operand_equal_p. */
2913 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2914 return 0;
2915
2916 if (TREE_CODE (x) == STRING_CST)
2917 return 0;
2918
2919 return 1;
79973b57 2920}
2921
2922/* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2923static bool
2924candidate_equal_p (const_tree x, const_tree y)
2925{
2926 return (x == y) || (x && y && operand_equal_p (x, y, 0));
4e91a871 2927}
481c6ce6 2928
4e91a871 2929/* Walk the tree X, and record accesses to variables. If X is written by the
2930 parent tree, WRITER is the parent.
2931 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2932 expression or its only operand forces a sequence point, then everything up
2933 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2934 in PNO_SP.
2935 Once we return, we will have emitted warnings if any subexpression before
2936 such a sequence point could be undefined. On a higher level, however, the
2937 sequence point may not be relevant, and we'll merge the two lists.
2938
2939 Example: (b++, a) + b;
2940 The call that processes the COMPOUND_EXPR will store the increment of B
2941 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2942 processes the PLUS_EXPR will need to merge the two lists so that
2943 eventually, all accesses end up on the same list (and we'll warn about the
2944 unordered subexpressions b++ and b.
2945
2946 A note on merging. If we modify the former example so that our expression
2947 becomes
2948 (b++, b) + a
2949 care must be taken not simply to add all three expressions into the final
2950 PNO_SP list. The function merge_tlist takes care of that by merging the
2951 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2952 way, so that no more than one access to B is recorded. */
481c6ce6 2953
4e91a871 2954static void
1cae46be 2955verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2956 tree writer)
4e91a871 2957{
2958 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2959 enum tree_code code;
ce45a448 2960 enum tree_code_class cl;
481c6ce6 2961
e5b75768 2962 /* X may be NULL if it is the operand of an empty statement expression
2963 ({ }). */
2964 if (x == NULL)
2965 return;
2966
4e91a871 2967 restart:
2968 code = TREE_CODE (x);
e916c70c 2969 cl = TREE_CODE_CLASS (code);
481c6ce6 2970
4e91a871 2971 if (warning_candidate_p (x))
79973b57 2972 *pno_sp = new_tlist (*pno_sp, x, writer);
4e91a871 2973
2974 switch (code)
2975 {
67b28e3e 2976 case CONSTRUCTOR:
8e71dad2 2977 case SIZEOF_EXPR:
67b28e3e 2978 return;
2979
4e91a871 2980 case COMPOUND_EXPR:
2981 case TRUTH_ANDIF_EXPR:
2982 case TRUTH_ORIF_EXPR:
2983 tmp_before = tmp_nosp = tmp_list3 = 0;
2984 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2985 warn_for_collisions (tmp_nosp);
2986 merge_tlist (pbefore_sp, tmp_before, 0);
2987 merge_tlist (pbefore_sp, tmp_nosp, 0);
2988 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
2989 merge_tlist (pbefore_sp, tmp_list3, 0);
2990 return;
2991
2992 case COND_EXPR:
2993 tmp_before = tmp_list2 = 0;
2994 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2995 warn_for_collisions (tmp_list2);
2996 merge_tlist (pbefore_sp, tmp_before, 0);
312243bb 2997 merge_tlist (pbefore_sp, tmp_list2, 0);
4e91a871 2998
2999 tmp_list3 = tmp_nosp = 0;
3000 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3001 warn_for_collisions (tmp_nosp);
3002 merge_tlist (pbefore_sp, tmp_list3, 0);
3003
3004 tmp_list3 = tmp_list2 = 0;
3005 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3006 warn_for_collisions (tmp_list2);
3007 merge_tlist (pbefore_sp, tmp_list3, 0);
3008 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3009 two first, to avoid warning for (a ? b++ : b++). */
3010 merge_tlist (&tmp_nosp, tmp_list2, 0);
3011 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3012 return;
3013
481c6ce6 3014 case PREDECREMENT_EXPR:
3015 case PREINCREMENT_EXPR:
3016 case POSTDECREMENT_EXPR:
3017 case POSTINCREMENT_EXPR:
4e91a871 3018 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3019 return;
3020
3021 case MODIFY_EXPR:
3022 tmp_before = tmp_nosp = tmp_list3 = 0;
3023 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3024 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3025 /* Expressions inside the LHS are not ordered wrt. the sequence points
3026 in the RHS. Example:
3027 *a = (a++, 2)
3028 Despite the fact that the modification of "a" is in the before_sp
3029 list (tmp_before), it conflicts with the use of "a" in the LHS.
3030 We can handle this by adding the contents of tmp_list3
3031 to those of tmp_before, and redoing the collision warnings for that
3032 list. */
3033 add_tlist (&tmp_before, tmp_list3, x, 1);
3034 warn_for_collisions (tmp_before);
3035 /* Exclude the LHS itself here; we first have to merge it into the
3036 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3037 didn't exclude the LHS, we'd get it twice, once as a read and once
3038 as a write. */
3039 add_tlist (pno_sp, tmp_list3, x, 0);
3040 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3041
3042 merge_tlist (pbefore_sp, tmp_before, 0);
3043 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3044 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3045 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3046 return;
481c6ce6 3047
3048 case CALL_EXPR:
4e91a871 3049 /* We need to warn about conflicts among arguments and conflicts between
3050 args and the function address. Side effects of the function address,
3051 however, are not ordered by the sequence point of the call. */
c2f47e15 3052 {
3053 call_expr_arg_iterator iter;
3054 tree arg;
48e1416a 3055 tmp_before = tmp_nosp = 0;
c2f47e15 3056 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3057 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3058 {
3059 tmp_list2 = tmp_list3 = 0;
3060 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3061 merge_tlist (&tmp_list3, tmp_list2, 0);
3062 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3063 }
3064 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3065 warn_for_collisions (tmp_before);
3066 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3067 return;
3068 }
481c6ce6 3069
3070 case TREE_LIST:
3071 /* Scan all the list, e.g. indices of multi dimensional array. */
3072 while (x)
3073 {
4e91a871 3074 tmp_before = tmp_nosp = 0;
3075 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3076 merge_tlist (&tmp_nosp, tmp_before, 0);
3077 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
481c6ce6 3078 x = TREE_CHAIN (x);
3079 }
4e91a871 3080 return;
481c6ce6 3081
4e91a871 3082 case SAVE_EXPR:
3083 {
3084 struct tlist_cache *t;
3085 for (t = save_expr_cache; t; t = t->next)
79973b57 3086 if (candidate_equal_p (t->expr, x))
4e91a871 3087 break;
481c6ce6 3088
84166705 3089 if (!t)
481c6ce6 3090 {
9318f22c 3091 t = XOBNEW (&tlist_obstack, struct tlist_cache);
4e91a871 3092 t->next = save_expr_cache;
3093 t->expr = x;
3094 save_expr_cache = t;
3095
3096 tmp_before = tmp_nosp = 0;
3097 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3098 warn_for_collisions (tmp_nosp);
3099
3100 tmp_list3 = 0;
312243bb 3101 merge_tlist (&tmp_list3, tmp_nosp, 0);
4e91a871 3102 t->cache_before_sp = tmp_before;
3103 t->cache_after_sp = tmp_list3;
481c6ce6 3104 }
4e91a871 3105 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3106 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3107 return;
3108 }
481c6ce6 3109
012916cb 3110 case ADDR_EXPR:
3111 x = TREE_OPERAND (x, 0);
3112 if (DECL_P (x))
3113 return;
3114 writer = 0;
3115 goto restart;
3116
ce45a448 3117 default:
3118 /* For other expressions, simply recurse on their operands.
a0c938f0 3119 Manual tail recursion for unary expressions.
ce45a448 3120 Other non-expressions need not be processed. */
3121 if (cl == tcc_unary)
3122 {
ce45a448 3123 x = TREE_OPERAND (x, 0);
3124 writer = 0;
3125 goto restart;
3126 }
3127 else if (IS_EXPR_CODE_CLASS (cl))
3128 {
3129 int lp;
c2f47e15 3130 int max = TREE_OPERAND_LENGTH (x);
ce45a448 3131 for (lp = 0; lp < max; lp++)
3132 {
3133 tmp_before = tmp_nosp = 0;
3134 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3135 merge_tlist (&tmp_nosp, tmp_before, 0);
3136 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3137 }
3138 }
3139 return;
481c6ce6 3140 }
481c6ce6 3141}
3142
974e2c0c 3143/* Try to warn for undefined behavior in EXPR due to missing sequence
481c6ce6 3144 points. */
3145
4b987fac 3146DEBUG_FUNCTION void
1cae46be 3147verify_sequence_points (tree expr)
481c6ce6 3148{
4e91a871 3149 struct tlist *before_sp = 0, *after_sp = 0;
481c6ce6 3150
4e91a871 3151 warned_ids = 0;
3152 save_expr_cache = 0;
3153 if (tlist_firstobj == 0)
481c6ce6 3154 {
4e91a871 3155 gcc_obstack_init (&tlist_obstack);
4fd61bc6 3156 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
481c6ce6 3157 }
3158
4e91a871 3159 verify_tree (expr, &before_sp, &after_sp, 0);
3160 warn_for_collisions (after_sp);
3161 obstack_free (&tlist_obstack, tlist_firstobj);
481c6ce6 3162}
b0fc3e72 3163\f
3164/* Validate the expression after `case' and apply default promotions. */
3165
2ca392fd 3166static tree
2d2f6a15 3167check_case_value (location_t loc, tree value)
b0fc3e72 3168{
3169 if (value == NULL_TREE)
3170 return value;
3171
b96dc121 3172 if (TREE_CODE (value) == INTEGER_CST)
3173 /* Promote char or short to int. */
3174 value = perform_integral_promotions (value);
3175 else if (value != error_mark_node)
b0fc3e72 3176 {
2d2f6a15 3177 error_at (loc, "case label does not reduce to an integer constant");
b0fc3e72 3178 value = error_mark_node;
3179 }
b0fc3e72 3180
6433f1c2 3181 constant_expression_warning (value);
3182
b0fc3e72 3183 return value;
3184}
3185\f
2ca392fd 3186/* See if the case values LOW and HIGH are in the range of the original
5c9dae64 3187 type (i.e. before the default conversion to int) of the switch testing
2ca392fd 3188 expression.
3189 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
91275768 3190 the type before promoting it. CASE_LOW_P is a pointer to the lower
2ca392fd 3191 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3192 if the case is not a case range.
3193 The caller has to make sure that we are not called with NULL for
be23b16f 3194 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
3195 was a case value that doesn't fit into the range of the ORIG_TYPE.
442e3cb9 3196 Returns true if the case label is in range of ORIG_TYPE (saturated or
2ca392fd 3197 untouched) or false if the label is out of range. */
3198
3199static bool
f61a9bc2 3200check_case_bounds (location_t loc, tree type, tree orig_type,
be23b16f 3201 tree *case_low_p, tree *case_high_p,
3202 bool *outside_range_p)
2ca392fd 3203{
3204 tree min_value, max_value;
3205 tree case_low = *case_low_p;
3206 tree case_high = case_high_p ? *case_high_p : case_low;
3207
3208 /* If there was a problem with the original type, do nothing. */
3209 if (orig_type == error_mark_node)
3210 return true;
3211
3212 min_value = TYPE_MIN_VALUE (orig_type);
3213 max_value = TYPE_MAX_VALUE (orig_type);
3214
73f67931 3215 /* We'll really need integer constants here. */
3216 case_low = fold (case_low);
3217 case_high = fold (case_high);
3218
2ca392fd 3219 /* Case label is less than minimum for type. */
3220 if (tree_int_cst_compare (case_low, min_value) < 0
3221 && tree_int_cst_compare (case_high, min_value) < 0)
3222 {
f61a9bc2 3223 warning_at (loc, 0, "case label value is less than minimum value "
3224 "for type");
be23b16f 3225 *outside_range_p = true;
2ca392fd 3226 return false;
3227 }
b27ac6b5 3228
2ca392fd 3229 /* Case value is greater than maximum for type. */
3230 if (tree_int_cst_compare (case_low, max_value) > 0
3231 && tree_int_cst_compare (case_high, max_value) > 0)
3232 {
f61a9bc2 3233 warning_at (loc, 0, "case label value exceeds maximum value for type");
be23b16f 3234 *outside_range_p = true;
2ca392fd 3235 return false;
3236 }
3237
3238 /* Saturate lower case label value to minimum. */
3239 if (tree_int_cst_compare (case_high, min_value) >= 0
3240 && tree_int_cst_compare (case_low, min_value) < 0)
3241 {
f61a9bc2 3242 warning_at (loc, 0, "lower value in case label range"
3243 " less than minimum value for type");
be23b16f 3244 *outside_range_p = true;
2ca392fd 3245 case_low = min_value;
3246 }
b27ac6b5 3247
2ca392fd 3248 /* Saturate upper case label value to maximum. */
3249 if (tree_int_cst_compare (case_low, max_value) <= 0
3250 && tree_int_cst_compare (case_high, max_value) > 0)
3251 {
f61a9bc2 3252 warning_at (loc, 0, "upper value in case label range"
3253 " exceeds maximum value for type");
be23b16f 3254 *outside_range_p = true;
2ca392fd 3255 case_high = max_value;
3256 }
3257
3258 if (*case_low_p != case_low)
3259 *case_low_p = convert (type, case_low);
3260 if (case_high_p && *case_high_p != case_high)
3261 *case_high_p = convert (type, case_high);
3262
3263 return true;
3264}
3265\f
b0fc3e72 3266/* Return an integer type with BITS bits of precision,
3267 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3268
3269tree
1cae46be 3270c_common_type_for_size (unsigned int bits, int unsignedp)
b0fc3e72 3271{
9f75f026 3272 int i;
3273
46375237 3274 if (bits == TYPE_PRECISION (integer_type_node))
3275 return unsignedp ? unsigned_type_node : integer_type_node;
3276
bacde65a 3277 if (bits == TYPE_PRECISION (signed_char_type_node))
b0fc3e72 3278 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3279
bacde65a 3280 if (bits == TYPE_PRECISION (short_integer_type_node))
b0fc3e72 3281 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3282
bacde65a 3283 if (bits == TYPE_PRECISION (long_integer_type_node))
b0fc3e72 3284 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3285
bacde65a 3286 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b0fc3e72 3287 return (unsignedp ? long_long_unsigned_type_node
3288 : long_long_integer_type_node);
3289
9f75f026 3290 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3291 if (int_n_enabled_p[i]
3292 && bits == int_n_data[i].bitsize)
3293 return (unsignedp ? int_n_trees[i].unsigned_type
3294 : int_n_trees[i].signed_type);
6388cfe2 3295
f57fa2ea 3296 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3297 return (unsignedp ? widest_unsigned_literal_type_node
3298 : widest_integer_literal_type_node);
3299
bacde65a 3300 if (bits <= TYPE_PRECISION (intQI_type_node))
3301 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3302
3303 if (bits <= TYPE_PRECISION (intHI_type_node))
3304 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3305
3306 if (bits <= TYPE_PRECISION (intSI_type_node))
3307 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3308
3309 if (bits <= TYPE_PRECISION (intDI_type_node))
3310 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3311
b0fc3e72 3312 return 0;
3313}
3314
9421ebb9 3315/* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3316 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3317 and saturating if SATP is nonzero, otherwise not saturating. */
3318
3319tree
3320c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3321 int unsignedp, int satp)
3322{
3754d046 3323 machine_mode mode;
9421ebb9 3324 if (ibit == 0)
3325 mode = unsignedp ? UQQmode : QQmode;
3326 else
3327 mode = unsignedp ? UHAmode : HAmode;
3328
3329 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3330 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3331 break;
3332
3333 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3334 {
3335 sorry ("GCC cannot support operators with integer types and "
3336 "fixed-point types that have too many integral and "
3337 "fractional bits together");
3338 return 0;
3339 }
3340
3341 return c_common_type_for_mode (mode, satp);
3342}
3343
5b247e9f 3344/* Used for communication between c_common_type_for_mode and
3345 c_register_builtin_type. */
c1917557 3346tree registered_builtin_types;
5b247e9f 3347
b0fc3e72 3348/* Return a data type that has machine mode MODE.
3349 If the mode is an integer,
9421ebb9 3350 then UNSIGNEDP selects between signed and unsigned types.
3351 If the mode is a fixed-point mode,
3352 then UNSIGNEDP selects between saturating and nonsaturating types. */
b0fc3e72 3353
3354tree
3754d046 3355c_common_type_for_mode (machine_mode mode, int unsignedp)
b0fc3e72 3356{
5b247e9f 3357 tree t;
9f75f026 3358 int i;
5b247e9f 3359
46375237 3360 if (mode == TYPE_MODE (integer_type_node))
3361 return unsignedp ? unsigned_type_node : integer_type_node;
3362
b0fc3e72 3363 if (mode == TYPE_MODE (signed_char_type_node))
3364 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3365
3366 if (mode == TYPE_MODE (short_integer_type_node))
3367 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3368
b0fc3e72 3369 if (mode == TYPE_MODE (long_integer_type_node))
3370 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3371
3372 if (mode == TYPE_MODE (long_long_integer_type_node))
3373 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3374
9f75f026 3375 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3376 if (int_n_enabled_p[i]
3377 && mode == int_n_data[i].m)
3378 return (unsignedp ? int_n_trees[i].unsigned_type
3379 : int_n_trees[i].signed_type);
6388cfe2 3380
f57fa2ea 3381 if (mode == TYPE_MODE (widest_integer_literal_type_node))
44e9fa65 3382 return unsignedp ? widest_unsigned_literal_type_node
4ee9c684 3383 : widest_integer_literal_type_node;
f57fa2ea 3384
88ae7f04 3385 if (mode == QImode)
bacde65a 3386 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3387
88ae7f04 3388 if (mode == HImode)
bacde65a 3389 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3390
88ae7f04 3391 if (mode == SImode)
bacde65a 3392 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3393
88ae7f04 3394 if (mode == DImode)
bacde65a 3395 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
ab2c1de8 3396
cc1cc1c7 3397#if HOST_BITS_PER_WIDE_INT >= 64
6274009c 3398 if (mode == TYPE_MODE (intTI_type_node))
3399 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
cc1cc1c7 3400#endif
6274009c 3401
b0fc3e72 3402 if (mode == TYPE_MODE (float_type_node))
3403 return float_type_node;
3404
3405 if (mode == TYPE_MODE (double_type_node))
3406 return double_type_node;
3407
3408 if (mode == TYPE_MODE (long_double_type_node))
3409 return long_double_type_node;
3410
545c2bde 3411 if (mode == TYPE_MODE (void_type_node))
3412 return void_type_node;
b27ac6b5 3413
b0fc3e72 3414 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
61b9b73c 3415 return (unsignedp
3416 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3417 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3418
3419 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
61b9b73c 3420 return (unsignedp
3421 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3422 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3423
0dfc45b5 3424 if (COMPLEX_MODE_P (mode))
3425 {
3754d046 3426 machine_mode inner_mode;
0dfc45b5 3427 tree inner_type;
3428
3429 if (mode == TYPE_MODE (complex_float_type_node))
3430 return complex_float_type_node;
3431 if (mode == TYPE_MODE (complex_double_type_node))
3432 return complex_double_type_node;
3433 if (mode == TYPE_MODE (complex_long_double_type_node))
3434 return complex_long_double_type_node;
3435
3436 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3437 return complex_integer_type_node;
3438
3439 inner_mode = GET_MODE_INNER (mode);
3440 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3441 if (inner_type != NULL_TREE)
3442 return build_complex_type (inner_type);
3443 }
3444 else if (VECTOR_MODE_P (mode))
4917c376 3445 {
3754d046 3446 machine_mode inner_mode = GET_MODE_INNER (mode);
4917c376 3447 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3448 if (inner_type != NULL_TREE)
3449 return build_vector_type_for_mode (inner_type, mode);
88ae7f04 3450 }
e2ea7e3a 3451
c4503c0a 3452 if (mode == TYPE_MODE (dfloat32_type_node))
3453 return dfloat32_type_node;
3454 if (mode == TYPE_MODE (dfloat64_type_node))
3455 return dfloat64_type_node;
3456 if (mode == TYPE_MODE (dfloat128_type_node))
3457 return dfloat128_type_node;
3458
9421ebb9 3459 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3460 {
3461 if (mode == TYPE_MODE (short_fract_type_node))
3462 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3463 if (mode == TYPE_MODE (fract_type_node))
3464 return unsignedp ? sat_fract_type_node : fract_type_node;
3465 if (mode == TYPE_MODE (long_fract_type_node))
3466 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3467 if (mode == TYPE_MODE (long_long_fract_type_node))
3468 return unsignedp ? sat_long_long_fract_type_node
3469 : long_long_fract_type_node;
3470
3471 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3472 return unsignedp ? sat_unsigned_short_fract_type_node
3473 : unsigned_short_fract_type_node;
3474 if (mode == TYPE_MODE (unsigned_fract_type_node))
3475 return unsignedp ? sat_unsigned_fract_type_node
3476 : unsigned_fract_type_node;
3477 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3478 return unsignedp ? sat_unsigned_long_fract_type_node
3479 : unsigned_long_fract_type_node;
3480 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3481 return unsignedp ? sat_unsigned_long_long_fract_type_node
3482 : unsigned_long_long_fract_type_node;
3483
3484 if (mode == TYPE_MODE (short_accum_type_node))
3485 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3486 if (mode == TYPE_MODE (accum_type_node))
3487 return unsignedp ? sat_accum_type_node : accum_type_node;
3488 if (mode == TYPE_MODE (long_accum_type_node))
3489 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3490 if (mode == TYPE_MODE (long_long_accum_type_node))
3491 return unsignedp ? sat_long_long_accum_type_node
3492 : long_long_accum_type_node;
3493
3494 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3495 return unsignedp ? sat_unsigned_short_accum_type_node
3496 : unsigned_short_accum_type_node;
3497 if (mode == TYPE_MODE (unsigned_accum_type_node))
3498 return unsignedp ? sat_unsigned_accum_type_node
3499 : unsigned_accum_type_node;
3500 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3501 return unsignedp ? sat_unsigned_long_accum_type_node
3502 : unsigned_long_accum_type_node;
3503 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3504 return unsignedp ? sat_unsigned_long_long_accum_type_node
3505 : unsigned_long_long_accum_type_node;
3506
3507 if (mode == QQmode)
3508 return unsignedp ? sat_qq_type_node : qq_type_node;
3509 if (mode == HQmode)
3510 return unsignedp ? sat_hq_type_node : hq_type_node;
3511 if (mode == SQmode)
3512 return unsignedp ? sat_sq_type_node : sq_type_node;
3513 if (mode == DQmode)
3514 return unsignedp ? sat_dq_type_node : dq_type_node;
3515 if (mode == TQmode)
3516 return unsignedp ? sat_tq_type_node : tq_type_node;
3517
3518 if (mode == UQQmode)
3519 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3520 if (mode == UHQmode)
3521 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3522 if (mode == USQmode)
3523 return unsignedp ? sat_usq_type_node : usq_type_node;
3524 if (mode == UDQmode)
3525 return unsignedp ? sat_udq_type_node : udq_type_node;
3526 if (mode == UTQmode)
3527 return unsignedp ? sat_utq_type_node : utq_type_node;
3528
3529 if (mode == HAmode)
3530 return unsignedp ? sat_ha_type_node : ha_type_node;
3531 if (mode == SAmode)
3532 return unsignedp ? sat_sa_type_node : sa_type_node;
3533 if (mode == DAmode)
3534 return unsignedp ? sat_da_type_node : da_type_node;
3535 if (mode == TAmode)
3536 return unsignedp ? sat_ta_type_node : ta_type_node;
3537
3538 if (mode == UHAmode)
3539 return unsignedp ? sat_uha_type_node : uha_type_node;
3540 if (mode == USAmode)
3541 return unsignedp ? sat_usa_type_node : usa_type_node;
3542 if (mode == UDAmode)
3543 return unsignedp ? sat_uda_type_node : uda_type_node;
3544 if (mode == UTAmode)
3545 return unsignedp ? sat_uta_type_node : uta_type_node;
3546 }
3547
5b247e9f 3548 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
baec58e1 3549 if (TYPE_MODE (TREE_VALUE (t)) == mode
3550 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
5b247e9f 3551 return TREE_VALUE (t);
3552
b0fc3e72 3553 return 0;
3554}
20d39783 3555
11773141 3556tree
3557c_common_unsigned_type (tree type)
3558{
3559 return c_common_signed_or_unsigned_type (1, type);
3560}
3561
20d39783 3562/* Return a signed type the same as TYPE in other respects. */
3563
3564tree
1cae46be 3565c_common_signed_type (tree type)
20d39783 3566{
4070745f 3567 return c_common_signed_or_unsigned_type (0, type);
20d39783 3568}
3569
3570/* Return a type the same as TYPE except unsigned or
3571 signed according to UNSIGNEDP. */
3572
3573tree
1cae46be 3574c_common_signed_or_unsigned_type (int unsignedp, tree type)
20d39783 3575{
7a91101f 3576 tree type1;
9f75f026 3577 int i;
20d39783 3578
7a91101f 3579 /* This block of code emulates the behavior of the old
3580 c_common_unsigned_type. In particular, it returns
3581 long_unsigned_type_node if passed a long, even when a int would
3582 have the same size. This is necessary for warnings to work
3583 correctly in archs where sizeof(int) == sizeof(long) */
3584
3585 type1 = TYPE_MAIN_VARIANT (type);
3586 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3587 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3588 if (type1 == integer_type_node || type1 == unsigned_type_node)
3589 return unsignedp ? unsigned_type_node : integer_type_node;
3590 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3591 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3592 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3593 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3594 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3595 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
9f75f026 3596
3597 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3598 if (int_n_enabled_p[i]
3599 && (type1 == int_n_trees[i].unsigned_type
3600 || type1 == int_n_trees[i].signed_type))
3601 return (unsignedp ? int_n_trees[i].unsigned_type
3602 : int_n_trees[i].signed_type);
3603
7a91101f 3604 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3605 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3606#if HOST_BITS_PER_WIDE_INT >= 64
3607 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3608 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3609#endif
3610 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3611 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3612 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3613 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3614 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3615 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3616 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3617 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3618
9f92e1a2 3619#define C_COMMON_FIXED_TYPES(NAME) \
3620 if (type1 == short_ ## NAME ## _type_node \
3621 || type1 == unsigned_short_ ## NAME ## _type_node) \
3622 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3623 : short_ ## NAME ## _type_node; \
3624 if (type1 == NAME ## _type_node \
3625 || type1 == unsigned_ ## NAME ## _type_node) \
3626 return unsignedp ? unsigned_ ## NAME ## _type_node \
3627 : NAME ## _type_node; \
3628 if (type1 == long_ ## NAME ## _type_node \
3629 || type1 == unsigned_long_ ## NAME ## _type_node) \
3630 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3631 : long_ ## NAME ## _type_node; \
3632 if (type1 == long_long_ ## NAME ## _type_node \
3633 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3634 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3635 : long_long_ ## NAME ## _type_node;
3636
3637#define C_COMMON_FIXED_MODE_TYPES(NAME) \
3638 if (type1 == NAME ## _type_node \
3639 || type1 == u ## NAME ## _type_node) \
3640 return unsignedp ? u ## NAME ## _type_node \
3641 : NAME ## _type_node;
3642
3643#define C_COMMON_FIXED_TYPES_SAT(NAME) \
3644 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3645 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3646 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3647 : sat_ ## short_ ## NAME ## _type_node; \
3648 if (type1 == sat_ ## NAME ## _type_node \
3649 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3650 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3651 : sat_ ## NAME ## _type_node; \
3652 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3653 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3654 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3655 : sat_ ## long_ ## NAME ## _type_node; \
3656 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3657 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3658 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3659 : sat_ ## long_long_ ## NAME ## _type_node;
3660
3661#define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3662 if (type1 == sat_ ## NAME ## _type_node \
3663 || type1 == sat_ ## u ## NAME ## _type_node) \
3664 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3665 : sat_ ## NAME ## _type_node;
3666
3667 C_COMMON_FIXED_TYPES (fract);
3668 C_COMMON_FIXED_TYPES_SAT (fract);
3669 C_COMMON_FIXED_TYPES (accum);
3670 C_COMMON_FIXED_TYPES_SAT (accum);
3671
3672 C_COMMON_FIXED_MODE_TYPES (qq);
3673 C_COMMON_FIXED_MODE_TYPES (hq);
3674 C_COMMON_FIXED_MODE_TYPES (sq);
3675 C_COMMON_FIXED_MODE_TYPES (dq);
3676 C_COMMON_FIXED_MODE_TYPES (tq);
3677 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3678 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3679 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3680 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3681 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3682 C_COMMON_FIXED_MODE_TYPES (ha);
3683 C_COMMON_FIXED_MODE_TYPES (sa);
3684 C_COMMON_FIXED_MODE_TYPES (da);
3685 C_COMMON_FIXED_MODE_TYPES (ta);
3686 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3687 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3688 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3689 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
9421ebb9 3690
4f7f7efd 3691 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3692 the precision; they have precision set to match their range, but
3693 may use a wider mode to match an ABI. If we change modes, we may
3694 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3695 the precision as well, so as to yield correct results for
3696 bit-field types. C++ does not have these separate bit-field
3697 types, and producing a signed or unsigned variant of an
3698 ENUMERAL_TYPE may cause other problems as well. */
3699
ac265864 3700 if (!INTEGRAL_TYPE_P (type)
3701 || TYPE_UNSIGNED (type) == unsignedp)
3702 return type;
3703
4f7f7efd 3704#define TYPE_OK(node) \
3705 (TYPE_MODE (type) == TYPE_MODE (node) \
0c4abe5b 3706 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
4f7f7efd 3707 if (TYPE_OK (signed_char_type_node))
20d39783 3708 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4f7f7efd 3709 if (TYPE_OK (integer_type_node))
20d39783 3710 return unsignedp ? unsigned_type_node : integer_type_node;
4f7f7efd 3711 if (TYPE_OK (short_integer_type_node))
20d39783 3712 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4f7f7efd 3713 if (TYPE_OK (long_integer_type_node))
20d39783 3714 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4f7f7efd 3715 if (TYPE_OK (long_long_integer_type_node))
20d39783 3716 return (unsignedp ? long_long_unsigned_type_node
3717 : long_long_integer_type_node);
9f75f026 3718
3719 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3720 if (int_n_enabled_p[i]
3721 && TYPE_MODE (type) == int_n_data[i].m
3722 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3723 return (unsignedp ? int_n_trees[i].unsigned_type
3724 : int_n_trees[i].signed_type);
3725
4f7f7efd 3726 if (TYPE_OK (widest_integer_literal_type_node))
20d39783 3727 return (unsignedp ? widest_unsigned_literal_type_node
3728 : widest_integer_literal_type_node);
ef11801e 3729
3730#if HOST_BITS_PER_WIDE_INT >= 64
4f7f7efd 3731 if (TYPE_OK (intTI_type_node))
ef11801e 3732 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3733#endif
4f7f7efd 3734 if (TYPE_OK (intDI_type_node))
ef11801e 3735 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4f7f7efd 3736 if (TYPE_OK (intSI_type_node))
ef11801e 3737 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4f7f7efd 3738 if (TYPE_OK (intHI_type_node))
ef11801e 3739 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4f7f7efd 3740 if (TYPE_OK (intQI_type_node))
ef11801e 3741 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4f7f7efd 3742#undef TYPE_OK
ef11801e 3743
0c4abe5b 3744 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
20d39783 3745}
b268e47e 3746
c0e47fd4 3747/* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3748
3749tree
3750c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3751{
9f75f026 3752 int i;
3753
c0e47fd4 3754 /* Extended integer types of the same width as a standard type have
3755 lesser rank, so those of the same width as int promote to int or
3756 unsigned int and are valid for printf formats expecting int or
3757 unsigned int. To avoid such special cases, avoid creating
3758 extended integer types for bit-fields if a standard integer type
3759 is available. */
3760 if (width == TYPE_PRECISION (integer_type_node))
3761 return unsignedp ? unsigned_type_node : integer_type_node;
3762 if (width == TYPE_PRECISION (signed_char_type_node))
3763 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3764 if (width == TYPE_PRECISION (short_integer_type_node))
3765 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3766 if (width == TYPE_PRECISION (long_integer_type_node))
3767 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3768 if (width == TYPE_PRECISION (long_long_integer_type_node))
3769 return (unsignedp ? long_long_unsigned_type_node
3770 : long_long_integer_type_node);
9f75f026 3771 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3772 if (int_n_enabled_p[i]
3773 && width == int_n_data[i].bitsize)
3774 return (unsignedp ? int_n_trees[i].unsigned_type
3775 : int_n_trees[i].signed_type);
c0e47fd4 3776 return build_nonstandard_integer_type (width, unsignedp);
3777}
3778
b268e47e 3779/* The C version of the register_builtin_type langhook. */
3780
3781void
3782c_register_builtin_type (tree type, const char* name)
3783{
3784 tree decl;
3785
e60a6f7b 3786 decl = build_decl (UNKNOWN_LOCATION,
3787 TYPE_DECL, get_identifier (name), type);
b268e47e 3788 DECL_ARTIFICIAL (decl) = 1;
3789 if (!TYPE_NAME (type))
3790 TYPE_NAME (type) = decl;
3791 pushdecl (decl);
5b247e9f 3792
3793 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
b268e47e 3794}
a9b9d10c 3795\f
aff9e656 3796/* Print an error message for invalid operands to arith operation
8e70fb09 3797 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
c0bf500c 3798 RICHLOC is a rich location for the message, containing either
3799 three separate locations for each of the operator and operands
3800
3801 lhs op rhs
3802 ~~~ ^~ ~~~
3803
3804 (C FE), or one location ranging over all over them
3805
3806 lhs op rhs
3807 ~~~~^~~~~~
3808
3809 (C++ FE). */
b0fc3e72 3810
3811void
c0bf500c 3812binary_op_error (rich_location *richloc, enum tree_code code,
8e70fb09 3813 tree type0, tree type1)
b0fc3e72 3814{
19cb6b50 3815 const char *opname;
f03946e4 3816
b0fc3e72 3817 switch (code)
3818 {
b0fc3e72 3819 case PLUS_EXPR:
3820 opname = "+"; break;
3821 case MINUS_EXPR:
3822 opname = "-"; break;
3823 case MULT_EXPR:
3824 opname = "*"; break;
3825 case MAX_EXPR:
3826 opname = "max"; break;
3827 case MIN_EXPR:
3828 opname = "min"; break;
3829 case EQ_EXPR:
3830 opname = "=="; break;
3831 case NE_EXPR:
3832 opname = "!="; break;
3833 case LE_EXPR:
3834 opname = "<="; break;
3835 case GE_EXPR:
3836 opname = ">="; break;
3837 case LT_EXPR:
3838 opname = "<"; break;
3839 case GT_EXPR:
3840 opname = ">"; break;
3841 case LSHIFT_EXPR:
3842 opname = "<<"; break;
3843 case RSHIFT_EXPR:
3844 opname = ">>"; break;
3845 case TRUNC_MOD_EXPR:
66618a1e 3846 case FLOOR_MOD_EXPR:
b0fc3e72 3847 opname = "%"; break;
3848 case TRUNC_DIV_EXPR:
66618a1e 3849 case FLOOR_DIV_EXPR:
b0fc3e72 3850 opname = "/"; break;
3851 case BIT_AND_EXPR:
3852 opname = "&"; break;
3853 case BIT_IOR_EXPR:
3854 opname = "|"; break;
3855 case TRUTH_ANDIF_EXPR:
3856 opname = "&&"; break;
3857 case TRUTH_ORIF_EXPR:
3858 opname = "||"; break;
3859 case BIT_XOR_EXPR:
3860 opname = "^"; break;
31f820d2 3861 default:
315ba355 3862 gcc_unreachable ();
b0fc3e72 3863 }
c0bf500c 3864 error_at_rich_loc (richloc,
3865 "invalid operands to binary %s (have %qT and %qT)",
3866 opname, type0, type1);
b0fc3e72 3867}
3868\f
03fe1dc2 3869/* Given an expression as a tree, return its original type. Do this
3870 by stripping any conversion that preserves the sign and precision. */
3871static tree
3872expr_original_type (tree expr)
3873{
3874 STRIP_SIGN_NOPS (expr);
3875 return TREE_TYPE (expr);
3876}
3877
b0fc3e72 3878/* Subroutine of build_binary_op, used for comparison operations.
3879 See if the operands have both been converted from subword integer types
3880 and, if so, perhaps change them both back to their original type.
5b511807 3881 This function is also responsible for converting the two operands
3882 to the proper common type for comparison.
b0fc3e72 3883
3884 The arguments of this function are all pointers to local variables
3885 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3886 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3887
2623625f 3888 LOC is the location of the comparison.
3889
b0fc3e72 3890 If this function returns nonzero, it means that the comparison has
3891 a constant value. What this function returns is an expression for
3892 that value. */
3893
3894tree
2623625f 3895shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
3896 tree *restype_ptr, enum tree_code *rescode_ptr)
b0fc3e72 3897{
19cb6b50 3898 tree type;
b0fc3e72 3899 tree op0 = *op0_ptr;
3900 tree op1 = *op1_ptr;
3901 int unsignedp0, unsignedp1;
3902 int real1, real2;
3903 tree primop0, primop1;
3904 enum tree_code code = *rescode_ptr;
3905
3906 /* Throw away any conversions to wider types
3907 already present in the operands. */
3908
7f506bca 3909 primop0 = c_common_get_narrower (op0, &unsignedp0);
3910 primop1 = c_common_get_narrower (op1, &unsignedp1);
b0fc3e72 3911
119d06b2 3912 /* If primopN is first sign-extended from primopN's precision to opN's
3913 precision, then zero-extended from opN's precision to
3914 *restype_ptr precision, shortenings might be invalid. */
3915 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3916 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3917 && !unsignedp0
3918 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3919 primop0 = op0;
3920 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3921 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3922 && !unsignedp1
3923 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3924 primop1 = op1;
3925
b0fc3e72 3926 /* Handle the case that OP0 does not *contain* a conversion
3927 but it *requires* conversion to FINAL_TYPE. */
3928
3929 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
78a8ed03 3930 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
b0fc3e72 3931 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
78a8ed03 3932 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b0fc3e72 3933
3934 /* If one of the operands must be floated, we cannot optimize. */
3935 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3936 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
ab2c1de8 3937
b0fc3e72 3938 /* If first arg is constant, swap the args (changing operation
2bd278cc 3939 so value is preserved), for canonicalization. Don't do this if
3940 the second arg is 0. */
b0fc3e72 3941
2bd278cc 3942 if (TREE_CONSTANT (primop0)
9421ebb9 3943 && !integer_zerop (primop1) && !real_zerop (primop1)
3944 && !fixed_zerop (primop1))
b0fc3e72 3945 {
dfcf26a5 3946 std::swap (primop0, primop1);
3947 std::swap (op0, op1);
b0fc3e72 3948 *op0_ptr = op0;
3949 *op1_ptr = op1;
dfcf26a5 3950 std::swap (unsignedp0, unsignedp1);
3951 std::swap (real1, real2);
b0fc3e72 3952
3953 switch (code)
3954 {
3955 case LT_EXPR:
3956 code = GT_EXPR;
3957 break;
3958 case GT_EXPR:
3959 code = LT_EXPR;
3960 break;
3961 case LE_EXPR:
3962 code = GE_EXPR;
3963 break;
3964 case GE_EXPR:
3965 code = LE_EXPR;
3966 break;
31f820d2 3967 default:
3968 break;
b0fc3e72 3969 }
3970 *rescode_ptr = code;
3971 }
3972
3973 /* If comparing an integer against a constant more bits wide,
3974 maybe we can deduce a value of 1 or 0 independent of the data.
3975 Or else truncate the constant now
3976 rather than extend the variable at run time.
3977
3978 This is only interesting if the constant is the wider arg.
3979 Also, it is not safe if the constant is unsigned and the
3980 variable arg is signed, since in this case the variable
3981 would be sign-extended and then regarded as unsigned.
3982 Our technique fails in this case because the lowest/highest
3983 possible unsigned results don't follow naturally from the
3984 lowest/highest possible values of the variable operand.
3985 For just EQ_EXPR and NE_EXPR there is another technique that
3986 could be used: see if the constant can be faithfully represented
3987 in the other operand's type, by truncating it and reextending it
3988 and see if that preserves the constant's value. */
3989
3990 if (!real1 && !real2
9421ebb9 3991 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
b0fc3e72 3992 && TREE_CODE (primop1) == INTEGER_CST
3993 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3994 {
3995 int min_gt, max_gt, min_lt, max_lt;
3996 tree maxval, minval;
3997 /* 1 if comparison is nominally unsigned. */
78a8ed03 3998 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
b0fc3e72 3999 tree val;
4000
4070745f 4001 type = c_common_signed_or_unsigned_type (unsignedp0,
4002 TREE_TYPE (primop0));
cda09c61 4003
b0fc3e72 4004 maxval = TYPE_MAX_VALUE (type);
4005 minval = TYPE_MIN_VALUE (type);
4006
4007 if (unsignedp && !unsignedp0)
4070745f 4008 *restype_ptr = c_common_signed_type (*restype_ptr);
b0fc3e72 4009
4010 if (TREE_TYPE (primop1) != *restype_ptr)
18dbec6f 4011 {
9a5e8086 4012 /* Convert primop1 to target type, but do not introduce
4013 additional overflow. We know primop1 is an int_cst. */
e913b5cd 4014 primop1 = force_fit_type (*restype_ptr,
ee31dd3d 4015 wi::to_wide
4016 (primop1,
4017 TYPE_PRECISION (*restype_ptr)),
e913b5cd 4018 0, TREE_OVERFLOW (primop1));
18dbec6f 4019 }
b0fc3e72 4020 if (type != *restype_ptr)
4021 {
4022 minval = convert (*restype_ptr, minval);
4023 maxval = convert (*restype_ptr, maxval);
4024 }
4025
d99d10ca 4026 min_gt = tree_int_cst_lt (primop1, minval);
4027 max_gt = tree_int_cst_lt (primop1, maxval);
4028 min_lt = tree_int_cst_lt (minval, primop1);
4029 max_lt = tree_int_cst_lt (maxval, primop1);
b0fc3e72 4030
4031 val = 0;
4032 /* This used to be a switch, but Genix compiler can't handle that. */
4033 if (code == NE_EXPR)
4034 {
4035 if (max_lt || min_gt)
3c2239cf 4036 val = truthvalue_true_node;
b0fc3e72 4037 }
4038 else if (code == EQ_EXPR)
4039 {
4040 if (max_lt || min_gt)
3c2239cf 4041 val = truthvalue_false_node;
b0fc3e72 4042 }
4043 else if (code == LT_EXPR)
4044 {
4045 if (max_lt)
3c2239cf 4046 val = truthvalue_true_node;
b0fc3e72 4047 if (!min_lt)
3c2239cf 4048 val = truthvalue_false_node;
b0fc3e72 4049 }
4050 else if (code == GT_EXPR)
4051 {
4052 if (min_gt)
3c2239cf 4053 val = truthvalue_true_node;
b0fc3e72 4054 if (!max_gt)
3c2239cf 4055 val = truthvalue_false_node;
b0fc3e72 4056 }
4057 else if (code == LE_EXPR)
4058 {
4059 if (!max_gt)
3c2239cf 4060 val = truthvalue_true_node;
b0fc3e72 4061 if (min_gt)
3c2239cf 4062 val = truthvalue_false_node;
b0fc3e72 4063 }
4064 else if (code == GE_EXPR)
4065 {
4066 if (!min_lt)
3c2239cf 4067 val = truthvalue_true_node;
b0fc3e72 4068 if (max_lt)
3c2239cf 4069 val = truthvalue_false_node;
b0fc3e72 4070 }
4071
4072 /* If primop0 was sign-extended and unsigned comparison specd,
4073 we did a signed comparison above using the signed type bounds.
4074 But the comparison we output must be unsigned.
4075
4076 Also, for inequalities, VAL is no good; but if the signed
4077 comparison had *any* fixed result, it follows that the
4078 unsigned comparison just tests the sign in reverse
4079 (positive values are LE, negative ones GE).
4080 So we can generate an unsigned comparison
4081 against an extreme value of the signed type. */
4082
4083 if (unsignedp && !unsignedp0)
4084 {
4085 if (val != 0)
4086 switch (code)
4087 {
4088 case LT_EXPR:
4089 case GE_EXPR:
4090 primop1 = TYPE_MIN_VALUE (type);
4091 val = 0;
4092 break;
4093
4094 case LE_EXPR:
4095 case GT_EXPR:
4096 primop1 = TYPE_MAX_VALUE (type);
4097 val = 0;
4098 break;
31f820d2 4099
4100 default:
4101 break;
b0fc3e72 4102 }
11773141 4103 type = c_common_unsigned_type (type);
b0fc3e72 4104 }
4105
2d3d3af7 4106 if (TREE_CODE (primop0) != INTEGER_CST
01f11119 4107 /* Don't warn if it's from a (non-system) macro. */
4108 && !(from_macro_expansion_at
4109 (expansion_point_location_if_in_system_header
4110 (EXPR_LOCATION (primop0)))))
b0fc3e72 4111 {
3c2239cf 4112 if (val == truthvalue_false_node)
03fe1dc2 4113 warning_at (loc, OPT_Wtype_limits,
4114 "comparison is always false due to limited range of data type");
3c2239cf 4115 if (val == truthvalue_true_node)
03fe1dc2 4116 warning_at (loc, OPT_Wtype_limits,
4117 "comparison is always true due to limited range of data type");
b0fc3e72 4118 }
4119
4120 if (val != 0)
4121 {
4122 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4123 if (TREE_SIDE_EFFECTS (primop0))
14ae0310 4124 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
b0fc3e72 4125 return val;
4126 }
4127
4128 /* Value is not predetermined, but do the comparison
4129 in the type of the operand that is not constant.
4130 TYPE is already properly set. */
4131 }
c4503c0a 4132
4133 /* If either arg is decimal float and the other is float, find the
4134 proper common type to use for comparison. */
7fd22aae 4135 else if (real1 && real2
4136 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4137 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4138 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4139
4140 /* If either arg is decimal float and the other is float, fail. */
c4503c0a 4141 else if (real1 && real2
4142 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4143 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
7fd22aae 4144 return 0;
c4503c0a 4145
b0fc3e72 4146 else if (real1 && real2
2203bd5c 4147 && (TYPE_PRECISION (TREE_TYPE (primop0))
4148 == TYPE_PRECISION (TREE_TYPE (primop1))))
b0fc3e72 4149 type = TREE_TYPE (primop0);
4150
4151 /* If args' natural types are both narrower than nominal type
4152 and both extend in the same manner, compare them
4153 in the type of the wider arg.
4154 Otherwise must actually extend both to the nominal
4155 common type lest different ways of extending
4156 alter the result.
4157 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4158
4159 else if (unsignedp0 == unsignedp1 && real1 == real2
4160 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4161 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4162 {
4163 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4070745f 4164 type = c_common_signed_or_unsigned_type (unsignedp0
78a8ed03 4165 || TYPE_UNSIGNED (*restype_ptr),
4070745f 4166 type);
b0fc3e72 4167 /* Make sure shorter operand is extended the right way
4168 to match the longer operand. */
4070745f 4169 primop0
4170 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4171 TREE_TYPE (primop0)),
4172 primop0);
4173 primop1
4174 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4175 TREE_TYPE (primop1)),
4176 primop1);
b0fc3e72 4177 }
4178 else
4179 {
4180 /* Here we must do the comparison on the nominal type
4181 using the args exactly as we received them. */
4182 type = *restype_ptr;
4183 primop0 = op0;
4184 primop1 = op1;
4185
4186 if (!real1 && !real2 && integer_zerop (primop1)
78a8ed03 4187 && TYPE_UNSIGNED (*restype_ptr))
b0fc3e72 4188 {
4189 tree value = 0;
03fe1dc2 4190 /* All unsigned values are >= 0, so we warn. However,
4191 if OP0 is a constant that is >= 0, the signedness of
4192 the comparison isn't an issue, so suppress the
4193 warning. */
4194 bool warn =
3df42822 4195 warn_type_limits && !in_system_header_at (loc)
03fe1dc2 4196 && !(TREE_CODE (primop0) == INTEGER_CST
4197 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4198 primop0)))
4199 /* Do not warn for enumeration types. */
4200 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4201
b0fc3e72 4202 switch (code)
4203 {
4204 case GE_EXPR:
03fe1dc2 4205 if (warn)
4206 warning_at (loc, OPT_Wtype_limits,
4207 "comparison of unsigned expression >= 0 is always true");
3c2239cf 4208 value = truthvalue_true_node;
b0fc3e72 4209 break;
4210
4211 case LT_EXPR:
03fe1dc2 4212 if (warn)
4213 warning_at (loc, OPT_Wtype_limits,
4214 "comparison of unsigned expression < 0 is always false");
3c2239cf 4215 value = truthvalue_false_node;
31f820d2 4216 break;
4217
4218 default:
4219 break;
b0fc3e72 4220 }
4221
4222 if (value != 0)
4223 {
4224 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4225 if (TREE_SIDE_EFFECTS (primop0))
14ae0310 4226 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4227 primop0, value);
b0fc3e72 4228 return value;
4229 }
4230 }
4231 }
4232
4233 *op0_ptr = convert (type, primop0);
4234 *op1_ptr = convert (type, primop1);
4235
3c2239cf 4236 *restype_ptr = truthvalue_type_node;
b0fc3e72 4237
4238 return 0;
4239}
4240\f
1c26100f 4241/* Return a tree for the sum or difference (RESULTCODE says which)
4242 of pointer PTROP and integer INTOP. */
4243
4244tree
389dd41b 4245pointer_int_sum (location_t loc, enum tree_code resultcode,
42f9a786 4246 tree ptrop, tree intop, bool complain)
1c26100f 4247{
add6ee5e 4248 tree size_exp, ret;
1c26100f 4249
1c26100f 4250 /* The result is a pointer of the same type that is being added. */
1c26100f 4251 tree result_type = TREE_TYPE (ptrop);
4252
4253 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4254 {
42f9a786 4255 if (complain && warn_pointer_arith)
4256 pedwarn (loc, OPT_Wpointer_arith,
4257 "pointer of type %<void *%> used in arithmetic");
4258 else if (!complain)
4259 return error_mark_node;
1c26100f 4260 size_exp = integer_one_node;
4261 }
4262 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4263 {
42f9a786 4264 if (complain && warn_pointer_arith)
4265 pedwarn (loc, OPT_Wpointer_arith,
4266 "pointer to a function used in arithmetic");
4267 else if (!complain)
4268 return error_mark_node;
1c26100f 4269 size_exp = integer_one_node;
4270 }
1c26100f 4271 else
22a3f7bd 4272 size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type));
1c26100f 4273
add6ee5e 4274 /* We are manipulating pointer values, so we don't need to warn
4275 about relying on undefined signed overflow. We disable the
4276 warning here because we use integer types so fold won't know that
4277 they are really pointers. */
4278 fold_defer_overflow_warnings ();
4279
1c26100f 4280 /* If what we are about to multiply by the size of the elements
4281 contains a constant term, apply distributive law
4282 and multiply that constant term separately.
4283 This helps produce common subexpressions. */
1c26100f 4284 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
84166705 4285 && !TREE_CONSTANT (intop)
1c26100f 4286 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4287 && TREE_CONSTANT (size_exp)
4288 /* If the constant comes from pointer subtraction,
4289 skip this optimization--it would cause an error. */
4290 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4291 /* If the constant is unsigned, and smaller than the pointer size,
4292 then we must skip this optimization. This is because it could cause
4293 an overflow error if the constant is negative but INTOP is not. */
84166705 4294 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
1c26100f 4295 || (TYPE_PRECISION (TREE_TYPE (intop))
4296 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4297 {
4298 enum tree_code subcode = resultcode;
4299 tree int_type = TREE_TYPE (intop);
4300 if (TREE_CODE (intop) == MINUS_EXPR)
4301 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4302 /* Convert both subexpression types to the type of intop,
4303 because weird cases involving pointer arithmetic
4304 can result in a sum or difference with different type args. */
8e70fb09 4305 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4306 subcode, ptrop,
1c26100f 4307 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4308 intop = convert (int_type, TREE_OPERAND (intop, 0));
4309 }
4310
4311 /* Convert the integer argument to a type the same size as sizetype
4312 so the multiply won't overflow spuriously. */
1c26100f 4313 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
78a8ed03 4314 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
1cae46be 4315 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
78a8ed03 4316 TYPE_UNSIGNED (sizetype)), intop);
1c26100f 4317
4318 /* Replace the integer argument with a suitable product by the object size.
c6feb9f1 4319 Do this multiplication as signed, then convert to the appropriate type
8032877c 4320 for the pointer operation and disregard an overflow that occurred only
c6feb9f1 4321 because of the sign-extension change in the latter conversion. */
4322 {
f576a2e4 4323 tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
4324 convert (TREE_TYPE (intop), size_exp));
c6feb9f1 4325 intop = convert (sizetype, t);
4326 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
e913b5cd 4327 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
c6feb9f1 4328 }
0de36bdb 4329
499e523f 4330 /* Create the sum or difference. */
0de36bdb 4331 if (resultcode == MINUS_EXPR)
389dd41b 4332 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
1c26100f 4333
2cc66f2a 4334 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
add6ee5e 4335
4336 fold_undefer_and_ignore_overflow_warnings ();
4337
4338 return ret;
1c26100f 4339}
4340\f
f59e3889 4341/* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4342 and if NON_CONST is known not to be permitted in an evaluated part
4343 of a constant expression. */
4344
4345tree
4346c_wrap_maybe_const (tree expr, bool non_const)
4347{
4348 bool nowarning = TREE_NO_WARNING (expr);
4349 location_t loc = EXPR_LOCATION (expr);
4350
4351 /* This should never be called for C++. */
4352 if (c_dialect_cxx ())
4353 gcc_unreachable ();
4354
4355 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4356 STRIP_TYPE_NOPS (expr);
4357 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4358 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4359 if (nowarning)
4360 TREE_NO_WARNING (expr) = 1;
4361 protected_set_expr_location (expr, loc);
4362
4363 return expr;
4364}
4365
a75b1c71 4366/* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4367 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4368 around the SAVE_EXPR if needed so that c_fully_fold does not need
4369 to look inside SAVE_EXPRs. */
4370
4371tree
4372c_save_expr (tree expr)
4373{
4374 bool maybe_const = true;
4375 if (c_dialect_cxx ())
4376 return save_expr (expr);
4377 expr = c_fully_fold (expr, false, &maybe_const);
4378 expr = save_expr (expr);
4379 if (!maybe_const)
f59e3889 4380 expr = c_wrap_maybe_const (expr, true);
a75b1c71 4381 return expr;
4382}
4383
6b68e71a 4384/* Return whether EXPR is a declaration whose address can never be
4385 NULL. */
4386
4387bool
9f627b1a 4388decl_with_nonnull_addr_p (const_tree expr)
6b68e71a 4389{
4390 return (DECL_P (expr)
4391 && (TREE_CODE (expr) == PARM_DECL
4392 || TREE_CODE (expr) == LABEL_DECL
4393 || !DECL_WEAK (expr)));
4394}
4395
b0fc3e72 4396/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
45a78cc0 4397 or for an `if' or `while' statement or ?..: exp. It should already
4398 have been validated to be of suitable type; otherwise, a bad
4399 diagnostic may result.
b0fc3e72 4400
8e70fb09 4401 The EXPR is located at LOCATION.
4402
b0fc3e72 4403 This preparation consists of taking the ordinary
4404 representation of an expression expr and producing a valid tree
4405 boolean expression describing whether expr is nonzero. We could
3c2239cf 4406 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
b0fc3e72 4407 but we optimize comparisons, &&, ||, and !.
4408
3c2239cf 4409 The resulting type should always be `truthvalue_type_node'. */
b0fc3e72 4410
4411tree
8e70fb09 4412c_common_truthvalue_conversion (location_t location, tree expr)
b0fc3e72 4413{
b0fc3e72 4414 switch (TREE_CODE (expr))
4415 {
318a728f 4416 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
e1232ce2 4417 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4418 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4419 case ORDERED_EXPR: case UNORDERED_EXPR:
ce04dcdc 4420 if (TREE_TYPE (expr) == truthvalue_type_node)
4421 return expr;
e60a6f7b 4422 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
ce04dcdc 4423 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
e60a6f7b 4424 goto ret;
ce04dcdc 4425
b0fc3e72 4426 case TRUTH_ANDIF_EXPR:
4427 case TRUTH_ORIF_EXPR:
4428 case TRUTH_AND_EXPR:
4429 case TRUTH_OR_EXPR:
31f6e93c 4430 case TRUTH_XOR_EXPR:
ce04dcdc 4431 if (TREE_TYPE (expr) == truthvalue_type_node)
4432 return expr;
e60a6f7b 4433 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
48e1416a 4434 c_common_truthvalue_conversion (location,
e60a6f7b 4435 TREE_OPERAND (expr, 0)),
4436 c_common_truthvalue_conversion (location,
4437 TREE_OPERAND (expr, 1)));
4438 goto ret;
3e851b85 4439
f6e28f72 4440 case TRUTH_NOT_EXPR:
ce04dcdc 4441 if (TREE_TYPE (expr) == truthvalue_type_node)
4442 return expr;
e60a6f7b 4443 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4444 c_common_truthvalue_conversion (location,
4445 TREE_OPERAND (expr, 0)));
4446 goto ret;
f6e28f72 4447
b0fc3e72 4448 case ERROR_MARK:
4449 return expr;
ab2c1de8 4450
b0fc3e72 4451 case INTEGER_CST:
eddad94a 4452 return integer_zerop (expr) ? truthvalue_false_node
4453 : truthvalue_true_node;
b0fc3e72 4454
4455 case REAL_CST:
5000e21c 4456 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4457 ? truthvalue_true_node
4458 : truthvalue_false_node;
b0fc3e72 4459
9421ebb9 4460 case FIXED_CST:
4461 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4462 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4463 ? truthvalue_true_node
4464 : truthvalue_false_node;
4465
ce04dcdc 4466 case FUNCTION_DECL:
b6889cb0 4467 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
ce04dcdc 4468 /* Fall through. */
4469
b0fc3e72 4470 case ADDR_EXPR:
ce871053 4471 {
ee7d4d6a 4472 tree inner = TREE_OPERAND (expr, 0);
6b68e71a 4473 if (decl_with_nonnull_addr_p (inner))
ce871053 4474 {
6b68e71a 4475 /* Common Ada/Pascal programmer's mistake. */
8e70fb09 4476 warning_at (location,
4477 OPT_Waddress,
4478 "the address of %qD will always evaluate as %<true%>",
4479 inner);
ce871053 4480 return truthvalue_true_node;
4481 }
d473d901 4482 break;
ce871053 4483 }
b0fc3e72 4484
2203bd5c 4485 case COMPLEX_EXPR:
e60a6f7b 4486 expr = build_binary_op (EXPR_LOCATION (expr),
8e70fb09 4487 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
95809de4 4488 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
8e70fb09 4489 c_common_truthvalue_conversion (location,
4490 TREE_OPERAND (expr, 0)),
4491 c_common_truthvalue_conversion (location,
4492 TREE_OPERAND (expr, 1)),
2203bd5c 4493 0);
e60a6f7b 4494 goto ret;
2203bd5c 4495
b0fc3e72 4496 case NEGATE_EXPR:
4497 case ABS_EXPR:
4498 case FLOAT_EXPR:
c6418a4e 4499 case EXCESS_PRECISION_EXPR:
d10cfa8d 4500 /* These don't change whether an object is nonzero or zero. */
8e70fb09 4501 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
b0fc3e72 4502
4503 case LROTATE_EXPR:
4504 case RROTATE_EXPR:
d10cfa8d 4505 /* These don't change whether an object is zero or nonzero, but
b0fc3e72 4506 we can't ignore them if their second arg has side-effects. */
4507 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
e60a6f7b 4508 {
4509 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4510 TREE_OPERAND (expr, 1),
48e1416a 4511 c_common_truthvalue_conversion
e60a6f7b 4512 (location, TREE_OPERAND (expr, 0)));
4513 goto ret;
4514 }
b0fc3e72 4515 else
8e70fb09 4516 return c_common_truthvalue_conversion (location,
4517 TREE_OPERAND (expr, 0));
73be5127 4518
b0fc3e72 4519 case COND_EXPR:
4520 /* Distribute the conversion into the arms of a COND_EXPR. */
a75b1c71 4521 if (c_dialect_cxx ())
e60a6f7b 4522 {
d0389adc 4523 tree op1 = TREE_OPERAND (expr, 1);
4524 tree op2 = TREE_OPERAND (expr, 2);
4525 /* In C++ one of the arms might have void type if it is throw. */
4526 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4527 op1 = c_common_truthvalue_conversion (location, op1);
4528 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4529 op2 = c_common_truthvalue_conversion (location, op2);
389dd41b 4530 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
d0389adc 4531 TREE_OPERAND (expr, 0), op1, op2);
e60a6f7b 4532 goto ret;
4533 }
a75b1c71 4534 else
e60a6f7b 4535 {
4536 /* Folding will happen later for C. */
4537 expr = build3 (COND_EXPR, truthvalue_type_node,
4538 TREE_OPERAND (expr, 0),
4539 c_common_truthvalue_conversion (location,
4540 TREE_OPERAND (expr, 1)),
4541 c_common_truthvalue_conversion (location,
4542 TREE_OPERAND (expr, 2)));
4543 goto ret;
4544 }
b0fc3e72 4545
72dd6141 4546 CASE_CONVERT:
f8913d47 4547 {
4548 tree totype = TREE_TYPE (expr);
4549 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4550
db3d1ffc 4551 if (POINTER_TYPE_P (totype)
4552 && TREE_CODE (fromtype) == REFERENCE_TYPE)
4553 {
4554 tree inner = expr;
4555 STRIP_NOPS (inner);
4556
4557 if (DECL_P (inner))
4558 warning_at (location,
4559 OPT_Waddress,
4560 "the compiler can assume that the address of "
4561 "%qD will always evaluate to %<true%>",
4562 inner);
4563 }
4564
f8913d47 4565 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4566 since that affects how `default_conversion' will behave. */
4567 if (TREE_CODE (totype) == REFERENCE_TYPE
4568 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4569 break;
4570 /* Don't strip a conversion from C++0x scoped enum, since they
4571 don't implicitly convert to other types. */
4572 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4573 && ENUM_IS_SCOPED (fromtype))
4574 break;
4575 /* If this isn't narrowing the argument, we can ignore it. */
4576 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4577 return c_common_truthvalue_conversion (location,
4578 TREE_OPERAND (expr, 0));
4579 }
b0fc3e72 4580 break;
4581
16837b18 4582 case MODIFY_EXPR:
60a0513e 4583 if (!TREE_NO_WARNING (expr)
4584 && warn_parentheses)
4585 {
4586 warning (OPT_Wparentheses,
4587 "suggest parentheses around assignment used as truth value");
4588 TREE_NO_WARNING (expr) = 1;
4589 }
16837b18 4590 break;
73be5127 4591
31f820d2 4592 default:
4593 break;
b0fc3e72 4594 }
4595
2ba726d2 4596 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
a0748b7d 4597 {
93be21c0 4598 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
e60a6f7b 4599 expr = (build_binary_op
8e70fb09 4600 (EXPR_LOCATION (expr),
4601 (TREE_SIDE_EFFECTS (expr)
a0748b7d 4602 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
b6889cb0 4603 c_common_truthvalue_conversion
4604 (location,
4605 build_unary_op (location, REALPART_EXPR, t, 0)),
4606 c_common_truthvalue_conversion
4607 (location,
4608 build_unary_op (location, IMAGPART_EXPR, t, 0)),
a0748b7d 4609 0));
e60a6f7b 4610 goto ret;
a0748b7d 4611 }
2ba726d2 4612
9421ebb9 4613 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4614 {
4615 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4616 FCONST0 (TYPE_MODE
4617 (TREE_TYPE (expr))));
43158006 4618 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
9421ebb9 4619 }
e60a6f7b 4620 else
4621 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
9421ebb9 4622
e60a6f7b 4623 ret:
4624 protected_set_expr_location (expr, location);
4625 return expr;
b0fc3e72 4626}
4627\f
3237155d 4628static void def_builtin_1 (enum built_in_function fncode,
4629 const char *name,
4630 enum built_in_class fnclass,
4631 tree fntype, tree libtype,
4632 bool both_p, bool fallback_p, bool nonansi_p,
4633 tree fnattrs, bool implicit_p);
0d4238dc 4634
a5b1863e 4635
4636/* Apply the TYPE_QUALS to the new DECL. */
4637
4638void
1cae46be 4639c_apply_type_quals_to_decl (int type_quals, tree decl)
a5b1863e 4640{
adfb367f 4641 tree type = TREE_TYPE (decl);
b27ac6b5 4642
e4eabbe4 4643 if (type == error_mark_node)
4644 return;
adfb367f 4645
98a33d9f 4646 if ((type_quals & TYPE_QUAL_CONST)
4647 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4648 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4649 constructor can produce constant init, so rely on cp_finish_decl to
4650 clear TREE_READONLY if the variable has non-constant init. */
a5b1863e 4651 TREE_READONLY (decl) = 1;
4652 if (type_quals & TYPE_QUAL_VOLATILE)
4653 {
4654 TREE_SIDE_EFFECTS (decl) = 1;
4655 TREE_THIS_VOLATILE (decl) = 1;
4656 }
d91a20bc 4657 if (type_quals & TYPE_QUAL_RESTRICT)
a5b1863e 4658 {
adfb367f 4659 while (type && TREE_CODE (type) == ARRAY_TYPE)
4660 /* Allow 'restrict' on arrays of pointers.
4661 FIXME currently we just ignore it. */
4662 type = TREE_TYPE (type);
4663 if (!type
4664 || !POINTER_TYPE_P (type)
4665 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
b0b1af64 4666 error ("invalid use of %<restrict%>");
a5b1863e 4667 }
4668}
4669
b594087e 4670struct c_type_hasher : ggc_ptr_hash<tree_node>
2ef51f0e 4671{
4672 static hashval_t hash (tree);
4673 static bool equal (tree, tree);
4674};
4675
4ee9c684 4676/* Hash function for the problem of multiple type definitions in
4677 different files. This must hash all types that will compare
4678 equal via comptypes to the same value. In practice it hashes
0bed3869 4679 on some of the simple stuff and leaves the details to comptypes. */
4ee9c684 4680
2ef51f0e 4681hashval_t
4682c_type_hasher::hash (tree t)
4ee9c684 4683{
ecf2703d 4684 int n_elements;
4ee9c684 4685 int shift, size;
4ee9c684 4686 tree t2;
4687 switch (TREE_CODE (t))
4688 {
fbf0afd1 4689 /* For pointers, hash on pointee type plus some swizzling. */
2363ef00 4690 case POINTER_TYPE:
2ef51f0e 4691 return hash (TREE_TYPE (t)) ^ 0x3003003;
2363ef00 4692 /* Hash on number of elements and total size. */
4693 case ENUMERAL_TYPE:
4694 shift = 3;
4695 t2 = TYPE_VALUES (t);
4696 break;
4697 case RECORD_TYPE:
4698 shift = 0;
4699 t2 = TYPE_FIELDS (t);
4700 break;
4701 case QUAL_UNION_TYPE:
4702 shift = 1;
4703 t2 = TYPE_FIELDS (t);
4704 break;
4705 case UNION_TYPE:
4706 shift = 2;
4707 t2 = TYPE_FIELDS (t);
4708 break;
4709 default:
231bd014 4710 gcc_unreachable ();
4ee9c684 4711 }
ecf2703d 4712 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4713 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4714 n_elements = list_length (t2);
78c2e180 4715 /* We might have a VLA here. */
4716 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4717 size = 0;
4718 else
f9ae6f95 4719 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
ecf2703d 4720 return ((size << 24) | (n_elements << shift));
4ee9c684 4721}
4722
2ef51f0e 4723bool
4724c_type_hasher::equal (tree t1, tree t2)
4725{
4726 return lang_hooks.types_compatible_p (t1, t2);
4727}
4728
4729static GTY(()) hash_table<c_type_hasher> *type_hash_table;
1ecd4018 4730
b5ba9f3a 4731/* Return the typed-based alias set for T, which may be an expression
f7c44134 4732 or a type. Return -1 if we don't do anything special. */
ab2c1de8 4733
32c2fdea 4734alias_set_type
1cae46be 4735c_common_get_alias_set (tree t)
b5ba9f3a 4736{
be4f2de7 4737 tree u;
1cae46be 4738
e58c17e7 4739 /* For VLAs, use the alias set of the element type rather than the
4740 default of alias set 0 for types compared structurally. */
4741 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4742 {
4743 if (TREE_CODE (t) == ARRAY_TYPE)
4744 return get_alias_set (TREE_TYPE (t));
4745 return -1;
4746 }
4747
be4f2de7 4748 /* Permit type-punning when accessing a union, provided the access
4749 is directly through the union. For example, this code does not
4750 permit taking the address of a union member and then storing
4751 through it. Even the type-punning allowed here is a GCC
4752 extension, albeit a common and useful one; the C standard says
4753 that such accesses have implementation-defined behavior. */
4754 for (u = t;
4755 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
4756 u = TREE_OPERAND (u, 0))
4757 if (TREE_CODE (u) == COMPONENT_REF
4758 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
4759 return 0;
1e2513d9 4760
9fcc3e54 4761 /* That's all the expressions we handle specially. */
84166705 4762 if (!TYPE_P (t))
9fcc3e54 4763 return -1;
4764
d716ce75 4765 /* The C standard guarantees that any object may be accessed via an
9fcc3e54 4766 lvalue that has character type. */
4767 if (t == char_type_node
4768 || t == signed_char_type_node
4769 || t == unsigned_char_type_node)
f7c44134 4770 return 0;
a5b1863e 4771
1607663f 4772 /* The C standard specifically allows aliasing between signed and
4773 unsigned variants of the same type. We treat the signed
4774 variant as canonical. */
78a8ed03 4775 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
a8868e19 4776 {
4070745f 4777 tree t1 = c_common_signed_type (t);
1607663f 4778
a8868e19 4779 /* t1 == t can happen for boolean nodes which are always unsigned. */
4780 if (t1 != t)
4781 return get_alias_set (t1);
4782 }
1e2513d9 4783
4ee9c684 4784 /* Handle the case of multiple type nodes referring to "the same" type,
4785 which occurs with IMA. These share an alias set. FIXME: Currently only
4786 C90 is handled. (In C99 type compatibility is not transitive, which
4787 complicates things mightily. The alias set splay trees can theoretically
4788 represent this, but insertion is tricky when you consider all the
4789 different orders things might arrive in.) */
4790
4791 if (c_language != clk_c || flag_isoc99)
4792 return -1;
4793
0bed3869 4794 /* Save time if there's only one input file. */
e08bd2f4 4795 if (num_in_fnames == 1)
4ee9c684 4796 return -1;
4797
4798 /* Pointers need special handling if they point to any type that
4799 needs special handling (below). */
4800 if (TREE_CODE (t) == POINTER_TYPE)
4801 {
4802 tree t2;
4803 /* Find bottom type under any nested POINTERs. */
b27ac6b5 4804 for (t2 = TREE_TYPE (t);
af592f67 4805 TREE_CODE (t2) == POINTER_TYPE;
4806 t2 = TREE_TYPE (t2))
4807 ;
8e539fdc 4808 if (!RECORD_OR_UNION_TYPE_P (t2)
4809 && TREE_CODE (t2) != ENUMERAL_TYPE)
af592f67 4810 return -1;
4ee9c684 4811 if (TYPE_SIZE (t2) == 0)
af592f67 4812 return -1;
4ee9c684 4813 }
4814 /* These are the only cases that need special handling. */
8e539fdc 4815 if (!RECORD_OR_UNION_TYPE_P (t)
4ee9c684 4816 && TREE_CODE (t) != ENUMERAL_TYPE
4ee9c684 4817 && TREE_CODE (t) != POINTER_TYPE)
4818 return -1;
4819 /* Undefined? */
4820 if (TYPE_SIZE (t) == 0)
4821 return -1;
4822
b27ac6b5 4823 /* Look up t in hash table. Only one of the compatible types within each
4ee9c684 4824 alias set is recorded in the table. */
4825 if (!type_hash_table)
2ef51f0e 4826 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
4827 tree *slot = type_hash_table->find_slot (t, INSERT);
4ee9c684 4828 if (*slot != NULL)
ad16cb2c 4829 {
4830 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4831 return TYPE_ALIAS_SET ((tree)*slot);
4832 }
4ee9c684 4833 else
4834 /* Our caller will assign and record (in t) a new alias set; all we need
4835 to do is remember t in the hash table. */
4836 *slot = t;
4837
f7c44134 4838 return -1;
b5ba9f3a 4839}
902b4e01 4840\f
e60a6f7b 4841/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
a179a7dc 4842 the IS_SIZEOF parameter indicates which operator is being applied.
e60a6f7b 4843 The COMPLAIN flag controls whether we should diagnose possibly
4844 ill-formed constructs or not. LOC is the location of the SIZEOF or
a179a7dc 4845 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
4846 a type in any context should be returned, rather than the normal
4847 alignment for that type. */
908c697e 4848
902b4e01 4849tree
e60a6f7b 4850c_sizeof_or_alignof_type (location_t loc,
a179a7dc 4851 tree type, bool is_sizeof, bool min_alignof,
4852 int complain)
902b4e01 4853{
d4c4d95c 4854 const char *op_name;
4855 tree value = NULL;
4856 enum tree_code type_code = TREE_CODE (type);
1cae46be 4857
908c697e 4858 op_name = is_sizeof ? "sizeof" : "__alignof__";
1cae46be 4859
d4c4d95c 4860 if (type_code == FUNCTION_TYPE)
902b4e01 4861 {
908c697e 4862 if (is_sizeof)
d4c4d95c 4863 {
9205a6cc 4864 if (complain && warn_pointer_arith)
4865 pedwarn (loc, OPT_Wpointer_arith,
8864917d 4866 "invalid application of %<sizeof%> to a function type");
ebd21de4 4867 else if (!complain)
4868 return error_mark_node;
d4c4d95c 4869 value = size_one_node;
4870 }
4871 else
83e25171 4872 {
4873 if (complain)
4874 {
4875 if (c_dialect_cxx ())
29438999 4876 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
83e25171 4877 "%<alignof%> applied to a function type");
4878 else
29438999 4879 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
83e25171 4880 "%<_Alignof%> applied to a function type");
4881 }
4882 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4883 }
d4c4d95c 4884 }
4885 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4886 {
1cae46be 4887 if (type_code == VOID_TYPE
9205a6cc 4888 && complain && warn_pointer_arith)
4889 pedwarn (loc, OPT_Wpointer_arith,
8864917d 4890 "invalid application of %qs to a void type", op_name);
ebd21de4 4891 else if (!complain)
4892 return error_mark_node;
d4c4d95c 4893 value = size_one_node;
902b4e01 4894 }
3df19e1b 4895 else if (!COMPLETE_TYPE_P (type)
4896 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
902b4e01 4897 {
d3a4d008 4898 if (complain)
3df19e1b 4899 error_at (loc, "invalid application of %qs to incomplete type %qT",
e60a6f7b 4900 op_name, type);
9c719c74 4901 return error_mark_node;
902b4e01 4902 }
3df19e1b 4903 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4904 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4905 {
4906 if (complain)
4907 error_at (loc, "invalid application of %qs to array type %qT of "
4908 "incomplete element type", op_name, type);
4909 return error_mark_node;
4910 }
902b4e01 4911 else
d4c4d95c 4912 {
908c697e 4913 if (is_sizeof)
d4c4d95c 4914 /* Convert in case a char is more than one unit. */
389dd41b 4915 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4916 size_int (TYPE_PRECISION (char_type_node)
4917 / BITS_PER_UNIT));
a179a7dc 4918 else if (min_alignof)
c2c4ae8d 4919 value = size_int (min_align_of_type (type));
d4c4d95c 4920 else
d37625c0 4921 value = size_int (TYPE_ALIGN_UNIT (type));
d4c4d95c 4922 }
902b4e01 4923
5a1fe2db 4924 /* VALUE will have the middle-end integer type sizetype.
4925 However, we should really return a value of type `size_t',
4926 which is just a typedef for an ordinary integer type. */
389dd41b 4927 value = fold_convert_loc (loc, size_type_node, value);
1cae46be 4928
d4c4d95c 4929 return value;
902b4e01 4930}
4931
4932/* Implement the __alignof keyword: Return the minimum required
097b5c8b 4933 alignment of EXPR, measured in bytes. For VAR_DECLs,
4934 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
e60a6f7b 4935 from an "aligned" __attribute__ specification). LOC is the
4936 location of the ALIGNOF operator. */
72040e7e 4937
902b4e01 4938tree
e60a6f7b 4939c_alignof_expr (location_t loc, tree expr)
902b4e01 4940{
4941 tree t;
4942
097b5c8b 4943 if (VAR_OR_FUNCTION_DECL_P (expr))
d37625c0 4944 t = size_int (DECL_ALIGN_UNIT (expr));
1cae46be 4945
902b4e01 4946 else if (TREE_CODE (expr) == COMPONENT_REF
4947 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4948 {
e60a6f7b 4949 error_at (loc, "%<__alignof%> applied to a bit-field");
902b4e01 4950 t = size_one_node;
4951 }
4952 else if (TREE_CODE (expr) == COMPONENT_REF
7cc7e163 4953 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
d37625c0 4954 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
1cae46be 4955
aa3e402e 4956 else if (INDIRECT_REF_P (expr))
902b4e01 4957 {
4958 tree t = TREE_OPERAND (expr, 0);
4959 tree best = t;
4960 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1cae46be 4961
72dd6141 4962 while (CONVERT_EXPR_P (t)
7cc7e163 4963 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
902b4e01 4964 {
4965 int thisalign;
4966
4967 t = TREE_OPERAND (t, 0);
4968 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4969 if (thisalign > bestalign)
4970 best = t, bestalign = thisalign;
4971 }
e60a6f7b 4972 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
902b4e01 4973 }
4974 else
e60a6f7b 4975 return c_alignof (loc, TREE_TYPE (expr));
902b4e01 4976
389dd41b 4977 return fold_convert_loc (loc, size_type_node, t);
902b4e01 4978}
4979\f
8fe4a266 4980/* Handle C and C++ default attributes. */
4981
4982enum built_in_attribute
4983{
4984#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4985#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
c8010b80 4986#define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
8fe4a266 4987#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4988#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
8fe4a266 4989#include "builtin-attrs.def"
4990#undef DEF_ATTR_NULL_TREE
4991#undef DEF_ATTR_INT
c8010b80 4992#undef DEF_ATTR_STRING
8fe4a266 4993#undef DEF_ATTR_IDENT
4994#undef DEF_ATTR_TREE_LIST
8fe4a266 4995 ATTR_LAST
4996};
4997
4998static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4999
1cae46be 5000static void c_init_attributes (void);
8fe4a266 5001
27213ba3 5002enum c_builtin_type
72040e7e 5003{
d2d4bdde 5004#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5005#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5006#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5007#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5008#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5009#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
0a39fd54 5010#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
3c77ca67 5011#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5012 ARG6) NAME,
5013#define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5014 ARG6, ARG7) NAME,
5015#define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5016 ARG6, ARG7, ARG8) NAME,
43895be5 5017#define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5018 ARG6, ARG7, ARG8, ARG9) NAME,
5019#define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5020 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
5021#define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5022 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
d2d4bdde 5023#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5024#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5025#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
03901330 5026#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
0a39fd54 5027#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3c77ca67 5028#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
6349b8cc 5029 NAME,
e561d5e1 5030#define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5031 ARG6) NAME,
6349b8cc 5032#define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5033 ARG6, ARG7) NAME,
d2d4bdde 5034#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5035#include "builtin-types.def"
5036#undef DEF_PRIMITIVE_TYPE
5037#undef DEF_FUNCTION_TYPE_0
5038#undef DEF_FUNCTION_TYPE_1
5039#undef DEF_FUNCTION_TYPE_2
5040#undef DEF_FUNCTION_TYPE_3
5041#undef DEF_FUNCTION_TYPE_4
0a39fd54 5042#undef DEF_FUNCTION_TYPE_5
5043#undef DEF_FUNCTION_TYPE_6
27213ba3 5044#undef DEF_FUNCTION_TYPE_7
bc7bff74 5045#undef DEF_FUNCTION_TYPE_8
43895be5 5046#undef DEF_FUNCTION_TYPE_9
5047#undef DEF_FUNCTION_TYPE_10
5048#undef DEF_FUNCTION_TYPE_11
d2d4bdde 5049#undef DEF_FUNCTION_TYPE_VAR_0
5050#undef DEF_FUNCTION_TYPE_VAR_1
5051#undef DEF_FUNCTION_TYPE_VAR_2
03901330 5052#undef DEF_FUNCTION_TYPE_VAR_3
0a39fd54 5053#undef DEF_FUNCTION_TYPE_VAR_4
5054#undef DEF_FUNCTION_TYPE_VAR_5
e561d5e1 5055#undef DEF_FUNCTION_TYPE_VAR_6
6349b8cc 5056#undef DEF_FUNCTION_TYPE_VAR_7
d2d4bdde 5057#undef DEF_POINTER_TYPE
27213ba3 5058 BT_LAST
5059};
5060
5061typedef enum c_builtin_type builtin_type;
d2d4bdde 5062
27213ba3 5063/* A temporary array for c_common_nodes_and_builtins. Used in
5064 communication with def_fn_type. */
5065static tree builtin_types[(int) BT_LAST + 1];
d2d4bdde 5066
27213ba3 5067/* A helper function for c_common_nodes_and_builtins. Build function type
5068 for DEF with return type RET and N arguments. If VAR is true, then the
5069 function should be variadic after those N arguments.
5070
5071 Takes special care not to ICE if any of the types involved are
5072 error_mark_node, which indicates that said type is not in fact available
5073 (see builtin_type_for_size). In which case the function type as a whole
5074 should be error_mark_node. */
5075
5076static void
5077def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5078{
3a939d12 5079 tree t;
5080 tree *args = XALLOCAVEC (tree, n);
27213ba3 5081 va_list list;
5082 int i;
5083
5084 va_start (list, n);
5085 for (i = 0; i < n; ++i)
5086 {
7d339f93 5087 builtin_type a = (builtin_type) va_arg (list, int);
27213ba3 5088 t = builtin_types[a];
5089 if (t == error_mark_node)
5090 goto egress;
3a939d12 5091 args[i] = t;
27213ba3 5092 }
27213ba3 5093
27213ba3 5094 t = builtin_types[ret];
5095 if (t == error_mark_node)
5096 goto egress;
3a939d12 5097 if (var)
5098 t = build_varargs_function_type_array (t, n, args);
5099 else
5100 t = build_function_type_array (t, n, args);
27213ba3 5101
5102 egress:
5103 builtin_types[def] = t;
451c8e2f 5104 va_end (list);
27213ba3 5105}
5106
dce22712 5107/* Build builtin functions common to both C and C++ language
5108 frontends. */
5109
5110static void
5111c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5112{
5113#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5114 builtin_types[ENUM] = VALUE;
5115#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5116 def_fn_type (ENUM, RETURN, 0, 0);
5117#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5118 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5119#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5120 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5121#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5122 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5123#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5124 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5125#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5126 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5127#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5128 ARG6) \
5129 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5130#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5131 ARG6, ARG7) \
5132 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
bc7bff74 5133#define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5134 ARG6, ARG7, ARG8) \
5135 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5136 ARG7, ARG8);
43895be5 5137#define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5138 ARG6, ARG7, ARG8, ARG9) \
5139 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5140 ARG7, ARG8, ARG9);
5141#define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5142 ARG6, ARG7, ARG8, ARG9, ARG10) \
5143 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5144 ARG7, ARG8, ARG9, ARG10);
5145#define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5146 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5147 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5148 ARG7, ARG8, ARG9, ARG10, ARG11);
dce22712 5149#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5150 def_fn_type (ENUM, RETURN, 1, 0);
5151#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5152 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5153#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5154 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5155#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5156 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5157#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5158 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5159#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5160 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
e561d5e1 5161#define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5162 ARG6) \
5163 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
6349b8cc 5164#define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5165 ARG6, ARG7) \
5166 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
dce22712 5167#define DEF_POINTER_TYPE(ENUM, TYPE) \
5168 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5169
5170#include "builtin-types.def"
5171
5172#undef DEF_PRIMITIVE_TYPE
3c77ca67 5173#undef DEF_FUNCTION_TYPE_0
dce22712 5174#undef DEF_FUNCTION_TYPE_1
5175#undef DEF_FUNCTION_TYPE_2
5176#undef DEF_FUNCTION_TYPE_3
5177#undef DEF_FUNCTION_TYPE_4
5178#undef DEF_FUNCTION_TYPE_5
5179#undef DEF_FUNCTION_TYPE_6
3c77ca67 5180#undef DEF_FUNCTION_TYPE_7
5181#undef DEF_FUNCTION_TYPE_8
43895be5 5182#undef DEF_FUNCTION_TYPE_9
5183#undef DEF_FUNCTION_TYPE_10
5184#undef DEF_FUNCTION_TYPE_11
dce22712 5185#undef DEF_FUNCTION_TYPE_VAR_0
5186#undef DEF_FUNCTION_TYPE_VAR_1
5187#undef DEF_FUNCTION_TYPE_VAR_2
5188#undef DEF_FUNCTION_TYPE_VAR_3
5189#undef DEF_FUNCTION_TYPE_VAR_4
5190#undef DEF_FUNCTION_TYPE_VAR_5
e561d5e1 5191#undef DEF_FUNCTION_TYPE_VAR_6
6349b8cc 5192#undef DEF_FUNCTION_TYPE_VAR_7
dce22712 5193#undef DEF_POINTER_TYPE
5194 builtin_types[(int) BT_LAST] = NULL_TREE;
5195
5196 c_init_attributes ();
5197
5198#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5199 NONANSI_P, ATTRS, IMPLICIT, COND) \
5200 if (NAME && COND) \
5201 def_builtin_1 (ENUM, NAME, CLASS, \
5202 builtin_types[(int) TYPE], \
5203 builtin_types[(int) LIBTYPE], \
5204 BOTH_P, FALLBACK_P, NONANSI_P, \
5205 built_in_attributes[(int) ATTRS], IMPLICIT);
5206#include "builtins.def"
dce22712 5207
87eb1c28 5208 targetm.init_builtins ();
5209
471eff36 5210 build_common_builtin_nodes ();
dce22712 5211
a89e6c15 5212 if (flag_cilkplus)
d037099f 5213 cilk_init_builtins ();
dce22712 5214}
5215
9e6687c8 5216/* Like get_identifier, but avoid warnings about null arguments when
5217 the argument may be NULL for targets where GCC lacks stdint.h type
5218 information. */
5219
5220static inline tree
5221c_get_ident (const char *id)
5222{
5223 return get_identifier (id);
5224}
5225
27213ba3 5226/* Build tree nodes and builtin functions common to both C and C++ language
5227 frontends. */
5228
5229void
5230c_common_nodes_and_builtins (void)
5231{
924bbf02 5232 int char16_type_size;
5233 int char32_type_size;
174fcc61 5234 int wchar_type_size;
5235 tree array_domain_type;
2d47cc32 5236 tree va_list_ref_type_node;
8a15c04a 5237 tree va_list_arg_type_node;
9f75f026 5238 int i;
a66c9326 5239
600695e0 5240 build_common_tree_nodes (flag_signed_char);
e593356b 5241
174fcc61 5242 /* Define `int' and `char' first so that dbx will output them first. */
d946ea19 5243 record_builtin_type (RID_INT, NULL, integer_type_node);
174fcc61 5244 record_builtin_type (RID_CHAR, "char", char_type_node);
5245
5246 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5247 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5248 but not C. Are the conditionals here needed? */
c0f19401 5249 if (c_dialect_cxx ())
d946ea19 5250 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
174fcc61 5251 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5252 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5253 record_builtin_type (RID_MAX, "long unsigned int",
5254 long_unsigned_type_node);
9f75f026 5255
5256 for (i = 0; i < NUM_INT_N_ENTS; i ++)
6388cfe2 5257 {
9f75f026 5258 char name[25];
5259
5260 sprintf (name, "__int%d", int_n_data[i].bitsize);
76738f56 5261 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
9f75f026 5262 int_n_trees[i].signed_type);
5263 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
76738f56 5264 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
6388cfe2 5265 }
9f75f026 5266
c0f19401 5267 if (c_dialect_cxx ())
174fcc61 5268 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5269 record_builtin_type (RID_MAX, "long long int",
5270 long_long_integer_type_node);
5271 record_builtin_type (RID_MAX, "long long unsigned int",
5272 long_long_unsigned_type_node);
c0f19401 5273 if (c_dialect_cxx ())
174fcc61 5274 record_builtin_type (RID_MAX, "long long unsigned",
5275 long_long_unsigned_type_node);
5276 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5277 record_builtin_type (RID_MAX, "short unsigned int",
5278 short_unsigned_type_node);
c0f19401 5279 if (c_dialect_cxx ())
174fcc61 5280 record_builtin_type (RID_MAX, "unsigned short",
5281 short_unsigned_type_node);
5282
5283 /* Define both `signed char' and `unsigned char'. */
5284 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5285 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5286
771d21fa 5287 /* These are types that c_common_type_for_size and
5288 c_common_type_for_mode use. */
e60a6f7b 5289 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5290 TYPE_DECL, NULL_TREE,
dc24ddbd 5291 intQI_type_node));
e60a6f7b 5292 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5293 TYPE_DECL, NULL_TREE,
dc24ddbd 5294 intHI_type_node));
e60a6f7b 5295 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5296 TYPE_DECL, NULL_TREE,
dc24ddbd 5297 intSI_type_node));
e60a6f7b 5298 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5299 TYPE_DECL, NULL_TREE,
dc24ddbd 5300 intDI_type_node));
174fcc61 5301#if HOST_BITS_PER_WIDE_INT >= 64
9f75f026 5302 /* Note that this is different than the __int128 type that's part of
5303 the generic __intN support. */
f1515a39 5304 if (targetm.scalar_mode_supported_p (TImode))
e60a6f7b 5305 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5306 TYPE_DECL,
f1515a39 5307 get_identifier ("__int128_t"),
5308 intTI_type_node));
174fcc61 5309#endif
e60a6f7b 5310 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5311 TYPE_DECL, NULL_TREE,
dc24ddbd 5312 unsigned_intQI_type_node));
e60a6f7b 5313 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5314 TYPE_DECL, NULL_TREE,
dc24ddbd 5315 unsigned_intHI_type_node));
e60a6f7b 5316 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5317 TYPE_DECL, NULL_TREE,
dc24ddbd 5318 unsigned_intSI_type_node));
e60a6f7b 5319 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5320 TYPE_DECL, NULL_TREE,
dc24ddbd 5321 unsigned_intDI_type_node));
174fcc61 5322#if HOST_BITS_PER_WIDE_INT >= 64
f1515a39 5323 if (targetm.scalar_mode_supported_p (TImode))
e60a6f7b 5324 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5325 TYPE_DECL,
f1515a39 5326 get_identifier ("__uint128_t"),
5327 unsigned_intTI_type_node));
174fcc61 5328#endif
5329
5330 /* Create the widest literal types. */
5331 widest_integer_literal_type_node
5332 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
e60a6f7b 5333 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5334 TYPE_DECL, NULL_TREE,
dc24ddbd 5335 widest_integer_literal_type_node));
174fcc61 5336
5337 widest_unsigned_literal_type_node
5338 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
e60a6f7b 5339 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5340 TYPE_DECL, NULL_TREE,
dc24ddbd 5341 widest_unsigned_literal_type_node));
174fcc61 5342
654ef926 5343 signed_size_type_node = c_common_signed_type (size_type_node);
174fcc61 5344
73673831 5345 pid_type_node =
5346 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5347
d946ea19 5348 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5349 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
174fcc61 5350 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5351
c4503c0a 5352 /* Only supported decimal floating point extension if the target
5353 actually supports underlying modes. */
48e1416a 5354 if (targetm.scalar_mode_supported_p (SDmode)
c4503c0a 5355 && targetm.scalar_mode_supported_p (DDmode)
5356 && targetm.scalar_mode_supported_p (TDmode))
5357 {
5358 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5359 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5360 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5361 }
5362
9421ebb9 5363 if (targetm.fixed_point_supported_p ())
5364 {
5365 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5366 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5367 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5368 record_builtin_type (RID_MAX, "long long _Fract",
5369 long_long_fract_type_node);
5370 record_builtin_type (RID_MAX, "unsigned short _Fract",
5371 unsigned_short_fract_type_node);
5372 record_builtin_type (RID_MAX, "unsigned _Fract",
5373 unsigned_fract_type_node);
5374 record_builtin_type (RID_MAX, "unsigned long _Fract",
5375 unsigned_long_fract_type_node);
5376 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5377 unsigned_long_long_fract_type_node);
5378 record_builtin_type (RID_MAX, "_Sat short _Fract",
5379 sat_short_fract_type_node);
5380 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5381 record_builtin_type (RID_MAX, "_Sat long _Fract",
5382 sat_long_fract_type_node);
5383 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5384 sat_long_long_fract_type_node);
5385 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5386 sat_unsigned_short_fract_type_node);
5387 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5388 sat_unsigned_fract_type_node);
5389 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5390 sat_unsigned_long_fract_type_node);
5391 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5392 sat_unsigned_long_long_fract_type_node);
5393 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5394 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5395 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5396 record_builtin_type (RID_MAX, "long long _Accum",
5397 long_long_accum_type_node);
5398 record_builtin_type (RID_MAX, "unsigned short _Accum",
5399 unsigned_short_accum_type_node);
5400 record_builtin_type (RID_MAX, "unsigned _Accum",
5401 unsigned_accum_type_node);
5402 record_builtin_type (RID_MAX, "unsigned long _Accum",
5403 unsigned_long_accum_type_node);
5404 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5405 unsigned_long_long_accum_type_node);
5406 record_builtin_type (RID_MAX, "_Sat short _Accum",
5407 sat_short_accum_type_node);
5408 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5409 record_builtin_type (RID_MAX, "_Sat long _Accum",
5410 sat_long_accum_type_node);
5411 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5412 sat_long_long_accum_type_node);
5413 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5414 sat_unsigned_short_accum_type_node);
5415 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5416 sat_unsigned_accum_type_node);
5417 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5418 sat_unsigned_long_accum_type_node);
5419 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5420 sat_unsigned_long_long_accum_type_node);
5421
5422 }
5423
e60a6f7b 5424 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5425 TYPE_DECL,
dc24ddbd 5426 get_identifier ("complex int"),
5427 complex_integer_type_node));
e60a6f7b 5428 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5429 TYPE_DECL,
dc24ddbd 5430 get_identifier ("complex float"),
5431 complex_float_type_node));
e60a6f7b 5432 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5433 TYPE_DECL,
dc24ddbd 5434 get_identifier ("complex double"),
5435 complex_double_type_node));
5436 lang_hooks.decls.pushdecl
e60a6f7b 5437 (build_decl (UNKNOWN_LOCATION,
5438 TYPE_DECL, get_identifier ("complex long double"),
20325f61 5439 complex_long_double_type_node));
174fcc61 5440
e256d445 5441 if (c_dialect_cxx ())
5442 /* For C++, make fileptr_type_node a distinct void * type until
5443 FILE type is defined. */
e086912e 5444 fileptr_type_node = build_variant_type_copy (ptr_type_node);
e256d445 5445
d946ea19 5446 record_builtin_type (RID_VOID, NULL, void_type_node);
174fcc61 5447
6753bca0 5448 /* Set the TYPE_NAME for any variants that were built before
5449 record_builtin_type gave names to the built-in types. */
5450 {
5451 tree void_name = TYPE_NAME (void_type_node);
5452 TYPE_NAME (void_type_node) = NULL_TREE;
5453 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5454 = void_name;
5455 TYPE_NAME (void_type_node) = void_name;
5456 }
5457
174fcc61 5458 void_list_node = build_void_list_node ();
5459
5460 /* Make a type to be the domain of a few array types
5461 whose domains don't really matter.
5462 200 is small enough that it always fits in size_t
5463 and large enough that it can hold most function names for the
5464 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5465 array_domain_type = build_index_type (size_int (200));
5466
5467 /* Make a type for arrays of characters.
5468 With luck nothing will ever really depend on the length of this
5469 array type. */
5470 char_array_type_node
5471 = build_array_type (char_type_node, array_domain_type);
5472
d2d4bdde 5473 string_type_node = build_pointer_type (char_type_node);
5474 const_string_type_node
5475 = build_pointer_type (build_qualified_type
5476 (char_type_node, TYPE_QUAL_CONST));
5477
174fcc61 5478 /* This is special for C++ so functions can be overloaded. */
18ef7ac2 5479 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
174fcc61 5480 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5481 wchar_type_size = TYPE_PRECISION (wchar_type_node);
f3449a3c 5482 underlying_wchar_type_node = wchar_type_node;
c0f19401 5483 if (c_dialect_cxx ())
174fcc61 5484 {
78a8ed03 5485 if (TYPE_UNSIGNED (wchar_type_node))
174fcc61 5486 wchar_type_node = make_unsigned_type (wchar_type_size);
5487 else
5488 wchar_type_node = make_signed_type (wchar_type_size);
5489 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5490 }
174fcc61 5491
5492 /* This is for wide string constants. */
5493 wchar_array_type_node
5494 = build_array_type (wchar_type_node, array_domain_type);
5495
924bbf02 5496 /* Define 'char16_t'. */
5497 char16_type_node = get_identifier (CHAR16_TYPE);
5498 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5499 char16_type_size = TYPE_PRECISION (char16_type_node);
5500 if (c_dialect_cxx ())
5501 {
5502 char16_type_node = make_unsigned_type (char16_type_size);
5503
60777f69 5504 if (cxx_dialect >= cxx11)
924bbf02 5505 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5506 }
5507
5508 /* This is for UTF-16 string constants. */
5509 char16_array_type_node
5510 = build_array_type (char16_type_node, array_domain_type);
5511
5512 /* Define 'char32_t'. */
5513 char32_type_node = get_identifier (CHAR32_TYPE);
5514 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5515 char32_type_size = TYPE_PRECISION (char32_type_node);
5516 if (c_dialect_cxx ())
5517 {
5518 char32_type_node = make_unsigned_type (char32_type_size);
5519
60777f69 5520 if (cxx_dialect >= cxx11)
924bbf02 5521 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5522 }
5523
5524 /* This is for UTF-32 string constants. */
5525 char32_array_type_node
5526 = build_array_type (char32_type_node, array_domain_type);
5527
6bf5ed8d 5528 wint_type_node =
5529 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5530
5531 intmax_type_node =
5532 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5533 uintmax_type_node =
5534 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5535
f3449a3c 5536 if (SIG_ATOMIC_TYPE)
5537 sig_atomic_type_node =
9e6687c8 5538 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
f3449a3c 5539 if (INT8_TYPE)
5540 int8_type_node =
9e6687c8 5541 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
f3449a3c 5542 if (INT16_TYPE)
5543 int16_type_node =
9e6687c8 5544 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
f3449a3c 5545 if (INT32_TYPE)
5546 int32_type_node =
9e6687c8 5547 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
f3449a3c 5548 if (INT64_TYPE)
5549 int64_type_node =
9e6687c8 5550 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
f3449a3c 5551 if (UINT8_TYPE)
5552 uint8_type_node =
9e6687c8 5553 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
f3449a3c 5554 if (UINT16_TYPE)
d1081017 5555 c_uint16_type_node = uint16_type_node =
9e6687c8 5556 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
f3449a3c 5557 if (UINT32_TYPE)
d1081017 5558 c_uint32_type_node = uint32_type_node =
9e6687c8 5559 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
f3449a3c 5560 if (UINT64_TYPE)
d1081017 5561 c_uint64_type_node = uint64_type_node =
9e6687c8 5562 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
f3449a3c 5563 if (INT_LEAST8_TYPE)
5564 int_least8_type_node =
9e6687c8 5565 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
f3449a3c 5566 if (INT_LEAST16_TYPE)
5567 int_least16_type_node =
9e6687c8 5568 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
f3449a3c 5569 if (INT_LEAST32_TYPE)
5570 int_least32_type_node =
9e6687c8 5571 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
f3449a3c 5572 if (INT_LEAST64_TYPE)
5573 int_least64_type_node =
9e6687c8 5574 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
f3449a3c 5575 if (UINT_LEAST8_TYPE)
5576 uint_least8_type_node =
9e6687c8 5577 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
f3449a3c 5578 if (UINT_LEAST16_TYPE)
5579 uint_least16_type_node =
9e6687c8 5580 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
f3449a3c 5581 if (UINT_LEAST32_TYPE)
5582 uint_least32_type_node =
9e6687c8 5583 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
f3449a3c 5584 if (UINT_LEAST64_TYPE)
5585 uint_least64_type_node =
9e6687c8 5586 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
f3449a3c 5587 if (INT_FAST8_TYPE)
5588 int_fast8_type_node =
9e6687c8 5589 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
f3449a3c 5590 if (INT_FAST16_TYPE)
5591 int_fast16_type_node =
9e6687c8 5592 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
f3449a3c 5593 if (INT_FAST32_TYPE)
5594 int_fast32_type_node =
9e6687c8 5595 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
f3449a3c 5596 if (INT_FAST64_TYPE)
5597 int_fast64_type_node =
9e6687c8 5598 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
f3449a3c 5599 if (UINT_FAST8_TYPE)
5600 uint_fast8_type_node =
9e6687c8 5601 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
f3449a3c 5602 if (UINT_FAST16_TYPE)
5603 uint_fast16_type_node =
9e6687c8 5604 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
f3449a3c 5605 if (UINT_FAST32_TYPE)
5606 uint_fast32_type_node =
9e6687c8 5607 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
f3449a3c 5608 if (UINT_FAST64_TYPE)
5609 uint_fast64_type_node =
9e6687c8 5610 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
f3449a3c 5611 if (INTPTR_TYPE)
5612 intptr_type_node =
9e6687c8 5613 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
f3449a3c 5614 if (UINTPTR_TYPE)
5615 uintptr_type_node =
9e6687c8 5616 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
f3449a3c 5617
3a939d12 5618 default_function_type
5619 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
6bf5ed8d 5620 ptrdiff_type_node
5621 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
11773141 5622 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
6bf5ed8d 5623
dc24ddbd 5624 lang_hooks.decls.pushdecl
e60a6f7b 5625 (build_decl (UNKNOWN_LOCATION,
5626 TYPE_DECL, get_identifier ("__builtin_va_list"),
20325f61 5627 va_list_type_node));
202d6e5f 5628 if (targetm.enum_va_list_p)
acd6f472 5629 {
5630 int l;
5631 const char *pname;
5632 tree ptype;
5f57a8b1 5633
202d6e5f 5634 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
acd6f472 5635 {
5636 lang_hooks.decls.pushdecl
5637 (build_decl (UNKNOWN_LOCATION,
5638 TYPE_DECL, get_identifier (pname),
5639 ptype));
ab2c1de8 5640
acd6f472 5641 }
5642 }
8a15c04a 5643
8a15c04a 5644 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
2d47cc32 5645 {
5646 va_list_arg_type_node = va_list_ref_type_node =
5647 build_pointer_type (TREE_TYPE (va_list_type_node));
5648 }
8a15c04a 5649 else
2d47cc32 5650 {
5651 va_list_arg_type_node = va_list_type_node;
5652 va_list_ref_type_node = build_reference_type (va_list_type_node);
5653 }
1cae46be 5654
dce22712 5655 if (!flag_preprocess_only)
5656 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
ffa8918b 5657
5c62f199 5658 main_identifier_node = get_identifier ("main");
ae84079f 5659
5660 /* Create the built-in __null node. It is important that this is
5661 not shared. */
271e739a 5662 null_node = make_int_cst (1, 1);
ae84079f 5663 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
27213ba3 5664
5665 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5666 memset (builtin_types, 0, sizeof (builtin_types));
72040e7e 5667}
a66c9326 5668
79b01846 5669/* The number of named compound-literals generated thus far. */
5670static GTY(()) int compound_literal_number;
5671
5672/* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5673
5674void
5675set_compound_literal_name (tree decl)
5676{
5677 char *name;
5678 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5679 compound_literal_number);
5680 compound_literal_number++;
5681 DECL_NAME (decl) = get_identifier (name);
5682}
5683
2e474820 5684/* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
5685 TYPE and operand OP. */
5686
5687static tree
5688build_va_arg_1 (location_t loc, tree type, tree op)
5689{
5690 tree expr = build1 (VA_ARG_EXPR, type, op);
5691 SET_EXPR_LOCATION (expr, loc);
5692 return expr;
5693}
5694
5695/* Return a VA_ARG_EXPR corresponding to a source-level expression
5696 va_arg (EXPR, TYPE) at source location LOC. */
5697
a66c9326 5698tree
e60a6f7b 5699build_va_arg (location_t loc, tree expr, tree type)
a66c9326 5700{
c37be9ec 5701 tree va_type = TREE_TYPE (expr);
5702 tree canon_va_type = (va_type == error_mark_node
5703 ? NULL_TREE
5704 : targetm.canonical_va_list_type (va_type));
5705
2e474820 5706 if (va_type == error_mark_node
5707 || canon_va_type == NULL_TREE)
c37be9ec 5708 {
2e474820 5709 /* Let's handle things neutrallly, if expr:
5710 - has undeclared type, or
5711 - is not an va_list type. */
5712 return build_va_arg_1 (loc, type, expr);
c37be9ec 5713 }
f7fec1c7 5714
2e474820 5715 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
5716 {
5717 /* Case 1: Not an array type. */
5718
5719 /* Take the address, to get '&ap'. */
5720 mark_addressable (expr);
5721 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
5722
5723 /* Verify that &ap is still recognized as having va_list type. */
5724 tree canon_expr_type
5725 = targetm.canonical_va_list_type (TREE_TYPE (expr));
926f9422 5726 if (canon_expr_type == NULL_TREE)
5727 {
5728 error_at (loc,
5729 "first argument to %<va_arg%> not of type %<va_list%>");
5730 return error_mark_node;
5731 }
2e474820 5732
5733 return build_va_arg_1 (loc, type, expr);
5734 }
5735
5736 /* Case 2: Array type.
5737
5738 Background:
5739
5740 For contrast, let's start with the simple case (case 1). If
5741 canon_va_type is not an array type, but say a char *, then when
5742 passing-by-value a va_list, the type of the va_list param decl is
5743 the same as for another va_list decl (all ap's are char *):
5744
5745 f2_1 (char * ap)
5746 D.1815 = VA_ARG (&ap, 0B, 1);
5747 return D.1815;
5748
5749 f2 (int i)
5750 char * ap.0;
5751 char * ap;
5752 __builtin_va_start (&ap, 0);
5753 ap.0 = ap;
5754 res = f2_1 (ap.0);
5755 __builtin_va_end (&ap);
5756 D.1812 = res;
5757 return D.1812;
5758
5759 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
5760 va_list the type of the va_list param decl (case 2b, struct * ap) is not
5761 the same as for another va_list decl (case 2a, struct ap[1]).
5762
5763 f2_1 (struct * ap)
5764 D.1844 = VA_ARG (ap, 0B, 0);
5765 return D.1844;
5766
5767 f2 (int i)
5768 struct ap[1];
5769 __builtin_va_start (&ap, 0);
5770 res = f2_1 (&ap);
5771 __builtin_va_end (&ap);
5772 D.1841 = res;
5773 return D.1841;
5774
5775 Case 2b is different because:
5776 - on the callee side, the parm decl has declared type va_list, but
5777 grokdeclarator changes the type of the parm decl to a pointer to the
5778 array elem type.
5779 - on the caller side, the pass-by-value uses &ap.
5780
5781 We unify these two cases (case 2a: va_list is array type,
5782 case 2b: va_list is pointer to array elem type), by adding '&' for the
5783 array type case, such that we have a pointer to array elem in both
5784 cases. */
5785
5786 if (TREE_CODE (va_type) == ARRAY_TYPE)
5787 {
5788 /* Case 2a: va_list is array type. */
5789
5790 /* Take the address, to get '&ap'. Make sure it's a pointer to array
5791 elem type. */
5792 mark_addressable (expr);
5793 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
5794 expr);
5795
5796 /* Verify that &ap is still recognized as having va_list type. */
5797 tree canon_expr_type
5798 = targetm.canonical_va_list_type (TREE_TYPE (expr));
926f9422 5799 if (canon_expr_type == NULL_TREE)
5800 {
5801 error_at (loc,
5802 "first argument to %<va_arg%> not of type %<va_list%>");
5803 return error_mark_node;
5804 }
2e474820 5805 }
5806 else
5807 {
5808 /* Case 2b: va_list is pointer to array elem type. */
5809 gcc_assert (POINTER_TYPE_P (va_type));
5810 gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type));
5811
5812 /* Don't take the address. We've already got '&ap'. */
5813 ;
5814 }
5815
5816 return build_va_arg_1 (loc, type, expr);
a66c9326 5817}
0d4238dc 5818
5819
dd878098 5820/* Linked list of disabled built-in functions. */
5821
6dc50383 5822struct disabled_builtin
dd878098 5823{
5824 const char *name;
5825 struct disabled_builtin *next;
6dc50383 5826};
dd878098 5827static disabled_builtin *disabled_builtins = NULL;
5828
1cae46be 5829static bool builtin_function_disabled_p (const char *);
dd878098 5830
5831/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5832 begins with "__builtin_", give an error. */
5833
5834void
1cae46be 5835disable_builtin_function (const char *name)
dd878098 5836{
5837 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
b0b1af64 5838 error ("cannot disable built-in function %qs", name);
dd878098 5839 else
5840 {
e85905e5 5841 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5842 new_disabled_builtin->name = name;
5843 new_disabled_builtin->next = disabled_builtins;
5844 disabled_builtins = new_disabled_builtin;
dd878098 5845 }
5846}
5847
5848
5849/* Return true if the built-in function NAME has been disabled, false
5850 otherwise. */
5851
5852static bool
1cae46be 5853builtin_function_disabled_p (const char *name)
dd878098 5854{
5855 disabled_builtin *p;
5856 for (p = disabled_builtins; p != NULL; p = p->next)
5857 {
5858 if (strcmp (name, p->name) == 0)
5859 return true;
5860 }
5861 return false;
5862}
5863
5864
3237155d 5865/* Worker for DEF_BUILTIN.
5866 Possibly define a builtin function with one or two names.
5867 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5868 nonansi_p and flag_no_nonansi_builtin. */
0d4238dc 5869
3237155d 5870static void
5871def_builtin_1 (enum built_in_function fncode,
5872 const char *name,
5873 enum built_in_class fnclass,
5874 tree fntype, tree libtype,
5875 bool both_p, bool fallback_p, bool nonansi_p,
5876 tree fnattrs, bool implicit_p)
0d4238dc 5877{
3237155d 5878 tree decl;
5879 const char *libname;
5880
27213ba3 5881 if (fntype == error_mark_node)
5882 return;
5883
3237155d 5884 gcc_assert ((!both_p && !fallback_p)
5885 || !strncmp (name, "__builtin_",
5886 strlen ("__builtin_")));
5887
5888 libname = name + strlen ("__builtin_");
54be5d7e 5889 decl = add_builtin_function (name, fntype, fncode, fnclass,
5890 (fallback_p ? libname : NULL),
5891 fnattrs);
b9a16870 5892
5893 set_builtin_decl (fncode, decl, implicit_p);
5894
3237155d 5895 if (both_p
5896 && !flag_no_builtin && !builtin_function_disabled_p (libname)
dd878098 5897 && !(nonansi_p && flag_no_nonansi_builtin))
54be5d7e 5898 add_builtin_function (libname, libtype, fncode, fnclass,
5899 NULL, fnattrs);
0d4238dc 5900}
e94026da 5901\f
d7aeef06 5902/* Nonzero if the type T promotes to int. This is (nearly) the
5903 integral promotions defined in ISO C99 6.3.1.1/2. */
5904
5905bool
9f627b1a 5906c_promoting_integer_type_p (const_tree t)
d7aeef06 5907{
5908 switch (TREE_CODE (t))
5909 {
5910 case INTEGER_TYPE:
5911 return (TYPE_MAIN_VARIANT (t) == char_type_node
5912 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5913 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5914 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
7aa1e6eb 5915 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5916 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d7aeef06 5917
5918 case ENUMERAL_TYPE:
5919 /* ??? Technically all enumerations not larger than an int
5920 promote to an int. But this is used along code paths
5921 that only want to notice a size change. */
5922 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5923
5924 case BOOLEAN_TYPE:
5925 return 1;
5926
5927 default:
5928 return 0;
5929 }
5930}
5931
e94026da 5932/* Return 1 if PARMS specifies a fixed number of parameters
5933 and none of their types is affected by default promotions. */
5934
5935int
9f627b1a 5936self_promoting_args_p (const_tree parms)
e94026da 5937{
9f627b1a 5938 const_tree t;
e94026da 5939 for (t = parms; t; t = TREE_CHAIN (t))
5940 {
19cb6b50 5941 tree type = TREE_VALUE (t);
43f74bc4 5942
e1d8e198 5943 if (type == error_mark_node)
5944 continue;
5945
e94026da 5946 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5947 return 0;
5948
5949 if (type == 0)
5950 return 0;
5951
5952 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5953 return 0;
5954
d7aeef06 5955 if (c_promoting_integer_type_p (type))
e94026da 5956 return 0;
5957 }
5958 return 1;
5959}
605fb01e 5960
c10de5e7 5961/* Recursively remove any '*' or '&' operator from TYPE. */
5962tree
5963strip_pointer_operator (tree t)
5964{
5965 while (POINTER_TYPE_P (t))
5966 t = TREE_TYPE (t);
5967 return t;
5968}
5969
57a0ed23 5970/* Recursively remove pointer or array type from TYPE. */
5971tree
5972strip_pointer_or_array_types (tree t)
5973{
5974 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5975 t = TREE_TYPE (t);
5976 return t;
5977}
5978
e41f0d80 5979/* Used to compare case labels. K1 and K2 are actually tree nodes
5980 representing case labels, or NULL_TREE for a `default' label.
5981 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5982 K2, and 0 if K1 and K2 are equal. */
5983
5984int
1cae46be 5985case_compare (splay_tree_key k1, splay_tree_key k2)
e41f0d80 5986{
5987 /* Consider a NULL key (such as arises with a `default' label) to be
5988 smaller than anything else. */
5989 if (!k1)
5990 return k2 ? -1 : 0;
5991 else if (!k2)
5992 return k1 ? 1 : 0;
5993
5994 return tree_int_cst_compare ((tree) k1, (tree) k2);
5995}
5996
e60a6f7b 5997/* Process a case label, located at LOC, for the range LOW_VALUE
5998 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5999 then this case label is actually a `default' label. If only
6000 HIGH_VALUE is NULL_TREE, then case label was declared using the
6001 usual C/C++ syntax, rather than the GNU case range extension.
6002 CASES is a tree containing all the case ranges processed so far;
be23b16f 6003 COND is the condition for the switch-statement itself.
6004 OUTSIDE_RANGE_P says whether there was a case value that doesn't
6005 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
6006 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
e41f0d80 6007
6008tree
e60a6f7b 6009c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
be23b16f 6010 tree low_value, tree high_value, bool *outside_range_p)
e41f0d80 6011{
6012 tree type;
6013 tree label;
6014 tree case_label;
6015 splay_tree_node node;
6016
6017 /* Create the LABEL_DECL itself. */
e60a6f7b 6018 label = create_artificial_label (loc);
e41f0d80 6019
6020 /* If there was an error processing the switch condition, bail now
6021 before we get more confused. */
6022 if (!cond || cond == error_mark_node)
4ee9c684 6023 goto error_out;
e41f0d80 6024
1cae46be 6025 if ((low_value && TREE_TYPE (low_value)
6026 && POINTER_TYPE_P (TREE_TYPE (low_value)))
e41f0d80 6027 || (high_value && TREE_TYPE (high_value)
6028 && POINTER_TYPE_P (TREE_TYPE (high_value))))
b96dc121 6029 {
e60a6f7b 6030 error_at (loc, "pointers are not permitted as case values");
b96dc121 6031 goto error_out;
6032 }
e41f0d80 6033
6034 /* Case ranges are a GNU extension. */
8864917d 6035 if (high_value)
29438999 6036 pedwarn (loc, OPT_Wpedantic,
8864917d 6037 "range expressions in switch statements are non-standard");
e41f0d80 6038
6039 type = TREE_TYPE (cond);
6040 if (low_value)
6041 {
2d2f6a15 6042 low_value = check_case_value (loc, low_value);
22a75734 6043 low_value = convert_and_check (loc, type, low_value);
96722196 6044 if (low_value == error_mark_node)
6045 goto error_out;
e41f0d80 6046 }
6047 if (high_value)
6048 {
2d2f6a15 6049 high_value = check_case_value (loc, high_value);
22a75734 6050 high_value = convert_and_check (loc, type, high_value);
96722196 6051 if (high_value == error_mark_node)
6052 goto error_out;
e41f0d80 6053 }
6054
96722196 6055 if (low_value && high_value)
6056 {
6057 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
a0c938f0 6058 really a case range, even though it was written that way.
6059 Remove the HIGH_VALUE to simplify later processing. */
96722196 6060 if (tree_int_cst_equal (low_value, high_value))
6061 high_value = NULL_TREE;
6062 else if (!tree_int_cst_lt (low_value, high_value))
e60a6f7b 6063 warning_at (loc, 0, "empty range specified");
96722196 6064 }
e41f0d80 6065
2ca392fd 6066 /* See if the case is in range of the type of the original testing
6067 expression. If both low_value and high_value are out of range,
6068 don't insert the case label and return NULL_TREE. */
6069 if (low_value
f61a9bc2 6070 && !check_case_bounds (loc, type, orig_type,
be23b16f 6071 &low_value, high_value ? &high_value : NULL,
6072 outside_range_p))
2ca392fd 6073 return NULL_TREE;
6074
e41f0d80 6075 /* Look up the LOW_VALUE in the table of case labels we already
6076 have. */
6077 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6078 /* If there was not an exact match, check for overlapping ranges.
6079 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6080 that's a `default' label and the only overlap is an exact match. */
6081 if (!node && (low_value || high_value))
6082 {
6083 splay_tree_node low_bound;
6084 splay_tree_node high_bound;
6085
6086 /* Even though there wasn't an exact match, there might be an
6087 overlap between this case range and another case range.
6088 Since we've (inductively) not allowed any overlapping case
6089 ranges, we simply need to find the greatest low case label
6090 that is smaller that LOW_VALUE, and the smallest low case
6091 label that is greater than LOW_VALUE. If there is an overlap
6092 it will occur in one of these two ranges. */
6093 low_bound = splay_tree_predecessor (cases,
6094 (splay_tree_key) low_value);
6095 high_bound = splay_tree_successor (cases,
6096 (splay_tree_key) low_value);
6097
6098 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6099 the LOW_VALUE, so there is no need to check unless the
6100 LOW_BOUND is in fact itself a case range. */
6101 if (low_bound
6102 && CASE_HIGH ((tree) low_bound->value)
6103 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6104 low_value) >= 0)
6105 node = low_bound;
6106 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6107 range is bigger than the low end of the current range, so we
6108 are only interested if the current range is a real range, and
6109 not an ordinary case label. */
1cae46be 6110 else if (high_bound
e41f0d80 6111 && high_value
6112 && (tree_int_cst_compare ((tree) high_bound->key,
6113 high_value)
6114 <= 0))
6115 node = high_bound;
6116 }
6117 /* If there was an overlap, issue an error. */
6118 if (node)
6119 {
eaae3b75 6120 tree duplicate = CASE_LABEL ((tree) node->value);
e41f0d80 6121
6122 if (high_value)
6123 {
e60a6f7b 6124 error_at (loc, "duplicate (or overlapping) case value");
6125 error_at (DECL_SOURCE_LOCATION (duplicate),
6126 "this is the first entry overlapping that value");
e41f0d80 6127 }
6128 else if (low_value)
6129 {
e60a6f7b 6130 error_at (loc, "duplicate case value") ;
6131 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
e41f0d80 6132 }
6133 else
6134 {
e60a6f7b 6135 error_at (loc, "multiple default labels in one switch");
6136 error_at (DECL_SOURCE_LOCATION (duplicate),
6137 "this is the first default label");
e41f0d80 6138 }
4ee9c684 6139 goto error_out;
e41f0d80 6140 }
6141
6142 /* Add a CASE_LABEL to the statement-tree. */
b6e3dd65 6143 case_label = add_stmt (build_case_label (low_value, high_value, label));
e41f0d80 6144 /* Register this case label in the splay tree. */
1cae46be 6145 splay_tree_insert (cases,
e41f0d80 6146 (splay_tree_key) low_value,
6147 (splay_tree_value) case_label);
6148
6149 return case_label;
4ee9c684 6150
6151 error_out:
daf6dff5 6152 /* Add a label so that the back-end doesn't think that the beginning of
4ee9c684 6153 the switch is unreachable. Note that we do not add a case label, as
a53ff4c1 6154 that just leads to duplicates and thence to failure later on. */
4ee9c684 6155 if (!cases->root)
6156 {
e60a6f7b 6157 tree t = create_artificial_label (loc);
6158 add_stmt (build_stmt (loc, LABEL_EXPR, t));
4ee9c684 6159 }
6160 return error_mark_node;
6161}
6162
6163/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6164 Used to verify that case values match up with enumerator values. */
6165
6166static void
6167match_case_to_enum_1 (tree key, tree type, tree label)
6168{
e913b5cd 6169 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6170
dd76621f 6171 if (tree_fits_uhwi_p (key))
6172 print_dec (key, buf, UNSIGNED);
6173 else if (tree_fits_shwi_p (key))
6174 print_dec (key, buf, SIGNED);
4ee9c684 6175 else
e913b5cd 6176 print_hex (key, buf);
4ee9c684 6177
6178 if (TYPE_NAME (type) == 0)
712d2297 6179 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6180 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6181 "case value %qs not in enumerated type",
6182 buf);
4ee9c684 6183 else
712d2297 6184 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6185 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6186 "case value %qs not in enumerated type %qT",
6187 buf, type);
4ee9c684 6188}
6189
359d87c6 6190/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6191 Used to verify that case values match up with enumerator values. */
6192
4ee9c684 6193static int
6194match_case_to_enum (splay_tree_node node, void *data)
6195{
6196 tree label = (tree) node->value;
4fd61bc6 6197 tree type = (tree) data;
4ee9c684 6198
6199 /* Skip default case. */
6200 if (!CASE_LOW (label))
6201 return 0;
6202
359d87c6 6203 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
4ee9c684 6204 when we did our enum->case scan. Reset our scratch bit after. */
359d87c6 6205 if (!CASE_LOW_SEEN (label))
4ee9c684 6206 match_case_to_enum_1 (CASE_LOW (label), type, label);
6207 else
359d87c6 6208 CASE_LOW_SEEN (label) = 0;
4ee9c684 6209
359d87c6 6210 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6211 not set, that means that CASE_HIGH did not appear when we did our
6212 enum->case scan. Reset our scratch bit after. */
4ee9c684 6213 if (CASE_HIGH (label))
6214 {
359d87c6 6215 if (!CASE_HIGH_SEEN (label))
6216 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6217 else
6218 CASE_HIGH_SEEN (label) = 0;
4ee9c684 6219 }
6220
6221 return 0;
6222}
6223
e7911019 6224/* Handle -Wswitch*. Called from the front end after parsing the
6225 switch construct. */
6226/* ??? Should probably be somewhere generic, since other languages
6227 besides C and C++ would want this. At the moment, however, C/C++
6228 are the only tree-ssa languages that support enumerations at all,
6229 so the point is moot. */
4ee9c684 6230
e7911019 6231void
6232c_do_switch_warnings (splay_tree cases, location_t switch_location,
be23b16f 6233 tree type, tree cond, bool bool_cond_p,
6234 bool outside_range_p)
4ee9c684 6235{
b27ac6b5 6236 splay_tree_node default_node;
359d87c6 6237 splay_tree_node node;
6238 tree chain;
4ee9c684 6239
be23b16f 6240 if (!warn_switch && !warn_switch_enum && !warn_switch_default
6241 && !warn_switch_bool)
4ee9c684 6242 return;
6243
4ee9c684 6244 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
8b6866af 6245 if (!default_node)
5fb6a912 6246 warning_at (switch_location, OPT_Wswitch_default,
6247 "switch missing default case");
4ee9c684 6248
be23b16f 6249 /* There are certain cases where -Wswitch-bool warnings aren't
6250 desirable, such as
6251 switch (boolean)
6252 {
6253 case true: ...
6254 case false: ...
6255 }
6256 so be careful here. */
6257 if (warn_switch_bool && bool_cond_p)
6258 {
6259 splay_tree_node min_node;
6260 /* If there's a default node, it's also the value with the minimal
6261 key. So look at the penultimate key (if any). */
6262 if (default_node)
6263 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
6264 else
6265 min_node = splay_tree_min (cases);
6266 tree min = min_node ? (tree) min_node->key : NULL_TREE;
6267
6268 splay_tree_node max_node = splay_tree_max (cases);
6269 /* This might be a case range, so look at the value with the
6270 maximal key and then check CASE_HIGH. */
6271 tree max = max_node ? (tree) max_node->value : NULL_TREE;
6272 if (max)
6273 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
6274
6275 /* If there's a case value > 1 or < 0, that is outside bool
6276 range, warn. */
6277 if (outside_range_p
6278 || (max && wi::gts_p (max, 1))
6279 || (min && wi::lts_p (min, 0))
6280 /* And handle the
6281 switch (boolean)
6282 {
6283 case true: ...
6284 case false: ...
6285 default: ...
6286 }
6287 case, where we want to warn. */
6288 || (default_node
6289 && max && wi::eq_p (max, 1)
6290 && min && wi::eq_p (min, 0)))
6291 warning_at (switch_location, OPT_Wswitch_bool,
6292 "switch condition has boolean value");
6293 }
6294
47ae02b7 6295 /* From here on, we only care about enumerated types. */
359d87c6 6296 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6297 return;
6298
561017b5 6299 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6300 if (!warn_switch_enum && !warn_switch)
359d87c6 6301 return;
6302
561017b5 6303 /* Check the cases. Warn about case values which are not members of
6304 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6305 there is no default case, check that exactly all enumeration
6306 literals are covered by the cases. */
6307
359d87c6 6308 /* Clearing COND if it is not an integer constant simplifies
6309 the tests inside the loop below. */
6310 if (TREE_CODE (cond) != INTEGER_CST)
6311 cond = NULL_TREE;
6312
6313 /* The time complexity here is O(N*lg(N)) worst case, but for the
6314 common case of monotonically increasing enumerators, it is
6315 O(N), since the nature of the splay tree will keep the next
6316 element adjacent to the root at all times. */
4ee9c684 6317
359d87c6 6318 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6319 {
6320 tree value = TREE_VALUE (chain);
3f00a6c0 6321 if (TREE_CODE (value) == CONST_DECL)
6322 value = DECL_INITIAL (value);
359d87c6 6323 node = splay_tree_lookup (cases, (splay_tree_key) value);
6324 if (node)
4ee9c684 6325 {
359d87c6 6326 /* Mark the CASE_LOW part of the case entry as seen. */
6327 tree label = (tree) node->value;
6328 CASE_LOW_SEEN (label) = 1;
6329 continue;
6330 }
6331
6332 /* Even though there wasn't an exact match, there might be a
f0b5f617 6333 case range which includes the enumerator's value. */
359d87c6 6334 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6335 if (node && CASE_HIGH ((tree) node->value))
6336 {
6337 tree label = (tree) node->value;
6338 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6339 if (cmp >= 0)
4ee9c684 6340 {
359d87c6 6341 /* If we match the upper bound exactly, mark the CASE_HIGH
6342 part of the case entry as seen. */
6343 if (cmp == 0)
6344 CASE_HIGH_SEEN (label) = 1;
6345 continue;
4ee9c684 6346 }
6347 }
6348
359d87c6 6349 /* We've now determined that this enumerated literal isn't
6350 handled by the case labels of the switch statement. */
4ee9c684 6351
359d87c6 6352 /* If the switch expression is a constant, we only really care
6353 about whether that constant is handled by the switch. */
6354 if (cond && tree_int_cst_compare (cond, value))
6355 continue;
4ee9c684 6356
6cbbbc89 6357 /* If there is a default_node, the only relevant option is
561017b5 6358 Wswitch-enum. Otherwise, if both are enabled then we prefer
6cbbbc89 6359 to warn using -Wswitch because -Wswitch is enabled by -Wall
6360 while -Wswitch-enum is explicit. */
561017b5 6361 warning_at (switch_location,
6362 (default_node || !warn_switch
6363 ? OPT_Wswitch_enum
6364 : OPT_Wswitch),
6365 "enumeration value %qE not handled in switch",
6366 TREE_PURPOSE (chain));
4ee9c684 6367 }
359d87c6 6368
6369 /* Warn if there are case expressions that don't correspond to
6370 enumerators. This can occur since C and C++ don't enforce
6371 type-checking of assignments to enumeration variables.
6372
6373 The time complexity here is now always O(N) worst case, since
6374 we should have marked both the lower bound and upper bound of
6375 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6376 above. This scan also resets those fields. */
6cbbbc89 6377
359d87c6 6378 splay_tree_foreach (cases, match_case_to_enum, type);
e41f0d80 6379}
6380
9dd48740 6381/* Finish an expression taking the address of LABEL (an
dda49785 6382 IDENTIFIER_NODE). Returns an expression for the address.
6383
6384 LOC is the location for the expression returned. */
d0a47c8d 6385
1cae46be 6386tree
dda49785 6387finish_label_address_expr (tree label, location_t loc)
d0a47c8d 6388{
6389 tree result;
6390
29438999 6391 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
d0a47c8d 6392
9dd48740 6393 if (label == error_mark_node)
6394 return error_mark_node;
6395
d0a47c8d 6396 label = lookup_label (label);
6397 if (label == NULL_TREE)
6398 result = null_pointer_node;
6399 else
6400 {
6401 TREE_USED (label) = 1;
6402 result = build1 (ADDR_EXPR, ptr_type_node, label);
344cd9b2 6403 /* The current function is not necessarily uninlinable.
d0a47c8d 6404 Computed gotos are incompatible with inlining, but the value
6405 here could be used only in a diagnostic, for example. */
dda49785 6406 protected_set_expr_location (result, loc);
d0a47c8d 6407 }
6408
6409 return result;
6410}
4f9a1c9b 6411\f
6412
6413/* Given a boolean expression ARG, return a tree representing an increment
6414 or decrement (as indicated by CODE) of ARG. The front end must check for
6415 invalid cases (e.g., decrement in C++). */
6416tree
1cae46be 6417boolean_increment (enum tree_code code, tree arg)
4f9a1c9b 6418{
6419 tree val;
69db191c 6420 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
c0f19401 6421
4f9a1c9b 6422 arg = stabilize_reference (arg);
6423 switch (code)
6424 {
6425 case PREINCREMENT_EXPR:
14ae0310 6426 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6427 break;
6428 case POSTINCREMENT_EXPR:
14ae0310 6429 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6430 arg = save_expr (arg);
14ae0310 6431 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6432 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6433 break;
6434 case PREDECREMENT_EXPR:
14ae0310 6435 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6436 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6437 break;
6438 case POSTDECREMENT_EXPR:
14ae0310 6439 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6440 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6441 arg = save_expr (arg);
14ae0310 6442 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6443 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6444 break;
6445 default:
231bd014 6446 gcc_unreachable ();
4f9a1c9b 6447 }
6448 TREE_SIDE_EFFECTS (val) = 1;
6449 return val;
6450}
76a6e674 6451\f
f3449a3c 6452/* Built-in macros for stddef.h and stdint.h, that require macros
6453 defined in this file. */
79cf3ec1 6454void
1cae46be 6455c_stddef_cpp_builtins(void)
1ed9d5f5 6456{
63994318 6457 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6458 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6459 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6460 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
36bccbfc 6461 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6462 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
b0726616 6463 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6464 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
f3449a3c 6465 if (SIG_ATOMIC_TYPE)
6466 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6467 if (INT8_TYPE)
6468 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6469 if (INT16_TYPE)
6470 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6471 if (INT32_TYPE)
6472 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6473 if (INT64_TYPE)
6474 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6475 if (UINT8_TYPE)
6476 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6477 if (UINT16_TYPE)
6478 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6479 if (UINT32_TYPE)
6480 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6481 if (UINT64_TYPE)
6482 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6483 if (INT_LEAST8_TYPE)
6484 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6485 if (INT_LEAST16_TYPE)
6486 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6487 if (INT_LEAST32_TYPE)
6488 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6489 if (INT_LEAST64_TYPE)
6490 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6491 if (UINT_LEAST8_TYPE)
6492 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6493 if (UINT_LEAST16_TYPE)
6494 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6495 if (UINT_LEAST32_TYPE)
6496 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6497 if (UINT_LEAST64_TYPE)
6498 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6499 if (INT_FAST8_TYPE)
6500 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6501 if (INT_FAST16_TYPE)
6502 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6503 if (INT_FAST32_TYPE)
6504 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6505 if (INT_FAST64_TYPE)
6506 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6507 if (UINT_FAST8_TYPE)
6508 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6509 if (UINT_FAST16_TYPE)
6510 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6511 if (UINT_FAST32_TYPE)
6512 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6513 if (UINT_FAST64_TYPE)
6514 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6515 if (INTPTR_TYPE)
6516 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6517 if (UINTPTR_TYPE)
6518 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
574006c3 6519}
6520
7d3b509a 6521static void
1cae46be 6522c_init_attributes (void)
7d3b509a 6523{
6524 /* Fill in the built_in_attributes array. */
7c446c95 6525#define DEF_ATTR_NULL_TREE(ENUM) \
7d3b509a 6526 built_in_attributes[(int) ENUM] = NULL_TREE;
7c446c95 6527#define DEF_ATTR_INT(ENUM, VALUE) \
ceb7b692 6528 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
c8010b80 6529#define DEF_ATTR_STRING(ENUM, VALUE) \
6530 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
7d3b509a 6531#define DEF_ATTR_IDENT(ENUM, STRING) \
6532 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6533#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6534 built_in_attributes[(int) ENUM] \
6535 = tree_cons (built_in_attributes[(int) PURPOSE], \
6536 built_in_attributes[(int) VALUE], \
6537 built_in_attributes[(int) CHAIN]);
7d3b509a 6538#include "builtin-attrs.def"
6539#undef DEF_ATTR_NULL_TREE
6540#undef DEF_ATTR_INT
6541#undef DEF_ATTR_IDENT
6542#undef DEF_ATTR_TREE_LIST
76a6e674 6543}
5f3cead1 6544
33199a81 6545/* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6546 identifier as an argument, so the front end shouldn't look it up. */
6547
6548bool
47b19b94 6549attribute_takes_identifier_p (const_tree attr_id)
33199a81 6550{
9bf1c74e 6551 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
02cb1060 6552 if (spec == NULL)
6553 /* Unknown attribute that we'll end up ignoring, return true so we
6554 don't complain about an identifier argument. */
6555 return true;
6556 else if (!strcmp ("mode", spec->name)
6557 || !strcmp ("format", spec->name)
6558 || !strcmp ("cleanup", spec->name))
47b19b94 6559 return true;
6560 else
6561 return targetm.attribute_takes_identifier_p (attr_id);
33199a81 6562}
6563
f8e93a2e 6564/* Attribute handlers common to C front ends. */
6565
6566/* Handle a "packed" attribute; arguments as in
6567 struct attribute_spec.handler. */
6568
6569static tree
9a03a746 6570handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
09347743 6571 int flags, bool *no_add_attrs)
f8e93a2e 6572{
f40175cb 6573 if (TYPE_P (*node))
f8e93a2e 6574 {
6575 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 6576 *node = build_variant_type_copy (*node);
f40175cb 6577 TYPE_PACKED (*node) = 1;
f8e93a2e 6578 }
6579 else if (TREE_CODE (*node) == FIELD_DECL)
c2ab04f9 6580 {
9fd767c5 6581 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6582 /* Still pack bitfields. */
6583 && ! DECL_INITIAL (*node))
c2ab04f9 6584 warning (OPT_Wattributes,
6585 "%qE attribute ignored for field of type %qT",
6586 name, TREE_TYPE (*node));
6587 else
6588 DECL_PACKED (*node) = 1;
6589 }
f8e93a2e 6590 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
f40175cb 6591 used for DECL_REGISTER. It wouldn't mean anything anyway.
6592 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6593 that changes what the typedef is typing. */
f8e93a2e 6594 else
6595 {
9b2d6d13 6596 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6597 *no_add_attrs = true;
6598 }
6599
6600 return NULL_TREE;
6601}
6602
6603/* Handle a "nocommon" attribute; arguments as in
6604 struct attribute_spec.handler. */
6605
6606static tree
1cae46be 6607handle_nocommon_attribute (tree *node, tree name,
9a03a746 6608 tree ARG_UNUSED (args),
6609 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6610{
f48c7f4a 6611 if (VAR_P (*node))
f8e93a2e 6612 DECL_COMMON (*node) = 0;
6613 else
6614 {
9b2d6d13 6615 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6616 *no_add_attrs = true;
6617 }
6618
6619 return NULL_TREE;
6620}
6621
6622/* Handle a "common" attribute; arguments as in
6623 struct attribute_spec.handler. */
6624
6625static tree
9a03a746 6626handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6627 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6628{
f48c7f4a 6629 if (VAR_P (*node))
f8e93a2e 6630 DECL_COMMON (*node) = 1;
6631 else
6632 {
9b2d6d13 6633 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6634 *no_add_attrs = true;
6635 }
6636
6637 return NULL_TREE;
6638}
6639
6640/* Handle a "noreturn" attribute; arguments as in
6641 struct attribute_spec.handler. */
6642
6643static tree
9a03a746 6644handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6645 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6646{
6647 tree type = TREE_TYPE (*node);
6648
6649 /* See FIXME comment in c_common_attribute_table. */
8c582e4f 6650 if (TREE_CODE (*node) == FUNCTION_DECL
6651 || objc_method_decl (TREE_CODE (*node)))
f8e93a2e 6652 TREE_THIS_VOLATILE (*node) = 1;
6653 else if (TREE_CODE (type) == POINTER_TYPE
6654 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6655 TREE_TYPE (*node)
9d4eeb52 6656 = (build_qualified_type
6657 (build_pointer_type
6658 (build_type_variant (TREE_TYPE (type),
6659 TYPE_READONLY (TREE_TYPE (type)), 1)),
6660 TYPE_QUALS (type)));
f8e93a2e 6661 else
6662 {
9b2d6d13 6663 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6664 *no_add_attrs = true;
6665 }
6666
6667 return NULL_TREE;
6668}
6669
5de92639 6670/* Handle a "hot" and attribute; arguments as in
6671 struct attribute_spec.handler. */
6672
6673static tree
6674handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
46f8e3b0 6675 int ARG_UNUSED (flags), bool *no_add_attrs)
5de92639 6676{
758a38ab 6677 if (TREE_CODE (*node) == FUNCTION_DECL
6678 || TREE_CODE (*node) == LABEL_DECL)
5de92639 6679 {
6680 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6681 {
4a026b48 6682 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6683 "with attribute %qs", name, "cold");
5de92639 6684 *no_add_attrs = true;
6685 }
24470055 6686 /* Most of the rest of the hot processing is done later with
6687 lookup_attribute. */
5de92639 6688 }
6689 else
6690 {
6691 warning (OPT_Wattributes, "%qE attribute ignored", name);
6692 *no_add_attrs = true;
6693 }
6694
6695 return NULL_TREE;
6696}
758a38ab 6697
5de92639 6698/* Handle a "cold" and attribute; arguments as in
6699 struct attribute_spec.handler. */
6700
6701static tree
6702handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6703 int ARG_UNUSED (flags), bool *no_add_attrs)
6704{
758a38ab 6705 if (TREE_CODE (*node) == FUNCTION_DECL
6706 || TREE_CODE (*node) == LABEL_DECL)
5de92639 6707 {
6708 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6709 {
4a026b48 6710 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6711 "with attribute %qs", name, "hot");
5de92639 6712 *no_add_attrs = true;
6713 }
24470055 6714 /* Most of the rest of the cold processing is done later with
6715 lookup_attribute. */
5de92639 6716 }
6717 else
6718 {
6719 warning (OPT_Wattributes, "%qE attribute ignored", name);
6720 *no_add_attrs = true;
6721 }
6722
6723 return NULL_TREE;
6724}
6725
a9196da9 6726/* Handle a "no_sanitize_address" attribute; arguments as in
d413ffdd 6727 struct attribute_spec.handler. */
6728
6729static tree
a9196da9 6730handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6731 bool *no_add_attrs)
d413ffdd 6732{
6733 if (TREE_CODE (*node) != FUNCTION_DECL)
6734 {
6735 warning (OPT_Wattributes, "%qE attribute ignored", name);
6736 *no_add_attrs = true;
6737 }
6738
6739 return NULL_TREE;
6740}
6741
a9196da9 6742/* Handle a "no_address_safety_analysis" attribute; arguments as in
6743 struct attribute_spec.handler. */
6744
6745static tree
6746handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6747 bool *no_add_attrs)
6748{
6749 if (TREE_CODE (*node) != FUNCTION_DECL)
6750 warning (OPT_Wattributes, "%qE attribute ignored", name);
6751 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6752 DECL_ATTRIBUTES (*node)
6753 = tree_cons (get_identifier ("no_sanitize_address"),
6754 NULL_TREE, DECL_ATTRIBUTES (*node));
6755 *no_add_attrs = true;
6756 return NULL_TREE;
6757}
6758
05f893e1 6759/* Handle a "no_sanitize_undefined" attribute; arguments as in
6760 struct attribute_spec.handler. */
6761
6762static tree
6763handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6764 bool *no_add_attrs)
6765{
6766 if (TREE_CODE (*node) != FUNCTION_DECL)
6767 {
6768 warning (OPT_Wattributes, "%qE attribute ignored", name);
6769 *no_add_attrs = true;
6770 }
6771
6772 return NULL_TREE;
6773}
6774
947aa916 6775/* Handle a "stack_protect" attribute; arguments as in
6776 struct attribute_spec.handler. */
6777static tree
6778handle_stack_protect_attribute (tree *node, tree name, tree, int,
6779 bool *no_add_attrs)
6780{
6781 if (TREE_CODE (*node) != FUNCTION_DECL)
6782 {
6783 warning (OPT_Wattributes, "%qE attribute ignored", name);
6784 *no_add_attrs = true;
6785 }
6786 else
6787 DECL_ATTRIBUTES (*node)
6788 = tree_cons (get_identifier ("stack_protect"),
6789 NULL_TREE, DECL_ATTRIBUTES (*node));
6790
6791 return NULL_TREE;
6792}
6793
f8e93a2e 6794/* Handle a "noinline" attribute; arguments as in
6795 struct attribute_spec.handler. */
6796
6797static tree
1cae46be 6798handle_noinline_attribute (tree *node, tree name,
9a03a746 6799 tree ARG_UNUSED (args),
6800 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6801{
6802 if (TREE_CODE (*node) == FUNCTION_DECL)
4a026b48 6803 {
6804 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6805 {
6806 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6807 "with attribute %qs", name, "always_inline");
6808 *no_add_attrs = true;
6809 }
6810 else
6811 DECL_UNINLINABLE (*node) = 1;
6812 }
f8e93a2e 6813 else
6814 {
9b2d6d13 6815 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6816 *no_add_attrs = true;
6817 }
6818
6819 return NULL_TREE;
6820}
6821
bdb1f0d1 6822/* Handle a "noclone" attribute; arguments as in
6823 struct attribute_spec.handler. */
6824
6825static tree
6826handle_noclone_attribute (tree *node, tree name,
6827 tree ARG_UNUSED (args),
6828 int ARG_UNUSED (flags), bool *no_add_attrs)
6829{
6830 if (TREE_CODE (*node) != FUNCTION_DECL)
6831 {
6832 warning (OPT_Wattributes, "%qE attribute ignored", name);
6833 *no_add_attrs = true;
6834 }
6835
6836 return NULL_TREE;
6837}
6838
85fbea97 6839/* Handle a "no_icf" attribute; arguments as in
6840 struct attribute_spec.handler. */
6841
6842static tree
6843handle_noicf_attribute (tree *node, tree name,
6844 tree ARG_UNUSED (args),
6845 int ARG_UNUSED (flags), bool *no_add_attrs)
6846{
6847 if (TREE_CODE (*node) != FUNCTION_DECL)
6848 {
6849 warning (OPT_Wattributes, "%qE attribute ignored", name);
6850 *no_add_attrs = true;
6851 }
6852
6853 return NULL_TREE;
6854}
6855
6856
f8e93a2e 6857/* Handle a "always_inline" attribute; arguments as in
6858 struct attribute_spec.handler. */
6859
6860static tree
1cae46be 6861handle_always_inline_attribute (tree *node, tree name,
9a03a746 6862 tree ARG_UNUSED (args),
6863 int ARG_UNUSED (flags),
09347743 6864 bool *no_add_attrs)
f8e93a2e 6865{
6866 if (TREE_CODE (*node) == FUNCTION_DECL)
6867 {
4a026b48 6868 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6869 {
6870 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6871 "with %qs attribute", name, "noinline");
6872 *no_add_attrs = true;
6873 }
ab50af2a 6874 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
6875 {
6876 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6877 "with %qs attribute", name, "target_clones");
6878 *no_add_attrs = true;
6879 }
4a026b48 6880 else
6881 /* Set the attribute and mark it for disregarding inline
6882 limits. */
6883 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
f8e93a2e 6884 }
6885 else
6886 {
9b2d6d13 6887 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6888 *no_add_attrs = true;
541e4101 6889 }
6890
6891 return NULL_TREE;
6892}
6893
6894/* Handle a "gnu_inline" attribute; arguments as in
6895 struct attribute_spec.handler. */
6896
6897static tree
6898handle_gnu_inline_attribute (tree *node, tree name,
6899 tree ARG_UNUSED (args),
6900 int ARG_UNUSED (flags),
6901 bool *no_add_attrs)
6902{
6903 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6904 {
6905 /* Do nothing else, just set the attribute. We'll get at
6906 it later with lookup_attribute. */
6907 }
6908 else
6909 {
6910 warning (OPT_Wattributes, "%qE attribute ignored", name);
6911 *no_add_attrs = true;
7bd95dfd 6912 }
6913
6914 return NULL_TREE;
6915}
6916
6917/* Handle a "leaf" attribute; arguments as in
6918 struct attribute_spec.handler. */
6919
6920static tree
6921handle_leaf_attribute (tree *node, tree name,
6922 tree ARG_UNUSED (args),
6923 int ARG_UNUSED (flags), bool *no_add_attrs)
6924{
6925 if (TREE_CODE (*node) != FUNCTION_DECL)
6926 {
6927 warning (OPT_Wattributes, "%qE attribute ignored", name);
6928 *no_add_attrs = true;
6929 }
6930 if (!TREE_PUBLIC (*node))
6931 {
6932 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6933 *no_add_attrs = true;
f8e93a2e 6934 }
6935
6936 return NULL_TREE;
6937}
6938
1b16fc45 6939/* Handle an "artificial" attribute; arguments as in
6940 struct attribute_spec.handler. */
6941
6942static tree
6943handle_artificial_attribute (tree *node, tree name,
6944 tree ARG_UNUSED (args),
6945 int ARG_UNUSED (flags),
6946 bool *no_add_attrs)
6947{
6948 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6949 {
6950 /* Do nothing else, just set the attribute. We'll get at
6951 it later with lookup_attribute. */
6952 }
6953 else
6954 {
6955 warning (OPT_Wattributes, "%qE attribute ignored", name);
6956 *no_add_attrs = true;
6957 }
6958
6959 return NULL_TREE;
6960}
6961
0cdd9887 6962/* Handle a "flatten" attribute; arguments as in
6963 struct attribute_spec.handler. */
6964
6965static tree
6966handle_flatten_attribute (tree *node, tree name,
a0c938f0 6967 tree args ATTRIBUTE_UNUSED,
6968 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
0cdd9887 6969{
6970 if (TREE_CODE (*node) == FUNCTION_DECL)
6971 /* Do nothing else, just set the attribute. We'll get at
6972 it later with lookup_attribute. */
6973 ;
6974 else
6975 {
6976 warning (OPT_Wattributes, "%qE attribute ignored", name);
6977 *no_add_attrs = true;
6978 }
6979
6980 return NULL_TREE;
6981}
6982
10fc867f 6983/* Handle a "warning" or "error" attribute; arguments as in
6984 struct attribute_spec.handler. */
6985
6986static tree
6987handle_error_attribute (tree *node, tree name, tree args,
6988 int ARG_UNUSED (flags), bool *no_add_attrs)
6989{
6990 if (TREE_CODE (*node) == FUNCTION_DECL
df936998 6991 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
10fc867f 6992 /* Do nothing else, just set the attribute. We'll get at
6993 it later with lookup_attribute. */
6994 ;
6995 else
6996 {
6997 warning (OPT_Wattributes, "%qE attribute ignored", name);
6998 *no_add_attrs = true;
6999 }
7000
7001 return NULL_TREE;
7002}
0cdd9887 7003
f8e93a2e 7004/* Handle a "used" attribute; arguments as in
7005 struct attribute_spec.handler. */
7006
7007static tree
9a03a746 7008handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7009 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7010{
d0a31bd8 7011 tree node = *pnode;
7012
7013 if (TREE_CODE (node) == FUNCTION_DECL
f48c7f4a 7014 || (VAR_P (node) && TREE_STATIC (node))
a4e3ffad 7015 || (TREE_CODE (node) == TYPE_DECL))
f54ed8bc 7016 {
f54ed8bc 7017 TREE_USED (node) = 1;
9423c9b7 7018 DECL_PRESERVE_P (node) = 1;
f48c7f4a 7019 if (VAR_P (node))
abc6c64f 7020 DECL_READ_P (node) = 1;
f54ed8bc 7021 }
f8e93a2e 7022 else
7023 {
9b2d6d13 7024 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7025 *no_add_attrs = true;
7026 }
7027
7028 return NULL_TREE;
7029}
7030
7031/* Handle a "unused" attribute; arguments as in
7032 struct attribute_spec.handler. */
7033
f5d49c14 7034tree
9a03a746 7035handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7036 int flags, bool *no_add_attrs)
f8e93a2e 7037{
7038 if (DECL_P (*node))
7039 {
7040 tree decl = *node;
7041
7042 if (TREE_CODE (decl) == PARM_DECL
4e81b384 7043 || VAR_OR_FUNCTION_DECL_P (decl)
f8e93a2e 7044 || TREE_CODE (decl) == LABEL_DECL
f5d49c14 7045 || TREE_CODE (decl) == CONST_DECL
f8e93a2e 7046 || TREE_CODE (decl) == TYPE_DECL)
abc6c64f 7047 {
7048 TREE_USED (decl) = 1;
f48c7f4a 7049 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
abc6c64f 7050 DECL_READ_P (decl) = 1;
7051 }
f8e93a2e 7052 else
7053 {
9b2d6d13 7054 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7055 *no_add_attrs = true;
7056 }
7057 }
7058 else
7059 {
7060 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 7061 *node = build_variant_type_copy (*node);
f8e93a2e 7062 TREE_USED (*node) = 1;
7063 }
7064
7065 return NULL_TREE;
7066}
7067
62eec3b4 7068/* Handle a "externally_visible" attribute; arguments as in
7069 struct attribute_spec.handler. */
7070
7071static tree
7072handle_externally_visible_attribute (tree *pnode, tree name,
7073 tree ARG_UNUSED (args),
7074 int ARG_UNUSED (flags),
7075 bool *no_add_attrs)
7076{
7077 tree node = *pnode;
7078
b443c459 7079 if (VAR_OR_FUNCTION_DECL_P (node))
62eec3b4 7080 {
ba12ea31 7081 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7082 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7083 {
7084 warning (OPT_Wattributes,
7085 "%qE attribute have effect only on public objects", name);
7086 *no_add_attrs = true;
7087 }
62eec3b4 7088 }
62eec3b4 7089 else
7090 {
7091 warning (OPT_Wattributes, "%qE attribute ignored", name);
7092 *no_add_attrs = true;
7093 }
7094
7095 return NULL_TREE;
7096}
7097
6b722052 7098/* Handle the "no_reorder" attribute. Arguments as in
7099 struct attribute_spec.handler. */
7100
7101static tree
7102handle_no_reorder_attribute (tree *pnode,
7103 tree name,
7104 tree,
7105 int,
7106 bool *no_add_attrs)
7107{
7108 tree node = *pnode;
7109
b443c459 7110 if (!VAR_OR_FUNCTION_DECL_P (node)
6b722052 7111 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7112 {
7113 warning (OPT_Wattributes,
7114 "%qE attribute only affects top level objects",
7115 name);
7116 *no_add_attrs = true;
7117 }
7118
7119 return NULL_TREE;
7120}
7121
f8e93a2e 7122/* Handle a "const" attribute; arguments as in
7123 struct attribute_spec.handler. */
7124
7125static tree
9a03a746 7126handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7127 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7128{
7129 tree type = TREE_TYPE (*node);
7130
7131 /* See FIXME comment on noreturn in c_common_attribute_table. */
7132 if (TREE_CODE (*node) == FUNCTION_DECL)
7133 TREE_READONLY (*node) = 1;
7134 else if (TREE_CODE (type) == POINTER_TYPE
7135 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7136 TREE_TYPE (*node)
9d4eeb52 7137 = (build_qualified_type
7138 (build_pointer_type
7139 (build_type_variant (TREE_TYPE (type), 1,
7140 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7141 TYPE_QUALS (type)));
f8e93a2e 7142 else
7143 {
9b2d6d13 7144 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7145 *no_add_attrs = true;
7146 }
7147
7148 return NULL_TREE;
7149}
7150
292237f3 7151/* Handle a "scalar_storage_order" attribute; arguments as in
7152 struct attribute_spec.handler. */
7153
7154static tree
7155handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
7156 int flags, bool *no_add_attrs)
7157{
7158 tree id = TREE_VALUE (args);
7159 tree type;
7160
7161 if (TREE_CODE (*node) == TYPE_DECL
7162 && ! (flags & ATTR_FLAG_CXX11))
7163 node = &TREE_TYPE (*node);
7164 type = *node;
7165
7166 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
7167 {
7168 error ("scalar_storage_order is not supported because endianness "
7169 "is not uniform");
7170 return NULL_TREE;
7171 }
7172
7173 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
7174 {
7175 bool reverse = false;
7176
7177 if (TREE_CODE (id) == STRING_CST
7178 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
7179 reverse = !BYTES_BIG_ENDIAN;
7180 else if (TREE_CODE (id) == STRING_CST
7181 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
7182 reverse = BYTES_BIG_ENDIAN;
7183 else
7184 {
7185 error ("scalar_storage_order argument must be one of \"big-endian\""
7186 " or \"little-endian\"");
7187 return NULL_TREE;
7188 }
7189
7190 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7191 {
7192 if (reverse)
7193 /* A type variant isn't good enough, since we don't want a cast
7194 to such a type to be removed as a no-op. */
7195 *node = type = build_duplicate_type (type);
7196 }
7197
7198 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
7199 return NULL_TREE;
7200 }
7201
7202 warning (OPT_Wattributes, "%qE attribute ignored", name);
7203 *no_add_attrs = true;
7204 return NULL_TREE;
7205}
7206
f8e93a2e 7207/* Handle a "transparent_union" attribute; arguments as in
7208 struct attribute_spec.handler. */
7209
7210static tree
1cae46be 7211handle_transparent_union_attribute (tree *node, tree name,
9a03a746 7212 tree ARG_UNUSED (args), int flags,
09347743 7213 bool *no_add_attrs)
f8e93a2e 7214{
881eb642 7215 tree type;
03908818 7216
7217 *no_add_attrs = true;
f8e93a2e 7218
ffcdbf9c 7219 if (TREE_CODE (*node) == TYPE_DECL
7220 && ! (flags & ATTR_FLAG_CXX11))
881eb642 7221 node = &TREE_TYPE (*node);
7222 type = *node;
f8e93a2e 7223
03908818 7224 if (TREE_CODE (type) == UNION_TYPE)
f8e93a2e 7225 {
fca86134 7226 /* Make sure that the first field will work for a transparent union.
7227 If the type isn't complete yet, leave the check to the code in
7228 finish_struct. */
7229 if (TYPE_SIZE (type))
7230 {
7231 tree first = first_field (type);
7232 if (first == NULL_TREE
7233 || DECL_ARTIFICIAL (first)
7234 || TYPE_MODE (type) != DECL_MODE (first))
7235 goto ignored;
7236 }
7237
f8e93a2e 7238 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
03908818 7239 {
fca86134 7240 /* If the type isn't complete yet, setting the flag
7241 on a variant wouldn't ever be checked. */
7242 if (!TYPE_SIZE (type))
7243 goto ignored;
7244
7245 /* build_duplicate_type doesn't work for C++. */
7246 if (c_dialect_cxx ())
03908818 7247 goto ignored;
7248
292237f3 7249 /* A type variant isn't good enough, since we don't want a cast
7250 to such a type to be removed as a no-op. */
03908818 7251 *node = type = build_duplicate_type (type);
7252 }
7253
c25b3e3f 7254 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7255 TYPE_TRANSPARENT_AGGR (t) = 1;
03908818 7256 return NULL_TREE;
f8e93a2e 7257 }
7258
03908818 7259 ignored:
7260 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7261 return NULL_TREE;
7262}
7263
9af7fd5b 7264/* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7265 get the requested priority for a constructor or destructor,
7266 possibly issuing diagnostics for invalid or reserved
7267 priorities. */
7268
7269static priority_type
7270get_priority (tree args, bool is_destructor)
7271{
7272 HOST_WIDE_INT pri;
6c181a06 7273 tree arg;
9af7fd5b 7274
7275 if (!args)
7276 return DEFAULT_INIT_PRIORITY;
48e1416a 7277
28fbc04f 7278 if (!SUPPORTS_INIT_PRIORITY)
7279 {
7280 if (is_destructor)
7281 error ("destructor priorities are not supported");
7282 else
7283 error ("constructor priorities are not supported");
7284 return DEFAULT_INIT_PRIORITY;
7285 }
7286
6c181a06 7287 arg = TREE_VALUE (args);
253e1cae 7288 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7289 goto invalid;
7290 if (arg == error_mark_node)
7291 return DEFAULT_INIT_PRIORITY;
c28ddc97 7292 arg = default_conversion (arg);
e913b5cd 7293 if (!tree_fits_shwi_p (arg)
6c181a06 7294 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
9af7fd5b 7295 goto invalid;
7296
e913b5cd 7297 pri = tree_to_shwi (arg);
9af7fd5b 7298 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7299 goto invalid;
7300
7301 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7302 {
7303 if (is_destructor)
7304 warning (0,
7305 "destructor priorities from 0 to %d are reserved "
48e1416a 7306 "for the implementation",
9af7fd5b 7307 MAX_RESERVED_INIT_PRIORITY);
7308 else
7309 warning (0,
7310 "constructor priorities from 0 to %d are reserved "
48e1416a 7311 "for the implementation",
9af7fd5b 7312 MAX_RESERVED_INIT_PRIORITY);
7313 }
7314 return pri;
7315
7316 invalid:
7317 if (is_destructor)
7318 error ("destructor priorities must be integers from 0 to %d inclusive",
7319 MAX_INIT_PRIORITY);
7320 else
7321 error ("constructor priorities must be integers from 0 to %d inclusive",
7322 MAX_INIT_PRIORITY);
7323 return DEFAULT_INIT_PRIORITY;
7324}
7325
f8e93a2e 7326/* Handle a "constructor" attribute; arguments as in
7327 struct attribute_spec.handler. */
7328
7329static tree
9af7fd5b 7330handle_constructor_attribute (tree *node, tree name, tree args,
9a03a746 7331 int ARG_UNUSED (flags),
09347743 7332 bool *no_add_attrs)
f8e93a2e 7333{
7334 tree decl = *node;
7335 tree type = TREE_TYPE (decl);
7336
7337 if (TREE_CODE (decl) == FUNCTION_DECL
7338 && TREE_CODE (type) == FUNCTION_TYPE
7339 && decl_function_context (decl) == 0)
7340 {
9af7fd5b 7341 priority_type priority;
f8e93a2e 7342 DECL_STATIC_CONSTRUCTOR (decl) = 1;
9af7fd5b 7343 priority = get_priority (args, /*is_destructor=*/false);
7344 SET_DECL_INIT_PRIORITY (decl, priority);
f8e93a2e 7345 TREE_USED (decl) = 1;
7346 }
7347 else
7348 {
9b2d6d13 7349 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7350 *no_add_attrs = true;
7351 }
7352
7353 return NULL_TREE;
7354}
7355
7356/* Handle a "destructor" attribute; arguments as in
7357 struct attribute_spec.handler. */
7358
7359static tree
9af7fd5b 7360handle_destructor_attribute (tree *node, tree name, tree args,
9a03a746 7361 int ARG_UNUSED (flags),
09347743 7362 bool *no_add_attrs)
f8e93a2e 7363{
7364 tree decl = *node;
7365 tree type = TREE_TYPE (decl);
7366
7367 if (TREE_CODE (decl) == FUNCTION_DECL
7368 && TREE_CODE (type) == FUNCTION_TYPE
7369 && decl_function_context (decl) == 0)
7370 {
9af7fd5b 7371 priority_type priority;
f8e93a2e 7372 DECL_STATIC_DESTRUCTOR (decl) = 1;
9af7fd5b 7373 priority = get_priority (args, /*is_destructor=*/true);
7374 SET_DECL_FINI_PRIORITY (decl, priority);
f8e93a2e 7375 TREE_USED (decl) = 1;
7376 }
7377 else
7378 {
9b2d6d13 7379 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7380 *no_add_attrs = true;
7381 }
7382
7383 return NULL_TREE;
7384}
7385
1c58e3f1 7386/* Nonzero if the mode is a valid vector mode for this architecture.
7387 This returns nonzero even if there is no hardware support for the
7388 vector mode, but we can emulate with narrower modes. */
7389
7390static int
3754d046 7391vector_mode_valid_p (machine_mode mode)
1c58e3f1 7392{
7393 enum mode_class mclass = GET_MODE_CLASS (mode);
3754d046 7394 machine_mode innermode;
1c58e3f1 7395
7396 /* Doh! What's going on? */
7397 if (mclass != MODE_VECTOR_INT
7398 && mclass != MODE_VECTOR_FLOAT
7399 && mclass != MODE_VECTOR_FRACT
7400 && mclass != MODE_VECTOR_UFRACT
7401 && mclass != MODE_VECTOR_ACCUM
7402 && mclass != MODE_VECTOR_UACCUM)
7403 return 0;
7404
7405 /* Hardware support. Woo hoo! */
7406 if (targetm.vector_mode_supported_p (mode))
7407 return 1;
7408
7409 innermode = GET_MODE_INNER (mode);
7410
7411 /* We should probably return 1 if requesting V4DI and we have no DI,
7412 but we have V2DI, but this is probably very unlikely. */
7413
7414 /* If we have support for the inner mode, we can safely emulate it.
7415 We may not have V2DI, but me can emulate with a pair of DIs. */
7416 return targetm.scalar_mode_supported_p (innermode);
7417}
7418
7419
f8e93a2e 7420/* Handle a "mode" attribute; arguments as in
7421 struct attribute_spec.handler. */
7422
7423static tree
9a03a746 7424handle_mode_attribute (tree *node, tree name, tree args,
7425 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7426{
7427 tree type = *node;
d1dd9ac0 7428 tree ident = TREE_VALUE (args);
f8e93a2e 7429
7430 *no_add_attrs = true;
ab2c1de8 7431
d1dd9ac0 7432 if (TREE_CODE (ident) != IDENTIFIER_NODE)
9b2d6d13 7433 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7434 else
7435 {
7436 int j;
d1dd9ac0 7437 const char *p = IDENTIFIER_POINTER (ident);
f8e93a2e 7438 int len = strlen (p);
3754d046 7439 machine_mode mode = VOIDmode;
f8e93a2e 7440 tree typefm;
b2aef146 7441 bool valid_mode;
f8e93a2e 7442
7443 if (len > 4 && p[0] == '_' && p[1] == '_'
7444 && p[len - 1] == '_' && p[len - 2] == '_')
7445 {
4fd61bc6 7446 char *newp = (char *) alloca (len - 1);
f8e93a2e 7447
7448 strcpy (newp, &p[2]);
7449 newp[len - 4] = '\0';
7450 p = newp;
7451 }
7452
7453 /* Change this type to have a type with the specified mode.
7454 First check for the special modes. */
84166705 7455 if (!strcmp (p, "byte"))
f8e93a2e 7456 mode = byte_mode;
7457 else if (!strcmp (p, "word"))
7458 mode = word_mode;
84166705 7459 else if (!strcmp (p, "pointer"))
f8e93a2e 7460 mode = ptr_mode;
0ef89dfd 7461 else if (!strcmp (p, "libgcc_cmp_return"))
7462 mode = targetm.libgcc_cmp_return_mode ();
7463 else if (!strcmp (p, "libgcc_shift_count"))
7464 mode = targetm.libgcc_shift_count_mode ();
1bd43494 7465 else if (!strcmp (p, "unwind_word"))
7466 mode = targetm.unwind_word_mode ();
f8e93a2e 7467 else
7468 for (j = 0; j < NUM_MACHINE_MODES; j++)
7469 if (!strcmp (p, GET_MODE_NAME (j)))
743a6f47 7470 {
3754d046 7471 mode = (machine_mode) j;
743a6f47 7472 break;
7473 }
f8e93a2e 7474
7475 if (mode == VOIDmode)
4917c376 7476 {
d1dd9ac0 7477 error ("unknown machine mode %qE", ident);
4917c376 7478 return NULL_TREE;
7479 }
7480
b2aef146 7481 valid_mode = false;
7482 switch (GET_MODE_CLASS (mode))
4917c376 7483 {
b2aef146 7484 case MODE_INT:
7485 case MODE_PARTIAL_INT:
7486 case MODE_FLOAT:
c4503c0a 7487 case MODE_DECIMAL_FLOAT:
9421ebb9 7488 case MODE_FRACT:
7489 case MODE_UFRACT:
7490 case MODE_ACCUM:
7491 case MODE_UACCUM:
b2aef146 7492 valid_mode = targetm.scalar_mode_supported_p (mode);
7493 break;
7494
7495 case MODE_COMPLEX_INT:
7496 case MODE_COMPLEX_FLOAT:
7497 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7498 break;
7499
7500 case MODE_VECTOR_INT:
7501 case MODE_VECTOR_FLOAT:
9421ebb9 7502 case MODE_VECTOR_FRACT:
7503 case MODE_VECTOR_UFRACT:
7504 case MODE_VECTOR_ACCUM:
7505 case MODE_VECTOR_UACCUM:
9b2d6d13 7506 warning (OPT_Wattributes, "specifying vector types with "
7507 "__attribute__ ((mode)) is deprecated");
7508 warning (OPT_Wattributes,
7509 "use __attribute__ ((vector_size)) instead");
b2aef146 7510 valid_mode = vector_mode_valid_p (mode);
7511 break;
4917c376 7512
b2aef146 7513 default:
7514 break;
7515 }
7516 if (!valid_mode)
7517 {
1e5fcbe2 7518 error ("unable to emulate %qs", p);
b2aef146 7519 return NULL_TREE;
7520 }
4917c376 7521
b2aef146 7522 if (POINTER_TYPE_P (type))
ead34f59 7523 {
6d5d708e 7524 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
3754d046 7525 tree (*fn)(tree, machine_mode, bool);
b2aef146 7526
6d5d708e 7527 if (!targetm.addr_space.valid_pointer_mode (mode, as))
ead34f59 7528 {
1e5fcbe2 7529 error ("invalid pointer mode %qs", p);
ead34f59 7530 return NULL_TREE;
7531 }
7532
a0c938f0 7533 if (TREE_CODE (type) == POINTER_TYPE)
b2aef146 7534 fn = build_pointer_type_for_mode;
805e22b2 7535 else
b2aef146 7536 fn = build_reference_type_for_mode;
7537 typefm = fn (TREE_TYPE (type), mode, false);
ead34f59 7538 }
b2aef146 7539 else
9421ebb9 7540 {
7541 /* For fixed-point modes, we need to test if the signness of type
7542 and the machine mode are consistent. */
7543 if (ALL_FIXED_POINT_MODE_P (mode)
7544 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7545 {
bf776685 7546 error ("signedness of type and machine mode %qs don%'t match", p);
9421ebb9 7547 return NULL_TREE;
7548 }
7549 /* For fixed-point modes, we need to pass saturating info. */
7550 typefm = lang_hooks.types.type_for_mode (mode,
7551 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7552 : TYPE_UNSIGNED (type));
7553 }
3a648ab9 7554
b2aef146 7555 if (typefm == NULL_TREE)
7556 {
743a6f47 7557 error ("no data type for mode %qs", p);
b2aef146 7558 return NULL_TREE;
7559 }
3a648ab9 7560 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7561 {
7562 /* For enumeral types, copy the precision from the integer
7563 type returned above. If not an INTEGER_TYPE, we can't use
7564 this mode for this type. */
7565 if (TREE_CODE (typefm) != INTEGER_TYPE)
7566 {
743a6f47 7567 error ("cannot use mode %qs for enumeral types", p);
3a648ab9 7568 return NULL_TREE;
7569 }
7570
10080eac 7571 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7572 {
7573 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7574 typefm = type;
7575 }
7576 else
7577 {
7578 /* We cannot build a type variant, as there's code that assumes
7579 that TYPE_MAIN_VARIANT has the same mode. This includes the
7580 debug generators. Instead, create a subrange type. This
7581 results in all of the enumeral values being emitted only once
7582 in the original, and the subtype gets them by reference. */
7583 if (TYPE_UNSIGNED (type))
7584 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7585 else
7586 typefm = make_signed_type (TYPE_PRECISION (typefm));
7587 TREE_TYPE (typefm) = type;
7588 }
3a648ab9 7589 }
4bf450a1 7590 else if (VECTOR_MODE_P (mode)
7591 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7592 : TREE_CODE (type) != TREE_CODE (typefm))
743a6f47 7593 {
7594 error ("mode %qs applied to inappropriate type", p);
7595 return NULL_TREE;
7596 }
7597
b2aef146 7598 *node = typefm;
f8e93a2e 7599 }
7600
7601 return NULL_TREE;
7602}
7603
7604/* Handle a "section" attribute; arguments as in
7605 struct attribute_spec.handler. */
7606
7607static tree
9a03a746 7608handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7609 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7610{
7611 tree decl = *node;
7612
9866562d 7613 if (!targetm_common.have_named_sections)
f8e93a2e 7614 {
9866562d 7615 error_at (DECL_SOURCE_LOCATION (*node),
7616 "section attributes are not supported for this target");
7617 goto fail;
7618 }
065efcb1 7619
9866562d 7620 user_defined_section_attribute = true;
f8e93a2e 7621
b443c459 7622 if (!VAR_OR_FUNCTION_DECL_P (decl))
9866562d 7623 {
7624 error ("section attribute not allowed for %q+D", *node);
7625 goto fail;
f8e93a2e 7626 }
9866562d 7627
7628 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
f8e93a2e 7629 {
9866562d 7630 error ("section attribute argument not a string constant");
7631 goto fail;
f8e93a2e 7632 }
7633
f48c7f4a 7634 if (VAR_P (decl)
9866562d 7635 && current_function_decl != NULL_TREE
7636 && !TREE_STATIC (decl))
7637 {
7638 error_at (DECL_SOURCE_LOCATION (decl),
7639 "section attribute cannot be specified for local variables");
7640 goto fail;
7641 }
7642
7643 /* The decl may have already been given a section attribute
7644 from a previous declaration. Ensure they match. */
7645 if (DECL_SECTION_NAME (decl) != NULL
7646 && strcmp (DECL_SECTION_NAME (decl),
7647 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7648 {
7649 error ("section of %q+D conflicts with previous declaration", *node);
7650 goto fail;
7651 }
7652
f48c7f4a 7653 if (VAR_P (decl)
9866562d 7654 && !targetm.have_tls && targetm.emutls.tmpl_section
7655 && DECL_THREAD_LOCAL_P (decl))
7656 {
7657 error ("section of %q+D cannot be overridden", *node);
7658 goto fail;
7659 }
7660
7661 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
7662 return NULL_TREE;
7663
7664fail:
7665 *no_add_attrs = true;
f8e93a2e 7666 return NULL_TREE;
7667}
7668
83e25171 7669/* Check whether ALIGN is a valid user-specified alignment. If so,
7670 return its base-2 log; if not, output an error and return -1. If
7671 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7672 no error. */
7673int
7674check_user_alignment (const_tree align, bool allow_zero)
7675{
7676 int i;
7677
3e5a8b00 7678 if (error_operand_p (align))
7679 return -1;
5abaa10a 7680 if (TREE_CODE (align) != INTEGER_CST
7681 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
83e25171 7682 {
7683 error ("requested alignment is not an integer constant");
7684 return -1;
7685 }
7686 else if (allow_zero && integer_zerop (align))
7687 return -1;
1a087624 7688 else if (tree_int_cst_sgn (align) == -1
7689 || (i = tree_log2 (align)) == -1)
83e25171 7690 {
1a087624 7691 error ("requested alignment is not a positive power of 2");
83e25171 7692 return -1;
7693 }
7694 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7695 {
7696 error ("requested alignment is too large");
7697 return -1;
7698 }
7699 return i;
7700}
7701
ffcdbf9c 7702/*
7703 If in c++-11, check if the c++-11 alignment constraint with respect
7704 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7705 c++-11 mode, does nothing.
7706
7707 [dcl.align]2/ says:
7708
7709 [* if the constant expression evaluates to a fundamental alignment,
7710 the alignment requirement of the declared entity shall be the
7711 specified fundamental alignment.
7712
7713 * if the constant expression evaluates to an extended alignment
7714 and the implementation supports that alignment in the context
7715 of the declaration, the alignment of the declared entity shall
7716 be that alignment
7717
7718 * if the constant expression evaluates to an extended alignment
7719 and the implementation does not support that alignment in the
7720 context of the declaration, the program is ill-formed]. */
7721
7722static bool
7723check_cxx_fundamental_alignment_constraints (tree node,
7724 unsigned align_log,
7725 int flags)
7726{
7727 bool alignment_too_large_p = false;
7728 unsigned requested_alignment = 1U << align_log;
7729 unsigned max_align = 0;
7730
7731 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7732 || (node == NULL_TREE || node == error_mark_node))
7733 return true;
7734
7735 if (cxx_fundamental_alignment_p (requested_alignment))
7736 return true;
7737
7738 if (DECL_P (node))
7739 {
7740 if (TREE_STATIC (node))
7741 {
7742 /* For file scope variables and static members, the target
7743 supports alignments that are at most
7744 MAX_OFILE_ALIGNMENT. */
7745 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7746 alignment_too_large_p = true;
7747 }
7748 else
7749 {
7750#ifdef BIGGEST_FIELD_ALIGNMENT
7751#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7752#else
7753#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7754#endif
7755 /* For non-static members, the target supports either
7756 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7757 if it is defined or BIGGEST_ALIGNMENT. */
7758 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7759 if (TREE_CODE (node) == FIELD_DECL
7760 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7761 alignment_too_large_p = true;
7762#undef MAX_TARGET_FIELD_ALIGNMENT
7763 /* For stack variables, the target supports at most
7764 MAX_STACK_ALIGNMENT. */
7765 else if (decl_function_context (node) != NULL
7766 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7767 alignment_too_large_p = true;
7768 }
7769 }
7770 else if (TYPE_P (node))
7771 {
7772 /* Let's be liberal for types. */
7773 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7774 alignment_too_large_p = true;
7775 }
7776
7777 if (alignment_too_large_p)
7778 pedwarn (input_location, OPT_Wattributes,
7779 "requested alignment %d is larger than %d",
7780 requested_alignment, max_align);
7781
7782 return !alignment_too_large_p;
7783}
7784
f8e93a2e 7785/* Handle a "aligned" attribute; arguments as in
7786 struct attribute_spec.handler. */
7787
7788static tree
9a03a746 7789handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
09347743 7790 int flags, bool *no_add_attrs)
f8e93a2e 7791{
7792 tree decl = NULL_TREE;
7793 tree *type = NULL;
7794 int is_type = 0;
caf62483 7795 tree align_expr;
f8e93a2e 7796 int i;
7797
caf62483 7798 if (args)
7799 {
7800 align_expr = TREE_VALUE (args);
3e5a8b00 7801 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7802 && TREE_CODE (align_expr) != FUNCTION_DECL)
caf62483 7803 align_expr = default_conversion (align_expr);
7804 }
7805 else
7806 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7807
f8e93a2e 7808 if (DECL_P (*node))
7809 {
7810 decl = *node;
7811 type = &TREE_TYPE (decl);
7812 is_type = TREE_CODE (*node) == TYPE_DECL;
7813 }
7814 else if (TYPE_P (*node))
7815 type = node, is_type = 1;
7816
eb16928e 7817 if ((i = check_user_alignment (align_expr, true)) == -1
ffcdbf9c 7818 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
83e25171 7819 *no_add_attrs = true;
f8e93a2e 7820 else if (is_type)
7821 {
2ec3af9c 7822 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7823 /* OK, modify the type in place. */;
f8e93a2e 7824 /* If we have a TYPE_DECL, then copy the type, so that we
7825 don't accidentally modify a builtin type. See pushdecl. */
2ec3af9c 7826 else if (decl && TREE_TYPE (decl) != error_mark_node
7827 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
f8e93a2e 7828 {
7829 tree tt = TREE_TYPE (decl);
e086912e 7830 *type = build_variant_type_copy (*type);
f8e93a2e 7831 DECL_ORIGINAL_TYPE (decl) = tt;
7832 TYPE_NAME (*type) = decl;
7833 TREE_USED (*type) = TREE_USED (decl);
7834 TREE_TYPE (decl) = *type;
7835 }
2ec3af9c 7836 else
e086912e 7837 *type = build_variant_type_copy (*type);
f8e93a2e 7838
5d4b30ea 7839 SET_TYPE_ALIGN (*type, (1U << i) * BITS_PER_UNIT);
f8e93a2e 7840 TYPE_USER_ALIGN (*type) = 1;
7841 }
097b5c8b 7842 else if (! VAR_OR_FUNCTION_DECL_P (decl)
f8e93a2e 7843 && TREE_CODE (decl) != FIELD_DECL)
7844 {
3cf8b391 7845 error ("alignment may not be specified for %q+D", decl);
f8e93a2e 7846 *no_add_attrs = true;
7847 }
ffcdbf9c 7848 else if (DECL_USER_ALIGN (decl)
7849 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7850 /* C++-11 [dcl.align/4]:
7851
7852 When multiple alignment-specifiers are specified for an
7853 entity, the alignment requirement shall be set to the
7854 strictest specified alignment.
7855
7856 This formally comes from the c++11 specification but we are
7857 doing it for the GNU attribute syntax as well. */
7858 *no_add_attrs = true;
097b5c8b 7859 else if (TREE_CODE (decl) == FUNCTION_DECL
7cfdc2f0 7860 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
097b5c8b 7861 {
7862 if (DECL_USER_ALIGN (decl))
7863 error ("alignment for %q+D was previously specified as %d "
7864 "and may not be decreased", decl,
7865 DECL_ALIGN (decl) / BITS_PER_UNIT);
7866 else
7867 error ("alignment for %q+D must be at least %d", decl,
7868 DECL_ALIGN (decl) / BITS_PER_UNIT);
7869 *no_add_attrs = true;
7870 }
f8e93a2e 7871 else
7872 {
5d4b30ea 7873 SET_DECL_ALIGN (decl, (1U << i) * BITS_PER_UNIT);
f8e93a2e 7874 DECL_USER_ALIGN (decl) = 1;
7875 }
7876
7877 return NULL_TREE;
7878}
7879
7880/* Handle a "weak" attribute; arguments as in
7881 struct attribute_spec.handler. */
7882
7883static tree
f948b309 7884handle_weak_attribute (tree *node, tree name,
9a03a746 7885 tree ARG_UNUSED (args),
7886 int ARG_UNUSED (flags),
7887 bool * ARG_UNUSED (no_add_attrs))
f8e93a2e 7888{
f948b309 7889 if (TREE_CODE (*node) == FUNCTION_DECL
059a60f3 7890 && DECL_DECLARED_INLINE_P (*node))
7891 {
0725e25c 7892 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
059a60f3 7893 *no_add_attrs = true;
7894 }
85c0a25c 7895 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7896 {
7897 error ("indirect function %q+D cannot be declared weak", *node);
7898 *no_add_attrs = true;
7899 return NULL_TREE;
7900 }
b443c459 7901 else if (VAR_OR_FUNCTION_DECL_P (*node))
9e6bcade 7902 declare_weak (*node);
f948b309 7903 else
7904 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7905
7906 return NULL_TREE;
7907}
7908
4641882f 7909/* Handle a "noplt" attribute; arguments as in
7910 struct attribute_spec.handler. */
7911
7912static tree
7913handle_noplt_attribute (tree *node, tree name,
7914 tree ARG_UNUSED (args),
7915 int ARG_UNUSED (flags),
7916 bool * ARG_UNUSED (no_add_attrs))
7917{
7918 if (TREE_CODE (*node) != FUNCTION_DECL)
7919 {
7920 warning (OPT_Wattributes,
7921 "%qE attribute is only applicable on functions", name);
7922 *no_add_attrs = true;
7923 return NULL_TREE;
7924 }
7925 return NULL_TREE;
7926}
7927
85c0a25c 7928/* Handle an "alias" or "ifunc" attribute; arguments as in
7929 struct attribute_spec.handler, except that IS_ALIAS tells us
7930 whether this is an alias as opposed to ifunc attribute. */
f8e93a2e 7931
7932static tree
85c0a25c 7933handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7934 bool *no_add_attrs)
f8e93a2e 7935{
7936 tree decl = *node;
7937
85c0a25c 7938 if (TREE_CODE (decl) != FUNCTION_DECL
f48c7f4a 7939 && (!is_alias || !VAR_P (decl)))
9e830260 7940 {
7941 warning (OPT_Wattributes, "%qE attribute ignored", name);
7942 *no_add_attrs = true;
7943 }
7944 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
48e1416a 7945 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 7946 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7947 /* A static variable declaration is always a tentative definition,
7948 but the alias is a non-tentative definition which overrides. */
48e1416a 7949 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 7950 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
f8e93a2e 7951 {
85c0a25c 7952 error ("%q+D defined both normally and as %qE attribute", decl, name);
f8e93a2e 7953 *no_add_attrs = true;
85c0a25c 7954 return NULL_TREE;
f8e93a2e 7955 }
85c0a25c 7956 else if (!is_alias
7957 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7958 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7959 {
7960 error ("weak %q+D cannot be defined %qE", decl, name);
7961 *no_add_attrs = true;
7962 return NULL_TREE;
7963 }
8c42f0d9 7964
7965 /* Note that the very first time we process a nested declaration,
7966 decl_function_context will not be set. Indeed, *would* never
7967 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7968 we do below. After such frobbery, pushdecl would set the context.
7969 In any case, this is never what we want. */
7970 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
f8e93a2e 7971 {
7972 tree id;
7973
7974 id = TREE_VALUE (args);
7975 if (TREE_CODE (id) != STRING_CST)
7976 {
85c0a25c 7977 error ("attribute %qE argument not a string", name);
f8e93a2e 7978 *no_add_attrs = true;
7979 return NULL_TREE;
7980 }
7981 id = get_identifier (TREE_STRING_POINTER (id));
7982 /* This counts as a use of the object pointed to. */
7983 TREE_USED (id) = 1;
7984
7985 if (TREE_CODE (decl) == FUNCTION_DECL)
7986 DECL_INITIAL (decl) = error_mark_node;
7987 else
f2526cce 7988 TREE_STATIC (decl) = 1;
85c0a25c 7989
7990 if (!is_alias)
7991 /* ifuncs are also aliases, so set that attribute too. */
7992 DECL_ATTRIBUTES (decl)
7993 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
f8e93a2e 7994 }
7995 else
7996 {
9b2d6d13 7997 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7998 *no_add_attrs = true;
7999 }
8000
8e857c41 8001 if (decl_in_symtab_p (*node))
8002 {
8003 struct symtab_node *n = symtab_node::get (decl);
8004 if (n && n->refuse_visibility_changes)
8005 {
8006 if (is_alias)
8007 error ("%+D declared alias after being used", decl);
8008 else
8009 error ("%+D declared ifunc after being used", decl);
8010 }
8011 }
8012
8013
f8e93a2e 8014 return NULL_TREE;
8015}
8016
85c0a25c 8017/* Handle an "alias" or "ifunc" attribute; arguments as in
8018 struct attribute_spec.handler. */
8019
8020static tree
8021handle_ifunc_attribute (tree *node, tree name, tree args,
8022 int ARG_UNUSED (flags), bool *no_add_attrs)
8023{
8024 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8025}
8026
8027/* Handle an "alias" or "ifunc" attribute; arguments as in
8028 struct attribute_spec.handler. */
8029
8030static tree
8031handle_alias_attribute (tree *node, tree name, tree args,
8032 int ARG_UNUSED (flags), bool *no_add_attrs)
8033{
8034 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8035}
8036
f4a30bd7 8037/* Handle a "weakref" attribute; arguments as in struct
8038 attribute_spec.handler. */
8039
8040static tree
8041handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8042 int flags, bool *no_add_attrs)
8043{
8044 tree attr = NULL_TREE;
8045
83852912 8046 /* We must ignore the attribute when it is associated with
8047 local-scoped decls, since attribute alias is ignored and many
8048 such symbols do not even have a DECL_WEAK field. */
64a7bd81 8049 if (decl_function_context (*node)
8050 || current_function_decl
b443c459 8051 || !VAR_OR_FUNCTION_DECL_P (*node))
83852912 8052 {
8053 warning (OPT_Wattributes, "%qE attribute ignored", name);
8054 *no_add_attrs = true;
8055 return NULL_TREE;
8056 }
8057
85c0a25c 8058 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8059 {
8060 error ("indirect function %q+D cannot be declared weakref", *node);
8061 *no_add_attrs = true;
8062 return NULL_TREE;
8063 }
8064
f4a30bd7 8065 /* The idea here is that `weakref("name")' mutates into `weakref,
8066 alias("name")', and weakref without arguments, in turn,
8067 implicitly adds weak. */
8068
8069 if (args)
8070 {
8071 attr = tree_cons (get_identifier ("alias"), args, attr);
8072 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8073
8074 *no_add_attrs = true;
0a3ecdc1 8075
8076 decl_attributes (node, attr, flags);
f4a30bd7 8077 }
8078 else
8079 {
8080 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
712d2297 8081 error_at (DECL_SOURCE_LOCATION (*node),
8082 "weakref attribute must appear before alias attribute");
f4a30bd7 8083
0a3ecdc1 8084 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8085 and that isn't supported; and because it wants to add it to
8086 the list of weak decls, which isn't helpful. */
8087 DECL_WEAK (*node) = 1;
f4a30bd7 8088 }
8089
8e857c41 8090 if (decl_in_symtab_p (*node))
8091 {
8092 struct symtab_node *n = symtab_node::get (*node);
8093 if (n && n->refuse_visibility_changes)
8094 error ("%+D declared weakref after being used", *node);
8095 }
8096
f4a30bd7 8097 return NULL_TREE;
8098}
8099
f8e93a2e 8100/* Handle an "visibility" attribute; arguments as in
8101 struct attribute_spec.handler. */
8102
8103static tree
1cae46be 8104handle_visibility_attribute (tree *node, tree name, tree args,
9a03a746 8105 int ARG_UNUSED (flags),
4a2849cb 8106 bool *ARG_UNUSED (no_add_attrs))
f8e93a2e 8107{
8108 tree decl = *node;
9c40570a 8109 tree id = TREE_VALUE (args);
4a2849cb 8110 enum symbol_visibility vis;
f8e93a2e 8111
b212f378 8112 if (TYPE_P (*node))
8113 {
4a2849cb 8114 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8115 /* OK */;
8e539fdc 8116 else if (!RECORD_OR_UNION_TYPE_P (*node))
4a2849cb 8117 {
8118 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8119 name);
8120 return NULL_TREE;
8121 }
8122 else if (TYPE_FIELDS (*node))
8123 {
8124 error ("%qE attribute ignored because %qT is already defined",
8125 name, *node);
8126 return NULL_TREE;
8127 }
b212f378 8128 }
84166705 8129 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
f8e93a2e 8130 {
9b2d6d13 8131 warning (OPT_Wattributes, "%qE attribute ignored", name);
9c40570a 8132 return NULL_TREE;
f8e93a2e 8133 }
f8e93a2e 8134
9c40570a 8135 if (TREE_CODE (id) != STRING_CST)
8136 {
07e3a3d2 8137 error ("visibility argument not a string");
9c40570a 8138 return NULL_TREE;
f8e93a2e 8139 }
b27ac6b5 8140
b212f378 8141 /* If this is a type, set the visibility on the type decl. */
8142 if (TYPE_P (decl))
8143 {
8144 decl = TYPE_NAME (decl);
84166705 8145 if (!decl)
a0c938f0 8146 return NULL_TREE;
e147aab3 8147 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8148 {
9b2d6d13 8149 warning (OPT_Wattributes, "%qE attribute ignored on types",
e147aab3 8150 name);
8151 return NULL_TREE;
8152 }
b212f378 8153 }
f8e93a2e 8154
9c40570a 8155 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
4a2849cb 8156 vis = VISIBILITY_DEFAULT;
9c40570a 8157 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
4a2849cb 8158 vis = VISIBILITY_INTERNAL;
9c40570a 8159 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
4a2849cb 8160 vis = VISIBILITY_HIDDEN;
9c40570a 8161 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
4a2849cb 8162 vis = VISIBILITY_PROTECTED;
9c40570a 8163 else
4a2849cb 8164 {
8165 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8166 vis = VISIBILITY_DEFAULT;
8167 }
8168
8169 if (DECL_VISIBILITY_SPECIFIED (decl)
098a01e7 8170 && vis != DECL_VISIBILITY (decl))
8171 {
8172 tree attributes = (TYPE_P (*node)
8173 ? TYPE_ATTRIBUTES (*node)
8174 : DECL_ATTRIBUTES (decl));
8175 if (lookup_attribute ("visibility", attributes))
8176 error ("%qD redeclared with different visibility", decl);
8177 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8178 && lookup_attribute ("dllimport", attributes))
8179 error ("%qD was declared %qs which implies default visibility",
8180 decl, "dllimport");
8181 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8182 && lookup_attribute ("dllexport", attributes))
8183 error ("%qD was declared %qs which implies default visibility",
8184 decl, "dllexport");
8185 }
4a2849cb 8186
8187 DECL_VISIBILITY (decl) = vis;
b212f378 8188 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8189
4a2849cb 8190 /* Go ahead and attach the attribute to the node as well. This is needed
8191 so we can determine whether we have VISIBILITY_DEFAULT because the
8192 visibility was not specified, or because it was explicitly overridden
8193 from the containing scope. */
9c40570a 8194
f8e93a2e 8195 return NULL_TREE;
8196}
8197
3aa0c315 8198/* Determine the ELF symbol visibility for DECL, which is either a
8199 variable or a function. It is an error to use this function if a
8200 definition of DECL is not available in this translation unit.
8201 Returns true if the final visibility has been determined by this
8202 function; false if the caller is free to make additional
8203 modifications. */
8204
8205bool
8206c_determine_visibility (tree decl)
8207{
b443c459 8208 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
3aa0c315 8209
8210 /* If the user explicitly specified the visibility with an
8211 attribute, honor that. DECL_VISIBILITY will have been set during
920f5a70 8212 the processing of the attribute. We check for an explicit
8213 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8214 to distinguish the use of an attribute from the use of a "#pragma
8215 GCC visibility push(...)"; in the latter case we still want other
8216 considerations to be able to overrule the #pragma. */
8217 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8218 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8219 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8220 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
3aa0c315 8221 return true;
8222
4a2849cb 8223 /* Set default visibility to whatever the user supplied with
8224 visibility_specified depending on #pragma GCC visibility. */
8225 if (!DECL_VISIBILITY_SPECIFIED (decl))
8226 {
2d9d8740 8227 if (visibility_options.inpragma
8228 || DECL_VISIBILITY (decl) != default_visibility)
8229 {
8230 DECL_VISIBILITY (decl) = default_visibility;
8231 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8232 /* If visibility changed and DECL already has DECL_RTL, ensure
8233 symbol flags are updated. */
f48c7f4a 8234 if (((VAR_P (decl) && TREE_STATIC (decl))
2d9d8740 8235 || TREE_CODE (decl) == FUNCTION_DECL)
8236 && DECL_RTL_SET_P (decl))
8237 make_decl_rtl (decl);
8238 }
4a2849cb 8239 }
3aa0c315 8240 return false;
8241}
8242
24dfead4 8243/* Handle an "tls_model" attribute; arguments as in
8244 struct attribute_spec.handler. */
8245
8246static tree
1cae46be 8247handle_tls_model_attribute (tree *node, tree name, tree args,
9a03a746 8248 int ARG_UNUSED (flags), bool *no_add_attrs)
24dfead4 8249{
1b53eb20 8250 tree id;
24dfead4 8251 tree decl = *node;
1b53eb20 8252 enum tls_model kind;
24dfead4 8253
1b53eb20 8254 *no_add_attrs = true;
8255
f48c7f4a 8256 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
24dfead4 8257 {
9b2d6d13 8258 warning (OPT_Wattributes, "%qE attribute ignored", name);
1b53eb20 8259 return NULL_TREE;
24dfead4 8260 }
24dfead4 8261
1b53eb20 8262 kind = DECL_TLS_MODEL (decl);
8263 id = TREE_VALUE (args);
8264 if (TREE_CODE (id) != STRING_CST)
8265 {
8266 error ("tls_model argument not a string");
8267 return NULL_TREE;
24dfead4 8268 }
8269
1b53eb20 8270 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8271 kind = TLS_MODEL_LOCAL_EXEC;
8272 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8273 kind = TLS_MODEL_INITIAL_EXEC;
8274 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8275 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8276 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8277 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8278 else
8279 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8280
5e68df57 8281 set_decl_tls_model (decl, kind);
24dfead4 8282 return NULL_TREE;
8283}
8284
f8e93a2e 8285/* Handle a "no_instrument_function" attribute; arguments as in
8286 struct attribute_spec.handler. */
8287
8288static tree
1cae46be 8289handle_no_instrument_function_attribute (tree *node, tree name,
9a03a746 8290 tree ARG_UNUSED (args),
8291 int ARG_UNUSED (flags),
09347743 8292 bool *no_add_attrs)
f8e93a2e 8293{
8294 tree decl = *node;
8295
8296 if (TREE_CODE (decl) != FUNCTION_DECL)
8297 {
712d2297 8298 error_at (DECL_SOURCE_LOCATION (decl),
8299 "%qE attribute applies only to functions", name);
f8e93a2e 8300 *no_add_attrs = true;
8301 }
f8e93a2e 8302 else
8303 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8304
8305 return NULL_TREE;
8306}
8307
8308/* Handle a "malloc" attribute; arguments as in
8309 struct attribute_spec.handler. */
8310
8311static tree
9a03a746 8312handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8313 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8314{
a5147fca 8315 if (TREE_CODE (*node) == FUNCTION_DECL
8316 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
f8e93a2e 8317 DECL_IS_MALLOC (*node) = 1;
f8e93a2e 8318 else
8319 {
9b2d6d13 8320 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8321 *no_add_attrs = true;
8322 }
8323
8324 return NULL_TREE;
8325}
8326
4a29c97c 8327/* Handle a "alloc_size" attribute; arguments as in
8328 struct attribute_spec.handler. */
8329
8330static tree
8331handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8332 int ARG_UNUSED (flags), bool *no_add_attrs)
8333{
2802826e 8334 unsigned arg_count = type_num_arguments (*node);
4a29c97c 8335 for (; args; args = TREE_CHAIN (args))
8336 {
8337 tree position = TREE_VALUE (args);
caf62483 8338 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8339 && TREE_CODE (position) != FUNCTION_DECL)
8340 position = default_conversion (position);
4a29c97c 8341
237e78b1 8342 if (!tree_fits_uhwi_p (position)
8343 || !arg_count
8344 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
4a29c97c 8345 {
48e1416a 8346 warning (OPT_Wattributes,
4a29c97c 8347 "alloc_size parameter outside range");
8348 *no_add_attrs = true;
8349 return NULL_TREE;
8350 }
8351 }
8352 return NULL_TREE;
8353}
8354
237e78b1 8355/* Handle a "alloc_align" attribute; arguments as in
8356 struct attribute_spec.handler. */
8357
8358static tree
8359handle_alloc_align_attribute (tree *node, tree, tree args, int,
8360 bool *no_add_attrs)
8361{
8362 unsigned arg_count = type_num_arguments (*node);
8363 tree position = TREE_VALUE (args);
8364 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8365 position = default_conversion (position);
8366
8367 if (!tree_fits_uhwi_p (position)
8368 || !arg_count
8369 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8370 {
8371 warning (OPT_Wattributes,
8372 "alloc_align parameter outside range");
8373 *no_add_attrs = true;
8374 return NULL_TREE;
8375 }
8376 return NULL_TREE;
8377}
8378
8379/* Handle a "assume_aligned" attribute; arguments as in
8380 struct attribute_spec.handler. */
8381
8382static tree
8383handle_assume_aligned_attribute (tree *, tree, tree args, int,
8384 bool *no_add_attrs)
8385{
8386 for (; args; args = TREE_CHAIN (args))
8387 {
8388 tree position = TREE_VALUE (args);
8389 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8390 && TREE_CODE (position) != FUNCTION_DECL)
8391 position = default_conversion (position);
8392
8393 if (TREE_CODE (position) != INTEGER_CST)
8394 {
8395 warning (OPT_Wattributes,
8396 "assume_aligned parameter not integer constant");
8397 *no_add_attrs = true;
8398 return NULL_TREE;
8399 }
8400 }
8401 return NULL_TREE;
8402}
8403
8ce86007 8404/* Handle a "fn spec" attribute; arguments as in
8405 struct attribute_spec.handler. */
8406
8407static tree
8408handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8409 tree args, int ARG_UNUSED (flags),
8410 bool *no_add_attrs ATTRIBUTE_UNUSED)
8411{
8412 gcc_assert (args
8413 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8414 && !TREE_CHAIN (args));
8415 return NULL_TREE;
8416}
8417
058a1b7a 8418/* Handle a "bnd_variable_size" attribute; arguments as in
8419 struct attribute_spec.handler. */
8420
8421static tree
8422handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8423 int ARG_UNUSED (flags), bool *no_add_attrs)
8424{
8425 if (TREE_CODE (*node) != FIELD_DECL)
8426 {
8427 warning (OPT_Wattributes, "%qE attribute ignored", name);
8428 *no_add_attrs = true;
8429 }
8430
8431 return NULL_TREE;
8432}
8433
8434/* Handle a "bnd_legacy" attribute; arguments as in
8435 struct attribute_spec.handler. */
8436
8437static tree
8438handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8439 int ARG_UNUSED (flags), bool *no_add_attrs)
8440{
8441 if (TREE_CODE (*node) != FUNCTION_DECL)
8442 {
8443 warning (OPT_Wattributes, "%qE attribute ignored", name);
8444 *no_add_attrs = true;
8445 }
8446
8447 return NULL_TREE;
8448}
8449
8450/* Handle a "bnd_instrument" attribute; arguments as in
8451 struct attribute_spec.handler. */
8452
8453static tree
8454handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8455 int ARG_UNUSED (flags), bool *no_add_attrs)
8456{
8457 if (TREE_CODE (*node) != FUNCTION_DECL)
8458 {
8459 warning (OPT_Wattributes, "%qE attribute ignored", name);
8460 *no_add_attrs = true;
8461 }
8462
8463 return NULL_TREE;
8464}
8465
a96c3cc1 8466/* Handle a "warn_unused" attribute; arguments as in
8467 struct attribute_spec.handler. */
8468
8469static tree
8470handle_warn_unused_attribute (tree *node, tree name,
8471 tree args ATTRIBUTE_UNUSED,
8472 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8473{
8474 if (TYPE_P (*node))
8475 /* Do nothing else, just set the attribute. We'll get at
8476 it later with lookup_attribute. */
8477 ;
8478 else
8479 {
8480 warning (OPT_Wattributes, "%qE attribute ignored", name);
8481 *no_add_attrs = true;
8482 }
8483
8484 return NULL_TREE;
8485}
8486
bc7bff74 8487/* Handle an "omp declare simd" attribute; arguments as in
8488 struct attribute_spec.handler. */
8489
8490static tree
8491handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8492{
8493 return NULL_TREE;
8494}
8495
c58a4cfd 8496/* Handle a "simd" attribute. */
8497
8498static tree
9841c6ee 8499handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
c58a4cfd 8500{
8501 if (TREE_CODE (*node) == FUNCTION_DECL)
8502 {
8503 if (lookup_attribute ("cilk simd function",
8504 DECL_ATTRIBUTES (*node)) != NULL)
8505 {
8506 error_at (DECL_SOURCE_LOCATION (*node),
8507 "%<__simd__%> attribute cannot be used in the same "
8508 "function marked as a Cilk Plus SIMD-enabled function");
8509 *no_add_attrs = true;
8510 }
8511 else
9841c6ee 8512 {
8513 tree t = get_identifier ("omp declare simd");
8514 tree attr = NULL_TREE;
8515 if (args)
8516 {
8517 tree id = TREE_VALUE (args);
8518
8519 if (TREE_CODE (id) != STRING_CST)
8520 {
8521 error ("attribute %qE argument not a string", name);
8522 *no_add_attrs = true;
8523 return NULL_TREE;
8524 }
8525
8526 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
8527 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8528 OMP_CLAUSE_NOTINBRANCH);
8529 else
8530 if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
8531 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8532 OMP_CLAUSE_INBRANCH);
8533 else
8534 {
8535 error ("only %<inbranch%> and %<notinbranch%> flags are "
8536 "allowed for %<__simd__%> attribute");
8537 *no_add_attrs = true;
8538 return NULL_TREE;
8539 }
8540 }
8541
8542 DECL_ATTRIBUTES (*node) = tree_cons (t,
8543 build_tree_list (NULL_TREE,
8544 attr),
8545 DECL_ATTRIBUTES (*node));
8546 }
c58a4cfd 8547 }
8548 else
8549 {
8550 warning (OPT_Wattributes, "%qE attribute ignored", name);
8551 *no_add_attrs = true;
8552 }
8553
8554 return NULL_TREE;
8555}
8556
bc7bff74 8557/* Handle an "omp declare target" attribute; arguments as in
8558 struct attribute_spec.handler. */
8559
8560static tree
8561handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8562{
8563 return NULL_TREE;
8564}
8565
26d1c5ff 8566/* Handle a "returns_twice" attribute; arguments as in
8567 struct attribute_spec.handler. */
8568
8569static tree
8570handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8571 int ARG_UNUSED (flags), bool *no_add_attrs)
8572{
8573 if (TREE_CODE (*node) == FUNCTION_DECL)
8574 DECL_IS_RETURNS_TWICE (*node) = 1;
8575 else
8576 {
9b2d6d13 8577 warning (OPT_Wattributes, "%qE attribute ignored", name);
26d1c5ff 8578 *no_add_attrs = true;
8579 }
8580
8581 return NULL_TREE;
8582}
8583
f8e93a2e 8584/* Handle a "no_limit_stack" attribute; arguments as in
8585 struct attribute_spec.handler. */
8586
8587static tree
1cae46be 8588handle_no_limit_stack_attribute (tree *node, tree name,
9a03a746 8589 tree ARG_UNUSED (args),
8590 int ARG_UNUSED (flags),
09347743 8591 bool *no_add_attrs)
f8e93a2e 8592{
8593 tree decl = *node;
8594
8595 if (TREE_CODE (decl) != FUNCTION_DECL)
8596 {
712d2297 8597 error_at (DECL_SOURCE_LOCATION (decl),
8598 "%qE attribute applies only to functions", name);
f8e93a2e 8599 *no_add_attrs = true;
8600 }
8601 else if (DECL_INITIAL (decl))
8602 {
712d2297 8603 error_at (DECL_SOURCE_LOCATION (decl),
8604 "can%'t set %qE attribute after definition", name);
f8e93a2e 8605 *no_add_attrs = true;
8606 }
8607 else
8608 DECL_NO_LIMIT_STACK (decl) = 1;
8609
8610 return NULL_TREE;
8611}
8612
8613/* Handle a "pure" attribute; arguments as in
8614 struct attribute_spec.handler. */
8615
8616static tree
9a03a746 8617handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8618 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8619{
8620 if (TREE_CODE (*node) == FUNCTION_DECL)
9c2a0c05 8621 DECL_PURE_P (*node) = 1;
f8e93a2e 8622 /* ??? TODO: Support types. */
8623 else
8624 {
9b2d6d13 8625 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8626 *no_add_attrs = true;
8627 }
8628
8629 return NULL_TREE;
8630}
8631
4c0315d0 8632/* Digest an attribute list destined for a transactional memory statement.
8633 ALLOWED is the set of attributes that are allowed for this statement;
8634 return the attribute we parsed. Multiple attributes are never allowed. */
8635
8636int
8637parse_tm_stmt_attr (tree attrs, int allowed)
8638{
8639 tree a_seen = NULL;
8640 int m_seen = 0;
8641
8642 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8643 {
8644 tree a = TREE_PURPOSE (attrs);
8645 int m = 0;
8646
8647 if (is_attribute_p ("outer", a))
8648 m = TM_STMT_ATTR_OUTER;
8649
8650 if ((m & allowed) == 0)
8651 {
8652 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8653 continue;
8654 }
8655
8656 if (m_seen == 0)
8657 {
8658 a_seen = a;
8659 m_seen = m;
8660 }
8661 else if (m_seen == m)
8662 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8663 else
8664 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8665 }
8666
8667 return m_seen;
8668}
8669
8670/* Transform a TM attribute name into a maskable integer and back.
8671 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8672 to how the lack of an attribute is treated. */
8673
8674int
8675tm_attr_to_mask (tree attr)
8676{
8677 if (attr == NULL)
8678 return 0;
8679 if (is_attribute_p ("transaction_safe", attr))
8680 return TM_ATTR_SAFE;
8681 if (is_attribute_p ("transaction_callable", attr))
8682 return TM_ATTR_CALLABLE;
8683 if (is_attribute_p ("transaction_pure", attr))
8684 return TM_ATTR_PURE;
8685 if (is_attribute_p ("transaction_unsafe", attr))
8686 return TM_ATTR_IRREVOCABLE;
8687 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8688 return TM_ATTR_MAY_CANCEL_OUTER;
8689 return 0;
8690}
8691
8692tree
8693tm_mask_to_attr (int mask)
8694{
8695 const char *str;
8696 switch (mask)
8697 {
8698 case TM_ATTR_SAFE:
8699 str = "transaction_safe";
8700 break;
8701 case TM_ATTR_CALLABLE:
8702 str = "transaction_callable";
8703 break;
8704 case TM_ATTR_PURE:
8705 str = "transaction_pure";
8706 break;
8707 case TM_ATTR_IRREVOCABLE:
8708 str = "transaction_unsafe";
8709 break;
8710 case TM_ATTR_MAY_CANCEL_OUTER:
8711 str = "transaction_may_cancel_outer";
8712 break;
8713 default:
8714 gcc_unreachable ();
8715 }
8716 return get_identifier (str);
8717}
8718
8719/* Return the first TM attribute seen in LIST. */
8720
8721tree
8722find_tm_attribute (tree list)
8723{
8724 for (; list ; list = TREE_CHAIN (list))
8725 {
8726 tree name = TREE_PURPOSE (list);
8727 if (tm_attr_to_mask (name) != 0)
8728 return name;
8729 }
8730 return NULL_TREE;
8731}
8732
8733/* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8734 Here we accept only function types, and verify that none of the other
8735 function TM attributes are also applied. */
8736/* ??? We need to accept class types for C++, but not C. This greatly
8737 complicates this function, since we can no longer rely on the extra
8738 processing given by function_type_required. */
8739
8740static tree
8741handle_tm_attribute (tree *node, tree name, tree args,
8742 int flags, bool *no_add_attrs)
8743{
8744 /* Only one path adds the attribute; others don't. */
8745 *no_add_attrs = true;
8746
8747 switch (TREE_CODE (*node))
8748 {
8749 case RECORD_TYPE:
8750 case UNION_TYPE:
8751 /* Only tm_callable and tm_safe apply to classes. */
8752 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8753 goto ignored;
8754 /* FALLTHRU */
8755
8756 case FUNCTION_TYPE:
8757 case METHOD_TYPE:
8758 {
8759 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8760 if (old_name == name)
8761 ;
8762 else if (old_name != NULL_TREE)
8763 error ("type was previously declared %qE", old_name);
8764 else
8765 *no_add_attrs = false;
8766 }
8767 break;
8768
6d02e6b2 8769 case FUNCTION_DECL:
8770 {
8771 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
8772 want to set transaction_safe on the type. */
8773 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
8774 if (!TYPE_P (DECL_CONTEXT (*node)))
8775 error_at (DECL_SOURCE_LOCATION (*node),
8776 "%<transaction_safe_dynamic%> may only be specified for "
8777 "a virtual function");
8778 *no_add_attrs = false;
8779 decl_attributes (&TREE_TYPE (*node),
8780 build_tree_list (get_identifier ("transaction_safe"),
8781 NULL_TREE),
8782 0);
8783 break;
8784 }
8785
4c0315d0 8786 case POINTER_TYPE:
8787 {
8788 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8789 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8790 {
8791 tree fn_tmp = TREE_TYPE (*node);
8792 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8793 *node = build_pointer_type (fn_tmp);
8794 break;
8795 }
8796 }
8797 /* FALLTHRU */
8798
8799 default:
8800 /* If a function is next, pass it on to be tried next. */
8801 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8802 return tree_cons (name, args, NULL);
8803
8804 ignored:
8805 warning (OPT_Wattributes, "%qE attribute ignored", name);
8806 break;
8807 }
8808
8809 return NULL_TREE;
8810}
8811
8812/* Handle the TM_WRAP attribute; arguments as in
8813 struct attribute_spec.handler. */
8814
8815static tree
8816handle_tm_wrap_attribute (tree *node, tree name, tree args,
8817 int ARG_UNUSED (flags), bool *no_add_attrs)
8818{
8819 tree decl = *node;
8820
8821 /* We don't need the attribute even on success, since we
8822 record the entry in an external table. */
8823 *no_add_attrs = true;
8824
8825 if (TREE_CODE (decl) != FUNCTION_DECL)
8826 warning (OPT_Wattributes, "%qE attribute ignored", name);
8827 else
8828 {
8829 tree wrap_decl = TREE_VALUE (args);
3e5a8b00 8830 if (error_operand_p (wrap_decl))
8831 ;
8832 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
b443c459 8833 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
4c0315d0 8834 error ("%qE argument not an identifier", name);
8835 else
8836 {
8837 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8838 wrap_decl = lookup_name (wrap_decl);
8839 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8840 {
8841 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8842 TREE_TYPE (wrap_decl)))
8843 record_tm_replacement (wrap_decl, decl);
8844 else
8845 error ("%qD is not compatible with %qD", wrap_decl, decl);
8846 }
8847 else
cdf34fca 8848 error ("%qE argument is not a function", name);
4c0315d0 8849 }
8850 }
8851
8852 return NULL_TREE;
8853}
8854
8855/* Ignore the given attribute. Used when this attribute may be usefully
8856 overridden by the target, but is not used generically. */
8857
8858static tree
8859ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8860 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8861 bool *no_add_attrs)
8862{
8863 *no_add_attrs = true;
8864 return NULL_TREE;
8865}
8866
fc09b200 8867/* Handle a "no vops" attribute; arguments as in
8868 struct attribute_spec.handler. */
8869
8870static tree
8871handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8872 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8873 bool *ARG_UNUSED (no_add_attrs))
8874{
8875 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8876 DECL_IS_NOVOPS (*node) = 1;
8877 return NULL_TREE;
8878}
8879
f8e93a2e 8880/* Handle a "deprecated" attribute; arguments as in
8881 struct attribute_spec.handler. */
1cae46be 8882
f8e93a2e 8883static tree
1cae46be 8884handle_deprecated_attribute (tree *node, tree name,
45c4e798 8885 tree args, int flags,
09347743 8886 bool *no_add_attrs)
f8e93a2e 8887{
8888 tree type = NULL_TREE;
8889 int warn = 0;
782858b8 8890 tree what = NULL_TREE;
1cae46be 8891
45c4e798 8892 if (!args)
8893 *no_add_attrs = true;
8894 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8895 {
8896 error ("deprecated message is not a string");
8897 *no_add_attrs = true;
8898 }
8899
f8e93a2e 8900 if (DECL_P (*node))
8901 {
8902 tree decl = *node;
8903 type = TREE_TYPE (decl);
1cae46be 8904
f8e93a2e 8905 if (TREE_CODE (decl) == TYPE_DECL
8906 || TREE_CODE (decl) == PARM_DECL
b443c459 8907 || VAR_OR_FUNCTION_DECL_P (decl)
40c8d1dd 8908 || TREE_CODE (decl) == FIELD_DECL
5a4c69dd 8909 || TREE_CODE (decl) == CONST_DECL
40c8d1dd 8910 || objc_method_decl (TREE_CODE (decl)))
f8e93a2e 8911 TREE_DEPRECATED (decl) = 1;
8912 else
8913 warn = 1;
8914 }
8915 else if (TYPE_P (*node))
8916 {
8917 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 8918 *node = build_variant_type_copy (*node);
f8e93a2e 8919 TREE_DEPRECATED (*node) = 1;
8920 type = *node;
8921 }
8922 else
8923 warn = 1;
1cae46be 8924
f8e93a2e 8925 if (warn)
8926 {
8927 *no_add_attrs = true;
8928 if (type && TYPE_NAME (type))
8929 {
8930 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
782858b8 8931 what = TYPE_NAME (*node);
f8e93a2e 8932 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8933 && DECL_NAME (TYPE_NAME (type)))
782858b8 8934 what = DECL_NAME (TYPE_NAME (type));
f8e93a2e 8935 }
8936 if (what)
9b2d6d13 8937 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
f8e93a2e 8938 else
9b2d6d13 8939 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8940 }
8941
8942 return NULL_TREE;
8943}
8944
f8e93a2e 8945/* Handle a "vector_size" attribute; arguments as in
8946 struct attribute_spec.handler. */
8947
8948static tree
1cae46be 8949handle_vector_size_attribute (tree *node, tree name, tree args,
9a03a746 8950 int ARG_UNUSED (flags),
09347743 8951 bool *no_add_attrs)
f8e93a2e 8952{
8953 unsigned HOST_WIDE_INT vecsize, nunits;
3754d046 8954 machine_mode orig_mode;
4917c376 8955 tree type = *node, new_type, size;
f8e93a2e 8956
8957 *no_add_attrs = true;
8958
4917c376 8959 size = TREE_VALUE (args);
3e5a8b00 8960 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8961 && TREE_CODE (size) != FUNCTION_DECL)
caf62483 8962 size = default_conversion (size);
4917c376 8963
e913b5cd 8964 if (!tree_fits_uhwi_p (size))
f8e93a2e 8965 {
9b2d6d13 8966 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8967 return NULL_TREE;
8968 }
8969
8970 /* Get the vector size (in bytes). */
e913b5cd 8971 vecsize = tree_to_uhwi (size);
f8e93a2e 8972
8973 /* We need to provide for vector pointers, vector arrays, and
8974 functions returning vectors. For example:
8975
8976 __attribute__((vector_size(16))) short *foo;
8977
8978 In this case, the mode is SI, but the type being modified is
8979 HI, so we need to look further. */
8980
8981 while (POINTER_TYPE_P (type)
8982 || TREE_CODE (type) == FUNCTION_TYPE
5bfb0742 8983 || TREE_CODE (type) == METHOD_TYPE
2cb0e5d9 8984 || TREE_CODE (type) == ARRAY_TYPE
8985 || TREE_CODE (type) == OFFSET_TYPE)
f8e93a2e 8986 type = TREE_TYPE (type);
8987
8988 /* Get the mode of the type being modified. */
8989 orig_mode = TYPE_MODE (type);
8990
2cb0e5d9 8991 if ((!INTEGRAL_TYPE_P (type)
8992 && !SCALAR_FLOAT_TYPE_P (type)
8993 && !FIXED_POINT_TYPE_P (type))
cee7491d 8994 || (!SCALAR_FLOAT_MODE_P (orig_mode)
9421ebb9 8995 && GET_MODE_CLASS (orig_mode) != MODE_INT
8996 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
e913b5cd 8997 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7ec31215 8998 || TREE_CODE (type) == BOOLEAN_TYPE)
f8e93a2e 8999 {
782858b8 9000 error ("invalid vector type for attribute %qE", name);
f8e93a2e 9001 return NULL_TREE;
9002 }
9003
e913b5cd 9004 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
39cc3e6d 9005 {
9006 error ("vector size not an integral multiple of component size");
9007 return NULL;
9008 }
9009
9010 if (vecsize == 0)
9011 {
9012 error ("zero vector size");
9013 return NULL;
9014 }
9015
f8e93a2e 9016 /* Calculate how many units fit in the vector. */
e913b5cd 9017 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
83e2a11b 9018 if (nunits & (nunits - 1))
f8e93a2e 9019 {
83e2a11b 9020 error ("number of components of the vector not a power of two");
f8e93a2e 9021 return NULL_TREE;
9022 }
9023
83e2a11b 9024 new_type = build_vector_type (type, nunits);
f8e93a2e 9025
9026 /* Build back pointers if needed. */
d991e6e8 9027 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
f8e93a2e 9028
9029 return NULL_TREE;
9030}
9031
dbf6c367 9032/* Handle the "nonnull" attribute. */
9033static tree
9a03a746 9034handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9035 tree args, int ARG_UNUSED (flags),
09347743 9036 bool *no_add_attrs)
dbf6c367 9037{
9038 tree type = *node;
9039 unsigned HOST_WIDE_INT attr_arg_num;
9040
9041 /* If no arguments are specified, all pointer arguments should be
d716ce75 9042 non-null. Verify a full prototype is given so that the arguments
dbf6c367 9043 will have the correct types when we actually check them later. */
84166705 9044 if (!args)
dbf6c367 9045 {
a36cf284 9046 if (!prototype_p (type))
dbf6c367 9047 {
9048 error ("nonnull attribute without arguments on a non-prototype");
4ee9c684 9049 *no_add_attrs = true;
dbf6c367 9050 }
9051 return NULL_TREE;
9052 }
9053
9054 /* Argument list specified. Verify that each argument number references
9055 a pointer argument. */
caf62483 9056 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
dbf6c367 9057 {
4ee9c684 9058 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
dbf6c367 9059
caf62483 9060 tree arg = TREE_VALUE (args);
9061 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9062 && TREE_CODE (arg) != FUNCTION_DECL)
9063 arg = default_conversion (arg);
9064
9065 if (!get_nonnull_operand (arg, &arg_num))
dbf6c367 9066 {
07e3a3d2 9067 error ("nonnull argument has invalid operand number (argument %lu)",
dbf6c367 9068 (unsigned long) attr_arg_num);
9069 *no_add_attrs = true;
9070 return NULL_TREE;
9071 }
9072
d0af78c5 9073 if (prototype_p (type))
dbf6c367 9074 {
d0af78c5 9075 function_args_iterator iter;
9076 tree argument;
9077
9078 function_args_iter_init (&iter, type);
9079 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
dbf6c367 9080 {
d0af78c5 9081 argument = function_args_iter_cond (&iter);
9082 if (argument == NULL_TREE || ck_num == arg_num)
dbf6c367 9083 break;
dbf6c367 9084 }
9085
84166705 9086 if (!argument
d0af78c5 9087 || TREE_CODE (argument) == VOID_TYPE)
dbf6c367 9088 {
07e3a3d2 9089 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
dbf6c367 9090 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9091 *no_add_attrs = true;
9092 return NULL_TREE;
9093 }
9094
d0af78c5 9095 if (TREE_CODE (argument) != POINTER_TYPE)
dbf6c367 9096 {
07e3a3d2 9097 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
dbf6c367 9098 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9099 *no_add_attrs = true;
9100 return NULL_TREE;
9101 }
9102 }
9103 }
9104
9105 return NULL_TREE;
9106}
9107
9108/* Check the argument list of a function call for null in argument slots
d01f58f9 9109 that are marked as requiring a non-null pointer argument. The NARGS
bda05c57 9110 arguments are passed in the array ARGARRAY. */
dbf6c367 9111
9112static void
bda05c57 9113check_function_nonnull (location_t loc, tree attrs, int nargs, tree *argarray)
dbf6c367 9114{
9ca77b08 9115 tree a;
d01f58f9 9116 int i;
dbf6c367 9117
9ca77b08 9118 attrs = lookup_attribute ("nonnull", attrs);
9119 if (attrs == NULL_TREE)
9120 return;
9121
9122 a = attrs;
9123 /* See if any of the nonnull attributes has no arguments. If so,
9124 then every pointer argument is checked (in which case the check
9125 for pointer type is done in check_nonnull_arg). */
9126 if (TREE_VALUE (a) != NULL_TREE)
9127 do
9128 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9129 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9130
9131 if (a != NULL_TREE)
9132 for (i = 0; i < nargs; i++)
bda05c57 9133 check_function_arguments_recurse (check_nonnull_arg, &loc, argarray[i],
9ca77b08 9134 i + 1);
9135 else
dbf6c367 9136 {
9ca77b08 9137 /* Walk the argument list. If we encounter an argument number we
9138 should check for non-null, do it. */
9139 for (i = 0; i < nargs; i++)
dbf6c367 9140 {
9ca77b08 9141 for (a = attrs; ; a = TREE_CHAIN (a))
4ee9c684 9142 {
9ca77b08 9143 a = lookup_attribute ("nonnull", a);
9144 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9145 break;
4ee9c684 9146 }
9ca77b08 9147
9148 if (a != NULL_TREE)
bda05c57 9149 check_function_arguments_recurse (check_nonnull_arg, &loc,
9ca77b08 9150 argarray[i], i + 1);
dbf6c367 9151 }
9152 }
9153}
9154
50ca527f 9155/* Check that the Nth argument of a function call (counting backwards
d01f58f9 9156 from the end) is a (pointer)0. The NARGS arguments are passed in the
9157 array ARGARRAY. */
bf6c8de0 9158
9159static void
774e9d58 9160check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
bf6c8de0 9161{
774e9d58 9162 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
bf6c8de0 9163
9164 if (attr)
9165 {
d01f58f9 9166 int len = 0;
9167 int pos = 0;
9168 tree sentinel;
774e9d58 9169 function_args_iterator iter;
9170 tree t;
a0c938f0 9171
d01f58f9 9172 /* Skip over the named arguments. */
774e9d58 9173 FOREACH_FUNCTION_ARGS (fntype, t, iter)
a0c938f0 9174 {
774e9d58 9175 if (len == nargs)
9176 break;
d01f58f9 9177 len++;
9178 }
50ca527f 9179
d01f58f9 9180 if (TREE_VALUE (attr))
9181 {
9182 tree p = TREE_VALUE (TREE_VALUE (attr));
f9ae6f95 9183 pos = TREE_INT_CST_LOW (p);
d01f58f9 9184 }
50ca527f 9185
d01f58f9 9186 /* The sentinel must be one of the varargs, i.e.
9187 in position >= the number of fixed arguments. */
9188 if ((nargs - 1 - pos) < len)
9189 {
77a357e3 9190 warning (OPT_Wformat_,
d01f58f9 9191 "not enough variable arguments to fit a sentinel");
9192 return;
bf6c8de0 9193 }
d01f58f9 9194
9195 /* Validate the sentinel. */
9196 sentinel = argarray[nargs - 1 - pos];
9197 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9198 || !integer_zerop (sentinel))
9199 /* Although __null (in C++) is only an integer we allow it
9200 nevertheless, as we are guaranteed that it's exactly
9201 as wide as a pointer, and we don't want to force
9202 users to cast the NULL they have written there.
9203 We warn with -Wstrict-null-sentinel, though. */
9204 && (warn_strict_null_sentinel || null_node != sentinel))
77a357e3 9205 warning (OPT_Wformat_, "missing sentinel in function call");
bf6c8de0 9206 }
9207}
9208
dbf6c367 9209/* Helper for check_function_nonnull; given a list of operands which
9210 must be non-null in ARGS, determine if operand PARAM_NUM should be
9211 checked. */
9212
9213static bool
1cae46be 9214nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
dbf6c367 9215{
4ee9c684 9216 unsigned HOST_WIDE_INT arg_num = 0;
dbf6c367 9217
9218 for (; args; args = TREE_CHAIN (args))
9219 {
231bd014 9220 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9221
9222 gcc_assert (found);
dbf6c367 9223
9224 if (arg_num == param_num)
9225 return true;
9226 }
9227 return false;
9228}
9229
9230/* Check that the function argument PARAM (which is operand number
9231 PARAM_NUM) is non-null. This is called by check_function_nonnull
9232 via check_function_arguments_recurse. */
9233
9234static void
bda05c57 9235check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
dbf6c367 9236{
bda05c57 9237 location_t *ploc = (location_t *) ctx;
9238
dbf6c367 9239 /* Just skip checking the argument if it's not a pointer. This can
9240 happen if the "nonnull" attribute was given without an operand
9241 list (which means to check every pointer argument). */
9242
9243 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9244 return;
9245
9246 if (integer_zerop (param))
bda05c57 9247 warning_at (*ploc, OPT_Wnonnull, "null argument where non-null required "
9248 "(argument %lu)", (unsigned long) param_num);
dbf6c367 9249}
9250
9251/* Helper for nonnull attribute handling; fetch the operand number
9252 from the attribute argument list. */
9253
9254static bool
1cae46be 9255get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
dbf6c367 9256{
e913b5cd 9257 /* Verify the arg number is a small constant. */
e1d65c9f 9258 if (tree_fits_uhwi_p (arg_num_expr))
e913b5cd 9259 {
f9ae6f95 9260 *valp = TREE_INT_CST_LOW (arg_num_expr);
e913b5cd 9261 return true;
9262 }
9263 else
dbf6c367 9264 return false;
dbf6c367 9265}
fa987697 9266
9267/* Handle a "nothrow" attribute; arguments as in
9268 struct attribute_spec.handler. */
9269
9270static tree
9a03a746 9271handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9272 int ARG_UNUSED (flags), bool *no_add_attrs)
fa987697 9273{
9274 if (TREE_CODE (*node) == FUNCTION_DECL)
9275 TREE_NOTHROW (*node) = 1;
9276 /* ??? TODO: Support types. */
9277 else
9278 {
9b2d6d13 9279 warning (OPT_Wattributes, "%qE attribute ignored", name);
fa987697 9280 *no_add_attrs = true;
9281 }
9282
9283 return NULL_TREE;
9284}
7acb29a3 9285
9286/* Handle a "cleanup" attribute; arguments as in
9287 struct attribute_spec.handler. */
9288
9289static tree
1cae46be 9290handle_cleanup_attribute (tree *node, tree name, tree args,
9a03a746 9291 int ARG_UNUSED (flags), bool *no_add_attrs)
7acb29a3 9292{
9293 tree decl = *node;
9294 tree cleanup_id, cleanup_decl;
9295
9296 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9297 for global destructors in C++. This requires infrastructure that
9298 we don't have generically at the moment. It's also not a feature
9299 we'd be missing too much, since we do have attribute constructor. */
f48c7f4a 9300 if (!VAR_P (decl) || TREE_STATIC (decl))
7acb29a3 9301 {
9b2d6d13 9302 warning (OPT_Wattributes, "%qE attribute ignored", name);
7acb29a3 9303 *no_add_attrs = true;
9304 return NULL_TREE;
9305 }
9306
9307 /* Verify that the argument is a function in scope. */
9308 /* ??? We could support pointers to functions here as well, if
9309 that was considered desirable. */
9310 cleanup_id = TREE_VALUE (args);
9311 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9312 {
07e3a3d2 9313 error ("cleanup argument not an identifier");
7acb29a3 9314 *no_add_attrs = true;
9315 return NULL_TREE;
9316 }
d1c41717 9317 cleanup_decl = lookup_name (cleanup_id);
7acb29a3 9318 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9319 {
07e3a3d2 9320 error ("cleanup argument not a function");
7acb29a3 9321 *no_add_attrs = true;
9322 return NULL_TREE;
9323 }
9324
1cae46be 9325 /* That the function has proper type is checked with the
7acb29a3 9326 eventual call to build_function_call. */
9327
9328 return NULL_TREE;
9329}
8a8cdb8d 9330
9331/* Handle a "warn_unused_result" attribute. No special handling. */
9332
9333static tree
9334handle_warn_unused_result_attribute (tree *node, tree name,
9a03a746 9335 tree ARG_UNUSED (args),
9336 int ARG_UNUSED (flags), bool *no_add_attrs)
8a8cdb8d 9337{
9338 /* Ignore the attribute for functions not returning any value. */
9339 if (VOID_TYPE_P (TREE_TYPE (*node)))
9340 {
9b2d6d13 9341 warning (OPT_Wattributes, "%qE attribute ignored", name);
8a8cdb8d 9342 *no_add_attrs = true;
9343 }
9344
9345 return NULL_TREE;
9346}
bf6c8de0 9347
9348/* Handle a "sentinel" attribute. */
9349
9350static tree
50ca527f 9351handle_sentinel_attribute (tree *node, tree name, tree args,
bf6c8de0 9352 int ARG_UNUSED (flags), bool *no_add_attrs)
9353{
a36cf284 9354 if (!prototype_p (*node))
bf6c8de0 9355 {
9b2d6d13 9356 warning (OPT_Wattributes,
9357 "%qE attribute requires prototypes with named arguments", name);
bf6c8de0 9358 *no_add_attrs = true;
bf6c8de0 9359 }
50ca527f 9360 else
9361 {
c33080b9 9362 if (!stdarg_p (*node))
a0c938f0 9363 {
9b2d6d13 9364 warning (OPT_Wattributes,
9365 "%qE attribute only applies to variadic functions", name);
50ca527f 9366 *no_add_attrs = true;
9367 }
9368 }
a0c938f0 9369
50ca527f 9370 if (args)
bf6c8de0 9371 {
50ca527f 9372 tree position = TREE_VALUE (args);
3e5a8b00 9373 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9374 && TREE_CODE (position) != FUNCTION_DECL)
9375 position = default_conversion (position);
50ca527f 9376
3e5a8b00 9377 if (TREE_CODE (position) != INTEGER_CST
9378 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
a0c938f0 9379 {
48e1416a 9380 warning (OPT_Wattributes,
01b54db5 9381 "requested position is not an integer constant");
50ca527f 9382 *no_add_attrs = true;
9383 }
9384 else
a0c938f0 9385 {
50ca527f 9386 if (tree_int_cst_lt (position, integer_zero_node))
9387 {
01b54db5 9388 warning (OPT_Wattributes,
9389 "requested position is less than zero");
50ca527f 9390 *no_add_attrs = true;
9391 }
9392 }
bf6c8de0 9393 }
a0c938f0 9394
bf6c8de0 9395 return NULL_TREE;
9396}
b5c26b42 9397
9398/* Handle a "type_generic" attribute. */
9399
9400static tree
9401handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9402 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9403 bool * ARG_UNUSED (no_add_attrs))
9404{
19fbe3a4 9405 /* Ensure we have a function type. */
9406 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
48e1416a 9407
19fbe3a4 9408 /* Ensure we have a variadic function. */
c33080b9 9409 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
b5c26b42 9410
9411 return NULL_TREE;
9412}
46f8e3b0 9413
24470055 9414/* Handle a "target" attribute. */
46f8e3b0 9415
9416static tree
24470055 9417handle_target_attribute (tree *node, tree name, tree args, int flags,
46f8e3b0 9418 bool *no_add_attrs)
9419{
9420 /* Ensure we have a function type. */
9421 if (TREE_CODE (*node) != FUNCTION_DECL)
9422 {
9423 warning (OPT_Wattributes, "%qE attribute ignored", name);
9424 *no_add_attrs = true;
9425 }
ab50af2a 9426 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
9427 {
9428 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9429 "with %qs attribute", name, "target_clones");
9430 *no_add_attrs = true;
9431 }
46f8e3b0 9432 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
ae0c3984 9433 flags))
46f8e3b0 9434 *no_add_attrs = true;
9435
9436 return NULL_TREE;
9437}
9438
ab50af2a 9439/* Handle a "target_clones" attribute. */
9440
9441static tree
9442handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9443 int ARG_UNUSED (flags), bool *no_add_attrs)
9444{
9445 /* Ensure we have a function type. */
9446 if (TREE_CODE (*node) == FUNCTION_DECL)
9447 {
9448 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
9449 {
9450 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9451 "with %qs attribute", name, "always_inline");
9452 *no_add_attrs = true;
9453 }
9454 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
9455 {
9456 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9457 "with %qs attribute", name, "target");
9458 *no_add_attrs = true;
9459 }
9460 else
9461 /* Do not inline functions with multiple clone targets. */
9462 DECL_UNINLINABLE (*node) = 1;
9463 }
9464 else
9465 {
9466 warning (OPT_Wattributes, "%qE attribute ignored", name);
9467 *no_add_attrs = true;
9468 }
9469 return NULL_TREE;
9470}
9471
46f8e3b0 9472/* Arguments being collected for optimization. */
9473typedef const char *const_char_p; /* For DEF_VEC_P. */
f1f41a6c 9474static GTY(()) vec<const_char_p, va_gc> *optimize_args;
46f8e3b0 9475
9476
9477/* Inner function to convert a TREE_LIST to argv string to parse the optimize
9478 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9479 false for #pragma GCC optimize. */
9480
9481bool
9482parse_optimize_options (tree args, bool attr_p)
9483{
9484 bool ret = true;
9485 unsigned opt_argc;
9486 unsigned i;
9487 const char **opt_argv;
615ef0bb 9488 struct cl_decoded_option *decoded_options;
9489 unsigned int decoded_options_count;
46f8e3b0 9490 tree ap;
9491
9492 /* Build up argv vector. Just in case the string is stored away, use garbage
9493 collected strings. */
f1f41a6c 9494 vec_safe_truncate (optimize_args, 0);
9495 vec_safe_push (optimize_args, (const char *) NULL);
46f8e3b0 9496
9497 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9498 {
9499 tree value = TREE_VALUE (ap);
9500
9501 if (TREE_CODE (value) == INTEGER_CST)
9502 {
9503 char buffer[20];
f9ae6f95 9504 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
f1f41a6c 9505 vec_safe_push (optimize_args, ggc_strdup (buffer));
46f8e3b0 9506 }
9507
9508 else if (TREE_CODE (value) == STRING_CST)
9509 {
9510 /* Split string into multiple substrings. */
9511 size_t len = TREE_STRING_LENGTH (value);
9512 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9513 char *end = p + len;
9514 char *comma;
9515 char *next_p = p;
9516
9517 while (next_p != NULL)
9518 {
9519 size_t len2;
9520 char *q, *r;
9521
9522 p = next_p;
9523 comma = strchr (p, ',');
9524 if (comma)
9525 {
9526 len2 = comma - p;
9527 *comma = '\0';
9528 next_p = comma+1;
9529 }
9530 else
9531 {
9532 len2 = end - p;
9533 next_p = NULL;
9534 }
9535
ba72912a 9536 r = q = (char *) ggc_alloc_atomic (len2 + 3);
46f8e3b0 9537
9538 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9539 options. */
9540 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9541 {
9542 ret = false;
9543 if (attr_p)
9544 warning (OPT_Wattributes,
ebd7c4c1 9545 "bad option %s to optimize attribute", p);
46f8e3b0 9546 else
9547 warning (OPT_Wpragmas,
e44b0a1f 9548 "bad option %s to pragma attribute", p);
46f8e3b0 9549 continue;
9550 }
9551
9552 if (*p != '-')
9553 {
9554 *r++ = '-';
9555
9556 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9557 itself is -Os, and any other switch begins with a -f. */
9558 if ((*p >= '0' && *p <= '9')
9559 || (p[0] == 's' && p[1] == '\0'))
9560 *r++ = 'O';
9561 else if (*p != 'O')
9562 *r++ = 'f';
9563 }
9564
9565 memcpy (r, p, len2);
9566 r[len2] = '\0';
f1f41a6c 9567 vec_safe_push (optimize_args, (const char *) q);
46f8e3b0 9568 }
9569
9570 }
9571 }
9572
f1f41a6c 9573 opt_argc = optimize_args->length ();
46f8e3b0 9574 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9575
9576 for (i = 1; i < opt_argc; i++)
f1f41a6c 9577 opt_argv[i] = (*optimize_args)[i];
46f8e3b0 9578
9579 /* Now parse the options. */
f3f006ad 9580 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9581 &decoded_options,
9582 &decoded_options_count);
b032c4dd 9583 /* Drop non-Optimization options. */
9584 unsigned j = 1;
9585 for (i = 1; i < decoded_options_count; ++i)
9586 {
9587 if (! (cl_options[decoded_options[i].opt_index].flags & CL_OPTIMIZATION))
9588 {
9589 ret = false;
9590 if (attr_p)
9591 warning (OPT_Wattributes,
9592 "bad option %s to optimize attribute",
9593 decoded_options[i].orig_option_with_args_text);
9594 else
9595 warning (OPT_Wpragmas,
9596 "bad option %s to pragma attribute",
9597 decoded_options[i].orig_option_with_args_text);
9598 continue;
9599 }
9600 if (i != j)
9601 decoded_options[j] = decoded_options[i];
9602 j++;
9603 }
9604 decoded_options_count = j;
9605 /* And apply them. */
f3f006ad 9606 decode_options (&global_options, &global_options_set,
3c6c0e40 9607 decoded_options, decoded_options_count,
9608 input_location, global_dc);
46f8e3b0 9609
4bec06b3 9610 targetm.override_options_after_change();
9611
f1f41a6c 9612 optimize_args->truncate (0);
46f8e3b0 9613 return ret;
9614}
9615
9616/* For handling "optimize" attribute. arguments as in
9617 struct attribute_spec.handler. */
9618
9619static tree
9620handle_optimize_attribute (tree *node, tree name, tree args,
9621 int ARG_UNUSED (flags), bool *no_add_attrs)
9622{
9623 /* Ensure we have a function type. */
9624 if (TREE_CODE (*node) != FUNCTION_DECL)
9625 {
9626 warning (OPT_Wattributes, "%qE attribute ignored", name);
9627 *no_add_attrs = true;
9628 }
9629 else
9630 {
9631 struct cl_optimization cur_opts;
9632 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9633
9634 /* Save current options. */
2c5d2e39 9635 cl_optimization_save (&cur_opts, &global_options);
46f8e3b0 9636
9637 /* If we previously had some optimization options, use them as the
9638 default. */
9639 if (old_opts)
2c5d2e39 9640 cl_optimization_restore (&global_options,
9641 TREE_OPTIMIZATION (old_opts));
46f8e3b0 9642
9643 /* Parse options, and update the vector. */
9644 parse_optimize_options (args, true);
9645 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
55310327 9646 = build_optimization_node (&global_options);
46f8e3b0 9647
9648 /* Restore current options. */
2c5d2e39 9649 cl_optimization_restore (&global_options, &cur_opts);
46f8e3b0 9650 }
9651
9652 return NULL_TREE;
9653}
48b14f50 9654
9655/* Handle a "no_split_stack" attribute. */
9656
9657static tree
9658handle_no_split_stack_attribute (tree *node, tree name,
9659 tree ARG_UNUSED (args),
9660 int ARG_UNUSED (flags),
9661 bool *no_add_attrs)
9662{
9663 tree decl = *node;
9664
9665 if (TREE_CODE (decl) != FUNCTION_DECL)
9666 {
9667 error_at (DECL_SOURCE_LOCATION (decl),
9668 "%qE attribute applies only to functions", name);
9669 *no_add_attrs = true;
9670 }
9671 else if (DECL_INITIAL (decl))
9672 {
9673 error_at (DECL_SOURCE_LOCATION (decl),
9674 "can%'t set %qE attribute after definition", name);
9675 *no_add_attrs = true;
9676 }
9677
9678 return NULL_TREE;
9679}
d7dcba40 9680
9681/* Handle a "returns_nonnull" attribute; arguments as in
9682 struct attribute_spec.handler. */
9683
9684static tree
9685handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9686 bool *no_add_attrs)
9687{
9688 // Even without a prototype we still have a return type we can check.
9689 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9690 {
9691 error ("returns_nonnull attribute on a function not returning a pointer");
9692 *no_add_attrs = true;
9693 }
9694 return NULL_TREE;
9695}
9696
74691f46 9697/* Handle a "designated_init" attribute; arguments as in
9698 struct attribute_spec.handler. */
9699
9700static tree
9701handle_designated_init_attribute (tree *node, tree name, tree, int,
9702 bool *no_add_attrs)
9703{
9704 if (TREE_CODE (*node) != RECORD_TYPE)
9705 {
9706 error ("%qE attribute is only valid on %<struct%> type", name);
9707 *no_add_attrs = true;
9708 }
9709 return NULL_TREE;
9710}
9711
dbf6c367 9712\f
774e9d58 9713/* Check for valid arguments being passed to a function with FNTYPE.
bda05c57 9714 There are NARGS arguments in the array ARGARRAY. LOC should be used for
9715 diagnostics. */
dbf6c367 9716void
bda05c57 9717check_function_arguments (location_t loc, const_tree fntype, int nargs,
9718 tree *argarray)
dbf6c367 9719{
9720 /* Check for null being passed in a pointer argument that must be
9721 non-null. We also need to do this if format checking is enabled. */
9722
9723 if (warn_nonnull)
bda05c57 9724 check_function_nonnull (loc, TYPE_ATTRIBUTES (fntype), nargs, argarray);
dbf6c367 9725
9726 /* Check for errors in format strings. */
9727
068bea1e 9728 if (warn_format || warn_suggest_attribute_format)
774e9d58 9729 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
95c90e04 9730
9731 if (warn_format)
774e9d58 9732 check_function_sentinel (fntype, nargs, argarray);
dbf6c367 9733}
9734
9735/* Generic argument checking recursion routine. PARAM is the argument to
9736 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9737 once the argument is resolved. CTX is context for the callback. */
9738void
1cae46be 9739check_function_arguments_recurse (void (*callback)
9740 (void *, tree, unsigned HOST_WIDE_INT),
9741 void *ctx, tree param,
9742 unsigned HOST_WIDE_INT param_num)
dbf6c367 9743{
72dd6141 9744 if (CONVERT_EXPR_P (param)
c44afe23 9745 && (TYPE_PRECISION (TREE_TYPE (param))
9746 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
dbf6c367 9747 {
9748 /* Strip coercion. */
9749 check_function_arguments_recurse (callback, ctx,
4ee9c684 9750 TREE_OPERAND (param, 0), param_num);
dbf6c367 9751 return;
9752 }
9753
9754 if (TREE_CODE (param) == CALL_EXPR)
9755 {
c2f47e15 9756 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
dbf6c367 9757 tree attrs;
9758 bool found_format_arg = false;
9759
9760 /* See if this is a call to a known internationalization function
9761 that modifies a format arg. Such a function may have multiple
9762 format_arg attributes (for example, ngettext). */
9763
9764 for (attrs = TYPE_ATTRIBUTES (type);
9765 attrs;
9766 attrs = TREE_CHAIN (attrs))
9767 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9768 {
c2f47e15 9769 tree inner_arg;
dbf6c367 9770 tree format_num_expr;
9771 int format_num;
9772 int i;
c2f47e15 9773 call_expr_arg_iterator iter;
dbf6c367 9774
9775 /* Extract the argument number, which was previously checked
9776 to be valid. */
9777 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
ddb1be65 9778
e913b5cd 9779 format_num = tree_to_uhwi (format_num_expr);
dbf6c367 9780
c2f47e15 9781 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9782 inner_arg != 0;
9783 inner_arg = next_call_expr_arg (&iter), i++)
dbf6c367 9784 if (i == format_num)
9785 {
9786 check_function_arguments_recurse (callback, ctx,
c2f47e15 9787 inner_arg, param_num);
dbf6c367 9788 found_format_arg = true;
9789 break;
9790 }
9791 }
9792
9793 /* If we found a format_arg attribute and did a recursive check,
9794 we are done with checking this argument. Otherwise, we continue
9795 and this will be considered a non-literal. */
9796 if (found_format_arg)
9797 return;
9798 }
9799
9800 if (TREE_CODE (param) == COND_EXPR)
9801 {
f6dfb86a 9802 /* Simplify to avoid warning for an impossible case. */
9803 param = fold_for_warn (param);
9804 if (TREE_CODE (param) == COND_EXPR)
9805 {
9806 /* Check both halves of the conditional expression. */
9807 check_function_arguments_recurse (callback, ctx,
9808 TREE_OPERAND (param, 1),
9809 param_num);
9810 check_function_arguments_recurse (callback, ctx,
9811 TREE_OPERAND (param, 2),
9812 param_num);
9813 return;
9814 }
dbf6c367 9815 }
9816
9817 (*callback) (ctx, param, param_num);
9818}
1f3233d1 9819
60cce472 9820/* Checks for a builtin function FNDECL that the number of arguments
9821 NARGS against the required number REQUIRED and issues an error if
9822 there is a mismatch. Returns true if the number of arguments is
5d4db8ef 9823 correct, otherwise false. LOC is the location of FNDECL. */
d43cee80 9824
9825static bool
5d4db8ef 9826builtin_function_validate_nargs (location_t loc, tree fndecl, int nargs,
9827 int required)
d43cee80 9828{
9829 if (nargs < required)
9830 {
5d4db8ef 9831 error_at (loc, "not enough arguments to function %qE", fndecl);
d43cee80 9832 return false;
9833 }
9834 else if (nargs > required)
9835 {
5d4db8ef 9836 error_at (loc, "too many arguments to function %qE", fndecl);
d43cee80 9837 return false;
9838 }
9839 return true;
9840}
9841
5d4db8ef 9842/* Helper macro for check_builtin_function_arguments. */
9843#define ARG_LOCATION(N) \
9844 (arg_loc.is_empty () \
9845 ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
9846 : expansion_point_location (arg_loc[(N)]))
9847
d43cee80 9848/* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
5d4db8ef 9849 Returns false if there was an error, otherwise true. LOC is the
9850 location of the function; ARG_LOC is a vector of locations of the
9851 arguments. */
d43cee80 9852
9853bool
5d4db8ef 9854check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
9855 tree fndecl, int nargs, tree *args)
d43cee80 9856{
9857 if (!DECL_BUILT_IN (fndecl)
9858 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9859 return true;
9860
9861 switch (DECL_FUNCTION_CODE (fndecl))
9862 {
8c38d887 9863 case BUILT_IN_ALLOCA_WITH_ALIGN:
9864 {
9865 /* Get the requested alignment (in bits) if it's a constant
9866 integer expression. */
9867 unsigned HOST_WIDE_INT align
9868 = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
9869
9870 /* Determine if the requested alignment is a power of 2. */
9871 if ((align & (align - 1)))
9872 align = 0;
9873
9874 /* The maximum alignment in bits corresponding to the same
9875 maximum in bytes enforced in check_user_alignment(). */
9876 unsigned maxalign = (UINT_MAX >> 1) + 1;
5d4db8ef 9877
8c38d887 9878 /* Reject invalid alignments. */
9879 if (align < BITS_PER_UNIT || maxalign < align)
9880 {
5d4db8ef 9881 error_at (ARG_LOCATION (1),
8c38d887 9882 "second argument to function %qE must be a constant "
9883 "integer power of 2 between %qi and %qu bits",
9884 fndecl, BITS_PER_UNIT, maxalign);
9885 return false;
9886 }
5d4db8ef 9887 return true;
8c38d887 9888 }
9889
d43cee80 9890 case BUILT_IN_CONSTANT_P:
5d4db8ef 9891 return builtin_function_validate_nargs (loc, fndecl, nargs, 1);
d43cee80 9892
9893 case BUILT_IN_ISFINITE:
9894 case BUILT_IN_ISINF:
c319d56a 9895 case BUILT_IN_ISINF_SIGN:
d43cee80 9896 case BUILT_IN_ISNAN:
9897 case BUILT_IN_ISNORMAL:
10902624 9898 case BUILT_IN_SIGNBIT:
5d4db8ef 9899 if (builtin_function_validate_nargs (loc, fndecl, nargs, 1))
d43cee80 9900 {
9901 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9902 {
5d4db8ef 9903 error_at (ARG_LOCATION (0), "non-floating-point argument in "
9904 "call to function %qE", fndecl);
d43cee80 9905 return false;
9906 }
9907 return true;
9908 }
9909 return false;
9910
9911 case BUILT_IN_ISGREATER:
9912 case BUILT_IN_ISGREATEREQUAL:
9913 case BUILT_IN_ISLESS:
9914 case BUILT_IN_ISLESSEQUAL:
9915 case BUILT_IN_ISLESSGREATER:
9916 case BUILT_IN_ISUNORDERED:
5d4db8ef 9917 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2))
d43cee80 9918 {
9919 enum tree_code code0, code1;
9920 code0 = TREE_CODE (TREE_TYPE (args[0]));
9921 code1 = TREE_CODE (TREE_TYPE (args[1]));
9922 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9923 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9924 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9925 {
5d4db8ef 9926 error_at (loc, "non-floating-point arguments in call to "
9927 "function %qE", fndecl);
d43cee80 9928 return false;
9929 }
9930 return true;
9931 }
9932 return false;
9933
19fbe3a4 9934 case BUILT_IN_FPCLASSIFY:
5d4db8ef 9935 if (builtin_function_validate_nargs (loc, fndecl, nargs, 6))
19fbe3a4 9936 {
5d4db8ef 9937 for (unsigned int i = 0; i < 5; i++)
19fbe3a4 9938 if (TREE_CODE (args[i]) != INTEGER_CST)
9939 {
5d4db8ef 9940 error_at (ARG_LOCATION (i), "non-const integer argument %u in "
9941 "call to function %qE", i + 1, fndecl);
19fbe3a4 9942 return false;
9943 }
9944
9945 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9946 {
5d4db8ef 9947 error_at (ARG_LOCATION (5), "non-floating-point argument in "
9948 "call to function %qE", fndecl);
19fbe3a4 9949 return false;
9950 }
9951 return true;
9952 }
9953 return false;
9954
fca0886c 9955 case BUILT_IN_ASSUME_ALIGNED:
5d4db8ef 9956 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2 + (nargs > 2)))
fca0886c 9957 {
9958 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9959 {
5d4db8ef 9960 error_at (ARG_LOCATION (2), "non-integer argument 3 in call to "
9961 "function %qE", fndecl);
fca0886c 9962 return false;
9963 }
9964 return true;
9965 }
9966 return false;
9967
0c93c8a9 9968 case BUILT_IN_ADD_OVERFLOW:
9969 case BUILT_IN_SUB_OVERFLOW:
9970 case BUILT_IN_MUL_OVERFLOW:
5d4db8ef 9971 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
0c93c8a9 9972 {
9973 unsigned i;
9974 for (i = 0; i < 2; i++)
9975 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
9976 {
5d4db8ef 9977 error_at (ARG_LOCATION (i), "argument %u in call to function "
9978 "%qE does not have integral type", i + 1, fndecl);
0c93c8a9 9979 return false;
9980 }
9981 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
9982 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
9983 {
5d4db8ef 9984 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
9985 "does not have pointer to integer type", fndecl);
0c93c8a9 9986 return false;
9987 }
9988 return true;
9989 }
9990 return false;
9991
d43cee80 9992 default:
9993 return true;
9994 }
9995}
9996
860251be 9997/* Function to help qsort sort FIELD_DECLs by name order. */
9998
9999int
10000field_decl_cmp (const void *x_p, const void *y_p)
10001{
4fd61bc6 10002 const tree *const x = (const tree *const) x_p;
10003 const tree *const y = (const tree *const) y_p;
10004
860251be 10005 if (DECL_NAME (*x) == DECL_NAME (*y))
10006 /* A nontype is "greater" than a type. */
10007 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10008 if (DECL_NAME (*x) == NULL_TREE)
10009 return -1;
10010 if (DECL_NAME (*y) == NULL_TREE)
10011 return 1;
10012 if (DECL_NAME (*x) < DECL_NAME (*y))
10013 return -1;
10014 return 1;
10015}
10016
10017static struct {
10018 gt_pointer_operator new_value;
10019 void *cookie;
10020} resort_data;
10021
10022/* This routine compares two fields like field_decl_cmp but using the
10023pointer operator in resort_data. */
10024
10025static int
10026resort_field_decl_cmp (const void *x_p, const void *y_p)
10027{
4fd61bc6 10028 const tree *const x = (const tree *const) x_p;
10029 const tree *const y = (const tree *const) y_p;
860251be 10030
10031 if (DECL_NAME (*x) == DECL_NAME (*y))
10032 /* A nontype is "greater" than a type. */
10033 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10034 if (DECL_NAME (*x) == NULL_TREE)
10035 return -1;
10036 if (DECL_NAME (*y) == NULL_TREE)
10037 return 1;
10038 {
10039 tree d1 = DECL_NAME (*x);
10040 tree d2 = DECL_NAME (*y);
10041 resort_data.new_value (&d1, resort_data.cookie);
10042 resort_data.new_value (&d2, resort_data.cookie);
10043 if (d1 < d2)
10044 return -1;
10045 }
10046 return 1;
10047}
10048
10049/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10050
10051void
10052resort_sorted_fields (void *obj,
9a03a746 10053 void * ARG_UNUSED (orig_obj),
4ee9c684 10054 gt_pointer_operator new_value,
10055 void *cookie)
860251be 10056{
9a03a746 10057 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
860251be 10058 resort_data.new_value = new_value;
10059 resort_data.cookie = cookie;
10060 qsort (&sf->elts[0], sf->len, sizeof (tree),
4ee9c684 10061 resort_field_decl_cmp);
860251be 10062}
10063
209c9752 10064/* Subroutine of c_parse_error.
10065 Return the result of concatenating LHS and RHS. RHS is really
10066 a string literal, its first character is indicated by RHS_START and
cfee01e3 10067 RHS_SIZE is its length (including the terminating NUL character).
209c9752 10068
10069 The caller is responsible for deleting the returned pointer. */
10070
10071static char *
10072catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10073{
10074 const int lhs_size = strlen (lhs);
10075 char *result = XNEWVEC (char, lhs_size + rhs_size);
10076 strncpy (result, lhs, lhs_size);
10077 strncpy (result + lhs_size, rhs_start, rhs_size);
10078 return result;
10079}
10080
380c6697 10081/* Issue the error given by GMSGID, indicating that it occurred before
92b128ed 10082 TOKEN, which had the associated VALUE. */
10083
10084void
48e1416a 10085c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
ba99525e 10086 tree value, unsigned char token_flags)
92b128ed 10087{
209c9752 10088#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10089
10090 char *message = NULL;
92b128ed 10091
ba99525e 10092 if (token_type == CPP_EOF)
380c6697 10093 message = catenate_messages (gmsgid, " at end of input");
48e1416a 10094 else if (token_type == CPP_CHAR
10095 || token_type == CPP_WCHAR
ba99525e 10096 || token_type == CPP_CHAR16
30b1ba42 10097 || token_type == CPP_CHAR32
10098 || token_type == CPP_UTF8CHAR)
92b128ed 10099 {
f9ae6f95 10100 unsigned int val = TREE_INT_CST_LOW (value);
924bbf02 10101 const char *prefix;
10102
ba99525e 10103 switch (token_type)
924bbf02 10104 {
10105 default:
10106 prefix = "";
10107 break;
10108 case CPP_WCHAR:
10109 prefix = "L";
10110 break;
10111 case CPP_CHAR16:
10112 prefix = "u";
10113 break;
10114 case CPP_CHAR32:
10115 prefix = "U";
10116 break;
30b1ba42 10117 case CPP_UTF8CHAR:
10118 prefix = "u8";
10119 break;
924bbf02 10120 }
10121
92b128ed 10122 if (val <= UCHAR_MAX && ISGRAPH (val))
a0c938f0 10123 message = catenate_messages (gmsgid, " before %s'%c'");
92b128ed 10124 else
a0c938f0 10125 message = catenate_messages (gmsgid, " before %s'\\x%x'");
209c9752 10126
924bbf02 10127 error (message, prefix, val);
209c9752 10128 free (message);
10129 message = NULL;
92b128ed 10130 }
1898176c 10131 else if (token_type == CPP_CHAR_USERDEF
10132 || token_type == CPP_WCHAR_USERDEF
10133 || token_type == CPP_CHAR16_USERDEF
30b1ba42 10134 || token_type == CPP_CHAR32_USERDEF
10135 || token_type == CPP_UTF8CHAR_USERDEF)
1898176c 10136 message = catenate_messages (gmsgid,
10137 " before user-defined character literal");
10138 else if (token_type == CPP_STRING_USERDEF
10139 || token_type == CPP_WSTRING_USERDEF
10140 || token_type == CPP_STRING16_USERDEF
10141 || token_type == CPP_STRING32_USERDEF
10142 || token_type == CPP_UTF8STRING_USERDEF)
10143 message = catenate_messages (gmsgid, " before user-defined string literal");
48e1416a 10144 else if (token_type == CPP_STRING
10145 || token_type == CPP_WSTRING
ba99525e 10146 || token_type == CPP_STRING16
538ba11a 10147 || token_type == CPP_STRING32
10148 || token_type == CPP_UTF8STRING)
380c6697 10149 message = catenate_messages (gmsgid, " before string constant");
ba99525e 10150 else if (token_type == CPP_NUMBER)
380c6697 10151 message = catenate_messages (gmsgid, " before numeric constant");
ba99525e 10152 else if (token_type == CPP_NAME)
209c9752 10153 {
380c6697 10154 message = catenate_messages (gmsgid, " before %qE");
782858b8 10155 error (message, value);
209c9752 10156 free (message);
10157 message = NULL;
10158 }
ba99525e 10159 else if (token_type == CPP_PRAGMA)
b75b98aa 10160 message = catenate_messages (gmsgid, " before %<#pragma%>");
ba99525e 10161 else if (token_type == CPP_PRAGMA_EOL)
b75b98aa 10162 message = catenate_messages (gmsgid, " before end of line");
07b8f133 10163 else if (token_type == CPP_DECLTYPE)
10164 message = catenate_messages (gmsgid, " before %<decltype%>");
ba99525e 10165 else if (token_type < N_TTYPES)
209c9752 10166 {
380c6697 10167 message = catenate_messages (gmsgid, " before %qs token");
ba99525e 10168 error (message, cpp_type2name (token_type, token_flags));
209c9752 10169 free (message);
10170 message = NULL;
10171 }
92b128ed 10172 else
380c6697 10173 error (gmsgid);
209c9752 10174
10175 if (message)
10176 {
10177 error (message);
10178 free (message);
10179 }
a0c938f0 10180#undef catenate_messages
92b128ed 10181}
10182
3a79f5da 10183/* Return the gcc option code associated with the reason for a cpp
10184 message, or 0 if none. */
10185
10186static int
10187c_option_controlling_cpp_error (int reason)
10188{
7ff8db31 10189 const struct cpp_reason_option_codes_t *entry;
3a79f5da 10190
7ff8db31 10191 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
3a79f5da 10192 {
10193 if (entry->reason == reason)
10194 return entry->option_code;
10195 }
10196 return 0;
10197}
10198
7f5f3953 10199/* Callback from cpp_error for PFILE to print diagnostics from the
3a79f5da 10200 preprocessor. The diagnostic is of type LEVEL, with REASON set
10201 to the reason code if LEVEL is represents a warning, at location
f0479000 10202 RICHLOC unless this is after lexing and the compiler's location
10203 should be used instead; MSG is the translated message and AP
7f5f3953 10204 the arguments. Returns true if a diagnostic was emitted, false
10205 otherwise. */
10206
10207bool
3a79f5da 10208c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
f0479000 10209 rich_location *richloc,
7f5f3953 10210 const char *msg, va_list *ap)
10211{
10212 diagnostic_info diagnostic;
10213 diagnostic_t dlevel;
5ae82d58 10214 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
7f5f3953 10215 bool ret;
10216
10217 switch (level)
10218 {
10219 case CPP_DL_WARNING_SYSHDR:
10220 if (flag_no_output)
10221 return false;
5ae82d58 10222 global_dc->dc_warn_system_headers = 1;
7f5f3953 10223 /* Fall through. */
10224 case CPP_DL_WARNING:
10225 if (flag_no_output)
10226 return false;
10227 dlevel = DK_WARNING;
10228 break;
10229 case CPP_DL_PEDWARN:
10230 if (flag_no_output && !flag_pedantic_errors)
10231 return false;
10232 dlevel = DK_PEDWARN;
10233 break;
10234 case CPP_DL_ERROR:
10235 dlevel = DK_ERROR;
10236 break;
10237 case CPP_DL_ICE:
10238 dlevel = DK_ICE;
10239 break;
10240 case CPP_DL_NOTE:
10241 dlevel = DK_NOTE;
10242 break;
ff903809 10243 case CPP_DL_FATAL:
10244 dlevel = DK_FATAL;
10245 break;
7f5f3953 10246 default:
10247 gcc_unreachable ();
10248 }
10249 if (done_lexing)
d0f713f4 10250 richloc->set_range (line_table, 0, input_location, true);
7f5f3953 10251 diagnostic_set_info_translated (&diagnostic, msg, ap,
f0479000 10252 richloc, dlevel);
3a79f5da 10253 diagnostic_override_option_index (&diagnostic,
10254 c_option_controlling_cpp_error (reason));
7f5f3953 10255 ret = report_diagnostic (&diagnostic);
10256 if (level == CPP_DL_WARNING_SYSHDR)
5ae82d58 10257 global_dc->dc_warn_system_headers = save_warn_system_headers;
7f5f3953 10258 return ret;
10259}
10260
624d37a6 10261/* Convert a character from the host to the target execution character
10262 set. cpplib handles this, mostly. */
10263
10264HOST_WIDE_INT
10265c_common_to_target_charset (HOST_WIDE_INT c)
10266{
10267 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10268 zero-extended under -fno-signed-char. cpplib insists that characters
10269 and character constants are always unsigned. Hence we must convert
10270 back and forth. */
10271 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10272
10273 uc = cpp_host_to_exec_charset (parse_in, uc);
10274
10275 if (flag_signed_char)
10276 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10277 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10278 else
10279 return uc;
10280}
10281
7549df0d 10282/* Fold an offsetof-like expression. EXPR is a nested sequence of component
10283 references with an INDIRECT_REF of a constant at the bottom; much like the
10284 traditional rendering of offsetof as a macro. Return the folded result. */
af28855b 10285
7549df0d 10286tree
aeaccb75 10287fold_offsetof_1 (tree expr, enum tree_code ctx)
af28855b 10288{
af28855b 10289 tree base, off, t;
aeaccb75 10290 tree_code code = TREE_CODE (expr);
10291 switch (code)
af28855b 10292 {
10293 case ERROR_MARK:
10294 return expr;
10295
6b11d2e3 10296 case VAR_DECL:
10297 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10298 return error_mark_node;
10299
d897f7c2 10300 case CALL_EXPR:
cf1a89a3 10301 case TARGET_EXPR:
d897f7c2 10302 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10303 return error_mark_node;
10304
d897f7c2 10305 case NOP_EXPR:
10306 case INDIRECT_REF:
7549df0d 10307 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
64ed018c 10308 {
10309 error ("cannot apply %<offsetof%> to a non constant address");
10310 return error_mark_node;
10311 }
7549df0d 10312 return TREE_OPERAND (expr, 0);
d897f7c2 10313
af28855b 10314 case COMPONENT_REF:
aeaccb75 10315 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
af28855b 10316 if (base == error_mark_node)
10317 return base;
10318
10319 t = TREE_OPERAND (expr, 1);
10320 if (DECL_C_BIT_FIELD (t))
10321 {
10322 error ("attempt to take address of bit-field structure "
782858b8 10323 "member %qD", t);
af28855b 10324 return error_mark_node;
10325 }
389dd41b 10326 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
e913b5cd 10327 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
389dd41b 10328 / BITS_PER_UNIT));
af28855b 10329 break;
10330
10331 case ARRAY_REF:
aeaccb75 10332 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
af28855b 10333 if (base == error_mark_node)
10334 return base;
10335
10336 t = TREE_OPERAND (expr, 1);
64ed018c 10337
10338 /* Check if the offset goes beyond the upper bound of the array. */
7549df0d 10339 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
e0559d69 10340 {
10341 tree upbound = array_ref_up_bound (expr);
10342 if (upbound != NULL_TREE
10343 && TREE_CODE (upbound) == INTEGER_CST
10344 && !tree_int_cst_equal (upbound,
10345 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10346 {
aeaccb75 10347 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
10348 upbound = size_binop (PLUS_EXPR, upbound,
10349 build_int_cst (TREE_TYPE (upbound), 1));
e0559d69 10350 if (tree_int_cst_lt (upbound, t))
10351 {
10352 tree v;
10353
10354 for (v = TREE_OPERAND (expr, 0);
10355 TREE_CODE (v) == COMPONENT_REF;
10356 v = TREE_OPERAND (v, 0))
10357 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10358 == RECORD_TYPE)
10359 {
1767a056 10360 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10361 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
e0559d69 10362 if (TREE_CODE (fld_chain) == FIELD_DECL)
10363 break;
10364
10365 if (fld_chain)
10366 break;
10367 }
10368 /* Don't warn if the array might be considered a poor
10369 man's flexible array member with a very permissive
10370 definition thereof. */
10371 if (TREE_CODE (v) == ARRAY_REF
10372 || TREE_CODE (v) == COMPONENT_REF)
10373 warning (OPT_Warray_bounds,
10374 "index %E denotes an offset "
10375 "greater than size of %qT",
10376 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10377 }
10378 }
10379 }
7549df0d 10380
10381 t = convert (sizetype, t);
10382 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
af28855b 10383 break;
10384
ede90cc2 10385 case COMPOUND_EXPR:
10386 /* Handle static members of volatile structs. */
10387 t = TREE_OPERAND (expr, 1);
f48c7f4a 10388 gcc_assert (VAR_P (t));
7549df0d 10389 return fold_offsetof_1 (t);
ede90cc2 10390
af28855b 10391 default:
231bd014 10392 gcc_unreachable ();
af28855b 10393 }
10394
7549df0d 10395 return fold_build_pointer_plus (base, off);
af28855b 10396}
10397
7549df0d 10398/* Likewise, but convert it to the return type of offsetof. */
10399
af28855b 10400tree
7549df0d 10401fold_offsetof (tree expr)
af28855b 10402{
7549df0d 10403 return convert (size_type_node, fold_offsetof_1 (expr));
af28855b 10404}
10405
b9bdfa0b 10406/* Warn for A ?: C expressions (with B omitted) where A is a boolean
10407 expression, because B will always be true. */
10408
10409void
10410warn_for_omitted_condop (location_t location, tree cond)
10411{
10412 if (truth_value_p (TREE_CODE (cond)))
10413 warning_at (location, OPT_Wparentheses,
10414 "the omitted middle operand in ?: will always be %<true%>, "
10415 "suggest explicit middle operand");
10416}
10417
a1f90215 10418/* Give an error for storing into ARG, which is 'const'. USE indicates
10419 how ARG was being used. */
10420
10421void
f2697631 10422readonly_error (location_t loc, tree arg, enum lvalue_use use)
a1f90215 10423{
10424 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10425 || use == lv_asm);
10426 /* Using this macro rather than (for example) arrays of messages
10427 ensures that all the format strings are checked at compile
10428 time. */
10429#define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10430 : (use == lv_increment ? (I) \
10431 : (use == lv_decrement ? (D) : (AS))))
10432 if (TREE_CODE (arg) == COMPONENT_REF)
10433 {
10434 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
f2697631 10435 error_at (loc, READONLY_MSG (G_("assignment of member "
10436 "%qD in read-only object"),
10437 G_("increment of member "
10438 "%qD in read-only object"),
10439 G_("decrement of member "
10440 "%qD in read-only object"),
10441 G_("member %qD in read-only object "
10442 "used as %<asm%> output")),
10443 TREE_OPERAND (arg, 1));
a1f90215 10444 else
f2697631 10445 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10446 G_("increment of read-only member %qD"),
10447 G_("decrement of read-only member %qD"),
10448 G_("read-only member %qD used as %<asm%> output")),
10449 TREE_OPERAND (arg, 1));
a1f90215 10450 }
f48c7f4a 10451 else if (VAR_P (arg))
f2697631 10452 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10453 G_("increment of read-only variable %qD"),
10454 G_("decrement of read-only variable %qD"),
10455 G_("read-only variable %qD used as %<asm%> output")),
10456 arg);
a1f90215 10457 else if (TREE_CODE (arg) == PARM_DECL)
f2697631 10458 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10459 G_("increment of read-only parameter %qD"),
10460 G_("decrement of read-only parameter %qD"),
10461 G_("read-only parameter %qD use as %<asm%> output")),
10462 arg);
a1f90215 10463 else if (TREE_CODE (arg) == RESULT_DECL)
10464 {
10465 gcc_assert (c_dialect_cxx ());
f2697631 10466 error_at (loc, READONLY_MSG (G_("assignment of "
10467 "read-only named return value %qD"),
10468 G_("increment of "
10469 "read-only named return value %qD"),
10470 G_("decrement of "
10471 "read-only named return value %qD"),
10472 G_("read-only named return value %qD "
10473 "used as %<asm%>output")),
10474 arg);
a1f90215 10475 }
10476 else if (TREE_CODE (arg) == FUNCTION_DECL)
f2697631 10477 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10478 G_("increment of function %qD"),
10479 G_("decrement of function %qD"),
10480 G_("function %qD used as %<asm%> output")),
10481 arg);
a1f90215 10482 else
f2697631 10483 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10484 G_("increment of read-only location %qE"),
10485 G_("decrement of read-only location %qE"),
10486 G_("read-only location %qE used as %<asm%> output")),
10487 arg);
a1f90215 10488}
10489
e35976b1 10490/* Print an error message for an invalid lvalue. USE says
fdd84b77 10491 how the lvalue is being used and so selects the error message. LOC
10492 is the location for the error. */
ab6bb714 10493
e35976b1 10494void
fdd84b77 10495lvalue_error (location_t loc, enum lvalue_use use)
ab6bb714 10496{
e35976b1 10497 switch (use)
ab6bb714 10498 {
e35976b1 10499 case lv_assign:
fdd84b77 10500 error_at (loc, "lvalue required as left operand of assignment");
e35976b1 10501 break;
10502 case lv_increment:
fdd84b77 10503 error_at (loc, "lvalue required as increment operand");
e35976b1 10504 break;
10505 case lv_decrement:
fdd84b77 10506 error_at (loc, "lvalue required as decrement operand");
e35976b1 10507 break;
10508 case lv_addressof:
fdd84b77 10509 error_at (loc, "lvalue required as unary %<&%> operand");
e35976b1 10510 break;
10511 case lv_asm:
fdd84b77 10512 error_at (loc, "lvalue required in asm statement");
e35976b1 10513 break;
10514 default:
10515 gcc_unreachable ();
ab6bb714 10516 }
ab6bb714 10517}
b1bbc8e5 10518
10519/* Print an error message for an invalid indirection of type TYPE.
10520 ERRSTRING is the name of the operator for the indirection. */
10521
10522void
10523invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10524{
10525 switch (errstring)
10526 {
10527 case RO_NULL:
10528 gcc_assert (c_dialect_cxx ());
10529 error_at (loc, "invalid type argument (have %qT)", type);
10530 break;
10531 case RO_ARRAY_INDEXING:
10532 error_at (loc,
10533 "invalid type argument of array indexing (have %qT)",
10534 type);
10535 break;
10536 case RO_UNARY_STAR:
10537 error_at (loc,
10538 "invalid type argument of unary %<*%> (have %qT)",
10539 type);
10540 break;
10541 case RO_ARROW:
10542 error_at (loc,
10543 "invalid type argument of %<->%> (have %qT)",
10544 type);
10545 break;
7354a89b 10546 case RO_ARROW_STAR:
10547 error_at (loc,
10548 "invalid type argument of %<->*%> (have %qT)",
10549 type);
10550 break;
b1bbc8e5 10551 case RO_IMPLICIT_CONVERSION:
10552 error_at (loc,
10553 "invalid type argument of implicit conversion (have %qT)",
10554 type);
10555 break;
10556 default:
10557 gcc_unreachable ();
10558 }
10559}
c271bdb2 10560\f
10561/* *PTYPE is an incomplete array. Complete it with a domain based on
10562 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10563 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10564 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10565
10566int
10567complete_array_type (tree *ptype, tree initial_value, bool do_default)
10568{
10569 tree maxindex, type, main_type, elt, unqual_elt;
10570 int failure = 0, quals;
6753bca0 10571 hashval_t hashcode = 0;
f5298614 10572 bool overflow_p = false;
c271bdb2 10573
10574 maxindex = size_zero_node;
10575 if (initial_value)
10576 {
10577 if (TREE_CODE (initial_value) == STRING_CST)
10578 {
10579 int eltsize
10580 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10581 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10582 }
10583 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10584 {
f1f41a6c 10585 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
c271bdb2 10586
f1f41a6c 10587 if (vec_safe_is_empty (v))
c271bdb2 10588 {
10589 if (pedantic)
10590 failure = 3;
7542c3b4 10591 maxindex = ssize_int (-1);
c271bdb2 10592 }
10593 else
10594 {
10595 tree curindex;
c75b4594 10596 unsigned HOST_WIDE_INT cnt;
10597 constructor_elt *ce;
cee43f7e 10598 bool fold_p = false;
c271bdb2 10599
f1f41a6c 10600 if ((*v)[0].index)
f5298614 10601 maxindex = (*v)[0].index, fold_p = true;
10602
c271bdb2 10603 curindex = maxindex;
10604
f1f41a6c 10605 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
c271bdb2 10606 {
cee43f7e 10607 bool curfold_p = false;
c75b4594 10608 if (ce->index)
cee43f7e 10609 curindex = ce->index, curfold_p = true;
c271bdb2 10610 else
cee43f7e 10611 {
10612 if (fold_p)
f5298614 10613 {
10614 /* Since we treat size types now as ordinary
10615 unsigned types, we need an explicit overflow
10616 check. */
10617 tree orig = curindex;
10618 curindex = fold_convert (sizetype, curindex);
10619 overflow_p |= tree_int_cst_lt (curindex, orig);
10620 }
389dd41b 10621 curindex = size_binop (PLUS_EXPR, curindex,
10622 size_one_node);
cee43f7e 10623 }
c271bdb2 10624 if (tree_int_cst_lt (maxindex, curindex))
cee43f7e 10625 maxindex = curindex, fold_p = curfold_p;
c271bdb2 10626 }
f5298614 10627 if (fold_p)
10628 {
10629 tree orig = maxindex;
10630 maxindex = fold_convert (sizetype, maxindex);
10631 overflow_p |= tree_int_cst_lt (maxindex, orig);
10632 }
c271bdb2 10633 }
10634 }
10635 else
10636 {
10637 /* Make an error message unless that happened already. */
10638 if (initial_value != error_mark_node)
10639 failure = 1;
10640 }
10641 }
10642 else
10643 {
10644 failure = 2;
10645 if (!do_default)
10646 return failure;
10647 }
10648
10649 type = *ptype;
10650 elt = TREE_TYPE (type);
10651 quals = TYPE_QUALS (strip_array_types (elt));
10652 if (quals == 0)
10653 unqual_elt = elt;
10654 else
6d5d708e 10655 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
c271bdb2 10656
10657 /* Using build_distinct_type_copy and modifying things afterward instead
10658 of using build_array_type to create a new type preserves all of the
10659 TYPE_LANG_FLAG_? bits that the front end may have set. */
10660 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10661 TREE_TYPE (main_type) = unqual_elt;
783bb57e 10662 TYPE_DOMAIN (main_type)
10663 = build_range_type (TREE_TYPE (maxindex),
10664 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
c271bdb2 10665 layout_type (main_type);
10666
6753bca0 10667 /* Make sure we have the canonical MAIN_TYPE. */
10668 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
48e1416a 10669 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
6753bca0 10670 hashcode);
10671 main_type = type_hash_canon (hashcode, main_type);
10672
796735dc 10673 /* Fix the canonical type. */
10674 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10675 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10676 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10677 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10678 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10679 != TYPE_DOMAIN (main_type)))
48e1416a 10680 TYPE_CANONICAL (main_type)
796735dc 10681 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10682 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10683 else
10684 TYPE_CANONICAL (main_type) = main_type;
10685
c271bdb2 10686 if (quals == 0)
10687 type = main_type;
10688 else
10689 type = c_build_qualified_type (main_type, quals);
10690
4f5b8f2a 10691 if (COMPLETE_TYPE_P (type)
10692 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
f5298614 10693 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
4f5b8f2a 10694 {
10695 error ("size of array is too large");
10696 /* If we proceed with the array type as it is, we'll eventually
08f817b3 10697 crash in tree_to_[su]hwi(). */
4f5b8f2a 10698 type = error_mark_node;
10699 }
10700
c271bdb2 10701 *ptype = type;
10702 return failure;
10703}
ab6bb714 10704
93426222 10705/* Like c_mark_addressable but don't check register qualifier. */
10706void
10707c_common_mark_addressable_vec (tree t)
10708{
10709 while (handled_component_p (t))
10710 t = TREE_OPERAND (t, 0);
f48c7f4a 10711 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
93426222 10712 return;
10713 TREE_ADDRESSABLE (t) = 1;
10714}
10715
10716
b6a5fc45 10717\f
10718/* Used to help initialize the builtin-types.def table. When a type of
10719 the correct size doesn't exist, use error_mark_node instead of NULL.
10720 The later results in segfaults even when a decl using the type doesn't
10721 get invoked. */
10722
10723tree
10724builtin_type_for_size (int size, bool unsignedp)
10725{
a51edb4c 10726 tree type = c_common_type_for_size (size, unsignedp);
b6a5fc45 10727 return type ? type : error_mark_node;
10728}
10729
10730/* A helper function for resolve_overloaded_builtin in resolving the
10731 overloaded __sync_ builtins. Returns a positive power of 2 if the
10732 first operand of PARAMS is a pointer to a supported data type.
517906ff 10733 Returns 0 if an error is encountered.
10734 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
10735 built-ins. */
b6a5fc45 10736
10737static int
517906ff 10738sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch)
b6a5fc45 10739{
517906ff 10740 /* Type of the argument. */
10741 tree argtype;
10742 /* Type the argument points to. */
b6a5fc45 10743 tree type;
10744 int size;
10745
77454e6e 10746 if (vec_safe_is_empty (params))
b6a5fc45 10747 {
10748 error ("too few arguments to function %qE", function);
10749 return 0;
10750 }
10751
517906ff 10752 argtype = type = TREE_TYPE ((*params)[0]);
0d284870 10753 if (TREE_CODE (type) == ARRAY_TYPE)
10754 {
10755 /* Force array-to-pointer decay for C++. */
10756 gcc_assert (c_dialect_cxx());
10757 (*params)[0] = default_conversion ((*params)[0]);
10758 type = TREE_TYPE ((*params)[0]);
10759 }
b6a5fc45 10760 if (TREE_CODE (type) != POINTER_TYPE)
10761 goto incompatible;
10762
10763 type = TREE_TYPE (type);
10764 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10765 goto incompatible;
10766
517906ff 10767 if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
10768 goto incompatible;
10769
e913b5cd 10770 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
27213ba3 10771 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
b6a5fc45 10772 return size;
10773
10774 incompatible:
bbbbe8ab 10775 /* Issue the diagnostic only if the argument is valid, otherwise
10776 it would be redundant at best and could be misleading. */
10777 if (argtype != error_mark_node)
10778 error ("operand type %qT is incompatible with argument %d of %qE",
10779 argtype, 1, function);
b6a5fc45 10780 return 0;
10781}
10782
a0c938f0 10783/* A helper function for resolve_overloaded_builtin. Adds casts to
b6a5fc45 10784 PARAMS to make arguments match up with those of FUNCTION. Drops
10785 the variadic arguments at the end. Returns false if some error
10786 was encountered; true on success. */
10787
10788static bool
1cd6e20d 10789sync_resolve_params (location_t loc, tree orig_function, tree function,
f1f41a6c 10790 vec<tree, va_gc> *params, bool orig_format)
b6a5fc45 10791{
d0af78c5 10792 function_args_iterator iter;
b6a5fc45 10793 tree ptype;
b9c74b4d 10794 unsigned int parmnum;
b6a5fc45 10795
d0af78c5 10796 function_args_iter_init (&iter, TREE_TYPE (function));
b6a5fc45 10797 /* We've declared the implementation functions to use "volatile void *"
10798 as the pointer parameter, so we shouldn't get any complaints from the
10799 call to check_function_arguments what ever type the user used. */
d0af78c5 10800 function_args_iter_next (&iter);
f1f41a6c 10801 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
b560fabd 10802 ptype = TYPE_MAIN_VARIANT (ptype);
b6a5fc45 10803
10804 /* For the rest of the values, we need to cast these to FTYPE, so that we
10805 don't get warnings for passing pointer types, etc. */
b9c74b4d 10806 parmnum = 0;
d0af78c5 10807 while (1)
b6a5fc45 10808 {
d0af78c5 10809 tree val, arg_type;
10810
10811 arg_type = function_args_iter_cond (&iter);
10812 /* XXX void_type_node belies the abstraction. */
10813 if (arg_type == void_type_node)
10814 break;
b6a5fc45 10815
b9c74b4d 10816 ++parmnum;
f1f41a6c 10817 if (params->length () <= parmnum)
b6a5fc45 10818 {
1cd6e20d 10819 error_at (loc, "too few arguments to function %qE", orig_function);
b6a5fc45 10820 return false;
10821 }
10822
0f6a7cb7 10823 /* Only convert parameters if arg_type is unsigned integer type with
10824 new format sync routines, i.e. don't attempt to convert pointer
10825 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10826 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10827 kinds). */
10828 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
1cd6e20d 10829 {
10830 /* Ideally for the first conversion we'd use convert_for_assignment
10831 so that we get warnings for anything that doesn't match the pointer
10832 type. This isn't portable across the C and C++ front ends atm. */
f1f41a6c 10833 val = (*params)[parmnum];
1cd6e20d 10834 val = convert (ptype, val);
10835 val = convert (arg_type, val);
f1f41a6c 10836 (*params)[parmnum] = val;
1cd6e20d 10837 }
b6a5fc45 10838
d0af78c5 10839 function_args_iter_next (&iter);
b6a5fc45 10840 }
10841
1cd6e20d 10842 /* __atomic routines are not variadic. */
f1f41a6c 10843 if (!orig_format && params->length () != parmnum + 1)
1cd6e20d 10844 {
10845 error_at (loc, "too many arguments to function %qE", orig_function);
10846 return false;
10847 }
10848
b6a5fc45 10849 /* The definition of these primitives is variadic, with the remaining
10850 being "an optional list of variables protected by the memory barrier".
10851 No clue what that's supposed to mean, precisely, but we consider all
10852 call-clobbered variables to be protected so we're safe. */
f1f41a6c 10853 params->truncate (parmnum + 1);
b6a5fc45 10854
10855 return true;
10856}
10857
a0c938f0 10858/* A helper function for resolve_overloaded_builtin. Adds a cast to
b6a5fc45 10859 RESULT to make it match the type of the first pointer argument in
10860 PARAMS. */
10861
10862static tree
1cd6e20d 10863sync_resolve_return (tree first_param, tree result, bool orig_format)
b6a5fc45 10864{
b9c74b4d 10865 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
1cd6e20d 10866 tree rtype = TREE_TYPE (result);
10080eac 10867 ptype = TYPE_MAIN_VARIANT (ptype);
1cd6e20d 10868
10869 /* New format doesn't require casting unless the types are the same size. */
10870 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10871 return convert (ptype, result);
10872 else
10873 return result;
10874}
10875
10876/* This function verifies the PARAMS to generic atomic FUNCTION.
10877 It returns the size if all the parameters are the same size, otherwise
10878 0 is returned if the parameters are invalid. */
10879
10880static int
f1f41a6c 10881get_atomic_generic_size (location_t loc, tree function,
10882 vec<tree, va_gc> *params)
1cd6e20d 10883{
10884 unsigned int n_param;
10885 unsigned int n_model;
10886 unsigned int x;
10887 int size_0;
10888 tree type_0;
10889
10890 /* Determine the parameter makeup. */
10891 switch (DECL_FUNCTION_CODE (function))
10892 {
10893 case BUILT_IN_ATOMIC_EXCHANGE:
10894 n_param = 4;
10895 n_model = 1;
10896 break;
10897 case BUILT_IN_ATOMIC_LOAD:
10898 case BUILT_IN_ATOMIC_STORE:
10899 n_param = 3;
10900 n_model = 1;
10901 break;
10902 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10903 n_param = 6;
10904 n_model = 2;
10905 break;
10906 default:
1d581089 10907 gcc_unreachable ();
1cd6e20d 10908 }
10909
f1f41a6c 10910 if (vec_safe_length (params) != n_param)
1cd6e20d 10911 {
10912 error_at (loc, "incorrect number of arguments to function %qE", function);
10913 return 0;
10914 }
10915
10916 /* Get type of first parameter, and determine its size. */
f1f41a6c 10917 type_0 = TREE_TYPE ((*params)[0]);
0d284870 10918 if (TREE_CODE (type_0) == ARRAY_TYPE)
10919 {
10920 /* Force array-to-pointer decay for C++. */
10921 gcc_assert (c_dialect_cxx());
10922 (*params)[0] = default_conversion ((*params)[0]);
10923 type_0 = TREE_TYPE ((*params)[0]);
10924 }
1d581089 10925 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10926 {
10927 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10928 function);
10929 return 0;
10930 }
10931
10932 /* Types must be compile time constant sizes. */
10933 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
1cd6e20d 10934 {
1d581089 10935 error_at (loc,
10936 "argument 1 of %qE must be a pointer to a constant size type",
10937 function);
1cd6e20d 10938 return 0;
10939 }
1d581089 10940
e913b5cd 10941 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
1cd6e20d 10942
1d581089 10943 /* Zero size objects are not allowed. */
10944 if (size_0 == 0)
10945 {
10946 error_at (loc,
10947 "argument 1 of %qE must be a pointer to a nonzero size object",
10948 function);
10949 return 0;
10950 }
10951
1cd6e20d 10952 /* Check each other parameter is a pointer and the same size. */
10953 for (x = 0; x < n_param - n_model; x++)
10954 {
10955 int size;
f1f41a6c 10956 tree type = TREE_TYPE ((*params)[x]);
a04e8d62 10957 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
1cd6e20d 10958 if (n_param == 6 && x == 3)
10959 continue;
10960 if (!POINTER_TYPE_P (type))
10961 {
10962 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10963 function);
10964 return 0;
10965 }
1f6be080 10966 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10967 size = type_size ? tree_to_uhwi (type_size) : 0;
1cd6e20d 10968 if (size != size_0)
10969 {
10970 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10971 function);
10972 return 0;
10973 }
10974 }
10975
10976 /* Check memory model parameters for validity. */
10977 for (x = n_param - n_model ; x < n_param; x++)
10978 {
f1f41a6c 10979 tree p = (*params)[x];
1cd6e20d 10980 if (TREE_CODE (p) == INTEGER_CST)
10981 {
e913b5cd 10982 int i = tree_to_uhwi (p);
a372f7ca 10983 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
1cd6e20d 10984 {
10985 warning_at (loc, OPT_Winvalid_memory_model,
10986 "invalid memory model argument %d of %qE", x + 1,
10987 function);
1cd6e20d 10988 }
10989 }
10990 else
10991 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10992 {
10993 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10994 function);
10995 return 0;
10996 }
10997 }
10998
10999 return size_0;
11000}
11001
11002
11003/* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
11004 at the beginning of the parameter list PARAMS representing the size of the
11005 objects. This is to match the library ABI requirement. LOC is the location
11006 of the function call.
11007 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
11008 returned to allow the external call to be constructed. */
11009
11010static tree
11011add_atomic_size_parameter (unsigned n, location_t loc, tree function,
f1f41a6c 11012 vec<tree, va_gc> *params)
1cd6e20d 11013{
11014 tree size_node;
11015
11016 /* Insert a SIZE_T parameter as the first param. If there isn't
11017 enough space, allocate a new vector and recursively re-build with that. */
f1f41a6c 11018 if (!params->space (1))
1cd6e20d 11019 {
11020 unsigned int z, len;
f1f41a6c 11021 vec<tree, va_gc> *v;
1cd6e20d 11022 tree f;
11023
f1f41a6c 11024 len = params->length ();
11025 vec_alloc (v, len + 1);
5a672e62 11026 v->quick_push (build_int_cst (size_type_node, n));
1cd6e20d 11027 for (z = 0; z < len; z++)
f1f41a6c 11028 v->quick_push ((*params)[z]);
ec761d5a 11029 f = build_function_call_vec (loc, vNULL, function, v, NULL);
f1f41a6c 11030 vec_free (v);
1cd6e20d 11031 return f;
11032 }
11033
11034 /* Add the size parameter and leave as a function call for processing. */
11035 size_node = build_int_cst (size_type_node, n);
f1f41a6c 11036 params->quick_insert (0, size_node);
1cd6e20d 11037 return NULL_TREE;
11038}
11039
11040
a056826c 11041/* Return whether atomic operations for naturally aligned N-byte
11042 arguments are supported, whether inline or through libatomic. */
11043static bool
11044atomic_size_supported_p (int n)
11045{
11046 switch (n)
11047 {
11048 case 1:
11049 case 2:
11050 case 4:
11051 case 8:
11052 return true;
11053
11054 case 16:
11055 return targetm.scalar_mode_supported_p (TImode);
11056
11057 default:
11058 return false;
11059 }
11060}
11061
1cd6e20d 11062/* This will process an __atomic_exchange function call, determine whether it
11063 needs to be mapped to the _N variation, or turned into a library call.
11064 LOC is the location of the builtin call.
11065 FUNCTION is the DECL that has been invoked;
11066 PARAMS is the argument list for the call. The return value is non-null
11067 TRUE is returned if it is translated into the proper format for a call to the
11068 external library, and NEW_RETURN is set the tree for that function.
11069 FALSE is returned if processing for the _N variation is required, and
47ae02b7 11070 NEW_RETURN is set to the return value the result is copied into. */
1cd6e20d 11071static bool
11072resolve_overloaded_atomic_exchange (location_t loc, tree function,
f1f41a6c 11073 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11074{
11075 tree p0, p1, p2, p3;
11076 tree I_type, I_type_ptr;
11077 int n = get_atomic_generic_size (loc, function, params);
11078
1d581089 11079 /* Size of 0 is an error condition. */
11080 if (n == 0)
11081 {
11082 *new_return = error_mark_node;
11083 return true;
11084 }
11085
1cd6e20d 11086 /* If not a lock-free size, change to the library generic format. */
a056826c 11087 if (!atomic_size_supported_p (n))
1cd6e20d 11088 {
11089 *new_return = add_atomic_size_parameter (n, loc, function, params);
11090 return true;
11091 }
11092
11093 /* Otherwise there is a lockfree match, transform the call from:
11094 void fn(T* mem, T* desired, T* return, model)
11095 into
11096 *return = (T) (fn (In* mem, (In) *desired, model)) */
11097
f1f41a6c 11098 p0 = (*params)[0];
11099 p1 = (*params)[1];
11100 p2 = (*params)[2];
11101 p3 = (*params)[3];
1cd6e20d 11102
11103 /* Create pointer to appropriate size. */
11104 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11105 I_type_ptr = build_pointer_type (I_type);
11106
11107 /* Convert object pointer to required type. */
11108 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11109 (*params)[0] = p0;
1cd6e20d 11110 /* Convert new value to required type, and dereference it. */
11111 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11112 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 11113 (*params)[1] = p1;
1cd6e20d 11114
11115 /* Move memory model to the 3rd position, and end param list. */
f1f41a6c 11116 (*params)[2] = p3;
11117 params->truncate (3);
1cd6e20d 11118
11119 /* Convert return pointer and dereference it for later assignment. */
11120 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11121
11122 return false;
b6a5fc45 11123}
11124
1cd6e20d 11125
11126/* This will process an __atomic_compare_exchange function call, determine
11127 whether it needs to be mapped to the _N variation, or turned into a lib call.
11128 LOC is the location of the builtin call.
11129 FUNCTION is the DECL that has been invoked;
11130 PARAMS is the argument list for the call. The return value is non-null
11131 TRUE is returned if it is translated into the proper format for a call to the
11132 external library, and NEW_RETURN is set the tree for that function.
11133 FALSE is returned if processing for the _N variation is required. */
11134
11135static bool
11136resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
f1f41a6c 11137 vec<tree, va_gc> *params,
1cd6e20d 11138 tree *new_return)
11139{
11140 tree p0, p1, p2;
11141 tree I_type, I_type_ptr;
11142 int n = get_atomic_generic_size (loc, function, params);
11143
1d581089 11144 /* Size of 0 is an error condition. */
11145 if (n == 0)
11146 {
11147 *new_return = error_mark_node;
11148 return true;
11149 }
11150
1cd6e20d 11151 /* If not a lock-free size, change to the library generic format. */
a056826c 11152 if (!atomic_size_supported_p (n))
1cd6e20d 11153 {
11154 /* The library generic format does not have the weak parameter, so
11155 remove it from the param list. Since a parameter has been removed,
11156 we can be sure that there is room for the SIZE_T parameter, meaning
11157 there will not be a recursive rebuilding of the parameter list, so
11158 there is no danger this will be done twice. */
11159 if (n > 0)
11160 {
f1f41a6c 11161 (*params)[3] = (*params)[4];
11162 (*params)[4] = (*params)[5];
11163 params->truncate (5);
1cd6e20d 11164 }
11165 *new_return = add_atomic_size_parameter (n, loc, function, params);
11166 return true;
11167 }
11168
11169 /* Otherwise, there is a match, so the call needs to be transformed from:
11170 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11171 into
11172 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
ab2c1de8 11173
f1f41a6c 11174 p0 = (*params)[0];
11175 p1 = (*params)[1];
11176 p2 = (*params)[2];
1cd6e20d 11177
11178 /* Create pointer to appropriate size. */
11179 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11180 I_type_ptr = build_pointer_type (I_type);
11181
11182 /* Convert object pointer to required type. */
11183 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11184 (*params)[0] = p0;
1cd6e20d 11185
11186 /* Convert expected pointer to required type. */
11187 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
f1f41a6c 11188 (*params)[1] = p1;
1cd6e20d 11189
11190 /* Convert desired value to required type, and dereference it. */
11191 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11192 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
f1f41a6c 11193 (*params)[2] = p2;
1cd6e20d 11194
11195 /* The rest of the parameters are fine. NULL means no special return value
11196 processing.*/
11197 *new_return = NULL;
11198 return false;
11199}
11200
11201
11202/* This will process an __atomic_load function call, determine whether it
11203 needs to be mapped to the _N variation, or turned into a library call.
11204 LOC is the location of the builtin call.
11205 FUNCTION is the DECL that has been invoked;
11206 PARAMS is the argument list for the call. The return value is non-null
11207 TRUE is returned if it is translated into the proper format for a call to the
11208 external library, and NEW_RETURN is set the tree for that function.
11209 FALSE is returned if processing for the _N variation is required, and
47ae02b7 11210 NEW_RETURN is set to the return value the result is copied into. */
1cd6e20d 11211
11212static bool
11213resolve_overloaded_atomic_load (location_t loc, tree function,
f1f41a6c 11214 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11215{
11216 tree p0, p1, p2;
11217 tree I_type, I_type_ptr;
11218 int n = get_atomic_generic_size (loc, function, params);
11219
1d581089 11220 /* Size of 0 is an error condition. */
11221 if (n == 0)
11222 {
11223 *new_return = error_mark_node;
11224 return true;
11225 }
11226
1cd6e20d 11227 /* If not a lock-free size, change to the library generic format. */
a056826c 11228 if (!atomic_size_supported_p (n))
1cd6e20d 11229 {
11230 *new_return = add_atomic_size_parameter (n, loc, function, params);
11231 return true;
11232 }
11233
11234 /* Otherwise, there is a match, so the call needs to be transformed from:
11235 void fn(T* mem, T* return, model)
11236 into
11237 *return = (T) (fn ((In *) mem, model)) */
11238
f1f41a6c 11239 p0 = (*params)[0];
11240 p1 = (*params)[1];
11241 p2 = (*params)[2];
1cd6e20d 11242
11243 /* Create pointer to appropriate size. */
11244 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11245 I_type_ptr = build_pointer_type (I_type);
11246
11247 /* Convert object pointer to required type. */
11248 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11249 (*params)[0] = p0;
1cd6e20d 11250
11251 /* Move memory model to the 2nd position, and end param list. */
f1f41a6c 11252 (*params)[1] = p2;
11253 params->truncate (2);
1cd6e20d 11254
11255 /* Convert return pointer and dereference it for later assignment. */
11256 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11257
11258 return false;
11259}
11260
11261
11262/* This will process an __atomic_store function call, determine whether it
11263 needs to be mapped to the _N variation, or turned into a library call.
11264 LOC is the location of the builtin call.
11265 FUNCTION is the DECL that has been invoked;
11266 PARAMS is the argument list for the call. The return value is non-null
11267 TRUE is returned if it is translated into the proper format for a call to the
11268 external library, and NEW_RETURN is set the tree for that function.
11269 FALSE is returned if processing for the _N variation is required, and
47ae02b7 11270 NEW_RETURN is set to the return value the result is copied into. */
1cd6e20d 11271
11272static bool
11273resolve_overloaded_atomic_store (location_t loc, tree function,
f1f41a6c 11274 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11275{
11276 tree p0, p1;
11277 tree I_type, I_type_ptr;
11278 int n = get_atomic_generic_size (loc, function, params);
11279
1d581089 11280 /* Size of 0 is an error condition. */
11281 if (n == 0)
11282 {
11283 *new_return = error_mark_node;
11284 return true;
11285 }
11286
1cd6e20d 11287 /* If not a lock-free size, change to the library generic format. */
a056826c 11288 if (!atomic_size_supported_p (n))
1cd6e20d 11289 {
11290 *new_return = add_atomic_size_parameter (n, loc, function, params);
11291 return true;
11292 }
11293
11294 /* Otherwise, there is a match, so the call needs to be transformed from:
11295 void fn(T* mem, T* value, model)
11296 into
11297 fn ((In *) mem, (In) *value, model) */
11298
f1f41a6c 11299 p0 = (*params)[0];
11300 p1 = (*params)[1];
1cd6e20d 11301
11302 /* Create pointer to appropriate size. */
11303 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11304 I_type_ptr = build_pointer_type (I_type);
11305
11306 /* Convert object pointer to required type. */
11307 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11308 (*params)[0] = p0;
1cd6e20d 11309
11310 /* Convert new value to required type, and dereference it. */
11311 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11312 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 11313 (*params)[1] = p1;
1cd6e20d 11314
11315 /* The memory model is in the right spot already. Return is void. */
11316 *new_return = NULL_TREE;
11317
11318 return false;
11319}
11320
11321
b6a5fc45 11322/* Some builtin functions are placeholders for other expressions. This
11323 function should be called immediately after parsing the call expression
11324 before surrounding code has committed to the type of the expression.
11325
e60a6f7b 11326 LOC is the location of the builtin call.
11327
b6a5fc45 11328 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11329 PARAMS is the argument list for the call. The return value is non-null
11330 when expansion is complete, and null if normal processing should
11331 continue. */
11332
11333tree
f1f41a6c 11334resolve_overloaded_builtin (location_t loc, tree function,
11335 vec<tree, va_gc> *params)
b6a5fc45 11336{
11337 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
517906ff 11338
11339 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
11340 Those are not valid to call with a pointer to _Bool (or C++ bool)
11341 and so must be rejected. */
11342 bool fetch_op = true;
1cd6e20d 11343 bool orig_format = true;
11344 tree new_return = NULL_TREE;
11345
65441f6f 11346 switch (DECL_BUILT_IN_CLASS (function))
11347 {
11348 case BUILT_IN_NORMAL:
11349 break;
11350 case BUILT_IN_MD:
11351 if (targetm.resolve_overloaded_builtin)
e60a6f7b 11352 return targetm.resolve_overloaded_builtin (loc, function, params);
65441f6f 11353 else
a0c938f0 11354 return NULL_TREE;
65441f6f 11355 default:
11356 return NULL_TREE;
11357 }
a0c938f0 11358
65441f6f 11359 /* Handle BUILT_IN_NORMAL here. */
b6a5fc45 11360 switch (orig_code)
11361 {
1cd6e20d 11362 case BUILT_IN_ATOMIC_EXCHANGE:
11363 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11364 case BUILT_IN_ATOMIC_LOAD:
11365 case BUILT_IN_ATOMIC_STORE:
11366 {
11367 /* Handle these 4 together so that they can fall through to the next
11368 case if the call is transformed to an _N variant. */
11369 switch (orig_code)
11370 {
11371 case BUILT_IN_ATOMIC_EXCHANGE:
11372 {
11373 if (resolve_overloaded_atomic_exchange (loc, function, params,
11374 &new_return))
11375 return new_return;
11376 /* Change to the _N variant. */
11377 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11378 break;
11379 }
11380
11381 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11382 {
11383 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11384 params,
11385 &new_return))
11386 return new_return;
11387 /* Change to the _N variant. */
11388 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11389 break;
11390 }
11391 case BUILT_IN_ATOMIC_LOAD:
11392 {
11393 if (resolve_overloaded_atomic_load (loc, function, params,
11394 &new_return))
11395 return new_return;
11396 /* Change to the _N variant. */
11397 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11398 break;
11399 }
11400 case BUILT_IN_ATOMIC_STORE:
11401 {
11402 if (resolve_overloaded_atomic_store (loc, function, params,
11403 &new_return))
11404 return new_return;
11405 /* Change to the _N variant. */
11406 orig_code = BUILT_IN_ATOMIC_STORE_N;
11407 break;
11408 }
11409 default:
11410 gcc_unreachable ();
11411 }
11412 /* Fallthrough to the normal processing. */
11413 }
11414 case BUILT_IN_ATOMIC_EXCHANGE_N:
11415 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11416 case BUILT_IN_ATOMIC_LOAD_N:
11417 case BUILT_IN_ATOMIC_STORE_N:
517906ff 11418 {
11419 fetch_op = false;
11420 /* Fallthrough to further processing. */
11421 }
1cd6e20d 11422 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11423 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11424 case BUILT_IN_ATOMIC_AND_FETCH_N:
11425 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11426 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11427 case BUILT_IN_ATOMIC_OR_FETCH_N:
11428 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11429 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11430 case BUILT_IN_ATOMIC_FETCH_AND_N:
11431 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11432 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11433 case BUILT_IN_ATOMIC_FETCH_OR_N:
11434 {
11435 orig_format = false;
11436 /* Fallthru for parameter processing. */
11437 }
2797f13a 11438 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11439 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11440 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11441 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11442 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11443 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11444 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11445 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11446 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11447 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11448 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11449 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11450 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11451 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11452 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11453 case BUILT_IN_SYNC_LOCK_RELEASE_N:
b6a5fc45 11454 {
517906ff 11455 /* The following are not _FETCH_OPs and must be accepted with
11456 pointers to _Bool (or C++ bool). */
11457 if (fetch_op)
11458 fetch_op =
11459 (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11460 && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
11461 && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
11462 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
11463
11464 int n = sync_resolve_size (function, params, fetch_op);
b9c74b4d 11465 tree new_function, first_param, result;
b9a16870 11466 enum built_in_function fncode;
b6a5fc45 11467
11468 if (n == 0)
11469 return error_mark_node;
11470
b9a16870 11471 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11472 new_function = builtin_decl_explicit (fncode);
1cd6e20d 11473 if (!sync_resolve_params (loc, function, new_function, params,
11474 orig_format))
b6a5fc45 11475 return error_mark_node;
11476
f1f41a6c 11477 first_param = (*params)[0];
ec761d5a 11478 result = build_function_call_vec (loc, vNULL, new_function, params,
11479 NULL);
1cd6e20d 11480 if (result == error_mark_node)
11481 return result;
2797f13a 11482 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
1cd6e20d 11483 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11484 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11485 result = sync_resolve_return (first_param, result, orig_format);
b6a5fc45 11486
92e7ab1e 11487 if (fetch_op)
11488 /* Prevent -Wunused-value warning. */
11489 TREE_USED (result) = true;
11490
1cd6e20d 11491 /* If new_return is set, assign function to that expr and cast the
11492 result to void since the generic interface returned void. */
11493 if (new_return)
11494 {
11495 /* Cast function result from I{1,2,4,8,16} to the required type. */
11496 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11497 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11498 result);
11499 TREE_SIDE_EFFECTS (result) = 1;
11500 protected_set_expr_location (result, loc);
11501 result = convert (void_type_node, result);
11502 }
b6a5fc45 11503 return result;
11504 }
11505
11506 default:
65441f6f 11507 return NULL_TREE;
b6a5fc45 11508 }
11509}
11510
73437615 11511/* vector_types_compatible_elements_p is used in type checks of vectors
11512 values used as operands of binary operators. Where it returns true, and
11513 the other checks of the caller succeed (being vector types in he first
11514 place, and matching number of elements), we can just treat the types
11515 as essentially the same.
11516 Contrast with vector_targets_convertible_p, which is used for vector
11517 pointer types, and vector_types_convertible_p, which will allow
11518 language-specific matches under the control of flag_lax_vector_conversions,
11519 and might still require a conversion. */
11520/* True if vector types T1 and T2 can be inputs to the same binary
11521 operator without conversion.
11522 We don't check the overall vector size here because some of our callers
11523 want to give different error messages when the vectors are compatible
11524 except for the element count. */
11525
491255f5 11526bool
73437615 11527vector_types_compatible_elements_p (tree t1, tree t2)
491255f5 11528{
73437615 11529 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11530 t1 = TREE_TYPE (t1);
11531 t2 = TREE_TYPE (t2);
11532
491255f5 11533 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11534
9421ebb9 11535 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11536 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11537 || c2 == FIXED_POINT_TYPE));
491255f5 11538
73437615 11539 t1 = c_common_signed_type (t1);
11540 t2 = c_common_signed_type (t2);
491255f5 11541 /* Equality works here because c_common_signed_type uses
11542 TYPE_MAIN_VARIANT. */
73437615 11543 if (t1 == t2)
11544 return true;
11545 if (opaque && c1 == c2
11546 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11547 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11548 return true;
11549 return false;
491255f5 11550}
11551
be7350e7 11552/* Check for missing format attributes on function pointers. LTYPE is
11553 the new type or left-hand side type. RTYPE is the old type or
11554 right-hand side type. Returns TRUE if LTYPE is missing the desired
11555 attribute. */
11556
11557bool
11558check_missing_format_attribute (tree ltype, tree rtype)
11559{
11560 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11561 tree ra;
11562
11563 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11564 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11565 break;
11566 if (ra)
11567 {
11568 tree la;
11569 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11570 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11571 break;
11572 return !la;
11573 }
11574 else
11575 return false;
11576}
11577
2840aae4 11578/* Subscripting with type char is likely to lose on a machine where
11579 chars are signed. So warn on any machine, but optionally. Don't
11580 warn for unsigned char since that type is safe. Don't warn for
11581 signed char because anyone who uses that must have done so
11582 deliberately. Furthermore, we reduce the false positive load by
11583 warning only for non-constant value of type char. */
11584
11585void
92b63884 11586warn_array_subscript_with_type_char (location_t loc, tree index)
2840aae4 11587{
11588 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11589 && TREE_CODE (index) != INTEGER_CST)
92b63884 11590 warning_at (loc, OPT_Wchar_subscripts,
11591 "array subscript has type %<char%>");
2840aae4 11592}
11593
e534436e 11594/* Implement -Wparentheses for the unexpected C precedence rules, to
11595 cover cases like x + y << z which readers are likely to
11596 misinterpret. We have seen an expression in which CODE is a binary
82012ffe 11597 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11598 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11599 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11600 expression was not formed using a binary or unary operator, or it
11601 was enclosed in parentheses. */
e534436e 11602
11603void
b0e7825e 11604warn_about_parentheses (location_t loc, enum tree_code code,
269f7979 11605 enum tree_code code_left, tree arg_left,
82012ffe 11606 enum tree_code code_right, tree arg_right)
e534436e 11607{
11608 if (!warn_parentheses)
11609 return;
11610
82012ffe 11611 /* This macro tests that the expression ARG with original tree code
11612 CODE appears to be a boolean expression. or the result of folding a
11613 boolean expression. */
11614#define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11615 (truth_value_p (TREE_CODE (ARG)) \
11616 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11617 /* Folding may create 0 or 1 integers from other expressions. */ \
11618 || ((CODE) != INTEGER_CST \
11619 && (integer_onep (ARG) || integer_zerop (ARG))))
11620
48e1416a 11621 switch (code)
e534436e 11622 {
82012ffe 11623 case LSHIFT_EXPR:
b0e7825e 11624 if (code_left == PLUS_EXPR)
11625 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11626 "suggest parentheses around %<+%> inside %<<<%>");
11627 else if (code_right == PLUS_EXPR)
11628 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11629 "suggest parentheses around %<+%> inside %<<<%>");
11630 else if (code_left == MINUS_EXPR)
11631 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11632 "suggest parentheses around %<-%> inside %<<<%>");
11633 else if (code_right == MINUS_EXPR)
11634 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11635 "suggest parentheses around %<-%> inside %<<<%>");
82012ffe 11636 return;
e534436e 11637
82012ffe 11638 case RSHIFT_EXPR:
b0e7825e 11639 if (code_left == PLUS_EXPR)
11640 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11641 "suggest parentheses around %<+%> inside %<>>%>");
11642 else if (code_right == PLUS_EXPR)
11643 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11644 "suggest parentheses around %<+%> inside %<>>%>");
11645 else if (code_left == MINUS_EXPR)
11646 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11647 "suggest parentheses around %<-%> inside %<>>%>");
11648 else if (code_right == MINUS_EXPR)
11649 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11650 "suggest parentheses around %<-%> inside %<>>%>");
82012ffe 11651 return;
e534436e 11652
82012ffe 11653 case TRUTH_ORIF_EXPR:
b0e7825e 11654 if (code_left == TRUTH_ANDIF_EXPR)
11655 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11656 "suggest parentheses around %<&&%> within %<||%>");
11657 else if (code_right == TRUTH_ANDIF_EXPR)
11658 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11659 "suggest parentheses around %<&&%> within %<||%>");
82012ffe 11660 return;
11661
11662 case BIT_IOR_EXPR:
e534436e 11663 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
b0e7825e 11664 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11665 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11666 "suggest parentheses around arithmetic in operand of %<|%>");
11667 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11668 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11669 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11670 "suggest parentheses around arithmetic in operand of %<|%>");
e534436e 11671 /* Check cases like x|y==z */
b0e7825e 11672 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11673 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11674 "suggest parentheses around comparison in operand of %<|%>");
11675 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11676 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11677 "suggest parentheses around comparison in operand of %<|%>");
11678 /* Check cases like !x | y */
11679 else if (code_left == TRUTH_NOT_EXPR
11680 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 11681 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11682 "suggest parentheses around operand of "
11683 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
82012ffe 11684 return;
e534436e 11685
82012ffe 11686 case BIT_XOR_EXPR:
e534436e 11687 if (code_left == BIT_AND_EXPR
b0e7825e 11688 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11689 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11690 "suggest parentheses around arithmetic in operand of %<^%>");
11691 else if (code_right == BIT_AND_EXPR
11692 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11693 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11694 "suggest parentheses around arithmetic in operand of %<^%>");
e534436e 11695 /* Check cases like x^y==z */
b0e7825e 11696 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11697 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11698 "suggest parentheses around comparison in operand of %<^%>");
11699 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11700 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11701 "suggest parentheses around comparison in operand of %<^%>");
11702 return;
e534436e 11703
82012ffe 11704 case BIT_AND_EXPR:
b0e7825e 11705 if (code_left == PLUS_EXPR)
11706 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11707 "suggest parentheses around %<+%> in operand of %<&%>");
11708 else if (code_right == PLUS_EXPR)
11709 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11710 "suggest parentheses around %<+%> in operand of %<&%>");
b0e7825e 11711 else if (code_left == MINUS_EXPR)
11712 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11713 "suggest parentheses around %<-%> in operand of %<&%>");
11714 else if (code_right == MINUS_EXPR)
11715 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11716 "suggest parentheses around %<-%> in operand of %<&%>");
e534436e 11717 /* Check cases like x&y==z */
b0e7825e 11718 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11719 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11720 "suggest parentheses around comparison in operand of %<&%>");
11721 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11722 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11723 "suggest parentheses around comparison in operand of %<&%>");
11724 /* Check cases like !x & y */
11725 else if (code_left == TRUTH_NOT_EXPR
11726 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 11727 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11728 "suggest parentheses around operand of "
11729 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
82012ffe 11730 return;
e534436e 11731
82012ffe 11732 case EQ_EXPR:
b0e7825e 11733 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11734 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11735 "suggest parentheses around comparison in operand of %<==%>");
11736 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11737 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11738 "suggest parentheses around comparison in operand of %<==%>");
11739 return;
11740 case NE_EXPR:
b0e7825e 11741 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11742 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11743 "suggest parentheses around comparison in operand of %<!=%>");
11744 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11745 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11746 "suggest parentheses around comparison in operand of %<!=%>");
11747 return;
11748
11749 default:
b0e7825e 11750 if (TREE_CODE_CLASS (code) == tcc_comparison)
11751 {
11752 if (TREE_CODE_CLASS (code_left) == tcc_comparison
269f7979 11753 && code_left != NE_EXPR && code_left != EQ_EXPR
11754 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
b0e7825e 11755 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11756 "comparisons like %<X<=Y<=Z%> do not "
11757 "have their mathematical meaning");
11758 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
269f7979 11759 && code_right != NE_EXPR && code_right != EQ_EXPR
b0e7825e 11760 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11761 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11762 "comparisons like %<X<=Y<=Z%> do not "
11763 "have their mathematical meaning");
11764 }
82012ffe 11765 return;
6ce0c450 11766 }
82012ffe 11767#undef NOT_A_BOOLEAN_EXPR_P
e534436e 11768}
11769
92fccaaa 11770/* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11771
11772void
11773warn_for_unused_label (tree label)
11774{
11775 if (!TREE_USED (label))
11776 {
11777 if (DECL_INITIAL (label))
11778 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11779 else
11780 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11781 }
11782}
2840aae4 11783
b6889cb0 11784/* Warn for division by zero according to the value of DIVISOR. LOC
11785 is the location of the division operator. */
f092582b 11786
11787void
b6889cb0 11788warn_for_div_by_zero (location_t loc, tree divisor)
f092582b 11789{
9421ebb9 11790 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11791 about division by zero. Do not issue a warning if DIVISOR has a
f092582b 11792 floating-point type, since we consider 0.0/0.0 a valid way of
11793 generating a NaN. */
48d94ede 11794 if (c_inhibit_evaluation_warnings == 0
9421ebb9 11795 && (integer_zerop (divisor) || fixed_zerop (divisor)))
b6889cb0 11796 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
f092582b 11797}
11798
40385231 11799/* Warn for patterns where memset appears to be used incorrectly. The
11800 warning location should be LOC. ARG0, and ARG2 are the first and
11801 last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for
11802 each argument that was a literal zero. */
11803
11804void
11805warn_for_memset (location_t loc, tree arg0, tree arg2,
11806 int literal_zero_mask)
11807{
11808 if (warn_memset_transposed_args
11809 && integer_zerop (arg2)
11810 && (literal_zero_mask & (1 << 2)) != 0
11811 && (literal_zero_mask & (1 << 1)) == 0)
11812 warning_at (loc, OPT_Wmemset_transposed_args,
11813 "%<memset%> used with constant zero length "
11814 "parameter; this could be due to transposed "
11815 "parameters");
11816
11817 if (warn_memset_elt_size && TREE_CODE (arg2) == INTEGER_CST)
11818 {
11819 STRIP_NOPS (arg0);
11820 if (TREE_CODE (arg0) == ADDR_EXPR)
11821 arg0 = TREE_OPERAND (arg0, 0);
11822 tree type = TREE_TYPE (arg0);
11823 if (TREE_CODE (type) == ARRAY_TYPE)
11824 {
11825 tree elt_type = TREE_TYPE (type);
11826 tree domain = TYPE_DOMAIN (type);
11827 if (!integer_onep (TYPE_SIZE_UNIT (elt_type))
025dd1e0 11828 && domain != NULL_TREE
40385231 11829 && TYPE_MAXVAL (domain)
11830 && TYPE_MINVAL (domain)
11831 && integer_zerop (TYPE_MINVAL (domain))
11832 && integer_onep (fold_build2 (MINUS_EXPR, domain,
11833 arg2,
11834 TYPE_MAXVAL (domain))))
11835 warning_at (loc, OPT_Wmemset_elt_size,
11836 "%<memset%> used with length equal to "
11837 "number of elements without multiplication "
11838 "by element size");
11839 }
11840 }
11841}
11842
13869a99 11843/* Subroutine of build_binary_op. Give warnings for comparisons
11844 between signed and unsigned quantities that may fail. Do the
11845 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11846 so that casts will be considered, but default promotions won't
8e70fb09 11847 be.
11848
11849 LOCATION is the location of the comparison operator.
13869a99 11850
11851 The arguments of this function map directly to local variables
11852 of build_binary_op. */
11853
48e1416a 11854void
8e70fb09 11855warn_for_sign_compare (location_t location,
48e1416a 11856 tree orig_op0, tree orig_op1,
11857 tree op0, tree op1,
13869a99 11858 tree result_type, enum tree_code resultcode)
11859{
11860 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11861 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11862 int unsignedp0, unsignedp1;
48e1416a 11863
13869a99 11864 /* In C++, check for comparison of different enum types. */
11865 if (c_dialect_cxx()
11866 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11867 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11868 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
895b662f 11869 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
13869a99 11870 {
8e70fb09 11871 warning_at (location,
11872 OPT_Wsign_compare, "comparison between types %qT and %qT",
11873 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
13869a99 11874 }
11875
11876 /* Do not warn if the comparison is being done in a signed type,
11877 since the signed type will only be chosen if it can represent
11878 all the values of the unsigned type. */
11879 if (!TYPE_UNSIGNED (result_type))
11880 /* OK */;
11881 /* Do not warn if both operands are unsigned. */
11882 else if (op0_signed == op1_signed)
11883 /* OK */;
11884 else
11885 {
895b662f 11886 tree sop, uop, base_type;
13869a99 11887 bool ovf;
895b662f 11888
13869a99 11889 if (op0_signed)
11890 sop = orig_op0, uop = orig_op1;
48e1416a 11891 else
13869a99 11892 sop = orig_op1, uop = orig_op0;
11893
48e1416a 11894 STRIP_TYPE_NOPS (sop);
13869a99 11895 STRIP_TYPE_NOPS (uop);
895b662f 11896 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11897 ? TREE_TYPE (result_type) : result_type);
13869a99 11898
11899 /* Do not warn if the signed quantity is an unsuffixed integer
11900 literal (or some static constant expression involving such
11901 literals or a conditional expression involving such literals)
11902 and it is non-negative. */
11903 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11904 /* OK */;
11905 /* Do not warn if the comparison is an equality operation, the
11906 unsigned quantity is an integral constant, and it would fit
11907 in the result if the result were signed. */
11908 else if (TREE_CODE (uop) == INTEGER_CST
11909 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
895b662f 11910 && int_fits_type_p (uop, c_common_signed_type (base_type)))
13869a99 11911 /* OK */;
11912 /* In C, do not warn if the unsigned quantity is an enumeration
11913 constant and its maximum value would fit in the result if the
11914 result were signed. */
11915 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11916 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11917 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
895b662f 11918 c_common_signed_type (base_type)))
13869a99 11919 /* OK */;
48e1416a 11920 else
8e70fb09 11921 warning_at (location,
48e1416a 11922 OPT_Wsign_compare,
8e70fb09 11923 "comparison between signed and unsigned integer expressions");
13869a99 11924 }
48e1416a 11925
13869a99 11926 /* Warn if two unsigned values are being compared in a size larger
11927 than their original size, and one (and only one) is the result of
11928 a `~' operator. This comparison will always fail.
48e1416a 11929
13869a99 11930 Also warn if one operand is a constant, and the constant does not
11931 have all bits set that are set in the ~ operand when it is
11932 extended. */
11933
7f506bca 11934 op0 = c_common_get_narrower (op0, &unsignedp0);
11935 op1 = c_common_get_narrower (op1, &unsignedp1);
48e1416a 11936
13869a99 11937 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11938 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11939 {
11940 if (TREE_CODE (op0) == BIT_NOT_EXPR)
7f506bca 11941 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
13869a99 11942 if (TREE_CODE (op1) == BIT_NOT_EXPR)
7f506bca 11943 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
13869a99 11944
e913b5cd 11945 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
13869a99 11946 {
11947 tree primop;
11948 HOST_WIDE_INT constant, mask;
11949 int unsignedp;
11950 unsigned int bits;
48e1416a 11951
e913b5cd 11952 if (tree_fits_shwi_p (op0))
13869a99 11953 {
11954 primop = op1;
11955 unsignedp = unsignedp1;
e913b5cd 11956 constant = tree_to_shwi (op0);
13869a99 11957 }
11958 else
11959 {
11960 primop = op0;
11961 unsignedp = unsignedp0;
e913b5cd 11962 constant = tree_to_shwi (op1);
13869a99 11963 }
48e1416a 11964
13869a99 11965 bits = TYPE_PRECISION (TREE_TYPE (primop));
11966 if (bits < TYPE_PRECISION (result_type)
11967 && bits < HOST_BITS_PER_LONG && unsignedp)
11968 {
85f5e2ee 11969 mask = (~ (unsigned HOST_WIDE_INT) 0) << bits;
13869a99 11970 if ((mask & constant) != mask)
11971 {
11972 if (constant == 0)
76fdceeb 11973 warning_at (location, OPT_Wsign_compare,
11974 "promoted ~unsigned is always non-zero");
13869a99 11975 else
48e1416a 11976 warning_at (location, OPT_Wsign_compare,
8e70fb09 11977 "comparison of promoted ~unsigned with constant");
13869a99 11978 }
11979 }
11980 }
11981 else if (unsignedp0 && unsignedp1
11982 && (TYPE_PRECISION (TREE_TYPE (op0))
11983 < TYPE_PRECISION (result_type))
11984 && (TYPE_PRECISION (TREE_TYPE (op1))
11985 < TYPE_PRECISION (result_type)))
8e70fb09 11986 warning_at (location, OPT_Wsign_compare,
13869a99 11987 "comparison of promoted ~unsigned with unsigned");
11988 }
11989}
11990
5ba33bf4 11991/* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11992 type via c_common_type. If -Wdouble-promotion is in use, and the
11993 conditions for warning have been met, issue a warning. GMSGID is
11994 the warning message. It must have two %T specifiers for the type
11995 that was converted (generally "float") and the type to which it was
11996 converted (generally "double), respectively. LOC is the location
11997 to which the awrning should refer. */
11998
11999void
12000do_warn_double_promotion (tree result_type, tree type1, tree type2,
12001 const char *gmsgid, location_t loc)
12002{
12003 tree source_type;
12004
12005 if (!warn_double_promotion)
12006 return;
12007 /* If the conversion will not occur at run-time, there is no need to
12008 warn about it. */
12009 if (c_inhibit_evaluation_warnings)
12010 return;
12011 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
12012 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
12013 return;
12014 if (TYPE_MAIN_VARIANT (type1) == float_type_node
12015 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
12016 source_type = type1;
12017 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
12018 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
12019 source_type = type2;
12020 else
12021 return;
12022 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
12023}
12024
0949f227 12025/* Possibly warn about unused parameters. */
12026
12027void
12028do_warn_unused_parameter (tree fn)
12029{
12030 tree decl;
12031
12032 for (decl = DECL_ARGUMENTS (fn);
12033 decl; decl = DECL_CHAIN (decl))
12034 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
12035 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
12036 && !TREE_NO_WARNING (decl))
12037 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
12038 "unused parameter %qD", decl);
12039}
12040
12041
41771881 12042/* Setup a TYPE_DECL node as a typedef representation.
12043
12044 X is a TYPE_DECL for a typedef statement. Create a brand new
12045 ..._TYPE node (which will be just a variant of the existing
12046 ..._TYPE node with identical properties) and then install X
12047 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
12048
12049 The whole point here is to end up with a situation where each
12050 and every ..._TYPE node the compiler creates will be uniquely
12051 associated with AT MOST one node representing a typedef name.
12052 This way, even though the compiler substitutes corresponding
12053 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
12054 early on, later parts of the compiler can always do the reverse
12055 translation and get back the corresponding typedef name. For
12056 example, given:
ab2c1de8 12057
41771881 12058 typedef struct S MY_TYPE;
12059 MY_TYPE object;
12060
12061 Later parts of the compiler might only know that `object' was of
12062 type `struct S' if it were not for code just below. With this
12063 code however, later parts of the compiler see something like:
12064
12065 struct S' == struct S
12066 typedef struct S' MY_TYPE;
12067 struct S' object;
12068
12069 And they can then deduce (from the node for type struct S') that
12070 the original object declaration was:
12071
12072 MY_TYPE object;
12073
12074 Being able to do this is important for proper support of protoize,
12075 and also for generating precise symbolic debugging information
12076 which takes full account of the programmer's (typedef) vocabulary.
12077
12078 Obviously, we don't want to generate a duplicate ..._TYPE node if
12079 the TYPE_DECL node that we are now processing really represents a
12080 standard built-in type. */
12081
12082void
12083set_underlying_type (tree x)
12084{
12085 if (x == error_mark_node)
12086 return;
12087 if (DECL_IS_BUILTIN (x))
12088 {
12089 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12090 TYPE_NAME (TREE_TYPE (x)) = x;
12091 }
12092 else if (TREE_TYPE (x) != error_mark_node
12093 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12094 {
12095 tree tt = TREE_TYPE (x);
12096 DECL_ORIGINAL_TYPE (x) = tt;
12097 tt = build_variant_type_copy (tt);
12098 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12099 TYPE_NAME (tt) = x;
12100 TREE_USED (tt) = TREE_USED (x);
12101 TREE_TYPE (x) = tt;
12102 }
12103}
12104
1a4c44c5 12105/* Record the types used by the current global variable declaration
12106 being parsed, so that we can decide later to emit their debug info.
12107 Those types are in types_used_by_cur_var_decl, and we are going to
12108 store them in the types_used_by_vars_hash hash table.
12109 DECL is the declaration of the global variable that has been parsed. */
12110
12111void
12112record_types_used_by_current_var_decl (tree decl)
12113{
12114 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12115
f1f41a6c 12116 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
1a4c44c5 12117 {
f1f41a6c 12118 tree type = types_used_by_cur_var_decl->pop ();
aef48c9a 12119 types_used_by_var_decl_insert (type, decl);
1a4c44c5 12120 }
12121}
12122
a4e3ffad 12123/* If DECL is a typedef that is declared in the current function,
12124 record it for the purpose of -Wunused-local-typedefs. */
12125
12126void
12127record_locally_defined_typedef (tree decl)
12128{
12129 struct c_language_function *l;
12130
12131 if (!warn_unused_local_typedefs
12132 || cfun == NULL
12133 /* if this is not a locally defined typedef then we are not
12134 interested. */
12135 || !is_typedef_decl (decl)
12136 || !decl_function_context (decl))
12137 return;
12138
12139 l = (struct c_language_function *) cfun->language;
f1f41a6c 12140 vec_safe_push (l->local_typedefs, decl);
a4e3ffad 12141}
12142
12143/* If T is a TYPE_DECL declared locally, mark it as used. */
12144
12145void
12146maybe_record_typedef_use (tree t)
12147{
12148 if (!is_typedef_decl (t))
12149 return;
12150
12151 TREE_USED (t) = true;
12152}
12153
12154/* Warn if there are some unused locally defined typedefs in the
12155 current function. */
12156
12157void
12158maybe_warn_unused_local_typedefs (void)
12159{
12160 int i;
12161 tree decl;
12162 /* The number of times we have emitted -Wunused-local-typedefs
12163 warnings. If this is different from errorcount, that means some
12164 unrelated errors have been issued. In which case, we'll avoid
12165 emitting "unused-local-typedefs" warnings. */
12166 static int unused_local_typedefs_warn_count;
12167 struct c_language_function *l;
12168
12169 if (cfun == NULL)
12170 return;
12171
12172 if ((l = (struct c_language_function *) cfun->language) == NULL)
12173 return;
12174
12175 if (warn_unused_local_typedefs
12176 && errorcount == unused_local_typedefs_warn_count)
12177 {
f1f41a6c 12178 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
a4e3ffad 12179 if (!TREE_USED (decl))
12180 warning_at (DECL_SOURCE_LOCATION (decl),
12181 OPT_Wunused_local_typedefs,
12182 "typedef %qD locally defined but not used", decl);
12183 unused_local_typedefs_warn_count = errorcount;
12184 }
12185
f1f41a6c 12186 vec_free (l->local_typedefs);
a4e3ffad 12187}
12188
78bf4156 12189/* Warn about boolean expression compared with an integer value different
12190 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12191 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12192 are the operands of the comparison. The caller must ensure that
12193 either operand is a boolean expression. */
12194
12195void
12196maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12197 tree op1)
12198{
12199 if (TREE_CODE_CLASS (code) != tcc_comparison)
12200 return;
12201
9c691dbd 12202 tree f, cst;
12203 if (f = fold_for_warn (op0),
12204 TREE_CODE (f) == INTEGER_CST)
12205 cst = op0 = f;
12206 else if (f = fold_for_warn (op1),
12207 TREE_CODE (f) == INTEGER_CST)
12208 cst = op1 = f;
12209 else
78bf4156 12210 return;
12211
12212 if (!integer_zerop (cst) && !integer_onep (cst))
12213 {
a720ab1c 12214 int sign = (TREE_CODE (op0) == INTEGER_CST
12215 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
78bf4156 12216 if (code == EQ_EXPR
12217 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12218 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12219 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12220 "with boolean expression is always false", cst);
12221 else
12222 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12223 "with boolean expression is always true", cst);
12224 }
a720ab1c 12225 else if (integer_zerop (cst) || integer_onep (cst))
12226 {
14744a16 12227 /* If the non-constant operand isn't of a boolean type, we
12228 don't want to warn here. */
12229 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12230 /* Handle booleans promoted to integers. */
12231 if (CONVERT_EXPR_P (noncst)
12232 && TREE_TYPE (noncst) == integer_type_node
12233 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
12234 /* Warn. */;
12235 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12236 && !truth_value_p (TREE_CODE (noncst)))
12237 return;
a720ab1c 12238 /* Do some magic to get the right diagnostics. */
12239 bool flag = TREE_CODE (op0) == INTEGER_CST;
12240 flag = integer_zerop (cst) ? flag : !flag;
12241 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12242 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12243 "with boolean expression is always true", cst);
12244 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12245 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12246 "with boolean expression is always false", cst);
12247 }
78bf4156 12248}
12249
8d669e79 12250/* Warn if signed left shift overflows. We don't warn
12251 about left-shifting 1 into the sign bit in C++14; cf.
12252 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
12253 LOC is a location of the shift; OP0 and OP1 are the operands.
12254 Return true if an overflow is detected, false otherwise. */
12255
12256bool
12257maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
12258{
12259 if (TREE_CODE (op0) != INTEGER_CST
12260 || TREE_CODE (op1) != INTEGER_CST)
12261 return false;
12262
12263 tree type0 = TREE_TYPE (op0);
12264 unsigned int prec0 = TYPE_PRECISION (type0);
12265
12266 /* Left-hand operand must be signed. */
12267 if (TYPE_UNSIGNED (type0))
12268 return false;
12269
f4809955 12270 unsigned int min_prec = (wi::min_precision (op0, SIGNED)
12271 + TREE_INT_CST_LOW (op1));
99e82b02 12272 /* Handle the case of left-shifting 1 into the sign bit.
12273 * However, shifting 1 _out_ of the sign bit, as in
12274 * INT_MIN << 1, is considered an overflow.
12275 */
12276 if (!tree_int_cst_sign_bit(op0) && min_prec == prec0 + 1)
8d669e79 12277 {
12278 /* Never warn for C++14 onwards. */
12279 if (cxx_dialect >= cxx14)
12280 return false;
12281 /* Otherwise only if -Wshift-overflow=2. But return
12282 true to signal an overflow for the sake of integer
12283 constant expressions. */
12284 if (warn_shift_overflow < 2)
12285 return true;
12286 }
12287
8d669e79 12288 bool overflowed = min_prec > prec0;
12289 if (overflowed && c_inhibit_evaluation_warnings == 0)
12290 warning_at (loc, OPT_Wshift_overflow_,
12291 "result of %qE requires %u bits to represent, "
12292 "but %qT only has %u bits",
12293 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
12294 min_prec, type0, prec0);
12295
12296 return overflowed;
12297}
12298
f352a3fb 12299/* The C and C++ parsers both use vectors to hold function arguments.
12300 For efficiency, we keep a cache of unused vectors. This is the
12301 cache. */
12302
f1f41a6c 12303typedef vec<tree, va_gc> *tree_gc_vec;
12304static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
f352a3fb 12305
12306/* Return a new vector from the cache. If the cache is empty,
12307 allocate a new vector. These vectors are GC'ed, so it is OK if the
12308 pointer is not released.. */
12309
f1f41a6c 12310vec<tree, va_gc> *
f352a3fb 12311make_tree_vector (void)
12312{
f1f41a6c 12313 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12314 return tree_vector_cache->pop ();
f352a3fb 12315 else
12316 {
f1f41a6c 12317 /* Passing 0 to vec::alloc returns NULL, and our callers require
f352a3fb 12318 that we always return a non-NULL value. The vector code uses
12319 4 when growing a NULL vector, so we do too. */
f1f41a6c 12320 vec<tree, va_gc> *v;
12321 vec_alloc (v, 4);
12322 return v;
f352a3fb 12323 }
12324}
12325
12326/* Release a vector of trees back to the cache. */
12327
12328void
f1f41a6c 12329release_tree_vector (vec<tree, va_gc> *vec)
f352a3fb 12330{
12331 if (vec != NULL)
12332 {
f1f41a6c 12333 vec->truncate (0);
12334 vec_safe_push (tree_vector_cache, vec);
f352a3fb 12335 }
12336}
12337
12338/* Get a new tree vector holding a single tree. */
12339
f1f41a6c 12340vec<tree, va_gc> *
f352a3fb 12341make_tree_vector_single (tree t)
12342{
f1f41a6c 12343 vec<tree, va_gc> *ret = make_tree_vector ();
12344 ret->quick_push (t);
f352a3fb 12345 return ret;
12346}
12347
c66c81be 12348/* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12349
f1f41a6c 12350vec<tree, va_gc> *
c66c81be 12351make_tree_vector_from_list (tree list)
12352{
f1f41a6c 12353 vec<tree, va_gc> *ret = make_tree_vector ();
c66c81be 12354 for (; list; list = TREE_CHAIN (list))
f1f41a6c 12355 vec_safe_push (ret, TREE_VALUE (list));
c66c81be 12356 return ret;
12357}
12358
f352a3fb 12359/* Get a new tree vector which is a copy of an existing one. */
12360
f1f41a6c 12361vec<tree, va_gc> *
12362make_tree_vector_copy (const vec<tree, va_gc> *orig)
f352a3fb 12363{
f1f41a6c 12364 vec<tree, va_gc> *ret;
f352a3fb 12365 unsigned int ix;
12366 tree t;
12367
12368 ret = make_tree_vector ();
f1f41a6c 12369 vec_safe_reserve (ret, vec_safe_length (orig));
12370 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12371 ret->quick_push (t);
f352a3fb 12372 return ret;
12373}
12374
a9ffdd35 12375/* Return true if KEYWORD starts a type specifier. */
12376
12377bool
12378keyword_begins_type_specifier (enum rid keyword)
12379{
12380 switch (keyword)
12381 {
4fba5eb9 12382 case RID_AUTO_TYPE:
a9ffdd35 12383 case RID_INT:
12384 case RID_CHAR:
12385 case RID_FLOAT:
12386 case RID_DOUBLE:
12387 case RID_VOID:
a9ffdd35 12388 case RID_UNSIGNED:
12389 case RID_LONG:
12390 case RID_SHORT:
12391 case RID_SIGNED:
12392 case RID_DFLOAT32:
12393 case RID_DFLOAT64:
12394 case RID_DFLOAT128:
12395 case RID_FRACT:
12396 case RID_ACCUM:
12397 case RID_BOOL:
12398 case RID_WCHAR:
12399 case RID_CHAR16:
12400 case RID_CHAR32:
12401 case RID_SAT:
12402 case RID_COMPLEX:
12403 case RID_TYPEOF:
12404 case RID_STRUCT:
12405 case RID_CLASS:
12406 case RID_UNION:
12407 case RID_ENUM:
12408 return true;
12409 default:
9f75f026 12410 if (keyword >= RID_FIRST_INT_N
12411 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12412 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12413 return true;
a9ffdd35 12414 return false;
12415 }
12416}
12417
12418/* Return true if KEYWORD names a type qualifier. */
12419
12420bool
12421keyword_is_type_qualifier (enum rid keyword)
12422{
12423 switch (keyword)
12424 {
12425 case RID_CONST:
12426 case RID_VOLATILE:
12427 case RID_RESTRICT:
b560fabd 12428 case RID_ATOMIC:
a9ffdd35 12429 return true;
12430 default:
12431 return false;
12432 }
12433}
12434
12435/* Return true if KEYWORD names a storage class specifier.
12436
12437 RID_TYPEDEF is not included in this list despite `typedef' being
12438 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12439 such for syntactic convenience only. */
12440
12441bool
12442keyword_is_storage_class_specifier (enum rid keyword)
12443{
12444 switch (keyword)
12445 {
12446 case RID_STATIC:
12447 case RID_EXTERN:
12448 case RID_REGISTER:
12449 case RID_AUTO:
12450 case RID_MUTABLE:
12451 case RID_THREAD:
12452 return true;
12453 default:
12454 return false;
12455 }
12456}
12457
fad3f658 12458/* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12459
12460static bool
12461keyword_is_function_specifier (enum rid keyword)
12462{
12463 switch (keyword)
12464 {
12465 case RID_INLINE:
985c6e3a 12466 case RID_NORETURN:
fad3f658 12467 case RID_VIRTUAL:
12468 case RID_EXPLICIT:
12469 return true;
12470 default:
12471 return false;
12472 }
12473}
12474
12475/* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12476 declaration-specifier (C99 6.7). */
12477
12478bool
12479keyword_is_decl_specifier (enum rid keyword)
12480{
12481 if (keyword_is_storage_class_specifier (keyword)
12482 || keyword_is_type_qualifier (keyword)
12483 || keyword_is_function_specifier (keyword))
12484 return true;
12485
12486 switch (keyword)
12487 {
12488 case RID_TYPEDEF:
12489 case RID_FRIEND:
12490 case RID_CONSTEXPR:
12491 return true;
12492 default:
12493 return false;
12494 }
12495}
12496
9b88d08d 12497/* Initialize language-specific-bits of tree_contains_struct. */
12498
12499void
12500c_common_init_ts (void)
12501{
12502 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12503 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
3c6d4197 12504 MARK_TS_TYPED (ARRAY_NOTATION_REF);
9b88d08d 12505}
12506
244db24d 12507/* Build a user-defined numeric literal out of an integer constant type VALUE
12508 with identifier SUFFIX. */
12509
12510tree
324ca377 12511build_userdef_literal (tree suffix_id, tree value,
12512 enum overflow_type overflow, tree num_string)
244db24d 12513{
12514 tree literal = make_node (USERDEF_LITERAL);
12515 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12516 USERDEF_LITERAL_VALUE (literal) = value;
324ca377 12517 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
244db24d 12518 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12519 return literal;
12520}
12521
7345b977 12522/* For vector[index], convert the vector to an array of the underlying type.
12523 Return true if the resulting ARRAY_REF should not be an lvalue. */
c61ef207 12524
12525bool
7345b977 12526convert_vector_to_array_for_subscript (location_t loc,
12527 tree *vecp, tree index)
7059d45d 12528{
c61ef207 12529 bool ret = false;
6290f0db 12530 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
7059d45d 12531 {
12532 tree type = TREE_TYPE (*vecp);
7059d45d 12533
c61ef207 12534 ret = !lvalue_p (*vecp);
7345b977 12535
7059d45d 12536 if (TREE_CODE (index) == INTEGER_CST)
e913b5cd 12537 if (!tree_fits_uhwi_p (index)
aa59f000 12538 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7059d45d 12539 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12540
7345b977 12541 /* We are building an ARRAY_REF so mark the vector as addressable
12542 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
12543 for function parameters. */
12544 c_common_mark_addressable_vec (*vecp);
12545
12546 *vecp = build1 (VIEW_CONVERT_EXPR,
12547 build_array_type_nelts (TREE_TYPE (type),
12548 TYPE_VECTOR_SUBPARTS (type)),
12549 *vecp);
7059d45d 12550 }
c61ef207 12551 return ret;
7059d45d 12552}
12553
41ed701a 12554/* Determine which of the operands, if any, is a scalar that needs to be
12555 converted to a vector, for the range of operations. */
12556enum stv_conv
12557scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12558 bool complain)
12559{
12560 tree type0 = TREE_TYPE (op0);
12561 tree type1 = TREE_TYPE (op1);
12562 bool integer_only_op = false;
12563 enum stv_conv ret = stv_firstarg;
12564
6290f0db 12565 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
41ed701a 12566 switch (code)
12567 {
12568 /* Most GENERIC binary expressions require homogeneous arguments.
12569 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12570 argument that is a vector and a second one that is a scalar, so
12571 we never return stv_secondarg for them. */
12572 case RSHIFT_EXPR:
12573 case LSHIFT_EXPR:
12574 if (TREE_CODE (type0) == INTEGER_TYPE
12575 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12576 {
22a75734 12577 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12578 {
12579 if (complain)
12580 error_at (loc, "conversion of scalar %qT to vector %qT "
12581 "involves truncation", type0, type1);
12582 return stv_error;
12583 }
12584 else
12585 return stv_firstarg;
12586 }
12587 break;
12588
12589 case BIT_IOR_EXPR:
12590 case BIT_XOR_EXPR:
12591 case BIT_AND_EXPR:
12592 integer_only_op = true;
12593 /* ... fall through ... */
12594
7b463b19 12595 case VEC_COND_EXPR:
12596
41ed701a 12597 case PLUS_EXPR:
12598 case MINUS_EXPR:
12599 case MULT_EXPR:
12600 case TRUNC_DIV_EXPR:
12601 case CEIL_DIV_EXPR:
12602 case FLOOR_DIV_EXPR:
12603 case ROUND_DIV_EXPR:
12604 case EXACT_DIV_EXPR:
12605 case TRUNC_MOD_EXPR:
12606 case FLOOR_MOD_EXPR:
12607 case RDIV_EXPR:
12608 case EQ_EXPR:
12609 case NE_EXPR:
12610 case LE_EXPR:
12611 case GE_EXPR:
12612 case LT_EXPR:
12613 case GT_EXPR:
12614 /* What about UNLT_EXPR? */
6290f0db 12615 if (VECTOR_TYPE_P (type0))
41ed701a 12616 {
41ed701a 12617 ret = stv_secondarg;
a4f59596 12618 std::swap (type0, type1);
12619 std::swap (op0, op1);
41ed701a 12620 }
12621
12622 if (TREE_CODE (type0) == INTEGER_TYPE
12623 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12624 {
22a75734 12625 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12626 {
12627 if (complain)
12628 error_at (loc, "conversion of scalar %qT to vector %qT "
12629 "involves truncation", type0, type1);
12630 return stv_error;
12631 }
12632 return ret;
12633 }
12634 else if (!integer_only_op
12635 /* Allow integer --> real conversion if safe. */
12636 && (TREE_CODE (type0) == REAL_TYPE
12637 || TREE_CODE (type0) == INTEGER_TYPE)
12638 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12639 {
22a75734 12640 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12641 {
12642 if (complain)
12643 error_at (loc, "conversion of scalar %qT to vector %qT "
12644 "involves truncation", type0, type1);
12645 return stv_error;
12646 }
12647 return ret;
12648 }
12649 default:
12650 break;
12651 }
12652
12653 return stv_nothing;
12654}
12655
ffcdbf9c 12656/* Return true iff ALIGN is an integral constant that is a fundamental
12657 alignment, as defined by [basic.align] in the c++-11
12658 specifications.
12659
12660 That is:
12661
12662 [A fundamental alignment is represented by an alignment less than or
12663 equal to the greatest alignment supported by the implementation
12664 in all contexts, which is equal to
12665 alignof(max_align_t)]. */
12666
12667bool
12668cxx_fundamental_alignment_p (unsigned align)
12669{
12670 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12671 TYPE_ALIGN (long_double_type_node)));
12672}
12673
46da3601 12674/* Return true if T is a pointer to a zero-sized aggregate. */
12675
12676bool
12677pointer_to_zero_sized_aggr_p (tree t)
12678{
12679 if (!POINTER_TYPE_P (t))
12680 return false;
12681 t = TREE_TYPE (t);
12682 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12683}
12684
547c6b1f 12685/* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
12686 with no library fallback or for an ADDR_EXPR whose operand is such type
12687 issues an error pointing to the location LOC.
12688 Returns true when the expression has been diagnosed and false
12689 otherwise. */
12690bool
12691reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
12692{
12693 if (TREE_CODE (expr) == ADDR_EXPR)
12694 expr = TREE_OPERAND (expr, 0);
12695
12696 if (TREE_TYPE (expr)
12697 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
12cc1225 12698 && TREE_CODE (expr) == FUNCTION_DECL
547c6b1f 12699 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
12700 false positives for user-declared built-ins such as abs or
12701 strlen, and for C++ operators new and delete.
12702 The c_decl_implicit() test avoids false positives for implicitly
12703 declared built-ins with library fallbacks (such as abs). */
12704 && DECL_BUILT_IN (expr)
12705 && DECL_IS_BUILTIN (expr)
12706 && !c_decl_implicit (expr)
12707 && !DECL_ASSEMBLER_NAME_SET_P (expr))
12708 {
12709 if (loc == UNKNOWN_LOCATION)
12710 loc = EXPR_LOC_OR_LOC (expr, input_location);
12711
12712 /* Reject arguments that are built-in functions with
12713 no library fallback. */
12714 error_at (loc, "built-in function %qE must be directly called", expr);
12715
12716 return true;
12717 }
12718
12719 return false;
12720}
12721
ef17a71a 12722/* If we're creating an if-else-if condition chain, first see if we
12723 already have this COND in the CHAIN. If so, warn and don't add COND
12724 into the vector, otherwise add the COND there. LOC is the location
12725 of COND. */
12726
12727void
12728warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
12729{
12730 /* No chain has been created yet. Do nothing. */
12731 if (*chain == NULL)
12732 return;
12733
12734 if (TREE_SIDE_EFFECTS (cond))
12735 {
12736 /* Uh-oh! This condition has a side-effect, thus invalidates
12737 the whole chain. */
12738 delete *chain;
12739 *chain = NULL;
12740 return;
12741 }
12742
12743 unsigned int ix;
12744 tree t;
12745 bool found = false;
12746 FOR_EACH_VEC_ELT (**chain, ix, t)
12747 if (operand_equal_p (cond, t, 0))
12748 {
12749 if (warning_at (loc, OPT_Wduplicated_cond,
12750 "duplicated %<if%> condition"))
12751 inform (EXPR_LOCATION (t), "previously used here");
12752 found = true;
12753 break;
12754 }
12755
12756 if (!found
12757 && !CONSTANT_CLASS_P (cond)
12758 /* Don't infinitely grow the chain. */
12759 && (*chain)->length () < 512)
12760 (*chain)->safe_push (cond);
12761}
12762
81b1b2a8 12763/* Check if array size calculations overflow or if the array covers more
12764 than half of the address space. Return true if the size of the array
12765 is valid, false otherwise. TYPE is the type of the array and NAME is
12766 the name of the array, or NULL_TREE for unnamed arrays. */
12767
12768bool
12769valid_array_size_p (location_t loc, tree type, tree name)
12770{
12771 if (type != error_mark_node
12772 && COMPLETE_TYPE_P (type)
12773 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
12774 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
12775 {
12776 if (name)
12777 error_at (loc, "size of array %qE is too large", name);
12778 else
12779 error_at (loc, "size of unnamed array is too large");
12780 return false;
12781 }
12782 return true;
12783}
12784
e3e8c48c 12785/* Read SOURCE_DATE_EPOCH from environment to have a deterministic
12786 timestamp to replace embedded current dates to get reproducible
12787 results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
dfa5c0d3 12788
e3e8c48c 12789time_t
dfa5c0d3 12790cb_get_source_date_epoch (cpp_reader *pfile ATTRIBUTE_UNUSED)
e3e8c48c 12791{
12792 char *source_date_epoch;
12793 long long epoch;
12794 char *endptr;
12795
12796 source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
12797 if (!source_date_epoch)
12798 return (time_t) -1;
12799
12800 errno = 0;
12801 epoch = strtoll (source_date_epoch, &endptr, 10);
dfa5c0d3 12802 if (errno != 0 || endptr == source_date_epoch || *endptr != '\0'
12803 || epoch < 0 || epoch > MAX_SOURCE_DATE_EPOCH)
12804 {
12805 error_at (input_location, "environment variable SOURCE_DATE_EPOCH must "
12806 "expand to a non-negative integer less than or equal to %wd",
12807 MAX_SOURCE_DATE_EPOCH);
12808 return (time_t) -1;
12809 }
e3e8c48c 12810
12811 return (time_t) epoch;
12812}
12813
7f8012a5 12814/* Check and possibly warn if two declarations have contradictory
12815 attributes, such as always_inline vs. noinline. */
12816
12817bool
12818diagnose_mismatched_attributes (tree olddecl, tree newdecl)
12819{
12820 bool warned = false;
12821
12822 tree a1 = lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl));
12823 tree a2 = lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl));
12824 /* An optimization attribute applied on a declaration after the
12825 definition is likely not what the user wanted. */
12826 if (a2 != NULL_TREE
12827 && DECL_SAVED_TREE (olddecl) != NULL_TREE
12828 && (a1 == NULL_TREE || !attribute_list_equal (a1, a2)))
12829 warned |= warning (OPT_Wattributes,
12830 "optimization attribute on %qD follows "
12831 "definition but the attribute doesn%'t match",
12832 newdecl);
12833
12834 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
12835 if (DECL_DECLARED_INLINE_P (newdecl)
12836 && DECL_UNINLINABLE (olddecl)
12837 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
12838 warned |= warning (OPT_Wattributes, "inline declaration of %qD follows "
12839 "declaration with attribute noinline", newdecl);
12840 else if (DECL_DECLARED_INLINE_P (olddecl)
12841 && DECL_UNINLINABLE (newdecl)
12842 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
12843 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
12844 "noinline follows inline declaration ", newdecl);
12845 else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))
12846 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (olddecl)))
12847 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
12848 "%qs follows declaration with attribute %qs",
12849 newdecl, "noinline", "always_inline");
12850 else if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (newdecl))
12851 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
12852 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
12853 "%qs follows declaration with attribute %qs",
12854 newdecl, "always_inline", "noinline");
12855 else if (lookup_attribute ("cold", DECL_ATTRIBUTES (newdecl))
12856 && lookup_attribute ("hot", DECL_ATTRIBUTES (olddecl)))
12857 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
12858 "%qs follows declaration with attribute %qs",
12859 newdecl, "cold", "hot");
12860 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (newdecl))
12861 && lookup_attribute ("cold", DECL_ATTRIBUTES (olddecl)))
12862 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
12863 "%qs follows declaration with attribute %qs",
12864 newdecl, "hot", "cold");
12865 return warned;
12866}
12867
7bedc3a0 12868#include "gt-c-family-c-common.h"