]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-common.c
* config/nvptx/nvptx.c (nvptx_option_override): Don't kill debug level.
[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 *);
330static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
62eec3b4 331static tree handle_externally_visible_attribute (tree *, tree, tree, int,
332 bool *);
6b722052 333static tree handle_no_reorder_attribute (tree *, tree, tree, int,
334 bool *);
1cae46be 335static tree handle_const_attribute (tree *, tree, tree, int, bool *);
336static tree handle_transparent_union_attribute (tree *, tree, tree,
337 int, bool *);
292237f3 338static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
339 int, bool *);
1cae46be 340static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
341static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
342static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
343static tree handle_section_attribute (tree *, tree, tree, int, bool *);
344static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
345static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
4641882f 346static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
85c0a25c 347static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
348static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
1cae46be 349static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
f4a30bd7 350static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
1cae46be 351static tree handle_visibility_attribute (tree *, tree, tree, int,
352 bool *);
353static tree handle_tls_model_attribute (tree *, tree, tree, int,
354 bool *);
355static tree handle_no_instrument_function_attribute (tree *, tree,
356 tree, int, bool *);
357static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
26d1c5ff 358static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
1cae46be 359static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
360 bool *);
361static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
4c0315d0 362static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
363static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
fc09b200 364static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
1cae46be 365static tree handle_deprecated_attribute (tree *, tree, tree, int,
366 bool *);
367static tree handle_vector_size_attribute (tree *, tree, tree, int,
368 bool *);
369static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
370static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
371static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
8a8cdb8d 372static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
373 bool *);
bf6c8de0 374static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
b5c26b42 375static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
4a29c97c 376static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
237e78b1 377static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
378static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
24470055 379static tree handle_target_attribute (tree *, tree, tree, int, bool *);
ab50af2a 380static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
46f8e3b0 381static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
4c0315d0 382static tree ignore_attribute (tree *, tree, tree, int, bool *);
48b14f50 383static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
8ce86007 384static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
a96c3cc1 385static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
d7dcba40 386static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
bc7bff74 387static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
388 bool *);
c58a4cfd 389static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
bc7bff74 390static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
391 bool *);
74691f46 392static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
058a1b7a 393static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
394static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
395static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
1cae46be 396
1cae46be 397static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
398static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
399static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
860251be 400static int resort_field_decl_cmp (const void *, const void *);
dbf6c367 401
5c6e5756 402/* Reserved words. The third field is a mask: keywords are disabled
403 if they match the mask.
404
405 Masks for languages:
406 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
407 C --std=c99: D_CXXONLY | D_OBJC
408 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
409 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
410 C++ --std=c0x: D_CONLY | D_OBJC
411 ObjC++ is like C++ except that D_OBJC is not set
412
413 If -fno-asm is used, D_ASM is added to the mask. If
414 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
415 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
b27e241e 416 In C with -Wc++-compat, we warn if D_CXXWARN is set.
5c6e5756 417
b27e241e 418 Note the complication of the D_CXX_OBJC keywords. These are
419 reserved words such as 'class'. In C++, 'class' is a reserved
420 word. In Objective-C++ it is too. In Objective-C, it is a
421 reserved word too, but only if it follows an '@' sign.
422*/
5c6e5756 423const struct c_common_resword c_common_reswords[] =
424{
83e25171 425 { "_Alignas", RID_ALIGNAS, D_CONLY },
426 { "_Alignof", RID_ALIGNOF, D_CONLY },
b560fabd 427 { "_Atomic", RID_ATOMIC, D_CONLY },
5c6e5756 428 { "_Bool", RID_BOOL, D_CONLY },
429 { "_Complex", RID_COMPLEX, 0 },
d037099f 430 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
431 { "_Cilk_sync", RID_CILK_SYNC, 0 },
40750995 432 { "_Cilk_for", RID_CILK_FOR, 0 },
c1800156 433 { "_Imaginary", RID_IMAGINARY, D_CONLY },
5c6e5756 434 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
435 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
436 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
437 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
438 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
439 { "_Sat", RID_SAT, D_CONLY | D_EXT },
f80e7755 440 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
985c6e3a 441 { "_Noreturn", RID_NORETURN, D_CONLY },
7aa04c8d 442 { "_Generic", RID_GENERIC, D_CONLY },
d184e0c0 443 { "_Thread_local", RID_THREAD, D_CONLY },
5c6e5756 444 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
445 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
446 { "__alignof", RID_ALIGNOF, 0 },
447 { "__alignof__", RID_ALIGNOF, 0 },
448 { "__asm", RID_ASM, 0 },
449 { "__asm__", RID_ASM, 0 },
450 { "__attribute", RID_ATTRIBUTE, 0 },
451 { "__attribute__", RID_ATTRIBUTE, 0 },
4fba5eb9 452 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
e6014a82 453 { "__bases", RID_BASES, D_CXXONLY },
6f58cf06 454 { "__builtin_call_with_static_chain",
455 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
5c6e5756 456 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
bff4ad11 457 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
bf0cb017 458 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
5c6e5756 459 { "__builtin_offsetof", RID_OFFSETOF, 0 },
460 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
461 { "__builtin_va_arg", RID_VA_ARG, 0 },
462 { "__complex", RID_COMPLEX, 0 },
463 { "__complex__", RID_COMPLEX, 0 },
464 { "__const", RID_CONST, 0 },
465 { "__const__", RID_CONST, 0 },
466 { "__decltype", RID_DECLTYPE, D_CXXONLY },
e6014a82 467 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
5c6e5756 468 { "__extension__", RID_EXTENSION, 0 },
469 { "__func__", RID_C99_FUNCTION_NAME, 0 },
470 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
471 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
472 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
473 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
474 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
475 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
476 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
477 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
23407dc9 478 { "__imag", RID_IMAGPART, 0 },
479 { "__imag__", RID_IMAGPART, 0 },
480 { "__inline", RID_INLINE, 0 },
481 { "__inline__", RID_INLINE, 0 },
5c6e5756 482 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
483 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
484 { "__is_class", RID_IS_CLASS, D_CXXONLY },
5c6e5756 485 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
486 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
aa4313eb 487 { "__is_final", RID_IS_FINAL, D_CXXONLY },
23407dc9 488 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
5c6e5756 489 { "__is_pod", RID_IS_POD, D_CXXONLY },
490 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
56c12fd4 491 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
c1c67b4f 492 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
493 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
f76a9aa8 494 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
495 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
717e52f9 496 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
5c6e5756 497 { "__is_union", RID_IS_UNION, D_CXXONLY },
5c6e5756 498 { "__label__", RID_LABEL, 0 },
499 { "__null", RID_NULL, 0 },
500 { "__real", RID_REALPART, 0 },
501 { "__real__", RID_REALPART, 0 },
502 { "__restrict", RID_RESTRICT, 0 },
503 { "__restrict__", RID_RESTRICT, 0 },
504 { "__signed", RID_SIGNED, 0 },
505 { "__signed__", RID_SIGNED, 0 },
506 { "__thread", RID_THREAD, 0 },
4c0315d0 507 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
508 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
509 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
5c6e5756 510 { "__typeof", RID_TYPEOF, 0 },
511 { "__typeof__", RID_TYPEOF, 0 },
23407dc9 512 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
5c6e5756 513 { "__volatile", RID_VOLATILE, 0 },
514 { "__volatile__", RID_VOLATILE, 0 },
fa769cc5 515 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
516 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 517 { "asm", RID_ASM, D_ASM },
518 { "auto", RID_AUTO, 0 },
dbd982c9 519 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
5c6e5756 520 { "break", RID_BREAK, 0 },
521 { "case", RID_CASE, 0 },
51030405 522 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
5c6e5756 523 { "char", RID_CHAR, 0 },
fa769cc5 524 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
525 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
51030405 526 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
5c6e5756 527 { "const", RID_CONST, 0 },
fa769cc5 528 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 529 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
530 { "continue", RID_CONTINUE, 0 },
fa769cc5 531 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 532 { "default", RID_DEFAULT, 0 },
51030405 533 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
5c6e5756 534 { "do", RID_DO, 0 },
535 { "double", RID_DOUBLE, 0 },
536 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
537 { "else", RID_ELSE, 0 },
538 { "enum", RID_ENUM, 0 },
51030405 539 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
540 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
5c6e5756 541 { "extern", RID_EXTERN, 0 },
51030405 542 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
5c6e5756 543 { "float", RID_FLOAT, 0 },
544 { "for", RID_FOR, 0 },
51030405 545 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
5c6e5756 546 { "goto", RID_GOTO, 0 },
547 { "if", RID_IF, 0 },
548 { "inline", RID_INLINE, D_EXT89 },
549 { "int", RID_INT, 0 },
550 { "long", RID_LONG, 0 },
551 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
51030405 552 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
553 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
fa769cc5 554 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
555 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
51030405 556 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
557 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
558 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
559 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
5c6e5756 560 { "register", RID_REGISTER, 0 },
561 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
562 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
563 { "return", RID_RETURN, 0 },
564 { "short", RID_SHORT, 0 },
565 { "signed", RID_SIGNED, 0 },
566 { "sizeof", RID_SIZEOF, 0 },
567 { "static", RID_STATIC, 0 },
fa769cc5 568 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 569 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
570 { "struct", RID_STRUCT, 0 },
571 { "switch", RID_SWITCH, 0 },
51030405 572 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
573 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
fa769cc5 574 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
51030405 575 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
576 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
577 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
5c6e5756 578 { "typedef", RID_TYPEDEF, 0 },
51030405 579 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
580 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
5c6e5756 581 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
582 { "union", RID_UNION, 0 },
583 { "unsigned", RID_UNSIGNED, 0 },
51030405 584 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
585 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
5c6e5756 586 { "void", RID_VOID, 0 },
587 { "volatile", RID_VOLATILE, 0 },
588 { "wchar_t", RID_WCHAR, D_CXXONLY },
589 { "while", RID_WHILE, 0 },
56c12fd4 590
6d02e6b2 591 /* C++ transactional memory. */
592 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
593 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
594 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
595 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
596
56c12fd4 597 /* Concepts-related keywords */
598 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
599 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
600
5c6e5756 601 /* These Objective-C keywords are recognized only immediately after
602 an '@'. */
603 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
604 { "defs", RID_AT_DEFS, D_OBJC },
605 { "encode", RID_AT_ENCODE, D_OBJC },
606 { "end", RID_AT_END, D_OBJC },
607 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
608 { "interface", RID_AT_INTERFACE, D_OBJC },
609 { "protocol", RID_AT_PROTOCOL, D_OBJC },
610 { "selector", RID_AT_SELECTOR, D_OBJC },
611 { "finally", RID_AT_FINALLY, D_OBJC },
069761fb 612 { "optional", RID_AT_OPTIONAL, D_OBJC },
613 { "required", RID_AT_REQUIRED, D_OBJC },
86c110ac 614 { "property", RID_AT_PROPERTY, D_OBJC },
4a8875ed 615 { "package", RID_AT_PACKAGE, D_OBJC },
e1f293c0 616 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
617 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
5c6e5756 618 /* These are recognized only in protocol-qualifier context
619 (see above) */
620 { "bycopy", RID_BYCOPY, D_OBJC },
621 { "byref", RID_BYREF, D_OBJC },
622 { "in", RID_IN, D_OBJC },
623 { "inout", RID_INOUT, D_OBJC },
624 { "oneway", RID_ONEWAY, D_OBJC },
625 { "out", RID_OUT, D_OBJC },
86c110ac 626 /* These are recognized inside a property attribute list */
7590f0e5 627 { "assign", RID_ASSIGN, D_OBJC },
628 { "copy", RID_COPY, D_OBJC },
86c110ac 629 { "getter", RID_GETTER, D_OBJC },
7590f0e5 630 { "nonatomic", RID_NONATOMIC, D_OBJC },
631 { "readonly", RID_READONLY, D_OBJC },
632 { "readwrite", RID_READWRITE, D_OBJC },
633 { "retain", RID_RETAIN, D_OBJC },
86c110ac 634 { "setter", RID_SETTER, D_OBJC },
5c6e5756 635};
636
637const unsigned int num_c_common_reswords =
638 sizeof c_common_reswords / sizeof (struct c_common_resword);
639
058a1b7a 640/* Table of machine-independent attributes common to all C-like languages.
641
642 All attributes referencing arguments should be additionally processed
643 in chkp_copy_function_type_adding_bounds for correct instrumentation
644 by Pointer Bounds Checker.
645 Current list of processed common attributes: nonnull. */
f8e93a2e 646const struct attribute_spec c_common_attribute_table[] =
647{
ac86af5d 648 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
649 affects_type_identity } */
f8e93a2e 650 { "packed", 0, 0, false, false, false,
ac86af5d 651 handle_packed_attribute , false},
f8e93a2e 652 { "nocommon", 0, 0, true, false, false,
ac86af5d 653 handle_nocommon_attribute, false},
f8e93a2e 654 { "common", 0, 0, true, false, false,
ac86af5d 655 handle_common_attribute, false },
f8e93a2e 656 /* FIXME: logically, noreturn attributes should be listed as
657 "false, true, true" and apply to function types. But implementing this
658 would require all the places in the compiler that use TREE_THIS_VOLATILE
659 on a decl to identify non-returning functions to be located and fixed
660 to check the function type instead. */
661 { "noreturn", 0, 0, true, false, false,
ac86af5d 662 handle_noreturn_attribute, false },
f8e93a2e 663 { "volatile", 0, 0, true, false, false,
ac86af5d 664 handle_noreturn_attribute, false },
947aa916 665 { "stack_protect", 0, 0, true, false, false,
666 handle_stack_protect_attribute, false },
f8e93a2e 667 { "noinline", 0, 0, true, false, false,
ac86af5d 668 handle_noinline_attribute, false },
bdb1f0d1 669 { "noclone", 0, 0, true, false, false,
ac86af5d 670 handle_noclone_attribute, false },
85fbea97 671 { "no_icf", 0, 0, true, false, false,
672 handle_noicf_attribute, false },
7bd95dfd 673 { "leaf", 0, 0, true, false, false,
ac86af5d 674 handle_leaf_attribute, false },
f8e93a2e 675 { "always_inline", 0, 0, true, false, false,
ac86af5d 676 handle_always_inline_attribute, false },
541e4101 677 { "gnu_inline", 0, 0, true, false, false,
ac86af5d 678 handle_gnu_inline_attribute, false },
1b16fc45 679 { "artificial", 0, 0, true, false, false,
ac86af5d 680 handle_artificial_attribute, false },
0cdd9887 681 { "flatten", 0, 0, true, false, false,
ac86af5d 682 handle_flatten_attribute, false },
f8e93a2e 683 { "used", 0, 0, true, false, false,
ac86af5d 684 handle_used_attribute, false },
f8e93a2e 685 { "unused", 0, 0, false, false, false,
ac86af5d 686 handle_unused_attribute, false },
62eec3b4 687 { "externally_visible", 0, 0, true, false, false,
ac86af5d 688 handle_externally_visible_attribute, false },
6b722052 689 { "no_reorder", 0, 0, true, false, false,
690 handle_no_reorder_attribute, false },
f8e93a2e 691 /* The same comments as for noreturn attributes apply to const ones. */
692 { "const", 0, 0, true, false, false,
ac86af5d 693 handle_const_attribute, false },
292237f3 694 { "scalar_storage_order", 1, 1, false, false, false,
695 handle_scalar_storage_order_attribute, false },
f8e93a2e 696 { "transparent_union", 0, 0, false, false, false,
ac86af5d 697 handle_transparent_union_attribute, false },
9af7fd5b 698 { "constructor", 0, 1, true, false, false,
ac86af5d 699 handle_constructor_attribute, false },
9af7fd5b 700 { "destructor", 0, 1, true, false, false,
ac86af5d 701 handle_destructor_attribute, false },
f8e93a2e 702 { "mode", 1, 1, false, true, false,
ac86af5d 703 handle_mode_attribute, false },
f8e93a2e 704 { "section", 1, 1, true, false, false,
ac86af5d 705 handle_section_attribute, false },
f8e93a2e 706 { "aligned", 0, 1, false, false, false,
ac86af5d 707 handle_aligned_attribute, false },
f8e93a2e 708 { "weak", 0, 0, true, false, false,
ac86af5d 709 handle_weak_attribute, false },
4641882f 710 { "noplt", 0, 0, true, false, false,
711 handle_noplt_attribute, false },
85c0a25c 712 { "ifunc", 1, 1, true, false, false,
ac86af5d 713 handle_ifunc_attribute, false },
f8e93a2e 714 { "alias", 1, 1, true, false, false,
ac86af5d 715 handle_alias_attribute, false },
f4a30bd7 716 { "weakref", 0, 1, true, false, false,
ac86af5d 717 handle_weakref_attribute, false },
f8e93a2e 718 { "no_instrument_function", 0, 0, true, false, false,
ac86af5d 719 handle_no_instrument_function_attribute,
720 false },
f8e93a2e 721 { "malloc", 0, 0, true, false, false,
ac86af5d 722 handle_malloc_attribute, false },
26d1c5ff 723 { "returns_twice", 0, 0, true, false, false,
ac86af5d 724 handle_returns_twice_attribute, false },
f8e93a2e 725 { "no_stack_limit", 0, 0, true, false, false,
ac86af5d 726 handle_no_limit_stack_attribute, false },
f8e93a2e 727 { "pure", 0, 0, true, false, false,
ac86af5d 728 handle_pure_attribute, false },
4c0315d0 729 { "transaction_callable", 0, 0, false, true, false,
730 handle_tm_attribute, false },
731 { "transaction_unsafe", 0, 0, false, true, false,
6d02e6b2 732 handle_tm_attribute, true },
4c0315d0 733 { "transaction_safe", 0, 0, false, true, false,
6d02e6b2 734 handle_tm_attribute, true },
735 { "transaction_safe_dynamic", 0, 0, true, false, false,
4c0315d0 736 handle_tm_attribute, false },
737 { "transaction_may_cancel_outer", 0, 0, false, true, false,
738 handle_tm_attribute, false },
739 /* ??? These two attributes didn't make the transition from the
740 Intel language document to the multi-vendor language document. */
741 { "transaction_pure", 0, 0, false, true, false,
742 handle_tm_attribute, false },
743 { "transaction_wrap", 1, 1, true, false, false,
744 handle_tm_wrap_attribute, false },
fc09b200 745 /* For internal use (marking of builtins) only. The name contains space
746 to prevent its usage in source code. */
747 { "no vops", 0, 0, true, false, false,
ac86af5d 748 handle_novops_attribute, false },
45c4e798 749 { "deprecated", 0, 1, false, false, false,
ac86af5d 750 handle_deprecated_attribute, false },
f8e93a2e 751 { "vector_size", 1, 1, false, true, false,
ac86af5d 752 handle_vector_size_attribute, false },
b212f378 753 { "visibility", 1, 1, false, false, false,
ac86af5d 754 handle_visibility_attribute, false },
24dfead4 755 { "tls_model", 1, 1, true, false, false,
ac86af5d 756 handle_tls_model_attribute, false },
dbf6c367 757 { "nonnull", 0, -1, false, true, true,
ac86af5d 758 handle_nonnull_attribute, false },
fa987697 759 { "nothrow", 0, 0, true, false, false,
ac86af5d 760 handle_nothrow_attribute, false },
761 { "may_alias", 0, 0, false, true, false, NULL, false },
7acb29a3 762 { "cleanup", 1, 1, true, false, false,
ac86af5d 763 handle_cleanup_attribute, false },
8a8cdb8d 764 { "warn_unused_result", 0, 0, false, true, true,
ac86af5d 765 handle_warn_unused_result_attribute, false },
50ca527f 766 { "sentinel", 0, 1, false, true, true,
ac86af5d 767 handle_sentinel_attribute, false },
b5c26b42 768 /* For internal use (marking of builtins) only. The name contains space
769 to prevent its usage in source code. */
770 { "type generic", 0, 0, false, true, true,
ac86af5d 771 handle_type_generic_attribute, false },
4a29c97c 772 { "alloc_size", 1, 2, false, true, true,
ac86af5d 773 handle_alloc_size_attribute, false },
5de92639 774 { "cold", 0, 0, true, false, false,
ac86af5d 775 handle_cold_attribute, false },
5de92639 776 { "hot", 0, 0, true, false, false,
ac86af5d 777 handle_hot_attribute, false },
d413ffdd 778 { "no_address_safety_analysis",
779 0, 0, true, false, false,
780 handle_no_address_safety_analysis_attribute,
781 false },
a9196da9 782 { "no_sanitize_address", 0, 0, true, false, false,
783 handle_no_sanitize_address_attribute,
784 false },
d1e96383 785 { "no_sanitize_thread", 0, 0, true, false, false,
786 handle_no_sanitize_address_attribute,
787 false },
05f893e1 788 { "no_sanitize_undefined", 0, 0, true, false, false,
789 handle_no_sanitize_undefined_attribute,
790 false },
10fc867f 791 { "warning", 1, 1, true, false, false,
ac86af5d 792 handle_error_attribute, false },
10fc867f 793 { "error", 1, 1, true, false, false,
ac86af5d 794 handle_error_attribute, false },
24470055 795 { "target", 1, -1, true, false, false,
ac86af5d 796 handle_target_attribute, false },
ab50af2a 797 { "target_clones", 1, -1, true, false, false,
798 handle_target_clones_attribute, false },
46f8e3b0 799 { "optimize", 1, -1, true, false, false,
ac86af5d 800 handle_optimize_attribute, false },
4c0315d0 801 /* For internal use only. The leading '*' both prevents its usage in
802 source code and signals that it may be overridden by machine tables. */
803 { "*tm regparm", 0, 0, false, true, true,
804 ignore_attribute, false },
48b14f50 805 { "no_split_stack", 0, 0, true, false, false,
ac86af5d 806 handle_no_split_stack_attribute, false },
8ce86007 807 /* For internal use (marking of builtins and runtime functions) only.
808 The name contains space to prevent its usage in source code. */
809 { "fn spec", 1, 1, false, true, true,
ac86af5d 810 handle_fnspec_attribute, false },
a96c3cc1 811 { "warn_unused", 0, 0, false, false, false,
812 handle_warn_unused_attribute, false },
d7dcba40 813 { "returns_nonnull", 0, 0, false, true, true,
814 handle_returns_nonnull_attribute, false },
bc7bff74 815 { "omp declare simd", 0, -1, true, false, false,
816 handle_omp_declare_simd_attribute, false },
74acc703 817 { "cilk simd function", 0, -1, true, false, false,
818 handle_omp_declare_simd_attribute, false },
9841c6ee 819 { "simd", 0, 1, true, false, false,
c58a4cfd 820 handle_simd_attribute, false },
bc7bff74 821 { "omp declare target", 0, 0, true, false, false,
822 handle_omp_declare_target_attribute, false },
c0998828 823 { "omp declare target link", 0, 0, true, false, false,
824 handle_omp_declare_target_attribute, false },
237e78b1 825 { "alloc_align", 1, 1, false, true, true,
826 handle_alloc_align_attribute, false },
827 { "assume_aligned", 1, 2, false, true, true,
828 handle_assume_aligned_attribute, false },
74691f46 829 { "designated_init", 0, 0, false, true, false,
830 handle_designated_init_attribute, false },
058a1b7a 831 { "bnd_variable_size", 0, 0, true, false, false,
832 handle_bnd_variable_size_attribute, false },
833 { "bnd_legacy", 0, 0, true, false, false,
834 handle_bnd_legacy, false },
835 { "bnd_instrument", 0, 0, true, false, false,
836 handle_bnd_instrument, false },
ac86af5d 837 { NULL, 0, 0, false, false, false, NULL, false }
f8e93a2e 838};
839
840/* Give the specifications for the format attributes, used by C and all
058a1b7a 841 descendants.
f8e93a2e 842
058a1b7a 843 All attributes referencing arguments should be additionally processed
844 in chkp_copy_function_type_adding_bounds for correct instrumentation
845 by Pointer Bounds Checker.
846 Current list of processed format attributes: format, format_arg. */
f8e93a2e 847const struct attribute_spec c_common_format_attribute_table[] =
848{
ac86af5d 849 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
850 affects_type_identity } */
f8e93a2e 851 { "format", 3, 3, false, true, true,
ac86af5d 852 handle_format_attribute, false },
f8e93a2e 853 { "format_arg", 1, 1, false, true, true,
ac86af5d 854 handle_format_arg_attribute, false },
855 { NULL, 0, 0, false, false, false, NULL, false }
f8e93a2e 856};
857
6d5d708e 858/* Return identifier for address space AS. */
34208e18 859
6d5d708e 860const char *
861c_addr_space_name (addr_space_t as)
862{
34208e18 863 int rid = RID_FIRST_ADDR_SPACE + as;
864 gcc_assert (ridpointers [rid]);
865 return IDENTIFIER_POINTER (ridpointers [rid]);
6d5d708e 866}
867
2c0e001b 868/* Push current bindings for the function name VAR_DECLS. */
f4e3c278 869
870void
1cae46be 871start_fname_decls (void)
f4e3c278 872{
65b7f83f 873 unsigned ix;
874 tree saved = NULL_TREE;
1cae46be 875
65b7f83f 876 for (ix = 0; fname_vars[ix].decl; ix++)
877 {
878 tree decl = *fname_vars[ix].decl;
f4e3c278 879
65b7f83f 880 if (decl)
881 {
ceb7b692 882 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
883 saved);
65b7f83f 884 *fname_vars[ix].decl = NULL_TREE;
885 }
886 }
887 if (saved || saved_function_name_decls)
888 /* Normally they'll have been NULL, so only push if we've got a
889 stack, or they are non-NULL. */
890 saved_function_name_decls = tree_cons (saved, NULL_TREE,
891 saved_function_name_decls);
892}
893
2363ef00 894/* Finish up the current bindings, adding them into the current function's
895 statement tree. This must be done _before_ finish_stmt_tree is called.
896 If there is no current function, we must be at file scope and no statements
897 are involved. Pop the previous bindings. */
65b7f83f 898
899void
1cae46be 900finish_fname_decls (void)
65b7f83f 901{
902 unsigned ix;
2363ef00 903 tree stmts = NULL_TREE;
65b7f83f 904 tree stack = saved_function_name_decls;
905
906 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
2363ef00 907 append_to_statement_list (TREE_VALUE (stack), &stmts);
1cae46be 908
2363ef00 909 if (stmts)
65b7f83f 910 {
2363ef00 911 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
5c423bd6 912
2363ef00 913 if (TREE_CODE (*bodyp) == BIND_EXPR)
914 bodyp = &BIND_EXPR_BODY (*bodyp);
81010c97 915
bc2b76e0 916 append_to_statement_list_force (*bodyp, &stmts);
2363ef00 917 *bodyp = stmts;
65b7f83f 918 }
1cae46be 919
65b7f83f 920 for (ix = 0; fname_vars[ix].decl; ix++)
921 *fname_vars[ix].decl = NULL_TREE;
1cae46be 922
65b7f83f 923 if (stack)
f4e3c278 924 {
2c0e001b 925 /* We had saved values, restore them. */
65b7f83f 926 tree saved;
927
928 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
929 {
930 tree decl = TREE_PURPOSE (saved);
f9ae6f95 931 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
1cae46be 932
65b7f83f 933 *fname_vars[ix].decl = decl;
934 }
935 stack = TREE_CHAIN (stack);
f4e3c278 936 }
65b7f83f 937 saved_function_name_decls = stack;
938}
939
81010c97 940/* Return the text name of the current function, suitably prettified
5fc7fa69 941 by PRETTY_P. Return string must be freed by caller. */
65b7f83f 942
943const char *
1cae46be 944fname_as_string (int pretty_p)
65b7f83f 945{
9ad4bb1e 946 const char *name = "top level";
5fc7fa69 947 char *namep;
8115b8be 948 int vrb = 2, len;
949 cpp_string cstr = { 0, 0 }, strname;
9ad4bb1e 950
84166705 951 if (!pretty_p)
9ad4bb1e 952 {
953 name = "";
954 vrb = 0;
955 }
956
957 if (current_function_decl)
dc24ddbd 958 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
9ad4bb1e 959
8115b8be 960 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
5fc7fa69 961
8115b8be 962 namep = XNEWVEC (char, len);
963 snprintf (namep, len, "\"%s\"", name);
964 strname.text = (unsigned char *) namep;
965 strname.len = len - 1;
5fc7fa69 966
924bbf02 967 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
8115b8be 968 {
969 XDELETEVEC (namep);
970 return (const char *) cstr.text;
5fc7fa69 971 }
5fc7fa69 972
973 return namep;
65b7f83f 974}
975
65b7f83f 976/* Return the VAR_DECL for a const char array naming the current
977 function. If the VAR_DECL has not yet been created, create it
978 now. RID indicates how it should be formatted and IDENTIFIER_NODE
979 ID is its name (unfortunately C and C++ hold the RID values of
980 keywords in different places, so we can't derive RID from ID in
e3b80d49 981 this language independent code. LOC is the location of the
982 function. */
65b7f83f 983
984tree
e3b80d49 985fname_decl (location_t loc, unsigned int rid, tree id)
65b7f83f 986{
987 unsigned ix;
988 tree decl = NULL_TREE;
989
990 for (ix = 0; fname_vars[ix].decl; ix++)
991 if (fname_vars[ix].rid == rid)
992 break;
993
994 decl = *fname_vars[ix].decl;
995 if (!decl)
f4e3c278 996 {
2222b3c6 997 /* If a tree is built here, it would normally have the lineno of
998 the current statement. Later this tree will be moved to the
999 beginning of the function and this line number will be wrong.
1000 To avoid this problem set the lineno to 0 here; that prevents
7299020b 1001 it from appearing in the RTL. */
2363ef00 1002 tree stmts;
9a6486a6 1003 location_t saved_location = input_location;
9a6486a6 1004 input_location = UNKNOWN_LOCATION;
1cae46be 1005
2363ef00 1006 stmts = push_stmt_list ();
e60a6f7b 1007 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
2363ef00 1008 stmts = pop_stmt_list (stmts);
1009 if (!IS_EMPTY_STMT (stmts))
1010 saved_function_name_decls
1011 = tree_cons (decl, stmts, saved_function_name_decls);
65b7f83f 1012 *fname_vars[ix].decl = decl;
9a6486a6 1013 input_location = saved_location;
f4e3c278 1014 }
65b7f83f 1015 if (!ix && !current_function_decl)
e3b80d49 1016 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
81010c97 1017
65b7f83f 1018 return decl;
f4e3c278 1019}
1020
070236f0 1021/* Given a STRING_CST, give it a suitable array-of-chars data type. */
b0fc3e72 1022
1023tree
1cae46be 1024fix_string_type (tree value)
b0fc3e72 1025{
070236f0 1026 int length = TREE_STRING_LENGTH (value);
1027 int nchars;
00d26680 1028 tree e_type, i_type, a_type;
1029
73be5127 1030 /* Compute the number of elements, for the array type. */
924bbf02 1031 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1032 {
1033 nchars = length;
1034 e_type = char_type_node;
1035 }
1036 else if (TREE_TYPE (value) == char16_array_type_node)
1037 {
1038 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1039 e_type = char16_type_node;
1040 }
1041 else if (TREE_TYPE (value) == char32_array_type_node)
1042 {
1043 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1044 e_type = char32_type_node;
1045 }
1046 else
1047 {
1048 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1049 e_type = wchar_type_node;
1050 }
b0fc3e72 1051
1d752508 1052 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1053 limit in C++98 Annex B is very large (65536) and is not normative,
1054 so we do not diagnose it (warn_overlength_strings is forced off
1055 in c_common_post_options). */
1056 if (warn_overlength_strings)
1057 {
1058 const int nchars_max = flag_isoc99 ? 4095 : 509;
1059 const int relevant_std = flag_isoc99 ? 99 : 90;
1060 if (nchars - 1 > nchars_max)
1061 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1062 separate the %d from the 'C'. 'ISO' should not be
1063 translated, but it may be moved after 'C%d' in languages
1064 where modifiers follow nouns. */
21ca8540 1065 pedwarn (input_location, OPT_Woverlength_strings,
8864917d 1066 "string length %qd is greater than the length %qd "
1d752508 1067 "ISO C%d compilers are required to support",
1068 nchars - 1, nchars_max, relevant_std);
1069 }
82cfc7f7 1070
390be14e 1071 /* Create the array type for the string constant. The ISO C++
1072 standard says that a string literal has type `const char[N]' or
1073 `const wchar_t[N]'. We use the same logic when invoked as a C
1074 front-end with -Wwrite-strings.
1075 ??? We should change the type of an expression depending on the
1076 state of a warning flag. We should just be warning -- see how
1077 this is handled in the C++ front-end for the deprecated implicit
1078 conversion from string literals to `char*' or `wchar_t*'.
00d26680 1079
1080 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1081 array type being the unqualified version of that type.
1082 Therefore, if we are constructing an array of const char, we must
1083 construct the matching unqualified array type first. The C front
1084 end does not require this, but it does no harm, so we do it
1085 unconditionally. */
ceb7b692 1086 i_type = build_index_type (size_int (nchars - 1));
00d26680 1087 a_type = build_array_type (e_type, i_type);
390be14e 1088 if (c_dialect_cxx() || warn_write_strings)
aebc8537 1089 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
3a10ba35 1090
00d26680 1091 TREE_TYPE (value) = a_type;
b8e3b7ad 1092 TREE_CONSTANT (value) = 1;
a814bad5 1093 TREE_READONLY (value) = 1;
b0fc3e72 1094 TREE_STATIC (value) = 1;
1095 return value;
1096}
1097\f
9c691dbd 1098/* Fold X for consideration by one of the warning functions when checking
1099 whether an expression has a constant value. */
1100
1101static tree
1102fold_for_warn (tree x)
1103{
1104 if (c_dialect_cxx ())
1105 return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
1106 else
1107 /* The C front-end has already folded X appropriately. */
1108 return x;
1109}
1110
2a1736ed 1111/* Print a warning if a constant expression had overflow in folding.
1112 Invoke this function on every expression that the language
1113 requires to be a constant expression.
1114 Note the ANSI C standard says it is erroneous for a
1115 constant expression to overflow. */
b2806639 1116
1117void
1cae46be 1118constant_expression_warning (tree value)
07317e69 1119{
48e1416a 1120 if (warn_overflow && pedantic
07317e69 1121 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1122 || TREE_CODE (value) == FIXED_CST
1123 || TREE_CODE (value) == VECTOR_CST
1124 || TREE_CODE (value) == COMPLEX_CST)
1125 && TREE_OVERFLOW (value))
21ca8540 1126 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
07317e69 1127}
1128
1129/* The same as above but print an unconditional error. */
1130void
1131constant_expression_error (tree value)
b2806639 1132{
837e1122 1133 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
9421ebb9 1134 || TREE_CODE (value) == FIXED_CST
886cfd4f 1135 || TREE_CODE (value) == VECTOR_CST
837e1122 1136 || TREE_CODE (value) == COMPLEX_CST)
07317e69 1137 && TREE_OVERFLOW (value))
1138 error ("overflow in constant expression");
2a1736ed 1139}
1140
f170d67f 1141/* Print a warning if an expression had overflow in folding and its
1142 operands hadn't.
1143
2a1736ed 1144 Invoke this function on every expression that
1145 (1) appears in the source code, and
f170d67f 1146 (2) is a constant expression that overflowed, and
2a1736ed 1147 (3) is not already checked by convert_and_check;
f170d67f 1148 however, do not invoke this function on operands of explicit casts
1149 or when the expression is the result of an operator and any operand
1150 already overflowed. */
2a1736ed 1151
1152void
e60a6f7b 1153overflow_warning (location_t loc, tree value)
2a1736ed 1154{
48d94ede 1155 if (c_inhibit_evaluation_warnings != 0)
1156 return;
f170d67f 1157
1158 switch (TREE_CODE (value))
886cfd4f 1159 {
f170d67f 1160 case INTEGER_CST:
e60a6f7b 1161 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
f170d67f 1162 break;
48e1416a 1163
f170d67f 1164 case REAL_CST:
e60a6f7b 1165 warning_at (loc, OPT_Woverflow,
1166 "floating point overflow in expression");
f170d67f 1167 break;
48e1416a 1168
9421ebb9 1169 case FIXED_CST:
e60a6f7b 1170 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
9421ebb9 1171 break;
1172
f170d67f 1173 case VECTOR_CST:
e60a6f7b 1174 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
f170d67f 1175 break;
48e1416a 1176
f170d67f 1177 case COMPLEX_CST:
1178 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
e60a6f7b 1179 warning_at (loc, OPT_Woverflow,
1180 "complex integer overflow in expression");
f170d67f 1181 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
e60a6f7b 1182 warning_at (loc, OPT_Woverflow,
1183 "complex floating point overflow in expression");
f170d67f 1184 break;
1185
1186 default:
1187 break;
886cfd4f 1188 }
2a1736ed 1189}
1190
03033af4 1191/* Warn about uses of logical || / && operator in a context where it
1192 is likely that the bitwise equivalent was intended by the
1193 programmer. We have seen an expression in which CODE is a binary
9c20c4fc 1194 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1195 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
b13d1547 1196void
9c20c4fc 1197warn_logical_operator (location_t location, enum tree_code code, tree type,
48e1416a 1198 enum tree_code code_left, tree op_left,
03033af4 1199 enum tree_code ARG_UNUSED (code_right), tree op_right)
b13d1547 1200{
9c20c4fc 1201 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1202 int in0_p, in1_p, in_p;
1203 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1204 bool strict_overflow_p = false;
1205
03033af4 1206 if (code != TRUTH_ANDIF_EXPR
1207 && code != TRUTH_AND_EXPR
1208 && code != TRUTH_ORIF_EXPR
1209 && code != TRUTH_OR_EXPR)
1210 return;
1211
439606a9 1212 /* We don't want to warn if either operand comes from a macro
1213 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1214 see PR61534. */
1215 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1216 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1217 return;
1218
03033af4 1219 /* Warn if &&/|| are being used in a context where it is
1220 likely that the bitwise equivalent was intended by the
1221 programmer. That is, an expression such as op && MASK
1222 where op should not be any boolean expression, nor a
1223 constant, and mask seems to be a non-boolean integer constant. */
9c691dbd 1224 if (TREE_CODE (op_right) == CONST_DECL)
1225 /* An enumerator counts as a constant. */
1226 op_right = DECL_INITIAL (op_right);
03033af4 1227 if (!truth_value_p (code_left)
1228 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1229 && !CONSTANT_CLASS_P (op_left)
1230 && !TREE_NO_WARNING (op_left)
1231 && TREE_CODE (op_right) == INTEGER_CST
1232 && !integer_zerop (op_right)
1233 && !integer_onep (op_right))
b13d1547 1234 {
9c20c4fc 1235 if (or_op)
03033af4 1236 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1237 " applied to non-boolean constant");
1238 else
1239 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1240 " applied to non-boolean constant");
1241 TREE_NO_WARNING (op_left) = true;
9c20c4fc 1242 return;
1243 }
1244
1245 /* We do not warn for constants because they are typical of macro
1246 expansions that test for features. */
9c691dbd 1247 if (CONSTANT_CLASS_P (fold_for_warn (op_left))
1248 || CONSTANT_CLASS_P (fold_for_warn (op_right)))
9c20c4fc 1249 return;
1250
1251 /* This warning only makes sense with logical operands. */
1252 if (!(truth_value_p (TREE_CODE (op_left))
1253 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1254 || !(truth_value_p (TREE_CODE (op_right))
1255 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1256 return;
1257
5e84569c 1258 /* The range computations only work with scalars. */
1259 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1260 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1261 return;
9c20c4fc 1262
686369e8 1263 /* We first test whether either side separately is trivially true
1264 (with OR) or trivially false (with AND). If so, do not warn.
1265 This is a common idiom for testing ranges of data types in
1266 portable code. */
1267 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1268 if (!lhs)
1269 return;
1270 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
9c20c4fc 1271 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1272
686369e8 1273 /* If this is an OR operation, invert both sides; now, the result
1274 should be always false to get a warning. */
1275 if (or_op)
1276 in0_p = !in0_p;
1277
1278 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
d42e7c5a 1279 if (tem && integer_zerop (tem))
686369e8 1280 return;
1281
1282 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1283 if (!rhs)
1284 return;
1285 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
9c20c4fc 1286 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
48e1416a 1287
686369e8 1288 /* If this is an OR operation, invert both sides; now, the result
1289 should be always false to get a warning. */
9c20c4fc 1290 if (or_op)
686369e8 1291 in1_p = !in1_p;
48e1416a 1292
686369e8 1293 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
d42e7c5a 1294 if (tem && integer_zerop (tem))
686369e8 1295 return;
1296
1297 /* If both expressions have the same operand, if we can merge the
485f6b9c 1298 ranges, ... */
686369e8 1299 if (operand_equal_p (lhs, rhs, 0)
9c20c4fc 1300 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
485f6b9c 1301 in1_p, low1, high1))
9c20c4fc 1302 {
485f6b9c 1303 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1304 /* ... and if the range test is always false, then warn. */
1305 if (tem && integer_zerop (tem))
1306 {
1307 if (or_op)
1308 warning_at (location, OPT_Wlogical_op,
1309 "logical %<or%> of collectively exhaustive tests is "
1310 "always true");
1311 else
1312 warning_at (location, OPT_Wlogical_op,
1313 "logical %<and%> of mutually exclusive tests is "
1314 "always false");
1315 }
1316 /* Or warn if the operands have exactly the same range, e.g.
1317 A > 0 && A > 0. */
0200602e 1318 else if (tree_int_cst_equal (low0, low1)
1319 && tree_int_cst_equal (high0, high1))
485f6b9c 1320 {
1321 if (or_op)
1322 warning_at (location, OPT_Wlogical_op,
1323 "logical %<or%> of equal expressions");
1324 else
1325 warning_at (location, OPT_Wlogical_op,
1326 "logical %<and%> of equal expressions");
1327 }
b13d1547 1328 }
1329}
1330
6784a472 1331/* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
1332 with constant indices. */
1333
1334static tree
1335find_array_ref_with_const_idx_r (tree *expr_p, int *walk_subtrees, void *data)
1336{
1337 tree expr = *expr_p;
1338
1339 if ((TREE_CODE (expr) == ARRAY_REF
1340 || TREE_CODE (expr) == ARRAY_RANGE_REF)
1341 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
1342 {
1343 *(bool *) data = true;
1344 *walk_subtrees = 0;
1345 }
1346
1347 return NULL_TREE;
1348}
1349
1350/* Warn if a self-comparison always evaluates to true or false. LOC
1351 is the location of the comparison with code CODE, LHS and RHS are
1352 operands of the comparison. */
1353
1354void
1355warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
1356{
1357 if (TREE_CODE_CLASS (code) != tcc_comparison)
1358 return;
1359
d9e4fe27 1360 /* Don't warn for various macro expansions. */
1361 if (from_macro_expansion_at (loc)
1362 || from_macro_expansion_at (EXPR_LOCATION (lhs))
1363 || from_macro_expansion_at (EXPR_LOCATION (rhs)))
1364 return;
1365
6784a472 1366 /* We do not warn for constants because they are typical of macro
1367 expansions that test for features, sizeof, and similar. */
9c691dbd 1368 if (CONSTANT_CLASS_P (fold_for_warn (lhs))
1369 || CONSTANT_CLASS_P (fold_for_warn (rhs)))
6784a472 1370 return;
1371
1372 /* Don't warn for e.g.
1373 HOST_WIDE_INT n;
1374 ...
1375 if (n == (long) n) ...
1376 */
1377 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
1378 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
1379 return;
1380
f58dd84b 1381 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
1382 It could be a NaN, and NaN never compares equal to anything, even
1383 itself. */
1384 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
1385 return;
1386
6784a472 1387 if (operand_equal_p (lhs, rhs, 0))
1388 {
1389 /* Don't warn about array references with constant indices;
1390 these are likely to come from a macro. */
1391 bool found = false;
1392 walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
1393 &found);
1394 if (found)
1395 return;
1396 const bool always_true = (code == EQ_EXPR || code == LE_EXPR
1397 || code == GE_EXPR || code == UNLE_EXPR
1398 || code == UNGE_EXPR || code == UNEQ_EXPR);
1399 if (always_true)
1400 warning_at (loc, OPT_Wtautological_compare,
1401 "self-comparison always evaluates to true");
1402 else
1403 warning_at (loc, OPT_Wtautological_compare,
1404 "self-comparison always evaluates to false");
1405 }
1406}
1407
32dc1512 1408/* Warn about logical not used on the left hand side operand of a comparison.
1409 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
dc6229e8 1410 Do not warn if RHS is of a boolean type. */
32dc1512 1411
1412void
1413warn_logical_not_parentheses (location_t location, enum tree_code code,
dc6229e8 1414 tree rhs)
32dc1512 1415{
dc6229e8 1416 if (TREE_CODE_CLASS (code) != tcc_comparison
1417 || TREE_TYPE (rhs) == NULL_TREE
1418 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
32dc1512 1419 return;
1420
16f958b3 1421 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1422 !(x == 0) or !(y != 0). */
1423 if ((code == EQ_EXPR || code == NE_EXPR)
1424 && integer_zerop (rhs))
1425 return;
1426
32dc1512 1427 warning_at (location, OPT_Wlogical_not_parentheses,
1428 "logical not is only applied to the left hand side of "
1429 "comparison");
1430}
b13d1547 1431
3d177e8c 1432/* Warn if EXP contains any computations whose results are not used.
1433 Return true if a warning is printed; false otherwise. LOCUS is the
1434 (potential) location of the expression. */
1435
1436bool
1437warn_if_unused_value (const_tree exp, location_t locus)
1438{
1439 restart:
1440 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1441 return false;
1442
1443 /* Don't warn about void constructs. This includes casting to void,
1444 void function calls, and statement expressions with a final cast
1445 to void. */
1446 if (VOID_TYPE_P (TREE_TYPE (exp)))
1447 return false;
1448
1449 if (EXPR_HAS_LOCATION (exp))
1450 locus = EXPR_LOCATION (exp);
1451
1452 switch (TREE_CODE (exp))
1453 {
1454 case PREINCREMENT_EXPR:
1455 case POSTINCREMENT_EXPR:
1456 case PREDECREMENT_EXPR:
1457 case POSTDECREMENT_EXPR:
1458 case MODIFY_EXPR:
1459 case INIT_EXPR:
1460 case TARGET_EXPR:
1461 case CALL_EXPR:
1462 case TRY_CATCH_EXPR:
1463 case WITH_CLEANUP_EXPR:
1464 case EXIT_EXPR:
1465 case VA_ARG_EXPR:
1466 return false;
1467
1468 case BIND_EXPR:
1469 /* For a binding, warn if no side effect within it. */
1470 exp = BIND_EXPR_BODY (exp);
1471 goto restart;
1472
1473 case SAVE_EXPR:
1474 case NON_LVALUE_EXPR:
d85dbdb3 1475 case NOP_EXPR:
3d177e8c 1476 exp = TREE_OPERAND (exp, 0);
1477 goto restart;
1478
1479 case TRUTH_ORIF_EXPR:
1480 case TRUTH_ANDIF_EXPR:
1481 /* In && or ||, warn if 2nd operand has no side effect. */
1482 exp = TREE_OPERAND (exp, 1);
1483 goto restart;
1484
1485 case COMPOUND_EXPR:
1486 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1487 return true;
1488 /* Let people do `(foo (), 0)' without a warning. */
1489 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1490 return false;
1491 exp = TREE_OPERAND (exp, 1);
1492 goto restart;
1493
1494 case COND_EXPR:
1495 /* If this is an expression with side effects, don't warn; this
1496 case commonly appears in macro expansions. */
1497 if (TREE_SIDE_EFFECTS (exp))
1498 return false;
1499 goto warn;
1500
1501 case INDIRECT_REF:
1502 /* Don't warn about automatic dereferencing of references, since
1503 the user cannot control it. */
1504 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1505 {
1506 exp = TREE_OPERAND (exp, 0);
1507 goto restart;
1508 }
1509 /* Fall through. */
1510
1511 default:
1512 /* Referencing a volatile value is a side effect, so don't warn. */
1513 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1514 && TREE_THIS_VOLATILE (exp))
1515 return false;
1516
1517 /* If this is an expression which has no operands, there is no value
1518 to be unused. There are no such language-independent codes,
1519 but front ends may define such. */
1520 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1521 return false;
1522
1523 warn:
1524 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1525 }
1526}
1527
1528
bcf22371 1529/* Print a warning about casts that might indicate violation
1530 of strict aliasing rules if -Wstrict-aliasing is used and
1e31ff37 1531 strict aliasing mode is in effect. OTYPE is the original
1532 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
bcf22371 1533
e6fa0ea6 1534bool
1e31ff37 1535strict_aliasing_warning (tree otype, tree type, tree expr)
bcf22371 1536{
45bb3afb 1537 /* Strip pointer conversion chains and get to the correct original type. */
1538 STRIP_NOPS (expr);
1539 otype = TREE_TYPE (expr);
1540
f06537f2 1541 if (!(flag_strict_aliasing
1542 && POINTER_TYPE_P (type)
1543 && POINTER_TYPE_P (otype)
1544 && !VOID_TYPE_P (TREE_TYPE (type)))
1545 /* If the type we are casting to is a ref-all pointer
1546 dereferencing it is always valid. */
1547 || TYPE_REF_CAN_ALIAS_ALL (type))
e6fa0ea6 1548 return false;
1549
1550 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
bcf22371 1551 && (DECL_P (TREE_OPERAND (expr, 0))
e6fa0ea6 1552 || handled_component_p (TREE_OPERAND (expr, 0))))
bcf22371 1553 {
1554 /* Casting the address of an object to non void pointer. Warn
1555 if the cast breaks type based aliasing. */
e6fa0ea6 1556 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1557 {
1558 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1559 "might break strict-aliasing rules");
1560 return true;
1561 }
bcf22371 1562 else
1563 {
48e1416a 1564 /* warn_strict_aliasing >= 3. This includes the default (3).
e6fa0ea6 1565 Only warn if the cast is dereferenced immediately. */
32c2fdea 1566 alias_set_type set1 =
e6fa0ea6 1567 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
32c2fdea 1568 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
bcf22371 1569
62d823d0 1570 if (set1 != set2 && set2 != 0
e692d332 1571 && (set1 == 0 || !alias_set_subset_of (set2, set1)))
e6fa0ea6 1572 {
1573 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1574 "pointer will break strict-aliasing rules");
1575 return true;
1576 }
1577 else if (warn_strict_aliasing == 2
879f881c 1578 && !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 1579 {
1580 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1581 "pointer might break strict-aliasing rules");
1582 return true;
1583 }
bcf22371 1584 }
1585 }
e6fa0ea6 1586 else
1587 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1588 {
1589 /* At this level, warn for any conversions, even if an address is
1590 not taken in the same statement. This will likely produce many
1591 false positives, but could be useful to pinpoint problems that
1592 are not revealed at higher levels. */
32c2fdea 1593 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1594 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1595 if (!COMPLETE_TYPE_P (type)
879f881c 1596 || !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 1597 {
1598 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1599 "pointer might break strict-aliasing rules");
1600 return true;
1601 }
1602 }
1603
1604 return false;
bcf22371 1605}
1606
f003f5dc 1607/* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1608 sizeof as last operand of certain builtins. */
1609
1610void
57f872a2 1611sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
f1f41a6c 1612 vec<tree, va_gc> *params, tree *sizeof_arg,
f003f5dc 1613 bool (*comp_types) (tree, tree))
1614{
1615 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
57f872a2 1616 bool strop = false, cmp = false;
1617 unsigned int idx = ~0;
1618 location_t loc;
f003f5dc 1619
1620 if (TREE_CODE (callee) != FUNCTION_DECL
1621 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
f1f41a6c 1622 || vec_safe_length (params) <= 1)
f003f5dc 1623 return;
1624
f003f5dc 1625 switch (DECL_FUNCTION_CODE (callee))
1626 {
1627 case BUILT_IN_STRNCMP:
1628 case BUILT_IN_STRNCASECMP:
57f872a2 1629 cmp = true;
1630 /* FALLTHRU */
f003f5dc 1631 case BUILT_IN_STRNCPY:
57f872a2 1632 case BUILT_IN_STRNCPY_CHK:
f003f5dc 1633 case BUILT_IN_STRNCAT:
57f872a2 1634 case BUILT_IN_STRNCAT_CHK:
1635 case BUILT_IN_STPNCPY:
1636 case BUILT_IN_STPNCPY_CHK:
f003f5dc 1637 strop = true;
1638 /* FALLTHRU */
1639 case BUILT_IN_MEMCPY:
57f872a2 1640 case BUILT_IN_MEMCPY_CHK:
f003f5dc 1641 case BUILT_IN_MEMMOVE:
57f872a2 1642 case BUILT_IN_MEMMOVE_CHK:
f1f41a6c 1643 if (params->length () < 3)
57f872a2 1644 return;
f1f41a6c 1645 src = (*params)[1];
1646 dest = (*params)[0];
57f872a2 1647 idx = 2;
1648 break;
1649 case BUILT_IN_BCOPY:
f1f41a6c 1650 if (params->length () < 3)
57f872a2 1651 return;
f1f41a6c 1652 src = (*params)[0];
1653 dest = (*params)[1];
57f872a2 1654 idx = 2;
1655 break;
f003f5dc 1656 case BUILT_IN_MEMCMP:
57f872a2 1657 case BUILT_IN_BCMP:
f1f41a6c 1658 if (params->length () < 3)
f003f5dc 1659 return;
f1f41a6c 1660 src = (*params)[1];
1661 dest = (*params)[0];
57f872a2 1662 idx = 2;
1663 cmp = true;
f003f5dc 1664 break;
1665 case BUILT_IN_MEMSET:
57f872a2 1666 case BUILT_IN_MEMSET_CHK:
f1f41a6c 1667 if (params->length () < 3)
f003f5dc 1668 return;
f1f41a6c 1669 dest = (*params)[0];
57f872a2 1670 idx = 2;
1671 break;
1672 case BUILT_IN_BZERO:
f1f41a6c 1673 dest = (*params)[0];
57f872a2 1674 idx = 1;
f003f5dc 1675 break;
1676 case BUILT_IN_STRNDUP:
f1f41a6c 1677 src = (*params)[0];
f003f5dc 1678 strop = true;
57f872a2 1679 idx = 1;
1680 break;
1681 case BUILT_IN_MEMCHR:
f1f41a6c 1682 if (params->length () < 3)
57f872a2 1683 return;
f1f41a6c 1684 src = (*params)[0];
57f872a2 1685 idx = 2;
1686 break;
1687 case BUILT_IN_SNPRINTF:
1688 case BUILT_IN_SNPRINTF_CHK:
1689 case BUILT_IN_VSNPRINTF:
1690 case BUILT_IN_VSNPRINTF_CHK:
f1f41a6c 1691 dest = (*params)[0];
57f872a2 1692 idx = 1;
1693 strop = true;
f003f5dc 1694 break;
1695 default:
1696 break;
1697 }
1698
57f872a2 1699 if (idx >= 3)
1700 return;
1701
1702 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1703 return;
1704
1705 type = TYPE_P (sizeof_arg[idx])
1706 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1707 if (!POINTER_TYPE_P (type))
1708 return;
1709
f003f5dc 1710 if (dest
1711 && (tem = tree_strip_nop_conversions (dest))
1712 && POINTER_TYPE_P (TREE_TYPE (tem))
1713 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1714 return;
1715
1716 if (src
1717 && (tem = tree_strip_nop_conversions (src))
1718 && POINTER_TYPE_P (TREE_TYPE (tem))
1719 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1720 return;
1721
57f872a2 1722 loc = sizeof_arg_loc[idx];
1723
1724 if (dest && !cmp)
f003f5dc 1725 {
57f872a2 1726 if (!TYPE_P (sizeof_arg[idx])
1727 && operand_equal_p (dest, sizeof_arg[idx], 0)
f003f5dc 1728 && comp_types (TREE_TYPE (dest), type))
1729 {
57f872a2 1730 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
f003f5dc 1731 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1732 "argument to %<sizeof%> in %qD call is the same "
1733 "expression as the destination; did you mean to "
1734 "remove the addressof?", callee);
1735 else if ((TYPE_PRECISION (TREE_TYPE (type))
1736 == TYPE_PRECISION (char_type_node))
1737 || strop)
1738 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1739 "argument to %<sizeof%> in %qD call is the same "
1740 "expression as the destination; did you mean to "
1741 "provide an explicit length?", callee);
1742 else
1743 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1744 "argument to %<sizeof%> in %qD call is the same "
1745 "expression as the destination; did you mean to "
1746 "dereference it?", callee);
1747 return;
1748 }
1749
1750 if (POINTER_TYPE_P (TREE_TYPE (dest))
1751 && !strop
1752 && comp_types (TREE_TYPE (dest), type)
1753 && !VOID_TYPE_P (TREE_TYPE (type)))
1754 {
1755 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1756 "argument to %<sizeof%> in %qD call is the same "
1757 "pointer type %qT as the destination; expected %qT "
1758 "or an explicit length", callee, TREE_TYPE (dest),
1759 TREE_TYPE (TREE_TYPE (dest)));
1760 return;
1761 }
1762 }
1763
57f872a2 1764 if (src && !cmp)
f003f5dc 1765 {
57f872a2 1766 if (!TYPE_P (sizeof_arg[idx])
1767 && operand_equal_p (src, sizeof_arg[idx], 0)
f003f5dc 1768 && comp_types (TREE_TYPE (src), type))
1769 {
57f872a2 1770 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
f003f5dc 1771 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1772 "argument to %<sizeof%> in %qD call is the same "
1773 "expression as the source; did you mean to "
1774 "remove the addressof?", callee);
1775 else if ((TYPE_PRECISION (TREE_TYPE (type))
1776 == TYPE_PRECISION (char_type_node))
1777 || strop)
1778 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1779 "argument to %<sizeof%> in %qD call is the same "
1780 "expression as the source; did you mean to "
1781 "provide an explicit length?", callee);
1782 else
1783 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1784 "argument to %<sizeof%> in %qD call is the same "
1785 "expression as the source; did you mean to "
1786 "dereference it?", callee);
1787 return;
1788 }
1789
1790 if (POINTER_TYPE_P (TREE_TYPE (src))
1791 && !strop
1792 && comp_types (TREE_TYPE (src), type)
1793 && !VOID_TYPE_P (TREE_TYPE (type)))
1794 {
1795 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1796 "argument to %<sizeof%> in %qD call is the same "
1797 "pointer type %qT as the source; expected %qT "
1798 "or an explicit length", callee, TREE_TYPE (src),
1799 TREE_TYPE (TREE_TYPE (src)));
1800 return;
1801 }
1802 }
57f872a2 1803
1804 if (dest)
1805 {
1806 if (!TYPE_P (sizeof_arg[idx])
1807 && operand_equal_p (dest, sizeof_arg[idx], 0)
1808 && comp_types (TREE_TYPE (dest), type))
1809 {
1810 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1811 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1812 "argument to %<sizeof%> in %qD call is the same "
1813 "expression as the first source; did you mean to "
1814 "remove the addressof?", callee);
1815 else if ((TYPE_PRECISION (TREE_TYPE (type))
1816 == TYPE_PRECISION (char_type_node))
1817 || strop)
1818 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1819 "argument to %<sizeof%> in %qD call is the same "
1820 "expression as the first source; did you mean to "
1821 "provide an explicit length?", callee);
1822 else
1823 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1824 "argument to %<sizeof%> in %qD call is the same "
1825 "expression as the first source; did you mean to "
1826 "dereference it?", callee);
1827 return;
1828 }
1829
1830 if (POINTER_TYPE_P (TREE_TYPE (dest))
1831 && !strop
1832 && comp_types (TREE_TYPE (dest), type)
1833 && !VOID_TYPE_P (TREE_TYPE (type)))
1834 {
1835 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1836 "argument to %<sizeof%> in %qD call is the same "
1837 "pointer type %qT as the first source; expected %qT "
1838 "or an explicit length", callee, TREE_TYPE (dest),
1839 TREE_TYPE (TREE_TYPE (dest)));
1840 return;
1841 }
1842 }
1843
1844 if (src)
1845 {
1846 if (!TYPE_P (sizeof_arg[idx])
1847 && operand_equal_p (src, sizeof_arg[idx], 0)
1848 && comp_types (TREE_TYPE (src), type))
1849 {
1850 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1851 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1852 "argument to %<sizeof%> in %qD call is the same "
1853 "expression as the second source; did you mean to "
1854 "remove the addressof?", callee);
1855 else if ((TYPE_PRECISION (TREE_TYPE (type))
1856 == TYPE_PRECISION (char_type_node))
1857 || strop)
1858 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1859 "argument to %<sizeof%> in %qD call is the same "
1860 "expression as the second source; did you mean to "
1861 "provide an explicit length?", callee);
1862 else
1863 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1864 "argument to %<sizeof%> in %qD call is the same "
1865 "expression as the second source; did you mean to "
1866 "dereference it?", callee);
1867 return;
1868 }
1869
1870 if (POINTER_TYPE_P (TREE_TYPE (src))
1871 && !strop
1872 && comp_types (TREE_TYPE (src), type)
1873 && !VOID_TYPE_P (TREE_TYPE (type)))
1874 {
1875 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1876 "argument to %<sizeof%> in %qD call is the same "
1877 "pointer type %qT as the second source; expected %qT "
1878 "or an explicit length", callee, TREE_TYPE (src),
1879 TREE_TYPE (TREE_TYPE (src)));
1880 return;
1881 }
1882 }
1883
f003f5dc 1884}
1885
3f08e399 1886/* Warn for unlikely, improbable, or stupid DECL declarations
1887 of `main'. */
1888
1889void
1890check_main_parameter_types (tree decl)
1891{
d0af78c5 1892 function_args_iterator iter;
1893 tree type;
3f08e399 1894 int argct = 0;
1895
d0af78c5 1896 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
1897 {
1898 /* XXX void_type_node belies the abstraction. */
1899 if (type == void_type_node || type == error_mark_node )
1900 break;
1901
2026249a 1902 tree t = type;
1903 if (TYPE_ATOMIC (t))
1904 pedwarn (input_location, OPT_Wmain,
1905 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1906 type, decl);
1907 while (POINTER_TYPE_P (t))
1908 {
1909 t = TREE_TYPE (t);
1910 if (TYPE_ATOMIC (t))
1911 pedwarn (input_location, OPT_Wmain,
1912 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1913 type, decl);
1914 }
1915
d0af78c5 1916 ++argct;
1917 switch (argct)
1918 {
1919 case 1:
1920 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1921 pedwarn (input_location, OPT_Wmain,
1922 "first argument of %q+D should be %<int%>", decl);
1923 break;
1924
1925 case 2:
1926 if (TREE_CODE (type) != POINTER_TYPE
1927 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1928 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1929 != char_type_node))
1930 pedwarn (input_location, OPT_Wmain,
1931 "second argument of %q+D should be %<char **%>", decl);
1932 break;
1933
1934 case 3:
1935 if (TREE_CODE (type) != POINTER_TYPE
1936 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1937 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1938 != char_type_node))
1939 pedwarn (input_location, OPT_Wmain,
1940 "third argument of %q+D should probably be "
1941 "%<char **%>", decl);
1942 break;
1943 }
1944 }
3f08e399 1945
1946 /* It is intentional that this message does not mention the third
1947 argument because it's only mentioned in an appendix of the
1948 standard. */
1949 if (argct > 0 && (argct < 2 || argct > 3))
d0af78c5 1950 pedwarn (input_location, OPT_Wmain,
1951 "%q+D takes only zero or two arguments", decl);
90e645fa 1952
1953 if (stdarg_p (TREE_TYPE (decl)))
1954 pedwarn (input_location, OPT_Wmain,
1955 "%q+D declared as variadic function", decl);
3f08e399 1956}
1957
73437615 1958/* vector_targets_convertible_p is used for vector pointer types. The
1959 callers perform various checks that the qualifiers are satisfactory,
1960 while OTOH vector_targets_convertible_p ignores the number of elements
1961 in the vectors. That's fine with vector pointers as we can consider,
1962 say, a vector of 8 elements as two consecutive vectors of 4 elements,
1963 and that does not require and conversion of the pointer values.
1964 In contrast, vector_types_convertible_p and
1965 vector_types_compatible_elements_p are used for vector value types. */
ed7c4e62 1966/* True if pointers to distinct types T1 and T2 can be converted to
1967 each other without an explicit cast. Only returns true for opaque
1968 vector types. */
1969bool
1970vector_targets_convertible_p (const_tree t1, const_tree t2)
1971{
6290f0db 1972 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
8d125f7d 1973 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ed7c4e62 1974 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1975 return true;
1976
1977 return false;
1978}
1979
73437615 1980/* vector_types_convertible_p is used for vector value types.
1981 It could in principle call vector_targets_convertible_p as a subroutine,
1982 but then the check for vector type would be duplicated with its callers,
1983 and also the purpose of vector_targets_convertible_p would become
1984 muddled.
1985 Where vector_types_convertible_p returns true, a conversion might still be
1986 needed to make the types match.
1987 In contrast, vector_targets_convertible_p is used for vector pointer
1988 values, and vector_types_compatible_elements_p is used specifically
1989 in the context for binary operators, as a check if use is possible without
1990 conversion. */
546c4794 1991/* True if vector types T1 and T2 can be converted to each other
1992 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
1993 can only be converted with -flax-vector-conversions yet that is not
1994 in effect, emit a note telling the user about that option if such
1995 a note has not previously been emitted. */
1996bool
9f627b1a 1997vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
8b4b9810 1998{
546c4794 1999 static bool emitted_lax_note = false;
ae6db8ab 2000 bool convertible_lax;
2001
8d125f7d 2002 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ae6db8ab 2003 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2004 return true;
2005
2006 convertible_lax =
2007 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2008 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
432dd330 2009 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
ae6db8ab 2010 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2011 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
546c4794 2012
2013 if (!convertible_lax || flag_lax_vector_conversions)
2014 return convertible_lax;
2015
2016 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
29f51994 2017 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
546c4794 2018 return true;
2019
2020 if (emit_lax_note && !emitted_lax_note)
2021 {
2022 emitted_lax_note = true;
5bcc316e 2023 inform (input_location, "use -flax-vector-conversions to permit "
546c4794 2024 "conversions between vectors with differing "
2025 "element types or numbers of subparts");
2026 }
2027
2028 return false;
8b4b9810 2029}
2030
bf0cb017 2031/* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2032 and have vector types, V0 has the same type as V1, and the number of
2033 elements of V0, V1, MASK is the same.
2034
2035 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2036 called with two arguments. In this case implementation passes the
2037 first argument twice in order to share the same tree code. This fact
2038 could enable the mask-values being twice the vector length. This is
2039 an implementation accident and this semantics is not guaranteed to
2040 the user. */
2041tree
68ea4406 2042c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2043 bool complain)
bf0cb017 2044{
2045 tree ret;
2046 bool wrap = true;
2047 bool maybe_const = false;
2048 bool two_arguments = false;
2049
2050 if (v1 == NULL_TREE)
2051 {
2052 two_arguments = true;
2053 v1 = v0;
2054 }
2055
2056 if (v0 == error_mark_node || v1 == error_mark_node
2057 || mask == error_mark_node)
2058 return error_mark_node;
2059
6290f0db 2060 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
bf0cb017 2061 {
68ea4406 2062 if (complain)
2063 error_at (loc, "__builtin_shuffle last argument must "
2064 "be an integer vector");
bf0cb017 2065 return error_mark_node;
2066 }
2067
6290f0db 2068 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
2069 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
bf0cb017 2070 {
68ea4406 2071 if (complain)
2072 error_at (loc, "__builtin_shuffle arguments must be vectors");
bf0cb017 2073 return error_mark_node;
2074 }
2075
2076 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2077 {
68ea4406 2078 if (complain)
2079 error_at (loc, "__builtin_shuffle argument vectors must be of "
2080 "the same type");
bf0cb017 2081 return error_mark_node;
2082 }
2083
2084 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2085 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2086 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2087 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2088 {
68ea4406 2089 if (complain)
2090 error_at (loc, "__builtin_shuffle number of elements of the "
2091 "argument vector(s) and the mask vector should "
2092 "be the same");
bf0cb017 2093 return error_mark_node;
2094 }
2095
2096 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2097 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2098 {
68ea4406 2099 if (complain)
2100 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2101 "must have the same size as inner type of the mask");
bf0cb017 2102 return error_mark_node;
2103 }
2104
2105 if (!c_dialect_cxx ())
2106 {
2107 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2108 v0 = c_fully_fold (v0, false, &maybe_const);
2109 wrap &= maybe_const;
2110
2111 if (two_arguments)
2112 v1 = v0 = save_expr (v0);
2113 else
2114 {
2115 v1 = c_fully_fold (v1, false, &maybe_const);
2116 wrap &= maybe_const;
2117 }
2118
2119 mask = c_fully_fold (mask, false, &maybe_const);
2120 wrap &= maybe_const;
2121 }
68ea4406 2122 else if (two_arguments)
2123 v1 = v0 = save_expr (v0);
bf0cb017 2124
2125 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2126
2127 if (!c_dialect_cxx () && !wrap)
2128 ret = c_wrap_maybe_const (ret, true);
2129
2130 return ret;
2131}
2132
7f506bca 2133/* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2134 to integral type. */
2135
2136static tree
2137c_common_get_narrower (tree op, int *unsignedp_ptr)
2138{
2139 op = get_narrower (op, unsignedp_ptr);
2140
2141 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2142 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2143 {
2144 /* C++0x scoped enumerations don't implicitly convert to integral
2145 type; if we stripped an explicit conversion to a larger type we
2146 need to replace it so common_type will still work. */
a51edb4c 2147 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2148 TYPE_UNSIGNED (TREE_TYPE (op)));
7f506bca 2149 op = fold_convert (type, op);
2150 }
2151 return op;
2152}
2153
2561cea2 2154/* This is a helper function of build_binary_op.
2155
2156 For certain operations if both args were extended from the same
2157 smaller type, do the arithmetic in that type and then extend.
2158
2159 BITWISE indicates a bitwise operation.
2160 For them, this optimization is safe only if
2161 both args are zero-extended or both are sign-extended.
2162 Otherwise, we might change the result.
2163 Eg, (short)-1 | (unsigned short)-1 is (int)-1
48e1416a 2164 but calculated in (unsigned short) it would be (unsigned short)-1.
2561cea2 2165*/
7f506bca 2166tree
2167shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2561cea2 2168{
2169 int unsigned0, unsigned1;
2170 tree arg0, arg1;
2171 int uns;
2172 tree type;
2173
2174 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2175 excessive narrowing when we call get_narrower below. For
2176 example, suppose that OP0 is of unsigned int extended
2177 from signed char and that RESULT_TYPE is long long int.
2178 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2179 like
48e1416a 2180
2561cea2 2181 (long long int) (unsigned int) signed_char
2182
2183 which get_narrower would narrow down to
48e1416a 2184
2561cea2 2185 (unsigned int) signed char
48e1416a 2186
2561cea2 2187 If we do not cast OP0 first, get_narrower would return
2188 signed_char, which is inconsistent with the case of the
2189 explicit cast. */
2190 op0 = convert (result_type, op0);
2191 op1 = convert (result_type, op1);
2192
7f506bca 2193 arg0 = c_common_get_narrower (op0, &unsigned0);
2194 arg1 = c_common_get_narrower (op1, &unsigned1);
ab2c1de8 2195
2561cea2 2196 /* UNS is 1 if the operation to be done is an unsigned one. */
2197 uns = TYPE_UNSIGNED (result_type);
2198
2199 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2200 but it *requires* conversion to FINAL_TYPE. */
48e1416a 2201
2561cea2 2202 if ((TYPE_PRECISION (TREE_TYPE (op0))
2203 == TYPE_PRECISION (TREE_TYPE (arg0)))
2204 && TREE_TYPE (op0) != result_type)
2205 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2206 if ((TYPE_PRECISION (TREE_TYPE (op1))
2207 == TYPE_PRECISION (TREE_TYPE (arg1)))
2208 && TREE_TYPE (op1) != result_type)
2209 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
48e1416a 2210
2561cea2 2211 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
48e1416a 2212
2561cea2 2213 /* For bitwise operations, signedness of nominal type
2214 does not matter. Consider only how operands were extended. */
2215 if (bitwise)
2216 uns = unsigned0;
48e1416a 2217
2561cea2 2218 /* Note that in all three cases below we refrain from optimizing
2219 an unsigned operation on sign-extended args.
2220 That would not be valid. */
48e1416a 2221
2561cea2 2222 /* Both args variable: if both extended in same way
2223 from same width, do it in that width.
2224 Do it unsigned if args were zero-extended. */
2225 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2226 < TYPE_PRECISION (result_type))
2227 && (TYPE_PRECISION (TREE_TYPE (arg1))
2228 == TYPE_PRECISION (TREE_TYPE (arg0)))
2229 && unsigned0 == unsigned1
2230 && (unsigned0 || !uns))
2231 return c_common_signed_or_unsigned_type
2232 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2233
2234 else if (TREE_CODE (arg0) == INTEGER_CST
2235 && (unsigned1 || !uns)
2236 && (TYPE_PRECISION (TREE_TYPE (arg1))
2237 < TYPE_PRECISION (result_type))
2238 && (type
2239 = c_common_signed_or_unsigned_type (unsigned1,
2240 TREE_TYPE (arg1)))
2241 && !POINTER_TYPE_P (type)
2242 && int_fits_type_p (arg0, type))
2243 return type;
2244
2245 else if (TREE_CODE (arg1) == INTEGER_CST
2246 && (unsigned0 || !uns)
2247 && (TYPE_PRECISION (TREE_TYPE (arg0))
2248 < TYPE_PRECISION (result_type))
2249 && (type
2250 = c_common_signed_or_unsigned_type (unsigned0,
2251 TREE_TYPE (arg0)))
2252 && !POINTER_TYPE_P (type)
2253 && int_fits_type_p (arg1, type))
2254 return type;
2255
2256 return result_type;
2257}
2258
e53013a8 2259/* Returns true iff any integer value of type FROM_TYPE can be represented as
2260 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2261
2262static bool
2263int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2264{
2265 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2266 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2267 REAL_VALUE_TYPE real_low_bound =
2268 real_value_from_int_cst (0, type_low_bound);
2269 REAL_VALUE_TYPE real_high_bound =
2270 real_value_from_int_cst (0, type_high_bound);
2271
2272 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2273 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2274}
2275
2276/* Checks if expression EXPR of complex/real/integer type cannot be converted
2277 to the complex/real/integer type TYPE. Function returns non-zero when:
22a75734 2278 * EXPR is a constant which cannot be exactly converted to TYPE.
2279 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
e53013a8 2280 for EXPR type and TYPE being both integers or both real, or both
2281 complex.
2282 * EXPR is not a constant of complex type and TYPE is a real or
2283 an integer.
22a75734 2284 * EXPR is not a constant of real type and TYPE is an integer.
2285 * EXPR is not a constant of integer type which cannot be
2286 exactly converted to real type.
e53013a8 2287
7dfa155b 2288 Function allows conversions between types of different signedness and
ca9d7d74 2289 can return SAFE_CONVERSION (zero) in that case. Function can produce
e53013a8 2290 signedness warnings if PRODUCE_WARNS is true.
2291
2292 Function allows conversions from complex constants to non-complex types,
2293 provided that imaginary part is zero and real part can be safely converted
2294 to TYPE. */
22a75734 2295
ca9d7d74 2296enum conversion_safety
22a75734 2297unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
d31d55f0 2298{
ca9d7d74 2299 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
27259707 2300 tree expr_type = TREE_TYPE (expr);
22a75734 2301 loc = expansion_point_location_if_in_system_header (loc);
d31d55f0 2302
7dfa155b 2303 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
5b16c152 2304 {
e53013a8 2305 /* If type is complex, we are interested in compatibility with
2306 underlying type. */
2307 if (TREE_CODE (type) == COMPLEX_TYPE)
2308 type = TREE_TYPE (type);
2309
d31d55f0 2310 /* Warn for real constant that is not an exact integer converted
7dfa155b 2311 to integer type. */
27259707 2312 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2313 && TREE_CODE (type) == INTEGER_TYPE)
2314 {
2315 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
ca9d7d74 2316 give_warning = UNSAFE_REAL;
7dfa155b 2317 }
da1fb07b 2318 /* Warn for an integer constant that does not fit into integer type. */
27259707 2319 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2320 && TREE_CODE (type) == INTEGER_TYPE
2321 && !int_fits_type_p (expr, type))
2322 {
2323 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
27259707 2324 && tree_int_cst_sgn (expr) < 0)
7dfa155b 2325 {
2326 if (produce_warns)
2327 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2328 " implicitly converted to unsigned type");
2329 }
2330 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2331 {
2332 if (produce_warns)
2333 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2334 " constant value to negative integer");
2335 }
7ee0d227 2336 else
ca9d7d74 2337 give_warning = UNSAFE_OTHER;
7dfa155b 2338 }
d31d55f0 2339 else if (TREE_CODE (type) == REAL_TYPE)
7dfa155b 2340 {
2341 /* Warn for an integer constant that does not fit into real type. */
2342 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2343 {
2344 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2345 if (!exact_real_truncate (TYPE_MODE (type), &a))
ca9d7d74 2346 give_warning = UNSAFE_REAL;
7dfa155b 2347 }
2348 /* Warn for a real constant that does not fit into a smaller
2349 real type. */
2350 else if (TREE_CODE (expr_type) == REAL_TYPE
2351 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2352 {
2353 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2354 if (!exact_real_truncate (TYPE_MODE (type), &a))
ca9d7d74 2355 give_warning = UNSAFE_REAL;
7dfa155b 2356 }
2357 }
2358 }
e53013a8 2359
2360 else if (TREE_CODE (expr) == COMPLEX_CST)
2361 {
2362 tree imag_part = TREE_IMAGPART (expr);
2363 /* Conversion from complex constant with zero imaginary part,
2364 perform check for conversion of real part. */
2365 if ((TREE_CODE (imag_part) == REAL_CST
2366 && real_zerop (imag_part))
2367 || (TREE_CODE (imag_part) == INTEGER_CST
2368 && integer_zerop (imag_part)))
2369 /* Note: in this branch we use recursive call to unsafe_conversion_p
2370 with different type of EXPR, but it is still safe, because when EXPR
2371 is a constant, it's type is not used in text of generated warnings
2372 (otherwise they could sound misleading). */
2373 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2374 produce_warns);
2375 /* Conversion from complex constant with non-zero imaginary part. */
2376 else
2377 {
2378 /* Conversion to complex type.
2379 Perform checks for both real and imaginary parts. */
2380 if (TREE_CODE (type) == COMPLEX_TYPE)
2381 {
2382 /* Unfortunately, produce_warns must be false in two subsequent
2383 calls of unsafe_conversion_p, because otherwise we could
2384 produce strange "double" warnings, if both real and imaginary
2385 parts have conversion problems related to signedness.
2386
2387 For example:
2388 int32_t _Complex a = 0x80000000 + 0x80000000i;
2389
2390 Possible solution: add a separate function for checking
2391 constants and combine result of two calls appropriately. */
2392 enum conversion_safety re_safety =
2393 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2394 enum conversion_safety im_safety =
2395 unsafe_conversion_p (loc, type, imag_part, false);
2396
2397 /* Merge the results into appropriate single warning. */
2398
2399 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2400 if (re_safety == im_safety)
2401 give_warning = re_safety;
2402 else if (!re_safety && im_safety)
2403 give_warning = im_safety;
2404 else if (re_safety && !im_safety)
2405 give_warning = re_safety;
2406 else
2407 give_warning = UNSAFE_OTHER;
2408 }
2409 /* Warn about conversion from complex to real or integer type. */
2410 else
2411 give_warning = UNSAFE_IMAGINARY;
2412 }
2413 }
2414
2415 /* Checks for remaining case: EXPR is not constant. */
7dfa155b 2416 else
2417 {
d31d55f0 2418 /* Warn for real types converted to integer types. */
2561cea2 2419 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2420 && TREE_CODE (type) == INTEGER_TYPE)
ca9d7d74 2421 give_warning = UNSAFE_REAL;
d31d55f0 2422
2561cea2 2423 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2424 && TREE_CODE (type) == INTEGER_TYPE)
2425 {
69609004 2426 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
f9d856a4 2427 expr = get_unwidened (expr, 0);
2561cea2 2428 expr_type = TREE_TYPE (expr);
69609004 2429
2561cea2 2430 /* Don't warn for short y; short x = ((int)y & 0xff); */
48e1416a 2431 if (TREE_CODE (expr) == BIT_AND_EXPR
7dfa155b 2432 || TREE_CODE (expr) == BIT_IOR_EXPR
2561cea2 2433 || TREE_CODE (expr) == BIT_XOR_EXPR)
2434 {
27259707 2435 /* If both args were extended from a shortest type,
2436 use that type if that is safe. */
48e1416a 2437 expr_type = shorten_binary_op (expr_type,
2438 TREE_OPERAND (expr, 0),
2439 TREE_OPERAND (expr, 1),
2561cea2 2440 /* bitwise */1);
2441
2561cea2 2442 if (TREE_CODE (expr) == BIT_AND_EXPR)
2443 {
2444 tree op0 = TREE_OPERAND (expr, 0);
2445 tree op1 = TREE_OPERAND (expr, 1);
30de145b 2446 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2447 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2448
2449 /* If one of the operands is a non-negative constant
2450 that fits in the target type, then the type of the
2451 other operand does not matter. */
2561cea2 2452 if ((TREE_CODE (op0) == INTEGER_CST
2453 && int_fits_type_p (op0, c_common_signed_type (type))
2454 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2455 || (TREE_CODE (op1) == INTEGER_CST
27259707 2456 && int_fits_type_p (op1, c_common_signed_type (type))
48e1416a 2457 && int_fits_type_p (op1,
27259707 2458 c_common_unsigned_type (type))))
ca9d7d74 2459 return SAFE_CONVERSION;
30de145b 2460 /* If constant is unsigned and fits in the target
2461 type, then the result will also fit. */
2462 else if ((TREE_CODE (op0) == INTEGER_CST
48e1416a 2463 && unsigned0
30de145b 2464 && int_fits_type_p (op0, type))
2465 || (TREE_CODE (op1) == INTEGER_CST
2466 && unsigned1
2467 && int_fits_type_p (op1, type)))
ca9d7d74 2468 return SAFE_CONVERSION;
2561cea2 2469 }
2470 }
7dfa155b 2471 /* Warn for integer types converted to smaller integer types. */
48e1416a 2472 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
ca9d7d74 2473 give_warning = UNSAFE_OTHER;
7ee0d227 2474
2475 /* When they are the same width but different signedness,
2476 then the value may change. */
7dfa155b 2477 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2561cea2 2478 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
7ee0d227 2479 /* Even when converted to a bigger type, if the type is
2480 unsigned but expr is signed, then negative values
2481 will be changed. */
7dfa155b 2482 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2483 && produce_warns)
200dd99c 2484 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2485 "may change the sign of the result",
2486 type, expr_type);
7dfa155b 2487 }
d31d55f0 2488
2489 /* Warn for integer types converted to real types if and only if
7dfa155b 2490 all the range of values of the integer type cannot be
2491 represented by the real type. */
2561cea2 2492 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2493 && TREE_CODE (type) == REAL_TYPE)
2494 {
4c2cfa81 2495 /* Don't warn about char y = 0xff; float x = (int) y; */
2496 expr = get_unwidened (expr, 0);
2497 expr_type = TREE_TYPE (expr);
2498
e53013a8 2499 if (!int_safely_convertible_to_real_p (expr_type, type))
ca9d7d74 2500 give_warning = UNSAFE_OTHER;
7dfa155b 2501 }
d31d55f0 2502
2503 /* Warn for real types converted to smaller real types. */
2561cea2 2504 else if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2505 && TREE_CODE (type) == REAL_TYPE
2506 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
ca9d7d74 2507 give_warning = UNSAFE_REAL;
e53013a8 2508
2509 /* Check conversion between two complex types. */
2510 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2511 && TREE_CODE (type) == COMPLEX_TYPE)
2512 {
2513 /* Extract underlying types (i.e., type of real and imaginary
2514 parts) of expr_type and type. */
2515 tree from_type = TREE_TYPE (expr_type);
2516 tree to_type = TREE_TYPE (type);
2517
2518 /* Warn for real types converted to integer types. */
2519 if (TREE_CODE (from_type) == REAL_TYPE
2520 && TREE_CODE (to_type) == INTEGER_TYPE)
2521 give_warning = UNSAFE_REAL;
2522
2523 /* Warn for real types converted to smaller real types. */
2524 else if (TREE_CODE (from_type) == REAL_TYPE
2525 && TREE_CODE (to_type) == REAL_TYPE
2526 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2527 give_warning = UNSAFE_REAL;
2528
2529 /* Check conversion for complex integer types. Here implementation
2530 is simpler than for real-domain integers because it does not
2531 involve sophisticated cases, such as bitmasks, casts, etc. */
2532 else if (TREE_CODE (from_type) == INTEGER_TYPE
2533 && TREE_CODE (to_type) == INTEGER_TYPE)
2534 {
2535 /* Warn for integer types converted to smaller integer types. */
2536 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2537 give_warning = UNSAFE_OTHER;
2538
2539 /* Check for different signedness, see case for real-domain
2540 integers (above) for a more detailed comment. */
2541 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
2542 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
2543 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
2544 && produce_warns)
2545 warning_at (loc, OPT_Wsign_conversion,
2546 "conversion to %qT from %qT "
2547 "may change the sign of the result",
2548 type, expr_type);
2549 }
2550 else if (TREE_CODE (from_type) == INTEGER_TYPE
2551 && TREE_CODE (to_type) == REAL_TYPE
2552 && !int_safely_convertible_to_real_p (from_type, to_type))
2553 give_warning = UNSAFE_OTHER;
2554 }
2555
2556 /* Warn for complex types converted to real or integer types. */
2557 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2558 && TREE_CODE (type) != COMPLEX_TYPE)
2559 give_warning = UNSAFE_IMAGINARY;
7dfa155b 2560 }
2561
2562 return give_warning;
2563}
2564
2565/* Warns if the conversion of EXPR to TYPE may alter a value.
2566 This is a helper function for warnings_for_convert_and_check. */
2567
2568static void
22a75734 2569conversion_warning (location_t loc, tree type, tree expr)
7dfa155b 2570{
7dfa155b 2571 tree expr_type = TREE_TYPE (expr);
ca9d7d74 2572 enum conversion_safety conversion_kind;
d31d55f0 2573
ca9d7d74 2574 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
7dfa155b 2575 return;
d31d55f0 2576
ec704957 2577 /* This may happen, because for LHS op= RHS we preevaluate
2578 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2579 means we could no longer see the code of the EXPR. */
2580 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2581 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2582 if (TREE_CODE (expr) == SAVE_EXPR)
2583 expr = TREE_OPERAND (expr, 0);
2584
7dfa155b 2585 switch (TREE_CODE (expr))
2586 {
2587 case EQ_EXPR:
2588 case NE_EXPR:
2589 case LE_EXPR:
2590 case GE_EXPR:
2591 case LT_EXPR:
2592 case GT_EXPR:
2593 case TRUTH_ANDIF_EXPR:
2594 case TRUTH_ORIF_EXPR:
2595 case TRUTH_AND_EXPR:
2596 case TRUTH_OR_EXPR:
2597 case TRUTH_XOR_EXPR:
2598 case TRUTH_NOT_EXPR:
2599 /* Conversion from boolean to a signed:1 bit-field (which only
2600 can hold the values 0 and -1) doesn't lose information - but
2601 it does change the value. */
2602 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2603 warning_at (loc, OPT_Wconversion,
2604 "conversion to %qT from boolean expression", type);
2605 return;
2606
2607 case REAL_CST:
2608 case INTEGER_CST:
e53013a8 2609 case COMPLEX_CST:
22a75734 2610 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
ca9d7d74 2611 if (conversion_kind == UNSAFE_REAL)
2612 warning_at (loc, OPT_Wfloat_conversion,
2613 "conversion to %qT alters %qT constant value",
2614 type, expr_type);
2615 else if (conversion_kind)
7dfa155b 2616 warning_at (loc, OPT_Wconversion,
2617 "conversion to %qT alters %qT constant value",
2618 type, expr_type);
2619 return;
2620
2621 case COND_EXPR:
2622 {
0e4e775a 2623 /* In case of COND_EXPR, we do not care about the type of
2624 COND_EXPR, only about the conversion of each operand. */
2625 tree op1 = TREE_OPERAND (expr, 1);
2626 tree op2 = TREE_OPERAND (expr, 2);
2627
22a75734 2628 conversion_warning (loc, type, op1);
2629 conversion_warning (loc, type, op2);
0e4e775a 2630 return;
7dfa155b 2631 }
2632
2633 default: /* 'expr' is not a constant. */
22a75734 2634 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
ca9d7d74 2635 if (conversion_kind == UNSAFE_REAL)
2636 warning_at (loc, OPT_Wfloat_conversion,
2637 "conversion to %qT from %qT may alter its value",
2638 type, expr_type);
e53013a8 2639 else if (conversion_kind == UNSAFE_IMAGINARY)
2640 warning_at (loc, OPT_Wconversion,
2641 "conversion to %qT from %qT discards imaginary component",
2642 type, expr_type);
ca9d7d74 2643 else if (conversion_kind)
7dfa155b 2644 warning_at (loc, OPT_Wconversion,
200dd99c 2645 "conversion to %qT from %qT may alter its value",
2646 type, expr_type);
d31d55f0 2647 }
2648}
2649
59dd8856 2650/* Produce warnings after a conversion. RESULT is the result of
2651 converting EXPR to TYPE. This is a helper function for
2652 convert_and_check and cp_convert_and_check. */
2a1736ed 2653
59dd8856 2654void
22a75734 2655warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2656 tree result)
2a1736ed 2657{
22a75734 2658 loc = expansion_point_location_if_in_system_header (loc);
61f69bc9 2659
da1fb07b 2660 if (TREE_CODE (expr) == INTEGER_CST
2661 && (TREE_CODE (type) == INTEGER_TYPE
2662 || TREE_CODE (type) == ENUMERAL_TYPE)
2663 && !int_fits_type_p (expr, type))
2664 {
d31d55f0 2665 /* Do not diagnose overflow in a constant expression merely
2666 because a conversion overflowed. */
da1fb07b 2667 if (TREE_OVERFLOW (result))
eddad94a 2668 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2669
da1fb07b 2670 if (TYPE_UNSIGNED (type))
d31d55f0 2671 {
da1fb07b 2672 /* This detects cases like converting -129 or 256 to
2673 unsigned char. */
2674 if (!int_fits_type_p (expr, c_common_signed_type (type)))
61f69bc9 2675 warning_at (loc, OPT_Woverflow,
2676 "large integer implicitly truncated to unsigned type");
7ee0d227 2677 else
22a75734 2678 conversion_warning (loc, type, expr);
da1fb07b 2679 }
48e1416a 2680 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
22a75734 2681 warning_at (loc, OPT_Woverflow,
e0913805 2682 "overflow in implicit constant conversion");
2683 /* No warning for converting 0x80000000 to int. */
2684 else if (pedantic
2685 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2686 || TYPE_PRECISION (TREE_TYPE (expr))
2687 != TYPE_PRECISION (type)))
61f69bc9 2688 warning_at (loc, OPT_Woverflow,
2689 "overflow in implicit constant conversion");
e0913805 2690
7ee0d227 2691 else
22a75734 2692 conversion_warning (loc, type, expr);
2a1736ed 2693 }
9421ebb9 2694 else if ((TREE_CODE (result) == INTEGER_CST
2695 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
61f69bc9 2696 warning_at (loc, OPT_Woverflow,
2697 "overflow in implicit constant conversion");
7ee0d227 2698 else
22a75734 2699 conversion_warning (loc, type, expr);
59dd8856 2700}
2701
2702
2703/* Convert EXPR to TYPE, warning about conversion problems with constants.
2704 Invoke this function on every expression that is converted implicitly,
2705 i.e. because of language rules and not because of an explicit cast. */
2706
2707tree
22a75734 2708convert_and_check (location_t loc, tree type, tree expr)
59dd8856 2709{
2710 tree result;
c6418a4e 2711 tree expr_for_warning;
2712
2713 /* Convert from a value with possible excess precision rather than
2714 via the semantic type, but do not warn about values not fitting
2715 exactly in the semantic type. */
2716 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2717 {
2718 tree orig_type = TREE_TYPE (expr);
2719 expr = TREE_OPERAND (expr, 0);
2720 expr_for_warning = convert (orig_type, expr);
2721 if (orig_type == type)
2722 return expr_for_warning;
2723 }
2724 else
2725 expr_for_warning = expr;
59dd8856 2726
2727 if (TREE_TYPE (expr) == type)
2728 return expr;
48e1416a 2729
59dd8856 2730 result = convert (type, expr);
2731
48d94ede 2732 if (c_inhibit_evaluation_warnings == 0
2733 && !TREE_OVERFLOW_P (expr)
2734 && result != error_mark_node)
22a75734 2735 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
59dd8856 2736
da1fb07b 2737 return result;
b2806639 2738}
2739\f
4e91a871 2740/* A node in a list that describes references to variables (EXPR), which are
2741 either read accesses if WRITER is zero, or write accesses, in which case
2742 WRITER is the parent of EXPR. */
2743struct tlist
2744{
2745 struct tlist *next;
2746 tree expr, writer;
2747};
2748
2749/* Used to implement a cache the results of a call to verify_tree. We only
2750 use this for SAVE_EXPRs. */
2751struct tlist_cache
2752{
2753 struct tlist_cache *next;
2754 struct tlist *cache_before_sp;
2755 struct tlist *cache_after_sp;
2756 tree expr;
481c6ce6 2757};
2758
4e91a871 2759/* Obstack to use when allocating tlist structures, and corresponding
2760 firstobj. */
2761static struct obstack tlist_obstack;
2762static char *tlist_firstobj = 0;
2763
2764/* Keep track of the identifiers we've warned about, so we can avoid duplicate
2765 warnings. */
2766static struct tlist *warned_ids;
2767/* SAVE_EXPRs need special treatment. We process them only once and then
2768 cache the results. */
2769static struct tlist_cache *save_expr_cache;
2770
1cae46be 2771static void add_tlist (struct tlist **, struct tlist *, tree, int);
2772static void merge_tlist (struct tlist **, struct tlist *, int);
2773static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2774static int warning_candidate_p (tree);
79973b57 2775static bool candidate_equal_p (const_tree, const_tree);
1cae46be 2776static void warn_for_collisions (struct tlist *);
2777static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2778static struct tlist *new_tlist (struct tlist *, tree, tree);
481c6ce6 2779
4e91a871 2780/* Create a new struct tlist and fill in its fields. */
2781static struct tlist *
1cae46be 2782new_tlist (struct tlist *next, tree t, tree writer)
4e91a871 2783{
2784 struct tlist *l;
9318f22c 2785 l = XOBNEW (&tlist_obstack, struct tlist);
4e91a871 2786 l->next = next;
2787 l->expr = t;
2788 l->writer = writer;
2789 return l;
2790}
2791
2792/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2793 is nonnull, we ignore any node we find which has a writer equal to it. */
2794
2795static void
1cae46be 2796add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
4e91a871 2797{
2798 while (add)
2799 {
2800 struct tlist *next = add->next;
84166705 2801 if (!copy)
4e91a871 2802 add->next = *to;
79973b57 2803 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
4e91a871 2804 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2805 add = next;
2806 }
2807}
2808
2809/* Merge the nodes of ADD into TO. This merging process is done so that for
2810 each variable that already exists in TO, no new node is added; however if
2811 there is a write access recorded in ADD, and an occurrence on TO is only
2812 a read access, then the occurrence in TO will be modified to record the
2813 write. */
481c6ce6 2814
2815static void
1cae46be 2816merge_tlist (struct tlist **to, struct tlist *add, int copy)
4e91a871 2817{
2818 struct tlist **end = to;
2819
2820 while (*end)
2821 end = &(*end)->next;
2822
2823 while (add)
2824 {
2825 int found = 0;
2826 struct tlist *tmp2;
2827 struct tlist *next = add->next;
2828
2829 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
79973b57 2830 if (candidate_equal_p (tmp2->expr, add->expr))
4e91a871 2831 {
2832 found = 1;
84166705 2833 if (!tmp2->writer)
4e91a871 2834 tmp2->writer = add->writer;
2835 }
84166705 2836 if (!found)
4e91a871 2837 {
312243bb 2838 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
4e91a871 2839 end = &(*end)->next;
2840 *end = 0;
2841 }
2842 add = next;
2843 }
2844}
2845
2846/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2847 references in list LIST conflict with it, excluding reads if ONLY writers
2848 is nonzero. */
2849
2850static void
1cae46be 2851warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2852 int only_writes)
4e91a871 2853{
2854 struct tlist *tmp;
2855
2856 /* Avoid duplicate warnings. */
2857 for (tmp = warned_ids; tmp; tmp = tmp->next)
79973b57 2858 if (candidate_equal_p (tmp->expr, written))
4e91a871 2859 return;
2860
2861 while (list)
2862 {
79973b57 2863 if (candidate_equal_p (list->expr, written)
2864 && !candidate_equal_p (list->writer, writer)
2865 && (!only_writes || list->writer))
4e91a871 2866 {
2867 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3df42822 2868 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
6513b50d 2869 OPT_Wsequence_point, "operation on %qE may be undefined",
2870 list->expr);
4e91a871 2871 }
2872 list = list->next;
2873 }
2874}
2875
2876/* Given a list LIST of references to variables, find whether any of these
2877 can cause conflicts due to missing sequence points. */
2878
2879static void
1cae46be 2880warn_for_collisions (struct tlist *list)
4e91a871 2881{
2882 struct tlist *tmp;
1cae46be 2883
4e91a871 2884 for (tmp = list; tmp; tmp = tmp->next)
2885 {
2886 if (tmp->writer)
2887 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2888 }
2889}
2890
734c98be 2891/* Return nonzero if X is a tree that can be verified by the sequence point
4e91a871 2892 warnings. */
2893static int
1cae46be 2894warning_candidate_p (tree x)
481c6ce6 2895{
6ef8d12f 2896 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2897 return 0;
2898
027fc6ef 2899 if (TREE_CODE (x) == BLOCK)
2900 return 0;
2901
6ef8d12f 2902 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
79973b57 2903 (lvalue_p) crash on TRY/CATCH. */
6ef8d12f 2904 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2905 return 0;
2906
2907 if (!lvalue_p (x))
2908 return 0;
2909
2910 /* No point to track non-const calls, they will never satisfy
2911 operand_equal_p. */
2912 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2913 return 0;
2914
2915 if (TREE_CODE (x) == STRING_CST)
2916 return 0;
2917
2918 return 1;
79973b57 2919}
2920
2921/* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2922static bool
2923candidate_equal_p (const_tree x, const_tree y)
2924{
2925 return (x == y) || (x && y && operand_equal_p (x, y, 0));
4e91a871 2926}
481c6ce6 2927
4e91a871 2928/* Walk the tree X, and record accesses to variables. If X is written by the
2929 parent tree, WRITER is the parent.
2930 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2931 expression or its only operand forces a sequence point, then everything up
2932 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2933 in PNO_SP.
2934 Once we return, we will have emitted warnings if any subexpression before
2935 such a sequence point could be undefined. On a higher level, however, the
2936 sequence point may not be relevant, and we'll merge the two lists.
2937
2938 Example: (b++, a) + b;
2939 The call that processes the COMPOUND_EXPR will store the increment of B
2940 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2941 processes the PLUS_EXPR will need to merge the two lists so that
2942 eventually, all accesses end up on the same list (and we'll warn about the
2943 unordered subexpressions b++ and b.
2944
2945 A note on merging. If we modify the former example so that our expression
2946 becomes
2947 (b++, b) + a
2948 care must be taken not simply to add all three expressions into the final
2949 PNO_SP list. The function merge_tlist takes care of that by merging the
2950 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2951 way, so that no more than one access to B is recorded. */
481c6ce6 2952
4e91a871 2953static void
1cae46be 2954verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2955 tree writer)
4e91a871 2956{
2957 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2958 enum tree_code code;
ce45a448 2959 enum tree_code_class cl;
481c6ce6 2960
e5b75768 2961 /* X may be NULL if it is the operand of an empty statement expression
2962 ({ }). */
2963 if (x == NULL)
2964 return;
2965
4e91a871 2966 restart:
2967 code = TREE_CODE (x);
e916c70c 2968 cl = TREE_CODE_CLASS (code);
481c6ce6 2969
4e91a871 2970 if (warning_candidate_p (x))
79973b57 2971 *pno_sp = new_tlist (*pno_sp, x, writer);
4e91a871 2972
2973 switch (code)
2974 {
67b28e3e 2975 case CONSTRUCTOR:
8e71dad2 2976 case SIZEOF_EXPR:
67b28e3e 2977 return;
2978
4e91a871 2979 case COMPOUND_EXPR:
2980 case TRUTH_ANDIF_EXPR:
2981 case TRUTH_ORIF_EXPR:
2982 tmp_before = tmp_nosp = tmp_list3 = 0;
2983 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2984 warn_for_collisions (tmp_nosp);
2985 merge_tlist (pbefore_sp, tmp_before, 0);
2986 merge_tlist (pbefore_sp, tmp_nosp, 0);
2987 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
2988 merge_tlist (pbefore_sp, tmp_list3, 0);
2989 return;
2990
2991 case COND_EXPR:
2992 tmp_before = tmp_list2 = 0;
2993 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2994 warn_for_collisions (tmp_list2);
2995 merge_tlist (pbefore_sp, tmp_before, 0);
312243bb 2996 merge_tlist (pbefore_sp, tmp_list2, 0);
4e91a871 2997
2998 tmp_list3 = tmp_nosp = 0;
2999 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3000 warn_for_collisions (tmp_nosp);
3001 merge_tlist (pbefore_sp, tmp_list3, 0);
3002
3003 tmp_list3 = tmp_list2 = 0;
3004 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3005 warn_for_collisions (tmp_list2);
3006 merge_tlist (pbefore_sp, tmp_list3, 0);
3007 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3008 two first, to avoid warning for (a ? b++ : b++). */
3009 merge_tlist (&tmp_nosp, tmp_list2, 0);
3010 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3011 return;
3012
481c6ce6 3013 case PREDECREMENT_EXPR:
3014 case PREINCREMENT_EXPR:
3015 case POSTDECREMENT_EXPR:
3016 case POSTINCREMENT_EXPR:
4e91a871 3017 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3018 return;
3019
3020 case MODIFY_EXPR:
3021 tmp_before = tmp_nosp = tmp_list3 = 0;
3022 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3023 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3024 /* Expressions inside the LHS are not ordered wrt. the sequence points
3025 in the RHS. Example:
3026 *a = (a++, 2)
3027 Despite the fact that the modification of "a" is in the before_sp
3028 list (tmp_before), it conflicts with the use of "a" in the LHS.
3029 We can handle this by adding the contents of tmp_list3
3030 to those of tmp_before, and redoing the collision warnings for that
3031 list. */
3032 add_tlist (&tmp_before, tmp_list3, x, 1);
3033 warn_for_collisions (tmp_before);
3034 /* Exclude the LHS itself here; we first have to merge it into the
3035 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3036 didn't exclude the LHS, we'd get it twice, once as a read and once
3037 as a write. */
3038 add_tlist (pno_sp, tmp_list3, x, 0);
3039 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3040
3041 merge_tlist (pbefore_sp, tmp_before, 0);
3042 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3043 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3044 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3045 return;
481c6ce6 3046
3047 case CALL_EXPR:
4e91a871 3048 /* We need to warn about conflicts among arguments and conflicts between
3049 args and the function address. Side effects of the function address,
3050 however, are not ordered by the sequence point of the call. */
c2f47e15 3051 {
3052 call_expr_arg_iterator iter;
3053 tree arg;
48e1416a 3054 tmp_before = tmp_nosp = 0;
c2f47e15 3055 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3056 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3057 {
3058 tmp_list2 = tmp_list3 = 0;
3059 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3060 merge_tlist (&tmp_list3, tmp_list2, 0);
3061 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3062 }
3063 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3064 warn_for_collisions (tmp_before);
3065 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3066 return;
3067 }
481c6ce6 3068
3069 case TREE_LIST:
3070 /* Scan all the list, e.g. indices of multi dimensional array. */
3071 while (x)
3072 {
4e91a871 3073 tmp_before = tmp_nosp = 0;
3074 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3075 merge_tlist (&tmp_nosp, tmp_before, 0);
3076 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
481c6ce6 3077 x = TREE_CHAIN (x);
3078 }
4e91a871 3079 return;
481c6ce6 3080
4e91a871 3081 case SAVE_EXPR:
3082 {
3083 struct tlist_cache *t;
3084 for (t = save_expr_cache; t; t = t->next)
79973b57 3085 if (candidate_equal_p (t->expr, x))
4e91a871 3086 break;
481c6ce6 3087
84166705 3088 if (!t)
481c6ce6 3089 {
9318f22c 3090 t = XOBNEW (&tlist_obstack, struct tlist_cache);
4e91a871 3091 t->next = save_expr_cache;
3092 t->expr = x;
3093 save_expr_cache = t;
3094
3095 tmp_before = tmp_nosp = 0;
3096 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3097 warn_for_collisions (tmp_nosp);
3098
3099 tmp_list3 = 0;
312243bb 3100 merge_tlist (&tmp_list3, tmp_nosp, 0);
4e91a871 3101 t->cache_before_sp = tmp_before;
3102 t->cache_after_sp = tmp_list3;
481c6ce6 3103 }
4e91a871 3104 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3105 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3106 return;
3107 }
481c6ce6 3108
012916cb 3109 case ADDR_EXPR:
3110 x = TREE_OPERAND (x, 0);
3111 if (DECL_P (x))
3112 return;
3113 writer = 0;
3114 goto restart;
3115
ce45a448 3116 default:
3117 /* For other expressions, simply recurse on their operands.
a0c938f0 3118 Manual tail recursion for unary expressions.
ce45a448 3119 Other non-expressions need not be processed. */
3120 if (cl == tcc_unary)
3121 {
ce45a448 3122 x = TREE_OPERAND (x, 0);
3123 writer = 0;
3124 goto restart;
3125 }
3126 else if (IS_EXPR_CODE_CLASS (cl))
3127 {
3128 int lp;
c2f47e15 3129 int max = TREE_OPERAND_LENGTH (x);
ce45a448 3130 for (lp = 0; lp < max; lp++)
3131 {
3132 tmp_before = tmp_nosp = 0;
3133 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3134 merge_tlist (&tmp_nosp, tmp_before, 0);
3135 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3136 }
3137 }
3138 return;
481c6ce6 3139 }
481c6ce6 3140}
3141
974e2c0c 3142/* Try to warn for undefined behavior in EXPR due to missing sequence
481c6ce6 3143 points. */
3144
4b987fac 3145DEBUG_FUNCTION void
1cae46be 3146verify_sequence_points (tree expr)
481c6ce6 3147{
4e91a871 3148 struct tlist *before_sp = 0, *after_sp = 0;
481c6ce6 3149
4e91a871 3150 warned_ids = 0;
3151 save_expr_cache = 0;
3152 if (tlist_firstobj == 0)
481c6ce6 3153 {
4e91a871 3154 gcc_obstack_init (&tlist_obstack);
4fd61bc6 3155 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
481c6ce6 3156 }
3157
4e91a871 3158 verify_tree (expr, &before_sp, &after_sp, 0);
3159 warn_for_collisions (after_sp);
3160 obstack_free (&tlist_obstack, tlist_firstobj);
481c6ce6 3161}
b0fc3e72 3162\f
3163/* Validate the expression after `case' and apply default promotions. */
3164
2ca392fd 3165static tree
2d2f6a15 3166check_case_value (location_t loc, tree value)
b0fc3e72 3167{
3168 if (value == NULL_TREE)
3169 return value;
3170
b96dc121 3171 if (TREE_CODE (value) == INTEGER_CST)
3172 /* Promote char or short to int. */
3173 value = perform_integral_promotions (value);
3174 else if (value != error_mark_node)
b0fc3e72 3175 {
2d2f6a15 3176 error_at (loc, "case label does not reduce to an integer constant");
b0fc3e72 3177 value = error_mark_node;
3178 }
b0fc3e72 3179
6433f1c2 3180 constant_expression_warning (value);
3181
b0fc3e72 3182 return value;
3183}
3184\f
2ca392fd 3185/* See if the case values LOW and HIGH are in the range of the original
5c9dae64 3186 type (i.e. before the default conversion to int) of the switch testing
2ca392fd 3187 expression.
3188 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
91275768 3189 the type before promoting it. CASE_LOW_P is a pointer to the lower
2ca392fd 3190 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3191 if the case is not a case range.
3192 The caller has to make sure that we are not called with NULL for
be23b16f 3193 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
3194 was a case value that doesn't fit into the range of the ORIG_TYPE.
442e3cb9 3195 Returns true if the case label is in range of ORIG_TYPE (saturated or
2ca392fd 3196 untouched) or false if the label is out of range. */
3197
3198static bool
f61a9bc2 3199check_case_bounds (location_t loc, tree type, tree orig_type,
be23b16f 3200 tree *case_low_p, tree *case_high_p,
3201 bool *outside_range_p)
2ca392fd 3202{
3203 tree min_value, max_value;
3204 tree case_low = *case_low_p;
3205 tree case_high = case_high_p ? *case_high_p : case_low;
3206
3207 /* If there was a problem with the original type, do nothing. */
3208 if (orig_type == error_mark_node)
3209 return true;
3210
3211 min_value = TYPE_MIN_VALUE (orig_type);
3212 max_value = TYPE_MAX_VALUE (orig_type);
3213
73f67931 3214 /* We'll really need integer constants here. */
3215 case_low = fold (case_low);
3216 case_high = fold (case_high);
3217
2ca392fd 3218 /* Case label is less than minimum for type. */
3219 if (tree_int_cst_compare (case_low, min_value) < 0
3220 && tree_int_cst_compare (case_high, min_value) < 0)
3221 {
f61a9bc2 3222 warning_at (loc, 0, "case label value is less than minimum value "
3223 "for type");
be23b16f 3224 *outside_range_p = true;
2ca392fd 3225 return false;
3226 }
b27ac6b5 3227
2ca392fd 3228 /* Case value is greater than maximum for type. */
3229 if (tree_int_cst_compare (case_low, max_value) > 0
3230 && tree_int_cst_compare (case_high, max_value) > 0)
3231 {
f61a9bc2 3232 warning_at (loc, 0, "case label value exceeds maximum value for type");
be23b16f 3233 *outside_range_p = true;
2ca392fd 3234 return false;
3235 }
3236
3237 /* Saturate lower case label value to minimum. */
3238 if (tree_int_cst_compare (case_high, min_value) >= 0
3239 && tree_int_cst_compare (case_low, min_value) < 0)
3240 {
f61a9bc2 3241 warning_at (loc, 0, "lower value in case label range"
3242 " less than minimum value for type");
be23b16f 3243 *outside_range_p = true;
2ca392fd 3244 case_low = min_value;
3245 }
b27ac6b5 3246
2ca392fd 3247 /* Saturate upper case label value to maximum. */
3248 if (tree_int_cst_compare (case_low, max_value) <= 0
3249 && tree_int_cst_compare (case_high, max_value) > 0)
3250 {
f61a9bc2 3251 warning_at (loc, 0, "upper value in case label range"
3252 " exceeds maximum value for type");
be23b16f 3253 *outside_range_p = true;
2ca392fd 3254 case_high = max_value;
3255 }
3256
3257 if (*case_low_p != case_low)
3258 *case_low_p = convert (type, case_low);
3259 if (case_high_p && *case_high_p != case_high)
3260 *case_high_p = convert (type, case_high);
3261
3262 return true;
3263}
3264\f
b0fc3e72 3265/* Return an integer type with BITS bits of precision,
3266 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3267
3268tree
1cae46be 3269c_common_type_for_size (unsigned int bits, int unsignedp)
b0fc3e72 3270{
9f75f026 3271 int i;
3272
46375237 3273 if (bits == TYPE_PRECISION (integer_type_node))
3274 return unsignedp ? unsigned_type_node : integer_type_node;
3275
bacde65a 3276 if (bits == TYPE_PRECISION (signed_char_type_node))
b0fc3e72 3277 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3278
bacde65a 3279 if (bits == TYPE_PRECISION (short_integer_type_node))
b0fc3e72 3280 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3281
bacde65a 3282 if (bits == TYPE_PRECISION (long_integer_type_node))
b0fc3e72 3283 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3284
bacde65a 3285 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b0fc3e72 3286 return (unsignedp ? long_long_unsigned_type_node
3287 : long_long_integer_type_node);
3288
9f75f026 3289 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3290 if (int_n_enabled_p[i]
3291 && bits == int_n_data[i].bitsize)
3292 return (unsignedp ? int_n_trees[i].unsigned_type
3293 : int_n_trees[i].signed_type);
6388cfe2 3294
f57fa2ea 3295 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3296 return (unsignedp ? widest_unsigned_literal_type_node
3297 : widest_integer_literal_type_node);
3298
bacde65a 3299 if (bits <= TYPE_PRECISION (intQI_type_node))
3300 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3301
3302 if (bits <= TYPE_PRECISION (intHI_type_node))
3303 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3304
3305 if (bits <= TYPE_PRECISION (intSI_type_node))
3306 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3307
3308 if (bits <= TYPE_PRECISION (intDI_type_node))
3309 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3310
b0fc3e72 3311 return 0;
3312}
3313
9421ebb9 3314/* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3315 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3316 and saturating if SATP is nonzero, otherwise not saturating. */
3317
3318tree
3319c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3320 int unsignedp, int satp)
3321{
3754d046 3322 machine_mode mode;
9421ebb9 3323 if (ibit == 0)
3324 mode = unsignedp ? UQQmode : QQmode;
3325 else
3326 mode = unsignedp ? UHAmode : HAmode;
3327
3328 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3329 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3330 break;
3331
3332 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3333 {
3334 sorry ("GCC cannot support operators with integer types and "
3335 "fixed-point types that have too many integral and "
3336 "fractional bits together");
3337 return 0;
3338 }
3339
3340 return c_common_type_for_mode (mode, satp);
3341}
3342
5b247e9f 3343/* Used for communication between c_common_type_for_mode and
3344 c_register_builtin_type. */
c1917557 3345tree registered_builtin_types;
5b247e9f 3346
b0fc3e72 3347/* Return a data type that has machine mode MODE.
3348 If the mode is an integer,
9421ebb9 3349 then UNSIGNEDP selects between signed and unsigned types.
3350 If the mode is a fixed-point mode,
3351 then UNSIGNEDP selects between saturating and nonsaturating types. */
b0fc3e72 3352
3353tree
3754d046 3354c_common_type_for_mode (machine_mode mode, int unsignedp)
b0fc3e72 3355{
5b247e9f 3356 tree t;
9f75f026 3357 int i;
5b247e9f 3358
46375237 3359 if (mode == TYPE_MODE (integer_type_node))
3360 return unsignedp ? unsigned_type_node : integer_type_node;
3361
b0fc3e72 3362 if (mode == TYPE_MODE (signed_char_type_node))
3363 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3364
3365 if (mode == TYPE_MODE (short_integer_type_node))
3366 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3367
b0fc3e72 3368 if (mode == TYPE_MODE (long_integer_type_node))
3369 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3370
3371 if (mode == TYPE_MODE (long_long_integer_type_node))
3372 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3373
9f75f026 3374 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3375 if (int_n_enabled_p[i]
3376 && mode == int_n_data[i].m)
3377 return (unsignedp ? int_n_trees[i].unsigned_type
3378 : int_n_trees[i].signed_type);
6388cfe2 3379
f57fa2ea 3380 if (mode == TYPE_MODE (widest_integer_literal_type_node))
44e9fa65 3381 return unsignedp ? widest_unsigned_literal_type_node
4ee9c684 3382 : widest_integer_literal_type_node;
f57fa2ea 3383
88ae7f04 3384 if (mode == QImode)
bacde65a 3385 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3386
88ae7f04 3387 if (mode == HImode)
bacde65a 3388 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3389
88ae7f04 3390 if (mode == SImode)
bacde65a 3391 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3392
88ae7f04 3393 if (mode == DImode)
bacde65a 3394 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
ab2c1de8 3395
cc1cc1c7 3396#if HOST_BITS_PER_WIDE_INT >= 64
6274009c 3397 if (mode == TYPE_MODE (intTI_type_node))
3398 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
cc1cc1c7 3399#endif
6274009c 3400
b0fc3e72 3401 if (mode == TYPE_MODE (float_type_node))
3402 return float_type_node;
3403
3404 if (mode == TYPE_MODE (double_type_node))
3405 return double_type_node;
3406
3407 if (mode == TYPE_MODE (long_double_type_node))
3408 return long_double_type_node;
3409
545c2bde 3410 if (mode == TYPE_MODE (void_type_node))
3411 return void_type_node;
b27ac6b5 3412
b0fc3e72 3413 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
61b9b73c 3414 return (unsignedp
3415 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3416 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3417
3418 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
61b9b73c 3419 return (unsignedp
3420 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3421 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3422
0dfc45b5 3423 if (COMPLEX_MODE_P (mode))
3424 {
3754d046 3425 machine_mode inner_mode;
0dfc45b5 3426 tree inner_type;
3427
3428 if (mode == TYPE_MODE (complex_float_type_node))
3429 return complex_float_type_node;
3430 if (mode == TYPE_MODE (complex_double_type_node))
3431 return complex_double_type_node;
3432 if (mode == TYPE_MODE (complex_long_double_type_node))
3433 return complex_long_double_type_node;
3434
3435 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3436 return complex_integer_type_node;
3437
3438 inner_mode = GET_MODE_INNER (mode);
3439 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3440 if (inner_type != NULL_TREE)
3441 return build_complex_type (inner_type);
3442 }
3443 else if (VECTOR_MODE_P (mode))
4917c376 3444 {
3754d046 3445 machine_mode inner_mode = GET_MODE_INNER (mode);
4917c376 3446 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3447 if (inner_type != NULL_TREE)
3448 return build_vector_type_for_mode (inner_type, mode);
88ae7f04 3449 }
e2ea7e3a 3450
c4503c0a 3451 if (mode == TYPE_MODE (dfloat32_type_node))
3452 return dfloat32_type_node;
3453 if (mode == TYPE_MODE (dfloat64_type_node))
3454 return dfloat64_type_node;
3455 if (mode == TYPE_MODE (dfloat128_type_node))
3456 return dfloat128_type_node;
3457
9421ebb9 3458 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3459 {
3460 if (mode == TYPE_MODE (short_fract_type_node))
3461 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3462 if (mode == TYPE_MODE (fract_type_node))
3463 return unsignedp ? sat_fract_type_node : fract_type_node;
3464 if (mode == TYPE_MODE (long_fract_type_node))
3465 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3466 if (mode == TYPE_MODE (long_long_fract_type_node))
3467 return unsignedp ? sat_long_long_fract_type_node
3468 : long_long_fract_type_node;
3469
3470 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3471 return unsignedp ? sat_unsigned_short_fract_type_node
3472 : unsigned_short_fract_type_node;
3473 if (mode == TYPE_MODE (unsigned_fract_type_node))
3474 return unsignedp ? sat_unsigned_fract_type_node
3475 : unsigned_fract_type_node;
3476 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3477 return unsignedp ? sat_unsigned_long_fract_type_node
3478 : unsigned_long_fract_type_node;
3479 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3480 return unsignedp ? sat_unsigned_long_long_fract_type_node
3481 : unsigned_long_long_fract_type_node;
3482
3483 if (mode == TYPE_MODE (short_accum_type_node))
3484 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3485 if (mode == TYPE_MODE (accum_type_node))
3486 return unsignedp ? sat_accum_type_node : accum_type_node;
3487 if (mode == TYPE_MODE (long_accum_type_node))
3488 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3489 if (mode == TYPE_MODE (long_long_accum_type_node))
3490 return unsignedp ? sat_long_long_accum_type_node
3491 : long_long_accum_type_node;
3492
3493 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3494 return unsignedp ? sat_unsigned_short_accum_type_node
3495 : unsigned_short_accum_type_node;
3496 if (mode == TYPE_MODE (unsigned_accum_type_node))
3497 return unsignedp ? sat_unsigned_accum_type_node
3498 : unsigned_accum_type_node;
3499 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3500 return unsignedp ? sat_unsigned_long_accum_type_node
3501 : unsigned_long_accum_type_node;
3502 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3503 return unsignedp ? sat_unsigned_long_long_accum_type_node
3504 : unsigned_long_long_accum_type_node;
3505
3506 if (mode == QQmode)
3507 return unsignedp ? sat_qq_type_node : qq_type_node;
3508 if (mode == HQmode)
3509 return unsignedp ? sat_hq_type_node : hq_type_node;
3510 if (mode == SQmode)
3511 return unsignedp ? sat_sq_type_node : sq_type_node;
3512 if (mode == DQmode)
3513 return unsignedp ? sat_dq_type_node : dq_type_node;
3514 if (mode == TQmode)
3515 return unsignedp ? sat_tq_type_node : tq_type_node;
3516
3517 if (mode == UQQmode)
3518 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3519 if (mode == UHQmode)
3520 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3521 if (mode == USQmode)
3522 return unsignedp ? sat_usq_type_node : usq_type_node;
3523 if (mode == UDQmode)
3524 return unsignedp ? sat_udq_type_node : udq_type_node;
3525 if (mode == UTQmode)
3526 return unsignedp ? sat_utq_type_node : utq_type_node;
3527
3528 if (mode == HAmode)
3529 return unsignedp ? sat_ha_type_node : ha_type_node;
3530 if (mode == SAmode)
3531 return unsignedp ? sat_sa_type_node : sa_type_node;
3532 if (mode == DAmode)
3533 return unsignedp ? sat_da_type_node : da_type_node;
3534 if (mode == TAmode)
3535 return unsignedp ? sat_ta_type_node : ta_type_node;
3536
3537 if (mode == UHAmode)
3538 return unsignedp ? sat_uha_type_node : uha_type_node;
3539 if (mode == USAmode)
3540 return unsignedp ? sat_usa_type_node : usa_type_node;
3541 if (mode == UDAmode)
3542 return unsignedp ? sat_uda_type_node : uda_type_node;
3543 if (mode == UTAmode)
3544 return unsignedp ? sat_uta_type_node : uta_type_node;
3545 }
3546
5b247e9f 3547 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
baec58e1 3548 if (TYPE_MODE (TREE_VALUE (t)) == mode
3549 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
5b247e9f 3550 return TREE_VALUE (t);
3551
b0fc3e72 3552 return 0;
3553}
20d39783 3554
11773141 3555tree
3556c_common_unsigned_type (tree type)
3557{
3558 return c_common_signed_or_unsigned_type (1, type);
3559}
3560
20d39783 3561/* Return a signed type the same as TYPE in other respects. */
3562
3563tree
1cae46be 3564c_common_signed_type (tree type)
20d39783 3565{
4070745f 3566 return c_common_signed_or_unsigned_type (0, type);
20d39783 3567}
3568
3569/* Return a type the same as TYPE except unsigned or
3570 signed according to UNSIGNEDP. */
3571
3572tree
1cae46be 3573c_common_signed_or_unsigned_type (int unsignedp, tree type)
20d39783 3574{
7a91101f 3575 tree type1;
9f75f026 3576 int i;
20d39783 3577
7a91101f 3578 /* This block of code emulates the behavior of the old
3579 c_common_unsigned_type. In particular, it returns
3580 long_unsigned_type_node if passed a long, even when a int would
3581 have the same size. This is necessary for warnings to work
3582 correctly in archs where sizeof(int) == sizeof(long) */
3583
3584 type1 = TYPE_MAIN_VARIANT (type);
3585 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3586 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3587 if (type1 == integer_type_node || type1 == unsigned_type_node)
3588 return unsignedp ? unsigned_type_node : integer_type_node;
3589 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3590 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3591 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3592 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3593 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3594 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
9f75f026 3595
3596 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3597 if (int_n_enabled_p[i]
3598 && (type1 == int_n_trees[i].unsigned_type
3599 || type1 == int_n_trees[i].signed_type))
3600 return (unsignedp ? int_n_trees[i].unsigned_type
3601 : int_n_trees[i].signed_type);
3602
7a91101f 3603 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3604 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3605#if HOST_BITS_PER_WIDE_INT >= 64
3606 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3607 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3608#endif
3609 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3610 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3611 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3612 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3613 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3614 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3615 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3616 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3617
9f92e1a2 3618#define C_COMMON_FIXED_TYPES(NAME) \
3619 if (type1 == short_ ## NAME ## _type_node \
3620 || type1 == unsigned_short_ ## NAME ## _type_node) \
3621 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3622 : short_ ## NAME ## _type_node; \
3623 if (type1 == NAME ## _type_node \
3624 || type1 == unsigned_ ## NAME ## _type_node) \
3625 return unsignedp ? unsigned_ ## NAME ## _type_node \
3626 : NAME ## _type_node; \
3627 if (type1 == long_ ## NAME ## _type_node \
3628 || type1 == unsigned_long_ ## NAME ## _type_node) \
3629 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3630 : long_ ## NAME ## _type_node; \
3631 if (type1 == long_long_ ## NAME ## _type_node \
3632 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3633 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3634 : long_long_ ## NAME ## _type_node;
3635
3636#define C_COMMON_FIXED_MODE_TYPES(NAME) \
3637 if (type1 == NAME ## _type_node \
3638 || type1 == u ## NAME ## _type_node) \
3639 return unsignedp ? u ## NAME ## _type_node \
3640 : NAME ## _type_node;
3641
3642#define C_COMMON_FIXED_TYPES_SAT(NAME) \
3643 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3644 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3645 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3646 : sat_ ## short_ ## NAME ## _type_node; \
3647 if (type1 == sat_ ## NAME ## _type_node \
3648 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3649 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3650 : sat_ ## NAME ## _type_node; \
3651 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3652 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3653 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3654 : sat_ ## long_ ## NAME ## _type_node; \
3655 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3656 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3657 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3658 : sat_ ## long_long_ ## NAME ## _type_node;
3659
3660#define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3661 if (type1 == sat_ ## NAME ## _type_node \
3662 || type1 == sat_ ## u ## NAME ## _type_node) \
3663 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3664 : sat_ ## NAME ## _type_node;
3665
3666 C_COMMON_FIXED_TYPES (fract);
3667 C_COMMON_FIXED_TYPES_SAT (fract);
3668 C_COMMON_FIXED_TYPES (accum);
3669 C_COMMON_FIXED_TYPES_SAT (accum);
3670
3671 C_COMMON_FIXED_MODE_TYPES (qq);
3672 C_COMMON_FIXED_MODE_TYPES (hq);
3673 C_COMMON_FIXED_MODE_TYPES (sq);
3674 C_COMMON_FIXED_MODE_TYPES (dq);
3675 C_COMMON_FIXED_MODE_TYPES (tq);
3676 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3677 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3678 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3679 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3680 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3681 C_COMMON_FIXED_MODE_TYPES (ha);
3682 C_COMMON_FIXED_MODE_TYPES (sa);
3683 C_COMMON_FIXED_MODE_TYPES (da);
3684 C_COMMON_FIXED_MODE_TYPES (ta);
3685 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3686 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3687 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3688 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
9421ebb9 3689
4f7f7efd 3690 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3691 the precision; they have precision set to match their range, but
3692 may use a wider mode to match an ABI. If we change modes, we may
3693 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3694 the precision as well, so as to yield correct results for
3695 bit-field types. C++ does not have these separate bit-field
3696 types, and producing a signed or unsigned variant of an
3697 ENUMERAL_TYPE may cause other problems as well. */
3698
ac265864 3699 if (!INTEGRAL_TYPE_P (type)
3700 || TYPE_UNSIGNED (type) == unsignedp)
3701 return type;
3702
4f7f7efd 3703#define TYPE_OK(node) \
3704 (TYPE_MODE (type) == TYPE_MODE (node) \
0c4abe5b 3705 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
4f7f7efd 3706 if (TYPE_OK (signed_char_type_node))
20d39783 3707 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4f7f7efd 3708 if (TYPE_OK (integer_type_node))
20d39783 3709 return unsignedp ? unsigned_type_node : integer_type_node;
4f7f7efd 3710 if (TYPE_OK (short_integer_type_node))
20d39783 3711 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4f7f7efd 3712 if (TYPE_OK (long_integer_type_node))
20d39783 3713 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4f7f7efd 3714 if (TYPE_OK (long_long_integer_type_node))
20d39783 3715 return (unsignedp ? long_long_unsigned_type_node
3716 : long_long_integer_type_node);
9f75f026 3717
3718 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3719 if (int_n_enabled_p[i]
3720 && TYPE_MODE (type) == int_n_data[i].m
3721 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3722 return (unsignedp ? int_n_trees[i].unsigned_type
3723 : int_n_trees[i].signed_type);
3724
4f7f7efd 3725 if (TYPE_OK (widest_integer_literal_type_node))
20d39783 3726 return (unsignedp ? widest_unsigned_literal_type_node
3727 : widest_integer_literal_type_node);
ef11801e 3728
3729#if HOST_BITS_PER_WIDE_INT >= 64
4f7f7efd 3730 if (TYPE_OK (intTI_type_node))
ef11801e 3731 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3732#endif
4f7f7efd 3733 if (TYPE_OK (intDI_type_node))
ef11801e 3734 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4f7f7efd 3735 if (TYPE_OK (intSI_type_node))
ef11801e 3736 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4f7f7efd 3737 if (TYPE_OK (intHI_type_node))
ef11801e 3738 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4f7f7efd 3739 if (TYPE_OK (intQI_type_node))
ef11801e 3740 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4f7f7efd 3741#undef TYPE_OK
ef11801e 3742
0c4abe5b 3743 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
20d39783 3744}
b268e47e 3745
c0e47fd4 3746/* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3747
3748tree
3749c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3750{
9f75f026 3751 int i;
3752
c0e47fd4 3753 /* Extended integer types of the same width as a standard type have
3754 lesser rank, so those of the same width as int promote to int or
3755 unsigned int and are valid for printf formats expecting int or
3756 unsigned int. To avoid such special cases, avoid creating
3757 extended integer types for bit-fields if a standard integer type
3758 is available. */
3759 if (width == TYPE_PRECISION (integer_type_node))
3760 return unsignedp ? unsigned_type_node : integer_type_node;
3761 if (width == TYPE_PRECISION (signed_char_type_node))
3762 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3763 if (width == TYPE_PRECISION (short_integer_type_node))
3764 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3765 if (width == TYPE_PRECISION (long_integer_type_node))
3766 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3767 if (width == TYPE_PRECISION (long_long_integer_type_node))
3768 return (unsignedp ? long_long_unsigned_type_node
3769 : long_long_integer_type_node);
9f75f026 3770 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3771 if (int_n_enabled_p[i]
3772 && width == int_n_data[i].bitsize)
3773 return (unsignedp ? int_n_trees[i].unsigned_type
3774 : int_n_trees[i].signed_type);
c0e47fd4 3775 return build_nonstandard_integer_type (width, unsignedp);
3776}
3777
b268e47e 3778/* The C version of the register_builtin_type langhook. */
3779
3780void
3781c_register_builtin_type (tree type, const char* name)
3782{
3783 tree decl;
3784
e60a6f7b 3785 decl = build_decl (UNKNOWN_LOCATION,
3786 TYPE_DECL, get_identifier (name), type);
b268e47e 3787 DECL_ARTIFICIAL (decl) = 1;
3788 if (!TYPE_NAME (type))
3789 TYPE_NAME (type) = decl;
3790 pushdecl (decl);
5b247e9f 3791
3792 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
b268e47e 3793}
a9b9d10c 3794\f
aff9e656 3795/* Print an error message for invalid operands to arith operation
8e70fb09 3796 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
c0bf500c 3797 RICHLOC is a rich location for the message, containing either
3798 three separate locations for each of the operator and operands
3799
3800 lhs op rhs
3801 ~~~ ^~ ~~~
3802
3803 (C FE), or one location ranging over all over them
3804
3805 lhs op rhs
3806 ~~~~^~~~~~
3807
3808 (C++ FE). */
b0fc3e72 3809
3810void
c0bf500c 3811binary_op_error (rich_location *richloc, enum tree_code code,
8e70fb09 3812 tree type0, tree type1)
b0fc3e72 3813{
19cb6b50 3814 const char *opname;
f03946e4 3815
b0fc3e72 3816 switch (code)
3817 {
b0fc3e72 3818 case PLUS_EXPR:
3819 opname = "+"; break;
3820 case MINUS_EXPR:
3821 opname = "-"; break;
3822 case MULT_EXPR:
3823 opname = "*"; break;
3824 case MAX_EXPR:
3825 opname = "max"; break;
3826 case MIN_EXPR:
3827 opname = "min"; break;
3828 case EQ_EXPR:
3829 opname = "=="; break;
3830 case NE_EXPR:
3831 opname = "!="; break;
3832 case LE_EXPR:
3833 opname = "<="; break;
3834 case GE_EXPR:
3835 opname = ">="; break;
3836 case LT_EXPR:
3837 opname = "<"; break;
3838 case GT_EXPR:
3839 opname = ">"; break;
3840 case LSHIFT_EXPR:
3841 opname = "<<"; break;
3842 case RSHIFT_EXPR:
3843 opname = ">>"; break;
3844 case TRUNC_MOD_EXPR:
66618a1e 3845 case FLOOR_MOD_EXPR:
b0fc3e72 3846 opname = "%"; break;
3847 case TRUNC_DIV_EXPR:
66618a1e 3848 case FLOOR_DIV_EXPR:
b0fc3e72 3849 opname = "/"; break;
3850 case BIT_AND_EXPR:
3851 opname = "&"; break;
3852 case BIT_IOR_EXPR:
3853 opname = "|"; break;
3854 case TRUTH_ANDIF_EXPR:
3855 opname = "&&"; break;
3856 case TRUTH_ORIF_EXPR:
3857 opname = "||"; break;
3858 case BIT_XOR_EXPR:
3859 opname = "^"; break;
31f820d2 3860 default:
315ba355 3861 gcc_unreachable ();
b0fc3e72 3862 }
c0bf500c 3863 error_at_rich_loc (richloc,
3864 "invalid operands to binary %s (have %qT and %qT)",
3865 opname, type0, type1);
b0fc3e72 3866}
3867\f
03fe1dc2 3868/* Given an expression as a tree, return its original type. Do this
3869 by stripping any conversion that preserves the sign and precision. */
3870static tree
3871expr_original_type (tree expr)
3872{
3873 STRIP_SIGN_NOPS (expr);
3874 return TREE_TYPE (expr);
3875}
3876
b0fc3e72 3877/* Subroutine of build_binary_op, used for comparison operations.
3878 See if the operands have both been converted from subword integer types
3879 and, if so, perhaps change them both back to their original type.
5b511807 3880 This function is also responsible for converting the two operands
3881 to the proper common type for comparison.
b0fc3e72 3882
3883 The arguments of this function are all pointers to local variables
3884 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3885 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3886
2623625f 3887 LOC is the location of the comparison.
3888
b0fc3e72 3889 If this function returns nonzero, it means that the comparison has
3890 a constant value. What this function returns is an expression for
3891 that value. */
3892
3893tree
2623625f 3894shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
3895 tree *restype_ptr, enum tree_code *rescode_ptr)
b0fc3e72 3896{
19cb6b50 3897 tree type;
b0fc3e72 3898 tree op0 = *op0_ptr;
3899 tree op1 = *op1_ptr;
3900 int unsignedp0, unsignedp1;
3901 int real1, real2;
3902 tree primop0, primop1;
3903 enum tree_code code = *rescode_ptr;
3904
3905 /* Throw away any conversions to wider types
3906 already present in the operands. */
3907
7f506bca 3908 primop0 = c_common_get_narrower (op0, &unsignedp0);
3909 primop1 = c_common_get_narrower (op1, &unsignedp1);
b0fc3e72 3910
119d06b2 3911 /* If primopN is first sign-extended from primopN's precision to opN's
3912 precision, then zero-extended from opN's precision to
3913 *restype_ptr precision, shortenings might be invalid. */
3914 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3915 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3916 && !unsignedp0
3917 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3918 primop0 = op0;
3919 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3920 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3921 && !unsignedp1
3922 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3923 primop1 = op1;
3924
b0fc3e72 3925 /* Handle the case that OP0 does not *contain* a conversion
3926 but it *requires* conversion to FINAL_TYPE. */
3927
3928 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
78a8ed03 3929 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
b0fc3e72 3930 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
78a8ed03 3931 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b0fc3e72 3932
3933 /* If one of the operands must be floated, we cannot optimize. */
3934 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
3935 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
ab2c1de8 3936
b0fc3e72 3937 /* If first arg is constant, swap the args (changing operation
2bd278cc 3938 so value is preserved), for canonicalization. Don't do this if
3939 the second arg is 0. */
b0fc3e72 3940
2bd278cc 3941 if (TREE_CONSTANT (primop0)
9421ebb9 3942 && !integer_zerop (primop1) && !real_zerop (primop1)
3943 && !fixed_zerop (primop1))
b0fc3e72 3944 {
dfcf26a5 3945 std::swap (primop0, primop1);
3946 std::swap (op0, op1);
b0fc3e72 3947 *op0_ptr = op0;
3948 *op1_ptr = op1;
dfcf26a5 3949 std::swap (unsignedp0, unsignedp1);
3950 std::swap (real1, real2);
b0fc3e72 3951
3952 switch (code)
3953 {
3954 case LT_EXPR:
3955 code = GT_EXPR;
3956 break;
3957 case GT_EXPR:
3958 code = LT_EXPR;
3959 break;
3960 case LE_EXPR:
3961 code = GE_EXPR;
3962 break;
3963 case GE_EXPR:
3964 code = LE_EXPR;
3965 break;
31f820d2 3966 default:
3967 break;
b0fc3e72 3968 }
3969 *rescode_ptr = code;
3970 }
3971
3972 /* If comparing an integer against a constant more bits wide,
3973 maybe we can deduce a value of 1 or 0 independent of the data.
3974 Or else truncate the constant now
3975 rather than extend the variable at run time.
3976
3977 This is only interesting if the constant is the wider arg.
3978 Also, it is not safe if the constant is unsigned and the
3979 variable arg is signed, since in this case the variable
3980 would be sign-extended and then regarded as unsigned.
3981 Our technique fails in this case because the lowest/highest
3982 possible unsigned results don't follow naturally from the
3983 lowest/highest possible values of the variable operand.
3984 For just EQ_EXPR and NE_EXPR there is another technique that
3985 could be used: see if the constant can be faithfully represented
3986 in the other operand's type, by truncating it and reextending it
3987 and see if that preserves the constant's value. */
3988
3989 if (!real1 && !real2
9421ebb9 3990 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
b0fc3e72 3991 && TREE_CODE (primop1) == INTEGER_CST
3992 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3993 {
3994 int min_gt, max_gt, min_lt, max_lt;
3995 tree maxval, minval;
3996 /* 1 if comparison is nominally unsigned. */
78a8ed03 3997 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
b0fc3e72 3998 tree val;
3999
4070745f 4000 type = c_common_signed_or_unsigned_type (unsignedp0,
4001 TREE_TYPE (primop0));
cda09c61 4002
b0fc3e72 4003 maxval = TYPE_MAX_VALUE (type);
4004 minval = TYPE_MIN_VALUE (type);
4005
4006 if (unsignedp && !unsignedp0)
4070745f 4007 *restype_ptr = c_common_signed_type (*restype_ptr);
b0fc3e72 4008
4009 if (TREE_TYPE (primop1) != *restype_ptr)
18dbec6f 4010 {
9a5e8086 4011 /* Convert primop1 to target type, but do not introduce
4012 additional overflow. We know primop1 is an int_cst. */
e913b5cd 4013 primop1 = force_fit_type (*restype_ptr,
796b6678 4014 wide_int::from
4015 (primop1,
4016 TYPE_PRECISION (*restype_ptr),
4017 TYPE_SIGN (TREE_TYPE (primop1))),
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
4272 size_exp = size_in_bytes (TREE_TYPE (result_type));
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));
5726 gcc_assert (canon_expr_type != NULL_TREE);
5727
5728 return build_va_arg_1 (loc, type, expr);
5729 }
5730
5731 /* Case 2: Array type.
5732
5733 Background:
5734
5735 For contrast, let's start with the simple case (case 1). If
5736 canon_va_type is not an array type, but say a char *, then when
5737 passing-by-value a va_list, the type of the va_list param decl is
5738 the same as for another va_list decl (all ap's are char *):
5739
5740 f2_1 (char * ap)
5741 D.1815 = VA_ARG (&ap, 0B, 1);
5742 return D.1815;
5743
5744 f2 (int i)
5745 char * ap.0;
5746 char * ap;
5747 __builtin_va_start (&ap, 0);
5748 ap.0 = ap;
5749 res = f2_1 (ap.0);
5750 __builtin_va_end (&ap);
5751 D.1812 = res;
5752 return D.1812;
5753
5754 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
5755 va_list the type of the va_list param decl (case 2b, struct * ap) is not
5756 the same as for another va_list decl (case 2a, struct ap[1]).
5757
5758 f2_1 (struct * ap)
5759 D.1844 = VA_ARG (ap, 0B, 0);
5760 return D.1844;
5761
5762 f2 (int i)
5763 struct ap[1];
5764 __builtin_va_start (&ap, 0);
5765 res = f2_1 (&ap);
5766 __builtin_va_end (&ap);
5767 D.1841 = res;
5768 return D.1841;
5769
5770 Case 2b is different because:
5771 - on the callee side, the parm decl has declared type va_list, but
5772 grokdeclarator changes the type of the parm decl to a pointer to the
5773 array elem type.
5774 - on the caller side, the pass-by-value uses &ap.
5775
5776 We unify these two cases (case 2a: va_list is array type,
5777 case 2b: va_list is pointer to array elem type), by adding '&' for the
5778 array type case, such that we have a pointer to array elem in both
5779 cases. */
5780
5781 if (TREE_CODE (va_type) == ARRAY_TYPE)
5782 {
5783 /* Case 2a: va_list is array type. */
5784
5785 /* Take the address, to get '&ap'. Make sure it's a pointer to array
5786 elem type. */
5787 mark_addressable (expr);
5788 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
5789 expr);
5790
5791 /* Verify that &ap is still recognized as having va_list type. */
5792 tree canon_expr_type
5793 = targetm.canonical_va_list_type (TREE_TYPE (expr));
5794 gcc_assert (canon_expr_type != NULL_TREE);
5795 }
5796 else
5797 {
5798 /* Case 2b: va_list is pointer to array elem type. */
5799 gcc_assert (POINTER_TYPE_P (va_type));
5800 gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type));
5801
5802 /* Don't take the address. We've already got '&ap'. */
5803 ;
5804 }
5805
5806 return build_va_arg_1 (loc, type, expr);
a66c9326 5807}
0d4238dc 5808
5809
dd878098 5810/* Linked list of disabled built-in functions. */
5811
6dc50383 5812struct disabled_builtin
dd878098 5813{
5814 const char *name;
5815 struct disabled_builtin *next;
6dc50383 5816};
dd878098 5817static disabled_builtin *disabled_builtins = NULL;
5818
1cae46be 5819static bool builtin_function_disabled_p (const char *);
dd878098 5820
5821/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5822 begins with "__builtin_", give an error. */
5823
5824void
1cae46be 5825disable_builtin_function (const char *name)
dd878098 5826{
5827 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
b0b1af64 5828 error ("cannot disable built-in function %qs", name);
dd878098 5829 else
5830 {
e85905e5 5831 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5832 new_disabled_builtin->name = name;
5833 new_disabled_builtin->next = disabled_builtins;
5834 disabled_builtins = new_disabled_builtin;
dd878098 5835 }
5836}
5837
5838
5839/* Return true if the built-in function NAME has been disabled, false
5840 otherwise. */
5841
5842static bool
1cae46be 5843builtin_function_disabled_p (const char *name)
dd878098 5844{
5845 disabled_builtin *p;
5846 for (p = disabled_builtins; p != NULL; p = p->next)
5847 {
5848 if (strcmp (name, p->name) == 0)
5849 return true;
5850 }
5851 return false;
5852}
5853
5854
3237155d 5855/* Worker for DEF_BUILTIN.
5856 Possibly define a builtin function with one or two names.
5857 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5858 nonansi_p and flag_no_nonansi_builtin. */
0d4238dc 5859
3237155d 5860static void
5861def_builtin_1 (enum built_in_function fncode,
5862 const char *name,
5863 enum built_in_class fnclass,
5864 tree fntype, tree libtype,
5865 bool both_p, bool fallback_p, bool nonansi_p,
5866 tree fnattrs, bool implicit_p)
0d4238dc 5867{
3237155d 5868 tree decl;
5869 const char *libname;
5870
27213ba3 5871 if (fntype == error_mark_node)
5872 return;
5873
3237155d 5874 gcc_assert ((!both_p && !fallback_p)
5875 || !strncmp (name, "__builtin_",
5876 strlen ("__builtin_")));
5877
5878 libname = name + strlen ("__builtin_");
54be5d7e 5879 decl = add_builtin_function (name, fntype, fncode, fnclass,
5880 (fallback_p ? libname : NULL),
5881 fnattrs);
b9a16870 5882
5883 set_builtin_decl (fncode, decl, implicit_p);
5884
3237155d 5885 if (both_p
5886 && !flag_no_builtin && !builtin_function_disabled_p (libname)
dd878098 5887 && !(nonansi_p && flag_no_nonansi_builtin))
54be5d7e 5888 add_builtin_function (libname, libtype, fncode, fnclass,
5889 NULL, fnattrs);
0d4238dc 5890}
e94026da 5891\f
d7aeef06 5892/* Nonzero if the type T promotes to int. This is (nearly) the
5893 integral promotions defined in ISO C99 6.3.1.1/2. */
5894
5895bool
9f627b1a 5896c_promoting_integer_type_p (const_tree t)
d7aeef06 5897{
5898 switch (TREE_CODE (t))
5899 {
5900 case INTEGER_TYPE:
5901 return (TYPE_MAIN_VARIANT (t) == char_type_node
5902 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5903 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5904 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
7aa1e6eb 5905 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5906 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d7aeef06 5907
5908 case ENUMERAL_TYPE:
5909 /* ??? Technically all enumerations not larger than an int
5910 promote to an int. But this is used along code paths
5911 that only want to notice a size change. */
5912 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5913
5914 case BOOLEAN_TYPE:
5915 return 1;
5916
5917 default:
5918 return 0;
5919 }
5920}
5921
e94026da 5922/* Return 1 if PARMS specifies a fixed number of parameters
5923 and none of their types is affected by default promotions. */
5924
5925int
9f627b1a 5926self_promoting_args_p (const_tree parms)
e94026da 5927{
9f627b1a 5928 const_tree t;
e94026da 5929 for (t = parms; t; t = TREE_CHAIN (t))
5930 {
19cb6b50 5931 tree type = TREE_VALUE (t);
43f74bc4 5932
e1d8e198 5933 if (type == error_mark_node)
5934 continue;
5935
e94026da 5936 if (TREE_CHAIN (t) == 0 && type != void_type_node)
5937 return 0;
5938
5939 if (type == 0)
5940 return 0;
5941
5942 if (TYPE_MAIN_VARIANT (type) == float_type_node)
5943 return 0;
5944
d7aeef06 5945 if (c_promoting_integer_type_p (type))
e94026da 5946 return 0;
5947 }
5948 return 1;
5949}
605fb01e 5950
c10de5e7 5951/* Recursively remove any '*' or '&' operator from TYPE. */
5952tree
5953strip_pointer_operator (tree t)
5954{
5955 while (POINTER_TYPE_P (t))
5956 t = TREE_TYPE (t);
5957 return t;
5958}
5959
57a0ed23 5960/* Recursively remove pointer or array type from TYPE. */
5961tree
5962strip_pointer_or_array_types (tree t)
5963{
5964 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5965 t = TREE_TYPE (t);
5966 return t;
5967}
5968
e41f0d80 5969/* Used to compare case labels. K1 and K2 are actually tree nodes
5970 representing case labels, or NULL_TREE for a `default' label.
5971 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5972 K2, and 0 if K1 and K2 are equal. */
5973
5974int
1cae46be 5975case_compare (splay_tree_key k1, splay_tree_key k2)
e41f0d80 5976{
5977 /* Consider a NULL key (such as arises with a `default' label) to be
5978 smaller than anything else. */
5979 if (!k1)
5980 return k2 ? -1 : 0;
5981 else if (!k2)
5982 return k1 ? 1 : 0;
5983
5984 return tree_int_cst_compare ((tree) k1, (tree) k2);
5985}
5986
e60a6f7b 5987/* Process a case label, located at LOC, for the range LOW_VALUE
5988 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5989 then this case label is actually a `default' label. If only
5990 HIGH_VALUE is NULL_TREE, then case label was declared using the
5991 usual C/C++ syntax, rather than the GNU case range extension.
5992 CASES is a tree containing all the case ranges processed so far;
be23b16f 5993 COND is the condition for the switch-statement itself.
5994 OUTSIDE_RANGE_P says whether there was a case value that doesn't
5995 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
5996 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
e41f0d80 5997
5998tree
e60a6f7b 5999c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
be23b16f 6000 tree low_value, tree high_value, bool *outside_range_p)
e41f0d80 6001{
6002 tree type;
6003 tree label;
6004 tree case_label;
6005 splay_tree_node node;
6006
6007 /* Create the LABEL_DECL itself. */
e60a6f7b 6008 label = create_artificial_label (loc);
e41f0d80 6009
6010 /* If there was an error processing the switch condition, bail now
6011 before we get more confused. */
6012 if (!cond || cond == error_mark_node)
4ee9c684 6013 goto error_out;
e41f0d80 6014
1cae46be 6015 if ((low_value && TREE_TYPE (low_value)
6016 && POINTER_TYPE_P (TREE_TYPE (low_value)))
e41f0d80 6017 || (high_value && TREE_TYPE (high_value)
6018 && POINTER_TYPE_P (TREE_TYPE (high_value))))
b96dc121 6019 {
e60a6f7b 6020 error_at (loc, "pointers are not permitted as case values");
b96dc121 6021 goto error_out;
6022 }
e41f0d80 6023
6024 /* Case ranges are a GNU extension. */
8864917d 6025 if (high_value)
29438999 6026 pedwarn (loc, OPT_Wpedantic,
8864917d 6027 "range expressions in switch statements are non-standard");
e41f0d80 6028
6029 type = TREE_TYPE (cond);
6030 if (low_value)
6031 {
2d2f6a15 6032 low_value = check_case_value (loc, low_value);
22a75734 6033 low_value = convert_and_check (loc, type, low_value);
96722196 6034 if (low_value == error_mark_node)
6035 goto error_out;
e41f0d80 6036 }
6037 if (high_value)
6038 {
2d2f6a15 6039 high_value = check_case_value (loc, high_value);
22a75734 6040 high_value = convert_and_check (loc, type, high_value);
96722196 6041 if (high_value == error_mark_node)
6042 goto error_out;
e41f0d80 6043 }
6044
96722196 6045 if (low_value && high_value)
6046 {
6047 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
a0c938f0 6048 really a case range, even though it was written that way.
6049 Remove the HIGH_VALUE to simplify later processing. */
96722196 6050 if (tree_int_cst_equal (low_value, high_value))
6051 high_value = NULL_TREE;
6052 else if (!tree_int_cst_lt (low_value, high_value))
e60a6f7b 6053 warning_at (loc, 0, "empty range specified");
96722196 6054 }
e41f0d80 6055
2ca392fd 6056 /* See if the case is in range of the type of the original testing
6057 expression. If both low_value and high_value are out of range,
6058 don't insert the case label and return NULL_TREE. */
6059 if (low_value
f61a9bc2 6060 && !check_case_bounds (loc, type, orig_type,
be23b16f 6061 &low_value, high_value ? &high_value : NULL,
6062 outside_range_p))
2ca392fd 6063 return NULL_TREE;
6064
e41f0d80 6065 /* Look up the LOW_VALUE in the table of case labels we already
6066 have. */
6067 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6068 /* If there was not an exact match, check for overlapping ranges.
6069 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6070 that's a `default' label and the only overlap is an exact match. */
6071 if (!node && (low_value || high_value))
6072 {
6073 splay_tree_node low_bound;
6074 splay_tree_node high_bound;
6075
6076 /* Even though there wasn't an exact match, there might be an
6077 overlap between this case range and another case range.
6078 Since we've (inductively) not allowed any overlapping case
6079 ranges, we simply need to find the greatest low case label
6080 that is smaller that LOW_VALUE, and the smallest low case
6081 label that is greater than LOW_VALUE. If there is an overlap
6082 it will occur in one of these two ranges. */
6083 low_bound = splay_tree_predecessor (cases,
6084 (splay_tree_key) low_value);
6085 high_bound = splay_tree_successor (cases,
6086 (splay_tree_key) low_value);
6087
6088 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6089 the LOW_VALUE, so there is no need to check unless the
6090 LOW_BOUND is in fact itself a case range. */
6091 if (low_bound
6092 && CASE_HIGH ((tree) low_bound->value)
6093 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6094 low_value) >= 0)
6095 node = low_bound;
6096 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6097 range is bigger than the low end of the current range, so we
6098 are only interested if the current range is a real range, and
6099 not an ordinary case label. */
1cae46be 6100 else if (high_bound
e41f0d80 6101 && high_value
6102 && (tree_int_cst_compare ((tree) high_bound->key,
6103 high_value)
6104 <= 0))
6105 node = high_bound;
6106 }
6107 /* If there was an overlap, issue an error. */
6108 if (node)
6109 {
eaae3b75 6110 tree duplicate = CASE_LABEL ((tree) node->value);
e41f0d80 6111
6112 if (high_value)
6113 {
e60a6f7b 6114 error_at (loc, "duplicate (or overlapping) case value");
6115 error_at (DECL_SOURCE_LOCATION (duplicate),
6116 "this is the first entry overlapping that value");
e41f0d80 6117 }
6118 else if (low_value)
6119 {
e60a6f7b 6120 error_at (loc, "duplicate case value") ;
6121 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
e41f0d80 6122 }
6123 else
6124 {
e60a6f7b 6125 error_at (loc, "multiple default labels in one switch");
6126 error_at (DECL_SOURCE_LOCATION (duplicate),
6127 "this is the first default label");
e41f0d80 6128 }
4ee9c684 6129 goto error_out;
e41f0d80 6130 }
6131
6132 /* Add a CASE_LABEL to the statement-tree. */
b6e3dd65 6133 case_label = add_stmt (build_case_label (low_value, high_value, label));
e41f0d80 6134 /* Register this case label in the splay tree. */
1cae46be 6135 splay_tree_insert (cases,
e41f0d80 6136 (splay_tree_key) low_value,
6137 (splay_tree_value) case_label);
6138
6139 return case_label;
4ee9c684 6140
6141 error_out:
daf6dff5 6142 /* Add a label so that the back-end doesn't think that the beginning of
4ee9c684 6143 the switch is unreachable. Note that we do not add a case label, as
a53ff4c1 6144 that just leads to duplicates and thence to failure later on. */
4ee9c684 6145 if (!cases->root)
6146 {
e60a6f7b 6147 tree t = create_artificial_label (loc);
6148 add_stmt (build_stmt (loc, LABEL_EXPR, t));
4ee9c684 6149 }
6150 return error_mark_node;
6151}
6152
6153/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6154 Used to verify that case values match up with enumerator values. */
6155
6156static void
6157match_case_to_enum_1 (tree key, tree type, tree label)
6158{
e913b5cd 6159 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6160
dd76621f 6161 if (tree_fits_uhwi_p (key))
6162 print_dec (key, buf, UNSIGNED);
6163 else if (tree_fits_shwi_p (key))
6164 print_dec (key, buf, SIGNED);
4ee9c684 6165 else
e913b5cd 6166 print_hex (key, buf);
4ee9c684 6167
6168 if (TYPE_NAME (type) == 0)
712d2297 6169 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6170 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6171 "case value %qs not in enumerated type",
6172 buf);
4ee9c684 6173 else
712d2297 6174 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6175 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6176 "case value %qs not in enumerated type %qT",
6177 buf, type);
4ee9c684 6178}
6179
359d87c6 6180/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6181 Used to verify that case values match up with enumerator values. */
6182
4ee9c684 6183static int
6184match_case_to_enum (splay_tree_node node, void *data)
6185{
6186 tree label = (tree) node->value;
4fd61bc6 6187 tree type = (tree) data;
4ee9c684 6188
6189 /* Skip default case. */
6190 if (!CASE_LOW (label))
6191 return 0;
6192
359d87c6 6193 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
4ee9c684 6194 when we did our enum->case scan. Reset our scratch bit after. */
359d87c6 6195 if (!CASE_LOW_SEEN (label))
4ee9c684 6196 match_case_to_enum_1 (CASE_LOW (label), type, label);
6197 else
359d87c6 6198 CASE_LOW_SEEN (label) = 0;
4ee9c684 6199
359d87c6 6200 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6201 not set, that means that CASE_HIGH did not appear when we did our
6202 enum->case scan. Reset our scratch bit after. */
4ee9c684 6203 if (CASE_HIGH (label))
6204 {
359d87c6 6205 if (!CASE_HIGH_SEEN (label))
6206 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6207 else
6208 CASE_HIGH_SEEN (label) = 0;
4ee9c684 6209 }
6210
6211 return 0;
6212}
6213
e7911019 6214/* Handle -Wswitch*. Called from the front end after parsing the
6215 switch construct. */
6216/* ??? Should probably be somewhere generic, since other languages
6217 besides C and C++ would want this. At the moment, however, C/C++
6218 are the only tree-ssa languages that support enumerations at all,
6219 so the point is moot. */
4ee9c684 6220
e7911019 6221void
6222c_do_switch_warnings (splay_tree cases, location_t switch_location,
be23b16f 6223 tree type, tree cond, bool bool_cond_p,
6224 bool outside_range_p)
4ee9c684 6225{
b27ac6b5 6226 splay_tree_node default_node;
359d87c6 6227 splay_tree_node node;
6228 tree chain;
4ee9c684 6229
be23b16f 6230 if (!warn_switch && !warn_switch_enum && !warn_switch_default
6231 && !warn_switch_bool)
4ee9c684 6232 return;
6233
4ee9c684 6234 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
8b6866af 6235 if (!default_node)
5fb6a912 6236 warning_at (switch_location, OPT_Wswitch_default,
6237 "switch missing default case");
4ee9c684 6238
be23b16f 6239 /* There are certain cases where -Wswitch-bool warnings aren't
6240 desirable, such as
6241 switch (boolean)
6242 {
6243 case true: ...
6244 case false: ...
6245 }
6246 so be careful here. */
6247 if (warn_switch_bool && bool_cond_p)
6248 {
6249 splay_tree_node min_node;
6250 /* If there's a default node, it's also the value with the minimal
6251 key. So look at the penultimate key (if any). */
6252 if (default_node)
6253 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
6254 else
6255 min_node = splay_tree_min (cases);
6256 tree min = min_node ? (tree) min_node->key : NULL_TREE;
6257
6258 splay_tree_node max_node = splay_tree_max (cases);
6259 /* This might be a case range, so look at the value with the
6260 maximal key and then check CASE_HIGH. */
6261 tree max = max_node ? (tree) max_node->value : NULL_TREE;
6262 if (max)
6263 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
6264
6265 /* If there's a case value > 1 or < 0, that is outside bool
6266 range, warn. */
6267 if (outside_range_p
6268 || (max && wi::gts_p (max, 1))
6269 || (min && wi::lts_p (min, 0))
6270 /* And handle the
6271 switch (boolean)
6272 {
6273 case true: ...
6274 case false: ...
6275 default: ...
6276 }
6277 case, where we want to warn. */
6278 || (default_node
6279 && max && wi::eq_p (max, 1)
6280 && min && wi::eq_p (min, 0)))
6281 warning_at (switch_location, OPT_Wswitch_bool,
6282 "switch condition has boolean value");
6283 }
6284
47ae02b7 6285 /* From here on, we only care about enumerated types. */
359d87c6 6286 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6287 return;
6288
561017b5 6289 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6290 if (!warn_switch_enum && !warn_switch)
359d87c6 6291 return;
6292
561017b5 6293 /* Check the cases. Warn about case values which are not members of
6294 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6295 there is no default case, check that exactly all enumeration
6296 literals are covered by the cases. */
6297
359d87c6 6298 /* Clearing COND if it is not an integer constant simplifies
6299 the tests inside the loop below. */
6300 if (TREE_CODE (cond) != INTEGER_CST)
6301 cond = NULL_TREE;
6302
6303 /* The time complexity here is O(N*lg(N)) worst case, but for the
6304 common case of monotonically increasing enumerators, it is
6305 O(N), since the nature of the splay tree will keep the next
6306 element adjacent to the root at all times. */
4ee9c684 6307
359d87c6 6308 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6309 {
6310 tree value = TREE_VALUE (chain);
3f00a6c0 6311 if (TREE_CODE (value) == CONST_DECL)
6312 value = DECL_INITIAL (value);
359d87c6 6313 node = splay_tree_lookup (cases, (splay_tree_key) value);
6314 if (node)
4ee9c684 6315 {
359d87c6 6316 /* Mark the CASE_LOW part of the case entry as seen. */
6317 tree label = (tree) node->value;
6318 CASE_LOW_SEEN (label) = 1;
6319 continue;
6320 }
6321
6322 /* Even though there wasn't an exact match, there might be a
f0b5f617 6323 case range which includes the enumerator's value. */
359d87c6 6324 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6325 if (node && CASE_HIGH ((tree) node->value))
6326 {
6327 tree label = (tree) node->value;
6328 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6329 if (cmp >= 0)
4ee9c684 6330 {
359d87c6 6331 /* If we match the upper bound exactly, mark the CASE_HIGH
6332 part of the case entry as seen. */
6333 if (cmp == 0)
6334 CASE_HIGH_SEEN (label) = 1;
6335 continue;
4ee9c684 6336 }
6337 }
6338
359d87c6 6339 /* We've now determined that this enumerated literal isn't
6340 handled by the case labels of the switch statement. */
4ee9c684 6341
359d87c6 6342 /* If the switch expression is a constant, we only really care
6343 about whether that constant is handled by the switch. */
6344 if (cond && tree_int_cst_compare (cond, value))
6345 continue;
4ee9c684 6346
6cbbbc89 6347 /* If there is a default_node, the only relevant option is
561017b5 6348 Wswitch-enum. Otherwise, if both are enabled then we prefer
6cbbbc89 6349 to warn using -Wswitch because -Wswitch is enabled by -Wall
6350 while -Wswitch-enum is explicit. */
561017b5 6351 warning_at (switch_location,
6352 (default_node || !warn_switch
6353 ? OPT_Wswitch_enum
6354 : OPT_Wswitch),
6355 "enumeration value %qE not handled in switch",
6356 TREE_PURPOSE (chain));
4ee9c684 6357 }
359d87c6 6358
6359 /* Warn if there are case expressions that don't correspond to
6360 enumerators. This can occur since C and C++ don't enforce
6361 type-checking of assignments to enumeration variables.
6362
6363 The time complexity here is now always O(N) worst case, since
6364 we should have marked both the lower bound and upper bound of
6365 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6366 above. This scan also resets those fields. */
6cbbbc89 6367
359d87c6 6368 splay_tree_foreach (cases, match_case_to_enum, type);
e41f0d80 6369}
6370
9dd48740 6371/* Finish an expression taking the address of LABEL (an
dda49785 6372 IDENTIFIER_NODE). Returns an expression for the address.
6373
6374 LOC is the location for the expression returned. */
d0a47c8d 6375
1cae46be 6376tree
dda49785 6377finish_label_address_expr (tree label, location_t loc)
d0a47c8d 6378{
6379 tree result;
6380
29438999 6381 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
d0a47c8d 6382
9dd48740 6383 if (label == error_mark_node)
6384 return error_mark_node;
6385
d0a47c8d 6386 label = lookup_label (label);
6387 if (label == NULL_TREE)
6388 result = null_pointer_node;
6389 else
6390 {
6391 TREE_USED (label) = 1;
6392 result = build1 (ADDR_EXPR, ptr_type_node, label);
344cd9b2 6393 /* The current function is not necessarily uninlinable.
d0a47c8d 6394 Computed gotos are incompatible with inlining, but the value
6395 here could be used only in a diagnostic, for example. */
dda49785 6396 protected_set_expr_location (result, loc);
d0a47c8d 6397 }
6398
6399 return result;
6400}
4f9a1c9b 6401\f
6402
6403/* Given a boolean expression ARG, return a tree representing an increment
6404 or decrement (as indicated by CODE) of ARG. The front end must check for
6405 invalid cases (e.g., decrement in C++). */
6406tree
1cae46be 6407boolean_increment (enum tree_code code, tree arg)
4f9a1c9b 6408{
6409 tree val;
69db191c 6410 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
c0f19401 6411
4f9a1c9b 6412 arg = stabilize_reference (arg);
6413 switch (code)
6414 {
6415 case PREINCREMENT_EXPR:
14ae0310 6416 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6417 break;
6418 case POSTINCREMENT_EXPR:
14ae0310 6419 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6420 arg = save_expr (arg);
14ae0310 6421 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6422 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6423 break;
6424 case PREDECREMENT_EXPR:
14ae0310 6425 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6426 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6427 break;
6428 case POSTDECREMENT_EXPR:
14ae0310 6429 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6430 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6431 arg = save_expr (arg);
14ae0310 6432 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6433 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6434 break;
6435 default:
231bd014 6436 gcc_unreachable ();
4f9a1c9b 6437 }
6438 TREE_SIDE_EFFECTS (val) = 1;
6439 return val;
6440}
76a6e674 6441\f
f3449a3c 6442/* Built-in macros for stddef.h and stdint.h, that require macros
6443 defined in this file. */
79cf3ec1 6444void
1cae46be 6445c_stddef_cpp_builtins(void)
1ed9d5f5 6446{
63994318 6447 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6448 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6449 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6450 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
36bccbfc 6451 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6452 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
b0726616 6453 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6454 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
f3449a3c 6455 if (SIG_ATOMIC_TYPE)
6456 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6457 if (INT8_TYPE)
6458 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6459 if (INT16_TYPE)
6460 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6461 if (INT32_TYPE)
6462 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6463 if (INT64_TYPE)
6464 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6465 if (UINT8_TYPE)
6466 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6467 if (UINT16_TYPE)
6468 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6469 if (UINT32_TYPE)
6470 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6471 if (UINT64_TYPE)
6472 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6473 if (INT_LEAST8_TYPE)
6474 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6475 if (INT_LEAST16_TYPE)
6476 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6477 if (INT_LEAST32_TYPE)
6478 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6479 if (INT_LEAST64_TYPE)
6480 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6481 if (UINT_LEAST8_TYPE)
6482 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6483 if (UINT_LEAST16_TYPE)
6484 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6485 if (UINT_LEAST32_TYPE)
6486 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6487 if (UINT_LEAST64_TYPE)
6488 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6489 if (INT_FAST8_TYPE)
6490 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6491 if (INT_FAST16_TYPE)
6492 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6493 if (INT_FAST32_TYPE)
6494 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6495 if (INT_FAST64_TYPE)
6496 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6497 if (UINT_FAST8_TYPE)
6498 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6499 if (UINT_FAST16_TYPE)
6500 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6501 if (UINT_FAST32_TYPE)
6502 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6503 if (UINT_FAST64_TYPE)
6504 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6505 if (INTPTR_TYPE)
6506 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6507 if (UINTPTR_TYPE)
6508 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
574006c3 6509}
6510
7d3b509a 6511static void
1cae46be 6512c_init_attributes (void)
7d3b509a 6513{
6514 /* Fill in the built_in_attributes array. */
7c446c95 6515#define DEF_ATTR_NULL_TREE(ENUM) \
7d3b509a 6516 built_in_attributes[(int) ENUM] = NULL_TREE;
7c446c95 6517#define DEF_ATTR_INT(ENUM, VALUE) \
ceb7b692 6518 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
c8010b80 6519#define DEF_ATTR_STRING(ENUM, VALUE) \
6520 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
7d3b509a 6521#define DEF_ATTR_IDENT(ENUM, STRING) \
6522 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6523#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6524 built_in_attributes[(int) ENUM] \
6525 = tree_cons (built_in_attributes[(int) PURPOSE], \
6526 built_in_attributes[(int) VALUE], \
6527 built_in_attributes[(int) CHAIN]);
7d3b509a 6528#include "builtin-attrs.def"
6529#undef DEF_ATTR_NULL_TREE
6530#undef DEF_ATTR_INT
6531#undef DEF_ATTR_IDENT
6532#undef DEF_ATTR_TREE_LIST
76a6e674 6533}
5f3cead1 6534
33199a81 6535/* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6536 identifier as an argument, so the front end shouldn't look it up. */
6537
6538bool
47b19b94 6539attribute_takes_identifier_p (const_tree attr_id)
33199a81 6540{
9bf1c74e 6541 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
02cb1060 6542 if (spec == NULL)
6543 /* Unknown attribute that we'll end up ignoring, return true so we
6544 don't complain about an identifier argument. */
6545 return true;
6546 else if (!strcmp ("mode", spec->name)
6547 || !strcmp ("format", spec->name)
6548 || !strcmp ("cleanup", spec->name))
47b19b94 6549 return true;
6550 else
6551 return targetm.attribute_takes_identifier_p (attr_id);
33199a81 6552}
6553
f8e93a2e 6554/* Attribute handlers common to C front ends. */
6555
6556/* Handle a "packed" attribute; arguments as in
6557 struct attribute_spec.handler. */
6558
6559static tree
9a03a746 6560handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
09347743 6561 int flags, bool *no_add_attrs)
f8e93a2e 6562{
f40175cb 6563 if (TYPE_P (*node))
f8e93a2e 6564 {
6565 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 6566 *node = build_variant_type_copy (*node);
f40175cb 6567 TYPE_PACKED (*node) = 1;
f8e93a2e 6568 }
6569 else if (TREE_CODE (*node) == FIELD_DECL)
c2ab04f9 6570 {
9fd767c5 6571 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6572 /* Still pack bitfields. */
6573 && ! DECL_INITIAL (*node))
c2ab04f9 6574 warning (OPT_Wattributes,
6575 "%qE attribute ignored for field of type %qT",
6576 name, TREE_TYPE (*node));
6577 else
6578 DECL_PACKED (*node) = 1;
6579 }
f8e93a2e 6580 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
f40175cb 6581 used for DECL_REGISTER. It wouldn't mean anything anyway.
6582 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6583 that changes what the typedef is typing. */
f8e93a2e 6584 else
6585 {
9b2d6d13 6586 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6587 *no_add_attrs = true;
6588 }
6589
6590 return NULL_TREE;
6591}
6592
6593/* Handle a "nocommon" attribute; arguments as in
6594 struct attribute_spec.handler. */
6595
6596static tree
1cae46be 6597handle_nocommon_attribute (tree *node, tree name,
9a03a746 6598 tree ARG_UNUSED (args),
6599 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6600{
f48c7f4a 6601 if (VAR_P (*node))
f8e93a2e 6602 DECL_COMMON (*node) = 0;
6603 else
6604 {
9b2d6d13 6605 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6606 *no_add_attrs = true;
6607 }
6608
6609 return NULL_TREE;
6610}
6611
6612/* Handle a "common" attribute; arguments as in
6613 struct attribute_spec.handler. */
6614
6615static tree
9a03a746 6616handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6617 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6618{
f48c7f4a 6619 if (VAR_P (*node))
f8e93a2e 6620 DECL_COMMON (*node) = 1;
6621 else
6622 {
9b2d6d13 6623 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6624 *no_add_attrs = true;
6625 }
6626
6627 return NULL_TREE;
6628}
6629
6630/* Handle a "noreturn" attribute; arguments as in
6631 struct attribute_spec.handler. */
6632
6633static tree
9a03a746 6634handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6635 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6636{
6637 tree type = TREE_TYPE (*node);
6638
6639 /* See FIXME comment in c_common_attribute_table. */
8c582e4f 6640 if (TREE_CODE (*node) == FUNCTION_DECL
6641 || objc_method_decl (TREE_CODE (*node)))
f8e93a2e 6642 TREE_THIS_VOLATILE (*node) = 1;
6643 else if (TREE_CODE (type) == POINTER_TYPE
6644 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6645 TREE_TYPE (*node)
9d4eeb52 6646 = (build_qualified_type
6647 (build_pointer_type
6648 (build_type_variant (TREE_TYPE (type),
6649 TYPE_READONLY (TREE_TYPE (type)), 1)),
6650 TYPE_QUALS (type)));
f8e93a2e 6651 else
6652 {
9b2d6d13 6653 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6654 *no_add_attrs = true;
6655 }
6656
6657 return NULL_TREE;
6658}
6659
5de92639 6660/* Handle a "hot" and attribute; arguments as in
6661 struct attribute_spec.handler. */
6662
6663static tree
6664handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
46f8e3b0 6665 int ARG_UNUSED (flags), bool *no_add_attrs)
5de92639 6666{
758a38ab 6667 if (TREE_CODE (*node) == FUNCTION_DECL
6668 || TREE_CODE (*node) == LABEL_DECL)
5de92639 6669 {
6670 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6671 {
4a026b48 6672 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6673 "with attribute %qs", name, "cold");
5de92639 6674 *no_add_attrs = true;
6675 }
24470055 6676 /* Most of the rest of the hot processing is done later with
6677 lookup_attribute. */
5de92639 6678 }
6679 else
6680 {
6681 warning (OPT_Wattributes, "%qE attribute ignored", name);
6682 *no_add_attrs = true;
6683 }
6684
6685 return NULL_TREE;
6686}
758a38ab 6687
5de92639 6688/* Handle a "cold" and attribute; arguments as in
6689 struct attribute_spec.handler. */
6690
6691static tree
6692handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6693 int ARG_UNUSED (flags), bool *no_add_attrs)
6694{
758a38ab 6695 if (TREE_CODE (*node) == FUNCTION_DECL
6696 || TREE_CODE (*node) == LABEL_DECL)
5de92639 6697 {
6698 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6699 {
4a026b48 6700 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6701 "with attribute %qs", name, "hot");
5de92639 6702 *no_add_attrs = true;
6703 }
24470055 6704 /* Most of the rest of the cold processing is done later with
6705 lookup_attribute. */
5de92639 6706 }
6707 else
6708 {
6709 warning (OPT_Wattributes, "%qE attribute ignored", name);
6710 *no_add_attrs = true;
6711 }
6712
6713 return NULL_TREE;
6714}
6715
a9196da9 6716/* Handle a "no_sanitize_address" attribute; arguments as in
d413ffdd 6717 struct attribute_spec.handler. */
6718
6719static tree
a9196da9 6720handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6721 bool *no_add_attrs)
d413ffdd 6722{
6723 if (TREE_CODE (*node) != FUNCTION_DECL)
6724 {
6725 warning (OPT_Wattributes, "%qE attribute ignored", name);
6726 *no_add_attrs = true;
6727 }
6728
6729 return NULL_TREE;
6730}
6731
a9196da9 6732/* Handle a "no_address_safety_analysis" attribute; arguments as in
6733 struct attribute_spec.handler. */
6734
6735static tree
6736handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6737 bool *no_add_attrs)
6738{
6739 if (TREE_CODE (*node) != FUNCTION_DECL)
6740 warning (OPT_Wattributes, "%qE attribute ignored", name);
6741 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6742 DECL_ATTRIBUTES (*node)
6743 = tree_cons (get_identifier ("no_sanitize_address"),
6744 NULL_TREE, DECL_ATTRIBUTES (*node));
6745 *no_add_attrs = true;
6746 return NULL_TREE;
6747}
6748
05f893e1 6749/* Handle a "no_sanitize_undefined" attribute; arguments as in
6750 struct attribute_spec.handler. */
6751
6752static tree
6753handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6754 bool *no_add_attrs)
6755{
6756 if (TREE_CODE (*node) != FUNCTION_DECL)
6757 {
6758 warning (OPT_Wattributes, "%qE attribute ignored", name);
6759 *no_add_attrs = true;
6760 }
6761
6762 return NULL_TREE;
6763}
6764
947aa916 6765/* Handle a "stack_protect" attribute; arguments as in
6766 struct attribute_spec.handler. */
6767static tree
6768handle_stack_protect_attribute (tree *node, tree name, tree, int,
6769 bool *no_add_attrs)
6770{
6771 if (TREE_CODE (*node) != FUNCTION_DECL)
6772 {
6773 warning (OPT_Wattributes, "%qE attribute ignored", name);
6774 *no_add_attrs = true;
6775 }
6776 else
6777 DECL_ATTRIBUTES (*node)
6778 = tree_cons (get_identifier ("stack_protect"),
6779 NULL_TREE, DECL_ATTRIBUTES (*node));
6780
6781 return NULL_TREE;
6782}
6783
f8e93a2e 6784/* Handle a "noinline" attribute; arguments as in
6785 struct attribute_spec.handler. */
6786
6787static tree
1cae46be 6788handle_noinline_attribute (tree *node, tree name,
9a03a746 6789 tree ARG_UNUSED (args),
6790 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6791{
6792 if (TREE_CODE (*node) == FUNCTION_DECL)
4a026b48 6793 {
6794 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6795 {
6796 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6797 "with attribute %qs", name, "always_inline");
6798 *no_add_attrs = true;
6799 }
6800 else
6801 DECL_UNINLINABLE (*node) = 1;
6802 }
f8e93a2e 6803 else
6804 {
9b2d6d13 6805 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6806 *no_add_attrs = true;
6807 }
6808
6809 return NULL_TREE;
6810}
6811
bdb1f0d1 6812/* Handle a "noclone" attribute; arguments as in
6813 struct attribute_spec.handler. */
6814
6815static tree
6816handle_noclone_attribute (tree *node, tree name,
6817 tree ARG_UNUSED (args),
6818 int ARG_UNUSED (flags), bool *no_add_attrs)
6819{
6820 if (TREE_CODE (*node) != FUNCTION_DECL)
6821 {
6822 warning (OPT_Wattributes, "%qE attribute ignored", name);
6823 *no_add_attrs = true;
6824 }
6825
6826 return NULL_TREE;
6827}
6828
85fbea97 6829/* Handle a "no_icf" attribute; arguments as in
6830 struct attribute_spec.handler. */
6831
6832static tree
6833handle_noicf_attribute (tree *node, tree name,
6834 tree ARG_UNUSED (args),
6835 int ARG_UNUSED (flags), bool *no_add_attrs)
6836{
6837 if (TREE_CODE (*node) != FUNCTION_DECL)
6838 {
6839 warning (OPT_Wattributes, "%qE attribute ignored", name);
6840 *no_add_attrs = true;
6841 }
6842
6843 return NULL_TREE;
6844}
6845
6846
f8e93a2e 6847/* Handle a "always_inline" attribute; arguments as in
6848 struct attribute_spec.handler. */
6849
6850static tree
1cae46be 6851handle_always_inline_attribute (tree *node, tree name,
9a03a746 6852 tree ARG_UNUSED (args),
6853 int ARG_UNUSED (flags),
09347743 6854 bool *no_add_attrs)
f8e93a2e 6855{
6856 if (TREE_CODE (*node) == FUNCTION_DECL)
6857 {
4a026b48 6858 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6859 {
6860 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6861 "with %qs attribute", name, "noinline");
6862 *no_add_attrs = true;
6863 }
ab50af2a 6864 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
6865 {
6866 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6867 "with %qs attribute", name, "target_clones");
6868 *no_add_attrs = true;
6869 }
4a026b48 6870 else
6871 /* Set the attribute and mark it for disregarding inline
6872 limits. */
6873 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
f8e93a2e 6874 }
6875 else
6876 {
9b2d6d13 6877 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6878 *no_add_attrs = true;
541e4101 6879 }
6880
6881 return NULL_TREE;
6882}
6883
6884/* Handle a "gnu_inline" attribute; arguments as in
6885 struct attribute_spec.handler. */
6886
6887static tree
6888handle_gnu_inline_attribute (tree *node, tree name,
6889 tree ARG_UNUSED (args),
6890 int ARG_UNUSED (flags),
6891 bool *no_add_attrs)
6892{
6893 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6894 {
6895 /* Do nothing else, just set the attribute. We'll get at
6896 it later with lookup_attribute. */
6897 }
6898 else
6899 {
6900 warning (OPT_Wattributes, "%qE attribute ignored", name);
6901 *no_add_attrs = true;
7bd95dfd 6902 }
6903
6904 return NULL_TREE;
6905}
6906
6907/* Handle a "leaf" attribute; arguments as in
6908 struct attribute_spec.handler. */
6909
6910static tree
6911handle_leaf_attribute (tree *node, tree name,
6912 tree ARG_UNUSED (args),
6913 int ARG_UNUSED (flags), bool *no_add_attrs)
6914{
6915 if (TREE_CODE (*node) != FUNCTION_DECL)
6916 {
6917 warning (OPT_Wattributes, "%qE attribute ignored", name);
6918 *no_add_attrs = true;
6919 }
6920 if (!TREE_PUBLIC (*node))
6921 {
6922 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6923 *no_add_attrs = true;
f8e93a2e 6924 }
6925
6926 return NULL_TREE;
6927}
6928
1b16fc45 6929/* Handle an "artificial" attribute; arguments as in
6930 struct attribute_spec.handler. */
6931
6932static tree
6933handle_artificial_attribute (tree *node, tree name,
6934 tree ARG_UNUSED (args),
6935 int ARG_UNUSED (flags),
6936 bool *no_add_attrs)
6937{
6938 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6939 {
6940 /* Do nothing else, just set the attribute. We'll get at
6941 it later with lookup_attribute. */
6942 }
6943 else
6944 {
6945 warning (OPT_Wattributes, "%qE attribute ignored", name);
6946 *no_add_attrs = true;
6947 }
6948
6949 return NULL_TREE;
6950}
6951
0cdd9887 6952/* Handle a "flatten" attribute; arguments as in
6953 struct attribute_spec.handler. */
6954
6955static tree
6956handle_flatten_attribute (tree *node, tree name,
a0c938f0 6957 tree args ATTRIBUTE_UNUSED,
6958 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
0cdd9887 6959{
6960 if (TREE_CODE (*node) == FUNCTION_DECL)
6961 /* Do nothing else, just set the attribute. We'll get at
6962 it later with lookup_attribute. */
6963 ;
6964 else
6965 {
6966 warning (OPT_Wattributes, "%qE attribute ignored", name);
6967 *no_add_attrs = true;
6968 }
6969
6970 return NULL_TREE;
6971}
6972
10fc867f 6973/* Handle a "warning" or "error" attribute; arguments as in
6974 struct attribute_spec.handler. */
6975
6976static tree
6977handle_error_attribute (tree *node, tree name, tree args,
6978 int ARG_UNUSED (flags), bool *no_add_attrs)
6979{
6980 if (TREE_CODE (*node) == FUNCTION_DECL
df936998 6981 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
10fc867f 6982 /* Do nothing else, just set the attribute. We'll get at
6983 it later with lookup_attribute. */
6984 ;
6985 else
6986 {
6987 warning (OPT_Wattributes, "%qE attribute ignored", name);
6988 *no_add_attrs = true;
6989 }
6990
6991 return NULL_TREE;
6992}
0cdd9887 6993
f8e93a2e 6994/* Handle a "used" attribute; arguments as in
6995 struct attribute_spec.handler. */
6996
6997static tree
9a03a746 6998handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
6999 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7000{
d0a31bd8 7001 tree node = *pnode;
7002
7003 if (TREE_CODE (node) == FUNCTION_DECL
f48c7f4a 7004 || (VAR_P (node) && TREE_STATIC (node))
a4e3ffad 7005 || (TREE_CODE (node) == TYPE_DECL))
f54ed8bc 7006 {
f54ed8bc 7007 TREE_USED (node) = 1;
9423c9b7 7008 DECL_PRESERVE_P (node) = 1;
f48c7f4a 7009 if (VAR_P (node))
abc6c64f 7010 DECL_READ_P (node) = 1;
f54ed8bc 7011 }
f8e93a2e 7012 else
7013 {
9b2d6d13 7014 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7015 *no_add_attrs = true;
7016 }
7017
7018 return NULL_TREE;
7019}
7020
7021/* Handle a "unused" attribute; arguments as in
7022 struct attribute_spec.handler. */
7023
7024static tree
9a03a746 7025handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7026 int flags, bool *no_add_attrs)
f8e93a2e 7027{
7028 if (DECL_P (*node))
7029 {
7030 tree decl = *node;
7031
7032 if (TREE_CODE (decl) == PARM_DECL
4e81b384 7033 || VAR_OR_FUNCTION_DECL_P (decl)
f8e93a2e 7034 || TREE_CODE (decl) == LABEL_DECL
7035 || TREE_CODE (decl) == TYPE_DECL)
abc6c64f 7036 {
7037 TREE_USED (decl) = 1;
f48c7f4a 7038 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
abc6c64f 7039 DECL_READ_P (decl) = 1;
7040 }
f8e93a2e 7041 else
7042 {
9b2d6d13 7043 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7044 *no_add_attrs = true;
7045 }
7046 }
7047 else
7048 {
7049 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 7050 *node = build_variant_type_copy (*node);
f8e93a2e 7051 TREE_USED (*node) = 1;
7052 }
7053
7054 return NULL_TREE;
7055}
7056
62eec3b4 7057/* Handle a "externally_visible" attribute; arguments as in
7058 struct attribute_spec.handler. */
7059
7060static tree
7061handle_externally_visible_attribute (tree *pnode, tree name,
7062 tree ARG_UNUSED (args),
7063 int ARG_UNUSED (flags),
7064 bool *no_add_attrs)
7065{
7066 tree node = *pnode;
7067
b443c459 7068 if (VAR_OR_FUNCTION_DECL_P (node))
62eec3b4 7069 {
ba12ea31 7070 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7071 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7072 {
7073 warning (OPT_Wattributes,
7074 "%qE attribute have effect only on public objects", name);
7075 *no_add_attrs = true;
7076 }
62eec3b4 7077 }
62eec3b4 7078 else
7079 {
7080 warning (OPT_Wattributes, "%qE attribute ignored", name);
7081 *no_add_attrs = true;
7082 }
7083
7084 return NULL_TREE;
7085}
7086
6b722052 7087/* Handle the "no_reorder" attribute. Arguments as in
7088 struct attribute_spec.handler. */
7089
7090static tree
7091handle_no_reorder_attribute (tree *pnode,
7092 tree name,
7093 tree,
7094 int,
7095 bool *no_add_attrs)
7096{
7097 tree node = *pnode;
7098
b443c459 7099 if (!VAR_OR_FUNCTION_DECL_P (node)
6b722052 7100 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7101 {
7102 warning (OPT_Wattributes,
7103 "%qE attribute only affects top level objects",
7104 name);
7105 *no_add_attrs = true;
7106 }
7107
7108 return NULL_TREE;
7109}
7110
f8e93a2e 7111/* Handle a "const" attribute; arguments as in
7112 struct attribute_spec.handler. */
7113
7114static tree
9a03a746 7115handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7116 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7117{
7118 tree type = TREE_TYPE (*node);
7119
7120 /* See FIXME comment on noreturn in c_common_attribute_table. */
7121 if (TREE_CODE (*node) == FUNCTION_DECL)
7122 TREE_READONLY (*node) = 1;
7123 else if (TREE_CODE (type) == POINTER_TYPE
7124 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7125 TREE_TYPE (*node)
9d4eeb52 7126 = (build_qualified_type
7127 (build_pointer_type
7128 (build_type_variant (TREE_TYPE (type), 1,
7129 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7130 TYPE_QUALS (type)));
f8e93a2e 7131 else
7132 {
9b2d6d13 7133 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7134 *no_add_attrs = true;
7135 }
7136
7137 return NULL_TREE;
7138}
7139
292237f3 7140/* Handle a "scalar_storage_order" attribute; arguments as in
7141 struct attribute_spec.handler. */
7142
7143static tree
7144handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
7145 int flags, bool *no_add_attrs)
7146{
7147 tree id = TREE_VALUE (args);
7148 tree type;
7149
7150 if (TREE_CODE (*node) == TYPE_DECL
7151 && ! (flags & ATTR_FLAG_CXX11))
7152 node = &TREE_TYPE (*node);
7153 type = *node;
7154
7155 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
7156 {
7157 error ("scalar_storage_order is not supported because endianness "
7158 "is not uniform");
7159 return NULL_TREE;
7160 }
7161
7162 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
7163 {
7164 bool reverse = false;
7165
7166 if (TREE_CODE (id) == STRING_CST
7167 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
7168 reverse = !BYTES_BIG_ENDIAN;
7169 else if (TREE_CODE (id) == STRING_CST
7170 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
7171 reverse = BYTES_BIG_ENDIAN;
7172 else
7173 {
7174 error ("scalar_storage_order argument must be one of \"big-endian\""
7175 " or \"little-endian\"");
7176 return NULL_TREE;
7177 }
7178
7179 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7180 {
7181 if (reverse)
7182 /* A type variant isn't good enough, since we don't want a cast
7183 to such a type to be removed as a no-op. */
7184 *node = type = build_duplicate_type (type);
7185 }
7186
7187 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
7188 return NULL_TREE;
7189 }
7190
7191 warning (OPT_Wattributes, "%qE attribute ignored", name);
7192 *no_add_attrs = true;
7193 return NULL_TREE;
7194}
7195
f8e93a2e 7196/* Handle a "transparent_union" attribute; arguments as in
7197 struct attribute_spec.handler. */
7198
7199static tree
1cae46be 7200handle_transparent_union_attribute (tree *node, tree name,
9a03a746 7201 tree ARG_UNUSED (args), int flags,
09347743 7202 bool *no_add_attrs)
f8e93a2e 7203{
881eb642 7204 tree type;
03908818 7205
7206 *no_add_attrs = true;
f8e93a2e 7207
ffcdbf9c 7208 if (TREE_CODE (*node) == TYPE_DECL
7209 && ! (flags & ATTR_FLAG_CXX11))
881eb642 7210 node = &TREE_TYPE (*node);
7211 type = *node;
f8e93a2e 7212
03908818 7213 if (TREE_CODE (type) == UNION_TYPE)
f8e93a2e 7214 {
fca86134 7215 /* Make sure that the first field will work for a transparent union.
7216 If the type isn't complete yet, leave the check to the code in
7217 finish_struct. */
7218 if (TYPE_SIZE (type))
7219 {
7220 tree first = first_field (type);
7221 if (first == NULL_TREE
7222 || DECL_ARTIFICIAL (first)
7223 || TYPE_MODE (type) != DECL_MODE (first))
7224 goto ignored;
7225 }
7226
f8e93a2e 7227 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
03908818 7228 {
fca86134 7229 /* If the type isn't complete yet, setting the flag
7230 on a variant wouldn't ever be checked. */
7231 if (!TYPE_SIZE (type))
7232 goto ignored;
7233
7234 /* build_duplicate_type doesn't work for C++. */
7235 if (c_dialect_cxx ())
03908818 7236 goto ignored;
7237
292237f3 7238 /* A type variant isn't good enough, since we don't want a cast
7239 to such a type to be removed as a no-op. */
03908818 7240 *node = type = build_duplicate_type (type);
7241 }
7242
c25b3e3f 7243 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7244 TYPE_TRANSPARENT_AGGR (t) = 1;
03908818 7245 return NULL_TREE;
f8e93a2e 7246 }
7247
03908818 7248 ignored:
7249 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7250 return NULL_TREE;
7251}
7252
9af7fd5b 7253/* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7254 get the requested priority for a constructor or destructor,
7255 possibly issuing diagnostics for invalid or reserved
7256 priorities. */
7257
7258static priority_type
7259get_priority (tree args, bool is_destructor)
7260{
7261 HOST_WIDE_INT pri;
6c181a06 7262 tree arg;
9af7fd5b 7263
7264 if (!args)
7265 return DEFAULT_INIT_PRIORITY;
48e1416a 7266
28fbc04f 7267 if (!SUPPORTS_INIT_PRIORITY)
7268 {
7269 if (is_destructor)
7270 error ("destructor priorities are not supported");
7271 else
7272 error ("constructor priorities are not supported");
7273 return DEFAULT_INIT_PRIORITY;
7274 }
7275
6c181a06 7276 arg = TREE_VALUE (args);
253e1cae 7277 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7278 goto invalid;
7279 if (arg == error_mark_node)
7280 return DEFAULT_INIT_PRIORITY;
c28ddc97 7281 arg = default_conversion (arg);
e913b5cd 7282 if (!tree_fits_shwi_p (arg)
6c181a06 7283 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
9af7fd5b 7284 goto invalid;
7285
e913b5cd 7286 pri = tree_to_shwi (arg);
9af7fd5b 7287 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7288 goto invalid;
7289
7290 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7291 {
7292 if (is_destructor)
7293 warning (0,
7294 "destructor priorities from 0 to %d are reserved "
48e1416a 7295 "for the implementation",
9af7fd5b 7296 MAX_RESERVED_INIT_PRIORITY);
7297 else
7298 warning (0,
7299 "constructor priorities from 0 to %d are reserved "
48e1416a 7300 "for the implementation",
9af7fd5b 7301 MAX_RESERVED_INIT_PRIORITY);
7302 }
7303 return pri;
7304
7305 invalid:
7306 if (is_destructor)
7307 error ("destructor priorities must be integers from 0 to %d inclusive",
7308 MAX_INIT_PRIORITY);
7309 else
7310 error ("constructor priorities must be integers from 0 to %d inclusive",
7311 MAX_INIT_PRIORITY);
7312 return DEFAULT_INIT_PRIORITY;
7313}
7314
f8e93a2e 7315/* Handle a "constructor" attribute; arguments as in
7316 struct attribute_spec.handler. */
7317
7318static tree
9af7fd5b 7319handle_constructor_attribute (tree *node, tree name, tree args,
9a03a746 7320 int ARG_UNUSED (flags),
09347743 7321 bool *no_add_attrs)
f8e93a2e 7322{
7323 tree decl = *node;
7324 tree type = TREE_TYPE (decl);
7325
7326 if (TREE_CODE (decl) == FUNCTION_DECL
7327 && TREE_CODE (type) == FUNCTION_TYPE
7328 && decl_function_context (decl) == 0)
7329 {
9af7fd5b 7330 priority_type priority;
f8e93a2e 7331 DECL_STATIC_CONSTRUCTOR (decl) = 1;
9af7fd5b 7332 priority = get_priority (args, /*is_destructor=*/false);
7333 SET_DECL_INIT_PRIORITY (decl, priority);
f8e93a2e 7334 TREE_USED (decl) = 1;
7335 }
7336 else
7337 {
9b2d6d13 7338 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7339 *no_add_attrs = true;
7340 }
7341
7342 return NULL_TREE;
7343}
7344
7345/* Handle a "destructor" attribute; arguments as in
7346 struct attribute_spec.handler. */
7347
7348static tree
9af7fd5b 7349handle_destructor_attribute (tree *node, tree name, tree args,
9a03a746 7350 int ARG_UNUSED (flags),
09347743 7351 bool *no_add_attrs)
f8e93a2e 7352{
7353 tree decl = *node;
7354 tree type = TREE_TYPE (decl);
7355
7356 if (TREE_CODE (decl) == FUNCTION_DECL
7357 && TREE_CODE (type) == FUNCTION_TYPE
7358 && decl_function_context (decl) == 0)
7359 {
9af7fd5b 7360 priority_type priority;
f8e93a2e 7361 DECL_STATIC_DESTRUCTOR (decl) = 1;
9af7fd5b 7362 priority = get_priority (args, /*is_destructor=*/true);
7363 SET_DECL_FINI_PRIORITY (decl, priority);
f8e93a2e 7364 TREE_USED (decl) = 1;
7365 }
7366 else
7367 {
9b2d6d13 7368 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7369 *no_add_attrs = true;
7370 }
7371
7372 return NULL_TREE;
7373}
7374
1c58e3f1 7375/* Nonzero if the mode is a valid vector mode for this architecture.
7376 This returns nonzero even if there is no hardware support for the
7377 vector mode, but we can emulate with narrower modes. */
7378
7379static int
3754d046 7380vector_mode_valid_p (machine_mode mode)
1c58e3f1 7381{
7382 enum mode_class mclass = GET_MODE_CLASS (mode);
3754d046 7383 machine_mode innermode;
1c58e3f1 7384
7385 /* Doh! What's going on? */
7386 if (mclass != MODE_VECTOR_INT
7387 && mclass != MODE_VECTOR_FLOAT
7388 && mclass != MODE_VECTOR_FRACT
7389 && mclass != MODE_VECTOR_UFRACT
7390 && mclass != MODE_VECTOR_ACCUM
7391 && mclass != MODE_VECTOR_UACCUM)
7392 return 0;
7393
7394 /* Hardware support. Woo hoo! */
7395 if (targetm.vector_mode_supported_p (mode))
7396 return 1;
7397
7398 innermode = GET_MODE_INNER (mode);
7399
7400 /* We should probably return 1 if requesting V4DI and we have no DI,
7401 but we have V2DI, but this is probably very unlikely. */
7402
7403 /* If we have support for the inner mode, we can safely emulate it.
7404 We may not have V2DI, but me can emulate with a pair of DIs. */
7405 return targetm.scalar_mode_supported_p (innermode);
7406}
7407
7408
f8e93a2e 7409/* Handle a "mode" attribute; arguments as in
7410 struct attribute_spec.handler. */
7411
7412static tree
9a03a746 7413handle_mode_attribute (tree *node, tree name, tree args,
7414 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7415{
7416 tree type = *node;
d1dd9ac0 7417 tree ident = TREE_VALUE (args);
f8e93a2e 7418
7419 *no_add_attrs = true;
ab2c1de8 7420
d1dd9ac0 7421 if (TREE_CODE (ident) != IDENTIFIER_NODE)
9b2d6d13 7422 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7423 else
7424 {
7425 int j;
d1dd9ac0 7426 const char *p = IDENTIFIER_POINTER (ident);
f8e93a2e 7427 int len = strlen (p);
3754d046 7428 machine_mode mode = VOIDmode;
f8e93a2e 7429 tree typefm;
b2aef146 7430 bool valid_mode;
f8e93a2e 7431
7432 if (len > 4 && p[0] == '_' && p[1] == '_'
7433 && p[len - 1] == '_' && p[len - 2] == '_')
7434 {
4fd61bc6 7435 char *newp = (char *) alloca (len - 1);
f8e93a2e 7436
7437 strcpy (newp, &p[2]);
7438 newp[len - 4] = '\0';
7439 p = newp;
7440 }
7441
7442 /* Change this type to have a type with the specified mode.
7443 First check for the special modes. */
84166705 7444 if (!strcmp (p, "byte"))
f8e93a2e 7445 mode = byte_mode;
7446 else if (!strcmp (p, "word"))
7447 mode = word_mode;
84166705 7448 else if (!strcmp (p, "pointer"))
f8e93a2e 7449 mode = ptr_mode;
0ef89dfd 7450 else if (!strcmp (p, "libgcc_cmp_return"))
7451 mode = targetm.libgcc_cmp_return_mode ();
7452 else if (!strcmp (p, "libgcc_shift_count"))
7453 mode = targetm.libgcc_shift_count_mode ();
1bd43494 7454 else if (!strcmp (p, "unwind_word"))
7455 mode = targetm.unwind_word_mode ();
f8e93a2e 7456 else
7457 for (j = 0; j < NUM_MACHINE_MODES; j++)
7458 if (!strcmp (p, GET_MODE_NAME (j)))
743a6f47 7459 {
3754d046 7460 mode = (machine_mode) j;
743a6f47 7461 break;
7462 }
f8e93a2e 7463
7464 if (mode == VOIDmode)
4917c376 7465 {
d1dd9ac0 7466 error ("unknown machine mode %qE", ident);
4917c376 7467 return NULL_TREE;
7468 }
7469
b2aef146 7470 valid_mode = false;
7471 switch (GET_MODE_CLASS (mode))
4917c376 7472 {
b2aef146 7473 case MODE_INT:
7474 case MODE_PARTIAL_INT:
7475 case MODE_FLOAT:
c4503c0a 7476 case MODE_DECIMAL_FLOAT:
9421ebb9 7477 case MODE_FRACT:
7478 case MODE_UFRACT:
7479 case MODE_ACCUM:
7480 case MODE_UACCUM:
b2aef146 7481 valid_mode = targetm.scalar_mode_supported_p (mode);
7482 break;
7483
7484 case MODE_COMPLEX_INT:
7485 case MODE_COMPLEX_FLOAT:
7486 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7487 break;
7488
7489 case MODE_VECTOR_INT:
7490 case MODE_VECTOR_FLOAT:
9421ebb9 7491 case MODE_VECTOR_FRACT:
7492 case MODE_VECTOR_UFRACT:
7493 case MODE_VECTOR_ACCUM:
7494 case MODE_VECTOR_UACCUM:
9b2d6d13 7495 warning (OPT_Wattributes, "specifying vector types with "
7496 "__attribute__ ((mode)) is deprecated");
7497 warning (OPT_Wattributes,
7498 "use __attribute__ ((vector_size)) instead");
b2aef146 7499 valid_mode = vector_mode_valid_p (mode);
7500 break;
4917c376 7501
b2aef146 7502 default:
7503 break;
7504 }
7505 if (!valid_mode)
7506 {
1e5fcbe2 7507 error ("unable to emulate %qs", p);
b2aef146 7508 return NULL_TREE;
7509 }
4917c376 7510
b2aef146 7511 if (POINTER_TYPE_P (type))
ead34f59 7512 {
6d5d708e 7513 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
3754d046 7514 tree (*fn)(tree, machine_mode, bool);
b2aef146 7515
6d5d708e 7516 if (!targetm.addr_space.valid_pointer_mode (mode, as))
ead34f59 7517 {
1e5fcbe2 7518 error ("invalid pointer mode %qs", p);
ead34f59 7519 return NULL_TREE;
7520 }
7521
a0c938f0 7522 if (TREE_CODE (type) == POINTER_TYPE)
b2aef146 7523 fn = build_pointer_type_for_mode;
805e22b2 7524 else
b2aef146 7525 fn = build_reference_type_for_mode;
7526 typefm = fn (TREE_TYPE (type), mode, false);
ead34f59 7527 }
b2aef146 7528 else
9421ebb9 7529 {
7530 /* For fixed-point modes, we need to test if the signness of type
7531 and the machine mode are consistent. */
7532 if (ALL_FIXED_POINT_MODE_P (mode)
7533 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7534 {
bf776685 7535 error ("signedness of type and machine mode %qs don%'t match", p);
9421ebb9 7536 return NULL_TREE;
7537 }
7538 /* For fixed-point modes, we need to pass saturating info. */
7539 typefm = lang_hooks.types.type_for_mode (mode,
7540 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7541 : TYPE_UNSIGNED (type));
7542 }
3a648ab9 7543
b2aef146 7544 if (typefm == NULL_TREE)
7545 {
743a6f47 7546 error ("no data type for mode %qs", p);
b2aef146 7547 return NULL_TREE;
7548 }
3a648ab9 7549 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7550 {
7551 /* For enumeral types, copy the precision from the integer
7552 type returned above. If not an INTEGER_TYPE, we can't use
7553 this mode for this type. */
7554 if (TREE_CODE (typefm) != INTEGER_TYPE)
7555 {
743a6f47 7556 error ("cannot use mode %qs for enumeral types", p);
3a648ab9 7557 return NULL_TREE;
7558 }
7559
10080eac 7560 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7561 {
7562 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7563 typefm = type;
7564 }
7565 else
7566 {
7567 /* We cannot build a type variant, as there's code that assumes
7568 that TYPE_MAIN_VARIANT has the same mode. This includes the
7569 debug generators. Instead, create a subrange type. This
7570 results in all of the enumeral values being emitted only once
7571 in the original, and the subtype gets them by reference. */
7572 if (TYPE_UNSIGNED (type))
7573 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7574 else
7575 typefm = make_signed_type (TYPE_PRECISION (typefm));
7576 TREE_TYPE (typefm) = type;
7577 }
3a648ab9 7578 }
4bf450a1 7579 else if (VECTOR_MODE_P (mode)
7580 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7581 : TREE_CODE (type) != TREE_CODE (typefm))
743a6f47 7582 {
7583 error ("mode %qs applied to inappropriate type", p);
7584 return NULL_TREE;
7585 }
7586
b2aef146 7587 *node = typefm;
f8e93a2e 7588 }
7589
7590 return NULL_TREE;
7591}
7592
7593/* Handle a "section" attribute; arguments as in
7594 struct attribute_spec.handler. */
7595
7596static tree
9a03a746 7597handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7598 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7599{
7600 tree decl = *node;
7601
9866562d 7602 if (!targetm_common.have_named_sections)
f8e93a2e 7603 {
9866562d 7604 error_at (DECL_SOURCE_LOCATION (*node),
7605 "section attributes are not supported for this target");
7606 goto fail;
7607 }
065efcb1 7608
9866562d 7609 user_defined_section_attribute = true;
f8e93a2e 7610
b443c459 7611 if (!VAR_OR_FUNCTION_DECL_P (decl))
9866562d 7612 {
7613 error ("section attribute not allowed for %q+D", *node);
7614 goto fail;
f8e93a2e 7615 }
9866562d 7616
7617 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
f8e93a2e 7618 {
9866562d 7619 error ("section attribute argument not a string constant");
7620 goto fail;
f8e93a2e 7621 }
7622
f48c7f4a 7623 if (VAR_P (decl)
9866562d 7624 && current_function_decl != NULL_TREE
7625 && !TREE_STATIC (decl))
7626 {
7627 error_at (DECL_SOURCE_LOCATION (decl),
7628 "section attribute cannot be specified for local variables");
7629 goto fail;
7630 }
7631
7632 /* The decl may have already been given a section attribute
7633 from a previous declaration. Ensure they match. */
7634 if (DECL_SECTION_NAME (decl) != NULL
7635 && strcmp (DECL_SECTION_NAME (decl),
7636 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7637 {
7638 error ("section of %q+D conflicts with previous declaration", *node);
7639 goto fail;
7640 }
7641
f48c7f4a 7642 if (VAR_P (decl)
9866562d 7643 && !targetm.have_tls && targetm.emutls.tmpl_section
7644 && DECL_THREAD_LOCAL_P (decl))
7645 {
7646 error ("section of %q+D cannot be overridden", *node);
7647 goto fail;
7648 }
7649
7650 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
7651 return NULL_TREE;
7652
7653fail:
7654 *no_add_attrs = true;
f8e93a2e 7655 return NULL_TREE;
7656}
7657
83e25171 7658/* Check whether ALIGN is a valid user-specified alignment. If so,
7659 return its base-2 log; if not, output an error and return -1. If
7660 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7661 no error. */
7662int
7663check_user_alignment (const_tree align, bool allow_zero)
7664{
7665 int i;
7666
3e5a8b00 7667 if (error_operand_p (align))
7668 return -1;
5abaa10a 7669 if (TREE_CODE (align) != INTEGER_CST
7670 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
83e25171 7671 {
7672 error ("requested alignment is not an integer constant");
7673 return -1;
7674 }
7675 else if (allow_zero && integer_zerop (align))
7676 return -1;
1a087624 7677 else if (tree_int_cst_sgn (align) == -1
7678 || (i = tree_log2 (align)) == -1)
83e25171 7679 {
1a087624 7680 error ("requested alignment is not a positive power of 2");
83e25171 7681 return -1;
7682 }
7683 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7684 {
7685 error ("requested alignment is too large");
7686 return -1;
7687 }
7688 return i;
7689}
7690
ffcdbf9c 7691/*
7692 If in c++-11, check if the c++-11 alignment constraint with respect
7693 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7694 c++-11 mode, does nothing.
7695
7696 [dcl.align]2/ says:
7697
7698 [* if the constant expression evaluates to a fundamental alignment,
7699 the alignment requirement of the declared entity shall be the
7700 specified fundamental alignment.
7701
7702 * if the constant expression evaluates to an extended alignment
7703 and the implementation supports that alignment in the context
7704 of the declaration, the alignment of the declared entity shall
7705 be that alignment
7706
7707 * if the constant expression evaluates to an extended alignment
7708 and the implementation does not support that alignment in the
7709 context of the declaration, the program is ill-formed]. */
7710
7711static bool
7712check_cxx_fundamental_alignment_constraints (tree node,
7713 unsigned align_log,
7714 int flags)
7715{
7716 bool alignment_too_large_p = false;
7717 unsigned requested_alignment = 1U << align_log;
7718 unsigned max_align = 0;
7719
7720 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7721 || (node == NULL_TREE || node == error_mark_node))
7722 return true;
7723
7724 if (cxx_fundamental_alignment_p (requested_alignment))
7725 return true;
7726
7727 if (DECL_P (node))
7728 {
7729 if (TREE_STATIC (node))
7730 {
7731 /* For file scope variables and static members, the target
7732 supports alignments that are at most
7733 MAX_OFILE_ALIGNMENT. */
7734 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7735 alignment_too_large_p = true;
7736 }
7737 else
7738 {
7739#ifdef BIGGEST_FIELD_ALIGNMENT
7740#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7741#else
7742#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7743#endif
7744 /* For non-static members, the target supports either
7745 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7746 if it is defined or BIGGEST_ALIGNMENT. */
7747 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7748 if (TREE_CODE (node) == FIELD_DECL
7749 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7750 alignment_too_large_p = true;
7751#undef MAX_TARGET_FIELD_ALIGNMENT
7752 /* For stack variables, the target supports at most
7753 MAX_STACK_ALIGNMENT. */
7754 else if (decl_function_context (node) != NULL
7755 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7756 alignment_too_large_p = true;
7757 }
7758 }
7759 else if (TYPE_P (node))
7760 {
7761 /* Let's be liberal for types. */
7762 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7763 alignment_too_large_p = true;
7764 }
7765
7766 if (alignment_too_large_p)
7767 pedwarn (input_location, OPT_Wattributes,
7768 "requested alignment %d is larger than %d",
7769 requested_alignment, max_align);
7770
7771 return !alignment_too_large_p;
7772}
7773
f8e93a2e 7774/* Handle a "aligned" attribute; arguments as in
7775 struct attribute_spec.handler. */
7776
7777static tree
9a03a746 7778handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
09347743 7779 int flags, bool *no_add_attrs)
f8e93a2e 7780{
7781 tree decl = NULL_TREE;
7782 tree *type = NULL;
7783 int is_type = 0;
caf62483 7784 tree align_expr;
f8e93a2e 7785 int i;
7786
caf62483 7787 if (args)
7788 {
7789 align_expr = TREE_VALUE (args);
3e5a8b00 7790 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7791 && TREE_CODE (align_expr) != FUNCTION_DECL)
caf62483 7792 align_expr = default_conversion (align_expr);
7793 }
7794 else
7795 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7796
f8e93a2e 7797 if (DECL_P (*node))
7798 {
7799 decl = *node;
7800 type = &TREE_TYPE (decl);
7801 is_type = TREE_CODE (*node) == TYPE_DECL;
7802 }
7803 else if (TYPE_P (*node))
7804 type = node, is_type = 1;
7805
ffcdbf9c 7806 if ((i = check_user_alignment (align_expr, false)) == -1
7807 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
83e25171 7808 *no_add_attrs = true;
f8e93a2e 7809 else if (is_type)
7810 {
2ec3af9c 7811 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7812 /* OK, modify the type in place. */;
f8e93a2e 7813 /* If we have a TYPE_DECL, then copy the type, so that we
7814 don't accidentally modify a builtin type. See pushdecl. */
2ec3af9c 7815 else if (decl && TREE_TYPE (decl) != error_mark_node
7816 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
f8e93a2e 7817 {
7818 tree tt = TREE_TYPE (decl);
e086912e 7819 *type = build_variant_type_copy (*type);
f8e93a2e 7820 DECL_ORIGINAL_TYPE (decl) = tt;
7821 TYPE_NAME (*type) = decl;
7822 TREE_USED (*type) = TREE_USED (decl);
7823 TREE_TYPE (decl) = *type;
7824 }
2ec3af9c 7825 else
e086912e 7826 *type = build_variant_type_copy (*type);
f8e93a2e 7827
7cfdc2f0 7828 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
f8e93a2e 7829 TYPE_USER_ALIGN (*type) = 1;
7830 }
097b5c8b 7831 else if (! VAR_OR_FUNCTION_DECL_P (decl)
f8e93a2e 7832 && TREE_CODE (decl) != FIELD_DECL)
7833 {
3cf8b391 7834 error ("alignment may not be specified for %q+D", decl);
f8e93a2e 7835 *no_add_attrs = true;
7836 }
ffcdbf9c 7837 else if (DECL_USER_ALIGN (decl)
7838 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7839 /* C++-11 [dcl.align/4]:
7840
7841 When multiple alignment-specifiers are specified for an
7842 entity, the alignment requirement shall be set to the
7843 strictest specified alignment.
7844
7845 This formally comes from the c++11 specification but we are
7846 doing it for the GNU attribute syntax as well. */
7847 *no_add_attrs = true;
097b5c8b 7848 else if (TREE_CODE (decl) == FUNCTION_DECL
7cfdc2f0 7849 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
097b5c8b 7850 {
7851 if (DECL_USER_ALIGN (decl))
7852 error ("alignment for %q+D was previously specified as %d "
7853 "and may not be decreased", decl,
7854 DECL_ALIGN (decl) / BITS_PER_UNIT);
7855 else
7856 error ("alignment for %q+D must be at least %d", decl,
7857 DECL_ALIGN (decl) / BITS_PER_UNIT);
7858 *no_add_attrs = true;
7859 }
f8e93a2e 7860 else
7861 {
7cfdc2f0 7862 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
f8e93a2e 7863 DECL_USER_ALIGN (decl) = 1;
7864 }
7865
7866 return NULL_TREE;
7867}
7868
7869/* Handle a "weak" attribute; arguments as in
7870 struct attribute_spec.handler. */
7871
7872static tree
f948b309 7873handle_weak_attribute (tree *node, tree name,
9a03a746 7874 tree ARG_UNUSED (args),
7875 int ARG_UNUSED (flags),
7876 bool * ARG_UNUSED (no_add_attrs))
f8e93a2e 7877{
f948b309 7878 if (TREE_CODE (*node) == FUNCTION_DECL
059a60f3 7879 && DECL_DECLARED_INLINE_P (*node))
7880 {
0725e25c 7881 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
059a60f3 7882 *no_add_attrs = true;
7883 }
85c0a25c 7884 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7885 {
7886 error ("indirect function %q+D cannot be declared weak", *node);
7887 *no_add_attrs = true;
7888 return NULL_TREE;
7889 }
b443c459 7890 else if (VAR_OR_FUNCTION_DECL_P (*node))
9e6bcade 7891 declare_weak (*node);
f948b309 7892 else
7893 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7894
7895 return NULL_TREE;
7896}
7897
4641882f 7898/* Handle a "noplt" attribute; arguments as in
7899 struct attribute_spec.handler. */
7900
7901static tree
7902handle_noplt_attribute (tree *node, tree name,
7903 tree ARG_UNUSED (args),
7904 int ARG_UNUSED (flags),
7905 bool * ARG_UNUSED (no_add_attrs))
7906{
7907 if (TREE_CODE (*node) != FUNCTION_DECL)
7908 {
7909 warning (OPT_Wattributes,
7910 "%qE attribute is only applicable on functions", name);
7911 *no_add_attrs = true;
7912 return NULL_TREE;
7913 }
7914 return NULL_TREE;
7915}
7916
85c0a25c 7917/* Handle an "alias" or "ifunc" attribute; arguments as in
7918 struct attribute_spec.handler, except that IS_ALIAS tells us
7919 whether this is an alias as opposed to ifunc attribute. */
f8e93a2e 7920
7921static tree
85c0a25c 7922handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7923 bool *no_add_attrs)
f8e93a2e 7924{
7925 tree decl = *node;
7926
85c0a25c 7927 if (TREE_CODE (decl) != FUNCTION_DECL
f48c7f4a 7928 && (!is_alias || !VAR_P (decl)))
9e830260 7929 {
7930 warning (OPT_Wattributes, "%qE attribute ignored", name);
7931 *no_add_attrs = true;
7932 }
7933 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
48e1416a 7934 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 7935 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
7936 /* A static variable declaration is always a tentative definition,
7937 but the alias is a non-tentative definition which overrides. */
48e1416a 7938 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 7939 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
f8e93a2e 7940 {
85c0a25c 7941 error ("%q+D defined both normally and as %qE attribute", decl, name);
f8e93a2e 7942 *no_add_attrs = true;
85c0a25c 7943 return NULL_TREE;
f8e93a2e 7944 }
85c0a25c 7945 else if (!is_alias
7946 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
7947 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
7948 {
7949 error ("weak %q+D cannot be defined %qE", decl, name);
7950 *no_add_attrs = true;
7951 return NULL_TREE;
7952 }
8c42f0d9 7953
7954 /* Note that the very first time we process a nested declaration,
7955 decl_function_context will not be set. Indeed, *would* never
7956 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
7957 we do below. After such frobbery, pushdecl would set the context.
7958 In any case, this is never what we want. */
7959 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
f8e93a2e 7960 {
7961 tree id;
7962
7963 id = TREE_VALUE (args);
7964 if (TREE_CODE (id) != STRING_CST)
7965 {
85c0a25c 7966 error ("attribute %qE argument not a string", name);
f8e93a2e 7967 *no_add_attrs = true;
7968 return NULL_TREE;
7969 }
7970 id = get_identifier (TREE_STRING_POINTER (id));
7971 /* This counts as a use of the object pointed to. */
7972 TREE_USED (id) = 1;
7973
7974 if (TREE_CODE (decl) == FUNCTION_DECL)
7975 DECL_INITIAL (decl) = error_mark_node;
7976 else
f2526cce 7977 TREE_STATIC (decl) = 1;
85c0a25c 7978
7979 if (!is_alias)
7980 /* ifuncs are also aliases, so set that attribute too. */
7981 DECL_ATTRIBUTES (decl)
7982 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
f8e93a2e 7983 }
7984 else
7985 {
9b2d6d13 7986 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7987 *no_add_attrs = true;
7988 }
7989
8e857c41 7990 if (decl_in_symtab_p (*node))
7991 {
7992 struct symtab_node *n = symtab_node::get (decl);
7993 if (n && n->refuse_visibility_changes)
7994 {
7995 if (is_alias)
7996 error ("%+D declared alias after being used", decl);
7997 else
7998 error ("%+D declared ifunc after being used", decl);
7999 }
8000 }
8001
8002
f8e93a2e 8003 return NULL_TREE;
8004}
8005
85c0a25c 8006/* Handle an "alias" or "ifunc" attribute; arguments as in
8007 struct attribute_spec.handler. */
8008
8009static tree
8010handle_ifunc_attribute (tree *node, tree name, tree args,
8011 int ARG_UNUSED (flags), bool *no_add_attrs)
8012{
8013 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8014}
8015
8016/* Handle an "alias" or "ifunc" attribute; arguments as in
8017 struct attribute_spec.handler. */
8018
8019static tree
8020handle_alias_attribute (tree *node, tree name, tree args,
8021 int ARG_UNUSED (flags), bool *no_add_attrs)
8022{
8023 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8024}
8025
f4a30bd7 8026/* Handle a "weakref" attribute; arguments as in struct
8027 attribute_spec.handler. */
8028
8029static tree
8030handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8031 int flags, bool *no_add_attrs)
8032{
8033 tree attr = NULL_TREE;
8034
83852912 8035 /* We must ignore the attribute when it is associated with
8036 local-scoped decls, since attribute alias is ignored and many
8037 such symbols do not even have a DECL_WEAK field. */
64a7bd81 8038 if (decl_function_context (*node)
8039 || current_function_decl
b443c459 8040 || !VAR_OR_FUNCTION_DECL_P (*node))
83852912 8041 {
8042 warning (OPT_Wattributes, "%qE attribute ignored", name);
8043 *no_add_attrs = true;
8044 return NULL_TREE;
8045 }
8046
85c0a25c 8047 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8048 {
8049 error ("indirect function %q+D cannot be declared weakref", *node);
8050 *no_add_attrs = true;
8051 return NULL_TREE;
8052 }
8053
f4a30bd7 8054 /* The idea here is that `weakref("name")' mutates into `weakref,
8055 alias("name")', and weakref without arguments, in turn,
8056 implicitly adds weak. */
8057
8058 if (args)
8059 {
8060 attr = tree_cons (get_identifier ("alias"), args, attr);
8061 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8062
8063 *no_add_attrs = true;
0a3ecdc1 8064
8065 decl_attributes (node, attr, flags);
f4a30bd7 8066 }
8067 else
8068 {
8069 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
712d2297 8070 error_at (DECL_SOURCE_LOCATION (*node),
8071 "weakref attribute must appear before alias attribute");
f4a30bd7 8072
0a3ecdc1 8073 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8074 and that isn't supported; and because it wants to add it to
8075 the list of weak decls, which isn't helpful. */
8076 DECL_WEAK (*node) = 1;
f4a30bd7 8077 }
8078
8e857c41 8079 if (decl_in_symtab_p (*node))
8080 {
8081 struct symtab_node *n = symtab_node::get (*node);
8082 if (n && n->refuse_visibility_changes)
8083 error ("%+D declared weakref after being used", *node);
8084 }
8085
f4a30bd7 8086 return NULL_TREE;
8087}
8088
f8e93a2e 8089/* Handle an "visibility" attribute; arguments as in
8090 struct attribute_spec.handler. */
8091
8092static tree
1cae46be 8093handle_visibility_attribute (tree *node, tree name, tree args,
9a03a746 8094 int ARG_UNUSED (flags),
4a2849cb 8095 bool *ARG_UNUSED (no_add_attrs))
f8e93a2e 8096{
8097 tree decl = *node;
9c40570a 8098 tree id = TREE_VALUE (args);
4a2849cb 8099 enum symbol_visibility vis;
f8e93a2e 8100
b212f378 8101 if (TYPE_P (*node))
8102 {
4a2849cb 8103 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8104 /* OK */;
8e539fdc 8105 else if (!RECORD_OR_UNION_TYPE_P (*node))
4a2849cb 8106 {
8107 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8108 name);
8109 return NULL_TREE;
8110 }
8111 else if (TYPE_FIELDS (*node))
8112 {
8113 error ("%qE attribute ignored because %qT is already defined",
8114 name, *node);
8115 return NULL_TREE;
8116 }
b212f378 8117 }
84166705 8118 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
f8e93a2e 8119 {
9b2d6d13 8120 warning (OPT_Wattributes, "%qE attribute ignored", name);
9c40570a 8121 return NULL_TREE;
f8e93a2e 8122 }
f8e93a2e 8123
9c40570a 8124 if (TREE_CODE (id) != STRING_CST)
8125 {
07e3a3d2 8126 error ("visibility argument not a string");
9c40570a 8127 return NULL_TREE;
f8e93a2e 8128 }
b27ac6b5 8129
b212f378 8130 /* If this is a type, set the visibility on the type decl. */
8131 if (TYPE_P (decl))
8132 {
8133 decl = TYPE_NAME (decl);
84166705 8134 if (!decl)
a0c938f0 8135 return NULL_TREE;
e147aab3 8136 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8137 {
9b2d6d13 8138 warning (OPT_Wattributes, "%qE attribute ignored on types",
e147aab3 8139 name);
8140 return NULL_TREE;
8141 }
b212f378 8142 }
f8e93a2e 8143
9c40570a 8144 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
4a2849cb 8145 vis = VISIBILITY_DEFAULT;
9c40570a 8146 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
4a2849cb 8147 vis = VISIBILITY_INTERNAL;
9c40570a 8148 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
4a2849cb 8149 vis = VISIBILITY_HIDDEN;
9c40570a 8150 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
4a2849cb 8151 vis = VISIBILITY_PROTECTED;
9c40570a 8152 else
4a2849cb 8153 {
8154 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8155 vis = VISIBILITY_DEFAULT;
8156 }
8157
8158 if (DECL_VISIBILITY_SPECIFIED (decl)
098a01e7 8159 && vis != DECL_VISIBILITY (decl))
8160 {
8161 tree attributes = (TYPE_P (*node)
8162 ? TYPE_ATTRIBUTES (*node)
8163 : DECL_ATTRIBUTES (decl));
8164 if (lookup_attribute ("visibility", attributes))
8165 error ("%qD redeclared with different visibility", decl);
8166 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8167 && lookup_attribute ("dllimport", attributes))
8168 error ("%qD was declared %qs which implies default visibility",
8169 decl, "dllimport");
8170 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8171 && lookup_attribute ("dllexport", attributes))
8172 error ("%qD was declared %qs which implies default visibility",
8173 decl, "dllexport");
8174 }
4a2849cb 8175
8176 DECL_VISIBILITY (decl) = vis;
b212f378 8177 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8178
4a2849cb 8179 /* Go ahead and attach the attribute to the node as well. This is needed
8180 so we can determine whether we have VISIBILITY_DEFAULT because the
8181 visibility was not specified, or because it was explicitly overridden
8182 from the containing scope. */
9c40570a 8183
f8e93a2e 8184 return NULL_TREE;
8185}
8186
3aa0c315 8187/* Determine the ELF symbol visibility for DECL, which is either a
8188 variable or a function. It is an error to use this function if a
8189 definition of DECL is not available in this translation unit.
8190 Returns true if the final visibility has been determined by this
8191 function; false if the caller is free to make additional
8192 modifications. */
8193
8194bool
8195c_determine_visibility (tree decl)
8196{
b443c459 8197 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
3aa0c315 8198
8199 /* If the user explicitly specified the visibility with an
8200 attribute, honor that. DECL_VISIBILITY will have been set during
920f5a70 8201 the processing of the attribute. We check for an explicit
8202 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8203 to distinguish the use of an attribute from the use of a "#pragma
8204 GCC visibility push(...)"; in the latter case we still want other
8205 considerations to be able to overrule the #pragma. */
8206 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8207 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8208 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8209 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
3aa0c315 8210 return true;
8211
4a2849cb 8212 /* Set default visibility to whatever the user supplied with
8213 visibility_specified depending on #pragma GCC visibility. */
8214 if (!DECL_VISIBILITY_SPECIFIED (decl))
8215 {
2d9d8740 8216 if (visibility_options.inpragma
8217 || DECL_VISIBILITY (decl) != default_visibility)
8218 {
8219 DECL_VISIBILITY (decl) = default_visibility;
8220 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8221 /* If visibility changed and DECL already has DECL_RTL, ensure
8222 symbol flags are updated. */
f48c7f4a 8223 if (((VAR_P (decl) && TREE_STATIC (decl))
2d9d8740 8224 || TREE_CODE (decl) == FUNCTION_DECL)
8225 && DECL_RTL_SET_P (decl))
8226 make_decl_rtl (decl);
8227 }
4a2849cb 8228 }
3aa0c315 8229 return false;
8230}
8231
24dfead4 8232/* Handle an "tls_model" attribute; arguments as in
8233 struct attribute_spec.handler. */
8234
8235static tree
1cae46be 8236handle_tls_model_attribute (tree *node, tree name, tree args,
9a03a746 8237 int ARG_UNUSED (flags), bool *no_add_attrs)
24dfead4 8238{
1b53eb20 8239 tree id;
24dfead4 8240 tree decl = *node;
1b53eb20 8241 enum tls_model kind;
24dfead4 8242
1b53eb20 8243 *no_add_attrs = true;
8244
f48c7f4a 8245 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
24dfead4 8246 {
9b2d6d13 8247 warning (OPT_Wattributes, "%qE attribute ignored", name);
1b53eb20 8248 return NULL_TREE;
24dfead4 8249 }
24dfead4 8250
1b53eb20 8251 kind = DECL_TLS_MODEL (decl);
8252 id = TREE_VALUE (args);
8253 if (TREE_CODE (id) != STRING_CST)
8254 {
8255 error ("tls_model argument not a string");
8256 return NULL_TREE;
24dfead4 8257 }
8258
1b53eb20 8259 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8260 kind = TLS_MODEL_LOCAL_EXEC;
8261 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8262 kind = TLS_MODEL_INITIAL_EXEC;
8263 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8264 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8265 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8266 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8267 else
8268 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8269
5e68df57 8270 set_decl_tls_model (decl, kind);
24dfead4 8271 return NULL_TREE;
8272}
8273
f8e93a2e 8274/* Handle a "no_instrument_function" attribute; arguments as in
8275 struct attribute_spec.handler. */
8276
8277static tree
1cae46be 8278handle_no_instrument_function_attribute (tree *node, tree name,
9a03a746 8279 tree ARG_UNUSED (args),
8280 int ARG_UNUSED (flags),
09347743 8281 bool *no_add_attrs)
f8e93a2e 8282{
8283 tree decl = *node;
8284
8285 if (TREE_CODE (decl) != FUNCTION_DECL)
8286 {
712d2297 8287 error_at (DECL_SOURCE_LOCATION (decl),
8288 "%qE attribute applies only to functions", name);
f8e93a2e 8289 *no_add_attrs = true;
8290 }
f8e93a2e 8291 else
8292 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8293
8294 return NULL_TREE;
8295}
8296
8297/* Handle a "malloc" attribute; arguments as in
8298 struct attribute_spec.handler. */
8299
8300static tree
9a03a746 8301handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8302 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8303{
a5147fca 8304 if (TREE_CODE (*node) == FUNCTION_DECL
8305 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
f8e93a2e 8306 DECL_IS_MALLOC (*node) = 1;
f8e93a2e 8307 else
8308 {
9b2d6d13 8309 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8310 *no_add_attrs = true;
8311 }
8312
8313 return NULL_TREE;
8314}
8315
4a29c97c 8316/* Handle a "alloc_size" attribute; arguments as in
8317 struct attribute_spec.handler. */
8318
8319static tree
8320handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8321 int ARG_UNUSED (flags), bool *no_add_attrs)
8322{
2802826e 8323 unsigned arg_count = type_num_arguments (*node);
4a29c97c 8324 for (; args; args = TREE_CHAIN (args))
8325 {
8326 tree position = TREE_VALUE (args);
caf62483 8327 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8328 && TREE_CODE (position) != FUNCTION_DECL)
8329 position = default_conversion (position);
4a29c97c 8330
237e78b1 8331 if (!tree_fits_uhwi_p (position)
8332 || !arg_count
8333 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
4a29c97c 8334 {
48e1416a 8335 warning (OPT_Wattributes,
4a29c97c 8336 "alloc_size parameter outside range");
8337 *no_add_attrs = true;
8338 return NULL_TREE;
8339 }
8340 }
8341 return NULL_TREE;
8342}
8343
237e78b1 8344/* Handle a "alloc_align" attribute; arguments as in
8345 struct attribute_spec.handler. */
8346
8347static tree
8348handle_alloc_align_attribute (tree *node, tree, tree args, int,
8349 bool *no_add_attrs)
8350{
8351 unsigned arg_count = type_num_arguments (*node);
8352 tree position = TREE_VALUE (args);
8353 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8354 position = default_conversion (position);
8355
8356 if (!tree_fits_uhwi_p (position)
8357 || !arg_count
8358 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8359 {
8360 warning (OPT_Wattributes,
8361 "alloc_align parameter outside range");
8362 *no_add_attrs = true;
8363 return NULL_TREE;
8364 }
8365 return NULL_TREE;
8366}
8367
8368/* Handle a "assume_aligned" attribute; arguments as in
8369 struct attribute_spec.handler. */
8370
8371static tree
8372handle_assume_aligned_attribute (tree *, tree, tree args, int,
8373 bool *no_add_attrs)
8374{
8375 for (; args; args = TREE_CHAIN (args))
8376 {
8377 tree position = TREE_VALUE (args);
8378 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8379 && TREE_CODE (position) != FUNCTION_DECL)
8380 position = default_conversion (position);
8381
8382 if (TREE_CODE (position) != INTEGER_CST)
8383 {
8384 warning (OPT_Wattributes,
8385 "assume_aligned parameter not integer constant");
8386 *no_add_attrs = true;
8387 return NULL_TREE;
8388 }
8389 }
8390 return NULL_TREE;
8391}
8392
8ce86007 8393/* Handle a "fn spec" attribute; arguments as in
8394 struct attribute_spec.handler. */
8395
8396static tree
8397handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8398 tree args, int ARG_UNUSED (flags),
8399 bool *no_add_attrs ATTRIBUTE_UNUSED)
8400{
8401 gcc_assert (args
8402 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8403 && !TREE_CHAIN (args));
8404 return NULL_TREE;
8405}
8406
058a1b7a 8407/* Handle a "bnd_variable_size" attribute; arguments as in
8408 struct attribute_spec.handler. */
8409
8410static tree
8411handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8412 int ARG_UNUSED (flags), bool *no_add_attrs)
8413{
8414 if (TREE_CODE (*node) != FIELD_DECL)
8415 {
8416 warning (OPT_Wattributes, "%qE attribute ignored", name);
8417 *no_add_attrs = true;
8418 }
8419
8420 return NULL_TREE;
8421}
8422
8423/* Handle a "bnd_legacy" attribute; arguments as in
8424 struct attribute_spec.handler. */
8425
8426static tree
8427handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8428 int ARG_UNUSED (flags), bool *no_add_attrs)
8429{
8430 if (TREE_CODE (*node) != FUNCTION_DECL)
8431 {
8432 warning (OPT_Wattributes, "%qE attribute ignored", name);
8433 *no_add_attrs = true;
8434 }
8435
8436 return NULL_TREE;
8437}
8438
8439/* Handle a "bnd_instrument" attribute; arguments as in
8440 struct attribute_spec.handler. */
8441
8442static tree
8443handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8444 int ARG_UNUSED (flags), bool *no_add_attrs)
8445{
8446 if (TREE_CODE (*node) != FUNCTION_DECL)
8447 {
8448 warning (OPT_Wattributes, "%qE attribute ignored", name);
8449 *no_add_attrs = true;
8450 }
8451
8452 return NULL_TREE;
8453}
8454
a96c3cc1 8455/* Handle a "warn_unused" attribute; arguments as in
8456 struct attribute_spec.handler. */
8457
8458static tree
8459handle_warn_unused_attribute (tree *node, tree name,
8460 tree args ATTRIBUTE_UNUSED,
8461 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8462{
8463 if (TYPE_P (*node))
8464 /* Do nothing else, just set the attribute. We'll get at
8465 it later with lookup_attribute. */
8466 ;
8467 else
8468 {
8469 warning (OPT_Wattributes, "%qE attribute ignored", name);
8470 *no_add_attrs = true;
8471 }
8472
8473 return NULL_TREE;
8474}
8475
bc7bff74 8476/* Handle an "omp declare simd" attribute; arguments as in
8477 struct attribute_spec.handler. */
8478
8479static tree
8480handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8481{
8482 return NULL_TREE;
8483}
8484
c58a4cfd 8485/* Handle a "simd" attribute. */
8486
8487static tree
9841c6ee 8488handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
c58a4cfd 8489{
8490 if (TREE_CODE (*node) == FUNCTION_DECL)
8491 {
8492 if (lookup_attribute ("cilk simd function",
8493 DECL_ATTRIBUTES (*node)) != NULL)
8494 {
8495 error_at (DECL_SOURCE_LOCATION (*node),
8496 "%<__simd__%> attribute cannot be used in the same "
8497 "function marked as a Cilk Plus SIMD-enabled function");
8498 *no_add_attrs = true;
8499 }
8500 else
9841c6ee 8501 {
8502 tree t = get_identifier ("omp declare simd");
8503 tree attr = NULL_TREE;
8504 if (args)
8505 {
8506 tree id = TREE_VALUE (args);
8507
8508 if (TREE_CODE (id) != STRING_CST)
8509 {
8510 error ("attribute %qE argument not a string", name);
8511 *no_add_attrs = true;
8512 return NULL_TREE;
8513 }
8514
8515 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
8516 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8517 OMP_CLAUSE_NOTINBRANCH);
8518 else
8519 if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
8520 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8521 OMP_CLAUSE_INBRANCH);
8522 else
8523 {
8524 error ("only %<inbranch%> and %<notinbranch%> flags are "
8525 "allowed for %<__simd__%> attribute");
8526 *no_add_attrs = true;
8527 return NULL_TREE;
8528 }
8529 }
8530
8531 DECL_ATTRIBUTES (*node) = tree_cons (t,
8532 build_tree_list (NULL_TREE,
8533 attr),
8534 DECL_ATTRIBUTES (*node));
8535 }
c58a4cfd 8536 }
8537 else
8538 {
8539 warning (OPT_Wattributes, "%qE attribute ignored", name);
8540 *no_add_attrs = true;
8541 }
8542
8543 return NULL_TREE;
8544}
8545
bc7bff74 8546/* Handle an "omp declare target" attribute; arguments as in
8547 struct attribute_spec.handler. */
8548
8549static tree
8550handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8551{
8552 return NULL_TREE;
8553}
8554
26d1c5ff 8555/* Handle a "returns_twice" attribute; arguments as in
8556 struct attribute_spec.handler. */
8557
8558static tree
8559handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8560 int ARG_UNUSED (flags), bool *no_add_attrs)
8561{
8562 if (TREE_CODE (*node) == FUNCTION_DECL)
8563 DECL_IS_RETURNS_TWICE (*node) = 1;
8564 else
8565 {
9b2d6d13 8566 warning (OPT_Wattributes, "%qE attribute ignored", name);
26d1c5ff 8567 *no_add_attrs = true;
8568 }
8569
8570 return NULL_TREE;
8571}
8572
f8e93a2e 8573/* Handle a "no_limit_stack" attribute; arguments as in
8574 struct attribute_spec.handler. */
8575
8576static tree
1cae46be 8577handle_no_limit_stack_attribute (tree *node, tree name,
9a03a746 8578 tree ARG_UNUSED (args),
8579 int ARG_UNUSED (flags),
09347743 8580 bool *no_add_attrs)
f8e93a2e 8581{
8582 tree decl = *node;
8583
8584 if (TREE_CODE (decl) != FUNCTION_DECL)
8585 {
712d2297 8586 error_at (DECL_SOURCE_LOCATION (decl),
8587 "%qE attribute applies only to functions", name);
f8e93a2e 8588 *no_add_attrs = true;
8589 }
8590 else if (DECL_INITIAL (decl))
8591 {
712d2297 8592 error_at (DECL_SOURCE_LOCATION (decl),
8593 "can%'t set %qE attribute after definition", name);
f8e93a2e 8594 *no_add_attrs = true;
8595 }
8596 else
8597 DECL_NO_LIMIT_STACK (decl) = 1;
8598
8599 return NULL_TREE;
8600}
8601
8602/* Handle a "pure" attribute; arguments as in
8603 struct attribute_spec.handler. */
8604
8605static tree
9a03a746 8606handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8607 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8608{
8609 if (TREE_CODE (*node) == FUNCTION_DECL)
9c2a0c05 8610 DECL_PURE_P (*node) = 1;
f8e93a2e 8611 /* ??? TODO: Support types. */
8612 else
8613 {
9b2d6d13 8614 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8615 *no_add_attrs = true;
8616 }
8617
8618 return NULL_TREE;
8619}
8620
4c0315d0 8621/* Digest an attribute list destined for a transactional memory statement.
8622 ALLOWED is the set of attributes that are allowed for this statement;
8623 return the attribute we parsed. Multiple attributes are never allowed. */
8624
8625int
8626parse_tm_stmt_attr (tree attrs, int allowed)
8627{
8628 tree a_seen = NULL;
8629 int m_seen = 0;
8630
8631 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8632 {
8633 tree a = TREE_PURPOSE (attrs);
8634 int m = 0;
8635
8636 if (is_attribute_p ("outer", a))
8637 m = TM_STMT_ATTR_OUTER;
8638
8639 if ((m & allowed) == 0)
8640 {
8641 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8642 continue;
8643 }
8644
8645 if (m_seen == 0)
8646 {
8647 a_seen = a;
8648 m_seen = m;
8649 }
8650 else if (m_seen == m)
8651 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8652 else
8653 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8654 }
8655
8656 return m_seen;
8657}
8658
8659/* Transform a TM attribute name into a maskable integer and back.
8660 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8661 to how the lack of an attribute is treated. */
8662
8663int
8664tm_attr_to_mask (tree attr)
8665{
8666 if (attr == NULL)
8667 return 0;
8668 if (is_attribute_p ("transaction_safe", attr))
8669 return TM_ATTR_SAFE;
8670 if (is_attribute_p ("transaction_callable", attr))
8671 return TM_ATTR_CALLABLE;
8672 if (is_attribute_p ("transaction_pure", attr))
8673 return TM_ATTR_PURE;
8674 if (is_attribute_p ("transaction_unsafe", attr))
8675 return TM_ATTR_IRREVOCABLE;
8676 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8677 return TM_ATTR_MAY_CANCEL_OUTER;
8678 return 0;
8679}
8680
8681tree
8682tm_mask_to_attr (int mask)
8683{
8684 const char *str;
8685 switch (mask)
8686 {
8687 case TM_ATTR_SAFE:
8688 str = "transaction_safe";
8689 break;
8690 case TM_ATTR_CALLABLE:
8691 str = "transaction_callable";
8692 break;
8693 case TM_ATTR_PURE:
8694 str = "transaction_pure";
8695 break;
8696 case TM_ATTR_IRREVOCABLE:
8697 str = "transaction_unsafe";
8698 break;
8699 case TM_ATTR_MAY_CANCEL_OUTER:
8700 str = "transaction_may_cancel_outer";
8701 break;
8702 default:
8703 gcc_unreachable ();
8704 }
8705 return get_identifier (str);
8706}
8707
8708/* Return the first TM attribute seen in LIST. */
8709
8710tree
8711find_tm_attribute (tree list)
8712{
8713 for (; list ; list = TREE_CHAIN (list))
8714 {
8715 tree name = TREE_PURPOSE (list);
8716 if (tm_attr_to_mask (name) != 0)
8717 return name;
8718 }
8719 return NULL_TREE;
8720}
8721
8722/* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8723 Here we accept only function types, and verify that none of the other
8724 function TM attributes are also applied. */
8725/* ??? We need to accept class types for C++, but not C. This greatly
8726 complicates this function, since we can no longer rely on the extra
8727 processing given by function_type_required. */
8728
8729static tree
8730handle_tm_attribute (tree *node, tree name, tree args,
8731 int flags, bool *no_add_attrs)
8732{
8733 /* Only one path adds the attribute; others don't. */
8734 *no_add_attrs = true;
8735
8736 switch (TREE_CODE (*node))
8737 {
8738 case RECORD_TYPE:
8739 case UNION_TYPE:
8740 /* Only tm_callable and tm_safe apply to classes. */
8741 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8742 goto ignored;
8743 /* FALLTHRU */
8744
8745 case FUNCTION_TYPE:
8746 case METHOD_TYPE:
8747 {
8748 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8749 if (old_name == name)
8750 ;
8751 else if (old_name != NULL_TREE)
8752 error ("type was previously declared %qE", old_name);
8753 else
8754 *no_add_attrs = false;
8755 }
8756 break;
8757
6d02e6b2 8758 case FUNCTION_DECL:
8759 {
8760 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
8761 want to set transaction_safe on the type. */
8762 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
8763 if (!TYPE_P (DECL_CONTEXT (*node)))
8764 error_at (DECL_SOURCE_LOCATION (*node),
8765 "%<transaction_safe_dynamic%> may only be specified for "
8766 "a virtual function");
8767 *no_add_attrs = false;
8768 decl_attributes (&TREE_TYPE (*node),
8769 build_tree_list (get_identifier ("transaction_safe"),
8770 NULL_TREE),
8771 0);
8772 break;
8773 }
8774
4c0315d0 8775 case POINTER_TYPE:
8776 {
8777 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8778 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8779 {
8780 tree fn_tmp = TREE_TYPE (*node);
8781 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8782 *node = build_pointer_type (fn_tmp);
8783 break;
8784 }
8785 }
8786 /* FALLTHRU */
8787
8788 default:
8789 /* If a function is next, pass it on to be tried next. */
8790 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8791 return tree_cons (name, args, NULL);
8792
8793 ignored:
8794 warning (OPT_Wattributes, "%qE attribute ignored", name);
8795 break;
8796 }
8797
8798 return NULL_TREE;
8799}
8800
8801/* Handle the TM_WRAP attribute; arguments as in
8802 struct attribute_spec.handler. */
8803
8804static tree
8805handle_tm_wrap_attribute (tree *node, tree name, tree args,
8806 int ARG_UNUSED (flags), bool *no_add_attrs)
8807{
8808 tree decl = *node;
8809
8810 /* We don't need the attribute even on success, since we
8811 record the entry in an external table. */
8812 *no_add_attrs = true;
8813
8814 if (TREE_CODE (decl) != FUNCTION_DECL)
8815 warning (OPT_Wattributes, "%qE attribute ignored", name);
8816 else
8817 {
8818 tree wrap_decl = TREE_VALUE (args);
3e5a8b00 8819 if (error_operand_p (wrap_decl))
8820 ;
8821 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
b443c459 8822 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
4c0315d0 8823 error ("%qE argument not an identifier", name);
8824 else
8825 {
8826 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8827 wrap_decl = lookup_name (wrap_decl);
8828 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8829 {
8830 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8831 TREE_TYPE (wrap_decl)))
8832 record_tm_replacement (wrap_decl, decl);
8833 else
8834 error ("%qD is not compatible with %qD", wrap_decl, decl);
8835 }
8836 else
cdf34fca 8837 error ("%qE argument is not a function", name);
4c0315d0 8838 }
8839 }
8840
8841 return NULL_TREE;
8842}
8843
8844/* Ignore the given attribute. Used when this attribute may be usefully
8845 overridden by the target, but is not used generically. */
8846
8847static tree
8848ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8849 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8850 bool *no_add_attrs)
8851{
8852 *no_add_attrs = true;
8853 return NULL_TREE;
8854}
8855
fc09b200 8856/* Handle a "no vops" attribute; arguments as in
8857 struct attribute_spec.handler. */
8858
8859static tree
8860handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8861 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8862 bool *ARG_UNUSED (no_add_attrs))
8863{
8864 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8865 DECL_IS_NOVOPS (*node) = 1;
8866 return NULL_TREE;
8867}
8868
f8e93a2e 8869/* Handle a "deprecated" attribute; arguments as in
8870 struct attribute_spec.handler. */
1cae46be 8871
f8e93a2e 8872static tree
1cae46be 8873handle_deprecated_attribute (tree *node, tree name,
45c4e798 8874 tree args, int flags,
09347743 8875 bool *no_add_attrs)
f8e93a2e 8876{
8877 tree type = NULL_TREE;
8878 int warn = 0;
782858b8 8879 tree what = NULL_TREE;
1cae46be 8880
45c4e798 8881 if (!args)
8882 *no_add_attrs = true;
8883 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8884 {
8885 error ("deprecated message is not a string");
8886 *no_add_attrs = true;
8887 }
8888
f8e93a2e 8889 if (DECL_P (*node))
8890 {
8891 tree decl = *node;
8892 type = TREE_TYPE (decl);
1cae46be 8893
f8e93a2e 8894 if (TREE_CODE (decl) == TYPE_DECL
8895 || TREE_CODE (decl) == PARM_DECL
b443c459 8896 || VAR_OR_FUNCTION_DECL_P (decl)
40c8d1dd 8897 || TREE_CODE (decl) == FIELD_DECL
5a4c69dd 8898 || TREE_CODE (decl) == CONST_DECL
40c8d1dd 8899 || objc_method_decl (TREE_CODE (decl)))
f8e93a2e 8900 TREE_DEPRECATED (decl) = 1;
8901 else
8902 warn = 1;
8903 }
8904 else if (TYPE_P (*node))
8905 {
8906 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 8907 *node = build_variant_type_copy (*node);
f8e93a2e 8908 TREE_DEPRECATED (*node) = 1;
8909 type = *node;
8910 }
8911 else
8912 warn = 1;
1cae46be 8913
f8e93a2e 8914 if (warn)
8915 {
8916 *no_add_attrs = true;
8917 if (type && TYPE_NAME (type))
8918 {
8919 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
782858b8 8920 what = TYPE_NAME (*node);
f8e93a2e 8921 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
8922 && DECL_NAME (TYPE_NAME (type)))
782858b8 8923 what = DECL_NAME (TYPE_NAME (type));
f8e93a2e 8924 }
8925 if (what)
9b2d6d13 8926 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
f8e93a2e 8927 else
9b2d6d13 8928 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8929 }
8930
8931 return NULL_TREE;
8932}
8933
f8e93a2e 8934/* Handle a "vector_size" attribute; arguments as in
8935 struct attribute_spec.handler. */
8936
8937static tree
1cae46be 8938handle_vector_size_attribute (tree *node, tree name, tree args,
9a03a746 8939 int ARG_UNUSED (flags),
09347743 8940 bool *no_add_attrs)
f8e93a2e 8941{
8942 unsigned HOST_WIDE_INT vecsize, nunits;
3754d046 8943 machine_mode orig_mode;
4917c376 8944 tree type = *node, new_type, size;
f8e93a2e 8945
8946 *no_add_attrs = true;
8947
4917c376 8948 size = TREE_VALUE (args);
3e5a8b00 8949 if (size && TREE_CODE (size) != IDENTIFIER_NODE
8950 && TREE_CODE (size) != FUNCTION_DECL)
caf62483 8951 size = default_conversion (size);
4917c376 8952
e913b5cd 8953 if (!tree_fits_uhwi_p (size))
f8e93a2e 8954 {
9b2d6d13 8955 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8956 return NULL_TREE;
8957 }
8958
8959 /* Get the vector size (in bytes). */
e913b5cd 8960 vecsize = tree_to_uhwi (size);
f8e93a2e 8961
8962 /* We need to provide for vector pointers, vector arrays, and
8963 functions returning vectors. For example:
8964
8965 __attribute__((vector_size(16))) short *foo;
8966
8967 In this case, the mode is SI, but the type being modified is
8968 HI, so we need to look further. */
8969
8970 while (POINTER_TYPE_P (type)
8971 || TREE_CODE (type) == FUNCTION_TYPE
5bfb0742 8972 || TREE_CODE (type) == METHOD_TYPE
2cb0e5d9 8973 || TREE_CODE (type) == ARRAY_TYPE
8974 || TREE_CODE (type) == OFFSET_TYPE)
f8e93a2e 8975 type = TREE_TYPE (type);
8976
8977 /* Get the mode of the type being modified. */
8978 orig_mode = TYPE_MODE (type);
8979
2cb0e5d9 8980 if ((!INTEGRAL_TYPE_P (type)
8981 && !SCALAR_FLOAT_TYPE_P (type)
8982 && !FIXED_POINT_TYPE_P (type))
cee7491d 8983 || (!SCALAR_FLOAT_MODE_P (orig_mode)
9421ebb9 8984 && GET_MODE_CLASS (orig_mode) != MODE_INT
8985 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
e913b5cd 8986 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7ec31215 8987 || TREE_CODE (type) == BOOLEAN_TYPE)
f8e93a2e 8988 {
782858b8 8989 error ("invalid vector type for attribute %qE", name);
f8e93a2e 8990 return NULL_TREE;
8991 }
8992
e913b5cd 8993 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
39cc3e6d 8994 {
8995 error ("vector size not an integral multiple of component size");
8996 return NULL;
8997 }
8998
8999 if (vecsize == 0)
9000 {
9001 error ("zero vector size");
9002 return NULL;
9003 }
9004
f8e93a2e 9005 /* Calculate how many units fit in the vector. */
e913b5cd 9006 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
83e2a11b 9007 if (nunits & (nunits - 1))
f8e93a2e 9008 {
83e2a11b 9009 error ("number of components of the vector not a power of two");
f8e93a2e 9010 return NULL_TREE;
9011 }
9012
83e2a11b 9013 new_type = build_vector_type (type, nunits);
f8e93a2e 9014
9015 /* Build back pointers if needed. */
d991e6e8 9016 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
f8e93a2e 9017
9018 return NULL_TREE;
9019}
9020
dbf6c367 9021/* Handle the "nonnull" attribute. */
9022static tree
9a03a746 9023handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9024 tree args, int ARG_UNUSED (flags),
09347743 9025 bool *no_add_attrs)
dbf6c367 9026{
9027 tree type = *node;
9028 unsigned HOST_WIDE_INT attr_arg_num;
9029
9030 /* If no arguments are specified, all pointer arguments should be
d716ce75 9031 non-null. Verify a full prototype is given so that the arguments
dbf6c367 9032 will have the correct types when we actually check them later. */
84166705 9033 if (!args)
dbf6c367 9034 {
a36cf284 9035 if (!prototype_p (type))
dbf6c367 9036 {
9037 error ("nonnull attribute without arguments on a non-prototype");
4ee9c684 9038 *no_add_attrs = true;
dbf6c367 9039 }
9040 return NULL_TREE;
9041 }
9042
9043 /* Argument list specified. Verify that each argument number references
9044 a pointer argument. */
caf62483 9045 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
dbf6c367 9046 {
4ee9c684 9047 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
dbf6c367 9048
caf62483 9049 tree arg = TREE_VALUE (args);
9050 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9051 && TREE_CODE (arg) != FUNCTION_DECL)
9052 arg = default_conversion (arg);
9053
9054 if (!get_nonnull_operand (arg, &arg_num))
dbf6c367 9055 {
07e3a3d2 9056 error ("nonnull argument has invalid operand number (argument %lu)",
dbf6c367 9057 (unsigned long) attr_arg_num);
9058 *no_add_attrs = true;
9059 return NULL_TREE;
9060 }
9061
d0af78c5 9062 if (prototype_p (type))
dbf6c367 9063 {
d0af78c5 9064 function_args_iterator iter;
9065 tree argument;
9066
9067 function_args_iter_init (&iter, type);
9068 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
dbf6c367 9069 {
d0af78c5 9070 argument = function_args_iter_cond (&iter);
9071 if (argument == NULL_TREE || ck_num == arg_num)
dbf6c367 9072 break;
dbf6c367 9073 }
9074
84166705 9075 if (!argument
d0af78c5 9076 || TREE_CODE (argument) == VOID_TYPE)
dbf6c367 9077 {
07e3a3d2 9078 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
dbf6c367 9079 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9080 *no_add_attrs = true;
9081 return NULL_TREE;
9082 }
9083
d0af78c5 9084 if (TREE_CODE (argument) != POINTER_TYPE)
dbf6c367 9085 {
07e3a3d2 9086 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
dbf6c367 9087 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9088 *no_add_attrs = true;
9089 return NULL_TREE;
9090 }
9091 }
9092 }
9093
9094 return NULL_TREE;
9095}
9096
9097/* Check the argument list of a function call for null in argument slots
d01f58f9 9098 that are marked as requiring a non-null pointer argument. The NARGS
bda05c57 9099 arguments are passed in the array ARGARRAY. */
dbf6c367 9100
9101static void
bda05c57 9102check_function_nonnull (location_t loc, tree attrs, int nargs, tree *argarray)
dbf6c367 9103{
9ca77b08 9104 tree a;
d01f58f9 9105 int i;
dbf6c367 9106
9ca77b08 9107 attrs = lookup_attribute ("nonnull", attrs);
9108 if (attrs == NULL_TREE)
9109 return;
9110
9111 a = attrs;
9112 /* See if any of the nonnull attributes has no arguments. If so,
9113 then every pointer argument is checked (in which case the check
9114 for pointer type is done in check_nonnull_arg). */
9115 if (TREE_VALUE (a) != NULL_TREE)
9116 do
9117 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9118 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9119
9120 if (a != NULL_TREE)
9121 for (i = 0; i < nargs; i++)
bda05c57 9122 check_function_arguments_recurse (check_nonnull_arg, &loc, argarray[i],
9ca77b08 9123 i + 1);
9124 else
dbf6c367 9125 {
9ca77b08 9126 /* Walk the argument list. If we encounter an argument number we
9127 should check for non-null, do it. */
9128 for (i = 0; i < nargs; i++)
dbf6c367 9129 {
9ca77b08 9130 for (a = attrs; ; a = TREE_CHAIN (a))
4ee9c684 9131 {
9ca77b08 9132 a = lookup_attribute ("nonnull", a);
9133 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9134 break;
4ee9c684 9135 }
9ca77b08 9136
9137 if (a != NULL_TREE)
bda05c57 9138 check_function_arguments_recurse (check_nonnull_arg, &loc,
9ca77b08 9139 argarray[i], i + 1);
dbf6c367 9140 }
9141 }
9142}
9143
50ca527f 9144/* Check that the Nth argument of a function call (counting backwards
d01f58f9 9145 from the end) is a (pointer)0. The NARGS arguments are passed in the
9146 array ARGARRAY. */
bf6c8de0 9147
9148static void
774e9d58 9149check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
bf6c8de0 9150{
774e9d58 9151 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
bf6c8de0 9152
9153 if (attr)
9154 {
d01f58f9 9155 int len = 0;
9156 int pos = 0;
9157 tree sentinel;
774e9d58 9158 function_args_iterator iter;
9159 tree t;
a0c938f0 9160
d01f58f9 9161 /* Skip over the named arguments. */
774e9d58 9162 FOREACH_FUNCTION_ARGS (fntype, t, iter)
a0c938f0 9163 {
774e9d58 9164 if (len == nargs)
9165 break;
d01f58f9 9166 len++;
9167 }
50ca527f 9168
d01f58f9 9169 if (TREE_VALUE (attr))
9170 {
9171 tree p = TREE_VALUE (TREE_VALUE (attr));
f9ae6f95 9172 pos = TREE_INT_CST_LOW (p);
d01f58f9 9173 }
50ca527f 9174
d01f58f9 9175 /* The sentinel must be one of the varargs, i.e.
9176 in position >= the number of fixed arguments. */
9177 if ((nargs - 1 - pos) < len)
9178 {
77a357e3 9179 warning (OPT_Wformat_,
d01f58f9 9180 "not enough variable arguments to fit a sentinel");
9181 return;
bf6c8de0 9182 }
d01f58f9 9183
9184 /* Validate the sentinel. */
9185 sentinel = argarray[nargs - 1 - pos];
9186 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9187 || !integer_zerop (sentinel))
9188 /* Although __null (in C++) is only an integer we allow it
9189 nevertheless, as we are guaranteed that it's exactly
9190 as wide as a pointer, and we don't want to force
9191 users to cast the NULL they have written there.
9192 We warn with -Wstrict-null-sentinel, though. */
9193 && (warn_strict_null_sentinel || null_node != sentinel))
77a357e3 9194 warning (OPT_Wformat_, "missing sentinel in function call");
bf6c8de0 9195 }
9196}
9197
dbf6c367 9198/* Helper for check_function_nonnull; given a list of operands which
9199 must be non-null in ARGS, determine if operand PARAM_NUM should be
9200 checked. */
9201
9202static bool
1cae46be 9203nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
dbf6c367 9204{
4ee9c684 9205 unsigned HOST_WIDE_INT arg_num = 0;
dbf6c367 9206
9207 for (; args; args = TREE_CHAIN (args))
9208 {
231bd014 9209 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9210
9211 gcc_assert (found);
dbf6c367 9212
9213 if (arg_num == param_num)
9214 return true;
9215 }
9216 return false;
9217}
9218
9219/* Check that the function argument PARAM (which is operand number
9220 PARAM_NUM) is non-null. This is called by check_function_nonnull
9221 via check_function_arguments_recurse. */
9222
9223static void
bda05c57 9224check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
dbf6c367 9225{
bda05c57 9226 location_t *ploc = (location_t *) ctx;
9227
dbf6c367 9228 /* Just skip checking the argument if it's not a pointer. This can
9229 happen if the "nonnull" attribute was given without an operand
9230 list (which means to check every pointer argument). */
9231
9232 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9233 return;
9234
9235 if (integer_zerop (param))
bda05c57 9236 warning_at (*ploc, OPT_Wnonnull, "null argument where non-null required "
9237 "(argument %lu)", (unsigned long) param_num);
dbf6c367 9238}
9239
9240/* Helper for nonnull attribute handling; fetch the operand number
9241 from the attribute argument list. */
9242
9243static bool
1cae46be 9244get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
dbf6c367 9245{
e913b5cd 9246 /* Verify the arg number is a small constant. */
e1d65c9f 9247 if (tree_fits_uhwi_p (arg_num_expr))
e913b5cd 9248 {
f9ae6f95 9249 *valp = TREE_INT_CST_LOW (arg_num_expr);
e913b5cd 9250 return true;
9251 }
9252 else
dbf6c367 9253 return false;
dbf6c367 9254}
fa987697 9255
9256/* Handle a "nothrow" attribute; arguments as in
9257 struct attribute_spec.handler. */
9258
9259static tree
9a03a746 9260handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9261 int ARG_UNUSED (flags), bool *no_add_attrs)
fa987697 9262{
9263 if (TREE_CODE (*node) == FUNCTION_DECL)
9264 TREE_NOTHROW (*node) = 1;
9265 /* ??? TODO: Support types. */
9266 else
9267 {
9b2d6d13 9268 warning (OPT_Wattributes, "%qE attribute ignored", name);
fa987697 9269 *no_add_attrs = true;
9270 }
9271
9272 return NULL_TREE;
9273}
7acb29a3 9274
9275/* Handle a "cleanup" attribute; arguments as in
9276 struct attribute_spec.handler. */
9277
9278static tree
1cae46be 9279handle_cleanup_attribute (tree *node, tree name, tree args,
9a03a746 9280 int ARG_UNUSED (flags), bool *no_add_attrs)
7acb29a3 9281{
9282 tree decl = *node;
9283 tree cleanup_id, cleanup_decl;
9284
9285 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9286 for global destructors in C++. This requires infrastructure that
9287 we don't have generically at the moment. It's also not a feature
9288 we'd be missing too much, since we do have attribute constructor. */
f48c7f4a 9289 if (!VAR_P (decl) || TREE_STATIC (decl))
7acb29a3 9290 {
9b2d6d13 9291 warning (OPT_Wattributes, "%qE attribute ignored", name);
7acb29a3 9292 *no_add_attrs = true;
9293 return NULL_TREE;
9294 }
9295
9296 /* Verify that the argument is a function in scope. */
9297 /* ??? We could support pointers to functions here as well, if
9298 that was considered desirable. */
9299 cleanup_id = TREE_VALUE (args);
9300 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9301 {
07e3a3d2 9302 error ("cleanup argument not an identifier");
7acb29a3 9303 *no_add_attrs = true;
9304 return NULL_TREE;
9305 }
d1c41717 9306 cleanup_decl = lookup_name (cleanup_id);
7acb29a3 9307 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9308 {
07e3a3d2 9309 error ("cleanup argument not a function");
7acb29a3 9310 *no_add_attrs = true;
9311 return NULL_TREE;
9312 }
9313
1cae46be 9314 /* That the function has proper type is checked with the
7acb29a3 9315 eventual call to build_function_call. */
9316
9317 return NULL_TREE;
9318}
8a8cdb8d 9319
9320/* Handle a "warn_unused_result" attribute. No special handling. */
9321
9322static tree
9323handle_warn_unused_result_attribute (tree *node, tree name,
9a03a746 9324 tree ARG_UNUSED (args),
9325 int ARG_UNUSED (flags), bool *no_add_attrs)
8a8cdb8d 9326{
9327 /* Ignore the attribute for functions not returning any value. */
9328 if (VOID_TYPE_P (TREE_TYPE (*node)))
9329 {
9b2d6d13 9330 warning (OPT_Wattributes, "%qE attribute ignored", name);
8a8cdb8d 9331 *no_add_attrs = true;
9332 }
9333
9334 return NULL_TREE;
9335}
bf6c8de0 9336
9337/* Handle a "sentinel" attribute. */
9338
9339static tree
50ca527f 9340handle_sentinel_attribute (tree *node, tree name, tree args,
bf6c8de0 9341 int ARG_UNUSED (flags), bool *no_add_attrs)
9342{
a36cf284 9343 if (!prototype_p (*node))
bf6c8de0 9344 {
9b2d6d13 9345 warning (OPT_Wattributes,
9346 "%qE attribute requires prototypes with named arguments", name);
bf6c8de0 9347 *no_add_attrs = true;
bf6c8de0 9348 }
50ca527f 9349 else
9350 {
c33080b9 9351 if (!stdarg_p (*node))
a0c938f0 9352 {
9b2d6d13 9353 warning (OPT_Wattributes,
9354 "%qE attribute only applies to variadic functions", name);
50ca527f 9355 *no_add_attrs = true;
9356 }
9357 }
a0c938f0 9358
50ca527f 9359 if (args)
bf6c8de0 9360 {
50ca527f 9361 tree position = TREE_VALUE (args);
3e5a8b00 9362 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9363 && TREE_CODE (position) != FUNCTION_DECL)
9364 position = default_conversion (position);
50ca527f 9365
3e5a8b00 9366 if (TREE_CODE (position) != INTEGER_CST
9367 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
a0c938f0 9368 {
48e1416a 9369 warning (OPT_Wattributes,
01b54db5 9370 "requested position is not an integer constant");
50ca527f 9371 *no_add_attrs = true;
9372 }
9373 else
a0c938f0 9374 {
50ca527f 9375 if (tree_int_cst_lt (position, integer_zero_node))
9376 {
01b54db5 9377 warning (OPT_Wattributes,
9378 "requested position is less than zero");
50ca527f 9379 *no_add_attrs = true;
9380 }
9381 }
bf6c8de0 9382 }
a0c938f0 9383
bf6c8de0 9384 return NULL_TREE;
9385}
b5c26b42 9386
9387/* Handle a "type_generic" attribute. */
9388
9389static tree
9390handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9391 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9392 bool * ARG_UNUSED (no_add_attrs))
9393{
19fbe3a4 9394 /* Ensure we have a function type. */
9395 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
48e1416a 9396
19fbe3a4 9397 /* Ensure we have a variadic function. */
c33080b9 9398 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
b5c26b42 9399
9400 return NULL_TREE;
9401}
46f8e3b0 9402
24470055 9403/* Handle a "target" attribute. */
46f8e3b0 9404
9405static tree
24470055 9406handle_target_attribute (tree *node, tree name, tree args, int flags,
46f8e3b0 9407 bool *no_add_attrs)
9408{
9409 /* Ensure we have a function type. */
9410 if (TREE_CODE (*node) != FUNCTION_DECL)
9411 {
9412 warning (OPT_Wattributes, "%qE attribute ignored", name);
9413 *no_add_attrs = true;
9414 }
ab50af2a 9415 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
9416 {
9417 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9418 "with %qs attribute", name, "target_clones");
9419 *no_add_attrs = true;
9420 }
46f8e3b0 9421 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
ae0c3984 9422 flags))
46f8e3b0 9423 *no_add_attrs = true;
9424
9425 return NULL_TREE;
9426}
9427
ab50af2a 9428/* Handle a "target_clones" attribute. */
9429
9430static tree
9431handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9432 int ARG_UNUSED (flags), bool *no_add_attrs)
9433{
9434 /* Ensure we have a function type. */
9435 if (TREE_CODE (*node) == FUNCTION_DECL)
9436 {
9437 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
9438 {
9439 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9440 "with %qs attribute", name, "always_inline");
9441 *no_add_attrs = true;
9442 }
9443 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
9444 {
9445 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9446 "with %qs attribute", name, "target");
9447 *no_add_attrs = true;
9448 }
9449 else
9450 /* Do not inline functions with multiple clone targets. */
9451 DECL_UNINLINABLE (*node) = 1;
9452 }
9453 else
9454 {
9455 warning (OPT_Wattributes, "%qE attribute ignored", name);
9456 *no_add_attrs = true;
9457 }
9458 return NULL_TREE;
9459}
9460
46f8e3b0 9461/* Arguments being collected for optimization. */
9462typedef const char *const_char_p; /* For DEF_VEC_P. */
f1f41a6c 9463static GTY(()) vec<const_char_p, va_gc> *optimize_args;
46f8e3b0 9464
9465
9466/* Inner function to convert a TREE_LIST to argv string to parse the optimize
9467 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9468 false for #pragma GCC optimize. */
9469
9470bool
9471parse_optimize_options (tree args, bool attr_p)
9472{
9473 bool ret = true;
9474 unsigned opt_argc;
9475 unsigned i;
9476 const char **opt_argv;
615ef0bb 9477 struct cl_decoded_option *decoded_options;
9478 unsigned int decoded_options_count;
46f8e3b0 9479 tree ap;
9480
9481 /* Build up argv vector. Just in case the string is stored away, use garbage
9482 collected strings. */
f1f41a6c 9483 vec_safe_truncate (optimize_args, 0);
9484 vec_safe_push (optimize_args, (const char *) NULL);
46f8e3b0 9485
9486 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9487 {
9488 tree value = TREE_VALUE (ap);
9489
9490 if (TREE_CODE (value) == INTEGER_CST)
9491 {
9492 char buffer[20];
f9ae6f95 9493 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
f1f41a6c 9494 vec_safe_push (optimize_args, ggc_strdup (buffer));
46f8e3b0 9495 }
9496
9497 else if (TREE_CODE (value) == STRING_CST)
9498 {
9499 /* Split string into multiple substrings. */
9500 size_t len = TREE_STRING_LENGTH (value);
9501 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9502 char *end = p + len;
9503 char *comma;
9504 char *next_p = p;
9505
9506 while (next_p != NULL)
9507 {
9508 size_t len2;
9509 char *q, *r;
9510
9511 p = next_p;
9512 comma = strchr (p, ',');
9513 if (comma)
9514 {
9515 len2 = comma - p;
9516 *comma = '\0';
9517 next_p = comma+1;
9518 }
9519 else
9520 {
9521 len2 = end - p;
9522 next_p = NULL;
9523 }
9524
ba72912a 9525 r = q = (char *) ggc_alloc_atomic (len2 + 3);
46f8e3b0 9526
9527 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9528 options. */
9529 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9530 {
9531 ret = false;
9532 if (attr_p)
9533 warning (OPT_Wattributes,
ebd7c4c1 9534 "bad option %s to optimize attribute", p);
46f8e3b0 9535 else
9536 warning (OPT_Wpragmas,
e44b0a1f 9537 "bad option %s to pragma attribute", p);
46f8e3b0 9538 continue;
9539 }
9540
9541 if (*p != '-')
9542 {
9543 *r++ = '-';
9544
9545 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9546 itself is -Os, and any other switch begins with a -f. */
9547 if ((*p >= '0' && *p <= '9')
9548 || (p[0] == 's' && p[1] == '\0'))
9549 *r++ = 'O';
9550 else if (*p != 'O')
9551 *r++ = 'f';
9552 }
9553
9554 memcpy (r, p, len2);
9555 r[len2] = '\0';
f1f41a6c 9556 vec_safe_push (optimize_args, (const char *) q);
46f8e3b0 9557 }
9558
9559 }
9560 }
9561
f1f41a6c 9562 opt_argc = optimize_args->length ();
46f8e3b0 9563 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9564
9565 for (i = 1; i < opt_argc; i++)
f1f41a6c 9566 opt_argv[i] = (*optimize_args)[i];
46f8e3b0 9567
9568 /* Now parse the options. */
f3f006ad 9569 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9570 &decoded_options,
9571 &decoded_options_count);
9572 decode_options (&global_options, &global_options_set,
3c6c0e40 9573 decoded_options, decoded_options_count,
9574 input_location, global_dc);
46f8e3b0 9575
4bec06b3 9576 targetm.override_options_after_change();
9577
f1f41a6c 9578 optimize_args->truncate (0);
46f8e3b0 9579 return ret;
9580}
9581
9582/* For handling "optimize" attribute. arguments as in
9583 struct attribute_spec.handler. */
9584
9585static tree
9586handle_optimize_attribute (tree *node, tree name, tree args,
9587 int ARG_UNUSED (flags), bool *no_add_attrs)
9588{
9589 /* Ensure we have a function type. */
9590 if (TREE_CODE (*node) != FUNCTION_DECL)
9591 {
9592 warning (OPT_Wattributes, "%qE attribute ignored", name);
9593 *no_add_attrs = true;
9594 }
9595 else
9596 {
9597 struct cl_optimization cur_opts;
9598 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9599
9600 /* Save current options. */
2c5d2e39 9601 cl_optimization_save (&cur_opts, &global_options);
46f8e3b0 9602
9603 /* If we previously had some optimization options, use them as the
9604 default. */
9605 if (old_opts)
2c5d2e39 9606 cl_optimization_restore (&global_options,
9607 TREE_OPTIMIZATION (old_opts));
46f8e3b0 9608
9609 /* Parse options, and update the vector. */
9610 parse_optimize_options (args, true);
9611 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
55310327 9612 = build_optimization_node (&global_options);
46f8e3b0 9613
9614 /* Restore current options. */
2c5d2e39 9615 cl_optimization_restore (&global_options, &cur_opts);
46f8e3b0 9616 }
9617
9618 return NULL_TREE;
9619}
48b14f50 9620
9621/* Handle a "no_split_stack" attribute. */
9622
9623static tree
9624handle_no_split_stack_attribute (tree *node, tree name,
9625 tree ARG_UNUSED (args),
9626 int ARG_UNUSED (flags),
9627 bool *no_add_attrs)
9628{
9629 tree decl = *node;
9630
9631 if (TREE_CODE (decl) != FUNCTION_DECL)
9632 {
9633 error_at (DECL_SOURCE_LOCATION (decl),
9634 "%qE attribute applies only to functions", name);
9635 *no_add_attrs = true;
9636 }
9637 else if (DECL_INITIAL (decl))
9638 {
9639 error_at (DECL_SOURCE_LOCATION (decl),
9640 "can%'t set %qE attribute after definition", name);
9641 *no_add_attrs = true;
9642 }
9643
9644 return NULL_TREE;
9645}
d7dcba40 9646
9647/* Handle a "returns_nonnull" attribute; arguments as in
9648 struct attribute_spec.handler. */
9649
9650static tree
9651handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9652 bool *no_add_attrs)
9653{
9654 // Even without a prototype we still have a return type we can check.
9655 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9656 {
9657 error ("returns_nonnull attribute on a function not returning a pointer");
9658 *no_add_attrs = true;
9659 }
9660 return NULL_TREE;
9661}
9662
74691f46 9663/* Handle a "designated_init" attribute; arguments as in
9664 struct attribute_spec.handler. */
9665
9666static tree
9667handle_designated_init_attribute (tree *node, tree name, tree, int,
9668 bool *no_add_attrs)
9669{
9670 if (TREE_CODE (*node) != RECORD_TYPE)
9671 {
9672 error ("%qE attribute is only valid on %<struct%> type", name);
9673 *no_add_attrs = true;
9674 }
9675 return NULL_TREE;
9676}
9677
dbf6c367 9678\f
774e9d58 9679/* Check for valid arguments being passed to a function with FNTYPE.
bda05c57 9680 There are NARGS arguments in the array ARGARRAY. LOC should be used for
9681 diagnostics. */
dbf6c367 9682void
bda05c57 9683check_function_arguments (location_t loc, const_tree fntype, int nargs,
9684 tree *argarray)
dbf6c367 9685{
9686 /* Check for null being passed in a pointer argument that must be
9687 non-null. We also need to do this if format checking is enabled. */
9688
9689 if (warn_nonnull)
bda05c57 9690 check_function_nonnull (loc, TYPE_ATTRIBUTES (fntype), nargs, argarray);
dbf6c367 9691
9692 /* Check for errors in format strings. */
9693
068bea1e 9694 if (warn_format || warn_suggest_attribute_format)
774e9d58 9695 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
95c90e04 9696
9697 if (warn_format)
774e9d58 9698 check_function_sentinel (fntype, nargs, argarray);
dbf6c367 9699}
9700
9701/* Generic argument checking recursion routine. PARAM is the argument to
9702 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9703 once the argument is resolved. CTX is context for the callback. */
9704void
1cae46be 9705check_function_arguments_recurse (void (*callback)
9706 (void *, tree, unsigned HOST_WIDE_INT),
9707 void *ctx, tree param,
9708 unsigned HOST_WIDE_INT param_num)
dbf6c367 9709{
72dd6141 9710 if (CONVERT_EXPR_P (param)
c44afe23 9711 && (TYPE_PRECISION (TREE_TYPE (param))
9712 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
dbf6c367 9713 {
9714 /* Strip coercion. */
9715 check_function_arguments_recurse (callback, ctx,
4ee9c684 9716 TREE_OPERAND (param, 0), param_num);
dbf6c367 9717 return;
9718 }
9719
9720 if (TREE_CODE (param) == CALL_EXPR)
9721 {
c2f47e15 9722 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
dbf6c367 9723 tree attrs;
9724 bool found_format_arg = false;
9725
9726 /* See if this is a call to a known internationalization function
9727 that modifies a format arg. Such a function may have multiple
9728 format_arg attributes (for example, ngettext). */
9729
9730 for (attrs = TYPE_ATTRIBUTES (type);
9731 attrs;
9732 attrs = TREE_CHAIN (attrs))
9733 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9734 {
c2f47e15 9735 tree inner_arg;
dbf6c367 9736 tree format_num_expr;
9737 int format_num;
9738 int i;
c2f47e15 9739 call_expr_arg_iterator iter;
dbf6c367 9740
9741 /* Extract the argument number, which was previously checked
9742 to be valid. */
9743 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
ddb1be65 9744
e913b5cd 9745 format_num = tree_to_uhwi (format_num_expr);
dbf6c367 9746
c2f47e15 9747 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9748 inner_arg != 0;
9749 inner_arg = next_call_expr_arg (&iter), i++)
dbf6c367 9750 if (i == format_num)
9751 {
9752 check_function_arguments_recurse (callback, ctx,
c2f47e15 9753 inner_arg, param_num);
dbf6c367 9754 found_format_arg = true;
9755 break;
9756 }
9757 }
9758
9759 /* If we found a format_arg attribute and did a recursive check,
9760 we are done with checking this argument. Otherwise, we continue
9761 and this will be considered a non-literal. */
9762 if (found_format_arg)
9763 return;
9764 }
9765
9766 if (TREE_CODE (param) == COND_EXPR)
9767 {
f6dfb86a 9768 /* Simplify to avoid warning for an impossible case. */
9769 param = fold_for_warn (param);
9770 if (TREE_CODE (param) == COND_EXPR)
9771 {
9772 /* Check both halves of the conditional expression. */
9773 check_function_arguments_recurse (callback, ctx,
9774 TREE_OPERAND (param, 1),
9775 param_num);
9776 check_function_arguments_recurse (callback, ctx,
9777 TREE_OPERAND (param, 2),
9778 param_num);
9779 return;
9780 }
dbf6c367 9781 }
9782
9783 (*callback) (ctx, param, param_num);
9784}
1f3233d1 9785
60cce472 9786/* Checks for a builtin function FNDECL that the number of arguments
9787 NARGS against the required number REQUIRED and issues an error if
9788 there is a mismatch. Returns true if the number of arguments is
9789 correct, otherwise false. */
d43cee80 9790
9791static bool
60cce472 9792builtin_function_validate_nargs (tree fndecl, int nargs, int required)
d43cee80 9793{
9794 if (nargs < required)
9795 {
60cce472 9796 error_at (input_location,
9797 "not enough arguments to function %qE", fndecl);
d43cee80 9798 return false;
9799 }
9800 else if (nargs > required)
9801 {
60cce472 9802 error_at (input_location,
9803 "too many arguments to function %qE", fndecl);
d43cee80 9804 return false;
9805 }
9806 return true;
9807}
9808
9809/* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9810 Returns false if there was an error, otherwise true. */
9811
9812bool
9813check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9814{
9815 if (!DECL_BUILT_IN (fndecl)
9816 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9817 return true;
9818
9819 switch (DECL_FUNCTION_CODE (fndecl))
9820 {
8c38d887 9821 case BUILT_IN_ALLOCA_WITH_ALIGN:
9822 {
9823 /* Get the requested alignment (in bits) if it's a constant
9824 integer expression. */
9825 unsigned HOST_WIDE_INT align
9826 = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
9827
9828 /* Determine if the requested alignment is a power of 2. */
9829 if ((align & (align - 1)))
9830 align = 0;
9831
9832 /* The maximum alignment in bits corresponding to the same
9833 maximum in bytes enforced in check_user_alignment(). */
9834 unsigned maxalign = (UINT_MAX >> 1) + 1;
9835
9836 /* Reject invalid alignments. */
9837 if (align < BITS_PER_UNIT || maxalign < align)
9838 {
9839 error_at (EXPR_LOC_OR_LOC (args[1], input_location),
9840 "second argument to function %qE must be a constant "
9841 "integer power of 2 between %qi and %qu bits",
9842 fndecl, BITS_PER_UNIT, maxalign);
9843 return false;
9844 }
9845 return true;
9846 }
9847
d43cee80 9848 case BUILT_IN_CONSTANT_P:
60cce472 9849 return builtin_function_validate_nargs (fndecl, nargs, 1);
d43cee80 9850
9851 case BUILT_IN_ISFINITE:
9852 case BUILT_IN_ISINF:
c319d56a 9853 case BUILT_IN_ISINF_SIGN:
d43cee80 9854 case BUILT_IN_ISNAN:
9855 case BUILT_IN_ISNORMAL:
10902624 9856 case BUILT_IN_SIGNBIT:
60cce472 9857 if (builtin_function_validate_nargs (fndecl, nargs, 1))
d43cee80 9858 {
9859 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9860 {
9861 error ("non-floating-point argument in call to "
9862 "function %qE", fndecl);
9863 return false;
9864 }
9865 return true;
9866 }
9867 return false;
9868
9869 case BUILT_IN_ISGREATER:
9870 case BUILT_IN_ISGREATEREQUAL:
9871 case BUILT_IN_ISLESS:
9872 case BUILT_IN_ISLESSEQUAL:
9873 case BUILT_IN_ISLESSGREATER:
9874 case BUILT_IN_ISUNORDERED:
60cce472 9875 if (builtin_function_validate_nargs (fndecl, nargs, 2))
d43cee80 9876 {
9877 enum tree_code code0, code1;
9878 code0 = TREE_CODE (TREE_TYPE (args[0]));
9879 code1 = TREE_CODE (TREE_TYPE (args[1]));
9880 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
9881 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
9882 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
9883 {
9884 error ("non-floating-point arguments in call to "
9885 "function %qE", fndecl);
9886 return false;
9887 }
9888 return true;
9889 }
9890 return false;
9891
19fbe3a4 9892 case BUILT_IN_FPCLASSIFY:
60cce472 9893 if (builtin_function_validate_nargs (fndecl, nargs, 6))
19fbe3a4 9894 {
9895 unsigned i;
48e1416a 9896
19fbe3a4 9897 for (i=0; i<5; i++)
9898 if (TREE_CODE (args[i]) != INTEGER_CST)
9899 {
9900 error ("non-const integer argument %u in call to function %qE",
9901 i+1, fndecl);
9902 return false;
9903 }
9904
9905 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
9906 {
9907 error ("non-floating-point argument in call to function %qE",
9908 fndecl);
9909 return false;
9910 }
9911 return true;
9912 }
9913 return false;
9914
fca0886c 9915 case BUILT_IN_ASSUME_ALIGNED:
9916 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
9917 {
9918 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
9919 {
9920 error ("non-integer argument 3 in call to function %qE", fndecl);
9921 return false;
9922 }
9923 return true;
9924 }
9925 return false;
9926
0c93c8a9 9927 case BUILT_IN_ADD_OVERFLOW:
9928 case BUILT_IN_SUB_OVERFLOW:
9929 case BUILT_IN_MUL_OVERFLOW:
9930 if (builtin_function_validate_nargs (fndecl, nargs, 3))
9931 {
9932 unsigned i;
9933 for (i = 0; i < 2; i++)
9934 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
9935 {
9936 error ("argument %u in call to function %qE does not have "
9937 "integral type", i + 1, fndecl);
9938 return false;
9939 }
9940 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
9941 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
9942 {
9943 error ("argument 3 in call to function %qE does not have "
9944 "pointer to integer type", fndecl);
9945 return false;
9946 }
9947 return true;
9948 }
9949 return false;
9950
d43cee80 9951 default:
9952 return true;
9953 }
9954}
9955
860251be 9956/* Function to help qsort sort FIELD_DECLs by name order. */
9957
9958int
9959field_decl_cmp (const void *x_p, const void *y_p)
9960{
4fd61bc6 9961 const tree *const x = (const tree *const) x_p;
9962 const tree *const y = (const tree *const) y_p;
9963
860251be 9964 if (DECL_NAME (*x) == DECL_NAME (*y))
9965 /* A nontype is "greater" than a type. */
9966 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9967 if (DECL_NAME (*x) == NULL_TREE)
9968 return -1;
9969 if (DECL_NAME (*y) == NULL_TREE)
9970 return 1;
9971 if (DECL_NAME (*x) < DECL_NAME (*y))
9972 return -1;
9973 return 1;
9974}
9975
9976static struct {
9977 gt_pointer_operator new_value;
9978 void *cookie;
9979} resort_data;
9980
9981/* This routine compares two fields like field_decl_cmp but using the
9982pointer operator in resort_data. */
9983
9984static int
9985resort_field_decl_cmp (const void *x_p, const void *y_p)
9986{
4fd61bc6 9987 const tree *const x = (const tree *const) x_p;
9988 const tree *const y = (const tree *const) y_p;
860251be 9989
9990 if (DECL_NAME (*x) == DECL_NAME (*y))
9991 /* A nontype is "greater" than a type. */
9992 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
9993 if (DECL_NAME (*x) == NULL_TREE)
9994 return -1;
9995 if (DECL_NAME (*y) == NULL_TREE)
9996 return 1;
9997 {
9998 tree d1 = DECL_NAME (*x);
9999 tree d2 = DECL_NAME (*y);
10000 resort_data.new_value (&d1, resort_data.cookie);
10001 resort_data.new_value (&d2, resort_data.cookie);
10002 if (d1 < d2)
10003 return -1;
10004 }
10005 return 1;
10006}
10007
10008/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10009
10010void
10011resort_sorted_fields (void *obj,
9a03a746 10012 void * ARG_UNUSED (orig_obj),
4ee9c684 10013 gt_pointer_operator new_value,
10014 void *cookie)
860251be 10015{
9a03a746 10016 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
860251be 10017 resort_data.new_value = new_value;
10018 resort_data.cookie = cookie;
10019 qsort (&sf->elts[0], sf->len, sizeof (tree),
4ee9c684 10020 resort_field_decl_cmp);
860251be 10021}
10022
209c9752 10023/* Subroutine of c_parse_error.
10024 Return the result of concatenating LHS and RHS. RHS is really
10025 a string literal, its first character is indicated by RHS_START and
cfee01e3 10026 RHS_SIZE is its length (including the terminating NUL character).
209c9752 10027
10028 The caller is responsible for deleting the returned pointer. */
10029
10030static char *
10031catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10032{
10033 const int lhs_size = strlen (lhs);
10034 char *result = XNEWVEC (char, lhs_size + rhs_size);
10035 strncpy (result, lhs, lhs_size);
10036 strncpy (result + lhs_size, rhs_start, rhs_size);
10037 return result;
10038}
10039
380c6697 10040/* Issue the error given by GMSGID, indicating that it occurred before
92b128ed 10041 TOKEN, which had the associated VALUE. */
10042
10043void
48e1416a 10044c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
ba99525e 10045 tree value, unsigned char token_flags)
92b128ed 10046{
209c9752 10047#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10048
10049 char *message = NULL;
92b128ed 10050
ba99525e 10051 if (token_type == CPP_EOF)
380c6697 10052 message = catenate_messages (gmsgid, " at end of input");
48e1416a 10053 else if (token_type == CPP_CHAR
10054 || token_type == CPP_WCHAR
ba99525e 10055 || token_type == CPP_CHAR16
30b1ba42 10056 || token_type == CPP_CHAR32
10057 || token_type == CPP_UTF8CHAR)
92b128ed 10058 {
f9ae6f95 10059 unsigned int val = TREE_INT_CST_LOW (value);
924bbf02 10060 const char *prefix;
10061
ba99525e 10062 switch (token_type)
924bbf02 10063 {
10064 default:
10065 prefix = "";
10066 break;
10067 case CPP_WCHAR:
10068 prefix = "L";
10069 break;
10070 case CPP_CHAR16:
10071 prefix = "u";
10072 break;
10073 case CPP_CHAR32:
10074 prefix = "U";
10075 break;
30b1ba42 10076 case CPP_UTF8CHAR:
10077 prefix = "u8";
10078 break;
924bbf02 10079 }
10080
92b128ed 10081 if (val <= UCHAR_MAX && ISGRAPH (val))
a0c938f0 10082 message = catenate_messages (gmsgid, " before %s'%c'");
92b128ed 10083 else
a0c938f0 10084 message = catenate_messages (gmsgid, " before %s'\\x%x'");
209c9752 10085
924bbf02 10086 error (message, prefix, val);
209c9752 10087 free (message);
10088 message = NULL;
92b128ed 10089 }
1898176c 10090 else if (token_type == CPP_CHAR_USERDEF
10091 || token_type == CPP_WCHAR_USERDEF
10092 || token_type == CPP_CHAR16_USERDEF
30b1ba42 10093 || token_type == CPP_CHAR32_USERDEF
10094 || token_type == CPP_UTF8CHAR_USERDEF)
1898176c 10095 message = catenate_messages (gmsgid,
10096 " before user-defined character literal");
10097 else if (token_type == CPP_STRING_USERDEF
10098 || token_type == CPP_WSTRING_USERDEF
10099 || token_type == CPP_STRING16_USERDEF
10100 || token_type == CPP_STRING32_USERDEF
10101 || token_type == CPP_UTF8STRING_USERDEF)
10102 message = catenate_messages (gmsgid, " before user-defined string literal");
48e1416a 10103 else if (token_type == CPP_STRING
10104 || token_type == CPP_WSTRING
ba99525e 10105 || token_type == CPP_STRING16
538ba11a 10106 || token_type == CPP_STRING32
10107 || token_type == CPP_UTF8STRING)
380c6697 10108 message = catenate_messages (gmsgid, " before string constant");
ba99525e 10109 else if (token_type == CPP_NUMBER)
380c6697 10110 message = catenate_messages (gmsgid, " before numeric constant");
ba99525e 10111 else if (token_type == CPP_NAME)
209c9752 10112 {
380c6697 10113 message = catenate_messages (gmsgid, " before %qE");
782858b8 10114 error (message, value);
209c9752 10115 free (message);
10116 message = NULL;
10117 }
ba99525e 10118 else if (token_type == CPP_PRAGMA)
b75b98aa 10119 message = catenate_messages (gmsgid, " before %<#pragma%>");
ba99525e 10120 else if (token_type == CPP_PRAGMA_EOL)
b75b98aa 10121 message = catenate_messages (gmsgid, " before end of line");
07b8f133 10122 else if (token_type == CPP_DECLTYPE)
10123 message = catenate_messages (gmsgid, " before %<decltype%>");
ba99525e 10124 else if (token_type < N_TTYPES)
209c9752 10125 {
380c6697 10126 message = catenate_messages (gmsgid, " before %qs token");
ba99525e 10127 error (message, cpp_type2name (token_type, token_flags));
209c9752 10128 free (message);
10129 message = NULL;
10130 }
92b128ed 10131 else
380c6697 10132 error (gmsgid);
209c9752 10133
10134 if (message)
10135 {
10136 error (message);
10137 free (message);
10138 }
a0c938f0 10139#undef catenate_messages
92b128ed 10140}
10141
3a79f5da 10142/* Return the gcc option code associated with the reason for a cpp
10143 message, or 0 if none. */
10144
10145static int
10146c_option_controlling_cpp_error (int reason)
10147{
7ff8db31 10148 const struct cpp_reason_option_codes_t *entry;
3a79f5da 10149
7ff8db31 10150 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
3a79f5da 10151 {
10152 if (entry->reason == reason)
10153 return entry->option_code;
10154 }
10155 return 0;
10156}
10157
7f5f3953 10158/* Callback from cpp_error for PFILE to print diagnostics from the
3a79f5da 10159 preprocessor. The diagnostic is of type LEVEL, with REASON set
10160 to the reason code if LEVEL is represents a warning, at location
f0479000 10161 RICHLOC unless this is after lexing and the compiler's location
10162 should be used instead; MSG is the translated message and AP
7f5f3953 10163 the arguments. Returns true if a diagnostic was emitted, false
10164 otherwise. */
10165
10166bool
3a79f5da 10167c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
f0479000 10168 rich_location *richloc,
7f5f3953 10169 const char *msg, va_list *ap)
10170{
10171 diagnostic_info diagnostic;
10172 diagnostic_t dlevel;
5ae82d58 10173 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
7f5f3953 10174 bool ret;
10175
10176 switch (level)
10177 {
10178 case CPP_DL_WARNING_SYSHDR:
10179 if (flag_no_output)
10180 return false;
5ae82d58 10181 global_dc->dc_warn_system_headers = 1;
7f5f3953 10182 /* Fall through. */
10183 case CPP_DL_WARNING:
10184 if (flag_no_output)
10185 return false;
10186 dlevel = DK_WARNING;
10187 break;
10188 case CPP_DL_PEDWARN:
10189 if (flag_no_output && !flag_pedantic_errors)
10190 return false;
10191 dlevel = DK_PEDWARN;
10192 break;
10193 case CPP_DL_ERROR:
10194 dlevel = DK_ERROR;
10195 break;
10196 case CPP_DL_ICE:
10197 dlevel = DK_ICE;
10198 break;
10199 case CPP_DL_NOTE:
10200 dlevel = DK_NOTE;
10201 break;
ff903809 10202 case CPP_DL_FATAL:
10203 dlevel = DK_FATAL;
10204 break;
7f5f3953 10205 default:
10206 gcc_unreachable ();
10207 }
10208 if (done_lexing)
d0f713f4 10209 richloc->set_range (line_table, 0, input_location, true);
7f5f3953 10210 diagnostic_set_info_translated (&diagnostic, msg, ap,
f0479000 10211 richloc, dlevel);
3a79f5da 10212 diagnostic_override_option_index (&diagnostic,
10213 c_option_controlling_cpp_error (reason));
7f5f3953 10214 ret = report_diagnostic (&diagnostic);
10215 if (level == CPP_DL_WARNING_SYSHDR)
5ae82d58 10216 global_dc->dc_warn_system_headers = save_warn_system_headers;
7f5f3953 10217 return ret;
10218}
10219
624d37a6 10220/* Convert a character from the host to the target execution character
10221 set. cpplib handles this, mostly. */
10222
10223HOST_WIDE_INT
10224c_common_to_target_charset (HOST_WIDE_INT c)
10225{
10226 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10227 zero-extended under -fno-signed-char. cpplib insists that characters
10228 and character constants are always unsigned. Hence we must convert
10229 back and forth. */
10230 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10231
10232 uc = cpp_host_to_exec_charset (parse_in, uc);
10233
10234 if (flag_signed_char)
10235 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10236 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10237 else
10238 return uc;
10239}
10240
7549df0d 10241/* Fold an offsetof-like expression. EXPR is a nested sequence of component
10242 references with an INDIRECT_REF of a constant at the bottom; much like the
10243 traditional rendering of offsetof as a macro. Return the folded result. */
af28855b 10244
7549df0d 10245tree
aeaccb75 10246fold_offsetof_1 (tree expr, enum tree_code ctx)
af28855b 10247{
af28855b 10248 tree base, off, t;
aeaccb75 10249 tree_code code = TREE_CODE (expr);
10250 switch (code)
af28855b 10251 {
10252 case ERROR_MARK:
10253 return expr;
10254
6b11d2e3 10255 case VAR_DECL:
10256 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10257 return error_mark_node;
10258
d897f7c2 10259 case CALL_EXPR:
cf1a89a3 10260 case TARGET_EXPR:
d897f7c2 10261 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10262 return error_mark_node;
10263
d897f7c2 10264 case NOP_EXPR:
10265 case INDIRECT_REF:
7549df0d 10266 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
64ed018c 10267 {
10268 error ("cannot apply %<offsetof%> to a non constant address");
10269 return error_mark_node;
10270 }
7549df0d 10271 return TREE_OPERAND (expr, 0);
d897f7c2 10272
af28855b 10273 case COMPONENT_REF:
aeaccb75 10274 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
af28855b 10275 if (base == error_mark_node)
10276 return base;
10277
10278 t = TREE_OPERAND (expr, 1);
10279 if (DECL_C_BIT_FIELD (t))
10280 {
10281 error ("attempt to take address of bit-field structure "
782858b8 10282 "member %qD", t);
af28855b 10283 return error_mark_node;
10284 }
389dd41b 10285 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
e913b5cd 10286 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
389dd41b 10287 / BITS_PER_UNIT));
af28855b 10288 break;
10289
10290 case ARRAY_REF:
aeaccb75 10291 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
af28855b 10292 if (base == error_mark_node)
10293 return base;
10294
10295 t = TREE_OPERAND (expr, 1);
64ed018c 10296
10297 /* Check if the offset goes beyond the upper bound of the array. */
7549df0d 10298 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
e0559d69 10299 {
10300 tree upbound = array_ref_up_bound (expr);
10301 if (upbound != NULL_TREE
10302 && TREE_CODE (upbound) == INTEGER_CST
10303 && !tree_int_cst_equal (upbound,
10304 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10305 {
aeaccb75 10306 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
10307 upbound = size_binop (PLUS_EXPR, upbound,
10308 build_int_cst (TREE_TYPE (upbound), 1));
e0559d69 10309 if (tree_int_cst_lt (upbound, t))
10310 {
10311 tree v;
10312
10313 for (v = TREE_OPERAND (expr, 0);
10314 TREE_CODE (v) == COMPONENT_REF;
10315 v = TREE_OPERAND (v, 0))
10316 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10317 == RECORD_TYPE)
10318 {
1767a056 10319 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10320 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
e0559d69 10321 if (TREE_CODE (fld_chain) == FIELD_DECL)
10322 break;
10323
10324 if (fld_chain)
10325 break;
10326 }
10327 /* Don't warn if the array might be considered a poor
10328 man's flexible array member with a very permissive
10329 definition thereof. */
10330 if (TREE_CODE (v) == ARRAY_REF
10331 || TREE_CODE (v) == COMPONENT_REF)
10332 warning (OPT_Warray_bounds,
10333 "index %E denotes an offset "
10334 "greater than size of %qT",
10335 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10336 }
10337 }
10338 }
7549df0d 10339
10340 t = convert (sizetype, t);
10341 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
af28855b 10342 break;
10343
ede90cc2 10344 case COMPOUND_EXPR:
10345 /* Handle static members of volatile structs. */
10346 t = TREE_OPERAND (expr, 1);
f48c7f4a 10347 gcc_assert (VAR_P (t));
7549df0d 10348 return fold_offsetof_1 (t);
ede90cc2 10349
af28855b 10350 default:
231bd014 10351 gcc_unreachable ();
af28855b 10352 }
10353
7549df0d 10354 return fold_build_pointer_plus (base, off);
af28855b 10355}
10356
7549df0d 10357/* Likewise, but convert it to the return type of offsetof. */
10358
af28855b 10359tree
7549df0d 10360fold_offsetof (tree expr)
af28855b 10361{
7549df0d 10362 return convert (size_type_node, fold_offsetof_1 (expr));
af28855b 10363}
10364
b9bdfa0b 10365/* Warn for A ?: C expressions (with B omitted) where A is a boolean
10366 expression, because B will always be true. */
10367
10368void
10369warn_for_omitted_condop (location_t location, tree cond)
10370{
10371 if (truth_value_p (TREE_CODE (cond)))
10372 warning_at (location, OPT_Wparentheses,
10373 "the omitted middle operand in ?: will always be %<true%>, "
10374 "suggest explicit middle operand");
10375}
10376
a1f90215 10377/* Give an error for storing into ARG, which is 'const'. USE indicates
10378 how ARG was being used. */
10379
10380void
f2697631 10381readonly_error (location_t loc, tree arg, enum lvalue_use use)
a1f90215 10382{
10383 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10384 || use == lv_asm);
10385 /* Using this macro rather than (for example) arrays of messages
10386 ensures that all the format strings are checked at compile
10387 time. */
10388#define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10389 : (use == lv_increment ? (I) \
10390 : (use == lv_decrement ? (D) : (AS))))
10391 if (TREE_CODE (arg) == COMPONENT_REF)
10392 {
10393 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
f2697631 10394 error_at (loc, READONLY_MSG (G_("assignment of member "
10395 "%qD in read-only object"),
10396 G_("increment of member "
10397 "%qD in read-only object"),
10398 G_("decrement of member "
10399 "%qD in read-only object"),
10400 G_("member %qD in read-only object "
10401 "used as %<asm%> output")),
10402 TREE_OPERAND (arg, 1));
a1f90215 10403 else
f2697631 10404 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10405 G_("increment of read-only member %qD"),
10406 G_("decrement of read-only member %qD"),
10407 G_("read-only member %qD used as %<asm%> output")),
10408 TREE_OPERAND (arg, 1));
a1f90215 10409 }
f48c7f4a 10410 else if (VAR_P (arg))
f2697631 10411 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10412 G_("increment of read-only variable %qD"),
10413 G_("decrement of read-only variable %qD"),
10414 G_("read-only variable %qD used as %<asm%> output")),
10415 arg);
a1f90215 10416 else if (TREE_CODE (arg) == PARM_DECL)
f2697631 10417 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10418 G_("increment of read-only parameter %qD"),
10419 G_("decrement of read-only parameter %qD"),
10420 G_("read-only parameter %qD use as %<asm%> output")),
10421 arg);
a1f90215 10422 else if (TREE_CODE (arg) == RESULT_DECL)
10423 {
10424 gcc_assert (c_dialect_cxx ());
f2697631 10425 error_at (loc, READONLY_MSG (G_("assignment of "
10426 "read-only named return value %qD"),
10427 G_("increment of "
10428 "read-only named return value %qD"),
10429 G_("decrement of "
10430 "read-only named return value %qD"),
10431 G_("read-only named return value %qD "
10432 "used as %<asm%>output")),
10433 arg);
a1f90215 10434 }
10435 else if (TREE_CODE (arg) == FUNCTION_DECL)
f2697631 10436 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10437 G_("increment of function %qD"),
10438 G_("decrement of function %qD"),
10439 G_("function %qD used as %<asm%> output")),
10440 arg);
a1f90215 10441 else
f2697631 10442 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10443 G_("increment of read-only location %qE"),
10444 G_("decrement of read-only location %qE"),
10445 G_("read-only location %qE used as %<asm%> output")),
10446 arg);
a1f90215 10447}
10448
e35976b1 10449/* Print an error message for an invalid lvalue. USE says
fdd84b77 10450 how the lvalue is being used and so selects the error message. LOC
10451 is the location for the error. */
ab6bb714 10452
e35976b1 10453void
fdd84b77 10454lvalue_error (location_t loc, enum lvalue_use use)
ab6bb714 10455{
e35976b1 10456 switch (use)
ab6bb714 10457 {
e35976b1 10458 case lv_assign:
fdd84b77 10459 error_at (loc, "lvalue required as left operand of assignment");
e35976b1 10460 break;
10461 case lv_increment:
fdd84b77 10462 error_at (loc, "lvalue required as increment operand");
e35976b1 10463 break;
10464 case lv_decrement:
fdd84b77 10465 error_at (loc, "lvalue required as decrement operand");
e35976b1 10466 break;
10467 case lv_addressof:
fdd84b77 10468 error_at (loc, "lvalue required as unary %<&%> operand");
e35976b1 10469 break;
10470 case lv_asm:
fdd84b77 10471 error_at (loc, "lvalue required in asm statement");
e35976b1 10472 break;
10473 default:
10474 gcc_unreachable ();
ab6bb714 10475 }
ab6bb714 10476}
b1bbc8e5 10477
10478/* Print an error message for an invalid indirection of type TYPE.
10479 ERRSTRING is the name of the operator for the indirection. */
10480
10481void
10482invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10483{
10484 switch (errstring)
10485 {
10486 case RO_NULL:
10487 gcc_assert (c_dialect_cxx ());
10488 error_at (loc, "invalid type argument (have %qT)", type);
10489 break;
10490 case RO_ARRAY_INDEXING:
10491 error_at (loc,
10492 "invalid type argument of array indexing (have %qT)",
10493 type);
10494 break;
10495 case RO_UNARY_STAR:
10496 error_at (loc,
10497 "invalid type argument of unary %<*%> (have %qT)",
10498 type);
10499 break;
10500 case RO_ARROW:
10501 error_at (loc,
10502 "invalid type argument of %<->%> (have %qT)",
10503 type);
10504 break;
7354a89b 10505 case RO_ARROW_STAR:
10506 error_at (loc,
10507 "invalid type argument of %<->*%> (have %qT)",
10508 type);
10509 break;
b1bbc8e5 10510 case RO_IMPLICIT_CONVERSION:
10511 error_at (loc,
10512 "invalid type argument of implicit conversion (have %qT)",
10513 type);
10514 break;
10515 default:
10516 gcc_unreachable ();
10517 }
10518}
c271bdb2 10519\f
10520/* *PTYPE is an incomplete array. Complete it with a domain based on
10521 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10522 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10523 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10524
10525int
10526complete_array_type (tree *ptype, tree initial_value, bool do_default)
10527{
10528 tree maxindex, type, main_type, elt, unqual_elt;
10529 int failure = 0, quals;
6753bca0 10530 hashval_t hashcode = 0;
f5298614 10531 bool overflow_p = false;
c271bdb2 10532
10533 maxindex = size_zero_node;
10534 if (initial_value)
10535 {
10536 if (TREE_CODE (initial_value) == STRING_CST)
10537 {
10538 int eltsize
10539 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10540 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10541 }
10542 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10543 {
f1f41a6c 10544 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
c271bdb2 10545
f1f41a6c 10546 if (vec_safe_is_empty (v))
c271bdb2 10547 {
10548 if (pedantic)
10549 failure = 3;
7542c3b4 10550 maxindex = ssize_int (-1);
c271bdb2 10551 }
10552 else
10553 {
10554 tree curindex;
c75b4594 10555 unsigned HOST_WIDE_INT cnt;
10556 constructor_elt *ce;
cee43f7e 10557 bool fold_p = false;
c271bdb2 10558
f1f41a6c 10559 if ((*v)[0].index)
f5298614 10560 maxindex = (*v)[0].index, fold_p = true;
10561
c271bdb2 10562 curindex = maxindex;
10563
f1f41a6c 10564 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
c271bdb2 10565 {
cee43f7e 10566 bool curfold_p = false;
c75b4594 10567 if (ce->index)
cee43f7e 10568 curindex = ce->index, curfold_p = true;
c271bdb2 10569 else
cee43f7e 10570 {
10571 if (fold_p)
f5298614 10572 {
10573 /* Since we treat size types now as ordinary
10574 unsigned types, we need an explicit overflow
10575 check. */
10576 tree orig = curindex;
10577 curindex = fold_convert (sizetype, curindex);
10578 overflow_p |= tree_int_cst_lt (curindex, orig);
10579 }
389dd41b 10580 curindex = size_binop (PLUS_EXPR, curindex,
10581 size_one_node);
cee43f7e 10582 }
c271bdb2 10583 if (tree_int_cst_lt (maxindex, curindex))
cee43f7e 10584 maxindex = curindex, fold_p = curfold_p;
c271bdb2 10585 }
f5298614 10586 if (fold_p)
10587 {
10588 tree orig = maxindex;
10589 maxindex = fold_convert (sizetype, maxindex);
10590 overflow_p |= tree_int_cst_lt (maxindex, orig);
10591 }
c271bdb2 10592 }
10593 }
10594 else
10595 {
10596 /* Make an error message unless that happened already. */
10597 if (initial_value != error_mark_node)
10598 failure = 1;
10599 }
10600 }
10601 else
10602 {
10603 failure = 2;
10604 if (!do_default)
10605 return failure;
10606 }
10607
10608 type = *ptype;
10609 elt = TREE_TYPE (type);
10610 quals = TYPE_QUALS (strip_array_types (elt));
10611 if (quals == 0)
10612 unqual_elt = elt;
10613 else
6d5d708e 10614 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
c271bdb2 10615
10616 /* Using build_distinct_type_copy and modifying things afterward instead
10617 of using build_array_type to create a new type preserves all of the
10618 TYPE_LANG_FLAG_? bits that the front end may have set. */
10619 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10620 TREE_TYPE (main_type) = unqual_elt;
783bb57e 10621 TYPE_DOMAIN (main_type)
10622 = build_range_type (TREE_TYPE (maxindex),
10623 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
c271bdb2 10624 layout_type (main_type);
10625
6753bca0 10626 /* Make sure we have the canonical MAIN_TYPE. */
10627 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
48e1416a 10628 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
6753bca0 10629 hashcode);
10630 main_type = type_hash_canon (hashcode, main_type);
10631
796735dc 10632 /* Fix the canonical type. */
10633 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10634 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10635 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10636 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10637 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10638 != TYPE_DOMAIN (main_type)))
48e1416a 10639 TYPE_CANONICAL (main_type)
796735dc 10640 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10641 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10642 else
10643 TYPE_CANONICAL (main_type) = main_type;
10644
c271bdb2 10645 if (quals == 0)
10646 type = main_type;
10647 else
10648 type = c_build_qualified_type (main_type, quals);
10649
4f5b8f2a 10650 if (COMPLETE_TYPE_P (type)
10651 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
f5298614 10652 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
4f5b8f2a 10653 {
10654 error ("size of array is too large");
10655 /* If we proceed with the array type as it is, we'll eventually
08f817b3 10656 crash in tree_to_[su]hwi(). */
4f5b8f2a 10657 type = error_mark_node;
10658 }
10659
c271bdb2 10660 *ptype = type;
10661 return failure;
10662}
ab6bb714 10663
93426222 10664/* Like c_mark_addressable but don't check register qualifier. */
10665void
10666c_common_mark_addressable_vec (tree t)
10667{
10668 while (handled_component_p (t))
10669 t = TREE_OPERAND (t, 0);
f48c7f4a 10670 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
93426222 10671 return;
10672 TREE_ADDRESSABLE (t) = 1;
10673}
10674
10675
b6a5fc45 10676\f
10677/* Used to help initialize the builtin-types.def table. When a type of
10678 the correct size doesn't exist, use error_mark_node instead of NULL.
10679 The later results in segfaults even when a decl using the type doesn't
10680 get invoked. */
10681
10682tree
10683builtin_type_for_size (int size, bool unsignedp)
10684{
a51edb4c 10685 tree type = c_common_type_for_size (size, unsignedp);
b6a5fc45 10686 return type ? type : error_mark_node;
10687}
10688
10689/* A helper function for resolve_overloaded_builtin in resolving the
10690 overloaded __sync_ builtins. Returns a positive power of 2 if the
10691 first operand of PARAMS is a pointer to a supported data type.
517906ff 10692 Returns 0 if an error is encountered.
10693 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
10694 built-ins. */
b6a5fc45 10695
10696static int
517906ff 10697sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch)
b6a5fc45 10698{
517906ff 10699 /* Type of the argument. */
10700 tree argtype;
10701 /* Type the argument points to. */
b6a5fc45 10702 tree type;
10703 int size;
10704
77454e6e 10705 if (vec_safe_is_empty (params))
b6a5fc45 10706 {
10707 error ("too few arguments to function %qE", function);
10708 return 0;
10709 }
10710
517906ff 10711 argtype = type = TREE_TYPE ((*params)[0]);
0d284870 10712 if (TREE_CODE (type) == ARRAY_TYPE)
10713 {
10714 /* Force array-to-pointer decay for C++. */
10715 gcc_assert (c_dialect_cxx());
10716 (*params)[0] = default_conversion ((*params)[0]);
10717 type = TREE_TYPE ((*params)[0]);
10718 }
b6a5fc45 10719 if (TREE_CODE (type) != POINTER_TYPE)
10720 goto incompatible;
10721
10722 type = TREE_TYPE (type);
10723 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10724 goto incompatible;
10725
517906ff 10726 if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
10727 goto incompatible;
10728
e913b5cd 10729 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
27213ba3 10730 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
b6a5fc45 10731 return size;
10732
10733 incompatible:
bbbbe8ab 10734 /* Issue the diagnostic only if the argument is valid, otherwise
10735 it would be redundant at best and could be misleading. */
10736 if (argtype != error_mark_node)
10737 error ("operand type %qT is incompatible with argument %d of %qE",
10738 argtype, 1, function);
b6a5fc45 10739 return 0;
10740}
10741
a0c938f0 10742/* A helper function for resolve_overloaded_builtin. Adds casts to
b6a5fc45 10743 PARAMS to make arguments match up with those of FUNCTION. Drops
10744 the variadic arguments at the end. Returns false if some error
10745 was encountered; true on success. */
10746
10747static bool
1cd6e20d 10748sync_resolve_params (location_t loc, tree orig_function, tree function,
f1f41a6c 10749 vec<tree, va_gc> *params, bool orig_format)
b6a5fc45 10750{
d0af78c5 10751 function_args_iterator iter;
b6a5fc45 10752 tree ptype;
b9c74b4d 10753 unsigned int parmnum;
b6a5fc45 10754
d0af78c5 10755 function_args_iter_init (&iter, TREE_TYPE (function));
b6a5fc45 10756 /* We've declared the implementation functions to use "volatile void *"
10757 as the pointer parameter, so we shouldn't get any complaints from the
10758 call to check_function_arguments what ever type the user used. */
d0af78c5 10759 function_args_iter_next (&iter);
f1f41a6c 10760 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
b560fabd 10761 ptype = TYPE_MAIN_VARIANT (ptype);
b6a5fc45 10762
10763 /* For the rest of the values, we need to cast these to FTYPE, so that we
10764 don't get warnings for passing pointer types, etc. */
b9c74b4d 10765 parmnum = 0;
d0af78c5 10766 while (1)
b6a5fc45 10767 {
d0af78c5 10768 tree val, arg_type;
10769
10770 arg_type = function_args_iter_cond (&iter);
10771 /* XXX void_type_node belies the abstraction. */
10772 if (arg_type == void_type_node)
10773 break;
b6a5fc45 10774
b9c74b4d 10775 ++parmnum;
f1f41a6c 10776 if (params->length () <= parmnum)
b6a5fc45 10777 {
1cd6e20d 10778 error_at (loc, "too few arguments to function %qE", orig_function);
b6a5fc45 10779 return false;
10780 }
10781
0f6a7cb7 10782 /* Only convert parameters if arg_type is unsigned integer type with
10783 new format sync routines, i.e. don't attempt to convert pointer
10784 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10785 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10786 kinds). */
10787 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
1cd6e20d 10788 {
10789 /* Ideally for the first conversion we'd use convert_for_assignment
10790 so that we get warnings for anything that doesn't match the pointer
10791 type. This isn't portable across the C and C++ front ends atm. */
f1f41a6c 10792 val = (*params)[parmnum];
1cd6e20d 10793 val = convert (ptype, val);
10794 val = convert (arg_type, val);
f1f41a6c 10795 (*params)[parmnum] = val;
1cd6e20d 10796 }
b6a5fc45 10797
d0af78c5 10798 function_args_iter_next (&iter);
b6a5fc45 10799 }
10800
1cd6e20d 10801 /* __atomic routines are not variadic. */
f1f41a6c 10802 if (!orig_format && params->length () != parmnum + 1)
1cd6e20d 10803 {
10804 error_at (loc, "too many arguments to function %qE", orig_function);
10805 return false;
10806 }
10807
b6a5fc45 10808 /* The definition of these primitives is variadic, with the remaining
10809 being "an optional list of variables protected by the memory barrier".
10810 No clue what that's supposed to mean, precisely, but we consider all
10811 call-clobbered variables to be protected so we're safe. */
f1f41a6c 10812 params->truncate (parmnum + 1);
b6a5fc45 10813
10814 return true;
10815}
10816
a0c938f0 10817/* A helper function for resolve_overloaded_builtin. Adds a cast to
b6a5fc45 10818 RESULT to make it match the type of the first pointer argument in
10819 PARAMS. */
10820
10821static tree
1cd6e20d 10822sync_resolve_return (tree first_param, tree result, bool orig_format)
b6a5fc45 10823{
b9c74b4d 10824 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
1cd6e20d 10825 tree rtype = TREE_TYPE (result);
10080eac 10826 ptype = TYPE_MAIN_VARIANT (ptype);
1cd6e20d 10827
10828 /* New format doesn't require casting unless the types are the same size. */
10829 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10830 return convert (ptype, result);
10831 else
10832 return result;
10833}
10834
10835/* This function verifies the PARAMS to generic atomic FUNCTION.
10836 It returns the size if all the parameters are the same size, otherwise
10837 0 is returned if the parameters are invalid. */
10838
10839static int
f1f41a6c 10840get_atomic_generic_size (location_t loc, tree function,
10841 vec<tree, va_gc> *params)
1cd6e20d 10842{
10843 unsigned int n_param;
10844 unsigned int n_model;
10845 unsigned int x;
10846 int size_0;
10847 tree type_0;
10848
10849 /* Determine the parameter makeup. */
10850 switch (DECL_FUNCTION_CODE (function))
10851 {
10852 case BUILT_IN_ATOMIC_EXCHANGE:
10853 n_param = 4;
10854 n_model = 1;
10855 break;
10856 case BUILT_IN_ATOMIC_LOAD:
10857 case BUILT_IN_ATOMIC_STORE:
10858 n_param = 3;
10859 n_model = 1;
10860 break;
10861 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10862 n_param = 6;
10863 n_model = 2;
10864 break;
10865 default:
1d581089 10866 gcc_unreachable ();
1cd6e20d 10867 }
10868
f1f41a6c 10869 if (vec_safe_length (params) != n_param)
1cd6e20d 10870 {
10871 error_at (loc, "incorrect number of arguments to function %qE", function);
10872 return 0;
10873 }
10874
10875 /* Get type of first parameter, and determine its size. */
f1f41a6c 10876 type_0 = TREE_TYPE ((*params)[0]);
0d284870 10877 if (TREE_CODE (type_0) == ARRAY_TYPE)
10878 {
10879 /* Force array-to-pointer decay for C++. */
10880 gcc_assert (c_dialect_cxx());
10881 (*params)[0] = default_conversion ((*params)[0]);
10882 type_0 = TREE_TYPE ((*params)[0]);
10883 }
1d581089 10884 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
10885 {
10886 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
10887 function);
10888 return 0;
10889 }
10890
10891 /* Types must be compile time constant sizes. */
10892 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
1cd6e20d 10893 {
1d581089 10894 error_at (loc,
10895 "argument 1 of %qE must be a pointer to a constant size type",
10896 function);
1cd6e20d 10897 return 0;
10898 }
1d581089 10899
e913b5cd 10900 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
1cd6e20d 10901
1d581089 10902 /* Zero size objects are not allowed. */
10903 if (size_0 == 0)
10904 {
10905 error_at (loc,
10906 "argument 1 of %qE must be a pointer to a nonzero size object",
10907 function);
10908 return 0;
10909 }
10910
1cd6e20d 10911 /* Check each other parameter is a pointer and the same size. */
10912 for (x = 0; x < n_param - n_model; x++)
10913 {
10914 int size;
f1f41a6c 10915 tree type = TREE_TYPE ((*params)[x]);
a04e8d62 10916 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
1cd6e20d 10917 if (n_param == 6 && x == 3)
10918 continue;
10919 if (!POINTER_TYPE_P (type))
10920 {
10921 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
10922 function);
10923 return 0;
10924 }
1f6be080 10925 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
10926 size = type_size ? tree_to_uhwi (type_size) : 0;
1cd6e20d 10927 if (size != size_0)
10928 {
10929 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
10930 function);
10931 return 0;
10932 }
10933 }
10934
10935 /* Check memory model parameters for validity. */
10936 for (x = n_param - n_model ; x < n_param; x++)
10937 {
f1f41a6c 10938 tree p = (*params)[x];
1cd6e20d 10939 if (TREE_CODE (p) == INTEGER_CST)
10940 {
e913b5cd 10941 int i = tree_to_uhwi (p);
a372f7ca 10942 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
1cd6e20d 10943 {
10944 warning_at (loc, OPT_Winvalid_memory_model,
10945 "invalid memory model argument %d of %qE", x + 1,
10946 function);
1cd6e20d 10947 }
10948 }
10949 else
10950 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
10951 {
10952 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
10953 function);
10954 return 0;
10955 }
10956 }
10957
10958 return size_0;
10959}
10960
10961
10962/* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
10963 at the beginning of the parameter list PARAMS representing the size of the
10964 objects. This is to match the library ABI requirement. LOC is the location
10965 of the function call.
10966 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
10967 returned to allow the external call to be constructed. */
10968
10969static tree
10970add_atomic_size_parameter (unsigned n, location_t loc, tree function,
f1f41a6c 10971 vec<tree, va_gc> *params)
1cd6e20d 10972{
10973 tree size_node;
10974
10975 /* Insert a SIZE_T parameter as the first param. If there isn't
10976 enough space, allocate a new vector and recursively re-build with that. */
f1f41a6c 10977 if (!params->space (1))
1cd6e20d 10978 {
10979 unsigned int z, len;
f1f41a6c 10980 vec<tree, va_gc> *v;
1cd6e20d 10981 tree f;
10982
f1f41a6c 10983 len = params->length ();
10984 vec_alloc (v, len + 1);
5a672e62 10985 v->quick_push (build_int_cst (size_type_node, n));
1cd6e20d 10986 for (z = 0; z < len; z++)
f1f41a6c 10987 v->quick_push ((*params)[z]);
ec761d5a 10988 f = build_function_call_vec (loc, vNULL, function, v, NULL);
f1f41a6c 10989 vec_free (v);
1cd6e20d 10990 return f;
10991 }
10992
10993 /* Add the size parameter and leave as a function call for processing. */
10994 size_node = build_int_cst (size_type_node, n);
f1f41a6c 10995 params->quick_insert (0, size_node);
1cd6e20d 10996 return NULL_TREE;
10997}
10998
10999
a056826c 11000/* Return whether atomic operations for naturally aligned N-byte
11001 arguments are supported, whether inline or through libatomic. */
11002static bool
11003atomic_size_supported_p (int n)
11004{
11005 switch (n)
11006 {
11007 case 1:
11008 case 2:
11009 case 4:
11010 case 8:
11011 return true;
11012
11013 case 16:
11014 return targetm.scalar_mode_supported_p (TImode);
11015
11016 default:
11017 return false;
11018 }
11019}
11020
1cd6e20d 11021/* This will process an __atomic_exchange function call, determine whether it
11022 needs to be mapped to the _N variation, or turned into a library call.
11023 LOC is the location of the builtin call.
11024 FUNCTION is the DECL that has been invoked;
11025 PARAMS is the argument list for the call. The return value is non-null
11026 TRUE is returned if it is translated into the proper format for a call to the
11027 external library, and NEW_RETURN is set the tree for that function.
11028 FALSE is returned if processing for the _N variation is required, and
47ae02b7 11029 NEW_RETURN is set to the return value the result is copied into. */
1cd6e20d 11030static bool
11031resolve_overloaded_atomic_exchange (location_t loc, tree function,
f1f41a6c 11032 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11033{
11034 tree p0, p1, p2, p3;
11035 tree I_type, I_type_ptr;
11036 int n = get_atomic_generic_size (loc, function, params);
11037
1d581089 11038 /* Size of 0 is an error condition. */
11039 if (n == 0)
11040 {
11041 *new_return = error_mark_node;
11042 return true;
11043 }
11044
1cd6e20d 11045 /* If not a lock-free size, change to the library generic format. */
a056826c 11046 if (!atomic_size_supported_p (n))
1cd6e20d 11047 {
11048 *new_return = add_atomic_size_parameter (n, loc, function, params);
11049 return true;
11050 }
11051
11052 /* Otherwise there is a lockfree match, transform the call from:
11053 void fn(T* mem, T* desired, T* return, model)
11054 into
11055 *return = (T) (fn (In* mem, (In) *desired, model)) */
11056
f1f41a6c 11057 p0 = (*params)[0];
11058 p1 = (*params)[1];
11059 p2 = (*params)[2];
11060 p3 = (*params)[3];
1cd6e20d 11061
11062 /* Create pointer to appropriate size. */
11063 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11064 I_type_ptr = build_pointer_type (I_type);
11065
11066 /* Convert object pointer to required type. */
11067 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11068 (*params)[0] = p0;
1cd6e20d 11069 /* Convert new value to required type, and dereference it. */
11070 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11071 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 11072 (*params)[1] = p1;
1cd6e20d 11073
11074 /* Move memory model to the 3rd position, and end param list. */
f1f41a6c 11075 (*params)[2] = p3;
11076 params->truncate (3);
1cd6e20d 11077
11078 /* Convert return pointer and dereference it for later assignment. */
11079 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11080
11081 return false;
b6a5fc45 11082}
11083
1cd6e20d 11084
11085/* This will process an __atomic_compare_exchange function call, determine
11086 whether it needs to be mapped to the _N variation, or turned into a lib call.
11087 LOC is the location of the builtin call.
11088 FUNCTION is the DECL that has been invoked;
11089 PARAMS is the argument list for the call. The return value is non-null
11090 TRUE is returned if it is translated into the proper format for a call to the
11091 external library, and NEW_RETURN is set the tree for that function.
11092 FALSE is returned if processing for the _N variation is required. */
11093
11094static bool
11095resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
f1f41a6c 11096 vec<tree, va_gc> *params,
1cd6e20d 11097 tree *new_return)
11098{
11099 tree p0, p1, p2;
11100 tree I_type, I_type_ptr;
11101 int n = get_atomic_generic_size (loc, function, params);
11102
1d581089 11103 /* Size of 0 is an error condition. */
11104 if (n == 0)
11105 {
11106 *new_return = error_mark_node;
11107 return true;
11108 }
11109
1cd6e20d 11110 /* If not a lock-free size, change to the library generic format. */
a056826c 11111 if (!atomic_size_supported_p (n))
1cd6e20d 11112 {
11113 /* The library generic format does not have the weak parameter, so
11114 remove it from the param list. Since a parameter has been removed,
11115 we can be sure that there is room for the SIZE_T parameter, meaning
11116 there will not be a recursive rebuilding of the parameter list, so
11117 there is no danger this will be done twice. */
11118 if (n > 0)
11119 {
f1f41a6c 11120 (*params)[3] = (*params)[4];
11121 (*params)[4] = (*params)[5];
11122 params->truncate (5);
1cd6e20d 11123 }
11124 *new_return = add_atomic_size_parameter (n, loc, function, params);
11125 return true;
11126 }
11127
11128 /* Otherwise, there is a match, so the call needs to be transformed from:
11129 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11130 into
11131 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
ab2c1de8 11132
f1f41a6c 11133 p0 = (*params)[0];
11134 p1 = (*params)[1];
11135 p2 = (*params)[2];
1cd6e20d 11136
11137 /* Create pointer to appropriate size. */
11138 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11139 I_type_ptr = build_pointer_type (I_type);
11140
11141 /* Convert object pointer to required type. */
11142 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11143 (*params)[0] = p0;
1cd6e20d 11144
11145 /* Convert expected pointer to required type. */
11146 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
f1f41a6c 11147 (*params)[1] = p1;
1cd6e20d 11148
11149 /* Convert desired value to required type, and dereference it. */
11150 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11151 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
f1f41a6c 11152 (*params)[2] = p2;
1cd6e20d 11153
11154 /* The rest of the parameters are fine. NULL means no special return value
11155 processing.*/
11156 *new_return = NULL;
11157 return false;
11158}
11159
11160
11161/* This will process an __atomic_load function call, determine whether it
11162 needs to be mapped to the _N variation, or turned into a library call.
11163 LOC is the location of the builtin call.
11164 FUNCTION is the DECL that has been invoked;
11165 PARAMS is the argument list for the call. The return value is non-null
11166 TRUE is returned if it is translated into the proper format for a call to the
11167 external library, and NEW_RETURN is set the tree for that function.
11168 FALSE is returned if processing for the _N variation is required, and
47ae02b7 11169 NEW_RETURN is set to the return value the result is copied into. */
1cd6e20d 11170
11171static bool
11172resolve_overloaded_atomic_load (location_t loc, tree function,
f1f41a6c 11173 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11174{
11175 tree p0, p1, p2;
11176 tree I_type, I_type_ptr;
11177 int n = get_atomic_generic_size (loc, function, params);
11178
1d581089 11179 /* Size of 0 is an error condition. */
11180 if (n == 0)
11181 {
11182 *new_return = error_mark_node;
11183 return true;
11184 }
11185
1cd6e20d 11186 /* If not a lock-free size, change to the library generic format. */
a056826c 11187 if (!atomic_size_supported_p (n))
1cd6e20d 11188 {
11189 *new_return = add_atomic_size_parameter (n, loc, function, params);
11190 return true;
11191 }
11192
11193 /* Otherwise, there is a match, so the call needs to be transformed from:
11194 void fn(T* mem, T* return, model)
11195 into
11196 *return = (T) (fn ((In *) mem, model)) */
11197
f1f41a6c 11198 p0 = (*params)[0];
11199 p1 = (*params)[1];
11200 p2 = (*params)[2];
1cd6e20d 11201
11202 /* Create pointer to appropriate size. */
11203 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11204 I_type_ptr = build_pointer_type (I_type);
11205
11206 /* Convert object pointer to required type. */
11207 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11208 (*params)[0] = p0;
1cd6e20d 11209
11210 /* Move memory model to the 2nd position, and end param list. */
f1f41a6c 11211 (*params)[1] = p2;
11212 params->truncate (2);
1cd6e20d 11213
11214 /* Convert return pointer and dereference it for later assignment. */
11215 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11216
11217 return false;
11218}
11219
11220
11221/* This will process an __atomic_store function call, determine whether it
11222 needs to be mapped to the _N variation, or turned into a library call.
11223 LOC is the location of the builtin call.
11224 FUNCTION is the DECL that has been invoked;
11225 PARAMS is the argument list for the call. The return value is non-null
11226 TRUE is returned if it is translated into the proper format for a call to the
11227 external library, and NEW_RETURN is set the tree for that function.
11228 FALSE is returned if processing for the _N variation is required, and
47ae02b7 11229 NEW_RETURN is set to the return value the result is copied into. */
1cd6e20d 11230
11231static bool
11232resolve_overloaded_atomic_store (location_t loc, tree function,
f1f41a6c 11233 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11234{
11235 tree p0, p1;
11236 tree I_type, I_type_ptr;
11237 int n = get_atomic_generic_size (loc, function, params);
11238
1d581089 11239 /* Size of 0 is an error condition. */
11240 if (n == 0)
11241 {
11242 *new_return = error_mark_node;
11243 return true;
11244 }
11245
1cd6e20d 11246 /* If not a lock-free size, change to the library generic format. */
a056826c 11247 if (!atomic_size_supported_p (n))
1cd6e20d 11248 {
11249 *new_return = add_atomic_size_parameter (n, loc, function, params);
11250 return true;
11251 }
11252
11253 /* Otherwise, there is a match, so the call needs to be transformed from:
11254 void fn(T* mem, T* value, model)
11255 into
11256 fn ((In *) mem, (In) *value, model) */
11257
f1f41a6c 11258 p0 = (*params)[0];
11259 p1 = (*params)[1];
1cd6e20d 11260
11261 /* Create pointer to appropriate size. */
11262 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11263 I_type_ptr = build_pointer_type (I_type);
11264
11265 /* Convert object pointer to required type. */
11266 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11267 (*params)[0] = p0;
1cd6e20d 11268
11269 /* Convert new value to required type, and dereference it. */
11270 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11271 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 11272 (*params)[1] = p1;
1cd6e20d 11273
11274 /* The memory model is in the right spot already. Return is void. */
11275 *new_return = NULL_TREE;
11276
11277 return false;
11278}
11279
11280
b6a5fc45 11281/* Some builtin functions are placeholders for other expressions. This
11282 function should be called immediately after parsing the call expression
11283 before surrounding code has committed to the type of the expression.
11284
e60a6f7b 11285 LOC is the location of the builtin call.
11286
b6a5fc45 11287 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11288 PARAMS is the argument list for the call. The return value is non-null
11289 when expansion is complete, and null if normal processing should
11290 continue. */
11291
11292tree
f1f41a6c 11293resolve_overloaded_builtin (location_t loc, tree function,
11294 vec<tree, va_gc> *params)
b6a5fc45 11295{
11296 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
517906ff 11297
11298 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
11299 Those are not valid to call with a pointer to _Bool (or C++ bool)
11300 and so must be rejected. */
11301 bool fetch_op = true;
1cd6e20d 11302 bool orig_format = true;
11303 tree new_return = NULL_TREE;
11304
65441f6f 11305 switch (DECL_BUILT_IN_CLASS (function))
11306 {
11307 case BUILT_IN_NORMAL:
11308 break;
11309 case BUILT_IN_MD:
11310 if (targetm.resolve_overloaded_builtin)
e60a6f7b 11311 return targetm.resolve_overloaded_builtin (loc, function, params);
65441f6f 11312 else
a0c938f0 11313 return NULL_TREE;
65441f6f 11314 default:
11315 return NULL_TREE;
11316 }
a0c938f0 11317
65441f6f 11318 /* Handle BUILT_IN_NORMAL here. */
b6a5fc45 11319 switch (orig_code)
11320 {
1cd6e20d 11321 case BUILT_IN_ATOMIC_EXCHANGE:
11322 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11323 case BUILT_IN_ATOMIC_LOAD:
11324 case BUILT_IN_ATOMIC_STORE:
11325 {
11326 /* Handle these 4 together so that they can fall through to the next
11327 case if the call is transformed to an _N variant. */
11328 switch (orig_code)
11329 {
11330 case BUILT_IN_ATOMIC_EXCHANGE:
11331 {
11332 if (resolve_overloaded_atomic_exchange (loc, function, params,
11333 &new_return))
11334 return new_return;
11335 /* Change to the _N variant. */
11336 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11337 break;
11338 }
11339
11340 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11341 {
11342 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11343 params,
11344 &new_return))
11345 return new_return;
11346 /* Change to the _N variant. */
11347 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11348 break;
11349 }
11350 case BUILT_IN_ATOMIC_LOAD:
11351 {
11352 if (resolve_overloaded_atomic_load (loc, function, params,
11353 &new_return))
11354 return new_return;
11355 /* Change to the _N variant. */
11356 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11357 break;
11358 }
11359 case BUILT_IN_ATOMIC_STORE:
11360 {
11361 if (resolve_overloaded_atomic_store (loc, function, params,
11362 &new_return))
11363 return new_return;
11364 /* Change to the _N variant. */
11365 orig_code = BUILT_IN_ATOMIC_STORE_N;
11366 break;
11367 }
11368 default:
11369 gcc_unreachable ();
11370 }
11371 /* Fallthrough to the normal processing. */
11372 }
11373 case BUILT_IN_ATOMIC_EXCHANGE_N:
11374 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11375 case BUILT_IN_ATOMIC_LOAD_N:
11376 case BUILT_IN_ATOMIC_STORE_N:
517906ff 11377 {
11378 fetch_op = false;
11379 /* Fallthrough to further processing. */
11380 }
1cd6e20d 11381 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11382 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11383 case BUILT_IN_ATOMIC_AND_FETCH_N:
11384 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11385 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11386 case BUILT_IN_ATOMIC_OR_FETCH_N:
11387 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11388 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11389 case BUILT_IN_ATOMIC_FETCH_AND_N:
11390 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11391 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11392 case BUILT_IN_ATOMIC_FETCH_OR_N:
11393 {
11394 orig_format = false;
11395 /* Fallthru for parameter processing. */
11396 }
2797f13a 11397 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11398 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11399 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11400 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11401 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11402 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11403 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11404 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11405 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11406 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11407 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11408 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11409 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11410 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11411 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11412 case BUILT_IN_SYNC_LOCK_RELEASE_N:
b6a5fc45 11413 {
517906ff 11414 /* The following are not _FETCH_OPs and must be accepted with
11415 pointers to _Bool (or C++ bool). */
11416 if (fetch_op)
11417 fetch_op =
11418 (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11419 && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
11420 && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
11421 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
11422
11423 int n = sync_resolve_size (function, params, fetch_op);
b9c74b4d 11424 tree new_function, first_param, result;
b9a16870 11425 enum built_in_function fncode;
b6a5fc45 11426
11427 if (n == 0)
11428 return error_mark_node;
11429
b9a16870 11430 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11431 new_function = builtin_decl_explicit (fncode);
1cd6e20d 11432 if (!sync_resolve_params (loc, function, new_function, params,
11433 orig_format))
b6a5fc45 11434 return error_mark_node;
11435
f1f41a6c 11436 first_param = (*params)[0];
ec761d5a 11437 result = build_function_call_vec (loc, vNULL, new_function, params,
11438 NULL);
1cd6e20d 11439 if (result == error_mark_node)
11440 return result;
2797f13a 11441 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
1cd6e20d 11442 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11443 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11444 result = sync_resolve_return (first_param, result, orig_format);
b6a5fc45 11445
1cd6e20d 11446 /* If new_return is set, assign function to that expr and cast the
11447 result to void since the generic interface returned void. */
11448 if (new_return)
11449 {
11450 /* Cast function result from I{1,2,4,8,16} to the required type. */
11451 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11452 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11453 result);
11454 TREE_SIDE_EFFECTS (result) = 1;
11455 protected_set_expr_location (result, loc);
11456 result = convert (void_type_node, result);
11457 }
b6a5fc45 11458 return result;
11459 }
11460
11461 default:
65441f6f 11462 return NULL_TREE;
b6a5fc45 11463 }
11464}
11465
73437615 11466/* vector_types_compatible_elements_p is used in type checks of vectors
11467 values used as operands of binary operators. Where it returns true, and
11468 the other checks of the caller succeed (being vector types in he first
11469 place, and matching number of elements), we can just treat the types
11470 as essentially the same.
11471 Contrast with vector_targets_convertible_p, which is used for vector
11472 pointer types, and vector_types_convertible_p, which will allow
11473 language-specific matches under the control of flag_lax_vector_conversions,
11474 and might still require a conversion. */
11475/* True if vector types T1 and T2 can be inputs to the same binary
11476 operator without conversion.
11477 We don't check the overall vector size here because some of our callers
11478 want to give different error messages when the vectors are compatible
11479 except for the element count. */
11480
491255f5 11481bool
73437615 11482vector_types_compatible_elements_p (tree t1, tree t2)
491255f5 11483{
73437615 11484 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11485 t1 = TREE_TYPE (t1);
11486 t2 = TREE_TYPE (t2);
11487
491255f5 11488 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11489
9421ebb9 11490 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11491 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11492 || c2 == FIXED_POINT_TYPE));
491255f5 11493
73437615 11494 t1 = c_common_signed_type (t1);
11495 t2 = c_common_signed_type (t2);
491255f5 11496 /* Equality works here because c_common_signed_type uses
11497 TYPE_MAIN_VARIANT. */
73437615 11498 if (t1 == t2)
11499 return true;
11500 if (opaque && c1 == c2
11501 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11502 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11503 return true;
11504 return false;
491255f5 11505}
11506
be7350e7 11507/* Check for missing format attributes on function pointers. LTYPE is
11508 the new type or left-hand side type. RTYPE is the old type or
11509 right-hand side type. Returns TRUE if LTYPE is missing the desired
11510 attribute. */
11511
11512bool
11513check_missing_format_attribute (tree ltype, tree rtype)
11514{
11515 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11516 tree ra;
11517
11518 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11519 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11520 break;
11521 if (ra)
11522 {
11523 tree la;
11524 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11525 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11526 break;
11527 return !la;
11528 }
11529 else
11530 return false;
11531}
11532
2840aae4 11533/* Subscripting with type char is likely to lose on a machine where
11534 chars are signed. So warn on any machine, but optionally. Don't
11535 warn for unsigned char since that type is safe. Don't warn for
11536 signed char because anyone who uses that must have done so
11537 deliberately. Furthermore, we reduce the false positive load by
11538 warning only for non-constant value of type char. */
11539
11540void
92b63884 11541warn_array_subscript_with_type_char (location_t loc, tree index)
2840aae4 11542{
11543 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11544 && TREE_CODE (index) != INTEGER_CST)
92b63884 11545 warning_at (loc, OPT_Wchar_subscripts,
11546 "array subscript has type %<char%>");
2840aae4 11547}
11548
e534436e 11549/* Implement -Wparentheses for the unexpected C precedence rules, to
11550 cover cases like x + y << z which readers are likely to
11551 misinterpret. We have seen an expression in which CODE is a binary
82012ffe 11552 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11553 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11554 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11555 expression was not formed using a binary or unary operator, or it
11556 was enclosed in parentheses. */
e534436e 11557
11558void
b0e7825e 11559warn_about_parentheses (location_t loc, enum tree_code code,
269f7979 11560 enum tree_code code_left, tree arg_left,
82012ffe 11561 enum tree_code code_right, tree arg_right)
e534436e 11562{
11563 if (!warn_parentheses)
11564 return;
11565
82012ffe 11566 /* This macro tests that the expression ARG with original tree code
11567 CODE appears to be a boolean expression. or the result of folding a
11568 boolean expression. */
11569#define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11570 (truth_value_p (TREE_CODE (ARG)) \
11571 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11572 /* Folding may create 0 or 1 integers from other expressions. */ \
11573 || ((CODE) != INTEGER_CST \
11574 && (integer_onep (ARG) || integer_zerop (ARG))))
11575
48e1416a 11576 switch (code)
e534436e 11577 {
82012ffe 11578 case LSHIFT_EXPR:
b0e7825e 11579 if (code_left == PLUS_EXPR)
11580 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11581 "suggest parentheses around %<+%> inside %<<<%>");
11582 else if (code_right == PLUS_EXPR)
11583 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11584 "suggest parentheses around %<+%> inside %<<<%>");
11585 else if (code_left == MINUS_EXPR)
11586 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11587 "suggest parentheses around %<-%> inside %<<<%>");
11588 else if (code_right == MINUS_EXPR)
11589 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11590 "suggest parentheses around %<-%> inside %<<<%>");
82012ffe 11591 return;
e534436e 11592
82012ffe 11593 case RSHIFT_EXPR:
b0e7825e 11594 if (code_left == PLUS_EXPR)
11595 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11596 "suggest parentheses around %<+%> inside %<>>%>");
11597 else if (code_right == PLUS_EXPR)
11598 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11599 "suggest parentheses around %<+%> inside %<>>%>");
11600 else if (code_left == MINUS_EXPR)
11601 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11602 "suggest parentheses around %<-%> inside %<>>%>");
11603 else if (code_right == MINUS_EXPR)
11604 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11605 "suggest parentheses around %<-%> inside %<>>%>");
82012ffe 11606 return;
e534436e 11607
82012ffe 11608 case TRUTH_ORIF_EXPR:
b0e7825e 11609 if (code_left == TRUTH_ANDIF_EXPR)
11610 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11611 "suggest parentheses around %<&&%> within %<||%>");
11612 else if (code_right == TRUTH_ANDIF_EXPR)
11613 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11614 "suggest parentheses around %<&&%> within %<||%>");
82012ffe 11615 return;
11616
11617 case BIT_IOR_EXPR:
e534436e 11618 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
b0e7825e 11619 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11620 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11621 "suggest parentheses around arithmetic in operand of %<|%>");
11622 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11623 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11624 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11625 "suggest parentheses around arithmetic in operand of %<|%>");
e534436e 11626 /* Check cases like x|y==z */
b0e7825e 11627 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11628 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11629 "suggest parentheses around comparison in operand of %<|%>");
11630 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11631 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11632 "suggest parentheses around comparison in operand of %<|%>");
11633 /* Check cases like !x | y */
11634 else if (code_left == TRUTH_NOT_EXPR
11635 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 11636 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11637 "suggest parentheses around operand of "
11638 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
82012ffe 11639 return;
e534436e 11640
82012ffe 11641 case BIT_XOR_EXPR:
e534436e 11642 if (code_left == BIT_AND_EXPR
b0e7825e 11643 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11644 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11645 "suggest parentheses around arithmetic in operand of %<^%>");
11646 else if (code_right == BIT_AND_EXPR
11647 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11648 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11649 "suggest parentheses around arithmetic in operand of %<^%>");
e534436e 11650 /* Check cases like x^y==z */
b0e7825e 11651 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11652 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11653 "suggest parentheses around comparison in operand of %<^%>");
11654 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11655 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11656 "suggest parentheses around comparison in operand of %<^%>");
11657 return;
e534436e 11658
82012ffe 11659 case BIT_AND_EXPR:
b0e7825e 11660 if (code_left == PLUS_EXPR)
11661 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11662 "suggest parentheses around %<+%> in operand of %<&%>");
11663 else if (code_right == PLUS_EXPR)
11664 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11665 "suggest parentheses around %<+%> in operand of %<&%>");
b0e7825e 11666 else if (code_left == MINUS_EXPR)
11667 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11668 "suggest parentheses around %<-%> in operand of %<&%>");
11669 else if (code_right == MINUS_EXPR)
11670 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11671 "suggest parentheses around %<-%> in operand of %<&%>");
e534436e 11672 /* Check cases like x&y==z */
b0e7825e 11673 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11674 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11675 "suggest parentheses around comparison in operand of %<&%>");
11676 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11677 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11678 "suggest parentheses around comparison in operand of %<&%>");
11679 /* Check cases like !x & y */
11680 else if (code_left == TRUTH_NOT_EXPR
11681 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 11682 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11683 "suggest parentheses around operand of "
11684 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
82012ffe 11685 return;
e534436e 11686
82012ffe 11687 case EQ_EXPR:
b0e7825e 11688 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11689 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11690 "suggest parentheses around comparison in operand of %<==%>");
11691 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11692 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11693 "suggest parentheses around comparison in operand of %<==%>");
11694 return;
11695 case NE_EXPR:
b0e7825e 11696 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;
11703
11704 default:
b0e7825e 11705 if (TREE_CODE_CLASS (code) == tcc_comparison)
11706 {
11707 if (TREE_CODE_CLASS (code_left) == tcc_comparison
269f7979 11708 && code_left != NE_EXPR && code_left != EQ_EXPR
11709 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
b0e7825e 11710 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11711 "comparisons like %<X<=Y<=Z%> do not "
11712 "have their mathematical meaning");
11713 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
269f7979 11714 && code_right != NE_EXPR && code_right != EQ_EXPR
b0e7825e 11715 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11716 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11717 "comparisons like %<X<=Y<=Z%> do not "
11718 "have their mathematical meaning");
11719 }
82012ffe 11720 return;
6ce0c450 11721 }
82012ffe 11722#undef NOT_A_BOOLEAN_EXPR_P
e534436e 11723}
11724
92fccaaa 11725/* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11726
11727void
11728warn_for_unused_label (tree label)
11729{
11730 if (!TREE_USED (label))
11731 {
11732 if (DECL_INITIAL (label))
11733 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11734 else
11735 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11736 }
11737}
2840aae4 11738
b6889cb0 11739/* Warn for division by zero according to the value of DIVISOR. LOC
11740 is the location of the division operator. */
f092582b 11741
11742void
b6889cb0 11743warn_for_div_by_zero (location_t loc, tree divisor)
f092582b 11744{
9421ebb9 11745 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11746 about division by zero. Do not issue a warning if DIVISOR has a
f092582b 11747 floating-point type, since we consider 0.0/0.0 a valid way of
11748 generating a NaN. */
48d94ede 11749 if (c_inhibit_evaluation_warnings == 0
9421ebb9 11750 && (integer_zerop (divisor) || fixed_zerop (divisor)))
b6889cb0 11751 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
f092582b 11752}
11753
13869a99 11754/* Subroutine of build_binary_op. Give warnings for comparisons
11755 between signed and unsigned quantities that may fail. Do the
11756 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11757 so that casts will be considered, but default promotions won't
8e70fb09 11758 be.
11759
11760 LOCATION is the location of the comparison operator.
13869a99 11761
11762 The arguments of this function map directly to local variables
11763 of build_binary_op. */
11764
48e1416a 11765void
8e70fb09 11766warn_for_sign_compare (location_t location,
48e1416a 11767 tree orig_op0, tree orig_op1,
11768 tree op0, tree op1,
13869a99 11769 tree result_type, enum tree_code resultcode)
11770{
11771 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11772 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11773 int unsignedp0, unsignedp1;
48e1416a 11774
13869a99 11775 /* In C++, check for comparison of different enum types. */
11776 if (c_dialect_cxx()
11777 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11778 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11779 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
895b662f 11780 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
13869a99 11781 {
8e70fb09 11782 warning_at (location,
11783 OPT_Wsign_compare, "comparison between types %qT and %qT",
11784 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
13869a99 11785 }
11786
11787 /* Do not warn if the comparison is being done in a signed type,
11788 since the signed type will only be chosen if it can represent
11789 all the values of the unsigned type. */
11790 if (!TYPE_UNSIGNED (result_type))
11791 /* OK */;
11792 /* Do not warn if both operands are unsigned. */
11793 else if (op0_signed == op1_signed)
11794 /* OK */;
11795 else
11796 {
895b662f 11797 tree sop, uop, base_type;
13869a99 11798 bool ovf;
895b662f 11799
13869a99 11800 if (op0_signed)
11801 sop = orig_op0, uop = orig_op1;
48e1416a 11802 else
13869a99 11803 sop = orig_op1, uop = orig_op0;
11804
48e1416a 11805 STRIP_TYPE_NOPS (sop);
13869a99 11806 STRIP_TYPE_NOPS (uop);
895b662f 11807 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11808 ? TREE_TYPE (result_type) : result_type);
13869a99 11809
11810 /* Do not warn if the signed quantity is an unsuffixed integer
11811 literal (or some static constant expression involving such
11812 literals or a conditional expression involving such literals)
11813 and it is non-negative. */
11814 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11815 /* OK */;
11816 /* Do not warn if the comparison is an equality operation, the
11817 unsigned quantity is an integral constant, and it would fit
11818 in the result if the result were signed. */
11819 else if (TREE_CODE (uop) == INTEGER_CST
11820 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
895b662f 11821 && int_fits_type_p (uop, c_common_signed_type (base_type)))
13869a99 11822 /* OK */;
11823 /* In C, do not warn if the unsigned quantity is an enumeration
11824 constant and its maximum value would fit in the result if the
11825 result were signed. */
11826 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11827 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11828 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
895b662f 11829 c_common_signed_type (base_type)))
13869a99 11830 /* OK */;
48e1416a 11831 else
8e70fb09 11832 warning_at (location,
48e1416a 11833 OPT_Wsign_compare,
8e70fb09 11834 "comparison between signed and unsigned integer expressions");
13869a99 11835 }
48e1416a 11836
13869a99 11837 /* Warn if two unsigned values are being compared in a size larger
11838 than their original size, and one (and only one) is the result of
11839 a `~' operator. This comparison will always fail.
48e1416a 11840
13869a99 11841 Also warn if one operand is a constant, and the constant does not
11842 have all bits set that are set in the ~ operand when it is
11843 extended. */
11844
7f506bca 11845 op0 = c_common_get_narrower (op0, &unsignedp0);
11846 op1 = c_common_get_narrower (op1, &unsignedp1);
48e1416a 11847
13869a99 11848 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11849 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11850 {
11851 if (TREE_CODE (op0) == BIT_NOT_EXPR)
7f506bca 11852 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
13869a99 11853 if (TREE_CODE (op1) == BIT_NOT_EXPR)
7f506bca 11854 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
13869a99 11855
e913b5cd 11856 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
13869a99 11857 {
11858 tree primop;
11859 HOST_WIDE_INT constant, mask;
11860 int unsignedp;
11861 unsigned int bits;
48e1416a 11862
e913b5cd 11863 if (tree_fits_shwi_p (op0))
13869a99 11864 {
11865 primop = op1;
11866 unsignedp = unsignedp1;
e913b5cd 11867 constant = tree_to_shwi (op0);
13869a99 11868 }
11869 else
11870 {
11871 primop = op0;
11872 unsignedp = unsignedp0;
e913b5cd 11873 constant = tree_to_shwi (op1);
13869a99 11874 }
48e1416a 11875
13869a99 11876 bits = TYPE_PRECISION (TREE_TYPE (primop));
11877 if (bits < TYPE_PRECISION (result_type)
11878 && bits < HOST_BITS_PER_LONG && unsignedp)
11879 {
85f5e2ee 11880 mask = (~ (unsigned HOST_WIDE_INT) 0) << bits;
13869a99 11881 if ((mask & constant) != mask)
11882 {
11883 if (constant == 0)
76fdceeb 11884 warning_at (location, OPT_Wsign_compare,
11885 "promoted ~unsigned is always non-zero");
13869a99 11886 else
48e1416a 11887 warning_at (location, OPT_Wsign_compare,
8e70fb09 11888 "comparison of promoted ~unsigned with constant");
13869a99 11889 }
11890 }
11891 }
11892 else if (unsignedp0 && unsignedp1
11893 && (TYPE_PRECISION (TREE_TYPE (op0))
11894 < TYPE_PRECISION (result_type))
11895 && (TYPE_PRECISION (TREE_TYPE (op1))
11896 < TYPE_PRECISION (result_type)))
8e70fb09 11897 warning_at (location, OPT_Wsign_compare,
13869a99 11898 "comparison of promoted ~unsigned with unsigned");
11899 }
11900}
11901
5ba33bf4 11902/* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
11903 type via c_common_type. If -Wdouble-promotion is in use, and the
11904 conditions for warning have been met, issue a warning. GMSGID is
11905 the warning message. It must have two %T specifiers for the type
11906 that was converted (generally "float") and the type to which it was
11907 converted (generally "double), respectively. LOC is the location
11908 to which the awrning should refer. */
11909
11910void
11911do_warn_double_promotion (tree result_type, tree type1, tree type2,
11912 const char *gmsgid, location_t loc)
11913{
11914 tree source_type;
11915
11916 if (!warn_double_promotion)
11917 return;
11918 /* If the conversion will not occur at run-time, there is no need to
11919 warn about it. */
11920 if (c_inhibit_evaluation_warnings)
11921 return;
11922 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
11923 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
11924 return;
11925 if (TYPE_MAIN_VARIANT (type1) == float_type_node
11926 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
11927 source_type = type1;
11928 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
11929 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
11930 source_type = type2;
11931 else
11932 return;
11933 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
11934}
11935
0949f227 11936/* Possibly warn about unused parameters. */
11937
11938void
11939do_warn_unused_parameter (tree fn)
11940{
11941 tree decl;
11942
11943 for (decl = DECL_ARGUMENTS (fn);
11944 decl; decl = DECL_CHAIN (decl))
11945 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
11946 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
11947 && !TREE_NO_WARNING (decl))
11948 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
11949 "unused parameter %qD", decl);
11950}
11951
11952
41771881 11953/* Setup a TYPE_DECL node as a typedef representation.
11954
11955 X is a TYPE_DECL for a typedef statement. Create a brand new
11956 ..._TYPE node (which will be just a variant of the existing
11957 ..._TYPE node with identical properties) and then install X
11958 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
11959
11960 The whole point here is to end up with a situation where each
11961 and every ..._TYPE node the compiler creates will be uniquely
11962 associated with AT MOST one node representing a typedef name.
11963 This way, even though the compiler substitutes corresponding
11964 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
11965 early on, later parts of the compiler can always do the reverse
11966 translation and get back the corresponding typedef name. For
11967 example, given:
ab2c1de8 11968
41771881 11969 typedef struct S MY_TYPE;
11970 MY_TYPE object;
11971
11972 Later parts of the compiler might only know that `object' was of
11973 type `struct S' if it were not for code just below. With this
11974 code however, later parts of the compiler see something like:
11975
11976 struct S' == struct S
11977 typedef struct S' MY_TYPE;
11978 struct S' object;
11979
11980 And they can then deduce (from the node for type struct S') that
11981 the original object declaration was:
11982
11983 MY_TYPE object;
11984
11985 Being able to do this is important for proper support of protoize,
11986 and also for generating precise symbolic debugging information
11987 which takes full account of the programmer's (typedef) vocabulary.
11988
11989 Obviously, we don't want to generate a duplicate ..._TYPE node if
11990 the TYPE_DECL node that we are now processing really represents a
11991 standard built-in type. */
11992
11993void
11994set_underlying_type (tree x)
11995{
11996 if (x == error_mark_node)
11997 return;
11998 if (DECL_IS_BUILTIN (x))
11999 {
12000 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12001 TYPE_NAME (TREE_TYPE (x)) = x;
12002 }
12003 else if (TREE_TYPE (x) != error_mark_node
12004 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12005 {
12006 tree tt = TREE_TYPE (x);
12007 DECL_ORIGINAL_TYPE (x) = tt;
12008 tt = build_variant_type_copy (tt);
12009 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12010 TYPE_NAME (tt) = x;
12011 TREE_USED (tt) = TREE_USED (x);
12012 TREE_TYPE (x) = tt;
12013 }
12014}
12015
1a4c44c5 12016/* Record the types used by the current global variable declaration
12017 being parsed, so that we can decide later to emit their debug info.
12018 Those types are in types_used_by_cur_var_decl, and we are going to
12019 store them in the types_used_by_vars_hash hash table.
12020 DECL is the declaration of the global variable that has been parsed. */
12021
12022void
12023record_types_used_by_current_var_decl (tree decl)
12024{
12025 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12026
f1f41a6c 12027 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
1a4c44c5 12028 {
f1f41a6c 12029 tree type = types_used_by_cur_var_decl->pop ();
aef48c9a 12030 types_used_by_var_decl_insert (type, decl);
1a4c44c5 12031 }
12032}
12033
a4e3ffad 12034/* If DECL is a typedef that is declared in the current function,
12035 record it for the purpose of -Wunused-local-typedefs. */
12036
12037void
12038record_locally_defined_typedef (tree decl)
12039{
12040 struct c_language_function *l;
12041
12042 if (!warn_unused_local_typedefs
12043 || cfun == NULL
12044 /* if this is not a locally defined typedef then we are not
12045 interested. */
12046 || !is_typedef_decl (decl)
12047 || !decl_function_context (decl))
12048 return;
12049
12050 l = (struct c_language_function *) cfun->language;
f1f41a6c 12051 vec_safe_push (l->local_typedefs, decl);
a4e3ffad 12052}
12053
12054/* If T is a TYPE_DECL declared locally, mark it as used. */
12055
12056void
12057maybe_record_typedef_use (tree t)
12058{
12059 if (!is_typedef_decl (t))
12060 return;
12061
12062 TREE_USED (t) = true;
12063}
12064
12065/* Warn if there are some unused locally defined typedefs in the
12066 current function. */
12067
12068void
12069maybe_warn_unused_local_typedefs (void)
12070{
12071 int i;
12072 tree decl;
12073 /* The number of times we have emitted -Wunused-local-typedefs
12074 warnings. If this is different from errorcount, that means some
12075 unrelated errors have been issued. In which case, we'll avoid
12076 emitting "unused-local-typedefs" warnings. */
12077 static int unused_local_typedefs_warn_count;
12078 struct c_language_function *l;
12079
12080 if (cfun == NULL)
12081 return;
12082
12083 if ((l = (struct c_language_function *) cfun->language) == NULL)
12084 return;
12085
12086 if (warn_unused_local_typedefs
12087 && errorcount == unused_local_typedefs_warn_count)
12088 {
f1f41a6c 12089 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
a4e3ffad 12090 if (!TREE_USED (decl))
12091 warning_at (DECL_SOURCE_LOCATION (decl),
12092 OPT_Wunused_local_typedefs,
12093 "typedef %qD locally defined but not used", decl);
12094 unused_local_typedefs_warn_count = errorcount;
12095 }
12096
f1f41a6c 12097 vec_free (l->local_typedefs);
a4e3ffad 12098}
12099
78bf4156 12100/* Warn about boolean expression compared with an integer value different
12101 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12102 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12103 are the operands of the comparison. The caller must ensure that
12104 either operand is a boolean expression. */
12105
12106void
12107maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12108 tree op1)
12109{
12110 if (TREE_CODE_CLASS (code) != tcc_comparison)
12111 return;
12112
9c691dbd 12113 tree f, cst;
12114 if (f = fold_for_warn (op0),
12115 TREE_CODE (f) == INTEGER_CST)
12116 cst = op0 = f;
12117 else if (f = fold_for_warn (op1),
12118 TREE_CODE (f) == INTEGER_CST)
12119 cst = op1 = f;
12120 else
78bf4156 12121 return;
12122
12123 if (!integer_zerop (cst) && !integer_onep (cst))
12124 {
a720ab1c 12125 int sign = (TREE_CODE (op0) == INTEGER_CST
12126 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
78bf4156 12127 if (code == EQ_EXPR
12128 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12129 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12130 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12131 "with boolean expression is always false", cst);
12132 else
12133 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12134 "with boolean expression is always true", cst);
12135 }
a720ab1c 12136 else if (integer_zerop (cst) || integer_onep (cst))
12137 {
14744a16 12138 /* If the non-constant operand isn't of a boolean type, we
12139 don't want to warn here. */
12140 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12141 /* Handle booleans promoted to integers. */
12142 if (CONVERT_EXPR_P (noncst)
12143 && TREE_TYPE (noncst) == integer_type_node
12144 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
12145 /* Warn. */;
12146 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12147 && !truth_value_p (TREE_CODE (noncst)))
12148 return;
a720ab1c 12149 /* Do some magic to get the right diagnostics. */
12150 bool flag = TREE_CODE (op0) == INTEGER_CST;
12151 flag = integer_zerop (cst) ? flag : !flag;
12152 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12153 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12154 "with boolean expression is always true", cst);
12155 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12156 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12157 "with boolean expression is always false", cst);
12158 }
78bf4156 12159}
12160
8d669e79 12161/* Warn if signed left shift overflows. We don't warn
12162 about left-shifting 1 into the sign bit in C++14; cf.
12163 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
12164 LOC is a location of the shift; OP0 and OP1 are the operands.
12165 Return true if an overflow is detected, false otherwise. */
12166
12167bool
12168maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
12169{
12170 if (TREE_CODE (op0) != INTEGER_CST
12171 || TREE_CODE (op1) != INTEGER_CST)
12172 return false;
12173
12174 tree type0 = TREE_TYPE (op0);
12175 unsigned int prec0 = TYPE_PRECISION (type0);
12176
12177 /* Left-hand operand must be signed. */
12178 if (TYPE_UNSIGNED (type0))
12179 return false;
12180
f4809955 12181 unsigned int min_prec = (wi::min_precision (op0, SIGNED)
12182 + TREE_INT_CST_LOW (op1));
99e82b02 12183 /* Handle the case of left-shifting 1 into the sign bit.
12184 * However, shifting 1 _out_ of the sign bit, as in
12185 * INT_MIN << 1, is considered an overflow.
12186 */
12187 if (!tree_int_cst_sign_bit(op0) && min_prec == prec0 + 1)
8d669e79 12188 {
12189 /* Never warn for C++14 onwards. */
12190 if (cxx_dialect >= cxx14)
12191 return false;
12192 /* Otherwise only if -Wshift-overflow=2. But return
12193 true to signal an overflow for the sake of integer
12194 constant expressions. */
12195 if (warn_shift_overflow < 2)
12196 return true;
12197 }
12198
8d669e79 12199 bool overflowed = min_prec > prec0;
12200 if (overflowed && c_inhibit_evaluation_warnings == 0)
12201 warning_at (loc, OPT_Wshift_overflow_,
12202 "result of %qE requires %u bits to represent, "
12203 "but %qT only has %u bits",
12204 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
12205 min_prec, type0, prec0);
12206
12207 return overflowed;
12208}
12209
f352a3fb 12210/* The C and C++ parsers both use vectors to hold function arguments.
12211 For efficiency, we keep a cache of unused vectors. This is the
12212 cache. */
12213
f1f41a6c 12214typedef vec<tree, va_gc> *tree_gc_vec;
12215static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
f352a3fb 12216
12217/* Return a new vector from the cache. If the cache is empty,
12218 allocate a new vector. These vectors are GC'ed, so it is OK if the
12219 pointer is not released.. */
12220
f1f41a6c 12221vec<tree, va_gc> *
f352a3fb 12222make_tree_vector (void)
12223{
f1f41a6c 12224 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12225 return tree_vector_cache->pop ();
f352a3fb 12226 else
12227 {
f1f41a6c 12228 /* Passing 0 to vec::alloc returns NULL, and our callers require
f352a3fb 12229 that we always return a non-NULL value. The vector code uses
12230 4 when growing a NULL vector, so we do too. */
f1f41a6c 12231 vec<tree, va_gc> *v;
12232 vec_alloc (v, 4);
12233 return v;
f352a3fb 12234 }
12235}
12236
12237/* Release a vector of trees back to the cache. */
12238
12239void
f1f41a6c 12240release_tree_vector (vec<tree, va_gc> *vec)
f352a3fb 12241{
12242 if (vec != NULL)
12243 {
f1f41a6c 12244 vec->truncate (0);
12245 vec_safe_push (tree_vector_cache, vec);
f352a3fb 12246 }
12247}
12248
12249/* Get a new tree vector holding a single tree. */
12250
f1f41a6c 12251vec<tree, va_gc> *
f352a3fb 12252make_tree_vector_single (tree t)
12253{
f1f41a6c 12254 vec<tree, va_gc> *ret = make_tree_vector ();
12255 ret->quick_push (t);
f352a3fb 12256 return ret;
12257}
12258
c66c81be 12259/* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12260
f1f41a6c 12261vec<tree, va_gc> *
c66c81be 12262make_tree_vector_from_list (tree list)
12263{
f1f41a6c 12264 vec<tree, va_gc> *ret = make_tree_vector ();
c66c81be 12265 for (; list; list = TREE_CHAIN (list))
f1f41a6c 12266 vec_safe_push (ret, TREE_VALUE (list));
c66c81be 12267 return ret;
12268}
12269
f352a3fb 12270/* Get a new tree vector which is a copy of an existing one. */
12271
f1f41a6c 12272vec<tree, va_gc> *
12273make_tree_vector_copy (const vec<tree, va_gc> *orig)
f352a3fb 12274{
f1f41a6c 12275 vec<tree, va_gc> *ret;
f352a3fb 12276 unsigned int ix;
12277 tree t;
12278
12279 ret = make_tree_vector ();
f1f41a6c 12280 vec_safe_reserve (ret, vec_safe_length (orig));
12281 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12282 ret->quick_push (t);
f352a3fb 12283 return ret;
12284}
12285
a9ffdd35 12286/* Return true if KEYWORD starts a type specifier. */
12287
12288bool
12289keyword_begins_type_specifier (enum rid keyword)
12290{
12291 switch (keyword)
12292 {
4fba5eb9 12293 case RID_AUTO_TYPE:
a9ffdd35 12294 case RID_INT:
12295 case RID_CHAR:
12296 case RID_FLOAT:
12297 case RID_DOUBLE:
12298 case RID_VOID:
a9ffdd35 12299 case RID_UNSIGNED:
12300 case RID_LONG:
12301 case RID_SHORT:
12302 case RID_SIGNED:
12303 case RID_DFLOAT32:
12304 case RID_DFLOAT64:
12305 case RID_DFLOAT128:
12306 case RID_FRACT:
12307 case RID_ACCUM:
12308 case RID_BOOL:
12309 case RID_WCHAR:
12310 case RID_CHAR16:
12311 case RID_CHAR32:
12312 case RID_SAT:
12313 case RID_COMPLEX:
12314 case RID_TYPEOF:
12315 case RID_STRUCT:
12316 case RID_CLASS:
12317 case RID_UNION:
12318 case RID_ENUM:
12319 return true;
12320 default:
9f75f026 12321 if (keyword >= RID_FIRST_INT_N
12322 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12323 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12324 return true;
a9ffdd35 12325 return false;
12326 }
12327}
12328
12329/* Return true if KEYWORD names a type qualifier. */
12330
12331bool
12332keyword_is_type_qualifier (enum rid keyword)
12333{
12334 switch (keyword)
12335 {
12336 case RID_CONST:
12337 case RID_VOLATILE:
12338 case RID_RESTRICT:
b560fabd 12339 case RID_ATOMIC:
a9ffdd35 12340 return true;
12341 default:
12342 return false;
12343 }
12344}
12345
12346/* Return true if KEYWORD names a storage class specifier.
12347
12348 RID_TYPEDEF is not included in this list despite `typedef' being
12349 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12350 such for syntactic convenience only. */
12351
12352bool
12353keyword_is_storage_class_specifier (enum rid keyword)
12354{
12355 switch (keyword)
12356 {
12357 case RID_STATIC:
12358 case RID_EXTERN:
12359 case RID_REGISTER:
12360 case RID_AUTO:
12361 case RID_MUTABLE:
12362 case RID_THREAD:
12363 return true;
12364 default:
12365 return false;
12366 }
12367}
12368
fad3f658 12369/* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12370
12371static bool
12372keyword_is_function_specifier (enum rid keyword)
12373{
12374 switch (keyword)
12375 {
12376 case RID_INLINE:
985c6e3a 12377 case RID_NORETURN:
fad3f658 12378 case RID_VIRTUAL:
12379 case RID_EXPLICIT:
12380 return true;
12381 default:
12382 return false;
12383 }
12384}
12385
12386/* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12387 declaration-specifier (C99 6.7). */
12388
12389bool
12390keyword_is_decl_specifier (enum rid keyword)
12391{
12392 if (keyword_is_storage_class_specifier (keyword)
12393 || keyword_is_type_qualifier (keyword)
12394 || keyword_is_function_specifier (keyword))
12395 return true;
12396
12397 switch (keyword)
12398 {
12399 case RID_TYPEDEF:
12400 case RID_FRIEND:
12401 case RID_CONSTEXPR:
12402 return true;
12403 default:
12404 return false;
12405 }
12406}
12407
9b88d08d 12408/* Initialize language-specific-bits of tree_contains_struct. */
12409
12410void
12411c_common_init_ts (void)
12412{
12413 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12414 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
3c6d4197 12415 MARK_TS_TYPED (ARRAY_NOTATION_REF);
9b88d08d 12416}
12417
244db24d 12418/* Build a user-defined numeric literal out of an integer constant type VALUE
12419 with identifier SUFFIX. */
12420
12421tree
324ca377 12422build_userdef_literal (tree suffix_id, tree value,
12423 enum overflow_type overflow, tree num_string)
244db24d 12424{
12425 tree literal = make_node (USERDEF_LITERAL);
12426 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12427 USERDEF_LITERAL_VALUE (literal) = value;
324ca377 12428 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
244db24d 12429 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12430 return literal;
12431}
12432
7059d45d 12433/* For vector[index], convert the vector to a
c61ef207 12434 pointer of the underlying type. Return true if the resulting
12435 ARRAY_REF should not be an lvalue. */
12436
12437bool
7059d45d 12438convert_vector_to_pointer_for_subscript (location_t loc,
c61ef207 12439 tree *vecp, tree index)
7059d45d 12440{
c61ef207 12441 bool ret = false;
6290f0db 12442 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
7059d45d 12443 {
12444 tree type = TREE_TYPE (*vecp);
12445 tree type1;
12446
c61ef207 12447 ret = !lvalue_p (*vecp);
7059d45d 12448 if (TREE_CODE (index) == INTEGER_CST)
e913b5cd 12449 if (!tree_fits_uhwi_p (index)
aa59f000 12450 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7059d45d 12451 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12452
c61ef207 12453 if (ret)
12454 {
f9e245b2 12455 tree tmp = create_tmp_var_raw (type);
c61ef207 12456 DECL_SOURCE_LOCATION (tmp) = loc;
12457 *vecp = c_save_expr (*vecp);
12458 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12459 {
12460 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12461 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12462 *vecp
12463 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12464 *vecp, NULL_TREE, NULL_TREE),
12465 non_const);
12466 }
12467 else
12468 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12469 NULL_TREE, NULL_TREE);
12470 SET_EXPR_LOCATION (*vecp, loc);
12471 c_common_mark_addressable_vec (tmp);
12472 }
12473 else
12474 c_common_mark_addressable_vec (*vecp);
7059d45d 12475 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7059d45d 12476 type1 = build_pointer_type (TREE_TYPE (*vecp));
b2ca6510 12477 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12478 if (!ref_all
12479 && !DECL_P (*vecp))
12480 {
12481 /* If the original vector isn't declared may_alias and it
12482 isn't a bare vector look if the subscripting would
12483 alias the vector we subscript, and if not, force ref-all. */
12484 alias_set_type vecset = get_alias_set (*vecp);
12485 alias_set_type sset = get_alias_set (type);
12486 if (!alias_sets_must_conflict_p (sset, vecset)
12487 && !alias_set_subset_of (sset, vecset))
12488 ref_all = true;
12489 }
12490 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
7059d45d 12491 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12492 *vecp = convert (type, *vecp);
12493 }
c61ef207 12494 return ret;
7059d45d 12495}
12496
41ed701a 12497/* Determine which of the operands, if any, is a scalar that needs to be
12498 converted to a vector, for the range of operations. */
12499enum stv_conv
12500scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12501 bool complain)
12502{
12503 tree type0 = TREE_TYPE (op0);
12504 tree type1 = TREE_TYPE (op1);
12505 bool integer_only_op = false;
12506 enum stv_conv ret = stv_firstarg;
12507
6290f0db 12508 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
41ed701a 12509 switch (code)
12510 {
12511 /* Most GENERIC binary expressions require homogeneous arguments.
12512 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12513 argument that is a vector and a second one that is a scalar, so
12514 we never return stv_secondarg for them. */
12515 case RSHIFT_EXPR:
12516 case LSHIFT_EXPR:
12517 if (TREE_CODE (type0) == INTEGER_TYPE
12518 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12519 {
22a75734 12520 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12521 {
12522 if (complain)
12523 error_at (loc, "conversion of scalar %qT to vector %qT "
12524 "involves truncation", type0, type1);
12525 return stv_error;
12526 }
12527 else
12528 return stv_firstarg;
12529 }
12530 break;
12531
12532 case BIT_IOR_EXPR:
12533 case BIT_XOR_EXPR:
12534 case BIT_AND_EXPR:
12535 integer_only_op = true;
12536 /* ... fall through ... */
12537
7b463b19 12538 case VEC_COND_EXPR:
12539
41ed701a 12540 case PLUS_EXPR:
12541 case MINUS_EXPR:
12542 case MULT_EXPR:
12543 case TRUNC_DIV_EXPR:
12544 case CEIL_DIV_EXPR:
12545 case FLOOR_DIV_EXPR:
12546 case ROUND_DIV_EXPR:
12547 case EXACT_DIV_EXPR:
12548 case TRUNC_MOD_EXPR:
12549 case FLOOR_MOD_EXPR:
12550 case RDIV_EXPR:
12551 case EQ_EXPR:
12552 case NE_EXPR:
12553 case LE_EXPR:
12554 case GE_EXPR:
12555 case LT_EXPR:
12556 case GT_EXPR:
12557 /* What about UNLT_EXPR? */
6290f0db 12558 if (VECTOR_TYPE_P (type0))
41ed701a 12559 {
41ed701a 12560 ret = stv_secondarg;
a4f59596 12561 std::swap (type0, type1);
12562 std::swap (op0, op1);
41ed701a 12563 }
12564
12565 if (TREE_CODE (type0) == INTEGER_TYPE
12566 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12567 {
22a75734 12568 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12569 {
12570 if (complain)
12571 error_at (loc, "conversion of scalar %qT to vector %qT "
12572 "involves truncation", type0, type1);
12573 return stv_error;
12574 }
12575 return ret;
12576 }
12577 else if (!integer_only_op
12578 /* Allow integer --> real conversion if safe. */
12579 && (TREE_CODE (type0) == REAL_TYPE
12580 || TREE_CODE (type0) == INTEGER_TYPE)
12581 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12582 {
22a75734 12583 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12584 {
12585 if (complain)
12586 error_at (loc, "conversion of scalar %qT to vector %qT "
12587 "involves truncation", type0, type1);
12588 return stv_error;
12589 }
12590 return ret;
12591 }
12592 default:
12593 break;
12594 }
12595
12596 return stv_nothing;
12597}
12598
ffcdbf9c 12599/* Return true iff ALIGN is an integral constant that is a fundamental
12600 alignment, as defined by [basic.align] in the c++-11
12601 specifications.
12602
12603 That is:
12604
12605 [A fundamental alignment is represented by an alignment less than or
12606 equal to the greatest alignment supported by the implementation
12607 in all contexts, which is equal to
12608 alignof(max_align_t)]. */
12609
12610bool
12611cxx_fundamental_alignment_p (unsigned align)
12612{
12613 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12614 TYPE_ALIGN (long_double_type_node)));
12615}
12616
46da3601 12617/* Return true if T is a pointer to a zero-sized aggregate. */
12618
12619bool
12620pointer_to_zero_sized_aggr_p (tree t)
12621{
12622 if (!POINTER_TYPE_P (t))
12623 return false;
12624 t = TREE_TYPE (t);
12625 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12626}
12627
547c6b1f 12628/* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
12629 with no library fallback or for an ADDR_EXPR whose operand is such type
12630 issues an error pointing to the location LOC.
12631 Returns true when the expression has been diagnosed and false
12632 otherwise. */
12633bool
12634reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
12635{
12636 if (TREE_CODE (expr) == ADDR_EXPR)
12637 expr = TREE_OPERAND (expr, 0);
12638
12639 if (TREE_TYPE (expr)
12640 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
12cc1225 12641 && TREE_CODE (expr) == FUNCTION_DECL
547c6b1f 12642 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
12643 false positives for user-declared built-ins such as abs or
12644 strlen, and for C++ operators new and delete.
12645 The c_decl_implicit() test avoids false positives for implicitly
12646 declared built-ins with library fallbacks (such as abs). */
12647 && DECL_BUILT_IN (expr)
12648 && DECL_IS_BUILTIN (expr)
12649 && !c_decl_implicit (expr)
12650 && !DECL_ASSEMBLER_NAME_SET_P (expr))
12651 {
12652 if (loc == UNKNOWN_LOCATION)
12653 loc = EXPR_LOC_OR_LOC (expr, input_location);
12654
12655 /* Reject arguments that are built-in functions with
12656 no library fallback. */
12657 error_at (loc, "built-in function %qE must be directly called", expr);
12658
12659 return true;
12660 }
12661
12662 return false;
12663}
12664
ef17a71a 12665/* If we're creating an if-else-if condition chain, first see if we
12666 already have this COND in the CHAIN. If so, warn and don't add COND
12667 into the vector, otherwise add the COND there. LOC is the location
12668 of COND. */
12669
12670void
12671warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
12672{
12673 /* No chain has been created yet. Do nothing. */
12674 if (*chain == NULL)
12675 return;
12676
12677 if (TREE_SIDE_EFFECTS (cond))
12678 {
12679 /* Uh-oh! This condition has a side-effect, thus invalidates
12680 the whole chain. */
12681 delete *chain;
12682 *chain = NULL;
12683 return;
12684 }
12685
12686 unsigned int ix;
12687 tree t;
12688 bool found = false;
12689 FOR_EACH_VEC_ELT (**chain, ix, t)
12690 if (operand_equal_p (cond, t, 0))
12691 {
12692 if (warning_at (loc, OPT_Wduplicated_cond,
12693 "duplicated %<if%> condition"))
12694 inform (EXPR_LOCATION (t), "previously used here");
12695 found = true;
12696 break;
12697 }
12698
12699 if (!found
12700 && !CONSTANT_CLASS_P (cond)
12701 /* Don't infinitely grow the chain. */
12702 && (*chain)->length () < 512)
12703 (*chain)->safe_push (cond);
12704}
12705
81b1b2a8 12706/* Check if array size calculations overflow or if the array covers more
12707 than half of the address space. Return true if the size of the array
12708 is valid, false otherwise. TYPE is the type of the array and NAME is
12709 the name of the array, or NULL_TREE for unnamed arrays. */
12710
12711bool
12712valid_array_size_p (location_t loc, tree type, tree name)
12713{
12714 if (type != error_mark_node
12715 && COMPLETE_TYPE_P (type)
12716 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
12717 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
12718 {
12719 if (name)
12720 error_at (loc, "size of array %qE is too large", name);
12721 else
12722 error_at (loc, "size of unnamed array is too large");
12723 return false;
12724 }
12725 return true;
12726}
12727
7bedc3a0 12728#include "gt-c-family-c-common.h"