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