]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-common.c
2015-06-17 Andrew MacLeod <amacleod@redhat.com>
[thirdparty/gcc.git] / gcc / c-family / c-common.c
CommitLineData
b0fc3e72 1/* Subroutines shared by all languages that are variants of C.
d353bf18 2 Copyright (C) 1992-2015 Free Software Foundation, Inc.
b0fc3e72 3
f12b58b3 4This file is part of GCC.
b0fc3e72 5
f12b58b3 6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8c4c00c1 8Software Foundation; either version 3, or (at your option) any later
f12b58b3 9version.
b0fc3e72 10
f12b58b3 11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
b0fc3e72 15
16You should have received a copy of the GNU General Public License
8c4c00c1 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
b0fc3e72 19
b20a8bb4 20#define GCC_C_COMMON_C
21
b0fc3e72 22#include "config.h"
405711de 23#include "system.h"
805e22b2 24#include "coretypes.h"
7ff8db31 25#include "c-common.h"
805e22b2 26#include "tm.h"
e48d0f41 27#include "intl.h"
b0fc3e72 28#include "tree.h"
9ed99284 29#include "fold-const.h"
30#include "stor-layout.h"
31#include "calls.h"
32#include "stringpool.h"
33#include "attribs.h"
34#include "varasm.h"
35#include "trans-mem.h"
b0fc3e72 36#include "flags.h"
a3fa7feb 37#include "c-pragma.h"
6c536c4f 38#include "c-objc.h"
d8c9779c 39#include "tm_p.h"
4e91a871 40#include "obstack.h"
a654e028 41#include "cpplib.h"
8ee295a7 42#include "target.h"
218e3e4e 43#include "common/common-target.h"
96554925 44#include "langhooks.h"
f3dde807 45#include "tree-inline.h"
69579044 46#include "toplev.h"
7f5f3953 47#include "diagnostic.h"
4ee9c684 48#include "tree-iterator.h"
e08bd2f4 49#include "opts.h"
1140c305 50#include "plugin-api.h"
1140c305 51#include "hard-reg-set.h"
1140c305 52#include "function.h"
53#include "ipa-ref.h"
62eec3b4 54#include "cgraph.h"
b9fc964a 55#include "target-def.h"
a8783bee 56#include "gimplify.h"
e913b5cd 57#include "wide-int-print.h"
c61ef207 58#include "gimple-expr.h"
fd6f6435 59
90cc7820 60cpp_reader *parse_in; /* Declared in c-pragma.h. */
a654e028 61
de801c28 62/* Mode used to build pointers (VOIDmode means ptr_mode). */
63
64machine_mode c_default_pointer_mode = VOIDmode;
65
72040e7e 66/* The following symbols are subsumed in the c_global_trees array, and
44e9fa65 67 listed here individually for documentation purposes.
72040e7e 68
69 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
70
71 tree short_integer_type_node;
72 tree long_integer_type_node;
73 tree long_long_integer_type_node;
74
75 tree short_unsigned_type_node;
76 tree long_unsigned_type_node;
77 tree long_long_unsigned_type_node;
78
3c2239cf 79 tree truthvalue_type_node;
80 tree truthvalue_false_node;
81 tree truthvalue_true_node;
72040e7e 82
83 tree ptrdiff_type_node;
84
85 tree unsigned_char_type_node;
86 tree signed_char_type_node;
87 tree wchar_type_node;
72040e7e 88
924bbf02 89 tree char16_type_node;
90 tree char32_type_node;
91
72040e7e 92 tree float_type_node;
93 tree double_type_node;
94 tree long_double_type_node;
95
96 tree complex_integer_type_node;
97 tree complex_float_type_node;
98 tree complex_double_type_node;
99 tree complex_long_double_type_node;
100
c4503c0a 101 tree dfloat32_type_node;
102 tree dfloat64_type_node;
103 tree_dfloat128_type_node;
104
72040e7e 105 tree intQI_type_node;
106 tree intHI_type_node;
107 tree intSI_type_node;
108 tree intDI_type_node;
109 tree intTI_type_node;
110
111 tree unsigned_intQI_type_node;
112 tree unsigned_intHI_type_node;
113 tree unsigned_intSI_type_node;
114 tree unsigned_intDI_type_node;
115 tree unsigned_intTI_type_node;
116
117 tree widest_integer_literal_type_node;
118 tree widest_unsigned_literal_type_node;
119
120 Nodes for types `void *' and `const void *'.
121
122 tree ptr_type_node, const_ptr_type_node;
123
124 Nodes for types `char *' and `const char *'.
125
126 tree string_type_node, const_string_type_node;
127
128 Type `char[SOMENUMBER]'.
129 Used when an array of char is needed and the size is irrelevant.
130
131 tree char_array_type_node;
132
72040e7e 133 Type `wchar_t[SOMENUMBER]' or something like it.
134 Used when a wide string literal is created.
135
136 tree wchar_array_type_node;
137
924bbf02 138 Type `char16_t[SOMENUMBER]' or something like it.
139 Used when a UTF-16 string literal is created.
140
141 tree char16_array_type_node;
142
143 Type `char32_t[SOMENUMBER]' or something like it.
144 Used when a UTF-32 string literal is created.
145
146 tree char32_array_type_node;
147
72040e7e 148 Type `int ()' -- used for implicit declaration of functions.
149
150 tree default_function_type;
151
72040e7e 152 A VOID_TYPE node, packaged in a TREE_LIST.
153
154 tree void_list_node;
155
734c98be 156 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
65b7f83f 157 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
158 VAR_DECLS, but C++ does.)
71d9fc9b 159
65b7f83f 160 tree function_name_decl_node;
734c98be 161 tree pretty_function_name_decl_node;
65b7f83f 162 tree c99_function_name_decl_node;
163
164 Stack of nested function name VAR_DECLs.
1cae46be 165
65b7f83f 166 tree saved_function_name_decls;
71d9fc9b 167
72040e7e 168*/
169
170tree c_global_trees[CTI_MAX];
573aba85 171\f
574a6990 172/* Switches common to the C front ends. */
173
f7070933 174/* Nonzero means don't output line number information. */
175
176char flag_no_line_commands;
177
178/* Nonzero causes -E output not to be done, but directives such as
179 #define that have side effects are still obeyed. */
180
181char flag_no_output;
182
183/* Nonzero means dump macros in some fashion. */
184
185char flag_dump_macros;
186
187/* Nonzero means pass #include lines through to the output. */
188
189char flag_dump_includes;
190
d718b525 191/* Nonzero means process PCH files while preprocessing. */
192
193bool flag_pch_preprocess;
194
573aba85 195/* The file name to which we should write a precompiled header, or
196 NULL if no header will be written in this compile. */
197
198const char *pch_file;
199
1ed9d5f5 200/* Nonzero if an ISO standard was selected. It rejects macros in the
201 user's namespace. */
202int flag_iso;
203
574a6990 204/* C/ObjC language option variables. */
205
206
574a6990 207/* Nonzero means allow type mismatches in conditional expressions;
208 just make their values `void'. */
209
210int flag_cond_mismatch;
211
212/* Nonzero means enable C89 Amendment 1 features. */
213
214int flag_isoc94;
215
32074525 216/* Nonzero means use the ISO C99 (or C11) dialect of C. */
574a6990 217
218int flag_isoc99;
219
32074525 220/* Nonzero means use the ISO C11 dialect of C. */
39012afb 221
32074525 222int flag_isoc11;
39012afb 223
8b332087 224/* Nonzero means that we have builtin functions, and main is an int. */
574a6990 225
226int flag_hosted = 1;
227
574a6990 228
229/* ObjC language option variables. */
230
231
574a6990 232/* Tells the compiler that this is a special run. Do not perform any
233 compiling, instead we are to test some platform dependent features
234 and output a C header file with appropriate definitions. */
235
236int print_struct_values;
237
f0b5f617 238/* Tells the compiler what is the constant string class for ObjC. */
574a6990 239
240const char *constant_string_class_name;
241
574a6990 242
243/* C++ language option variables. */
244
245
574a6990 246/* Nonzero means generate separate instantiation control files and
247 juggle them at link time. */
248
249int flag_use_repository;
250
d875b9d2 251/* The C++ dialect being used. Default set in c_common_post_options. */
0fe6eeac 252
d875b9d2 253enum cxx_dialect cxx_dialect = cxx_unset;
0fe6eeac 254
9dcd0d49 255/* Maximum template instantiation depth. This limit exists to limit the
8ce59854 256 time it takes to notice excessively recursive template instantiations.
574a6990 257
8ce59854 258 The default is lower than the 1024 recommended by the C++0x standard
259 because G++ runs out of stack before 1024 with highly recursive template
260 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
261
262int max_tinst_depth = 900;
574a6990 263
988fc1d1 264/* The elements of `ridpointers' are identifier nodes for the reserved
265 type names and storage classes. It is indexed by a RID_... value. */
266tree *ridpointers;
267
e60a6f7b 268tree (*make_fname_decl) (location_t, tree, int);
9e5a737d 269
48d94ede 270/* Nonzero means don't warn about problems that occur when the code is
271 executed. */
272int c_inhibit_evaluation_warnings;
e78703c1 273
93be21c0 274/* Whether we are building a boolean conversion inside
275 convert_for_assignment, or some other late binary operation. If
276 build_binary_op is called for C (from code shared by C and C++) in
277 this case, then the operands have already been folded and the
278 result will not be folded again, so C_MAYBE_CONST_EXPR should not
279 be generated. */
280bool in_late_binary_op;
281
7f5f3953 282/* Whether lexing has been completed, so subsequent preprocessor
283 errors should use the compiler's input_location. */
284bool done_lexing = false;
285
2c0e001b 286/* Information about how a function name is generated. */
65b7f83f 287struct fname_var_t
288{
e99c3a1d 289 tree *const decl; /* pointer to the VAR_DECL. */
290 const unsigned rid; /* RID number for the identifier. */
291 const int pretty; /* How pretty is it? */
65b7f83f 292};
293
2c0e001b 294/* The three ways of getting then name of the current function. */
65b7f83f 295
296const struct fname_var_t fname_vars[] =
297{
2c0e001b 298 /* C99 compliant __func__, must be first. */
65b7f83f 299 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
2c0e001b 300 /* GCC __FUNCTION__ compliant. */
65b7f83f 301 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
2c0e001b 302 /* GCC __PRETTY_FUNCTION__ compliant. */
65b7f83f 303 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
304 {NULL, 0, 0},
305};
306
79396169 307/* Global visibility options. */
308struct visibility_flags visibility_options;
309
aac24642 310static tree c_fully_fold_internal (tree expr, bool, bool *, bool *, bool);
2d2f6a15 311static tree check_case_value (location_t, tree);
f61a9bc2 312static bool check_case_bounds (location_t, tree, tree, tree *, tree *);
be43ff5a 313
1cae46be 314static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
315static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
316static tree handle_common_attribute (tree *, tree, tree, int, bool *);
317static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
5de92639 318static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
319static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
a9196da9 320static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
321 int, bool *);
d413ffdd 322static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
323 int, bool *);
05f893e1 324static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
325 bool *);
947aa916 326static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
1cae46be 327static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
bdb1f0d1 328static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
85fbea97 329static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
7bd95dfd 330static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
1cae46be 331static tree handle_always_inline_attribute (tree *, tree, tree, int,
332 bool *);
1b16fc45 333static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
334static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
0cdd9887 335static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
10fc867f 336static tree handle_error_attribute (tree *, tree, tree, int, bool *);
1cae46be 337static tree handle_used_attribute (tree *, tree, tree, int, bool *);
338static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
62eec3b4 339static tree handle_externally_visible_attribute (tree *, tree, tree, int,
340 bool *);
6b722052 341static tree handle_no_reorder_attribute (tree *, tree, tree, int,
342 bool *);
1cae46be 343static tree handle_const_attribute (tree *, tree, tree, int, bool *);
344static tree handle_transparent_union_attribute (tree *, tree, tree,
345 int, bool *);
346static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
347static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
348static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
349static tree handle_section_attribute (tree *, tree, tree, int, bool *);
350static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
351static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
4641882f 352static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
85c0a25c 353static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
354static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
1cae46be 355static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
f4a30bd7 356static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
1cae46be 357static tree handle_visibility_attribute (tree *, tree, tree, int,
358 bool *);
359static tree handle_tls_model_attribute (tree *, tree, tree, int,
360 bool *);
361static tree handle_no_instrument_function_attribute (tree *, tree,
362 tree, int, bool *);
363static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
26d1c5ff 364static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
1cae46be 365static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
366 bool *);
367static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
4c0315d0 368static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
369static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
fc09b200 370static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
1cae46be 371static tree handle_deprecated_attribute (tree *, tree, tree, int,
372 bool *);
373static tree handle_vector_size_attribute (tree *, tree, tree, int,
374 bool *);
375static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
376static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
377static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
8a8cdb8d 378static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
379 bool *);
bf6c8de0 380static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
b5c26b42 381static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
4a29c97c 382static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
237e78b1 383static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
384static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
24470055 385static tree handle_target_attribute (tree *, tree, tree, int, bool *);
46f8e3b0 386static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
4c0315d0 387static tree ignore_attribute (tree *, tree, tree, int, bool *);
48b14f50 388static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
8ce86007 389static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
a96c3cc1 390static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
d7dcba40 391static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
bc7bff74 392static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
393 bool *);
394static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
395 bool *);
74691f46 396static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
058a1b7a 397static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
398static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
399static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
1cae46be 400
d01f58f9 401static void check_function_nonnull (tree, int, tree *);
1cae46be 402static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
403static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
404static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
860251be 405static int resort_field_decl_cmp (const void *, const void *);
dbf6c367 406
5c6e5756 407/* Reserved words. The third field is a mask: keywords are disabled
408 if they match the mask.
409
410 Masks for languages:
411 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
412 C --std=c99: D_CXXONLY | D_OBJC
413 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
414 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
415 C++ --std=c0x: D_CONLY | D_OBJC
416 ObjC++ is like C++ except that D_OBJC is not set
417
418 If -fno-asm is used, D_ASM is added to the mask. If
419 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
420 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
b27e241e 421 In C with -Wc++-compat, we warn if D_CXXWARN is set.
5c6e5756 422
b27e241e 423 Note the complication of the D_CXX_OBJC keywords. These are
424 reserved words such as 'class'. In C++, 'class' is a reserved
425 word. In Objective-C++ it is too. In Objective-C, it is a
426 reserved word too, but only if it follows an '@' sign.
427*/
5c6e5756 428const struct c_common_resword c_common_reswords[] =
429{
83e25171 430 { "_Alignas", RID_ALIGNAS, D_CONLY },
431 { "_Alignof", RID_ALIGNOF, D_CONLY },
b560fabd 432 { "_Atomic", RID_ATOMIC, D_CONLY },
5c6e5756 433 { "_Bool", RID_BOOL, D_CONLY },
434 { "_Complex", RID_COMPLEX, 0 },
d037099f 435 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
436 { "_Cilk_sync", RID_CILK_SYNC, 0 },
40750995 437 { "_Cilk_for", RID_CILK_FOR, 0 },
c1800156 438 { "_Imaginary", RID_IMAGINARY, D_CONLY },
5c6e5756 439 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
440 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
441 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
442 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
443 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
444 { "_Sat", RID_SAT, D_CONLY | D_EXT },
f80e7755 445 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
985c6e3a 446 { "_Noreturn", RID_NORETURN, D_CONLY },
7aa04c8d 447 { "_Generic", RID_GENERIC, D_CONLY },
d184e0c0 448 { "_Thread_local", RID_THREAD, D_CONLY },
5c6e5756 449 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
450 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
451 { "__alignof", RID_ALIGNOF, 0 },
452 { "__alignof__", RID_ALIGNOF, 0 },
453 { "__asm", RID_ASM, 0 },
454 { "__asm__", RID_ASM, 0 },
455 { "__attribute", RID_ATTRIBUTE, 0 },
456 { "__attribute__", RID_ATTRIBUTE, 0 },
4fba5eb9 457 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
e6014a82 458 { "__bases", RID_BASES, D_CXXONLY },
6f58cf06 459 { "__builtin_call_with_static_chain",
460 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
5c6e5756 461 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
bff4ad11 462 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
bf0cb017 463 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
5c6e5756 464 { "__builtin_offsetof", RID_OFFSETOF, 0 },
465 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
466 { "__builtin_va_arg", RID_VA_ARG, 0 },
467 { "__complex", RID_COMPLEX, 0 },
468 { "__complex__", RID_COMPLEX, 0 },
469 { "__const", RID_CONST, 0 },
470 { "__const__", RID_CONST, 0 },
471 { "__decltype", RID_DECLTYPE, D_CXXONLY },
e6014a82 472 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
5c6e5756 473 { "__extension__", RID_EXTENSION, 0 },
474 { "__func__", RID_C99_FUNCTION_NAME, 0 },
475 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
476 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
477 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
478 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
479 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
480 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
481 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
482 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
23407dc9 483 { "__imag", RID_IMAGPART, 0 },
484 { "__imag__", RID_IMAGPART, 0 },
485 { "__inline", RID_INLINE, 0 },
486 { "__inline__", RID_INLINE, 0 },
5c6e5756 487 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
488 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
489 { "__is_class", RID_IS_CLASS, D_CXXONLY },
5c6e5756 490 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
491 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
aa4313eb 492 { "__is_final", RID_IS_FINAL, D_CXXONLY },
23407dc9 493 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
5c6e5756 494 { "__is_pod", RID_IS_POD, D_CXXONLY },
495 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
c1c67b4f 496 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
497 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
f76a9aa8 498 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
499 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
717e52f9 500 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
5c6e5756 501 { "__is_union", RID_IS_UNION, D_CXXONLY },
5c6e5756 502 { "__label__", RID_LABEL, 0 },
503 { "__null", RID_NULL, 0 },
504 { "__real", RID_REALPART, 0 },
505 { "__real__", RID_REALPART, 0 },
506 { "__restrict", RID_RESTRICT, 0 },
507 { "__restrict__", RID_RESTRICT, 0 },
508 { "__signed", RID_SIGNED, 0 },
509 { "__signed__", RID_SIGNED, 0 },
510 { "__thread", RID_THREAD, 0 },
4c0315d0 511 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
512 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
513 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
5c6e5756 514 { "__typeof", RID_TYPEOF, 0 },
515 { "__typeof__", RID_TYPEOF, 0 },
23407dc9 516 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
5c6e5756 517 { "__volatile", RID_VOLATILE, 0 },
518 { "__volatile__", RID_VOLATILE, 0 },
ffcdbf9c 519 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
e463efd7 520 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
5c6e5756 521 { "asm", RID_ASM, D_ASM },
522 { "auto", RID_AUTO, 0 },
dbd982c9 523 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
5c6e5756 524 { "break", RID_BREAK, 0 },
525 { "case", RID_CASE, 0 },
51030405 526 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
5c6e5756 527 { "char", RID_CHAR, 0 },
51030405 528 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
529 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
530 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
5c6e5756 531 { "const", RID_CONST, 0 },
17814aca 532 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
5c6e5756 533 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
534 { "continue", RID_CONTINUE, 0 },
51030405 535 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
5c6e5756 536 { "default", RID_DEFAULT, 0 },
51030405 537 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
5c6e5756 538 { "do", RID_DO, 0 },
539 { "double", RID_DOUBLE, 0 },
540 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
541 { "else", RID_ELSE, 0 },
542 { "enum", RID_ENUM, 0 },
51030405 543 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
544 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
5c6e5756 545 { "extern", RID_EXTERN, 0 },
51030405 546 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
5c6e5756 547 { "float", RID_FLOAT, 0 },
548 { "for", RID_FOR, 0 },
51030405 549 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
5c6e5756 550 { "goto", RID_GOTO, 0 },
551 { "if", RID_IF, 0 },
552 { "inline", RID_INLINE, D_EXT89 },
553 { "int", RID_INT, 0 },
554 { "long", RID_LONG, 0 },
555 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
51030405 556 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
557 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
98fe9664 558 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
6fe11077 559 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
51030405 560 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
561 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
562 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
563 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
5c6e5756 564 { "register", RID_REGISTER, 0 },
565 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
566 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
567 { "return", RID_RETURN, 0 },
568 { "short", RID_SHORT, 0 },
569 { "signed", RID_SIGNED, 0 },
570 { "sizeof", RID_SIZEOF, 0 },
571 { "static", RID_STATIC, 0 },
572 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
573 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
574 { "struct", RID_STRUCT, 0 },
575 { "switch", RID_SWITCH, 0 },
51030405 576 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
577 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
3740094c 578 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
51030405 579 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
580 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
581 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
5c6e5756 582 { "typedef", RID_TYPEDEF, 0 },
51030405 583 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
584 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
5c6e5756 585 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
586 { "union", RID_UNION, 0 },
587 { "unsigned", RID_UNSIGNED, 0 },
51030405 588 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
589 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
5c6e5756 590 { "void", RID_VOID, 0 },
591 { "volatile", RID_VOLATILE, 0 },
592 { "wchar_t", RID_WCHAR, D_CXXONLY },
593 { "while", RID_WHILE, 0 },
594 /* These Objective-C keywords are recognized only immediately after
595 an '@'. */
596 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
597 { "defs", RID_AT_DEFS, D_OBJC },
598 { "encode", RID_AT_ENCODE, D_OBJC },
599 { "end", RID_AT_END, D_OBJC },
600 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
601 { "interface", RID_AT_INTERFACE, D_OBJC },
602 { "protocol", RID_AT_PROTOCOL, D_OBJC },
603 { "selector", RID_AT_SELECTOR, D_OBJC },
604 { "finally", RID_AT_FINALLY, D_OBJC },
605 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
069761fb 606 { "optional", RID_AT_OPTIONAL, D_OBJC },
607 { "required", RID_AT_REQUIRED, D_OBJC },
86c110ac 608 { "property", RID_AT_PROPERTY, D_OBJC },
4a8875ed 609 { "package", RID_AT_PACKAGE, D_OBJC },
e1f293c0 610 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
611 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
5c6e5756 612 /* These are recognized only in protocol-qualifier context
613 (see above) */
614 { "bycopy", RID_BYCOPY, D_OBJC },
615 { "byref", RID_BYREF, D_OBJC },
616 { "in", RID_IN, D_OBJC },
617 { "inout", RID_INOUT, D_OBJC },
618 { "oneway", RID_ONEWAY, D_OBJC },
619 { "out", RID_OUT, D_OBJC },
86c110ac 620 /* These are recognized inside a property attribute list */
7590f0e5 621 { "assign", RID_ASSIGN, D_OBJC },
622 { "copy", RID_COPY, D_OBJC },
86c110ac 623 { "getter", RID_GETTER, D_OBJC },
7590f0e5 624 { "nonatomic", RID_NONATOMIC, D_OBJC },
625 { "readonly", RID_READONLY, D_OBJC },
626 { "readwrite", RID_READWRITE, D_OBJC },
627 { "retain", RID_RETAIN, D_OBJC },
86c110ac 628 { "setter", RID_SETTER, D_OBJC },
5c6e5756 629};
630
631const unsigned int num_c_common_reswords =
632 sizeof c_common_reswords / sizeof (struct c_common_resword);
633
058a1b7a 634/* Table of machine-independent attributes common to all C-like languages.
635
636 All attributes referencing arguments should be additionally processed
637 in chkp_copy_function_type_adding_bounds for correct instrumentation
638 by Pointer Bounds Checker.
639 Current list of processed common attributes: nonnull. */
f8e93a2e 640const struct attribute_spec c_common_attribute_table[] =
641{
ac86af5d 642 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
643 affects_type_identity } */
f8e93a2e 644 { "packed", 0, 0, false, false, false,
ac86af5d 645 handle_packed_attribute , false},
f8e93a2e 646 { "nocommon", 0, 0, true, false, false,
ac86af5d 647 handle_nocommon_attribute, false},
f8e93a2e 648 { "common", 0, 0, true, false, false,
ac86af5d 649 handle_common_attribute, false },
f8e93a2e 650 /* FIXME: logically, noreturn attributes should be listed as
651 "false, true, true" and apply to function types. But implementing this
652 would require all the places in the compiler that use TREE_THIS_VOLATILE
653 on a decl to identify non-returning functions to be located and fixed
654 to check the function type instead. */
655 { "noreturn", 0, 0, true, false, false,
ac86af5d 656 handle_noreturn_attribute, false },
f8e93a2e 657 { "volatile", 0, 0, true, false, false,
ac86af5d 658 handle_noreturn_attribute, false },
947aa916 659 { "stack_protect", 0, 0, true, false, false,
660 handle_stack_protect_attribute, false },
f8e93a2e 661 { "noinline", 0, 0, true, false, false,
ac86af5d 662 handle_noinline_attribute, false },
bdb1f0d1 663 { "noclone", 0, 0, true, false, false,
ac86af5d 664 handle_noclone_attribute, false },
85fbea97 665 { "no_icf", 0, 0, true, false, false,
666 handle_noicf_attribute, false },
7bd95dfd 667 { "leaf", 0, 0, true, false, false,
ac86af5d 668 handle_leaf_attribute, false },
f8e93a2e 669 { "always_inline", 0, 0, true, false, false,
ac86af5d 670 handle_always_inline_attribute, false },
541e4101 671 { "gnu_inline", 0, 0, true, false, false,
ac86af5d 672 handle_gnu_inline_attribute, false },
1b16fc45 673 { "artificial", 0, 0, true, false, false,
ac86af5d 674 handle_artificial_attribute, false },
0cdd9887 675 { "flatten", 0, 0, true, false, false,
ac86af5d 676 handle_flatten_attribute, false },
f8e93a2e 677 { "used", 0, 0, true, false, false,
ac86af5d 678 handle_used_attribute, false },
f8e93a2e 679 { "unused", 0, 0, false, false, false,
ac86af5d 680 handle_unused_attribute, false },
62eec3b4 681 { "externally_visible", 0, 0, true, false, false,
ac86af5d 682 handle_externally_visible_attribute, false },
6b722052 683 { "no_reorder", 0, 0, true, false, false,
684 handle_no_reorder_attribute, false },
f8e93a2e 685 /* The same comments as for noreturn attributes apply to const ones. */
686 { "const", 0, 0, true, false, false,
ac86af5d 687 handle_const_attribute, false },
f8e93a2e 688 { "transparent_union", 0, 0, false, false, false,
ac86af5d 689 handle_transparent_union_attribute, false },
9af7fd5b 690 { "constructor", 0, 1, true, false, false,
ac86af5d 691 handle_constructor_attribute, false },
9af7fd5b 692 { "destructor", 0, 1, true, false, false,
ac86af5d 693 handle_destructor_attribute, false },
f8e93a2e 694 { "mode", 1, 1, false, true, false,
ac86af5d 695 handle_mode_attribute, false },
f8e93a2e 696 { "section", 1, 1, true, false, false,
ac86af5d 697 handle_section_attribute, false },
f8e93a2e 698 { "aligned", 0, 1, false, false, false,
ac86af5d 699 handle_aligned_attribute, false },
f8e93a2e 700 { "weak", 0, 0, true, false, false,
ac86af5d 701 handle_weak_attribute, false },
4641882f 702 { "noplt", 0, 0, true, false, false,
703 handle_noplt_attribute, false },
85c0a25c 704 { "ifunc", 1, 1, true, false, false,
ac86af5d 705 handle_ifunc_attribute, false },
f8e93a2e 706 { "alias", 1, 1, true, false, false,
ac86af5d 707 handle_alias_attribute, false },
f4a30bd7 708 { "weakref", 0, 1, true, false, false,
ac86af5d 709 handle_weakref_attribute, false },
f8e93a2e 710 { "no_instrument_function", 0, 0, true, false, false,
ac86af5d 711 handle_no_instrument_function_attribute,
712 false },
f8e93a2e 713 { "malloc", 0, 0, true, false, false,
ac86af5d 714 handle_malloc_attribute, false },
26d1c5ff 715 { "returns_twice", 0, 0, true, false, false,
ac86af5d 716 handle_returns_twice_attribute, false },
f8e93a2e 717 { "no_stack_limit", 0, 0, true, false, false,
ac86af5d 718 handle_no_limit_stack_attribute, false },
f8e93a2e 719 { "pure", 0, 0, true, false, false,
ac86af5d 720 handle_pure_attribute, false },
4c0315d0 721 { "transaction_callable", 0, 0, false, true, false,
722 handle_tm_attribute, false },
723 { "transaction_unsafe", 0, 0, false, true, false,
724 handle_tm_attribute, false },
725 { "transaction_safe", 0, 0, false, true, false,
726 handle_tm_attribute, false },
727 { "transaction_may_cancel_outer", 0, 0, false, true, false,
728 handle_tm_attribute, false },
729 /* ??? These two attributes didn't make the transition from the
730 Intel language document to the multi-vendor language document. */
731 { "transaction_pure", 0, 0, false, true, false,
732 handle_tm_attribute, false },
733 { "transaction_wrap", 1, 1, true, false, false,
734 handle_tm_wrap_attribute, false },
fc09b200 735 /* For internal use (marking of builtins) only. The name contains space
736 to prevent its usage in source code. */
737 { "no vops", 0, 0, true, false, false,
ac86af5d 738 handle_novops_attribute, false },
45c4e798 739 { "deprecated", 0, 1, false, false, false,
ac86af5d 740 handle_deprecated_attribute, false },
f8e93a2e 741 { "vector_size", 1, 1, false, true, false,
ac86af5d 742 handle_vector_size_attribute, false },
b212f378 743 { "visibility", 1, 1, false, false, false,
ac86af5d 744 handle_visibility_attribute, false },
24dfead4 745 { "tls_model", 1, 1, true, false, false,
ac86af5d 746 handle_tls_model_attribute, false },
dbf6c367 747 { "nonnull", 0, -1, false, true, true,
ac86af5d 748 handle_nonnull_attribute, false },
fa987697 749 { "nothrow", 0, 0, true, false, false,
ac86af5d 750 handle_nothrow_attribute, false },
751 { "may_alias", 0, 0, false, true, false, NULL, false },
7acb29a3 752 { "cleanup", 1, 1, true, false, false,
ac86af5d 753 handle_cleanup_attribute, false },
8a8cdb8d 754 { "warn_unused_result", 0, 0, false, true, true,
ac86af5d 755 handle_warn_unused_result_attribute, false },
50ca527f 756 { "sentinel", 0, 1, false, true, true,
ac86af5d 757 handle_sentinel_attribute, false },
b5c26b42 758 /* For internal use (marking of builtins) only. The name contains space
759 to prevent its usage in source code. */
760 { "type generic", 0, 0, false, true, true,
ac86af5d 761 handle_type_generic_attribute, false },
4a29c97c 762 { "alloc_size", 1, 2, false, true, true,
ac86af5d 763 handle_alloc_size_attribute, false },
5de92639 764 { "cold", 0, 0, true, false, false,
ac86af5d 765 handle_cold_attribute, false },
5de92639 766 { "hot", 0, 0, true, false, false,
ac86af5d 767 handle_hot_attribute, false },
d413ffdd 768 { "no_address_safety_analysis",
769 0, 0, true, false, false,
770 handle_no_address_safety_analysis_attribute,
771 false },
a9196da9 772 { "no_sanitize_address", 0, 0, true, false, false,
773 handle_no_sanitize_address_attribute,
774 false },
d1e96383 775 { "no_sanitize_thread", 0, 0, true, false, false,
776 handle_no_sanitize_address_attribute,
777 false },
05f893e1 778 { "no_sanitize_undefined", 0, 0, true, false, false,
779 handle_no_sanitize_undefined_attribute,
780 false },
10fc867f 781 { "warning", 1, 1, true, false, false,
ac86af5d 782 handle_error_attribute, false },
10fc867f 783 { "error", 1, 1, true, false, false,
ac86af5d 784 handle_error_attribute, false },
24470055 785 { "target", 1, -1, true, false, false,
ac86af5d 786 handle_target_attribute, false },
46f8e3b0 787 { "optimize", 1, -1, true, false, false,
ac86af5d 788 handle_optimize_attribute, false },
4c0315d0 789 /* For internal use only. The leading '*' both prevents its usage in
790 source code and signals that it may be overridden by machine tables. */
791 { "*tm regparm", 0, 0, false, true, true,
792 ignore_attribute, false },
48b14f50 793 { "no_split_stack", 0, 0, true, false, false,
ac86af5d 794 handle_no_split_stack_attribute, false },
8ce86007 795 /* For internal use (marking of builtins and runtime functions) only.
796 The name contains space to prevent its usage in source code. */
797 { "fn spec", 1, 1, false, true, true,
ac86af5d 798 handle_fnspec_attribute, false },
a96c3cc1 799 { "warn_unused", 0, 0, false, false, false,
800 handle_warn_unused_attribute, false },
d7dcba40 801 { "returns_nonnull", 0, 0, false, true, true,
802 handle_returns_nonnull_attribute, false },
bc7bff74 803 { "omp declare simd", 0, -1, true, false, false,
804 handle_omp_declare_simd_attribute, false },
74acc703 805 { "cilk simd function", 0, -1, true, false, false,
806 handle_omp_declare_simd_attribute, false },
bc7bff74 807 { "omp declare target", 0, 0, true, false, false,
808 handle_omp_declare_target_attribute, false },
237e78b1 809 { "alloc_align", 1, 1, false, true, true,
810 handle_alloc_align_attribute, false },
811 { "assume_aligned", 1, 2, false, true, true,
812 handle_assume_aligned_attribute, false },
74691f46 813 { "designated_init", 0, 0, false, true, false,
814 handle_designated_init_attribute, false },
058a1b7a 815 { "bnd_variable_size", 0, 0, true, false, false,
816 handle_bnd_variable_size_attribute, false },
817 { "bnd_legacy", 0, 0, true, false, false,
818 handle_bnd_legacy, false },
819 { "bnd_instrument", 0, 0, true, false, false,
820 handle_bnd_instrument, false },
ac86af5d 821 { NULL, 0, 0, false, false, false, NULL, false }
f8e93a2e 822};
823
824/* Give the specifications for the format attributes, used by C and all
058a1b7a 825 descendants.
f8e93a2e 826
058a1b7a 827 All attributes referencing arguments should be additionally processed
828 in chkp_copy_function_type_adding_bounds for correct instrumentation
829 by Pointer Bounds Checker.
830 Current list of processed format attributes: format, format_arg. */
f8e93a2e 831const struct attribute_spec c_common_format_attribute_table[] =
832{
ac86af5d 833 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
834 affects_type_identity } */
f8e93a2e 835 { "format", 3, 3, false, true, true,
ac86af5d 836 handle_format_attribute, false },
f8e93a2e 837 { "format_arg", 1, 1, false, true, true,
ac86af5d 838 handle_format_arg_attribute, false },
839 { NULL, 0, 0, false, false, false, NULL, false }
f8e93a2e 840};
841
6d5d708e 842/* Return identifier for address space AS. */
34208e18 843
6d5d708e 844const char *
845c_addr_space_name (addr_space_t as)
846{
34208e18 847 int rid = RID_FIRST_ADDR_SPACE + as;
848 gcc_assert (ridpointers [rid]);
849 return IDENTIFIER_POINTER (ridpointers [rid]);
6d5d708e 850}
851
2c0e001b 852/* Push current bindings for the function name VAR_DECLS. */
f4e3c278 853
854void
1cae46be 855start_fname_decls (void)
f4e3c278 856{
65b7f83f 857 unsigned ix;
858 tree saved = NULL_TREE;
1cae46be 859
65b7f83f 860 for (ix = 0; fname_vars[ix].decl; ix++)
861 {
862 tree decl = *fname_vars[ix].decl;
f4e3c278 863
65b7f83f 864 if (decl)
865 {
ceb7b692 866 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
867 saved);
65b7f83f 868 *fname_vars[ix].decl = NULL_TREE;
869 }
870 }
871 if (saved || saved_function_name_decls)
872 /* Normally they'll have been NULL, so only push if we've got a
873 stack, or they are non-NULL. */
874 saved_function_name_decls = tree_cons (saved, NULL_TREE,
875 saved_function_name_decls);
876}
877
2363ef00 878/* Finish up the current bindings, adding them into the current function's
879 statement tree. This must be done _before_ finish_stmt_tree is called.
880 If there is no current function, we must be at file scope and no statements
881 are involved. Pop the previous bindings. */
65b7f83f 882
883void
1cae46be 884finish_fname_decls (void)
65b7f83f 885{
886 unsigned ix;
2363ef00 887 tree stmts = NULL_TREE;
65b7f83f 888 tree stack = saved_function_name_decls;
889
890 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
2363ef00 891 append_to_statement_list (TREE_VALUE (stack), &stmts);
1cae46be 892
2363ef00 893 if (stmts)
65b7f83f 894 {
2363ef00 895 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
5c423bd6 896
2363ef00 897 if (TREE_CODE (*bodyp) == BIND_EXPR)
898 bodyp = &BIND_EXPR_BODY (*bodyp);
81010c97 899
bc2b76e0 900 append_to_statement_list_force (*bodyp, &stmts);
2363ef00 901 *bodyp = stmts;
65b7f83f 902 }
1cae46be 903
65b7f83f 904 for (ix = 0; fname_vars[ix].decl; ix++)
905 *fname_vars[ix].decl = NULL_TREE;
1cae46be 906
65b7f83f 907 if (stack)
f4e3c278 908 {
2c0e001b 909 /* We had saved values, restore them. */
65b7f83f 910 tree saved;
911
912 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
913 {
914 tree decl = TREE_PURPOSE (saved);
f9ae6f95 915 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
1cae46be 916
65b7f83f 917 *fname_vars[ix].decl = decl;
918 }
919 stack = TREE_CHAIN (stack);
f4e3c278 920 }
65b7f83f 921 saved_function_name_decls = stack;
922}
923
81010c97 924/* Return the text name of the current function, suitably prettified
5fc7fa69 925 by PRETTY_P. Return string must be freed by caller. */
65b7f83f 926
927const char *
1cae46be 928fname_as_string (int pretty_p)
65b7f83f 929{
9ad4bb1e 930 const char *name = "top level";
5fc7fa69 931 char *namep;
8115b8be 932 int vrb = 2, len;
933 cpp_string cstr = { 0, 0 }, strname;
9ad4bb1e 934
84166705 935 if (!pretty_p)
9ad4bb1e 936 {
937 name = "";
938 vrb = 0;
939 }
940
941 if (current_function_decl)
dc24ddbd 942 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
9ad4bb1e 943
8115b8be 944 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
5fc7fa69 945
8115b8be 946 namep = XNEWVEC (char, len);
947 snprintf (namep, len, "\"%s\"", name);
948 strname.text = (unsigned char *) namep;
949 strname.len = len - 1;
5fc7fa69 950
924bbf02 951 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
8115b8be 952 {
953 XDELETEVEC (namep);
954 return (const char *) cstr.text;
5fc7fa69 955 }
5fc7fa69 956
957 return namep;
65b7f83f 958}
959
65b7f83f 960/* Return the VAR_DECL for a const char array naming the current
961 function. If the VAR_DECL has not yet been created, create it
962 now. RID indicates how it should be formatted and IDENTIFIER_NODE
963 ID is its name (unfortunately C and C++ hold the RID values of
964 keywords in different places, so we can't derive RID from ID in
e3b80d49 965 this language independent code. LOC is the location of the
966 function. */
65b7f83f 967
968tree
e3b80d49 969fname_decl (location_t loc, unsigned int rid, tree id)
65b7f83f 970{
971 unsigned ix;
972 tree decl = NULL_TREE;
973
974 for (ix = 0; fname_vars[ix].decl; ix++)
975 if (fname_vars[ix].rid == rid)
976 break;
977
978 decl = *fname_vars[ix].decl;
979 if (!decl)
f4e3c278 980 {
2222b3c6 981 /* If a tree is built here, it would normally have the lineno of
982 the current statement. Later this tree will be moved to the
983 beginning of the function and this line number will be wrong.
984 To avoid this problem set the lineno to 0 here; that prevents
7299020b 985 it from appearing in the RTL. */
2363ef00 986 tree stmts;
9a6486a6 987 location_t saved_location = input_location;
9a6486a6 988 input_location = UNKNOWN_LOCATION;
1cae46be 989
2363ef00 990 stmts = push_stmt_list ();
e60a6f7b 991 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
2363ef00 992 stmts = pop_stmt_list (stmts);
993 if (!IS_EMPTY_STMT (stmts))
994 saved_function_name_decls
995 = tree_cons (decl, stmts, saved_function_name_decls);
65b7f83f 996 *fname_vars[ix].decl = decl;
9a6486a6 997 input_location = saved_location;
f4e3c278 998 }
65b7f83f 999 if (!ix && !current_function_decl)
e3b80d49 1000 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
81010c97 1001
65b7f83f 1002 return decl;
f4e3c278 1003}
1004
070236f0 1005/* Given a STRING_CST, give it a suitable array-of-chars data type. */
b0fc3e72 1006
1007tree
1cae46be 1008fix_string_type (tree value)
b0fc3e72 1009{
070236f0 1010 int length = TREE_STRING_LENGTH (value);
1011 int nchars;
00d26680 1012 tree e_type, i_type, a_type;
1013
73be5127 1014 /* Compute the number of elements, for the array type. */
924bbf02 1015 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1016 {
1017 nchars = length;
1018 e_type = char_type_node;
1019 }
1020 else if (TREE_TYPE (value) == char16_array_type_node)
1021 {
1022 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1023 e_type = char16_type_node;
1024 }
1025 else if (TREE_TYPE (value) == char32_array_type_node)
1026 {
1027 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1028 e_type = char32_type_node;
1029 }
1030 else
1031 {
1032 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1033 e_type = wchar_type_node;
1034 }
b0fc3e72 1035
1d752508 1036 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1037 limit in C++98 Annex B is very large (65536) and is not normative,
1038 so we do not diagnose it (warn_overlength_strings is forced off
1039 in c_common_post_options). */
1040 if (warn_overlength_strings)
1041 {
1042 const int nchars_max = flag_isoc99 ? 4095 : 509;
1043 const int relevant_std = flag_isoc99 ? 99 : 90;
1044 if (nchars - 1 > nchars_max)
1045 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1046 separate the %d from the 'C'. 'ISO' should not be
1047 translated, but it may be moved after 'C%d' in languages
1048 where modifiers follow nouns. */
21ca8540 1049 pedwarn (input_location, OPT_Woverlength_strings,
8864917d 1050 "string length %qd is greater than the length %qd "
1d752508 1051 "ISO C%d compilers are required to support",
1052 nchars - 1, nchars_max, relevant_std);
1053 }
82cfc7f7 1054
390be14e 1055 /* Create the array type for the string constant. The ISO C++
1056 standard says that a string literal has type `const char[N]' or
1057 `const wchar_t[N]'. We use the same logic when invoked as a C
1058 front-end with -Wwrite-strings.
1059 ??? We should change the type of an expression depending on the
1060 state of a warning flag. We should just be warning -- see how
1061 this is handled in the C++ front-end for the deprecated implicit
1062 conversion from string literals to `char*' or `wchar_t*'.
00d26680 1063
1064 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1065 array type being the unqualified version of that type.
1066 Therefore, if we are constructing an array of const char, we must
1067 construct the matching unqualified array type first. The C front
1068 end does not require this, but it does no harm, so we do it
1069 unconditionally. */
ceb7b692 1070 i_type = build_index_type (size_int (nchars - 1));
00d26680 1071 a_type = build_array_type (e_type, i_type);
390be14e 1072 if (c_dialect_cxx() || warn_write_strings)
aebc8537 1073 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
3a10ba35 1074
00d26680 1075 TREE_TYPE (value) = a_type;
b8e3b7ad 1076 TREE_CONSTANT (value) = 1;
a814bad5 1077 TREE_READONLY (value) = 1;
b0fc3e72 1078 TREE_STATIC (value) = 1;
1079 return value;
1080}
1081\f
fc501191 1082/* If DISABLE is true, stop issuing warnings. This is used when
1083 parsing code that we know will not be executed. This function may
1084 be called multiple times, and works as a stack. */
1085
1086static void
1087c_disable_warnings (bool disable)
1088{
1089 if (disable)
1090 {
1091 ++c_inhibit_evaluation_warnings;
1092 fold_defer_overflow_warnings ();
1093 }
1094}
1095
1096/* If ENABLE is true, reenable issuing warnings. */
1097
1098static void
1099c_enable_warnings (bool enable)
1100{
1101 if (enable)
1102 {
1103 --c_inhibit_evaluation_warnings;
1104 fold_undefer_and_ignore_overflow_warnings ();
1105 }
1106}
1107
a75b1c71 1108/* Fully fold EXPR, an expression that was not folded (beyond integer
1109 constant expressions and null pointer constants) when being built
1110 up. If IN_INIT, this is in a static initializer and certain
1111 changes are made to the folding done. Clear *MAYBE_CONST if
1112 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1113 expression because it contains an evaluated operator (in C99) or an
1114 operator outside of sizeof returning an integer constant (in C90)
1115 not permitted in constant expressions, or because it contains an
1116 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1117 set to true by callers before calling this function.) Return the
1118 folded expression. Function arguments have already been folded
1119 before calling this function, as have the contents of SAVE_EXPR,
1120 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1121 C_MAYBE_CONST_EXPR. */
1122
1123tree
1124c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1125{
1126 tree ret;
c6418a4e 1127 tree eptype = NULL_TREE;
a75b1c71 1128 bool dummy = true;
1129 bool maybe_const_itself = true;
389dd41b 1130 location_t loc = EXPR_LOCATION (expr);
a75b1c71 1131
1132 /* This function is not relevant to C++ because C++ folds while
1133 parsing, and may need changes to be correct for C++ when C++
1134 stops folding while parsing. */
1135 if (c_dialect_cxx ())
1136 gcc_unreachable ();
1137
1138 if (!maybe_const)
1139 maybe_const = &dummy;
c6418a4e 1140 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1141 {
1142 eptype = TREE_TYPE (expr);
1143 expr = TREE_OPERAND (expr, 0);
1144 }
a75b1c71 1145 ret = c_fully_fold_internal (expr, in_init, maybe_const,
aac24642 1146 &maybe_const_itself, false);
c6418a4e 1147 if (eptype)
389dd41b 1148 ret = fold_convert_loc (loc, eptype, ret);
a75b1c71 1149 *maybe_const &= maybe_const_itself;
1150 return ret;
1151}
1152
1153/* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1154 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1155 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1156 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1157 both evaluated and unevaluated subexpressions while
1158 *MAYBE_CONST_ITSELF is carried from only evaluated
aac24642 1159 subexpressions). FOR_INT_CONST indicates if EXPR is an expression
1160 with integer constant operands, and if any of the operands doesn't
1161 get folded to an integer constant, don't fold the expression itself. */
a75b1c71 1162
1163static tree
1164c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
aac24642 1165 bool *maybe_const_itself, bool for_int_const)
a75b1c71 1166{
1167 tree ret = expr;
1168 enum tree_code code = TREE_CODE (expr);
1169 enum tree_code_class kind = TREE_CODE_CLASS (code);
1170 location_t loc = EXPR_LOCATION (expr);
1171 tree op0, op1, op2, op3;
1172 tree orig_op0, orig_op1, orig_op2;
1173 bool op0_const = true, op1_const = true, op2_const = true;
1174 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1175 bool nowarning = TREE_NO_WARNING (expr);
fc501191 1176 bool unused_p;
a75b1c71 1177
1178 /* This function is not relevant to C++ because C++ folds while
1179 parsing, and may need changes to be correct for C++ when C++
1180 stops folding while parsing. */
1181 if (c_dialect_cxx ())
1182 gcc_unreachable ();
1183
1184 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1185 anything else not counted as an expression cannot usefully be
1186 folded further at this point. */
1187 if (!IS_EXPR_CODE_CLASS (kind)
1188 || kind == tcc_statement
1189 || code == SAVE_EXPR)
1190 return expr;
1191
1192 /* Operands of variable-length expressions (function calls) have
1193 already been folded, as have __builtin_* function calls, and such
1194 expressions cannot occur in constant expressions. */
1195 if (kind == tcc_vl_exp)
1196 {
1197 *maybe_const_operands = false;
1198 ret = fold (expr);
1199 goto out;
1200 }
1201
1202 if (code == C_MAYBE_CONST_EXPR)
1203 {
1204 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1205 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1206 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1207 *maybe_const_operands = false;
1208 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
aac24642 1209 {
1210 *maybe_const_itself = false;
1211 inner = c_fully_fold_internal (inner, in_init, maybe_const_operands,
1212 maybe_const_itself, true);
1213 }
a75b1c71 1214 if (pre && !in_init)
1215 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1216 else
1217 ret = inner;
1218 goto out;
1219 }
1220
1221 /* Assignment, increment, decrement, function call and comma
1222 operators, and statement expressions, cannot occur in constant
1223 expressions if evaluated / outside of sizeof. (Function calls
1224 were handled above, though VA_ARG_EXPR is treated like a function
1225 call here, and statement expressions are handled through
1226 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1227 switch (code)
1228 {
1229 case MODIFY_EXPR:
1230 case PREDECREMENT_EXPR:
1231 case PREINCREMENT_EXPR:
1232 case POSTDECREMENT_EXPR:
1233 case POSTINCREMENT_EXPR:
1234 case COMPOUND_EXPR:
1235 *maybe_const_operands = false;
1236 break;
1237
1238 case VA_ARG_EXPR:
1239 case TARGET_EXPR:
1240 case BIND_EXPR:
1241 case OBJ_TYPE_REF:
1242 *maybe_const_operands = false;
1243 ret = fold (expr);
1244 goto out;
1245
1246 default:
1247 break;
1248 }
1249
1250 /* Fold individual tree codes as appropriate. */
1251 switch (code)
1252 {
1253 case COMPOUND_LITERAL_EXPR:
1254 /* Any non-constancy will have been marked in a containing
1255 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1256 goto out;
1257
1258 case COMPONENT_REF:
1259 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1260 op1 = TREE_OPERAND (expr, 1);
1261 op2 = TREE_OPERAND (expr, 2);
1262 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1263 maybe_const_itself, for_int_const);
f59e3889 1264 STRIP_TYPE_NOPS (op0);
a75b1c71 1265 if (op0 != orig_op0)
1266 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1267 if (ret != expr)
1268 {
1269 TREE_READONLY (ret) = TREE_READONLY (expr);
1270 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1271 }
1272 goto out;
1273
1274 case ARRAY_REF:
1275 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1276 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1277 op2 = TREE_OPERAND (expr, 2);
1278 op3 = TREE_OPERAND (expr, 3);
1279 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1280 maybe_const_itself, for_int_const);
f59e3889 1281 STRIP_TYPE_NOPS (op0);
a75b1c71 1282 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
aac24642 1283 maybe_const_itself, for_int_const);
f59e3889 1284 STRIP_TYPE_NOPS (op1);
a75b1c71 1285 op1 = decl_constant_value_for_optimization (op1);
1286 if (op0 != orig_op0 || op1 != orig_op1)
1287 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1288 if (ret != expr)
1289 {
1290 TREE_READONLY (ret) = TREE_READONLY (expr);
1291 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1292 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1293 }
1294 ret = fold (ret);
1295 goto out;
1296
1297 case COMPOUND_EXPR:
1298 case MODIFY_EXPR:
1299 case PREDECREMENT_EXPR:
1300 case PREINCREMENT_EXPR:
1301 case POSTDECREMENT_EXPR:
1302 case POSTINCREMENT_EXPR:
1303 case PLUS_EXPR:
1304 case MINUS_EXPR:
1305 case MULT_EXPR:
1306 case POINTER_PLUS_EXPR:
1307 case TRUNC_DIV_EXPR:
1308 case CEIL_DIV_EXPR:
1309 case FLOOR_DIV_EXPR:
1310 case TRUNC_MOD_EXPR:
1311 case RDIV_EXPR:
1312 case EXACT_DIV_EXPR:
1313 case LSHIFT_EXPR:
1314 case RSHIFT_EXPR:
1315 case BIT_IOR_EXPR:
1316 case BIT_XOR_EXPR:
1317 case BIT_AND_EXPR:
1318 case LT_EXPR:
1319 case LE_EXPR:
1320 case GT_EXPR:
1321 case GE_EXPR:
1322 case EQ_EXPR:
1323 case NE_EXPR:
1324 case COMPLEX_EXPR:
1325 case TRUTH_AND_EXPR:
1326 case TRUTH_OR_EXPR:
1327 case TRUTH_XOR_EXPR:
1328 case UNORDERED_EXPR:
1329 case ORDERED_EXPR:
1330 case UNLT_EXPR:
1331 case UNLE_EXPR:
1332 case UNGT_EXPR:
1333 case UNGE_EXPR:
1334 case UNEQ_EXPR:
1335 /* Binary operations evaluating both arguments (increment and
1336 decrement are binary internally in GCC). */
1337 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1338 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1339 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1340 maybe_const_itself, for_int_const);
f59e3889 1341 STRIP_TYPE_NOPS (op0);
a75b1c71 1342 if (code != MODIFY_EXPR
1343 && code != PREDECREMENT_EXPR
1344 && code != PREINCREMENT_EXPR
1345 && code != POSTDECREMENT_EXPR
1346 && code != POSTINCREMENT_EXPR)
1347 op0 = decl_constant_value_for_optimization (op0);
1348 /* The RHS of a MODIFY_EXPR was fully folded when building that
1349 expression for the sake of conversion warnings. */
1350 if (code != MODIFY_EXPR)
1351 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
aac24642 1352 maybe_const_itself, for_int_const);
f59e3889 1353 STRIP_TYPE_NOPS (op1);
a75b1c71 1354 op1 = decl_constant_value_for_optimization (op1);
aac24642 1355
1356 if (for_int_const && (TREE_CODE (op0) != INTEGER_CST
1357 || TREE_CODE (op1) != INTEGER_CST))
1358 goto out;
1359
a75b1c71 1360 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1361 ret = in_init
389dd41b 1362 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1363 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
a75b1c71 1364 else
1365 ret = fold (expr);
672d914b 1366 if (TREE_OVERFLOW_P (ret)
1367 && !TREE_OVERFLOW_P (op0)
1368 && !TREE_OVERFLOW_P (op1))
1369 overflow_warning (EXPR_LOCATION (expr), ret);
cdc64059 1370 if (code == LSHIFT_EXPR
1371 && TREE_CODE (orig_op0) != INTEGER_CST
1372 && TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1373 && TREE_CODE (op0) == INTEGER_CST
1374 && c_inhibit_evaluation_warnings == 0
1375 && tree_int_cst_sgn (op0) < 0)
1376 warning_at (loc, OPT_Wshift_negative_value,
1377 "left shift of negative value");
7c834436 1378 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1379 && TREE_CODE (orig_op1) != INTEGER_CST
1380 && TREE_CODE (op1) == INTEGER_CST
1381 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1382 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1383 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1384 && c_inhibit_evaluation_warnings == 0)
1385 {
1386 if (tree_int_cst_sgn (op1) < 0)
f11bdffb 1387 warning_at (loc, OPT_Wshift_count_negative,
1388 (code == LSHIFT_EXPR
1389 ? G_("left shift count is negative")
1390 : G_("right shift count is negative")));
7c834436 1391 else if (compare_tree_int (op1,
1392 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1393 >= 0)
f11bdffb 1394 warning_at (loc, OPT_Wshift_count_overflow,
1395 (code == LSHIFT_EXPR
1396 ? G_("left shift count >= width of type")
1397 : G_("right shift count >= width of type")));
7c834436 1398 }
b57910fa 1399 if ((code == TRUNC_DIV_EXPR
1400 || code == CEIL_DIV_EXPR
1401 || code == FLOOR_DIV_EXPR
1402 || code == EXACT_DIV_EXPR
1403 || code == TRUNC_MOD_EXPR)
1404 && TREE_CODE (orig_op1) != INTEGER_CST
1405 && TREE_CODE (op1) == INTEGER_CST
1406 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1407 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1408 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE)
1409 warn_for_div_by_zero (loc, op1);
a75b1c71 1410 goto out;
1411
1412 case INDIRECT_REF:
1413 case FIX_TRUNC_EXPR:
1414 case FLOAT_EXPR:
1415 CASE_CONVERT:
e66325ea 1416 case ADDR_SPACE_CONVERT_EXPR:
2b19dfe4 1417 case VIEW_CONVERT_EXPR:
a75b1c71 1418 case NON_LVALUE_EXPR:
1419 case NEGATE_EXPR:
1420 case BIT_NOT_EXPR:
1421 case TRUTH_NOT_EXPR:
1422 case ADDR_EXPR:
1423 case CONJ_EXPR:
1424 case REALPART_EXPR:
1425 case IMAGPART_EXPR:
1426 /* Unary operations. */
1427 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1428 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1429 maybe_const_itself, for_int_const);
f59e3889 1430 STRIP_TYPE_NOPS (op0);
a75b1c71 1431 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1432 op0 = decl_constant_value_for_optimization (op0);
aac24642 1433
1434 if (for_int_const && TREE_CODE (op0) != INTEGER_CST)
1435 goto out;
1436
737a23cc 1437 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1438 not prepared to deal with them if they occur in initializers. */
1439 if (op0 != orig_op0
1440 && code == ADDR_EXPR
1441 && (op1 = get_base_address (op0)) != NULL_TREE
1442 && TREE_CODE (op1) == INDIRECT_REF
1443 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
7549df0d 1444 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
737a23cc 1445 else if (op0 != orig_op0 || in_init)
a75b1c71 1446 ret = in_init
389dd41b 1447 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1448 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
a75b1c71 1449 else
1450 ret = fold (expr);
1451 if (code == INDIRECT_REF
1452 && ret != expr
1453 && TREE_CODE (ret) == INDIRECT_REF)
1454 {
1455 TREE_READONLY (ret) = TREE_READONLY (expr);
1456 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1457 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1458 }
672d914b 1459 switch (code)
1460 {
1461 case FIX_TRUNC_EXPR:
1462 case FLOAT_EXPR:
1463 CASE_CONVERT:
1464 /* Don't warn about explicit conversions. We will already
1465 have warned about suspect implicit conversions. */
1466 break;
1467
1468 default:
1469 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1470 overflow_warning (EXPR_LOCATION (expr), ret);
1471 break;
1472 }
a75b1c71 1473 goto out;
1474
1475 case TRUTH_ANDIF_EXPR:
1476 case TRUTH_ORIF_EXPR:
1477 /* Binary operations not necessarily evaluating both
1478 arguments. */
1479 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1480 orig_op1 = op1 = TREE_OPERAND (expr, 1);
aac24642 1481 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1482 for_int_const);
f59e3889 1483 STRIP_TYPE_NOPS (op0);
672d914b 1484
1485 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1486 ? truthvalue_false_node
1487 : truthvalue_true_node));
fc501191 1488 c_disable_warnings (unused_p);
aac24642 1489 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1490 for_int_const);
f59e3889 1491 STRIP_TYPE_NOPS (op1);
fc501191 1492 c_enable_warnings (unused_p);
672d914b 1493
aac24642 1494 if (for_int_const
1495 && (TREE_CODE (op0) != INTEGER_CST
1496 /* Require OP1 be an INTEGER_CST only if it's evaluated. */
1497 || (!unused_p && TREE_CODE (op1) != INTEGER_CST)))
1498 goto out;
1499
a75b1c71 1500 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1501 ret = in_init
389dd41b 1502 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1503 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
a75b1c71 1504 else
1505 ret = fold (expr);
1506 *maybe_const_operands &= op0_const;
1507 *maybe_const_itself &= op0_const_self;
1508 if (!(flag_isoc99
1509 && op0_const
1510 && op0_const_self
1511 && (code == TRUTH_ANDIF_EXPR
1512 ? op0 == truthvalue_false_node
1513 : op0 == truthvalue_true_node)))
1514 *maybe_const_operands &= op1_const;
1515 if (!(op0_const
1516 && op0_const_self
1517 && (code == TRUTH_ANDIF_EXPR
1518 ? op0 == truthvalue_false_node
1519 : op0 == truthvalue_true_node)))
1520 *maybe_const_itself &= op1_const_self;
1521 goto out;
1522
1523 case COND_EXPR:
1524 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1525 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1526 orig_op2 = op2 = TREE_OPERAND (expr, 2);
aac24642 1527 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1528 for_int_const);
672d914b 1529
f59e3889 1530 STRIP_TYPE_NOPS (op0);
fc501191 1531 c_disable_warnings (op0 == truthvalue_false_node);
aac24642 1532 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1533 for_int_const);
f59e3889 1534 STRIP_TYPE_NOPS (op1);
fc501191 1535 c_enable_warnings (op0 == truthvalue_false_node);
672d914b 1536
fc501191 1537 c_disable_warnings (op0 == truthvalue_true_node);
aac24642 1538 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self,
1539 for_int_const);
f59e3889 1540 STRIP_TYPE_NOPS (op2);
fc501191 1541 c_enable_warnings (op0 == truthvalue_true_node);
672d914b 1542
aac24642 1543 if (for_int_const
1544 && (TREE_CODE (op0) != INTEGER_CST
1545 /* Only the evaluated operand must be an INTEGER_CST. */
1546 || (op0 == truthvalue_true_node
1547 ? TREE_CODE (op1) != INTEGER_CST
1548 : TREE_CODE (op2) != INTEGER_CST)))
1549 goto out;
1550
a75b1c71 1551 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
389dd41b 1552 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
a75b1c71 1553 else
1554 ret = fold (expr);
1555 *maybe_const_operands &= op0_const;
1556 *maybe_const_itself &= op0_const_self;
1557 if (!(flag_isoc99
1558 && op0_const
1559 && op0_const_self
1560 && op0 == truthvalue_false_node))
1561 *maybe_const_operands &= op1_const;
1562 if (!(op0_const
1563 && op0_const_self
1564 && op0 == truthvalue_false_node))
1565 *maybe_const_itself &= op1_const_self;
1566 if (!(flag_isoc99
1567 && op0_const
1568 && op0_const_self
1569 && op0 == truthvalue_true_node))
1570 *maybe_const_operands &= op2_const;
1571 if (!(op0_const
1572 && op0_const_self
1573 && op0 == truthvalue_true_node))
1574 *maybe_const_itself &= op2_const_self;
1575 goto out;
1576
c6418a4e 1577 case EXCESS_PRECISION_EXPR:
1578 /* Each case where an operand with excess precision may be
1579 encountered must remove the EXCESS_PRECISION_EXPR around
1580 inner operands and possibly put one around the whole
1581 expression or possibly convert to the semantic type (which
1582 c_fully_fold does); we cannot tell at this stage which is
1583 appropriate in any particular case. */
1584 gcc_unreachable ();
1585
a75b1c71 1586 default:
1587 /* Various codes may appear through folding built-in functions
1588 and their arguments. */
1589 goto out;
1590 }
1591
1592 out:
1593 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1594 have been done by this point, so remove them again. */
1595 nowarning |= TREE_NO_WARNING (ret);
1596 STRIP_TYPE_NOPS (ret);
1597 if (nowarning && !TREE_NO_WARNING (ret))
1598 {
1599 if (!CAN_HAVE_LOCATION_P (ret))
1600 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1601 TREE_NO_WARNING (ret) = 1;
1602 }
1603 if (ret != expr)
1604 protected_set_expr_location (ret, loc);
1605 return ret;
1606}
1607
1608/* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1609 return EXP. Otherwise, return either EXP or its known constant
1610 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1611 Is the BLKmode test appropriate? */
1612
1613tree
1614decl_constant_value_for_optimization (tree exp)
1615{
1616 tree ret;
1617
1618 /* This function is only used by C, for c_fully_fold and other
1619 optimization, and may not be correct for C++. */
1620 if (c_dialect_cxx ())
1621 gcc_unreachable ();
1622
1623 if (!optimize
1624 || TREE_CODE (exp) != VAR_DECL
1625 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1626 || DECL_MODE (exp) == BLKmode)
1627 return exp;
1628
1629 ret = decl_constant_value (exp);
1630 /* Avoid unwanted tree sharing between the initializer and current
1631 function's body where the tree can be modified e.g. by the
1632 gimplifier. */
1633 if (ret != exp && TREE_STATIC (exp))
1634 ret = unshare_expr (ret);
1635 return ret;
1636}
1637
2a1736ed 1638/* Print a warning if a constant expression had overflow in folding.
1639 Invoke this function on every expression that the language
1640 requires to be a constant expression.
1641 Note the ANSI C standard says it is erroneous for a
1642 constant expression to overflow. */
b2806639 1643
1644void
1cae46be 1645constant_expression_warning (tree value)
07317e69 1646{
48e1416a 1647 if (warn_overflow && pedantic
07317e69 1648 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1649 || TREE_CODE (value) == FIXED_CST
1650 || TREE_CODE (value) == VECTOR_CST
1651 || TREE_CODE (value) == COMPLEX_CST)
1652 && TREE_OVERFLOW (value))
21ca8540 1653 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
07317e69 1654}
1655
1656/* The same as above but print an unconditional error. */
1657void
1658constant_expression_error (tree value)
b2806639 1659{
837e1122 1660 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
9421ebb9 1661 || TREE_CODE (value) == FIXED_CST
886cfd4f 1662 || TREE_CODE (value) == VECTOR_CST
837e1122 1663 || TREE_CODE (value) == COMPLEX_CST)
07317e69 1664 && TREE_OVERFLOW (value))
1665 error ("overflow in constant expression");
2a1736ed 1666}
1667
f170d67f 1668/* Print a warning if an expression had overflow in folding and its
1669 operands hadn't.
1670
2a1736ed 1671 Invoke this function on every expression that
1672 (1) appears in the source code, and
f170d67f 1673 (2) is a constant expression that overflowed, and
2a1736ed 1674 (3) is not already checked by convert_and_check;
f170d67f 1675 however, do not invoke this function on operands of explicit casts
1676 or when the expression is the result of an operator and any operand
1677 already overflowed. */
2a1736ed 1678
1679void
e60a6f7b 1680overflow_warning (location_t loc, tree value)
2a1736ed 1681{
48d94ede 1682 if (c_inhibit_evaluation_warnings != 0)
1683 return;
f170d67f 1684
1685 switch (TREE_CODE (value))
886cfd4f 1686 {
f170d67f 1687 case INTEGER_CST:
e60a6f7b 1688 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
f170d67f 1689 break;
48e1416a 1690
f170d67f 1691 case REAL_CST:
e60a6f7b 1692 warning_at (loc, OPT_Woverflow,
1693 "floating point overflow in expression");
f170d67f 1694 break;
48e1416a 1695
9421ebb9 1696 case FIXED_CST:
e60a6f7b 1697 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
9421ebb9 1698 break;
1699
f170d67f 1700 case VECTOR_CST:
e60a6f7b 1701 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
f170d67f 1702 break;
48e1416a 1703
f170d67f 1704 case COMPLEX_CST:
1705 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
e60a6f7b 1706 warning_at (loc, OPT_Woverflow,
1707 "complex integer overflow in expression");
f170d67f 1708 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
e60a6f7b 1709 warning_at (loc, OPT_Woverflow,
1710 "complex floating point overflow in expression");
f170d67f 1711 break;
1712
1713 default:
1714 break;
886cfd4f 1715 }
2a1736ed 1716}
1717
03033af4 1718/* Warn about uses of logical || / && operator in a context where it
1719 is likely that the bitwise equivalent was intended by the
1720 programmer. We have seen an expression in which CODE is a binary
9c20c4fc 1721 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1722 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
b13d1547 1723void
9c20c4fc 1724warn_logical_operator (location_t location, enum tree_code code, tree type,
48e1416a 1725 enum tree_code code_left, tree op_left,
03033af4 1726 enum tree_code ARG_UNUSED (code_right), tree op_right)
b13d1547 1727{
9c20c4fc 1728 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1729 int in0_p, in1_p, in_p;
1730 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1731 bool strict_overflow_p = false;
1732
03033af4 1733 if (code != TRUTH_ANDIF_EXPR
1734 && code != TRUTH_AND_EXPR
1735 && code != TRUTH_ORIF_EXPR
1736 && code != TRUTH_OR_EXPR)
1737 return;
1738
439606a9 1739 /* We don't want to warn if either operand comes from a macro
1740 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1741 see PR61534. */
1742 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1743 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1744 return;
1745
03033af4 1746 /* Warn if &&/|| are being used in a context where it is
1747 likely that the bitwise equivalent was intended by the
1748 programmer. That is, an expression such as op && MASK
1749 where op should not be any boolean expression, nor a
1750 constant, and mask seems to be a non-boolean integer constant. */
1751 if (!truth_value_p (code_left)
1752 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1753 && !CONSTANT_CLASS_P (op_left)
1754 && !TREE_NO_WARNING (op_left)
1755 && TREE_CODE (op_right) == INTEGER_CST
1756 && !integer_zerop (op_right)
1757 && !integer_onep (op_right))
b13d1547 1758 {
9c20c4fc 1759 if (or_op)
03033af4 1760 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1761 " applied to non-boolean constant");
1762 else
1763 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1764 " applied to non-boolean constant");
1765 TREE_NO_WARNING (op_left) = true;
9c20c4fc 1766 return;
1767 }
1768
1769 /* We do not warn for constants because they are typical of macro
1770 expansions that test for features. */
1771 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1772 return;
1773
1774 /* This warning only makes sense with logical operands. */
1775 if (!(truth_value_p (TREE_CODE (op_left))
1776 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1777 || !(truth_value_p (TREE_CODE (op_right))
1778 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1779 return;
1780
5e84569c 1781 /* The range computations only work with scalars. */
1782 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1783 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1784 return;
9c20c4fc 1785
686369e8 1786 /* We first test whether either side separately is trivially true
1787 (with OR) or trivially false (with AND). If so, do not warn.
1788 This is a common idiom for testing ranges of data types in
1789 portable code. */
1790 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1791 if (!lhs)
1792 return;
1793 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
9c20c4fc 1794 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1795
686369e8 1796 /* If this is an OR operation, invert both sides; now, the result
1797 should be always false to get a warning. */
1798 if (or_op)
1799 in0_p = !in0_p;
1800
1801 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
d42e7c5a 1802 if (tem && integer_zerop (tem))
686369e8 1803 return;
1804
1805 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1806 if (!rhs)
1807 return;
1808 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
9c20c4fc 1809 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
48e1416a 1810
686369e8 1811 /* If this is an OR operation, invert both sides; now, the result
1812 should be always false to get a warning. */
9c20c4fc 1813 if (or_op)
686369e8 1814 in1_p = !in1_p;
48e1416a 1815
686369e8 1816 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
d42e7c5a 1817 if (tem && integer_zerop (tem))
686369e8 1818 return;
1819
1820 /* If both expressions have the same operand, if we can merge the
485f6b9c 1821 ranges, ... */
686369e8 1822 if (operand_equal_p (lhs, rhs, 0)
9c20c4fc 1823 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
485f6b9c 1824 in1_p, low1, high1))
9c20c4fc 1825 {
485f6b9c 1826 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1827 /* ... and if the range test is always false, then warn. */
1828 if (tem && integer_zerop (tem))
1829 {
1830 if (or_op)
1831 warning_at (location, OPT_Wlogical_op,
1832 "logical %<or%> of collectively exhaustive tests is "
1833 "always true");
1834 else
1835 warning_at (location, OPT_Wlogical_op,
1836 "logical %<and%> of mutually exclusive tests is "
1837 "always false");
1838 }
1839 /* Or warn if the operands have exactly the same range, e.g.
1840 A > 0 && A > 0. */
1841 else if (low0 == low1 && high0 == high1)
1842 {
1843 if (or_op)
1844 warning_at (location, OPT_Wlogical_op,
1845 "logical %<or%> of equal expressions");
1846 else
1847 warning_at (location, OPT_Wlogical_op,
1848 "logical %<and%> of equal expressions");
1849 }
b13d1547 1850 }
1851}
1852
32dc1512 1853/* Warn about logical not used on the left hand side operand of a comparison.
1854 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
dc6229e8 1855 Do not warn if RHS is of a boolean type. */
32dc1512 1856
1857void
1858warn_logical_not_parentheses (location_t location, enum tree_code code,
dc6229e8 1859 tree rhs)
32dc1512 1860{
dc6229e8 1861 if (TREE_CODE_CLASS (code) != tcc_comparison
1862 || TREE_TYPE (rhs) == NULL_TREE
1863 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
32dc1512 1864 return;
1865
16f958b3 1866 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1867 !(x == 0) or !(y != 0). */
1868 if ((code == EQ_EXPR || code == NE_EXPR)
1869 && integer_zerop (rhs))
1870 return;
1871
32dc1512 1872 warning_at (location, OPT_Wlogical_not_parentheses,
1873 "logical not is only applied to the left hand side of "
1874 "comparison");
1875}
b13d1547 1876
3d177e8c 1877/* Warn if EXP contains any computations whose results are not used.
1878 Return true if a warning is printed; false otherwise. LOCUS is the
1879 (potential) location of the expression. */
1880
1881bool
1882warn_if_unused_value (const_tree exp, location_t locus)
1883{
1884 restart:
1885 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1886 return false;
1887
1888 /* Don't warn about void constructs. This includes casting to void,
1889 void function calls, and statement expressions with a final cast
1890 to void. */
1891 if (VOID_TYPE_P (TREE_TYPE (exp)))
1892 return false;
1893
1894 if (EXPR_HAS_LOCATION (exp))
1895 locus = EXPR_LOCATION (exp);
1896
1897 switch (TREE_CODE (exp))
1898 {
1899 case PREINCREMENT_EXPR:
1900 case POSTINCREMENT_EXPR:
1901 case PREDECREMENT_EXPR:
1902 case POSTDECREMENT_EXPR:
1903 case MODIFY_EXPR:
1904 case INIT_EXPR:
1905 case TARGET_EXPR:
1906 case CALL_EXPR:
1907 case TRY_CATCH_EXPR:
1908 case WITH_CLEANUP_EXPR:
1909 case EXIT_EXPR:
1910 case VA_ARG_EXPR:
1911 return false;
1912
1913 case BIND_EXPR:
1914 /* For a binding, warn if no side effect within it. */
1915 exp = BIND_EXPR_BODY (exp);
1916 goto restart;
1917
1918 case SAVE_EXPR:
1919 case NON_LVALUE_EXPR:
d85dbdb3 1920 case NOP_EXPR:
3d177e8c 1921 exp = TREE_OPERAND (exp, 0);
1922 goto restart;
1923
1924 case TRUTH_ORIF_EXPR:
1925 case TRUTH_ANDIF_EXPR:
1926 /* In && or ||, warn if 2nd operand has no side effect. */
1927 exp = TREE_OPERAND (exp, 1);
1928 goto restart;
1929
1930 case COMPOUND_EXPR:
1931 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1932 return true;
1933 /* Let people do `(foo (), 0)' without a warning. */
1934 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1935 return false;
1936 exp = TREE_OPERAND (exp, 1);
1937 goto restart;
1938
1939 case COND_EXPR:
1940 /* If this is an expression with side effects, don't warn; this
1941 case commonly appears in macro expansions. */
1942 if (TREE_SIDE_EFFECTS (exp))
1943 return false;
1944 goto warn;
1945
1946 case INDIRECT_REF:
1947 /* Don't warn about automatic dereferencing of references, since
1948 the user cannot control it. */
1949 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1950 {
1951 exp = TREE_OPERAND (exp, 0);
1952 goto restart;
1953 }
1954 /* Fall through. */
1955
1956 default:
1957 /* Referencing a volatile value is a side effect, so don't warn. */
1958 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1959 && TREE_THIS_VOLATILE (exp))
1960 return false;
1961
1962 /* If this is an expression which has no operands, there is no value
1963 to be unused. There are no such language-independent codes,
1964 but front ends may define such. */
1965 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1966 return false;
1967
1968 warn:
1969 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1970 }
1971}
1972
1973
bcf22371 1974/* Print a warning about casts that might indicate violation
1975 of strict aliasing rules if -Wstrict-aliasing is used and
1e31ff37 1976 strict aliasing mode is in effect. OTYPE is the original
1977 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
bcf22371 1978
e6fa0ea6 1979bool
1e31ff37 1980strict_aliasing_warning (tree otype, tree type, tree expr)
bcf22371 1981{
45bb3afb 1982 /* Strip pointer conversion chains and get to the correct original type. */
1983 STRIP_NOPS (expr);
1984 otype = TREE_TYPE (expr);
1985
f06537f2 1986 if (!(flag_strict_aliasing
1987 && POINTER_TYPE_P (type)
1988 && POINTER_TYPE_P (otype)
1989 && !VOID_TYPE_P (TREE_TYPE (type)))
1990 /* If the type we are casting to is a ref-all pointer
1991 dereferencing it is always valid. */
1992 || TYPE_REF_CAN_ALIAS_ALL (type))
e6fa0ea6 1993 return false;
1994
1995 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
bcf22371 1996 && (DECL_P (TREE_OPERAND (expr, 0))
e6fa0ea6 1997 || handled_component_p (TREE_OPERAND (expr, 0))))
bcf22371 1998 {
1999 /* Casting the address of an object to non void pointer. Warn
2000 if the cast breaks type based aliasing. */
e6fa0ea6 2001 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
2002 {
2003 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
2004 "might break strict-aliasing rules");
2005 return true;
2006 }
bcf22371 2007 else
2008 {
48e1416a 2009 /* warn_strict_aliasing >= 3. This includes the default (3).
e6fa0ea6 2010 Only warn if the cast is dereferenced immediately. */
32c2fdea 2011 alias_set_type set1 =
e6fa0ea6 2012 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
32c2fdea 2013 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
bcf22371 2014
62d823d0 2015 if (set1 != set2 && set2 != 0
2016 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
e6fa0ea6 2017 {
2018 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2019 "pointer will break strict-aliasing rules");
2020 return true;
2021 }
2022 else if (warn_strict_aliasing == 2
879f881c 2023 && !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 2024 {
2025 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2026 "pointer might break strict-aliasing rules");
2027 return true;
2028 }
bcf22371 2029 }
2030 }
e6fa0ea6 2031 else
2032 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
2033 {
2034 /* At this level, warn for any conversions, even if an address is
2035 not taken in the same statement. This will likely produce many
2036 false positives, but could be useful to pinpoint problems that
2037 are not revealed at higher levels. */
32c2fdea 2038 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
2039 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
2040 if (!COMPLETE_TYPE_P (type)
879f881c 2041 || !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 2042 {
2043 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2044 "pointer might break strict-aliasing rules");
2045 return true;
2046 }
2047 }
2048
2049 return false;
bcf22371 2050}
2051
f003f5dc 2052/* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
2053 sizeof as last operand of certain builtins. */
2054
2055void
57f872a2 2056sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
f1f41a6c 2057 vec<tree, va_gc> *params, tree *sizeof_arg,
f003f5dc 2058 bool (*comp_types) (tree, tree))
2059{
2060 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
57f872a2 2061 bool strop = false, cmp = false;
2062 unsigned int idx = ~0;
2063 location_t loc;
f003f5dc 2064
2065 if (TREE_CODE (callee) != FUNCTION_DECL
2066 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
f1f41a6c 2067 || vec_safe_length (params) <= 1)
f003f5dc 2068 return;
2069
f003f5dc 2070 switch (DECL_FUNCTION_CODE (callee))
2071 {
2072 case BUILT_IN_STRNCMP:
2073 case BUILT_IN_STRNCASECMP:
57f872a2 2074 cmp = true;
2075 /* FALLTHRU */
f003f5dc 2076 case BUILT_IN_STRNCPY:
57f872a2 2077 case BUILT_IN_STRNCPY_CHK:
f003f5dc 2078 case BUILT_IN_STRNCAT:
57f872a2 2079 case BUILT_IN_STRNCAT_CHK:
2080 case BUILT_IN_STPNCPY:
2081 case BUILT_IN_STPNCPY_CHK:
f003f5dc 2082 strop = true;
2083 /* FALLTHRU */
2084 case BUILT_IN_MEMCPY:
57f872a2 2085 case BUILT_IN_MEMCPY_CHK:
f003f5dc 2086 case BUILT_IN_MEMMOVE:
57f872a2 2087 case BUILT_IN_MEMMOVE_CHK:
f1f41a6c 2088 if (params->length () < 3)
57f872a2 2089 return;
f1f41a6c 2090 src = (*params)[1];
2091 dest = (*params)[0];
57f872a2 2092 idx = 2;
2093 break;
2094 case BUILT_IN_BCOPY:
f1f41a6c 2095 if (params->length () < 3)
57f872a2 2096 return;
f1f41a6c 2097 src = (*params)[0];
2098 dest = (*params)[1];
57f872a2 2099 idx = 2;
2100 break;
f003f5dc 2101 case BUILT_IN_MEMCMP:
57f872a2 2102 case BUILT_IN_BCMP:
f1f41a6c 2103 if (params->length () < 3)
f003f5dc 2104 return;
f1f41a6c 2105 src = (*params)[1];
2106 dest = (*params)[0];
57f872a2 2107 idx = 2;
2108 cmp = true;
f003f5dc 2109 break;
2110 case BUILT_IN_MEMSET:
57f872a2 2111 case BUILT_IN_MEMSET_CHK:
f1f41a6c 2112 if (params->length () < 3)
f003f5dc 2113 return;
f1f41a6c 2114 dest = (*params)[0];
57f872a2 2115 idx = 2;
2116 break;
2117 case BUILT_IN_BZERO:
f1f41a6c 2118 dest = (*params)[0];
57f872a2 2119 idx = 1;
f003f5dc 2120 break;
2121 case BUILT_IN_STRNDUP:
f1f41a6c 2122 src = (*params)[0];
f003f5dc 2123 strop = true;
57f872a2 2124 idx = 1;
2125 break;
2126 case BUILT_IN_MEMCHR:
f1f41a6c 2127 if (params->length () < 3)
57f872a2 2128 return;
f1f41a6c 2129 src = (*params)[0];
57f872a2 2130 idx = 2;
2131 break;
2132 case BUILT_IN_SNPRINTF:
2133 case BUILT_IN_SNPRINTF_CHK:
2134 case BUILT_IN_VSNPRINTF:
2135 case BUILT_IN_VSNPRINTF_CHK:
f1f41a6c 2136 dest = (*params)[0];
57f872a2 2137 idx = 1;
2138 strop = true;
f003f5dc 2139 break;
2140 default:
2141 break;
2142 }
2143
57f872a2 2144 if (idx >= 3)
2145 return;
2146
2147 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2148 return;
2149
2150 type = TYPE_P (sizeof_arg[idx])
2151 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2152 if (!POINTER_TYPE_P (type))
2153 return;
2154
f003f5dc 2155 if (dest
2156 && (tem = tree_strip_nop_conversions (dest))
2157 && POINTER_TYPE_P (TREE_TYPE (tem))
2158 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2159 return;
2160
2161 if (src
2162 && (tem = tree_strip_nop_conversions (src))
2163 && POINTER_TYPE_P (TREE_TYPE (tem))
2164 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2165 return;
2166
57f872a2 2167 loc = sizeof_arg_loc[idx];
2168
2169 if (dest && !cmp)
f003f5dc 2170 {
57f872a2 2171 if (!TYPE_P (sizeof_arg[idx])
2172 && operand_equal_p (dest, sizeof_arg[idx], 0)
f003f5dc 2173 && comp_types (TREE_TYPE (dest), type))
2174 {
57f872a2 2175 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
f003f5dc 2176 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2177 "argument to %<sizeof%> in %qD call is the same "
2178 "expression as the destination; did you mean to "
2179 "remove the addressof?", callee);
2180 else if ((TYPE_PRECISION (TREE_TYPE (type))
2181 == TYPE_PRECISION (char_type_node))
2182 || strop)
2183 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2184 "argument to %<sizeof%> in %qD call is the same "
2185 "expression as the destination; did you mean to "
2186 "provide an explicit length?", callee);
2187 else
2188 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2189 "argument to %<sizeof%> in %qD call is the same "
2190 "expression as the destination; did you mean to "
2191 "dereference it?", callee);
2192 return;
2193 }
2194
2195 if (POINTER_TYPE_P (TREE_TYPE (dest))
2196 && !strop
2197 && comp_types (TREE_TYPE (dest), type)
2198 && !VOID_TYPE_P (TREE_TYPE (type)))
2199 {
2200 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2201 "argument to %<sizeof%> in %qD call is the same "
2202 "pointer type %qT as the destination; expected %qT "
2203 "or an explicit length", callee, TREE_TYPE (dest),
2204 TREE_TYPE (TREE_TYPE (dest)));
2205 return;
2206 }
2207 }
2208
57f872a2 2209 if (src && !cmp)
f003f5dc 2210 {
57f872a2 2211 if (!TYPE_P (sizeof_arg[idx])
2212 && operand_equal_p (src, sizeof_arg[idx], 0)
f003f5dc 2213 && comp_types (TREE_TYPE (src), type))
2214 {
57f872a2 2215 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
f003f5dc 2216 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2217 "argument to %<sizeof%> in %qD call is the same "
2218 "expression as the source; did you mean to "
2219 "remove the addressof?", callee);
2220 else if ((TYPE_PRECISION (TREE_TYPE (type))
2221 == TYPE_PRECISION (char_type_node))
2222 || strop)
2223 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2224 "argument to %<sizeof%> in %qD call is the same "
2225 "expression as the source; did you mean to "
2226 "provide an explicit length?", callee);
2227 else
2228 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2229 "argument to %<sizeof%> in %qD call is the same "
2230 "expression as the source; did you mean to "
2231 "dereference it?", callee);
2232 return;
2233 }
2234
2235 if (POINTER_TYPE_P (TREE_TYPE (src))
2236 && !strop
2237 && comp_types (TREE_TYPE (src), type)
2238 && !VOID_TYPE_P (TREE_TYPE (type)))
2239 {
2240 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2241 "argument to %<sizeof%> in %qD call is the same "
2242 "pointer type %qT as the source; expected %qT "
2243 "or an explicit length", callee, TREE_TYPE (src),
2244 TREE_TYPE (TREE_TYPE (src)));
2245 return;
2246 }
2247 }
57f872a2 2248
2249 if (dest)
2250 {
2251 if (!TYPE_P (sizeof_arg[idx])
2252 && operand_equal_p (dest, sizeof_arg[idx], 0)
2253 && comp_types (TREE_TYPE (dest), type))
2254 {
2255 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2256 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2257 "argument to %<sizeof%> in %qD call is the same "
2258 "expression as the first source; did you mean to "
2259 "remove the addressof?", callee);
2260 else if ((TYPE_PRECISION (TREE_TYPE (type))
2261 == TYPE_PRECISION (char_type_node))
2262 || strop)
2263 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2264 "argument to %<sizeof%> in %qD call is the same "
2265 "expression as the first source; did you mean to "
2266 "provide an explicit length?", callee);
2267 else
2268 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2269 "argument to %<sizeof%> in %qD call is the same "
2270 "expression as the first source; did you mean to "
2271 "dereference it?", callee);
2272 return;
2273 }
2274
2275 if (POINTER_TYPE_P (TREE_TYPE (dest))
2276 && !strop
2277 && comp_types (TREE_TYPE (dest), type)
2278 && !VOID_TYPE_P (TREE_TYPE (type)))
2279 {
2280 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2281 "argument to %<sizeof%> in %qD call is the same "
2282 "pointer type %qT as the first source; expected %qT "
2283 "or an explicit length", callee, TREE_TYPE (dest),
2284 TREE_TYPE (TREE_TYPE (dest)));
2285 return;
2286 }
2287 }
2288
2289 if (src)
2290 {
2291 if (!TYPE_P (sizeof_arg[idx])
2292 && operand_equal_p (src, sizeof_arg[idx], 0)
2293 && comp_types (TREE_TYPE (src), type))
2294 {
2295 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2296 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2297 "argument to %<sizeof%> in %qD call is the same "
2298 "expression as the second source; did you mean to "
2299 "remove the addressof?", callee);
2300 else if ((TYPE_PRECISION (TREE_TYPE (type))
2301 == TYPE_PRECISION (char_type_node))
2302 || strop)
2303 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2304 "argument to %<sizeof%> in %qD call is the same "
2305 "expression as the second source; did you mean to "
2306 "provide an explicit length?", callee);
2307 else
2308 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2309 "argument to %<sizeof%> in %qD call is the same "
2310 "expression as the second source; did you mean to "
2311 "dereference it?", callee);
2312 return;
2313 }
2314
2315 if (POINTER_TYPE_P (TREE_TYPE (src))
2316 && !strop
2317 && comp_types (TREE_TYPE (src), type)
2318 && !VOID_TYPE_P (TREE_TYPE (type)))
2319 {
2320 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2321 "argument to %<sizeof%> in %qD call is the same "
2322 "pointer type %qT as the second source; expected %qT "
2323 "or an explicit length", callee, TREE_TYPE (src),
2324 TREE_TYPE (TREE_TYPE (src)));
2325 return;
2326 }
2327 }
2328
f003f5dc 2329}
2330
3f08e399 2331/* Warn for unlikely, improbable, or stupid DECL declarations
2332 of `main'. */
2333
2334void
2335check_main_parameter_types (tree decl)
2336{
d0af78c5 2337 function_args_iterator iter;
2338 tree type;
3f08e399 2339 int argct = 0;
2340
d0af78c5 2341 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2342 {
2343 /* XXX void_type_node belies the abstraction. */
2344 if (type == void_type_node || type == error_mark_node )
2345 break;
2346
2026249a 2347 tree t = type;
2348 if (TYPE_ATOMIC (t))
2349 pedwarn (input_location, OPT_Wmain,
2350 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2351 type, decl);
2352 while (POINTER_TYPE_P (t))
2353 {
2354 t = TREE_TYPE (t);
2355 if (TYPE_ATOMIC (t))
2356 pedwarn (input_location, OPT_Wmain,
2357 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2358 type, decl);
2359 }
2360
d0af78c5 2361 ++argct;
2362 switch (argct)
2363 {
2364 case 1:
2365 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2366 pedwarn (input_location, OPT_Wmain,
2367 "first argument of %q+D should be %<int%>", decl);
2368 break;
2369
2370 case 2:
2371 if (TREE_CODE (type) != POINTER_TYPE
2372 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2373 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2374 != char_type_node))
2375 pedwarn (input_location, OPT_Wmain,
2376 "second argument of %q+D should be %<char **%>", decl);
2377 break;
2378
2379 case 3:
2380 if (TREE_CODE (type) != POINTER_TYPE
2381 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2382 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2383 != char_type_node))
2384 pedwarn (input_location, OPT_Wmain,
2385 "third argument of %q+D should probably be "
2386 "%<char **%>", decl);
2387 break;
2388 }
2389 }
3f08e399 2390
2391 /* It is intentional that this message does not mention the third
2392 argument because it's only mentioned in an appendix of the
2393 standard. */
2394 if (argct > 0 && (argct < 2 || argct > 3))
d0af78c5 2395 pedwarn (input_location, OPT_Wmain,
2396 "%q+D takes only zero or two arguments", decl);
90e645fa 2397
2398 if (stdarg_p (TREE_TYPE (decl)))
2399 pedwarn (input_location, OPT_Wmain,
2400 "%q+D declared as variadic function", decl);
3f08e399 2401}
2402
73437615 2403/* vector_targets_convertible_p is used for vector pointer types. The
2404 callers perform various checks that the qualifiers are satisfactory,
2405 while OTOH vector_targets_convertible_p ignores the number of elements
2406 in the vectors. That's fine with vector pointers as we can consider,
2407 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2408 and that does not require and conversion of the pointer values.
2409 In contrast, vector_types_convertible_p and
2410 vector_types_compatible_elements_p are used for vector value types. */
ed7c4e62 2411/* True if pointers to distinct types T1 and T2 can be converted to
2412 each other without an explicit cast. Only returns true for opaque
2413 vector types. */
2414bool
2415vector_targets_convertible_p (const_tree t1, const_tree t2)
2416{
2417 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
8d125f7d 2418 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ed7c4e62 2419 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2420 return true;
2421
2422 return false;
2423}
2424
73437615 2425/* vector_types_convertible_p is used for vector value types.
2426 It could in principle call vector_targets_convertible_p as a subroutine,
2427 but then the check for vector type would be duplicated with its callers,
2428 and also the purpose of vector_targets_convertible_p would become
2429 muddled.
2430 Where vector_types_convertible_p returns true, a conversion might still be
2431 needed to make the types match.
2432 In contrast, vector_targets_convertible_p is used for vector pointer
2433 values, and vector_types_compatible_elements_p is used specifically
2434 in the context for binary operators, as a check if use is possible without
2435 conversion. */
546c4794 2436/* True if vector types T1 and T2 can be converted to each other
2437 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2438 can only be converted with -flax-vector-conversions yet that is not
2439 in effect, emit a note telling the user about that option if such
2440 a note has not previously been emitted. */
2441bool
9f627b1a 2442vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
8b4b9810 2443{
546c4794 2444 static bool emitted_lax_note = false;
ae6db8ab 2445 bool convertible_lax;
2446
8d125f7d 2447 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ae6db8ab 2448 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2449 return true;
2450
2451 convertible_lax =
2452 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2453 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
432dd330 2454 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
ae6db8ab 2455 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2456 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
546c4794 2457
2458 if (!convertible_lax || flag_lax_vector_conversions)
2459 return convertible_lax;
2460
2461 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
29f51994 2462 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
546c4794 2463 return true;
2464
2465 if (emit_lax_note && !emitted_lax_note)
2466 {
2467 emitted_lax_note = true;
5bcc316e 2468 inform (input_location, "use -flax-vector-conversions to permit "
546c4794 2469 "conversions between vectors with differing "
2470 "element types or numbers of subparts");
2471 }
2472
2473 return false;
8b4b9810 2474}
2475
bf0cb017 2476/* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2477 and have vector types, V0 has the same type as V1, and the number of
2478 elements of V0, V1, MASK is the same.
2479
2480 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2481 called with two arguments. In this case implementation passes the
2482 first argument twice in order to share the same tree code. This fact
2483 could enable the mask-values being twice the vector length. This is
2484 an implementation accident and this semantics is not guaranteed to
2485 the user. */
2486tree
68ea4406 2487c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2488 bool complain)
bf0cb017 2489{
2490 tree ret;
2491 bool wrap = true;
2492 bool maybe_const = false;
2493 bool two_arguments = false;
2494
2495 if (v1 == NULL_TREE)
2496 {
2497 two_arguments = true;
2498 v1 = v0;
2499 }
2500
2501 if (v0 == error_mark_node || v1 == error_mark_node
2502 || mask == error_mark_node)
2503 return error_mark_node;
2504
2505 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2506 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2507 {
68ea4406 2508 if (complain)
2509 error_at (loc, "__builtin_shuffle last argument must "
2510 "be an integer vector");
bf0cb017 2511 return error_mark_node;
2512 }
2513
2514 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2515 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2516 {
68ea4406 2517 if (complain)
2518 error_at (loc, "__builtin_shuffle arguments must be vectors");
bf0cb017 2519 return error_mark_node;
2520 }
2521
2522 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2523 {
68ea4406 2524 if (complain)
2525 error_at (loc, "__builtin_shuffle argument vectors must be of "
2526 "the same type");
bf0cb017 2527 return error_mark_node;
2528 }
2529
2530 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2531 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2532 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2533 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2534 {
68ea4406 2535 if (complain)
2536 error_at (loc, "__builtin_shuffle number of elements of the "
2537 "argument vector(s) and the mask vector should "
2538 "be the same");
bf0cb017 2539 return error_mark_node;
2540 }
2541
2542 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2543 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2544 {
68ea4406 2545 if (complain)
2546 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2547 "must have the same size as inner type of the mask");
bf0cb017 2548 return error_mark_node;
2549 }
2550
2551 if (!c_dialect_cxx ())
2552 {
2553 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2554 v0 = c_fully_fold (v0, false, &maybe_const);
2555 wrap &= maybe_const;
2556
2557 if (two_arguments)
2558 v1 = v0 = save_expr (v0);
2559 else
2560 {
2561 v1 = c_fully_fold (v1, false, &maybe_const);
2562 wrap &= maybe_const;
2563 }
2564
2565 mask = c_fully_fold (mask, false, &maybe_const);
2566 wrap &= maybe_const;
2567 }
68ea4406 2568 else if (two_arguments)
2569 v1 = v0 = save_expr (v0);
bf0cb017 2570
2571 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2572
2573 if (!c_dialect_cxx () && !wrap)
2574 ret = c_wrap_maybe_const (ret, true);
2575
2576 return ret;
2577}
2578
7f506bca 2579/* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2580 to integral type. */
2581
2582static tree
2583c_common_get_narrower (tree op, int *unsignedp_ptr)
2584{
2585 op = get_narrower (op, unsignedp_ptr);
2586
2587 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2588 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2589 {
2590 /* C++0x scoped enumerations don't implicitly convert to integral
2591 type; if we stripped an explicit conversion to a larger type we
2592 need to replace it so common_type will still work. */
a51edb4c 2593 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2594 TYPE_UNSIGNED (TREE_TYPE (op)));
7f506bca 2595 op = fold_convert (type, op);
2596 }
2597 return op;
2598}
2599
2561cea2 2600/* This is a helper function of build_binary_op.
2601
2602 For certain operations if both args were extended from the same
2603 smaller type, do the arithmetic in that type and then extend.
2604
2605 BITWISE indicates a bitwise operation.
2606 For them, this optimization is safe only if
2607 both args are zero-extended or both are sign-extended.
2608 Otherwise, we might change the result.
2609 Eg, (short)-1 | (unsigned short)-1 is (int)-1
48e1416a 2610 but calculated in (unsigned short) it would be (unsigned short)-1.
2561cea2 2611*/
7f506bca 2612tree
2613shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2561cea2 2614{
2615 int unsigned0, unsigned1;
2616 tree arg0, arg1;
2617 int uns;
2618 tree type;
2619
2620 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2621 excessive narrowing when we call get_narrower below. For
2622 example, suppose that OP0 is of unsigned int extended
2623 from signed char and that RESULT_TYPE is long long int.
2624 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2625 like
48e1416a 2626
2561cea2 2627 (long long int) (unsigned int) signed_char
2628
2629 which get_narrower would narrow down to
48e1416a 2630
2561cea2 2631 (unsigned int) signed char
48e1416a 2632
2561cea2 2633 If we do not cast OP0 first, get_narrower would return
2634 signed_char, which is inconsistent with the case of the
2635 explicit cast. */
2636 op0 = convert (result_type, op0);
2637 op1 = convert (result_type, op1);
2638
7f506bca 2639 arg0 = c_common_get_narrower (op0, &unsigned0);
2640 arg1 = c_common_get_narrower (op1, &unsigned1);
ab2c1de8 2641
2561cea2 2642 /* UNS is 1 if the operation to be done is an unsigned one. */
2643 uns = TYPE_UNSIGNED (result_type);
2644
2645 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2646 but it *requires* conversion to FINAL_TYPE. */
48e1416a 2647
2561cea2 2648 if ((TYPE_PRECISION (TREE_TYPE (op0))
2649 == TYPE_PRECISION (TREE_TYPE (arg0)))
2650 && TREE_TYPE (op0) != result_type)
2651 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2652 if ((TYPE_PRECISION (TREE_TYPE (op1))
2653 == TYPE_PRECISION (TREE_TYPE (arg1)))
2654 && TREE_TYPE (op1) != result_type)
2655 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
48e1416a 2656
2561cea2 2657 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
48e1416a 2658
2561cea2 2659 /* For bitwise operations, signedness of nominal type
2660 does not matter. Consider only how operands were extended. */
2661 if (bitwise)
2662 uns = unsigned0;
48e1416a 2663
2561cea2 2664 /* Note that in all three cases below we refrain from optimizing
2665 an unsigned operation on sign-extended args.
2666 That would not be valid. */
48e1416a 2667
2561cea2 2668 /* Both args variable: if both extended in same way
2669 from same width, do it in that width.
2670 Do it unsigned if args were zero-extended. */
2671 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2672 < TYPE_PRECISION (result_type))
2673 && (TYPE_PRECISION (TREE_TYPE (arg1))
2674 == TYPE_PRECISION (TREE_TYPE (arg0)))
2675 && unsigned0 == unsigned1
2676 && (unsigned0 || !uns))
2677 return c_common_signed_or_unsigned_type
2678 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2679
2680 else if (TREE_CODE (arg0) == INTEGER_CST
2681 && (unsigned1 || !uns)
2682 && (TYPE_PRECISION (TREE_TYPE (arg1))
2683 < TYPE_PRECISION (result_type))
2684 && (type
2685 = c_common_signed_or_unsigned_type (unsigned1,
2686 TREE_TYPE (arg1)))
2687 && !POINTER_TYPE_P (type)
2688 && int_fits_type_p (arg0, type))
2689 return type;
2690
2691 else if (TREE_CODE (arg1) == INTEGER_CST
2692 && (unsigned0 || !uns)
2693 && (TYPE_PRECISION (TREE_TYPE (arg0))
2694 < TYPE_PRECISION (result_type))
2695 && (type
2696 = c_common_signed_or_unsigned_type (unsigned0,
2697 TREE_TYPE (arg0)))
2698 && !POINTER_TYPE_P (type)
2699 && int_fits_type_p (arg1, type))
2700 return type;
2701
2702 return result_type;
2703}
2704
e53013a8 2705/* Returns true iff any integer value of type FROM_TYPE can be represented as
2706 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2707
2708static bool
2709int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2710{
2711 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2712 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2713 REAL_VALUE_TYPE real_low_bound =
2714 real_value_from_int_cst (0, type_low_bound);
2715 REAL_VALUE_TYPE real_high_bound =
2716 real_value_from_int_cst (0, type_high_bound);
2717
2718 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2719 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2720}
2721
2722/* Checks if expression EXPR of complex/real/integer type cannot be converted
2723 to the complex/real/integer type TYPE. Function returns non-zero when:
22a75734 2724 * EXPR is a constant which cannot be exactly converted to TYPE.
2725 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
e53013a8 2726 for EXPR type and TYPE being both integers or both real, or both
2727 complex.
2728 * EXPR is not a constant of complex type and TYPE is a real or
2729 an integer.
22a75734 2730 * EXPR is not a constant of real type and TYPE is an integer.
2731 * EXPR is not a constant of integer type which cannot be
2732 exactly converted to real type.
e53013a8 2733
7dfa155b 2734 Function allows conversions between types of different signedness and
ca9d7d74 2735 can return SAFE_CONVERSION (zero) in that case. Function can produce
e53013a8 2736 signedness warnings if PRODUCE_WARNS is true.
2737
2738 Function allows conversions from complex constants to non-complex types,
2739 provided that imaginary part is zero and real part can be safely converted
2740 to TYPE. */
22a75734 2741
ca9d7d74 2742enum conversion_safety
22a75734 2743unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
d31d55f0 2744{
ca9d7d74 2745 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
27259707 2746 tree expr_type = TREE_TYPE (expr);
22a75734 2747 loc = expansion_point_location_if_in_system_header (loc);
d31d55f0 2748
7dfa155b 2749 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
5b16c152 2750 {
e53013a8 2751 /* If type is complex, we are interested in compatibility with
2752 underlying type. */
2753 if (TREE_CODE (type) == COMPLEX_TYPE)
2754 type = TREE_TYPE (type);
2755
d31d55f0 2756 /* Warn for real constant that is not an exact integer converted
7dfa155b 2757 to integer type. */
27259707 2758 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2759 && TREE_CODE (type) == INTEGER_TYPE)
2760 {
2761 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
ca9d7d74 2762 give_warning = UNSAFE_REAL;
7dfa155b 2763 }
da1fb07b 2764 /* Warn for an integer constant that does not fit into integer type. */
27259707 2765 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2766 && TREE_CODE (type) == INTEGER_TYPE
2767 && !int_fits_type_p (expr, type))
2768 {
2769 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
27259707 2770 && tree_int_cst_sgn (expr) < 0)
7dfa155b 2771 {
2772 if (produce_warns)
2773 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2774 " implicitly converted to unsigned type");
2775 }
2776 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2777 {
2778 if (produce_warns)
2779 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2780 " constant value to negative integer");
2781 }
7ee0d227 2782 else
ca9d7d74 2783 give_warning = UNSAFE_OTHER;
7dfa155b 2784 }
d31d55f0 2785 else if (TREE_CODE (type) == REAL_TYPE)
7dfa155b 2786 {
2787 /* Warn for an integer constant that does not fit into real type. */
2788 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2789 {
2790 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2791 if (!exact_real_truncate (TYPE_MODE (type), &a))
ca9d7d74 2792 give_warning = UNSAFE_REAL;
7dfa155b 2793 }
2794 /* Warn for a real constant that does not fit into a smaller
2795 real type. */
2796 else if (TREE_CODE (expr_type) == REAL_TYPE
2797 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2798 {
2799 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2800 if (!exact_real_truncate (TYPE_MODE (type), &a))
ca9d7d74 2801 give_warning = UNSAFE_REAL;
7dfa155b 2802 }
2803 }
2804 }
e53013a8 2805
2806 else if (TREE_CODE (expr) == COMPLEX_CST)
2807 {
2808 tree imag_part = TREE_IMAGPART (expr);
2809 /* Conversion from complex constant with zero imaginary part,
2810 perform check for conversion of real part. */
2811 if ((TREE_CODE (imag_part) == REAL_CST
2812 && real_zerop (imag_part))
2813 || (TREE_CODE (imag_part) == INTEGER_CST
2814 && integer_zerop (imag_part)))
2815 /* Note: in this branch we use recursive call to unsafe_conversion_p
2816 with different type of EXPR, but it is still safe, because when EXPR
2817 is a constant, it's type is not used in text of generated warnings
2818 (otherwise they could sound misleading). */
2819 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2820 produce_warns);
2821 /* Conversion from complex constant with non-zero imaginary part. */
2822 else
2823 {
2824 /* Conversion to complex type.
2825 Perform checks for both real and imaginary parts. */
2826 if (TREE_CODE (type) == COMPLEX_TYPE)
2827 {
2828 /* Unfortunately, produce_warns must be false in two subsequent
2829 calls of unsafe_conversion_p, because otherwise we could
2830 produce strange "double" warnings, if both real and imaginary
2831 parts have conversion problems related to signedness.
2832
2833 For example:
2834 int32_t _Complex a = 0x80000000 + 0x80000000i;
2835
2836 Possible solution: add a separate function for checking
2837 constants and combine result of two calls appropriately. */
2838 enum conversion_safety re_safety =
2839 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2840 enum conversion_safety im_safety =
2841 unsafe_conversion_p (loc, type, imag_part, false);
2842
2843 /* Merge the results into appropriate single warning. */
2844
2845 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2846 if (re_safety == im_safety)
2847 give_warning = re_safety;
2848 else if (!re_safety && im_safety)
2849 give_warning = im_safety;
2850 else if (re_safety && !im_safety)
2851 give_warning = re_safety;
2852 else
2853 give_warning = UNSAFE_OTHER;
2854 }
2855 /* Warn about conversion from complex to real or integer type. */
2856 else
2857 give_warning = UNSAFE_IMAGINARY;
2858 }
2859 }
2860
2861 /* Checks for remaining case: EXPR is not constant. */
7dfa155b 2862 else
2863 {
d31d55f0 2864 /* Warn for real types converted to integer types. */
2561cea2 2865 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2866 && TREE_CODE (type) == INTEGER_TYPE)
ca9d7d74 2867 give_warning = UNSAFE_REAL;
d31d55f0 2868
2561cea2 2869 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2870 && TREE_CODE (type) == INTEGER_TYPE)
2871 {
69609004 2872 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
f9d856a4 2873 expr = get_unwidened (expr, 0);
2561cea2 2874 expr_type = TREE_TYPE (expr);
69609004 2875
2561cea2 2876 /* Don't warn for short y; short x = ((int)y & 0xff); */
48e1416a 2877 if (TREE_CODE (expr) == BIT_AND_EXPR
7dfa155b 2878 || TREE_CODE (expr) == BIT_IOR_EXPR
2561cea2 2879 || TREE_CODE (expr) == BIT_XOR_EXPR)
2880 {
27259707 2881 /* If both args were extended from a shortest type,
2882 use that type if that is safe. */
48e1416a 2883 expr_type = shorten_binary_op (expr_type,
2884 TREE_OPERAND (expr, 0),
2885 TREE_OPERAND (expr, 1),
2561cea2 2886 /* bitwise */1);
2887
2561cea2 2888 if (TREE_CODE (expr) == BIT_AND_EXPR)
2889 {
2890 tree op0 = TREE_OPERAND (expr, 0);
2891 tree op1 = TREE_OPERAND (expr, 1);
30de145b 2892 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2893 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2894
2895 /* If one of the operands is a non-negative constant
2896 that fits in the target type, then the type of the
2897 other operand does not matter. */
2561cea2 2898 if ((TREE_CODE (op0) == INTEGER_CST
2899 && int_fits_type_p (op0, c_common_signed_type (type))
2900 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2901 || (TREE_CODE (op1) == INTEGER_CST
27259707 2902 && int_fits_type_p (op1, c_common_signed_type (type))
48e1416a 2903 && int_fits_type_p (op1,
27259707 2904 c_common_unsigned_type (type))))
ca9d7d74 2905 return SAFE_CONVERSION;
30de145b 2906 /* If constant is unsigned and fits in the target
2907 type, then the result will also fit. */
2908 else if ((TREE_CODE (op0) == INTEGER_CST
48e1416a 2909 && unsigned0
30de145b 2910 && int_fits_type_p (op0, type))
2911 || (TREE_CODE (op1) == INTEGER_CST
2912 && unsigned1
2913 && int_fits_type_p (op1, type)))
ca9d7d74 2914 return SAFE_CONVERSION;
2561cea2 2915 }
2916 }
7dfa155b 2917 /* Warn for integer types converted to smaller integer types. */
48e1416a 2918 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
ca9d7d74 2919 give_warning = UNSAFE_OTHER;
7ee0d227 2920
2921 /* When they are the same width but different signedness,
2922 then the value may change. */
7dfa155b 2923 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2561cea2 2924 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
7ee0d227 2925 /* Even when converted to a bigger type, if the type is
2926 unsigned but expr is signed, then negative values
2927 will be changed. */
7dfa155b 2928 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2929 && produce_warns)
200dd99c 2930 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2931 "may change the sign of the result",
2932 type, expr_type);
7dfa155b 2933 }
d31d55f0 2934
2935 /* Warn for integer types converted to real types if and only if
7dfa155b 2936 all the range of values of the integer type cannot be
2937 represented by the real type. */
2561cea2 2938 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2939 && TREE_CODE (type) == REAL_TYPE)
2940 {
4c2cfa81 2941 /* Don't warn about char y = 0xff; float x = (int) y; */
2942 expr = get_unwidened (expr, 0);
2943 expr_type = TREE_TYPE (expr);
2944
e53013a8 2945 if (!int_safely_convertible_to_real_p (expr_type, type))
ca9d7d74 2946 give_warning = UNSAFE_OTHER;
7dfa155b 2947 }
d31d55f0 2948
2949 /* Warn for real types converted to smaller real types. */
2561cea2 2950 else if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2951 && TREE_CODE (type) == REAL_TYPE
2952 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
ca9d7d74 2953 give_warning = UNSAFE_REAL;
e53013a8 2954
2955 /* Check conversion between two complex types. */
2956 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2957 && TREE_CODE (type) == COMPLEX_TYPE)
2958 {
2959 /* Extract underlying types (i.e., type of real and imaginary
2960 parts) of expr_type and type. */
2961 tree from_type = TREE_TYPE (expr_type);
2962 tree to_type = TREE_TYPE (type);
2963
2964 /* Warn for real types converted to integer types. */
2965 if (TREE_CODE (from_type) == REAL_TYPE
2966 && TREE_CODE (to_type) == INTEGER_TYPE)
2967 give_warning = UNSAFE_REAL;
2968
2969 /* Warn for real types converted to smaller real types. */
2970 else if (TREE_CODE (from_type) == REAL_TYPE
2971 && TREE_CODE (to_type) == REAL_TYPE
2972 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2973 give_warning = UNSAFE_REAL;
2974
2975 /* Check conversion for complex integer types. Here implementation
2976 is simpler than for real-domain integers because it does not
2977 involve sophisticated cases, such as bitmasks, casts, etc. */
2978 else if (TREE_CODE (from_type) == INTEGER_TYPE
2979 && TREE_CODE (to_type) == INTEGER_TYPE)
2980 {
2981 /* Warn for integer types converted to smaller integer types. */
2982 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2983 give_warning = UNSAFE_OTHER;
2984
2985 /* Check for different signedness, see case for real-domain
2986 integers (above) for a more detailed comment. */
2987 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
2988 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
2989 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
2990 && produce_warns)
2991 warning_at (loc, OPT_Wsign_conversion,
2992 "conversion to %qT from %qT "
2993 "may change the sign of the result",
2994 type, expr_type);
2995 }
2996 else if (TREE_CODE (from_type) == INTEGER_TYPE
2997 && TREE_CODE (to_type) == REAL_TYPE
2998 && !int_safely_convertible_to_real_p (from_type, to_type))
2999 give_warning = UNSAFE_OTHER;
3000 }
3001
3002 /* Warn for complex types converted to real or integer types. */
3003 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
3004 && TREE_CODE (type) != COMPLEX_TYPE)
3005 give_warning = UNSAFE_IMAGINARY;
7dfa155b 3006 }
3007
3008 return give_warning;
3009}
3010
3011/* Warns if the conversion of EXPR to TYPE may alter a value.
3012 This is a helper function for warnings_for_convert_and_check. */
3013
3014static void
22a75734 3015conversion_warning (location_t loc, tree type, tree expr)
7dfa155b 3016{
7dfa155b 3017 tree expr_type = TREE_TYPE (expr);
ca9d7d74 3018 enum conversion_safety conversion_kind;
d31d55f0 3019
ca9d7d74 3020 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
7dfa155b 3021 return;
d31d55f0 3022
ec704957 3023 /* This may happen, because for LHS op= RHS we preevaluate
3024 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
3025 means we could no longer see the code of the EXPR. */
3026 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
3027 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
3028 if (TREE_CODE (expr) == SAVE_EXPR)
3029 expr = TREE_OPERAND (expr, 0);
3030
7dfa155b 3031 switch (TREE_CODE (expr))
3032 {
3033 case EQ_EXPR:
3034 case NE_EXPR:
3035 case LE_EXPR:
3036 case GE_EXPR:
3037 case LT_EXPR:
3038 case GT_EXPR:
3039 case TRUTH_ANDIF_EXPR:
3040 case TRUTH_ORIF_EXPR:
3041 case TRUTH_AND_EXPR:
3042 case TRUTH_OR_EXPR:
3043 case TRUTH_XOR_EXPR:
3044 case TRUTH_NOT_EXPR:
3045 /* Conversion from boolean to a signed:1 bit-field (which only
3046 can hold the values 0 and -1) doesn't lose information - but
3047 it does change the value. */
3048 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
3049 warning_at (loc, OPT_Wconversion,
3050 "conversion to %qT from boolean expression", type);
3051 return;
3052
3053 case REAL_CST:
3054 case INTEGER_CST:
e53013a8 3055 case COMPLEX_CST:
22a75734 3056 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
ca9d7d74 3057 if (conversion_kind == UNSAFE_REAL)
3058 warning_at (loc, OPT_Wfloat_conversion,
3059 "conversion to %qT alters %qT constant value",
3060 type, expr_type);
3061 else if (conversion_kind)
7dfa155b 3062 warning_at (loc, OPT_Wconversion,
3063 "conversion to %qT alters %qT constant value",
3064 type, expr_type);
3065 return;
3066
3067 case COND_EXPR:
3068 {
0e4e775a 3069 /* In case of COND_EXPR, we do not care about the type of
3070 COND_EXPR, only about the conversion of each operand. */
3071 tree op1 = TREE_OPERAND (expr, 1);
3072 tree op2 = TREE_OPERAND (expr, 2);
3073
22a75734 3074 conversion_warning (loc, type, op1);
3075 conversion_warning (loc, type, op2);
0e4e775a 3076 return;
7dfa155b 3077 }
3078
3079 default: /* 'expr' is not a constant. */
22a75734 3080 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
ca9d7d74 3081 if (conversion_kind == UNSAFE_REAL)
3082 warning_at (loc, OPT_Wfloat_conversion,
3083 "conversion to %qT from %qT may alter its value",
3084 type, expr_type);
e53013a8 3085 else if (conversion_kind == UNSAFE_IMAGINARY)
3086 warning_at (loc, OPT_Wconversion,
3087 "conversion to %qT from %qT discards imaginary component",
3088 type, expr_type);
ca9d7d74 3089 else if (conversion_kind)
7dfa155b 3090 warning_at (loc, OPT_Wconversion,
200dd99c 3091 "conversion to %qT from %qT may alter its value",
3092 type, expr_type);
d31d55f0 3093 }
3094}
3095
59dd8856 3096/* Produce warnings after a conversion. RESULT is the result of
3097 converting EXPR to TYPE. This is a helper function for
3098 convert_and_check and cp_convert_and_check. */
2a1736ed 3099
59dd8856 3100void
22a75734 3101warnings_for_convert_and_check (location_t loc, tree type, tree expr,
3102 tree result)
2a1736ed 3103{
22a75734 3104 loc = expansion_point_location_if_in_system_header (loc);
61f69bc9 3105
da1fb07b 3106 if (TREE_CODE (expr) == INTEGER_CST
3107 && (TREE_CODE (type) == INTEGER_TYPE
3108 || TREE_CODE (type) == ENUMERAL_TYPE)
3109 && !int_fits_type_p (expr, type))
3110 {
d31d55f0 3111 /* Do not diagnose overflow in a constant expression merely
3112 because a conversion overflowed. */
da1fb07b 3113 if (TREE_OVERFLOW (result))
eddad94a 3114 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
3115
da1fb07b 3116 if (TYPE_UNSIGNED (type))
d31d55f0 3117 {
da1fb07b 3118 /* This detects cases like converting -129 or 256 to
3119 unsigned char. */
3120 if (!int_fits_type_p (expr, c_common_signed_type (type)))
61f69bc9 3121 warning_at (loc, OPT_Woverflow,
3122 "large integer implicitly truncated to unsigned type");
7ee0d227 3123 else
22a75734 3124 conversion_warning (loc, type, expr);
da1fb07b 3125 }
48e1416a 3126 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
22a75734 3127 warning_at (loc, OPT_Woverflow,
e0913805 3128 "overflow in implicit constant conversion");
3129 /* No warning for converting 0x80000000 to int. */
3130 else if (pedantic
3131 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
3132 || TYPE_PRECISION (TREE_TYPE (expr))
3133 != TYPE_PRECISION (type)))
61f69bc9 3134 warning_at (loc, OPT_Woverflow,
3135 "overflow in implicit constant conversion");
e0913805 3136
7ee0d227 3137 else
22a75734 3138 conversion_warning (loc, type, expr);
2a1736ed 3139 }
9421ebb9 3140 else if ((TREE_CODE (result) == INTEGER_CST
3141 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
61f69bc9 3142 warning_at (loc, OPT_Woverflow,
3143 "overflow in implicit constant conversion");
7ee0d227 3144 else
22a75734 3145 conversion_warning (loc, type, expr);
59dd8856 3146}
3147
3148
3149/* Convert EXPR to TYPE, warning about conversion problems with constants.
3150 Invoke this function on every expression that is converted implicitly,
3151 i.e. because of language rules and not because of an explicit cast. */
3152
3153tree
22a75734 3154convert_and_check (location_t loc, tree type, tree expr)
59dd8856 3155{
3156 tree result;
c6418a4e 3157 tree expr_for_warning;
3158
3159 /* Convert from a value with possible excess precision rather than
3160 via the semantic type, but do not warn about values not fitting
3161 exactly in the semantic type. */
3162 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
3163 {
3164 tree orig_type = TREE_TYPE (expr);
3165 expr = TREE_OPERAND (expr, 0);
3166 expr_for_warning = convert (orig_type, expr);
3167 if (orig_type == type)
3168 return expr_for_warning;
3169 }
3170 else
3171 expr_for_warning = expr;
59dd8856 3172
3173 if (TREE_TYPE (expr) == type)
3174 return expr;
48e1416a 3175
59dd8856 3176 result = convert (type, expr);
3177
48d94ede 3178 if (c_inhibit_evaluation_warnings == 0
3179 && !TREE_OVERFLOW_P (expr)
3180 && result != error_mark_node)
22a75734 3181 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
59dd8856 3182
da1fb07b 3183 return result;
b2806639 3184}
3185\f
4e91a871 3186/* A node in a list that describes references to variables (EXPR), which are
3187 either read accesses if WRITER is zero, or write accesses, in which case
3188 WRITER is the parent of EXPR. */
3189struct tlist
3190{
3191 struct tlist *next;
3192 tree expr, writer;
3193};
3194
3195/* Used to implement a cache the results of a call to verify_tree. We only
3196 use this for SAVE_EXPRs. */
3197struct tlist_cache
3198{
3199 struct tlist_cache *next;
3200 struct tlist *cache_before_sp;
3201 struct tlist *cache_after_sp;
3202 tree expr;
481c6ce6 3203};
3204
4e91a871 3205/* Obstack to use when allocating tlist structures, and corresponding
3206 firstobj. */
3207static struct obstack tlist_obstack;
3208static char *tlist_firstobj = 0;
3209
3210/* Keep track of the identifiers we've warned about, so we can avoid duplicate
3211 warnings. */
3212static struct tlist *warned_ids;
3213/* SAVE_EXPRs need special treatment. We process them only once and then
3214 cache the results. */
3215static struct tlist_cache *save_expr_cache;
3216
1cae46be 3217static void add_tlist (struct tlist **, struct tlist *, tree, int);
3218static void merge_tlist (struct tlist **, struct tlist *, int);
3219static void verify_tree (tree, struct tlist **, struct tlist **, tree);
3220static int warning_candidate_p (tree);
79973b57 3221static bool candidate_equal_p (const_tree, const_tree);
1cae46be 3222static void warn_for_collisions (struct tlist *);
3223static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
3224static struct tlist *new_tlist (struct tlist *, tree, tree);
481c6ce6 3225
4e91a871 3226/* Create a new struct tlist and fill in its fields. */
3227static struct tlist *
1cae46be 3228new_tlist (struct tlist *next, tree t, tree writer)
4e91a871 3229{
3230 struct tlist *l;
9318f22c 3231 l = XOBNEW (&tlist_obstack, struct tlist);
4e91a871 3232 l->next = next;
3233 l->expr = t;
3234 l->writer = writer;
3235 return l;
3236}
3237
3238/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3239 is nonnull, we ignore any node we find which has a writer equal to it. */
3240
3241static void
1cae46be 3242add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
4e91a871 3243{
3244 while (add)
3245 {
3246 struct tlist *next = add->next;
84166705 3247 if (!copy)
4e91a871 3248 add->next = *to;
79973b57 3249 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
4e91a871 3250 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3251 add = next;
3252 }
3253}
3254
3255/* Merge the nodes of ADD into TO. This merging process is done so that for
3256 each variable that already exists in TO, no new node is added; however if
3257 there is a write access recorded in ADD, and an occurrence on TO is only
3258 a read access, then the occurrence in TO will be modified to record the
3259 write. */
481c6ce6 3260
3261static void
1cae46be 3262merge_tlist (struct tlist **to, struct tlist *add, int copy)
4e91a871 3263{
3264 struct tlist **end = to;
3265
3266 while (*end)
3267 end = &(*end)->next;
3268
3269 while (add)
3270 {
3271 int found = 0;
3272 struct tlist *tmp2;
3273 struct tlist *next = add->next;
3274
3275 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
79973b57 3276 if (candidate_equal_p (tmp2->expr, add->expr))
4e91a871 3277 {
3278 found = 1;
84166705 3279 if (!tmp2->writer)
4e91a871 3280 tmp2->writer = add->writer;
3281 }
84166705 3282 if (!found)
4e91a871 3283 {
312243bb 3284 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
4e91a871 3285 end = &(*end)->next;
3286 *end = 0;
3287 }
3288 add = next;
3289 }
3290}
3291
3292/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3293 references in list LIST conflict with it, excluding reads if ONLY writers
3294 is nonzero. */
3295
3296static void
1cae46be 3297warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3298 int only_writes)
4e91a871 3299{
3300 struct tlist *tmp;
3301
3302 /* Avoid duplicate warnings. */
3303 for (tmp = warned_ids; tmp; tmp = tmp->next)
79973b57 3304 if (candidate_equal_p (tmp->expr, written))
4e91a871 3305 return;
3306
3307 while (list)
3308 {
79973b57 3309 if (candidate_equal_p (list->expr, written)
3310 && !candidate_equal_p (list->writer, writer)
3311 && (!only_writes || list->writer))
4e91a871 3312 {
3313 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3df42822 3314 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
6513b50d 3315 OPT_Wsequence_point, "operation on %qE may be undefined",
3316 list->expr);
4e91a871 3317 }
3318 list = list->next;
3319 }
3320}
3321
3322/* Given a list LIST of references to variables, find whether any of these
3323 can cause conflicts due to missing sequence points. */
3324
3325static void
1cae46be 3326warn_for_collisions (struct tlist *list)
4e91a871 3327{
3328 struct tlist *tmp;
1cae46be 3329
4e91a871 3330 for (tmp = list; tmp; tmp = tmp->next)
3331 {
3332 if (tmp->writer)
3333 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3334 }
3335}
3336
734c98be 3337/* Return nonzero if X is a tree that can be verified by the sequence point
4e91a871 3338 warnings. */
3339static int
1cae46be 3340warning_candidate_p (tree x)
481c6ce6 3341{
6ef8d12f 3342 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3343 return 0;
3344
027fc6ef 3345 if (TREE_CODE (x) == BLOCK)
3346 return 0;
3347
6ef8d12f 3348 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
79973b57 3349 (lvalue_p) crash on TRY/CATCH. */
6ef8d12f 3350 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3351 return 0;
3352
3353 if (!lvalue_p (x))
3354 return 0;
3355
3356 /* No point to track non-const calls, they will never satisfy
3357 operand_equal_p. */
3358 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3359 return 0;
3360
3361 if (TREE_CODE (x) == STRING_CST)
3362 return 0;
3363
3364 return 1;
79973b57 3365}
3366
3367/* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3368static bool
3369candidate_equal_p (const_tree x, const_tree y)
3370{
3371 return (x == y) || (x && y && operand_equal_p (x, y, 0));
4e91a871 3372}
481c6ce6 3373
4e91a871 3374/* Walk the tree X, and record accesses to variables. If X is written by the
3375 parent tree, WRITER is the parent.
3376 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3377 expression or its only operand forces a sequence point, then everything up
3378 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3379 in PNO_SP.
3380 Once we return, we will have emitted warnings if any subexpression before
3381 such a sequence point could be undefined. On a higher level, however, the
3382 sequence point may not be relevant, and we'll merge the two lists.
3383
3384 Example: (b++, a) + b;
3385 The call that processes the COMPOUND_EXPR will store the increment of B
3386 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3387 processes the PLUS_EXPR will need to merge the two lists so that
3388 eventually, all accesses end up on the same list (and we'll warn about the
3389 unordered subexpressions b++ and b.
3390
3391 A note on merging. If we modify the former example so that our expression
3392 becomes
3393 (b++, b) + a
3394 care must be taken not simply to add all three expressions into the final
3395 PNO_SP list. The function merge_tlist takes care of that by merging the
3396 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3397 way, so that no more than one access to B is recorded. */
481c6ce6 3398
4e91a871 3399static void
1cae46be 3400verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3401 tree writer)
4e91a871 3402{
3403 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3404 enum tree_code code;
ce45a448 3405 enum tree_code_class cl;
481c6ce6 3406
e5b75768 3407 /* X may be NULL if it is the operand of an empty statement expression
3408 ({ }). */
3409 if (x == NULL)
3410 return;
3411
4e91a871 3412 restart:
3413 code = TREE_CODE (x);
e916c70c 3414 cl = TREE_CODE_CLASS (code);
481c6ce6 3415
4e91a871 3416 if (warning_candidate_p (x))
79973b57 3417 *pno_sp = new_tlist (*pno_sp, x, writer);
4e91a871 3418
3419 switch (code)
3420 {
67b28e3e 3421 case CONSTRUCTOR:
8e71dad2 3422 case SIZEOF_EXPR:
67b28e3e 3423 return;
3424
4e91a871 3425 case COMPOUND_EXPR:
3426 case TRUTH_ANDIF_EXPR:
3427 case TRUTH_ORIF_EXPR:
3428 tmp_before = tmp_nosp = tmp_list3 = 0;
3429 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3430 warn_for_collisions (tmp_nosp);
3431 merge_tlist (pbefore_sp, tmp_before, 0);
3432 merge_tlist (pbefore_sp, tmp_nosp, 0);
3433 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3434 merge_tlist (pbefore_sp, tmp_list3, 0);
3435 return;
3436
3437 case COND_EXPR:
3438 tmp_before = tmp_list2 = 0;
3439 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3440 warn_for_collisions (tmp_list2);
3441 merge_tlist (pbefore_sp, tmp_before, 0);
312243bb 3442 merge_tlist (pbefore_sp, tmp_list2, 0);
4e91a871 3443
3444 tmp_list3 = tmp_nosp = 0;
3445 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3446 warn_for_collisions (tmp_nosp);
3447 merge_tlist (pbefore_sp, tmp_list3, 0);
3448
3449 tmp_list3 = tmp_list2 = 0;
3450 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3451 warn_for_collisions (tmp_list2);
3452 merge_tlist (pbefore_sp, tmp_list3, 0);
3453 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3454 two first, to avoid warning for (a ? b++ : b++). */
3455 merge_tlist (&tmp_nosp, tmp_list2, 0);
3456 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3457 return;
3458
481c6ce6 3459 case PREDECREMENT_EXPR:
3460 case PREINCREMENT_EXPR:
3461 case POSTDECREMENT_EXPR:
3462 case POSTINCREMENT_EXPR:
4e91a871 3463 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3464 return;
3465
3466 case MODIFY_EXPR:
3467 tmp_before = tmp_nosp = tmp_list3 = 0;
3468 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3469 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3470 /* Expressions inside the LHS are not ordered wrt. the sequence points
3471 in the RHS. Example:
3472 *a = (a++, 2)
3473 Despite the fact that the modification of "a" is in the before_sp
3474 list (tmp_before), it conflicts with the use of "a" in the LHS.
3475 We can handle this by adding the contents of tmp_list3
3476 to those of tmp_before, and redoing the collision warnings for that
3477 list. */
3478 add_tlist (&tmp_before, tmp_list3, x, 1);
3479 warn_for_collisions (tmp_before);
3480 /* Exclude the LHS itself here; we first have to merge it into the
3481 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3482 didn't exclude the LHS, we'd get it twice, once as a read and once
3483 as a write. */
3484 add_tlist (pno_sp, tmp_list3, x, 0);
3485 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3486
3487 merge_tlist (pbefore_sp, tmp_before, 0);
3488 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3489 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3490 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3491 return;
481c6ce6 3492
3493 case CALL_EXPR:
4e91a871 3494 /* We need to warn about conflicts among arguments and conflicts between
3495 args and the function address. Side effects of the function address,
3496 however, are not ordered by the sequence point of the call. */
c2f47e15 3497 {
3498 call_expr_arg_iterator iter;
3499 tree arg;
48e1416a 3500 tmp_before = tmp_nosp = 0;
c2f47e15 3501 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3502 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3503 {
3504 tmp_list2 = tmp_list3 = 0;
3505 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3506 merge_tlist (&tmp_list3, tmp_list2, 0);
3507 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3508 }
3509 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3510 warn_for_collisions (tmp_before);
3511 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3512 return;
3513 }
481c6ce6 3514
3515 case TREE_LIST:
3516 /* Scan all the list, e.g. indices of multi dimensional array. */
3517 while (x)
3518 {
4e91a871 3519 tmp_before = tmp_nosp = 0;
3520 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3521 merge_tlist (&tmp_nosp, tmp_before, 0);
3522 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
481c6ce6 3523 x = TREE_CHAIN (x);
3524 }
4e91a871 3525 return;
481c6ce6 3526
4e91a871 3527 case SAVE_EXPR:
3528 {
3529 struct tlist_cache *t;
3530 for (t = save_expr_cache; t; t = t->next)
79973b57 3531 if (candidate_equal_p (t->expr, x))
4e91a871 3532 break;
481c6ce6 3533
84166705 3534 if (!t)
481c6ce6 3535 {
9318f22c 3536 t = XOBNEW (&tlist_obstack, struct tlist_cache);
4e91a871 3537 t->next = save_expr_cache;
3538 t->expr = x;
3539 save_expr_cache = t;
3540
3541 tmp_before = tmp_nosp = 0;
3542 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3543 warn_for_collisions (tmp_nosp);
3544
3545 tmp_list3 = 0;
312243bb 3546 merge_tlist (&tmp_list3, tmp_nosp, 0);
4e91a871 3547 t->cache_before_sp = tmp_before;
3548 t->cache_after_sp = tmp_list3;
481c6ce6 3549 }
4e91a871 3550 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3551 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3552 return;
3553 }
481c6ce6 3554
012916cb 3555 case ADDR_EXPR:
3556 x = TREE_OPERAND (x, 0);
3557 if (DECL_P (x))
3558 return;
3559 writer = 0;
3560 goto restart;
3561
ce45a448 3562 default:
3563 /* For other expressions, simply recurse on their operands.
a0c938f0 3564 Manual tail recursion for unary expressions.
ce45a448 3565 Other non-expressions need not be processed. */
3566 if (cl == tcc_unary)
3567 {
ce45a448 3568 x = TREE_OPERAND (x, 0);
3569 writer = 0;
3570 goto restart;
3571 }
3572 else if (IS_EXPR_CODE_CLASS (cl))
3573 {
3574 int lp;
c2f47e15 3575 int max = TREE_OPERAND_LENGTH (x);
ce45a448 3576 for (lp = 0; lp < max; lp++)
3577 {
3578 tmp_before = tmp_nosp = 0;
3579 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3580 merge_tlist (&tmp_nosp, tmp_before, 0);
3581 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3582 }
3583 }
3584 return;
481c6ce6 3585 }
481c6ce6 3586}
3587
974e2c0c 3588/* Try to warn for undefined behavior in EXPR due to missing sequence
481c6ce6 3589 points. */
3590
4b987fac 3591DEBUG_FUNCTION void
1cae46be 3592verify_sequence_points (tree expr)
481c6ce6 3593{
4e91a871 3594 struct tlist *before_sp = 0, *after_sp = 0;
481c6ce6 3595
4e91a871 3596 warned_ids = 0;
3597 save_expr_cache = 0;
3598 if (tlist_firstobj == 0)
481c6ce6 3599 {
4e91a871 3600 gcc_obstack_init (&tlist_obstack);
4fd61bc6 3601 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
481c6ce6 3602 }
3603
4e91a871 3604 verify_tree (expr, &before_sp, &after_sp, 0);
3605 warn_for_collisions (after_sp);
3606 obstack_free (&tlist_obstack, tlist_firstobj);
481c6ce6 3607}
b0fc3e72 3608\f
3609/* Validate the expression after `case' and apply default promotions. */
3610
2ca392fd 3611static tree
2d2f6a15 3612check_case_value (location_t loc, tree value)
b0fc3e72 3613{
3614 if (value == NULL_TREE)
3615 return value;
3616
b96dc121 3617 if (TREE_CODE (value) == INTEGER_CST)
3618 /* Promote char or short to int. */
3619 value = perform_integral_promotions (value);
3620 else if (value != error_mark_node)
b0fc3e72 3621 {
2d2f6a15 3622 error_at (loc, "case label does not reduce to an integer constant");
b0fc3e72 3623 value = error_mark_node;
3624 }
b0fc3e72 3625
6433f1c2 3626 constant_expression_warning (value);
3627
b0fc3e72 3628 return value;
3629}
3630\f
2ca392fd 3631/* See if the case values LOW and HIGH are in the range of the original
5c9dae64 3632 type (i.e. before the default conversion to int) of the switch testing
2ca392fd 3633 expression.
3634 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
91275768 3635 the type before promoting it. CASE_LOW_P is a pointer to the lower
2ca392fd 3636 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3637 if the case is not a case range.
3638 The caller has to make sure that we are not called with NULL for
5c9dae64 3639 CASE_LOW_P (i.e. the default case).
442e3cb9 3640 Returns true if the case label is in range of ORIG_TYPE (saturated or
2ca392fd 3641 untouched) or false if the label is out of range. */
3642
3643static bool
f61a9bc2 3644check_case_bounds (location_t loc, tree type, tree orig_type,
2ca392fd 3645 tree *case_low_p, tree *case_high_p)
3646{
3647 tree min_value, max_value;
3648 tree case_low = *case_low_p;
3649 tree case_high = case_high_p ? *case_high_p : case_low;
3650
3651 /* If there was a problem with the original type, do nothing. */
3652 if (orig_type == error_mark_node)
3653 return true;
3654
3655 min_value = TYPE_MIN_VALUE (orig_type);
3656 max_value = TYPE_MAX_VALUE (orig_type);
3657
3658 /* Case label is less than minimum for type. */
3659 if (tree_int_cst_compare (case_low, min_value) < 0
3660 && tree_int_cst_compare (case_high, min_value) < 0)
3661 {
f61a9bc2 3662 warning_at (loc, 0, "case label value is less than minimum value "
3663 "for type");
2ca392fd 3664 return false;
3665 }
b27ac6b5 3666
2ca392fd 3667 /* Case value is greater than maximum for type. */
3668 if (tree_int_cst_compare (case_low, max_value) > 0
3669 && tree_int_cst_compare (case_high, max_value) > 0)
3670 {
f61a9bc2 3671 warning_at (loc, 0, "case label value exceeds maximum value for type");
2ca392fd 3672 return false;
3673 }
3674
3675 /* Saturate lower case label value to minimum. */
3676 if (tree_int_cst_compare (case_high, min_value) >= 0
3677 && tree_int_cst_compare (case_low, min_value) < 0)
3678 {
f61a9bc2 3679 warning_at (loc, 0, "lower value in case label range"
3680 " less than minimum value for type");
2ca392fd 3681 case_low = min_value;
3682 }
b27ac6b5 3683
2ca392fd 3684 /* Saturate upper case label value to maximum. */
3685 if (tree_int_cst_compare (case_low, max_value) <= 0
3686 && tree_int_cst_compare (case_high, max_value) > 0)
3687 {
f61a9bc2 3688 warning_at (loc, 0, "upper value in case label range"
3689 " exceeds maximum value for type");
2ca392fd 3690 case_high = max_value;
3691 }
3692
3693 if (*case_low_p != case_low)
3694 *case_low_p = convert (type, case_low);
3695 if (case_high_p && *case_high_p != case_high)
3696 *case_high_p = convert (type, case_high);
3697
3698 return true;
3699}
3700\f
b0fc3e72 3701/* Return an integer type with BITS bits of precision,
3702 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3703
3704tree
1cae46be 3705c_common_type_for_size (unsigned int bits, int unsignedp)
b0fc3e72 3706{
9f75f026 3707 int i;
3708
46375237 3709 if (bits == TYPE_PRECISION (integer_type_node))
3710 return unsignedp ? unsigned_type_node : integer_type_node;
3711
bacde65a 3712 if (bits == TYPE_PRECISION (signed_char_type_node))
b0fc3e72 3713 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3714
bacde65a 3715 if (bits == TYPE_PRECISION (short_integer_type_node))
b0fc3e72 3716 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3717
bacde65a 3718 if (bits == TYPE_PRECISION (long_integer_type_node))
b0fc3e72 3719 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3720
bacde65a 3721 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b0fc3e72 3722 return (unsignedp ? long_long_unsigned_type_node
3723 : long_long_integer_type_node);
3724
9f75f026 3725 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3726 if (int_n_enabled_p[i]
3727 && bits == int_n_data[i].bitsize)
3728 return (unsignedp ? int_n_trees[i].unsigned_type
3729 : int_n_trees[i].signed_type);
6388cfe2 3730
f57fa2ea 3731 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3732 return (unsignedp ? widest_unsigned_literal_type_node
3733 : widest_integer_literal_type_node);
3734
bacde65a 3735 if (bits <= TYPE_PRECISION (intQI_type_node))
3736 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3737
3738 if (bits <= TYPE_PRECISION (intHI_type_node))
3739 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3740
3741 if (bits <= TYPE_PRECISION (intSI_type_node))
3742 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3743
3744 if (bits <= TYPE_PRECISION (intDI_type_node))
3745 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3746
b0fc3e72 3747 return 0;
3748}
3749
9421ebb9 3750/* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3751 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3752 and saturating if SATP is nonzero, otherwise not saturating. */
3753
3754tree
3755c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3756 int unsignedp, int satp)
3757{
3754d046 3758 machine_mode mode;
9421ebb9 3759 if (ibit == 0)
3760 mode = unsignedp ? UQQmode : QQmode;
3761 else
3762 mode = unsignedp ? UHAmode : HAmode;
3763
3764 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3765 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3766 break;
3767
3768 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3769 {
3770 sorry ("GCC cannot support operators with integer types and "
3771 "fixed-point types that have too many integral and "
3772 "fractional bits together");
3773 return 0;
3774 }
3775
3776 return c_common_type_for_mode (mode, satp);
3777}
3778
5b247e9f 3779/* Used for communication between c_common_type_for_mode and
3780 c_register_builtin_type. */
c1917557 3781tree registered_builtin_types;
5b247e9f 3782
b0fc3e72 3783/* Return a data type that has machine mode MODE.
3784 If the mode is an integer,
9421ebb9 3785 then UNSIGNEDP selects between signed and unsigned types.
3786 If the mode is a fixed-point mode,
3787 then UNSIGNEDP selects between saturating and nonsaturating types. */
b0fc3e72 3788
3789tree
3754d046 3790c_common_type_for_mode (machine_mode mode, int unsignedp)
b0fc3e72 3791{
5b247e9f 3792 tree t;
9f75f026 3793 int i;
5b247e9f 3794
46375237 3795 if (mode == TYPE_MODE (integer_type_node))
3796 return unsignedp ? unsigned_type_node : integer_type_node;
3797
b0fc3e72 3798 if (mode == TYPE_MODE (signed_char_type_node))
3799 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3800
3801 if (mode == TYPE_MODE (short_integer_type_node))
3802 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3803
b0fc3e72 3804 if (mode == TYPE_MODE (long_integer_type_node))
3805 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3806
3807 if (mode == TYPE_MODE (long_long_integer_type_node))
3808 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3809
9f75f026 3810 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3811 if (int_n_enabled_p[i]
3812 && mode == int_n_data[i].m)
3813 return (unsignedp ? int_n_trees[i].unsigned_type
3814 : int_n_trees[i].signed_type);
6388cfe2 3815
f57fa2ea 3816 if (mode == TYPE_MODE (widest_integer_literal_type_node))
44e9fa65 3817 return unsignedp ? widest_unsigned_literal_type_node
4ee9c684 3818 : widest_integer_literal_type_node;
f57fa2ea 3819
88ae7f04 3820 if (mode == QImode)
bacde65a 3821 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3822
88ae7f04 3823 if (mode == HImode)
bacde65a 3824 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3825
88ae7f04 3826 if (mode == SImode)
bacde65a 3827 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3828
88ae7f04 3829 if (mode == DImode)
bacde65a 3830 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
ab2c1de8 3831
cc1cc1c7 3832#if HOST_BITS_PER_WIDE_INT >= 64
6274009c 3833 if (mode == TYPE_MODE (intTI_type_node))
3834 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
cc1cc1c7 3835#endif
6274009c 3836
b0fc3e72 3837 if (mode == TYPE_MODE (float_type_node))
3838 return float_type_node;
3839
3840 if (mode == TYPE_MODE (double_type_node))
3841 return double_type_node;
3842
3843 if (mode == TYPE_MODE (long_double_type_node))
3844 return long_double_type_node;
3845
545c2bde 3846 if (mode == TYPE_MODE (void_type_node))
3847 return void_type_node;
b27ac6b5 3848
b0fc3e72 3849 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
61b9b73c 3850 return (unsignedp
3851 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3852 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3853
3854 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
61b9b73c 3855 return (unsignedp
3856 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3857 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3858
0dfc45b5 3859 if (COMPLEX_MODE_P (mode))
3860 {
3754d046 3861 machine_mode inner_mode;
0dfc45b5 3862 tree inner_type;
3863
3864 if (mode == TYPE_MODE (complex_float_type_node))
3865 return complex_float_type_node;
3866 if (mode == TYPE_MODE (complex_double_type_node))
3867 return complex_double_type_node;
3868 if (mode == TYPE_MODE (complex_long_double_type_node))
3869 return complex_long_double_type_node;
3870
3871 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3872 return complex_integer_type_node;
3873
3874 inner_mode = GET_MODE_INNER (mode);
3875 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3876 if (inner_type != NULL_TREE)
3877 return build_complex_type (inner_type);
3878 }
3879 else if (VECTOR_MODE_P (mode))
4917c376 3880 {
3754d046 3881 machine_mode inner_mode = GET_MODE_INNER (mode);
4917c376 3882 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3883 if (inner_type != NULL_TREE)
3884 return build_vector_type_for_mode (inner_type, mode);
88ae7f04 3885 }
e2ea7e3a 3886
c4503c0a 3887 if (mode == TYPE_MODE (dfloat32_type_node))
3888 return dfloat32_type_node;
3889 if (mode == TYPE_MODE (dfloat64_type_node))
3890 return dfloat64_type_node;
3891 if (mode == TYPE_MODE (dfloat128_type_node))
3892 return dfloat128_type_node;
3893
9421ebb9 3894 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3895 {
3896 if (mode == TYPE_MODE (short_fract_type_node))
3897 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3898 if (mode == TYPE_MODE (fract_type_node))
3899 return unsignedp ? sat_fract_type_node : fract_type_node;
3900 if (mode == TYPE_MODE (long_fract_type_node))
3901 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3902 if (mode == TYPE_MODE (long_long_fract_type_node))
3903 return unsignedp ? sat_long_long_fract_type_node
3904 : long_long_fract_type_node;
3905
3906 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3907 return unsignedp ? sat_unsigned_short_fract_type_node
3908 : unsigned_short_fract_type_node;
3909 if (mode == TYPE_MODE (unsigned_fract_type_node))
3910 return unsignedp ? sat_unsigned_fract_type_node
3911 : unsigned_fract_type_node;
3912 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3913 return unsignedp ? sat_unsigned_long_fract_type_node
3914 : unsigned_long_fract_type_node;
3915 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3916 return unsignedp ? sat_unsigned_long_long_fract_type_node
3917 : unsigned_long_long_fract_type_node;
3918
3919 if (mode == TYPE_MODE (short_accum_type_node))
3920 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3921 if (mode == TYPE_MODE (accum_type_node))
3922 return unsignedp ? sat_accum_type_node : accum_type_node;
3923 if (mode == TYPE_MODE (long_accum_type_node))
3924 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3925 if (mode == TYPE_MODE (long_long_accum_type_node))
3926 return unsignedp ? sat_long_long_accum_type_node
3927 : long_long_accum_type_node;
3928
3929 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3930 return unsignedp ? sat_unsigned_short_accum_type_node
3931 : unsigned_short_accum_type_node;
3932 if (mode == TYPE_MODE (unsigned_accum_type_node))
3933 return unsignedp ? sat_unsigned_accum_type_node
3934 : unsigned_accum_type_node;
3935 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3936 return unsignedp ? sat_unsigned_long_accum_type_node
3937 : unsigned_long_accum_type_node;
3938 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3939 return unsignedp ? sat_unsigned_long_long_accum_type_node
3940 : unsigned_long_long_accum_type_node;
3941
3942 if (mode == QQmode)
3943 return unsignedp ? sat_qq_type_node : qq_type_node;
3944 if (mode == HQmode)
3945 return unsignedp ? sat_hq_type_node : hq_type_node;
3946 if (mode == SQmode)
3947 return unsignedp ? sat_sq_type_node : sq_type_node;
3948 if (mode == DQmode)
3949 return unsignedp ? sat_dq_type_node : dq_type_node;
3950 if (mode == TQmode)
3951 return unsignedp ? sat_tq_type_node : tq_type_node;
3952
3953 if (mode == UQQmode)
3954 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3955 if (mode == UHQmode)
3956 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3957 if (mode == USQmode)
3958 return unsignedp ? sat_usq_type_node : usq_type_node;
3959 if (mode == UDQmode)
3960 return unsignedp ? sat_udq_type_node : udq_type_node;
3961 if (mode == UTQmode)
3962 return unsignedp ? sat_utq_type_node : utq_type_node;
3963
3964 if (mode == HAmode)
3965 return unsignedp ? sat_ha_type_node : ha_type_node;
3966 if (mode == SAmode)
3967 return unsignedp ? sat_sa_type_node : sa_type_node;
3968 if (mode == DAmode)
3969 return unsignedp ? sat_da_type_node : da_type_node;
3970 if (mode == TAmode)
3971 return unsignedp ? sat_ta_type_node : ta_type_node;
3972
3973 if (mode == UHAmode)
3974 return unsignedp ? sat_uha_type_node : uha_type_node;
3975 if (mode == USAmode)
3976 return unsignedp ? sat_usa_type_node : usa_type_node;
3977 if (mode == UDAmode)
3978 return unsignedp ? sat_uda_type_node : uda_type_node;
3979 if (mode == UTAmode)
3980 return unsignedp ? sat_uta_type_node : uta_type_node;
3981 }
3982
5b247e9f 3983 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
baec58e1 3984 if (TYPE_MODE (TREE_VALUE (t)) == mode
3985 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
5b247e9f 3986 return TREE_VALUE (t);
3987
b0fc3e72 3988 return 0;
3989}
20d39783 3990
11773141 3991tree
3992c_common_unsigned_type (tree type)
3993{
3994 return c_common_signed_or_unsigned_type (1, type);
3995}
3996
20d39783 3997/* Return a signed type the same as TYPE in other respects. */
3998
3999tree
1cae46be 4000c_common_signed_type (tree type)
20d39783 4001{
4070745f 4002 return c_common_signed_or_unsigned_type (0, type);
20d39783 4003}
4004
4005/* Return a type the same as TYPE except unsigned or
4006 signed according to UNSIGNEDP. */
4007
4008tree
1cae46be 4009c_common_signed_or_unsigned_type (int unsignedp, tree type)
20d39783 4010{
7a91101f 4011 tree type1;
9f75f026 4012 int i;
20d39783 4013
7a91101f 4014 /* This block of code emulates the behavior of the old
4015 c_common_unsigned_type. In particular, it returns
4016 long_unsigned_type_node if passed a long, even when a int would
4017 have the same size. This is necessary for warnings to work
4018 correctly in archs where sizeof(int) == sizeof(long) */
4019
4020 type1 = TYPE_MAIN_VARIANT (type);
4021 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
4022 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4023 if (type1 == integer_type_node || type1 == unsigned_type_node)
4024 return unsignedp ? unsigned_type_node : integer_type_node;
4025 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
4026 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4027 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
4028 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4029 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
4030 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
9f75f026 4031
4032 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4033 if (int_n_enabled_p[i]
4034 && (type1 == int_n_trees[i].unsigned_type
4035 || type1 == int_n_trees[i].signed_type))
4036 return (unsignedp ? int_n_trees[i].unsigned_type
4037 : int_n_trees[i].signed_type);
4038
7a91101f 4039 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
4040 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
4041#if HOST_BITS_PER_WIDE_INT >= 64
4042 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
4043 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4044#endif
4045 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
4046 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4047 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
4048 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4049 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
4050 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4051 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
4052 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4053
9f92e1a2 4054#define C_COMMON_FIXED_TYPES(NAME) \
4055 if (type1 == short_ ## NAME ## _type_node \
4056 || type1 == unsigned_short_ ## NAME ## _type_node) \
4057 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
4058 : short_ ## NAME ## _type_node; \
4059 if (type1 == NAME ## _type_node \
4060 || type1 == unsigned_ ## NAME ## _type_node) \
4061 return unsignedp ? unsigned_ ## NAME ## _type_node \
4062 : NAME ## _type_node; \
4063 if (type1 == long_ ## NAME ## _type_node \
4064 || type1 == unsigned_long_ ## NAME ## _type_node) \
4065 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
4066 : long_ ## NAME ## _type_node; \
4067 if (type1 == long_long_ ## NAME ## _type_node \
4068 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
4069 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
4070 : long_long_ ## NAME ## _type_node;
4071
4072#define C_COMMON_FIXED_MODE_TYPES(NAME) \
4073 if (type1 == NAME ## _type_node \
4074 || type1 == u ## NAME ## _type_node) \
4075 return unsignedp ? u ## NAME ## _type_node \
4076 : NAME ## _type_node;
4077
4078#define C_COMMON_FIXED_TYPES_SAT(NAME) \
4079 if (type1 == sat_ ## short_ ## NAME ## _type_node \
4080 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
4081 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
4082 : sat_ ## short_ ## NAME ## _type_node; \
4083 if (type1 == sat_ ## NAME ## _type_node \
4084 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
4085 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
4086 : sat_ ## NAME ## _type_node; \
4087 if (type1 == sat_ ## long_ ## NAME ## _type_node \
4088 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
4089 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
4090 : sat_ ## long_ ## NAME ## _type_node; \
4091 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
4092 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
4093 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
4094 : sat_ ## long_long_ ## NAME ## _type_node;
4095
4096#define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
4097 if (type1 == sat_ ## NAME ## _type_node \
4098 || type1 == sat_ ## u ## NAME ## _type_node) \
4099 return unsignedp ? sat_ ## u ## NAME ## _type_node \
4100 : sat_ ## NAME ## _type_node;
4101
4102 C_COMMON_FIXED_TYPES (fract);
4103 C_COMMON_FIXED_TYPES_SAT (fract);
4104 C_COMMON_FIXED_TYPES (accum);
4105 C_COMMON_FIXED_TYPES_SAT (accum);
4106
4107 C_COMMON_FIXED_MODE_TYPES (qq);
4108 C_COMMON_FIXED_MODE_TYPES (hq);
4109 C_COMMON_FIXED_MODE_TYPES (sq);
4110 C_COMMON_FIXED_MODE_TYPES (dq);
4111 C_COMMON_FIXED_MODE_TYPES (tq);
4112 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
4113 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
4114 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
4115 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
4116 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
4117 C_COMMON_FIXED_MODE_TYPES (ha);
4118 C_COMMON_FIXED_MODE_TYPES (sa);
4119 C_COMMON_FIXED_MODE_TYPES (da);
4120 C_COMMON_FIXED_MODE_TYPES (ta);
4121 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
4122 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
4123 C_COMMON_FIXED_MODE_TYPES_SAT (da);
4124 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
9421ebb9 4125
4f7f7efd 4126 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
4127 the precision; they have precision set to match their range, but
4128 may use a wider mode to match an ABI. If we change modes, we may
4129 wind up with bad conversions. For INTEGER_TYPEs in C, must check
4130 the precision as well, so as to yield correct results for
4131 bit-field types. C++ does not have these separate bit-field
4132 types, and producing a signed or unsigned variant of an
4133 ENUMERAL_TYPE may cause other problems as well. */
4134
ac265864 4135 if (!INTEGRAL_TYPE_P (type)
4136 || TYPE_UNSIGNED (type) == unsignedp)
4137 return type;
4138
4f7f7efd 4139#define TYPE_OK(node) \
4140 (TYPE_MODE (type) == TYPE_MODE (node) \
0c4abe5b 4141 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
4f7f7efd 4142 if (TYPE_OK (signed_char_type_node))
20d39783 4143 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4f7f7efd 4144 if (TYPE_OK (integer_type_node))
20d39783 4145 return unsignedp ? unsigned_type_node : integer_type_node;
4f7f7efd 4146 if (TYPE_OK (short_integer_type_node))
20d39783 4147 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4f7f7efd 4148 if (TYPE_OK (long_integer_type_node))
20d39783 4149 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4f7f7efd 4150 if (TYPE_OK (long_long_integer_type_node))
20d39783 4151 return (unsignedp ? long_long_unsigned_type_node
4152 : long_long_integer_type_node);
9f75f026 4153
4154 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4155 if (int_n_enabled_p[i]
4156 && TYPE_MODE (type) == int_n_data[i].m
4157 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
4158 return (unsignedp ? int_n_trees[i].unsigned_type
4159 : int_n_trees[i].signed_type);
4160
4f7f7efd 4161 if (TYPE_OK (widest_integer_literal_type_node))
20d39783 4162 return (unsignedp ? widest_unsigned_literal_type_node
4163 : widest_integer_literal_type_node);
ef11801e 4164
4165#if HOST_BITS_PER_WIDE_INT >= 64
4f7f7efd 4166 if (TYPE_OK (intTI_type_node))
ef11801e 4167 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4168#endif
4f7f7efd 4169 if (TYPE_OK (intDI_type_node))
ef11801e 4170 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4f7f7efd 4171 if (TYPE_OK (intSI_type_node))
ef11801e 4172 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4f7f7efd 4173 if (TYPE_OK (intHI_type_node))
ef11801e 4174 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4f7f7efd 4175 if (TYPE_OK (intQI_type_node))
ef11801e 4176 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4f7f7efd 4177#undef TYPE_OK
ef11801e 4178
0c4abe5b 4179 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
20d39783 4180}
b268e47e 4181
c0e47fd4 4182/* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
4183
4184tree
4185c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
4186{
9f75f026 4187 int i;
4188
c0e47fd4 4189 /* Extended integer types of the same width as a standard type have
4190 lesser rank, so those of the same width as int promote to int or
4191 unsigned int and are valid for printf formats expecting int or
4192 unsigned int. To avoid such special cases, avoid creating
4193 extended integer types for bit-fields if a standard integer type
4194 is available. */
4195 if (width == TYPE_PRECISION (integer_type_node))
4196 return unsignedp ? unsigned_type_node : integer_type_node;
4197 if (width == TYPE_PRECISION (signed_char_type_node))
4198 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4199 if (width == TYPE_PRECISION (short_integer_type_node))
4200 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4201 if (width == TYPE_PRECISION (long_integer_type_node))
4202 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4203 if (width == TYPE_PRECISION (long_long_integer_type_node))
4204 return (unsignedp ? long_long_unsigned_type_node
4205 : long_long_integer_type_node);
9f75f026 4206 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4207 if (int_n_enabled_p[i]
4208 && width == int_n_data[i].bitsize)
4209 return (unsignedp ? int_n_trees[i].unsigned_type
4210 : int_n_trees[i].signed_type);
c0e47fd4 4211 return build_nonstandard_integer_type (width, unsignedp);
4212}
4213
b268e47e 4214/* The C version of the register_builtin_type langhook. */
4215
4216void
4217c_register_builtin_type (tree type, const char* name)
4218{
4219 tree decl;
4220
e60a6f7b 4221 decl = build_decl (UNKNOWN_LOCATION,
4222 TYPE_DECL, get_identifier (name), type);
b268e47e 4223 DECL_ARTIFICIAL (decl) = 1;
4224 if (!TYPE_NAME (type))
4225 TYPE_NAME (type) = decl;
4226 pushdecl (decl);
5b247e9f 4227
4228 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
b268e47e 4229}
a9b9d10c 4230\f
aff9e656 4231/* Print an error message for invalid operands to arith operation
8e70fb09 4232 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4233 LOCATION is the location of the message. */
b0fc3e72 4234
4235void
8e70fb09 4236binary_op_error (location_t location, enum tree_code code,
4237 tree type0, tree type1)
b0fc3e72 4238{
19cb6b50 4239 const char *opname;
f03946e4 4240
b0fc3e72 4241 switch (code)
4242 {
b0fc3e72 4243 case PLUS_EXPR:
4244 opname = "+"; break;
4245 case MINUS_EXPR:
4246 opname = "-"; break;
4247 case MULT_EXPR:
4248 opname = "*"; break;
4249 case MAX_EXPR:
4250 opname = "max"; break;
4251 case MIN_EXPR:
4252 opname = "min"; break;
4253 case EQ_EXPR:
4254 opname = "=="; break;
4255 case NE_EXPR:
4256 opname = "!="; break;
4257 case LE_EXPR:
4258 opname = "<="; break;
4259 case GE_EXPR:
4260 opname = ">="; break;
4261 case LT_EXPR:
4262 opname = "<"; break;
4263 case GT_EXPR:
4264 opname = ">"; break;
4265 case LSHIFT_EXPR:
4266 opname = "<<"; break;
4267 case RSHIFT_EXPR:
4268 opname = ">>"; break;
4269 case TRUNC_MOD_EXPR:
66618a1e 4270 case FLOOR_MOD_EXPR:
b0fc3e72 4271 opname = "%"; break;
4272 case TRUNC_DIV_EXPR:
66618a1e 4273 case FLOOR_DIV_EXPR:
b0fc3e72 4274 opname = "/"; break;
4275 case BIT_AND_EXPR:
4276 opname = "&"; break;
4277 case BIT_IOR_EXPR:
4278 opname = "|"; break;
4279 case TRUTH_ANDIF_EXPR:
4280 opname = "&&"; break;
4281 case TRUTH_ORIF_EXPR:
4282 opname = "||"; break;
4283 case BIT_XOR_EXPR:
4284 opname = "^"; break;
31f820d2 4285 default:
315ba355 4286 gcc_unreachable ();
b0fc3e72 4287 }
8e70fb09 4288 error_at (location,
4289 "invalid operands to binary %s (have %qT and %qT)", opname,
4290 type0, type1);
b0fc3e72 4291}
4292\f
03fe1dc2 4293/* Given an expression as a tree, return its original type. Do this
4294 by stripping any conversion that preserves the sign and precision. */
4295static tree
4296expr_original_type (tree expr)
4297{
4298 STRIP_SIGN_NOPS (expr);
4299 return TREE_TYPE (expr);
4300}
4301
b0fc3e72 4302/* Subroutine of build_binary_op, used for comparison operations.
4303 See if the operands have both been converted from subword integer types
4304 and, if so, perhaps change them both back to their original type.
5b511807 4305 This function is also responsible for converting the two operands
4306 to the proper common type for comparison.
b0fc3e72 4307
4308 The arguments of this function are all pointers to local variables
4309 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4310 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4311
2623625f 4312 LOC is the location of the comparison.
4313
b0fc3e72 4314 If this function returns nonzero, it means that the comparison has
4315 a constant value. What this function returns is an expression for
4316 that value. */
4317
4318tree
2623625f 4319shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4320 tree *restype_ptr, enum tree_code *rescode_ptr)
b0fc3e72 4321{
19cb6b50 4322 tree type;
b0fc3e72 4323 tree op0 = *op0_ptr;
4324 tree op1 = *op1_ptr;
4325 int unsignedp0, unsignedp1;
4326 int real1, real2;
4327 tree primop0, primop1;
4328 enum tree_code code = *rescode_ptr;
4329
4330 /* Throw away any conversions to wider types
4331 already present in the operands. */
4332
7f506bca 4333 primop0 = c_common_get_narrower (op0, &unsignedp0);
4334 primop1 = c_common_get_narrower (op1, &unsignedp1);
b0fc3e72 4335
119d06b2 4336 /* If primopN is first sign-extended from primopN's precision to opN's
4337 precision, then zero-extended from opN's precision to
4338 *restype_ptr precision, shortenings might be invalid. */
4339 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4340 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4341 && !unsignedp0
4342 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4343 primop0 = op0;
4344 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4345 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4346 && !unsignedp1
4347 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4348 primop1 = op1;
4349
b0fc3e72 4350 /* Handle the case that OP0 does not *contain* a conversion
4351 but it *requires* conversion to FINAL_TYPE. */
4352
4353 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
78a8ed03 4354 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
b0fc3e72 4355 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
78a8ed03 4356 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b0fc3e72 4357
4358 /* If one of the operands must be floated, we cannot optimize. */
4359 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4360 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
ab2c1de8 4361
b0fc3e72 4362 /* If first arg is constant, swap the args (changing operation
2bd278cc 4363 so value is preserved), for canonicalization. Don't do this if
4364 the second arg is 0. */
b0fc3e72 4365
2bd278cc 4366 if (TREE_CONSTANT (primop0)
9421ebb9 4367 && !integer_zerop (primop1) && !real_zerop (primop1)
4368 && !fixed_zerop (primop1))
b0fc3e72 4369 {
dfcf26a5 4370 std::swap (primop0, primop1);
4371 std::swap (op0, op1);
b0fc3e72 4372 *op0_ptr = op0;
4373 *op1_ptr = op1;
dfcf26a5 4374 std::swap (unsignedp0, unsignedp1);
4375 std::swap (real1, real2);
b0fc3e72 4376
4377 switch (code)
4378 {
4379 case LT_EXPR:
4380 code = GT_EXPR;
4381 break;
4382 case GT_EXPR:
4383 code = LT_EXPR;
4384 break;
4385 case LE_EXPR:
4386 code = GE_EXPR;
4387 break;
4388 case GE_EXPR:
4389 code = LE_EXPR;
4390 break;
31f820d2 4391 default:
4392 break;
b0fc3e72 4393 }
4394 *rescode_ptr = code;
4395 }
4396
4397 /* If comparing an integer against a constant more bits wide,
4398 maybe we can deduce a value of 1 or 0 independent of the data.
4399 Or else truncate the constant now
4400 rather than extend the variable at run time.
4401
4402 This is only interesting if the constant is the wider arg.
4403 Also, it is not safe if the constant is unsigned and the
4404 variable arg is signed, since in this case the variable
4405 would be sign-extended and then regarded as unsigned.
4406 Our technique fails in this case because the lowest/highest
4407 possible unsigned results don't follow naturally from the
4408 lowest/highest possible values of the variable operand.
4409 For just EQ_EXPR and NE_EXPR there is another technique that
4410 could be used: see if the constant can be faithfully represented
4411 in the other operand's type, by truncating it and reextending it
4412 and see if that preserves the constant's value. */
4413
4414 if (!real1 && !real2
9421ebb9 4415 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
b0fc3e72 4416 && TREE_CODE (primop1) == INTEGER_CST
4417 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4418 {
4419 int min_gt, max_gt, min_lt, max_lt;
4420 tree maxval, minval;
4421 /* 1 if comparison is nominally unsigned. */
78a8ed03 4422 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
b0fc3e72 4423 tree val;
4424
4070745f 4425 type = c_common_signed_or_unsigned_type (unsignedp0,
4426 TREE_TYPE (primop0));
cda09c61 4427
b0fc3e72 4428 maxval = TYPE_MAX_VALUE (type);
4429 minval = TYPE_MIN_VALUE (type);
4430
4431 if (unsignedp && !unsignedp0)
4070745f 4432 *restype_ptr = c_common_signed_type (*restype_ptr);
b0fc3e72 4433
4434 if (TREE_TYPE (primop1) != *restype_ptr)
18dbec6f 4435 {
9a5e8086 4436 /* Convert primop1 to target type, but do not introduce
4437 additional overflow. We know primop1 is an int_cst. */
e913b5cd 4438 primop1 = force_fit_type (*restype_ptr,
796b6678 4439 wide_int::from
4440 (primop1,
4441 TYPE_PRECISION (*restype_ptr),
4442 TYPE_SIGN (TREE_TYPE (primop1))),
e913b5cd 4443 0, TREE_OVERFLOW (primop1));
18dbec6f 4444 }
b0fc3e72 4445 if (type != *restype_ptr)
4446 {
4447 minval = convert (*restype_ptr, minval);
4448 maxval = convert (*restype_ptr, maxval);
4449 }
4450
d99d10ca 4451 min_gt = tree_int_cst_lt (primop1, minval);
4452 max_gt = tree_int_cst_lt (primop1, maxval);
4453 min_lt = tree_int_cst_lt (minval, primop1);
4454 max_lt = tree_int_cst_lt (maxval, primop1);
b0fc3e72 4455
4456 val = 0;
4457 /* This used to be a switch, but Genix compiler can't handle that. */
4458 if (code == NE_EXPR)
4459 {
4460 if (max_lt || min_gt)
3c2239cf 4461 val = truthvalue_true_node;
b0fc3e72 4462 }
4463 else if (code == EQ_EXPR)
4464 {
4465 if (max_lt || min_gt)
3c2239cf 4466 val = truthvalue_false_node;
b0fc3e72 4467 }
4468 else if (code == LT_EXPR)
4469 {
4470 if (max_lt)
3c2239cf 4471 val = truthvalue_true_node;
b0fc3e72 4472 if (!min_lt)
3c2239cf 4473 val = truthvalue_false_node;
b0fc3e72 4474 }
4475 else if (code == GT_EXPR)
4476 {
4477 if (min_gt)
3c2239cf 4478 val = truthvalue_true_node;
b0fc3e72 4479 if (!max_gt)
3c2239cf 4480 val = truthvalue_false_node;
b0fc3e72 4481 }
4482 else if (code == LE_EXPR)
4483 {
4484 if (!max_gt)
3c2239cf 4485 val = truthvalue_true_node;
b0fc3e72 4486 if (min_gt)
3c2239cf 4487 val = truthvalue_false_node;
b0fc3e72 4488 }
4489 else if (code == GE_EXPR)
4490 {
4491 if (!min_lt)
3c2239cf 4492 val = truthvalue_true_node;
b0fc3e72 4493 if (max_lt)
3c2239cf 4494 val = truthvalue_false_node;
b0fc3e72 4495 }
4496
4497 /* If primop0 was sign-extended and unsigned comparison specd,
4498 we did a signed comparison above using the signed type bounds.
4499 But the comparison we output must be unsigned.
4500
4501 Also, for inequalities, VAL is no good; but if the signed
4502 comparison had *any* fixed result, it follows that the
4503 unsigned comparison just tests the sign in reverse
4504 (positive values are LE, negative ones GE).
4505 So we can generate an unsigned comparison
4506 against an extreme value of the signed type. */
4507
4508 if (unsignedp && !unsignedp0)
4509 {
4510 if (val != 0)
4511 switch (code)
4512 {
4513 case LT_EXPR:
4514 case GE_EXPR:
4515 primop1 = TYPE_MIN_VALUE (type);
4516 val = 0;
4517 break;
4518
4519 case LE_EXPR:
4520 case GT_EXPR:
4521 primop1 = TYPE_MAX_VALUE (type);
4522 val = 0;
4523 break;
31f820d2 4524
4525 default:
4526 break;
b0fc3e72 4527 }
11773141 4528 type = c_common_unsigned_type (type);
b0fc3e72 4529 }
4530
734ec290 4531 if (TREE_CODE (primop0) != INTEGER_CST)
b0fc3e72 4532 {
3c2239cf 4533 if (val == truthvalue_false_node)
03fe1dc2 4534 warning_at (loc, OPT_Wtype_limits,
4535 "comparison is always false due to limited range of data type");
3c2239cf 4536 if (val == truthvalue_true_node)
03fe1dc2 4537 warning_at (loc, OPT_Wtype_limits,
4538 "comparison is always true due to limited range of data type");
b0fc3e72 4539 }
4540
4541 if (val != 0)
4542 {
4543 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4544 if (TREE_SIDE_EFFECTS (primop0))
14ae0310 4545 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
b0fc3e72 4546 return val;
4547 }
4548
4549 /* Value is not predetermined, but do the comparison
4550 in the type of the operand that is not constant.
4551 TYPE is already properly set. */
4552 }
c4503c0a 4553
4554 /* If either arg is decimal float and the other is float, find the
4555 proper common type to use for comparison. */
7fd22aae 4556 else if (real1 && real2
4557 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4558 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4559 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4560
4561 /* If either arg is decimal float and the other is float, fail. */
c4503c0a 4562 else if (real1 && real2
4563 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4564 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
7fd22aae 4565 return 0;
c4503c0a 4566
b0fc3e72 4567 else if (real1 && real2
2203bd5c 4568 && (TYPE_PRECISION (TREE_TYPE (primop0))
4569 == TYPE_PRECISION (TREE_TYPE (primop1))))
b0fc3e72 4570 type = TREE_TYPE (primop0);
4571
4572 /* If args' natural types are both narrower than nominal type
4573 and both extend in the same manner, compare them
4574 in the type of the wider arg.
4575 Otherwise must actually extend both to the nominal
4576 common type lest different ways of extending
4577 alter the result.
4578 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4579
4580 else if (unsignedp0 == unsignedp1 && real1 == real2
4581 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4582 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4583 {
4584 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4070745f 4585 type = c_common_signed_or_unsigned_type (unsignedp0
78a8ed03 4586 || TYPE_UNSIGNED (*restype_ptr),
4070745f 4587 type);
b0fc3e72 4588 /* Make sure shorter operand is extended the right way
4589 to match the longer operand. */
4070745f 4590 primop0
4591 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4592 TREE_TYPE (primop0)),
4593 primop0);
4594 primop1
4595 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4596 TREE_TYPE (primop1)),
4597 primop1);
b0fc3e72 4598 }
4599 else
4600 {
4601 /* Here we must do the comparison on the nominal type
4602 using the args exactly as we received them. */
4603 type = *restype_ptr;
4604 primop0 = op0;
4605 primop1 = op1;
4606
4607 if (!real1 && !real2 && integer_zerop (primop1)
78a8ed03 4608 && TYPE_UNSIGNED (*restype_ptr))
b0fc3e72 4609 {
4610 tree value = 0;
03fe1dc2 4611 /* All unsigned values are >= 0, so we warn. However,
4612 if OP0 is a constant that is >= 0, the signedness of
4613 the comparison isn't an issue, so suppress the
4614 warning. */
4615 bool warn =
3df42822 4616 warn_type_limits && !in_system_header_at (loc)
03fe1dc2 4617 && !(TREE_CODE (primop0) == INTEGER_CST
4618 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4619 primop0)))
4620 /* Do not warn for enumeration types. */
4621 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4622
b0fc3e72 4623 switch (code)
4624 {
4625 case GE_EXPR:
03fe1dc2 4626 if (warn)
4627 warning_at (loc, OPT_Wtype_limits,
4628 "comparison of unsigned expression >= 0 is always true");
3c2239cf 4629 value = truthvalue_true_node;
b0fc3e72 4630 break;
4631
4632 case LT_EXPR:
03fe1dc2 4633 if (warn)
4634 warning_at (loc, OPT_Wtype_limits,
4635 "comparison of unsigned expression < 0 is always false");
3c2239cf 4636 value = truthvalue_false_node;
31f820d2 4637 break;
4638
4639 default:
4640 break;
b0fc3e72 4641 }
4642
4643 if (value != 0)
4644 {
4645 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4646 if (TREE_SIDE_EFFECTS (primop0))
14ae0310 4647 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4648 primop0, value);
b0fc3e72 4649 return value;
4650 }
4651 }
4652 }
4653
4654 *op0_ptr = convert (type, primop0);
4655 *op1_ptr = convert (type, primop1);
4656
3c2239cf 4657 *restype_ptr = truthvalue_type_node;
b0fc3e72 4658
4659 return 0;
4660}
4661\f
1c26100f 4662/* Return a tree for the sum or difference (RESULTCODE says which)
4663 of pointer PTROP and integer INTOP. */
4664
4665tree
389dd41b 4666pointer_int_sum (location_t loc, enum tree_code resultcode,
42f9a786 4667 tree ptrop, tree intop, bool complain)
1c26100f 4668{
add6ee5e 4669 tree size_exp, ret;
1c26100f 4670
1c26100f 4671 /* The result is a pointer of the same type that is being added. */
1c26100f 4672 tree result_type = TREE_TYPE (ptrop);
4673
4674 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4675 {
42f9a786 4676 if (complain && warn_pointer_arith)
4677 pedwarn (loc, OPT_Wpointer_arith,
4678 "pointer of type %<void *%> used in arithmetic");
4679 else if (!complain)
4680 return error_mark_node;
1c26100f 4681 size_exp = integer_one_node;
4682 }
4683 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4684 {
42f9a786 4685 if (complain && warn_pointer_arith)
4686 pedwarn (loc, OPT_Wpointer_arith,
4687 "pointer to a function used in arithmetic");
4688 else if (!complain)
4689 return error_mark_node;
1c26100f 4690 size_exp = integer_one_node;
4691 }
1c26100f 4692 else
4693 size_exp = size_in_bytes (TREE_TYPE (result_type));
4694
add6ee5e 4695 /* We are manipulating pointer values, so we don't need to warn
4696 about relying on undefined signed overflow. We disable the
4697 warning here because we use integer types so fold won't know that
4698 they are really pointers. */
4699 fold_defer_overflow_warnings ();
4700
1c26100f 4701 /* If what we are about to multiply by the size of the elements
4702 contains a constant term, apply distributive law
4703 and multiply that constant term separately.
4704 This helps produce common subexpressions. */
1c26100f 4705 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
84166705 4706 && !TREE_CONSTANT (intop)
1c26100f 4707 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4708 && TREE_CONSTANT (size_exp)
4709 /* If the constant comes from pointer subtraction,
4710 skip this optimization--it would cause an error. */
4711 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4712 /* If the constant is unsigned, and smaller than the pointer size,
4713 then we must skip this optimization. This is because it could cause
4714 an overflow error if the constant is negative but INTOP is not. */
84166705 4715 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
1c26100f 4716 || (TYPE_PRECISION (TREE_TYPE (intop))
4717 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4718 {
4719 enum tree_code subcode = resultcode;
4720 tree int_type = TREE_TYPE (intop);
4721 if (TREE_CODE (intop) == MINUS_EXPR)
4722 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4723 /* Convert both subexpression types to the type of intop,
4724 because weird cases involving pointer arithmetic
4725 can result in a sum or difference with different type args. */
8e70fb09 4726 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4727 subcode, ptrop,
1c26100f 4728 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4729 intop = convert (int_type, TREE_OPERAND (intop, 0));
4730 }
4731
4732 /* Convert the integer argument to a type the same size as sizetype
4733 so the multiply won't overflow spuriously. */
1c26100f 4734 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
78a8ed03 4735 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
1cae46be 4736 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
78a8ed03 4737 TYPE_UNSIGNED (sizetype)), intop);
1c26100f 4738
4739 /* Replace the integer argument with a suitable product by the object size.
c6feb9f1 4740 Do this multiplication as signed, then convert to the appropriate type
8032877c 4741 for the pointer operation and disregard an overflow that occurred only
c6feb9f1 4742 because of the sign-extension change in the latter conversion. */
4743 {
4744 tree t = build_binary_op (loc,
4745 MULT_EXPR, intop,
4746 convert (TREE_TYPE (intop), size_exp), 1);
4747 intop = convert (sizetype, t);
4748 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
e913b5cd 4749 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
c6feb9f1 4750 }
0de36bdb 4751
499e523f 4752 /* Create the sum or difference. */
0de36bdb 4753 if (resultcode == MINUS_EXPR)
389dd41b 4754 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
1c26100f 4755
2cc66f2a 4756 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
add6ee5e 4757
4758 fold_undefer_and_ignore_overflow_warnings ();
4759
4760 return ret;
1c26100f 4761}
4762\f
f59e3889 4763/* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4764 and if NON_CONST is known not to be permitted in an evaluated part
4765 of a constant expression. */
4766
4767tree
4768c_wrap_maybe_const (tree expr, bool non_const)
4769{
4770 bool nowarning = TREE_NO_WARNING (expr);
4771 location_t loc = EXPR_LOCATION (expr);
4772
4773 /* This should never be called for C++. */
4774 if (c_dialect_cxx ())
4775 gcc_unreachable ();
4776
4777 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4778 STRIP_TYPE_NOPS (expr);
4779 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4780 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4781 if (nowarning)
4782 TREE_NO_WARNING (expr) = 1;
4783 protected_set_expr_location (expr, loc);
4784
4785 return expr;
4786}
4787
a75b1c71 4788/* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4789 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4790 around the SAVE_EXPR if needed so that c_fully_fold does not need
4791 to look inside SAVE_EXPRs. */
4792
4793tree
4794c_save_expr (tree expr)
4795{
4796 bool maybe_const = true;
4797 if (c_dialect_cxx ())
4798 return save_expr (expr);
4799 expr = c_fully_fold (expr, false, &maybe_const);
4800 expr = save_expr (expr);
4801 if (!maybe_const)
f59e3889 4802 expr = c_wrap_maybe_const (expr, true);
a75b1c71 4803 return expr;
4804}
4805
6b68e71a 4806/* Return whether EXPR is a declaration whose address can never be
4807 NULL. */
4808
4809bool
9f627b1a 4810decl_with_nonnull_addr_p (const_tree expr)
6b68e71a 4811{
4812 return (DECL_P (expr)
4813 && (TREE_CODE (expr) == PARM_DECL
4814 || TREE_CODE (expr) == LABEL_DECL
4815 || !DECL_WEAK (expr)));
4816}
4817
b0fc3e72 4818/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
45a78cc0 4819 or for an `if' or `while' statement or ?..: exp. It should already
4820 have been validated to be of suitable type; otherwise, a bad
4821 diagnostic may result.
b0fc3e72 4822
8e70fb09 4823 The EXPR is located at LOCATION.
4824
b0fc3e72 4825 This preparation consists of taking the ordinary
4826 representation of an expression expr and producing a valid tree
4827 boolean expression describing whether expr is nonzero. We could
3c2239cf 4828 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
b0fc3e72 4829 but we optimize comparisons, &&, ||, and !.
4830
3c2239cf 4831 The resulting type should always be `truthvalue_type_node'. */
b0fc3e72 4832
4833tree
8e70fb09 4834c_common_truthvalue_conversion (location_t location, tree expr)
b0fc3e72 4835{
b0fc3e72 4836 switch (TREE_CODE (expr))
4837 {
318a728f 4838 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
e1232ce2 4839 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4840 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4841 case ORDERED_EXPR: case UNORDERED_EXPR:
ce04dcdc 4842 if (TREE_TYPE (expr) == truthvalue_type_node)
4843 return expr;
e60a6f7b 4844 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
ce04dcdc 4845 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
e60a6f7b 4846 goto ret;
ce04dcdc 4847
b0fc3e72 4848 case TRUTH_ANDIF_EXPR:
4849 case TRUTH_ORIF_EXPR:
4850 case TRUTH_AND_EXPR:
4851 case TRUTH_OR_EXPR:
31f6e93c 4852 case TRUTH_XOR_EXPR:
ce04dcdc 4853 if (TREE_TYPE (expr) == truthvalue_type_node)
4854 return expr;
e60a6f7b 4855 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
48e1416a 4856 c_common_truthvalue_conversion (location,
e60a6f7b 4857 TREE_OPERAND (expr, 0)),
4858 c_common_truthvalue_conversion (location,
4859 TREE_OPERAND (expr, 1)));
4860 goto ret;
3e851b85 4861
f6e28f72 4862 case TRUTH_NOT_EXPR:
ce04dcdc 4863 if (TREE_TYPE (expr) == truthvalue_type_node)
4864 return expr;
e60a6f7b 4865 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4866 c_common_truthvalue_conversion (location,
4867 TREE_OPERAND (expr, 0)));
4868 goto ret;
f6e28f72 4869
b0fc3e72 4870 case ERROR_MARK:
4871 return expr;
ab2c1de8 4872
b0fc3e72 4873 case INTEGER_CST:
eddad94a 4874 return integer_zerop (expr) ? truthvalue_false_node
4875 : truthvalue_true_node;
b0fc3e72 4876
4877 case REAL_CST:
5000e21c 4878 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4879 ? truthvalue_true_node
4880 : truthvalue_false_node;
b0fc3e72 4881
9421ebb9 4882 case FIXED_CST:
4883 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4884 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4885 ? truthvalue_true_node
4886 : truthvalue_false_node;
4887
ce04dcdc 4888 case FUNCTION_DECL:
b6889cb0 4889 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
ce04dcdc 4890 /* Fall through. */
4891
b0fc3e72 4892 case ADDR_EXPR:
ce871053 4893 {
ee7d4d6a 4894 tree inner = TREE_OPERAND (expr, 0);
6b68e71a 4895 if (decl_with_nonnull_addr_p (inner))
ce871053 4896 {
6b68e71a 4897 /* Common Ada/Pascal programmer's mistake. */
8e70fb09 4898 warning_at (location,
4899 OPT_Waddress,
4900 "the address of %qD will always evaluate as %<true%>",
4901 inner);
ce871053 4902 return truthvalue_true_node;
4903 }
d473d901 4904 break;
ce871053 4905 }
b0fc3e72 4906
2203bd5c 4907 case COMPLEX_EXPR:
e60a6f7b 4908 expr = build_binary_op (EXPR_LOCATION (expr),
8e70fb09 4909 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
95809de4 4910 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
8e70fb09 4911 c_common_truthvalue_conversion (location,
4912 TREE_OPERAND (expr, 0)),
4913 c_common_truthvalue_conversion (location,
4914 TREE_OPERAND (expr, 1)),
2203bd5c 4915 0);
e60a6f7b 4916 goto ret;
2203bd5c 4917
b0fc3e72 4918 case NEGATE_EXPR:
4919 case ABS_EXPR:
4920 case FLOAT_EXPR:
c6418a4e 4921 case EXCESS_PRECISION_EXPR:
d10cfa8d 4922 /* These don't change whether an object is nonzero or zero. */
8e70fb09 4923 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
b0fc3e72 4924
4925 case LROTATE_EXPR:
4926 case RROTATE_EXPR:
d10cfa8d 4927 /* These don't change whether an object is zero or nonzero, but
b0fc3e72 4928 we can't ignore them if their second arg has side-effects. */
4929 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
e60a6f7b 4930 {
4931 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4932 TREE_OPERAND (expr, 1),
48e1416a 4933 c_common_truthvalue_conversion
e60a6f7b 4934 (location, TREE_OPERAND (expr, 0)));
4935 goto ret;
4936 }
b0fc3e72 4937 else
8e70fb09 4938 return c_common_truthvalue_conversion (location,
4939 TREE_OPERAND (expr, 0));
73be5127 4940
b0fc3e72 4941 case COND_EXPR:
4942 /* Distribute the conversion into the arms of a COND_EXPR. */
a75b1c71 4943 if (c_dialect_cxx ())
e60a6f7b 4944 {
d0389adc 4945 tree op1 = TREE_OPERAND (expr, 1);
4946 tree op2 = TREE_OPERAND (expr, 2);
4947 /* In C++ one of the arms might have void type if it is throw. */
4948 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4949 op1 = c_common_truthvalue_conversion (location, op1);
4950 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4951 op2 = c_common_truthvalue_conversion (location, op2);
389dd41b 4952 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
d0389adc 4953 TREE_OPERAND (expr, 0), op1, op2);
e60a6f7b 4954 goto ret;
4955 }
a75b1c71 4956 else
e60a6f7b 4957 {
4958 /* Folding will happen later for C. */
4959 expr = build3 (COND_EXPR, truthvalue_type_node,
4960 TREE_OPERAND (expr, 0),
4961 c_common_truthvalue_conversion (location,
4962 TREE_OPERAND (expr, 1)),
4963 c_common_truthvalue_conversion (location,
4964 TREE_OPERAND (expr, 2)));
4965 goto ret;
4966 }
b0fc3e72 4967
72dd6141 4968 CASE_CONVERT:
f8913d47 4969 {
4970 tree totype = TREE_TYPE (expr);
4971 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4972
db3d1ffc 4973 if (POINTER_TYPE_P (totype)
4974 && TREE_CODE (fromtype) == REFERENCE_TYPE)
4975 {
4976 tree inner = expr;
4977 STRIP_NOPS (inner);
4978
4979 if (DECL_P (inner))
4980 warning_at (location,
4981 OPT_Waddress,
4982 "the compiler can assume that the address of "
4983 "%qD will always evaluate to %<true%>",
4984 inner);
4985 }
4986
f8913d47 4987 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4988 since that affects how `default_conversion' will behave. */
4989 if (TREE_CODE (totype) == REFERENCE_TYPE
4990 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4991 break;
4992 /* Don't strip a conversion from C++0x scoped enum, since they
4993 don't implicitly convert to other types. */
4994 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4995 && ENUM_IS_SCOPED (fromtype))
4996 break;
4997 /* If this isn't narrowing the argument, we can ignore it. */
4998 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4999 return c_common_truthvalue_conversion (location,
5000 TREE_OPERAND (expr, 0));
5001 }
b0fc3e72 5002 break;
5003
16837b18 5004 case MODIFY_EXPR:
60a0513e 5005 if (!TREE_NO_WARNING (expr)
5006 && warn_parentheses)
5007 {
5008 warning (OPT_Wparentheses,
5009 "suggest parentheses around assignment used as truth value");
5010 TREE_NO_WARNING (expr) = 1;
5011 }
16837b18 5012 break;
73be5127 5013
31f820d2 5014 default:
5015 break;
b0fc3e72 5016 }
5017
2ba726d2 5018 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
a0748b7d 5019 {
93be21c0 5020 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
e60a6f7b 5021 expr = (build_binary_op
8e70fb09 5022 (EXPR_LOCATION (expr),
5023 (TREE_SIDE_EFFECTS (expr)
a0748b7d 5024 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
b6889cb0 5025 c_common_truthvalue_conversion
5026 (location,
5027 build_unary_op (location, REALPART_EXPR, t, 0)),
5028 c_common_truthvalue_conversion
5029 (location,
5030 build_unary_op (location, IMAGPART_EXPR, t, 0)),
a0748b7d 5031 0));
e60a6f7b 5032 goto ret;
a0748b7d 5033 }
2ba726d2 5034
9421ebb9 5035 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
5036 {
5037 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
5038 FCONST0 (TYPE_MODE
5039 (TREE_TYPE (expr))));
43158006 5040 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
9421ebb9 5041 }
e60a6f7b 5042 else
5043 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
9421ebb9 5044
e60a6f7b 5045 ret:
5046 protected_set_expr_location (expr, location);
5047 return expr;
b0fc3e72 5048}
5049\f
3237155d 5050static void def_builtin_1 (enum built_in_function fncode,
5051 const char *name,
5052 enum built_in_class fnclass,
5053 tree fntype, tree libtype,
5054 bool both_p, bool fallback_p, bool nonansi_p,
5055 tree fnattrs, bool implicit_p);
0d4238dc 5056
a5b1863e 5057
5058/* Apply the TYPE_QUALS to the new DECL. */
5059
5060void
1cae46be 5061c_apply_type_quals_to_decl (int type_quals, tree decl)
a5b1863e 5062{
adfb367f 5063 tree type = TREE_TYPE (decl);
b27ac6b5 5064
e4eabbe4 5065 if (type == error_mark_node)
5066 return;
adfb367f 5067
98a33d9f 5068 if ((type_quals & TYPE_QUAL_CONST)
5069 || (type && TREE_CODE (type) == REFERENCE_TYPE))
5070 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
5071 constructor can produce constant init, so rely on cp_finish_decl to
5072 clear TREE_READONLY if the variable has non-constant init. */
a5b1863e 5073 TREE_READONLY (decl) = 1;
5074 if (type_quals & TYPE_QUAL_VOLATILE)
5075 {
5076 TREE_SIDE_EFFECTS (decl) = 1;
5077 TREE_THIS_VOLATILE (decl) = 1;
5078 }
d91a20bc 5079 if (type_quals & TYPE_QUAL_RESTRICT)
a5b1863e 5080 {
adfb367f 5081 while (type && TREE_CODE (type) == ARRAY_TYPE)
5082 /* Allow 'restrict' on arrays of pointers.
5083 FIXME currently we just ignore it. */
5084 type = TREE_TYPE (type);
5085 if (!type
5086 || !POINTER_TYPE_P (type)
5087 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
b0b1af64 5088 error ("invalid use of %<restrict%>");
a5b1863e 5089 }
5090}
5091
2ef51f0e 5092struct c_type_hasher : ggc_hasher<tree>
5093{
5094 static hashval_t hash (tree);
5095 static bool equal (tree, tree);
5096};
5097
4ee9c684 5098/* Hash function for the problem of multiple type definitions in
5099 different files. This must hash all types that will compare
5100 equal via comptypes to the same value. In practice it hashes
0bed3869 5101 on some of the simple stuff and leaves the details to comptypes. */
4ee9c684 5102
2ef51f0e 5103hashval_t
5104c_type_hasher::hash (tree t)
4ee9c684 5105{
ecf2703d 5106 int n_elements;
4ee9c684 5107 int shift, size;
4ee9c684 5108 tree t2;
5109 switch (TREE_CODE (t))
5110 {
fbf0afd1 5111 /* For pointers, hash on pointee type plus some swizzling. */
2363ef00 5112 case POINTER_TYPE:
2ef51f0e 5113 return hash (TREE_TYPE (t)) ^ 0x3003003;
2363ef00 5114 /* Hash on number of elements and total size. */
5115 case ENUMERAL_TYPE:
5116 shift = 3;
5117 t2 = TYPE_VALUES (t);
5118 break;
5119 case RECORD_TYPE:
5120 shift = 0;
5121 t2 = TYPE_FIELDS (t);
5122 break;
5123 case QUAL_UNION_TYPE:
5124 shift = 1;
5125 t2 = TYPE_FIELDS (t);
5126 break;
5127 case UNION_TYPE:
5128 shift = 2;
5129 t2 = TYPE_FIELDS (t);
5130 break;
5131 default:
231bd014 5132 gcc_unreachable ();
4ee9c684 5133 }
ecf2703d 5134 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
5135 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
5136 n_elements = list_length (t2);
78c2e180 5137 /* We might have a VLA here. */
5138 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
5139 size = 0;
5140 else
f9ae6f95 5141 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
ecf2703d 5142 return ((size << 24) | (n_elements << shift));
4ee9c684 5143}
5144
2ef51f0e 5145bool
5146c_type_hasher::equal (tree t1, tree t2)
5147{
5148 return lang_hooks.types_compatible_p (t1, t2);
5149}
5150
5151static GTY(()) hash_table<c_type_hasher> *type_hash_table;
1ecd4018 5152
b5ba9f3a 5153/* Return the typed-based alias set for T, which may be an expression
f7c44134 5154 or a type. Return -1 if we don't do anything special. */
ab2c1de8 5155
32c2fdea 5156alias_set_type
1cae46be 5157c_common_get_alias_set (tree t)
b5ba9f3a 5158{
be4f2de7 5159 tree u;
1cae46be 5160
e58c17e7 5161 /* For VLAs, use the alias set of the element type rather than the
5162 default of alias set 0 for types compared structurally. */
5163 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
5164 {
5165 if (TREE_CODE (t) == ARRAY_TYPE)
5166 return get_alias_set (TREE_TYPE (t));
5167 return -1;
5168 }
5169
be4f2de7 5170 /* Permit type-punning when accessing a union, provided the access
5171 is directly through the union. For example, this code does not
5172 permit taking the address of a union member and then storing
5173 through it. Even the type-punning allowed here is a GCC
5174 extension, albeit a common and useful one; the C standard says
5175 that such accesses have implementation-defined behavior. */
5176 for (u = t;
5177 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
5178 u = TREE_OPERAND (u, 0))
5179 if (TREE_CODE (u) == COMPONENT_REF
5180 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
5181 return 0;
1e2513d9 5182
9fcc3e54 5183 /* That's all the expressions we handle specially. */
84166705 5184 if (!TYPE_P (t))
9fcc3e54 5185 return -1;
5186
d716ce75 5187 /* The C standard guarantees that any object may be accessed via an
9fcc3e54 5188 lvalue that has character type. */
5189 if (t == char_type_node
5190 || t == signed_char_type_node
5191 || t == unsigned_char_type_node)
f7c44134 5192 return 0;
a5b1863e 5193
1607663f 5194 /* The C standard specifically allows aliasing between signed and
5195 unsigned variants of the same type. We treat the signed
5196 variant as canonical. */
78a8ed03 5197 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
a8868e19 5198 {
4070745f 5199 tree t1 = c_common_signed_type (t);
1607663f 5200
a8868e19 5201 /* t1 == t can happen for boolean nodes which are always unsigned. */
5202 if (t1 != t)
5203 return get_alias_set (t1);
5204 }
1e2513d9 5205
4ee9c684 5206 /* Handle the case of multiple type nodes referring to "the same" type,
5207 which occurs with IMA. These share an alias set. FIXME: Currently only
5208 C90 is handled. (In C99 type compatibility is not transitive, which
5209 complicates things mightily. The alias set splay trees can theoretically
5210 represent this, but insertion is tricky when you consider all the
5211 different orders things might arrive in.) */
5212
5213 if (c_language != clk_c || flag_isoc99)
5214 return -1;
5215
0bed3869 5216 /* Save time if there's only one input file. */
e08bd2f4 5217 if (num_in_fnames == 1)
4ee9c684 5218 return -1;
5219
5220 /* Pointers need special handling if they point to any type that
5221 needs special handling (below). */
5222 if (TREE_CODE (t) == POINTER_TYPE)
5223 {
5224 tree t2;
5225 /* Find bottom type under any nested POINTERs. */
b27ac6b5 5226 for (t2 = TREE_TYPE (t);
af592f67 5227 TREE_CODE (t2) == POINTER_TYPE;
5228 t2 = TREE_TYPE (t2))
5229 ;
b27ac6b5 5230 if (TREE_CODE (t2) != RECORD_TYPE
af592f67 5231 && TREE_CODE (t2) != ENUMERAL_TYPE
5232 && TREE_CODE (t2) != QUAL_UNION_TYPE
5233 && TREE_CODE (t2) != UNION_TYPE)
5234 return -1;
4ee9c684 5235 if (TYPE_SIZE (t2) == 0)
af592f67 5236 return -1;
4ee9c684 5237 }
5238 /* These are the only cases that need special handling. */
b27ac6b5 5239 if (TREE_CODE (t) != RECORD_TYPE
4ee9c684 5240 && TREE_CODE (t) != ENUMERAL_TYPE
5241 && TREE_CODE (t) != QUAL_UNION_TYPE
5242 && TREE_CODE (t) != UNION_TYPE
5243 && TREE_CODE (t) != POINTER_TYPE)
5244 return -1;
5245 /* Undefined? */
5246 if (TYPE_SIZE (t) == 0)
5247 return -1;
5248
b27ac6b5 5249 /* Look up t in hash table. Only one of the compatible types within each
4ee9c684 5250 alias set is recorded in the table. */
5251 if (!type_hash_table)
2ef51f0e 5252 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
5253 tree *slot = type_hash_table->find_slot (t, INSERT);
4ee9c684 5254 if (*slot != NULL)
ad16cb2c 5255 {
5256 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5257 return TYPE_ALIAS_SET ((tree)*slot);
5258 }
4ee9c684 5259 else
5260 /* Our caller will assign and record (in t) a new alias set; all we need
5261 to do is remember t in the hash table. */
5262 *slot = t;
5263
f7c44134 5264 return -1;
b5ba9f3a 5265}
902b4e01 5266\f
e60a6f7b 5267/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
a179a7dc 5268 the IS_SIZEOF parameter indicates which operator is being applied.
e60a6f7b 5269 The COMPLAIN flag controls whether we should diagnose possibly
5270 ill-formed constructs or not. LOC is the location of the SIZEOF or
a179a7dc 5271 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5272 a type in any context should be returned, rather than the normal
5273 alignment for that type. */
908c697e 5274
902b4e01 5275tree
e60a6f7b 5276c_sizeof_or_alignof_type (location_t loc,
a179a7dc 5277 tree type, bool is_sizeof, bool min_alignof,
5278 int complain)
902b4e01 5279{
d4c4d95c 5280 const char *op_name;
5281 tree value = NULL;
5282 enum tree_code type_code = TREE_CODE (type);
1cae46be 5283
908c697e 5284 op_name = is_sizeof ? "sizeof" : "__alignof__";
1cae46be 5285
d4c4d95c 5286 if (type_code == FUNCTION_TYPE)
902b4e01 5287 {
908c697e 5288 if (is_sizeof)
d4c4d95c 5289 {
9205a6cc 5290 if (complain && warn_pointer_arith)
5291 pedwarn (loc, OPT_Wpointer_arith,
8864917d 5292 "invalid application of %<sizeof%> to a function type");
ebd21de4 5293 else if (!complain)
5294 return error_mark_node;
d4c4d95c 5295 value = size_one_node;
5296 }
5297 else
83e25171 5298 {
5299 if (complain)
5300 {
5301 if (c_dialect_cxx ())
29438999 5302 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
83e25171 5303 "%<alignof%> applied to a function type");
5304 else
29438999 5305 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
83e25171 5306 "%<_Alignof%> applied to a function type");
5307 }
5308 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5309 }
d4c4d95c 5310 }
5311 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5312 {
1cae46be 5313 if (type_code == VOID_TYPE
9205a6cc 5314 && complain && warn_pointer_arith)
5315 pedwarn (loc, OPT_Wpointer_arith,
8864917d 5316 "invalid application of %qs to a void type", op_name);
ebd21de4 5317 else if (!complain)
5318 return error_mark_node;
d4c4d95c 5319 value = size_one_node;
902b4e01 5320 }
3df19e1b 5321 else if (!COMPLETE_TYPE_P (type)
5322 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
902b4e01 5323 {
d3a4d008 5324 if (complain)
3df19e1b 5325 error_at (loc, "invalid application of %qs to incomplete type %qT",
e60a6f7b 5326 op_name, type);
9c719c74 5327 return error_mark_node;
902b4e01 5328 }
3df19e1b 5329 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5330 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5331 {
5332 if (complain)
5333 error_at (loc, "invalid application of %qs to array type %qT of "
5334 "incomplete element type", op_name, type);
5335 return error_mark_node;
5336 }
902b4e01 5337 else
d4c4d95c 5338 {
908c697e 5339 if (is_sizeof)
d4c4d95c 5340 /* Convert in case a char is more than one unit. */
389dd41b 5341 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5342 size_int (TYPE_PRECISION (char_type_node)
5343 / BITS_PER_UNIT));
a179a7dc 5344 else if (min_alignof)
c2c4ae8d 5345 value = size_int (min_align_of_type (type));
d4c4d95c 5346 else
d37625c0 5347 value = size_int (TYPE_ALIGN_UNIT (type));
d4c4d95c 5348 }
902b4e01 5349
5a1fe2db 5350 /* VALUE will have the middle-end integer type sizetype.
5351 However, we should really return a value of type `size_t',
5352 which is just a typedef for an ordinary integer type. */
389dd41b 5353 value = fold_convert_loc (loc, size_type_node, value);
1cae46be 5354
d4c4d95c 5355 return value;
902b4e01 5356}
5357
5358/* Implement the __alignof keyword: Return the minimum required
097b5c8b 5359 alignment of EXPR, measured in bytes. For VAR_DECLs,
5360 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
e60a6f7b 5361 from an "aligned" __attribute__ specification). LOC is the
5362 location of the ALIGNOF operator. */
72040e7e 5363
902b4e01 5364tree
e60a6f7b 5365c_alignof_expr (location_t loc, tree expr)
902b4e01 5366{
5367 tree t;
5368
097b5c8b 5369 if (VAR_OR_FUNCTION_DECL_P (expr))
d37625c0 5370 t = size_int (DECL_ALIGN_UNIT (expr));
1cae46be 5371
902b4e01 5372 else if (TREE_CODE (expr) == COMPONENT_REF
5373 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5374 {
e60a6f7b 5375 error_at (loc, "%<__alignof%> applied to a bit-field");
902b4e01 5376 t = size_one_node;
5377 }
5378 else if (TREE_CODE (expr) == COMPONENT_REF
7cc7e163 5379 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
d37625c0 5380 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
1cae46be 5381
902b4e01 5382 else if (TREE_CODE (expr) == INDIRECT_REF)
5383 {
5384 tree t = TREE_OPERAND (expr, 0);
5385 tree best = t;
5386 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1cae46be 5387
72dd6141 5388 while (CONVERT_EXPR_P (t)
7cc7e163 5389 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
902b4e01 5390 {
5391 int thisalign;
5392
5393 t = TREE_OPERAND (t, 0);
5394 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5395 if (thisalign > bestalign)
5396 best = t, bestalign = thisalign;
5397 }
e60a6f7b 5398 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
902b4e01 5399 }
5400 else
e60a6f7b 5401 return c_alignof (loc, TREE_TYPE (expr));
902b4e01 5402
389dd41b 5403 return fold_convert_loc (loc, size_type_node, t);
902b4e01 5404}
5405\f
8fe4a266 5406/* Handle C and C++ default attributes. */
5407
5408enum built_in_attribute
5409{
5410#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5411#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
c8010b80 5412#define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
8fe4a266 5413#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5414#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
8fe4a266 5415#include "builtin-attrs.def"
5416#undef DEF_ATTR_NULL_TREE
5417#undef DEF_ATTR_INT
c8010b80 5418#undef DEF_ATTR_STRING
8fe4a266 5419#undef DEF_ATTR_IDENT
5420#undef DEF_ATTR_TREE_LIST
8fe4a266 5421 ATTR_LAST
5422};
5423
5424static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5425
1cae46be 5426static void c_init_attributes (void);
8fe4a266 5427
27213ba3 5428enum c_builtin_type
72040e7e 5429{
d2d4bdde 5430#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5431#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5432#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5433#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5434#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5435#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
0a39fd54 5436#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
3c77ca67 5437#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5438 ARG6) NAME,
5439#define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5440 ARG6, ARG7) NAME,
5441#define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5442 ARG6, ARG7, ARG8) NAME,
d2d4bdde 5443#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5444#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5445#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
03901330 5446#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
0a39fd54 5447#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3c77ca67 5448#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
6349b8cc 5449 NAME,
5450#define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5451 ARG6, ARG7) NAME,
5452#define DEF_FUNCTION_TYPE_VAR_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5453 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
d2d4bdde 5454#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5455#include "builtin-types.def"
5456#undef DEF_PRIMITIVE_TYPE
5457#undef DEF_FUNCTION_TYPE_0
5458#undef DEF_FUNCTION_TYPE_1
5459#undef DEF_FUNCTION_TYPE_2
5460#undef DEF_FUNCTION_TYPE_3
5461#undef DEF_FUNCTION_TYPE_4
0a39fd54 5462#undef DEF_FUNCTION_TYPE_5
5463#undef DEF_FUNCTION_TYPE_6
27213ba3 5464#undef DEF_FUNCTION_TYPE_7
bc7bff74 5465#undef DEF_FUNCTION_TYPE_8
d2d4bdde 5466#undef DEF_FUNCTION_TYPE_VAR_0
5467#undef DEF_FUNCTION_TYPE_VAR_1
5468#undef DEF_FUNCTION_TYPE_VAR_2
03901330 5469#undef DEF_FUNCTION_TYPE_VAR_3
0a39fd54 5470#undef DEF_FUNCTION_TYPE_VAR_4
5471#undef DEF_FUNCTION_TYPE_VAR_5
6349b8cc 5472#undef DEF_FUNCTION_TYPE_VAR_7
5473#undef DEF_FUNCTION_TYPE_VAR_11
d2d4bdde 5474#undef DEF_POINTER_TYPE
27213ba3 5475 BT_LAST
5476};
5477
5478typedef enum c_builtin_type builtin_type;
d2d4bdde 5479
27213ba3 5480/* A temporary array for c_common_nodes_and_builtins. Used in
5481 communication with def_fn_type. */
5482static tree builtin_types[(int) BT_LAST + 1];
d2d4bdde 5483
27213ba3 5484/* A helper function for c_common_nodes_and_builtins. Build function type
5485 for DEF with return type RET and N arguments. If VAR is true, then the
5486 function should be variadic after those N arguments.
5487
5488 Takes special care not to ICE if any of the types involved are
5489 error_mark_node, which indicates that said type is not in fact available
5490 (see builtin_type_for_size). In which case the function type as a whole
5491 should be error_mark_node. */
5492
5493static void
5494def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5495{
3a939d12 5496 tree t;
5497 tree *args = XALLOCAVEC (tree, n);
27213ba3 5498 va_list list;
5499 int i;
5500
5501 va_start (list, n);
5502 for (i = 0; i < n; ++i)
5503 {
7d339f93 5504 builtin_type a = (builtin_type) va_arg (list, int);
27213ba3 5505 t = builtin_types[a];
5506 if (t == error_mark_node)
5507 goto egress;
3a939d12 5508 args[i] = t;
27213ba3 5509 }
27213ba3 5510
27213ba3 5511 t = builtin_types[ret];
5512 if (t == error_mark_node)
5513 goto egress;
3a939d12 5514 if (var)
5515 t = build_varargs_function_type_array (t, n, args);
5516 else
5517 t = build_function_type_array (t, n, args);
27213ba3 5518
5519 egress:
5520 builtin_types[def] = t;
451c8e2f 5521 va_end (list);
27213ba3 5522}
5523
dce22712 5524/* Build builtin functions common to both C and C++ language
5525 frontends. */
5526
5527static void
5528c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5529{
5530#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5531 builtin_types[ENUM] = VALUE;
5532#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5533 def_fn_type (ENUM, RETURN, 0, 0);
5534#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5535 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5536#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5537 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5538#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5539 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5540#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5541 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5542#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5543 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5544#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5545 ARG6) \
5546 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5547#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5548 ARG6, ARG7) \
5549 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
bc7bff74 5550#define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5551 ARG6, ARG7, ARG8) \
5552 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5553 ARG7, ARG8);
dce22712 5554#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5555 def_fn_type (ENUM, RETURN, 1, 0);
5556#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5557 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5558#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5559 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5560#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5561 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5562#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5563 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5564#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5565 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
6349b8cc 5566#define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5567 ARG6, ARG7) \
5568 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5569#define DEF_FUNCTION_TYPE_VAR_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5570 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5571 def_fn_type (ENUM, RETURN, 1, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5572 ARG7, ARG8, ARG9, ARG10, ARG11);
dce22712 5573#define DEF_POINTER_TYPE(ENUM, TYPE) \
5574 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5575
5576#include "builtin-types.def"
5577
5578#undef DEF_PRIMITIVE_TYPE
3c77ca67 5579#undef DEF_FUNCTION_TYPE_0
dce22712 5580#undef DEF_FUNCTION_TYPE_1
5581#undef DEF_FUNCTION_TYPE_2
5582#undef DEF_FUNCTION_TYPE_3
5583#undef DEF_FUNCTION_TYPE_4
5584#undef DEF_FUNCTION_TYPE_5
5585#undef DEF_FUNCTION_TYPE_6
3c77ca67 5586#undef DEF_FUNCTION_TYPE_7
5587#undef DEF_FUNCTION_TYPE_8
dce22712 5588#undef DEF_FUNCTION_TYPE_VAR_0
5589#undef DEF_FUNCTION_TYPE_VAR_1
5590#undef DEF_FUNCTION_TYPE_VAR_2
5591#undef DEF_FUNCTION_TYPE_VAR_3
5592#undef DEF_FUNCTION_TYPE_VAR_4
5593#undef DEF_FUNCTION_TYPE_VAR_5
6349b8cc 5594#undef DEF_FUNCTION_TYPE_VAR_7
5595#undef DEF_FUNCTION_TYPE_VAR_11
dce22712 5596#undef DEF_POINTER_TYPE
5597 builtin_types[(int) BT_LAST] = NULL_TREE;
5598
5599 c_init_attributes ();
5600
5601#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5602 NONANSI_P, ATTRS, IMPLICIT, COND) \
5603 if (NAME && COND) \
5604 def_builtin_1 (ENUM, NAME, CLASS, \
5605 builtin_types[(int) TYPE], \
5606 builtin_types[(int) LIBTYPE], \
5607 BOTH_P, FALLBACK_P, NONANSI_P, \
5608 built_in_attributes[(int) ATTRS], IMPLICIT);
5609#include "builtins.def"
5610#undef DEF_BUILTIN
5611
87eb1c28 5612 targetm.init_builtins ();
5613
471eff36 5614 build_common_builtin_nodes ();
dce22712 5615
a89e6c15 5616 if (flag_cilkplus)
d037099f 5617 cilk_init_builtins ();
dce22712 5618}
5619
9e6687c8 5620/* Like get_identifier, but avoid warnings about null arguments when
5621 the argument may be NULL for targets where GCC lacks stdint.h type
5622 information. */
5623
5624static inline tree
5625c_get_ident (const char *id)
5626{
5627 return get_identifier (id);
5628}
5629
27213ba3 5630/* Build tree nodes and builtin functions common to both C and C++ language
5631 frontends. */
5632
5633void
5634c_common_nodes_and_builtins (void)
5635{
924bbf02 5636 int char16_type_size;
5637 int char32_type_size;
174fcc61 5638 int wchar_type_size;
5639 tree array_domain_type;
2d47cc32 5640 tree va_list_ref_type_node;
8a15c04a 5641 tree va_list_arg_type_node;
9f75f026 5642 int i;
a66c9326 5643
c38a75b7 5644 build_common_tree_nodes (flag_signed_char, flag_short_double);
e593356b 5645
174fcc61 5646 /* Define `int' and `char' first so that dbx will output them first. */
d946ea19 5647 record_builtin_type (RID_INT, NULL, integer_type_node);
174fcc61 5648 record_builtin_type (RID_CHAR, "char", char_type_node);
5649
5650 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5651 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5652 but not C. Are the conditionals here needed? */
c0f19401 5653 if (c_dialect_cxx ())
d946ea19 5654 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
174fcc61 5655 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5656 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5657 record_builtin_type (RID_MAX, "long unsigned int",
5658 long_unsigned_type_node);
9f75f026 5659
5660 for (i = 0; i < NUM_INT_N_ENTS; i ++)
6388cfe2 5661 {
9f75f026 5662 char name[25];
5663
5664 sprintf (name, "__int%d", int_n_data[i].bitsize);
76738f56 5665 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
9f75f026 5666 int_n_trees[i].signed_type);
5667 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
76738f56 5668 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
6388cfe2 5669 }
9f75f026 5670
c0f19401 5671 if (c_dialect_cxx ())
174fcc61 5672 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5673 record_builtin_type (RID_MAX, "long long int",
5674 long_long_integer_type_node);
5675 record_builtin_type (RID_MAX, "long long unsigned int",
5676 long_long_unsigned_type_node);
c0f19401 5677 if (c_dialect_cxx ())
174fcc61 5678 record_builtin_type (RID_MAX, "long long unsigned",
5679 long_long_unsigned_type_node);
5680 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5681 record_builtin_type (RID_MAX, "short unsigned int",
5682 short_unsigned_type_node);
c0f19401 5683 if (c_dialect_cxx ())
174fcc61 5684 record_builtin_type (RID_MAX, "unsigned short",
5685 short_unsigned_type_node);
5686
5687 /* Define both `signed char' and `unsigned char'. */
5688 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5689 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5690
771d21fa 5691 /* These are types that c_common_type_for_size and
5692 c_common_type_for_mode use. */
e60a6f7b 5693 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5694 TYPE_DECL, NULL_TREE,
dc24ddbd 5695 intQI_type_node));
e60a6f7b 5696 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5697 TYPE_DECL, NULL_TREE,
dc24ddbd 5698 intHI_type_node));
e60a6f7b 5699 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5700 TYPE_DECL, NULL_TREE,
dc24ddbd 5701 intSI_type_node));
e60a6f7b 5702 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5703 TYPE_DECL, NULL_TREE,
dc24ddbd 5704 intDI_type_node));
174fcc61 5705#if HOST_BITS_PER_WIDE_INT >= 64
9f75f026 5706 /* Note that this is different than the __int128 type that's part of
5707 the generic __intN support. */
f1515a39 5708 if (targetm.scalar_mode_supported_p (TImode))
e60a6f7b 5709 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5710 TYPE_DECL,
f1515a39 5711 get_identifier ("__int128_t"),
5712 intTI_type_node));
174fcc61 5713#endif
e60a6f7b 5714 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5715 TYPE_DECL, NULL_TREE,
dc24ddbd 5716 unsigned_intQI_type_node));
e60a6f7b 5717 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5718 TYPE_DECL, NULL_TREE,
dc24ddbd 5719 unsigned_intHI_type_node));
e60a6f7b 5720 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5721 TYPE_DECL, NULL_TREE,
dc24ddbd 5722 unsigned_intSI_type_node));
e60a6f7b 5723 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5724 TYPE_DECL, NULL_TREE,
dc24ddbd 5725 unsigned_intDI_type_node));
174fcc61 5726#if HOST_BITS_PER_WIDE_INT >= 64
f1515a39 5727 if (targetm.scalar_mode_supported_p (TImode))
e60a6f7b 5728 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5729 TYPE_DECL,
f1515a39 5730 get_identifier ("__uint128_t"),
5731 unsigned_intTI_type_node));
174fcc61 5732#endif
5733
5734 /* Create the widest literal types. */
5735 widest_integer_literal_type_node
5736 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
e60a6f7b 5737 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5738 TYPE_DECL, NULL_TREE,
dc24ddbd 5739 widest_integer_literal_type_node));
174fcc61 5740
5741 widest_unsigned_literal_type_node
5742 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
e60a6f7b 5743 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5744 TYPE_DECL, NULL_TREE,
dc24ddbd 5745 widest_unsigned_literal_type_node));
174fcc61 5746
654ef926 5747 signed_size_type_node = c_common_signed_type (size_type_node);
174fcc61 5748
73673831 5749 pid_type_node =
5750 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5751
d946ea19 5752 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5753 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
174fcc61 5754 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5755
c4503c0a 5756 /* Only supported decimal floating point extension if the target
5757 actually supports underlying modes. */
48e1416a 5758 if (targetm.scalar_mode_supported_p (SDmode)
c4503c0a 5759 && targetm.scalar_mode_supported_p (DDmode)
5760 && targetm.scalar_mode_supported_p (TDmode))
5761 {
5762 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5763 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5764 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5765 }
5766
9421ebb9 5767 if (targetm.fixed_point_supported_p ())
5768 {
5769 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5770 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5771 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5772 record_builtin_type (RID_MAX, "long long _Fract",
5773 long_long_fract_type_node);
5774 record_builtin_type (RID_MAX, "unsigned short _Fract",
5775 unsigned_short_fract_type_node);
5776 record_builtin_type (RID_MAX, "unsigned _Fract",
5777 unsigned_fract_type_node);
5778 record_builtin_type (RID_MAX, "unsigned long _Fract",
5779 unsigned_long_fract_type_node);
5780 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5781 unsigned_long_long_fract_type_node);
5782 record_builtin_type (RID_MAX, "_Sat short _Fract",
5783 sat_short_fract_type_node);
5784 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5785 record_builtin_type (RID_MAX, "_Sat long _Fract",
5786 sat_long_fract_type_node);
5787 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5788 sat_long_long_fract_type_node);
5789 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5790 sat_unsigned_short_fract_type_node);
5791 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5792 sat_unsigned_fract_type_node);
5793 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5794 sat_unsigned_long_fract_type_node);
5795 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5796 sat_unsigned_long_long_fract_type_node);
5797 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5798 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5799 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5800 record_builtin_type (RID_MAX, "long long _Accum",
5801 long_long_accum_type_node);
5802 record_builtin_type (RID_MAX, "unsigned short _Accum",
5803 unsigned_short_accum_type_node);
5804 record_builtin_type (RID_MAX, "unsigned _Accum",
5805 unsigned_accum_type_node);
5806 record_builtin_type (RID_MAX, "unsigned long _Accum",
5807 unsigned_long_accum_type_node);
5808 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5809 unsigned_long_long_accum_type_node);
5810 record_builtin_type (RID_MAX, "_Sat short _Accum",
5811 sat_short_accum_type_node);
5812 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5813 record_builtin_type (RID_MAX, "_Sat long _Accum",
5814 sat_long_accum_type_node);
5815 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5816 sat_long_long_accum_type_node);
5817 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5818 sat_unsigned_short_accum_type_node);
5819 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5820 sat_unsigned_accum_type_node);
5821 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5822 sat_unsigned_long_accum_type_node);
5823 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5824 sat_unsigned_long_long_accum_type_node);
5825
5826 }
5827
e60a6f7b 5828 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5829 TYPE_DECL,
dc24ddbd 5830 get_identifier ("complex int"),
5831 complex_integer_type_node));
e60a6f7b 5832 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5833 TYPE_DECL,
dc24ddbd 5834 get_identifier ("complex float"),
5835 complex_float_type_node));
e60a6f7b 5836 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5837 TYPE_DECL,
dc24ddbd 5838 get_identifier ("complex double"),
5839 complex_double_type_node));
5840 lang_hooks.decls.pushdecl
e60a6f7b 5841 (build_decl (UNKNOWN_LOCATION,
5842 TYPE_DECL, get_identifier ("complex long double"),
20325f61 5843 complex_long_double_type_node));
174fcc61 5844
e256d445 5845 if (c_dialect_cxx ())
5846 /* For C++, make fileptr_type_node a distinct void * type until
5847 FILE type is defined. */
e086912e 5848 fileptr_type_node = build_variant_type_copy (ptr_type_node);
e256d445 5849
d946ea19 5850 record_builtin_type (RID_VOID, NULL, void_type_node);
174fcc61 5851
6753bca0 5852 /* Set the TYPE_NAME for any variants that were built before
5853 record_builtin_type gave names to the built-in types. */
5854 {
5855 tree void_name = TYPE_NAME (void_type_node);
5856 TYPE_NAME (void_type_node) = NULL_TREE;
5857 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5858 = void_name;
5859 TYPE_NAME (void_type_node) = void_name;
5860 }
5861
174fcc61 5862 void_list_node = build_void_list_node ();
5863
5864 /* Make a type to be the domain of a few array types
5865 whose domains don't really matter.
5866 200 is small enough that it always fits in size_t
5867 and large enough that it can hold most function names for the
5868 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5869 array_domain_type = build_index_type (size_int (200));
5870
5871 /* Make a type for arrays of characters.
5872 With luck nothing will ever really depend on the length of this
5873 array type. */
5874 char_array_type_node
5875 = build_array_type (char_type_node, array_domain_type);
5876
d2d4bdde 5877 string_type_node = build_pointer_type (char_type_node);
5878 const_string_type_node
5879 = build_pointer_type (build_qualified_type
5880 (char_type_node, TYPE_QUAL_CONST));
5881
174fcc61 5882 /* This is special for C++ so functions can be overloaded. */
18ef7ac2 5883 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
174fcc61 5884 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5885 wchar_type_size = TYPE_PRECISION (wchar_type_node);
f3449a3c 5886 underlying_wchar_type_node = wchar_type_node;
c0f19401 5887 if (c_dialect_cxx ())
174fcc61 5888 {
78a8ed03 5889 if (TYPE_UNSIGNED (wchar_type_node))
174fcc61 5890 wchar_type_node = make_unsigned_type (wchar_type_size);
5891 else
5892 wchar_type_node = make_signed_type (wchar_type_size);
5893 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5894 }
174fcc61 5895
5896 /* This is for wide string constants. */
5897 wchar_array_type_node
5898 = build_array_type (wchar_type_node, array_domain_type);
5899
924bbf02 5900 /* Define 'char16_t'. */
5901 char16_type_node = get_identifier (CHAR16_TYPE);
5902 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5903 char16_type_size = TYPE_PRECISION (char16_type_node);
5904 if (c_dialect_cxx ())
5905 {
5906 char16_type_node = make_unsigned_type (char16_type_size);
5907
60777f69 5908 if (cxx_dialect >= cxx11)
924bbf02 5909 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5910 }
5911
5912 /* This is for UTF-16 string constants. */
5913 char16_array_type_node
5914 = build_array_type (char16_type_node, array_domain_type);
5915
5916 /* Define 'char32_t'. */
5917 char32_type_node = get_identifier (CHAR32_TYPE);
5918 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5919 char32_type_size = TYPE_PRECISION (char32_type_node);
5920 if (c_dialect_cxx ())
5921 {
5922 char32_type_node = make_unsigned_type (char32_type_size);
5923
60777f69 5924 if (cxx_dialect >= cxx11)
924bbf02 5925 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5926 }
5927
5928 /* This is for UTF-32 string constants. */
5929 char32_array_type_node
5930 = build_array_type (char32_type_node, array_domain_type);
5931
6bf5ed8d 5932 wint_type_node =
5933 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5934
5935 intmax_type_node =
5936 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5937 uintmax_type_node =
5938 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5939
f3449a3c 5940 if (SIG_ATOMIC_TYPE)
5941 sig_atomic_type_node =
9e6687c8 5942 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
f3449a3c 5943 if (INT8_TYPE)
5944 int8_type_node =
9e6687c8 5945 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
f3449a3c 5946 if (INT16_TYPE)
5947 int16_type_node =
9e6687c8 5948 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
f3449a3c 5949 if (INT32_TYPE)
5950 int32_type_node =
9e6687c8 5951 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
f3449a3c 5952 if (INT64_TYPE)
5953 int64_type_node =
9e6687c8 5954 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
f3449a3c 5955 if (UINT8_TYPE)
5956 uint8_type_node =
9e6687c8 5957 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
f3449a3c 5958 if (UINT16_TYPE)
d1081017 5959 c_uint16_type_node = uint16_type_node =
9e6687c8 5960 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
f3449a3c 5961 if (UINT32_TYPE)
d1081017 5962 c_uint32_type_node = uint32_type_node =
9e6687c8 5963 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
f3449a3c 5964 if (UINT64_TYPE)
d1081017 5965 c_uint64_type_node = uint64_type_node =
9e6687c8 5966 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
f3449a3c 5967 if (INT_LEAST8_TYPE)
5968 int_least8_type_node =
9e6687c8 5969 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
f3449a3c 5970 if (INT_LEAST16_TYPE)
5971 int_least16_type_node =
9e6687c8 5972 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
f3449a3c 5973 if (INT_LEAST32_TYPE)
5974 int_least32_type_node =
9e6687c8 5975 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
f3449a3c 5976 if (INT_LEAST64_TYPE)
5977 int_least64_type_node =
9e6687c8 5978 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
f3449a3c 5979 if (UINT_LEAST8_TYPE)
5980 uint_least8_type_node =
9e6687c8 5981 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
f3449a3c 5982 if (UINT_LEAST16_TYPE)
5983 uint_least16_type_node =
9e6687c8 5984 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
f3449a3c 5985 if (UINT_LEAST32_TYPE)
5986 uint_least32_type_node =
9e6687c8 5987 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
f3449a3c 5988 if (UINT_LEAST64_TYPE)
5989 uint_least64_type_node =
9e6687c8 5990 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
f3449a3c 5991 if (INT_FAST8_TYPE)
5992 int_fast8_type_node =
9e6687c8 5993 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
f3449a3c 5994 if (INT_FAST16_TYPE)
5995 int_fast16_type_node =
9e6687c8 5996 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
f3449a3c 5997 if (INT_FAST32_TYPE)
5998 int_fast32_type_node =
9e6687c8 5999 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
f3449a3c 6000 if (INT_FAST64_TYPE)
6001 int_fast64_type_node =
9e6687c8 6002 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
f3449a3c 6003 if (UINT_FAST8_TYPE)
6004 uint_fast8_type_node =
9e6687c8 6005 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
f3449a3c 6006 if (UINT_FAST16_TYPE)
6007 uint_fast16_type_node =
9e6687c8 6008 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
f3449a3c 6009 if (UINT_FAST32_TYPE)
6010 uint_fast32_type_node =
9e6687c8 6011 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
f3449a3c 6012 if (UINT_FAST64_TYPE)
6013 uint_fast64_type_node =
9e6687c8 6014 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
f3449a3c 6015 if (INTPTR_TYPE)
6016 intptr_type_node =
9e6687c8 6017 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
f3449a3c 6018 if (UINTPTR_TYPE)
6019 uintptr_type_node =
9e6687c8 6020 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
f3449a3c 6021
3a939d12 6022 default_function_type
6023 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
6bf5ed8d 6024 ptrdiff_type_node
6025 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
11773141 6026 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
6bf5ed8d 6027
dc24ddbd 6028 lang_hooks.decls.pushdecl
e60a6f7b 6029 (build_decl (UNKNOWN_LOCATION,
6030 TYPE_DECL, get_identifier ("__builtin_va_list"),
20325f61 6031 va_list_type_node));
202d6e5f 6032 if (targetm.enum_va_list_p)
acd6f472 6033 {
6034 int l;
6035 const char *pname;
6036 tree ptype;
5f57a8b1 6037
202d6e5f 6038 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
acd6f472 6039 {
6040 lang_hooks.decls.pushdecl
6041 (build_decl (UNKNOWN_LOCATION,
6042 TYPE_DECL, get_identifier (pname),
6043 ptype));
ab2c1de8 6044
acd6f472 6045 }
6046 }
8a15c04a 6047
8a15c04a 6048 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
2d47cc32 6049 {
6050 va_list_arg_type_node = va_list_ref_type_node =
6051 build_pointer_type (TREE_TYPE (va_list_type_node));
6052 }
8a15c04a 6053 else
2d47cc32 6054 {
6055 va_list_arg_type_node = va_list_type_node;
6056 va_list_ref_type_node = build_reference_type (va_list_type_node);
6057 }
1cae46be 6058
dce22712 6059 if (!flag_preprocess_only)
6060 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
ffa8918b 6061
5c62f199 6062 main_identifier_node = get_identifier ("main");
ae84079f 6063
6064 /* Create the built-in __null node. It is important that this is
6065 not shared. */
271e739a 6066 null_node = make_int_cst (1, 1);
ae84079f 6067 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
27213ba3 6068
6069 /* Since builtin_types isn't gc'ed, don't export these nodes. */
6070 memset (builtin_types, 0, sizeof (builtin_types));
72040e7e 6071}
a66c9326 6072
79b01846 6073/* The number of named compound-literals generated thus far. */
6074static GTY(()) int compound_literal_number;
6075
6076/* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
6077
6078void
6079set_compound_literal_name (tree decl)
6080{
6081 char *name;
6082 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
6083 compound_literal_number);
6084 compound_literal_number++;
6085 DECL_NAME (decl) = get_identifier (name);
6086}
6087
2e474820 6088/* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
6089 TYPE and operand OP. */
6090
6091static tree
6092build_va_arg_1 (location_t loc, tree type, tree op)
6093{
6094 tree expr = build1 (VA_ARG_EXPR, type, op);
6095 SET_EXPR_LOCATION (expr, loc);
6096 return expr;
6097}
6098
6099/* Return a VA_ARG_EXPR corresponding to a source-level expression
6100 va_arg (EXPR, TYPE) at source location LOC. */
6101
a66c9326 6102tree
e60a6f7b 6103build_va_arg (location_t loc, tree expr, tree type)
a66c9326 6104{
c37be9ec 6105 tree va_type = TREE_TYPE (expr);
6106 tree canon_va_type = (va_type == error_mark_node
6107 ? NULL_TREE
6108 : targetm.canonical_va_list_type (va_type));
6109
2e474820 6110 if (va_type == error_mark_node
6111 || canon_va_type == NULL_TREE)
c37be9ec 6112 {
2e474820 6113 /* Let's handle things neutrallly, if expr:
6114 - has undeclared type, or
6115 - is not an va_list type. */
6116 return build_va_arg_1 (loc, type, expr);
c37be9ec 6117 }
f7fec1c7 6118
2e474820 6119 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
6120 {
6121 /* Case 1: Not an array type. */
6122
6123 /* Take the address, to get '&ap'. */
6124 mark_addressable (expr);
6125 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
6126
6127 /* Verify that &ap is still recognized as having va_list type. */
6128 tree canon_expr_type
6129 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6130 gcc_assert (canon_expr_type != NULL_TREE);
6131
6132 return build_va_arg_1 (loc, type, expr);
6133 }
6134
6135 /* Case 2: Array type.
6136
6137 Background:
6138
6139 For contrast, let's start with the simple case (case 1). If
6140 canon_va_type is not an array type, but say a char *, then when
6141 passing-by-value a va_list, the type of the va_list param decl is
6142 the same as for another va_list decl (all ap's are char *):
6143
6144 f2_1 (char * ap)
6145 D.1815 = VA_ARG (&ap, 0B, 1);
6146 return D.1815;
6147
6148 f2 (int i)
6149 char * ap.0;
6150 char * ap;
6151 __builtin_va_start (&ap, 0);
6152 ap.0 = ap;
6153 res = f2_1 (ap.0);
6154 __builtin_va_end (&ap);
6155 D.1812 = res;
6156 return D.1812;
6157
6158 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
6159 va_list the type of the va_list param decl (case 2b, struct * ap) is not
6160 the same as for another va_list decl (case 2a, struct ap[1]).
6161
6162 f2_1 (struct * ap)
6163 D.1844 = VA_ARG (ap, 0B, 0);
6164 return D.1844;
6165
6166 f2 (int i)
6167 struct ap[1];
6168 __builtin_va_start (&ap, 0);
6169 res = f2_1 (&ap);
6170 __builtin_va_end (&ap);
6171 D.1841 = res;
6172 return D.1841;
6173
6174 Case 2b is different because:
6175 - on the callee side, the parm decl has declared type va_list, but
6176 grokdeclarator changes the type of the parm decl to a pointer to the
6177 array elem type.
6178 - on the caller side, the pass-by-value uses &ap.
6179
6180 We unify these two cases (case 2a: va_list is array type,
6181 case 2b: va_list is pointer to array elem type), by adding '&' for the
6182 array type case, such that we have a pointer to array elem in both
6183 cases. */
6184
6185 if (TREE_CODE (va_type) == ARRAY_TYPE)
6186 {
6187 /* Case 2a: va_list is array type. */
6188
6189 /* Take the address, to get '&ap'. Make sure it's a pointer to array
6190 elem type. */
6191 mark_addressable (expr);
6192 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
6193 expr);
6194
6195 /* Verify that &ap is still recognized as having va_list type. */
6196 tree canon_expr_type
6197 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6198 gcc_assert (canon_expr_type != NULL_TREE);
6199 }
6200 else
6201 {
6202 /* Case 2b: va_list is pointer to array elem type. */
6203 gcc_assert (POINTER_TYPE_P (va_type));
6204 gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type));
6205
6206 /* Don't take the address. We've already got '&ap'. */
6207 ;
6208 }
6209
6210 return build_va_arg_1 (loc, type, expr);
a66c9326 6211}
0d4238dc 6212
6213
dd878098 6214/* Linked list of disabled built-in functions. */
6215
6216typedef struct disabled_builtin
6217{
6218 const char *name;
6219 struct disabled_builtin *next;
6220} disabled_builtin;
6221static disabled_builtin *disabled_builtins = NULL;
6222
1cae46be 6223static bool builtin_function_disabled_p (const char *);
dd878098 6224
6225/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
6226 begins with "__builtin_", give an error. */
6227
6228void
1cae46be 6229disable_builtin_function (const char *name)
dd878098 6230{
6231 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
b0b1af64 6232 error ("cannot disable built-in function %qs", name);
dd878098 6233 else
6234 {
e85905e5 6235 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
6236 new_disabled_builtin->name = name;
6237 new_disabled_builtin->next = disabled_builtins;
6238 disabled_builtins = new_disabled_builtin;
dd878098 6239 }
6240}
6241
6242
6243/* Return true if the built-in function NAME has been disabled, false
6244 otherwise. */
6245
6246static bool
1cae46be 6247builtin_function_disabled_p (const char *name)
dd878098 6248{
6249 disabled_builtin *p;
6250 for (p = disabled_builtins; p != NULL; p = p->next)
6251 {
6252 if (strcmp (name, p->name) == 0)
6253 return true;
6254 }
6255 return false;
6256}
6257
6258
3237155d 6259/* Worker for DEF_BUILTIN.
6260 Possibly define a builtin function with one or two names.
6261 Does not declare a non-__builtin_ function if flag_no_builtin, or if
6262 nonansi_p and flag_no_nonansi_builtin. */
0d4238dc 6263
3237155d 6264static void
6265def_builtin_1 (enum built_in_function fncode,
6266 const char *name,
6267 enum built_in_class fnclass,
6268 tree fntype, tree libtype,
6269 bool both_p, bool fallback_p, bool nonansi_p,
6270 tree fnattrs, bool implicit_p)
0d4238dc 6271{
3237155d 6272 tree decl;
6273 const char *libname;
6274
27213ba3 6275 if (fntype == error_mark_node)
6276 return;
6277
3237155d 6278 gcc_assert ((!both_p && !fallback_p)
6279 || !strncmp (name, "__builtin_",
6280 strlen ("__builtin_")));
6281
6282 libname = name + strlen ("__builtin_");
54be5d7e 6283 decl = add_builtin_function (name, fntype, fncode, fnclass,
6284 (fallback_p ? libname : NULL),
6285 fnattrs);
b9a16870 6286
6287 set_builtin_decl (fncode, decl, implicit_p);
6288
3237155d 6289 if (both_p
6290 && !flag_no_builtin && !builtin_function_disabled_p (libname)
dd878098 6291 && !(nonansi_p && flag_no_nonansi_builtin))
54be5d7e 6292 add_builtin_function (libname, libtype, fncode, fnclass,
6293 NULL, fnattrs);
0d4238dc 6294}
e94026da 6295\f
d7aeef06 6296/* Nonzero if the type T promotes to int. This is (nearly) the
6297 integral promotions defined in ISO C99 6.3.1.1/2. */
6298
6299bool
9f627b1a 6300c_promoting_integer_type_p (const_tree t)
d7aeef06 6301{
6302 switch (TREE_CODE (t))
6303 {
6304 case INTEGER_TYPE:
6305 return (TYPE_MAIN_VARIANT (t) == char_type_node
6306 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
6307 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
6308 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
7aa1e6eb 6309 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
6310 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d7aeef06 6311
6312 case ENUMERAL_TYPE:
6313 /* ??? Technically all enumerations not larger than an int
6314 promote to an int. But this is used along code paths
6315 that only want to notice a size change. */
6316 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6317
6318 case BOOLEAN_TYPE:
6319 return 1;
6320
6321 default:
6322 return 0;
6323 }
6324}
6325
e94026da 6326/* Return 1 if PARMS specifies a fixed number of parameters
6327 and none of their types is affected by default promotions. */
6328
6329int
9f627b1a 6330self_promoting_args_p (const_tree parms)
e94026da 6331{
9f627b1a 6332 const_tree t;
e94026da 6333 for (t = parms; t; t = TREE_CHAIN (t))
6334 {
19cb6b50 6335 tree type = TREE_VALUE (t);
43f74bc4 6336
e1d8e198 6337 if (type == error_mark_node)
6338 continue;
6339
e94026da 6340 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6341 return 0;
6342
6343 if (type == 0)
6344 return 0;
6345
6346 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6347 return 0;
6348
d7aeef06 6349 if (c_promoting_integer_type_p (type))
e94026da 6350 return 0;
6351 }
6352 return 1;
6353}
605fb01e 6354
c10de5e7 6355/* Recursively remove any '*' or '&' operator from TYPE. */
6356tree
6357strip_pointer_operator (tree t)
6358{
6359 while (POINTER_TYPE_P (t))
6360 t = TREE_TYPE (t);
6361 return t;
6362}
6363
57a0ed23 6364/* Recursively remove pointer or array type from TYPE. */
6365tree
6366strip_pointer_or_array_types (tree t)
6367{
6368 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6369 t = TREE_TYPE (t);
6370 return t;
6371}
6372
e41f0d80 6373/* Used to compare case labels. K1 and K2 are actually tree nodes
6374 representing case labels, or NULL_TREE for a `default' label.
6375 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6376 K2, and 0 if K1 and K2 are equal. */
6377
6378int
1cae46be 6379case_compare (splay_tree_key k1, splay_tree_key k2)
e41f0d80 6380{
6381 /* Consider a NULL key (such as arises with a `default' label) to be
6382 smaller than anything else. */
6383 if (!k1)
6384 return k2 ? -1 : 0;
6385 else if (!k2)
6386 return k1 ? 1 : 0;
6387
6388 return tree_int_cst_compare ((tree) k1, (tree) k2);
6389}
6390
e60a6f7b 6391/* Process a case label, located at LOC, for the range LOW_VALUE
6392 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6393 then this case label is actually a `default' label. If only
6394 HIGH_VALUE is NULL_TREE, then case label was declared using the
6395 usual C/C++ syntax, rather than the GNU case range extension.
6396 CASES is a tree containing all the case ranges processed so far;
6397 COND is the condition for the switch-statement itself. Returns the
6398 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
6399 is created. */
e41f0d80 6400
6401tree
e60a6f7b 6402c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
2ca392fd 6403 tree low_value, tree high_value)
e41f0d80 6404{
6405 tree type;
6406 tree label;
6407 tree case_label;
6408 splay_tree_node node;
6409
6410 /* Create the LABEL_DECL itself. */
e60a6f7b 6411 label = create_artificial_label (loc);
e41f0d80 6412
6413 /* If there was an error processing the switch condition, bail now
6414 before we get more confused. */
6415 if (!cond || cond == error_mark_node)
4ee9c684 6416 goto error_out;
e41f0d80 6417
1cae46be 6418 if ((low_value && TREE_TYPE (low_value)
6419 && POINTER_TYPE_P (TREE_TYPE (low_value)))
e41f0d80 6420 || (high_value && TREE_TYPE (high_value)
6421 && POINTER_TYPE_P (TREE_TYPE (high_value))))
b96dc121 6422 {
e60a6f7b 6423 error_at (loc, "pointers are not permitted as case values");
b96dc121 6424 goto error_out;
6425 }
e41f0d80 6426
6427 /* Case ranges are a GNU extension. */
8864917d 6428 if (high_value)
29438999 6429 pedwarn (loc, OPT_Wpedantic,
8864917d 6430 "range expressions in switch statements are non-standard");
e41f0d80 6431
6432 type = TREE_TYPE (cond);
6433 if (low_value)
6434 {
2d2f6a15 6435 low_value = check_case_value (loc, low_value);
22a75734 6436 low_value = convert_and_check (loc, type, low_value);
96722196 6437 if (low_value == error_mark_node)
6438 goto error_out;
e41f0d80 6439 }
6440 if (high_value)
6441 {
2d2f6a15 6442 high_value = check_case_value (loc, high_value);
22a75734 6443 high_value = convert_and_check (loc, type, high_value);
96722196 6444 if (high_value == error_mark_node)
6445 goto error_out;
e41f0d80 6446 }
6447
96722196 6448 if (low_value && high_value)
6449 {
6450 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
a0c938f0 6451 really a case range, even though it was written that way.
6452 Remove the HIGH_VALUE to simplify later processing. */
96722196 6453 if (tree_int_cst_equal (low_value, high_value))
6454 high_value = NULL_TREE;
6455 else if (!tree_int_cst_lt (low_value, high_value))
e60a6f7b 6456 warning_at (loc, 0, "empty range specified");
96722196 6457 }
e41f0d80 6458
2ca392fd 6459 /* See if the case is in range of the type of the original testing
6460 expression. If both low_value and high_value are out of range,
6461 don't insert the case label and return NULL_TREE. */
6462 if (low_value
f61a9bc2 6463 && !check_case_bounds (loc, type, orig_type,
84166705 6464 &low_value, high_value ? &high_value : NULL))
2ca392fd 6465 return NULL_TREE;
6466
e41f0d80 6467 /* Look up the LOW_VALUE in the table of case labels we already
6468 have. */
6469 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6470 /* If there was not an exact match, check for overlapping ranges.
6471 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6472 that's a `default' label and the only overlap is an exact match. */
6473 if (!node && (low_value || high_value))
6474 {
6475 splay_tree_node low_bound;
6476 splay_tree_node high_bound;
6477
6478 /* Even though there wasn't an exact match, there might be an
6479 overlap between this case range and another case range.
6480 Since we've (inductively) not allowed any overlapping case
6481 ranges, we simply need to find the greatest low case label
6482 that is smaller that LOW_VALUE, and the smallest low case
6483 label that is greater than LOW_VALUE. If there is an overlap
6484 it will occur in one of these two ranges. */
6485 low_bound = splay_tree_predecessor (cases,
6486 (splay_tree_key) low_value);
6487 high_bound = splay_tree_successor (cases,
6488 (splay_tree_key) low_value);
6489
6490 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6491 the LOW_VALUE, so there is no need to check unless the
6492 LOW_BOUND is in fact itself a case range. */
6493 if (low_bound
6494 && CASE_HIGH ((tree) low_bound->value)
6495 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6496 low_value) >= 0)
6497 node = low_bound;
6498 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6499 range is bigger than the low end of the current range, so we
6500 are only interested if the current range is a real range, and
6501 not an ordinary case label. */
1cae46be 6502 else if (high_bound
e41f0d80 6503 && high_value
6504 && (tree_int_cst_compare ((tree) high_bound->key,
6505 high_value)
6506 <= 0))
6507 node = high_bound;
6508 }
6509 /* If there was an overlap, issue an error. */
6510 if (node)
6511 {
eaae3b75 6512 tree duplicate = CASE_LABEL ((tree) node->value);
e41f0d80 6513
6514 if (high_value)
6515 {
e60a6f7b 6516 error_at (loc, "duplicate (or overlapping) case value");
6517 error_at (DECL_SOURCE_LOCATION (duplicate),
6518 "this is the first entry overlapping that value");
e41f0d80 6519 }
6520 else if (low_value)
6521 {
e60a6f7b 6522 error_at (loc, "duplicate case value") ;
6523 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
e41f0d80 6524 }
6525 else
6526 {
e60a6f7b 6527 error_at (loc, "multiple default labels in one switch");
6528 error_at (DECL_SOURCE_LOCATION (duplicate),
6529 "this is the first default label");
e41f0d80 6530 }
4ee9c684 6531 goto error_out;
e41f0d80 6532 }
6533
6534 /* Add a CASE_LABEL to the statement-tree. */
b6e3dd65 6535 case_label = add_stmt (build_case_label (low_value, high_value, label));
e41f0d80 6536 /* Register this case label in the splay tree. */
1cae46be 6537 splay_tree_insert (cases,
e41f0d80 6538 (splay_tree_key) low_value,
6539 (splay_tree_value) case_label);
6540
6541 return case_label;
4ee9c684 6542
6543 error_out:
daf6dff5 6544 /* Add a label so that the back-end doesn't think that the beginning of
4ee9c684 6545 the switch is unreachable. Note that we do not add a case label, as
a53ff4c1 6546 that just leads to duplicates and thence to failure later on. */
4ee9c684 6547 if (!cases->root)
6548 {
e60a6f7b 6549 tree t = create_artificial_label (loc);
6550 add_stmt (build_stmt (loc, LABEL_EXPR, t));
4ee9c684 6551 }
6552 return error_mark_node;
6553}
6554
6555/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6556 Used to verify that case values match up with enumerator values. */
6557
6558static void
6559match_case_to_enum_1 (tree key, tree type, tree label)
6560{
e913b5cd 6561 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6562
dd76621f 6563 if (tree_fits_uhwi_p (key))
6564 print_dec (key, buf, UNSIGNED);
6565 else if (tree_fits_shwi_p (key))
6566 print_dec (key, buf, SIGNED);
4ee9c684 6567 else
e913b5cd 6568 print_hex (key, buf);
4ee9c684 6569
6570 if (TYPE_NAME (type) == 0)
712d2297 6571 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6572 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6573 "case value %qs not in enumerated type",
6574 buf);
4ee9c684 6575 else
712d2297 6576 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6577 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6578 "case value %qs not in enumerated type %qT",
6579 buf, type);
4ee9c684 6580}
6581
359d87c6 6582/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6583 Used to verify that case values match up with enumerator values. */
6584
4ee9c684 6585static int
6586match_case_to_enum (splay_tree_node node, void *data)
6587{
6588 tree label = (tree) node->value;
4fd61bc6 6589 tree type = (tree) data;
4ee9c684 6590
6591 /* Skip default case. */
6592 if (!CASE_LOW (label))
6593 return 0;
6594
359d87c6 6595 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
4ee9c684 6596 when we did our enum->case scan. Reset our scratch bit after. */
359d87c6 6597 if (!CASE_LOW_SEEN (label))
4ee9c684 6598 match_case_to_enum_1 (CASE_LOW (label), type, label);
6599 else
359d87c6 6600 CASE_LOW_SEEN (label) = 0;
4ee9c684 6601
359d87c6 6602 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6603 not set, that means that CASE_HIGH did not appear when we did our
6604 enum->case scan. Reset our scratch bit after. */
4ee9c684 6605 if (CASE_HIGH (label))
6606 {
359d87c6 6607 if (!CASE_HIGH_SEEN (label))
6608 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6609 else
6610 CASE_HIGH_SEEN (label) = 0;
4ee9c684 6611 }
6612
6613 return 0;
6614}
6615
e7911019 6616/* Handle -Wswitch*. Called from the front end after parsing the
6617 switch construct. */
6618/* ??? Should probably be somewhere generic, since other languages
6619 besides C and C++ would want this. At the moment, however, C/C++
6620 are the only tree-ssa languages that support enumerations at all,
6621 so the point is moot. */
4ee9c684 6622
e7911019 6623void
6624c_do_switch_warnings (splay_tree cases, location_t switch_location,
6625 tree type, tree cond)
4ee9c684 6626{
b27ac6b5 6627 splay_tree_node default_node;
359d87c6 6628 splay_tree_node node;
6629 tree chain;
4ee9c684 6630
6631 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6632 return;
6633
4ee9c684 6634 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
8b6866af 6635 if (!default_node)
5fb6a912 6636 warning_at (switch_location, OPT_Wswitch_default,
6637 "switch missing default case");
4ee9c684 6638
359d87c6 6639 /* From here on, we only care about about enumerated types. */
6640 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6641 return;
6642
561017b5 6643 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6644 if (!warn_switch_enum && !warn_switch)
359d87c6 6645 return;
6646
561017b5 6647 /* Check the cases. Warn about case values which are not members of
6648 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6649 there is no default case, check that exactly all enumeration
6650 literals are covered by the cases. */
6651
359d87c6 6652 /* Clearing COND if it is not an integer constant simplifies
6653 the tests inside the loop below. */
6654 if (TREE_CODE (cond) != INTEGER_CST)
6655 cond = NULL_TREE;
6656
6657 /* The time complexity here is O(N*lg(N)) worst case, but for the
6658 common case of monotonically increasing enumerators, it is
6659 O(N), since the nature of the splay tree will keep the next
6660 element adjacent to the root at all times. */
4ee9c684 6661
359d87c6 6662 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6663 {
6664 tree value = TREE_VALUE (chain);
3f00a6c0 6665 if (TREE_CODE (value) == CONST_DECL)
6666 value = DECL_INITIAL (value);
359d87c6 6667 node = splay_tree_lookup (cases, (splay_tree_key) value);
6668 if (node)
4ee9c684 6669 {
359d87c6 6670 /* Mark the CASE_LOW part of the case entry as seen. */
6671 tree label = (tree) node->value;
6672 CASE_LOW_SEEN (label) = 1;
6673 continue;
6674 }
6675
6676 /* Even though there wasn't an exact match, there might be a
f0b5f617 6677 case range which includes the enumerator's value. */
359d87c6 6678 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6679 if (node && CASE_HIGH ((tree) node->value))
6680 {
6681 tree label = (tree) node->value;
6682 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6683 if (cmp >= 0)
4ee9c684 6684 {
359d87c6 6685 /* If we match the upper bound exactly, mark the CASE_HIGH
6686 part of the case entry as seen. */
6687 if (cmp == 0)
6688 CASE_HIGH_SEEN (label) = 1;
6689 continue;
4ee9c684 6690 }
6691 }
6692
359d87c6 6693 /* We've now determined that this enumerated literal isn't
6694 handled by the case labels of the switch statement. */
4ee9c684 6695
359d87c6 6696 /* If the switch expression is a constant, we only really care
6697 about whether that constant is handled by the switch. */
6698 if (cond && tree_int_cst_compare (cond, value))
6699 continue;
4ee9c684 6700
6cbbbc89 6701 /* If there is a default_node, the only relevant option is
561017b5 6702 Wswitch-enum. Otherwise, if both are enabled then we prefer
6cbbbc89 6703 to warn using -Wswitch because -Wswitch is enabled by -Wall
6704 while -Wswitch-enum is explicit. */
561017b5 6705 warning_at (switch_location,
6706 (default_node || !warn_switch
6707 ? OPT_Wswitch_enum
6708 : OPT_Wswitch),
6709 "enumeration value %qE not handled in switch",
6710 TREE_PURPOSE (chain));
4ee9c684 6711 }
359d87c6 6712
6713 /* Warn if there are case expressions that don't correspond to
6714 enumerators. This can occur since C and C++ don't enforce
6715 type-checking of assignments to enumeration variables.
6716
6717 The time complexity here is now always O(N) worst case, since
6718 we should have marked both the lower bound and upper bound of
6719 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6720 above. This scan also resets those fields. */
6cbbbc89 6721
359d87c6 6722 splay_tree_foreach (cases, match_case_to_enum, type);
e41f0d80 6723}
6724
9dd48740 6725/* Finish an expression taking the address of LABEL (an
dda49785 6726 IDENTIFIER_NODE). Returns an expression for the address.
6727
6728 LOC is the location for the expression returned. */
d0a47c8d 6729
1cae46be 6730tree
dda49785 6731finish_label_address_expr (tree label, location_t loc)
d0a47c8d 6732{
6733 tree result;
6734
29438999 6735 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
d0a47c8d 6736
9dd48740 6737 if (label == error_mark_node)
6738 return error_mark_node;
6739
d0a47c8d 6740 label = lookup_label (label);
6741 if (label == NULL_TREE)
6742 result = null_pointer_node;
6743 else
6744 {
6745 TREE_USED (label) = 1;
6746 result = build1 (ADDR_EXPR, ptr_type_node, label);
344cd9b2 6747 /* The current function is not necessarily uninlinable.
d0a47c8d 6748 Computed gotos are incompatible with inlining, but the value
6749 here could be used only in a diagnostic, for example. */
dda49785 6750 protected_set_expr_location (result, loc);
d0a47c8d 6751 }
6752
6753 return result;
6754}
4f9a1c9b 6755\f
6756
6757/* Given a boolean expression ARG, return a tree representing an increment
6758 or decrement (as indicated by CODE) of ARG. The front end must check for
6759 invalid cases (e.g., decrement in C++). */
6760tree
1cae46be 6761boolean_increment (enum tree_code code, tree arg)
4f9a1c9b 6762{
6763 tree val;
69db191c 6764 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
c0f19401 6765
4f9a1c9b 6766 arg = stabilize_reference (arg);
6767 switch (code)
6768 {
6769 case PREINCREMENT_EXPR:
14ae0310 6770 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6771 break;
6772 case POSTINCREMENT_EXPR:
14ae0310 6773 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6774 arg = save_expr (arg);
14ae0310 6775 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6776 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6777 break;
6778 case PREDECREMENT_EXPR:
14ae0310 6779 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6780 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6781 break;
6782 case POSTDECREMENT_EXPR:
14ae0310 6783 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6784 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6785 arg = save_expr (arg);
14ae0310 6786 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6787 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6788 break;
6789 default:
231bd014 6790 gcc_unreachable ();
4f9a1c9b 6791 }
6792 TREE_SIDE_EFFECTS (val) = 1;
6793 return val;
6794}
76a6e674 6795\f
f3449a3c 6796/* Built-in macros for stddef.h and stdint.h, that require macros
6797 defined in this file. */
79cf3ec1 6798void
1cae46be 6799c_stddef_cpp_builtins(void)
1ed9d5f5 6800{
63994318 6801 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6802 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6803 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6804 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
36bccbfc 6805 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6806 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
b0726616 6807 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6808 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
f3449a3c 6809 if (SIG_ATOMIC_TYPE)
6810 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6811 if (INT8_TYPE)
6812 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6813 if (INT16_TYPE)
6814 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6815 if (INT32_TYPE)
6816 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6817 if (INT64_TYPE)
6818 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6819 if (UINT8_TYPE)
6820 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6821 if (UINT16_TYPE)
6822 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6823 if (UINT32_TYPE)
6824 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6825 if (UINT64_TYPE)
6826 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6827 if (INT_LEAST8_TYPE)
6828 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6829 if (INT_LEAST16_TYPE)
6830 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6831 if (INT_LEAST32_TYPE)
6832 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6833 if (INT_LEAST64_TYPE)
6834 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6835 if (UINT_LEAST8_TYPE)
6836 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6837 if (UINT_LEAST16_TYPE)
6838 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6839 if (UINT_LEAST32_TYPE)
6840 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6841 if (UINT_LEAST64_TYPE)
6842 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6843 if (INT_FAST8_TYPE)
6844 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6845 if (INT_FAST16_TYPE)
6846 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6847 if (INT_FAST32_TYPE)
6848 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6849 if (INT_FAST64_TYPE)
6850 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6851 if (UINT_FAST8_TYPE)
6852 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6853 if (UINT_FAST16_TYPE)
6854 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6855 if (UINT_FAST32_TYPE)
6856 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6857 if (UINT_FAST64_TYPE)
6858 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6859 if (INTPTR_TYPE)
6860 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6861 if (UINTPTR_TYPE)
6862 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
574006c3 6863}
6864
7d3b509a 6865static void
1cae46be 6866c_init_attributes (void)
7d3b509a 6867{
6868 /* Fill in the built_in_attributes array. */
7c446c95 6869#define DEF_ATTR_NULL_TREE(ENUM) \
7d3b509a 6870 built_in_attributes[(int) ENUM] = NULL_TREE;
7c446c95 6871#define DEF_ATTR_INT(ENUM, VALUE) \
ceb7b692 6872 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
c8010b80 6873#define DEF_ATTR_STRING(ENUM, VALUE) \
6874 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
7d3b509a 6875#define DEF_ATTR_IDENT(ENUM, STRING) \
6876 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6877#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6878 built_in_attributes[(int) ENUM] \
6879 = tree_cons (built_in_attributes[(int) PURPOSE], \
6880 built_in_attributes[(int) VALUE], \
6881 built_in_attributes[(int) CHAIN]);
7d3b509a 6882#include "builtin-attrs.def"
6883#undef DEF_ATTR_NULL_TREE
6884#undef DEF_ATTR_INT
6885#undef DEF_ATTR_IDENT
6886#undef DEF_ATTR_TREE_LIST
76a6e674 6887}
5f3cead1 6888
33199a81 6889/* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6890 identifier as an argument, so the front end shouldn't look it up. */
6891
6892bool
47b19b94 6893attribute_takes_identifier_p (const_tree attr_id)
33199a81 6894{
9bf1c74e 6895 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
02cb1060 6896 if (spec == NULL)
6897 /* Unknown attribute that we'll end up ignoring, return true so we
6898 don't complain about an identifier argument. */
6899 return true;
6900 else if (!strcmp ("mode", spec->name)
6901 || !strcmp ("format", spec->name)
6902 || !strcmp ("cleanup", spec->name))
47b19b94 6903 return true;
6904 else
6905 return targetm.attribute_takes_identifier_p (attr_id);
33199a81 6906}
6907
f8e93a2e 6908/* Attribute handlers common to C front ends. */
6909
6910/* Handle a "packed" attribute; arguments as in
6911 struct attribute_spec.handler. */
6912
6913static tree
9a03a746 6914handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
09347743 6915 int flags, bool *no_add_attrs)
f8e93a2e 6916{
f40175cb 6917 if (TYPE_P (*node))
f8e93a2e 6918 {
6919 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 6920 *node = build_variant_type_copy (*node);
f40175cb 6921 TYPE_PACKED (*node) = 1;
f8e93a2e 6922 }
6923 else if (TREE_CODE (*node) == FIELD_DECL)
c2ab04f9 6924 {
9fd767c5 6925 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6926 /* Still pack bitfields. */
6927 && ! DECL_INITIAL (*node))
c2ab04f9 6928 warning (OPT_Wattributes,
6929 "%qE attribute ignored for field of type %qT",
6930 name, TREE_TYPE (*node));
6931 else
6932 DECL_PACKED (*node) = 1;
6933 }
f8e93a2e 6934 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
f40175cb 6935 used for DECL_REGISTER. It wouldn't mean anything anyway.
6936 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6937 that changes what the typedef is typing. */
f8e93a2e 6938 else
6939 {
9b2d6d13 6940 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6941 *no_add_attrs = true;
6942 }
6943
6944 return NULL_TREE;
6945}
6946
6947/* Handle a "nocommon" attribute; arguments as in
6948 struct attribute_spec.handler. */
6949
6950static tree
1cae46be 6951handle_nocommon_attribute (tree *node, tree name,
9a03a746 6952 tree ARG_UNUSED (args),
6953 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6954{
6955 if (TREE_CODE (*node) == VAR_DECL)
6956 DECL_COMMON (*node) = 0;
6957 else
6958 {
9b2d6d13 6959 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6960 *no_add_attrs = true;
6961 }
6962
6963 return NULL_TREE;
6964}
6965
6966/* Handle a "common" attribute; arguments as in
6967 struct attribute_spec.handler. */
6968
6969static tree
9a03a746 6970handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6971 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6972{
6973 if (TREE_CODE (*node) == VAR_DECL)
6974 DECL_COMMON (*node) = 1;
6975 else
6976 {
9b2d6d13 6977 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6978 *no_add_attrs = true;
6979 }
6980
6981 return NULL_TREE;
6982}
6983
6984/* Handle a "noreturn" attribute; arguments as in
6985 struct attribute_spec.handler. */
6986
6987static tree
9a03a746 6988handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6989 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6990{
6991 tree type = TREE_TYPE (*node);
6992
6993 /* See FIXME comment in c_common_attribute_table. */
8c582e4f 6994 if (TREE_CODE (*node) == FUNCTION_DECL
6995 || objc_method_decl (TREE_CODE (*node)))
f8e93a2e 6996 TREE_THIS_VOLATILE (*node) = 1;
6997 else if (TREE_CODE (type) == POINTER_TYPE
6998 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6999 TREE_TYPE (*node)
9d4eeb52 7000 = (build_qualified_type
7001 (build_pointer_type
7002 (build_type_variant (TREE_TYPE (type),
7003 TYPE_READONLY (TREE_TYPE (type)), 1)),
7004 TYPE_QUALS (type)));
f8e93a2e 7005 else
7006 {
9b2d6d13 7007 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7008 *no_add_attrs = true;
7009 }
7010
7011 return NULL_TREE;
7012}
7013
5de92639 7014/* Handle a "hot" and attribute; arguments as in
7015 struct attribute_spec.handler. */
7016
7017static tree
7018handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
46f8e3b0 7019 int ARG_UNUSED (flags), bool *no_add_attrs)
5de92639 7020{
758a38ab 7021 if (TREE_CODE (*node) == FUNCTION_DECL
7022 || TREE_CODE (*node) == LABEL_DECL)
5de92639 7023 {
7024 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
7025 {
4a026b48 7026 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7027 "with attribute %qs", name, "cold");
5de92639 7028 *no_add_attrs = true;
7029 }
24470055 7030 /* Most of the rest of the hot processing is done later with
7031 lookup_attribute. */
5de92639 7032 }
7033 else
7034 {
7035 warning (OPT_Wattributes, "%qE attribute ignored", name);
7036 *no_add_attrs = true;
7037 }
7038
7039 return NULL_TREE;
7040}
758a38ab 7041
5de92639 7042/* Handle a "cold" and attribute; arguments as in
7043 struct attribute_spec.handler. */
7044
7045static tree
7046handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7047 int ARG_UNUSED (flags), bool *no_add_attrs)
7048{
758a38ab 7049 if (TREE_CODE (*node) == FUNCTION_DECL
7050 || TREE_CODE (*node) == LABEL_DECL)
5de92639 7051 {
7052 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
7053 {
4a026b48 7054 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7055 "with attribute %qs", name, "hot");
5de92639 7056 *no_add_attrs = true;
7057 }
24470055 7058 /* Most of the rest of the cold processing is done later with
7059 lookup_attribute. */
5de92639 7060 }
7061 else
7062 {
7063 warning (OPT_Wattributes, "%qE attribute ignored", name);
7064 *no_add_attrs = true;
7065 }
7066
7067 return NULL_TREE;
7068}
7069
a9196da9 7070/* Handle a "no_sanitize_address" attribute; arguments as in
d413ffdd 7071 struct attribute_spec.handler. */
7072
7073static tree
a9196da9 7074handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
7075 bool *no_add_attrs)
d413ffdd 7076{
7077 if (TREE_CODE (*node) != FUNCTION_DECL)
7078 {
7079 warning (OPT_Wattributes, "%qE attribute ignored", name);
7080 *no_add_attrs = true;
7081 }
7082
7083 return NULL_TREE;
7084}
7085
a9196da9 7086/* Handle a "no_address_safety_analysis" attribute; arguments as in
7087 struct attribute_spec.handler. */
7088
7089static tree
7090handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
7091 bool *no_add_attrs)
7092{
7093 if (TREE_CODE (*node) != FUNCTION_DECL)
7094 warning (OPT_Wattributes, "%qE attribute ignored", name);
7095 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
7096 DECL_ATTRIBUTES (*node)
7097 = tree_cons (get_identifier ("no_sanitize_address"),
7098 NULL_TREE, DECL_ATTRIBUTES (*node));
7099 *no_add_attrs = true;
7100 return NULL_TREE;
7101}
7102
05f893e1 7103/* Handle a "no_sanitize_undefined" attribute; arguments as in
7104 struct attribute_spec.handler. */
7105
7106static tree
7107handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
7108 bool *no_add_attrs)
7109{
7110 if (TREE_CODE (*node) != FUNCTION_DECL)
7111 {
7112 warning (OPT_Wattributes, "%qE attribute ignored", name);
7113 *no_add_attrs = true;
7114 }
7115
7116 return NULL_TREE;
7117}
7118
947aa916 7119/* Handle a "stack_protect" attribute; arguments as in
7120 struct attribute_spec.handler. */
7121static tree
7122handle_stack_protect_attribute (tree *node, tree name, tree, int,
7123 bool *no_add_attrs)
7124{
7125 if (TREE_CODE (*node) != FUNCTION_DECL)
7126 {
7127 warning (OPT_Wattributes, "%qE attribute ignored", name);
7128 *no_add_attrs = true;
7129 }
7130 else
7131 DECL_ATTRIBUTES (*node)
7132 = tree_cons (get_identifier ("stack_protect"),
7133 NULL_TREE, DECL_ATTRIBUTES (*node));
7134
7135 return NULL_TREE;
7136}
7137
f8e93a2e 7138/* Handle a "noinline" attribute; arguments as in
7139 struct attribute_spec.handler. */
7140
7141static tree
1cae46be 7142handle_noinline_attribute (tree *node, tree name,
9a03a746 7143 tree ARG_UNUSED (args),
7144 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7145{
7146 if (TREE_CODE (*node) == FUNCTION_DECL)
4a026b48 7147 {
7148 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
7149 {
7150 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7151 "with attribute %qs", name, "always_inline");
7152 *no_add_attrs = true;
7153 }
7154 else
7155 DECL_UNINLINABLE (*node) = 1;
7156 }
f8e93a2e 7157 else
7158 {
9b2d6d13 7159 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7160 *no_add_attrs = true;
7161 }
7162
7163 return NULL_TREE;
7164}
7165
bdb1f0d1 7166/* Handle a "noclone" attribute; arguments as in
7167 struct attribute_spec.handler. */
7168
7169static tree
7170handle_noclone_attribute (tree *node, tree name,
7171 tree ARG_UNUSED (args),
7172 int ARG_UNUSED (flags), bool *no_add_attrs)
7173{
7174 if (TREE_CODE (*node) != FUNCTION_DECL)
7175 {
7176 warning (OPT_Wattributes, "%qE attribute ignored", name);
7177 *no_add_attrs = true;
7178 }
7179
7180 return NULL_TREE;
7181}
7182
85fbea97 7183/* Handle a "no_icf" attribute; arguments as in
7184 struct attribute_spec.handler. */
7185
7186static tree
7187handle_noicf_attribute (tree *node, tree name,
7188 tree ARG_UNUSED (args),
7189 int ARG_UNUSED (flags), bool *no_add_attrs)
7190{
7191 if (TREE_CODE (*node) != FUNCTION_DECL)
7192 {
7193 warning (OPT_Wattributes, "%qE attribute ignored", name);
7194 *no_add_attrs = true;
7195 }
7196
7197 return NULL_TREE;
7198}
7199
7200
f8e93a2e 7201/* Handle a "always_inline" attribute; arguments as in
7202 struct attribute_spec.handler. */
7203
7204static tree
1cae46be 7205handle_always_inline_attribute (tree *node, tree name,
9a03a746 7206 tree ARG_UNUSED (args),
7207 int ARG_UNUSED (flags),
09347743 7208 bool *no_add_attrs)
f8e93a2e 7209{
7210 if (TREE_CODE (*node) == FUNCTION_DECL)
7211 {
4a026b48 7212 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
7213 {
7214 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7215 "with %qs attribute", name, "noinline");
7216 *no_add_attrs = true;
7217 }
7218 else
7219 /* Set the attribute and mark it for disregarding inline
7220 limits. */
7221 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
f8e93a2e 7222 }
7223 else
7224 {
9b2d6d13 7225 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7226 *no_add_attrs = true;
541e4101 7227 }
7228
7229 return NULL_TREE;
7230}
7231
7232/* Handle a "gnu_inline" attribute; arguments as in
7233 struct attribute_spec.handler. */
7234
7235static tree
7236handle_gnu_inline_attribute (tree *node, tree name,
7237 tree ARG_UNUSED (args),
7238 int ARG_UNUSED (flags),
7239 bool *no_add_attrs)
7240{
7241 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7242 {
7243 /* Do nothing else, just set the attribute. We'll get at
7244 it later with lookup_attribute. */
7245 }
7246 else
7247 {
7248 warning (OPT_Wattributes, "%qE attribute ignored", name);
7249 *no_add_attrs = true;
7bd95dfd 7250 }
7251
7252 return NULL_TREE;
7253}
7254
7255/* Handle a "leaf" attribute; arguments as in
7256 struct attribute_spec.handler. */
7257
7258static tree
7259handle_leaf_attribute (tree *node, tree name,
7260 tree ARG_UNUSED (args),
7261 int ARG_UNUSED (flags), bool *no_add_attrs)
7262{
7263 if (TREE_CODE (*node) != FUNCTION_DECL)
7264 {
7265 warning (OPT_Wattributes, "%qE attribute ignored", name);
7266 *no_add_attrs = true;
7267 }
7268 if (!TREE_PUBLIC (*node))
7269 {
7270 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
7271 *no_add_attrs = true;
f8e93a2e 7272 }
7273
7274 return NULL_TREE;
7275}
7276
1b16fc45 7277/* Handle an "artificial" attribute; arguments as in
7278 struct attribute_spec.handler. */
7279
7280static tree
7281handle_artificial_attribute (tree *node, tree name,
7282 tree ARG_UNUSED (args),
7283 int ARG_UNUSED (flags),
7284 bool *no_add_attrs)
7285{
7286 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7287 {
7288 /* Do nothing else, just set the attribute. We'll get at
7289 it later with lookup_attribute. */
7290 }
7291 else
7292 {
7293 warning (OPT_Wattributes, "%qE attribute ignored", name);
7294 *no_add_attrs = true;
7295 }
7296
7297 return NULL_TREE;
7298}
7299
0cdd9887 7300/* Handle a "flatten" attribute; arguments as in
7301 struct attribute_spec.handler. */
7302
7303static tree
7304handle_flatten_attribute (tree *node, tree name,
a0c938f0 7305 tree args ATTRIBUTE_UNUSED,
7306 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
0cdd9887 7307{
7308 if (TREE_CODE (*node) == FUNCTION_DECL)
7309 /* Do nothing else, just set the attribute. We'll get at
7310 it later with lookup_attribute. */
7311 ;
7312 else
7313 {
7314 warning (OPT_Wattributes, "%qE attribute ignored", name);
7315 *no_add_attrs = true;
7316 }
7317
7318 return NULL_TREE;
7319}
7320
10fc867f 7321/* Handle a "warning" or "error" attribute; arguments as in
7322 struct attribute_spec.handler. */
7323
7324static tree
7325handle_error_attribute (tree *node, tree name, tree args,
7326 int ARG_UNUSED (flags), bool *no_add_attrs)
7327{
7328 if (TREE_CODE (*node) == FUNCTION_DECL
df936998 7329 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
10fc867f 7330 /* Do nothing else, just set the attribute. We'll get at
7331 it later with lookup_attribute. */
7332 ;
7333 else
7334 {
7335 warning (OPT_Wattributes, "%qE attribute ignored", name);
7336 *no_add_attrs = true;
7337 }
7338
7339 return NULL_TREE;
7340}
0cdd9887 7341
f8e93a2e 7342/* Handle a "used" attribute; arguments as in
7343 struct attribute_spec.handler. */
7344
7345static tree
9a03a746 7346handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7347 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7348{
d0a31bd8 7349 tree node = *pnode;
7350
7351 if (TREE_CODE (node) == FUNCTION_DECL
a4e3ffad 7352 || (TREE_CODE (node) == VAR_DECL && TREE_STATIC (node))
7353 || (TREE_CODE (node) == TYPE_DECL))
f54ed8bc 7354 {
f54ed8bc 7355 TREE_USED (node) = 1;
9423c9b7 7356 DECL_PRESERVE_P (node) = 1;
abc6c64f 7357 if (TREE_CODE (node) == VAR_DECL)
7358 DECL_READ_P (node) = 1;
f54ed8bc 7359 }
f8e93a2e 7360 else
7361 {
9b2d6d13 7362 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7363 *no_add_attrs = true;
7364 }
7365
7366 return NULL_TREE;
7367}
7368
7369/* Handle a "unused" attribute; arguments as in
7370 struct attribute_spec.handler. */
7371
7372static tree
9a03a746 7373handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7374 int flags, bool *no_add_attrs)
f8e93a2e 7375{
7376 if (DECL_P (*node))
7377 {
7378 tree decl = *node;
7379
7380 if (TREE_CODE (decl) == PARM_DECL
7381 || TREE_CODE (decl) == VAR_DECL
7382 || TREE_CODE (decl) == FUNCTION_DECL
7383 || TREE_CODE (decl) == LABEL_DECL
7384 || TREE_CODE (decl) == TYPE_DECL)
abc6c64f 7385 {
7386 TREE_USED (decl) = 1;
7387 if (TREE_CODE (decl) == VAR_DECL
7388 || TREE_CODE (decl) == PARM_DECL)
7389 DECL_READ_P (decl) = 1;
7390 }
f8e93a2e 7391 else
7392 {
9b2d6d13 7393 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7394 *no_add_attrs = true;
7395 }
7396 }
7397 else
7398 {
7399 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 7400 *node = build_variant_type_copy (*node);
f8e93a2e 7401 TREE_USED (*node) = 1;
7402 }
7403
7404 return NULL_TREE;
7405}
7406
62eec3b4 7407/* Handle a "externally_visible" attribute; arguments as in
7408 struct attribute_spec.handler. */
7409
7410static tree
7411handle_externally_visible_attribute (tree *pnode, tree name,
7412 tree ARG_UNUSED (args),
7413 int ARG_UNUSED (flags),
7414 bool *no_add_attrs)
7415{
7416 tree node = *pnode;
7417
b443c459 7418 if (VAR_OR_FUNCTION_DECL_P (node))
62eec3b4 7419 {
ba12ea31 7420 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7421 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7422 {
7423 warning (OPT_Wattributes,
7424 "%qE attribute have effect only on public objects", name);
7425 *no_add_attrs = true;
7426 }
62eec3b4 7427 }
62eec3b4 7428 else
7429 {
7430 warning (OPT_Wattributes, "%qE attribute ignored", name);
7431 *no_add_attrs = true;
7432 }
7433
7434 return NULL_TREE;
7435}
7436
6b722052 7437/* Handle the "no_reorder" attribute. Arguments as in
7438 struct attribute_spec.handler. */
7439
7440static tree
7441handle_no_reorder_attribute (tree *pnode,
7442 tree name,
7443 tree,
7444 int,
7445 bool *no_add_attrs)
7446{
7447 tree node = *pnode;
7448
b443c459 7449 if (!VAR_OR_FUNCTION_DECL_P (node)
6b722052 7450 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7451 {
7452 warning (OPT_Wattributes,
7453 "%qE attribute only affects top level objects",
7454 name);
7455 *no_add_attrs = true;
7456 }
7457
7458 return NULL_TREE;
7459}
7460
f8e93a2e 7461/* Handle a "const" attribute; arguments as in
7462 struct attribute_spec.handler. */
7463
7464static tree
9a03a746 7465handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7466 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7467{
7468 tree type = TREE_TYPE (*node);
7469
7470 /* See FIXME comment on noreturn in c_common_attribute_table. */
7471 if (TREE_CODE (*node) == FUNCTION_DECL)
7472 TREE_READONLY (*node) = 1;
7473 else if (TREE_CODE (type) == POINTER_TYPE
7474 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7475 TREE_TYPE (*node)
9d4eeb52 7476 = (build_qualified_type
7477 (build_pointer_type
7478 (build_type_variant (TREE_TYPE (type), 1,
7479 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7480 TYPE_QUALS (type)));
f8e93a2e 7481 else
7482 {
9b2d6d13 7483 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7484 *no_add_attrs = true;
7485 }
7486
7487 return NULL_TREE;
7488}
7489
7490/* Handle a "transparent_union" attribute; arguments as in
7491 struct attribute_spec.handler. */
7492
7493static tree
1cae46be 7494handle_transparent_union_attribute (tree *node, tree name,
9a03a746 7495 tree ARG_UNUSED (args), int flags,
09347743 7496 bool *no_add_attrs)
f8e93a2e 7497{
881eb642 7498 tree type;
03908818 7499
7500 *no_add_attrs = true;
f8e93a2e 7501
ffcdbf9c 7502
7503 if (TREE_CODE (*node) == TYPE_DECL
7504 && ! (flags & ATTR_FLAG_CXX11))
881eb642 7505 node = &TREE_TYPE (*node);
7506 type = *node;
f8e93a2e 7507
03908818 7508 if (TREE_CODE (type) == UNION_TYPE)
f8e93a2e 7509 {
fca86134 7510 /* Make sure that the first field will work for a transparent union.
7511 If the type isn't complete yet, leave the check to the code in
7512 finish_struct. */
7513 if (TYPE_SIZE (type))
7514 {
7515 tree first = first_field (type);
7516 if (first == NULL_TREE
7517 || DECL_ARTIFICIAL (first)
7518 || TYPE_MODE (type) != DECL_MODE (first))
7519 goto ignored;
7520 }
7521
f8e93a2e 7522 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
03908818 7523 {
fca86134 7524 /* If the type isn't complete yet, setting the flag
7525 on a variant wouldn't ever be checked. */
7526 if (!TYPE_SIZE (type))
7527 goto ignored;
7528
7529 /* build_duplicate_type doesn't work for C++. */
7530 if (c_dialect_cxx ())
03908818 7531 goto ignored;
7532
7533 /* A type variant isn't good enough, since we don't a cast
7534 to such a type removed as a no-op. */
7535 *node = type = build_duplicate_type (type);
7536 }
7537
8df5a43d 7538 TYPE_TRANSPARENT_AGGR (type) = 1;
03908818 7539 return NULL_TREE;
f8e93a2e 7540 }
7541
03908818 7542 ignored:
7543 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7544 return NULL_TREE;
7545}
7546
9af7fd5b 7547/* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7548 get the requested priority for a constructor or destructor,
7549 possibly issuing diagnostics for invalid or reserved
7550 priorities. */
7551
7552static priority_type
7553get_priority (tree args, bool is_destructor)
7554{
7555 HOST_WIDE_INT pri;
6c181a06 7556 tree arg;
9af7fd5b 7557
7558 if (!args)
7559 return DEFAULT_INIT_PRIORITY;
48e1416a 7560
28fbc04f 7561 if (!SUPPORTS_INIT_PRIORITY)
7562 {
7563 if (is_destructor)
7564 error ("destructor priorities are not supported");
7565 else
7566 error ("constructor priorities are not supported");
7567 return DEFAULT_INIT_PRIORITY;
7568 }
7569
6c181a06 7570 arg = TREE_VALUE (args);
253e1cae 7571 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7572 goto invalid;
7573 if (arg == error_mark_node)
7574 return DEFAULT_INIT_PRIORITY;
c28ddc97 7575 arg = default_conversion (arg);
e913b5cd 7576 if (!tree_fits_shwi_p (arg)
6c181a06 7577 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
9af7fd5b 7578 goto invalid;
7579
e913b5cd 7580 pri = tree_to_shwi (arg);
9af7fd5b 7581 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7582 goto invalid;
7583
7584 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7585 {
7586 if (is_destructor)
7587 warning (0,
7588 "destructor priorities from 0 to %d are reserved "
48e1416a 7589 "for the implementation",
9af7fd5b 7590 MAX_RESERVED_INIT_PRIORITY);
7591 else
7592 warning (0,
7593 "constructor priorities from 0 to %d are reserved "
48e1416a 7594 "for the implementation",
9af7fd5b 7595 MAX_RESERVED_INIT_PRIORITY);
7596 }
7597 return pri;
7598
7599 invalid:
7600 if (is_destructor)
7601 error ("destructor priorities must be integers from 0 to %d inclusive",
7602 MAX_INIT_PRIORITY);
7603 else
7604 error ("constructor priorities must be integers from 0 to %d inclusive",
7605 MAX_INIT_PRIORITY);
7606 return DEFAULT_INIT_PRIORITY;
7607}
7608
f8e93a2e 7609/* Handle a "constructor" attribute; arguments as in
7610 struct attribute_spec.handler. */
7611
7612static tree
9af7fd5b 7613handle_constructor_attribute (tree *node, tree name, tree args,
9a03a746 7614 int ARG_UNUSED (flags),
09347743 7615 bool *no_add_attrs)
f8e93a2e 7616{
7617 tree decl = *node;
7618 tree type = TREE_TYPE (decl);
7619
7620 if (TREE_CODE (decl) == FUNCTION_DECL
7621 && TREE_CODE (type) == FUNCTION_TYPE
7622 && decl_function_context (decl) == 0)
7623 {
9af7fd5b 7624 priority_type priority;
f8e93a2e 7625 DECL_STATIC_CONSTRUCTOR (decl) = 1;
9af7fd5b 7626 priority = get_priority (args, /*is_destructor=*/false);
7627 SET_DECL_INIT_PRIORITY (decl, priority);
f8e93a2e 7628 TREE_USED (decl) = 1;
7629 }
7630 else
7631 {
9b2d6d13 7632 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7633 *no_add_attrs = true;
7634 }
7635
7636 return NULL_TREE;
7637}
7638
7639/* Handle a "destructor" attribute; arguments as in
7640 struct attribute_spec.handler. */
7641
7642static tree
9af7fd5b 7643handle_destructor_attribute (tree *node, tree name, tree args,
9a03a746 7644 int ARG_UNUSED (flags),
09347743 7645 bool *no_add_attrs)
f8e93a2e 7646{
7647 tree decl = *node;
7648 tree type = TREE_TYPE (decl);
7649
7650 if (TREE_CODE (decl) == FUNCTION_DECL
7651 && TREE_CODE (type) == FUNCTION_TYPE
7652 && decl_function_context (decl) == 0)
7653 {
9af7fd5b 7654 priority_type priority;
f8e93a2e 7655 DECL_STATIC_DESTRUCTOR (decl) = 1;
9af7fd5b 7656 priority = get_priority (args, /*is_destructor=*/true);
7657 SET_DECL_FINI_PRIORITY (decl, priority);
f8e93a2e 7658 TREE_USED (decl) = 1;
7659 }
7660 else
7661 {
9b2d6d13 7662 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7663 *no_add_attrs = true;
7664 }
7665
7666 return NULL_TREE;
7667}
7668
1c58e3f1 7669/* Nonzero if the mode is a valid vector mode for this architecture.
7670 This returns nonzero even if there is no hardware support for the
7671 vector mode, but we can emulate with narrower modes. */
7672
7673static int
3754d046 7674vector_mode_valid_p (machine_mode mode)
1c58e3f1 7675{
7676 enum mode_class mclass = GET_MODE_CLASS (mode);
3754d046 7677 machine_mode innermode;
1c58e3f1 7678
7679 /* Doh! What's going on? */
7680 if (mclass != MODE_VECTOR_INT
7681 && mclass != MODE_VECTOR_FLOAT
7682 && mclass != MODE_VECTOR_FRACT
7683 && mclass != MODE_VECTOR_UFRACT
7684 && mclass != MODE_VECTOR_ACCUM
7685 && mclass != MODE_VECTOR_UACCUM)
7686 return 0;
7687
7688 /* Hardware support. Woo hoo! */
7689 if (targetm.vector_mode_supported_p (mode))
7690 return 1;
7691
7692 innermode = GET_MODE_INNER (mode);
7693
7694 /* We should probably return 1 if requesting V4DI and we have no DI,
7695 but we have V2DI, but this is probably very unlikely. */
7696
7697 /* If we have support for the inner mode, we can safely emulate it.
7698 We may not have V2DI, but me can emulate with a pair of DIs. */
7699 return targetm.scalar_mode_supported_p (innermode);
7700}
7701
7702
f8e93a2e 7703/* Handle a "mode" attribute; arguments as in
7704 struct attribute_spec.handler. */
7705
7706static tree
9a03a746 7707handle_mode_attribute (tree *node, tree name, tree args,
7708 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7709{
7710 tree type = *node;
d1dd9ac0 7711 tree ident = TREE_VALUE (args);
f8e93a2e 7712
7713 *no_add_attrs = true;
ab2c1de8 7714
d1dd9ac0 7715 if (TREE_CODE (ident) != IDENTIFIER_NODE)
9b2d6d13 7716 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7717 else
7718 {
7719 int j;
d1dd9ac0 7720 const char *p = IDENTIFIER_POINTER (ident);
f8e93a2e 7721 int len = strlen (p);
3754d046 7722 machine_mode mode = VOIDmode;
f8e93a2e 7723 tree typefm;
b2aef146 7724 bool valid_mode;
f8e93a2e 7725
7726 if (len > 4 && p[0] == '_' && p[1] == '_'
7727 && p[len - 1] == '_' && p[len - 2] == '_')
7728 {
4fd61bc6 7729 char *newp = (char *) alloca (len - 1);
f8e93a2e 7730
7731 strcpy (newp, &p[2]);
7732 newp[len - 4] = '\0';
7733 p = newp;
7734 }
7735
7736 /* Change this type to have a type with the specified mode.
7737 First check for the special modes. */
84166705 7738 if (!strcmp (p, "byte"))
f8e93a2e 7739 mode = byte_mode;
7740 else if (!strcmp (p, "word"))
7741 mode = word_mode;
84166705 7742 else if (!strcmp (p, "pointer"))
f8e93a2e 7743 mode = ptr_mode;
0ef89dfd 7744 else if (!strcmp (p, "libgcc_cmp_return"))
7745 mode = targetm.libgcc_cmp_return_mode ();
7746 else if (!strcmp (p, "libgcc_shift_count"))
7747 mode = targetm.libgcc_shift_count_mode ();
1bd43494 7748 else if (!strcmp (p, "unwind_word"))
7749 mode = targetm.unwind_word_mode ();
f8e93a2e 7750 else
7751 for (j = 0; j < NUM_MACHINE_MODES; j++)
7752 if (!strcmp (p, GET_MODE_NAME (j)))
743a6f47 7753 {
3754d046 7754 mode = (machine_mode) j;
743a6f47 7755 break;
7756 }
f8e93a2e 7757
7758 if (mode == VOIDmode)
4917c376 7759 {
d1dd9ac0 7760 error ("unknown machine mode %qE", ident);
4917c376 7761 return NULL_TREE;
7762 }
7763
b2aef146 7764 valid_mode = false;
7765 switch (GET_MODE_CLASS (mode))
4917c376 7766 {
b2aef146 7767 case MODE_INT:
7768 case MODE_PARTIAL_INT:
7769 case MODE_FLOAT:
c4503c0a 7770 case MODE_DECIMAL_FLOAT:
9421ebb9 7771 case MODE_FRACT:
7772 case MODE_UFRACT:
7773 case MODE_ACCUM:
7774 case MODE_UACCUM:
b2aef146 7775 valid_mode = targetm.scalar_mode_supported_p (mode);
7776 break;
7777
7778 case MODE_COMPLEX_INT:
7779 case MODE_COMPLEX_FLOAT:
7780 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7781 break;
7782
7783 case MODE_VECTOR_INT:
7784 case MODE_VECTOR_FLOAT:
9421ebb9 7785 case MODE_VECTOR_FRACT:
7786 case MODE_VECTOR_UFRACT:
7787 case MODE_VECTOR_ACCUM:
7788 case MODE_VECTOR_UACCUM:
9b2d6d13 7789 warning (OPT_Wattributes, "specifying vector types with "
7790 "__attribute__ ((mode)) is deprecated");
7791 warning (OPT_Wattributes,
7792 "use __attribute__ ((vector_size)) instead");
b2aef146 7793 valid_mode = vector_mode_valid_p (mode);
7794 break;
4917c376 7795
b2aef146 7796 default:
7797 break;
7798 }
7799 if (!valid_mode)
7800 {
1e5fcbe2 7801 error ("unable to emulate %qs", p);
b2aef146 7802 return NULL_TREE;
7803 }
4917c376 7804
b2aef146 7805 if (POINTER_TYPE_P (type))
ead34f59 7806 {
6d5d708e 7807 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
3754d046 7808 tree (*fn)(tree, machine_mode, bool);
b2aef146 7809
6d5d708e 7810 if (!targetm.addr_space.valid_pointer_mode (mode, as))
ead34f59 7811 {
1e5fcbe2 7812 error ("invalid pointer mode %qs", p);
ead34f59 7813 return NULL_TREE;
7814 }
7815
a0c938f0 7816 if (TREE_CODE (type) == POINTER_TYPE)
b2aef146 7817 fn = build_pointer_type_for_mode;
805e22b2 7818 else
b2aef146 7819 fn = build_reference_type_for_mode;
7820 typefm = fn (TREE_TYPE (type), mode, false);
ead34f59 7821 }
b2aef146 7822 else
9421ebb9 7823 {
7824 /* For fixed-point modes, we need to test if the signness of type
7825 and the machine mode are consistent. */
7826 if (ALL_FIXED_POINT_MODE_P (mode)
7827 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7828 {
bf776685 7829 error ("signedness of type and machine mode %qs don%'t match", p);
9421ebb9 7830 return NULL_TREE;
7831 }
7832 /* For fixed-point modes, we need to pass saturating info. */
7833 typefm = lang_hooks.types.type_for_mode (mode,
7834 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7835 : TYPE_UNSIGNED (type));
7836 }
3a648ab9 7837
b2aef146 7838 if (typefm == NULL_TREE)
7839 {
743a6f47 7840 error ("no data type for mode %qs", p);
b2aef146 7841 return NULL_TREE;
7842 }
3a648ab9 7843 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7844 {
7845 /* For enumeral types, copy the precision from the integer
7846 type returned above. If not an INTEGER_TYPE, we can't use
7847 this mode for this type. */
7848 if (TREE_CODE (typefm) != INTEGER_TYPE)
7849 {
743a6f47 7850 error ("cannot use mode %qs for enumeral types", p);
3a648ab9 7851 return NULL_TREE;
7852 }
7853
10080eac 7854 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7855 {
7856 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7857 typefm = type;
7858 }
7859 else
7860 {
7861 /* We cannot build a type variant, as there's code that assumes
7862 that TYPE_MAIN_VARIANT has the same mode. This includes the
7863 debug generators. Instead, create a subrange type. This
7864 results in all of the enumeral values being emitted only once
7865 in the original, and the subtype gets them by reference. */
7866 if (TYPE_UNSIGNED (type))
7867 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7868 else
7869 typefm = make_signed_type (TYPE_PRECISION (typefm));
7870 TREE_TYPE (typefm) = type;
7871 }
3a648ab9 7872 }
4bf450a1 7873 else if (VECTOR_MODE_P (mode)
7874 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7875 : TREE_CODE (type) != TREE_CODE (typefm))
743a6f47 7876 {
7877 error ("mode %qs applied to inappropriate type", p);
7878 return NULL_TREE;
7879 }
7880
b2aef146 7881 *node = typefm;
f8e93a2e 7882 }
7883
7884 return NULL_TREE;
7885}
7886
7887/* Handle a "section" attribute; arguments as in
7888 struct attribute_spec.handler. */
7889
7890static tree
9a03a746 7891handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7892 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7893{
7894 tree decl = *node;
7895
9866562d 7896 if (!targetm_common.have_named_sections)
f8e93a2e 7897 {
9866562d 7898 error_at (DECL_SOURCE_LOCATION (*node),
7899 "section attributes are not supported for this target");
7900 goto fail;
7901 }
065efcb1 7902
9866562d 7903 user_defined_section_attribute = true;
f8e93a2e 7904
b443c459 7905 if (!VAR_OR_FUNCTION_DECL_P (decl))
9866562d 7906 {
7907 error ("section attribute not allowed for %q+D", *node);
7908 goto fail;
f8e93a2e 7909 }
9866562d 7910
7911 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
f8e93a2e 7912 {
9866562d 7913 error ("section attribute argument not a string constant");
7914 goto fail;
f8e93a2e 7915 }
7916
9866562d 7917 if (TREE_CODE (decl) == VAR_DECL
7918 && current_function_decl != NULL_TREE
7919 && !TREE_STATIC (decl))
7920 {
7921 error_at (DECL_SOURCE_LOCATION (decl),
7922 "section attribute cannot be specified for local variables");
7923 goto fail;
7924 }
7925
7926 /* The decl may have already been given a section attribute
7927 from a previous declaration. Ensure they match. */
7928 if (DECL_SECTION_NAME (decl) != NULL
7929 && strcmp (DECL_SECTION_NAME (decl),
7930 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7931 {
7932 error ("section of %q+D conflicts with previous declaration", *node);
7933 goto fail;
7934 }
7935
7936 if (TREE_CODE (decl) == VAR_DECL
7937 && !targetm.have_tls && targetm.emutls.tmpl_section
7938 && DECL_THREAD_LOCAL_P (decl))
7939 {
7940 error ("section of %q+D cannot be overridden", *node);
7941 goto fail;
7942 }
7943
7944 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
7945 return NULL_TREE;
7946
7947fail:
7948 *no_add_attrs = true;
f8e93a2e 7949 return NULL_TREE;
7950}
7951
83e25171 7952/* Check whether ALIGN is a valid user-specified alignment. If so,
7953 return its base-2 log; if not, output an error and return -1. If
7954 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7955 no error. */
7956int
7957check_user_alignment (const_tree align, bool allow_zero)
7958{
7959 int i;
7960
3e5a8b00 7961 if (error_operand_p (align))
7962 return -1;
5abaa10a 7963 if (TREE_CODE (align) != INTEGER_CST
7964 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
83e25171 7965 {
7966 error ("requested alignment is not an integer constant");
7967 return -1;
7968 }
7969 else if (allow_zero && integer_zerop (align))
7970 return -1;
1a087624 7971 else if (tree_int_cst_sgn (align) == -1
7972 || (i = tree_log2 (align)) == -1)
83e25171 7973 {
1a087624 7974 error ("requested alignment is not a positive power of 2");
83e25171 7975 return -1;
7976 }
7977 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7978 {
7979 error ("requested alignment is too large");
7980 return -1;
7981 }
7982 return i;
7983}
7984
ffcdbf9c 7985/*
7986 If in c++-11, check if the c++-11 alignment constraint with respect
7987 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7988 c++-11 mode, does nothing.
7989
7990 [dcl.align]2/ says:
7991
7992 [* if the constant expression evaluates to a fundamental alignment,
7993 the alignment requirement of the declared entity shall be the
7994 specified fundamental alignment.
7995
7996 * if the constant expression evaluates to an extended alignment
7997 and the implementation supports that alignment in the context
7998 of the declaration, the alignment of the declared entity shall
7999 be that alignment
8000
8001 * if the constant expression evaluates to an extended alignment
8002 and the implementation does not support that alignment in the
8003 context of the declaration, the program is ill-formed]. */
8004
8005static bool
8006check_cxx_fundamental_alignment_constraints (tree node,
8007 unsigned align_log,
8008 int flags)
8009{
8010 bool alignment_too_large_p = false;
8011 unsigned requested_alignment = 1U << align_log;
8012 unsigned max_align = 0;
8013
8014 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
8015 || (node == NULL_TREE || node == error_mark_node))
8016 return true;
8017
8018 if (cxx_fundamental_alignment_p (requested_alignment))
8019 return true;
8020
8021 if (DECL_P (node))
8022 {
8023 if (TREE_STATIC (node))
8024 {
8025 /* For file scope variables and static members, the target
8026 supports alignments that are at most
8027 MAX_OFILE_ALIGNMENT. */
8028 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
8029 alignment_too_large_p = true;
8030 }
8031 else
8032 {
8033#ifdef BIGGEST_FIELD_ALIGNMENT
8034#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
8035#else
8036#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
8037#endif
8038 /* For non-static members, the target supports either
8039 alignments that at most either BIGGEST_FIELD_ALIGNMENT
8040 if it is defined or BIGGEST_ALIGNMENT. */
8041 max_align = MAX_TARGET_FIELD_ALIGNMENT;
8042 if (TREE_CODE (node) == FIELD_DECL
8043 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
8044 alignment_too_large_p = true;
8045#undef MAX_TARGET_FIELD_ALIGNMENT
8046 /* For stack variables, the target supports at most
8047 MAX_STACK_ALIGNMENT. */
8048 else if (decl_function_context (node) != NULL
8049 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
8050 alignment_too_large_p = true;
8051 }
8052 }
8053 else if (TYPE_P (node))
8054 {
8055 /* Let's be liberal for types. */
8056 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
8057 alignment_too_large_p = true;
8058 }
8059
8060 if (alignment_too_large_p)
8061 pedwarn (input_location, OPT_Wattributes,
8062 "requested alignment %d is larger than %d",
8063 requested_alignment, max_align);
8064
8065 return !alignment_too_large_p;
8066}
8067
f8e93a2e 8068/* Handle a "aligned" attribute; arguments as in
8069 struct attribute_spec.handler. */
8070
8071static tree
9a03a746 8072handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
09347743 8073 int flags, bool *no_add_attrs)
f8e93a2e 8074{
8075 tree decl = NULL_TREE;
8076 tree *type = NULL;
8077 int is_type = 0;
caf62483 8078 tree align_expr;
f8e93a2e 8079 int i;
8080
caf62483 8081 if (args)
8082 {
8083 align_expr = TREE_VALUE (args);
3e5a8b00 8084 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
8085 && TREE_CODE (align_expr) != FUNCTION_DECL)
caf62483 8086 align_expr = default_conversion (align_expr);
8087 }
8088 else
8089 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
8090
f8e93a2e 8091 if (DECL_P (*node))
8092 {
8093 decl = *node;
8094 type = &TREE_TYPE (decl);
8095 is_type = TREE_CODE (*node) == TYPE_DECL;
8096 }
8097 else if (TYPE_P (*node))
8098 type = node, is_type = 1;
8099
ffcdbf9c 8100 if ((i = check_user_alignment (align_expr, false)) == -1
8101 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
83e25171 8102 *no_add_attrs = true;
f8e93a2e 8103 else if (is_type)
8104 {
2ec3af9c 8105 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8106 /* OK, modify the type in place. */;
f8e93a2e 8107 /* If we have a TYPE_DECL, then copy the type, so that we
8108 don't accidentally modify a builtin type. See pushdecl. */
2ec3af9c 8109 else if (decl && TREE_TYPE (decl) != error_mark_node
8110 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
f8e93a2e 8111 {
8112 tree tt = TREE_TYPE (decl);
e086912e 8113 *type = build_variant_type_copy (*type);
f8e93a2e 8114 DECL_ORIGINAL_TYPE (decl) = tt;
8115 TYPE_NAME (*type) = decl;
8116 TREE_USED (*type) = TREE_USED (decl);
8117 TREE_TYPE (decl) = *type;
8118 }
2ec3af9c 8119 else
e086912e 8120 *type = build_variant_type_copy (*type);
f8e93a2e 8121
7cfdc2f0 8122 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
f8e93a2e 8123 TYPE_USER_ALIGN (*type) = 1;
8124 }
097b5c8b 8125 else if (! VAR_OR_FUNCTION_DECL_P (decl)
f8e93a2e 8126 && TREE_CODE (decl) != FIELD_DECL)
8127 {
3cf8b391 8128 error ("alignment may not be specified for %q+D", decl);
f8e93a2e 8129 *no_add_attrs = true;
8130 }
ffcdbf9c 8131 else if (DECL_USER_ALIGN (decl)
8132 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
8133 /* C++-11 [dcl.align/4]:
8134
8135 When multiple alignment-specifiers are specified for an
8136 entity, the alignment requirement shall be set to the
8137 strictest specified alignment.
8138
8139 This formally comes from the c++11 specification but we are
8140 doing it for the GNU attribute syntax as well. */
8141 *no_add_attrs = true;
097b5c8b 8142 else if (TREE_CODE (decl) == FUNCTION_DECL
7cfdc2f0 8143 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
097b5c8b 8144 {
8145 if (DECL_USER_ALIGN (decl))
8146 error ("alignment for %q+D was previously specified as %d "
8147 "and may not be decreased", decl,
8148 DECL_ALIGN (decl) / BITS_PER_UNIT);
8149 else
8150 error ("alignment for %q+D must be at least %d", decl,
8151 DECL_ALIGN (decl) / BITS_PER_UNIT);
8152 *no_add_attrs = true;
8153 }
f8e93a2e 8154 else
8155 {
7cfdc2f0 8156 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
f8e93a2e 8157 DECL_USER_ALIGN (decl) = 1;
8158 }
8159
8160 return NULL_TREE;
8161}
8162
8163/* Handle a "weak" attribute; arguments as in
8164 struct attribute_spec.handler. */
8165
8166static tree
f948b309 8167handle_weak_attribute (tree *node, tree name,
9a03a746 8168 tree ARG_UNUSED (args),
8169 int ARG_UNUSED (flags),
8170 bool * ARG_UNUSED (no_add_attrs))
f8e93a2e 8171{
f948b309 8172 if (TREE_CODE (*node) == FUNCTION_DECL
059a60f3 8173 && DECL_DECLARED_INLINE_P (*node))
8174 {
0725e25c 8175 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
059a60f3 8176 *no_add_attrs = true;
8177 }
85c0a25c 8178 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8179 {
8180 error ("indirect function %q+D cannot be declared weak", *node);
8181 *no_add_attrs = true;
8182 return NULL_TREE;
8183 }
b443c459 8184 else if (VAR_OR_FUNCTION_DECL_P (*node))
c11b875d 8185 {
8186 struct symtab_node *n = symtab_node::get (*node);
8187 if (n && n->refuse_visibility_changes)
8188 error ("%+D declared weak after being used", *node);
8189 declare_weak (*node);
8190 }
f948b309 8191 else
8192 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8193
8194 return NULL_TREE;
8195}
8196
4641882f 8197/* Handle a "noplt" attribute; arguments as in
8198 struct attribute_spec.handler. */
8199
8200static tree
8201handle_noplt_attribute (tree *node, tree name,
8202 tree ARG_UNUSED (args),
8203 int ARG_UNUSED (flags),
8204 bool * ARG_UNUSED (no_add_attrs))
8205{
8206 if (TREE_CODE (*node) != FUNCTION_DECL)
8207 {
8208 warning (OPT_Wattributes,
8209 "%qE attribute is only applicable on functions", name);
8210 *no_add_attrs = true;
8211 return NULL_TREE;
8212 }
8213 return NULL_TREE;
8214}
8215
85c0a25c 8216/* Handle an "alias" or "ifunc" attribute; arguments as in
8217 struct attribute_spec.handler, except that IS_ALIAS tells us
8218 whether this is an alias as opposed to ifunc attribute. */
f8e93a2e 8219
8220static tree
85c0a25c 8221handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
8222 bool *no_add_attrs)
f8e93a2e 8223{
8224 tree decl = *node;
8225
85c0a25c 8226 if (TREE_CODE (decl) != FUNCTION_DECL
8227 && (!is_alias || TREE_CODE (decl) != VAR_DECL))
9e830260 8228 {
8229 warning (OPT_Wattributes, "%qE attribute ignored", name);
8230 *no_add_attrs = true;
8231 }
8232 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
48e1416a 8233 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 8234 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
8235 /* A static variable declaration is always a tentative definition,
8236 but the alias is a non-tentative definition which overrides. */
48e1416a 8237 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 8238 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
f8e93a2e 8239 {
85c0a25c 8240 error ("%q+D defined both normally and as %qE attribute", decl, name);
f8e93a2e 8241 *no_add_attrs = true;
85c0a25c 8242 return NULL_TREE;
f8e93a2e 8243 }
85c0a25c 8244 else if (!is_alias
8245 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
8246 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
8247 {
8248 error ("weak %q+D cannot be defined %qE", decl, name);
8249 *no_add_attrs = true;
8250 return NULL_TREE;
8251 }
8c42f0d9 8252
8253 /* Note that the very first time we process a nested declaration,
8254 decl_function_context will not be set. Indeed, *would* never
8255 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8256 we do below. After such frobbery, pushdecl would set the context.
8257 In any case, this is never what we want. */
8258 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
f8e93a2e 8259 {
8260 tree id;
8261
8262 id = TREE_VALUE (args);
8263 if (TREE_CODE (id) != STRING_CST)
8264 {
85c0a25c 8265 error ("attribute %qE argument not a string", name);
f8e93a2e 8266 *no_add_attrs = true;
8267 return NULL_TREE;
8268 }
8269 id = get_identifier (TREE_STRING_POINTER (id));
8270 /* This counts as a use of the object pointed to. */
8271 TREE_USED (id) = 1;
8272
8273 if (TREE_CODE (decl) == FUNCTION_DECL)
8274 DECL_INITIAL (decl) = error_mark_node;
8275 else
f2526cce 8276 TREE_STATIC (decl) = 1;
85c0a25c 8277
8278 if (!is_alias)
8279 /* ifuncs are also aliases, so set that attribute too. */
8280 DECL_ATTRIBUTES (decl)
8281 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
f8e93a2e 8282 }
8283 else
8284 {
9b2d6d13 8285 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8286 *no_add_attrs = true;
8287 }
8288
8e857c41 8289 if (decl_in_symtab_p (*node))
8290 {
8291 struct symtab_node *n = symtab_node::get (decl);
8292 if (n && n->refuse_visibility_changes)
8293 {
8294 if (is_alias)
8295 error ("%+D declared alias after being used", decl);
8296 else
8297 error ("%+D declared ifunc after being used", decl);
8298 }
8299 }
8300
8301
f8e93a2e 8302 return NULL_TREE;
8303}
8304
85c0a25c 8305/* Handle an "alias" or "ifunc" attribute; arguments as in
8306 struct attribute_spec.handler. */
8307
8308static tree
8309handle_ifunc_attribute (tree *node, tree name, tree args,
8310 int ARG_UNUSED (flags), bool *no_add_attrs)
8311{
8312 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8313}
8314
8315/* Handle an "alias" or "ifunc" attribute; arguments as in
8316 struct attribute_spec.handler. */
8317
8318static tree
8319handle_alias_attribute (tree *node, tree name, tree args,
8320 int ARG_UNUSED (flags), bool *no_add_attrs)
8321{
8322 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8323}
8324
f4a30bd7 8325/* Handle a "weakref" attribute; arguments as in struct
8326 attribute_spec.handler. */
8327
8328static tree
8329handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8330 int flags, bool *no_add_attrs)
8331{
8332 tree attr = NULL_TREE;
8333
83852912 8334 /* We must ignore the attribute when it is associated with
8335 local-scoped decls, since attribute alias is ignored and many
8336 such symbols do not even have a DECL_WEAK field. */
64a7bd81 8337 if (decl_function_context (*node)
8338 || current_function_decl
b443c459 8339 || !VAR_OR_FUNCTION_DECL_P (*node))
83852912 8340 {
8341 warning (OPT_Wattributes, "%qE attribute ignored", name);
8342 *no_add_attrs = true;
8343 return NULL_TREE;
8344 }
8345
85c0a25c 8346 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8347 {
8348 error ("indirect function %q+D cannot be declared weakref", *node);
8349 *no_add_attrs = true;
8350 return NULL_TREE;
8351 }
8352
f4a30bd7 8353 /* The idea here is that `weakref("name")' mutates into `weakref,
8354 alias("name")', and weakref without arguments, in turn,
8355 implicitly adds weak. */
8356
8357 if (args)
8358 {
8359 attr = tree_cons (get_identifier ("alias"), args, attr);
8360 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8361
8362 *no_add_attrs = true;
0a3ecdc1 8363
8364 decl_attributes (node, attr, flags);
f4a30bd7 8365 }
8366 else
8367 {
8368 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
712d2297 8369 error_at (DECL_SOURCE_LOCATION (*node),
8370 "weakref attribute must appear before alias attribute");
f4a30bd7 8371
0a3ecdc1 8372 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8373 and that isn't supported; and because it wants to add it to
8374 the list of weak decls, which isn't helpful. */
8375 DECL_WEAK (*node) = 1;
f4a30bd7 8376 }
8377
8e857c41 8378 if (decl_in_symtab_p (*node))
8379 {
8380 struct symtab_node *n = symtab_node::get (*node);
8381 if (n && n->refuse_visibility_changes)
8382 error ("%+D declared weakref after being used", *node);
8383 }
8384
f4a30bd7 8385 return NULL_TREE;
8386}
8387
f8e93a2e 8388/* Handle an "visibility" attribute; arguments as in
8389 struct attribute_spec.handler. */
8390
8391static tree
1cae46be 8392handle_visibility_attribute (tree *node, tree name, tree args,
9a03a746 8393 int ARG_UNUSED (flags),
4a2849cb 8394 bool *ARG_UNUSED (no_add_attrs))
f8e93a2e 8395{
8396 tree decl = *node;
9c40570a 8397 tree id = TREE_VALUE (args);
4a2849cb 8398 enum symbol_visibility vis;
f8e93a2e 8399
b212f378 8400 if (TYPE_P (*node))
8401 {
4a2849cb 8402 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8403 /* OK */;
8404 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
8405 {
8406 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8407 name);
8408 return NULL_TREE;
8409 }
8410 else if (TYPE_FIELDS (*node))
8411 {
8412 error ("%qE attribute ignored because %qT is already defined",
8413 name, *node);
8414 return NULL_TREE;
8415 }
b212f378 8416 }
84166705 8417 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
f8e93a2e 8418 {
9b2d6d13 8419 warning (OPT_Wattributes, "%qE attribute ignored", name);
9c40570a 8420 return NULL_TREE;
f8e93a2e 8421 }
f8e93a2e 8422
9c40570a 8423 if (TREE_CODE (id) != STRING_CST)
8424 {
07e3a3d2 8425 error ("visibility argument not a string");
9c40570a 8426 return NULL_TREE;
f8e93a2e 8427 }
b27ac6b5 8428
b212f378 8429 /* If this is a type, set the visibility on the type decl. */
8430 if (TYPE_P (decl))
8431 {
8432 decl = TYPE_NAME (decl);
84166705 8433 if (!decl)
a0c938f0 8434 return NULL_TREE;
e147aab3 8435 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8436 {
9b2d6d13 8437 warning (OPT_Wattributes, "%qE attribute ignored on types",
e147aab3 8438 name);
8439 return NULL_TREE;
8440 }
b212f378 8441 }
f8e93a2e 8442
9c40570a 8443 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
4a2849cb 8444 vis = VISIBILITY_DEFAULT;
9c40570a 8445 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
4a2849cb 8446 vis = VISIBILITY_INTERNAL;
9c40570a 8447 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
4a2849cb 8448 vis = VISIBILITY_HIDDEN;
9c40570a 8449 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
4a2849cb 8450 vis = VISIBILITY_PROTECTED;
9c40570a 8451 else
4a2849cb 8452 {
8453 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8454 vis = VISIBILITY_DEFAULT;
8455 }
8456
8457 if (DECL_VISIBILITY_SPECIFIED (decl)
098a01e7 8458 && vis != DECL_VISIBILITY (decl))
8459 {
8460 tree attributes = (TYPE_P (*node)
8461 ? TYPE_ATTRIBUTES (*node)
8462 : DECL_ATTRIBUTES (decl));
8463 if (lookup_attribute ("visibility", attributes))
8464 error ("%qD redeclared with different visibility", decl);
8465 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8466 && lookup_attribute ("dllimport", attributes))
8467 error ("%qD was declared %qs which implies default visibility",
8468 decl, "dllimport");
8469 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8470 && lookup_attribute ("dllexport", attributes))
8471 error ("%qD was declared %qs which implies default visibility",
8472 decl, "dllexport");
8473 }
4a2849cb 8474
8475 DECL_VISIBILITY (decl) = vis;
b212f378 8476 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8477
4a2849cb 8478 /* Go ahead and attach the attribute to the node as well. This is needed
8479 so we can determine whether we have VISIBILITY_DEFAULT because the
8480 visibility was not specified, or because it was explicitly overridden
8481 from the containing scope. */
9c40570a 8482
f8e93a2e 8483 return NULL_TREE;
8484}
8485
3aa0c315 8486/* Determine the ELF symbol visibility for DECL, which is either a
8487 variable or a function. It is an error to use this function if a
8488 definition of DECL is not available in this translation unit.
8489 Returns true if the final visibility has been determined by this
8490 function; false if the caller is free to make additional
8491 modifications. */
8492
8493bool
8494c_determine_visibility (tree decl)
8495{
b443c459 8496 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
3aa0c315 8497
8498 /* If the user explicitly specified the visibility with an
8499 attribute, honor that. DECL_VISIBILITY will have been set during
920f5a70 8500 the processing of the attribute. We check for an explicit
8501 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8502 to distinguish the use of an attribute from the use of a "#pragma
8503 GCC visibility push(...)"; in the latter case we still want other
8504 considerations to be able to overrule the #pragma. */
8505 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8506 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8507 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8508 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
3aa0c315 8509 return true;
8510
4a2849cb 8511 /* Set default visibility to whatever the user supplied with
8512 visibility_specified depending on #pragma GCC visibility. */
8513 if (!DECL_VISIBILITY_SPECIFIED (decl))
8514 {
2d9d8740 8515 if (visibility_options.inpragma
8516 || DECL_VISIBILITY (decl) != default_visibility)
8517 {
8518 DECL_VISIBILITY (decl) = default_visibility;
8519 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8520 /* If visibility changed and DECL already has DECL_RTL, ensure
8521 symbol flags are updated. */
8522 if (((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
8523 || TREE_CODE (decl) == FUNCTION_DECL)
8524 && DECL_RTL_SET_P (decl))
8525 make_decl_rtl (decl);
8526 }
4a2849cb 8527 }
3aa0c315 8528 return false;
8529}
8530
24dfead4 8531/* Handle an "tls_model" attribute; arguments as in
8532 struct attribute_spec.handler. */
8533
8534static tree
1cae46be 8535handle_tls_model_attribute (tree *node, tree name, tree args,
9a03a746 8536 int ARG_UNUSED (flags), bool *no_add_attrs)
24dfead4 8537{
1b53eb20 8538 tree id;
24dfead4 8539 tree decl = *node;
1b53eb20 8540 enum tls_model kind;
24dfead4 8541
1b53eb20 8542 *no_add_attrs = true;
8543
c34f8a78 8544 if (TREE_CODE (decl) != VAR_DECL || !DECL_THREAD_LOCAL_P (decl))
24dfead4 8545 {
9b2d6d13 8546 warning (OPT_Wattributes, "%qE attribute ignored", name);
1b53eb20 8547 return NULL_TREE;
24dfead4 8548 }
24dfead4 8549
1b53eb20 8550 kind = DECL_TLS_MODEL (decl);
8551 id = TREE_VALUE (args);
8552 if (TREE_CODE (id) != STRING_CST)
8553 {
8554 error ("tls_model argument not a string");
8555 return NULL_TREE;
24dfead4 8556 }
8557
1b53eb20 8558 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8559 kind = TLS_MODEL_LOCAL_EXEC;
8560 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8561 kind = TLS_MODEL_INITIAL_EXEC;
8562 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8563 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8564 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8565 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8566 else
8567 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8568
5e68df57 8569 set_decl_tls_model (decl, kind);
24dfead4 8570 return NULL_TREE;
8571}
8572
f8e93a2e 8573/* Handle a "no_instrument_function" attribute; arguments as in
8574 struct attribute_spec.handler. */
8575
8576static tree
1cae46be 8577handle_no_instrument_function_attribute (tree *node, tree name,
9a03a746 8578 tree ARG_UNUSED (args),
8579 int ARG_UNUSED (flags),
09347743 8580 bool *no_add_attrs)
f8e93a2e 8581{
8582 tree decl = *node;
8583
8584 if (TREE_CODE (decl) != FUNCTION_DECL)
8585 {
712d2297 8586 error_at (DECL_SOURCE_LOCATION (decl),
8587 "%qE attribute applies only to functions", name);
f8e93a2e 8588 *no_add_attrs = true;
8589 }
f8e93a2e 8590 else
8591 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8592
8593 return NULL_TREE;
8594}
8595
8596/* Handle a "malloc" attribute; arguments as in
8597 struct attribute_spec.handler. */
8598
8599static tree
9a03a746 8600handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8601 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8602{
a5147fca 8603 if (TREE_CODE (*node) == FUNCTION_DECL
8604 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
f8e93a2e 8605 DECL_IS_MALLOC (*node) = 1;
f8e93a2e 8606 else
8607 {
9b2d6d13 8608 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8609 *no_add_attrs = true;
8610 }
8611
8612 return NULL_TREE;
8613}
8614
4a29c97c 8615/* Handle a "alloc_size" attribute; arguments as in
8616 struct attribute_spec.handler. */
8617
8618static tree
8619handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8620 int ARG_UNUSED (flags), bool *no_add_attrs)
8621{
2802826e 8622 unsigned arg_count = type_num_arguments (*node);
4a29c97c 8623 for (; args; args = TREE_CHAIN (args))
8624 {
8625 tree position = TREE_VALUE (args);
caf62483 8626 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8627 && TREE_CODE (position) != FUNCTION_DECL)
8628 position = default_conversion (position);
4a29c97c 8629
237e78b1 8630 if (!tree_fits_uhwi_p (position)
8631 || !arg_count
8632 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
4a29c97c 8633 {
48e1416a 8634 warning (OPT_Wattributes,
4a29c97c 8635 "alloc_size parameter outside range");
8636 *no_add_attrs = true;
8637 return NULL_TREE;
8638 }
8639 }
8640 return NULL_TREE;
8641}
8642
237e78b1 8643/* Handle a "alloc_align" attribute; arguments as in
8644 struct attribute_spec.handler. */
8645
8646static tree
8647handle_alloc_align_attribute (tree *node, tree, tree args, int,
8648 bool *no_add_attrs)
8649{
8650 unsigned arg_count = type_num_arguments (*node);
8651 tree position = TREE_VALUE (args);
8652 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8653 position = default_conversion (position);
8654
8655 if (!tree_fits_uhwi_p (position)
8656 || !arg_count
8657 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8658 {
8659 warning (OPT_Wattributes,
8660 "alloc_align parameter outside range");
8661 *no_add_attrs = true;
8662 return NULL_TREE;
8663 }
8664 return NULL_TREE;
8665}
8666
8667/* Handle a "assume_aligned" attribute; arguments as in
8668 struct attribute_spec.handler. */
8669
8670static tree
8671handle_assume_aligned_attribute (tree *, tree, tree args, int,
8672 bool *no_add_attrs)
8673{
8674 for (; args; args = TREE_CHAIN (args))
8675 {
8676 tree position = TREE_VALUE (args);
8677 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8678 && TREE_CODE (position) != FUNCTION_DECL)
8679 position = default_conversion (position);
8680
8681 if (TREE_CODE (position) != INTEGER_CST)
8682 {
8683 warning (OPT_Wattributes,
8684 "assume_aligned parameter not integer constant");
8685 *no_add_attrs = true;
8686 return NULL_TREE;
8687 }
8688 }
8689 return NULL_TREE;
8690}
8691
8ce86007 8692/* Handle a "fn spec" attribute; arguments as in
8693 struct attribute_spec.handler. */
8694
8695static tree
8696handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8697 tree args, int ARG_UNUSED (flags),
8698 bool *no_add_attrs ATTRIBUTE_UNUSED)
8699{
8700 gcc_assert (args
8701 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8702 && !TREE_CHAIN (args));
8703 return NULL_TREE;
8704}
8705
058a1b7a 8706/* Handle a "bnd_variable_size" attribute; arguments as in
8707 struct attribute_spec.handler. */
8708
8709static tree
8710handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8711 int ARG_UNUSED (flags), bool *no_add_attrs)
8712{
8713 if (TREE_CODE (*node) != FIELD_DECL)
8714 {
8715 warning (OPT_Wattributes, "%qE attribute ignored", name);
8716 *no_add_attrs = true;
8717 }
8718
8719 return NULL_TREE;
8720}
8721
8722/* Handle a "bnd_legacy" attribute; arguments as in
8723 struct attribute_spec.handler. */
8724
8725static tree
8726handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8727 int ARG_UNUSED (flags), bool *no_add_attrs)
8728{
8729 if (TREE_CODE (*node) != FUNCTION_DECL)
8730 {
8731 warning (OPT_Wattributes, "%qE attribute ignored", name);
8732 *no_add_attrs = true;
8733 }
8734
8735 return NULL_TREE;
8736}
8737
8738/* Handle a "bnd_instrument" attribute; arguments as in
8739 struct attribute_spec.handler. */
8740
8741static tree
8742handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8743 int ARG_UNUSED (flags), bool *no_add_attrs)
8744{
8745 if (TREE_CODE (*node) != FUNCTION_DECL)
8746 {
8747 warning (OPT_Wattributes, "%qE attribute ignored", name);
8748 *no_add_attrs = true;
8749 }
8750
8751 return NULL_TREE;
8752}
8753
a96c3cc1 8754/* Handle a "warn_unused" attribute; arguments as in
8755 struct attribute_spec.handler. */
8756
8757static tree
8758handle_warn_unused_attribute (tree *node, tree name,
8759 tree args ATTRIBUTE_UNUSED,
8760 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8761{
8762 if (TYPE_P (*node))
8763 /* Do nothing else, just set the attribute. We'll get at
8764 it later with lookup_attribute. */
8765 ;
8766 else
8767 {
8768 warning (OPT_Wattributes, "%qE attribute ignored", name);
8769 *no_add_attrs = true;
8770 }
8771
8772 return NULL_TREE;
8773}
8774
bc7bff74 8775/* Handle an "omp declare simd" attribute; arguments as in
8776 struct attribute_spec.handler. */
8777
8778static tree
8779handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8780{
8781 return NULL_TREE;
8782}
8783
8784/* Handle an "omp declare target" attribute; arguments as in
8785 struct attribute_spec.handler. */
8786
8787static tree
8788handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8789{
8790 return NULL_TREE;
8791}
8792
26d1c5ff 8793/* Handle a "returns_twice" attribute; arguments as in
8794 struct attribute_spec.handler. */
8795
8796static tree
8797handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8798 int ARG_UNUSED (flags), bool *no_add_attrs)
8799{
8800 if (TREE_CODE (*node) == FUNCTION_DECL)
8801 DECL_IS_RETURNS_TWICE (*node) = 1;
8802 else
8803 {
9b2d6d13 8804 warning (OPT_Wattributes, "%qE attribute ignored", name);
26d1c5ff 8805 *no_add_attrs = true;
8806 }
8807
8808 return NULL_TREE;
8809}
8810
f8e93a2e 8811/* Handle a "no_limit_stack" attribute; arguments as in
8812 struct attribute_spec.handler. */
8813
8814static tree
1cae46be 8815handle_no_limit_stack_attribute (tree *node, tree name,
9a03a746 8816 tree ARG_UNUSED (args),
8817 int ARG_UNUSED (flags),
09347743 8818 bool *no_add_attrs)
f8e93a2e 8819{
8820 tree decl = *node;
8821
8822 if (TREE_CODE (decl) != FUNCTION_DECL)
8823 {
712d2297 8824 error_at (DECL_SOURCE_LOCATION (decl),
8825 "%qE attribute applies only to functions", name);
f8e93a2e 8826 *no_add_attrs = true;
8827 }
8828 else if (DECL_INITIAL (decl))
8829 {
712d2297 8830 error_at (DECL_SOURCE_LOCATION (decl),
8831 "can%'t set %qE attribute after definition", name);
f8e93a2e 8832 *no_add_attrs = true;
8833 }
8834 else
8835 DECL_NO_LIMIT_STACK (decl) = 1;
8836
8837 return NULL_TREE;
8838}
8839
8840/* Handle a "pure" attribute; arguments as in
8841 struct attribute_spec.handler. */
8842
8843static tree
9a03a746 8844handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8845 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8846{
8847 if (TREE_CODE (*node) == FUNCTION_DECL)
9c2a0c05 8848 DECL_PURE_P (*node) = 1;
f8e93a2e 8849 /* ??? TODO: Support types. */
8850 else
8851 {
9b2d6d13 8852 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8853 *no_add_attrs = true;
8854 }
8855
8856 return NULL_TREE;
8857}
8858
4c0315d0 8859/* Digest an attribute list destined for a transactional memory statement.
8860 ALLOWED is the set of attributes that are allowed for this statement;
8861 return the attribute we parsed. Multiple attributes are never allowed. */
8862
8863int
8864parse_tm_stmt_attr (tree attrs, int allowed)
8865{
8866 tree a_seen = NULL;
8867 int m_seen = 0;
8868
8869 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8870 {
8871 tree a = TREE_PURPOSE (attrs);
8872 int m = 0;
8873
8874 if (is_attribute_p ("outer", a))
8875 m = TM_STMT_ATTR_OUTER;
8876
8877 if ((m & allowed) == 0)
8878 {
8879 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8880 continue;
8881 }
8882
8883 if (m_seen == 0)
8884 {
8885 a_seen = a;
8886 m_seen = m;
8887 }
8888 else if (m_seen == m)
8889 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8890 else
8891 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8892 }
8893
8894 return m_seen;
8895}
8896
8897/* Transform a TM attribute name into a maskable integer and back.
8898 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8899 to how the lack of an attribute is treated. */
8900
8901int
8902tm_attr_to_mask (tree attr)
8903{
8904 if (attr == NULL)
8905 return 0;
8906 if (is_attribute_p ("transaction_safe", attr))
8907 return TM_ATTR_SAFE;
8908 if (is_attribute_p ("transaction_callable", attr))
8909 return TM_ATTR_CALLABLE;
8910 if (is_attribute_p ("transaction_pure", attr))
8911 return TM_ATTR_PURE;
8912 if (is_attribute_p ("transaction_unsafe", attr))
8913 return TM_ATTR_IRREVOCABLE;
8914 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8915 return TM_ATTR_MAY_CANCEL_OUTER;
8916 return 0;
8917}
8918
8919tree
8920tm_mask_to_attr (int mask)
8921{
8922 const char *str;
8923 switch (mask)
8924 {
8925 case TM_ATTR_SAFE:
8926 str = "transaction_safe";
8927 break;
8928 case TM_ATTR_CALLABLE:
8929 str = "transaction_callable";
8930 break;
8931 case TM_ATTR_PURE:
8932 str = "transaction_pure";
8933 break;
8934 case TM_ATTR_IRREVOCABLE:
8935 str = "transaction_unsafe";
8936 break;
8937 case TM_ATTR_MAY_CANCEL_OUTER:
8938 str = "transaction_may_cancel_outer";
8939 break;
8940 default:
8941 gcc_unreachable ();
8942 }
8943 return get_identifier (str);
8944}
8945
8946/* Return the first TM attribute seen in LIST. */
8947
8948tree
8949find_tm_attribute (tree list)
8950{
8951 for (; list ; list = TREE_CHAIN (list))
8952 {
8953 tree name = TREE_PURPOSE (list);
8954 if (tm_attr_to_mask (name) != 0)
8955 return name;
8956 }
8957 return NULL_TREE;
8958}
8959
8960/* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8961 Here we accept only function types, and verify that none of the other
8962 function TM attributes are also applied. */
8963/* ??? We need to accept class types for C++, but not C. This greatly
8964 complicates this function, since we can no longer rely on the extra
8965 processing given by function_type_required. */
8966
8967static tree
8968handle_tm_attribute (tree *node, tree name, tree args,
8969 int flags, bool *no_add_attrs)
8970{
8971 /* Only one path adds the attribute; others don't. */
8972 *no_add_attrs = true;
8973
8974 switch (TREE_CODE (*node))
8975 {
8976 case RECORD_TYPE:
8977 case UNION_TYPE:
8978 /* Only tm_callable and tm_safe apply to classes. */
8979 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8980 goto ignored;
8981 /* FALLTHRU */
8982
8983 case FUNCTION_TYPE:
8984 case METHOD_TYPE:
8985 {
8986 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8987 if (old_name == name)
8988 ;
8989 else if (old_name != NULL_TREE)
8990 error ("type was previously declared %qE", old_name);
8991 else
8992 *no_add_attrs = false;
8993 }
8994 break;
8995
8996 case POINTER_TYPE:
8997 {
8998 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8999 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
9000 {
9001 tree fn_tmp = TREE_TYPE (*node);
9002 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
9003 *node = build_pointer_type (fn_tmp);
9004 break;
9005 }
9006 }
9007 /* FALLTHRU */
9008
9009 default:
9010 /* If a function is next, pass it on to be tried next. */
9011 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
9012 return tree_cons (name, args, NULL);
9013
9014 ignored:
9015 warning (OPT_Wattributes, "%qE attribute ignored", name);
9016 break;
9017 }
9018
9019 return NULL_TREE;
9020}
9021
9022/* Handle the TM_WRAP attribute; arguments as in
9023 struct attribute_spec.handler. */
9024
9025static tree
9026handle_tm_wrap_attribute (tree *node, tree name, tree args,
9027 int ARG_UNUSED (flags), bool *no_add_attrs)
9028{
9029 tree decl = *node;
9030
9031 /* We don't need the attribute even on success, since we
9032 record the entry in an external table. */
9033 *no_add_attrs = true;
9034
9035 if (TREE_CODE (decl) != FUNCTION_DECL)
9036 warning (OPT_Wattributes, "%qE attribute ignored", name);
9037 else
9038 {
9039 tree wrap_decl = TREE_VALUE (args);
3e5a8b00 9040 if (error_operand_p (wrap_decl))
9041 ;
9042 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
b443c459 9043 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
4c0315d0 9044 error ("%qE argument not an identifier", name);
9045 else
9046 {
9047 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
9048 wrap_decl = lookup_name (wrap_decl);
9049 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
9050 {
9051 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
9052 TREE_TYPE (wrap_decl)))
9053 record_tm_replacement (wrap_decl, decl);
9054 else
9055 error ("%qD is not compatible with %qD", wrap_decl, decl);
9056 }
9057 else
cdf34fca 9058 error ("%qE argument is not a function", name);
4c0315d0 9059 }
9060 }
9061
9062 return NULL_TREE;
9063}
9064
9065/* Ignore the given attribute. Used when this attribute may be usefully
9066 overridden by the target, but is not used generically. */
9067
9068static tree
9069ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
9070 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9071 bool *no_add_attrs)
9072{
9073 *no_add_attrs = true;
9074 return NULL_TREE;
9075}
9076
fc09b200 9077/* Handle a "no vops" attribute; arguments as in
9078 struct attribute_spec.handler. */
9079
9080static tree
9081handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
9082 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9083 bool *ARG_UNUSED (no_add_attrs))
9084{
9085 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
9086 DECL_IS_NOVOPS (*node) = 1;
9087 return NULL_TREE;
9088}
9089
f8e93a2e 9090/* Handle a "deprecated" attribute; arguments as in
9091 struct attribute_spec.handler. */
1cae46be 9092
f8e93a2e 9093static tree
1cae46be 9094handle_deprecated_attribute (tree *node, tree name,
45c4e798 9095 tree args, int flags,
09347743 9096 bool *no_add_attrs)
f8e93a2e 9097{
9098 tree type = NULL_TREE;
9099 int warn = 0;
782858b8 9100 tree what = NULL_TREE;
1cae46be 9101
45c4e798 9102 if (!args)
9103 *no_add_attrs = true;
9104 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
9105 {
9106 error ("deprecated message is not a string");
9107 *no_add_attrs = true;
9108 }
9109
f8e93a2e 9110 if (DECL_P (*node))
9111 {
9112 tree decl = *node;
9113 type = TREE_TYPE (decl);
1cae46be 9114
f8e93a2e 9115 if (TREE_CODE (decl) == TYPE_DECL
9116 || TREE_CODE (decl) == PARM_DECL
b443c459 9117 || VAR_OR_FUNCTION_DECL_P (decl)
40c8d1dd 9118 || TREE_CODE (decl) == FIELD_DECL
5a4c69dd 9119 || TREE_CODE (decl) == CONST_DECL
40c8d1dd 9120 || objc_method_decl (TREE_CODE (decl)))
f8e93a2e 9121 TREE_DEPRECATED (decl) = 1;
9122 else
9123 warn = 1;
9124 }
9125 else if (TYPE_P (*node))
9126 {
9127 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 9128 *node = build_variant_type_copy (*node);
f8e93a2e 9129 TREE_DEPRECATED (*node) = 1;
9130 type = *node;
9131 }
9132 else
9133 warn = 1;
1cae46be 9134
f8e93a2e 9135 if (warn)
9136 {
9137 *no_add_attrs = true;
9138 if (type && TYPE_NAME (type))
9139 {
9140 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
782858b8 9141 what = TYPE_NAME (*node);
f8e93a2e 9142 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9143 && DECL_NAME (TYPE_NAME (type)))
782858b8 9144 what = DECL_NAME (TYPE_NAME (type));
f8e93a2e 9145 }
9146 if (what)
9b2d6d13 9147 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
f8e93a2e 9148 else
9b2d6d13 9149 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 9150 }
9151
9152 return NULL_TREE;
9153}
9154
f8e93a2e 9155/* Handle a "vector_size" attribute; arguments as in
9156 struct attribute_spec.handler. */
9157
9158static tree
1cae46be 9159handle_vector_size_attribute (tree *node, tree name, tree args,
9a03a746 9160 int ARG_UNUSED (flags),
09347743 9161 bool *no_add_attrs)
f8e93a2e 9162{
9163 unsigned HOST_WIDE_INT vecsize, nunits;
3754d046 9164 machine_mode orig_mode;
4917c376 9165 tree type = *node, new_type, size;
f8e93a2e 9166
9167 *no_add_attrs = true;
9168
4917c376 9169 size = TREE_VALUE (args);
3e5a8b00 9170 if (size && TREE_CODE (size) != IDENTIFIER_NODE
9171 && TREE_CODE (size) != FUNCTION_DECL)
caf62483 9172 size = default_conversion (size);
4917c376 9173
e913b5cd 9174 if (!tree_fits_uhwi_p (size))
f8e93a2e 9175 {
9b2d6d13 9176 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 9177 return NULL_TREE;
9178 }
9179
9180 /* Get the vector size (in bytes). */
e913b5cd 9181 vecsize = tree_to_uhwi (size);
f8e93a2e 9182
9183 /* We need to provide for vector pointers, vector arrays, and
9184 functions returning vectors. For example:
9185
9186 __attribute__((vector_size(16))) short *foo;
9187
9188 In this case, the mode is SI, but the type being modified is
9189 HI, so we need to look further. */
9190
9191 while (POINTER_TYPE_P (type)
9192 || TREE_CODE (type) == FUNCTION_TYPE
5bfb0742 9193 || TREE_CODE (type) == METHOD_TYPE
2cb0e5d9 9194 || TREE_CODE (type) == ARRAY_TYPE
9195 || TREE_CODE (type) == OFFSET_TYPE)
f8e93a2e 9196 type = TREE_TYPE (type);
9197
9198 /* Get the mode of the type being modified. */
9199 orig_mode = TYPE_MODE (type);
9200
2cb0e5d9 9201 if ((!INTEGRAL_TYPE_P (type)
9202 && !SCALAR_FLOAT_TYPE_P (type)
9203 && !FIXED_POINT_TYPE_P (type))
cee7491d 9204 || (!SCALAR_FLOAT_MODE_P (orig_mode)
9421ebb9 9205 && GET_MODE_CLASS (orig_mode) != MODE_INT
9206 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
e913b5cd 9207 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7ec31215 9208 || TREE_CODE (type) == BOOLEAN_TYPE)
f8e93a2e 9209 {
782858b8 9210 error ("invalid vector type for attribute %qE", name);
f8e93a2e 9211 return NULL_TREE;
9212 }
9213
e913b5cd 9214 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
39cc3e6d 9215 {
9216 error ("vector size not an integral multiple of component size");
9217 return NULL;
9218 }
9219
9220 if (vecsize == 0)
9221 {
9222 error ("zero vector size");
9223 return NULL;
9224 }
9225
f8e93a2e 9226 /* Calculate how many units fit in the vector. */
e913b5cd 9227 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
83e2a11b 9228 if (nunits & (nunits - 1))
f8e93a2e 9229 {
83e2a11b 9230 error ("number of components of the vector not a power of two");
f8e93a2e 9231 return NULL_TREE;
9232 }
9233
83e2a11b 9234 new_type = build_vector_type (type, nunits);
f8e93a2e 9235
9236 /* Build back pointers if needed. */
d991e6e8 9237 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
f8e93a2e 9238
9239 return NULL_TREE;
9240}
9241
dbf6c367 9242/* Handle the "nonnull" attribute. */
9243static tree
9a03a746 9244handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9245 tree args, int ARG_UNUSED (flags),
09347743 9246 bool *no_add_attrs)
dbf6c367 9247{
9248 tree type = *node;
9249 unsigned HOST_WIDE_INT attr_arg_num;
9250
9251 /* If no arguments are specified, all pointer arguments should be
d716ce75 9252 non-null. Verify a full prototype is given so that the arguments
dbf6c367 9253 will have the correct types when we actually check them later. */
84166705 9254 if (!args)
dbf6c367 9255 {
a36cf284 9256 if (!prototype_p (type))
dbf6c367 9257 {
9258 error ("nonnull attribute without arguments on a non-prototype");
4ee9c684 9259 *no_add_attrs = true;
dbf6c367 9260 }
9261 return NULL_TREE;
9262 }
9263
9264 /* Argument list specified. Verify that each argument number references
9265 a pointer argument. */
caf62483 9266 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
dbf6c367 9267 {
4ee9c684 9268 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
dbf6c367 9269
caf62483 9270 tree arg = TREE_VALUE (args);
9271 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9272 && TREE_CODE (arg) != FUNCTION_DECL)
9273 arg = default_conversion (arg);
9274
9275 if (!get_nonnull_operand (arg, &arg_num))
dbf6c367 9276 {
07e3a3d2 9277 error ("nonnull argument has invalid operand number (argument %lu)",
dbf6c367 9278 (unsigned long) attr_arg_num);
9279 *no_add_attrs = true;
9280 return NULL_TREE;
9281 }
9282
d0af78c5 9283 if (prototype_p (type))
dbf6c367 9284 {
d0af78c5 9285 function_args_iterator iter;
9286 tree argument;
9287
9288 function_args_iter_init (&iter, type);
9289 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
dbf6c367 9290 {
d0af78c5 9291 argument = function_args_iter_cond (&iter);
9292 if (argument == NULL_TREE || ck_num == arg_num)
dbf6c367 9293 break;
dbf6c367 9294 }
9295
84166705 9296 if (!argument
d0af78c5 9297 || TREE_CODE (argument) == VOID_TYPE)
dbf6c367 9298 {
07e3a3d2 9299 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
dbf6c367 9300 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9301 *no_add_attrs = true;
9302 return NULL_TREE;
9303 }
9304
d0af78c5 9305 if (TREE_CODE (argument) != POINTER_TYPE)
dbf6c367 9306 {
07e3a3d2 9307 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
dbf6c367 9308 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9309 *no_add_attrs = true;
9310 return NULL_TREE;
9311 }
9312 }
9313 }
9314
9315 return NULL_TREE;
9316}
9317
9318/* Check the argument list of a function call for null in argument slots
d01f58f9 9319 that are marked as requiring a non-null pointer argument. The NARGS
9320 arguments are passed in the array ARGARRAY.
9321*/
dbf6c367 9322
9323static void
d01f58f9 9324check_function_nonnull (tree attrs, int nargs, tree *argarray)
dbf6c367 9325{
9ca77b08 9326 tree a;
d01f58f9 9327 int i;
dbf6c367 9328
9ca77b08 9329 attrs = lookup_attribute ("nonnull", attrs);
9330 if (attrs == NULL_TREE)
9331 return;
9332
9333 a = attrs;
9334 /* See if any of the nonnull attributes has no arguments. If so,
9335 then every pointer argument is checked (in which case the check
9336 for pointer type is done in check_nonnull_arg). */
9337 if (TREE_VALUE (a) != NULL_TREE)
9338 do
9339 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9340 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9341
9342 if (a != NULL_TREE)
9343 for (i = 0; i < nargs; i++)
9344 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9345 i + 1);
9346 else
dbf6c367 9347 {
9ca77b08 9348 /* Walk the argument list. If we encounter an argument number we
9349 should check for non-null, do it. */
9350 for (i = 0; i < nargs; i++)
dbf6c367 9351 {
9ca77b08 9352 for (a = attrs; ; a = TREE_CHAIN (a))
4ee9c684 9353 {
9ca77b08 9354 a = lookup_attribute ("nonnull", a);
9355 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9356 break;
4ee9c684 9357 }
9ca77b08 9358
9359 if (a != NULL_TREE)
9360 check_function_arguments_recurse (check_nonnull_arg, NULL,
9361 argarray[i], i + 1);
dbf6c367 9362 }
9363 }
9364}
9365
50ca527f 9366/* Check that the Nth argument of a function call (counting backwards
d01f58f9 9367 from the end) is a (pointer)0. The NARGS arguments are passed in the
9368 array ARGARRAY. */
bf6c8de0 9369
9370static void
774e9d58 9371check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
bf6c8de0 9372{
774e9d58 9373 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
bf6c8de0 9374
9375 if (attr)
9376 {
d01f58f9 9377 int len = 0;
9378 int pos = 0;
9379 tree sentinel;
774e9d58 9380 function_args_iterator iter;
9381 tree t;
a0c938f0 9382
d01f58f9 9383 /* Skip over the named arguments. */
774e9d58 9384 FOREACH_FUNCTION_ARGS (fntype, t, iter)
a0c938f0 9385 {
774e9d58 9386 if (len == nargs)
9387 break;
d01f58f9 9388 len++;
9389 }
50ca527f 9390
d01f58f9 9391 if (TREE_VALUE (attr))
9392 {
9393 tree p = TREE_VALUE (TREE_VALUE (attr));
f9ae6f95 9394 pos = TREE_INT_CST_LOW (p);
d01f58f9 9395 }
50ca527f 9396
d01f58f9 9397 /* The sentinel must be one of the varargs, i.e.
9398 in position >= the number of fixed arguments. */
9399 if ((nargs - 1 - pos) < len)
9400 {
77a357e3 9401 warning (OPT_Wformat_,
d01f58f9 9402 "not enough variable arguments to fit a sentinel");
9403 return;
bf6c8de0 9404 }
d01f58f9 9405
9406 /* Validate the sentinel. */
9407 sentinel = argarray[nargs - 1 - pos];
9408 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9409 || !integer_zerop (sentinel))
9410 /* Although __null (in C++) is only an integer we allow it
9411 nevertheless, as we are guaranteed that it's exactly
9412 as wide as a pointer, and we don't want to force
9413 users to cast the NULL they have written there.
9414 We warn with -Wstrict-null-sentinel, though. */
9415 && (warn_strict_null_sentinel || null_node != sentinel))
77a357e3 9416 warning (OPT_Wformat_, "missing sentinel in function call");
bf6c8de0 9417 }
9418}
9419
dbf6c367 9420/* Helper for check_function_nonnull; given a list of operands which
9421 must be non-null in ARGS, determine if operand PARAM_NUM should be
9422 checked. */
9423
9424static bool
1cae46be 9425nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
dbf6c367 9426{
4ee9c684 9427 unsigned HOST_WIDE_INT arg_num = 0;
dbf6c367 9428
9429 for (; args; args = TREE_CHAIN (args))
9430 {
231bd014 9431 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9432
9433 gcc_assert (found);
dbf6c367 9434
9435 if (arg_num == param_num)
9436 return true;
9437 }
9438 return false;
9439}
9440
9441/* Check that the function argument PARAM (which is operand number
9442 PARAM_NUM) is non-null. This is called by check_function_nonnull
9443 via check_function_arguments_recurse. */
9444
9445static void
9a03a746 9446check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
1cae46be 9447 unsigned HOST_WIDE_INT param_num)
dbf6c367 9448{
9449 /* Just skip checking the argument if it's not a pointer. This can
9450 happen if the "nonnull" attribute was given without an operand
9451 list (which means to check every pointer argument). */
9452
9453 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9454 return;
9455
9456 if (integer_zerop (param))
155b601b 9457 warning (OPT_Wnonnull, "null argument where non-null required "
9458 "(argument %lu)", (unsigned long) param_num);
dbf6c367 9459}
9460
9461/* Helper for nonnull attribute handling; fetch the operand number
9462 from the attribute argument list. */
9463
9464static bool
1cae46be 9465get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
dbf6c367 9466{
e913b5cd 9467 /* Verify the arg number is a small constant. */
e1d65c9f 9468 if (tree_fits_uhwi_p (arg_num_expr))
e913b5cd 9469 {
f9ae6f95 9470 *valp = TREE_INT_CST_LOW (arg_num_expr);
e913b5cd 9471 return true;
9472 }
9473 else
dbf6c367 9474 return false;
dbf6c367 9475}
fa987697 9476
9477/* Handle a "nothrow" attribute; arguments as in
9478 struct attribute_spec.handler. */
9479
9480static tree
9a03a746 9481handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9482 int ARG_UNUSED (flags), bool *no_add_attrs)
fa987697 9483{
9484 if (TREE_CODE (*node) == FUNCTION_DECL)
9485 TREE_NOTHROW (*node) = 1;
9486 /* ??? TODO: Support types. */
9487 else
9488 {
9b2d6d13 9489 warning (OPT_Wattributes, "%qE attribute ignored", name);
fa987697 9490 *no_add_attrs = true;
9491 }
9492
9493 return NULL_TREE;
9494}
7acb29a3 9495
9496/* Handle a "cleanup" attribute; arguments as in
9497 struct attribute_spec.handler. */
9498
9499static tree
1cae46be 9500handle_cleanup_attribute (tree *node, tree name, tree args,
9a03a746 9501 int ARG_UNUSED (flags), bool *no_add_attrs)
7acb29a3 9502{
9503 tree decl = *node;
9504 tree cleanup_id, cleanup_decl;
9505
9506 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9507 for global destructors in C++. This requires infrastructure that
9508 we don't have generically at the moment. It's also not a feature
9509 we'd be missing too much, since we do have attribute constructor. */
9510 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
9511 {
9b2d6d13 9512 warning (OPT_Wattributes, "%qE attribute ignored", name);
7acb29a3 9513 *no_add_attrs = true;
9514 return NULL_TREE;
9515 }
9516
9517 /* Verify that the argument is a function in scope. */
9518 /* ??? We could support pointers to functions here as well, if
9519 that was considered desirable. */
9520 cleanup_id = TREE_VALUE (args);
9521 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9522 {
07e3a3d2 9523 error ("cleanup argument not an identifier");
7acb29a3 9524 *no_add_attrs = true;
9525 return NULL_TREE;
9526 }
d1c41717 9527 cleanup_decl = lookup_name (cleanup_id);
7acb29a3 9528 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9529 {
07e3a3d2 9530 error ("cleanup argument not a function");
7acb29a3 9531 *no_add_attrs = true;
9532 return NULL_TREE;
9533 }
9534
1cae46be 9535 /* That the function has proper type is checked with the
7acb29a3 9536 eventual call to build_function_call. */
9537
9538 return NULL_TREE;
9539}
8a8cdb8d 9540
9541/* Handle a "warn_unused_result" attribute. No special handling. */
9542
9543static tree
9544handle_warn_unused_result_attribute (tree *node, tree name,
9a03a746 9545 tree ARG_UNUSED (args),
9546 int ARG_UNUSED (flags), bool *no_add_attrs)
8a8cdb8d 9547{
9548 /* Ignore the attribute for functions not returning any value. */
9549 if (VOID_TYPE_P (TREE_TYPE (*node)))
9550 {
9b2d6d13 9551 warning (OPT_Wattributes, "%qE attribute ignored", name);
8a8cdb8d 9552 *no_add_attrs = true;
9553 }
9554
9555 return NULL_TREE;
9556}
bf6c8de0 9557
9558/* Handle a "sentinel" attribute. */
9559
9560static tree
50ca527f 9561handle_sentinel_attribute (tree *node, tree name, tree args,
bf6c8de0 9562 int ARG_UNUSED (flags), bool *no_add_attrs)
9563{
a36cf284 9564 if (!prototype_p (*node))
bf6c8de0 9565 {
9b2d6d13 9566 warning (OPT_Wattributes,
9567 "%qE attribute requires prototypes with named arguments", name);
bf6c8de0 9568 *no_add_attrs = true;
bf6c8de0 9569 }
50ca527f 9570 else
9571 {
c33080b9 9572 if (!stdarg_p (*node))
a0c938f0 9573 {
9b2d6d13 9574 warning (OPT_Wattributes,
9575 "%qE attribute only applies to variadic functions", name);
50ca527f 9576 *no_add_attrs = true;
9577 }
9578 }
a0c938f0 9579
50ca527f 9580 if (args)
bf6c8de0 9581 {
50ca527f 9582 tree position = TREE_VALUE (args);
3e5a8b00 9583 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9584 && TREE_CODE (position) != FUNCTION_DECL)
9585 position = default_conversion (position);
50ca527f 9586
3e5a8b00 9587 if (TREE_CODE (position) != INTEGER_CST
9588 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
a0c938f0 9589 {
48e1416a 9590 warning (OPT_Wattributes,
01b54db5 9591 "requested position is not an integer constant");
50ca527f 9592 *no_add_attrs = true;
9593 }
9594 else
a0c938f0 9595 {
50ca527f 9596 if (tree_int_cst_lt (position, integer_zero_node))
9597 {
01b54db5 9598 warning (OPT_Wattributes,
9599 "requested position is less than zero");
50ca527f 9600 *no_add_attrs = true;
9601 }
9602 }
bf6c8de0 9603 }
a0c938f0 9604
bf6c8de0 9605 return NULL_TREE;
9606}
b5c26b42 9607
9608/* Handle a "type_generic" attribute. */
9609
9610static tree
9611handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9612 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9613 bool * ARG_UNUSED (no_add_attrs))
9614{
19fbe3a4 9615 /* Ensure we have a function type. */
9616 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
48e1416a 9617
19fbe3a4 9618 /* Ensure we have a variadic function. */
c33080b9 9619 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
b5c26b42 9620
9621 return NULL_TREE;
9622}
46f8e3b0 9623
24470055 9624/* Handle a "target" attribute. */
46f8e3b0 9625
9626static tree
24470055 9627handle_target_attribute (tree *node, tree name, tree args, int flags,
46f8e3b0 9628 bool *no_add_attrs)
9629{
9630 /* Ensure we have a function type. */
9631 if (TREE_CODE (*node) != FUNCTION_DECL)
9632 {
9633 warning (OPT_Wattributes, "%qE attribute ignored", name);
9634 *no_add_attrs = true;
9635 }
46f8e3b0 9636 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
ae0c3984 9637 flags))
46f8e3b0 9638 *no_add_attrs = true;
9639
9640 return NULL_TREE;
9641}
9642
9643/* Arguments being collected for optimization. */
9644typedef const char *const_char_p; /* For DEF_VEC_P. */
f1f41a6c 9645static GTY(()) vec<const_char_p, va_gc> *optimize_args;
46f8e3b0 9646
9647
9648/* Inner function to convert a TREE_LIST to argv string to parse the optimize
9649 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9650 false for #pragma GCC optimize. */
9651
9652bool
9653parse_optimize_options (tree args, bool attr_p)
9654{
9655 bool ret = true;
9656 unsigned opt_argc;
9657 unsigned i;
2becf397 9658 int saved_flag_strict_aliasing;
46f8e3b0 9659 const char **opt_argv;
615ef0bb 9660 struct cl_decoded_option *decoded_options;
9661 unsigned int decoded_options_count;
46f8e3b0 9662 tree ap;
9663
9664 /* Build up argv vector. Just in case the string is stored away, use garbage
9665 collected strings. */
f1f41a6c 9666 vec_safe_truncate (optimize_args, 0);
9667 vec_safe_push (optimize_args, (const char *) NULL);
46f8e3b0 9668
9669 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9670 {
9671 tree value = TREE_VALUE (ap);
9672
9673 if (TREE_CODE (value) == INTEGER_CST)
9674 {
9675 char buffer[20];
f9ae6f95 9676 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
f1f41a6c 9677 vec_safe_push (optimize_args, ggc_strdup (buffer));
46f8e3b0 9678 }
9679
9680 else if (TREE_CODE (value) == STRING_CST)
9681 {
9682 /* Split string into multiple substrings. */
9683 size_t len = TREE_STRING_LENGTH (value);
9684 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9685 char *end = p + len;
9686 char *comma;
9687 char *next_p = p;
9688
9689 while (next_p != NULL)
9690 {
9691 size_t len2;
9692 char *q, *r;
9693
9694 p = next_p;
9695 comma = strchr (p, ',');
9696 if (comma)
9697 {
9698 len2 = comma - p;
9699 *comma = '\0';
9700 next_p = comma+1;
9701 }
9702 else
9703 {
9704 len2 = end - p;
9705 next_p = NULL;
9706 }
9707
ba72912a 9708 r = q = (char *) ggc_alloc_atomic (len2 + 3);
46f8e3b0 9709
9710 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9711 options. */
9712 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9713 {
9714 ret = false;
9715 if (attr_p)
9716 warning (OPT_Wattributes,
ebd7c4c1 9717 "bad option %s to optimize attribute", p);
46f8e3b0 9718 else
9719 warning (OPT_Wpragmas,
e44b0a1f 9720 "bad option %s to pragma attribute", p);
46f8e3b0 9721 continue;
9722 }
9723
9724 if (*p != '-')
9725 {
9726 *r++ = '-';
9727
9728 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9729 itself is -Os, and any other switch begins with a -f. */
9730 if ((*p >= '0' && *p <= '9')
9731 || (p[0] == 's' && p[1] == '\0'))
9732 *r++ = 'O';
9733 else if (*p != 'O')
9734 *r++ = 'f';
9735 }
9736
9737 memcpy (r, p, len2);
9738 r[len2] = '\0';
f1f41a6c 9739 vec_safe_push (optimize_args, (const char *) q);
46f8e3b0 9740 }
9741
9742 }
9743 }
9744
f1f41a6c 9745 opt_argc = optimize_args->length ();
46f8e3b0 9746 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9747
9748 for (i = 1; i < opt_argc; i++)
f1f41a6c 9749 opt_argv[i] = (*optimize_args)[i];
46f8e3b0 9750
2becf397 9751 saved_flag_strict_aliasing = flag_strict_aliasing;
9752
46f8e3b0 9753 /* Now parse the options. */
f3f006ad 9754 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9755 &decoded_options,
9756 &decoded_options_count);
9757 decode_options (&global_options, &global_options_set,
3c6c0e40 9758 decoded_options, decoded_options_count,
9759 input_location, global_dc);
46f8e3b0 9760
4bec06b3 9761 targetm.override_options_after_change();
9762
2becf397 9763 /* Don't allow changing -fstrict-aliasing. */
9764 flag_strict_aliasing = saved_flag_strict_aliasing;
9765
f1f41a6c 9766 optimize_args->truncate (0);
46f8e3b0 9767 return ret;
9768}
9769
9770/* For handling "optimize" attribute. arguments as in
9771 struct attribute_spec.handler. */
9772
9773static tree
9774handle_optimize_attribute (tree *node, tree name, tree args,
9775 int ARG_UNUSED (flags), bool *no_add_attrs)
9776{
9777 /* Ensure we have a function type. */
9778 if (TREE_CODE (*node) != FUNCTION_DECL)
9779 {
9780 warning (OPT_Wattributes, "%qE attribute ignored", name);
9781 *no_add_attrs = true;
9782 }
9783 else
9784 {
9785 struct cl_optimization cur_opts;
9786 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9787
9788 /* Save current options. */
2c5d2e39 9789 cl_optimization_save (&cur_opts, &global_options);
46f8e3b0 9790
9791 /* If we previously had some optimization options, use them as the
9792 default. */
9793 if (old_opts)
2c5d2e39 9794 cl_optimization_restore (&global_options,
9795 TREE_OPTIMIZATION (old_opts));
46f8e3b0 9796
9797 /* Parse options, and update the vector. */
9798 parse_optimize_options (args, true);
9799 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
55310327 9800 = build_optimization_node (&global_options);
46f8e3b0 9801
9802 /* Restore current options. */
2c5d2e39 9803 cl_optimization_restore (&global_options, &cur_opts);
46f8e3b0 9804 }
9805
9806 return NULL_TREE;
9807}
48b14f50 9808
9809/* Handle a "no_split_stack" attribute. */
9810
9811static tree
9812handle_no_split_stack_attribute (tree *node, tree name,
9813 tree ARG_UNUSED (args),
9814 int ARG_UNUSED (flags),
9815 bool *no_add_attrs)
9816{
9817 tree decl = *node;
9818
9819 if (TREE_CODE (decl) != FUNCTION_DECL)
9820 {
9821 error_at (DECL_SOURCE_LOCATION (decl),
9822 "%qE attribute applies only to functions", name);
9823 *no_add_attrs = true;
9824 }
9825 else if (DECL_INITIAL (decl))
9826 {
9827 error_at (DECL_SOURCE_LOCATION (decl),
9828 "can%'t set %qE attribute after definition", name);
9829 *no_add_attrs = true;
9830 }
9831
9832 return NULL_TREE;
9833}
d7dcba40 9834
9835/* Handle a "returns_nonnull" attribute; arguments as in
9836 struct attribute_spec.handler. */
9837
9838static tree
9839handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9840 bool *no_add_attrs)
9841{
9842 // Even without a prototype we still have a return type we can check.
9843 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9844 {
9845 error ("returns_nonnull attribute on a function not returning a pointer");
9846 *no_add_attrs = true;
9847 }
9848 return NULL_TREE;
9849}
9850
74691f46 9851/* Handle a "designated_init" attribute; arguments as in
9852 struct attribute_spec.handler. */
9853
9854static tree
9855handle_designated_init_attribute (tree *node, tree name, tree, int,
9856 bool *no_add_attrs)
9857{
9858 if (TREE_CODE (*node) != RECORD_TYPE)
9859 {
9860 error ("%qE attribute is only valid on %<struct%> type", name);
9861 *no_add_attrs = true;
9862 }
9863 return NULL_TREE;
9864}
9865
dbf6c367 9866\f
774e9d58 9867/* Check for valid arguments being passed to a function with FNTYPE.
9868 There are NARGS arguments in the array ARGARRAY. */
dbf6c367 9869void
774e9d58 9870check_function_arguments (const_tree fntype, int nargs, tree *argarray)
dbf6c367 9871{
9872 /* Check for null being passed in a pointer argument that must be
9873 non-null. We also need to do this if format checking is enabled. */
9874
9875 if (warn_nonnull)
774e9d58 9876 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
dbf6c367 9877
9878 /* Check for errors in format strings. */
9879
068bea1e 9880 if (warn_format || warn_suggest_attribute_format)
774e9d58 9881 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
95c90e04 9882
9883 if (warn_format)
774e9d58 9884 check_function_sentinel (fntype, nargs, argarray);
dbf6c367 9885}
9886
9887/* Generic argument checking recursion routine. PARAM is the argument to
9888 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9889 once the argument is resolved. CTX is context for the callback. */
9890void
1cae46be 9891check_function_arguments_recurse (void (*callback)
9892 (void *, tree, unsigned HOST_WIDE_INT),
9893 void *ctx, tree param,
9894 unsigned HOST_WIDE_INT param_num)
dbf6c367 9895{
72dd6141 9896 if (CONVERT_EXPR_P (param)
c44afe23 9897 && (TYPE_PRECISION (TREE_TYPE (param))
9898 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
dbf6c367 9899 {
9900 /* Strip coercion. */
9901 check_function_arguments_recurse (callback, ctx,
4ee9c684 9902 TREE_OPERAND (param, 0), param_num);
dbf6c367 9903 return;
9904 }
9905
9906 if (TREE_CODE (param) == CALL_EXPR)
9907 {
c2f47e15 9908 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
dbf6c367 9909 tree attrs;
9910 bool found_format_arg = false;
9911
9912 /* See if this is a call to a known internationalization function
9913 that modifies a format arg. Such a function may have multiple
9914 format_arg attributes (for example, ngettext). */
9915
9916 for (attrs = TYPE_ATTRIBUTES (type);
9917 attrs;
9918 attrs = TREE_CHAIN (attrs))
9919 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9920 {
c2f47e15 9921 tree inner_arg;
dbf6c367 9922 tree format_num_expr;
9923 int format_num;
9924 int i;
c2f47e15 9925 call_expr_arg_iterator iter;
dbf6c367 9926
9927 /* Extract the argument number, which was previously checked
9928 to be valid. */
9929 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
ddb1be65 9930
e913b5cd 9931 format_num = tree_to_uhwi (format_num_expr);
dbf6c367 9932
c2f47e15 9933 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9934 inner_arg != 0;
9935 inner_arg = next_call_expr_arg (&iter), i++)
dbf6c367 9936 if (i == format_num)
9937 {
9938 check_function_arguments_recurse (callback, ctx,
c2f47e15 9939 inner_arg, param_num);
dbf6c367 9940 found_format_arg = true;
9941 break;
9942 }
9943 }
9944
9945 /* If we found a format_arg attribute and did a recursive check,
9946 we are done with checking this argument. Otherwise, we continue
9947 and this will be considered a non-literal. */
9948 if (found_format_arg)
9949 return;
9950 }
9951
9952 if (TREE_CODE (param) == COND_EXPR)
9953 {
9954 /* Check both halves of the conditional expression. */
9955 check_function_arguments_recurse (callback, ctx,
4ee9c684 9956 TREE_OPERAND (param, 1), param_num);
dbf6c367 9957 check_function_arguments_recurse (callback, ctx,
4ee9c684 9958 TREE_OPERAND (param, 2), param_num);
dbf6c367 9959 return;
9960 }
9961
9962 (*callback) (ctx, param, param_num);
9963}
1f3233d1 9964
60cce472 9965/* Checks for a builtin function FNDECL that the number of arguments
9966 NARGS against the required number REQUIRED and issues an error if
9967 there is a mismatch. Returns true if the number of arguments is
9968 correct, otherwise false. */
d43cee80 9969
9970static bool
60cce472 9971builtin_function_validate_nargs (tree fndecl, int nargs, int required)
d43cee80 9972{
9973 if (nargs < required)
9974 {
60cce472 9975 error_at (input_location,
9976 "not enough arguments to function %qE", fndecl);
d43cee80 9977 return false;
9978 }
9979 else if (nargs > required)
9980 {
60cce472 9981 error_at (input_location,
9982 "too many arguments to function %qE", fndecl);
d43cee80 9983 return false;
9984 }
9985 return true;
9986}
9987
9988/* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9989 Returns false if there was an error, otherwise true. */
9990
9991bool
9992check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9993{
9994 if (!DECL_BUILT_IN (fndecl)
9995 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9996 return true;
9997
9998 switch (DECL_FUNCTION_CODE (fndecl))
9999 {
10000 case BUILT_IN_CONSTANT_P:
60cce472 10001 return builtin_function_validate_nargs (fndecl, nargs, 1);
d43cee80 10002
10003 case BUILT_IN_ISFINITE:
10004 case BUILT_IN_ISINF:
c319d56a 10005 case BUILT_IN_ISINF_SIGN:
d43cee80 10006 case BUILT_IN_ISNAN:
10007 case BUILT_IN_ISNORMAL:
60cce472 10008 if (builtin_function_validate_nargs (fndecl, nargs, 1))
d43cee80 10009 {
10010 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
10011 {
10012 error ("non-floating-point argument in call to "
10013 "function %qE", fndecl);
10014 return false;
10015 }
10016 return true;
10017 }
10018 return false;
10019
10020 case BUILT_IN_ISGREATER:
10021 case BUILT_IN_ISGREATEREQUAL:
10022 case BUILT_IN_ISLESS:
10023 case BUILT_IN_ISLESSEQUAL:
10024 case BUILT_IN_ISLESSGREATER:
10025 case BUILT_IN_ISUNORDERED:
60cce472 10026 if (builtin_function_validate_nargs (fndecl, nargs, 2))
d43cee80 10027 {
10028 enum tree_code code0, code1;
10029 code0 = TREE_CODE (TREE_TYPE (args[0]));
10030 code1 = TREE_CODE (TREE_TYPE (args[1]));
10031 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
10032 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10033 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
10034 {
10035 error ("non-floating-point arguments in call to "
10036 "function %qE", fndecl);
10037 return false;
10038 }
10039 return true;
10040 }
10041 return false;
10042
19fbe3a4 10043 case BUILT_IN_FPCLASSIFY:
60cce472 10044 if (builtin_function_validate_nargs (fndecl, nargs, 6))
19fbe3a4 10045 {
10046 unsigned i;
48e1416a 10047
19fbe3a4 10048 for (i=0; i<5; i++)
10049 if (TREE_CODE (args[i]) != INTEGER_CST)
10050 {
10051 error ("non-const integer argument %u in call to function %qE",
10052 i+1, fndecl);
10053 return false;
10054 }
10055
10056 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
10057 {
10058 error ("non-floating-point argument in call to function %qE",
10059 fndecl);
10060 return false;
10061 }
10062 return true;
10063 }
10064 return false;
10065
fca0886c 10066 case BUILT_IN_ASSUME_ALIGNED:
10067 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
10068 {
10069 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
10070 {
10071 error ("non-integer argument 3 in call to function %qE", fndecl);
10072 return false;
10073 }
10074 return true;
10075 }
10076 return false;
10077
0c93c8a9 10078 case BUILT_IN_ADD_OVERFLOW:
10079 case BUILT_IN_SUB_OVERFLOW:
10080 case BUILT_IN_MUL_OVERFLOW:
10081 if (builtin_function_validate_nargs (fndecl, nargs, 3))
10082 {
10083 unsigned i;
10084 for (i = 0; i < 2; i++)
10085 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
10086 {
10087 error ("argument %u in call to function %qE does not have "
10088 "integral type", i + 1, fndecl);
10089 return false;
10090 }
10091 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
10092 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
10093 {
10094 error ("argument 3 in call to function %qE does not have "
10095 "pointer to integer type", fndecl);
10096 return false;
10097 }
10098 return true;
10099 }
10100 return false;
10101
d43cee80 10102 default:
10103 return true;
10104 }
10105}
10106
860251be 10107/* Function to help qsort sort FIELD_DECLs by name order. */
10108
10109int
10110field_decl_cmp (const void *x_p, const void *y_p)
10111{
4fd61bc6 10112 const tree *const x = (const tree *const) x_p;
10113 const tree *const y = (const tree *const) y_p;
10114
860251be 10115 if (DECL_NAME (*x) == DECL_NAME (*y))
10116 /* A nontype is "greater" than a type. */
10117 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10118 if (DECL_NAME (*x) == NULL_TREE)
10119 return -1;
10120 if (DECL_NAME (*y) == NULL_TREE)
10121 return 1;
10122 if (DECL_NAME (*x) < DECL_NAME (*y))
10123 return -1;
10124 return 1;
10125}
10126
10127static struct {
10128 gt_pointer_operator new_value;
10129 void *cookie;
10130} resort_data;
10131
10132/* This routine compares two fields like field_decl_cmp but using the
10133pointer operator in resort_data. */
10134
10135static int
10136resort_field_decl_cmp (const void *x_p, const void *y_p)
10137{
4fd61bc6 10138 const tree *const x = (const tree *const) x_p;
10139 const tree *const y = (const tree *const) y_p;
860251be 10140
10141 if (DECL_NAME (*x) == DECL_NAME (*y))
10142 /* A nontype is "greater" than a type. */
10143 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10144 if (DECL_NAME (*x) == NULL_TREE)
10145 return -1;
10146 if (DECL_NAME (*y) == NULL_TREE)
10147 return 1;
10148 {
10149 tree d1 = DECL_NAME (*x);
10150 tree d2 = DECL_NAME (*y);
10151 resort_data.new_value (&d1, resort_data.cookie);
10152 resort_data.new_value (&d2, resort_data.cookie);
10153 if (d1 < d2)
10154 return -1;
10155 }
10156 return 1;
10157}
10158
10159/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10160
10161void
10162resort_sorted_fields (void *obj,
9a03a746 10163 void * ARG_UNUSED (orig_obj),
4ee9c684 10164 gt_pointer_operator new_value,
10165 void *cookie)
860251be 10166{
9a03a746 10167 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
860251be 10168 resort_data.new_value = new_value;
10169 resort_data.cookie = cookie;
10170 qsort (&sf->elts[0], sf->len, sizeof (tree),
4ee9c684 10171 resort_field_decl_cmp);
860251be 10172}
10173
209c9752 10174/* Subroutine of c_parse_error.
10175 Return the result of concatenating LHS and RHS. RHS is really
10176 a string literal, its first character is indicated by RHS_START and
cfee01e3 10177 RHS_SIZE is its length (including the terminating NUL character).
209c9752 10178
10179 The caller is responsible for deleting the returned pointer. */
10180
10181static char *
10182catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10183{
10184 const int lhs_size = strlen (lhs);
10185 char *result = XNEWVEC (char, lhs_size + rhs_size);
10186 strncpy (result, lhs, lhs_size);
10187 strncpy (result + lhs_size, rhs_start, rhs_size);
10188 return result;
10189}
10190
380c6697 10191/* Issue the error given by GMSGID, indicating that it occurred before
92b128ed 10192 TOKEN, which had the associated VALUE. */
10193
10194void
48e1416a 10195c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
ba99525e 10196 tree value, unsigned char token_flags)
92b128ed 10197{
209c9752 10198#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10199
10200 char *message = NULL;
92b128ed 10201
ba99525e 10202 if (token_type == CPP_EOF)
380c6697 10203 message = catenate_messages (gmsgid, " at end of input");
48e1416a 10204 else if (token_type == CPP_CHAR
10205 || token_type == CPP_WCHAR
ba99525e 10206 || token_type == CPP_CHAR16
10207 || token_type == CPP_CHAR32)
92b128ed 10208 {
f9ae6f95 10209 unsigned int val = TREE_INT_CST_LOW (value);
924bbf02 10210 const char *prefix;
10211
ba99525e 10212 switch (token_type)
924bbf02 10213 {
10214 default:
10215 prefix = "";
10216 break;
10217 case CPP_WCHAR:
10218 prefix = "L";
10219 break;
10220 case CPP_CHAR16:
10221 prefix = "u";
10222 break;
10223 case CPP_CHAR32:
10224 prefix = "U";
10225 break;
10226 }
10227
92b128ed 10228 if (val <= UCHAR_MAX && ISGRAPH (val))
a0c938f0 10229 message = catenate_messages (gmsgid, " before %s'%c'");
92b128ed 10230 else
a0c938f0 10231 message = catenate_messages (gmsgid, " before %s'\\x%x'");
209c9752 10232
924bbf02 10233 error (message, prefix, val);
209c9752 10234 free (message);
10235 message = NULL;
92b128ed 10236 }
1898176c 10237 else if (token_type == CPP_CHAR_USERDEF
10238 || token_type == CPP_WCHAR_USERDEF
10239 || token_type == CPP_CHAR16_USERDEF
10240 || token_type == CPP_CHAR32_USERDEF)
10241 message = catenate_messages (gmsgid,
10242 " before user-defined character literal");
10243 else if (token_type == CPP_STRING_USERDEF
10244 || token_type == CPP_WSTRING_USERDEF
10245 || token_type == CPP_STRING16_USERDEF
10246 || token_type == CPP_STRING32_USERDEF
10247 || token_type == CPP_UTF8STRING_USERDEF)
10248 message = catenate_messages (gmsgid, " before user-defined string literal");
48e1416a 10249 else if (token_type == CPP_STRING
10250 || token_type == CPP_WSTRING
ba99525e 10251 || token_type == CPP_STRING16
538ba11a 10252 || token_type == CPP_STRING32
10253 || token_type == CPP_UTF8STRING)
380c6697 10254 message = catenate_messages (gmsgid, " before string constant");
ba99525e 10255 else if (token_type == CPP_NUMBER)
380c6697 10256 message = catenate_messages (gmsgid, " before numeric constant");
ba99525e 10257 else if (token_type == CPP_NAME)
209c9752 10258 {
380c6697 10259 message = catenate_messages (gmsgid, " before %qE");
782858b8 10260 error (message, value);
209c9752 10261 free (message);
10262 message = NULL;
10263 }
ba99525e 10264 else if (token_type == CPP_PRAGMA)
b75b98aa 10265 message = catenate_messages (gmsgid, " before %<#pragma%>");
ba99525e 10266 else if (token_type == CPP_PRAGMA_EOL)
b75b98aa 10267 message = catenate_messages (gmsgid, " before end of line");
07b8f133 10268 else if (token_type == CPP_DECLTYPE)
10269 message = catenate_messages (gmsgid, " before %<decltype%>");
ba99525e 10270 else if (token_type < N_TTYPES)
209c9752 10271 {
380c6697 10272 message = catenate_messages (gmsgid, " before %qs token");
ba99525e 10273 error (message, cpp_type2name (token_type, token_flags));
209c9752 10274 free (message);
10275 message = NULL;
10276 }
92b128ed 10277 else
380c6697 10278 error (gmsgid);
209c9752 10279
10280 if (message)
10281 {
10282 error (message);
10283 free (message);
10284 }
a0c938f0 10285#undef catenate_messages
92b128ed 10286}
10287
3a79f5da 10288/* Return the gcc option code associated with the reason for a cpp
10289 message, or 0 if none. */
10290
10291static int
10292c_option_controlling_cpp_error (int reason)
10293{
7ff8db31 10294 const struct cpp_reason_option_codes_t *entry;
3a79f5da 10295
7ff8db31 10296 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
3a79f5da 10297 {
10298 if (entry->reason == reason)
10299 return entry->option_code;
10300 }
10301 return 0;
10302}
10303
7f5f3953 10304/* Callback from cpp_error for PFILE to print diagnostics from the
3a79f5da 10305 preprocessor. The diagnostic is of type LEVEL, with REASON set
10306 to the reason code if LEVEL is represents a warning, at location
7f5f3953 10307 LOCATION unless this is after lexing and the compiler's location
10308 should be used instead, with column number possibly overridden by
10309 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
10310 the arguments. Returns true if a diagnostic was emitted, false
10311 otherwise. */
10312
10313bool
3a79f5da 10314c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
7f5f3953 10315 location_t location, unsigned int column_override,
10316 const char *msg, va_list *ap)
10317{
10318 diagnostic_info diagnostic;
10319 diagnostic_t dlevel;
5ae82d58 10320 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
7f5f3953 10321 bool ret;
10322
10323 switch (level)
10324 {
10325 case CPP_DL_WARNING_SYSHDR:
10326 if (flag_no_output)
10327 return false;
5ae82d58 10328 global_dc->dc_warn_system_headers = 1;
7f5f3953 10329 /* Fall through. */
10330 case CPP_DL_WARNING:
10331 if (flag_no_output)
10332 return false;
10333 dlevel = DK_WARNING;
10334 break;
10335 case CPP_DL_PEDWARN:
10336 if (flag_no_output && !flag_pedantic_errors)
10337 return false;
10338 dlevel = DK_PEDWARN;
10339 break;
10340 case CPP_DL_ERROR:
10341 dlevel = DK_ERROR;
10342 break;
10343 case CPP_DL_ICE:
10344 dlevel = DK_ICE;
10345 break;
10346 case CPP_DL_NOTE:
10347 dlevel = DK_NOTE;
10348 break;
ff903809 10349 case CPP_DL_FATAL:
10350 dlevel = DK_FATAL;
10351 break;
7f5f3953 10352 default:
10353 gcc_unreachable ();
10354 }
10355 if (done_lexing)
10356 location = input_location;
10357 diagnostic_set_info_translated (&diagnostic, msg, ap,
10358 location, dlevel);
10359 if (column_override)
10360 diagnostic_override_column (&diagnostic, column_override);
3a79f5da 10361 diagnostic_override_option_index (&diagnostic,
10362 c_option_controlling_cpp_error (reason));
7f5f3953 10363 ret = report_diagnostic (&diagnostic);
10364 if (level == CPP_DL_WARNING_SYSHDR)
5ae82d58 10365 global_dc->dc_warn_system_headers = save_warn_system_headers;
7f5f3953 10366 return ret;
10367}
10368
624d37a6 10369/* Convert a character from the host to the target execution character
10370 set. cpplib handles this, mostly. */
10371
10372HOST_WIDE_INT
10373c_common_to_target_charset (HOST_WIDE_INT c)
10374{
10375 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10376 zero-extended under -fno-signed-char. cpplib insists that characters
10377 and character constants are always unsigned. Hence we must convert
10378 back and forth. */
10379 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10380
10381 uc = cpp_host_to_exec_charset (parse_in, uc);
10382
10383 if (flag_signed_char)
10384 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10385 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10386 else
10387 return uc;
10388}
10389
7549df0d 10390/* Fold an offsetof-like expression. EXPR is a nested sequence of component
10391 references with an INDIRECT_REF of a constant at the bottom; much like the
10392 traditional rendering of offsetof as a macro. Return the folded result. */
af28855b 10393
7549df0d 10394tree
10395fold_offsetof_1 (tree expr)
af28855b 10396{
af28855b 10397 tree base, off, t;
10398
10399 switch (TREE_CODE (expr))
10400 {
10401 case ERROR_MARK:
10402 return expr;
10403
6b11d2e3 10404 case VAR_DECL:
10405 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10406 return error_mark_node;
10407
d897f7c2 10408 case CALL_EXPR:
cf1a89a3 10409 case TARGET_EXPR:
d897f7c2 10410 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10411 return error_mark_node;
10412
d897f7c2 10413 case NOP_EXPR:
10414 case INDIRECT_REF:
7549df0d 10415 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
64ed018c 10416 {
10417 error ("cannot apply %<offsetof%> to a non constant address");
10418 return error_mark_node;
10419 }
7549df0d 10420 return TREE_OPERAND (expr, 0);
d897f7c2 10421
af28855b 10422 case COMPONENT_REF:
7549df0d 10423 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
af28855b 10424 if (base == error_mark_node)
10425 return base;
10426
10427 t = TREE_OPERAND (expr, 1);
10428 if (DECL_C_BIT_FIELD (t))
10429 {
10430 error ("attempt to take address of bit-field structure "
782858b8 10431 "member %qD", t);
af28855b 10432 return error_mark_node;
10433 }
389dd41b 10434 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
e913b5cd 10435 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
389dd41b 10436 / BITS_PER_UNIT));
af28855b 10437 break;
10438
10439 case ARRAY_REF:
7549df0d 10440 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
af28855b 10441 if (base == error_mark_node)
10442 return base;
10443
10444 t = TREE_OPERAND (expr, 1);
64ed018c 10445
10446 /* Check if the offset goes beyond the upper bound of the array. */
7549df0d 10447 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
e0559d69 10448 {
10449 tree upbound = array_ref_up_bound (expr);
10450 if (upbound != NULL_TREE
10451 && TREE_CODE (upbound) == INTEGER_CST
10452 && !tree_int_cst_equal (upbound,
10453 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10454 {
10455 upbound = size_binop (PLUS_EXPR, upbound,
10456 build_int_cst (TREE_TYPE (upbound), 1));
10457 if (tree_int_cst_lt (upbound, t))
10458 {
10459 tree v;
10460
10461 for (v = TREE_OPERAND (expr, 0);
10462 TREE_CODE (v) == COMPONENT_REF;
10463 v = TREE_OPERAND (v, 0))
10464 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10465 == RECORD_TYPE)
10466 {
1767a056 10467 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10468 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
e0559d69 10469 if (TREE_CODE (fld_chain) == FIELD_DECL)
10470 break;
10471
10472 if (fld_chain)
10473 break;
10474 }
10475 /* Don't warn if the array might be considered a poor
10476 man's flexible array member with a very permissive
10477 definition thereof. */
10478 if (TREE_CODE (v) == ARRAY_REF
10479 || TREE_CODE (v) == COMPONENT_REF)
10480 warning (OPT_Warray_bounds,
10481 "index %E denotes an offset "
10482 "greater than size of %qT",
10483 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10484 }
10485 }
10486 }
7549df0d 10487
10488 t = convert (sizetype, t);
10489 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
af28855b 10490 break;
10491
ede90cc2 10492 case COMPOUND_EXPR:
10493 /* Handle static members of volatile structs. */
10494 t = TREE_OPERAND (expr, 1);
10495 gcc_assert (TREE_CODE (t) == VAR_DECL);
7549df0d 10496 return fold_offsetof_1 (t);
ede90cc2 10497
af28855b 10498 default:
231bd014 10499 gcc_unreachable ();
af28855b 10500 }
10501
7549df0d 10502 return fold_build_pointer_plus (base, off);
af28855b 10503}
10504
7549df0d 10505/* Likewise, but convert it to the return type of offsetof. */
10506
af28855b 10507tree
7549df0d 10508fold_offsetof (tree expr)
af28855b 10509{
7549df0d 10510 return convert (size_type_node, fold_offsetof_1 (expr));
af28855b 10511}
10512
b9bdfa0b 10513/* Warn for A ?: C expressions (with B omitted) where A is a boolean
10514 expression, because B will always be true. */
10515
10516void
10517warn_for_omitted_condop (location_t location, tree cond)
10518{
10519 if (truth_value_p (TREE_CODE (cond)))
10520 warning_at (location, OPT_Wparentheses,
10521 "the omitted middle operand in ?: will always be %<true%>, "
10522 "suggest explicit middle operand");
10523}
10524
a1f90215 10525/* Give an error for storing into ARG, which is 'const'. USE indicates
10526 how ARG was being used. */
10527
10528void
f2697631 10529readonly_error (location_t loc, tree arg, enum lvalue_use use)
a1f90215 10530{
10531 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10532 || use == lv_asm);
10533 /* Using this macro rather than (for example) arrays of messages
10534 ensures that all the format strings are checked at compile
10535 time. */
10536#define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10537 : (use == lv_increment ? (I) \
10538 : (use == lv_decrement ? (D) : (AS))))
10539 if (TREE_CODE (arg) == COMPONENT_REF)
10540 {
10541 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
f2697631 10542 error_at (loc, READONLY_MSG (G_("assignment of member "
10543 "%qD in read-only object"),
10544 G_("increment of member "
10545 "%qD in read-only object"),
10546 G_("decrement of member "
10547 "%qD in read-only object"),
10548 G_("member %qD in read-only object "
10549 "used as %<asm%> output")),
10550 TREE_OPERAND (arg, 1));
a1f90215 10551 else
f2697631 10552 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10553 G_("increment of read-only member %qD"),
10554 G_("decrement of read-only member %qD"),
10555 G_("read-only member %qD used as %<asm%> output")),
10556 TREE_OPERAND (arg, 1));
a1f90215 10557 }
10558 else if (TREE_CODE (arg) == VAR_DECL)
f2697631 10559 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10560 G_("increment of read-only variable %qD"),
10561 G_("decrement of read-only variable %qD"),
10562 G_("read-only variable %qD used as %<asm%> output")),
10563 arg);
a1f90215 10564 else if (TREE_CODE (arg) == PARM_DECL)
f2697631 10565 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10566 G_("increment of read-only parameter %qD"),
10567 G_("decrement of read-only parameter %qD"),
10568 G_("read-only parameter %qD use as %<asm%> output")),
10569 arg);
a1f90215 10570 else if (TREE_CODE (arg) == RESULT_DECL)
10571 {
10572 gcc_assert (c_dialect_cxx ());
f2697631 10573 error_at (loc, READONLY_MSG (G_("assignment of "
10574 "read-only named return value %qD"),
10575 G_("increment of "
10576 "read-only named return value %qD"),
10577 G_("decrement of "
10578 "read-only named return value %qD"),
10579 G_("read-only named return value %qD "
10580 "used as %<asm%>output")),
10581 arg);
a1f90215 10582 }
10583 else if (TREE_CODE (arg) == FUNCTION_DECL)
f2697631 10584 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10585 G_("increment of function %qD"),
10586 G_("decrement of function %qD"),
10587 G_("function %qD used as %<asm%> output")),
10588 arg);
a1f90215 10589 else
f2697631 10590 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10591 G_("increment of read-only location %qE"),
10592 G_("decrement of read-only location %qE"),
10593 G_("read-only location %qE used as %<asm%> output")),
10594 arg);
a1f90215 10595}
10596
e35976b1 10597/* Print an error message for an invalid lvalue. USE says
fdd84b77 10598 how the lvalue is being used and so selects the error message. LOC
10599 is the location for the error. */
ab6bb714 10600
e35976b1 10601void
fdd84b77 10602lvalue_error (location_t loc, enum lvalue_use use)
ab6bb714 10603{
e35976b1 10604 switch (use)
ab6bb714 10605 {
e35976b1 10606 case lv_assign:
fdd84b77 10607 error_at (loc, "lvalue required as left operand of assignment");
e35976b1 10608 break;
10609 case lv_increment:
fdd84b77 10610 error_at (loc, "lvalue required as increment operand");
e35976b1 10611 break;
10612 case lv_decrement:
fdd84b77 10613 error_at (loc, "lvalue required as decrement operand");
e35976b1 10614 break;
10615 case lv_addressof:
fdd84b77 10616 error_at (loc, "lvalue required as unary %<&%> operand");
e35976b1 10617 break;
10618 case lv_asm:
fdd84b77 10619 error_at (loc, "lvalue required in asm statement");
e35976b1 10620 break;
10621 default:
10622 gcc_unreachable ();
ab6bb714 10623 }
ab6bb714 10624}
b1bbc8e5 10625
10626/* Print an error message for an invalid indirection of type TYPE.
10627 ERRSTRING is the name of the operator for the indirection. */
10628
10629void
10630invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10631{
10632 switch (errstring)
10633 {
10634 case RO_NULL:
10635 gcc_assert (c_dialect_cxx ());
10636 error_at (loc, "invalid type argument (have %qT)", type);
10637 break;
10638 case RO_ARRAY_INDEXING:
10639 error_at (loc,
10640 "invalid type argument of array indexing (have %qT)",
10641 type);
10642 break;
10643 case RO_UNARY_STAR:
10644 error_at (loc,
10645 "invalid type argument of unary %<*%> (have %qT)",
10646 type);
10647 break;
10648 case RO_ARROW:
10649 error_at (loc,
10650 "invalid type argument of %<->%> (have %qT)",
10651 type);
10652 break;
7354a89b 10653 case RO_ARROW_STAR:
10654 error_at (loc,
10655 "invalid type argument of %<->*%> (have %qT)",
10656 type);
10657 break;
b1bbc8e5 10658 case RO_IMPLICIT_CONVERSION:
10659 error_at (loc,
10660 "invalid type argument of implicit conversion (have %qT)",
10661 type);
10662 break;
10663 default:
10664 gcc_unreachable ();
10665 }
10666}
c271bdb2 10667\f
10668/* *PTYPE is an incomplete array. Complete it with a domain based on
10669 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10670 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10671 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10672
10673int
10674complete_array_type (tree *ptype, tree initial_value, bool do_default)
10675{
10676 tree maxindex, type, main_type, elt, unqual_elt;
10677 int failure = 0, quals;
6753bca0 10678 hashval_t hashcode = 0;
f5298614 10679 bool overflow_p = false;
c271bdb2 10680
10681 maxindex = size_zero_node;
10682 if (initial_value)
10683 {
10684 if (TREE_CODE (initial_value) == STRING_CST)
10685 {
10686 int eltsize
10687 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10688 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10689 }
10690 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10691 {
f1f41a6c 10692 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
c271bdb2 10693
f1f41a6c 10694 if (vec_safe_is_empty (v))
c271bdb2 10695 {
10696 if (pedantic)
10697 failure = 3;
7542c3b4 10698 maxindex = ssize_int (-1);
c271bdb2 10699 }
10700 else
10701 {
10702 tree curindex;
c75b4594 10703 unsigned HOST_WIDE_INT cnt;
10704 constructor_elt *ce;
cee43f7e 10705 bool fold_p = false;
c271bdb2 10706
f1f41a6c 10707 if ((*v)[0].index)
f5298614 10708 maxindex = (*v)[0].index, fold_p = true;
10709
c271bdb2 10710 curindex = maxindex;
10711
f1f41a6c 10712 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
c271bdb2 10713 {
cee43f7e 10714 bool curfold_p = false;
c75b4594 10715 if (ce->index)
cee43f7e 10716 curindex = ce->index, curfold_p = true;
c271bdb2 10717 else
cee43f7e 10718 {
10719 if (fold_p)
f5298614 10720 {
10721 /* Since we treat size types now as ordinary
10722 unsigned types, we need an explicit overflow
10723 check. */
10724 tree orig = curindex;
10725 curindex = fold_convert (sizetype, curindex);
10726 overflow_p |= tree_int_cst_lt (curindex, orig);
10727 }
389dd41b 10728 curindex = size_binop (PLUS_EXPR, curindex,
10729 size_one_node);
cee43f7e 10730 }
c271bdb2 10731 if (tree_int_cst_lt (maxindex, curindex))
cee43f7e 10732 maxindex = curindex, fold_p = curfold_p;
c271bdb2 10733 }
f5298614 10734 if (fold_p)
10735 {
10736 tree orig = maxindex;
10737 maxindex = fold_convert (sizetype, maxindex);
10738 overflow_p |= tree_int_cst_lt (maxindex, orig);
10739 }
c271bdb2 10740 }
10741 }
10742 else
10743 {
10744 /* Make an error message unless that happened already. */
10745 if (initial_value != error_mark_node)
10746 failure = 1;
10747 }
10748 }
10749 else
10750 {
10751 failure = 2;
10752 if (!do_default)
10753 return failure;
10754 }
10755
10756 type = *ptype;
10757 elt = TREE_TYPE (type);
10758 quals = TYPE_QUALS (strip_array_types (elt));
10759 if (quals == 0)
10760 unqual_elt = elt;
10761 else
6d5d708e 10762 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
c271bdb2 10763
10764 /* Using build_distinct_type_copy and modifying things afterward instead
10765 of using build_array_type to create a new type preserves all of the
10766 TYPE_LANG_FLAG_? bits that the front end may have set. */
10767 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10768 TREE_TYPE (main_type) = unqual_elt;
783bb57e 10769 TYPE_DOMAIN (main_type)
10770 = build_range_type (TREE_TYPE (maxindex),
10771 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
c271bdb2 10772 layout_type (main_type);
10773
6753bca0 10774 /* Make sure we have the canonical MAIN_TYPE. */
10775 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
48e1416a 10776 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
6753bca0 10777 hashcode);
10778 main_type = type_hash_canon (hashcode, main_type);
10779
796735dc 10780 /* Fix the canonical type. */
10781 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10782 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10783 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10784 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10785 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10786 != TYPE_DOMAIN (main_type)))
48e1416a 10787 TYPE_CANONICAL (main_type)
796735dc 10788 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10789 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10790 else
10791 TYPE_CANONICAL (main_type) = main_type;
10792
c271bdb2 10793 if (quals == 0)
10794 type = main_type;
10795 else
10796 type = c_build_qualified_type (main_type, quals);
10797
4f5b8f2a 10798 if (COMPLETE_TYPE_P (type)
10799 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
f5298614 10800 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
4f5b8f2a 10801 {
10802 error ("size of array is too large");
10803 /* If we proceed with the array type as it is, we'll eventually
08f817b3 10804 crash in tree_to_[su]hwi(). */
4f5b8f2a 10805 type = error_mark_node;
10806 }
10807
c271bdb2 10808 *ptype = type;
10809 return failure;
10810}
ab6bb714 10811
93426222 10812/* Like c_mark_addressable but don't check register qualifier. */
10813void
10814c_common_mark_addressable_vec (tree t)
10815{
10816 while (handled_component_p (t))
10817 t = TREE_OPERAND (t, 0);
10818 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10819 return;
10820 TREE_ADDRESSABLE (t) = 1;
10821}
10822
10823
b6a5fc45 10824\f
10825/* Used to help initialize the builtin-types.def table. When a type of
10826 the correct size doesn't exist, use error_mark_node instead of NULL.
10827 The later results in segfaults even when a decl using the type doesn't
10828 get invoked. */
10829
10830tree
10831builtin_type_for_size (int size, bool unsignedp)
10832{
a51edb4c 10833 tree type = c_common_type_for_size (size, unsignedp);
b6a5fc45 10834 return type ? type : error_mark_node;
10835}
10836
10837/* A helper function for resolve_overloaded_builtin in resolving the
10838 overloaded __sync_ builtins. Returns a positive power of 2 if the
10839 first operand of PARAMS is a pointer to a supported data type.
10840 Returns 0 if an error is encountered. */
10841
10842static int
f1f41a6c 10843sync_resolve_size (tree function, vec<tree, va_gc> *params)
b6a5fc45 10844{
10845 tree type;
10846 int size;
10847
f1f41a6c 10848 if (!params)
b6a5fc45 10849 {
10850 error ("too few arguments to function %qE", function);
10851 return 0;
10852 }
10853
f1f41a6c 10854 type = TREE_TYPE ((*params)[0]);
0d284870 10855 if (TREE_CODE (type) == ARRAY_TYPE)
10856 {
10857 /* Force array-to-pointer decay for C++. */
10858 gcc_assert (c_dialect_cxx());
10859 (*params)[0] = default_conversion ((*params)[0]);
10860 type = TREE_TYPE ((*params)[0]);
10861 }
b6a5fc45 10862 if (TREE_CODE (type) != POINTER_TYPE)
10863 goto incompatible;
10864
10865 type = TREE_TYPE (type);
10866 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10867 goto incompatible;
10868
e913b5cd 10869 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
27213ba3 10870 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
b6a5fc45 10871 return size;
10872
10873 incompatible:
10874 error ("incompatible type for argument %d of %qE", 1, function);
10875 return 0;
10876}
10877
a0c938f0 10878/* A helper function for resolve_overloaded_builtin. Adds casts to
b6a5fc45 10879 PARAMS to make arguments match up with those of FUNCTION. Drops
10880 the variadic arguments at the end. Returns false if some error
10881 was encountered; true on success. */
10882
10883static bool
1cd6e20d 10884sync_resolve_params (location_t loc, tree orig_function, tree function,
f1f41a6c 10885 vec<tree, va_gc> *params, bool orig_format)
b6a5fc45 10886{
d0af78c5 10887 function_args_iterator iter;
b6a5fc45 10888 tree ptype;
b9c74b4d 10889 unsigned int parmnum;
b6a5fc45 10890
d0af78c5 10891 function_args_iter_init (&iter, TREE_TYPE (function));
b6a5fc45 10892 /* We've declared the implementation functions to use "volatile void *"
10893 as the pointer parameter, so we shouldn't get any complaints from the
10894 call to check_function_arguments what ever type the user used. */
d0af78c5 10895 function_args_iter_next (&iter);
f1f41a6c 10896 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
b560fabd 10897 ptype = TYPE_MAIN_VARIANT (ptype);
b6a5fc45 10898
10899 /* For the rest of the values, we need to cast these to FTYPE, so that we
10900 don't get warnings for passing pointer types, etc. */
b9c74b4d 10901 parmnum = 0;
d0af78c5 10902 while (1)
b6a5fc45 10903 {
d0af78c5 10904 tree val, arg_type;
10905
10906 arg_type = function_args_iter_cond (&iter);
10907 /* XXX void_type_node belies the abstraction. */
10908 if (arg_type == void_type_node)
10909 break;
b6a5fc45 10910
b9c74b4d 10911 ++parmnum;
f1f41a6c 10912 if (params->length () <= parmnum)
b6a5fc45 10913 {
1cd6e20d 10914 error_at (loc, "too few arguments to function %qE", orig_function);
b6a5fc45 10915 return false;
10916 }
10917
0f6a7cb7 10918 /* Only convert parameters if arg_type is unsigned integer type with
10919 new format sync routines, i.e. don't attempt to convert pointer
10920 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10921 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10922 kinds). */
10923 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
1cd6e20d 10924 {
10925 /* Ideally for the first conversion we'd use convert_for_assignment
10926 so that we get warnings for anything that doesn't match the pointer
10927 type. This isn't portable across the C and C++ front ends atm. */
f1f41a6c 10928 val = (*params)[parmnum];
1cd6e20d 10929 val = convert (ptype, val);
10930 val = convert (arg_type, val);
f1f41a6c 10931 (*params)[parmnum] = val;
1cd6e20d 10932 }
b6a5fc45 10933
d0af78c5 10934 function_args_iter_next (&iter);
b6a5fc45 10935 }
10936
1cd6e20d 10937 /* __atomic routines are not variadic. */
f1f41a6c 10938 if (!orig_format && params->length () != parmnum + 1)
1cd6e20d 10939 {
10940 error_at (loc, "too many arguments to function %qE", orig_function);
10941 return false;
10942 }
10943
b6a5fc45 10944 /* The definition of these primitives is variadic, with the remaining
10945 being "an optional list of variables protected by the memory barrier".
10946 No clue what that's supposed to mean, precisely, but we consider all
10947 call-clobbered variables to be protected so we're safe. */
f1f41a6c 10948 params->truncate (parmnum + 1);
b6a5fc45 10949
10950 return true;
10951}
10952
a0c938f0 10953/* A helper function for resolve_overloaded_builtin. Adds a cast to
b6a5fc45 10954 RESULT to make it match the type of the first pointer argument in
10955 PARAMS. */
10956
10957static tree
1cd6e20d 10958sync_resolve_return (tree first_param, tree result, bool orig_format)
b6a5fc45 10959{
b9c74b4d 10960 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
1cd6e20d 10961 tree rtype = TREE_TYPE (result);
10080eac 10962 ptype = TYPE_MAIN_VARIANT (ptype);
1cd6e20d 10963
10964 /* New format doesn't require casting unless the types are the same size. */
10965 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10966 return convert (ptype, result);
10967 else
10968 return result;
10969}
10970
10971/* This function verifies the PARAMS to generic atomic FUNCTION.
10972 It returns the size if all the parameters are the same size, otherwise
10973 0 is returned if the parameters are invalid. */
10974
10975static int
f1f41a6c 10976get_atomic_generic_size (location_t loc, tree function,
10977 vec<tree, va_gc> *params)
1cd6e20d 10978{
10979 unsigned int n_param;
10980 unsigned int n_model;
10981 unsigned int x;
10982 int size_0;
10983 tree type_0;
10984
10985 /* Determine the parameter makeup. */
10986 switch (DECL_FUNCTION_CODE (function))
10987 {
10988 case BUILT_IN_ATOMIC_EXCHANGE:
10989 n_param = 4;
10990 n_model = 1;
10991 break;
10992 case BUILT_IN_ATOMIC_LOAD:
10993 case BUILT_IN_ATOMIC_STORE:
10994 n_param = 3;
10995 n_model = 1;
10996 break;
10997 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10998 n_param = 6;
10999 n_model = 2;
11000 break;
11001 default:
1d581089 11002 gcc_unreachable ();
1cd6e20d 11003 }
11004
f1f41a6c 11005 if (vec_safe_length (params) != n_param)
1cd6e20d 11006 {
11007 error_at (loc, "incorrect number of arguments to function %qE", function);
11008 return 0;
11009 }
11010
11011 /* Get type of first parameter, and determine its size. */
f1f41a6c 11012 type_0 = TREE_TYPE ((*params)[0]);
0d284870 11013 if (TREE_CODE (type_0) == ARRAY_TYPE)
11014 {
11015 /* Force array-to-pointer decay for C++. */
11016 gcc_assert (c_dialect_cxx());
11017 (*params)[0] = default_conversion ((*params)[0]);
11018 type_0 = TREE_TYPE ((*params)[0]);
11019 }
1d581089 11020 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
11021 {
11022 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
11023 function);
11024 return 0;
11025 }
11026
11027 /* Types must be compile time constant sizes. */
11028 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
1cd6e20d 11029 {
1d581089 11030 error_at (loc,
11031 "argument 1 of %qE must be a pointer to a constant size type",
11032 function);
1cd6e20d 11033 return 0;
11034 }
1d581089 11035
e913b5cd 11036 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
1cd6e20d 11037
1d581089 11038 /* Zero size objects are not allowed. */
11039 if (size_0 == 0)
11040 {
11041 error_at (loc,
11042 "argument 1 of %qE must be a pointer to a nonzero size object",
11043 function);
11044 return 0;
11045 }
11046
1cd6e20d 11047 /* Check each other parameter is a pointer and the same size. */
11048 for (x = 0; x < n_param - n_model; x++)
11049 {
11050 int size;
f1f41a6c 11051 tree type = TREE_TYPE ((*params)[x]);
a04e8d62 11052 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
1cd6e20d 11053 if (n_param == 6 && x == 3)
11054 continue;
11055 if (!POINTER_TYPE_P (type))
11056 {
11057 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
11058 function);
11059 return 0;
11060 }
1f6be080 11061 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
11062 size = type_size ? tree_to_uhwi (type_size) : 0;
1cd6e20d 11063 if (size != size_0)
11064 {
11065 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
11066 function);
11067 return 0;
11068 }
11069 }
11070
11071 /* Check memory model parameters for validity. */
11072 for (x = n_param - n_model ; x < n_param; x++)
11073 {
f1f41a6c 11074 tree p = (*params)[x];
1cd6e20d 11075 if (TREE_CODE (p) == INTEGER_CST)
11076 {
e913b5cd 11077 int i = tree_to_uhwi (p);
a372f7ca 11078 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
1cd6e20d 11079 {
11080 warning_at (loc, OPT_Winvalid_memory_model,
11081 "invalid memory model argument %d of %qE", x + 1,
11082 function);
1cd6e20d 11083 }
11084 }
11085 else
11086 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
11087 {
11088 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
11089 function);
11090 return 0;
11091 }
11092 }
11093
11094 return size_0;
11095}
11096
11097
11098/* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
11099 at the beginning of the parameter list PARAMS representing the size of the
11100 objects. This is to match the library ABI requirement. LOC is the location
11101 of the function call.
11102 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
11103 returned to allow the external call to be constructed. */
11104
11105static tree
11106add_atomic_size_parameter (unsigned n, location_t loc, tree function,
f1f41a6c 11107 vec<tree, va_gc> *params)
1cd6e20d 11108{
11109 tree size_node;
11110
11111 /* Insert a SIZE_T parameter as the first param. If there isn't
11112 enough space, allocate a new vector and recursively re-build with that. */
f1f41a6c 11113 if (!params->space (1))
1cd6e20d 11114 {
11115 unsigned int z, len;
f1f41a6c 11116 vec<tree, va_gc> *v;
1cd6e20d 11117 tree f;
11118
f1f41a6c 11119 len = params->length ();
11120 vec_alloc (v, len + 1);
5a672e62 11121 v->quick_push (build_int_cst (size_type_node, n));
1cd6e20d 11122 for (z = 0; z < len; z++)
f1f41a6c 11123 v->quick_push ((*params)[z]);
ec761d5a 11124 f = build_function_call_vec (loc, vNULL, function, v, NULL);
f1f41a6c 11125 vec_free (v);
1cd6e20d 11126 return f;
11127 }
11128
11129 /* Add the size parameter and leave as a function call for processing. */
11130 size_node = build_int_cst (size_type_node, n);
f1f41a6c 11131 params->quick_insert (0, size_node);
1cd6e20d 11132 return NULL_TREE;
11133}
11134
11135
a056826c 11136/* Return whether atomic operations for naturally aligned N-byte
11137 arguments are supported, whether inline or through libatomic. */
11138static bool
11139atomic_size_supported_p (int n)
11140{
11141 switch (n)
11142 {
11143 case 1:
11144 case 2:
11145 case 4:
11146 case 8:
11147 return true;
11148
11149 case 16:
11150 return targetm.scalar_mode_supported_p (TImode);
11151
11152 default:
11153 return false;
11154 }
11155}
11156
1cd6e20d 11157/* This will process an __atomic_exchange function call, determine whether it
11158 needs to be mapped to the _N variation, or turned into a library call.
11159 LOC is the location of the builtin call.
11160 FUNCTION is the DECL that has been invoked;
11161 PARAMS is the argument list for the call. The return value is non-null
11162 TRUE is returned if it is translated into the proper format for a call to the
11163 external library, and NEW_RETURN is set the tree for that function.
11164 FALSE is returned if processing for the _N variation is required, and
11165 NEW_RETURN is set to the the return value the result is copied into. */
11166static bool
11167resolve_overloaded_atomic_exchange (location_t loc, tree function,
f1f41a6c 11168 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11169{
11170 tree p0, p1, p2, p3;
11171 tree I_type, I_type_ptr;
11172 int n = get_atomic_generic_size (loc, function, params);
11173
1d581089 11174 /* Size of 0 is an error condition. */
11175 if (n == 0)
11176 {
11177 *new_return = error_mark_node;
11178 return true;
11179 }
11180
1cd6e20d 11181 /* If not a lock-free size, change to the library generic format. */
a056826c 11182 if (!atomic_size_supported_p (n))
1cd6e20d 11183 {
11184 *new_return = add_atomic_size_parameter (n, loc, function, params);
11185 return true;
11186 }
11187
11188 /* Otherwise there is a lockfree match, transform the call from:
11189 void fn(T* mem, T* desired, T* return, model)
11190 into
11191 *return = (T) (fn (In* mem, (In) *desired, model)) */
11192
f1f41a6c 11193 p0 = (*params)[0];
11194 p1 = (*params)[1];
11195 p2 = (*params)[2];
11196 p3 = (*params)[3];
1cd6e20d 11197
11198 /* Create pointer to appropriate size. */
11199 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11200 I_type_ptr = build_pointer_type (I_type);
11201
11202 /* Convert object pointer to required type. */
11203 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11204 (*params)[0] = p0;
1cd6e20d 11205 /* Convert new value to required type, and dereference it. */
11206 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11207 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 11208 (*params)[1] = p1;
1cd6e20d 11209
11210 /* Move memory model to the 3rd position, and end param list. */
f1f41a6c 11211 (*params)[2] = p3;
11212 params->truncate (3);
1cd6e20d 11213
11214 /* Convert return pointer and dereference it for later assignment. */
11215 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11216
11217 return false;
b6a5fc45 11218}
11219
1cd6e20d 11220
11221/* This will process an __atomic_compare_exchange function call, determine
11222 whether it needs to be mapped to the _N variation, or turned into a lib call.
11223 LOC is the location of the builtin call.
11224 FUNCTION is the DECL that has been invoked;
11225 PARAMS is the argument list for the call. The return value is non-null
11226 TRUE is returned if it is translated into the proper format for a call to the
11227 external library, and NEW_RETURN is set the tree for that function.
11228 FALSE is returned if processing for the _N variation is required. */
11229
11230static bool
11231resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
f1f41a6c 11232 vec<tree, va_gc> *params,
1cd6e20d 11233 tree *new_return)
11234{
11235 tree p0, p1, p2;
11236 tree I_type, I_type_ptr;
11237 int n = get_atomic_generic_size (loc, function, params);
11238
1d581089 11239 /* Size of 0 is an error condition. */
11240 if (n == 0)
11241 {
11242 *new_return = error_mark_node;
11243 return true;
11244 }
11245
1cd6e20d 11246 /* If not a lock-free size, change to the library generic format. */
a056826c 11247 if (!atomic_size_supported_p (n))
1cd6e20d 11248 {
11249 /* The library generic format does not have the weak parameter, so
11250 remove it from the param list. Since a parameter has been removed,
11251 we can be sure that there is room for the SIZE_T parameter, meaning
11252 there will not be a recursive rebuilding of the parameter list, so
11253 there is no danger this will be done twice. */
11254 if (n > 0)
11255 {
f1f41a6c 11256 (*params)[3] = (*params)[4];
11257 (*params)[4] = (*params)[5];
11258 params->truncate (5);
1cd6e20d 11259 }
11260 *new_return = add_atomic_size_parameter (n, loc, function, params);
11261 return true;
11262 }
11263
11264 /* Otherwise, there is a match, so the call needs to be transformed from:
11265 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11266 into
11267 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
ab2c1de8 11268
f1f41a6c 11269 p0 = (*params)[0];
11270 p1 = (*params)[1];
11271 p2 = (*params)[2];
1cd6e20d 11272
11273 /* Create pointer to appropriate size. */
11274 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11275 I_type_ptr = build_pointer_type (I_type);
11276
11277 /* Convert object pointer to required type. */
11278 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11279 (*params)[0] = p0;
1cd6e20d 11280
11281 /* Convert expected pointer to required type. */
11282 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
f1f41a6c 11283 (*params)[1] = p1;
1cd6e20d 11284
11285 /* Convert desired value to required type, and dereference it. */
11286 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11287 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
f1f41a6c 11288 (*params)[2] = p2;
1cd6e20d 11289
11290 /* The rest of the parameters are fine. NULL means no special return value
11291 processing.*/
11292 *new_return = NULL;
11293 return false;
11294}
11295
11296
11297/* This will process an __atomic_load function call, determine whether it
11298 needs to be mapped to the _N variation, or turned into a library call.
11299 LOC is the location of the builtin call.
11300 FUNCTION is the DECL that has been invoked;
11301 PARAMS is the argument list for the call. The return value is non-null
11302 TRUE is returned if it is translated into the proper format for a call to the
11303 external library, and NEW_RETURN is set the tree for that function.
11304 FALSE is returned if processing for the _N variation is required, and
11305 NEW_RETURN is set to the the return value the result is copied into. */
11306
11307static bool
11308resolve_overloaded_atomic_load (location_t loc, tree function,
f1f41a6c 11309 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11310{
11311 tree p0, p1, p2;
11312 tree I_type, I_type_ptr;
11313 int n = get_atomic_generic_size (loc, function, params);
11314
1d581089 11315 /* Size of 0 is an error condition. */
11316 if (n == 0)
11317 {
11318 *new_return = error_mark_node;
11319 return true;
11320 }
11321
1cd6e20d 11322 /* If not a lock-free size, change to the library generic format. */
a056826c 11323 if (!atomic_size_supported_p (n))
1cd6e20d 11324 {
11325 *new_return = add_atomic_size_parameter (n, loc, function, params);
11326 return true;
11327 }
11328
11329 /* Otherwise, there is a match, so the call needs to be transformed from:
11330 void fn(T* mem, T* return, model)
11331 into
11332 *return = (T) (fn ((In *) mem, model)) */
11333
f1f41a6c 11334 p0 = (*params)[0];
11335 p1 = (*params)[1];
11336 p2 = (*params)[2];
1cd6e20d 11337
11338 /* Create pointer to appropriate size. */
11339 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11340 I_type_ptr = build_pointer_type (I_type);
11341
11342 /* Convert object pointer to required type. */
11343 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11344 (*params)[0] = p0;
1cd6e20d 11345
11346 /* Move memory model to the 2nd position, and end param list. */
f1f41a6c 11347 (*params)[1] = p2;
11348 params->truncate (2);
1cd6e20d 11349
11350 /* Convert return pointer and dereference it for later assignment. */
11351 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11352
11353 return false;
11354}
11355
11356
11357/* This will process an __atomic_store function call, determine whether it
11358 needs to be mapped to the _N variation, or turned into a library call.
11359 LOC is the location of the builtin call.
11360 FUNCTION is the DECL that has been invoked;
11361 PARAMS is the argument list for the call. The return value is non-null
11362 TRUE is returned if it is translated into the proper format for a call to the
11363 external library, and NEW_RETURN is set the tree for that function.
11364 FALSE is returned if processing for the _N variation is required, and
11365 NEW_RETURN is set to the the return value the result is copied into. */
11366
11367static bool
11368resolve_overloaded_atomic_store (location_t loc, tree function,
f1f41a6c 11369 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11370{
11371 tree p0, p1;
11372 tree I_type, I_type_ptr;
11373 int n = get_atomic_generic_size (loc, function, params);
11374
1d581089 11375 /* Size of 0 is an error condition. */
11376 if (n == 0)
11377 {
11378 *new_return = error_mark_node;
11379 return true;
11380 }
11381
1cd6e20d 11382 /* If not a lock-free size, change to the library generic format. */
a056826c 11383 if (!atomic_size_supported_p (n))
1cd6e20d 11384 {
11385 *new_return = add_atomic_size_parameter (n, loc, function, params);
11386 return true;
11387 }
11388
11389 /* Otherwise, there is a match, so the call needs to be transformed from:
11390 void fn(T* mem, T* value, model)
11391 into
11392 fn ((In *) mem, (In) *value, model) */
11393
f1f41a6c 11394 p0 = (*params)[0];
11395 p1 = (*params)[1];
1cd6e20d 11396
11397 /* Create pointer to appropriate size. */
11398 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11399 I_type_ptr = build_pointer_type (I_type);
11400
11401 /* Convert object pointer to required type. */
11402 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11403 (*params)[0] = p0;
1cd6e20d 11404
11405 /* Convert new value to required type, and dereference it. */
11406 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11407 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 11408 (*params)[1] = p1;
1cd6e20d 11409
11410 /* The memory model is in the right spot already. Return is void. */
11411 *new_return = NULL_TREE;
11412
11413 return false;
11414}
11415
11416
b6a5fc45 11417/* Some builtin functions are placeholders for other expressions. This
11418 function should be called immediately after parsing the call expression
11419 before surrounding code has committed to the type of the expression.
11420
e60a6f7b 11421 LOC is the location of the builtin call.
11422
b6a5fc45 11423 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11424 PARAMS is the argument list for the call. The return value is non-null
11425 when expansion is complete, and null if normal processing should
11426 continue. */
11427
11428tree
f1f41a6c 11429resolve_overloaded_builtin (location_t loc, tree function,
11430 vec<tree, va_gc> *params)
b6a5fc45 11431{
11432 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
1cd6e20d 11433 bool orig_format = true;
11434 tree new_return = NULL_TREE;
11435
65441f6f 11436 switch (DECL_BUILT_IN_CLASS (function))
11437 {
11438 case BUILT_IN_NORMAL:
11439 break;
11440 case BUILT_IN_MD:
11441 if (targetm.resolve_overloaded_builtin)
e60a6f7b 11442 return targetm.resolve_overloaded_builtin (loc, function, params);
65441f6f 11443 else
a0c938f0 11444 return NULL_TREE;
65441f6f 11445 default:
11446 return NULL_TREE;
11447 }
a0c938f0 11448
65441f6f 11449 /* Handle BUILT_IN_NORMAL here. */
b6a5fc45 11450 switch (orig_code)
11451 {
1cd6e20d 11452 case BUILT_IN_ATOMIC_EXCHANGE:
11453 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11454 case BUILT_IN_ATOMIC_LOAD:
11455 case BUILT_IN_ATOMIC_STORE:
11456 {
11457 /* Handle these 4 together so that they can fall through to the next
11458 case if the call is transformed to an _N variant. */
11459 switch (orig_code)
11460 {
11461 case BUILT_IN_ATOMIC_EXCHANGE:
11462 {
11463 if (resolve_overloaded_atomic_exchange (loc, function, params,
11464 &new_return))
11465 return new_return;
11466 /* Change to the _N variant. */
11467 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11468 break;
11469 }
11470
11471 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11472 {
11473 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11474 params,
11475 &new_return))
11476 return new_return;
11477 /* Change to the _N variant. */
11478 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11479 break;
11480 }
11481 case BUILT_IN_ATOMIC_LOAD:
11482 {
11483 if (resolve_overloaded_atomic_load (loc, function, params,
11484 &new_return))
11485 return new_return;
11486 /* Change to the _N variant. */
11487 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11488 break;
11489 }
11490 case BUILT_IN_ATOMIC_STORE:
11491 {
11492 if (resolve_overloaded_atomic_store (loc, function, params,
11493 &new_return))
11494 return new_return;
11495 /* Change to the _N variant. */
11496 orig_code = BUILT_IN_ATOMIC_STORE_N;
11497 break;
11498 }
11499 default:
11500 gcc_unreachable ();
11501 }
11502 /* Fallthrough to the normal processing. */
11503 }
11504 case BUILT_IN_ATOMIC_EXCHANGE_N:
11505 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11506 case BUILT_IN_ATOMIC_LOAD_N:
11507 case BUILT_IN_ATOMIC_STORE_N:
11508 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11509 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11510 case BUILT_IN_ATOMIC_AND_FETCH_N:
11511 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11512 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11513 case BUILT_IN_ATOMIC_OR_FETCH_N:
11514 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11515 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11516 case BUILT_IN_ATOMIC_FETCH_AND_N:
11517 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11518 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11519 case BUILT_IN_ATOMIC_FETCH_OR_N:
11520 {
11521 orig_format = false;
11522 /* Fallthru for parameter processing. */
11523 }
2797f13a 11524 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11525 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11526 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11527 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11528 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11529 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11530 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11531 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11532 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11533 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11534 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11535 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11536 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11537 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11538 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11539 case BUILT_IN_SYNC_LOCK_RELEASE_N:
b6a5fc45 11540 {
11541 int n = sync_resolve_size (function, params);
b9c74b4d 11542 tree new_function, first_param, result;
b9a16870 11543 enum built_in_function fncode;
b6a5fc45 11544
11545 if (n == 0)
11546 return error_mark_node;
11547
b9a16870 11548 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11549 new_function = builtin_decl_explicit (fncode);
1cd6e20d 11550 if (!sync_resolve_params (loc, function, new_function, params,
11551 orig_format))
b6a5fc45 11552 return error_mark_node;
11553
f1f41a6c 11554 first_param = (*params)[0];
ec761d5a 11555 result = build_function_call_vec (loc, vNULL, new_function, params,
11556 NULL);
1cd6e20d 11557 if (result == error_mark_node)
11558 return result;
2797f13a 11559 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
1cd6e20d 11560 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11561 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11562 result = sync_resolve_return (first_param, result, orig_format);
b6a5fc45 11563
1cd6e20d 11564 /* If new_return is set, assign function to that expr and cast the
11565 result to void since the generic interface returned void. */
11566 if (new_return)
11567 {
11568 /* Cast function result from I{1,2,4,8,16} to the required type. */
11569 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11570 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11571 result);
11572 TREE_SIDE_EFFECTS (result) = 1;
11573 protected_set_expr_location (result, loc);
11574 result = convert (void_type_node, result);
11575 }
b6a5fc45 11576 return result;
11577 }
11578
11579 default:
65441f6f 11580 return NULL_TREE;
b6a5fc45 11581 }
11582}
11583
73437615 11584/* vector_types_compatible_elements_p is used in type checks of vectors
11585 values used as operands of binary operators. Where it returns true, and
11586 the other checks of the caller succeed (being vector types in he first
11587 place, and matching number of elements), we can just treat the types
11588 as essentially the same.
11589 Contrast with vector_targets_convertible_p, which is used for vector
11590 pointer types, and vector_types_convertible_p, which will allow
11591 language-specific matches under the control of flag_lax_vector_conversions,
11592 and might still require a conversion. */
11593/* True if vector types T1 and T2 can be inputs to the same binary
11594 operator without conversion.
11595 We don't check the overall vector size here because some of our callers
11596 want to give different error messages when the vectors are compatible
11597 except for the element count. */
11598
491255f5 11599bool
73437615 11600vector_types_compatible_elements_p (tree t1, tree t2)
491255f5 11601{
73437615 11602 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11603 t1 = TREE_TYPE (t1);
11604 t2 = TREE_TYPE (t2);
11605
491255f5 11606 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11607
9421ebb9 11608 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11609 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11610 || c2 == FIXED_POINT_TYPE));
491255f5 11611
73437615 11612 t1 = c_common_signed_type (t1);
11613 t2 = c_common_signed_type (t2);
491255f5 11614 /* Equality works here because c_common_signed_type uses
11615 TYPE_MAIN_VARIANT. */
73437615 11616 if (t1 == t2)
11617 return true;
11618 if (opaque && c1 == c2
11619 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11620 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11621 return true;
11622 return false;
491255f5 11623}
11624
be7350e7 11625/* Check for missing format attributes on function pointers. LTYPE is
11626 the new type or left-hand side type. RTYPE is the old type or
11627 right-hand side type. Returns TRUE if LTYPE is missing the desired
11628 attribute. */
11629
11630bool
11631check_missing_format_attribute (tree ltype, tree rtype)
11632{
11633 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11634 tree ra;
11635
11636 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11637 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11638 break;
11639 if (ra)
11640 {
11641 tree la;
11642 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11643 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11644 break;
11645 return !la;
11646 }
11647 else
11648 return false;
11649}
11650
2840aae4 11651/* Subscripting with type char is likely to lose on a machine where
11652 chars are signed. So warn on any machine, but optionally. Don't
11653 warn for unsigned char since that type is safe. Don't warn for
11654 signed char because anyone who uses that must have done so
11655 deliberately. Furthermore, we reduce the false positive load by
11656 warning only for non-constant value of type char. */
11657
11658void
92b63884 11659warn_array_subscript_with_type_char (location_t loc, tree index)
2840aae4 11660{
11661 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11662 && TREE_CODE (index) != INTEGER_CST)
92b63884 11663 warning_at (loc, OPT_Wchar_subscripts,
11664 "array subscript has type %<char%>");
2840aae4 11665}
11666
e534436e 11667/* Implement -Wparentheses for the unexpected C precedence rules, to
11668 cover cases like x + y << z which readers are likely to
11669 misinterpret. We have seen an expression in which CODE is a binary
82012ffe 11670 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11671 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11672 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11673 expression was not formed using a binary or unary operator, or it
11674 was enclosed in parentheses. */
e534436e 11675
11676void
b0e7825e 11677warn_about_parentheses (location_t loc, enum tree_code code,
269f7979 11678 enum tree_code code_left, tree arg_left,
82012ffe 11679 enum tree_code code_right, tree arg_right)
e534436e 11680{
11681 if (!warn_parentheses)
11682 return;
11683
82012ffe 11684 /* This macro tests that the expression ARG with original tree code
11685 CODE appears to be a boolean expression. or the result of folding a
11686 boolean expression. */
11687#define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11688 (truth_value_p (TREE_CODE (ARG)) \
11689 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11690 /* Folding may create 0 or 1 integers from other expressions. */ \
11691 || ((CODE) != INTEGER_CST \
11692 && (integer_onep (ARG) || integer_zerop (ARG))))
11693
48e1416a 11694 switch (code)
e534436e 11695 {
82012ffe 11696 case LSHIFT_EXPR:
b0e7825e 11697 if (code_left == PLUS_EXPR)
11698 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11699 "suggest parentheses around %<+%> inside %<<<%>");
11700 else if (code_right == PLUS_EXPR)
11701 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11702 "suggest parentheses around %<+%> inside %<<<%>");
11703 else if (code_left == MINUS_EXPR)
11704 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11705 "suggest parentheses around %<-%> inside %<<<%>");
11706 else if (code_right == MINUS_EXPR)
11707 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11708 "suggest parentheses around %<-%> inside %<<<%>");
82012ffe 11709 return;
e534436e 11710
82012ffe 11711 case RSHIFT_EXPR:
b0e7825e 11712 if (code_left == PLUS_EXPR)
11713 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11714 "suggest parentheses around %<+%> inside %<>>%>");
11715 else if (code_right == PLUS_EXPR)
11716 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11717 "suggest parentheses around %<+%> inside %<>>%>");
11718 else if (code_left == MINUS_EXPR)
11719 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11720 "suggest parentheses around %<-%> inside %<>>%>");
11721 else if (code_right == MINUS_EXPR)
11722 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11723 "suggest parentheses around %<-%> inside %<>>%>");
82012ffe 11724 return;
e534436e 11725
82012ffe 11726 case TRUTH_ORIF_EXPR:
b0e7825e 11727 if (code_left == TRUTH_ANDIF_EXPR)
11728 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11729 "suggest parentheses around %<&&%> within %<||%>");
11730 else if (code_right == TRUTH_ANDIF_EXPR)
11731 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11732 "suggest parentheses around %<&&%> within %<||%>");
82012ffe 11733 return;
11734
11735 case BIT_IOR_EXPR:
e534436e 11736 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
b0e7825e 11737 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11738 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11739 "suggest parentheses around arithmetic in operand of %<|%>");
11740 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11741 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11742 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11743 "suggest parentheses around arithmetic in operand of %<|%>");
e534436e 11744 /* Check cases like x|y==z */
b0e7825e 11745 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11746 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11747 "suggest parentheses around comparison in operand of %<|%>");
11748 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11749 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11750 "suggest parentheses around comparison in operand of %<|%>");
11751 /* Check cases like !x | y */
11752 else if (code_left == TRUTH_NOT_EXPR
11753 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 11754 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11755 "suggest parentheses around operand of "
11756 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
82012ffe 11757 return;
e534436e 11758
82012ffe 11759 case BIT_XOR_EXPR:
e534436e 11760 if (code_left == BIT_AND_EXPR
b0e7825e 11761 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11762 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11763 "suggest parentheses around arithmetic in operand of %<^%>");
11764 else if (code_right == BIT_AND_EXPR
11765 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11766 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11767 "suggest parentheses around arithmetic in operand of %<^%>");
e534436e 11768 /* Check cases like x^y==z */
b0e7825e 11769 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11770 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11771 "suggest parentheses around comparison in operand of %<^%>");
11772 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11773 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11774 "suggest parentheses around comparison in operand of %<^%>");
11775 return;
e534436e 11776
82012ffe 11777 case BIT_AND_EXPR:
b0e7825e 11778 if (code_left == PLUS_EXPR)
11779 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11780 "suggest parentheses around %<+%> in operand of %<&%>");
11781 else if (code_right == PLUS_EXPR)
11782 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11783 "suggest parentheses around %<+%> in operand of %<&%>");
b0e7825e 11784 else if (code_left == MINUS_EXPR)
11785 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11786 "suggest parentheses around %<-%> in operand of %<&%>");
11787 else if (code_right == MINUS_EXPR)
11788 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11789 "suggest parentheses around %<-%> in operand of %<&%>");
e534436e 11790 /* Check cases like x&y==z */
b0e7825e 11791 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11792 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11793 "suggest parentheses around comparison in operand of %<&%>");
11794 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11795 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11796 "suggest parentheses around comparison in operand of %<&%>");
11797 /* Check cases like !x & y */
11798 else if (code_left == TRUTH_NOT_EXPR
11799 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 11800 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11801 "suggest parentheses around operand of "
11802 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
82012ffe 11803 return;
e534436e 11804
82012ffe 11805 case EQ_EXPR:
b0e7825e 11806 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11807 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11808 "suggest parentheses around comparison in operand of %<==%>");
11809 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11810 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11811 "suggest parentheses around comparison in operand of %<==%>");
11812 return;
11813 case NE_EXPR:
b0e7825e 11814 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11815 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11816 "suggest parentheses around comparison in operand of %<!=%>");
11817 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11818 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11819 "suggest parentheses around comparison in operand of %<!=%>");
11820 return;
11821
11822 default:
b0e7825e 11823 if (TREE_CODE_CLASS (code) == tcc_comparison)
11824 {
11825 if (TREE_CODE_CLASS (code_left) == tcc_comparison
269f7979 11826 && code_left != NE_EXPR && code_left != EQ_EXPR
11827 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
b0e7825e 11828 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11829 "comparisons like %<X<=Y<=Z%> do not "
11830 "have their mathematical meaning");
11831 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
269f7979 11832 && code_right != NE_EXPR && code_right != EQ_EXPR
b0e7825e 11833 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11834 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11835 "comparisons like %<X<=Y<=Z%> do not "
11836 "have their mathematical meaning");
11837 }
82012ffe 11838 return;
6ce0c450 11839 }
82012ffe 11840#undef NOT_A_BOOLEAN_EXPR_P
e534436e 11841}
11842
92fccaaa 11843/* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11844
11845void
11846warn_for_unused_label (tree label)
11847{
11848 if (!TREE_USED (label))
11849 {
11850 if (DECL_INITIAL (label))
11851 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11852 else
11853 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11854 }
11855}
2840aae4 11856
b6889cb0 11857/* Warn for division by zero according to the value of DIVISOR. LOC
11858 is the location of the division operator. */
f092582b 11859
11860void
b6889cb0 11861warn_for_div_by_zero (location_t loc, tree divisor)
f092582b 11862{
9421ebb9 11863 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11864 about division by zero. Do not issue a warning if DIVISOR has a
f092582b 11865 floating-point type, since we consider 0.0/0.0 a valid way of
11866 generating a NaN. */
48d94ede 11867 if (c_inhibit_evaluation_warnings == 0
9421ebb9 11868 && (integer_zerop (divisor) || fixed_zerop (divisor)))
b6889cb0 11869 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
f092582b 11870}
11871
13869a99 11872/* Subroutine of build_binary_op. Give warnings for comparisons
11873 between signed and unsigned quantities that may fail. Do the
11874 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11875 so that casts will be considered, but default promotions won't
8e70fb09 11876 be.
11877
11878 LOCATION is the location of the comparison operator.
13869a99 11879
11880 The arguments of this function map directly to local variables
11881 of build_binary_op. */
11882
48e1416a 11883void
8e70fb09 11884warn_for_sign_compare (location_t location,
48e1416a 11885 tree orig_op0, tree orig_op1,
11886 tree op0, tree op1,
13869a99 11887 tree result_type, enum tree_code resultcode)
11888{
11889 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11890 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11891 int unsignedp0, unsignedp1;
48e1416a 11892
13869a99 11893 /* In C++, check for comparison of different enum types. */
11894 if (c_dialect_cxx()
11895 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11896 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11897 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
895b662f 11898 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
13869a99 11899 {
8e70fb09 11900 warning_at (location,
11901 OPT_Wsign_compare, "comparison between types %qT and %qT",
11902 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
13869a99 11903 }
11904
11905 /* Do not warn if the comparison is being done in a signed type,
11906 since the signed type will only be chosen if it can represent
11907 all the values of the unsigned type. */
11908 if (!TYPE_UNSIGNED (result_type))
11909 /* OK */;
11910 /* Do not warn if both operands are unsigned. */
11911 else if (op0_signed == op1_signed)
11912 /* OK */;
11913 else
11914 {
895b662f 11915 tree sop, uop, base_type;
13869a99 11916 bool ovf;
895b662f 11917
13869a99 11918 if (op0_signed)
11919 sop = orig_op0, uop = orig_op1;
48e1416a 11920 else
13869a99 11921 sop = orig_op1, uop = orig_op0;
11922
48e1416a 11923 STRIP_TYPE_NOPS (sop);
13869a99 11924 STRIP_TYPE_NOPS (uop);
895b662f 11925 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11926 ? TREE_TYPE (result_type) : result_type);
13869a99 11927
11928 /* Do not warn if the signed quantity is an unsuffixed integer
11929 literal (or some static constant expression involving such
11930 literals or a conditional expression involving such literals)
11931 and it is non-negative. */
11932 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11933 /* OK */;
11934 /* Do not warn if the comparison is an equality operation, the
11935 unsigned quantity is an integral constant, and it would fit
11936 in the result if the result were signed. */
11937 else if (TREE_CODE (uop) == INTEGER_CST
11938 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
895b662f 11939 && int_fits_type_p (uop, c_common_signed_type (base_type)))
13869a99 11940 /* OK */;
11941 /* In C, do not warn if the unsigned quantity is an enumeration
11942 constant and its maximum value would fit in the result if the
11943 result were signed. */
11944 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11945 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11946 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
895b662f 11947 c_common_signed_type (base_type)))
13869a99 11948 /* OK */;
48e1416a 11949 else
8e70fb09 11950 warning_at (location,
48e1416a 11951 OPT_Wsign_compare,
8e70fb09 11952 "comparison between signed and unsigned integer expressions");
13869a99 11953 }
48e1416a 11954
13869a99 11955 /* Warn if two unsigned values are being compared in a size larger
11956 than their original size, and one (and only one) is the result of
11957 a `~' operator. This comparison will always fail.
48e1416a 11958
13869a99 11959 Also warn if one operand is a constant, and the constant does not
11960 have all bits set that are set in the ~ operand when it is
11961 extended. */
11962
7f506bca 11963 op0 = c_common_get_narrower (op0, &unsignedp0);
11964 op1 = c_common_get_narrower (op1, &unsignedp1);
48e1416a 11965
13869a99 11966 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11967 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11968 {
11969 if (TREE_CODE (op0) == BIT_NOT_EXPR)
7f506bca 11970 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
13869a99 11971 if (TREE_CODE (op1) == BIT_NOT_EXPR)
7f506bca 11972 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
13869a99 11973
e913b5cd 11974 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
13869a99 11975 {
11976 tree primop;
11977 HOST_WIDE_INT constant, mask;
11978 int unsignedp;
11979 unsigned int bits;
48e1416a 11980
e913b5cd 11981 if (tree_fits_shwi_p (op0))
13869a99 11982 {
11983 primop = op1;
11984 unsignedp = unsignedp1;
e913b5cd 11985 constant = tree_to_shwi (op0);
13869a99 11986 }
11987 else
11988 {
11989 primop = op0;
11990 unsignedp = unsignedp0;
e913b5cd 11991 constant = tree_to_shwi (op1);
13869a99 11992 }
48e1416a 11993
13869a99 11994 bits = TYPE_PRECISION (TREE_TYPE (primop));
11995 if (bits < TYPE_PRECISION (result_type)
11996 && bits < HOST_BITS_PER_LONG && unsignedp)
11997 {
11998 mask = (~ (HOST_WIDE_INT) 0) << bits;
11999 if ((mask & constant) != mask)
12000 {
12001 if (constant == 0)
76fdceeb 12002 warning_at (location, OPT_Wsign_compare,
12003 "promoted ~unsigned is always non-zero");
13869a99 12004 else
48e1416a 12005 warning_at (location, OPT_Wsign_compare,
8e70fb09 12006 "comparison of promoted ~unsigned with constant");
13869a99 12007 }
12008 }
12009 }
12010 else if (unsignedp0 && unsignedp1
12011 && (TYPE_PRECISION (TREE_TYPE (op0))
12012 < TYPE_PRECISION (result_type))
12013 && (TYPE_PRECISION (TREE_TYPE (op1))
12014 < TYPE_PRECISION (result_type)))
8e70fb09 12015 warning_at (location, OPT_Wsign_compare,
13869a99 12016 "comparison of promoted ~unsigned with unsigned");
12017 }
12018}
12019
5ba33bf4 12020/* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
12021 type via c_common_type. If -Wdouble-promotion is in use, and the
12022 conditions for warning have been met, issue a warning. GMSGID is
12023 the warning message. It must have two %T specifiers for the type
12024 that was converted (generally "float") and the type to which it was
12025 converted (generally "double), respectively. LOC is the location
12026 to which the awrning should refer. */
12027
12028void
12029do_warn_double_promotion (tree result_type, tree type1, tree type2,
12030 const char *gmsgid, location_t loc)
12031{
12032 tree source_type;
12033
12034 if (!warn_double_promotion)
12035 return;
12036 /* If the conversion will not occur at run-time, there is no need to
12037 warn about it. */
12038 if (c_inhibit_evaluation_warnings)
12039 return;
12040 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
12041 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
12042 return;
12043 if (TYPE_MAIN_VARIANT (type1) == float_type_node
12044 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
12045 source_type = type1;
12046 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
12047 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
12048 source_type = type2;
12049 else
12050 return;
12051 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
12052}
12053
41771881 12054/* Setup a TYPE_DECL node as a typedef representation.
12055
12056 X is a TYPE_DECL for a typedef statement. Create a brand new
12057 ..._TYPE node (which will be just a variant of the existing
12058 ..._TYPE node with identical properties) and then install X
12059 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
12060
12061 The whole point here is to end up with a situation where each
12062 and every ..._TYPE node the compiler creates will be uniquely
12063 associated with AT MOST one node representing a typedef name.
12064 This way, even though the compiler substitutes corresponding
12065 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
12066 early on, later parts of the compiler can always do the reverse
12067 translation and get back the corresponding typedef name. For
12068 example, given:
ab2c1de8 12069
41771881 12070 typedef struct S MY_TYPE;
12071 MY_TYPE object;
12072
12073 Later parts of the compiler might only know that `object' was of
12074 type `struct S' if it were not for code just below. With this
12075 code however, later parts of the compiler see something like:
12076
12077 struct S' == struct S
12078 typedef struct S' MY_TYPE;
12079 struct S' object;
12080
12081 And they can then deduce (from the node for type struct S') that
12082 the original object declaration was:
12083
12084 MY_TYPE object;
12085
12086 Being able to do this is important for proper support of protoize,
12087 and also for generating precise symbolic debugging information
12088 which takes full account of the programmer's (typedef) vocabulary.
12089
12090 Obviously, we don't want to generate a duplicate ..._TYPE node if
12091 the TYPE_DECL node that we are now processing really represents a
12092 standard built-in type. */
12093
12094void
12095set_underlying_type (tree x)
12096{
12097 if (x == error_mark_node)
12098 return;
12099 if (DECL_IS_BUILTIN (x))
12100 {
12101 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12102 TYPE_NAME (TREE_TYPE (x)) = x;
12103 }
12104 else if (TREE_TYPE (x) != error_mark_node
12105 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12106 {
12107 tree tt = TREE_TYPE (x);
12108 DECL_ORIGINAL_TYPE (x) = tt;
12109 tt = build_variant_type_copy (tt);
12110 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12111 TYPE_NAME (tt) = x;
12112 TREE_USED (tt) = TREE_USED (x);
12113 TREE_TYPE (x) = tt;
12114 }
12115}
12116
1a4c44c5 12117/* Record the types used by the current global variable declaration
12118 being parsed, so that we can decide later to emit their debug info.
12119 Those types are in types_used_by_cur_var_decl, and we are going to
12120 store them in the types_used_by_vars_hash hash table.
12121 DECL is the declaration of the global variable that has been parsed. */
12122
12123void
12124record_types_used_by_current_var_decl (tree decl)
12125{
12126 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12127
f1f41a6c 12128 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
1a4c44c5 12129 {
f1f41a6c 12130 tree type = types_used_by_cur_var_decl->pop ();
aef48c9a 12131 types_used_by_var_decl_insert (type, decl);
1a4c44c5 12132 }
12133}
12134
a4e3ffad 12135/* If DECL is a typedef that is declared in the current function,
12136 record it for the purpose of -Wunused-local-typedefs. */
12137
12138void
12139record_locally_defined_typedef (tree decl)
12140{
12141 struct c_language_function *l;
12142
12143 if (!warn_unused_local_typedefs
12144 || cfun == NULL
12145 /* if this is not a locally defined typedef then we are not
12146 interested. */
12147 || !is_typedef_decl (decl)
12148 || !decl_function_context (decl))
12149 return;
12150
12151 l = (struct c_language_function *) cfun->language;
f1f41a6c 12152 vec_safe_push (l->local_typedefs, decl);
a4e3ffad 12153}
12154
12155/* If T is a TYPE_DECL declared locally, mark it as used. */
12156
12157void
12158maybe_record_typedef_use (tree t)
12159{
12160 if (!is_typedef_decl (t))
12161 return;
12162
12163 TREE_USED (t) = true;
12164}
12165
12166/* Warn if there are some unused locally defined typedefs in the
12167 current function. */
12168
12169void
12170maybe_warn_unused_local_typedefs (void)
12171{
12172 int i;
12173 tree decl;
12174 /* The number of times we have emitted -Wunused-local-typedefs
12175 warnings. If this is different from errorcount, that means some
12176 unrelated errors have been issued. In which case, we'll avoid
12177 emitting "unused-local-typedefs" warnings. */
12178 static int unused_local_typedefs_warn_count;
12179 struct c_language_function *l;
12180
12181 if (cfun == NULL)
12182 return;
12183
12184 if ((l = (struct c_language_function *) cfun->language) == NULL)
12185 return;
12186
12187 if (warn_unused_local_typedefs
12188 && errorcount == unused_local_typedefs_warn_count)
12189 {
f1f41a6c 12190 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
a4e3ffad 12191 if (!TREE_USED (decl))
12192 warning_at (DECL_SOURCE_LOCATION (decl),
12193 OPT_Wunused_local_typedefs,
12194 "typedef %qD locally defined but not used", decl);
12195 unused_local_typedefs_warn_count = errorcount;
12196 }
12197
f1f41a6c 12198 vec_free (l->local_typedefs);
a4e3ffad 12199}
12200
78bf4156 12201/* Warn about boolean expression compared with an integer value different
12202 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12203 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12204 are the operands of the comparison. The caller must ensure that
12205 either operand is a boolean expression. */
12206
12207void
12208maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12209 tree op1)
12210{
12211 if (TREE_CODE_CLASS (code) != tcc_comparison)
12212 return;
12213
12214 tree cst = (TREE_CODE (op0) == INTEGER_CST)
12215 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
12216 if (!cst)
12217 return;
12218
12219 if (!integer_zerop (cst) && !integer_onep (cst))
12220 {
a720ab1c 12221 int sign = (TREE_CODE (op0) == INTEGER_CST
12222 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
78bf4156 12223 if (code == EQ_EXPR
12224 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12225 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12226 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12227 "with boolean expression is always false", cst);
12228 else
12229 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12230 "with boolean expression is always true", cst);
12231 }
a720ab1c 12232 else if (integer_zerop (cst) || integer_onep (cst))
12233 {
14744a16 12234 /* If the non-constant operand isn't of a boolean type, we
12235 don't want to warn here. */
12236 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12237 /* Handle booleans promoted to integers. */
12238 if (CONVERT_EXPR_P (noncst)
12239 && TREE_TYPE (noncst) == integer_type_node
12240 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
12241 /* Warn. */;
12242 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12243 && !truth_value_p (TREE_CODE (noncst)))
12244 return;
a720ab1c 12245 /* Do some magic to get the right diagnostics. */
12246 bool flag = TREE_CODE (op0) == INTEGER_CST;
12247 flag = integer_zerop (cst) ? flag : !flag;
12248 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12249 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12250 "with boolean expression is always true", cst);
12251 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12252 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12253 "with boolean expression is always false", cst);
12254 }
78bf4156 12255}
12256
f352a3fb 12257/* The C and C++ parsers both use vectors to hold function arguments.
12258 For efficiency, we keep a cache of unused vectors. This is the
12259 cache. */
12260
f1f41a6c 12261typedef vec<tree, va_gc> *tree_gc_vec;
12262static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
f352a3fb 12263
12264/* Return a new vector from the cache. If the cache is empty,
12265 allocate a new vector. These vectors are GC'ed, so it is OK if the
12266 pointer is not released.. */
12267
f1f41a6c 12268vec<tree, va_gc> *
f352a3fb 12269make_tree_vector (void)
12270{
f1f41a6c 12271 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12272 return tree_vector_cache->pop ();
f352a3fb 12273 else
12274 {
f1f41a6c 12275 /* Passing 0 to vec::alloc returns NULL, and our callers require
f352a3fb 12276 that we always return a non-NULL value. The vector code uses
12277 4 when growing a NULL vector, so we do too. */
f1f41a6c 12278 vec<tree, va_gc> *v;
12279 vec_alloc (v, 4);
12280 return v;
f352a3fb 12281 }
12282}
12283
12284/* Release a vector of trees back to the cache. */
12285
12286void
f1f41a6c 12287release_tree_vector (vec<tree, va_gc> *vec)
f352a3fb 12288{
12289 if (vec != NULL)
12290 {
f1f41a6c 12291 vec->truncate (0);
12292 vec_safe_push (tree_vector_cache, vec);
f352a3fb 12293 }
12294}
12295
12296/* Get a new tree vector holding a single tree. */
12297
f1f41a6c 12298vec<tree, va_gc> *
f352a3fb 12299make_tree_vector_single (tree t)
12300{
f1f41a6c 12301 vec<tree, va_gc> *ret = make_tree_vector ();
12302 ret->quick_push (t);
f352a3fb 12303 return ret;
12304}
12305
c66c81be 12306/* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12307
f1f41a6c 12308vec<tree, va_gc> *
c66c81be 12309make_tree_vector_from_list (tree list)
12310{
f1f41a6c 12311 vec<tree, va_gc> *ret = make_tree_vector ();
c66c81be 12312 for (; list; list = TREE_CHAIN (list))
f1f41a6c 12313 vec_safe_push (ret, TREE_VALUE (list));
c66c81be 12314 return ret;
12315}
12316
f352a3fb 12317/* Get a new tree vector which is a copy of an existing one. */
12318
f1f41a6c 12319vec<tree, va_gc> *
12320make_tree_vector_copy (const vec<tree, va_gc> *orig)
f352a3fb 12321{
f1f41a6c 12322 vec<tree, va_gc> *ret;
f352a3fb 12323 unsigned int ix;
12324 tree t;
12325
12326 ret = make_tree_vector ();
f1f41a6c 12327 vec_safe_reserve (ret, vec_safe_length (orig));
12328 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12329 ret->quick_push (t);
f352a3fb 12330 return ret;
12331}
12332
a9ffdd35 12333/* Return true if KEYWORD starts a type specifier. */
12334
12335bool
12336keyword_begins_type_specifier (enum rid keyword)
12337{
12338 switch (keyword)
12339 {
4fba5eb9 12340 case RID_AUTO_TYPE:
a9ffdd35 12341 case RID_INT:
12342 case RID_CHAR:
12343 case RID_FLOAT:
12344 case RID_DOUBLE:
12345 case RID_VOID:
a9ffdd35 12346 case RID_UNSIGNED:
12347 case RID_LONG:
12348 case RID_SHORT:
12349 case RID_SIGNED:
12350 case RID_DFLOAT32:
12351 case RID_DFLOAT64:
12352 case RID_DFLOAT128:
12353 case RID_FRACT:
12354 case RID_ACCUM:
12355 case RID_BOOL:
12356 case RID_WCHAR:
12357 case RID_CHAR16:
12358 case RID_CHAR32:
12359 case RID_SAT:
12360 case RID_COMPLEX:
12361 case RID_TYPEOF:
12362 case RID_STRUCT:
12363 case RID_CLASS:
12364 case RID_UNION:
12365 case RID_ENUM:
12366 return true;
12367 default:
9f75f026 12368 if (keyword >= RID_FIRST_INT_N
12369 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12370 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12371 return true;
a9ffdd35 12372 return false;
12373 }
12374}
12375
12376/* Return true if KEYWORD names a type qualifier. */
12377
12378bool
12379keyword_is_type_qualifier (enum rid keyword)
12380{
12381 switch (keyword)
12382 {
12383 case RID_CONST:
12384 case RID_VOLATILE:
12385 case RID_RESTRICT:
b560fabd 12386 case RID_ATOMIC:
a9ffdd35 12387 return true;
12388 default:
12389 return false;
12390 }
12391}
12392
12393/* Return true if KEYWORD names a storage class specifier.
12394
12395 RID_TYPEDEF is not included in this list despite `typedef' being
12396 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12397 such for syntactic convenience only. */
12398
12399bool
12400keyword_is_storage_class_specifier (enum rid keyword)
12401{
12402 switch (keyword)
12403 {
12404 case RID_STATIC:
12405 case RID_EXTERN:
12406 case RID_REGISTER:
12407 case RID_AUTO:
12408 case RID_MUTABLE:
12409 case RID_THREAD:
12410 return true;
12411 default:
12412 return false;
12413 }
12414}
12415
fad3f658 12416/* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12417
12418static bool
12419keyword_is_function_specifier (enum rid keyword)
12420{
12421 switch (keyword)
12422 {
12423 case RID_INLINE:
985c6e3a 12424 case RID_NORETURN:
fad3f658 12425 case RID_VIRTUAL:
12426 case RID_EXPLICIT:
12427 return true;
12428 default:
12429 return false;
12430 }
12431}
12432
12433/* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12434 declaration-specifier (C99 6.7). */
12435
12436bool
12437keyword_is_decl_specifier (enum rid keyword)
12438{
12439 if (keyword_is_storage_class_specifier (keyword)
12440 || keyword_is_type_qualifier (keyword)
12441 || keyword_is_function_specifier (keyword))
12442 return true;
12443
12444 switch (keyword)
12445 {
12446 case RID_TYPEDEF:
12447 case RID_FRIEND:
12448 case RID_CONSTEXPR:
12449 return true;
12450 default:
12451 return false;
12452 }
12453}
12454
9b88d08d 12455/* Initialize language-specific-bits of tree_contains_struct. */
12456
12457void
12458c_common_init_ts (void)
12459{
12460 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12461 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
3c6d4197 12462 MARK_TS_TYPED (ARRAY_NOTATION_REF);
9b88d08d 12463}
12464
244db24d 12465/* Build a user-defined numeric literal out of an integer constant type VALUE
12466 with identifier SUFFIX. */
12467
12468tree
324ca377 12469build_userdef_literal (tree suffix_id, tree value,
12470 enum overflow_type overflow, tree num_string)
244db24d 12471{
12472 tree literal = make_node (USERDEF_LITERAL);
12473 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12474 USERDEF_LITERAL_VALUE (literal) = value;
324ca377 12475 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
244db24d 12476 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12477 return literal;
12478}
12479
7059d45d 12480/* For vector[index], convert the vector to a
c61ef207 12481 pointer of the underlying type. Return true if the resulting
12482 ARRAY_REF should not be an lvalue. */
12483
12484bool
7059d45d 12485convert_vector_to_pointer_for_subscript (location_t loc,
c61ef207 12486 tree *vecp, tree index)
7059d45d 12487{
c61ef207 12488 bool ret = false;
7059d45d 12489 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
12490 {
12491 tree type = TREE_TYPE (*vecp);
12492 tree type1;
12493
c61ef207 12494 ret = !lvalue_p (*vecp);
7059d45d 12495 if (TREE_CODE (index) == INTEGER_CST)
e913b5cd 12496 if (!tree_fits_uhwi_p (index)
aa59f000 12497 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7059d45d 12498 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12499
c61ef207 12500 if (ret)
12501 {
f9e245b2 12502 tree tmp = create_tmp_var_raw (type);
c61ef207 12503 DECL_SOURCE_LOCATION (tmp) = loc;
12504 *vecp = c_save_expr (*vecp);
12505 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12506 {
12507 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12508 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12509 *vecp
12510 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12511 *vecp, NULL_TREE, NULL_TREE),
12512 non_const);
12513 }
12514 else
12515 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12516 NULL_TREE, NULL_TREE);
12517 SET_EXPR_LOCATION (*vecp, loc);
12518 c_common_mark_addressable_vec (tmp);
12519 }
12520 else
12521 c_common_mark_addressable_vec (*vecp);
7059d45d 12522 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7059d45d 12523 type1 = build_pointer_type (TREE_TYPE (*vecp));
b2ca6510 12524 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12525 if (!ref_all
12526 && !DECL_P (*vecp))
12527 {
12528 /* If the original vector isn't declared may_alias and it
12529 isn't a bare vector look if the subscripting would
12530 alias the vector we subscript, and if not, force ref-all. */
12531 alias_set_type vecset = get_alias_set (*vecp);
12532 alias_set_type sset = get_alias_set (type);
12533 if (!alias_sets_must_conflict_p (sset, vecset)
12534 && !alias_set_subset_of (sset, vecset))
12535 ref_all = true;
12536 }
12537 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
7059d45d 12538 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12539 *vecp = convert (type, *vecp);
12540 }
c61ef207 12541 return ret;
7059d45d 12542}
12543
41ed701a 12544/* Determine which of the operands, if any, is a scalar that needs to be
12545 converted to a vector, for the range of operations. */
12546enum stv_conv
12547scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12548 bool complain)
12549{
12550 tree type0 = TREE_TYPE (op0);
12551 tree type1 = TREE_TYPE (op1);
12552 bool integer_only_op = false;
12553 enum stv_conv ret = stv_firstarg;
12554
12555 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
12556 || TREE_CODE (type1) == VECTOR_TYPE);
12557 switch (code)
12558 {
12559 /* Most GENERIC binary expressions require homogeneous arguments.
12560 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12561 argument that is a vector and a second one that is a scalar, so
12562 we never return stv_secondarg for them. */
12563 case RSHIFT_EXPR:
12564 case LSHIFT_EXPR:
12565 if (TREE_CODE (type0) == INTEGER_TYPE
12566 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12567 {
22a75734 12568 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12569 {
12570 if (complain)
12571 error_at (loc, "conversion of scalar %qT to vector %qT "
12572 "involves truncation", type0, type1);
12573 return stv_error;
12574 }
12575 else
12576 return stv_firstarg;
12577 }
12578 break;
12579
12580 case BIT_IOR_EXPR:
12581 case BIT_XOR_EXPR:
12582 case BIT_AND_EXPR:
12583 integer_only_op = true;
12584 /* ... fall through ... */
12585
7b463b19 12586 case VEC_COND_EXPR:
12587
41ed701a 12588 case PLUS_EXPR:
12589 case MINUS_EXPR:
12590 case MULT_EXPR:
12591 case TRUNC_DIV_EXPR:
12592 case CEIL_DIV_EXPR:
12593 case FLOOR_DIV_EXPR:
12594 case ROUND_DIV_EXPR:
12595 case EXACT_DIV_EXPR:
12596 case TRUNC_MOD_EXPR:
12597 case FLOOR_MOD_EXPR:
12598 case RDIV_EXPR:
12599 case EQ_EXPR:
12600 case NE_EXPR:
12601 case LE_EXPR:
12602 case GE_EXPR:
12603 case LT_EXPR:
12604 case GT_EXPR:
12605 /* What about UNLT_EXPR? */
12606 if (TREE_CODE (type0) == VECTOR_TYPE)
12607 {
12608 tree tmp;
12609 ret = stv_secondarg;
12610 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
12611 tmp = type0; type0 = type1; type1 = tmp;
12612 tmp = op0; op0 = op1; op1 = tmp;
12613 }
12614
12615 if (TREE_CODE (type0) == INTEGER_TYPE
12616 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12617 {
22a75734 12618 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12619 {
12620 if (complain)
12621 error_at (loc, "conversion of scalar %qT to vector %qT "
12622 "involves truncation", type0, type1);
12623 return stv_error;
12624 }
12625 return ret;
12626 }
12627 else if (!integer_only_op
12628 /* Allow integer --> real conversion if safe. */
12629 && (TREE_CODE (type0) == REAL_TYPE
12630 || TREE_CODE (type0) == INTEGER_TYPE)
12631 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12632 {
22a75734 12633 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12634 {
12635 if (complain)
12636 error_at (loc, "conversion of scalar %qT to vector %qT "
12637 "involves truncation", type0, type1);
12638 return stv_error;
12639 }
12640 return ret;
12641 }
12642 default:
12643 break;
12644 }
12645
12646 return stv_nothing;
12647}
12648
ffcdbf9c 12649/* Return true iff ALIGN is an integral constant that is a fundamental
12650 alignment, as defined by [basic.align] in the c++-11
12651 specifications.
12652
12653 That is:
12654
12655 [A fundamental alignment is represented by an alignment less than or
12656 equal to the greatest alignment supported by the implementation
12657 in all contexts, which is equal to
12658 alignof(max_align_t)]. */
12659
12660bool
12661cxx_fundamental_alignment_p (unsigned align)
12662{
12663 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12664 TYPE_ALIGN (long_double_type_node)));
12665}
12666
46da3601 12667/* Return true if T is a pointer to a zero-sized aggregate. */
12668
12669bool
12670pointer_to_zero_sized_aggr_p (tree t)
12671{
12672 if (!POINTER_TYPE_P (t))
12673 return false;
12674 t = TREE_TYPE (t);
12675 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12676}
12677
7bedc3a0 12678#include "gt-c-family-c-common.h"