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