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