]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-common.c
.gitignore: Ignore in-tree prerequisites.
[thirdparty/gcc.git] / gcc / c-family / c-common.c
CommitLineData
b0fc3e72 1/* Subroutines shared by all languages that are variants of C.
f1717362 2 Copyright (C) 1992-2016 Free Software Foundation, Inc.
b0fc3e72 3
f12b58b3 4This file is part of GCC.
b0fc3e72 5
f12b58b3 6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8c4c00c1 8Software Foundation; either version 3, or (at your option) any later
f12b58b3 9version.
b0fc3e72 10
f12b58b3 11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
b0fc3e72 15
16You should have received a copy of the GNU General Public License
8c4c00c1 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
b0fc3e72 19
b20a8bb4 20#define GCC_C_COMMON_C
21
b0fc3e72 22#include "config.h"
405711de 23#include "system.h"
805e22b2 24#include "coretypes.h"
4cba6f60 25#include "target.h"
26#include "function.h"
4cba6f60 27#include "tree.h"
7ff8db31 28#include "c-common.h"
4cba6f60 29#include "gimple-expr.h"
30#include "tm_p.h"
31#include "stringpool.h"
32#include "cgraph.h"
33#include "diagnostic.h"
e48d0f41 34#include "intl.h"
9ed99284 35#include "stor-layout.h"
36#include "calls.h"
9ed99284 37#include "attribs.h"
38#include "varasm.h"
39#include "trans-mem.h"
6c536c4f 40#include "c-objc.h"
218e3e4e 41#include "common/common-target.h"
96554925 42#include "langhooks.h"
f3dde807 43#include "tree-inline.h"
69579044 44#include "toplev.h"
4ee9c684 45#include "tree-iterator.h"
e08bd2f4 46#include "opts.h"
a8783bee 47#include "gimplify.h"
d4166bdc 48#include "substring-locations.h"
5c8151fa 49#include "spellcheck.h"
fd6f6435 50
90cc7820 51cpp_reader *parse_in; /* Declared in c-pragma.h. */
a654e028 52
de801c28 53/* Mode used to build pointers (VOIDmode means ptr_mode). */
54
55machine_mode c_default_pointer_mode = VOIDmode;
56
72040e7e 57/* The following symbols are subsumed in the c_global_trees array, and
44e9fa65 58 listed here individually for documentation purposes.
72040e7e 59
60 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
61
62 tree short_integer_type_node;
63 tree long_integer_type_node;
64 tree long_long_integer_type_node;
65
66 tree short_unsigned_type_node;
67 tree long_unsigned_type_node;
68 tree long_long_unsigned_type_node;
69
3c2239cf 70 tree truthvalue_type_node;
71 tree truthvalue_false_node;
72 tree truthvalue_true_node;
72040e7e 73
74 tree ptrdiff_type_node;
75
76 tree unsigned_char_type_node;
77 tree signed_char_type_node;
78 tree wchar_type_node;
72040e7e 79
924bbf02 80 tree char16_type_node;
81 tree char32_type_node;
82
72040e7e 83 tree float_type_node;
84 tree double_type_node;
85 tree long_double_type_node;
86
87 tree complex_integer_type_node;
88 tree complex_float_type_node;
89 tree complex_double_type_node;
90 tree complex_long_double_type_node;
91
c4503c0a 92 tree dfloat32_type_node;
93 tree dfloat64_type_node;
94 tree_dfloat128_type_node;
95
72040e7e 96 tree intQI_type_node;
97 tree intHI_type_node;
98 tree intSI_type_node;
99 tree intDI_type_node;
100 tree intTI_type_node;
101
102 tree unsigned_intQI_type_node;
103 tree unsigned_intHI_type_node;
104 tree unsigned_intSI_type_node;
105 tree unsigned_intDI_type_node;
106 tree unsigned_intTI_type_node;
107
108 tree widest_integer_literal_type_node;
109 tree widest_unsigned_literal_type_node;
110
111 Nodes for types `void *' and `const void *'.
112
113 tree ptr_type_node, const_ptr_type_node;
114
115 Nodes for types `char *' and `const char *'.
116
117 tree string_type_node, const_string_type_node;
118
119 Type `char[SOMENUMBER]'.
120 Used when an array of char is needed and the size is irrelevant.
121
122 tree char_array_type_node;
123
72040e7e 124 Type `wchar_t[SOMENUMBER]' or something like it.
125 Used when a wide string literal is created.
126
127 tree wchar_array_type_node;
128
924bbf02 129 Type `char16_t[SOMENUMBER]' or something like it.
130 Used when a UTF-16 string literal is created.
131
132 tree char16_array_type_node;
133
134 Type `char32_t[SOMENUMBER]' or something like it.
135 Used when a UTF-32 string literal is created.
136
137 tree char32_array_type_node;
138
72040e7e 139 Type `int ()' -- used for implicit declaration of functions.
140
141 tree default_function_type;
142
72040e7e 143 A VOID_TYPE node, packaged in a TREE_LIST.
144
145 tree void_list_node;
146
734c98be 147 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
65b7f83f 148 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
149 VAR_DECLS, but C++ does.)
71d9fc9b 150
65b7f83f 151 tree function_name_decl_node;
734c98be 152 tree pretty_function_name_decl_node;
65b7f83f 153 tree c99_function_name_decl_node;
154
155 Stack of nested function name VAR_DECLs.
1cae46be 156
65b7f83f 157 tree saved_function_name_decls;
71d9fc9b 158
72040e7e 159*/
160
161tree c_global_trees[CTI_MAX];
573aba85 162\f
574a6990 163/* Switches common to the C front ends. */
164
f7070933 165/* Nonzero means don't output line number information. */
166
167char flag_no_line_commands;
168
169/* Nonzero causes -E output not to be done, but directives such as
170 #define that have side effects are still obeyed. */
171
172char flag_no_output;
173
174/* Nonzero means dump macros in some fashion. */
175
176char flag_dump_macros;
177
178/* Nonzero means pass #include lines through to the output. */
179
180char flag_dump_includes;
181
d718b525 182/* Nonzero means process PCH files while preprocessing. */
183
184bool flag_pch_preprocess;
185
573aba85 186/* The file name to which we should write a precompiled header, or
187 NULL if no header will be written in this compile. */
188
189const char *pch_file;
190
1ed9d5f5 191/* Nonzero if an ISO standard was selected. It rejects macros in the
192 user's namespace. */
193int flag_iso;
194
574a6990 195/* C/ObjC language option variables. */
196
197
574a6990 198/* Nonzero means allow type mismatches in conditional expressions;
199 just make their values `void'. */
200
201int flag_cond_mismatch;
202
203/* Nonzero means enable C89 Amendment 1 features. */
204
205int flag_isoc94;
206
32074525 207/* Nonzero means use the ISO C99 (or C11) dialect of C. */
574a6990 208
209int flag_isoc99;
210
32074525 211/* Nonzero means use the ISO C11 dialect of C. */
39012afb 212
32074525 213int flag_isoc11;
39012afb 214
8b332087 215/* Nonzero means that we have builtin functions, and main is an int. */
574a6990 216
217int flag_hosted = 1;
218
574a6990 219
220/* ObjC language option variables. */
221
222
574a6990 223/* Tells the compiler that this is a special run. Do not perform any
224 compiling, instead we are to test some platform dependent features
225 and output a C header file with appropriate definitions. */
226
227int print_struct_values;
228
f0b5f617 229/* Tells the compiler what is the constant string class for ObjC. */
574a6990 230
231const char *constant_string_class_name;
232
574a6990 233
234/* C++ language option variables. */
235
229a58b1 236/* The reference version of the ABI for -Wabi. */
237
238int warn_abi_version = -1;
574a6990 239
574a6990 240/* Nonzero means generate separate instantiation control files and
241 juggle them at link time. */
242
243int flag_use_repository;
244
d875b9d2 245/* The C++ dialect being used. Default set in c_common_post_options. */
0fe6eeac 246
d875b9d2 247enum cxx_dialect cxx_dialect = cxx_unset;
0fe6eeac 248
9dcd0d49 249/* Maximum template instantiation depth. This limit exists to limit the
8ce59854 250 time it takes to notice excessively recursive template instantiations.
574a6990 251
8ce59854 252 The default is lower than the 1024 recommended by the C++0x standard
253 because G++ runs out of stack before 1024 with highly recursive template
254 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
255
256int max_tinst_depth = 900;
574a6990 257
988fc1d1 258/* The elements of `ridpointers' are identifier nodes for the reserved
259 type names and storage classes. It is indexed by a RID_... value. */
260tree *ridpointers;
261
e60a6f7b 262tree (*make_fname_decl) (location_t, tree, int);
9e5a737d 263
48d94ede 264/* Nonzero means don't warn about problems that occur when the code is
265 executed. */
266int c_inhibit_evaluation_warnings;
e78703c1 267
93be21c0 268/* Whether we are building a boolean conversion inside
269 convert_for_assignment, or some other late binary operation. If
270 build_binary_op is called for C (from code shared by C and C++) in
271 this case, then the operands have already been folded and the
272 result will not be folded again, so C_MAYBE_CONST_EXPR should not
273 be generated. */
274bool in_late_binary_op;
275
7f5f3953 276/* Whether lexing has been completed, so subsequent preprocessor
277 errors should use the compiler's input_location. */
278bool done_lexing = false;
279
2c0e001b 280/* Information about how a function name is generated. */
65b7f83f 281struct fname_var_t
282{
e99c3a1d 283 tree *const decl; /* pointer to the VAR_DECL. */
284 const unsigned rid; /* RID number for the identifier. */
285 const int pretty; /* How pretty is it? */
65b7f83f 286};
287
2c0e001b 288/* The three ways of getting then name of the current function. */
65b7f83f 289
290const struct fname_var_t fname_vars[] =
291{
2c0e001b 292 /* C99 compliant __func__, must be first. */
65b7f83f 293 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
2c0e001b 294 /* GCC __FUNCTION__ compliant. */
65b7f83f 295 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
2c0e001b 296 /* GCC __PRETTY_FUNCTION__ compliant. */
65b7f83f 297 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
298 {NULL, 0, 0},
299};
300
79396169 301/* Global visibility options. */
302struct visibility_flags visibility_options;
303
2d2f6a15 304static tree check_case_value (location_t, tree);
be23b16f 305static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
306 bool *);
be43ff5a 307
1cae46be 308static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
309static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
310static tree handle_common_attribute (tree *, tree, tree, int, bool *);
311static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
5de92639 312static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
313static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
a9196da9 314static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
315 int, bool *);
d413ffdd 316static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
317 int, bool *);
05f893e1 318static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
319 bool *);
947aa916 320static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
1cae46be 321static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
bdb1f0d1 322static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
85fbea97 323static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
7bd95dfd 324static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
1cae46be 325static tree handle_always_inline_attribute (tree *, tree, tree, int,
326 bool *);
1b16fc45 327static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
328static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
0cdd9887 329static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
10fc867f 330static tree handle_error_attribute (tree *, tree, tree, int, bool *);
1cae46be 331static tree handle_used_attribute (tree *, tree, tree, int, bool *);
62eec3b4 332static tree handle_externally_visible_attribute (tree *, tree, tree, int,
333 bool *);
6b722052 334static tree handle_no_reorder_attribute (tree *, tree, tree, int,
335 bool *);
1cae46be 336static tree handle_const_attribute (tree *, tree, tree, int, bool *);
337static tree handle_transparent_union_attribute (tree *, tree, tree,
338 int, bool *);
292237f3 339static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
340 int, bool *);
1cae46be 341static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
342static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
343static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
344static tree handle_section_attribute (tree *, tree, tree, int, bool *);
345static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
346static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
4641882f 347static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
85c0a25c 348static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
349static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
1cae46be 350static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
f4a30bd7 351static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
1cae46be 352static tree handle_visibility_attribute (tree *, tree, tree, int,
353 bool *);
354static tree handle_tls_model_attribute (tree *, tree, tree, int,
355 bool *);
356static tree handle_no_instrument_function_attribute (tree *, tree,
357 tree, int, bool *);
595e387a 358static tree handle_no_profile_instrument_function_attribute (tree *, tree,
359 tree, int, bool *);
1cae46be 360static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
26d1c5ff 361static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
1cae46be 362static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
363 bool *);
364static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
4c0315d0 365static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
366static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
fc09b200 367static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
1cae46be 368static tree handle_deprecated_attribute (tree *, tree, tree, int,
369 bool *);
370static tree handle_vector_size_attribute (tree *, tree, tree, int,
371 bool *);
372static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
373static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
374static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
8a8cdb8d 375static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
376 bool *);
bf6c8de0 377static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
b5c26b42 378static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
4a29c97c 379static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
237e78b1 380static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
381static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
24470055 382static tree handle_target_attribute (tree *, tree, tree, int, bool *);
ab50af2a 383static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
46f8e3b0 384static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
4c0315d0 385static tree ignore_attribute (tree *, tree, tree, int, bool *);
48b14f50 386static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
8ce86007 387static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
a96c3cc1 388static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
d7dcba40 389static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
bc7bff74 390static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
391 bool *);
c58a4cfd 392static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
bc7bff74 393static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
394 bool *);
74691f46 395static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
058a1b7a 396static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
397static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
398static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
1cae46be 399
1cae46be 400static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
401static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
402static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
860251be 403static int resort_field_decl_cmp (const void *, const void *);
dbf6c367 404
5c6e5756 405/* Reserved words. The third field is a mask: keywords are disabled
406 if they match the mask.
407
408 Masks for languages:
409 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
410 C --std=c99: D_CXXONLY | D_OBJC
411 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
412 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
413 C++ --std=c0x: D_CONLY | D_OBJC
414 ObjC++ is like C++ except that D_OBJC is not set
415
416 If -fno-asm is used, D_ASM is added to the mask. If
417 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
418 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
b27e241e 419 In C with -Wc++-compat, we warn if D_CXXWARN is set.
5c6e5756 420
b27e241e 421 Note the complication of the D_CXX_OBJC keywords. These are
422 reserved words such as 'class'. In C++, 'class' is a reserved
423 word. In Objective-C++ it is too. In Objective-C, it is a
424 reserved word too, but only if it follows an '@' sign.
425*/
5c6e5756 426const struct c_common_resword c_common_reswords[] =
427{
83e25171 428 { "_Alignas", RID_ALIGNAS, D_CONLY },
429 { "_Alignof", RID_ALIGNOF, D_CONLY },
b560fabd 430 { "_Atomic", RID_ATOMIC, D_CONLY },
5c6e5756 431 { "_Bool", RID_BOOL, D_CONLY },
432 { "_Complex", RID_COMPLEX, 0 },
d037099f 433 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
434 { "_Cilk_sync", RID_CILK_SYNC, 0 },
40750995 435 { "_Cilk_for", RID_CILK_FOR, 0 },
c1800156 436 { "_Imaginary", RID_IMAGINARY, D_CONLY },
82c85aba 437 { "_Float16", RID_FLOAT16, D_CONLY },
438 { "_Float32", RID_FLOAT32, D_CONLY },
439 { "_Float64", RID_FLOAT64, D_CONLY },
440 { "_Float128", RID_FLOAT128, D_CONLY },
441 { "_Float32x", RID_FLOAT32X, D_CONLY },
442 { "_Float64x", RID_FLOAT64X, D_CONLY },
443 { "_Float128x", RID_FLOAT128X, D_CONLY },
5c6e5756 444 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
445 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
446 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
447 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
448 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
449 { "_Sat", RID_SAT, D_CONLY | D_EXT },
f80e7755 450 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
985c6e3a 451 { "_Noreturn", RID_NORETURN, D_CONLY },
7aa04c8d 452 { "_Generic", RID_GENERIC, D_CONLY },
d184e0c0 453 { "_Thread_local", RID_THREAD, D_CONLY },
5c6e5756 454 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
455 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
456 { "__alignof", RID_ALIGNOF, 0 },
457 { "__alignof__", RID_ALIGNOF, 0 },
458 { "__asm", RID_ASM, 0 },
459 { "__asm__", RID_ASM, 0 },
460 { "__attribute", RID_ATTRIBUTE, 0 },
461 { "__attribute__", RID_ATTRIBUTE, 0 },
4fba5eb9 462 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
e6014a82 463 { "__bases", RID_BASES, D_CXXONLY },
6f58cf06 464 { "__builtin_call_with_static_chain",
465 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
5c6e5756 466 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
bff4ad11 467 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
bf0cb017 468 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
5c6e5756 469 { "__builtin_offsetof", RID_OFFSETOF, 0 },
470 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
471 { "__builtin_va_arg", RID_VA_ARG, 0 },
472 { "__complex", RID_COMPLEX, 0 },
473 { "__complex__", RID_COMPLEX, 0 },
474 { "__const", RID_CONST, 0 },
475 { "__const__", RID_CONST, 0 },
476 { "__decltype", RID_DECLTYPE, D_CXXONLY },
e6014a82 477 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
5c6e5756 478 { "__extension__", RID_EXTENSION, 0 },
479 { "__func__", RID_C99_FUNCTION_NAME, 0 },
480 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
481 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
482 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
483 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
484 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
485 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
486 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
487 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
23407dc9 488 { "__imag", RID_IMAGPART, 0 },
489 { "__imag__", RID_IMAGPART, 0 },
490 { "__inline", RID_INLINE, 0 },
491 { "__inline__", RID_INLINE, 0 },
5c6e5756 492 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
493 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
494 { "__is_class", RID_IS_CLASS, D_CXXONLY },
5c6e5756 495 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
496 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
aa4313eb 497 { "__is_final", RID_IS_FINAL, D_CXXONLY },
23407dc9 498 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
5c6e5756 499 { "__is_pod", RID_IS_POD, D_CXXONLY },
500 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
56c12fd4 501 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
c1c67b4f 502 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
503 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
f76a9aa8 504 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
505 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
717e52f9 506 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
5c6e5756 507 { "__is_union", RID_IS_UNION, D_CXXONLY },
5c6e5756 508 { "__label__", RID_LABEL, 0 },
509 { "__null", RID_NULL, 0 },
510 { "__real", RID_REALPART, 0 },
511 { "__real__", RID_REALPART, 0 },
512 { "__restrict", RID_RESTRICT, 0 },
513 { "__restrict__", RID_RESTRICT, 0 },
514 { "__signed", RID_SIGNED, 0 },
515 { "__signed__", RID_SIGNED, 0 },
516 { "__thread", RID_THREAD, 0 },
4c0315d0 517 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
518 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
519 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
5c6e5756 520 { "__typeof", RID_TYPEOF, 0 },
521 { "__typeof__", RID_TYPEOF, 0 },
23407dc9 522 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
5c6e5756 523 { "__volatile", RID_VOLATILE, 0 },
524 { "__volatile__", RID_VOLATILE, 0 },
fa769cc5 525 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
526 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 527 { "asm", RID_ASM, D_ASM },
528 { "auto", RID_AUTO, 0 },
dbd982c9 529 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
5c6e5756 530 { "break", RID_BREAK, 0 },
531 { "case", RID_CASE, 0 },
51030405 532 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
5c6e5756 533 { "char", RID_CHAR, 0 },
fa769cc5 534 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
535 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
51030405 536 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
5c6e5756 537 { "const", RID_CONST, 0 },
fa769cc5 538 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 539 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
540 { "continue", RID_CONTINUE, 0 },
fa769cc5 541 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 542 { "default", RID_DEFAULT, 0 },
51030405 543 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
5c6e5756 544 { "do", RID_DO, 0 },
545 { "double", RID_DOUBLE, 0 },
546 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
547 { "else", RID_ELSE, 0 },
548 { "enum", RID_ENUM, 0 },
51030405 549 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
550 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
5c6e5756 551 { "extern", RID_EXTERN, 0 },
51030405 552 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
5c6e5756 553 { "float", RID_FLOAT, 0 },
554 { "for", RID_FOR, 0 },
51030405 555 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
5c6e5756 556 { "goto", RID_GOTO, 0 },
557 { "if", RID_IF, 0 },
558 { "inline", RID_INLINE, D_EXT89 },
559 { "int", RID_INT, 0 },
560 { "long", RID_LONG, 0 },
561 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
51030405 562 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
563 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
fa769cc5 564 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
565 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
51030405 566 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
567 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
568 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
569 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
5c6e5756 570 { "register", RID_REGISTER, 0 },
571 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
572 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
573 { "return", RID_RETURN, 0 },
574 { "short", RID_SHORT, 0 },
575 { "signed", RID_SIGNED, 0 },
576 { "sizeof", RID_SIZEOF, 0 },
577 { "static", RID_STATIC, 0 },
fa769cc5 578 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 579 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
580 { "struct", RID_STRUCT, 0 },
581 { "switch", RID_SWITCH, 0 },
51030405 582 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
583 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
fa769cc5 584 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
51030405 585 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
586 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
587 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
5c6e5756 588 { "typedef", RID_TYPEDEF, 0 },
51030405 589 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
590 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
5c6e5756 591 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
592 { "union", RID_UNION, 0 },
593 { "unsigned", RID_UNSIGNED, 0 },
51030405 594 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
595 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
5c6e5756 596 { "void", RID_VOID, 0 },
597 { "volatile", RID_VOLATILE, 0 },
598 { "wchar_t", RID_WCHAR, D_CXXONLY },
599 { "while", RID_WHILE, 0 },
56c12fd4 600
6d02e6b2 601 /* C++ transactional memory. */
602 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
603 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
604 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
605 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
606
56c12fd4 607 /* Concepts-related keywords */
608 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
609 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
610
5c6e5756 611 /* These Objective-C keywords are recognized only immediately after
612 an '@'. */
613 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
614 { "defs", RID_AT_DEFS, D_OBJC },
615 { "encode", RID_AT_ENCODE, D_OBJC },
616 { "end", RID_AT_END, D_OBJC },
617 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
618 { "interface", RID_AT_INTERFACE, D_OBJC },
619 { "protocol", RID_AT_PROTOCOL, D_OBJC },
620 { "selector", RID_AT_SELECTOR, D_OBJC },
621 { "finally", RID_AT_FINALLY, D_OBJC },
069761fb 622 { "optional", RID_AT_OPTIONAL, D_OBJC },
623 { "required", RID_AT_REQUIRED, D_OBJC },
86c110ac 624 { "property", RID_AT_PROPERTY, D_OBJC },
4a8875ed 625 { "package", RID_AT_PACKAGE, D_OBJC },
e1f293c0 626 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
627 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
5c6e5756 628 /* These are recognized only in protocol-qualifier context
629 (see above) */
630 { "bycopy", RID_BYCOPY, D_OBJC },
631 { "byref", RID_BYREF, D_OBJC },
632 { "in", RID_IN, D_OBJC },
633 { "inout", RID_INOUT, D_OBJC },
634 { "oneway", RID_ONEWAY, D_OBJC },
635 { "out", RID_OUT, D_OBJC },
86c110ac 636 /* These are recognized inside a property attribute list */
7590f0e5 637 { "assign", RID_ASSIGN, D_OBJC },
638 { "copy", RID_COPY, D_OBJC },
86c110ac 639 { "getter", RID_GETTER, D_OBJC },
7590f0e5 640 { "nonatomic", RID_NONATOMIC, D_OBJC },
641 { "readonly", RID_READONLY, D_OBJC },
642 { "readwrite", RID_READWRITE, D_OBJC },
643 { "retain", RID_RETAIN, D_OBJC },
86c110ac 644 { "setter", RID_SETTER, D_OBJC },
5c6e5756 645};
646
647const unsigned int num_c_common_reswords =
648 sizeof c_common_reswords / sizeof (struct c_common_resword);
649
058a1b7a 650/* Table of machine-independent attributes common to all C-like languages.
651
652 All attributes referencing arguments should be additionally processed
653 in chkp_copy_function_type_adding_bounds for correct instrumentation
654 by Pointer Bounds Checker.
655 Current list of processed common attributes: nonnull. */
f8e93a2e 656const struct attribute_spec c_common_attribute_table[] =
657{
ac86af5d 658 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
659 affects_type_identity } */
f8e93a2e 660 { "packed", 0, 0, false, false, false,
ac86af5d 661 handle_packed_attribute , false},
f8e93a2e 662 { "nocommon", 0, 0, true, false, false,
ac86af5d 663 handle_nocommon_attribute, false},
f8e93a2e 664 { "common", 0, 0, true, false, false,
ac86af5d 665 handle_common_attribute, false },
f8e93a2e 666 /* FIXME: logically, noreturn attributes should be listed as
667 "false, true, true" and apply to function types. But implementing this
668 would require all the places in the compiler that use TREE_THIS_VOLATILE
669 on a decl to identify non-returning functions to be located and fixed
670 to check the function type instead. */
671 { "noreturn", 0, 0, true, false, false,
ac86af5d 672 handle_noreturn_attribute, false },
f8e93a2e 673 { "volatile", 0, 0, true, false, false,
ac86af5d 674 handle_noreturn_attribute, false },
947aa916 675 { "stack_protect", 0, 0, true, false, false,
676 handle_stack_protect_attribute, false },
f8e93a2e 677 { "noinline", 0, 0, true, false, false,
ac86af5d 678 handle_noinline_attribute, false },
bdb1f0d1 679 { "noclone", 0, 0, true, false, false,
ac86af5d 680 handle_noclone_attribute, false },
85fbea97 681 { "no_icf", 0, 0, true, false, false,
682 handle_noicf_attribute, false },
7bd95dfd 683 { "leaf", 0, 0, true, false, false,
ac86af5d 684 handle_leaf_attribute, false },
f8e93a2e 685 { "always_inline", 0, 0, true, false, false,
ac86af5d 686 handle_always_inline_attribute, false },
541e4101 687 { "gnu_inline", 0, 0, true, false, false,
ac86af5d 688 handle_gnu_inline_attribute, false },
1b16fc45 689 { "artificial", 0, 0, true, false, false,
ac86af5d 690 handle_artificial_attribute, false },
0cdd9887 691 { "flatten", 0, 0, true, false, false,
ac86af5d 692 handle_flatten_attribute, false },
f8e93a2e 693 { "used", 0, 0, true, false, false,
ac86af5d 694 handle_used_attribute, false },
f8e93a2e 695 { "unused", 0, 0, false, false, false,
ac86af5d 696 handle_unused_attribute, false },
62eec3b4 697 { "externally_visible", 0, 0, true, false, false,
ac86af5d 698 handle_externally_visible_attribute, false },
6b722052 699 { "no_reorder", 0, 0, true, false, false,
700 handle_no_reorder_attribute, false },
f8e93a2e 701 /* The same comments as for noreturn attributes apply to const ones. */
702 { "const", 0, 0, true, false, false,
ac86af5d 703 handle_const_attribute, false },
292237f3 704 { "scalar_storage_order", 1, 1, false, false, false,
705 handle_scalar_storage_order_attribute, false },
f8e93a2e 706 { "transparent_union", 0, 0, false, false, false,
ac86af5d 707 handle_transparent_union_attribute, false },
9af7fd5b 708 { "constructor", 0, 1, true, false, false,
ac86af5d 709 handle_constructor_attribute, false },
9af7fd5b 710 { "destructor", 0, 1, true, false, false,
ac86af5d 711 handle_destructor_attribute, false },
f8e93a2e 712 { "mode", 1, 1, false, true, false,
ac86af5d 713 handle_mode_attribute, false },
f8e93a2e 714 { "section", 1, 1, true, false, false,
ac86af5d 715 handle_section_attribute, false },
f8e93a2e 716 { "aligned", 0, 1, false, false, false,
ac86af5d 717 handle_aligned_attribute, false },
f8e93a2e 718 { "weak", 0, 0, true, false, false,
ac86af5d 719 handle_weak_attribute, false },
4641882f 720 { "noplt", 0, 0, true, false, false,
721 handle_noplt_attribute, false },
85c0a25c 722 { "ifunc", 1, 1, true, false, false,
ac86af5d 723 handle_ifunc_attribute, false },
f8e93a2e 724 { "alias", 1, 1, true, false, false,
ac86af5d 725 handle_alias_attribute, false },
f4a30bd7 726 { "weakref", 0, 1, true, false, false,
ac86af5d 727 handle_weakref_attribute, false },
f8e93a2e 728 { "no_instrument_function", 0, 0, true, false, false,
ac86af5d 729 handle_no_instrument_function_attribute,
730 false },
595e387a 731 { "no_profile_instrument_function", 0, 0, true, false, false,
732 handle_no_profile_instrument_function_attribute,
733 false },
f8e93a2e 734 { "malloc", 0, 0, true, false, false,
ac86af5d 735 handle_malloc_attribute, false },
26d1c5ff 736 { "returns_twice", 0, 0, true, false, false,
ac86af5d 737 handle_returns_twice_attribute, false },
f8e93a2e 738 { "no_stack_limit", 0, 0, true, false, false,
ac86af5d 739 handle_no_limit_stack_attribute, false },
f8e93a2e 740 { "pure", 0, 0, true, false, false,
ac86af5d 741 handle_pure_attribute, false },
4c0315d0 742 { "transaction_callable", 0, 0, false, true, false,
743 handle_tm_attribute, false },
744 { "transaction_unsafe", 0, 0, false, true, false,
6d02e6b2 745 handle_tm_attribute, true },
4c0315d0 746 { "transaction_safe", 0, 0, false, true, false,
6d02e6b2 747 handle_tm_attribute, true },
748 { "transaction_safe_dynamic", 0, 0, true, false, false,
4c0315d0 749 handle_tm_attribute, false },
750 { "transaction_may_cancel_outer", 0, 0, false, true, false,
751 handle_tm_attribute, false },
752 /* ??? These two attributes didn't make the transition from the
753 Intel language document to the multi-vendor language document. */
754 { "transaction_pure", 0, 0, false, true, false,
755 handle_tm_attribute, false },
756 { "transaction_wrap", 1, 1, true, false, false,
757 handle_tm_wrap_attribute, false },
fc09b200 758 /* For internal use (marking of builtins) only. The name contains space
759 to prevent its usage in source code. */
760 { "no vops", 0, 0, true, false, false,
ac86af5d 761 handle_novops_attribute, false },
45c4e798 762 { "deprecated", 0, 1, false, false, false,
ac86af5d 763 handle_deprecated_attribute, false },
f8e93a2e 764 { "vector_size", 1, 1, false, true, false,
80488e20 765 handle_vector_size_attribute, true },
b212f378 766 { "visibility", 1, 1, false, false, false,
ac86af5d 767 handle_visibility_attribute, false },
24dfead4 768 { "tls_model", 1, 1, true, false, false,
ac86af5d 769 handle_tls_model_attribute, false },
dbf6c367 770 { "nonnull", 0, -1, false, true, true,
ac86af5d 771 handle_nonnull_attribute, false },
fa987697 772 { "nothrow", 0, 0, true, false, false,
ac86af5d 773 handle_nothrow_attribute, false },
774 { "may_alias", 0, 0, false, true, false, NULL, false },
7acb29a3 775 { "cleanup", 1, 1, true, false, false,
ac86af5d 776 handle_cleanup_attribute, false },
8a8cdb8d 777 { "warn_unused_result", 0, 0, false, true, true,
ac86af5d 778 handle_warn_unused_result_attribute, false },
50ca527f 779 { "sentinel", 0, 1, false, true, true,
ac86af5d 780 handle_sentinel_attribute, false },
b5c26b42 781 /* For internal use (marking of builtins) only. The name contains space
782 to prevent its usage in source code. */
783 { "type generic", 0, 0, false, true, true,
ac86af5d 784 handle_type_generic_attribute, false },
4a29c97c 785 { "alloc_size", 1, 2, false, true, true,
ac86af5d 786 handle_alloc_size_attribute, false },
5de92639 787 { "cold", 0, 0, true, false, false,
ac86af5d 788 handle_cold_attribute, false },
5de92639 789 { "hot", 0, 0, true, false, false,
ac86af5d 790 handle_hot_attribute, false },
d413ffdd 791 { "no_address_safety_analysis",
792 0, 0, true, false, false,
793 handle_no_address_safety_analysis_attribute,
794 false },
a9196da9 795 { "no_sanitize_address", 0, 0, true, false, false,
796 handle_no_sanitize_address_attribute,
797 false },
d1e96383 798 { "no_sanitize_thread", 0, 0, true, false, false,
799 handle_no_sanitize_address_attribute,
800 false },
05f893e1 801 { "no_sanitize_undefined", 0, 0, true, false, false,
802 handle_no_sanitize_undefined_attribute,
803 false },
10fc867f 804 { "warning", 1, 1, true, false, false,
ac86af5d 805 handle_error_attribute, false },
10fc867f 806 { "error", 1, 1, true, false, false,
ac86af5d 807 handle_error_attribute, false },
24470055 808 { "target", 1, -1, true, false, false,
ac86af5d 809 handle_target_attribute, false },
ab50af2a 810 { "target_clones", 1, -1, true, false, false,
811 handle_target_clones_attribute, false },
46f8e3b0 812 { "optimize", 1, -1, true, false, false,
ac86af5d 813 handle_optimize_attribute, false },
4c0315d0 814 /* For internal use only. The leading '*' both prevents its usage in
815 source code and signals that it may be overridden by machine tables. */
816 { "*tm regparm", 0, 0, false, true, true,
817 ignore_attribute, false },
48b14f50 818 { "no_split_stack", 0, 0, true, false, false,
ac86af5d 819 handle_no_split_stack_attribute, false },
8ce86007 820 /* For internal use (marking of builtins and runtime functions) only.
821 The name contains space to prevent its usage in source code. */
822 { "fn spec", 1, 1, false, true, true,
ac86af5d 823 handle_fnspec_attribute, false },
a96c3cc1 824 { "warn_unused", 0, 0, false, false, false,
825 handle_warn_unused_attribute, false },
d7dcba40 826 { "returns_nonnull", 0, 0, false, true, true,
827 handle_returns_nonnull_attribute, false },
bc7bff74 828 { "omp declare simd", 0, -1, true, false, false,
829 handle_omp_declare_simd_attribute, false },
74acc703 830 { "cilk simd function", 0, -1, true, false, false,
831 handle_omp_declare_simd_attribute, false },
9841c6ee 832 { "simd", 0, 1, true, false, false,
c58a4cfd 833 handle_simd_attribute, false },
bc7bff74 834 { "omp declare target", 0, 0, true, false, false,
835 handle_omp_declare_target_attribute, false },
c0998828 836 { "omp declare target link", 0, 0, true, false, false,
837 handle_omp_declare_target_attribute, false },
237e78b1 838 { "alloc_align", 1, 1, false, true, true,
839 handle_alloc_align_attribute, false },
840 { "assume_aligned", 1, 2, false, true, true,
841 handle_assume_aligned_attribute, false },
74691f46 842 { "designated_init", 0, 0, false, true, false,
843 handle_designated_init_attribute, false },
058a1b7a 844 { "bnd_variable_size", 0, 0, true, false, false,
845 handle_bnd_variable_size_attribute, false },
846 { "bnd_legacy", 0, 0, true, false, false,
847 handle_bnd_legacy, false },
848 { "bnd_instrument", 0, 0, true, false, false,
849 handle_bnd_instrument, false },
ac86af5d 850 { NULL, 0, 0, false, false, false, NULL, false }
f8e93a2e 851};
852
853/* Give the specifications for the format attributes, used by C and all
058a1b7a 854 descendants.
f8e93a2e 855
058a1b7a 856 All attributes referencing arguments should be additionally processed
857 in chkp_copy_function_type_adding_bounds for correct instrumentation
858 by Pointer Bounds Checker.
859 Current list of processed format attributes: format, format_arg. */
f8e93a2e 860const struct attribute_spec c_common_format_attribute_table[] =
861{
ac86af5d 862 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
863 affects_type_identity } */
f8e93a2e 864 { "format", 3, 3, false, true, true,
ac86af5d 865 handle_format_attribute, false },
f8e93a2e 866 { "format_arg", 1, 1, false, true, true,
ac86af5d 867 handle_format_arg_attribute, false },
868 { NULL, 0, 0, false, false, false, NULL, false }
f8e93a2e 869};
870
6d5d708e 871/* Return identifier for address space AS. */
34208e18 872
6d5d708e 873const char *
874c_addr_space_name (addr_space_t as)
875{
34208e18 876 int rid = RID_FIRST_ADDR_SPACE + as;
877 gcc_assert (ridpointers [rid]);
878 return IDENTIFIER_POINTER (ridpointers [rid]);
6d5d708e 879}
880
2c0e001b 881/* Push current bindings for the function name VAR_DECLS. */
f4e3c278 882
883void
1cae46be 884start_fname_decls (void)
f4e3c278 885{
65b7f83f 886 unsigned ix;
887 tree saved = NULL_TREE;
1cae46be 888
65b7f83f 889 for (ix = 0; fname_vars[ix].decl; ix++)
890 {
891 tree decl = *fname_vars[ix].decl;
f4e3c278 892
65b7f83f 893 if (decl)
894 {
ceb7b692 895 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
896 saved);
65b7f83f 897 *fname_vars[ix].decl = NULL_TREE;
898 }
899 }
900 if (saved || saved_function_name_decls)
901 /* Normally they'll have been NULL, so only push if we've got a
902 stack, or they are non-NULL. */
903 saved_function_name_decls = tree_cons (saved, NULL_TREE,
904 saved_function_name_decls);
905}
906
2363ef00 907/* Finish up the current bindings, adding them into the current function's
908 statement tree. This must be done _before_ finish_stmt_tree is called.
909 If there is no current function, we must be at file scope and no statements
910 are involved. Pop the previous bindings. */
65b7f83f 911
912void
1cae46be 913finish_fname_decls (void)
65b7f83f 914{
915 unsigned ix;
2363ef00 916 tree stmts = NULL_TREE;
65b7f83f 917 tree stack = saved_function_name_decls;
918
919 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
2363ef00 920 append_to_statement_list (TREE_VALUE (stack), &stmts);
1cae46be 921
2363ef00 922 if (stmts)
65b7f83f 923 {
2363ef00 924 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
5c423bd6 925
2363ef00 926 if (TREE_CODE (*bodyp) == BIND_EXPR)
927 bodyp = &BIND_EXPR_BODY (*bodyp);
81010c97 928
bc2b76e0 929 append_to_statement_list_force (*bodyp, &stmts);
2363ef00 930 *bodyp = stmts;
65b7f83f 931 }
1cae46be 932
65b7f83f 933 for (ix = 0; fname_vars[ix].decl; ix++)
934 *fname_vars[ix].decl = NULL_TREE;
1cae46be 935
65b7f83f 936 if (stack)
f4e3c278 937 {
2c0e001b 938 /* We had saved values, restore them. */
65b7f83f 939 tree saved;
940
941 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
942 {
943 tree decl = TREE_PURPOSE (saved);
f9ae6f95 944 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
1cae46be 945
65b7f83f 946 *fname_vars[ix].decl = decl;
947 }
948 stack = TREE_CHAIN (stack);
f4e3c278 949 }
65b7f83f 950 saved_function_name_decls = stack;
951}
952
81010c97 953/* Return the text name of the current function, suitably prettified
5fc7fa69 954 by PRETTY_P. Return string must be freed by caller. */
65b7f83f 955
956const char *
1cae46be 957fname_as_string (int pretty_p)
65b7f83f 958{
9ad4bb1e 959 const char *name = "top level";
5fc7fa69 960 char *namep;
8115b8be 961 int vrb = 2, len;
962 cpp_string cstr = { 0, 0 }, strname;
9ad4bb1e 963
84166705 964 if (!pretty_p)
9ad4bb1e 965 {
966 name = "";
967 vrb = 0;
968 }
969
970 if (current_function_decl)
dc24ddbd 971 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
9ad4bb1e 972
8115b8be 973 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
5fc7fa69 974
8115b8be 975 namep = XNEWVEC (char, len);
976 snprintf (namep, len, "\"%s\"", name);
977 strname.text = (unsigned char *) namep;
978 strname.len = len - 1;
5fc7fa69 979
924bbf02 980 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
8115b8be 981 {
982 XDELETEVEC (namep);
983 return (const char *) cstr.text;
5fc7fa69 984 }
5fc7fa69 985
986 return namep;
65b7f83f 987}
988
65b7f83f 989/* Return the VAR_DECL for a const char array naming the current
990 function. If the VAR_DECL has not yet been created, create it
991 now. RID indicates how it should be formatted and IDENTIFIER_NODE
992 ID is its name (unfortunately C and C++ hold the RID values of
993 keywords in different places, so we can't derive RID from ID in
e3b80d49 994 this language independent code. LOC is the location of the
995 function. */
65b7f83f 996
997tree
e3b80d49 998fname_decl (location_t loc, unsigned int rid, tree id)
65b7f83f 999{
1000 unsigned ix;
1001 tree decl = NULL_TREE;
1002
1003 for (ix = 0; fname_vars[ix].decl; ix++)
1004 if (fname_vars[ix].rid == rid)
1005 break;
1006
1007 decl = *fname_vars[ix].decl;
1008 if (!decl)
f4e3c278 1009 {
2222b3c6 1010 /* If a tree is built here, it would normally have the lineno of
1011 the current statement. Later this tree will be moved to the
1012 beginning of the function and this line number will be wrong.
1013 To avoid this problem set the lineno to 0 here; that prevents
7299020b 1014 it from appearing in the RTL. */
2363ef00 1015 tree stmts;
9a6486a6 1016 location_t saved_location = input_location;
9a6486a6 1017 input_location = UNKNOWN_LOCATION;
1cae46be 1018
2363ef00 1019 stmts = push_stmt_list ();
e60a6f7b 1020 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
2363ef00 1021 stmts = pop_stmt_list (stmts);
1022 if (!IS_EMPTY_STMT (stmts))
1023 saved_function_name_decls
1024 = tree_cons (decl, stmts, saved_function_name_decls);
65b7f83f 1025 *fname_vars[ix].decl = decl;
9a6486a6 1026 input_location = saved_location;
f4e3c278 1027 }
65b7f83f 1028 if (!ix && !current_function_decl)
e3b80d49 1029 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
81010c97 1030
65b7f83f 1031 return decl;
f4e3c278 1032}
1033
070236f0 1034/* Given a STRING_CST, give it a suitable array-of-chars data type. */
b0fc3e72 1035
1036tree
1cae46be 1037fix_string_type (tree value)
b0fc3e72 1038{
070236f0 1039 int length = TREE_STRING_LENGTH (value);
1040 int nchars;
00d26680 1041 tree e_type, i_type, a_type;
1042
73be5127 1043 /* Compute the number of elements, for the array type. */
924bbf02 1044 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1045 {
1046 nchars = length;
1047 e_type = char_type_node;
1048 }
1049 else if (TREE_TYPE (value) == char16_array_type_node)
1050 {
1051 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1052 e_type = char16_type_node;
1053 }
1054 else if (TREE_TYPE (value) == char32_array_type_node)
1055 {
1056 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1057 e_type = char32_type_node;
1058 }
1059 else
1060 {
1061 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1062 e_type = wchar_type_node;
1063 }
b0fc3e72 1064
1d752508 1065 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1066 limit in C++98 Annex B is very large (65536) and is not normative,
1067 so we do not diagnose it (warn_overlength_strings is forced off
1068 in c_common_post_options). */
1069 if (warn_overlength_strings)
1070 {
1071 const int nchars_max = flag_isoc99 ? 4095 : 509;
1072 const int relevant_std = flag_isoc99 ? 99 : 90;
1073 if (nchars - 1 > nchars_max)
1074 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1075 separate the %d from the 'C'. 'ISO' should not be
1076 translated, but it may be moved after 'C%d' in languages
1077 where modifiers follow nouns. */
21ca8540 1078 pedwarn (input_location, OPT_Woverlength_strings,
8864917d 1079 "string length %qd is greater than the length %qd "
1d752508 1080 "ISO C%d compilers are required to support",
1081 nchars - 1, nchars_max, relevant_std);
1082 }
82cfc7f7 1083
390be14e 1084 /* Create the array type for the string constant. The ISO C++
1085 standard says that a string literal has type `const char[N]' or
1086 `const wchar_t[N]'. We use the same logic when invoked as a C
1087 front-end with -Wwrite-strings.
1088 ??? We should change the type of an expression depending on the
1089 state of a warning flag. We should just be warning -- see how
1090 this is handled in the C++ front-end for the deprecated implicit
1091 conversion from string literals to `char*' or `wchar_t*'.
00d26680 1092
1093 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1094 array type being the unqualified version of that type.
1095 Therefore, if we are constructing an array of const char, we must
1096 construct the matching unqualified array type first. The C front
1097 end does not require this, but it does no harm, so we do it
1098 unconditionally. */
ceb7b692 1099 i_type = build_index_type (size_int (nchars - 1));
00d26680 1100 a_type = build_array_type (e_type, i_type);
390be14e 1101 if (c_dialect_cxx() || warn_write_strings)
aebc8537 1102 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
3a10ba35 1103
00d26680 1104 TREE_TYPE (value) = a_type;
b8e3b7ad 1105 TREE_CONSTANT (value) = 1;
a814bad5 1106 TREE_READONLY (value) = 1;
b0fc3e72 1107 TREE_STATIC (value) = 1;
1108 return value;
1109}
d4166bdc 1110
1111/* Given a string of type STRING_TYPE, determine what kind of string
1112 token would give an equivalent execution encoding: CPP_STRING,
1113 CPP_STRING16, or CPP_STRING32. Return CPP_OTHER in case of error.
1114 This may not be exactly the string token type that initially created
1115 the string, since CPP_WSTRING is indistinguishable from the 16/32 bit
1116 string type at this point.
1117
1118 This effectively reverses part of the logic in lex_string and
1119 fix_string_type. */
1120
1121static enum cpp_ttype
1122get_cpp_ttype_from_string_type (tree string_type)
1123{
1124 gcc_assert (string_type);
3da97ff7 1125 if (TREE_CODE (string_type) == POINTER_TYPE)
1126 string_type = TREE_TYPE (string_type);
1127
d4166bdc 1128 if (TREE_CODE (string_type) != ARRAY_TYPE)
1129 return CPP_OTHER;
1130
1131 tree element_type = TREE_TYPE (string_type);
1132 if (TREE_CODE (element_type) != INTEGER_TYPE)
1133 return CPP_OTHER;
1134
1135 int bits_per_character = TYPE_PRECISION (element_type);
1136 switch (bits_per_character)
1137 {
1138 case 8:
1139 return CPP_STRING; /* It could have also been CPP_UTF8STRING. */
1140 case 16:
1141 return CPP_STRING16;
1142 case 32:
1143 return CPP_STRING32;
1144 }
1145
1146 return CPP_OTHER;
1147}
1148
1149/* The global record of string concatentations, for use in
1150 extracting locations within string literals. */
1151
1152GTY(()) string_concat_db *g_string_concat_db;
1153
3da97ff7 1154/* Implementation of LANG_HOOKS_GET_SUBSTRING_LOCATION. */
d4166bdc 1155
1156const char *
3da97ff7 1157c_get_substring_location (const substring_loc &substr_loc,
1158 location_t *out_loc)
d4166bdc 1159{
3da97ff7 1160 enum cpp_ttype tok_type
1161 = get_cpp_ttype_from_string_type (substr_loc.get_string_type ());
d4166bdc 1162 if (tok_type == CPP_OTHER)
1163 return "unrecognized string type";
1164
5927e78e 1165 return get_source_location_for_substring (parse_in, g_string_concat_db,
3da97ff7 1166 substr_loc.get_fmt_string_loc (),
1167 tok_type,
1168 substr_loc.get_caret_idx (),
1169 substr_loc.get_start_idx (),
1170 substr_loc.get_end_idx (),
5927e78e 1171 out_loc);
d4166bdc 1172}
1173
b0fc3e72 1174\f
9c691dbd 1175/* Fold X for consideration by one of the warning functions when checking
1176 whether an expression has a constant value. */
1177
1178static tree
1179fold_for_warn (tree x)
1180{
1181 if (c_dialect_cxx ())
1182 return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
1183 else
1184 /* The C front-end has already folded X appropriately. */
1185 return x;
1186}
1187
2a1736ed 1188/* Print a warning if a constant expression had overflow in folding.
1189 Invoke this function on every expression that the language
1190 requires to be a constant expression.
1191 Note the ANSI C standard says it is erroneous for a
1192 constant expression to overflow. */
b2806639 1193
1194void
1cae46be 1195constant_expression_warning (tree value)
07317e69 1196{
48e1416a 1197 if (warn_overflow && pedantic
07317e69 1198 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1199 || TREE_CODE (value) == FIXED_CST
1200 || TREE_CODE (value) == VECTOR_CST
1201 || TREE_CODE (value) == COMPLEX_CST)
1202 && TREE_OVERFLOW (value))
21ca8540 1203 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
07317e69 1204}
1205
1206/* The same as above but print an unconditional error. */
1207void
1208constant_expression_error (tree value)
b2806639 1209{
837e1122 1210 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
9421ebb9 1211 || TREE_CODE (value) == FIXED_CST
886cfd4f 1212 || TREE_CODE (value) == VECTOR_CST
837e1122 1213 || TREE_CODE (value) == COMPLEX_CST)
07317e69 1214 && TREE_OVERFLOW (value))
1215 error ("overflow in constant expression");
2a1736ed 1216}
1217
f170d67f 1218/* Print a warning if an expression had overflow in folding and its
1219 operands hadn't.
1220
2a1736ed 1221 Invoke this function on every expression that
1222 (1) appears in the source code, and
f170d67f 1223 (2) is a constant expression that overflowed, and
2a1736ed 1224 (3) is not already checked by convert_and_check;
f170d67f 1225 however, do not invoke this function on operands of explicit casts
1226 or when the expression is the result of an operator and any operand
1227 already overflowed. */
2a1736ed 1228
1229void
e60a6f7b 1230overflow_warning (location_t loc, tree value)
2a1736ed 1231{
48d94ede 1232 if (c_inhibit_evaluation_warnings != 0)
1233 return;
f170d67f 1234
1235 switch (TREE_CODE (value))
886cfd4f 1236 {
f170d67f 1237 case INTEGER_CST:
e60a6f7b 1238 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
f170d67f 1239 break;
48e1416a 1240
f170d67f 1241 case REAL_CST:
e60a6f7b 1242 warning_at (loc, OPT_Woverflow,
1243 "floating point overflow in expression");
f170d67f 1244 break;
48e1416a 1245
9421ebb9 1246 case FIXED_CST:
e60a6f7b 1247 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
9421ebb9 1248 break;
1249
f170d67f 1250 case VECTOR_CST:
e60a6f7b 1251 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
f170d67f 1252 break;
48e1416a 1253
f170d67f 1254 case COMPLEX_CST:
1255 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
e60a6f7b 1256 warning_at (loc, OPT_Woverflow,
1257 "complex integer overflow in expression");
f170d67f 1258 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
e60a6f7b 1259 warning_at (loc, OPT_Woverflow,
1260 "complex floating point overflow in expression");
f170d67f 1261 break;
1262
1263 default:
1264 break;
886cfd4f 1265 }
2a1736ed 1266}
1267
03033af4 1268/* Warn about uses of logical || / && operator in a context where it
1269 is likely that the bitwise equivalent was intended by the
1270 programmer. We have seen an expression in which CODE is a binary
9c20c4fc 1271 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1272 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
b13d1547 1273void
9c20c4fc 1274warn_logical_operator (location_t location, enum tree_code code, tree type,
48e1416a 1275 enum tree_code code_left, tree op_left,
03033af4 1276 enum tree_code ARG_UNUSED (code_right), tree op_right)
b13d1547 1277{
9c20c4fc 1278 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1279 int in0_p, in1_p, in_p;
1280 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1281 bool strict_overflow_p = false;
1282
03033af4 1283 if (code != TRUTH_ANDIF_EXPR
1284 && code != TRUTH_AND_EXPR
1285 && code != TRUTH_ORIF_EXPR
1286 && code != TRUTH_OR_EXPR)
1287 return;
1288
439606a9 1289 /* We don't want to warn if either operand comes from a macro
1290 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1291 see PR61534. */
1292 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1293 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1294 return;
1295
03033af4 1296 /* Warn if &&/|| are being used in a context where it is
1297 likely that the bitwise equivalent was intended by the
1298 programmer. That is, an expression such as op && MASK
1299 where op should not be any boolean expression, nor a
1300 constant, and mask seems to be a non-boolean integer constant. */
9c691dbd 1301 if (TREE_CODE (op_right) == CONST_DECL)
1302 /* An enumerator counts as a constant. */
1303 op_right = DECL_INITIAL (op_right);
03033af4 1304 if (!truth_value_p (code_left)
1305 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1306 && !CONSTANT_CLASS_P (op_left)
1307 && !TREE_NO_WARNING (op_left)
1308 && TREE_CODE (op_right) == INTEGER_CST
1309 && !integer_zerop (op_right)
1310 && !integer_onep (op_right))
b13d1547 1311 {
9c20c4fc 1312 if (or_op)
03033af4 1313 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1314 " applied to non-boolean constant");
1315 else
1316 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1317 " applied to non-boolean constant");
1318 TREE_NO_WARNING (op_left) = true;
9c20c4fc 1319 return;
1320 }
1321
1322 /* We do not warn for constants because they are typical of macro
1323 expansions that test for features. */
9c691dbd 1324 if (CONSTANT_CLASS_P (fold_for_warn (op_left))
1325 || CONSTANT_CLASS_P (fold_for_warn (op_right)))
9c20c4fc 1326 return;
1327
1328 /* This warning only makes sense with logical operands. */
1329 if (!(truth_value_p (TREE_CODE (op_left))
1330 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1331 || !(truth_value_p (TREE_CODE (op_right))
1332 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1333 return;
1334
5e84569c 1335 /* The range computations only work with scalars. */
1336 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1337 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1338 return;
9c20c4fc 1339
686369e8 1340 /* We first test whether either side separately is trivially true
1341 (with OR) or trivially false (with AND). If so, do not warn.
1342 This is a common idiom for testing ranges of data types in
1343 portable code. */
1344 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1345 if (!lhs)
1346 return;
1347 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
9c20c4fc 1348 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1349
686369e8 1350 /* If this is an OR operation, invert both sides; now, the result
1351 should be always false to get a warning. */
1352 if (or_op)
1353 in0_p = !in0_p;
1354
1355 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
d42e7c5a 1356 if (tem && integer_zerop (tem))
686369e8 1357 return;
1358
1359 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1360 if (!rhs)
1361 return;
1362 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
9c20c4fc 1363 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
48e1416a 1364
686369e8 1365 /* If this is an OR operation, invert both sides; now, the result
1366 should be always false to get a warning. */
9c20c4fc 1367 if (or_op)
686369e8 1368 in1_p = !in1_p;
48e1416a 1369
686369e8 1370 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
d42e7c5a 1371 if (tem && integer_zerop (tem))
686369e8 1372 return;
1373
1374 /* If both expressions have the same operand, if we can merge the
485f6b9c 1375 ranges, ... */
686369e8 1376 if (operand_equal_p (lhs, rhs, 0)
9c20c4fc 1377 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
485f6b9c 1378 in1_p, low1, high1))
9c20c4fc 1379 {
485f6b9c 1380 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1381 /* ... and if the range test is always false, then warn. */
1382 if (tem && integer_zerop (tem))
1383 {
1384 if (or_op)
1385 warning_at (location, OPT_Wlogical_op,
1386 "logical %<or%> of collectively exhaustive tests is "
1387 "always true");
1388 else
1389 warning_at (location, OPT_Wlogical_op,
1390 "logical %<and%> of mutually exclusive tests is "
1391 "always false");
1392 }
1393 /* Or warn if the operands have exactly the same range, e.g.
1394 A > 0 && A > 0. */
0200602e 1395 else if (tree_int_cst_equal (low0, low1)
1396 && tree_int_cst_equal (high0, high1))
485f6b9c 1397 {
1398 if (or_op)
1399 warning_at (location, OPT_Wlogical_op,
1400 "logical %<or%> of equal expressions");
1401 else
1402 warning_at (location, OPT_Wlogical_op,
1403 "logical %<and%> of equal expressions");
1404 }
b13d1547 1405 }
1406}
1407
6784a472 1408/* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
1409 with constant indices. */
1410
1411static tree
1412find_array_ref_with_const_idx_r (tree *expr_p, int *walk_subtrees, void *data)
1413{
1414 tree expr = *expr_p;
1415
1416 if ((TREE_CODE (expr) == ARRAY_REF
1417 || TREE_CODE (expr) == ARRAY_RANGE_REF)
1418 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
1419 {
1420 *(bool *) data = true;
1421 *walk_subtrees = 0;
1422 }
1423
1424 return NULL_TREE;
1425}
1426
1427/* Warn if a self-comparison always evaluates to true or false. LOC
1428 is the location of the comparison with code CODE, LHS and RHS are
1429 operands of the comparison. */
1430
1431void
1432warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
1433{
1434 if (TREE_CODE_CLASS (code) != tcc_comparison)
1435 return;
1436
d9e4fe27 1437 /* Don't warn for various macro expansions. */
1438 if (from_macro_expansion_at (loc)
1439 || from_macro_expansion_at (EXPR_LOCATION (lhs))
1440 || from_macro_expansion_at (EXPR_LOCATION (rhs)))
1441 return;
1442
6784a472 1443 /* We do not warn for constants because they are typical of macro
1444 expansions that test for features, sizeof, and similar. */
9c691dbd 1445 if (CONSTANT_CLASS_P (fold_for_warn (lhs))
1446 || CONSTANT_CLASS_P (fold_for_warn (rhs)))
6784a472 1447 return;
1448
1449 /* Don't warn for e.g.
1450 HOST_WIDE_INT n;
1451 ...
1452 if (n == (long) n) ...
1453 */
1454 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
1455 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
1456 return;
1457
f58dd84b 1458 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
1459 It could be a NaN, and NaN never compares equal to anything, even
1460 itself. */
1461 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
1462 return;
1463
6784a472 1464 if (operand_equal_p (lhs, rhs, 0))
1465 {
1466 /* Don't warn about array references with constant indices;
1467 these are likely to come from a macro. */
1468 bool found = false;
1469 walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
1470 &found);
1471 if (found)
1472 return;
1473 const bool always_true = (code == EQ_EXPR || code == LE_EXPR
1474 || code == GE_EXPR || code == UNLE_EXPR
1475 || code == UNGE_EXPR || code == UNEQ_EXPR);
1476 if (always_true)
1477 warning_at (loc, OPT_Wtautological_compare,
1478 "self-comparison always evaluates to true");
1479 else
1480 warning_at (loc, OPT_Wtautological_compare,
1481 "self-comparison always evaluates to false");
1482 }
1483}
1484
8f8828ba 1485/* Return true iff T is a boolean promoted to int. */
1486
1487static bool
1488bool_promoted_to_int_p (tree t)
1489{
1490 return (CONVERT_EXPR_P (t)
1491 && TREE_TYPE (t) == integer_type_node
1492 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == BOOLEAN_TYPE);
1493}
1494
1495/* Return true iff EXPR only contains boolean operands, or comparisons. */
1496
1497static bool
1498expr_has_boolean_operands_p (tree expr)
1499{
1500 STRIP_NOPS (expr);
1501
1502 if (CONVERT_EXPR_P (expr))
1503 return bool_promoted_to_int_p (expr);
1504 else if (UNARY_CLASS_P (expr))
1505 return expr_has_boolean_operands_p (TREE_OPERAND (expr, 0));
1506 else if (BINARY_CLASS_P (expr))
1507 return (expr_has_boolean_operands_p (TREE_OPERAND (expr, 0))
1508 && expr_has_boolean_operands_p (TREE_OPERAND (expr, 1)));
1509 else if (COMPARISON_CLASS_P (expr))
1510 return true;
1511 else
1512 return false;
1513}
1514
32dc1512 1515/* Warn about logical not used on the left hand side operand of a comparison.
1516 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
f293b7f2 1517 Do not warn if RHS is of a boolean type, a logical operator, or
1518 a comparison. */
32dc1512 1519
1520void
1521warn_logical_not_parentheses (location_t location, enum tree_code code,
c4963714 1522 tree lhs, tree rhs)
32dc1512 1523{
dc6229e8 1524 if (TREE_CODE_CLASS (code) != tcc_comparison
1525 || TREE_TYPE (rhs) == NULL_TREE
f293b7f2 1526 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE
1527 || truth_value_p (TREE_CODE (rhs)))
32dc1512 1528 return;
1529
8f8828ba 1530 /* Don't warn for expression like !x == ~(bool1 | bool2). */
1531 if (expr_has_boolean_operands_p (rhs))
1532 return;
1533
16f958b3 1534 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1535 !(x == 0) or !(y != 0). */
1536 if ((code == EQ_EXPR || code == NE_EXPR)
1537 && integer_zerop (rhs))
1538 return;
1539
c4963714 1540 if (warning_at (location, OPT_Wlogical_not_parentheses,
1541 "logical not is only applied to the left hand side of "
1542 "comparison")
1543 && EXPR_HAS_LOCATION (lhs))
1544 {
1545 location_t lhs_loc = EXPR_LOCATION (lhs);
1546 rich_location richloc (line_table, lhs_loc);
1547 richloc.add_fixit_insert (lhs_loc, "(");
1548 location_t finish = get_finish (lhs_loc);
1549 location_t next_loc
1550 = linemap_position_for_loc_and_offset (line_table, finish, 1);
1551 richloc.add_fixit_insert (next_loc, ")");
1552 inform_at_rich_loc (&richloc, "add parentheses around left hand side "
1553 "expression to silence this warning");
1554 }
32dc1512 1555}
b13d1547 1556
3d177e8c 1557/* Warn if EXP contains any computations whose results are not used.
1558 Return true if a warning is printed; false otherwise. LOCUS is the
1559 (potential) location of the expression. */
1560
1561bool
1562warn_if_unused_value (const_tree exp, location_t locus)
1563{
1564 restart:
1565 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1566 return false;
1567
1568 /* Don't warn about void constructs. This includes casting to void,
1569 void function calls, and statement expressions with a final cast
1570 to void. */
1571 if (VOID_TYPE_P (TREE_TYPE (exp)))
1572 return false;
1573
1574 if (EXPR_HAS_LOCATION (exp))
1575 locus = EXPR_LOCATION (exp);
1576
1577 switch (TREE_CODE (exp))
1578 {
1579 case PREINCREMENT_EXPR:
1580 case POSTINCREMENT_EXPR:
1581 case PREDECREMENT_EXPR:
1582 case POSTDECREMENT_EXPR:
1583 case MODIFY_EXPR:
1584 case INIT_EXPR:
1585 case TARGET_EXPR:
1586 case CALL_EXPR:
1587 case TRY_CATCH_EXPR:
1588 case WITH_CLEANUP_EXPR:
1589 case EXIT_EXPR:
1590 case VA_ARG_EXPR:
1591 return false;
1592
1593 case BIND_EXPR:
1594 /* For a binding, warn if no side effect within it. */
1595 exp = BIND_EXPR_BODY (exp);
1596 goto restart;
1597
1598 case SAVE_EXPR:
1599 case NON_LVALUE_EXPR:
d85dbdb3 1600 case NOP_EXPR:
3d177e8c 1601 exp = TREE_OPERAND (exp, 0);
1602 goto restart;
1603
1604 case TRUTH_ORIF_EXPR:
1605 case TRUTH_ANDIF_EXPR:
1606 /* In && or ||, warn if 2nd operand has no side effect. */
1607 exp = TREE_OPERAND (exp, 1);
1608 goto restart;
1609
1610 case COMPOUND_EXPR:
1611 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1612 return true;
1613 /* Let people do `(foo (), 0)' without a warning. */
1614 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1615 return false;
1616 exp = TREE_OPERAND (exp, 1);
1617 goto restart;
1618
1619 case COND_EXPR:
1620 /* If this is an expression with side effects, don't warn; this
1621 case commonly appears in macro expansions. */
1622 if (TREE_SIDE_EFFECTS (exp))
1623 return false;
1624 goto warn;
1625
1626 case INDIRECT_REF:
1627 /* Don't warn about automatic dereferencing of references, since
1628 the user cannot control it. */
1629 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1630 {
1631 exp = TREE_OPERAND (exp, 0);
1632 goto restart;
1633 }
1634 /* Fall through. */
1635
1636 default:
1637 /* Referencing a volatile value is a side effect, so don't warn. */
1638 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1639 && TREE_THIS_VOLATILE (exp))
1640 return false;
1641
1642 /* If this is an expression which has no operands, there is no value
1643 to be unused. There are no such language-independent codes,
1644 but front ends may define such. */
1645 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1646 return false;
1647
1648 warn:
1649 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1650 }
1651}
1652
1653
bcf22371 1654/* Print a warning about casts that might indicate violation
1655 of strict aliasing rules if -Wstrict-aliasing is used and
1e31ff37 1656 strict aliasing mode is in effect. OTYPE is the original
1657 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
bcf22371 1658
e6fa0ea6 1659bool
1e31ff37 1660strict_aliasing_warning (tree otype, tree type, tree expr)
bcf22371 1661{
45bb3afb 1662 /* Strip pointer conversion chains and get to the correct original type. */
1663 STRIP_NOPS (expr);
1664 otype = TREE_TYPE (expr);
1665
f06537f2 1666 if (!(flag_strict_aliasing
1667 && POINTER_TYPE_P (type)
1668 && POINTER_TYPE_P (otype)
1669 && !VOID_TYPE_P (TREE_TYPE (type)))
1670 /* If the type we are casting to is a ref-all pointer
1671 dereferencing it is always valid. */
1672 || TYPE_REF_CAN_ALIAS_ALL (type))
e6fa0ea6 1673 return false;
1674
1675 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
bcf22371 1676 && (DECL_P (TREE_OPERAND (expr, 0))
e6fa0ea6 1677 || handled_component_p (TREE_OPERAND (expr, 0))))
bcf22371 1678 {
1679 /* Casting the address of an object to non void pointer. Warn
1680 if the cast breaks type based aliasing. */
e6fa0ea6 1681 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1682 {
1683 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1684 "might break strict-aliasing rules");
1685 return true;
1686 }
bcf22371 1687 else
1688 {
48e1416a 1689 /* warn_strict_aliasing >= 3. This includes the default (3).
e6fa0ea6 1690 Only warn if the cast is dereferenced immediately. */
32c2fdea 1691 alias_set_type set1 =
e6fa0ea6 1692 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
32c2fdea 1693 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
bcf22371 1694
62d823d0 1695 if (set1 != set2 && set2 != 0
42bb3bfb 1696 && (set1 == 0
1697 || (!alias_set_subset_of (set2, set1)
1698 && !alias_sets_conflict_p (set1, set2))))
e6fa0ea6 1699 {
1700 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1701 "pointer will break strict-aliasing rules");
1702 return true;
1703 }
1704 else if (warn_strict_aliasing == 2
879f881c 1705 && !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 1706 {
1707 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1708 "pointer might break strict-aliasing rules");
1709 return true;
1710 }
bcf22371 1711 }
1712 }
e6fa0ea6 1713 else
1714 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1715 {
1716 /* At this level, warn for any conversions, even if an address is
1717 not taken in the same statement. This will likely produce many
1718 false positives, but could be useful to pinpoint problems that
1719 are not revealed at higher levels. */
32c2fdea 1720 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1721 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1722 if (!COMPLETE_TYPE_P (type)
879f881c 1723 || !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 1724 {
1725 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1726 "pointer might break strict-aliasing rules");
1727 return true;
1728 }
1729 }
1730
1731 return false;
bcf22371 1732}
1733
f003f5dc 1734/* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1735 sizeof as last operand of certain builtins. */
1736
1737void
57f872a2 1738sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
f1f41a6c 1739 vec<tree, va_gc> *params, tree *sizeof_arg,
f003f5dc 1740 bool (*comp_types) (tree, tree))
1741{
1742 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
57f872a2 1743 bool strop = false, cmp = false;
1744 unsigned int idx = ~0;
1745 location_t loc;
f003f5dc 1746
1747 if (TREE_CODE (callee) != FUNCTION_DECL
1748 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
f1f41a6c 1749 || vec_safe_length (params) <= 1)
f003f5dc 1750 return;
1751
f003f5dc 1752 switch (DECL_FUNCTION_CODE (callee))
1753 {
1754 case BUILT_IN_STRNCMP:
1755 case BUILT_IN_STRNCASECMP:
57f872a2 1756 cmp = true;
1757 /* FALLTHRU */
f003f5dc 1758 case BUILT_IN_STRNCPY:
57f872a2 1759 case BUILT_IN_STRNCPY_CHK:
f003f5dc 1760 case BUILT_IN_STRNCAT:
57f872a2 1761 case BUILT_IN_STRNCAT_CHK:
1762 case BUILT_IN_STPNCPY:
1763 case BUILT_IN_STPNCPY_CHK:
f003f5dc 1764 strop = true;
1765 /* FALLTHRU */
1766 case BUILT_IN_MEMCPY:
57f872a2 1767 case BUILT_IN_MEMCPY_CHK:
f003f5dc 1768 case BUILT_IN_MEMMOVE:
57f872a2 1769 case BUILT_IN_MEMMOVE_CHK:
f1f41a6c 1770 if (params->length () < 3)
57f872a2 1771 return;
f1f41a6c 1772 src = (*params)[1];
1773 dest = (*params)[0];
57f872a2 1774 idx = 2;
1775 break;
1776 case BUILT_IN_BCOPY:
f1f41a6c 1777 if (params->length () < 3)
57f872a2 1778 return;
f1f41a6c 1779 src = (*params)[0];
1780 dest = (*params)[1];
57f872a2 1781 idx = 2;
1782 break;
f003f5dc 1783 case BUILT_IN_MEMCMP:
57f872a2 1784 case BUILT_IN_BCMP:
f1f41a6c 1785 if (params->length () < 3)
f003f5dc 1786 return;
f1f41a6c 1787 src = (*params)[1];
1788 dest = (*params)[0];
57f872a2 1789 idx = 2;
1790 cmp = true;
f003f5dc 1791 break;
1792 case BUILT_IN_MEMSET:
57f872a2 1793 case BUILT_IN_MEMSET_CHK:
f1f41a6c 1794 if (params->length () < 3)
f003f5dc 1795 return;
f1f41a6c 1796 dest = (*params)[0];
57f872a2 1797 idx = 2;
1798 break;
1799 case BUILT_IN_BZERO:
f1f41a6c 1800 dest = (*params)[0];
57f872a2 1801 idx = 1;
f003f5dc 1802 break;
1803 case BUILT_IN_STRNDUP:
f1f41a6c 1804 src = (*params)[0];
f003f5dc 1805 strop = true;
57f872a2 1806 idx = 1;
1807 break;
1808 case BUILT_IN_MEMCHR:
f1f41a6c 1809 if (params->length () < 3)
57f872a2 1810 return;
f1f41a6c 1811 src = (*params)[0];
57f872a2 1812 idx = 2;
1813 break;
1814 case BUILT_IN_SNPRINTF:
1815 case BUILT_IN_SNPRINTF_CHK:
1816 case BUILT_IN_VSNPRINTF:
1817 case BUILT_IN_VSNPRINTF_CHK:
f1f41a6c 1818 dest = (*params)[0];
57f872a2 1819 idx = 1;
1820 strop = true;
f003f5dc 1821 break;
1822 default:
1823 break;
1824 }
1825
57f872a2 1826 if (idx >= 3)
1827 return;
1828
1829 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1830 return;
1831
1832 type = TYPE_P (sizeof_arg[idx])
1833 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1834 if (!POINTER_TYPE_P (type))
1835 return;
1836
f003f5dc 1837 if (dest
1838 && (tem = tree_strip_nop_conversions (dest))
1839 && POINTER_TYPE_P (TREE_TYPE (tem))
1840 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1841 return;
1842
1843 if (src
1844 && (tem = tree_strip_nop_conversions (src))
1845 && POINTER_TYPE_P (TREE_TYPE (tem))
1846 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1847 return;
1848
57f872a2 1849 loc = sizeof_arg_loc[idx];
1850
1851 if (dest && !cmp)
f003f5dc 1852 {
57f872a2 1853 if (!TYPE_P (sizeof_arg[idx])
1854 && operand_equal_p (dest, sizeof_arg[idx], 0)
f003f5dc 1855 && comp_types (TREE_TYPE (dest), type))
1856 {
57f872a2 1857 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
f003f5dc 1858 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1859 "argument to %<sizeof%> in %qD call is the same "
1860 "expression as the destination; did you mean to "
1861 "remove the addressof?", callee);
1862 else if ((TYPE_PRECISION (TREE_TYPE (type))
1863 == TYPE_PRECISION (char_type_node))
1864 || strop)
1865 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1866 "argument to %<sizeof%> in %qD call is the same "
1867 "expression as the destination; did you mean to "
1868 "provide an explicit length?", callee);
1869 else
1870 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1871 "argument to %<sizeof%> in %qD call is the same "
1872 "expression as the destination; did you mean to "
1873 "dereference it?", callee);
1874 return;
1875 }
1876
1877 if (POINTER_TYPE_P (TREE_TYPE (dest))
1878 && !strop
1879 && comp_types (TREE_TYPE (dest), type)
1880 && !VOID_TYPE_P (TREE_TYPE (type)))
1881 {
1882 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1883 "argument to %<sizeof%> in %qD call is the same "
1884 "pointer type %qT as the destination; expected %qT "
1885 "or an explicit length", callee, TREE_TYPE (dest),
1886 TREE_TYPE (TREE_TYPE (dest)));
1887 return;
1888 }
1889 }
1890
57f872a2 1891 if (src && !cmp)
f003f5dc 1892 {
57f872a2 1893 if (!TYPE_P (sizeof_arg[idx])
1894 && operand_equal_p (src, sizeof_arg[idx], 0)
f003f5dc 1895 && comp_types (TREE_TYPE (src), type))
1896 {
57f872a2 1897 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
f003f5dc 1898 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1899 "argument to %<sizeof%> in %qD call is the same "
1900 "expression as the source; did you mean to "
1901 "remove the addressof?", callee);
1902 else if ((TYPE_PRECISION (TREE_TYPE (type))
1903 == TYPE_PRECISION (char_type_node))
1904 || strop)
1905 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1906 "argument to %<sizeof%> in %qD call is the same "
1907 "expression as the source; did you mean to "
1908 "provide an explicit length?", callee);
1909 else
1910 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1911 "argument to %<sizeof%> in %qD call is the same "
1912 "expression as the source; did you mean to "
1913 "dereference it?", callee);
1914 return;
1915 }
1916
1917 if (POINTER_TYPE_P (TREE_TYPE (src))
1918 && !strop
1919 && comp_types (TREE_TYPE (src), type)
1920 && !VOID_TYPE_P (TREE_TYPE (type)))
1921 {
1922 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1923 "argument to %<sizeof%> in %qD call is the same "
1924 "pointer type %qT as the source; expected %qT "
1925 "or an explicit length", callee, TREE_TYPE (src),
1926 TREE_TYPE (TREE_TYPE (src)));
1927 return;
1928 }
1929 }
57f872a2 1930
1931 if (dest)
1932 {
1933 if (!TYPE_P (sizeof_arg[idx])
1934 && operand_equal_p (dest, sizeof_arg[idx], 0)
1935 && comp_types (TREE_TYPE (dest), type))
1936 {
1937 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1938 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1939 "argument to %<sizeof%> in %qD call is the same "
1940 "expression as the first source; did you mean to "
1941 "remove the addressof?", callee);
1942 else if ((TYPE_PRECISION (TREE_TYPE (type))
1943 == TYPE_PRECISION (char_type_node))
1944 || strop)
1945 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1946 "argument to %<sizeof%> in %qD call is the same "
1947 "expression as the first source; did you mean to "
1948 "provide an explicit length?", callee);
1949 else
1950 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1951 "argument to %<sizeof%> in %qD call is the same "
1952 "expression as the first source; did you mean to "
1953 "dereference it?", callee);
1954 return;
1955 }
1956
1957 if (POINTER_TYPE_P (TREE_TYPE (dest))
1958 && !strop
1959 && comp_types (TREE_TYPE (dest), type)
1960 && !VOID_TYPE_P (TREE_TYPE (type)))
1961 {
1962 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1963 "argument to %<sizeof%> in %qD call is the same "
1964 "pointer type %qT as the first source; expected %qT "
1965 "or an explicit length", callee, TREE_TYPE (dest),
1966 TREE_TYPE (TREE_TYPE (dest)));
1967 return;
1968 }
1969 }
1970
1971 if (src)
1972 {
1973 if (!TYPE_P (sizeof_arg[idx])
1974 && operand_equal_p (src, sizeof_arg[idx], 0)
1975 && comp_types (TREE_TYPE (src), type))
1976 {
1977 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1978 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1979 "argument to %<sizeof%> in %qD call is the same "
1980 "expression as the second source; did you mean to "
1981 "remove the addressof?", callee);
1982 else if ((TYPE_PRECISION (TREE_TYPE (type))
1983 == TYPE_PRECISION (char_type_node))
1984 || strop)
1985 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1986 "argument to %<sizeof%> in %qD call is the same "
1987 "expression as the second source; did you mean to "
1988 "provide an explicit length?", callee);
1989 else
1990 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1991 "argument to %<sizeof%> in %qD call is the same "
1992 "expression as the second source; did you mean to "
1993 "dereference it?", callee);
1994 return;
1995 }
1996
1997 if (POINTER_TYPE_P (TREE_TYPE (src))
1998 && !strop
1999 && comp_types (TREE_TYPE (src), type)
2000 && !VOID_TYPE_P (TREE_TYPE (type)))
2001 {
2002 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2003 "argument to %<sizeof%> in %qD call is the same "
2004 "pointer type %qT as the second source; expected %qT "
2005 "or an explicit length", callee, TREE_TYPE (src),
2006 TREE_TYPE (TREE_TYPE (src)));
2007 return;
2008 }
2009 }
2010
f003f5dc 2011}
2012
3f08e399 2013/* Warn for unlikely, improbable, or stupid DECL declarations
2014 of `main'. */
2015
2016void
2017check_main_parameter_types (tree decl)
2018{
d0af78c5 2019 function_args_iterator iter;
2020 tree type;
3f08e399 2021 int argct = 0;
2022
d0af78c5 2023 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2024 {
2025 /* XXX void_type_node belies the abstraction. */
2026 if (type == void_type_node || type == error_mark_node )
2027 break;
2028
2026249a 2029 tree t = type;
2030 if (TYPE_ATOMIC (t))
2031 pedwarn (input_location, OPT_Wmain,
2032 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2033 type, decl);
2034 while (POINTER_TYPE_P (t))
2035 {
2036 t = TREE_TYPE (t);
2037 if (TYPE_ATOMIC (t))
2038 pedwarn (input_location, OPT_Wmain,
2039 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2040 type, decl);
2041 }
2042
d0af78c5 2043 ++argct;
2044 switch (argct)
2045 {
2046 case 1:
2047 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2048 pedwarn (input_location, OPT_Wmain,
2049 "first argument of %q+D should be %<int%>", decl);
2050 break;
2051
2052 case 2:
2053 if (TREE_CODE (type) != POINTER_TYPE
2054 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2055 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2056 != char_type_node))
2057 pedwarn (input_location, OPT_Wmain,
2058 "second argument of %q+D should be %<char **%>", decl);
2059 break;
2060
2061 case 3:
2062 if (TREE_CODE (type) != POINTER_TYPE
2063 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2064 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2065 != char_type_node))
2066 pedwarn (input_location, OPT_Wmain,
2067 "third argument of %q+D should probably be "
2068 "%<char **%>", decl);
2069 break;
2070 }
2071 }
3f08e399 2072
2073 /* It is intentional that this message does not mention the third
2074 argument because it's only mentioned in an appendix of the
2075 standard. */
2076 if (argct > 0 && (argct < 2 || argct > 3))
d0af78c5 2077 pedwarn (input_location, OPT_Wmain,
2078 "%q+D takes only zero or two arguments", decl);
90e645fa 2079
2080 if (stdarg_p (TREE_TYPE (decl)))
2081 pedwarn (input_location, OPT_Wmain,
2082 "%q+D declared as variadic function", decl);
3f08e399 2083}
2084
73437615 2085/* vector_targets_convertible_p is used for vector pointer types. The
2086 callers perform various checks that the qualifiers are satisfactory,
2087 while OTOH vector_targets_convertible_p ignores the number of elements
2088 in the vectors. That's fine with vector pointers as we can consider,
2089 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2090 and that does not require and conversion of the pointer values.
2091 In contrast, vector_types_convertible_p and
2092 vector_types_compatible_elements_p are used for vector value types. */
ed7c4e62 2093/* True if pointers to distinct types T1 and T2 can be converted to
2094 each other without an explicit cast. Only returns true for opaque
2095 vector types. */
2096bool
2097vector_targets_convertible_p (const_tree t1, const_tree t2)
2098{
6290f0db 2099 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
8d125f7d 2100 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ed7c4e62 2101 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2102 return true;
2103
2104 return false;
2105}
2106
73437615 2107/* vector_types_convertible_p is used for vector value types.
2108 It could in principle call vector_targets_convertible_p as a subroutine,
2109 but then the check for vector type would be duplicated with its callers,
2110 and also the purpose of vector_targets_convertible_p would become
2111 muddled.
2112 Where vector_types_convertible_p returns true, a conversion might still be
2113 needed to make the types match.
2114 In contrast, vector_targets_convertible_p is used for vector pointer
2115 values, and vector_types_compatible_elements_p is used specifically
2116 in the context for binary operators, as a check if use is possible without
2117 conversion. */
546c4794 2118/* True if vector types T1 and T2 can be converted to each other
2119 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2120 can only be converted with -flax-vector-conversions yet that is not
2121 in effect, emit a note telling the user about that option if such
2122 a note has not previously been emitted. */
2123bool
9f627b1a 2124vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
8b4b9810 2125{
546c4794 2126 static bool emitted_lax_note = false;
ae6db8ab 2127 bool convertible_lax;
2128
8d125f7d 2129 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ae6db8ab 2130 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2131 return true;
2132
2133 convertible_lax =
2134 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2135 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
432dd330 2136 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
ae6db8ab 2137 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2138 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
546c4794 2139
2140 if (!convertible_lax || flag_lax_vector_conversions)
2141 return convertible_lax;
2142
2143 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
29f51994 2144 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
546c4794 2145 return true;
2146
2147 if (emit_lax_note && !emitted_lax_note)
2148 {
2149 emitted_lax_note = true;
5bcc316e 2150 inform (input_location, "use -flax-vector-conversions to permit "
546c4794 2151 "conversions between vectors with differing "
2152 "element types or numbers of subparts");
2153 }
2154
2155 return false;
8b4b9810 2156}
2157
bf0cb017 2158/* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2159 and have vector types, V0 has the same type as V1, and the number of
2160 elements of V0, V1, MASK is the same.
2161
2162 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2163 called with two arguments. In this case implementation passes the
2164 first argument twice in order to share the same tree code. This fact
2165 could enable the mask-values being twice the vector length. This is
2166 an implementation accident and this semantics is not guaranteed to
2167 the user. */
2168tree
68ea4406 2169c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2170 bool complain)
bf0cb017 2171{
2172 tree ret;
2173 bool wrap = true;
2174 bool maybe_const = false;
2175 bool two_arguments = false;
2176
2177 if (v1 == NULL_TREE)
2178 {
2179 two_arguments = true;
2180 v1 = v0;
2181 }
2182
2183 if (v0 == error_mark_node || v1 == error_mark_node
2184 || mask == error_mark_node)
2185 return error_mark_node;
2186
6290f0db 2187 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
bf0cb017 2188 {
68ea4406 2189 if (complain)
2190 error_at (loc, "__builtin_shuffle last argument must "
2191 "be an integer vector");
bf0cb017 2192 return error_mark_node;
2193 }
2194
6290f0db 2195 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
2196 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
bf0cb017 2197 {
68ea4406 2198 if (complain)
2199 error_at (loc, "__builtin_shuffle arguments must be vectors");
bf0cb017 2200 return error_mark_node;
2201 }
2202
2203 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2204 {
68ea4406 2205 if (complain)
2206 error_at (loc, "__builtin_shuffle argument vectors must be of "
2207 "the same type");
bf0cb017 2208 return error_mark_node;
2209 }
2210
2211 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2212 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2213 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2214 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2215 {
68ea4406 2216 if (complain)
2217 error_at (loc, "__builtin_shuffle number of elements of the "
2218 "argument vector(s) and the mask vector should "
2219 "be the same");
bf0cb017 2220 return error_mark_node;
2221 }
2222
2223 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2224 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2225 {
68ea4406 2226 if (complain)
2227 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2228 "must have the same size as inner type of the mask");
bf0cb017 2229 return error_mark_node;
2230 }
2231
2232 if (!c_dialect_cxx ())
2233 {
2234 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2235 v0 = c_fully_fold (v0, false, &maybe_const);
2236 wrap &= maybe_const;
2237
2238 if (two_arguments)
2239 v1 = v0 = save_expr (v0);
2240 else
2241 {
2242 v1 = c_fully_fold (v1, false, &maybe_const);
2243 wrap &= maybe_const;
2244 }
2245
2246 mask = c_fully_fold (mask, false, &maybe_const);
2247 wrap &= maybe_const;
2248 }
68ea4406 2249 else if (two_arguments)
2250 v1 = v0 = save_expr (v0);
bf0cb017 2251
2252 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2253
2254 if (!c_dialect_cxx () && !wrap)
2255 ret = c_wrap_maybe_const (ret, true);
2256
2257 return ret;
2258}
2259
7f506bca 2260/* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2261 to integral type. */
2262
2263static tree
2264c_common_get_narrower (tree op, int *unsignedp_ptr)
2265{
2266 op = get_narrower (op, unsignedp_ptr);
2267
2268 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2269 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2270 {
2271 /* C++0x scoped enumerations don't implicitly convert to integral
2272 type; if we stripped an explicit conversion to a larger type we
2273 need to replace it so common_type will still work. */
a51edb4c 2274 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2275 TYPE_UNSIGNED (TREE_TYPE (op)));
7f506bca 2276 op = fold_convert (type, op);
2277 }
2278 return op;
2279}
2280
2561cea2 2281/* This is a helper function of build_binary_op.
2282
2283 For certain operations if both args were extended from the same
2284 smaller type, do the arithmetic in that type and then extend.
2285
2286 BITWISE indicates a bitwise operation.
2287 For them, this optimization is safe only if
2288 both args are zero-extended or both are sign-extended.
2289 Otherwise, we might change the result.
2290 Eg, (short)-1 | (unsigned short)-1 is (int)-1
48e1416a 2291 but calculated in (unsigned short) it would be (unsigned short)-1.
2561cea2 2292*/
7f506bca 2293tree
2294shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2561cea2 2295{
2296 int unsigned0, unsigned1;
2297 tree arg0, arg1;
2298 int uns;
2299 tree type;
2300
2301 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2302 excessive narrowing when we call get_narrower below. For
2303 example, suppose that OP0 is of unsigned int extended
2304 from signed char and that RESULT_TYPE is long long int.
2305 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2306 like
48e1416a 2307
2561cea2 2308 (long long int) (unsigned int) signed_char
2309
2310 which get_narrower would narrow down to
48e1416a 2311
2561cea2 2312 (unsigned int) signed char
48e1416a 2313
2561cea2 2314 If we do not cast OP0 first, get_narrower would return
2315 signed_char, which is inconsistent with the case of the
2316 explicit cast. */
2317 op0 = convert (result_type, op0);
2318 op1 = convert (result_type, op1);
2319
7f506bca 2320 arg0 = c_common_get_narrower (op0, &unsigned0);
2321 arg1 = c_common_get_narrower (op1, &unsigned1);
ab2c1de8 2322
2561cea2 2323 /* UNS is 1 if the operation to be done is an unsigned one. */
2324 uns = TYPE_UNSIGNED (result_type);
2325
2326 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2327 but it *requires* conversion to FINAL_TYPE. */
48e1416a 2328
2561cea2 2329 if ((TYPE_PRECISION (TREE_TYPE (op0))
2330 == TYPE_PRECISION (TREE_TYPE (arg0)))
2331 && TREE_TYPE (op0) != result_type)
2332 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2333 if ((TYPE_PRECISION (TREE_TYPE (op1))
2334 == TYPE_PRECISION (TREE_TYPE (arg1)))
2335 && TREE_TYPE (op1) != result_type)
2336 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
48e1416a 2337
2561cea2 2338 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
48e1416a 2339
2561cea2 2340 /* For bitwise operations, signedness of nominal type
2341 does not matter. Consider only how operands were extended. */
2342 if (bitwise)
2343 uns = unsigned0;
48e1416a 2344
2561cea2 2345 /* Note that in all three cases below we refrain from optimizing
2346 an unsigned operation on sign-extended args.
2347 That would not be valid. */
48e1416a 2348
2561cea2 2349 /* Both args variable: if both extended in same way
2350 from same width, do it in that width.
2351 Do it unsigned if args were zero-extended. */
2352 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2353 < TYPE_PRECISION (result_type))
2354 && (TYPE_PRECISION (TREE_TYPE (arg1))
2355 == TYPE_PRECISION (TREE_TYPE (arg0)))
2356 && unsigned0 == unsigned1
2357 && (unsigned0 || !uns))
2358 return c_common_signed_or_unsigned_type
2359 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2360
2361 else if (TREE_CODE (arg0) == INTEGER_CST
2362 && (unsigned1 || !uns)
2363 && (TYPE_PRECISION (TREE_TYPE (arg1))
2364 < TYPE_PRECISION (result_type))
2365 && (type
2366 = c_common_signed_or_unsigned_type (unsigned1,
2367 TREE_TYPE (arg1)))
2368 && !POINTER_TYPE_P (type)
2369 && int_fits_type_p (arg0, type))
2370 return type;
2371
2372 else if (TREE_CODE (arg1) == INTEGER_CST
2373 && (unsigned0 || !uns)
2374 && (TYPE_PRECISION (TREE_TYPE (arg0))
2375 < TYPE_PRECISION (result_type))
2376 && (type
2377 = c_common_signed_or_unsigned_type (unsigned0,
2378 TREE_TYPE (arg0)))
2379 && !POINTER_TYPE_P (type)
2380 && int_fits_type_p (arg1, type))
2381 return type;
2382
2383 return result_type;
2384}
2385
e53013a8 2386/* Returns true iff any integer value of type FROM_TYPE can be represented as
2387 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2388
2389static bool
2390int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2391{
2392 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2393 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2394 REAL_VALUE_TYPE real_low_bound =
2395 real_value_from_int_cst (0, type_low_bound);
2396 REAL_VALUE_TYPE real_high_bound =
2397 real_value_from_int_cst (0, type_high_bound);
2398
2399 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2400 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2401}
2402
2403/* Checks if expression EXPR of complex/real/integer type cannot be converted
2404 to the complex/real/integer type TYPE. Function returns non-zero when:
22a75734 2405 * EXPR is a constant which cannot be exactly converted to TYPE.
2406 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
e53013a8 2407 for EXPR type and TYPE being both integers or both real, or both
2408 complex.
2409 * EXPR is not a constant of complex type and TYPE is a real or
2410 an integer.
22a75734 2411 * EXPR is not a constant of real type and TYPE is an integer.
2412 * EXPR is not a constant of integer type which cannot be
2413 exactly converted to real type.
e53013a8 2414
7dfa155b 2415 Function allows conversions between types of different signedness and
ca9d7d74 2416 can return SAFE_CONVERSION (zero) in that case. Function can produce
e53013a8 2417 signedness warnings if PRODUCE_WARNS is true.
2418
2419 Function allows conversions from complex constants to non-complex types,
2420 provided that imaginary part is zero and real part can be safely converted
2421 to TYPE. */
22a75734 2422
ca9d7d74 2423enum conversion_safety
22a75734 2424unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
d31d55f0 2425{
ca9d7d74 2426 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
27259707 2427 tree expr_type = TREE_TYPE (expr);
22a75734 2428 loc = expansion_point_location_if_in_system_header (loc);
d31d55f0 2429
7dfa155b 2430 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
5b16c152 2431 {
e53013a8 2432 /* If type is complex, we are interested in compatibility with
2433 underlying type. */
2434 if (TREE_CODE (type) == COMPLEX_TYPE)
2435 type = TREE_TYPE (type);
2436
d31d55f0 2437 /* Warn for real constant that is not an exact integer converted
7dfa155b 2438 to integer type. */
27259707 2439 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2440 && TREE_CODE (type) == INTEGER_TYPE)
2441 {
2442 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
ca9d7d74 2443 give_warning = UNSAFE_REAL;
7dfa155b 2444 }
da1fb07b 2445 /* Warn for an integer constant that does not fit into integer type. */
27259707 2446 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2447 && TREE_CODE (type) == INTEGER_TYPE
2448 && !int_fits_type_p (expr, type))
2449 {
2450 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
27259707 2451 && tree_int_cst_sgn (expr) < 0)
7dfa155b 2452 {
2453 if (produce_warns)
2454 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2455 " implicitly converted to unsigned type");
2456 }
2457 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2458 {
2459 if (produce_warns)
2460 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2461 " constant value to negative integer");
2462 }
7ee0d227 2463 else
ca9d7d74 2464 give_warning = UNSAFE_OTHER;
7dfa155b 2465 }
d31d55f0 2466 else if (TREE_CODE (type) == REAL_TYPE)
7dfa155b 2467 {
2468 /* Warn for an integer constant that does not fit into real type. */
2469 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2470 {
2471 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2472 if (!exact_real_truncate (TYPE_MODE (type), &a))
ca9d7d74 2473 give_warning = UNSAFE_REAL;
7dfa155b 2474 }
2475 /* Warn for a real constant that does not fit into a smaller
2476 real type. */
2477 else if (TREE_CODE (expr_type) == REAL_TYPE
2478 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2479 {
2480 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2481 if (!exact_real_truncate (TYPE_MODE (type), &a))
ca9d7d74 2482 give_warning = UNSAFE_REAL;
7dfa155b 2483 }
2484 }
2485 }
e53013a8 2486
2487 else if (TREE_CODE (expr) == COMPLEX_CST)
2488 {
2489 tree imag_part = TREE_IMAGPART (expr);
2490 /* Conversion from complex constant with zero imaginary part,
2491 perform check for conversion of real part. */
2492 if ((TREE_CODE (imag_part) == REAL_CST
2493 && real_zerop (imag_part))
2494 || (TREE_CODE (imag_part) == INTEGER_CST
2495 && integer_zerop (imag_part)))
2496 /* Note: in this branch we use recursive call to unsafe_conversion_p
2497 with different type of EXPR, but it is still safe, because when EXPR
2498 is a constant, it's type is not used in text of generated warnings
2499 (otherwise they could sound misleading). */
2500 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2501 produce_warns);
2502 /* Conversion from complex constant with non-zero imaginary part. */
2503 else
2504 {
2505 /* Conversion to complex type.
2506 Perform checks for both real and imaginary parts. */
2507 if (TREE_CODE (type) == COMPLEX_TYPE)
2508 {
2509 /* Unfortunately, produce_warns must be false in two subsequent
2510 calls of unsafe_conversion_p, because otherwise we could
2511 produce strange "double" warnings, if both real and imaginary
2512 parts have conversion problems related to signedness.
2513
2514 For example:
2515 int32_t _Complex a = 0x80000000 + 0x80000000i;
2516
2517 Possible solution: add a separate function for checking
2518 constants and combine result of two calls appropriately. */
2519 enum conversion_safety re_safety =
2520 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2521 enum conversion_safety im_safety =
2522 unsafe_conversion_p (loc, type, imag_part, false);
2523
2524 /* Merge the results into appropriate single warning. */
2525
2526 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2527 if (re_safety == im_safety)
2528 give_warning = re_safety;
2529 else if (!re_safety && im_safety)
2530 give_warning = im_safety;
2531 else if (re_safety && !im_safety)
2532 give_warning = re_safety;
2533 else
2534 give_warning = UNSAFE_OTHER;
2535 }
2536 /* Warn about conversion from complex to real or integer type. */
2537 else
2538 give_warning = UNSAFE_IMAGINARY;
2539 }
2540 }
2541
2542 /* Checks for remaining case: EXPR is not constant. */
7dfa155b 2543 else
2544 {
d31d55f0 2545 /* Warn for real types converted to integer types. */
2561cea2 2546 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2547 && TREE_CODE (type) == INTEGER_TYPE)
ca9d7d74 2548 give_warning = UNSAFE_REAL;
d31d55f0 2549
2561cea2 2550 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2551 && TREE_CODE (type) == INTEGER_TYPE)
2552 {
69609004 2553 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
f9d856a4 2554 expr = get_unwidened (expr, 0);
2561cea2 2555 expr_type = TREE_TYPE (expr);
69609004 2556
2561cea2 2557 /* Don't warn for short y; short x = ((int)y & 0xff); */
48e1416a 2558 if (TREE_CODE (expr) == BIT_AND_EXPR
7dfa155b 2559 || TREE_CODE (expr) == BIT_IOR_EXPR
2561cea2 2560 || TREE_CODE (expr) == BIT_XOR_EXPR)
2561 {
27259707 2562 /* If both args were extended from a shortest type,
2563 use that type if that is safe. */
48e1416a 2564 expr_type = shorten_binary_op (expr_type,
2565 TREE_OPERAND (expr, 0),
2566 TREE_OPERAND (expr, 1),
2561cea2 2567 /* bitwise */1);
2568
2561cea2 2569 if (TREE_CODE (expr) == BIT_AND_EXPR)
2570 {
2571 tree op0 = TREE_OPERAND (expr, 0);
2572 tree op1 = TREE_OPERAND (expr, 1);
30de145b 2573 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2574 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2575
2576 /* If one of the operands is a non-negative constant
2577 that fits in the target type, then the type of the
2578 other operand does not matter. */
2561cea2 2579 if ((TREE_CODE (op0) == INTEGER_CST
2580 && int_fits_type_p (op0, c_common_signed_type (type))
2581 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2582 || (TREE_CODE (op1) == INTEGER_CST
27259707 2583 && int_fits_type_p (op1, c_common_signed_type (type))
48e1416a 2584 && int_fits_type_p (op1,
27259707 2585 c_common_unsigned_type (type))))
ca9d7d74 2586 return SAFE_CONVERSION;
30de145b 2587 /* If constant is unsigned and fits in the target
2588 type, then the result will also fit. */
2589 else if ((TREE_CODE (op0) == INTEGER_CST
48e1416a 2590 && unsigned0
30de145b 2591 && int_fits_type_p (op0, type))
2592 || (TREE_CODE (op1) == INTEGER_CST
2593 && unsigned1
2594 && int_fits_type_p (op1, type)))
ca9d7d74 2595 return SAFE_CONVERSION;
2561cea2 2596 }
2597 }
7dfa155b 2598 /* Warn for integer types converted to smaller integer types. */
48e1416a 2599 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
ca9d7d74 2600 give_warning = UNSAFE_OTHER;
7ee0d227 2601
2602 /* When they are the same width but different signedness,
2603 then the value may change. */
7dfa155b 2604 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2561cea2 2605 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
7ee0d227 2606 /* Even when converted to a bigger type, if the type is
2607 unsigned but expr is signed, then negative values
2608 will be changed. */
7dfa155b 2609 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2610 && produce_warns)
200dd99c 2611 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2612 "may change the sign of the result",
2613 type, expr_type);
7dfa155b 2614 }
d31d55f0 2615
2616 /* Warn for integer types converted to real types if and only if
7dfa155b 2617 all the range of values of the integer type cannot be
2618 represented by the real type. */
2561cea2 2619 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2620 && TREE_CODE (type) == REAL_TYPE)
2621 {
4c2cfa81 2622 /* Don't warn about char y = 0xff; float x = (int) y; */
2623 expr = get_unwidened (expr, 0);
2624 expr_type = TREE_TYPE (expr);
2625
e53013a8 2626 if (!int_safely_convertible_to_real_p (expr_type, type))
ca9d7d74 2627 give_warning = UNSAFE_OTHER;
7dfa155b 2628 }
d31d55f0 2629
2630 /* Warn for real types converted to smaller real types. */
2561cea2 2631 else if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2632 && TREE_CODE (type) == REAL_TYPE
2633 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
ca9d7d74 2634 give_warning = UNSAFE_REAL;
e53013a8 2635
2636 /* Check conversion between two complex types. */
2637 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2638 && TREE_CODE (type) == COMPLEX_TYPE)
2639 {
2640 /* Extract underlying types (i.e., type of real and imaginary
2641 parts) of expr_type and type. */
2642 tree from_type = TREE_TYPE (expr_type);
2643 tree to_type = TREE_TYPE (type);
2644
2645 /* Warn for real types converted to integer types. */
2646 if (TREE_CODE (from_type) == REAL_TYPE
2647 && TREE_CODE (to_type) == INTEGER_TYPE)
2648 give_warning = UNSAFE_REAL;
2649
2650 /* Warn for real types converted to smaller real types. */
2651 else if (TREE_CODE (from_type) == REAL_TYPE
2652 && TREE_CODE (to_type) == REAL_TYPE
2653 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2654 give_warning = UNSAFE_REAL;
2655
2656 /* Check conversion for complex integer types. Here implementation
2657 is simpler than for real-domain integers because it does not
2658 involve sophisticated cases, such as bitmasks, casts, etc. */
2659 else if (TREE_CODE (from_type) == INTEGER_TYPE
2660 && TREE_CODE (to_type) == INTEGER_TYPE)
2661 {
2662 /* Warn for integer types converted to smaller integer types. */
2663 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2664 give_warning = UNSAFE_OTHER;
2665
2666 /* Check for different signedness, see case for real-domain
2667 integers (above) for a more detailed comment. */
2668 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
2669 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
2670 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
2671 && produce_warns)
2672 warning_at (loc, OPT_Wsign_conversion,
2673 "conversion to %qT from %qT "
2674 "may change the sign of the result",
2675 type, expr_type);
2676 }
2677 else if (TREE_CODE (from_type) == INTEGER_TYPE
2678 && TREE_CODE (to_type) == REAL_TYPE
2679 && !int_safely_convertible_to_real_p (from_type, to_type))
2680 give_warning = UNSAFE_OTHER;
2681 }
2682
2683 /* Warn for complex types converted to real or integer types. */
2684 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2685 && TREE_CODE (type) != COMPLEX_TYPE)
2686 give_warning = UNSAFE_IMAGINARY;
7dfa155b 2687 }
2688
2689 return give_warning;
2690}
2691
2692/* Warns if the conversion of EXPR to TYPE may alter a value.
2693 This is a helper function for warnings_for_convert_and_check. */
2694
2695static void
22a75734 2696conversion_warning (location_t loc, tree type, tree expr)
7dfa155b 2697{
7dfa155b 2698 tree expr_type = TREE_TYPE (expr);
ca9d7d74 2699 enum conversion_safety conversion_kind;
d31d55f0 2700
ca9d7d74 2701 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
7dfa155b 2702 return;
d31d55f0 2703
ec704957 2704 /* This may happen, because for LHS op= RHS we preevaluate
2705 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2706 means we could no longer see the code of the EXPR. */
2707 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2708 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2709 if (TREE_CODE (expr) == SAVE_EXPR)
2710 expr = TREE_OPERAND (expr, 0);
2711
7dfa155b 2712 switch (TREE_CODE (expr))
2713 {
2714 case EQ_EXPR:
2715 case NE_EXPR:
2716 case LE_EXPR:
2717 case GE_EXPR:
2718 case LT_EXPR:
2719 case GT_EXPR:
2720 case TRUTH_ANDIF_EXPR:
2721 case TRUTH_ORIF_EXPR:
2722 case TRUTH_AND_EXPR:
2723 case TRUTH_OR_EXPR:
2724 case TRUTH_XOR_EXPR:
2725 case TRUTH_NOT_EXPR:
2726 /* Conversion from boolean to a signed:1 bit-field (which only
2727 can hold the values 0 and -1) doesn't lose information - but
2728 it does change the value. */
2729 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2730 warning_at (loc, OPT_Wconversion,
2731 "conversion to %qT from boolean expression", type);
2732 return;
2733
2734 case REAL_CST:
2735 case INTEGER_CST:
e53013a8 2736 case COMPLEX_CST:
22a75734 2737 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
ca9d7d74 2738 if (conversion_kind == UNSAFE_REAL)
2739 warning_at (loc, OPT_Wfloat_conversion,
2740 "conversion to %qT alters %qT constant value",
2741 type, expr_type);
2742 else if (conversion_kind)
7dfa155b 2743 warning_at (loc, OPT_Wconversion,
2744 "conversion to %qT alters %qT constant value",
2745 type, expr_type);
2746 return;
2747
2748 case COND_EXPR:
2749 {
0e4e775a 2750 /* In case of COND_EXPR, we do not care about the type of
2751 COND_EXPR, only about the conversion of each operand. */
2752 tree op1 = TREE_OPERAND (expr, 1);
2753 tree op2 = TREE_OPERAND (expr, 2);
2754
22a75734 2755 conversion_warning (loc, type, op1);
2756 conversion_warning (loc, type, op2);
0e4e775a 2757 return;
7dfa155b 2758 }
2759
2760 default: /* 'expr' is not a constant. */
22a75734 2761 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
ca9d7d74 2762 if (conversion_kind == UNSAFE_REAL)
2763 warning_at (loc, OPT_Wfloat_conversion,
2764 "conversion to %qT from %qT may alter its value",
2765 type, expr_type);
e53013a8 2766 else if (conversion_kind == UNSAFE_IMAGINARY)
2767 warning_at (loc, OPT_Wconversion,
2768 "conversion to %qT from %qT discards imaginary component",
2769 type, expr_type);
ca9d7d74 2770 else if (conversion_kind)
7dfa155b 2771 warning_at (loc, OPT_Wconversion,
200dd99c 2772 "conversion to %qT from %qT may alter its value",
2773 type, expr_type);
d31d55f0 2774 }
2775}
2776
59dd8856 2777/* Produce warnings after a conversion. RESULT is the result of
2778 converting EXPR to TYPE. This is a helper function for
2779 convert_and_check and cp_convert_and_check. */
2a1736ed 2780
59dd8856 2781void
22a75734 2782warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2783 tree result)
2a1736ed 2784{
22a75734 2785 loc = expansion_point_location_if_in_system_header (loc);
61f69bc9 2786
da1fb07b 2787 if (TREE_CODE (expr) == INTEGER_CST
2788 && (TREE_CODE (type) == INTEGER_TYPE
2789 || TREE_CODE (type) == ENUMERAL_TYPE)
2790 && !int_fits_type_p (expr, type))
2791 {
d31d55f0 2792 /* Do not diagnose overflow in a constant expression merely
2793 because a conversion overflowed. */
da1fb07b 2794 if (TREE_OVERFLOW (result))
eddad94a 2795 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2796
da1fb07b 2797 if (TYPE_UNSIGNED (type))
d31d55f0 2798 {
da1fb07b 2799 /* This detects cases like converting -129 or 256 to
2800 unsigned char. */
2801 if (!int_fits_type_p (expr, c_common_signed_type (type)))
61f69bc9 2802 warning_at (loc, OPT_Woverflow,
2803 "large integer implicitly truncated to unsigned type");
7ee0d227 2804 else
22a75734 2805 conversion_warning (loc, type, expr);
da1fb07b 2806 }
48e1416a 2807 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
22a75734 2808 warning_at (loc, OPT_Woverflow,
e0913805 2809 "overflow in implicit constant conversion");
2810 /* No warning for converting 0x80000000 to int. */
2811 else if (pedantic
2812 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2813 || TYPE_PRECISION (TREE_TYPE (expr))
2814 != TYPE_PRECISION (type)))
61f69bc9 2815 warning_at (loc, OPT_Woverflow,
2816 "overflow in implicit constant conversion");
e0913805 2817
7ee0d227 2818 else
22a75734 2819 conversion_warning (loc, type, expr);
2a1736ed 2820 }
9421ebb9 2821 else if ((TREE_CODE (result) == INTEGER_CST
2822 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
61f69bc9 2823 warning_at (loc, OPT_Woverflow,
2824 "overflow in implicit constant conversion");
7ee0d227 2825 else
22a75734 2826 conversion_warning (loc, type, expr);
59dd8856 2827}
2828
2829
2830/* Convert EXPR to TYPE, warning about conversion problems with constants.
2831 Invoke this function on every expression that is converted implicitly,
2832 i.e. because of language rules and not because of an explicit cast. */
2833
2834tree
22a75734 2835convert_and_check (location_t loc, tree type, tree expr)
59dd8856 2836{
2837 tree result;
c6418a4e 2838 tree expr_for_warning;
2839
2840 /* Convert from a value with possible excess precision rather than
2841 via the semantic type, but do not warn about values not fitting
2842 exactly in the semantic type. */
2843 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2844 {
2845 tree orig_type = TREE_TYPE (expr);
2846 expr = TREE_OPERAND (expr, 0);
2847 expr_for_warning = convert (orig_type, expr);
2848 if (orig_type == type)
2849 return expr_for_warning;
2850 }
2851 else
2852 expr_for_warning = expr;
59dd8856 2853
2854 if (TREE_TYPE (expr) == type)
2855 return expr;
48e1416a 2856
59dd8856 2857 result = convert (type, expr);
2858
48d94ede 2859 if (c_inhibit_evaluation_warnings == 0
2860 && !TREE_OVERFLOW_P (expr)
2861 && result != error_mark_node)
22a75734 2862 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
59dd8856 2863
da1fb07b 2864 return result;
b2806639 2865}
2866\f
4e91a871 2867/* A node in a list that describes references to variables (EXPR), which are
2868 either read accesses if WRITER is zero, or write accesses, in which case
2869 WRITER is the parent of EXPR. */
2870struct tlist
2871{
2872 struct tlist *next;
2873 tree expr, writer;
2874};
2875
2876/* Used to implement a cache the results of a call to verify_tree. We only
2877 use this for SAVE_EXPRs. */
2878struct tlist_cache
2879{
2880 struct tlist_cache *next;
2881 struct tlist *cache_before_sp;
2882 struct tlist *cache_after_sp;
2883 tree expr;
481c6ce6 2884};
2885
4e91a871 2886/* Obstack to use when allocating tlist structures, and corresponding
2887 firstobj. */
2888static struct obstack tlist_obstack;
2889static char *tlist_firstobj = 0;
2890
2891/* Keep track of the identifiers we've warned about, so we can avoid duplicate
2892 warnings. */
2893static struct tlist *warned_ids;
2894/* SAVE_EXPRs need special treatment. We process them only once and then
2895 cache the results. */
2896static struct tlist_cache *save_expr_cache;
2897
1cae46be 2898static void add_tlist (struct tlist **, struct tlist *, tree, int);
2899static void merge_tlist (struct tlist **, struct tlist *, int);
2900static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2901static int warning_candidate_p (tree);
79973b57 2902static bool candidate_equal_p (const_tree, const_tree);
1cae46be 2903static void warn_for_collisions (struct tlist *);
2904static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2905static struct tlist *new_tlist (struct tlist *, tree, tree);
481c6ce6 2906
4e91a871 2907/* Create a new struct tlist and fill in its fields. */
2908static struct tlist *
1cae46be 2909new_tlist (struct tlist *next, tree t, tree writer)
4e91a871 2910{
2911 struct tlist *l;
9318f22c 2912 l = XOBNEW (&tlist_obstack, struct tlist);
4e91a871 2913 l->next = next;
2914 l->expr = t;
2915 l->writer = writer;
2916 return l;
2917}
2918
2919/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2920 is nonnull, we ignore any node we find which has a writer equal to it. */
2921
2922static void
1cae46be 2923add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
4e91a871 2924{
2925 while (add)
2926 {
2927 struct tlist *next = add->next;
84166705 2928 if (!copy)
4e91a871 2929 add->next = *to;
79973b57 2930 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
4e91a871 2931 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2932 add = next;
2933 }
2934}
2935
2936/* Merge the nodes of ADD into TO. This merging process is done so that for
2937 each variable that already exists in TO, no new node is added; however if
2938 there is a write access recorded in ADD, and an occurrence on TO is only
2939 a read access, then the occurrence in TO will be modified to record the
2940 write. */
481c6ce6 2941
2942static void
1cae46be 2943merge_tlist (struct tlist **to, struct tlist *add, int copy)
4e91a871 2944{
2945 struct tlist **end = to;
2946
2947 while (*end)
2948 end = &(*end)->next;
2949
2950 while (add)
2951 {
2952 int found = 0;
2953 struct tlist *tmp2;
2954 struct tlist *next = add->next;
2955
2956 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
79973b57 2957 if (candidate_equal_p (tmp2->expr, add->expr))
4e91a871 2958 {
2959 found = 1;
84166705 2960 if (!tmp2->writer)
4e91a871 2961 tmp2->writer = add->writer;
2962 }
84166705 2963 if (!found)
4e91a871 2964 {
312243bb 2965 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
4e91a871 2966 end = &(*end)->next;
2967 *end = 0;
2968 }
2969 add = next;
2970 }
2971}
2972
2973/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2974 references in list LIST conflict with it, excluding reads if ONLY writers
2975 is nonzero. */
2976
2977static void
1cae46be 2978warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2979 int only_writes)
4e91a871 2980{
2981 struct tlist *tmp;
2982
2983 /* Avoid duplicate warnings. */
2984 for (tmp = warned_ids; tmp; tmp = tmp->next)
79973b57 2985 if (candidate_equal_p (tmp->expr, written))
4e91a871 2986 return;
2987
2988 while (list)
2989 {
79973b57 2990 if (candidate_equal_p (list->expr, written)
2991 && !candidate_equal_p (list->writer, writer)
2992 && (!only_writes || list->writer))
4e91a871 2993 {
2994 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3df42822 2995 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
6513b50d 2996 OPT_Wsequence_point, "operation on %qE may be undefined",
2997 list->expr);
4e91a871 2998 }
2999 list = list->next;
3000 }
3001}
3002
3003/* Given a list LIST of references to variables, find whether any of these
3004 can cause conflicts due to missing sequence points. */
3005
3006static void
1cae46be 3007warn_for_collisions (struct tlist *list)
4e91a871 3008{
3009 struct tlist *tmp;
1cae46be 3010
4e91a871 3011 for (tmp = list; tmp; tmp = tmp->next)
3012 {
3013 if (tmp->writer)
3014 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3015 }
3016}
3017
734c98be 3018/* Return nonzero if X is a tree that can be verified by the sequence point
4e91a871 3019 warnings. */
3020static int
1cae46be 3021warning_candidate_p (tree x)
481c6ce6 3022{
6ef8d12f 3023 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3024 return 0;
3025
027fc6ef 3026 if (TREE_CODE (x) == BLOCK)
3027 return 0;
3028
6ef8d12f 3029 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
79973b57 3030 (lvalue_p) crash on TRY/CATCH. */
6ef8d12f 3031 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3032 return 0;
3033
3034 if (!lvalue_p (x))
3035 return 0;
3036
3037 /* No point to track non-const calls, they will never satisfy
3038 operand_equal_p. */
3039 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3040 return 0;
3041
3042 if (TREE_CODE (x) == STRING_CST)
3043 return 0;
3044
3045 return 1;
79973b57 3046}
3047
3048/* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3049static bool
3050candidate_equal_p (const_tree x, const_tree y)
3051{
3052 return (x == y) || (x && y && operand_equal_p (x, y, 0));
4e91a871 3053}
481c6ce6 3054
4e91a871 3055/* Walk the tree X, and record accesses to variables. If X is written by the
3056 parent tree, WRITER is the parent.
3057 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3058 expression or its only operand forces a sequence point, then everything up
3059 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3060 in PNO_SP.
3061 Once we return, we will have emitted warnings if any subexpression before
3062 such a sequence point could be undefined. On a higher level, however, the
3063 sequence point may not be relevant, and we'll merge the two lists.
3064
3065 Example: (b++, a) + b;
3066 The call that processes the COMPOUND_EXPR will store the increment of B
3067 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3068 processes the PLUS_EXPR will need to merge the two lists so that
3069 eventually, all accesses end up on the same list (and we'll warn about the
3070 unordered subexpressions b++ and b.
3071
3072 A note on merging. If we modify the former example so that our expression
3073 becomes
3074 (b++, b) + a
3075 care must be taken not simply to add all three expressions into the final
3076 PNO_SP list. The function merge_tlist takes care of that by merging the
3077 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3078 way, so that no more than one access to B is recorded. */
481c6ce6 3079
4e91a871 3080static void
1cae46be 3081verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3082 tree writer)
4e91a871 3083{
3084 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3085 enum tree_code code;
ce45a448 3086 enum tree_code_class cl;
481c6ce6 3087
e5b75768 3088 /* X may be NULL if it is the operand of an empty statement expression
3089 ({ }). */
3090 if (x == NULL)
3091 return;
3092
4e91a871 3093 restart:
3094 code = TREE_CODE (x);
e916c70c 3095 cl = TREE_CODE_CLASS (code);
481c6ce6 3096
4e91a871 3097 if (warning_candidate_p (x))
79973b57 3098 *pno_sp = new_tlist (*pno_sp, x, writer);
4e91a871 3099
3100 switch (code)
3101 {
67b28e3e 3102 case CONSTRUCTOR:
8e71dad2 3103 case SIZEOF_EXPR:
67b28e3e 3104 return;
3105
4e91a871 3106 case COMPOUND_EXPR:
3107 case TRUTH_ANDIF_EXPR:
3108 case TRUTH_ORIF_EXPR:
176aa551 3109 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
4e91a871 3110 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3111 warn_for_collisions (tmp_nosp);
3112 merge_tlist (pbefore_sp, tmp_before, 0);
3113 merge_tlist (pbefore_sp, tmp_nosp, 0);
176aa551 3114 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_list2, NULL_TREE);
3115 warn_for_collisions (tmp_list2);
4e91a871 3116 merge_tlist (pbefore_sp, tmp_list3, 0);
176aa551 3117 merge_tlist (pno_sp, tmp_list2, 0);
4e91a871 3118 return;
3119
3120 case COND_EXPR:
3121 tmp_before = tmp_list2 = 0;
3122 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3123 warn_for_collisions (tmp_list2);
3124 merge_tlist (pbefore_sp, tmp_before, 0);
312243bb 3125 merge_tlist (pbefore_sp, tmp_list2, 0);
4e91a871 3126
3127 tmp_list3 = tmp_nosp = 0;
3128 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3129 warn_for_collisions (tmp_nosp);
3130 merge_tlist (pbefore_sp, tmp_list3, 0);
3131
3132 tmp_list3 = tmp_list2 = 0;
3133 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3134 warn_for_collisions (tmp_list2);
3135 merge_tlist (pbefore_sp, tmp_list3, 0);
3136 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3137 two first, to avoid warning for (a ? b++ : b++). */
3138 merge_tlist (&tmp_nosp, tmp_list2, 0);
3139 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3140 return;
3141
481c6ce6 3142 case PREDECREMENT_EXPR:
3143 case PREINCREMENT_EXPR:
3144 case POSTDECREMENT_EXPR:
3145 case POSTINCREMENT_EXPR:
4e91a871 3146 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3147 return;
3148
3149 case MODIFY_EXPR:
3150 tmp_before = tmp_nosp = tmp_list3 = 0;
3151 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3152 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3153 /* Expressions inside the LHS are not ordered wrt. the sequence points
3154 in the RHS. Example:
3155 *a = (a++, 2)
3156 Despite the fact that the modification of "a" is in the before_sp
3157 list (tmp_before), it conflicts with the use of "a" in the LHS.
3158 We can handle this by adding the contents of tmp_list3
3159 to those of tmp_before, and redoing the collision warnings for that
3160 list. */
3161 add_tlist (&tmp_before, tmp_list3, x, 1);
3162 warn_for_collisions (tmp_before);
3163 /* Exclude the LHS itself here; we first have to merge it into the
3164 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3165 didn't exclude the LHS, we'd get it twice, once as a read and once
3166 as a write. */
3167 add_tlist (pno_sp, tmp_list3, x, 0);
3168 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3169
3170 merge_tlist (pbefore_sp, tmp_before, 0);
3171 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3172 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3173 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3174 return;
481c6ce6 3175
3176 case CALL_EXPR:
4e91a871 3177 /* We need to warn about conflicts among arguments and conflicts between
3178 args and the function address. Side effects of the function address,
3179 however, are not ordered by the sequence point of the call. */
c2f47e15 3180 {
3181 call_expr_arg_iterator iter;
3182 tree arg;
48e1416a 3183 tmp_before = tmp_nosp = 0;
c2f47e15 3184 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3185 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3186 {
3187 tmp_list2 = tmp_list3 = 0;
3188 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3189 merge_tlist (&tmp_list3, tmp_list2, 0);
3190 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3191 }
3192 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3193 warn_for_collisions (tmp_before);
3194 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3195 return;
3196 }
481c6ce6 3197
3198 case TREE_LIST:
3199 /* Scan all the list, e.g. indices of multi dimensional array. */
3200 while (x)
3201 {
4e91a871 3202 tmp_before = tmp_nosp = 0;
3203 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3204 merge_tlist (&tmp_nosp, tmp_before, 0);
3205 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
481c6ce6 3206 x = TREE_CHAIN (x);
3207 }
4e91a871 3208 return;
481c6ce6 3209
4e91a871 3210 case SAVE_EXPR:
3211 {
3212 struct tlist_cache *t;
3213 for (t = save_expr_cache; t; t = t->next)
79973b57 3214 if (candidate_equal_p (t->expr, x))
4e91a871 3215 break;
481c6ce6 3216
84166705 3217 if (!t)
481c6ce6 3218 {
9318f22c 3219 t = XOBNEW (&tlist_obstack, struct tlist_cache);
4e91a871 3220 t->next = save_expr_cache;
3221 t->expr = x;
3222 save_expr_cache = t;
3223
3224 tmp_before = tmp_nosp = 0;
3225 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3226 warn_for_collisions (tmp_nosp);
3227
3228 tmp_list3 = 0;
312243bb 3229 merge_tlist (&tmp_list3, tmp_nosp, 0);
4e91a871 3230 t->cache_before_sp = tmp_before;
3231 t->cache_after_sp = tmp_list3;
481c6ce6 3232 }
4e91a871 3233 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3234 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3235 return;
3236 }
481c6ce6 3237
012916cb 3238 case ADDR_EXPR:
3239 x = TREE_OPERAND (x, 0);
3240 if (DECL_P (x))
3241 return;
3242 writer = 0;
3243 goto restart;
3244
ce45a448 3245 default:
3246 /* For other expressions, simply recurse on their operands.
a0c938f0 3247 Manual tail recursion for unary expressions.
ce45a448 3248 Other non-expressions need not be processed. */
3249 if (cl == tcc_unary)
3250 {
ce45a448 3251 x = TREE_OPERAND (x, 0);
3252 writer = 0;
3253 goto restart;
3254 }
3255 else if (IS_EXPR_CODE_CLASS (cl))
3256 {
3257 int lp;
c2f47e15 3258 int max = TREE_OPERAND_LENGTH (x);
ce45a448 3259 for (lp = 0; lp < max; lp++)
3260 {
3261 tmp_before = tmp_nosp = 0;
3262 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3263 merge_tlist (&tmp_nosp, tmp_before, 0);
3264 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3265 }
3266 }
3267 return;
481c6ce6 3268 }
481c6ce6 3269}
3270
974e2c0c 3271/* Try to warn for undefined behavior in EXPR due to missing sequence
481c6ce6 3272 points. */
3273
4b987fac 3274DEBUG_FUNCTION void
1cae46be 3275verify_sequence_points (tree expr)
481c6ce6 3276{
4e91a871 3277 struct tlist *before_sp = 0, *after_sp = 0;
481c6ce6 3278
4e91a871 3279 warned_ids = 0;
3280 save_expr_cache = 0;
3281 if (tlist_firstobj == 0)
481c6ce6 3282 {
4e91a871 3283 gcc_obstack_init (&tlist_obstack);
4fd61bc6 3284 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
481c6ce6 3285 }
3286
4e91a871 3287 verify_tree (expr, &before_sp, &after_sp, 0);
3288 warn_for_collisions (after_sp);
3289 obstack_free (&tlist_obstack, tlist_firstobj);
481c6ce6 3290}
b0fc3e72 3291\f
3292/* Validate the expression after `case' and apply default promotions. */
3293
2ca392fd 3294static tree
2d2f6a15 3295check_case_value (location_t loc, tree value)
b0fc3e72 3296{
3297 if (value == NULL_TREE)
3298 return value;
3299
b96dc121 3300 if (TREE_CODE (value) == INTEGER_CST)
3301 /* Promote char or short to int. */
3302 value = perform_integral_promotions (value);
3303 else if (value != error_mark_node)
b0fc3e72 3304 {
2d2f6a15 3305 error_at (loc, "case label does not reduce to an integer constant");
b0fc3e72 3306 value = error_mark_node;
3307 }
b0fc3e72 3308
6433f1c2 3309 constant_expression_warning (value);
3310
b0fc3e72 3311 return value;
3312}
3313\f
2ca392fd 3314/* See if the case values LOW and HIGH are in the range of the original
5c9dae64 3315 type (i.e. before the default conversion to int) of the switch testing
2ca392fd 3316 expression.
3317 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
91275768 3318 the type before promoting it. CASE_LOW_P is a pointer to the lower
2ca392fd 3319 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3320 if the case is not a case range.
3321 The caller has to make sure that we are not called with NULL for
be23b16f 3322 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
3323 was a case value that doesn't fit into the range of the ORIG_TYPE.
442e3cb9 3324 Returns true if the case label is in range of ORIG_TYPE (saturated or
2ca392fd 3325 untouched) or false if the label is out of range. */
3326
3327static bool
f61a9bc2 3328check_case_bounds (location_t loc, tree type, tree orig_type,
be23b16f 3329 tree *case_low_p, tree *case_high_p,
3330 bool *outside_range_p)
2ca392fd 3331{
3332 tree min_value, max_value;
3333 tree case_low = *case_low_p;
3334 tree case_high = case_high_p ? *case_high_p : case_low;
3335
3336 /* If there was a problem with the original type, do nothing. */
3337 if (orig_type == error_mark_node)
3338 return true;
3339
3340 min_value = TYPE_MIN_VALUE (orig_type);
3341 max_value = TYPE_MAX_VALUE (orig_type);
3342
73f67931 3343 /* We'll really need integer constants here. */
3344 case_low = fold (case_low);
3345 case_high = fold (case_high);
3346
2ca392fd 3347 /* Case label is less than minimum for type. */
3348 if (tree_int_cst_compare (case_low, min_value) < 0
3349 && tree_int_cst_compare (case_high, min_value) < 0)
3350 {
f61a9bc2 3351 warning_at (loc, 0, "case label value is less than minimum value "
3352 "for type");
be23b16f 3353 *outside_range_p = true;
2ca392fd 3354 return false;
3355 }
b27ac6b5 3356
2ca392fd 3357 /* Case value is greater than maximum for type. */
3358 if (tree_int_cst_compare (case_low, max_value) > 0
3359 && tree_int_cst_compare (case_high, max_value) > 0)
3360 {
f61a9bc2 3361 warning_at (loc, 0, "case label value exceeds maximum value for type");
be23b16f 3362 *outside_range_p = true;
2ca392fd 3363 return false;
3364 }
3365
3366 /* Saturate lower case label value to minimum. */
3367 if (tree_int_cst_compare (case_high, min_value) >= 0
3368 && tree_int_cst_compare (case_low, min_value) < 0)
3369 {
f61a9bc2 3370 warning_at (loc, 0, "lower value in case label range"
3371 " less than minimum value for type");
be23b16f 3372 *outside_range_p = true;
2ca392fd 3373 case_low = min_value;
3374 }
b27ac6b5 3375
2ca392fd 3376 /* Saturate upper case label value to maximum. */
3377 if (tree_int_cst_compare (case_low, max_value) <= 0
3378 && tree_int_cst_compare (case_high, max_value) > 0)
3379 {
f61a9bc2 3380 warning_at (loc, 0, "upper value in case label range"
3381 " exceeds maximum value for type");
be23b16f 3382 *outside_range_p = true;
2ca392fd 3383 case_high = max_value;
3384 }
3385
3386 if (*case_low_p != case_low)
3387 *case_low_p = convert (type, case_low);
3388 if (case_high_p && *case_high_p != case_high)
3389 *case_high_p = convert (type, case_high);
3390
3391 return true;
3392}
3393\f
b0fc3e72 3394/* Return an integer type with BITS bits of precision,
3395 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3396
3397tree
1cae46be 3398c_common_type_for_size (unsigned int bits, int unsignedp)
b0fc3e72 3399{
9f75f026 3400 int i;
3401
46375237 3402 if (bits == TYPE_PRECISION (integer_type_node))
3403 return unsignedp ? unsigned_type_node : integer_type_node;
3404
bacde65a 3405 if (bits == TYPE_PRECISION (signed_char_type_node))
b0fc3e72 3406 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3407
bacde65a 3408 if (bits == TYPE_PRECISION (short_integer_type_node))
b0fc3e72 3409 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3410
bacde65a 3411 if (bits == TYPE_PRECISION (long_integer_type_node))
b0fc3e72 3412 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3413
bacde65a 3414 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b0fc3e72 3415 return (unsignedp ? long_long_unsigned_type_node
3416 : long_long_integer_type_node);
3417
9f75f026 3418 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3419 if (int_n_enabled_p[i]
3420 && bits == int_n_data[i].bitsize)
3421 return (unsignedp ? int_n_trees[i].unsigned_type
3422 : int_n_trees[i].signed_type);
6388cfe2 3423
f57fa2ea 3424 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3425 return (unsignedp ? widest_unsigned_literal_type_node
3426 : widest_integer_literal_type_node);
3427
bacde65a 3428 if (bits <= TYPE_PRECISION (intQI_type_node))
3429 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3430
3431 if (bits <= TYPE_PRECISION (intHI_type_node))
3432 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3433
3434 if (bits <= TYPE_PRECISION (intSI_type_node))
3435 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3436
3437 if (bits <= TYPE_PRECISION (intDI_type_node))
3438 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3439
b0fc3e72 3440 return 0;
3441}
3442
9421ebb9 3443/* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3444 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3445 and saturating if SATP is nonzero, otherwise not saturating. */
3446
3447tree
3448c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3449 int unsignedp, int satp)
3450{
3754d046 3451 machine_mode mode;
9421ebb9 3452 if (ibit == 0)
3453 mode = unsignedp ? UQQmode : QQmode;
3454 else
3455 mode = unsignedp ? UHAmode : HAmode;
3456
3457 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3458 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3459 break;
3460
3461 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3462 {
3463 sorry ("GCC cannot support operators with integer types and "
3464 "fixed-point types that have too many integral and "
3465 "fractional bits together");
3466 return 0;
3467 }
3468
3469 return c_common_type_for_mode (mode, satp);
3470}
3471
5b247e9f 3472/* Used for communication between c_common_type_for_mode and
3473 c_register_builtin_type. */
c1917557 3474tree registered_builtin_types;
5b247e9f 3475
b0fc3e72 3476/* Return a data type that has machine mode MODE.
3477 If the mode is an integer,
9421ebb9 3478 then UNSIGNEDP selects between signed and unsigned types.
3479 If the mode is a fixed-point mode,
3480 then UNSIGNEDP selects between saturating and nonsaturating types. */
b0fc3e72 3481
3482tree
3754d046 3483c_common_type_for_mode (machine_mode mode, int unsignedp)
b0fc3e72 3484{
5b247e9f 3485 tree t;
9f75f026 3486 int i;
5b247e9f 3487
46375237 3488 if (mode == TYPE_MODE (integer_type_node))
3489 return unsignedp ? unsigned_type_node : integer_type_node;
3490
b0fc3e72 3491 if (mode == TYPE_MODE (signed_char_type_node))
3492 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3493
3494 if (mode == TYPE_MODE (short_integer_type_node))
3495 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3496
b0fc3e72 3497 if (mode == TYPE_MODE (long_integer_type_node))
3498 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3499
3500 if (mode == TYPE_MODE (long_long_integer_type_node))
3501 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3502
9f75f026 3503 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3504 if (int_n_enabled_p[i]
3505 && mode == int_n_data[i].m)
3506 return (unsignedp ? int_n_trees[i].unsigned_type
3507 : int_n_trees[i].signed_type);
6388cfe2 3508
f57fa2ea 3509 if (mode == TYPE_MODE (widest_integer_literal_type_node))
44e9fa65 3510 return unsignedp ? widest_unsigned_literal_type_node
4ee9c684 3511 : widest_integer_literal_type_node;
f57fa2ea 3512
88ae7f04 3513 if (mode == QImode)
bacde65a 3514 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3515
88ae7f04 3516 if (mode == HImode)
bacde65a 3517 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3518
88ae7f04 3519 if (mode == SImode)
bacde65a 3520 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3521
88ae7f04 3522 if (mode == DImode)
bacde65a 3523 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
ab2c1de8 3524
cc1cc1c7 3525#if HOST_BITS_PER_WIDE_INT >= 64
6274009c 3526 if (mode == TYPE_MODE (intTI_type_node))
3527 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
cc1cc1c7 3528#endif
6274009c 3529
b0fc3e72 3530 if (mode == TYPE_MODE (float_type_node))
3531 return float_type_node;
3532
3533 if (mode == TYPE_MODE (double_type_node))
3534 return double_type_node;
3535
3536 if (mode == TYPE_MODE (long_double_type_node))
3537 return long_double_type_node;
3538
82c85aba 3539 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3540 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE
3541 && mode == TYPE_MODE (FLOATN_NX_TYPE_NODE (i)))
3542 return FLOATN_NX_TYPE_NODE (i);
3543
545c2bde 3544 if (mode == TYPE_MODE (void_type_node))
3545 return void_type_node;
b27ac6b5 3546
b0fc3e72 3547 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
61b9b73c 3548 return (unsignedp
3549 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3550 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3551
3552 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
61b9b73c 3553 return (unsignedp
3554 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3555 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3556
0dfc45b5 3557 if (COMPLEX_MODE_P (mode))
3558 {
3754d046 3559 machine_mode inner_mode;
0dfc45b5 3560 tree inner_type;
3561
3562 if (mode == TYPE_MODE (complex_float_type_node))
3563 return complex_float_type_node;
3564 if (mode == TYPE_MODE (complex_double_type_node))
3565 return complex_double_type_node;
3566 if (mode == TYPE_MODE (complex_long_double_type_node))
3567 return complex_long_double_type_node;
3568
82c85aba 3569 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3570 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
3571 && mode == TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i)))
3572 return COMPLEX_FLOATN_NX_TYPE_NODE (i);
3573
0dfc45b5 3574 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3575 return complex_integer_type_node;
3576
3577 inner_mode = GET_MODE_INNER (mode);
3578 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3579 if (inner_type != NULL_TREE)
3580 return build_complex_type (inner_type);
3581 }
3582 else if (VECTOR_MODE_P (mode))
4917c376 3583 {
3754d046 3584 machine_mode inner_mode = GET_MODE_INNER (mode);
4917c376 3585 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3586 if (inner_type != NULL_TREE)
3587 return build_vector_type_for_mode (inner_type, mode);
88ae7f04 3588 }
e2ea7e3a 3589
c4503c0a 3590 if (mode == TYPE_MODE (dfloat32_type_node))
3591 return dfloat32_type_node;
3592 if (mode == TYPE_MODE (dfloat64_type_node))
3593 return dfloat64_type_node;
3594 if (mode == TYPE_MODE (dfloat128_type_node))
3595 return dfloat128_type_node;
3596
9421ebb9 3597 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3598 {
3599 if (mode == TYPE_MODE (short_fract_type_node))
3600 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3601 if (mode == TYPE_MODE (fract_type_node))
3602 return unsignedp ? sat_fract_type_node : fract_type_node;
3603 if (mode == TYPE_MODE (long_fract_type_node))
3604 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3605 if (mode == TYPE_MODE (long_long_fract_type_node))
3606 return unsignedp ? sat_long_long_fract_type_node
3607 : long_long_fract_type_node;
3608
3609 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3610 return unsignedp ? sat_unsigned_short_fract_type_node
3611 : unsigned_short_fract_type_node;
3612 if (mode == TYPE_MODE (unsigned_fract_type_node))
3613 return unsignedp ? sat_unsigned_fract_type_node
3614 : unsigned_fract_type_node;
3615 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3616 return unsignedp ? sat_unsigned_long_fract_type_node
3617 : unsigned_long_fract_type_node;
3618 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3619 return unsignedp ? sat_unsigned_long_long_fract_type_node
3620 : unsigned_long_long_fract_type_node;
3621
3622 if (mode == TYPE_MODE (short_accum_type_node))
3623 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3624 if (mode == TYPE_MODE (accum_type_node))
3625 return unsignedp ? sat_accum_type_node : accum_type_node;
3626 if (mode == TYPE_MODE (long_accum_type_node))
3627 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3628 if (mode == TYPE_MODE (long_long_accum_type_node))
3629 return unsignedp ? sat_long_long_accum_type_node
3630 : long_long_accum_type_node;
3631
3632 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3633 return unsignedp ? sat_unsigned_short_accum_type_node
3634 : unsigned_short_accum_type_node;
3635 if (mode == TYPE_MODE (unsigned_accum_type_node))
3636 return unsignedp ? sat_unsigned_accum_type_node
3637 : unsigned_accum_type_node;
3638 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3639 return unsignedp ? sat_unsigned_long_accum_type_node
3640 : unsigned_long_accum_type_node;
3641 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3642 return unsignedp ? sat_unsigned_long_long_accum_type_node
3643 : unsigned_long_long_accum_type_node;
3644
3645 if (mode == QQmode)
3646 return unsignedp ? sat_qq_type_node : qq_type_node;
3647 if (mode == HQmode)
3648 return unsignedp ? sat_hq_type_node : hq_type_node;
3649 if (mode == SQmode)
3650 return unsignedp ? sat_sq_type_node : sq_type_node;
3651 if (mode == DQmode)
3652 return unsignedp ? sat_dq_type_node : dq_type_node;
3653 if (mode == TQmode)
3654 return unsignedp ? sat_tq_type_node : tq_type_node;
3655
3656 if (mode == UQQmode)
3657 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3658 if (mode == UHQmode)
3659 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3660 if (mode == USQmode)
3661 return unsignedp ? sat_usq_type_node : usq_type_node;
3662 if (mode == UDQmode)
3663 return unsignedp ? sat_udq_type_node : udq_type_node;
3664 if (mode == UTQmode)
3665 return unsignedp ? sat_utq_type_node : utq_type_node;
3666
3667 if (mode == HAmode)
3668 return unsignedp ? sat_ha_type_node : ha_type_node;
3669 if (mode == SAmode)
3670 return unsignedp ? sat_sa_type_node : sa_type_node;
3671 if (mode == DAmode)
3672 return unsignedp ? sat_da_type_node : da_type_node;
3673 if (mode == TAmode)
3674 return unsignedp ? sat_ta_type_node : ta_type_node;
3675
3676 if (mode == UHAmode)
3677 return unsignedp ? sat_uha_type_node : uha_type_node;
3678 if (mode == USAmode)
3679 return unsignedp ? sat_usa_type_node : usa_type_node;
3680 if (mode == UDAmode)
3681 return unsignedp ? sat_uda_type_node : uda_type_node;
3682 if (mode == UTAmode)
3683 return unsignedp ? sat_uta_type_node : uta_type_node;
3684 }
3685
5b247e9f 3686 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
baec58e1 3687 if (TYPE_MODE (TREE_VALUE (t)) == mode
3688 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
5b247e9f 3689 return TREE_VALUE (t);
3690
b0fc3e72 3691 return 0;
3692}
20d39783 3693
11773141 3694tree
3695c_common_unsigned_type (tree type)
3696{
3697 return c_common_signed_or_unsigned_type (1, type);
3698}
3699
20d39783 3700/* Return a signed type the same as TYPE in other respects. */
3701
3702tree
1cae46be 3703c_common_signed_type (tree type)
20d39783 3704{
4070745f 3705 return c_common_signed_or_unsigned_type (0, type);
20d39783 3706}
3707
3708/* Return a type the same as TYPE except unsigned or
3709 signed according to UNSIGNEDP. */
3710
3711tree
1cae46be 3712c_common_signed_or_unsigned_type (int unsignedp, tree type)
20d39783 3713{
7a91101f 3714 tree type1;
9f75f026 3715 int i;
20d39783 3716
7a91101f 3717 /* This block of code emulates the behavior of the old
3718 c_common_unsigned_type. In particular, it returns
3719 long_unsigned_type_node if passed a long, even when a int would
3720 have the same size. This is necessary for warnings to work
3721 correctly in archs where sizeof(int) == sizeof(long) */
3722
3723 type1 = TYPE_MAIN_VARIANT (type);
3724 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3725 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3726 if (type1 == integer_type_node || type1 == unsigned_type_node)
3727 return unsignedp ? unsigned_type_node : integer_type_node;
3728 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3729 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3730 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3731 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3732 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3733 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
9f75f026 3734
3735 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3736 if (int_n_enabled_p[i]
3737 && (type1 == int_n_trees[i].unsigned_type
3738 || type1 == int_n_trees[i].signed_type))
3739 return (unsignedp ? int_n_trees[i].unsigned_type
3740 : int_n_trees[i].signed_type);
3741
7a91101f 3742 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3743 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3744#if HOST_BITS_PER_WIDE_INT >= 64
3745 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3746 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3747#endif
3748 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3749 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3750 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3751 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3752 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3753 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3754 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3755 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3756
9f92e1a2 3757#define C_COMMON_FIXED_TYPES(NAME) \
3758 if (type1 == short_ ## NAME ## _type_node \
3759 || type1 == unsigned_short_ ## NAME ## _type_node) \
3760 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3761 : short_ ## NAME ## _type_node; \
3762 if (type1 == NAME ## _type_node \
3763 || type1 == unsigned_ ## NAME ## _type_node) \
3764 return unsignedp ? unsigned_ ## NAME ## _type_node \
3765 : NAME ## _type_node; \
3766 if (type1 == long_ ## NAME ## _type_node \
3767 || type1 == unsigned_long_ ## NAME ## _type_node) \
3768 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3769 : long_ ## NAME ## _type_node; \
3770 if (type1 == long_long_ ## NAME ## _type_node \
3771 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3772 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3773 : long_long_ ## NAME ## _type_node;
3774
3775#define C_COMMON_FIXED_MODE_TYPES(NAME) \
3776 if (type1 == NAME ## _type_node \
3777 || type1 == u ## NAME ## _type_node) \
3778 return unsignedp ? u ## NAME ## _type_node \
3779 : NAME ## _type_node;
3780
3781#define C_COMMON_FIXED_TYPES_SAT(NAME) \
3782 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3783 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3784 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3785 : sat_ ## short_ ## NAME ## _type_node; \
3786 if (type1 == sat_ ## NAME ## _type_node \
3787 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3788 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3789 : sat_ ## NAME ## _type_node; \
3790 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3791 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3792 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3793 : sat_ ## long_ ## NAME ## _type_node; \
3794 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3795 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3796 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3797 : sat_ ## long_long_ ## NAME ## _type_node;
3798
3799#define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3800 if (type1 == sat_ ## NAME ## _type_node \
3801 || type1 == sat_ ## u ## NAME ## _type_node) \
3802 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3803 : sat_ ## NAME ## _type_node;
3804
3805 C_COMMON_FIXED_TYPES (fract);
3806 C_COMMON_FIXED_TYPES_SAT (fract);
3807 C_COMMON_FIXED_TYPES (accum);
3808 C_COMMON_FIXED_TYPES_SAT (accum);
3809
3810 C_COMMON_FIXED_MODE_TYPES (qq);
3811 C_COMMON_FIXED_MODE_TYPES (hq);
3812 C_COMMON_FIXED_MODE_TYPES (sq);
3813 C_COMMON_FIXED_MODE_TYPES (dq);
3814 C_COMMON_FIXED_MODE_TYPES (tq);
3815 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3816 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3817 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3818 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3819 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3820 C_COMMON_FIXED_MODE_TYPES (ha);
3821 C_COMMON_FIXED_MODE_TYPES (sa);
3822 C_COMMON_FIXED_MODE_TYPES (da);
3823 C_COMMON_FIXED_MODE_TYPES (ta);
3824 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3825 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3826 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3827 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
9421ebb9 3828
4f7f7efd 3829 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3830 the precision; they have precision set to match their range, but
3831 may use a wider mode to match an ABI. If we change modes, we may
3832 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3833 the precision as well, so as to yield correct results for
3834 bit-field types. C++ does not have these separate bit-field
3835 types, and producing a signed or unsigned variant of an
3836 ENUMERAL_TYPE may cause other problems as well. */
3837
ac265864 3838 if (!INTEGRAL_TYPE_P (type)
3839 || TYPE_UNSIGNED (type) == unsignedp)
3840 return type;
3841
4f7f7efd 3842#define TYPE_OK(node) \
3843 (TYPE_MODE (type) == TYPE_MODE (node) \
0c4abe5b 3844 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
4f7f7efd 3845 if (TYPE_OK (signed_char_type_node))
20d39783 3846 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4f7f7efd 3847 if (TYPE_OK (integer_type_node))
20d39783 3848 return unsignedp ? unsigned_type_node : integer_type_node;
4f7f7efd 3849 if (TYPE_OK (short_integer_type_node))
20d39783 3850 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4f7f7efd 3851 if (TYPE_OK (long_integer_type_node))
20d39783 3852 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4f7f7efd 3853 if (TYPE_OK (long_long_integer_type_node))
20d39783 3854 return (unsignedp ? long_long_unsigned_type_node
3855 : long_long_integer_type_node);
9f75f026 3856
3857 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3858 if (int_n_enabled_p[i]
3859 && TYPE_MODE (type) == int_n_data[i].m
3860 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3861 return (unsignedp ? int_n_trees[i].unsigned_type
3862 : int_n_trees[i].signed_type);
3863
4f7f7efd 3864 if (TYPE_OK (widest_integer_literal_type_node))
20d39783 3865 return (unsignedp ? widest_unsigned_literal_type_node
3866 : widest_integer_literal_type_node);
ef11801e 3867
3868#if HOST_BITS_PER_WIDE_INT >= 64
4f7f7efd 3869 if (TYPE_OK (intTI_type_node))
ef11801e 3870 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3871#endif
4f7f7efd 3872 if (TYPE_OK (intDI_type_node))
ef11801e 3873 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4f7f7efd 3874 if (TYPE_OK (intSI_type_node))
ef11801e 3875 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4f7f7efd 3876 if (TYPE_OK (intHI_type_node))
ef11801e 3877 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4f7f7efd 3878 if (TYPE_OK (intQI_type_node))
ef11801e 3879 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4f7f7efd 3880#undef TYPE_OK
ef11801e 3881
0c4abe5b 3882 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
20d39783 3883}
b268e47e 3884
c0e47fd4 3885/* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3886
3887tree
3888c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3889{
9f75f026 3890 int i;
3891
c0e47fd4 3892 /* Extended integer types of the same width as a standard type have
3893 lesser rank, so those of the same width as int promote to int or
3894 unsigned int and are valid for printf formats expecting int or
3895 unsigned int. To avoid such special cases, avoid creating
3896 extended integer types for bit-fields if a standard integer type
3897 is available. */
3898 if (width == TYPE_PRECISION (integer_type_node))
3899 return unsignedp ? unsigned_type_node : integer_type_node;
3900 if (width == TYPE_PRECISION (signed_char_type_node))
3901 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3902 if (width == TYPE_PRECISION (short_integer_type_node))
3903 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3904 if (width == TYPE_PRECISION (long_integer_type_node))
3905 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3906 if (width == TYPE_PRECISION (long_long_integer_type_node))
3907 return (unsignedp ? long_long_unsigned_type_node
3908 : long_long_integer_type_node);
9f75f026 3909 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3910 if (int_n_enabled_p[i]
3911 && width == int_n_data[i].bitsize)
3912 return (unsignedp ? int_n_trees[i].unsigned_type
3913 : int_n_trees[i].signed_type);
c0e47fd4 3914 return build_nonstandard_integer_type (width, unsignedp);
3915}
3916
b268e47e 3917/* The C version of the register_builtin_type langhook. */
3918
3919void
3920c_register_builtin_type (tree type, const char* name)
3921{
3922 tree decl;
3923
e60a6f7b 3924 decl = build_decl (UNKNOWN_LOCATION,
3925 TYPE_DECL, get_identifier (name), type);
b268e47e 3926 DECL_ARTIFICIAL (decl) = 1;
3927 if (!TYPE_NAME (type))
3928 TYPE_NAME (type) = decl;
3929 pushdecl (decl);
5b247e9f 3930
3931 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
b268e47e 3932}
a9b9d10c 3933\f
aff9e656 3934/* Print an error message for invalid operands to arith operation
8e70fb09 3935 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
c0bf500c 3936 RICHLOC is a rich location for the message, containing either
3937 three separate locations for each of the operator and operands
3938
3939 lhs op rhs
3940 ~~~ ^~ ~~~
3941
3942 (C FE), or one location ranging over all over them
3943
3944 lhs op rhs
3945 ~~~~^~~~~~
3946
3947 (C++ FE). */
b0fc3e72 3948
3949void
c0bf500c 3950binary_op_error (rich_location *richloc, enum tree_code code,
8e70fb09 3951 tree type0, tree type1)
b0fc3e72 3952{
19cb6b50 3953 const char *opname;
f03946e4 3954
b0fc3e72 3955 switch (code)
3956 {
b0fc3e72 3957 case PLUS_EXPR:
3958 opname = "+"; break;
3959 case MINUS_EXPR:
3960 opname = "-"; break;
3961 case MULT_EXPR:
3962 opname = "*"; break;
3963 case MAX_EXPR:
3964 opname = "max"; break;
3965 case MIN_EXPR:
3966 opname = "min"; break;
3967 case EQ_EXPR:
3968 opname = "=="; break;
3969 case NE_EXPR:
3970 opname = "!="; break;
3971 case LE_EXPR:
3972 opname = "<="; break;
3973 case GE_EXPR:
3974 opname = ">="; break;
3975 case LT_EXPR:
3976 opname = "<"; break;
3977 case GT_EXPR:
3978 opname = ">"; break;
3979 case LSHIFT_EXPR:
3980 opname = "<<"; break;
3981 case RSHIFT_EXPR:
3982 opname = ">>"; break;
3983 case TRUNC_MOD_EXPR:
66618a1e 3984 case FLOOR_MOD_EXPR:
b0fc3e72 3985 opname = "%"; break;
3986 case TRUNC_DIV_EXPR:
66618a1e 3987 case FLOOR_DIV_EXPR:
b0fc3e72 3988 opname = "/"; break;
3989 case BIT_AND_EXPR:
3990 opname = "&"; break;
3991 case BIT_IOR_EXPR:
3992 opname = "|"; break;
3993 case TRUTH_ANDIF_EXPR:
3994 opname = "&&"; break;
3995 case TRUTH_ORIF_EXPR:
3996 opname = "||"; break;
3997 case BIT_XOR_EXPR:
3998 opname = "^"; break;
31f820d2 3999 default:
315ba355 4000 gcc_unreachable ();
b0fc3e72 4001 }
c0bf500c 4002 error_at_rich_loc (richloc,
4003 "invalid operands to binary %s (have %qT and %qT)",
4004 opname, type0, type1);
b0fc3e72 4005}
4006\f
03fe1dc2 4007/* Given an expression as a tree, return its original type. Do this
4008 by stripping any conversion that preserves the sign and precision. */
4009static tree
4010expr_original_type (tree expr)
4011{
4012 STRIP_SIGN_NOPS (expr);
4013 return TREE_TYPE (expr);
4014}
4015
b0fc3e72 4016/* Subroutine of build_binary_op, used for comparison operations.
4017 See if the operands have both been converted from subword integer types
4018 and, if so, perhaps change them both back to their original type.
5b511807 4019 This function is also responsible for converting the two operands
4020 to the proper common type for comparison.
b0fc3e72 4021
4022 The arguments of this function are all pointers to local variables
4023 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4024 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4025
2623625f 4026 LOC is the location of the comparison.
4027
b0fc3e72 4028 If this function returns nonzero, it means that the comparison has
4029 a constant value. What this function returns is an expression for
4030 that value. */
4031
4032tree
2623625f 4033shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4034 tree *restype_ptr, enum tree_code *rescode_ptr)
b0fc3e72 4035{
19cb6b50 4036 tree type;
b0fc3e72 4037 tree op0 = *op0_ptr;
4038 tree op1 = *op1_ptr;
4039 int unsignedp0, unsignedp1;
4040 int real1, real2;
4041 tree primop0, primop1;
4042 enum tree_code code = *rescode_ptr;
4043
4044 /* Throw away any conversions to wider types
4045 already present in the operands. */
4046
7f506bca 4047 primop0 = c_common_get_narrower (op0, &unsignedp0);
4048 primop1 = c_common_get_narrower (op1, &unsignedp1);
b0fc3e72 4049
119d06b2 4050 /* If primopN is first sign-extended from primopN's precision to opN's
4051 precision, then zero-extended from opN's precision to
4052 *restype_ptr precision, shortenings might be invalid. */
4053 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4054 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4055 && !unsignedp0
4056 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4057 primop0 = op0;
4058 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4059 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4060 && !unsignedp1
4061 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4062 primop1 = op1;
4063
b0fc3e72 4064 /* Handle the case that OP0 does not *contain* a conversion
4065 but it *requires* conversion to FINAL_TYPE. */
4066
4067 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
78a8ed03 4068 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
b0fc3e72 4069 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
78a8ed03 4070 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b0fc3e72 4071
4072 /* If one of the operands must be floated, we cannot optimize. */
4073 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4074 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
ab2c1de8 4075
b0fc3e72 4076 /* If first arg is constant, swap the args (changing operation
2bd278cc 4077 so value is preserved), for canonicalization. Don't do this if
4078 the second arg is 0. */
b0fc3e72 4079
2bd278cc 4080 if (TREE_CONSTANT (primop0)
9421ebb9 4081 && !integer_zerop (primop1) && !real_zerop (primop1)
4082 && !fixed_zerop (primop1))
b0fc3e72 4083 {
dfcf26a5 4084 std::swap (primop0, primop1);
4085 std::swap (op0, op1);
b0fc3e72 4086 *op0_ptr = op0;
4087 *op1_ptr = op1;
dfcf26a5 4088 std::swap (unsignedp0, unsignedp1);
4089 std::swap (real1, real2);
b0fc3e72 4090
4091 switch (code)
4092 {
4093 case LT_EXPR:
4094 code = GT_EXPR;
4095 break;
4096 case GT_EXPR:
4097 code = LT_EXPR;
4098 break;
4099 case LE_EXPR:
4100 code = GE_EXPR;
4101 break;
4102 case GE_EXPR:
4103 code = LE_EXPR;
4104 break;
31f820d2 4105 default:
4106 break;
b0fc3e72 4107 }
4108 *rescode_ptr = code;
4109 }
4110
4111 /* If comparing an integer against a constant more bits wide,
4112 maybe we can deduce a value of 1 or 0 independent of the data.
4113 Or else truncate the constant now
4114 rather than extend the variable at run time.
4115
4116 This is only interesting if the constant is the wider arg.
4117 Also, it is not safe if the constant is unsigned and the
4118 variable arg is signed, since in this case the variable
4119 would be sign-extended and then regarded as unsigned.
4120 Our technique fails in this case because the lowest/highest
4121 possible unsigned results don't follow naturally from the
4122 lowest/highest possible values of the variable operand.
4123 For just EQ_EXPR and NE_EXPR there is another technique that
4124 could be used: see if the constant can be faithfully represented
4125 in the other operand's type, by truncating it and reextending it
4126 and see if that preserves the constant's value. */
4127
4128 if (!real1 && !real2
9421ebb9 4129 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
b0fc3e72 4130 && TREE_CODE (primop1) == INTEGER_CST
4131 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4132 {
4133 int min_gt, max_gt, min_lt, max_lt;
4134 tree maxval, minval;
4135 /* 1 if comparison is nominally unsigned. */
78a8ed03 4136 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
b0fc3e72 4137 tree val;
4138
4070745f 4139 type = c_common_signed_or_unsigned_type (unsignedp0,
4140 TREE_TYPE (primop0));
cda09c61 4141
b0fc3e72 4142 maxval = TYPE_MAX_VALUE (type);
4143 minval = TYPE_MIN_VALUE (type);
4144
4145 if (unsignedp && !unsignedp0)
4070745f 4146 *restype_ptr = c_common_signed_type (*restype_ptr);
b0fc3e72 4147
4148 if (TREE_TYPE (primop1) != *restype_ptr)
18dbec6f 4149 {
9a5e8086 4150 /* Convert primop1 to target type, but do not introduce
4151 additional overflow. We know primop1 is an int_cst. */
e913b5cd 4152 primop1 = force_fit_type (*restype_ptr,
ee31dd3d 4153 wi::to_wide
4154 (primop1,
4155 TYPE_PRECISION (*restype_ptr)),
e913b5cd 4156 0, TREE_OVERFLOW (primop1));
18dbec6f 4157 }
b0fc3e72 4158 if (type != *restype_ptr)
4159 {
4160 minval = convert (*restype_ptr, minval);
4161 maxval = convert (*restype_ptr, maxval);
4162 }
4163
d99d10ca 4164 min_gt = tree_int_cst_lt (primop1, minval);
4165 max_gt = tree_int_cst_lt (primop1, maxval);
4166 min_lt = tree_int_cst_lt (minval, primop1);
4167 max_lt = tree_int_cst_lt (maxval, primop1);
b0fc3e72 4168
4169 val = 0;
4170 /* This used to be a switch, but Genix compiler can't handle that. */
4171 if (code == NE_EXPR)
4172 {
4173 if (max_lt || min_gt)
3c2239cf 4174 val = truthvalue_true_node;
b0fc3e72 4175 }
4176 else if (code == EQ_EXPR)
4177 {
4178 if (max_lt || min_gt)
3c2239cf 4179 val = truthvalue_false_node;
b0fc3e72 4180 }
4181 else if (code == LT_EXPR)
4182 {
4183 if (max_lt)
3c2239cf 4184 val = truthvalue_true_node;
b0fc3e72 4185 if (!min_lt)
3c2239cf 4186 val = truthvalue_false_node;
b0fc3e72 4187 }
4188 else if (code == GT_EXPR)
4189 {
4190 if (min_gt)
3c2239cf 4191 val = truthvalue_true_node;
b0fc3e72 4192 if (!max_gt)
3c2239cf 4193 val = truthvalue_false_node;
b0fc3e72 4194 }
4195 else if (code == LE_EXPR)
4196 {
4197 if (!max_gt)
3c2239cf 4198 val = truthvalue_true_node;
b0fc3e72 4199 if (min_gt)
3c2239cf 4200 val = truthvalue_false_node;
b0fc3e72 4201 }
4202 else if (code == GE_EXPR)
4203 {
4204 if (!min_lt)
3c2239cf 4205 val = truthvalue_true_node;
b0fc3e72 4206 if (max_lt)
3c2239cf 4207 val = truthvalue_false_node;
b0fc3e72 4208 }
4209
4210 /* If primop0 was sign-extended and unsigned comparison specd,
4211 we did a signed comparison above using the signed type bounds.
4212 But the comparison we output must be unsigned.
4213
4214 Also, for inequalities, VAL is no good; but if the signed
4215 comparison had *any* fixed result, it follows that the
4216 unsigned comparison just tests the sign in reverse
4217 (positive values are LE, negative ones GE).
4218 So we can generate an unsigned comparison
4219 against an extreme value of the signed type. */
4220
4221 if (unsignedp && !unsignedp0)
4222 {
4223 if (val != 0)
4224 switch (code)
4225 {
4226 case LT_EXPR:
4227 case GE_EXPR:
4228 primop1 = TYPE_MIN_VALUE (type);
4229 val = 0;
4230 break;
4231
4232 case LE_EXPR:
4233 case GT_EXPR:
4234 primop1 = TYPE_MAX_VALUE (type);
4235 val = 0;
4236 break;
31f820d2 4237
4238 default:
4239 break;
b0fc3e72 4240 }
11773141 4241 type = c_common_unsigned_type (type);
b0fc3e72 4242 }
4243
2d3d3af7 4244 if (TREE_CODE (primop0) != INTEGER_CST
01f11119 4245 /* Don't warn if it's from a (non-system) macro. */
4246 && !(from_macro_expansion_at
4247 (expansion_point_location_if_in_system_header
4248 (EXPR_LOCATION (primop0)))))
b0fc3e72 4249 {
3c2239cf 4250 if (val == truthvalue_false_node)
03fe1dc2 4251 warning_at (loc, OPT_Wtype_limits,
4252 "comparison is always false due to limited range of data type");
3c2239cf 4253 if (val == truthvalue_true_node)
03fe1dc2 4254 warning_at (loc, OPT_Wtype_limits,
4255 "comparison is always true due to limited range of data type");
b0fc3e72 4256 }
4257
4258 if (val != 0)
4259 {
4260 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4261 if (TREE_SIDE_EFFECTS (primop0))
14ae0310 4262 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
b0fc3e72 4263 return val;
4264 }
4265
4266 /* Value is not predetermined, but do the comparison
4267 in the type of the operand that is not constant.
4268 TYPE is already properly set. */
4269 }
c4503c0a 4270
4271 /* If either arg is decimal float and the other is float, find the
4272 proper common type to use for comparison. */
7fd22aae 4273 else if (real1 && real2
4274 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4275 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4276 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4277
4278 /* If either arg is decimal float and the other is float, fail. */
c4503c0a 4279 else if (real1 && real2
4280 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4281 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
7fd22aae 4282 return 0;
c4503c0a 4283
b0fc3e72 4284 else if (real1 && real2
2203bd5c 4285 && (TYPE_PRECISION (TREE_TYPE (primop0))
4286 == TYPE_PRECISION (TREE_TYPE (primop1))))
b0fc3e72 4287 type = TREE_TYPE (primop0);
4288
4289 /* If args' natural types are both narrower than nominal type
4290 and both extend in the same manner, compare them
4291 in the type of the wider arg.
4292 Otherwise must actually extend both to the nominal
4293 common type lest different ways of extending
4294 alter the result.
4295 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4296
4297 else if (unsignedp0 == unsignedp1 && real1 == real2
4298 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4299 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4300 {
4301 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4070745f 4302 type = c_common_signed_or_unsigned_type (unsignedp0
78a8ed03 4303 || TYPE_UNSIGNED (*restype_ptr),
4070745f 4304 type);
b0fc3e72 4305 /* Make sure shorter operand is extended the right way
4306 to match the longer operand. */
4070745f 4307 primop0
4308 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4309 TREE_TYPE (primop0)),
4310 primop0);
4311 primop1
4312 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4313 TREE_TYPE (primop1)),
4314 primop1);
b0fc3e72 4315 }
4316 else
4317 {
4318 /* Here we must do the comparison on the nominal type
4319 using the args exactly as we received them. */
4320 type = *restype_ptr;
4321 primop0 = op0;
4322 primop1 = op1;
4323
4324 if (!real1 && !real2 && integer_zerop (primop1)
78a8ed03 4325 && TYPE_UNSIGNED (*restype_ptr))
b0fc3e72 4326 {
4327 tree value = 0;
03fe1dc2 4328 /* All unsigned values are >= 0, so we warn. However,
4329 if OP0 is a constant that is >= 0, the signedness of
4330 the comparison isn't an issue, so suppress the
4331 warning. */
4332 bool warn =
3df42822 4333 warn_type_limits && !in_system_header_at (loc)
03fe1dc2 4334 && !(TREE_CODE (primop0) == INTEGER_CST
4335 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4336 primop0)))
4337 /* Do not warn for enumeration types. */
4338 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4339
b0fc3e72 4340 switch (code)
4341 {
4342 case GE_EXPR:
03fe1dc2 4343 if (warn)
4344 warning_at (loc, OPT_Wtype_limits,
4345 "comparison of unsigned expression >= 0 is always true");
3c2239cf 4346 value = truthvalue_true_node;
b0fc3e72 4347 break;
4348
4349 case LT_EXPR:
03fe1dc2 4350 if (warn)
4351 warning_at (loc, OPT_Wtype_limits,
4352 "comparison of unsigned expression < 0 is always false");
3c2239cf 4353 value = truthvalue_false_node;
31f820d2 4354 break;
4355
4356 default:
4357 break;
b0fc3e72 4358 }
4359
4360 if (value != 0)
4361 {
4362 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4363 if (TREE_SIDE_EFFECTS (primop0))
14ae0310 4364 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4365 primop0, value);
b0fc3e72 4366 return value;
4367 }
4368 }
4369 }
4370
4371 *op0_ptr = convert (type, primop0);
4372 *op1_ptr = convert (type, primop1);
4373
3c2239cf 4374 *restype_ptr = truthvalue_type_node;
b0fc3e72 4375
4376 return 0;
4377}
4378\f
1c26100f 4379/* Return a tree for the sum or difference (RESULTCODE says which)
4380 of pointer PTROP and integer INTOP. */
4381
4382tree
389dd41b 4383pointer_int_sum (location_t loc, enum tree_code resultcode,
42f9a786 4384 tree ptrop, tree intop, bool complain)
1c26100f 4385{
add6ee5e 4386 tree size_exp, ret;
1c26100f 4387
1c26100f 4388 /* The result is a pointer of the same type that is being added. */
1c26100f 4389 tree result_type = TREE_TYPE (ptrop);
4390
4391 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4392 {
42f9a786 4393 if (complain && warn_pointer_arith)
4394 pedwarn (loc, OPT_Wpointer_arith,
4395 "pointer of type %<void *%> used in arithmetic");
4396 else if (!complain)
4397 return error_mark_node;
1c26100f 4398 size_exp = integer_one_node;
4399 }
4400 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4401 {
42f9a786 4402 if (complain && warn_pointer_arith)
4403 pedwarn (loc, OPT_Wpointer_arith,
4404 "pointer to a function used in arithmetic");
4405 else if (!complain)
4406 return error_mark_node;
1c26100f 4407 size_exp = integer_one_node;
4408 }
1c26100f 4409 else
22a3f7bd 4410 size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type));
1c26100f 4411
add6ee5e 4412 /* We are manipulating pointer values, so we don't need to warn
4413 about relying on undefined signed overflow. We disable the
4414 warning here because we use integer types so fold won't know that
4415 they are really pointers. */
4416 fold_defer_overflow_warnings ();
4417
1c26100f 4418 /* If what we are about to multiply by the size of the elements
4419 contains a constant term, apply distributive law
4420 and multiply that constant term separately.
4421 This helps produce common subexpressions. */
1c26100f 4422 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
84166705 4423 && !TREE_CONSTANT (intop)
1c26100f 4424 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4425 && TREE_CONSTANT (size_exp)
4426 /* If the constant comes from pointer subtraction,
4427 skip this optimization--it would cause an error. */
4428 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4429 /* If the constant is unsigned, and smaller than the pointer size,
4430 then we must skip this optimization. This is because it could cause
4431 an overflow error if the constant is negative but INTOP is not. */
84166705 4432 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
1c26100f 4433 || (TYPE_PRECISION (TREE_TYPE (intop))
4434 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4435 {
4436 enum tree_code subcode = resultcode;
4437 tree int_type = TREE_TYPE (intop);
4438 if (TREE_CODE (intop) == MINUS_EXPR)
4439 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4440 /* Convert both subexpression types to the type of intop,
4441 because weird cases involving pointer arithmetic
4442 can result in a sum or difference with different type args. */
8e70fb09 4443 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4444 subcode, ptrop,
1c26100f 4445 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4446 intop = convert (int_type, TREE_OPERAND (intop, 0));
4447 }
4448
4449 /* Convert the integer argument to a type the same size as sizetype
4450 so the multiply won't overflow spuriously. */
1c26100f 4451 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
78a8ed03 4452 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
1cae46be 4453 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
78a8ed03 4454 TYPE_UNSIGNED (sizetype)), intop);
1c26100f 4455
4456 /* Replace the integer argument with a suitable product by the object size.
c6feb9f1 4457 Do this multiplication as signed, then convert to the appropriate type
8032877c 4458 for the pointer operation and disregard an overflow that occurred only
c6feb9f1 4459 because of the sign-extension change in the latter conversion. */
4460 {
f576a2e4 4461 tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
4462 convert (TREE_TYPE (intop), size_exp));
c6feb9f1 4463 intop = convert (sizetype, t);
4464 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
e913b5cd 4465 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
c6feb9f1 4466 }
0de36bdb 4467
499e523f 4468 /* Create the sum or difference. */
0de36bdb 4469 if (resultcode == MINUS_EXPR)
389dd41b 4470 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
1c26100f 4471
2cc66f2a 4472 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
add6ee5e 4473
4474 fold_undefer_and_ignore_overflow_warnings ();
4475
4476 return ret;
1c26100f 4477}
4478\f
f59e3889 4479/* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4480 and if NON_CONST is known not to be permitted in an evaluated part
4481 of a constant expression. */
4482
4483tree
4484c_wrap_maybe_const (tree expr, bool non_const)
4485{
4486 bool nowarning = TREE_NO_WARNING (expr);
4487 location_t loc = EXPR_LOCATION (expr);
4488
4489 /* This should never be called for C++. */
4490 if (c_dialect_cxx ())
4491 gcc_unreachable ();
4492
4493 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4494 STRIP_TYPE_NOPS (expr);
4495 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4496 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4497 if (nowarning)
4498 TREE_NO_WARNING (expr) = 1;
4499 protected_set_expr_location (expr, loc);
4500
4501 return expr;
4502}
4503
a75b1c71 4504/* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4505 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4506 around the SAVE_EXPR if needed so that c_fully_fold does not need
4507 to look inside SAVE_EXPRs. */
4508
4509tree
4510c_save_expr (tree expr)
4511{
4512 bool maybe_const = true;
4513 if (c_dialect_cxx ())
4514 return save_expr (expr);
4515 expr = c_fully_fold (expr, false, &maybe_const);
4516 expr = save_expr (expr);
4517 if (!maybe_const)
f59e3889 4518 expr = c_wrap_maybe_const (expr, true);
a75b1c71 4519 return expr;
4520}
4521
6b68e71a 4522/* Return whether EXPR is a declaration whose address can never be
4523 NULL. */
4524
4525bool
9f627b1a 4526decl_with_nonnull_addr_p (const_tree expr)
6b68e71a 4527{
4528 return (DECL_P (expr)
4529 && (TREE_CODE (expr) == PARM_DECL
4530 || TREE_CODE (expr) == LABEL_DECL
4531 || !DECL_WEAK (expr)));
4532}
4533
b0fc3e72 4534/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
45a78cc0 4535 or for an `if' or `while' statement or ?..: exp. It should already
4536 have been validated to be of suitable type; otherwise, a bad
4537 diagnostic may result.
b0fc3e72 4538
8e70fb09 4539 The EXPR is located at LOCATION.
4540
b0fc3e72 4541 This preparation consists of taking the ordinary
4542 representation of an expression expr and producing a valid tree
4543 boolean expression describing whether expr is nonzero. We could
3c2239cf 4544 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
b0fc3e72 4545 but we optimize comparisons, &&, ||, and !.
4546
3c2239cf 4547 The resulting type should always be `truthvalue_type_node'. */
b0fc3e72 4548
4549tree
8e70fb09 4550c_common_truthvalue_conversion (location_t location, tree expr)
b0fc3e72 4551{
b0fc3e72 4552 switch (TREE_CODE (expr))
4553 {
318a728f 4554 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
e1232ce2 4555 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4556 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4557 case ORDERED_EXPR: case UNORDERED_EXPR:
ce04dcdc 4558 if (TREE_TYPE (expr) == truthvalue_type_node)
4559 return expr;
e60a6f7b 4560 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
ce04dcdc 4561 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
e60a6f7b 4562 goto ret;
ce04dcdc 4563
b0fc3e72 4564 case TRUTH_ANDIF_EXPR:
4565 case TRUTH_ORIF_EXPR:
4566 case TRUTH_AND_EXPR:
4567 case TRUTH_OR_EXPR:
31f6e93c 4568 case TRUTH_XOR_EXPR:
ce04dcdc 4569 if (TREE_TYPE (expr) == truthvalue_type_node)
4570 return expr;
e60a6f7b 4571 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
48e1416a 4572 c_common_truthvalue_conversion (location,
e60a6f7b 4573 TREE_OPERAND (expr, 0)),
4574 c_common_truthvalue_conversion (location,
4575 TREE_OPERAND (expr, 1)));
4576 goto ret;
3e851b85 4577
f6e28f72 4578 case TRUTH_NOT_EXPR:
ce04dcdc 4579 if (TREE_TYPE (expr) == truthvalue_type_node)
4580 return expr;
e60a6f7b 4581 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4582 c_common_truthvalue_conversion (location,
4583 TREE_OPERAND (expr, 0)));
4584 goto ret;
f6e28f72 4585
b0fc3e72 4586 case ERROR_MARK:
4587 return expr;
ab2c1de8 4588
b0fc3e72 4589 case INTEGER_CST:
eddad94a 4590 return integer_zerop (expr) ? truthvalue_false_node
4591 : truthvalue_true_node;
b0fc3e72 4592
4593 case REAL_CST:
5000e21c 4594 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4595 ? truthvalue_true_node
4596 : truthvalue_false_node;
b0fc3e72 4597
9421ebb9 4598 case FIXED_CST:
4599 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4600 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4601 ? truthvalue_true_node
4602 : truthvalue_false_node;
4603
ce04dcdc 4604 case FUNCTION_DECL:
b6889cb0 4605 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
ce04dcdc 4606 /* Fall through. */
4607
b0fc3e72 4608 case ADDR_EXPR:
ce871053 4609 {
ee7d4d6a 4610 tree inner = TREE_OPERAND (expr, 0);
6b68e71a 4611 if (decl_with_nonnull_addr_p (inner))
ce871053 4612 {
6b68e71a 4613 /* Common Ada/Pascal programmer's mistake. */
8e70fb09 4614 warning_at (location,
4615 OPT_Waddress,
4616 "the address of %qD will always evaluate as %<true%>",
4617 inner);
ce871053 4618 return truthvalue_true_node;
4619 }
d473d901 4620 break;
ce871053 4621 }
b0fc3e72 4622
2203bd5c 4623 case COMPLEX_EXPR:
e60a6f7b 4624 expr = build_binary_op (EXPR_LOCATION (expr),
8e70fb09 4625 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
95809de4 4626 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
8e70fb09 4627 c_common_truthvalue_conversion (location,
4628 TREE_OPERAND (expr, 0)),
4629 c_common_truthvalue_conversion (location,
4630 TREE_OPERAND (expr, 1)),
2203bd5c 4631 0);
e60a6f7b 4632 goto ret;
2203bd5c 4633
b0fc3e72 4634 case NEGATE_EXPR:
4635 case ABS_EXPR:
4636 case FLOAT_EXPR:
c6418a4e 4637 case EXCESS_PRECISION_EXPR:
d10cfa8d 4638 /* These don't change whether an object is nonzero or zero. */
8e70fb09 4639 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
b0fc3e72 4640
4641 case LROTATE_EXPR:
4642 case RROTATE_EXPR:
d10cfa8d 4643 /* These don't change whether an object is zero or nonzero, but
b0fc3e72 4644 we can't ignore them if their second arg has side-effects. */
4645 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
e60a6f7b 4646 {
4647 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4648 TREE_OPERAND (expr, 1),
48e1416a 4649 c_common_truthvalue_conversion
e60a6f7b 4650 (location, TREE_OPERAND (expr, 0)));
4651 goto ret;
4652 }
b0fc3e72 4653 else
8e70fb09 4654 return c_common_truthvalue_conversion (location,
4655 TREE_OPERAND (expr, 0));
73be5127 4656
b0fc3e72 4657 case COND_EXPR:
4658 /* Distribute the conversion into the arms of a COND_EXPR. */
a75b1c71 4659 if (c_dialect_cxx ())
e60a6f7b 4660 {
d0389adc 4661 tree op1 = TREE_OPERAND (expr, 1);
4662 tree op2 = TREE_OPERAND (expr, 2);
4663 /* In C++ one of the arms might have void type if it is throw. */
4664 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4665 op1 = c_common_truthvalue_conversion (location, op1);
4666 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4667 op2 = c_common_truthvalue_conversion (location, op2);
389dd41b 4668 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
d0389adc 4669 TREE_OPERAND (expr, 0), op1, op2);
e60a6f7b 4670 goto ret;
4671 }
a75b1c71 4672 else
e60a6f7b 4673 {
4674 /* Folding will happen later for C. */
4675 expr = build3 (COND_EXPR, truthvalue_type_node,
4676 TREE_OPERAND (expr, 0),
4677 c_common_truthvalue_conversion (location,
4678 TREE_OPERAND (expr, 1)),
4679 c_common_truthvalue_conversion (location,
4680 TREE_OPERAND (expr, 2)));
4681 goto ret;
4682 }
b0fc3e72 4683
72dd6141 4684 CASE_CONVERT:
f8913d47 4685 {
4686 tree totype = TREE_TYPE (expr);
4687 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4688
db3d1ffc 4689 if (POINTER_TYPE_P (totype)
0c0f63cb 4690 && !c_inhibit_evaluation_warnings
db3d1ffc 4691 && TREE_CODE (fromtype) == REFERENCE_TYPE)
4692 {
4693 tree inner = expr;
4694 STRIP_NOPS (inner);
4695
4696 if (DECL_P (inner))
4697 warning_at (location,
4698 OPT_Waddress,
4699 "the compiler can assume that the address of "
4700 "%qD will always evaluate to %<true%>",
4701 inner);
4702 }
4703
f8913d47 4704 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4705 since that affects how `default_conversion' will behave. */
4706 if (TREE_CODE (totype) == REFERENCE_TYPE
4707 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4708 break;
4709 /* Don't strip a conversion from C++0x scoped enum, since they
4710 don't implicitly convert to other types. */
4711 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4712 && ENUM_IS_SCOPED (fromtype))
4713 break;
4714 /* If this isn't narrowing the argument, we can ignore it. */
4715 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4716 return c_common_truthvalue_conversion (location,
4717 TREE_OPERAND (expr, 0));
4718 }
b0fc3e72 4719 break;
4720
16837b18 4721 case MODIFY_EXPR:
60a0513e 4722 if (!TREE_NO_WARNING (expr)
4723 && warn_parentheses)
4724 {
8ae70602 4725 warning_at (location, OPT_Wparentheses,
4726 "suggest parentheses around assignment used as "
4727 "truth value");
60a0513e 4728 TREE_NO_WARNING (expr) = 1;
4729 }
16837b18 4730 break;
73be5127 4731
31f820d2 4732 default:
4733 break;
b0fc3e72 4734 }
4735
2ba726d2 4736 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
a0748b7d 4737 {
93be21c0 4738 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
e60a6f7b 4739 expr = (build_binary_op
8e70fb09 4740 (EXPR_LOCATION (expr),
4741 (TREE_SIDE_EFFECTS (expr)
a0748b7d 4742 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
b6889cb0 4743 c_common_truthvalue_conversion
4744 (location,
4745 build_unary_op (location, REALPART_EXPR, t, 0)),
4746 c_common_truthvalue_conversion
4747 (location,
4748 build_unary_op (location, IMAGPART_EXPR, t, 0)),
a0748b7d 4749 0));
e60a6f7b 4750 goto ret;
a0748b7d 4751 }
2ba726d2 4752
9421ebb9 4753 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4754 {
4755 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4756 FCONST0 (TYPE_MODE
4757 (TREE_TYPE (expr))));
43158006 4758 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
9421ebb9 4759 }
e60a6f7b 4760 else
4761 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
9421ebb9 4762
e60a6f7b 4763 ret:
4764 protected_set_expr_location (expr, location);
4765 return expr;
b0fc3e72 4766}
4767\f
3237155d 4768static void def_builtin_1 (enum built_in_function fncode,
4769 const char *name,
4770 enum built_in_class fnclass,
4771 tree fntype, tree libtype,
4772 bool both_p, bool fallback_p, bool nonansi_p,
4773 tree fnattrs, bool implicit_p);
0d4238dc 4774
a5b1863e 4775
4776/* Apply the TYPE_QUALS to the new DECL. */
4777
4778void
1cae46be 4779c_apply_type_quals_to_decl (int type_quals, tree decl)
a5b1863e 4780{
adfb367f 4781 tree type = TREE_TYPE (decl);
b27ac6b5 4782
e4eabbe4 4783 if (type == error_mark_node)
4784 return;
adfb367f 4785
98a33d9f 4786 if ((type_quals & TYPE_QUAL_CONST)
4787 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4788 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4789 constructor can produce constant init, so rely on cp_finish_decl to
4790 clear TREE_READONLY if the variable has non-constant init. */
a5b1863e 4791 TREE_READONLY (decl) = 1;
4792 if (type_quals & TYPE_QUAL_VOLATILE)
4793 {
4794 TREE_SIDE_EFFECTS (decl) = 1;
4795 TREE_THIS_VOLATILE (decl) = 1;
4796 }
d91a20bc 4797 if (type_quals & TYPE_QUAL_RESTRICT)
a5b1863e 4798 {
adfb367f 4799 while (type && TREE_CODE (type) == ARRAY_TYPE)
4800 /* Allow 'restrict' on arrays of pointers.
4801 FIXME currently we just ignore it. */
4802 type = TREE_TYPE (type);
4803 if (!type
4804 || !POINTER_TYPE_P (type)
4805 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
b0b1af64 4806 error ("invalid use of %<restrict%>");
a5b1863e 4807 }
4808}
4809
b594087e 4810struct c_type_hasher : ggc_ptr_hash<tree_node>
2ef51f0e 4811{
4812 static hashval_t hash (tree);
4813 static bool equal (tree, tree);
4814};
4815
4ee9c684 4816/* Hash function for the problem of multiple type definitions in
4817 different files. This must hash all types that will compare
4818 equal via comptypes to the same value. In practice it hashes
0bed3869 4819 on some of the simple stuff and leaves the details to comptypes. */
4ee9c684 4820
2ef51f0e 4821hashval_t
4822c_type_hasher::hash (tree t)
4ee9c684 4823{
ecf2703d 4824 int n_elements;
4ee9c684 4825 int shift, size;
4ee9c684 4826 tree t2;
4827 switch (TREE_CODE (t))
4828 {
fbf0afd1 4829 /* For pointers, hash on pointee type plus some swizzling. */
2363ef00 4830 case POINTER_TYPE:
2ef51f0e 4831 return hash (TREE_TYPE (t)) ^ 0x3003003;
2363ef00 4832 /* Hash on number of elements and total size. */
4833 case ENUMERAL_TYPE:
4834 shift = 3;
4835 t2 = TYPE_VALUES (t);
4836 break;
4837 case RECORD_TYPE:
4838 shift = 0;
4839 t2 = TYPE_FIELDS (t);
4840 break;
4841 case QUAL_UNION_TYPE:
4842 shift = 1;
4843 t2 = TYPE_FIELDS (t);
4844 break;
4845 case UNION_TYPE:
4846 shift = 2;
4847 t2 = TYPE_FIELDS (t);
4848 break;
4849 default:
231bd014 4850 gcc_unreachable ();
4ee9c684 4851 }
ecf2703d 4852 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4853 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4854 n_elements = list_length (t2);
78c2e180 4855 /* We might have a VLA here. */
4856 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4857 size = 0;
4858 else
f9ae6f95 4859 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
ecf2703d 4860 return ((size << 24) | (n_elements << shift));
4ee9c684 4861}
4862
2ef51f0e 4863bool
4864c_type_hasher::equal (tree t1, tree t2)
4865{
4866 return lang_hooks.types_compatible_p (t1, t2);
4867}
4868
4869static GTY(()) hash_table<c_type_hasher> *type_hash_table;
1ecd4018 4870
b5ba9f3a 4871/* Return the typed-based alias set for T, which may be an expression
f7c44134 4872 or a type. Return -1 if we don't do anything special. */
ab2c1de8 4873
32c2fdea 4874alias_set_type
1cae46be 4875c_common_get_alias_set (tree t)
b5ba9f3a 4876{
e58c17e7 4877 /* For VLAs, use the alias set of the element type rather than the
4878 default of alias set 0 for types compared structurally. */
4879 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4880 {
4881 if (TREE_CODE (t) == ARRAY_TYPE)
4882 return get_alias_set (TREE_TYPE (t));
4883 return -1;
4884 }
4885
9fcc3e54 4886 /* That's all the expressions we handle specially. */
84166705 4887 if (!TYPE_P (t))
9fcc3e54 4888 return -1;
4889
d716ce75 4890 /* The C standard guarantees that any object may be accessed via an
9fcc3e54 4891 lvalue that has character type. */
4892 if (t == char_type_node
4893 || t == signed_char_type_node
4894 || t == unsigned_char_type_node)
f7c44134 4895 return 0;
a5b1863e 4896
1607663f 4897 /* The C standard specifically allows aliasing between signed and
4898 unsigned variants of the same type. We treat the signed
4899 variant as canonical. */
78a8ed03 4900 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
a8868e19 4901 {
4070745f 4902 tree t1 = c_common_signed_type (t);
1607663f 4903
a8868e19 4904 /* t1 == t can happen for boolean nodes which are always unsigned. */
4905 if (t1 != t)
4906 return get_alias_set (t1);
4907 }
1e2513d9 4908
4ee9c684 4909 /* Handle the case of multiple type nodes referring to "the same" type,
4910 which occurs with IMA. These share an alias set. FIXME: Currently only
4911 C90 is handled. (In C99 type compatibility is not transitive, which
4912 complicates things mightily. The alias set splay trees can theoretically
4913 represent this, but insertion is tricky when you consider all the
4914 different orders things might arrive in.) */
4915
4916 if (c_language != clk_c || flag_isoc99)
4917 return -1;
4918
0bed3869 4919 /* Save time if there's only one input file. */
e08bd2f4 4920 if (num_in_fnames == 1)
4ee9c684 4921 return -1;
4922
4923 /* Pointers need special handling if they point to any type that
4924 needs special handling (below). */
4925 if (TREE_CODE (t) == POINTER_TYPE)
4926 {
4927 tree t2;
4928 /* Find bottom type under any nested POINTERs. */
b27ac6b5 4929 for (t2 = TREE_TYPE (t);
af592f67 4930 TREE_CODE (t2) == POINTER_TYPE;
4931 t2 = TREE_TYPE (t2))
4932 ;
8e539fdc 4933 if (!RECORD_OR_UNION_TYPE_P (t2)
4934 && TREE_CODE (t2) != ENUMERAL_TYPE)
af592f67 4935 return -1;
4ee9c684 4936 if (TYPE_SIZE (t2) == 0)
af592f67 4937 return -1;
4ee9c684 4938 }
4939 /* These are the only cases that need special handling. */
8e539fdc 4940 if (!RECORD_OR_UNION_TYPE_P (t)
4ee9c684 4941 && TREE_CODE (t) != ENUMERAL_TYPE
4ee9c684 4942 && TREE_CODE (t) != POINTER_TYPE)
4943 return -1;
4944 /* Undefined? */
4945 if (TYPE_SIZE (t) == 0)
4946 return -1;
4947
b27ac6b5 4948 /* Look up t in hash table. Only one of the compatible types within each
4ee9c684 4949 alias set is recorded in the table. */
4950 if (!type_hash_table)
2ef51f0e 4951 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
4952 tree *slot = type_hash_table->find_slot (t, INSERT);
4ee9c684 4953 if (*slot != NULL)
ad16cb2c 4954 {
4955 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4956 return TYPE_ALIAS_SET ((tree)*slot);
4957 }
4ee9c684 4958 else
4959 /* Our caller will assign and record (in t) a new alias set; all we need
4960 to do is remember t in the hash table. */
4961 *slot = t;
4962
f7c44134 4963 return -1;
b5ba9f3a 4964}
902b4e01 4965\f
e60a6f7b 4966/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
a179a7dc 4967 the IS_SIZEOF parameter indicates which operator is being applied.
e60a6f7b 4968 The COMPLAIN flag controls whether we should diagnose possibly
4969 ill-formed constructs or not. LOC is the location of the SIZEOF or
a179a7dc 4970 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
4971 a type in any context should be returned, rather than the normal
4972 alignment for that type. */
908c697e 4973
902b4e01 4974tree
e60a6f7b 4975c_sizeof_or_alignof_type (location_t loc,
a179a7dc 4976 tree type, bool is_sizeof, bool min_alignof,
4977 int complain)
902b4e01 4978{
d4c4d95c 4979 const char *op_name;
4980 tree value = NULL;
4981 enum tree_code type_code = TREE_CODE (type);
1cae46be 4982
908c697e 4983 op_name = is_sizeof ? "sizeof" : "__alignof__";
1cae46be 4984
d4c4d95c 4985 if (type_code == FUNCTION_TYPE)
902b4e01 4986 {
908c697e 4987 if (is_sizeof)
d4c4d95c 4988 {
9205a6cc 4989 if (complain && warn_pointer_arith)
4990 pedwarn (loc, OPT_Wpointer_arith,
8864917d 4991 "invalid application of %<sizeof%> to a function type");
ebd21de4 4992 else if (!complain)
4993 return error_mark_node;
d4c4d95c 4994 value = size_one_node;
4995 }
4996 else
83e25171 4997 {
4998 if (complain)
4999 {
5000 if (c_dialect_cxx ())
29438999 5001 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
83e25171 5002 "%<alignof%> applied to a function type");
5003 else
29438999 5004 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
83e25171 5005 "%<_Alignof%> applied to a function type");
5006 }
5007 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5008 }
d4c4d95c 5009 }
5010 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5011 {
1cae46be 5012 if (type_code == VOID_TYPE
9205a6cc 5013 && complain && warn_pointer_arith)
5014 pedwarn (loc, OPT_Wpointer_arith,
8864917d 5015 "invalid application of %qs to a void type", op_name);
ebd21de4 5016 else if (!complain)
5017 return error_mark_node;
d4c4d95c 5018 value = size_one_node;
902b4e01 5019 }
3df19e1b 5020 else if (!COMPLETE_TYPE_P (type)
5021 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
902b4e01 5022 {
d3a4d008 5023 if (complain)
3df19e1b 5024 error_at (loc, "invalid application of %qs to incomplete type %qT",
e60a6f7b 5025 op_name, type);
9c719c74 5026 return error_mark_node;
902b4e01 5027 }
3df19e1b 5028 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5029 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5030 {
5031 if (complain)
5032 error_at (loc, "invalid application of %qs to array type %qT of "
5033 "incomplete element type", op_name, type);
5034 return error_mark_node;
5035 }
902b4e01 5036 else
d4c4d95c 5037 {
908c697e 5038 if (is_sizeof)
d4c4d95c 5039 /* Convert in case a char is more than one unit. */
389dd41b 5040 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5041 size_int (TYPE_PRECISION (char_type_node)
5042 / BITS_PER_UNIT));
a179a7dc 5043 else if (min_alignof)
c2c4ae8d 5044 value = size_int (min_align_of_type (type));
d4c4d95c 5045 else
d37625c0 5046 value = size_int (TYPE_ALIGN_UNIT (type));
d4c4d95c 5047 }
902b4e01 5048
5a1fe2db 5049 /* VALUE will have the middle-end integer type sizetype.
5050 However, we should really return a value of type `size_t',
5051 which is just a typedef for an ordinary integer type. */
389dd41b 5052 value = fold_convert_loc (loc, size_type_node, value);
1cae46be 5053
d4c4d95c 5054 return value;
902b4e01 5055}
5056
5057/* Implement the __alignof keyword: Return the minimum required
097b5c8b 5058 alignment of EXPR, measured in bytes. For VAR_DECLs,
5059 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
e60a6f7b 5060 from an "aligned" __attribute__ specification). LOC is the
5061 location of the ALIGNOF operator. */
72040e7e 5062
902b4e01 5063tree
e60a6f7b 5064c_alignof_expr (location_t loc, tree expr)
902b4e01 5065{
5066 tree t;
5067
097b5c8b 5068 if (VAR_OR_FUNCTION_DECL_P (expr))
d37625c0 5069 t = size_int (DECL_ALIGN_UNIT (expr));
1cae46be 5070
902b4e01 5071 else if (TREE_CODE (expr) == COMPONENT_REF
5072 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5073 {
e60a6f7b 5074 error_at (loc, "%<__alignof%> applied to a bit-field");
902b4e01 5075 t = size_one_node;
5076 }
5077 else if (TREE_CODE (expr) == COMPONENT_REF
7cc7e163 5078 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
d37625c0 5079 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
1cae46be 5080
aa3e402e 5081 else if (INDIRECT_REF_P (expr))
902b4e01 5082 {
5083 tree t = TREE_OPERAND (expr, 0);
5084 tree best = t;
5085 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1cae46be 5086
72dd6141 5087 while (CONVERT_EXPR_P (t)
7cc7e163 5088 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
902b4e01 5089 {
5090 int thisalign;
5091
5092 t = TREE_OPERAND (t, 0);
5093 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5094 if (thisalign > bestalign)
5095 best = t, bestalign = thisalign;
5096 }
e60a6f7b 5097 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
902b4e01 5098 }
5099 else
e60a6f7b 5100 return c_alignof (loc, TREE_TYPE (expr));
902b4e01 5101
389dd41b 5102 return fold_convert_loc (loc, size_type_node, t);
902b4e01 5103}
5104\f
8fe4a266 5105/* Handle C and C++ default attributes. */
5106
5107enum built_in_attribute
5108{
5109#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5110#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
c8010b80 5111#define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
8fe4a266 5112#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5113#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
8fe4a266 5114#include "builtin-attrs.def"
5115#undef DEF_ATTR_NULL_TREE
5116#undef DEF_ATTR_INT
c8010b80 5117#undef DEF_ATTR_STRING
8fe4a266 5118#undef DEF_ATTR_IDENT
5119#undef DEF_ATTR_TREE_LIST
8fe4a266 5120 ATTR_LAST
5121};
5122
5123static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5124
1cae46be 5125static void c_init_attributes (void);
8fe4a266 5126
27213ba3 5127enum c_builtin_type
72040e7e 5128{
d2d4bdde 5129#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5130#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5131#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5132#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5133#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5134#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
0a39fd54 5135#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
3c77ca67 5136#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5137 ARG6) NAME,
5138#define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5139 ARG6, ARG7) NAME,
5140#define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5141 ARG6, ARG7, ARG8) NAME,
43895be5 5142#define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5143 ARG6, ARG7, ARG8, ARG9) NAME,
5144#define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5145 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
5146#define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5147 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
d2d4bdde 5148#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5149#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5150#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
03901330 5151#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
0a39fd54 5152#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3c77ca67 5153#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
6349b8cc 5154 NAME,
e561d5e1 5155#define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5156 ARG6) NAME,
6349b8cc 5157#define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5158 ARG6, ARG7) NAME,
d2d4bdde 5159#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5160#include "builtin-types.def"
5161#undef DEF_PRIMITIVE_TYPE
5162#undef DEF_FUNCTION_TYPE_0
5163#undef DEF_FUNCTION_TYPE_1
5164#undef DEF_FUNCTION_TYPE_2
5165#undef DEF_FUNCTION_TYPE_3
5166#undef DEF_FUNCTION_TYPE_4
0a39fd54 5167#undef DEF_FUNCTION_TYPE_5
5168#undef DEF_FUNCTION_TYPE_6
27213ba3 5169#undef DEF_FUNCTION_TYPE_7
bc7bff74 5170#undef DEF_FUNCTION_TYPE_8
43895be5 5171#undef DEF_FUNCTION_TYPE_9
5172#undef DEF_FUNCTION_TYPE_10
5173#undef DEF_FUNCTION_TYPE_11
d2d4bdde 5174#undef DEF_FUNCTION_TYPE_VAR_0
5175#undef DEF_FUNCTION_TYPE_VAR_1
5176#undef DEF_FUNCTION_TYPE_VAR_2
03901330 5177#undef DEF_FUNCTION_TYPE_VAR_3
0a39fd54 5178#undef DEF_FUNCTION_TYPE_VAR_4
5179#undef DEF_FUNCTION_TYPE_VAR_5
e561d5e1 5180#undef DEF_FUNCTION_TYPE_VAR_6
6349b8cc 5181#undef DEF_FUNCTION_TYPE_VAR_7
d2d4bdde 5182#undef DEF_POINTER_TYPE
27213ba3 5183 BT_LAST
5184};
5185
5186typedef enum c_builtin_type builtin_type;
d2d4bdde 5187
27213ba3 5188/* A temporary array for c_common_nodes_and_builtins. Used in
5189 communication with def_fn_type. */
5190static tree builtin_types[(int) BT_LAST + 1];
d2d4bdde 5191
27213ba3 5192/* A helper function for c_common_nodes_and_builtins. Build function type
5193 for DEF with return type RET and N arguments. If VAR is true, then the
5194 function should be variadic after those N arguments.
5195
5196 Takes special care not to ICE if any of the types involved are
5197 error_mark_node, which indicates that said type is not in fact available
5198 (see builtin_type_for_size). In which case the function type as a whole
5199 should be error_mark_node. */
5200
5201static void
5202def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5203{
3a939d12 5204 tree t;
5205 tree *args = XALLOCAVEC (tree, n);
27213ba3 5206 va_list list;
5207 int i;
5208
5209 va_start (list, n);
5210 for (i = 0; i < n; ++i)
5211 {
7d339f93 5212 builtin_type a = (builtin_type) va_arg (list, int);
27213ba3 5213 t = builtin_types[a];
5214 if (t == error_mark_node)
5215 goto egress;
3a939d12 5216 args[i] = t;
27213ba3 5217 }
27213ba3 5218
27213ba3 5219 t = builtin_types[ret];
5220 if (t == error_mark_node)
5221 goto egress;
3a939d12 5222 if (var)
5223 t = build_varargs_function_type_array (t, n, args);
5224 else
5225 t = build_function_type_array (t, n, args);
27213ba3 5226
5227 egress:
5228 builtin_types[def] = t;
451c8e2f 5229 va_end (list);
27213ba3 5230}
5231
dce22712 5232/* Build builtin functions common to both C and C++ language
5233 frontends. */
5234
5235static void
5236c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5237{
5238#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5239 builtin_types[ENUM] = VALUE;
5240#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5241 def_fn_type (ENUM, RETURN, 0, 0);
5242#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5243 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5244#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5245 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5246#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5247 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5248#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5249 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5250#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5251 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5252#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5253 ARG6) \
5254 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5255#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5256 ARG6, ARG7) \
5257 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
bc7bff74 5258#define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5259 ARG6, ARG7, ARG8) \
5260 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5261 ARG7, ARG8);
43895be5 5262#define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5263 ARG6, ARG7, ARG8, ARG9) \
5264 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5265 ARG7, ARG8, ARG9);
5266#define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5267 ARG6, ARG7, ARG8, ARG9, ARG10) \
5268 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5269 ARG7, ARG8, ARG9, ARG10);
5270#define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5271 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5272 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5273 ARG7, ARG8, ARG9, ARG10, ARG11);
dce22712 5274#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5275 def_fn_type (ENUM, RETURN, 1, 0);
5276#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5277 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5278#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5279 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5280#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5281 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5282#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5283 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5284#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5285 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
e561d5e1 5286#define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5287 ARG6) \
5288 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
6349b8cc 5289#define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5290 ARG6, ARG7) \
5291 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
dce22712 5292#define DEF_POINTER_TYPE(ENUM, TYPE) \
5293 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5294
5295#include "builtin-types.def"
5296
5297#undef DEF_PRIMITIVE_TYPE
3c77ca67 5298#undef DEF_FUNCTION_TYPE_0
dce22712 5299#undef DEF_FUNCTION_TYPE_1
5300#undef DEF_FUNCTION_TYPE_2
5301#undef DEF_FUNCTION_TYPE_3
5302#undef DEF_FUNCTION_TYPE_4
5303#undef DEF_FUNCTION_TYPE_5
5304#undef DEF_FUNCTION_TYPE_6
3c77ca67 5305#undef DEF_FUNCTION_TYPE_7
5306#undef DEF_FUNCTION_TYPE_8
43895be5 5307#undef DEF_FUNCTION_TYPE_9
5308#undef DEF_FUNCTION_TYPE_10
5309#undef DEF_FUNCTION_TYPE_11
dce22712 5310#undef DEF_FUNCTION_TYPE_VAR_0
5311#undef DEF_FUNCTION_TYPE_VAR_1
5312#undef DEF_FUNCTION_TYPE_VAR_2
5313#undef DEF_FUNCTION_TYPE_VAR_3
5314#undef DEF_FUNCTION_TYPE_VAR_4
5315#undef DEF_FUNCTION_TYPE_VAR_5
e561d5e1 5316#undef DEF_FUNCTION_TYPE_VAR_6
6349b8cc 5317#undef DEF_FUNCTION_TYPE_VAR_7
dce22712 5318#undef DEF_POINTER_TYPE
5319 builtin_types[(int) BT_LAST] = NULL_TREE;
5320
5321 c_init_attributes ();
5322
5323#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5324 NONANSI_P, ATTRS, IMPLICIT, COND) \
5325 if (NAME && COND) \
5326 def_builtin_1 (ENUM, NAME, CLASS, \
5327 builtin_types[(int) TYPE], \
5328 builtin_types[(int) LIBTYPE], \
5329 BOTH_P, FALLBACK_P, NONANSI_P, \
5330 built_in_attributes[(int) ATTRS], IMPLICIT);
5331#include "builtins.def"
dce22712 5332
87eb1c28 5333 targetm.init_builtins ();
5334
471eff36 5335 build_common_builtin_nodes ();
dce22712 5336
a89e6c15 5337 if (flag_cilkplus)
d037099f 5338 cilk_init_builtins ();
dce22712 5339}
5340
9e6687c8 5341/* Like get_identifier, but avoid warnings about null arguments when
5342 the argument may be NULL for targets where GCC lacks stdint.h type
5343 information. */
5344
5345static inline tree
5346c_get_ident (const char *id)
5347{
5348 return get_identifier (id);
5349}
5350
27213ba3 5351/* Build tree nodes and builtin functions common to both C and C++ language
5352 frontends. */
5353
5354void
5355c_common_nodes_and_builtins (void)
5356{
924bbf02 5357 int char16_type_size;
5358 int char32_type_size;
174fcc61 5359 int wchar_type_size;
5360 tree array_domain_type;
2d47cc32 5361 tree va_list_ref_type_node;
8a15c04a 5362 tree va_list_arg_type_node;
9f75f026 5363 int i;
a66c9326 5364
600695e0 5365 build_common_tree_nodes (flag_signed_char);
e593356b 5366
174fcc61 5367 /* Define `int' and `char' first so that dbx will output them first. */
d946ea19 5368 record_builtin_type (RID_INT, NULL, integer_type_node);
174fcc61 5369 record_builtin_type (RID_CHAR, "char", char_type_node);
5370
5371 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5372 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5373 but not C. Are the conditionals here needed? */
c0f19401 5374 if (c_dialect_cxx ())
d946ea19 5375 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
174fcc61 5376 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5377 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5378 record_builtin_type (RID_MAX, "long unsigned int",
5379 long_unsigned_type_node);
9f75f026 5380
5381 for (i = 0; i < NUM_INT_N_ENTS; i ++)
6388cfe2 5382 {
9f75f026 5383 char name[25];
5384
5385 sprintf (name, "__int%d", int_n_data[i].bitsize);
76738f56 5386 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
9f75f026 5387 int_n_trees[i].signed_type);
5388 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
76738f56 5389 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
6388cfe2 5390 }
9f75f026 5391
c0f19401 5392 if (c_dialect_cxx ())
174fcc61 5393 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5394 record_builtin_type (RID_MAX, "long long int",
5395 long_long_integer_type_node);
5396 record_builtin_type (RID_MAX, "long long unsigned int",
5397 long_long_unsigned_type_node);
c0f19401 5398 if (c_dialect_cxx ())
174fcc61 5399 record_builtin_type (RID_MAX, "long long unsigned",
5400 long_long_unsigned_type_node);
5401 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5402 record_builtin_type (RID_MAX, "short unsigned int",
5403 short_unsigned_type_node);
c0f19401 5404 if (c_dialect_cxx ())
174fcc61 5405 record_builtin_type (RID_MAX, "unsigned short",
5406 short_unsigned_type_node);
5407
5408 /* Define both `signed char' and `unsigned char'. */
5409 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5410 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5411
771d21fa 5412 /* These are types that c_common_type_for_size and
5413 c_common_type_for_mode use. */
e60a6f7b 5414 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5415 TYPE_DECL, NULL_TREE,
dc24ddbd 5416 intQI_type_node));
e60a6f7b 5417 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5418 TYPE_DECL, NULL_TREE,
dc24ddbd 5419 intHI_type_node));
e60a6f7b 5420 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5421 TYPE_DECL, NULL_TREE,
dc24ddbd 5422 intSI_type_node));
e60a6f7b 5423 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5424 TYPE_DECL, NULL_TREE,
dc24ddbd 5425 intDI_type_node));
174fcc61 5426#if HOST_BITS_PER_WIDE_INT >= 64
9f75f026 5427 /* Note that this is different than the __int128 type that's part of
5428 the generic __intN support. */
f1515a39 5429 if (targetm.scalar_mode_supported_p (TImode))
e60a6f7b 5430 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5431 TYPE_DECL,
f1515a39 5432 get_identifier ("__int128_t"),
5433 intTI_type_node));
174fcc61 5434#endif
e60a6f7b 5435 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5436 TYPE_DECL, NULL_TREE,
dc24ddbd 5437 unsigned_intQI_type_node));
e60a6f7b 5438 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5439 TYPE_DECL, NULL_TREE,
dc24ddbd 5440 unsigned_intHI_type_node));
e60a6f7b 5441 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5442 TYPE_DECL, NULL_TREE,
dc24ddbd 5443 unsigned_intSI_type_node));
e60a6f7b 5444 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5445 TYPE_DECL, NULL_TREE,
dc24ddbd 5446 unsigned_intDI_type_node));
174fcc61 5447#if HOST_BITS_PER_WIDE_INT >= 64
f1515a39 5448 if (targetm.scalar_mode_supported_p (TImode))
e60a6f7b 5449 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5450 TYPE_DECL,
f1515a39 5451 get_identifier ("__uint128_t"),
5452 unsigned_intTI_type_node));
174fcc61 5453#endif
5454
5455 /* Create the widest literal types. */
5456 widest_integer_literal_type_node
5457 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
e60a6f7b 5458 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5459 TYPE_DECL, NULL_TREE,
dc24ddbd 5460 widest_integer_literal_type_node));
174fcc61 5461
5462 widest_unsigned_literal_type_node
5463 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
e60a6f7b 5464 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5465 TYPE_DECL, NULL_TREE,
dc24ddbd 5466 widest_unsigned_literal_type_node));
174fcc61 5467
654ef926 5468 signed_size_type_node = c_common_signed_type (size_type_node);
174fcc61 5469
73673831 5470 pid_type_node =
5471 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5472
d946ea19 5473 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5474 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
174fcc61 5475 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5476
82c85aba 5477 if (!c_dialect_cxx ())
5478 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
5479 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
5480 record_builtin_type ((enum rid) (RID_FLOATN_NX_FIRST + i), NULL,
5481 FLOATN_NX_TYPE_NODE (i));
5482
c4503c0a 5483 /* Only supported decimal floating point extension if the target
5484 actually supports underlying modes. */
48e1416a 5485 if (targetm.scalar_mode_supported_p (SDmode)
c4503c0a 5486 && targetm.scalar_mode_supported_p (DDmode)
5487 && targetm.scalar_mode_supported_p (TDmode))
5488 {
5489 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5490 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5491 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5492 }
5493
9421ebb9 5494 if (targetm.fixed_point_supported_p ())
5495 {
5496 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5497 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5498 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5499 record_builtin_type (RID_MAX, "long long _Fract",
5500 long_long_fract_type_node);
5501 record_builtin_type (RID_MAX, "unsigned short _Fract",
5502 unsigned_short_fract_type_node);
5503 record_builtin_type (RID_MAX, "unsigned _Fract",
5504 unsigned_fract_type_node);
5505 record_builtin_type (RID_MAX, "unsigned long _Fract",
5506 unsigned_long_fract_type_node);
5507 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5508 unsigned_long_long_fract_type_node);
5509 record_builtin_type (RID_MAX, "_Sat short _Fract",
5510 sat_short_fract_type_node);
5511 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5512 record_builtin_type (RID_MAX, "_Sat long _Fract",
5513 sat_long_fract_type_node);
5514 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5515 sat_long_long_fract_type_node);
5516 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5517 sat_unsigned_short_fract_type_node);
5518 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5519 sat_unsigned_fract_type_node);
5520 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5521 sat_unsigned_long_fract_type_node);
5522 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5523 sat_unsigned_long_long_fract_type_node);
5524 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5525 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5526 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5527 record_builtin_type (RID_MAX, "long long _Accum",
5528 long_long_accum_type_node);
5529 record_builtin_type (RID_MAX, "unsigned short _Accum",
5530 unsigned_short_accum_type_node);
5531 record_builtin_type (RID_MAX, "unsigned _Accum",
5532 unsigned_accum_type_node);
5533 record_builtin_type (RID_MAX, "unsigned long _Accum",
5534 unsigned_long_accum_type_node);
5535 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5536 unsigned_long_long_accum_type_node);
5537 record_builtin_type (RID_MAX, "_Sat short _Accum",
5538 sat_short_accum_type_node);
5539 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5540 record_builtin_type (RID_MAX, "_Sat long _Accum",
5541 sat_long_accum_type_node);
5542 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5543 sat_long_long_accum_type_node);
5544 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5545 sat_unsigned_short_accum_type_node);
5546 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5547 sat_unsigned_accum_type_node);
5548 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5549 sat_unsigned_long_accum_type_node);
5550 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5551 sat_unsigned_long_long_accum_type_node);
5552
5553 }
5554
e60a6f7b 5555 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5556 TYPE_DECL,
dc24ddbd 5557 get_identifier ("complex int"),
5558 complex_integer_type_node));
e60a6f7b 5559 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5560 TYPE_DECL,
dc24ddbd 5561 get_identifier ("complex float"),
5562 complex_float_type_node));
e60a6f7b 5563 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5564 TYPE_DECL,
dc24ddbd 5565 get_identifier ("complex double"),
5566 complex_double_type_node));
5567 lang_hooks.decls.pushdecl
e60a6f7b 5568 (build_decl (UNKNOWN_LOCATION,
5569 TYPE_DECL, get_identifier ("complex long double"),
20325f61 5570 complex_long_double_type_node));
174fcc61 5571
82c85aba 5572 if (!c_dialect_cxx ())
5573 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
5574 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
5575 {
5576 char buf[30];
5577 sprintf (buf, "complex _Float%d%s", floatn_nx_types[i].n,
5578 floatn_nx_types[i].extended ? "x" : "");
5579 lang_hooks.decls.pushdecl
5580 (build_decl (UNKNOWN_LOCATION,
5581 TYPE_DECL,
5582 get_identifier (buf),
5583 COMPLEX_FLOATN_NX_TYPE_NODE (i)));
5584 }
5585
e256d445 5586 if (c_dialect_cxx ())
5587 /* For C++, make fileptr_type_node a distinct void * type until
5588 FILE type is defined. */
e086912e 5589 fileptr_type_node = build_variant_type_copy (ptr_type_node);
e256d445 5590
d946ea19 5591 record_builtin_type (RID_VOID, NULL, void_type_node);
174fcc61 5592
6753bca0 5593 /* Set the TYPE_NAME for any variants that were built before
5594 record_builtin_type gave names to the built-in types. */
5595 {
5596 tree void_name = TYPE_NAME (void_type_node);
5597 TYPE_NAME (void_type_node) = NULL_TREE;
5598 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5599 = void_name;
5600 TYPE_NAME (void_type_node) = void_name;
5601 }
5602
174fcc61 5603 void_list_node = build_void_list_node ();
5604
5605 /* Make a type to be the domain of a few array types
5606 whose domains don't really matter.
5607 200 is small enough that it always fits in size_t
5608 and large enough that it can hold most function names for the
5609 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5610 array_domain_type = build_index_type (size_int (200));
5611
5612 /* Make a type for arrays of characters.
5613 With luck nothing will ever really depend on the length of this
5614 array type. */
5615 char_array_type_node
5616 = build_array_type (char_type_node, array_domain_type);
5617
d2d4bdde 5618 string_type_node = build_pointer_type (char_type_node);
5619 const_string_type_node
5620 = build_pointer_type (build_qualified_type
5621 (char_type_node, TYPE_QUAL_CONST));
5622
174fcc61 5623 /* This is special for C++ so functions can be overloaded. */
18ef7ac2 5624 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
174fcc61 5625 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5626 wchar_type_size = TYPE_PRECISION (wchar_type_node);
f3449a3c 5627 underlying_wchar_type_node = wchar_type_node;
c0f19401 5628 if (c_dialect_cxx ())
174fcc61 5629 {
78a8ed03 5630 if (TYPE_UNSIGNED (wchar_type_node))
174fcc61 5631 wchar_type_node = make_unsigned_type (wchar_type_size);
5632 else
5633 wchar_type_node = make_signed_type (wchar_type_size);
5634 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5635 }
174fcc61 5636
5637 /* This is for wide string constants. */
5638 wchar_array_type_node
5639 = build_array_type (wchar_type_node, array_domain_type);
5640
924bbf02 5641 /* Define 'char16_t'. */
5642 char16_type_node = get_identifier (CHAR16_TYPE);
5643 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5644 char16_type_size = TYPE_PRECISION (char16_type_node);
5645 if (c_dialect_cxx ())
5646 {
5647 char16_type_node = make_unsigned_type (char16_type_size);
5648
60777f69 5649 if (cxx_dialect >= cxx11)
924bbf02 5650 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5651 }
5652
5653 /* This is for UTF-16 string constants. */
5654 char16_array_type_node
5655 = build_array_type (char16_type_node, array_domain_type);
5656
5657 /* Define 'char32_t'. */
5658 char32_type_node = get_identifier (CHAR32_TYPE);
5659 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5660 char32_type_size = TYPE_PRECISION (char32_type_node);
5661 if (c_dialect_cxx ())
5662 {
5663 char32_type_node = make_unsigned_type (char32_type_size);
5664
60777f69 5665 if (cxx_dialect >= cxx11)
924bbf02 5666 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5667 }
5668
5669 /* This is for UTF-32 string constants. */
5670 char32_array_type_node
5671 = build_array_type (char32_type_node, array_domain_type);
5672
6bf5ed8d 5673 wint_type_node =
5674 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5675
5676 intmax_type_node =
5677 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5678 uintmax_type_node =
5679 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5680
f3449a3c 5681 if (SIG_ATOMIC_TYPE)
5682 sig_atomic_type_node =
9e6687c8 5683 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
f3449a3c 5684 if (INT8_TYPE)
5685 int8_type_node =
9e6687c8 5686 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
f3449a3c 5687 if (INT16_TYPE)
5688 int16_type_node =
9e6687c8 5689 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
f3449a3c 5690 if (INT32_TYPE)
5691 int32_type_node =
9e6687c8 5692 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
f3449a3c 5693 if (INT64_TYPE)
5694 int64_type_node =
9e6687c8 5695 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
f3449a3c 5696 if (UINT8_TYPE)
5697 uint8_type_node =
9e6687c8 5698 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
f3449a3c 5699 if (UINT16_TYPE)
d1081017 5700 c_uint16_type_node = uint16_type_node =
9e6687c8 5701 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
f3449a3c 5702 if (UINT32_TYPE)
d1081017 5703 c_uint32_type_node = uint32_type_node =
9e6687c8 5704 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
f3449a3c 5705 if (UINT64_TYPE)
d1081017 5706 c_uint64_type_node = uint64_type_node =
9e6687c8 5707 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
f3449a3c 5708 if (INT_LEAST8_TYPE)
5709 int_least8_type_node =
9e6687c8 5710 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
f3449a3c 5711 if (INT_LEAST16_TYPE)
5712 int_least16_type_node =
9e6687c8 5713 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
f3449a3c 5714 if (INT_LEAST32_TYPE)
5715 int_least32_type_node =
9e6687c8 5716 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
f3449a3c 5717 if (INT_LEAST64_TYPE)
5718 int_least64_type_node =
9e6687c8 5719 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
f3449a3c 5720 if (UINT_LEAST8_TYPE)
5721 uint_least8_type_node =
9e6687c8 5722 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
f3449a3c 5723 if (UINT_LEAST16_TYPE)
5724 uint_least16_type_node =
9e6687c8 5725 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
f3449a3c 5726 if (UINT_LEAST32_TYPE)
5727 uint_least32_type_node =
9e6687c8 5728 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
f3449a3c 5729 if (UINT_LEAST64_TYPE)
5730 uint_least64_type_node =
9e6687c8 5731 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
f3449a3c 5732 if (INT_FAST8_TYPE)
5733 int_fast8_type_node =
9e6687c8 5734 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
f3449a3c 5735 if (INT_FAST16_TYPE)
5736 int_fast16_type_node =
9e6687c8 5737 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
f3449a3c 5738 if (INT_FAST32_TYPE)
5739 int_fast32_type_node =
9e6687c8 5740 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
f3449a3c 5741 if (INT_FAST64_TYPE)
5742 int_fast64_type_node =
9e6687c8 5743 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
f3449a3c 5744 if (UINT_FAST8_TYPE)
5745 uint_fast8_type_node =
9e6687c8 5746 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
f3449a3c 5747 if (UINT_FAST16_TYPE)
5748 uint_fast16_type_node =
9e6687c8 5749 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
f3449a3c 5750 if (UINT_FAST32_TYPE)
5751 uint_fast32_type_node =
9e6687c8 5752 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
f3449a3c 5753 if (UINT_FAST64_TYPE)
5754 uint_fast64_type_node =
9e6687c8 5755 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
f3449a3c 5756 if (INTPTR_TYPE)
5757 intptr_type_node =
9e6687c8 5758 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
f3449a3c 5759 if (UINTPTR_TYPE)
5760 uintptr_type_node =
9e6687c8 5761 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
f3449a3c 5762
3a939d12 5763 default_function_type
5764 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
6bf5ed8d 5765 ptrdiff_type_node
5766 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
11773141 5767 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
6bf5ed8d 5768
dc24ddbd 5769 lang_hooks.decls.pushdecl
e60a6f7b 5770 (build_decl (UNKNOWN_LOCATION,
5771 TYPE_DECL, get_identifier ("__builtin_va_list"),
20325f61 5772 va_list_type_node));
202d6e5f 5773 if (targetm.enum_va_list_p)
acd6f472 5774 {
5775 int l;
5776 const char *pname;
5777 tree ptype;
5f57a8b1 5778
202d6e5f 5779 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
acd6f472 5780 {
5781 lang_hooks.decls.pushdecl
5782 (build_decl (UNKNOWN_LOCATION,
5783 TYPE_DECL, get_identifier (pname),
5784 ptype));
ab2c1de8 5785
acd6f472 5786 }
5787 }
8a15c04a 5788
8a15c04a 5789 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
2d47cc32 5790 {
5791 va_list_arg_type_node = va_list_ref_type_node =
5792 build_pointer_type (TREE_TYPE (va_list_type_node));
5793 }
8a15c04a 5794 else
2d47cc32 5795 {
5796 va_list_arg_type_node = va_list_type_node;
5797 va_list_ref_type_node = build_reference_type (va_list_type_node);
5798 }
1cae46be 5799
dce22712 5800 if (!flag_preprocess_only)
5801 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
ffa8918b 5802
5c62f199 5803 main_identifier_node = get_identifier ("main");
ae84079f 5804
5805 /* Create the built-in __null node. It is important that this is
5806 not shared. */
271e739a 5807 null_node = make_int_cst (1, 1);
ae84079f 5808 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
27213ba3 5809
5810 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5811 memset (builtin_types, 0, sizeof (builtin_types));
72040e7e 5812}
a66c9326 5813
79b01846 5814/* The number of named compound-literals generated thus far. */
5815static GTY(()) int compound_literal_number;
5816
5817/* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5818
5819void
5820set_compound_literal_name (tree decl)
5821{
5822 char *name;
5823 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5824 compound_literal_number);
5825 compound_literal_number++;
5826 DECL_NAME (decl) = get_identifier (name);
5827}
5828
2e474820 5829/* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
5830 TYPE and operand OP. */
5831
5832static tree
5833build_va_arg_1 (location_t loc, tree type, tree op)
5834{
5835 tree expr = build1 (VA_ARG_EXPR, type, op);
5836 SET_EXPR_LOCATION (expr, loc);
5837 return expr;
5838}
5839
5840/* Return a VA_ARG_EXPR corresponding to a source-level expression
5841 va_arg (EXPR, TYPE) at source location LOC. */
5842
a66c9326 5843tree
e60a6f7b 5844build_va_arg (location_t loc, tree expr, tree type)
a66c9326 5845{
c37be9ec 5846 tree va_type = TREE_TYPE (expr);
5847 tree canon_va_type = (va_type == error_mark_node
ea2ec8f6 5848 ? error_mark_node
c37be9ec 5849 : targetm.canonical_va_list_type (va_type));
5850
2e474820 5851 if (va_type == error_mark_node
5852 || canon_va_type == NULL_TREE)
c37be9ec 5853 {
ea2ec8f6 5854 if (canon_va_type == NULL_TREE)
5855 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
5856
2e474820 5857 /* Let's handle things neutrallly, if expr:
5858 - has undeclared type, or
5859 - is not an va_list type. */
ea2ec8f6 5860 return build_va_arg_1 (loc, type, error_mark_node);
c37be9ec 5861 }
f7fec1c7 5862
2e474820 5863 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
5864 {
5865 /* Case 1: Not an array type. */
5866
5867 /* Take the address, to get '&ap'. */
5868 mark_addressable (expr);
5869 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
5870
5871 /* Verify that &ap is still recognized as having va_list type. */
5872 tree canon_expr_type
5873 = targetm.canonical_va_list_type (TREE_TYPE (expr));
8ff61e58 5874 if (canon_expr_type == NULL_TREE)
5875 {
5876 error_at (loc,
5877 "first argument to %<va_arg%> not of type %<va_list%>");
5878 return error_mark_node;
5879 }
2e474820 5880
5881 return build_va_arg_1 (loc, type, expr);
5882 }
5883
5884 /* Case 2: Array type.
5885
5886 Background:
5887
5888 For contrast, let's start with the simple case (case 1). If
5889 canon_va_type is not an array type, but say a char *, then when
5890 passing-by-value a va_list, the type of the va_list param decl is
5891 the same as for another va_list decl (all ap's are char *):
5892
5893 f2_1 (char * ap)
5894 D.1815 = VA_ARG (&ap, 0B, 1);
5895 return D.1815;
5896
5897 f2 (int i)
5898 char * ap.0;
5899 char * ap;
5900 __builtin_va_start (&ap, 0);
5901 ap.0 = ap;
5902 res = f2_1 (ap.0);
5903 __builtin_va_end (&ap);
5904 D.1812 = res;
5905 return D.1812;
5906
5907 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
5908 va_list the type of the va_list param decl (case 2b, struct * ap) is not
5909 the same as for another va_list decl (case 2a, struct ap[1]).
5910
5911 f2_1 (struct * ap)
5912 D.1844 = VA_ARG (ap, 0B, 0);
5913 return D.1844;
5914
5915 f2 (int i)
5916 struct ap[1];
5917 __builtin_va_start (&ap, 0);
5918 res = f2_1 (&ap);
5919 __builtin_va_end (&ap);
5920 D.1841 = res;
5921 return D.1841;
5922
5923 Case 2b is different because:
5924 - on the callee side, the parm decl has declared type va_list, but
5925 grokdeclarator changes the type of the parm decl to a pointer to the
5926 array elem type.
5927 - on the caller side, the pass-by-value uses &ap.
5928
5929 We unify these two cases (case 2a: va_list is array type,
5930 case 2b: va_list is pointer to array elem type), by adding '&' for the
5931 array type case, such that we have a pointer to array elem in both
5932 cases. */
5933
5934 if (TREE_CODE (va_type) == ARRAY_TYPE)
5935 {
5936 /* Case 2a: va_list is array type. */
5937
5938 /* Take the address, to get '&ap'. Make sure it's a pointer to array
5939 elem type. */
5940 mark_addressable (expr);
5941 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
5942 expr);
5943
5944 /* Verify that &ap is still recognized as having va_list type. */
5945 tree canon_expr_type
5946 = targetm.canonical_va_list_type (TREE_TYPE (expr));
8ff61e58 5947 if (canon_expr_type == NULL_TREE)
5948 {
5949 error_at (loc,
5950 "first argument to %<va_arg%> not of type %<va_list%>");
5951 return error_mark_node;
5952 }
2e474820 5953 }
5954 else
5955 {
5956 /* Case 2b: va_list is pointer to array elem type. */
5957 gcc_assert (POINTER_TYPE_P (va_type));
839e4d28 5958
5959 /* Comparison as in std_canonical_va_list_type. */
5960 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (va_type))
5961 == TYPE_MAIN_VARIANT (TREE_TYPE (canon_va_type)));
2e474820 5962
5963 /* Don't take the address. We've already got '&ap'. */
5964 ;
5965 }
5966
5967 return build_va_arg_1 (loc, type, expr);
a66c9326 5968}
0d4238dc 5969
5970
dd878098 5971/* Linked list of disabled built-in functions. */
5972
6dc50383 5973struct disabled_builtin
dd878098 5974{
5975 const char *name;
5976 struct disabled_builtin *next;
6dc50383 5977};
dd878098 5978static disabled_builtin *disabled_builtins = NULL;
5979
1cae46be 5980static bool builtin_function_disabled_p (const char *);
dd878098 5981
5982/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5983 begins with "__builtin_", give an error. */
5984
5985void
1cae46be 5986disable_builtin_function (const char *name)
dd878098 5987{
5988 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
b0b1af64 5989 error ("cannot disable built-in function %qs", name);
dd878098 5990 else
5991 {
e85905e5 5992 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5993 new_disabled_builtin->name = name;
5994 new_disabled_builtin->next = disabled_builtins;
5995 disabled_builtins = new_disabled_builtin;
dd878098 5996 }
5997}
5998
5999
6000/* Return true if the built-in function NAME has been disabled, false
6001 otherwise. */
6002
6003static bool
1cae46be 6004builtin_function_disabled_p (const char *name)
dd878098 6005{
6006 disabled_builtin *p;
6007 for (p = disabled_builtins; p != NULL; p = p->next)
6008 {
6009 if (strcmp (name, p->name) == 0)
6010 return true;
6011 }
6012 return false;
6013}
6014
6015
3237155d 6016/* Worker for DEF_BUILTIN.
6017 Possibly define a builtin function with one or two names.
6018 Does not declare a non-__builtin_ function if flag_no_builtin, or if
6019 nonansi_p and flag_no_nonansi_builtin. */
0d4238dc 6020
3237155d 6021static void
6022def_builtin_1 (enum built_in_function fncode,
6023 const char *name,
6024 enum built_in_class fnclass,
6025 tree fntype, tree libtype,
6026 bool both_p, bool fallback_p, bool nonansi_p,
6027 tree fnattrs, bool implicit_p)
0d4238dc 6028{
3237155d 6029 tree decl;
6030 const char *libname;
6031
27213ba3 6032 if (fntype == error_mark_node)
6033 return;
6034
3237155d 6035 gcc_assert ((!both_p && !fallback_p)
6036 || !strncmp (name, "__builtin_",
6037 strlen ("__builtin_")));
6038
6039 libname = name + strlen ("__builtin_");
54be5d7e 6040 decl = add_builtin_function (name, fntype, fncode, fnclass,
6041 (fallback_p ? libname : NULL),
6042 fnattrs);
b9a16870 6043
6044 set_builtin_decl (fncode, decl, implicit_p);
6045
3237155d 6046 if (both_p
6047 && !flag_no_builtin && !builtin_function_disabled_p (libname)
dd878098 6048 && !(nonansi_p && flag_no_nonansi_builtin))
54be5d7e 6049 add_builtin_function (libname, libtype, fncode, fnclass,
6050 NULL, fnattrs);
0d4238dc 6051}
e94026da 6052\f
d7aeef06 6053/* Nonzero if the type T promotes to int. This is (nearly) the
6054 integral promotions defined in ISO C99 6.3.1.1/2. */
6055
6056bool
9f627b1a 6057c_promoting_integer_type_p (const_tree t)
d7aeef06 6058{
6059 switch (TREE_CODE (t))
6060 {
6061 case INTEGER_TYPE:
6062 return (TYPE_MAIN_VARIANT (t) == char_type_node
6063 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
6064 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
6065 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
7aa1e6eb 6066 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
6067 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d7aeef06 6068
6069 case ENUMERAL_TYPE:
6070 /* ??? Technically all enumerations not larger than an int
6071 promote to an int. But this is used along code paths
6072 that only want to notice a size change. */
6073 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6074
6075 case BOOLEAN_TYPE:
6076 return 1;
6077
6078 default:
6079 return 0;
6080 }
6081}
6082
e94026da 6083/* Return 1 if PARMS specifies a fixed number of parameters
6084 and none of their types is affected by default promotions. */
6085
6086int
9f627b1a 6087self_promoting_args_p (const_tree parms)
e94026da 6088{
9f627b1a 6089 const_tree t;
e94026da 6090 for (t = parms; t; t = TREE_CHAIN (t))
6091 {
19cb6b50 6092 tree type = TREE_VALUE (t);
43f74bc4 6093
e1d8e198 6094 if (type == error_mark_node)
6095 continue;
6096
e94026da 6097 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6098 return 0;
6099
6100 if (type == 0)
6101 return 0;
6102
6103 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6104 return 0;
6105
d7aeef06 6106 if (c_promoting_integer_type_p (type))
e94026da 6107 return 0;
6108 }
6109 return 1;
6110}
605fb01e 6111
c10de5e7 6112/* Recursively remove any '*' or '&' operator from TYPE. */
6113tree
6114strip_pointer_operator (tree t)
6115{
6116 while (POINTER_TYPE_P (t))
6117 t = TREE_TYPE (t);
6118 return t;
6119}
6120
57a0ed23 6121/* Recursively remove pointer or array type from TYPE. */
6122tree
6123strip_pointer_or_array_types (tree t)
6124{
6125 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6126 t = TREE_TYPE (t);
6127 return t;
6128}
6129
e41f0d80 6130/* Used to compare case labels. K1 and K2 are actually tree nodes
6131 representing case labels, or NULL_TREE for a `default' label.
6132 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6133 K2, and 0 if K1 and K2 are equal. */
6134
6135int
1cae46be 6136case_compare (splay_tree_key k1, splay_tree_key k2)
e41f0d80 6137{
6138 /* Consider a NULL key (such as arises with a `default' label) to be
6139 smaller than anything else. */
6140 if (!k1)
6141 return k2 ? -1 : 0;
6142 else if (!k2)
6143 return k1 ? 1 : 0;
6144
6145 return tree_int_cst_compare ((tree) k1, (tree) k2);
6146}
6147
e60a6f7b 6148/* Process a case label, located at LOC, for the range LOW_VALUE
6149 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6150 then this case label is actually a `default' label. If only
6151 HIGH_VALUE is NULL_TREE, then case label was declared using the
6152 usual C/C++ syntax, rather than the GNU case range extension.
6153 CASES is a tree containing all the case ranges processed so far;
be23b16f 6154 COND is the condition for the switch-statement itself.
6155 OUTSIDE_RANGE_P says whether there was a case value that doesn't
6156 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
6157 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
e41f0d80 6158
6159tree
e60a6f7b 6160c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
be23b16f 6161 tree low_value, tree high_value, bool *outside_range_p)
e41f0d80 6162{
6163 tree type;
6164 tree label;
6165 tree case_label;
6166 splay_tree_node node;
6167
6168 /* Create the LABEL_DECL itself. */
e60a6f7b 6169 label = create_artificial_label (loc);
e41f0d80 6170
6171 /* If there was an error processing the switch condition, bail now
6172 before we get more confused. */
6173 if (!cond || cond == error_mark_node)
4ee9c684 6174 goto error_out;
e41f0d80 6175
1cae46be 6176 if ((low_value && TREE_TYPE (low_value)
6177 && POINTER_TYPE_P (TREE_TYPE (low_value)))
e41f0d80 6178 || (high_value && TREE_TYPE (high_value)
6179 && POINTER_TYPE_P (TREE_TYPE (high_value))))
b96dc121 6180 {
e60a6f7b 6181 error_at (loc, "pointers are not permitted as case values");
b96dc121 6182 goto error_out;
6183 }
e41f0d80 6184
6185 /* Case ranges are a GNU extension. */
8864917d 6186 if (high_value)
29438999 6187 pedwarn (loc, OPT_Wpedantic,
8864917d 6188 "range expressions in switch statements are non-standard");
e41f0d80 6189
6190 type = TREE_TYPE (cond);
6191 if (low_value)
6192 {
2d2f6a15 6193 low_value = check_case_value (loc, low_value);
22a75734 6194 low_value = convert_and_check (loc, type, low_value);
96722196 6195 if (low_value == error_mark_node)
6196 goto error_out;
e41f0d80 6197 }
6198 if (high_value)
6199 {
2d2f6a15 6200 high_value = check_case_value (loc, high_value);
22a75734 6201 high_value = convert_and_check (loc, type, high_value);
96722196 6202 if (high_value == error_mark_node)
6203 goto error_out;
e41f0d80 6204 }
6205
96722196 6206 if (low_value && high_value)
6207 {
6208 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
a0c938f0 6209 really a case range, even though it was written that way.
6210 Remove the HIGH_VALUE to simplify later processing. */
96722196 6211 if (tree_int_cst_equal (low_value, high_value))
6212 high_value = NULL_TREE;
6213 else if (!tree_int_cst_lt (low_value, high_value))
e60a6f7b 6214 warning_at (loc, 0, "empty range specified");
96722196 6215 }
e41f0d80 6216
2ca392fd 6217 /* See if the case is in range of the type of the original testing
6218 expression. If both low_value and high_value are out of range,
6219 don't insert the case label and return NULL_TREE. */
6220 if (low_value
f61a9bc2 6221 && !check_case_bounds (loc, type, orig_type,
be23b16f 6222 &low_value, high_value ? &high_value : NULL,
6223 outside_range_p))
2ca392fd 6224 return NULL_TREE;
6225
e41f0d80 6226 /* Look up the LOW_VALUE in the table of case labels we already
6227 have. */
6228 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6229 /* If there was not an exact match, check for overlapping ranges.
6230 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6231 that's a `default' label and the only overlap is an exact match. */
6232 if (!node && (low_value || high_value))
6233 {
6234 splay_tree_node low_bound;
6235 splay_tree_node high_bound;
6236
6237 /* Even though there wasn't an exact match, there might be an
6238 overlap between this case range and another case range.
6239 Since we've (inductively) not allowed any overlapping case
6240 ranges, we simply need to find the greatest low case label
6241 that is smaller that LOW_VALUE, and the smallest low case
6242 label that is greater than LOW_VALUE. If there is an overlap
6243 it will occur in one of these two ranges. */
6244 low_bound = splay_tree_predecessor (cases,
6245 (splay_tree_key) low_value);
6246 high_bound = splay_tree_successor (cases,
6247 (splay_tree_key) low_value);
6248
6249 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6250 the LOW_VALUE, so there is no need to check unless the
6251 LOW_BOUND is in fact itself a case range. */
6252 if (low_bound
6253 && CASE_HIGH ((tree) low_bound->value)
6254 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6255 low_value) >= 0)
6256 node = low_bound;
6257 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6258 range is bigger than the low end of the current range, so we
6259 are only interested if the current range is a real range, and
6260 not an ordinary case label. */
1cae46be 6261 else if (high_bound
e41f0d80 6262 && high_value
6263 && (tree_int_cst_compare ((tree) high_bound->key,
6264 high_value)
6265 <= 0))
6266 node = high_bound;
6267 }
6268 /* If there was an overlap, issue an error. */
6269 if (node)
6270 {
eaae3b75 6271 tree duplicate = CASE_LABEL ((tree) node->value);
e41f0d80 6272
6273 if (high_value)
6274 {
e60a6f7b 6275 error_at (loc, "duplicate (or overlapping) case value");
6276 error_at (DECL_SOURCE_LOCATION (duplicate),
6277 "this is the first entry overlapping that value");
e41f0d80 6278 }
6279 else if (low_value)
6280 {
e60a6f7b 6281 error_at (loc, "duplicate case value") ;
6282 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
e41f0d80 6283 }
6284 else
6285 {
e60a6f7b 6286 error_at (loc, "multiple default labels in one switch");
6287 error_at (DECL_SOURCE_LOCATION (duplicate),
6288 "this is the first default label");
e41f0d80 6289 }
4ee9c684 6290 goto error_out;
e41f0d80 6291 }
6292
6293 /* Add a CASE_LABEL to the statement-tree. */
b6e3dd65 6294 case_label = add_stmt (build_case_label (low_value, high_value, label));
e41f0d80 6295 /* Register this case label in the splay tree. */
1cae46be 6296 splay_tree_insert (cases,
e41f0d80 6297 (splay_tree_key) low_value,
6298 (splay_tree_value) case_label);
6299
6300 return case_label;
4ee9c684 6301
6302 error_out:
daf6dff5 6303 /* Add a label so that the back-end doesn't think that the beginning of
4ee9c684 6304 the switch is unreachable. Note that we do not add a case label, as
a53ff4c1 6305 that just leads to duplicates and thence to failure later on. */
4ee9c684 6306 if (!cases->root)
6307 {
e60a6f7b 6308 tree t = create_artificial_label (loc);
6309 add_stmt (build_stmt (loc, LABEL_EXPR, t));
4ee9c684 6310 }
6311 return error_mark_node;
6312}
6313
6314/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6315 Used to verify that case values match up with enumerator values. */
6316
6317static void
6318match_case_to_enum_1 (tree key, tree type, tree label)
6319{
e913b5cd 6320 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6321
dd76621f 6322 if (tree_fits_uhwi_p (key))
6323 print_dec (key, buf, UNSIGNED);
6324 else if (tree_fits_shwi_p (key))
6325 print_dec (key, buf, SIGNED);
4ee9c684 6326 else
e913b5cd 6327 print_hex (key, buf);
4ee9c684 6328
6329 if (TYPE_NAME (type) == 0)
712d2297 6330 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6331 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6332 "case value %qs not in enumerated type",
6333 buf);
4ee9c684 6334 else
712d2297 6335 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6336 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6337 "case value %qs not in enumerated type %qT",
6338 buf, type);
4ee9c684 6339}
6340
359d87c6 6341/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6342 Used to verify that case values match up with enumerator values. */
6343
4ee9c684 6344static int
6345match_case_to_enum (splay_tree_node node, void *data)
6346{
6347 tree label = (tree) node->value;
4fd61bc6 6348 tree type = (tree) data;
4ee9c684 6349
6350 /* Skip default case. */
6351 if (!CASE_LOW (label))
6352 return 0;
6353
359d87c6 6354 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
4ee9c684 6355 when we did our enum->case scan. Reset our scratch bit after. */
359d87c6 6356 if (!CASE_LOW_SEEN (label))
4ee9c684 6357 match_case_to_enum_1 (CASE_LOW (label), type, label);
6358 else
359d87c6 6359 CASE_LOW_SEEN (label) = 0;
4ee9c684 6360
359d87c6 6361 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6362 not set, that means that CASE_HIGH did not appear when we did our
6363 enum->case scan. Reset our scratch bit after. */
4ee9c684 6364 if (CASE_HIGH (label))
6365 {
359d87c6 6366 if (!CASE_HIGH_SEEN (label))
6367 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6368 else
6369 CASE_HIGH_SEEN (label) = 0;
4ee9c684 6370 }
6371
6372 return 0;
6373}
6374
e7911019 6375/* Handle -Wswitch*. Called from the front end after parsing the
6376 switch construct. */
6377/* ??? Should probably be somewhere generic, since other languages
6378 besides C and C++ would want this. At the moment, however, C/C++
6379 are the only tree-ssa languages that support enumerations at all,
6380 so the point is moot. */
4ee9c684 6381
e7911019 6382void
6383c_do_switch_warnings (splay_tree cases, location_t switch_location,
be23b16f 6384 tree type, tree cond, bool bool_cond_p,
6385 bool outside_range_p)
4ee9c684 6386{
b27ac6b5 6387 splay_tree_node default_node;
359d87c6 6388 splay_tree_node node;
6389 tree chain;
4ee9c684 6390
be23b16f 6391 if (!warn_switch && !warn_switch_enum && !warn_switch_default
6392 && !warn_switch_bool)
4ee9c684 6393 return;
6394
4ee9c684 6395 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
8b6866af 6396 if (!default_node)
5fb6a912 6397 warning_at (switch_location, OPT_Wswitch_default,
6398 "switch missing default case");
4ee9c684 6399
be23b16f 6400 /* There are certain cases where -Wswitch-bool warnings aren't
6401 desirable, such as
6402 switch (boolean)
6403 {
6404 case true: ...
6405 case false: ...
6406 }
6407 so be careful here. */
6408 if (warn_switch_bool && bool_cond_p)
6409 {
6410 splay_tree_node min_node;
6411 /* If there's a default node, it's also the value with the minimal
6412 key. So look at the penultimate key (if any). */
6413 if (default_node)
6414 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
6415 else
6416 min_node = splay_tree_min (cases);
6417 tree min = min_node ? (tree) min_node->key : NULL_TREE;
6418
6419 splay_tree_node max_node = splay_tree_max (cases);
6420 /* This might be a case range, so look at the value with the
6421 maximal key and then check CASE_HIGH. */
6422 tree max = max_node ? (tree) max_node->value : NULL_TREE;
6423 if (max)
6424 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
6425
6426 /* If there's a case value > 1 or < 0, that is outside bool
6427 range, warn. */
6428 if (outside_range_p
6429 || (max && wi::gts_p (max, 1))
6430 || (min && wi::lts_p (min, 0))
6431 /* And handle the
6432 switch (boolean)
6433 {
6434 case true: ...
6435 case false: ...
6436 default: ...
6437 }
6438 case, where we want to warn. */
6439 || (default_node
6440 && max && wi::eq_p (max, 1)
6441 && min && wi::eq_p (min, 0)))
6442 warning_at (switch_location, OPT_Wswitch_bool,
6443 "switch condition has boolean value");
6444 }
6445
47ae02b7 6446 /* From here on, we only care about enumerated types. */
359d87c6 6447 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6448 return;
6449
561017b5 6450 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6451 if (!warn_switch_enum && !warn_switch)
359d87c6 6452 return;
6453
561017b5 6454 /* Check the cases. Warn about case values which are not members of
6455 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6456 there is no default case, check that exactly all enumeration
6457 literals are covered by the cases. */
6458
359d87c6 6459 /* Clearing COND if it is not an integer constant simplifies
6460 the tests inside the loop below. */
6461 if (TREE_CODE (cond) != INTEGER_CST)
6462 cond = NULL_TREE;
6463
6464 /* The time complexity here is O(N*lg(N)) worst case, but for the
6465 common case of monotonically increasing enumerators, it is
6466 O(N), since the nature of the splay tree will keep the next
6467 element adjacent to the root at all times. */
4ee9c684 6468
359d87c6 6469 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6470 {
6471 tree value = TREE_VALUE (chain);
3f00a6c0 6472 if (TREE_CODE (value) == CONST_DECL)
6473 value = DECL_INITIAL (value);
359d87c6 6474 node = splay_tree_lookup (cases, (splay_tree_key) value);
6475 if (node)
4ee9c684 6476 {
359d87c6 6477 /* Mark the CASE_LOW part of the case entry as seen. */
6478 tree label = (tree) node->value;
6479 CASE_LOW_SEEN (label) = 1;
6480 continue;
6481 }
6482
6483 /* Even though there wasn't an exact match, there might be a
f0b5f617 6484 case range which includes the enumerator's value. */
359d87c6 6485 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6486 if (node && CASE_HIGH ((tree) node->value))
6487 {
6488 tree label = (tree) node->value;
6489 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6490 if (cmp >= 0)
4ee9c684 6491 {
359d87c6 6492 /* If we match the upper bound exactly, mark the CASE_HIGH
6493 part of the case entry as seen. */
6494 if (cmp == 0)
6495 CASE_HIGH_SEEN (label) = 1;
6496 continue;
4ee9c684 6497 }
6498 }
6499
359d87c6 6500 /* We've now determined that this enumerated literal isn't
6501 handled by the case labels of the switch statement. */
4ee9c684 6502
359d87c6 6503 /* If the switch expression is a constant, we only really care
6504 about whether that constant is handled by the switch. */
6505 if (cond && tree_int_cst_compare (cond, value))
6506 continue;
4ee9c684 6507
6cbbbc89 6508 /* If there is a default_node, the only relevant option is
561017b5 6509 Wswitch-enum. Otherwise, if both are enabled then we prefer
6cbbbc89 6510 to warn using -Wswitch because -Wswitch is enabled by -Wall
6511 while -Wswitch-enum is explicit. */
561017b5 6512 warning_at (switch_location,
6513 (default_node || !warn_switch
6514 ? OPT_Wswitch_enum
6515 : OPT_Wswitch),
6516 "enumeration value %qE not handled in switch",
6517 TREE_PURPOSE (chain));
4ee9c684 6518 }
359d87c6 6519
6520 /* Warn if there are case expressions that don't correspond to
6521 enumerators. This can occur since C and C++ don't enforce
6522 type-checking of assignments to enumeration variables.
6523
6524 The time complexity here is now always O(N) worst case, since
6525 we should have marked both the lower bound and upper bound of
6526 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6527 above. This scan also resets those fields. */
6cbbbc89 6528
359d87c6 6529 splay_tree_foreach (cases, match_case_to_enum, type);
e41f0d80 6530}
6531
9dd48740 6532/* Finish an expression taking the address of LABEL (an
dda49785 6533 IDENTIFIER_NODE). Returns an expression for the address.
6534
6535 LOC is the location for the expression returned. */
d0a47c8d 6536
1cae46be 6537tree
dda49785 6538finish_label_address_expr (tree label, location_t loc)
d0a47c8d 6539{
6540 tree result;
6541
29438999 6542 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
d0a47c8d 6543
9dd48740 6544 if (label == error_mark_node)
6545 return error_mark_node;
6546
d0a47c8d 6547 label = lookup_label (label);
6548 if (label == NULL_TREE)
6549 result = null_pointer_node;
6550 else
6551 {
6552 TREE_USED (label) = 1;
6553 result = build1 (ADDR_EXPR, ptr_type_node, label);
344cd9b2 6554 /* The current function is not necessarily uninlinable.
d0a47c8d 6555 Computed gotos are incompatible with inlining, but the value
6556 here could be used only in a diagnostic, for example. */
dda49785 6557 protected_set_expr_location (result, loc);
d0a47c8d 6558 }
6559
6560 return result;
6561}
4f9a1c9b 6562\f
6563
6564/* Given a boolean expression ARG, return a tree representing an increment
6565 or decrement (as indicated by CODE) of ARG. The front end must check for
6566 invalid cases (e.g., decrement in C++). */
6567tree
1cae46be 6568boolean_increment (enum tree_code code, tree arg)
4f9a1c9b 6569{
6570 tree val;
69db191c 6571 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
c0f19401 6572
4f9a1c9b 6573 arg = stabilize_reference (arg);
6574 switch (code)
6575 {
6576 case PREINCREMENT_EXPR:
14ae0310 6577 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6578 break;
6579 case POSTINCREMENT_EXPR:
14ae0310 6580 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6581 arg = save_expr (arg);
14ae0310 6582 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6583 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6584 break;
6585 case PREDECREMENT_EXPR:
14ae0310 6586 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6587 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6588 break;
6589 case POSTDECREMENT_EXPR:
14ae0310 6590 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6591 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6592 arg = save_expr (arg);
14ae0310 6593 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6594 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6595 break;
6596 default:
231bd014 6597 gcc_unreachable ();
4f9a1c9b 6598 }
6599 TREE_SIDE_EFFECTS (val) = 1;
6600 return val;
6601}
76a6e674 6602\f
f3449a3c 6603/* Built-in macros for stddef.h and stdint.h, that require macros
6604 defined in this file. */
79cf3ec1 6605void
1cae46be 6606c_stddef_cpp_builtins(void)
1ed9d5f5 6607{
63994318 6608 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6609 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6610 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6611 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
36bccbfc 6612 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6613 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
b0726616 6614 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6615 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
f3449a3c 6616 if (SIG_ATOMIC_TYPE)
6617 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6618 if (INT8_TYPE)
6619 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6620 if (INT16_TYPE)
6621 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6622 if (INT32_TYPE)
6623 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6624 if (INT64_TYPE)
6625 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6626 if (UINT8_TYPE)
6627 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6628 if (UINT16_TYPE)
6629 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6630 if (UINT32_TYPE)
6631 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6632 if (UINT64_TYPE)
6633 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6634 if (INT_LEAST8_TYPE)
6635 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6636 if (INT_LEAST16_TYPE)
6637 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6638 if (INT_LEAST32_TYPE)
6639 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6640 if (INT_LEAST64_TYPE)
6641 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6642 if (UINT_LEAST8_TYPE)
6643 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6644 if (UINT_LEAST16_TYPE)
6645 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6646 if (UINT_LEAST32_TYPE)
6647 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6648 if (UINT_LEAST64_TYPE)
6649 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6650 if (INT_FAST8_TYPE)
6651 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6652 if (INT_FAST16_TYPE)
6653 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6654 if (INT_FAST32_TYPE)
6655 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6656 if (INT_FAST64_TYPE)
6657 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6658 if (UINT_FAST8_TYPE)
6659 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6660 if (UINT_FAST16_TYPE)
6661 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6662 if (UINT_FAST32_TYPE)
6663 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6664 if (UINT_FAST64_TYPE)
6665 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6666 if (INTPTR_TYPE)
6667 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6668 if (UINTPTR_TYPE)
6669 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
574006c3 6670}
6671
7d3b509a 6672static void
1cae46be 6673c_init_attributes (void)
7d3b509a 6674{
6675 /* Fill in the built_in_attributes array. */
7c446c95 6676#define DEF_ATTR_NULL_TREE(ENUM) \
7d3b509a 6677 built_in_attributes[(int) ENUM] = NULL_TREE;
7c446c95 6678#define DEF_ATTR_INT(ENUM, VALUE) \
ceb7b692 6679 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
c8010b80 6680#define DEF_ATTR_STRING(ENUM, VALUE) \
6681 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
7d3b509a 6682#define DEF_ATTR_IDENT(ENUM, STRING) \
6683 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6684#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6685 built_in_attributes[(int) ENUM] \
6686 = tree_cons (built_in_attributes[(int) PURPOSE], \
6687 built_in_attributes[(int) VALUE], \
6688 built_in_attributes[(int) CHAIN]);
7d3b509a 6689#include "builtin-attrs.def"
6690#undef DEF_ATTR_NULL_TREE
6691#undef DEF_ATTR_INT
6692#undef DEF_ATTR_IDENT
6693#undef DEF_ATTR_TREE_LIST
76a6e674 6694}
5f3cead1 6695
33199a81 6696/* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6697 identifier as an argument, so the front end shouldn't look it up. */
6698
6699bool
47b19b94 6700attribute_takes_identifier_p (const_tree attr_id)
33199a81 6701{
9bf1c74e 6702 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
02cb1060 6703 if (spec == NULL)
6704 /* Unknown attribute that we'll end up ignoring, return true so we
6705 don't complain about an identifier argument. */
6706 return true;
6707 else if (!strcmp ("mode", spec->name)
6708 || !strcmp ("format", spec->name)
6709 || !strcmp ("cleanup", spec->name))
47b19b94 6710 return true;
6711 else
6712 return targetm.attribute_takes_identifier_p (attr_id);
33199a81 6713}
6714
f8e93a2e 6715/* Attribute handlers common to C front ends. */
6716
6717/* Handle a "packed" attribute; arguments as in
6718 struct attribute_spec.handler. */
6719
6720static tree
9a03a746 6721handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
09347743 6722 int flags, bool *no_add_attrs)
f8e93a2e 6723{
f40175cb 6724 if (TYPE_P (*node))
f8e93a2e 6725 {
6726 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 6727 *node = build_variant_type_copy (*node);
f40175cb 6728 TYPE_PACKED (*node) = 1;
f8e93a2e 6729 }
6730 else if (TREE_CODE (*node) == FIELD_DECL)
c2ab04f9 6731 {
9fd767c5 6732 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6733 /* Still pack bitfields. */
6734 && ! DECL_INITIAL (*node))
c2ab04f9 6735 warning (OPT_Wattributes,
6736 "%qE attribute ignored for field of type %qT",
6737 name, TREE_TYPE (*node));
6738 else
6739 DECL_PACKED (*node) = 1;
6740 }
f8e93a2e 6741 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
f40175cb 6742 used for DECL_REGISTER. It wouldn't mean anything anyway.
6743 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6744 that changes what the typedef is typing. */
f8e93a2e 6745 else
6746 {
9b2d6d13 6747 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6748 *no_add_attrs = true;
6749 }
6750
6751 return NULL_TREE;
6752}
6753
6754/* Handle a "nocommon" attribute; arguments as in
6755 struct attribute_spec.handler. */
6756
6757static tree
1cae46be 6758handle_nocommon_attribute (tree *node, tree name,
9a03a746 6759 tree ARG_UNUSED (args),
6760 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6761{
f48c7f4a 6762 if (VAR_P (*node))
f8e93a2e 6763 DECL_COMMON (*node) = 0;
6764 else
6765 {
9b2d6d13 6766 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6767 *no_add_attrs = true;
6768 }
6769
6770 return NULL_TREE;
6771}
6772
6773/* Handle a "common" attribute; arguments as in
6774 struct attribute_spec.handler. */
6775
6776static tree
9a03a746 6777handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6778 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6779{
f48c7f4a 6780 if (VAR_P (*node))
f8e93a2e 6781 DECL_COMMON (*node) = 1;
6782 else
6783 {
9b2d6d13 6784 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6785 *no_add_attrs = true;
6786 }
6787
6788 return NULL_TREE;
6789}
6790
6791/* Handle a "noreturn" attribute; arguments as in
6792 struct attribute_spec.handler. */
6793
6794static tree
9a03a746 6795handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6796 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6797{
6798 tree type = TREE_TYPE (*node);
6799
6800 /* See FIXME comment in c_common_attribute_table. */
8c582e4f 6801 if (TREE_CODE (*node) == FUNCTION_DECL
6802 || objc_method_decl (TREE_CODE (*node)))
f8e93a2e 6803 TREE_THIS_VOLATILE (*node) = 1;
6804 else if (TREE_CODE (type) == POINTER_TYPE
6805 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6806 TREE_TYPE (*node)
9d4eeb52 6807 = (build_qualified_type
6808 (build_pointer_type
6809 (build_type_variant (TREE_TYPE (type),
6810 TYPE_READONLY (TREE_TYPE (type)), 1)),
6811 TYPE_QUALS (type)));
f8e93a2e 6812 else
6813 {
9b2d6d13 6814 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6815 *no_add_attrs = true;
6816 }
6817
6818 return NULL_TREE;
6819}
6820
5de92639 6821/* Handle a "hot" and attribute; arguments as in
6822 struct attribute_spec.handler. */
6823
6824static tree
6825handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
46f8e3b0 6826 int ARG_UNUSED (flags), bool *no_add_attrs)
5de92639 6827{
758a38ab 6828 if (TREE_CODE (*node) == FUNCTION_DECL
6829 || TREE_CODE (*node) == LABEL_DECL)
5de92639 6830 {
6831 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6832 {
4a026b48 6833 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6834 "with attribute %qs", name, "cold");
5de92639 6835 *no_add_attrs = true;
6836 }
24470055 6837 /* Most of the rest of the hot processing is done later with
6838 lookup_attribute. */
5de92639 6839 }
6840 else
6841 {
6842 warning (OPT_Wattributes, "%qE attribute ignored", name);
6843 *no_add_attrs = true;
6844 }
6845
6846 return NULL_TREE;
6847}
758a38ab 6848
5de92639 6849/* Handle a "cold" and attribute; arguments as in
6850 struct attribute_spec.handler. */
6851
6852static tree
6853handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6854 int ARG_UNUSED (flags), bool *no_add_attrs)
6855{
758a38ab 6856 if (TREE_CODE (*node) == FUNCTION_DECL
6857 || TREE_CODE (*node) == LABEL_DECL)
5de92639 6858 {
6859 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6860 {
4a026b48 6861 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6862 "with attribute %qs", name, "hot");
5de92639 6863 *no_add_attrs = true;
6864 }
24470055 6865 /* Most of the rest of the cold processing is done later with
6866 lookup_attribute. */
5de92639 6867 }
6868 else
6869 {
6870 warning (OPT_Wattributes, "%qE attribute ignored", name);
6871 *no_add_attrs = true;
6872 }
6873
6874 return NULL_TREE;
6875}
6876
a9196da9 6877/* Handle a "no_sanitize_address" attribute; arguments as in
d413ffdd 6878 struct attribute_spec.handler. */
6879
6880static tree
a9196da9 6881handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6882 bool *no_add_attrs)
d413ffdd 6883{
6884 if (TREE_CODE (*node) != FUNCTION_DECL)
6885 {
6886 warning (OPT_Wattributes, "%qE attribute ignored", name);
6887 *no_add_attrs = true;
6888 }
6889
6890 return NULL_TREE;
6891}
6892
a9196da9 6893/* Handle a "no_address_safety_analysis" attribute; arguments as in
6894 struct attribute_spec.handler. */
6895
6896static tree
6897handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6898 bool *no_add_attrs)
6899{
6900 if (TREE_CODE (*node) != FUNCTION_DECL)
6901 warning (OPT_Wattributes, "%qE attribute ignored", name);
6902 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6903 DECL_ATTRIBUTES (*node)
6904 = tree_cons (get_identifier ("no_sanitize_address"),
6905 NULL_TREE, DECL_ATTRIBUTES (*node));
6906 *no_add_attrs = true;
6907 return NULL_TREE;
6908}
6909
05f893e1 6910/* Handle a "no_sanitize_undefined" attribute; arguments as in
6911 struct attribute_spec.handler. */
6912
6913static tree
6914handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6915 bool *no_add_attrs)
6916{
6917 if (TREE_CODE (*node) != FUNCTION_DECL)
6918 {
6919 warning (OPT_Wattributes, "%qE attribute ignored", name);
6920 *no_add_attrs = true;
6921 }
6922
6923 return NULL_TREE;
6924}
6925
947aa916 6926/* Handle a "stack_protect" attribute; arguments as in
6927 struct attribute_spec.handler. */
6928static tree
6929handle_stack_protect_attribute (tree *node, tree name, tree, int,
6930 bool *no_add_attrs)
6931{
6932 if (TREE_CODE (*node) != FUNCTION_DECL)
6933 {
6934 warning (OPT_Wattributes, "%qE attribute ignored", name);
6935 *no_add_attrs = true;
6936 }
6937 else
6938 DECL_ATTRIBUTES (*node)
6939 = tree_cons (get_identifier ("stack_protect"),
6940 NULL_TREE, DECL_ATTRIBUTES (*node));
6941
6942 return NULL_TREE;
6943}
6944
f8e93a2e 6945/* Handle a "noinline" attribute; arguments as in
6946 struct attribute_spec.handler. */
6947
6948static tree
1cae46be 6949handle_noinline_attribute (tree *node, tree name,
9a03a746 6950 tree ARG_UNUSED (args),
6951 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6952{
6953 if (TREE_CODE (*node) == FUNCTION_DECL)
4a026b48 6954 {
6955 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6956 {
6957 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6958 "with attribute %qs", name, "always_inline");
6959 *no_add_attrs = true;
6960 }
6961 else
6962 DECL_UNINLINABLE (*node) = 1;
6963 }
f8e93a2e 6964 else
6965 {
9b2d6d13 6966 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6967 *no_add_attrs = true;
6968 }
6969
6970 return NULL_TREE;
6971}
6972
bdb1f0d1 6973/* Handle a "noclone" attribute; arguments as in
6974 struct attribute_spec.handler. */
6975
6976static tree
6977handle_noclone_attribute (tree *node, tree name,
6978 tree ARG_UNUSED (args),
6979 int ARG_UNUSED (flags), bool *no_add_attrs)
6980{
6981 if (TREE_CODE (*node) != FUNCTION_DECL)
6982 {
6983 warning (OPT_Wattributes, "%qE attribute ignored", name);
6984 *no_add_attrs = true;
6985 }
6986
6987 return NULL_TREE;
6988}
6989
85fbea97 6990/* Handle a "no_icf" attribute; arguments as in
6991 struct attribute_spec.handler. */
6992
6993static tree
6994handle_noicf_attribute (tree *node, tree name,
6995 tree ARG_UNUSED (args),
6996 int ARG_UNUSED (flags), bool *no_add_attrs)
6997{
6998 if (TREE_CODE (*node) != FUNCTION_DECL)
6999 {
7000 warning (OPT_Wattributes, "%qE attribute ignored", name);
7001 *no_add_attrs = true;
7002 }
7003
7004 return NULL_TREE;
7005}
7006
7007
f8e93a2e 7008/* Handle a "always_inline" attribute; arguments as in
7009 struct attribute_spec.handler. */
7010
7011static tree
1cae46be 7012handle_always_inline_attribute (tree *node, tree name,
9a03a746 7013 tree ARG_UNUSED (args),
7014 int ARG_UNUSED (flags),
09347743 7015 bool *no_add_attrs)
f8e93a2e 7016{
7017 if (TREE_CODE (*node) == FUNCTION_DECL)
7018 {
4a026b48 7019 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
7020 {
7021 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7022 "with %qs attribute", name, "noinline");
7023 *no_add_attrs = true;
7024 }
ab50af2a 7025 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
7026 {
7027 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7028 "with %qs attribute", name, "target_clones");
7029 *no_add_attrs = true;
7030 }
4a026b48 7031 else
7032 /* Set the attribute and mark it for disregarding inline
7033 limits. */
7034 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
f8e93a2e 7035 }
7036 else
7037 {
9b2d6d13 7038 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7039 *no_add_attrs = true;
541e4101 7040 }
7041
7042 return NULL_TREE;
7043}
7044
7045/* Handle a "gnu_inline" attribute; arguments as in
7046 struct attribute_spec.handler. */
7047
7048static tree
7049handle_gnu_inline_attribute (tree *node, tree name,
7050 tree ARG_UNUSED (args),
7051 int ARG_UNUSED (flags),
7052 bool *no_add_attrs)
7053{
7054 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7055 {
7056 /* Do nothing else, just set the attribute. We'll get at
7057 it later with lookup_attribute. */
7058 }
7059 else
7060 {
7061 warning (OPT_Wattributes, "%qE attribute ignored", name);
7062 *no_add_attrs = true;
7bd95dfd 7063 }
7064
7065 return NULL_TREE;
7066}
7067
7068/* Handle a "leaf" attribute; arguments as in
7069 struct attribute_spec.handler. */
7070
7071static tree
7072handle_leaf_attribute (tree *node, tree name,
7073 tree ARG_UNUSED (args),
7074 int ARG_UNUSED (flags), bool *no_add_attrs)
7075{
7076 if (TREE_CODE (*node) != FUNCTION_DECL)
7077 {
7078 warning (OPT_Wattributes, "%qE attribute ignored", name);
7079 *no_add_attrs = true;
7080 }
7081 if (!TREE_PUBLIC (*node))
7082 {
7083 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
7084 *no_add_attrs = true;
f8e93a2e 7085 }
7086
7087 return NULL_TREE;
7088}
7089
1b16fc45 7090/* Handle an "artificial" attribute; arguments as in
7091 struct attribute_spec.handler. */
7092
7093static tree
7094handle_artificial_attribute (tree *node, tree name,
7095 tree ARG_UNUSED (args),
7096 int ARG_UNUSED (flags),
7097 bool *no_add_attrs)
7098{
7099 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7100 {
7101 /* Do nothing else, just set the attribute. We'll get at
7102 it later with lookup_attribute. */
7103 }
7104 else
7105 {
7106 warning (OPT_Wattributes, "%qE attribute ignored", name);
7107 *no_add_attrs = true;
7108 }
7109
7110 return NULL_TREE;
7111}
7112
0cdd9887 7113/* Handle a "flatten" attribute; arguments as in
7114 struct attribute_spec.handler. */
7115
7116static tree
7117handle_flatten_attribute (tree *node, tree name,
a0c938f0 7118 tree args ATTRIBUTE_UNUSED,
7119 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
0cdd9887 7120{
7121 if (TREE_CODE (*node) == FUNCTION_DECL)
7122 /* Do nothing else, just set the attribute. We'll get at
7123 it later with lookup_attribute. */
7124 ;
7125 else
7126 {
7127 warning (OPT_Wattributes, "%qE attribute ignored", name);
7128 *no_add_attrs = true;
7129 }
7130
7131 return NULL_TREE;
7132}
7133
10fc867f 7134/* Handle a "warning" or "error" attribute; arguments as in
7135 struct attribute_spec.handler. */
7136
7137static tree
7138handle_error_attribute (tree *node, tree name, tree args,
7139 int ARG_UNUSED (flags), bool *no_add_attrs)
7140{
7141 if (TREE_CODE (*node) == FUNCTION_DECL
df936998 7142 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
10fc867f 7143 /* Do nothing else, just set the attribute. We'll get at
7144 it later with lookup_attribute. */
7145 ;
7146 else
7147 {
7148 warning (OPT_Wattributes, "%qE attribute ignored", name);
7149 *no_add_attrs = true;
7150 }
7151
7152 return NULL_TREE;
7153}
0cdd9887 7154
f8e93a2e 7155/* Handle a "used" attribute; arguments as in
7156 struct attribute_spec.handler. */
7157
7158static tree
9a03a746 7159handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7160 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7161{
d0a31bd8 7162 tree node = *pnode;
7163
7164 if (TREE_CODE (node) == FUNCTION_DECL
f48c7f4a 7165 || (VAR_P (node) && TREE_STATIC (node))
a4e3ffad 7166 || (TREE_CODE (node) == TYPE_DECL))
f54ed8bc 7167 {
f54ed8bc 7168 TREE_USED (node) = 1;
9423c9b7 7169 DECL_PRESERVE_P (node) = 1;
f48c7f4a 7170 if (VAR_P (node))
abc6c64f 7171 DECL_READ_P (node) = 1;
f54ed8bc 7172 }
f8e93a2e 7173 else
7174 {
9b2d6d13 7175 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7176 *no_add_attrs = true;
7177 }
7178
7179 return NULL_TREE;
7180}
7181
7182/* Handle a "unused" attribute; arguments as in
7183 struct attribute_spec.handler. */
7184
f5d49c14 7185tree
9a03a746 7186handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7187 int flags, bool *no_add_attrs)
f8e93a2e 7188{
7189 if (DECL_P (*node))
7190 {
7191 tree decl = *node;
7192
7193 if (TREE_CODE (decl) == PARM_DECL
4e81b384 7194 || VAR_OR_FUNCTION_DECL_P (decl)
f8e93a2e 7195 || TREE_CODE (decl) == LABEL_DECL
f5d49c14 7196 || TREE_CODE (decl) == CONST_DECL
f8e93a2e 7197 || TREE_CODE (decl) == TYPE_DECL)
abc6c64f 7198 {
7199 TREE_USED (decl) = 1;
f48c7f4a 7200 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
abc6c64f 7201 DECL_READ_P (decl) = 1;
7202 }
f8e93a2e 7203 else
7204 {
9b2d6d13 7205 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7206 *no_add_attrs = true;
7207 }
7208 }
7209 else
7210 {
7211 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 7212 *node = build_variant_type_copy (*node);
f8e93a2e 7213 TREE_USED (*node) = 1;
7214 }
7215
7216 return NULL_TREE;
7217}
7218
62eec3b4 7219/* Handle a "externally_visible" attribute; arguments as in
7220 struct attribute_spec.handler. */
7221
7222static tree
7223handle_externally_visible_attribute (tree *pnode, tree name,
7224 tree ARG_UNUSED (args),
7225 int ARG_UNUSED (flags),
7226 bool *no_add_attrs)
7227{
7228 tree node = *pnode;
7229
b443c459 7230 if (VAR_OR_FUNCTION_DECL_P (node))
62eec3b4 7231 {
ba12ea31 7232 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7233 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7234 {
7235 warning (OPT_Wattributes,
7236 "%qE attribute have effect only on public objects", name);
7237 *no_add_attrs = true;
7238 }
62eec3b4 7239 }
62eec3b4 7240 else
7241 {
7242 warning (OPT_Wattributes, "%qE attribute ignored", name);
7243 *no_add_attrs = true;
7244 }
7245
7246 return NULL_TREE;
7247}
7248
6b722052 7249/* Handle the "no_reorder" attribute. Arguments as in
7250 struct attribute_spec.handler. */
7251
7252static tree
7253handle_no_reorder_attribute (tree *pnode,
7254 tree name,
7255 tree,
7256 int,
7257 bool *no_add_attrs)
7258{
7259 tree node = *pnode;
7260
b443c459 7261 if (!VAR_OR_FUNCTION_DECL_P (node)
6b722052 7262 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7263 {
7264 warning (OPT_Wattributes,
7265 "%qE attribute only affects top level objects",
7266 name);
7267 *no_add_attrs = true;
7268 }
7269
7270 return NULL_TREE;
7271}
7272
f8e93a2e 7273/* Handle a "const" attribute; arguments as in
7274 struct attribute_spec.handler. */
7275
7276static tree
9a03a746 7277handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7278 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7279{
7280 tree type = TREE_TYPE (*node);
7281
7282 /* See FIXME comment on noreturn in c_common_attribute_table. */
7283 if (TREE_CODE (*node) == FUNCTION_DECL)
7284 TREE_READONLY (*node) = 1;
7285 else if (TREE_CODE (type) == POINTER_TYPE
7286 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7287 TREE_TYPE (*node)
9d4eeb52 7288 = (build_qualified_type
7289 (build_pointer_type
7290 (build_type_variant (TREE_TYPE (type), 1,
7291 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7292 TYPE_QUALS (type)));
f8e93a2e 7293 else
7294 {
9b2d6d13 7295 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7296 *no_add_attrs = true;
7297 }
7298
7299 return NULL_TREE;
7300}
7301
292237f3 7302/* Handle a "scalar_storage_order" attribute; arguments as in
7303 struct attribute_spec.handler. */
7304
7305static tree
7306handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
7307 int flags, bool *no_add_attrs)
7308{
7309 tree id = TREE_VALUE (args);
7310 tree type;
7311
7312 if (TREE_CODE (*node) == TYPE_DECL
7313 && ! (flags & ATTR_FLAG_CXX11))
7314 node = &TREE_TYPE (*node);
7315 type = *node;
7316
7317 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
7318 {
7319 error ("scalar_storage_order is not supported because endianness "
7320 "is not uniform");
7321 return NULL_TREE;
7322 }
7323
7324 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
7325 {
7326 bool reverse = false;
7327
7328 if (TREE_CODE (id) == STRING_CST
7329 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
7330 reverse = !BYTES_BIG_ENDIAN;
7331 else if (TREE_CODE (id) == STRING_CST
7332 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
7333 reverse = BYTES_BIG_ENDIAN;
7334 else
7335 {
7336 error ("scalar_storage_order argument must be one of \"big-endian\""
7337 " or \"little-endian\"");
7338 return NULL_TREE;
7339 }
7340
7341 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7342 {
7343 if (reverse)
7344 /* A type variant isn't good enough, since we don't want a cast
7345 to such a type to be removed as a no-op. */
7346 *node = type = build_duplicate_type (type);
7347 }
7348
7349 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
7350 return NULL_TREE;
7351 }
7352
7353 warning (OPT_Wattributes, "%qE attribute ignored", name);
7354 *no_add_attrs = true;
7355 return NULL_TREE;
7356}
7357
f8e93a2e 7358/* Handle a "transparent_union" attribute; arguments as in
7359 struct attribute_spec.handler. */
7360
7361static tree
1cae46be 7362handle_transparent_union_attribute (tree *node, tree name,
9a03a746 7363 tree ARG_UNUSED (args), int flags,
09347743 7364 bool *no_add_attrs)
f8e93a2e 7365{
881eb642 7366 tree type;
03908818 7367
7368 *no_add_attrs = true;
f8e93a2e 7369
ffcdbf9c 7370 if (TREE_CODE (*node) == TYPE_DECL
7371 && ! (flags & ATTR_FLAG_CXX11))
881eb642 7372 node = &TREE_TYPE (*node);
7373 type = *node;
f8e93a2e 7374
03908818 7375 if (TREE_CODE (type) == UNION_TYPE)
f8e93a2e 7376 {
fca86134 7377 /* Make sure that the first field will work for a transparent union.
7378 If the type isn't complete yet, leave the check to the code in
7379 finish_struct. */
7380 if (TYPE_SIZE (type))
7381 {
7382 tree first = first_field (type);
7383 if (first == NULL_TREE
7384 || DECL_ARTIFICIAL (first)
7385 || TYPE_MODE (type) != DECL_MODE (first))
7386 goto ignored;
7387 }
7388
f8e93a2e 7389 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
03908818 7390 {
fca86134 7391 /* If the type isn't complete yet, setting the flag
7392 on a variant wouldn't ever be checked. */
7393 if (!TYPE_SIZE (type))
7394 goto ignored;
7395
7396 /* build_duplicate_type doesn't work for C++. */
7397 if (c_dialect_cxx ())
03908818 7398 goto ignored;
7399
292237f3 7400 /* A type variant isn't good enough, since we don't want a cast
7401 to such a type to be removed as a no-op. */
03908818 7402 *node = type = build_duplicate_type (type);
7403 }
7404
c25b3e3f 7405 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7406 TYPE_TRANSPARENT_AGGR (t) = 1;
03908818 7407 return NULL_TREE;
f8e93a2e 7408 }
7409
03908818 7410 ignored:
7411 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7412 return NULL_TREE;
7413}
7414
9af7fd5b 7415/* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7416 get the requested priority for a constructor or destructor,
7417 possibly issuing diagnostics for invalid or reserved
7418 priorities. */
7419
7420static priority_type
7421get_priority (tree args, bool is_destructor)
7422{
7423 HOST_WIDE_INT pri;
6c181a06 7424 tree arg;
9af7fd5b 7425
7426 if (!args)
7427 return DEFAULT_INIT_PRIORITY;
48e1416a 7428
28fbc04f 7429 if (!SUPPORTS_INIT_PRIORITY)
7430 {
7431 if (is_destructor)
7432 error ("destructor priorities are not supported");
7433 else
7434 error ("constructor priorities are not supported");
7435 return DEFAULT_INIT_PRIORITY;
7436 }
7437
6c181a06 7438 arg = TREE_VALUE (args);
253e1cae 7439 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7440 goto invalid;
7441 if (arg == error_mark_node)
7442 return DEFAULT_INIT_PRIORITY;
c28ddc97 7443 arg = default_conversion (arg);
e913b5cd 7444 if (!tree_fits_shwi_p (arg)
6c181a06 7445 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
9af7fd5b 7446 goto invalid;
7447
e913b5cd 7448 pri = tree_to_shwi (arg);
9af7fd5b 7449 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7450 goto invalid;
7451
7452 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7453 {
7454 if (is_destructor)
7455 warning (0,
7456 "destructor priorities from 0 to %d are reserved "
48e1416a 7457 "for the implementation",
9af7fd5b 7458 MAX_RESERVED_INIT_PRIORITY);
7459 else
7460 warning (0,
7461 "constructor priorities from 0 to %d are reserved "
48e1416a 7462 "for the implementation",
9af7fd5b 7463 MAX_RESERVED_INIT_PRIORITY);
7464 }
7465 return pri;
7466
7467 invalid:
7468 if (is_destructor)
7469 error ("destructor priorities must be integers from 0 to %d inclusive",
7470 MAX_INIT_PRIORITY);
7471 else
7472 error ("constructor priorities must be integers from 0 to %d inclusive",
7473 MAX_INIT_PRIORITY);
7474 return DEFAULT_INIT_PRIORITY;
7475}
7476
f8e93a2e 7477/* Handle a "constructor" attribute; arguments as in
7478 struct attribute_spec.handler. */
7479
7480static tree
9af7fd5b 7481handle_constructor_attribute (tree *node, tree name, tree args,
9a03a746 7482 int ARG_UNUSED (flags),
09347743 7483 bool *no_add_attrs)
f8e93a2e 7484{
7485 tree decl = *node;
7486 tree type = TREE_TYPE (decl);
7487
7488 if (TREE_CODE (decl) == FUNCTION_DECL
7489 && TREE_CODE (type) == FUNCTION_TYPE
7490 && decl_function_context (decl) == 0)
7491 {
9af7fd5b 7492 priority_type priority;
f8e93a2e 7493 DECL_STATIC_CONSTRUCTOR (decl) = 1;
9af7fd5b 7494 priority = get_priority (args, /*is_destructor=*/false);
7495 SET_DECL_INIT_PRIORITY (decl, priority);
f8e93a2e 7496 TREE_USED (decl) = 1;
7497 }
7498 else
7499 {
9b2d6d13 7500 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7501 *no_add_attrs = true;
7502 }
7503
7504 return NULL_TREE;
7505}
7506
7507/* Handle a "destructor" attribute; arguments as in
7508 struct attribute_spec.handler. */
7509
7510static tree
9af7fd5b 7511handle_destructor_attribute (tree *node, tree name, tree args,
9a03a746 7512 int ARG_UNUSED (flags),
09347743 7513 bool *no_add_attrs)
f8e93a2e 7514{
7515 tree decl = *node;
7516 tree type = TREE_TYPE (decl);
7517
7518 if (TREE_CODE (decl) == FUNCTION_DECL
7519 && TREE_CODE (type) == FUNCTION_TYPE
7520 && decl_function_context (decl) == 0)
7521 {
9af7fd5b 7522 priority_type priority;
f8e93a2e 7523 DECL_STATIC_DESTRUCTOR (decl) = 1;
9af7fd5b 7524 priority = get_priority (args, /*is_destructor=*/true);
7525 SET_DECL_FINI_PRIORITY (decl, priority);
f8e93a2e 7526 TREE_USED (decl) = 1;
7527 }
7528 else
7529 {
9b2d6d13 7530 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7531 *no_add_attrs = true;
7532 }
7533
7534 return NULL_TREE;
7535}
7536
1c58e3f1 7537/* Nonzero if the mode is a valid vector mode for this architecture.
7538 This returns nonzero even if there is no hardware support for the
7539 vector mode, but we can emulate with narrower modes. */
7540
7541static int
3754d046 7542vector_mode_valid_p (machine_mode mode)
1c58e3f1 7543{
7544 enum mode_class mclass = GET_MODE_CLASS (mode);
3754d046 7545 machine_mode innermode;
1c58e3f1 7546
7547 /* Doh! What's going on? */
7548 if (mclass != MODE_VECTOR_INT
7549 && mclass != MODE_VECTOR_FLOAT
7550 && mclass != MODE_VECTOR_FRACT
7551 && mclass != MODE_VECTOR_UFRACT
7552 && mclass != MODE_VECTOR_ACCUM
7553 && mclass != MODE_VECTOR_UACCUM)
7554 return 0;
7555
7556 /* Hardware support. Woo hoo! */
7557 if (targetm.vector_mode_supported_p (mode))
7558 return 1;
7559
7560 innermode = GET_MODE_INNER (mode);
7561
7562 /* We should probably return 1 if requesting V4DI and we have no DI,
7563 but we have V2DI, but this is probably very unlikely. */
7564
7565 /* If we have support for the inner mode, we can safely emulate it.
7566 We may not have V2DI, but me can emulate with a pair of DIs. */
7567 return targetm.scalar_mode_supported_p (innermode);
7568}
7569
7570
f8e93a2e 7571/* Handle a "mode" attribute; arguments as in
7572 struct attribute_spec.handler. */
7573
7574static tree
9a03a746 7575handle_mode_attribute (tree *node, tree name, tree args,
7576 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7577{
7578 tree type = *node;
d1dd9ac0 7579 tree ident = TREE_VALUE (args);
f8e93a2e 7580
7581 *no_add_attrs = true;
ab2c1de8 7582
d1dd9ac0 7583 if (TREE_CODE (ident) != IDENTIFIER_NODE)
9b2d6d13 7584 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7585 else
7586 {
7587 int j;
d1dd9ac0 7588 const char *p = IDENTIFIER_POINTER (ident);
f8e93a2e 7589 int len = strlen (p);
3754d046 7590 machine_mode mode = VOIDmode;
f8e93a2e 7591 tree typefm;
b2aef146 7592 bool valid_mode;
f8e93a2e 7593
7594 if (len > 4 && p[0] == '_' && p[1] == '_'
7595 && p[len - 1] == '_' && p[len - 2] == '_')
7596 {
4fd61bc6 7597 char *newp = (char *) alloca (len - 1);
f8e93a2e 7598
7599 strcpy (newp, &p[2]);
7600 newp[len - 4] = '\0';
7601 p = newp;
7602 }
7603
7604 /* Change this type to have a type with the specified mode.
7605 First check for the special modes. */
84166705 7606 if (!strcmp (p, "byte"))
f8e93a2e 7607 mode = byte_mode;
7608 else if (!strcmp (p, "word"))
7609 mode = word_mode;
84166705 7610 else if (!strcmp (p, "pointer"))
f8e93a2e 7611 mode = ptr_mode;
0ef89dfd 7612 else if (!strcmp (p, "libgcc_cmp_return"))
7613 mode = targetm.libgcc_cmp_return_mode ();
7614 else if (!strcmp (p, "libgcc_shift_count"))
7615 mode = targetm.libgcc_shift_count_mode ();
1bd43494 7616 else if (!strcmp (p, "unwind_word"))
7617 mode = targetm.unwind_word_mode ();
f8e93a2e 7618 else
7619 for (j = 0; j < NUM_MACHINE_MODES; j++)
7620 if (!strcmp (p, GET_MODE_NAME (j)))
743a6f47 7621 {
3754d046 7622 mode = (machine_mode) j;
743a6f47 7623 break;
7624 }
f8e93a2e 7625
7626 if (mode == VOIDmode)
4917c376 7627 {
d1dd9ac0 7628 error ("unknown machine mode %qE", ident);
4917c376 7629 return NULL_TREE;
7630 }
7631
b2aef146 7632 valid_mode = false;
7633 switch (GET_MODE_CLASS (mode))
4917c376 7634 {
b2aef146 7635 case MODE_INT:
7636 case MODE_PARTIAL_INT:
7637 case MODE_FLOAT:
c4503c0a 7638 case MODE_DECIMAL_FLOAT:
9421ebb9 7639 case MODE_FRACT:
7640 case MODE_UFRACT:
7641 case MODE_ACCUM:
7642 case MODE_UACCUM:
b2aef146 7643 valid_mode = targetm.scalar_mode_supported_p (mode);
7644 break;
7645
7646 case MODE_COMPLEX_INT:
7647 case MODE_COMPLEX_FLOAT:
7648 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7649 break;
7650
7651 case MODE_VECTOR_INT:
7652 case MODE_VECTOR_FLOAT:
9421ebb9 7653 case MODE_VECTOR_FRACT:
7654 case MODE_VECTOR_UFRACT:
7655 case MODE_VECTOR_ACCUM:
7656 case MODE_VECTOR_UACCUM:
9b2d6d13 7657 warning (OPT_Wattributes, "specifying vector types with "
7658 "__attribute__ ((mode)) is deprecated");
7659 warning (OPT_Wattributes,
7660 "use __attribute__ ((vector_size)) instead");
b2aef146 7661 valid_mode = vector_mode_valid_p (mode);
7662 break;
4917c376 7663
b2aef146 7664 default:
7665 break;
7666 }
7667 if (!valid_mode)
7668 {
1e5fcbe2 7669 error ("unable to emulate %qs", p);
b2aef146 7670 return NULL_TREE;
7671 }
4917c376 7672
b2aef146 7673 if (POINTER_TYPE_P (type))
ead34f59 7674 {
6d5d708e 7675 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
3754d046 7676 tree (*fn)(tree, machine_mode, bool);
b2aef146 7677
6d5d708e 7678 if (!targetm.addr_space.valid_pointer_mode (mode, as))
ead34f59 7679 {
1e5fcbe2 7680 error ("invalid pointer mode %qs", p);
ead34f59 7681 return NULL_TREE;
7682 }
7683
a0c938f0 7684 if (TREE_CODE (type) == POINTER_TYPE)
b2aef146 7685 fn = build_pointer_type_for_mode;
805e22b2 7686 else
b2aef146 7687 fn = build_reference_type_for_mode;
7688 typefm = fn (TREE_TYPE (type), mode, false);
ead34f59 7689 }
b2aef146 7690 else
9421ebb9 7691 {
7692 /* For fixed-point modes, we need to test if the signness of type
7693 and the machine mode are consistent. */
7694 if (ALL_FIXED_POINT_MODE_P (mode)
7695 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7696 {
bf776685 7697 error ("signedness of type and machine mode %qs don%'t match", p);
9421ebb9 7698 return NULL_TREE;
7699 }
7700 /* For fixed-point modes, we need to pass saturating info. */
7701 typefm = lang_hooks.types.type_for_mode (mode,
7702 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7703 : TYPE_UNSIGNED (type));
7704 }
3a648ab9 7705
b2aef146 7706 if (typefm == NULL_TREE)
7707 {
743a6f47 7708 error ("no data type for mode %qs", p);
b2aef146 7709 return NULL_TREE;
7710 }
3a648ab9 7711 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7712 {
7713 /* For enumeral types, copy the precision from the integer
7714 type returned above. If not an INTEGER_TYPE, we can't use
7715 this mode for this type. */
7716 if (TREE_CODE (typefm) != INTEGER_TYPE)
7717 {
743a6f47 7718 error ("cannot use mode %qs for enumeral types", p);
3a648ab9 7719 return NULL_TREE;
7720 }
7721
10080eac 7722 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7723 {
7724 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7725 typefm = type;
7726 }
7727 else
7728 {
7729 /* We cannot build a type variant, as there's code that assumes
7730 that TYPE_MAIN_VARIANT has the same mode. This includes the
7731 debug generators. Instead, create a subrange type. This
7732 results in all of the enumeral values being emitted only once
7733 in the original, and the subtype gets them by reference. */
7734 if (TYPE_UNSIGNED (type))
7735 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7736 else
7737 typefm = make_signed_type (TYPE_PRECISION (typefm));
7738 TREE_TYPE (typefm) = type;
7739 }
3a648ab9 7740 }
4bf450a1 7741 else if (VECTOR_MODE_P (mode)
7742 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7743 : TREE_CODE (type) != TREE_CODE (typefm))
743a6f47 7744 {
7745 error ("mode %qs applied to inappropriate type", p);
7746 return NULL_TREE;
7747 }
7748
b2aef146 7749 *node = typefm;
f8e93a2e 7750 }
7751
7752 return NULL_TREE;
7753}
7754
7755/* Handle a "section" attribute; arguments as in
7756 struct attribute_spec.handler. */
7757
7758static tree
9a03a746 7759handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7760 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7761{
7762 tree decl = *node;
7763
9866562d 7764 if (!targetm_common.have_named_sections)
f8e93a2e 7765 {
9866562d 7766 error_at (DECL_SOURCE_LOCATION (*node),
7767 "section attributes are not supported for this target");
7768 goto fail;
7769 }
065efcb1 7770
9866562d 7771 user_defined_section_attribute = true;
f8e93a2e 7772
b443c459 7773 if (!VAR_OR_FUNCTION_DECL_P (decl))
9866562d 7774 {
7775 error ("section attribute not allowed for %q+D", *node);
7776 goto fail;
f8e93a2e 7777 }
9866562d 7778
7779 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
f8e93a2e 7780 {
9866562d 7781 error ("section attribute argument not a string constant");
7782 goto fail;
f8e93a2e 7783 }
7784
f48c7f4a 7785 if (VAR_P (decl)
9866562d 7786 && current_function_decl != NULL_TREE
7787 && !TREE_STATIC (decl))
7788 {
7789 error_at (DECL_SOURCE_LOCATION (decl),
7790 "section attribute cannot be specified for local variables");
7791 goto fail;
7792 }
7793
7794 /* The decl may have already been given a section attribute
7795 from a previous declaration. Ensure they match. */
7796 if (DECL_SECTION_NAME (decl) != NULL
7797 && strcmp (DECL_SECTION_NAME (decl),
7798 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7799 {
7800 error ("section of %q+D conflicts with previous declaration", *node);
7801 goto fail;
7802 }
7803
f48c7f4a 7804 if (VAR_P (decl)
9866562d 7805 && !targetm.have_tls && targetm.emutls.tmpl_section
7806 && DECL_THREAD_LOCAL_P (decl))
7807 {
7808 error ("section of %q+D cannot be overridden", *node);
7809 goto fail;
7810 }
7811
7812 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
7813 return NULL_TREE;
7814
7815fail:
7816 *no_add_attrs = true;
f8e93a2e 7817 return NULL_TREE;
7818}
7819
83e25171 7820/* Check whether ALIGN is a valid user-specified alignment. If so,
7821 return its base-2 log; if not, output an error and return -1. If
7822 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7823 no error. */
7824int
7825check_user_alignment (const_tree align, bool allow_zero)
7826{
7827 int i;
7828
3e5a8b00 7829 if (error_operand_p (align))
7830 return -1;
5abaa10a 7831 if (TREE_CODE (align) != INTEGER_CST
7832 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
83e25171 7833 {
7834 error ("requested alignment is not an integer constant");
7835 return -1;
7836 }
7837 else if (allow_zero && integer_zerop (align))
7838 return -1;
1a087624 7839 else if (tree_int_cst_sgn (align) == -1
7840 || (i = tree_log2 (align)) == -1)
83e25171 7841 {
1a087624 7842 error ("requested alignment is not a positive power of 2");
83e25171 7843 return -1;
7844 }
296103aa 7845 else if (i >= HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT)
83e25171 7846 {
7847 error ("requested alignment is too large");
7848 return -1;
7849 }
7850 return i;
7851}
7852
ffcdbf9c 7853/*
7854 If in c++-11, check if the c++-11 alignment constraint with respect
7855 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7856 c++-11 mode, does nothing.
7857
7858 [dcl.align]2/ says:
7859
7860 [* if the constant expression evaluates to a fundamental alignment,
7861 the alignment requirement of the declared entity shall be the
7862 specified fundamental alignment.
7863
7864 * if the constant expression evaluates to an extended alignment
7865 and the implementation supports that alignment in the context
7866 of the declaration, the alignment of the declared entity shall
7867 be that alignment
7868
7869 * if the constant expression evaluates to an extended alignment
7870 and the implementation does not support that alignment in the
7871 context of the declaration, the program is ill-formed]. */
7872
7873static bool
7874check_cxx_fundamental_alignment_constraints (tree node,
7875 unsigned align_log,
7876 int flags)
7877{
7878 bool alignment_too_large_p = false;
7879 unsigned requested_alignment = 1U << align_log;
7880 unsigned max_align = 0;
7881
7882 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7883 || (node == NULL_TREE || node == error_mark_node))
7884 return true;
7885
7886 if (cxx_fundamental_alignment_p (requested_alignment))
7887 return true;
7888
7889 if (DECL_P (node))
7890 {
7891 if (TREE_STATIC (node))
7892 {
7893 /* For file scope variables and static members, the target
7894 supports alignments that are at most
7895 MAX_OFILE_ALIGNMENT. */
7896 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7897 alignment_too_large_p = true;
7898 }
7899 else
7900 {
7901#ifdef BIGGEST_FIELD_ALIGNMENT
7902#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7903#else
7904#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7905#endif
7906 /* For non-static members, the target supports either
7907 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7908 if it is defined or BIGGEST_ALIGNMENT. */
7909 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7910 if (TREE_CODE (node) == FIELD_DECL
7911 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7912 alignment_too_large_p = true;
7913#undef MAX_TARGET_FIELD_ALIGNMENT
7914 /* For stack variables, the target supports at most
7915 MAX_STACK_ALIGNMENT. */
7916 else if (decl_function_context (node) != NULL
7917 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7918 alignment_too_large_p = true;
7919 }
7920 }
7921 else if (TYPE_P (node))
7922 {
7923 /* Let's be liberal for types. */
7924 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7925 alignment_too_large_p = true;
7926 }
7927
7928 if (alignment_too_large_p)
7929 pedwarn (input_location, OPT_Wattributes,
7930 "requested alignment %d is larger than %d",
7931 requested_alignment, max_align);
7932
7933 return !alignment_too_large_p;
7934}
7935
f8e93a2e 7936/* Handle a "aligned" attribute; arguments as in
7937 struct attribute_spec.handler. */
7938
7939static tree
9a03a746 7940handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
09347743 7941 int flags, bool *no_add_attrs)
f8e93a2e 7942{
7943 tree decl = NULL_TREE;
7944 tree *type = NULL;
7945 int is_type = 0;
caf62483 7946 tree align_expr;
f8e93a2e 7947 int i;
7948
caf62483 7949 if (args)
7950 {
7951 align_expr = TREE_VALUE (args);
3e5a8b00 7952 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7953 && TREE_CODE (align_expr) != FUNCTION_DECL)
caf62483 7954 align_expr = default_conversion (align_expr);
7955 }
7956 else
7957 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7958
f8e93a2e 7959 if (DECL_P (*node))
7960 {
7961 decl = *node;
7962 type = &TREE_TYPE (decl);
7963 is_type = TREE_CODE (*node) == TYPE_DECL;
7964 }
7965 else if (TYPE_P (*node))
7966 type = node, is_type = 1;
7967
eb16928e 7968 if ((i = check_user_alignment (align_expr, true)) == -1
ffcdbf9c 7969 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
83e25171 7970 *no_add_attrs = true;
f8e93a2e 7971 else if (is_type)
7972 {
2ec3af9c 7973 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7974 /* OK, modify the type in place. */;
f8e93a2e 7975 /* If we have a TYPE_DECL, then copy the type, so that we
7976 don't accidentally modify a builtin type. See pushdecl. */
2ec3af9c 7977 else if (decl && TREE_TYPE (decl) != error_mark_node
7978 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
f8e93a2e 7979 {
7980 tree tt = TREE_TYPE (decl);
e086912e 7981 *type = build_variant_type_copy (*type);
f8e93a2e 7982 DECL_ORIGINAL_TYPE (decl) = tt;
7983 TYPE_NAME (*type) = decl;
7984 TREE_USED (*type) = TREE_USED (decl);
7985 TREE_TYPE (decl) = *type;
7986 }
2ec3af9c 7987 else
e086912e 7988 *type = build_variant_type_copy (*type);
f8e93a2e 7989
5d4b30ea 7990 SET_TYPE_ALIGN (*type, (1U << i) * BITS_PER_UNIT);
f8e93a2e 7991 TYPE_USER_ALIGN (*type) = 1;
7992 }
097b5c8b 7993 else if (! VAR_OR_FUNCTION_DECL_P (decl)
f8e93a2e 7994 && TREE_CODE (decl) != FIELD_DECL)
7995 {
3cf8b391 7996 error ("alignment may not be specified for %q+D", decl);
f8e93a2e 7997 *no_add_attrs = true;
7998 }
ffcdbf9c 7999 else if (DECL_USER_ALIGN (decl)
8000 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
8001 /* C++-11 [dcl.align/4]:
8002
8003 When multiple alignment-specifiers are specified for an
8004 entity, the alignment requirement shall be set to the
8005 strictest specified alignment.
8006
8007 This formally comes from the c++11 specification but we are
8008 doing it for the GNU attribute syntax as well. */
8009 *no_add_attrs = true;
097b5c8b 8010 else if (TREE_CODE (decl) == FUNCTION_DECL
7cfdc2f0 8011 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
097b5c8b 8012 {
8013 if (DECL_USER_ALIGN (decl))
8014 error ("alignment for %q+D was previously specified as %d "
8015 "and may not be decreased", decl,
8016 DECL_ALIGN (decl) / BITS_PER_UNIT);
8017 else
8018 error ("alignment for %q+D must be at least %d", decl,
8019 DECL_ALIGN (decl) / BITS_PER_UNIT);
8020 *no_add_attrs = true;
8021 }
f8e93a2e 8022 else
8023 {
5d4b30ea 8024 SET_DECL_ALIGN (decl, (1U << i) * BITS_PER_UNIT);
f8e93a2e 8025 DECL_USER_ALIGN (decl) = 1;
8026 }
8027
8028 return NULL_TREE;
8029}
8030
8031/* Handle a "weak" attribute; arguments as in
8032 struct attribute_spec.handler. */
8033
8034static tree
f948b309 8035handle_weak_attribute (tree *node, tree name,
9a03a746 8036 tree ARG_UNUSED (args),
8037 int ARG_UNUSED (flags),
8038 bool * ARG_UNUSED (no_add_attrs))
f8e93a2e 8039{
f948b309 8040 if (TREE_CODE (*node) == FUNCTION_DECL
059a60f3 8041 && DECL_DECLARED_INLINE_P (*node))
8042 {
0725e25c 8043 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
059a60f3 8044 *no_add_attrs = true;
8045 }
85c0a25c 8046 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8047 {
8048 error ("indirect function %q+D cannot be declared weak", *node);
8049 *no_add_attrs = true;
8050 return NULL_TREE;
8051 }
b443c459 8052 else if (VAR_OR_FUNCTION_DECL_P (*node))
9e6bcade 8053 declare_weak (*node);
f948b309 8054 else
8055 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8056
8057 return NULL_TREE;
8058}
8059
4641882f 8060/* Handle a "noplt" attribute; arguments as in
8061 struct attribute_spec.handler. */
8062
8063static tree
8064handle_noplt_attribute (tree *node, tree name,
8065 tree ARG_UNUSED (args),
8066 int ARG_UNUSED (flags),
8067 bool * ARG_UNUSED (no_add_attrs))
8068{
8069 if (TREE_CODE (*node) != FUNCTION_DECL)
8070 {
8071 warning (OPT_Wattributes,
8072 "%qE attribute is only applicable on functions", name);
8073 *no_add_attrs = true;
8074 return NULL_TREE;
8075 }
8076 return NULL_TREE;
8077}
8078
85c0a25c 8079/* Handle an "alias" or "ifunc" attribute; arguments as in
8080 struct attribute_spec.handler, except that IS_ALIAS tells us
8081 whether this is an alias as opposed to ifunc attribute. */
f8e93a2e 8082
8083static tree
85c0a25c 8084handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
8085 bool *no_add_attrs)
f8e93a2e 8086{
8087 tree decl = *node;
8088
85c0a25c 8089 if (TREE_CODE (decl) != FUNCTION_DECL
f48c7f4a 8090 && (!is_alias || !VAR_P (decl)))
9e830260 8091 {
8092 warning (OPT_Wattributes, "%qE attribute ignored", name);
8093 *no_add_attrs = true;
8094 }
8095 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
48e1416a 8096 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 8097 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
8098 /* A static variable declaration is always a tentative definition,
8099 but the alias is a non-tentative definition which overrides. */
48e1416a 8100 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 8101 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
f8e93a2e 8102 {
85c0a25c 8103 error ("%q+D defined both normally and as %qE attribute", decl, name);
f8e93a2e 8104 *no_add_attrs = true;
85c0a25c 8105 return NULL_TREE;
f8e93a2e 8106 }
85c0a25c 8107 else if (!is_alias
8108 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
8109 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
8110 {
8111 error ("weak %q+D cannot be defined %qE", decl, name);
8112 *no_add_attrs = true;
8113 return NULL_TREE;
8114 }
8c42f0d9 8115
8116 /* Note that the very first time we process a nested declaration,
8117 decl_function_context will not be set. Indeed, *would* never
8118 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8119 we do below. After such frobbery, pushdecl would set the context.
8120 In any case, this is never what we want. */
8121 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
f8e93a2e 8122 {
8123 tree id;
8124
8125 id = TREE_VALUE (args);
8126 if (TREE_CODE (id) != STRING_CST)
8127 {
85c0a25c 8128 error ("attribute %qE argument not a string", name);
f8e93a2e 8129 *no_add_attrs = true;
8130 return NULL_TREE;
8131 }
8132 id = get_identifier (TREE_STRING_POINTER (id));
8133 /* This counts as a use of the object pointed to. */
8134 TREE_USED (id) = 1;
8135
8136 if (TREE_CODE (decl) == FUNCTION_DECL)
8137 DECL_INITIAL (decl) = error_mark_node;
8138 else
f2526cce 8139 TREE_STATIC (decl) = 1;
85c0a25c 8140
8141 if (!is_alias)
8142 /* ifuncs are also aliases, so set that attribute too. */
8143 DECL_ATTRIBUTES (decl)
8144 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
f8e93a2e 8145 }
8146 else
8147 {
9b2d6d13 8148 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8149 *no_add_attrs = true;
8150 }
8151
8e857c41 8152 if (decl_in_symtab_p (*node))
8153 {
8154 struct symtab_node *n = symtab_node::get (decl);
8155 if (n && n->refuse_visibility_changes)
8156 {
8157 if (is_alias)
8158 error ("%+D declared alias after being used", decl);
8159 else
8160 error ("%+D declared ifunc after being used", decl);
8161 }
8162 }
8163
8164
f8e93a2e 8165 return NULL_TREE;
8166}
8167
85c0a25c 8168/* Handle an "alias" or "ifunc" attribute; arguments as in
8169 struct attribute_spec.handler. */
8170
8171static tree
8172handle_ifunc_attribute (tree *node, tree name, tree args,
8173 int ARG_UNUSED (flags), bool *no_add_attrs)
8174{
8175 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8176}
8177
8178/* Handle an "alias" or "ifunc" attribute; arguments as in
8179 struct attribute_spec.handler. */
8180
8181static tree
8182handle_alias_attribute (tree *node, tree name, tree args,
8183 int ARG_UNUSED (flags), bool *no_add_attrs)
8184{
8185 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8186}
8187
f4a30bd7 8188/* Handle a "weakref" attribute; arguments as in struct
8189 attribute_spec.handler. */
8190
8191static tree
8192handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8193 int flags, bool *no_add_attrs)
8194{
8195 tree attr = NULL_TREE;
8196
83852912 8197 /* We must ignore the attribute when it is associated with
8198 local-scoped decls, since attribute alias is ignored and many
8199 such symbols do not even have a DECL_WEAK field. */
64a7bd81 8200 if (decl_function_context (*node)
8201 || current_function_decl
b443c459 8202 || !VAR_OR_FUNCTION_DECL_P (*node))
83852912 8203 {
8204 warning (OPT_Wattributes, "%qE attribute ignored", name);
8205 *no_add_attrs = true;
8206 return NULL_TREE;
8207 }
8208
85c0a25c 8209 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8210 {
8211 error ("indirect function %q+D cannot be declared weakref", *node);
8212 *no_add_attrs = true;
8213 return NULL_TREE;
8214 }
8215
f4a30bd7 8216 /* The idea here is that `weakref("name")' mutates into `weakref,
8217 alias("name")', and weakref without arguments, in turn,
8218 implicitly adds weak. */
8219
8220 if (args)
8221 {
8222 attr = tree_cons (get_identifier ("alias"), args, attr);
8223 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8224
8225 *no_add_attrs = true;
0a3ecdc1 8226
8227 decl_attributes (node, attr, flags);
f4a30bd7 8228 }
8229 else
8230 {
8231 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
712d2297 8232 error_at (DECL_SOURCE_LOCATION (*node),
8233 "weakref attribute must appear before alias attribute");
f4a30bd7 8234
0a3ecdc1 8235 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8236 and that isn't supported; and because it wants to add it to
8237 the list of weak decls, which isn't helpful. */
8238 DECL_WEAK (*node) = 1;
f4a30bd7 8239 }
8240
8e857c41 8241 if (decl_in_symtab_p (*node))
8242 {
8243 struct symtab_node *n = symtab_node::get (*node);
8244 if (n && n->refuse_visibility_changes)
8245 error ("%+D declared weakref after being used", *node);
8246 }
8247
f4a30bd7 8248 return NULL_TREE;
8249}
8250
f8e93a2e 8251/* Handle an "visibility" attribute; arguments as in
8252 struct attribute_spec.handler. */
8253
8254static tree
1cae46be 8255handle_visibility_attribute (tree *node, tree name, tree args,
9a03a746 8256 int ARG_UNUSED (flags),
4a2849cb 8257 bool *ARG_UNUSED (no_add_attrs))
f8e93a2e 8258{
8259 tree decl = *node;
9c40570a 8260 tree id = TREE_VALUE (args);
4a2849cb 8261 enum symbol_visibility vis;
f8e93a2e 8262
b212f378 8263 if (TYPE_P (*node))
8264 {
4a2849cb 8265 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8266 /* OK */;
8e539fdc 8267 else if (!RECORD_OR_UNION_TYPE_P (*node))
4a2849cb 8268 {
8269 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8270 name);
8271 return NULL_TREE;
8272 }
8273 else if (TYPE_FIELDS (*node))
8274 {
8275 error ("%qE attribute ignored because %qT is already defined",
8276 name, *node);
8277 return NULL_TREE;
8278 }
b212f378 8279 }
84166705 8280 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
f8e93a2e 8281 {
9b2d6d13 8282 warning (OPT_Wattributes, "%qE attribute ignored", name);
9c40570a 8283 return NULL_TREE;
f8e93a2e 8284 }
f8e93a2e 8285
9c40570a 8286 if (TREE_CODE (id) != STRING_CST)
8287 {
07e3a3d2 8288 error ("visibility argument not a string");
9c40570a 8289 return NULL_TREE;
f8e93a2e 8290 }
b27ac6b5 8291
b212f378 8292 /* If this is a type, set the visibility on the type decl. */
8293 if (TYPE_P (decl))
8294 {
8295 decl = TYPE_NAME (decl);
84166705 8296 if (!decl)
a0c938f0 8297 return NULL_TREE;
e147aab3 8298 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8299 {
9b2d6d13 8300 warning (OPT_Wattributes, "%qE attribute ignored on types",
e147aab3 8301 name);
8302 return NULL_TREE;
8303 }
b212f378 8304 }
f8e93a2e 8305
9c40570a 8306 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
4a2849cb 8307 vis = VISIBILITY_DEFAULT;
9c40570a 8308 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
4a2849cb 8309 vis = VISIBILITY_INTERNAL;
9c40570a 8310 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
4a2849cb 8311 vis = VISIBILITY_HIDDEN;
9c40570a 8312 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
4a2849cb 8313 vis = VISIBILITY_PROTECTED;
9c40570a 8314 else
4a2849cb 8315 {
8316 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8317 vis = VISIBILITY_DEFAULT;
8318 }
8319
8320 if (DECL_VISIBILITY_SPECIFIED (decl)
098a01e7 8321 && vis != DECL_VISIBILITY (decl))
8322 {
8323 tree attributes = (TYPE_P (*node)
8324 ? TYPE_ATTRIBUTES (*node)
8325 : DECL_ATTRIBUTES (decl));
8326 if (lookup_attribute ("visibility", attributes))
8327 error ("%qD redeclared with different visibility", decl);
8328 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8329 && lookup_attribute ("dllimport", attributes))
8330 error ("%qD was declared %qs which implies default visibility",
8331 decl, "dllimport");
8332 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8333 && lookup_attribute ("dllexport", attributes))
8334 error ("%qD was declared %qs which implies default visibility",
8335 decl, "dllexport");
8336 }
4a2849cb 8337
8338 DECL_VISIBILITY (decl) = vis;
b212f378 8339 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8340
4a2849cb 8341 /* Go ahead and attach the attribute to the node as well. This is needed
8342 so we can determine whether we have VISIBILITY_DEFAULT because the
8343 visibility was not specified, or because it was explicitly overridden
8344 from the containing scope. */
9c40570a 8345
f8e93a2e 8346 return NULL_TREE;
8347}
8348
3aa0c315 8349/* Determine the ELF symbol visibility for DECL, which is either a
8350 variable or a function. It is an error to use this function if a
8351 definition of DECL is not available in this translation unit.
8352 Returns true if the final visibility has been determined by this
8353 function; false if the caller is free to make additional
8354 modifications. */
8355
8356bool
8357c_determine_visibility (tree decl)
8358{
b443c459 8359 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
3aa0c315 8360
8361 /* If the user explicitly specified the visibility with an
8362 attribute, honor that. DECL_VISIBILITY will have been set during
920f5a70 8363 the processing of the attribute. We check for an explicit
8364 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8365 to distinguish the use of an attribute from the use of a "#pragma
8366 GCC visibility push(...)"; in the latter case we still want other
8367 considerations to be able to overrule the #pragma. */
8368 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8369 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8370 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8371 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
3aa0c315 8372 return true;
8373
4a2849cb 8374 /* Set default visibility to whatever the user supplied with
8375 visibility_specified depending on #pragma GCC visibility. */
8376 if (!DECL_VISIBILITY_SPECIFIED (decl))
8377 {
2d9d8740 8378 if (visibility_options.inpragma
8379 || DECL_VISIBILITY (decl) != default_visibility)
8380 {
8381 DECL_VISIBILITY (decl) = default_visibility;
8382 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8383 /* If visibility changed and DECL already has DECL_RTL, ensure
8384 symbol flags are updated. */
f48c7f4a 8385 if (((VAR_P (decl) && TREE_STATIC (decl))
2d9d8740 8386 || TREE_CODE (decl) == FUNCTION_DECL)
8387 && DECL_RTL_SET_P (decl))
8388 make_decl_rtl (decl);
8389 }
4a2849cb 8390 }
3aa0c315 8391 return false;
8392}
8393
24dfead4 8394/* Handle an "tls_model" attribute; arguments as in
8395 struct attribute_spec.handler. */
8396
8397static tree
1cae46be 8398handle_tls_model_attribute (tree *node, tree name, tree args,
9a03a746 8399 int ARG_UNUSED (flags), bool *no_add_attrs)
24dfead4 8400{
1b53eb20 8401 tree id;
24dfead4 8402 tree decl = *node;
1b53eb20 8403 enum tls_model kind;
24dfead4 8404
1b53eb20 8405 *no_add_attrs = true;
8406
f48c7f4a 8407 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
24dfead4 8408 {
9b2d6d13 8409 warning (OPT_Wattributes, "%qE attribute ignored", name);
1b53eb20 8410 return NULL_TREE;
24dfead4 8411 }
24dfead4 8412
1b53eb20 8413 kind = DECL_TLS_MODEL (decl);
8414 id = TREE_VALUE (args);
8415 if (TREE_CODE (id) != STRING_CST)
8416 {
8417 error ("tls_model argument not a string");
8418 return NULL_TREE;
24dfead4 8419 }
8420
1b53eb20 8421 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8422 kind = TLS_MODEL_LOCAL_EXEC;
8423 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8424 kind = TLS_MODEL_INITIAL_EXEC;
8425 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8426 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8427 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8428 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8429 else
8430 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8431
5e68df57 8432 set_decl_tls_model (decl, kind);
24dfead4 8433 return NULL_TREE;
8434}
8435
f8e93a2e 8436/* Handle a "no_instrument_function" attribute; arguments as in
8437 struct attribute_spec.handler. */
8438
8439static tree
1cae46be 8440handle_no_instrument_function_attribute (tree *node, tree name,
9a03a746 8441 tree ARG_UNUSED (args),
8442 int ARG_UNUSED (flags),
09347743 8443 bool *no_add_attrs)
f8e93a2e 8444{
8445 tree decl = *node;
8446
8447 if (TREE_CODE (decl) != FUNCTION_DECL)
8448 {
712d2297 8449 error_at (DECL_SOURCE_LOCATION (decl),
8450 "%qE attribute applies only to functions", name);
f8e93a2e 8451 *no_add_attrs = true;
8452 }
f8e93a2e 8453 else
8454 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8455
8456 return NULL_TREE;
8457}
8458
595e387a 8459/* Handle a "no_profile_instrument_function" attribute; arguments as in
8460 struct attribute_spec.handler. */
8461
8462static tree
8463handle_no_profile_instrument_function_attribute (tree *node, tree name, tree,
8464 int, bool *no_add_attrs)
8465{
8466 if (TREE_CODE (*node) != FUNCTION_DECL)
8467 {
8468 warning (OPT_Wattributes, "%qE attribute ignored", name);
8469 *no_add_attrs = true;
8470 }
8471
8472 return NULL_TREE;
8473}
8474
f8e93a2e 8475/* Handle a "malloc" attribute; arguments as in
8476 struct attribute_spec.handler. */
8477
8478static tree
9a03a746 8479handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8480 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8481{
a5147fca 8482 if (TREE_CODE (*node) == FUNCTION_DECL
8483 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
f8e93a2e 8484 DECL_IS_MALLOC (*node) = 1;
f8e93a2e 8485 else
8486 {
9b2d6d13 8487 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8488 *no_add_attrs = true;
8489 }
8490
8491 return NULL_TREE;
8492}
8493
4a29c97c 8494/* Handle a "alloc_size" attribute; arguments as in
8495 struct attribute_spec.handler. */
8496
8497static tree
8498handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8499 int ARG_UNUSED (flags), bool *no_add_attrs)
8500{
2802826e 8501 unsigned arg_count = type_num_arguments (*node);
4a29c97c 8502 for (; args; args = TREE_CHAIN (args))
8503 {
8504 tree position = TREE_VALUE (args);
caf62483 8505 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8506 && TREE_CODE (position) != FUNCTION_DECL)
8507 position = default_conversion (position);
4a29c97c 8508
237e78b1 8509 if (!tree_fits_uhwi_p (position)
8510 || !arg_count
8511 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
4a29c97c 8512 {
48e1416a 8513 warning (OPT_Wattributes,
4a29c97c 8514 "alloc_size parameter outside range");
8515 *no_add_attrs = true;
8516 return NULL_TREE;
8517 }
8518 }
8519 return NULL_TREE;
8520}
8521
237e78b1 8522/* Handle a "alloc_align" attribute; arguments as in
8523 struct attribute_spec.handler. */
8524
8525static tree
8526handle_alloc_align_attribute (tree *node, tree, tree args, int,
8527 bool *no_add_attrs)
8528{
8529 unsigned arg_count = type_num_arguments (*node);
8530 tree position = TREE_VALUE (args);
7648c9c7 8531 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8532 && TREE_CODE (position) != FUNCTION_DECL)
237e78b1 8533 position = default_conversion (position);
8534
8535 if (!tree_fits_uhwi_p (position)
8536 || !arg_count
8537 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8538 {
8539 warning (OPT_Wattributes,
8540 "alloc_align parameter outside range");
8541 *no_add_attrs = true;
8542 return NULL_TREE;
8543 }
8544 return NULL_TREE;
8545}
8546
8547/* Handle a "assume_aligned" attribute; arguments as in
8548 struct attribute_spec.handler. */
8549
8550static tree
8551handle_assume_aligned_attribute (tree *, tree, tree args, int,
8552 bool *no_add_attrs)
8553{
8554 for (; args; args = TREE_CHAIN (args))
8555 {
8556 tree position = TREE_VALUE (args);
8557 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8558 && TREE_CODE (position) != FUNCTION_DECL)
8559 position = default_conversion (position);
8560
8561 if (TREE_CODE (position) != INTEGER_CST)
8562 {
8563 warning (OPT_Wattributes,
8564 "assume_aligned parameter not integer constant");
8565 *no_add_attrs = true;
8566 return NULL_TREE;
8567 }
8568 }
8569 return NULL_TREE;
8570}
8571
8ce86007 8572/* Handle a "fn spec" attribute; arguments as in
8573 struct attribute_spec.handler. */
8574
8575static tree
8576handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8577 tree args, int ARG_UNUSED (flags),
8578 bool *no_add_attrs ATTRIBUTE_UNUSED)
8579{
8580 gcc_assert (args
8581 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8582 && !TREE_CHAIN (args));
8583 return NULL_TREE;
8584}
8585
058a1b7a 8586/* Handle a "bnd_variable_size" attribute; arguments as in
8587 struct attribute_spec.handler. */
8588
8589static tree
8590handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8591 int ARG_UNUSED (flags), bool *no_add_attrs)
8592{
8593 if (TREE_CODE (*node) != FIELD_DECL)
8594 {
8595 warning (OPT_Wattributes, "%qE attribute ignored", name);
8596 *no_add_attrs = true;
8597 }
8598
8599 return NULL_TREE;
8600}
8601
8602/* Handle a "bnd_legacy" attribute; arguments as in
8603 struct attribute_spec.handler. */
8604
8605static tree
8606handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8607 int ARG_UNUSED (flags), bool *no_add_attrs)
8608{
8609 if (TREE_CODE (*node) != FUNCTION_DECL)
8610 {
8611 warning (OPT_Wattributes, "%qE attribute ignored", name);
8612 *no_add_attrs = true;
8613 }
8614
8615 return NULL_TREE;
8616}
8617
8618/* Handle a "bnd_instrument" attribute; arguments as in
8619 struct attribute_spec.handler. */
8620
8621static tree
8622handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8623 int ARG_UNUSED (flags), bool *no_add_attrs)
8624{
8625 if (TREE_CODE (*node) != FUNCTION_DECL)
8626 {
8627 warning (OPT_Wattributes, "%qE attribute ignored", name);
8628 *no_add_attrs = true;
8629 }
8630
8631 return NULL_TREE;
8632}
8633
a96c3cc1 8634/* Handle a "warn_unused" attribute; arguments as in
8635 struct attribute_spec.handler. */
8636
8637static tree
8638handle_warn_unused_attribute (tree *node, tree name,
8639 tree args ATTRIBUTE_UNUSED,
8640 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8641{
8642 if (TYPE_P (*node))
8643 /* Do nothing else, just set the attribute. We'll get at
8644 it later with lookup_attribute. */
8645 ;
8646 else
8647 {
8648 warning (OPT_Wattributes, "%qE attribute ignored", name);
8649 *no_add_attrs = true;
8650 }
8651
8652 return NULL_TREE;
8653}
8654
bc7bff74 8655/* Handle an "omp declare simd" attribute; arguments as in
8656 struct attribute_spec.handler. */
8657
8658static tree
8659handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8660{
8661 return NULL_TREE;
8662}
8663
c58a4cfd 8664/* Handle a "simd" attribute. */
8665
8666static tree
9841c6ee 8667handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
c58a4cfd 8668{
8669 if (TREE_CODE (*node) == FUNCTION_DECL)
8670 {
8671 if (lookup_attribute ("cilk simd function",
8672 DECL_ATTRIBUTES (*node)) != NULL)
8673 {
8674 error_at (DECL_SOURCE_LOCATION (*node),
8675 "%<__simd__%> attribute cannot be used in the same "
8676 "function marked as a Cilk Plus SIMD-enabled function");
8677 *no_add_attrs = true;
8678 }
8679 else
9841c6ee 8680 {
8681 tree t = get_identifier ("omp declare simd");
8682 tree attr = NULL_TREE;
8683 if (args)
8684 {
8685 tree id = TREE_VALUE (args);
8686
8687 if (TREE_CODE (id) != STRING_CST)
8688 {
8689 error ("attribute %qE argument not a string", name);
8690 *no_add_attrs = true;
8691 return NULL_TREE;
8692 }
8693
8694 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
8695 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8696 OMP_CLAUSE_NOTINBRANCH);
8697 else
8698 if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
8699 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8700 OMP_CLAUSE_INBRANCH);
8701 else
8702 {
8703 error ("only %<inbranch%> and %<notinbranch%> flags are "
8704 "allowed for %<__simd__%> attribute");
8705 *no_add_attrs = true;
8706 return NULL_TREE;
8707 }
8708 }
8709
8710 DECL_ATTRIBUTES (*node) = tree_cons (t,
8711 build_tree_list (NULL_TREE,
8712 attr),
8713 DECL_ATTRIBUTES (*node));
8714 }
c58a4cfd 8715 }
8716 else
8717 {
8718 warning (OPT_Wattributes, "%qE attribute ignored", name);
8719 *no_add_attrs = true;
8720 }
8721
8722 return NULL_TREE;
8723}
8724
bc7bff74 8725/* Handle an "omp declare target" attribute; arguments as in
8726 struct attribute_spec.handler. */
8727
8728static tree
8729handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8730{
8731 return NULL_TREE;
8732}
8733
26d1c5ff 8734/* Handle a "returns_twice" attribute; arguments as in
8735 struct attribute_spec.handler. */
8736
8737static tree
8738handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8739 int ARG_UNUSED (flags), bool *no_add_attrs)
8740{
8741 if (TREE_CODE (*node) == FUNCTION_DECL)
8742 DECL_IS_RETURNS_TWICE (*node) = 1;
8743 else
8744 {
9b2d6d13 8745 warning (OPT_Wattributes, "%qE attribute ignored", name);
26d1c5ff 8746 *no_add_attrs = true;
8747 }
8748
8749 return NULL_TREE;
8750}
8751
f8e93a2e 8752/* Handle a "no_limit_stack" attribute; arguments as in
8753 struct attribute_spec.handler. */
8754
8755static tree
1cae46be 8756handle_no_limit_stack_attribute (tree *node, tree name,
9a03a746 8757 tree ARG_UNUSED (args),
8758 int ARG_UNUSED (flags),
09347743 8759 bool *no_add_attrs)
f8e93a2e 8760{
8761 tree decl = *node;
8762
8763 if (TREE_CODE (decl) != FUNCTION_DECL)
8764 {
712d2297 8765 error_at (DECL_SOURCE_LOCATION (decl),
8766 "%qE attribute applies only to functions", name);
f8e93a2e 8767 *no_add_attrs = true;
8768 }
8769 else if (DECL_INITIAL (decl))
8770 {
712d2297 8771 error_at (DECL_SOURCE_LOCATION (decl),
8772 "can%'t set %qE attribute after definition", name);
f8e93a2e 8773 *no_add_attrs = true;
8774 }
8775 else
8776 DECL_NO_LIMIT_STACK (decl) = 1;
8777
8778 return NULL_TREE;
8779}
8780
8781/* Handle a "pure" attribute; arguments as in
8782 struct attribute_spec.handler. */
8783
8784static tree
9a03a746 8785handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8786 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8787{
8788 if (TREE_CODE (*node) == FUNCTION_DECL)
9c2a0c05 8789 DECL_PURE_P (*node) = 1;
f8e93a2e 8790 /* ??? TODO: Support types. */
8791 else
8792 {
9b2d6d13 8793 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8794 *no_add_attrs = true;
8795 }
8796
8797 return NULL_TREE;
8798}
8799
4c0315d0 8800/* Digest an attribute list destined for a transactional memory statement.
8801 ALLOWED is the set of attributes that are allowed for this statement;
8802 return the attribute we parsed. Multiple attributes are never allowed. */
8803
8804int
8805parse_tm_stmt_attr (tree attrs, int allowed)
8806{
8807 tree a_seen = NULL;
8808 int m_seen = 0;
8809
8810 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8811 {
8812 tree a = TREE_PURPOSE (attrs);
8813 int m = 0;
8814
8815 if (is_attribute_p ("outer", a))
8816 m = TM_STMT_ATTR_OUTER;
8817
8818 if ((m & allowed) == 0)
8819 {
8820 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8821 continue;
8822 }
8823
8824 if (m_seen == 0)
8825 {
8826 a_seen = a;
8827 m_seen = m;
8828 }
8829 else if (m_seen == m)
8830 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8831 else
8832 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8833 }
8834
8835 return m_seen;
8836}
8837
8838/* Transform a TM attribute name into a maskable integer and back.
8839 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8840 to how the lack of an attribute is treated. */
8841
8842int
8843tm_attr_to_mask (tree attr)
8844{
8845 if (attr == NULL)
8846 return 0;
8847 if (is_attribute_p ("transaction_safe", attr))
8848 return TM_ATTR_SAFE;
8849 if (is_attribute_p ("transaction_callable", attr))
8850 return TM_ATTR_CALLABLE;
8851 if (is_attribute_p ("transaction_pure", attr))
8852 return TM_ATTR_PURE;
8853 if (is_attribute_p ("transaction_unsafe", attr))
8854 return TM_ATTR_IRREVOCABLE;
8855 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8856 return TM_ATTR_MAY_CANCEL_OUTER;
8857 return 0;
8858}
8859
8860tree
8861tm_mask_to_attr (int mask)
8862{
8863 const char *str;
8864 switch (mask)
8865 {
8866 case TM_ATTR_SAFE:
8867 str = "transaction_safe";
8868 break;
8869 case TM_ATTR_CALLABLE:
8870 str = "transaction_callable";
8871 break;
8872 case TM_ATTR_PURE:
8873 str = "transaction_pure";
8874 break;
8875 case TM_ATTR_IRREVOCABLE:
8876 str = "transaction_unsafe";
8877 break;
8878 case TM_ATTR_MAY_CANCEL_OUTER:
8879 str = "transaction_may_cancel_outer";
8880 break;
8881 default:
8882 gcc_unreachable ();
8883 }
8884 return get_identifier (str);
8885}
8886
8887/* Return the first TM attribute seen in LIST. */
8888
8889tree
8890find_tm_attribute (tree list)
8891{
8892 for (; list ; list = TREE_CHAIN (list))
8893 {
8894 tree name = TREE_PURPOSE (list);
8895 if (tm_attr_to_mask (name) != 0)
8896 return name;
8897 }
8898 return NULL_TREE;
8899}
8900
8901/* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8902 Here we accept only function types, and verify that none of the other
8903 function TM attributes are also applied. */
8904/* ??? We need to accept class types for C++, but not C. This greatly
8905 complicates this function, since we can no longer rely on the extra
8906 processing given by function_type_required. */
8907
8908static tree
8909handle_tm_attribute (tree *node, tree name, tree args,
8910 int flags, bool *no_add_attrs)
8911{
8912 /* Only one path adds the attribute; others don't. */
8913 *no_add_attrs = true;
8914
8915 switch (TREE_CODE (*node))
8916 {
8917 case RECORD_TYPE:
8918 case UNION_TYPE:
8919 /* Only tm_callable and tm_safe apply to classes. */
8920 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8921 goto ignored;
8922 /* FALLTHRU */
8923
8924 case FUNCTION_TYPE:
8925 case METHOD_TYPE:
8926 {
8927 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8928 if (old_name == name)
8929 ;
8930 else if (old_name != NULL_TREE)
8931 error ("type was previously declared %qE", old_name);
8932 else
8933 *no_add_attrs = false;
8934 }
8935 break;
8936
6d02e6b2 8937 case FUNCTION_DECL:
8938 {
8939 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
8940 want to set transaction_safe on the type. */
8941 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
8942 if (!TYPE_P (DECL_CONTEXT (*node)))
8943 error_at (DECL_SOURCE_LOCATION (*node),
8944 "%<transaction_safe_dynamic%> may only be specified for "
8945 "a virtual function");
8946 *no_add_attrs = false;
8947 decl_attributes (&TREE_TYPE (*node),
8948 build_tree_list (get_identifier ("transaction_safe"),
8949 NULL_TREE),
8950 0);
8951 break;
8952 }
8953
4c0315d0 8954 case POINTER_TYPE:
8955 {
8956 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8957 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8958 {
8959 tree fn_tmp = TREE_TYPE (*node);
8960 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8961 *node = build_pointer_type (fn_tmp);
8962 break;
8963 }
8964 }
8965 /* FALLTHRU */
8966
8967 default:
8968 /* If a function is next, pass it on to be tried next. */
8969 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8970 return tree_cons (name, args, NULL);
8971
8972 ignored:
8973 warning (OPT_Wattributes, "%qE attribute ignored", name);
8974 break;
8975 }
8976
8977 return NULL_TREE;
8978}
8979
8980/* Handle the TM_WRAP attribute; arguments as in
8981 struct attribute_spec.handler. */
8982
8983static tree
8984handle_tm_wrap_attribute (tree *node, tree name, tree args,
8985 int ARG_UNUSED (flags), bool *no_add_attrs)
8986{
8987 tree decl = *node;
8988
8989 /* We don't need the attribute even on success, since we
8990 record the entry in an external table. */
8991 *no_add_attrs = true;
8992
8993 if (TREE_CODE (decl) != FUNCTION_DECL)
8994 warning (OPT_Wattributes, "%qE attribute ignored", name);
8995 else
8996 {
8997 tree wrap_decl = TREE_VALUE (args);
3e5a8b00 8998 if (error_operand_p (wrap_decl))
8999 ;
9000 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
b443c459 9001 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
4c0315d0 9002 error ("%qE argument not an identifier", name);
9003 else
9004 {
9005 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
9006 wrap_decl = lookup_name (wrap_decl);
9007 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
9008 {
9009 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
9010 TREE_TYPE (wrap_decl)))
9011 record_tm_replacement (wrap_decl, decl);
9012 else
9013 error ("%qD is not compatible with %qD", wrap_decl, decl);
9014 }
9015 else
cdf34fca 9016 error ("%qE argument is not a function", name);
4c0315d0 9017 }
9018 }
9019
9020 return NULL_TREE;
9021}
9022
9023/* Ignore the given attribute. Used when this attribute may be usefully
9024 overridden by the target, but is not used generically. */
9025
9026static tree
9027ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
9028 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9029 bool *no_add_attrs)
9030{
9031 *no_add_attrs = true;
9032 return NULL_TREE;
9033}
9034
fc09b200 9035/* Handle a "no vops" attribute; arguments as in
9036 struct attribute_spec.handler. */
9037
9038static tree
9039handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
9040 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9041 bool *ARG_UNUSED (no_add_attrs))
9042{
9043 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
9044 DECL_IS_NOVOPS (*node) = 1;
9045 return NULL_TREE;
9046}
9047
f8e93a2e 9048/* Handle a "deprecated" attribute; arguments as in
9049 struct attribute_spec.handler. */
1cae46be 9050
f8e93a2e 9051static tree
1cae46be 9052handle_deprecated_attribute (tree *node, tree name,
45c4e798 9053 tree args, int flags,
09347743 9054 bool *no_add_attrs)
f8e93a2e 9055{
9056 tree type = NULL_TREE;
9057 int warn = 0;
782858b8 9058 tree what = NULL_TREE;
1cae46be 9059
45c4e798 9060 if (!args)
9061 *no_add_attrs = true;
9062 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
9063 {
9064 error ("deprecated message is not a string");
9065 *no_add_attrs = true;
9066 }
9067
f8e93a2e 9068 if (DECL_P (*node))
9069 {
9070 tree decl = *node;
9071 type = TREE_TYPE (decl);
1cae46be 9072
f8e93a2e 9073 if (TREE_CODE (decl) == TYPE_DECL
9074 || TREE_CODE (decl) == PARM_DECL
b443c459 9075 || VAR_OR_FUNCTION_DECL_P (decl)
40c8d1dd 9076 || TREE_CODE (decl) == FIELD_DECL
5a4c69dd 9077 || TREE_CODE (decl) == CONST_DECL
40c8d1dd 9078 || objc_method_decl (TREE_CODE (decl)))
f8e93a2e 9079 TREE_DEPRECATED (decl) = 1;
9080 else
9081 warn = 1;
9082 }
9083 else if (TYPE_P (*node))
9084 {
9085 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 9086 *node = build_variant_type_copy (*node);
f8e93a2e 9087 TREE_DEPRECATED (*node) = 1;
9088 type = *node;
9089 }
9090 else
9091 warn = 1;
1cae46be 9092
f8e93a2e 9093 if (warn)
9094 {
9095 *no_add_attrs = true;
9096 if (type && TYPE_NAME (type))
9097 {
9098 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
782858b8 9099 what = TYPE_NAME (*node);
f8e93a2e 9100 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9101 && DECL_NAME (TYPE_NAME (type)))
782858b8 9102 what = DECL_NAME (TYPE_NAME (type));
f8e93a2e 9103 }
9104 if (what)
9b2d6d13 9105 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
f8e93a2e 9106 else
9b2d6d13 9107 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 9108 }
9109
9110 return NULL_TREE;
9111}
9112
f8e93a2e 9113/* Handle a "vector_size" attribute; arguments as in
9114 struct attribute_spec.handler. */
9115
9116static tree
1cae46be 9117handle_vector_size_attribute (tree *node, tree name, tree args,
9a03a746 9118 int ARG_UNUSED (flags),
09347743 9119 bool *no_add_attrs)
f8e93a2e 9120{
9121 unsigned HOST_WIDE_INT vecsize, nunits;
3754d046 9122 machine_mode orig_mode;
4917c376 9123 tree type = *node, new_type, size;
f8e93a2e 9124
9125 *no_add_attrs = true;
9126
4917c376 9127 size = TREE_VALUE (args);
3e5a8b00 9128 if (size && TREE_CODE (size) != IDENTIFIER_NODE
9129 && TREE_CODE (size) != FUNCTION_DECL)
caf62483 9130 size = default_conversion (size);
4917c376 9131
e913b5cd 9132 if (!tree_fits_uhwi_p (size))
f8e93a2e 9133 {
9b2d6d13 9134 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 9135 return NULL_TREE;
9136 }
9137
9138 /* Get the vector size (in bytes). */
e913b5cd 9139 vecsize = tree_to_uhwi (size);
f8e93a2e 9140
9141 /* We need to provide for vector pointers, vector arrays, and
9142 functions returning vectors. For example:
9143
9144 __attribute__((vector_size(16))) short *foo;
9145
9146 In this case, the mode is SI, but the type being modified is
9147 HI, so we need to look further. */
9148
9149 while (POINTER_TYPE_P (type)
9150 || TREE_CODE (type) == FUNCTION_TYPE
5bfb0742 9151 || TREE_CODE (type) == METHOD_TYPE
2cb0e5d9 9152 || TREE_CODE (type) == ARRAY_TYPE
9153 || TREE_CODE (type) == OFFSET_TYPE)
f8e93a2e 9154 type = TREE_TYPE (type);
9155
9156 /* Get the mode of the type being modified. */
9157 orig_mode = TYPE_MODE (type);
9158
2cb0e5d9 9159 if ((!INTEGRAL_TYPE_P (type)
9160 && !SCALAR_FLOAT_TYPE_P (type)
9161 && !FIXED_POINT_TYPE_P (type))
cee7491d 9162 || (!SCALAR_FLOAT_MODE_P (orig_mode)
9421ebb9 9163 && GET_MODE_CLASS (orig_mode) != MODE_INT
9164 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
e913b5cd 9165 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7ec31215 9166 || TREE_CODE (type) == BOOLEAN_TYPE)
f8e93a2e 9167 {
782858b8 9168 error ("invalid vector type for attribute %qE", name);
f8e93a2e 9169 return NULL_TREE;
9170 }
9171
e913b5cd 9172 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
39cc3e6d 9173 {
9174 error ("vector size not an integral multiple of component size");
9175 return NULL;
9176 }
9177
9178 if (vecsize == 0)
9179 {
9180 error ("zero vector size");
9181 return NULL;
9182 }
9183
f8e93a2e 9184 /* Calculate how many units fit in the vector. */
e913b5cd 9185 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
83e2a11b 9186 if (nunits & (nunits - 1))
f8e93a2e 9187 {
83e2a11b 9188 error ("number of components of the vector not a power of two");
f8e93a2e 9189 return NULL_TREE;
9190 }
9191
83e2a11b 9192 new_type = build_vector_type (type, nunits);
f8e93a2e 9193
9194 /* Build back pointers if needed. */
d991e6e8 9195 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
f8e93a2e 9196
9197 return NULL_TREE;
9198}
9199
dbf6c367 9200/* Handle the "nonnull" attribute. */
9201static tree
9a03a746 9202handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9203 tree args, int ARG_UNUSED (flags),
09347743 9204 bool *no_add_attrs)
dbf6c367 9205{
9206 tree type = *node;
9207 unsigned HOST_WIDE_INT attr_arg_num;
9208
9209 /* If no arguments are specified, all pointer arguments should be
d716ce75 9210 non-null. Verify a full prototype is given so that the arguments
1dc4d519 9211 will have the correct types when we actually check them later.
9212 Avoid diagnosing type-generic built-ins since those have no
9213 prototype. */
84166705 9214 if (!args)
dbf6c367 9215 {
1dc4d519 9216 if (!prototype_p (type)
9217 && (!TYPE_ATTRIBUTES (type)
9218 || !lookup_attribute ("type generic", TYPE_ATTRIBUTES (type))))
dbf6c367 9219 {
9220 error ("nonnull attribute without arguments on a non-prototype");
4ee9c684 9221 *no_add_attrs = true;
dbf6c367 9222 }
9223 return NULL_TREE;
9224 }
9225
9226 /* Argument list specified. Verify that each argument number references
9227 a pointer argument. */
caf62483 9228 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
dbf6c367 9229 {
4ee9c684 9230 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
dbf6c367 9231
caf62483 9232 tree arg = TREE_VALUE (args);
9233 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9234 && TREE_CODE (arg) != FUNCTION_DECL)
9235 arg = default_conversion (arg);
9236
9237 if (!get_nonnull_operand (arg, &arg_num))
dbf6c367 9238 {
07e3a3d2 9239 error ("nonnull argument has invalid operand number (argument %lu)",
dbf6c367 9240 (unsigned long) attr_arg_num);
9241 *no_add_attrs = true;
9242 return NULL_TREE;
9243 }
9244
d0af78c5 9245 if (prototype_p (type))
dbf6c367 9246 {
d0af78c5 9247 function_args_iterator iter;
9248 tree argument;
9249
9250 function_args_iter_init (&iter, type);
9251 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
dbf6c367 9252 {
d0af78c5 9253 argument = function_args_iter_cond (&iter);
9254 if (argument == NULL_TREE || ck_num == arg_num)
dbf6c367 9255 break;
dbf6c367 9256 }
9257
84166705 9258 if (!argument
d0af78c5 9259 || TREE_CODE (argument) == VOID_TYPE)
dbf6c367 9260 {
07e3a3d2 9261 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
dbf6c367 9262 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9263 *no_add_attrs = true;
9264 return NULL_TREE;
9265 }
9266
d0af78c5 9267 if (TREE_CODE (argument) != POINTER_TYPE)
dbf6c367 9268 {
07e3a3d2 9269 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
dbf6c367 9270 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9271 *no_add_attrs = true;
9272 return NULL_TREE;
9273 }
9274 }
9275 }
9276
9277 return NULL_TREE;
9278}
9279
9280/* Check the argument list of a function call for null in argument slots
d01f58f9 9281 that are marked as requiring a non-null pointer argument. The NARGS
bda05c57 9282 arguments are passed in the array ARGARRAY. */
dbf6c367 9283
9284static void
bda05c57 9285check_function_nonnull (location_t loc, tree attrs, int nargs, tree *argarray)
dbf6c367 9286{
9ca77b08 9287 tree a;
d01f58f9 9288 int i;
dbf6c367 9289
9ca77b08 9290 attrs = lookup_attribute ("nonnull", attrs);
9291 if (attrs == NULL_TREE)
9292 return;
9293
9294 a = attrs;
9295 /* See if any of the nonnull attributes has no arguments. If so,
9296 then every pointer argument is checked (in which case the check
9297 for pointer type is done in check_nonnull_arg). */
9298 if (TREE_VALUE (a) != NULL_TREE)
9299 do
9300 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9301 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9302
9303 if (a != NULL_TREE)
9304 for (i = 0; i < nargs; i++)
bda05c57 9305 check_function_arguments_recurse (check_nonnull_arg, &loc, argarray[i],
9ca77b08 9306 i + 1);
9307 else
dbf6c367 9308 {
9ca77b08 9309 /* Walk the argument list. If we encounter an argument number we
9310 should check for non-null, do it. */
9311 for (i = 0; i < nargs; i++)
dbf6c367 9312 {
9ca77b08 9313 for (a = attrs; ; a = TREE_CHAIN (a))
4ee9c684 9314 {
9ca77b08 9315 a = lookup_attribute ("nonnull", a);
9316 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9317 break;
4ee9c684 9318 }
9ca77b08 9319
9320 if (a != NULL_TREE)
bda05c57 9321 check_function_arguments_recurse (check_nonnull_arg, &loc,
9ca77b08 9322 argarray[i], i + 1);
dbf6c367 9323 }
9324 }
9325}
9326
50ca527f 9327/* Check that the Nth argument of a function call (counting backwards
d01f58f9 9328 from the end) is a (pointer)0. The NARGS arguments are passed in the
9329 array ARGARRAY. */
bf6c8de0 9330
9331static void
774e9d58 9332check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
bf6c8de0 9333{
774e9d58 9334 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
bf6c8de0 9335
9336 if (attr)
9337 {
d01f58f9 9338 int len = 0;
9339 int pos = 0;
9340 tree sentinel;
774e9d58 9341 function_args_iterator iter;
9342 tree t;
a0c938f0 9343
d01f58f9 9344 /* Skip over the named arguments. */
774e9d58 9345 FOREACH_FUNCTION_ARGS (fntype, t, iter)
a0c938f0 9346 {
774e9d58 9347 if (len == nargs)
9348 break;
d01f58f9 9349 len++;
9350 }
50ca527f 9351
d01f58f9 9352 if (TREE_VALUE (attr))
9353 {
9354 tree p = TREE_VALUE (TREE_VALUE (attr));
f9ae6f95 9355 pos = TREE_INT_CST_LOW (p);
d01f58f9 9356 }
50ca527f 9357
d01f58f9 9358 /* The sentinel must be one of the varargs, i.e.
9359 in position >= the number of fixed arguments. */
9360 if ((nargs - 1 - pos) < len)
9361 {
77a357e3 9362 warning (OPT_Wformat_,
d01f58f9 9363 "not enough variable arguments to fit a sentinel");
9364 return;
bf6c8de0 9365 }
d01f58f9 9366
9367 /* Validate the sentinel. */
9368 sentinel = argarray[nargs - 1 - pos];
9369 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9370 || !integer_zerop (sentinel))
9371 /* Although __null (in C++) is only an integer we allow it
9372 nevertheless, as we are guaranteed that it's exactly
9373 as wide as a pointer, and we don't want to force
9374 users to cast the NULL they have written there.
9375 We warn with -Wstrict-null-sentinel, though. */
9376 && (warn_strict_null_sentinel || null_node != sentinel))
77a357e3 9377 warning (OPT_Wformat_, "missing sentinel in function call");
bf6c8de0 9378 }
9379}
9380
dbf6c367 9381/* Helper for check_function_nonnull; given a list of operands which
9382 must be non-null in ARGS, determine if operand PARAM_NUM should be
9383 checked. */
9384
9385static bool
1cae46be 9386nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
dbf6c367 9387{
4ee9c684 9388 unsigned HOST_WIDE_INT arg_num = 0;
dbf6c367 9389
9390 for (; args; args = TREE_CHAIN (args))
9391 {
231bd014 9392 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9393
9394 gcc_assert (found);
dbf6c367 9395
9396 if (arg_num == param_num)
9397 return true;
9398 }
9399 return false;
9400}
9401
9402/* Check that the function argument PARAM (which is operand number
9403 PARAM_NUM) is non-null. This is called by check_function_nonnull
9404 via check_function_arguments_recurse. */
9405
9406static void
bda05c57 9407check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
dbf6c367 9408{
bda05c57 9409 location_t *ploc = (location_t *) ctx;
9410
dbf6c367 9411 /* Just skip checking the argument if it's not a pointer. This can
9412 happen if the "nonnull" attribute was given without an operand
9413 list (which means to check every pointer argument). */
9414
9415 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9416 return;
9417
9418 if (integer_zerop (param))
bda05c57 9419 warning_at (*ploc, OPT_Wnonnull, "null argument where non-null required "
9420 "(argument %lu)", (unsigned long) param_num);
dbf6c367 9421}
9422
9423/* Helper for nonnull attribute handling; fetch the operand number
9424 from the attribute argument list. */
9425
9426static bool
1cae46be 9427get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
dbf6c367 9428{
e913b5cd 9429 /* Verify the arg number is a small constant. */
e1d65c9f 9430 if (tree_fits_uhwi_p (arg_num_expr))
e913b5cd 9431 {
f9ae6f95 9432 *valp = TREE_INT_CST_LOW (arg_num_expr);
e913b5cd 9433 return true;
9434 }
9435 else
dbf6c367 9436 return false;
dbf6c367 9437}
fa987697 9438
9439/* Handle a "nothrow" attribute; arguments as in
9440 struct attribute_spec.handler. */
9441
9442static tree
9a03a746 9443handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9444 int ARG_UNUSED (flags), bool *no_add_attrs)
fa987697 9445{
9446 if (TREE_CODE (*node) == FUNCTION_DECL)
9447 TREE_NOTHROW (*node) = 1;
9448 /* ??? TODO: Support types. */
9449 else
9450 {
9b2d6d13 9451 warning (OPT_Wattributes, "%qE attribute ignored", name);
fa987697 9452 *no_add_attrs = true;
9453 }
9454
9455 return NULL_TREE;
9456}
7acb29a3 9457
9458/* Handle a "cleanup" attribute; arguments as in
9459 struct attribute_spec.handler. */
9460
9461static tree
1cae46be 9462handle_cleanup_attribute (tree *node, tree name, tree args,
9a03a746 9463 int ARG_UNUSED (flags), bool *no_add_attrs)
7acb29a3 9464{
9465 tree decl = *node;
9466 tree cleanup_id, cleanup_decl;
9467
9468 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9469 for global destructors in C++. This requires infrastructure that
9470 we don't have generically at the moment. It's also not a feature
9471 we'd be missing too much, since we do have attribute constructor. */
f48c7f4a 9472 if (!VAR_P (decl) || TREE_STATIC (decl))
7acb29a3 9473 {
9b2d6d13 9474 warning (OPT_Wattributes, "%qE attribute ignored", name);
7acb29a3 9475 *no_add_attrs = true;
9476 return NULL_TREE;
9477 }
9478
9479 /* Verify that the argument is a function in scope. */
9480 /* ??? We could support pointers to functions here as well, if
9481 that was considered desirable. */
9482 cleanup_id = TREE_VALUE (args);
9483 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9484 {
07e3a3d2 9485 error ("cleanup argument not an identifier");
7acb29a3 9486 *no_add_attrs = true;
9487 return NULL_TREE;
9488 }
d1c41717 9489 cleanup_decl = lookup_name (cleanup_id);
7acb29a3 9490 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9491 {
07e3a3d2 9492 error ("cleanup argument not a function");
7acb29a3 9493 *no_add_attrs = true;
9494 return NULL_TREE;
9495 }
9496
1cae46be 9497 /* That the function has proper type is checked with the
7acb29a3 9498 eventual call to build_function_call. */
9499
9500 return NULL_TREE;
9501}
8a8cdb8d 9502
9503/* Handle a "warn_unused_result" attribute. No special handling. */
9504
9505static tree
9506handle_warn_unused_result_attribute (tree *node, tree name,
9a03a746 9507 tree ARG_UNUSED (args),
9508 int ARG_UNUSED (flags), bool *no_add_attrs)
8a8cdb8d 9509{
9510 /* Ignore the attribute for functions not returning any value. */
9511 if (VOID_TYPE_P (TREE_TYPE (*node)))
9512 {
9b2d6d13 9513 warning (OPT_Wattributes, "%qE attribute ignored", name);
8a8cdb8d 9514 *no_add_attrs = true;
9515 }
9516
9517 return NULL_TREE;
9518}
bf6c8de0 9519
9520/* Handle a "sentinel" attribute. */
9521
9522static tree
50ca527f 9523handle_sentinel_attribute (tree *node, tree name, tree args,
bf6c8de0 9524 int ARG_UNUSED (flags), bool *no_add_attrs)
9525{
a36cf284 9526 if (!prototype_p (*node))
bf6c8de0 9527 {
9b2d6d13 9528 warning (OPT_Wattributes,
9529 "%qE attribute requires prototypes with named arguments", name);
bf6c8de0 9530 *no_add_attrs = true;
bf6c8de0 9531 }
50ca527f 9532 else
9533 {
c33080b9 9534 if (!stdarg_p (*node))
a0c938f0 9535 {
9b2d6d13 9536 warning (OPT_Wattributes,
9537 "%qE attribute only applies to variadic functions", name);
50ca527f 9538 *no_add_attrs = true;
9539 }
9540 }
a0c938f0 9541
50ca527f 9542 if (args)
bf6c8de0 9543 {
50ca527f 9544 tree position = TREE_VALUE (args);
3e5a8b00 9545 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9546 && TREE_CODE (position) != FUNCTION_DECL)
9547 position = default_conversion (position);
50ca527f 9548
3e5a8b00 9549 if (TREE_CODE (position) != INTEGER_CST
9550 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
a0c938f0 9551 {
48e1416a 9552 warning (OPT_Wattributes,
01b54db5 9553 "requested position is not an integer constant");
50ca527f 9554 *no_add_attrs = true;
9555 }
9556 else
a0c938f0 9557 {
50ca527f 9558 if (tree_int_cst_lt (position, integer_zero_node))
9559 {
01b54db5 9560 warning (OPT_Wattributes,
9561 "requested position is less than zero");
50ca527f 9562 *no_add_attrs = true;
9563 }
9564 }
bf6c8de0 9565 }
a0c938f0 9566
bf6c8de0 9567 return NULL_TREE;
9568}
b5c26b42 9569
9570/* Handle a "type_generic" attribute. */
9571
9572static tree
9573handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9574 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9575 bool * ARG_UNUSED (no_add_attrs))
9576{
19fbe3a4 9577 /* Ensure we have a function type. */
9578 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
48e1416a 9579
19fbe3a4 9580 /* Ensure we have a variadic function. */
c33080b9 9581 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
b5c26b42 9582
9583 return NULL_TREE;
9584}
46f8e3b0 9585
24470055 9586/* Handle a "target" attribute. */
46f8e3b0 9587
9588static tree
24470055 9589handle_target_attribute (tree *node, tree name, tree args, int flags,
46f8e3b0 9590 bool *no_add_attrs)
9591{
9592 /* Ensure we have a function type. */
9593 if (TREE_CODE (*node) != FUNCTION_DECL)
9594 {
9595 warning (OPT_Wattributes, "%qE attribute ignored", name);
9596 *no_add_attrs = true;
9597 }
ab50af2a 9598 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
9599 {
9600 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9601 "with %qs attribute", name, "target_clones");
9602 *no_add_attrs = true;
9603 }
46f8e3b0 9604 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
ae0c3984 9605 flags))
46f8e3b0 9606 *no_add_attrs = true;
9607
9608 return NULL_TREE;
9609}
9610
ab50af2a 9611/* Handle a "target_clones" attribute. */
9612
9613static tree
9614handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9615 int ARG_UNUSED (flags), bool *no_add_attrs)
9616{
9617 /* Ensure we have a function type. */
9618 if (TREE_CODE (*node) == FUNCTION_DECL)
9619 {
9620 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
9621 {
9622 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9623 "with %qs attribute", name, "always_inline");
9624 *no_add_attrs = true;
9625 }
9626 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
9627 {
9628 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9629 "with %qs attribute", name, "target");
9630 *no_add_attrs = true;
9631 }
9632 else
9633 /* Do not inline functions with multiple clone targets. */
9634 DECL_UNINLINABLE (*node) = 1;
9635 }
9636 else
9637 {
9638 warning (OPT_Wattributes, "%qE attribute ignored", name);
9639 *no_add_attrs = true;
9640 }
9641 return NULL_TREE;
9642}
9643
46f8e3b0 9644/* Arguments being collected for optimization. */
9645typedef const char *const_char_p; /* For DEF_VEC_P. */
f1f41a6c 9646static GTY(()) vec<const_char_p, va_gc> *optimize_args;
46f8e3b0 9647
9648
9649/* Inner function to convert a TREE_LIST to argv string to parse the optimize
9650 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9651 false for #pragma GCC optimize. */
9652
9653bool
9654parse_optimize_options (tree args, bool attr_p)
9655{
9656 bool ret = true;
9657 unsigned opt_argc;
9658 unsigned i;
9659 const char **opt_argv;
615ef0bb 9660 struct cl_decoded_option *decoded_options;
9661 unsigned int decoded_options_count;
46f8e3b0 9662 tree ap;
9663
9664 /* Build up argv vector. Just in case the string is stored away, use garbage
9665 collected strings. */
f1f41a6c 9666 vec_safe_truncate (optimize_args, 0);
9667 vec_safe_push (optimize_args, (const char *) NULL);
46f8e3b0 9668
9669 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9670 {
9671 tree value = TREE_VALUE (ap);
9672
9673 if (TREE_CODE (value) == INTEGER_CST)
9674 {
9675 char buffer[20];
f9ae6f95 9676 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
f1f41a6c 9677 vec_safe_push (optimize_args, ggc_strdup (buffer));
46f8e3b0 9678 }
9679
9680 else if (TREE_CODE (value) == STRING_CST)
9681 {
9682 /* Split string into multiple substrings. */
9683 size_t len = TREE_STRING_LENGTH (value);
9684 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9685 char *end = p + len;
9686 char *comma;
9687 char *next_p = p;
9688
9689 while (next_p != NULL)
9690 {
9691 size_t len2;
9692 char *q, *r;
9693
9694 p = next_p;
9695 comma = strchr (p, ',');
9696 if (comma)
9697 {
9698 len2 = comma - p;
9699 *comma = '\0';
9700 next_p = comma+1;
9701 }
9702 else
9703 {
9704 len2 = end - p;
9705 next_p = NULL;
9706 }
9707
ba72912a 9708 r = q = (char *) ggc_alloc_atomic (len2 + 3);
46f8e3b0 9709
9710 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9711 options. */
9712 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9713 {
9714 ret = false;
9715 if (attr_p)
9716 warning (OPT_Wattributes,
86a5f91c 9717 "bad option %qs to attribute %<optimize%>", p);
46f8e3b0 9718 else
9719 warning (OPT_Wpragmas,
86a5f91c 9720 "bad option %qs to pragma %<optimize%>", p);
46f8e3b0 9721 continue;
9722 }
9723
9724 if (*p != '-')
9725 {
9726 *r++ = '-';
9727
9728 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9729 itself is -Os, and any other switch begins with a -f. */
9730 if ((*p >= '0' && *p <= '9')
9731 || (p[0] == 's' && p[1] == '\0'))
9732 *r++ = 'O';
9733 else if (*p != 'O')
9734 *r++ = 'f';
9735 }
9736
9737 memcpy (r, p, len2);
9738 r[len2] = '\0';
f1f41a6c 9739 vec_safe_push (optimize_args, (const char *) q);
46f8e3b0 9740 }
9741
9742 }
9743 }
9744
f1f41a6c 9745 opt_argc = optimize_args->length ();
46f8e3b0 9746 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9747
9748 for (i = 1; i < opt_argc; i++)
f1f41a6c 9749 opt_argv[i] = (*optimize_args)[i];
46f8e3b0 9750
9751 /* Now parse the options. */
f3f006ad 9752 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9753 &decoded_options,
9754 &decoded_options_count);
b032c4dd 9755 /* Drop non-Optimization options. */
9756 unsigned j = 1;
9757 for (i = 1; i < decoded_options_count; ++i)
9758 {
9759 if (! (cl_options[decoded_options[i].opt_index].flags & CL_OPTIMIZATION))
9760 {
9761 ret = false;
9762 if (attr_p)
9763 warning (OPT_Wattributes,
86a5f91c 9764 "bad option %qs to attribute %<optimize%>",
b032c4dd 9765 decoded_options[i].orig_option_with_args_text);
9766 else
9767 warning (OPT_Wpragmas,
86a5f91c 9768 "bad option %qs to pragma %<optimize%>",
b032c4dd 9769 decoded_options[i].orig_option_with_args_text);
9770 continue;
9771 }
9772 if (i != j)
9773 decoded_options[j] = decoded_options[i];
9774 j++;
9775 }
9776 decoded_options_count = j;
9777 /* And apply them. */
f3f006ad 9778 decode_options (&global_options, &global_options_set,
3c6c0e40 9779 decoded_options, decoded_options_count,
9780 input_location, global_dc);
46f8e3b0 9781
4bec06b3 9782 targetm.override_options_after_change();
9783
f1f41a6c 9784 optimize_args->truncate (0);
46f8e3b0 9785 return ret;
9786}
9787
9788/* For handling "optimize" attribute. arguments as in
9789 struct attribute_spec.handler. */
9790
9791static tree
9792handle_optimize_attribute (tree *node, tree name, tree args,
9793 int ARG_UNUSED (flags), bool *no_add_attrs)
9794{
9795 /* Ensure we have a function type. */
9796 if (TREE_CODE (*node) != FUNCTION_DECL)
9797 {
9798 warning (OPT_Wattributes, "%qE attribute ignored", name);
9799 *no_add_attrs = true;
9800 }
9801 else
9802 {
9803 struct cl_optimization cur_opts;
9804 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9805
9806 /* Save current options. */
2c5d2e39 9807 cl_optimization_save (&cur_opts, &global_options);
46f8e3b0 9808
9809 /* If we previously had some optimization options, use them as the
9810 default. */
9811 if (old_opts)
2c5d2e39 9812 cl_optimization_restore (&global_options,
9813 TREE_OPTIMIZATION (old_opts));
46f8e3b0 9814
9815 /* Parse options, and update the vector. */
9816 parse_optimize_options (args, true);
9817 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
55310327 9818 = build_optimization_node (&global_options);
46f8e3b0 9819
9820 /* Restore current options. */
2c5d2e39 9821 cl_optimization_restore (&global_options, &cur_opts);
46f8e3b0 9822 }
9823
9824 return NULL_TREE;
9825}
48b14f50 9826
9827/* Handle a "no_split_stack" attribute. */
9828
9829static tree
9830handle_no_split_stack_attribute (tree *node, tree name,
9831 tree ARG_UNUSED (args),
9832 int ARG_UNUSED (flags),
9833 bool *no_add_attrs)
9834{
9835 tree decl = *node;
9836
9837 if (TREE_CODE (decl) != FUNCTION_DECL)
9838 {
9839 error_at (DECL_SOURCE_LOCATION (decl),
9840 "%qE attribute applies only to functions", name);
9841 *no_add_attrs = true;
9842 }
9843 else if (DECL_INITIAL (decl))
9844 {
9845 error_at (DECL_SOURCE_LOCATION (decl),
9846 "can%'t set %qE attribute after definition", name);
9847 *no_add_attrs = true;
9848 }
9849
9850 return NULL_TREE;
9851}
d7dcba40 9852
9853/* Handle a "returns_nonnull" attribute; arguments as in
9854 struct attribute_spec.handler. */
9855
9856static tree
9857handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9858 bool *no_add_attrs)
9859{
9860 // Even without a prototype we still have a return type we can check.
9861 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9862 {
9863 error ("returns_nonnull attribute on a function not returning a pointer");
9864 *no_add_attrs = true;
9865 }
9866 return NULL_TREE;
9867}
9868
74691f46 9869/* Handle a "designated_init" attribute; arguments as in
9870 struct attribute_spec.handler. */
9871
9872static tree
9873handle_designated_init_attribute (tree *node, tree name, tree, int,
9874 bool *no_add_attrs)
9875{
9876 if (TREE_CODE (*node) != RECORD_TYPE)
9877 {
9878 error ("%qE attribute is only valid on %<struct%> type", name);
9879 *no_add_attrs = true;
9880 }
9881 return NULL_TREE;
9882}
9883
dbf6c367 9884\f
774e9d58 9885/* Check for valid arguments being passed to a function with FNTYPE.
bda05c57 9886 There are NARGS arguments in the array ARGARRAY. LOC should be used for
9887 diagnostics. */
dbf6c367 9888void
bda05c57 9889check_function_arguments (location_t loc, const_tree fntype, int nargs,
9890 tree *argarray)
dbf6c367 9891{
9892 /* Check for null being passed in a pointer argument that must be
9893 non-null. We also need to do this if format checking is enabled. */
9894
9895 if (warn_nonnull)
bda05c57 9896 check_function_nonnull (loc, TYPE_ATTRIBUTES (fntype), nargs, argarray);
dbf6c367 9897
9898 /* Check for errors in format strings. */
9899
068bea1e 9900 if (warn_format || warn_suggest_attribute_format)
774e9d58 9901 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
95c90e04 9902
9903 if (warn_format)
774e9d58 9904 check_function_sentinel (fntype, nargs, argarray);
dbf6c367 9905}
9906
9907/* Generic argument checking recursion routine. PARAM is the argument to
9908 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9909 once the argument is resolved. CTX is context for the callback. */
9910void
1cae46be 9911check_function_arguments_recurse (void (*callback)
9912 (void *, tree, unsigned HOST_WIDE_INT),
9913 void *ctx, tree param,
9914 unsigned HOST_WIDE_INT param_num)
dbf6c367 9915{
72dd6141 9916 if (CONVERT_EXPR_P (param)
c44afe23 9917 && (TYPE_PRECISION (TREE_TYPE (param))
9918 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
dbf6c367 9919 {
9920 /* Strip coercion. */
9921 check_function_arguments_recurse (callback, ctx,
4ee9c684 9922 TREE_OPERAND (param, 0), param_num);
dbf6c367 9923 return;
9924 }
9925
9926 if (TREE_CODE (param) == CALL_EXPR)
9927 {
c2f47e15 9928 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
dbf6c367 9929 tree attrs;
9930 bool found_format_arg = false;
9931
9932 /* See if this is a call to a known internationalization function
9933 that modifies a format arg. Such a function may have multiple
9934 format_arg attributes (for example, ngettext). */
9935
9936 for (attrs = TYPE_ATTRIBUTES (type);
9937 attrs;
9938 attrs = TREE_CHAIN (attrs))
9939 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9940 {
c2f47e15 9941 tree inner_arg;
dbf6c367 9942 tree format_num_expr;
9943 int format_num;
9944 int i;
c2f47e15 9945 call_expr_arg_iterator iter;
dbf6c367 9946
9947 /* Extract the argument number, which was previously checked
9948 to be valid. */
9949 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
ddb1be65 9950
e913b5cd 9951 format_num = tree_to_uhwi (format_num_expr);
dbf6c367 9952
c2f47e15 9953 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9954 inner_arg != 0;
9955 inner_arg = next_call_expr_arg (&iter), i++)
dbf6c367 9956 if (i == format_num)
9957 {
9958 check_function_arguments_recurse (callback, ctx,
c2f47e15 9959 inner_arg, param_num);
dbf6c367 9960 found_format_arg = true;
9961 break;
9962 }
9963 }
9964
9965 /* If we found a format_arg attribute and did a recursive check,
9966 we are done with checking this argument. Otherwise, we continue
9967 and this will be considered a non-literal. */
9968 if (found_format_arg)
9969 return;
9970 }
9971
9972 if (TREE_CODE (param) == COND_EXPR)
9973 {
f6dfb86a 9974 /* Simplify to avoid warning for an impossible case. */
9975 param = fold_for_warn (param);
9976 if (TREE_CODE (param) == COND_EXPR)
9977 {
9978 /* Check both halves of the conditional expression. */
9979 check_function_arguments_recurse (callback, ctx,
9980 TREE_OPERAND (param, 1),
9981 param_num);
9982 check_function_arguments_recurse (callback, ctx,
9983 TREE_OPERAND (param, 2),
9984 param_num);
9985 return;
9986 }
dbf6c367 9987 }
9988
9989 (*callback) (ctx, param, param_num);
9990}
1f3233d1 9991
60cce472 9992/* Checks for a builtin function FNDECL that the number of arguments
9993 NARGS against the required number REQUIRED and issues an error if
9994 there is a mismatch. Returns true if the number of arguments is
5d4db8ef 9995 correct, otherwise false. LOC is the location of FNDECL. */
d43cee80 9996
9997static bool
5d4db8ef 9998builtin_function_validate_nargs (location_t loc, tree fndecl, int nargs,
9999 int required)
d43cee80 10000{
10001 if (nargs < required)
10002 {
23b101c5 10003 error_at (loc, "too few arguments to function %qE", fndecl);
d43cee80 10004 return false;
10005 }
10006 else if (nargs > required)
10007 {
5d4db8ef 10008 error_at (loc, "too many arguments to function %qE", fndecl);
d43cee80 10009 return false;
10010 }
10011 return true;
10012}
10013
5d4db8ef 10014/* Helper macro for check_builtin_function_arguments. */
10015#define ARG_LOCATION(N) \
10016 (arg_loc.is_empty () \
10017 ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
10018 : expansion_point_location (arg_loc[(N)]))
10019
d43cee80 10020/* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
5d4db8ef 10021 Returns false if there was an error, otherwise true. LOC is the
10022 location of the function; ARG_LOC is a vector of locations of the
10023 arguments. */
d43cee80 10024
10025bool
5d4db8ef 10026check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
10027 tree fndecl, int nargs, tree *args)
d43cee80 10028{
10029 if (!DECL_BUILT_IN (fndecl)
10030 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
10031 return true;
10032
10033 switch (DECL_FUNCTION_CODE (fndecl))
10034 {
8c38d887 10035 case BUILT_IN_ALLOCA_WITH_ALIGN:
10036 {
10037 /* Get the requested alignment (in bits) if it's a constant
10038 integer expression. */
10039 unsigned HOST_WIDE_INT align
10040 = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
10041
10042 /* Determine if the requested alignment is a power of 2. */
10043 if ((align & (align - 1)))
10044 align = 0;
10045
10046 /* The maximum alignment in bits corresponding to the same
10047 maximum in bytes enforced in check_user_alignment(). */
10048 unsigned maxalign = (UINT_MAX >> 1) + 1;
5d4db8ef 10049
8c38d887 10050 /* Reject invalid alignments. */
10051 if (align < BITS_PER_UNIT || maxalign < align)
10052 {
5d4db8ef 10053 error_at (ARG_LOCATION (1),
8c38d887 10054 "second argument to function %qE must be a constant "
10055 "integer power of 2 between %qi and %qu bits",
10056 fndecl, BITS_PER_UNIT, maxalign);
10057 return false;
10058 }
5d4db8ef 10059 return true;
8c38d887 10060 }
10061
d43cee80 10062 case BUILT_IN_CONSTANT_P:
5d4db8ef 10063 return builtin_function_validate_nargs (loc, fndecl, nargs, 1);
d43cee80 10064
10065 case BUILT_IN_ISFINITE:
10066 case BUILT_IN_ISINF:
c319d56a 10067 case BUILT_IN_ISINF_SIGN:
d43cee80 10068 case BUILT_IN_ISNAN:
10069 case BUILT_IN_ISNORMAL:
10902624 10070 case BUILT_IN_SIGNBIT:
5d4db8ef 10071 if (builtin_function_validate_nargs (loc, fndecl, nargs, 1))
d43cee80 10072 {
10073 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
10074 {
5d4db8ef 10075 error_at (ARG_LOCATION (0), "non-floating-point argument in "
10076 "call to function %qE", fndecl);
d43cee80 10077 return false;
10078 }
10079 return true;
10080 }
10081 return false;
10082
10083 case BUILT_IN_ISGREATER:
10084 case BUILT_IN_ISGREATEREQUAL:
10085 case BUILT_IN_ISLESS:
10086 case BUILT_IN_ISLESSEQUAL:
10087 case BUILT_IN_ISLESSGREATER:
10088 case BUILT_IN_ISUNORDERED:
5d4db8ef 10089 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2))
d43cee80 10090 {
10091 enum tree_code code0, code1;
10092 code0 = TREE_CODE (TREE_TYPE (args[0]));
10093 code1 = TREE_CODE (TREE_TYPE (args[1]));
10094 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
10095 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10096 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
10097 {
5d4db8ef 10098 error_at (loc, "non-floating-point arguments in call to "
10099 "function %qE", fndecl);
d43cee80 10100 return false;
10101 }
10102 return true;
10103 }
10104 return false;
10105
19fbe3a4 10106 case BUILT_IN_FPCLASSIFY:
5d4db8ef 10107 if (builtin_function_validate_nargs (loc, fndecl, nargs, 6))
19fbe3a4 10108 {
5d4db8ef 10109 for (unsigned int i = 0; i < 5; i++)
19fbe3a4 10110 if (TREE_CODE (args[i]) != INTEGER_CST)
10111 {
5d4db8ef 10112 error_at (ARG_LOCATION (i), "non-const integer argument %u in "
10113 "call to function %qE", i + 1, fndecl);
19fbe3a4 10114 return false;
10115 }
10116
10117 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
10118 {
5d4db8ef 10119 error_at (ARG_LOCATION (5), "non-floating-point argument in "
10120 "call to function %qE", fndecl);
19fbe3a4 10121 return false;
10122 }
10123 return true;
10124 }
10125 return false;
10126
fca0886c 10127 case BUILT_IN_ASSUME_ALIGNED:
5d4db8ef 10128 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2 + (nargs > 2)))
fca0886c 10129 {
10130 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
10131 {
5d4db8ef 10132 error_at (ARG_LOCATION (2), "non-integer argument 3 in call to "
10133 "function %qE", fndecl);
fca0886c 10134 return false;
10135 }
10136 return true;
10137 }
10138 return false;
10139
0c93c8a9 10140 case BUILT_IN_ADD_OVERFLOW:
10141 case BUILT_IN_SUB_OVERFLOW:
10142 case BUILT_IN_MUL_OVERFLOW:
5d4db8ef 10143 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
0c93c8a9 10144 {
10145 unsigned i;
10146 for (i = 0; i < 2; i++)
10147 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
10148 {
5d4db8ef 10149 error_at (ARG_LOCATION (i), "argument %u in call to function "
10150 "%qE does not have integral type", i + 1, fndecl);
0c93c8a9 10151 return false;
10152 }
10153 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
77104764 10154 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args[2]))))
0c93c8a9 10155 {
5d4db8ef 10156 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
77104764 10157 "does not have pointer to integral type", fndecl);
10158 return false;
10159 }
10160 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == ENUMERAL_TYPE)
10161 {
10162 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
10163 "has pointer to enumerated type", fndecl);
10164 return false;
10165 }
10166 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == BOOLEAN_TYPE)
10167 {
10168 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
10169 "has pointer to boolean type", fndecl);
0c93c8a9 10170 return false;
10171 }
10172 return true;
10173 }
10174 return false;
10175
732905bb 10176 case BUILT_IN_ADD_OVERFLOW_P:
10177 case BUILT_IN_SUB_OVERFLOW_P:
10178 case BUILT_IN_MUL_OVERFLOW_P:
10179 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
10180 {
10181 unsigned i;
10182 for (i = 0; i < 3; i++)
10183 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
10184 {
10185 error_at (ARG_LOCATION (i), "argument %u in call to function "
10186 "%qE does not have integral type", i + 1, fndecl);
10187 return false;
10188 }
77104764 10189 if (TREE_CODE (TREE_TYPE (args[2])) == ENUMERAL_TYPE)
10190 {
10191 error_at (ARG_LOCATION (2), "argument 3 in call to function "
10192 "%qE has enumerated type", fndecl);
10193 return false;
10194 }
10195 else if (TREE_CODE (TREE_TYPE (args[2])) == BOOLEAN_TYPE)
10196 {
10197 error_at (ARG_LOCATION (2), "argument 3 in call to function "
10198 "%qE has boolean type", fndecl);
10199 return false;
10200 }
732905bb 10201 return true;
10202 }
10203 return false;
10204
d43cee80 10205 default:
10206 return true;
10207 }
10208}
10209
860251be 10210/* Function to help qsort sort FIELD_DECLs by name order. */
10211
10212int
10213field_decl_cmp (const void *x_p, const void *y_p)
10214{
4fd61bc6 10215 const tree *const x = (const tree *const) x_p;
10216 const tree *const y = (const tree *const) y_p;
10217
860251be 10218 if (DECL_NAME (*x) == DECL_NAME (*y))
10219 /* A nontype is "greater" than a type. */
10220 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10221 if (DECL_NAME (*x) == NULL_TREE)
10222 return -1;
10223 if (DECL_NAME (*y) == NULL_TREE)
10224 return 1;
10225 if (DECL_NAME (*x) < DECL_NAME (*y))
10226 return -1;
10227 return 1;
10228}
10229
10230static struct {
10231 gt_pointer_operator new_value;
10232 void *cookie;
10233} resort_data;
10234
10235/* This routine compares two fields like field_decl_cmp but using the
10236pointer operator in resort_data. */
10237
10238static int
10239resort_field_decl_cmp (const void *x_p, const void *y_p)
10240{
4fd61bc6 10241 const tree *const x = (const tree *const) x_p;
10242 const tree *const y = (const tree *const) y_p;
860251be 10243
10244 if (DECL_NAME (*x) == DECL_NAME (*y))
10245 /* A nontype is "greater" than a type. */
10246 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10247 if (DECL_NAME (*x) == NULL_TREE)
10248 return -1;
10249 if (DECL_NAME (*y) == NULL_TREE)
10250 return 1;
10251 {
10252 tree d1 = DECL_NAME (*x);
10253 tree d2 = DECL_NAME (*y);
10254 resort_data.new_value (&d1, resort_data.cookie);
10255 resort_data.new_value (&d2, resort_data.cookie);
10256 if (d1 < d2)
10257 return -1;
10258 }
10259 return 1;
10260}
10261
10262/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10263
10264void
10265resort_sorted_fields (void *obj,
9a03a746 10266 void * ARG_UNUSED (orig_obj),
4ee9c684 10267 gt_pointer_operator new_value,
10268 void *cookie)
860251be 10269{
9a03a746 10270 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
860251be 10271 resort_data.new_value = new_value;
10272 resort_data.cookie = cookie;
10273 qsort (&sf->elts[0], sf->len, sizeof (tree),
4ee9c684 10274 resort_field_decl_cmp);
860251be 10275}
10276
209c9752 10277/* Subroutine of c_parse_error.
10278 Return the result of concatenating LHS and RHS. RHS is really
10279 a string literal, its first character is indicated by RHS_START and
cfee01e3 10280 RHS_SIZE is its length (including the terminating NUL character).
209c9752 10281
10282 The caller is responsible for deleting the returned pointer. */
10283
10284static char *
10285catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10286{
10287 const int lhs_size = strlen (lhs);
10288 char *result = XNEWVEC (char, lhs_size + rhs_size);
10289 strncpy (result, lhs, lhs_size);
10290 strncpy (result + lhs_size, rhs_start, rhs_size);
10291 return result;
10292}
10293
380c6697 10294/* Issue the error given by GMSGID, indicating that it occurred before
92b128ed 10295 TOKEN, which had the associated VALUE. */
10296
10297void
48e1416a 10298c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
ba99525e 10299 tree value, unsigned char token_flags)
92b128ed 10300{
209c9752 10301#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10302
10303 char *message = NULL;
92b128ed 10304
ba99525e 10305 if (token_type == CPP_EOF)
380c6697 10306 message = catenate_messages (gmsgid, " at end of input");
48e1416a 10307 else if (token_type == CPP_CHAR
10308 || token_type == CPP_WCHAR
ba99525e 10309 || token_type == CPP_CHAR16
30b1ba42 10310 || token_type == CPP_CHAR32
10311 || token_type == CPP_UTF8CHAR)
92b128ed 10312 {
f9ae6f95 10313 unsigned int val = TREE_INT_CST_LOW (value);
924bbf02 10314 const char *prefix;
10315
ba99525e 10316 switch (token_type)
924bbf02 10317 {
10318 default:
10319 prefix = "";
10320 break;
10321 case CPP_WCHAR:
10322 prefix = "L";
10323 break;
10324 case CPP_CHAR16:
10325 prefix = "u";
10326 break;
10327 case CPP_CHAR32:
10328 prefix = "U";
10329 break;
30b1ba42 10330 case CPP_UTF8CHAR:
10331 prefix = "u8";
10332 break;
924bbf02 10333 }
10334
92b128ed 10335 if (val <= UCHAR_MAX && ISGRAPH (val))
a0c938f0 10336 message = catenate_messages (gmsgid, " before %s'%c'");
92b128ed 10337 else
a0c938f0 10338 message = catenate_messages (gmsgid, " before %s'\\x%x'");
209c9752 10339
924bbf02 10340 error (message, prefix, val);
209c9752 10341 free (message);
10342 message = NULL;
92b128ed 10343 }
1898176c 10344 else if (token_type == CPP_CHAR_USERDEF
10345 || token_type == CPP_WCHAR_USERDEF
10346 || token_type == CPP_CHAR16_USERDEF
30b1ba42 10347 || token_type == CPP_CHAR32_USERDEF
10348 || token_type == CPP_UTF8CHAR_USERDEF)
1898176c 10349 message = catenate_messages (gmsgid,
10350 " before user-defined character literal");
10351 else if (token_type == CPP_STRING_USERDEF
10352 || token_type == CPP_WSTRING_USERDEF
10353 || token_type == CPP_STRING16_USERDEF
10354 || token_type == CPP_STRING32_USERDEF
10355 || token_type == CPP_UTF8STRING_USERDEF)
10356 message = catenate_messages (gmsgid, " before user-defined string literal");
48e1416a 10357 else if (token_type == CPP_STRING
10358 || token_type == CPP_WSTRING
ba99525e 10359 || token_type == CPP_STRING16
538ba11a 10360 || token_type == CPP_STRING32
10361 || token_type == CPP_UTF8STRING)
380c6697 10362 message = catenate_messages (gmsgid, " before string constant");
ba99525e 10363 else if (token_type == CPP_NUMBER)
380c6697 10364 message = catenate_messages (gmsgid, " before numeric constant");
ba99525e 10365 else if (token_type == CPP_NAME)
209c9752 10366 {
380c6697 10367 message = catenate_messages (gmsgid, " before %qE");
782858b8 10368 error (message, value);
209c9752 10369 free (message);
10370 message = NULL;
10371 }
ba99525e 10372 else if (token_type == CPP_PRAGMA)
b75b98aa 10373 message = catenate_messages (gmsgid, " before %<#pragma%>");
ba99525e 10374 else if (token_type == CPP_PRAGMA_EOL)
b75b98aa 10375 message = catenate_messages (gmsgid, " before end of line");
07b8f133 10376 else if (token_type == CPP_DECLTYPE)
10377 message = catenate_messages (gmsgid, " before %<decltype%>");
ba99525e 10378 else if (token_type < N_TTYPES)
209c9752 10379 {
380c6697 10380 message = catenate_messages (gmsgid, " before %qs token");
ba99525e 10381 error (message, cpp_type2name (token_type, token_flags));
209c9752 10382 free (message);
10383 message = NULL;
10384 }
92b128ed 10385 else
380c6697 10386 error (gmsgid);
209c9752 10387
10388 if (message)
10389 {
10390 error (message);
10391 free (message);
10392 }
a0c938f0 10393#undef catenate_messages
92b128ed 10394}
10395
3a79f5da 10396/* Return the gcc option code associated with the reason for a cpp
10397 message, or 0 if none. */
10398
10399static int
10400c_option_controlling_cpp_error (int reason)
10401{
7ff8db31 10402 const struct cpp_reason_option_codes_t *entry;
3a79f5da 10403
7ff8db31 10404 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
3a79f5da 10405 {
10406 if (entry->reason == reason)
10407 return entry->option_code;
10408 }
10409 return 0;
10410}
10411
7f5f3953 10412/* Callback from cpp_error for PFILE to print diagnostics from the
3a79f5da 10413 preprocessor. The diagnostic is of type LEVEL, with REASON set
10414 to the reason code if LEVEL is represents a warning, at location
f0479000 10415 RICHLOC unless this is after lexing and the compiler's location
10416 should be used instead; MSG is the translated message and AP
7f5f3953 10417 the arguments. Returns true if a diagnostic was emitted, false
10418 otherwise. */
10419
10420bool
3a79f5da 10421c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
f0479000 10422 rich_location *richloc,
7f5f3953 10423 const char *msg, va_list *ap)
10424{
10425 diagnostic_info diagnostic;
10426 diagnostic_t dlevel;
5ae82d58 10427 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
7f5f3953 10428 bool ret;
10429
10430 switch (level)
10431 {
10432 case CPP_DL_WARNING_SYSHDR:
10433 if (flag_no_output)
10434 return false;
5ae82d58 10435 global_dc->dc_warn_system_headers = 1;
7f5f3953 10436 /* Fall through. */
10437 case CPP_DL_WARNING:
10438 if (flag_no_output)
10439 return false;
10440 dlevel = DK_WARNING;
10441 break;
10442 case CPP_DL_PEDWARN:
10443 if (flag_no_output && !flag_pedantic_errors)
10444 return false;
10445 dlevel = DK_PEDWARN;
10446 break;
10447 case CPP_DL_ERROR:
10448 dlevel = DK_ERROR;
10449 break;
10450 case CPP_DL_ICE:
10451 dlevel = DK_ICE;
10452 break;
10453 case CPP_DL_NOTE:
10454 dlevel = DK_NOTE;
10455 break;
ff903809 10456 case CPP_DL_FATAL:
10457 dlevel = DK_FATAL;
10458 break;
7f5f3953 10459 default:
10460 gcc_unreachable ();
10461 }
10462 if (done_lexing)
d0f713f4 10463 richloc->set_range (line_table, 0, input_location, true);
7f5f3953 10464 diagnostic_set_info_translated (&diagnostic, msg, ap,
f0479000 10465 richloc, dlevel);
3a79f5da 10466 diagnostic_override_option_index (&diagnostic,
10467 c_option_controlling_cpp_error (reason));
7f5f3953 10468 ret = report_diagnostic (&diagnostic);
10469 if (level == CPP_DL_WARNING_SYSHDR)
5ae82d58 10470 global_dc->dc_warn_system_headers = save_warn_system_headers;
7f5f3953 10471 return ret;
10472}
10473
624d37a6 10474/* Convert a character from the host to the target execution character
10475 set. cpplib handles this, mostly. */
10476
10477HOST_WIDE_INT
10478c_common_to_target_charset (HOST_WIDE_INT c)
10479{
10480 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10481 zero-extended under -fno-signed-char. cpplib insists that characters
10482 and character constants are always unsigned. Hence we must convert
10483 back and forth. */
10484 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10485
10486 uc = cpp_host_to_exec_charset (parse_in, uc);
10487
10488 if (flag_signed_char)
10489 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10490 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10491 else
10492 return uc;
10493}
10494
7549df0d 10495/* Fold an offsetof-like expression. EXPR is a nested sequence of component
10496 references with an INDIRECT_REF of a constant at the bottom; much like the
10497 traditional rendering of offsetof as a macro. Return the folded result. */
af28855b 10498
7549df0d 10499tree
aeaccb75 10500fold_offsetof_1 (tree expr, enum tree_code ctx)
af28855b 10501{
af28855b 10502 tree base, off, t;
aeaccb75 10503 tree_code code = TREE_CODE (expr);
10504 switch (code)
af28855b 10505 {
10506 case ERROR_MARK:
10507 return expr;
10508
6b11d2e3 10509 case VAR_DECL:
10510 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10511 return error_mark_node;
10512
d897f7c2 10513 case CALL_EXPR:
cf1a89a3 10514 case TARGET_EXPR:
d897f7c2 10515 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10516 return error_mark_node;
10517
d897f7c2 10518 case NOP_EXPR:
10519 case INDIRECT_REF:
7549df0d 10520 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
64ed018c 10521 {
10522 error ("cannot apply %<offsetof%> to a non constant address");
10523 return error_mark_node;
10524 }
7549df0d 10525 return TREE_OPERAND (expr, 0);
d897f7c2 10526
af28855b 10527 case COMPONENT_REF:
aeaccb75 10528 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
af28855b 10529 if (base == error_mark_node)
10530 return base;
10531
10532 t = TREE_OPERAND (expr, 1);
10533 if (DECL_C_BIT_FIELD (t))
10534 {
10535 error ("attempt to take address of bit-field structure "
782858b8 10536 "member %qD", t);
af28855b 10537 return error_mark_node;
10538 }
389dd41b 10539 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
e913b5cd 10540 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
389dd41b 10541 / BITS_PER_UNIT));
af28855b 10542 break;
10543
10544 case ARRAY_REF:
aeaccb75 10545 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
af28855b 10546 if (base == error_mark_node)
10547 return base;
10548
10549 t = TREE_OPERAND (expr, 1);
64ed018c 10550
10551 /* Check if the offset goes beyond the upper bound of the array. */
7549df0d 10552 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
e0559d69 10553 {
10554 tree upbound = array_ref_up_bound (expr);
10555 if (upbound != NULL_TREE
10556 && TREE_CODE (upbound) == INTEGER_CST
10557 && !tree_int_cst_equal (upbound,
10558 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10559 {
aeaccb75 10560 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
10561 upbound = size_binop (PLUS_EXPR, upbound,
10562 build_int_cst (TREE_TYPE (upbound), 1));
e0559d69 10563 if (tree_int_cst_lt (upbound, t))
10564 {
10565 tree v;
10566
10567 for (v = TREE_OPERAND (expr, 0);
10568 TREE_CODE (v) == COMPONENT_REF;
10569 v = TREE_OPERAND (v, 0))
10570 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10571 == RECORD_TYPE)
10572 {
1767a056 10573 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10574 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
e0559d69 10575 if (TREE_CODE (fld_chain) == FIELD_DECL)
10576 break;
10577
10578 if (fld_chain)
10579 break;
10580 }
10581 /* Don't warn if the array might be considered a poor
10582 man's flexible array member with a very permissive
10583 definition thereof. */
10584 if (TREE_CODE (v) == ARRAY_REF
10585 || TREE_CODE (v) == COMPONENT_REF)
10586 warning (OPT_Warray_bounds,
10587 "index %E denotes an offset "
10588 "greater than size of %qT",
10589 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10590 }
10591 }
10592 }
7549df0d 10593
10594 t = convert (sizetype, t);
10595 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
af28855b 10596 break;
10597
ede90cc2 10598 case COMPOUND_EXPR:
10599 /* Handle static members of volatile structs. */
10600 t = TREE_OPERAND (expr, 1);
f48c7f4a 10601 gcc_assert (VAR_P (t));
7549df0d 10602 return fold_offsetof_1 (t);
ede90cc2 10603
af28855b 10604 default:
231bd014 10605 gcc_unreachable ();
af28855b 10606 }
10607
7549df0d 10608 return fold_build_pointer_plus (base, off);
af28855b 10609}
10610
7549df0d 10611/* Likewise, but convert it to the return type of offsetof. */
10612
af28855b 10613tree
7549df0d 10614fold_offsetof (tree expr)
af28855b 10615{
7549df0d 10616 return convert (size_type_node, fold_offsetof_1 (expr));
af28855b 10617}
10618
b9bdfa0b 10619/* Warn for A ?: C expressions (with B omitted) where A is a boolean
10620 expression, because B will always be true. */
10621
10622void
10623warn_for_omitted_condop (location_t location, tree cond)
10624{
10625 if (truth_value_p (TREE_CODE (cond)))
10626 warning_at (location, OPT_Wparentheses,
10627 "the omitted middle operand in ?: will always be %<true%>, "
10628 "suggest explicit middle operand");
10629}
10630
a1f90215 10631/* Give an error for storing into ARG, which is 'const'. USE indicates
10632 how ARG was being used. */
10633
10634void
f2697631 10635readonly_error (location_t loc, tree arg, enum lvalue_use use)
a1f90215 10636{
10637 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10638 || use == lv_asm);
10639 /* Using this macro rather than (for example) arrays of messages
10640 ensures that all the format strings are checked at compile
10641 time. */
10642#define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10643 : (use == lv_increment ? (I) \
10644 : (use == lv_decrement ? (D) : (AS))))
10645 if (TREE_CODE (arg) == COMPONENT_REF)
10646 {
10647 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
f2697631 10648 error_at (loc, READONLY_MSG (G_("assignment of member "
10649 "%qD in read-only object"),
10650 G_("increment of member "
10651 "%qD in read-only object"),
10652 G_("decrement of member "
10653 "%qD in read-only object"),
10654 G_("member %qD in read-only object "
10655 "used as %<asm%> output")),
10656 TREE_OPERAND (arg, 1));
a1f90215 10657 else
f2697631 10658 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10659 G_("increment of read-only member %qD"),
10660 G_("decrement of read-only member %qD"),
10661 G_("read-only member %qD used as %<asm%> output")),
10662 TREE_OPERAND (arg, 1));
a1f90215 10663 }
f48c7f4a 10664 else if (VAR_P (arg))
f2697631 10665 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10666 G_("increment of read-only variable %qD"),
10667 G_("decrement of read-only variable %qD"),
10668 G_("read-only variable %qD used as %<asm%> output")),
10669 arg);
a1f90215 10670 else if (TREE_CODE (arg) == PARM_DECL)
f2697631 10671 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10672 G_("increment of read-only parameter %qD"),
10673 G_("decrement of read-only parameter %qD"),
10674 G_("read-only parameter %qD use as %<asm%> output")),
10675 arg);
a1f90215 10676 else if (TREE_CODE (arg) == RESULT_DECL)
10677 {
10678 gcc_assert (c_dialect_cxx ());
f2697631 10679 error_at (loc, READONLY_MSG (G_("assignment of "
10680 "read-only named return value %qD"),
10681 G_("increment of "
10682 "read-only named return value %qD"),
10683 G_("decrement of "
10684 "read-only named return value %qD"),
10685 G_("read-only named return value %qD "
10686 "used as %<asm%>output")),
10687 arg);
a1f90215 10688 }
10689 else if (TREE_CODE (arg) == FUNCTION_DECL)
f2697631 10690 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10691 G_("increment of function %qD"),
10692 G_("decrement of function %qD"),
10693 G_("function %qD used as %<asm%> output")),
10694 arg);
a1f90215 10695 else
f2697631 10696 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10697 G_("increment of read-only location %qE"),
10698 G_("decrement of read-only location %qE"),
10699 G_("read-only location %qE used as %<asm%> output")),
10700 arg);
a1f90215 10701}
10702
e35976b1 10703/* Print an error message for an invalid lvalue. USE says
fdd84b77 10704 how the lvalue is being used and so selects the error message. LOC
10705 is the location for the error. */
ab6bb714 10706
e35976b1 10707void
fdd84b77 10708lvalue_error (location_t loc, enum lvalue_use use)
ab6bb714 10709{
e35976b1 10710 switch (use)
ab6bb714 10711 {
e35976b1 10712 case lv_assign:
fdd84b77 10713 error_at (loc, "lvalue required as left operand of assignment");
e35976b1 10714 break;
10715 case lv_increment:
fdd84b77 10716 error_at (loc, "lvalue required as increment operand");
e35976b1 10717 break;
10718 case lv_decrement:
fdd84b77 10719 error_at (loc, "lvalue required as decrement operand");
e35976b1 10720 break;
10721 case lv_addressof:
fdd84b77 10722 error_at (loc, "lvalue required as unary %<&%> operand");
e35976b1 10723 break;
10724 case lv_asm:
fdd84b77 10725 error_at (loc, "lvalue required in asm statement");
e35976b1 10726 break;
10727 default:
10728 gcc_unreachable ();
ab6bb714 10729 }
ab6bb714 10730}
b1bbc8e5 10731
10732/* Print an error message for an invalid indirection of type TYPE.
10733 ERRSTRING is the name of the operator for the indirection. */
10734
10735void
10736invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10737{
10738 switch (errstring)
10739 {
10740 case RO_NULL:
10741 gcc_assert (c_dialect_cxx ());
10742 error_at (loc, "invalid type argument (have %qT)", type);
10743 break;
10744 case RO_ARRAY_INDEXING:
10745 error_at (loc,
10746 "invalid type argument of array indexing (have %qT)",
10747 type);
10748 break;
10749 case RO_UNARY_STAR:
10750 error_at (loc,
10751 "invalid type argument of unary %<*%> (have %qT)",
10752 type);
10753 break;
10754 case RO_ARROW:
10755 error_at (loc,
10756 "invalid type argument of %<->%> (have %qT)",
10757 type);
10758 break;
7354a89b 10759 case RO_ARROW_STAR:
10760 error_at (loc,
10761 "invalid type argument of %<->*%> (have %qT)",
10762 type);
10763 break;
b1bbc8e5 10764 case RO_IMPLICIT_CONVERSION:
10765 error_at (loc,
10766 "invalid type argument of implicit conversion (have %qT)",
10767 type);
10768 break;
10769 default:
10770 gcc_unreachable ();
10771 }
10772}
c271bdb2 10773\f
10774/* *PTYPE is an incomplete array. Complete it with a domain based on
10775 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10776 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10777 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10778
10779int
10780complete_array_type (tree *ptype, tree initial_value, bool do_default)
10781{
10782 tree maxindex, type, main_type, elt, unqual_elt;
10783 int failure = 0, quals;
6753bca0 10784 hashval_t hashcode = 0;
f5298614 10785 bool overflow_p = false;
c271bdb2 10786
10787 maxindex = size_zero_node;
10788 if (initial_value)
10789 {
10790 if (TREE_CODE (initial_value) == STRING_CST)
10791 {
10792 int eltsize
10793 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10794 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10795 }
10796 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10797 {
f1f41a6c 10798 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
c271bdb2 10799
f1f41a6c 10800 if (vec_safe_is_empty (v))
c271bdb2 10801 {
10802 if (pedantic)
10803 failure = 3;
7542c3b4 10804 maxindex = ssize_int (-1);
c271bdb2 10805 }
10806 else
10807 {
10808 tree curindex;
c75b4594 10809 unsigned HOST_WIDE_INT cnt;
10810 constructor_elt *ce;
cee43f7e 10811 bool fold_p = false;
c271bdb2 10812
f1f41a6c 10813 if ((*v)[0].index)
f5298614 10814 maxindex = (*v)[0].index, fold_p = true;
10815
c271bdb2 10816 curindex = maxindex;
10817
f1f41a6c 10818 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
c271bdb2 10819 {
cee43f7e 10820 bool curfold_p = false;
c75b4594 10821 if (ce->index)
cee43f7e 10822 curindex = ce->index, curfold_p = true;
c271bdb2 10823 else
cee43f7e 10824 {
10825 if (fold_p)
f5298614 10826 {
10827 /* Since we treat size types now as ordinary
10828 unsigned types, we need an explicit overflow
10829 check. */
10830 tree orig = curindex;
10831 curindex = fold_convert (sizetype, curindex);
10832 overflow_p |= tree_int_cst_lt (curindex, orig);
10833 }
389dd41b 10834 curindex = size_binop (PLUS_EXPR, curindex,
10835 size_one_node);
cee43f7e 10836 }
c271bdb2 10837 if (tree_int_cst_lt (maxindex, curindex))
cee43f7e 10838 maxindex = curindex, fold_p = curfold_p;
c271bdb2 10839 }
f5298614 10840 if (fold_p)
10841 {
10842 tree orig = maxindex;
10843 maxindex = fold_convert (sizetype, maxindex);
10844 overflow_p |= tree_int_cst_lt (maxindex, orig);
10845 }
c271bdb2 10846 }
10847 }
10848 else
10849 {
10850 /* Make an error message unless that happened already. */
10851 if (initial_value != error_mark_node)
10852 failure = 1;
10853 }
10854 }
10855 else
10856 {
10857 failure = 2;
10858 if (!do_default)
10859 return failure;
10860 }
10861
10862 type = *ptype;
10863 elt = TREE_TYPE (type);
10864 quals = TYPE_QUALS (strip_array_types (elt));
10865 if (quals == 0)
10866 unqual_elt = elt;
10867 else
6d5d708e 10868 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
c271bdb2 10869
10870 /* Using build_distinct_type_copy and modifying things afterward instead
10871 of using build_array_type to create a new type preserves all of the
10872 TYPE_LANG_FLAG_? bits that the front end may have set. */
10873 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10874 TREE_TYPE (main_type) = unqual_elt;
783bb57e 10875 TYPE_DOMAIN (main_type)
10876 = build_range_type (TREE_TYPE (maxindex),
10877 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
c271bdb2 10878 layout_type (main_type);
10879
6753bca0 10880 /* Make sure we have the canonical MAIN_TYPE. */
10881 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
48e1416a 10882 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
6753bca0 10883 hashcode);
10884 main_type = type_hash_canon (hashcode, main_type);
10885
796735dc 10886 /* Fix the canonical type. */
10887 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10888 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10889 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10890 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10891 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10892 != TYPE_DOMAIN (main_type)))
48e1416a 10893 TYPE_CANONICAL (main_type)
796735dc 10894 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10895 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10896 else
10897 TYPE_CANONICAL (main_type) = main_type;
10898
c271bdb2 10899 if (quals == 0)
10900 type = main_type;
10901 else
10902 type = c_build_qualified_type (main_type, quals);
10903
4f5b8f2a 10904 if (COMPLETE_TYPE_P (type)
10905 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
f5298614 10906 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
4f5b8f2a 10907 {
10908 error ("size of array is too large");
10909 /* If we proceed with the array type as it is, we'll eventually
08f817b3 10910 crash in tree_to_[su]hwi(). */
4f5b8f2a 10911 type = error_mark_node;
10912 }
10913
c271bdb2 10914 *ptype = type;
10915 return failure;
10916}
ab6bb714 10917
93426222 10918/* Like c_mark_addressable but don't check register qualifier. */
10919void
10920c_common_mark_addressable_vec (tree t)
10921{
10922 while (handled_component_p (t))
10923 t = TREE_OPERAND (t, 0);
4c04bcce 10924 if (!VAR_P (t)
10925 && TREE_CODE (t) != PARM_DECL
10926 && TREE_CODE (t) != COMPOUND_LITERAL_EXPR)
93426222 10927 return;
10928 TREE_ADDRESSABLE (t) = 1;
10929}
10930
10931
b6a5fc45 10932\f
10933/* Used to help initialize the builtin-types.def table. When a type of
10934 the correct size doesn't exist, use error_mark_node instead of NULL.
10935 The later results in segfaults even when a decl using the type doesn't
10936 get invoked. */
10937
10938tree
10939builtin_type_for_size (int size, bool unsignedp)
10940{
a51edb4c 10941 tree type = c_common_type_for_size (size, unsignedp);
b6a5fc45 10942 return type ? type : error_mark_node;
10943}
10944
10945/* A helper function for resolve_overloaded_builtin in resolving the
10946 overloaded __sync_ builtins. Returns a positive power of 2 if the
10947 first operand of PARAMS is a pointer to a supported data type.
517906ff 10948 Returns 0 if an error is encountered.
10949 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
10950 built-ins. */
b6a5fc45 10951
10952static int
517906ff 10953sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch)
b6a5fc45 10954{
517906ff 10955 /* Type of the argument. */
10956 tree argtype;
10957 /* Type the argument points to. */
b6a5fc45 10958 tree type;
10959 int size;
10960
77454e6e 10961 if (vec_safe_is_empty (params))
b6a5fc45 10962 {
10963 error ("too few arguments to function %qE", function);
10964 return 0;
10965 }
10966
517906ff 10967 argtype = type = TREE_TYPE ((*params)[0]);
0d284870 10968 if (TREE_CODE (type) == ARRAY_TYPE)
10969 {
10970 /* Force array-to-pointer decay for C++. */
10971 gcc_assert (c_dialect_cxx());
10972 (*params)[0] = default_conversion ((*params)[0]);
10973 type = TREE_TYPE ((*params)[0]);
10974 }
b6a5fc45 10975 if (TREE_CODE (type) != POINTER_TYPE)
10976 goto incompatible;
10977
10978 type = TREE_TYPE (type);
10979 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10980 goto incompatible;
10981
517906ff 10982 if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
10983 goto incompatible;
10984
e913b5cd 10985 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
27213ba3 10986 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
b6a5fc45 10987 return size;
10988
10989 incompatible:
bbbbe8ab 10990 /* Issue the diagnostic only if the argument is valid, otherwise
10991 it would be redundant at best and could be misleading. */
10992 if (argtype != error_mark_node)
10993 error ("operand type %qT is incompatible with argument %d of %qE",
10994 argtype, 1, function);
b6a5fc45 10995 return 0;
10996}
10997
a0c938f0 10998/* A helper function for resolve_overloaded_builtin. Adds casts to
b6a5fc45 10999 PARAMS to make arguments match up with those of FUNCTION. Drops
11000 the variadic arguments at the end. Returns false if some error
11001 was encountered; true on success. */
11002
11003static bool
1cd6e20d 11004sync_resolve_params (location_t loc, tree orig_function, tree function,
f1f41a6c 11005 vec<tree, va_gc> *params, bool orig_format)
b6a5fc45 11006{
d0af78c5 11007 function_args_iterator iter;
b6a5fc45 11008 tree ptype;
b9c74b4d 11009 unsigned int parmnum;
b6a5fc45 11010
d0af78c5 11011 function_args_iter_init (&iter, TREE_TYPE (function));
b6a5fc45 11012 /* We've declared the implementation functions to use "volatile void *"
11013 as the pointer parameter, so we shouldn't get any complaints from the
11014 call to check_function_arguments what ever type the user used. */
d0af78c5 11015 function_args_iter_next (&iter);
f1f41a6c 11016 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
b560fabd 11017 ptype = TYPE_MAIN_VARIANT (ptype);
b6a5fc45 11018
11019 /* For the rest of the values, we need to cast these to FTYPE, so that we
11020 don't get warnings for passing pointer types, etc. */
b9c74b4d 11021 parmnum = 0;
d0af78c5 11022 while (1)
b6a5fc45 11023 {
d0af78c5 11024 tree val, arg_type;
11025
11026 arg_type = function_args_iter_cond (&iter);
11027 /* XXX void_type_node belies the abstraction. */
11028 if (arg_type == void_type_node)
11029 break;
b6a5fc45 11030
b9c74b4d 11031 ++parmnum;
f1f41a6c 11032 if (params->length () <= parmnum)
b6a5fc45 11033 {
1cd6e20d 11034 error_at (loc, "too few arguments to function %qE", orig_function);
b6a5fc45 11035 return false;
11036 }
11037
0f6a7cb7 11038 /* Only convert parameters if arg_type is unsigned integer type with
11039 new format sync routines, i.e. don't attempt to convert pointer
11040 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
11041 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
11042 kinds). */
11043 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
1cd6e20d 11044 {
11045 /* Ideally for the first conversion we'd use convert_for_assignment
11046 so that we get warnings for anything that doesn't match the pointer
11047 type. This isn't portable across the C and C++ front ends atm. */
f1f41a6c 11048 val = (*params)[parmnum];
1cd6e20d 11049 val = convert (ptype, val);
11050 val = convert (arg_type, val);
f1f41a6c 11051 (*params)[parmnum] = val;
1cd6e20d 11052 }
b6a5fc45 11053
d0af78c5 11054 function_args_iter_next (&iter);
b6a5fc45 11055 }
11056
1cd6e20d 11057 /* __atomic routines are not variadic. */
f1f41a6c 11058 if (!orig_format && params->length () != parmnum + 1)
1cd6e20d 11059 {
11060 error_at (loc, "too many arguments to function %qE", orig_function);
11061 return false;
11062 }
11063
b6a5fc45 11064 /* The definition of these primitives is variadic, with the remaining
11065 being "an optional list of variables protected by the memory barrier".
11066 No clue what that's supposed to mean, precisely, but we consider all
11067 call-clobbered variables to be protected so we're safe. */
f1f41a6c 11068 params->truncate (parmnum + 1);
b6a5fc45 11069
11070 return true;
11071}
11072
a0c938f0 11073/* A helper function for resolve_overloaded_builtin. Adds a cast to
b6a5fc45 11074 RESULT to make it match the type of the first pointer argument in
11075 PARAMS. */
11076
11077static tree
1cd6e20d 11078sync_resolve_return (tree first_param, tree result, bool orig_format)
b6a5fc45 11079{
b9c74b4d 11080 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
1cd6e20d 11081 tree rtype = TREE_TYPE (result);
10080eac 11082 ptype = TYPE_MAIN_VARIANT (ptype);
1cd6e20d 11083
11084 /* New format doesn't require casting unless the types are the same size. */
11085 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
11086 return convert (ptype, result);
11087 else
11088 return result;
11089}
11090
11091/* This function verifies the PARAMS to generic atomic FUNCTION.
11092 It returns the size if all the parameters are the same size, otherwise
11093 0 is returned if the parameters are invalid. */
11094
11095static int
f1f41a6c 11096get_atomic_generic_size (location_t loc, tree function,
11097 vec<tree, va_gc> *params)
1cd6e20d 11098{
11099 unsigned int n_param;
11100 unsigned int n_model;
11101 unsigned int x;
11102 int size_0;
11103 tree type_0;
11104
11105 /* Determine the parameter makeup. */
11106 switch (DECL_FUNCTION_CODE (function))
11107 {
11108 case BUILT_IN_ATOMIC_EXCHANGE:
11109 n_param = 4;
11110 n_model = 1;
11111 break;
11112 case BUILT_IN_ATOMIC_LOAD:
11113 case BUILT_IN_ATOMIC_STORE:
11114 n_param = 3;
11115 n_model = 1;
11116 break;
11117 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11118 n_param = 6;
11119 n_model = 2;
11120 break;
11121 default:
1d581089 11122 gcc_unreachable ();
1cd6e20d 11123 }
11124
f1f41a6c 11125 if (vec_safe_length (params) != n_param)
1cd6e20d 11126 {
11127 error_at (loc, "incorrect number of arguments to function %qE", function);
11128 return 0;
11129 }
11130
11131 /* Get type of first parameter, and determine its size. */
f1f41a6c 11132 type_0 = TREE_TYPE ((*params)[0]);
0d284870 11133 if (TREE_CODE (type_0) == ARRAY_TYPE)
11134 {
11135 /* Force array-to-pointer decay for C++. */
11136 gcc_assert (c_dialect_cxx());
11137 (*params)[0] = default_conversion ((*params)[0]);
11138 type_0 = TREE_TYPE ((*params)[0]);
11139 }
1d581089 11140 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
11141 {
11142 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
11143 function);
11144 return 0;
11145 }
11146
11147 /* Types must be compile time constant sizes. */
11148 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
1cd6e20d 11149 {
1d581089 11150 error_at (loc,
11151 "argument 1 of %qE must be a pointer to a constant size type",
11152 function);
1cd6e20d 11153 return 0;
11154 }
1d581089 11155
e913b5cd 11156 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
1cd6e20d 11157
1d581089 11158 /* Zero size objects are not allowed. */
11159 if (size_0 == 0)
11160 {
11161 error_at (loc,
11162 "argument 1 of %qE must be a pointer to a nonzero size object",
11163 function);
11164 return 0;
11165 }
11166
1cd6e20d 11167 /* Check each other parameter is a pointer and the same size. */
11168 for (x = 0; x < n_param - n_model; x++)
11169 {
11170 int size;
f1f41a6c 11171 tree type = TREE_TYPE ((*params)[x]);
a04e8d62 11172 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
1cd6e20d 11173 if (n_param == 6 && x == 3)
11174 continue;
11175 if (!POINTER_TYPE_P (type))
11176 {
11177 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
11178 function);
11179 return 0;
11180 }
26040f06 11181 else if (TYPE_SIZE_UNIT (TREE_TYPE (type))
11182 && TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type))))
11183 != INTEGER_CST)
11184 {
11185 error_at (loc, "argument %d of %qE must be a pointer to a constant "
11186 "size type", x + 1, function);
11187 return 0;
11188 }
11189 else if (FUNCTION_POINTER_TYPE_P (type))
11190 {
11191 error_at (loc, "argument %d of %qE must not be a pointer to a "
11192 "function", x + 1, function);
11193 return 0;
11194 }
1f6be080 11195 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
11196 size = type_size ? tree_to_uhwi (type_size) : 0;
1cd6e20d 11197 if (size != size_0)
11198 {
11199 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
11200 function);
11201 return 0;
11202 }
11203 }
11204
11205 /* Check memory model parameters for validity. */
11206 for (x = n_param - n_model ; x < n_param; x++)
11207 {
f1f41a6c 11208 tree p = (*params)[x];
1cd6e20d 11209 if (TREE_CODE (p) == INTEGER_CST)
11210 {
e913b5cd 11211 int i = tree_to_uhwi (p);
a372f7ca 11212 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
1cd6e20d 11213 {
11214 warning_at (loc, OPT_Winvalid_memory_model,
11215 "invalid memory model argument %d of %qE", x + 1,
11216 function);
1cd6e20d 11217 }
11218 }
11219 else
11220 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
11221 {
11222 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
11223 function);
11224 return 0;
11225 }
11226 }
11227
11228 return size_0;
11229}
11230
11231
11232/* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
11233 at the beginning of the parameter list PARAMS representing the size of the
11234 objects. This is to match the library ABI requirement. LOC is the location
11235 of the function call.
11236 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
11237 returned to allow the external call to be constructed. */
11238
11239static tree
11240add_atomic_size_parameter (unsigned n, location_t loc, tree function,
f1f41a6c 11241 vec<tree, va_gc> *params)
1cd6e20d 11242{
11243 tree size_node;
11244
11245 /* Insert a SIZE_T parameter as the first param. If there isn't
11246 enough space, allocate a new vector and recursively re-build with that. */
f1f41a6c 11247 if (!params->space (1))
1cd6e20d 11248 {
11249 unsigned int z, len;
f1f41a6c 11250 vec<tree, va_gc> *v;
1cd6e20d 11251 tree f;
11252
f1f41a6c 11253 len = params->length ();
11254 vec_alloc (v, len + 1);
5a672e62 11255 v->quick_push (build_int_cst (size_type_node, n));
1cd6e20d 11256 for (z = 0; z < len; z++)
f1f41a6c 11257 v->quick_push ((*params)[z]);
ec761d5a 11258 f = build_function_call_vec (loc, vNULL, function, v, NULL);
f1f41a6c 11259 vec_free (v);
1cd6e20d 11260 return f;
11261 }
11262
11263 /* Add the size parameter and leave as a function call for processing. */
11264 size_node = build_int_cst (size_type_node, n);
f1f41a6c 11265 params->quick_insert (0, size_node);
1cd6e20d 11266 return NULL_TREE;
11267}
11268
11269
a056826c 11270/* Return whether atomic operations for naturally aligned N-byte
11271 arguments are supported, whether inline or through libatomic. */
11272static bool
11273atomic_size_supported_p (int n)
11274{
11275 switch (n)
11276 {
11277 case 1:
11278 case 2:
11279 case 4:
11280 case 8:
11281 return true;
11282
11283 case 16:
11284 return targetm.scalar_mode_supported_p (TImode);
11285
11286 default:
11287 return false;
11288 }
11289}
11290
1cd6e20d 11291/* This will process an __atomic_exchange function call, determine whether it
11292 needs to be mapped to the _N variation, or turned into a library call.
11293 LOC is the location of the builtin call.
11294 FUNCTION is the DECL that has been invoked;
11295 PARAMS is the argument list for the call. The return value is non-null
11296 TRUE is returned if it is translated into the proper format for a call to the
11297 external library, and NEW_RETURN is set the tree for that function.
11298 FALSE is returned if processing for the _N variation is required, and
47ae02b7 11299 NEW_RETURN is set to the return value the result is copied into. */
1cd6e20d 11300static bool
11301resolve_overloaded_atomic_exchange (location_t loc, tree function,
f1f41a6c 11302 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11303{
11304 tree p0, p1, p2, p3;
11305 tree I_type, I_type_ptr;
11306 int n = get_atomic_generic_size (loc, function, params);
11307
1d581089 11308 /* Size of 0 is an error condition. */
11309 if (n == 0)
11310 {
11311 *new_return = error_mark_node;
11312 return true;
11313 }
11314
1cd6e20d 11315 /* If not a lock-free size, change to the library generic format. */
a056826c 11316 if (!atomic_size_supported_p (n))
1cd6e20d 11317 {
11318 *new_return = add_atomic_size_parameter (n, loc, function, params);
11319 return true;
11320 }
11321
11322 /* Otherwise there is a lockfree match, transform the call from:
11323 void fn(T* mem, T* desired, T* return, model)
11324 into
11325 *return = (T) (fn (In* mem, (In) *desired, model)) */
11326
f1f41a6c 11327 p0 = (*params)[0];
11328 p1 = (*params)[1];
11329 p2 = (*params)[2];
11330 p3 = (*params)[3];
1cd6e20d 11331
11332 /* Create pointer to appropriate size. */
11333 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11334 I_type_ptr = build_pointer_type (I_type);
11335
11336 /* Convert object pointer to required type. */
11337 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11338 (*params)[0] = p0;
1cd6e20d 11339 /* Convert new value to required type, and dereference it. */
11340 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11341 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 11342 (*params)[1] = p1;
1cd6e20d 11343
11344 /* Move memory model to the 3rd position, and end param list. */
f1f41a6c 11345 (*params)[2] = p3;
11346 params->truncate (3);
1cd6e20d 11347
11348 /* Convert return pointer and dereference it for later assignment. */
11349 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11350
11351 return false;
b6a5fc45 11352}
11353
1cd6e20d 11354
11355/* This will process an __atomic_compare_exchange function call, determine
11356 whether it needs to be mapped to the _N variation, or turned into a lib call.
11357 LOC is the location of the builtin call.
11358 FUNCTION is the DECL that has been invoked;
11359 PARAMS is the argument list for the call. The return value is non-null
11360 TRUE is returned if it is translated into the proper format for a call to the
11361 external library, and NEW_RETURN is set the tree for that function.
11362 FALSE is returned if processing for the _N variation is required. */
11363
11364static bool
11365resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
f1f41a6c 11366 vec<tree, va_gc> *params,
1cd6e20d 11367 tree *new_return)
11368{
11369 tree p0, p1, p2;
11370 tree I_type, I_type_ptr;
11371 int n = get_atomic_generic_size (loc, function, params);
11372
1d581089 11373 /* Size of 0 is an error condition. */
11374 if (n == 0)
11375 {
11376 *new_return = error_mark_node;
11377 return true;
11378 }
11379
1cd6e20d 11380 /* If not a lock-free size, change to the library generic format. */
a056826c 11381 if (!atomic_size_supported_p (n))
1cd6e20d 11382 {
11383 /* The library generic format does not have the weak parameter, so
11384 remove it from the param list. Since a parameter has been removed,
11385 we can be sure that there is room for the SIZE_T parameter, meaning
11386 there will not be a recursive rebuilding of the parameter list, so
11387 there is no danger this will be done twice. */
11388 if (n > 0)
11389 {
f1f41a6c 11390 (*params)[3] = (*params)[4];
11391 (*params)[4] = (*params)[5];
11392 params->truncate (5);
1cd6e20d 11393 }
11394 *new_return = add_atomic_size_parameter (n, loc, function, params);
11395 return true;
11396 }
11397
11398 /* Otherwise, there is a match, so the call needs to be transformed from:
11399 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11400 into
11401 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
ab2c1de8 11402
f1f41a6c 11403 p0 = (*params)[0];
11404 p1 = (*params)[1];
11405 p2 = (*params)[2];
1cd6e20d 11406
11407 /* Create pointer to appropriate size. */
11408 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11409 I_type_ptr = build_pointer_type (I_type);
11410
11411 /* Convert object pointer to required type. */
11412 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11413 (*params)[0] = p0;
1cd6e20d 11414
11415 /* Convert expected pointer to required type. */
11416 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
f1f41a6c 11417 (*params)[1] = p1;
1cd6e20d 11418
11419 /* Convert desired value to required type, and dereference it. */
11420 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11421 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
f1f41a6c 11422 (*params)[2] = p2;
1cd6e20d 11423
11424 /* The rest of the parameters are fine. NULL means no special return value
11425 processing.*/
11426 *new_return = NULL;
11427 return false;
11428}
11429
11430
11431/* This will process an __atomic_load function call, determine whether it
11432 needs to be mapped to the _N variation, or turned into a library call.
11433 LOC is the location of the builtin call.
11434 FUNCTION is the DECL that has been invoked;
11435 PARAMS is the argument list for the call. The return value is non-null
11436 TRUE is returned if it is translated into the proper format for a call to the
11437 external library, and NEW_RETURN is set the tree for that function.
11438 FALSE is returned if processing for the _N variation is required, and
47ae02b7 11439 NEW_RETURN is set to the return value the result is copied into. */
1cd6e20d 11440
11441static bool
11442resolve_overloaded_atomic_load (location_t loc, tree function,
f1f41a6c 11443 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11444{
11445 tree p0, p1, p2;
11446 tree I_type, I_type_ptr;
11447 int n = get_atomic_generic_size (loc, function, params);
11448
1d581089 11449 /* Size of 0 is an error condition. */
11450 if (n == 0)
11451 {
11452 *new_return = error_mark_node;
11453 return true;
11454 }
11455
1cd6e20d 11456 /* If not a lock-free size, change to the library generic format. */
a056826c 11457 if (!atomic_size_supported_p (n))
1cd6e20d 11458 {
11459 *new_return = add_atomic_size_parameter (n, loc, function, params);
11460 return true;
11461 }
11462
11463 /* Otherwise, there is a match, so the call needs to be transformed from:
11464 void fn(T* mem, T* return, model)
11465 into
11466 *return = (T) (fn ((In *) mem, model)) */
11467
f1f41a6c 11468 p0 = (*params)[0];
11469 p1 = (*params)[1];
11470 p2 = (*params)[2];
1cd6e20d 11471
11472 /* Create pointer to appropriate size. */
11473 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11474 I_type_ptr = build_pointer_type (I_type);
11475
11476 /* Convert object pointer to required type. */
11477 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11478 (*params)[0] = p0;
1cd6e20d 11479
11480 /* Move memory model to the 2nd position, and end param list. */
f1f41a6c 11481 (*params)[1] = p2;
11482 params->truncate (2);
1cd6e20d 11483
11484 /* Convert return pointer and dereference it for later assignment. */
11485 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11486
11487 return false;
11488}
11489
11490
11491/* This will process an __atomic_store function call, determine whether it
11492 needs to be mapped to the _N variation, or turned into a library call.
11493 LOC is the location of the builtin call.
11494 FUNCTION is the DECL that has been invoked;
11495 PARAMS is the argument list for the call. The return value is non-null
11496 TRUE is returned if it is translated into the proper format for a call to the
11497 external library, and NEW_RETURN is set the tree for that function.
11498 FALSE is returned if processing for the _N variation is required, and
47ae02b7 11499 NEW_RETURN is set to the return value the result is copied into. */
1cd6e20d 11500
11501static bool
11502resolve_overloaded_atomic_store (location_t loc, tree function,
f1f41a6c 11503 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11504{
11505 tree p0, p1;
11506 tree I_type, I_type_ptr;
11507 int n = get_atomic_generic_size (loc, function, params);
11508
1d581089 11509 /* Size of 0 is an error condition. */
11510 if (n == 0)
11511 {
11512 *new_return = error_mark_node;
11513 return true;
11514 }
11515
1cd6e20d 11516 /* If not a lock-free size, change to the library generic format. */
a056826c 11517 if (!atomic_size_supported_p (n))
1cd6e20d 11518 {
11519 *new_return = add_atomic_size_parameter (n, loc, function, params);
11520 return true;
11521 }
11522
11523 /* Otherwise, there is a match, so the call needs to be transformed from:
11524 void fn(T* mem, T* value, model)
11525 into
11526 fn ((In *) mem, (In) *value, model) */
11527
f1f41a6c 11528 p0 = (*params)[0];
11529 p1 = (*params)[1];
1cd6e20d 11530
11531 /* Create pointer to appropriate size. */
11532 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11533 I_type_ptr = build_pointer_type (I_type);
11534
11535 /* Convert object pointer to required type. */
11536 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11537 (*params)[0] = p0;
1cd6e20d 11538
11539 /* Convert new value to required type, and dereference it. */
11540 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11541 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 11542 (*params)[1] = p1;
1cd6e20d 11543
11544 /* The memory model is in the right spot already. Return is void. */
11545 *new_return = NULL_TREE;
11546
11547 return false;
11548}
11549
11550
b6a5fc45 11551/* Some builtin functions are placeholders for other expressions. This
11552 function should be called immediately after parsing the call expression
11553 before surrounding code has committed to the type of the expression.
11554
e60a6f7b 11555 LOC is the location of the builtin call.
11556
b6a5fc45 11557 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11558 PARAMS is the argument list for the call. The return value is non-null
11559 when expansion is complete, and null if normal processing should
11560 continue. */
11561
11562tree
f1f41a6c 11563resolve_overloaded_builtin (location_t loc, tree function,
11564 vec<tree, va_gc> *params)
b6a5fc45 11565{
11566 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
517906ff 11567
11568 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
11569 Those are not valid to call with a pointer to _Bool (or C++ bool)
11570 and so must be rejected. */
11571 bool fetch_op = true;
1cd6e20d 11572 bool orig_format = true;
11573 tree new_return = NULL_TREE;
11574
65441f6f 11575 switch (DECL_BUILT_IN_CLASS (function))
11576 {
11577 case BUILT_IN_NORMAL:
11578 break;
11579 case BUILT_IN_MD:
11580 if (targetm.resolve_overloaded_builtin)
e60a6f7b 11581 return targetm.resolve_overloaded_builtin (loc, function, params);
65441f6f 11582 else
a0c938f0 11583 return NULL_TREE;
65441f6f 11584 default:
11585 return NULL_TREE;
11586 }
a0c938f0 11587
65441f6f 11588 /* Handle BUILT_IN_NORMAL here. */
b6a5fc45 11589 switch (orig_code)
11590 {
1cd6e20d 11591 case BUILT_IN_ATOMIC_EXCHANGE:
11592 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11593 case BUILT_IN_ATOMIC_LOAD:
11594 case BUILT_IN_ATOMIC_STORE:
11595 {
11596 /* Handle these 4 together so that they can fall through to the next
11597 case if the call is transformed to an _N variant. */
11598 switch (orig_code)
c7afb782 11599 {
1cd6e20d 11600 case BUILT_IN_ATOMIC_EXCHANGE:
11601 {
11602 if (resolve_overloaded_atomic_exchange (loc, function, params,
11603 &new_return))
11604 return new_return;
11605 /* Change to the _N variant. */
11606 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11607 break;
11608 }
11609
11610 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11611 {
11612 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11613 params,
11614 &new_return))
11615 return new_return;
11616 /* Change to the _N variant. */
11617 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11618 break;
11619 }
11620 case BUILT_IN_ATOMIC_LOAD:
11621 {
11622 if (resolve_overloaded_atomic_load (loc, function, params,
11623 &new_return))
11624 return new_return;
11625 /* Change to the _N variant. */
11626 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11627 break;
11628 }
11629 case BUILT_IN_ATOMIC_STORE:
11630 {
11631 if (resolve_overloaded_atomic_store (loc, function, params,
11632 &new_return))
11633 return new_return;
11634 /* Change to the _N variant. */
11635 orig_code = BUILT_IN_ATOMIC_STORE_N;
11636 break;
11637 }
11638 default:
11639 gcc_unreachable ();
c7afb782 11640 }
1cd6e20d 11641 }
c7afb782 11642 /* FALLTHRU */
1cd6e20d 11643 case BUILT_IN_ATOMIC_EXCHANGE_N:
11644 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11645 case BUILT_IN_ATOMIC_LOAD_N:
11646 case BUILT_IN_ATOMIC_STORE_N:
c7afb782 11647 fetch_op = false;
11648 /* FALLTHRU */
1cd6e20d 11649 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11650 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11651 case BUILT_IN_ATOMIC_AND_FETCH_N:
11652 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11653 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11654 case BUILT_IN_ATOMIC_OR_FETCH_N:
11655 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11656 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11657 case BUILT_IN_ATOMIC_FETCH_AND_N:
11658 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11659 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11660 case BUILT_IN_ATOMIC_FETCH_OR_N:
c7afb782 11661 orig_format = false;
11662 /* FALLTHRU */
2797f13a 11663 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11664 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11665 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11666 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11667 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11668 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11669 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11670 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11671 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11672 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11673 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11674 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11675 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11676 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11677 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11678 case BUILT_IN_SYNC_LOCK_RELEASE_N:
b6a5fc45 11679 {
517906ff 11680 /* The following are not _FETCH_OPs and must be accepted with
11681 pointers to _Bool (or C++ bool). */
11682 if (fetch_op)
11683 fetch_op =
11684 (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11685 && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
11686 && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
11687 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
11688
11689 int n = sync_resolve_size (function, params, fetch_op);
b9c74b4d 11690 tree new_function, first_param, result;
b9a16870 11691 enum built_in_function fncode;
b6a5fc45 11692
11693 if (n == 0)
11694 return error_mark_node;
11695
b9a16870 11696 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11697 new_function = builtin_decl_explicit (fncode);
1cd6e20d 11698 if (!sync_resolve_params (loc, function, new_function, params,
11699 orig_format))
b6a5fc45 11700 return error_mark_node;
11701
f1f41a6c 11702 first_param = (*params)[0];
ec761d5a 11703 result = build_function_call_vec (loc, vNULL, new_function, params,
11704 NULL);
1cd6e20d 11705 if (result == error_mark_node)
11706 return result;
2797f13a 11707 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
1cd6e20d 11708 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
efa8e86e 11709 && orig_code != BUILT_IN_ATOMIC_STORE_N
11710 && orig_code != BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N)
1cd6e20d 11711 result = sync_resolve_return (first_param, result, orig_format);
b6a5fc45 11712
92e7ab1e 11713 if (fetch_op)
11714 /* Prevent -Wunused-value warning. */
11715 TREE_USED (result) = true;
11716
1cd6e20d 11717 /* If new_return is set, assign function to that expr and cast the
11718 result to void since the generic interface returned void. */
11719 if (new_return)
11720 {
11721 /* Cast function result from I{1,2,4,8,16} to the required type. */
11722 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11723 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11724 result);
11725 TREE_SIDE_EFFECTS (result) = 1;
11726 protected_set_expr_location (result, loc);
11727 result = convert (void_type_node, result);
11728 }
b6a5fc45 11729 return result;
11730 }
11731
11732 default:
65441f6f 11733 return NULL_TREE;
b6a5fc45 11734 }
11735}
11736
73437615 11737/* vector_types_compatible_elements_p is used in type checks of vectors
11738 values used as operands of binary operators. Where it returns true, and
11739 the other checks of the caller succeed (being vector types in he first
11740 place, and matching number of elements), we can just treat the types
11741 as essentially the same.
11742 Contrast with vector_targets_convertible_p, which is used for vector
11743 pointer types, and vector_types_convertible_p, which will allow
11744 language-specific matches under the control of flag_lax_vector_conversions,
11745 and might still require a conversion. */
11746/* True if vector types T1 and T2 can be inputs to the same binary
11747 operator without conversion.
11748 We don't check the overall vector size here because some of our callers
11749 want to give different error messages when the vectors are compatible
11750 except for the element count. */
11751
491255f5 11752bool
73437615 11753vector_types_compatible_elements_p (tree t1, tree t2)
491255f5 11754{
73437615 11755 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11756 t1 = TREE_TYPE (t1);
11757 t2 = TREE_TYPE (t2);
11758
491255f5 11759 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11760
9421ebb9 11761 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11762 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11763 || c2 == FIXED_POINT_TYPE));
491255f5 11764
73437615 11765 t1 = c_common_signed_type (t1);
11766 t2 = c_common_signed_type (t2);
491255f5 11767 /* Equality works here because c_common_signed_type uses
11768 TYPE_MAIN_VARIANT. */
73437615 11769 if (t1 == t2)
11770 return true;
11771 if (opaque && c1 == c2
11772 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11773 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11774 return true;
11775 return false;
491255f5 11776}
11777
be7350e7 11778/* Check for missing format attributes on function pointers. LTYPE is
11779 the new type or left-hand side type. RTYPE is the old type or
11780 right-hand side type. Returns TRUE if LTYPE is missing the desired
11781 attribute. */
11782
11783bool
11784check_missing_format_attribute (tree ltype, tree rtype)
11785{
11786 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11787 tree ra;
11788
11789 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11790 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11791 break;
11792 if (ra)
11793 {
11794 tree la;
11795 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11796 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11797 break;
11798 return !la;
11799 }
11800 else
11801 return false;
11802}
11803
2840aae4 11804/* Subscripting with type char is likely to lose on a machine where
11805 chars are signed. So warn on any machine, but optionally. Don't
11806 warn for unsigned char since that type is safe. Don't warn for
11807 signed char because anyone who uses that must have done so
11808 deliberately. Furthermore, we reduce the false positive load by
11809 warning only for non-constant value of type char. */
11810
11811void
92b63884 11812warn_array_subscript_with_type_char (location_t loc, tree index)
2840aae4 11813{
11814 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11815 && TREE_CODE (index) != INTEGER_CST)
92b63884 11816 warning_at (loc, OPT_Wchar_subscripts,
11817 "array subscript has type %<char%>");
2840aae4 11818}
11819
e534436e 11820/* Implement -Wparentheses for the unexpected C precedence rules, to
11821 cover cases like x + y << z which readers are likely to
11822 misinterpret. We have seen an expression in which CODE is a binary
82012ffe 11823 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11824 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11825 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11826 expression was not formed using a binary or unary operator, or it
11827 was enclosed in parentheses. */
e534436e 11828
11829void
b0e7825e 11830warn_about_parentheses (location_t loc, enum tree_code code,
269f7979 11831 enum tree_code code_left, tree arg_left,
82012ffe 11832 enum tree_code code_right, tree arg_right)
e534436e 11833{
11834 if (!warn_parentheses)
11835 return;
11836
82012ffe 11837 /* This macro tests that the expression ARG with original tree code
11838 CODE appears to be a boolean expression. or the result of folding a
11839 boolean expression. */
11840#define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11841 (truth_value_p (TREE_CODE (ARG)) \
11842 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11843 /* Folding may create 0 or 1 integers from other expressions. */ \
11844 || ((CODE) != INTEGER_CST \
11845 && (integer_onep (ARG) || integer_zerop (ARG))))
11846
48e1416a 11847 switch (code)
e534436e 11848 {
82012ffe 11849 case LSHIFT_EXPR:
b0e7825e 11850 if (code_left == PLUS_EXPR)
11851 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11852 "suggest parentheses around %<+%> inside %<<<%>");
11853 else if (code_right == PLUS_EXPR)
11854 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11855 "suggest parentheses around %<+%> inside %<<<%>");
11856 else if (code_left == MINUS_EXPR)
11857 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11858 "suggest parentheses around %<-%> inside %<<<%>");
11859 else if (code_right == MINUS_EXPR)
11860 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11861 "suggest parentheses around %<-%> inside %<<<%>");
82012ffe 11862 return;
e534436e 11863
82012ffe 11864 case RSHIFT_EXPR:
b0e7825e 11865 if (code_left == PLUS_EXPR)
11866 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11867 "suggest parentheses around %<+%> inside %<>>%>");
11868 else if (code_right == PLUS_EXPR)
11869 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11870 "suggest parentheses around %<+%> inside %<>>%>");
11871 else if (code_left == MINUS_EXPR)
11872 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11873 "suggest parentheses around %<-%> inside %<>>%>");
11874 else if (code_right == MINUS_EXPR)
11875 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11876 "suggest parentheses around %<-%> inside %<>>%>");
82012ffe 11877 return;
e534436e 11878
82012ffe 11879 case TRUTH_ORIF_EXPR:
b0e7825e 11880 if (code_left == TRUTH_ANDIF_EXPR)
11881 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11882 "suggest parentheses around %<&&%> within %<||%>");
11883 else if (code_right == TRUTH_ANDIF_EXPR)
11884 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11885 "suggest parentheses around %<&&%> within %<||%>");
82012ffe 11886 return;
11887
11888 case BIT_IOR_EXPR:
e534436e 11889 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
b0e7825e 11890 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11891 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11892 "suggest parentheses around arithmetic in operand of %<|%>");
11893 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11894 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11895 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11896 "suggest parentheses around arithmetic in operand of %<|%>");
e534436e 11897 /* Check cases like x|y==z */
b0e7825e 11898 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11899 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11900 "suggest parentheses around comparison in operand of %<|%>");
11901 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11902 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11903 "suggest parentheses around comparison in operand of %<|%>");
11904 /* Check cases like !x | y */
11905 else if (code_left == TRUTH_NOT_EXPR
11906 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 11907 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11908 "suggest parentheses around operand of "
11909 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
82012ffe 11910 return;
e534436e 11911
82012ffe 11912 case BIT_XOR_EXPR:
e534436e 11913 if (code_left == BIT_AND_EXPR
b0e7825e 11914 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11915 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11916 "suggest parentheses around arithmetic in operand of %<^%>");
11917 else if (code_right == BIT_AND_EXPR
11918 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11919 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11920 "suggest parentheses around arithmetic in operand of %<^%>");
e534436e 11921 /* Check cases like x^y==z */
b0e7825e 11922 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11923 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11924 "suggest parentheses around comparison in operand of %<^%>");
11925 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11926 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11927 "suggest parentheses around comparison in operand of %<^%>");
11928 return;
e534436e 11929
82012ffe 11930 case BIT_AND_EXPR:
b0e7825e 11931 if (code_left == PLUS_EXPR)
11932 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11933 "suggest parentheses around %<+%> in operand of %<&%>");
11934 else if (code_right == PLUS_EXPR)
11935 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11936 "suggest parentheses around %<+%> in operand of %<&%>");
b0e7825e 11937 else if (code_left == MINUS_EXPR)
11938 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11939 "suggest parentheses around %<-%> in operand of %<&%>");
11940 else if (code_right == MINUS_EXPR)
11941 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11942 "suggest parentheses around %<-%> in operand of %<&%>");
e534436e 11943 /* Check cases like x&y==z */
b0e7825e 11944 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11945 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11946 "suggest parentheses around comparison in operand of %<&%>");
11947 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11948 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11949 "suggest parentheses around comparison in operand of %<&%>");
11950 /* Check cases like !x & y */
11951 else if (code_left == TRUTH_NOT_EXPR
11952 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 11953 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11954 "suggest parentheses around operand of "
11955 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
82012ffe 11956 return;
e534436e 11957
82012ffe 11958 case EQ_EXPR:
b0e7825e 11959 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11960 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11961 "suggest parentheses around comparison in operand of %<==%>");
11962 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11963 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11964 "suggest parentheses around comparison in operand of %<==%>");
11965 return;
11966 case NE_EXPR:
b0e7825e 11967 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11968 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11969 "suggest parentheses around comparison in operand of %<!=%>");
11970 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11971 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11972 "suggest parentheses around comparison in operand of %<!=%>");
11973 return;
11974
11975 default:
b0e7825e 11976 if (TREE_CODE_CLASS (code) == tcc_comparison)
11977 {
11978 if (TREE_CODE_CLASS (code_left) == tcc_comparison
269f7979 11979 && code_left != NE_EXPR && code_left != EQ_EXPR
11980 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
b0e7825e 11981 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11982 "comparisons like %<X<=Y<=Z%> do not "
11983 "have their mathematical meaning");
11984 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
269f7979 11985 && code_right != NE_EXPR && code_right != EQ_EXPR
b0e7825e 11986 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11987 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11988 "comparisons like %<X<=Y<=Z%> do not "
11989 "have their mathematical meaning");
11990 }
82012ffe 11991 return;
6ce0c450 11992 }
82012ffe 11993#undef NOT_A_BOOLEAN_EXPR_P
e534436e 11994}
11995
92fccaaa 11996/* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11997
11998void
11999warn_for_unused_label (tree label)
12000{
12001 if (!TREE_USED (label))
12002 {
12003 if (DECL_INITIAL (label))
12004 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
12005 else
12006 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
12007 }
12008}
2840aae4 12009
b6889cb0 12010/* Warn for division by zero according to the value of DIVISOR. LOC
12011 is the location of the division operator. */
f092582b 12012
12013void
b6889cb0 12014warn_for_div_by_zero (location_t loc, tree divisor)
f092582b 12015{
9421ebb9 12016 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
12017 about division by zero. Do not issue a warning if DIVISOR has a
f092582b 12018 floating-point type, since we consider 0.0/0.0 a valid way of
12019 generating a NaN. */
48d94ede 12020 if (c_inhibit_evaluation_warnings == 0
9421ebb9 12021 && (integer_zerop (divisor) || fixed_zerop (divisor)))
b6889cb0 12022 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
f092582b 12023}
12024
40385231 12025/* Warn for patterns where memset appears to be used incorrectly. The
12026 warning location should be LOC. ARG0, and ARG2 are the first and
12027 last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for
12028 each argument that was a literal zero. */
12029
12030void
12031warn_for_memset (location_t loc, tree arg0, tree arg2,
12032 int literal_zero_mask)
12033{
12034 if (warn_memset_transposed_args
12035 && integer_zerop (arg2)
12036 && (literal_zero_mask & (1 << 2)) != 0
12037 && (literal_zero_mask & (1 << 1)) == 0)
12038 warning_at (loc, OPT_Wmemset_transposed_args,
12039 "%<memset%> used with constant zero length "
12040 "parameter; this could be due to transposed "
12041 "parameters");
12042
12043 if (warn_memset_elt_size && TREE_CODE (arg2) == INTEGER_CST)
12044 {
12045 STRIP_NOPS (arg0);
12046 if (TREE_CODE (arg0) == ADDR_EXPR)
12047 arg0 = TREE_OPERAND (arg0, 0);
12048 tree type = TREE_TYPE (arg0);
364743f3 12049 if (type != NULL_TREE && TREE_CODE (type) == ARRAY_TYPE)
40385231 12050 {
12051 tree elt_type = TREE_TYPE (type);
12052 tree domain = TYPE_DOMAIN (type);
12053 if (!integer_onep (TYPE_SIZE_UNIT (elt_type))
025dd1e0 12054 && domain != NULL_TREE
40385231 12055 && TYPE_MAXVAL (domain)
12056 && TYPE_MINVAL (domain)
12057 && integer_zerop (TYPE_MINVAL (domain))
12058 && integer_onep (fold_build2 (MINUS_EXPR, domain,
12059 arg2,
12060 TYPE_MAXVAL (domain))))
12061 warning_at (loc, OPT_Wmemset_elt_size,
12062 "%<memset%> used with length equal to "
12063 "number of elements without multiplication "
12064 "by element size");
12065 }
12066 }
12067}
12068
13869a99 12069/* Subroutine of build_binary_op. Give warnings for comparisons
12070 between signed and unsigned quantities that may fail. Do the
12071 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
12072 so that casts will be considered, but default promotions won't
8e70fb09 12073 be.
12074
12075 LOCATION is the location of the comparison operator.
13869a99 12076
12077 The arguments of this function map directly to local variables
12078 of build_binary_op. */
12079
48e1416a 12080void
8e70fb09 12081warn_for_sign_compare (location_t location,
48e1416a 12082 tree orig_op0, tree orig_op1,
12083 tree op0, tree op1,
13869a99 12084 tree result_type, enum tree_code resultcode)
12085{
12086 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
12087 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
12088 int unsignedp0, unsignedp1;
48e1416a 12089
13869a99 12090 /* In C++, check for comparison of different enum types. */
12091 if (c_dialect_cxx()
12092 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
12093 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
12094 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
895b662f 12095 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
13869a99 12096 {
8e70fb09 12097 warning_at (location,
12098 OPT_Wsign_compare, "comparison between types %qT and %qT",
12099 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
13869a99 12100 }
12101
12102 /* Do not warn if the comparison is being done in a signed type,
12103 since the signed type will only be chosen if it can represent
12104 all the values of the unsigned type. */
12105 if (!TYPE_UNSIGNED (result_type))
12106 /* OK */;
12107 /* Do not warn if both operands are unsigned. */
12108 else if (op0_signed == op1_signed)
12109 /* OK */;
12110 else
12111 {
895b662f 12112 tree sop, uop, base_type;
13869a99 12113 bool ovf;
895b662f 12114
13869a99 12115 if (op0_signed)
12116 sop = orig_op0, uop = orig_op1;
48e1416a 12117 else
13869a99 12118 sop = orig_op1, uop = orig_op0;
12119
48e1416a 12120 STRIP_TYPE_NOPS (sop);
13869a99 12121 STRIP_TYPE_NOPS (uop);
895b662f 12122 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
12123 ? TREE_TYPE (result_type) : result_type);
13869a99 12124
12125 /* Do not warn if the signed quantity is an unsuffixed integer
12126 literal (or some static constant expression involving such
12127 literals or a conditional expression involving such literals)
12128 and it is non-negative. */
12129 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
12130 /* OK */;
12131 /* Do not warn if the comparison is an equality operation, the
12132 unsigned quantity is an integral constant, and it would fit
12133 in the result if the result were signed. */
12134 else if (TREE_CODE (uop) == INTEGER_CST
12135 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
895b662f 12136 && int_fits_type_p (uop, c_common_signed_type (base_type)))
13869a99 12137 /* OK */;
12138 /* In C, do not warn if the unsigned quantity is an enumeration
12139 constant and its maximum value would fit in the result if the
12140 result were signed. */
12141 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
12142 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
12143 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
895b662f 12144 c_common_signed_type (base_type)))
13869a99 12145 /* OK */;
48e1416a 12146 else
8e70fb09 12147 warning_at (location,
48e1416a 12148 OPT_Wsign_compare,
8e70fb09 12149 "comparison between signed and unsigned integer expressions");
13869a99 12150 }
48e1416a 12151
13869a99 12152 /* Warn if two unsigned values are being compared in a size larger
12153 than their original size, and one (and only one) is the result of
12154 a `~' operator. This comparison will always fail.
48e1416a 12155
13869a99 12156 Also warn if one operand is a constant, and the constant does not
12157 have all bits set that are set in the ~ operand when it is
12158 extended. */
12159
7f506bca 12160 op0 = c_common_get_narrower (op0, &unsignedp0);
12161 op1 = c_common_get_narrower (op1, &unsignedp1);
48e1416a 12162
13869a99 12163 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
12164 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
12165 {
12166 if (TREE_CODE (op0) == BIT_NOT_EXPR)
7f506bca 12167 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
13869a99 12168 if (TREE_CODE (op1) == BIT_NOT_EXPR)
7f506bca 12169 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
13869a99 12170
e913b5cd 12171 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
13869a99 12172 {
12173 tree primop;
12174 HOST_WIDE_INT constant, mask;
12175 int unsignedp;
12176 unsigned int bits;
48e1416a 12177
e913b5cd 12178 if (tree_fits_shwi_p (op0))
13869a99 12179 {
12180 primop = op1;
12181 unsignedp = unsignedp1;
e913b5cd 12182 constant = tree_to_shwi (op0);
13869a99 12183 }
12184 else
12185 {
12186 primop = op0;
12187 unsignedp = unsignedp0;
e913b5cd 12188 constant = tree_to_shwi (op1);
13869a99 12189 }
48e1416a 12190
13869a99 12191 bits = TYPE_PRECISION (TREE_TYPE (primop));
12192 if (bits < TYPE_PRECISION (result_type)
12193 && bits < HOST_BITS_PER_LONG && unsignedp)
12194 {
59c1507a 12195 mask = HOST_WIDE_INT_M1U << bits;
13869a99 12196 if ((mask & constant) != mask)
12197 {
12198 if (constant == 0)
76fdceeb 12199 warning_at (location, OPT_Wsign_compare,
12200 "promoted ~unsigned is always non-zero");
13869a99 12201 else
48e1416a 12202 warning_at (location, OPT_Wsign_compare,
8e70fb09 12203 "comparison of promoted ~unsigned with constant");
13869a99 12204 }
12205 }
12206 }
12207 else if (unsignedp0 && unsignedp1
12208 && (TYPE_PRECISION (TREE_TYPE (op0))
12209 < TYPE_PRECISION (result_type))
12210 && (TYPE_PRECISION (TREE_TYPE (op1))
12211 < TYPE_PRECISION (result_type)))
8e70fb09 12212 warning_at (location, OPT_Wsign_compare,
13869a99 12213 "comparison of promoted ~unsigned with unsigned");
12214 }
12215}
12216
5ba33bf4 12217/* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
12218 type via c_common_type. If -Wdouble-promotion is in use, and the
12219 conditions for warning have been met, issue a warning. GMSGID is
12220 the warning message. It must have two %T specifiers for the type
12221 that was converted (generally "float") and the type to which it was
12222 converted (generally "double), respectively. LOC is the location
12223 to which the awrning should refer. */
12224
12225void
12226do_warn_double_promotion (tree result_type, tree type1, tree type2,
12227 const char *gmsgid, location_t loc)
12228{
12229 tree source_type;
12230
12231 if (!warn_double_promotion)
12232 return;
12233 /* If the conversion will not occur at run-time, there is no need to
12234 warn about it. */
12235 if (c_inhibit_evaluation_warnings)
12236 return;
12237 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
12238 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
12239 return;
12240 if (TYPE_MAIN_VARIANT (type1) == float_type_node
12241 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
12242 source_type = type1;
12243 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
12244 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
12245 source_type = type2;
12246 else
12247 return;
12248 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
12249}
12250
0949f227 12251/* Possibly warn about unused parameters. */
12252
12253void
12254do_warn_unused_parameter (tree fn)
12255{
12256 tree decl;
12257
12258 for (decl = DECL_ARGUMENTS (fn);
12259 decl; decl = DECL_CHAIN (decl))
12260 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
12261 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
12262 && !TREE_NO_WARNING (decl))
12263 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
12264 "unused parameter %qD", decl);
12265}
12266
12267
41771881 12268/* Setup a TYPE_DECL node as a typedef representation.
12269
12270 X is a TYPE_DECL for a typedef statement. Create a brand new
12271 ..._TYPE node (which will be just a variant of the existing
12272 ..._TYPE node with identical properties) and then install X
12273 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
12274
12275 The whole point here is to end up with a situation where each
12276 and every ..._TYPE node the compiler creates will be uniquely
12277 associated with AT MOST one node representing a typedef name.
12278 This way, even though the compiler substitutes corresponding
12279 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
12280 early on, later parts of the compiler can always do the reverse
12281 translation and get back the corresponding typedef name. For
12282 example, given:
ab2c1de8 12283
41771881 12284 typedef struct S MY_TYPE;
12285 MY_TYPE object;
12286
12287 Later parts of the compiler might only know that `object' was of
12288 type `struct S' if it were not for code just below. With this
12289 code however, later parts of the compiler see something like:
12290
12291 struct S' == struct S
12292 typedef struct S' MY_TYPE;
12293 struct S' object;
12294
12295 And they can then deduce (from the node for type struct S') that
12296 the original object declaration was:
12297
12298 MY_TYPE object;
12299
12300 Being able to do this is important for proper support of protoize,
12301 and also for generating precise symbolic debugging information
12302 which takes full account of the programmer's (typedef) vocabulary.
12303
12304 Obviously, we don't want to generate a duplicate ..._TYPE node if
12305 the TYPE_DECL node that we are now processing really represents a
12306 standard built-in type. */
12307
12308void
12309set_underlying_type (tree x)
12310{
12311 if (x == error_mark_node)
12312 return;
12313 if (DECL_IS_BUILTIN (x))
12314 {
12315 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12316 TYPE_NAME (TREE_TYPE (x)) = x;
12317 }
12318 else if (TREE_TYPE (x) != error_mark_node
12319 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12320 {
12321 tree tt = TREE_TYPE (x);
12322 DECL_ORIGINAL_TYPE (x) = tt;
12323 tt = build_variant_type_copy (tt);
12324 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12325 TYPE_NAME (tt) = x;
12326 TREE_USED (tt) = TREE_USED (x);
12327 TREE_TYPE (x) = tt;
12328 }
12329}
12330
1a4c44c5 12331/* Record the types used by the current global variable declaration
12332 being parsed, so that we can decide later to emit their debug info.
12333 Those types are in types_used_by_cur_var_decl, and we are going to
12334 store them in the types_used_by_vars_hash hash table.
12335 DECL is the declaration of the global variable that has been parsed. */
12336
12337void
12338record_types_used_by_current_var_decl (tree decl)
12339{
12340 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12341
f1f41a6c 12342 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
1a4c44c5 12343 {
f1f41a6c 12344 tree type = types_used_by_cur_var_decl->pop ();
aef48c9a 12345 types_used_by_var_decl_insert (type, decl);
1a4c44c5 12346 }
12347}
12348
a4e3ffad 12349/* If DECL is a typedef that is declared in the current function,
12350 record it for the purpose of -Wunused-local-typedefs. */
12351
12352void
12353record_locally_defined_typedef (tree decl)
12354{
12355 struct c_language_function *l;
12356
12357 if (!warn_unused_local_typedefs
12358 || cfun == NULL
12359 /* if this is not a locally defined typedef then we are not
12360 interested. */
12361 || !is_typedef_decl (decl)
12362 || !decl_function_context (decl))
12363 return;
12364
12365 l = (struct c_language_function *) cfun->language;
f1f41a6c 12366 vec_safe_push (l->local_typedefs, decl);
a4e3ffad 12367}
12368
12369/* If T is a TYPE_DECL declared locally, mark it as used. */
12370
12371void
12372maybe_record_typedef_use (tree t)
12373{
12374 if (!is_typedef_decl (t))
12375 return;
12376
12377 TREE_USED (t) = true;
12378}
12379
12380/* Warn if there are some unused locally defined typedefs in the
12381 current function. */
12382
12383void
12384maybe_warn_unused_local_typedefs (void)
12385{
12386 int i;
12387 tree decl;
12388 /* The number of times we have emitted -Wunused-local-typedefs
12389 warnings. If this is different from errorcount, that means some
12390 unrelated errors have been issued. In which case, we'll avoid
12391 emitting "unused-local-typedefs" warnings. */
12392 static int unused_local_typedefs_warn_count;
12393 struct c_language_function *l;
12394
12395 if (cfun == NULL)
12396 return;
12397
12398 if ((l = (struct c_language_function *) cfun->language) == NULL)
12399 return;
12400
12401 if (warn_unused_local_typedefs
12402 && errorcount == unused_local_typedefs_warn_count)
12403 {
f1f41a6c 12404 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
a4e3ffad 12405 if (!TREE_USED (decl))
12406 warning_at (DECL_SOURCE_LOCATION (decl),
12407 OPT_Wunused_local_typedefs,
12408 "typedef %qD locally defined but not used", decl);
12409 unused_local_typedefs_warn_count = errorcount;
12410 }
12411
f1f41a6c 12412 vec_free (l->local_typedefs);
a4e3ffad 12413}
12414
78bf4156 12415/* Warn about boolean expression compared with an integer value different
12416 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12417 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12418 are the operands of the comparison. The caller must ensure that
12419 either operand is a boolean expression. */
12420
12421void
12422maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12423 tree op1)
12424{
12425 if (TREE_CODE_CLASS (code) != tcc_comparison)
12426 return;
12427
9c691dbd 12428 tree f, cst;
12429 if (f = fold_for_warn (op0),
12430 TREE_CODE (f) == INTEGER_CST)
12431 cst = op0 = f;
12432 else if (f = fold_for_warn (op1),
12433 TREE_CODE (f) == INTEGER_CST)
12434 cst = op1 = f;
12435 else
78bf4156 12436 return;
12437
12438 if (!integer_zerop (cst) && !integer_onep (cst))
12439 {
a720ab1c 12440 int sign = (TREE_CODE (op0) == INTEGER_CST
12441 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
78bf4156 12442 if (code == EQ_EXPR
12443 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12444 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12445 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12446 "with boolean expression is always false", cst);
12447 else
12448 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12449 "with boolean expression is always true", cst);
12450 }
a720ab1c 12451 else if (integer_zerop (cst) || integer_onep (cst))
12452 {
14744a16 12453 /* If the non-constant operand isn't of a boolean type, we
12454 don't want to warn here. */
12455 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12456 /* Handle booleans promoted to integers. */
8f8828ba 12457 if (bool_promoted_to_int_p (noncst))
14744a16 12458 /* Warn. */;
12459 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12460 && !truth_value_p (TREE_CODE (noncst)))
12461 return;
a720ab1c 12462 /* Do some magic to get the right diagnostics. */
12463 bool flag = TREE_CODE (op0) == INTEGER_CST;
12464 flag = integer_zerop (cst) ? flag : !flag;
12465 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12466 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12467 "with boolean expression is always true", cst);
12468 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12469 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12470 "with boolean expression is always false", cst);
12471 }
78bf4156 12472}
12473
8d669e79 12474/* Warn if signed left shift overflows. We don't warn
12475 about left-shifting 1 into the sign bit in C++14; cf.
12476 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
12477 LOC is a location of the shift; OP0 and OP1 are the operands.
12478 Return true if an overflow is detected, false otherwise. */
12479
12480bool
12481maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
12482{
12483 if (TREE_CODE (op0) != INTEGER_CST
12484 || TREE_CODE (op1) != INTEGER_CST)
12485 return false;
12486
12487 tree type0 = TREE_TYPE (op0);
12488 unsigned int prec0 = TYPE_PRECISION (type0);
12489
12490 /* Left-hand operand must be signed. */
12491 if (TYPE_UNSIGNED (type0))
12492 return false;
12493
f4809955 12494 unsigned int min_prec = (wi::min_precision (op0, SIGNED)
12495 + TREE_INT_CST_LOW (op1));
99e82b02 12496 /* Handle the case of left-shifting 1 into the sign bit.
12497 * However, shifting 1 _out_ of the sign bit, as in
12498 * INT_MIN << 1, is considered an overflow.
12499 */
12500 if (!tree_int_cst_sign_bit(op0) && min_prec == prec0 + 1)
8d669e79 12501 {
12502 /* Never warn for C++14 onwards. */
12503 if (cxx_dialect >= cxx14)
12504 return false;
12505 /* Otherwise only if -Wshift-overflow=2. But return
12506 true to signal an overflow for the sake of integer
12507 constant expressions. */
12508 if (warn_shift_overflow < 2)
12509 return true;
12510 }
12511
8d669e79 12512 bool overflowed = min_prec > prec0;
12513 if (overflowed && c_inhibit_evaluation_warnings == 0)
12514 warning_at (loc, OPT_Wshift_overflow_,
12515 "result of %qE requires %u bits to represent, "
12516 "but %qT only has %u bits",
12517 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
12518 min_prec, type0, prec0);
12519
12520 return overflowed;
12521}
12522
f352a3fb 12523/* The C and C++ parsers both use vectors to hold function arguments.
12524 For efficiency, we keep a cache of unused vectors. This is the
12525 cache. */
12526
f1f41a6c 12527typedef vec<tree, va_gc> *tree_gc_vec;
12528static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
f352a3fb 12529
12530/* Return a new vector from the cache. If the cache is empty,
12531 allocate a new vector. These vectors are GC'ed, so it is OK if the
12532 pointer is not released.. */
12533
f1f41a6c 12534vec<tree, va_gc> *
f352a3fb 12535make_tree_vector (void)
12536{
f1f41a6c 12537 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12538 return tree_vector_cache->pop ();
f352a3fb 12539 else
12540 {
f1f41a6c 12541 /* Passing 0 to vec::alloc returns NULL, and our callers require
f352a3fb 12542 that we always return a non-NULL value. The vector code uses
12543 4 when growing a NULL vector, so we do too. */
f1f41a6c 12544 vec<tree, va_gc> *v;
12545 vec_alloc (v, 4);
12546 return v;
f352a3fb 12547 }
12548}
12549
12550/* Release a vector of trees back to the cache. */
12551
12552void
f1f41a6c 12553release_tree_vector (vec<tree, va_gc> *vec)
f352a3fb 12554{
12555 if (vec != NULL)
12556 {
f1f41a6c 12557 vec->truncate (0);
12558 vec_safe_push (tree_vector_cache, vec);
f352a3fb 12559 }
12560}
12561
12562/* Get a new tree vector holding a single tree. */
12563
f1f41a6c 12564vec<tree, va_gc> *
f352a3fb 12565make_tree_vector_single (tree t)
12566{
f1f41a6c 12567 vec<tree, va_gc> *ret = make_tree_vector ();
12568 ret->quick_push (t);
f352a3fb 12569 return ret;
12570}
12571
c66c81be 12572/* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12573
f1f41a6c 12574vec<tree, va_gc> *
c66c81be 12575make_tree_vector_from_list (tree list)
12576{
f1f41a6c 12577 vec<tree, va_gc> *ret = make_tree_vector ();
c66c81be 12578 for (; list; list = TREE_CHAIN (list))
f1f41a6c 12579 vec_safe_push (ret, TREE_VALUE (list));
c66c81be 12580 return ret;
12581}
12582
f352a3fb 12583/* Get a new tree vector which is a copy of an existing one. */
12584
f1f41a6c 12585vec<tree, va_gc> *
12586make_tree_vector_copy (const vec<tree, va_gc> *orig)
f352a3fb 12587{
f1f41a6c 12588 vec<tree, va_gc> *ret;
f352a3fb 12589 unsigned int ix;
12590 tree t;
12591
12592 ret = make_tree_vector ();
f1f41a6c 12593 vec_safe_reserve (ret, vec_safe_length (orig));
12594 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12595 ret->quick_push (t);
f352a3fb 12596 return ret;
12597}
12598
a9ffdd35 12599/* Return true if KEYWORD starts a type specifier. */
12600
12601bool
12602keyword_begins_type_specifier (enum rid keyword)
12603{
12604 switch (keyword)
12605 {
4fba5eb9 12606 case RID_AUTO_TYPE:
a9ffdd35 12607 case RID_INT:
12608 case RID_CHAR:
12609 case RID_FLOAT:
12610 case RID_DOUBLE:
12611 case RID_VOID:
a9ffdd35 12612 case RID_UNSIGNED:
12613 case RID_LONG:
12614 case RID_SHORT:
12615 case RID_SIGNED:
82c85aba 12616 CASE_RID_FLOATN_NX:
a9ffdd35 12617 case RID_DFLOAT32:
12618 case RID_DFLOAT64:
12619 case RID_DFLOAT128:
12620 case RID_FRACT:
12621 case RID_ACCUM:
12622 case RID_BOOL:
12623 case RID_WCHAR:
12624 case RID_CHAR16:
12625 case RID_CHAR32:
12626 case RID_SAT:
12627 case RID_COMPLEX:
12628 case RID_TYPEOF:
12629 case RID_STRUCT:
12630 case RID_CLASS:
12631 case RID_UNION:
12632 case RID_ENUM:
12633 return true;
12634 default:
9f75f026 12635 if (keyword >= RID_FIRST_INT_N
12636 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12637 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12638 return true;
a9ffdd35 12639 return false;
12640 }
12641}
12642
12643/* Return true if KEYWORD names a type qualifier. */
12644
12645bool
12646keyword_is_type_qualifier (enum rid keyword)
12647{
12648 switch (keyword)
12649 {
12650 case RID_CONST:
12651 case RID_VOLATILE:
12652 case RID_RESTRICT:
b560fabd 12653 case RID_ATOMIC:
a9ffdd35 12654 return true;
12655 default:
12656 return false;
12657 }
12658}
12659
12660/* Return true if KEYWORD names a storage class specifier.
12661
12662 RID_TYPEDEF is not included in this list despite `typedef' being
12663 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12664 such for syntactic convenience only. */
12665
12666bool
12667keyword_is_storage_class_specifier (enum rid keyword)
12668{
12669 switch (keyword)
12670 {
12671 case RID_STATIC:
12672 case RID_EXTERN:
12673 case RID_REGISTER:
12674 case RID_AUTO:
12675 case RID_MUTABLE:
12676 case RID_THREAD:
12677 return true;
12678 default:
12679 return false;
12680 }
12681}
12682
fad3f658 12683/* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12684
12685static bool
12686keyword_is_function_specifier (enum rid keyword)
12687{
12688 switch (keyword)
12689 {
12690 case RID_INLINE:
985c6e3a 12691 case RID_NORETURN:
fad3f658 12692 case RID_VIRTUAL:
12693 case RID_EXPLICIT:
12694 return true;
12695 default:
12696 return false;
12697 }
12698}
12699
12700/* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12701 declaration-specifier (C99 6.7). */
12702
12703bool
12704keyword_is_decl_specifier (enum rid keyword)
12705{
12706 if (keyword_is_storage_class_specifier (keyword)
12707 || keyword_is_type_qualifier (keyword)
12708 || keyword_is_function_specifier (keyword))
12709 return true;
12710
12711 switch (keyword)
12712 {
12713 case RID_TYPEDEF:
12714 case RID_FRIEND:
12715 case RID_CONSTEXPR:
12716 return true;
12717 default:
12718 return false;
12719 }
12720}
12721
9b88d08d 12722/* Initialize language-specific-bits of tree_contains_struct. */
12723
12724void
12725c_common_init_ts (void)
12726{
12727 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12728 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
3c6d4197 12729 MARK_TS_TYPED (ARRAY_NOTATION_REF);
9b88d08d 12730}
12731
244db24d 12732/* Build a user-defined numeric literal out of an integer constant type VALUE
12733 with identifier SUFFIX. */
12734
12735tree
324ca377 12736build_userdef_literal (tree suffix_id, tree value,
12737 enum overflow_type overflow, tree num_string)
244db24d 12738{
12739 tree literal = make_node (USERDEF_LITERAL);
12740 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12741 USERDEF_LITERAL_VALUE (literal) = value;
324ca377 12742 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
244db24d 12743 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12744 return literal;
12745}
12746
7345b977 12747/* For vector[index], convert the vector to an array of the underlying type.
12748 Return true if the resulting ARRAY_REF should not be an lvalue. */
c61ef207 12749
12750bool
7345b977 12751convert_vector_to_array_for_subscript (location_t loc,
12752 tree *vecp, tree index)
7059d45d 12753{
c61ef207 12754 bool ret = false;
6290f0db 12755 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
7059d45d 12756 {
12757 tree type = TREE_TYPE (*vecp);
7059d45d 12758
c61ef207 12759 ret = !lvalue_p (*vecp);
7345b977 12760
7059d45d 12761 if (TREE_CODE (index) == INTEGER_CST)
e913b5cd 12762 if (!tree_fits_uhwi_p (index)
aa59f000 12763 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7059d45d 12764 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12765
7345b977 12766 /* We are building an ARRAY_REF so mark the vector as addressable
12767 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
12768 for function parameters. */
12769 c_common_mark_addressable_vec (*vecp);
12770
12771 *vecp = build1 (VIEW_CONVERT_EXPR,
12772 build_array_type_nelts (TREE_TYPE (type),
12773 TYPE_VECTOR_SUBPARTS (type)),
12774 *vecp);
7059d45d 12775 }
c61ef207 12776 return ret;
7059d45d 12777}
12778
41ed701a 12779/* Determine which of the operands, if any, is a scalar that needs to be
12780 converted to a vector, for the range of operations. */
12781enum stv_conv
12782scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12783 bool complain)
12784{
12785 tree type0 = TREE_TYPE (op0);
12786 tree type1 = TREE_TYPE (op1);
12787 bool integer_only_op = false;
12788 enum stv_conv ret = stv_firstarg;
12789
6290f0db 12790 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
41ed701a 12791 switch (code)
12792 {
12793 /* Most GENERIC binary expressions require homogeneous arguments.
12794 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12795 argument that is a vector and a second one that is a scalar, so
12796 we never return stv_secondarg for them. */
12797 case RSHIFT_EXPR:
12798 case LSHIFT_EXPR:
12799 if (TREE_CODE (type0) == INTEGER_TYPE
12800 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12801 {
22a75734 12802 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12803 {
12804 if (complain)
12805 error_at (loc, "conversion of scalar %qT to vector %qT "
12806 "involves truncation", type0, type1);
12807 return stv_error;
12808 }
12809 else
12810 return stv_firstarg;
12811 }
12812 break;
12813
12814 case BIT_IOR_EXPR:
12815 case BIT_XOR_EXPR:
12816 case BIT_AND_EXPR:
12817 integer_only_op = true;
e3533433 12818 /* fall through */
41ed701a 12819
7b463b19 12820 case VEC_COND_EXPR:
12821
41ed701a 12822 case PLUS_EXPR:
12823 case MINUS_EXPR:
12824 case MULT_EXPR:
12825 case TRUNC_DIV_EXPR:
12826 case CEIL_DIV_EXPR:
12827 case FLOOR_DIV_EXPR:
12828 case ROUND_DIV_EXPR:
12829 case EXACT_DIV_EXPR:
12830 case TRUNC_MOD_EXPR:
12831 case FLOOR_MOD_EXPR:
12832 case RDIV_EXPR:
12833 case EQ_EXPR:
12834 case NE_EXPR:
12835 case LE_EXPR:
12836 case GE_EXPR:
12837 case LT_EXPR:
12838 case GT_EXPR:
12839 /* What about UNLT_EXPR? */
6290f0db 12840 if (VECTOR_TYPE_P (type0))
41ed701a 12841 {
41ed701a 12842 ret = stv_secondarg;
a4f59596 12843 std::swap (type0, type1);
12844 std::swap (op0, op1);
41ed701a 12845 }
12846
12847 if (TREE_CODE (type0) == INTEGER_TYPE
12848 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12849 {
22a75734 12850 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12851 {
12852 if (complain)
12853 error_at (loc, "conversion of scalar %qT to vector %qT "
12854 "involves truncation", type0, type1);
12855 return stv_error;
12856 }
12857 return ret;
12858 }
12859 else if (!integer_only_op
12860 /* Allow integer --> real conversion if safe. */
12861 && (TREE_CODE (type0) == REAL_TYPE
12862 || TREE_CODE (type0) == INTEGER_TYPE)
12863 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12864 {
22a75734 12865 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12866 {
12867 if (complain)
12868 error_at (loc, "conversion of scalar %qT to vector %qT "
12869 "involves truncation", type0, type1);
12870 return stv_error;
12871 }
12872 return ret;
12873 }
12874 default:
12875 break;
12876 }
12877
12878 return stv_nothing;
12879}
12880
ffcdbf9c 12881/* Return true iff ALIGN is an integral constant that is a fundamental
12882 alignment, as defined by [basic.align] in the c++-11
12883 specifications.
12884
12885 That is:
12886
12887 [A fundamental alignment is represented by an alignment less than or
12888 equal to the greatest alignment supported by the implementation
12889 in all contexts, which is equal to
12890 alignof(max_align_t)]. */
12891
12892bool
12893cxx_fundamental_alignment_p (unsigned align)
12894{
12895 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12896 TYPE_ALIGN (long_double_type_node)));
12897}
12898
46da3601 12899/* Return true if T is a pointer to a zero-sized aggregate. */
12900
12901bool
12902pointer_to_zero_sized_aggr_p (tree t)
12903{
12904 if (!POINTER_TYPE_P (t))
12905 return false;
12906 t = TREE_TYPE (t);
12907 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12908}
12909
547c6b1f 12910/* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
12911 with no library fallback or for an ADDR_EXPR whose operand is such type
12912 issues an error pointing to the location LOC.
12913 Returns true when the expression has been diagnosed and false
12914 otherwise. */
12915bool
12916reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
12917{
12918 if (TREE_CODE (expr) == ADDR_EXPR)
12919 expr = TREE_OPERAND (expr, 0);
12920
12921 if (TREE_TYPE (expr)
12922 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
12cc1225 12923 && TREE_CODE (expr) == FUNCTION_DECL
547c6b1f 12924 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
12925 false positives for user-declared built-ins such as abs or
12926 strlen, and for C++ operators new and delete.
12927 The c_decl_implicit() test avoids false positives for implicitly
12928 declared built-ins with library fallbacks (such as abs). */
12929 && DECL_BUILT_IN (expr)
12930 && DECL_IS_BUILTIN (expr)
12931 && !c_decl_implicit (expr)
12932 && !DECL_ASSEMBLER_NAME_SET_P (expr))
12933 {
12934 if (loc == UNKNOWN_LOCATION)
12935 loc = EXPR_LOC_OR_LOC (expr, input_location);
12936
12937 /* Reject arguments that are built-in functions with
12938 no library fallback. */
12939 error_at (loc, "built-in function %qE must be directly called", expr);
12940
12941 return true;
12942 }
12943
12944 return false;
12945}
12946
ef17a71a 12947/* If we're creating an if-else-if condition chain, first see if we
12948 already have this COND in the CHAIN. If so, warn and don't add COND
12949 into the vector, otherwise add the COND there. LOC is the location
12950 of COND. */
12951
12952void
12953warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
12954{
12955 /* No chain has been created yet. Do nothing. */
12956 if (*chain == NULL)
12957 return;
12958
12959 if (TREE_SIDE_EFFECTS (cond))
12960 {
12961 /* Uh-oh! This condition has a side-effect, thus invalidates
12962 the whole chain. */
12963 delete *chain;
12964 *chain = NULL;
12965 return;
12966 }
12967
12968 unsigned int ix;
12969 tree t;
12970 bool found = false;
12971 FOR_EACH_VEC_ELT (**chain, ix, t)
12972 if (operand_equal_p (cond, t, 0))
12973 {
12974 if (warning_at (loc, OPT_Wduplicated_cond,
12975 "duplicated %<if%> condition"))
12976 inform (EXPR_LOCATION (t), "previously used here");
12977 found = true;
12978 break;
12979 }
12980
12981 if (!found
12982 && !CONSTANT_CLASS_P (cond)
12983 /* Don't infinitely grow the chain. */
12984 && (*chain)->length () < 512)
12985 (*chain)->safe_push (cond);
12986}
12987
81b1b2a8 12988/* Check if array size calculations overflow or if the array covers more
12989 than half of the address space. Return true if the size of the array
12990 is valid, false otherwise. TYPE is the type of the array and NAME is
12991 the name of the array, or NULL_TREE for unnamed arrays. */
12992
12993bool
12994valid_array_size_p (location_t loc, tree type, tree name)
12995{
12996 if (type != error_mark_node
12997 && COMPLETE_TYPE_P (type)
12998 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
12999 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
13000 {
13001 if (name)
13002 error_at (loc, "size of array %qE is too large", name);
13003 else
13004 error_at (loc, "size of unnamed array is too large");
13005 return false;
13006 }
13007 return true;
13008}
13009
e3e8c48c 13010/* Read SOURCE_DATE_EPOCH from environment to have a deterministic
13011 timestamp to replace embedded current dates to get reproducible
13012 results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
dfa5c0d3 13013
e3e8c48c 13014time_t
dfa5c0d3 13015cb_get_source_date_epoch (cpp_reader *pfile ATTRIBUTE_UNUSED)
e3e8c48c 13016{
13017 char *source_date_epoch;
bd08c370 13018 int64_t epoch;
e3e8c48c 13019 char *endptr;
13020
13021 source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
13022 if (!source_date_epoch)
13023 return (time_t) -1;
13024
13025 errno = 0;
bd08c370 13026#if defined(INT64_T_IS_LONG)
13027 epoch = strtol (source_date_epoch, &endptr, 10);
13028#else
e3e8c48c 13029 epoch = strtoll (source_date_epoch, &endptr, 10);
bd08c370 13030#endif
dfa5c0d3 13031 if (errno != 0 || endptr == source_date_epoch || *endptr != '\0'
13032 || epoch < 0 || epoch > MAX_SOURCE_DATE_EPOCH)
13033 {
13034 error_at (input_location, "environment variable SOURCE_DATE_EPOCH must "
13035 "expand to a non-negative integer less than or equal to %wd",
13036 MAX_SOURCE_DATE_EPOCH);
13037 return (time_t) -1;
13038 }
e3e8c48c 13039
13040 return (time_t) epoch;
13041}
13042
5c8151fa 13043/* Callback for libcpp for offering spelling suggestions for misspelled
13044 directives. GOAL is an unrecognized string; CANDIDATES is a
13045 NULL-terminated array of candidate strings. Return the closest
13046 match to GOAL within CANDIDATES, or NULL if none are good
13047 suggestions. */
13048
13049const char *
13050cb_get_suggestion (cpp_reader *, const char *goal,
13051 const char *const *candidates)
13052{
13053 best_match<const char *, const char *> bm (goal);
13054 while (*candidates)
13055 bm.consider (*candidates++);
13056 return bm.get_best_meaningful_candidate ();
13057}
13058
7f8012a5 13059/* Check and possibly warn if two declarations have contradictory
13060 attributes, such as always_inline vs. noinline. */
13061
13062bool
13063diagnose_mismatched_attributes (tree olddecl, tree newdecl)
13064{
13065 bool warned = false;
13066
13067 tree a1 = lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl));
13068 tree a2 = lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl));
13069 /* An optimization attribute applied on a declaration after the
13070 definition is likely not what the user wanted. */
13071 if (a2 != NULL_TREE
13072 && DECL_SAVED_TREE (olddecl) != NULL_TREE
13073 && (a1 == NULL_TREE || !attribute_list_equal (a1, a2)))
13074 warned |= warning (OPT_Wattributes,
13075 "optimization attribute on %qD follows "
13076 "definition but the attribute doesn%'t match",
13077 newdecl);
13078
13079 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
13080 if (DECL_DECLARED_INLINE_P (newdecl)
13081 && DECL_UNINLINABLE (olddecl)
13082 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
13083 warned |= warning (OPT_Wattributes, "inline declaration of %qD follows "
13084 "declaration with attribute noinline", newdecl);
13085 else if (DECL_DECLARED_INLINE_P (olddecl)
13086 && DECL_UNINLINABLE (newdecl)
13087 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
13088 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
13089 "noinline follows inline declaration ", newdecl);
13090 else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))
13091 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (olddecl)))
13092 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
13093 "%qs follows declaration with attribute %qs",
13094 newdecl, "noinline", "always_inline");
13095 else if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (newdecl))
13096 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
13097 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
13098 "%qs follows declaration with attribute %qs",
13099 newdecl, "always_inline", "noinline");
13100 else if (lookup_attribute ("cold", DECL_ATTRIBUTES (newdecl))
13101 && lookup_attribute ("hot", DECL_ATTRIBUTES (olddecl)))
13102 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
13103 "%qs follows declaration with attribute %qs",
13104 newdecl, "cold", "hot");
13105 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (newdecl))
13106 && lookup_attribute ("cold", DECL_ATTRIBUTES (olddecl)))
13107 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
13108 "%qs follows declaration with attribute %qs",
13109 newdecl, "hot", "cold");
13110 return warned;
13111}
13112
7bedc3a0 13113#include "gt-c-family-c-common.h"