]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-common.c
* config/i386/i386.c (classify_argument): Use CEIL where applicable.
[thirdparty/gcc.git] / gcc / c-family / c-common.c
CommitLineData
b0fc3e72 1/* Subroutines shared by all languages that are variants of C.
d353bf18 2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
b0fc3e72 3
f12b58b3 4This file is part of GCC.
b0fc3e72 5
f12b58b3 6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8c4c00c1 8Software Foundation; either version 3, or (at your option) any later
f12b58b3 9version.
b0fc3e72 10
f12b58b3 11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
b0fc3e72 15
16You should have received a copy of the GNU General Public License
8c4c00c1 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
b0fc3e72 19
b20a8bb4 20#define GCC_C_COMMON_C
21
b0fc3e72 22#include "config.h"
405711de 23#include "system.h"
805e22b2 24#include "coretypes.h"
7ff8db31 25#include "c-common.h"
805e22b2 26#include "tm.h"
e48d0f41 27#include "intl.h"
b0fc3e72 28#include "tree.h"
9ed99284 29#include "fold-const.h"
30#include "stor-layout.h"
31#include "calls.h"
32#include "stringpool.h"
33#include "attribs.h"
34#include "varasm.h"
35#include "trans-mem.h"
b0fc3e72 36#include "flags.h"
a3fa7feb 37#include "c-pragma.h"
6c536c4f 38#include "c-objc.h"
d8c9779c 39#include "tm_p.h"
4e91a871 40#include "obstack.h"
a654e028 41#include "cpplib.h"
8ee295a7 42#include "target.h"
218e3e4e 43#include "common/common-target.h"
96554925 44#include "langhooks.h"
f3dde807 45#include "tree-inline.h"
69579044 46#include "toplev.h"
7f5f3953 47#include "diagnostic.h"
4ee9c684 48#include "tree-iterator.h"
e08bd2f4 49#include "opts.h"
1140c305 50#include "hard-reg-set.h"
1140c305 51#include "function.h"
62eec3b4 52#include "cgraph.h"
a8783bee 53#include "gimplify.h"
e913b5cd 54#include "wide-int-print.h"
c61ef207 55#include "gimple-expr.h"
fd6f6435 56
90cc7820 57cpp_reader *parse_in; /* Declared in c-pragma.h. */
a654e028 58
de801c28 59/* Mode used to build pointers (VOIDmode means ptr_mode). */
60
61machine_mode c_default_pointer_mode = VOIDmode;
62
72040e7e 63/* The following symbols are subsumed in the c_global_trees array, and
44e9fa65 64 listed here individually for documentation purposes.
72040e7e 65
66 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
67
68 tree short_integer_type_node;
69 tree long_integer_type_node;
70 tree long_long_integer_type_node;
71
72 tree short_unsigned_type_node;
73 tree long_unsigned_type_node;
74 tree long_long_unsigned_type_node;
75
3c2239cf 76 tree truthvalue_type_node;
77 tree truthvalue_false_node;
78 tree truthvalue_true_node;
72040e7e 79
80 tree ptrdiff_type_node;
81
82 tree unsigned_char_type_node;
83 tree signed_char_type_node;
84 tree wchar_type_node;
72040e7e 85
924bbf02 86 tree char16_type_node;
87 tree char32_type_node;
88
72040e7e 89 tree float_type_node;
90 tree double_type_node;
91 tree long_double_type_node;
92
93 tree complex_integer_type_node;
94 tree complex_float_type_node;
95 tree complex_double_type_node;
96 tree complex_long_double_type_node;
97
c4503c0a 98 tree dfloat32_type_node;
99 tree dfloat64_type_node;
100 tree_dfloat128_type_node;
101
72040e7e 102 tree intQI_type_node;
103 tree intHI_type_node;
104 tree intSI_type_node;
105 tree intDI_type_node;
106 tree intTI_type_node;
107
108 tree unsigned_intQI_type_node;
109 tree unsigned_intHI_type_node;
110 tree unsigned_intSI_type_node;
111 tree unsigned_intDI_type_node;
112 tree unsigned_intTI_type_node;
113
114 tree widest_integer_literal_type_node;
115 tree widest_unsigned_literal_type_node;
116
117 Nodes for types `void *' and `const void *'.
118
119 tree ptr_type_node, const_ptr_type_node;
120
121 Nodes for types `char *' and `const char *'.
122
123 tree string_type_node, const_string_type_node;
124
125 Type `char[SOMENUMBER]'.
126 Used when an array of char is needed and the size is irrelevant.
127
128 tree char_array_type_node;
129
72040e7e 130 Type `wchar_t[SOMENUMBER]' or something like it.
131 Used when a wide string literal is created.
132
133 tree wchar_array_type_node;
134
924bbf02 135 Type `char16_t[SOMENUMBER]' or something like it.
136 Used when a UTF-16 string literal is created.
137
138 tree char16_array_type_node;
139
140 Type `char32_t[SOMENUMBER]' or something like it.
141 Used when a UTF-32 string literal is created.
142
143 tree char32_array_type_node;
144
72040e7e 145 Type `int ()' -- used for implicit declaration of functions.
146
147 tree default_function_type;
148
72040e7e 149 A VOID_TYPE node, packaged in a TREE_LIST.
150
151 tree void_list_node;
152
734c98be 153 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
65b7f83f 154 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
155 VAR_DECLS, but C++ does.)
71d9fc9b 156
65b7f83f 157 tree function_name_decl_node;
734c98be 158 tree pretty_function_name_decl_node;
65b7f83f 159 tree c99_function_name_decl_node;
160
161 Stack of nested function name VAR_DECLs.
1cae46be 162
65b7f83f 163 tree saved_function_name_decls;
71d9fc9b 164
72040e7e 165*/
166
167tree c_global_trees[CTI_MAX];
573aba85 168\f
574a6990 169/* Switches common to the C front ends. */
170
f7070933 171/* Nonzero means don't output line number information. */
172
173char flag_no_line_commands;
174
175/* Nonzero causes -E output not to be done, but directives such as
176 #define that have side effects are still obeyed. */
177
178char flag_no_output;
179
180/* Nonzero means dump macros in some fashion. */
181
182char flag_dump_macros;
183
184/* Nonzero means pass #include lines through to the output. */
185
186char flag_dump_includes;
187
d718b525 188/* Nonzero means process PCH files while preprocessing. */
189
190bool flag_pch_preprocess;
191
573aba85 192/* The file name to which we should write a precompiled header, or
193 NULL if no header will be written in this compile. */
194
195const char *pch_file;
196
1ed9d5f5 197/* Nonzero if an ISO standard was selected. It rejects macros in the
198 user's namespace. */
199int flag_iso;
200
574a6990 201/* C/ObjC language option variables. */
202
203
574a6990 204/* Nonzero means allow type mismatches in conditional expressions;
205 just make their values `void'. */
206
207int flag_cond_mismatch;
208
209/* Nonzero means enable C89 Amendment 1 features. */
210
211int flag_isoc94;
212
32074525 213/* Nonzero means use the ISO C99 (or C11) dialect of C. */
574a6990 214
215int flag_isoc99;
216
32074525 217/* Nonzero means use the ISO C11 dialect of C. */
39012afb 218
32074525 219int flag_isoc11;
39012afb 220
8b332087 221/* Nonzero means that we have builtin functions, and main is an int. */
574a6990 222
223int flag_hosted = 1;
224
574a6990 225
226/* ObjC language option variables. */
227
228
574a6990 229/* Tells the compiler that this is a special run. Do not perform any
230 compiling, instead we are to test some platform dependent features
231 and output a C header file with appropriate definitions. */
232
233int print_struct_values;
234
f0b5f617 235/* Tells the compiler what is the constant string class for ObjC. */
574a6990 236
237const char *constant_string_class_name;
238
574a6990 239
240/* C++ language option variables. */
241
229a58b1 242/* The reference version of the ABI for -Wabi. */
243
244int warn_abi_version = -1;
574a6990 245
574a6990 246/* Nonzero means generate separate instantiation control files and
247 juggle them at link time. */
248
249int flag_use_repository;
250
d875b9d2 251/* The C++ dialect being used. Default set in c_common_post_options. */
0fe6eeac 252
d875b9d2 253enum cxx_dialect cxx_dialect = cxx_unset;
0fe6eeac 254
9dcd0d49 255/* Maximum template instantiation depth. This limit exists to limit the
8ce59854 256 time it takes to notice excessively recursive template instantiations.
574a6990 257
8ce59854 258 The default is lower than the 1024 recommended by the C++0x standard
259 because G++ runs out of stack before 1024 with highly recursive template
260 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
261
262int max_tinst_depth = 900;
574a6990 263
988fc1d1 264/* The elements of `ridpointers' are identifier nodes for the reserved
265 type names and storage classes. It is indexed by a RID_... value. */
266tree *ridpointers;
267
e60a6f7b 268tree (*make_fname_decl) (location_t, tree, int);
9e5a737d 269
48d94ede 270/* Nonzero means don't warn about problems that occur when the code is
271 executed. */
272int c_inhibit_evaluation_warnings;
e78703c1 273
93be21c0 274/* Whether we are building a boolean conversion inside
275 convert_for_assignment, or some other late binary operation. If
276 build_binary_op is called for C (from code shared by C and C++) in
277 this case, then the operands have already been folded and the
278 result will not be folded again, so C_MAYBE_CONST_EXPR should not
279 be generated. */
280bool in_late_binary_op;
281
7f5f3953 282/* Whether lexing has been completed, so subsequent preprocessor
283 errors should use the compiler's input_location. */
284bool done_lexing = false;
285
2c0e001b 286/* Information about how a function name is generated. */
65b7f83f 287struct fname_var_t
288{
e99c3a1d 289 tree *const decl; /* pointer to the VAR_DECL. */
290 const unsigned rid; /* RID number for the identifier. */
291 const int pretty; /* How pretty is it? */
65b7f83f 292};
293
2c0e001b 294/* The three ways of getting then name of the current function. */
65b7f83f 295
296const struct fname_var_t fname_vars[] =
297{
2c0e001b 298 /* C99 compliant __func__, must be first. */
65b7f83f 299 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
2c0e001b 300 /* GCC __FUNCTION__ compliant. */
65b7f83f 301 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
2c0e001b 302 /* GCC __PRETTY_FUNCTION__ compliant. */
65b7f83f 303 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
304 {NULL, 0, 0},
305};
306
79396169 307/* Global visibility options. */
308struct visibility_flags visibility_options;
309
aac24642 310static tree c_fully_fold_internal (tree expr, bool, bool *, bool *, bool);
2d2f6a15 311static tree check_case_value (location_t, tree);
be23b16f 312static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
313 bool *);
be43ff5a 314
1cae46be 315static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
316static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
317static tree handle_common_attribute (tree *, tree, tree, int, bool *);
318static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
5de92639 319static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
320static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
a9196da9 321static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
322 int, bool *);
d413ffdd 323static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
324 int, bool *);
05f893e1 325static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
326 bool *);
947aa916 327static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
1cae46be 328static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
bdb1f0d1 329static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
85fbea97 330static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
7bd95dfd 331static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
1cae46be 332static tree handle_always_inline_attribute (tree *, tree, tree, int,
333 bool *);
1b16fc45 334static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
335static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
0cdd9887 336static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
10fc867f 337static tree handle_error_attribute (tree *, tree, tree, int, bool *);
1cae46be 338static tree handle_used_attribute (tree *, tree, tree, int, bool *);
339static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
62eec3b4 340static tree handle_externally_visible_attribute (tree *, tree, tree, int,
341 bool *);
6b722052 342static tree handle_no_reorder_attribute (tree *, tree, tree, int,
343 bool *);
1cae46be 344static tree handle_const_attribute (tree *, tree, tree, int, bool *);
345static tree handle_transparent_union_attribute (tree *, tree, tree,
346 int, bool *);
347static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
348static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
349static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
350static tree handle_section_attribute (tree *, tree, tree, int, bool *);
351static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
352static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
4641882f 353static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
85c0a25c 354static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
355static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
1cae46be 356static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
f4a30bd7 357static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
1cae46be 358static tree handle_visibility_attribute (tree *, tree, tree, int,
359 bool *);
360static tree handle_tls_model_attribute (tree *, tree, tree, int,
361 bool *);
362static tree handle_no_instrument_function_attribute (tree *, tree,
363 tree, int, bool *);
364static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
26d1c5ff 365static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
1cae46be 366static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
367 bool *);
368static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
4c0315d0 369static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
370static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
fc09b200 371static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
1cae46be 372static tree handle_deprecated_attribute (tree *, tree, tree, int,
373 bool *);
374static tree handle_vector_size_attribute (tree *, tree, tree, int,
375 bool *);
376static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
377static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
378static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
8a8cdb8d 379static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
380 bool *);
bf6c8de0 381static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
b5c26b42 382static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
4a29c97c 383static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
237e78b1 384static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
385static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
24470055 386static tree handle_target_attribute (tree *, tree, tree, int, bool *);
46f8e3b0 387static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
4c0315d0 388static tree ignore_attribute (tree *, tree, tree, int, bool *);
48b14f50 389static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
8ce86007 390static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
a96c3cc1 391static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
d7dcba40 392static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
bc7bff74 393static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
394 bool *);
395static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
396 bool *);
74691f46 397static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
058a1b7a 398static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
399static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
400static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
1cae46be 401
d01f58f9 402static void check_function_nonnull (tree, int, tree *);
1cae46be 403static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
404static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
405static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
860251be 406static int resort_field_decl_cmp (const void *, const void *);
dbf6c367 407
5c6e5756 408/* Reserved words. The third field is a mask: keywords are disabled
409 if they match the mask.
410
411 Masks for languages:
412 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
413 C --std=c99: D_CXXONLY | D_OBJC
414 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
415 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
416 C++ --std=c0x: D_CONLY | D_OBJC
417 ObjC++ is like C++ except that D_OBJC is not set
418
419 If -fno-asm is used, D_ASM is added to the mask. If
420 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
421 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
b27e241e 422 In C with -Wc++-compat, we warn if D_CXXWARN is set.
5c6e5756 423
b27e241e 424 Note the complication of the D_CXX_OBJC keywords. These are
425 reserved words such as 'class'. In C++, 'class' is a reserved
426 word. In Objective-C++ it is too. In Objective-C, it is a
427 reserved word too, but only if it follows an '@' sign.
428*/
5c6e5756 429const struct c_common_resword c_common_reswords[] =
430{
83e25171 431 { "_Alignas", RID_ALIGNAS, D_CONLY },
432 { "_Alignof", RID_ALIGNOF, D_CONLY },
b560fabd 433 { "_Atomic", RID_ATOMIC, D_CONLY },
5c6e5756 434 { "_Bool", RID_BOOL, D_CONLY },
435 { "_Complex", RID_COMPLEX, 0 },
d037099f 436 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
437 { "_Cilk_sync", RID_CILK_SYNC, 0 },
40750995 438 { "_Cilk_for", RID_CILK_FOR, 0 },
c1800156 439 { "_Imaginary", RID_IMAGINARY, D_CONLY },
5c6e5756 440 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
441 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
442 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
443 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
444 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
445 { "_Sat", RID_SAT, D_CONLY | D_EXT },
f80e7755 446 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
985c6e3a 447 { "_Noreturn", RID_NORETURN, D_CONLY },
7aa04c8d 448 { "_Generic", RID_GENERIC, D_CONLY },
d184e0c0 449 { "_Thread_local", RID_THREAD, D_CONLY },
5c6e5756 450 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
451 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
452 { "__alignof", RID_ALIGNOF, 0 },
453 { "__alignof__", RID_ALIGNOF, 0 },
454 { "__asm", RID_ASM, 0 },
455 { "__asm__", RID_ASM, 0 },
456 { "__attribute", RID_ATTRIBUTE, 0 },
457 { "__attribute__", RID_ATTRIBUTE, 0 },
4fba5eb9 458 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
e6014a82 459 { "__bases", RID_BASES, D_CXXONLY },
6f58cf06 460 { "__builtin_call_with_static_chain",
461 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
5c6e5756 462 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
bff4ad11 463 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
bf0cb017 464 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
5c6e5756 465 { "__builtin_offsetof", RID_OFFSETOF, 0 },
466 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
467 { "__builtin_va_arg", RID_VA_ARG, 0 },
468 { "__complex", RID_COMPLEX, 0 },
469 { "__complex__", RID_COMPLEX, 0 },
470 { "__const", RID_CONST, 0 },
471 { "__const__", RID_CONST, 0 },
472 { "__decltype", RID_DECLTYPE, D_CXXONLY },
e6014a82 473 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
5c6e5756 474 { "__extension__", RID_EXTENSION, 0 },
475 { "__func__", RID_C99_FUNCTION_NAME, 0 },
476 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
477 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
478 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
479 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
480 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
481 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
482 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
483 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
23407dc9 484 { "__imag", RID_IMAGPART, 0 },
485 { "__imag__", RID_IMAGPART, 0 },
486 { "__inline", RID_INLINE, 0 },
487 { "__inline__", RID_INLINE, 0 },
5c6e5756 488 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
489 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
490 { "__is_class", RID_IS_CLASS, D_CXXONLY },
5c6e5756 491 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
492 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
aa4313eb 493 { "__is_final", RID_IS_FINAL, D_CXXONLY },
23407dc9 494 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
5c6e5756 495 { "__is_pod", RID_IS_POD, D_CXXONLY },
496 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
56c12fd4 497 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
c1c67b4f 498 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
499 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
f76a9aa8 500 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
501 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
717e52f9 502 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
5c6e5756 503 { "__is_union", RID_IS_UNION, D_CXXONLY },
5c6e5756 504 { "__label__", RID_LABEL, 0 },
505 { "__null", RID_NULL, 0 },
506 { "__real", RID_REALPART, 0 },
507 { "__real__", RID_REALPART, 0 },
508 { "__restrict", RID_RESTRICT, 0 },
509 { "__restrict__", RID_RESTRICT, 0 },
510 { "__signed", RID_SIGNED, 0 },
511 { "__signed__", RID_SIGNED, 0 },
512 { "__thread", RID_THREAD, 0 },
4c0315d0 513 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
514 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
515 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
5c6e5756 516 { "__typeof", RID_TYPEOF, 0 },
517 { "__typeof__", RID_TYPEOF, 0 },
23407dc9 518 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
5c6e5756 519 { "__volatile", RID_VOLATILE, 0 },
520 { "__volatile__", RID_VOLATILE, 0 },
fa769cc5 521 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
522 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 523 { "asm", RID_ASM, D_ASM },
524 { "auto", RID_AUTO, 0 },
dbd982c9 525 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
5c6e5756 526 { "break", RID_BREAK, 0 },
527 { "case", RID_CASE, 0 },
51030405 528 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
5c6e5756 529 { "char", RID_CHAR, 0 },
fa769cc5 530 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
531 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
51030405 532 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
5c6e5756 533 { "const", RID_CONST, 0 },
fa769cc5 534 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 535 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
536 { "continue", RID_CONTINUE, 0 },
fa769cc5 537 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 538 { "default", RID_DEFAULT, 0 },
51030405 539 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
5c6e5756 540 { "do", RID_DO, 0 },
541 { "double", RID_DOUBLE, 0 },
542 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
543 { "else", RID_ELSE, 0 },
544 { "enum", RID_ENUM, 0 },
51030405 545 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
546 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
5c6e5756 547 { "extern", RID_EXTERN, 0 },
51030405 548 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
5c6e5756 549 { "float", RID_FLOAT, 0 },
550 { "for", RID_FOR, 0 },
51030405 551 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
5c6e5756 552 { "goto", RID_GOTO, 0 },
553 { "if", RID_IF, 0 },
554 { "inline", RID_INLINE, D_EXT89 },
555 { "int", RID_INT, 0 },
556 { "long", RID_LONG, 0 },
557 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
51030405 558 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
559 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
fa769cc5 560 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
561 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
51030405 562 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
563 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
564 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
565 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
5c6e5756 566 { "register", RID_REGISTER, 0 },
567 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
568 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
569 { "return", RID_RETURN, 0 },
570 { "short", RID_SHORT, 0 },
571 { "signed", RID_SIGNED, 0 },
572 { "sizeof", RID_SIZEOF, 0 },
573 { "static", RID_STATIC, 0 },
fa769cc5 574 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 575 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
576 { "struct", RID_STRUCT, 0 },
577 { "switch", RID_SWITCH, 0 },
51030405 578 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
579 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
fa769cc5 580 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
51030405 581 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
582 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
583 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
5c6e5756 584 { "typedef", RID_TYPEDEF, 0 },
51030405 585 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
586 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
5c6e5756 587 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
588 { "union", RID_UNION, 0 },
589 { "unsigned", RID_UNSIGNED, 0 },
51030405 590 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
591 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
5c6e5756 592 { "void", RID_VOID, 0 },
593 { "volatile", RID_VOLATILE, 0 },
594 { "wchar_t", RID_WCHAR, D_CXXONLY },
595 { "while", RID_WHILE, 0 },
56c12fd4 596
6d02e6b2 597 /* C++ transactional memory. */
598 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
599 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
600 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
601 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
602
56c12fd4 603 /* Concepts-related keywords */
604 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
605 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
606
5c6e5756 607 /* These Objective-C keywords are recognized only immediately after
608 an '@'. */
609 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
610 { "defs", RID_AT_DEFS, D_OBJC },
611 { "encode", RID_AT_ENCODE, D_OBJC },
612 { "end", RID_AT_END, D_OBJC },
613 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
614 { "interface", RID_AT_INTERFACE, D_OBJC },
615 { "protocol", RID_AT_PROTOCOL, D_OBJC },
616 { "selector", RID_AT_SELECTOR, D_OBJC },
617 { "finally", RID_AT_FINALLY, D_OBJC },
069761fb 618 { "optional", RID_AT_OPTIONAL, D_OBJC },
619 { "required", RID_AT_REQUIRED, D_OBJC },
86c110ac 620 { "property", RID_AT_PROPERTY, D_OBJC },
4a8875ed 621 { "package", RID_AT_PACKAGE, D_OBJC },
e1f293c0 622 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
623 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
5c6e5756 624 /* These are recognized only in protocol-qualifier context
625 (see above) */
626 { "bycopy", RID_BYCOPY, D_OBJC },
627 { "byref", RID_BYREF, D_OBJC },
628 { "in", RID_IN, D_OBJC },
629 { "inout", RID_INOUT, D_OBJC },
630 { "oneway", RID_ONEWAY, D_OBJC },
631 { "out", RID_OUT, D_OBJC },
86c110ac 632 /* These are recognized inside a property attribute list */
7590f0e5 633 { "assign", RID_ASSIGN, D_OBJC },
634 { "copy", RID_COPY, D_OBJC },
86c110ac 635 { "getter", RID_GETTER, D_OBJC },
7590f0e5 636 { "nonatomic", RID_NONATOMIC, D_OBJC },
637 { "readonly", RID_READONLY, D_OBJC },
638 { "readwrite", RID_READWRITE, D_OBJC },
639 { "retain", RID_RETAIN, D_OBJC },
86c110ac 640 { "setter", RID_SETTER, D_OBJC },
5c6e5756 641};
642
643const unsigned int num_c_common_reswords =
644 sizeof c_common_reswords / sizeof (struct c_common_resword);
645
058a1b7a 646/* Table of machine-independent attributes common to all C-like languages.
647
648 All attributes referencing arguments should be additionally processed
649 in chkp_copy_function_type_adding_bounds for correct instrumentation
650 by Pointer Bounds Checker.
651 Current list of processed common attributes: nonnull. */
f8e93a2e 652const struct attribute_spec c_common_attribute_table[] =
653{
ac86af5d 654 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
655 affects_type_identity } */
f8e93a2e 656 { "packed", 0, 0, false, false, false,
ac86af5d 657 handle_packed_attribute , false},
f8e93a2e 658 { "nocommon", 0, 0, true, false, false,
ac86af5d 659 handle_nocommon_attribute, false},
f8e93a2e 660 { "common", 0, 0, true, false, false,
ac86af5d 661 handle_common_attribute, false },
f8e93a2e 662 /* FIXME: logically, noreturn attributes should be listed as
663 "false, true, true" and apply to function types. But implementing this
664 would require all the places in the compiler that use TREE_THIS_VOLATILE
665 on a decl to identify non-returning functions to be located and fixed
666 to check the function type instead. */
667 { "noreturn", 0, 0, true, false, false,
ac86af5d 668 handle_noreturn_attribute, false },
f8e93a2e 669 { "volatile", 0, 0, true, false, false,
ac86af5d 670 handle_noreturn_attribute, false },
947aa916 671 { "stack_protect", 0, 0, true, false, false,
672 handle_stack_protect_attribute, false },
f8e93a2e 673 { "noinline", 0, 0, true, false, false,
ac86af5d 674 handle_noinline_attribute, false },
bdb1f0d1 675 { "noclone", 0, 0, true, false, false,
ac86af5d 676 handle_noclone_attribute, false },
85fbea97 677 { "no_icf", 0, 0, true, false, false,
678 handle_noicf_attribute, false },
7bd95dfd 679 { "leaf", 0, 0, true, false, false,
ac86af5d 680 handle_leaf_attribute, false },
f8e93a2e 681 { "always_inline", 0, 0, true, false, false,
ac86af5d 682 handle_always_inline_attribute, false },
541e4101 683 { "gnu_inline", 0, 0, true, false, false,
ac86af5d 684 handle_gnu_inline_attribute, false },
1b16fc45 685 { "artificial", 0, 0, true, false, false,
ac86af5d 686 handle_artificial_attribute, false },
0cdd9887 687 { "flatten", 0, 0, true, false, false,
ac86af5d 688 handle_flatten_attribute, false },
f8e93a2e 689 { "used", 0, 0, true, false, false,
ac86af5d 690 handle_used_attribute, false },
f8e93a2e 691 { "unused", 0, 0, false, false, false,
ac86af5d 692 handle_unused_attribute, false },
62eec3b4 693 { "externally_visible", 0, 0, true, false, false,
ac86af5d 694 handle_externally_visible_attribute, false },
6b722052 695 { "no_reorder", 0, 0, true, false, false,
696 handle_no_reorder_attribute, false },
f8e93a2e 697 /* The same comments as for noreturn attributes apply to const ones. */
698 { "const", 0, 0, true, false, false,
ac86af5d 699 handle_const_attribute, false },
f8e93a2e 700 { "transparent_union", 0, 0, false, false, false,
ac86af5d 701 handle_transparent_union_attribute, false },
9af7fd5b 702 { "constructor", 0, 1, true, false, false,
ac86af5d 703 handle_constructor_attribute, false },
9af7fd5b 704 { "destructor", 0, 1, true, false, false,
ac86af5d 705 handle_destructor_attribute, false },
f8e93a2e 706 { "mode", 1, 1, false, true, false,
ac86af5d 707 handle_mode_attribute, false },
f8e93a2e 708 { "section", 1, 1, true, false, false,
ac86af5d 709 handle_section_attribute, false },
f8e93a2e 710 { "aligned", 0, 1, false, false, false,
ac86af5d 711 handle_aligned_attribute, false },
f8e93a2e 712 { "weak", 0, 0, true, false, false,
ac86af5d 713 handle_weak_attribute, false },
4641882f 714 { "noplt", 0, 0, true, false, false,
715 handle_noplt_attribute, false },
85c0a25c 716 { "ifunc", 1, 1, true, false, false,
ac86af5d 717 handle_ifunc_attribute, false },
f8e93a2e 718 { "alias", 1, 1, true, false, false,
ac86af5d 719 handle_alias_attribute, false },
f4a30bd7 720 { "weakref", 0, 1, true, false, false,
ac86af5d 721 handle_weakref_attribute, false },
f8e93a2e 722 { "no_instrument_function", 0, 0, true, false, false,
ac86af5d 723 handle_no_instrument_function_attribute,
724 false },
f8e93a2e 725 { "malloc", 0, 0, true, false, false,
ac86af5d 726 handle_malloc_attribute, false },
26d1c5ff 727 { "returns_twice", 0, 0, true, false, false,
ac86af5d 728 handle_returns_twice_attribute, false },
f8e93a2e 729 { "no_stack_limit", 0, 0, true, false, false,
ac86af5d 730 handle_no_limit_stack_attribute, false },
f8e93a2e 731 { "pure", 0, 0, true, false, false,
ac86af5d 732 handle_pure_attribute, false },
4c0315d0 733 { "transaction_callable", 0, 0, false, true, false,
734 handle_tm_attribute, false },
735 { "transaction_unsafe", 0, 0, false, true, false,
6d02e6b2 736 handle_tm_attribute, true },
4c0315d0 737 { "transaction_safe", 0, 0, false, true, false,
6d02e6b2 738 handle_tm_attribute, true },
739 { "transaction_safe_dynamic", 0, 0, true, false, false,
4c0315d0 740 handle_tm_attribute, false },
741 { "transaction_may_cancel_outer", 0, 0, false, true, false,
742 handle_tm_attribute, false },
743 /* ??? These two attributes didn't make the transition from the
744 Intel language document to the multi-vendor language document. */
745 { "transaction_pure", 0, 0, false, true, false,
746 handle_tm_attribute, false },
747 { "transaction_wrap", 1, 1, true, false, false,
748 handle_tm_wrap_attribute, false },
fc09b200 749 /* For internal use (marking of builtins) only. The name contains space
750 to prevent its usage in source code. */
751 { "no vops", 0, 0, true, false, false,
ac86af5d 752 handle_novops_attribute, false },
45c4e798 753 { "deprecated", 0, 1, false, false, false,
ac86af5d 754 handle_deprecated_attribute, false },
f8e93a2e 755 { "vector_size", 1, 1, false, true, false,
ac86af5d 756 handle_vector_size_attribute, false },
b212f378 757 { "visibility", 1, 1, false, false, false,
ac86af5d 758 handle_visibility_attribute, false },
24dfead4 759 { "tls_model", 1, 1, true, false, false,
ac86af5d 760 handle_tls_model_attribute, false },
dbf6c367 761 { "nonnull", 0, -1, false, true, true,
ac86af5d 762 handle_nonnull_attribute, false },
fa987697 763 { "nothrow", 0, 0, true, false, false,
ac86af5d 764 handle_nothrow_attribute, false },
765 { "may_alias", 0, 0, false, true, false, NULL, false },
7acb29a3 766 { "cleanup", 1, 1, true, false, false,
ac86af5d 767 handle_cleanup_attribute, false },
8a8cdb8d 768 { "warn_unused_result", 0, 0, false, true, true,
ac86af5d 769 handle_warn_unused_result_attribute, false },
50ca527f 770 { "sentinel", 0, 1, false, true, true,
ac86af5d 771 handle_sentinel_attribute, false },
b5c26b42 772 /* For internal use (marking of builtins) only. The name contains space
773 to prevent its usage in source code. */
774 { "type generic", 0, 0, false, true, true,
ac86af5d 775 handle_type_generic_attribute, false },
4a29c97c 776 { "alloc_size", 1, 2, false, true, true,
ac86af5d 777 handle_alloc_size_attribute, false },
5de92639 778 { "cold", 0, 0, true, false, false,
ac86af5d 779 handle_cold_attribute, false },
5de92639 780 { "hot", 0, 0, true, false, false,
ac86af5d 781 handle_hot_attribute, false },
d413ffdd 782 { "no_address_safety_analysis",
783 0, 0, true, false, false,
784 handle_no_address_safety_analysis_attribute,
785 false },
a9196da9 786 { "no_sanitize_address", 0, 0, true, false, false,
787 handle_no_sanitize_address_attribute,
788 false },
d1e96383 789 { "no_sanitize_thread", 0, 0, true, false, false,
790 handle_no_sanitize_address_attribute,
791 false },
05f893e1 792 { "no_sanitize_undefined", 0, 0, true, false, false,
793 handle_no_sanitize_undefined_attribute,
794 false },
10fc867f 795 { "warning", 1, 1, true, false, false,
ac86af5d 796 handle_error_attribute, false },
10fc867f 797 { "error", 1, 1, true, false, false,
ac86af5d 798 handle_error_attribute, false },
24470055 799 { "target", 1, -1, true, false, false,
ac86af5d 800 handle_target_attribute, false },
46f8e3b0 801 { "optimize", 1, -1, true, false, false,
ac86af5d 802 handle_optimize_attribute, false },
4c0315d0 803 /* For internal use only. The leading '*' both prevents its usage in
804 source code and signals that it may be overridden by machine tables. */
805 { "*tm regparm", 0, 0, false, true, true,
806 ignore_attribute, false },
48b14f50 807 { "no_split_stack", 0, 0, true, false, false,
ac86af5d 808 handle_no_split_stack_attribute, false },
8ce86007 809 /* For internal use (marking of builtins and runtime functions) only.
810 The name contains space to prevent its usage in source code. */
811 { "fn spec", 1, 1, false, true, true,
ac86af5d 812 handle_fnspec_attribute, false },
a96c3cc1 813 { "warn_unused", 0, 0, false, false, false,
814 handle_warn_unused_attribute, false },
d7dcba40 815 { "returns_nonnull", 0, 0, false, true, true,
816 handle_returns_nonnull_attribute, false },
bc7bff74 817 { "omp declare simd", 0, -1, true, false, false,
818 handle_omp_declare_simd_attribute, false },
74acc703 819 { "cilk simd function", 0, -1, true, false, false,
820 handle_omp_declare_simd_attribute, false },
bc7bff74 821 { "omp declare target", 0, 0, true, false, false,
822 handle_omp_declare_target_attribute, false },
237e78b1 823 { "alloc_align", 1, 1, false, true, true,
824 handle_alloc_align_attribute, false },
825 { "assume_aligned", 1, 2, false, true, true,
826 handle_assume_aligned_attribute, false },
74691f46 827 { "designated_init", 0, 0, false, true, false,
828 handle_designated_init_attribute, false },
058a1b7a 829 { "bnd_variable_size", 0, 0, true, false, false,
830 handle_bnd_variable_size_attribute, false },
831 { "bnd_legacy", 0, 0, true, false, false,
832 handle_bnd_legacy, false },
833 { "bnd_instrument", 0, 0, true, false, false,
834 handle_bnd_instrument, false },
ac86af5d 835 { NULL, 0, 0, false, false, false, NULL, false }
f8e93a2e 836};
837
838/* Give the specifications for the format attributes, used by C and all
058a1b7a 839 descendants.
f8e93a2e 840
058a1b7a 841 All attributes referencing arguments should be additionally processed
842 in chkp_copy_function_type_adding_bounds for correct instrumentation
843 by Pointer Bounds Checker.
844 Current list of processed format attributes: format, format_arg. */
f8e93a2e 845const struct attribute_spec c_common_format_attribute_table[] =
846{
ac86af5d 847 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
848 affects_type_identity } */
f8e93a2e 849 { "format", 3, 3, false, true, true,
ac86af5d 850 handle_format_attribute, false },
f8e93a2e 851 { "format_arg", 1, 1, false, true, true,
ac86af5d 852 handle_format_arg_attribute, false },
853 { NULL, 0, 0, false, false, false, NULL, false }
f8e93a2e 854};
855
6d5d708e 856/* Return identifier for address space AS. */
34208e18 857
6d5d708e 858const char *
859c_addr_space_name (addr_space_t as)
860{
34208e18 861 int rid = RID_FIRST_ADDR_SPACE + as;
862 gcc_assert (ridpointers [rid]);
863 return IDENTIFIER_POINTER (ridpointers [rid]);
6d5d708e 864}
865
2c0e001b 866/* Push current bindings for the function name VAR_DECLS. */
f4e3c278 867
868void
1cae46be 869start_fname_decls (void)
f4e3c278 870{
65b7f83f 871 unsigned ix;
872 tree saved = NULL_TREE;
1cae46be 873
65b7f83f 874 for (ix = 0; fname_vars[ix].decl; ix++)
875 {
876 tree decl = *fname_vars[ix].decl;
f4e3c278 877
65b7f83f 878 if (decl)
879 {
ceb7b692 880 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
881 saved);
65b7f83f 882 *fname_vars[ix].decl = NULL_TREE;
883 }
884 }
885 if (saved || saved_function_name_decls)
886 /* Normally they'll have been NULL, so only push if we've got a
887 stack, or they are non-NULL. */
888 saved_function_name_decls = tree_cons (saved, NULL_TREE,
889 saved_function_name_decls);
890}
891
2363ef00 892/* Finish up the current bindings, adding them into the current function's
893 statement tree. This must be done _before_ finish_stmt_tree is called.
894 If there is no current function, we must be at file scope and no statements
895 are involved. Pop the previous bindings. */
65b7f83f 896
897void
1cae46be 898finish_fname_decls (void)
65b7f83f 899{
900 unsigned ix;
2363ef00 901 tree stmts = NULL_TREE;
65b7f83f 902 tree stack = saved_function_name_decls;
903
904 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
2363ef00 905 append_to_statement_list (TREE_VALUE (stack), &stmts);
1cae46be 906
2363ef00 907 if (stmts)
65b7f83f 908 {
2363ef00 909 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
5c423bd6 910
2363ef00 911 if (TREE_CODE (*bodyp) == BIND_EXPR)
912 bodyp = &BIND_EXPR_BODY (*bodyp);
81010c97 913
bc2b76e0 914 append_to_statement_list_force (*bodyp, &stmts);
2363ef00 915 *bodyp = stmts;
65b7f83f 916 }
1cae46be 917
65b7f83f 918 for (ix = 0; fname_vars[ix].decl; ix++)
919 *fname_vars[ix].decl = NULL_TREE;
1cae46be 920
65b7f83f 921 if (stack)
f4e3c278 922 {
2c0e001b 923 /* We had saved values, restore them. */
65b7f83f 924 tree saved;
925
926 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
927 {
928 tree decl = TREE_PURPOSE (saved);
f9ae6f95 929 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
1cae46be 930
65b7f83f 931 *fname_vars[ix].decl = decl;
932 }
933 stack = TREE_CHAIN (stack);
f4e3c278 934 }
65b7f83f 935 saved_function_name_decls = stack;
936}
937
81010c97 938/* Return the text name of the current function, suitably prettified
5fc7fa69 939 by PRETTY_P. Return string must be freed by caller. */
65b7f83f 940
941const char *
1cae46be 942fname_as_string (int pretty_p)
65b7f83f 943{
9ad4bb1e 944 const char *name = "top level";
5fc7fa69 945 char *namep;
8115b8be 946 int vrb = 2, len;
947 cpp_string cstr = { 0, 0 }, strname;
9ad4bb1e 948
84166705 949 if (!pretty_p)
9ad4bb1e 950 {
951 name = "";
952 vrb = 0;
953 }
954
955 if (current_function_decl)
dc24ddbd 956 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
9ad4bb1e 957
8115b8be 958 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
5fc7fa69 959
8115b8be 960 namep = XNEWVEC (char, len);
961 snprintf (namep, len, "\"%s\"", name);
962 strname.text = (unsigned char *) namep;
963 strname.len = len - 1;
5fc7fa69 964
924bbf02 965 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
8115b8be 966 {
967 XDELETEVEC (namep);
968 return (const char *) cstr.text;
5fc7fa69 969 }
5fc7fa69 970
971 return namep;
65b7f83f 972}
973
65b7f83f 974/* Return the VAR_DECL for a const char array naming the current
975 function. If the VAR_DECL has not yet been created, create it
976 now. RID indicates how it should be formatted and IDENTIFIER_NODE
977 ID is its name (unfortunately C and C++ hold the RID values of
978 keywords in different places, so we can't derive RID from ID in
e3b80d49 979 this language independent code. LOC is the location of the
980 function. */
65b7f83f 981
982tree
e3b80d49 983fname_decl (location_t loc, unsigned int rid, tree id)
65b7f83f 984{
985 unsigned ix;
986 tree decl = NULL_TREE;
987
988 for (ix = 0; fname_vars[ix].decl; ix++)
989 if (fname_vars[ix].rid == rid)
990 break;
991
992 decl = *fname_vars[ix].decl;
993 if (!decl)
f4e3c278 994 {
2222b3c6 995 /* If a tree is built here, it would normally have the lineno of
996 the current statement. Later this tree will be moved to the
997 beginning of the function and this line number will be wrong.
998 To avoid this problem set the lineno to 0 here; that prevents
7299020b 999 it from appearing in the RTL. */
2363ef00 1000 tree stmts;
9a6486a6 1001 location_t saved_location = input_location;
9a6486a6 1002 input_location = UNKNOWN_LOCATION;
1cae46be 1003
2363ef00 1004 stmts = push_stmt_list ();
e60a6f7b 1005 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
2363ef00 1006 stmts = pop_stmt_list (stmts);
1007 if (!IS_EMPTY_STMT (stmts))
1008 saved_function_name_decls
1009 = tree_cons (decl, stmts, saved_function_name_decls);
65b7f83f 1010 *fname_vars[ix].decl = decl;
9a6486a6 1011 input_location = saved_location;
f4e3c278 1012 }
65b7f83f 1013 if (!ix && !current_function_decl)
e3b80d49 1014 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
81010c97 1015
65b7f83f 1016 return decl;
f4e3c278 1017}
1018
070236f0 1019/* Given a STRING_CST, give it a suitable array-of-chars data type. */
b0fc3e72 1020
1021tree
1cae46be 1022fix_string_type (tree value)
b0fc3e72 1023{
070236f0 1024 int length = TREE_STRING_LENGTH (value);
1025 int nchars;
00d26680 1026 tree e_type, i_type, a_type;
1027
73be5127 1028 /* Compute the number of elements, for the array type. */
924bbf02 1029 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1030 {
1031 nchars = length;
1032 e_type = char_type_node;
1033 }
1034 else if (TREE_TYPE (value) == char16_array_type_node)
1035 {
1036 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1037 e_type = char16_type_node;
1038 }
1039 else if (TREE_TYPE (value) == char32_array_type_node)
1040 {
1041 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1042 e_type = char32_type_node;
1043 }
1044 else
1045 {
1046 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1047 e_type = wchar_type_node;
1048 }
b0fc3e72 1049
1d752508 1050 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1051 limit in C++98 Annex B is very large (65536) and is not normative,
1052 so we do not diagnose it (warn_overlength_strings is forced off
1053 in c_common_post_options). */
1054 if (warn_overlength_strings)
1055 {
1056 const int nchars_max = flag_isoc99 ? 4095 : 509;
1057 const int relevant_std = flag_isoc99 ? 99 : 90;
1058 if (nchars - 1 > nchars_max)
1059 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1060 separate the %d from the 'C'. 'ISO' should not be
1061 translated, but it may be moved after 'C%d' in languages
1062 where modifiers follow nouns. */
21ca8540 1063 pedwarn (input_location, OPT_Woverlength_strings,
8864917d 1064 "string length %qd is greater than the length %qd "
1d752508 1065 "ISO C%d compilers are required to support",
1066 nchars - 1, nchars_max, relevant_std);
1067 }
82cfc7f7 1068
390be14e 1069 /* Create the array type for the string constant. The ISO C++
1070 standard says that a string literal has type `const char[N]' or
1071 `const wchar_t[N]'. We use the same logic when invoked as a C
1072 front-end with -Wwrite-strings.
1073 ??? We should change the type of an expression depending on the
1074 state of a warning flag. We should just be warning -- see how
1075 this is handled in the C++ front-end for the deprecated implicit
1076 conversion from string literals to `char*' or `wchar_t*'.
00d26680 1077
1078 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1079 array type being the unqualified version of that type.
1080 Therefore, if we are constructing an array of const char, we must
1081 construct the matching unqualified array type first. The C front
1082 end does not require this, but it does no harm, so we do it
1083 unconditionally. */
ceb7b692 1084 i_type = build_index_type (size_int (nchars - 1));
00d26680 1085 a_type = build_array_type (e_type, i_type);
390be14e 1086 if (c_dialect_cxx() || warn_write_strings)
aebc8537 1087 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
3a10ba35 1088
00d26680 1089 TREE_TYPE (value) = a_type;
b8e3b7ad 1090 TREE_CONSTANT (value) = 1;
a814bad5 1091 TREE_READONLY (value) = 1;
b0fc3e72 1092 TREE_STATIC (value) = 1;
1093 return value;
1094}
1095\f
fc501191 1096/* If DISABLE is true, stop issuing warnings. This is used when
1097 parsing code that we know will not be executed. This function may
1098 be called multiple times, and works as a stack. */
1099
1100static void
1101c_disable_warnings (bool disable)
1102{
1103 if (disable)
1104 {
1105 ++c_inhibit_evaluation_warnings;
1106 fold_defer_overflow_warnings ();
1107 }
1108}
1109
1110/* If ENABLE is true, reenable issuing warnings. */
1111
1112static void
1113c_enable_warnings (bool enable)
1114{
1115 if (enable)
1116 {
1117 --c_inhibit_evaluation_warnings;
1118 fold_undefer_and_ignore_overflow_warnings ();
1119 }
1120}
1121
a75b1c71 1122/* Fully fold EXPR, an expression that was not folded (beyond integer
1123 constant expressions and null pointer constants) when being built
1124 up. If IN_INIT, this is in a static initializer and certain
1125 changes are made to the folding done. Clear *MAYBE_CONST if
1126 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1127 expression because it contains an evaluated operator (in C99) or an
1128 operator outside of sizeof returning an integer constant (in C90)
1129 not permitted in constant expressions, or because it contains an
1130 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1131 set to true by callers before calling this function.) Return the
1132 folded expression. Function arguments have already been folded
1133 before calling this function, as have the contents of SAVE_EXPR,
1134 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1135 C_MAYBE_CONST_EXPR. */
1136
1137tree
1138c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1139{
1140 tree ret;
c6418a4e 1141 tree eptype = NULL_TREE;
a75b1c71 1142 bool dummy = true;
1143 bool maybe_const_itself = true;
389dd41b 1144 location_t loc = EXPR_LOCATION (expr);
a75b1c71 1145
1146 /* This function is not relevant to C++ because C++ folds while
1147 parsing, and may need changes to be correct for C++ when C++
1148 stops folding while parsing. */
1149 if (c_dialect_cxx ())
1150 gcc_unreachable ();
1151
1152 if (!maybe_const)
1153 maybe_const = &dummy;
c6418a4e 1154 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1155 {
1156 eptype = TREE_TYPE (expr);
1157 expr = TREE_OPERAND (expr, 0);
1158 }
a75b1c71 1159 ret = c_fully_fold_internal (expr, in_init, maybe_const,
aac24642 1160 &maybe_const_itself, false);
c6418a4e 1161 if (eptype)
389dd41b 1162 ret = fold_convert_loc (loc, eptype, ret);
a75b1c71 1163 *maybe_const &= maybe_const_itself;
1164 return ret;
1165}
1166
1167/* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1168 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1169 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1170 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1171 both evaluated and unevaluated subexpressions while
1172 *MAYBE_CONST_ITSELF is carried from only evaluated
aac24642 1173 subexpressions). FOR_INT_CONST indicates if EXPR is an expression
1174 with integer constant operands, and if any of the operands doesn't
1175 get folded to an integer constant, don't fold the expression itself. */
a75b1c71 1176
1177static tree
1178c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
aac24642 1179 bool *maybe_const_itself, bool for_int_const)
a75b1c71 1180{
1181 tree ret = expr;
1182 enum tree_code code = TREE_CODE (expr);
1183 enum tree_code_class kind = TREE_CODE_CLASS (code);
1184 location_t loc = EXPR_LOCATION (expr);
1185 tree op0, op1, op2, op3;
1186 tree orig_op0, orig_op1, orig_op2;
1187 bool op0_const = true, op1_const = true, op2_const = true;
1188 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1189 bool nowarning = TREE_NO_WARNING (expr);
fc501191 1190 bool unused_p;
a75b1c71 1191
1192 /* This function is not relevant to C++ because C++ folds while
1193 parsing, and may need changes to be correct for C++ when C++
1194 stops folding while parsing. */
1195 if (c_dialect_cxx ())
1196 gcc_unreachable ();
1197
1198 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1199 anything else not counted as an expression cannot usefully be
1200 folded further at this point. */
1201 if (!IS_EXPR_CODE_CLASS (kind)
1202 || kind == tcc_statement
1203 || code == SAVE_EXPR)
1204 return expr;
1205
1206 /* Operands of variable-length expressions (function calls) have
1207 already been folded, as have __builtin_* function calls, and such
1208 expressions cannot occur in constant expressions. */
1209 if (kind == tcc_vl_exp)
1210 {
1211 *maybe_const_operands = false;
1212 ret = fold (expr);
1213 goto out;
1214 }
1215
1216 if (code == C_MAYBE_CONST_EXPR)
1217 {
1218 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1219 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1220 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1221 *maybe_const_operands = false;
1222 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
aac24642 1223 {
1224 *maybe_const_itself = false;
1225 inner = c_fully_fold_internal (inner, in_init, maybe_const_operands,
1226 maybe_const_itself, true);
1227 }
a75b1c71 1228 if (pre && !in_init)
1229 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1230 else
1231 ret = inner;
1232 goto out;
1233 }
1234
1235 /* Assignment, increment, decrement, function call and comma
1236 operators, and statement expressions, cannot occur in constant
1237 expressions if evaluated / outside of sizeof. (Function calls
1238 were handled above, though VA_ARG_EXPR is treated like a function
1239 call here, and statement expressions are handled through
1240 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1241 switch (code)
1242 {
1243 case MODIFY_EXPR:
1244 case PREDECREMENT_EXPR:
1245 case PREINCREMENT_EXPR:
1246 case POSTDECREMENT_EXPR:
1247 case POSTINCREMENT_EXPR:
1248 case COMPOUND_EXPR:
1249 *maybe_const_operands = false;
1250 break;
1251
1252 case VA_ARG_EXPR:
1253 case TARGET_EXPR:
1254 case BIND_EXPR:
1255 case OBJ_TYPE_REF:
1256 *maybe_const_operands = false;
1257 ret = fold (expr);
1258 goto out;
1259
1260 default:
1261 break;
1262 }
1263
1264 /* Fold individual tree codes as appropriate. */
1265 switch (code)
1266 {
1267 case COMPOUND_LITERAL_EXPR:
1268 /* Any non-constancy will have been marked in a containing
1269 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1270 goto out;
1271
1272 case COMPONENT_REF:
1273 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1274 op1 = TREE_OPERAND (expr, 1);
1275 op2 = TREE_OPERAND (expr, 2);
1276 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1277 maybe_const_itself, for_int_const);
f59e3889 1278 STRIP_TYPE_NOPS (op0);
a75b1c71 1279 if (op0 != orig_op0)
1280 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1281 if (ret != expr)
1282 {
1283 TREE_READONLY (ret) = TREE_READONLY (expr);
1284 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1285 }
1286 goto out;
1287
1288 case ARRAY_REF:
1289 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1290 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1291 op2 = TREE_OPERAND (expr, 2);
1292 op3 = TREE_OPERAND (expr, 3);
1293 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1294 maybe_const_itself, for_int_const);
f59e3889 1295 STRIP_TYPE_NOPS (op0);
a75b1c71 1296 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
aac24642 1297 maybe_const_itself, for_int_const);
f59e3889 1298 STRIP_TYPE_NOPS (op1);
a75b1c71 1299 op1 = decl_constant_value_for_optimization (op1);
1300 if (op0 != orig_op0 || op1 != orig_op1)
1301 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1302 if (ret != expr)
1303 {
1304 TREE_READONLY (ret) = TREE_READONLY (expr);
1305 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1306 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1307 }
1308 ret = fold (ret);
1309 goto out;
1310
1311 case COMPOUND_EXPR:
1312 case MODIFY_EXPR:
1313 case PREDECREMENT_EXPR:
1314 case PREINCREMENT_EXPR:
1315 case POSTDECREMENT_EXPR:
1316 case POSTINCREMENT_EXPR:
1317 case PLUS_EXPR:
1318 case MINUS_EXPR:
1319 case MULT_EXPR:
1320 case POINTER_PLUS_EXPR:
1321 case TRUNC_DIV_EXPR:
1322 case CEIL_DIV_EXPR:
1323 case FLOOR_DIV_EXPR:
1324 case TRUNC_MOD_EXPR:
1325 case RDIV_EXPR:
1326 case EXACT_DIV_EXPR:
1327 case LSHIFT_EXPR:
1328 case RSHIFT_EXPR:
1329 case BIT_IOR_EXPR:
1330 case BIT_XOR_EXPR:
1331 case BIT_AND_EXPR:
1332 case LT_EXPR:
1333 case LE_EXPR:
1334 case GT_EXPR:
1335 case GE_EXPR:
1336 case EQ_EXPR:
1337 case NE_EXPR:
1338 case COMPLEX_EXPR:
1339 case TRUTH_AND_EXPR:
1340 case TRUTH_OR_EXPR:
1341 case TRUTH_XOR_EXPR:
1342 case UNORDERED_EXPR:
1343 case ORDERED_EXPR:
1344 case UNLT_EXPR:
1345 case UNLE_EXPR:
1346 case UNGT_EXPR:
1347 case UNGE_EXPR:
1348 case UNEQ_EXPR:
1349 /* Binary operations evaluating both arguments (increment and
1350 decrement are binary internally in GCC). */
1351 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1352 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1353 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1354 maybe_const_itself, for_int_const);
f59e3889 1355 STRIP_TYPE_NOPS (op0);
a75b1c71 1356 if (code != MODIFY_EXPR
1357 && code != PREDECREMENT_EXPR
1358 && code != PREINCREMENT_EXPR
1359 && code != POSTDECREMENT_EXPR
1360 && code != POSTINCREMENT_EXPR)
1361 op0 = decl_constant_value_for_optimization (op0);
1362 /* The RHS of a MODIFY_EXPR was fully folded when building that
1363 expression for the sake of conversion warnings. */
1364 if (code != MODIFY_EXPR)
1365 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
aac24642 1366 maybe_const_itself, for_int_const);
f59e3889 1367 STRIP_TYPE_NOPS (op1);
a75b1c71 1368 op1 = decl_constant_value_for_optimization (op1);
aac24642 1369
1370 if (for_int_const && (TREE_CODE (op0) != INTEGER_CST
1371 || TREE_CODE (op1) != INTEGER_CST))
1372 goto out;
1373
a75b1c71 1374 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1375 ret = in_init
389dd41b 1376 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1377 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
a75b1c71 1378 else
1379 ret = fold (expr);
672d914b 1380 if (TREE_OVERFLOW_P (ret)
1381 && !TREE_OVERFLOW_P (op0)
1382 && !TREE_OVERFLOW_P (op1))
8d669e79 1383 overflow_warning (EXPR_LOC_OR_LOC (expr, input_location), ret);
cdc64059 1384 if (code == LSHIFT_EXPR
1385 && TREE_CODE (orig_op0) != INTEGER_CST
1386 && TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1387 && TREE_CODE (op0) == INTEGER_CST
1388 && c_inhibit_evaluation_warnings == 0
1389 && tree_int_cst_sgn (op0) < 0)
1390 warning_at (loc, OPT_Wshift_negative_value,
1391 "left shift of negative value");
7c834436 1392 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1393 && TREE_CODE (orig_op1) != INTEGER_CST
1394 && TREE_CODE (op1) == INTEGER_CST
1395 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1396 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1397 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1398 && c_inhibit_evaluation_warnings == 0)
1399 {
1400 if (tree_int_cst_sgn (op1) < 0)
f11bdffb 1401 warning_at (loc, OPT_Wshift_count_negative,
1402 (code == LSHIFT_EXPR
1403 ? G_("left shift count is negative")
1404 : G_("right shift count is negative")));
7c834436 1405 else if (compare_tree_int (op1,
1406 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1407 >= 0)
f11bdffb 1408 warning_at (loc, OPT_Wshift_count_overflow,
1409 (code == LSHIFT_EXPR
1410 ? G_("left shift count >= width of type")
1411 : G_("right shift count >= width of type")));
7c834436 1412 }
8d669e79 1413 if (code == LSHIFT_EXPR
1414 /* If either OP0 has been folded to INTEGER_CST... */
1415 && ((TREE_CODE (orig_op0) != INTEGER_CST
1416 && TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1417 && TREE_CODE (op0) == INTEGER_CST)
1418 /* ...or if OP1 has been folded to INTEGER_CST... */
1419 || (TREE_CODE (orig_op1) != INTEGER_CST
1420 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1421 && TREE_CODE (op1) == INTEGER_CST))
1422 && c_inhibit_evaluation_warnings == 0)
1423 /* ...then maybe we can detect an overflow. */
1424 maybe_warn_shift_overflow (loc, op0, op1);
b57910fa 1425 if ((code == TRUNC_DIV_EXPR
1426 || code == CEIL_DIV_EXPR
1427 || code == FLOOR_DIV_EXPR
1428 || code == EXACT_DIV_EXPR
1429 || code == TRUNC_MOD_EXPR)
1430 && TREE_CODE (orig_op1) != INTEGER_CST
1431 && TREE_CODE (op1) == INTEGER_CST
1432 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1433 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1434 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE)
1435 warn_for_div_by_zero (loc, op1);
a75b1c71 1436 goto out;
1437
1438 case INDIRECT_REF:
1439 case FIX_TRUNC_EXPR:
1440 case FLOAT_EXPR:
1441 CASE_CONVERT:
e66325ea 1442 case ADDR_SPACE_CONVERT_EXPR:
2b19dfe4 1443 case VIEW_CONVERT_EXPR:
a75b1c71 1444 case NON_LVALUE_EXPR:
1445 case NEGATE_EXPR:
1446 case BIT_NOT_EXPR:
1447 case TRUTH_NOT_EXPR:
1448 case ADDR_EXPR:
1449 case CONJ_EXPR:
1450 case REALPART_EXPR:
1451 case IMAGPART_EXPR:
1452 /* Unary operations. */
1453 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1454 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1455 maybe_const_itself, for_int_const);
f59e3889 1456 STRIP_TYPE_NOPS (op0);
a75b1c71 1457 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1458 op0 = decl_constant_value_for_optimization (op0);
aac24642 1459
1460 if (for_int_const && TREE_CODE (op0) != INTEGER_CST)
1461 goto out;
1462
737a23cc 1463 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1464 not prepared to deal with them if they occur in initializers. */
1465 if (op0 != orig_op0
1466 && code == ADDR_EXPR
1467 && (op1 = get_base_address (op0)) != NULL_TREE
aa3e402e 1468 && INDIRECT_REF_P (op1)
737a23cc 1469 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
7549df0d 1470 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
737a23cc 1471 else if (op0 != orig_op0 || in_init)
a75b1c71 1472 ret = in_init
389dd41b 1473 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1474 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
a75b1c71 1475 else
1476 ret = fold (expr);
1477 if (code == INDIRECT_REF
1478 && ret != expr
aa3e402e 1479 && INDIRECT_REF_P (ret))
a75b1c71 1480 {
1481 TREE_READONLY (ret) = TREE_READONLY (expr);
1482 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1483 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1484 }
672d914b 1485 switch (code)
1486 {
1487 case FIX_TRUNC_EXPR:
1488 case FLOAT_EXPR:
1489 CASE_CONVERT:
1490 /* Don't warn about explicit conversions. We will already
1491 have warned about suspect implicit conversions. */
1492 break;
1493
1494 default:
1495 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1496 overflow_warning (EXPR_LOCATION (expr), ret);
1497 break;
1498 }
a75b1c71 1499 goto out;
1500
1501 case TRUTH_ANDIF_EXPR:
1502 case TRUTH_ORIF_EXPR:
1503 /* Binary operations not necessarily evaluating both
1504 arguments. */
1505 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1506 orig_op1 = op1 = TREE_OPERAND (expr, 1);
aac24642 1507 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1508 for_int_const);
f59e3889 1509 STRIP_TYPE_NOPS (op0);
672d914b 1510
1511 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1512 ? truthvalue_false_node
1513 : truthvalue_true_node));
fc501191 1514 c_disable_warnings (unused_p);
aac24642 1515 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1516 for_int_const);
f59e3889 1517 STRIP_TYPE_NOPS (op1);
fc501191 1518 c_enable_warnings (unused_p);
672d914b 1519
aac24642 1520 if (for_int_const
1521 && (TREE_CODE (op0) != INTEGER_CST
1522 /* Require OP1 be an INTEGER_CST only if it's evaluated. */
1523 || (!unused_p && TREE_CODE (op1) != INTEGER_CST)))
1524 goto out;
1525
a75b1c71 1526 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1527 ret = in_init
389dd41b 1528 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1529 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
a75b1c71 1530 else
1531 ret = fold (expr);
1532 *maybe_const_operands &= op0_const;
1533 *maybe_const_itself &= op0_const_self;
1534 if (!(flag_isoc99
1535 && op0_const
1536 && op0_const_self
1537 && (code == TRUTH_ANDIF_EXPR
1538 ? op0 == truthvalue_false_node
1539 : op0 == truthvalue_true_node)))
1540 *maybe_const_operands &= op1_const;
1541 if (!(op0_const
1542 && op0_const_self
1543 && (code == TRUTH_ANDIF_EXPR
1544 ? op0 == truthvalue_false_node
1545 : op0 == truthvalue_true_node)))
1546 *maybe_const_itself &= op1_const_self;
1547 goto out;
1548
1549 case COND_EXPR:
1550 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1551 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1552 orig_op2 = op2 = TREE_OPERAND (expr, 2);
aac24642 1553 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1554 for_int_const);
672d914b 1555
f59e3889 1556 STRIP_TYPE_NOPS (op0);
fc501191 1557 c_disable_warnings (op0 == truthvalue_false_node);
aac24642 1558 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1559 for_int_const);
f59e3889 1560 STRIP_TYPE_NOPS (op1);
fc501191 1561 c_enable_warnings (op0 == truthvalue_false_node);
672d914b 1562
fc501191 1563 c_disable_warnings (op0 == truthvalue_true_node);
aac24642 1564 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self,
1565 for_int_const);
f59e3889 1566 STRIP_TYPE_NOPS (op2);
fc501191 1567 c_enable_warnings (op0 == truthvalue_true_node);
672d914b 1568
aac24642 1569 if (for_int_const
1570 && (TREE_CODE (op0) != INTEGER_CST
1571 /* Only the evaluated operand must be an INTEGER_CST. */
1572 || (op0 == truthvalue_true_node
1573 ? TREE_CODE (op1) != INTEGER_CST
1574 : TREE_CODE (op2) != INTEGER_CST)))
1575 goto out;
1576
a75b1c71 1577 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
389dd41b 1578 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
a75b1c71 1579 else
1580 ret = fold (expr);
1581 *maybe_const_operands &= op0_const;
1582 *maybe_const_itself &= op0_const_self;
1583 if (!(flag_isoc99
1584 && op0_const
1585 && op0_const_self
1586 && op0 == truthvalue_false_node))
1587 *maybe_const_operands &= op1_const;
1588 if (!(op0_const
1589 && op0_const_self
1590 && op0 == truthvalue_false_node))
1591 *maybe_const_itself &= op1_const_self;
1592 if (!(flag_isoc99
1593 && op0_const
1594 && op0_const_self
1595 && op0 == truthvalue_true_node))
1596 *maybe_const_operands &= op2_const;
1597 if (!(op0_const
1598 && op0_const_self
1599 && op0 == truthvalue_true_node))
1600 *maybe_const_itself &= op2_const_self;
1601 goto out;
1602
c6418a4e 1603 case EXCESS_PRECISION_EXPR:
1604 /* Each case where an operand with excess precision may be
1605 encountered must remove the EXCESS_PRECISION_EXPR around
1606 inner operands and possibly put one around the whole
1607 expression or possibly convert to the semantic type (which
1608 c_fully_fold does); we cannot tell at this stage which is
1609 appropriate in any particular case. */
1610 gcc_unreachable ();
1611
a75b1c71 1612 default:
1613 /* Various codes may appear through folding built-in functions
1614 and their arguments. */
1615 goto out;
1616 }
1617
1618 out:
1619 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1620 have been done by this point, so remove them again. */
1621 nowarning |= TREE_NO_WARNING (ret);
1622 STRIP_TYPE_NOPS (ret);
1623 if (nowarning && !TREE_NO_WARNING (ret))
1624 {
1625 if (!CAN_HAVE_LOCATION_P (ret))
1626 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1627 TREE_NO_WARNING (ret) = 1;
1628 }
1629 if (ret != expr)
1630 protected_set_expr_location (ret, loc);
1631 return ret;
1632}
1633
1634/* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1635 return EXP. Otherwise, return either EXP or its known constant
1636 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1637 Is the BLKmode test appropriate? */
1638
1639tree
1640decl_constant_value_for_optimization (tree exp)
1641{
1642 tree ret;
1643
1644 /* This function is only used by C, for c_fully_fold and other
1645 optimization, and may not be correct for C++. */
1646 if (c_dialect_cxx ())
1647 gcc_unreachable ();
1648
1649 if (!optimize
f48c7f4a 1650 || !VAR_P (exp)
a75b1c71 1651 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1652 || DECL_MODE (exp) == BLKmode)
1653 return exp;
1654
1655 ret = decl_constant_value (exp);
1656 /* Avoid unwanted tree sharing between the initializer and current
1657 function's body where the tree can be modified e.g. by the
1658 gimplifier. */
1659 if (ret != exp && TREE_STATIC (exp))
1660 ret = unshare_expr (ret);
1661 return ret;
1662}
1663
2a1736ed 1664/* Print a warning if a constant expression had overflow in folding.
1665 Invoke this function on every expression that the language
1666 requires to be a constant expression.
1667 Note the ANSI C standard says it is erroneous for a
1668 constant expression to overflow. */
b2806639 1669
1670void
1cae46be 1671constant_expression_warning (tree value)
07317e69 1672{
48e1416a 1673 if (warn_overflow && pedantic
07317e69 1674 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1675 || TREE_CODE (value) == FIXED_CST
1676 || TREE_CODE (value) == VECTOR_CST
1677 || TREE_CODE (value) == COMPLEX_CST)
1678 && TREE_OVERFLOW (value))
21ca8540 1679 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
07317e69 1680}
1681
1682/* The same as above but print an unconditional error. */
1683void
1684constant_expression_error (tree value)
b2806639 1685{
837e1122 1686 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
9421ebb9 1687 || TREE_CODE (value) == FIXED_CST
886cfd4f 1688 || TREE_CODE (value) == VECTOR_CST
837e1122 1689 || TREE_CODE (value) == COMPLEX_CST)
07317e69 1690 && TREE_OVERFLOW (value))
1691 error ("overflow in constant expression");
2a1736ed 1692}
1693
f170d67f 1694/* Print a warning if an expression had overflow in folding and its
1695 operands hadn't.
1696
2a1736ed 1697 Invoke this function on every expression that
1698 (1) appears in the source code, and
f170d67f 1699 (2) is a constant expression that overflowed, and
2a1736ed 1700 (3) is not already checked by convert_and_check;
f170d67f 1701 however, do not invoke this function on operands of explicit casts
1702 or when the expression is the result of an operator and any operand
1703 already overflowed. */
2a1736ed 1704
1705void
e60a6f7b 1706overflow_warning (location_t loc, tree value)
2a1736ed 1707{
48d94ede 1708 if (c_inhibit_evaluation_warnings != 0)
1709 return;
f170d67f 1710
1711 switch (TREE_CODE (value))
886cfd4f 1712 {
f170d67f 1713 case INTEGER_CST:
e60a6f7b 1714 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
f170d67f 1715 break;
48e1416a 1716
f170d67f 1717 case REAL_CST:
e60a6f7b 1718 warning_at (loc, OPT_Woverflow,
1719 "floating point overflow in expression");
f170d67f 1720 break;
48e1416a 1721
9421ebb9 1722 case FIXED_CST:
e60a6f7b 1723 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
9421ebb9 1724 break;
1725
f170d67f 1726 case VECTOR_CST:
e60a6f7b 1727 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
f170d67f 1728 break;
48e1416a 1729
f170d67f 1730 case COMPLEX_CST:
1731 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
e60a6f7b 1732 warning_at (loc, OPT_Woverflow,
1733 "complex integer overflow in expression");
f170d67f 1734 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
e60a6f7b 1735 warning_at (loc, OPT_Woverflow,
1736 "complex floating point overflow in expression");
f170d67f 1737 break;
1738
1739 default:
1740 break;
886cfd4f 1741 }
2a1736ed 1742}
1743
03033af4 1744/* Warn about uses of logical || / && operator in a context where it
1745 is likely that the bitwise equivalent was intended by the
1746 programmer. We have seen an expression in which CODE is a binary
9c20c4fc 1747 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1748 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
b13d1547 1749void
9c20c4fc 1750warn_logical_operator (location_t location, enum tree_code code, tree type,
48e1416a 1751 enum tree_code code_left, tree op_left,
03033af4 1752 enum tree_code ARG_UNUSED (code_right), tree op_right)
b13d1547 1753{
9c20c4fc 1754 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1755 int in0_p, in1_p, in_p;
1756 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1757 bool strict_overflow_p = false;
1758
03033af4 1759 if (code != TRUTH_ANDIF_EXPR
1760 && code != TRUTH_AND_EXPR
1761 && code != TRUTH_ORIF_EXPR
1762 && code != TRUTH_OR_EXPR)
1763 return;
1764
439606a9 1765 /* We don't want to warn if either operand comes from a macro
1766 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1767 see PR61534. */
1768 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1769 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1770 return;
1771
03033af4 1772 /* Warn if &&/|| are being used in a context where it is
1773 likely that the bitwise equivalent was intended by the
1774 programmer. That is, an expression such as op && MASK
1775 where op should not be any boolean expression, nor a
1776 constant, and mask seems to be a non-boolean integer constant. */
1777 if (!truth_value_p (code_left)
1778 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1779 && !CONSTANT_CLASS_P (op_left)
1780 && !TREE_NO_WARNING (op_left)
1781 && TREE_CODE (op_right) == INTEGER_CST
1782 && !integer_zerop (op_right)
1783 && !integer_onep (op_right))
b13d1547 1784 {
9c20c4fc 1785 if (or_op)
03033af4 1786 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1787 " applied to non-boolean constant");
1788 else
1789 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1790 " applied to non-boolean constant");
1791 TREE_NO_WARNING (op_left) = true;
9c20c4fc 1792 return;
1793 }
1794
1795 /* We do not warn for constants because they are typical of macro
1796 expansions that test for features. */
1797 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1798 return;
1799
1800 /* This warning only makes sense with logical operands. */
1801 if (!(truth_value_p (TREE_CODE (op_left))
1802 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1803 || !(truth_value_p (TREE_CODE (op_right))
1804 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1805 return;
1806
5e84569c 1807 /* The range computations only work with scalars. */
1808 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1809 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1810 return;
9c20c4fc 1811
686369e8 1812 /* We first test whether either side separately is trivially true
1813 (with OR) or trivially false (with AND). If so, do not warn.
1814 This is a common idiom for testing ranges of data types in
1815 portable code. */
1816 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1817 if (!lhs)
1818 return;
1819 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
9c20c4fc 1820 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1821
686369e8 1822 /* If this is an OR operation, invert both sides; now, the result
1823 should be always false to get a warning. */
1824 if (or_op)
1825 in0_p = !in0_p;
1826
1827 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
d42e7c5a 1828 if (tem && integer_zerop (tem))
686369e8 1829 return;
1830
1831 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1832 if (!rhs)
1833 return;
1834 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
9c20c4fc 1835 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
48e1416a 1836
686369e8 1837 /* If this is an OR operation, invert both sides; now, the result
1838 should be always false to get a warning. */
9c20c4fc 1839 if (or_op)
686369e8 1840 in1_p = !in1_p;
48e1416a 1841
686369e8 1842 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
d42e7c5a 1843 if (tem && integer_zerop (tem))
686369e8 1844 return;
1845
1846 /* If both expressions have the same operand, if we can merge the
485f6b9c 1847 ranges, ... */
686369e8 1848 if (operand_equal_p (lhs, rhs, 0)
9c20c4fc 1849 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
485f6b9c 1850 in1_p, low1, high1))
9c20c4fc 1851 {
485f6b9c 1852 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1853 /* ... and if the range test is always false, then warn. */
1854 if (tem && integer_zerop (tem))
1855 {
1856 if (or_op)
1857 warning_at (location, OPT_Wlogical_op,
1858 "logical %<or%> of collectively exhaustive tests is "
1859 "always true");
1860 else
1861 warning_at (location, OPT_Wlogical_op,
1862 "logical %<and%> of mutually exclusive tests is "
1863 "always false");
1864 }
1865 /* Or warn if the operands have exactly the same range, e.g.
1866 A > 0 && A > 0. */
0200602e 1867 else if (tree_int_cst_equal (low0, low1)
1868 && tree_int_cst_equal (high0, high1))
485f6b9c 1869 {
1870 if (or_op)
1871 warning_at (location, OPT_Wlogical_op,
1872 "logical %<or%> of equal expressions");
1873 else
1874 warning_at (location, OPT_Wlogical_op,
1875 "logical %<and%> of equal expressions");
1876 }
b13d1547 1877 }
1878}
1879
6784a472 1880/* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
1881 with constant indices. */
1882
1883static tree
1884find_array_ref_with_const_idx_r (tree *expr_p, int *walk_subtrees, void *data)
1885{
1886 tree expr = *expr_p;
1887
1888 if ((TREE_CODE (expr) == ARRAY_REF
1889 || TREE_CODE (expr) == ARRAY_RANGE_REF)
1890 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
1891 {
1892 *(bool *) data = true;
1893 *walk_subtrees = 0;
1894 }
1895
1896 return NULL_TREE;
1897}
1898
1899/* Warn if a self-comparison always evaluates to true or false. LOC
1900 is the location of the comparison with code CODE, LHS and RHS are
1901 operands of the comparison. */
1902
1903void
1904warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
1905{
1906 if (TREE_CODE_CLASS (code) != tcc_comparison)
1907 return;
1908
d9e4fe27 1909 /* Don't warn for various macro expansions. */
1910 if (from_macro_expansion_at (loc)
1911 || from_macro_expansion_at (EXPR_LOCATION (lhs))
1912 || from_macro_expansion_at (EXPR_LOCATION (rhs)))
1913 return;
1914
6784a472 1915 /* We do not warn for constants because they are typical of macro
1916 expansions that test for features, sizeof, and similar. */
1917 if (CONSTANT_CLASS_P (lhs) || CONSTANT_CLASS_P (rhs))
1918 return;
1919
1920 /* Don't warn for e.g.
1921 HOST_WIDE_INT n;
1922 ...
1923 if (n == (long) n) ...
1924 */
1925 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
1926 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
1927 return;
1928
f58dd84b 1929 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
1930 It could be a NaN, and NaN never compares equal to anything, even
1931 itself. */
1932 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
1933 return;
1934
6784a472 1935 if (operand_equal_p (lhs, rhs, 0))
1936 {
1937 /* Don't warn about array references with constant indices;
1938 these are likely to come from a macro. */
1939 bool found = false;
1940 walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
1941 &found);
1942 if (found)
1943 return;
1944 const bool always_true = (code == EQ_EXPR || code == LE_EXPR
1945 || code == GE_EXPR || code == UNLE_EXPR
1946 || code == UNGE_EXPR || code == UNEQ_EXPR);
1947 if (always_true)
1948 warning_at (loc, OPT_Wtautological_compare,
1949 "self-comparison always evaluates to true");
1950 else
1951 warning_at (loc, OPT_Wtautological_compare,
1952 "self-comparison always evaluates to false");
1953 }
1954}
1955
32dc1512 1956/* Warn about logical not used on the left hand side operand of a comparison.
1957 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
dc6229e8 1958 Do not warn if RHS is of a boolean type. */
32dc1512 1959
1960void
1961warn_logical_not_parentheses (location_t location, enum tree_code code,
dc6229e8 1962 tree rhs)
32dc1512 1963{
dc6229e8 1964 if (TREE_CODE_CLASS (code) != tcc_comparison
1965 || TREE_TYPE (rhs) == NULL_TREE
1966 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
32dc1512 1967 return;
1968
16f958b3 1969 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1970 !(x == 0) or !(y != 0). */
1971 if ((code == EQ_EXPR || code == NE_EXPR)
1972 && integer_zerop (rhs))
1973 return;
1974
32dc1512 1975 warning_at (location, OPT_Wlogical_not_parentheses,
1976 "logical not is only applied to the left hand side of "
1977 "comparison");
1978}
b13d1547 1979
3d177e8c 1980/* Warn if EXP contains any computations whose results are not used.
1981 Return true if a warning is printed; false otherwise. LOCUS is the
1982 (potential) location of the expression. */
1983
1984bool
1985warn_if_unused_value (const_tree exp, location_t locus)
1986{
1987 restart:
1988 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1989 return false;
1990
1991 /* Don't warn about void constructs. This includes casting to void,
1992 void function calls, and statement expressions with a final cast
1993 to void. */
1994 if (VOID_TYPE_P (TREE_TYPE (exp)))
1995 return false;
1996
1997 if (EXPR_HAS_LOCATION (exp))
1998 locus = EXPR_LOCATION (exp);
1999
2000 switch (TREE_CODE (exp))
2001 {
2002 case PREINCREMENT_EXPR:
2003 case POSTINCREMENT_EXPR:
2004 case PREDECREMENT_EXPR:
2005 case POSTDECREMENT_EXPR:
2006 case MODIFY_EXPR:
2007 case INIT_EXPR:
2008 case TARGET_EXPR:
2009 case CALL_EXPR:
2010 case TRY_CATCH_EXPR:
2011 case WITH_CLEANUP_EXPR:
2012 case EXIT_EXPR:
2013 case VA_ARG_EXPR:
2014 return false;
2015
2016 case BIND_EXPR:
2017 /* For a binding, warn if no side effect within it. */
2018 exp = BIND_EXPR_BODY (exp);
2019 goto restart;
2020
2021 case SAVE_EXPR:
2022 case NON_LVALUE_EXPR:
d85dbdb3 2023 case NOP_EXPR:
3d177e8c 2024 exp = TREE_OPERAND (exp, 0);
2025 goto restart;
2026
2027 case TRUTH_ORIF_EXPR:
2028 case TRUTH_ANDIF_EXPR:
2029 /* In && or ||, warn if 2nd operand has no side effect. */
2030 exp = TREE_OPERAND (exp, 1);
2031 goto restart;
2032
2033 case COMPOUND_EXPR:
2034 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
2035 return true;
2036 /* Let people do `(foo (), 0)' without a warning. */
2037 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
2038 return false;
2039 exp = TREE_OPERAND (exp, 1);
2040 goto restart;
2041
2042 case COND_EXPR:
2043 /* If this is an expression with side effects, don't warn; this
2044 case commonly appears in macro expansions. */
2045 if (TREE_SIDE_EFFECTS (exp))
2046 return false;
2047 goto warn;
2048
2049 case INDIRECT_REF:
2050 /* Don't warn about automatic dereferencing of references, since
2051 the user cannot control it. */
2052 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
2053 {
2054 exp = TREE_OPERAND (exp, 0);
2055 goto restart;
2056 }
2057 /* Fall through. */
2058
2059 default:
2060 /* Referencing a volatile value is a side effect, so don't warn. */
2061 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
2062 && TREE_THIS_VOLATILE (exp))
2063 return false;
2064
2065 /* If this is an expression which has no operands, there is no value
2066 to be unused. There are no such language-independent codes,
2067 but front ends may define such. */
2068 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
2069 return false;
2070
2071 warn:
2072 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
2073 }
2074}
2075
2076
bcf22371 2077/* Print a warning about casts that might indicate violation
2078 of strict aliasing rules if -Wstrict-aliasing is used and
1e31ff37 2079 strict aliasing mode is in effect. OTYPE is the original
2080 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
bcf22371 2081
e6fa0ea6 2082bool
1e31ff37 2083strict_aliasing_warning (tree otype, tree type, tree expr)
bcf22371 2084{
45bb3afb 2085 /* Strip pointer conversion chains and get to the correct original type. */
2086 STRIP_NOPS (expr);
2087 otype = TREE_TYPE (expr);
2088
f06537f2 2089 if (!(flag_strict_aliasing
2090 && POINTER_TYPE_P (type)
2091 && POINTER_TYPE_P (otype)
2092 && !VOID_TYPE_P (TREE_TYPE (type)))
2093 /* If the type we are casting to is a ref-all pointer
2094 dereferencing it is always valid. */
2095 || TYPE_REF_CAN_ALIAS_ALL (type))
e6fa0ea6 2096 return false;
2097
2098 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
bcf22371 2099 && (DECL_P (TREE_OPERAND (expr, 0))
e6fa0ea6 2100 || handled_component_p (TREE_OPERAND (expr, 0))))
bcf22371 2101 {
2102 /* Casting the address of an object to non void pointer. Warn
2103 if the cast breaks type based aliasing. */
e6fa0ea6 2104 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
2105 {
2106 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
2107 "might break strict-aliasing rules");
2108 return true;
2109 }
bcf22371 2110 else
2111 {
48e1416a 2112 /* warn_strict_aliasing >= 3. This includes the default (3).
e6fa0ea6 2113 Only warn if the cast is dereferenced immediately. */
32c2fdea 2114 alias_set_type set1 =
e6fa0ea6 2115 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
32c2fdea 2116 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
bcf22371 2117
62d823d0 2118 if (set1 != set2 && set2 != 0
2119 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
e6fa0ea6 2120 {
2121 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2122 "pointer will break strict-aliasing rules");
2123 return true;
2124 }
2125 else if (warn_strict_aliasing == 2
879f881c 2126 && !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 2127 {
2128 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2129 "pointer might break strict-aliasing rules");
2130 return true;
2131 }
bcf22371 2132 }
2133 }
e6fa0ea6 2134 else
2135 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
2136 {
2137 /* At this level, warn for any conversions, even if an address is
2138 not taken in the same statement. This will likely produce many
2139 false positives, but could be useful to pinpoint problems that
2140 are not revealed at higher levels. */
32c2fdea 2141 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
2142 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
2143 if (!COMPLETE_TYPE_P (type)
879f881c 2144 || !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 2145 {
2146 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2147 "pointer might break strict-aliasing rules");
2148 return true;
2149 }
2150 }
2151
2152 return false;
bcf22371 2153}
2154
f003f5dc 2155/* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
2156 sizeof as last operand of certain builtins. */
2157
2158void
57f872a2 2159sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
f1f41a6c 2160 vec<tree, va_gc> *params, tree *sizeof_arg,
f003f5dc 2161 bool (*comp_types) (tree, tree))
2162{
2163 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
57f872a2 2164 bool strop = false, cmp = false;
2165 unsigned int idx = ~0;
2166 location_t loc;
f003f5dc 2167
2168 if (TREE_CODE (callee) != FUNCTION_DECL
2169 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
f1f41a6c 2170 || vec_safe_length (params) <= 1)
f003f5dc 2171 return;
2172
f003f5dc 2173 switch (DECL_FUNCTION_CODE (callee))
2174 {
2175 case BUILT_IN_STRNCMP:
2176 case BUILT_IN_STRNCASECMP:
57f872a2 2177 cmp = true;
2178 /* FALLTHRU */
f003f5dc 2179 case BUILT_IN_STRNCPY:
57f872a2 2180 case BUILT_IN_STRNCPY_CHK:
f003f5dc 2181 case BUILT_IN_STRNCAT:
57f872a2 2182 case BUILT_IN_STRNCAT_CHK:
2183 case BUILT_IN_STPNCPY:
2184 case BUILT_IN_STPNCPY_CHK:
f003f5dc 2185 strop = true;
2186 /* FALLTHRU */
2187 case BUILT_IN_MEMCPY:
57f872a2 2188 case BUILT_IN_MEMCPY_CHK:
f003f5dc 2189 case BUILT_IN_MEMMOVE:
57f872a2 2190 case BUILT_IN_MEMMOVE_CHK:
f1f41a6c 2191 if (params->length () < 3)
57f872a2 2192 return;
f1f41a6c 2193 src = (*params)[1];
2194 dest = (*params)[0];
57f872a2 2195 idx = 2;
2196 break;
2197 case BUILT_IN_BCOPY:
f1f41a6c 2198 if (params->length () < 3)
57f872a2 2199 return;
f1f41a6c 2200 src = (*params)[0];
2201 dest = (*params)[1];
57f872a2 2202 idx = 2;
2203 break;
f003f5dc 2204 case BUILT_IN_MEMCMP:
57f872a2 2205 case BUILT_IN_BCMP:
f1f41a6c 2206 if (params->length () < 3)
f003f5dc 2207 return;
f1f41a6c 2208 src = (*params)[1];
2209 dest = (*params)[0];
57f872a2 2210 idx = 2;
2211 cmp = true;
f003f5dc 2212 break;
2213 case BUILT_IN_MEMSET:
57f872a2 2214 case BUILT_IN_MEMSET_CHK:
f1f41a6c 2215 if (params->length () < 3)
f003f5dc 2216 return;
f1f41a6c 2217 dest = (*params)[0];
57f872a2 2218 idx = 2;
2219 break;
2220 case BUILT_IN_BZERO:
f1f41a6c 2221 dest = (*params)[0];
57f872a2 2222 idx = 1;
f003f5dc 2223 break;
2224 case BUILT_IN_STRNDUP:
f1f41a6c 2225 src = (*params)[0];
f003f5dc 2226 strop = true;
57f872a2 2227 idx = 1;
2228 break;
2229 case BUILT_IN_MEMCHR:
f1f41a6c 2230 if (params->length () < 3)
57f872a2 2231 return;
f1f41a6c 2232 src = (*params)[0];
57f872a2 2233 idx = 2;
2234 break;
2235 case BUILT_IN_SNPRINTF:
2236 case BUILT_IN_SNPRINTF_CHK:
2237 case BUILT_IN_VSNPRINTF:
2238 case BUILT_IN_VSNPRINTF_CHK:
f1f41a6c 2239 dest = (*params)[0];
57f872a2 2240 idx = 1;
2241 strop = true;
f003f5dc 2242 break;
2243 default:
2244 break;
2245 }
2246
57f872a2 2247 if (idx >= 3)
2248 return;
2249
2250 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2251 return;
2252
2253 type = TYPE_P (sizeof_arg[idx])
2254 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2255 if (!POINTER_TYPE_P (type))
2256 return;
2257
f003f5dc 2258 if (dest
2259 && (tem = tree_strip_nop_conversions (dest))
2260 && POINTER_TYPE_P (TREE_TYPE (tem))
2261 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2262 return;
2263
2264 if (src
2265 && (tem = tree_strip_nop_conversions (src))
2266 && POINTER_TYPE_P (TREE_TYPE (tem))
2267 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2268 return;
2269
57f872a2 2270 loc = sizeof_arg_loc[idx];
2271
2272 if (dest && !cmp)
f003f5dc 2273 {
57f872a2 2274 if (!TYPE_P (sizeof_arg[idx])
2275 && operand_equal_p (dest, sizeof_arg[idx], 0)
f003f5dc 2276 && comp_types (TREE_TYPE (dest), type))
2277 {
57f872a2 2278 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
f003f5dc 2279 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2280 "argument to %<sizeof%> in %qD call is the same "
2281 "expression as the destination; did you mean to "
2282 "remove the addressof?", callee);
2283 else if ((TYPE_PRECISION (TREE_TYPE (type))
2284 == TYPE_PRECISION (char_type_node))
2285 || strop)
2286 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2287 "argument to %<sizeof%> in %qD call is the same "
2288 "expression as the destination; did you mean to "
2289 "provide an explicit length?", callee);
2290 else
2291 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2292 "argument to %<sizeof%> in %qD call is the same "
2293 "expression as the destination; did you mean to "
2294 "dereference it?", callee);
2295 return;
2296 }
2297
2298 if (POINTER_TYPE_P (TREE_TYPE (dest))
2299 && !strop
2300 && comp_types (TREE_TYPE (dest), type)
2301 && !VOID_TYPE_P (TREE_TYPE (type)))
2302 {
2303 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2304 "argument to %<sizeof%> in %qD call is the same "
2305 "pointer type %qT as the destination; expected %qT "
2306 "or an explicit length", callee, TREE_TYPE (dest),
2307 TREE_TYPE (TREE_TYPE (dest)));
2308 return;
2309 }
2310 }
2311
57f872a2 2312 if (src && !cmp)
f003f5dc 2313 {
57f872a2 2314 if (!TYPE_P (sizeof_arg[idx])
2315 && operand_equal_p (src, sizeof_arg[idx], 0)
f003f5dc 2316 && comp_types (TREE_TYPE (src), type))
2317 {
57f872a2 2318 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
f003f5dc 2319 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2320 "argument to %<sizeof%> in %qD call is the same "
2321 "expression as the source; did you mean to "
2322 "remove the addressof?", callee);
2323 else if ((TYPE_PRECISION (TREE_TYPE (type))
2324 == TYPE_PRECISION (char_type_node))
2325 || strop)
2326 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2327 "argument to %<sizeof%> in %qD call is the same "
2328 "expression as the source; did you mean to "
2329 "provide an explicit length?", callee);
2330 else
2331 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2332 "argument to %<sizeof%> in %qD call is the same "
2333 "expression as the source; did you mean to "
2334 "dereference it?", callee);
2335 return;
2336 }
2337
2338 if (POINTER_TYPE_P (TREE_TYPE (src))
2339 && !strop
2340 && comp_types (TREE_TYPE (src), type)
2341 && !VOID_TYPE_P (TREE_TYPE (type)))
2342 {
2343 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2344 "argument to %<sizeof%> in %qD call is the same "
2345 "pointer type %qT as the source; expected %qT "
2346 "or an explicit length", callee, TREE_TYPE (src),
2347 TREE_TYPE (TREE_TYPE (src)));
2348 return;
2349 }
2350 }
57f872a2 2351
2352 if (dest)
2353 {
2354 if (!TYPE_P (sizeof_arg[idx])
2355 && operand_equal_p (dest, sizeof_arg[idx], 0)
2356 && comp_types (TREE_TYPE (dest), type))
2357 {
2358 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2359 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2360 "argument to %<sizeof%> in %qD call is the same "
2361 "expression as the first source; did you mean to "
2362 "remove the addressof?", callee);
2363 else if ((TYPE_PRECISION (TREE_TYPE (type))
2364 == TYPE_PRECISION (char_type_node))
2365 || strop)
2366 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2367 "argument to %<sizeof%> in %qD call is the same "
2368 "expression as the first source; did you mean to "
2369 "provide an explicit length?", callee);
2370 else
2371 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2372 "argument to %<sizeof%> in %qD call is the same "
2373 "expression as the first source; did you mean to "
2374 "dereference it?", callee);
2375 return;
2376 }
2377
2378 if (POINTER_TYPE_P (TREE_TYPE (dest))
2379 && !strop
2380 && comp_types (TREE_TYPE (dest), type)
2381 && !VOID_TYPE_P (TREE_TYPE (type)))
2382 {
2383 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2384 "argument to %<sizeof%> in %qD call is the same "
2385 "pointer type %qT as the first source; expected %qT "
2386 "or an explicit length", callee, TREE_TYPE (dest),
2387 TREE_TYPE (TREE_TYPE (dest)));
2388 return;
2389 }
2390 }
2391
2392 if (src)
2393 {
2394 if (!TYPE_P (sizeof_arg[idx])
2395 && operand_equal_p (src, sizeof_arg[idx], 0)
2396 && comp_types (TREE_TYPE (src), type))
2397 {
2398 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2399 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2400 "argument to %<sizeof%> in %qD call is the same "
2401 "expression as the second source; did you mean to "
2402 "remove the addressof?", callee);
2403 else if ((TYPE_PRECISION (TREE_TYPE (type))
2404 == TYPE_PRECISION (char_type_node))
2405 || strop)
2406 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2407 "argument to %<sizeof%> in %qD call is the same "
2408 "expression as the second source; did you mean to "
2409 "provide an explicit length?", callee);
2410 else
2411 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2412 "argument to %<sizeof%> in %qD call is the same "
2413 "expression as the second source; did you mean to "
2414 "dereference it?", callee);
2415 return;
2416 }
2417
2418 if (POINTER_TYPE_P (TREE_TYPE (src))
2419 && !strop
2420 && comp_types (TREE_TYPE (src), type)
2421 && !VOID_TYPE_P (TREE_TYPE (type)))
2422 {
2423 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2424 "argument to %<sizeof%> in %qD call is the same "
2425 "pointer type %qT as the second source; expected %qT "
2426 "or an explicit length", callee, TREE_TYPE (src),
2427 TREE_TYPE (TREE_TYPE (src)));
2428 return;
2429 }
2430 }
2431
f003f5dc 2432}
2433
3f08e399 2434/* Warn for unlikely, improbable, or stupid DECL declarations
2435 of `main'. */
2436
2437void
2438check_main_parameter_types (tree decl)
2439{
d0af78c5 2440 function_args_iterator iter;
2441 tree type;
3f08e399 2442 int argct = 0;
2443
d0af78c5 2444 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2445 {
2446 /* XXX void_type_node belies the abstraction. */
2447 if (type == void_type_node || type == error_mark_node )
2448 break;
2449
2026249a 2450 tree t = type;
2451 if (TYPE_ATOMIC (t))
2452 pedwarn (input_location, OPT_Wmain,
2453 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2454 type, decl);
2455 while (POINTER_TYPE_P (t))
2456 {
2457 t = TREE_TYPE (t);
2458 if (TYPE_ATOMIC (t))
2459 pedwarn (input_location, OPT_Wmain,
2460 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2461 type, decl);
2462 }
2463
d0af78c5 2464 ++argct;
2465 switch (argct)
2466 {
2467 case 1:
2468 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2469 pedwarn (input_location, OPT_Wmain,
2470 "first argument of %q+D should be %<int%>", decl);
2471 break;
2472
2473 case 2:
2474 if (TREE_CODE (type) != POINTER_TYPE
2475 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2476 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2477 != char_type_node))
2478 pedwarn (input_location, OPT_Wmain,
2479 "second argument of %q+D should be %<char **%>", decl);
2480 break;
2481
2482 case 3:
2483 if (TREE_CODE (type) != POINTER_TYPE
2484 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2485 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2486 != char_type_node))
2487 pedwarn (input_location, OPT_Wmain,
2488 "third argument of %q+D should probably be "
2489 "%<char **%>", decl);
2490 break;
2491 }
2492 }
3f08e399 2493
2494 /* It is intentional that this message does not mention the third
2495 argument because it's only mentioned in an appendix of the
2496 standard. */
2497 if (argct > 0 && (argct < 2 || argct > 3))
d0af78c5 2498 pedwarn (input_location, OPT_Wmain,
2499 "%q+D takes only zero or two arguments", decl);
90e645fa 2500
2501 if (stdarg_p (TREE_TYPE (decl)))
2502 pedwarn (input_location, OPT_Wmain,
2503 "%q+D declared as variadic function", decl);
3f08e399 2504}
2505
73437615 2506/* vector_targets_convertible_p is used for vector pointer types. The
2507 callers perform various checks that the qualifiers are satisfactory,
2508 while OTOH vector_targets_convertible_p ignores the number of elements
2509 in the vectors. That's fine with vector pointers as we can consider,
2510 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2511 and that does not require and conversion of the pointer values.
2512 In contrast, vector_types_convertible_p and
2513 vector_types_compatible_elements_p are used for vector value types. */
ed7c4e62 2514/* True if pointers to distinct types T1 and T2 can be converted to
2515 each other without an explicit cast. Only returns true for opaque
2516 vector types. */
2517bool
2518vector_targets_convertible_p (const_tree t1, const_tree t2)
2519{
6290f0db 2520 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
8d125f7d 2521 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ed7c4e62 2522 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2523 return true;
2524
2525 return false;
2526}
2527
73437615 2528/* vector_types_convertible_p is used for vector value types.
2529 It could in principle call vector_targets_convertible_p as a subroutine,
2530 but then the check for vector type would be duplicated with its callers,
2531 and also the purpose of vector_targets_convertible_p would become
2532 muddled.
2533 Where vector_types_convertible_p returns true, a conversion might still be
2534 needed to make the types match.
2535 In contrast, vector_targets_convertible_p is used for vector pointer
2536 values, and vector_types_compatible_elements_p is used specifically
2537 in the context for binary operators, as a check if use is possible without
2538 conversion. */
546c4794 2539/* True if vector types T1 and T2 can be converted to each other
2540 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2541 can only be converted with -flax-vector-conversions yet that is not
2542 in effect, emit a note telling the user about that option if such
2543 a note has not previously been emitted. */
2544bool
9f627b1a 2545vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
8b4b9810 2546{
546c4794 2547 static bool emitted_lax_note = false;
ae6db8ab 2548 bool convertible_lax;
2549
8d125f7d 2550 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ae6db8ab 2551 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2552 return true;
2553
2554 convertible_lax =
2555 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2556 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
432dd330 2557 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
ae6db8ab 2558 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2559 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
546c4794 2560
2561 if (!convertible_lax || flag_lax_vector_conversions)
2562 return convertible_lax;
2563
2564 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
29f51994 2565 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
546c4794 2566 return true;
2567
2568 if (emit_lax_note && !emitted_lax_note)
2569 {
2570 emitted_lax_note = true;
5bcc316e 2571 inform (input_location, "use -flax-vector-conversions to permit "
546c4794 2572 "conversions between vectors with differing "
2573 "element types or numbers of subparts");
2574 }
2575
2576 return false;
8b4b9810 2577}
2578
bf0cb017 2579/* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2580 and have vector types, V0 has the same type as V1, and the number of
2581 elements of V0, V1, MASK is the same.
2582
2583 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2584 called with two arguments. In this case implementation passes the
2585 first argument twice in order to share the same tree code. This fact
2586 could enable the mask-values being twice the vector length. This is
2587 an implementation accident and this semantics is not guaranteed to
2588 the user. */
2589tree
68ea4406 2590c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2591 bool complain)
bf0cb017 2592{
2593 tree ret;
2594 bool wrap = true;
2595 bool maybe_const = false;
2596 bool two_arguments = false;
2597
2598 if (v1 == NULL_TREE)
2599 {
2600 two_arguments = true;
2601 v1 = v0;
2602 }
2603
2604 if (v0 == error_mark_node || v1 == error_mark_node
2605 || mask == error_mark_node)
2606 return error_mark_node;
2607
6290f0db 2608 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
bf0cb017 2609 {
68ea4406 2610 if (complain)
2611 error_at (loc, "__builtin_shuffle last argument must "
2612 "be an integer vector");
bf0cb017 2613 return error_mark_node;
2614 }
2615
6290f0db 2616 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
2617 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
bf0cb017 2618 {
68ea4406 2619 if (complain)
2620 error_at (loc, "__builtin_shuffle arguments must be vectors");
bf0cb017 2621 return error_mark_node;
2622 }
2623
2624 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2625 {
68ea4406 2626 if (complain)
2627 error_at (loc, "__builtin_shuffle argument vectors must be of "
2628 "the same type");
bf0cb017 2629 return error_mark_node;
2630 }
2631
2632 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2633 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2634 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2635 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2636 {
68ea4406 2637 if (complain)
2638 error_at (loc, "__builtin_shuffle number of elements of the "
2639 "argument vector(s) and the mask vector should "
2640 "be the same");
bf0cb017 2641 return error_mark_node;
2642 }
2643
2644 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2645 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2646 {
68ea4406 2647 if (complain)
2648 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2649 "must have the same size as inner type of the mask");
bf0cb017 2650 return error_mark_node;
2651 }
2652
2653 if (!c_dialect_cxx ())
2654 {
2655 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2656 v0 = c_fully_fold (v0, false, &maybe_const);
2657 wrap &= maybe_const;
2658
2659 if (two_arguments)
2660 v1 = v0 = save_expr (v0);
2661 else
2662 {
2663 v1 = c_fully_fold (v1, false, &maybe_const);
2664 wrap &= maybe_const;
2665 }
2666
2667 mask = c_fully_fold (mask, false, &maybe_const);
2668 wrap &= maybe_const;
2669 }
68ea4406 2670 else if (two_arguments)
2671 v1 = v0 = save_expr (v0);
bf0cb017 2672
2673 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2674
2675 if (!c_dialect_cxx () && !wrap)
2676 ret = c_wrap_maybe_const (ret, true);
2677
2678 return ret;
2679}
2680
7f506bca 2681/* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2682 to integral type. */
2683
2684static tree
2685c_common_get_narrower (tree op, int *unsignedp_ptr)
2686{
2687 op = get_narrower (op, unsignedp_ptr);
2688
2689 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2690 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2691 {
2692 /* C++0x scoped enumerations don't implicitly convert to integral
2693 type; if we stripped an explicit conversion to a larger type we
2694 need to replace it so common_type will still work. */
a51edb4c 2695 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2696 TYPE_UNSIGNED (TREE_TYPE (op)));
7f506bca 2697 op = fold_convert (type, op);
2698 }
2699 return op;
2700}
2701
2561cea2 2702/* This is a helper function of build_binary_op.
2703
2704 For certain operations if both args were extended from the same
2705 smaller type, do the arithmetic in that type and then extend.
2706
2707 BITWISE indicates a bitwise operation.
2708 For them, this optimization is safe only if
2709 both args are zero-extended or both are sign-extended.
2710 Otherwise, we might change the result.
2711 Eg, (short)-1 | (unsigned short)-1 is (int)-1
48e1416a 2712 but calculated in (unsigned short) it would be (unsigned short)-1.
2561cea2 2713*/
7f506bca 2714tree
2715shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2561cea2 2716{
2717 int unsigned0, unsigned1;
2718 tree arg0, arg1;
2719 int uns;
2720 tree type;
2721
2722 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2723 excessive narrowing when we call get_narrower below. For
2724 example, suppose that OP0 is of unsigned int extended
2725 from signed char and that RESULT_TYPE is long long int.
2726 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2727 like
48e1416a 2728
2561cea2 2729 (long long int) (unsigned int) signed_char
2730
2731 which get_narrower would narrow down to
48e1416a 2732
2561cea2 2733 (unsigned int) signed char
48e1416a 2734
2561cea2 2735 If we do not cast OP0 first, get_narrower would return
2736 signed_char, which is inconsistent with the case of the
2737 explicit cast. */
2738 op0 = convert (result_type, op0);
2739 op1 = convert (result_type, op1);
2740
7f506bca 2741 arg0 = c_common_get_narrower (op0, &unsigned0);
2742 arg1 = c_common_get_narrower (op1, &unsigned1);
ab2c1de8 2743
2561cea2 2744 /* UNS is 1 if the operation to be done is an unsigned one. */
2745 uns = TYPE_UNSIGNED (result_type);
2746
2747 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2748 but it *requires* conversion to FINAL_TYPE. */
48e1416a 2749
2561cea2 2750 if ((TYPE_PRECISION (TREE_TYPE (op0))
2751 == TYPE_PRECISION (TREE_TYPE (arg0)))
2752 && TREE_TYPE (op0) != result_type)
2753 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2754 if ((TYPE_PRECISION (TREE_TYPE (op1))
2755 == TYPE_PRECISION (TREE_TYPE (arg1)))
2756 && TREE_TYPE (op1) != result_type)
2757 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
48e1416a 2758
2561cea2 2759 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
48e1416a 2760
2561cea2 2761 /* For bitwise operations, signedness of nominal type
2762 does not matter. Consider only how operands were extended. */
2763 if (bitwise)
2764 uns = unsigned0;
48e1416a 2765
2561cea2 2766 /* Note that in all three cases below we refrain from optimizing
2767 an unsigned operation on sign-extended args.
2768 That would not be valid. */
48e1416a 2769
2561cea2 2770 /* Both args variable: if both extended in same way
2771 from same width, do it in that width.
2772 Do it unsigned if args were zero-extended. */
2773 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2774 < TYPE_PRECISION (result_type))
2775 && (TYPE_PRECISION (TREE_TYPE (arg1))
2776 == TYPE_PRECISION (TREE_TYPE (arg0)))
2777 && unsigned0 == unsigned1
2778 && (unsigned0 || !uns))
2779 return c_common_signed_or_unsigned_type
2780 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2781
2782 else if (TREE_CODE (arg0) == INTEGER_CST
2783 && (unsigned1 || !uns)
2784 && (TYPE_PRECISION (TREE_TYPE (arg1))
2785 < TYPE_PRECISION (result_type))
2786 && (type
2787 = c_common_signed_or_unsigned_type (unsigned1,
2788 TREE_TYPE (arg1)))
2789 && !POINTER_TYPE_P (type)
2790 && int_fits_type_p (arg0, type))
2791 return type;
2792
2793 else if (TREE_CODE (arg1) == INTEGER_CST
2794 && (unsigned0 || !uns)
2795 && (TYPE_PRECISION (TREE_TYPE (arg0))
2796 < TYPE_PRECISION (result_type))
2797 && (type
2798 = c_common_signed_or_unsigned_type (unsigned0,
2799 TREE_TYPE (arg0)))
2800 && !POINTER_TYPE_P (type)
2801 && int_fits_type_p (arg1, type))
2802 return type;
2803
2804 return result_type;
2805}
2806
e53013a8 2807/* Returns true iff any integer value of type FROM_TYPE can be represented as
2808 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2809
2810static bool
2811int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2812{
2813 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2814 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2815 REAL_VALUE_TYPE real_low_bound =
2816 real_value_from_int_cst (0, type_low_bound);
2817 REAL_VALUE_TYPE real_high_bound =
2818 real_value_from_int_cst (0, type_high_bound);
2819
2820 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2821 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2822}
2823
2824/* Checks if expression EXPR of complex/real/integer type cannot be converted
2825 to the complex/real/integer type TYPE. Function returns non-zero when:
22a75734 2826 * EXPR is a constant which cannot be exactly converted to TYPE.
2827 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
e53013a8 2828 for EXPR type and TYPE being both integers or both real, or both
2829 complex.
2830 * EXPR is not a constant of complex type and TYPE is a real or
2831 an integer.
22a75734 2832 * EXPR is not a constant of real type and TYPE is an integer.
2833 * EXPR is not a constant of integer type which cannot be
2834 exactly converted to real type.
e53013a8 2835
7dfa155b 2836 Function allows conversions between types of different signedness and
ca9d7d74 2837 can return SAFE_CONVERSION (zero) in that case. Function can produce
e53013a8 2838 signedness warnings if PRODUCE_WARNS is true.
2839
2840 Function allows conversions from complex constants to non-complex types,
2841 provided that imaginary part is zero and real part can be safely converted
2842 to TYPE. */
22a75734 2843
ca9d7d74 2844enum conversion_safety
22a75734 2845unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
d31d55f0 2846{
ca9d7d74 2847 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
27259707 2848 tree expr_type = TREE_TYPE (expr);
22a75734 2849 loc = expansion_point_location_if_in_system_header (loc);
d31d55f0 2850
7dfa155b 2851 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
5b16c152 2852 {
e53013a8 2853 /* If type is complex, we are interested in compatibility with
2854 underlying type. */
2855 if (TREE_CODE (type) == COMPLEX_TYPE)
2856 type = TREE_TYPE (type);
2857
d31d55f0 2858 /* Warn for real constant that is not an exact integer converted
7dfa155b 2859 to integer type. */
27259707 2860 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2861 && TREE_CODE (type) == INTEGER_TYPE)
2862 {
2863 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
ca9d7d74 2864 give_warning = UNSAFE_REAL;
7dfa155b 2865 }
da1fb07b 2866 /* Warn for an integer constant that does not fit into integer type. */
27259707 2867 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2868 && TREE_CODE (type) == INTEGER_TYPE
2869 && !int_fits_type_p (expr, type))
2870 {
2871 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
27259707 2872 && tree_int_cst_sgn (expr) < 0)
7dfa155b 2873 {
2874 if (produce_warns)
2875 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2876 " implicitly converted to unsigned type");
2877 }
2878 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2879 {
2880 if (produce_warns)
2881 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2882 " constant value to negative integer");
2883 }
7ee0d227 2884 else
ca9d7d74 2885 give_warning = UNSAFE_OTHER;
7dfa155b 2886 }
d31d55f0 2887 else if (TREE_CODE (type) == REAL_TYPE)
7dfa155b 2888 {
2889 /* Warn for an integer constant that does not fit into real type. */
2890 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2891 {
2892 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2893 if (!exact_real_truncate (TYPE_MODE (type), &a))
ca9d7d74 2894 give_warning = UNSAFE_REAL;
7dfa155b 2895 }
2896 /* Warn for a real constant that does not fit into a smaller
2897 real type. */
2898 else if (TREE_CODE (expr_type) == REAL_TYPE
2899 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2900 {
2901 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2902 if (!exact_real_truncate (TYPE_MODE (type), &a))
ca9d7d74 2903 give_warning = UNSAFE_REAL;
7dfa155b 2904 }
2905 }
2906 }
e53013a8 2907
2908 else if (TREE_CODE (expr) == COMPLEX_CST)
2909 {
2910 tree imag_part = TREE_IMAGPART (expr);
2911 /* Conversion from complex constant with zero imaginary part,
2912 perform check for conversion of real part. */
2913 if ((TREE_CODE (imag_part) == REAL_CST
2914 && real_zerop (imag_part))
2915 || (TREE_CODE (imag_part) == INTEGER_CST
2916 && integer_zerop (imag_part)))
2917 /* Note: in this branch we use recursive call to unsafe_conversion_p
2918 with different type of EXPR, but it is still safe, because when EXPR
2919 is a constant, it's type is not used in text of generated warnings
2920 (otherwise they could sound misleading). */
2921 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2922 produce_warns);
2923 /* Conversion from complex constant with non-zero imaginary part. */
2924 else
2925 {
2926 /* Conversion to complex type.
2927 Perform checks for both real and imaginary parts. */
2928 if (TREE_CODE (type) == COMPLEX_TYPE)
2929 {
2930 /* Unfortunately, produce_warns must be false in two subsequent
2931 calls of unsafe_conversion_p, because otherwise we could
2932 produce strange "double" warnings, if both real and imaginary
2933 parts have conversion problems related to signedness.
2934
2935 For example:
2936 int32_t _Complex a = 0x80000000 + 0x80000000i;
2937
2938 Possible solution: add a separate function for checking
2939 constants and combine result of two calls appropriately. */
2940 enum conversion_safety re_safety =
2941 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2942 enum conversion_safety im_safety =
2943 unsafe_conversion_p (loc, type, imag_part, false);
2944
2945 /* Merge the results into appropriate single warning. */
2946
2947 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2948 if (re_safety == im_safety)
2949 give_warning = re_safety;
2950 else if (!re_safety && im_safety)
2951 give_warning = im_safety;
2952 else if (re_safety && !im_safety)
2953 give_warning = re_safety;
2954 else
2955 give_warning = UNSAFE_OTHER;
2956 }
2957 /* Warn about conversion from complex to real or integer type. */
2958 else
2959 give_warning = UNSAFE_IMAGINARY;
2960 }
2961 }
2962
2963 /* Checks for remaining case: EXPR is not constant. */
7dfa155b 2964 else
2965 {
d31d55f0 2966 /* Warn for real types converted to integer types. */
2561cea2 2967 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2968 && TREE_CODE (type) == INTEGER_TYPE)
ca9d7d74 2969 give_warning = UNSAFE_REAL;
d31d55f0 2970
2561cea2 2971 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2972 && TREE_CODE (type) == INTEGER_TYPE)
2973 {
69609004 2974 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
f9d856a4 2975 expr = get_unwidened (expr, 0);
2561cea2 2976 expr_type = TREE_TYPE (expr);
69609004 2977
2561cea2 2978 /* Don't warn for short y; short x = ((int)y & 0xff); */
48e1416a 2979 if (TREE_CODE (expr) == BIT_AND_EXPR
7dfa155b 2980 || TREE_CODE (expr) == BIT_IOR_EXPR
2561cea2 2981 || TREE_CODE (expr) == BIT_XOR_EXPR)
2982 {
27259707 2983 /* If both args were extended from a shortest type,
2984 use that type if that is safe. */
48e1416a 2985 expr_type = shorten_binary_op (expr_type,
2986 TREE_OPERAND (expr, 0),
2987 TREE_OPERAND (expr, 1),
2561cea2 2988 /* bitwise */1);
2989
2561cea2 2990 if (TREE_CODE (expr) == BIT_AND_EXPR)
2991 {
2992 tree op0 = TREE_OPERAND (expr, 0);
2993 tree op1 = TREE_OPERAND (expr, 1);
30de145b 2994 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2995 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2996
2997 /* If one of the operands is a non-negative constant
2998 that fits in the target type, then the type of the
2999 other operand does not matter. */
2561cea2 3000 if ((TREE_CODE (op0) == INTEGER_CST
3001 && int_fits_type_p (op0, c_common_signed_type (type))
3002 && int_fits_type_p (op0, c_common_unsigned_type (type)))
3003 || (TREE_CODE (op1) == INTEGER_CST
27259707 3004 && int_fits_type_p (op1, c_common_signed_type (type))
48e1416a 3005 && int_fits_type_p (op1,
27259707 3006 c_common_unsigned_type (type))))
ca9d7d74 3007 return SAFE_CONVERSION;
30de145b 3008 /* If constant is unsigned and fits in the target
3009 type, then the result will also fit. */
3010 else if ((TREE_CODE (op0) == INTEGER_CST
48e1416a 3011 && unsigned0
30de145b 3012 && int_fits_type_p (op0, type))
3013 || (TREE_CODE (op1) == INTEGER_CST
3014 && unsigned1
3015 && int_fits_type_p (op1, type)))
ca9d7d74 3016 return SAFE_CONVERSION;
2561cea2 3017 }
3018 }
7dfa155b 3019 /* Warn for integer types converted to smaller integer types. */
48e1416a 3020 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
ca9d7d74 3021 give_warning = UNSAFE_OTHER;
7ee0d227 3022
3023 /* When they are the same width but different signedness,
3024 then the value may change. */
7dfa155b 3025 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2561cea2 3026 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
7ee0d227 3027 /* Even when converted to a bigger type, if the type is
3028 unsigned but expr is signed, then negative values
3029 will be changed. */
7dfa155b 3030 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
3031 && produce_warns)
200dd99c 3032 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
3033 "may change the sign of the result",
3034 type, expr_type);
7dfa155b 3035 }
d31d55f0 3036
3037 /* Warn for integer types converted to real types if and only if
7dfa155b 3038 all the range of values of the integer type cannot be
3039 represented by the real type. */
2561cea2 3040 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 3041 && TREE_CODE (type) == REAL_TYPE)
3042 {
4c2cfa81 3043 /* Don't warn about char y = 0xff; float x = (int) y; */
3044 expr = get_unwidened (expr, 0);
3045 expr_type = TREE_TYPE (expr);
3046
e53013a8 3047 if (!int_safely_convertible_to_real_p (expr_type, type))
ca9d7d74 3048 give_warning = UNSAFE_OTHER;
7dfa155b 3049 }
d31d55f0 3050
3051 /* Warn for real types converted to smaller real types. */
2561cea2 3052 else if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 3053 && TREE_CODE (type) == REAL_TYPE
3054 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
ca9d7d74 3055 give_warning = UNSAFE_REAL;
e53013a8 3056
3057 /* Check conversion between two complex types. */
3058 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
3059 && TREE_CODE (type) == COMPLEX_TYPE)
3060 {
3061 /* Extract underlying types (i.e., type of real and imaginary
3062 parts) of expr_type and type. */
3063 tree from_type = TREE_TYPE (expr_type);
3064 tree to_type = TREE_TYPE (type);
3065
3066 /* Warn for real types converted to integer types. */
3067 if (TREE_CODE (from_type) == REAL_TYPE
3068 && TREE_CODE (to_type) == INTEGER_TYPE)
3069 give_warning = UNSAFE_REAL;
3070
3071 /* Warn for real types converted to smaller real types. */
3072 else if (TREE_CODE (from_type) == REAL_TYPE
3073 && TREE_CODE (to_type) == REAL_TYPE
3074 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
3075 give_warning = UNSAFE_REAL;
3076
3077 /* Check conversion for complex integer types. Here implementation
3078 is simpler than for real-domain integers because it does not
3079 involve sophisticated cases, such as bitmasks, casts, etc. */
3080 else if (TREE_CODE (from_type) == INTEGER_TYPE
3081 && TREE_CODE (to_type) == INTEGER_TYPE)
3082 {
3083 /* Warn for integer types converted to smaller integer types. */
3084 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
3085 give_warning = UNSAFE_OTHER;
3086
3087 /* Check for different signedness, see case for real-domain
3088 integers (above) for a more detailed comment. */
3089 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
3090 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
3091 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
3092 && produce_warns)
3093 warning_at (loc, OPT_Wsign_conversion,
3094 "conversion to %qT from %qT "
3095 "may change the sign of the result",
3096 type, expr_type);
3097 }
3098 else if (TREE_CODE (from_type) == INTEGER_TYPE
3099 && TREE_CODE (to_type) == REAL_TYPE
3100 && !int_safely_convertible_to_real_p (from_type, to_type))
3101 give_warning = UNSAFE_OTHER;
3102 }
3103
3104 /* Warn for complex types converted to real or integer types. */
3105 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
3106 && TREE_CODE (type) != COMPLEX_TYPE)
3107 give_warning = UNSAFE_IMAGINARY;
7dfa155b 3108 }
3109
3110 return give_warning;
3111}
3112
3113/* Warns if the conversion of EXPR to TYPE may alter a value.
3114 This is a helper function for warnings_for_convert_and_check. */
3115
3116static void
22a75734 3117conversion_warning (location_t loc, tree type, tree expr)
7dfa155b 3118{
7dfa155b 3119 tree expr_type = TREE_TYPE (expr);
ca9d7d74 3120 enum conversion_safety conversion_kind;
d31d55f0 3121
ca9d7d74 3122 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
7dfa155b 3123 return;
d31d55f0 3124
ec704957 3125 /* This may happen, because for LHS op= RHS we preevaluate
3126 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
3127 means we could no longer see the code of the EXPR. */
3128 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
3129 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
3130 if (TREE_CODE (expr) == SAVE_EXPR)
3131 expr = TREE_OPERAND (expr, 0);
3132
7dfa155b 3133 switch (TREE_CODE (expr))
3134 {
3135 case EQ_EXPR:
3136 case NE_EXPR:
3137 case LE_EXPR:
3138 case GE_EXPR:
3139 case LT_EXPR:
3140 case GT_EXPR:
3141 case TRUTH_ANDIF_EXPR:
3142 case TRUTH_ORIF_EXPR:
3143 case TRUTH_AND_EXPR:
3144 case TRUTH_OR_EXPR:
3145 case TRUTH_XOR_EXPR:
3146 case TRUTH_NOT_EXPR:
3147 /* Conversion from boolean to a signed:1 bit-field (which only
3148 can hold the values 0 and -1) doesn't lose information - but
3149 it does change the value. */
3150 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
3151 warning_at (loc, OPT_Wconversion,
3152 "conversion to %qT from boolean expression", type);
3153 return;
3154
3155 case REAL_CST:
3156 case INTEGER_CST:
e53013a8 3157 case COMPLEX_CST:
22a75734 3158 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
ca9d7d74 3159 if (conversion_kind == UNSAFE_REAL)
3160 warning_at (loc, OPT_Wfloat_conversion,
3161 "conversion to %qT alters %qT constant value",
3162 type, expr_type);
3163 else if (conversion_kind)
7dfa155b 3164 warning_at (loc, OPT_Wconversion,
3165 "conversion to %qT alters %qT constant value",
3166 type, expr_type);
3167 return;
3168
3169 case COND_EXPR:
3170 {
0e4e775a 3171 /* In case of COND_EXPR, we do not care about the type of
3172 COND_EXPR, only about the conversion of each operand. */
3173 tree op1 = TREE_OPERAND (expr, 1);
3174 tree op2 = TREE_OPERAND (expr, 2);
3175
22a75734 3176 conversion_warning (loc, type, op1);
3177 conversion_warning (loc, type, op2);
0e4e775a 3178 return;
7dfa155b 3179 }
3180
3181 default: /* 'expr' is not a constant. */
22a75734 3182 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
ca9d7d74 3183 if (conversion_kind == UNSAFE_REAL)
3184 warning_at (loc, OPT_Wfloat_conversion,
3185 "conversion to %qT from %qT may alter its value",
3186 type, expr_type);
e53013a8 3187 else if (conversion_kind == UNSAFE_IMAGINARY)
3188 warning_at (loc, OPT_Wconversion,
3189 "conversion to %qT from %qT discards imaginary component",
3190 type, expr_type);
ca9d7d74 3191 else if (conversion_kind)
7dfa155b 3192 warning_at (loc, OPT_Wconversion,
200dd99c 3193 "conversion to %qT from %qT may alter its value",
3194 type, expr_type);
d31d55f0 3195 }
3196}
3197
59dd8856 3198/* Produce warnings after a conversion. RESULT is the result of
3199 converting EXPR to TYPE. This is a helper function for
3200 convert_and_check and cp_convert_and_check. */
2a1736ed 3201
59dd8856 3202void
22a75734 3203warnings_for_convert_and_check (location_t loc, tree type, tree expr,
3204 tree result)
2a1736ed 3205{
22a75734 3206 loc = expansion_point_location_if_in_system_header (loc);
61f69bc9 3207
da1fb07b 3208 if (TREE_CODE (expr) == INTEGER_CST
3209 && (TREE_CODE (type) == INTEGER_TYPE
3210 || TREE_CODE (type) == ENUMERAL_TYPE)
3211 && !int_fits_type_p (expr, type))
3212 {
d31d55f0 3213 /* Do not diagnose overflow in a constant expression merely
3214 because a conversion overflowed. */
da1fb07b 3215 if (TREE_OVERFLOW (result))
eddad94a 3216 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
3217
da1fb07b 3218 if (TYPE_UNSIGNED (type))
d31d55f0 3219 {
da1fb07b 3220 /* This detects cases like converting -129 or 256 to
3221 unsigned char. */
3222 if (!int_fits_type_p (expr, c_common_signed_type (type)))
61f69bc9 3223 warning_at (loc, OPT_Woverflow,
3224 "large integer implicitly truncated to unsigned type");
7ee0d227 3225 else
22a75734 3226 conversion_warning (loc, type, expr);
da1fb07b 3227 }
48e1416a 3228 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
22a75734 3229 warning_at (loc, OPT_Woverflow,
e0913805 3230 "overflow in implicit constant conversion");
3231 /* No warning for converting 0x80000000 to int. */
3232 else if (pedantic
3233 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
3234 || TYPE_PRECISION (TREE_TYPE (expr))
3235 != TYPE_PRECISION (type)))
61f69bc9 3236 warning_at (loc, OPT_Woverflow,
3237 "overflow in implicit constant conversion");
e0913805 3238
7ee0d227 3239 else
22a75734 3240 conversion_warning (loc, type, expr);
2a1736ed 3241 }
9421ebb9 3242 else if ((TREE_CODE (result) == INTEGER_CST
3243 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
61f69bc9 3244 warning_at (loc, OPT_Woverflow,
3245 "overflow in implicit constant conversion");
7ee0d227 3246 else
22a75734 3247 conversion_warning (loc, type, expr);
59dd8856 3248}
3249
3250
3251/* Convert EXPR to TYPE, warning about conversion problems with constants.
3252 Invoke this function on every expression that is converted implicitly,
3253 i.e. because of language rules and not because of an explicit cast. */
3254
3255tree
22a75734 3256convert_and_check (location_t loc, tree type, tree expr)
59dd8856 3257{
3258 tree result;
c6418a4e 3259 tree expr_for_warning;
3260
3261 /* Convert from a value with possible excess precision rather than
3262 via the semantic type, but do not warn about values not fitting
3263 exactly in the semantic type. */
3264 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
3265 {
3266 tree orig_type = TREE_TYPE (expr);
3267 expr = TREE_OPERAND (expr, 0);
3268 expr_for_warning = convert (orig_type, expr);
3269 if (orig_type == type)
3270 return expr_for_warning;
3271 }
3272 else
3273 expr_for_warning = expr;
59dd8856 3274
3275 if (TREE_TYPE (expr) == type)
3276 return expr;
48e1416a 3277
59dd8856 3278 result = convert (type, expr);
3279
48d94ede 3280 if (c_inhibit_evaluation_warnings == 0
3281 && !TREE_OVERFLOW_P (expr)
3282 && result != error_mark_node)
22a75734 3283 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
59dd8856 3284
da1fb07b 3285 return result;
b2806639 3286}
3287\f
4e91a871 3288/* A node in a list that describes references to variables (EXPR), which are
3289 either read accesses if WRITER is zero, or write accesses, in which case
3290 WRITER is the parent of EXPR. */
3291struct tlist
3292{
3293 struct tlist *next;
3294 tree expr, writer;
3295};
3296
3297/* Used to implement a cache the results of a call to verify_tree. We only
3298 use this for SAVE_EXPRs. */
3299struct tlist_cache
3300{
3301 struct tlist_cache *next;
3302 struct tlist *cache_before_sp;
3303 struct tlist *cache_after_sp;
3304 tree expr;
481c6ce6 3305};
3306
4e91a871 3307/* Obstack to use when allocating tlist structures, and corresponding
3308 firstobj. */
3309static struct obstack tlist_obstack;
3310static char *tlist_firstobj = 0;
3311
3312/* Keep track of the identifiers we've warned about, so we can avoid duplicate
3313 warnings. */
3314static struct tlist *warned_ids;
3315/* SAVE_EXPRs need special treatment. We process them only once and then
3316 cache the results. */
3317static struct tlist_cache *save_expr_cache;
3318
1cae46be 3319static void add_tlist (struct tlist **, struct tlist *, tree, int);
3320static void merge_tlist (struct tlist **, struct tlist *, int);
3321static void verify_tree (tree, struct tlist **, struct tlist **, tree);
3322static int warning_candidate_p (tree);
79973b57 3323static bool candidate_equal_p (const_tree, const_tree);
1cae46be 3324static void warn_for_collisions (struct tlist *);
3325static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
3326static struct tlist *new_tlist (struct tlist *, tree, tree);
481c6ce6 3327
4e91a871 3328/* Create a new struct tlist and fill in its fields. */
3329static struct tlist *
1cae46be 3330new_tlist (struct tlist *next, tree t, tree writer)
4e91a871 3331{
3332 struct tlist *l;
9318f22c 3333 l = XOBNEW (&tlist_obstack, struct tlist);
4e91a871 3334 l->next = next;
3335 l->expr = t;
3336 l->writer = writer;
3337 return l;
3338}
3339
3340/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3341 is nonnull, we ignore any node we find which has a writer equal to it. */
3342
3343static void
1cae46be 3344add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
4e91a871 3345{
3346 while (add)
3347 {
3348 struct tlist *next = add->next;
84166705 3349 if (!copy)
4e91a871 3350 add->next = *to;
79973b57 3351 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
4e91a871 3352 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3353 add = next;
3354 }
3355}
3356
3357/* Merge the nodes of ADD into TO. This merging process is done so that for
3358 each variable that already exists in TO, no new node is added; however if
3359 there is a write access recorded in ADD, and an occurrence on TO is only
3360 a read access, then the occurrence in TO will be modified to record the
3361 write. */
481c6ce6 3362
3363static void
1cae46be 3364merge_tlist (struct tlist **to, struct tlist *add, int copy)
4e91a871 3365{
3366 struct tlist **end = to;
3367
3368 while (*end)
3369 end = &(*end)->next;
3370
3371 while (add)
3372 {
3373 int found = 0;
3374 struct tlist *tmp2;
3375 struct tlist *next = add->next;
3376
3377 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
79973b57 3378 if (candidate_equal_p (tmp2->expr, add->expr))
4e91a871 3379 {
3380 found = 1;
84166705 3381 if (!tmp2->writer)
4e91a871 3382 tmp2->writer = add->writer;
3383 }
84166705 3384 if (!found)
4e91a871 3385 {
312243bb 3386 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
4e91a871 3387 end = &(*end)->next;
3388 *end = 0;
3389 }
3390 add = next;
3391 }
3392}
3393
3394/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3395 references in list LIST conflict with it, excluding reads if ONLY writers
3396 is nonzero. */
3397
3398static void
1cae46be 3399warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3400 int only_writes)
4e91a871 3401{
3402 struct tlist *tmp;
3403
3404 /* Avoid duplicate warnings. */
3405 for (tmp = warned_ids; tmp; tmp = tmp->next)
79973b57 3406 if (candidate_equal_p (tmp->expr, written))
4e91a871 3407 return;
3408
3409 while (list)
3410 {
79973b57 3411 if (candidate_equal_p (list->expr, written)
3412 && !candidate_equal_p (list->writer, writer)
3413 && (!only_writes || list->writer))
4e91a871 3414 {
3415 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3df42822 3416 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
6513b50d 3417 OPT_Wsequence_point, "operation on %qE may be undefined",
3418 list->expr);
4e91a871 3419 }
3420 list = list->next;
3421 }
3422}
3423
3424/* Given a list LIST of references to variables, find whether any of these
3425 can cause conflicts due to missing sequence points. */
3426
3427static void
1cae46be 3428warn_for_collisions (struct tlist *list)
4e91a871 3429{
3430 struct tlist *tmp;
1cae46be 3431
4e91a871 3432 for (tmp = list; tmp; tmp = tmp->next)
3433 {
3434 if (tmp->writer)
3435 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3436 }
3437}
3438
734c98be 3439/* Return nonzero if X is a tree that can be verified by the sequence point
4e91a871 3440 warnings. */
3441static int
1cae46be 3442warning_candidate_p (tree x)
481c6ce6 3443{
6ef8d12f 3444 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3445 return 0;
3446
027fc6ef 3447 if (TREE_CODE (x) == BLOCK)
3448 return 0;
3449
6ef8d12f 3450 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
79973b57 3451 (lvalue_p) crash on TRY/CATCH. */
6ef8d12f 3452 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3453 return 0;
3454
3455 if (!lvalue_p (x))
3456 return 0;
3457
3458 /* No point to track non-const calls, they will never satisfy
3459 operand_equal_p. */
3460 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3461 return 0;
3462
3463 if (TREE_CODE (x) == STRING_CST)
3464 return 0;
3465
3466 return 1;
79973b57 3467}
3468
3469/* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3470static bool
3471candidate_equal_p (const_tree x, const_tree y)
3472{
3473 return (x == y) || (x && y && operand_equal_p (x, y, 0));
4e91a871 3474}
481c6ce6 3475
4e91a871 3476/* Walk the tree X, and record accesses to variables. If X is written by the
3477 parent tree, WRITER is the parent.
3478 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3479 expression or its only operand forces a sequence point, then everything up
3480 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3481 in PNO_SP.
3482 Once we return, we will have emitted warnings if any subexpression before
3483 such a sequence point could be undefined. On a higher level, however, the
3484 sequence point may not be relevant, and we'll merge the two lists.
3485
3486 Example: (b++, a) + b;
3487 The call that processes the COMPOUND_EXPR will store the increment of B
3488 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3489 processes the PLUS_EXPR will need to merge the two lists so that
3490 eventually, all accesses end up on the same list (and we'll warn about the
3491 unordered subexpressions b++ and b.
3492
3493 A note on merging. If we modify the former example so that our expression
3494 becomes
3495 (b++, b) + a
3496 care must be taken not simply to add all three expressions into the final
3497 PNO_SP list. The function merge_tlist takes care of that by merging the
3498 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3499 way, so that no more than one access to B is recorded. */
481c6ce6 3500
4e91a871 3501static void
1cae46be 3502verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3503 tree writer)
4e91a871 3504{
3505 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3506 enum tree_code code;
ce45a448 3507 enum tree_code_class cl;
481c6ce6 3508
e5b75768 3509 /* X may be NULL if it is the operand of an empty statement expression
3510 ({ }). */
3511 if (x == NULL)
3512 return;
3513
4e91a871 3514 restart:
3515 code = TREE_CODE (x);
e916c70c 3516 cl = TREE_CODE_CLASS (code);
481c6ce6 3517
4e91a871 3518 if (warning_candidate_p (x))
79973b57 3519 *pno_sp = new_tlist (*pno_sp, x, writer);
4e91a871 3520
3521 switch (code)
3522 {
67b28e3e 3523 case CONSTRUCTOR:
8e71dad2 3524 case SIZEOF_EXPR:
67b28e3e 3525 return;
3526
4e91a871 3527 case COMPOUND_EXPR:
3528 case TRUTH_ANDIF_EXPR:
3529 case TRUTH_ORIF_EXPR:
3530 tmp_before = tmp_nosp = tmp_list3 = 0;
3531 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3532 warn_for_collisions (tmp_nosp);
3533 merge_tlist (pbefore_sp, tmp_before, 0);
3534 merge_tlist (pbefore_sp, tmp_nosp, 0);
3535 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3536 merge_tlist (pbefore_sp, tmp_list3, 0);
3537 return;
3538
3539 case COND_EXPR:
3540 tmp_before = tmp_list2 = 0;
3541 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3542 warn_for_collisions (tmp_list2);
3543 merge_tlist (pbefore_sp, tmp_before, 0);
312243bb 3544 merge_tlist (pbefore_sp, tmp_list2, 0);
4e91a871 3545
3546 tmp_list3 = tmp_nosp = 0;
3547 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3548 warn_for_collisions (tmp_nosp);
3549 merge_tlist (pbefore_sp, tmp_list3, 0);
3550
3551 tmp_list3 = tmp_list2 = 0;
3552 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3553 warn_for_collisions (tmp_list2);
3554 merge_tlist (pbefore_sp, tmp_list3, 0);
3555 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3556 two first, to avoid warning for (a ? b++ : b++). */
3557 merge_tlist (&tmp_nosp, tmp_list2, 0);
3558 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3559 return;
3560
481c6ce6 3561 case PREDECREMENT_EXPR:
3562 case PREINCREMENT_EXPR:
3563 case POSTDECREMENT_EXPR:
3564 case POSTINCREMENT_EXPR:
4e91a871 3565 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3566 return;
3567
3568 case MODIFY_EXPR:
3569 tmp_before = tmp_nosp = tmp_list3 = 0;
3570 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3571 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3572 /* Expressions inside the LHS are not ordered wrt. the sequence points
3573 in the RHS. Example:
3574 *a = (a++, 2)
3575 Despite the fact that the modification of "a" is in the before_sp
3576 list (tmp_before), it conflicts with the use of "a" in the LHS.
3577 We can handle this by adding the contents of tmp_list3
3578 to those of tmp_before, and redoing the collision warnings for that
3579 list. */
3580 add_tlist (&tmp_before, tmp_list3, x, 1);
3581 warn_for_collisions (tmp_before);
3582 /* Exclude the LHS itself here; we first have to merge it into the
3583 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3584 didn't exclude the LHS, we'd get it twice, once as a read and once
3585 as a write. */
3586 add_tlist (pno_sp, tmp_list3, x, 0);
3587 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3588
3589 merge_tlist (pbefore_sp, tmp_before, 0);
3590 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3591 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3592 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3593 return;
481c6ce6 3594
3595 case CALL_EXPR:
4e91a871 3596 /* We need to warn about conflicts among arguments and conflicts between
3597 args and the function address. Side effects of the function address,
3598 however, are not ordered by the sequence point of the call. */
c2f47e15 3599 {
3600 call_expr_arg_iterator iter;
3601 tree arg;
48e1416a 3602 tmp_before = tmp_nosp = 0;
c2f47e15 3603 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3604 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3605 {
3606 tmp_list2 = tmp_list3 = 0;
3607 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3608 merge_tlist (&tmp_list3, tmp_list2, 0);
3609 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3610 }
3611 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3612 warn_for_collisions (tmp_before);
3613 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3614 return;
3615 }
481c6ce6 3616
3617 case TREE_LIST:
3618 /* Scan all the list, e.g. indices of multi dimensional array. */
3619 while (x)
3620 {
4e91a871 3621 tmp_before = tmp_nosp = 0;
3622 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3623 merge_tlist (&tmp_nosp, tmp_before, 0);
3624 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
481c6ce6 3625 x = TREE_CHAIN (x);
3626 }
4e91a871 3627 return;
481c6ce6 3628
4e91a871 3629 case SAVE_EXPR:
3630 {
3631 struct tlist_cache *t;
3632 for (t = save_expr_cache; t; t = t->next)
79973b57 3633 if (candidate_equal_p (t->expr, x))
4e91a871 3634 break;
481c6ce6 3635
84166705 3636 if (!t)
481c6ce6 3637 {
9318f22c 3638 t = XOBNEW (&tlist_obstack, struct tlist_cache);
4e91a871 3639 t->next = save_expr_cache;
3640 t->expr = x;
3641 save_expr_cache = t;
3642
3643 tmp_before = tmp_nosp = 0;
3644 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3645 warn_for_collisions (tmp_nosp);
3646
3647 tmp_list3 = 0;
312243bb 3648 merge_tlist (&tmp_list3, tmp_nosp, 0);
4e91a871 3649 t->cache_before_sp = tmp_before;
3650 t->cache_after_sp = tmp_list3;
481c6ce6 3651 }
4e91a871 3652 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3653 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3654 return;
3655 }
481c6ce6 3656
012916cb 3657 case ADDR_EXPR:
3658 x = TREE_OPERAND (x, 0);
3659 if (DECL_P (x))
3660 return;
3661 writer = 0;
3662 goto restart;
3663
ce45a448 3664 default:
3665 /* For other expressions, simply recurse on their operands.
a0c938f0 3666 Manual tail recursion for unary expressions.
ce45a448 3667 Other non-expressions need not be processed. */
3668 if (cl == tcc_unary)
3669 {
ce45a448 3670 x = TREE_OPERAND (x, 0);
3671 writer = 0;
3672 goto restart;
3673 }
3674 else if (IS_EXPR_CODE_CLASS (cl))
3675 {
3676 int lp;
c2f47e15 3677 int max = TREE_OPERAND_LENGTH (x);
ce45a448 3678 for (lp = 0; lp < max; lp++)
3679 {
3680 tmp_before = tmp_nosp = 0;
3681 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3682 merge_tlist (&tmp_nosp, tmp_before, 0);
3683 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3684 }
3685 }
3686 return;
481c6ce6 3687 }
481c6ce6 3688}
3689
974e2c0c 3690/* Try to warn for undefined behavior in EXPR due to missing sequence
481c6ce6 3691 points. */
3692
4b987fac 3693DEBUG_FUNCTION void
1cae46be 3694verify_sequence_points (tree expr)
481c6ce6 3695{
4e91a871 3696 struct tlist *before_sp = 0, *after_sp = 0;
481c6ce6 3697
4e91a871 3698 warned_ids = 0;
3699 save_expr_cache = 0;
3700 if (tlist_firstobj == 0)
481c6ce6 3701 {
4e91a871 3702 gcc_obstack_init (&tlist_obstack);
4fd61bc6 3703 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
481c6ce6 3704 }
3705
4e91a871 3706 verify_tree (expr, &before_sp, &after_sp, 0);
3707 warn_for_collisions (after_sp);
3708 obstack_free (&tlist_obstack, tlist_firstobj);
481c6ce6 3709}
b0fc3e72 3710\f
3711/* Validate the expression after `case' and apply default promotions. */
3712
2ca392fd 3713static tree
2d2f6a15 3714check_case_value (location_t loc, tree value)
b0fc3e72 3715{
3716 if (value == NULL_TREE)
3717 return value;
3718
b96dc121 3719 if (TREE_CODE (value) == INTEGER_CST)
3720 /* Promote char or short to int. */
3721 value = perform_integral_promotions (value);
3722 else if (value != error_mark_node)
b0fc3e72 3723 {
2d2f6a15 3724 error_at (loc, "case label does not reduce to an integer constant");
b0fc3e72 3725 value = error_mark_node;
3726 }
b0fc3e72 3727
6433f1c2 3728 constant_expression_warning (value);
3729
b0fc3e72 3730 return value;
3731}
3732\f
2ca392fd 3733/* See if the case values LOW and HIGH are in the range of the original
5c9dae64 3734 type (i.e. before the default conversion to int) of the switch testing
2ca392fd 3735 expression.
3736 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
91275768 3737 the type before promoting it. CASE_LOW_P is a pointer to the lower
2ca392fd 3738 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3739 if the case is not a case range.
3740 The caller has to make sure that we are not called with NULL for
be23b16f 3741 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
3742 was a case value that doesn't fit into the range of the ORIG_TYPE.
442e3cb9 3743 Returns true if the case label is in range of ORIG_TYPE (saturated or
2ca392fd 3744 untouched) or false if the label is out of range. */
3745
3746static bool
f61a9bc2 3747check_case_bounds (location_t loc, tree type, tree orig_type,
be23b16f 3748 tree *case_low_p, tree *case_high_p,
3749 bool *outside_range_p)
2ca392fd 3750{
3751 tree min_value, max_value;
3752 tree case_low = *case_low_p;
3753 tree case_high = case_high_p ? *case_high_p : case_low;
3754
3755 /* If there was a problem with the original type, do nothing. */
3756 if (orig_type == error_mark_node)
3757 return true;
3758
3759 min_value = TYPE_MIN_VALUE (orig_type);
3760 max_value = TYPE_MAX_VALUE (orig_type);
3761
3762 /* Case label is less than minimum for type. */
3763 if (tree_int_cst_compare (case_low, min_value) < 0
3764 && tree_int_cst_compare (case_high, min_value) < 0)
3765 {
f61a9bc2 3766 warning_at (loc, 0, "case label value is less than minimum value "
3767 "for type");
be23b16f 3768 *outside_range_p = true;
2ca392fd 3769 return false;
3770 }
b27ac6b5 3771
2ca392fd 3772 /* Case value is greater than maximum for type. */
3773 if (tree_int_cst_compare (case_low, max_value) > 0
3774 && tree_int_cst_compare (case_high, max_value) > 0)
3775 {
f61a9bc2 3776 warning_at (loc, 0, "case label value exceeds maximum value for type");
be23b16f 3777 *outside_range_p = true;
2ca392fd 3778 return false;
3779 }
3780
3781 /* Saturate lower case label value to minimum. */
3782 if (tree_int_cst_compare (case_high, min_value) >= 0
3783 && tree_int_cst_compare (case_low, min_value) < 0)
3784 {
f61a9bc2 3785 warning_at (loc, 0, "lower value in case label range"
3786 " less than minimum value for type");
be23b16f 3787 *outside_range_p = true;
2ca392fd 3788 case_low = min_value;
3789 }
b27ac6b5 3790
2ca392fd 3791 /* Saturate upper case label value to maximum. */
3792 if (tree_int_cst_compare (case_low, max_value) <= 0
3793 && tree_int_cst_compare (case_high, max_value) > 0)
3794 {
f61a9bc2 3795 warning_at (loc, 0, "upper value in case label range"
3796 " exceeds maximum value for type");
be23b16f 3797 *outside_range_p = true;
2ca392fd 3798 case_high = max_value;
3799 }
3800
3801 if (*case_low_p != case_low)
3802 *case_low_p = convert (type, case_low);
3803 if (case_high_p && *case_high_p != case_high)
3804 *case_high_p = convert (type, case_high);
3805
3806 return true;
3807}
3808\f
b0fc3e72 3809/* Return an integer type with BITS bits of precision,
3810 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3811
3812tree
1cae46be 3813c_common_type_for_size (unsigned int bits, int unsignedp)
b0fc3e72 3814{
9f75f026 3815 int i;
3816
46375237 3817 if (bits == TYPE_PRECISION (integer_type_node))
3818 return unsignedp ? unsigned_type_node : integer_type_node;
3819
bacde65a 3820 if (bits == TYPE_PRECISION (signed_char_type_node))
b0fc3e72 3821 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3822
bacde65a 3823 if (bits == TYPE_PRECISION (short_integer_type_node))
b0fc3e72 3824 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3825
bacde65a 3826 if (bits == TYPE_PRECISION (long_integer_type_node))
b0fc3e72 3827 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3828
bacde65a 3829 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b0fc3e72 3830 return (unsignedp ? long_long_unsigned_type_node
3831 : long_long_integer_type_node);
3832
9f75f026 3833 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3834 if (int_n_enabled_p[i]
3835 && bits == int_n_data[i].bitsize)
3836 return (unsignedp ? int_n_trees[i].unsigned_type
3837 : int_n_trees[i].signed_type);
6388cfe2 3838
f57fa2ea 3839 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3840 return (unsignedp ? widest_unsigned_literal_type_node
3841 : widest_integer_literal_type_node);
3842
bacde65a 3843 if (bits <= TYPE_PRECISION (intQI_type_node))
3844 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3845
3846 if (bits <= TYPE_PRECISION (intHI_type_node))
3847 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3848
3849 if (bits <= TYPE_PRECISION (intSI_type_node))
3850 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3851
3852 if (bits <= TYPE_PRECISION (intDI_type_node))
3853 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3854
b0fc3e72 3855 return 0;
3856}
3857
9421ebb9 3858/* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3859 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3860 and saturating if SATP is nonzero, otherwise not saturating. */
3861
3862tree
3863c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3864 int unsignedp, int satp)
3865{
3754d046 3866 machine_mode mode;
9421ebb9 3867 if (ibit == 0)
3868 mode = unsignedp ? UQQmode : QQmode;
3869 else
3870 mode = unsignedp ? UHAmode : HAmode;
3871
3872 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3873 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3874 break;
3875
3876 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3877 {
3878 sorry ("GCC cannot support operators with integer types and "
3879 "fixed-point types that have too many integral and "
3880 "fractional bits together");
3881 return 0;
3882 }
3883
3884 return c_common_type_for_mode (mode, satp);
3885}
3886
5b247e9f 3887/* Used for communication between c_common_type_for_mode and
3888 c_register_builtin_type. */
c1917557 3889tree registered_builtin_types;
5b247e9f 3890
b0fc3e72 3891/* Return a data type that has machine mode MODE.
3892 If the mode is an integer,
9421ebb9 3893 then UNSIGNEDP selects between signed and unsigned types.
3894 If the mode is a fixed-point mode,
3895 then UNSIGNEDP selects between saturating and nonsaturating types. */
b0fc3e72 3896
3897tree
3754d046 3898c_common_type_for_mode (machine_mode mode, int unsignedp)
b0fc3e72 3899{
5b247e9f 3900 tree t;
9f75f026 3901 int i;
5b247e9f 3902
46375237 3903 if (mode == TYPE_MODE (integer_type_node))
3904 return unsignedp ? unsigned_type_node : integer_type_node;
3905
b0fc3e72 3906 if (mode == TYPE_MODE (signed_char_type_node))
3907 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3908
3909 if (mode == TYPE_MODE (short_integer_type_node))
3910 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3911
b0fc3e72 3912 if (mode == TYPE_MODE (long_integer_type_node))
3913 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3914
3915 if (mode == TYPE_MODE (long_long_integer_type_node))
3916 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3917
9f75f026 3918 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3919 if (int_n_enabled_p[i]
3920 && mode == int_n_data[i].m)
3921 return (unsignedp ? int_n_trees[i].unsigned_type
3922 : int_n_trees[i].signed_type);
6388cfe2 3923
f57fa2ea 3924 if (mode == TYPE_MODE (widest_integer_literal_type_node))
44e9fa65 3925 return unsignedp ? widest_unsigned_literal_type_node
4ee9c684 3926 : widest_integer_literal_type_node;
f57fa2ea 3927
88ae7f04 3928 if (mode == QImode)
bacde65a 3929 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3930
88ae7f04 3931 if (mode == HImode)
bacde65a 3932 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3933
88ae7f04 3934 if (mode == SImode)
bacde65a 3935 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3936
88ae7f04 3937 if (mode == DImode)
bacde65a 3938 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
ab2c1de8 3939
cc1cc1c7 3940#if HOST_BITS_PER_WIDE_INT >= 64
6274009c 3941 if (mode == TYPE_MODE (intTI_type_node))
3942 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
cc1cc1c7 3943#endif
6274009c 3944
b0fc3e72 3945 if (mode == TYPE_MODE (float_type_node))
3946 return float_type_node;
3947
3948 if (mode == TYPE_MODE (double_type_node))
3949 return double_type_node;
3950
3951 if (mode == TYPE_MODE (long_double_type_node))
3952 return long_double_type_node;
3953
545c2bde 3954 if (mode == TYPE_MODE (void_type_node))
3955 return void_type_node;
b27ac6b5 3956
b0fc3e72 3957 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
61b9b73c 3958 return (unsignedp
3959 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3960 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3961
3962 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
61b9b73c 3963 return (unsignedp
3964 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3965 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3966
0dfc45b5 3967 if (COMPLEX_MODE_P (mode))
3968 {
3754d046 3969 machine_mode inner_mode;
0dfc45b5 3970 tree inner_type;
3971
3972 if (mode == TYPE_MODE (complex_float_type_node))
3973 return complex_float_type_node;
3974 if (mode == TYPE_MODE (complex_double_type_node))
3975 return complex_double_type_node;
3976 if (mode == TYPE_MODE (complex_long_double_type_node))
3977 return complex_long_double_type_node;
3978
3979 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3980 return complex_integer_type_node;
3981
3982 inner_mode = GET_MODE_INNER (mode);
3983 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3984 if (inner_type != NULL_TREE)
3985 return build_complex_type (inner_type);
3986 }
3987 else if (VECTOR_MODE_P (mode))
4917c376 3988 {
3754d046 3989 machine_mode inner_mode = GET_MODE_INNER (mode);
4917c376 3990 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3991 if (inner_type != NULL_TREE)
3992 return build_vector_type_for_mode (inner_type, mode);
88ae7f04 3993 }
e2ea7e3a 3994
c4503c0a 3995 if (mode == TYPE_MODE (dfloat32_type_node))
3996 return dfloat32_type_node;
3997 if (mode == TYPE_MODE (dfloat64_type_node))
3998 return dfloat64_type_node;
3999 if (mode == TYPE_MODE (dfloat128_type_node))
4000 return dfloat128_type_node;
4001
9421ebb9 4002 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
4003 {
4004 if (mode == TYPE_MODE (short_fract_type_node))
4005 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
4006 if (mode == TYPE_MODE (fract_type_node))
4007 return unsignedp ? sat_fract_type_node : fract_type_node;
4008 if (mode == TYPE_MODE (long_fract_type_node))
4009 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
4010 if (mode == TYPE_MODE (long_long_fract_type_node))
4011 return unsignedp ? sat_long_long_fract_type_node
4012 : long_long_fract_type_node;
4013
4014 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
4015 return unsignedp ? sat_unsigned_short_fract_type_node
4016 : unsigned_short_fract_type_node;
4017 if (mode == TYPE_MODE (unsigned_fract_type_node))
4018 return unsignedp ? sat_unsigned_fract_type_node
4019 : unsigned_fract_type_node;
4020 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
4021 return unsignedp ? sat_unsigned_long_fract_type_node
4022 : unsigned_long_fract_type_node;
4023 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
4024 return unsignedp ? sat_unsigned_long_long_fract_type_node
4025 : unsigned_long_long_fract_type_node;
4026
4027 if (mode == TYPE_MODE (short_accum_type_node))
4028 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
4029 if (mode == TYPE_MODE (accum_type_node))
4030 return unsignedp ? sat_accum_type_node : accum_type_node;
4031 if (mode == TYPE_MODE (long_accum_type_node))
4032 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
4033 if (mode == TYPE_MODE (long_long_accum_type_node))
4034 return unsignedp ? sat_long_long_accum_type_node
4035 : long_long_accum_type_node;
4036
4037 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
4038 return unsignedp ? sat_unsigned_short_accum_type_node
4039 : unsigned_short_accum_type_node;
4040 if (mode == TYPE_MODE (unsigned_accum_type_node))
4041 return unsignedp ? sat_unsigned_accum_type_node
4042 : unsigned_accum_type_node;
4043 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
4044 return unsignedp ? sat_unsigned_long_accum_type_node
4045 : unsigned_long_accum_type_node;
4046 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
4047 return unsignedp ? sat_unsigned_long_long_accum_type_node
4048 : unsigned_long_long_accum_type_node;
4049
4050 if (mode == QQmode)
4051 return unsignedp ? sat_qq_type_node : qq_type_node;
4052 if (mode == HQmode)
4053 return unsignedp ? sat_hq_type_node : hq_type_node;
4054 if (mode == SQmode)
4055 return unsignedp ? sat_sq_type_node : sq_type_node;
4056 if (mode == DQmode)
4057 return unsignedp ? sat_dq_type_node : dq_type_node;
4058 if (mode == TQmode)
4059 return unsignedp ? sat_tq_type_node : tq_type_node;
4060
4061 if (mode == UQQmode)
4062 return unsignedp ? sat_uqq_type_node : uqq_type_node;
4063 if (mode == UHQmode)
4064 return unsignedp ? sat_uhq_type_node : uhq_type_node;
4065 if (mode == USQmode)
4066 return unsignedp ? sat_usq_type_node : usq_type_node;
4067 if (mode == UDQmode)
4068 return unsignedp ? sat_udq_type_node : udq_type_node;
4069 if (mode == UTQmode)
4070 return unsignedp ? sat_utq_type_node : utq_type_node;
4071
4072 if (mode == HAmode)
4073 return unsignedp ? sat_ha_type_node : ha_type_node;
4074 if (mode == SAmode)
4075 return unsignedp ? sat_sa_type_node : sa_type_node;
4076 if (mode == DAmode)
4077 return unsignedp ? sat_da_type_node : da_type_node;
4078 if (mode == TAmode)
4079 return unsignedp ? sat_ta_type_node : ta_type_node;
4080
4081 if (mode == UHAmode)
4082 return unsignedp ? sat_uha_type_node : uha_type_node;
4083 if (mode == USAmode)
4084 return unsignedp ? sat_usa_type_node : usa_type_node;
4085 if (mode == UDAmode)
4086 return unsignedp ? sat_uda_type_node : uda_type_node;
4087 if (mode == UTAmode)
4088 return unsignedp ? sat_uta_type_node : uta_type_node;
4089 }
4090
5b247e9f 4091 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
baec58e1 4092 if (TYPE_MODE (TREE_VALUE (t)) == mode
4093 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
5b247e9f 4094 return TREE_VALUE (t);
4095
b0fc3e72 4096 return 0;
4097}
20d39783 4098
11773141 4099tree
4100c_common_unsigned_type (tree type)
4101{
4102 return c_common_signed_or_unsigned_type (1, type);
4103}
4104
20d39783 4105/* Return a signed type the same as TYPE in other respects. */
4106
4107tree
1cae46be 4108c_common_signed_type (tree type)
20d39783 4109{
4070745f 4110 return c_common_signed_or_unsigned_type (0, type);
20d39783 4111}
4112
4113/* Return a type the same as TYPE except unsigned or
4114 signed according to UNSIGNEDP. */
4115
4116tree
1cae46be 4117c_common_signed_or_unsigned_type (int unsignedp, tree type)
20d39783 4118{
7a91101f 4119 tree type1;
9f75f026 4120 int i;
20d39783 4121
7a91101f 4122 /* This block of code emulates the behavior of the old
4123 c_common_unsigned_type. In particular, it returns
4124 long_unsigned_type_node if passed a long, even when a int would
4125 have the same size. This is necessary for warnings to work
4126 correctly in archs where sizeof(int) == sizeof(long) */
4127
4128 type1 = TYPE_MAIN_VARIANT (type);
4129 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
4130 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4131 if (type1 == integer_type_node || type1 == unsigned_type_node)
4132 return unsignedp ? unsigned_type_node : integer_type_node;
4133 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
4134 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4135 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
4136 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4137 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
4138 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
9f75f026 4139
4140 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4141 if (int_n_enabled_p[i]
4142 && (type1 == int_n_trees[i].unsigned_type
4143 || type1 == int_n_trees[i].signed_type))
4144 return (unsignedp ? int_n_trees[i].unsigned_type
4145 : int_n_trees[i].signed_type);
4146
7a91101f 4147 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
4148 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
4149#if HOST_BITS_PER_WIDE_INT >= 64
4150 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
4151 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4152#endif
4153 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
4154 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4155 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
4156 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4157 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
4158 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4159 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
4160 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4161
9f92e1a2 4162#define C_COMMON_FIXED_TYPES(NAME) \
4163 if (type1 == short_ ## NAME ## _type_node \
4164 || type1 == unsigned_short_ ## NAME ## _type_node) \
4165 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
4166 : short_ ## NAME ## _type_node; \
4167 if (type1 == NAME ## _type_node \
4168 || type1 == unsigned_ ## NAME ## _type_node) \
4169 return unsignedp ? unsigned_ ## NAME ## _type_node \
4170 : NAME ## _type_node; \
4171 if (type1 == long_ ## NAME ## _type_node \
4172 || type1 == unsigned_long_ ## NAME ## _type_node) \
4173 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
4174 : long_ ## NAME ## _type_node; \
4175 if (type1 == long_long_ ## NAME ## _type_node \
4176 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
4177 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
4178 : long_long_ ## NAME ## _type_node;
4179
4180#define C_COMMON_FIXED_MODE_TYPES(NAME) \
4181 if (type1 == NAME ## _type_node \
4182 || type1 == u ## NAME ## _type_node) \
4183 return unsignedp ? u ## NAME ## _type_node \
4184 : NAME ## _type_node;
4185
4186#define C_COMMON_FIXED_TYPES_SAT(NAME) \
4187 if (type1 == sat_ ## short_ ## NAME ## _type_node \
4188 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
4189 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
4190 : sat_ ## short_ ## NAME ## _type_node; \
4191 if (type1 == sat_ ## NAME ## _type_node \
4192 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
4193 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
4194 : sat_ ## NAME ## _type_node; \
4195 if (type1 == sat_ ## long_ ## NAME ## _type_node \
4196 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
4197 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
4198 : sat_ ## long_ ## NAME ## _type_node; \
4199 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
4200 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
4201 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
4202 : sat_ ## long_long_ ## NAME ## _type_node;
4203
4204#define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
4205 if (type1 == sat_ ## NAME ## _type_node \
4206 || type1 == sat_ ## u ## NAME ## _type_node) \
4207 return unsignedp ? sat_ ## u ## NAME ## _type_node \
4208 : sat_ ## NAME ## _type_node;
4209
4210 C_COMMON_FIXED_TYPES (fract);
4211 C_COMMON_FIXED_TYPES_SAT (fract);
4212 C_COMMON_FIXED_TYPES (accum);
4213 C_COMMON_FIXED_TYPES_SAT (accum);
4214
4215 C_COMMON_FIXED_MODE_TYPES (qq);
4216 C_COMMON_FIXED_MODE_TYPES (hq);
4217 C_COMMON_FIXED_MODE_TYPES (sq);
4218 C_COMMON_FIXED_MODE_TYPES (dq);
4219 C_COMMON_FIXED_MODE_TYPES (tq);
4220 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
4221 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
4222 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
4223 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
4224 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
4225 C_COMMON_FIXED_MODE_TYPES (ha);
4226 C_COMMON_FIXED_MODE_TYPES (sa);
4227 C_COMMON_FIXED_MODE_TYPES (da);
4228 C_COMMON_FIXED_MODE_TYPES (ta);
4229 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
4230 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
4231 C_COMMON_FIXED_MODE_TYPES_SAT (da);
4232 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
9421ebb9 4233
4f7f7efd 4234 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
4235 the precision; they have precision set to match their range, but
4236 may use a wider mode to match an ABI. If we change modes, we may
4237 wind up with bad conversions. For INTEGER_TYPEs in C, must check
4238 the precision as well, so as to yield correct results for
4239 bit-field types. C++ does not have these separate bit-field
4240 types, and producing a signed or unsigned variant of an
4241 ENUMERAL_TYPE may cause other problems as well. */
4242
ac265864 4243 if (!INTEGRAL_TYPE_P (type)
4244 || TYPE_UNSIGNED (type) == unsignedp)
4245 return type;
4246
4f7f7efd 4247#define TYPE_OK(node) \
4248 (TYPE_MODE (type) == TYPE_MODE (node) \
0c4abe5b 4249 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
4f7f7efd 4250 if (TYPE_OK (signed_char_type_node))
20d39783 4251 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4f7f7efd 4252 if (TYPE_OK (integer_type_node))
20d39783 4253 return unsignedp ? unsigned_type_node : integer_type_node;
4f7f7efd 4254 if (TYPE_OK (short_integer_type_node))
20d39783 4255 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4f7f7efd 4256 if (TYPE_OK (long_integer_type_node))
20d39783 4257 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4f7f7efd 4258 if (TYPE_OK (long_long_integer_type_node))
20d39783 4259 return (unsignedp ? long_long_unsigned_type_node
4260 : long_long_integer_type_node);
9f75f026 4261
4262 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4263 if (int_n_enabled_p[i]
4264 && TYPE_MODE (type) == int_n_data[i].m
4265 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
4266 return (unsignedp ? int_n_trees[i].unsigned_type
4267 : int_n_trees[i].signed_type);
4268
4f7f7efd 4269 if (TYPE_OK (widest_integer_literal_type_node))
20d39783 4270 return (unsignedp ? widest_unsigned_literal_type_node
4271 : widest_integer_literal_type_node);
ef11801e 4272
4273#if HOST_BITS_PER_WIDE_INT >= 64
4f7f7efd 4274 if (TYPE_OK (intTI_type_node))
ef11801e 4275 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4276#endif
4f7f7efd 4277 if (TYPE_OK (intDI_type_node))
ef11801e 4278 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4f7f7efd 4279 if (TYPE_OK (intSI_type_node))
ef11801e 4280 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4f7f7efd 4281 if (TYPE_OK (intHI_type_node))
ef11801e 4282 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4f7f7efd 4283 if (TYPE_OK (intQI_type_node))
ef11801e 4284 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4f7f7efd 4285#undef TYPE_OK
ef11801e 4286
0c4abe5b 4287 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
20d39783 4288}
b268e47e 4289
c0e47fd4 4290/* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
4291
4292tree
4293c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
4294{
9f75f026 4295 int i;
4296
c0e47fd4 4297 /* Extended integer types of the same width as a standard type have
4298 lesser rank, so those of the same width as int promote to int or
4299 unsigned int and are valid for printf formats expecting int or
4300 unsigned int. To avoid such special cases, avoid creating
4301 extended integer types for bit-fields if a standard integer type
4302 is available. */
4303 if (width == TYPE_PRECISION (integer_type_node))
4304 return unsignedp ? unsigned_type_node : integer_type_node;
4305 if (width == TYPE_PRECISION (signed_char_type_node))
4306 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4307 if (width == TYPE_PRECISION (short_integer_type_node))
4308 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4309 if (width == TYPE_PRECISION (long_integer_type_node))
4310 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4311 if (width == TYPE_PRECISION (long_long_integer_type_node))
4312 return (unsignedp ? long_long_unsigned_type_node
4313 : long_long_integer_type_node);
9f75f026 4314 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4315 if (int_n_enabled_p[i]
4316 && width == int_n_data[i].bitsize)
4317 return (unsignedp ? int_n_trees[i].unsigned_type
4318 : int_n_trees[i].signed_type);
c0e47fd4 4319 return build_nonstandard_integer_type (width, unsignedp);
4320}
4321
b268e47e 4322/* The C version of the register_builtin_type langhook. */
4323
4324void
4325c_register_builtin_type (tree type, const char* name)
4326{
4327 tree decl;
4328
e60a6f7b 4329 decl = build_decl (UNKNOWN_LOCATION,
4330 TYPE_DECL, get_identifier (name), type);
b268e47e 4331 DECL_ARTIFICIAL (decl) = 1;
4332 if (!TYPE_NAME (type))
4333 TYPE_NAME (type) = decl;
4334 pushdecl (decl);
5b247e9f 4335
4336 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
b268e47e 4337}
a9b9d10c 4338\f
aff9e656 4339/* Print an error message for invalid operands to arith operation
8e70fb09 4340 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4341 LOCATION is the location of the message. */
b0fc3e72 4342
4343void
8e70fb09 4344binary_op_error (location_t location, enum tree_code code,
4345 tree type0, tree type1)
b0fc3e72 4346{
19cb6b50 4347 const char *opname;
f03946e4 4348
b0fc3e72 4349 switch (code)
4350 {
b0fc3e72 4351 case PLUS_EXPR:
4352 opname = "+"; break;
4353 case MINUS_EXPR:
4354 opname = "-"; break;
4355 case MULT_EXPR:
4356 opname = "*"; break;
4357 case MAX_EXPR:
4358 opname = "max"; break;
4359 case MIN_EXPR:
4360 opname = "min"; break;
4361 case EQ_EXPR:
4362 opname = "=="; break;
4363 case NE_EXPR:
4364 opname = "!="; break;
4365 case LE_EXPR:
4366 opname = "<="; break;
4367 case GE_EXPR:
4368 opname = ">="; break;
4369 case LT_EXPR:
4370 opname = "<"; break;
4371 case GT_EXPR:
4372 opname = ">"; break;
4373 case LSHIFT_EXPR:
4374 opname = "<<"; break;
4375 case RSHIFT_EXPR:
4376 opname = ">>"; break;
4377 case TRUNC_MOD_EXPR:
66618a1e 4378 case FLOOR_MOD_EXPR:
b0fc3e72 4379 opname = "%"; break;
4380 case TRUNC_DIV_EXPR:
66618a1e 4381 case FLOOR_DIV_EXPR:
b0fc3e72 4382 opname = "/"; break;
4383 case BIT_AND_EXPR:
4384 opname = "&"; break;
4385 case BIT_IOR_EXPR:
4386 opname = "|"; break;
4387 case TRUTH_ANDIF_EXPR:
4388 opname = "&&"; break;
4389 case TRUTH_ORIF_EXPR:
4390 opname = "||"; break;
4391 case BIT_XOR_EXPR:
4392 opname = "^"; break;
31f820d2 4393 default:
315ba355 4394 gcc_unreachable ();
b0fc3e72 4395 }
8e70fb09 4396 error_at (location,
4397 "invalid operands to binary %s (have %qT and %qT)", opname,
4398 type0, type1);
b0fc3e72 4399}
4400\f
03fe1dc2 4401/* Given an expression as a tree, return its original type. Do this
4402 by stripping any conversion that preserves the sign and precision. */
4403static tree
4404expr_original_type (tree expr)
4405{
4406 STRIP_SIGN_NOPS (expr);
4407 return TREE_TYPE (expr);
4408}
4409
b0fc3e72 4410/* Subroutine of build_binary_op, used for comparison operations.
4411 See if the operands have both been converted from subword integer types
4412 and, if so, perhaps change them both back to their original type.
5b511807 4413 This function is also responsible for converting the two operands
4414 to the proper common type for comparison.
b0fc3e72 4415
4416 The arguments of this function are all pointers to local variables
4417 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4418 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4419
2623625f 4420 LOC is the location of the comparison.
4421
b0fc3e72 4422 If this function returns nonzero, it means that the comparison has
4423 a constant value. What this function returns is an expression for
4424 that value. */
4425
4426tree
2623625f 4427shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4428 tree *restype_ptr, enum tree_code *rescode_ptr)
b0fc3e72 4429{
19cb6b50 4430 tree type;
b0fc3e72 4431 tree op0 = *op0_ptr;
4432 tree op1 = *op1_ptr;
4433 int unsignedp0, unsignedp1;
4434 int real1, real2;
4435 tree primop0, primop1;
4436 enum tree_code code = *rescode_ptr;
4437
4438 /* Throw away any conversions to wider types
4439 already present in the operands. */
4440
7f506bca 4441 primop0 = c_common_get_narrower (op0, &unsignedp0);
4442 primop1 = c_common_get_narrower (op1, &unsignedp1);
b0fc3e72 4443
119d06b2 4444 /* If primopN is first sign-extended from primopN's precision to opN's
4445 precision, then zero-extended from opN's precision to
4446 *restype_ptr precision, shortenings might be invalid. */
4447 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4448 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4449 && !unsignedp0
4450 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4451 primop0 = op0;
4452 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4453 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4454 && !unsignedp1
4455 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4456 primop1 = op1;
4457
b0fc3e72 4458 /* Handle the case that OP0 does not *contain* a conversion
4459 but it *requires* conversion to FINAL_TYPE. */
4460
4461 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
78a8ed03 4462 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
b0fc3e72 4463 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
78a8ed03 4464 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b0fc3e72 4465
4466 /* If one of the operands must be floated, we cannot optimize. */
4467 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4468 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
ab2c1de8 4469
b0fc3e72 4470 /* If first arg is constant, swap the args (changing operation
2bd278cc 4471 so value is preserved), for canonicalization. Don't do this if
4472 the second arg is 0. */
b0fc3e72 4473
2bd278cc 4474 if (TREE_CONSTANT (primop0)
9421ebb9 4475 && !integer_zerop (primop1) && !real_zerop (primop1)
4476 && !fixed_zerop (primop1))
b0fc3e72 4477 {
dfcf26a5 4478 std::swap (primop0, primop1);
4479 std::swap (op0, op1);
b0fc3e72 4480 *op0_ptr = op0;
4481 *op1_ptr = op1;
dfcf26a5 4482 std::swap (unsignedp0, unsignedp1);
4483 std::swap (real1, real2);
b0fc3e72 4484
4485 switch (code)
4486 {
4487 case LT_EXPR:
4488 code = GT_EXPR;
4489 break;
4490 case GT_EXPR:
4491 code = LT_EXPR;
4492 break;
4493 case LE_EXPR:
4494 code = GE_EXPR;
4495 break;
4496 case GE_EXPR:
4497 code = LE_EXPR;
4498 break;
31f820d2 4499 default:
4500 break;
b0fc3e72 4501 }
4502 *rescode_ptr = code;
4503 }
4504
4505 /* If comparing an integer against a constant more bits wide,
4506 maybe we can deduce a value of 1 or 0 independent of the data.
4507 Or else truncate the constant now
4508 rather than extend the variable at run time.
4509
4510 This is only interesting if the constant is the wider arg.
4511 Also, it is not safe if the constant is unsigned and the
4512 variable arg is signed, since in this case the variable
4513 would be sign-extended and then regarded as unsigned.
4514 Our technique fails in this case because the lowest/highest
4515 possible unsigned results don't follow naturally from the
4516 lowest/highest possible values of the variable operand.
4517 For just EQ_EXPR and NE_EXPR there is another technique that
4518 could be used: see if the constant can be faithfully represented
4519 in the other operand's type, by truncating it and reextending it
4520 and see if that preserves the constant's value. */
4521
4522 if (!real1 && !real2
9421ebb9 4523 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
b0fc3e72 4524 && TREE_CODE (primop1) == INTEGER_CST
4525 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4526 {
4527 int min_gt, max_gt, min_lt, max_lt;
4528 tree maxval, minval;
4529 /* 1 if comparison is nominally unsigned. */
78a8ed03 4530 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
b0fc3e72 4531 tree val;
4532
4070745f 4533 type = c_common_signed_or_unsigned_type (unsignedp0,
4534 TREE_TYPE (primop0));
cda09c61 4535
b0fc3e72 4536 maxval = TYPE_MAX_VALUE (type);
4537 minval = TYPE_MIN_VALUE (type);
4538
4539 if (unsignedp && !unsignedp0)
4070745f 4540 *restype_ptr = c_common_signed_type (*restype_ptr);
b0fc3e72 4541
4542 if (TREE_TYPE (primop1) != *restype_ptr)
18dbec6f 4543 {
9a5e8086 4544 /* Convert primop1 to target type, but do not introduce
4545 additional overflow. We know primop1 is an int_cst. */
e913b5cd 4546 primop1 = force_fit_type (*restype_ptr,
796b6678 4547 wide_int::from
4548 (primop1,
4549 TYPE_PRECISION (*restype_ptr),
4550 TYPE_SIGN (TREE_TYPE (primop1))),
e913b5cd 4551 0, TREE_OVERFLOW (primop1));
18dbec6f 4552 }
b0fc3e72 4553 if (type != *restype_ptr)
4554 {
4555 minval = convert (*restype_ptr, minval);
4556 maxval = convert (*restype_ptr, maxval);
4557 }
4558
d99d10ca 4559 min_gt = tree_int_cst_lt (primop1, minval);
4560 max_gt = tree_int_cst_lt (primop1, maxval);
4561 min_lt = tree_int_cst_lt (minval, primop1);
4562 max_lt = tree_int_cst_lt (maxval, primop1);
b0fc3e72 4563
4564 val = 0;
4565 /* This used to be a switch, but Genix compiler can't handle that. */
4566 if (code == NE_EXPR)
4567 {
4568 if (max_lt || min_gt)
3c2239cf 4569 val = truthvalue_true_node;
b0fc3e72 4570 }
4571 else if (code == EQ_EXPR)
4572 {
4573 if (max_lt || min_gt)
3c2239cf 4574 val = truthvalue_false_node;
b0fc3e72 4575 }
4576 else if (code == LT_EXPR)
4577 {
4578 if (max_lt)
3c2239cf 4579 val = truthvalue_true_node;
b0fc3e72 4580 if (!min_lt)
3c2239cf 4581 val = truthvalue_false_node;
b0fc3e72 4582 }
4583 else if (code == GT_EXPR)
4584 {
4585 if (min_gt)
3c2239cf 4586 val = truthvalue_true_node;
b0fc3e72 4587 if (!max_gt)
3c2239cf 4588 val = truthvalue_false_node;
b0fc3e72 4589 }
4590 else if (code == LE_EXPR)
4591 {
4592 if (!max_gt)
3c2239cf 4593 val = truthvalue_true_node;
b0fc3e72 4594 if (min_gt)
3c2239cf 4595 val = truthvalue_false_node;
b0fc3e72 4596 }
4597 else if (code == GE_EXPR)
4598 {
4599 if (!min_lt)
3c2239cf 4600 val = truthvalue_true_node;
b0fc3e72 4601 if (max_lt)
3c2239cf 4602 val = truthvalue_false_node;
b0fc3e72 4603 }
4604
4605 /* If primop0 was sign-extended and unsigned comparison specd,
4606 we did a signed comparison above using the signed type bounds.
4607 But the comparison we output must be unsigned.
4608
4609 Also, for inequalities, VAL is no good; but if the signed
4610 comparison had *any* fixed result, it follows that the
4611 unsigned comparison just tests the sign in reverse
4612 (positive values are LE, negative ones GE).
4613 So we can generate an unsigned comparison
4614 against an extreme value of the signed type. */
4615
4616 if (unsignedp && !unsignedp0)
4617 {
4618 if (val != 0)
4619 switch (code)
4620 {
4621 case LT_EXPR:
4622 case GE_EXPR:
4623 primop1 = TYPE_MIN_VALUE (type);
4624 val = 0;
4625 break;
4626
4627 case LE_EXPR:
4628 case GT_EXPR:
4629 primop1 = TYPE_MAX_VALUE (type);
4630 val = 0;
4631 break;
31f820d2 4632
4633 default:
4634 break;
b0fc3e72 4635 }
11773141 4636 type = c_common_unsigned_type (type);
b0fc3e72 4637 }
4638
734ec290 4639 if (TREE_CODE (primop0) != INTEGER_CST)
b0fc3e72 4640 {
3c2239cf 4641 if (val == truthvalue_false_node)
03fe1dc2 4642 warning_at (loc, OPT_Wtype_limits,
4643 "comparison is always false due to limited range of data type");
3c2239cf 4644 if (val == truthvalue_true_node)
03fe1dc2 4645 warning_at (loc, OPT_Wtype_limits,
4646 "comparison is always true due to limited range of data type");
b0fc3e72 4647 }
4648
4649 if (val != 0)
4650 {
4651 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4652 if (TREE_SIDE_EFFECTS (primop0))
14ae0310 4653 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
b0fc3e72 4654 return val;
4655 }
4656
4657 /* Value is not predetermined, but do the comparison
4658 in the type of the operand that is not constant.
4659 TYPE is already properly set. */
4660 }
c4503c0a 4661
4662 /* If either arg is decimal float and the other is float, find the
4663 proper common type to use for comparison. */
7fd22aae 4664 else if (real1 && real2
4665 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4666 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4667 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4668
4669 /* If either arg is decimal float and the other is float, fail. */
c4503c0a 4670 else if (real1 && real2
4671 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4672 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
7fd22aae 4673 return 0;
c4503c0a 4674
b0fc3e72 4675 else if (real1 && real2
2203bd5c 4676 && (TYPE_PRECISION (TREE_TYPE (primop0))
4677 == TYPE_PRECISION (TREE_TYPE (primop1))))
b0fc3e72 4678 type = TREE_TYPE (primop0);
4679
4680 /* If args' natural types are both narrower than nominal type
4681 and both extend in the same manner, compare them
4682 in the type of the wider arg.
4683 Otherwise must actually extend both to the nominal
4684 common type lest different ways of extending
4685 alter the result.
4686 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4687
4688 else if (unsignedp0 == unsignedp1 && real1 == real2
4689 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4690 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4691 {
4692 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4070745f 4693 type = c_common_signed_or_unsigned_type (unsignedp0
78a8ed03 4694 || TYPE_UNSIGNED (*restype_ptr),
4070745f 4695 type);
b0fc3e72 4696 /* Make sure shorter operand is extended the right way
4697 to match the longer operand. */
4070745f 4698 primop0
4699 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4700 TREE_TYPE (primop0)),
4701 primop0);
4702 primop1
4703 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4704 TREE_TYPE (primop1)),
4705 primop1);
b0fc3e72 4706 }
4707 else
4708 {
4709 /* Here we must do the comparison on the nominal type
4710 using the args exactly as we received them. */
4711 type = *restype_ptr;
4712 primop0 = op0;
4713 primop1 = op1;
4714
4715 if (!real1 && !real2 && integer_zerop (primop1)
78a8ed03 4716 && TYPE_UNSIGNED (*restype_ptr))
b0fc3e72 4717 {
4718 tree value = 0;
03fe1dc2 4719 /* All unsigned values are >= 0, so we warn. However,
4720 if OP0 is a constant that is >= 0, the signedness of
4721 the comparison isn't an issue, so suppress the
4722 warning. */
4723 bool warn =
3df42822 4724 warn_type_limits && !in_system_header_at (loc)
03fe1dc2 4725 && !(TREE_CODE (primop0) == INTEGER_CST
4726 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4727 primop0)))
4728 /* Do not warn for enumeration types. */
4729 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4730
b0fc3e72 4731 switch (code)
4732 {
4733 case GE_EXPR:
03fe1dc2 4734 if (warn)
4735 warning_at (loc, OPT_Wtype_limits,
4736 "comparison of unsigned expression >= 0 is always true");
3c2239cf 4737 value = truthvalue_true_node;
b0fc3e72 4738 break;
4739
4740 case LT_EXPR:
03fe1dc2 4741 if (warn)
4742 warning_at (loc, OPT_Wtype_limits,
4743 "comparison of unsigned expression < 0 is always false");
3c2239cf 4744 value = truthvalue_false_node;
31f820d2 4745 break;
4746
4747 default:
4748 break;
b0fc3e72 4749 }
4750
4751 if (value != 0)
4752 {
4753 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4754 if (TREE_SIDE_EFFECTS (primop0))
14ae0310 4755 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4756 primop0, value);
b0fc3e72 4757 return value;
4758 }
4759 }
4760 }
4761
4762 *op0_ptr = convert (type, primop0);
4763 *op1_ptr = convert (type, primop1);
4764
3c2239cf 4765 *restype_ptr = truthvalue_type_node;
b0fc3e72 4766
4767 return 0;
4768}
4769\f
1c26100f 4770/* Return a tree for the sum or difference (RESULTCODE says which)
4771 of pointer PTROP and integer INTOP. */
4772
4773tree
389dd41b 4774pointer_int_sum (location_t loc, enum tree_code resultcode,
42f9a786 4775 tree ptrop, tree intop, bool complain)
1c26100f 4776{
add6ee5e 4777 tree size_exp, ret;
1c26100f 4778
1c26100f 4779 /* The result is a pointer of the same type that is being added. */
1c26100f 4780 tree result_type = TREE_TYPE (ptrop);
4781
4782 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4783 {
42f9a786 4784 if (complain && warn_pointer_arith)
4785 pedwarn (loc, OPT_Wpointer_arith,
4786 "pointer of type %<void *%> used in arithmetic");
4787 else if (!complain)
4788 return error_mark_node;
1c26100f 4789 size_exp = integer_one_node;
4790 }
4791 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4792 {
42f9a786 4793 if (complain && warn_pointer_arith)
4794 pedwarn (loc, OPT_Wpointer_arith,
4795 "pointer to a function used in arithmetic");
4796 else if (!complain)
4797 return error_mark_node;
1c26100f 4798 size_exp = integer_one_node;
4799 }
1c26100f 4800 else
4801 size_exp = size_in_bytes (TREE_TYPE (result_type));
4802
add6ee5e 4803 /* We are manipulating pointer values, so we don't need to warn
4804 about relying on undefined signed overflow. We disable the
4805 warning here because we use integer types so fold won't know that
4806 they are really pointers. */
4807 fold_defer_overflow_warnings ();
4808
1c26100f 4809 /* If what we are about to multiply by the size of the elements
4810 contains a constant term, apply distributive law
4811 and multiply that constant term separately.
4812 This helps produce common subexpressions. */
1c26100f 4813 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
84166705 4814 && !TREE_CONSTANT (intop)
1c26100f 4815 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4816 && TREE_CONSTANT (size_exp)
4817 /* If the constant comes from pointer subtraction,
4818 skip this optimization--it would cause an error. */
4819 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4820 /* If the constant is unsigned, and smaller than the pointer size,
4821 then we must skip this optimization. This is because it could cause
4822 an overflow error if the constant is negative but INTOP is not. */
84166705 4823 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
1c26100f 4824 || (TYPE_PRECISION (TREE_TYPE (intop))
4825 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4826 {
4827 enum tree_code subcode = resultcode;
4828 tree int_type = TREE_TYPE (intop);
4829 if (TREE_CODE (intop) == MINUS_EXPR)
4830 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4831 /* Convert both subexpression types to the type of intop,
4832 because weird cases involving pointer arithmetic
4833 can result in a sum or difference with different type args. */
8e70fb09 4834 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4835 subcode, ptrop,
1c26100f 4836 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4837 intop = convert (int_type, TREE_OPERAND (intop, 0));
4838 }
4839
4840 /* Convert the integer argument to a type the same size as sizetype
4841 so the multiply won't overflow spuriously. */
1c26100f 4842 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
78a8ed03 4843 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
1cae46be 4844 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
78a8ed03 4845 TYPE_UNSIGNED (sizetype)), intop);
1c26100f 4846
4847 /* Replace the integer argument with a suitable product by the object size.
c6feb9f1 4848 Do this multiplication as signed, then convert to the appropriate type
8032877c 4849 for the pointer operation and disregard an overflow that occurred only
c6feb9f1 4850 because of the sign-extension change in the latter conversion. */
4851 {
4852 tree t = build_binary_op (loc,
4853 MULT_EXPR, intop,
4854 convert (TREE_TYPE (intop), size_exp), 1);
4855 intop = convert (sizetype, t);
4856 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
e913b5cd 4857 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
c6feb9f1 4858 }
0de36bdb 4859
499e523f 4860 /* Create the sum or difference. */
0de36bdb 4861 if (resultcode == MINUS_EXPR)
389dd41b 4862 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
1c26100f 4863
2cc66f2a 4864 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
add6ee5e 4865
4866 fold_undefer_and_ignore_overflow_warnings ();
4867
4868 return ret;
1c26100f 4869}
4870\f
f59e3889 4871/* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4872 and if NON_CONST is known not to be permitted in an evaluated part
4873 of a constant expression. */
4874
4875tree
4876c_wrap_maybe_const (tree expr, bool non_const)
4877{
4878 bool nowarning = TREE_NO_WARNING (expr);
4879 location_t loc = EXPR_LOCATION (expr);
4880
4881 /* This should never be called for C++. */
4882 if (c_dialect_cxx ())
4883 gcc_unreachable ();
4884
4885 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4886 STRIP_TYPE_NOPS (expr);
4887 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4888 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4889 if (nowarning)
4890 TREE_NO_WARNING (expr) = 1;
4891 protected_set_expr_location (expr, loc);
4892
4893 return expr;
4894}
4895
a75b1c71 4896/* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4897 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4898 around the SAVE_EXPR if needed so that c_fully_fold does not need
4899 to look inside SAVE_EXPRs. */
4900
4901tree
4902c_save_expr (tree expr)
4903{
4904 bool maybe_const = true;
4905 if (c_dialect_cxx ())
4906 return save_expr (expr);
4907 expr = c_fully_fold (expr, false, &maybe_const);
4908 expr = save_expr (expr);
4909 if (!maybe_const)
f59e3889 4910 expr = c_wrap_maybe_const (expr, true);
a75b1c71 4911 return expr;
4912}
4913
6b68e71a 4914/* Return whether EXPR is a declaration whose address can never be
4915 NULL. */
4916
4917bool
9f627b1a 4918decl_with_nonnull_addr_p (const_tree expr)
6b68e71a 4919{
4920 return (DECL_P (expr)
4921 && (TREE_CODE (expr) == PARM_DECL
4922 || TREE_CODE (expr) == LABEL_DECL
4923 || !DECL_WEAK (expr)));
4924}
4925
b0fc3e72 4926/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
45a78cc0 4927 or for an `if' or `while' statement or ?..: exp. It should already
4928 have been validated to be of suitable type; otherwise, a bad
4929 diagnostic may result.
b0fc3e72 4930
8e70fb09 4931 The EXPR is located at LOCATION.
4932
b0fc3e72 4933 This preparation consists of taking the ordinary
4934 representation of an expression expr and producing a valid tree
4935 boolean expression describing whether expr is nonzero. We could
3c2239cf 4936 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
b0fc3e72 4937 but we optimize comparisons, &&, ||, and !.
4938
3c2239cf 4939 The resulting type should always be `truthvalue_type_node'. */
b0fc3e72 4940
4941tree
8e70fb09 4942c_common_truthvalue_conversion (location_t location, tree expr)
b0fc3e72 4943{
b0fc3e72 4944 switch (TREE_CODE (expr))
4945 {
318a728f 4946 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
e1232ce2 4947 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4948 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4949 case ORDERED_EXPR: case UNORDERED_EXPR:
ce04dcdc 4950 if (TREE_TYPE (expr) == truthvalue_type_node)
4951 return expr;
e60a6f7b 4952 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
ce04dcdc 4953 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
e60a6f7b 4954 goto ret;
ce04dcdc 4955
b0fc3e72 4956 case TRUTH_ANDIF_EXPR:
4957 case TRUTH_ORIF_EXPR:
4958 case TRUTH_AND_EXPR:
4959 case TRUTH_OR_EXPR:
31f6e93c 4960 case TRUTH_XOR_EXPR:
ce04dcdc 4961 if (TREE_TYPE (expr) == truthvalue_type_node)
4962 return expr;
e60a6f7b 4963 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
48e1416a 4964 c_common_truthvalue_conversion (location,
e60a6f7b 4965 TREE_OPERAND (expr, 0)),
4966 c_common_truthvalue_conversion (location,
4967 TREE_OPERAND (expr, 1)));
4968 goto ret;
3e851b85 4969
f6e28f72 4970 case TRUTH_NOT_EXPR:
ce04dcdc 4971 if (TREE_TYPE (expr) == truthvalue_type_node)
4972 return expr;
e60a6f7b 4973 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4974 c_common_truthvalue_conversion (location,
4975 TREE_OPERAND (expr, 0)));
4976 goto ret;
f6e28f72 4977
b0fc3e72 4978 case ERROR_MARK:
4979 return expr;
ab2c1de8 4980
b0fc3e72 4981 case INTEGER_CST:
eddad94a 4982 return integer_zerop (expr) ? truthvalue_false_node
4983 : truthvalue_true_node;
b0fc3e72 4984
4985 case REAL_CST:
5000e21c 4986 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4987 ? truthvalue_true_node
4988 : truthvalue_false_node;
b0fc3e72 4989
9421ebb9 4990 case FIXED_CST:
4991 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4992 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4993 ? truthvalue_true_node
4994 : truthvalue_false_node;
4995
ce04dcdc 4996 case FUNCTION_DECL:
b6889cb0 4997 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
ce04dcdc 4998 /* Fall through. */
4999
b0fc3e72 5000 case ADDR_EXPR:
ce871053 5001 {
ee7d4d6a 5002 tree inner = TREE_OPERAND (expr, 0);
6b68e71a 5003 if (decl_with_nonnull_addr_p (inner))
ce871053 5004 {
6b68e71a 5005 /* Common Ada/Pascal programmer's mistake. */
8e70fb09 5006 warning_at (location,
5007 OPT_Waddress,
5008 "the address of %qD will always evaluate as %<true%>",
5009 inner);
ce871053 5010 return truthvalue_true_node;
5011 }
d473d901 5012 break;
ce871053 5013 }
b0fc3e72 5014
2203bd5c 5015 case COMPLEX_EXPR:
e60a6f7b 5016 expr = build_binary_op (EXPR_LOCATION (expr),
8e70fb09 5017 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
95809de4 5018 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
8e70fb09 5019 c_common_truthvalue_conversion (location,
5020 TREE_OPERAND (expr, 0)),
5021 c_common_truthvalue_conversion (location,
5022 TREE_OPERAND (expr, 1)),
2203bd5c 5023 0);
e60a6f7b 5024 goto ret;
2203bd5c 5025
b0fc3e72 5026 case NEGATE_EXPR:
5027 case ABS_EXPR:
5028 case FLOAT_EXPR:
c6418a4e 5029 case EXCESS_PRECISION_EXPR:
d10cfa8d 5030 /* These don't change whether an object is nonzero or zero. */
8e70fb09 5031 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
b0fc3e72 5032
5033 case LROTATE_EXPR:
5034 case RROTATE_EXPR:
d10cfa8d 5035 /* These don't change whether an object is zero or nonzero, but
b0fc3e72 5036 we can't ignore them if their second arg has side-effects. */
5037 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
e60a6f7b 5038 {
5039 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
5040 TREE_OPERAND (expr, 1),
48e1416a 5041 c_common_truthvalue_conversion
e60a6f7b 5042 (location, TREE_OPERAND (expr, 0)));
5043 goto ret;
5044 }
b0fc3e72 5045 else
8e70fb09 5046 return c_common_truthvalue_conversion (location,
5047 TREE_OPERAND (expr, 0));
73be5127 5048
b0fc3e72 5049 case COND_EXPR:
5050 /* Distribute the conversion into the arms of a COND_EXPR. */
a75b1c71 5051 if (c_dialect_cxx ())
e60a6f7b 5052 {
d0389adc 5053 tree op1 = TREE_OPERAND (expr, 1);
5054 tree op2 = TREE_OPERAND (expr, 2);
5055 /* In C++ one of the arms might have void type if it is throw. */
5056 if (!VOID_TYPE_P (TREE_TYPE (op1)))
5057 op1 = c_common_truthvalue_conversion (location, op1);
5058 if (!VOID_TYPE_P (TREE_TYPE (op2)))
5059 op2 = c_common_truthvalue_conversion (location, op2);
389dd41b 5060 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
d0389adc 5061 TREE_OPERAND (expr, 0), op1, op2);
e60a6f7b 5062 goto ret;
5063 }
a75b1c71 5064 else
e60a6f7b 5065 {
5066 /* Folding will happen later for C. */
5067 expr = build3 (COND_EXPR, truthvalue_type_node,
5068 TREE_OPERAND (expr, 0),
5069 c_common_truthvalue_conversion (location,
5070 TREE_OPERAND (expr, 1)),
5071 c_common_truthvalue_conversion (location,
5072 TREE_OPERAND (expr, 2)));
5073 goto ret;
5074 }
b0fc3e72 5075
72dd6141 5076 CASE_CONVERT:
f8913d47 5077 {
5078 tree totype = TREE_TYPE (expr);
5079 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
5080
db3d1ffc 5081 if (POINTER_TYPE_P (totype)
5082 && TREE_CODE (fromtype) == REFERENCE_TYPE)
5083 {
5084 tree inner = expr;
5085 STRIP_NOPS (inner);
5086
5087 if (DECL_P (inner))
5088 warning_at (location,
5089 OPT_Waddress,
5090 "the compiler can assume that the address of "
5091 "%qD will always evaluate to %<true%>",
5092 inner);
5093 }
5094
f8913d47 5095 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
5096 since that affects how `default_conversion' will behave. */
5097 if (TREE_CODE (totype) == REFERENCE_TYPE
5098 || TREE_CODE (fromtype) == REFERENCE_TYPE)
5099 break;
5100 /* Don't strip a conversion from C++0x scoped enum, since they
5101 don't implicitly convert to other types. */
5102 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
5103 && ENUM_IS_SCOPED (fromtype))
5104 break;
5105 /* If this isn't narrowing the argument, we can ignore it. */
5106 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
5107 return c_common_truthvalue_conversion (location,
5108 TREE_OPERAND (expr, 0));
5109 }
b0fc3e72 5110 break;
5111
16837b18 5112 case MODIFY_EXPR:
60a0513e 5113 if (!TREE_NO_WARNING (expr)
5114 && warn_parentheses)
5115 {
5116 warning (OPT_Wparentheses,
5117 "suggest parentheses around assignment used as truth value");
5118 TREE_NO_WARNING (expr) = 1;
5119 }
16837b18 5120 break;
73be5127 5121
31f820d2 5122 default:
5123 break;
b0fc3e72 5124 }
5125
2ba726d2 5126 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
a0748b7d 5127 {
93be21c0 5128 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
e60a6f7b 5129 expr = (build_binary_op
8e70fb09 5130 (EXPR_LOCATION (expr),
5131 (TREE_SIDE_EFFECTS (expr)
a0748b7d 5132 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
b6889cb0 5133 c_common_truthvalue_conversion
5134 (location,
5135 build_unary_op (location, REALPART_EXPR, t, 0)),
5136 c_common_truthvalue_conversion
5137 (location,
5138 build_unary_op (location, IMAGPART_EXPR, t, 0)),
a0748b7d 5139 0));
e60a6f7b 5140 goto ret;
a0748b7d 5141 }
2ba726d2 5142
9421ebb9 5143 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
5144 {
5145 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
5146 FCONST0 (TYPE_MODE
5147 (TREE_TYPE (expr))));
43158006 5148 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
9421ebb9 5149 }
e60a6f7b 5150 else
5151 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
9421ebb9 5152
e60a6f7b 5153 ret:
5154 protected_set_expr_location (expr, location);
5155 return expr;
b0fc3e72 5156}
5157\f
3237155d 5158static void def_builtin_1 (enum built_in_function fncode,
5159 const char *name,
5160 enum built_in_class fnclass,
5161 tree fntype, tree libtype,
5162 bool both_p, bool fallback_p, bool nonansi_p,
5163 tree fnattrs, bool implicit_p);
0d4238dc 5164
a5b1863e 5165
5166/* Apply the TYPE_QUALS to the new DECL. */
5167
5168void
1cae46be 5169c_apply_type_quals_to_decl (int type_quals, tree decl)
a5b1863e 5170{
adfb367f 5171 tree type = TREE_TYPE (decl);
b27ac6b5 5172
e4eabbe4 5173 if (type == error_mark_node)
5174 return;
adfb367f 5175
98a33d9f 5176 if ((type_quals & TYPE_QUAL_CONST)
5177 || (type && TREE_CODE (type) == REFERENCE_TYPE))
5178 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
5179 constructor can produce constant init, so rely on cp_finish_decl to
5180 clear TREE_READONLY if the variable has non-constant init. */
a5b1863e 5181 TREE_READONLY (decl) = 1;
5182 if (type_quals & TYPE_QUAL_VOLATILE)
5183 {
5184 TREE_SIDE_EFFECTS (decl) = 1;
5185 TREE_THIS_VOLATILE (decl) = 1;
5186 }
d91a20bc 5187 if (type_quals & TYPE_QUAL_RESTRICT)
a5b1863e 5188 {
adfb367f 5189 while (type && TREE_CODE (type) == ARRAY_TYPE)
5190 /* Allow 'restrict' on arrays of pointers.
5191 FIXME currently we just ignore it. */
5192 type = TREE_TYPE (type);
5193 if (!type
5194 || !POINTER_TYPE_P (type)
5195 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
b0b1af64 5196 error ("invalid use of %<restrict%>");
a5b1863e 5197 }
5198}
5199
b594087e 5200struct c_type_hasher : ggc_ptr_hash<tree_node>
2ef51f0e 5201{
5202 static hashval_t hash (tree);
5203 static bool equal (tree, tree);
5204};
5205
4ee9c684 5206/* Hash function for the problem of multiple type definitions in
5207 different files. This must hash all types that will compare
5208 equal via comptypes to the same value. In practice it hashes
0bed3869 5209 on some of the simple stuff and leaves the details to comptypes. */
4ee9c684 5210
2ef51f0e 5211hashval_t
5212c_type_hasher::hash (tree t)
4ee9c684 5213{
ecf2703d 5214 int n_elements;
4ee9c684 5215 int shift, size;
4ee9c684 5216 tree t2;
5217 switch (TREE_CODE (t))
5218 {
fbf0afd1 5219 /* For pointers, hash on pointee type plus some swizzling. */
2363ef00 5220 case POINTER_TYPE:
2ef51f0e 5221 return hash (TREE_TYPE (t)) ^ 0x3003003;
2363ef00 5222 /* Hash on number of elements and total size. */
5223 case ENUMERAL_TYPE:
5224 shift = 3;
5225 t2 = TYPE_VALUES (t);
5226 break;
5227 case RECORD_TYPE:
5228 shift = 0;
5229 t2 = TYPE_FIELDS (t);
5230 break;
5231 case QUAL_UNION_TYPE:
5232 shift = 1;
5233 t2 = TYPE_FIELDS (t);
5234 break;
5235 case UNION_TYPE:
5236 shift = 2;
5237 t2 = TYPE_FIELDS (t);
5238 break;
5239 default:
231bd014 5240 gcc_unreachable ();
4ee9c684 5241 }
ecf2703d 5242 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
5243 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
5244 n_elements = list_length (t2);
78c2e180 5245 /* We might have a VLA here. */
5246 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
5247 size = 0;
5248 else
f9ae6f95 5249 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
ecf2703d 5250 return ((size << 24) | (n_elements << shift));
4ee9c684 5251}
5252
2ef51f0e 5253bool
5254c_type_hasher::equal (tree t1, tree t2)
5255{
5256 return lang_hooks.types_compatible_p (t1, t2);
5257}
5258
5259static GTY(()) hash_table<c_type_hasher> *type_hash_table;
1ecd4018 5260
b5ba9f3a 5261/* Return the typed-based alias set for T, which may be an expression
f7c44134 5262 or a type. Return -1 if we don't do anything special. */
ab2c1de8 5263
32c2fdea 5264alias_set_type
1cae46be 5265c_common_get_alias_set (tree t)
b5ba9f3a 5266{
be4f2de7 5267 tree u;
1cae46be 5268
e58c17e7 5269 /* For VLAs, use the alias set of the element type rather than the
5270 default of alias set 0 for types compared structurally. */
5271 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
5272 {
5273 if (TREE_CODE (t) == ARRAY_TYPE)
5274 return get_alias_set (TREE_TYPE (t));
5275 return -1;
5276 }
5277
be4f2de7 5278 /* Permit type-punning when accessing a union, provided the access
5279 is directly through the union. For example, this code does not
5280 permit taking the address of a union member and then storing
5281 through it. Even the type-punning allowed here is a GCC
5282 extension, albeit a common and useful one; the C standard says
5283 that such accesses have implementation-defined behavior. */
5284 for (u = t;
5285 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
5286 u = TREE_OPERAND (u, 0))
5287 if (TREE_CODE (u) == COMPONENT_REF
5288 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
5289 return 0;
1e2513d9 5290
9fcc3e54 5291 /* That's all the expressions we handle specially. */
84166705 5292 if (!TYPE_P (t))
9fcc3e54 5293 return -1;
5294
d716ce75 5295 /* The C standard guarantees that any object may be accessed via an
9fcc3e54 5296 lvalue that has character type. */
5297 if (t == char_type_node
5298 || t == signed_char_type_node
5299 || t == unsigned_char_type_node)
f7c44134 5300 return 0;
a5b1863e 5301
1607663f 5302 /* The C standard specifically allows aliasing between signed and
5303 unsigned variants of the same type. We treat the signed
5304 variant as canonical. */
78a8ed03 5305 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
a8868e19 5306 {
4070745f 5307 tree t1 = c_common_signed_type (t);
1607663f 5308
a8868e19 5309 /* t1 == t can happen for boolean nodes which are always unsigned. */
5310 if (t1 != t)
5311 return get_alias_set (t1);
5312 }
1e2513d9 5313
4ee9c684 5314 /* Handle the case of multiple type nodes referring to "the same" type,
5315 which occurs with IMA. These share an alias set. FIXME: Currently only
5316 C90 is handled. (In C99 type compatibility is not transitive, which
5317 complicates things mightily. The alias set splay trees can theoretically
5318 represent this, but insertion is tricky when you consider all the
5319 different orders things might arrive in.) */
5320
5321 if (c_language != clk_c || flag_isoc99)
5322 return -1;
5323
0bed3869 5324 /* Save time if there's only one input file. */
e08bd2f4 5325 if (num_in_fnames == 1)
4ee9c684 5326 return -1;
5327
5328 /* Pointers need special handling if they point to any type that
5329 needs special handling (below). */
5330 if (TREE_CODE (t) == POINTER_TYPE)
5331 {
5332 tree t2;
5333 /* Find bottom type under any nested POINTERs. */
b27ac6b5 5334 for (t2 = TREE_TYPE (t);
af592f67 5335 TREE_CODE (t2) == POINTER_TYPE;
5336 t2 = TREE_TYPE (t2))
5337 ;
b27ac6b5 5338 if (TREE_CODE (t2) != RECORD_TYPE
af592f67 5339 && TREE_CODE (t2) != ENUMERAL_TYPE
5340 && TREE_CODE (t2) != QUAL_UNION_TYPE
5341 && TREE_CODE (t2) != UNION_TYPE)
5342 return -1;
4ee9c684 5343 if (TYPE_SIZE (t2) == 0)
af592f67 5344 return -1;
4ee9c684 5345 }
5346 /* These are the only cases that need special handling. */
b27ac6b5 5347 if (TREE_CODE (t) != RECORD_TYPE
4ee9c684 5348 && TREE_CODE (t) != ENUMERAL_TYPE
5349 && TREE_CODE (t) != QUAL_UNION_TYPE
5350 && TREE_CODE (t) != UNION_TYPE
5351 && TREE_CODE (t) != POINTER_TYPE)
5352 return -1;
5353 /* Undefined? */
5354 if (TYPE_SIZE (t) == 0)
5355 return -1;
5356
b27ac6b5 5357 /* Look up t in hash table. Only one of the compatible types within each
4ee9c684 5358 alias set is recorded in the table. */
5359 if (!type_hash_table)
2ef51f0e 5360 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
5361 tree *slot = type_hash_table->find_slot (t, INSERT);
4ee9c684 5362 if (*slot != NULL)
ad16cb2c 5363 {
5364 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5365 return TYPE_ALIAS_SET ((tree)*slot);
5366 }
4ee9c684 5367 else
5368 /* Our caller will assign and record (in t) a new alias set; all we need
5369 to do is remember t in the hash table. */
5370 *slot = t;
5371
f7c44134 5372 return -1;
b5ba9f3a 5373}
902b4e01 5374\f
e60a6f7b 5375/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
a179a7dc 5376 the IS_SIZEOF parameter indicates which operator is being applied.
e60a6f7b 5377 The COMPLAIN flag controls whether we should diagnose possibly
5378 ill-formed constructs or not. LOC is the location of the SIZEOF or
a179a7dc 5379 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5380 a type in any context should be returned, rather than the normal
5381 alignment for that type. */
908c697e 5382
902b4e01 5383tree
e60a6f7b 5384c_sizeof_or_alignof_type (location_t loc,
a179a7dc 5385 tree type, bool is_sizeof, bool min_alignof,
5386 int complain)
902b4e01 5387{
d4c4d95c 5388 const char *op_name;
5389 tree value = NULL;
5390 enum tree_code type_code = TREE_CODE (type);
1cae46be 5391
908c697e 5392 op_name = is_sizeof ? "sizeof" : "__alignof__";
1cae46be 5393
d4c4d95c 5394 if (type_code == FUNCTION_TYPE)
902b4e01 5395 {
908c697e 5396 if (is_sizeof)
d4c4d95c 5397 {
9205a6cc 5398 if (complain && warn_pointer_arith)
5399 pedwarn (loc, OPT_Wpointer_arith,
8864917d 5400 "invalid application of %<sizeof%> to a function type");
ebd21de4 5401 else if (!complain)
5402 return error_mark_node;
d4c4d95c 5403 value = size_one_node;
5404 }
5405 else
83e25171 5406 {
5407 if (complain)
5408 {
5409 if (c_dialect_cxx ())
29438999 5410 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
83e25171 5411 "%<alignof%> applied to a function type");
5412 else
29438999 5413 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
83e25171 5414 "%<_Alignof%> applied to a function type");
5415 }
5416 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5417 }
d4c4d95c 5418 }
5419 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5420 {
1cae46be 5421 if (type_code == VOID_TYPE
9205a6cc 5422 && complain && warn_pointer_arith)
5423 pedwarn (loc, OPT_Wpointer_arith,
8864917d 5424 "invalid application of %qs to a void type", op_name);
ebd21de4 5425 else if (!complain)
5426 return error_mark_node;
d4c4d95c 5427 value = size_one_node;
902b4e01 5428 }
3df19e1b 5429 else if (!COMPLETE_TYPE_P (type)
5430 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
902b4e01 5431 {
d3a4d008 5432 if (complain)
3df19e1b 5433 error_at (loc, "invalid application of %qs to incomplete type %qT",
e60a6f7b 5434 op_name, type);
9c719c74 5435 return error_mark_node;
902b4e01 5436 }
3df19e1b 5437 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5438 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5439 {
5440 if (complain)
5441 error_at (loc, "invalid application of %qs to array type %qT of "
5442 "incomplete element type", op_name, type);
5443 return error_mark_node;
5444 }
902b4e01 5445 else
d4c4d95c 5446 {
908c697e 5447 if (is_sizeof)
d4c4d95c 5448 /* Convert in case a char is more than one unit. */
389dd41b 5449 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5450 size_int (TYPE_PRECISION (char_type_node)
5451 / BITS_PER_UNIT));
a179a7dc 5452 else if (min_alignof)
c2c4ae8d 5453 value = size_int (min_align_of_type (type));
d4c4d95c 5454 else
d37625c0 5455 value = size_int (TYPE_ALIGN_UNIT (type));
d4c4d95c 5456 }
902b4e01 5457
5a1fe2db 5458 /* VALUE will have the middle-end integer type sizetype.
5459 However, we should really return a value of type `size_t',
5460 which is just a typedef for an ordinary integer type. */
389dd41b 5461 value = fold_convert_loc (loc, size_type_node, value);
1cae46be 5462
d4c4d95c 5463 return value;
902b4e01 5464}
5465
5466/* Implement the __alignof keyword: Return the minimum required
097b5c8b 5467 alignment of EXPR, measured in bytes. For VAR_DECLs,
5468 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
e60a6f7b 5469 from an "aligned" __attribute__ specification). LOC is the
5470 location of the ALIGNOF operator. */
72040e7e 5471
902b4e01 5472tree
e60a6f7b 5473c_alignof_expr (location_t loc, tree expr)
902b4e01 5474{
5475 tree t;
5476
097b5c8b 5477 if (VAR_OR_FUNCTION_DECL_P (expr))
d37625c0 5478 t = size_int (DECL_ALIGN_UNIT (expr));
1cae46be 5479
902b4e01 5480 else if (TREE_CODE (expr) == COMPONENT_REF
5481 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5482 {
e60a6f7b 5483 error_at (loc, "%<__alignof%> applied to a bit-field");
902b4e01 5484 t = size_one_node;
5485 }
5486 else if (TREE_CODE (expr) == COMPONENT_REF
7cc7e163 5487 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
d37625c0 5488 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
1cae46be 5489
aa3e402e 5490 else if (INDIRECT_REF_P (expr))
902b4e01 5491 {
5492 tree t = TREE_OPERAND (expr, 0);
5493 tree best = t;
5494 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1cae46be 5495
72dd6141 5496 while (CONVERT_EXPR_P (t)
7cc7e163 5497 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
902b4e01 5498 {
5499 int thisalign;
5500
5501 t = TREE_OPERAND (t, 0);
5502 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5503 if (thisalign > bestalign)
5504 best = t, bestalign = thisalign;
5505 }
e60a6f7b 5506 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
902b4e01 5507 }
5508 else
e60a6f7b 5509 return c_alignof (loc, TREE_TYPE (expr));
902b4e01 5510
389dd41b 5511 return fold_convert_loc (loc, size_type_node, t);
902b4e01 5512}
5513\f
8fe4a266 5514/* Handle C and C++ default attributes. */
5515
5516enum built_in_attribute
5517{
5518#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5519#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
c8010b80 5520#define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
8fe4a266 5521#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5522#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
8fe4a266 5523#include "builtin-attrs.def"
5524#undef DEF_ATTR_NULL_TREE
5525#undef DEF_ATTR_INT
c8010b80 5526#undef DEF_ATTR_STRING
8fe4a266 5527#undef DEF_ATTR_IDENT
5528#undef DEF_ATTR_TREE_LIST
8fe4a266 5529 ATTR_LAST
5530};
5531
5532static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5533
1cae46be 5534static void c_init_attributes (void);
8fe4a266 5535
27213ba3 5536enum c_builtin_type
72040e7e 5537{
d2d4bdde 5538#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5539#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5540#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5541#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5542#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5543#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
0a39fd54 5544#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
3c77ca67 5545#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5546 ARG6) NAME,
5547#define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5548 ARG6, ARG7) NAME,
5549#define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5550 ARG6, ARG7, ARG8) NAME,
d2d4bdde 5551#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5552#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5553#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
03901330 5554#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
0a39fd54 5555#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3c77ca67 5556#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
6349b8cc 5557 NAME,
e561d5e1 5558#define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5559 ARG6) NAME,
6349b8cc 5560#define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5561 ARG6, ARG7) NAME,
d2d4bdde 5562#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5563#include "builtin-types.def"
5564#undef DEF_PRIMITIVE_TYPE
5565#undef DEF_FUNCTION_TYPE_0
5566#undef DEF_FUNCTION_TYPE_1
5567#undef DEF_FUNCTION_TYPE_2
5568#undef DEF_FUNCTION_TYPE_3
5569#undef DEF_FUNCTION_TYPE_4
0a39fd54 5570#undef DEF_FUNCTION_TYPE_5
5571#undef DEF_FUNCTION_TYPE_6
27213ba3 5572#undef DEF_FUNCTION_TYPE_7
bc7bff74 5573#undef DEF_FUNCTION_TYPE_8
d2d4bdde 5574#undef DEF_FUNCTION_TYPE_VAR_0
5575#undef DEF_FUNCTION_TYPE_VAR_1
5576#undef DEF_FUNCTION_TYPE_VAR_2
03901330 5577#undef DEF_FUNCTION_TYPE_VAR_3
0a39fd54 5578#undef DEF_FUNCTION_TYPE_VAR_4
5579#undef DEF_FUNCTION_TYPE_VAR_5
e561d5e1 5580#undef DEF_FUNCTION_TYPE_VAR_6
6349b8cc 5581#undef DEF_FUNCTION_TYPE_VAR_7
d2d4bdde 5582#undef DEF_POINTER_TYPE
27213ba3 5583 BT_LAST
5584};
5585
5586typedef enum c_builtin_type builtin_type;
d2d4bdde 5587
27213ba3 5588/* A temporary array for c_common_nodes_and_builtins. Used in
5589 communication with def_fn_type. */
5590static tree builtin_types[(int) BT_LAST + 1];
d2d4bdde 5591
27213ba3 5592/* A helper function for c_common_nodes_and_builtins. Build function type
5593 for DEF with return type RET and N arguments. If VAR is true, then the
5594 function should be variadic after those N arguments.
5595
5596 Takes special care not to ICE if any of the types involved are
5597 error_mark_node, which indicates that said type is not in fact available
5598 (see builtin_type_for_size). In which case the function type as a whole
5599 should be error_mark_node. */
5600
5601static void
5602def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5603{
3a939d12 5604 tree t;
5605 tree *args = XALLOCAVEC (tree, n);
27213ba3 5606 va_list list;
5607 int i;
5608
5609 va_start (list, n);
5610 for (i = 0; i < n; ++i)
5611 {
7d339f93 5612 builtin_type a = (builtin_type) va_arg (list, int);
27213ba3 5613 t = builtin_types[a];
5614 if (t == error_mark_node)
5615 goto egress;
3a939d12 5616 args[i] = t;
27213ba3 5617 }
27213ba3 5618
27213ba3 5619 t = builtin_types[ret];
5620 if (t == error_mark_node)
5621 goto egress;
3a939d12 5622 if (var)
5623 t = build_varargs_function_type_array (t, n, args);
5624 else
5625 t = build_function_type_array (t, n, args);
27213ba3 5626
5627 egress:
5628 builtin_types[def] = t;
451c8e2f 5629 va_end (list);
27213ba3 5630}
5631
dce22712 5632/* Build builtin functions common to both C and C++ language
5633 frontends. */
5634
5635static void
5636c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5637{
5638#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5639 builtin_types[ENUM] = VALUE;
5640#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5641 def_fn_type (ENUM, RETURN, 0, 0);
5642#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5643 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5644#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5645 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5646#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5647 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5648#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5649 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5650#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5651 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5652#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5653 ARG6) \
5654 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5655#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5656 ARG6, ARG7) \
5657 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
bc7bff74 5658#define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5659 ARG6, ARG7, ARG8) \
5660 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5661 ARG7, ARG8);
dce22712 5662#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5663 def_fn_type (ENUM, RETURN, 1, 0);
5664#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5665 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5666#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5667 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5668#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5669 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5670#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5671 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5672#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5673 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
e561d5e1 5674#define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5675 ARG6) \
5676 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
6349b8cc 5677#define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5678 ARG6, ARG7) \
5679 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
dce22712 5680#define DEF_POINTER_TYPE(ENUM, TYPE) \
5681 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5682
5683#include "builtin-types.def"
5684
5685#undef DEF_PRIMITIVE_TYPE
3c77ca67 5686#undef DEF_FUNCTION_TYPE_0
dce22712 5687#undef DEF_FUNCTION_TYPE_1
5688#undef DEF_FUNCTION_TYPE_2
5689#undef DEF_FUNCTION_TYPE_3
5690#undef DEF_FUNCTION_TYPE_4
5691#undef DEF_FUNCTION_TYPE_5
5692#undef DEF_FUNCTION_TYPE_6
3c77ca67 5693#undef DEF_FUNCTION_TYPE_7
5694#undef DEF_FUNCTION_TYPE_8
dce22712 5695#undef DEF_FUNCTION_TYPE_VAR_0
5696#undef DEF_FUNCTION_TYPE_VAR_1
5697#undef DEF_FUNCTION_TYPE_VAR_2
5698#undef DEF_FUNCTION_TYPE_VAR_3
5699#undef DEF_FUNCTION_TYPE_VAR_4
5700#undef DEF_FUNCTION_TYPE_VAR_5
e561d5e1 5701#undef DEF_FUNCTION_TYPE_VAR_6
6349b8cc 5702#undef DEF_FUNCTION_TYPE_VAR_7
dce22712 5703#undef DEF_POINTER_TYPE
5704 builtin_types[(int) BT_LAST] = NULL_TREE;
5705
5706 c_init_attributes ();
5707
5708#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5709 NONANSI_P, ATTRS, IMPLICIT, COND) \
5710 if (NAME && COND) \
5711 def_builtin_1 (ENUM, NAME, CLASS, \
5712 builtin_types[(int) TYPE], \
5713 builtin_types[(int) LIBTYPE], \
5714 BOTH_P, FALLBACK_P, NONANSI_P, \
5715 built_in_attributes[(int) ATTRS], IMPLICIT);
5716#include "builtins.def"
5717#undef DEF_BUILTIN
5718
87eb1c28 5719 targetm.init_builtins ();
5720
471eff36 5721 build_common_builtin_nodes ();
dce22712 5722
a89e6c15 5723 if (flag_cilkplus)
d037099f 5724 cilk_init_builtins ();
dce22712 5725}
5726
9e6687c8 5727/* Like get_identifier, but avoid warnings about null arguments when
5728 the argument may be NULL for targets where GCC lacks stdint.h type
5729 information. */
5730
5731static inline tree
5732c_get_ident (const char *id)
5733{
5734 return get_identifier (id);
5735}
5736
27213ba3 5737/* Build tree nodes and builtin functions common to both C and C++ language
5738 frontends. */
5739
5740void
5741c_common_nodes_and_builtins (void)
5742{
924bbf02 5743 int char16_type_size;
5744 int char32_type_size;
174fcc61 5745 int wchar_type_size;
5746 tree array_domain_type;
2d47cc32 5747 tree va_list_ref_type_node;
8a15c04a 5748 tree va_list_arg_type_node;
9f75f026 5749 int i;
a66c9326 5750
c38a75b7 5751 build_common_tree_nodes (flag_signed_char, flag_short_double);
e593356b 5752
174fcc61 5753 /* Define `int' and `char' first so that dbx will output them first. */
d946ea19 5754 record_builtin_type (RID_INT, NULL, integer_type_node);
174fcc61 5755 record_builtin_type (RID_CHAR, "char", char_type_node);
5756
5757 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5758 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5759 but not C. Are the conditionals here needed? */
c0f19401 5760 if (c_dialect_cxx ())
d946ea19 5761 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
174fcc61 5762 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5763 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5764 record_builtin_type (RID_MAX, "long unsigned int",
5765 long_unsigned_type_node);
9f75f026 5766
5767 for (i = 0; i < NUM_INT_N_ENTS; i ++)
6388cfe2 5768 {
9f75f026 5769 char name[25];
5770
5771 sprintf (name, "__int%d", int_n_data[i].bitsize);
76738f56 5772 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
9f75f026 5773 int_n_trees[i].signed_type);
5774 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
76738f56 5775 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
6388cfe2 5776 }
9f75f026 5777
c0f19401 5778 if (c_dialect_cxx ())
174fcc61 5779 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5780 record_builtin_type (RID_MAX, "long long int",
5781 long_long_integer_type_node);
5782 record_builtin_type (RID_MAX, "long long unsigned int",
5783 long_long_unsigned_type_node);
c0f19401 5784 if (c_dialect_cxx ())
174fcc61 5785 record_builtin_type (RID_MAX, "long long unsigned",
5786 long_long_unsigned_type_node);
5787 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5788 record_builtin_type (RID_MAX, "short unsigned int",
5789 short_unsigned_type_node);
c0f19401 5790 if (c_dialect_cxx ())
174fcc61 5791 record_builtin_type (RID_MAX, "unsigned short",
5792 short_unsigned_type_node);
5793
5794 /* Define both `signed char' and `unsigned char'. */
5795 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5796 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5797
771d21fa 5798 /* These are types that c_common_type_for_size and
5799 c_common_type_for_mode use. */
e60a6f7b 5800 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5801 TYPE_DECL, NULL_TREE,
dc24ddbd 5802 intQI_type_node));
e60a6f7b 5803 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5804 TYPE_DECL, NULL_TREE,
dc24ddbd 5805 intHI_type_node));
e60a6f7b 5806 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5807 TYPE_DECL, NULL_TREE,
dc24ddbd 5808 intSI_type_node));
e60a6f7b 5809 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5810 TYPE_DECL, NULL_TREE,
dc24ddbd 5811 intDI_type_node));
174fcc61 5812#if HOST_BITS_PER_WIDE_INT >= 64
9f75f026 5813 /* Note that this is different than the __int128 type that's part of
5814 the generic __intN support. */
f1515a39 5815 if (targetm.scalar_mode_supported_p (TImode))
e60a6f7b 5816 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5817 TYPE_DECL,
f1515a39 5818 get_identifier ("__int128_t"),
5819 intTI_type_node));
174fcc61 5820#endif
e60a6f7b 5821 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5822 TYPE_DECL, NULL_TREE,
dc24ddbd 5823 unsigned_intQI_type_node));
e60a6f7b 5824 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5825 TYPE_DECL, NULL_TREE,
dc24ddbd 5826 unsigned_intHI_type_node));
e60a6f7b 5827 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5828 TYPE_DECL, NULL_TREE,
dc24ddbd 5829 unsigned_intSI_type_node));
e60a6f7b 5830 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5831 TYPE_DECL, NULL_TREE,
dc24ddbd 5832 unsigned_intDI_type_node));
174fcc61 5833#if HOST_BITS_PER_WIDE_INT >= 64
f1515a39 5834 if (targetm.scalar_mode_supported_p (TImode))
e60a6f7b 5835 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5836 TYPE_DECL,
f1515a39 5837 get_identifier ("__uint128_t"),
5838 unsigned_intTI_type_node));
174fcc61 5839#endif
5840
5841 /* Create the widest literal types. */
5842 widest_integer_literal_type_node
5843 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
e60a6f7b 5844 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5845 TYPE_DECL, NULL_TREE,
dc24ddbd 5846 widest_integer_literal_type_node));
174fcc61 5847
5848 widest_unsigned_literal_type_node
5849 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
e60a6f7b 5850 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5851 TYPE_DECL, NULL_TREE,
dc24ddbd 5852 widest_unsigned_literal_type_node));
174fcc61 5853
654ef926 5854 signed_size_type_node = c_common_signed_type (size_type_node);
174fcc61 5855
73673831 5856 pid_type_node =
5857 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5858
d946ea19 5859 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5860 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
174fcc61 5861 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5862
c4503c0a 5863 /* Only supported decimal floating point extension if the target
5864 actually supports underlying modes. */
48e1416a 5865 if (targetm.scalar_mode_supported_p (SDmode)
c4503c0a 5866 && targetm.scalar_mode_supported_p (DDmode)
5867 && targetm.scalar_mode_supported_p (TDmode))
5868 {
5869 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5870 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5871 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5872 }
5873
9421ebb9 5874 if (targetm.fixed_point_supported_p ())
5875 {
5876 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5877 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5878 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5879 record_builtin_type (RID_MAX, "long long _Fract",
5880 long_long_fract_type_node);
5881 record_builtin_type (RID_MAX, "unsigned short _Fract",
5882 unsigned_short_fract_type_node);
5883 record_builtin_type (RID_MAX, "unsigned _Fract",
5884 unsigned_fract_type_node);
5885 record_builtin_type (RID_MAX, "unsigned long _Fract",
5886 unsigned_long_fract_type_node);
5887 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5888 unsigned_long_long_fract_type_node);
5889 record_builtin_type (RID_MAX, "_Sat short _Fract",
5890 sat_short_fract_type_node);
5891 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5892 record_builtin_type (RID_MAX, "_Sat long _Fract",
5893 sat_long_fract_type_node);
5894 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5895 sat_long_long_fract_type_node);
5896 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5897 sat_unsigned_short_fract_type_node);
5898 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5899 sat_unsigned_fract_type_node);
5900 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5901 sat_unsigned_long_fract_type_node);
5902 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5903 sat_unsigned_long_long_fract_type_node);
5904 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5905 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5906 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5907 record_builtin_type (RID_MAX, "long long _Accum",
5908 long_long_accum_type_node);
5909 record_builtin_type (RID_MAX, "unsigned short _Accum",
5910 unsigned_short_accum_type_node);
5911 record_builtin_type (RID_MAX, "unsigned _Accum",
5912 unsigned_accum_type_node);
5913 record_builtin_type (RID_MAX, "unsigned long _Accum",
5914 unsigned_long_accum_type_node);
5915 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5916 unsigned_long_long_accum_type_node);
5917 record_builtin_type (RID_MAX, "_Sat short _Accum",
5918 sat_short_accum_type_node);
5919 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5920 record_builtin_type (RID_MAX, "_Sat long _Accum",
5921 sat_long_accum_type_node);
5922 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5923 sat_long_long_accum_type_node);
5924 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5925 sat_unsigned_short_accum_type_node);
5926 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5927 sat_unsigned_accum_type_node);
5928 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5929 sat_unsigned_long_accum_type_node);
5930 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5931 sat_unsigned_long_long_accum_type_node);
5932
5933 }
5934
e60a6f7b 5935 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5936 TYPE_DECL,
dc24ddbd 5937 get_identifier ("complex int"),
5938 complex_integer_type_node));
e60a6f7b 5939 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5940 TYPE_DECL,
dc24ddbd 5941 get_identifier ("complex float"),
5942 complex_float_type_node));
e60a6f7b 5943 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5944 TYPE_DECL,
dc24ddbd 5945 get_identifier ("complex double"),
5946 complex_double_type_node));
5947 lang_hooks.decls.pushdecl
e60a6f7b 5948 (build_decl (UNKNOWN_LOCATION,
5949 TYPE_DECL, get_identifier ("complex long double"),
20325f61 5950 complex_long_double_type_node));
174fcc61 5951
e256d445 5952 if (c_dialect_cxx ())
5953 /* For C++, make fileptr_type_node a distinct void * type until
5954 FILE type is defined. */
e086912e 5955 fileptr_type_node = build_variant_type_copy (ptr_type_node);
e256d445 5956
d946ea19 5957 record_builtin_type (RID_VOID, NULL, void_type_node);
174fcc61 5958
6753bca0 5959 /* Set the TYPE_NAME for any variants that were built before
5960 record_builtin_type gave names to the built-in types. */
5961 {
5962 tree void_name = TYPE_NAME (void_type_node);
5963 TYPE_NAME (void_type_node) = NULL_TREE;
5964 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5965 = void_name;
5966 TYPE_NAME (void_type_node) = void_name;
5967 }
5968
174fcc61 5969 void_list_node = build_void_list_node ();
5970
5971 /* Make a type to be the domain of a few array types
5972 whose domains don't really matter.
5973 200 is small enough that it always fits in size_t
5974 and large enough that it can hold most function names for the
5975 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5976 array_domain_type = build_index_type (size_int (200));
5977
5978 /* Make a type for arrays of characters.
5979 With luck nothing will ever really depend on the length of this
5980 array type. */
5981 char_array_type_node
5982 = build_array_type (char_type_node, array_domain_type);
5983
d2d4bdde 5984 string_type_node = build_pointer_type (char_type_node);
5985 const_string_type_node
5986 = build_pointer_type (build_qualified_type
5987 (char_type_node, TYPE_QUAL_CONST));
5988
174fcc61 5989 /* This is special for C++ so functions can be overloaded. */
18ef7ac2 5990 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
174fcc61 5991 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5992 wchar_type_size = TYPE_PRECISION (wchar_type_node);
f3449a3c 5993 underlying_wchar_type_node = wchar_type_node;
c0f19401 5994 if (c_dialect_cxx ())
174fcc61 5995 {
78a8ed03 5996 if (TYPE_UNSIGNED (wchar_type_node))
174fcc61 5997 wchar_type_node = make_unsigned_type (wchar_type_size);
5998 else
5999 wchar_type_node = make_signed_type (wchar_type_size);
6000 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
6001 }
174fcc61 6002
6003 /* This is for wide string constants. */
6004 wchar_array_type_node
6005 = build_array_type (wchar_type_node, array_domain_type);
6006
924bbf02 6007 /* Define 'char16_t'. */
6008 char16_type_node = get_identifier (CHAR16_TYPE);
6009 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
6010 char16_type_size = TYPE_PRECISION (char16_type_node);
6011 if (c_dialect_cxx ())
6012 {
6013 char16_type_node = make_unsigned_type (char16_type_size);
6014
60777f69 6015 if (cxx_dialect >= cxx11)
924bbf02 6016 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
6017 }
6018
6019 /* This is for UTF-16 string constants. */
6020 char16_array_type_node
6021 = build_array_type (char16_type_node, array_domain_type);
6022
6023 /* Define 'char32_t'. */
6024 char32_type_node = get_identifier (CHAR32_TYPE);
6025 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
6026 char32_type_size = TYPE_PRECISION (char32_type_node);
6027 if (c_dialect_cxx ())
6028 {
6029 char32_type_node = make_unsigned_type (char32_type_size);
6030
60777f69 6031 if (cxx_dialect >= cxx11)
924bbf02 6032 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
6033 }
6034
6035 /* This is for UTF-32 string constants. */
6036 char32_array_type_node
6037 = build_array_type (char32_type_node, array_domain_type);
6038
6bf5ed8d 6039 wint_type_node =
6040 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
6041
6042 intmax_type_node =
6043 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
6044 uintmax_type_node =
6045 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
6046
f3449a3c 6047 if (SIG_ATOMIC_TYPE)
6048 sig_atomic_type_node =
9e6687c8 6049 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
f3449a3c 6050 if (INT8_TYPE)
6051 int8_type_node =
9e6687c8 6052 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
f3449a3c 6053 if (INT16_TYPE)
6054 int16_type_node =
9e6687c8 6055 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
f3449a3c 6056 if (INT32_TYPE)
6057 int32_type_node =
9e6687c8 6058 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
f3449a3c 6059 if (INT64_TYPE)
6060 int64_type_node =
9e6687c8 6061 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
f3449a3c 6062 if (UINT8_TYPE)
6063 uint8_type_node =
9e6687c8 6064 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
f3449a3c 6065 if (UINT16_TYPE)
d1081017 6066 c_uint16_type_node = uint16_type_node =
9e6687c8 6067 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
f3449a3c 6068 if (UINT32_TYPE)
d1081017 6069 c_uint32_type_node = uint32_type_node =
9e6687c8 6070 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
f3449a3c 6071 if (UINT64_TYPE)
d1081017 6072 c_uint64_type_node = uint64_type_node =
9e6687c8 6073 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
f3449a3c 6074 if (INT_LEAST8_TYPE)
6075 int_least8_type_node =
9e6687c8 6076 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
f3449a3c 6077 if (INT_LEAST16_TYPE)
6078 int_least16_type_node =
9e6687c8 6079 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
f3449a3c 6080 if (INT_LEAST32_TYPE)
6081 int_least32_type_node =
9e6687c8 6082 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
f3449a3c 6083 if (INT_LEAST64_TYPE)
6084 int_least64_type_node =
9e6687c8 6085 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
f3449a3c 6086 if (UINT_LEAST8_TYPE)
6087 uint_least8_type_node =
9e6687c8 6088 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
f3449a3c 6089 if (UINT_LEAST16_TYPE)
6090 uint_least16_type_node =
9e6687c8 6091 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
f3449a3c 6092 if (UINT_LEAST32_TYPE)
6093 uint_least32_type_node =
9e6687c8 6094 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
f3449a3c 6095 if (UINT_LEAST64_TYPE)
6096 uint_least64_type_node =
9e6687c8 6097 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
f3449a3c 6098 if (INT_FAST8_TYPE)
6099 int_fast8_type_node =
9e6687c8 6100 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
f3449a3c 6101 if (INT_FAST16_TYPE)
6102 int_fast16_type_node =
9e6687c8 6103 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
f3449a3c 6104 if (INT_FAST32_TYPE)
6105 int_fast32_type_node =
9e6687c8 6106 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
f3449a3c 6107 if (INT_FAST64_TYPE)
6108 int_fast64_type_node =
9e6687c8 6109 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
f3449a3c 6110 if (UINT_FAST8_TYPE)
6111 uint_fast8_type_node =
9e6687c8 6112 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
f3449a3c 6113 if (UINT_FAST16_TYPE)
6114 uint_fast16_type_node =
9e6687c8 6115 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
f3449a3c 6116 if (UINT_FAST32_TYPE)
6117 uint_fast32_type_node =
9e6687c8 6118 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
f3449a3c 6119 if (UINT_FAST64_TYPE)
6120 uint_fast64_type_node =
9e6687c8 6121 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
f3449a3c 6122 if (INTPTR_TYPE)
6123 intptr_type_node =
9e6687c8 6124 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
f3449a3c 6125 if (UINTPTR_TYPE)
6126 uintptr_type_node =
9e6687c8 6127 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
f3449a3c 6128
3a939d12 6129 default_function_type
6130 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
6bf5ed8d 6131 ptrdiff_type_node
6132 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
11773141 6133 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
6bf5ed8d 6134
dc24ddbd 6135 lang_hooks.decls.pushdecl
e60a6f7b 6136 (build_decl (UNKNOWN_LOCATION,
6137 TYPE_DECL, get_identifier ("__builtin_va_list"),
20325f61 6138 va_list_type_node));
202d6e5f 6139 if (targetm.enum_va_list_p)
acd6f472 6140 {
6141 int l;
6142 const char *pname;
6143 tree ptype;
5f57a8b1 6144
202d6e5f 6145 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
acd6f472 6146 {
6147 lang_hooks.decls.pushdecl
6148 (build_decl (UNKNOWN_LOCATION,
6149 TYPE_DECL, get_identifier (pname),
6150 ptype));
ab2c1de8 6151
acd6f472 6152 }
6153 }
8a15c04a 6154
8a15c04a 6155 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
2d47cc32 6156 {
6157 va_list_arg_type_node = va_list_ref_type_node =
6158 build_pointer_type (TREE_TYPE (va_list_type_node));
6159 }
8a15c04a 6160 else
2d47cc32 6161 {
6162 va_list_arg_type_node = va_list_type_node;
6163 va_list_ref_type_node = build_reference_type (va_list_type_node);
6164 }
1cae46be 6165
dce22712 6166 if (!flag_preprocess_only)
6167 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
ffa8918b 6168
5c62f199 6169 main_identifier_node = get_identifier ("main");
ae84079f 6170
6171 /* Create the built-in __null node. It is important that this is
6172 not shared. */
271e739a 6173 null_node = make_int_cst (1, 1);
ae84079f 6174 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
27213ba3 6175
6176 /* Since builtin_types isn't gc'ed, don't export these nodes. */
6177 memset (builtin_types, 0, sizeof (builtin_types));
72040e7e 6178}
a66c9326 6179
79b01846 6180/* The number of named compound-literals generated thus far. */
6181static GTY(()) int compound_literal_number;
6182
6183/* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
6184
6185void
6186set_compound_literal_name (tree decl)
6187{
6188 char *name;
6189 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
6190 compound_literal_number);
6191 compound_literal_number++;
6192 DECL_NAME (decl) = get_identifier (name);
6193}
6194
2e474820 6195/* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
6196 TYPE and operand OP. */
6197
6198static tree
6199build_va_arg_1 (location_t loc, tree type, tree op)
6200{
6201 tree expr = build1 (VA_ARG_EXPR, type, op);
6202 SET_EXPR_LOCATION (expr, loc);
6203 return expr;
6204}
6205
6206/* Return a VA_ARG_EXPR corresponding to a source-level expression
6207 va_arg (EXPR, TYPE) at source location LOC. */
6208
a66c9326 6209tree
e60a6f7b 6210build_va_arg (location_t loc, tree expr, tree type)
a66c9326 6211{
c37be9ec 6212 tree va_type = TREE_TYPE (expr);
6213 tree canon_va_type = (va_type == error_mark_node
6214 ? NULL_TREE
6215 : targetm.canonical_va_list_type (va_type));
6216
2e474820 6217 if (va_type == error_mark_node
6218 || canon_va_type == NULL_TREE)
c37be9ec 6219 {
2e474820 6220 /* Let's handle things neutrallly, if expr:
6221 - has undeclared type, or
6222 - is not an va_list type. */
6223 return build_va_arg_1 (loc, type, expr);
c37be9ec 6224 }
f7fec1c7 6225
2e474820 6226 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
6227 {
6228 /* Case 1: Not an array type. */
6229
6230 /* Take the address, to get '&ap'. */
6231 mark_addressable (expr);
6232 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
6233
6234 /* Verify that &ap is still recognized as having va_list type. */
6235 tree canon_expr_type
6236 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6237 gcc_assert (canon_expr_type != NULL_TREE);
6238
6239 return build_va_arg_1 (loc, type, expr);
6240 }
6241
6242 /* Case 2: Array type.
6243
6244 Background:
6245
6246 For contrast, let's start with the simple case (case 1). If
6247 canon_va_type is not an array type, but say a char *, then when
6248 passing-by-value a va_list, the type of the va_list param decl is
6249 the same as for another va_list decl (all ap's are char *):
6250
6251 f2_1 (char * ap)
6252 D.1815 = VA_ARG (&ap, 0B, 1);
6253 return D.1815;
6254
6255 f2 (int i)
6256 char * ap.0;
6257 char * ap;
6258 __builtin_va_start (&ap, 0);
6259 ap.0 = ap;
6260 res = f2_1 (ap.0);
6261 __builtin_va_end (&ap);
6262 D.1812 = res;
6263 return D.1812;
6264
6265 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
6266 va_list the type of the va_list param decl (case 2b, struct * ap) is not
6267 the same as for another va_list decl (case 2a, struct ap[1]).
6268
6269 f2_1 (struct * ap)
6270 D.1844 = VA_ARG (ap, 0B, 0);
6271 return D.1844;
6272
6273 f2 (int i)
6274 struct ap[1];
6275 __builtin_va_start (&ap, 0);
6276 res = f2_1 (&ap);
6277 __builtin_va_end (&ap);
6278 D.1841 = res;
6279 return D.1841;
6280
6281 Case 2b is different because:
6282 - on the callee side, the parm decl has declared type va_list, but
6283 grokdeclarator changes the type of the parm decl to a pointer to the
6284 array elem type.
6285 - on the caller side, the pass-by-value uses &ap.
6286
6287 We unify these two cases (case 2a: va_list is array type,
6288 case 2b: va_list is pointer to array elem type), by adding '&' for the
6289 array type case, such that we have a pointer to array elem in both
6290 cases. */
6291
6292 if (TREE_CODE (va_type) == ARRAY_TYPE)
6293 {
6294 /* Case 2a: va_list is array type. */
6295
6296 /* Take the address, to get '&ap'. Make sure it's a pointer to array
6297 elem type. */
6298 mark_addressable (expr);
6299 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
6300 expr);
6301
6302 /* Verify that &ap is still recognized as having va_list type. */
6303 tree canon_expr_type
6304 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6305 gcc_assert (canon_expr_type != NULL_TREE);
6306 }
6307 else
6308 {
6309 /* Case 2b: va_list is pointer to array elem type. */
6310 gcc_assert (POINTER_TYPE_P (va_type));
6311 gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type));
6312
6313 /* Don't take the address. We've already got '&ap'. */
6314 ;
6315 }
6316
6317 return build_va_arg_1 (loc, type, expr);
a66c9326 6318}
0d4238dc 6319
6320
dd878098 6321/* Linked list of disabled built-in functions. */
6322
6dc50383 6323struct disabled_builtin
dd878098 6324{
6325 const char *name;
6326 struct disabled_builtin *next;
6dc50383 6327};
dd878098 6328static disabled_builtin *disabled_builtins = NULL;
6329
1cae46be 6330static bool builtin_function_disabled_p (const char *);
dd878098 6331
6332/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
6333 begins with "__builtin_", give an error. */
6334
6335void
1cae46be 6336disable_builtin_function (const char *name)
dd878098 6337{
6338 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
b0b1af64 6339 error ("cannot disable built-in function %qs", name);
dd878098 6340 else
6341 {
e85905e5 6342 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
6343 new_disabled_builtin->name = name;
6344 new_disabled_builtin->next = disabled_builtins;
6345 disabled_builtins = new_disabled_builtin;
dd878098 6346 }
6347}
6348
6349
6350/* Return true if the built-in function NAME has been disabled, false
6351 otherwise. */
6352
6353static bool
1cae46be 6354builtin_function_disabled_p (const char *name)
dd878098 6355{
6356 disabled_builtin *p;
6357 for (p = disabled_builtins; p != NULL; p = p->next)
6358 {
6359 if (strcmp (name, p->name) == 0)
6360 return true;
6361 }
6362 return false;
6363}
6364
6365
3237155d 6366/* Worker for DEF_BUILTIN.
6367 Possibly define a builtin function with one or two names.
6368 Does not declare a non-__builtin_ function if flag_no_builtin, or if
6369 nonansi_p and flag_no_nonansi_builtin. */
0d4238dc 6370
3237155d 6371static void
6372def_builtin_1 (enum built_in_function fncode,
6373 const char *name,
6374 enum built_in_class fnclass,
6375 tree fntype, tree libtype,
6376 bool both_p, bool fallback_p, bool nonansi_p,
6377 tree fnattrs, bool implicit_p)
0d4238dc 6378{
3237155d 6379 tree decl;
6380 const char *libname;
6381
27213ba3 6382 if (fntype == error_mark_node)
6383 return;
6384
3237155d 6385 gcc_assert ((!both_p && !fallback_p)
6386 || !strncmp (name, "__builtin_",
6387 strlen ("__builtin_")));
6388
6389 libname = name + strlen ("__builtin_");
54be5d7e 6390 decl = add_builtin_function (name, fntype, fncode, fnclass,
6391 (fallback_p ? libname : NULL),
6392 fnattrs);
b9a16870 6393
6394 set_builtin_decl (fncode, decl, implicit_p);
6395
3237155d 6396 if (both_p
6397 && !flag_no_builtin && !builtin_function_disabled_p (libname)
dd878098 6398 && !(nonansi_p && flag_no_nonansi_builtin))
54be5d7e 6399 add_builtin_function (libname, libtype, fncode, fnclass,
6400 NULL, fnattrs);
0d4238dc 6401}
e94026da 6402\f
d7aeef06 6403/* Nonzero if the type T promotes to int. This is (nearly) the
6404 integral promotions defined in ISO C99 6.3.1.1/2. */
6405
6406bool
9f627b1a 6407c_promoting_integer_type_p (const_tree t)
d7aeef06 6408{
6409 switch (TREE_CODE (t))
6410 {
6411 case INTEGER_TYPE:
6412 return (TYPE_MAIN_VARIANT (t) == char_type_node
6413 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
6414 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
6415 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
7aa1e6eb 6416 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
6417 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d7aeef06 6418
6419 case ENUMERAL_TYPE:
6420 /* ??? Technically all enumerations not larger than an int
6421 promote to an int. But this is used along code paths
6422 that only want to notice a size change. */
6423 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6424
6425 case BOOLEAN_TYPE:
6426 return 1;
6427
6428 default:
6429 return 0;
6430 }
6431}
6432
e94026da 6433/* Return 1 if PARMS specifies a fixed number of parameters
6434 and none of their types is affected by default promotions. */
6435
6436int
9f627b1a 6437self_promoting_args_p (const_tree parms)
e94026da 6438{
9f627b1a 6439 const_tree t;
e94026da 6440 for (t = parms; t; t = TREE_CHAIN (t))
6441 {
19cb6b50 6442 tree type = TREE_VALUE (t);
43f74bc4 6443
e1d8e198 6444 if (type == error_mark_node)
6445 continue;
6446
e94026da 6447 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6448 return 0;
6449
6450 if (type == 0)
6451 return 0;
6452
6453 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6454 return 0;
6455
d7aeef06 6456 if (c_promoting_integer_type_p (type))
e94026da 6457 return 0;
6458 }
6459 return 1;
6460}
605fb01e 6461
c10de5e7 6462/* Recursively remove any '*' or '&' operator from TYPE. */
6463tree
6464strip_pointer_operator (tree t)
6465{
6466 while (POINTER_TYPE_P (t))
6467 t = TREE_TYPE (t);
6468 return t;
6469}
6470
57a0ed23 6471/* Recursively remove pointer or array type from TYPE. */
6472tree
6473strip_pointer_or_array_types (tree t)
6474{
6475 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6476 t = TREE_TYPE (t);
6477 return t;
6478}
6479
e41f0d80 6480/* Used to compare case labels. K1 and K2 are actually tree nodes
6481 representing case labels, or NULL_TREE for a `default' label.
6482 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6483 K2, and 0 if K1 and K2 are equal. */
6484
6485int
1cae46be 6486case_compare (splay_tree_key k1, splay_tree_key k2)
e41f0d80 6487{
6488 /* Consider a NULL key (such as arises with a `default' label) to be
6489 smaller than anything else. */
6490 if (!k1)
6491 return k2 ? -1 : 0;
6492 else if (!k2)
6493 return k1 ? 1 : 0;
6494
6495 return tree_int_cst_compare ((tree) k1, (tree) k2);
6496}
6497
e60a6f7b 6498/* Process a case label, located at LOC, for the range LOW_VALUE
6499 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6500 then this case label is actually a `default' label. If only
6501 HIGH_VALUE is NULL_TREE, then case label was declared using the
6502 usual C/C++ syntax, rather than the GNU case range extension.
6503 CASES is a tree containing all the case ranges processed so far;
be23b16f 6504 COND is the condition for the switch-statement itself.
6505 OUTSIDE_RANGE_P says whether there was a case value that doesn't
6506 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
6507 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
e41f0d80 6508
6509tree
e60a6f7b 6510c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
be23b16f 6511 tree low_value, tree high_value, bool *outside_range_p)
e41f0d80 6512{
6513 tree type;
6514 tree label;
6515 tree case_label;
6516 splay_tree_node node;
6517
6518 /* Create the LABEL_DECL itself. */
e60a6f7b 6519 label = create_artificial_label (loc);
e41f0d80 6520
6521 /* If there was an error processing the switch condition, bail now
6522 before we get more confused. */
6523 if (!cond || cond == error_mark_node)
4ee9c684 6524 goto error_out;
e41f0d80 6525
1cae46be 6526 if ((low_value && TREE_TYPE (low_value)
6527 && POINTER_TYPE_P (TREE_TYPE (low_value)))
e41f0d80 6528 || (high_value && TREE_TYPE (high_value)
6529 && POINTER_TYPE_P (TREE_TYPE (high_value))))
b96dc121 6530 {
e60a6f7b 6531 error_at (loc, "pointers are not permitted as case values");
b96dc121 6532 goto error_out;
6533 }
e41f0d80 6534
6535 /* Case ranges are a GNU extension. */
8864917d 6536 if (high_value)
29438999 6537 pedwarn (loc, OPT_Wpedantic,
8864917d 6538 "range expressions in switch statements are non-standard");
e41f0d80 6539
6540 type = TREE_TYPE (cond);
6541 if (low_value)
6542 {
2d2f6a15 6543 low_value = check_case_value (loc, low_value);
22a75734 6544 low_value = convert_and_check (loc, type, low_value);
96722196 6545 if (low_value == error_mark_node)
6546 goto error_out;
e41f0d80 6547 }
6548 if (high_value)
6549 {
2d2f6a15 6550 high_value = check_case_value (loc, high_value);
22a75734 6551 high_value = convert_and_check (loc, type, high_value);
96722196 6552 if (high_value == error_mark_node)
6553 goto error_out;
e41f0d80 6554 }
6555
96722196 6556 if (low_value && high_value)
6557 {
6558 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
a0c938f0 6559 really a case range, even though it was written that way.
6560 Remove the HIGH_VALUE to simplify later processing. */
96722196 6561 if (tree_int_cst_equal (low_value, high_value))
6562 high_value = NULL_TREE;
6563 else if (!tree_int_cst_lt (low_value, high_value))
e60a6f7b 6564 warning_at (loc, 0, "empty range specified");
96722196 6565 }
e41f0d80 6566
2ca392fd 6567 /* See if the case is in range of the type of the original testing
6568 expression. If both low_value and high_value are out of range,
6569 don't insert the case label and return NULL_TREE. */
6570 if (low_value
f61a9bc2 6571 && !check_case_bounds (loc, type, orig_type,
be23b16f 6572 &low_value, high_value ? &high_value : NULL,
6573 outside_range_p))
2ca392fd 6574 return NULL_TREE;
6575
e41f0d80 6576 /* Look up the LOW_VALUE in the table of case labels we already
6577 have. */
6578 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6579 /* If there was not an exact match, check for overlapping ranges.
6580 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6581 that's a `default' label and the only overlap is an exact match. */
6582 if (!node && (low_value || high_value))
6583 {
6584 splay_tree_node low_bound;
6585 splay_tree_node high_bound;
6586
6587 /* Even though there wasn't an exact match, there might be an
6588 overlap between this case range and another case range.
6589 Since we've (inductively) not allowed any overlapping case
6590 ranges, we simply need to find the greatest low case label
6591 that is smaller that LOW_VALUE, and the smallest low case
6592 label that is greater than LOW_VALUE. If there is an overlap
6593 it will occur in one of these two ranges. */
6594 low_bound = splay_tree_predecessor (cases,
6595 (splay_tree_key) low_value);
6596 high_bound = splay_tree_successor (cases,
6597 (splay_tree_key) low_value);
6598
6599 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6600 the LOW_VALUE, so there is no need to check unless the
6601 LOW_BOUND is in fact itself a case range. */
6602 if (low_bound
6603 && CASE_HIGH ((tree) low_bound->value)
6604 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6605 low_value) >= 0)
6606 node = low_bound;
6607 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6608 range is bigger than the low end of the current range, so we
6609 are only interested if the current range is a real range, and
6610 not an ordinary case label. */
1cae46be 6611 else if (high_bound
e41f0d80 6612 && high_value
6613 && (tree_int_cst_compare ((tree) high_bound->key,
6614 high_value)
6615 <= 0))
6616 node = high_bound;
6617 }
6618 /* If there was an overlap, issue an error. */
6619 if (node)
6620 {
eaae3b75 6621 tree duplicate = CASE_LABEL ((tree) node->value);
e41f0d80 6622
6623 if (high_value)
6624 {
e60a6f7b 6625 error_at (loc, "duplicate (or overlapping) case value");
6626 error_at (DECL_SOURCE_LOCATION (duplicate),
6627 "this is the first entry overlapping that value");
e41f0d80 6628 }
6629 else if (low_value)
6630 {
e60a6f7b 6631 error_at (loc, "duplicate case value") ;
6632 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
e41f0d80 6633 }
6634 else
6635 {
e60a6f7b 6636 error_at (loc, "multiple default labels in one switch");
6637 error_at (DECL_SOURCE_LOCATION (duplicate),
6638 "this is the first default label");
e41f0d80 6639 }
4ee9c684 6640 goto error_out;
e41f0d80 6641 }
6642
6643 /* Add a CASE_LABEL to the statement-tree. */
b6e3dd65 6644 case_label = add_stmt (build_case_label (low_value, high_value, label));
e41f0d80 6645 /* Register this case label in the splay tree. */
1cae46be 6646 splay_tree_insert (cases,
e41f0d80 6647 (splay_tree_key) low_value,
6648 (splay_tree_value) case_label);
6649
6650 return case_label;
4ee9c684 6651
6652 error_out:
daf6dff5 6653 /* Add a label so that the back-end doesn't think that the beginning of
4ee9c684 6654 the switch is unreachable. Note that we do not add a case label, as
a53ff4c1 6655 that just leads to duplicates and thence to failure later on. */
4ee9c684 6656 if (!cases->root)
6657 {
e60a6f7b 6658 tree t = create_artificial_label (loc);
6659 add_stmt (build_stmt (loc, LABEL_EXPR, t));
4ee9c684 6660 }
6661 return error_mark_node;
6662}
6663
6664/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6665 Used to verify that case values match up with enumerator values. */
6666
6667static void
6668match_case_to_enum_1 (tree key, tree type, tree label)
6669{
e913b5cd 6670 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6671
dd76621f 6672 if (tree_fits_uhwi_p (key))
6673 print_dec (key, buf, UNSIGNED);
6674 else if (tree_fits_shwi_p (key))
6675 print_dec (key, buf, SIGNED);
4ee9c684 6676 else
e913b5cd 6677 print_hex (key, buf);
4ee9c684 6678
6679 if (TYPE_NAME (type) == 0)
712d2297 6680 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6681 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6682 "case value %qs not in enumerated type",
6683 buf);
4ee9c684 6684 else
712d2297 6685 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6686 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6687 "case value %qs not in enumerated type %qT",
6688 buf, type);
4ee9c684 6689}
6690
359d87c6 6691/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6692 Used to verify that case values match up with enumerator values. */
6693
4ee9c684 6694static int
6695match_case_to_enum (splay_tree_node node, void *data)
6696{
6697 tree label = (tree) node->value;
4fd61bc6 6698 tree type = (tree) data;
4ee9c684 6699
6700 /* Skip default case. */
6701 if (!CASE_LOW (label))
6702 return 0;
6703
359d87c6 6704 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
4ee9c684 6705 when we did our enum->case scan. Reset our scratch bit after. */
359d87c6 6706 if (!CASE_LOW_SEEN (label))
4ee9c684 6707 match_case_to_enum_1 (CASE_LOW (label), type, label);
6708 else
359d87c6 6709 CASE_LOW_SEEN (label) = 0;
4ee9c684 6710
359d87c6 6711 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6712 not set, that means that CASE_HIGH did not appear when we did our
6713 enum->case scan. Reset our scratch bit after. */
4ee9c684 6714 if (CASE_HIGH (label))
6715 {
359d87c6 6716 if (!CASE_HIGH_SEEN (label))
6717 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6718 else
6719 CASE_HIGH_SEEN (label) = 0;
4ee9c684 6720 }
6721
6722 return 0;
6723}
6724
e7911019 6725/* Handle -Wswitch*. Called from the front end after parsing the
6726 switch construct. */
6727/* ??? Should probably be somewhere generic, since other languages
6728 besides C and C++ would want this. At the moment, however, C/C++
6729 are the only tree-ssa languages that support enumerations at all,
6730 so the point is moot. */
4ee9c684 6731
e7911019 6732void
6733c_do_switch_warnings (splay_tree cases, location_t switch_location,
be23b16f 6734 tree type, tree cond, bool bool_cond_p,
6735 bool outside_range_p)
4ee9c684 6736{
b27ac6b5 6737 splay_tree_node default_node;
359d87c6 6738 splay_tree_node node;
6739 tree chain;
4ee9c684 6740
be23b16f 6741 if (!warn_switch && !warn_switch_enum && !warn_switch_default
6742 && !warn_switch_bool)
4ee9c684 6743 return;
6744
4ee9c684 6745 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
8b6866af 6746 if (!default_node)
5fb6a912 6747 warning_at (switch_location, OPT_Wswitch_default,
6748 "switch missing default case");
4ee9c684 6749
be23b16f 6750 /* There are certain cases where -Wswitch-bool warnings aren't
6751 desirable, such as
6752 switch (boolean)
6753 {
6754 case true: ...
6755 case false: ...
6756 }
6757 so be careful here. */
6758 if (warn_switch_bool && bool_cond_p)
6759 {
6760 splay_tree_node min_node;
6761 /* If there's a default node, it's also the value with the minimal
6762 key. So look at the penultimate key (if any). */
6763 if (default_node)
6764 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
6765 else
6766 min_node = splay_tree_min (cases);
6767 tree min = min_node ? (tree) min_node->key : NULL_TREE;
6768
6769 splay_tree_node max_node = splay_tree_max (cases);
6770 /* This might be a case range, so look at the value with the
6771 maximal key and then check CASE_HIGH. */
6772 tree max = max_node ? (tree) max_node->value : NULL_TREE;
6773 if (max)
6774 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
6775
6776 /* If there's a case value > 1 or < 0, that is outside bool
6777 range, warn. */
6778 if (outside_range_p
6779 || (max && wi::gts_p (max, 1))
6780 || (min && wi::lts_p (min, 0))
6781 /* And handle the
6782 switch (boolean)
6783 {
6784 case true: ...
6785 case false: ...
6786 default: ...
6787 }
6788 case, where we want to warn. */
6789 || (default_node
6790 && max && wi::eq_p (max, 1)
6791 && min && wi::eq_p (min, 0)))
6792 warning_at (switch_location, OPT_Wswitch_bool,
6793 "switch condition has boolean value");
6794 }
6795
47ae02b7 6796 /* From here on, we only care about enumerated types. */
359d87c6 6797 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6798 return;
6799
561017b5 6800 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6801 if (!warn_switch_enum && !warn_switch)
359d87c6 6802 return;
6803
561017b5 6804 /* Check the cases. Warn about case values which are not members of
6805 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6806 there is no default case, check that exactly all enumeration
6807 literals are covered by the cases. */
6808
359d87c6 6809 /* Clearing COND if it is not an integer constant simplifies
6810 the tests inside the loop below. */
6811 if (TREE_CODE (cond) != INTEGER_CST)
6812 cond = NULL_TREE;
6813
6814 /* The time complexity here is O(N*lg(N)) worst case, but for the
6815 common case of monotonically increasing enumerators, it is
6816 O(N), since the nature of the splay tree will keep the next
6817 element adjacent to the root at all times. */
4ee9c684 6818
359d87c6 6819 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6820 {
6821 tree value = TREE_VALUE (chain);
3f00a6c0 6822 if (TREE_CODE (value) == CONST_DECL)
6823 value = DECL_INITIAL (value);
359d87c6 6824 node = splay_tree_lookup (cases, (splay_tree_key) value);
6825 if (node)
4ee9c684 6826 {
359d87c6 6827 /* Mark the CASE_LOW part of the case entry as seen. */
6828 tree label = (tree) node->value;
6829 CASE_LOW_SEEN (label) = 1;
6830 continue;
6831 }
6832
6833 /* Even though there wasn't an exact match, there might be a
f0b5f617 6834 case range which includes the enumerator's value. */
359d87c6 6835 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6836 if (node && CASE_HIGH ((tree) node->value))
6837 {
6838 tree label = (tree) node->value;
6839 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6840 if (cmp >= 0)
4ee9c684 6841 {
359d87c6 6842 /* If we match the upper bound exactly, mark the CASE_HIGH
6843 part of the case entry as seen. */
6844 if (cmp == 0)
6845 CASE_HIGH_SEEN (label) = 1;
6846 continue;
4ee9c684 6847 }
6848 }
6849
359d87c6 6850 /* We've now determined that this enumerated literal isn't
6851 handled by the case labels of the switch statement. */
4ee9c684 6852
359d87c6 6853 /* If the switch expression is a constant, we only really care
6854 about whether that constant is handled by the switch. */
6855 if (cond && tree_int_cst_compare (cond, value))
6856 continue;
4ee9c684 6857
6cbbbc89 6858 /* If there is a default_node, the only relevant option is
561017b5 6859 Wswitch-enum. Otherwise, if both are enabled then we prefer
6cbbbc89 6860 to warn using -Wswitch because -Wswitch is enabled by -Wall
6861 while -Wswitch-enum is explicit. */
561017b5 6862 warning_at (switch_location,
6863 (default_node || !warn_switch
6864 ? OPT_Wswitch_enum
6865 : OPT_Wswitch),
6866 "enumeration value %qE not handled in switch",
6867 TREE_PURPOSE (chain));
4ee9c684 6868 }
359d87c6 6869
6870 /* Warn if there are case expressions that don't correspond to
6871 enumerators. This can occur since C and C++ don't enforce
6872 type-checking of assignments to enumeration variables.
6873
6874 The time complexity here is now always O(N) worst case, since
6875 we should have marked both the lower bound and upper bound of
6876 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6877 above. This scan also resets those fields. */
6cbbbc89 6878
359d87c6 6879 splay_tree_foreach (cases, match_case_to_enum, type);
e41f0d80 6880}
6881
9dd48740 6882/* Finish an expression taking the address of LABEL (an
dda49785 6883 IDENTIFIER_NODE). Returns an expression for the address.
6884
6885 LOC is the location for the expression returned. */
d0a47c8d 6886
1cae46be 6887tree
dda49785 6888finish_label_address_expr (tree label, location_t loc)
d0a47c8d 6889{
6890 tree result;
6891
29438999 6892 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
d0a47c8d 6893
9dd48740 6894 if (label == error_mark_node)
6895 return error_mark_node;
6896
d0a47c8d 6897 label = lookup_label (label);
6898 if (label == NULL_TREE)
6899 result = null_pointer_node;
6900 else
6901 {
6902 TREE_USED (label) = 1;
6903 result = build1 (ADDR_EXPR, ptr_type_node, label);
344cd9b2 6904 /* The current function is not necessarily uninlinable.
d0a47c8d 6905 Computed gotos are incompatible with inlining, but the value
6906 here could be used only in a diagnostic, for example. */
dda49785 6907 protected_set_expr_location (result, loc);
d0a47c8d 6908 }
6909
6910 return result;
6911}
4f9a1c9b 6912\f
6913
6914/* Given a boolean expression ARG, return a tree representing an increment
6915 or decrement (as indicated by CODE) of ARG. The front end must check for
6916 invalid cases (e.g., decrement in C++). */
6917tree
1cae46be 6918boolean_increment (enum tree_code code, tree arg)
4f9a1c9b 6919{
6920 tree val;
69db191c 6921 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
c0f19401 6922
4f9a1c9b 6923 arg = stabilize_reference (arg);
6924 switch (code)
6925 {
6926 case PREINCREMENT_EXPR:
14ae0310 6927 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6928 break;
6929 case POSTINCREMENT_EXPR:
14ae0310 6930 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6931 arg = save_expr (arg);
14ae0310 6932 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6933 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6934 break;
6935 case PREDECREMENT_EXPR:
14ae0310 6936 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6937 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6938 break;
6939 case POSTDECREMENT_EXPR:
14ae0310 6940 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6941 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6942 arg = save_expr (arg);
14ae0310 6943 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6944 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6945 break;
6946 default:
231bd014 6947 gcc_unreachable ();
4f9a1c9b 6948 }
6949 TREE_SIDE_EFFECTS (val) = 1;
6950 return val;
6951}
76a6e674 6952\f
f3449a3c 6953/* Built-in macros for stddef.h and stdint.h, that require macros
6954 defined in this file. */
79cf3ec1 6955void
1cae46be 6956c_stddef_cpp_builtins(void)
1ed9d5f5 6957{
63994318 6958 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6959 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6960 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6961 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
36bccbfc 6962 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6963 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
b0726616 6964 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6965 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
f3449a3c 6966 if (SIG_ATOMIC_TYPE)
6967 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6968 if (INT8_TYPE)
6969 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6970 if (INT16_TYPE)
6971 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6972 if (INT32_TYPE)
6973 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6974 if (INT64_TYPE)
6975 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6976 if (UINT8_TYPE)
6977 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6978 if (UINT16_TYPE)
6979 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6980 if (UINT32_TYPE)
6981 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6982 if (UINT64_TYPE)
6983 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6984 if (INT_LEAST8_TYPE)
6985 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6986 if (INT_LEAST16_TYPE)
6987 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6988 if (INT_LEAST32_TYPE)
6989 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6990 if (INT_LEAST64_TYPE)
6991 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6992 if (UINT_LEAST8_TYPE)
6993 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6994 if (UINT_LEAST16_TYPE)
6995 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6996 if (UINT_LEAST32_TYPE)
6997 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6998 if (UINT_LEAST64_TYPE)
6999 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
7000 if (INT_FAST8_TYPE)
7001 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
7002 if (INT_FAST16_TYPE)
7003 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
7004 if (INT_FAST32_TYPE)
7005 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
7006 if (INT_FAST64_TYPE)
7007 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
7008 if (UINT_FAST8_TYPE)
7009 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
7010 if (UINT_FAST16_TYPE)
7011 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
7012 if (UINT_FAST32_TYPE)
7013 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
7014 if (UINT_FAST64_TYPE)
7015 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
7016 if (INTPTR_TYPE)
7017 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
7018 if (UINTPTR_TYPE)
7019 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
574006c3 7020}
7021
7d3b509a 7022static void
1cae46be 7023c_init_attributes (void)
7d3b509a 7024{
7025 /* Fill in the built_in_attributes array. */
7c446c95 7026#define DEF_ATTR_NULL_TREE(ENUM) \
7d3b509a 7027 built_in_attributes[(int) ENUM] = NULL_TREE;
7c446c95 7028#define DEF_ATTR_INT(ENUM, VALUE) \
ceb7b692 7029 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
c8010b80 7030#define DEF_ATTR_STRING(ENUM, VALUE) \
7031 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
7d3b509a 7032#define DEF_ATTR_IDENT(ENUM, STRING) \
7033 built_in_attributes[(int) ENUM] = get_identifier (STRING);
7034#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
7035 built_in_attributes[(int) ENUM] \
7036 = tree_cons (built_in_attributes[(int) PURPOSE], \
7037 built_in_attributes[(int) VALUE], \
7038 built_in_attributes[(int) CHAIN]);
7d3b509a 7039#include "builtin-attrs.def"
7040#undef DEF_ATTR_NULL_TREE
7041#undef DEF_ATTR_INT
7042#undef DEF_ATTR_IDENT
7043#undef DEF_ATTR_TREE_LIST
76a6e674 7044}
5f3cead1 7045
33199a81 7046/* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
7047 identifier as an argument, so the front end shouldn't look it up. */
7048
7049bool
47b19b94 7050attribute_takes_identifier_p (const_tree attr_id)
33199a81 7051{
9bf1c74e 7052 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
02cb1060 7053 if (spec == NULL)
7054 /* Unknown attribute that we'll end up ignoring, return true so we
7055 don't complain about an identifier argument. */
7056 return true;
7057 else if (!strcmp ("mode", spec->name)
7058 || !strcmp ("format", spec->name)
7059 || !strcmp ("cleanup", spec->name))
47b19b94 7060 return true;
7061 else
7062 return targetm.attribute_takes_identifier_p (attr_id);
33199a81 7063}
7064
f8e93a2e 7065/* Attribute handlers common to C front ends. */
7066
7067/* Handle a "packed" attribute; arguments as in
7068 struct attribute_spec.handler. */
7069
7070static tree
9a03a746 7071handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
09347743 7072 int flags, bool *no_add_attrs)
f8e93a2e 7073{
f40175cb 7074 if (TYPE_P (*node))
f8e93a2e 7075 {
7076 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 7077 *node = build_variant_type_copy (*node);
f40175cb 7078 TYPE_PACKED (*node) = 1;
f8e93a2e 7079 }
7080 else if (TREE_CODE (*node) == FIELD_DECL)
c2ab04f9 7081 {
9fd767c5 7082 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
7083 /* Still pack bitfields. */
7084 && ! DECL_INITIAL (*node))
c2ab04f9 7085 warning (OPT_Wattributes,
7086 "%qE attribute ignored for field of type %qT",
7087 name, TREE_TYPE (*node));
7088 else
7089 DECL_PACKED (*node) = 1;
7090 }
f8e93a2e 7091 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
f40175cb 7092 used for DECL_REGISTER. It wouldn't mean anything anyway.
7093 We can't set DECL_PACKED on the type of a TYPE_DECL, because
7094 that changes what the typedef is typing. */
f8e93a2e 7095 else
7096 {
9b2d6d13 7097 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7098 *no_add_attrs = true;
7099 }
7100
7101 return NULL_TREE;
7102}
7103
7104/* Handle a "nocommon" attribute; arguments as in
7105 struct attribute_spec.handler. */
7106
7107static tree
1cae46be 7108handle_nocommon_attribute (tree *node, tree name,
9a03a746 7109 tree ARG_UNUSED (args),
7110 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7111{
f48c7f4a 7112 if (VAR_P (*node))
f8e93a2e 7113 DECL_COMMON (*node) = 0;
7114 else
7115 {
9b2d6d13 7116 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7117 *no_add_attrs = true;
7118 }
7119
7120 return NULL_TREE;
7121}
7122
7123/* Handle a "common" attribute; arguments as in
7124 struct attribute_spec.handler. */
7125
7126static tree
9a03a746 7127handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7128 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7129{
f48c7f4a 7130 if (VAR_P (*node))
f8e93a2e 7131 DECL_COMMON (*node) = 1;
7132 else
7133 {
9b2d6d13 7134 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7135 *no_add_attrs = true;
7136 }
7137
7138 return NULL_TREE;
7139}
7140
7141/* Handle a "noreturn" attribute; arguments as in
7142 struct attribute_spec.handler. */
7143
7144static tree
9a03a746 7145handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7146 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7147{
7148 tree type = TREE_TYPE (*node);
7149
7150 /* See FIXME comment in c_common_attribute_table. */
8c582e4f 7151 if (TREE_CODE (*node) == FUNCTION_DECL
7152 || objc_method_decl (TREE_CODE (*node)))
f8e93a2e 7153 TREE_THIS_VOLATILE (*node) = 1;
7154 else if (TREE_CODE (type) == POINTER_TYPE
7155 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7156 TREE_TYPE (*node)
9d4eeb52 7157 = (build_qualified_type
7158 (build_pointer_type
7159 (build_type_variant (TREE_TYPE (type),
7160 TYPE_READONLY (TREE_TYPE (type)), 1)),
7161 TYPE_QUALS (type)));
f8e93a2e 7162 else
7163 {
9b2d6d13 7164 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7165 *no_add_attrs = true;
7166 }
7167
7168 return NULL_TREE;
7169}
7170
5de92639 7171/* Handle a "hot" and attribute; arguments as in
7172 struct attribute_spec.handler. */
7173
7174static tree
7175handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
46f8e3b0 7176 int ARG_UNUSED (flags), bool *no_add_attrs)
5de92639 7177{
758a38ab 7178 if (TREE_CODE (*node) == FUNCTION_DECL
7179 || TREE_CODE (*node) == LABEL_DECL)
5de92639 7180 {
7181 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
7182 {
4a026b48 7183 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7184 "with attribute %qs", name, "cold");
5de92639 7185 *no_add_attrs = true;
7186 }
24470055 7187 /* Most of the rest of the hot processing is done later with
7188 lookup_attribute. */
5de92639 7189 }
7190 else
7191 {
7192 warning (OPT_Wattributes, "%qE attribute ignored", name);
7193 *no_add_attrs = true;
7194 }
7195
7196 return NULL_TREE;
7197}
758a38ab 7198
5de92639 7199/* Handle a "cold" and attribute; arguments as in
7200 struct attribute_spec.handler. */
7201
7202static tree
7203handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7204 int ARG_UNUSED (flags), bool *no_add_attrs)
7205{
758a38ab 7206 if (TREE_CODE (*node) == FUNCTION_DECL
7207 || TREE_CODE (*node) == LABEL_DECL)
5de92639 7208 {
7209 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
7210 {
4a026b48 7211 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7212 "with attribute %qs", name, "hot");
5de92639 7213 *no_add_attrs = true;
7214 }
24470055 7215 /* Most of the rest of the cold processing is done later with
7216 lookup_attribute. */
5de92639 7217 }
7218 else
7219 {
7220 warning (OPT_Wattributes, "%qE attribute ignored", name);
7221 *no_add_attrs = true;
7222 }
7223
7224 return NULL_TREE;
7225}
7226
a9196da9 7227/* Handle a "no_sanitize_address" attribute; arguments as in
d413ffdd 7228 struct attribute_spec.handler. */
7229
7230static tree
a9196da9 7231handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
7232 bool *no_add_attrs)
d413ffdd 7233{
7234 if (TREE_CODE (*node) != FUNCTION_DECL)
7235 {
7236 warning (OPT_Wattributes, "%qE attribute ignored", name);
7237 *no_add_attrs = true;
7238 }
7239
7240 return NULL_TREE;
7241}
7242
a9196da9 7243/* Handle a "no_address_safety_analysis" attribute; arguments as in
7244 struct attribute_spec.handler. */
7245
7246static tree
7247handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
7248 bool *no_add_attrs)
7249{
7250 if (TREE_CODE (*node) != FUNCTION_DECL)
7251 warning (OPT_Wattributes, "%qE attribute ignored", name);
7252 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
7253 DECL_ATTRIBUTES (*node)
7254 = tree_cons (get_identifier ("no_sanitize_address"),
7255 NULL_TREE, DECL_ATTRIBUTES (*node));
7256 *no_add_attrs = true;
7257 return NULL_TREE;
7258}
7259
05f893e1 7260/* Handle a "no_sanitize_undefined" attribute; arguments as in
7261 struct attribute_spec.handler. */
7262
7263static tree
7264handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
7265 bool *no_add_attrs)
7266{
7267 if (TREE_CODE (*node) != FUNCTION_DECL)
7268 {
7269 warning (OPT_Wattributes, "%qE attribute ignored", name);
7270 *no_add_attrs = true;
7271 }
7272
7273 return NULL_TREE;
7274}
7275
947aa916 7276/* Handle a "stack_protect" attribute; arguments as in
7277 struct attribute_spec.handler. */
7278static tree
7279handle_stack_protect_attribute (tree *node, tree name, tree, int,
7280 bool *no_add_attrs)
7281{
7282 if (TREE_CODE (*node) != FUNCTION_DECL)
7283 {
7284 warning (OPT_Wattributes, "%qE attribute ignored", name);
7285 *no_add_attrs = true;
7286 }
7287 else
7288 DECL_ATTRIBUTES (*node)
7289 = tree_cons (get_identifier ("stack_protect"),
7290 NULL_TREE, DECL_ATTRIBUTES (*node));
7291
7292 return NULL_TREE;
7293}
7294
f8e93a2e 7295/* Handle a "noinline" attribute; arguments as in
7296 struct attribute_spec.handler. */
7297
7298static tree
1cae46be 7299handle_noinline_attribute (tree *node, tree name,
9a03a746 7300 tree ARG_UNUSED (args),
7301 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7302{
7303 if (TREE_CODE (*node) == FUNCTION_DECL)
4a026b48 7304 {
7305 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
7306 {
7307 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7308 "with attribute %qs", name, "always_inline");
7309 *no_add_attrs = true;
7310 }
7311 else
7312 DECL_UNINLINABLE (*node) = 1;
7313 }
f8e93a2e 7314 else
7315 {
9b2d6d13 7316 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7317 *no_add_attrs = true;
7318 }
7319
7320 return NULL_TREE;
7321}
7322
bdb1f0d1 7323/* Handle a "noclone" attribute; arguments as in
7324 struct attribute_spec.handler. */
7325
7326static tree
7327handle_noclone_attribute (tree *node, tree name,
7328 tree ARG_UNUSED (args),
7329 int ARG_UNUSED (flags), bool *no_add_attrs)
7330{
7331 if (TREE_CODE (*node) != FUNCTION_DECL)
7332 {
7333 warning (OPT_Wattributes, "%qE attribute ignored", name);
7334 *no_add_attrs = true;
7335 }
7336
7337 return NULL_TREE;
7338}
7339
85fbea97 7340/* Handle a "no_icf" attribute; arguments as in
7341 struct attribute_spec.handler. */
7342
7343static tree
7344handle_noicf_attribute (tree *node, tree name,
7345 tree ARG_UNUSED (args),
7346 int ARG_UNUSED (flags), bool *no_add_attrs)
7347{
7348 if (TREE_CODE (*node) != FUNCTION_DECL)
7349 {
7350 warning (OPT_Wattributes, "%qE attribute ignored", name);
7351 *no_add_attrs = true;
7352 }
7353
7354 return NULL_TREE;
7355}
7356
7357
f8e93a2e 7358/* Handle a "always_inline" attribute; arguments as in
7359 struct attribute_spec.handler. */
7360
7361static tree
1cae46be 7362handle_always_inline_attribute (tree *node, tree name,
9a03a746 7363 tree ARG_UNUSED (args),
7364 int ARG_UNUSED (flags),
09347743 7365 bool *no_add_attrs)
f8e93a2e 7366{
7367 if (TREE_CODE (*node) == FUNCTION_DECL)
7368 {
4a026b48 7369 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
7370 {
7371 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7372 "with %qs attribute", name, "noinline");
7373 *no_add_attrs = true;
7374 }
7375 else
7376 /* Set the attribute and mark it for disregarding inline
7377 limits. */
7378 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
f8e93a2e 7379 }
7380 else
7381 {
9b2d6d13 7382 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7383 *no_add_attrs = true;
541e4101 7384 }
7385
7386 return NULL_TREE;
7387}
7388
7389/* Handle a "gnu_inline" attribute; arguments as in
7390 struct attribute_spec.handler. */
7391
7392static tree
7393handle_gnu_inline_attribute (tree *node, tree name,
7394 tree ARG_UNUSED (args),
7395 int ARG_UNUSED (flags),
7396 bool *no_add_attrs)
7397{
7398 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7399 {
7400 /* Do nothing else, just set the attribute. We'll get at
7401 it later with lookup_attribute. */
7402 }
7403 else
7404 {
7405 warning (OPT_Wattributes, "%qE attribute ignored", name);
7406 *no_add_attrs = true;
7bd95dfd 7407 }
7408
7409 return NULL_TREE;
7410}
7411
7412/* Handle a "leaf" attribute; arguments as in
7413 struct attribute_spec.handler. */
7414
7415static tree
7416handle_leaf_attribute (tree *node, tree name,
7417 tree ARG_UNUSED (args),
7418 int ARG_UNUSED (flags), bool *no_add_attrs)
7419{
7420 if (TREE_CODE (*node) != FUNCTION_DECL)
7421 {
7422 warning (OPT_Wattributes, "%qE attribute ignored", name);
7423 *no_add_attrs = true;
7424 }
7425 if (!TREE_PUBLIC (*node))
7426 {
7427 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
7428 *no_add_attrs = true;
f8e93a2e 7429 }
7430
7431 return NULL_TREE;
7432}
7433
1b16fc45 7434/* Handle an "artificial" attribute; arguments as in
7435 struct attribute_spec.handler. */
7436
7437static tree
7438handle_artificial_attribute (tree *node, tree name,
7439 tree ARG_UNUSED (args),
7440 int ARG_UNUSED (flags),
7441 bool *no_add_attrs)
7442{
7443 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7444 {
7445 /* Do nothing else, just set the attribute. We'll get at
7446 it later with lookup_attribute. */
7447 }
7448 else
7449 {
7450 warning (OPT_Wattributes, "%qE attribute ignored", name);
7451 *no_add_attrs = true;
7452 }
7453
7454 return NULL_TREE;
7455}
7456
0cdd9887 7457/* Handle a "flatten" attribute; arguments as in
7458 struct attribute_spec.handler. */
7459
7460static tree
7461handle_flatten_attribute (tree *node, tree name,
a0c938f0 7462 tree args ATTRIBUTE_UNUSED,
7463 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
0cdd9887 7464{
7465 if (TREE_CODE (*node) == FUNCTION_DECL)
7466 /* Do nothing else, just set the attribute. We'll get at
7467 it later with lookup_attribute. */
7468 ;
7469 else
7470 {
7471 warning (OPT_Wattributes, "%qE attribute ignored", name);
7472 *no_add_attrs = true;
7473 }
7474
7475 return NULL_TREE;
7476}
7477
10fc867f 7478/* Handle a "warning" or "error" attribute; arguments as in
7479 struct attribute_spec.handler. */
7480
7481static tree
7482handle_error_attribute (tree *node, tree name, tree args,
7483 int ARG_UNUSED (flags), bool *no_add_attrs)
7484{
7485 if (TREE_CODE (*node) == FUNCTION_DECL
df936998 7486 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
10fc867f 7487 /* Do nothing else, just set the attribute. We'll get at
7488 it later with lookup_attribute. */
7489 ;
7490 else
7491 {
7492 warning (OPT_Wattributes, "%qE attribute ignored", name);
7493 *no_add_attrs = true;
7494 }
7495
7496 return NULL_TREE;
7497}
0cdd9887 7498
f8e93a2e 7499/* Handle a "used" attribute; arguments as in
7500 struct attribute_spec.handler. */
7501
7502static tree
9a03a746 7503handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7504 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7505{
d0a31bd8 7506 tree node = *pnode;
7507
7508 if (TREE_CODE (node) == FUNCTION_DECL
f48c7f4a 7509 || (VAR_P (node) && TREE_STATIC (node))
a4e3ffad 7510 || (TREE_CODE (node) == TYPE_DECL))
f54ed8bc 7511 {
f54ed8bc 7512 TREE_USED (node) = 1;
9423c9b7 7513 DECL_PRESERVE_P (node) = 1;
f48c7f4a 7514 if (VAR_P (node))
abc6c64f 7515 DECL_READ_P (node) = 1;
f54ed8bc 7516 }
f8e93a2e 7517 else
7518 {
9b2d6d13 7519 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7520 *no_add_attrs = true;
7521 }
7522
7523 return NULL_TREE;
7524}
7525
7526/* Handle a "unused" attribute; arguments as in
7527 struct attribute_spec.handler. */
7528
7529static tree
9a03a746 7530handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7531 int flags, bool *no_add_attrs)
f8e93a2e 7532{
7533 if (DECL_P (*node))
7534 {
7535 tree decl = *node;
7536
7537 if (TREE_CODE (decl) == PARM_DECL
4e81b384 7538 || VAR_OR_FUNCTION_DECL_P (decl)
f8e93a2e 7539 || TREE_CODE (decl) == LABEL_DECL
7540 || TREE_CODE (decl) == TYPE_DECL)
abc6c64f 7541 {
7542 TREE_USED (decl) = 1;
f48c7f4a 7543 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
abc6c64f 7544 DECL_READ_P (decl) = 1;
7545 }
f8e93a2e 7546 else
7547 {
9b2d6d13 7548 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7549 *no_add_attrs = true;
7550 }
7551 }
7552 else
7553 {
7554 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 7555 *node = build_variant_type_copy (*node);
f8e93a2e 7556 TREE_USED (*node) = 1;
7557 }
7558
7559 return NULL_TREE;
7560}
7561
62eec3b4 7562/* Handle a "externally_visible" attribute; arguments as in
7563 struct attribute_spec.handler. */
7564
7565static tree
7566handle_externally_visible_attribute (tree *pnode, tree name,
7567 tree ARG_UNUSED (args),
7568 int ARG_UNUSED (flags),
7569 bool *no_add_attrs)
7570{
7571 tree node = *pnode;
7572
b443c459 7573 if (VAR_OR_FUNCTION_DECL_P (node))
62eec3b4 7574 {
ba12ea31 7575 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7576 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7577 {
7578 warning (OPT_Wattributes,
7579 "%qE attribute have effect only on public objects", name);
7580 *no_add_attrs = true;
7581 }
62eec3b4 7582 }
62eec3b4 7583 else
7584 {
7585 warning (OPT_Wattributes, "%qE attribute ignored", name);
7586 *no_add_attrs = true;
7587 }
7588
7589 return NULL_TREE;
7590}
7591
6b722052 7592/* Handle the "no_reorder" attribute. Arguments as in
7593 struct attribute_spec.handler. */
7594
7595static tree
7596handle_no_reorder_attribute (tree *pnode,
7597 tree name,
7598 tree,
7599 int,
7600 bool *no_add_attrs)
7601{
7602 tree node = *pnode;
7603
b443c459 7604 if (!VAR_OR_FUNCTION_DECL_P (node)
6b722052 7605 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7606 {
7607 warning (OPT_Wattributes,
7608 "%qE attribute only affects top level objects",
7609 name);
7610 *no_add_attrs = true;
7611 }
7612
7613 return NULL_TREE;
7614}
7615
f8e93a2e 7616/* Handle a "const" attribute; arguments as in
7617 struct attribute_spec.handler. */
7618
7619static tree
9a03a746 7620handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7621 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7622{
7623 tree type = TREE_TYPE (*node);
7624
7625 /* See FIXME comment on noreturn in c_common_attribute_table. */
7626 if (TREE_CODE (*node) == FUNCTION_DECL)
7627 TREE_READONLY (*node) = 1;
7628 else if (TREE_CODE (type) == POINTER_TYPE
7629 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7630 TREE_TYPE (*node)
9d4eeb52 7631 = (build_qualified_type
7632 (build_pointer_type
7633 (build_type_variant (TREE_TYPE (type), 1,
7634 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7635 TYPE_QUALS (type)));
f8e93a2e 7636 else
7637 {
9b2d6d13 7638 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7639 *no_add_attrs = true;
7640 }
7641
7642 return NULL_TREE;
7643}
7644
7645/* Handle a "transparent_union" attribute; arguments as in
7646 struct attribute_spec.handler. */
7647
7648static tree
1cae46be 7649handle_transparent_union_attribute (tree *node, tree name,
9a03a746 7650 tree ARG_UNUSED (args), int flags,
09347743 7651 bool *no_add_attrs)
f8e93a2e 7652{
881eb642 7653 tree type;
03908818 7654
7655 *no_add_attrs = true;
f8e93a2e 7656
ffcdbf9c 7657
7658 if (TREE_CODE (*node) == TYPE_DECL
7659 && ! (flags & ATTR_FLAG_CXX11))
881eb642 7660 node = &TREE_TYPE (*node);
7661 type = *node;
f8e93a2e 7662
03908818 7663 if (TREE_CODE (type) == UNION_TYPE)
f8e93a2e 7664 {
fca86134 7665 /* Make sure that the first field will work for a transparent union.
7666 If the type isn't complete yet, leave the check to the code in
7667 finish_struct. */
7668 if (TYPE_SIZE (type))
7669 {
7670 tree first = first_field (type);
7671 if (first == NULL_TREE
7672 || DECL_ARTIFICIAL (first)
7673 || TYPE_MODE (type) != DECL_MODE (first))
7674 goto ignored;
7675 }
7676
f8e93a2e 7677 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
03908818 7678 {
fca86134 7679 /* If the type isn't complete yet, setting the flag
7680 on a variant wouldn't ever be checked. */
7681 if (!TYPE_SIZE (type))
7682 goto ignored;
7683
7684 /* build_duplicate_type doesn't work for C++. */
7685 if (c_dialect_cxx ())
03908818 7686 goto ignored;
7687
7688 /* A type variant isn't good enough, since we don't a cast
7689 to such a type removed as a no-op. */
7690 *node = type = build_duplicate_type (type);
7691 }
7692
8df5a43d 7693 TYPE_TRANSPARENT_AGGR (type) = 1;
03908818 7694 return NULL_TREE;
f8e93a2e 7695 }
7696
03908818 7697 ignored:
7698 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7699 return NULL_TREE;
7700}
7701
9af7fd5b 7702/* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7703 get the requested priority for a constructor or destructor,
7704 possibly issuing diagnostics for invalid or reserved
7705 priorities. */
7706
7707static priority_type
7708get_priority (tree args, bool is_destructor)
7709{
7710 HOST_WIDE_INT pri;
6c181a06 7711 tree arg;
9af7fd5b 7712
7713 if (!args)
7714 return DEFAULT_INIT_PRIORITY;
48e1416a 7715
28fbc04f 7716 if (!SUPPORTS_INIT_PRIORITY)
7717 {
7718 if (is_destructor)
7719 error ("destructor priorities are not supported");
7720 else
7721 error ("constructor priorities are not supported");
7722 return DEFAULT_INIT_PRIORITY;
7723 }
7724
6c181a06 7725 arg = TREE_VALUE (args);
253e1cae 7726 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7727 goto invalid;
7728 if (arg == error_mark_node)
7729 return DEFAULT_INIT_PRIORITY;
c28ddc97 7730 arg = default_conversion (arg);
e913b5cd 7731 if (!tree_fits_shwi_p (arg)
6c181a06 7732 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
9af7fd5b 7733 goto invalid;
7734
e913b5cd 7735 pri = tree_to_shwi (arg);
9af7fd5b 7736 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7737 goto invalid;
7738
7739 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7740 {
7741 if (is_destructor)
7742 warning (0,
7743 "destructor priorities from 0 to %d are reserved "
48e1416a 7744 "for the implementation",
9af7fd5b 7745 MAX_RESERVED_INIT_PRIORITY);
7746 else
7747 warning (0,
7748 "constructor priorities from 0 to %d are reserved "
48e1416a 7749 "for the implementation",
9af7fd5b 7750 MAX_RESERVED_INIT_PRIORITY);
7751 }
7752 return pri;
7753
7754 invalid:
7755 if (is_destructor)
7756 error ("destructor priorities must be integers from 0 to %d inclusive",
7757 MAX_INIT_PRIORITY);
7758 else
7759 error ("constructor priorities must be integers from 0 to %d inclusive",
7760 MAX_INIT_PRIORITY);
7761 return DEFAULT_INIT_PRIORITY;
7762}
7763
f8e93a2e 7764/* Handle a "constructor" attribute; arguments as in
7765 struct attribute_spec.handler. */
7766
7767static tree
9af7fd5b 7768handle_constructor_attribute (tree *node, tree name, tree args,
9a03a746 7769 int ARG_UNUSED (flags),
09347743 7770 bool *no_add_attrs)
f8e93a2e 7771{
7772 tree decl = *node;
7773 tree type = TREE_TYPE (decl);
7774
7775 if (TREE_CODE (decl) == FUNCTION_DECL
7776 && TREE_CODE (type) == FUNCTION_TYPE
7777 && decl_function_context (decl) == 0)
7778 {
9af7fd5b 7779 priority_type priority;
f8e93a2e 7780 DECL_STATIC_CONSTRUCTOR (decl) = 1;
9af7fd5b 7781 priority = get_priority (args, /*is_destructor=*/false);
7782 SET_DECL_INIT_PRIORITY (decl, priority);
f8e93a2e 7783 TREE_USED (decl) = 1;
7784 }
7785 else
7786 {
9b2d6d13 7787 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7788 *no_add_attrs = true;
7789 }
7790
7791 return NULL_TREE;
7792}
7793
7794/* Handle a "destructor" attribute; arguments as in
7795 struct attribute_spec.handler. */
7796
7797static tree
9af7fd5b 7798handle_destructor_attribute (tree *node, tree name, tree args,
9a03a746 7799 int ARG_UNUSED (flags),
09347743 7800 bool *no_add_attrs)
f8e93a2e 7801{
7802 tree decl = *node;
7803 tree type = TREE_TYPE (decl);
7804
7805 if (TREE_CODE (decl) == FUNCTION_DECL
7806 && TREE_CODE (type) == FUNCTION_TYPE
7807 && decl_function_context (decl) == 0)
7808 {
9af7fd5b 7809 priority_type priority;
f8e93a2e 7810 DECL_STATIC_DESTRUCTOR (decl) = 1;
9af7fd5b 7811 priority = get_priority (args, /*is_destructor=*/true);
7812 SET_DECL_FINI_PRIORITY (decl, priority);
f8e93a2e 7813 TREE_USED (decl) = 1;
7814 }
7815 else
7816 {
9b2d6d13 7817 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7818 *no_add_attrs = true;
7819 }
7820
7821 return NULL_TREE;
7822}
7823
1c58e3f1 7824/* Nonzero if the mode is a valid vector mode for this architecture.
7825 This returns nonzero even if there is no hardware support for the
7826 vector mode, but we can emulate with narrower modes. */
7827
7828static int
3754d046 7829vector_mode_valid_p (machine_mode mode)
1c58e3f1 7830{
7831 enum mode_class mclass = GET_MODE_CLASS (mode);
3754d046 7832 machine_mode innermode;
1c58e3f1 7833
7834 /* Doh! What's going on? */
7835 if (mclass != MODE_VECTOR_INT
7836 && mclass != MODE_VECTOR_FLOAT
7837 && mclass != MODE_VECTOR_FRACT
7838 && mclass != MODE_VECTOR_UFRACT
7839 && mclass != MODE_VECTOR_ACCUM
7840 && mclass != MODE_VECTOR_UACCUM)
7841 return 0;
7842
7843 /* Hardware support. Woo hoo! */
7844 if (targetm.vector_mode_supported_p (mode))
7845 return 1;
7846
7847 innermode = GET_MODE_INNER (mode);
7848
7849 /* We should probably return 1 if requesting V4DI and we have no DI,
7850 but we have V2DI, but this is probably very unlikely. */
7851
7852 /* If we have support for the inner mode, we can safely emulate it.
7853 We may not have V2DI, but me can emulate with a pair of DIs. */
7854 return targetm.scalar_mode_supported_p (innermode);
7855}
7856
7857
f8e93a2e 7858/* Handle a "mode" attribute; arguments as in
7859 struct attribute_spec.handler. */
7860
7861static tree
9a03a746 7862handle_mode_attribute (tree *node, tree name, tree args,
7863 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7864{
7865 tree type = *node;
d1dd9ac0 7866 tree ident = TREE_VALUE (args);
f8e93a2e 7867
7868 *no_add_attrs = true;
ab2c1de8 7869
d1dd9ac0 7870 if (TREE_CODE (ident) != IDENTIFIER_NODE)
9b2d6d13 7871 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7872 else
7873 {
7874 int j;
d1dd9ac0 7875 const char *p = IDENTIFIER_POINTER (ident);
f8e93a2e 7876 int len = strlen (p);
3754d046 7877 machine_mode mode = VOIDmode;
f8e93a2e 7878 tree typefm;
b2aef146 7879 bool valid_mode;
f8e93a2e 7880
7881 if (len > 4 && p[0] == '_' && p[1] == '_'
7882 && p[len - 1] == '_' && p[len - 2] == '_')
7883 {
4fd61bc6 7884 char *newp = (char *) alloca (len - 1);
f8e93a2e 7885
7886 strcpy (newp, &p[2]);
7887 newp[len - 4] = '\0';
7888 p = newp;
7889 }
7890
7891 /* Change this type to have a type with the specified mode.
7892 First check for the special modes. */
84166705 7893 if (!strcmp (p, "byte"))
f8e93a2e 7894 mode = byte_mode;
7895 else if (!strcmp (p, "word"))
7896 mode = word_mode;
84166705 7897 else if (!strcmp (p, "pointer"))
f8e93a2e 7898 mode = ptr_mode;
0ef89dfd 7899 else if (!strcmp (p, "libgcc_cmp_return"))
7900 mode = targetm.libgcc_cmp_return_mode ();
7901 else if (!strcmp (p, "libgcc_shift_count"))
7902 mode = targetm.libgcc_shift_count_mode ();
1bd43494 7903 else if (!strcmp (p, "unwind_word"))
7904 mode = targetm.unwind_word_mode ();
f8e93a2e 7905 else
7906 for (j = 0; j < NUM_MACHINE_MODES; j++)
7907 if (!strcmp (p, GET_MODE_NAME (j)))
743a6f47 7908 {
3754d046 7909 mode = (machine_mode) j;
743a6f47 7910 break;
7911 }
f8e93a2e 7912
7913 if (mode == VOIDmode)
4917c376 7914 {
d1dd9ac0 7915 error ("unknown machine mode %qE", ident);
4917c376 7916 return NULL_TREE;
7917 }
7918
b2aef146 7919 valid_mode = false;
7920 switch (GET_MODE_CLASS (mode))
4917c376 7921 {
b2aef146 7922 case MODE_INT:
7923 case MODE_PARTIAL_INT:
7924 case MODE_FLOAT:
c4503c0a 7925 case MODE_DECIMAL_FLOAT:
9421ebb9 7926 case MODE_FRACT:
7927 case MODE_UFRACT:
7928 case MODE_ACCUM:
7929 case MODE_UACCUM:
b2aef146 7930 valid_mode = targetm.scalar_mode_supported_p (mode);
7931 break;
7932
7933 case MODE_COMPLEX_INT:
7934 case MODE_COMPLEX_FLOAT:
7935 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7936 break;
7937
7938 case MODE_VECTOR_INT:
7939 case MODE_VECTOR_FLOAT:
9421ebb9 7940 case MODE_VECTOR_FRACT:
7941 case MODE_VECTOR_UFRACT:
7942 case MODE_VECTOR_ACCUM:
7943 case MODE_VECTOR_UACCUM:
9b2d6d13 7944 warning (OPT_Wattributes, "specifying vector types with "
7945 "__attribute__ ((mode)) is deprecated");
7946 warning (OPT_Wattributes,
7947 "use __attribute__ ((vector_size)) instead");
b2aef146 7948 valid_mode = vector_mode_valid_p (mode);
7949 break;
4917c376 7950
b2aef146 7951 default:
7952 break;
7953 }
7954 if (!valid_mode)
7955 {
1e5fcbe2 7956 error ("unable to emulate %qs", p);
b2aef146 7957 return NULL_TREE;
7958 }
4917c376 7959
b2aef146 7960 if (POINTER_TYPE_P (type))
ead34f59 7961 {
6d5d708e 7962 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
3754d046 7963 tree (*fn)(tree, machine_mode, bool);
b2aef146 7964
6d5d708e 7965 if (!targetm.addr_space.valid_pointer_mode (mode, as))
ead34f59 7966 {
1e5fcbe2 7967 error ("invalid pointer mode %qs", p);
ead34f59 7968 return NULL_TREE;
7969 }
7970
a0c938f0 7971 if (TREE_CODE (type) == POINTER_TYPE)
b2aef146 7972 fn = build_pointer_type_for_mode;
805e22b2 7973 else
b2aef146 7974 fn = build_reference_type_for_mode;
7975 typefm = fn (TREE_TYPE (type), mode, false);
ead34f59 7976 }
b2aef146 7977 else
9421ebb9 7978 {
7979 /* For fixed-point modes, we need to test if the signness of type
7980 and the machine mode are consistent. */
7981 if (ALL_FIXED_POINT_MODE_P (mode)
7982 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7983 {
bf776685 7984 error ("signedness of type and machine mode %qs don%'t match", p);
9421ebb9 7985 return NULL_TREE;
7986 }
7987 /* For fixed-point modes, we need to pass saturating info. */
7988 typefm = lang_hooks.types.type_for_mode (mode,
7989 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7990 : TYPE_UNSIGNED (type));
7991 }
3a648ab9 7992
b2aef146 7993 if (typefm == NULL_TREE)
7994 {
743a6f47 7995 error ("no data type for mode %qs", p);
b2aef146 7996 return NULL_TREE;
7997 }
3a648ab9 7998 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7999 {
8000 /* For enumeral types, copy the precision from the integer
8001 type returned above. If not an INTEGER_TYPE, we can't use
8002 this mode for this type. */
8003 if (TREE_CODE (typefm) != INTEGER_TYPE)
8004 {
743a6f47 8005 error ("cannot use mode %qs for enumeral types", p);
3a648ab9 8006 return NULL_TREE;
8007 }
8008
10080eac 8009 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
8010 {
8011 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
8012 typefm = type;
8013 }
8014 else
8015 {
8016 /* We cannot build a type variant, as there's code that assumes
8017 that TYPE_MAIN_VARIANT has the same mode. This includes the
8018 debug generators. Instead, create a subrange type. This
8019 results in all of the enumeral values being emitted only once
8020 in the original, and the subtype gets them by reference. */
8021 if (TYPE_UNSIGNED (type))
8022 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
8023 else
8024 typefm = make_signed_type (TYPE_PRECISION (typefm));
8025 TREE_TYPE (typefm) = type;
8026 }
3a648ab9 8027 }
4bf450a1 8028 else if (VECTOR_MODE_P (mode)
8029 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
8030 : TREE_CODE (type) != TREE_CODE (typefm))
743a6f47 8031 {
8032 error ("mode %qs applied to inappropriate type", p);
8033 return NULL_TREE;
8034 }
8035
b2aef146 8036 *node = typefm;
f8e93a2e 8037 }
8038
8039 return NULL_TREE;
8040}
8041
8042/* Handle a "section" attribute; arguments as in
8043 struct attribute_spec.handler. */
8044
8045static tree
9a03a746 8046handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8047 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8048{
8049 tree decl = *node;
8050
9866562d 8051 if (!targetm_common.have_named_sections)
f8e93a2e 8052 {
9866562d 8053 error_at (DECL_SOURCE_LOCATION (*node),
8054 "section attributes are not supported for this target");
8055 goto fail;
8056 }
065efcb1 8057
9866562d 8058 user_defined_section_attribute = true;
f8e93a2e 8059
b443c459 8060 if (!VAR_OR_FUNCTION_DECL_P (decl))
9866562d 8061 {
8062 error ("section attribute not allowed for %q+D", *node);
8063 goto fail;
f8e93a2e 8064 }
9866562d 8065
8066 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
f8e93a2e 8067 {
9866562d 8068 error ("section attribute argument not a string constant");
8069 goto fail;
f8e93a2e 8070 }
8071
f48c7f4a 8072 if (VAR_P (decl)
9866562d 8073 && current_function_decl != NULL_TREE
8074 && !TREE_STATIC (decl))
8075 {
8076 error_at (DECL_SOURCE_LOCATION (decl),
8077 "section attribute cannot be specified for local variables");
8078 goto fail;
8079 }
8080
8081 /* The decl may have already been given a section attribute
8082 from a previous declaration. Ensure they match. */
8083 if (DECL_SECTION_NAME (decl) != NULL
8084 && strcmp (DECL_SECTION_NAME (decl),
8085 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
8086 {
8087 error ("section of %q+D conflicts with previous declaration", *node);
8088 goto fail;
8089 }
8090
f48c7f4a 8091 if (VAR_P (decl)
9866562d 8092 && !targetm.have_tls && targetm.emutls.tmpl_section
8093 && DECL_THREAD_LOCAL_P (decl))
8094 {
8095 error ("section of %q+D cannot be overridden", *node);
8096 goto fail;
8097 }
8098
8099 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
8100 return NULL_TREE;
8101
8102fail:
8103 *no_add_attrs = true;
f8e93a2e 8104 return NULL_TREE;
8105}
8106
83e25171 8107/* Check whether ALIGN is a valid user-specified alignment. If so,
8108 return its base-2 log; if not, output an error and return -1. If
8109 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
8110 no error. */
8111int
8112check_user_alignment (const_tree align, bool allow_zero)
8113{
8114 int i;
8115
3e5a8b00 8116 if (error_operand_p (align))
8117 return -1;
5abaa10a 8118 if (TREE_CODE (align) != INTEGER_CST
8119 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
83e25171 8120 {
8121 error ("requested alignment is not an integer constant");
8122 return -1;
8123 }
8124 else if (allow_zero && integer_zerop (align))
8125 return -1;
1a087624 8126 else if (tree_int_cst_sgn (align) == -1
8127 || (i = tree_log2 (align)) == -1)
83e25171 8128 {
1a087624 8129 error ("requested alignment is not a positive power of 2");
83e25171 8130 return -1;
8131 }
8132 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
8133 {
8134 error ("requested alignment is too large");
8135 return -1;
8136 }
8137 return i;
8138}
8139
ffcdbf9c 8140/*
8141 If in c++-11, check if the c++-11 alignment constraint with respect
8142 to fundamental alignment (in [dcl.align]) are satisfied. If not in
8143 c++-11 mode, does nothing.
8144
8145 [dcl.align]2/ says:
8146
8147 [* if the constant expression evaluates to a fundamental alignment,
8148 the alignment requirement of the declared entity shall be the
8149 specified fundamental alignment.
8150
8151 * if the constant expression evaluates to an extended alignment
8152 and the implementation supports that alignment in the context
8153 of the declaration, the alignment of the declared entity shall
8154 be that alignment
8155
8156 * if the constant expression evaluates to an extended alignment
8157 and the implementation does not support that alignment in the
8158 context of the declaration, the program is ill-formed]. */
8159
8160static bool
8161check_cxx_fundamental_alignment_constraints (tree node,
8162 unsigned align_log,
8163 int flags)
8164{
8165 bool alignment_too_large_p = false;
8166 unsigned requested_alignment = 1U << align_log;
8167 unsigned max_align = 0;
8168
8169 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
8170 || (node == NULL_TREE || node == error_mark_node))
8171 return true;
8172
8173 if (cxx_fundamental_alignment_p (requested_alignment))
8174 return true;
8175
8176 if (DECL_P (node))
8177 {
8178 if (TREE_STATIC (node))
8179 {
8180 /* For file scope variables and static members, the target
8181 supports alignments that are at most
8182 MAX_OFILE_ALIGNMENT. */
8183 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
8184 alignment_too_large_p = true;
8185 }
8186 else
8187 {
8188#ifdef BIGGEST_FIELD_ALIGNMENT
8189#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
8190#else
8191#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
8192#endif
8193 /* For non-static members, the target supports either
8194 alignments that at most either BIGGEST_FIELD_ALIGNMENT
8195 if it is defined or BIGGEST_ALIGNMENT. */
8196 max_align = MAX_TARGET_FIELD_ALIGNMENT;
8197 if (TREE_CODE (node) == FIELD_DECL
8198 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
8199 alignment_too_large_p = true;
8200#undef MAX_TARGET_FIELD_ALIGNMENT
8201 /* For stack variables, the target supports at most
8202 MAX_STACK_ALIGNMENT. */
8203 else if (decl_function_context (node) != NULL
8204 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
8205 alignment_too_large_p = true;
8206 }
8207 }
8208 else if (TYPE_P (node))
8209 {
8210 /* Let's be liberal for types. */
8211 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
8212 alignment_too_large_p = true;
8213 }
8214
8215 if (alignment_too_large_p)
8216 pedwarn (input_location, OPT_Wattributes,
8217 "requested alignment %d is larger than %d",
8218 requested_alignment, max_align);
8219
8220 return !alignment_too_large_p;
8221}
8222
f8e93a2e 8223/* Handle a "aligned" attribute; arguments as in
8224 struct attribute_spec.handler. */
8225
8226static tree
9a03a746 8227handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
09347743 8228 int flags, bool *no_add_attrs)
f8e93a2e 8229{
8230 tree decl = NULL_TREE;
8231 tree *type = NULL;
8232 int is_type = 0;
caf62483 8233 tree align_expr;
f8e93a2e 8234 int i;
8235
caf62483 8236 if (args)
8237 {
8238 align_expr = TREE_VALUE (args);
3e5a8b00 8239 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
8240 && TREE_CODE (align_expr) != FUNCTION_DECL)
caf62483 8241 align_expr = default_conversion (align_expr);
8242 }
8243 else
8244 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
8245
f8e93a2e 8246 if (DECL_P (*node))
8247 {
8248 decl = *node;
8249 type = &TREE_TYPE (decl);
8250 is_type = TREE_CODE (*node) == TYPE_DECL;
8251 }
8252 else if (TYPE_P (*node))
8253 type = node, is_type = 1;
8254
ffcdbf9c 8255 if ((i = check_user_alignment (align_expr, false)) == -1
8256 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
83e25171 8257 *no_add_attrs = true;
f8e93a2e 8258 else if (is_type)
8259 {
2ec3af9c 8260 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8261 /* OK, modify the type in place. */;
f8e93a2e 8262 /* If we have a TYPE_DECL, then copy the type, so that we
8263 don't accidentally modify a builtin type. See pushdecl. */
2ec3af9c 8264 else if (decl && TREE_TYPE (decl) != error_mark_node
8265 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
f8e93a2e 8266 {
8267 tree tt = TREE_TYPE (decl);
e086912e 8268 *type = build_variant_type_copy (*type);
f8e93a2e 8269 DECL_ORIGINAL_TYPE (decl) = tt;
8270 TYPE_NAME (*type) = decl;
8271 TREE_USED (*type) = TREE_USED (decl);
8272 TREE_TYPE (decl) = *type;
8273 }
2ec3af9c 8274 else
e086912e 8275 *type = build_variant_type_copy (*type);
f8e93a2e 8276
7cfdc2f0 8277 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
f8e93a2e 8278 TYPE_USER_ALIGN (*type) = 1;
8279 }
097b5c8b 8280 else if (! VAR_OR_FUNCTION_DECL_P (decl)
f8e93a2e 8281 && TREE_CODE (decl) != FIELD_DECL)
8282 {
3cf8b391 8283 error ("alignment may not be specified for %q+D", decl);
f8e93a2e 8284 *no_add_attrs = true;
8285 }
ffcdbf9c 8286 else if (DECL_USER_ALIGN (decl)
8287 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
8288 /* C++-11 [dcl.align/4]:
8289
8290 When multiple alignment-specifiers are specified for an
8291 entity, the alignment requirement shall be set to the
8292 strictest specified alignment.
8293
8294 This formally comes from the c++11 specification but we are
8295 doing it for the GNU attribute syntax as well. */
8296 *no_add_attrs = true;
097b5c8b 8297 else if (TREE_CODE (decl) == FUNCTION_DECL
7cfdc2f0 8298 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
097b5c8b 8299 {
8300 if (DECL_USER_ALIGN (decl))
8301 error ("alignment for %q+D was previously specified as %d "
8302 "and may not be decreased", decl,
8303 DECL_ALIGN (decl) / BITS_PER_UNIT);
8304 else
8305 error ("alignment for %q+D must be at least %d", decl,
8306 DECL_ALIGN (decl) / BITS_PER_UNIT);
8307 *no_add_attrs = true;
8308 }
f8e93a2e 8309 else
8310 {
7cfdc2f0 8311 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
f8e93a2e 8312 DECL_USER_ALIGN (decl) = 1;
8313 }
8314
8315 return NULL_TREE;
8316}
8317
8318/* Handle a "weak" attribute; arguments as in
8319 struct attribute_spec.handler. */
8320
8321static tree
f948b309 8322handle_weak_attribute (tree *node, tree name,
9a03a746 8323 tree ARG_UNUSED (args),
8324 int ARG_UNUSED (flags),
8325 bool * ARG_UNUSED (no_add_attrs))
f8e93a2e 8326{
f948b309 8327 if (TREE_CODE (*node) == FUNCTION_DECL
059a60f3 8328 && DECL_DECLARED_INLINE_P (*node))
8329 {
0725e25c 8330 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
059a60f3 8331 *no_add_attrs = true;
8332 }
85c0a25c 8333 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8334 {
8335 error ("indirect function %q+D cannot be declared weak", *node);
8336 *no_add_attrs = true;
8337 return NULL_TREE;
8338 }
b443c459 8339 else if (VAR_OR_FUNCTION_DECL_P (*node))
9e6bcade 8340 declare_weak (*node);
f948b309 8341 else
8342 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8343
8344 return NULL_TREE;
8345}
8346
4641882f 8347/* Handle a "noplt" attribute; arguments as in
8348 struct attribute_spec.handler. */
8349
8350static tree
8351handle_noplt_attribute (tree *node, tree name,
8352 tree ARG_UNUSED (args),
8353 int ARG_UNUSED (flags),
8354 bool * ARG_UNUSED (no_add_attrs))
8355{
8356 if (TREE_CODE (*node) != FUNCTION_DECL)
8357 {
8358 warning (OPT_Wattributes,
8359 "%qE attribute is only applicable on functions", name);
8360 *no_add_attrs = true;
8361 return NULL_TREE;
8362 }
8363 return NULL_TREE;
8364}
8365
85c0a25c 8366/* Handle an "alias" or "ifunc" attribute; arguments as in
8367 struct attribute_spec.handler, except that IS_ALIAS tells us
8368 whether this is an alias as opposed to ifunc attribute. */
f8e93a2e 8369
8370static tree
85c0a25c 8371handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
8372 bool *no_add_attrs)
f8e93a2e 8373{
8374 tree decl = *node;
8375
85c0a25c 8376 if (TREE_CODE (decl) != FUNCTION_DECL
f48c7f4a 8377 && (!is_alias || !VAR_P (decl)))
9e830260 8378 {
8379 warning (OPT_Wattributes, "%qE attribute ignored", name);
8380 *no_add_attrs = true;
8381 }
8382 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
48e1416a 8383 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 8384 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
8385 /* A static variable declaration is always a tentative definition,
8386 but the alias is a non-tentative definition which overrides. */
48e1416a 8387 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 8388 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
f8e93a2e 8389 {
85c0a25c 8390 error ("%q+D defined both normally and as %qE attribute", decl, name);
f8e93a2e 8391 *no_add_attrs = true;
85c0a25c 8392 return NULL_TREE;
f8e93a2e 8393 }
85c0a25c 8394 else if (!is_alias
8395 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
8396 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
8397 {
8398 error ("weak %q+D cannot be defined %qE", decl, name);
8399 *no_add_attrs = true;
8400 return NULL_TREE;
8401 }
8c42f0d9 8402
8403 /* Note that the very first time we process a nested declaration,
8404 decl_function_context will not be set. Indeed, *would* never
8405 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8406 we do below. After such frobbery, pushdecl would set the context.
8407 In any case, this is never what we want. */
8408 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
f8e93a2e 8409 {
8410 tree id;
8411
8412 id = TREE_VALUE (args);
8413 if (TREE_CODE (id) != STRING_CST)
8414 {
85c0a25c 8415 error ("attribute %qE argument not a string", name);
f8e93a2e 8416 *no_add_attrs = true;
8417 return NULL_TREE;
8418 }
8419 id = get_identifier (TREE_STRING_POINTER (id));
8420 /* This counts as a use of the object pointed to. */
8421 TREE_USED (id) = 1;
8422
8423 if (TREE_CODE (decl) == FUNCTION_DECL)
8424 DECL_INITIAL (decl) = error_mark_node;
8425 else
f2526cce 8426 TREE_STATIC (decl) = 1;
85c0a25c 8427
8428 if (!is_alias)
8429 /* ifuncs are also aliases, so set that attribute too. */
8430 DECL_ATTRIBUTES (decl)
8431 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
f8e93a2e 8432 }
8433 else
8434 {
9b2d6d13 8435 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8436 *no_add_attrs = true;
8437 }
8438
8e857c41 8439 if (decl_in_symtab_p (*node))
8440 {
8441 struct symtab_node *n = symtab_node::get (decl);
8442 if (n && n->refuse_visibility_changes)
8443 {
8444 if (is_alias)
8445 error ("%+D declared alias after being used", decl);
8446 else
8447 error ("%+D declared ifunc after being used", decl);
8448 }
8449 }
8450
8451
f8e93a2e 8452 return NULL_TREE;
8453}
8454
85c0a25c 8455/* Handle an "alias" or "ifunc" attribute; arguments as in
8456 struct attribute_spec.handler. */
8457
8458static tree
8459handle_ifunc_attribute (tree *node, tree name, tree args,
8460 int ARG_UNUSED (flags), bool *no_add_attrs)
8461{
8462 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8463}
8464
8465/* Handle an "alias" or "ifunc" attribute; arguments as in
8466 struct attribute_spec.handler. */
8467
8468static tree
8469handle_alias_attribute (tree *node, tree name, tree args,
8470 int ARG_UNUSED (flags), bool *no_add_attrs)
8471{
8472 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8473}
8474
f4a30bd7 8475/* Handle a "weakref" attribute; arguments as in struct
8476 attribute_spec.handler. */
8477
8478static tree
8479handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8480 int flags, bool *no_add_attrs)
8481{
8482 tree attr = NULL_TREE;
8483
83852912 8484 /* We must ignore the attribute when it is associated with
8485 local-scoped decls, since attribute alias is ignored and many
8486 such symbols do not even have a DECL_WEAK field. */
64a7bd81 8487 if (decl_function_context (*node)
8488 || current_function_decl
b443c459 8489 || !VAR_OR_FUNCTION_DECL_P (*node))
83852912 8490 {
8491 warning (OPT_Wattributes, "%qE attribute ignored", name);
8492 *no_add_attrs = true;
8493 return NULL_TREE;
8494 }
8495
85c0a25c 8496 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8497 {
8498 error ("indirect function %q+D cannot be declared weakref", *node);
8499 *no_add_attrs = true;
8500 return NULL_TREE;
8501 }
8502
f4a30bd7 8503 /* The idea here is that `weakref("name")' mutates into `weakref,
8504 alias("name")', and weakref without arguments, in turn,
8505 implicitly adds weak. */
8506
8507 if (args)
8508 {
8509 attr = tree_cons (get_identifier ("alias"), args, attr);
8510 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8511
8512 *no_add_attrs = true;
0a3ecdc1 8513
8514 decl_attributes (node, attr, flags);
f4a30bd7 8515 }
8516 else
8517 {
8518 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
712d2297 8519 error_at (DECL_SOURCE_LOCATION (*node),
8520 "weakref attribute must appear before alias attribute");
f4a30bd7 8521
0a3ecdc1 8522 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8523 and that isn't supported; and because it wants to add it to
8524 the list of weak decls, which isn't helpful. */
8525 DECL_WEAK (*node) = 1;
f4a30bd7 8526 }
8527
8e857c41 8528 if (decl_in_symtab_p (*node))
8529 {
8530 struct symtab_node *n = symtab_node::get (*node);
8531 if (n && n->refuse_visibility_changes)
8532 error ("%+D declared weakref after being used", *node);
8533 }
8534
f4a30bd7 8535 return NULL_TREE;
8536}
8537
f8e93a2e 8538/* Handle an "visibility" attribute; arguments as in
8539 struct attribute_spec.handler. */
8540
8541static tree
1cae46be 8542handle_visibility_attribute (tree *node, tree name, tree args,
9a03a746 8543 int ARG_UNUSED (flags),
4a2849cb 8544 bool *ARG_UNUSED (no_add_attrs))
f8e93a2e 8545{
8546 tree decl = *node;
9c40570a 8547 tree id = TREE_VALUE (args);
4a2849cb 8548 enum symbol_visibility vis;
f8e93a2e 8549
b212f378 8550 if (TYPE_P (*node))
8551 {
4a2849cb 8552 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8553 /* OK */;
8554 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
8555 {
8556 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8557 name);
8558 return NULL_TREE;
8559 }
8560 else if (TYPE_FIELDS (*node))
8561 {
8562 error ("%qE attribute ignored because %qT is already defined",
8563 name, *node);
8564 return NULL_TREE;
8565 }
b212f378 8566 }
84166705 8567 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
f8e93a2e 8568 {
9b2d6d13 8569 warning (OPT_Wattributes, "%qE attribute ignored", name);
9c40570a 8570 return NULL_TREE;
f8e93a2e 8571 }
f8e93a2e 8572
9c40570a 8573 if (TREE_CODE (id) != STRING_CST)
8574 {
07e3a3d2 8575 error ("visibility argument not a string");
9c40570a 8576 return NULL_TREE;
f8e93a2e 8577 }
b27ac6b5 8578
b212f378 8579 /* If this is a type, set the visibility on the type decl. */
8580 if (TYPE_P (decl))
8581 {
8582 decl = TYPE_NAME (decl);
84166705 8583 if (!decl)
a0c938f0 8584 return NULL_TREE;
e147aab3 8585 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8586 {
9b2d6d13 8587 warning (OPT_Wattributes, "%qE attribute ignored on types",
e147aab3 8588 name);
8589 return NULL_TREE;
8590 }
b212f378 8591 }
f8e93a2e 8592
9c40570a 8593 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
4a2849cb 8594 vis = VISIBILITY_DEFAULT;
9c40570a 8595 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
4a2849cb 8596 vis = VISIBILITY_INTERNAL;
9c40570a 8597 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
4a2849cb 8598 vis = VISIBILITY_HIDDEN;
9c40570a 8599 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
4a2849cb 8600 vis = VISIBILITY_PROTECTED;
9c40570a 8601 else
4a2849cb 8602 {
8603 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8604 vis = VISIBILITY_DEFAULT;
8605 }
8606
8607 if (DECL_VISIBILITY_SPECIFIED (decl)
098a01e7 8608 && vis != DECL_VISIBILITY (decl))
8609 {
8610 tree attributes = (TYPE_P (*node)
8611 ? TYPE_ATTRIBUTES (*node)
8612 : DECL_ATTRIBUTES (decl));
8613 if (lookup_attribute ("visibility", attributes))
8614 error ("%qD redeclared with different visibility", decl);
8615 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8616 && lookup_attribute ("dllimport", attributes))
8617 error ("%qD was declared %qs which implies default visibility",
8618 decl, "dllimport");
8619 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8620 && lookup_attribute ("dllexport", attributes))
8621 error ("%qD was declared %qs which implies default visibility",
8622 decl, "dllexport");
8623 }
4a2849cb 8624
8625 DECL_VISIBILITY (decl) = vis;
b212f378 8626 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8627
4a2849cb 8628 /* Go ahead and attach the attribute to the node as well. This is needed
8629 so we can determine whether we have VISIBILITY_DEFAULT because the
8630 visibility was not specified, or because it was explicitly overridden
8631 from the containing scope. */
9c40570a 8632
f8e93a2e 8633 return NULL_TREE;
8634}
8635
3aa0c315 8636/* Determine the ELF symbol visibility for DECL, which is either a
8637 variable or a function. It is an error to use this function if a
8638 definition of DECL is not available in this translation unit.
8639 Returns true if the final visibility has been determined by this
8640 function; false if the caller is free to make additional
8641 modifications. */
8642
8643bool
8644c_determine_visibility (tree decl)
8645{
b443c459 8646 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
3aa0c315 8647
8648 /* If the user explicitly specified the visibility with an
8649 attribute, honor that. DECL_VISIBILITY will have been set during
920f5a70 8650 the processing of the attribute. We check for an explicit
8651 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8652 to distinguish the use of an attribute from the use of a "#pragma
8653 GCC visibility push(...)"; in the latter case we still want other
8654 considerations to be able to overrule the #pragma. */
8655 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8656 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8657 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8658 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
3aa0c315 8659 return true;
8660
4a2849cb 8661 /* Set default visibility to whatever the user supplied with
8662 visibility_specified depending on #pragma GCC visibility. */
8663 if (!DECL_VISIBILITY_SPECIFIED (decl))
8664 {
2d9d8740 8665 if (visibility_options.inpragma
8666 || DECL_VISIBILITY (decl) != default_visibility)
8667 {
8668 DECL_VISIBILITY (decl) = default_visibility;
8669 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8670 /* If visibility changed and DECL already has DECL_RTL, ensure
8671 symbol flags are updated. */
f48c7f4a 8672 if (((VAR_P (decl) && TREE_STATIC (decl))
2d9d8740 8673 || TREE_CODE (decl) == FUNCTION_DECL)
8674 && DECL_RTL_SET_P (decl))
8675 make_decl_rtl (decl);
8676 }
4a2849cb 8677 }
3aa0c315 8678 return false;
8679}
8680
24dfead4 8681/* Handle an "tls_model" attribute; arguments as in
8682 struct attribute_spec.handler. */
8683
8684static tree
1cae46be 8685handle_tls_model_attribute (tree *node, tree name, tree args,
9a03a746 8686 int ARG_UNUSED (flags), bool *no_add_attrs)
24dfead4 8687{
1b53eb20 8688 tree id;
24dfead4 8689 tree decl = *node;
1b53eb20 8690 enum tls_model kind;
24dfead4 8691
1b53eb20 8692 *no_add_attrs = true;
8693
f48c7f4a 8694 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
24dfead4 8695 {
9b2d6d13 8696 warning (OPT_Wattributes, "%qE attribute ignored", name);
1b53eb20 8697 return NULL_TREE;
24dfead4 8698 }
24dfead4 8699
1b53eb20 8700 kind = DECL_TLS_MODEL (decl);
8701 id = TREE_VALUE (args);
8702 if (TREE_CODE (id) != STRING_CST)
8703 {
8704 error ("tls_model argument not a string");
8705 return NULL_TREE;
24dfead4 8706 }
8707
1b53eb20 8708 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8709 kind = TLS_MODEL_LOCAL_EXEC;
8710 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8711 kind = TLS_MODEL_INITIAL_EXEC;
8712 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8713 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8714 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8715 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8716 else
8717 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8718
5e68df57 8719 set_decl_tls_model (decl, kind);
24dfead4 8720 return NULL_TREE;
8721}
8722
f8e93a2e 8723/* Handle a "no_instrument_function" attribute; arguments as in
8724 struct attribute_spec.handler. */
8725
8726static tree
1cae46be 8727handle_no_instrument_function_attribute (tree *node, tree name,
9a03a746 8728 tree ARG_UNUSED (args),
8729 int ARG_UNUSED (flags),
09347743 8730 bool *no_add_attrs)
f8e93a2e 8731{
8732 tree decl = *node;
8733
8734 if (TREE_CODE (decl) != FUNCTION_DECL)
8735 {
712d2297 8736 error_at (DECL_SOURCE_LOCATION (decl),
8737 "%qE attribute applies only to functions", name);
f8e93a2e 8738 *no_add_attrs = true;
8739 }
f8e93a2e 8740 else
8741 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8742
8743 return NULL_TREE;
8744}
8745
8746/* Handle a "malloc" attribute; arguments as in
8747 struct attribute_spec.handler. */
8748
8749static tree
9a03a746 8750handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8751 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8752{
a5147fca 8753 if (TREE_CODE (*node) == FUNCTION_DECL
8754 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
f8e93a2e 8755 DECL_IS_MALLOC (*node) = 1;
f8e93a2e 8756 else
8757 {
9b2d6d13 8758 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8759 *no_add_attrs = true;
8760 }
8761
8762 return NULL_TREE;
8763}
8764
4a29c97c 8765/* Handle a "alloc_size" attribute; arguments as in
8766 struct attribute_spec.handler. */
8767
8768static tree
8769handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8770 int ARG_UNUSED (flags), bool *no_add_attrs)
8771{
2802826e 8772 unsigned arg_count = type_num_arguments (*node);
4a29c97c 8773 for (; args; args = TREE_CHAIN (args))
8774 {
8775 tree position = TREE_VALUE (args);
caf62483 8776 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8777 && TREE_CODE (position) != FUNCTION_DECL)
8778 position = default_conversion (position);
4a29c97c 8779
237e78b1 8780 if (!tree_fits_uhwi_p (position)
8781 || !arg_count
8782 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
4a29c97c 8783 {
48e1416a 8784 warning (OPT_Wattributes,
4a29c97c 8785 "alloc_size parameter outside range");
8786 *no_add_attrs = true;
8787 return NULL_TREE;
8788 }
8789 }
8790 return NULL_TREE;
8791}
8792
237e78b1 8793/* Handle a "alloc_align" attribute; arguments as in
8794 struct attribute_spec.handler. */
8795
8796static tree
8797handle_alloc_align_attribute (tree *node, tree, tree args, int,
8798 bool *no_add_attrs)
8799{
8800 unsigned arg_count = type_num_arguments (*node);
8801 tree position = TREE_VALUE (args);
8802 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8803 position = default_conversion (position);
8804
8805 if (!tree_fits_uhwi_p (position)
8806 || !arg_count
8807 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8808 {
8809 warning (OPT_Wattributes,
8810 "alloc_align parameter outside range");
8811 *no_add_attrs = true;
8812 return NULL_TREE;
8813 }
8814 return NULL_TREE;
8815}
8816
8817/* Handle a "assume_aligned" attribute; arguments as in
8818 struct attribute_spec.handler. */
8819
8820static tree
8821handle_assume_aligned_attribute (tree *, tree, tree args, int,
8822 bool *no_add_attrs)
8823{
8824 for (; args; args = TREE_CHAIN (args))
8825 {
8826 tree position = TREE_VALUE (args);
8827 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8828 && TREE_CODE (position) != FUNCTION_DECL)
8829 position = default_conversion (position);
8830
8831 if (TREE_CODE (position) != INTEGER_CST)
8832 {
8833 warning (OPT_Wattributes,
8834 "assume_aligned parameter not integer constant");
8835 *no_add_attrs = true;
8836 return NULL_TREE;
8837 }
8838 }
8839 return NULL_TREE;
8840}
8841
8ce86007 8842/* Handle a "fn spec" attribute; arguments as in
8843 struct attribute_spec.handler. */
8844
8845static tree
8846handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8847 tree args, int ARG_UNUSED (flags),
8848 bool *no_add_attrs ATTRIBUTE_UNUSED)
8849{
8850 gcc_assert (args
8851 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8852 && !TREE_CHAIN (args));
8853 return NULL_TREE;
8854}
8855
058a1b7a 8856/* Handle a "bnd_variable_size" attribute; arguments as in
8857 struct attribute_spec.handler. */
8858
8859static tree
8860handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8861 int ARG_UNUSED (flags), bool *no_add_attrs)
8862{
8863 if (TREE_CODE (*node) != FIELD_DECL)
8864 {
8865 warning (OPT_Wattributes, "%qE attribute ignored", name);
8866 *no_add_attrs = true;
8867 }
8868
8869 return NULL_TREE;
8870}
8871
8872/* Handle a "bnd_legacy" attribute; arguments as in
8873 struct attribute_spec.handler. */
8874
8875static tree
8876handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8877 int ARG_UNUSED (flags), bool *no_add_attrs)
8878{
8879 if (TREE_CODE (*node) != FUNCTION_DECL)
8880 {
8881 warning (OPT_Wattributes, "%qE attribute ignored", name);
8882 *no_add_attrs = true;
8883 }
8884
8885 return NULL_TREE;
8886}
8887
8888/* Handle a "bnd_instrument" attribute; arguments as in
8889 struct attribute_spec.handler. */
8890
8891static tree
8892handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8893 int ARG_UNUSED (flags), bool *no_add_attrs)
8894{
8895 if (TREE_CODE (*node) != FUNCTION_DECL)
8896 {
8897 warning (OPT_Wattributes, "%qE attribute ignored", name);
8898 *no_add_attrs = true;
8899 }
8900
8901 return NULL_TREE;
8902}
8903
a96c3cc1 8904/* Handle a "warn_unused" attribute; arguments as in
8905 struct attribute_spec.handler. */
8906
8907static tree
8908handle_warn_unused_attribute (tree *node, tree name,
8909 tree args ATTRIBUTE_UNUSED,
8910 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8911{
8912 if (TYPE_P (*node))
8913 /* Do nothing else, just set the attribute. We'll get at
8914 it later with lookup_attribute. */
8915 ;
8916 else
8917 {
8918 warning (OPT_Wattributes, "%qE attribute ignored", name);
8919 *no_add_attrs = true;
8920 }
8921
8922 return NULL_TREE;
8923}
8924
bc7bff74 8925/* Handle an "omp declare simd" attribute; arguments as in
8926 struct attribute_spec.handler. */
8927
8928static tree
8929handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8930{
8931 return NULL_TREE;
8932}
8933
8934/* Handle an "omp declare target" attribute; arguments as in
8935 struct attribute_spec.handler. */
8936
8937static tree
8938handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8939{
8940 return NULL_TREE;
8941}
8942
26d1c5ff 8943/* Handle a "returns_twice" attribute; arguments as in
8944 struct attribute_spec.handler. */
8945
8946static tree
8947handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8948 int ARG_UNUSED (flags), bool *no_add_attrs)
8949{
8950 if (TREE_CODE (*node) == FUNCTION_DECL)
8951 DECL_IS_RETURNS_TWICE (*node) = 1;
8952 else
8953 {
9b2d6d13 8954 warning (OPT_Wattributes, "%qE attribute ignored", name);
26d1c5ff 8955 *no_add_attrs = true;
8956 }
8957
8958 return NULL_TREE;
8959}
8960
f8e93a2e 8961/* Handle a "no_limit_stack" attribute; arguments as in
8962 struct attribute_spec.handler. */
8963
8964static tree
1cae46be 8965handle_no_limit_stack_attribute (tree *node, tree name,
9a03a746 8966 tree ARG_UNUSED (args),
8967 int ARG_UNUSED (flags),
09347743 8968 bool *no_add_attrs)
f8e93a2e 8969{
8970 tree decl = *node;
8971
8972 if (TREE_CODE (decl) != FUNCTION_DECL)
8973 {
712d2297 8974 error_at (DECL_SOURCE_LOCATION (decl),
8975 "%qE attribute applies only to functions", name);
f8e93a2e 8976 *no_add_attrs = true;
8977 }
8978 else if (DECL_INITIAL (decl))
8979 {
712d2297 8980 error_at (DECL_SOURCE_LOCATION (decl),
8981 "can%'t set %qE attribute after definition", name);
f8e93a2e 8982 *no_add_attrs = true;
8983 }
8984 else
8985 DECL_NO_LIMIT_STACK (decl) = 1;
8986
8987 return NULL_TREE;
8988}
8989
8990/* Handle a "pure" attribute; arguments as in
8991 struct attribute_spec.handler. */
8992
8993static tree
9a03a746 8994handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8995 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8996{
8997 if (TREE_CODE (*node) == FUNCTION_DECL)
9c2a0c05 8998 DECL_PURE_P (*node) = 1;
f8e93a2e 8999 /* ??? TODO: Support types. */
9000 else
9001 {
9b2d6d13 9002 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 9003 *no_add_attrs = true;
9004 }
9005
9006 return NULL_TREE;
9007}
9008
4c0315d0 9009/* Digest an attribute list destined for a transactional memory statement.
9010 ALLOWED is the set of attributes that are allowed for this statement;
9011 return the attribute we parsed. Multiple attributes are never allowed. */
9012
9013int
9014parse_tm_stmt_attr (tree attrs, int allowed)
9015{
9016 tree a_seen = NULL;
9017 int m_seen = 0;
9018
9019 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
9020 {
9021 tree a = TREE_PURPOSE (attrs);
9022 int m = 0;
9023
9024 if (is_attribute_p ("outer", a))
9025 m = TM_STMT_ATTR_OUTER;
9026
9027 if ((m & allowed) == 0)
9028 {
9029 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
9030 continue;
9031 }
9032
9033 if (m_seen == 0)
9034 {
9035 a_seen = a;
9036 m_seen = m;
9037 }
9038 else if (m_seen == m)
9039 warning (OPT_Wattributes, "%qE attribute duplicated", a);
9040 else
9041 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
9042 }
9043
9044 return m_seen;
9045}
9046
9047/* Transform a TM attribute name into a maskable integer and back.
9048 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
9049 to how the lack of an attribute is treated. */
9050
9051int
9052tm_attr_to_mask (tree attr)
9053{
9054 if (attr == NULL)
9055 return 0;
9056 if (is_attribute_p ("transaction_safe", attr))
9057 return TM_ATTR_SAFE;
9058 if (is_attribute_p ("transaction_callable", attr))
9059 return TM_ATTR_CALLABLE;
9060 if (is_attribute_p ("transaction_pure", attr))
9061 return TM_ATTR_PURE;
9062 if (is_attribute_p ("transaction_unsafe", attr))
9063 return TM_ATTR_IRREVOCABLE;
9064 if (is_attribute_p ("transaction_may_cancel_outer", attr))
9065 return TM_ATTR_MAY_CANCEL_OUTER;
9066 return 0;
9067}
9068
9069tree
9070tm_mask_to_attr (int mask)
9071{
9072 const char *str;
9073 switch (mask)
9074 {
9075 case TM_ATTR_SAFE:
9076 str = "transaction_safe";
9077 break;
9078 case TM_ATTR_CALLABLE:
9079 str = "transaction_callable";
9080 break;
9081 case TM_ATTR_PURE:
9082 str = "transaction_pure";
9083 break;
9084 case TM_ATTR_IRREVOCABLE:
9085 str = "transaction_unsafe";
9086 break;
9087 case TM_ATTR_MAY_CANCEL_OUTER:
9088 str = "transaction_may_cancel_outer";
9089 break;
9090 default:
9091 gcc_unreachable ();
9092 }
9093 return get_identifier (str);
9094}
9095
9096/* Return the first TM attribute seen in LIST. */
9097
9098tree
9099find_tm_attribute (tree list)
9100{
9101 for (; list ; list = TREE_CHAIN (list))
9102 {
9103 tree name = TREE_PURPOSE (list);
9104 if (tm_attr_to_mask (name) != 0)
9105 return name;
9106 }
9107 return NULL_TREE;
9108}
9109
9110/* Handle the TM attributes; arguments as in struct attribute_spec.handler.
9111 Here we accept only function types, and verify that none of the other
9112 function TM attributes are also applied. */
9113/* ??? We need to accept class types for C++, but not C. This greatly
9114 complicates this function, since we can no longer rely on the extra
9115 processing given by function_type_required. */
9116
9117static tree
9118handle_tm_attribute (tree *node, tree name, tree args,
9119 int flags, bool *no_add_attrs)
9120{
9121 /* Only one path adds the attribute; others don't. */
9122 *no_add_attrs = true;
9123
9124 switch (TREE_CODE (*node))
9125 {
9126 case RECORD_TYPE:
9127 case UNION_TYPE:
9128 /* Only tm_callable and tm_safe apply to classes. */
9129 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
9130 goto ignored;
9131 /* FALLTHRU */
9132
9133 case FUNCTION_TYPE:
9134 case METHOD_TYPE:
9135 {
9136 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
9137 if (old_name == name)
9138 ;
9139 else if (old_name != NULL_TREE)
9140 error ("type was previously declared %qE", old_name);
9141 else
9142 *no_add_attrs = false;
9143 }
9144 break;
9145
6d02e6b2 9146 case FUNCTION_DECL:
9147 {
9148 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
9149 want to set transaction_safe on the type. */
9150 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
9151 if (!TYPE_P (DECL_CONTEXT (*node)))
9152 error_at (DECL_SOURCE_LOCATION (*node),
9153 "%<transaction_safe_dynamic%> may only be specified for "
9154 "a virtual function");
9155 *no_add_attrs = false;
9156 decl_attributes (&TREE_TYPE (*node),
9157 build_tree_list (get_identifier ("transaction_safe"),
9158 NULL_TREE),
9159 0);
9160 break;
9161 }
9162
4c0315d0 9163 case POINTER_TYPE:
9164 {
9165 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
9166 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
9167 {
9168 tree fn_tmp = TREE_TYPE (*node);
9169 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
9170 *node = build_pointer_type (fn_tmp);
9171 break;
9172 }
9173 }
9174 /* FALLTHRU */
9175
9176 default:
9177 /* If a function is next, pass it on to be tried next. */
9178 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
9179 return tree_cons (name, args, NULL);
9180
9181 ignored:
9182 warning (OPT_Wattributes, "%qE attribute ignored", name);
9183 break;
9184 }
9185
9186 return NULL_TREE;
9187}
9188
9189/* Handle the TM_WRAP attribute; arguments as in
9190 struct attribute_spec.handler. */
9191
9192static tree
9193handle_tm_wrap_attribute (tree *node, tree name, tree args,
9194 int ARG_UNUSED (flags), bool *no_add_attrs)
9195{
9196 tree decl = *node;
9197
9198 /* We don't need the attribute even on success, since we
9199 record the entry in an external table. */
9200 *no_add_attrs = true;
9201
9202 if (TREE_CODE (decl) != FUNCTION_DECL)
9203 warning (OPT_Wattributes, "%qE attribute ignored", name);
9204 else
9205 {
9206 tree wrap_decl = TREE_VALUE (args);
3e5a8b00 9207 if (error_operand_p (wrap_decl))
9208 ;
9209 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
b443c459 9210 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
4c0315d0 9211 error ("%qE argument not an identifier", name);
9212 else
9213 {
9214 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
9215 wrap_decl = lookup_name (wrap_decl);
9216 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
9217 {
9218 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
9219 TREE_TYPE (wrap_decl)))
9220 record_tm_replacement (wrap_decl, decl);
9221 else
9222 error ("%qD is not compatible with %qD", wrap_decl, decl);
9223 }
9224 else
cdf34fca 9225 error ("%qE argument is not a function", name);
4c0315d0 9226 }
9227 }
9228
9229 return NULL_TREE;
9230}
9231
9232/* Ignore the given attribute. Used when this attribute may be usefully
9233 overridden by the target, but is not used generically. */
9234
9235static tree
9236ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
9237 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9238 bool *no_add_attrs)
9239{
9240 *no_add_attrs = true;
9241 return NULL_TREE;
9242}
9243
fc09b200 9244/* Handle a "no vops" attribute; arguments as in
9245 struct attribute_spec.handler. */
9246
9247static tree
9248handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
9249 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9250 bool *ARG_UNUSED (no_add_attrs))
9251{
9252 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
9253 DECL_IS_NOVOPS (*node) = 1;
9254 return NULL_TREE;
9255}
9256
f8e93a2e 9257/* Handle a "deprecated" attribute; arguments as in
9258 struct attribute_spec.handler. */
1cae46be 9259
f8e93a2e 9260static tree
1cae46be 9261handle_deprecated_attribute (tree *node, tree name,
45c4e798 9262 tree args, int flags,
09347743 9263 bool *no_add_attrs)
f8e93a2e 9264{
9265 tree type = NULL_TREE;
9266 int warn = 0;
782858b8 9267 tree what = NULL_TREE;
1cae46be 9268
45c4e798 9269 if (!args)
9270 *no_add_attrs = true;
9271 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
9272 {
9273 error ("deprecated message is not a string");
9274 *no_add_attrs = true;
9275 }
9276
f8e93a2e 9277 if (DECL_P (*node))
9278 {
9279 tree decl = *node;
9280 type = TREE_TYPE (decl);
1cae46be 9281
f8e93a2e 9282 if (TREE_CODE (decl) == TYPE_DECL
9283 || TREE_CODE (decl) == PARM_DECL
b443c459 9284 || VAR_OR_FUNCTION_DECL_P (decl)
40c8d1dd 9285 || TREE_CODE (decl) == FIELD_DECL
5a4c69dd 9286 || TREE_CODE (decl) == CONST_DECL
40c8d1dd 9287 || objc_method_decl (TREE_CODE (decl)))
f8e93a2e 9288 TREE_DEPRECATED (decl) = 1;
9289 else
9290 warn = 1;
9291 }
9292 else if (TYPE_P (*node))
9293 {
9294 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 9295 *node = build_variant_type_copy (*node);
f8e93a2e 9296 TREE_DEPRECATED (*node) = 1;
9297 type = *node;
9298 }
9299 else
9300 warn = 1;
1cae46be 9301
f8e93a2e 9302 if (warn)
9303 {
9304 *no_add_attrs = true;
9305 if (type && TYPE_NAME (type))
9306 {
9307 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
782858b8 9308 what = TYPE_NAME (*node);
f8e93a2e 9309 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9310 && DECL_NAME (TYPE_NAME (type)))
782858b8 9311 what = DECL_NAME (TYPE_NAME (type));
f8e93a2e 9312 }
9313 if (what)
9b2d6d13 9314 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
f8e93a2e 9315 else
9b2d6d13 9316 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 9317 }
9318
9319 return NULL_TREE;
9320}
9321
f8e93a2e 9322/* Handle a "vector_size" attribute; arguments as in
9323 struct attribute_spec.handler. */
9324
9325static tree
1cae46be 9326handle_vector_size_attribute (tree *node, tree name, tree args,
9a03a746 9327 int ARG_UNUSED (flags),
09347743 9328 bool *no_add_attrs)
f8e93a2e 9329{
9330 unsigned HOST_WIDE_INT vecsize, nunits;
3754d046 9331 machine_mode orig_mode;
4917c376 9332 tree type = *node, new_type, size;
f8e93a2e 9333
9334 *no_add_attrs = true;
9335
4917c376 9336 size = TREE_VALUE (args);
3e5a8b00 9337 if (size && TREE_CODE (size) != IDENTIFIER_NODE
9338 && TREE_CODE (size) != FUNCTION_DECL)
caf62483 9339 size = default_conversion (size);
4917c376 9340
e913b5cd 9341 if (!tree_fits_uhwi_p (size))
f8e93a2e 9342 {
9b2d6d13 9343 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 9344 return NULL_TREE;
9345 }
9346
9347 /* Get the vector size (in bytes). */
e913b5cd 9348 vecsize = tree_to_uhwi (size);
f8e93a2e 9349
9350 /* We need to provide for vector pointers, vector arrays, and
9351 functions returning vectors. For example:
9352
9353 __attribute__((vector_size(16))) short *foo;
9354
9355 In this case, the mode is SI, but the type being modified is
9356 HI, so we need to look further. */
9357
9358 while (POINTER_TYPE_P (type)
9359 || TREE_CODE (type) == FUNCTION_TYPE
5bfb0742 9360 || TREE_CODE (type) == METHOD_TYPE
2cb0e5d9 9361 || TREE_CODE (type) == ARRAY_TYPE
9362 || TREE_CODE (type) == OFFSET_TYPE)
f8e93a2e 9363 type = TREE_TYPE (type);
9364
9365 /* Get the mode of the type being modified. */
9366 orig_mode = TYPE_MODE (type);
9367
2cb0e5d9 9368 if ((!INTEGRAL_TYPE_P (type)
9369 && !SCALAR_FLOAT_TYPE_P (type)
9370 && !FIXED_POINT_TYPE_P (type))
cee7491d 9371 || (!SCALAR_FLOAT_MODE_P (orig_mode)
9421ebb9 9372 && GET_MODE_CLASS (orig_mode) != MODE_INT
9373 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
e913b5cd 9374 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7ec31215 9375 || TREE_CODE (type) == BOOLEAN_TYPE)
f8e93a2e 9376 {
782858b8 9377 error ("invalid vector type for attribute %qE", name);
f8e93a2e 9378 return NULL_TREE;
9379 }
9380
e913b5cd 9381 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
39cc3e6d 9382 {
9383 error ("vector size not an integral multiple of component size");
9384 return NULL;
9385 }
9386
9387 if (vecsize == 0)
9388 {
9389 error ("zero vector size");
9390 return NULL;
9391 }
9392
f8e93a2e 9393 /* Calculate how many units fit in the vector. */
e913b5cd 9394 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
83e2a11b 9395 if (nunits & (nunits - 1))
f8e93a2e 9396 {
83e2a11b 9397 error ("number of components of the vector not a power of two");
f8e93a2e 9398 return NULL_TREE;
9399 }
9400
83e2a11b 9401 new_type = build_vector_type (type, nunits);
f8e93a2e 9402
9403 /* Build back pointers if needed. */
d991e6e8 9404 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
f8e93a2e 9405
9406 return NULL_TREE;
9407}
9408
dbf6c367 9409/* Handle the "nonnull" attribute. */
9410static tree
9a03a746 9411handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9412 tree args, int ARG_UNUSED (flags),
09347743 9413 bool *no_add_attrs)
dbf6c367 9414{
9415 tree type = *node;
9416 unsigned HOST_WIDE_INT attr_arg_num;
9417
9418 /* If no arguments are specified, all pointer arguments should be
d716ce75 9419 non-null. Verify a full prototype is given so that the arguments
dbf6c367 9420 will have the correct types when we actually check them later. */
84166705 9421 if (!args)
dbf6c367 9422 {
a36cf284 9423 if (!prototype_p (type))
dbf6c367 9424 {
9425 error ("nonnull attribute without arguments on a non-prototype");
4ee9c684 9426 *no_add_attrs = true;
dbf6c367 9427 }
9428 return NULL_TREE;
9429 }
9430
9431 /* Argument list specified. Verify that each argument number references
9432 a pointer argument. */
caf62483 9433 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
dbf6c367 9434 {
4ee9c684 9435 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
dbf6c367 9436
caf62483 9437 tree arg = TREE_VALUE (args);
9438 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9439 && TREE_CODE (arg) != FUNCTION_DECL)
9440 arg = default_conversion (arg);
9441
9442 if (!get_nonnull_operand (arg, &arg_num))
dbf6c367 9443 {
07e3a3d2 9444 error ("nonnull argument has invalid operand number (argument %lu)",
dbf6c367 9445 (unsigned long) attr_arg_num);
9446 *no_add_attrs = true;
9447 return NULL_TREE;
9448 }
9449
d0af78c5 9450 if (prototype_p (type))
dbf6c367 9451 {
d0af78c5 9452 function_args_iterator iter;
9453 tree argument;
9454
9455 function_args_iter_init (&iter, type);
9456 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
dbf6c367 9457 {
d0af78c5 9458 argument = function_args_iter_cond (&iter);
9459 if (argument == NULL_TREE || ck_num == arg_num)
dbf6c367 9460 break;
dbf6c367 9461 }
9462
84166705 9463 if (!argument
d0af78c5 9464 || TREE_CODE (argument) == VOID_TYPE)
dbf6c367 9465 {
07e3a3d2 9466 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
dbf6c367 9467 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9468 *no_add_attrs = true;
9469 return NULL_TREE;
9470 }
9471
d0af78c5 9472 if (TREE_CODE (argument) != POINTER_TYPE)
dbf6c367 9473 {
07e3a3d2 9474 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
dbf6c367 9475 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9476 *no_add_attrs = true;
9477 return NULL_TREE;
9478 }
9479 }
9480 }
9481
9482 return NULL_TREE;
9483}
9484
9485/* Check the argument list of a function call for null in argument slots
d01f58f9 9486 that are marked as requiring a non-null pointer argument. The NARGS
9487 arguments are passed in the array ARGARRAY.
9488*/
dbf6c367 9489
9490static void
d01f58f9 9491check_function_nonnull (tree attrs, int nargs, tree *argarray)
dbf6c367 9492{
9ca77b08 9493 tree a;
d01f58f9 9494 int i;
dbf6c367 9495
9ca77b08 9496 attrs = lookup_attribute ("nonnull", attrs);
9497 if (attrs == NULL_TREE)
9498 return;
9499
9500 a = attrs;
9501 /* See if any of the nonnull attributes has no arguments. If so,
9502 then every pointer argument is checked (in which case the check
9503 for pointer type is done in check_nonnull_arg). */
9504 if (TREE_VALUE (a) != NULL_TREE)
9505 do
9506 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9507 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9508
9509 if (a != NULL_TREE)
9510 for (i = 0; i < nargs; i++)
9511 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9512 i + 1);
9513 else
dbf6c367 9514 {
9ca77b08 9515 /* Walk the argument list. If we encounter an argument number we
9516 should check for non-null, do it. */
9517 for (i = 0; i < nargs; i++)
dbf6c367 9518 {
9ca77b08 9519 for (a = attrs; ; a = TREE_CHAIN (a))
4ee9c684 9520 {
9ca77b08 9521 a = lookup_attribute ("nonnull", a);
9522 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9523 break;
4ee9c684 9524 }
9ca77b08 9525
9526 if (a != NULL_TREE)
9527 check_function_arguments_recurse (check_nonnull_arg, NULL,
9528 argarray[i], i + 1);
dbf6c367 9529 }
9530 }
9531}
9532
50ca527f 9533/* Check that the Nth argument of a function call (counting backwards
d01f58f9 9534 from the end) is a (pointer)0. The NARGS arguments are passed in the
9535 array ARGARRAY. */
bf6c8de0 9536
9537static void
774e9d58 9538check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
bf6c8de0 9539{
774e9d58 9540 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
bf6c8de0 9541
9542 if (attr)
9543 {
d01f58f9 9544 int len = 0;
9545 int pos = 0;
9546 tree sentinel;
774e9d58 9547 function_args_iterator iter;
9548 tree t;
a0c938f0 9549
d01f58f9 9550 /* Skip over the named arguments. */
774e9d58 9551 FOREACH_FUNCTION_ARGS (fntype, t, iter)
a0c938f0 9552 {
774e9d58 9553 if (len == nargs)
9554 break;
d01f58f9 9555 len++;
9556 }
50ca527f 9557
d01f58f9 9558 if (TREE_VALUE (attr))
9559 {
9560 tree p = TREE_VALUE (TREE_VALUE (attr));
f9ae6f95 9561 pos = TREE_INT_CST_LOW (p);
d01f58f9 9562 }
50ca527f 9563
d01f58f9 9564 /* The sentinel must be one of the varargs, i.e.
9565 in position >= the number of fixed arguments. */
9566 if ((nargs - 1 - pos) < len)
9567 {
77a357e3 9568 warning (OPT_Wformat_,
d01f58f9 9569 "not enough variable arguments to fit a sentinel");
9570 return;
bf6c8de0 9571 }
d01f58f9 9572
9573 /* Validate the sentinel. */
9574 sentinel = argarray[nargs - 1 - pos];
9575 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9576 || !integer_zerop (sentinel))
9577 /* Although __null (in C++) is only an integer we allow it
9578 nevertheless, as we are guaranteed that it's exactly
9579 as wide as a pointer, and we don't want to force
9580 users to cast the NULL they have written there.
9581 We warn with -Wstrict-null-sentinel, though. */
9582 && (warn_strict_null_sentinel || null_node != sentinel))
77a357e3 9583 warning (OPT_Wformat_, "missing sentinel in function call");
bf6c8de0 9584 }
9585}
9586
dbf6c367 9587/* Helper for check_function_nonnull; given a list of operands which
9588 must be non-null in ARGS, determine if operand PARAM_NUM should be
9589 checked. */
9590
9591static bool
1cae46be 9592nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
dbf6c367 9593{
4ee9c684 9594 unsigned HOST_WIDE_INT arg_num = 0;
dbf6c367 9595
9596 for (; args; args = TREE_CHAIN (args))
9597 {
231bd014 9598 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9599
9600 gcc_assert (found);
dbf6c367 9601
9602 if (arg_num == param_num)
9603 return true;
9604 }
9605 return false;
9606}
9607
9608/* Check that the function argument PARAM (which is operand number
9609 PARAM_NUM) is non-null. This is called by check_function_nonnull
9610 via check_function_arguments_recurse. */
9611
9612static void
9a03a746 9613check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
1cae46be 9614 unsigned HOST_WIDE_INT param_num)
dbf6c367 9615{
9616 /* Just skip checking the argument if it's not a pointer. This can
9617 happen if the "nonnull" attribute was given without an operand
9618 list (which means to check every pointer argument). */
9619
9620 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9621 return;
9622
9623 if (integer_zerop (param))
155b601b 9624 warning (OPT_Wnonnull, "null argument where non-null required "
9625 "(argument %lu)", (unsigned long) param_num);
dbf6c367 9626}
9627
9628/* Helper for nonnull attribute handling; fetch the operand number
9629 from the attribute argument list. */
9630
9631static bool
1cae46be 9632get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
dbf6c367 9633{
e913b5cd 9634 /* Verify the arg number is a small constant. */
e1d65c9f 9635 if (tree_fits_uhwi_p (arg_num_expr))
e913b5cd 9636 {
f9ae6f95 9637 *valp = TREE_INT_CST_LOW (arg_num_expr);
e913b5cd 9638 return true;
9639 }
9640 else
dbf6c367 9641 return false;
dbf6c367 9642}
fa987697 9643
9644/* Handle a "nothrow" attribute; arguments as in
9645 struct attribute_spec.handler. */
9646
9647static tree
9a03a746 9648handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9649 int ARG_UNUSED (flags), bool *no_add_attrs)
fa987697 9650{
9651 if (TREE_CODE (*node) == FUNCTION_DECL)
9652 TREE_NOTHROW (*node) = 1;
9653 /* ??? TODO: Support types. */
9654 else
9655 {
9b2d6d13 9656 warning (OPT_Wattributes, "%qE attribute ignored", name);
fa987697 9657 *no_add_attrs = true;
9658 }
9659
9660 return NULL_TREE;
9661}
7acb29a3 9662
9663/* Handle a "cleanup" attribute; arguments as in
9664 struct attribute_spec.handler. */
9665
9666static tree
1cae46be 9667handle_cleanup_attribute (tree *node, tree name, tree args,
9a03a746 9668 int ARG_UNUSED (flags), bool *no_add_attrs)
7acb29a3 9669{
9670 tree decl = *node;
9671 tree cleanup_id, cleanup_decl;
9672
9673 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9674 for global destructors in C++. This requires infrastructure that
9675 we don't have generically at the moment. It's also not a feature
9676 we'd be missing too much, since we do have attribute constructor. */
f48c7f4a 9677 if (!VAR_P (decl) || TREE_STATIC (decl))
7acb29a3 9678 {
9b2d6d13 9679 warning (OPT_Wattributes, "%qE attribute ignored", name);
7acb29a3 9680 *no_add_attrs = true;
9681 return NULL_TREE;
9682 }
9683
9684 /* Verify that the argument is a function in scope. */
9685 /* ??? We could support pointers to functions here as well, if
9686 that was considered desirable. */
9687 cleanup_id = TREE_VALUE (args);
9688 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9689 {
07e3a3d2 9690 error ("cleanup argument not an identifier");
7acb29a3 9691 *no_add_attrs = true;
9692 return NULL_TREE;
9693 }
d1c41717 9694 cleanup_decl = lookup_name (cleanup_id);
7acb29a3 9695 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9696 {
07e3a3d2 9697 error ("cleanup argument not a function");
7acb29a3 9698 *no_add_attrs = true;
9699 return NULL_TREE;
9700 }
9701
1cae46be 9702 /* That the function has proper type is checked with the
7acb29a3 9703 eventual call to build_function_call. */
9704
9705 return NULL_TREE;
9706}
8a8cdb8d 9707
9708/* Handle a "warn_unused_result" attribute. No special handling. */
9709
9710static tree
9711handle_warn_unused_result_attribute (tree *node, tree name,
9a03a746 9712 tree ARG_UNUSED (args),
9713 int ARG_UNUSED (flags), bool *no_add_attrs)
8a8cdb8d 9714{
9715 /* Ignore the attribute for functions not returning any value. */
9716 if (VOID_TYPE_P (TREE_TYPE (*node)))
9717 {
9b2d6d13 9718 warning (OPT_Wattributes, "%qE attribute ignored", name);
8a8cdb8d 9719 *no_add_attrs = true;
9720 }
9721
9722 return NULL_TREE;
9723}
bf6c8de0 9724
9725/* Handle a "sentinel" attribute. */
9726
9727static tree
50ca527f 9728handle_sentinel_attribute (tree *node, tree name, tree args,
bf6c8de0 9729 int ARG_UNUSED (flags), bool *no_add_attrs)
9730{
a36cf284 9731 if (!prototype_p (*node))
bf6c8de0 9732 {
9b2d6d13 9733 warning (OPT_Wattributes,
9734 "%qE attribute requires prototypes with named arguments", name);
bf6c8de0 9735 *no_add_attrs = true;
bf6c8de0 9736 }
50ca527f 9737 else
9738 {
c33080b9 9739 if (!stdarg_p (*node))
a0c938f0 9740 {
9b2d6d13 9741 warning (OPT_Wattributes,
9742 "%qE attribute only applies to variadic functions", name);
50ca527f 9743 *no_add_attrs = true;
9744 }
9745 }
a0c938f0 9746
50ca527f 9747 if (args)
bf6c8de0 9748 {
50ca527f 9749 tree position = TREE_VALUE (args);
3e5a8b00 9750 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9751 && TREE_CODE (position) != FUNCTION_DECL)
9752 position = default_conversion (position);
50ca527f 9753
3e5a8b00 9754 if (TREE_CODE (position) != INTEGER_CST
9755 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
a0c938f0 9756 {
48e1416a 9757 warning (OPT_Wattributes,
01b54db5 9758 "requested position is not an integer constant");
50ca527f 9759 *no_add_attrs = true;
9760 }
9761 else
a0c938f0 9762 {
50ca527f 9763 if (tree_int_cst_lt (position, integer_zero_node))
9764 {
01b54db5 9765 warning (OPT_Wattributes,
9766 "requested position is less than zero");
50ca527f 9767 *no_add_attrs = true;
9768 }
9769 }
bf6c8de0 9770 }
a0c938f0 9771
bf6c8de0 9772 return NULL_TREE;
9773}
b5c26b42 9774
9775/* Handle a "type_generic" attribute. */
9776
9777static tree
9778handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9779 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9780 bool * ARG_UNUSED (no_add_attrs))
9781{
19fbe3a4 9782 /* Ensure we have a function type. */
9783 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
48e1416a 9784
19fbe3a4 9785 /* Ensure we have a variadic function. */
c33080b9 9786 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
b5c26b42 9787
9788 return NULL_TREE;
9789}
46f8e3b0 9790
24470055 9791/* Handle a "target" attribute. */
46f8e3b0 9792
9793static tree
24470055 9794handle_target_attribute (tree *node, tree name, tree args, int flags,
46f8e3b0 9795 bool *no_add_attrs)
9796{
9797 /* Ensure we have a function type. */
9798 if (TREE_CODE (*node) != FUNCTION_DECL)
9799 {
9800 warning (OPT_Wattributes, "%qE attribute ignored", name);
9801 *no_add_attrs = true;
9802 }
46f8e3b0 9803 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
ae0c3984 9804 flags))
46f8e3b0 9805 *no_add_attrs = true;
9806
9807 return NULL_TREE;
9808}
9809
9810/* Arguments being collected for optimization. */
9811typedef const char *const_char_p; /* For DEF_VEC_P. */
f1f41a6c 9812static GTY(()) vec<const_char_p, va_gc> *optimize_args;
46f8e3b0 9813
9814
9815/* Inner function to convert a TREE_LIST to argv string to parse the optimize
9816 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9817 false for #pragma GCC optimize. */
9818
9819bool
9820parse_optimize_options (tree args, bool attr_p)
9821{
9822 bool ret = true;
9823 unsigned opt_argc;
9824 unsigned i;
2becf397 9825 int saved_flag_strict_aliasing;
46f8e3b0 9826 const char **opt_argv;
615ef0bb 9827 struct cl_decoded_option *decoded_options;
9828 unsigned int decoded_options_count;
46f8e3b0 9829 tree ap;
9830
9831 /* Build up argv vector. Just in case the string is stored away, use garbage
9832 collected strings. */
f1f41a6c 9833 vec_safe_truncate (optimize_args, 0);
9834 vec_safe_push (optimize_args, (const char *) NULL);
46f8e3b0 9835
9836 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9837 {
9838 tree value = TREE_VALUE (ap);
9839
9840 if (TREE_CODE (value) == INTEGER_CST)
9841 {
9842 char buffer[20];
f9ae6f95 9843 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
f1f41a6c 9844 vec_safe_push (optimize_args, ggc_strdup (buffer));
46f8e3b0 9845 }
9846
9847 else if (TREE_CODE (value) == STRING_CST)
9848 {
9849 /* Split string into multiple substrings. */
9850 size_t len = TREE_STRING_LENGTH (value);
9851 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9852 char *end = p + len;
9853 char *comma;
9854 char *next_p = p;
9855
9856 while (next_p != NULL)
9857 {
9858 size_t len2;
9859 char *q, *r;
9860
9861 p = next_p;
9862 comma = strchr (p, ',');
9863 if (comma)
9864 {
9865 len2 = comma - p;
9866 *comma = '\0';
9867 next_p = comma+1;
9868 }
9869 else
9870 {
9871 len2 = end - p;
9872 next_p = NULL;
9873 }
9874
ba72912a 9875 r = q = (char *) ggc_alloc_atomic (len2 + 3);
46f8e3b0 9876
9877 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9878 options. */
9879 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9880 {
9881 ret = false;
9882 if (attr_p)
9883 warning (OPT_Wattributes,
ebd7c4c1 9884 "bad option %s to optimize attribute", p);
46f8e3b0 9885 else
9886 warning (OPT_Wpragmas,
e44b0a1f 9887 "bad option %s to pragma attribute", p);
46f8e3b0 9888 continue;
9889 }
9890
9891 if (*p != '-')
9892 {
9893 *r++ = '-';
9894
9895 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9896 itself is -Os, and any other switch begins with a -f. */
9897 if ((*p >= '0' && *p <= '9')
9898 || (p[0] == 's' && p[1] == '\0'))
9899 *r++ = 'O';
9900 else if (*p != 'O')
9901 *r++ = 'f';
9902 }
9903
9904 memcpy (r, p, len2);
9905 r[len2] = '\0';
f1f41a6c 9906 vec_safe_push (optimize_args, (const char *) q);
46f8e3b0 9907 }
9908
9909 }
9910 }
9911
f1f41a6c 9912 opt_argc = optimize_args->length ();
46f8e3b0 9913 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9914
9915 for (i = 1; i < opt_argc; i++)
f1f41a6c 9916 opt_argv[i] = (*optimize_args)[i];
46f8e3b0 9917
2becf397 9918 saved_flag_strict_aliasing = flag_strict_aliasing;
9919
46f8e3b0 9920 /* Now parse the options. */
f3f006ad 9921 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9922 &decoded_options,
9923 &decoded_options_count);
9924 decode_options (&global_options, &global_options_set,
3c6c0e40 9925 decoded_options, decoded_options_count,
9926 input_location, global_dc);
46f8e3b0 9927
4bec06b3 9928 targetm.override_options_after_change();
9929
2becf397 9930 /* Don't allow changing -fstrict-aliasing. */
9931 flag_strict_aliasing = saved_flag_strict_aliasing;
9932
f1f41a6c 9933 optimize_args->truncate (0);
46f8e3b0 9934 return ret;
9935}
9936
9937/* For handling "optimize" attribute. arguments as in
9938 struct attribute_spec.handler. */
9939
9940static tree
9941handle_optimize_attribute (tree *node, tree name, tree args,
9942 int ARG_UNUSED (flags), bool *no_add_attrs)
9943{
9944 /* Ensure we have a function type. */
9945 if (TREE_CODE (*node) != FUNCTION_DECL)
9946 {
9947 warning (OPT_Wattributes, "%qE attribute ignored", name);
9948 *no_add_attrs = true;
9949 }
9950 else
9951 {
9952 struct cl_optimization cur_opts;
9953 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9954
9955 /* Save current options. */
2c5d2e39 9956 cl_optimization_save (&cur_opts, &global_options);
46f8e3b0 9957
9958 /* If we previously had some optimization options, use them as the
9959 default. */
9960 if (old_opts)
2c5d2e39 9961 cl_optimization_restore (&global_options,
9962 TREE_OPTIMIZATION (old_opts));
46f8e3b0 9963
9964 /* Parse options, and update the vector. */
9965 parse_optimize_options (args, true);
9966 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
55310327 9967 = build_optimization_node (&global_options);
46f8e3b0 9968
9969 /* Restore current options. */
2c5d2e39 9970 cl_optimization_restore (&global_options, &cur_opts);
46f8e3b0 9971 }
9972
9973 return NULL_TREE;
9974}
48b14f50 9975
9976/* Handle a "no_split_stack" attribute. */
9977
9978static tree
9979handle_no_split_stack_attribute (tree *node, tree name,
9980 tree ARG_UNUSED (args),
9981 int ARG_UNUSED (flags),
9982 bool *no_add_attrs)
9983{
9984 tree decl = *node;
9985
9986 if (TREE_CODE (decl) != FUNCTION_DECL)
9987 {
9988 error_at (DECL_SOURCE_LOCATION (decl),
9989 "%qE attribute applies only to functions", name);
9990 *no_add_attrs = true;
9991 }
9992 else if (DECL_INITIAL (decl))
9993 {
9994 error_at (DECL_SOURCE_LOCATION (decl),
9995 "can%'t set %qE attribute after definition", name);
9996 *no_add_attrs = true;
9997 }
9998
9999 return NULL_TREE;
10000}
d7dcba40 10001
10002/* Handle a "returns_nonnull" attribute; arguments as in
10003 struct attribute_spec.handler. */
10004
10005static tree
10006handle_returns_nonnull_attribute (tree *node, tree, tree, int,
10007 bool *no_add_attrs)
10008{
10009 // Even without a prototype we still have a return type we can check.
10010 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
10011 {
10012 error ("returns_nonnull attribute on a function not returning a pointer");
10013 *no_add_attrs = true;
10014 }
10015 return NULL_TREE;
10016}
10017
74691f46 10018/* Handle a "designated_init" attribute; arguments as in
10019 struct attribute_spec.handler. */
10020
10021static tree
10022handle_designated_init_attribute (tree *node, tree name, tree, int,
10023 bool *no_add_attrs)
10024{
10025 if (TREE_CODE (*node) != RECORD_TYPE)
10026 {
10027 error ("%qE attribute is only valid on %<struct%> type", name);
10028 *no_add_attrs = true;
10029 }
10030 return NULL_TREE;
10031}
10032
dbf6c367 10033\f
774e9d58 10034/* Check for valid arguments being passed to a function with FNTYPE.
10035 There are NARGS arguments in the array ARGARRAY. */
dbf6c367 10036void
774e9d58 10037check_function_arguments (const_tree fntype, int nargs, tree *argarray)
dbf6c367 10038{
10039 /* Check for null being passed in a pointer argument that must be
10040 non-null. We also need to do this if format checking is enabled. */
10041
10042 if (warn_nonnull)
774e9d58 10043 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
dbf6c367 10044
10045 /* Check for errors in format strings. */
10046
068bea1e 10047 if (warn_format || warn_suggest_attribute_format)
774e9d58 10048 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
95c90e04 10049
10050 if (warn_format)
774e9d58 10051 check_function_sentinel (fntype, nargs, argarray);
dbf6c367 10052}
10053
10054/* Generic argument checking recursion routine. PARAM is the argument to
10055 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
10056 once the argument is resolved. CTX is context for the callback. */
10057void
1cae46be 10058check_function_arguments_recurse (void (*callback)
10059 (void *, tree, unsigned HOST_WIDE_INT),
10060 void *ctx, tree param,
10061 unsigned HOST_WIDE_INT param_num)
dbf6c367 10062{
72dd6141 10063 if (CONVERT_EXPR_P (param)
c44afe23 10064 && (TYPE_PRECISION (TREE_TYPE (param))
10065 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
dbf6c367 10066 {
10067 /* Strip coercion. */
10068 check_function_arguments_recurse (callback, ctx,
4ee9c684 10069 TREE_OPERAND (param, 0), param_num);
dbf6c367 10070 return;
10071 }
10072
10073 if (TREE_CODE (param) == CALL_EXPR)
10074 {
c2f47e15 10075 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
dbf6c367 10076 tree attrs;
10077 bool found_format_arg = false;
10078
10079 /* See if this is a call to a known internationalization function
10080 that modifies a format arg. Such a function may have multiple
10081 format_arg attributes (for example, ngettext). */
10082
10083 for (attrs = TYPE_ATTRIBUTES (type);
10084 attrs;
10085 attrs = TREE_CHAIN (attrs))
10086 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
10087 {
c2f47e15 10088 tree inner_arg;
dbf6c367 10089 tree format_num_expr;
10090 int format_num;
10091 int i;
c2f47e15 10092 call_expr_arg_iterator iter;
dbf6c367 10093
10094 /* Extract the argument number, which was previously checked
10095 to be valid. */
10096 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
ddb1be65 10097
e913b5cd 10098 format_num = tree_to_uhwi (format_num_expr);
dbf6c367 10099
c2f47e15 10100 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
10101 inner_arg != 0;
10102 inner_arg = next_call_expr_arg (&iter), i++)
dbf6c367 10103 if (i == format_num)
10104 {
10105 check_function_arguments_recurse (callback, ctx,
c2f47e15 10106 inner_arg, param_num);
dbf6c367 10107 found_format_arg = true;
10108 break;
10109 }
10110 }
10111
10112 /* If we found a format_arg attribute and did a recursive check,
10113 we are done with checking this argument. Otherwise, we continue
10114 and this will be considered a non-literal. */
10115 if (found_format_arg)
10116 return;
10117 }
10118
10119 if (TREE_CODE (param) == COND_EXPR)
10120 {
10121 /* Check both halves of the conditional expression. */
10122 check_function_arguments_recurse (callback, ctx,
4ee9c684 10123 TREE_OPERAND (param, 1), param_num);
dbf6c367 10124 check_function_arguments_recurse (callback, ctx,
4ee9c684 10125 TREE_OPERAND (param, 2), param_num);
dbf6c367 10126 return;
10127 }
10128
10129 (*callback) (ctx, param, param_num);
10130}
1f3233d1 10131
60cce472 10132/* Checks for a builtin function FNDECL that the number of arguments
10133 NARGS against the required number REQUIRED and issues an error if
10134 there is a mismatch. Returns true if the number of arguments is
10135 correct, otherwise false. */
d43cee80 10136
10137static bool
60cce472 10138builtin_function_validate_nargs (tree fndecl, int nargs, int required)
d43cee80 10139{
10140 if (nargs < required)
10141 {
60cce472 10142 error_at (input_location,
10143 "not enough arguments to function %qE", fndecl);
d43cee80 10144 return false;
10145 }
10146 else if (nargs > required)
10147 {
60cce472 10148 error_at (input_location,
10149 "too many arguments to function %qE", fndecl);
d43cee80 10150 return false;
10151 }
10152 return true;
10153}
10154
10155/* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
10156 Returns false if there was an error, otherwise true. */
10157
10158bool
10159check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
10160{
10161 if (!DECL_BUILT_IN (fndecl)
10162 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
10163 return true;
10164
10165 switch (DECL_FUNCTION_CODE (fndecl))
10166 {
10167 case BUILT_IN_CONSTANT_P:
60cce472 10168 return builtin_function_validate_nargs (fndecl, nargs, 1);
d43cee80 10169
10170 case BUILT_IN_ISFINITE:
10171 case BUILT_IN_ISINF:
c319d56a 10172 case BUILT_IN_ISINF_SIGN:
d43cee80 10173 case BUILT_IN_ISNAN:
10174 case BUILT_IN_ISNORMAL:
10902624 10175 case BUILT_IN_SIGNBIT:
60cce472 10176 if (builtin_function_validate_nargs (fndecl, nargs, 1))
d43cee80 10177 {
10178 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
10179 {
10180 error ("non-floating-point argument in call to "
10181 "function %qE", fndecl);
10182 return false;
10183 }
10184 return true;
10185 }
10186 return false;
10187
10188 case BUILT_IN_ISGREATER:
10189 case BUILT_IN_ISGREATEREQUAL:
10190 case BUILT_IN_ISLESS:
10191 case BUILT_IN_ISLESSEQUAL:
10192 case BUILT_IN_ISLESSGREATER:
10193 case BUILT_IN_ISUNORDERED:
60cce472 10194 if (builtin_function_validate_nargs (fndecl, nargs, 2))
d43cee80 10195 {
10196 enum tree_code code0, code1;
10197 code0 = TREE_CODE (TREE_TYPE (args[0]));
10198 code1 = TREE_CODE (TREE_TYPE (args[1]));
10199 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
10200 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10201 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
10202 {
10203 error ("non-floating-point arguments in call to "
10204 "function %qE", fndecl);
10205 return false;
10206 }
10207 return true;
10208 }
10209 return false;
10210
19fbe3a4 10211 case BUILT_IN_FPCLASSIFY:
60cce472 10212 if (builtin_function_validate_nargs (fndecl, nargs, 6))
19fbe3a4 10213 {
10214 unsigned i;
48e1416a 10215
19fbe3a4 10216 for (i=0; i<5; i++)
10217 if (TREE_CODE (args[i]) != INTEGER_CST)
10218 {
10219 error ("non-const integer argument %u in call to function %qE",
10220 i+1, fndecl);
10221 return false;
10222 }
10223
10224 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
10225 {
10226 error ("non-floating-point argument in call to function %qE",
10227 fndecl);
10228 return false;
10229 }
10230 return true;
10231 }
10232 return false;
10233
fca0886c 10234 case BUILT_IN_ASSUME_ALIGNED:
10235 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
10236 {
10237 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
10238 {
10239 error ("non-integer argument 3 in call to function %qE", fndecl);
10240 return false;
10241 }
10242 return true;
10243 }
10244 return false;
10245
0c93c8a9 10246 case BUILT_IN_ADD_OVERFLOW:
10247 case BUILT_IN_SUB_OVERFLOW:
10248 case BUILT_IN_MUL_OVERFLOW:
10249 if (builtin_function_validate_nargs (fndecl, nargs, 3))
10250 {
10251 unsigned i;
10252 for (i = 0; i < 2; i++)
10253 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
10254 {
10255 error ("argument %u in call to function %qE does not have "
10256 "integral type", i + 1, fndecl);
10257 return false;
10258 }
10259 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
10260 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
10261 {
10262 error ("argument 3 in call to function %qE does not have "
10263 "pointer to integer type", fndecl);
10264 return false;
10265 }
10266 return true;
10267 }
10268 return false;
10269
d43cee80 10270 default:
10271 return true;
10272 }
10273}
10274
860251be 10275/* Function to help qsort sort FIELD_DECLs by name order. */
10276
10277int
10278field_decl_cmp (const void *x_p, const void *y_p)
10279{
4fd61bc6 10280 const tree *const x = (const tree *const) x_p;
10281 const tree *const y = (const tree *const) y_p;
10282
860251be 10283 if (DECL_NAME (*x) == DECL_NAME (*y))
10284 /* A nontype is "greater" than a type. */
10285 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10286 if (DECL_NAME (*x) == NULL_TREE)
10287 return -1;
10288 if (DECL_NAME (*y) == NULL_TREE)
10289 return 1;
10290 if (DECL_NAME (*x) < DECL_NAME (*y))
10291 return -1;
10292 return 1;
10293}
10294
10295static struct {
10296 gt_pointer_operator new_value;
10297 void *cookie;
10298} resort_data;
10299
10300/* This routine compares two fields like field_decl_cmp but using the
10301pointer operator in resort_data. */
10302
10303static int
10304resort_field_decl_cmp (const void *x_p, const void *y_p)
10305{
4fd61bc6 10306 const tree *const x = (const tree *const) x_p;
10307 const tree *const y = (const tree *const) y_p;
860251be 10308
10309 if (DECL_NAME (*x) == DECL_NAME (*y))
10310 /* A nontype is "greater" than a type. */
10311 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10312 if (DECL_NAME (*x) == NULL_TREE)
10313 return -1;
10314 if (DECL_NAME (*y) == NULL_TREE)
10315 return 1;
10316 {
10317 tree d1 = DECL_NAME (*x);
10318 tree d2 = DECL_NAME (*y);
10319 resort_data.new_value (&d1, resort_data.cookie);
10320 resort_data.new_value (&d2, resort_data.cookie);
10321 if (d1 < d2)
10322 return -1;
10323 }
10324 return 1;
10325}
10326
10327/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10328
10329void
10330resort_sorted_fields (void *obj,
9a03a746 10331 void * ARG_UNUSED (orig_obj),
4ee9c684 10332 gt_pointer_operator new_value,
10333 void *cookie)
860251be 10334{
9a03a746 10335 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
860251be 10336 resort_data.new_value = new_value;
10337 resort_data.cookie = cookie;
10338 qsort (&sf->elts[0], sf->len, sizeof (tree),
4ee9c684 10339 resort_field_decl_cmp);
860251be 10340}
10341
209c9752 10342/* Subroutine of c_parse_error.
10343 Return the result of concatenating LHS and RHS. RHS is really
10344 a string literal, its first character is indicated by RHS_START and
cfee01e3 10345 RHS_SIZE is its length (including the terminating NUL character).
209c9752 10346
10347 The caller is responsible for deleting the returned pointer. */
10348
10349static char *
10350catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10351{
10352 const int lhs_size = strlen (lhs);
10353 char *result = XNEWVEC (char, lhs_size + rhs_size);
10354 strncpy (result, lhs, lhs_size);
10355 strncpy (result + lhs_size, rhs_start, rhs_size);
10356 return result;
10357}
10358
380c6697 10359/* Issue the error given by GMSGID, indicating that it occurred before
92b128ed 10360 TOKEN, which had the associated VALUE. */
10361
10362void
48e1416a 10363c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
ba99525e 10364 tree value, unsigned char token_flags)
92b128ed 10365{
209c9752 10366#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10367
10368 char *message = NULL;
92b128ed 10369
ba99525e 10370 if (token_type == CPP_EOF)
380c6697 10371 message = catenate_messages (gmsgid, " at end of input");
48e1416a 10372 else if (token_type == CPP_CHAR
10373 || token_type == CPP_WCHAR
ba99525e 10374 || token_type == CPP_CHAR16
30b1ba42 10375 || token_type == CPP_CHAR32
10376 || token_type == CPP_UTF8CHAR)
92b128ed 10377 {
f9ae6f95 10378 unsigned int val = TREE_INT_CST_LOW (value);
924bbf02 10379 const char *prefix;
10380
ba99525e 10381 switch (token_type)
924bbf02 10382 {
10383 default:
10384 prefix = "";
10385 break;
10386 case CPP_WCHAR:
10387 prefix = "L";
10388 break;
10389 case CPP_CHAR16:
10390 prefix = "u";
10391 break;
10392 case CPP_CHAR32:
10393 prefix = "U";
10394 break;
30b1ba42 10395 case CPP_UTF8CHAR:
10396 prefix = "u8";
10397 break;
924bbf02 10398 }
10399
92b128ed 10400 if (val <= UCHAR_MAX && ISGRAPH (val))
a0c938f0 10401 message = catenate_messages (gmsgid, " before %s'%c'");
92b128ed 10402 else
a0c938f0 10403 message = catenate_messages (gmsgid, " before %s'\\x%x'");
209c9752 10404
924bbf02 10405 error (message, prefix, val);
209c9752 10406 free (message);
10407 message = NULL;
92b128ed 10408 }
1898176c 10409 else if (token_type == CPP_CHAR_USERDEF
10410 || token_type == CPP_WCHAR_USERDEF
10411 || token_type == CPP_CHAR16_USERDEF
30b1ba42 10412 || token_type == CPP_CHAR32_USERDEF
10413 || token_type == CPP_UTF8CHAR_USERDEF)
1898176c 10414 message = catenate_messages (gmsgid,
10415 " before user-defined character literal");
10416 else if (token_type == CPP_STRING_USERDEF
10417 || token_type == CPP_WSTRING_USERDEF
10418 || token_type == CPP_STRING16_USERDEF
10419 || token_type == CPP_STRING32_USERDEF
10420 || token_type == CPP_UTF8STRING_USERDEF)
10421 message = catenate_messages (gmsgid, " before user-defined string literal");
48e1416a 10422 else if (token_type == CPP_STRING
10423 || token_type == CPP_WSTRING
ba99525e 10424 || token_type == CPP_STRING16
538ba11a 10425 || token_type == CPP_STRING32
10426 || token_type == CPP_UTF8STRING)
380c6697 10427 message = catenate_messages (gmsgid, " before string constant");
ba99525e 10428 else if (token_type == CPP_NUMBER)
380c6697 10429 message = catenate_messages (gmsgid, " before numeric constant");
ba99525e 10430 else if (token_type == CPP_NAME)
209c9752 10431 {
380c6697 10432 message = catenate_messages (gmsgid, " before %qE");
782858b8 10433 error (message, value);
209c9752 10434 free (message);
10435 message = NULL;
10436 }
ba99525e 10437 else if (token_type == CPP_PRAGMA)
b75b98aa 10438 message = catenate_messages (gmsgid, " before %<#pragma%>");
ba99525e 10439 else if (token_type == CPP_PRAGMA_EOL)
b75b98aa 10440 message = catenate_messages (gmsgid, " before end of line");
07b8f133 10441 else if (token_type == CPP_DECLTYPE)
10442 message = catenate_messages (gmsgid, " before %<decltype%>");
ba99525e 10443 else if (token_type < N_TTYPES)
209c9752 10444 {
380c6697 10445 message = catenate_messages (gmsgid, " before %qs token");
ba99525e 10446 error (message, cpp_type2name (token_type, token_flags));
209c9752 10447 free (message);
10448 message = NULL;
10449 }
92b128ed 10450 else
380c6697 10451 error (gmsgid);
209c9752 10452
10453 if (message)
10454 {
10455 error (message);
10456 free (message);
10457 }
a0c938f0 10458#undef catenate_messages
92b128ed 10459}
10460
3a79f5da 10461/* Return the gcc option code associated with the reason for a cpp
10462 message, or 0 if none. */
10463
10464static int
10465c_option_controlling_cpp_error (int reason)
10466{
7ff8db31 10467 const struct cpp_reason_option_codes_t *entry;
3a79f5da 10468
7ff8db31 10469 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
3a79f5da 10470 {
10471 if (entry->reason == reason)
10472 return entry->option_code;
10473 }
10474 return 0;
10475}
10476
7f5f3953 10477/* Callback from cpp_error for PFILE to print diagnostics from the
3a79f5da 10478 preprocessor. The diagnostic is of type LEVEL, with REASON set
10479 to the reason code if LEVEL is represents a warning, at location
7f5f3953 10480 LOCATION unless this is after lexing and the compiler's location
10481 should be used instead, with column number possibly overridden by
10482 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
10483 the arguments. Returns true if a diagnostic was emitted, false
10484 otherwise. */
10485
10486bool
3a79f5da 10487c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
7f5f3953 10488 location_t location, unsigned int column_override,
10489 const char *msg, va_list *ap)
10490{
10491 diagnostic_info diagnostic;
10492 diagnostic_t dlevel;
5ae82d58 10493 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
7f5f3953 10494 bool ret;
10495
10496 switch (level)
10497 {
10498 case CPP_DL_WARNING_SYSHDR:
10499 if (flag_no_output)
10500 return false;
5ae82d58 10501 global_dc->dc_warn_system_headers = 1;
7f5f3953 10502 /* Fall through. */
10503 case CPP_DL_WARNING:
10504 if (flag_no_output)
10505 return false;
10506 dlevel = DK_WARNING;
10507 break;
10508 case CPP_DL_PEDWARN:
10509 if (flag_no_output && !flag_pedantic_errors)
10510 return false;
10511 dlevel = DK_PEDWARN;
10512 break;
10513 case CPP_DL_ERROR:
10514 dlevel = DK_ERROR;
10515 break;
10516 case CPP_DL_ICE:
10517 dlevel = DK_ICE;
10518 break;
10519 case CPP_DL_NOTE:
10520 dlevel = DK_NOTE;
10521 break;
ff903809 10522 case CPP_DL_FATAL:
10523 dlevel = DK_FATAL;
10524 break;
7f5f3953 10525 default:
10526 gcc_unreachable ();
10527 }
10528 if (done_lexing)
10529 location = input_location;
10530 diagnostic_set_info_translated (&diagnostic, msg, ap,
10531 location, dlevel);
10532 if (column_override)
10533 diagnostic_override_column (&diagnostic, column_override);
3a79f5da 10534 diagnostic_override_option_index (&diagnostic,
10535 c_option_controlling_cpp_error (reason));
7f5f3953 10536 ret = report_diagnostic (&diagnostic);
10537 if (level == CPP_DL_WARNING_SYSHDR)
5ae82d58 10538 global_dc->dc_warn_system_headers = save_warn_system_headers;
7f5f3953 10539 return ret;
10540}
10541
624d37a6 10542/* Convert a character from the host to the target execution character
10543 set. cpplib handles this, mostly. */
10544
10545HOST_WIDE_INT
10546c_common_to_target_charset (HOST_WIDE_INT c)
10547{
10548 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10549 zero-extended under -fno-signed-char. cpplib insists that characters
10550 and character constants are always unsigned. Hence we must convert
10551 back and forth. */
10552 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10553
10554 uc = cpp_host_to_exec_charset (parse_in, uc);
10555
10556 if (flag_signed_char)
10557 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10558 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10559 else
10560 return uc;
10561}
10562
7549df0d 10563/* Fold an offsetof-like expression. EXPR is a nested sequence of component
10564 references with an INDIRECT_REF of a constant at the bottom; much like the
10565 traditional rendering of offsetof as a macro. Return the folded result. */
af28855b 10566
7549df0d 10567tree
10568fold_offsetof_1 (tree expr)
af28855b 10569{
af28855b 10570 tree base, off, t;
10571
10572 switch (TREE_CODE (expr))
10573 {
10574 case ERROR_MARK:
10575 return expr;
10576
6b11d2e3 10577 case VAR_DECL:
10578 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10579 return error_mark_node;
10580
d897f7c2 10581 case CALL_EXPR:
cf1a89a3 10582 case TARGET_EXPR:
d897f7c2 10583 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10584 return error_mark_node;
10585
d897f7c2 10586 case NOP_EXPR:
10587 case INDIRECT_REF:
7549df0d 10588 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
64ed018c 10589 {
10590 error ("cannot apply %<offsetof%> to a non constant address");
10591 return error_mark_node;
10592 }
7549df0d 10593 return TREE_OPERAND (expr, 0);
d897f7c2 10594
af28855b 10595 case COMPONENT_REF:
7549df0d 10596 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
af28855b 10597 if (base == error_mark_node)
10598 return base;
10599
10600 t = TREE_OPERAND (expr, 1);
10601 if (DECL_C_BIT_FIELD (t))
10602 {
10603 error ("attempt to take address of bit-field structure "
782858b8 10604 "member %qD", t);
af28855b 10605 return error_mark_node;
10606 }
389dd41b 10607 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
e913b5cd 10608 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
389dd41b 10609 / BITS_PER_UNIT));
af28855b 10610 break;
10611
10612 case ARRAY_REF:
7549df0d 10613 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
af28855b 10614 if (base == error_mark_node)
10615 return base;
10616
10617 t = TREE_OPERAND (expr, 1);
64ed018c 10618
10619 /* Check if the offset goes beyond the upper bound of the array. */
7549df0d 10620 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
e0559d69 10621 {
10622 tree upbound = array_ref_up_bound (expr);
10623 if (upbound != NULL_TREE
10624 && TREE_CODE (upbound) == INTEGER_CST
10625 && !tree_int_cst_equal (upbound,
10626 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10627 {
10628 upbound = size_binop (PLUS_EXPR, upbound,
10629 build_int_cst (TREE_TYPE (upbound), 1));
10630 if (tree_int_cst_lt (upbound, t))
10631 {
10632 tree v;
10633
10634 for (v = TREE_OPERAND (expr, 0);
10635 TREE_CODE (v) == COMPONENT_REF;
10636 v = TREE_OPERAND (v, 0))
10637 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10638 == RECORD_TYPE)
10639 {
1767a056 10640 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10641 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
e0559d69 10642 if (TREE_CODE (fld_chain) == FIELD_DECL)
10643 break;
10644
10645 if (fld_chain)
10646 break;
10647 }
10648 /* Don't warn if the array might be considered a poor
10649 man's flexible array member with a very permissive
10650 definition thereof. */
10651 if (TREE_CODE (v) == ARRAY_REF
10652 || TREE_CODE (v) == COMPONENT_REF)
10653 warning (OPT_Warray_bounds,
10654 "index %E denotes an offset "
10655 "greater than size of %qT",
10656 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10657 }
10658 }
10659 }
7549df0d 10660
10661 t = convert (sizetype, t);
10662 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
af28855b 10663 break;
10664
ede90cc2 10665 case COMPOUND_EXPR:
10666 /* Handle static members of volatile structs. */
10667 t = TREE_OPERAND (expr, 1);
f48c7f4a 10668 gcc_assert (VAR_P (t));
7549df0d 10669 return fold_offsetof_1 (t);
ede90cc2 10670
af28855b 10671 default:
231bd014 10672 gcc_unreachable ();
af28855b 10673 }
10674
7549df0d 10675 return fold_build_pointer_plus (base, off);
af28855b 10676}
10677
7549df0d 10678/* Likewise, but convert it to the return type of offsetof. */
10679
af28855b 10680tree
7549df0d 10681fold_offsetof (tree expr)
af28855b 10682{
7549df0d 10683 return convert (size_type_node, fold_offsetof_1 (expr));
af28855b 10684}
10685
b9bdfa0b 10686/* Warn for A ?: C expressions (with B omitted) where A is a boolean
10687 expression, because B will always be true. */
10688
10689void
10690warn_for_omitted_condop (location_t location, tree cond)
10691{
10692 if (truth_value_p (TREE_CODE (cond)))
10693 warning_at (location, OPT_Wparentheses,
10694 "the omitted middle operand in ?: will always be %<true%>, "
10695 "suggest explicit middle operand");
10696}
10697
a1f90215 10698/* Give an error for storing into ARG, which is 'const'. USE indicates
10699 how ARG was being used. */
10700
10701void
f2697631 10702readonly_error (location_t loc, tree arg, enum lvalue_use use)
a1f90215 10703{
10704 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10705 || use == lv_asm);
10706 /* Using this macro rather than (for example) arrays of messages
10707 ensures that all the format strings are checked at compile
10708 time. */
10709#define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10710 : (use == lv_increment ? (I) \
10711 : (use == lv_decrement ? (D) : (AS))))
10712 if (TREE_CODE (arg) == COMPONENT_REF)
10713 {
10714 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
f2697631 10715 error_at (loc, READONLY_MSG (G_("assignment of member "
10716 "%qD in read-only object"),
10717 G_("increment of member "
10718 "%qD in read-only object"),
10719 G_("decrement of member "
10720 "%qD in read-only object"),
10721 G_("member %qD in read-only object "
10722 "used as %<asm%> output")),
10723 TREE_OPERAND (arg, 1));
a1f90215 10724 else
f2697631 10725 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10726 G_("increment of read-only member %qD"),
10727 G_("decrement of read-only member %qD"),
10728 G_("read-only member %qD used as %<asm%> output")),
10729 TREE_OPERAND (arg, 1));
a1f90215 10730 }
f48c7f4a 10731 else if (VAR_P (arg))
f2697631 10732 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10733 G_("increment of read-only variable %qD"),
10734 G_("decrement of read-only variable %qD"),
10735 G_("read-only variable %qD used as %<asm%> output")),
10736 arg);
a1f90215 10737 else if (TREE_CODE (arg) == PARM_DECL)
f2697631 10738 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10739 G_("increment of read-only parameter %qD"),
10740 G_("decrement of read-only parameter %qD"),
10741 G_("read-only parameter %qD use as %<asm%> output")),
10742 arg);
a1f90215 10743 else if (TREE_CODE (arg) == RESULT_DECL)
10744 {
10745 gcc_assert (c_dialect_cxx ());
f2697631 10746 error_at (loc, READONLY_MSG (G_("assignment of "
10747 "read-only named return value %qD"),
10748 G_("increment of "
10749 "read-only named return value %qD"),
10750 G_("decrement of "
10751 "read-only named return value %qD"),
10752 G_("read-only named return value %qD "
10753 "used as %<asm%>output")),
10754 arg);
a1f90215 10755 }
10756 else if (TREE_CODE (arg) == FUNCTION_DECL)
f2697631 10757 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10758 G_("increment of function %qD"),
10759 G_("decrement of function %qD"),
10760 G_("function %qD used as %<asm%> output")),
10761 arg);
a1f90215 10762 else
f2697631 10763 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10764 G_("increment of read-only location %qE"),
10765 G_("decrement of read-only location %qE"),
10766 G_("read-only location %qE used as %<asm%> output")),
10767 arg);
a1f90215 10768}
10769
e35976b1 10770/* Print an error message for an invalid lvalue. USE says
fdd84b77 10771 how the lvalue is being used and so selects the error message. LOC
10772 is the location for the error. */
ab6bb714 10773
e35976b1 10774void
fdd84b77 10775lvalue_error (location_t loc, enum lvalue_use use)
ab6bb714 10776{
e35976b1 10777 switch (use)
ab6bb714 10778 {
e35976b1 10779 case lv_assign:
fdd84b77 10780 error_at (loc, "lvalue required as left operand of assignment");
e35976b1 10781 break;
10782 case lv_increment:
fdd84b77 10783 error_at (loc, "lvalue required as increment operand");
e35976b1 10784 break;
10785 case lv_decrement:
fdd84b77 10786 error_at (loc, "lvalue required as decrement operand");
e35976b1 10787 break;
10788 case lv_addressof:
fdd84b77 10789 error_at (loc, "lvalue required as unary %<&%> operand");
e35976b1 10790 break;
10791 case lv_asm:
fdd84b77 10792 error_at (loc, "lvalue required in asm statement");
e35976b1 10793 break;
10794 default:
10795 gcc_unreachable ();
ab6bb714 10796 }
ab6bb714 10797}
b1bbc8e5 10798
10799/* Print an error message for an invalid indirection of type TYPE.
10800 ERRSTRING is the name of the operator for the indirection. */
10801
10802void
10803invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10804{
10805 switch (errstring)
10806 {
10807 case RO_NULL:
10808 gcc_assert (c_dialect_cxx ());
10809 error_at (loc, "invalid type argument (have %qT)", type);
10810 break;
10811 case RO_ARRAY_INDEXING:
10812 error_at (loc,
10813 "invalid type argument of array indexing (have %qT)",
10814 type);
10815 break;
10816 case RO_UNARY_STAR:
10817 error_at (loc,
10818 "invalid type argument of unary %<*%> (have %qT)",
10819 type);
10820 break;
10821 case RO_ARROW:
10822 error_at (loc,
10823 "invalid type argument of %<->%> (have %qT)",
10824 type);
10825 break;
7354a89b 10826 case RO_ARROW_STAR:
10827 error_at (loc,
10828 "invalid type argument of %<->*%> (have %qT)",
10829 type);
10830 break;
b1bbc8e5 10831 case RO_IMPLICIT_CONVERSION:
10832 error_at (loc,
10833 "invalid type argument of implicit conversion (have %qT)",
10834 type);
10835 break;
10836 default:
10837 gcc_unreachable ();
10838 }
10839}
c271bdb2 10840\f
10841/* *PTYPE is an incomplete array. Complete it with a domain based on
10842 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10843 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10844 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10845
10846int
10847complete_array_type (tree *ptype, tree initial_value, bool do_default)
10848{
10849 tree maxindex, type, main_type, elt, unqual_elt;
10850 int failure = 0, quals;
6753bca0 10851 hashval_t hashcode = 0;
f5298614 10852 bool overflow_p = false;
c271bdb2 10853
10854 maxindex = size_zero_node;
10855 if (initial_value)
10856 {
10857 if (TREE_CODE (initial_value) == STRING_CST)
10858 {
10859 int eltsize
10860 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10861 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10862 }
10863 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10864 {
f1f41a6c 10865 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
c271bdb2 10866
f1f41a6c 10867 if (vec_safe_is_empty (v))
c271bdb2 10868 {
10869 if (pedantic)
10870 failure = 3;
7542c3b4 10871 maxindex = ssize_int (-1);
c271bdb2 10872 }
10873 else
10874 {
10875 tree curindex;
c75b4594 10876 unsigned HOST_WIDE_INT cnt;
10877 constructor_elt *ce;
cee43f7e 10878 bool fold_p = false;
c271bdb2 10879
f1f41a6c 10880 if ((*v)[0].index)
f5298614 10881 maxindex = (*v)[0].index, fold_p = true;
10882
c271bdb2 10883 curindex = maxindex;
10884
f1f41a6c 10885 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
c271bdb2 10886 {
cee43f7e 10887 bool curfold_p = false;
c75b4594 10888 if (ce->index)
cee43f7e 10889 curindex = ce->index, curfold_p = true;
c271bdb2 10890 else
cee43f7e 10891 {
10892 if (fold_p)
f5298614 10893 {
10894 /* Since we treat size types now as ordinary
10895 unsigned types, we need an explicit overflow
10896 check. */
10897 tree orig = curindex;
10898 curindex = fold_convert (sizetype, curindex);
10899 overflow_p |= tree_int_cst_lt (curindex, orig);
10900 }
389dd41b 10901 curindex = size_binop (PLUS_EXPR, curindex,
10902 size_one_node);
cee43f7e 10903 }
c271bdb2 10904 if (tree_int_cst_lt (maxindex, curindex))
cee43f7e 10905 maxindex = curindex, fold_p = curfold_p;
c271bdb2 10906 }
f5298614 10907 if (fold_p)
10908 {
10909 tree orig = maxindex;
10910 maxindex = fold_convert (sizetype, maxindex);
10911 overflow_p |= tree_int_cst_lt (maxindex, orig);
10912 }
c271bdb2 10913 }
10914 }
10915 else
10916 {
10917 /* Make an error message unless that happened already. */
10918 if (initial_value != error_mark_node)
10919 failure = 1;
10920 }
10921 }
10922 else
10923 {
10924 failure = 2;
10925 if (!do_default)
10926 return failure;
10927 }
10928
10929 type = *ptype;
10930 elt = TREE_TYPE (type);
10931 quals = TYPE_QUALS (strip_array_types (elt));
10932 if (quals == 0)
10933 unqual_elt = elt;
10934 else
6d5d708e 10935 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
c271bdb2 10936
10937 /* Using build_distinct_type_copy and modifying things afterward instead
10938 of using build_array_type to create a new type preserves all of the
10939 TYPE_LANG_FLAG_? bits that the front end may have set. */
10940 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10941 TREE_TYPE (main_type) = unqual_elt;
783bb57e 10942 TYPE_DOMAIN (main_type)
10943 = build_range_type (TREE_TYPE (maxindex),
10944 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
c271bdb2 10945 layout_type (main_type);
10946
6753bca0 10947 /* Make sure we have the canonical MAIN_TYPE. */
10948 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
48e1416a 10949 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
6753bca0 10950 hashcode);
10951 main_type = type_hash_canon (hashcode, main_type);
10952
796735dc 10953 /* Fix the canonical type. */
10954 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10955 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10956 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10957 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10958 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10959 != TYPE_DOMAIN (main_type)))
48e1416a 10960 TYPE_CANONICAL (main_type)
796735dc 10961 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10962 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10963 else
10964 TYPE_CANONICAL (main_type) = main_type;
10965
c271bdb2 10966 if (quals == 0)
10967 type = main_type;
10968 else
10969 type = c_build_qualified_type (main_type, quals);
10970
4f5b8f2a 10971 if (COMPLETE_TYPE_P (type)
10972 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
f5298614 10973 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
4f5b8f2a 10974 {
10975 error ("size of array is too large");
10976 /* If we proceed with the array type as it is, we'll eventually
08f817b3 10977 crash in tree_to_[su]hwi(). */
4f5b8f2a 10978 type = error_mark_node;
10979 }
10980
c271bdb2 10981 *ptype = type;
10982 return failure;
10983}
ab6bb714 10984
93426222 10985/* Like c_mark_addressable but don't check register qualifier. */
10986void
10987c_common_mark_addressable_vec (tree t)
10988{
10989 while (handled_component_p (t))
10990 t = TREE_OPERAND (t, 0);
f48c7f4a 10991 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
93426222 10992 return;
10993 TREE_ADDRESSABLE (t) = 1;
10994}
10995
10996
b6a5fc45 10997\f
10998/* Used to help initialize the builtin-types.def table. When a type of
10999 the correct size doesn't exist, use error_mark_node instead of NULL.
11000 The later results in segfaults even when a decl using the type doesn't
11001 get invoked. */
11002
11003tree
11004builtin_type_for_size (int size, bool unsignedp)
11005{
a51edb4c 11006 tree type = c_common_type_for_size (size, unsignedp);
b6a5fc45 11007 return type ? type : error_mark_node;
11008}
11009
11010/* A helper function for resolve_overloaded_builtin in resolving the
11011 overloaded __sync_ builtins. Returns a positive power of 2 if the
11012 first operand of PARAMS is a pointer to a supported data type.
11013 Returns 0 if an error is encountered. */
11014
11015static int
f1f41a6c 11016sync_resolve_size (tree function, vec<tree, va_gc> *params)
b6a5fc45 11017{
11018 tree type;
11019 int size;
11020
f1f41a6c 11021 if (!params)
b6a5fc45 11022 {
11023 error ("too few arguments to function %qE", function);
11024 return 0;
11025 }
11026
f1f41a6c 11027 type = TREE_TYPE ((*params)[0]);
0d284870 11028 if (TREE_CODE (type) == ARRAY_TYPE)
11029 {
11030 /* Force array-to-pointer decay for C++. */
11031 gcc_assert (c_dialect_cxx());
11032 (*params)[0] = default_conversion ((*params)[0]);
11033 type = TREE_TYPE ((*params)[0]);
11034 }
b6a5fc45 11035 if (TREE_CODE (type) != POINTER_TYPE)
11036 goto incompatible;
11037
11038 type = TREE_TYPE (type);
11039 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
11040 goto incompatible;
11041
e913b5cd 11042 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
27213ba3 11043 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
b6a5fc45 11044 return size;
11045
11046 incompatible:
11047 error ("incompatible type for argument %d of %qE", 1, function);
11048 return 0;
11049}
11050
a0c938f0 11051/* A helper function for resolve_overloaded_builtin. Adds casts to
b6a5fc45 11052 PARAMS to make arguments match up with those of FUNCTION. Drops
11053 the variadic arguments at the end. Returns false if some error
11054 was encountered; true on success. */
11055
11056static bool
1cd6e20d 11057sync_resolve_params (location_t loc, tree orig_function, tree function,
f1f41a6c 11058 vec<tree, va_gc> *params, bool orig_format)
b6a5fc45 11059{
d0af78c5 11060 function_args_iterator iter;
b6a5fc45 11061 tree ptype;
b9c74b4d 11062 unsigned int parmnum;
b6a5fc45 11063
d0af78c5 11064 function_args_iter_init (&iter, TREE_TYPE (function));
b6a5fc45 11065 /* We've declared the implementation functions to use "volatile void *"
11066 as the pointer parameter, so we shouldn't get any complaints from the
11067 call to check_function_arguments what ever type the user used. */
d0af78c5 11068 function_args_iter_next (&iter);
f1f41a6c 11069 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
b560fabd 11070 ptype = TYPE_MAIN_VARIANT (ptype);
b6a5fc45 11071
11072 /* For the rest of the values, we need to cast these to FTYPE, so that we
11073 don't get warnings for passing pointer types, etc. */
b9c74b4d 11074 parmnum = 0;
d0af78c5 11075 while (1)
b6a5fc45 11076 {
d0af78c5 11077 tree val, arg_type;
11078
11079 arg_type = function_args_iter_cond (&iter);
11080 /* XXX void_type_node belies the abstraction. */
11081 if (arg_type == void_type_node)
11082 break;
b6a5fc45 11083
b9c74b4d 11084 ++parmnum;
f1f41a6c 11085 if (params->length () <= parmnum)
b6a5fc45 11086 {
1cd6e20d 11087 error_at (loc, "too few arguments to function %qE", orig_function);
b6a5fc45 11088 return false;
11089 }
11090
0f6a7cb7 11091 /* Only convert parameters if arg_type is unsigned integer type with
11092 new format sync routines, i.e. don't attempt to convert pointer
11093 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
11094 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
11095 kinds). */
11096 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
1cd6e20d 11097 {
11098 /* Ideally for the first conversion we'd use convert_for_assignment
11099 so that we get warnings for anything that doesn't match the pointer
11100 type. This isn't portable across the C and C++ front ends atm. */
f1f41a6c 11101 val = (*params)[parmnum];
1cd6e20d 11102 val = convert (ptype, val);
11103 val = convert (arg_type, val);
f1f41a6c 11104 (*params)[parmnum] = val;
1cd6e20d 11105 }
b6a5fc45 11106
d0af78c5 11107 function_args_iter_next (&iter);
b6a5fc45 11108 }
11109
1cd6e20d 11110 /* __atomic routines are not variadic. */
f1f41a6c 11111 if (!orig_format && params->length () != parmnum + 1)
1cd6e20d 11112 {
11113 error_at (loc, "too many arguments to function %qE", orig_function);
11114 return false;
11115 }
11116
b6a5fc45 11117 /* The definition of these primitives is variadic, with the remaining
11118 being "an optional list of variables protected by the memory barrier".
11119 No clue what that's supposed to mean, precisely, but we consider all
11120 call-clobbered variables to be protected so we're safe. */
f1f41a6c 11121 params->truncate (parmnum + 1);
b6a5fc45 11122
11123 return true;
11124}
11125
a0c938f0 11126/* A helper function for resolve_overloaded_builtin. Adds a cast to
b6a5fc45 11127 RESULT to make it match the type of the first pointer argument in
11128 PARAMS. */
11129
11130static tree
1cd6e20d 11131sync_resolve_return (tree first_param, tree result, bool orig_format)
b6a5fc45 11132{
b9c74b4d 11133 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
1cd6e20d 11134 tree rtype = TREE_TYPE (result);
10080eac 11135 ptype = TYPE_MAIN_VARIANT (ptype);
1cd6e20d 11136
11137 /* New format doesn't require casting unless the types are the same size. */
11138 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
11139 return convert (ptype, result);
11140 else
11141 return result;
11142}
11143
11144/* This function verifies the PARAMS to generic atomic FUNCTION.
11145 It returns the size if all the parameters are the same size, otherwise
11146 0 is returned if the parameters are invalid. */
11147
11148static int
f1f41a6c 11149get_atomic_generic_size (location_t loc, tree function,
11150 vec<tree, va_gc> *params)
1cd6e20d 11151{
11152 unsigned int n_param;
11153 unsigned int n_model;
11154 unsigned int x;
11155 int size_0;
11156 tree type_0;
11157
11158 /* Determine the parameter makeup. */
11159 switch (DECL_FUNCTION_CODE (function))
11160 {
11161 case BUILT_IN_ATOMIC_EXCHANGE:
11162 n_param = 4;
11163 n_model = 1;
11164 break;
11165 case BUILT_IN_ATOMIC_LOAD:
11166 case BUILT_IN_ATOMIC_STORE:
11167 n_param = 3;
11168 n_model = 1;
11169 break;
11170 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11171 n_param = 6;
11172 n_model = 2;
11173 break;
11174 default:
1d581089 11175 gcc_unreachable ();
1cd6e20d 11176 }
11177
f1f41a6c 11178 if (vec_safe_length (params) != n_param)
1cd6e20d 11179 {
11180 error_at (loc, "incorrect number of arguments to function %qE", function);
11181 return 0;
11182 }
11183
11184 /* Get type of first parameter, and determine its size. */
f1f41a6c 11185 type_0 = TREE_TYPE ((*params)[0]);
0d284870 11186 if (TREE_CODE (type_0) == ARRAY_TYPE)
11187 {
11188 /* Force array-to-pointer decay for C++. */
11189 gcc_assert (c_dialect_cxx());
11190 (*params)[0] = default_conversion ((*params)[0]);
11191 type_0 = TREE_TYPE ((*params)[0]);
11192 }
1d581089 11193 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
11194 {
11195 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
11196 function);
11197 return 0;
11198 }
11199
11200 /* Types must be compile time constant sizes. */
11201 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
1cd6e20d 11202 {
1d581089 11203 error_at (loc,
11204 "argument 1 of %qE must be a pointer to a constant size type",
11205 function);
1cd6e20d 11206 return 0;
11207 }
1d581089 11208
e913b5cd 11209 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
1cd6e20d 11210
1d581089 11211 /* Zero size objects are not allowed. */
11212 if (size_0 == 0)
11213 {
11214 error_at (loc,
11215 "argument 1 of %qE must be a pointer to a nonzero size object",
11216 function);
11217 return 0;
11218 }
11219
1cd6e20d 11220 /* Check each other parameter is a pointer and the same size. */
11221 for (x = 0; x < n_param - n_model; x++)
11222 {
11223 int size;
f1f41a6c 11224 tree type = TREE_TYPE ((*params)[x]);
a04e8d62 11225 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
1cd6e20d 11226 if (n_param == 6 && x == 3)
11227 continue;
11228 if (!POINTER_TYPE_P (type))
11229 {
11230 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
11231 function);
11232 return 0;
11233 }
1f6be080 11234 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
11235 size = type_size ? tree_to_uhwi (type_size) : 0;
1cd6e20d 11236 if (size != size_0)
11237 {
11238 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
11239 function);
11240 return 0;
11241 }
11242 }
11243
11244 /* Check memory model parameters for validity. */
11245 for (x = n_param - n_model ; x < n_param; x++)
11246 {
f1f41a6c 11247 tree p = (*params)[x];
1cd6e20d 11248 if (TREE_CODE (p) == INTEGER_CST)
11249 {
e913b5cd 11250 int i = tree_to_uhwi (p);
a372f7ca 11251 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
1cd6e20d 11252 {
11253 warning_at (loc, OPT_Winvalid_memory_model,
11254 "invalid memory model argument %d of %qE", x + 1,
11255 function);
1cd6e20d 11256 }
11257 }
11258 else
11259 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
11260 {
11261 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
11262 function);
11263 return 0;
11264 }
11265 }
11266
11267 return size_0;
11268}
11269
11270
11271/* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
11272 at the beginning of the parameter list PARAMS representing the size of the
11273 objects. This is to match the library ABI requirement. LOC is the location
11274 of the function call.
11275 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
11276 returned to allow the external call to be constructed. */
11277
11278static tree
11279add_atomic_size_parameter (unsigned n, location_t loc, tree function,
f1f41a6c 11280 vec<tree, va_gc> *params)
1cd6e20d 11281{
11282 tree size_node;
11283
11284 /* Insert a SIZE_T parameter as the first param. If there isn't
11285 enough space, allocate a new vector and recursively re-build with that. */
f1f41a6c 11286 if (!params->space (1))
1cd6e20d 11287 {
11288 unsigned int z, len;
f1f41a6c 11289 vec<tree, va_gc> *v;
1cd6e20d 11290 tree f;
11291
f1f41a6c 11292 len = params->length ();
11293 vec_alloc (v, len + 1);
5a672e62 11294 v->quick_push (build_int_cst (size_type_node, n));
1cd6e20d 11295 for (z = 0; z < len; z++)
f1f41a6c 11296 v->quick_push ((*params)[z]);
ec761d5a 11297 f = build_function_call_vec (loc, vNULL, function, v, NULL);
f1f41a6c 11298 vec_free (v);
1cd6e20d 11299 return f;
11300 }
11301
11302 /* Add the size parameter and leave as a function call for processing. */
11303 size_node = build_int_cst (size_type_node, n);
f1f41a6c 11304 params->quick_insert (0, size_node);
1cd6e20d 11305 return NULL_TREE;
11306}
11307
11308
a056826c 11309/* Return whether atomic operations for naturally aligned N-byte
11310 arguments are supported, whether inline or through libatomic. */
11311static bool
11312atomic_size_supported_p (int n)
11313{
11314 switch (n)
11315 {
11316 case 1:
11317 case 2:
11318 case 4:
11319 case 8:
11320 return true;
11321
11322 case 16:
11323 return targetm.scalar_mode_supported_p (TImode);
11324
11325 default:
11326 return false;
11327 }
11328}
11329
1cd6e20d 11330/* This will process an __atomic_exchange function call, determine whether it
11331 needs to be mapped to the _N variation, or turned into a library call.
11332 LOC is the location of the builtin call.
11333 FUNCTION is the DECL that has been invoked;
11334 PARAMS is the argument list for the call. The return value is non-null
11335 TRUE is returned if it is translated into the proper format for a call to the
11336 external library, and NEW_RETURN is set the tree for that function.
11337 FALSE is returned if processing for the _N variation is required, and
47ae02b7 11338 NEW_RETURN is set to the return value the result is copied into. */
1cd6e20d 11339static bool
11340resolve_overloaded_atomic_exchange (location_t loc, tree function,
f1f41a6c 11341 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11342{
11343 tree p0, p1, p2, p3;
11344 tree I_type, I_type_ptr;
11345 int n = get_atomic_generic_size (loc, function, params);
11346
1d581089 11347 /* Size of 0 is an error condition. */
11348 if (n == 0)
11349 {
11350 *new_return = error_mark_node;
11351 return true;
11352 }
11353
1cd6e20d 11354 /* If not a lock-free size, change to the library generic format. */
a056826c 11355 if (!atomic_size_supported_p (n))
1cd6e20d 11356 {
11357 *new_return = add_atomic_size_parameter (n, loc, function, params);
11358 return true;
11359 }
11360
11361 /* Otherwise there is a lockfree match, transform the call from:
11362 void fn(T* mem, T* desired, T* return, model)
11363 into
11364 *return = (T) (fn (In* mem, (In) *desired, model)) */
11365
f1f41a6c 11366 p0 = (*params)[0];
11367 p1 = (*params)[1];
11368 p2 = (*params)[2];
11369 p3 = (*params)[3];
1cd6e20d 11370
11371 /* Create pointer to appropriate size. */
11372 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11373 I_type_ptr = build_pointer_type (I_type);
11374
11375 /* Convert object pointer to required type. */
11376 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11377 (*params)[0] = p0;
1cd6e20d 11378 /* Convert new value to required type, and dereference it. */
11379 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11380 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 11381 (*params)[1] = p1;
1cd6e20d 11382
11383 /* Move memory model to the 3rd position, and end param list. */
f1f41a6c 11384 (*params)[2] = p3;
11385 params->truncate (3);
1cd6e20d 11386
11387 /* Convert return pointer and dereference it for later assignment. */
11388 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11389
11390 return false;
b6a5fc45 11391}
11392
1cd6e20d 11393
11394/* This will process an __atomic_compare_exchange function call, determine
11395 whether it needs to be mapped to the _N variation, or turned into a lib call.
11396 LOC is the location of the builtin call.
11397 FUNCTION is the DECL that has been invoked;
11398 PARAMS is the argument list for the call. The return value is non-null
11399 TRUE is returned if it is translated into the proper format for a call to the
11400 external library, and NEW_RETURN is set the tree for that function.
11401 FALSE is returned if processing for the _N variation is required. */
11402
11403static bool
11404resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
f1f41a6c 11405 vec<tree, va_gc> *params,
1cd6e20d 11406 tree *new_return)
11407{
11408 tree p0, p1, p2;
11409 tree I_type, I_type_ptr;
11410 int n = get_atomic_generic_size (loc, function, params);
11411
1d581089 11412 /* Size of 0 is an error condition. */
11413 if (n == 0)
11414 {
11415 *new_return = error_mark_node;
11416 return true;
11417 }
11418
1cd6e20d 11419 /* If not a lock-free size, change to the library generic format. */
a056826c 11420 if (!atomic_size_supported_p (n))
1cd6e20d 11421 {
11422 /* The library generic format does not have the weak parameter, so
11423 remove it from the param list. Since a parameter has been removed,
11424 we can be sure that there is room for the SIZE_T parameter, meaning
11425 there will not be a recursive rebuilding of the parameter list, so
11426 there is no danger this will be done twice. */
11427 if (n > 0)
11428 {
f1f41a6c 11429 (*params)[3] = (*params)[4];
11430 (*params)[4] = (*params)[5];
11431 params->truncate (5);
1cd6e20d 11432 }
11433 *new_return = add_atomic_size_parameter (n, loc, function, params);
11434 return true;
11435 }
11436
11437 /* Otherwise, there is a match, so the call needs to be transformed from:
11438 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11439 into
11440 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
ab2c1de8 11441
f1f41a6c 11442 p0 = (*params)[0];
11443 p1 = (*params)[1];
11444 p2 = (*params)[2];
1cd6e20d 11445
11446 /* Create pointer to appropriate size. */
11447 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11448 I_type_ptr = build_pointer_type (I_type);
11449
11450 /* Convert object pointer to required type. */
11451 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11452 (*params)[0] = p0;
1cd6e20d 11453
11454 /* Convert expected pointer to required type. */
11455 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
f1f41a6c 11456 (*params)[1] = p1;
1cd6e20d 11457
11458 /* Convert desired value to required type, and dereference it. */
11459 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11460 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
f1f41a6c 11461 (*params)[2] = p2;
1cd6e20d 11462
11463 /* The rest of the parameters are fine. NULL means no special return value
11464 processing.*/
11465 *new_return = NULL;
11466 return false;
11467}
11468
11469
11470/* This will process an __atomic_load function call, determine whether it
11471 needs to be mapped to the _N variation, or turned into a library call.
11472 LOC is the location of the builtin call.
11473 FUNCTION is the DECL that has been invoked;
11474 PARAMS is the argument list for the call. The return value is non-null
11475 TRUE is returned if it is translated into the proper format for a call to the
11476 external library, and NEW_RETURN is set the tree for that function.
11477 FALSE is returned if processing for the _N variation is required, and
47ae02b7 11478 NEW_RETURN is set to the return value the result is copied into. */
1cd6e20d 11479
11480static bool
11481resolve_overloaded_atomic_load (location_t loc, tree function,
f1f41a6c 11482 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11483{
11484 tree p0, p1, p2;
11485 tree I_type, I_type_ptr;
11486 int n = get_atomic_generic_size (loc, function, params);
11487
1d581089 11488 /* Size of 0 is an error condition. */
11489 if (n == 0)
11490 {
11491 *new_return = error_mark_node;
11492 return true;
11493 }
11494
1cd6e20d 11495 /* If not a lock-free size, change to the library generic format. */
a056826c 11496 if (!atomic_size_supported_p (n))
1cd6e20d 11497 {
11498 *new_return = add_atomic_size_parameter (n, loc, function, params);
11499 return true;
11500 }
11501
11502 /* Otherwise, there is a match, so the call needs to be transformed from:
11503 void fn(T* mem, T* return, model)
11504 into
11505 *return = (T) (fn ((In *) mem, model)) */
11506
f1f41a6c 11507 p0 = (*params)[0];
11508 p1 = (*params)[1];
11509 p2 = (*params)[2];
1cd6e20d 11510
11511 /* Create pointer to appropriate size. */
11512 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11513 I_type_ptr = build_pointer_type (I_type);
11514
11515 /* Convert object pointer to required type. */
11516 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11517 (*params)[0] = p0;
1cd6e20d 11518
11519 /* Move memory model to the 2nd position, and end param list. */
f1f41a6c 11520 (*params)[1] = p2;
11521 params->truncate (2);
1cd6e20d 11522
11523 /* Convert return pointer and dereference it for later assignment. */
11524 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11525
11526 return false;
11527}
11528
11529
11530/* This will process an __atomic_store function call, determine whether it
11531 needs to be mapped to the _N variation, or turned into a library call.
11532 LOC is the location of the builtin call.
11533 FUNCTION is the DECL that has been invoked;
11534 PARAMS is the argument list for the call. The return value is non-null
11535 TRUE is returned if it is translated into the proper format for a call to the
11536 external library, and NEW_RETURN is set the tree for that function.
11537 FALSE is returned if processing for the _N variation is required, and
47ae02b7 11538 NEW_RETURN is set to the return value the result is copied into. */
1cd6e20d 11539
11540static bool
11541resolve_overloaded_atomic_store (location_t loc, tree function,
f1f41a6c 11542 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11543{
11544 tree p0, p1;
11545 tree I_type, I_type_ptr;
11546 int n = get_atomic_generic_size (loc, function, params);
11547
1d581089 11548 /* Size of 0 is an error condition. */
11549 if (n == 0)
11550 {
11551 *new_return = error_mark_node;
11552 return true;
11553 }
11554
1cd6e20d 11555 /* If not a lock-free size, change to the library generic format. */
a056826c 11556 if (!atomic_size_supported_p (n))
1cd6e20d 11557 {
11558 *new_return = add_atomic_size_parameter (n, loc, function, params);
11559 return true;
11560 }
11561
11562 /* Otherwise, there is a match, so the call needs to be transformed from:
11563 void fn(T* mem, T* value, model)
11564 into
11565 fn ((In *) mem, (In) *value, model) */
11566
f1f41a6c 11567 p0 = (*params)[0];
11568 p1 = (*params)[1];
1cd6e20d 11569
11570 /* Create pointer to appropriate size. */
11571 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11572 I_type_ptr = build_pointer_type (I_type);
11573
11574 /* Convert object pointer to required type. */
11575 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11576 (*params)[0] = p0;
1cd6e20d 11577
11578 /* Convert new value to required type, and dereference it. */
11579 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11580 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 11581 (*params)[1] = p1;
1cd6e20d 11582
11583 /* The memory model is in the right spot already. Return is void. */
11584 *new_return = NULL_TREE;
11585
11586 return false;
11587}
11588
11589
b6a5fc45 11590/* Some builtin functions are placeholders for other expressions. This
11591 function should be called immediately after parsing the call expression
11592 before surrounding code has committed to the type of the expression.
11593
e60a6f7b 11594 LOC is the location of the builtin call.
11595
b6a5fc45 11596 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11597 PARAMS is the argument list for the call. The return value is non-null
11598 when expansion is complete, and null if normal processing should
11599 continue. */
11600
11601tree
f1f41a6c 11602resolve_overloaded_builtin (location_t loc, tree function,
11603 vec<tree, va_gc> *params)
b6a5fc45 11604{
11605 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
1cd6e20d 11606 bool orig_format = true;
11607 tree new_return = NULL_TREE;
11608
65441f6f 11609 switch (DECL_BUILT_IN_CLASS (function))
11610 {
11611 case BUILT_IN_NORMAL:
11612 break;
11613 case BUILT_IN_MD:
11614 if (targetm.resolve_overloaded_builtin)
e60a6f7b 11615 return targetm.resolve_overloaded_builtin (loc, function, params);
65441f6f 11616 else
a0c938f0 11617 return NULL_TREE;
65441f6f 11618 default:
11619 return NULL_TREE;
11620 }
a0c938f0 11621
65441f6f 11622 /* Handle BUILT_IN_NORMAL here. */
b6a5fc45 11623 switch (orig_code)
11624 {
1cd6e20d 11625 case BUILT_IN_ATOMIC_EXCHANGE:
11626 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11627 case BUILT_IN_ATOMIC_LOAD:
11628 case BUILT_IN_ATOMIC_STORE:
11629 {
11630 /* Handle these 4 together so that they can fall through to the next
11631 case if the call is transformed to an _N variant. */
11632 switch (orig_code)
11633 {
11634 case BUILT_IN_ATOMIC_EXCHANGE:
11635 {
11636 if (resolve_overloaded_atomic_exchange (loc, function, params,
11637 &new_return))
11638 return new_return;
11639 /* Change to the _N variant. */
11640 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11641 break;
11642 }
11643
11644 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11645 {
11646 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11647 params,
11648 &new_return))
11649 return new_return;
11650 /* Change to the _N variant. */
11651 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11652 break;
11653 }
11654 case BUILT_IN_ATOMIC_LOAD:
11655 {
11656 if (resolve_overloaded_atomic_load (loc, function, params,
11657 &new_return))
11658 return new_return;
11659 /* Change to the _N variant. */
11660 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11661 break;
11662 }
11663 case BUILT_IN_ATOMIC_STORE:
11664 {
11665 if (resolve_overloaded_atomic_store (loc, function, params,
11666 &new_return))
11667 return new_return;
11668 /* Change to the _N variant. */
11669 orig_code = BUILT_IN_ATOMIC_STORE_N;
11670 break;
11671 }
11672 default:
11673 gcc_unreachable ();
11674 }
11675 /* Fallthrough to the normal processing. */
11676 }
11677 case BUILT_IN_ATOMIC_EXCHANGE_N:
11678 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11679 case BUILT_IN_ATOMIC_LOAD_N:
11680 case BUILT_IN_ATOMIC_STORE_N:
11681 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11682 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11683 case BUILT_IN_ATOMIC_AND_FETCH_N:
11684 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11685 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11686 case BUILT_IN_ATOMIC_OR_FETCH_N:
11687 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11688 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11689 case BUILT_IN_ATOMIC_FETCH_AND_N:
11690 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11691 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11692 case BUILT_IN_ATOMIC_FETCH_OR_N:
11693 {
11694 orig_format = false;
11695 /* Fallthru for parameter processing. */
11696 }
2797f13a 11697 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11698 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11699 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11700 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11701 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11702 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11703 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11704 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11705 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11706 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11707 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11708 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11709 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11710 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11711 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11712 case BUILT_IN_SYNC_LOCK_RELEASE_N:
b6a5fc45 11713 {
11714 int n = sync_resolve_size (function, params);
b9c74b4d 11715 tree new_function, first_param, result;
b9a16870 11716 enum built_in_function fncode;
b6a5fc45 11717
11718 if (n == 0)
11719 return error_mark_node;
11720
b9a16870 11721 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11722 new_function = builtin_decl_explicit (fncode);
1cd6e20d 11723 if (!sync_resolve_params (loc, function, new_function, params,
11724 orig_format))
b6a5fc45 11725 return error_mark_node;
11726
f1f41a6c 11727 first_param = (*params)[0];
ec761d5a 11728 result = build_function_call_vec (loc, vNULL, new_function, params,
11729 NULL);
1cd6e20d 11730 if (result == error_mark_node)
11731 return result;
2797f13a 11732 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
1cd6e20d 11733 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11734 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11735 result = sync_resolve_return (first_param, result, orig_format);
b6a5fc45 11736
1cd6e20d 11737 /* If new_return is set, assign function to that expr and cast the
11738 result to void since the generic interface returned void. */
11739 if (new_return)
11740 {
11741 /* Cast function result from I{1,2,4,8,16} to the required type. */
11742 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11743 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11744 result);
11745 TREE_SIDE_EFFECTS (result) = 1;
11746 protected_set_expr_location (result, loc);
11747 result = convert (void_type_node, result);
11748 }
b6a5fc45 11749 return result;
11750 }
11751
11752 default:
65441f6f 11753 return NULL_TREE;
b6a5fc45 11754 }
11755}
11756
73437615 11757/* vector_types_compatible_elements_p is used in type checks of vectors
11758 values used as operands of binary operators. Where it returns true, and
11759 the other checks of the caller succeed (being vector types in he first
11760 place, and matching number of elements), we can just treat the types
11761 as essentially the same.
11762 Contrast with vector_targets_convertible_p, which is used for vector
11763 pointer types, and vector_types_convertible_p, which will allow
11764 language-specific matches under the control of flag_lax_vector_conversions,
11765 and might still require a conversion. */
11766/* True if vector types T1 and T2 can be inputs to the same binary
11767 operator without conversion.
11768 We don't check the overall vector size here because some of our callers
11769 want to give different error messages when the vectors are compatible
11770 except for the element count. */
11771
491255f5 11772bool
73437615 11773vector_types_compatible_elements_p (tree t1, tree t2)
491255f5 11774{
73437615 11775 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11776 t1 = TREE_TYPE (t1);
11777 t2 = TREE_TYPE (t2);
11778
491255f5 11779 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11780
9421ebb9 11781 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11782 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11783 || c2 == FIXED_POINT_TYPE));
491255f5 11784
73437615 11785 t1 = c_common_signed_type (t1);
11786 t2 = c_common_signed_type (t2);
491255f5 11787 /* Equality works here because c_common_signed_type uses
11788 TYPE_MAIN_VARIANT. */
73437615 11789 if (t1 == t2)
11790 return true;
11791 if (opaque && c1 == c2
11792 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11793 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11794 return true;
11795 return false;
491255f5 11796}
11797
be7350e7 11798/* Check for missing format attributes on function pointers. LTYPE is
11799 the new type or left-hand side type. RTYPE is the old type or
11800 right-hand side type. Returns TRUE if LTYPE is missing the desired
11801 attribute. */
11802
11803bool
11804check_missing_format_attribute (tree ltype, tree rtype)
11805{
11806 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11807 tree ra;
11808
11809 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11810 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11811 break;
11812 if (ra)
11813 {
11814 tree la;
11815 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11816 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11817 break;
11818 return !la;
11819 }
11820 else
11821 return false;
11822}
11823
2840aae4 11824/* Subscripting with type char is likely to lose on a machine where
11825 chars are signed. So warn on any machine, but optionally. Don't
11826 warn for unsigned char since that type is safe. Don't warn for
11827 signed char because anyone who uses that must have done so
11828 deliberately. Furthermore, we reduce the false positive load by
11829 warning only for non-constant value of type char. */
11830
11831void
92b63884 11832warn_array_subscript_with_type_char (location_t loc, tree index)
2840aae4 11833{
11834 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11835 && TREE_CODE (index) != INTEGER_CST)
92b63884 11836 warning_at (loc, OPT_Wchar_subscripts,
11837 "array subscript has type %<char%>");
2840aae4 11838}
11839
e534436e 11840/* Implement -Wparentheses for the unexpected C precedence rules, to
11841 cover cases like x + y << z which readers are likely to
11842 misinterpret. We have seen an expression in which CODE is a binary
82012ffe 11843 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11844 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11845 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11846 expression was not formed using a binary or unary operator, or it
11847 was enclosed in parentheses. */
e534436e 11848
11849void
b0e7825e 11850warn_about_parentheses (location_t loc, enum tree_code code,
269f7979 11851 enum tree_code code_left, tree arg_left,
82012ffe 11852 enum tree_code code_right, tree arg_right)
e534436e 11853{
11854 if (!warn_parentheses)
11855 return;
11856
82012ffe 11857 /* This macro tests that the expression ARG with original tree code
11858 CODE appears to be a boolean expression. or the result of folding a
11859 boolean expression. */
11860#define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11861 (truth_value_p (TREE_CODE (ARG)) \
11862 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11863 /* Folding may create 0 or 1 integers from other expressions. */ \
11864 || ((CODE) != INTEGER_CST \
11865 && (integer_onep (ARG) || integer_zerop (ARG))))
11866
48e1416a 11867 switch (code)
e534436e 11868 {
82012ffe 11869 case LSHIFT_EXPR:
b0e7825e 11870 if (code_left == PLUS_EXPR)
11871 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11872 "suggest parentheses around %<+%> inside %<<<%>");
11873 else if (code_right == PLUS_EXPR)
11874 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11875 "suggest parentheses around %<+%> inside %<<<%>");
11876 else if (code_left == MINUS_EXPR)
11877 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11878 "suggest parentheses around %<-%> inside %<<<%>");
11879 else if (code_right == MINUS_EXPR)
11880 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11881 "suggest parentheses around %<-%> inside %<<<%>");
82012ffe 11882 return;
e534436e 11883
82012ffe 11884 case RSHIFT_EXPR:
b0e7825e 11885 if (code_left == PLUS_EXPR)
11886 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11887 "suggest parentheses around %<+%> inside %<>>%>");
11888 else if (code_right == PLUS_EXPR)
11889 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11890 "suggest parentheses around %<+%> inside %<>>%>");
11891 else if (code_left == MINUS_EXPR)
11892 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11893 "suggest parentheses around %<-%> inside %<>>%>");
11894 else if (code_right == MINUS_EXPR)
11895 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11896 "suggest parentheses around %<-%> inside %<>>%>");
82012ffe 11897 return;
e534436e 11898
82012ffe 11899 case TRUTH_ORIF_EXPR:
b0e7825e 11900 if (code_left == TRUTH_ANDIF_EXPR)
11901 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11902 "suggest parentheses around %<&&%> within %<||%>");
11903 else if (code_right == TRUTH_ANDIF_EXPR)
11904 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11905 "suggest parentheses around %<&&%> within %<||%>");
82012ffe 11906 return;
11907
11908 case BIT_IOR_EXPR:
e534436e 11909 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
b0e7825e 11910 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11911 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11912 "suggest parentheses around arithmetic in operand of %<|%>");
11913 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11914 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11915 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11916 "suggest parentheses around arithmetic in operand of %<|%>");
e534436e 11917 /* Check cases like x|y==z */
b0e7825e 11918 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11919 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11920 "suggest parentheses around comparison in operand of %<|%>");
11921 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11922 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11923 "suggest parentheses around comparison in operand of %<|%>");
11924 /* Check cases like !x | y */
11925 else if (code_left == TRUTH_NOT_EXPR
11926 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 11927 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11928 "suggest parentheses around operand of "
11929 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
82012ffe 11930 return;
e534436e 11931
82012ffe 11932 case BIT_XOR_EXPR:
e534436e 11933 if (code_left == BIT_AND_EXPR
b0e7825e 11934 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11935 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11936 "suggest parentheses around arithmetic in operand of %<^%>");
11937 else if (code_right == BIT_AND_EXPR
11938 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11939 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11940 "suggest parentheses around arithmetic in operand of %<^%>");
e534436e 11941 /* Check cases like x^y==z */
b0e7825e 11942 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11943 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11944 "suggest parentheses around comparison in operand of %<^%>");
11945 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11946 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11947 "suggest parentheses around comparison in operand of %<^%>");
11948 return;
e534436e 11949
82012ffe 11950 case BIT_AND_EXPR:
b0e7825e 11951 if (code_left == PLUS_EXPR)
11952 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11953 "suggest parentheses around %<+%> in operand of %<&%>");
11954 else if (code_right == PLUS_EXPR)
11955 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11956 "suggest parentheses around %<+%> in operand of %<&%>");
b0e7825e 11957 else if (code_left == MINUS_EXPR)
11958 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11959 "suggest parentheses around %<-%> in operand of %<&%>");
11960 else if (code_right == MINUS_EXPR)
11961 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11962 "suggest parentheses around %<-%> in operand of %<&%>");
e534436e 11963 /* Check cases like x&y==z */
b0e7825e 11964 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11965 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11966 "suggest parentheses around comparison in operand of %<&%>");
11967 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11968 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11969 "suggest parentheses around comparison in operand of %<&%>");
11970 /* Check cases like !x & y */
11971 else if (code_left == TRUTH_NOT_EXPR
11972 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 11973 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11974 "suggest parentheses around operand of "
11975 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
82012ffe 11976 return;
e534436e 11977
82012ffe 11978 case EQ_EXPR:
b0e7825e 11979 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11980 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11981 "suggest parentheses around comparison in operand of %<==%>");
11982 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11983 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11984 "suggest parentheses around comparison in operand of %<==%>");
11985 return;
11986 case NE_EXPR:
b0e7825e 11987 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11988 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11989 "suggest parentheses around comparison in operand of %<!=%>");
11990 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11991 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11992 "suggest parentheses around comparison in operand of %<!=%>");
11993 return;
11994
11995 default:
b0e7825e 11996 if (TREE_CODE_CLASS (code) == tcc_comparison)
11997 {
11998 if (TREE_CODE_CLASS (code_left) == tcc_comparison
269f7979 11999 && code_left != NE_EXPR && code_left != EQ_EXPR
12000 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
b0e7825e 12001 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
12002 "comparisons like %<X<=Y<=Z%> do not "
12003 "have their mathematical meaning");
12004 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
269f7979 12005 && code_right != NE_EXPR && code_right != EQ_EXPR
b0e7825e 12006 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
12007 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
12008 "comparisons like %<X<=Y<=Z%> do not "
12009 "have their mathematical meaning");
12010 }
82012ffe 12011 return;
6ce0c450 12012 }
82012ffe 12013#undef NOT_A_BOOLEAN_EXPR_P
e534436e 12014}
12015
92fccaaa 12016/* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
12017
12018void
12019warn_for_unused_label (tree label)
12020{
12021 if (!TREE_USED (label))
12022 {
12023 if (DECL_INITIAL (label))
12024 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
12025 else
12026 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
12027 }
12028}
2840aae4 12029
b6889cb0 12030/* Warn for division by zero according to the value of DIVISOR. LOC
12031 is the location of the division operator. */
f092582b 12032
12033void
b6889cb0 12034warn_for_div_by_zero (location_t loc, tree divisor)
f092582b 12035{
9421ebb9 12036 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
12037 about division by zero. Do not issue a warning if DIVISOR has a
f092582b 12038 floating-point type, since we consider 0.0/0.0 a valid way of
12039 generating a NaN. */
48d94ede 12040 if (c_inhibit_evaluation_warnings == 0
9421ebb9 12041 && (integer_zerop (divisor) || fixed_zerop (divisor)))
b6889cb0 12042 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
f092582b 12043}
12044
13869a99 12045/* Subroutine of build_binary_op. Give warnings for comparisons
12046 between signed and unsigned quantities that may fail. Do the
12047 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
12048 so that casts will be considered, but default promotions won't
8e70fb09 12049 be.
12050
12051 LOCATION is the location of the comparison operator.
13869a99 12052
12053 The arguments of this function map directly to local variables
12054 of build_binary_op. */
12055
48e1416a 12056void
8e70fb09 12057warn_for_sign_compare (location_t location,
48e1416a 12058 tree orig_op0, tree orig_op1,
12059 tree op0, tree op1,
13869a99 12060 tree result_type, enum tree_code resultcode)
12061{
12062 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
12063 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
12064 int unsignedp0, unsignedp1;
48e1416a 12065
13869a99 12066 /* In C++, check for comparison of different enum types. */
12067 if (c_dialect_cxx()
12068 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
12069 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
12070 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
895b662f 12071 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
13869a99 12072 {
8e70fb09 12073 warning_at (location,
12074 OPT_Wsign_compare, "comparison between types %qT and %qT",
12075 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
13869a99 12076 }
12077
12078 /* Do not warn if the comparison is being done in a signed type,
12079 since the signed type will only be chosen if it can represent
12080 all the values of the unsigned type. */
12081 if (!TYPE_UNSIGNED (result_type))
12082 /* OK */;
12083 /* Do not warn if both operands are unsigned. */
12084 else if (op0_signed == op1_signed)
12085 /* OK */;
12086 else
12087 {
895b662f 12088 tree sop, uop, base_type;
13869a99 12089 bool ovf;
895b662f 12090
13869a99 12091 if (op0_signed)
12092 sop = orig_op0, uop = orig_op1;
48e1416a 12093 else
13869a99 12094 sop = orig_op1, uop = orig_op0;
12095
48e1416a 12096 STRIP_TYPE_NOPS (sop);
13869a99 12097 STRIP_TYPE_NOPS (uop);
895b662f 12098 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
12099 ? TREE_TYPE (result_type) : result_type);
13869a99 12100
12101 /* Do not warn if the signed quantity is an unsuffixed integer
12102 literal (or some static constant expression involving such
12103 literals or a conditional expression involving such literals)
12104 and it is non-negative. */
12105 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
12106 /* OK */;
12107 /* Do not warn if the comparison is an equality operation, the
12108 unsigned quantity is an integral constant, and it would fit
12109 in the result if the result were signed. */
12110 else if (TREE_CODE (uop) == INTEGER_CST
12111 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
895b662f 12112 && int_fits_type_p (uop, c_common_signed_type (base_type)))
13869a99 12113 /* OK */;
12114 /* In C, do not warn if the unsigned quantity is an enumeration
12115 constant and its maximum value would fit in the result if the
12116 result were signed. */
12117 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
12118 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
12119 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
895b662f 12120 c_common_signed_type (base_type)))
13869a99 12121 /* OK */;
48e1416a 12122 else
8e70fb09 12123 warning_at (location,
48e1416a 12124 OPT_Wsign_compare,
8e70fb09 12125 "comparison between signed and unsigned integer expressions");
13869a99 12126 }
48e1416a 12127
13869a99 12128 /* Warn if two unsigned values are being compared in a size larger
12129 than their original size, and one (and only one) is the result of
12130 a `~' operator. This comparison will always fail.
48e1416a 12131
13869a99 12132 Also warn if one operand is a constant, and the constant does not
12133 have all bits set that are set in the ~ operand when it is
12134 extended. */
12135
7f506bca 12136 op0 = c_common_get_narrower (op0, &unsignedp0);
12137 op1 = c_common_get_narrower (op1, &unsignedp1);
48e1416a 12138
13869a99 12139 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
12140 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
12141 {
12142 if (TREE_CODE (op0) == BIT_NOT_EXPR)
7f506bca 12143 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
13869a99 12144 if (TREE_CODE (op1) == BIT_NOT_EXPR)
7f506bca 12145 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
13869a99 12146
e913b5cd 12147 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
13869a99 12148 {
12149 tree primop;
12150 HOST_WIDE_INT constant, mask;
12151 int unsignedp;
12152 unsigned int bits;
48e1416a 12153
e913b5cd 12154 if (tree_fits_shwi_p (op0))
13869a99 12155 {
12156 primop = op1;
12157 unsignedp = unsignedp1;
e913b5cd 12158 constant = tree_to_shwi (op0);
13869a99 12159 }
12160 else
12161 {
12162 primop = op0;
12163 unsignedp = unsignedp0;
e913b5cd 12164 constant = tree_to_shwi (op1);
13869a99 12165 }
48e1416a 12166
13869a99 12167 bits = TYPE_PRECISION (TREE_TYPE (primop));
12168 if (bits < TYPE_PRECISION (result_type)
12169 && bits < HOST_BITS_PER_LONG && unsignedp)
12170 {
85f5e2ee 12171 mask = (~ (unsigned HOST_WIDE_INT) 0) << bits;
13869a99 12172 if ((mask & constant) != mask)
12173 {
12174 if (constant == 0)
76fdceeb 12175 warning_at (location, OPT_Wsign_compare,
12176 "promoted ~unsigned is always non-zero");
13869a99 12177 else
48e1416a 12178 warning_at (location, OPT_Wsign_compare,
8e70fb09 12179 "comparison of promoted ~unsigned with constant");
13869a99 12180 }
12181 }
12182 }
12183 else if (unsignedp0 && unsignedp1
12184 && (TYPE_PRECISION (TREE_TYPE (op0))
12185 < TYPE_PRECISION (result_type))
12186 && (TYPE_PRECISION (TREE_TYPE (op1))
12187 < TYPE_PRECISION (result_type)))
8e70fb09 12188 warning_at (location, OPT_Wsign_compare,
13869a99 12189 "comparison of promoted ~unsigned with unsigned");
12190 }
12191}
12192
5ba33bf4 12193/* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
12194 type via c_common_type. If -Wdouble-promotion is in use, and the
12195 conditions for warning have been met, issue a warning. GMSGID is
12196 the warning message. It must have two %T specifiers for the type
12197 that was converted (generally "float") and the type to which it was
12198 converted (generally "double), respectively. LOC is the location
12199 to which the awrning should refer. */
12200
12201void
12202do_warn_double_promotion (tree result_type, tree type1, tree type2,
12203 const char *gmsgid, location_t loc)
12204{
12205 tree source_type;
12206
12207 if (!warn_double_promotion)
12208 return;
12209 /* If the conversion will not occur at run-time, there is no need to
12210 warn about it. */
12211 if (c_inhibit_evaluation_warnings)
12212 return;
12213 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
12214 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
12215 return;
12216 if (TYPE_MAIN_VARIANT (type1) == float_type_node
12217 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
12218 source_type = type1;
12219 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
12220 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
12221 source_type = type2;
12222 else
12223 return;
12224 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
12225}
12226
0949f227 12227/* Possibly warn about unused parameters. */
12228
12229void
12230do_warn_unused_parameter (tree fn)
12231{
12232 tree decl;
12233
12234 for (decl = DECL_ARGUMENTS (fn);
12235 decl; decl = DECL_CHAIN (decl))
12236 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
12237 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
12238 && !TREE_NO_WARNING (decl))
12239 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
12240 "unused parameter %qD", decl);
12241}
12242
12243
41771881 12244/* Setup a TYPE_DECL node as a typedef representation.
12245
12246 X is a TYPE_DECL for a typedef statement. Create a brand new
12247 ..._TYPE node (which will be just a variant of the existing
12248 ..._TYPE node with identical properties) and then install X
12249 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
12250
12251 The whole point here is to end up with a situation where each
12252 and every ..._TYPE node the compiler creates will be uniquely
12253 associated with AT MOST one node representing a typedef name.
12254 This way, even though the compiler substitutes corresponding
12255 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
12256 early on, later parts of the compiler can always do the reverse
12257 translation and get back the corresponding typedef name. For
12258 example, given:
ab2c1de8 12259
41771881 12260 typedef struct S MY_TYPE;
12261 MY_TYPE object;
12262
12263 Later parts of the compiler might only know that `object' was of
12264 type `struct S' if it were not for code just below. With this
12265 code however, later parts of the compiler see something like:
12266
12267 struct S' == struct S
12268 typedef struct S' MY_TYPE;
12269 struct S' object;
12270
12271 And they can then deduce (from the node for type struct S') that
12272 the original object declaration was:
12273
12274 MY_TYPE object;
12275
12276 Being able to do this is important for proper support of protoize,
12277 and also for generating precise symbolic debugging information
12278 which takes full account of the programmer's (typedef) vocabulary.
12279
12280 Obviously, we don't want to generate a duplicate ..._TYPE node if
12281 the TYPE_DECL node that we are now processing really represents a
12282 standard built-in type. */
12283
12284void
12285set_underlying_type (tree x)
12286{
12287 if (x == error_mark_node)
12288 return;
12289 if (DECL_IS_BUILTIN (x))
12290 {
12291 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12292 TYPE_NAME (TREE_TYPE (x)) = x;
12293 }
12294 else if (TREE_TYPE (x) != error_mark_node
12295 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12296 {
12297 tree tt = TREE_TYPE (x);
12298 DECL_ORIGINAL_TYPE (x) = tt;
12299 tt = build_variant_type_copy (tt);
12300 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12301 TYPE_NAME (tt) = x;
12302 TREE_USED (tt) = TREE_USED (x);
12303 TREE_TYPE (x) = tt;
12304 }
12305}
12306
1a4c44c5 12307/* Record the types used by the current global variable declaration
12308 being parsed, so that we can decide later to emit their debug info.
12309 Those types are in types_used_by_cur_var_decl, and we are going to
12310 store them in the types_used_by_vars_hash hash table.
12311 DECL is the declaration of the global variable that has been parsed. */
12312
12313void
12314record_types_used_by_current_var_decl (tree decl)
12315{
12316 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12317
f1f41a6c 12318 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
1a4c44c5 12319 {
f1f41a6c 12320 tree type = types_used_by_cur_var_decl->pop ();
aef48c9a 12321 types_used_by_var_decl_insert (type, decl);
1a4c44c5 12322 }
12323}
12324
a4e3ffad 12325/* If DECL is a typedef that is declared in the current function,
12326 record it for the purpose of -Wunused-local-typedefs. */
12327
12328void
12329record_locally_defined_typedef (tree decl)
12330{
12331 struct c_language_function *l;
12332
12333 if (!warn_unused_local_typedefs
12334 || cfun == NULL
12335 /* if this is not a locally defined typedef then we are not
12336 interested. */
12337 || !is_typedef_decl (decl)
12338 || !decl_function_context (decl))
12339 return;
12340
12341 l = (struct c_language_function *) cfun->language;
f1f41a6c 12342 vec_safe_push (l->local_typedefs, decl);
a4e3ffad 12343}
12344
12345/* If T is a TYPE_DECL declared locally, mark it as used. */
12346
12347void
12348maybe_record_typedef_use (tree t)
12349{
12350 if (!is_typedef_decl (t))
12351 return;
12352
12353 TREE_USED (t) = true;
12354}
12355
12356/* Warn if there are some unused locally defined typedefs in the
12357 current function. */
12358
12359void
12360maybe_warn_unused_local_typedefs (void)
12361{
12362 int i;
12363 tree decl;
12364 /* The number of times we have emitted -Wunused-local-typedefs
12365 warnings. If this is different from errorcount, that means some
12366 unrelated errors have been issued. In which case, we'll avoid
12367 emitting "unused-local-typedefs" warnings. */
12368 static int unused_local_typedefs_warn_count;
12369 struct c_language_function *l;
12370
12371 if (cfun == NULL)
12372 return;
12373
12374 if ((l = (struct c_language_function *) cfun->language) == NULL)
12375 return;
12376
12377 if (warn_unused_local_typedefs
12378 && errorcount == unused_local_typedefs_warn_count)
12379 {
f1f41a6c 12380 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
a4e3ffad 12381 if (!TREE_USED (decl))
12382 warning_at (DECL_SOURCE_LOCATION (decl),
12383 OPT_Wunused_local_typedefs,
12384 "typedef %qD locally defined but not used", decl);
12385 unused_local_typedefs_warn_count = errorcount;
12386 }
12387
f1f41a6c 12388 vec_free (l->local_typedefs);
a4e3ffad 12389}
12390
78bf4156 12391/* Warn about boolean expression compared with an integer value different
12392 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12393 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12394 are the operands of the comparison. The caller must ensure that
12395 either operand is a boolean expression. */
12396
12397void
12398maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12399 tree op1)
12400{
12401 if (TREE_CODE_CLASS (code) != tcc_comparison)
12402 return;
12403
12404 tree cst = (TREE_CODE (op0) == INTEGER_CST)
12405 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
12406 if (!cst)
12407 return;
12408
12409 if (!integer_zerop (cst) && !integer_onep (cst))
12410 {
a720ab1c 12411 int sign = (TREE_CODE (op0) == INTEGER_CST
12412 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
78bf4156 12413 if (code == EQ_EXPR
12414 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12415 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12416 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12417 "with boolean expression is always false", cst);
12418 else
12419 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12420 "with boolean expression is always true", cst);
12421 }
a720ab1c 12422 else if (integer_zerop (cst) || integer_onep (cst))
12423 {
14744a16 12424 /* If the non-constant operand isn't of a boolean type, we
12425 don't want to warn here. */
12426 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12427 /* Handle booleans promoted to integers. */
12428 if (CONVERT_EXPR_P (noncst)
12429 && TREE_TYPE (noncst) == integer_type_node
12430 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
12431 /* Warn. */;
12432 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12433 && !truth_value_p (TREE_CODE (noncst)))
12434 return;
a720ab1c 12435 /* Do some magic to get the right diagnostics. */
12436 bool flag = TREE_CODE (op0) == INTEGER_CST;
12437 flag = integer_zerop (cst) ? flag : !flag;
12438 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12439 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12440 "with boolean expression is always true", cst);
12441 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12442 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12443 "with boolean expression is always false", cst);
12444 }
78bf4156 12445}
12446
8d669e79 12447/* Warn if signed left shift overflows. We don't warn
12448 about left-shifting 1 into the sign bit in C++14; cf.
12449 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
12450 LOC is a location of the shift; OP0 and OP1 are the operands.
12451 Return true if an overflow is detected, false otherwise. */
12452
12453bool
12454maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
12455{
12456 if (TREE_CODE (op0) != INTEGER_CST
12457 || TREE_CODE (op1) != INTEGER_CST)
12458 return false;
12459
12460 tree type0 = TREE_TYPE (op0);
12461 unsigned int prec0 = TYPE_PRECISION (type0);
12462
12463 /* Left-hand operand must be signed. */
12464 if (TYPE_UNSIGNED (type0))
12465 return false;
12466
f4809955 12467 unsigned int min_prec = (wi::min_precision (op0, SIGNED)
12468 + TREE_INT_CST_LOW (op1));
8d669e79 12469 /* Handle the left-shifting 1 into the sign bit case. */
f4809955 12470 if (min_prec == prec0 + 1)
8d669e79 12471 {
12472 /* Never warn for C++14 onwards. */
12473 if (cxx_dialect >= cxx14)
12474 return false;
12475 /* Otherwise only if -Wshift-overflow=2. But return
12476 true to signal an overflow for the sake of integer
12477 constant expressions. */
12478 if (warn_shift_overflow < 2)
12479 return true;
12480 }
12481
8d669e79 12482 bool overflowed = min_prec > prec0;
12483 if (overflowed && c_inhibit_evaluation_warnings == 0)
12484 warning_at (loc, OPT_Wshift_overflow_,
12485 "result of %qE requires %u bits to represent, "
12486 "but %qT only has %u bits",
12487 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
12488 min_prec, type0, prec0);
12489
12490 return overflowed;
12491}
12492
f352a3fb 12493/* The C and C++ parsers both use vectors to hold function arguments.
12494 For efficiency, we keep a cache of unused vectors. This is the
12495 cache. */
12496
f1f41a6c 12497typedef vec<tree, va_gc> *tree_gc_vec;
12498static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
f352a3fb 12499
12500/* Return a new vector from the cache. If the cache is empty,
12501 allocate a new vector. These vectors are GC'ed, so it is OK if the
12502 pointer is not released.. */
12503
f1f41a6c 12504vec<tree, va_gc> *
f352a3fb 12505make_tree_vector (void)
12506{
f1f41a6c 12507 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12508 return tree_vector_cache->pop ();
f352a3fb 12509 else
12510 {
f1f41a6c 12511 /* Passing 0 to vec::alloc returns NULL, and our callers require
f352a3fb 12512 that we always return a non-NULL value. The vector code uses
12513 4 when growing a NULL vector, so we do too. */
f1f41a6c 12514 vec<tree, va_gc> *v;
12515 vec_alloc (v, 4);
12516 return v;
f352a3fb 12517 }
12518}
12519
12520/* Release a vector of trees back to the cache. */
12521
12522void
f1f41a6c 12523release_tree_vector (vec<tree, va_gc> *vec)
f352a3fb 12524{
12525 if (vec != NULL)
12526 {
f1f41a6c 12527 vec->truncate (0);
12528 vec_safe_push (tree_vector_cache, vec);
f352a3fb 12529 }
12530}
12531
12532/* Get a new tree vector holding a single tree. */
12533
f1f41a6c 12534vec<tree, va_gc> *
f352a3fb 12535make_tree_vector_single (tree t)
12536{
f1f41a6c 12537 vec<tree, va_gc> *ret = make_tree_vector ();
12538 ret->quick_push (t);
f352a3fb 12539 return ret;
12540}
12541
c66c81be 12542/* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12543
f1f41a6c 12544vec<tree, va_gc> *
c66c81be 12545make_tree_vector_from_list (tree list)
12546{
f1f41a6c 12547 vec<tree, va_gc> *ret = make_tree_vector ();
c66c81be 12548 for (; list; list = TREE_CHAIN (list))
f1f41a6c 12549 vec_safe_push (ret, TREE_VALUE (list));
c66c81be 12550 return ret;
12551}
12552
f352a3fb 12553/* Get a new tree vector which is a copy of an existing one. */
12554
f1f41a6c 12555vec<tree, va_gc> *
12556make_tree_vector_copy (const vec<tree, va_gc> *orig)
f352a3fb 12557{
f1f41a6c 12558 vec<tree, va_gc> *ret;
f352a3fb 12559 unsigned int ix;
12560 tree t;
12561
12562 ret = make_tree_vector ();
f1f41a6c 12563 vec_safe_reserve (ret, vec_safe_length (orig));
12564 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12565 ret->quick_push (t);
f352a3fb 12566 return ret;
12567}
12568
a9ffdd35 12569/* Return true if KEYWORD starts a type specifier. */
12570
12571bool
12572keyword_begins_type_specifier (enum rid keyword)
12573{
12574 switch (keyword)
12575 {
4fba5eb9 12576 case RID_AUTO_TYPE:
a9ffdd35 12577 case RID_INT:
12578 case RID_CHAR:
12579 case RID_FLOAT:
12580 case RID_DOUBLE:
12581 case RID_VOID:
a9ffdd35 12582 case RID_UNSIGNED:
12583 case RID_LONG:
12584 case RID_SHORT:
12585 case RID_SIGNED:
12586 case RID_DFLOAT32:
12587 case RID_DFLOAT64:
12588 case RID_DFLOAT128:
12589 case RID_FRACT:
12590 case RID_ACCUM:
12591 case RID_BOOL:
12592 case RID_WCHAR:
12593 case RID_CHAR16:
12594 case RID_CHAR32:
12595 case RID_SAT:
12596 case RID_COMPLEX:
12597 case RID_TYPEOF:
12598 case RID_STRUCT:
12599 case RID_CLASS:
12600 case RID_UNION:
12601 case RID_ENUM:
12602 return true;
12603 default:
9f75f026 12604 if (keyword >= RID_FIRST_INT_N
12605 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12606 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12607 return true;
a9ffdd35 12608 return false;
12609 }
12610}
12611
12612/* Return true if KEYWORD names a type qualifier. */
12613
12614bool
12615keyword_is_type_qualifier (enum rid keyword)
12616{
12617 switch (keyword)
12618 {
12619 case RID_CONST:
12620 case RID_VOLATILE:
12621 case RID_RESTRICT:
b560fabd 12622 case RID_ATOMIC:
a9ffdd35 12623 return true;
12624 default:
12625 return false;
12626 }
12627}
12628
12629/* Return true if KEYWORD names a storage class specifier.
12630
12631 RID_TYPEDEF is not included in this list despite `typedef' being
12632 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12633 such for syntactic convenience only. */
12634
12635bool
12636keyword_is_storage_class_specifier (enum rid keyword)
12637{
12638 switch (keyword)
12639 {
12640 case RID_STATIC:
12641 case RID_EXTERN:
12642 case RID_REGISTER:
12643 case RID_AUTO:
12644 case RID_MUTABLE:
12645 case RID_THREAD:
12646 return true;
12647 default:
12648 return false;
12649 }
12650}
12651
fad3f658 12652/* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12653
12654static bool
12655keyword_is_function_specifier (enum rid keyword)
12656{
12657 switch (keyword)
12658 {
12659 case RID_INLINE:
985c6e3a 12660 case RID_NORETURN:
fad3f658 12661 case RID_VIRTUAL:
12662 case RID_EXPLICIT:
12663 return true;
12664 default:
12665 return false;
12666 }
12667}
12668
12669/* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12670 declaration-specifier (C99 6.7). */
12671
12672bool
12673keyword_is_decl_specifier (enum rid keyword)
12674{
12675 if (keyword_is_storage_class_specifier (keyword)
12676 || keyword_is_type_qualifier (keyword)
12677 || keyword_is_function_specifier (keyword))
12678 return true;
12679
12680 switch (keyword)
12681 {
12682 case RID_TYPEDEF:
12683 case RID_FRIEND:
12684 case RID_CONSTEXPR:
12685 return true;
12686 default:
12687 return false;
12688 }
12689}
12690
9b88d08d 12691/* Initialize language-specific-bits of tree_contains_struct. */
12692
12693void
12694c_common_init_ts (void)
12695{
12696 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12697 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
3c6d4197 12698 MARK_TS_TYPED (ARRAY_NOTATION_REF);
9b88d08d 12699}
12700
244db24d 12701/* Build a user-defined numeric literal out of an integer constant type VALUE
12702 with identifier SUFFIX. */
12703
12704tree
324ca377 12705build_userdef_literal (tree suffix_id, tree value,
12706 enum overflow_type overflow, tree num_string)
244db24d 12707{
12708 tree literal = make_node (USERDEF_LITERAL);
12709 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12710 USERDEF_LITERAL_VALUE (literal) = value;
324ca377 12711 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
244db24d 12712 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12713 return literal;
12714}
12715
7059d45d 12716/* For vector[index], convert the vector to a
c61ef207 12717 pointer of the underlying type. Return true if the resulting
12718 ARRAY_REF should not be an lvalue. */
12719
12720bool
7059d45d 12721convert_vector_to_pointer_for_subscript (location_t loc,
c61ef207 12722 tree *vecp, tree index)
7059d45d 12723{
c61ef207 12724 bool ret = false;
6290f0db 12725 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
7059d45d 12726 {
12727 tree type = TREE_TYPE (*vecp);
12728 tree type1;
12729
c61ef207 12730 ret = !lvalue_p (*vecp);
7059d45d 12731 if (TREE_CODE (index) == INTEGER_CST)
e913b5cd 12732 if (!tree_fits_uhwi_p (index)
aa59f000 12733 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7059d45d 12734 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12735
c61ef207 12736 if (ret)
12737 {
f9e245b2 12738 tree tmp = create_tmp_var_raw (type);
c61ef207 12739 DECL_SOURCE_LOCATION (tmp) = loc;
12740 *vecp = c_save_expr (*vecp);
12741 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12742 {
12743 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12744 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12745 *vecp
12746 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12747 *vecp, NULL_TREE, NULL_TREE),
12748 non_const);
12749 }
12750 else
12751 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12752 NULL_TREE, NULL_TREE);
12753 SET_EXPR_LOCATION (*vecp, loc);
12754 c_common_mark_addressable_vec (tmp);
12755 }
12756 else
12757 c_common_mark_addressable_vec (*vecp);
7059d45d 12758 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7059d45d 12759 type1 = build_pointer_type (TREE_TYPE (*vecp));
b2ca6510 12760 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12761 if (!ref_all
12762 && !DECL_P (*vecp))
12763 {
12764 /* If the original vector isn't declared may_alias and it
12765 isn't a bare vector look if the subscripting would
12766 alias the vector we subscript, and if not, force ref-all. */
12767 alias_set_type vecset = get_alias_set (*vecp);
12768 alias_set_type sset = get_alias_set (type);
12769 if (!alias_sets_must_conflict_p (sset, vecset)
12770 && !alias_set_subset_of (sset, vecset))
12771 ref_all = true;
12772 }
12773 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
7059d45d 12774 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12775 *vecp = convert (type, *vecp);
12776 }
c61ef207 12777 return ret;
7059d45d 12778}
12779
41ed701a 12780/* Determine which of the operands, if any, is a scalar that needs to be
12781 converted to a vector, for the range of operations. */
12782enum stv_conv
12783scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12784 bool complain)
12785{
12786 tree type0 = TREE_TYPE (op0);
12787 tree type1 = TREE_TYPE (op1);
12788 bool integer_only_op = false;
12789 enum stv_conv ret = stv_firstarg;
12790
6290f0db 12791 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
41ed701a 12792 switch (code)
12793 {
12794 /* Most GENERIC binary expressions require homogeneous arguments.
12795 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12796 argument that is a vector and a second one that is a scalar, so
12797 we never return stv_secondarg for them. */
12798 case RSHIFT_EXPR:
12799 case LSHIFT_EXPR:
12800 if (TREE_CODE (type0) == INTEGER_TYPE
12801 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12802 {
22a75734 12803 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12804 {
12805 if (complain)
12806 error_at (loc, "conversion of scalar %qT to vector %qT "
12807 "involves truncation", type0, type1);
12808 return stv_error;
12809 }
12810 else
12811 return stv_firstarg;
12812 }
12813 break;
12814
12815 case BIT_IOR_EXPR:
12816 case BIT_XOR_EXPR:
12817 case BIT_AND_EXPR:
12818 integer_only_op = true;
12819 /* ... fall through ... */
12820
7b463b19 12821 case VEC_COND_EXPR:
12822
41ed701a 12823 case PLUS_EXPR:
12824 case MINUS_EXPR:
12825 case MULT_EXPR:
12826 case TRUNC_DIV_EXPR:
12827 case CEIL_DIV_EXPR:
12828 case FLOOR_DIV_EXPR:
12829 case ROUND_DIV_EXPR:
12830 case EXACT_DIV_EXPR:
12831 case TRUNC_MOD_EXPR:
12832 case FLOOR_MOD_EXPR:
12833 case RDIV_EXPR:
12834 case EQ_EXPR:
12835 case NE_EXPR:
12836 case LE_EXPR:
12837 case GE_EXPR:
12838 case LT_EXPR:
12839 case GT_EXPR:
12840 /* What about UNLT_EXPR? */
6290f0db 12841 if (VECTOR_TYPE_P (type0))
41ed701a 12842 {
41ed701a 12843 ret = stv_secondarg;
a4f59596 12844 std::swap (type0, type1);
12845 std::swap (op0, op1);
41ed701a 12846 }
12847
12848 if (TREE_CODE (type0) == INTEGER_TYPE
12849 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12850 {
22a75734 12851 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12852 {
12853 if (complain)
12854 error_at (loc, "conversion of scalar %qT to vector %qT "
12855 "involves truncation", type0, type1);
12856 return stv_error;
12857 }
12858 return ret;
12859 }
12860 else if (!integer_only_op
12861 /* Allow integer --> real conversion if safe. */
12862 && (TREE_CODE (type0) == REAL_TYPE
12863 || TREE_CODE (type0) == INTEGER_TYPE)
12864 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12865 {
22a75734 12866 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12867 {
12868 if (complain)
12869 error_at (loc, "conversion of scalar %qT to vector %qT "
12870 "involves truncation", type0, type1);
12871 return stv_error;
12872 }
12873 return ret;
12874 }
12875 default:
12876 break;
12877 }
12878
12879 return stv_nothing;
12880}
12881
ffcdbf9c 12882/* Return true iff ALIGN is an integral constant that is a fundamental
12883 alignment, as defined by [basic.align] in the c++-11
12884 specifications.
12885
12886 That is:
12887
12888 [A fundamental alignment is represented by an alignment less than or
12889 equal to the greatest alignment supported by the implementation
12890 in all contexts, which is equal to
12891 alignof(max_align_t)]. */
12892
12893bool
12894cxx_fundamental_alignment_p (unsigned align)
12895{
12896 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12897 TYPE_ALIGN (long_double_type_node)));
12898}
12899
46da3601 12900/* Return true if T is a pointer to a zero-sized aggregate. */
12901
12902bool
12903pointer_to_zero_sized_aggr_p (tree t)
12904{
12905 if (!POINTER_TYPE_P (t))
12906 return false;
12907 t = TREE_TYPE (t);
12908 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12909}
12910
547c6b1f 12911/* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
12912 with no library fallback or for an ADDR_EXPR whose operand is such type
12913 issues an error pointing to the location LOC.
12914 Returns true when the expression has been diagnosed and false
12915 otherwise. */
12916bool
12917reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
12918{
12919 if (TREE_CODE (expr) == ADDR_EXPR)
12920 expr = TREE_OPERAND (expr, 0);
12921
12922 if (TREE_TYPE (expr)
12923 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
12924 && DECL_P (expr)
12925 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
12926 false positives for user-declared built-ins such as abs or
12927 strlen, and for C++ operators new and delete.
12928 The c_decl_implicit() test avoids false positives for implicitly
12929 declared built-ins with library fallbacks (such as abs). */
12930 && DECL_BUILT_IN (expr)
12931 && DECL_IS_BUILTIN (expr)
12932 && !c_decl_implicit (expr)
12933 && !DECL_ASSEMBLER_NAME_SET_P (expr))
12934 {
12935 if (loc == UNKNOWN_LOCATION)
12936 loc = EXPR_LOC_OR_LOC (expr, input_location);
12937
12938 /* Reject arguments that are built-in functions with
12939 no library fallback. */
12940 error_at (loc, "built-in function %qE must be directly called", expr);
12941
12942 return true;
12943 }
12944
12945 return false;
12946}
12947
ef17a71a 12948/* If we're creating an if-else-if condition chain, first see if we
12949 already have this COND in the CHAIN. If so, warn and don't add COND
12950 into the vector, otherwise add the COND there. LOC is the location
12951 of COND. */
12952
12953void
12954warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
12955{
12956 /* No chain has been created yet. Do nothing. */
12957 if (*chain == NULL)
12958 return;
12959
12960 if (TREE_SIDE_EFFECTS (cond))
12961 {
12962 /* Uh-oh! This condition has a side-effect, thus invalidates
12963 the whole chain. */
12964 delete *chain;
12965 *chain = NULL;
12966 return;
12967 }
12968
12969 unsigned int ix;
12970 tree t;
12971 bool found = false;
12972 FOR_EACH_VEC_ELT (**chain, ix, t)
12973 if (operand_equal_p (cond, t, 0))
12974 {
12975 if (warning_at (loc, OPT_Wduplicated_cond,
12976 "duplicated %<if%> condition"))
12977 inform (EXPR_LOCATION (t), "previously used here");
12978 found = true;
12979 break;
12980 }
12981
12982 if (!found
12983 && !CONSTANT_CLASS_P (cond)
12984 /* Don't infinitely grow the chain. */
12985 && (*chain)->length () < 512)
12986 (*chain)->safe_push (cond);
12987}
12988
7bedc3a0 12989#include "gt-c-family-c-common.h"