]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-common.c
2015-08-06 Yaakov Selkowitz <yselkowi@redhat.com>
[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
242
574a6990 243/* Nonzero means generate separate instantiation control files and
244 juggle them at link time. */
245
246int flag_use_repository;
247
d875b9d2 248/* The C++ dialect being used. Default set in c_common_post_options. */
0fe6eeac 249
d875b9d2 250enum cxx_dialect cxx_dialect = cxx_unset;
0fe6eeac 251
9dcd0d49 252/* Maximum template instantiation depth. This limit exists to limit the
8ce59854 253 time it takes to notice excessively recursive template instantiations.
574a6990 254
8ce59854 255 The default is lower than the 1024 recommended by the C++0x standard
256 because G++ runs out of stack before 1024 with highly recursive template
257 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
258
259int max_tinst_depth = 900;
574a6990 260
988fc1d1 261/* The elements of `ridpointers' are identifier nodes for the reserved
262 type names and storage classes. It is indexed by a RID_... value. */
263tree *ridpointers;
264
e60a6f7b 265tree (*make_fname_decl) (location_t, tree, int);
9e5a737d 266
48d94ede 267/* Nonzero means don't warn about problems that occur when the code is
268 executed. */
269int c_inhibit_evaluation_warnings;
e78703c1 270
93be21c0 271/* Whether we are building a boolean conversion inside
272 convert_for_assignment, or some other late binary operation. If
273 build_binary_op is called for C (from code shared by C and C++) in
274 this case, then the operands have already been folded and the
275 result will not be folded again, so C_MAYBE_CONST_EXPR should not
276 be generated. */
277bool in_late_binary_op;
278
7f5f3953 279/* Whether lexing has been completed, so subsequent preprocessor
280 errors should use the compiler's input_location. */
281bool done_lexing = false;
282
2c0e001b 283/* Information about how a function name is generated. */
65b7f83f 284struct fname_var_t
285{
e99c3a1d 286 tree *const decl; /* pointer to the VAR_DECL. */
287 const unsigned rid; /* RID number for the identifier. */
288 const int pretty; /* How pretty is it? */
65b7f83f 289};
290
2c0e001b 291/* The three ways of getting then name of the current function. */
65b7f83f 292
293const struct fname_var_t fname_vars[] =
294{
2c0e001b 295 /* C99 compliant __func__, must be first. */
65b7f83f 296 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
2c0e001b 297 /* GCC __FUNCTION__ compliant. */
65b7f83f 298 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
2c0e001b 299 /* GCC __PRETTY_FUNCTION__ compliant. */
65b7f83f 300 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
301 {NULL, 0, 0},
302};
303
79396169 304/* Global visibility options. */
305struct visibility_flags visibility_options;
306
aac24642 307static tree c_fully_fold_internal (tree expr, bool, bool *, bool *, bool);
2d2f6a15 308static tree check_case_value (location_t, tree);
be23b16f 309static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
310 bool *);
be43ff5a 311
1cae46be 312static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
313static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
314static tree handle_common_attribute (tree *, tree, tree, int, bool *);
315static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
5de92639 316static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
317static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
a9196da9 318static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
319 int, bool *);
d413ffdd 320static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
321 int, bool *);
05f893e1 322static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
323 bool *);
947aa916 324static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
1cae46be 325static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
bdb1f0d1 326static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
85fbea97 327static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
7bd95dfd 328static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
1cae46be 329static tree handle_always_inline_attribute (tree *, tree, tree, int,
330 bool *);
1b16fc45 331static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
332static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
0cdd9887 333static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
10fc867f 334static tree handle_error_attribute (tree *, tree, tree, int, bool *);
1cae46be 335static tree handle_used_attribute (tree *, tree, tree, int, bool *);
336static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
62eec3b4 337static tree handle_externally_visible_attribute (tree *, tree, tree, int,
338 bool *);
6b722052 339static tree handle_no_reorder_attribute (tree *, tree, tree, int,
340 bool *);
1cae46be 341static tree handle_const_attribute (tree *, tree, tree, int, bool *);
342static tree handle_transparent_union_attribute (tree *, tree, tree,
343 int, bool *);
344static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
345static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
346static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
347static tree handle_section_attribute (tree *, tree, tree, int, bool *);
348static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
349static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
4641882f 350static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
85c0a25c 351static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
352static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
1cae46be 353static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
f4a30bd7 354static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
1cae46be 355static tree handle_visibility_attribute (tree *, tree, tree, int,
356 bool *);
357static tree handle_tls_model_attribute (tree *, tree, tree, int,
358 bool *);
359static tree handle_no_instrument_function_attribute (tree *, tree,
360 tree, int, bool *);
361static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
26d1c5ff 362static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
1cae46be 363static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
364 bool *);
365static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
4c0315d0 366static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
367static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
fc09b200 368static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
1cae46be 369static tree handle_deprecated_attribute (tree *, tree, tree, int,
370 bool *);
371static tree handle_vector_size_attribute (tree *, tree, tree, int,
372 bool *);
373static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
374static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
375static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
8a8cdb8d 376static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
377 bool *);
bf6c8de0 378static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
b5c26b42 379static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
4a29c97c 380static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
237e78b1 381static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
382static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
24470055 383static tree handle_target_attribute (tree *, tree, tree, int, bool *);
46f8e3b0 384static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
4c0315d0 385static tree ignore_attribute (tree *, tree, tree, int, bool *);
48b14f50 386static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
8ce86007 387static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
a96c3cc1 388static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
d7dcba40 389static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
bc7bff74 390static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
391 bool *);
392static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
393 bool *);
74691f46 394static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
058a1b7a 395static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
396static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
397static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
1cae46be 398
d01f58f9 399static void check_function_nonnull (tree, int, tree *);
1cae46be 400static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
401static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
402static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
860251be 403static int resort_field_decl_cmp (const void *, const void *);
dbf6c367 404
5c6e5756 405/* Reserved words. The third field is a mask: keywords are disabled
406 if they match the mask.
407
408 Masks for languages:
409 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
410 C --std=c99: D_CXXONLY | D_OBJC
411 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
412 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
413 C++ --std=c0x: D_CONLY | D_OBJC
414 ObjC++ is like C++ except that D_OBJC is not set
415
416 If -fno-asm is used, D_ASM is added to the mask. If
417 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
418 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
b27e241e 419 In C with -Wc++-compat, we warn if D_CXXWARN is set.
5c6e5756 420
b27e241e 421 Note the complication of the D_CXX_OBJC keywords. These are
422 reserved words such as 'class'. In C++, 'class' is a reserved
423 word. In Objective-C++ it is too. In Objective-C, it is a
424 reserved word too, but only if it follows an '@' sign.
425*/
5c6e5756 426const struct c_common_resword c_common_reswords[] =
427{
83e25171 428 { "_Alignas", RID_ALIGNAS, D_CONLY },
429 { "_Alignof", RID_ALIGNOF, D_CONLY },
b560fabd 430 { "_Atomic", RID_ATOMIC, D_CONLY },
5c6e5756 431 { "_Bool", RID_BOOL, D_CONLY },
432 { "_Complex", RID_COMPLEX, 0 },
d037099f 433 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
434 { "_Cilk_sync", RID_CILK_SYNC, 0 },
40750995 435 { "_Cilk_for", RID_CILK_FOR, 0 },
c1800156 436 { "_Imaginary", RID_IMAGINARY, D_CONLY },
5c6e5756 437 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
438 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
439 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
440 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
441 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
442 { "_Sat", RID_SAT, D_CONLY | D_EXT },
f80e7755 443 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
985c6e3a 444 { "_Noreturn", RID_NORETURN, D_CONLY },
7aa04c8d 445 { "_Generic", RID_GENERIC, D_CONLY },
d184e0c0 446 { "_Thread_local", RID_THREAD, D_CONLY },
5c6e5756 447 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
448 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
449 { "__alignof", RID_ALIGNOF, 0 },
450 { "__alignof__", RID_ALIGNOF, 0 },
451 { "__asm", RID_ASM, 0 },
452 { "__asm__", RID_ASM, 0 },
453 { "__attribute", RID_ATTRIBUTE, 0 },
454 { "__attribute__", RID_ATTRIBUTE, 0 },
4fba5eb9 455 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
e6014a82 456 { "__bases", RID_BASES, D_CXXONLY },
6f58cf06 457 { "__builtin_call_with_static_chain",
458 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
5c6e5756 459 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
bff4ad11 460 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
bf0cb017 461 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
5c6e5756 462 { "__builtin_offsetof", RID_OFFSETOF, 0 },
463 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
464 { "__builtin_va_arg", RID_VA_ARG, 0 },
465 { "__complex", RID_COMPLEX, 0 },
466 { "__complex__", RID_COMPLEX, 0 },
467 { "__const", RID_CONST, 0 },
468 { "__const__", RID_CONST, 0 },
469 { "__decltype", RID_DECLTYPE, D_CXXONLY },
e6014a82 470 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
5c6e5756 471 { "__extension__", RID_EXTENSION, 0 },
472 { "__func__", RID_C99_FUNCTION_NAME, 0 },
473 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
474 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
475 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
476 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
477 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
478 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
479 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
480 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
23407dc9 481 { "__imag", RID_IMAGPART, 0 },
482 { "__imag__", RID_IMAGPART, 0 },
483 { "__inline", RID_INLINE, 0 },
484 { "__inline__", RID_INLINE, 0 },
5c6e5756 485 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
486 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
487 { "__is_class", RID_IS_CLASS, D_CXXONLY },
5c6e5756 488 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
489 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
aa4313eb 490 { "__is_final", RID_IS_FINAL, D_CXXONLY },
23407dc9 491 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
5c6e5756 492 { "__is_pod", RID_IS_POD, D_CXXONLY },
493 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
c1c67b4f 494 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
495 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
f76a9aa8 496 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
497 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
717e52f9 498 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
5c6e5756 499 { "__is_union", RID_IS_UNION, D_CXXONLY },
5c6e5756 500 { "__label__", RID_LABEL, 0 },
501 { "__null", RID_NULL, 0 },
502 { "__real", RID_REALPART, 0 },
503 { "__real__", RID_REALPART, 0 },
504 { "__restrict", RID_RESTRICT, 0 },
505 { "__restrict__", RID_RESTRICT, 0 },
506 { "__signed", RID_SIGNED, 0 },
507 { "__signed__", RID_SIGNED, 0 },
508 { "__thread", RID_THREAD, 0 },
4c0315d0 509 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
510 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
511 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
5c6e5756 512 { "__typeof", RID_TYPEOF, 0 },
513 { "__typeof__", RID_TYPEOF, 0 },
23407dc9 514 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
5c6e5756 515 { "__volatile", RID_VOLATILE, 0 },
516 { "__volatile__", RID_VOLATILE, 0 },
fa769cc5 517 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
518 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 519 { "asm", RID_ASM, D_ASM },
520 { "auto", RID_AUTO, 0 },
dbd982c9 521 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
5c6e5756 522 { "break", RID_BREAK, 0 },
523 { "case", RID_CASE, 0 },
51030405 524 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
5c6e5756 525 { "char", RID_CHAR, 0 },
fa769cc5 526 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
527 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
51030405 528 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
5c6e5756 529 { "const", RID_CONST, 0 },
fa769cc5 530 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 531 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
532 { "continue", RID_CONTINUE, 0 },
fa769cc5 533 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 534 { "default", RID_DEFAULT, 0 },
51030405 535 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
5c6e5756 536 { "do", RID_DO, 0 },
537 { "double", RID_DOUBLE, 0 },
538 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
539 { "else", RID_ELSE, 0 },
540 { "enum", RID_ENUM, 0 },
51030405 541 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
542 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
5c6e5756 543 { "extern", RID_EXTERN, 0 },
51030405 544 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
5c6e5756 545 { "float", RID_FLOAT, 0 },
546 { "for", RID_FOR, 0 },
51030405 547 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
5c6e5756 548 { "goto", RID_GOTO, 0 },
549 { "if", RID_IF, 0 },
550 { "inline", RID_INLINE, D_EXT89 },
551 { "int", RID_INT, 0 },
552 { "long", RID_LONG, 0 },
553 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
51030405 554 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
555 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
fa769cc5 556 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
557 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
51030405 558 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
559 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
560 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
561 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
5c6e5756 562 { "register", RID_REGISTER, 0 },
563 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
564 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
565 { "return", RID_RETURN, 0 },
566 { "short", RID_SHORT, 0 },
567 { "signed", RID_SIGNED, 0 },
568 { "sizeof", RID_SIZEOF, 0 },
569 { "static", RID_STATIC, 0 },
fa769cc5 570 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 571 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
572 { "struct", RID_STRUCT, 0 },
573 { "switch", RID_SWITCH, 0 },
51030405 574 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
575 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
fa769cc5 576 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
51030405 577 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
578 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
579 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
5c6e5756 580 { "typedef", RID_TYPEDEF, 0 },
51030405 581 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
582 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
5c6e5756 583 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
584 { "union", RID_UNION, 0 },
585 { "unsigned", RID_UNSIGNED, 0 },
51030405 586 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
587 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
5c6e5756 588 { "void", RID_VOID, 0 },
589 { "volatile", RID_VOLATILE, 0 },
590 { "wchar_t", RID_WCHAR, D_CXXONLY },
591 { "while", RID_WHILE, 0 },
592 /* These Objective-C keywords are recognized only immediately after
593 an '@'. */
594 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
595 { "defs", RID_AT_DEFS, D_OBJC },
596 { "encode", RID_AT_ENCODE, D_OBJC },
597 { "end", RID_AT_END, D_OBJC },
598 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
599 { "interface", RID_AT_INTERFACE, D_OBJC },
600 { "protocol", RID_AT_PROTOCOL, D_OBJC },
601 { "selector", RID_AT_SELECTOR, D_OBJC },
602 { "finally", RID_AT_FINALLY, D_OBJC },
603 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
069761fb 604 { "optional", RID_AT_OPTIONAL, D_OBJC },
605 { "required", RID_AT_REQUIRED, D_OBJC },
86c110ac 606 { "property", RID_AT_PROPERTY, D_OBJC },
4a8875ed 607 { "package", RID_AT_PACKAGE, D_OBJC },
e1f293c0 608 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
609 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
5c6e5756 610 /* These are recognized only in protocol-qualifier context
611 (see above) */
612 { "bycopy", RID_BYCOPY, D_OBJC },
613 { "byref", RID_BYREF, D_OBJC },
614 { "in", RID_IN, D_OBJC },
615 { "inout", RID_INOUT, D_OBJC },
616 { "oneway", RID_ONEWAY, D_OBJC },
617 { "out", RID_OUT, D_OBJC },
86c110ac 618 /* These are recognized inside a property attribute list */
7590f0e5 619 { "assign", RID_ASSIGN, D_OBJC },
620 { "copy", RID_COPY, D_OBJC },
86c110ac 621 { "getter", RID_GETTER, D_OBJC },
7590f0e5 622 { "nonatomic", RID_NONATOMIC, D_OBJC },
623 { "readonly", RID_READONLY, D_OBJC },
624 { "readwrite", RID_READWRITE, D_OBJC },
625 { "retain", RID_RETAIN, D_OBJC },
86c110ac 626 { "setter", RID_SETTER, D_OBJC },
5c6e5756 627};
628
629const unsigned int num_c_common_reswords =
630 sizeof c_common_reswords / sizeof (struct c_common_resword);
631
058a1b7a 632/* Table of machine-independent attributes common to all C-like languages.
633
634 All attributes referencing arguments should be additionally processed
635 in chkp_copy_function_type_adding_bounds for correct instrumentation
636 by Pointer Bounds Checker.
637 Current list of processed common attributes: nonnull. */
f8e93a2e 638const struct attribute_spec c_common_attribute_table[] =
639{
ac86af5d 640 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
641 affects_type_identity } */
f8e93a2e 642 { "packed", 0, 0, false, false, false,
ac86af5d 643 handle_packed_attribute , false},
f8e93a2e 644 { "nocommon", 0, 0, true, false, false,
ac86af5d 645 handle_nocommon_attribute, false},
f8e93a2e 646 { "common", 0, 0, true, false, false,
ac86af5d 647 handle_common_attribute, false },
f8e93a2e 648 /* FIXME: logically, noreturn attributes should be listed as
649 "false, true, true" and apply to function types. But implementing this
650 would require all the places in the compiler that use TREE_THIS_VOLATILE
651 on a decl to identify non-returning functions to be located and fixed
652 to check the function type instead. */
653 { "noreturn", 0, 0, true, false, false,
ac86af5d 654 handle_noreturn_attribute, false },
f8e93a2e 655 { "volatile", 0, 0, true, false, false,
ac86af5d 656 handle_noreturn_attribute, false },
947aa916 657 { "stack_protect", 0, 0, true, false, false,
658 handle_stack_protect_attribute, false },
f8e93a2e 659 { "noinline", 0, 0, true, false, false,
ac86af5d 660 handle_noinline_attribute, false },
bdb1f0d1 661 { "noclone", 0, 0, true, false, false,
ac86af5d 662 handle_noclone_attribute, false },
85fbea97 663 { "no_icf", 0, 0, true, false, false,
664 handle_noicf_attribute, false },
7bd95dfd 665 { "leaf", 0, 0, true, false, false,
ac86af5d 666 handle_leaf_attribute, false },
f8e93a2e 667 { "always_inline", 0, 0, true, false, false,
ac86af5d 668 handle_always_inline_attribute, false },
541e4101 669 { "gnu_inline", 0, 0, true, false, false,
ac86af5d 670 handle_gnu_inline_attribute, false },
1b16fc45 671 { "artificial", 0, 0, true, false, false,
ac86af5d 672 handle_artificial_attribute, false },
0cdd9887 673 { "flatten", 0, 0, true, false, false,
ac86af5d 674 handle_flatten_attribute, false },
f8e93a2e 675 { "used", 0, 0, true, false, false,
ac86af5d 676 handle_used_attribute, false },
f8e93a2e 677 { "unused", 0, 0, false, false, false,
ac86af5d 678 handle_unused_attribute, false },
62eec3b4 679 { "externally_visible", 0, 0, true, false, false,
ac86af5d 680 handle_externally_visible_attribute, false },
6b722052 681 { "no_reorder", 0, 0, true, false, false,
682 handle_no_reorder_attribute, false },
f8e93a2e 683 /* The same comments as for noreturn attributes apply to const ones. */
684 { "const", 0, 0, true, false, false,
ac86af5d 685 handle_const_attribute, false },
f8e93a2e 686 { "transparent_union", 0, 0, false, false, false,
ac86af5d 687 handle_transparent_union_attribute, false },
9af7fd5b 688 { "constructor", 0, 1, true, false, false,
ac86af5d 689 handle_constructor_attribute, false },
9af7fd5b 690 { "destructor", 0, 1, true, false, false,
ac86af5d 691 handle_destructor_attribute, false },
f8e93a2e 692 { "mode", 1, 1, false, true, false,
ac86af5d 693 handle_mode_attribute, false },
f8e93a2e 694 { "section", 1, 1, true, false, false,
ac86af5d 695 handle_section_attribute, false },
f8e93a2e 696 { "aligned", 0, 1, false, false, false,
ac86af5d 697 handle_aligned_attribute, false },
f8e93a2e 698 { "weak", 0, 0, true, false, false,
ac86af5d 699 handle_weak_attribute, false },
4641882f 700 { "noplt", 0, 0, true, false, false,
701 handle_noplt_attribute, false },
85c0a25c 702 { "ifunc", 1, 1, true, false, false,
ac86af5d 703 handle_ifunc_attribute, false },
f8e93a2e 704 { "alias", 1, 1, true, false, false,
ac86af5d 705 handle_alias_attribute, false },
f4a30bd7 706 { "weakref", 0, 1, true, false, false,
ac86af5d 707 handle_weakref_attribute, false },
f8e93a2e 708 { "no_instrument_function", 0, 0, true, false, false,
ac86af5d 709 handle_no_instrument_function_attribute,
710 false },
f8e93a2e 711 { "malloc", 0, 0, true, false, false,
ac86af5d 712 handle_malloc_attribute, false },
26d1c5ff 713 { "returns_twice", 0, 0, true, false, false,
ac86af5d 714 handle_returns_twice_attribute, false },
f8e93a2e 715 { "no_stack_limit", 0, 0, true, false, false,
ac86af5d 716 handle_no_limit_stack_attribute, false },
f8e93a2e 717 { "pure", 0, 0, true, false, false,
ac86af5d 718 handle_pure_attribute, false },
4c0315d0 719 { "transaction_callable", 0, 0, false, true, false,
720 handle_tm_attribute, false },
721 { "transaction_unsafe", 0, 0, false, true, false,
722 handle_tm_attribute, false },
723 { "transaction_safe", 0, 0, false, true, false,
724 handle_tm_attribute, false },
725 { "transaction_may_cancel_outer", 0, 0, false, true, false,
726 handle_tm_attribute, false },
727 /* ??? These two attributes didn't make the transition from the
728 Intel language document to the multi-vendor language document. */
729 { "transaction_pure", 0, 0, false, true, false,
730 handle_tm_attribute, false },
731 { "transaction_wrap", 1, 1, true, false, false,
732 handle_tm_wrap_attribute, false },
fc09b200 733 /* For internal use (marking of builtins) only. The name contains space
734 to prevent its usage in source code. */
735 { "no vops", 0, 0, true, false, false,
ac86af5d 736 handle_novops_attribute, false },
45c4e798 737 { "deprecated", 0, 1, false, false, false,
ac86af5d 738 handle_deprecated_attribute, false },
f8e93a2e 739 { "vector_size", 1, 1, false, true, false,
ac86af5d 740 handle_vector_size_attribute, false },
b212f378 741 { "visibility", 1, 1, false, false, false,
ac86af5d 742 handle_visibility_attribute, false },
24dfead4 743 { "tls_model", 1, 1, true, false, false,
ac86af5d 744 handle_tls_model_attribute, false },
dbf6c367 745 { "nonnull", 0, -1, false, true, true,
ac86af5d 746 handle_nonnull_attribute, false },
fa987697 747 { "nothrow", 0, 0, true, false, false,
ac86af5d 748 handle_nothrow_attribute, false },
749 { "may_alias", 0, 0, false, true, false, NULL, false },
7acb29a3 750 { "cleanup", 1, 1, true, false, false,
ac86af5d 751 handle_cleanup_attribute, false },
8a8cdb8d 752 { "warn_unused_result", 0, 0, false, true, true,
ac86af5d 753 handle_warn_unused_result_attribute, false },
50ca527f 754 { "sentinel", 0, 1, false, true, true,
ac86af5d 755 handle_sentinel_attribute, false },
b5c26b42 756 /* For internal use (marking of builtins) only. The name contains space
757 to prevent its usage in source code. */
758 { "type generic", 0, 0, false, true, true,
ac86af5d 759 handle_type_generic_attribute, false },
4a29c97c 760 { "alloc_size", 1, 2, false, true, true,
ac86af5d 761 handle_alloc_size_attribute, false },
5de92639 762 { "cold", 0, 0, true, false, false,
ac86af5d 763 handle_cold_attribute, false },
5de92639 764 { "hot", 0, 0, true, false, false,
ac86af5d 765 handle_hot_attribute, false },
d413ffdd 766 { "no_address_safety_analysis",
767 0, 0, true, false, false,
768 handle_no_address_safety_analysis_attribute,
769 false },
a9196da9 770 { "no_sanitize_address", 0, 0, true, false, false,
771 handle_no_sanitize_address_attribute,
772 false },
d1e96383 773 { "no_sanitize_thread", 0, 0, true, false, false,
774 handle_no_sanitize_address_attribute,
775 false },
05f893e1 776 { "no_sanitize_undefined", 0, 0, true, false, false,
777 handle_no_sanitize_undefined_attribute,
778 false },
10fc867f 779 { "warning", 1, 1, true, false, false,
ac86af5d 780 handle_error_attribute, false },
10fc867f 781 { "error", 1, 1, true, false, false,
ac86af5d 782 handle_error_attribute, false },
24470055 783 { "target", 1, -1, true, false, false,
ac86af5d 784 handle_target_attribute, false },
46f8e3b0 785 { "optimize", 1, -1, true, false, false,
ac86af5d 786 handle_optimize_attribute, false },
4c0315d0 787 /* For internal use only. The leading '*' both prevents its usage in
788 source code and signals that it may be overridden by machine tables. */
789 { "*tm regparm", 0, 0, false, true, true,
790 ignore_attribute, false },
48b14f50 791 { "no_split_stack", 0, 0, true, false, false,
ac86af5d 792 handle_no_split_stack_attribute, false },
8ce86007 793 /* For internal use (marking of builtins and runtime functions) only.
794 The name contains space to prevent its usage in source code. */
795 { "fn spec", 1, 1, false, true, true,
ac86af5d 796 handle_fnspec_attribute, false },
a96c3cc1 797 { "warn_unused", 0, 0, false, false, false,
798 handle_warn_unused_attribute, false },
d7dcba40 799 { "returns_nonnull", 0, 0, false, true, true,
800 handle_returns_nonnull_attribute, false },
bc7bff74 801 { "omp declare simd", 0, -1, true, false, false,
802 handle_omp_declare_simd_attribute, false },
74acc703 803 { "cilk simd function", 0, -1, true, false, false,
804 handle_omp_declare_simd_attribute, false },
bc7bff74 805 { "omp declare target", 0, 0, true, false, false,
806 handle_omp_declare_target_attribute, false },
237e78b1 807 { "alloc_align", 1, 1, false, true, true,
808 handle_alloc_align_attribute, false },
809 { "assume_aligned", 1, 2, false, true, true,
810 handle_assume_aligned_attribute, false },
74691f46 811 { "designated_init", 0, 0, false, true, false,
812 handle_designated_init_attribute, false },
058a1b7a 813 { "bnd_variable_size", 0, 0, true, false, false,
814 handle_bnd_variable_size_attribute, false },
815 { "bnd_legacy", 0, 0, true, false, false,
816 handle_bnd_legacy, false },
817 { "bnd_instrument", 0, 0, true, false, false,
818 handle_bnd_instrument, false },
ac86af5d 819 { NULL, 0, 0, false, false, false, NULL, false }
f8e93a2e 820};
821
822/* Give the specifications for the format attributes, used by C and all
058a1b7a 823 descendants.
f8e93a2e 824
058a1b7a 825 All attributes referencing arguments should be additionally processed
826 in chkp_copy_function_type_adding_bounds for correct instrumentation
827 by Pointer Bounds Checker.
828 Current list of processed format attributes: format, format_arg. */
f8e93a2e 829const struct attribute_spec c_common_format_attribute_table[] =
830{
ac86af5d 831 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
832 affects_type_identity } */
f8e93a2e 833 { "format", 3, 3, false, true, true,
ac86af5d 834 handle_format_attribute, false },
f8e93a2e 835 { "format_arg", 1, 1, false, true, true,
ac86af5d 836 handle_format_arg_attribute, false },
837 { NULL, 0, 0, false, false, false, NULL, false }
f8e93a2e 838};
839
6d5d708e 840/* Return identifier for address space AS. */
34208e18 841
6d5d708e 842const char *
843c_addr_space_name (addr_space_t as)
844{
34208e18 845 int rid = RID_FIRST_ADDR_SPACE + as;
846 gcc_assert (ridpointers [rid]);
847 return IDENTIFIER_POINTER (ridpointers [rid]);
6d5d708e 848}
849
2c0e001b 850/* Push current bindings for the function name VAR_DECLS. */
f4e3c278 851
852void
1cae46be 853start_fname_decls (void)
f4e3c278 854{
65b7f83f 855 unsigned ix;
856 tree saved = NULL_TREE;
1cae46be 857
65b7f83f 858 for (ix = 0; fname_vars[ix].decl; ix++)
859 {
860 tree decl = *fname_vars[ix].decl;
f4e3c278 861
65b7f83f 862 if (decl)
863 {
ceb7b692 864 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
865 saved);
65b7f83f 866 *fname_vars[ix].decl = NULL_TREE;
867 }
868 }
869 if (saved || saved_function_name_decls)
870 /* Normally they'll have been NULL, so only push if we've got a
871 stack, or they are non-NULL. */
872 saved_function_name_decls = tree_cons (saved, NULL_TREE,
873 saved_function_name_decls);
874}
875
2363ef00 876/* Finish up the current bindings, adding them into the current function's
877 statement tree. This must be done _before_ finish_stmt_tree is called.
878 If there is no current function, we must be at file scope and no statements
879 are involved. Pop the previous bindings. */
65b7f83f 880
881void
1cae46be 882finish_fname_decls (void)
65b7f83f 883{
884 unsigned ix;
2363ef00 885 tree stmts = NULL_TREE;
65b7f83f 886 tree stack = saved_function_name_decls;
887
888 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
2363ef00 889 append_to_statement_list (TREE_VALUE (stack), &stmts);
1cae46be 890
2363ef00 891 if (stmts)
65b7f83f 892 {
2363ef00 893 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
5c423bd6 894
2363ef00 895 if (TREE_CODE (*bodyp) == BIND_EXPR)
896 bodyp = &BIND_EXPR_BODY (*bodyp);
81010c97 897
bc2b76e0 898 append_to_statement_list_force (*bodyp, &stmts);
2363ef00 899 *bodyp = stmts;
65b7f83f 900 }
1cae46be 901
65b7f83f 902 for (ix = 0; fname_vars[ix].decl; ix++)
903 *fname_vars[ix].decl = NULL_TREE;
1cae46be 904
65b7f83f 905 if (stack)
f4e3c278 906 {
2c0e001b 907 /* We had saved values, restore them. */
65b7f83f 908 tree saved;
909
910 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
911 {
912 tree decl = TREE_PURPOSE (saved);
f9ae6f95 913 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
1cae46be 914
65b7f83f 915 *fname_vars[ix].decl = decl;
916 }
917 stack = TREE_CHAIN (stack);
f4e3c278 918 }
65b7f83f 919 saved_function_name_decls = stack;
920}
921
81010c97 922/* Return the text name of the current function, suitably prettified
5fc7fa69 923 by PRETTY_P. Return string must be freed by caller. */
65b7f83f 924
925const char *
1cae46be 926fname_as_string (int pretty_p)
65b7f83f 927{
9ad4bb1e 928 const char *name = "top level";
5fc7fa69 929 char *namep;
8115b8be 930 int vrb = 2, len;
931 cpp_string cstr = { 0, 0 }, strname;
9ad4bb1e 932
84166705 933 if (!pretty_p)
9ad4bb1e 934 {
935 name = "";
936 vrb = 0;
937 }
938
939 if (current_function_decl)
dc24ddbd 940 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
9ad4bb1e 941
8115b8be 942 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
5fc7fa69 943
8115b8be 944 namep = XNEWVEC (char, len);
945 snprintf (namep, len, "\"%s\"", name);
946 strname.text = (unsigned char *) namep;
947 strname.len = len - 1;
5fc7fa69 948
924bbf02 949 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
8115b8be 950 {
951 XDELETEVEC (namep);
952 return (const char *) cstr.text;
5fc7fa69 953 }
5fc7fa69 954
955 return namep;
65b7f83f 956}
957
65b7f83f 958/* Return the VAR_DECL for a const char array naming the current
959 function. If the VAR_DECL has not yet been created, create it
960 now. RID indicates how it should be formatted and IDENTIFIER_NODE
961 ID is its name (unfortunately C and C++ hold the RID values of
962 keywords in different places, so we can't derive RID from ID in
e3b80d49 963 this language independent code. LOC is the location of the
964 function. */
65b7f83f 965
966tree
e3b80d49 967fname_decl (location_t loc, unsigned int rid, tree id)
65b7f83f 968{
969 unsigned ix;
970 tree decl = NULL_TREE;
971
972 for (ix = 0; fname_vars[ix].decl; ix++)
973 if (fname_vars[ix].rid == rid)
974 break;
975
976 decl = *fname_vars[ix].decl;
977 if (!decl)
f4e3c278 978 {
2222b3c6 979 /* If a tree is built here, it would normally have the lineno of
980 the current statement. Later this tree will be moved to the
981 beginning of the function and this line number will be wrong.
982 To avoid this problem set the lineno to 0 here; that prevents
7299020b 983 it from appearing in the RTL. */
2363ef00 984 tree stmts;
9a6486a6 985 location_t saved_location = input_location;
9a6486a6 986 input_location = UNKNOWN_LOCATION;
1cae46be 987
2363ef00 988 stmts = push_stmt_list ();
e60a6f7b 989 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
2363ef00 990 stmts = pop_stmt_list (stmts);
991 if (!IS_EMPTY_STMT (stmts))
992 saved_function_name_decls
993 = tree_cons (decl, stmts, saved_function_name_decls);
65b7f83f 994 *fname_vars[ix].decl = decl;
9a6486a6 995 input_location = saved_location;
f4e3c278 996 }
65b7f83f 997 if (!ix && !current_function_decl)
e3b80d49 998 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
81010c97 999
65b7f83f 1000 return decl;
f4e3c278 1001}
1002
070236f0 1003/* Given a STRING_CST, give it a suitable array-of-chars data type. */
b0fc3e72 1004
1005tree
1cae46be 1006fix_string_type (tree value)
b0fc3e72 1007{
070236f0 1008 int length = TREE_STRING_LENGTH (value);
1009 int nchars;
00d26680 1010 tree e_type, i_type, a_type;
1011
73be5127 1012 /* Compute the number of elements, for the array type. */
924bbf02 1013 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1014 {
1015 nchars = length;
1016 e_type = char_type_node;
1017 }
1018 else if (TREE_TYPE (value) == char16_array_type_node)
1019 {
1020 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1021 e_type = char16_type_node;
1022 }
1023 else if (TREE_TYPE (value) == char32_array_type_node)
1024 {
1025 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1026 e_type = char32_type_node;
1027 }
1028 else
1029 {
1030 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1031 e_type = wchar_type_node;
1032 }
b0fc3e72 1033
1d752508 1034 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1035 limit in C++98 Annex B is very large (65536) and is not normative,
1036 so we do not diagnose it (warn_overlength_strings is forced off
1037 in c_common_post_options). */
1038 if (warn_overlength_strings)
1039 {
1040 const int nchars_max = flag_isoc99 ? 4095 : 509;
1041 const int relevant_std = flag_isoc99 ? 99 : 90;
1042 if (nchars - 1 > nchars_max)
1043 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1044 separate the %d from the 'C'. 'ISO' should not be
1045 translated, but it may be moved after 'C%d' in languages
1046 where modifiers follow nouns. */
21ca8540 1047 pedwarn (input_location, OPT_Woverlength_strings,
8864917d 1048 "string length %qd is greater than the length %qd "
1d752508 1049 "ISO C%d compilers are required to support",
1050 nchars - 1, nchars_max, relevant_std);
1051 }
82cfc7f7 1052
390be14e 1053 /* Create the array type for the string constant. The ISO C++
1054 standard says that a string literal has type `const char[N]' or
1055 `const wchar_t[N]'. We use the same logic when invoked as a C
1056 front-end with -Wwrite-strings.
1057 ??? We should change the type of an expression depending on the
1058 state of a warning flag. We should just be warning -- see how
1059 this is handled in the C++ front-end for the deprecated implicit
1060 conversion from string literals to `char*' or `wchar_t*'.
00d26680 1061
1062 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1063 array type being the unqualified version of that type.
1064 Therefore, if we are constructing an array of const char, we must
1065 construct the matching unqualified array type first. The C front
1066 end does not require this, but it does no harm, so we do it
1067 unconditionally. */
ceb7b692 1068 i_type = build_index_type (size_int (nchars - 1));
00d26680 1069 a_type = build_array_type (e_type, i_type);
390be14e 1070 if (c_dialect_cxx() || warn_write_strings)
aebc8537 1071 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
3a10ba35 1072
00d26680 1073 TREE_TYPE (value) = a_type;
b8e3b7ad 1074 TREE_CONSTANT (value) = 1;
a814bad5 1075 TREE_READONLY (value) = 1;
b0fc3e72 1076 TREE_STATIC (value) = 1;
1077 return value;
1078}
1079\f
fc501191 1080/* If DISABLE is true, stop issuing warnings. This is used when
1081 parsing code that we know will not be executed. This function may
1082 be called multiple times, and works as a stack. */
1083
1084static void
1085c_disable_warnings (bool disable)
1086{
1087 if (disable)
1088 {
1089 ++c_inhibit_evaluation_warnings;
1090 fold_defer_overflow_warnings ();
1091 }
1092}
1093
1094/* If ENABLE is true, reenable issuing warnings. */
1095
1096static void
1097c_enable_warnings (bool enable)
1098{
1099 if (enable)
1100 {
1101 --c_inhibit_evaluation_warnings;
1102 fold_undefer_and_ignore_overflow_warnings ();
1103 }
1104}
1105
a75b1c71 1106/* Fully fold EXPR, an expression that was not folded (beyond integer
1107 constant expressions and null pointer constants) when being built
1108 up. If IN_INIT, this is in a static initializer and certain
1109 changes are made to the folding done. Clear *MAYBE_CONST if
1110 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1111 expression because it contains an evaluated operator (in C99) or an
1112 operator outside of sizeof returning an integer constant (in C90)
1113 not permitted in constant expressions, or because it contains an
1114 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1115 set to true by callers before calling this function.) Return the
1116 folded expression. Function arguments have already been folded
1117 before calling this function, as have the contents of SAVE_EXPR,
1118 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1119 C_MAYBE_CONST_EXPR. */
1120
1121tree
1122c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1123{
1124 tree ret;
c6418a4e 1125 tree eptype = NULL_TREE;
a75b1c71 1126 bool dummy = true;
1127 bool maybe_const_itself = true;
389dd41b 1128 location_t loc = EXPR_LOCATION (expr);
a75b1c71 1129
1130 /* This function is not relevant to C++ because C++ folds while
1131 parsing, and may need changes to be correct for C++ when C++
1132 stops folding while parsing. */
1133 if (c_dialect_cxx ())
1134 gcc_unreachable ();
1135
1136 if (!maybe_const)
1137 maybe_const = &dummy;
c6418a4e 1138 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1139 {
1140 eptype = TREE_TYPE (expr);
1141 expr = TREE_OPERAND (expr, 0);
1142 }
a75b1c71 1143 ret = c_fully_fold_internal (expr, in_init, maybe_const,
aac24642 1144 &maybe_const_itself, false);
c6418a4e 1145 if (eptype)
389dd41b 1146 ret = fold_convert_loc (loc, eptype, ret);
a75b1c71 1147 *maybe_const &= maybe_const_itself;
1148 return ret;
1149}
1150
1151/* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1152 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1153 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1154 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1155 both evaluated and unevaluated subexpressions while
1156 *MAYBE_CONST_ITSELF is carried from only evaluated
aac24642 1157 subexpressions). FOR_INT_CONST indicates if EXPR is an expression
1158 with integer constant operands, and if any of the operands doesn't
1159 get folded to an integer constant, don't fold the expression itself. */
a75b1c71 1160
1161static tree
1162c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
aac24642 1163 bool *maybe_const_itself, bool for_int_const)
a75b1c71 1164{
1165 tree ret = expr;
1166 enum tree_code code = TREE_CODE (expr);
1167 enum tree_code_class kind = TREE_CODE_CLASS (code);
1168 location_t loc = EXPR_LOCATION (expr);
1169 tree op0, op1, op2, op3;
1170 tree orig_op0, orig_op1, orig_op2;
1171 bool op0_const = true, op1_const = true, op2_const = true;
1172 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1173 bool nowarning = TREE_NO_WARNING (expr);
fc501191 1174 bool unused_p;
a75b1c71 1175
1176 /* This function is not relevant to C++ because C++ folds while
1177 parsing, and may need changes to be correct for C++ when C++
1178 stops folding while parsing. */
1179 if (c_dialect_cxx ())
1180 gcc_unreachable ();
1181
1182 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1183 anything else not counted as an expression cannot usefully be
1184 folded further at this point. */
1185 if (!IS_EXPR_CODE_CLASS (kind)
1186 || kind == tcc_statement
1187 || code == SAVE_EXPR)
1188 return expr;
1189
1190 /* Operands of variable-length expressions (function calls) have
1191 already been folded, as have __builtin_* function calls, and such
1192 expressions cannot occur in constant expressions. */
1193 if (kind == tcc_vl_exp)
1194 {
1195 *maybe_const_operands = false;
1196 ret = fold (expr);
1197 goto out;
1198 }
1199
1200 if (code == C_MAYBE_CONST_EXPR)
1201 {
1202 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1203 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1204 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1205 *maybe_const_operands = false;
1206 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
aac24642 1207 {
1208 *maybe_const_itself = false;
1209 inner = c_fully_fold_internal (inner, in_init, maybe_const_operands,
1210 maybe_const_itself, true);
1211 }
a75b1c71 1212 if (pre && !in_init)
1213 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1214 else
1215 ret = inner;
1216 goto out;
1217 }
1218
1219 /* Assignment, increment, decrement, function call and comma
1220 operators, and statement expressions, cannot occur in constant
1221 expressions if evaluated / outside of sizeof. (Function calls
1222 were handled above, though VA_ARG_EXPR is treated like a function
1223 call here, and statement expressions are handled through
1224 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1225 switch (code)
1226 {
1227 case MODIFY_EXPR:
1228 case PREDECREMENT_EXPR:
1229 case PREINCREMENT_EXPR:
1230 case POSTDECREMENT_EXPR:
1231 case POSTINCREMENT_EXPR:
1232 case COMPOUND_EXPR:
1233 *maybe_const_operands = false;
1234 break;
1235
1236 case VA_ARG_EXPR:
1237 case TARGET_EXPR:
1238 case BIND_EXPR:
1239 case OBJ_TYPE_REF:
1240 *maybe_const_operands = false;
1241 ret = fold (expr);
1242 goto out;
1243
1244 default:
1245 break;
1246 }
1247
1248 /* Fold individual tree codes as appropriate. */
1249 switch (code)
1250 {
1251 case COMPOUND_LITERAL_EXPR:
1252 /* Any non-constancy will have been marked in a containing
1253 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1254 goto out;
1255
1256 case COMPONENT_REF:
1257 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1258 op1 = TREE_OPERAND (expr, 1);
1259 op2 = TREE_OPERAND (expr, 2);
1260 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1261 maybe_const_itself, for_int_const);
f59e3889 1262 STRIP_TYPE_NOPS (op0);
a75b1c71 1263 if (op0 != orig_op0)
1264 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1265 if (ret != expr)
1266 {
1267 TREE_READONLY (ret) = TREE_READONLY (expr);
1268 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1269 }
1270 goto out;
1271
1272 case ARRAY_REF:
1273 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1274 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1275 op2 = TREE_OPERAND (expr, 2);
1276 op3 = TREE_OPERAND (expr, 3);
1277 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1278 maybe_const_itself, for_int_const);
f59e3889 1279 STRIP_TYPE_NOPS (op0);
a75b1c71 1280 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
aac24642 1281 maybe_const_itself, for_int_const);
f59e3889 1282 STRIP_TYPE_NOPS (op1);
a75b1c71 1283 op1 = decl_constant_value_for_optimization (op1);
1284 if (op0 != orig_op0 || op1 != orig_op1)
1285 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1286 if (ret != expr)
1287 {
1288 TREE_READONLY (ret) = TREE_READONLY (expr);
1289 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1290 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1291 }
1292 ret = fold (ret);
1293 goto out;
1294
1295 case COMPOUND_EXPR:
1296 case MODIFY_EXPR:
1297 case PREDECREMENT_EXPR:
1298 case PREINCREMENT_EXPR:
1299 case POSTDECREMENT_EXPR:
1300 case POSTINCREMENT_EXPR:
1301 case PLUS_EXPR:
1302 case MINUS_EXPR:
1303 case MULT_EXPR:
1304 case POINTER_PLUS_EXPR:
1305 case TRUNC_DIV_EXPR:
1306 case CEIL_DIV_EXPR:
1307 case FLOOR_DIV_EXPR:
1308 case TRUNC_MOD_EXPR:
1309 case RDIV_EXPR:
1310 case EXACT_DIV_EXPR:
1311 case LSHIFT_EXPR:
1312 case RSHIFT_EXPR:
1313 case BIT_IOR_EXPR:
1314 case BIT_XOR_EXPR:
1315 case BIT_AND_EXPR:
1316 case LT_EXPR:
1317 case LE_EXPR:
1318 case GT_EXPR:
1319 case GE_EXPR:
1320 case EQ_EXPR:
1321 case NE_EXPR:
1322 case COMPLEX_EXPR:
1323 case TRUTH_AND_EXPR:
1324 case TRUTH_OR_EXPR:
1325 case TRUTH_XOR_EXPR:
1326 case UNORDERED_EXPR:
1327 case ORDERED_EXPR:
1328 case UNLT_EXPR:
1329 case UNLE_EXPR:
1330 case UNGT_EXPR:
1331 case UNGE_EXPR:
1332 case UNEQ_EXPR:
1333 /* Binary operations evaluating both arguments (increment and
1334 decrement are binary internally in GCC). */
1335 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1336 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1337 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1338 maybe_const_itself, for_int_const);
f59e3889 1339 STRIP_TYPE_NOPS (op0);
a75b1c71 1340 if (code != MODIFY_EXPR
1341 && code != PREDECREMENT_EXPR
1342 && code != PREINCREMENT_EXPR
1343 && code != POSTDECREMENT_EXPR
1344 && code != POSTINCREMENT_EXPR)
1345 op0 = decl_constant_value_for_optimization (op0);
1346 /* The RHS of a MODIFY_EXPR was fully folded when building that
1347 expression for the sake of conversion warnings. */
1348 if (code != MODIFY_EXPR)
1349 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
aac24642 1350 maybe_const_itself, for_int_const);
f59e3889 1351 STRIP_TYPE_NOPS (op1);
a75b1c71 1352 op1 = decl_constant_value_for_optimization (op1);
aac24642 1353
1354 if (for_int_const && (TREE_CODE (op0) != INTEGER_CST
1355 || TREE_CODE (op1) != INTEGER_CST))
1356 goto out;
1357
a75b1c71 1358 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1359 ret = in_init
389dd41b 1360 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1361 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
a75b1c71 1362 else
1363 ret = fold (expr);
672d914b 1364 if (TREE_OVERFLOW_P (ret)
1365 && !TREE_OVERFLOW_P (op0)
1366 && !TREE_OVERFLOW_P (op1))
8d669e79 1367 overflow_warning (EXPR_LOC_OR_LOC (expr, input_location), ret);
cdc64059 1368 if (code == LSHIFT_EXPR
1369 && TREE_CODE (orig_op0) != INTEGER_CST
1370 && TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1371 && TREE_CODE (op0) == INTEGER_CST
1372 && c_inhibit_evaluation_warnings == 0
1373 && tree_int_cst_sgn (op0) < 0)
1374 warning_at (loc, OPT_Wshift_negative_value,
1375 "left shift of negative value");
7c834436 1376 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1377 && TREE_CODE (orig_op1) != INTEGER_CST
1378 && TREE_CODE (op1) == INTEGER_CST
1379 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1380 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1381 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1382 && c_inhibit_evaluation_warnings == 0)
1383 {
1384 if (tree_int_cst_sgn (op1) < 0)
f11bdffb 1385 warning_at (loc, OPT_Wshift_count_negative,
1386 (code == LSHIFT_EXPR
1387 ? G_("left shift count is negative")
1388 : G_("right shift count is negative")));
7c834436 1389 else if (compare_tree_int (op1,
1390 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1391 >= 0)
f11bdffb 1392 warning_at (loc, OPT_Wshift_count_overflow,
1393 (code == LSHIFT_EXPR
1394 ? G_("left shift count >= width of type")
1395 : G_("right shift count >= width of type")));
7c834436 1396 }
8d669e79 1397 if (code == LSHIFT_EXPR
1398 /* If either OP0 has been folded to INTEGER_CST... */
1399 && ((TREE_CODE (orig_op0) != INTEGER_CST
1400 && TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1401 && TREE_CODE (op0) == INTEGER_CST)
1402 /* ...or if OP1 has been folded to INTEGER_CST... */
1403 || (TREE_CODE (orig_op1) != INTEGER_CST
1404 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1405 && TREE_CODE (op1) == INTEGER_CST))
1406 && c_inhibit_evaluation_warnings == 0)
1407 /* ...then maybe we can detect an overflow. */
1408 maybe_warn_shift_overflow (loc, op0, op1);
b57910fa 1409 if ((code == TRUNC_DIV_EXPR
1410 || code == CEIL_DIV_EXPR
1411 || code == FLOOR_DIV_EXPR
1412 || code == EXACT_DIV_EXPR
1413 || code == TRUNC_MOD_EXPR)
1414 && TREE_CODE (orig_op1) != INTEGER_CST
1415 && TREE_CODE (op1) == INTEGER_CST
1416 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1417 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1418 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE)
1419 warn_for_div_by_zero (loc, op1);
a75b1c71 1420 goto out;
1421
1422 case INDIRECT_REF:
1423 case FIX_TRUNC_EXPR:
1424 case FLOAT_EXPR:
1425 CASE_CONVERT:
e66325ea 1426 case ADDR_SPACE_CONVERT_EXPR:
2b19dfe4 1427 case VIEW_CONVERT_EXPR:
a75b1c71 1428 case NON_LVALUE_EXPR:
1429 case NEGATE_EXPR:
1430 case BIT_NOT_EXPR:
1431 case TRUTH_NOT_EXPR:
1432 case ADDR_EXPR:
1433 case CONJ_EXPR:
1434 case REALPART_EXPR:
1435 case IMAGPART_EXPR:
1436 /* Unary operations. */
1437 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1438 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1439 maybe_const_itself, for_int_const);
f59e3889 1440 STRIP_TYPE_NOPS (op0);
a75b1c71 1441 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1442 op0 = decl_constant_value_for_optimization (op0);
aac24642 1443
1444 if (for_int_const && TREE_CODE (op0) != INTEGER_CST)
1445 goto out;
1446
737a23cc 1447 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1448 not prepared to deal with them if they occur in initializers. */
1449 if (op0 != orig_op0
1450 && code == ADDR_EXPR
1451 && (op1 = get_base_address (op0)) != NULL_TREE
aa3e402e 1452 && INDIRECT_REF_P (op1)
737a23cc 1453 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
7549df0d 1454 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
737a23cc 1455 else if (op0 != orig_op0 || in_init)
a75b1c71 1456 ret = in_init
389dd41b 1457 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1458 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
a75b1c71 1459 else
1460 ret = fold (expr);
1461 if (code == INDIRECT_REF
1462 && ret != expr
aa3e402e 1463 && INDIRECT_REF_P (ret))
a75b1c71 1464 {
1465 TREE_READONLY (ret) = TREE_READONLY (expr);
1466 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1467 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1468 }
672d914b 1469 switch (code)
1470 {
1471 case FIX_TRUNC_EXPR:
1472 case FLOAT_EXPR:
1473 CASE_CONVERT:
1474 /* Don't warn about explicit conversions. We will already
1475 have warned about suspect implicit conversions. */
1476 break;
1477
1478 default:
1479 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1480 overflow_warning (EXPR_LOCATION (expr), ret);
1481 break;
1482 }
a75b1c71 1483 goto out;
1484
1485 case TRUTH_ANDIF_EXPR:
1486 case TRUTH_ORIF_EXPR:
1487 /* Binary operations not necessarily evaluating both
1488 arguments. */
1489 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1490 orig_op1 = op1 = TREE_OPERAND (expr, 1);
aac24642 1491 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1492 for_int_const);
f59e3889 1493 STRIP_TYPE_NOPS (op0);
672d914b 1494
1495 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1496 ? truthvalue_false_node
1497 : truthvalue_true_node));
fc501191 1498 c_disable_warnings (unused_p);
aac24642 1499 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1500 for_int_const);
f59e3889 1501 STRIP_TYPE_NOPS (op1);
fc501191 1502 c_enable_warnings (unused_p);
672d914b 1503
aac24642 1504 if (for_int_const
1505 && (TREE_CODE (op0) != INTEGER_CST
1506 /* Require OP1 be an INTEGER_CST only if it's evaluated. */
1507 || (!unused_p && TREE_CODE (op1) != INTEGER_CST)))
1508 goto out;
1509
a75b1c71 1510 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1511 ret = in_init
389dd41b 1512 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1513 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
a75b1c71 1514 else
1515 ret = fold (expr);
1516 *maybe_const_operands &= op0_const;
1517 *maybe_const_itself &= op0_const_self;
1518 if (!(flag_isoc99
1519 && op0_const
1520 && op0_const_self
1521 && (code == TRUTH_ANDIF_EXPR
1522 ? op0 == truthvalue_false_node
1523 : op0 == truthvalue_true_node)))
1524 *maybe_const_operands &= op1_const;
1525 if (!(op0_const
1526 && op0_const_self
1527 && (code == TRUTH_ANDIF_EXPR
1528 ? op0 == truthvalue_false_node
1529 : op0 == truthvalue_true_node)))
1530 *maybe_const_itself &= op1_const_self;
1531 goto out;
1532
1533 case COND_EXPR:
1534 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1535 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1536 orig_op2 = op2 = TREE_OPERAND (expr, 2);
aac24642 1537 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1538 for_int_const);
672d914b 1539
f59e3889 1540 STRIP_TYPE_NOPS (op0);
fc501191 1541 c_disable_warnings (op0 == truthvalue_false_node);
aac24642 1542 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1543 for_int_const);
f59e3889 1544 STRIP_TYPE_NOPS (op1);
fc501191 1545 c_enable_warnings (op0 == truthvalue_false_node);
672d914b 1546
fc501191 1547 c_disable_warnings (op0 == truthvalue_true_node);
aac24642 1548 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self,
1549 for_int_const);
f59e3889 1550 STRIP_TYPE_NOPS (op2);
fc501191 1551 c_enable_warnings (op0 == truthvalue_true_node);
672d914b 1552
aac24642 1553 if (for_int_const
1554 && (TREE_CODE (op0) != INTEGER_CST
1555 /* Only the evaluated operand must be an INTEGER_CST. */
1556 || (op0 == truthvalue_true_node
1557 ? TREE_CODE (op1) != INTEGER_CST
1558 : TREE_CODE (op2) != INTEGER_CST)))
1559 goto out;
1560
a75b1c71 1561 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
389dd41b 1562 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
a75b1c71 1563 else
1564 ret = fold (expr);
1565 *maybe_const_operands &= op0_const;
1566 *maybe_const_itself &= op0_const_self;
1567 if (!(flag_isoc99
1568 && op0_const
1569 && op0_const_self
1570 && op0 == truthvalue_false_node))
1571 *maybe_const_operands &= op1_const;
1572 if (!(op0_const
1573 && op0_const_self
1574 && op0 == truthvalue_false_node))
1575 *maybe_const_itself &= op1_const_self;
1576 if (!(flag_isoc99
1577 && op0_const
1578 && op0_const_self
1579 && op0 == truthvalue_true_node))
1580 *maybe_const_operands &= op2_const;
1581 if (!(op0_const
1582 && op0_const_self
1583 && op0 == truthvalue_true_node))
1584 *maybe_const_itself &= op2_const_self;
1585 goto out;
1586
c6418a4e 1587 case EXCESS_PRECISION_EXPR:
1588 /* Each case where an operand with excess precision may be
1589 encountered must remove the EXCESS_PRECISION_EXPR around
1590 inner operands and possibly put one around the whole
1591 expression or possibly convert to the semantic type (which
1592 c_fully_fold does); we cannot tell at this stage which is
1593 appropriate in any particular case. */
1594 gcc_unreachable ();
1595
a75b1c71 1596 default:
1597 /* Various codes may appear through folding built-in functions
1598 and their arguments. */
1599 goto out;
1600 }
1601
1602 out:
1603 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1604 have been done by this point, so remove them again. */
1605 nowarning |= TREE_NO_WARNING (ret);
1606 STRIP_TYPE_NOPS (ret);
1607 if (nowarning && !TREE_NO_WARNING (ret))
1608 {
1609 if (!CAN_HAVE_LOCATION_P (ret))
1610 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1611 TREE_NO_WARNING (ret) = 1;
1612 }
1613 if (ret != expr)
1614 protected_set_expr_location (ret, loc);
1615 return ret;
1616}
1617
1618/* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1619 return EXP. Otherwise, return either EXP or its known constant
1620 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1621 Is the BLKmode test appropriate? */
1622
1623tree
1624decl_constant_value_for_optimization (tree exp)
1625{
1626 tree ret;
1627
1628 /* This function is only used by C, for c_fully_fold and other
1629 optimization, and may not be correct for C++. */
1630 if (c_dialect_cxx ())
1631 gcc_unreachable ();
1632
1633 if (!optimize
f48c7f4a 1634 || !VAR_P (exp)
a75b1c71 1635 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1636 || DECL_MODE (exp) == BLKmode)
1637 return exp;
1638
1639 ret = decl_constant_value (exp);
1640 /* Avoid unwanted tree sharing between the initializer and current
1641 function's body where the tree can be modified e.g. by the
1642 gimplifier. */
1643 if (ret != exp && TREE_STATIC (exp))
1644 ret = unshare_expr (ret);
1645 return ret;
1646}
1647
2a1736ed 1648/* Print a warning if a constant expression had overflow in folding.
1649 Invoke this function on every expression that the language
1650 requires to be a constant expression.
1651 Note the ANSI C standard says it is erroneous for a
1652 constant expression to overflow. */
b2806639 1653
1654void
1cae46be 1655constant_expression_warning (tree value)
07317e69 1656{
48e1416a 1657 if (warn_overflow && pedantic
07317e69 1658 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1659 || TREE_CODE (value) == FIXED_CST
1660 || TREE_CODE (value) == VECTOR_CST
1661 || TREE_CODE (value) == COMPLEX_CST)
1662 && TREE_OVERFLOW (value))
21ca8540 1663 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
07317e69 1664}
1665
1666/* The same as above but print an unconditional error. */
1667void
1668constant_expression_error (tree value)
b2806639 1669{
837e1122 1670 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
9421ebb9 1671 || TREE_CODE (value) == FIXED_CST
886cfd4f 1672 || TREE_CODE (value) == VECTOR_CST
837e1122 1673 || TREE_CODE (value) == COMPLEX_CST)
07317e69 1674 && TREE_OVERFLOW (value))
1675 error ("overflow in constant expression");
2a1736ed 1676}
1677
f170d67f 1678/* Print a warning if an expression had overflow in folding and its
1679 operands hadn't.
1680
2a1736ed 1681 Invoke this function on every expression that
1682 (1) appears in the source code, and
f170d67f 1683 (2) is a constant expression that overflowed, and
2a1736ed 1684 (3) is not already checked by convert_and_check;
f170d67f 1685 however, do not invoke this function on operands of explicit casts
1686 or when the expression is the result of an operator and any operand
1687 already overflowed. */
2a1736ed 1688
1689void
e60a6f7b 1690overflow_warning (location_t loc, tree value)
2a1736ed 1691{
48d94ede 1692 if (c_inhibit_evaluation_warnings != 0)
1693 return;
f170d67f 1694
1695 switch (TREE_CODE (value))
886cfd4f 1696 {
f170d67f 1697 case INTEGER_CST:
e60a6f7b 1698 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
f170d67f 1699 break;
48e1416a 1700
f170d67f 1701 case REAL_CST:
e60a6f7b 1702 warning_at (loc, OPT_Woverflow,
1703 "floating point overflow in expression");
f170d67f 1704 break;
48e1416a 1705
9421ebb9 1706 case FIXED_CST:
e60a6f7b 1707 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
9421ebb9 1708 break;
1709
f170d67f 1710 case VECTOR_CST:
e60a6f7b 1711 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
f170d67f 1712 break;
48e1416a 1713
f170d67f 1714 case COMPLEX_CST:
1715 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
e60a6f7b 1716 warning_at (loc, OPT_Woverflow,
1717 "complex integer overflow in expression");
f170d67f 1718 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
e60a6f7b 1719 warning_at (loc, OPT_Woverflow,
1720 "complex floating point overflow in expression");
f170d67f 1721 break;
1722
1723 default:
1724 break;
886cfd4f 1725 }
2a1736ed 1726}
1727
03033af4 1728/* Warn about uses of logical || / && operator in a context where it
1729 is likely that the bitwise equivalent was intended by the
1730 programmer. We have seen an expression in which CODE is a binary
9c20c4fc 1731 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1732 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
b13d1547 1733void
9c20c4fc 1734warn_logical_operator (location_t location, enum tree_code code, tree type,
48e1416a 1735 enum tree_code code_left, tree op_left,
03033af4 1736 enum tree_code ARG_UNUSED (code_right), tree op_right)
b13d1547 1737{
9c20c4fc 1738 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1739 int in0_p, in1_p, in_p;
1740 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1741 bool strict_overflow_p = false;
1742
03033af4 1743 if (code != TRUTH_ANDIF_EXPR
1744 && code != TRUTH_AND_EXPR
1745 && code != TRUTH_ORIF_EXPR
1746 && code != TRUTH_OR_EXPR)
1747 return;
1748
439606a9 1749 /* We don't want to warn if either operand comes from a macro
1750 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1751 see PR61534. */
1752 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1753 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1754 return;
1755
03033af4 1756 /* Warn if &&/|| are being used in a context where it is
1757 likely that the bitwise equivalent was intended by the
1758 programmer. That is, an expression such as op && MASK
1759 where op should not be any boolean expression, nor a
1760 constant, and mask seems to be a non-boolean integer constant. */
1761 if (!truth_value_p (code_left)
1762 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1763 && !CONSTANT_CLASS_P (op_left)
1764 && !TREE_NO_WARNING (op_left)
1765 && TREE_CODE (op_right) == INTEGER_CST
1766 && !integer_zerop (op_right)
1767 && !integer_onep (op_right))
b13d1547 1768 {
9c20c4fc 1769 if (or_op)
03033af4 1770 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1771 " applied to non-boolean constant");
1772 else
1773 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1774 " applied to non-boolean constant");
1775 TREE_NO_WARNING (op_left) = true;
9c20c4fc 1776 return;
1777 }
1778
1779 /* We do not warn for constants because they are typical of macro
1780 expansions that test for features. */
1781 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1782 return;
1783
1784 /* This warning only makes sense with logical operands. */
1785 if (!(truth_value_p (TREE_CODE (op_left))
1786 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1787 || !(truth_value_p (TREE_CODE (op_right))
1788 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1789 return;
1790
5e84569c 1791 /* The range computations only work with scalars. */
1792 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1793 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1794 return;
9c20c4fc 1795
686369e8 1796 /* We first test whether either side separately is trivially true
1797 (with OR) or trivially false (with AND). If so, do not warn.
1798 This is a common idiom for testing ranges of data types in
1799 portable code. */
1800 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1801 if (!lhs)
1802 return;
1803 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
9c20c4fc 1804 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1805
686369e8 1806 /* If this is an OR operation, invert both sides; now, the result
1807 should be always false to get a warning. */
1808 if (or_op)
1809 in0_p = !in0_p;
1810
1811 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
d42e7c5a 1812 if (tem && integer_zerop (tem))
686369e8 1813 return;
1814
1815 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1816 if (!rhs)
1817 return;
1818 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
9c20c4fc 1819 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
48e1416a 1820
686369e8 1821 /* If this is an OR operation, invert both sides; now, the result
1822 should be always false to get a warning. */
9c20c4fc 1823 if (or_op)
686369e8 1824 in1_p = !in1_p;
48e1416a 1825
686369e8 1826 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
d42e7c5a 1827 if (tem && integer_zerop (tem))
686369e8 1828 return;
1829
1830 /* If both expressions have the same operand, if we can merge the
485f6b9c 1831 ranges, ... */
686369e8 1832 if (operand_equal_p (lhs, rhs, 0)
9c20c4fc 1833 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
485f6b9c 1834 in1_p, low1, high1))
9c20c4fc 1835 {
485f6b9c 1836 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1837 /* ... and if the range test is always false, then warn. */
1838 if (tem && integer_zerop (tem))
1839 {
1840 if (or_op)
1841 warning_at (location, OPT_Wlogical_op,
1842 "logical %<or%> of collectively exhaustive tests is "
1843 "always true");
1844 else
1845 warning_at (location, OPT_Wlogical_op,
1846 "logical %<and%> of mutually exclusive tests is "
1847 "always false");
1848 }
1849 /* Or warn if the operands have exactly the same range, e.g.
1850 A > 0 && A > 0. */
0200602e 1851 else if (tree_int_cst_equal (low0, low1)
1852 && tree_int_cst_equal (high0, high1))
485f6b9c 1853 {
1854 if (or_op)
1855 warning_at (location, OPT_Wlogical_op,
1856 "logical %<or%> of equal expressions");
1857 else
1858 warning_at (location, OPT_Wlogical_op,
1859 "logical %<and%> of equal expressions");
1860 }
b13d1547 1861 }
1862}
1863
6784a472 1864/* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
1865 with constant indices. */
1866
1867static tree
1868find_array_ref_with_const_idx_r (tree *expr_p, int *walk_subtrees, void *data)
1869{
1870 tree expr = *expr_p;
1871
1872 if ((TREE_CODE (expr) == ARRAY_REF
1873 || TREE_CODE (expr) == ARRAY_RANGE_REF)
1874 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
1875 {
1876 *(bool *) data = true;
1877 *walk_subtrees = 0;
1878 }
1879
1880 return NULL_TREE;
1881}
1882
1883/* Warn if a self-comparison always evaluates to true or false. LOC
1884 is the location of the comparison with code CODE, LHS and RHS are
1885 operands of the comparison. */
1886
1887void
1888warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
1889{
1890 if (TREE_CODE_CLASS (code) != tcc_comparison)
1891 return;
1892
d9e4fe27 1893 /* Don't warn for various macro expansions. */
1894 if (from_macro_expansion_at (loc)
1895 || from_macro_expansion_at (EXPR_LOCATION (lhs))
1896 || from_macro_expansion_at (EXPR_LOCATION (rhs)))
1897 return;
1898
6784a472 1899 /* We do not warn for constants because they are typical of macro
1900 expansions that test for features, sizeof, and similar. */
1901 if (CONSTANT_CLASS_P (lhs) || CONSTANT_CLASS_P (rhs))
1902 return;
1903
1904 /* Don't warn for e.g.
1905 HOST_WIDE_INT n;
1906 ...
1907 if (n == (long) n) ...
1908 */
1909 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
1910 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
1911 return;
1912
f58dd84b 1913 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
1914 It could be a NaN, and NaN never compares equal to anything, even
1915 itself. */
1916 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
1917 return;
1918
6784a472 1919 if (operand_equal_p (lhs, rhs, 0))
1920 {
1921 /* Don't warn about array references with constant indices;
1922 these are likely to come from a macro. */
1923 bool found = false;
1924 walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
1925 &found);
1926 if (found)
1927 return;
1928 const bool always_true = (code == EQ_EXPR || code == LE_EXPR
1929 || code == GE_EXPR || code == UNLE_EXPR
1930 || code == UNGE_EXPR || code == UNEQ_EXPR);
1931 if (always_true)
1932 warning_at (loc, OPT_Wtautological_compare,
1933 "self-comparison always evaluates to true");
1934 else
1935 warning_at (loc, OPT_Wtautological_compare,
1936 "self-comparison always evaluates to false");
1937 }
1938}
1939
32dc1512 1940/* Warn about logical not used on the left hand side operand of a comparison.
1941 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
dc6229e8 1942 Do not warn if RHS is of a boolean type. */
32dc1512 1943
1944void
1945warn_logical_not_parentheses (location_t location, enum tree_code code,
dc6229e8 1946 tree rhs)
32dc1512 1947{
dc6229e8 1948 if (TREE_CODE_CLASS (code) != tcc_comparison
1949 || TREE_TYPE (rhs) == NULL_TREE
1950 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
32dc1512 1951 return;
1952
16f958b3 1953 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1954 !(x == 0) or !(y != 0). */
1955 if ((code == EQ_EXPR || code == NE_EXPR)
1956 && integer_zerop (rhs))
1957 return;
1958
32dc1512 1959 warning_at (location, OPT_Wlogical_not_parentheses,
1960 "logical not is only applied to the left hand side of "
1961 "comparison");
1962}
b13d1547 1963
3d177e8c 1964/* Warn if EXP contains any computations whose results are not used.
1965 Return true if a warning is printed; false otherwise. LOCUS is the
1966 (potential) location of the expression. */
1967
1968bool
1969warn_if_unused_value (const_tree exp, location_t locus)
1970{
1971 restart:
1972 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1973 return false;
1974
1975 /* Don't warn about void constructs. This includes casting to void,
1976 void function calls, and statement expressions with a final cast
1977 to void. */
1978 if (VOID_TYPE_P (TREE_TYPE (exp)))
1979 return false;
1980
1981 if (EXPR_HAS_LOCATION (exp))
1982 locus = EXPR_LOCATION (exp);
1983
1984 switch (TREE_CODE (exp))
1985 {
1986 case PREINCREMENT_EXPR:
1987 case POSTINCREMENT_EXPR:
1988 case PREDECREMENT_EXPR:
1989 case POSTDECREMENT_EXPR:
1990 case MODIFY_EXPR:
1991 case INIT_EXPR:
1992 case TARGET_EXPR:
1993 case CALL_EXPR:
1994 case TRY_CATCH_EXPR:
1995 case WITH_CLEANUP_EXPR:
1996 case EXIT_EXPR:
1997 case VA_ARG_EXPR:
1998 return false;
1999
2000 case BIND_EXPR:
2001 /* For a binding, warn if no side effect within it. */
2002 exp = BIND_EXPR_BODY (exp);
2003 goto restart;
2004
2005 case SAVE_EXPR:
2006 case NON_LVALUE_EXPR:
d85dbdb3 2007 case NOP_EXPR:
3d177e8c 2008 exp = TREE_OPERAND (exp, 0);
2009 goto restart;
2010
2011 case TRUTH_ORIF_EXPR:
2012 case TRUTH_ANDIF_EXPR:
2013 /* In && or ||, warn if 2nd operand has no side effect. */
2014 exp = TREE_OPERAND (exp, 1);
2015 goto restart;
2016
2017 case COMPOUND_EXPR:
2018 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
2019 return true;
2020 /* Let people do `(foo (), 0)' without a warning. */
2021 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
2022 return false;
2023 exp = TREE_OPERAND (exp, 1);
2024 goto restart;
2025
2026 case COND_EXPR:
2027 /* If this is an expression with side effects, don't warn; this
2028 case commonly appears in macro expansions. */
2029 if (TREE_SIDE_EFFECTS (exp))
2030 return false;
2031 goto warn;
2032
2033 case INDIRECT_REF:
2034 /* Don't warn about automatic dereferencing of references, since
2035 the user cannot control it. */
2036 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
2037 {
2038 exp = TREE_OPERAND (exp, 0);
2039 goto restart;
2040 }
2041 /* Fall through. */
2042
2043 default:
2044 /* Referencing a volatile value is a side effect, so don't warn. */
2045 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
2046 && TREE_THIS_VOLATILE (exp))
2047 return false;
2048
2049 /* If this is an expression which has no operands, there is no value
2050 to be unused. There are no such language-independent codes,
2051 but front ends may define such. */
2052 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
2053 return false;
2054
2055 warn:
2056 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
2057 }
2058}
2059
2060
bcf22371 2061/* Print a warning about casts that might indicate violation
2062 of strict aliasing rules if -Wstrict-aliasing is used and
1e31ff37 2063 strict aliasing mode is in effect. OTYPE is the original
2064 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
bcf22371 2065
e6fa0ea6 2066bool
1e31ff37 2067strict_aliasing_warning (tree otype, tree type, tree expr)
bcf22371 2068{
45bb3afb 2069 /* Strip pointer conversion chains and get to the correct original type. */
2070 STRIP_NOPS (expr);
2071 otype = TREE_TYPE (expr);
2072
f06537f2 2073 if (!(flag_strict_aliasing
2074 && POINTER_TYPE_P (type)
2075 && POINTER_TYPE_P (otype)
2076 && !VOID_TYPE_P (TREE_TYPE (type)))
2077 /* If the type we are casting to is a ref-all pointer
2078 dereferencing it is always valid. */
2079 || TYPE_REF_CAN_ALIAS_ALL (type))
e6fa0ea6 2080 return false;
2081
2082 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
bcf22371 2083 && (DECL_P (TREE_OPERAND (expr, 0))
e6fa0ea6 2084 || handled_component_p (TREE_OPERAND (expr, 0))))
bcf22371 2085 {
2086 /* Casting the address of an object to non void pointer. Warn
2087 if the cast breaks type based aliasing. */
e6fa0ea6 2088 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
2089 {
2090 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
2091 "might break strict-aliasing rules");
2092 return true;
2093 }
bcf22371 2094 else
2095 {
48e1416a 2096 /* warn_strict_aliasing >= 3. This includes the default (3).
e6fa0ea6 2097 Only warn if the cast is dereferenced immediately. */
32c2fdea 2098 alias_set_type set1 =
e6fa0ea6 2099 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
32c2fdea 2100 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
bcf22371 2101
62d823d0 2102 if (set1 != set2 && set2 != 0
2103 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
e6fa0ea6 2104 {
2105 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2106 "pointer will break strict-aliasing rules");
2107 return true;
2108 }
2109 else if (warn_strict_aliasing == 2
879f881c 2110 && !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 2111 {
2112 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2113 "pointer might break strict-aliasing rules");
2114 return true;
2115 }
bcf22371 2116 }
2117 }
e6fa0ea6 2118 else
2119 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
2120 {
2121 /* At this level, warn for any conversions, even if an address is
2122 not taken in the same statement. This will likely produce many
2123 false positives, but could be useful to pinpoint problems that
2124 are not revealed at higher levels. */
32c2fdea 2125 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
2126 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
2127 if (!COMPLETE_TYPE_P (type)
879f881c 2128 || !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 2129 {
2130 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2131 "pointer might break strict-aliasing rules");
2132 return true;
2133 }
2134 }
2135
2136 return false;
bcf22371 2137}
2138
f003f5dc 2139/* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
2140 sizeof as last operand of certain builtins. */
2141
2142void
57f872a2 2143sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
f1f41a6c 2144 vec<tree, va_gc> *params, tree *sizeof_arg,
f003f5dc 2145 bool (*comp_types) (tree, tree))
2146{
2147 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
57f872a2 2148 bool strop = false, cmp = false;
2149 unsigned int idx = ~0;
2150 location_t loc;
f003f5dc 2151
2152 if (TREE_CODE (callee) != FUNCTION_DECL
2153 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
f1f41a6c 2154 || vec_safe_length (params) <= 1)
f003f5dc 2155 return;
2156
f003f5dc 2157 switch (DECL_FUNCTION_CODE (callee))
2158 {
2159 case BUILT_IN_STRNCMP:
2160 case BUILT_IN_STRNCASECMP:
57f872a2 2161 cmp = true;
2162 /* FALLTHRU */
f003f5dc 2163 case BUILT_IN_STRNCPY:
57f872a2 2164 case BUILT_IN_STRNCPY_CHK:
f003f5dc 2165 case BUILT_IN_STRNCAT:
57f872a2 2166 case BUILT_IN_STRNCAT_CHK:
2167 case BUILT_IN_STPNCPY:
2168 case BUILT_IN_STPNCPY_CHK:
f003f5dc 2169 strop = true;
2170 /* FALLTHRU */
2171 case BUILT_IN_MEMCPY:
57f872a2 2172 case BUILT_IN_MEMCPY_CHK:
f003f5dc 2173 case BUILT_IN_MEMMOVE:
57f872a2 2174 case BUILT_IN_MEMMOVE_CHK:
f1f41a6c 2175 if (params->length () < 3)
57f872a2 2176 return;
f1f41a6c 2177 src = (*params)[1];
2178 dest = (*params)[0];
57f872a2 2179 idx = 2;
2180 break;
2181 case BUILT_IN_BCOPY:
f1f41a6c 2182 if (params->length () < 3)
57f872a2 2183 return;
f1f41a6c 2184 src = (*params)[0];
2185 dest = (*params)[1];
57f872a2 2186 idx = 2;
2187 break;
f003f5dc 2188 case BUILT_IN_MEMCMP:
57f872a2 2189 case BUILT_IN_BCMP:
f1f41a6c 2190 if (params->length () < 3)
f003f5dc 2191 return;
f1f41a6c 2192 src = (*params)[1];
2193 dest = (*params)[0];
57f872a2 2194 idx = 2;
2195 cmp = true;
f003f5dc 2196 break;
2197 case BUILT_IN_MEMSET:
57f872a2 2198 case BUILT_IN_MEMSET_CHK:
f1f41a6c 2199 if (params->length () < 3)
f003f5dc 2200 return;
f1f41a6c 2201 dest = (*params)[0];
57f872a2 2202 idx = 2;
2203 break;
2204 case BUILT_IN_BZERO:
f1f41a6c 2205 dest = (*params)[0];
57f872a2 2206 idx = 1;
f003f5dc 2207 break;
2208 case BUILT_IN_STRNDUP:
f1f41a6c 2209 src = (*params)[0];
f003f5dc 2210 strop = true;
57f872a2 2211 idx = 1;
2212 break;
2213 case BUILT_IN_MEMCHR:
f1f41a6c 2214 if (params->length () < 3)
57f872a2 2215 return;
f1f41a6c 2216 src = (*params)[0];
57f872a2 2217 idx = 2;
2218 break;
2219 case BUILT_IN_SNPRINTF:
2220 case BUILT_IN_SNPRINTF_CHK:
2221 case BUILT_IN_VSNPRINTF:
2222 case BUILT_IN_VSNPRINTF_CHK:
f1f41a6c 2223 dest = (*params)[0];
57f872a2 2224 idx = 1;
2225 strop = true;
f003f5dc 2226 break;
2227 default:
2228 break;
2229 }
2230
57f872a2 2231 if (idx >= 3)
2232 return;
2233
2234 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2235 return;
2236
2237 type = TYPE_P (sizeof_arg[idx])
2238 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2239 if (!POINTER_TYPE_P (type))
2240 return;
2241
f003f5dc 2242 if (dest
2243 && (tem = tree_strip_nop_conversions (dest))
2244 && POINTER_TYPE_P (TREE_TYPE (tem))
2245 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2246 return;
2247
2248 if (src
2249 && (tem = tree_strip_nop_conversions (src))
2250 && POINTER_TYPE_P (TREE_TYPE (tem))
2251 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2252 return;
2253
57f872a2 2254 loc = sizeof_arg_loc[idx];
2255
2256 if (dest && !cmp)
f003f5dc 2257 {
57f872a2 2258 if (!TYPE_P (sizeof_arg[idx])
2259 && operand_equal_p (dest, sizeof_arg[idx], 0)
f003f5dc 2260 && comp_types (TREE_TYPE (dest), type))
2261 {
57f872a2 2262 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
f003f5dc 2263 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2264 "argument to %<sizeof%> in %qD call is the same "
2265 "expression as the destination; did you mean to "
2266 "remove the addressof?", callee);
2267 else if ((TYPE_PRECISION (TREE_TYPE (type))
2268 == TYPE_PRECISION (char_type_node))
2269 || strop)
2270 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2271 "argument to %<sizeof%> in %qD call is the same "
2272 "expression as the destination; did you mean to "
2273 "provide an explicit length?", callee);
2274 else
2275 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2276 "argument to %<sizeof%> in %qD call is the same "
2277 "expression as the destination; did you mean to "
2278 "dereference it?", callee);
2279 return;
2280 }
2281
2282 if (POINTER_TYPE_P (TREE_TYPE (dest))
2283 && !strop
2284 && comp_types (TREE_TYPE (dest), type)
2285 && !VOID_TYPE_P (TREE_TYPE (type)))
2286 {
2287 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2288 "argument to %<sizeof%> in %qD call is the same "
2289 "pointer type %qT as the destination; expected %qT "
2290 "or an explicit length", callee, TREE_TYPE (dest),
2291 TREE_TYPE (TREE_TYPE (dest)));
2292 return;
2293 }
2294 }
2295
57f872a2 2296 if (src && !cmp)
f003f5dc 2297 {
57f872a2 2298 if (!TYPE_P (sizeof_arg[idx])
2299 && operand_equal_p (src, sizeof_arg[idx], 0)
f003f5dc 2300 && comp_types (TREE_TYPE (src), type))
2301 {
57f872a2 2302 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
f003f5dc 2303 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2304 "argument to %<sizeof%> in %qD call is the same "
2305 "expression as the source; did you mean to "
2306 "remove the addressof?", callee);
2307 else if ((TYPE_PRECISION (TREE_TYPE (type))
2308 == TYPE_PRECISION (char_type_node))
2309 || strop)
2310 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2311 "argument to %<sizeof%> in %qD call is the same "
2312 "expression as the source; did you mean to "
2313 "provide an explicit length?", callee);
2314 else
2315 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2316 "argument to %<sizeof%> in %qD call is the same "
2317 "expression as the source; did you mean to "
2318 "dereference it?", callee);
2319 return;
2320 }
2321
2322 if (POINTER_TYPE_P (TREE_TYPE (src))
2323 && !strop
2324 && comp_types (TREE_TYPE (src), type)
2325 && !VOID_TYPE_P (TREE_TYPE (type)))
2326 {
2327 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2328 "argument to %<sizeof%> in %qD call is the same "
2329 "pointer type %qT as the source; expected %qT "
2330 "or an explicit length", callee, TREE_TYPE (src),
2331 TREE_TYPE (TREE_TYPE (src)));
2332 return;
2333 }
2334 }
57f872a2 2335
2336 if (dest)
2337 {
2338 if (!TYPE_P (sizeof_arg[idx])
2339 && operand_equal_p (dest, sizeof_arg[idx], 0)
2340 && comp_types (TREE_TYPE (dest), type))
2341 {
2342 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2343 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2344 "argument to %<sizeof%> in %qD call is the same "
2345 "expression as the first source; did you mean to "
2346 "remove the addressof?", callee);
2347 else if ((TYPE_PRECISION (TREE_TYPE (type))
2348 == TYPE_PRECISION (char_type_node))
2349 || strop)
2350 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2351 "argument to %<sizeof%> in %qD call is the same "
2352 "expression as the first source; did you mean to "
2353 "provide an explicit length?", callee);
2354 else
2355 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2356 "argument to %<sizeof%> in %qD call is the same "
2357 "expression as the first source; did you mean to "
2358 "dereference it?", callee);
2359 return;
2360 }
2361
2362 if (POINTER_TYPE_P (TREE_TYPE (dest))
2363 && !strop
2364 && comp_types (TREE_TYPE (dest), type)
2365 && !VOID_TYPE_P (TREE_TYPE (type)))
2366 {
2367 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2368 "argument to %<sizeof%> in %qD call is the same "
2369 "pointer type %qT as the first source; expected %qT "
2370 "or an explicit length", callee, TREE_TYPE (dest),
2371 TREE_TYPE (TREE_TYPE (dest)));
2372 return;
2373 }
2374 }
2375
2376 if (src)
2377 {
2378 if (!TYPE_P (sizeof_arg[idx])
2379 && operand_equal_p (src, sizeof_arg[idx], 0)
2380 && comp_types (TREE_TYPE (src), type))
2381 {
2382 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2383 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2384 "argument to %<sizeof%> in %qD call is the same "
2385 "expression as the second source; did you mean to "
2386 "remove the addressof?", callee);
2387 else if ((TYPE_PRECISION (TREE_TYPE (type))
2388 == TYPE_PRECISION (char_type_node))
2389 || strop)
2390 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2391 "argument to %<sizeof%> in %qD call is the same "
2392 "expression as the second source; did you mean to "
2393 "provide an explicit length?", callee);
2394 else
2395 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2396 "argument to %<sizeof%> in %qD call is the same "
2397 "expression as the second source; did you mean to "
2398 "dereference it?", callee);
2399 return;
2400 }
2401
2402 if (POINTER_TYPE_P (TREE_TYPE (src))
2403 && !strop
2404 && comp_types (TREE_TYPE (src), type)
2405 && !VOID_TYPE_P (TREE_TYPE (type)))
2406 {
2407 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2408 "argument to %<sizeof%> in %qD call is the same "
2409 "pointer type %qT as the second source; expected %qT "
2410 "or an explicit length", callee, TREE_TYPE (src),
2411 TREE_TYPE (TREE_TYPE (src)));
2412 return;
2413 }
2414 }
2415
f003f5dc 2416}
2417
3f08e399 2418/* Warn for unlikely, improbable, or stupid DECL declarations
2419 of `main'. */
2420
2421void
2422check_main_parameter_types (tree decl)
2423{
d0af78c5 2424 function_args_iterator iter;
2425 tree type;
3f08e399 2426 int argct = 0;
2427
d0af78c5 2428 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2429 {
2430 /* XXX void_type_node belies the abstraction. */
2431 if (type == void_type_node || type == error_mark_node )
2432 break;
2433
2026249a 2434 tree t = type;
2435 if (TYPE_ATOMIC (t))
2436 pedwarn (input_location, OPT_Wmain,
2437 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2438 type, decl);
2439 while (POINTER_TYPE_P (t))
2440 {
2441 t = TREE_TYPE (t);
2442 if (TYPE_ATOMIC (t))
2443 pedwarn (input_location, OPT_Wmain,
2444 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2445 type, decl);
2446 }
2447
d0af78c5 2448 ++argct;
2449 switch (argct)
2450 {
2451 case 1:
2452 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2453 pedwarn (input_location, OPT_Wmain,
2454 "first argument of %q+D should be %<int%>", decl);
2455 break;
2456
2457 case 2:
2458 if (TREE_CODE (type) != POINTER_TYPE
2459 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2460 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2461 != char_type_node))
2462 pedwarn (input_location, OPT_Wmain,
2463 "second argument of %q+D should be %<char **%>", decl);
2464 break;
2465
2466 case 3:
2467 if (TREE_CODE (type) != POINTER_TYPE
2468 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2469 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2470 != char_type_node))
2471 pedwarn (input_location, OPT_Wmain,
2472 "third argument of %q+D should probably be "
2473 "%<char **%>", decl);
2474 break;
2475 }
2476 }
3f08e399 2477
2478 /* It is intentional that this message does not mention the third
2479 argument because it's only mentioned in an appendix of the
2480 standard. */
2481 if (argct > 0 && (argct < 2 || argct > 3))
d0af78c5 2482 pedwarn (input_location, OPT_Wmain,
2483 "%q+D takes only zero or two arguments", decl);
90e645fa 2484
2485 if (stdarg_p (TREE_TYPE (decl)))
2486 pedwarn (input_location, OPT_Wmain,
2487 "%q+D declared as variadic function", decl);
3f08e399 2488}
2489
73437615 2490/* vector_targets_convertible_p is used for vector pointer types. The
2491 callers perform various checks that the qualifiers are satisfactory,
2492 while OTOH vector_targets_convertible_p ignores the number of elements
2493 in the vectors. That's fine with vector pointers as we can consider,
2494 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2495 and that does not require and conversion of the pointer values.
2496 In contrast, vector_types_convertible_p and
2497 vector_types_compatible_elements_p are used for vector value types. */
ed7c4e62 2498/* True if pointers to distinct types T1 and T2 can be converted to
2499 each other without an explicit cast. Only returns true for opaque
2500 vector types. */
2501bool
2502vector_targets_convertible_p (const_tree t1, const_tree t2)
2503{
6290f0db 2504 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
8d125f7d 2505 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ed7c4e62 2506 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2507 return true;
2508
2509 return false;
2510}
2511
73437615 2512/* vector_types_convertible_p is used for vector value types.
2513 It could in principle call vector_targets_convertible_p as a subroutine,
2514 but then the check for vector type would be duplicated with its callers,
2515 and also the purpose of vector_targets_convertible_p would become
2516 muddled.
2517 Where vector_types_convertible_p returns true, a conversion might still be
2518 needed to make the types match.
2519 In contrast, vector_targets_convertible_p is used for vector pointer
2520 values, and vector_types_compatible_elements_p is used specifically
2521 in the context for binary operators, as a check if use is possible without
2522 conversion. */
546c4794 2523/* True if vector types T1 and T2 can be converted to each other
2524 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2525 can only be converted with -flax-vector-conversions yet that is not
2526 in effect, emit a note telling the user about that option if such
2527 a note has not previously been emitted. */
2528bool
9f627b1a 2529vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
8b4b9810 2530{
546c4794 2531 static bool emitted_lax_note = false;
ae6db8ab 2532 bool convertible_lax;
2533
8d125f7d 2534 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ae6db8ab 2535 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2536 return true;
2537
2538 convertible_lax =
2539 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2540 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
432dd330 2541 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
ae6db8ab 2542 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2543 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
546c4794 2544
2545 if (!convertible_lax || flag_lax_vector_conversions)
2546 return convertible_lax;
2547
2548 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
29f51994 2549 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
546c4794 2550 return true;
2551
2552 if (emit_lax_note && !emitted_lax_note)
2553 {
2554 emitted_lax_note = true;
5bcc316e 2555 inform (input_location, "use -flax-vector-conversions to permit "
546c4794 2556 "conversions between vectors with differing "
2557 "element types or numbers of subparts");
2558 }
2559
2560 return false;
8b4b9810 2561}
2562
bf0cb017 2563/* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2564 and have vector types, V0 has the same type as V1, and the number of
2565 elements of V0, V1, MASK is the same.
2566
2567 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2568 called with two arguments. In this case implementation passes the
2569 first argument twice in order to share the same tree code. This fact
2570 could enable the mask-values being twice the vector length. This is
2571 an implementation accident and this semantics is not guaranteed to
2572 the user. */
2573tree
68ea4406 2574c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2575 bool complain)
bf0cb017 2576{
2577 tree ret;
2578 bool wrap = true;
2579 bool maybe_const = false;
2580 bool two_arguments = false;
2581
2582 if (v1 == NULL_TREE)
2583 {
2584 two_arguments = true;
2585 v1 = v0;
2586 }
2587
2588 if (v0 == error_mark_node || v1 == error_mark_node
2589 || mask == error_mark_node)
2590 return error_mark_node;
2591
6290f0db 2592 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
bf0cb017 2593 {
68ea4406 2594 if (complain)
2595 error_at (loc, "__builtin_shuffle last argument must "
2596 "be an integer vector");
bf0cb017 2597 return error_mark_node;
2598 }
2599
6290f0db 2600 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
2601 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
bf0cb017 2602 {
68ea4406 2603 if (complain)
2604 error_at (loc, "__builtin_shuffle arguments must be vectors");
bf0cb017 2605 return error_mark_node;
2606 }
2607
2608 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2609 {
68ea4406 2610 if (complain)
2611 error_at (loc, "__builtin_shuffle argument vectors must be of "
2612 "the same type");
bf0cb017 2613 return error_mark_node;
2614 }
2615
2616 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2617 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2618 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2619 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2620 {
68ea4406 2621 if (complain)
2622 error_at (loc, "__builtin_shuffle number of elements of the "
2623 "argument vector(s) and the mask vector should "
2624 "be the same");
bf0cb017 2625 return error_mark_node;
2626 }
2627
2628 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2629 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2630 {
68ea4406 2631 if (complain)
2632 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2633 "must have the same size as inner type of the mask");
bf0cb017 2634 return error_mark_node;
2635 }
2636
2637 if (!c_dialect_cxx ())
2638 {
2639 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2640 v0 = c_fully_fold (v0, false, &maybe_const);
2641 wrap &= maybe_const;
2642
2643 if (two_arguments)
2644 v1 = v0 = save_expr (v0);
2645 else
2646 {
2647 v1 = c_fully_fold (v1, false, &maybe_const);
2648 wrap &= maybe_const;
2649 }
2650
2651 mask = c_fully_fold (mask, false, &maybe_const);
2652 wrap &= maybe_const;
2653 }
68ea4406 2654 else if (two_arguments)
2655 v1 = v0 = save_expr (v0);
bf0cb017 2656
2657 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2658
2659 if (!c_dialect_cxx () && !wrap)
2660 ret = c_wrap_maybe_const (ret, true);
2661
2662 return ret;
2663}
2664
7f506bca 2665/* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2666 to integral type. */
2667
2668static tree
2669c_common_get_narrower (tree op, int *unsignedp_ptr)
2670{
2671 op = get_narrower (op, unsignedp_ptr);
2672
2673 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2674 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2675 {
2676 /* C++0x scoped enumerations don't implicitly convert to integral
2677 type; if we stripped an explicit conversion to a larger type we
2678 need to replace it so common_type will still work. */
a51edb4c 2679 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2680 TYPE_UNSIGNED (TREE_TYPE (op)));
7f506bca 2681 op = fold_convert (type, op);
2682 }
2683 return op;
2684}
2685
2561cea2 2686/* This is a helper function of build_binary_op.
2687
2688 For certain operations if both args were extended from the same
2689 smaller type, do the arithmetic in that type and then extend.
2690
2691 BITWISE indicates a bitwise operation.
2692 For them, this optimization is safe only if
2693 both args are zero-extended or both are sign-extended.
2694 Otherwise, we might change the result.
2695 Eg, (short)-1 | (unsigned short)-1 is (int)-1
48e1416a 2696 but calculated in (unsigned short) it would be (unsigned short)-1.
2561cea2 2697*/
7f506bca 2698tree
2699shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2561cea2 2700{
2701 int unsigned0, unsigned1;
2702 tree arg0, arg1;
2703 int uns;
2704 tree type;
2705
2706 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2707 excessive narrowing when we call get_narrower below. For
2708 example, suppose that OP0 is of unsigned int extended
2709 from signed char and that RESULT_TYPE is long long int.
2710 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2711 like
48e1416a 2712
2561cea2 2713 (long long int) (unsigned int) signed_char
2714
2715 which get_narrower would narrow down to
48e1416a 2716
2561cea2 2717 (unsigned int) signed char
48e1416a 2718
2561cea2 2719 If we do not cast OP0 first, get_narrower would return
2720 signed_char, which is inconsistent with the case of the
2721 explicit cast. */
2722 op0 = convert (result_type, op0);
2723 op1 = convert (result_type, op1);
2724
7f506bca 2725 arg0 = c_common_get_narrower (op0, &unsigned0);
2726 arg1 = c_common_get_narrower (op1, &unsigned1);
ab2c1de8 2727
2561cea2 2728 /* UNS is 1 if the operation to be done is an unsigned one. */
2729 uns = TYPE_UNSIGNED (result_type);
2730
2731 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2732 but it *requires* conversion to FINAL_TYPE. */
48e1416a 2733
2561cea2 2734 if ((TYPE_PRECISION (TREE_TYPE (op0))
2735 == TYPE_PRECISION (TREE_TYPE (arg0)))
2736 && TREE_TYPE (op0) != result_type)
2737 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2738 if ((TYPE_PRECISION (TREE_TYPE (op1))
2739 == TYPE_PRECISION (TREE_TYPE (arg1)))
2740 && TREE_TYPE (op1) != result_type)
2741 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
48e1416a 2742
2561cea2 2743 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
48e1416a 2744
2561cea2 2745 /* For bitwise operations, signedness of nominal type
2746 does not matter. Consider only how operands were extended. */
2747 if (bitwise)
2748 uns = unsigned0;
48e1416a 2749
2561cea2 2750 /* Note that in all three cases below we refrain from optimizing
2751 an unsigned operation on sign-extended args.
2752 That would not be valid. */
48e1416a 2753
2561cea2 2754 /* Both args variable: if both extended in same way
2755 from same width, do it in that width.
2756 Do it unsigned if args were zero-extended. */
2757 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2758 < TYPE_PRECISION (result_type))
2759 && (TYPE_PRECISION (TREE_TYPE (arg1))
2760 == TYPE_PRECISION (TREE_TYPE (arg0)))
2761 && unsigned0 == unsigned1
2762 && (unsigned0 || !uns))
2763 return c_common_signed_or_unsigned_type
2764 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2765
2766 else if (TREE_CODE (arg0) == INTEGER_CST
2767 && (unsigned1 || !uns)
2768 && (TYPE_PRECISION (TREE_TYPE (arg1))
2769 < TYPE_PRECISION (result_type))
2770 && (type
2771 = c_common_signed_or_unsigned_type (unsigned1,
2772 TREE_TYPE (arg1)))
2773 && !POINTER_TYPE_P (type)
2774 && int_fits_type_p (arg0, type))
2775 return type;
2776
2777 else if (TREE_CODE (arg1) == INTEGER_CST
2778 && (unsigned0 || !uns)
2779 && (TYPE_PRECISION (TREE_TYPE (arg0))
2780 < TYPE_PRECISION (result_type))
2781 && (type
2782 = c_common_signed_or_unsigned_type (unsigned0,
2783 TREE_TYPE (arg0)))
2784 && !POINTER_TYPE_P (type)
2785 && int_fits_type_p (arg1, type))
2786 return type;
2787
2788 return result_type;
2789}
2790
e53013a8 2791/* Returns true iff any integer value of type FROM_TYPE can be represented as
2792 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2793
2794static bool
2795int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2796{
2797 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2798 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2799 REAL_VALUE_TYPE real_low_bound =
2800 real_value_from_int_cst (0, type_low_bound);
2801 REAL_VALUE_TYPE real_high_bound =
2802 real_value_from_int_cst (0, type_high_bound);
2803
2804 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2805 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2806}
2807
2808/* Checks if expression EXPR of complex/real/integer type cannot be converted
2809 to the complex/real/integer type TYPE. Function returns non-zero when:
22a75734 2810 * EXPR is a constant which cannot be exactly converted to TYPE.
2811 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
e53013a8 2812 for EXPR type and TYPE being both integers or both real, or both
2813 complex.
2814 * EXPR is not a constant of complex type and TYPE is a real or
2815 an integer.
22a75734 2816 * EXPR is not a constant of real type and TYPE is an integer.
2817 * EXPR is not a constant of integer type which cannot be
2818 exactly converted to real type.
e53013a8 2819
7dfa155b 2820 Function allows conversions between types of different signedness and
ca9d7d74 2821 can return SAFE_CONVERSION (zero) in that case. Function can produce
e53013a8 2822 signedness warnings if PRODUCE_WARNS is true.
2823
2824 Function allows conversions from complex constants to non-complex types,
2825 provided that imaginary part is zero and real part can be safely converted
2826 to TYPE. */
22a75734 2827
ca9d7d74 2828enum conversion_safety
22a75734 2829unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
d31d55f0 2830{
ca9d7d74 2831 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
27259707 2832 tree expr_type = TREE_TYPE (expr);
22a75734 2833 loc = expansion_point_location_if_in_system_header (loc);
d31d55f0 2834
7dfa155b 2835 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
5b16c152 2836 {
e53013a8 2837 /* If type is complex, we are interested in compatibility with
2838 underlying type. */
2839 if (TREE_CODE (type) == COMPLEX_TYPE)
2840 type = TREE_TYPE (type);
2841
d31d55f0 2842 /* Warn for real constant that is not an exact integer converted
7dfa155b 2843 to integer type. */
27259707 2844 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2845 && TREE_CODE (type) == INTEGER_TYPE)
2846 {
2847 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
ca9d7d74 2848 give_warning = UNSAFE_REAL;
7dfa155b 2849 }
da1fb07b 2850 /* Warn for an integer constant that does not fit into integer type. */
27259707 2851 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2852 && TREE_CODE (type) == INTEGER_TYPE
2853 && !int_fits_type_p (expr, type))
2854 {
2855 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
27259707 2856 && tree_int_cst_sgn (expr) < 0)
7dfa155b 2857 {
2858 if (produce_warns)
2859 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2860 " implicitly converted to unsigned type");
2861 }
2862 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2863 {
2864 if (produce_warns)
2865 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2866 " constant value to negative integer");
2867 }
7ee0d227 2868 else
ca9d7d74 2869 give_warning = UNSAFE_OTHER;
7dfa155b 2870 }
d31d55f0 2871 else if (TREE_CODE (type) == REAL_TYPE)
7dfa155b 2872 {
2873 /* Warn for an integer constant that does not fit into real type. */
2874 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2875 {
2876 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2877 if (!exact_real_truncate (TYPE_MODE (type), &a))
ca9d7d74 2878 give_warning = UNSAFE_REAL;
7dfa155b 2879 }
2880 /* Warn for a real constant that does not fit into a smaller
2881 real type. */
2882 else if (TREE_CODE (expr_type) == REAL_TYPE
2883 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2884 {
2885 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2886 if (!exact_real_truncate (TYPE_MODE (type), &a))
ca9d7d74 2887 give_warning = UNSAFE_REAL;
7dfa155b 2888 }
2889 }
2890 }
e53013a8 2891
2892 else if (TREE_CODE (expr) == COMPLEX_CST)
2893 {
2894 tree imag_part = TREE_IMAGPART (expr);
2895 /* Conversion from complex constant with zero imaginary part,
2896 perform check for conversion of real part. */
2897 if ((TREE_CODE (imag_part) == REAL_CST
2898 && real_zerop (imag_part))
2899 || (TREE_CODE (imag_part) == INTEGER_CST
2900 && integer_zerop (imag_part)))
2901 /* Note: in this branch we use recursive call to unsafe_conversion_p
2902 with different type of EXPR, but it is still safe, because when EXPR
2903 is a constant, it's type is not used in text of generated warnings
2904 (otherwise they could sound misleading). */
2905 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2906 produce_warns);
2907 /* Conversion from complex constant with non-zero imaginary part. */
2908 else
2909 {
2910 /* Conversion to complex type.
2911 Perform checks for both real and imaginary parts. */
2912 if (TREE_CODE (type) == COMPLEX_TYPE)
2913 {
2914 /* Unfortunately, produce_warns must be false in two subsequent
2915 calls of unsafe_conversion_p, because otherwise we could
2916 produce strange "double" warnings, if both real and imaginary
2917 parts have conversion problems related to signedness.
2918
2919 For example:
2920 int32_t _Complex a = 0x80000000 + 0x80000000i;
2921
2922 Possible solution: add a separate function for checking
2923 constants and combine result of two calls appropriately. */
2924 enum conversion_safety re_safety =
2925 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2926 enum conversion_safety im_safety =
2927 unsafe_conversion_p (loc, type, imag_part, false);
2928
2929 /* Merge the results into appropriate single warning. */
2930
2931 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2932 if (re_safety == im_safety)
2933 give_warning = re_safety;
2934 else if (!re_safety && im_safety)
2935 give_warning = im_safety;
2936 else if (re_safety && !im_safety)
2937 give_warning = re_safety;
2938 else
2939 give_warning = UNSAFE_OTHER;
2940 }
2941 /* Warn about conversion from complex to real or integer type. */
2942 else
2943 give_warning = UNSAFE_IMAGINARY;
2944 }
2945 }
2946
2947 /* Checks for remaining case: EXPR is not constant. */
7dfa155b 2948 else
2949 {
d31d55f0 2950 /* Warn for real types converted to integer types. */
2561cea2 2951 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2952 && TREE_CODE (type) == INTEGER_TYPE)
ca9d7d74 2953 give_warning = UNSAFE_REAL;
d31d55f0 2954
2561cea2 2955 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2956 && TREE_CODE (type) == INTEGER_TYPE)
2957 {
69609004 2958 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
f9d856a4 2959 expr = get_unwidened (expr, 0);
2561cea2 2960 expr_type = TREE_TYPE (expr);
69609004 2961
2561cea2 2962 /* Don't warn for short y; short x = ((int)y & 0xff); */
48e1416a 2963 if (TREE_CODE (expr) == BIT_AND_EXPR
7dfa155b 2964 || TREE_CODE (expr) == BIT_IOR_EXPR
2561cea2 2965 || TREE_CODE (expr) == BIT_XOR_EXPR)
2966 {
27259707 2967 /* If both args were extended from a shortest type,
2968 use that type if that is safe. */
48e1416a 2969 expr_type = shorten_binary_op (expr_type,
2970 TREE_OPERAND (expr, 0),
2971 TREE_OPERAND (expr, 1),
2561cea2 2972 /* bitwise */1);
2973
2561cea2 2974 if (TREE_CODE (expr) == BIT_AND_EXPR)
2975 {
2976 tree op0 = TREE_OPERAND (expr, 0);
2977 tree op1 = TREE_OPERAND (expr, 1);
30de145b 2978 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2979 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2980
2981 /* If one of the operands is a non-negative constant
2982 that fits in the target type, then the type of the
2983 other operand does not matter. */
2561cea2 2984 if ((TREE_CODE (op0) == INTEGER_CST
2985 && int_fits_type_p (op0, c_common_signed_type (type))
2986 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2987 || (TREE_CODE (op1) == INTEGER_CST
27259707 2988 && int_fits_type_p (op1, c_common_signed_type (type))
48e1416a 2989 && int_fits_type_p (op1,
27259707 2990 c_common_unsigned_type (type))))
ca9d7d74 2991 return SAFE_CONVERSION;
30de145b 2992 /* If constant is unsigned and fits in the target
2993 type, then the result will also fit. */
2994 else if ((TREE_CODE (op0) == INTEGER_CST
48e1416a 2995 && unsigned0
30de145b 2996 && int_fits_type_p (op0, type))
2997 || (TREE_CODE (op1) == INTEGER_CST
2998 && unsigned1
2999 && int_fits_type_p (op1, type)))
ca9d7d74 3000 return SAFE_CONVERSION;
2561cea2 3001 }
3002 }
7dfa155b 3003 /* Warn for integer types converted to smaller integer types. */
48e1416a 3004 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
ca9d7d74 3005 give_warning = UNSAFE_OTHER;
7ee0d227 3006
3007 /* When they are the same width but different signedness,
3008 then the value may change. */
7dfa155b 3009 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2561cea2 3010 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
7ee0d227 3011 /* Even when converted to a bigger type, if the type is
3012 unsigned but expr is signed, then negative values
3013 will be changed. */
7dfa155b 3014 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
3015 && produce_warns)
200dd99c 3016 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
3017 "may change the sign of the result",
3018 type, expr_type);
7dfa155b 3019 }
d31d55f0 3020
3021 /* Warn for integer types converted to real types if and only if
7dfa155b 3022 all the range of values of the integer type cannot be
3023 represented by the real type. */
2561cea2 3024 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 3025 && TREE_CODE (type) == REAL_TYPE)
3026 {
4c2cfa81 3027 /* Don't warn about char y = 0xff; float x = (int) y; */
3028 expr = get_unwidened (expr, 0);
3029 expr_type = TREE_TYPE (expr);
3030
e53013a8 3031 if (!int_safely_convertible_to_real_p (expr_type, type))
ca9d7d74 3032 give_warning = UNSAFE_OTHER;
7dfa155b 3033 }
d31d55f0 3034
3035 /* Warn for real types converted to smaller real types. */
2561cea2 3036 else if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 3037 && TREE_CODE (type) == REAL_TYPE
3038 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
ca9d7d74 3039 give_warning = UNSAFE_REAL;
e53013a8 3040
3041 /* Check conversion between two complex types. */
3042 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
3043 && TREE_CODE (type) == COMPLEX_TYPE)
3044 {
3045 /* Extract underlying types (i.e., type of real and imaginary
3046 parts) of expr_type and type. */
3047 tree from_type = TREE_TYPE (expr_type);
3048 tree to_type = TREE_TYPE (type);
3049
3050 /* Warn for real types converted to integer types. */
3051 if (TREE_CODE (from_type) == REAL_TYPE
3052 && TREE_CODE (to_type) == INTEGER_TYPE)
3053 give_warning = UNSAFE_REAL;
3054
3055 /* Warn for real types converted to smaller real types. */
3056 else if (TREE_CODE (from_type) == REAL_TYPE
3057 && TREE_CODE (to_type) == REAL_TYPE
3058 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
3059 give_warning = UNSAFE_REAL;
3060
3061 /* Check conversion for complex integer types. Here implementation
3062 is simpler than for real-domain integers because it does not
3063 involve sophisticated cases, such as bitmasks, casts, etc. */
3064 else if (TREE_CODE (from_type) == INTEGER_TYPE
3065 && TREE_CODE (to_type) == INTEGER_TYPE)
3066 {
3067 /* Warn for integer types converted to smaller integer types. */
3068 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
3069 give_warning = UNSAFE_OTHER;
3070
3071 /* Check for different signedness, see case for real-domain
3072 integers (above) for a more detailed comment. */
3073 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
3074 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
3075 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
3076 && produce_warns)
3077 warning_at (loc, OPT_Wsign_conversion,
3078 "conversion to %qT from %qT "
3079 "may change the sign of the result",
3080 type, expr_type);
3081 }
3082 else if (TREE_CODE (from_type) == INTEGER_TYPE
3083 && TREE_CODE (to_type) == REAL_TYPE
3084 && !int_safely_convertible_to_real_p (from_type, to_type))
3085 give_warning = UNSAFE_OTHER;
3086 }
3087
3088 /* Warn for complex types converted to real or integer types. */
3089 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
3090 && TREE_CODE (type) != COMPLEX_TYPE)
3091 give_warning = UNSAFE_IMAGINARY;
7dfa155b 3092 }
3093
3094 return give_warning;
3095}
3096
3097/* Warns if the conversion of EXPR to TYPE may alter a value.
3098 This is a helper function for warnings_for_convert_and_check. */
3099
3100static void
22a75734 3101conversion_warning (location_t loc, tree type, tree expr)
7dfa155b 3102{
7dfa155b 3103 tree expr_type = TREE_TYPE (expr);
ca9d7d74 3104 enum conversion_safety conversion_kind;
d31d55f0 3105
ca9d7d74 3106 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
7dfa155b 3107 return;
d31d55f0 3108
ec704957 3109 /* This may happen, because for LHS op= RHS we preevaluate
3110 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
3111 means we could no longer see the code of the EXPR. */
3112 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
3113 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
3114 if (TREE_CODE (expr) == SAVE_EXPR)
3115 expr = TREE_OPERAND (expr, 0);
3116
7dfa155b 3117 switch (TREE_CODE (expr))
3118 {
3119 case EQ_EXPR:
3120 case NE_EXPR:
3121 case LE_EXPR:
3122 case GE_EXPR:
3123 case LT_EXPR:
3124 case GT_EXPR:
3125 case TRUTH_ANDIF_EXPR:
3126 case TRUTH_ORIF_EXPR:
3127 case TRUTH_AND_EXPR:
3128 case TRUTH_OR_EXPR:
3129 case TRUTH_XOR_EXPR:
3130 case TRUTH_NOT_EXPR:
3131 /* Conversion from boolean to a signed:1 bit-field (which only
3132 can hold the values 0 and -1) doesn't lose information - but
3133 it does change the value. */
3134 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
3135 warning_at (loc, OPT_Wconversion,
3136 "conversion to %qT from boolean expression", type);
3137 return;
3138
3139 case REAL_CST:
3140 case INTEGER_CST:
e53013a8 3141 case COMPLEX_CST:
22a75734 3142 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
ca9d7d74 3143 if (conversion_kind == UNSAFE_REAL)
3144 warning_at (loc, OPT_Wfloat_conversion,
3145 "conversion to %qT alters %qT constant value",
3146 type, expr_type);
3147 else if (conversion_kind)
7dfa155b 3148 warning_at (loc, OPT_Wconversion,
3149 "conversion to %qT alters %qT constant value",
3150 type, expr_type);
3151 return;
3152
3153 case COND_EXPR:
3154 {
0e4e775a 3155 /* In case of COND_EXPR, we do not care about the type of
3156 COND_EXPR, only about the conversion of each operand. */
3157 tree op1 = TREE_OPERAND (expr, 1);
3158 tree op2 = TREE_OPERAND (expr, 2);
3159
22a75734 3160 conversion_warning (loc, type, op1);
3161 conversion_warning (loc, type, op2);
0e4e775a 3162 return;
7dfa155b 3163 }
3164
3165 default: /* 'expr' is not a constant. */
22a75734 3166 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
ca9d7d74 3167 if (conversion_kind == UNSAFE_REAL)
3168 warning_at (loc, OPT_Wfloat_conversion,
3169 "conversion to %qT from %qT may alter its value",
3170 type, expr_type);
e53013a8 3171 else if (conversion_kind == UNSAFE_IMAGINARY)
3172 warning_at (loc, OPT_Wconversion,
3173 "conversion to %qT from %qT discards imaginary component",
3174 type, expr_type);
ca9d7d74 3175 else if (conversion_kind)
7dfa155b 3176 warning_at (loc, OPT_Wconversion,
200dd99c 3177 "conversion to %qT from %qT may alter its value",
3178 type, expr_type);
d31d55f0 3179 }
3180}
3181
59dd8856 3182/* Produce warnings after a conversion. RESULT is the result of
3183 converting EXPR to TYPE. This is a helper function for
3184 convert_and_check and cp_convert_and_check. */
2a1736ed 3185
59dd8856 3186void
22a75734 3187warnings_for_convert_and_check (location_t loc, tree type, tree expr,
3188 tree result)
2a1736ed 3189{
22a75734 3190 loc = expansion_point_location_if_in_system_header (loc);
61f69bc9 3191
da1fb07b 3192 if (TREE_CODE (expr) == INTEGER_CST
3193 && (TREE_CODE (type) == INTEGER_TYPE
3194 || TREE_CODE (type) == ENUMERAL_TYPE)
3195 && !int_fits_type_p (expr, type))
3196 {
d31d55f0 3197 /* Do not diagnose overflow in a constant expression merely
3198 because a conversion overflowed. */
da1fb07b 3199 if (TREE_OVERFLOW (result))
eddad94a 3200 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
3201
da1fb07b 3202 if (TYPE_UNSIGNED (type))
d31d55f0 3203 {
da1fb07b 3204 /* This detects cases like converting -129 or 256 to
3205 unsigned char. */
3206 if (!int_fits_type_p (expr, c_common_signed_type (type)))
61f69bc9 3207 warning_at (loc, OPT_Woverflow,
3208 "large integer implicitly truncated to unsigned type");
7ee0d227 3209 else
22a75734 3210 conversion_warning (loc, type, expr);
da1fb07b 3211 }
48e1416a 3212 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
22a75734 3213 warning_at (loc, OPT_Woverflow,
e0913805 3214 "overflow in implicit constant conversion");
3215 /* No warning for converting 0x80000000 to int. */
3216 else if (pedantic
3217 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
3218 || TYPE_PRECISION (TREE_TYPE (expr))
3219 != TYPE_PRECISION (type)))
61f69bc9 3220 warning_at (loc, OPT_Woverflow,
3221 "overflow in implicit constant conversion");
e0913805 3222
7ee0d227 3223 else
22a75734 3224 conversion_warning (loc, type, expr);
2a1736ed 3225 }
9421ebb9 3226 else if ((TREE_CODE (result) == INTEGER_CST
3227 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
61f69bc9 3228 warning_at (loc, OPT_Woverflow,
3229 "overflow in implicit constant conversion");
7ee0d227 3230 else
22a75734 3231 conversion_warning (loc, type, expr);
59dd8856 3232}
3233
3234
3235/* Convert EXPR to TYPE, warning about conversion problems with constants.
3236 Invoke this function on every expression that is converted implicitly,
3237 i.e. because of language rules and not because of an explicit cast. */
3238
3239tree
22a75734 3240convert_and_check (location_t loc, tree type, tree expr)
59dd8856 3241{
3242 tree result;
c6418a4e 3243 tree expr_for_warning;
3244
3245 /* Convert from a value with possible excess precision rather than
3246 via the semantic type, but do not warn about values not fitting
3247 exactly in the semantic type. */
3248 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
3249 {
3250 tree orig_type = TREE_TYPE (expr);
3251 expr = TREE_OPERAND (expr, 0);
3252 expr_for_warning = convert (orig_type, expr);
3253 if (orig_type == type)
3254 return expr_for_warning;
3255 }
3256 else
3257 expr_for_warning = expr;
59dd8856 3258
3259 if (TREE_TYPE (expr) == type)
3260 return expr;
48e1416a 3261
59dd8856 3262 result = convert (type, expr);
3263
48d94ede 3264 if (c_inhibit_evaluation_warnings == 0
3265 && !TREE_OVERFLOW_P (expr)
3266 && result != error_mark_node)
22a75734 3267 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
59dd8856 3268
da1fb07b 3269 return result;
b2806639 3270}
3271\f
4e91a871 3272/* A node in a list that describes references to variables (EXPR), which are
3273 either read accesses if WRITER is zero, or write accesses, in which case
3274 WRITER is the parent of EXPR. */
3275struct tlist
3276{
3277 struct tlist *next;
3278 tree expr, writer;
3279};
3280
3281/* Used to implement a cache the results of a call to verify_tree. We only
3282 use this for SAVE_EXPRs. */
3283struct tlist_cache
3284{
3285 struct tlist_cache *next;
3286 struct tlist *cache_before_sp;
3287 struct tlist *cache_after_sp;
3288 tree expr;
481c6ce6 3289};
3290
4e91a871 3291/* Obstack to use when allocating tlist structures, and corresponding
3292 firstobj. */
3293static struct obstack tlist_obstack;
3294static char *tlist_firstobj = 0;
3295
3296/* Keep track of the identifiers we've warned about, so we can avoid duplicate
3297 warnings. */
3298static struct tlist *warned_ids;
3299/* SAVE_EXPRs need special treatment. We process them only once and then
3300 cache the results. */
3301static struct tlist_cache *save_expr_cache;
3302
1cae46be 3303static void add_tlist (struct tlist **, struct tlist *, tree, int);
3304static void merge_tlist (struct tlist **, struct tlist *, int);
3305static void verify_tree (tree, struct tlist **, struct tlist **, tree);
3306static int warning_candidate_p (tree);
79973b57 3307static bool candidate_equal_p (const_tree, const_tree);
1cae46be 3308static void warn_for_collisions (struct tlist *);
3309static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
3310static struct tlist *new_tlist (struct tlist *, tree, tree);
481c6ce6 3311
4e91a871 3312/* Create a new struct tlist and fill in its fields. */
3313static struct tlist *
1cae46be 3314new_tlist (struct tlist *next, tree t, tree writer)
4e91a871 3315{
3316 struct tlist *l;
9318f22c 3317 l = XOBNEW (&tlist_obstack, struct tlist);
4e91a871 3318 l->next = next;
3319 l->expr = t;
3320 l->writer = writer;
3321 return l;
3322}
3323
3324/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3325 is nonnull, we ignore any node we find which has a writer equal to it. */
3326
3327static void
1cae46be 3328add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
4e91a871 3329{
3330 while (add)
3331 {
3332 struct tlist *next = add->next;
84166705 3333 if (!copy)
4e91a871 3334 add->next = *to;
79973b57 3335 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
4e91a871 3336 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3337 add = next;
3338 }
3339}
3340
3341/* Merge the nodes of ADD into TO. This merging process is done so that for
3342 each variable that already exists in TO, no new node is added; however if
3343 there is a write access recorded in ADD, and an occurrence on TO is only
3344 a read access, then the occurrence in TO will be modified to record the
3345 write. */
481c6ce6 3346
3347static void
1cae46be 3348merge_tlist (struct tlist **to, struct tlist *add, int copy)
4e91a871 3349{
3350 struct tlist **end = to;
3351
3352 while (*end)
3353 end = &(*end)->next;
3354
3355 while (add)
3356 {
3357 int found = 0;
3358 struct tlist *tmp2;
3359 struct tlist *next = add->next;
3360
3361 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
79973b57 3362 if (candidate_equal_p (tmp2->expr, add->expr))
4e91a871 3363 {
3364 found = 1;
84166705 3365 if (!tmp2->writer)
4e91a871 3366 tmp2->writer = add->writer;
3367 }
84166705 3368 if (!found)
4e91a871 3369 {
312243bb 3370 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
4e91a871 3371 end = &(*end)->next;
3372 *end = 0;
3373 }
3374 add = next;
3375 }
3376}
3377
3378/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3379 references in list LIST conflict with it, excluding reads if ONLY writers
3380 is nonzero. */
3381
3382static void
1cae46be 3383warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3384 int only_writes)
4e91a871 3385{
3386 struct tlist *tmp;
3387
3388 /* Avoid duplicate warnings. */
3389 for (tmp = warned_ids; tmp; tmp = tmp->next)
79973b57 3390 if (candidate_equal_p (tmp->expr, written))
4e91a871 3391 return;
3392
3393 while (list)
3394 {
79973b57 3395 if (candidate_equal_p (list->expr, written)
3396 && !candidate_equal_p (list->writer, writer)
3397 && (!only_writes || list->writer))
4e91a871 3398 {
3399 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3df42822 3400 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
6513b50d 3401 OPT_Wsequence_point, "operation on %qE may be undefined",
3402 list->expr);
4e91a871 3403 }
3404 list = list->next;
3405 }
3406}
3407
3408/* Given a list LIST of references to variables, find whether any of these
3409 can cause conflicts due to missing sequence points. */
3410
3411static void
1cae46be 3412warn_for_collisions (struct tlist *list)
4e91a871 3413{
3414 struct tlist *tmp;
1cae46be 3415
4e91a871 3416 for (tmp = list; tmp; tmp = tmp->next)
3417 {
3418 if (tmp->writer)
3419 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3420 }
3421}
3422
734c98be 3423/* Return nonzero if X is a tree that can be verified by the sequence point
4e91a871 3424 warnings. */
3425static int
1cae46be 3426warning_candidate_p (tree x)
481c6ce6 3427{
6ef8d12f 3428 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3429 return 0;
3430
027fc6ef 3431 if (TREE_CODE (x) == BLOCK)
3432 return 0;
3433
6ef8d12f 3434 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
79973b57 3435 (lvalue_p) crash on TRY/CATCH. */
6ef8d12f 3436 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3437 return 0;
3438
3439 if (!lvalue_p (x))
3440 return 0;
3441
3442 /* No point to track non-const calls, they will never satisfy
3443 operand_equal_p. */
3444 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3445 return 0;
3446
3447 if (TREE_CODE (x) == STRING_CST)
3448 return 0;
3449
3450 return 1;
79973b57 3451}
3452
3453/* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3454static bool
3455candidate_equal_p (const_tree x, const_tree y)
3456{
3457 return (x == y) || (x && y && operand_equal_p (x, y, 0));
4e91a871 3458}
481c6ce6 3459
4e91a871 3460/* Walk the tree X, and record accesses to variables. If X is written by the
3461 parent tree, WRITER is the parent.
3462 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3463 expression or its only operand forces a sequence point, then everything up
3464 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3465 in PNO_SP.
3466 Once we return, we will have emitted warnings if any subexpression before
3467 such a sequence point could be undefined. On a higher level, however, the
3468 sequence point may not be relevant, and we'll merge the two lists.
3469
3470 Example: (b++, a) + b;
3471 The call that processes the COMPOUND_EXPR will store the increment of B
3472 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3473 processes the PLUS_EXPR will need to merge the two lists so that
3474 eventually, all accesses end up on the same list (and we'll warn about the
3475 unordered subexpressions b++ and b.
3476
3477 A note on merging. If we modify the former example so that our expression
3478 becomes
3479 (b++, b) + a
3480 care must be taken not simply to add all three expressions into the final
3481 PNO_SP list. The function merge_tlist takes care of that by merging the
3482 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3483 way, so that no more than one access to B is recorded. */
481c6ce6 3484
4e91a871 3485static void
1cae46be 3486verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3487 tree writer)
4e91a871 3488{
3489 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3490 enum tree_code code;
ce45a448 3491 enum tree_code_class cl;
481c6ce6 3492
e5b75768 3493 /* X may be NULL if it is the operand of an empty statement expression
3494 ({ }). */
3495 if (x == NULL)
3496 return;
3497
4e91a871 3498 restart:
3499 code = TREE_CODE (x);
e916c70c 3500 cl = TREE_CODE_CLASS (code);
481c6ce6 3501
4e91a871 3502 if (warning_candidate_p (x))
79973b57 3503 *pno_sp = new_tlist (*pno_sp, x, writer);
4e91a871 3504
3505 switch (code)
3506 {
67b28e3e 3507 case CONSTRUCTOR:
8e71dad2 3508 case SIZEOF_EXPR:
67b28e3e 3509 return;
3510
4e91a871 3511 case COMPOUND_EXPR:
3512 case TRUTH_ANDIF_EXPR:
3513 case TRUTH_ORIF_EXPR:
3514 tmp_before = tmp_nosp = tmp_list3 = 0;
3515 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3516 warn_for_collisions (tmp_nosp);
3517 merge_tlist (pbefore_sp, tmp_before, 0);
3518 merge_tlist (pbefore_sp, tmp_nosp, 0);
3519 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3520 merge_tlist (pbefore_sp, tmp_list3, 0);
3521 return;
3522
3523 case COND_EXPR:
3524 tmp_before = tmp_list2 = 0;
3525 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3526 warn_for_collisions (tmp_list2);
3527 merge_tlist (pbefore_sp, tmp_before, 0);
312243bb 3528 merge_tlist (pbefore_sp, tmp_list2, 0);
4e91a871 3529
3530 tmp_list3 = tmp_nosp = 0;
3531 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3532 warn_for_collisions (tmp_nosp);
3533 merge_tlist (pbefore_sp, tmp_list3, 0);
3534
3535 tmp_list3 = tmp_list2 = 0;
3536 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3537 warn_for_collisions (tmp_list2);
3538 merge_tlist (pbefore_sp, tmp_list3, 0);
3539 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3540 two first, to avoid warning for (a ? b++ : b++). */
3541 merge_tlist (&tmp_nosp, tmp_list2, 0);
3542 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3543 return;
3544
481c6ce6 3545 case PREDECREMENT_EXPR:
3546 case PREINCREMENT_EXPR:
3547 case POSTDECREMENT_EXPR:
3548 case POSTINCREMENT_EXPR:
4e91a871 3549 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3550 return;
3551
3552 case MODIFY_EXPR:
3553 tmp_before = tmp_nosp = tmp_list3 = 0;
3554 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3555 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3556 /* Expressions inside the LHS are not ordered wrt. the sequence points
3557 in the RHS. Example:
3558 *a = (a++, 2)
3559 Despite the fact that the modification of "a" is in the before_sp
3560 list (tmp_before), it conflicts with the use of "a" in the LHS.
3561 We can handle this by adding the contents of tmp_list3
3562 to those of tmp_before, and redoing the collision warnings for that
3563 list. */
3564 add_tlist (&tmp_before, tmp_list3, x, 1);
3565 warn_for_collisions (tmp_before);
3566 /* Exclude the LHS itself here; we first have to merge it into the
3567 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3568 didn't exclude the LHS, we'd get it twice, once as a read and once
3569 as a write. */
3570 add_tlist (pno_sp, tmp_list3, x, 0);
3571 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3572
3573 merge_tlist (pbefore_sp, tmp_before, 0);
3574 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3575 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3576 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3577 return;
481c6ce6 3578
3579 case CALL_EXPR:
4e91a871 3580 /* We need to warn about conflicts among arguments and conflicts between
3581 args and the function address. Side effects of the function address,
3582 however, are not ordered by the sequence point of the call. */
c2f47e15 3583 {
3584 call_expr_arg_iterator iter;
3585 tree arg;
48e1416a 3586 tmp_before = tmp_nosp = 0;
c2f47e15 3587 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3588 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3589 {
3590 tmp_list2 = tmp_list3 = 0;
3591 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3592 merge_tlist (&tmp_list3, tmp_list2, 0);
3593 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3594 }
3595 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3596 warn_for_collisions (tmp_before);
3597 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3598 return;
3599 }
481c6ce6 3600
3601 case TREE_LIST:
3602 /* Scan all the list, e.g. indices of multi dimensional array. */
3603 while (x)
3604 {
4e91a871 3605 tmp_before = tmp_nosp = 0;
3606 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3607 merge_tlist (&tmp_nosp, tmp_before, 0);
3608 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
481c6ce6 3609 x = TREE_CHAIN (x);
3610 }
4e91a871 3611 return;
481c6ce6 3612
4e91a871 3613 case SAVE_EXPR:
3614 {
3615 struct tlist_cache *t;
3616 for (t = save_expr_cache; t; t = t->next)
79973b57 3617 if (candidate_equal_p (t->expr, x))
4e91a871 3618 break;
481c6ce6 3619
84166705 3620 if (!t)
481c6ce6 3621 {
9318f22c 3622 t = XOBNEW (&tlist_obstack, struct tlist_cache);
4e91a871 3623 t->next = save_expr_cache;
3624 t->expr = x;
3625 save_expr_cache = t;
3626
3627 tmp_before = tmp_nosp = 0;
3628 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3629 warn_for_collisions (tmp_nosp);
3630
3631 tmp_list3 = 0;
312243bb 3632 merge_tlist (&tmp_list3, tmp_nosp, 0);
4e91a871 3633 t->cache_before_sp = tmp_before;
3634 t->cache_after_sp = tmp_list3;
481c6ce6 3635 }
4e91a871 3636 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3637 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3638 return;
3639 }
481c6ce6 3640
012916cb 3641 case ADDR_EXPR:
3642 x = TREE_OPERAND (x, 0);
3643 if (DECL_P (x))
3644 return;
3645 writer = 0;
3646 goto restart;
3647
ce45a448 3648 default:
3649 /* For other expressions, simply recurse on their operands.
a0c938f0 3650 Manual tail recursion for unary expressions.
ce45a448 3651 Other non-expressions need not be processed. */
3652 if (cl == tcc_unary)
3653 {
ce45a448 3654 x = TREE_OPERAND (x, 0);
3655 writer = 0;
3656 goto restart;
3657 }
3658 else if (IS_EXPR_CODE_CLASS (cl))
3659 {
3660 int lp;
c2f47e15 3661 int max = TREE_OPERAND_LENGTH (x);
ce45a448 3662 for (lp = 0; lp < max; lp++)
3663 {
3664 tmp_before = tmp_nosp = 0;
3665 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3666 merge_tlist (&tmp_nosp, tmp_before, 0);
3667 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3668 }
3669 }
3670 return;
481c6ce6 3671 }
481c6ce6 3672}
3673
974e2c0c 3674/* Try to warn for undefined behavior in EXPR due to missing sequence
481c6ce6 3675 points. */
3676
4b987fac 3677DEBUG_FUNCTION void
1cae46be 3678verify_sequence_points (tree expr)
481c6ce6 3679{
4e91a871 3680 struct tlist *before_sp = 0, *after_sp = 0;
481c6ce6 3681
4e91a871 3682 warned_ids = 0;
3683 save_expr_cache = 0;
3684 if (tlist_firstobj == 0)
481c6ce6 3685 {
4e91a871 3686 gcc_obstack_init (&tlist_obstack);
4fd61bc6 3687 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
481c6ce6 3688 }
3689
4e91a871 3690 verify_tree (expr, &before_sp, &after_sp, 0);
3691 warn_for_collisions (after_sp);
3692 obstack_free (&tlist_obstack, tlist_firstobj);
481c6ce6 3693}
b0fc3e72 3694\f
3695/* Validate the expression after `case' and apply default promotions. */
3696
2ca392fd 3697static tree
2d2f6a15 3698check_case_value (location_t loc, tree value)
b0fc3e72 3699{
3700 if (value == NULL_TREE)
3701 return value;
3702
b96dc121 3703 if (TREE_CODE (value) == INTEGER_CST)
3704 /* Promote char or short to int. */
3705 value = perform_integral_promotions (value);
3706 else if (value != error_mark_node)
b0fc3e72 3707 {
2d2f6a15 3708 error_at (loc, "case label does not reduce to an integer constant");
b0fc3e72 3709 value = error_mark_node;
3710 }
b0fc3e72 3711
6433f1c2 3712 constant_expression_warning (value);
3713
b0fc3e72 3714 return value;
3715}
3716\f
2ca392fd 3717/* See if the case values LOW and HIGH are in the range of the original
5c9dae64 3718 type (i.e. before the default conversion to int) of the switch testing
2ca392fd 3719 expression.
3720 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
91275768 3721 the type before promoting it. CASE_LOW_P is a pointer to the lower
2ca392fd 3722 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3723 if the case is not a case range.
3724 The caller has to make sure that we are not called with NULL for
be23b16f 3725 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
3726 was a case value that doesn't fit into the range of the ORIG_TYPE.
442e3cb9 3727 Returns true if the case label is in range of ORIG_TYPE (saturated or
2ca392fd 3728 untouched) or false if the label is out of range. */
3729
3730static bool
f61a9bc2 3731check_case_bounds (location_t loc, tree type, tree orig_type,
be23b16f 3732 tree *case_low_p, tree *case_high_p,
3733 bool *outside_range_p)
2ca392fd 3734{
3735 tree min_value, max_value;
3736 tree case_low = *case_low_p;
3737 tree case_high = case_high_p ? *case_high_p : case_low;
3738
3739 /* If there was a problem with the original type, do nothing. */
3740 if (orig_type == error_mark_node)
3741 return true;
3742
3743 min_value = TYPE_MIN_VALUE (orig_type);
3744 max_value = TYPE_MAX_VALUE (orig_type);
3745
3746 /* Case label is less than minimum for type. */
3747 if (tree_int_cst_compare (case_low, min_value) < 0
3748 && tree_int_cst_compare (case_high, min_value) < 0)
3749 {
f61a9bc2 3750 warning_at (loc, 0, "case label value is less than minimum value "
3751 "for type");
be23b16f 3752 *outside_range_p = true;
2ca392fd 3753 return false;
3754 }
b27ac6b5 3755
2ca392fd 3756 /* Case value is greater than maximum for type. */
3757 if (tree_int_cst_compare (case_low, max_value) > 0
3758 && tree_int_cst_compare (case_high, max_value) > 0)
3759 {
f61a9bc2 3760 warning_at (loc, 0, "case label value exceeds maximum value for type");
be23b16f 3761 *outside_range_p = true;
2ca392fd 3762 return false;
3763 }
3764
3765 /* Saturate lower case label value to minimum. */
3766 if (tree_int_cst_compare (case_high, min_value) >= 0
3767 && tree_int_cst_compare (case_low, min_value) < 0)
3768 {
f61a9bc2 3769 warning_at (loc, 0, "lower value in case label range"
3770 " less than minimum value for type");
be23b16f 3771 *outside_range_p = true;
2ca392fd 3772 case_low = min_value;
3773 }
b27ac6b5 3774
2ca392fd 3775 /* Saturate upper case label value to maximum. */
3776 if (tree_int_cst_compare (case_low, max_value) <= 0
3777 && tree_int_cst_compare (case_high, max_value) > 0)
3778 {
f61a9bc2 3779 warning_at (loc, 0, "upper value in case label range"
3780 " exceeds maximum value for type");
be23b16f 3781 *outside_range_p = true;
2ca392fd 3782 case_high = max_value;
3783 }
3784
3785 if (*case_low_p != case_low)
3786 *case_low_p = convert (type, case_low);
3787 if (case_high_p && *case_high_p != case_high)
3788 *case_high_p = convert (type, case_high);
3789
3790 return true;
3791}
3792\f
b0fc3e72 3793/* Return an integer type with BITS bits of precision,
3794 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3795
3796tree
1cae46be 3797c_common_type_for_size (unsigned int bits, int unsignedp)
b0fc3e72 3798{
9f75f026 3799 int i;
3800
46375237 3801 if (bits == TYPE_PRECISION (integer_type_node))
3802 return unsignedp ? unsigned_type_node : integer_type_node;
3803
bacde65a 3804 if (bits == TYPE_PRECISION (signed_char_type_node))
b0fc3e72 3805 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3806
bacde65a 3807 if (bits == TYPE_PRECISION (short_integer_type_node))
b0fc3e72 3808 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3809
bacde65a 3810 if (bits == TYPE_PRECISION (long_integer_type_node))
b0fc3e72 3811 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3812
bacde65a 3813 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b0fc3e72 3814 return (unsignedp ? long_long_unsigned_type_node
3815 : long_long_integer_type_node);
3816
9f75f026 3817 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3818 if (int_n_enabled_p[i]
3819 && bits == int_n_data[i].bitsize)
3820 return (unsignedp ? int_n_trees[i].unsigned_type
3821 : int_n_trees[i].signed_type);
6388cfe2 3822
f57fa2ea 3823 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3824 return (unsignedp ? widest_unsigned_literal_type_node
3825 : widest_integer_literal_type_node);
3826
bacde65a 3827 if (bits <= TYPE_PRECISION (intQI_type_node))
3828 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3829
3830 if (bits <= TYPE_PRECISION (intHI_type_node))
3831 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3832
3833 if (bits <= TYPE_PRECISION (intSI_type_node))
3834 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3835
3836 if (bits <= TYPE_PRECISION (intDI_type_node))
3837 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3838
b0fc3e72 3839 return 0;
3840}
3841
9421ebb9 3842/* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3843 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3844 and saturating if SATP is nonzero, otherwise not saturating. */
3845
3846tree
3847c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3848 int unsignedp, int satp)
3849{
3754d046 3850 machine_mode mode;
9421ebb9 3851 if (ibit == 0)
3852 mode = unsignedp ? UQQmode : QQmode;
3853 else
3854 mode = unsignedp ? UHAmode : HAmode;
3855
3856 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3857 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3858 break;
3859
3860 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3861 {
3862 sorry ("GCC cannot support operators with integer types and "
3863 "fixed-point types that have too many integral and "
3864 "fractional bits together");
3865 return 0;
3866 }
3867
3868 return c_common_type_for_mode (mode, satp);
3869}
3870
5b247e9f 3871/* Used for communication between c_common_type_for_mode and
3872 c_register_builtin_type. */
c1917557 3873tree registered_builtin_types;
5b247e9f 3874
b0fc3e72 3875/* Return a data type that has machine mode MODE.
3876 If the mode is an integer,
9421ebb9 3877 then UNSIGNEDP selects between signed and unsigned types.
3878 If the mode is a fixed-point mode,
3879 then UNSIGNEDP selects between saturating and nonsaturating types. */
b0fc3e72 3880
3881tree
3754d046 3882c_common_type_for_mode (machine_mode mode, int unsignedp)
b0fc3e72 3883{
5b247e9f 3884 tree t;
9f75f026 3885 int i;
5b247e9f 3886
46375237 3887 if (mode == TYPE_MODE (integer_type_node))
3888 return unsignedp ? unsigned_type_node : integer_type_node;
3889
b0fc3e72 3890 if (mode == TYPE_MODE (signed_char_type_node))
3891 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3892
3893 if (mode == TYPE_MODE (short_integer_type_node))
3894 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3895
b0fc3e72 3896 if (mode == TYPE_MODE (long_integer_type_node))
3897 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3898
3899 if (mode == TYPE_MODE (long_long_integer_type_node))
3900 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3901
9f75f026 3902 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3903 if (int_n_enabled_p[i]
3904 && mode == int_n_data[i].m)
3905 return (unsignedp ? int_n_trees[i].unsigned_type
3906 : int_n_trees[i].signed_type);
6388cfe2 3907
f57fa2ea 3908 if (mode == TYPE_MODE (widest_integer_literal_type_node))
44e9fa65 3909 return unsignedp ? widest_unsigned_literal_type_node
4ee9c684 3910 : widest_integer_literal_type_node;
f57fa2ea 3911
88ae7f04 3912 if (mode == QImode)
bacde65a 3913 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3914
88ae7f04 3915 if (mode == HImode)
bacde65a 3916 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3917
88ae7f04 3918 if (mode == SImode)
bacde65a 3919 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3920
88ae7f04 3921 if (mode == DImode)
bacde65a 3922 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
ab2c1de8 3923
cc1cc1c7 3924#if HOST_BITS_PER_WIDE_INT >= 64
6274009c 3925 if (mode == TYPE_MODE (intTI_type_node))
3926 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
cc1cc1c7 3927#endif
6274009c 3928
b0fc3e72 3929 if (mode == TYPE_MODE (float_type_node))
3930 return float_type_node;
3931
3932 if (mode == TYPE_MODE (double_type_node))
3933 return double_type_node;
3934
3935 if (mode == TYPE_MODE (long_double_type_node))
3936 return long_double_type_node;
3937
545c2bde 3938 if (mode == TYPE_MODE (void_type_node))
3939 return void_type_node;
b27ac6b5 3940
b0fc3e72 3941 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
61b9b73c 3942 return (unsignedp
3943 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3944 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3945
3946 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
61b9b73c 3947 return (unsignedp
3948 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3949 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3950
0dfc45b5 3951 if (COMPLEX_MODE_P (mode))
3952 {
3754d046 3953 machine_mode inner_mode;
0dfc45b5 3954 tree inner_type;
3955
3956 if (mode == TYPE_MODE (complex_float_type_node))
3957 return complex_float_type_node;
3958 if (mode == TYPE_MODE (complex_double_type_node))
3959 return complex_double_type_node;
3960 if (mode == TYPE_MODE (complex_long_double_type_node))
3961 return complex_long_double_type_node;
3962
3963 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3964 return complex_integer_type_node;
3965
3966 inner_mode = GET_MODE_INNER (mode);
3967 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3968 if (inner_type != NULL_TREE)
3969 return build_complex_type (inner_type);
3970 }
3971 else if (VECTOR_MODE_P (mode))
4917c376 3972 {
3754d046 3973 machine_mode inner_mode = GET_MODE_INNER (mode);
4917c376 3974 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3975 if (inner_type != NULL_TREE)
3976 return build_vector_type_for_mode (inner_type, mode);
88ae7f04 3977 }
e2ea7e3a 3978
c4503c0a 3979 if (mode == TYPE_MODE (dfloat32_type_node))
3980 return dfloat32_type_node;
3981 if (mode == TYPE_MODE (dfloat64_type_node))
3982 return dfloat64_type_node;
3983 if (mode == TYPE_MODE (dfloat128_type_node))
3984 return dfloat128_type_node;
3985
9421ebb9 3986 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3987 {
3988 if (mode == TYPE_MODE (short_fract_type_node))
3989 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3990 if (mode == TYPE_MODE (fract_type_node))
3991 return unsignedp ? sat_fract_type_node : fract_type_node;
3992 if (mode == TYPE_MODE (long_fract_type_node))
3993 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3994 if (mode == TYPE_MODE (long_long_fract_type_node))
3995 return unsignedp ? sat_long_long_fract_type_node
3996 : long_long_fract_type_node;
3997
3998 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3999 return unsignedp ? sat_unsigned_short_fract_type_node
4000 : unsigned_short_fract_type_node;
4001 if (mode == TYPE_MODE (unsigned_fract_type_node))
4002 return unsignedp ? sat_unsigned_fract_type_node
4003 : unsigned_fract_type_node;
4004 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
4005 return unsignedp ? sat_unsigned_long_fract_type_node
4006 : unsigned_long_fract_type_node;
4007 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
4008 return unsignedp ? sat_unsigned_long_long_fract_type_node
4009 : unsigned_long_long_fract_type_node;
4010
4011 if (mode == TYPE_MODE (short_accum_type_node))
4012 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
4013 if (mode == TYPE_MODE (accum_type_node))
4014 return unsignedp ? sat_accum_type_node : accum_type_node;
4015 if (mode == TYPE_MODE (long_accum_type_node))
4016 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
4017 if (mode == TYPE_MODE (long_long_accum_type_node))
4018 return unsignedp ? sat_long_long_accum_type_node
4019 : long_long_accum_type_node;
4020
4021 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
4022 return unsignedp ? sat_unsigned_short_accum_type_node
4023 : unsigned_short_accum_type_node;
4024 if (mode == TYPE_MODE (unsigned_accum_type_node))
4025 return unsignedp ? sat_unsigned_accum_type_node
4026 : unsigned_accum_type_node;
4027 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
4028 return unsignedp ? sat_unsigned_long_accum_type_node
4029 : unsigned_long_accum_type_node;
4030 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
4031 return unsignedp ? sat_unsigned_long_long_accum_type_node
4032 : unsigned_long_long_accum_type_node;
4033
4034 if (mode == QQmode)
4035 return unsignedp ? sat_qq_type_node : qq_type_node;
4036 if (mode == HQmode)
4037 return unsignedp ? sat_hq_type_node : hq_type_node;
4038 if (mode == SQmode)
4039 return unsignedp ? sat_sq_type_node : sq_type_node;
4040 if (mode == DQmode)
4041 return unsignedp ? sat_dq_type_node : dq_type_node;
4042 if (mode == TQmode)
4043 return unsignedp ? sat_tq_type_node : tq_type_node;
4044
4045 if (mode == UQQmode)
4046 return unsignedp ? sat_uqq_type_node : uqq_type_node;
4047 if (mode == UHQmode)
4048 return unsignedp ? sat_uhq_type_node : uhq_type_node;
4049 if (mode == USQmode)
4050 return unsignedp ? sat_usq_type_node : usq_type_node;
4051 if (mode == UDQmode)
4052 return unsignedp ? sat_udq_type_node : udq_type_node;
4053 if (mode == UTQmode)
4054 return unsignedp ? sat_utq_type_node : utq_type_node;
4055
4056 if (mode == HAmode)
4057 return unsignedp ? sat_ha_type_node : ha_type_node;
4058 if (mode == SAmode)
4059 return unsignedp ? sat_sa_type_node : sa_type_node;
4060 if (mode == DAmode)
4061 return unsignedp ? sat_da_type_node : da_type_node;
4062 if (mode == TAmode)
4063 return unsignedp ? sat_ta_type_node : ta_type_node;
4064
4065 if (mode == UHAmode)
4066 return unsignedp ? sat_uha_type_node : uha_type_node;
4067 if (mode == USAmode)
4068 return unsignedp ? sat_usa_type_node : usa_type_node;
4069 if (mode == UDAmode)
4070 return unsignedp ? sat_uda_type_node : uda_type_node;
4071 if (mode == UTAmode)
4072 return unsignedp ? sat_uta_type_node : uta_type_node;
4073 }
4074
5b247e9f 4075 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
baec58e1 4076 if (TYPE_MODE (TREE_VALUE (t)) == mode
4077 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
5b247e9f 4078 return TREE_VALUE (t);
4079
b0fc3e72 4080 return 0;
4081}
20d39783 4082
11773141 4083tree
4084c_common_unsigned_type (tree type)
4085{
4086 return c_common_signed_or_unsigned_type (1, type);
4087}
4088
20d39783 4089/* Return a signed type the same as TYPE in other respects. */
4090
4091tree
1cae46be 4092c_common_signed_type (tree type)
20d39783 4093{
4070745f 4094 return c_common_signed_or_unsigned_type (0, type);
20d39783 4095}
4096
4097/* Return a type the same as TYPE except unsigned or
4098 signed according to UNSIGNEDP. */
4099
4100tree
1cae46be 4101c_common_signed_or_unsigned_type (int unsignedp, tree type)
20d39783 4102{
7a91101f 4103 tree type1;
9f75f026 4104 int i;
20d39783 4105
7a91101f 4106 /* This block of code emulates the behavior of the old
4107 c_common_unsigned_type. In particular, it returns
4108 long_unsigned_type_node if passed a long, even when a int would
4109 have the same size. This is necessary for warnings to work
4110 correctly in archs where sizeof(int) == sizeof(long) */
4111
4112 type1 = TYPE_MAIN_VARIANT (type);
4113 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
4114 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4115 if (type1 == integer_type_node || type1 == unsigned_type_node)
4116 return unsignedp ? unsigned_type_node : integer_type_node;
4117 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
4118 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4119 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
4120 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4121 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
4122 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
9f75f026 4123
4124 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4125 if (int_n_enabled_p[i]
4126 && (type1 == int_n_trees[i].unsigned_type
4127 || type1 == int_n_trees[i].signed_type))
4128 return (unsignedp ? int_n_trees[i].unsigned_type
4129 : int_n_trees[i].signed_type);
4130
7a91101f 4131 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
4132 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
4133#if HOST_BITS_PER_WIDE_INT >= 64
4134 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
4135 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4136#endif
4137 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
4138 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4139 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
4140 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4141 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
4142 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4143 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
4144 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4145
9f92e1a2 4146#define C_COMMON_FIXED_TYPES(NAME) \
4147 if (type1 == short_ ## NAME ## _type_node \
4148 || type1 == unsigned_short_ ## NAME ## _type_node) \
4149 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
4150 : short_ ## NAME ## _type_node; \
4151 if (type1 == NAME ## _type_node \
4152 || type1 == unsigned_ ## NAME ## _type_node) \
4153 return unsignedp ? unsigned_ ## NAME ## _type_node \
4154 : NAME ## _type_node; \
4155 if (type1 == long_ ## NAME ## _type_node \
4156 || type1 == unsigned_long_ ## NAME ## _type_node) \
4157 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
4158 : long_ ## NAME ## _type_node; \
4159 if (type1 == long_long_ ## NAME ## _type_node \
4160 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
4161 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
4162 : long_long_ ## NAME ## _type_node;
4163
4164#define C_COMMON_FIXED_MODE_TYPES(NAME) \
4165 if (type1 == NAME ## _type_node \
4166 || type1 == u ## NAME ## _type_node) \
4167 return unsignedp ? u ## NAME ## _type_node \
4168 : NAME ## _type_node;
4169
4170#define C_COMMON_FIXED_TYPES_SAT(NAME) \
4171 if (type1 == sat_ ## short_ ## NAME ## _type_node \
4172 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
4173 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
4174 : sat_ ## short_ ## NAME ## _type_node; \
4175 if (type1 == sat_ ## NAME ## _type_node \
4176 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
4177 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
4178 : sat_ ## NAME ## _type_node; \
4179 if (type1 == sat_ ## long_ ## NAME ## _type_node \
4180 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
4181 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
4182 : sat_ ## long_ ## NAME ## _type_node; \
4183 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
4184 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
4185 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
4186 : sat_ ## long_long_ ## NAME ## _type_node;
4187
4188#define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
4189 if (type1 == sat_ ## NAME ## _type_node \
4190 || type1 == sat_ ## u ## NAME ## _type_node) \
4191 return unsignedp ? sat_ ## u ## NAME ## _type_node \
4192 : sat_ ## NAME ## _type_node;
4193
4194 C_COMMON_FIXED_TYPES (fract);
4195 C_COMMON_FIXED_TYPES_SAT (fract);
4196 C_COMMON_FIXED_TYPES (accum);
4197 C_COMMON_FIXED_TYPES_SAT (accum);
4198
4199 C_COMMON_FIXED_MODE_TYPES (qq);
4200 C_COMMON_FIXED_MODE_TYPES (hq);
4201 C_COMMON_FIXED_MODE_TYPES (sq);
4202 C_COMMON_FIXED_MODE_TYPES (dq);
4203 C_COMMON_FIXED_MODE_TYPES (tq);
4204 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
4205 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
4206 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
4207 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
4208 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
4209 C_COMMON_FIXED_MODE_TYPES (ha);
4210 C_COMMON_FIXED_MODE_TYPES (sa);
4211 C_COMMON_FIXED_MODE_TYPES (da);
4212 C_COMMON_FIXED_MODE_TYPES (ta);
4213 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
4214 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
4215 C_COMMON_FIXED_MODE_TYPES_SAT (da);
4216 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
9421ebb9 4217
4f7f7efd 4218 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
4219 the precision; they have precision set to match their range, but
4220 may use a wider mode to match an ABI. If we change modes, we may
4221 wind up with bad conversions. For INTEGER_TYPEs in C, must check
4222 the precision as well, so as to yield correct results for
4223 bit-field types. C++ does not have these separate bit-field
4224 types, and producing a signed or unsigned variant of an
4225 ENUMERAL_TYPE may cause other problems as well. */
4226
ac265864 4227 if (!INTEGRAL_TYPE_P (type)
4228 || TYPE_UNSIGNED (type) == unsignedp)
4229 return type;
4230
4f7f7efd 4231#define TYPE_OK(node) \
4232 (TYPE_MODE (type) == TYPE_MODE (node) \
0c4abe5b 4233 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
4f7f7efd 4234 if (TYPE_OK (signed_char_type_node))
20d39783 4235 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4f7f7efd 4236 if (TYPE_OK (integer_type_node))
20d39783 4237 return unsignedp ? unsigned_type_node : integer_type_node;
4f7f7efd 4238 if (TYPE_OK (short_integer_type_node))
20d39783 4239 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4f7f7efd 4240 if (TYPE_OK (long_integer_type_node))
20d39783 4241 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4f7f7efd 4242 if (TYPE_OK (long_long_integer_type_node))
20d39783 4243 return (unsignedp ? long_long_unsigned_type_node
4244 : long_long_integer_type_node);
9f75f026 4245
4246 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4247 if (int_n_enabled_p[i]
4248 && TYPE_MODE (type) == int_n_data[i].m
4249 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
4250 return (unsignedp ? int_n_trees[i].unsigned_type
4251 : int_n_trees[i].signed_type);
4252
4f7f7efd 4253 if (TYPE_OK (widest_integer_literal_type_node))
20d39783 4254 return (unsignedp ? widest_unsigned_literal_type_node
4255 : widest_integer_literal_type_node);
ef11801e 4256
4257#if HOST_BITS_PER_WIDE_INT >= 64
4f7f7efd 4258 if (TYPE_OK (intTI_type_node))
ef11801e 4259 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4260#endif
4f7f7efd 4261 if (TYPE_OK (intDI_type_node))
ef11801e 4262 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4f7f7efd 4263 if (TYPE_OK (intSI_type_node))
ef11801e 4264 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4f7f7efd 4265 if (TYPE_OK (intHI_type_node))
ef11801e 4266 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4f7f7efd 4267 if (TYPE_OK (intQI_type_node))
ef11801e 4268 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4f7f7efd 4269#undef TYPE_OK
ef11801e 4270
0c4abe5b 4271 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
20d39783 4272}
b268e47e 4273
c0e47fd4 4274/* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
4275
4276tree
4277c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
4278{
9f75f026 4279 int i;
4280
c0e47fd4 4281 /* Extended integer types of the same width as a standard type have
4282 lesser rank, so those of the same width as int promote to int or
4283 unsigned int and are valid for printf formats expecting int or
4284 unsigned int. To avoid such special cases, avoid creating
4285 extended integer types for bit-fields if a standard integer type
4286 is available. */
4287 if (width == TYPE_PRECISION (integer_type_node))
4288 return unsignedp ? unsigned_type_node : integer_type_node;
4289 if (width == TYPE_PRECISION (signed_char_type_node))
4290 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4291 if (width == TYPE_PRECISION (short_integer_type_node))
4292 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4293 if (width == TYPE_PRECISION (long_integer_type_node))
4294 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4295 if (width == TYPE_PRECISION (long_long_integer_type_node))
4296 return (unsignedp ? long_long_unsigned_type_node
4297 : long_long_integer_type_node);
9f75f026 4298 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4299 if (int_n_enabled_p[i]
4300 && width == int_n_data[i].bitsize)
4301 return (unsignedp ? int_n_trees[i].unsigned_type
4302 : int_n_trees[i].signed_type);
c0e47fd4 4303 return build_nonstandard_integer_type (width, unsignedp);
4304}
4305
b268e47e 4306/* The C version of the register_builtin_type langhook. */
4307
4308void
4309c_register_builtin_type (tree type, const char* name)
4310{
4311 tree decl;
4312
e60a6f7b 4313 decl = build_decl (UNKNOWN_LOCATION,
4314 TYPE_DECL, get_identifier (name), type);
b268e47e 4315 DECL_ARTIFICIAL (decl) = 1;
4316 if (!TYPE_NAME (type))
4317 TYPE_NAME (type) = decl;
4318 pushdecl (decl);
5b247e9f 4319
4320 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
b268e47e 4321}
a9b9d10c 4322\f
aff9e656 4323/* Print an error message for invalid operands to arith operation
8e70fb09 4324 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4325 LOCATION is the location of the message. */
b0fc3e72 4326
4327void
8e70fb09 4328binary_op_error (location_t location, enum tree_code code,
4329 tree type0, tree type1)
b0fc3e72 4330{
19cb6b50 4331 const char *opname;
f03946e4 4332
b0fc3e72 4333 switch (code)
4334 {
b0fc3e72 4335 case PLUS_EXPR:
4336 opname = "+"; break;
4337 case MINUS_EXPR:
4338 opname = "-"; break;
4339 case MULT_EXPR:
4340 opname = "*"; break;
4341 case MAX_EXPR:
4342 opname = "max"; break;
4343 case MIN_EXPR:
4344 opname = "min"; break;
4345 case EQ_EXPR:
4346 opname = "=="; break;
4347 case NE_EXPR:
4348 opname = "!="; break;
4349 case LE_EXPR:
4350 opname = "<="; break;
4351 case GE_EXPR:
4352 opname = ">="; break;
4353 case LT_EXPR:
4354 opname = "<"; break;
4355 case GT_EXPR:
4356 opname = ">"; break;
4357 case LSHIFT_EXPR:
4358 opname = "<<"; break;
4359 case RSHIFT_EXPR:
4360 opname = ">>"; break;
4361 case TRUNC_MOD_EXPR:
66618a1e 4362 case FLOOR_MOD_EXPR:
b0fc3e72 4363 opname = "%"; break;
4364 case TRUNC_DIV_EXPR:
66618a1e 4365 case FLOOR_DIV_EXPR:
b0fc3e72 4366 opname = "/"; break;
4367 case BIT_AND_EXPR:
4368 opname = "&"; break;
4369 case BIT_IOR_EXPR:
4370 opname = "|"; break;
4371 case TRUTH_ANDIF_EXPR:
4372 opname = "&&"; break;
4373 case TRUTH_ORIF_EXPR:
4374 opname = "||"; break;
4375 case BIT_XOR_EXPR:
4376 opname = "^"; break;
31f820d2 4377 default:
315ba355 4378 gcc_unreachable ();
b0fc3e72 4379 }
8e70fb09 4380 error_at (location,
4381 "invalid operands to binary %s (have %qT and %qT)", opname,
4382 type0, type1);
b0fc3e72 4383}
4384\f
03fe1dc2 4385/* Given an expression as a tree, return its original type. Do this
4386 by stripping any conversion that preserves the sign and precision. */
4387static tree
4388expr_original_type (tree expr)
4389{
4390 STRIP_SIGN_NOPS (expr);
4391 return TREE_TYPE (expr);
4392}
4393
b0fc3e72 4394/* Subroutine of build_binary_op, used for comparison operations.
4395 See if the operands have both been converted from subword integer types
4396 and, if so, perhaps change them both back to their original type.
5b511807 4397 This function is also responsible for converting the two operands
4398 to the proper common type for comparison.
b0fc3e72 4399
4400 The arguments of this function are all pointers to local variables
4401 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4402 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4403
2623625f 4404 LOC is the location of the comparison.
4405
b0fc3e72 4406 If this function returns nonzero, it means that the comparison has
4407 a constant value. What this function returns is an expression for
4408 that value. */
4409
4410tree
2623625f 4411shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4412 tree *restype_ptr, enum tree_code *rescode_ptr)
b0fc3e72 4413{
19cb6b50 4414 tree type;
b0fc3e72 4415 tree op0 = *op0_ptr;
4416 tree op1 = *op1_ptr;
4417 int unsignedp0, unsignedp1;
4418 int real1, real2;
4419 tree primop0, primop1;
4420 enum tree_code code = *rescode_ptr;
4421
4422 /* Throw away any conversions to wider types
4423 already present in the operands. */
4424
7f506bca 4425 primop0 = c_common_get_narrower (op0, &unsignedp0);
4426 primop1 = c_common_get_narrower (op1, &unsignedp1);
b0fc3e72 4427
119d06b2 4428 /* If primopN is first sign-extended from primopN's precision to opN's
4429 precision, then zero-extended from opN's precision to
4430 *restype_ptr precision, shortenings might be invalid. */
4431 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4432 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4433 && !unsignedp0
4434 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4435 primop0 = op0;
4436 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4437 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4438 && !unsignedp1
4439 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4440 primop1 = op1;
4441
b0fc3e72 4442 /* Handle the case that OP0 does not *contain* a conversion
4443 but it *requires* conversion to FINAL_TYPE. */
4444
4445 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
78a8ed03 4446 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
b0fc3e72 4447 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
78a8ed03 4448 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b0fc3e72 4449
4450 /* If one of the operands must be floated, we cannot optimize. */
4451 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4452 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
ab2c1de8 4453
b0fc3e72 4454 /* If first arg is constant, swap the args (changing operation
2bd278cc 4455 so value is preserved), for canonicalization. Don't do this if
4456 the second arg is 0. */
b0fc3e72 4457
2bd278cc 4458 if (TREE_CONSTANT (primop0)
9421ebb9 4459 && !integer_zerop (primop1) && !real_zerop (primop1)
4460 && !fixed_zerop (primop1))
b0fc3e72 4461 {
dfcf26a5 4462 std::swap (primop0, primop1);
4463 std::swap (op0, op1);
b0fc3e72 4464 *op0_ptr = op0;
4465 *op1_ptr = op1;
dfcf26a5 4466 std::swap (unsignedp0, unsignedp1);
4467 std::swap (real1, real2);
b0fc3e72 4468
4469 switch (code)
4470 {
4471 case LT_EXPR:
4472 code = GT_EXPR;
4473 break;
4474 case GT_EXPR:
4475 code = LT_EXPR;
4476 break;
4477 case LE_EXPR:
4478 code = GE_EXPR;
4479 break;
4480 case GE_EXPR:
4481 code = LE_EXPR;
4482 break;
31f820d2 4483 default:
4484 break;
b0fc3e72 4485 }
4486 *rescode_ptr = code;
4487 }
4488
4489 /* If comparing an integer against a constant more bits wide,
4490 maybe we can deduce a value of 1 or 0 independent of the data.
4491 Or else truncate the constant now
4492 rather than extend the variable at run time.
4493
4494 This is only interesting if the constant is the wider arg.
4495 Also, it is not safe if the constant is unsigned and the
4496 variable arg is signed, since in this case the variable
4497 would be sign-extended and then regarded as unsigned.
4498 Our technique fails in this case because the lowest/highest
4499 possible unsigned results don't follow naturally from the
4500 lowest/highest possible values of the variable operand.
4501 For just EQ_EXPR and NE_EXPR there is another technique that
4502 could be used: see if the constant can be faithfully represented
4503 in the other operand's type, by truncating it and reextending it
4504 and see if that preserves the constant's value. */
4505
4506 if (!real1 && !real2
9421ebb9 4507 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
b0fc3e72 4508 && TREE_CODE (primop1) == INTEGER_CST
4509 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4510 {
4511 int min_gt, max_gt, min_lt, max_lt;
4512 tree maxval, minval;
4513 /* 1 if comparison is nominally unsigned. */
78a8ed03 4514 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
b0fc3e72 4515 tree val;
4516
4070745f 4517 type = c_common_signed_or_unsigned_type (unsignedp0,
4518 TREE_TYPE (primop0));
cda09c61 4519
b0fc3e72 4520 maxval = TYPE_MAX_VALUE (type);
4521 minval = TYPE_MIN_VALUE (type);
4522
4523 if (unsignedp && !unsignedp0)
4070745f 4524 *restype_ptr = c_common_signed_type (*restype_ptr);
b0fc3e72 4525
4526 if (TREE_TYPE (primop1) != *restype_ptr)
18dbec6f 4527 {
9a5e8086 4528 /* Convert primop1 to target type, but do not introduce
4529 additional overflow. We know primop1 is an int_cst. */
e913b5cd 4530 primop1 = force_fit_type (*restype_ptr,
796b6678 4531 wide_int::from
4532 (primop1,
4533 TYPE_PRECISION (*restype_ptr),
4534 TYPE_SIGN (TREE_TYPE (primop1))),
e913b5cd 4535 0, TREE_OVERFLOW (primop1));
18dbec6f 4536 }
b0fc3e72 4537 if (type != *restype_ptr)
4538 {
4539 minval = convert (*restype_ptr, minval);
4540 maxval = convert (*restype_ptr, maxval);
4541 }
4542
d99d10ca 4543 min_gt = tree_int_cst_lt (primop1, minval);
4544 max_gt = tree_int_cst_lt (primop1, maxval);
4545 min_lt = tree_int_cst_lt (minval, primop1);
4546 max_lt = tree_int_cst_lt (maxval, primop1);
b0fc3e72 4547
4548 val = 0;
4549 /* This used to be a switch, but Genix compiler can't handle that. */
4550 if (code == NE_EXPR)
4551 {
4552 if (max_lt || min_gt)
3c2239cf 4553 val = truthvalue_true_node;
b0fc3e72 4554 }
4555 else if (code == EQ_EXPR)
4556 {
4557 if (max_lt || min_gt)
3c2239cf 4558 val = truthvalue_false_node;
b0fc3e72 4559 }
4560 else if (code == LT_EXPR)
4561 {
4562 if (max_lt)
3c2239cf 4563 val = truthvalue_true_node;
b0fc3e72 4564 if (!min_lt)
3c2239cf 4565 val = truthvalue_false_node;
b0fc3e72 4566 }
4567 else if (code == GT_EXPR)
4568 {
4569 if (min_gt)
3c2239cf 4570 val = truthvalue_true_node;
b0fc3e72 4571 if (!max_gt)
3c2239cf 4572 val = truthvalue_false_node;
b0fc3e72 4573 }
4574 else if (code == LE_EXPR)
4575 {
4576 if (!max_gt)
3c2239cf 4577 val = truthvalue_true_node;
b0fc3e72 4578 if (min_gt)
3c2239cf 4579 val = truthvalue_false_node;
b0fc3e72 4580 }
4581 else if (code == GE_EXPR)
4582 {
4583 if (!min_lt)
3c2239cf 4584 val = truthvalue_true_node;
b0fc3e72 4585 if (max_lt)
3c2239cf 4586 val = truthvalue_false_node;
b0fc3e72 4587 }
4588
4589 /* If primop0 was sign-extended and unsigned comparison specd,
4590 we did a signed comparison above using the signed type bounds.
4591 But the comparison we output must be unsigned.
4592
4593 Also, for inequalities, VAL is no good; but if the signed
4594 comparison had *any* fixed result, it follows that the
4595 unsigned comparison just tests the sign in reverse
4596 (positive values are LE, negative ones GE).
4597 So we can generate an unsigned comparison
4598 against an extreme value of the signed type. */
4599
4600 if (unsignedp && !unsignedp0)
4601 {
4602 if (val != 0)
4603 switch (code)
4604 {
4605 case LT_EXPR:
4606 case GE_EXPR:
4607 primop1 = TYPE_MIN_VALUE (type);
4608 val = 0;
4609 break;
4610
4611 case LE_EXPR:
4612 case GT_EXPR:
4613 primop1 = TYPE_MAX_VALUE (type);
4614 val = 0;
4615 break;
31f820d2 4616
4617 default:
4618 break;
b0fc3e72 4619 }
11773141 4620 type = c_common_unsigned_type (type);
b0fc3e72 4621 }
4622
734ec290 4623 if (TREE_CODE (primop0) != INTEGER_CST)
b0fc3e72 4624 {
3c2239cf 4625 if (val == truthvalue_false_node)
03fe1dc2 4626 warning_at (loc, OPT_Wtype_limits,
4627 "comparison is always false due to limited range of data type");
3c2239cf 4628 if (val == truthvalue_true_node)
03fe1dc2 4629 warning_at (loc, OPT_Wtype_limits,
4630 "comparison is always true due to limited range of data type");
b0fc3e72 4631 }
4632
4633 if (val != 0)
4634 {
4635 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4636 if (TREE_SIDE_EFFECTS (primop0))
14ae0310 4637 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
b0fc3e72 4638 return val;
4639 }
4640
4641 /* Value is not predetermined, but do the comparison
4642 in the type of the operand that is not constant.
4643 TYPE is already properly set. */
4644 }
c4503c0a 4645
4646 /* If either arg is decimal float and the other is float, find the
4647 proper common type to use for comparison. */
7fd22aae 4648 else if (real1 && real2
4649 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4650 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4651 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4652
4653 /* If either arg is decimal float and the other is float, fail. */
c4503c0a 4654 else if (real1 && real2
4655 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4656 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
7fd22aae 4657 return 0;
c4503c0a 4658
b0fc3e72 4659 else if (real1 && real2
2203bd5c 4660 && (TYPE_PRECISION (TREE_TYPE (primop0))
4661 == TYPE_PRECISION (TREE_TYPE (primop1))))
b0fc3e72 4662 type = TREE_TYPE (primop0);
4663
4664 /* If args' natural types are both narrower than nominal type
4665 and both extend in the same manner, compare them
4666 in the type of the wider arg.
4667 Otherwise must actually extend both to the nominal
4668 common type lest different ways of extending
4669 alter the result.
4670 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4671
4672 else if (unsignedp0 == unsignedp1 && real1 == real2
4673 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4674 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4675 {
4676 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4070745f 4677 type = c_common_signed_or_unsigned_type (unsignedp0
78a8ed03 4678 || TYPE_UNSIGNED (*restype_ptr),
4070745f 4679 type);
b0fc3e72 4680 /* Make sure shorter operand is extended the right way
4681 to match the longer operand. */
4070745f 4682 primop0
4683 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4684 TREE_TYPE (primop0)),
4685 primop0);
4686 primop1
4687 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4688 TREE_TYPE (primop1)),
4689 primop1);
b0fc3e72 4690 }
4691 else
4692 {
4693 /* Here we must do the comparison on the nominal type
4694 using the args exactly as we received them. */
4695 type = *restype_ptr;
4696 primop0 = op0;
4697 primop1 = op1;
4698
4699 if (!real1 && !real2 && integer_zerop (primop1)
78a8ed03 4700 && TYPE_UNSIGNED (*restype_ptr))
b0fc3e72 4701 {
4702 tree value = 0;
03fe1dc2 4703 /* All unsigned values are >= 0, so we warn. However,
4704 if OP0 is a constant that is >= 0, the signedness of
4705 the comparison isn't an issue, so suppress the
4706 warning. */
4707 bool warn =
3df42822 4708 warn_type_limits && !in_system_header_at (loc)
03fe1dc2 4709 && !(TREE_CODE (primop0) == INTEGER_CST
4710 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4711 primop0)))
4712 /* Do not warn for enumeration types. */
4713 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4714
b0fc3e72 4715 switch (code)
4716 {
4717 case GE_EXPR:
03fe1dc2 4718 if (warn)
4719 warning_at (loc, OPT_Wtype_limits,
4720 "comparison of unsigned expression >= 0 is always true");
3c2239cf 4721 value = truthvalue_true_node;
b0fc3e72 4722 break;
4723
4724 case LT_EXPR:
03fe1dc2 4725 if (warn)
4726 warning_at (loc, OPT_Wtype_limits,
4727 "comparison of unsigned expression < 0 is always false");
3c2239cf 4728 value = truthvalue_false_node;
31f820d2 4729 break;
4730
4731 default:
4732 break;
b0fc3e72 4733 }
4734
4735 if (value != 0)
4736 {
4737 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4738 if (TREE_SIDE_EFFECTS (primop0))
14ae0310 4739 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4740 primop0, value);
b0fc3e72 4741 return value;
4742 }
4743 }
4744 }
4745
4746 *op0_ptr = convert (type, primop0);
4747 *op1_ptr = convert (type, primop1);
4748
3c2239cf 4749 *restype_ptr = truthvalue_type_node;
b0fc3e72 4750
4751 return 0;
4752}
4753\f
1c26100f 4754/* Return a tree for the sum or difference (RESULTCODE says which)
4755 of pointer PTROP and integer INTOP. */
4756
4757tree
389dd41b 4758pointer_int_sum (location_t loc, enum tree_code resultcode,
42f9a786 4759 tree ptrop, tree intop, bool complain)
1c26100f 4760{
add6ee5e 4761 tree size_exp, ret;
1c26100f 4762
1c26100f 4763 /* The result is a pointer of the same type that is being added. */
1c26100f 4764 tree result_type = TREE_TYPE (ptrop);
4765
4766 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4767 {
42f9a786 4768 if (complain && warn_pointer_arith)
4769 pedwarn (loc, OPT_Wpointer_arith,
4770 "pointer of type %<void *%> used in arithmetic");
4771 else if (!complain)
4772 return error_mark_node;
1c26100f 4773 size_exp = integer_one_node;
4774 }
4775 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4776 {
42f9a786 4777 if (complain && warn_pointer_arith)
4778 pedwarn (loc, OPT_Wpointer_arith,
4779 "pointer to a function used in arithmetic");
4780 else if (!complain)
4781 return error_mark_node;
1c26100f 4782 size_exp = integer_one_node;
4783 }
1c26100f 4784 else
4785 size_exp = size_in_bytes (TREE_TYPE (result_type));
4786
add6ee5e 4787 /* We are manipulating pointer values, so we don't need to warn
4788 about relying on undefined signed overflow. We disable the
4789 warning here because we use integer types so fold won't know that
4790 they are really pointers. */
4791 fold_defer_overflow_warnings ();
4792
1c26100f 4793 /* If what we are about to multiply by the size of the elements
4794 contains a constant term, apply distributive law
4795 and multiply that constant term separately.
4796 This helps produce common subexpressions. */
1c26100f 4797 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
84166705 4798 && !TREE_CONSTANT (intop)
1c26100f 4799 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4800 && TREE_CONSTANT (size_exp)
4801 /* If the constant comes from pointer subtraction,
4802 skip this optimization--it would cause an error. */
4803 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4804 /* If the constant is unsigned, and smaller than the pointer size,
4805 then we must skip this optimization. This is because it could cause
4806 an overflow error if the constant is negative but INTOP is not. */
84166705 4807 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
1c26100f 4808 || (TYPE_PRECISION (TREE_TYPE (intop))
4809 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4810 {
4811 enum tree_code subcode = resultcode;
4812 tree int_type = TREE_TYPE (intop);
4813 if (TREE_CODE (intop) == MINUS_EXPR)
4814 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4815 /* Convert both subexpression types to the type of intop,
4816 because weird cases involving pointer arithmetic
4817 can result in a sum or difference with different type args. */
8e70fb09 4818 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4819 subcode, ptrop,
1c26100f 4820 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4821 intop = convert (int_type, TREE_OPERAND (intop, 0));
4822 }
4823
4824 /* Convert the integer argument to a type the same size as sizetype
4825 so the multiply won't overflow spuriously. */
1c26100f 4826 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
78a8ed03 4827 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
1cae46be 4828 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
78a8ed03 4829 TYPE_UNSIGNED (sizetype)), intop);
1c26100f 4830
4831 /* Replace the integer argument with a suitable product by the object size.
c6feb9f1 4832 Do this multiplication as signed, then convert to the appropriate type
8032877c 4833 for the pointer operation and disregard an overflow that occurred only
c6feb9f1 4834 because of the sign-extension change in the latter conversion. */
4835 {
4836 tree t = build_binary_op (loc,
4837 MULT_EXPR, intop,
4838 convert (TREE_TYPE (intop), size_exp), 1);
4839 intop = convert (sizetype, t);
4840 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
e913b5cd 4841 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
c6feb9f1 4842 }
0de36bdb 4843
499e523f 4844 /* Create the sum or difference. */
0de36bdb 4845 if (resultcode == MINUS_EXPR)
389dd41b 4846 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
1c26100f 4847
2cc66f2a 4848 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
add6ee5e 4849
4850 fold_undefer_and_ignore_overflow_warnings ();
4851
4852 return ret;
1c26100f 4853}
4854\f
f59e3889 4855/* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4856 and if NON_CONST is known not to be permitted in an evaluated part
4857 of a constant expression. */
4858
4859tree
4860c_wrap_maybe_const (tree expr, bool non_const)
4861{
4862 bool nowarning = TREE_NO_WARNING (expr);
4863 location_t loc = EXPR_LOCATION (expr);
4864
4865 /* This should never be called for C++. */
4866 if (c_dialect_cxx ())
4867 gcc_unreachable ();
4868
4869 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4870 STRIP_TYPE_NOPS (expr);
4871 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4872 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4873 if (nowarning)
4874 TREE_NO_WARNING (expr) = 1;
4875 protected_set_expr_location (expr, loc);
4876
4877 return expr;
4878}
4879
a75b1c71 4880/* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4881 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4882 around the SAVE_EXPR if needed so that c_fully_fold does not need
4883 to look inside SAVE_EXPRs. */
4884
4885tree
4886c_save_expr (tree expr)
4887{
4888 bool maybe_const = true;
4889 if (c_dialect_cxx ())
4890 return save_expr (expr);
4891 expr = c_fully_fold (expr, false, &maybe_const);
4892 expr = save_expr (expr);
4893 if (!maybe_const)
f59e3889 4894 expr = c_wrap_maybe_const (expr, true);
a75b1c71 4895 return expr;
4896}
4897
6b68e71a 4898/* Return whether EXPR is a declaration whose address can never be
4899 NULL. */
4900
4901bool
9f627b1a 4902decl_with_nonnull_addr_p (const_tree expr)
6b68e71a 4903{
4904 return (DECL_P (expr)
4905 && (TREE_CODE (expr) == PARM_DECL
4906 || TREE_CODE (expr) == LABEL_DECL
4907 || !DECL_WEAK (expr)));
4908}
4909
b0fc3e72 4910/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
45a78cc0 4911 or for an `if' or `while' statement or ?..: exp. It should already
4912 have been validated to be of suitable type; otherwise, a bad
4913 diagnostic may result.
b0fc3e72 4914
8e70fb09 4915 The EXPR is located at LOCATION.
4916
b0fc3e72 4917 This preparation consists of taking the ordinary
4918 representation of an expression expr and producing a valid tree
4919 boolean expression describing whether expr is nonzero. We could
3c2239cf 4920 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
b0fc3e72 4921 but we optimize comparisons, &&, ||, and !.
4922
3c2239cf 4923 The resulting type should always be `truthvalue_type_node'. */
b0fc3e72 4924
4925tree
8e70fb09 4926c_common_truthvalue_conversion (location_t location, tree expr)
b0fc3e72 4927{
b0fc3e72 4928 switch (TREE_CODE (expr))
4929 {
318a728f 4930 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
e1232ce2 4931 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4932 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4933 case ORDERED_EXPR: case UNORDERED_EXPR:
ce04dcdc 4934 if (TREE_TYPE (expr) == truthvalue_type_node)
4935 return expr;
e60a6f7b 4936 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
ce04dcdc 4937 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
e60a6f7b 4938 goto ret;
ce04dcdc 4939
b0fc3e72 4940 case TRUTH_ANDIF_EXPR:
4941 case TRUTH_ORIF_EXPR:
4942 case TRUTH_AND_EXPR:
4943 case TRUTH_OR_EXPR:
31f6e93c 4944 case TRUTH_XOR_EXPR:
ce04dcdc 4945 if (TREE_TYPE (expr) == truthvalue_type_node)
4946 return expr;
e60a6f7b 4947 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
48e1416a 4948 c_common_truthvalue_conversion (location,
e60a6f7b 4949 TREE_OPERAND (expr, 0)),
4950 c_common_truthvalue_conversion (location,
4951 TREE_OPERAND (expr, 1)));
4952 goto ret;
3e851b85 4953
f6e28f72 4954 case TRUTH_NOT_EXPR:
ce04dcdc 4955 if (TREE_TYPE (expr) == truthvalue_type_node)
4956 return expr;
e60a6f7b 4957 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4958 c_common_truthvalue_conversion (location,
4959 TREE_OPERAND (expr, 0)));
4960 goto ret;
f6e28f72 4961
b0fc3e72 4962 case ERROR_MARK:
4963 return expr;
ab2c1de8 4964
b0fc3e72 4965 case INTEGER_CST:
eddad94a 4966 return integer_zerop (expr) ? truthvalue_false_node
4967 : truthvalue_true_node;
b0fc3e72 4968
4969 case REAL_CST:
5000e21c 4970 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4971 ? truthvalue_true_node
4972 : truthvalue_false_node;
b0fc3e72 4973
9421ebb9 4974 case FIXED_CST:
4975 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4976 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4977 ? truthvalue_true_node
4978 : truthvalue_false_node;
4979
ce04dcdc 4980 case FUNCTION_DECL:
b6889cb0 4981 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
ce04dcdc 4982 /* Fall through. */
4983
b0fc3e72 4984 case ADDR_EXPR:
ce871053 4985 {
ee7d4d6a 4986 tree inner = TREE_OPERAND (expr, 0);
6b68e71a 4987 if (decl_with_nonnull_addr_p (inner))
ce871053 4988 {
6b68e71a 4989 /* Common Ada/Pascal programmer's mistake. */
8e70fb09 4990 warning_at (location,
4991 OPT_Waddress,
4992 "the address of %qD will always evaluate as %<true%>",
4993 inner);
ce871053 4994 return truthvalue_true_node;
4995 }
d473d901 4996 break;
ce871053 4997 }
b0fc3e72 4998
2203bd5c 4999 case COMPLEX_EXPR:
e60a6f7b 5000 expr = build_binary_op (EXPR_LOCATION (expr),
8e70fb09 5001 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
95809de4 5002 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
8e70fb09 5003 c_common_truthvalue_conversion (location,
5004 TREE_OPERAND (expr, 0)),
5005 c_common_truthvalue_conversion (location,
5006 TREE_OPERAND (expr, 1)),
2203bd5c 5007 0);
e60a6f7b 5008 goto ret;
2203bd5c 5009
b0fc3e72 5010 case NEGATE_EXPR:
5011 case ABS_EXPR:
5012 case FLOAT_EXPR:
c6418a4e 5013 case EXCESS_PRECISION_EXPR:
d10cfa8d 5014 /* These don't change whether an object is nonzero or zero. */
8e70fb09 5015 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
b0fc3e72 5016
5017 case LROTATE_EXPR:
5018 case RROTATE_EXPR:
d10cfa8d 5019 /* These don't change whether an object is zero or nonzero, but
b0fc3e72 5020 we can't ignore them if their second arg has side-effects. */
5021 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
e60a6f7b 5022 {
5023 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
5024 TREE_OPERAND (expr, 1),
48e1416a 5025 c_common_truthvalue_conversion
e60a6f7b 5026 (location, TREE_OPERAND (expr, 0)));
5027 goto ret;
5028 }
b0fc3e72 5029 else
8e70fb09 5030 return c_common_truthvalue_conversion (location,
5031 TREE_OPERAND (expr, 0));
73be5127 5032
b0fc3e72 5033 case COND_EXPR:
5034 /* Distribute the conversion into the arms of a COND_EXPR. */
a75b1c71 5035 if (c_dialect_cxx ())
e60a6f7b 5036 {
d0389adc 5037 tree op1 = TREE_OPERAND (expr, 1);
5038 tree op2 = TREE_OPERAND (expr, 2);
5039 /* In C++ one of the arms might have void type if it is throw. */
5040 if (!VOID_TYPE_P (TREE_TYPE (op1)))
5041 op1 = c_common_truthvalue_conversion (location, op1);
5042 if (!VOID_TYPE_P (TREE_TYPE (op2)))
5043 op2 = c_common_truthvalue_conversion (location, op2);
389dd41b 5044 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
d0389adc 5045 TREE_OPERAND (expr, 0), op1, op2);
e60a6f7b 5046 goto ret;
5047 }
a75b1c71 5048 else
e60a6f7b 5049 {
5050 /* Folding will happen later for C. */
5051 expr = build3 (COND_EXPR, truthvalue_type_node,
5052 TREE_OPERAND (expr, 0),
5053 c_common_truthvalue_conversion (location,
5054 TREE_OPERAND (expr, 1)),
5055 c_common_truthvalue_conversion (location,
5056 TREE_OPERAND (expr, 2)));
5057 goto ret;
5058 }
b0fc3e72 5059
72dd6141 5060 CASE_CONVERT:
f8913d47 5061 {
5062 tree totype = TREE_TYPE (expr);
5063 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
5064
db3d1ffc 5065 if (POINTER_TYPE_P (totype)
5066 && TREE_CODE (fromtype) == REFERENCE_TYPE)
5067 {
5068 tree inner = expr;
5069 STRIP_NOPS (inner);
5070
5071 if (DECL_P (inner))
5072 warning_at (location,
5073 OPT_Waddress,
5074 "the compiler can assume that the address of "
5075 "%qD will always evaluate to %<true%>",
5076 inner);
5077 }
5078
f8913d47 5079 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
5080 since that affects how `default_conversion' will behave. */
5081 if (TREE_CODE (totype) == REFERENCE_TYPE
5082 || TREE_CODE (fromtype) == REFERENCE_TYPE)
5083 break;
5084 /* Don't strip a conversion from C++0x scoped enum, since they
5085 don't implicitly convert to other types. */
5086 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
5087 && ENUM_IS_SCOPED (fromtype))
5088 break;
5089 /* If this isn't narrowing the argument, we can ignore it. */
5090 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
5091 return c_common_truthvalue_conversion (location,
5092 TREE_OPERAND (expr, 0));
5093 }
b0fc3e72 5094 break;
5095
16837b18 5096 case MODIFY_EXPR:
60a0513e 5097 if (!TREE_NO_WARNING (expr)
5098 && warn_parentheses)
5099 {
5100 warning (OPT_Wparentheses,
5101 "suggest parentheses around assignment used as truth value");
5102 TREE_NO_WARNING (expr) = 1;
5103 }
16837b18 5104 break;
73be5127 5105
31f820d2 5106 default:
5107 break;
b0fc3e72 5108 }
5109
2ba726d2 5110 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
a0748b7d 5111 {
93be21c0 5112 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
e60a6f7b 5113 expr = (build_binary_op
8e70fb09 5114 (EXPR_LOCATION (expr),
5115 (TREE_SIDE_EFFECTS (expr)
a0748b7d 5116 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
b6889cb0 5117 c_common_truthvalue_conversion
5118 (location,
5119 build_unary_op (location, REALPART_EXPR, t, 0)),
5120 c_common_truthvalue_conversion
5121 (location,
5122 build_unary_op (location, IMAGPART_EXPR, t, 0)),
a0748b7d 5123 0));
e60a6f7b 5124 goto ret;
a0748b7d 5125 }
2ba726d2 5126
9421ebb9 5127 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
5128 {
5129 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
5130 FCONST0 (TYPE_MODE
5131 (TREE_TYPE (expr))));
43158006 5132 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
9421ebb9 5133 }
e60a6f7b 5134 else
5135 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
9421ebb9 5136
e60a6f7b 5137 ret:
5138 protected_set_expr_location (expr, location);
5139 return expr;
b0fc3e72 5140}
5141\f
3237155d 5142static void def_builtin_1 (enum built_in_function fncode,
5143 const char *name,
5144 enum built_in_class fnclass,
5145 tree fntype, tree libtype,
5146 bool both_p, bool fallback_p, bool nonansi_p,
5147 tree fnattrs, bool implicit_p);
0d4238dc 5148
a5b1863e 5149
5150/* Apply the TYPE_QUALS to the new DECL. */
5151
5152void
1cae46be 5153c_apply_type_quals_to_decl (int type_quals, tree decl)
a5b1863e 5154{
adfb367f 5155 tree type = TREE_TYPE (decl);
b27ac6b5 5156
e4eabbe4 5157 if (type == error_mark_node)
5158 return;
adfb367f 5159
98a33d9f 5160 if ((type_quals & TYPE_QUAL_CONST)
5161 || (type && TREE_CODE (type) == REFERENCE_TYPE))
5162 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
5163 constructor can produce constant init, so rely on cp_finish_decl to
5164 clear TREE_READONLY if the variable has non-constant init. */
a5b1863e 5165 TREE_READONLY (decl) = 1;
5166 if (type_quals & TYPE_QUAL_VOLATILE)
5167 {
5168 TREE_SIDE_EFFECTS (decl) = 1;
5169 TREE_THIS_VOLATILE (decl) = 1;
5170 }
d91a20bc 5171 if (type_quals & TYPE_QUAL_RESTRICT)
a5b1863e 5172 {
adfb367f 5173 while (type && TREE_CODE (type) == ARRAY_TYPE)
5174 /* Allow 'restrict' on arrays of pointers.
5175 FIXME currently we just ignore it. */
5176 type = TREE_TYPE (type);
5177 if (!type
5178 || !POINTER_TYPE_P (type)
5179 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
b0b1af64 5180 error ("invalid use of %<restrict%>");
a5b1863e 5181 }
5182}
5183
b594087e 5184struct c_type_hasher : ggc_ptr_hash<tree_node>
2ef51f0e 5185{
5186 static hashval_t hash (tree);
5187 static bool equal (tree, tree);
5188};
5189
4ee9c684 5190/* Hash function for the problem of multiple type definitions in
5191 different files. This must hash all types that will compare
5192 equal via comptypes to the same value. In practice it hashes
0bed3869 5193 on some of the simple stuff and leaves the details to comptypes. */
4ee9c684 5194
2ef51f0e 5195hashval_t
5196c_type_hasher::hash (tree t)
4ee9c684 5197{
ecf2703d 5198 int n_elements;
4ee9c684 5199 int shift, size;
4ee9c684 5200 tree t2;
5201 switch (TREE_CODE (t))
5202 {
fbf0afd1 5203 /* For pointers, hash on pointee type plus some swizzling. */
2363ef00 5204 case POINTER_TYPE:
2ef51f0e 5205 return hash (TREE_TYPE (t)) ^ 0x3003003;
2363ef00 5206 /* Hash on number of elements and total size. */
5207 case ENUMERAL_TYPE:
5208 shift = 3;
5209 t2 = TYPE_VALUES (t);
5210 break;
5211 case RECORD_TYPE:
5212 shift = 0;
5213 t2 = TYPE_FIELDS (t);
5214 break;
5215 case QUAL_UNION_TYPE:
5216 shift = 1;
5217 t2 = TYPE_FIELDS (t);
5218 break;
5219 case UNION_TYPE:
5220 shift = 2;
5221 t2 = TYPE_FIELDS (t);
5222 break;
5223 default:
231bd014 5224 gcc_unreachable ();
4ee9c684 5225 }
ecf2703d 5226 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
5227 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
5228 n_elements = list_length (t2);
78c2e180 5229 /* We might have a VLA here. */
5230 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
5231 size = 0;
5232 else
f9ae6f95 5233 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
ecf2703d 5234 return ((size << 24) | (n_elements << shift));
4ee9c684 5235}
5236
2ef51f0e 5237bool
5238c_type_hasher::equal (tree t1, tree t2)
5239{
5240 return lang_hooks.types_compatible_p (t1, t2);
5241}
5242
5243static GTY(()) hash_table<c_type_hasher> *type_hash_table;
1ecd4018 5244
b5ba9f3a 5245/* Return the typed-based alias set for T, which may be an expression
f7c44134 5246 or a type. Return -1 if we don't do anything special. */
ab2c1de8 5247
32c2fdea 5248alias_set_type
1cae46be 5249c_common_get_alias_set (tree t)
b5ba9f3a 5250{
be4f2de7 5251 tree u;
1cae46be 5252
e58c17e7 5253 /* For VLAs, use the alias set of the element type rather than the
5254 default of alias set 0 for types compared structurally. */
5255 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
5256 {
5257 if (TREE_CODE (t) == ARRAY_TYPE)
5258 return get_alias_set (TREE_TYPE (t));
5259 return -1;
5260 }
5261
be4f2de7 5262 /* Permit type-punning when accessing a union, provided the access
5263 is directly through the union. For example, this code does not
5264 permit taking the address of a union member and then storing
5265 through it. Even the type-punning allowed here is a GCC
5266 extension, albeit a common and useful one; the C standard says
5267 that such accesses have implementation-defined behavior. */
5268 for (u = t;
5269 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
5270 u = TREE_OPERAND (u, 0))
5271 if (TREE_CODE (u) == COMPONENT_REF
5272 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
5273 return 0;
1e2513d9 5274
9fcc3e54 5275 /* That's all the expressions we handle specially. */
84166705 5276 if (!TYPE_P (t))
9fcc3e54 5277 return -1;
5278
d716ce75 5279 /* The C standard guarantees that any object may be accessed via an
9fcc3e54 5280 lvalue that has character type. */
5281 if (t == char_type_node
5282 || t == signed_char_type_node
5283 || t == unsigned_char_type_node)
f7c44134 5284 return 0;
a5b1863e 5285
1607663f 5286 /* The C standard specifically allows aliasing between signed and
5287 unsigned variants of the same type. We treat the signed
5288 variant as canonical. */
78a8ed03 5289 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
a8868e19 5290 {
4070745f 5291 tree t1 = c_common_signed_type (t);
1607663f 5292
a8868e19 5293 /* t1 == t can happen for boolean nodes which are always unsigned. */
5294 if (t1 != t)
5295 return get_alias_set (t1);
5296 }
1e2513d9 5297
4ee9c684 5298 /* Handle the case of multiple type nodes referring to "the same" type,
5299 which occurs with IMA. These share an alias set. FIXME: Currently only
5300 C90 is handled. (In C99 type compatibility is not transitive, which
5301 complicates things mightily. The alias set splay trees can theoretically
5302 represent this, but insertion is tricky when you consider all the
5303 different orders things might arrive in.) */
5304
5305 if (c_language != clk_c || flag_isoc99)
5306 return -1;
5307
0bed3869 5308 /* Save time if there's only one input file. */
e08bd2f4 5309 if (num_in_fnames == 1)
4ee9c684 5310 return -1;
5311
5312 /* Pointers need special handling if they point to any type that
5313 needs special handling (below). */
5314 if (TREE_CODE (t) == POINTER_TYPE)
5315 {
5316 tree t2;
5317 /* Find bottom type under any nested POINTERs. */
b27ac6b5 5318 for (t2 = TREE_TYPE (t);
af592f67 5319 TREE_CODE (t2) == POINTER_TYPE;
5320 t2 = TREE_TYPE (t2))
5321 ;
b27ac6b5 5322 if (TREE_CODE (t2) != RECORD_TYPE
af592f67 5323 && TREE_CODE (t2) != ENUMERAL_TYPE
5324 && TREE_CODE (t2) != QUAL_UNION_TYPE
5325 && TREE_CODE (t2) != UNION_TYPE)
5326 return -1;
4ee9c684 5327 if (TYPE_SIZE (t2) == 0)
af592f67 5328 return -1;
4ee9c684 5329 }
5330 /* These are the only cases that need special handling. */
b27ac6b5 5331 if (TREE_CODE (t) != RECORD_TYPE
4ee9c684 5332 && TREE_CODE (t) != ENUMERAL_TYPE
5333 && TREE_CODE (t) != QUAL_UNION_TYPE
5334 && TREE_CODE (t) != UNION_TYPE
5335 && TREE_CODE (t) != POINTER_TYPE)
5336 return -1;
5337 /* Undefined? */
5338 if (TYPE_SIZE (t) == 0)
5339 return -1;
5340
b27ac6b5 5341 /* Look up t in hash table. Only one of the compatible types within each
4ee9c684 5342 alias set is recorded in the table. */
5343 if (!type_hash_table)
2ef51f0e 5344 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
5345 tree *slot = type_hash_table->find_slot (t, INSERT);
4ee9c684 5346 if (*slot != NULL)
ad16cb2c 5347 {
5348 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5349 return TYPE_ALIAS_SET ((tree)*slot);
5350 }
4ee9c684 5351 else
5352 /* Our caller will assign and record (in t) a new alias set; all we need
5353 to do is remember t in the hash table. */
5354 *slot = t;
5355
f7c44134 5356 return -1;
b5ba9f3a 5357}
902b4e01 5358\f
e60a6f7b 5359/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
a179a7dc 5360 the IS_SIZEOF parameter indicates which operator is being applied.
e60a6f7b 5361 The COMPLAIN flag controls whether we should diagnose possibly
5362 ill-formed constructs or not. LOC is the location of the SIZEOF or
a179a7dc 5363 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5364 a type in any context should be returned, rather than the normal
5365 alignment for that type. */
908c697e 5366
902b4e01 5367tree
e60a6f7b 5368c_sizeof_or_alignof_type (location_t loc,
a179a7dc 5369 tree type, bool is_sizeof, bool min_alignof,
5370 int complain)
902b4e01 5371{
d4c4d95c 5372 const char *op_name;
5373 tree value = NULL;
5374 enum tree_code type_code = TREE_CODE (type);
1cae46be 5375
908c697e 5376 op_name = is_sizeof ? "sizeof" : "__alignof__";
1cae46be 5377
d4c4d95c 5378 if (type_code == FUNCTION_TYPE)
902b4e01 5379 {
908c697e 5380 if (is_sizeof)
d4c4d95c 5381 {
9205a6cc 5382 if (complain && warn_pointer_arith)
5383 pedwarn (loc, OPT_Wpointer_arith,
8864917d 5384 "invalid application of %<sizeof%> to a function type");
ebd21de4 5385 else if (!complain)
5386 return error_mark_node;
d4c4d95c 5387 value = size_one_node;
5388 }
5389 else
83e25171 5390 {
5391 if (complain)
5392 {
5393 if (c_dialect_cxx ())
29438999 5394 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
83e25171 5395 "%<alignof%> applied to a function type");
5396 else
29438999 5397 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
83e25171 5398 "%<_Alignof%> applied to a function type");
5399 }
5400 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5401 }
d4c4d95c 5402 }
5403 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5404 {
1cae46be 5405 if (type_code == VOID_TYPE
9205a6cc 5406 && complain && warn_pointer_arith)
5407 pedwarn (loc, OPT_Wpointer_arith,
8864917d 5408 "invalid application of %qs to a void type", op_name);
ebd21de4 5409 else if (!complain)
5410 return error_mark_node;
d4c4d95c 5411 value = size_one_node;
902b4e01 5412 }
3df19e1b 5413 else if (!COMPLETE_TYPE_P (type)
5414 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
902b4e01 5415 {
d3a4d008 5416 if (complain)
3df19e1b 5417 error_at (loc, "invalid application of %qs to incomplete type %qT",
e60a6f7b 5418 op_name, type);
9c719c74 5419 return error_mark_node;
902b4e01 5420 }
3df19e1b 5421 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5422 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5423 {
5424 if (complain)
5425 error_at (loc, "invalid application of %qs to array type %qT of "
5426 "incomplete element type", op_name, type);
5427 return error_mark_node;
5428 }
902b4e01 5429 else
d4c4d95c 5430 {
908c697e 5431 if (is_sizeof)
d4c4d95c 5432 /* Convert in case a char is more than one unit. */
389dd41b 5433 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5434 size_int (TYPE_PRECISION (char_type_node)
5435 / BITS_PER_UNIT));
a179a7dc 5436 else if (min_alignof)
c2c4ae8d 5437 value = size_int (min_align_of_type (type));
d4c4d95c 5438 else
d37625c0 5439 value = size_int (TYPE_ALIGN_UNIT (type));
d4c4d95c 5440 }
902b4e01 5441
5a1fe2db 5442 /* VALUE will have the middle-end integer type sizetype.
5443 However, we should really return a value of type `size_t',
5444 which is just a typedef for an ordinary integer type. */
389dd41b 5445 value = fold_convert_loc (loc, size_type_node, value);
1cae46be 5446
d4c4d95c 5447 return value;
902b4e01 5448}
5449
5450/* Implement the __alignof keyword: Return the minimum required
097b5c8b 5451 alignment of EXPR, measured in bytes. For VAR_DECLs,
5452 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
e60a6f7b 5453 from an "aligned" __attribute__ specification). LOC is the
5454 location of the ALIGNOF operator. */
72040e7e 5455
902b4e01 5456tree
e60a6f7b 5457c_alignof_expr (location_t loc, tree expr)
902b4e01 5458{
5459 tree t;
5460
097b5c8b 5461 if (VAR_OR_FUNCTION_DECL_P (expr))
d37625c0 5462 t = size_int (DECL_ALIGN_UNIT (expr));
1cae46be 5463
902b4e01 5464 else if (TREE_CODE (expr) == COMPONENT_REF
5465 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5466 {
e60a6f7b 5467 error_at (loc, "%<__alignof%> applied to a bit-field");
902b4e01 5468 t = size_one_node;
5469 }
5470 else if (TREE_CODE (expr) == COMPONENT_REF
7cc7e163 5471 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
d37625c0 5472 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
1cae46be 5473
aa3e402e 5474 else if (INDIRECT_REF_P (expr))
902b4e01 5475 {
5476 tree t = TREE_OPERAND (expr, 0);
5477 tree best = t;
5478 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1cae46be 5479
72dd6141 5480 while (CONVERT_EXPR_P (t)
7cc7e163 5481 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
902b4e01 5482 {
5483 int thisalign;
5484
5485 t = TREE_OPERAND (t, 0);
5486 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5487 if (thisalign > bestalign)
5488 best = t, bestalign = thisalign;
5489 }
e60a6f7b 5490 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
902b4e01 5491 }
5492 else
e60a6f7b 5493 return c_alignof (loc, TREE_TYPE (expr));
902b4e01 5494
389dd41b 5495 return fold_convert_loc (loc, size_type_node, t);
902b4e01 5496}
5497\f
8fe4a266 5498/* Handle C and C++ default attributes. */
5499
5500enum built_in_attribute
5501{
5502#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5503#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
c8010b80 5504#define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
8fe4a266 5505#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5506#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
8fe4a266 5507#include "builtin-attrs.def"
5508#undef DEF_ATTR_NULL_TREE
5509#undef DEF_ATTR_INT
c8010b80 5510#undef DEF_ATTR_STRING
8fe4a266 5511#undef DEF_ATTR_IDENT
5512#undef DEF_ATTR_TREE_LIST
8fe4a266 5513 ATTR_LAST
5514};
5515
5516static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5517
1cae46be 5518static void c_init_attributes (void);
8fe4a266 5519
27213ba3 5520enum c_builtin_type
72040e7e 5521{
d2d4bdde 5522#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5523#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5524#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5525#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5526#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5527#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
0a39fd54 5528#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
3c77ca67 5529#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5530 ARG6) NAME,
5531#define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5532 ARG6, ARG7) NAME,
5533#define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5534 ARG6, ARG7, ARG8) NAME,
d2d4bdde 5535#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5536#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5537#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
03901330 5538#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
0a39fd54 5539#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3c77ca67 5540#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
6349b8cc 5541 NAME,
5542#define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5543 ARG6, ARG7) NAME,
5544#define DEF_FUNCTION_TYPE_VAR_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5545 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
d2d4bdde 5546#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5547#include "builtin-types.def"
5548#undef DEF_PRIMITIVE_TYPE
5549#undef DEF_FUNCTION_TYPE_0
5550#undef DEF_FUNCTION_TYPE_1
5551#undef DEF_FUNCTION_TYPE_2
5552#undef DEF_FUNCTION_TYPE_3
5553#undef DEF_FUNCTION_TYPE_4
0a39fd54 5554#undef DEF_FUNCTION_TYPE_5
5555#undef DEF_FUNCTION_TYPE_6
27213ba3 5556#undef DEF_FUNCTION_TYPE_7
bc7bff74 5557#undef DEF_FUNCTION_TYPE_8
d2d4bdde 5558#undef DEF_FUNCTION_TYPE_VAR_0
5559#undef DEF_FUNCTION_TYPE_VAR_1
5560#undef DEF_FUNCTION_TYPE_VAR_2
03901330 5561#undef DEF_FUNCTION_TYPE_VAR_3
0a39fd54 5562#undef DEF_FUNCTION_TYPE_VAR_4
5563#undef DEF_FUNCTION_TYPE_VAR_5
6349b8cc 5564#undef DEF_FUNCTION_TYPE_VAR_7
5565#undef DEF_FUNCTION_TYPE_VAR_11
d2d4bdde 5566#undef DEF_POINTER_TYPE
27213ba3 5567 BT_LAST
5568};
5569
5570typedef enum c_builtin_type builtin_type;
d2d4bdde 5571
27213ba3 5572/* A temporary array for c_common_nodes_and_builtins. Used in
5573 communication with def_fn_type. */
5574static tree builtin_types[(int) BT_LAST + 1];
d2d4bdde 5575
27213ba3 5576/* A helper function for c_common_nodes_and_builtins. Build function type
5577 for DEF with return type RET and N arguments. If VAR is true, then the
5578 function should be variadic after those N arguments.
5579
5580 Takes special care not to ICE if any of the types involved are
5581 error_mark_node, which indicates that said type is not in fact available
5582 (see builtin_type_for_size). In which case the function type as a whole
5583 should be error_mark_node. */
5584
5585static void
5586def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5587{
3a939d12 5588 tree t;
5589 tree *args = XALLOCAVEC (tree, n);
27213ba3 5590 va_list list;
5591 int i;
5592
5593 va_start (list, n);
5594 for (i = 0; i < n; ++i)
5595 {
7d339f93 5596 builtin_type a = (builtin_type) va_arg (list, int);
27213ba3 5597 t = builtin_types[a];
5598 if (t == error_mark_node)
5599 goto egress;
3a939d12 5600 args[i] = t;
27213ba3 5601 }
27213ba3 5602
27213ba3 5603 t = builtin_types[ret];
5604 if (t == error_mark_node)
5605 goto egress;
3a939d12 5606 if (var)
5607 t = build_varargs_function_type_array (t, n, args);
5608 else
5609 t = build_function_type_array (t, n, args);
27213ba3 5610
5611 egress:
5612 builtin_types[def] = t;
451c8e2f 5613 va_end (list);
27213ba3 5614}
5615
dce22712 5616/* Build builtin functions common to both C and C++ language
5617 frontends. */
5618
5619static void
5620c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5621{
5622#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5623 builtin_types[ENUM] = VALUE;
5624#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5625 def_fn_type (ENUM, RETURN, 0, 0);
5626#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5627 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5628#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5629 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5630#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5631 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5632#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5633 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5634#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5635 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5636#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5637 ARG6) \
5638 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5639#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5640 ARG6, ARG7) \
5641 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
bc7bff74 5642#define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5643 ARG6, ARG7, ARG8) \
5644 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5645 ARG7, ARG8);
dce22712 5646#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5647 def_fn_type (ENUM, RETURN, 1, 0);
5648#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5649 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5650#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5651 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5652#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5653 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5654#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5655 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5656#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5657 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
6349b8cc 5658#define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5659 ARG6, ARG7) \
5660 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5661#define DEF_FUNCTION_TYPE_VAR_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5662 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5663 def_fn_type (ENUM, RETURN, 1, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5664 ARG7, ARG8, ARG9, ARG10, ARG11);
dce22712 5665#define DEF_POINTER_TYPE(ENUM, TYPE) \
5666 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5667
5668#include "builtin-types.def"
5669
5670#undef DEF_PRIMITIVE_TYPE
3c77ca67 5671#undef DEF_FUNCTION_TYPE_0
dce22712 5672#undef DEF_FUNCTION_TYPE_1
5673#undef DEF_FUNCTION_TYPE_2
5674#undef DEF_FUNCTION_TYPE_3
5675#undef DEF_FUNCTION_TYPE_4
5676#undef DEF_FUNCTION_TYPE_5
5677#undef DEF_FUNCTION_TYPE_6
3c77ca67 5678#undef DEF_FUNCTION_TYPE_7
5679#undef DEF_FUNCTION_TYPE_8
dce22712 5680#undef DEF_FUNCTION_TYPE_VAR_0
5681#undef DEF_FUNCTION_TYPE_VAR_1
5682#undef DEF_FUNCTION_TYPE_VAR_2
5683#undef DEF_FUNCTION_TYPE_VAR_3
5684#undef DEF_FUNCTION_TYPE_VAR_4
5685#undef DEF_FUNCTION_TYPE_VAR_5
6349b8cc 5686#undef DEF_FUNCTION_TYPE_VAR_7
5687#undef DEF_FUNCTION_TYPE_VAR_11
dce22712 5688#undef DEF_POINTER_TYPE
5689 builtin_types[(int) BT_LAST] = NULL_TREE;
5690
5691 c_init_attributes ();
5692
5693#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5694 NONANSI_P, ATTRS, IMPLICIT, COND) \
5695 if (NAME && COND) \
5696 def_builtin_1 (ENUM, NAME, CLASS, \
5697 builtin_types[(int) TYPE], \
5698 builtin_types[(int) LIBTYPE], \
5699 BOTH_P, FALLBACK_P, NONANSI_P, \
5700 built_in_attributes[(int) ATTRS], IMPLICIT);
5701#include "builtins.def"
5702#undef DEF_BUILTIN
5703
87eb1c28 5704 targetm.init_builtins ();
5705
471eff36 5706 build_common_builtin_nodes ();
dce22712 5707
a89e6c15 5708 if (flag_cilkplus)
d037099f 5709 cilk_init_builtins ();
dce22712 5710}
5711
9e6687c8 5712/* Like get_identifier, but avoid warnings about null arguments when
5713 the argument may be NULL for targets where GCC lacks stdint.h type
5714 information. */
5715
5716static inline tree
5717c_get_ident (const char *id)
5718{
5719 return get_identifier (id);
5720}
5721
27213ba3 5722/* Build tree nodes and builtin functions common to both C and C++ language
5723 frontends. */
5724
5725void
5726c_common_nodes_and_builtins (void)
5727{
924bbf02 5728 int char16_type_size;
5729 int char32_type_size;
174fcc61 5730 int wchar_type_size;
5731 tree array_domain_type;
2d47cc32 5732 tree va_list_ref_type_node;
8a15c04a 5733 tree va_list_arg_type_node;
9f75f026 5734 int i;
a66c9326 5735
c38a75b7 5736 build_common_tree_nodes (flag_signed_char, flag_short_double);
e593356b 5737
174fcc61 5738 /* Define `int' and `char' first so that dbx will output them first. */
d946ea19 5739 record_builtin_type (RID_INT, NULL, integer_type_node);
174fcc61 5740 record_builtin_type (RID_CHAR, "char", char_type_node);
5741
5742 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5743 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5744 but not C. Are the conditionals here needed? */
c0f19401 5745 if (c_dialect_cxx ())
d946ea19 5746 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
174fcc61 5747 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5748 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5749 record_builtin_type (RID_MAX, "long unsigned int",
5750 long_unsigned_type_node);
9f75f026 5751
5752 for (i = 0; i < NUM_INT_N_ENTS; i ++)
6388cfe2 5753 {
9f75f026 5754 char name[25];
5755
5756 sprintf (name, "__int%d", int_n_data[i].bitsize);
76738f56 5757 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
9f75f026 5758 int_n_trees[i].signed_type);
5759 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
76738f56 5760 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
6388cfe2 5761 }
9f75f026 5762
c0f19401 5763 if (c_dialect_cxx ())
174fcc61 5764 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5765 record_builtin_type (RID_MAX, "long long int",
5766 long_long_integer_type_node);
5767 record_builtin_type (RID_MAX, "long long unsigned int",
5768 long_long_unsigned_type_node);
c0f19401 5769 if (c_dialect_cxx ())
174fcc61 5770 record_builtin_type (RID_MAX, "long long unsigned",
5771 long_long_unsigned_type_node);
5772 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5773 record_builtin_type (RID_MAX, "short unsigned int",
5774 short_unsigned_type_node);
c0f19401 5775 if (c_dialect_cxx ())
174fcc61 5776 record_builtin_type (RID_MAX, "unsigned short",
5777 short_unsigned_type_node);
5778
5779 /* Define both `signed char' and `unsigned char'. */
5780 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5781 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5782
771d21fa 5783 /* These are types that c_common_type_for_size and
5784 c_common_type_for_mode use. */
e60a6f7b 5785 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5786 TYPE_DECL, NULL_TREE,
dc24ddbd 5787 intQI_type_node));
e60a6f7b 5788 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5789 TYPE_DECL, NULL_TREE,
dc24ddbd 5790 intHI_type_node));
e60a6f7b 5791 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5792 TYPE_DECL, NULL_TREE,
dc24ddbd 5793 intSI_type_node));
e60a6f7b 5794 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5795 TYPE_DECL, NULL_TREE,
dc24ddbd 5796 intDI_type_node));
174fcc61 5797#if HOST_BITS_PER_WIDE_INT >= 64
9f75f026 5798 /* Note that this is different than the __int128 type that's part of
5799 the generic __intN support. */
f1515a39 5800 if (targetm.scalar_mode_supported_p (TImode))
e60a6f7b 5801 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5802 TYPE_DECL,
f1515a39 5803 get_identifier ("__int128_t"),
5804 intTI_type_node));
174fcc61 5805#endif
e60a6f7b 5806 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5807 TYPE_DECL, NULL_TREE,
dc24ddbd 5808 unsigned_intQI_type_node));
e60a6f7b 5809 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5810 TYPE_DECL, NULL_TREE,
dc24ddbd 5811 unsigned_intHI_type_node));
e60a6f7b 5812 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5813 TYPE_DECL, NULL_TREE,
dc24ddbd 5814 unsigned_intSI_type_node));
e60a6f7b 5815 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5816 TYPE_DECL, NULL_TREE,
dc24ddbd 5817 unsigned_intDI_type_node));
174fcc61 5818#if HOST_BITS_PER_WIDE_INT >= 64
f1515a39 5819 if (targetm.scalar_mode_supported_p (TImode))
e60a6f7b 5820 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5821 TYPE_DECL,
f1515a39 5822 get_identifier ("__uint128_t"),
5823 unsigned_intTI_type_node));
174fcc61 5824#endif
5825
5826 /* Create the widest literal types. */
5827 widest_integer_literal_type_node
5828 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
e60a6f7b 5829 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5830 TYPE_DECL, NULL_TREE,
dc24ddbd 5831 widest_integer_literal_type_node));
174fcc61 5832
5833 widest_unsigned_literal_type_node
5834 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
e60a6f7b 5835 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5836 TYPE_DECL, NULL_TREE,
dc24ddbd 5837 widest_unsigned_literal_type_node));
174fcc61 5838
654ef926 5839 signed_size_type_node = c_common_signed_type (size_type_node);
174fcc61 5840
73673831 5841 pid_type_node =
5842 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5843
d946ea19 5844 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5845 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
174fcc61 5846 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5847
c4503c0a 5848 /* Only supported decimal floating point extension if the target
5849 actually supports underlying modes. */
48e1416a 5850 if (targetm.scalar_mode_supported_p (SDmode)
c4503c0a 5851 && targetm.scalar_mode_supported_p (DDmode)
5852 && targetm.scalar_mode_supported_p (TDmode))
5853 {
5854 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5855 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5856 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5857 }
5858
9421ebb9 5859 if (targetm.fixed_point_supported_p ())
5860 {
5861 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5862 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5863 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5864 record_builtin_type (RID_MAX, "long long _Fract",
5865 long_long_fract_type_node);
5866 record_builtin_type (RID_MAX, "unsigned short _Fract",
5867 unsigned_short_fract_type_node);
5868 record_builtin_type (RID_MAX, "unsigned _Fract",
5869 unsigned_fract_type_node);
5870 record_builtin_type (RID_MAX, "unsigned long _Fract",
5871 unsigned_long_fract_type_node);
5872 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5873 unsigned_long_long_fract_type_node);
5874 record_builtin_type (RID_MAX, "_Sat short _Fract",
5875 sat_short_fract_type_node);
5876 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5877 record_builtin_type (RID_MAX, "_Sat long _Fract",
5878 sat_long_fract_type_node);
5879 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5880 sat_long_long_fract_type_node);
5881 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5882 sat_unsigned_short_fract_type_node);
5883 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5884 sat_unsigned_fract_type_node);
5885 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5886 sat_unsigned_long_fract_type_node);
5887 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5888 sat_unsigned_long_long_fract_type_node);
5889 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5890 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5891 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5892 record_builtin_type (RID_MAX, "long long _Accum",
5893 long_long_accum_type_node);
5894 record_builtin_type (RID_MAX, "unsigned short _Accum",
5895 unsigned_short_accum_type_node);
5896 record_builtin_type (RID_MAX, "unsigned _Accum",
5897 unsigned_accum_type_node);
5898 record_builtin_type (RID_MAX, "unsigned long _Accum",
5899 unsigned_long_accum_type_node);
5900 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5901 unsigned_long_long_accum_type_node);
5902 record_builtin_type (RID_MAX, "_Sat short _Accum",
5903 sat_short_accum_type_node);
5904 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5905 record_builtin_type (RID_MAX, "_Sat long _Accum",
5906 sat_long_accum_type_node);
5907 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5908 sat_long_long_accum_type_node);
5909 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5910 sat_unsigned_short_accum_type_node);
5911 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5912 sat_unsigned_accum_type_node);
5913 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5914 sat_unsigned_long_accum_type_node);
5915 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5916 sat_unsigned_long_long_accum_type_node);
5917
5918 }
5919
e60a6f7b 5920 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5921 TYPE_DECL,
dc24ddbd 5922 get_identifier ("complex int"),
5923 complex_integer_type_node));
e60a6f7b 5924 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5925 TYPE_DECL,
dc24ddbd 5926 get_identifier ("complex float"),
5927 complex_float_type_node));
e60a6f7b 5928 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5929 TYPE_DECL,
dc24ddbd 5930 get_identifier ("complex double"),
5931 complex_double_type_node));
5932 lang_hooks.decls.pushdecl
e60a6f7b 5933 (build_decl (UNKNOWN_LOCATION,
5934 TYPE_DECL, get_identifier ("complex long double"),
20325f61 5935 complex_long_double_type_node));
174fcc61 5936
e256d445 5937 if (c_dialect_cxx ())
5938 /* For C++, make fileptr_type_node a distinct void * type until
5939 FILE type is defined. */
e086912e 5940 fileptr_type_node = build_variant_type_copy (ptr_type_node);
e256d445 5941
d946ea19 5942 record_builtin_type (RID_VOID, NULL, void_type_node);
174fcc61 5943
6753bca0 5944 /* Set the TYPE_NAME for any variants that were built before
5945 record_builtin_type gave names to the built-in types. */
5946 {
5947 tree void_name = TYPE_NAME (void_type_node);
5948 TYPE_NAME (void_type_node) = NULL_TREE;
5949 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5950 = void_name;
5951 TYPE_NAME (void_type_node) = void_name;
5952 }
5953
174fcc61 5954 void_list_node = build_void_list_node ();
5955
5956 /* Make a type to be the domain of a few array types
5957 whose domains don't really matter.
5958 200 is small enough that it always fits in size_t
5959 and large enough that it can hold most function names for the
5960 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5961 array_domain_type = build_index_type (size_int (200));
5962
5963 /* Make a type for arrays of characters.
5964 With luck nothing will ever really depend on the length of this
5965 array type. */
5966 char_array_type_node
5967 = build_array_type (char_type_node, array_domain_type);
5968
d2d4bdde 5969 string_type_node = build_pointer_type (char_type_node);
5970 const_string_type_node
5971 = build_pointer_type (build_qualified_type
5972 (char_type_node, TYPE_QUAL_CONST));
5973
174fcc61 5974 /* This is special for C++ so functions can be overloaded. */
18ef7ac2 5975 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
174fcc61 5976 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5977 wchar_type_size = TYPE_PRECISION (wchar_type_node);
f3449a3c 5978 underlying_wchar_type_node = wchar_type_node;
c0f19401 5979 if (c_dialect_cxx ())
174fcc61 5980 {
78a8ed03 5981 if (TYPE_UNSIGNED (wchar_type_node))
174fcc61 5982 wchar_type_node = make_unsigned_type (wchar_type_size);
5983 else
5984 wchar_type_node = make_signed_type (wchar_type_size);
5985 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5986 }
174fcc61 5987
5988 /* This is for wide string constants. */
5989 wchar_array_type_node
5990 = build_array_type (wchar_type_node, array_domain_type);
5991
924bbf02 5992 /* Define 'char16_t'. */
5993 char16_type_node = get_identifier (CHAR16_TYPE);
5994 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5995 char16_type_size = TYPE_PRECISION (char16_type_node);
5996 if (c_dialect_cxx ())
5997 {
5998 char16_type_node = make_unsigned_type (char16_type_size);
5999
60777f69 6000 if (cxx_dialect >= cxx11)
924bbf02 6001 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
6002 }
6003
6004 /* This is for UTF-16 string constants. */
6005 char16_array_type_node
6006 = build_array_type (char16_type_node, array_domain_type);
6007
6008 /* Define 'char32_t'. */
6009 char32_type_node = get_identifier (CHAR32_TYPE);
6010 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
6011 char32_type_size = TYPE_PRECISION (char32_type_node);
6012 if (c_dialect_cxx ())
6013 {
6014 char32_type_node = make_unsigned_type (char32_type_size);
6015
60777f69 6016 if (cxx_dialect >= cxx11)
924bbf02 6017 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
6018 }
6019
6020 /* This is for UTF-32 string constants. */
6021 char32_array_type_node
6022 = build_array_type (char32_type_node, array_domain_type);
6023
6bf5ed8d 6024 wint_type_node =
6025 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
6026
6027 intmax_type_node =
6028 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
6029 uintmax_type_node =
6030 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
6031
f3449a3c 6032 if (SIG_ATOMIC_TYPE)
6033 sig_atomic_type_node =
9e6687c8 6034 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
f3449a3c 6035 if (INT8_TYPE)
6036 int8_type_node =
9e6687c8 6037 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
f3449a3c 6038 if (INT16_TYPE)
6039 int16_type_node =
9e6687c8 6040 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
f3449a3c 6041 if (INT32_TYPE)
6042 int32_type_node =
9e6687c8 6043 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
f3449a3c 6044 if (INT64_TYPE)
6045 int64_type_node =
9e6687c8 6046 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
f3449a3c 6047 if (UINT8_TYPE)
6048 uint8_type_node =
9e6687c8 6049 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
f3449a3c 6050 if (UINT16_TYPE)
d1081017 6051 c_uint16_type_node = uint16_type_node =
9e6687c8 6052 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
f3449a3c 6053 if (UINT32_TYPE)
d1081017 6054 c_uint32_type_node = uint32_type_node =
9e6687c8 6055 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
f3449a3c 6056 if (UINT64_TYPE)
d1081017 6057 c_uint64_type_node = uint64_type_node =
9e6687c8 6058 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
f3449a3c 6059 if (INT_LEAST8_TYPE)
6060 int_least8_type_node =
9e6687c8 6061 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
f3449a3c 6062 if (INT_LEAST16_TYPE)
6063 int_least16_type_node =
9e6687c8 6064 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
f3449a3c 6065 if (INT_LEAST32_TYPE)
6066 int_least32_type_node =
9e6687c8 6067 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
f3449a3c 6068 if (INT_LEAST64_TYPE)
6069 int_least64_type_node =
9e6687c8 6070 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
f3449a3c 6071 if (UINT_LEAST8_TYPE)
6072 uint_least8_type_node =
9e6687c8 6073 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
f3449a3c 6074 if (UINT_LEAST16_TYPE)
6075 uint_least16_type_node =
9e6687c8 6076 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
f3449a3c 6077 if (UINT_LEAST32_TYPE)
6078 uint_least32_type_node =
9e6687c8 6079 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
f3449a3c 6080 if (UINT_LEAST64_TYPE)
6081 uint_least64_type_node =
9e6687c8 6082 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
f3449a3c 6083 if (INT_FAST8_TYPE)
6084 int_fast8_type_node =
9e6687c8 6085 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
f3449a3c 6086 if (INT_FAST16_TYPE)
6087 int_fast16_type_node =
9e6687c8 6088 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
f3449a3c 6089 if (INT_FAST32_TYPE)
6090 int_fast32_type_node =
9e6687c8 6091 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
f3449a3c 6092 if (INT_FAST64_TYPE)
6093 int_fast64_type_node =
9e6687c8 6094 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
f3449a3c 6095 if (UINT_FAST8_TYPE)
6096 uint_fast8_type_node =
9e6687c8 6097 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
f3449a3c 6098 if (UINT_FAST16_TYPE)
6099 uint_fast16_type_node =
9e6687c8 6100 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
f3449a3c 6101 if (UINT_FAST32_TYPE)
6102 uint_fast32_type_node =
9e6687c8 6103 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
f3449a3c 6104 if (UINT_FAST64_TYPE)
6105 uint_fast64_type_node =
9e6687c8 6106 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
f3449a3c 6107 if (INTPTR_TYPE)
6108 intptr_type_node =
9e6687c8 6109 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
f3449a3c 6110 if (UINTPTR_TYPE)
6111 uintptr_type_node =
9e6687c8 6112 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
f3449a3c 6113
3a939d12 6114 default_function_type
6115 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
6bf5ed8d 6116 ptrdiff_type_node
6117 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
11773141 6118 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
6bf5ed8d 6119
dc24ddbd 6120 lang_hooks.decls.pushdecl
e60a6f7b 6121 (build_decl (UNKNOWN_LOCATION,
6122 TYPE_DECL, get_identifier ("__builtin_va_list"),
20325f61 6123 va_list_type_node));
202d6e5f 6124 if (targetm.enum_va_list_p)
acd6f472 6125 {
6126 int l;
6127 const char *pname;
6128 tree ptype;
5f57a8b1 6129
202d6e5f 6130 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
acd6f472 6131 {
6132 lang_hooks.decls.pushdecl
6133 (build_decl (UNKNOWN_LOCATION,
6134 TYPE_DECL, get_identifier (pname),
6135 ptype));
ab2c1de8 6136
acd6f472 6137 }
6138 }
8a15c04a 6139
8a15c04a 6140 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
2d47cc32 6141 {
6142 va_list_arg_type_node = va_list_ref_type_node =
6143 build_pointer_type (TREE_TYPE (va_list_type_node));
6144 }
8a15c04a 6145 else
2d47cc32 6146 {
6147 va_list_arg_type_node = va_list_type_node;
6148 va_list_ref_type_node = build_reference_type (va_list_type_node);
6149 }
1cae46be 6150
dce22712 6151 if (!flag_preprocess_only)
6152 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
ffa8918b 6153
5c62f199 6154 main_identifier_node = get_identifier ("main");
ae84079f 6155
6156 /* Create the built-in __null node. It is important that this is
6157 not shared. */
271e739a 6158 null_node = make_int_cst (1, 1);
ae84079f 6159 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
27213ba3 6160
6161 /* Since builtin_types isn't gc'ed, don't export these nodes. */
6162 memset (builtin_types, 0, sizeof (builtin_types));
72040e7e 6163}
a66c9326 6164
79b01846 6165/* The number of named compound-literals generated thus far. */
6166static GTY(()) int compound_literal_number;
6167
6168/* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
6169
6170void
6171set_compound_literal_name (tree decl)
6172{
6173 char *name;
6174 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
6175 compound_literal_number);
6176 compound_literal_number++;
6177 DECL_NAME (decl) = get_identifier (name);
6178}
6179
2e474820 6180/* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
6181 TYPE and operand OP. */
6182
6183static tree
6184build_va_arg_1 (location_t loc, tree type, tree op)
6185{
6186 tree expr = build1 (VA_ARG_EXPR, type, op);
6187 SET_EXPR_LOCATION (expr, loc);
6188 return expr;
6189}
6190
6191/* Return a VA_ARG_EXPR corresponding to a source-level expression
6192 va_arg (EXPR, TYPE) at source location LOC. */
6193
a66c9326 6194tree
e60a6f7b 6195build_va_arg (location_t loc, tree expr, tree type)
a66c9326 6196{
c37be9ec 6197 tree va_type = TREE_TYPE (expr);
6198 tree canon_va_type = (va_type == error_mark_node
6199 ? NULL_TREE
6200 : targetm.canonical_va_list_type (va_type));
6201
2e474820 6202 if (va_type == error_mark_node
6203 || canon_va_type == NULL_TREE)
c37be9ec 6204 {
2e474820 6205 /* Let's handle things neutrallly, if expr:
6206 - has undeclared type, or
6207 - is not an va_list type. */
6208 return build_va_arg_1 (loc, type, expr);
c37be9ec 6209 }
f7fec1c7 6210
2e474820 6211 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
6212 {
6213 /* Case 1: Not an array type. */
6214
6215 /* Take the address, to get '&ap'. */
6216 mark_addressable (expr);
6217 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
6218
6219 /* Verify that &ap is still recognized as having va_list type. */
6220 tree canon_expr_type
6221 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6222 gcc_assert (canon_expr_type != NULL_TREE);
6223
6224 return build_va_arg_1 (loc, type, expr);
6225 }
6226
6227 /* Case 2: Array type.
6228
6229 Background:
6230
6231 For contrast, let's start with the simple case (case 1). If
6232 canon_va_type is not an array type, but say a char *, then when
6233 passing-by-value a va_list, the type of the va_list param decl is
6234 the same as for another va_list decl (all ap's are char *):
6235
6236 f2_1 (char * ap)
6237 D.1815 = VA_ARG (&ap, 0B, 1);
6238 return D.1815;
6239
6240 f2 (int i)
6241 char * ap.0;
6242 char * ap;
6243 __builtin_va_start (&ap, 0);
6244 ap.0 = ap;
6245 res = f2_1 (ap.0);
6246 __builtin_va_end (&ap);
6247 D.1812 = res;
6248 return D.1812;
6249
6250 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
6251 va_list the type of the va_list param decl (case 2b, struct * ap) is not
6252 the same as for another va_list decl (case 2a, struct ap[1]).
6253
6254 f2_1 (struct * ap)
6255 D.1844 = VA_ARG (ap, 0B, 0);
6256 return D.1844;
6257
6258 f2 (int i)
6259 struct ap[1];
6260 __builtin_va_start (&ap, 0);
6261 res = f2_1 (&ap);
6262 __builtin_va_end (&ap);
6263 D.1841 = res;
6264 return D.1841;
6265
6266 Case 2b is different because:
6267 - on the callee side, the parm decl has declared type va_list, but
6268 grokdeclarator changes the type of the parm decl to a pointer to the
6269 array elem type.
6270 - on the caller side, the pass-by-value uses &ap.
6271
6272 We unify these two cases (case 2a: va_list is array type,
6273 case 2b: va_list is pointer to array elem type), by adding '&' for the
6274 array type case, such that we have a pointer to array elem in both
6275 cases. */
6276
6277 if (TREE_CODE (va_type) == ARRAY_TYPE)
6278 {
6279 /* Case 2a: va_list is array type. */
6280
6281 /* Take the address, to get '&ap'. Make sure it's a pointer to array
6282 elem type. */
6283 mark_addressable (expr);
6284 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
6285 expr);
6286
6287 /* Verify that &ap is still recognized as having va_list type. */
6288 tree canon_expr_type
6289 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6290 gcc_assert (canon_expr_type != NULL_TREE);
6291 }
6292 else
6293 {
6294 /* Case 2b: va_list is pointer to array elem type. */
6295 gcc_assert (POINTER_TYPE_P (va_type));
6296 gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type));
6297
6298 /* Don't take the address. We've already got '&ap'. */
6299 ;
6300 }
6301
6302 return build_va_arg_1 (loc, type, expr);
a66c9326 6303}
0d4238dc 6304
6305
dd878098 6306/* Linked list of disabled built-in functions. */
6307
6308typedef struct disabled_builtin
6309{
6310 const char *name;
6311 struct disabled_builtin *next;
6312} disabled_builtin;
6313static disabled_builtin *disabled_builtins = NULL;
6314
1cae46be 6315static bool builtin_function_disabled_p (const char *);
dd878098 6316
6317/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
6318 begins with "__builtin_", give an error. */
6319
6320void
1cae46be 6321disable_builtin_function (const char *name)
dd878098 6322{
6323 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
b0b1af64 6324 error ("cannot disable built-in function %qs", name);
dd878098 6325 else
6326 {
e85905e5 6327 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
6328 new_disabled_builtin->name = name;
6329 new_disabled_builtin->next = disabled_builtins;
6330 disabled_builtins = new_disabled_builtin;
dd878098 6331 }
6332}
6333
6334
6335/* Return true if the built-in function NAME has been disabled, false
6336 otherwise. */
6337
6338static bool
1cae46be 6339builtin_function_disabled_p (const char *name)
dd878098 6340{
6341 disabled_builtin *p;
6342 for (p = disabled_builtins; p != NULL; p = p->next)
6343 {
6344 if (strcmp (name, p->name) == 0)
6345 return true;
6346 }
6347 return false;
6348}
6349
6350
3237155d 6351/* Worker for DEF_BUILTIN.
6352 Possibly define a builtin function with one or two names.
6353 Does not declare a non-__builtin_ function if flag_no_builtin, or if
6354 nonansi_p and flag_no_nonansi_builtin. */
0d4238dc 6355
3237155d 6356static void
6357def_builtin_1 (enum built_in_function fncode,
6358 const char *name,
6359 enum built_in_class fnclass,
6360 tree fntype, tree libtype,
6361 bool both_p, bool fallback_p, bool nonansi_p,
6362 tree fnattrs, bool implicit_p)
0d4238dc 6363{
3237155d 6364 tree decl;
6365 const char *libname;
6366
27213ba3 6367 if (fntype == error_mark_node)
6368 return;
6369
3237155d 6370 gcc_assert ((!both_p && !fallback_p)
6371 || !strncmp (name, "__builtin_",
6372 strlen ("__builtin_")));
6373
6374 libname = name + strlen ("__builtin_");
54be5d7e 6375 decl = add_builtin_function (name, fntype, fncode, fnclass,
6376 (fallback_p ? libname : NULL),
6377 fnattrs);
b9a16870 6378
6379 set_builtin_decl (fncode, decl, implicit_p);
6380
3237155d 6381 if (both_p
6382 && !flag_no_builtin && !builtin_function_disabled_p (libname)
dd878098 6383 && !(nonansi_p && flag_no_nonansi_builtin))
54be5d7e 6384 add_builtin_function (libname, libtype, fncode, fnclass,
6385 NULL, fnattrs);
0d4238dc 6386}
e94026da 6387\f
d7aeef06 6388/* Nonzero if the type T promotes to int. This is (nearly) the
6389 integral promotions defined in ISO C99 6.3.1.1/2. */
6390
6391bool
9f627b1a 6392c_promoting_integer_type_p (const_tree t)
d7aeef06 6393{
6394 switch (TREE_CODE (t))
6395 {
6396 case INTEGER_TYPE:
6397 return (TYPE_MAIN_VARIANT (t) == char_type_node
6398 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
6399 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
6400 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
7aa1e6eb 6401 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
6402 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d7aeef06 6403
6404 case ENUMERAL_TYPE:
6405 /* ??? Technically all enumerations not larger than an int
6406 promote to an int. But this is used along code paths
6407 that only want to notice a size change. */
6408 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6409
6410 case BOOLEAN_TYPE:
6411 return 1;
6412
6413 default:
6414 return 0;
6415 }
6416}
6417
e94026da 6418/* Return 1 if PARMS specifies a fixed number of parameters
6419 and none of their types is affected by default promotions. */
6420
6421int
9f627b1a 6422self_promoting_args_p (const_tree parms)
e94026da 6423{
9f627b1a 6424 const_tree t;
e94026da 6425 for (t = parms; t; t = TREE_CHAIN (t))
6426 {
19cb6b50 6427 tree type = TREE_VALUE (t);
43f74bc4 6428
e1d8e198 6429 if (type == error_mark_node)
6430 continue;
6431
e94026da 6432 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6433 return 0;
6434
6435 if (type == 0)
6436 return 0;
6437
6438 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6439 return 0;
6440
d7aeef06 6441 if (c_promoting_integer_type_p (type))
e94026da 6442 return 0;
6443 }
6444 return 1;
6445}
605fb01e 6446
c10de5e7 6447/* Recursively remove any '*' or '&' operator from TYPE. */
6448tree
6449strip_pointer_operator (tree t)
6450{
6451 while (POINTER_TYPE_P (t))
6452 t = TREE_TYPE (t);
6453 return t;
6454}
6455
57a0ed23 6456/* Recursively remove pointer or array type from TYPE. */
6457tree
6458strip_pointer_or_array_types (tree t)
6459{
6460 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6461 t = TREE_TYPE (t);
6462 return t;
6463}
6464
e41f0d80 6465/* Used to compare case labels. K1 and K2 are actually tree nodes
6466 representing case labels, or NULL_TREE for a `default' label.
6467 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6468 K2, and 0 if K1 and K2 are equal. */
6469
6470int
1cae46be 6471case_compare (splay_tree_key k1, splay_tree_key k2)
e41f0d80 6472{
6473 /* Consider a NULL key (such as arises with a `default' label) to be
6474 smaller than anything else. */
6475 if (!k1)
6476 return k2 ? -1 : 0;
6477 else if (!k2)
6478 return k1 ? 1 : 0;
6479
6480 return tree_int_cst_compare ((tree) k1, (tree) k2);
6481}
6482
e60a6f7b 6483/* Process a case label, located at LOC, for the range LOW_VALUE
6484 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6485 then this case label is actually a `default' label. If only
6486 HIGH_VALUE is NULL_TREE, then case label was declared using the
6487 usual C/C++ syntax, rather than the GNU case range extension.
6488 CASES is a tree containing all the case ranges processed so far;
be23b16f 6489 COND is the condition for the switch-statement itself.
6490 OUTSIDE_RANGE_P says whether there was a case value that doesn't
6491 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
6492 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
e41f0d80 6493
6494tree
e60a6f7b 6495c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
be23b16f 6496 tree low_value, tree high_value, bool *outside_range_p)
e41f0d80 6497{
6498 tree type;
6499 tree label;
6500 tree case_label;
6501 splay_tree_node node;
6502
6503 /* Create the LABEL_DECL itself. */
e60a6f7b 6504 label = create_artificial_label (loc);
e41f0d80 6505
6506 /* If there was an error processing the switch condition, bail now
6507 before we get more confused. */
6508 if (!cond || cond == error_mark_node)
4ee9c684 6509 goto error_out;
e41f0d80 6510
1cae46be 6511 if ((low_value && TREE_TYPE (low_value)
6512 && POINTER_TYPE_P (TREE_TYPE (low_value)))
e41f0d80 6513 || (high_value && TREE_TYPE (high_value)
6514 && POINTER_TYPE_P (TREE_TYPE (high_value))))
b96dc121 6515 {
e60a6f7b 6516 error_at (loc, "pointers are not permitted as case values");
b96dc121 6517 goto error_out;
6518 }
e41f0d80 6519
6520 /* Case ranges are a GNU extension. */
8864917d 6521 if (high_value)
29438999 6522 pedwarn (loc, OPT_Wpedantic,
8864917d 6523 "range expressions in switch statements are non-standard");
e41f0d80 6524
6525 type = TREE_TYPE (cond);
6526 if (low_value)
6527 {
2d2f6a15 6528 low_value = check_case_value (loc, low_value);
22a75734 6529 low_value = convert_and_check (loc, type, low_value);
96722196 6530 if (low_value == error_mark_node)
6531 goto error_out;
e41f0d80 6532 }
6533 if (high_value)
6534 {
2d2f6a15 6535 high_value = check_case_value (loc, high_value);
22a75734 6536 high_value = convert_and_check (loc, type, high_value);
96722196 6537 if (high_value == error_mark_node)
6538 goto error_out;
e41f0d80 6539 }
6540
96722196 6541 if (low_value && high_value)
6542 {
6543 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
a0c938f0 6544 really a case range, even though it was written that way.
6545 Remove the HIGH_VALUE to simplify later processing. */
96722196 6546 if (tree_int_cst_equal (low_value, high_value))
6547 high_value = NULL_TREE;
6548 else if (!tree_int_cst_lt (low_value, high_value))
e60a6f7b 6549 warning_at (loc, 0, "empty range specified");
96722196 6550 }
e41f0d80 6551
2ca392fd 6552 /* See if the case is in range of the type of the original testing
6553 expression. If both low_value and high_value are out of range,
6554 don't insert the case label and return NULL_TREE. */
6555 if (low_value
f61a9bc2 6556 && !check_case_bounds (loc, type, orig_type,
be23b16f 6557 &low_value, high_value ? &high_value : NULL,
6558 outside_range_p))
2ca392fd 6559 return NULL_TREE;
6560
e41f0d80 6561 /* Look up the LOW_VALUE in the table of case labels we already
6562 have. */
6563 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6564 /* If there was not an exact match, check for overlapping ranges.
6565 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6566 that's a `default' label and the only overlap is an exact match. */
6567 if (!node && (low_value || high_value))
6568 {
6569 splay_tree_node low_bound;
6570 splay_tree_node high_bound;
6571
6572 /* Even though there wasn't an exact match, there might be an
6573 overlap between this case range and another case range.
6574 Since we've (inductively) not allowed any overlapping case
6575 ranges, we simply need to find the greatest low case label
6576 that is smaller that LOW_VALUE, and the smallest low case
6577 label that is greater than LOW_VALUE. If there is an overlap
6578 it will occur in one of these two ranges. */
6579 low_bound = splay_tree_predecessor (cases,
6580 (splay_tree_key) low_value);
6581 high_bound = splay_tree_successor (cases,
6582 (splay_tree_key) low_value);
6583
6584 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6585 the LOW_VALUE, so there is no need to check unless the
6586 LOW_BOUND is in fact itself a case range. */
6587 if (low_bound
6588 && CASE_HIGH ((tree) low_bound->value)
6589 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6590 low_value) >= 0)
6591 node = low_bound;
6592 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6593 range is bigger than the low end of the current range, so we
6594 are only interested if the current range is a real range, and
6595 not an ordinary case label. */
1cae46be 6596 else if (high_bound
e41f0d80 6597 && high_value
6598 && (tree_int_cst_compare ((tree) high_bound->key,
6599 high_value)
6600 <= 0))
6601 node = high_bound;
6602 }
6603 /* If there was an overlap, issue an error. */
6604 if (node)
6605 {
eaae3b75 6606 tree duplicate = CASE_LABEL ((tree) node->value);
e41f0d80 6607
6608 if (high_value)
6609 {
e60a6f7b 6610 error_at (loc, "duplicate (or overlapping) case value");
6611 error_at (DECL_SOURCE_LOCATION (duplicate),
6612 "this is the first entry overlapping that value");
e41f0d80 6613 }
6614 else if (low_value)
6615 {
e60a6f7b 6616 error_at (loc, "duplicate case value") ;
6617 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
e41f0d80 6618 }
6619 else
6620 {
e60a6f7b 6621 error_at (loc, "multiple default labels in one switch");
6622 error_at (DECL_SOURCE_LOCATION (duplicate),
6623 "this is the first default label");
e41f0d80 6624 }
4ee9c684 6625 goto error_out;
e41f0d80 6626 }
6627
6628 /* Add a CASE_LABEL to the statement-tree. */
b6e3dd65 6629 case_label = add_stmt (build_case_label (low_value, high_value, label));
e41f0d80 6630 /* Register this case label in the splay tree. */
1cae46be 6631 splay_tree_insert (cases,
e41f0d80 6632 (splay_tree_key) low_value,
6633 (splay_tree_value) case_label);
6634
6635 return case_label;
4ee9c684 6636
6637 error_out:
daf6dff5 6638 /* Add a label so that the back-end doesn't think that the beginning of
4ee9c684 6639 the switch is unreachable. Note that we do not add a case label, as
a53ff4c1 6640 that just leads to duplicates and thence to failure later on. */
4ee9c684 6641 if (!cases->root)
6642 {
e60a6f7b 6643 tree t = create_artificial_label (loc);
6644 add_stmt (build_stmt (loc, LABEL_EXPR, t));
4ee9c684 6645 }
6646 return error_mark_node;
6647}
6648
6649/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6650 Used to verify that case values match up with enumerator values. */
6651
6652static void
6653match_case_to_enum_1 (tree key, tree type, tree label)
6654{
e913b5cd 6655 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6656
dd76621f 6657 if (tree_fits_uhwi_p (key))
6658 print_dec (key, buf, UNSIGNED);
6659 else if (tree_fits_shwi_p (key))
6660 print_dec (key, buf, SIGNED);
4ee9c684 6661 else
e913b5cd 6662 print_hex (key, buf);
4ee9c684 6663
6664 if (TYPE_NAME (type) == 0)
712d2297 6665 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6666 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6667 "case value %qs not in enumerated type",
6668 buf);
4ee9c684 6669 else
712d2297 6670 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6671 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6672 "case value %qs not in enumerated type %qT",
6673 buf, type);
4ee9c684 6674}
6675
359d87c6 6676/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6677 Used to verify that case values match up with enumerator values. */
6678
4ee9c684 6679static int
6680match_case_to_enum (splay_tree_node node, void *data)
6681{
6682 tree label = (tree) node->value;
4fd61bc6 6683 tree type = (tree) data;
4ee9c684 6684
6685 /* Skip default case. */
6686 if (!CASE_LOW (label))
6687 return 0;
6688
359d87c6 6689 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
4ee9c684 6690 when we did our enum->case scan. Reset our scratch bit after. */
359d87c6 6691 if (!CASE_LOW_SEEN (label))
4ee9c684 6692 match_case_to_enum_1 (CASE_LOW (label), type, label);
6693 else
359d87c6 6694 CASE_LOW_SEEN (label) = 0;
4ee9c684 6695
359d87c6 6696 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6697 not set, that means that CASE_HIGH did not appear when we did our
6698 enum->case scan. Reset our scratch bit after. */
4ee9c684 6699 if (CASE_HIGH (label))
6700 {
359d87c6 6701 if (!CASE_HIGH_SEEN (label))
6702 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6703 else
6704 CASE_HIGH_SEEN (label) = 0;
4ee9c684 6705 }
6706
6707 return 0;
6708}
6709
e7911019 6710/* Handle -Wswitch*. Called from the front end after parsing the
6711 switch construct. */
6712/* ??? Should probably be somewhere generic, since other languages
6713 besides C and C++ would want this. At the moment, however, C/C++
6714 are the only tree-ssa languages that support enumerations at all,
6715 so the point is moot. */
4ee9c684 6716
e7911019 6717void
6718c_do_switch_warnings (splay_tree cases, location_t switch_location,
be23b16f 6719 tree type, tree cond, bool bool_cond_p,
6720 bool outside_range_p)
4ee9c684 6721{
b27ac6b5 6722 splay_tree_node default_node;
359d87c6 6723 splay_tree_node node;
6724 tree chain;
4ee9c684 6725
be23b16f 6726 if (!warn_switch && !warn_switch_enum && !warn_switch_default
6727 && !warn_switch_bool)
4ee9c684 6728 return;
6729
4ee9c684 6730 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
8b6866af 6731 if (!default_node)
5fb6a912 6732 warning_at (switch_location, OPT_Wswitch_default,
6733 "switch missing default case");
4ee9c684 6734
be23b16f 6735 /* There are certain cases where -Wswitch-bool warnings aren't
6736 desirable, such as
6737 switch (boolean)
6738 {
6739 case true: ...
6740 case false: ...
6741 }
6742 so be careful here. */
6743 if (warn_switch_bool && bool_cond_p)
6744 {
6745 splay_tree_node min_node;
6746 /* If there's a default node, it's also the value with the minimal
6747 key. So look at the penultimate key (if any). */
6748 if (default_node)
6749 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
6750 else
6751 min_node = splay_tree_min (cases);
6752 tree min = min_node ? (tree) min_node->key : NULL_TREE;
6753
6754 splay_tree_node max_node = splay_tree_max (cases);
6755 /* This might be a case range, so look at the value with the
6756 maximal key and then check CASE_HIGH. */
6757 tree max = max_node ? (tree) max_node->value : NULL_TREE;
6758 if (max)
6759 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
6760
6761 /* If there's a case value > 1 or < 0, that is outside bool
6762 range, warn. */
6763 if (outside_range_p
6764 || (max && wi::gts_p (max, 1))
6765 || (min && wi::lts_p (min, 0))
6766 /* And handle the
6767 switch (boolean)
6768 {
6769 case true: ...
6770 case false: ...
6771 default: ...
6772 }
6773 case, where we want to warn. */
6774 || (default_node
6775 && max && wi::eq_p (max, 1)
6776 && min && wi::eq_p (min, 0)))
6777 warning_at (switch_location, OPT_Wswitch_bool,
6778 "switch condition has boolean value");
6779 }
6780
47ae02b7 6781 /* From here on, we only care about enumerated types. */
359d87c6 6782 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6783 return;
6784
561017b5 6785 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6786 if (!warn_switch_enum && !warn_switch)
359d87c6 6787 return;
6788
561017b5 6789 /* Check the cases. Warn about case values which are not members of
6790 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6791 there is no default case, check that exactly all enumeration
6792 literals are covered by the cases. */
6793
359d87c6 6794 /* Clearing COND if it is not an integer constant simplifies
6795 the tests inside the loop below. */
6796 if (TREE_CODE (cond) != INTEGER_CST)
6797 cond = NULL_TREE;
6798
6799 /* The time complexity here is O(N*lg(N)) worst case, but for the
6800 common case of monotonically increasing enumerators, it is
6801 O(N), since the nature of the splay tree will keep the next
6802 element adjacent to the root at all times. */
4ee9c684 6803
359d87c6 6804 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6805 {
6806 tree value = TREE_VALUE (chain);
3f00a6c0 6807 if (TREE_CODE (value) == CONST_DECL)
6808 value = DECL_INITIAL (value);
359d87c6 6809 node = splay_tree_lookup (cases, (splay_tree_key) value);
6810 if (node)
4ee9c684 6811 {
359d87c6 6812 /* Mark the CASE_LOW part of the case entry as seen. */
6813 tree label = (tree) node->value;
6814 CASE_LOW_SEEN (label) = 1;
6815 continue;
6816 }
6817
6818 /* Even though there wasn't an exact match, there might be a
f0b5f617 6819 case range which includes the enumerator's value. */
359d87c6 6820 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6821 if (node && CASE_HIGH ((tree) node->value))
6822 {
6823 tree label = (tree) node->value;
6824 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6825 if (cmp >= 0)
4ee9c684 6826 {
359d87c6 6827 /* If we match the upper bound exactly, mark the CASE_HIGH
6828 part of the case entry as seen. */
6829 if (cmp == 0)
6830 CASE_HIGH_SEEN (label) = 1;
6831 continue;
4ee9c684 6832 }
6833 }
6834
359d87c6 6835 /* We've now determined that this enumerated literal isn't
6836 handled by the case labels of the switch statement. */
4ee9c684 6837
359d87c6 6838 /* If the switch expression is a constant, we only really care
6839 about whether that constant is handled by the switch. */
6840 if (cond && tree_int_cst_compare (cond, value))
6841 continue;
4ee9c684 6842
6cbbbc89 6843 /* If there is a default_node, the only relevant option is
561017b5 6844 Wswitch-enum. Otherwise, if both are enabled then we prefer
6cbbbc89 6845 to warn using -Wswitch because -Wswitch is enabled by -Wall
6846 while -Wswitch-enum is explicit. */
561017b5 6847 warning_at (switch_location,
6848 (default_node || !warn_switch
6849 ? OPT_Wswitch_enum
6850 : OPT_Wswitch),
6851 "enumeration value %qE not handled in switch",
6852 TREE_PURPOSE (chain));
4ee9c684 6853 }
359d87c6 6854
6855 /* Warn if there are case expressions that don't correspond to
6856 enumerators. This can occur since C and C++ don't enforce
6857 type-checking of assignments to enumeration variables.
6858
6859 The time complexity here is now always O(N) worst case, since
6860 we should have marked both the lower bound and upper bound of
6861 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6862 above. This scan also resets those fields. */
6cbbbc89 6863
359d87c6 6864 splay_tree_foreach (cases, match_case_to_enum, type);
e41f0d80 6865}
6866
9dd48740 6867/* Finish an expression taking the address of LABEL (an
dda49785 6868 IDENTIFIER_NODE). Returns an expression for the address.
6869
6870 LOC is the location for the expression returned. */
d0a47c8d 6871
1cae46be 6872tree
dda49785 6873finish_label_address_expr (tree label, location_t loc)
d0a47c8d 6874{
6875 tree result;
6876
29438999 6877 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
d0a47c8d 6878
9dd48740 6879 if (label == error_mark_node)
6880 return error_mark_node;
6881
d0a47c8d 6882 label = lookup_label (label);
6883 if (label == NULL_TREE)
6884 result = null_pointer_node;
6885 else
6886 {
6887 TREE_USED (label) = 1;
6888 result = build1 (ADDR_EXPR, ptr_type_node, label);
344cd9b2 6889 /* The current function is not necessarily uninlinable.
d0a47c8d 6890 Computed gotos are incompatible with inlining, but the value
6891 here could be used only in a diagnostic, for example. */
dda49785 6892 protected_set_expr_location (result, loc);
d0a47c8d 6893 }
6894
6895 return result;
6896}
4f9a1c9b 6897\f
6898
6899/* Given a boolean expression ARG, return a tree representing an increment
6900 or decrement (as indicated by CODE) of ARG. The front end must check for
6901 invalid cases (e.g., decrement in C++). */
6902tree
1cae46be 6903boolean_increment (enum tree_code code, tree arg)
4f9a1c9b 6904{
6905 tree val;
69db191c 6906 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
c0f19401 6907
4f9a1c9b 6908 arg = stabilize_reference (arg);
6909 switch (code)
6910 {
6911 case PREINCREMENT_EXPR:
14ae0310 6912 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6913 break;
6914 case POSTINCREMENT_EXPR:
14ae0310 6915 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6916 arg = save_expr (arg);
14ae0310 6917 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6918 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6919 break;
6920 case PREDECREMENT_EXPR:
14ae0310 6921 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6922 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6923 break;
6924 case POSTDECREMENT_EXPR:
14ae0310 6925 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6926 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6927 arg = save_expr (arg);
14ae0310 6928 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6929 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6930 break;
6931 default:
231bd014 6932 gcc_unreachable ();
4f9a1c9b 6933 }
6934 TREE_SIDE_EFFECTS (val) = 1;
6935 return val;
6936}
76a6e674 6937\f
f3449a3c 6938/* Built-in macros for stddef.h and stdint.h, that require macros
6939 defined in this file. */
79cf3ec1 6940void
1cae46be 6941c_stddef_cpp_builtins(void)
1ed9d5f5 6942{
63994318 6943 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6944 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6945 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6946 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
36bccbfc 6947 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6948 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
b0726616 6949 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6950 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
f3449a3c 6951 if (SIG_ATOMIC_TYPE)
6952 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6953 if (INT8_TYPE)
6954 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6955 if (INT16_TYPE)
6956 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6957 if (INT32_TYPE)
6958 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6959 if (INT64_TYPE)
6960 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6961 if (UINT8_TYPE)
6962 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6963 if (UINT16_TYPE)
6964 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6965 if (UINT32_TYPE)
6966 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6967 if (UINT64_TYPE)
6968 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6969 if (INT_LEAST8_TYPE)
6970 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6971 if (INT_LEAST16_TYPE)
6972 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6973 if (INT_LEAST32_TYPE)
6974 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6975 if (INT_LEAST64_TYPE)
6976 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6977 if (UINT_LEAST8_TYPE)
6978 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6979 if (UINT_LEAST16_TYPE)
6980 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6981 if (UINT_LEAST32_TYPE)
6982 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6983 if (UINT_LEAST64_TYPE)
6984 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6985 if (INT_FAST8_TYPE)
6986 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6987 if (INT_FAST16_TYPE)
6988 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6989 if (INT_FAST32_TYPE)
6990 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6991 if (INT_FAST64_TYPE)
6992 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6993 if (UINT_FAST8_TYPE)
6994 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6995 if (UINT_FAST16_TYPE)
6996 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6997 if (UINT_FAST32_TYPE)
6998 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6999 if (UINT_FAST64_TYPE)
7000 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
7001 if (INTPTR_TYPE)
7002 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
7003 if (UINTPTR_TYPE)
7004 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
574006c3 7005}
7006
7d3b509a 7007static void
1cae46be 7008c_init_attributes (void)
7d3b509a 7009{
7010 /* Fill in the built_in_attributes array. */
7c446c95 7011#define DEF_ATTR_NULL_TREE(ENUM) \
7d3b509a 7012 built_in_attributes[(int) ENUM] = NULL_TREE;
7c446c95 7013#define DEF_ATTR_INT(ENUM, VALUE) \
ceb7b692 7014 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
c8010b80 7015#define DEF_ATTR_STRING(ENUM, VALUE) \
7016 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
7d3b509a 7017#define DEF_ATTR_IDENT(ENUM, STRING) \
7018 built_in_attributes[(int) ENUM] = get_identifier (STRING);
7019#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
7020 built_in_attributes[(int) ENUM] \
7021 = tree_cons (built_in_attributes[(int) PURPOSE], \
7022 built_in_attributes[(int) VALUE], \
7023 built_in_attributes[(int) CHAIN]);
7d3b509a 7024#include "builtin-attrs.def"
7025#undef DEF_ATTR_NULL_TREE
7026#undef DEF_ATTR_INT
7027#undef DEF_ATTR_IDENT
7028#undef DEF_ATTR_TREE_LIST
76a6e674 7029}
5f3cead1 7030
33199a81 7031/* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
7032 identifier as an argument, so the front end shouldn't look it up. */
7033
7034bool
47b19b94 7035attribute_takes_identifier_p (const_tree attr_id)
33199a81 7036{
9bf1c74e 7037 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
02cb1060 7038 if (spec == NULL)
7039 /* Unknown attribute that we'll end up ignoring, return true so we
7040 don't complain about an identifier argument. */
7041 return true;
7042 else if (!strcmp ("mode", spec->name)
7043 || !strcmp ("format", spec->name)
7044 || !strcmp ("cleanup", spec->name))
47b19b94 7045 return true;
7046 else
7047 return targetm.attribute_takes_identifier_p (attr_id);
33199a81 7048}
7049
f8e93a2e 7050/* Attribute handlers common to C front ends. */
7051
7052/* Handle a "packed" attribute; arguments as in
7053 struct attribute_spec.handler. */
7054
7055static tree
9a03a746 7056handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
09347743 7057 int flags, bool *no_add_attrs)
f8e93a2e 7058{
f40175cb 7059 if (TYPE_P (*node))
f8e93a2e 7060 {
7061 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 7062 *node = build_variant_type_copy (*node);
f40175cb 7063 TYPE_PACKED (*node) = 1;
f8e93a2e 7064 }
7065 else if (TREE_CODE (*node) == FIELD_DECL)
c2ab04f9 7066 {
9fd767c5 7067 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
7068 /* Still pack bitfields. */
7069 && ! DECL_INITIAL (*node))
c2ab04f9 7070 warning (OPT_Wattributes,
7071 "%qE attribute ignored for field of type %qT",
7072 name, TREE_TYPE (*node));
7073 else
7074 DECL_PACKED (*node) = 1;
7075 }
f8e93a2e 7076 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
f40175cb 7077 used for DECL_REGISTER. It wouldn't mean anything anyway.
7078 We can't set DECL_PACKED on the type of a TYPE_DECL, because
7079 that changes what the typedef is typing. */
f8e93a2e 7080 else
7081 {
9b2d6d13 7082 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7083 *no_add_attrs = true;
7084 }
7085
7086 return NULL_TREE;
7087}
7088
7089/* Handle a "nocommon" attribute; arguments as in
7090 struct attribute_spec.handler. */
7091
7092static tree
1cae46be 7093handle_nocommon_attribute (tree *node, tree name,
9a03a746 7094 tree ARG_UNUSED (args),
7095 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7096{
f48c7f4a 7097 if (VAR_P (*node))
f8e93a2e 7098 DECL_COMMON (*node) = 0;
7099 else
7100 {
9b2d6d13 7101 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7102 *no_add_attrs = true;
7103 }
7104
7105 return NULL_TREE;
7106}
7107
7108/* Handle a "common" attribute; arguments as in
7109 struct attribute_spec.handler. */
7110
7111static tree
9a03a746 7112handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7113 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7114{
f48c7f4a 7115 if (VAR_P (*node))
f8e93a2e 7116 DECL_COMMON (*node) = 1;
7117 else
7118 {
9b2d6d13 7119 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7120 *no_add_attrs = true;
7121 }
7122
7123 return NULL_TREE;
7124}
7125
7126/* Handle a "noreturn" attribute; arguments as in
7127 struct attribute_spec.handler. */
7128
7129static tree
9a03a746 7130handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7131 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7132{
7133 tree type = TREE_TYPE (*node);
7134
7135 /* See FIXME comment in c_common_attribute_table. */
8c582e4f 7136 if (TREE_CODE (*node) == FUNCTION_DECL
7137 || objc_method_decl (TREE_CODE (*node)))
f8e93a2e 7138 TREE_THIS_VOLATILE (*node) = 1;
7139 else if (TREE_CODE (type) == POINTER_TYPE
7140 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7141 TREE_TYPE (*node)
9d4eeb52 7142 = (build_qualified_type
7143 (build_pointer_type
7144 (build_type_variant (TREE_TYPE (type),
7145 TYPE_READONLY (TREE_TYPE (type)), 1)),
7146 TYPE_QUALS (type)));
f8e93a2e 7147 else
7148 {
9b2d6d13 7149 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7150 *no_add_attrs = true;
7151 }
7152
7153 return NULL_TREE;
7154}
7155
5de92639 7156/* Handle a "hot" and attribute; arguments as in
7157 struct attribute_spec.handler. */
7158
7159static tree
7160handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
46f8e3b0 7161 int ARG_UNUSED (flags), bool *no_add_attrs)
5de92639 7162{
758a38ab 7163 if (TREE_CODE (*node) == FUNCTION_DECL
7164 || TREE_CODE (*node) == LABEL_DECL)
5de92639 7165 {
7166 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
7167 {
4a026b48 7168 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7169 "with attribute %qs", name, "cold");
5de92639 7170 *no_add_attrs = true;
7171 }
24470055 7172 /* Most of the rest of the hot processing is done later with
7173 lookup_attribute. */
5de92639 7174 }
7175 else
7176 {
7177 warning (OPT_Wattributes, "%qE attribute ignored", name);
7178 *no_add_attrs = true;
7179 }
7180
7181 return NULL_TREE;
7182}
758a38ab 7183
5de92639 7184/* Handle a "cold" and attribute; arguments as in
7185 struct attribute_spec.handler. */
7186
7187static tree
7188handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7189 int ARG_UNUSED (flags), bool *no_add_attrs)
7190{
758a38ab 7191 if (TREE_CODE (*node) == FUNCTION_DECL
7192 || TREE_CODE (*node) == LABEL_DECL)
5de92639 7193 {
7194 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
7195 {
4a026b48 7196 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7197 "with attribute %qs", name, "hot");
5de92639 7198 *no_add_attrs = true;
7199 }
24470055 7200 /* Most of the rest of the cold processing is done later with
7201 lookup_attribute. */
5de92639 7202 }
7203 else
7204 {
7205 warning (OPT_Wattributes, "%qE attribute ignored", name);
7206 *no_add_attrs = true;
7207 }
7208
7209 return NULL_TREE;
7210}
7211
a9196da9 7212/* Handle a "no_sanitize_address" attribute; arguments as in
d413ffdd 7213 struct attribute_spec.handler. */
7214
7215static tree
a9196da9 7216handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
7217 bool *no_add_attrs)
d413ffdd 7218{
7219 if (TREE_CODE (*node) != FUNCTION_DECL)
7220 {
7221 warning (OPT_Wattributes, "%qE attribute ignored", name);
7222 *no_add_attrs = true;
7223 }
7224
7225 return NULL_TREE;
7226}
7227
a9196da9 7228/* Handle a "no_address_safety_analysis" attribute; arguments as in
7229 struct attribute_spec.handler. */
7230
7231static tree
7232handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
7233 bool *no_add_attrs)
7234{
7235 if (TREE_CODE (*node) != FUNCTION_DECL)
7236 warning (OPT_Wattributes, "%qE attribute ignored", name);
7237 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
7238 DECL_ATTRIBUTES (*node)
7239 = tree_cons (get_identifier ("no_sanitize_address"),
7240 NULL_TREE, DECL_ATTRIBUTES (*node));
7241 *no_add_attrs = true;
7242 return NULL_TREE;
7243}
7244
05f893e1 7245/* Handle a "no_sanitize_undefined" attribute; arguments as in
7246 struct attribute_spec.handler. */
7247
7248static tree
7249handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
7250 bool *no_add_attrs)
7251{
7252 if (TREE_CODE (*node) != FUNCTION_DECL)
7253 {
7254 warning (OPT_Wattributes, "%qE attribute ignored", name);
7255 *no_add_attrs = true;
7256 }
7257
7258 return NULL_TREE;
7259}
7260
947aa916 7261/* Handle a "stack_protect" attribute; arguments as in
7262 struct attribute_spec.handler. */
7263static tree
7264handle_stack_protect_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 else
7273 DECL_ATTRIBUTES (*node)
7274 = tree_cons (get_identifier ("stack_protect"),
7275 NULL_TREE, DECL_ATTRIBUTES (*node));
7276
7277 return NULL_TREE;
7278}
7279
f8e93a2e 7280/* Handle a "noinline" attribute; arguments as in
7281 struct attribute_spec.handler. */
7282
7283static tree
1cae46be 7284handle_noinline_attribute (tree *node, tree name,
9a03a746 7285 tree ARG_UNUSED (args),
7286 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7287{
7288 if (TREE_CODE (*node) == FUNCTION_DECL)
4a026b48 7289 {
7290 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
7291 {
7292 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7293 "with attribute %qs", name, "always_inline");
7294 *no_add_attrs = true;
7295 }
7296 else
7297 DECL_UNINLINABLE (*node) = 1;
7298 }
f8e93a2e 7299 else
7300 {
9b2d6d13 7301 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7302 *no_add_attrs = true;
7303 }
7304
7305 return NULL_TREE;
7306}
7307
bdb1f0d1 7308/* Handle a "noclone" attribute; arguments as in
7309 struct attribute_spec.handler. */
7310
7311static tree
7312handle_noclone_attribute (tree *node, tree name,
7313 tree ARG_UNUSED (args),
7314 int ARG_UNUSED (flags), bool *no_add_attrs)
7315{
7316 if (TREE_CODE (*node) != FUNCTION_DECL)
7317 {
7318 warning (OPT_Wattributes, "%qE attribute ignored", name);
7319 *no_add_attrs = true;
7320 }
7321
7322 return NULL_TREE;
7323}
7324
85fbea97 7325/* Handle a "no_icf" attribute; arguments as in
7326 struct attribute_spec.handler. */
7327
7328static tree
7329handle_noicf_attribute (tree *node, tree name,
7330 tree ARG_UNUSED (args),
7331 int ARG_UNUSED (flags), bool *no_add_attrs)
7332{
7333 if (TREE_CODE (*node) != FUNCTION_DECL)
7334 {
7335 warning (OPT_Wattributes, "%qE attribute ignored", name);
7336 *no_add_attrs = true;
7337 }
7338
7339 return NULL_TREE;
7340}
7341
7342
f8e93a2e 7343/* Handle a "always_inline" attribute; arguments as in
7344 struct attribute_spec.handler. */
7345
7346static tree
1cae46be 7347handle_always_inline_attribute (tree *node, tree name,
9a03a746 7348 tree ARG_UNUSED (args),
7349 int ARG_UNUSED (flags),
09347743 7350 bool *no_add_attrs)
f8e93a2e 7351{
7352 if (TREE_CODE (*node) == FUNCTION_DECL)
7353 {
4a026b48 7354 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
7355 {
7356 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7357 "with %qs attribute", name, "noinline");
7358 *no_add_attrs = true;
7359 }
7360 else
7361 /* Set the attribute and mark it for disregarding inline
7362 limits. */
7363 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
f8e93a2e 7364 }
7365 else
7366 {
9b2d6d13 7367 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7368 *no_add_attrs = true;
541e4101 7369 }
7370
7371 return NULL_TREE;
7372}
7373
7374/* Handle a "gnu_inline" attribute; arguments as in
7375 struct attribute_spec.handler. */
7376
7377static tree
7378handle_gnu_inline_attribute (tree *node, tree name,
7379 tree ARG_UNUSED (args),
7380 int ARG_UNUSED (flags),
7381 bool *no_add_attrs)
7382{
7383 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7384 {
7385 /* Do nothing else, just set the attribute. We'll get at
7386 it later with lookup_attribute. */
7387 }
7388 else
7389 {
7390 warning (OPT_Wattributes, "%qE attribute ignored", name);
7391 *no_add_attrs = true;
7bd95dfd 7392 }
7393
7394 return NULL_TREE;
7395}
7396
7397/* Handle a "leaf" attribute; arguments as in
7398 struct attribute_spec.handler. */
7399
7400static tree
7401handle_leaf_attribute (tree *node, tree name,
7402 tree ARG_UNUSED (args),
7403 int ARG_UNUSED (flags), bool *no_add_attrs)
7404{
7405 if (TREE_CODE (*node) != FUNCTION_DECL)
7406 {
7407 warning (OPT_Wattributes, "%qE attribute ignored", name);
7408 *no_add_attrs = true;
7409 }
7410 if (!TREE_PUBLIC (*node))
7411 {
7412 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
7413 *no_add_attrs = true;
f8e93a2e 7414 }
7415
7416 return NULL_TREE;
7417}
7418
1b16fc45 7419/* Handle an "artificial" attribute; arguments as in
7420 struct attribute_spec.handler. */
7421
7422static tree
7423handle_artificial_attribute (tree *node, tree name,
7424 tree ARG_UNUSED (args),
7425 int ARG_UNUSED (flags),
7426 bool *no_add_attrs)
7427{
7428 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7429 {
7430 /* Do nothing else, just set the attribute. We'll get at
7431 it later with lookup_attribute. */
7432 }
7433 else
7434 {
7435 warning (OPT_Wattributes, "%qE attribute ignored", name);
7436 *no_add_attrs = true;
7437 }
7438
7439 return NULL_TREE;
7440}
7441
0cdd9887 7442/* Handle a "flatten" attribute; arguments as in
7443 struct attribute_spec.handler. */
7444
7445static tree
7446handle_flatten_attribute (tree *node, tree name,
a0c938f0 7447 tree args ATTRIBUTE_UNUSED,
7448 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
0cdd9887 7449{
7450 if (TREE_CODE (*node) == FUNCTION_DECL)
7451 /* Do nothing else, just set the attribute. We'll get at
7452 it later with lookup_attribute. */
7453 ;
7454 else
7455 {
7456 warning (OPT_Wattributes, "%qE attribute ignored", name);
7457 *no_add_attrs = true;
7458 }
7459
7460 return NULL_TREE;
7461}
7462
10fc867f 7463/* Handle a "warning" or "error" attribute; arguments as in
7464 struct attribute_spec.handler. */
7465
7466static tree
7467handle_error_attribute (tree *node, tree name, tree args,
7468 int ARG_UNUSED (flags), bool *no_add_attrs)
7469{
7470 if (TREE_CODE (*node) == FUNCTION_DECL
df936998 7471 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
10fc867f 7472 /* Do nothing else, just set the attribute. We'll get at
7473 it later with lookup_attribute. */
7474 ;
7475 else
7476 {
7477 warning (OPT_Wattributes, "%qE attribute ignored", name);
7478 *no_add_attrs = true;
7479 }
7480
7481 return NULL_TREE;
7482}
0cdd9887 7483
f8e93a2e 7484/* Handle a "used" attribute; arguments as in
7485 struct attribute_spec.handler. */
7486
7487static tree
9a03a746 7488handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7489 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7490{
d0a31bd8 7491 tree node = *pnode;
7492
7493 if (TREE_CODE (node) == FUNCTION_DECL
f48c7f4a 7494 || (VAR_P (node) && TREE_STATIC (node))
a4e3ffad 7495 || (TREE_CODE (node) == TYPE_DECL))
f54ed8bc 7496 {
f54ed8bc 7497 TREE_USED (node) = 1;
9423c9b7 7498 DECL_PRESERVE_P (node) = 1;
f48c7f4a 7499 if (VAR_P (node))
abc6c64f 7500 DECL_READ_P (node) = 1;
f54ed8bc 7501 }
f8e93a2e 7502 else
7503 {
9b2d6d13 7504 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7505 *no_add_attrs = true;
7506 }
7507
7508 return NULL_TREE;
7509}
7510
7511/* Handle a "unused" attribute; arguments as in
7512 struct attribute_spec.handler. */
7513
7514static tree
9a03a746 7515handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7516 int flags, bool *no_add_attrs)
f8e93a2e 7517{
7518 if (DECL_P (*node))
7519 {
7520 tree decl = *node;
7521
7522 if (TREE_CODE (decl) == PARM_DECL
4e81b384 7523 || VAR_OR_FUNCTION_DECL_P (decl)
f8e93a2e 7524 || TREE_CODE (decl) == LABEL_DECL
7525 || TREE_CODE (decl) == TYPE_DECL)
abc6c64f 7526 {
7527 TREE_USED (decl) = 1;
f48c7f4a 7528 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
abc6c64f 7529 DECL_READ_P (decl) = 1;
7530 }
f8e93a2e 7531 else
7532 {
9b2d6d13 7533 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7534 *no_add_attrs = true;
7535 }
7536 }
7537 else
7538 {
7539 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 7540 *node = build_variant_type_copy (*node);
f8e93a2e 7541 TREE_USED (*node) = 1;
7542 }
7543
7544 return NULL_TREE;
7545}
7546
62eec3b4 7547/* Handle a "externally_visible" attribute; arguments as in
7548 struct attribute_spec.handler. */
7549
7550static tree
7551handle_externally_visible_attribute (tree *pnode, tree name,
7552 tree ARG_UNUSED (args),
7553 int ARG_UNUSED (flags),
7554 bool *no_add_attrs)
7555{
7556 tree node = *pnode;
7557
b443c459 7558 if (VAR_OR_FUNCTION_DECL_P (node))
62eec3b4 7559 {
ba12ea31 7560 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7561 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7562 {
7563 warning (OPT_Wattributes,
7564 "%qE attribute have effect only on public objects", name);
7565 *no_add_attrs = true;
7566 }
62eec3b4 7567 }
62eec3b4 7568 else
7569 {
7570 warning (OPT_Wattributes, "%qE attribute ignored", name);
7571 *no_add_attrs = true;
7572 }
7573
7574 return NULL_TREE;
7575}
7576
6b722052 7577/* Handle the "no_reorder" attribute. Arguments as in
7578 struct attribute_spec.handler. */
7579
7580static tree
7581handle_no_reorder_attribute (tree *pnode,
7582 tree name,
7583 tree,
7584 int,
7585 bool *no_add_attrs)
7586{
7587 tree node = *pnode;
7588
b443c459 7589 if (!VAR_OR_FUNCTION_DECL_P (node)
6b722052 7590 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7591 {
7592 warning (OPT_Wattributes,
7593 "%qE attribute only affects top level objects",
7594 name);
7595 *no_add_attrs = true;
7596 }
7597
7598 return NULL_TREE;
7599}
7600
f8e93a2e 7601/* Handle a "const" attribute; arguments as in
7602 struct attribute_spec.handler. */
7603
7604static tree
9a03a746 7605handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7606 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7607{
7608 tree type = TREE_TYPE (*node);
7609
7610 /* See FIXME comment on noreturn in c_common_attribute_table. */
7611 if (TREE_CODE (*node) == FUNCTION_DECL)
7612 TREE_READONLY (*node) = 1;
7613 else if (TREE_CODE (type) == POINTER_TYPE
7614 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7615 TREE_TYPE (*node)
9d4eeb52 7616 = (build_qualified_type
7617 (build_pointer_type
7618 (build_type_variant (TREE_TYPE (type), 1,
7619 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7620 TYPE_QUALS (type)));
f8e93a2e 7621 else
7622 {
9b2d6d13 7623 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7624 *no_add_attrs = true;
7625 }
7626
7627 return NULL_TREE;
7628}
7629
7630/* Handle a "transparent_union" attribute; arguments as in
7631 struct attribute_spec.handler. */
7632
7633static tree
1cae46be 7634handle_transparent_union_attribute (tree *node, tree name,
9a03a746 7635 tree ARG_UNUSED (args), int flags,
09347743 7636 bool *no_add_attrs)
f8e93a2e 7637{
881eb642 7638 tree type;
03908818 7639
7640 *no_add_attrs = true;
f8e93a2e 7641
ffcdbf9c 7642
7643 if (TREE_CODE (*node) == TYPE_DECL
7644 && ! (flags & ATTR_FLAG_CXX11))
881eb642 7645 node = &TREE_TYPE (*node);
7646 type = *node;
f8e93a2e 7647
03908818 7648 if (TREE_CODE (type) == UNION_TYPE)
f8e93a2e 7649 {
fca86134 7650 /* Make sure that the first field will work for a transparent union.
7651 If the type isn't complete yet, leave the check to the code in
7652 finish_struct. */
7653 if (TYPE_SIZE (type))
7654 {
7655 tree first = first_field (type);
7656 if (first == NULL_TREE
7657 || DECL_ARTIFICIAL (first)
7658 || TYPE_MODE (type) != DECL_MODE (first))
7659 goto ignored;
7660 }
7661
f8e93a2e 7662 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
03908818 7663 {
fca86134 7664 /* If the type isn't complete yet, setting the flag
7665 on a variant wouldn't ever be checked. */
7666 if (!TYPE_SIZE (type))
7667 goto ignored;
7668
7669 /* build_duplicate_type doesn't work for C++. */
7670 if (c_dialect_cxx ())
03908818 7671 goto ignored;
7672
7673 /* A type variant isn't good enough, since we don't a cast
7674 to such a type removed as a no-op. */
7675 *node = type = build_duplicate_type (type);
7676 }
7677
8df5a43d 7678 TYPE_TRANSPARENT_AGGR (type) = 1;
03908818 7679 return NULL_TREE;
f8e93a2e 7680 }
7681
03908818 7682 ignored:
7683 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7684 return NULL_TREE;
7685}
7686
9af7fd5b 7687/* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7688 get the requested priority for a constructor or destructor,
7689 possibly issuing diagnostics for invalid or reserved
7690 priorities. */
7691
7692static priority_type
7693get_priority (tree args, bool is_destructor)
7694{
7695 HOST_WIDE_INT pri;
6c181a06 7696 tree arg;
9af7fd5b 7697
7698 if (!args)
7699 return DEFAULT_INIT_PRIORITY;
48e1416a 7700
28fbc04f 7701 if (!SUPPORTS_INIT_PRIORITY)
7702 {
7703 if (is_destructor)
7704 error ("destructor priorities are not supported");
7705 else
7706 error ("constructor priorities are not supported");
7707 return DEFAULT_INIT_PRIORITY;
7708 }
7709
6c181a06 7710 arg = TREE_VALUE (args);
253e1cae 7711 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7712 goto invalid;
7713 if (arg == error_mark_node)
7714 return DEFAULT_INIT_PRIORITY;
c28ddc97 7715 arg = default_conversion (arg);
e913b5cd 7716 if (!tree_fits_shwi_p (arg)
6c181a06 7717 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
9af7fd5b 7718 goto invalid;
7719
e913b5cd 7720 pri = tree_to_shwi (arg);
9af7fd5b 7721 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7722 goto invalid;
7723
7724 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7725 {
7726 if (is_destructor)
7727 warning (0,
7728 "destructor priorities from 0 to %d are reserved "
48e1416a 7729 "for the implementation",
9af7fd5b 7730 MAX_RESERVED_INIT_PRIORITY);
7731 else
7732 warning (0,
7733 "constructor priorities from 0 to %d are reserved "
48e1416a 7734 "for the implementation",
9af7fd5b 7735 MAX_RESERVED_INIT_PRIORITY);
7736 }
7737 return pri;
7738
7739 invalid:
7740 if (is_destructor)
7741 error ("destructor priorities must be integers from 0 to %d inclusive",
7742 MAX_INIT_PRIORITY);
7743 else
7744 error ("constructor priorities must be integers from 0 to %d inclusive",
7745 MAX_INIT_PRIORITY);
7746 return DEFAULT_INIT_PRIORITY;
7747}
7748
f8e93a2e 7749/* Handle a "constructor" attribute; arguments as in
7750 struct attribute_spec.handler. */
7751
7752static tree
9af7fd5b 7753handle_constructor_attribute (tree *node, tree name, tree args,
9a03a746 7754 int ARG_UNUSED (flags),
09347743 7755 bool *no_add_attrs)
f8e93a2e 7756{
7757 tree decl = *node;
7758 tree type = TREE_TYPE (decl);
7759
7760 if (TREE_CODE (decl) == FUNCTION_DECL
7761 && TREE_CODE (type) == FUNCTION_TYPE
7762 && decl_function_context (decl) == 0)
7763 {
9af7fd5b 7764 priority_type priority;
f8e93a2e 7765 DECL_STATIC_CONSTRUCTOR (decl) = 1;
9af7fd5b 7766 priority = get_priority (args, /*is_destructor=*/false);
7767 SET_DECL_INIT_PRIORITY (decl, priority);
f8e93a2e 7768 TREE_USED (decl) = 1;
7769 }
7770 else
7771 {
9b2d6d13 7772 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7773 *no_add_attrs = true;
7774 }
7775
7776 return NULL_TREE;
7777}
7778
7779/* Handle a "destructor" attribute; arguments as in
7780 struct attribute_spec.handler. */
7781
7782static tree
9af7fd5b 7783handle_destructor_attribute (tree *node, tree name, tree args,
9a03a746 7784 int ARG_UNUSED (flags),
09347743 7785 bool *no_add_attrs)
f8e93a2e 7786{
7787 tree decl = *node;
7788 tree type = TREE_TYPE (decl);
7789
7790 if (TREE_CODE (decl) == FUNCTION_DECL
7791 && TREE_CODE (type) == FUNCTION_TYPE
7792 && decl_function_context (decl) == 0)
7793 {
9af7fd5b 7794 priority_type priority;
f8e93a2e 7795 DECL_STATIC_DESTRUCTOR (decl) = 1;
9af7fd5b 7796 priority = get_priority (args, /*is_destructor=*/true);
7797 SET_DECL_FINI_PRIORITY (decl, priority);
f8e93a2e 7798 TREE_USED (decl) = 1;
7799 }
7800 else
7801 {
9b2d6d13 7802 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7803 *no_add_attrs = true;
7804 }
7805
7806 return NULL_TREE;
7807}
7808
1c58e3f1 7809/* Nonzero if the mode is a valid vector mode for this architecture.
7810 This returns nonzero even if there is no hardware support for the
7811 vector mode, but we can emulate with narrower modes. */
7812
7813static int
3754d046 7814vector_mode_valid_p (machine_mode mode)
1c58e3f1 7815{
7816 enum mode_class mclass = GET_MODE_CLASS (mode);
3754d046 7817 machine_mode innermode;
1c58e3f1 7818
7819 /* Doh! What's going on? */
7820 if (mclass != MODE_VECTOR_INT
7821 && mclass != MODE_VECTOR_FLOAT
7822 && mclass != MODE_VECTOR_FRACT
7823 && mclass != MODE_VECTOR_UFRACT
7824 && mclass != MODE_VECTOR_ACCUM
7825 && mclass != MODE_VECTOR_UACCUM)
7826 return 0;
7827
7828 /* Hardware support. Woo hoo! */
7829 if (targetm.vector_mode_supported_p (mode))
7830 return 1;
7831
7832 innermode = GET_MODE_INNER (mode);
7833
7834 /* We should probably return 1 if requesting V4DI and we have no DI,
7835 but we have V2DI, but this is probably very unlikely. */
7836
7837 /* If we have support for the inner mode, we can safely emulate it.
7838 We may not have V2DI, but me can emulate with a pair of DIs. */
7839 return targetm.scalar_mode_supported_p (innermode);
7840}
7841
7842
f8e93a2e 7843/* Handle a "mode" attribute; arguments as in
7844 struct attribute_spec.handler. */
7845
7846static tree
9a03a746 7847handle_mode_attribute (tree *node, tree name, tree args,
7848 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7849{
7850 tree type = *node;
d1dd9ac0 7851 tree ident = TREE_VALUE (args);
f8e93a2e 7852
7853 *no_add_attrs = true;
ab2c1de8 7854
d1dd9ac0 7855 if (TREE_CODE (ident) != IDENTIFIER_NODE)
9b2d6d13 7856 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7857 else
7858 {
7859 int j;
d1dd9ac0 7860 const char *p = IDENTIFIER_POINTER (ident);
f8e93a2e 7861 int len = strlen (p);
3754d046 7862 machine_mode mode = VOIDmode;
f8e93a2e 7863 tree typefm;
b2aef146 7864 bool valid_mode;
f8e93a2e 7865
7866 if (len > 4 && p[0] == '_' && p[1] == '_'
7867 && p[len - 1] == '_' && p[len - 2] == '_')
7868 {
4fd61bc6 7869 char *newp = (char *) alloca (len - 1);
f8e93a2e 7870
7871 strcpy (newp, &p[2]);
7872 newp[len - 4] = '\0';
7873 p = newp;
7874 }
7875
7876 /* Change this type to have a type with the specified mode.
7877 First check for the special modes. */
84166705 7878 if (!strcmp (p, "byte"))
f8e93a2e 7879 mode = byte_mode;
7880 else if (!strcmp (p, "word"))
7881 mode = word_mode;
84166705 7882 else if (!strcmp (p, "pointer"))
f8e93a2e 7883 mode = ptr_mode;
0ef89dfd 7884 else if (!strcmp (p, "libgcc_cmp_return"))
7885 mode = targetm.libgcc_cmp_return_mode ();
7886 else if (!strcmp (p, "libgcc_shift_count"))
7887 mode = targetm.libgcc_shift_count_mode ();
1bd43494 7888 else if (!strcmp (p, "unwind_word"))
7889 mode = targetm.unwind_word_mode ();
f8e93a2e 7890 else
7891 for (j = 0; j < NUM_MACHINE_MODES; j++)
7892 if (!strcmp (p, GET_MODE_NAME (j)))
743a6f47 7893 {
3754d046 7894 mode = (machine_mode) j;
743a6f47 7895 break;
7896 }
f8e93a2e 7897
7898 if (mode == VOIDmode)
4917c376 7899 {
d1dd9ac0 7900 error ("unknown machine mode %qE", ident);
4917c376 7901 return NULL_TREE;
7902 }
7903
b2aef146 7904 valid_mode = false;
7905 switch (GET_MODE_CLASS (mode))
4917c376 7906 {
b2aef146 7907 case MODE_INT:
7908 case MODE_PARTIAL_INT:
7909 case MODE_FLOAT:
c4503c0a 7910 case MODE_DECIMAL_FLOAT:
9421ebb9 7911 case MODE_FRACT:
7912 case MODE_UFRACT:
7913 case MODE_ACCUM:
7914 case MODE_UACCUM:
b2aef146 7915 valid_mode = targetm.scalar_mode_supported_p (mode);
7916 break;
7917
7918 case MODE_COMPLEX_INT:
7919 case MODE_COMPLEX_FLOAT:
7920 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7921 break;
7922
7923 case MODE_VECTOR_INT:
7924 case MODE_VECTOR_FLOAT:
9421ebb9 7925 case MODE_VECTOR_FRACT:
7926 case MODE_VECTOR_UFRACT:
7927 case MODE_VECTOR_ACCUM:
7928 case MODE_VECTOR_UACCUM:
9b2d6d13 7929 warning (OPT_Wattributes, "specifying vector types with "
7930 "__attribute__ ((mode)) is deprecated");
7931 warning (OPT_Wattributes,
7932 "use __attribute__ ((vector_size)) instead");
b2aef146 7933 valid_mode = vector_mode_valid_p (mode);
7934 break;
4917c376 7935
b2aef146 7936 default:
7937 break;
7938 }
7939 if (!valid_mode)
7940 {
1e5fcbe2 7941 error ("unable to emulate %qs", p);
b2aef146 7942 return NULL_TREE;
7943 }
4917c376 7944
b2aef146 7945 if (POINTER_TYPE_P (type))
ead34f59 7946 {
6d5d708e 7947 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
3754d046 7948 tree (*fn)(tree, machine_mode, bool);
b2aef146 7949
6d5d708e 7950 if (!targetm.addr_space.valid_pointer_mode (mode, as))
ead34f59 7951 {
1e5fcbe2 7952 error ("invalid pointer mode %qs", p);
ead34f59 7953 return NULL_TREE;
7954 }
7955
a0c938f0 7956 if (TREE_CODE (type) == POINTER_TYPE)
b2aef146 7957 fn = build_pointer_type_for_mode;
805e22b2 7958 else
b2aef146 7959 fn = build_reference_type_for_mode;
7960 typefm = fn (TREE_TYPE (type), mode, false);
ead34f59 7961 }
b2aef146 7962 else
9421ebb9 7963 {
7964 /* For fixed-point modes, we need to test if the signness of type
7965 and the machine mode are consistent. */
7966 if (ALL_FIXED_POINT_MODE_P (mode)
7967 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7968 {
bf776685 7969 error ("signedness of type and machine mode %qs don%'t match", p);
9421ebb9 7970 return NULL_TREE;
7971 }
7972 /* For fixed-point modes, we need to pass saturating info. */
7973 typefm = lang_hooks.types.type_for_mode (mode,
7974 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7975 : TYPE_UNSIGNED (type));
7976 }
3a648ab9 7977
b2aef146 7978 if (typefm == NULL_TREE)
7979 {
743a6f47 7980 error ("no data type for mode %qs", p);
b2aef146 7981 return NULL_TREE;
7982 }
3a648ab9 7983 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7984 {
7985 /* For enumeral types, copy the precision from the integer
7986 type returned above. If not an INTEGER_TYPE, we can't use
7987 this mode for this type. */
7988 if (TREE_CODE (typefm) != INTEGER_TYPE)
7989 {
743a6f47 7990 error ("cannot use mode %qs for enumeral types", p);
3a648ab9 7991 return NULL_TREE;
7992 }
7993
10080eac 7994 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7995 {
7996 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7997 typefm = type;
7998 }
7999 else
8000 {
8001 /* We cannot build a type variant, as there's code that assumes
8002 that TYPE_MAIN_VARIANT has the same mode. This includes the
8003 debug generators. Instead, create a subrange type. This
8004 results in all of the enumeral values being emitted only once
8005 in the original, and the subtype gets them by reference. */
8006 if (TYPE_UNSIGNED (type))
8007 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
8008 else
8009 typefm = make_signed_type (TYPE_PRECISION (typefm));
8010 TREE_TYPE (typefm) = type;
8011 }
3a648ab9 8012 }
4bf450a1 8013 else if (VECTOR_MODE_P (mode)
8014 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
8015 : TREE_CODE (type) != TREE_CODE (typefm))
743a6f47 8016 {
8017 error ("mode %qs applied to inappropriate type", p);
8018 return NULL_TREE;
8019 }
8020
b2aef146 8021 *node = typefm;
f8e93a2e 8022 }
8023
8024 return NULL_TREE;
8025}
8026
8027/* Handle a "section" attribute; arguments as in
8028 struct attribute_spec.handler. */
8029
8030static tree
9a03a746 8031handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8032 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8033{
8034 tree decl = *node;
8035
9866562d 8036 if (!targetm_common.have_named_sections)
f8e93a2e 8037 {
9866562d 8038 error_at (DECL_SOURCE_LOCATION (*node),
8039 "section attributes are not supported for this target");
8040 goto fail;
8041 }
065efcb1 8042
9866562d 8043 user_defined_section_attribute = true;
f8e93a2e 8044
b443c459 8045 if (!VAR_OR_FUNCTION_DECL_P (decl))
9866562d 8046 {
8047 error ("section attribute not allowed for %q+D", *node);
8048 goto fail;
f8e93a2e 8049 }
9866562d 8050
8051 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
f8e93a2e 8052 {
9866562d 8053 error ("section attribute argument not a string constant");
8054 goto fail;
f8e93a2e 8055 }
8056
f48c7f4a 8057 if (VAR_P (decl)
9866562d 8058 && current_function_decl != NULL_TREE
8059 && !TREE_STATIC (decl))
8060 {
8061 error_at (DECL_SOURCE_LOCATION (decl),
8062 "section attribute cannot be specified for local variables");
8063 goto fail;
8064 }
8065
8066 /* The decl may have already been given a section attribute
8067 from a previous declaration. Ensure they match. */
8068 if (DECL_SECTION_NAME (decl) != NULL
8069 && strcmp (DECL_SECTION_NAME (decl),
8070 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
8071 {
8072 error ("section of %q+D conflicts with previous declaration", *node);
8073 goto fail;
8074 }
8075
f48c7f4a 8076 if (VAR_P (decl)
9866562d 8077 && !targetm.have_tls && targetm.emutls.tmpl_section
8078 && DECL_THREAD_LOCAL_P (decl))
8079 {
8080 error ("section of %q+D cannot be overridden", *node);
8081 goto fail;
8082 }
8083
8084 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
8085 return NULL_TREE;
8086
8087fail:
8088 *no_add_attrs = true;
f8e93a2e 8089 return NULL_TREE;
8090}
8091
83e25171 8092/* Check whether ALIGN is a valid user-specified alignment. If so,
8093 return its base-2 log; if not, output an error and return -1. If
8094 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
8095 no error. */
8096int
8097check_user_alignment (const_tree align, bool allow_zero)
8098{
8099 int i;
8100
3e5a8b00 8101 if (error_operand_p (align))
8102 return -1;
5abaa10a 8103 if (TREE_CODE (align) != INTEGER_CST
8104 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
83e25171 8105 {
8106 error ("requested alignment is not an integer constant");
8107 return -1;
8108 }
8109 else if (allow_zero && integer_zerop (align))
8110 return -1;
1a087624 8111 else if (tree_int_cst_sgn (align) == -1
8112 || (i = tree_log2 (align)) == -1)
83e25171 8113 {
1a087624 8114 error ("requested alignment is not a positive power of 2");
83e25171 8115 return -1;
8116 }
8117 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
8118 {
8119 error ("requested alignment is too large");
8120 return -1;
8121 }
8122 return i;
8123}
8124
ffcdbf9c 8125/*
8126 If in c++-11, check if the c++-11 alignment constraint with respect
8127 to fundamental alignment (in [dcl.align]) are satisfied. If not in
8128 c++-11 mode, does nothing.
8129
8130 [dcl.align]2/ says:
8131
8132 [* if the constant expression evaluates to a fundamental alignment,
8133 the alignment requirement of the declared entity shall be the
8134 specified fundamental alignment.
8135
8136 * if the constant expression evaluates to an extended alignment
8137 and the implementation supports that alignment in the context
8138 of the declaration, the alignment of the declared entity shall
8139 be that alignment
8140
8141 * if the constant expression evaluates to an extended alignment
8142 and the implementation does not support that alignment in the
8143 context of the declaration, the program is ill-formed]. */
8144
8145static bool
8146check_cxx_fundamental_alignment_constraints (tree node,
8147 unsigned align_log,
8148 int flags)
8149{
8150 bool alignment_too_large_p = false;
8151 unsigned requested_alignment = 1U << align_log;
8152 unsigned max_align = 0;
8153
8154 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
8155 || (node == NULL_TREE || node == error_mark_node))
8156 return true;
8157
8158 if (cxx_fundamental_alignment_p (requested_alignment))
8159 return true;
8160
8161 if (DECL_P (node))
8162 {
8163 if (TREE_STATIC (node))
8164 {
8165 /* For file scope variables and static members, the target
8166 supports alignments that are at most
8167 MAX_OFILE_ALIGNMENT. */
8168 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
8169 alignment_too_large_p = true;
8170 }
8171 else
8172 {
8173#ifdef BIGGEST_FIELD_ALIGNMENT
8174#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
8175#else
8176#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
8177#endif
8178 /* For non-static members, the target supports either
8179 alignments that at most either BIGGEST_FIELD_ALIGNMENT
8180 if it is defined or BIGGEST_ALIGNMENT. */
8181 max_align = MAX_TARGET_FIELD_ALIGNMENT;
8182 if (TREE_CODE (node) == FIELD_DECL
8183 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
8184 alignment_too_large_p = true;
8185#undef MAX_TARGET_FIELD_ALIGNMENT
8186 /* For stack variables, the target supports at most
8187 MAX_STACK_ALIGNMENT. */
8188 else if (decl_function_context (node) != NULL
8189 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
8190 alignment_too_large_p = true;
8191 }
8192 }
8193 else if (TYPE_P (node))
8194 {
8195 /* Let's be liberal for types. */
8196 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
8197 alignment_too_large_p = true;
8198 }
8199
8200 if (alignment_too_large_p)
8201 pedwarn (input_location, OPT_Wattributes,
8202 "requested alignment %d is larger than %d",
8203 requested_alignment, max_align);
8204
8205 return !alignment_too_large_p;
8206}
8207
f8e93a2e 8208/* Handle a "aligned" attribute; arguments as in
8209 struct attribute_spec.handler. */
8210
8211static tree
9a03a746 8212handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
09347743 8213 int flags, bool *no_add_attrs)
f8e93a2e 8214{
8215 tree decl = NULL_TREE;
8216 tree *type = NULL;
8217 int is_type = 0;
caf62483 8218 tree align_expr;
f8e93a2e 8219 int i;
8220
caf62483 8221 if (args)
8222 {
8223 align_expr = TREE_VALUE (args);
3e5a8b00 8224 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
8225 && TREE_CODE (align_expr) != FUNCTION_DECL)
caf62483 8226 align_expr = default_conversion (align_expr);
8227 }
8228 else
8229 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
8230
f8e93a2e 8231 if (DECL_P (*node))
8232 {
8233 decl = *node;
8234 type = &TREE_TYPE (decl);
8235 is_type = TREE_CODE (*node) == TYPE_DECL;
8236 }
8237 else if (TYPE_P (*node))
8238 type = node, is_type = 1;
8239
ffcdbf9c 8240 if ((i = check_user_alignment (align_expr, false)) == -1
8241 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
83e25171 8242 *no_add_attrs = true;
f8e93a2e 8243 else if (is_type)
8244 {
2ec3af9c 8245 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8246 /* OK, modify the type in place. */;
f8e93a2e 8247 /* If we have a TYPE_DECL, then copy the type, so that we
8248 don't accidentally modify a builtin type. See pushdecl. */
2ec3af9c 8249 else if (decl && TREE_TYPE (decl) != error_mark_node
8250 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
f8e93a2e 8251 {
8252 tree tt = TREE_TYPE (decl);
e086912e 8253 *type = build_variant_type_copy (*type);
f8e93a2e 8254 DECL_ORIGINAL_TYPE (decl) = tt;
8255 TYPE_NAME (*type) = decl;
8256 TREE_USED (*type) = TREE_USED (decl);
8257 TREE_TYPE (decl) = *type;
8258 }
2ec3af9c 8259 else
e086912e 8260 *type = build_variant_type_copy (*type);
f8e93a2e 8261
7cfdc2f0 8262 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
f8e93a2e 8263 TYPE_USER_ALIGN (*type) = 1;
8264 }
097b5c8b 8265 else if (! VAR_OR_FUNCTION_DECL_P (decl)
f8e93a2e 8266 && TREE_CODE (decl) != FIELD_DECL)
8267 {
3cf8b391 8268 error ("alignment may not be specified for %q+D", decl);
f8e93a2e 8269 *no_add_attrs = true;
8270 }
ffcdbf9c 8271 else if (DECL_USER_ALIGN (decl)
8272 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
8273 /* C++-11 [dcl.align/4]:
8274
8275 When multiple alignment-specifiers are specified for an
8276 entity, the alignment requirement shall be set to the
8277 strictest specified alignment.
8278
8279 This formally comes from the c++11 specification but we are
8280 doing it for the GNU attribute syntax as well. */
8281 *no_add_attrs = true;
097b5c8b 8282 else if (TREE_CODE (decl) == FUNCTION_DECL
7cfdc2f0 8283 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
097b5c8b 8284 {
8285 if (DECL_USER_ALIGN (decl))
8286 error ("alignment for %q+D was previously specified as %d "
8287 "and may not be decreased", decl,
8288 DECL_ALIGN (decl) / BITS_PER_UNIT);
8289 else
8290 error ("alignment for %q+D must be at least %d", decl,
8291 DECL_ALIGN (decl) / BITS_PER_UNIT);
8292 *no_add_attrs = true;
8293 }
f8e93a2e 8294 else
8295 {
7cfdc2f0 8296 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
f8e93a2e 8297 DECL_USER_ALIGN (decl) = 1;
8298 }
8299
8300 return NULL_TREE;
8301}
8302
8303/* Handle a "weak" attribute; arguments as in
8304 struct attribute_spec.handler. */
8305
8306static tree
f948b309 8307handle_weak_attribute (tree *node, tree name,
9a03a746 8308 tree ARG_UNUSED (args),
8309 int ARG_UNUSED (flags),
8310 bool * ARG_UNUSED (no_add_attrs))
f8e93a2e 8311{
f948b309 8312 if (TREE_CODE (*node) == FUNCTION_DECL
059a60f3 8313 && DECL_DECLARED_INLINE_P (*node))
8314 {
0725e25c 8315 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
059a60f3 8316 *no_add_attrs = true;
8317 }
85c0a25c 8318 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8319 {
8320 error ("indirect function %q+D cannot be declared weak", *node);
8321 *no_add_attrs = true;
8322 return NULL_TREE;
8323 }
b443c459 8324 else if (VAR_OR_FUNCTION_DECL_P (*node))
c11b875d 8325 {
8326 struct symtab_node *n = symtab_node::get (*node);
8327 if (n && n->refuse_visibility_changes)
8328 error ("%+D declared weak after being used", *node);
8329 declare_weak (*node);
8330 }
f948b309 8331 else
8332 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8333
8334 return NULL_TREE;
8335}
8336
4641882f 8337/* Handle a "noplt" attribute; arguments as in
8338 struct attribute_spec.handler. */
8339
8340static tree
8341handle_noplt_attribute (tree *node, tree name,
8342 tree ARG_UNUSED (args),
8343 int ARG_UNUSED (flags),
8344 bool * ARG_UNUSED (no_add_attrs))
8345{
8346 if (TREE_CODE (*node) != FUNCTION_DECL)
8347 {
8348 warning (OPT_Wattributes,
8349 "%qE attribute is only applicable on functions", name);
8350 *no_add_attrs = true;
8351 return NULL_TREE;
8352 }
8353 return NULL_TREE;
8354}
8355
85c0a25c 8356/* Handle an "alias" or "ifunc" attribute; arguments as in
8357 struct attribute_spec.handler, except that IS_ALIAS tells us
8358 whether this is an alias as opposed to ifunc attribute. */
f8e93a2e 8359
8360static tree
85c0a25c 8361handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
8362 bool *no_add_attrs)
f8e93a2e 8363{
8364 tree decl = *node;
8365
85c0a25c 8366 if (TREE_CODE (decl) != FUNCTION_DECL
f48c7f4a 8367 && (!is_alias || !VAR_P (decl)))
9e830260 8368 {
8369 warning (OPT_Wattributes, "%qE attribute ignored", name);
8370 *no_add_attrs = true;
8371 }
8372 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
48e1416a 8373 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 8374 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
8375 /* A static variable declaration is always a tentative definition,
8376 but the alias is a non-tentative definition which overrides. */
48e1416a 8377 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 8378 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
f8e93a2e 8379 {
85c0a25c 8380 error ("%q+D defined both normally and as %qE attribute", decl, name);
f8e93a2e 8381 *no_add_attrs = true;
85c0a25c 8382 return NULL_TREE;
f8e93a2e 8383 }
85c0a25c 8384 else if (!is_alias
8385 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
8386 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
8387 {
8388 error ("weak %q+D cannot be defined %qE", decl, name);
8389 *no_add_attrs = true;
8390 return NULL_TREE;
8391 }
8c42f0d9 8392
8393 /* Note that the very first time we process a nested declaration,
8394 decl_function_context will not be set. Indeed, *would* never
8395 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8396 we do below. After such frobbery, pushdecl would set the context.
8397 In any case, this is never what we want. */
8398 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
f8e93a2e 8399 {
8400 tree id;
8401
8402 id = TREE_VALUE (args);
8403 if (TREE_CODE (id) != STRING_CST)
8404 {
85c0a25c 8405 error ("attribute %qE argument not a string", name);
f8e93a2e 8406 *no_add_attrs = true;
8407 return NULL_TREE;
8408 }
8409 id = get_identifier (TREE_STRING_POINTER (id));
8410 /* This counts as a use of the object pointed to. */
8411 TREE_USED (id) = 1;
8412
8413 if (TREE_CODE (decl) == FUNCTION_DECL)
8414 DECL_INITIAL (decl) = error_mark_node;
8415 else
f2526cce 8416 TREE_STATIC (decl) = 1;
85c0a25c 8417
8418 if (!is_alias)
8419 /* ifuncs are also aliases, so set that attribute too. */
8420 DECL_ATTRIBUTES (decl)
8421 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
f8e93a2e 8422 }
8423 else
8424 {
9b2d6d13 8425 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8426 *no_add_attrs = true;
8427 }
8428
8e857c41 8429 if (decl_in_symtab_p (*node))
8430 {
8431 struct symtab_node *n = symtab_node::get (decl);
8432 if (n && n->refuse_visibility_changes)
8433 {
8434 if (is_alias)
8435 error ("%+D declared alias after being used", decl);
8436 else
8437 error ("%+D declared ifunc after being used", decl);
8438 }
8439 }
8440
8441
f8e93a2e 8442 return NULL_TREE;
8443}
8444
85c0a25c 8445/* Handle an "alias" or "ifunc" attribute; arguments as in
8446 struct attribute_spec.handler. */
8447
8448static tree
8449handle_ifunc_attribute (tree *node, tree name, tree args,
8450 int ARG_UNUSED (flags), bool *no_add_attrs)
8451{
8452 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8453}
8454
8455/* Handle an "alias" or "ifunc" attribute; arguments as in
8456 struct attribute_spec.handler. */
8457
8458static tree
8459handle_alias_attribute (tree *node, tree name, tree args,
8460 int ARG_UNUSED (flags), bool *no_add_attrs)
8461{
8462 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8463}
8464
f4a30bd7 8465/* Handle a "weakref" attribute; arguments as in struct
8466 attribute_spec.handler. */
8467
8468static tree
8469handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8470 int flags, bool *no_add_attrs)
8471{
8472 tree attr = NULL_TREE;
8473
83852912 8474 /* We must ignore the attribute when it is associated with
8475 local-scoped decls, since attribute alias is ignored and many
8476 such symbols do not even have a DECL_WEAK field. */
64a7bd81 8477 if (decl_function_context (*node)
8478 || current_function_decl
b443c459 8479 || !VAR_OR_FUNCTION_DECL_P (*node))
83852912 8480 {
8481 warning (OPT_Wattributes, "%qE attribute ignored", name);
8482 *no_add_attrs = true;
8483 return NULL_TREE;
8484 }
8485
85c0a25c 8486 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8487 {
8488 error ("indirect function %q+D cannot be declared weakref", *node);
8489 *no_add_attrs = true;
8490 return NULL_TREE;
8491 }
8492
f4a30bd7 8493 /* The idea here is that `weakref("name")' mutates into `weakref,
8494 alias("name")', and weakref without arguments, in turn,
8495 implicitly adds weak. */
8496
8497 if (args)
8498 {
8499 attr = tree_cons (get_identifier ("alias"), args, attr);
8500 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8501
8502 *no_add_attrs = true;
0a3ecdc1 8503
8504 decl_attributes (node, attr, flags);
f4a30bd7 8505 }
8506 else
8507 {
8508 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
712d2297 8509 error_at (DECL_SOURCE_LOCATION (*node),
8510 "weakref attribute must appear before alias attribute");
f4a30bd7 8511
0a3ecdc1 8512 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8513 and that isn't supported; and because it wants to add it to
8514 the list of weak decls, which isn't helpful. */
8515 DECL_WEAK (*node) = 1;
f4a30bd7 8516 }
8517
8e857c41 8518 if (decl_in_symtab_p (*node))
8519 {
8520 struct symtab_node *n = symtab_node::get (*node);
8521 if (n && n->refuse_visibility_changes)
8522 error ("%+D declared weakref after being used", *node);
8523 }
8524
f4a30bd7 8525 return NULL_TREE;
8526}
8527
f8e93a2e 8528/* Handle an "visibility" attribute; arguments as in
8529 struct attribute_spec.handler. */
8530
8531static tree
1cae46be 8532handle_visibility_attribute (tree *node, tree name, tree args,
9a03a746 8533 int ARG_UNUSED (flags),
4a2849cb 8534 bool *ARG_UNUSED (no_add_attrs))
f8e93a2e 8535{
8536 tree decl = *node;
9c40570a 8537 tree id = TREE_VALUE (args);
4a2849cb 8538 enum symbol_visibility vis;
f8e93a2e 8539
b212f378 8540 if (TYPE_P (*node))
8541 {
4a2849cb 8542 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8543 /* OK */;
8544 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
8545 {
8546 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8547 name);
8548 return NULL_TREE;
8549 }
8550 else if (TYPE_FIELDS (*node))
8551 {
8552 error ("%qE attribute ignored because %qT is already defined",
8553 name, *node);
8554 return NULL_TREE;
8555 }
b212f378 8556 }
84166705 8557 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
f8e93a2e 8558 {
9b2d6d13 8559 warning (OPT_Wattributes, "%qE attribute ignored", name);
9c40570a 8560 return NULL_TREE;
f8e93a2e 8561 }
f8e93a2e 8562
9c40570a 8563 if (TREE_CODE (id) != STRING_CST)
8564 {
07e3a3d2 8565 error ("visibility argument not a string");
9c40570a 8566 return NULL_TREE;
f8e93a2e 8567 }
b27ac6b5 8568
b212f378 8569 /* If this is a type, set the visibility on the type decl. */
8570 if (TYPE_P (decl))
8571 {
8572 decl = TYPE_NAME (decl);
84166705 8573 if (!decl)
a0c938f0 8574 return NULL_TREE;
e147aab3 8575 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8576 {
9b2d6d13 8577 warning (OPT_Wattributes, "%qE attribute ignored on types",
e147aab3 8578 name);
8579 return NULL_TREE;
8580 }
b212f378 8581 }
f8e93a2e 8582
9c40570a 8583 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
4a2849cb 8584 vis = VISIBILITY_DEFAULT;
9c40570a 8585 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
4a2849cb 8586 vis = VISIBILITY_INTERNAL;
9c40570a 8587 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
4a2849cb 8588 vis = VISIBILITY_HIDDEN;
9c40570a 8589 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
4a2849cb 8590 vis = VISIBILITY_PROTECTED;
9c40570a 8591 else
4a2849cb 8592 {
8593 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8594 vis = VISIBILITY_DEFAULT;
8595 }
8596
8597 if (DECL_VISIBILITY_SPECIFIED (decl)
098a01e7 8598 && vis != DECL_VISIBILITY (decl))
8599 {
8600 tree attributes = (TYPE_P (*node)
8601 ? TYPE_ATTRIBUTES (*node)
8602 : DECL_ATTRIBUTES (decl));
8603 if (lookup_attribute ("visibility", attributes))
8604 error ("%qD redeclared with different visibility", decl);
8605 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8606 && lookup_attribute ("dllimport", attributes))
8607 error ("%qD was declared %qs which implies default visibility",
8608 decl, "dllimport");
8609 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8610 && lookup_attribute ("dllexport", attributes))
8611 error ("%qD was declared %qs which implies default visibility",
8612 decl, "dllexport");
8613 }
4a2849cb 8614
8615 DECL_VISIBILITY (decl) = vis;
b212f378 8616 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8617
4a2849cb 8618 /* Go ahead and attach the attribute to the node as well. This is needed
8619 so we can determine whether we have VISIBILITY_DEFAULT because the
8620 visibility was not specified, or because it was explicitly overridden
8621 from the containing scope. */
9c40570a 8622
f8e93a2e 8623 return NULL_TREE;
8624}
8625
3aa0c315 8626/* Determine the ELF symbol visibility for DECL, which is either a
8627 variable or a function. It is an error to use this function if a
8628 definition of DECL is not available in this translation unit.
8629 Returns true if the final visibility has been determined by this
8630 function; false if the caller is free to make additional
8631 modifications. */
8632
8633bool
8634c_determine_visibility (tree decl)
8635{
b443c459 8636 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
3aa0c315 8637
8638 /* If the user explicitly specified the visibility with an
8639 attribute, honor that. DECL_VISIBILITY will have been set during
920f5a70 8640 the processing of the attribute. We check for an explicit
8641 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8642 to distinguish the use of an attribute from the use of a "#pragma
8643 GCC visibility push(...)"; in the latter case we still want other
8644 considerations to be able to overrule the #pragma. */
8645 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8646 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8647 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8648 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
3aa0c315 8649 return true;
8650
4a2849cb 8651 /* Set default visibility to whatever the user supplied with
8652 visibility_specified depending on #pragma GCC visibility. */
8653 if (!DECL_VISIBILITY_SPECIFIED (decl))
8654 {
2d9d8740 8655 if (visibility_options.inpragma
8656 || DECL_VISIBILITY (decl) != default_visibility)
8657 {
8658 DECL_VISIBILITY (decl) = default_visibility;
8659 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8660 /* If visibility changed and DECL already has DECL_RTL, ensure
8661 symbol flags are updated. */
f48c7f4a 8662 if (((VAR_P (decl) && TREE_STATIC (decl))
2d9d8740 8663 || TREE_CODE (decl) == FUNCTION_DECL)
8664 && DECL_RTL_SET_P (decl))
8665 make_decl_rtl (decl);
8666 }
4a2849cb 8667 }
3aa0c315 8668 return false;
8669}
8670
24dfead4 8671/* Handle an "tls_model" attribute; arguments as in
8672 struct attribute_spec.handler. */
8673
8674static tree
1cae46be 8675handle_tls_model_attribute (tree *node, tree name, tree args,
9a03a746 8676 int ARG_UNUSED (flags), bool *no_add_attrs)
24dfead4 8677{
1b53eb20 8678 tree id;
24dfead4 8679 tree decl = *node;
1b53eb20 8680 enum tls_model kind;
24dfead4 8681
1b53eb20 8682 *no_add_attrs = true;
8683
f48c7f4a 8684 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
24dfead4 8685 {
9b2d6d13 8686 warning (OPT_Wattributes, "%qE attribute ignored", name);
1b53eb20 8687 return NULL_TREE;
24dfead4 8688 }
24dfead4 8689
1b53eb20 8690 kind = DECL_TLS_MODEL (decl);
8691 id = TREE_VALUE (args);
8692 if (TREE_CODE (id) != STRING_CST)
8693 {
8694 error ("tls_model argument not a string");
8695 return NULL_TREE;
24dfead4 8696 }
8697
1b53eb20 8698 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8699 kind = TLS_MODEL_LOCAL_EXEC;
8700 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8701 kind = TLS_MODEL_INITIAL_EXEC;
8702 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8703 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8704 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8705 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8706 else
8707 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8708
5e68df57 8709 set_decl_tls_model (decl, kind);
24dfead4 8710 return NULL_TREE;
8711}
8712
f8e93a2e 8713/* Handle a "no_instrument_function" attribute; arguments as in
8714 struct attribute_spec.handler. */
8715
8716static tree
1cae46be 8717handle_no_instrument_function_attribute (tree *node, tree name,
9a03a746 8718 tree ARG_UNUSED (args),
8719 int ARG_UNUSED (flags),
09347743 8720 bool *no_add_attrs)
f8e93a2e 8721{
8722 tree decl = *node;
8723
8724 if (TREE_CODE (decl) != FUNCTION_DECL)
8725 {
712d2297 8726 error_at (DECL_SOURCE_LOCATION (decl),
8727 "%qE attribute applies only to functions", name);
f8e93a2e 8728 *no_add_attrs = true;
8729 }
f8e93a2e 8730 else
8731 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8732
8733 return NULL_TREE;
8734}
8735
8736/* Handle a "malloc" attribute; arguments as in
8737 struct attribute_spec.handler. */
8738
8739static tree
9a03a746 8740handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8741 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8742{
a5147fca 8743 if (TREE_CODE (*node) == FUNCTION_DECL
8744 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
f8e93a2e 8745 DECL_IS_MALLOC (*node) = 1;
f8e93a2e 8746 else
8747 {
9b2d6d13 8748 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8749 *no_add_attrs = true;
8750 }
8751
8752 return NULL_TREE;
8753}
8754
4a29c97c 8755/* Handle a "alloc_size" attribute; arguments as in
8756 struct attribute_spec.handler. */
8757
8758static tree
8759handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8760 int ARG_UNUSED (flags), bool *no_add_attrs)
8761{
2802826e 8762 unsigned arg_count = type_num_arguments (*node);
4a29c97c 8763 for (; args; args = TREE_CHAIN (args))
8764 {
8765 tree position = TREE_VALUE (args);
caf62483 8766 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8767 && TREE_CODE (position) != FUNCTION_DECL)
8768 position = default_conversion (position);
4a29c97c 8769
237e78b1 8770 if (!tree_fits_uhwi_p (position)
8771 || !arg_count
8772 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
4a29c97c 8773 {
48e1416a 8774 warning (OPT_Wattributes,
4a29c97c 8775 "alloc_size parameter outside range");
8776 *no_add_attrs = true;
8777 return NULL_TREE;
8778 }
8779 }
8780 return NULL_TREE;
8781}
8782
237e78b1 8783/* Handle a "alloc_align" attribute; arguments as in
8784 struct attribute_spec.handler. */
8785
8786static tree
8787handle_alloc_align_attribute (tree *node, tree, tree args, int,
8788 bool *no_add_attrs)
8789{
8790 unsigned arg_count = type_num_arguments (*node);
8791 tree position = TREE_VALUE (args);
8792 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8793 position = default_conversion (position);
8794
8795 if (!tree_fits_uhwi_p (position)
8796 || !arg_count
8797 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8798 {
8799 warning (OPT_Wattributes,
8800 "alloc_align parameter outside range");
8801 *no_add_attrs = true;
8802 return NULL_TREE;
8803 }
8804 return NULL_TREE;
8805}
8806
8807/* Handle a "assume_aligned" attribute; arguments as in
8808 struct attribute_spec.handler. */
8809
8810static tree
8811handle_assume_aligned_attribute (tree *, tree, tree args, int,
8812 bool *no_add_attrs)
8813{
8814 for (; args; args = TREE_CHAIN (args))
8815 {
8816 tree position = TREE_VALUE (args);
8817 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8818 && TREE_CODE (position) != FUNCTION_DECL)
8819 position = default_conversion (position);
8820
8821 if (TREE_CODE (position) != INTEGER_CST)
8822 {
8823 warning (OPT_Wattributes,
8824 "assume_aligned parameter not integer constant");
8825 *no_add_attrs = true;
8826 return NULL_TREE;
8827 }
8828 }
8829 return NULL_TREE;
8830}
8831
8ce86007 8832/* Handle a "fn spec" attribute; arguments as in
8833 struct attribute_spec.handler. */
8834
8835static tree
8836handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8837 tree args, int ARG_UNUSED (flags),
8838 bool *no_add_attrs ATTRIBUTE_UNUSED)
8839{
8840 gcc_assert (args
8841 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8842 && !TREE_CHAIN (args));
8843 return NULL_TREE;
8844}
8845
058a1b7a 8846/* Handle a "bnd_variable_size" attribute; arguments as in
8847 struct attribute_spec.handler. */
8848
8849static tree
8850handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8851 int ARG_UNUSED (flags), bool *no_add_attrs)
8852{
8853 if (TREE_CODE (*node) != FIELD_DECL)
8854 {
8855 warning (OPT_Wattributes, "%qE attribute ignored", name);
8856 *no_add_attrs = true;
8857 }
8858
8859 return NULL_TREE;
8860}
8861
8862/* Handle a "bnd_legacy" attribute; arguments as in
8863 struct attribute_spec.handler. */
8864
8865static tree
8866handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8867 int ARG_UNUSED (flags), bool *no_add_attrs)
8868{
8869 if (TREE_CODE (*node) != FUNCTION_DECL)
8870 {
8871 warning (OPT_Wattributes, "%qE attribute ignored", name);
8872 *no_add_attrs = true;
8873 }
8874
8875 return NULL_TREE;
8876}
8877
8878/* Handle a "bnd_instrument" attribute; arguments as in
8879 struct attribute_spec.handler. */
8880
8881static tree
8882handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8883 int ARG_UNUSED (flags), bool *no_add_attrs)
8884{
8885 if (TREE_CODE (*node) != FUNCTION_DECL)
8886 {
8887 warning (OPT_Wattributes, "%qE attribute ignored", name);
8888 *no_add_attrs = true;
8889 }
8890
8891 return NULL_TREE;
8892}
8893
a96c3cc1 8894/* Handle a "warn_unused" attribute; arguments as in
8895 struct attribute_spec.handler. */
8896
8897static tree
8898handle_warn_unused_attribute (tree *node, tree name,
8899 tree args ATTRIBUTE_UNUSED,
8900 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8901{
8902 if (TYPE_P (*node))
8903 /* Do nothing else, just set the attribute. We'll get at
8904 it later with lookup_attribute. */
8905 ;
8906 else
8907 {
8908 warning (OPT_Wattributes, "%qE attribute ignored", name);
8909 *no_add_attrs = true;
8910 }
8911
8912 return NULL_TREE;
8913}
8914
bc7bff74 8915/* Handle an "omp declare simd" attribute; arguments as in
8916 struct attribute_spec.handler. */
8917
8918static tree
8919handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8920{
8921 return NULL_TREE;
8922}
8923
8924/* Handle an "omp declare target" attribute; arguments as in
8925 struct attribute_spec.handler. */
8926
8927static tree
8928handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8929{
8930 return NULL_TREE;
8931}
8932
26d1c5ff 8933/* Handle a "returns_twice" attribute; arguments as in
8934 struct attribute_spec.handler. */
8935
8936static tree
8937handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8938 int ARG_UNUSED (flags), bool *no_add_attrs)
8939{
8940 if (TREE_CODE (*node) == FUNCTION_DECL)
8941 DECL_IS_RETURNS_TWICE (*node) = 1;
8942 else
8943 {
9b2d6d13 8944 warning (OPT_Wattributes, "%qE attribute ignored", name);
26d1c5ff 8945 *no_add_attrs = true;
8946 }
8947
8948 return NULL_TREE;
8949}
8950
f8e93a2e 8951/* Handle a "no_limit_stack" attribute; arguments as in
8952 struct attribute_spec.handler. */
8953
8954static tree
1cae46be 8955handle_no_limit_stack_attribute (tree *node, tree name,
9a03a746 8956 tree ARG_UNUSED (args),
8957 int ARG_UNUSED (flags),
09347743 8958 bool *no_add_attrs)
f8e93a2e 8959{
8960 tree decl = *node;
8961
8962 if (TREE_CODE (decl) != FUNCTION_DECL)
8963 {
712d2297 8964 error_at (DECL_SOURCE_LOCATION (decl),
8965 "%qE attribute applies only to functions", name);
f8e93a2e 8966 *no_add_attrs = true;
8967 }
8968 else if (DECL_INITIAL (decl))
8969 {
712d2297 8970 error_at (DECL_SOURCE_LOCATION (decl),
8971 "can%'t set %qE attribute after definition", name);
f8e93a2e 8972 *no_add_attrs = true;
8973 }
8974 else
8975 DECL_NO_LIMIT_STACK (decl) = 1;
8976
8977 return NULL_TREE;
8978}
8979
8980/* Handle a "pure" attribute; arguments as in
8981 struct attribute_spec.handler. */
8982
8983static tree
9a03a746 8984handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8985 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8986{
8987 if (TREE_CODE (*node) == FUNCTION_DECL)
9c2a0c05 8988 DECL_PURE_P (*node) = 1;
f8e93a2e 8989 /* ??? TODO: Support types. */
8990 else
8991 {
9b2d6d13 8992 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8993 *no_add_attrs = true;
8994 }
8995
8996 return NULL_TREE;
8997}
8998
4c0315d0 8999/* Digest an attribute list destined for a transactional memory statement.
9000 ALLOWED is the set of attributes that are allowed for this statement;
9001 return the attribute we parsed. Multiple attributes are never allowed. */
9002
9003int
9004parse_tm_stmt_attr (tree attrs, int allowed)
9005{
9006 tree a_seen = NULL;
9007 int m_seen = 0;
9008
9009 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
9010 {
9011 tree a = TREE_PURPOSE (attrs);
9012 int m = 0;
9013
9014 if (is_attribute_p ("outer", a))
9015 m = TM_STMT_ATTR_OUTER;
9016
9017 if ((m & allowed) == 0)
9018 {
9019 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
9020 continue;
9021 }
9022
9023 if (m_seen == 0)
9024 {
9025 a_seen = a;
9026 m_seen = m;
9027 }
9028 else if (m_seen == m)
9029 warning (OPT_Wattributes, "%qE attribute duplicated", a);
9030 else
9031 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
9032 }
9033
9034 return m_seen;
9035}
9036
9037/* Transform a TM attribute name into a maskable integer and back.
9038 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
9039 to how the lack of an attribute is treated. */
9040
9041int
9042tm_attr_to_mask (tree attr)
9043{
9044 if (attr == NULL)
9045 return 0;
9046 if (is_attribute_p ("transaction_safe", attr))
9047 return TM_ATTR_SAFE;
9048 if (is_attribute_p ("transaction_callable", attr))
9049 return TM_ATTR_CALLABLE;
9050 if (is_attribute_p ("transaction_pure", attr))
9051 return TM_ATTR_PURE;
9052 if (is_attribute_p ("transaction_unsafe", attr))
9053 return TM_ATTR_IRREVOCABLE;
9054 if (is_attribute_p ("transaction_may_cancel_outer", attr))
9055 return TM_ATTR_MAY_CANCEL_OUTER;
9056 return 0;
9057}
9058
9059tree
9060tm_mask_to_attr (int mask)
9061{
9062 const char *str;
9063 switch (mask)
9064 {
9065 case TM_ATTR_SAFE:
9066 str = "transaction_safe";
9067 break;
9068 case TM_ATTR_CALLABLE:
9069 str = "transaction_callable";
9070 break;
9071 case TM_ATTR_PURE:
9072 str = "transaction_pure";
9073 break;
9074 case TM_ATTR_IRREVOCABLE:
9075 str = "transaction_unsafe";
9076 break;
9077 case TM_ATTR_MAY_CANCEL_OUTER:
9078 str = "transaction_may_cancel_outer";
9079 break;
9080 default:
9081 gcc_unreachable ();
9082 }
9083 return get_identifier (str);
9084}
9085
9086/* Return the first TM attribute seen in LIST. */
9087
9088tree
9089find_tm_attribute (tree list)
9090{
9091 for (; list ; list = TREE_CHAIN (list))
9092 {
9093 tree name = TREE_PURPOSE (list);
9094 if (tm_attr_to_mask (name) != 0)
9095 return name;
9096 }
9097 return NULL_TREE;
9098}
9099
9100/* Handle the TM attributes; arguments as in struct attribute_spec.handler.
9101 Here we accept only function types, and verify that none of the other
9102 function TM attributes are also applied. */
9103/* ??? We need to accept class types for C++, but not C. This greatly
9104 complicates this function, since we can no longer rely on the extra
9105 processing given by function_type_required. */
9106
9107static tree
9108handle_tm_attribute (tree *node, tree name, tree args,
9109 int flags, bool *no_add_attrs)
9110{
9111 /* Only one path adds the attribute; others don't. */
9112 *no_add_attrs = true;
9113
9114 switch (TREE_CODE (*node))
9115 {
9116 case RECORD_TYPE:
9117 case UNION_TYPE:
9118 /* Only tm_callable and tm_safe apply to classes. */
9119 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
9120 goto ignored;
9121 /* FALLTHRU */
9122
9123 case FUNCTION_TYPE:
9124 case METHOD_TYPE:
9125 {
9126 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
9127 if (old_name == name)
9128 ;
9129 else if (old_name != NULL_TREE)
9130 error ("type was previously declared %qE", old_name);
9131 else
9132 *no_add_attrs = false;
9133 }
9134 break;
9135
9136 case POINTER_TYPE:
9137 {
9138 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
9139 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
9140 {
9141 tree fn_tmp = TREE_TYPE (*node);
9142 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
9143 *node = build_pointer_type (fn_tmp);
9144 break;
9145 }
9146 }
9147 /* FALLTHRU */
9148
9149 default:
9150 /* If a function is next, pass it on to be tried next. */
9151 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
9152 return tree_cons (name, args, NULL);
9153
9154 ignored:
9155 warning (OPT_Wattributes, "%qE attribute ignored", name);
9156 break;
9157 }
9158
9159 return NULL_TREE;
9160}
9161
9162/* Handle the TM_WRAP attribute; arguments as in
9163 struct attribute_spec.handler. */
9164
9165static tree
9166handle_tm_wrap_attribute (tree *node, tree name, tree args,
9167 int ARG_UNUSED (flags), bool *no_add_attrs)
9168{
9169 tree decl = *node;
9170
9171 /* We don't need the attribute even on success, since we
9172 record the entry in an external table. */
9173 *no_add_attrs = true;
9174
9175 if (TREE_CODE (decl) != FUNCTION_DECL)
9176 warning (OPT_Wattributes, "%qE attribute ignored", name);
9177 else
9178 {
9179 tree wrap_decl = TREE_VALUE (args);
3e5a8b00 9180 if (error_operand_p (wrap_decl))
9181 ;
9182 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
b443c459 9183 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
4c0315d0 9184 error ("%qE argument not an identifier", name);
9185 else
9186 {
9187 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
9188 wrap_decl = lookup_name (wrap_decl);
9189 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
9190 {
9191 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
9192 TREE_TYPE (wrap_decl)))
9193 record_tm_replacement (wrap_decl, decl);
9194 else
9195 error ("%qD is not compatible with %qD", wrap_decl, decl);
9196 }
9197 else
cdf34fca 9198 error ("%qE argument is not a function", name);
4c0315d0 9199 }
9200 }
9201
9202 return NULL_TREE;
9203}
9204
9205/* Ignore the given attribute. Used when this attribute may be usefully
9206 overridden by the target, but is not used generically. */
9207
9208static tree
9209ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
9210 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9211 bool *no_add_attrs)
9212{
9213 *no_add_attrs = true;
9214 return NULL_TREE;
9215}
9216
fc09b200 9217/* Handle a "no vops" attribute; arguments as in
9218 struct attribute_spec.handler. */
9219
9220static tree
9221handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
9222 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9223 bool *ARG_UNUSED (no_add_attrs))
9224{
9225 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
9226 DECL_IS_NOVOPS (*node) = 1;
9227 return NULL_TREE;
9228}
9229
f8e93a2e 9230/* Handle a "deprecated" attribute; arguments as in
9231 struct attribute_spec.handler. */
1cae46be 9232
f8e93a2e 9233static tree
1cae46be 9234handle_deprecated_attribute (tree *node, tree name,
45c4e798 9235 tree args, int flags,
09347743 9236 bool *no_add_attrs)
f8e93a2e 9237{
9238 tree type = NULL_TREE;
9239 int warn = 0;
782858b8 9240 tree what = NULL_TREE;
1cae46be 9241
45c4e798 9242 if (!args)
9243 *no_add_attrs = true;
9244 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
9245 {
9246 error ("deprecated message is not a string");
9247 *no_add_attrs = true;
9248 }
9249
f8e93a2e 9250 if (DECL_P (*node))
9251 {
9252 tree decl = *node;
9253 type = TREE_TYPE (decl);
1cae46be 9254
f8e93a2e 9255 if (TREE_CODE (decl) == TYPE_DECL
9256 || TREE_CODE (decl) == PARM_DECL
b443c459 9257 || VAR_OR_FUNCTION_DECL_P (decl)
40c8d1dd 9258 || TREE_CODE (decl) == FIELD_DECL
5a4c69dd 9259 || TREE_CODE (decl) == CONST_DECL
40c8d1dd 9260 || objc_method_decl (TREE_CODE (decl)))
f8e93a2e 9261 TREE_DEPRECATED (decl) = 1;
9262 else
9263 warn = 1;
9264 }
9265 else if (TYPE_P (*node))
9266 {
9267 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 9268 *node = build_variant_type_copy (*node);
f8e93a2e 9269 TREE_DEPRECATED (*node) = 1;
9270 type = *node;
9271 }
9272 else
9273 warn = 1;
1cae46be 9274
f8e93a2e 9275 if (warn)
9276 {
9277 *no_add_attrs = true;
9278 if (type && TYPE_NAME (type))
9279 {
9280 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
782858b8 9281 what = TYPE_NAME (*node);
f8e93a2e 9282 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9283 && DECL_NAME (TYPE_NAME (type)))
782858b8 9284 what = DECL_NAME (TYPE_NAME (type));
f8e93a2e 9285 }
9286 if (what)
9b2d6d13 9287 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
f8e93a2e 9288 else
9b2d6d13 9289 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 9290 }
9291
9292 return NULL_TREE;
9293}
9294
f8e93a2e 9295/* Handle a "vector_size" attribute; arguments as in
9296 struct attribute_spec.handler. */
9297
9298static tree
1cae46be 9299handle_vector_size_attribute (tree *node, tree name, tree args,
9a03a746 9300 int ARG_UNUSED (flags),
09347743 9301 bool *no_add_attrs)
f8e93a2e 9302{
9303 unsigned HOST_WIDE_INT vecsize, nunits;
3754d046 9304 machine_mode orig_mode;
4917c376 9305 tree type = *node, new_type, size;
f8e93a2e 9306
9307 *no_add_attrs = true;
9308
4917c376 9309 size = TREE_VALUE (args);
3e5a8b00 9310 if (size && TREE_CODE (size) != IDENTIFIER_NODE
9311 && TREE_CODE (size) != FUNCTION_DECL)
caf62483 9312 size = default_conversion (size);
4917c376 9313
e913b5cd 9314 if (!tree_fits_uhwi_p (size))
f8e93a2e 9315 {
9b2d6d13 9316 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 9317 return NULL_TREE;
9318 }
9319
9320 /* Get the vector size (in bytes). */
e913b5cd 9321 vecsize = tree_to_uhwi (size);
f8e93a2e 9322
9323 /* We need to provide for vector pointers, vector arrays, and
9324 functions returning vectors. For example:
9325
9326 __attribute__((vector_size(16))) short *foo;
9327
9328 In this case, the mode is SI, but the type being modified is
9329 HI, so we need to look further. */
9330
9331 while (POINTER_TYPE_P (type)
9332 || TREE_CODE (type) == FUNCTION_TYPE
5bfb0742 9333 || TREE_CODE (type) == METHOD_TYPE
2cb0e5d9 9334 || TREE_CODE (type) == ARRAY_TYPE
9335 || TREE_CODE (type) == OFFSET_TYPE)
f8e93a2e 9336 type = TREE_TYPE (type);
9337
9338 /* Get the mode of the type being modified. */
9339 orig_mode = TYPE_MODE (type);
9340
2cb0e5d9 9341 if ((!INTEGRAL_TYPE_P (type)
9342 && !SCALAR_FLOAT_TYPE_P (type)
9343 && !FIXED_POINT_TYPE_P (type))
cee7491d 9344 || (!SCALAR_FLOAT_MODE_P (orig_mode)
9421ebb9 9345 && GET_MODE_CLASS (orig_mode) != MODE_INT
9346 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
e913b5cd 9347 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7ec31215 9348 || TREE_CODE (type) == BOOLEAN_TYPE)
f8e93a2e 9349 {
782858b8 9350 error ("invalid vector type for attribute %qE", name);
f8e93a2e 9351 return NULL_TREE;
9352 }
9353
e913b5cd 9354 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
39cc3e6d 9355 {
9356 error ("vector size not an integral multiple of component size");
9357 return NULL;
9358 }
9359
9360 if (vecsize == 0)
9361 {
9362 error ("zero vector size");
9363 return NULL;
9364 }
9365
f8e93a2e 9366 /* Calculate how many units fit in the vector. */
e913b5cd 9367 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
83e2a11b 9368 if (nunits & (nunits - 1))
f8e93a2e 9369 {
83e2a11b 9370 error ("number of components of the vector not a power of two");
f8e93a2e 9371 return NULL_TREE;
9372 }
9373
83e2a11b 9374 new_type = build_vector_type (type, nunits);
f8e93a2e 9375
9376 /* Build back pointers if needed. */
d991e6e8 9377 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
f8e93a2e 9378
9379 return NULL_TREE;
9380}
9381
dbf6c367 9382/* Handle the "nonnull" attribute. */
9383static tree
9a03a746 9384handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9385 tree args, int ARG_UNUSED (flags),
09347743 9386 bool *no_add_attrs)
dbf6c367 9387{
9388 tree type = *node;
9389 unsigned HOST_WIDE_INT attr_arg_num;
9390
9391 /* If no arguments are specified, all pointer arguments should be
d716ce75 9392 non-null. Verify a full prototype is given so that the arguments
dbf6c367 9393 will have the correct types when we actually check them later. */
84166705 9394 if (!args)
dbf6c367 9395 {
a36cf284 9396 if (!prototype_p (type))
dbf6c367 9397 {
9398 error ("nonnull attribute without arguments on a non-prototype");
4ee9c684 9399 *no_add_attrs = true;
dbf6c367 9400 }
9401 return NULL_TREE;
9402 }
9403
9404 /* Argument list specified. Verify that each argument number references
9405 a pointer argument. */
caf62483 9406 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
dbf6c367 9407 {
4ee9c684 9408 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
dbf6c367 9409
caf62483 9410 tree arg = TREE_VALUE (args);
9411 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9412 && TREE_CODE (arg) != FUNCTION_DECL)
9413 arg = default_conversion (arg);
9414
9415 if (!get_nonnull_operand (arg, &arg_num))
dbf6c367 9416 {
07e3a3d2 9417 error ("nonnull argument has invalid operand number (argument %lu)",
dbf6c367 9418 (unsigned long) attr_arg_num);
9419 *no_add_attrs = true;
9420 return NULL_TREE;
9421 }
9422
d0af78c5 9423 if (prototype_p (type))
dbf6c367 9424 {
d0af78c5 9425 function_args_iterator iter;
9426 tree argument;
9427
9428 function_args_iter_init (&iter, type);
9429 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
dbf6c367 9430 {
d0af78c5 9431 argument = function_args_iter_cond (&iter);
9432 if (argument == NULL_TREE || ck_num == arg_num)
dbf6c367 9433 break;
dbf6c367 9434 }
9435
84166705 9436 if (!argument
d0af78c5 9437 || TREE_CODE (argument) == VOID_TYPE)
dbf6c367 9438 {
07e3a3d2 9439 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
dbf6c367 9440 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9441 *no_add_attrs = true;
9442 return NULL_TREE;
9443 }
9444
d0af78c5 9445 if (TREE_CODE (argument) != POINTER_TYPE)
dbf6c367 9446 {
07e3a3d2 9447 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
dbf6c367 9448 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9449 *no_add_attrs = true;
9450 return NULL_TREE;
9451 }
9452 }
9453 }
9454
9455 return NULL_TREE;
9456}
9457
9458/* Check the argument list of a function call for null in argument slots
d01f58f9 9459 that are marked as requiring a non-null pointer argument. The NARGS
9460 arguments are passed in the array ARGARRAY.
9461*/
dbf6c367 9462
9463static void
d01f58f9 9464check_function_nonnull (tree attrs, int nargs, tree *argarray)
dbf6c367 9465{
9ca77b08 9466 tree a;
d01f58f9 9467 int i;
dbf6c367 9468
9ca77b08 9469 attrs = lookup_attribute ("nonnull", attrs);
9470 if (attrs == NULL_TREE)
9471 return;
9472
9473 a = attrs;
9474 /* See if any of the nonnull attributes has no arguments. If so,
9475 then every pointer argument is checked (in which case the check
9476 for pointer type is done in check_nonnull_arg). */
9477 if (TREE_VALUE (a) != NULL_TREE)
9478 do
9479 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9480 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9481
9482 if (a != NULL_TREE)
9483 for (i = 0; i < nargs; i++)
9484 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9485 i + 1);
9486 else
dbf6c367 9487 {
9ca77b08 9488 /* Walk the argument list. If we encounter an argument number we
9489 should check for non-null, do it. */
9490 for (i = 0; i < nargs; i++)
dbf6c367 9491 {
9ca77b08 9492 for (a = attrs; ; a = TREE_CHAIN (a))
4ee9c684 9493 {
9ca77b08 9494 a = lookup_attribute ("nonnull", a);
9495 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9496 break;
4ee9c684 9497 }
9ca77b08 9498
9499 if (a != NULL_TREE)
9500 check_function_arguments_recurse (check_nonnull_arg, NULL,
9501 argarray[i], i + 1);
dbf6c367 9502 }
9503 }
9504}
9505
50ca527f 9506/* Check that the Nth argument of a function call (counting backwards
d01f58f9 9507 from the end) is a (pointer)0. The NARGS arguments are passed in the
9508 array ARGARRAY. */
bf6c8de0 9509
9510static void
774e9d58 9511check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
bf6c8de0 9512{
774e9d58 9513 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
bf6c8de0 9514
9515 if (attr)
9516 {
d01f58f9 9517 int len = 0;
9518 int pos = 0;
9519 tree sentinel;
774e9d58 9520 function_args_iterator iter;
9521 tree t;
a0c938f0 9522
d01f58f9 9523 /* Skip over the named arguments. */
774e9d58 9524 FOREACH_FUNCTION_ARGS (fntype, t, iter)
a0c938f0 9525 {
774e9d58 9526 if (len == nargs)
9527 break;
d01f58f9 9528 len++;
9529 }
50ca527f 9530
d01f58f9 9531 if (TREE_VALUE (attr))
9532 {
9533 tree p = TREE_VALUE (TREE_VALUE (attr));
f9ae6f95 9534 pos = TREE_INT_CST_LOW (p);
d01f58f9 9535 }
50ca527f 9536
d01f58f9 9537 /* The sentinel must be one of the varargs, i.e.
9538 in position >= the number of fixed arguments. */
9539 if ((nargs - 1 - pos) < len)
9540 {
77a357e3 9541 warning (OPT_Wformat_,
d01f58f9 9542 "not enough variable arguments to fit a sentinel");
9543 return;
bf6c8de0 9544 }
d01f58f9 9545
9546 /* Validate the sentinel. */
9547 sentinel = argarray[nargs - 1 - pos];
9548 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9549 || !integer_zerop (sentinel))
9550 /* Although __null (in C++) is only an integer we allow it
9551 nevertheless, as we are guaranteed that it's exactly
9552 as wide as a pointer, and we don't want to force
9553 users to cast the NULL they have written there.
9554 We warn with -Wstrict-null-sentinel, though. */
9555 && (warn_strict_null_sentinel || null_node != sentinel))
77a357e3 9556 warning (OPT_Wformat_, "missing sentinel in function call");
bf6c8de0 9557 }
9558}
9559
dbf6c367 9560/* Helper for check_function_nonnull; given a list of operands which
9561 must be non-null in ARGS, determine if operand PARAM_NUM should be
9562 checked. */
9563
9564static bool
1cae46be 9565nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
dbf6c367 9566{
4ee9c684 9567 unsigned HOST_WIDE_INT arg_num = 0;
dbf6c367 9568
9569 for (; args; args = TREE_CHAIN (args))
9570 {
231bd014 9571 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9572
9573 gcc_assert (found);
dbf6c367 9574
9575 if (arg_num == param_num)
9576 return true;
9577 }
9578 return false;
9579}
9580
9581/* Check that the function argument PARAM (which is operand number
9582 PARAM_NUM) is non-null. This is called by check_function_nonnull
9583 via check_function_arguments_recurse. */
9584
9585static void
9a03a746 9586check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
1cae46be 9587 unsigned HOST_WIDE_INT param_num)
dbf6c367 9588{
9589 /* Just skip checking the argument if it's not a pointer. This can
9590 happen if the "nonnull" attribute was given without an operand
9591 list (which means to check every pointer argument). */
9592
9593 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9594 return;
9595
9596 if (integer_zerop (param))
155b601b 9597 warning (OPT_Wnonnull, "null argument where non-null required "
9598 "(argument %lu)", (unsigned long) param_num);
dbf6c367 9599}
9600
9601/* Helper for nonnull attribute handling; fetch the operand number
9602 from the attribute argument list. */
9603
9604static bool
1cae46be 9605get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
dbf6c367 9606{
e913b5cd 9607 /* Verify the arg number is a small constant. */
e1d65c9f 9608 if (tree_fits_uhwi_p (arg_num_expr))
e913b5cd 9609 {
f9ae6f95 9610 *valp = TREE_INT_CST_LOW (arg_num_expr);
e913b5cd 9611 return true;
9612 }
9613 else
dbf6c367 9614 return false;
dbf6c367 9615}
fa987697 9616
9617/* Handle a "nothrow" attribute; arguments as in
9618 struct attribute_spec.handler. */
9619
9620static tree
9a03a746 9621handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9622 int ARG_UNUSED (flags), bool *no_add_attrs)
fa987697 9623{
9624 if (TREE_CODE (*node) == FUNCTION_DECL)
9625 TREE_NOTHROW (*node) = 1;
9626 /* ??? TODO: Support types. */
9627 else
9628 {
9b2d6d13 9629 warning (OPT_Wattributes, "%qE attribute ignored", name);
fa987697 9630 *no_add_attrs = true;
9631 }
9632
9633 return NULL_TREE;
9634}
7acb29a3 9635
9636/* Handle a "cleanup" attribute; arguments as in
9637 struct attribute_spec.handler. */
9638
9639static tree
1cae46be 9640handle_cleanup_attribute (tree *node, tree name, tree args,
9a03a746 9641 int ARG_UNUSED (flags), bool *no_add_attrs)
7acb29a3 9642{
9643 tree decl = *node;
9644 tree cleanup_id, cleanup_decl;
9645
9646 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9647 for global destructors in C++. This requires infrastructure that
9648 we don't have generically at the moment. It's also not a feature
9649 we'd be missing too much, since we do have attribute constructor. */
f48c7f4a 9650 if (!VAR_P (decl) || TREE_STATIC (decl))
7acb29a3 9651 {
9b2d6d13 9652 warning (OPT_Wattributes, "%qE attribute ignored", name);
7acb29a3 9653 *no_add_attrs = true;
9654 return NULL_TREE;
9655 }
9656
9657 /* Verify that the argument is a function in scope. */
9658 /* ??? We could support pointers to functions here as well, if
9659 that was considered desirable. */
9660 cleanup_id = TREE_VALUE (args);
9661 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9662 {
07e3a3d2 9663 error ("cleanup argument not an identifier");
7acb29a3 9664 *no_add_attrs = true;
9665 return NULL_TREE;
9666 }
d1c41717 9667 cleanup_decl = lookup_name (cleanup_id);
7acb29a3 9668 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9669 {
07e3a3d2 9670 error ("cleanup argument not a function");
7acb29a3 9671 *no_add_attrs = true;
9672 return NULL_TREE;
9673 }
9674
1cae46be 9675 /* That the function has proper type is checked with the
7acb29a3 9676 eventual call to build_function_call. */
9677
9678 return NULL_TREE;
9679}
8a8cdb8d 9680
9681/* Handle a "warn_unused_result" attribute. No special handling. */
9682
9683static tree
9684handle_warn_unused_result_attribute (tree *node, tree name,
9a03a746 9685 tree ARG_UNUSED (args),
9686 int ARG_UNUSED (flags), bool *no_add_attrs)
8a8cdb8d 9687{
9688 /* Ignore the attribute for functions not returning any value. */
9689 if (VOID_TYPE_P (TREE_TYPE (*node)))
9690 {
9b2d6d13 9691 warning (OPT_Wattributes, "%qE attribute ignored", name);
8a8cdb8d 9692 *no_add_attrs = true;
9693 }
9694
9695 return NULL_TREE;
9696}
bf6c8de0 9697
9698/* Handle a "sentinel" attribute. */
9699
9700static tree
50ca527f 9701handle_sentinel_attribute (tree *node, tree name, tree args,
bf6c8de0 9702 int ARG_UNUSED (flags), bool *no_add_attrs)
9703{
a36cf284 9704 if (!prototype_p (*node))
bf6c8de0 9705 {
9b2d6d13 9706 warning (OPT_Wattributes,
9707 "%qE attribute requires prototypes with named arguments", name);
bf6c8de0 9708 *no_add_attrs = true;
bf6c8de0 9709 }
50ca527f 9710 else
9711 {
c33080b9 9712 if (!stdarg_p (*node))
a0c938f0 9713 {
9b2d6d13 9714 warning (OPT_Wattributes,
9715 "%qE attribute only applies to variadic functions", name);
50ca527f 9716 *no_add_attrs = true;
9717 }
9718 }
a0c938f0 9719
50ca527f 9720 if (args)
bf6c8de0 9721 {
50ca527f 9722 tree position = TREE_VALUE (args);
3e5a8b00 9723 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9724 && TREE_CODE (position) != FUNCTION_DECL)
9725 position = default_conversion (position);
50ca527f 9726
3e5a8b00 9727 if (TREE_CODE (position) != INTEGER_CST
9728 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
a0c938f0 9729 {
48e1416a 9730 warning (OPT_Wattributes,
01b54db5 9731 "requested position is not an integer constant");
50ca527f 9732 *no_add_attrs = true;
9733 }
9734 else
a0c938f0 9735 {
50ca527f 9736 if (tree_int_cst_lt (position, integer_zero_node))
9737 {
01b54db5 9738 warning (OPT_Wattributes,
9739 "requested position is less than zero");
50ca527f 9740 *no_add_attrs = true;
9741 }
9742 }
bf6c8de0 9743 }
a0c938f0 9744
bf6c8de0 9745 return NULL_TREE;
9746}
b5c26b42 9747
9748/* Handle a "type_generic" attribute. */
9749
9750static tree
9751handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9752 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9753 bool * ARG_UNUSED (no_add_attrs))
9754{
19fbe3a4 9755 /* Ensure we have a function type. */
9756 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
48e1416a 9757
19fbe3a4 9758 /* Ensure we have a variadic function. */
c33080b9 9759 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
b5c26b42 9760
9761 return NULL_TREE;
9762}
46f8e3b0 9763
24470055 9764/* Handle a "target" attribute. */
46f8e3b0 9765
9766static tree
24470055 9767handle_target_attribute (tree *node, tree name, tree args, int flags,
46f8e3b0 9768 bool *no_add_attrs)
9769{
9770 /* Ensure we have a function type. */
9771 if (TREE_CODE (*node) != FUNCTION_DECL)
9772 {
9773 warning (OPT_Wattributes, "%qE attribute ignored", name);
9774 *no_add_attrs = true;
9775 }
46f8e3b0 9776 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
ae0c3984 9777 flags))
46f8e3b0 9778 *no_add_attrs = true;
9779
9780 return NULL_TREE;
9781}
9782
9783/* Arguments being collected for optimization. */
9784typedef const char *const_char_p; /* For DEF_VEC_P. */
f1f41a6c 9785static GTY(()) vec<const_char_p, va_gc> *optimize_args;
46f8e3b0 9786
9787
9788/* Inner function to convert a TREE_LIST to argv string to parse the optimize
9789 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9790 false for #pragma GCC optimize. */
9791
9792bool
9793parse_optimize_options (tree args, bool attr_p)
9794{
9795 bool ret = true;
9796 unsigned opt_argc;
9797 unsigned i;
2becf397 9798 int saved_flag_strict_aliasing;
46f8e3b0 9799 const char **opt_argv;
615ef0bb 9800 struct cl_decoded_option *decoded_options;
9801 unsigned int decoded_options_count;
46f8e3b0 9802 tree ap;
9803
9804 /* Build up argv vector. Just in case the string is stored away, use garbage
9805 collected strings. */
f1f41a6c 9806 vec_safe_truncate (optimize_args, 0);
9807 vec_safe_push (optimize_args, (const char *) NULL);
46f8e3b0 9808
9809 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9810 {
9811 tree value = TREE_VALUE (ap);
9812
9813 if (TREE_CODE (value) == INTEGER_CST)
9814 {
9815 char buffer[20];
f9ae6f95 9816 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
f1f41a6c 9817 vec_safe_push (optimize_args, ggc_strdup (buffer));
46f8e3b0 9818 }
9819
9820 else if (TREE_CODE (value) == STRING_CST)
9821 {
9822 /* Split string into multiple substrings. */
9823 size_t len = TREE_STRING_LENGTH (value);
9824 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9825 char *end = p + len;
9826 char *comma;
9827 char *next_p = p;
9828
9829 while (next_p != NULL)
9830 {
9831 size_t len2;
9832 char *q, *r;
9833
9834 p = next_p;
9835 comma = strchr (p, ',');
9836 if (comma)
9837 {
9838 len2 = comma - p;
9839 *comma = '\0';
9840 next_p = comma+1;
9841 }
9842 else
9843 {
9844 len2 = end - p;
9845 next_p = NULL;
9846 }
9847
ba72912a 9848 r = q = (char *) ggc_alloc_atomic (len2 + 3);
46f8e3b0 9849
9850 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9851 options. */
9852 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9853 {
9854 ret = false;
9855 if (attr_p)
9856 warning (OPT_Wattributes,
ebd7c4c1 9857 "bad option %s to optimize attribute", p);
46f8e3b0 9858 else
9859 warning (OPT_Wpragmas,
e44b0a1f 9860 "bad option %s to pragma attribute", p);
46f8e3b0 9861 continue;
9862 }
9863
9864 if (*p != '-')
9865 {
9866 *r++ = '-';
9867
9868 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9869 itself is -Os, and any other switch begins with a -f. */
9870 if ((*p >= '0' && *p <= '9')
9871 || (p[0] == 's' && p[1] == '\0'))
9872 *r++ = 'O';
9873 else if (*p != 'O')
9874 *r++ = 'f';
9875 }
9876
9877 memcpy (r, p, len2);
9878 r[len2] = '\0';
f1f41a6c 9879 vec_safe_push (optimize_args, (const char *) q);
46f8e3b0 9880 }
9881
9882 }
9883 }
9884
f1f41a6c 9885 opt_argc = optimize_args->length ();
46f8e3b0 9886 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9887
9888 for (i = 1; i < opt_argc; i++)
f1f41a6c 9889 opt_argv[i] = (*optimize_args)[i];
46f8e3b0 9890
2becf397 9891 saved_flag_strict_aliasing = flag_strict_aliasing;
9892
46f8e3b0 9893 /* Now parse the options. */
f3f006ad 9894 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9895 &decoded_options,
9896 &decoded_options_count);
9897 decode_options (&global_options, &global_options_set,
3c6c0e40 9898 decoded_options, decoded_options_count,
9899 input_location, global_dc);
46f8e3b0 9900
4bec06b3 9901 targetm.override_options_after_change();
9902
2becf397 9903 /* Don't allow changing -fstrict-aliasing. */
9904 flag_strict_aliasing = saved_flag_strict_aliasing;
9905
f1f41a6c 9906 optimize_args->truncate (0);
46f8e3b0 9907 return ret;
9908}
9909
9910/* For handling "optimize" attribute. arguments as in
9911 struct attribute_spec.handler. */
9912
9913static tree
9914handle_optimize_attribute (tree *node, tree name, tree args,
9915 int ARG_UNUSED (flags), bool *no_add_attrs)
9916{
9917 /* Ensure we have a function type. */
9918 if (TREE_CODE (*node) != FUNCTION_DECL)
9919 {
9920 warning (OPT_Wattributes, "%qE attribute ignored", name);
9921 *no_add_attrs = true;
9922 }
9923 else
9924 {
9925 struct cl_optimization cur_opts;
9926 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9927
9928 /* Save current options. */
2c5d2e39 9929 cl_optimization_save (&cur_opts, &global_options);
46f8e3b0 9930
9931 /* If we previously had some optimization options, use them as the
9932 default. */
9933 if (old_opts)
2c5d2e39 9934 cl_optimization_restore (&global_options,
9935 TREE_OPTIMIZATION (old_opts));
46f8e3b0 9936
9937 /* Parse options, and update the vector. */
9938 parse_optimize_options (args, true);
9939 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
55310327 9940 = build_optimization_node (&global_options);
46f8e3b0 9941
9942 /* Restore current options. */
2c5d2e39 9943 cl_optimization_restore (&global_options, &cur_opts);
46f8e3b0 9944 }
9945
9946 return NULL_TREE;
9947}
48b14f50 9948
9949/* Handle a "no_split_stack" attribute. */
9950
9951static tree
9952handle_no_split_stack_attribute (tree *node, tree name,
9953 tree ARG_UNUSED (args),
9954 int ARG_UNUSED (flags),
9955 bool *no_add_attrs)
9956{
9957 tree decl = *node;
9958
9959 if (TREE_CODE (decl) != FUNCTION_DECL)
9960 {
9961 error_at (DECL_SOURCE_LOCATION (decl),
9962 "%qE attribute applies only to functions", name);
9963 *no_add_attrs = true;
9964 }
9965 else if (DECL_INITIAL (decl))
9966 {
9967 error_at (DECL_SOURCE_LOCATION (decl),
9968 "can%'t set %qE attribute after definition", name);
9969 *no_add_attrs = true;
9970 }
9971
9972 return NULL_TREE;
9973}
d7dcba40 9974
9975/* Handle a "returns_nonnull" attribute; arguments as in
9976 struct attribute_spec.handler. */
9977
9978static tree
9979handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9980 bool *no_add_attrs)
9981{
9982 // Even without a prototype we still have a return type we can check.
9983 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9984 {
9985 error ("returns_nonnull attribute on a function not returning a pointer");
9986 *no_add_attrs = true;
9987 }
9988 return NULL_TREE;
9989}
9990
74691f46 9991/* Handle a "designated_init" attribute; arguments as in
9992 struct attribute_spec.handler. */
9993
9994static tree
9995handle_designated_init_attribute (tree *node, tree name, tree, int,
9996 bool *no_add_attrs)
9997{
9998 if (TREE_CODE (*node) != RECORD_TYPE)
9999 {
10000 error ("%qE attribute is only valid on %<struct%> type", name);
10001 *no_add_attrs = true;
10002 }
10003 return NULL_TREE;
10004}
10005
dbf6c367 10006\f
774e9d58 10007/* Check for valid arguments being passed to a function with FNTYPE.
10008 There are NARGS arguments in the array ARGARRAY. */
dbf6c367 10009void
774e9d58 10010check_function_arguments (const_tree fntype, int nargs, tree *argarray)
dbf6c367 10011{
10012 /* Check for null being passed in a pointer argument that must be
10013 non-null. We also need to do this if format checking is enabled. */
10014
10015 if (warn_nonnull)
774e9d58 10016 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
dbf6c367 10017
10018 /* Check for errors in format strings. */
10019
068bea1e 10020 if (warn_format || warn_suggest_attribute_format)
774e9d58 10021 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
95c90e04 10022
10023 if (warn_format)
774e9d58 10024 check_function_sentinel (fntype, nargs, argarray);
dbf6c367 10025}
10026
10027/* Generic argument checking recursion routine. PARAM is the argument to
10028 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
10029 once the argument is resolved. CTX is context for the callback. */
10030void
1cae46be 10031check_function_arguments_recurse (void (*callback)
10032 (void *, tree, unsigned HOST_WIDE_INT),
10033 void *ctx, tree param,
10034 unsigned HOST_WIDE_INT param_num)
dbf6c367 10035{
72dd6141 10036 if (CONVERT_EXPR_P (param)
c44afe23 10037 && (TYPE_PRECISION (TREE_TYPE (param))
10038 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
dbf6c367 10039 {
10040 /* Strip coercion. */
10041 check_function_arguments_recurse (callback, ctx,
4ee9c684 10042 TREE_OPERAND (param, 0), param_num);
dbf6c367 10043 return;
10044 }
10045
10046 if (TREE_CODE (param) == CALL_EXPR)
10047 {
c2f47e15 10048 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
dbf6c367 10049 tree attrs;
10050 bool found_format_arg = false;
10051
10052 /* See if this is a call to a known internationalization function
10053 that modifies a format arg. Such a function may have multiple
10054 format_arg attributes (for example, ngettext). */
10055
10056 for (attrs = TYPE_ATTRIBUTES (type);
10057 attrs;
10058 attrs = TREE_CHAIN (attrs))
10059 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
10060 {
c2f47e15 10061 tree inner_arg;
dbf6c367 10062 tree format_num_expr;
10063 int format_num;
10064 int i;
c2f47e15 10065 call_expr_arg_iterator iter;
dbf6c367 10066
10067 /* Extract the argument number, which was previously checked
10068 to be valid. */
10069 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
ddb1be65 10070
e913b5cd 10071 format_num = tree_to_uhwi (format_num_expr);
dbf6c367 10072
c2f47e15 10073 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
10074 inner_arg != 0;
10075 inner_arg = next_call_expr_arg (&iter), i++)
dbf6c367 10076 if (i == format_num)
10077 {
10078 check_function_arguments_recurse (callback, ctx,
c2f47e15 10079 inner_arg, param_num);
dbf6c367 10080 found_format_arg = true;
10081 break;
10082 }
10083 }
10084
10085 /* If we found a format_arg attribute and did a recursive check,
10086 we are done with checking this argument. Otherwise, we continue
10087 and this will be considered a non-literal. */
10088 if (found_format_arg)
10089 return;
10090 }
10091
10092 if (TREE_CODE (param) == COND_EXPR)
10093 {
10094 /* Check both halves of the conditional expression. */
10095 check_function_arguments_recurse (callback, ctx,
4ee9c684 10096 TREE_OPERAND (param, 1), param_num);
dbf6c367 10097 check_function_arguments_recurse (callback, ctx,
4ee9c684 10098 TREE_OPERAND (param, 2), param_num);
dbf6c367 10099 return;
10100 }
10101
10102 (*callback) (ctx, param, param_num);
10103}
1f3233d1 10104
60cce472 10105/* Checks for a builtin function FNDECL that the number of arguments
10106 NARGS against the required number REQUIRED and issues an error if
10107 there is a mismatch. Returns true if the number of arguments is
10108 correct, otherwise false. */
d43cee80 10109
10110static bool
60cce472 10111builtin_function_validate_nargs (tree fndecl, int nargs, int required)
d43cee80 10112{
10113 if (nargs < required)
10114 {
60cce472 10115 error_at (input_location,
10116 "not enough arguments to function %qE", fndecl);
d43cee80 10117 return false;
10118 }
10119 else if (nargs > required)
10120 {
60cce472 10121 error_at (input_location,
10122 "too many arguments to function %qE", fndecl);
d43cee80 10123 return false;
10124 }
10125 return true;
10126}
10127
10128/* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
10129 Returns false if there was an error, otherwise true. */
10130
10131bool
10132check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
10133{
10134 if (!DECL_BUILT_IN (fndecl)
10135 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
10136 return true;
10137
10138 switch (DECL_FUNCTION_CODE (fndecl))
10139 {
10140 case BUILT_IN_CONSTANT_P:
60cce472 10141 return builtin_function_validate_nargs (fndecl, nargs, 1);
d43cee80 10142
10143 case BUILT_IN_ISFINITE:
10144 case BUILT_IN_ISINF:
c319d56a 10145 case BUILT_IN_ISINF_SIGN:
d43cee80 10146 case BUILT_IN_ISNAN:
10147 case BUILT_IN_ISNORMAL:
60cce472 10148 if (builtin_function_validate_nargs (fndecl, nargs, 1))
d43cee80 10149 {
10150 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
10151 {
10152 error ("non-floating-point argument in call to "
10153 "function %qE", fndecl);
10154 return false;
10155 }
10156 return true;
10157 }
10158 return false;
10159
10160 case BUILT_IN_ISGREATER:
10161 case BUILT_IN_ISGREATEREQUAL:
10162 case BUILT_IN_ISLESS:
10163 case BUILT_IN_ISLESSEQUAL:
10164 case BUILT_IN_ISLESSGREATER:
10165 case BUILT_IN_ISUNORDERED:
60cce472 10166 if (builtin_function_validate_nargs (fndecl, nargs, 2))
d43cee80 10167 {
10168 enum tree_code code0, code1;
10169 code0 = TREE_CODE (TREE_TYPE (args[0]));
10170 code1 = TREE_CODE (TREE_TYPE (args[1]));
10171 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
10172 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10173 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
10174 {
10175 error ("non-floating-point arguments in call to "
10176 "function %qE", fndecl);
10177 return false;
10178 }
10179 return true;
10180 }
10181 return false;
10182
19fbe3a4 10183 case BUILT_IN_FPCLASSIFY:
60cce472 10184 if (builtin_function_validate_nargs (fndecl, nargs, 6))
19fbe3a4 10185 {
10186 unsigned i;
48e1416a 10187
19fbe3a4 10188 for (i=0; i<5; i++)
10189 if (TREE_CODE (args[i]) != INTEGER_CST)
10190 {
10191 error ("non-const integer argument %u in call to function %qE",
10192 i+1, fndecl);
10193 return false;
10194 }
10195
10196 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
10197 {
10198 error ("non-floating-point argument in call to function %qE",
10199 fndecl);
10200 return false;
10201 }
10202 return true;
10203 }
10204 return false;
10205
fca0886c 10206 case BUILT_IN_ASSUME_ALIGNED:
10207 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
10208 {
10209 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
10210 {
10211 error ("non-integer argument 3 in call to function %qE", fndecl);
10212 return false;
10213 }
10214 return true;
10215 }
10216 return false;
10217
0c93c8a9 10218 case BUILT_IN_ADD_OVERFLOW:
10219 case BUILT_IN_SUB_OVERFLOW:
10220 case BUILT_IN_MUL_OVERFLOW:
10221 if (builtin_function_validate_nargs (fndecl, nargs, 3))
10222 {
10223 unsigned i;
10224 for (i = 0; i < 2; i++)
10225 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
10226 {
10227 error ("argument %u in call to function %qE does not have "
10228 "integral type", i + 1, fndecl);
10229 return false;
10230 }
10231 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
10232 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
10233 {
10234 error ("argument 3 in call to function %qE does not have "
10235 "pointer to integer type", fndecl);
10236 return false;
10237 }
10238 return true;
10239 }
10240 return false;
10241
d43cee80 10242 default:
10243 return true;
10244 }
10245}
10246
860251be 10247/* Function to help qsort sort FIELD_DECLs by name order. */
10248
10249int
10250field_decl_cmp (const void *x_p, const void *y_p)
10251{
4fd61bc6 10252 const tree *const x = (const tree *const) x_p;
10253 const tree *const y = (const tree *const) y_p;
10254
860251be 10255 if (DECL_NAME (*x) == DECL_NAME (*y))
10256 /* A nontype is "greater" than a type. */
10257 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10258 if (DECL_NAME (*x) == NULL_TREE)
10259 return -1;
10260 if (DECL_NAME (*y) == NULL_TREE)
10261 return 1;
10262 if (DECL_NAME (*x) < DECL_NAME (*y))
10263 return -1;
10264 return 1;
10265}
10266
10267static struct {
10268 gt_pointer_operator new_value;
10269 void *cookie;
10270} resort_data;
10271
10272/* This routine compares two fields like field_decl_cmp but using the
10273pointer operator in resort_data. */
10274
10275static int
10276resort_field_decl_cmp (const void *x_p, const void *y_p)
10277{
4fd61bc6 10278 const tree *const x = (const tree *const) x_p;
10279 const tree *const y = (const tree *const) y_p;
860251be 10280
10281 if (DECL_NAME (*x) == DECL_NAME (*y))
10282 /* A nontype is "greater" than a type. */
10283 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10284 if (DECL_NAME (*x) == NULL_TREE)
10285 return -1;
10286 if (DECL_NAME (*y) == NULL_TREE)
10287 return 1;
10288 {
10289 tree d1 = DECL_NAME (*x);
10290 tree d2 = DECL_NAME (*y);
10291 resort_data.new_value (&d1, resort_data.cookie);
10292 resort_data.new_value (&d2, resort_data.cookie);
10293 if (d1 < d2)
10294 return -1;
10295 }
10296 return 1;
10297}
10298
10299/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10300
10301void
10302resort_sorted_fields (void *obj,
9a03a746 10303 void * ARG_UNUSED (orig_obj),
4ee9c684 10304 gt_pointer_operator new_value,
10305 void *cookie)
860251be 10306{
9a03a746 10307 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
860251be 10308 resort_data.new_value = new_value;
10309 resort_data.cookie = cookie;
10310 qsort (&sf->elts[0], sf->len, sizeof (tree),
4ee9c684 10311 resort_field_decl_cmp);
860251be 10312}
10313
209c9752 10314/* Subroutine of c_parse_error.
10315 Return the result of concatenating LHS and RHS. RHS is really
10316 a string literal, its first character is indicated by RHS_START and
cfee01e3 10317 RHS_SIZE is its length (including the terminating NUL character).
209c9752 10318
10319 The caller is responsible for deleting the returned pointer. */
10320
10321static char *
10322catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10323{
10324 const int lhs_size = strlen (lhs);
10325 char *result = XNEWVEC (char, lhs_size + rhs_size);
10326 strncpy (result, lhs, lhs_size);
10327 strncpy (result + lhs_size, rhs_start, rhs_size);
10328 return result;
10329}
10330
380c6697 10331/* Issue the error given by GMSGID, indicating that it occurred before
92b128ed 10332 TOKEN, which had the associated VALUE. */
10333
10334void
48e1416a 10335c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
ba99525e 10336 tree value, unsigned char token_flags)
92b128ed 10337{
209c9752 10338#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10339
10340 char *message = NULL;
92b128ed 10341
ba99525e 10342 if (token_type == CPP_EOF)
380c6697 10343 message = catenate_messages (gmsgid, " at end of input");
48e1416a 10344 else if (token_type == CPP_CHAR
10345 || token_type == CPP_WCHAR
ba99525e 10346 || token_type == CPP_CHAR16
30b1ba42 10347 || token_type == CPP_CHAR32
10348 || token_type == CPP_UTF8CHAR)
92b128ed 10349 {
f9ae6f95 10350 unsigned int val = TREE_INT_CST_LOW (value);
924bbf02 10351 const char *prefix;
10352
ba99525e 10353 switch (token_type)
924bbf02 10354 {
10355 default:
10356 prefix = "";
10357 break;
10358 case CPP_WCHAR:
10359 prefix = "L";
10360 break;
10361 case CPP_CHAR16:
10362 prefix = "u";
10363 break;
10364 case CPP_CHAR32:
10365 prefix = "U";
10366 break;
30b1ba42 10367 case CPP_UTF8CHAR:
10368 prefix = "u8";
10369 break;
924bbf02 10370 }
10371
92b128ed 10372 if (val <= UCHAR_MAX && ISGRAPH (val))
a0c938f0 10373 message = catenate_messages (gmsgid, " before %s'%c'");
92b128ed 10374 else
a0c938f0 10375 message = catenate_messages (gmsgid, " before %s'\\x%x'");
209c9752 10376
924bbf02 10377 error (message, prefix, val);
209c9752 10378 free (message);
10379 message = NULL;
92b128ed 10380 }
1898176c 10381 else if (token_type == CPP_CHAR_USERDEF
10382 || token_type == CPP_WCHAR_USERDEF
10383 || token_type == CPP_CHAR16_USERDEF
30b1ba42 10384 || token_type == CPP_CHAR32_USERDEF
10385 || token_type == CPP_UTF8CHAR_USERDEF)
1898176c 10386 message = catenate_messages (gmsgid,
10387 " before user-defined character literal");
10388 else if (token_type == CPP_STRING_USERDEF
10389 || token_type == CPP_WSTRING_USERDEF
10390 || token_type == CPP_STRING16_USERDEF
10391 || token_type == CPP_STRING32_USERDEF
10392 || token_type == CPP_UTF8STRING_USERDEF)
10393 message = catenate_messages (gmsgid, " before user-defined string literal");
48e1416a 10394 else if (token_type == CPP_STRING
10395 || token_type == CPP_WSTRING
ba99525e 10396 || token_type == CPP_STRING16
538ba11a 10397 || token_type == CPP_STRING32
10398 || token_type == CPP_UTF8STRING)
380c6697 10399 message = catenate_messages (gmsgid, " before string constant");
ba99525e 10400 else if (token_type == CPP_NUMBER)
380c6697 10401 message = catenate_messages (gmsgid, " before numeric constant");
ba99525e 10402 else if (token_type == CPP_NAME)
209c9752 10403 {
380c6697 10404 message = catenate_messages (gmsgid, " before %qE");
782858b8 10405 error (message, value);
209c9752 10406 free (message);
10407 message = NULL;
10408 }
ba99525e 10409 else if (token_type == CPP_PRAGMA)
b75b98aa 10410 message = catenate_messages (gmsgid, " before %<#pragma%>");
ba99525e 10411 else if (token_type == CPP_PRAGMA_EOL)
b75b98aa 10412 message = catenate_messages (gmsgid, " before end of line");
07b8f133 10413 else if (token_type == CPP_DECLTYPE)
10414 message = catenate_messages (gmsgid, " before %<decltype%>");
ba99525e 10415 else if (token_type < N_TTYPES)
209c9752 10416 {
380c6697 10417 message = catenate_messages (gmsgid, " before %qs token");
ba99525e 10418 error (message, cpp_type2name (token_type, token_flags));
209c9752 10419 free (message);
10420 message = NULL;
10421 }
92b128ed 10422 else
380c6697 10423 error (gmsgid);
209c9752 10424
10425 if (message)
10426 {
10427 error (message);
10428 free (message);
10429 }
a0c938f0 10430#undef catenate_messages
92b128ed 10431}
10432
3a79f5da 10433/* Return the gcc option code associated with the reason for a cpp
10434 message, or 0 if none. */
10435
10436static int
10437c_option_controlling_cpp_error (int reason)
10438{
7ff8db31 10439 const struct cpp_reason_option_codes_t *entry;
3a79f5da 10440
7ff8db31 10441 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
3a79f5da 10442 {
10443 if (entry->reason == reason)
10444 return entry->option_code;
10445 }
10446 return 0;
10447}
10448
7f5f3953 10449/* Callback from cpp_error for PFILE to print diagnostics from the
3a79f5da 10450 preprocessor. The diagnostic is of type LEVEL, with REASON set
10451 to the reason code if LEVEL is represents a warning, at location
7f5f3953 10452 LOCATION unless this is after lexing and the compiler's location
10453 should be used instead, with column number possibly overridden by
10454 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
10455 the arguments. Returns true if a diagnostic was emitted, false
10456 otherwise. */
10457
10458bool
3a79f5da 10459c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
7f5f3953 10460 location_t location, unsigned int column_override,
10461 const char *msg, va_list *ap)
10462{
10463 diagnostic_info diagnostic;
10464 diagnostic_t dlevel;
5ae82d58 10465 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
7f5f3953 10466 bool ret;
10467
10468 switch (level)
10469 {
10470 case CPP_DL_WARNING_SYSHDR:
10471 if (flag_no_output)
10472 return false;
5ae82d58 10473 global_dc->dc_warn_system_headers = 1;
7f5f3953 10474 /* Fall through. */
10475 case CPP_DL_WARNING:
10476 if (flag_no_output)
10477 return false;
10478 dlevel = DK_WARNING;
10479 break;
10480 case CPP_DL_PEDWARN:
10481 if (flag_no_output && !flag_pedantic_errors)
10482 return false;
10483 dlevel = DK_PEDWARN;
10484 break;
10485 case CPP_DL_ERROR:
10486 dlevel = DK_ERROR;
10487 break;
10488 case CPP_DL_ICE:
10489 dlevel = DK_ICE;
10490 break;
10491 case CPP_DL_NOTE:
10492 dlevel = DK_NOTE;
10493 break;
ff903809 10494 case CPP_DL_FATAL:
10495 dlevel = DK_FATAL;
10496 break;
7f5f3953 10497 default:
10498 gcc_unreachable ();
10499 }
10500 if (done_lexing)
10501 location = input_location;
10502 diagnostic_set_info_translated (&diagnostic, msg, ap,
10503 location, dlevel);
10504 if (column_override)
10505 diagnostic_override_column (&diagnostic, column_override);
3a79f5da 10506 diagnostic_override_option_index (&diagnostic,
10507 c_option_controlling_cpp_error (reason));
7f5f3953 10508 ret = report_diagnostic (&diagnostic);
10509 if (level == CPP_DL_WARNING_SYSHDR)
5ae82d58 10510 global_dc->dc_warn_system_headers = save_warn_system_headers;
7f5f3953 10511 return ret;
10512}
10513
624d37a6 10514/* Convert a character from the host to the target execution character
10515 set. cpplib handles this, mostly. */
10516
10517HOST_WIDE_INT
10518c_common_to_target_charset (HOST_WIDE_INT c)
10519{
10520 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10521 zero-extended under -fno-signed-char. cpplib insists that characters
10522 and character constants are always unsigned. Hence we must convert
10523 back and forth. */
10524 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10525
10526 uc = cpp_host_to_exec_charset (parse_in, uc);
10527
10528 if (flag_signed_char)
10529 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10530 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10531 else
10532 return uc;
10533}
10534
7549df0d 10535/* Fold an offsetof-like expression. EXPR is a nested sequence of component
10536 references with an INDIRECT_REF of a constant at the bottom; much like the
10537 traditional rendering of offsetof as a macro. Return the folded result. */
af28855b 10538
7549df0d 10539tree
10540fold_offsetof_1 (tree expr)
af28855b 10541{
af28855b 10542 tree base, off, t;
10543
10544 switch (TREE_CODE (expr))
10545 {
10546 case ERROR_MARK:
10547 return expr;
10548
6b11d2e3 10549 case VAR_DECL:
10550 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10551 return error_mark_node;
10552
d897f7c2 10553 case CALL_EXPR:
cf1a89a3 10554 case TARGET_EXPR:
d897f7c2 10555 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10556 return error_mark_node;
10557
d897f7c2 10558 case NOP_EXPR:
10559 case INDIRECT_REF:
7549df0d 10560 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
64ed018c 10561 {
10562 error ("cannot apply %<offsetof%> to a non constant address");
10563 return error_mark_node;
10564 }
7549df0d 10565 return TREE_OPERAND (expr, 0);
d897f7c2 10566
af28855b 10567 case COMPONENT_REF:
7549df0d 10568 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
af28855b 10569 if (base == error_mark_node)
10570 return base;
10571
10572 t = TREE_OPERAND (expr, 1);
10573 if (DECL_C_BIT_FIELD (t))
10574 {
10575 error ("attempt to take address of bit-field structure "
782858b8 10576 "member %qD", t);
af28855b 10577 return error_mark_node;
10578 }
389dd41b 10579 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
e913b5cd 10580 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
389dd41b 10581 / BITS_PER_UNIT));
af28855b 10582 break;
10583
10584 case ARRAY_REF:
7549df0d 10585 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
af28855b 10586 if (base == error_mark_node)
10587 return base;
10588
10589 t = TREE_OPERAND (expr, 1);
64ed018c 10590
10591 /* Check if the offset goes beyond the upper bound of the array. */
7549df0d 10592 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
e0559d69 10593 {
10594 tree upbound = array_ref_up_bound (expr);
10595 if (upbound != NULL_TREE
10596 && TREE_CODE (upbound) == INTEGER_CST
10597 && !tree_int_cst_equal (upbound,
10598 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10599 {
10600 upbound = size_binop (PLUS_EXPR, upbound,
10601 build_int_cst (TREE_TYPE (upbound), 1));
10602 if (tree_int_cst_lt (upbound, t))
10603 {
10604 tree v;
10605
10606 for (v = TREE_OPERAND (expr, 0);
10607 TREE_CODE (v) == COMPONENT_REF;
10608 v = TREE_OPERAND (v, 0))
10609 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10610 == RECORD_TYPE)
10611 {
1767a056 10612 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10613 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
e0559d69 10614 if (TREE_CODE (fld_chain) == FIELD_DECL)
10615 break;
10616
10617 if (fld_chain)
10618 break;
10619 }
10620 /* Don't warn if the array might be considered a poor
10621 man's flexible array member with a very permissive
10622 definition thereof. */
10623 if (TREE_CODE (v) == ARRAY_REF
10624 || TREE_CODE (v) == COMPONENT_REF)
10625 warning (OPT_Warray_bounds,
10626 "index %E denotes an offset "
10627 "greater than size of %qT",
10628 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10629 }
10630 }
10631 }
7549df0d 10632
10633 t = convert (sizetype, t);
10634 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
af28855b 10635 break;
10636
ede90cc2 10637 case COMPOUND_EXPR:
10638 /* Handle static members of volatile structs. */
10639 t = TREE_OPERAND (expr, 1);
f48c7f4a 10640 gcc_assert (VAR_P (t));
7549df0d 10641 return fold_offsetof_1 (t);
ede90cc2 10642
af28855b 10643 default:
231bd014 10644 gcc_unreachable ();
af28855b 10645 }
10646
7549df0d 10647 return fold_build_pointer_plus (base, off);
af28855b 10648}
10649
7549df0d 10650/* Likewise, but convert it to the return type of offsetof. */
10651
af28855b 10652tree
7549df0d 10653fold_offsetof (tree expr)
af28855b 10654{
7549df0d 10655 return convert (size_type_node, fold_offsetof_1 (expr));
af28855b 10656}
10657
b9bdfa0b 10658/* Warn for A ?: C expressions (with B omitted) where A is a boolean
10659 expression, because B will always be true. */
10660
10661void
10662warn_for_omitted_condop (location_t location, tree cond)
10663{
10664 if (truth_value_p (TREE_CODE (cond)))
10665 warning_at (location, OPT_Wparentheses,
10666 "the omitted middle operand in ?: will always be %<true%>, "
10667 "suggest explicit middle operand");
10668}
10669
a1f90215 10670/* Give an error for storing into ARG, which is 'const'. USE indicates
10671 how ARG was being used. */
10672
10673void
f2697631 10674readonly_error (location_t loc, tree arg, enum lvalue_use use)
a1f90215 10675{
10676 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10677 || use == lv_asm);
10678 /* Using this macro rather than (for example) arrays of messages
10679 ensures that all the format strings are checked at compile
10680 time. */
10681#define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10682 : (use == lv_increment ? (I) \
10683 : (use == lv_decrement ? (D) : (AS))))
10684 if (TREE_CODE (arg) == COMPONENT_REF)
10685 {
10686 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
f2697631 10687 error_at (loc, READONLY_MSG (G_("assignment of member "
10688 "%qD in read-only object"),
10689 G_("increment of member "
10690 "%qD in read-only object"),
10691 G_("decrement of member "
10692 "%qD in read-only object"),
10693 G_("member %qD in read-only object "
10694 "used as %<asm%> output")),
10695 TREE_OPERAND (arg, 1));
a1f90215 10696 else
f2697631 10697 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10698 G_("increment of read-only member %qD"),
10699 G_("decrement of read-only member %qD"),
10700 G_("read-only member %qD used as %<asm%> output")),
10701 TREE_OPERAND (arg, 1));
a1f90215 10702 }
f48c7f4a 10703 else if (VAR_P (arg))
f2697631 10704 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10705 G_("increment of read-only variable %qD"),
10706 G_("decrement of read-only variable %qD"),
10707 G_("read-only variable %qD used as %<asm%> output")),
10708 arg);
a1f90215 10709 else if (TREE_CODE (arg) == PARM_DECL)
f2697631 10710 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10711 G_("increment of read-only parameter %qD"),
10712 G_("decrement of read-only parameter %qD"),
10713 G_("read-only parameter %qD use as %<asm%> output")),
10714 arg);
a1f90215 10715 else if (TREE_CODE (arg) == RESULT_DECL)
10716 {
10717 gcc_assert (c_dialect_cxx ());
f2697631 10718 error_at (loc, READONLY_MSG (G_("assignment of "
10719 "read-only named return value %qD"),
10720 G_("increment of "
10721 "read-only named return value %qD"),
10722 G_("decrement of "
10723 "read-only named return value %qD"),
10724 G_("read-only named return value %qD "
10725 "used as %<asm%>output")),
10726 arg);
a1f90215 10727 }
10728 else if (TREE_CODE (arg) == FUNCTION_DECL)
f2697631 10729 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10730 G_("increment of function %qD"),
10731 G_("decrement of function %qD"),
10732 G_("function %qD used as %<asm%> output")),
10733 arg);
a1f90215 10734 else
f2697631 10735 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10736 G_("increment of read-only location %qE"),
10737 G_("decrement of read-only location %qE"),
10738 G_("read-only location %qE used as %<asm%> output")),
10739 arg);
a1f90215 10740}
10741
e35976b1 10742/* Print an error message for an invalid lvalue. USE says
fdd84b77 10743 how the lvalue is being used and so selects the error message. LOC
10744 is the location for the error. */
ab6bb714 10745
e35976b1 10746void
fdd84b77 10747lvalue_error (location_t loc, enum lvalue_use use)
ab6bb714 10748{
e35976b1 10749 switch (use)
ab6bb714 10750 {
e35976b1 10751 case lv_assign:
fdd84b77 10752 error_at (loc, "lvalue required as left operand of assignment");
e35976b1 10753 break;
10754 case lv_increment:
fdd84b77 10755 error_at (loc, "lvalue required as increment operand");
e35976b1 10756 break;
10757 case lv_decrement:
fdd84b77 10758 error_at (loc, "lvalue required as decrement operand");
e35976b1 10759 break;
10760 case lv_addressof:
fdd84b77 10761 error_at (loc, "lvalue required as unary %<&%> operand");
e35976b1 10762 break;
10763 case lv_asm:
fdd84b77 10764 error_at (loc, "lvalue required in asm statement");
e35976b1 10765 break;
10766 default:
10767 gcc_unreachable ();
ab6bb714 10768 }
ab6bb714 10769}
b1bbc8e5 10770
10771/* Print an error message for an invalid indirection of type TYPE.
10772 ERRSTRING is the name of the operator for the indirection. */
10773
10774void
10775invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10776{
10777 switch (errstring)
10778 {
10779 case RO_NULL:
10780 gcc_assert (c_dialect_cxx ());
10781 error_at (loc, "invalid type argument (have %qT)", type);
10782 break;
10783 case RO_ARRAY_INDEXING:
10784 error_at (loc,
10785 "invalid type argument of array indexing (have %qT)",
10786 type);
10787 break;
10788 case RO_UNARY_STAR:
10789 error_at (loc,
10790 "invalid type argument of unary %<*%> (have %qT)",
10791 type);
10792 break;
10793 case RO_ARROW:
10794 error_at (loc,
10795 "invalid type argument of %<->%> (have %qT)",
10796 type);
10797 break;
7354a89b 10798 case RO_ARROW_STAR:
10799 error_at (loc,
10800 "invalid type argument of %<->*%> (have %qT)",
10801 type);
10802 break;
b1bbc8e5 10803 case RO_IMPLICIT_CONVERSION:
10804 error_at (loc,
10805 "invalid type argument of implicit conversion (have %qT)",
10806 type);
10807 break;
10808 default:
10809 gcc_unreachable ();
10810 }
10811}
c271bdb2 10812\f
10813/* *PTYPE is an incomplete array. Complete it with a domain based on
10814 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10815 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10816 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10817
10818int
10819complete_array_type (tree *ptype, tree initial_value, bool do_default)
10820{
10821 tree maxindex, type, main_type, elt, unqual_elt;
10822 int failure = 0, quals;
6753bca0 10823 hashval_t hashcode = 0;
f5298614 10824 bool overflow_p = false;
c271bdb2 10825
10826 maxindex = size_zero_node;
10827 if (initial_value)
10828 {
10829 if (TREE_CODE (initial_value) == STRING_CST)
10830 {
10831 int eltsize
10832 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10833 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10834 }
10835 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10836 {
f1f41a6c 10837 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
c271bdb2 10838
f1f41a6c 10839 if (vec_safe_is_empty (v))
c271bdb2 10840 {
10841 if (pedantic)
10842 failure = 3;
7542c3b4 10843 maxindex = ssize_int (-1);
c271bdb2 10844 }
10845 else
10846 {
10847 tree curindex;
c75b4594 10848 unsigned HOST_WIDE_INT cnt;
10849 constructor_elt *ce;
cee43f7e 10850 bool fold_p = false;
c271bdb2 10851
f1f41a6c 10852 if ((*v)[0].index)
f5298614 10853 maxindex = (*v)[0].index, fold_p = true;
10854
c271bdb2 10855 curindex = maxindex;
10856
f1f41a6c 10857 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
c271bdb2 10858 {
cee43f7e 10859 bool curfold_p = false;
c75b4594 10860 if (ce->index)
cee43f7e 10861 curindex = ce->index, curfold_p = true;
c271bdb2 10862 else
cee43f7e 10863 {
10864 if (fold_p)
f5298614 10865 {
10866 /* Since we treat size types now as ordinary
10867 unsigned types, we need an explicit overflow
10868 check. */
10869 tree orig = curindex;
10870 curindex = fold_convert (sizetype, curindex);
10871 overflow_p |= tree_int_cst_lt (curindex, orig);
10872 }
389dd41b 10873 curindex = size_binop (PLUS_EXPR, curindex,
10874 size_one_node);
cee43f7e 10875 }
c271bdb2 10876 if (tree_int_cst_lt (maxindex, curindex))
cee43f7e 10877 maxindex = curindex, fold_p = curfold_p;
c271bdb2 10878 }
f5298614 10879 if (fold_p)
10880 {
10881 tree orig = maxindex;
10882 maxindex = fold_convert (sizetype, maxindex);
10883 overflow_p |= tree_int_cst_lt (maxindex, orig);
10884 }
c271bdb2 10885 }
10886 }
10887 else
10888 {
10889 /* Make an error message unless that happened already. */
10890 if (initial_value != error_mark_node)
10891 failure = 1;
10892 }
10893 }
10894 else
10895 {
10896 failure = 2;
10897 if (!do_default)
10898 return failure;
10899 }
10900
10901 type = *ptype;
10902 elt = TREE_TYPE (type);
10903 quals = TYPE_QUALS (strip_array_types (elt));
10904 if (quals == 0)
10905 unqual_elt = elt;
10906 else
6d5d708e 10907 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
c271bdb2 10908
10909 /* Using build_distinct_type_copy and modifying things afterward instead
10910 of using build_array_type to create a new type preserves all of the
10911 TYPE_LANG_FLAG_? bits that the front end may have set. */
10912 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10913 TREE_TYPE (main_type) = unqual_elt;
783bb57e 10914 TYPE_DOMAIN (main_type)
10915 = build_range_type (TREE_TYPE (maxindex),
10916 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
c271bdb2 10917 layout_type (main_type);
10918
6753bca0 10919 /* Make sure we have the canonical MAIN_TYPE. */
10920 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
48e1416a 10921 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
6753bca0 10922 hashcode);
10923 main_type = type_hash_canon (hashcode, main_type);
10924
796735dc 10925 /* Fix the canonical type. */
10926 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10927 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10928 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10929 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10930 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10931 != TYPE_DOMAIN (main_type)))
48e1416a 10932 TYPE_CANONICAL (main_type)
796735dc 10933 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10934 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10935 else
10936 TYPE_CANONICAL (main_type) = main_type;
10937
c271bdb2 10938 if (quals == 0)
10939 type = main_type;
10940 else
10941 type = c_build_qualified_type (main_type, quals);
10942
4f5b8f2a 10943 if (COMPLETE_TYPE_P (type)
10944 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
f5298614 10945 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
4f5b8f2a 10946 {
10947 error ("size of array is too large");
10948 /* If we proceed with the array type as it is, we'll eventually
08f817b3 10949 crash in tree_to_[su]hwi(). */
4f5b8f2a 10950 type = error_mark_node;
10951 }
10952
c271bdb2 10953 *ptype = type;
10954 return failure;
10955}
ab6bb714 10956
93426222 10957/* Like c_mark_addressable but don't check register qualifier. */
10958void
10959c_common_mark_addressable_vec (tree t)
10960{
10961 while (handled_component_p (t))
10962 t = TREE_OPERAND (t, 0);
f48c7f4a 10963 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
93426222 10964 return;
10965 TREE_ADDRESSABLE (t) = 1;
10966}
10967
10968
b6a5fc45 10969\f
10970/* Used to help initialize the builtin-types.def table. When a type of
10971 the correct size doesn't exist, use error_mark_node instead of NULL.
10972 The later results in segfaults even when a decl using the type doesn't
10973 get invoked. */
10974
10975tree
10976builtin_type_for_size (int size, bool unsignedp)
10977{
a51edb4c 10978 tree type = c_common_type_for_size (size, unsignedp);
b6a5fc45 10979 return type ? type : error_mark_node;
10980}
10981
10982/* A helper function for resolve_overloaded_builtin in resolving the
10983 overloaded __sync_ builtins. Returns a positive power of 2 if the
10984 first operand of PARAMS is a pointer to a supported data type.
10985 Returns 0 if an error is encountered. */
10986
10987static int
f1f41a6c 10988sync_resolve_size (tree function, vec<tree, va_gc> *params)
b6a5fc45 10989{
10990 tree type;
10991 int size;
10992
f1f41a6c 10993 if (!params)
b6a5fc45 10994 {
10995 error ("too few arguments to function %qE", function);
10996 return 0;
10997 }
10998
f1f41a6c 10999 type = TREE_TYPE ((*params)[0]);
0d284870 11000 if (TREE_CODE (type) == ARRAY_TYPE)
11001 {
11002 /* Force array-to-pointer decay for C++. */
11003 gcc_assert (c_dialect_cxx());
11004 (*params)[0] = default_conversion ((*params)[0]);
11005 type = TREE_TYPE ((*params)[0]);
11006 }
b6a5fc45 11007 if (TREE_CODE (type) != POINTER_TYPE)
11008 goto incompatible;
11009
11010 type = TREE_TYPE (type);
11011 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
11012 goto incompatible;
11013
e913b5cd 11014 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
27213ba3 11015 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
b6a5fc45 11016 return size;
11017
11018 incompatible:
11019 error ("incompatible type for argument %d of %qE", 1, function);
11020 return 0;
11021}
11022
a0c938f0 11023/* A helper function for resolve_overloaded_builtin. Adds casts to
b6a5fc45 11024 PARAMS to make arguments match up with those of FUNCTION. Drops
11025 the variadic arguments at the end. Returns false if some error
11026 was encountered; true on success. */
11027
11028static bool
1cd6e20d 11029sync_resolve_params (location_t loc, tree orig_function, tree function,
f1f41a6c 11030 vec<tree, va_gc> *params, bool orig_format)
b6a5fc45 11031{
d0af78c5 11032 function_args_iterator iter;
b6a5fc45 11033 tree ptype;
b9c74b4d 11034 unsigned int parmnum;
b6a5fc45 11035
d0af78c5 11036 function_args_iter_init (&iter, TREE_TYPE (function));
b6a5fc45 11037 /* We've declared the implementation functions to use "volatile void *"
11038 as the pointer parameter, so we shouldn't get any complaints from the
11039 call to check_function_arguments what ever type the user used. */
d0af78c5 11040 function_args_iter_next (&iter);
f1f41a6c 11041 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
b560fabd 11042 ptype = TYPE_MAIN_VARIANT (ptype);
b6a5fc45 11043
11044 /* For the rest of the values, we need to cast these to FTYPE, so that we
11045 don't get warnings for passing pointer types, etc. */
b9c74b4d 11046 parmnum = 0;
d0af78c5 11047 while (1)
b6a5fc45 11048 {
d0af78c5 11049 tree val, arg_type;
11050
11051 arg_type = function_args_iter_cond (&iter);
11052 /* XXX void_type_node belies the abstraction. */
11053 if (arg_type == void_type_node)
11054 break;
b6a5fc45 11055
b9c74b4d 11056 ++parmnum;
f1f41a6c 11057 if (params->length () <= parmnum)
b6a5fc45 11058 {
1cd6e20d 11059 error_at (loc, "too few arguments to function %qE", orig_function);
b6a5fc45 11060 return false;
11061 }
11062
0f6a7cb7 11063 /* Only convert parameters if arg_type is unsigned integer type with
11064 new format sync routines, i.e. don't attempt to convert pointer
11065 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
11066 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
11067 kinds). */
11068 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
1cd6e20d 11069 {
11070 /* Ideally for the first conversion we'd use convert_for_assignment
11071 so that we get warnings for anything that doesn't match the pointer
11072 type. This isn't portable across the C and C++ front ends atm. */
f1f41a6c 11073 val = (*params)[parmnum];
1cd6e20d 11074 val = convert (ptype, val);
11075 val = convert (arg_type, val);
f1f41a6c 11076 (*params)[parmnum] = val;
1cd6e20d 11077 }
b6a5fc45 11078
d0af78c5 11079 function_args_iter_next (&iter);
b6a5fc45 11080 }
11081
1cd6e20d 11082 /* __atomic routines are not variadic. */
f1f41a6c 11083 if (!orig_format && params->length () != parmnum + 1)
1cd6e20d 11084 {
11085 error_at (loc, "too many arguments to function %qE", orig_function);
11086 return false;
11087 }
11088
b6a5fc45 11089 /* The definition of these primitives is variadic, with the remaining
11090 being "an optional list of variables protected by the memory barrier".
11091 No clue what that's supposed to mean, precisely, but we consider all
11092 call-clobbered variables to be protected so we're safe. */
f1f41a6c 11093 params->truncate (parmnum + 1);
b6a5fc45 11094
11095 return true;
11096}
11097
a0c938f0 11098/* A helper function for resolve_overloaded_builtin. Adds a cast to
b6a5fc45 11099 RESULT to make it match the type of the first pointer argument in
11100 PARAMS. */
11101
11102static tree
1cd6e20d 11103sync_resolve_return (tree first_param, tree result, bool orig_format)
b6a5fc45 11104{
b9c74b4d 11105 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
1cd6e20d 11106 tree rtype = TREE_TYPE (result);
10080eac 11107 ptype = TYPE_MAIN_VARIANT (ptype);
1cd6e20d 11108
11109 /* New format doesn't require casting unless the types are the same size. */
11110 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
11111 return convert (ptype, result);
11112 else
11113 return result;
11114}
11115
11116/* This function verifies the PARAMS to generic atomic FUNCTION.
11117 It returns the size if all the parameters are the same size, otherwise
11118 0 is returned if the parameters are invalid. */
11119
11120static int
f1f41a6c 11121get_atomic_generic_size (location_t loc, tree function,
11122 vec<tree, va_gc> *params)
1cd6e20d 11123{
11124 unsigned int n_param;
11125 unsigned int n_model;
11126 unsigned int x;
11127 int size_0;
11128 tree type_0;
11129
11130 /* Determine the parameter makeup. */
11131 switch (DECL_FUNCTION_CODE (function))
11132 {
11133 case BUILT_IN_ATOMIC_EXCHANGE:
11134 n_param = 4;
11135 n_model = 1;
11136 break;
11137 case BUILT_IN_ATOMIC_LOAD:
11138 case BUILT_IN_ATOMIC_STORE:
11139 n_param = 3;
11140 n_model = 1;
11141 break;
11142 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11143 n_param = 6;
11144 n_model = 2;
11145 break;
11146 default:
1d581089 11147 gcc_unreachable ();
1cd6e20d 11148 }
11149
f1f41a6c 11150 if (vec_safe_length (params) != n_param)
1cd6e20d 11151 {
11152 error_at (loc, "incorrect number of arguments to function %qE", function);
11153 return 0;
11154 }
11155
11156 /* Get type of first parameter, and determine its size. */
f1f41a6c 11157 type_0 = TREE_TYPE ((*params)[0]);
0d284870 11158 if (TREE_CODE (type_0) == ARRAY_TYPE)
11159 {
11160 /* Force array-to-pointer decay for C++. */
11161 gcc_assert (c_dialect_cxx());
11162 (*params)[0] = default_conversion ((*params)[0]);
11163 type_0 = TREE_TYPE ((*params)[0]);
11164 }
1d581089 11165 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
11166 {
11167 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
11168 function);
11169 return 0;
11170 }
11171
11172 /* Types must be compile time constant sizes. */
11173 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
1cd6e20d 11174 {
1d581089 11175 error_at (loc,
11176 "argument 1 of %qE must be a pointer to a constant size type",
11177 function);
1cd6e20d 11178 return 0;
11179 }
1d581089 11180
e913b5cd 11181 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
1cd6e20d 11182
1d581089 11183 /* Zero size objects are not allowed. */
11184 if (size_0 == 0)
11185 {
11186 error_at (loc,
11187 "argument 1 of %qE must be a pointer to a nonzero size object",
11188 function);
11189 return 0;
11190 }
11191
1cd6e20d 11192 /* Check each other parameter is a pointer and the same size. */
11193 for (x = 0; x < n_param - n_model; x++)
11194 {
11195 int size;
f1f41a6c 11196 tree type = TREE_TYPE ((*params)[x]);
a04e8d62 11197 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
1cd6e20d 11198 if (n_param == 6 && x == 3)
11199 continue;
11200 if (!POINTER_TYPE_P (type))
11201 {
11202 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
11203 function);
11204 return 0;
11205 }
1f6be080 11206 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
11207 size = type_size ? tree_to_uhwi (type_size) : 0;
1cd6e20d 11208 if (size != size_0)
11209 {
11210 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
11211 function);
11212 return 0;
11213 }
11214 }
11215
11216 /* Check memory model parameters for validity. */
11217 for (x = n_param - n_model ; x < n_param; x++)
11218 {
f1f41a6c 11219 tree p = (*params)[x];
1cd6e20d 11220 if (TREE_CODE (p) == INTEGER_CST)
11221 {
e913b5cd 11222 int i = tree_to_uhwi (p);
a372f7ca 11223 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
1cd6e20d 11224 {
11225 warning_at (loc, OPT_Winvalid_memory_model,
11226 "invalid memory model argument %d of %qE", x + 1,
11227 function);
1cd6e20d 11228 }
11229 }
11230 else
11231 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
11232 {
11233 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
11234 function);
11235 return 0;
11236 }
11237 }
11238
11239 return size_0;
11240}
11241
11242
11243/* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
11244 at the beginning of the parameter list PARAMS representing the size of the
11245 objects. This is to match the library ABI requirement. LOC is the location
11246 of the function call.
11247 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
11248 returned to allow the external call to be constructed. */
11249
11250static tree
11251add_atomic_size_parameter (unsigned n, location_t loc, tree function,
f1f41a6c 11252 vec<tree, va_gc> *params)
1cd6e20d 11253{
11254 tree size_node;
11255
11256 /* Insert a SIZE_T parameter as the first param. If there isn't
11257 enough space, allocate a new vector and recursively re-build with that. */
f1f41a6c 11258 if (!params->space (1))
1cd6e20d 11259 {
11260 unsigned int z, len;
f1f41a6c 11261 vec<tree, va_gc> *v;
1cd6e20d 11262 tree f;
11263
f1f41a6c 11264 len = params->length ();
11265 vec_alloc (v, len + 1);
5a672e62 11266 v->quick_push (build_int_cst (size_type_node, n));
1cd6e20d 11267 for (z = 0; z < len; z++)
f1f41a6c 11268 v->quick_push ((*params)[z]);
ec761d5a 11269 f = build_function_call_vec (loc, vNULL, function, v, NULL);
f1f41a6c 11270 vec_free (v);
1cd6e20d 11271 return f;
11272 }
11273
11274 /* Add the size parameter and leave as a function call for processing. */
11275 size_node = build_int_cst (size_type_node, n);
f1f41a6c 11276 params->quick_insert (0, size_node);
1cd6e20d 11277 return NULL_TREE;
11278}
11279
11280
a056826c 11281/* Return whether atomic operations for naturally aligned N-byte
11282 arguments are supported, whether inline or through libatomic. */
11283static bool
11284atomic_size_supported_p (int n)
11285{
11286 switch (n)
11287 {
11288 case 1:
11289 case 2:
11290 case 4:
11291 case 8:
11292 return true;
11293
11294 case 16:
11295 return targetm.scalar_mode_supported_p (TImode);
11296
11297 default:
11298 return false;
11299 }
11300}
11301
1cd6e20d 11302/* This will process an __atomic_exchange function call, determine whether it
11303 needs to be mapped to the _N variation, or turned into a library call.
11304 LOC is the location of the builtin call.
11305 FUNCTION is the DECL that has been invoked;
11306 PARAMS is the argument list for the call. The return value is non-null
11307 TRUE is returned if it is translated into the proper format for a call to the
11308 external library, and NEW_RETURN is set the tree for that function.
11309 FALSE is returned if processing for the _N variation is required, and
47ae02b7 11310 NEW_RETURN is set to the return value the result is copied into. */
1cd6e20d 11311static bool
11312resolve_overloaded_atomic_exchange (location_t loc, tree function,
f1f41a6c 11313 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11314{
11315 tree p0, p1, p2, p3;
11316 tree I_type, I_type_ptr;
11317 int n = get_atomic_generic_size (loc, function, params);
11318
1d581089 11319 /* Size of 0 is an error condition. */
11320 if (n == 0)
11321 {
11322 *new_return = error_mark_node;
11323 return true;
11324 }
11325
1cd6e20d 11326 /* If not a lock-free size, change to the library generic format. */
a056826c 11327 if (!atomic_size_supported_p (n))
1cd6e20d 11328 {
11329 *new_return = add_atomic_size_parameter (n, loc, function, params);
11330 return true;
11331 }
11332
11333 /* Otherwise there is a lockfree match, transform the call from:
11334 void fn(T* mem, T* desired, T* return, model)
11335 into
11336 *return = (T) (fn (In* mem, (In) *desired, model)) */
11337
f1f41a6c 11338 p0 = (*params)[0];
11339 p1 = (*params)[1];
11340 p2 = (*params)[2];
11341 p3 = (*params)[3];
1cd6e20d 11342
11343 /* Create pointer to appropriate size. */
11344 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11345 I_type_ptr = build_pointer_type (I_type);
11346
11347 /* Convert object pointer to required type. */
11348 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11349 (*params)[0] = p0;
1cd6e20d 11350 /* Convert new value to required type, and dereference it. */
11351 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11352 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 11353 (*params)[1] = p1;
1cd6e20d 11354
11355 /* Move memory model to the 3rd position, and end param list. */
f1f41a6c 11356 (*params)[2] = p3;
11357 params->truncate (3);
1cd6e20d 11358
11359 /* Convert return pointer and dereference it for later assignment. */
11360 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11361
11362 return false;
b6a5fc45 11363}
11364
1cd6e20d 11365
11366/* This will process an __atomic_compare_exchange function call, determine
11367 whether it needs to be mapped to the _N variation, or turned into a lib call.
11368 LOC is the location of the builtin call.
11369 FUNCTION is the DECL that has been invoked;
11370 PARAMS is the argument list for the call. The return value is non-null
11371 TRUE is returned if it is translated into the proper format for a call to the
11372 external library, and NEW_RETURN is set the tree for that function.
11373 FALSE is returned if processing for the _N variation is required. */
11374
11375static bool
11376resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
f1f41a6c 11377 vec<tree, va_gc> *params,
1cd6e20d 11378 tree *new_return)
11379{
11380 tree p0, p1, p2;
11381 tree I_type, I_type_ptr;
11382 int n = get_atomic_generic_size (loc, function, params);
11383
1d581089 11384 /* Size of 0 is an error condition. */
11385 if (n == 0)
11386 {
11387 *new_return = error_mark_node;
11388 return true;
11389 }
11390
1cd6e20d 11391 /* If not a lock-free size, change to the library generic format. */
a056826c 11392 if (!atomic_size_supported_p (n))
1cd6e20d 11393 {
11394 /* The library generic format does not have the weak parameter, so
11395 remove it from the param list. Since a parameter has been removed,
11396 we can be sure that there is room for the SIZE_T parameter, meaning
11397 there will not be a recursive rebuilding of the parameter list, so
11398 there is no danger this will be done twice. */
11399 if (n > 0)
11400 {
f1f41a6c 11401 (*params)[3] = (*params)[4];
11402 (*params)[4] = (*params)[5];
11403 params->truncate (5);
1cd6e20d 11404 }
11405 *new_return = add_atomic_size_parameter (n, loc, function, params);
11406 return true;
11407 }
11408
11409 /* Otherwise, there is a match, so the call needs to be transformed from:
11410 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11411 into
11412 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
ab2c1de8 11413
f1f41a6c 11414 p0 = (*params)[0];
11415 p1 = (*params)[1];
11416 p2 = (*params)[2];
1cd6e20d 11417
11418 /* Create pointer to appropriate size. */
11419 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11420 I_type_ptr = build_pointer_type (I_type);
11421
11422 /* Convert object pointer to required type. */
11423 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11424 (*params)[0] = p0;
1cd6e20d 11425
11426 /* Convert expected pointer to required type. */
11427 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
f1f41a6c 11428 (*params)[1] = p1;
1cd6e20d 11429
11430 /* Convert desired value to required type, and dereference it. */
11431 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11432 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
f1f41a6c 11433 (*params)[2] = p2;
1cd6e20d 11434
11435 /* The rest of the parameters are fine. NULL means no special return value
11436 processing.*/
11437 *new_return = NULL;
11438 return false;
11439}
11440
11441
11442/* This will process an __atomic_load function call, determine whether it
11443 needs to be mapped to the _N variation, or turned into a library call.
11444 LOC is the location of the builtin call.
11445 FUNCTION is the DECL that has been invoked;
11446 PARAMS is the argument list for the call. The return value is non-null
11447 TRUE is returned if it is translated into the proper format for a call to the
11448 external library, and NEW_RETURN is set the tree for that function.
11449 FALSE is returned if processing for the _N variation is required, and
47ae02b7 11450 NEW_RETURN is set to the return value the result is copied into. */
1cd6e20d 11451
11452static bool
11453resolve_overloaded_atomic_load (location_t loc, tree function,
f1f41a6c 11454 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11455{
11456 tree p0, p1, p2;
11457 tree I_type, I_type_ptr;
11458 int n = get_atomic_generic_size (loc, function, params);
11459
1d581089 11460 /* Size of 0 is an error condition. */
11461 if (n == 0)
11462 {
11463 *new_return = error_mark_node;
11464 return true;
11465 }
11466
1cd6e20d 11467 /* If not a lock-free size, change to the library generic format. */
a056826c 11468 if (!atomic_size_supported_p (n))
1cd6e20d 11469 {
11470 *new_return = add_atomic_size_parameter (n, loc, function, params);
11471 return true;
11472 }
11473
11474 /* Otherwise, there is a match, so the call needs to be transformed from:
11475 void fn(T* mem, T* return, model)
11476 into
11477 *return = (T) (fn ((In *) mem, model)) */
11478
f1f41a6c 11479 p0 = (*params)[0];
11480 p1 = (*params)[1];
11481 p2 = (*params)[2];
1cd6e20d 11482
11483 /* Create pointer to appropriate size. */
11484 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11485 I_type_ptr = build_pointer_type (I_type);
11486
11487 /* Convert object pointer to required type. */
11488 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11489 (*params)[0] = p0;
1cd6e20d 11490
11491 /* Move memory model to the 2nd position, and end param list. */
f1f41a6c 11492 (*params)[1] = p2;
11493 params->truncate (2);
1cd6e20d 11494
11495 /* Convert return pointer and dereference it for later assignment. */
11496 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11497
11498 return false;
11499}
11500
11501
11502/* This will process an __atomic_store function call, determine whether it
11503 needs to be mapped to the _N variation, or turned into a library call.
11504 LOC is the location of the builtin call.
11505 FUNCTION is the DECL that has been invoked;
11506 PARAMS is the argument list for the call. The return value is non-null
11507 TRUE is returned if it is translated into the proper format for a call to the
11508 external library, and NEW_RETURN is set the tree for that function.
11509 FALSE is returned if processing for the _N variation is required, and
47ae02b7 11510 NEW_RETURN is set to the return value the result is copied into. */
1cd6e20d 11511
11512static bool
11513resolve_overloaded_atomic_store (location_t loc, tree function,
f1f41a6c 11514 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11515{
11516 tree p0, p1;
11517 tree I_type, I_type_ptr;
11518 int n = get_atomic_generic_size (loc, function, params);
11519
1d581089 11520 /* Size of 0 is an error condition. */
11521 if (n == 0)
11522 {
11523 *new_return = error_mark_node;
11524 return true;
11525 }
11526
1cd6e20d 11527 /* If not a lock-free size, change to the library generic format. */
a056826c 11528 if (!atomic_size_supported_p (n))
1cd6e20d 11529 {
11530 *new_return = add_atomic_size_parameter (n, loc, function, params);
11531 return true;
11532 }
11533
11534 /* Otherwise, there is a match, so the call needs to be transformed from:
11535 void fn(T* mem, T* value, model)
11536 into
11537 fn ((In *) mem, (In) *value, model) */
11538
f1f41a6c 11539 p0 = (*params)[0];
11540 p1 = (*params)[1];
1cd6e20d 11541
11542 /* Create pointer to appropriate size. */
11543 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11544 I_type_ptr = build_pointer_type (I_type);
11545
11546 /* Convert object pointer to required type. */
11547 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11548 (*params)[0] = p0;
1cd6e20d 11549
11550 /* Convert new value to required type, and dereference it. */
11551 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11552 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 11553 (*params)[1] = p1;
1cd6e20d 11554
11555 /* The memory model is in the right spot already. Return is void. */
11556 *new_return = NULL_TREE;
11557
11558 return false;
11559}
11560
11561
b6a5fc45 11562/* Some builtin functions are placeholders for other expressions. This
11563 function should be called immediately after parsing the call expression
11564 before surrounding code has committed to the type of the expression.
11565
e60a6f7b 11566 LOC is the location of the builtin call.
11567
b6a5fc45 11568 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11569 PARAMS is the argument list for the call. The return value is non-null
11570 when expansion is complete, and null if normal processing should
11571 continue. */
11572
11573tree
f1f41a6c 11574resolve_overloaded_builtin (location_t loc, tree function,
11575 vec<tree, va_gc> *params)
b6a5fc45 11576{
11577 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
1cd6e20d 11578 bool orig_format = true;
11579 tree new_return = NULL_TREE;
11580
65441f6f 11581 switch (DECL_BUILT_IN_CLASS (function))
11582 {
11583 case BUILT_IN_NORMAL:
11584 break;
11585 case BUILT_IN_MD:
11586 if (targetm.resolve_overloaded_builtin)
e60a6f7b 11587 return targetm.resolve_overloaded_builtin (loc, function, params);
65441f6f 11588 else
a0c938f0 11589 return NULL_TREE;
65441f6f 11590 default:
11591 return NULL_TREE;
11592 }
a0c938f0 11593
65441f6f 11594 /* Handle BUILT_IN_NORMAL here. */
b6a5fc45 11595 switch (orig_code)
11596 {
1cd6e20d 11597 case BUILT_IN_ATOMIC_EXCHANGE:
11598 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11599 case BUILT_IN_ATOMIC_LOAD:
11600 case BUILT_IN_ATOMIC_STORE:
11601 {
11602 /* Handle these 4 together so that they can fall through to the next
11603 case if the call is transformed to an _N variant. */
11604 switch (orig_code)
11605 {
11606 case BUILT_IN_ATOMIC_EXCHANGE:
11607 {
11608 if (resolve_overloaded_atomic_exchange (loc, function, params,
11609 &new_return))
11610 return new_return;
11611 /* Change to the _N variant. */
11612 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11613 break;
11614 }
11615
11616 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11617 {
11618 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11619 params,
11620 &new_return))
11621 return new_return;
11622 /* Change to the _N variant. */
11623 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11624 break;
11625 }
11626 case BUILT_IN_ATOMIC_LOAD:
11627 {
11628 if (resolve_overloaded_atomic_load (loc, function, params,
11629 &new_return))
11630 return new_return;
11631 /* Change to the _N variant. */
11632 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11633 break;
11634 }
11635 case BUILT_IN_ATOMIC_STORE:
11636 {
11637 if (resolve_overloaded_atomic_store (loc, function, params,
11638 &new_return))
11639 return new_return;
11640 /* Change to the _N variant. */
11641 orig_code = BUILT_IN_ATOMIC_STORE_N;
11642 break;
11643 }
11644 default:
11645 gcc_unreachable ();
11646 }
11647 /* Fallthrough to the normal processing. */
11648 }
11649 case BUILT_IN_ATOMIC_EXCHANGE_N:
11650 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11651 case BUILT_IN_ATOMIC_LOAD_N:
11652 case BUILT_IN_ATOMIC_STORE_N:
11653 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11654 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11655 case BUILT_IN_ATOMIC_AND_FETCH_N:
11656 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11657 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11658 case BUILT_IN_ATOMIC_OR_FETCH_N:
11659 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11660 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11661 case BUILT_IN_ATOMIC_FETCH_AND_N:
11662 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11663 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11664 case BUILT_IN_ATOMIC_FETCH_OR_N:
11665 {
11666 orig_format = false;
11667 /* Fallthru for parameter processing. */
11668 }
2797f13a 11669 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11670 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11671 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11672 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11673 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11674 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11675 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11676 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11677 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11678 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11679 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11680 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11681 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11682 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11683 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11684 case BUILT_IN_SYNC_LOCK_RELEASE_N:
b6a5fc45 11685 {
11686 int n = sync_resolve_size (function, params);
b9c74b4d 11687 tree new_function, first_param, result;
b9a16870 11688 enum built_in_function fncode;
b6a5fc45 11689
11690 if (n == 0)
11691 return error_mark_node;
11692
b9a16870 11693 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11694 new_function = builtin_decl_explicit (fncode);
1cd6e20d 11695 if (!sync_resolve_params (loc, function, new_function, params,
11696 orig_format))
b6a5fc45 11697 return error_mark_node;
11698
f1f41a6c 11699 first_param = (*params)[0];
ec761d5a 11700 result = build_function_call_vec (loc, vNULL, new_function, params,
11701 NULL);
1cd6e20d 11702 if (result == error_mark_node)
11703 return result;
2797f13a 11704 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
1cd6e20d 11705 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11706 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11707 result = sync_resolve_return (first_param, result, orig_format);
b6a5fc45 11708
1cd6e20d 11709 /* If new_return is set, assign function to that expr and cast the
11710 result to void since the generic interface returned void. */
11711 if (new_return)
11712 {
11713 /* Cast function result from I{1,2,4,8,16} to the required type. */
11714 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11715 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11716 result);
11717 TREE_SIDE_EFFECTS (result) = 1;
11718 protected_set_expr_location (result, loc);
11719 result = convert (void_type_node, result);
11720 }
b6a5fc45 11721 return result;
11722 }
11723
11724 default:
65441f6f 11725 return NULL_TREE;
b6a5fc45 11726 }
11727}
11728
73437615 11729/* vector_types_compatible_elements_p is used in type checks of vectors
11730 values used as operands of binary operators. Where it returns true, and
11731 the other checks of the caller succeed (being vector types in he first
11732 place, and matching number of elements), we can just treat the types
11733 as essentially the same.
11734 Contrast with vector_targets_convertible_p, which is used for vector
11735 pointer types, and vector_types_convertible_p, which will allow
11736 language-specific matches under the control of flag_lax_vector_conversions,
11737 and might still require a conversion. */
11738/* True if vector types T1 and T2 can be inputs to the same binary
11739 operator without conversion.
11740 We don't check the overall vector size here because some of our callers
11741 want to give different error messages when the vectors are compatible
11742 except for the element count. */
11743
491255f5 11744bool
73437615 11745vector_types_compatible_elements_p (tree t1, tree t2)
491255f5 11746{
73437615 11747 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11748 t1 = TREE_TYPE (t1);
11749 t2 = TREE_TYPE (t2);
11750
491255f5 11751 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11752
9421ebb9 11753 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11754 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11755 || c2 == FIXED_POINT_TYPE));
491255f5 11756
73437615 11757 t1 = c_common_signed_type (t1);
11758 t2 = c_common_signed_type (t2);
491255f5 11759 /* Equality works here because c_common_signed_type uses
11760 TYPE_MAIN_VARIANT. */
73437615 11761 if (t1 == t2)
11762 return true;
11763 if (opaque && c1 == c2
11764 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11765 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11766 return true;
11767 return false;
491255f5 11768}
11769
be7350e7 11770/* Check for missing format attributes on function pointers. LTYPE is
11771 the new type or left-hand side type. RTYPE is the old type or
11772 right-hand side type. Returns TRUE if LTYPE is missing the desired
11773 attribute. */
11774
11775bool
11776check_missing_format_attribute (tree ltype, tree rtype)
11777{
11778 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11779 tree ra;
11780
11781 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11782 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11783 break;
11784 if (ra)
11785 {
11786 tree la;
11787 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11788 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11789 break;
11790 return !la;
11791 }
11792 else
11793 return false;
11794}
11795
2840aae4 11796/* Subscripting with type char is likely to lose on a machine where
11797 chars are signed. So warn on any machine, but optionally. Don't
11798 warn for unsigned char since that type is safe. Don't warn for
11799 signed char because anyone who uses that must have done so
11800 deliberately. Furthermore, we reduce the false positive load by
11801 warning only for non-constant value of type char. */
11802
11803void
92b63884 11804warn_array_subscript_with_type_char (location_t loc, tree index)
2840aae4 11805{
11806 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11807 && TREE_CODE (index) != INTEGER_CST)
92b63884 11808 warning_at (loc, OPT_Wchar_subscripts,
11809 "array subscript has type %<char%>");
2840aae4 11810}
11811
e534436e 11812/* Implement -Wparentheses for the unexpected C precedence rules, to
11813 cover cases like x + y << z which readers are likely to
11814 misinterpret. We have seen an expression in which CODE is a binary
82012ffe 11815 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11816 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11817 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11818 expression was not formed using a binary or unary operator, or it
11819 was enclosed in parentheses. */
e534436e 11820
11821void
b0e7825e 11822warn_about_parentheses (location_t loc, enum tree_code code,
269f7979 11823 enum tree_code code_left, tree arg_left,
82012ffe 11824 enum tree_code code_right, tree arg_right)
e534436e 11825{
11826 if (!warn_parentheses)
11827 return;
11828
82012ffe 11829 /* This macro tests that the expression ARG with original tree code
11830 CODE appears to be a boolean expression. or the result of folding a
11831 boolean expression. */
11832#define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11833 (truth_value_p (TREE_CODE (ARG)) \
11834 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11835 /* Folding may create 0 or 1 integers from other expressions. */ \
11836 || ((CODE) != INTEGER_CST \
11837 && (integer_onep (ARG) || integer_zerop (ARG))))
11838
48e1416a 11839 switch (code)
e534436e 11840 {
82012ffe 11841 case LSHIFT_EXPR:
b0e7825e 11842 if (code_left == PLUS_EXPR)
11843 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11844 "suggest parentheses around %<+%> inside %<<<%>");
11845 else if (code_right == PLUS_EXPR)
11846 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11847 "suggest parentheses around %<+%> inside %<<<%>");
11848 else if (code_left == MINUS_EXPR)
11849 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11850 "suggest parentheses around %<-%> inside %<<<%>");
11851 else if (code_right == MINUS_EXPR)
11852 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11853 "suggest parentheses around %<-%> inside %<<<%>");
82012ffe 11854 return;
e534436e 11855
82012ffe 11856 case RSHIFT_EXPR:
b0e7825e 11857 if (code_left == PLUS_EXPR)
11858 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11859 "suggest parentheses around %<+%> inside %<>>%>");
11860 else if (code_right == PLUS_EXPR)
11861 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11862 "suggest parentheses around %<+%> inside %<>>%>");
11863 else if (code_left == MINUS_EXPR)
11864 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11865 "suggest parentheses around %<-%> inside %<>>%>");
11866 else if (code_right == MINUS_EXPR)
11867 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11868 "suggest parentheses around %<-%> inside %<>>%>");
82012ffe 11869 return;
e534436e 11870
82012ffe 11871 case TRUTH_ORIF_EXPR:
b0e7825e 11872 if (code_left == TRUTH_ANDIF_EXPR)
11873 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11874 "suggest parentheses around %<&&%> within %<||%>");
11875 else if (code_right == TRUTH_ANDIF_EXPR)
11876 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11877 "suggest parentheses around %<&&%> within %<||%>");
82012ffe 11878 return;
11879
11880 case BIT_IOR_EXPR:
e534436e 11881 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
b0e7825e 11882 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11883 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11884 "suggest parentheses around arithmetic in operand of %<|%>");
11885 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11886 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11887 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11888 "suggest parentheses around arithmetic in operand of %<|%>");
e534436e 11889 /* Check cases like x|y==z */
b0e7825e 11890 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11891 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11892 "suggest parentheses around comparison in operand of %<|%>");
11893 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11894 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11895 "suggest parentheses around comparison in operand of %<|%>");
11896 /* Check cases like !x | y */
11897 else if (code_left == TRUTH_NOT_EXPR
11898 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 11899 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11900 "suggest parentheses around operand of "
11901 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
82012ffe 11902 return;
e534436e 11903
82012ffe 11904 case BIT_XOR_EXPR:
e534436e 11905 if (code_left == BIT_AND_EXPR
b0e7825e 11906 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11907 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11908 "suggest parentheses around arithmetic in operand of %<^%>");
11909 else if (code_right == BIT_AND_EXPR
11910 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11911 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11912 "suggest parentheses around arithmetic in operand of %<^%>");
e534436e 11913 /* Check cases like x^y==z */
b0e7825e 11914 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11915 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11916 "suggest parentheses around comparison in operand of %<^%>");
11917 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11918 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11919 "suggest parentheses around comparison in operand of %<^%>");
11920 return;
e534436e 11921
82012ffe 11922 case BIT_AND_EXPR:
b0e7825e 11923 if (code_left == PLUS_EXPR)
11924 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11925 "suggest parentheses around %<+%> in operand of %<&%>");
11926 else if (code_right == PLUS_EXPR)
11927 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11928 "suggest parentheses around %<+%> in operand of %<&%>");
b0e7825e 11929 else if (code_left == MINUS_EXPR)
11930 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11931 "suggest parentheses around %<-%> in operand of %<&%>");
11932 else if (code_right == MINUS_EXPR)
11933 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11934 "suggest parentheses around %<-%> in operand of %<&%>");
e534436e 11935 /* Check cases like x&y==z */
b0e7825e 11936 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11937 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11938 "suggest parentheses around comparison in operand of %<&%>");
11939 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11940 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11941 "suggest parentheses around comparison in operand of %<&%>");
11942 /* Check cases like !x & y */
11943 else if (code_left == TRUTH_NOT_EXPR
11944 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 11945 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11946 "suggest parentheses around operand of "
11947 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
82012ffe 11948 return;
e534436e 11949
82012ffe 11950 case EQ_EXPR:
b0e7825e 11951 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11952 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11953 "suggest parentheses around comparison in operand of %<==%>");
11954 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11955 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11956 "suggest parentheses around comparison in operand of %<==%>");
11957 return;
11958 case NE_EXPR:
b0e7825e 11959 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11960 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11961 "suggest parentheses around comparison in operand of %<!=%>");
11962 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11963 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11964 "suggest parentheses around comparison in operand of %<!=%>");
11965 return;
11966
11967 default:
b0e7825e 11968 if (TREE_CODE_CLASS (code) == tcc_comparison)
11969 {
11970 if (TREE_CODE_CLASS (code_left) == tcc_comparison
269f7979 11971 && code_left != NE_EXPR && code_left != EQ_EXPR
11972 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
b0e7825e 11973 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11974 "comparisons like %<X<=Y<=Z%> do not "
11975 "have their mathematical meaning");
11976 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
269f7979 11977 && code_right != NE_EXPR && code_right != EQ_EXPR
b0e7825e 11978 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11979 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11980 "comparisons like %<X<=Y<=Z%> do not "
11981 "have their mathematical meaning");
11982 }
82012ffe 11983 return;
6ce0c450 11984 }
82012ffe 11985#undef NOT_A_BOOLEAN_EXPR_P
e534436e 11986}
11987
92fccaaa 11988/* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11989
11990void
11991warn_for_unused_label (tree label)
11992{
11993 if (!TREE_USED (label))
11994 {
11995 if (DECL_INITIAL (label))
11996 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11997 else
11998 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11999 }
12000}
2840aae4 12001
b6889cb0 12002/* Warn for division by zero according to the value of DIVISOR. LOC
12003 is the location of the division operator. */
f092582b 12004
12005void
b6889cb0 12006warn_for_div_by_zero (location_t loc, tree divisor)
f092582b 12007{
9421ebb9 12008 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
12009 about division by zero. Do not issue a warning if DIVISOR has a
f092582b 12010 floating-point type, since we consider 0.0/0.0 a valid way of
12011 generating a NaN. */
48d94ede 12012 if (c_inhibit_evaluation_warnings == 0
9421ebb9 12013 && (integer_zerop (divisor) || fixed_zerop (divisor)))
b6889cb0 12014 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
f092582b 12015}
12016
13869a99 12017/* Subroutine of build_binary_op. Give warnings for comparisons
12018 between signed and unsigned quantities that may fail. Do the
12019 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
12020 so that casts will be considered, but default promotions won't
8e70fb09 12021 be.
12022
12023 LOCATION is the location of the comparison operator.
13869a99 12024
12025 The arguments of this function map directly to local variables
12026 of build_binary_op. */
12027
48e1416a 12028void
8e70fb09 12029warn_for_sign_compare (location_t location,
48e1416a 12030 tree orig_op0, tree orig_op1,
12031 tree op0, tree op1,
13869a99 12032 tree result_type, enum tree_code resultcode)
12033{
12034 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
12035 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
12036 int unsignedp0, unsignedp1;
48e1416a 12037
13869a99 12038 /* In C++, check for comparison of different enum types. */
12039 if (c_dialect_cxx()
12040 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
12041 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
12042 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
895b662f 12043 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
13869a99 12044 {
8e70fb09 12045 warning_at (location,
12046 OPT_Wsign_compare, "comparison between types %qT and %qT",
12047 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
13869a99 12048 }
12049
12050 /* Do not warn if the comparison is being done in a signed type,
12051 since the signed type will only be chosen if it can represent
12052 all the values of the unsigned type. */
12053 if (!TYPE_UNSIGNED (result_type))
12054 /* OK */;
12055 /* Do not warn if both operands are unsigned. */
12056 else if (op0_signed == op1_signed)
12057 /* OK */;
12058 else
12059 {
895b662f 12060 tree sop, uop, base_type;
13869a99 12061 bool ovf;
895b662f 12062
13869a99 12063 if (op0_signed)
12064 sop = orig_op0, uop = orig_op1;
48e1416a 12065 else
13869a99 12066 sop = orig_op1, uop = orig_op0;
12067
48e1416a 12068 STRIP_TYPE_NOPS (sop);
13869a99 12069 STRIP_TYPE_NOPS (uop);
895b662f 12070 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
12071 ? TREE_TYPE (result_type) : result_type);
13869a99 12072
12073 /* Do not warn if the signed quantity is an unsuffixed integer
12074 literal (or some static constant expression involving such
12075 literals or a conditional expression involving such literals)
12076 and it is non-negative. */
12077 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
12078 /* OK */;
12079 /* Do not warn if the comparison is an equality operation, the
12080 unsigned quantity is an integral constant, and it would fit
12081 in the result if the result were signed. */
12082 else if (TREE_CODE (uop) == INTEGER_CST
12083 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
895b662f 12084 && int_fits_type_p (uop, c_common_signed_type (base_type)))
13869a99 12085 /* OK */;
12086 /* In C, do not warn if the unsigned quantity is an enumeration
12087 constant and its maximum value would fit in the result if the
12088 result were signed. */
12089 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
12090 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
12091 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
895b662f 12092 c_common_signed_type (base_type)))
13869a99 12093 /* OK */;
48e1416a 12094 else
8e70fb09 12095 warning_at (location,
48e1416a 12096 OPT_Wsign_compare,
8e70fb09 12097 "comparison between signed and unsigned integer expressions");
13869a99 12098 }
48e1416a 12099
13869a99 12100 /* Warn if two unsigned values are being compared in a size larger
12101 than their original size, and one (and only one) is the result of
12102 a `~' operator. This comparison will always fail.
48e1416a 12103
13869a99 12104 Also warn if one operand is a constant, and the constant does not
12105 have all bits set that are set in the ~ operand when it is
12106 extended. */
12107
7f506bca 12108 op0 = c_common_get_narrower (op0, &unsignedp0);
12109 op1 = c_common_get_narrower (op1, &unsignedp1);
48e1416a 12110
13869a99 12111 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
12112 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
12113 {
12114 if (TREE_CODE (op0) == BIT_NOT_EXPR)
7f506bca 12115 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
13869a99 12116 if (TREE_CODE (op1) == BIT_NOT_EXPR)
7f506bca 12117 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
13869a99 12118
e913b5cd 12119 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
13869a99 12120 {
12121 tree primop;
12122 HOST_WIDE_INT constant, mask;
12123 int unsignedp;
12124 unsigned int bits;
48e1416a 12125
e913b5cd 12126 if (tree_fits_shwi_p (op0))
13869a99 12127 {
12128 primop = op1;
12129 unsignedp = unsignedp1;
e913b5cd 12130 constant = tree_to_shwi (op0);
13869a99 12131 }
12132 else
12133 {
12134 primop = op0;
12135 unsignedp = unsignedp0;
e913b5cd 12136 constant = tree_to_shwi (op1);
13869a99 12137 }
48e1416a 12138
13869a99 12139 bits = TYPE_PRECISION (TREE_TYPE (primop));
12140 if (bits < TYPE_PRECISION (result_type)
12141 && bits < HOST_BITS_PER_LONG && unsignedp)
12142 {
12143 mask = (~ (HOST_WIDE_INT) 0) << bits;
12144 if ((mask & constant) != mask)
12145 {
12146 if (constant == 0)
76fdceeb 12147 warning_at (location, OPT_Wsign_compare,
12148 "promoted ~unsigned is always non-zero");
13869a99 12149 else
48e1416a 12150 warning_at (location, OPT_Wsign_compare,
8e70fb09 12151 "comparison of promoted ~unsigned with constant");
13869a99 12152 }
12153 }
12154 }
12155 else if (unsignedp0 && unsignedp1
12156 && (TYPE_PRECISION (TREE_TYPE (op0))
12157 < TYPE_PRECISION (result_type))
12158 && (TYPE_PRECISION (TREE_TYPE (op1))
12159 < TYPE_PRECISION (result_type)))
8e70fb09 12160 warning_at (location, OPT_Wsign_compare,
13869a99 12161 "comparison of promoted ~unsigned with unsigned");
12162 }
12163}
12164
5ba33bf4 12165/* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
12166 type via c_common_type. If -Wdouble-promotion is in use, and the
12167 conditions for warning have been met, issue a warning. GMSGID is
12168 the warning message. It must have two %T specifiers for the type
12169 that was converted (generally "float") and the type to which it was
12170 converted (generally "double), respectively. LOC is the location
12171 to which the awrning should refer. */
12172
12173void
12174do_warn_double_promotion (tree result_type, tree type1, tree type2,
12175 const char *gmsgid, location_t loc)
12176{
12177 tree source_type;
12178
12179 if (!warn_double_promotion)
12180 return;
12181 /* If the conversion will not occur at run-time, there is no need to
12182 warn about it. */
12183 if (c_inhibit_evaluation_warnings)
12184 return;
12185 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
12186 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
12187 return;
12188 if (TYPE_MAIN_VARIANT (type1) == float_type_node
12189 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
12190 source_type = type1;
12191 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
12192 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
12193 source_type = type2;
12194 else
12195 return;
12196 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
12197}
12198
0949f227 12199/* Possibly warn about unused parameters. */
12200
12201void
12202do_warn_unused_parameter (tree fn)
12203{
12204 tree decl;
12205
12206 for (decl = DECL_ARGUMENTS (fn);
12207 decl; decl = DECL_CHAIN (decl))
12208 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
12209 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
12210 && !TREE_NO_WARNING (decl))
12211 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
12212 "unused parameter %qD", decl);
12213}
12214
12215
41771881 12216/* Setup a TYPE_DECL node as a typedef representation.
12217
12218 X is a TYPE_DECL for a typedef statement. Create a brand new
12219 ..._TYPE node (which will be just a variant of the existing
12220 ..._TYPE node with identical properties) and then install X
12221 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
12222
12223 The whole point here is to end up with a situation where each
12224 and every ..._TYPE node the compiler creates will be uniquely
12225 associated with AT MOST one node representing a typedef name.
12226 This way, even though the compiler substitutes corresponding
12227 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
12228 early on, later parts of the compiler can always do the reverse
12229 translation and get back the corresponding typedef name. For
12230 example, given:
ab2c1de8 12231
41771881 12232 typedef struct S MY_TYPE;
12233 MY_TYPE object;
12234
12235 Later parts of the compiler might only know that `object' was of
12236 type `struct S' if it were not for code just below. With this
12237 code however, later parts of the compiler see something like:
12238
12239 struct S' == struct S
12240 typedef struct S' MY_TYPE;
12241 struct S' object;
12242
12243 And they can then deduce (from the node for type struct S') that
12244 the original object declaration was:
12245
12246 MY_TYPE object;
12247
12248 Being able to do this is important for proper support of protoize,
12249 and also for generating precise symbolic debugging information
12250 which takes full account of the programmer's (typedef) vocabulary.
12251
12252 Obviously, we don't want to generate a duplicate ..._TYPE node if
12253 the TYPE_DECL node that we are now processing really represents a
12254 standard built-in type. */
12255
12256void
12257set_underlying_type (tree x)
12258{
12259 if (x == error_mark_node)
12260 return;
12261 if (DECL_IS_BUILTIN (x))
12262 {
12263 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12264 TYPE_NAME (TREE_TYPE (x)) = x;
12265 }
12266 else if (TREE_TYPE (x) != error_mark_node
12267 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12268 {
12269 tree tt = TREE_TYPE (x);
12270 DECL_ORIGINAL_TYPE (x) = tt;
12271 tt = build_variant_type_copy (tt);
12272 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12273 TYPE_NAME (tt) = x;
12274 TREE_USED (tt) = TREE_USED (x);
12275 TREE_TYPE (x) = tt;
12276 }
12277}
12278
1a4c44c5 12279/* Record the types used by the current global variable declaration
12280 being parsed, so that we can decide later to emit their debug info.
12281 Those types are in types_used_by_cur_var_decl, and we are going to
12282 store them in the types_used_by_vars_hash hash table.
12283 DECL is the declaration of the global variable that has been parsed. */
12284
12285void
12286record_types_used_by_current_var_decl (tree decl)
12287{
12288 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12289
f1f41a6c 12290 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
1a4c44c5 12291 {
f1f41a6c 12292 tree type = types_used_by_cur_var_decl->pop ();
aef48c9a 12293 types_used_by_var_decl_insert (type, decl);
1a4c44c5 12294 }
12295}
12296
a4e3ffad 12297/* If DECL is a typedef that is declared in the current function,
12298 record it for the purpose of -Wunused-local-typedefs. */
12299
12300void
12301record_locally_defined_typedef (tree decl)
12302{
12303 struct c_language_function *l;
12304
12305 if (!warn_unused_local_typedefs
12306 || cfun == NULL
12307 /* if this is not a locally defined typedef then we are not
12308 interested. */
12309 || !is_typedef_decl (decl)
12310 || !decl_function_context (decl))
12311 return;
12312
12313 l = (struct c_language_function *) cfun->language;
f1f41a6c 12314 vec_safe_push (l->local_typedefs, decl);
a4e3ffad 12315}
12316
12317/* If T is a TYPE_DECL declared locally, mark it as used. */
12318
12319void
12320maybe_record_typedef_use (tree t)
12321{
12322 if (!is_typedef_decl (t))
12323 return;
12324
12325 TREE_USED (t) = true;
12326}
12327
12328/* Warn if there are some unused locally defined typedefs in the
12329 current function. */
12330
12331void
12332maybe_warn_unused_local_typedefs (void)
12333{
12334 int i;
12335 tree decl;
12336 /* The number of times we have emitted -Wunused-local-typedefs
12337 warnings. If this is different from errorcount, that means some
12338 unrelated errors have been issued. In which case, we'll avoid
12339 emitting "unused-local-typedefs" warnings. */
12340 static int unused_local_typedefs_warn_count;
12341 struct c_language_function *l;
12342
12343 if (cfun == NULL)
12344 return;
12345
12346 if ((l = (struct c_language_function *) cfun->language) == NULL)
12347 return;
12348
12349 if (warn_unused_local_typedefs
12350 && errorcount == unused_local_typedefs_warn_count)
12351 {
f1f41a6c 12352 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
a4e3ffad 12353 if (!TREE_USED (decl))
12354 warning_at (DECL_SOURCE_LOCATION (decl),
12355 OPT_Wunused_local_typedefs,
12356 "typedef %qD locally defined but not used", decl);
12357 unused_local_typedefs_warn_count = errorcount;
12358 }
12359
f1f41a6c 12360 vec_free (l->local_typedefs);
a4e3ffad 12361}
12362
78bf4156 12363/* Warn about boolean expression compared with an integer value different
12364 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12365 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12366 are the operands of the comparison. The caller must ensure that
12367 either operand is a boolean expression. */
12368
12369void
12370maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12371 tree op1)
12372{
12373 if (TREE_CODE_CLASS (code) != tcc_comparison)
12374 return;
12375
12376 tree cst = (TREE_CODE (op0) == INTEGER_CST)
12377 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
12378 if (!cst)
12379 return;
12380
12381 if (!integer_zerop (cst) && !integer_onep (cst))
12382 {
a720ab1c 12383 int sign = (TREE_CODE (op0) == INTEGER_CST
12384 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
78bf4156 12385 if (code == EQ_EXPR
12386 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12387 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12388 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12389 "with boolean expression is always false", cst);
12390 else
12391 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12392 "with boolean expression is always true", cst);
12393 }
a720ab1c 12394 else if (integer_zerop (cst) || integer_onep (cst))
12395 {
14744a16 12396 /* If the non-constant operand isn't of a boolean type, we
12397 don't want to warn here. */
12398 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12399 /* Handle booleans promoted to integers. */
12400 if (CONVERT_EXPR_P (noncst)
12401 && TREE_TYPE (noncst) == integer_type_node
12402 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
12403 /* Warn. */;
12404 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12405 && !truth_value_p (TREE_CODE (noncst)))
12406 return;
a720ab1c 12407 /* Do some magic to get the right diagnostics. */
12408 bool flag = TREE_CODE (op0) == INTEGER_CST;
12409 flag = integer_zerop (cst) ? flag : !flag;
12410 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12411 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12412 "with boolean expression is always true", cst);
12413 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12414 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12415 "with boolean expression is always false", cst);
12416 }
78bf4156 12417}
12418
8d669e79 12419/* Warn if signed left shift overflows. We don't warn
12420 about left-shifting 1 into the sign bit in C++14; cf.
12421 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
12422 LOC is a location of the shift; OP0 and OP1 are the operands.
12423 Return true if an overflow is detected, false otherwise. */
12424
12425bool
12426maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
12427{
12428 if (TREE_CODE (op0) != INTEGER_CST
12429 || TREE_CODE (op1) != INTEGER_CST)
12430 return false;
12431
12432 tree type0 = TREE_TYPE (op0);
12433 unsigned int prec0 = TYPE_PRECISION (type0);
12434
12435 /* Left-hand operand must be signed. */
12436 if (TYPE_UNSIGNED (type0))
12437 return false;
12438
12439 /* Handle the left-shifting 1 into the sign bit case. */
12440 if (integer_onep (op0)
12441 && compare_tree_int (op1, prec0 - 1) == 0)
12442 {
12443 /* Never warn for C++14 onwards. */
12444 if (cxx_dialect >= cxx14)
12445 return false;
12446 /* Otherwise only if -Wshift-overflow=2. But return
12447 true to signal an overflow for the sake of integer
12448 constant expressions. */
12449 if (warn_shift_overflow < 2)
12450 return true;
12451 }
12452
12453 unsigned int min_prec = (wi::min_precision (op0, SIGNED)
12454 + TREE_INT_CST_LOW (op1));
12455 bool overflowed = min_prec > prec0;
12456 if (overflowed && c_inhibit_evaluation_warnings == 0)
12457 warning_at (loc, OPT_Wshift_overflow_,
12458 "result of %qE requires %u bits to represent, "
12459 "but %qT only has %u bits",
12460 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
12461 min_prec, type0, prec0);
12462
12463 return overflowed;
12464}
12465
f352a3fb 12466/* The C and C++ parsers both use vectors to hold function arguments.
12467 For efficiency, we keep a cache of unused vectors. This is the
12468 cache. */
12469
f1f41a6c 12470typedef vec<tree, va_gc> *tree_gc_vec;
12471static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
f352a3fb 12472
12473/* Return a new vector from the cache. If the cache is empty,
12474 allocate a new vector. These vectors are GC'ed, so it is OK if the
12475 pointer is not released.. */
12476
f1f41a6c 12477vec<tree, va_gc> *
f352a3fb 12478make_tree_vector (void)
12479{
f1f41a6c 12480 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12481 return tree_vector_cache->pop ();
f352a3fb 12482 else
12483 {
f1f41a6c 12484 /* Passing 0 to vec::alloc returns NULL, and our callers require
f352a3fb 12485 that we always return a non-NULL value. The vector code uses
12486 4 when growing a NULL vector, so we do too. */
f1f41a6c 12487 vec<tree, va_gc> *v;
12488 vec_alloc (v, 4);
12489 return v;
f352a3fb 12490 }
12491}
12492
12493/* Release a vector of trees back to the cache. */
12494
12495void
f1f41a6c 12496release_tree_vector (vec<tree, va_gc> *vec)
f352a3fb 12497{
12498 if (vec != NULL)
12499 {
f1f41a6c 12500 vec->truncate (0);
12501 vec_safe_push (tree_vector_cache, vec);
f352a3fb 12502 }
12503}
12504
12505/* Get a new tree vector holding a single tree. */
12506
f1f41a6c 12507vec<tree, va_gc> *
f352a3fb 12508make_tree_vector_single (tree t)
12509{
f1f41a6c 12510 vec<tree, va_gc> *ret = make_tree_vector ();
12511 ret->quick_push (t);
f352a3fb 12512 return ret;
12513}
12514
c66c81be 12515/* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12516
f1f41a6c 12517vec<tree, va_gc> *
c66c81be 12518make_tree_vector_from_list (tree list)
12519{
f1f41a6c 12520 vec<tree, va_gc> *ret = make_tree_vector ();
c66c81be 12521 for (; list; list = TREE_CHAIN (list))
f1f41a6c 12522 vec_safe_push (ret, TREE_VALUE (list));
c66c81be 12523 return ret;
12524}
12525
f352a3fb 12526/* Get a new tree vector which is a copy of an existing one. */
12527
f1f41a6c 12528vec<tree, va_gc> *
12529make_tree_vector_copy (const vec<tree, va_gc> *orig)
f352a3fb 12530{
f1f41a6c 12531 vec<tree, va_gc> *ret;
f352a3fb 12532 unsigned int ix;
12533 tree t;
12534
12535 ret = make_tree_vector ();
f1f41a6c 12536 vec_safe_reserve (ret, vec_safe_length (orig));
12537 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12538 ret->quick_push (t);
f352a3fb 12539 return ret;
12540}
12541
a9ffdd35 12542/* Return true if KEYWORD starts a type specifier. */
12543
12544bool
12545keyword_begins_type_specifier (enum rid keyword)
12546{
12547 switch (keyword)
12548 {
4fba5eb9 12549 case RID_AUTO_TYPE:
a9ffdd35 12550 case RID_INT:
12551 case RID_CHAR:
12552 case RID_FLOAT:
12553 case RID_DOUBLE:
12554 case RID_VOID:
a9ffdd35 12555 case RID_UNSIGNED:
12556 case RID_LONG:
12557 case RID_SHORT:
12558 case RID_SIGNED:
12559 case RID_DFLOAT32:
12560 case RID_DFLOAT64:
12561 case RID_DFLOAT128:
12562 case RID_FRACT:
12563 case RID_ACCUM:
12564 case RID_BOOL:
12565 case RID_WCHAR:
12566 case RID_CHAR16:
12567 case RID_CHAR32:
12568 case RID_SAT:
12569 case RID_COMPLEX:
12570 case RID_TYPEOF:
12571 case RID_STRUCT:
12572 case RID_CLASS:
12573 case RID_UNION:
12574 case RID_ENUM:
12575 return true;
12576 default:
9f75f026 12577 if (keyword >= RID_FIRST_INT_N
12578 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12579 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12580 return true;
a9ffdd35 12581 return false;
12582 }
12583}
12584
12585/* Return true if KEYWORD names a type qualifier. */
12586
12587bool
12588keyword_is_type_qualifier (enum rid keyword)
12589{
12590 switch (keyword)
12591 {
12592 case RID_CONST:
12593 case RID_VOLATILE:
12594 case RID_RESTRICT:
b560fabd 12595 case RID_ATOMIC:
a9ffdd35 12596 return true;
12597 default:
12598 return false;
12599 }
12600}
12601
12602/* Return true if KEYWORD names a storage class specifier.
12603
12604 RID_TYPEDEF is not included in this list despite `typedef' being
12605 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12606 such for syntactic convenience only. */
12607
12608bool
12609keyword_is_storage_class_specifier (enum rid keyword)
12610{
12611 switch (keyword)
12612 {
12613 case RID_STATIC:
12614 case RID_EXTERN:
12615 case RID_REGISTER:
12616 case RID_AUTO:
12617 case RID_MUTABLE:
12618 case RID_THREAD:
12619 return true;
12620 default:
12621 return false;
12622 }
12623}
12624
fad3f658 12625/* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12626
12627static bool
12628keyword_is_function_specifier (enum rid keyword)
12629{
12630 switch (keyword)
12631 {
12632 case RID_INLINE:
985c6e3a 12633 case RID_NORETURN:
fad3f658 12634 case RID_VIRTUAL:
12635 case RID_EXPLICIT:
12636 return true;
12637 default:
12638 return false;
12639 }
12640}
12641
12642/* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12643 declaration-specifier (C99 6.7). */
12644
12645bool
12646keyword_is_decl_specifier (enum rid keyword)
12647{
12648 if (keyword_is_storage_class_specifier (keyword)
12649 || keyword_is_type_qualifier (keyword)
12650 || keyword_is_function_specifier (keyword))
12651 return true;
12652
12653 switch (keyword)
12654 {
12655 case RID_TYPEDEF:
12656 case RID_FRIEND:
12657 case RID_CONSTEXPR:
12658 return true;
12659 default:
12660 return false;
12661 }
12662}
12663
9b88d08d 12664/* Initialize language-specific-bits of tree_contains_struct. */
12665
12666void
12667c_common_init_ts (void)
12668{
12669 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12670 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
3c6d4197 12671 MARK_TS_TYPED (ARRAY_NOTATION_REF);
9b88d08d 12672}
12673
244db24d 12674/* Build a user-defined numeric literal out of an integer constant type VALUE
12675 with identifier SUFFIX. */
12676
12677tree
324ca377 12678build_userdef_literal (tree suffix_id, tree value,
12679 enum overflow_type overflow, tree num_string)
244db24d 12680{
12681 tree literal = make_node (USERDEF_LITERAL);
12682 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12683 USERDEF_LITERAL_VALUE (literal) = value;
324ca377 12684 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
244db24d 12685 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12686 return literal;
12687}
12688
7059d45d 12689/* For vector[index], convert the vector to a
c61ef207 12690 pointer of the underlying type. Return true if the resulting
12691 ARRAY_REF should not be an lvalue. */
12692
12693bool
7059d45d 12694convert_vector_to_pointer_for_subscript (location_t loc,
c61ef207 12695 tree *vecp, tree index)
7059d45d 12696{
c61ef207 12697 bool ret = false;
6290f0db 12698 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
7059d45d 12699 {
12700 tree type = TREE_TYPE (*vecp);
12701 tree type1;
12702
c61ef207 12703 ret = !lvalue_p (*vecp);
7059d45d 12704 if (TREE_CODE (index) == INTEGER_CST)
e913b5cd 12705 if (!tree_fits_uhwi_p (index)
aa59f000 12706 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7059d45d 12707 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12708
c61ef207 12709 if (ret)
12710 {
f9e245b2 12711 tree tmp = create_tmp_var_raw (type);
c61ef207 12712 DECL_SOURCE_LOCATION (tmp) = loc;
12713 *vecp = c_save_expr (*vecp);
12714 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12715 {
12716 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12717 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12718 *vecp
12719 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12720 *vecp, NULL_TREE, NULL_TREE),
12721 non_const);
12722 }
12723 else
12724 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12725 NULL_TREE, NULL_TREE);
12726 SET_EXPR_LOCATION (*vecp, loc);
12727 c_common_mark_addressable_vec (tmp);
12728 }
12729 else
12730 c_common_mark_addressable_vec (*vecp);
7059d45d 12731 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7059d45d 12732 type1 = build_pointer_type (TREE_TYPE (*vecp));
b2ca6510 12733 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12734 if (!ref_all
12735 && !DECL_P (*vecp))
12736 {
12737 /* If the original vector isn't declared may_alias and it
12738 isn't a bare vector look if the subscripting would
12739 alias the vector we subscript, and if not, force ref-all. */
12740 alias_set_type vecset = get_alias_set (*vecp);
12741 alias_set_type sset = get_alias_set (type);
12742 if (!alias_sets_must_conflict_p (sset, vecset)
12743 && !alias_set_subset_of (sset, vecset))
12744 ref_all = true;
12745 }
12746 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
7059d45d 12747 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12748 *vecp = convert (type, *vecp);
12749 }
c61ef207 12750 return ret;
7059d45d 12751}
12752
41ed701a 12753/* Determine which of the operands, if any, is a scalar that needs to be
12754 converted to a vector, for the range of operations. */
12755enum stv_conv
12756scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12757 bool complain)
12758{
12759 tree type0 = TREE_TYPE (op0);
12760 tree type1 = TREE_TYPE (op1);
12761 bool integer_only_op = false;
12762 enum stv_conv ret = stv_firstarg;
12763
6290f0db 12764 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
41ed701a 12765 switch (code)
12766 {
12767 /* Most GENERIC binary expressions require homogeneous arguments.
12768 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12769 argument that is a vector and a second one that is a scalar, so
12770 we never return stv_secondarg for them. */
12771 case RSHIFT_EXPR:
12772 case LSHIFT_EXPR:
12773 if (TREE_CODE (type0) == INTEGER_TYPE
12774 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12775 {
22a75734 12776 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12777 {
12778 if (complain)
12779 error_at (loc, "conversion of scalar %qT to vector %qT "
12780 "involves truncation", type0, type1);
12781 return stv_error;
12782 }
12783 else
12784 return stv_firstarg;
12785 }
12786 break;
12787
12788 case BIT_IOR_EXPR:
12789 case BIT_XOR_EXPR:
12790 case BIT_AND_EXPR:
12791 integer_only_op = true;
12792 /* ... fall through ... */
12793
7b463b19 12794 case VEC_COND_EXPR:
12795
41ed701a 12796 case PLUS_EXPR:
12797 case MINUS_EXPR:
12798 case MULT_EXPR:
12799 case TRUNC_DIV_EXPR:
12800 case CEIL_DIV_EXPR:
12801 case FLOOR_DIV_EXPR:
12802 case ROUND_DIV_EXPR:
12803 case EXACT_DIV_EXPR:
12804 case TRUNC_MOD_EXPR:
12805 case FLOOR_MOD_EXPR:
12806 case RDIV_EXPR:
12807 case EQ_EXPR:
12808 case NE_EXPR:
12809 case LE_EXPR:
12810 case GE_EXPR:
12811 case LT_EXPR:
12812 case GT_EXPR:
12813 /* What about UNLT_EXPR? */
6290f0db 12814 if (VECTOR_TYPE_P (type0))
41ed701a 12815 {
41ed701a 12816 ret = stv_secondarg;
a4f59596 12817 std::swap (type0, type1);
12818 std::swap (op0, op1);
41ed701a 12819 }
12820
12821 if (TREE_CODE (type0) == INTEGER_TYPE
12822 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12823 {
22a75734 12824 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12825 {
12826 if (complain)
12827 error_at (loc, "conversion of scalar %qT to vector %qT "
12828 "involves truncation", type0, type1);
12829 return stv_error;
12830 }
12831 return ret;
12832 }
12833 else if (!integer_only_op
12834 /* Allow integer --> real conversion if safe. */
12835 && (TREE_CODE (type0) == REAL_TYPE
12836 || TREE_CODE (type0) == INTEGER_TYPE)
12837 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12838 {
22a75734 12839 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12840 {
12841 if (complain)
12842 error_at (loc, "conversion of scalar %qT to vector %qT "
12843 "involves truncation", type0, type1);
12844 return stv_error;
12845 }
12846 return ret;
12847 }
12848 default:
12849 break;
12850 }
12851
12852 return stv_nothing;
12853}
12854
ffcdbf9c 12855/* Return true iff ALIGN is an integral constant that is a fundamental
12856 alignment, as defined by [basic.align] in the c++-11
12857 specifications.
12858
12859 That is:
12860
12861 [A fundamental alignment is represented by an alignment less than or
12862 equal to the greatest alignment supported by the implementation
12863 in all contexts, which is equal to
12864 alignof(max_align_t)]. */
12865
12866bool
12867cxx_fundamental_alignment_p (unsigned align)
12868{
12869 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12870 TYPE_ALIGN (long_double_type_node)));
12871}
12872
46da3601 12873/* Return true if T is a pointer to a zero-sized aggregate. */
12874
12875bool
12876pointer_to_zero_sized_aggr_p (tree t)
12877{
12878 if (!POINTER_TYPE_P (t))
12879 return false;
12880 t = TREE_TYPE (t);
12881 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12882}
12883
7bedc3a0 12884#include "gt-c-family-c-common.h"