]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-common.c
2015-06-25 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 "hard-reg-set.h"
1140c305 51#include "function.h"
62eec3b4 52#include "cgraph.h"
a8783bee 53#include "gimplify.h"
e913b5cd 54#include "wide-int-print.h"
c61ef207 55#include "gimple-expr.h"
fd6f6435 56
90cc7820 57cpp_reader *parse_in; /* Declared in c-pragma.h. */
a654e028 58
de801c28 59/* Mode used to build pointers (VOIDmode means ptr_mode). */
60
61machine_mode c_default_pointer_mode = VOIDmode;
62
72040e7e 63/* The following symbols are subsumed in the c_global_trees array, and
44e9fa65 64 listed here individually for documentation purposes.
72040e7e 65
66 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
67
68 tree short_integer_type_node;
69 tree long_integer_type_node;
70 tree long_long_integer_type_node;
71
72 tree short_unsigned_type_node;
73 tree long_unsigned_type_node;
74 tree long_long_unsigned_type_node;
75
3c2239cf 76 tree truthvalue_type_node;
77 tree truthvalue_false_node;
78 tree truthvalue_true_node;
72040e7e 79
80 tree ptrdiff_type_node;
81
82 tree unsigned_char_type_node;
83 tree signed_char_type_node;
84 tree wchar_type_node;
72040e7e 85
924bbf02 86 tree char16_type_node;
87 tree char32_type_node;
88
72040e7e 89 tree float_type_node;
90 tree double_type_node;
91 tree long_double_type_node;
92
93 tree complex_integer_type_node;
94 tree complex_float_type_node;
95 tree complex_double_type_node;
96 tree complex_long_double_type_node;
97
c4503c0a 98 tree dfloat32_type_node;
99 tree dfloat64_type_node;
100 tree_dfloat128_type_node;
101
72040e7e 102 tree intQI_type_node;
103 tree intHI_type_node;
104 tree intSI_type_node;
105 tree intDI_type_node;
106 tree intTI_type_node;
107
108 tree unsigned_intQI_type_node;
109 tree unsigned_intHI_type_node;
110 tree unsigned_intSI_type_node;
111 tree unsigned_intDI_type_node;
112 tree unsigned_intTI_type_node;
113
114 tree widest_integer_literal_type_node;
115 tree widest_unsigned_literal_type_node;
116
117 Nodes for types `void *' and `const void *'.
118
119 tree ptr_type_node, const_ptr_type_node;
120
121 Nodes for types `char *' and `const char *'.
122
123 tree string_type_node, const_string_type_node;
124
125 Type `char[SOMENUMBER]'.
126 Used when an array of char is needed and the size is irrelevant.
127
128 tree char_array_type_node;
129
72040e7e 130 Type `wchar_t[SOMENUMBER]' or something like it.
131 Used when a wide string literal is created.
132
133 tree wchar_array_type_node;
134
924bbf02 135 Type `char16_t[SOMENUMBER]' or something like it.
136 Used when a UTF-16 string literal is created.
137
138 tree char16_array_type_node;
139
140 Type `char32_t[SOMENUMBER]' or something like it.
141 Used when a UTF-32 string literal is created.
142
143 tree char32_array_type_node;
144
72040e7e 145 Type `int ()' -- used for implicit declaration of functions.
146
147 tree default_function_type;
148
72040e7e 149 A VOID_TYPE node, packaged in a TREE_LIST.
150
151 tree void_list_node;
152
734c98be 153 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
65b7f83f 154 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
155 VAR_DECLS, but C++ does.)
71d9fc9b 156
65b7f83f 157 tree function_name_decl_node;
734c98be 158 tree pretty_function_name_decl_node;
65b7f83f 159 tree c99_function_name_decl_node;
160
161 Stack of nested function name VAR_DECLs.
1cae46be 162
65b7f83f 163 tree saved_function_name_decls;
71d9fc9b 164
72040e7e 165*/
166
167tree c_global_trees[CTI_MAX];
573aba85 168\f
574a6990 169/* Switches common to the C front ends. */
170
f7070933 171/* Nonzero means don't output line number information. */
172
173char flag_no_line_commands;
174
175/* Nonzero causes -E output not to be done, but directives such as
176 #define that have side effects are still obeyed. */
177
178char flag_no_output;
179
180/* Nonzero means dump macros in some fashion. */
181
182char flag_dump_macros;
183
184/* Nonzero means pass #include lines through to the output. */
185
186char flag_dump_includes;
187
d718b525 188/* Nonzero means process PCH files while preprocessing. */
189
190bool flag_pch_preprocess;
191
573aba85 192/* The file name to which we should write a precompiled header, or
193 NULL if no header will be written in this compile. */
194
195const char *pch_file;
196
1ed9d5f5 197/* Nonzero if an ISO standard was selected. It rejects macros in the
198 user's namespace. */
199int flag_iso;
200
574a6990 201/* C/ObjC language option variables. */
202
203
574a6990 204/* Nonzero means allow type mismatches in conditional expressions;
205 just make their values `void'. */
206
207int flag_cond_mismatch;
208
209/* Nonzero means enable C89 Amendment 1 features. */
210
211int flag_isoc94;
212
32074525 213/* Nonzero means use the ISO C99 (or C11) dialect of C. */
574a6990 214
215int flag_isoc99;
216
32074525 217/* Nonzero means use the ISO C11 dialect of C. */
39012afb 218
32074525 219int flag_isoc11;
39012afb 220
8b332087 221/* Nonzero means that we have builtin functions, and main is an int. */
574a6990 222
223int flag_hosted = 1;
224
574a6990 225
226/* ObjC language option variables. */
227
228
574a6990 229/* Tells the compiler that this is a special run. Do not perform any
230 compiling, instead we are to test some platform dependent features
231 and output a C header file with appropriate definitions. */
232
233int print_struct_values;
234
f0b5f617 235/* Tells the compiler what is the constant string class for ObjC. */
574a6990 236
237const char *constant_string_class_name;
238
574a6990 239
240/* C++ language option variables. */
241
242
574a6990 243/* Nonzero means generate separate instantiation control files and
244 juggle them at link time. */
245
246int flag_use_repository;
247
d875b9d2 248/* The C++ dialect being used. Default set in c_common_post_options. */
0fe6eeac 249
d875b9d2 250enum cxx_dialect cxx_dialect = cxx_unset;
0fe6eeac 251
9dcd0d49 252/* Maximum template instantiation depth. This limit exists to limit the
8ce59854 253 time it takes to notice excessively recursive template instantiations.
574a6990 254
8ce59854 255 The default is lower than the 1024 recommended by the C++0x standard
256 because G++ runs out of stack before 1024 with highly recursive template
257 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
258
259int max_tinst_depth = 900;
574a6990 260
988fc1d1 261/* The elements of `ridpointers' are identifier nodes for the reserved
262 type names and storage classes. It is indexed by a RID_... value. */
263tree *ridpointers;
264
e60a6f7b 265tree (*make_fname_decl) (location_t, tree, int);
9e5a737d 266
48d94ede 267/* Nonzero means don't warn about problems that occur when the code is
268 executed. */
269int c_inhibit_evaluation_warnings;
e78703c1 270
93be21c0 271/* Whether we are building a boolean conversion inside
272 convert_for_assignment, or some other late binary operation. If
273 build_binary_op is called for C (from code shared by C and C++) in
274 this case, then the operands have already been folded and the
275 result will not be folded again, so C_MAYBE_CONST_EXPR should not
276 be generated. */
277bool in_late_binary_op;
278
7f5f3953 279/* Whether lexing has been completed, so subsequent preprocessor
280 errors should use the compiler's input_location. */
281bool done_lexing = false;
282
2c0e001b 283/* Information about how a function name is generated. */
65b7f83f 284struct fname_var_t
285{
e99c3a1d 286 tree *const decl; /* pointer to the VAR_DECL. */
287 const unsigned rid; /* RID number for the identifier. */
288 const int pretty; /* How pretty is it? */
65b7f83f 289};
290
2c0e001b 291/* The three ways of getting then name of the current function. */
65b7f83f 292
293const struct fname_var_t fname_vars[] =
294{
2c0e001b 295 /* C99 compliant __func__, must be first. */
65b7f83f 296 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
2c0e001b 297 /* GCC __FUNCTION__ compliant. */
65b7f83f 298 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
2c0e001b 299 /* GCC __PRETTY_FUNCTION__ compliant. */
65b7f83f 300 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
301 {NULL, 0, 0},
302};
303
79396169 304/* Global visibility options. */
305struct visibility_flags visibility_options;
306
aac24642 307static tree c_fully_fold_internal (tree expr, bool, bool *, bool *, bool);
2d2f6a15 308static tree check_case_value (location_t, tree);
f61a9bc2 309static bool check_case_bounds (location_t, tree, tree, tree *, tree *);
be43ff5a 310
1cae46be 311static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
312static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
313static tree handle_common_attribute (tree *, tree, tree, int, bool *);
314static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
5de92639 315static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
316static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
a9196da9 317static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
318 int, bool *);
d413ffdd 319static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
320 int, bool *);
05f893e1 321static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
322 bool *);
947aa916 323static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
1cae46be 324static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
bdb1f0d1 325static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
85fbea97 326static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
7bd95dfd 327static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
1cae46be 328static tree handle_always_inline_attribute (tree *, tree, tree, int,
329 bool *);
1b16fc45 330static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
331static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
0cdd9887 332static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
10fc867f 333static tree handle_error_attribute (tree *, tree, tree, int, bool *);
1cae46be 334static tree handle_used_attribute (tree *, tree, tree, int, bool *);
335static tree handle_unused_attribute (tree *, tree, tree, int, bool *);
62eec3b4 336static tree handle_externally_visible_attribute (tree *, tree, tree, int,
337 bool *);
6b722052 338static tree handle_no_reorder_attribute (tree *, tree, tree, int,
339 bool *);
1cae46be 340static tree handle_const_attribute (tree *, tree, tree, int, bool *);
341static tree handle_transparent_union_attribute (tree *, tree, tree,
342 int, bool *);
343static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
344static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
345static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
346static tree handle_section_attribute (tree *, tree, tree, int, bool *);
347static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
348static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
4641882f 349static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
85c0a25c 350static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
351static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
1cae46be 352static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
f4a30bd7 353static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
1cae46be 354static tree handle_visibility_attribute (tree *, tree, tree, int,
355 bool *);
356static tree handle_tls_model_attribute (tree *, tree, tree, int,
357 bool *);
358static tree handle_no_instrument_function_attribute (tree *, tree,
359 tree, int, bool *);
360static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
26d1c5ff 361static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
1cae46be 362static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
363 bool *);
364static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
4c0315d0 365static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
366static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
fc09b200 367static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
1cae46be 368static tree handle_deprecated_attribute (tree *, tree, tree, int,
369 bool *);
370static tree handle_vector_size_attribute (tree *, tree, tree, int,
371 bool *);
372static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
373static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
374static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
8a8cdb8d 375static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
376 bool *);
bf6c8de0 377static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
b5c26b42 378static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
4a29c97c 379static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
237e78b1 380static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
381static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
24470055 382static tree handle_target_attribute (tree *, tree, tree, int, bool *);
46f8e3b0 383static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
4c0315d0 384static tree ignore_attribute (tree *, tree, tree, int, bool *);
48b14f50 385static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
8ce86007 386static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
a96c3cc1 387static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
d7dcba40 388static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
bc7bff74 389static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
390 bool *);
391static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
392 bool *);
74691f46 393static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
058a1b7a 394static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
395static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
396static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
1cae46be 397
d01f58f9 398static void check_function_nonnull (tree, int, tree *);
1cae46be 399static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
400static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
401static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
860251be 402static int resort_field_decl_cmp (const void *, const void *);
dbf6c367 403
5c6e5756 404/* Reserved words. The third field is a mask: keywords are disabled
405 if they match the mask.
406
407 Masks for languages:
408 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
409 C --std=c99: D_CXXONLY | D_OBJC
410 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
411 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
412 C++ --std=c0x: D_CONLY | D_OBJC
413 ObjC++ is like C++ except that D_OBJC is not set
414
415 If -fno-asm is used, D_ASM is added to the mask. If
416 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
417 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
b27e241e 418 In C with -Wc++-compat, we warn if D_CXXWARN is set.
5c6e5756 419
b27e241e 420 Note the complication of the D_CXX_OBJC keywords. These are
421 reserved words such as 'class'. In C++, 'class' is a reserved
422 word. In Objective-C++ it is too. In Objective-C, it is a
423 reserved word too, but only if it follows an '@' sign.
424*/
5c6e5756 425const struct c_common_resword c_common_reswords[] =
426{
83e25171 427 { "_Alignas", RID_ALIGNAS, D_CONLY },
428 { "_Alignof", RID_ALIGNOF, D_CONLY },
b560fabd 429 { "_Atomic", RID_ATOMIC, D_CONLY },
5c6e5756 430 { "_Bool", RID_BOOL, D_CONLY },
431 { "_Complex", RID_COMPLEX, 0 },
d037099f 432 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
433 { "_Cilk_sync", RID_CILK_SYNC, 0 },
40750995 434 { "_Cilk_for", RID_CILK_FOR, 0 },
c1800156 435 { "_Imaginary", RID_IMAGINARY, D_CONLY },
5c6e5756 436 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
437 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
438 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
439 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
440 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
441 { "_Sat", RID_SAT, D_CONLY | D_EXT },
f80e7755 442 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
985c6e3a 443 { "_Noreturn", RID_NORETURN, D_CONLY },
7aa04c8d 444 { "_Generic", RID_GENERIC, D_CONLY },
d184e0c0 445 { "_Thread_local", RID_THREAD, D_CONLY },
5c6e5756 446 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
447 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
448 { "__alignof", RID_ALIGNOF, 0 },
449 { "__alignof__", RID_ALIGNOF, 0 },
450 { "__asm", RID_ASM, 0 },
451 { "__asm__", RID_ASM, 0 },
452 { "__attribute", RID_ATTRIBUTE, 0 },
453 { "__attribute__", RID_ATTRIBUTE, 0 },
4fba5eb9 454 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
e6014a82 455 { "__bases", RID_BASES, D_CXXONLY },
6f58cf06 456 { "__builtin_call_with_static_chain",
457 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
5c6e5756 458 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
bff4ad11 459 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
bf0cb017 460 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
5c6e5756 461 { "__builtin_offsetof", RID_OFFSETOF, 0 },
462 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
463 { "__builtin_va_arg", RID_VA_ARG, 0 },
464 { "__complex", RID_COMPLEX, 0 },
465 { "__complex__", RID_COMPLEX, 0 },
466 { "__const", RID_CONST, 0 },
467 { "__const__", RID_CONST, 0 },
468 { "__decltype", RID_DECLTYPE, D_CXXONLY },
e6014a82 469 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
5c6e5756 470 { "__extension__", RID_EXTENSION, 0 },
471 { "__func__", RID_C99_FUNCTION_NAME, 0 },
472 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
473 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
474 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
475 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
476 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
477 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
478 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
479 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
23407dc9 480 { "__imag", RID_IMAGPART, 0 },
481 { "__imag__", RID_IMAGPART, 0 },
482 { "__inline", RID_INLINE, 0 },
483 { "__inline__", RID_INLINE, 0 },
5c6e5756 484 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
485 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
486 { "__is_class", RID_IS_CLASS, D_CXXONLY },
5c6e5756 487 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
488 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
aa4313eb 489 { "__is_final", RID_IS_FINAL, D_CXXONLY },
23407dc9 490 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
5c6e5756 491 { "__is_pod", RID_IS_POD, D_CXXONLY },
492 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
c1c67b4f 493 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
494 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
f76a9aa8 495 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
496 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
717e52f9 497 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
5c6e5756 498 { "__is_union", RID_IS_UNION, D_CXXONLY },
5c6e5756 499 { "__label__", RID_LABEL, 0 },
500 { "__null", RID_NULL, 0 },
501 { "__real", RID_REALPART, 0 },
502 { "__real__", RID_REALPART, 0 },
503 { "__restrict", RID_RESTRICT, 0 },
504 { "__restrict__", RID_RESTRICT, 0 },
505 { "__signed", RID_SIGNED, 0 },
506 { "__signed__", RID_SIGNED, 0 },
507 { "__thread", RID_THREAD, 0 },
4c0315d0 508 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
509 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
510 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
5c6e5756 511 { "__typeof", RID_TYPEOF, 0 },
512 { "__typeof__", RID_TYPEOF, 0 },
23407dc9 513 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
5c6e5756 514 { "__volatile", RID_VOLATILE, 0 },
515 { "__volatile__", RID_VOLATILE, 0 },
ffcdbf9c 516 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX0X | D_CXXWARN },
e463efd7 517 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX0X | D_CXXWARN },
5c6e5756 518 { "asm", RID_ASM, D_ASM },
519 { "auto", RID_AUTO, 0 },
dbd982c9 520 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
5c6e5756 521 { "break", RID_BREAK, 0 },
522 { "case", RID_CASE, 0 },
51030405 523 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
5c6e5756 524 { "char", RID_CHAR, 0 },
51030405 525 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX0X | D_CXXWARN },
526 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX0X | D_CXXWARN },
527 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
5c6e5756 528 { "const", RID_CONST, 0 },
17814aca 529 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX0X | D_CXXWARN },
5c6e5756 530 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
531 { "continue", RID_CONTINUE, 0 },
51030405 532 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX0X | D_CXXWARN },
5c6e5756 533 { "default", RID_DEFAULT, 0 },
51030405 534 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
5c6e5756 535 { "do", RID_DO, 0 },
536 { "double", RID_DOUBLE, 0 },
537 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
538 { "else", RID_ELSE, 0 },
539 { "enum", RID_ENUM, 0 },
51030405 540 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
541 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
5c6e5756 542 { "extern", RID_EXTERN, 0 },
51030405 543 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
5c6e5756 544 { "float", RID_FLOAT, 0 },
545 { "for", RID_FOR, 0 },
51030405 546 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
5c6e5756 547 { "goto", RID_GOTO, 0 },
548 { "if", RID_IF, 0 },
549 { "inline", RID_INLINE, D_EXT89 },
550 { "int", RID_INT, 0 },
551 { "long", RID_LONG, 0 },
552 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
51030405 553 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
554 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
98fe9664 555 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX0X | D_CXXWARN },
6fe11077 556 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX0X | D_CXXWARN },
51030405 557 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
558 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
559 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
560 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
5c6e5756 561 { "register", RID_REGISTER, 0 },
562 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
563 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
564 { "return", RID_RETURN, 0 },
565 { "short", RID_SHORT, 0 },
566 { "signed", RID_SIGNED, 0 },
567 { "sizeof", RID_SIZEOF, 0 },
568 { "static", RID_STATIC, 0 },
569 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX0X | D_CXXWARN },
570 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
571 { "struct", RID_STRUCT, 0 },
572 { "switch", RID_SWITCH, 0 },
51030405 573 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
574 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
3740094c 575 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX0X | D_CXXWARN },
51030405 576 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
577 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
578 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
5c6e5756 579 { "typedef", RID_TYPEDEF, 0 },
51030405 580 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
581 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
5c6e5756 582 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
583 { "union", RID_UNION, 0 },
584 { "unsigned", RID_UNSIGNED, 0 },
51030405 585 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
586 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
5c6e5756 587 { "void", RID_VOID, 0 },
588 { "volatile", RID_VOLATILE, 0 },
589 { "wchar_t", RID_WCHAR, D_CXXONLY },
590 { "while", RID_WHILE, 0 },
591 /* These Objective-C keywords are recognized only immediately after
592 an '@'. */
593 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
594 { "defs", RID_AT_DEFS, D_OBJC },
595 { "encode", RID_AT_ENCODE, D_OBJC },
596 { "end", RID_AT_END, D_OBJC },
597 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
598 { "interface", RID_AT_INTERFACE, D_OBJC },
599 { "protocol", RID_AT_PROTOCOL, D_OBJC },
600 { "selector", RID_AT_SELECTOR, D_OBJC },
601 { "finally", RID_AT_FINALLY, D_OBJC },
602 { "synchronized", RID_AT_SYNCHRONIZED, D_OBJC },
069761fb 603 { "optional", RID_AT_OPTIONAL, D_OBJC },
604 { "required", RID_AT_REQUIRED, D_OBJC },
86c110ac 605 { "property", RID_AT_PROPERTY, D_OBJC },
4a8875ed 606 { "package", RID_AT_PACKAGE, D_OBJC },
e1f293c0 607 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
608 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
5c6e5756 609 /* These are recognized only in protocol-qualifier context
610 (see above) */
611 { "bycopy", RID_BYCOPY, D_OBJC },
612 { "byref", RID_BYREF, D_OBJC },
613 { "in", RID_IN, D_OBJC },
614 { "inout", RID_INOUT, D_OBJC },
615 { "oneway", RID_ONEWAY, D_OBJC },
616 { "out", RID_OUT, D_OBJC },
86c110ac 617 /* These are recognized inside a property attribute list */
7590f0e5 618 { "assign", RID_ASSIGN, D_OBJC },
619 { "copy", RID_COPY, D_OBJC },
86c110ac 620 { "getter", RID_GETTER, D_OBJC },
7590f0e5 621 { "nonatomic", RID_NONATOMIC, D_OBJC },
622 { "readonly", RID_READONLY, D_OBJC },
623 { "readwrite", RID_READWRITE, D_OBJC },
624 { "retain", RID_RETAIN, D_OBJC },
86c110ac 625 { "setter", RID_SETTER, D_OBJC },
5c6e5756 626};
627
628const unsigned int num_c_common_reswords =
629 sizeof c_common_reswords / sizeof (struct c_common_resword);
630
058a1b7a 631/* Table of machine-independent attributes common to all C-like languages.
632
633 All attributes referencing arguments should be additionally processed
634 in chkp_copy_function_type_adding_bounds for correct instrumentation
635 by Pointer Bounds Checker.
636 Current list of processed common attributes: nonnull. */
f8e93a2e 637const struct attribute_spec c_common_attribute_table[] =
638{
ac86af5d 639 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
640 affects_type_identity } */
f8e93a2e 641 { "packed", 0, 0, false, false, false,
ac86af5d 642 handle_packed_attribute , false},
f8e93a2e 643 { "nocommon", 0, 0, true, false, false,
ac86af5d 644 handle_nocommon_attribute, false},
f8e93a2e 645 { "common", 0, 0, true, false, false,
ac86af5d 646 handle_common_attribute, false },
f8e93a2e 647 /* FIXME: logically, noreturn attributes should be listed as
648 "false, true, true" and apply to function types. But implementing this
649 would require all the places in the compiler that use TREE_THIS_VOLATILE
650 on a decl to identify non-returning functions to be located and fixed
651 to check the function type instead. */
652 { "noreturn", 0, 0, true, false, false,
ac86af5d 653 handle_noreturn_attribute, false },
f8e93a2e 654 { "volatile", 0, 0, true, false, false,
ac86af5d 655 handle_noreturn_attribute, false },
947aa916 656 { "stack_protect", 0, 0, true, false, false,
657 handle_stack_protect_attribute, false },
f8e93a2e 658 { "noinline", 0, 0, true, false, false,
ac86af5d 659 handle_noinline_attribute, false },
bdb1f0d1 660 { "noclone", 0, 0, true, false, false,
ac86af5d 661 handle_noclone_attribute, false },
85fbea97 662 { "no_icf", 0, 0, true, false, false,
663 handle_noicf_attribute, false },
7bd95dfd 664 { "leaf", 0, 0, true, false, false,
ac86af5d 665 handle_leaf_attribute, false },
f8e93a2e 666 { "always_inline", 0, 0, true, false, false,
ac86af5d 667 handle_always_inline_attribute, false },
541e4101 668 { "gnu_inline", 0, 0, true, false, false,
ac86af5d 669 handle_gnu_inline_attribute, false },
1b16fc45 670 { "artificial", 0, 0, true, false, false,
ac86af5d 671 handle_artificial_attribute, false },
0cdd9887 672 { "flatten", 0, 0, true, false, false,
ac86af5d 673 handle_flatten_attribute, false },
f8e93a2e 674 { "used", 0, 0, true, false, false,
ac86af5d 675 handle_used_attribute, false },
f8e93a2e 676 { "unused", 0, 0, false, false, false,
ac86af5d 677 handle_unused_attribute, false },
62eec3b4 678 { "externally_visible", 0, 0, true, false, false,
ac86af5d 679 handle_externally_visible_attribute, false },
6b722052 680 { "no_reorder", 0, 0, true, false, false,
681 handle_no_reorder_attribute, false },
f8e93a2e 682 /* The same comments as for noreturn attributes apply to const ones. */
683 { "const", 0, 0, true, false, false,
ac86af5d 684 handle_const_attribute, false },
f8e93a2e 685 { "transparent_union", 0, 0, false, false, false,
ac86af5d 686 handle_transparent_union_attribute, false },
9af7fd5b 687 { "constructor", 0, 1, true, false, false,
ac86af5d 688 handle_constructor_attribute, false },
9af7fd5b 689 { "destructor", 0, 1, true, false, false,
ac86af5d 690 handle_destructor_attribute, false },
f8e93a2e 691 { "mode", 1, 1, false, true, false,
ac86af5d 692 handle_mode_attribute, false },
f8e93a2e 693 { "section", 1, 1, true, false, false,
ac86af5d 694 handle_section_attribute, false },
f8e93a2e 695 { "aligned", 0, 1, false, false, false,
ac86af5d 696 handle_aligned_attribute, false },
f8e93a2e 697 { "weak", 0, 0, true, false, false,
ac86af5d 698 handle_weak_attribute, false },
4641882f 699 { "noplt", 0, 0, true, false, false,
700 handle_noplt_attribute, false },
85c0a25c 701 { "ifunc", 1, 1, true, false, false,
ac86af5d 702 handle_ifunc_attribute, false },
f8e93a2e 703 { "alias", 1, 1, true, false, false,
ac86af5d 704 handle_alias_attribute, false },
f4a30bd7 705 { "weakref", 0, 1, true, false, false,
ac86af5d 706 handle_weakref_attribute, false },
f8e93a2e 707 { "no_instrument_function", 0, 0, true, false, false,
ac86af5d 708 handle_no_instrument_function_attribute,
709 false },
f8e93a2e 710 { "malloc", 0, 0, true, false, false,
ac86af5d 711 handle_malloc_attribute, false },
26d1c5ff 712 { "returns_twice", 0, 0, true, false, false,
ac86af5d 713 handle_returns_twice_attribute, false },
f8e93a2e 714 { "no_stack_limit", 0, 0, true, false, false,
ac86af5d 715 handle_no_limit_stack_attribute, false },
f8e93a2e 716 { "pure", 0, 0, true, false, false,
ac86af5d 717 handle_pure_attribute, false },
4c0315d0 718 { "transaction_callable", 0, 0, false, true, false,
719 handle_tm_attribute, false },
720 { "transaction_unsafe", 0, 0, false, true, false,
721 handle_tm_attribute, false },
722 { "transaction_safe", 0, 0, false, true, false,
723 handle_tm_attribute, false },
724 { "transaction_may_cancel_outer", 0, 0, false, true, false,
725 handle_tm_attribute, false },
726 /* ??? These two attributes didn't make the transition from the
727 Intel language document to the multi-vendor language document. */
728 { "transaction_pure", 0, 0, false, true, false,
729 handle_tm_attribute, false },
730 { "transaction_wrap", 1, 1, true, false, false,
731 handle_tm_wrap_attribute, false },
fc09b200 732 /* For internal use (marking of builtins) only. The name contains space
733 to prevent its usage in source code. */
734 { "no vops", 0, 0, true, false, false,
ac86af5d 735 handle_novops_attribute, false },
45c4e798 736 { "deprecated", 0, 1, false, false, false,
ac86af5d 737 handle_deprecated_attribute, false },
f8e93a2e 738 { "vector_size", 1, 1, false, true, false,
ac86af5d 739 handle_vector_size_attribute, false },
b212f378 740 { "visibility", 1, 1, false, false, false,
ac86af5d 741 handle_visibility_attribute, false },
24dfead4 742 { "tls_model", 1, 1, true, false, false,
ac86af5d 743 handle_tls_model_attribute, false },
dbf6c367 744 { "nonnull", 0, -1, false, true, true,
ac86af5d 745 handle_nonnull_attribute, false },
fa987697 746 { "nothrow", 0, 0, true, false, false,
ac86af5d 747 handle_nothrow_attribute, false },
748 { "may_alias", 0, 0, false, true, false, NULL, false },
7acb29a3 749 { "cleanup", 1, 1, true, false, false,
ac86af5d 750 handle_cleanup_attribute, false },
8a8cdb8d 751 { "warn_unused_result", 0, 0, false, true, true,
ac86af5d 752 handle_warn_unused_result_attribute, false },
50ca527f 753 { "sentinel", 0, 1, false, true, true,
ac86af5d 754 handle_sentinel_attribute, false },
b5c26b42 755 /* For internal use (marking of builtins) only. The name contains space
756 to prevent its usage in source code. */
757 { "type generic", 0, 0, false, true, true,
ac86af5d 758 handle_type_generic_attribute, false },
4a29c97c 759 { "alloc_size", 1, 2, false, true, true,
ac86af5d 760 handle_alloc_size_attribute, false },
5de92639 761 { "cold", 0, 0, true, false, false,
ac86af5d 762 handle_cold_attribute, false },
5de92639 763 { "hot", 0, 0, true, false, false,
ac86af5d 764 handle_hot_attribute, false },
d413ffdd 765 { "no_address_safety_analysis",
766 0, 0, true, false, false,
767 handle_no_address_safety_analysis_attribute,
768 false },
a9196da9 769 { "no_sanitize_address", 0, 0, true, false, false,
770 handle_no_sanitize_address_attribute,
771 false },
d1e96383 772 { "no_sanitize_thread", 0, 0, true, false, false,
773 handle_no_sanitize_address_attribute,
774 false },
05f893e1 775 { "no_sanitize_undefined", 0, 0, true, false, false,
776 handle_no_sanitize_undefined_attribute,
777 false },
10fc867f 778 { "warning", 1, 1, true, false, false,
ac86af5d 779 handle_error_attribute, false },
10fc867f 780 { "error", 1, 1, true, false, false,
ac86af5d 781 handle_error_attribute, false },
24470055 782 { "target", 1, -1, true, false, false,
ac86af5d 783 handle_target_attribute, false },
46f8e3b0 784 { "optimize", 1, -1, true, false, false,
ac86af5d 785 handle_optimize_attribute, false },
4c0315d0 786 /* For internal use only. The leading '*' both prevents its usage in
787 source code and signals that it may be overridden by machine tables. */
788 { "*tm regparm", 0, 0, false, true, true,
789 ignore_attribute, false },
48b14f50 790 { "no_split_stack", 0, 0, true, false, false,
ac86af5d 791 handle_no_split_stack_attribute, false },
8ce86007 792 /* For internal use (marking of builtins and runtime functions) only.
793 The name contains space to prevent its usage in source code. */
794 { "fn spec", 1, 1, false, true, true,
ac86af5d 795 handle_fnspec_attribute, false },
a96c3cc1 796 { "warn_unused", 0, 0, false, false, false,
797 handle_warn_unused_attribute, false },
d7dcba40 798 { "returns_nonnull", 0, 0, false, true, true,
799 handle_returns_nonnull_attribute, false },
bc7bff74 800 { "omp declare simd", 0, -1, true, false, false,
801 handle_omp_declare_simd_attribute, false },
74acc703 802 { "cilk simd function", 0, -1, true, false, false,
803 handle_omp_declare_simd_attribute, false },
bc7bff74 804 { "omp declare target", 0, 0, true, false, false,
805 handle_omp_declare_target_attribute, false },
237e78b1 806 { "alloc_align", 1, 1, false, true, true,
807 handle_alloc_align_attribute, false },
808 { "assume_aligned", 1, 2, false, true, true,
809 handle_assume_aligned_attribute, false },
74691f46 810 { "designated_init", 0, 0, false, true, false,
811 handle_designated_init_attribute, false },
058a1b7a 812 { "bnd_variable_size", 0, 0, true, false, false,
813 handle_bnd_variable_size_attribute, false },
814 { "bnd_legacy", 0, 0, true, false, false,
815 handle_bnd_legacy, false },
816 { "bnd_instrument", 0, 0, true, false, false,
817 handle_bnd_instrument, false },
ac86af5d 818 { NULL, 0, 0, false, false, false, NULL, false }
f8e93a2e 819};
820
821/* Give the specifications for the format attributes, used by C and all
058a1b7a 822 descendants.
f8e93a2e 823
058a1b7a 824 All attributes referencing arguments should be additionally processed
825 in chkp_copy_function_type_adding_bounds for correct instrumentation
826 by Pointer Bounds Checker.
827 Current list of processed format attributes: format, format_arg. */
f8e93a2e 828const struct attribute_spec c_common_format_attribute_table[] =
829{
ac86af5d 830 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
831 affects_type_identity } */
f8e93a2e 832 { "format", 3, 3, false, true, true,
ac86af5d 833 handle_format_attribute, false },
f8e93a2e 834 { "format_arg", 1, 1, false, true, true,
ac86af5d 835 handle_format_arg_attribute, false },
836 { NULL, 0, 0, false, false, false, NULL, false }
f8e93a2e 837};
838
6d5d708e 839/* Return identifier for address space AS. */
34208e18 840
6d5d708e 841const char *
842c_addr_space_name (addr_space_t as)
843{
34208e18 844 int rid = RID_FIRST_ADDR_SPACE + as;
845 gcc_assert (ridpointers [rid]);
846 return IDENTIFIER_POINTER (ridpointers [rid]);
6d5d708e 847}
848
2c0e001b 849/* Push current bindings for the function name VAR_DECLS. */
f4e3c278 850
851void
1cae46be 852start_fname_decls (void)
f4e3c278 853{
65b7f83f 854 unsigned ix;
855 tree saved = NULL_TREE;
1cae46be 856
65b7f83f 857 for (ix = 0; fname_vars[ix].decl; ix++)
858 {
859 tree decl = *fname_vars[ix].decl;
f4e3c278 860
65b7f83f 861 if (decl)
862 {
ceb7b692 863 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
864 saved);
65b7f83f 865 *fname_vars[ix].decl = NULL_TREE;
866 }
867 }
868 if (saved || saved_function_name_decls)
869 /* Normally they'll have been NULL, so only push if we've got a
870 stack, or they are non-NULL. */
871 saved_function_name_decls = tree_cons (saved, NULL_TREE,
872 saved_function_name_decls);
873}
874
2363ef00 875/* Finish up the current bindings, adding them into the current function's
876 statement tree. This must be done _before_ finish_stmt_tree is called.
877 If there is no current function, we must be at file scope and no statements
878 are involved. Pop the previous bindings. */
65b7f83f 879
880void
1cae46be 881finish_fname_decls (void)
65b7f83f 882{
883 unsigned ix;
2363ef00 884 tree stmts = NULL_TREE;
65b7f83f 885 tree stack = saved_function_name_decls;
886
887 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
2363ef00 888 append_to_statement_list (TREE_VALUE (stack), &stmts);
1cae46be 889
2363ef00 890 if (stmts)
65b7f83f 891 {
2363ef00 892 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
5c423bd6 893
2363ef00 894 if (TREE_CODE (*bodyp) == BIND_EXPR)
895 bodyp = &BIND_EXPR_BODY (*bodyp);
81010c97 896
bc2b76e0 897 append_to_statement_list_force (*bodyp, &stmts);
2363ef00 898 *bodyp = stmts;
65b7f83f 899 }
1cae46be 900
65b7f83f 901 for (ix = 0; fname_vars[ix].decl; ix++)
902 *fname_vars[ix].decl = NULL_TREE;
1cae46be 903
65b7f83f 904 if (stack)
f4e3c278 905 {
2c0e001b 906 /* We had saved values, restore them. */
65b7f83f 907 tree saved;
908
909 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
910 {
911 tree decl = TREE_PURPOSE (saved);
f9ae6f95 912 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
1cae46be 913
65b7f83f 914 *fname_vars[ix].decl = decl;
915 }
916 stack = TREE_CHAIN (stack);
f4e3c278 917 }
65b7f83f 918 saved_function_name_decls = stack;
919}
920
81010c97 921/* Return the text name of the current function, suitably prettified
5fc7fa69 922 by PRETTY_P. Return string must be freed by caller. */
65b7f83f 923
924const char *
1cae46be 925fname_as_string (int pretty_p)
65b7f83f 926{
9ad4bb1e 927 const char *name = "top level";
5fc7fa69 928 char *namep;
8115b8be 929 int vrb = 2, len;
930 cpp_string cstr = { 0, 0 }, strname;
9ad4bb1e 931
84166705 932 if (!pretty_p)
9ad4bb1e 933 {
934 name = "";
935 vrb = 0;
936 }
937
938 if (current_function_decl)
dc24ddbd 939 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
9ad4bb1e 940
8115b8be 941 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
5fc7fa69 942
8115b8be 943 namep = XNEWVEC (char, len);
944 snprintf (namep, len, "\"%s\"", name);
945 strname.text = (unsigned char *) namep;
946 strname.len = len - 1;
5fc7fa69 947
924bbf02 948 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
8115b8be 949 {
950 XDELETEVEC (namep);
951 return (const char *) cstr.text;
5fc7fa69 952 }
5fc7fa69 953
954 return namep;
65b7f83f 955}
956
65b7f83f 957/* Return the VAR_DECL for a const char array naming the current
958 function. If the VAR_DECL has not yet been created, create it
959 now. RID indicates how it should be formatted and IDENTIFIER_NODE
960 ID is its name (unfortunately C and C++ hold the RID values of
961 keywords in different places, so we can't derive RID from ID in
e3b80d49 962 this language independent code. LOC is the location of the
963 function. */
65b7f83f 964
965tree
e3b80d49 966fname_decl (location_t loc, unsigned int rid, tree id)
65b7f83f 967{
968 unsigned ix;
969 tree decl = NULL_TREE;
970
971 for (ix = 0; fname_vars[ix].decl; ix++)
972 if (fname_vars[ix].rid == rid)
973 break;
974
975 decl = *fname_vars[ix].decl;
976 if (!decl)
f4e3c278 977 {
2222b3c6 978 /* If a tree is built here, it would normally have the lineno of
979 the current statement. Later this tree will be moved to the
980 beginning of the function and this line number will be wrong.
981 To avoid this problem set the lineno to 0 here; that prevents
7299020b 982 it from appearing in the RTL. */
2363ef00 983 tree stmts;
9a6486a6 984 location_t saved_location = input_location;
9a6486a6 985 input_location = UNKNOWN_LOCATION;
1cae46be 986
2363ef00 987 stmts = push_stmt_list ();
e60a6f7b 988 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
2363ef00 989 stmts = pop_stmt_list (stmts);
990 if (!IS_EMPTY_STMT (stmts))
991 saved_function_name_decls
992 = tree_cons (decl, stmts, saved_function_name_decls);
65b7f83f 993 *fname_vars[ix].decl = decl;
9a6486a6 994 input_location = saved_location;
f4e3c278 995 }
65b7f83f 996 if (!ix && !current_function_decl)
e3b80d49 997 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
81010c97 998
65b7f83f 999 return decl;
f4e3c278 1000}
1001
070236f0 1002/* Given a STRING_CST, give it a suitable array-of-chars data type. */
b0fc3e72 1003
1004tree
1cae46be 1005fix_string_type (tree value)
b0fc3e72 1006{
070236f0 1007 int length = TREE_STRING_LENGTH (value);
1008 int nchars;
00d26680 1009 tree e_type, i_type, a_type;
1010
73be5127 1011 /* Compute the number of elements, for the array type. */
924bbf02 1012 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1013 {
1014 nchars = length;
1015 e_type = char_type_node;
1016 }
1017 else if (TREE_TYPE (value) == char16_array_type_node)
1018 {
1019 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1020 e_type = char16_type_node;
1021 }
1022 else if (TREE_TYPE (value) == char32_array_type_node)
1023 {
1024 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1025 e_type = char32_type_node;
1026 }
1027 else
1028 {
1029 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1030 e_type = wchar_type_node;
1031 }
b0fc3e72 1032
1d752508 1033 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1034 limit in C++98 Annex B is very large (65536) and is not normative,
1035 so we do not diagnose it (warn_overlength_strings is forced off
1036 in c_common_post_options). */
1037 if (warn_overlength_strings)
1038 {
1039 const int nchars_max = flag_isoc99 ? 4095 : 509;
1040 const int relevant_std = flag_isoc99 ? 99 : 90;
1041 if (nchars - 1 > nchars_max)
1042 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1043 separate the %d from the 'C'. 'ISO' should not be
1044 translated, but it may be moved after 'C%d' in languages
1045 where modifiers follow nouns. */
21ca8540 1046 pedwarn (input_location, OPT_Woverlength_strings,
8864917d 1047 "string length %qd is greater than the length %qd "
1d752508 1048 "ISO C%d compilers are required to support",
1049 nchars - 1, nchars_max, relevant_std);
1050 }
82cfc7f7 1051
390be14e 1052 /* Create the array type for the string constant. The ISO C++
1053 standard says that a string literal has type `const char[N]' or
1054 `const wchar_t[N]'. We use the same logic when invoked as a C
1055 front-end with -Wwrite-strings.
1056 ??? We should change the type of an expression depending on the
1057 state of a warning flag. We should just be warning -- see how
1058 this is handled in the C++ front-end for the deprecated implicit
1059 conversion from string literals to `char*' or `wchar_t*'.
00d26680 1060
1061 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1062 array type being the unqualified version of that type.
1063 Therefore, if we are constructing an array of const char, we must
1064 construct the matching unqualified array type first. The C front
1065 end does not require this, but it does no harm, so we do it
1066 unconditionally. */
ceb7b692 1067 i_type = build_index_type (size_int (nchars - 1));
00d26680 1068 a_type = build_array_type (e_type, i_type);
390be14e 1069 if (c_dialect_cxx() || warn_write_strings)
aebc8537 1070 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
3a10ba35 1071
00d26680 1072 TREE_TYPE (value) = a_type;
b8e3b7ad 1073 TREE_CONSTANT (value) = 1;
a814bad5 1074 TREE_READONLY (value) = 1;
b0fc3e72 1075 TREE_STATIC (value) = 1;
1076 return value;
1077}
1078\f
fc501191 1079/* If DISABLE is true, stop issuing warnings. This is used when
1080 parsing code that we know will not be executed. This function may
1081 be called multiple times, and works as a stack. */
1082
1083static void
1084c_disable_warnings (bool disable)
1085{
1086 if (disable)
1087 {
1088 ++c_inhibit_evaluation_warnings;
1089 fold_defer_overflow_warnings ();
1090 }
1091}
1092
1093/* If ENABLE is true, reenable issuing warnings. */
1094
1095static void
1096c_enable_warnings (bool enable)
1097{
1098 if (enable)
1099 {
1100 --c_inhibit_evaluation_warnings;
1101 fold_undefer_and_ignore_overflow_warnings ();
1102 }
1103}
1104
a75b1c71 1105/* Fully fold EXPR, an expression that was not folded (beyond integer
1106 constant expressions and null pointer constants) when being built
1107 up. If IN_INIT, this is in a static initializer and certain
1108 changes are made to the folding done. Clear *MAYBE_CONST if
1109 MAYBE_CONST is not NULL and EXPR is definitely not a constant
1110 expression because it contains an evaluated operator (in C99) or an
1111 operator outside of sizeof returning an integer constant (in C90)
1112 not permitted in constant expressions, or because it contains an
1113 evaluated arithmetic overflow. (*MAYBE_CONST should typically be
1114 set to true by callers before calling this function.) Return the
1115 folded expression. Function arguments have already been folded
1116 before calling this function, as have the contents of SAVE_EXPR,
1117 TARGET_EXPR, BIND_EXPR, VA_ARG_EXPR, OBJ_TYPE_REF and
1118 C_MAYBE_CONST_EXPR. */
1119
1120tree
1121c_fully_fold (tree expr, bool in_init, bool *maybe_const)
1122{
1123 tree ret;
c6418a4e 1124 tree eptype = NULL_TREE;
a75b1c71 1125 bool dummy = true;
1126 bool maybe_const_itself = true;
389dd41b 1127 location_t loc = EXPR_LOCATION (expr);
a75b1c71 1128
1129 /* This function is not relevant to C++ because C++ folds while
1130 parsing, and may need changes to be correct for C++ when C++
1131 stops folding while parsing. */
1132 if (c_dialect_cxx ())
1133 gcc_unreachable ();
1134
1135 if (!maybe_const)
1136 maybe_const = &dummy;
c6418a4e 1137 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1138 {
1139 eptype = TREE_TYPE (expr);
1140 expr = TREE_OPERAND (expr, 0);
1141 }
a75b1c71 1142 ret = c_fully_fold_internal (expr, in_init, maybe_const,
aac24642 1143 &maybe_const_itself, false);
c6418a4e 1144 if (eptype)
389dd41b 1145 ret = fold_convert_loc (loc, eptype, ret);
a75b1c71 1146 *maybe_const &= maybe_const_itself;
1147 return ret;
1148}
1149
1150/* Internal helper for c_fully_fold. EXPR and IN_INIT are as for
1151 c_fully_fold. *MAYBE_CONST_OPERANDS is cleared because of operands
1152 not permitted, while *MAYBE_CONST_ITSELF is cleared because of
1153 arithmetic overflow (for C90, *MAYBE_CONST_OPERANDS is carried from
1154 both evaluated and unevaluated subexpressions while
1155 *MAYBE_CONST_ITSELF is carried from only evaluated
aac24642 1156 subexpressions). FOR_INT_CONST indicates if EXPR is an expression
1157 with integer constant operands, and if any of the operands doesn't
1158 get folded to an integer constant, don't fold the expression itself. */
a75b1c71 1159
1160static tree
1161c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands,
aac24642 1162 bool *maybe_const_itself, bool for_int_const)
a75b1c71 1163{
1164 tree ret = expr;
1165 enum tree_code code = TREE_CODE (expr);
1166 enum tree_code_class kind = TREE_CODE_CLASS (code);
1167 location_t loc = EXPR_LOCATION (expr);
1168 tree op0, op1, op2, op3;
1169 tree orig_op0, orig_op1, orig_op2;
1170 bool op0_const = true, op1_const = true, op2_const = true;
1171 bool op0_const_self = true, op1_const_self = true, op2_const_self = true;
1172 bool nowarning = TREE_NO_WARNING (expr);
fc501191 1173 bool unused_p;
a75b1c71 1174
1175 /* This function is not relevant to C++ because C++ folds while
1176 parsing, and may need changes to be correct for C++ when C++
1177 stops folding while parsing. */
1178 if (c_dialect_cxx ())
1179 gcc_unreachable ();
1180
1181 /* Constants, declarations, statements, errors, SAVE_EXPRs and
1182 anything else not counted as an expression cannot usefully be
1183 folded further at this point. */
1184 if (!IS_EXPR_CODE_CLASS (kind)
1185 || kind == tcc_statement
1186 || code == SAVE_EXPR)
1187 return expr;
1188
1189 /* Operands of variable-length expressions (function calls) have
1190 already been folded, as have __builtin_* function calls, and such
1191 expressions cannot occur in constant expressions. */
1192 if (kind == tcc_vl_exp)
1193 {
1194 *maybe_const_operands = false;
1195 ret = fold (expr);
1196 goto out;
1197 }
1198
1199 if (code == C_MAYBE_CONST_EXPR)
1200 {
1201 tree pre = C_MAYBE_CONST_EXPR_PRE (expr);
1202 tree inner = C_MAYBE_CONST_EXPR_EXPR (expr);
1203 if (C_MAYBE_CONST_EXPR_NON_CONST (expr))
1204 *maybe_const_operands = false;
1205 if (C_MAYBE_CONST_EXPR_INT_OPERANDS (expr))
aac24642 1206 {
1207 *maybe_const_itself = false;
1208 inner = c_fully_fold_internal (inner, in_init, maybe_const_operands,
1209 maybe_const_itself, true);
1210 }
a75b1c71 1211 if (pre && !in_init)
1212 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr), pre, inner);
1213 else
1214 ret = inner;
1215 goto out;
1216 }
1217
1218 /* Assignment, increment, decrement, function call and comma
1219 operators, and statement expressions, cannot occur in constant
1220 expressions if evaluated / outside of sizeof. (Function calls
1221 were handled above, though VA_ARG_EXPR is treated like a function
1222 call here, and statement expressions are handled through
1223 C_MAYBE_CONST_EXPR to avoid folding inside them.) */
1224 switch (code)
1225 {
1226 case MODIFY_EXPR:
1227 case PREDECREMENT_EXPR:
1228 case PREINCREMENT_EXPR:
1229 case POSTDECREMENT_EXPR:
1230 case POSTINCREMENT_EXPR:
1231 case COMPOUND_EXPR:
1232 *maybe_const_operands = false;
1233 break;
1234
1235 case VA_ARG_EXPR:
1236 case TARGET_EXPR:
1237 case BIND_EXPR:
1238 case OBJ_TYPE_REF:
1239 *maybe_const_operands = false;
1240 ret = fold (expr);
1241 goto out;
1242
1243 default:
1244 break;
1245 }
1246
1247 /* Fold individual tree codes as appropriate. */
1248 switch (code)
1249 {
1250 case COMPOUND_LITERAL_EXPR:
1251 /* Any non-constancy will have been marked in a containing
1252 C_MAYBE_CONST_EXPR; there is no more folding to do here. */
1253 goto out;
1254
1255 case COMPONENT_REF:
1256 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1257 op1 = TREE_OPERAND (expr, 1);
1258 op2 = TREE_OPERAND (expr, 2);
1259 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1260 maybe_const_itself, for_int_const);
f59e3889 1261 STRIP_TYPE_NOPS (op0);
a75b1c71 1262 if (op0 != orig_op0)
1263 ret = build3 (COMPONENT_REF, TREE_TYPE (expr), op0, op1, op2);
1264 if (ret != expr)
1265 {
1266 TREE_READONLY (ret) = TREE_READONLY (expr);
1267 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1268 }
1269 goto out;
1270
1271 case ARRAY_REF:
1272 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1273 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1274 op2 = TREE_OPERAND (expr, 2);
1275 op3 = TREE_OPERAND (expr, 3);
1276 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1277 maybe_const_itself, for_int_const);
f59e3889 1278 STRIP_TYPE_NOPS (op0);
a75b1c71 1279 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
aac24642 1280 maybe_const_itself, for_int_const);
f59e3889 1281 STRIP_TYPE_NOPS (op1);
a75b1c71 1282 op1 = decl_constant_value_for_optimization (op1);
1283 if (op0 != orig_op0 || op1 != orig_op1)
1284 ret = build4 (ARRAY_REF, TREE_TYPE (expr), op0, op1, op2, op3);
1285 if (ret != expr)
1286 {
1287 TREE_READONLY (ret) = TREE_READONLY (expr);
1288 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1289 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1290 }
1291 ret = fold (ret);
1292 goto out;
1293
1294 case COMPOUND_EXPR:
1295 case MODIFY_EXPR:
1296 case PREDECREMENT_EXPR:
1297 case PREINCREMENT_EXPR:
1298 case POSTDECREMENT_EXPR:
1299 case POSTINCREMENT_EXPR:
1300 case PLUS_EXPR:
1301 case MINUS_EXPR:
1302 case MULT_EXPR:
1303 case POINTER_PLUS_EXPR:
1304 case TRUNC_DIV_EXPR:
1305 case CEIL_DIV_EXPR:
1306 case FLOOR_DIV_EXPR:
1307 case TRUNC_MOD_EXPR:
1308 case RDIV_EXPR:
1309 case EXACT_DIV_EXPR:
1310 case LSHIFT_EXPR:
1311 case RSHIFT_EXPR:
1312 case BIT_IOR_EXPR:
1313 case BIT_XOR_EXPR:
1314 case BIT_AND_EXPR:
1315 case LT_EXPR:
1316 case LE_EXPR:
1317 case GT_EXPR:
1318 case GE_EXPR:
1319 case EQ_EXPR:
1320 case NE_EXPR:
1321 case COMPLEX_EXPR:
1322 case TRUTH_AND_EXPR:
1323 case TRUTH_OR_EXPR:
1324 case TRUTH_XOR_EXPR:
1325 case UNORDERED_EXPR:
1326 case ORDERED_EXPR:
1327 case UNLT_EXPR:
1328 case UNLE_EXPR:
1329 case UNGT_EXPR:
1330 case UNGE_EXPR:
1331 case UNEQ_EXPR:
1332 /* Binary operations evaluating both arguments (increment and
1333 decrement are binary internally in GCC). */
1334 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1335 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1336 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1337 maybe_const_itself, for_int_const);
f59e3889 1338 STRIP_TYPE_NOPS (op0);
a75b1c71 1339 if (code != MODIFY_EXPR
1340 && code != PREDECREMENT_EXPR
1341 && code != PREINCREMENT_EXPR
1342 && code != POSTDECREMENT_EXPR
1343 && code != POSTINCREMENT_EXPR)
1344 op0 = decl_constant_value_for_optimization (op0);
1345 /* The RHS of a MODIFY_EXPR was fully folded when building that
1346 expression for the sake of conversion warnings. */
1347 if (code != MODIFY_EXPR)
1348 op1 = c_fully_fold_internal (op1, in_init, maybe_const_operands,
aac24642 1349 maybe_const_itself, for_int_const);
f59e3889 1350 STRIP_TYPE_NOPS (op1);
a75b1c71 1351 op1 = decl_constant_value_for_optimization (op1);
aac24642 1352
1353 if (for_int_const && (TREE_CODE (op0) != INTEGER_CST
1354 || TREE_CODE (op1) != INTEGER_CST))
1355 goto out;
1356
a75b1c71 1357 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1358 ret = in_init
389dd41b 1359 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1360 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
a75b1c71 1361 else
1362 ret = fold (expr);
672d914b 1363 if (TREE_OVERFLOW_P (ret)
1364 && !TREE_OVERFLOW_P (op0)
1365 && !TREE_OVERFLOW_P (op1))
1366 overflow_warning (EXPR_LOCATION (expr), ret);
cdc64059 1367 if (code == LSHIFT_EXPR
1368 && TREE_CODE (orig_op0) != INTEGER_CST
1369 && TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1370 && TREE_CODE (op0) == INTEGER_CST
1371 && c_inhibit_evaluation_warnings == 0
1372 && tree_int_cst_sgn (op0) < 0)
1373 warning_at (loc, OPT_Wshift_negative_value,
1374 "left shift of negative value");
7c834436 1375 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1376 && TREE_CODE (orig_op1) != INTEGER_CST
1377 && TREE_CODE (op1) == INTEGER_CST
1378 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1379 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1380 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE
1381 && c_inhibit_evaluation_warnings == 0)
1382 {
1383 if (tree_int_cst_sgn (op1) < 0)
f11bdffb 1384 warning_at (loc, OPT_Wshift_count_negative,
1385 (code == LSHIFT_EXPR
1386 ? G_("left shift count is negative")
1387 : G_("right shift count is negative")));
7c834436 1388 else if (compare_tree_int (op1,
1389 TYPE_PRECISION (TREE_TYPE (orig_op0)))
1390 >= 0)
f11bdffb 1391 warning_at (loc, OPT_Wshift_count_overflow,
1392 (code == LSHIFT_EXPR
1393 ? G_("left shift count >= width of type")
1394 : G_("right shift count >= width of type")));
7c834436 1395 }
b57910fa 1396 if ((code == TRUNC_DIV_EXPR
1397 || code == CEIL_DIV_EXPR
1398 || code == FLOOR_DIV_EXPR
1399 || code == EXACT_DIV_EXPR
1400 || code == TRUNC_MOD_EXPR)
1401 && TREE_CODE (orig_op1) != INTEGER_CST
1402 && TREE_CODE (op1) == INTEGER_CST
1403 && (TREE_CODE (TREE_TYPE (orig_op0)) == INTEGER_TYPE
1404 || TREE_CODE (TREE_TYPE (orig_op0)) == FIXED_POINT_TYPE)
1405 && TREE_CODE (TREE_TYPE (orig_op1)) == INTEGER_TYPE)
1406 warn_for_div_by_zero (loc, op1);
a75b1c71 1407 goto out;
1408
1409 case INDIRECT_REF:
1410 case FIX_TRUNC_EXPR:
1411 case FLOAT_EXPR:
1412 CASE_CONVERT:
e66325ea 1413 case ADDR_SPACE_CONVERT_EXPR:
2b19dfe4 1414 case VIEW_CONVERT_EXPR:
a75b1c71 1415 case NON_LVALUE_EXPR:
1416 case NEGATE_EXPR:
1417 case BIT_NOT_EXPR:
1418 case TRUTH_NOT_EXPR:
1419 case ADDR_EXPR:
1420 case CONJ_EXPR:
1421 case REALPART_EXPR:
1422 case IMAGPART_EXPR:
1423 /* Unary operations. */
1424 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1425 op0 = c_fully_fold_internal (op0, in_init, maybe_const_operands,
aac24642 1426 maybe_const_itself, for_int_const);
f59e3889 1427 STRIP_TYPE_NOPS (op0);
a75b1c71 1428 if (code != ADDR_EXPR && code != REALPART_EXPR && code != IMAGPART_EXPR)
1429 op0 = decl_constant_value_for_optimization (op0);
aac24642 1430
1431 if (for_int_const && TREE_CODE (op0) != INTEGER_CST)
1432 goto out;
1433
737a23cc 1434 /* ??? Cope with user tricks that amount to offsetof. The middle-end is
1435 not prepared to deal with them if they occur in initializers. */
1436 if (op0 != orig_op0
1437 && code == ADDR_EXPR
1438 && (op1 = get_base_address (op0)) != NULL_TREE
1439 && TREE_CODE (op1) == INDIRECT_REF
1440 && TREE_CONSTANT (TREE_OPERAND (op1, 0)))
7549df0d 1441 ret = fold_convert_loc (loc, TREE_TYPE (expr), fold_offsetof_1 (op0));
737a23cc 1442 else if (op0 != orig_op0 || in_init)
a75b1c71 1443 ret = in_init
389dd41b 1444 ? fold_build1_initializer_loc (loc, code, TREE_TYPE (expr), op0)
1445 : fold_build1_loc (loc, code, TREE_TYPE (expr), op0);
a75b1c71 1446 else
1447 ret = fold (expr);
1448 if (code == INDIRECT_REF
1449 && ret != expr
1450 && TREE_CODE (ret) == INDIRECT_REF)
1451 {
1452 TREE_READONLY (ret) = TREE_READONLY (expr);
1453 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (expr);
1454 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (expr);
1455 }
672d914b 1456 switch (code)
1457 {
1458 case FIX_TRUNC_EXPR:
1459 case FLOAT_EXPR:
1460 CASE_CONVERT:
1461 /* Don't warn about explicit conversions. We will already
1462 have warned about suspect implicit conversions. */
1463 break;
1464
1465 default:
1466 if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0))
1467 overflow_warning (EXPR_LOCATION (expr), ret);
1468 break;
1469 }
a75b1c71 1470 goto out;
1471
1472 case TRUTH_ANDIF_EXPR:
1473 case TRUTH_ORIF_EXPR:
1474 /* Binary operations not necessarily evaluating both
1475 arguments. */
1476 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1477 orig_op1 = op1 = TREE_OPERAND (expr, 1);
aac24642 1478 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1479 for_int_const);
f59e3889 1480 STRIP_TYPE_NOPS (op0);
672d914b 1481
1482 unused_p = (op0 == (code == TRUTH_ANDIF_EXPR
1483 ? truthvalue_false_node
1484 : truthvalue_true_node));
fc501191 1485 c_disable_warnings (unused_p);
aac24642 1486 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1487 for_int_const);
f59e3889 1488 STRIP_TYPE_NOPS (op1);
fc501191 1489 c_enable_warnings (unused_p);
672d914b 1490
aac24642 1491 if (for_int_const
1492 && (TREE_CODE (op0) != INTEGER_CST
1493 /* Require OP1 be an INTEGER_CST only if it's evaluated. */
1494 || (!unused_p && TREE_CODE (op1) != INTEGER_CST)))
1495 goto out;
1496
a75b1c71 1497 if (op0 != orig_op0 || op1 != orig_op1 || in_init)
1498 ret = in_init
389dd41b 1499 ? fold_build2_initializer_loc (loc, code, TREE_TYPE (expr), op0, op1)
1500 : fold_build2_loc (loc, code, TREE_TYPE (expr), op0, op1);
a75b1c71 1501 else
1502 ret = fold (expr);
1503 *maybe_const_operands &= op0_const;
1504 *maybe_const_itself &= op0_const_self;
1505 if (!(flag_isoc99
1506 && op0_const
1507 && op0_const_self
1508 && (code == TRUTH_ANDIF_EXPR
1509 ? op0 == truthvalue_false_node
1510 : op0 == truthvalue_true_node)))
1511 *maybe_const_operands &= op1_const;
1512 if (!(op0_const
1513 && op0_const_self
1514 && (code == TRUTH_ANDIF_EXPR
1515 ? op0 == truthvalue_false_node
1516 : op0 == truthvalue_true_node)))
1517 *maybe_const_itself &= op1_const_self;
1518 goto out;
1519
1520 case COND_EXPR:
1521 orig_op0 = op0 = TREE_OPERAND (expr, 0);
1522 orig_op1 = op1 = TREE_OPERAND (expr, 1);
1523 orig_op2 = op2 = TREE_OPERAND (expr, 2);
aac24642 1524 op0 = c_fully_fold_internal (op0, in_init, &op0_const, &op0_const_self,
1525 for_int_const);
672d914b 1526
f59e3889 1527 STRIP_TYPE_NOPS (op0);
fc501191 1528 c_disable_warnings (op0 == truthvalue_false_node);
aac24642 1529 op1 = c_fully_fold_internal (op1, in_init, &op1_const, &op1_const_self,
1530 for_int_const);
f59e3889 1531 STRIP_TYPE_NOPS (op1);
fc501191 1532 c_enable_warnings (op0 == truthvalue_false_node);
672d914b 1533
fc501191 1534 c_disable_warnings (op0 == truthvalue_true_node);
aac24642 1535 op2 = c_fully_fold_internal (op2, in_init, &op2_const, &op2_const_self,
1536 for_int_const);
f59e3889 1537 STRIP_TYPE_NOPS (op2);
fc501191 1538 c_enable_warnings (op0 == truthvalue_true_node);
672d914b 1539
aac24642 1540 if (for_int_const
1541 && (TREE_CODE (op0) != INTEGER_CST
1542 /* Only the evaluated operand must be an INTEGER_CST. */
1543 || (op0 == truthvalue_true_node
1544 ? TREE_CODE (op1) != INTEGER_CST
1545 : TREE_CODE (op2) != INTEGER_CST)))
1546 goto out;
1547
a75b1c71 1548 if (op0 != orig_op0 || op1 != orig_op1 || op2 != orig_op2)
389dd41b 1549 ret = fold_build3_loc (loc, code, TREE_TYPE (expr), op0, op1, op2);
a75b1c71 1550 else
1551 ret = fold (expr);
1552 *maybe_const_operands &= op0_const;
1553 *maybe_const_itself &= op0_const_self;
1554 if (!(flag_isoc99
1555 && op0_const
1556 && op0_const_self
1557 && op0 == truthvalue_false_node))
1558 *maybe_const_operands &= op1_const;
1559 if (!(op0_const
1560 && op0_const_self
1561 && op0 == truthvalue_false_node))
1562 *maybe_const_itself &= op1_const_self;
1563 if (!(flag_isoc99
1564 && op0_const
1565 && op0_const_self
1566 && op0 == truthvalue_true_node))
1567 *maybe_const_operands &= op2_const;
1568 if (!(op0_const
1569 && op0_const_self
1570 && op0 == truthvalue_true_node))
1571 *maybe_const_itself &= op2_const_self;
1572 goto out;
1573
c6418a4e 1574 case EXCESS_PRECISION_EXPR:
1575 /* Each case where an operand with excess precision may be
1576 encountered must remove the EXCESS_PRECISION_EXPR around
1577 inner operands and possibly put one around the whole
1578 expression or possibly convert to the semantic type (which
1579 c_fully_fold does); we cannot tell at this stage which is
1580 appropriate in any particular case. */
1581 gcc_unreachable ();
1582
a75b1c71 1583 default:
1584 /* Various codes may appear through folding built-in functions
1585 and their arguments. */
1586 goto out;
1587 }
1588
1589 out:
1590 /* Some folding may introduce NON_LVALUE_EXPRs; all lvalue checks
1591 have been done by this point, so remove them again. */
1592 nowarning |= TREE_NO_WARNING (ret);
1593 STRIP_TYPE_NOPS (ret);
1594 if (nowarning && !TREE_NO_WARNING (ret))
1595 {
1596 if (!CAN_HAVE_LOCATION_P (ret))
1597 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
1598 TREE_NO_WARNING (ret) = 1;
1599 }
1600 if (ret != expr)
1601 protected_set_expr_location (ret, loc);
1602 return ret;
1603}
1604
1605/* If not optimizing, EXP is not a VAR_DECL, or EXP has array type,
1606 return EXP. Otherwise, return either EXP or its known constant
1607 value (if it has one), but return EXP if EXP has mode BLKmode. ???
1608 Is the BLKmode test appropriate? */
1609
1610tree
1611decl_constant_value_for_optimization (tree exp)
1612{
1613 tree ret;
1614
1615 /* This function is only used by C, for c_fully_fold and other
1616 optimization, and may not be correct for C++. */
1617 if (c_dialect_cxx ())
1618 gcc_unreachable ();
1619
1620 if (!optimize
f48c7f4a 1621 || !VAR_P (exp)
a75b1c71 1622 || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1623 || DECL_MODE (exp) == BLKmode)
1624 return exp;
1625
1626 ret = decl_constant_value (exp);
1627 /* Avoid unwanted tree sharing between the initializer and current
1628 function's body where the tree can be modified e.g. by the
1629 gimplifier. */
1630 if (ret != exp && TREE_STATIC (exp))
1631 ret = unshare_expr (ret);
1632 return ret;
1633}
1634
2a1736ed 1635/* Print a warning if a constant expression had overflow in folding.
1636 Invoke this function on every expression that the language
1637 requires to be a constant expression.
1638 Note the ANSI C standard says it is erroneous for a
1639 constant expression to overflow. */
b2806639 1640
1641void
1cae46be 1642constant_expression_warning (tree value)
07317e69 1643{
48e1416a 1644 if (warn_overflow && pedantic
07317e69 1645 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1646 || TREE_CODE (value) == FIXED_CST
1647 || TREE_CODE (value) == VECTOR_CST
1648 || TREE_CODE (value) == COMPLEX_CST)
1649 && TREE_OVERFLOW (value))
21ca8540 1650 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
07317e69 1651}
1652
1653/* The same as above but print an unconditional error. */
1654void
1655constant_expression_error (tree value)
b2806639 1656{
837e1122 1657 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
9421ebb9 1658 || TREE_CODE (value) == FIXED_CST
886cfd4f 1659 || TREE_CODE (value) == VECTOR_CST
837e1122 1660 || TREE_CODE (value) == COMPLEX_CST)
07317e69 1661 && TREE_OVERFLOW (value))
1662 error ("overflow in constant expression");
2a1736ed 1663}
1664
f170d67f 1665/* Print a warning if an expression had overflow in folding and its
1666 operands hadn't.
1667
2a1736ed 1668 Invoke this function on every expression that
1669 (1) appears in the source code, and
f170d67f 1670 (2) is a constant expression that overflowed, and
2a1736ed 1671 (3) is not already checked by convert_and_check;
f170d67f 1672 however, do not invoke this function on operands of explicit casts
1673 or when the expression is the result of an operator and any operand
1674 already overflowed. */
2a1736ed 1675
1676void
e60a6f7b 1677overflow_warning (location_t loc, tree value)
2a1736ed 1678{
48d94ede 1679 if (c_inhibit_evaluation_warnings != 0)
1680 return;
f170d67f 1681
1682 switch (TREE_CODE (value))
886cfd4f 1683 {
f170d67f 1684 case INTEGER_CST:
e60a6f7b 1685 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
f170d67f 1686 break;
48e1416a 1687
f170d67f 1688 case REAL_CST:
e60a6f7b 1689 warning_at (loc, OPT_Woverflow,
1690 "floating point overflow in expression");
f170d67f 1691 break;
48e1416a 1692
9421ebb9 1693 case FIXED_CST:
e60a6f7b 1694 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
9421ebb9 1695 break;
1696
f170d67f 1697 case VECTOR_CST:
e60a6f7b 1698 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
f170d67f 1699 break;
48e1416a 1700
f170d67f 1701 case COMPLEX_CST:
1702 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
e60a6f7b 1703 warning_at (loc, OPT_Woverflow,
1704 "complex integer overflow in expression");
f170d67f 1705 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
e60a6f7b 1706 warning_at (loc, OPT_Woverflow,
1707 "complex floating point overflow in expression");
f170d67f 1708 break;
1709
1710 default:
1711 break;
886cfd4f 1712 }
2a1736ed 1713}
1714
03033af4 1715/* Warn about uses of logical || / && operator in a context where it
1716 is likely that the bitwise equivalent was intended by the
1717 programmer. We have seen an expression in which CODE is a binary
9c20c4fc 1718 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1719 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
b13d1547 1720void
9c20c4fc 1721warn_logical_operator (location_t location, enum tree_code code, tree type,
48e1416a 1722 enum tree_code code_left, tree op_left,
03033af4 1723 enum tree_code ARG_UNUSED (code_right), tree op_right)
b13d1547 1724{
9c20c4fc 1725 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1726 int in0_p, in1_p, in_p;
1727 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1728 bool strict_overflow_p = false;
1729
03033af4 1730 if (code != TRUTH_ANDIF_EXPR
1731 && code != TRUTH_AND_EXPR
1732 && code != TRUTH_ORIF_EXPR
1733 && code != TRUTH_OR_EXPR)
1734 return;
1735
439606a9 1736 /* We don't want to warn if either operand comes from a macro
1737 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1738 see PR61534. */
1739 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1740 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1741 return;
1742
03033af4 1743 /* Warn if &&/|| are being used in a context where it is
1744 likely that the bitwise equivalent was intended by the
1745 programmer. That is, an expression such as op && MASK
1746 where op should not be any boolean expression, nor a
1747 constant, and mask seems to be a non-boolean integer constant. */
1748 if (!truth_value_p (code_left)
1749 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1750 && !CONSTANT_CLASS_P (op_left)
1751 && !TREE_NO_WARNING (op_left)
1752 && TREE_CODE (op_right) == INTEGER_CST
1753 && !integer_zerop (op_right)
1754 && !integer_onep (op_right))
b13d1547 1755 {
9c20c4fc 1756 if (or_op)
03033af4 1757 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1758 " applied to non-boolean constant");
1759 else
1760 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1761 " applied to non-boolean constant");
1762 TREE_NO_WARNING (op_left) = true;
9c20c4fc 1763 return;
1764 }
1765
1766 /* We do not warn for constants because they are typical of macro
1767 expansions that test for features. */
1768 if (CONSTANT_CLASS_P (op_left) || CONSTANT_CLASS_P (op_right))
1769 return;
1770
1771 /* This warning only makes sense with logical operands. */
1772 if (!(truth_value_p (TREE_CODE (op_left))
1773 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1774 || !(truth_value_p (TREE_CODE (op_right))
1775 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1776 return;
1777
5e84569c 1778 /* The range computations only work with scalars. */
1779 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1780 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1781 return;
9c20c4fc 1782
686369e8 1783 /* We first test whether either side separately is trivially true
1784 (with OR) or trivially false (with AND). If so, do not warn.
1785 This is a common idiom for testing ranges of data types in
1786 portable code. */
1787 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1788 if (!lhs)
1789 return;
1790 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
9c20c4fc 1791 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1792
686369e8 1793 /* If this is an OR operation, invert both sides; now, the result
1794 should be always false to get a warning. */
1795 if (or_op)
1796 in0_p = !in0_p;
1797
1798 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
d42e7c5a 1799 if (tem && integer_zerop (tem))
686369e8 1800 return;
1801
1802 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1803 if (!rhs)
1804 return;
1805 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
9c20c4fc 1806 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
48e1416a 1807
686369e8 1808 /* If this is an OR operation, invert both sides; now, the result
1809 should be always false to get a warning. */
9c20c4fc 1810 if (or_op)
686369e8 1811 in1_p = !in1_p;
48e1416a 1812
686369e8 1813 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
d42e7c5a 1814 if (tem && integer_zerop (tem))
686369e8 1815 return;
1816
1817 /* If both expressions have the same operand, if we can merge the
485f6b9c 1818 ranges, ... */
686369e8 1819 if (operand_equal_p (lhs, rhs, 0)
9c20c4fc 1820 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
485f6b9c 1821 in1_p, low1, high1))
9c20c4fc 1822 {
485f6b9c 1823 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1824 /* ... and if the range test is always false, then warn. */
1825 if (tem && integer_zerop (tem))
1826 {
1827 if (or_op)
1828 warning_at (location, OPT_Wlogical_op,
1829 "logical %<or%> of collectively exhaustive tests is "
1830 "always true");
1831 else
1832 warning_at (location, OPT_Wlogical_op,
1833 "logical %<and%> of mutually exclusive tests is "
1834 "always false");
1835 }
1836 /* Or warn if the operands have exactly the same range, e.g.
1837 A > 0 && A > 0. */
0200602e 1838 else if (tree_int_cst_equal (low0, low1)
1839 && tree_int_cst_equal (high0, high1))
485f6b9c 1840 {
1841 if (or_op)
1842 warning_at (location, OPT_Wlogical_op,
1843 "logical %<or%> of equal expressions");
1844 else
1845 warning_at (location, OPT_Wlogical_op,
1846 "logical %<and%> of equal expressions");
1847 }
b13d1547 1848 }
1849}
1850
32dc1512 1851/* Warn about logical not used on the left hand side operand of a comparison.
1852 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
dc6229e8 1853 Do not warn if RHS is of a boolean type. */
32dc1512 1854
1855void
1856warn_logical_not_parentheses (location_t location, enum tree_code code,
dc6229e8 1857 tree rhs)
32dc1512 1858{
dc6229e8 1859 if (TREE_CODE_CLASS (code) != tcc_comparison
1860 || TREE_TYPE (rhs) == NULL_TREE
1861 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
32dc1512 1862 return;
1863
16f958b3 1864 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1865 !(x == 0) or !(y != 0). */
1866 if ((code == EQ_EXPR || code == NE_EXPR)
1867 && integer_zerop (rhs))
1868 return;
1869
32dc1512 1870 warning_at (location, OPT_Wlogical_not_parentheses,
1871 "logical not is only applied to the left hand side of "
1872 "comparison");
1873}
b13d1547 1874
3d177e8c 1875/* Warn if EXP contains any computations whose results are not used.
1876 Return true if a warning is printed; false otherwise. LOCUS is the
1877 (potential) location of the expression. */
1878
1879bool
1880warn_if_unused_value (const_tree exp, location_t locus)
1881{
1882 restart:
1883 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1884 return false;
1885
1886 /* Don't warn about void constructs. This includes casting to void,
1887 void function calls, and statement expressions with a final cast
1888 to void. */
1889 if (VOID_TYPE_P (TREE_TYPE (exp)))
1890 return false;
1891
1892 if (EXPR_HAS_LOCATION (exp))
1893 locus = EXPR_LOCATION (exp);
1894
1895 switch (TREE_CODE (exp))
1896 {
1897 case PREINCREMENT_EXPR:
1898 case POSTINCREMENT_EXPR:
1899 case PREDECREMENT_EXPR:
1900 case POSTDECREMENT_EXPR:
1901 case MODIFY_EXPR:
1902 case INIT_EXPR:
1903 case TARGET_EXPR:
1904 case CALL_EXPR:
1905 case TRY_CATCH_EXPR:
1906 case WITH_CLEANUP_EXPR:
1907 case EXIT_EXPR:
1908 case VA_ARG_EXPR:
1909 return false;
1910
1911 case BIND_EXPR:
1912 /* For a binding, warn if no side effect within it. */
1913 exp = BIND_EXPR_BODY (exp);
1914 goto restart;
1915
1916 case SAVE_EXPR:
1917 case NON_LVALUE_EXPR:
d85dbdb3 1918 case NOP_EXPR:
3d177e8c 1919 exp = TREE_OPERAND (exp, 0);
1920 goto restart;
1921
1922 case TRUTH_ORIF_EXPR:
1923 case TRUTH_ANDIF_EXPR:
1924 /* In && or ||, warn if 2nd operand has no side effect. */
1925 exp = TREE_OPERAND (exp, 1);
1926 goto restart;
1927
1928 case COMPOUND_EXPR:
1929 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1930 return true;
1931 /* Let people do `(foo (), 0)' without a warning. */
1932 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1933 return false;
1934 exp = TREE_OPERAND (exp, 1);
1935 goto restart;
1936
1937 case COND_EXPR:
1938 /* If this is an expression with side effects, don't warn; this
1939 case commonly appears in macro expansions. */
1940 if (TREE_SIDE_EFFECTS (exp))
1941 return false;
1942 goto warn;
1943
1944 case INDIRECT_REF:
1945 /* Don't warn about automatic dereferencing of references, since
1946 the user cannot control it. */
1947 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1948 {
1949 exp = TREE_OPERAND (exp, 0);
1950 goto restart;
1951 }
1952 /* Fall through. */
1953
1954 default:
1955 /* Referencing a volatile value is a side effect, so don't warn. */
1956 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1957 && TREE_THIS_VOLATILE (exp))
1958 return false;
1959
1960 /* If this is an expression which has no operands, there is no value
1961 to be unused. There are no such language-independent codes,
1962 but front ends may define such. */
1963 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1964 return false;
1965
1966 warn:
1967 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1968 }
1969}
1970
1971
bcf22371 1972/* Print a warning about casts that might indicate violation
1973 of strict aliasing rules if -Wstrict-aliasing is used and
1e31ff37 1974 strict aliasing mode is in effect. OTYPE is the original
1975 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
bcf22371 1976
e6fa0ea6 1977bool
1e31ff37 1978strict_aliasing_warning (tree otype, tree type, tree expr)
bcf22371 1979{
45bb3afb 1980 /* Strip pointer conversion chains and get to the correct original type. */
1981 STRIP_NOPS (expr);
1982 otype = TREE_TYPE (expr);
1983
f06537f2 1984 if (!(flag_strict_aliasing
1985 && POINTER_TYPE_P (type)
1986 && POINTER_TYPE_P (otype)
1987 && !VOID_TYPE_P (TREE_TYPE (type)))
1988 /* If the type we are casting to is a ref-all pointer
1989 dereferencing it is always valid. */
1990 || TYPE_REF_CAN_ALIAS_ALL (type))
e6fa0ea6 1991 return false;
1992
1993 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
bcf22371 1994 && (DECL_P (TREE_OPERAND (expr, 0))
e6fa0ea6 1995 || handled_component_p (TREE_OPERAND (expr, 0))))
bcf22371 1996 {
1997 /* Casting the address of an object to non void pointer. Warn
1998 if the cast breaks type based aliasing. */
e6fa0ea6 1999 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
2000 {
2001 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
2002 "might break strict-aliasing rules");
2003 return true;
2004 }
bcf22371 2005 else
2006 {
48e1416a 2007 /* warn_strict_aliasing >= 3. This includes the default (3).
e6fa0ea6 2008 Only warn if the cast is dereferenced immediately. */
32c2fdea 2009 alias_set_type set1 =
e6fa0ea6 2010 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
32c2fdea 2011 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
bcf22371 2012
62d823d0 2013 if (set1 != set2 && set2 != 0
2014 && (set1 == 0 || !alias_sets_conflict_p (set1, set2)))
e6fa0ea6 2015 {
2016 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2017 "pointer will break strict-aliasing rules");
2018 return true;
2019 }
2020 else if (warn_strict_aliasing == 2
879f881c 2021 && !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 2022 {
2023 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2024 "pointer might break strict-aliasing rules");
2025 return true;
2026 }
bcf22371 2027 }
2028 }
e6fa0ea6 2029 else
2030 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
2031 {
2032 /* At this level, warn for any conversions, even if an address is
2033 not taken in the same statement. This will likely produce many
2034 false positives, but could be useful to pinpoint problems that
2035 are not revealed at higher levels. */
32c2fdea 2036 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
2037 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
2038 if (!COMPLETE_TYPE_P (type)
879f881c 2039 || !alias_sets_must_conflict_p (set1, set2))
e6fa0ea6 2040 {
2041 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
2042 "pointer might break strict-aliasing rules");
2043 return true;
2044 }
2045 }
2046
2047 return false;
bcf22371 2048}
2049
f003f5dc 2050/* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
2051 sizeof as last operand of certain builtins. */
2052
2053void
57f872a2 2054sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
f1f41a6c 2055 vec<tree, va_gc> *params, tree *sizeof_arg,
f003f5dc 2056 bool (*comp_types) (tree, tree))
2057{
2058 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
57f872a2 2059 bool strop = false, cmp = false;
2060 unsigned int idx = ~0;
2061 location_t loc;
f003f5dc 2062
2063 if (TREE_CODE (callee) != FUNCTION_DECL
2064 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
f1f41a6c 2065 || vec_safe_length (params) <= 1)
f003f5dc 2066 return;
2067
f003f5dc 2068 switch (DECL_FUNCTION_CODE (callee))
2069 {
2070 case BUILT_IN_STRNCMP:
2071 case BUILT_IN_STRNCASECMP:
57f872a2 2072 cmp = true;
2073 /* FALLTHRU */
f003f5dc 2074 case BUILT_IN_STRNCPY:
57f872a2 2075 case BUILT_IN_STRNCPY_CHK:
f003f5dc 2076 case BUILT_IN_STRNCAT:
57f872a2 2077 case BUILT_IN_STRNCAT_CHK:
2078 case BUILT_IN_STPNCPY:
2079 case BUILT_IN_STPNCPY_CHK:
f003f5dc 2080 strop = true;
2081 /* FALLTHRU */
2082 case BUILT_IN_MEMCPY:
57f872a2 2083 case BUILT_IN_MEMCPY_CHK:
f003f5dc 2084 case BUILT_IN_MEMMOVE:
57f872a2 2085 case BUILT_IN_MEMMOVE_CHK:
f1f41a6c 2086 if (params->length () < 3)
57f872a2 2087 return;
f1f41a6c 2088 src = (*params)[1];
2089 dest = (*params)[0];
57f872a2 2090 idx = 2;
2091 break;
2092 case BUILT_IN_BCOPY:
f1f41a6c 2093 if (params->length () < 3)
57f872a2 2094 return;
f1f41a6c 2095 src = (*params)[0];
2096 dest = (*params)[1];
57f872a2 2097 idx = 2;
2098 break;
f003f5dc 2099 case BUILT_IN_MEMCMP:
57f872a2 2100 case BUILT_IN_BCMP:
f1f41a6c 2101 if (params->length () < 3)
f003f5dc 2102 return;
f1f41a6c 2103 src = (*params)[1];
2104 dest = (*params)[0];
57f872a2 2105 idx = 2;
2106 cmp = true;
f003f5dc 2107 break;
2108 case BUILT_IN_MEMSET:
57f872a2 2109 case BUILT_IN_MEMSET_CHK:
f1f41a6c 2110 if (params->length () < 3)
f003f5dc 2111 return;
f1f41a6c 2112 dest = (*params)[0];
57f872a2 2113 idx = 2;
2114 break;
2115 case BUILT_IN_BZERO:
f1f41a6c 2116 dest = (*params)[0];
57f872a2 2117 idx = 1;
f003f5dc 2118 break;
2119 case BUILT_IN_STRNDUP:
f1f41a6c 2120 src = (*params)[0];
f003f5dc 2121 strop = true;
57f872a2 2122 idx = 1;
2123 break;
2124 case BUILT_IN_MEMCHR:
f1f41a6c 2125 if (params->length () < 3)
57f872a2 2126 return;
f1f41a6c 2127 src = (*params)[0];
57f872a2 2128 idx = 2;
2129 break;
2130 case BUILT_IN_SNPRINTF:
2131 case BUILT_IN_SNPRINTF_CHK:
2132 case BUILT_IN_VSNPRINTF:
2133 case BUILT_IN_VSNPRINTF_CHK:
f1f41a6c 2134 dest = (*params)[0];
57f872a2 2135 idx = 1;
2136 strop = true;
f003f5dc 2137 break;
2138 default:
2139 break;
2140 }
2141
57f872a2 2142 if (idx >= 3)
2143 return;
2144
2145 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
2146 return;
2147
2148 type = TYPE_P (sizeof_arg[idx])
2149 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
2150 if (!POINTER_TYPE_P (type))
2151 return;
2152
f003f5dc 2153 if (dest
2154 && (tem = tree_strip_nop_conversions (dest))
2155 && POINTER_TYPE_P (TREE_TYPE (tem))
2156 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2157 return;
2158
2159 if (src
2160 && (tem = tree_strip_nop_conversions (src))
2161 && POINTER_TYPE_P (TREE_TYPE (tem))
2162 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
2163 return;
2164
57f872a2 2165 loc = sizeof_arg_loc[idx];
2166
2167 if (dest && !cmp)
f003f5dc 2168 {
57f872a2 2169 if (!TYPE_P (sizeof_arg[idx])
2170 && operand_equal_p (dest, sizeof_arg[idx], 0)
f003f5dc 2171 && comp_types (TREE_TYPE (dest), type))
2172 {
57f872a2 2173 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
f003f5dc 2174 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2175 "argument to %<sizeof%> in %qD call is the same "
2176 "expression as the destination; did you mean to "
2177 "remove the addressof?", callee);
2178 else if ((TYPE_PRECISION (TREE_TYPE (type))
2179 == TYPE_PRECISION (char_type_node))
2180 || strop)
2181 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2182 "argument to %<sizeof%> in %qD call is the same "
2183 "expression as the destination; did you mean to "
2184 "provide an explicit length?", callee);
2185 else
2186 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2187 "argument to %<sizeof%> in %qD call is the same "
2188 "expression as the destination; did you mean to "
2189 "dereference it?", callee);
2190 return;
2191 }
2192
2193 if (POINTER_TYPE_P (TREE_TYPE (dest))
2194 && !strop
2195 && comp_types (TREE_TYPE (dest), type)
2196 && !VOID_TYPE_P (TREE_TYPE (type)))
2197 {
2198 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2199 "argument to %<sizeof%> in %qD call is the same "
2200 "pointer type %qT as the destination; expected %qT "
2201 "or an explicit length", callee, TREE_TYPE (dest),
2202 TREE_TYPE (TREE_TYPE (dest)));
2203 return;
2204 }
2205 }
2206
57f872a2 2207 if (src && !cmp)
f003f5dc 2208 {
57f872a2 2209 if (!TYPE_P (sizeof_arg[idx])
2210 && operand_equal_p (src, sizeof_arg[idx], 0)
f003f5dc 2211 && comp_types (TREE_TYPE (src), type))
2212 {
57f872a2 2213 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
f003f5dc 2214 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2215 "argument to %<sizeof%> in %qD call is the same "
2216 "expression as the source; did you mean to "
2217 "remove the addressof?", callee);
2218 else if ((TYPE_PRECISION (TREE_TYPE (type))
2219 == TYPE_PRECISION (char_type_node))
2220 || strop)
2221 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2222 "argument to %<sizeof%> in %qD call is the same "
2223 "expression as the source; did you mean to "
2224 "provide an explicit length?", callee);
2225 else
2226 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2227 "argument to %<sizeof%> in %qD call is the same "
2228 "expression as the source; did you mean to "
2229 "dereference it?", callee);
2230 return;
2231 }
2232
2233 if (POINTER_TYPE_P (TREE_TYPE (src))
2234 && !strop
2235 && comp_types (TREE_TYPE (src), type)
2236 && !VOID_TYPE_P (TREE_TYPE (type)))
2237 {
2238 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2239 "argument to %<sizeof%> in %qD call is the same "
2240 "pointer type %qT as the source; expected %qT "
2241 "or an explicit length", callee, TREE_TYPE (src),
2242 TREE_TYPE (TREE_TYPE (src)));
2243 return;
2244 }
2245 }
57f872a2 2246
2247 if (dest)
2248 {
2249 if (!TYPE_P (sizeof_arg[idx])
2250 && operand_equal_p (dest, sizeof_arg[idx], 0)
2251 && comp_types (TREE_TYPE (dest), type))
2252 {
2253 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2254 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2255 "argument to %<sizeof%> in %qD call is the same "
2256 "expression as the first source; did you mean to "
2257 "remove the addressof?", callee);
2258 else if ((TYPE_PRECISION (TREE_TYPE (type))
2259 == TYPE_PRECISION (char_type_node))
2260 || strop)
2261 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2262 "argument to %<sizeof%> in %qD call is the same "
2263 "expression as the first source; did you mean to "
2264 "provide an explicit length?", callee);
2265 else
2266 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2267 "argument to %<sizeof%> in %qD call is the same "
2268 "expression as the first source; did you mean to "
2269 "dereference it?", callee);
2270 return;
2271 }
2272
2273 if (POINTER_TYPE_P (TREE_TYPE (dest))
2274 && !strop
2275 && comp_types (TREE_TYPE (dest), type)
2276 && !VOID_TYPE_P (TREE_TYPE (type)))
2277 {
2278 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2279 "argument to %<sizeof%> in %qD call is the same "
2280 "pointer type %qT as the first source; expected %qT "
2281 "or an explicit length", callee, TREE_TYPE (dest),
2282 TREE_TYPE (TREE_TYPE (dest)));
2283 return;
2284 }
2285 }
2286
2287 if (src)
2288 {
2289 if (!TYPE_P (sizeof_arg[idx])
2290 && operand_equal_p (src, sizeof_arg[idx], 0)
2291 && comp_types (TREE_TYPE (src), type))
2292 {
2293 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
2294 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2295 "argument to %<sizeof%> in %qD call is the same "
2296 "expression as the second source; did you mean to "
2297 "remove the addressof?", callee);
2298 else if ((TYPE_PRECISION (TREE_TYPE (type))
2299 == TYPE_PRECISION (char_type_node))
2300 || strop)
2301 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2302 "argument to %<sizeof%> in %qD call is the same "
2303 "expression as the second source; did you mean to "
2304 "provide an explicit length?", callee);
2305 else
2306 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2307 "argument to %<sizeof%> in %qD call is the same "
2308 "expression as the second source; did you mean to "
2309 "dereference it?", callee);
2310 return;
2311 }
2312
2313 if (POINTER_TYPE_P (TREE_TYPE (src))
2314 && !strop
2315 && comp_types (TREE_TYPE (src), type)
2316 && !VOID_TYPE_P (TREE_TYPE (type)))
2317 {
2318 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
2319 "argument to %<sizeof%> in %qD call is the same "
2320 "pointer type %qT as the second source; expected %qT "
2321 "or an explicit length", callee, TREE_TYPE (src),
2322 TREE_TYPE (TREE_TYPE (src)));
2323 return;
2324 }
2325 }
2326
f003f5dc 2327}
2328
3f08e399 2329/* Warn for unlikely, improbable, or stupid DECL declarations
2330 of `main'. */
2331
2332void
2333check_main_parameter_types (tree decl)
2334{
d0af78c5 2335 function_args_iterator iter;
2336 tree type;
3f08e399 2337 int argct = 0;
2338
d0af78c5 2339 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
2340 {
2341 /* XXX void_type_node belies the abstraction. */
2342 if (type == void_type_node || type == error_mark_node )
2343 break;
2344
2026249a 2345 tree t = type;
2346 if (TYPE_ATOMIC (t))
2347 pedwarn (input_location, OPT_Wmain,
2348 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2349 type, decl);
2350 while (POINTER_TYPE_P (t))
2351 {
2352 t = TREE_TYPE (t);
2353 if (TYPE_ATOMIC (t))
2354 pedwarn (input_location, OPT_Wmain,
2355 "%<_Atomic%>-qualified parameter type %qT of %q+D",
2356 type, decl);
2357 }
2358
d0af78c5 2359 ++argct;
2360 switch (argct)
2361 {
2362 case 1:
2363 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
2364 pedwarn (input_location, OPT_Wmain,
2365 "first argument of %q+D should be %<int%>", decl);
2366 break;
2367
2368 case 2:
2369 if (TREE_CODE (type) != POINTER_TYPE
2370 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2371 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2372 != char_type_node))
2373 pedwarn (input_location, OPT_Wmain,
2374 "second argument of %q+D should be %<char **%>", decl);
2375 break;
2376
2377 case 3:
2378 if (TREE_CODE (type) != POINTER_TYPE
2379 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2380 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2381 != char_type_node))
2382 pedwarn (input_location, OPT_Wmain,
2383 "third argument of %q+D should probably be "
2384 "%<char **%>", decl);
2385 break;
2386 }
2387 }
3f08e399 2388
2389 /* It is intentional that this message does not mention the third
2390 argument because it's only mentioned in an appendix of the
2391 standard. */
2392 if (argct > 0 && (argct < 2 || argct > 3))
d0af78c5 2393 pedwarn (input_location, OPT_Wmain,
2394 "%q+D takes only zero or two arguments", decl);
90e645fa 2395
2396 if (stdarg_p (TREE_TYPE (decl)))
2397 pedwarn (input_location, OPT_Wmain,
2398 "%q+D declared as variadic function", decl);
3f08e399 2399}
2400
73437615 2401/* vector_targets_convertible_p is used for vector pointer types. The
2402 callers perform various checks that the qualifiers are satisfactory,
2403 while OTOH vector_targets_convertible_p ignores the number of elements
2404 in the vectors. That's fine with vector pointers as we can consider,
2405 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2406 and that does not require and conversion of the pointer values.
2407 In contrast, vector_types_convertible_p and
2408 vector_types_compatible_elements_p are used for vector value types. */
ed7c4e62 2409/* True if pointers to distinct types T1 and T2 can be converted to
2410 each other without an explicit cast. Only returns true for opaque
2411 vector types. */
2412bool
2413vector_targets_convertible_p (const_tree t1, const_tree t2)
2414{
2415 if (TREE_CODE (t1) == VECTOR_TYPE && TREE_CODE (t2) == VECTOR_TYPE
8d125f7d 2416 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ed7c4e62 2417 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2418 return true;
2419
2420 return false;
2421}
2422
73437615 2423/* vector_types_convertible_p is used for vector value types.
2424 It could in principle call vector_targets_convertible_p as a subroutine,
2425 but then the check for vector type would be duplicated with its callers,
2426 and also the purpose of vector_targets_convertible_p would become
2427 muddled.
2428 Where vector_types_convertible_p returns true, a conversion might still be
2429 needed to make the types match.
2430 In contrast, vector_targets_convertible_p is used for vector pointer
2431 values, and vector_types_compatible_elements_p is used specifically
2432 in the context for binary operators, as a check if use is possible without
2433 conversion. */
546c4794 2434/* True if vector types T1 and T2 can be converted to each other
2435 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2436 can only be converted with -flax-vector-conversions yet that is not
2437 in effect, emit a note telling the user about that option if such
2438 a note has not previously been emitted. */
2439bool
9f627b1a 2440vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
8b4b9810 2441{
546c4794 2442 static bool emitted_lax_note = false;
ae6db8ab 2443 bool convertible_lax;
2444
8d125f7d 2445 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ae6db8ab 2446 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2447 return true;
2448
2449 convertible_lax =
2450 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2451 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
432dd330 2452 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
ae6db8ab 2453 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2454 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
546c4794 2455
2456 if (!convertible_lax || flag_lax_vector_conversions)
2457 return convertible_lax;
2458
2459 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
29f51994 2460 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
546c4794 2461 return true;
2462
2463 if (emit_lax_note && !emitted_lax_note)
2464 {
2465 emitted_lax_note = true;
5bcc316e 2466 inform (input_location, "use -flax-vector-conversions to permit "
546c4794 2467 "conversions between vectors with differing "
2468 "element types or numbers of subparts");
2469 }
2470
2471 return false;
8b4b9810 2472}
2473
bf0cb017 2474/* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2475 and have vector types, V0 has the same type as V1, and the number of
2476 elements of V0, V1, MASK is the same.
2477
2478 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2479 called with two arguments. In this case implementation passes the
2480 first argument twice in order to share the same tree code. This fact
2481 could enable the mask-values being twice the vector length. This is
2482 an implementation accident and this semantics is not guaranteed to
2483 the user. */
2484tree
68ea4406 2485c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2486 bool complain)
bf0cb017 2487{
2488 tree ret;
2489 bool wrap = true;
2490 bool maybe_const = false;
2491 bool two_arguments = false;
2492
2493 if (v1 == NULL_TREE)
2494 {
2495 two_arguments = true;
2496 v1 = v0;
2497 }
2498
2499 if (v0 == error_mark_node || v1 == error_mark_node
2500 || mask == error_mark_node)
2501 return error_mark_node;
2502
2503 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2504 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2505 {
68ea4406 2506 if (complain)
2507 error_at (loc, "__builtin_shuffle last argument must "
2508 "be an integer vector");
bf0cb017 2509 return error_mark_node;
2510 }
2511
2512 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2513 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2514 {
68ea4406 2515 if (complain)
2516 error_at (loc, "__builtin_shuffle arguments must be vectors");
bf0cb017 2517 return error_mark_node;
2518 }
2519
2520 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2521 {
68ea4406 2522 if (complain)
2523 error_at (loc, "__builtin_shuffle argument vectors must be of "
2524 "the same type");
bf0cb017 2525 return error_mark_node;
2526 }
2527
2528 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2529 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2530 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2531 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2532 {
68ea4406 2533 if (complain)
2534 error_at (loc, "__builtin_shuffle number of elements of the "
2535 "argument vector(s) and the mask vector should "
2536 "be the same");
bf0cb017 2537 return error_mark_node;
2538 }
2539
2540 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2541 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2542 {
68ea4406 2543 if (complain)
2544 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2545 "must have the same size as inner type of the mask");
bf0cb017 2546 return error_mark_node;
2547 }
2548
2549 if (!c_dialect_cxx ())
2550 {
2551 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2552 v0 = c_fully_fold (v0, false, &maybe_const);
2553 wrap &= maybe_const;
2554
2555 if (two_arguments)
2556 v1 = v0 = save_expr (v0);
2557 else
2558 {
2559 v1 = c_fully_fold (v1, false, &maybe_const);
2560 wrap &= maybe_const;
2561 }
2562
2563 mask = c_fully_fold (mask, false, &maybe_const);
2564 wrap &= maybe_const;
2565 }
68ea4406 2566 else if (two_arguments)
2567 v1 = v0 = save_expr (v0);
bf0cb017 2568
2569 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2570
2571 if (!c_dialect_cxx () && !wrap)
2572 ret = c_wrap_maybe_const (ret, true);
2573
2574 return ret;
2575}
2576
7f506bca 2577/* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2578 to integral type. */
2579
2580static tree
2581c_common_get_narrower (tree op, int *unsignedp_ptr)
2582{
2583 op = get_narrower (op, unsignedp_ptr);
2584
2585 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2586 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2587 {
2588 /* C++0x scoped enumerations don't implicitly convert to integral
2589 type; if we stripped an explicit conversion to a larger type we
2590 need to replace it so common_type will still work. */
a51edb4c 2591 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2592 TYPE_UNSIGNED (TREE_TYPE (op)));
7f506bca 2593 op = fold_convert (type, op);
2594 }
2595 return op;
2596}
2597
2561cea2 2598/* This is a helper function of build_binary_op.
2599
2600 For certain operations if both args were extended from the same
2601 smaller type, do the arithmetic in that type and then extend.
2602
2603 BITWISE indicates a bitwise operation.
2604 For them, this optimization is safe only if
2605 both args are zero-extended or both are sign-extended.
2606 Otherwise, we might change the result.
2607 Eg, (short)-1 | (unsigned short)-1 is (int)-1
48e1416a 2608 but calculated in (unsigned short) it would be (unsigned short)-1.
2561cea2 2609*/
7f506bca 2610tree
2611shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2561cea2 2612{
2613 int unsigned0, unsigned1;
2614 tree arg0, arg1;
2615 int uns;
2616 tree type;
2617
2618 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2619 excessive narrowing when we call get_narrower below. For
2620 example, suppose that OP0 is of unsigned int extended
2621 from signed char and that RESULT_TYPE is long long int.
2622 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2623 like
48e1416a 2624
2561cea2 2625 (long long int) (unsigned int) signed_char
2626
2627 which get_narrower would narrow down to
48e1416a 2628
2561cea2 2629 (unsigned int) signed char
48e1416a 2630
2561cea2 2631 If we do not cast OP0 first, get_narrower would return
2632 signed_char, which is inconsistent with the case of the
2633 explicit cast. */
2634 op0 = convert (result_type, op0);
2635 op1 = convert (result_type, op1);
2636
7f506bca 2637 arg0 = c_common_get_narrower (op0, &unsigned0);
2638 arg1 = c_common_get_narrower (op1, &unsigned1);
ab2c1de8 2639
2561cea2 2640 /* UNS is 1 if the operation to be done is an unsigned one. */
2641 uns = TYPE_UNSIGNED (result_type);
2642
2643 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2644 but it *requires* conversion to FINAL_TYPE. */
48e1416a 2645
2561cea2 2646 if ((TYPE_PRECISION (TREE_TYPE (op0))
2647 == TYPE_PRECISION (TREE_TYPE (arg0)))
2648 && TREE_TYPE (op0) != result_type)
2649 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2650 if ((TYPE_PRECISION (TREE_TYPE (op1))
2651 == TYPE_PRECISION (TREE_TYPE (arg1)))
2652 && TREE_TYPE (op1) != result_type)
2653 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
48e1416a 2654
2561cea2 2655 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
48e1416a 2656
2561cea2 2657 /* For bitwise operations, signedness of nominal type
2658 does not matter. Consider only how operands were extended. */
2659 if (bitwise)
2660 uns = unsigned0;
48e1416a 2661
2561cea2 2662 /* Note that in all three cases below we refrain from optimizing
2663 an unsigned operation on sign-extended args.
2664 That would not be valid. */
48e1416a 2665
2561cea2 2666 /* Both args variable: if both extended in same way
2667 from same width, do it in that width.
2668 Do it unsigned if args were zero-extended. */
2669 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2670 < TYPE_PRECISION (result_type))
2671 && (TYPE_PRECISION (TREE_TYPE (arg1))
2672 == TYPE_PRECISION (TREE_TYPE (arg0)))
2673 && unsigned0 == unsigned1
2674 && (unsigned0 || !uns))
2675 return c_common_signed_or_unsigned_type
2676 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2677
2678 else if (TREE_CODE (arg0) == INTEGER_CST
2679 && (unsigned1 || !uns)
2680 && (TYPE_PRECISION (TREE_TYPE (arg1))
2681 < TYPE_PRECISION (result_type))
2682 && (type
2683 = c_common_signed_or_unsigned_type (unsigned1,
2684 TREE_TYPE (arg1)))
2685 && !POINTER_TYPE_P (type)
2686 && int_fits_type_p (arg0, type))
2687 return type;
2688
2689 else if (TREE_CODE (arg1) == INTEGER_CST
2690 && (unsigned0 || !uns)
2691 && (TYPE_PRECISION (TREE_TYPE (arg0))
2692 < TYPE_PRECISION (result_type))
2693 && (type
2694 = c_common_signed_or_unsigned_type (unsigned0,
2695 TREE_TYPE (arg0)))
2696 && !POINTER_TYPE_P (type)
2697 && int_fits_type_p (arg1, type))
2698 return type;
2699
2700 return result_type;
2701}
2702
e53013a8 2703/* Returns true iff any integer value of type FROM_TYPE can be represented as
2704 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2705
2706static bool
2707int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2708{
2709 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2710 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2711 REAL_VALUE_TYPE real_low_bound =
2712 real_value_from_int_cst (0, type_low_bound);
2713 REAL_VALUE_TYPE real_high_bound =
2714 real_value_from_int_cst (0, type_high_bound);
2715
2716 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2717 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2718}
2719
2720/* Checks if expression EXPR of complex/real/integer type cannot be converted
2721 to the complex/real/integer type TYPE. Function returns non-zero when:
22a75734 2722 * EXPR is a constant which cannot be exactly converted to TYPE.
2723 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
e53013a8 2724 for EXPR type and TYPE being both integers or both real, or both
2725 complex.
2726 * EXPR is not a constant of complex type and TYPE is a real or
2727 an integer.
22a75734 2728 * EXPR is not a constant of real type and TYPE is an integer.
2729 * EXPR is not a constant of integer type which cannot be
2730 exactly converted to real type.
e53013a8 2731
7dfa155b 2732 Function allows conversions between types of different signedness and
ca9d7d74 2733 can return SAFE_CONVERSION (zero) in that case. Function can produce
e53013a8 2734 signedness warnings if PRODUCE_WARNS is true.
2735
2736 Function allows conversions from complex constants to non-complex types,
2737 provided that imaginary part is zero and real part can be safely converted
2738 to TYPE. */
22a75734 2739
ca9d7d74 2740enum conversion_safety
22a75734 2741unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
d31d55f0 2742{
ca9d7d74 2743 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
27259707 2744 tree expr_type = TREE_TYPE (expr);
22a75734 2745 loc = expansion_point_location_if_in_system_header (loc);
d31d55f0 2746
7dfa155b 2747 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
5b16c152 2748 {
e53013a8 2749 /* If type is complex, we are interested in compatibility with
2750 underlying type. */
2751 if (TREE_CODE (type) == COMPLEX_TYPE)
2752 type = TREE_TYPE (type);
2753
d31d55f0 2754 /* Warn for real constant that is not an exact integer converted
7dfa155b 2755 to integer type. */
27259707 2756 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2757 && TREE_CODE (type) == INTEGER_TYPE)
2758 {
2759 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
ca9d7d74 2760 give_warning = UNSAFE_REAL;
7dfa155b 2761 }
da1fb07b 2762 /* Warn for an integer constant that does not fit into integer type. */
27259707 2763 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2764 && TREE_CODE (type) == INTEGER_TYPE
2765 && !int_fits_type_p (expr, type))
2766 {
2767 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
27259707 2768 && tree_int_cst_sgn (expr) < 0)
7dfa155b 2769 {
2770 if (produce_warns)
2771 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2772 " implicitly converted to unsigned type");
2773 }
2774 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2775 {
2776 if (produce_warns)
2777 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2778 " constant value to negative integer");
2779 }
7ee0d227 2780 else
ca9d7d74 2781 give_warning = UNSAFE_OTHER;
7dfa155b 2782 }
d31d55f0 2783 else if (TREE_CODE (type) == REAL_TYPE)
7dfa155b 2784 {
2785 /* Warn for an integer constant that does not fit into real type. */
2786 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2787 {
2788 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2789 if (!exact_real_truncate (TYPE_MODE (type), &a))
ca9d7d74 2790 give_warning = UNSAFE_REAL;
7dfa155b 2791 }
2792 /* Warn for a real constant that does not fit into a smaller
2793 real type. */
2794 else if (TREE_CODE (expr_type) == REAL_TYPE
2795 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2796 {
2797 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2798 if (!exact_real_truncate (TYPE_MODE (type), &a))
ca9d7d74 2799 give_warning = UNSAFE_REAL;
7dfa155b 2800 }
2801 }
2802 }
e53013a8 2803
2804 else if (TREE_CODE (expr) == COMPLEX_CST)
2805 {
2806 tree imag_part = TREE_IMAGPART (expr);
2807 /* Conversion from complex constant with zero imaginary part,
2808 perform check for conversion of real part. */
2809 if ((TREE_CODE (imag_part) == REAL_CST
2810 && real_zerop (imag_part))
2811 || (TREE_CODE (imag_part) == INTEGER_CST
2812 && integer_zerop (imag_part)))
2813 /* Note: in this branch we use recursive call to unsafe_conversion_p
2814 with different type of EXPR, but it is still safe, because when EXPR
2815 is a constant, it's type is not used in text of generated warnings
2816 (otherwise they could sound misleading). */
2817 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2818 produce_warns);
2819 /* Conversion from complex constant with non-zero imaginary part. */
2820 else
2821 {
2822 /* Conversion to complex type.
2823 Perform checks for both real and imaginary parts. */
2824 if (TREE_CODE (type) == COMPLEX_TYPE)
2825 {
2826 /* Unfortunately, produce_warns must be false in two subsequent
2827 calls of unsafe_conversion_p, because otherwise we could
2828 produce strange "double" warnings, if both real and imaginary
2829 parts have conversion problems related to signedness.
2830
2831 For example:
2832 int32_t _Complex a = 0x80000000 + 0x80000000i;
2833
2834 Possible solution: add a separate function for checking
2835 constants and combine result of two calls appropriately. */
2836 enum conversion_safety re_safety =
2837 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2838 enum conversion_safety im_safety =
2839 unsafe_conversion_p (loc, type, imag_part, false);
2840
2841 /* Merge the results into appropriate single warning. */
2842
2843 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2844 if (re_safety == im_safety)
2845 give_warning = re_safety;
2846 else if (!re_safety && im_safety)
2847 give_warning = im_safety;
2848 else if (re_safety && !im_safety)
2849 give_warning = re_safety;
2850 else
2851 give_warning = UNSAFE_OTHER;
2852 }
2853 /* Warn about conversion from complex to real or integer type. */
2854 else
2855 give_warning = UNSAFE_IMAGINARY;
2856 }
2857 }
2858
2859 /* Checks for remaining case: EXPR is not constant. */
7dfa155b 2860 else
2861 {
d31d55f0 2862 /* Warn for real types converted to integer types. */
2561cea2 2863 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2864 && TREE_CODE (type) == INTEGER_TYPE)
ca9d7d74 2865 give_warning = UNSAFE_REAL;
d31d55f0 2866
2561cea2 2867 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2868 && TREE_CODE (type) == INTEGER_TYPE)
2869 {
69609004 2870 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
f9d856a4 2871 expr = get_unwidened (expr, 0);
2561cea2 2872 expr_type = TREE_TYPE (expr);
69609004 2873
2561cea2 2874 /* Don't warn for short y; short x = ((int)y & 0xff); */
48e1416a 2875 if (TREE_CODE (expr) == BIT_AND_EXPR
7dfa155b 2876 || TREE_CODE (expr) == BIT_IOR_EXPR
2561cea2 2877 || TREE_CODE (expr) == BIT_XOR_EXPR)
2878 {
27259707 2879 /* If both args were extended from a shortest type,
2880 use that type if that is safe. */
48e1416a 2881 expr_type = shorten_binary_op (expr_type,
2882 TREE_OPERAND (expr, 0),
2883 TREE_OPERAND (expr, 1),
2561cea2 2884 /* bitwise */1);
2885
2561cea2 2886 if (TREE_CODE (expr) == BIT_AND_EXPR)
2887 {
2888 tree op0 = TREE_OPERAND (expr, 0);
2889 tree op1 = TREE_OPERAND (expr, 1);
30de145b 2890 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2891 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2892
2893 /* If one of the operands is a non-negative constant
2894 that fits in the target type, then the type of the
2895 other operand does not matter. */
2561cea2 2896 if ((TREE_CODE (op0) == INTEGER_CST
2897 && int_fits_type_p (op0, c_common_signed_type (type))
2898 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2899 || (TREE_CODE (op1) == INTEGER_CST
27259707 2900 && int_fits_type_p (op1, c_common_signed_type (type))
48e1416a 2901 && int_fits_type_p (op1,
27259707 2902 c_common_unsigned_type (type))))
ca9d7d74 2903 return SAFE_CONVERSION;
30de145b 2904 /* If constant is unsigned and fits in the target
2905 type, then the result will also fit. */
2906 else if ((TREE_CODE (op0) == INTEGER_CST
48e1416a 2907 && unsigned0
30de145b 2908 && int_fits_type_p (op0, type))
2909 || (TREE_CODE (op1) == INTEGER_CST
2910 && unsigned1
2911 && int_fits_type_p (op1, type)))
ca9d7d74 2912 return SAFE_CONVERSION;
2561cea2 2913 }
2914 }
7dfa155b 2915 /* Warn for integer types converted to smaller integer types. */
48e1416a 2916 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
ca9d7d74 2917 give_warning = UNSAFE_OTHER;
7ee0d227 2918
2919 /* When they are the same width but different signedness,
2920 then the value may change. */
7dfa155b 2921 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2561cea2 2922 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
7ee0d227 2923 /* Even when converted to a bigger type, if the type is
2924 unsigned but expr is signed, then negative values
2925 will be changed. */
7dfa155b 2926 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2927 && produce_warns)
200dd99c 2928 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2929 "may change the sign of the result",
2930 type, expr_type);
7dfa155b 2931 }
d31d55f0 2932
2933 /* Warn for integer types converted to real types if and only if
7dfa155b 2934 all the range of values of the integer type cannot be
2935 represented by the real type. */
2561cea2 2936 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 2937 && TREE_CODE (type) == REAL_TYPE)
2938 {
4c2cfa81 2939 /* Don't warn about char y = 0xff; float x = (int) y; */
2940 expr = get_unwidened (expr, 0);
2941 expr_type = TREE_TYPE (expr);
2942
e53013a8 2943 if (!int_safely_convertible_to_real_p (expr_type, type))
ca9d7d74 2944 give_warning = UNSAFE_OTHER;
7dfa155b 2945 }
d31d55f0 2946
2947 /* Warn for real types converted to smaller real types. */
2561cea2 2948 else if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 2949 && TREE_CODE (type) == REAL_TYPE
2950 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
ca9d7d74 2951 give_warning = UNSAFE_REAL;
e53013a8 2952
2953 /* Check conversion between two complex types. */
2954 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2955 && TREE_CODE (type) == COMPLEX_TYPE)
2956 {
2957 /* Extract underlying types (i.e., type of real and imaginary
2958 parts) of expr_type and type. */
2959 tree from_type = TREE_TYPE (expr_type);
2960 tree to_type = TREE_TYPE (type);
2961
2962 /* Warn for real types converted to integer types. */
2963 if (TREE_CODE (from_type) == REAL_TYPE
2964 && TREE_CODE (to_type) == INTEGER_TYPE)
2965 give_warning = UNSAFE_REAL;
2966
2967 /* Warn for real types converted to smaller real types. */
2968 else if (TREE_CODE (from_type) == REAL_TYPE
2969 && TREE_CODE (to_type) == REAL_TYPE
2970 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2971 give_warning = UNSAFE_REAL;
2972
2973 /* Check conversion for complex integer types. Here implementation
2974 is simpler than for real-domain integers because it does not
2975 involve sophisticated cases, such as bitmasks, casts, etc. */
2976 else if (TREE_CODE (from_type) == INTEGER_TYPE
2977 && TREE_CODE (to_type) == INTEGER_TYPE)
2978 {
2979 /* Warn for integer types converted to smaller integer types. */
2980 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2981 give_warning = UNSAFE_OTHER;
2982
2983 /* Check for different signedness, see case for real-domain
2984 integers (above) for a more detailed comment. */
2985 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
2986 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
2987 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
2988 && produce_warns)
2989 warning_at (loc, OPT_Wsign_conversion,
2990 "conversion to %qT from %qT "
2991 "may change the sign of the result",
2992 type, expr_type);
2993 }
2994 else if (TREE_CODE (from_type) == INTEGER_TYPE
2995 && TREE_CODE (to_type) == REAL_TYPE
2996 && !int_safely_convertible_to_real_p (from_type, to_type))
2997 give_warning = UNSAFE_OTHER;
2998 }
2999
3000 /* Warn for complex types converted to real or integer types. */
3001 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
3002 && TREE_CODE (type) != COMPLEX_TYPE)
3003 give_warning = UNSAFE_IMAGINARY;
7dfa155b 3004 }
3005
3006 return give_warning;
3007}
3008
3009/* Warns if the conversion of EXPR to TYPE may alter a value.
3010 This is a helper function for warnings_for_convert_and_check. */
3011
3012static void
22a75734 3013conversion_warning (location_t loc, tree type, tree expr)
7dfa155b 3014{
7dfa155b 3015 tree expr_type = TREE_TYPE (expr);
ca9d7d74 3016 enum conversion_safety conversion_kind;
d31d55f0 3017
ca9d7d74 3018 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
7dfa155b 3019 return;
d31d55f0 3020
ec704957 3021 /* This may happen, because for LHS op= RHS we preevaluate
3022 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
3023 means we could no longer see the code of the EXPR. */
3024 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
3025 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
3026 if (TREE_CODE (expr) == SAVE_EXPR)
3027 expr = TREE_OPERAND (expr, 0);
3028
7dfa155b 3029 switch (TREE_CODE (expr))
3030 {
3031 case EQ_EXPR:
3032 case NE_EXPR:
3033 case LE_EXPR:
3034 case GE_EXPR:
3035 case LT_EXPR:
3036 case GT_EXPR:
3037 case TRUTH_ANDIF_EXPR:
3038 case TRUTH_ORIF_EXPR:
3039 case TRUTH_AND_EXPR:
3040 case TRUTH_OR_EXPR:
3041 case TRUTH_XOR_EXPR:
3042 case TRUTH_NOT_EXPR:
3043 /* Conversion from boolean to a signed:1 bit-field (which only
3044 can hold the values 0 and -1) doesn't lose information - but
3045 it does change the value. */
3046 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
3047 warning_at (loc, OPT_Wconversion,
3048 "conversion to %qT from boolean expression", type);
3049 return;
3050
3051 case REAL_CST:
3052 case INTEGER_CST:
e53013a8 3053 case COMPLEX_CST:
22a75734 3054 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
ca9d7d74 3055 if (conversion_kind == UNSAFE_REAL)
3056 warning_at (loc, OPT_Wfloat_conversion,
3057 "conversion to %qT alters %qT constant value",
3058 type, expr_type);
3059 else if (conversion_kind)
7dfa155b 3060 warning_at (loc, OPT_Wconversion,
3061 "conversion to %qT alters %qT constant value",
3062 type, expr_type);
3063 return;
3064
3065 case COND_EXPR:
3066 {
0e4e775a 3067 /* In case of COND_EXPR, we do not care about the type of
3068 COND_EXPR, only about the conversion of each operand. */
3069 tree op1 = TREE_OPERAND (expr, 1);
3070 tree op2 = TREE_OPERAND (expr, 2);
3071
22a75734 3072 conversion_warning (loc, type, op1);
3073 conversion_warning (loc, type, op2);
0e4e775a 3074 return;
7dfa155b 3075 }
3076
3077 default: /* 'expr' is not a constant. */
22a75734 3078 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
ca9d7d74 3079 if (conversion_kind == UNSAFE_REAL)
3080 warning_at (loc, OPT_Wfloat_conversion,
3081 "conversion to %qT from %qT may alter its value",
3082 type, expr_type);
e53013a8 3083 else if (conversion_kind == UNSAFE_IMAGINARY)
3084 warning_at (loc, OPT_Wconversion,
3085 "conversion to %qT from %qT discards imaginary component",
3086 type, expr_type);
ca9d7d74 3087 else if (conversion_kind)
7dfa155b 3088 warning_at (loc, OPT_Wconversion,
200dd99c 3089 "conversion to %qT from %qT may alter its value",
3090 type, expr_type);
d31d55f0 3091 }
3092}
3093
59dd8856 3094/* Produce warnings after a conversion. RESULT is the result of
3095 converting EXPR to TYPE. This is a helper function for
3096 convert_and_check and cp_convert_and_check. */
2a1736ed 3097
59dd8856 3098void
22a75734 3099warnings_for_convert_and_check (location_t loc, tree type, tree expr,
3100 tree result)
2a1736ed 3101{
22a75734 3102 loc = expansion_point_location_if_in_system_header (loc);
61f69bc9 3103
da1fb07b 3104 if (TREE_CODE (expr) == INTEGER_CST
3105 && (TREE_CODE (type) == INTEGER_TYPE
3106 || TREE_CODE (type) == ENUMERAL_TYPE)
3107 && !int_fits_type_p (expr, type))
3108 {
d31d55f0 3109 /* Do not diagnose overflow in a constant expression merely
3110 because a conversion overflowed. */
da1fb07b 3111 if (TREE_OVERFLOW (result))
eddad94a 3112 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
3113
da1fb07b 3114 if (TYPE_UNSIGNED (type))
d31d55f0 3115 {
da1fb07b 3116 /* This detects cases like converting -129 or 256 to
3117 unsigned char. */
3118 if (!int_fits_type_p (expr, c_common_signed_type (type)))
61f69bc9 3119 warning_at (loc, OPT_Woverflow,
3120 "large integer implicitly truncated to unsigned type");
7ee0d227 3121 else
22a75734 3122 conversion_warning (loc, type, expr);
da1fb07b 3123 }
48e1416a 3124 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
22a75734 3125 warning_at (loc, OPT_Woverflow,
e0913805 3126 "overflow in implicit constant conversion");
3127 /* No warning for converting 0x80000000 to int. */
3128 else if (pedantic
3129 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
3130 || TYPE_PRECISION (TREE_TYPE (expr))
3131 != TYPE_PRECISION (type)))
61f69bc9 3132 warning_at (loc, OPT_Woverflow,
3133 "overflow in implicit constant conversion");
e0913805 3134
7ee0d227 3135 else
22a75734 3136 conversion_warning (loc, type, expr);
2a1736ed 3137 }
9421ebb9 3138 else if ((TREE_CODE (result) == INTEGER_CST
3139 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
61f69bc9 3140 warning_at (loc, OPT_Woverflow,
3141 "overflow in implicit constant conversion");
7ee0d227 3142 else
22a75734 3143 conversion_warning (loc, type, expr);
59dd8856 3144}
3145
3146
3147/* Convert EXPR to TYPE, warning about conversion problems with constants.
3148 Invoke this function on every expression that is converted implicitly,
3149 i.e. because of language rules and not because of an explicit cast. */
3150
3151tree
22a75734 3152convert_and_check (location_t loc, tree type, tree expr)
59dd8856 3153{
3154 tree result;
c6418a4e 3155 tree expr_for_warning;
3156
3157 /* Convert from a value with possible excess precision rather than
3158 via the semantic type, but do not warn about values not fitting
3159 exactly in the semantic type. */
3160 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
3161 {
3162 tree orig_type = TREE_TYPE (expr);
3163 expr = TREE_OPERAND (expr, 0);
3164 expr_for_warning = convert (orig_type, expr);
3165 if (orig_type == type)
3166 return expr_for_warning;
3167 }
3168 else
3169 expr_for_warning = expr;
59dd8856 3170
3171 if (TREE_TYPE (expr) == type)
3172 return expr;
48e1416a 3173
59dd8856 3174 result = convert (type, expr);
3175
48d94ede 3176 if (c_inhibit_evaluation_warnings == 0
3177 && !TREE_OVERFLOW_P (expr)
3178 && result != error_mark_node)
22a75734 3179 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
59dd8856 3180
da1fb07b 3181 return result;
b2806639 3182}
3183\f
4e91a871 3184/* A node in a list that describes references to variables (EXPR), which are
3185 either read accesses if WRITER is zero, or write accesses, in which case
3186 WRITER is the parent of EXPR. */
3187struct tlist
3188{
3189 struct tlist *next;
3190 tree expr, writer;
3191};
3192
3193/* Used to implement a cache the results of a call to verify_tree. We only
3194 use this for SAVE_EXPRs. */
3195struct tlist_cache
3196{
3197 struct tlist_cache *next;
3198 struct tlist *cache_before_sp;
3199 struct tlist *cache_after_sp;
3200 tree expr;
481c6ce6 3201};
3202
4e91a871 3203/* Obstack to use when allocating tlist structures, and corresponding
3204 firstobj. */
3205static struct obstack tlist_obstack;
3206static char *tlist_firstobj = 0;
3207
3208/* Keep track of the identifiers we've warned about, so we can avoid duplicate
3209 warnings. */
3210static struct tlist *warned_ids;
3211/* SAVE_EXPRs need special treatment. We process them only once and then
3212 cache the results. */
3213static struct tlist_cache *save_expr_cache;
3214
1cae46be 3215static void add_tlist (struct tlist **, struct tlist *, tree, int);
3216static void merge_tlist (struct tlist **, struct tlist *, int);
3217static void verify_tree (tree, struct tlist **, struct tlist **, tree);
3218static int warning_candidate_p (tree);
79973b57 3219static bool candidate_equal_p (const_tree, const_tree);
1cae46be 3220static void warn_for_collisions (struct tlist *);
3221static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
3222static struct tlist *new_tlist (struct tlist *, tree, tree);
481c6ce6 3223
4e91a871 3224/* Create a new struct tlist and fill in its fields. */
3225static struct tlist *
1cae46be 3226new_tlist (struct tlist *next, tree t, tree writer)
4e91a871 3227{
3228 struct tlist *l;
9318f22c 3229 l = XOBNEW (&tlist_obstack, struct tlist);
4e91a871 3230 l->next = next;
3231 l->expr = t;
3232 l->writer = writer;
3233 return l;
3234}
3235
3236/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
3237 is nonnull, we ignore any node we find which has a writer equal to it. */
3238
3239static void
1cae46be 3240add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
4e91a871 3241{
3242 while (add)
3243 {
3244 struct tlist *next = add->next;
84166705 3245 if (!copy)
4e91a871 3246 add->next = *to;
79973b57 3247 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
4e91a871 3248 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
3249 add = next;
3250 }
3251}
3252
3253/* Merge the nodes of ADD into TO. This merging process is done so that for
3254 each variable that already exists in TO, no new node is added; however if
3255 there is a write access recorded in ADD, and an occurrence on TO is only
3256 a read access, then the occurrence in TO will be modified to record the
3257 write. */
481c6ce6 3258
3259static void
1cae46be 3260merge_tlist (struct tlist **to, struct tlist *add, int copy)
4e91a871 3261{
3262 struct tlist **end = to;
3263
3264 while (*end)
3265 end = &(*end)->next;
3266
3267 while (add)
3268 {
3269 int found = 0;
3270 struct tlist *tmp2;
3271 struct tlist *next = add->next;
3272
3273 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
79973b57 3274 if (candidate_equal_p (tmp2->expr, add->expr))
4e91a871 3275 {
3276 found = 1;
84166705 3277 if (!tmp2->writer)
4e91a871 3278 tmp2->writer = add->writer;
3279 }
84166705 3280 if (!found)
4e91a871 3281 {
312243bb 3282 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
4e91a871 3283 end = &(*end)->next;
3284 *end = 0;
3285 }
3286 add = next;
3287 }
3288}
3289
3290/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
3291 references in list LIST conflict with it, excluding reads if ONLY writers
3292 is nonzero. */
3293
3294static void
1cae46be 3295warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
3296 int only_writes)
4e91a871 3297{
3298 struct tlist *tmp;
3299
3300 /* Avoid duplicate warnings. */
3301 for (tmp = warned_ids; tmp; tmp = tmp->next)
79973b57 3302 if (candidate_equal_p (tmp->expr, written))
4e91a871 3303 return;
3304
3305 while (list)
3306 {
79973b57 3307 if (candidate_equal_p (list->expr, written)
3308 && !candidate_equal_p (list->writer, writer)
3309 && (!only_writes || list->writer))
4e91a871 3310 {
3311 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3df42822 3312 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
6513b50d 3313 OPT_Wsequence_point, "operation on %qE may be undefined",
3314 list->expr);
4e91a871 3315 }
3316 list = list->next;
3317 }
3318}
3319
3320/* Given a list LIST of references to variables, find whether any of these
3321 can cause conflicts due to missing sequence points. */
3322
3323static void
1cae46be 3324warn_for_collisions (struct tlist *list)
4e91a871 3325{
3326 struct tlist *tmp;
1cae46be 3327
4e91a871 3328 for (tmp = list; tmp; tmp = tmp->next)
3329 {
3330 if (tmp->writer)
3331 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
3332 }
3333}
3334
734c98be 3335/* Return nonzero if X is a tree that can be verified by the sequence point
4e91a871 3336 warnings. */
3337static int
1cae46be 3338warning_candidate_p (tree x)
481c6ce6 3339{
6ef8d12f 3340 if (DECL_P (x) && DECL_ARTIFICIAL (x))
3341 return 0;
3342
027fc6ef 3343 if (TREE_CODE (x) == BLOCK)
3344 return 0;
3345
6ef8d12f 3346 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
79973b57 3347 (lvalue_p) crash on TRY/CATCH. */
6ef8d12f 3348 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
3349 return 0;
3350
3351 if (!lvalue_p (x))
3352 return 0;
3353
3354 /* No point to track non-const calls, they will never satisfy
3355 operand_equal_p. */
3356 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
3357 return 0;
3358
3359 if (TREE_CODE (x) == STRING_CST)
3360 return 0;
3361
3362 return 1;
79973b57 3363}
3364
3365/* Return nonzero if X and Y appear to be the same candidate (or NULL) */
3366static bool
3367candidate_equal_p (const_tree x, const_tree y)
3368{
3369 return (x == y) || (x && y && operand_equal_p (x, y, 0));
4e91a871 3370}
481c6ce6 3371
4e91a871 3372/* Walk the tree X, and record accesses to variables. If X is written by the
3373 parent tree, WRITER is the parent.
3374 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
3375 expression or its only operand forces a sequence point, then everything up
3376 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3377 in PNO_SP.
3378 Once we return, we will have emitted warnings if any subexpression before
3379 such a sequence point could be undefined. On a higher level, however, the
3380 sequence point may not be relevant, and we'll merge the two lists.
3381
3382 Example: (b++, a) + b;
3383 The call that processes the COMPOUND_EXPR will store the increment of B
3384 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3385 processes the PLUS_EXPR will need to merge the two lists so that
3386 eventually, all accesses end up on the same list (and we'll warn about the
3387 unordered subexpressions b++ and b.
3388
3389 A note on merging. If we modify the former example so that our expression
3390 becomes
3391 (b++, b) + a
3392 care must be taken not simply to add all three expressions into the final
3393 PNO_SP list. The function merge_tlist takes care of that by merging the
3394 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3395 way, so that no more than one access to B is recorded. */
481c6ce6 3396
4e91a871 3397static void
1cae46be 3398verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3399 tree writer)
4e91a871 3400{
3401 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3402 enum tree_code code;
ce45a448 3403 enum tree_code_class cl;
481c6ce6 3404
e5b75768 3405 /* X may be NULL if it is the operand of an empty statement expression
3406 ({ }). */
3407 if (x == NULL)
3408 return;
3409
4e91a871 3410 restart:
3411 code = TREE_CODE (x);
e916c70c 3412 cl = TREE_CODE_CLASS (code);
481c6ce6 3413
4e91a871 3414 if (warning_candidate_p (x))
79973b57 3415 *pno_sp = new_tlist (*pno_sp, x, writer);
4e91a871 3416
3417 switch (code)
3418 {
67b28e3e 3419 case CONSTRUCTOR:
8e71dad2 3420 case SIZEOF_EXPR:
67b28e3e 3421 return;
3422
4e91a871 3423 case COMPOUND_EXPR:
3424 case TRUTH_ANDIF_EXPR:
3425 case TRUTH_ORIF_EXPR:
3426 tmp_before = tmp_nosp = tmp_list3 = 0;
3427 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3428 warn_for_collisions (tmp_nosp);
3429 merge_tlist (pbefore_sp, tmp_before, 0);
3430 merge_tlist (pbefore_sp, tmp_nosp, 0);
3431 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
3432 merge_tlist (pbefore_sp, tmp_list3, 0);
3433 return;
3434
3435 case COND_EXPR:
3436 tmp_before = tmp_list2 = 0;
3437 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3438 warn_for_collisions (tmp_list2);
3439 merge_tlist (pbefore_sp, tmp_before, 0);
312243bb 3440 merge_tlist (pbefore_sp, tmp_list2, 0);
4e91a871 3441
3442 tmp_list3 = tmp_nosp = 0;
3443 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3444 warn_for_collisions (tmp_nosp);
3445 merge_tlist (pbefore_sp, tmp_list3, 0);
3446
3447 tmp_list3 = tmp_list2 = 0;
3448 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3449 warn_for_collisions (tmp_list2);
3450 merge_tlist (pbefore_sp, tmp_list3, 0);
3451 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3452 two first, to avoid warning for (a ? b++ : b++). */
3453 merge_tlist (&tmp_nosp, tmp_list2, 0);
3454 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3455 return;
3456
481c6ce6 3457 case PREDECREMENT_EXPR:
3458 case PREINCREMENT_EXPR:
3459 case POSTDECREMENT_EXPR:
3460 case POSTINCREMENT_EXPR:
4e91a871 3461 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3462 return;
3463
3464 case MODIFY_EXPR:
3465 tmp_before = tmp_nosp = tmp_list3 = 0;
3466 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3467 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3468 /* Expressions inside the LHS are not ordered wrt. the sequence points
3469 in the RHS. Example:
3470 *a = (a++, 2)
3471 Despite the fact that the modification of "a" is in the before_sp
3472 list (tmp_before), it conflicts with the use of "a" in the LHS.
3473 We can handle this by adding the contents of tmp_list3
3474 to those of tmp_before, and redoing the collision warnings for that
3475 list. */
3476 add_tlist (&tmp_before, tmp_list3, x, 1);
3477 warn_for_collisions (tmp_before);
3478 /* Exclude the LHS itself here; we first have to merge it into the
3479 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3480 didn't exclude the LHS, we'd get it twice, once as a read and once
3481 as a write. */
3482 add_tlist (pno_sp, tmp_list3, x, 0);
3483 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3484
3485 merge_tlist (pbefore_sp, tmp_before, 0);
3486 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3487 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3488 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3489 return;
481c6ce6 3490
3491 case CALL_EXPR:
4e91a871 3492 /* We need to warn about conflicts among arguments and conflicts between
3493 args and the function address. Side effects of the function address,
3494 however, are not ordered by the sequence point of the call. */
c2f47e15 3495 {
3496 call_expr_arg_iterator iter;
3497 tree arg;
48e1416a 3498 tmp_before = tmp_nosp = 0;
c2f47e15 3499 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3500 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3501 {
3502 tmp_list2 = tmp_list3 = 0;
3503 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3504 merge_tlist (&tmp_list3, tmp_list2, 0);
3505 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3506 }
3507 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3508 warn_for_collisions (tmp_before);
3509 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3510 return;
3511 }
481c6ce6 3512
3513 case TREE_LIST:
3514 /* Scan all the list, e.g. indices of multi dimensional array. */
3515 while (x)
3516 {
4e91a871 3517 tmp_before = tmp_nosp = 0;
3518 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3519 merge_tlist (&tmp_nosp, tmp_before, 0);
3520 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
481c6ce6 3521 x = TREE_CHAIN (x);
3522 }
4e91a871 3523 return;
481c6ce6 3524
4e91a871 3525 case SAVE_EXPR:
3526 {
3527 struct tlist_cache *t;
3528 for (t = save_expr_cache; t; t = t->next)
79973b57 3529 if (candidate_equal_p (t->expr, x))
4e91a871 3530 break;
481c6ce6 3531
84166705 3532 if (!t)
481c6ce6 3533 {
9318f22c 3534 t = XOBNEW (&tlist_obstack, struct tlist_cache);
4e91a871 3535 t->next = save_expr_cache;
3536 t->expr = x;
3537 save_expr_cache = t;
3538
3539 tmp_before = tmp_nosp = 0;
3540 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3541 warn_for_collisions (tmp_nosp);
3542
3543 tmp_list3 = 0;
312243bb 3544 merge_tlist (&tmp_list3, tmp_nosp, 0);
4e91a871 3545 t->cache_before_sp = tmp_before;
3546 t->cache_after_sp = tmp_list3;
481c6ce6 3547 }
4e91a871 3548 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3549 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3550 return;
3551 }
481c6ce6 3552
012916cb 3553 case ADDR_EXPR:
3554 x = TREE_OPERAND (x, 0);
3555 if (DECL_P (x))
3556 return;
3557 writer = 0;
3558 goto restart;
3559
ce45a448 3560 default:
3561 /* For other expressions, simply recurse on their operands.
a0c938f0 3562 Manual tail recursion for unary expressions.
ce45a448 3563 Other non-expressions need not be processed. */
3564 if (cl == tcc_unary)
3565 {
ce45a448 3566 x = TREE_OPERAND (x, 0);
3567 writer = 0;
3568 goto restart;
3569 }
3570 else if (IS_EXPR_CODE_CLASS (cl))
3571 {
3572 int lp;
c2f47e15 3573 int max = TREE_OPERAND_LENGTH (x);
ce45a448 3574 for (lp = 0; lp < max; lp++)
3575 {
3576 tmp_before = tmp_nosp = 0;
3577 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3578 merge_tlist (&tmp_nosp, tmp_before, 0);
3579 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3580 }
3581 }
3582 return;
481c6ce6 3583 }
481c6ce6 3584}
3585
974e2c0c 3586/* Try to warn for undefined behavior in EXPR due to missing sequence
481c6ce6 3587 points. */
3588
4b987fac 3589DEBUG_FUNCTION void
1cae46be 3590verify_sequence_points (tree expr)
481c6ce6 3591{
4e91a871 3592 struct tlist *before_sp = 0, *after_sp = 0;
481c6ce6 3593
4e91a871 3594 warned_ids = 0;
3595 save_expr_cache = 0;
3596 if (tlist_firstobj == 0)
481c6ce6 3597 {
4e91a871 3598 gcc_obstack_init (&tlist_obstack);
4fd61bc6 3599 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
481c6ce6 3600 }
3601
4e91a871 3602 verify_tree (expr, &before_sp, &after_sp, 0);
3603 warn_for_collisions (after_sp);
3604 obstack_free (&tlist_obstack, tlist_firstobj);
481c6ce6 3605}
b0fc3e72 3606\f
3607/* Validate the expression after `case' and apply default promotions. */
3608
2ca392fd 3609static tree
2d2f6a15 3610check_case_value (location_t loc, tree value)
b0fc3e72 3611{
3612 if (value == NULL_TREE)
3613 return value;
3614
b96dc121 3615 if (TREE_CODE (value) == INTEGER_CST)
3616 /* Promote char or short to int. */
3617 value = perform_integral_promotions (value);
3618 else if (value != error_mark_node)
b0fc3e72 3619 {
2d2f6a15 3620 error_at (loc, "case label does not reduce to an integer constant");
b0fc3e72 3621 value = error_mark_node;
3622 }
b0fc3e72 3623
6433f1c2 3624 constant_expression_warning (value);
3625
b0fc3e72 3626 return value;
3627}
3628\f
2ca392fd 3629/* See if the case values LOW and HIGH are in the range of the original
5c9dae64 3630 type (i.e. before the default conversion to int) of the switch testing
2ca392fd 3631 expression.
3632 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
91275768 3633 the type before promoting it. CASE_LOW_P is a pointer to the lower
2ca392fd 3634 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3635 if the case is not a case range.
3636 The caller has to make sure that we are not called with NULL for
5c9dae64 3637 CASE_LOW_P (i.e. the default case).
442e3cb9 3638 Returns true if the case label is in range of ORIG_TYPE (saturated or
2ca392fd 3639 untouched) or false if the label is out of range. */
3640
3641static bool
f61a9bc2 3642check_case_bounds (location_t loc, tree type, tree orig_type,
2ca392fd 3643 tree *case_low_p, tree *case_high_p)
3644{
3645 tree min_value, max_value;
3646 tree case_low = *case_low_p;
3647 tree case_high = case_high_p ? *case_high_p : case_low;
3648
3649 /* If there was a problem with the original type, do nothing. */
3650 if (orig_type == error_mark_node)
3651 return true;
3652
3653 min_value = TYPE_MIN_VALUE (orig_type);
3654 max_value = TYPE_MAX_VALUE (orig_type);
3655
3656 /* Case label is less than minimum for type. */
3657 if (tree_int_cst_compare (case_low, min_value) < 0
3658 && tree_int_cst_compare (case_high, min_value) < 0)
3659 {
f61a9bc2 3660 warning_at (loc, 0, "case label value is less than minimum value "
3661 "for type");
2ca392fd 3662 return false;
3663 }
b27ac6b5 3664
2ca392fd 3665 /* Case value is greater than maximum for type. */
3666 if (tree_int_cst_compare (case_low, max_value) > 0
3667 && tree_int_cst_compare (case_high, max_value) > 0)
3668 {
f61a9bc2 3669 warning_at (loc, 0, "case label value exceeds maximum value for type");
2ca392fd 3670 return false;
3671 }
3672
3673 /* Saturate lower case label value to minimum. */
3674 if (tree_int_cst_compare (case_high, min_value) >= 0
3675 && tree_int_cst_compare (case_low, min_value) < 0)
3676 {
f61a9bc2 3677 warning_at (loc, 0, "lower value in case label range"
3678 " less than minimum value for type");
2ca392fd 3679 case_low = min_value;
3680 }
b27ac6b5 3681
2ca392fd 3682 /* Saturate upper case label value to maximum. */
3683 if (tree_int_cst_compare (case_low, max_value) <= 0
3684 && tree_int_cst_compare (case_high, max_value) > 0)
3685 {
f61a9bc2 3686 warning_at (loc, 0, "upper value in case label range"
3687 " exceeds maximum value for type");
2ca392fd 3688 case_high = max_value;
3689 }
3690
3691 if (*case_low_p != case_low)
3692 *case_low_p = convert (type, case_low);
3693 if (case_high_p && *case_high_p != case_high)
3694 *case_high_p = convert (type, case_high);
3695
3696 return true;
3697}
3698\f
b0fc3e72 3699/* Return an integer type with BITS bits of precision,
3700 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3701
3702tree
1cae46be 3703c_common_type_for_size (unsigned int bits, int unsignedp)
b0fc3e72 3704{
9f75f026 3705 int i;
3706
46375237 3707 if (bits == TYPE_PRECISION (integer_type_node))
3708 return unsignedp ? unsigned_type_node : integer_type_node;
3709
bacde65a 3710 if (bits == TYPE_PRECISION (signed_char_type_node))
b0fc3e72 3711 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3712
bacde65a 3713 if (bits == TYPE_PRECISION (short_integer_type_node))
b0fc3e72 3714 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3715
bacde65a 3716 if (bits == TYPE_PRECISION (long_integer_type_node))
b0fc3e72 3717 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3718
bacde65a 3719 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b0fc3e72 3720 return (unsignedp ? long_long_unsigned_type_node
3721 : long_long_integer_type_node);
3722
9f75f026 3723 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3724 if (int_n_enabled_p[i]
3725 && bits == int_n_data[i].bitsize)
3726 return (unsignedp ? int_n_trees[i].unsigned_type
3727 : int_n_trees[i].signed_type);
6388cfe2 3728
f57fa2ea 3729 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3730 return (unsignedp ? widest_unsigned_literal_type_node
3731 : widest_integer_literal_type_node);
3732
bacde65a 3733 if (bits <= TYPE_PRECISION (intQI_type_node))
3734 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3735
3736 if (bits <= TYPE_PRECISION (intHI_type_node))
3737 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3738
3739 if (bits <= TYPE_PRECISION (intSI_type_node))
3740 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3741
3742 if (bits <= TYPE_PRECISION (intDI_type_node))
3743 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3744
b0fc3e72 3745 return 0;
3746}
3747
9421ebb9 3748/* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3749 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3750 and saturating if SATP is nonzero, otherwise not saturating. */
3751
3752tree
3753c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3754 int unsignedp, int satp)
3755{
3754d046 3756 machine_mode mode;
9421ebb9 3757 if (ibit == 0)
3758 mode = unsignedp ? UQQmode : QQmode;
3759 else
3760 mode = unsignedp ? UHAmode : HAmode;
3761
3762 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3763 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3764 break;
3765
3766 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3767 {
3768 sorry ("GCC cannot support operators with integer types and "
3769 "fixed-point types that have too many integral and "
3770 "fractional bits together");
3771 return 0;
3772 }
3773
3774 return c_common_type_for_mode (mode, satp);
3775}
3776
5b247e9f 3777/* Used for communication between c_common_type_for_mode and
3778 c_register_builtin_type. */
c1917557 3779tree registered_builtin_types;
5b247e9f 3780
b0fc3e72 3781/* Return a data type that has machine mode MODE.
3782 If the mode is an integer,
9421ebb9 3783 then UNSIGNEDP selects between signed and unsigned types.
3784 If the mode is a fixed-point mode,
3785 then UNSIGNEDP selects between saturating and nonsaturating types. */
b0fc3e72 3786
3787tree
3754d046 3788c_common_type_for_mode (machine_mode mode, int unsignedp)
b0fc3e72 3789{
5b247e9f 3790 tree t;
9f75f026 3791 int i;
5b247e9f 3792
46375237 3793 if (mode == TYPE_MODE (integer_type_node))
3794 return unsignedp ? unsigned_type_node : integer_type_node;
3795
b0fc3e72 3796 if (mode == TYPE_MODE (signed_char_type_node))
3797 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3798
3799 if (mode == TYPE_MODE (short_integer_type_node))
3800 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3801
b0fc3e72 3802 if (mode == TYPE_MODE (long_integer_type_node))
3803 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3804
3805 if (mode == TYPE_MODE (long_long_integer_type_node))
3806 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3807
9f75f026 3808 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3809 if (int_n_enabled_p[i]
3810 && mode == int_n_data[i].m)
3811 return (unsignedp ? int_n_trees[i].unsigned_type
3812 : int_n_trees[i].signed_type);
6388cfe2 3813
f57fa2ea 3814 if (mode == TYPE_MODE (widest_integer_literal_type_node))
44e9fa65 3815 return unsignedp ? widest_unsigned_literal_type_node
4ee9c684 3816 : widest_integer_literal_type_node;
f57fa2ea 3817
88ae7f04 3818 if (mode == QImode)
bacde65a 3819 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3820
88ae7f04 3821 if (mode == HImode)
bacde65a 3822 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3823
88ae7f04 3824 if (mode == SImode)
bacde65a 3825 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3826
88ae7f04 3827 if (mode == DImode)
bacde65a 3828 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
ab2c1de8 3829
cc1cc1c7 3830#if HOST_BITS_PER_WIDE_INT >= 64
6274009c 3831 if (mode == TYPE_MODE (intTI_type_node))
3832 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
cc1cc1c7 3833#endif
6274009c 3834
b0fc3e72 3835 if (mode == TYPE_MODE (float_type_node))
3836 return float_type_node;
3837
3838 if (mode == TYPE_MODE (double_type_node))
3839 return double_type_node;
3840
3841 if (mode == TYPE_MODE (long_double_type_node))
3842 return long_double_type_node;
3843
545c2bde 3844 if (mode == TYPE_MODE (void_type_node))
3845 return void_type_node;
b27ac6b5 3846
b0fc3e72 3847 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
61b9b73c 3848 return (unsignedp
3849 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3850 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3851
3852 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
61b9b73c 3853 return (unsignedp
3854 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3855 : make_signed_type (GET_MODE_PRECISION (mode)));
b0fc3e72 3856
0dfc45b5 3857 if (COMPLEX_MODE_P (mode))
3858 {
3754d046 3859 machine_mode inner_mode;
0dfc45b5 3860 tree inner_type;
3861
3862 if (mode == TYPE_MODE (complex_float_type_node))
3863 return complex_float_type_node;
3864 if (mode == TYPE_MODE (complex_double_type_node))
3865 return complex_double_type_node;
3866 if (mode == TYPE_MODE (complex_long_double_type_node))
3867 return complex_long_double_type_node;
3868
3869 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3870 return complex_integer_type_node;
3871
3872 inner_mode = GET_MODE_INNER (mode);
3873 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3874 if (inner_type != NULL_TREE)
3875 return build_complex_type (inner_type);
3876 }
3877 else if (VECTOR_MODE_P (mode))
4917c376 3878 {
3754d046 3879 machine_mode inner_mode = GET_MODE_INNER (mode);
4917c376 3880 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3881 if (inner_type != NULL_TREE)
3882 return build_vector_type_for_mode (inner_type, mode);
88ae7f04 3883 }
e2ea7e3a 3884
c4503c0a 3885 if (mode == TYPE_MODE (dfloat32_type_node))
3886 return dfloat32_type_node;
3887 if (mode == TYPE_MODE (dfloat64_type_node))
3888 return dfloat64_type_node;
3889 if (mode == TYPE_MODE (dfloat128_type_node))
3890 return dfloat128_type_node;
3891
9421ebb9 3892 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3893 {
3894 if (mode == TYPE_MODE (short_fract_type_node))
3895 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3896 if (mode == TYPE_MODE (fract_type_node))
3897 return unsignedp ? sat_fract_type_node : fract_type_node;
3898 if (mode == TYPE_MODE (long_fract_type_node))
3899 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3900 if (mode == TYPE_MODE (long_long_fract_type_node))
3901 return unsignedp ? sat_long_long_fract_type_node
3902 : long_long_fract_type_node;
3903
3904 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3905 return unsignedp ? sat_unsigned_short_fract_type_node
3906 : unsigned_short_fract_type_node;
3907 if (mode == TYPE_MODE (unsigned_fract_type_node))
3908 return unsignedp ? sat_unsigned_fract_type_node
3909 : unsigned_fract_type_node;
3910 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3911 return unsignedp ? sat_unsigned_long_fract_type_node
3912 : unsigned_long_fract_type_node;
3913 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3914 return unsignedp ? sat_unsigned_long_long_fract_type_node
3915 : unsigned_long_long_fract_type_node;
3916
3917 if (mode == TYPE_MODE (short_accum_type_node))
3918 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3919 if (mode == TYPE_MODE (accum_type_node))
3920 return unsignedp ? sat_accum_type_node : accum_type_node;
3921 if (mode == TYPE_MODE (long_accum_type_node))
3922 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3923 if (mode == TYPE_MODE (long_long_accum_type_node))
3924 return unsignedp ? sat_long_long_accum_type_node
3925 : long_long_accum_type_node;
3926
3927 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3928 return unsignedp ? sat_unsigned_short_accum_type_node
3929 : unsigned_short_accum_type_node;
3930 if (mode == TYPE_MODE (unsigned_accum_type_node))
3931 return unsignedp ? sat_unsigned_accum_type_node
3932 : unsigned_accum_type_node;
3933 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3934 return unsignedp ? sat_unsigned_long_accum_type_node
3935 : unsigned_long_accum_type_node;
3936 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3937 return unsignedp ? sat_unsigned_long_long_accum_type_node
3938 : unsigned_long_long_accum_type_node;
3939
3940 if (mode == QQmode)
3941 return unsignedp ? sat_qq_type_node : qq_type_node;
3942 if (mode == HQmode)
3943 return unsignedp ? sat_hq_type_node : hq_type_node;
3944 if (mode == SQmode)
3945 return unsignedp ? sat_sq_type_node : sq_type_node;
3946 if (mode == DQmode)
3947 return unsignedp ? sat_dq_type_node : dq_type_node;
3948 if (mode == TQmode)
3949 return unsignedp ? sat_tq_type_node : tq_type_node;
3950
3951 if (mode == UQQmode)
3952 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3953 if (mode == UHQmode)
3954 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3955 if (mode == USQmode)
3956 return unsignedp ? sat_usq_type_node : usq_type_node;
3957 if (mode == UDQmode)
3958 return unsignedp ? sat_udq_type_node : udq_type_node;
3959 if (mode == UTQmode)
3960 return unsignedp ? sat_utq_type_node : utq_type_node;
3961
3962 if (mode == HAmode)
3963 return unsignedp ? sat_ha_type_node : ha_type_node;
3964 if (mode == SAmode)
3965 return unsignedp ? sat_sa_type_node : sa_type_node;
3966 if (mode == DAmode)
3967 return unsignedp ? sat_da_type_node : da_type_node;
3968 if (mode == TAmode)
3969 return unsignedp ? sat_ta_type_node : ta_type_node;
3970
3971 if (mode == UHAmode)
3972 return unsignedp ? sat_uha_type_node : uha_type_node;
3973 if (mode == USAmode)
3974 return unsignedp ? sat_usa_type_node : usa_type_node;
3975 if (mode == UDAmode)
3976 return unsignedp ? sat_uda_type_node : uda_type_node;
3977 if (mode == UTAmode)
3978 return unsignedp ? sat_uta_type_node : uta_type_node;
3979 }
3980
5b247e9f 3981 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
baec58e1 3982 if (TYPE_MODE (TREE_VALUE (t)) == mode
3983 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
5b247e9f 3984 return TREE_VALUE (t);
3985
b0fc3e72 3986 return 0;
3987}
20d39783 3988
11773141 3989tree
3990c_common_unsigned_type (tree type)
3991{
3992 return c_common_signed_or_unsigned_type (1, type);
3993}
3994
20d39783 3995/* Return a signed type the same as TYPE in other respects. */
3996
3997tree
1cae46be 3998c_common_signed_type (tree type)
20d39783 3999{
4070745f 4000 return c_common_signed_or_unsigned_type (0, type);
20d39783 4001}
4002
4003/* Return a type the same as TYPE except unsigned or
4004 signed according to UNSIGNEDP. */
4005
4006tree
1cae46be 4007c_common_signed_or_unsigned_type (int unsignedp, tree type)
20d39783 4008{
7a91101f 4009 tree type1;
9f75f026 4010 int i;
20d39783 4011
7a91101f 4012 /* This block of code emulates the behavior of the old
4013 c_common_unsigned_type. In particular, it returns
4014 long_unsigned_type_node if passed a long, even when a int would
4015 have the same size. This is necessary for warnings to work
4016 correctly in archs where sizeof(int) == sizeof(long) */
4017
4018 type1 = TYPE_MAIN_VARIANT (type);
4019 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
4020 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4021 if (type1 == integer_type_node || type1 == unsigned_type_node)
4022 return unsignedp ? unsigned_type_node : integer_type_node;
4023 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
4024 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4025 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
4026 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4027 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
4028 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
9f75f026 4029
4030 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4031 if (int_n_enabled_p[i]
4032 && (type1 == int_n_trees[i].unsigned_type
4033 || type1 == int_n_trees[i].signed_type))
4034 return (unsignedp ? int_n_trees[i].unsigned_type
4035 : int_n_trees[i].signed_type);
4036
7a91101f 4037 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
4038 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
4039#if HOST_BITS_PER_WIDE_INT >= 64
4040 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
4041 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4042#endif
4043 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
4044 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4045 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
4046 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4047 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
4048 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4049 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
4050 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4051
9f92e1a2 4052#define C_COMMON_FIXED_TYPES(NAME) \
4053 if (type1 == short_ ## NAME ## _type_node \
4054 || type1 == unsigned_short_ ## NAME ## _type_node) \
4055 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
4056 : short_ ## NAME ## _type_node; \
4057 if (type1 == NAME ## _type_node \
4058 || type1 == unsigned_ ## NAME ## _type_node) \
4059 return unsignedp ? unsigned_ ## NAME ## _type_node \
4060 : NAME ## _type_node; \
4061 if (type1 == long_ ## NAME ## _type_node \
4062 || type1 == unsigned_long_ ## NAME ## _type_node) \
4063 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
4064 : long_ ## NAME ## _type_node; \
4065 if (type1 == long_long_ ## NAME ## _type_node \
4066 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
4067 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
4068 : long_long_ ## NAME ## _type_node;
4069
4070#define C_COMMON_FIXED_MODE_TYPES(NAME) \
4071 if (type1 == NAME ## _type_node \
4072 || type1 == u ## NAME ## _type_node) \
4073 return unsignedp ? u ## NAME ## _type_node \
4074 : NAME ## _type_node;
4075
4076#define C_COMMON_FIXED_TYPES_SAT(NAME) \
4077 if (type1 == sat_ ## short_ ## NAME ## _type_node \
4078 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
4079 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
4080 : sat_ ## short_ ## NAME ## _type_node; \
4081 if (type1 == sat_ ## NAME ## _type_node \
4082 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
4083 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
4084 : sat_ ## NAME ## _type_node; \
4085 if (type1 == sat_ ## long_ ## NAME ## _type_node \
4086 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
4087 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
4088 : sat_ ## long_ ## NAME ## _type_node; \
4089 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
4090 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
4091 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
4092 : sat_ ## long_long_ ## NAME ## _type_node;
4093
4094#define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
4095 if (type1 == sat_ ## NAME ## _type_node \
4096 || type1 == sat_ ## u ## NAME ## _type_node) \
4097 return unsignedp ? sat_ ## u ## NAME ## _type_node \
4098 : sat_ ## NAME ## _type_node;
4099
4100 C_COMMON_FIXED_TYPES (fract);
4101 C_COMMON_FIXED_TYPES_SAT (fract);
4102 C_COMMON_FIXED_TYPES (accum);
4103 C_COMMON_FIXED_TYPES_SAT (accum);
4104
4105 C_COMMON_FIXED_MODE_TYPES (qq);
4106 C_COMMON_FIXED_MODE_TYPES (hq);
4107 C_COMMON_FIXED_MODE_TYPES (sq);
4108 C_COMMON_FIXED_MODE_TYPES (dq);
4109 C_COMMON_FIXED_MODE_TYPES (tq);
4110 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
4111 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
4112 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
4113 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
4114 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
4115 C_COMMON_FIXED_MODE_TYPES (ha);
4116 C_COMMON_FIXED_MODE_TYPES (sa);
4117 C_COMMON_FIXED_MODE_TYPES (da);
4118 C_COMMON_FIXED_MODE_TYPES (ta);
4119 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
4120 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
4121 C_COMMON_FIXED_MODE_TYPES_SAT (da);
4122 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
9421ebb9 4123
4f7f7efd 4124 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
4125 the precision; they have precision set to match their range, but
4126 may use a wider mode to match an ABI. If we change modes, we may
4127 wind up with bad conversions. For INTEGER_TYPEs in C, must check
4128 the precision as well, so as to yield correct results for
4129 bit-field types. C++ does not have these separate bit-field
4130 types, and producing a signed or unsigned variant of an
4131 ENUMERAL_TYPE may cause other problems as well. */
4132
ac265864 4133 if (!INTEGRAL_TYPE_P (type)
4134 || TYPE_UNSIGNED (type) == unsignedp)
4135 return type;
4136
4f7f7efd 4137#define TYPE_OK(node) \
4138 (TYPE_MODE (type) == TYPE_MODE (node) \
0c4abe5b 4139 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
4f7f7efd 4140 if (TYPE_OK (signed_char_type_node))
20d39783 4141 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4f7f7efd 4142 if (TYPE_OK (integer_type_node))
20d39783 4143 return unsignedp ? unsigned_type_node : integer_type_node;
4f7f7efd 4144 if (TYPE_OK (short_integer_type_node))
20d39783 4145 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4f7f7efd 4146 if (TYPE_OK (long_integer_type_node))
20d39783 4147 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4f7f7efd 4148 if (TYPE_OK (long_long_integer_type_node))
20d39783 4149 return (unsignedp ? long_long_unsigned_type_node
4150 : long_long_integer_type_node);
9f75f026 4151
4152 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4153 if (int_n_enabled_p[i]
4154 && TYPE_MODE (type) == int_n_data[i].m
4155 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
4156 return (unsignedp ? int_n_trees[i].unsigned_type
4157 : int_n_trees[i].signed_type);
4158
4f7f7efd 4159 if (TYPE_OK (widest_integer_literal_type_node))
20d39783 4160 return (unsignedp ? widest_unsigned_literal_type_node
4161 : widest_integer_literal_type_node);
ef11801e 4162
4163#if HOST_BITS_PER_WIDE_INT >= 64
4f7f7efd 4164 if (TYPE_OK (intTI_type_node))
ef11801e 4165 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
4166#endif
4f7f7efd 4167 if (TYPE_OK (intDI_type_node))
ef11801e 4168 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4f7f7efd 4169 if (TYPE_OK (intSI_type_node))
ef11801e 4170 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4f7f7efd 4171 if (TYPE_OK (intHI_type_node))
ef11801e 4172 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4f7f7efd 4173 if (TYPE_OK (intQI_type_node))
ef11801e 4174 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4f7f7efd 4175#undef TYPE_OK
ef11801e 4176
0c4abe5b 4177 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
20d39783 4178}
b268e47e 4179
c0e47fd4 4180/* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
4181
4182tree
4183c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
4184{
9f75f026 4185 int i;
4186
c0e47fd4 4187 /* Extended integer types of the same width as a standard type have
4188 lesser rank, so those of the same width as int promote to int or
4189 unsigned int and are valid for printf formats expecting int or
4190 unsigned int. To avoid such special cases, avoid creating
4191 extended integer types for bit-fields if a standard integer type
4192 is available. */
4193 if (width == TYPE_PRECISION (integer_type_node))
4194 return unsignedp ? unsigned_type_node : integer_type_node;
4195 if (width == TYPE_PRECISION (signed_char_type_node))
4196 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4197 if (width == TYPE_PRECISION (short_integer_type_node))
4198 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4199 if (width == TYPE_PRECISION (long_integer_type_node))
4200 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4201 if (width == TYPE_PRECISION (long_long_integer_type_node))
4202 return (unsignedp ? long_long_unsigned_type_node
4203 : long_long_integer_type_node);
9f75f026 4204 for (i = 0; i < NUM_INT_N_ENTS; i ++)
4205 if (int_n_enabled_p[i]
4206 && width == int_n_data[i].bitsize)
4207 return (unsignedp ? int_n_trees[i].unsigned_type
4208 : int_n_trees[i].signed_type);
c0e47fd4 4209 return build_nonstandard_integer_type (width, unsignedp);
4210}
4211
b268e47e 4212/* The C version of the register_builtin_type langhook. */
4213
4214void
4215c_register_builtin_type (tree type, const char* name)
4216{
4217 tree decl;
4218
e60a6f7b 4219 decl = build_decl (UNKNOWN_LOCATION,
4220 TYPE_DECL, get_identifier (name), type);
b268e47e 4221 DECL_ARTIFICIAL (decl) = 1;
4222 if (!TYPE_NAME (type))
4223 TYPE_NAME (type) = decl;
4224 pushdecl (decl);
5b247e9f 4225
4226 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
b268e47e 4227}
a9b9d10c 4228\f
aff9e656 4229/* Print an error message for invalid operands to arith operation
8e70fb09 4230 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
4231 LOCATION is the location of the message. */
b0fc3e72 4232
4233void
8e70fb09 4234binary_op_error (location_t location, enum tree_code code,
4235 tree type0, tree type1)
b0fc3e72 4236{
19cb6b50 4237 const char *opname;
f03946e4 4238
b0fc3e72 4239 switch (code)
4240 {
b0fc3e72 4241 case PLUS_EXPR:
4242 opname = "+"; break;
4243 case MINUS_EXPR:
4244 opname = "-"; break;
4245 case MULT_EXPR:
4246 opname = "*"; break;
4247 case MAX_EXPR:
4248 opname = "max"; break;
4249 case MIN_EXPR:
4250 opname = "min"; break;
4251 case EQ_EXPR:
4252 opname = "=="; break;
4253 case NE_EXPR:
4254 opname = "!="; break;
4255 case LE_EXPR:
4256 opname = "<="; break;
4257 case GE_EXPR:
4258 opname = ">="; break;
4259 case LT_EXPR:
4260 opname = "<"; break;
4261 case GT_EXPR:
4262 opname = ">"; break;
4263 case LSHIFT_EXPR:
4264 opname = "<<"; break;
4265 case RSHIFT_EXPR:
4266 opname = ">>"; break;
4267 case TRUNC_MOD_EXPR:
66618a1e 4268 case FLOOR_MOD_EXPR:
b0fc3e72 4269 opname = "%"; break;
4270 case TRUNC_DIV_EXPR:
66618a1e 4271 case FLOOR_DIV_EXPR:
b0fc3e72 4272 opname = "/"; break;
4273 case BIT_AND_EXPR:
4274 opname = "&"; break;
4275 case BIT_IOR_EXPR:
4276 opname = "|"; break;
4277 case TRUTH_ANDIF_EXPR:
4278 opname = "&&"; break;
4279 case TRUTH_ORIF_EXPR:
4280 opname = "||"; break;
4281 case BIT_XOR_EXPR:
4282 opname = "^"; break;
31f820d2 4283 default:
315ba355 4284 gcc_unreachable ();
b0fc3e72 4285 }
8e70fb09 4286 error_at (location,
4287 "invalid operands to binary %s (have %qT and %qT)", opname,
4288 type0, type1);
b0fc3e72 4289}
4290\f
03fe1dc2 4291/* Given an expression as a tree, return its original type. Do this
4292 by stripping any conversion that preserves the sign and precision. */
4293static tree
4294expr_original_type (tree expr)
4295{
4296 STRIP_SIGN_NOPS (expr);
4297 return TREE_TYPE (expr);
4298}
4299
b0fc3e72 4300/* Subroutine of build_binary_op, used for comparison operations.
4301 See if the operands have both been converted from subword integer types
4302 and, if so, perhaps change them both back to their original type.
5b511807 4303 This function is also responsible for converting the two operands
4304 to the proper common type for comparison.
b0fc3e72 4305
4306 The arguments of this function are all pointers to local variables
4307 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
4308 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
4309
2623625f 4310 LOC is the location of the comparison.
4311
b0fc3e72 4312 If this function returns nonzero, it means that the comparison has
4313 a constant value. What this function returns is an expression for
4314 that value. */
4315
4316tree
2623625f 4317shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
4318 tree *restype_ptr, enum tree_code *rescode_ptr)
b0fc3e72 4319{
19cb6b50 4320 tree type;
b0fc3e72 4321 tree op0 = *op0_ptr;
4322 tree op1 = *op1_ptr;
4323 int unsignedp0, unsignedp1;
4324 int real1, real2;
4325 tree primop0, primop1;
4326 enum tree_code code = *rescode_ptr;
4327
4328 /* Throw away any conversions to wider types
4329 already present in the operands. */
4330
7f506bca 4331 primop0 = c_common_get_narrower (op0, &unsignedp0);
4332 primop1 = c_common_get_narrower (op1, &unsignedp1);
b0fc3e72 4333
119d06b2 4334 /* If primopN is first sign-extended from primopN's precision to opN's
4335 precision, then zero-extended from opN's precision to
4336 *restype_ptr precision, shortenings might be invalid. */
4337 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
4338 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
4339 && !unsignedp0
4340 && TYPE_UNSIGNED (TREE_TYPE (op0)))
4341 primop0 = op0;
4342 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
4343 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
4344 && !unsignedp1
4345 && TYPE_UNSIGNED (TREE_TYPE (op1)))
4346 primop1 = op1;
4347
b0fc3e72 4348 /* Handle the case that OP0 does not *contain* a conversion
4349 but it *requires* conversion to FINAL_TYPE. */
4350
4351 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
78a8ed03 4352 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
b0fc3e72 4353 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
78a8ed03 4354 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b0fc3e72 4355
4356 /* If one of the operands must be floated, we cannot optimize. */
4357 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4358 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
ab2c1de8 4359
b0fc3e72 4360 /* If first arg is constant, swap the args (changing operation
2bd278cc 4361 so value is preserved), for canonicalization. Don't do this if
4362 the second arg is 0. */
b0fc3e72 4363
2bd278cc 4364 if (TREE_CONSTANT (primop0)
9421ebb9 4365 && !integer_zerop (primop1) && !real_zerop (primop1)
4366 && !fixed_zerop (primop1))
b0fc3e72 4367 {
dfcf26a5 4368 std::swap (primop0, primop1);
4369 std::swap (op0, op1);
b0fc3e72 4370 *op0_ptr = op0;
4371 *op1_ptr = op1;
dfcf26a5 4372 std::swap (unsignedp0, unsignedp1);
4373 std::swap (real1, real2);
b0fc3e72 4374
4375 switch (code)
4376 {
4377 case LT_EXPR:
4378 code = GT_EXPR;
4379 break;
4380 case GT_EXPR:
4381 code = LT_EXPR;
4382 break;
4383 case LE_EXPR:
4384 code = GE_EXPR;
4385 break;
4386 case GE_EXPR:
4387 code = LE_EXPR;
4388 break;
31f820d2 4389 default:
4390 break;
b0fc3e72 4391 }
4392 *rescode_ptr = code;
4393 }
4394
4395 /* If comparing an integer against a constant more bits wide,
4396 maybe we can deduce a value of 1 or 0 independent of the data.
4397 Or else truncate the constant now
4398 rather than extend the variable at run time.
4399
4400 This is only interesting if the constant is the wider arg.
4401 Also, it is not safe if the constant is unsigned and the
4402 variable arg is signed, since in this case the variable
4403 would be sign-extended and then regarded as unsigned.
4404 Our technique fails in this case because the lowest/highest
4405 possible unsigned results don't follow naturally from the
4406 lowest/highest possible values of the variable operand.
4407 For just EQ_EXPR and NE_EXPR there is another technique that
4408 could be used: see if the constant can be faithfully represented
4409 in the other operand's type, by truncating it and reextending it
4410 and see if that preserves the constant's value. */
4411
4412 if (!real1 && !real2
9421ebb9 4413 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
b0fc3e72 4414 && TREE_CODE (primop1) == INTEGER_CST
4415 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4416 {
4417 int min_gt, max_gt, min_lt, max_lt;
4418 tree maxval, minval;
4419 /* 1 if comparison is nominally unsigned. */
78a8ed03 4420 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
b0fc3e72 4421 tree val;
4422
4070745f 4423 type = c_common_signed_or_unsigned_type (unsignedp0,
4424 TREE_TYPE (primop0));
cda09c61 4425
b0fc3e72 4426 maxval = TYPE_MAX_VALUE (type);
4427 minval = TYPE_MIN_VALUE (type);
4428
4429 if (unsignedp && !unsignedp0)
4070745f 4430 *restype_ptr = c_common_signed_type (*restype_ptr);
b0fc3e72 4431
4432 if (TREE_TYPE (primop1) != *restype_ptr)
18dbec6f 4433 {
9a5e8086 4434 /* Convert primop1 to target type, but do not introduce
4435 additional overflow. We know primop1 is an int_cst. */
e913b5cd 4436 primop1 = force_fit_type (*restype_ptr,
796b6678 4437 wide_int::from
4438 (primop1,
4439 TYPE_PRECISION (*restype_ptr),
4440 TYPE_SIGN (TREE_TYPE (primop1))),
e913b5cd 4441 0, TREE_OVERFLOW (primop1));
18dbec6f 4442 }
b0fc3e72 4443 if (type != *restype_ptr)
4444 {
4445 minval = convert (*restype_ptr, minval);
4446 maxval = convert (*restype_ptr, maxval);
4447 }
4448
d99d10ca 4449 min_gt = tree_int_cst_lt (primop1, minval);
4450 max_gt = tree_int_cst_lt (primop1, maxval);
4451 min_lt = tree_int_cst_lt (minval, primop1);
4452 max_lt = tree_int_cst_lt (maxval, primop1);
b0fc3e72 4453
4454 val = 0;
4455 /* This used to be a switch, but Genix compiler can't handle that. */
4456 if (code == NE_EXPR)
4457 {
4458 if (max_lt || min_gt)
3c2239cf 4459 val = truthvalue_true_node;
b0fc3e72 4460 }
4461 else if (code == EQ_EXPR)
4462 {
4463 if (max_lt || min_gt)
3c2239cf 4464 val = truthvalue_false_node;
b0fc3e72 4465 }
4466 else if (code == LT_EXPR)
4467 {
4468 if (max_lt)
3c2239cf 4469 val = truthvalue_true_node;
b0fc3e72 4470 if (!min_lt)
3c2239cf 4471 val = truthvalue_false_node;
b0fc3e72 4472 }
4473 else if (code == GT_EXPR)
4474 {
4475 if (min_gt)
3c2239cf 4476 val = truthvalue_true_node;
b0fc3e72 4477 if (!max_gt)
3c2239cf 4478 val = truthvalue_false_node;
b0fc3e72 4479 }
4480 else if (code == LE_EXPR)
4481 {
4482 if (!max_gt)
3c2239cf 4483 val = truthvalue_true_node;
b0fc3e72 4484 if (min_gt)
3c2239cf 4485 val = truthvalue_false_node;
b0fc3e72 4486 }
4487 else if (code == GE_EXPR)
4488 {
4489 if (!min_lt)
3c2239cf 4490 val = truthvalue_true_node;
b0fc3e72 4491 if (max_lt)
3c2239cf 4492 val = truthvalue_false_node;
b0fc3e72 4493 }
4494
4495 /* If primop0 was sign-extended and unsigned comparison specd,
4496 we did a signed comparison above using the signed type bounds.
4497 But the comparison we output must be unsigned.
4498
4499 Also, for inequalities, VAL is no good; but if the signed
4500 comparison had *any* fixed result, it follows that the
4501 unsigned comparison just tests the sign in reverse
4502 (positive values are LE, negative ones GE).
4503 So we can generate an unsigned comparison
4504 against an extreme value of the signed type. */
4505
4506 if (unsignedp && !unsignedp0)
4507 {
4508 if (val != 0)
4509 switch (code)
4510 {
4511 case LT_EXPR:
4512 case GE_EXPR:
4513 primop1 = TYPE_MIN_VALUE (type);
4514 val = 0;
4515 break;
4516
4517 case LE_EXPR:
4518 case GT_EXPR:
4519 primop1 = TYPE_MAX_VALUE (type);
4520 val = 0;
4521 break;
31f820d2 4522
4523 default:
4524 break;
b0fc3e72 4525 }
11773141 4526 type = c_common_unsigned_type (type);
b0fc3e72 4527 }
4528
734ec290 4529 if (TREE_CODE (primop0) != INTEGER_CST)
b0fc3e72 4530 {
3c2239cf 4531 if (val == truthvalue_false_node)
03fe1dc2 4532 warning_at (loc, OPT_Wtype_limits,
4533 "comparison is always false due to limited range of data type");
3c2239cf 4534 if (val == truthvalue_true_node)
03fe1dc2 4535 warning_at (loc, OPT_Wtype_limits,
4536 "comparison is always true due to limited range of data type");
b0fc3e72 4537 }
4538
4539 if (val != 0)
4540 {
4541 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4542 if (TREE_SIDE_EFFECTS (primop0))
14ae0310 4543 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
b0fc3e72 4544 return val;
4545 }
4546
4547 /* Value is not predetermined, but do the comparison
4548 in the type of the operand that is not constant.
4549 TYPE is already properly set. */
4550 }
c4503c0a 4551
4552 /* If either arg is decimal float and the other is float, find the
4553 proper common type to use for comparison. */
7fd22aae 4554 else if (real1 && real2
4555 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4556 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4557 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4558
4559 /* If either arg is decimal float and the other is float, fail. */
c4503c0a 4560 else if (real1 && real2
4561 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4562 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
7fd22aae 4563 return 0;
c4503c0a 4564
b0fc3e72 4565 else if (real1 && real2
2203bd5c 4566 && (TYPE_PRECISION (TREE_TYPE (primop0))
4567 == TYPE_PRECISION (TREE_TYPE (primop1))))
b0fc3e72 4568 type = TREE_TYPE (primop0);
4569
4570 /* If args' natural types are both narrower than nominal type
4571 and both extend in the same manner, compare them
4572 in the type of the wider arg.
4573 Otherwise must actually extend both to the nominal
4574 common type lest different ways of extending
4575 alter the result.
4576 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4577
4578 else if (unsignedp0 == unsignedp1 && real1 == real2
4579 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4580 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4581 {
4582 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4070745f 4583 type = c_common_signed_or_unsigned_type (unsignedp0
78a8ed03 4584 || TYPE_UNSIGNED (*restype_ptr),
4070745f 4585 type);
b0fc3e72 4586 /* Make sure shorter operand is extended the right way
4587 to match the longer operand. */
4070745f 4588 primop0
4589 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4590 TREE_TYPE (primop0)),
4591 primop0);
4592 primop1
4593 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4594 TREE_TYPE (primop1)),
4595 primop1);
b0fc3e72 4596 }
4597 else
4598 {
4599 /* Here we must do the comparison on the nominal type
4600 using the args exactly as we received them. */
4601 type = *restype_ptr;
4602 primop0 = op0;
4603 primop1 = op1;
4604
4605 if (!real1 && !real2 && integer_zerop (primop1)
78a8ed03 4606 && TYPE_UNSIGNED (*restype_ptr))
b0fc3e72 4607 {
4608 tree value = 0;
03fe1dc2 4609 /* All unsigned values are >= 0, so we warn. However,
4610 if OP0 is a constant that is >= 0, the signedness of
4611 the comparison isn't an issue, so suppress the
4612 warning. */
4613 bool warn =
3df42822 4614 warn_type_limits && !in_system_header_at (loc)
03fe1dc2 4615 && !(TREE_CODE (primop0) == INTEGER_CST
4616 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4617 primop0)))
4618 /* Do not warn for enumeration types. */
4619 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4620
b0fc3e72 4621 switch (code)
4622 {
4623 case GE_EXPR:
03fe1dc2 4624 if (warn)
4625 warning_at (loc, OPT_Wtype_limits,
4626 "comparison of unsigned expression >= 0 is always true");
3c2239cf 4627 value = truthvalue_true_node;
b0fc3e72 4628 break;
4629
4630 case LT_EXPR:
03fe1dc2 4631 if (warn)
4632 warning_at (loc, OPT_Wtype_limits,
4633 "comparison of unsigned expression < 0 is always false");
3c2239cf 4634 value = truthvalue_false_node;
31f820d2 4635 break;
4636
4637 default:
4638 break;
b0fc3e72 4639 }
4640
4641 if (value != 0)
4642 {
4643 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4644 if (TREE_SIDE_EFFECTS (primop0))
14ae0310 4645 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4646 primop0, value);
b0fc3e72 4647 return value;
4648 }
4649 }
4650 }
4651
4652 *op0_ptr = convert (type, primop0);
4653 *op1_ptr = convert (type, primop1);
4654
3c2239cf 4655 *restype_ptr = truthvalue_type_node;
b0fc3e72 4656
4657 return 0;
4658}
4659\f
1c26100f 4660/* Return a tree for the sum or difference (RESULTCODE says which)
4661 of pointer PTROP and integer INTOP. */
4662
4663tree
389dd41b 4664pointer_int_sum (location_t loc, enum tree_code resultcode,
42f9a786 4665 tree ptrop, tree intop, bool complain)
1c26100f 4666{
add6ee5e 4667 tree size_exp, ret;
1c26100f 4668
1c26100f 4669 /* The result is a pointer of the same type that is being added. */
1c26100f 4670 tree result_type = TREE_TYPE (ptrop);
4671
4672 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4673 {
42f9a786 4674 if (complain && warn_pointer_arith)
4675 pedwarn (loc, OPT_Wpointer_arith,
4676 "pointer of type %<void *%> used in arithmetic");
4677 else if (!complain)
4678 return error_mark_node;
1c26100f 4679 size_exp = integer_one_node;
4680 }
4681 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4682 {
42f9a786 4683 if (complain && warn_pointer_arith)
4684 pedwarn (loc, OPT_Wpointer_arith,
4685 "pointer to a function used in arithmetic");
4686 else if (!complain)
4687 return error_mark_node;
1c26100f 4688 size_exp = integer_one_node;
4689 }
1c26100f 4690 else
4691 size_exp = size_in_bytes (TREE_TYPE (result_type));
4692
add6ee5e 4693 /* We are manipulating pointer values, so we don't need to warn
4694 about relying on undefined signed overflow. We disable the
4695 warning here because we use integer types so fold won't know that
4696 they are really pointers. */
4697 fold_defer_overflow_warnings ();
4698
1c26100f 4699 /* If what we are about to multiply by the size of the elements
4700 contains a constant term, apply distributive law
4701 and multiply that constant term separately.
4702 This helps produce common subexpressions. */
1c26100f 4703 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
84166705 4704 && !TREE_CONSTANT (intop)
1c26100f 4705 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4706 && TREE_CONSTANT (size_exp)
4707 /* If the constant comes from pointer subtraction,
4708 skip this optimization--it would cause an error. */
4709 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4710 /* If the constant is unsigned, and smaller than the pointer size,
4711 then we must skip this optimization. This is because it could cause
4712 an overflow error if the constant is negative but INTOP is not. */
84166705 4713 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
1c26100f 4714 || (TYPE_PRECISION (TREE_TYPE (intop))
4715 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4716 {
4717 enum tree_code subcode = resultcode;
4718 tree int_type = TREE_TYPE (intop);
4719 if (TREE_CODE (intop) == MINUS_EXPR)
4720 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4721 /* Convert both subexpression types to the type of intop,
4722 because weird cases involving pointer arithmetic
4723 can result in a sum or difference with different type args. */
8e70fb09 4724 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4725 subcode, ptrop,
1c26100f 4726 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4727 intop = convert (int_type, TREE_OPERAND (intop, 0));
4728 }
4729
4730 /* Convert the integer argument to a type the same size as sizetype
4731 so the multiply won't overflow spuriously. */
1c26100f 4732 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
78a8ed03 4733 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
1cae46be 4734 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
78a8ed03 4735 TYPE_UNSIGNED (sizetype)), intop);
1c26100f 4736
4737 /* Replace the integer argument with a suitable product by the object size.
c6feb9f1 4738 Do this multiplication as signed, then convert to the appropriate type
8032877c 4739 for the pointer operation and disregard an overflow that occurred only
c6feb9f1 4740 because of the sign-extension change in the latter conversion. */
4741 {
4742 tree t = build_binary_op (loc,
4743 MULT_EXPR, intop,
4744 convert (TREE_TYPE (intop), size_exp), 1);
4745 intop = convert (sizetype, t);
4746 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
e913b5cd 4747 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
c6feb9f1 4748 }
0de36bdb 4749
499e523f 4750 /* Create the sum or difference. */
0de36bdb 4751 if (resultcode == MINUS_EXPR)
389dd41b 4752 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
1c26100f 4753
2cc66f2a 4754 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
add6ee5e 4755
4756 fold_undefer_and_ignore_overflow_warnings ();
4757
4758 return ret;
1c26100f 4759}
4760\f
f59e3889 4761/* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4762 and if NON_CONST is known not to be permitted in an evaluated part
4763 of a constant expression. */
4764
4765tree
4766c_wrap_maybe_const (tree expr, bool non_const)
4767{
4768 bool nowarning = TREE_NO_WARNING (expr);
4769 location_t loc = EXPR_LOCATION (expr);
4770
4771 /* This should never be called for C++. */
4772 if (c_dialect_cxx ())
4773 gcc_unreachable ();
4774
4775 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4776 STRIP_TYPE_NOPS (expr);
4777 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4778 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4779 if (nowarning)
4780 TREE_NO_WARNING (expr) = 1;
4781 protected_set_expr_location (expr, loc);
4782
4783 return expr;
4784}
4785
a75b1c71 4786/* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4787 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4788 around the SAVE_EXPR if needed so that c_fully_fold does not need
4789 to look inside SAVE_EXPRs. */
4790
4791tree
4792c_save_expr (tree expr)
4793{
4794 bool maybe_const = true;
4795 if (c_dialect_cxx ())
4796 return save_expr (expr);
4797 expr = c_fully_fold (expr, false, &maybe_const);
4798 expr = save_expr (expr);
4799 if (!maybe_const)
f59e3889 4800 expr = c_wrap_maybe_const (expr, true);
a75b1c71 4801 return expr;
4802}
4803
6b68e71a 4804/* Return whether EXPR is a declaration whose address can never be
4805 NULL. */
4806
4807bool
9f627b1a 4808decl_with_nonnull_addr_p (const_tree expr)
6b68e71a 4809{
4810 return (DECL_P (expr)
4811 && (TREE_CODE (expr) == PARM_DECL
4812 || TREE_CODE (expr) == LABEL_DECL
4813 || !DECL_WEAK (expr)));
4814}
4815
b0fc3e72 4816/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
45a78cc0 4817 or for an `if' or `while' statement or ?..: exp. It should already
4818 have been validated to be of suitable type; otherwise, a bad
4819 diagnostic may result.
b0fc3e72 4820
8e70fb09 4821 The EXPR is located at LOCATION.
4822
b0fc3e72 4823 This preparation consists of taking the ordinary
4824 representation of an expression expr and producing a valid tree
4825 boolean expression describing whether expr is nonzero. We could
3c2239cf 4826 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
b0fc3e72 4827 but we optimize comparisons, &&, ||, and !.
4828
3c2239cf 4829 The resulting type should always be `truthvalue_type_node'. */
b0fc3e72 4830
4831tree
8e70fb09 4832c_common_truthvalue_conversion (location_t location, tree expr)
b0fc3e72 4833{
b0fc3e72 4834 switch (TREE_CODE (expr))
4835 {
318a728f 4836 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
e1232ce2 4837 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4838 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4839 case ORDERED_EXPR: case UNORDERED_EXPR:
ce04dcdc 4840 if (TREE_TYPE (expr) == truthvalue_type_node)
4841 return expr;
e60a6f7b 4842 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
ce04dcdc 4843 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
e60a6f7b 4844 goto ret;
ce04dcdc 4845
b0fc3e72 4846 case TRUTH_ANDIF_EXPR:
4847 case TRUTH_ORIF_EXPR:
4848 case TRUTH_AND_EXPR:
4849 case TRUTH_OR_EXPR:
31f6e93c 4850 case TRUTH_XOR_EXPR:
ce04dcdc 4851 if (TREE_TYPE (expr) == truthvalue_type_node)
4852 return expr;
e60a6f7b 4853 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
48e1416a 4854 c_common_truthvalue_conversion (location,
e60a6f7b 4855 TREE_OPERAND (expr, 0)),
4856 c_common_truthvalue_conversion (location,
4857 TREE_OPERAND (expr, 1)));
4858 goto ret;
3e851b85 4859
f6e28f72 4860 case TRUTH_NOT_EXPR:
ce04dcdc 4861 if (TREE_TYPE (expr) == truthvalue_type_node)
4862 return expr;
e60a6f7b 4863 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4864 c_common_truthvalue_conversion (location,
4865 TREE_OPERAND (expr, 0)));
4866 goto ret;
f6e28f72 4867
b0fc3e72 4868 case ERROR_MARK:
4869 return expr;
ab2c1de8 4870
b0fc3e72 4871 case INTEGER_CST:
eddad94a 4872 return integer_zerop (expr) ? truthvalue_false_node
4873 : truthvalue_true_node;
b0fc3e72 4874
4875 case REAL_CST:
5000e21c 4876 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4877 ? truthvalue_true_node
4878 : truthvalue_false_node;
b0fc3e72 4879
9421ebb9 4880 case FIXED_CST:
4881 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4882 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4883 ? truthvalue_true_node
4884 : truthvalue_false_node;
4885
ce04dcdc 4886 case FUNCTION_DECL:
b6889cb0 4887 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
ce04dcdc 4888 /* Fall through. */
4889
b0fc3e72 4890 case ADDR_EXPR:
ce871053 4891 {
ee7d4d6a 4892 tree inner = TREE_OPERAND (expr, 0);
6b68e71a 4893 if (decl_with_nonnull_addr_p (inner))
ce871053 4894 {
6b68e71a 4895 /* Common Ada/Pascal programmer's mistake. */
8e70fb09 4896 warning_at (location,
4897 OPT_Waddress,
4898 "the address of %qD will always evaluate as %<true%>",
4899 inner);
ce871053 4900 return truthvalue_true_node;
4901 }
d473d901 4902 break;
ce871053 4903 }
b0fc3e72 4904
2203bd5c 4905 case COMPLEX_EXPR:
e60a6f7b 4906 expr = build_binary_op (EXPR_LOCATION (expr),
8e70fb09 4907 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
95809de4 4908 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
8e70fb09 4909 c_common_truthvalue_conversion (location,
4910 TREE_OPERAND (expr, 0)),
4911 c_common_truthvalue_conversion (location,
4912 TREE_OPERAND (expr, 1)),
2203bd5c 4913 0);
e60a6f7b 4914 goto ret;
2203bd5c 4915
b0fc3e72 4916 case NEGATE_EXPR:
4917 case ABS_EXPR:
4918 case FLOAT_EXPR:
c6418a4e 4919 case EXCESS_PRECISION_EXPR:
d10cfa8d 4920 /* These don't change whether an object is nonzero or zero. */
8e70fb09 4921 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
b0fc3e72 4922
4923 case LROTATE_EXPR:
4924 case RROTATE_EXPR:
d10cfa8d 4925 /* These don't change whether an object is zero or nonzero, but
b0fc3e72 4926 we can't ignore them if their second arg has side-effects. */
4927 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
e60a6f7b 4928 {
4929 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4930 TREE_OPERAND (expr, 1),
48e1416a 4931 c_common_truthvalue_conversion
e60a6f7b 4932 (location, TREE_OPERAND (expr, 0)));
4933 goto ret;
4934 }
b0fc3e72 4935 else
8e70fb09 4936 return c_common_truthvalue_conversion (location,
4937 TREE_OPERAND (expr, 0));
73be5127 4938
b0fc3e72 4939 case COND_EXPR:
4940 /* Distribute the conversion into the arms of a COND_EXPR. */
a75b1c71 4941 if (c_dialect_cxx ())
e60a6f7b 4942 {
d0389adc 4943 tree op1 = TREE_OPERAND (expr, 1);
4944 tree op2 = TREE_OPERAND (expr, 2);
4945 /* In C++ one of the arms might have void type if it is throw. */
4946 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4947 op1 = c_common_truthvalue_conversion (location, op1);
4948 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4949 op2 = c_common_truthvalue_conversion (location, op2);
389dd41b 4950 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
d0389adc 4951 TREE_OPERAND (expr, 0), op1, op2);
e60a6f7b 4952 goto ret;
4953 }
a75b1c71 4954 else
e60a6f7b 4955 {
4956 /* Folding will happen later for C. */
4957 expr = build3 (COND_EXPR, truthvalue_type_node,
4958 TREE_OPERAND (expr, 0),
4959 c_common_truthvalue_conversion (location,
4960 TREE_OPERAND (expr, 1)),
4961 c_common_truthvalue_conversion (location,
4962 TREE_OPERAND (expr, 2)));
4963 goto ret;
4964 }
b0fc3e72 4965
72dd6141 4966 CASE_CONVERT:
f8913d47 4967 {
4968 tree totype = TREE_TYPE (expr);
4969 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4970
db3d1ffc 4971 if (POINTER_TYPE_P (totype)
4972 && TREE_CODE (fromtype) == REFERENCE_TYPE)
4973 {
4974 tree inner = expr;
4975 STRIP_NOPS (inner);
4976
4977 if (DECL_P (inner))
4978 warning_at (location,
4979 OPT_Waddress,
4980 "the compiler can assume that the address of "
4981 "%qD will always evaluate to %<true%>",
4982 inner);
4983 }
4984
f8913d47 4985 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4986 since that affects how `default_conversion' will behave. */
4987 if (TREE_CODE (totype) == REFERENCE_TYPE
4988 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4989 break;
4990 /* Don't strip a conversion from C++0x scoped enum, since they
4991 don't implicitly convert to other types. */
4992 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4993 && ENUM_IS_SCOPED (fromtype))
4994 break;
4995 /* If this isn't narrowing the argument, we can ignore it. */
4996 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4997 return c_common_truthvalue_conversion (location,
4998 TREE_OPERAND (expr, 0));
4999 }
b0fc3e72 5000 break;
5001
16837b18 5002 case MODIFY_EXPR:
60a0513e 5003 if (!TREE_NO_WARNING (expr)
5004 && warn_parentheses)
5005 {
5006 warning (OPT_Wparentheses,
5007 "suggest parentheses around assignment used as truth value");
5008 TREE_NO_WARNING (expr) = 1;
5009 }
16837b18 5010 break;
73be5127 5011
31f820d2 5012 default:
5013 break;
b0fc3e72 5014 }
5015
2ba726d2 5016 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
a0748b7d 5017 {
93be21c0 5018 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
e60a6f7b 5019 expr = (build_binary_op
8e70fb09 5020 (EXPR_LOCATION (expr),
5021 (TREE_SIDE_EFFECTS (expr)
a0748b7d 5022 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
b6889cb0 5023 c_common_truthvalue_conversion
5024 (location,
5025 build_unary_op (location, REALPART_EXPR, t, 0)),
5026 c_common_truthvalue_conversion
5027 (location,
5028 build_unary_op (location, IMAGPART_EXPR, t, 0)),
a0748b7d 5029 0));
e60a6f7b 5030 goto ret;
a0748b7d 5031 }
2ba726d2 5032
9421ebb9 5033 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
5034 {
5035 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
5036 FCONST0 (TYPE_MODE
5037 (TREE_TYPE (expr))));
43158006 5038 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
9421ebb9 5039 }
e60a6f7b 5040 else
5041 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
9421ebb9 5042
e60a6f7b 5043 ret:
5044 protected_set_expr_location (expr, location);
5045 return expr;
b0fc3e72 5046}
5047\f
3237155d 5048static void def_builtin_1 (enum built_in_function fncode,
5049 const char *name,
5050 enum built_in_class fnclass,
5051 tree fntype, tree libtype,
5052 bool both_p, bool fallback_p, bool nonansi_p,
5053 tree fnattrs, bool implicit_p);
0d4238dc 5054
a5b1863e 5055
5056/* Apply the TYPE_QUALS to the new DECL. */
5057
5058void
1cae46be 5059c_apply_type_quals_to_decl (int type_quals, tree decl)
a5b1863e 5060{
adfb367f 5061 tree type = TREE_TYPE (decl);
b27ac6b5 5062
e4eabbe4 5063 if (type == error_mark_node)
5064 return;
adfb367f 5065
98a33d9f 5066 if ((type_quals & TYPE_QUAL_CONST)
5067 || (type && TREE_CODE (type) == REFERENCE_TYPE))
5068 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
5069 constructor can produce constant init, so rely on cp_finish_decl to
5070 clear TREE_READONLY if the variable has non-constant init. */
a5b1863e 5071 TREE_READONLY (decl) = 1;
5072 if (type_quals & TYPE_QUAL_VOLATILE)
5073 {
5074 TREE_SIDE_EFFECTS (decl) = 1;
5075 TREE_THIS_VOLATILE (decl) = 1;
5076 }
d91a20bc 5077 if (type_quals & TYPE_QUAL_RESTRICT)
a5b1863e 5078 {
adfb367f 5079 while (type && TREE_CODE (type) == ARRAY_TYPE)
5080 /* Allow 'restrict' on arrays of pointers.
5081 FIXME currently we just ignore it. */
5082 type = TREE_TYPE (type);
5083 if (!type
5084 || !POINTER_TYPE_P (type)
5085 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
b0b1af64 5086 error ("invalid use of %<restrict%>");
a5b1863e 5087 }
5088}
5089
b594087e 5090struct c_type_hasher : ggc_ptr_hash<tree_node>
2ef51f0e 5091{
5092 static hashval_t hash (tree);
5093 static bool equal (tree, tree);
5094};
5095
4ee9c684 5096/* Hash function for the problem of multiple type definitions in
5097 different files. This must hash all types that will compare
5098 equal via comptypes to the same value. In practice it hashes
0bed3869 5099 on some of the simple stuff and leaves the details to comptypes. */
4ee9c684 5100
2ef51f0e 5101hashval_t
5102c_type_hasher::hash (tree t)
4ee9c684 5103{
ecf2703d 5104 int n_elements;
4ee9c684 5105 int shift, size;
4ee9c684 5106 tree t2;
5107 switch (TREE_CODE (t))
5108 {
fbf0afd1 5109 /* For pointers, hash on pointee type plus some swizzling. */
2363ef00 5110 case POINTER_TYPE:
2ef51f0e 5111 return hash (TREE_TYPE (t)) ^ 0x3003003;
2363ef00 5112 /* Hash on number of elements and total size. */
5113 case ENUMERAL_TYPE:
5114 shift = 3;
5115 t2 = TYPE_VALUES (t);
5116 break;
5117 case RECORD_TYPE:
5118 shift = 0;
5119 t2 = TYPE_FIELDS (t);
5120 break;
5121 case QUAL_UNION_TYPE:
5122 shift = 1;
5123 t2 = TYPE_FIELDS (t);
5124 break;
5125 case UNION_TYPE:
5126 shift = 2;
5127 t2 = TYPE_FIELDS (t);
5128 break;
5129 default:
231bd014 5130 gcc_unreachable ();
4ee9c684 5131 }
ecf2703d 5132 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
5133 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
5134 n_elements = list_length (t2);
78c2e180 5135 /* We might have a VLA here. */
5136 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
5137 size = 0;
5138 else
f9ae6f95 5139 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
ecf2703d 5140 return ((size << 24) | (n_elements << shift));
4ee9c684 5141}
5142
2ef51f0e 5143bool
5144c_type_hasher::equal (tree t1, tree t2)
5145{
5146 return lang_hooks.types_compatible_p (t1, t2);
5147}
5148
5149static GTY(()) hash_table<c_type_hasher> *type_hash_table;
1ecd4018 5150
b5ba9f3a 5151/* Return the typed-based alias set for T, which may be an expression
f7c44134 5152 or a type. Return -1 if we don't do anything special. */
ab2c1de8 5153
32c2fdea 5154alias_set_type
1cae46be 5155c_common_get_alias_set (tree t)
b5ba9f3a 5156{
be4f2de7 5157 tree u;
1cae46be 5158
e58c17e7 5159 /* For VLAs, use the alias set of the element type rather than the
5160 default of alias set 0 for types compared structurally. */
5161 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
5162 {
5163 if (TREE_CODE (t) == ARRAY_TYPE)
5164 return get_alias_set (TREE_TYPE (t));
5165 return -1;
5166 }
5167
be4f2de7 5168 /* Permit type-punning when accessing a union, provided the access
5169 is directly through the union. For example, this code does not
5170 permit taking the address of a union member and then storing
5171 through it. Even the type-punning allowed here is a GCC
5172 extension, albeit a common and useful one; the C standard says
5173 that such accesses have implementation-defined behavior. */
5174 for (u = t;
5175 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
5176 u = TREE_OPERAND (u, 0))
5177 if (TREE_CODE (u) == COMPONENT_REF
5178 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
5179 return 0;
1e2513d9 5180
9fcc3e54 5181 /* That's all the expressions we handle specially. */
84166705 5182 if (!TYPE_P (t))
9fcc3e54 5183 return -1;
5184
d716ce75 5185 /* The C standard guarantees that any object may be accessed via an
9fcc3e54 5186 lvalue that has character type. */
5187 if (t == char_type_node
5188 || t == signed_char_type_node
5189 || t == unsigned_char_type_node)
f7c44134 5190 return 0;
a5b1863e 5191
1607663f 5192 /* The C standard specifically allows aliasing between signed and
5193 unsigned variants of the same type. We treat the signed
5194 variant as canonical. */
78a8ed03 5195 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
a8868e19 5196 {
4070745f 5197 tree t1 = c_common_signed_type (t);
1607663f 5198
a8868e19 5199 /* t1 == t can happen for boolean nodes which are always unsigned. */
5200 if (t1 != t)
5201 return get_alias_set (t1);
5202 }
1e2513d9 5203
4ee9c684 5204 /* Handle the case of multiple type nodes referring to "the same" type,
5205 which occurs with IMA. These share an alias set. FIXME: Currently only
5206 C90 is handled. (In C99 type compatibility is not transitive, which
5207 complicates things mightily. The alias set splay trees can theoretically
5208 represent this, but insertion is tricky when you consider all the
5209 different orders things might arrive in.) */
5210
5211 if (c_language != clk_c || flag_isoc99)
5212 return -1;
5213
0bed3869 5214 /* Save time if there's only one input file. */
e08bd2f4 5215 if (num_in_fnames == 1)
4ee9c684 5216 return -1;
5217
5218 /* Pointers need special handling if they point to any type that
5219 needs special handling (below). */
5220 if (TREE_CODE (t) == POINTER_TYPE)
5221 {
5222 tree t2;
5223 /* Find bottom type under any nested POINTERs. */
b27ac6b5 5224 for (t2 = TREE_TYPE (t);
af592f67 5225 TREE_CODE (t2) == POINTER_TYPE;
5226 t2 = TREE_TYPE (t2))
5227 ;
b27ac6b5 5228 if (TREE_CODE (t2) != RECORD_TYPE
af592f67 5229 && TREE_CODE (t2) != ENUMERAL_TYPE
5230 && TREE_CODE (t2) != QUAL_UNION_TYPE
5231 && TREE_CODE (t2) != UNION_TYPE)
5232 return -1;
4ee9c684 5233 if (TYPE_SIZE (t2) == 0)
af592f67 5234 return -1;
4ee9c684 5235 }
5236 /* These are the only cases that need special handling. */
b27ac6b5 5237 if (TREE_CODE (t) != RECORD_TYPE
4ee9c684 5238 && TREE_CODE (t) != ENUMERAL_TYPE
5239 && TREE_CODE (t) != QUAL_UNION_TYPE
5240 && TREE_CODE (t) != UNION_TYPE
5241 && TREE_CODE (t) != POINTER_TYPE)
5242 return -1;
5243 /* Undefined? */
5244 if (TYPE_SIZE (t) == 0)
5245 return -1;
5246
b27ac6b5 5247 /* Look up t in hash table. Only one of the compatible types within each
4ee9c684 5248 alias set is recorded in the table. */
5249 if (!type_hash_table)
2ef51f0e 5250 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
5251 tree *slot = type_hash_table->find_slot (t, INSERT);
4ee9c684 5252 if (*slot != NULL)
ad16cb2c 5253 {
5254 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
5255 return TYPE_ALIAS_SET ((tree)*slot);
5256 }
4ee9c684 5257 else
5258 /* Our caller will assign and record (in t) a new alias set; all we need
5259 to do is remember t in the hash table. */
5260 *slot = t;
5261
f7c44134 5262 return -1;
b5ba9f3a 5263}
902b4e01 5264\f
e60a6f7b 5265/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
a179a7dc 5266 the IS_SIZEOF parameter indicates which operator is being applied.
e60a6f7b 5267 The COMPLAIN flag controls whether we should diagnose possibly
5268 ill-formed constructs or not. LOC is the location of the SIZEOF or
a179a7dc 5269 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
5270 a type in any context should be returned, rather than the normal
5271 alignment for that type. */
908c697e 5272
902b4e01 5273tree
e60a6f7b 5274c_sizeof_or_alignof_type (location_t loc,
a179a7dc 5275 tree type, bool is_sizeof, bool min_alignof,
5276 int complain)
902b4e01 5277{
d4c4d95c 5278 const char *op_name;
5279 tree value = NULL;
5280 enum tree_code type_code = TREE_CODE (type);
1cae46be 5281
908c697e 5282 op_name = is_sizeof ? "sizeof" : "__alignof__";
1cae46be 5283
d4c4d95c 5284 if (type_code == FUNCTION_TYPE)
902b4e01 5285 {
908c697e 5286 if (is_sizeof)
d4c4d95c 5287 {
9205a6cc 5288 if (complain && warn_pointer_arith)
5289 pedwarn (loc, OPT_Wpointer_arith,
8864917d 5290 "invalid application of %<sizeof%> to a function type");
ebd21de4 5291 else if (!complain)
5292 return error_mark_node;
d4c4d95c 5293 value = size_one_node;
5294 }
5295 else
83e25171 5296 {
5297 if (complain)
5298 {
5299 if (c_dialect_cxx ())
29438999 5300 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
83e25171 5301 "%<alignof%> applied to a function type");
5302 else
29438999 5303 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
83e25171 5304 "%<_Alignof%> applied to a function type");
5305 }
5306 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
5307 }
d4c4d95c 5308 }
5309 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
5310 {
1cae46be 5311 if (type_code == VOID_TYPE
9205a6cc 5312 && complain && warn_pointer_arith)
5313 pedwarn (loc, OPT_Wpointer_arith,
8864917d 5314 "invalid application of %qs to a void type", op_name);
ebd21de4 5315 else if (!complain)
5316 return error_mark_node;
d4c4d95c 5317 value = size_one_node;
902b4e01 5318 }
3df19e1b 5319 else if (!COMPLETE_TYPE_P (type)
5320 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
902b4e01 5321 {
d3a4d008 5322 if (complain)
3df19e1b 5323 error_at (loc, "invalid application of %qs to incomplete type %qT",
e60a6f7b 5324 op_name, type);
9c719c74 5325 return error_mark_node;
902b4e01 5326 }
3df19e1b 5327 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
5328 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
5329 {
5330 if (complain)
5331 error_at (loc, "invalid application of %qs to array type %qT of "
5332 "incomplete element type", op_name, type);
5333 return error_mark_node;
5334 }
902b4e01 5335 else
d4c4d95c 5336 {
908c697e 5337 if (is_sizeof)
d4c4d95c 5338 /* Convert in case a char is more than one unit. */
389dd41b 5339 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
5340 size_int (TYPE_PRECISION (char_type_node)
5341 / BITS_PER_UNIT));
a179a7dc 5342 else if (min_alignof)
c2c4ae8d 5343 value = size_int (min_align_of_type (type));
d4c4d95c 5344 else
d37625c0 5345 value = size_int (TYPE_ALIGN_UNIT (type));
d4c4d95c 5346 }
902b4e01 5347
5a1fe2db 5348 /* VALUE will have the middle-end integer type sizetype.
5349 However, we should really return a value of type `size_t',
5350 which is just a typedef for an ordinary integer type. */
389dd41b 5351 value = fold_convert_loc (loc, size_type_node, value);
1cae46be 5352
d4c4d95c 5353 return value;
902b4e01 5354}
5355
5356/* Implement the __alignof keyword: Return the minimum required
097b5c8b 5357 alignment of EXPR, measured in bytes. For VAR_DECLs,
5358 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
e60a6f7b 5359 from an "aligned" __attribute__ specification). LOC is the
5360 location of the ALIGNOF operator. */
72040e7e 5361
902b4e01 5362tree
e60a6f7b 5363c_alignof_expr (location_t loc, tree expr)
902b4e01 5364{
5365 tree t;
5366
097b5c8b 5367 if (VAR_OR_FUNCTION_DECL_P (expr))
d37625c0 5368 t = size_int (DECL_ALIGN_UNIT (expr));
1cae46be 5369
902b4e01 5370 else if (TREE_CODE (expr) == COMPONENT_REF
5371 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5372 {
e60a6f7b 5373 error_at (loc, "%<__alignof%> applied to a bit-field");
902b4e01 5374 t = size_one_node;
5375 }
5376 else if (TREE_CODE (expr) == COMPONENT_REF
7cc7e163 5377 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
d37625c0 5378 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
1cae46be 5379
902b4e01 5380 else if (TREE_CODE (expr) == INDIRECT_REF)
5381 {
5382 tree t = TREE_OPERAND (expr, 0);
5383 tree best = t;
5384 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1cae46be 5385
72dd6141 5386 while (CONVERT_EXPR_P (t)
7cc7e163 5387 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
902b4e01 5388 {
5389 int thisalign;
5390
5391 t = TREE_OPERAND (t, 0);
5392 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5393 if (thisalign > bestalign)
5394 best = t, bestalign = thisalign;
5395 }
e60a6f7b 5396 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
902b4e01 5397 }
5398 else
e60a6f7b 5399 return c_alignof (loc, TREE_TYPE (expr));
902b4e01 5400
389dd41b 5401 return fold_convert_loc (loc, size_type_node, t);
902b4e01 5402}
5403\f
8fe4a266 5404/* Handle C and C++ default attributes. */
5405
5406enum built_in_attribute
5407{
5408#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5409#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
c8010b80 5410#define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
8fe4a266 5411#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5412#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
8fe4a266 5413#include "builtin-attrs.def"
5414#undef DEF_ATTR_NULL_TREE
5415#undef DEF_ATTR_INT
c8010b80 5416#undef DEF_ATTR_STRING
8fe4a266 5417#undef DEF_ATTR_IDENT
5418#undef DEF_ATTR_TREE_LIST
8fe4a266 5419 ATTR_LAST
5420};
5421
5422static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5423
1cae46be 5424static void c_init_attributes (void);
8fe4a266 5425
27213ba3 5426enum c_builtin_type
72040e7e 5427{
d2d4bdde 5428#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5429#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5430#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5431#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5432#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5433#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
0a39fd54 5434#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
3c77ca67 5435#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5436 ARG6) NAME,
5437#define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5438 ARG6, ARG7) NAME,
5439#define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5440 ARG6, ARG7, ARG8) NAME,
d2d4bdde 5441#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5442#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5443#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
03901330 5444#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
0a39fd54 5445#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3c77ca67 5446#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
6349b8cc 5447 NAME,
5448#define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5449 ARG6, ARG7) NAME,
5450#define DEF_FUNCTION_TYPE_VAR_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5451 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
d2d4bdde 5452#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5453#include "builtin-types.def"
5454#undef DEF_PRIMITIVE_TYPE
5455#undef DEF_FUNCTION_TYPE_0
5456#undef DEF_FUNCTION_TYPE_1
5457#undef DEF_FUNCTION_TYPE_2
5458#undef DEF_FUNCTION_TYPE_3
5459#undef DEF_FUNCTION_TYPE_4
0a39fd54 5460#undef DEF_FUNCTION_TYPE_5
5461#undef DEF_FUNCTION_TYPE_6
27213ba3 5462#undef DEF_FUNCTION_TYPE_7
bc7bff74 5463#undef DEF_FUNCTION_TYPE_8
d2d4bdde 5464#undef DEF_FUNCTION_TYPE_VAR_0
5465#undef DEF_FUNCTION_TYPE_VAR_1
5466#undef DEF_FUNCTION_TYPE_VAR_2
03901330 5467#undef DEF_FUNCTION_TYPE_VAR_3
0a39fd54 5468#undef DEF_FUNCTION_TYPE_VAR_4
5469#undef DEF_FUNCTION_TYPE_VAR_5
6349b8cc 5470#undef DEF_FUNCTION_TYPE_VAR_7
5471#undef DEF_FUNCTION_TYPE_VAR_11
d2d4bdde 5472#undef DEF_POINTER_TYPE
27213ba3 5473 BT_LAST
5474};
5475
5476typedef enum c_builtin_type builtin_type;
d2d4bdde 5477
27213ba3 5478/* A temporary array for c_common_nodes_and_builtins. Used in
5479 communication with def_fn_type. */
5480static tree builtin_types[(int) BT_LAST + 1];
d2d4bdde 5481
27213ba3 5482/* A helper function for c_common_nodes_and_builtins. Build function type
5483 for DEF with return type RET and N arguments. If VAR is true, then the
5484 function should be variadic after those N arguments.
5485
5486 Takes special care not to ICE if any of the types involved are
5487 error_mark_node, which indicates that said type is not in fact available
5488 (see builtin_type_for_size). In which case the function type as a whole
5489 should be error_mark_node. */
5490
5491static void
5492def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5493{
3a939d12 5494 tree t;
5495 tree *args = XALLOCAVEC (tree, n);
27213ba3 5496 va_list list;
5497 int i;
5498
5499 va_start (list, n);
5500 for (i = 0; i < n; ++i)
5501 {
7d339f93 5502 builtin_type a = (builtin_type) va_arg (list, int);
27213ba3 5503 t = builtin_types[a];
5504 if (t == error_mark_node)
5505 goto egress;
3a939d12 5506 args[i] = t;
27213ba3 5507 }
27213ba3 5508
27213ba3 5509 t = builtin_types[ret];
5510 if (t == error_mark_node)
5511 goto egress;
3a939d12 5512 if (var)
5513 t = build_varargs_function_type_array (t, n, args);
5514 else
5515 t = build_function_type_array (t, n, args);
27213ba3 5516
5517 egress:
5518 builtin_types[def] = t;
451c8e2f 5519 va_end (list);
27213ba3 5520}
5521
dce22712 5522/* Build builtin functions common to both C and C++ language
5523 frontends. */
5524
5525static void
5526c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5527{
5528#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5529 builtin_types[ENUM] = VALUE;
5530#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5531 def_fn_type (ENUM, RETURN, 0, 0);
5532#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5533 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5534#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5535 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5536#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5537 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5538#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5539 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5540#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5541 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5542#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5543 ARG6) \
5544 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5545#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5546 ARG6, ARG7) \
5547 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
bc7bff74 5548#define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5549 ARG6, ARG7, ARG8) \
5550 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5551 ARG7, ARG8);
dce22712 5552#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5553 def_fn_type (ENUM, RETURN, 1, 0);
5554#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5555 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5556#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5557 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5558#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5559 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5560#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5561 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5562#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5563 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
6349b8cc 5564#define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5565 ARG6, ARG7) \
5566 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
5567#define DEF_FUNCTION_TYPE_VAR_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5568 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5569 def_fn_type (ENUM, RETURN, 1, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5570 ARG7, ARG8, ARG9, ARG10, ARG11);
dce22712 5571#define DEF_POINTER_TYPE(ENUM, TYPE) \
5572 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5573
5574#include "builtin-types.def"
5575
5576#undef DEF_PRIMITIVE_TYPE
3c77ca67 5577#undef DEF_FUNCTION_TYPE_0
dce22712 5578#undef DEF_FUNCTION_TYPE_1
5579#undef DEF_FUNCTION_TYPE_2
5580#undef DEF_FUNCTION_TYPE_3
5581#undef DEF_FUNCTION_TYPE_4
5582#undef DEF_FUNCTION_TYPE_5
5583#undef DEF_FUNCTION_TYPE_6
3c77ca67 5584#undef DEF_FUNCTION_TYPE_7
5585#undef DEF_FUNCTION_TYPE_8
dce22712 5586#undef DEF_FUNCTION_TYPE_VAR_0
5587#undef DEF_FUNCTION_TYPE_VAR_1
5588#undef DEF_FUNCTION_TYPE_VAR_2
5589#undef DEF_FUNCTION_TYPE_VAR_3
5590#undef DEF_FUNCTION_TYPE_VAR_4
5591#undef DEF_FUNCTION_TYPE_VAR_5
6349b8cc 5592#undef DEF_FUNCTION_TYPE_VAR_7
5593#undef DEF_FUNCTION_TYPE_VAR_11
dce22712 5594#undef DEF_POINTER_TYPE
5595 builtin_types[(int) BT_LAST] = NULL_TREE;
5596
5597 c_init_attributes ();
5598
5599#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5600 NONANSI_P, ATTRS, IMPLICIT, COND) \
5601 if (NAME && COND) \
5602 def_builtin_1 (ENUM, NAME, CLASS, \
5603 builtin_types[(int) TYPE], \
5604 builtin_types[(int) LIBTYPE], \
5605 BOTH_P, FALLBACK_P, NONANSI_P, \
5606 built_in_attributes[(int) ATTRS], IMPLICIT);
5607#include "builtins.def"
5608#undef DEF_BUILTIN
5609
87eb1c28 5610 targetm.init_builtins ();
5611
471eff36 5612 build_common_builtin_nodes ();
dce22712 5613
a89e6c15 5614 if (flag_cilkplus)
d037099f 5615 cilk_init_builtins ();
dce22712 5616}
5617
9e6687c8 5618/* Like get_identifier, but avoid warnings about null arguments when
5619 the argument may be NULL for targets where GCC lacks stdint.h type
5620 information. */
5621
5622static inline tree
5623c_get_ident (const char *id)
5624{
5625 return get_identifier (id);
5626}
5627
27213ba3 5628/* Build tree nodes and builtin functions common to both C and C++ language
5629 frontends. */
5630
5631void
5632c_common_nodes_and_builtins (void)
5633{
924bbf02 5634 int char16_type_size;
5635 int char32_type_size;
174fcc61 5636 int wchar_type_size;
5637 tree array_domain_type;
2d47cc32 5638 tree va_list_ref_type_node;
8a15c04a 5639 tree va_list_arg_type_node;
9f75f026 5640 int i;
a66c9326 5641
c38a75b7 5642 build_common_tree_nodes (flag_signed_char, flag_short_double);
e593356b 5643
174fcc61 5644 /* Define `int' and `char' first so that dbx will output them first. */
d946ea19 5645 record_builtin_type (RID_INT, NULL, integer_type_node);
174fcc61 5646 record_builtin_type (RID_CHAR, "char", char_type_node);
5647
5648 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5649 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5650 but not C. Are the conditionals here needed? */
c0f19401 5651 if (c_dialect_cxx ())
d946ea19 5652 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
174fcc61 5653 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5654 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5655 record_builtin_type (RID_MAX, "long unsigned int",
5656 long_unsigned_type_node);
9f75f026 5657
5658 for (i = 0; i < NUM_INT_N_ENTS; i ++)
6388cfe2 5659 {
9f75f026 5660 char name[25];
5661
5662 sprintf (name, "__int%d", int_n_data[i].bitsize);
76738f56 5663 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
9f75f026 5664 int_n_trees[i].signed_type);
5665 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
76738f56 5666 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
6388cfe2 5667 }
9f75f026 5668
c0f19401 5669 if (c_dialect_cxx ())
174fcc61 5670 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5671 record_builtin_type (RID_MAX, "long long int",
5672 long_long_integer_type_node);
5673 record_builtin_type (RID_MAX, "long long unsigned int",
5674 long_long_unsigned_type_node);
c0f19401 5675 if (c_dialect_cxx ())
174fcc61 5676 record_builtin_type (RID_MAX, "long long unsigned",
5677 long_long_unsigned_type_node);
5678 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5679 record_builtin_type (RID_MAX, "short unsigned int",
5680 short_unsigned_type_node);
c0f19401 5681 if (c_dialect_cxx ())
174fcc61 5682 record_builtin_type (RID_MAX, "unsigned short",
5683 short_unsigned_type_node);
5684
5685 /* Define both `signed char' and `unsigned char'. */
5686 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5687 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5688
771d21fa 5689 /* These are types that c_common_type_for_size and
5690 c_common_type_for_mode use. */
e60a6f7b 5691 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5692 TYPE_DECL, NULL_TREE,
dc24ddbd 5693 intQI_type_node));
e60a6f7b 5694 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5695 TYPE_DECL, NULL_TREE,
dc24ddbd 5696 intHI_type_node));
e60a6f7b 5697 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5698 TYPE_DECL, NULL_TREE,
dc24ddbd 5699 intSI_type_node));
e60a6f7b 5700 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5701 TYPE_DECL, NULL_TREE,
dc24ddbd 5702 intDI_type_node));
174fcc61 5703#if HOST_BITS_PER_WIDE_INT >= 64
9f75f026 5704 /* Note that this is different than the __int128 type that's part of
5705 the generic __intN support. */
f1515a39 5706 if (targetm.scalar_mode_supported_p (TImode))
e60a6f7b 5707 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5708 TYPE_DECL,
f1515a39 5709 get_identifier ("__int128_t"),
5710 intTI_type_node));
174fcc61 5711#endif
e60a6f7b 5712 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5713 TYPE_DECL, NULL_TREE,
dc24ddbd 5714 unsigned_intQI_type_node));
e60a6f7b 5715 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5716 TYPE_DECL, NULL_TREE,
dc24ddbd 5717 unsigned_intHI_type_node));
e60a6f7b 5718 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5719 TYPE_DECL, NULL_TREE,
dc24ddbd 5720 unsigned_intSI_type_node));
e60a6f7b 5721 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5722 TYPE_DECL, NULL_TREE,
dc24ddbd 5723 unsigned_intDI_type_node));
174fcc61 5724#if HOST_BITS_PER_WIDE_INT >= 64
f1515a39 5725 if (targetm.scalar_mode_supported_p (TImode))
e60a6f7b 5726 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5727 TYPE_DECL,
f1515a39 5728 get_identifier ("__uint128_t"),
5729 unsigned_intTI_type_node));
174fcc61 5730#endif
5731
5732 /* Create the widest literal types. */
5733 widest_integer_literal_type_node
5734 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
e60a6f7b 5735 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5736 TYPE_DECL, NULL_TREE,
dc24ddbd 5737 widest_integer_literal_type_node));
174fcc61 5738
5739 widest_unsigned_literal_type_node
5740 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
e60a6f7b 5741 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5742 TYPE_DECL, NULL_TREE,
dc24ddbd 5743 widest_unsigned_literal_type_node));
174fcc61 5744
654ef926 5745 signed_size_type_node = c_common_signed_type (size_type_node);
174fcc61 5746
73673831 5747 pid_type_node =
5748 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5749
d946ea19 5750 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5751 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
174fcc61 5752 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5753
c4503c0a 5754 /* Only supported decimal floating point extension if the target
5755 actually supports underlying modes. */
48e1416a 5756 if (targetm.scalar_mode_supported_p (SDmode)
c4503c0a 5757 && targetm.scalar_mode_supported_p (DDmode)
5758 && targetm.scalar_mode_supported_p (TDmode))
5759 {
5760 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5761 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5762 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5763 }
5764
9421ebb9 5765 if (targetm.fixed_point_supported_p ())
5766 {
5767 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5768 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5769 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5770 record_builtin_type (RID_MAX, "long long _Fract",
5771 long_long_fract_type_node);
5772 record_builtin_type (RID_MAX, "unsigned short _Fract",
5773 unsigned_short_fract_type_node);
5774 record_builtin_type (RID_MAX, "unsigned _Fract",
5775 unsigned_fract_type_node);
5776 record_builtin_type (RID_MAX, "unsigned long _Fract",
5777 unsigned_long_fract_type_node);
5778 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5779 unsigned_long_long_fract_type_node);
5780 record_builtin_type (RID_MAX, "_Sat short _Fract",
5781 sat_short_fract_type_node);
5782 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5783 record_builtin_type (RID_MAX, "_Sat long _Fract",
5784 sat_long_fract_type_node);
5785 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5786 sat_long_long_fract_type_node);
5787 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5788 sat_unsigned_short_fract_type_node);
5789 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5790 sat_unsigned_fract_type_node);
5791 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5792 sat_unsigned_long_fract_type_node);
5793 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5794 sat_unsigned_long_long_fract_type_node);
5795 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5796 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5797 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5798 record_builtin_type (RID_MAX, "long long _Accum",
5799 long_long_accum_type_node);
5800 record_builtin_type (RID_MAX, "unsigned short _Accum",
5801 unsigned_short_accum_type_node);
5802 record_builtin_type (RID_MAX, "unsigned _Accum",
5803 unsigned_accum_type_node);
5804 record_builtin_type (RID_MAX, "unsigned long _Accum",
5805 unsigned_long_accum_type_node);
5806 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5807 unsigned_long_long_accum_type_node);
5808 record_builtin_type (RID_MAX, "_Sat short _Accum",
5809 sat_short_accum_type_node);
5810 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5811 record_builtin_type (RID_MAX, "_Sat long _Accum",
5812 sat_long_accum_type_node);
5813 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5814 sat_long_long_accum_type_node);
5815 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5816 sat_unsigned_short_accum_type_node);
5817 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5818 sat_unsigned_accum_type_node);
5819 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5820 sat_unsigned_long_accum_type_node);
5821 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5822 sat_unsigned_long_long_accum_type_node);
5823
5824 }
5825
e60a6f7b 5826 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5827 TYPE_DECL,
dc24ddbd 5828 get_identifier ("complex int"),
5829 complex_integer_type_node));
e60a6f7b 5830 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5831 TYPE_DECL,
dc24ddbd 5832 get_identifier ("complex float"),
5833 complex_float_type_node));
e60a6f7b 5834 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5835 TYPE_DECL,
dc24ddbd 5836 get_identifier ("complex double"),
5837 complex_double_type_node));
5838 lang_hooks.decls.pushdecl
e60a6f7b 5839 (build_decl (UNKNOWN_LOCATION,
5840 TYPE_DECL, get_identifier ("complex long double"),
20325f61 5841 complex_long_double_type_node));
174fcc61 5842
e256d445 5843 if (c_dialect_cxx ())
5844 /* For C++, make fileptr_type_node a distinct void * type until
5845 FILE type is defined. */
e086912e 5846 fileptr_type_node = build_variant_type_copy (ptr_type_node);
e256d445 5847
d946ea19 5848 record_builtin_type (RID_VOID, NULL, void_type_node);
174fcc61 5849
6753bca0 5850 /* Set the TYPE_NAME for any variants that were built before
5851 record_builtin_type gave names to the built-in types. */
5852 {
5853 tree void_name = TYPE_NAME (void_type_node);
5854 TYPE_NAME (void_type_node) = NULL_TREE;
5855 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5856 = void_name;
5857 TYPE_NAME (void_type_node) = void_name;
5858 }
5859
174fcc61 5860 void_list_node = build_void_list_node ();
5861
5862 /* Make a type to be the domain of a few array types
5863 whose domains don't really matter.
5864 200 is small enough that it always fits in size_t
5865 and large enough that it can hold most function names for the
5866 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5867 array_domain_type = build_index_type (size_int (200));
5868
5869 /* Make a type for arrays of characters.
5870 With luck nothing will ever really depend on the length of this
5871 array type. */
5872 char_array_type_node
5873 = build_array_type (char_type_node, array_domain_type);
5874
d2d4bdde 5875 string_type_node = build_pointer_type (char_type_node);
5876 const_string_type_node
5877 = build_pointer_type (build_qualified_type
5878 (char_type_node, TYPE_QUAL_CONST));
5879
174fcc61 5880 /* This is special for C++ so functions can be overloaded. */
18ef7ac2 5881 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
174fcc61 5882 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5883 wchar_type_size = TYPE_PRECISION (wchar_type_node);
f3449a3c 5884 underlying_wchar_type_node = wchar_type_node;
c0f19401 5885 if (c_dialect_cxx ())
174fcc61 5886 {
78a8ed03 5887 if (TYPE_UNSIGNED (wchar_type_node))
174fcc61 5888 wchar_type_node = make_unsigned_type (wchar_type_size);
5889 else
5890 wchar_type_node = make_signed_type (wchar_type_size);
5891 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5892 }
174fcc61 5893
5894 /* This is for wide string constants. */
5895 wchar_array_type_node
5896 = build_array_type (wchar_type_node, array_domain_type);
5897
924bbf02 5898 /* Define 'char16_t'. */
5899 char16_type_node = get_identifier (CHAR16_TYPE);
5900 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5901 char16_type_size = TYPE_PRECISION (char16_type_node);
5902 if (c_dialect_cxx ())
5903 {
5904 char16_type_node = make_unsigned_type (char16_type_size);
5905
60777f69 5906 if (cxx_dialect >= cxx11)
924bbf02 5907 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5908 }
5909
5910 /* This is for UTF-16 string constants. */
5911 char16_array_type_node
5912 = build_array_type (char16_type_node, array_domain_type);
5913
5914 /* Define 'char32_t'. */
5915 char32_type_node = get_identifier (CHAR32_TYPE);
5916 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5917 char32_type_size = TYPE_PRECISION (char32_type_node);
5918 if (c_dialect_cxx ())
5919 {
5920 char32_type_node = make_unsigned_type (char32_type_size);
5921
60777f69 5922 if (cxx_dialect >= cxx11)
924bbf02 5923 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5924 }
5925
5926 /* This is for UTF-32 string constants. */
5927 char32_array_type_node
5928 = build_array_type (char32_type_node, array_domain_type);
5929
6bf5ed8d 5930 wint_type_node =
5931 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5932
5933 intmax_type_node =
5934 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5935 uintmax_type_node =
5936 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5937
f3449a3c 5938 if (SIG_ATOMIC_TYPE)
5939 sig_atomic_type_node =
9e6687c8 5940 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
f3449a3c 5941 if (INT8_TYPE)
5942 int8_type_node =
9e6687c8 5943 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
f3449a3c 5944 if (INT16_TYPE)
5945 int16_type_node =
9e6687c8 5946 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
f3449a3c 5947 if (INT32_TYPE)
5948 int32_type_node =
9e6687c8 5949 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
f3449a3c 5950 if (INT64_TYPE)
5951 int64_type_node =
9e6687c8 5952 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
f3449a3c 5953 if (UINT8_TYPE)
5954 uint8_type_node =
9e6687c8 5955 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
f3449a3c 5956 if (UINT16_TYPE)
d1081017 5957 c_uint16_type_node = uint16_type_node =
9e6687c8 5958 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
f3449a3c 5959 if (UINT32_TYPE)
d1081017 5960 c_uint32_type_node = uint32_type_node =
9e6687c8 5961 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
f3449a3c 5962 if (UINT64_TYPE)
d1081017 5963 c_uint64_type_node = uint64_type_node =
9e6687c8 5964 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
f3449a3c 5965 if (INT_LEAST8_TYPE)
5966 int_least8_type_node =
9e6687c8 5967 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
f3449a3c 5968 if (INT_LEAST16_TYPE)
5969 int_least16_type_node =
9e6687c8 5970 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
f3449a3c 5971 if (INT_LEAST32_TYPE)
5972 int_least32_type_node =
9e6687c8 5973 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
f3449a3c 5974 if (INT_LEAST64_TYPE)
5975 int_least64_type_node =
9e6687c8 5976 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
f3449a3c 5977 if (UINT_LEAST8_TYPE)
5978 uint_least8_type_node =
9e6687c8 5979 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
f3449a3c 5980 if (UINT_LEAST16_TYPE)
5981 uint_least16_type_node =
9e6687c8 5982 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
f3449a3c 5983 if (UINT_LEAST32_TYPE)
5984 uint_least32_type_node =
9e6687c8 5985 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
f3449a3c 5986 if (UINT_LEAST64_TYPE)
5987 uint_least64_type_node =
9e6687c8 5988 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
f3449a3c 5989 if (INT_FAST8_TYPE)
5990 int_fast8_type_node =
9e6687c8 5991 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
f3449a3c 5992 if (INT_FAST16_TYPE)
5993 int_fast16_type_node =
9e6687c8 5994 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
f3449a3c 5995 if (INT_FAST32_TYPE)
5996 int_fast32_type_node =
9e6687c8 5997 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
f3449a3c 5998 if (INT_FAST64_TYPE)
5999 int_fast64_type_node =
9e6687c8 6000 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
f3449a3c 6001 if (UINT_FAST8_TYPE)
6002 uint_fast8_type_node =
9e6687c8 6003 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
f3449a3c 6004 if (UINT_FAST16_TYPE)
6005 uint_fast16_type_node =
9e6687c8 6006 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
f3449a3c 6007 if (UINT_FAST32_TYPE)
6008 uint_fast32_type_node =
9e6687c8 6009 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
f3449a3c 6010 if (UINT_FAST64_TYPE)
6011 uint_fast64_type_node =
9e6687c8 6012 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
f3449a3c 6013 if (INTPTR_TYPE)
6014 intptr_type_node =
9e6687c8 6015 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
f3449a3c 6016 if (UINTPTR_TYPE)
6017 uintptr_type_node =
9e6687c8 6018 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
f3449a3c 6019
3a939d12 6020 default_function_type
6021 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
6bf5ed8d 6022 ptrdiff_type_node
6023 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
11773141 6024 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
6bf5ed8d 6025
dc24ddbd 6026 lang_hooks.decls.pushdecl
e60a6f7b 6027 (build_decl (UNKNOWN_LOCATION,
6028 TYPE_DECL, get_identifier ("__builtin_va_list"),
20325f61 6029 va_list_type_node));
202d6e5f 6030 if (targetm.enum_va_list_p)
acd6f472 6031 {
6032 int l;
6033 const char *pname;
6034 tree ptype;
5f57a8b1 6035
202d6e5f 6036 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
acd6f472 6037 {
6038 lang_hooks.decls.pushdecl
6039 (build_decl (UNKNOWN_LOCATION,
6040 TYPE_DECL, get_identifier (pname),
6041 ptype));
ab2c1de8 6042
acd6f472 6043 }
6044 }
8a15c04a 6045
8a15c04a 6046 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
2d47cc32 6047 {
6048 va_list_arg_type_node = va_list_ref_type_node =
6049 build_pointer_type (TREE_TYPE (va_list_type_node));
6050 }
8a15c04a 6051 else
2d47cc32 6052 {
6053 va_list_arg_type_node = va_list_type_node;
6054 va_list_ref_type_node = build_reference_type (va_list_type_node);
6055 }
1cae46be 6056
dce22712 6057 if (!flag_preprocess_only)
6058 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
ffa8918b 6059
5c62f199 6060 main_identifier_node = get_identifier ("main");
ae84079f 6061
6062 /* Create the built-in __null node. It is important that this is
6063 not shared. */
271e739a 6064 null_node = make_int_cst (1, 1);
ae84079f 6065 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
27213ba3 6066
6067 /* Since builtin_types isn't gc'ed, don't export these nodes. */
6068 memset (builtin_types, 0, sizeof (builtin_types));
72040e7e 6069}
a66c9326 6070
79b01846 6071/* The number of named compound-literals generated thus far. */
6072static GTY(()) int compound_literal_number;
6073
6074/* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
6075
6076void
6077set_compound_literal_name (tree decl)
6078{
6079 char *name;
6080 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
6081 compound_literal_number);
6082 compound_literal_number++;
6083 DECL_NAME (decl) = get_identifier (name);
6084}
6085
2e474820 6086/* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
6087 TYPE and operand OP. */
6088
6089static tree
6090build_va_arg_1 (location_t loc, tree type, tree op)
6091{
6092 tree expr = build1 (VA_ARG_EXPR, type, op);
6093 SET_EXPR_LOCATION (expr, loc);
6094 return expr;
6095}
6096
6097/* Return a VA_ARG_EXPR corresponding to a source-level expression
6098 va_arg (EXPR, TYPE) at source location LOC. */
6099
a66c9326 6100tree
e60a6f7b 6101build_va_arg (location_t loc, tree expr, tree type)
a66c9326 6102{
c37be9ec 6103 tree va_type = TREE_TYPE (expr);
6104 tree canon_va_type = (va_type == error_mark_node
6105 ? NULL_TREE
6106 : targetm.canonical_va_list_type (va_type));
6107
2e474820 6108 if (va_type == error_mark_node
6109 || canon_va_type == NULL_TREE)
c37be9ec 6110 {
2e474820 6111 /* Let's handle things neutrallly, if expr:
6112 - has undeclared type, or
6113 - is not an va_list type. */
6114 return build_va_arg_1 (loc, type, expr);
c37be9ec 6115 }
f7fec1c7 6116
2e474820 6117 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
6118 {
6119 /* Case 1: Not an array type. */
6120
6121 /* Take the address, to get '&ap'. */
6122 mark_addressable (expr);
6123 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
6124
6125 /* Verify that &ap is still recognized as having va_list type. */
6126 tree canon_expr_type
6127 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6128 gcc_assert (canon_expr_type != NULL_TREE);
6129
6130 return build_va_arg_1 (loc, type, expr);
6131 }
6132
6133 /* Case 2: Array type.
6134
6135 Background:
6136
6137 For contrast, let's start with the simple case (case 1). If
6138 canon_va_type is not an array type, but say a char *, then when
6139 passing-by-value a va_list, the type of the va_list param decl is
6140 the same as for another va_list decl (all ap's are char *):
6141
6142 f2_1 (char * ap)
6143 D.1815 = VA_ARG (&ap, 0B, 1);
6144 return D.1815;
6145
6146 f2 (int i)
6147 char * ap.0;
6148 char * ap;
6149 __builtin_va_start (&ap, 0);
6150 ap.0 = ap;
6151 res = f2_1 (ap.0);
6152 __builtin_va_end (&ap);
6153 D.1812 = res;
6154 return D.1812;
6155
6156 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
6157 va_list the type of the va_list param decl (case 2b, struct * ap) is not
6158 the same as for another va_list decl (case 2a, struct ap[1]).
6159
6160 f2_1 (struct * ap)
6161 D.1844 = VA_ARG (ap, 0B, 0);
6162 return D.1844;
6163
6164 f2 (int i)
6165 struct ap[1];
6166 __builtin_va_start (&ap, 0);
6167 res = f2_1 (&ap);
6168 __builtin_va_end (&ap);
6169 D.1841 = res;
6170 return D.1841;
6171
6172 Case 2b is different because:
6173 - on the callee side, the parm decl has declared type va_list, but
6174 grokdeclarator changes the type of the parm decl to a pointer to the
6175 array elem type.
6176 - on the caller side, the pass-by-value uses &ap.
6177
6178 We unify these two cases (case 2a: va_list is array type,
6179 case 2b: va_list is pointer to array elem type), by adding '&' for the
6180 array type case, such that we have a pointer to array elem in both
6181 cases. */
6182
6183 if (TREE_CODE (va_type) == ARRAY_TYPE)
6184 {
6185 /* Case 2a: va_list is array type. */
6186
6187 /* Take the address, to get '&ap'. Make sure it's a pointer to array
6188 elem type. */
6189 mark_addressable (expr);
6190 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
6191 expr);
6192
6193 /* Verify that &ap is still recognized as having va_list type. */
6194 tree canon_expr_type
6195 = targetm.canonical_va_list_type (TREE_TYPE (expr));
6196 gcc_assert (canon_expr_type != NULL_TREE);
6197 }
6198 else
6199 {
6200 /* Case 2b: va_list is pointer to array elem type. */
6201 gcc_assert (POINTER_TYPE_P (va_type));
6202 gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type));
6203
6204 /* Don't take the address. We've already got '&ap'. */
6205 ;
6206 }
6207
6208 return build_va_arg_1 (loc, type, expr);
a66c9326 6209}
0d4238dc 6210
6211
dd878098 6212/* Linked list of disabled built-in functions. */
6213
6214typedef struct disabled_builtin
6215{
6216 const char *name;
6217 struct disabled_builtin *next;
6218} disabled_builtin;
6219static disabled_builtin *disabled_builtins = NULL;
6220
1cae46be 6221static bool builtin_function_disabled_p (const char *);
dd878098 6222
6223/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
6224 begins with "__builtin_", give an error. */
6225
6226void
1cae46be 6227disable_builtin_function (const char *name)
dd878098 6228{
6229 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
b0b1af64 6230 error ("cannot disable built-in function %qs", name);
dd878098 6231 else
6232 {
e85905e5 6233 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
6234 new_disabled_builtin->name = name;
6235 new_disabled_builtin->next = disabled_builtins;
6236 disabled_builtins = new_disabled_builtin;
dd878098 6237 }
6238}
6239
6240
6241/* Return true if the built-in function NAME has been disabled, false
6242 otherwise. */
6243
6244static bool
1cae46be 6245builtin_function_disabled_p (const char *name)
dd878098 6246{
6247 disabled_builtin *p;
6248 for (p = disabled_builtins; p != NULL; p = p->next)
6249 {
6250 if (strcmp (name, p->name) == 0)
6251 return true;
6252 }
6253 return false;
6254}
6255
6256
3237155d 6257/* Worker for DEF_BUILTIN.
6258 Possibly define a builtin function with one or two names.
6259 Does not declare a non-__builtin_ function if flag_no_builtin, or if
6260 nonansi_p and flag_no_nonansi_builtin. */
0d4238dc 6261
3237155d 6262static void
6263def_builtin_1 (enum built_in_function fncode,
6264 const char *name,
6265 enum built_in_class fnclass,
6266 tree fntype, tree libtype,
6267 bool both_p, bool fallback_p, bool nonansi_p,
6268 tree fnattrs, bool implicit_p)
0d4238dc 6269{
3237155d 6270 tree decl;
6271 const char *libname;
6272
27213ba3 6273 if (fntype == error_mark_node)
6274 return;
6275
3237155d 6276 gcc_assert ((!both_p && !fallback_p)
6277 || !strncmp (name, "__builtin_",
6278 strlen ("__builtin_")));
6279
6280 libname = name + strlen ("__builtin_");
54be5d7e 6281 decl = add_builtin_function (name, fntype, fncode, fnclass,
6282 (fallback_p ? libname : NULL),
6283 fnattrs);
b9a16870 6284
6285 set_builtin_decl (fncode, decl, implicit_p);
6286
3237155d 6287 if (both_p
6288 && !flag_no_builtin && !builtin_function_disabled_p (libname)
dd878098 6289 && !(nonansi_p && flag_no_nonansi_builtin))
54be5d7e 6290 add_builtin_function (libname, libtype, fncode, fnclass,
6291 NULL, fnattrs);
0d4238dc 6292}
e94026da 6293\f
d7aeef06 6294/* Nonzero if the type T promotes to int. This is (nearly) the
6295 integral promotions defined in ISO C99 6.3.1.1/2. */
6296
6297bool
9f627b1a 6298c_promoting_integer_type_p (const_tree t)
d7aeef06 6299{
6300 switch (TREE_CODE (t))
6301 {
6302 case INTEGER_TYPE:
6303 return (TYPE_MAIN_VARIANT (t) == char_type_node
6304 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
6305 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
6306 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
7aa1e6eb 6307 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
6308 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d7aeef06 6309
6310 case ENUMERAL_TYPE:
6311 /* ??? Technically all enumerations not larger than an int
6312 promote to an int. But this is used along code paths
6313 that only want to notice a size change. */
6314 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
6315
6316 case BOOLEAN_TYPE:
6317 return 1;
6318
6319 default:
6320 return 0;
6321 }
6322}
6323
e94026da 6324/* Return 1 if PARMS specifies a fixed number of parameters
6325 and none of their types is affected by default promotions. */
6326
6327int
9f627b1a 6328self_promoting_args_p (const_tree parms)
e94026da 6329{
9f627b1a 6330 const_tree t;
e94026da 6331 for (t = parms; t; t = TREE_CHAIN (t))
6332 {
19cb6b50 6333 tree type = TREE_VALUE (t);
43f74bc4 6334
e1d8e198 6335 if (type == error_mark_node)
6336 continue;
6337
e94026da 6338 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6339 return 0;
6340
6341 if (type == 0)
6342 return 0;
6343
6344 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6345 return 0;
6346
d7aeef06 6347 if (c_promoting_integer_type_p (type))
e94026da 6348 return 0;
6349 }
6350 return 1;
6351}
605fb01e 6352
c10de5e7 6353/* Recursively remove any '*' or '&' operator from TYPE. */
6354tree
6355strip_pointer_operator (tree t)
6356{
6357 while (POINTER_TYPE_P (t))
6358 t = TREE_TYPE (t);
6359 return t;
6360}
6361
57a0ed23 6362/* Recursively remove pointer or array type from TYPE. */
6363tree
6364strip_pointer_or_array_types (tree t)
6365{
6366 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6367 t = TREE_TYPE (t);
6368 return t;
6369}
6370
e41f0d80 6371/* Used to compare case labels. K1 and K2 are actually tree nodes
6372 representing case labels, or NULL_TREE for a `default' label.
6373 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6374 K2, and 0 if K1 and K2 are equal. */
6375
6376int
1cae46be 6377case_compare (splay_tree_key k1, splay_tree_key k2)
e41f0d80 6378{
6379 /* Consider a NULL key (such as arises with a `default' label) to be
6380 smaller than anything else. */
6381 if (!k1)
6382 return k2 ? -1 : 0;
6383 else if (!k2)
6384 return k1 ? 1 : 0;
6385
6386 return tree_int_cst_compare ((tree) k1, (tree) k2);
6387}
6388
e60a6f7b 6389/* Process a case label, located at LOC, for the range LOW_VALUE
6390 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6391 then this case label is actually a `default' label. If only
6392 HIGH_VALUE is NULL_TREE, then case label was declared using the
6393 usual C/C++ syntax, rather than the GNU case range extension.
6394 CASES is a tree containing all the case ranges processed so far;
6395 COND is the condition for the switch-statement itself. Returns the
6396 CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR
6397 is created. */
e41f0d80 6398
6399tree
e60a6f7b 6400c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
2ca392fd 6401 tree low_value, tree high_value)
e41f0d80 6402{
6403 tree type;
6404 tree label;
6405 tree case_label;
6406 splay_tree_node node;
6407
6408 /* Create the LABEL_DECL itself. */
e60a6f7b 6409 label = create_artificial_label (loc);
e41f0d80 6410
6411 /* If there was an error processing the switch condition, bail now
6412 before we get more confused. */
6413 if (!cond || cond == error_mark_node)
4ee9c684 6414 goto error_out;
e41f0d80 6415
1cae46be 6416 if ((low_value && TREE_TYPE (low_value)
6417 && POINTER_TYPE_P (TREE_TYPE (low_value)))
e41f0d80 6418 || (high_value && TREE_TYPE (high_value)
6419 && POINTER_TYPE_P (TREE_TYPE (high_value))))
b96dc121 6420 {
e60a6f7b 6421 error_at (loc, "pointers are not permitted as case values");
b96dc121 6422 goto error_out;
6423 }
e41f0d80 6424
6425 /* Case ranges are a GNU extension. */
8864917d 6426 if (high_value)
29438999 6427 pedwarn (loc, OPT_Wpedantic,
8864917d 6428 "range expressions in switch statements are non-standard");
e41f0d80 6429
6430 type = TREE_TYPE (cond);
6431 if (low_value)
6432 {
2d2f6a15 6433 low_value = check_case_value (loc, low_value);
22a75734 6434 low_value = convert_and_check (loc, type, low_value);
96722196 6435 if (low_value == error_mark_node)
6436 goto error_out;
e41f0d80 6437 }
6438 if (high_value)
6439 {
2d2f6a15 6440 high_value = check_case_value (loc, high_value);
22a75734 6441 high_value = convert_and_check (loc, type, high_value);
96722196 6442 if (high_value == error_mark_node)
6443 goto error_out;
e41f0d80 6444 }
6445
96722196 6446 if (low_value && high_value)
6447 {
6448 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
a0c938f0 6449 really a case range, even though it was written that way.
6450 Remove the HIGH_VALUE to simplify later processing. */
96722196 6451 if (tree_int_cst_equal (low_value, high_value))
6452 high_value = NULL_TREE;
6453 else if (!tree_int_cst_lt (low_value, high_value))
e60a6f7b 6454 warning_at (loc, 0, "empty range specified");
96722196 6455 }
e41f0d80 6456
2ca392fd 6457 /* See if the case is in range of the type of the original testing
6458 expression. If both low_value and high_value are out of range,
6459 don't insert the case label and return NULL_TREE. */
6460 if (low_value
f61a9bc2 6461 && !check_case_bounds (loc, type, orig_type,
84166705 6462 &low_value, high_value ? &high_value : NULL))
2ca392fd 6463 return NULL_TREE;
6464
e41f0d80 6465 /* Look up the LOW_VALUE in the table of case labels we already
6466 have. */
6467 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6468 /* If there was not an exact match, check for overlapping ranges.
6469 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6470 that's a `default' label and the only overlap is an exact match. */
6471 if (!node && (low_value || high_value))
6472 {
6473 splay_tree_node low_bound;
6474 splay_tree_node high_bound;
6475
6476 /* Even though there wasn't an exact match, there might be an
6477 overlap between this case range and another case range.
6478 Since we've (inductively) not allowed any overlapping case
6479 ranges, we simply need to find the greatest low case label
6480 that is smaller that LOW_VALUE, and the smallest low case
6481 label that is greater than LOW_VALUE. If there is an overlap
6482 it will occur in one of these two ranges. */
6483 low_bound = splay_tree_predecessor (cases,
6484 (splay_tree_key) low_value);
6485 high_bound = splay_tree_successor (cases,
6486 (splay_tree_key) low_value);
6487
6488 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6489 the LOW_VALUE, so there is no need to check unless the
6490 LOW_BOUND is in fact itself a case range. */
6491 if (low_bound
6492 && CASE_HIGH ((tree) low_bound->value)
6493 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6494 low_value) >= 0)
6495 node = low_bound;
6496 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6497 range is bigger than the low end of the current range, so we
6498 are only interested if the current range is a real range, and
6499 not an ordinary case label. */
1cae46be 6500 else if (high_bound
e41f0d80 6501 && high_value
6502 && (tree_int_cst_compare ((tree) high_bound->key,
6503 high_value)
6504 <= 0))
6505 node = high_bound;
6506 }
6507 /* If there was an overlap, issue an error. */
6508 if (node)
6509 {
eaae3b75 6510 tree duplicate = CASE_LABEL ((tree) node->value);
e41f0d80 6511
6512 if (high_value)
6513 {
e60a6f7b 6514 error_at (loc, "duplicate (or overlapping) case value");
6515 error_at (DECL_SOURCE_LOCATION (duplicate),
6516 "this is the first entry overlapping that value");
e41f0d80 6517 }
6518 else if (low_value)
6519 {
e60a6f7b 6520 error_at (loc, "duplicate case value") ;
6521 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
e41f0d80 6522 }
6523 else
6524 {
e60a6f7b 6525 error_at (loc, "multiple default labels in one switch");
6526 error_at (DECL_SOURCE_LOCATION (duplicate),
6527 "this is the first default label");
e41f0d80 6528 }
4ee9c684 6529 goto error_out;
e41f0d80 6530 }
6531
6532 /* Add a CASE_LABEL to the statement-tree. */
b6e3dd65 6533 case_label = add_stmt (build_case_label (low_value, high_value, label));
e41f0d80 6534 /* Register this case label in the splay tree. */
1cae46be 6535 splay_tree_insert (cases,
e41f0d80 6536 (splay_tree_key) low_value,
6537 (splay_tree_value) case_label);
6538
6539 return case_label;
4ee9c684 6540
6541 error_out:
daf6dff5 6542 /* Add a label so that the back-end doesn't think that the beginning of
4ee9c684 6543 the switch is unreachable. Note that we do not add a case label, as
a53ff4c1 6544 that just leads to duplicates and thence to failure later on. */
4ee9c684 6545 if (!cases->root)
6546 {
e60a6f7b 6547 tree t = create_artificial_label (loc);
6548 add_stmt (build_stmt (loc, LABEL_EXPR, t));
4ee9c684 6549 }
6550 return error_mark_node;
6551}
6552
6553/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6554 Used to verify that case values match up with enumerator values. */
6555
6556static void
6557match_case_to_enum_1 (tree key, tree type, tree label)
6558{
e913b5cd 6559 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6560
dd76621f 6561 if (tree_fits_uhwi_p (key))
6562 print_dec (key, buf, UNSIGNED);
6563 else if (tree_fits_shwi_p (key))
6564 print_dec (key, buf, SIGNED);
4ee9c684 6565 else
e913b5cd 6566 print_hex (key, buf);
4ee9c684 6567
6568 if (TYPE_NAME (type) == 0)
712d2297 6569 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6570 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6571 "case value %qs not in enumerated type",
6572 buf);
4ee9c684 6573 else
712d2297 6574 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6575 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6576 "case value %qs not in enumerated type %qT",
6577 buf, type);
4ee9c684 6578}
6579
359d87c6 6580/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6581 Used to verify that case values match up with enumerator values. */
6582
4ee9c684 6583static int
6584match_case_to_enum (splay_tree_node node, void *data)
6585{
6586 tree label = (tree) node->value;
4fd61bc6 6587 tree type = (tree) data;
4ee9c684 6588
6589 /* Skip default case. */
6590 if (!CASE_LOW (label))
6591 return 0;
6592
359d87c6 6593 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
4ee9c684 6594 when we did our enum->case scan. Reset our scratch bit after. */
359d87c6 6595 if (!CASE_LOW_SEEN (label))
4ee9c684 6596 match_case_to_enum_1 (CASE_LOW (label), type, label);
6597 else
359d87c6 6598 CASE_LOW_SEEN (label) = 0;
4ee9c684 6599
359d87c6 6600 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6601 not set, that means that CASE_HIGH did not appear when we did our
6602 enum->case scan. Reset our scratch bit after. */
4ee9c684 6603 if (CASE_HIGH (label))
6604 {
359d87c6 6605 if (!CASE_HIGH_SEEN (label))
6606 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6607 else
6608 CASE_HIGH_SEEN (label) = 0;
4ee9c684 6609 }
6610
6611 return 0;
6612}
6613
e7911019 6614/* Handle -Wswitch*. Called from the front end after parsing the
6615 switch construct. */
6616/* ??? Should probably be somewhere generic, since other languages
6617 besides C and C++ would want this. At the moment, however, C/C++
6618 are the only tree-ssa languages that support enumerations at all,
6619 so the point is moot. */
4ee9c684 6620
e7911019 6621void
6622c_do_switch_warnings (splay_tree cases, location_t switch_location,
6623 tree type, tree cond)
4ee9c684 6624{
b27ac6b5 6625 splay_tree_node default_node;
359d87c6 6626 splay_tree_node node;
6627 tree chain;
4ee9c684 6628
6629 if (!warn_switch && !warn_switch_enum && !warn_switch_default)
6630 return;
6631
4ee9c684 6632 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
8b6866af 6633 if (!default_node)
5fb6a912 6634 warning_at (switch_location, OPT_Wswitch_default,
6635 "switch missing default case");
4ee9c684 6636
359d87c6 6637 /* From here on, we only care about about enumerated types. */
6638 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6639 return;
6640
561017b5 6641 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6642 if (!warn_switch_enum && !warn_switch)
359d87c6 6643 return;
6644
561017b5 6645 /* Check the cases. Warn about case values which are not members of
6646 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6647 there is no default case, check that exactly all enumeration
6648 literals are covered by the cases. */
6649
359d87c6 6650 /* Clearing COND if it is not an integer constant simplifies
6651 the tests inside the loop below. */
6652 if (TREE_CODE (cond) != INTEGER_CST)
6653 cond = NULL_TREE;
6654
6655 /* The time complexity here is O(N*lg(N)) worst case, but for the
6656 common case of monotonically increasing enumerators, it is
6657 O(N), since the nature of the splay tree will keep the next
6658 element adjacent to the root at all times. */
4ee9c684 6659
359d87c6 6660 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6661 {
6662 tree value = TREE_VALUE (chain);
3f00a6c0 6663 if (TREE_CODE (value) == CONST_DECL)
6664 value = DECL_INITIAL (value);
359d87c6 6665 node = splay_tree_lookup (cases, (splay_tree_key) value);
6666 if (node)
4ee9c684 6667 {
359d87c6 6668 /* Mark the CASE_LOW part of the case entry as seen. */
6669 tree label = (tree) node->value;
6670 CASE_LOW_SEEN (label) = 1;
6671 continue;
6672 }
6673
6674 /* Even though there wasn't an exact match, there might be a
f0b5f617 6675 case range which includes the enumerator's value. */
359d87c6 6676 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6677 if (node && CASE_HIGH ((tree) node->value))
6678 {
6679 tree label = (tree) node->value;
6680 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6681 if (cmp >= 0)
4ee9c684 6682 {
359d87c6 6683 /* If we match the upper bound exactly, mark the CASE_HIGH
6684 part of the case entry as seen. */
6685 if (cmp == 0)
6686 CASE_HIGH_SEEN (label) = 1;
6687 continue;
4ee9c684 6688 }
6689 }
6690
359d87c6 6691 /* We've now determined that this enumerated literal isn't
6692 handled by the case labels of the switch statement. */
4ee9c684 6693
359d87c6 6694 /* If the switch expression is a constant, we only really care
6695 about whether that constant is handled by the switch. */
6696 if (cond && tree_int_cst_compare (cond, value))
6697 continue;
4ee9c684 6698
6cbbbc89 6699 /* If there is a default_node, the only relevant option is
561017b5 6700 Wswitch-enum. Otherwise, if both are enabled then we prefer
6cbbbc89 6701 to warn using -Wswitch because -Wswitch is enabled by -Wall
6702 while -Wswitch-enum is explicit. */
561017b5 6703 warning_at (switch_location,
6704 (default_node || !warn_switch
6705 ? OPT_Wswitch_enum
6706 : OPT_Wswitch),
6707 "enumeration value %qE not handled in switch",
6708 TREE_PURPOSE (chain));
4ee9c684 6709 }
359d87c6 6710
6711 /* Warn if there are case expressions that don't correspond to
6712 enumerators. This can occur since C and C++ don't enforce
6713 type-checking of assignments to enumeration variables.
6714
6715 The time complexity here is now always O(N) worst case, since
6716 we should have marked both the lower bound and upper bound of
6717 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6718 above. This scan also resets those fields. */
6cbbbc89 6719
359d87c6 6720 splay_tree_foreach (cases, match_case_to_enum, type);
e41f0d80 6721}
6722
9dd48740 6723/* Finish an expression taking the address of LABEL (an
dda49785 6724 IDENTIFIER_NODE). Returns an expression for the address.
6725
6726 LOC is the location for the expression returned. */
d0a47c8d 6727
1cae46be 6728tree
dda49785 6729finish_label_address_expr (tree label, location_t loc)
d0a47c8d 6730{
6731 tree result;
6732
29438999 6733 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
d0a47c8d 6734
9dd48740 6735 if (label == error_mark_node)
6736 return error_mark_node;
6737
d0a47c8d 6738 label = lookup_label (label);
6739 if (label == NULL_TREE)
6740 result = null_pointer_node;
6741 else
6742 {
6743 TREE_USED (label) = 1;
6744 result = build1 (ADDR_EXPR, ptr_type_node, label);
344cd9b2 6745 /* The current function is not necessarily uninlinable.
d0a47c8d 6746 Computed gotos are incompatible with inlining, but the value
6747 here could be used only in a diagnostic, for example. */
dda49785 6748 protected_set_expr_location (result, loc);
d0a47c8d 6749 }
6750
6751 return result;
6752}
4f9a1c9b 6753\f
6754
6755/* Given a boolean expression ARG, return a tree representing an increment
6756 or decrement (as indicated by CODE) of ARG. The front end must check for
6757 invalid cases (e.g., decrement in C++). */
6758tree
1cae46be 6759boolean_increment (enum tree_code code, tree arg)
4f9a1c9b 6760{
6761 tree val;
69db191c 6762 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
c0f19401 6763
4f9a1c9b 6764 arg = stabilize_reference (arg);
6765 switch (code)
6766 {
6767 case PREINCREMENT_EXPR:
14ae0310 6768 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6769 break;
6770 case POSTINCREMENT_EXPR:
14ae0310 6771 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 6772 arg = save_expr (arg);
14ae0310 6773 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6774 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6775 break;
6776 case PREDECREMENT_EXPR:
14ae0310 6777 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6778 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6779 break;
6780 case POSTDECREMENT_EXPR:
14ae0310 6781 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 6782 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 6783 arg = save_expr (arg);
14ae0310 6784 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6785 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 6786 break;
6787 default:
231bd014 6788 gcc_unreachable ();
4f9a1c9b 6789 }
6790 TREE_SIDE_EFFECTS (val) = 1;
6791 return val;
6792}
76a6e674 6793\f
f3449a3c 6794/* Built-in macros for stddef.h and stdint.h, that require macros
6795 defined in this file. */
79cf3ec1 6796void
1cae46be 6797c_stddef_cpp_builtins(void)
1ed9d5f5 6798{
63994318 6799 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6800 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6801 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6802 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
36bccbfc 6803 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6804 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
b0726616 6805 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6806 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
f3449a3c 6807 if (SIG_ATOMIC_TYPE)
6808 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6809 if (INT8_TYPE)
6810 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6811 if (INT16_TYPE)
6812 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6813 if (INT32_TYPE)
6814 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6815 if (INT64_TYPE)
6816 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6817 if (UINT8_TYPE)
6818 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6819 if (UINT16_TYPE)
6820 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6821 if (UINT32_TYPE)
6822 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6823 if (UINT64_TYPE)
6824 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6825 if (INT_LEAST8_TYPE)
6826 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6827 if (INT_LEAST16_TYPE)
6828 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6829 if (INT_LEAST32_TYPE)
6830 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6831 if (INT_LEAST64_TYPE)
6832 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6833 if (UINT_LEAST8_TYPE)
6834 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6835 if (UINT_LEAST16_TYPE)
6836 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6837 if (UINT_LEAST32_TYPE)
6838 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6839 if (UINT_LEAST64_TYPE)
6840 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6841 if (INT_FAST8_TYPE)
6842 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6843 if (INT_FAST16_TYPE)
6844 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6845 if (INT_FAST32_TYPE)
6846 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6847 if (INT_FAST64_TYPE)
6848 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6849 if (UINT_FAST8_TYPE)
6850 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6851 if (UINT_FAST16_TYPE)
6852 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6853 if (UINT_FAST32_TYPE)
6854 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6855 if (UINT_FAST64_TYPE)
6856 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6857 if (INTPTR_TYPE)
6858 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6859 if (UINTPTR_TYPE)
6860 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
574006c3 6861}
6862
7d3b509a 6863static void
1cae46be 6864c_init_attributes (void)
7d3b509a 6865{
6866 /* Fill in the built_in_attributes array. */
7c446c95 6867#define DEF_ATTR_NULL_TREE(ENUM) \
7d3b509a 6868 built_in_attributes[(int) ENUM] = NULL_TREE;
7c446c95 6869#define DEF_ATTR_INT(ENUM, VALUE) \
ceb7b692 6870 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
c8010b80 6871#define DEF_ATTR_STRING(ENUM, VALUE) \
6872 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
7d3b509a 6873#define DEF_ATTR_IDENT(ENUM, STRING) \
6874 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6875#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6876 built_in_attributes[(int) ENUM] \
6877 = tree_cons (built_in_attributes[(int) PURPOSE], \
6878 built_in_attributes[(int) VALUE], \
6879 built_in_attributes[(int) CHAIN]);
7d3b509a 6880#include "builtin-attrs.def"
6881#undef DEF_ATTR_NULL_TREE
6882#undef DEF_ATTR_INT
6883#undef DEF_ATTR_IDENT
6884#undef DEF_ATTR_TREE_LIST
76a6e674 6885}
5f3cead1 6886
33199a81 6887/* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6888 identifier as an argument, so the front end shouldn't look it up. */
6889
6890bool
47b19b94 6891attribute_takes_identifier_p (const_tree attr_id)
33199a81 6892{
9bf1c74e 6893 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
02cb1060 6894 if (spec == NULL)
6895 /* Unknown attribute that we'll end up ignoring, return true so we
6896 don't complain about an identifier argument. */
6897 return true;
6898 else if (!strcmp ("mode", spec->name)
6899 || !strcmp ("format", spec->name)
6900 || !strcmp ("cleanup", spec->name))
47b19b94 6901 return true;
6902 else
6903 return targetm.attribute_takes_identifier_p (attr_id);
33199a81 6904}
6905
f8e93a2e 6906/* Attribute handlers common to C front ends. */
6907
6908/* Handle a "packed" attribute; arguments as in
6909 struct attribute_spec.handler. */
6910
6911static tree
9a03a746 6912handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
09347743 6913 int flags, bool *no_add_attrs)
f8e93a2e 6914{
f40175cb 6915 if (TYPE_P (*node))
f8e93a2e 6916 {
6917 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 6918 *node = build_variant_type_copy (*node);
f40175cb 6919 TYPE_PACKED (*node) = 1;
f8e93a2e 6920 }
6921 else if (TREE_CODE (*node) == FIELD_DECL)
c2ab04f9 6922 {
9fd767c5 6923 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6924 /* Still pack bitfields. */
6925 && ! DECL_INITIAL (*node))
c2ab04f9 6926 warning (OPT_Wattributes,
6927 "%qE attribute ignored for field of type %qT",
6928 name, TREE_TYPE (*node));
6929 else
6930 DECL_PACKED (*node) = 1;
6931 }
f8e93a2e 6932 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
f40175cb 6933 used for DECL_REGISTER. It wouldn't mean anything anyway.
6934 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6935 that changes what the typedef is typing. */
f8e93a2e 6936 else
6937 {
9b2d6d13 6938 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6939 *no_add_attrs = true;
6940 }
6941
6942 return NULL_TREE;
6943}
6944
6945/* Handle a "nocommon" attribute; arguments as in
6946 struct attribute_spec.handler. */
6947
6948static tree
1cae46be 6949handle_nocommon_attribute (tree *node, tree name,
9a03a746 6950 tree ARG_UNUSED (args),
6951 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6952{
f48c7f4a 6953 if (VAR_P (*node))
f8e93a2e 6954 DECL_COMMON (*node) = 0;
6955 else
6956 {
9b2d6d13 6957 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6958 *no_add_attrs = true;
6959 }
6960
6961 return NULL_TREE;
6962}
6963
6964/* Handle a "common" attribute; arguments as in
6965 struct attribute_spec.handler. */
6966
6967static tree
9a03a746 6968handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6969 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6970{
f48c7f4a 6971 if (VAR_P (*node))
f8e93a2e 6972 DECL_COMMON (*node) = 1;
6973 else
6974 {
9b2d6d13 6975 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 6976 *no_add_attrs = true;
6977 }
6978
6979 return NULL_TREE;
6980}
6981
6982/* Handle a "noreturn" attribute; arguments as in
6983 struct attribute_spec.handler. */
6984
6985static tree
9a03a746 6986handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6987 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 6988{
6989 tree type = TREE_TYPE (*node);
6990
6991 /* See FIXME comment in c_common_attribute_table. */
8c582e4f 6992 if (TREE_CODE (*node) == FUNCTION_DECL
6993 || objc_method_decl (TREE_CODE (*node)))
f8e93a2e 6994 TREE_THIS_VOLATILE (*node) = 1;
6995 else if (TREE_CODE (type) == POINTER_TYPE
6996 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6997 TREE_TYPE (*node)
9d4eeb52 6998 = (build_qualified_type
6999 (build_pointer_type
7000 (build_type_variant (TREE_TYPE (type),
7001 TYPE_READONLY (TREE_TYPE (type)), 1)),
7002 TYPE_QUALS (type)));
f8e93a2e 7003 else
7004 {
9b2d6d13 7005 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7006 *no_add_attrs = true;
7007 }
7008
7009 return NULL_TREE;
7010}
7011
5de92639 7012/* Handle a "hot" and attribute; arguments as in
7013 struct attribute_spec.handler. */
7014
7015static tree
7016handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
46f8e3b0 7017 int ARG_UNUSED (flags), bool *no_add_attrs)
5de92639 7018{
758a38ab 7019 if (TREE_CODE (*node) == FUNCTION_DECL
7020 || TREE_CODE (*node) == LABEL_DECL)
5de92639 7021 {
7022 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
7023 {
4a026b48 7024 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7025 "with attribute %qs", name, "cold");
5de92639 7026 *no_add_attrs = true;
7027 }
24470055 7028 /* Most of the rest of the hot processing is done later with
7029 lookup_attribute. */
5de92639 7030 }
7031 else
7032 {
7033 warning (OPT_Wattributes, "%qE attribute ignored", name);
7034 *no_add_attrs = true;
7035 }
7036
7037 return NULL_TREE;
7038}
758a38ab 7039
5de92639 7040/* Handle a "cold" and attribute; arguments as in
7041 struct attribute_spec.handler. */
7042
7043static tree
7044handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7045 int ARG_UNUSED (flags), bool *no_add_attrs)
7046{
758a38ab 7047 if (TREE_CODE (*node) == FUNCTION_DECL
7048 || TREE_CODE (*node) == LABEL_DECL)
5de92639 7049 {
7050 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
7051 {
4a026b48 7052 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7053 "with attribute %qs", name, "hot");
5de92639 7054 *no_add_attrs = true;
7055 }
24470055 7056 /* Most of the rest of the cold processing is done later with
7057 lookup_attribute. */
5de92639 7058 }
7059 else
7060 {
7061 warning (OPT_Wattributes, "%qE attribute ignored", name);
7062 *no_add_attrs = true;
7063 }
7064
7065 return NULL_TREE;
7066}
7067
a9196da9 7068/* Handle a "no_sanitize_address" attribute; arguments as in
d413ffdd 7069 struct attribute_spec.handler. */
7070
7071static tree
a9196da9 7072handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
7073 bool *no_add_attrs)
d413ffdd 7074{
7075 if (TREE_CODE (*node) != FUNCTION_DECL)
7076 {
7077 warning (OPT_Wattributes, "%qE attribute ignored", name);
7078 *no_add_attrs = true;
7079 }
7080
7081 return NULL_TREE;
7082}
7083
a9196da9 7084/* Handle a "no_address_safety_analysis" attribute; arguments as in
7085 struct attribute_spec.handler. */
7086
7087static tree
7088handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
7089 bool *no_add_attrs)
7090{
7091 if (TREE_CODE (*node) != FUNCTION_DECL)
7092 warning (OPT_Wattributes, "%qE attribute ignored", name);
7093 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
7094 DECL_ATTRIBUTES (*node)
7095 = tree_cons (get_identifier ("no_sanitize_address"),
7096 NULL_TREE, DECL_ATTRIBUTES (*node));
7097 *no_add_attrs = true;
7098 return NULL_TREE;
7099}
7100
05f893e1 7101/* Handle a "no_sanitize_undefined" attribute; arguments as in
7102 struct attribute_spec.handler. */
7103
7104static tree
7105handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
7106 bool *no_add_attrs)
7107{
7108 if (TREE_CODE (*node) != FUNCTION_DECL)
7109 {
7110 warning (OPT_Wattributes, "%qE attribute ignored", name);
7111 *no_add_attrs = true;
7112 }
7113
7114 return NULL_TREE;
7115}
7116
947aa916 7117/* Handle a "stack_protect" attribute; arguments as in
7118 struct attribute_spec.handler. */
7119static tree
7120handle_stack_protect_attribute (tree *node, tree name, tree, int,
7121 bool *no_add_attrs)
7122{
7123 if (TREE_CODE (*node) != FUNCTION_DECL)
7124 {
7125 warning (OPT_Wattributes, "%qE attribute ignored", name);
7126 *no_add_attrs = true;
7127 }
7128 else
7129 DECL_ATTRIBUTES (*node)
7130 = tree_cons (get_identifier ("stack_protect"),
7131 NULL_TREE, DECL_ATTRIBUTES (*node));
7132
7133 return NULL_TREE;
7134}
7135
f8e93a2e 7136/* Handle a "noinline" attribute; arguments as in
7137 struct attribute_spec.handler. */
7138
7139static tree
1cae46be 7140handle_noinline_attribute (tree *node, tree name,
9a03a746 7141 tree ARG_UNUSED (args),
7142 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7143{
7144 if (TREE_CODE (*node) == FUNCTION_DECL)
4a026b48 7145 {
7146 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
7147 {
7148 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7149 "with attribute %qs", name, "always_inline");
7150 *no_add_attrs = true;
7151 }
7152 else
7153 DECL_UNINLINABLE (*node) = 1;
7154 }
f8e93a2e 7155 else
7156 {
9b2d6d13 7157 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7158 *no_add_attrs = true;
7159 }
7160
7161 return NULL_TREE;
7162}
7163
bdb1f0d1 7164/* Handle a "noclone" attribute; arguments as in
7165 struct attribute_spec.handler. */
7166
7167static tree
7168handle_noclone_attribute (tree *node, tree name,
7169 tree ARG_UNUSED (args),
7170 int ARG_UNUSED (flags), bool *no_add_attrs)
7171{
7172 if (TREE_CODE (*node) != FUNCTION_DECL)
7173 {
7174 warning (OPT_Wattributes, "%qE attribute ignored", name);
7175 *no_add_attrs = true;
7176 }
7177
7178 return NULL_TREE;
7179}
7180
85fbea97 7181/* Handle a "no_icf" attribute; arguments as in
7182 struct attribute_spec.handler. */
7183
7184static tree
7185handle_noicf_attribute (tree *node, tree name,
7186 tree ARG_UNUSED (args),
7187 int ARG_UNUSED (flags), bool *no_add_attrs)
7188{
7189 if (TREE_CODE (*node) != FUNCTION_DECL)
7190 {
7191 warning (OPT_Wattributes, "%qE attribute ignored", name);
7192 *no_add_attrs = true;
7193 }
7194
7195 return NULL_TREE;
7196}
7197
7198
f8e93a2e 7199/* Handle a "always_inline" attribute; arguments as in
7200 struct attribute_spec.handler. */
7201
7202static tree
1cae46be 7203handle_always_inline_attribute (tree *node, tree name,
9a03a746 7204 tree ARG_UNUSED (args),
7205 int ARG_UNUSED (flags),
09347743 7206 bool *no_add_attrs)
f8e93a2e 7207{
7208 if (TREE_CODE (*node) == FUNCTION_DECL)
7209 {
4a026b48 7210 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
7211 {
7212 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
7213 "with %qs attribute", name, "noinline");
7214 *no_add_attrs = true;
7215 }
7216 else
7217 /* Set the attribute and mark it for disregarding inline
7218 limits. */
7219 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
f8e93a2e 7220 }
7221 else
7222 {
9b2d6d13 7223 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7224 *no_add_attrs = true;
541e4101 7225 }
7226
7227 return NULL_TREE;
7228}
7229
7230/* Handle a "gnu_inline" attribute; arguments as in
7231 struct attribute_spec.handler. */
7232
7233static tree
7234handle_gnu_inline_attribute (tree *node, tree name,
7235 tree ARG_UNUSED (args),
7236 int ARG_UNUSED (flags),
7237 bool *no_add_attrs)
7238{
7239 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7240 {
7241 /* Do nothing else, just set the attribute. We'll get at
7242 it later with lookup_attribute. */
7243 }
7244 else
7245 {
7246 warning (OPT_Wattributes, "%qE attribute ignored", name);
7247 *no_add_attrs = true;
7bd95dfd 7248 }
7249
7250 return NULL_TREE;
7251}
7252
7253/* Handle a "leaf" attribute; arguments as in
7254 struct attribute_spec.handler. */
7255
7256static tree
7257handle_leaf_attribute (tree *node, tree name,
7258 tree ARG_UNUSED (args),
7259 int ARG_UNUSED (flags), bool *no_add_attrs)
7260{
7261 if (TREE_CODE (*node) != FUNCTION_DECL)
7262 {
7263 warning (OPT_Wattributes, "%qE attribute ignored", name);
7264 *no_add_attrs = true;
7265 }
7266 if (!TREE_PUBLIC (*node))
7267 {
7268 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
7269 *no_add_attrs = true;
f8e93a2e 7270 }
7271
7272 return NULL_TREE;
7273}
7274
1b16fc45 7275/* Handle an "artificial" attribute; arguments as in
7276 struct attribute_spec.handler. */
7277
7278static tree
7279handle_artificial_attribute (tree *node, tree name,
7280 tree ARG_UNUSED (args),
7281 int ARG_UNUSED (flags),
7282 bool *no_add_attrs)
7283{
7284 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7285 {
7286 /* Do nothing else, just set the attribute. We'll get at
7287 it later with lookup_attribute. */
7288 }
7289 else
7290 {
7291 warning (OPT_Wattributes, "%qE attribute ignored", name);
7292 *no_add_attrs = true;
7293 }
7294
7295 return NULL_TREE;
7296}
7297
0cdd9887 7298/* Handle a "flatten" attribute; arguments as in
7299 struct attribute_spec.handler. */
7300
7301static tree
7302handle_flatten_attribute (tree *node, tree name,
a0c938f0 7303 tree args ATTRIBUTE_UNUSED,
7304 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
0cdd9887 7305{
7306 if (TREE_CODE (*node) == FUNCTION_DECL)
7307 /* Do nothing else, just set the attribute. We'll get at
7308 it later with lookup_attribute. */
7309 ;
7310 else
7311 {
7312 warning (OPT_Wattributes, "%qE attribute ignored", name);
7313 *no_add_attrs = true;
7314 }
7315
7316 return NULL_TREE;
7317}
7318
10fc867f 7319/* Handle a "warning" or "error" attribute; arguments as in
7320 struct attribute_spec.handler. */
7321
7322static tree
7323handle_error_attribute (tree *node, tree name, tree args,
7324 int ARG_UNUSED (flags), bool *no_add_attrs)
7325{
7326 if (TREE_CODE (*node) == FUNCTION_DECL
df936998 7327 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
10fc867f 7328 /* Do nothing else, just set the attribute. We'll get at
7329 it later with lookup_attribute. */
7330 ;
7331 else
7332 {
7333 warning (OPT_Wattributes, "%qE attribute ignored", name);
7334 *no_add_attrs = true;
7335 }
7336
7337 return NULL_TREE;
7338}
0cdd9887 7339
f8e93a2e 7340/* Handle a "used" attribute; arguments as in
7341 struct attribute_spec.handler. */
7342
7343static tree
9a03a746 7344handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7345 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7346{
d0a31bd8 7347 tree node = *pnode;
7348
7349 if (TREE_CODE (node) == FUNCTION_DECL
f48c7f4a 7350 || (VAR_P (node) && TREE_STATIC (node))
a4e3ffad 7351 || (TREE_CODE (node) == TYPE_DECL))
f54ed8bc 7352 {
f54ed8bc 7353 TREE_USED (node) = 1;
9423c9b7 7354 DECL_PRESERVE_P (node) = 1;
f48c7f4a 7355 if (VAR_P (node))
abc6c64f 7356 DECL_READ_P (node) = 1;
f54ed8bc 7357 }
f8e93a2e 7358 else
7359 {
9b2d6d13 7360 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7361 *no_add_attrs = true;
7362 }
7363
7364 return NULL_TREE;
7365}
7366
7367/* Handle a "unused" attribute; arguments as in
7368 struct attribute_spec.handler. */
7369
7370static tree
9a03a746 7371handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7372 int flags, bool *no_add_attrs)
f8e93a2e 7373{
7374 if (DECL_P (*node))
7375 {
7376 tree decl = *node;
7377
7378 if (TREE_CODE (decl) == PARM_DECL
f48c7f4a 7379 || VAR_P (decl)
f8e93a2e 7380 || TREE_CODE (decl) == FUNCTION_DECL
7381 || TREE_CODE (decl) == LABEL_DECL
7382 || TREE_CODE (decl) == TYPE_DECL)
abc6c64f 7383 {
7384 TREE_USED (decl) = 1;
f48c7f4a 7385 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
abc6c64f 7386 DECL_READ_P (decl) = 1;
7387 }
f8e93a2e 7388 else
7389 {
9b2d6d13 7390 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7391 *no_add_attrs = true;
7392 }
7393 }
7394 else
7395 {
7396 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 7397 *node = build_variant_type_copy (*node);
f8e93a2e 7398 TREE_USED (*node) = 1;
7399 }
7400
7401 return NULL_TREE;
7402}
7403
62eec3b4 7404/* Handle a "externally_visible" attribute; arguments as in
7405 struct attribute_spec.handler. */
7406
7407static tree
7408handle_externally_visible_attribute (tree *pnode, tree name,
7409 tree ARG_UNUSED (args),
7410 int ARG_UNUSED (flags),
7411 bool *no_add_attrs)
7412{
7413 tree node = *pnode;
7414
b443c459 7415 if (VAR_OR_FUNCTION_DECL_P (node))
62eec3b4 7416 {
ba12ea31 7417 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7418 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7419 {
7420 warning (OPT_Wattributes,
7421 "%qE attribute have effect only on public objects", name);
7422 *no_add_attrs = true;
7423 }
62eec3b4 7424 }
62eec3b4 7425 else
7426 {
7427 warning (OPT_Wattributes, "%qE attribute ignored", name);
7428 *no_add_attrs = true;
7429 }
7430
7431 return NULL_TREE;
7432}
7433
6b722052 7434/* Handle the "no_reorder" attribute. Arguments as in
7435 struct attribute_spec.handler. */
7436
7437static tree
7438handle_no_reorder_attribute (tree *pnode,
7439 tree name,
7440 tree,
7441 int,
7442 bool *no_add_attrs)
7443{
7444 tree node = *pnode;
7445
b443c459 7446 if (!VAR_OR_FUNCTION_DECL_P (node)
6b722052 7447 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7448 {
7449 warning (OPT_Wattributes,
7450 "%qE attribute only affects top level objects",
7451 name);
7452 *no_add_attrs = true;
7453 }
7454
7455 return NULL_TREE;
7456}
7457
f8e93a2e 7458/* Handle a "const" attribute; arguments as in
7459 struct attribute_spec.handler. */
7460
7461static tree
9a03a746 7462handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7463 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7464{
7465 tree type = TREE_TYPE (*node);
7466
7467 /* See FIXME comment on noreturn in c_common_attribute_table. */
7468 if (TREE_CODE (*node) == FUNCTION_DECL)
7469 TREE_READONLY (*node) = 1;
7470 else if (TREE_CODE (type) == POINTER_TYPE
7471 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7472 TREE_TYPE (*node)
9d4eeb52 7473 = (build_qualified_type
7474 (build_pointer_type
7475 (build_type_variant (TREE_TYPE (type), 1,
7476 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7477 TYPE_QUALS (type)));
f8e93a2e 7478 else
7479 {
9b2d6d13 7480 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7481 *no_add_attrs = true;
7482 }
7483
7484 return NULL_TREE;
7485}
7486
7487/* Handle a "transparent_union" attribute; arguments as in
7488 struct attribute_spec.handler. */
7489
7490static tree
1cae46be 7491handle_transparent_union_attribute (tree *node, tree name,
9a03a746 7492 tree ARG_UNUSED (args), int flags,
09347743 7493 bool *no_add_attrs)
f8e93a2e 7494{
881eb642 7495 tree type;
03908818 7496
7497 *no_add_attrs = true;
f8e93a2e 7498
ffcdbf9c 7499
7500 if (TREE_CODE (*node) == TYPE_DECL
7501 && ! (flags & ATTR_FLAG_CXX11))
881eb642 7502 node = &TREE_TYPE (*node);
7503 type = *node;
f8e93a2e 7504
03908818 7505 if (TREE_CODE (type) == UNION_TYPE)
f8e93a2e 7506 {
fca86134 7507 /* Make sure that the first field will work for a transparent union.
7508 If the type isn't complete yet, leave the check to the code in
7509 finish_struct. */
7510 if (TYPE_SIZE (type))
7511 {
7512 tree first = first_field (type);
7513 if (first == NULL_TREE
7514 || DECL_ARTIFICIAL (first)
7515 || TYPE_MODE (type) != DECL_MODE (first))
7516 goto ignored;
7517 }
7518
f8e93a2e 7519 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
03908818 7520 {
fca86134 7521 /* If the type isn't complete yet, setting the flag
7522 on a variant wouldn't ever be checked. */
7523 if (!TYPE_SIZE (type))
7524 goto ignored;
7525
7526 /* build_duplicate_type doesn't work for C++. */
7527 if (c_dialect_cxx ())
03908818 7528 goto ignored;
7529
7530 /* A type variant isn't good enough, since we don't a cast
7531 to such a type removed as a no-op. */
7532 *node = type = build_duplicate_type (type);
7533 }
7534
8df5a43d 7535 TYPE_TRANSPARENT_AGGR (type) = 1;
03908818 7536 return NULL_TREE;
f8e93a2e 7537 }
7538
03908818 7539 ignored:
7540 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7541 return NULL_TREE;
7542}
7543
9af7fd5b 7544/* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7545 get the requested priority for a constructor or destructor,
7546 possibly issuing diagnostics for invalid or reserved
7547 priorities. */
7548
7549static priority_type
7550get_priority (tree args, bool is_destructor)
7551{
7552 HOST_WIDE_INT pri;
6c181a06 7553 tree arg;
9af7fd5b 7554
7555 if (!args)
7556 return DEFAULT_INIT_PRIORITY;
48e1416a 7557
28fbc04f 7558 if (!SUPPORTS_INIT_PRIORITY)
7559 {
7560 if (is_destructor)
7561 error ("destructor priorities are not supported");
7562 else
7563 error ("constructor priorities are not supported");
7564 return DEFAULT_INIT_PRIORITY;
7565 }
7566
6c181a06 7567 arg = TREE_VALUE (args);
253e1cae 7568 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7569 goto invalid;
7570 if (arg == error_mark_node)
7571 return DEFAULT_INIT_PRIORITY;
c28ddc97 7572 arg = default_conversion (arg);
e913b5cd 7573 if (!tree_fits_shwi_p (arg)
6c181a06 7574 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
9af7fd5b 7575 goto invalid;
7576
e913b5cd 7577 pri = tree_to_shwi (arg);
9af7fd5b 7578 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7579 goto invalid;
7580
7581 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7582 {
7583 if (is_destructor)
7584 warning (0,
7585 "destructor priorities from 0 to %d are reserved "
48e1416a 7586 "for the implementation",
9af7fd5b 7587 MAX_RESERVED_INIT_PRIORITY);
7588 else
7589 warning (0,
7590 "constructor priorities from 0 to %d are reserved "
48e1416a 7591 "for the implementation",
9af7fd5b 7592 MAX_RESERVED_INIT_PRIORITY);
7593 }
7594 return pri;
7595
7596 invalid:
7597 if (is_destructor)
7598 error ("destructor priorities must be integers from 0 to %d inclusive",
7599 MAX_INIT_PRIORITY);
7600 else
7601 error ("constructor priorities must be integers from 0 to %d inclusive",
7602 MAX_INIT_PRIORITY);
7603 return DEFAULT_INIT_PRIORITY;
7604}
7605
f8e93a2e 7606/* Handle a "constructor" attribute; arguments as in
7607 struct attribute_spec.handler. */
7608
7609static tree
9af7fd5b 7610handle_constructor_attribute (tree *node, tree name, tree args,
9a03a746 7611 int ARG_UNUSED (flags),
09347743 7612 bool *no_add_attrs)
f8e93a2e 7613{
7614 tree decl = *node;
7615 tree type = TREE_TYPE (decl);
7616
7617 if (TREE_CODE (decl) == FUNCTION_DECL
7618 && TREE_CODE (type) == FUNCTION_TYPE
7619 && decl_function_context (decl) == 0)
7620 {
9af7fd5b 7621 priority_type priority;
f8e93a2e 7622 DECL_STATIC_CONSTRUCTOR (decl) = 1;
9af7fd5b 7623 priority = get_priority (args, /*is_destructor=*/false);
7624 SET_DECL_INIT_PRIORITY (decl, priority);
f8e93a2e 7625 TREE_USED (decl) = 1;
7626 }
7627 else
7628 {
9b2d6d13 7629 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7630 *no_add_attrs = true;
7631 }
7632
7633 return NULL_TREE;
7634}
7635
7636/* Handle a "destructor" attribute; arguments as in
7637 struct attribute_spec.handler. */
7638
7639static tree
9af7fd5b 7640handle_destructor_attribute (tree *node, tree name, tree args,
9a03a746 7641 int ARG_UNUSED (flags),
09347743 7642 bool *no_add_attrs)
f8e93a2e 7643{
7644 tree decl = *node;
7645 tree type = TREE_TYPE (decl);
7646
7647 if (TREE_CODE (decl) == FUNCTION_DECL
7648 && TREE_CODE (type) == FUNCTION_TYPE
7649 && decl_function_context (decl) == 0)
7650 {
9af7fd5b 7651 priority_type priority;
f8e93a2e 7652 DECL_STATIC_DESTRUCTOR (decl) = 1;
9af7fd5b 7653 priority = get_priority (args, /*is_destructor=*/true);
7654 SET_DECL_FINI_PRIORITY (decl, priority);
f8e93a2e 7655 TREE_USED (decl) = 1;
7656 }
7657 else
7658 {
9b2d6d13 7659 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7660 *no_add_attrs = true;
7661 }
7662
7663 return NULL_TREE;
7664}
7665
1c58e3f1 7666/* Nonzero if the mode is a valid vector mode for this architecture.
7667 This returns nonzero even if there is no hardware support for the
7668 vector mode, but we can emulate with narrower modes. */
7669
7670static int
3754d046 7671vector_mode_valid_p (machine_mode mode)
1c58e3f1 7672{
7673 enum mode_class mclass = GET_MODE_CLASS (mode);
3754d046 7674 machine_mode innermode;
1c58e3f1 7675
7676 /* Doh! What's going on? */
7677 if (mclass != MODE_VECTOR_INT
7678 && mclass != MODE_VECTOR_FLOAT
7679 && mclass != MODE_VECTOR_FRACT
7680 && mclass != MODE_VECTOR_UFRACT
7681 && mclass != MODE_VECTOR_ACCUM
7682 && mclass != MODE_VECTOR_UACCUM)
7683 return 0;
7684
7685 /* Hardware support. Woo hoo! */
7686 if (targetm.vector_mode_supported_p (mode))
7687 return 1;
7688
7689 innermode = GET_MODE_INNER (mode);
7690
7691 /* We should probably return 1 if requesting V4DI and we have no DI,
7692 but we have V2DI, but this is probably very unlikely. */
7693
7694 /* If we have support for the inner mode, we can safely emulate it.
7695 We may not have V2DI, but me can emulate with a pair of DIs. */
7696 return targetm.scalar_mode_supported_p (innermode);
7697}
7698
7699
f8e93a2e 7700/* Handle a "mode" attribute; arguments as in
7701 struct attribute_spec.handler. */
7702
7703static tree
9a03a746 7704handle_mode_attribute (tree *node, tree name, tree args,
7705 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7706{
7707 tree type = *node;
d1dd9ac0 7708 tree ident = TREE_VALUE (args);
f8e93a2e 7709
7710 *no_add_attrs = true;
ab2c1de8 7711
d1dd9ac0 7712 if (TREE_CODE (ident) != IDENTIFIER_NODE)
9b2d6d13 7713 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 7714 else
7715 {
7716 int j;
d1dd9ac0 7717 const char *p = IDENTIFIER_POINTER (ident);
f8e93a2e 7718 int len = strlen (p);
3754d046 7719 machine_mode mode = VOIDmode;
f8e93a2e 7720 tree typefm;
b2aef146 7721 bool valid_mode;
f8e93a2e 7722
7723 if (len > 4 && p[0] == '_' && p[1] == '_'
7724 && p[len - 1] == '_' && p[len - 2] == '_')
7725 {
4fd61bc6 7726 char *newp = (char *) alloca (len - 1);
f8e93a2e 7727
7728 strcpy (newp, &p[2]);
7729 newp[len - 4] = '\0';
7730 p = newp;
7731 }
7732
7733 /* Change this type to have a type with the specified mode.
7734 First check for the special modes. */
84166705 7735 if (!strcmp (p, "byte"))
f8e93a2e 7736 mode = byte_mode;
7737 else if (!strcmp (p, "word"))
7738 mode = word_mode;
84166705 7739 else if (!strcmp (p, "pointer"))
f8e93a2e 7740 mode = ptr_mode;
0ef89dfd 7741 else if (!strcmp (p, "libgcc_cmp_return"))
7742 mode = targetm.libgcc_cmp_return_mode ();
7743 else if (!strcmp (p, "libgcc_shift_count"))
7744 mode = targetm.libgcc_shift_count_mode ();
1bd43494 7745 else if (!strcmp (p, "unwind_word"))
7746 mode = targetm.unwind_word_mode ();
f8e93a2e 7747 else
7748 for (j = 0; j < NUM_MACHINE_MODES; j++)
7749 if (!strcmp (p, GET_MODE_NAME (j)))
743a6f47 7750 {
3754d046 7751 mode = (machine_mode) j;
743a6f47 7752 break;
7753 }
f8e93a2e 7754
7755 if (mode == VOIDmode)
4917c376 7756 {
d1dd9ac0 7757 error ("unknown machine mode %qE", ident);
4917c376 7758 return NULL_TREE;
7759 }
7760
b2aef146 7761 valid_mode = false;
7762 switch (GET_MODE_CLASS (mode))
4917c376 7763 {
b2aef146 7764 case MODE_INT:
7765 case MODE_PARTIAL_INT:
7766 case MODE_FLOAT:
c4503c0a 7767 case MODE_DECIMAL_FLOAT:
9421ebb9 7768 case MODE_FRACT:
7769 case MODE_UFRACT:
7770 case MODE_ACCUM:
7771 case MODE_UACCUM:
b2aef146 7772 valid_mode = targetm.scalar_mode_supported_p (mode);
7773 break;
7774
7775 case MODE_COMPLEX_INT:
7776 case MODE_COMPLEX_FLOAT:
7777 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7778 break;
7779
7780 case MODE_VECTOR_INT:
7781 case MODE_VECTOR_FLOAT:
9421ebb9 7782 case MODE_VECTOR_FRACT:
7783 case MODE_VECTOR_UFRACT:
7784 case MODE_VECTOR_ACCUM:
7785 case MODE_VECTOR_UACCUM:
9b2d6d13 7786 warning (OPT_Wattributes, "specifying vector types with "
7787 "__attribute__ ((mode)) is deprecated");
7788 warning (OPT_Wattributes,
7789 "use __attribute__ ((vector_size)) instead");
b2aef146 7790 valid_mode = vector_mode_valid_p (mode);
7791 break;
4917c376 7792
b2aef146 7793 default:
7794 break;
7795 }
7796 if (!valid_mode)
7797 {
1e5fcbe2 7798 error ("unable to emulate %qs", p);
b2aef146 7799 return NULL_TREE;
7800 }
4917c376 7801
b2aef146 7802 if (POINTER_TYPE_P (type))
ead34f59 7803 {
6d5d708e 7804 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
3754d046 7805 tree (*fn)(tree, machine_mode, bool);
b2aef146 7806
6d5d708e 7807 if (!targetm.addr_space.valid_pointer_mode (mode, as))
ead34f59 7808 {
1e5fcbe2 7809 error ("invalid pointer mode %qs", p);
ead34f59 7810 return NULL_TREE;
7811 }
7812
a0c938f0 7813 if (TREE_CODE (type) == POINTER_TYPE)
b2aef146 7814 fn = build_pointer_type_for_mode;
805e22b2 7815 else
b2aef146 7816 fn = build_reference_type_for_mode;
7817 typefm = fn (TREE_TYPE (type), mode, false);
ead34f59 7818 }
b2aef146 7819 else
9421ebb9 7820 {
7821 /* For fixed-point modes, we need to test if the signness of type
7822 and the machine mode are consistent. */
7823 if (ALL_FIXED_POINT_MODE_P (mode)
7824 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7825 {
bf776685 7826 error ("signedness of type and machine mode %qs don%'t match", p);
9421ebb9 7827 return NULL_TREE;
7828 }
7829 /* For fixed-point modes, we need to pass saturating info. */
7830 typefm = lang_hooks.types.type_for_mode (mode,
7831 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7832 : TYPE_UNSIGNED (type));
7833 }
3a648ab9 7834
b2aef146 7835 if (typefm == NULL_TREE)
7836 {
743a6f47 7837 error ("no data type for mode %qs", p);
b2aef146 7838 return NULL_TREE;
7839 }
3a648ab9 7840 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7841 {
7842 /* For enumeral types, copy the precision from the integer
7843 type returned above. If not an INTEGER_TYPE, we can't use
7844 this mode for this type. */
7845 if (TREE_CODE (typefm) != INTEGER_TYPE)
7846 {
743a6f47 7847 error ("cannot use mode %qs for enumeral types", p);
3a648ab9 7848 return NULL_TREE;
7849 }
7850
10080eac 7851 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7852 {
7853 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7854 typefm = type;
7855 }
7856 else
7857 {
7858 /* We cannot build a type variant, as there's code that assumes
7859 that TYPE_MAIN_VARIANT has the same mode. This includes the
7860 debug generators. Instead, create a subrange type. This
7861 results in all of the enumeral values being emitted only once
7862 in the original, and the subtype gets them by reference. */
7863 if (TYPE_UNSIGNED (type))
7864 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7865 else
7866 typefm = make_signed_type (TYPE_PRECISION (typefm));
7867 TREE_TYPE (typefm) = type;
7868 }
3a648ab9 7869 }
4bf450a1 7870 else if (VECTOR_MODE_P (mode)
7871 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7872 : TREE_CODE (type) != TREE_CODE (typefm))
743a6f47 7873 {
7874 error ("mode %qs applied to inappropriate type", p);
7875 return NULL_TREE;
7876 }
7877
b2aef146 7878 *node = typefm;
f8e93a2e 7879 }
7880
7881 return NULL_TREE;
7882}
7883
7884/* Handle a "section" attribute; arguments as in
7885 struct attribute_spec.handler. */
7886
7887static tree
9a03a746 7888handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7889 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 7890{
7891 tree decl = *node;
7892
9866562d 7893 if (!targetm_common.have_named_sections)
f8e93a2e 7894 {
9866562d 7895 error_at (DECL_SOURCE_LOCATION (*node),
7896 "section attributes are not supported for this target");
7897 goto fail;
7898 }
065efcb1 7899
9866562d 7900 user_defined_section_attribute = true;
f8e93a2e 7901
b443c459 7902 if (!VAR_OR_FUNCTION_DECL_P (decl))
9866562d 7903 {
7904 error ("section attribute not allowed for %q+D", *node);
7905 goto fail;
f8e93a2e 7906 }
9866562d 7907
7908 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
f8e93a2e 7909 {
9866562d 7910 error ("section attribute argument not a string constant");
7911 goto fail;
f8e93a2e 7912 }
7913
f48c7f4a 7914 if (VAR_P (decl)
9866562d 7915 && current_function_decl != NULL_TREE
7916 && !TREE_STATIC (decl))
7917 {
7918 error_at (DECL_SOURCE_LOCATION (decl),
7919 "section attribute cannot be specified for local variables");
7920 goto fail;
7921 }
7922
7923 /* The decl may have already been given a section attribute
7924 from a previous declaration. Ensure they match. */
7925 if (DECL_SECTION_NAME (decl) != NULL
7926 && strcmp (DECL_SECTION_NAME (decl),
7927 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7928 {
7929 error ("section of %q+D conflicts with previous declaration", *node);
7930 goto fail;
7931 }
7932
f48c7f4a 7933 if (VAR_P (decl)
9866562d 7934 && !targetm.have_tls && targetm.emutls.tmpl_section
7935 && DECL_THREAD_LOCAL_P (decl))
7936 {
7937 error ("section of %q+D cannot be overridden", *node);
7938 goto fail;
7939 }
7940
7941 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
7942 return NULL_TREE;
7943
7944fail:
7945 *no_add_attrs = true;
f8e93a2e 7946 return NULL_TREE;
7947}
7948
83e25171 7949/* Check whether ALIGN is a valid user-specified alignment. If so,
7950 return its base-2 log; if not, output an error and return -1. If
7951 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7952 no error. */
7953int
7954check_user_alignment (const_tree align, bool allow_zero)
7955{
7956 int i;
7957
3e5a8b00 7958 if (error_operand_p (align))
7959 return -1;
5abaa10a 7960 if (TREE_CODE (align) != INTEGER_CST
7961 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
83e25171 7962 {
7963 error ("requested alignment is not an integer constant");
7964 return -1;
7965 }
7966 else if (allow_zero && integer_zerop (align))
7967 return -1;
1a087624 7968 else if (tree_int_cst_sgn (align) == -1
7969 || (i = tree_log2 (align)) == -1)
83e25171 7970 {
1a087624 7971 error ("requested alignment is not a positive power of 2");
83e25171 7972 return -1;
7973 }
7974 else if (i >= HOST_BITS_PER_INT - BITS_PER_UNIT_LOG)
7975 {
7976 error ("requested alignment is too large");
7977 return -1;
7978 }
7979 return i;
7980}
7981
ffcdbf9c 7982/*
7983 If in c++-11, check if the c++-11 alignment constraint with respect
7984 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7985 c++-11 mode, does nothing.
7986
7987 [dcl.align]2/ says:
7988
7989 [* if the constant expression evaluates to a fundamental alignment,
7990 the alignment requirement of the declared entity shall be the
7991 specified fundamental alignment.
7992
7993 * if the constant expression evaluates to an extended alignment
7994 and the implementation supports that alignment in the context
7995 of the declaration, the alignment of the declared entity shall
7996 be that alignment
7997
7998 * if the constant expression evaluates to an extended alignment
7999 and the implementation does not support that alignment in the
8000 context of the declaration, the program is ill-formed]. */
8001
8002static bool
8003check_cxx_fundamental_alignment_constraints (tree node,
8004 unsigned align_log,
8005 int flags)
8006{
8007 bool alignment_too_large_p = false;
8008 unsigned requested_alignment = 1U << align_log;
8009 unsigned max_align = 0;
8010
8011 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
8012 || (node == NULL_TREE || node == error_mark_node))
8013 return true;
8014
8015 if (cxx_fundamental_alignment_p (requested_alignment))
8016 return true;
8017
8018 if (DECL_P (node))
8019 {
8020 if (TREE_STATIC (node))
8021 {
8022 /* For file scope variables and static members, the target
8023 supports alignments that are at most
8024 MAX_OFILE_ALIGNMENT. */
8025 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
8026 alignment_too_large_p = true;
8027 }
8028 else
8029 {
8030#ifdef BIGGEST_FIELD_ALIGNMENT
8031#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
8032#else
8033#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
8034#endif
8035 /* For non-static members, the target supports either
8036 alignments that at most either BIGGEST_FIELD_ALIGNMENT
8037 if it is defined or BIGGEST_ALIGNMENT. */
8038 max_align = MAX_TARGET_FIELD_ALIGNMENT;
8039 if (TREE_CODE (node) == FIELD_DECL
8040 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
8041 alignment_too_large_p = true;
8042#undef MAX_TARGET_FIELD_ALIGNMENT
8043 /* For stack variables, the target supports at most
8044 MAX_STACK_ALIGNMENT. */
8045 else if (decl_function_context (node) != NULL
8046 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
8047 alignment_too_large_p = true;
8048 }
8049 }
8050 else if (TYPE_P (node))
8051 {
8052 /* Let's be liberal for types. */
8053 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
8054 alignment_too_large_p = true;
8055 }
8056
8057 if (alignment_too_large_p)
8058 pedwarn (input_location, OPT_Wattributes,
8059 "requested alignment %d is larger than %d",
8060 requested_alignment, max_align);
8061
8062 return !alignment_too_large_p;
8063}
8064
f8e93a2e 8065/* Handle a "aligned" attribute; arguments as in
8066 struct attribute_spec.handler. */
8067
8068static tree
9a03a746 8069handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
09347743 8070 int flags, bool *no_add_attrs)
f8e93a2e 8071{
8072 tree decl = NULL_TREE;
8073 tree *type = NULL;
8074 int is_type = 0;
caf62483 8075 tree align_expr;
f8e93a2e 8076 int i;
8077
caf62483 8078 if (args)
8079 {
8080 align_expr = TREE_VALUE (args);
3e5a8b00 8081 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
8082 && TREE_CODE (align_expr) != FUNCTION_DECL)
caf62483 8083 align_expr = default_conversion (align_expr);
8084 }
8085 else
8086 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
8087
f8e93a2e 8088 if (DECL_P (*node))
8089 {
8090 decl = *node;
8091 type = &TREE_TYPE (decl);
8092 is_type = TREE_CODE (*node) == TYPE_DECL;
8093 }
8094 else if (TYPE_P (*node))
8095 type = node, is_type = 1;
8096
ffcdbf9c 8097 if ((i = check_user_alignment (align_expr, false)) == -1
8098 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
83e25171 8099 *no_add_attrs = true;
f8e93a2e 8100 else if (is_type)
8101 {
2ec3af9c 8102 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8103 /* OK, modify the type in place. */;
f8e93a2e 8104 /* If we have a TYPE_DECL, then copy the type, so that we
8105 don't accidentally modify a builtin type. See pushdecl. */
2ec3af9c 8106 else if (decl && TREE_TYPE (decl) != error_mark_node
8107 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
f8e93a2e 8108 {
8109 tree tt = TREE_TYPE (decl);
e086912e 8110 *type = build_variant_type_copy (*type);
f8e93a2e 8111 DECL_ORIGINAL_TYPE (decl) = tt;
8112 TYPE_NAME (*type) = decl;
8113 TREE_USED (*type) = TREE_USED (decl);
8114 TREE_TYPE (decl) = *type;
8115 }
2ec3af9c 8116 else
e086912e 8117 *type = build_variant_type_copy (*type);
f8e93a2e 8118
7cfdc2f0 8119 TYPE_ALIGN (*type) = (1U << i) * BITS_PER_UNIT;
f8e93a2e 8120 TYPE_USER_ALIGN (*type) = 1;
8121 }
097b5c8b 8122 else if (! VAR_OR_FUNCTION_DECL_P (decl)
f8e93a2e 8123 && TREE_CODE (decl) != FIELD_DECL)
8124 {
3cf8b391 8125 error ("alignment may not be specified for %q+D", decl);
f8e93a2e 8126 *no_add_attrs = true;
8127 }
ffcdbf9c 8128 else if (DECL_USER_ALIGN (decl)
8129 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
8130 /* C++-11 [dcl.align/4]:
8131
8132 When multiple alignment-specifiers are specified for an
8133 entity, the alignment requirement shall be set to the
8134 strictest specified alignment.
8135
8136 This formally comes from the c++11 specification but we are
8137 doing it for the GNU attribute syntax as well. */
8138 *no_add_attrs = true;
097b5c8b 8139 else if (TREE_CODE (decl) == FUNCTION_DECL
7cfdc2f0 8140 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
097b5c8b 8141 {
8142 if (DECL_USER_ALIGN (decl))
8143 error ("alignment for %q+D was previously specified as %d "
8144 "and may not be decreased", decl,
8145 DECL_ALIGN (decl) / BITS_PER_UNIT);
8146 else
8147 error ("alignment for %q+D must be at least %d", decl,
8148 DECL_ALIGN (decl) / BITS_PER_UNIT);
8149 *no_add_attrs = true;
8150 }
f8e93a2e 8151 else
8152 {
7cfdc2f0 8153 DECL_ALIGN (decl) = (1U << i) * BITS_PER_UNIT;
f8e93a2e 8154 DECL_USER_ALIGN (decl) = 1;
8155 }
8156
8157 return NULL_TREE;
8158}
8159
8160/* Handle a "weak" attribute; arguments as in
8161 struct attribute_spec.handler. */
8162
8163static tree
f948b309 8164handle_weak_attribute (tree *node, tree name,
9a03a746 8165 tree ARG_UNUSED (args),
8166 int ARG_UNUSED (flags),
8167 bool * ARG_UNUSED (no_add_attrs))
f8e93a2e 8168{
f948b309 8169 if (TREE_CODE (*node) == FUNCTION_DECL
059a60f3 8170 && DECL_DECLARED_INLINE_P (*node))
8171 {
0725e25c 8172 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
059a60f3 8173 *no_add_attrs = true;
8174 }
85c0a25c 8175 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8176 {
8177 error ("indirect function %q+D cannot be declared weak", *node);
8178 *no_add_attrs = true;
8179 return NULL_TREE;
8180 }
b443c459 8181 else if (VAR_OR_FUNCTION_DECL_P (*node))
c11b875d 8182 {
8183 struct symtab_node *n = symtab_node::get (*node);
8184 if (n && n->refuse_visibility_changes)
8185 error ("%+D declared weak after being used", *node);
8186 declare_weak (*node);
8187 }
f948b309 8188 else
8189 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8190
8191 return NULL_TREE;
8192}
8193
4641882f 8194/* Handle a "noplt" attribute; arguments as in
8195 struct attribute_spec.handler. */
8196
8197static tree
8198handle_noplt_attribute (tree *node, tree name,
8199 tree ARG_UNUSED (args),
8200 int ARG_UNUSED (flags),
8201 bool * ARG_UNUSED (no_add_attrs))
8202{
8203 if (TREE_CODE (*node) != FUNCTION_DECL)
8204 {
8205 warning (OPT_Wattributes,
8206 "%qE attribute is only applicable on functions", name);
8207 *no_add_attrs = true;
8208 return NULL_TREE;
8209 }
8210 return NULL_TREE;
8211}
8212
85c0a25c 8213/* Handle an "alias" or "ifunc" attribute; arguments as in
8214 struct attribute_spec.handler, except that IS_ALIAS tells us
8215 whether this is an alias as opposed to ifunc attribute. */
f8e93a2e 8216
8217static tree
85c0a25c 8218handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
8219 bool *no_add_attrs)
f8e93a2e 8220{
8221 tree decl = *node;
8222
85c0a25c 8223 if (TREE_CODE (decl) != FUNCTION_DECL
f48c7f4a 8224 && (!is_alias || !VAR_P (decl)))
9e830260 8225 {
8226 warning (OPT_Wattributes, "%qE attribute ignored", name);
8227 *no_add_attrs = true;
8228 }
8229 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
48e1416a 8230 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 8231 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
8232 /* A static variable declaration is always a tentative definition,
8233 but the alias is a non-tentative definition which overrides. */
48e1416a 8234 || (TREE_CODE (decl) != FUNCTION_DECL
0a3ecdc1 8235 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
f8e93a2e 8236 {
85c0a25c 8237 error ("%q+D defined both normally and as %qE attribute", decl, name);
f8e93a2e 8238 *no_add_attrs = true;
85c0a25c 8239 return NULL_TREE;
f8e93a2e 8240 }
85c0a25c 8241 else if (!is_alias
8242 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
8243 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
8244 {
8245 error ("weak %q+D cannot be defined %qE", decl, name);
8246 *no_add_attrs = true;
8247 return NULL_TREE;
8248 }
8c42f0d9 8249
8250 /* Note that the very first time we process a nested declaration,
8251 decl_function_context will not be set. Indeed, *would* never
8252 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8253 we do below. After such frobbery, pushdecl would set the context.
8254 In any case, this is never what we want. */
8255 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
f8e93a2e 8256 {
8257 tree id;
8258
8259 id = TREE_VALUE (args);
8260 if (TREE_CODE (id) != STRING_CST)
8261 {
85c0a25c 8262 error ("attribute %qE argument not a string", name);
f8e93a2e 8263 *no_add_attrs = true;
8264 return NULL_TREE;
8265 }
8266 id = get_identifier (TREE_STRING_POINTER (id));
8267 /* This counts as a use of the object pointed to. */
8268 TREE_USED (id) = 1;
8269
8270 if (TREE_CODE (decl) == FUNCTION_DECL)
8271 DECL_INITIAL (decl) = error_mark_node;
8272 else
f2526cce 8273 TREE_STATIC (decl) = 1;
85c0a25c 8274
8275 if (!is_alias)
8276 /* ifuncs are also aliases, so set that attribute too. */
8277 DECL_ATTRIBUTES (decl)
8278 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
f8e93a2e 8279 }
8280 else
8281 {
9b2d6d13 8282 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8283 *no_add_attrs = true;
8284 }
8285
8e857c41 8286 if (decl_in_symtab_p (*node))
8287 {
8288 struct symtab_node *n = symtab_node::get (decl);
8289 if (n && n->refuse_visibility_changes)
8290 {
8291 if (is_alias)
8292 error ("%+D declared alias after being used", decl);
8293 else
8294 error ("%+D declared ifunc after being used", decl);
8295 }
8296 }
8297
8298
f8e93a2e 8299 return NULL_TREE;
8300}
8301
85c0a25c 8302/* Handle an "alias" or "ifunc" attribute; arguments as in
8303 struct attribute_spec.handler. */
8304
8305static tree
8306handle_ifunc_attribute (tree *node, tree name, tree args,
8307 int ARG_UNUSED (flags), bool *no_add_attrs)
8308{
8309 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8310}
8311
8312/* Handle an "alias" or "ifunc" attribute; arguments as in
8313 struct attribute_spec.handler. */
8314
8315static tree
8316handle_alias_attribute (tree *node, tree name, tree args,
8317 int ARG_UNUSED (flags), bool *no_add_attrs)
8318{
8319 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8320}
8321
f4a30bd7 8322/* Handle a "weakref" attribute; arguments as in struct
8323 attribute_spec.handler. */
8324
8325static tree
8326handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8327 int flags, bool *no_add_attrs)
8328{
8329 tree attr = NULL_TREE;
8330
83852912 8331 /* We must ignore the attribute when it is associated with
8332 local-scoped decls, since attribute alias is ignored and many
8333 such symbols do not even have a DECL_WEAK field. */
64a7bd81 8334 if (decl_function_context (*node)
8335 || current_function_decl
b443c459 8336 || !VAR_OR_FUNCTION_DECL_P (*node))
83852912 8337 {
8338 warning (OPT_Wattributes, "%qE attribute ignored", name);
8339 *no_add_attrs = true;
8340 return NULL_TREE;
8341 }
8342
85c0a25c 8343 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8344 {
8345 error ("indirect function %q+D cannot be declared weakref", *node);
8346 *no_add_attrs = true;
8347 return NULL_TREE;
8348 }
8349
f4a30bd7 8350 /* The idea here is that `weakref("name")' mutates into `weakref,
8351 alias("name")', and weakref without arguments, in turn,
8352 implicitly adds weak. */
8353
8354 if (args)
8355 {
8356 attr = tree_cons (get_identifier ("alias"), args, attr);
8357 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8358
8359 *no_add_attrs = true;
0a3ecdc1 8360
8361 decl_attributes (node, attr, flags);
f4a30bd7 8362 }
8363 else
8364 {
8365 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
712d2297 8366 error_at (DECL_SOURCE_LOCATION (*node),
8367 "weakref attribute must appear before alias attribute");
f4a30bd7 8368
0a3ecdc1 8369 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8370 and that isn't supported; and because it wants to add it to
8371 the list of weak decls, which isn't helpful. */
8372 DECL_WEAK (*node) = 1;
f4a30bd7 8373 }
8374
8e857c41 8375 if (decl_in_symtab_p (*node))
8376 {
8377 struct symtab_node *n = symtab_node::get (*node);
8378 if (n && n->refuse_visibility_changes)
8379 error ("%+D declared weakref after being used", *node);
8380 }
8381
f4a30bd7 8382 return NULL_TREE;
8383}
8384
f8e93a2e 8385/* Handle an "visibility" attribute; arguments as in
8386 struct attribute_spec.handler. */
8387
8388static tree
1cae46be 8389handle_visibility_attribute (tree *node, tree name, tree args,
9a03a746 8390 int ARG_UNUSED (flags),
4a2849cb 8391 bool *ARG_UNUSED (no_add_attrs))
f8e93a2e 8392{
8393 tree decl = *node;
9c40570a 8394 tree id = TREE_VALUE (args);
4a2849cb 8395 enum symbol_visibility vis;
f8e93a2e 8396
b212f378 8397 if (TYPE_P (*node))
8398 {
4a2849cb 8399 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8400 /* OK */;
8401 else if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE)
8402 {
8403 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8404 name);
8405 return NULL_TREE;
8406 }
8407 else if (TYPE_FIELDS (*node))
8408 {
8409 error ("%qE attribute ignored because %qT is already defined",
8410 name, *node);
8411 return NULL_TREE;
8412 }
b212f378 8413 }
84166705 8414 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
f8e93a2e 8415 {
9b2d6d13 8416 warning (OPT_Wattributes, "%qE attribute ignored", name);
9c40570a 8417 return NULL_TREE;
f8e93a2e 8418 }
f8e93a2e 8419
9c40570a 8420 if (TREE_CODE (id) != STRING_CST)
8421 {
07e3a3d2 8422 error ("visibility argument not a string");
9c40570a 8423 return NULL_TREE;
f8e93a2e 8424 }
b27ac6b5 8425
b212f378 8426 /* If this is a type, set the visibility on the type decl. */
8427 if (TYPE_P (decl))
8428 {
8429 decl = TYPE_NAME (decl);
84166705 8430 if (!decl)
a0c938f0 8431 return NULL_TREE;
e147aab3 8432 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8433 {
9b2d6d13 8434 warning (OPT_Wattributes, "%qE attribute ignored on types",
e147aab3 8435 name);
8436 return NULL_TREE;
8437 }
b212f378 8438 }
f8e93a2e 8439
9c40570a 8440 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
4a2849cb 8441 vis = VISIBILITY_DEFAULT;
9c40570a 8442 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
4a2849cb 8443 vis = VISIBILITY_INTERNAL;
9c40570a 8444 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
4a2849cb 8445 vis = VISIBILITY_HIDDEN;
9c40570a 8446 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
4a2849cb 8447 vis = VISIBILITY_PROTECTED;
9c40570a 8448 else
4a2849cb 8449 {
8450 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8451 vis = VISIBILITY_DEFAULT;
8452 }
8453
8454 if (DECL_VISIBILITY_SPECIFIED (decl)
098a01e7 8455 && vis != DECL_VISIBILITY (decl))
8456 {
8457 tree attributes = (TYPE_P (*node)
8458 ? TYPE_ATTRIBUTES (*node)
8459 : DECL_ATTRIBUTES (decl));
8460 if (lookup_attribute ("visibility", attributes))
8461 error ("%qD redeclared with different visibility", decl);
8462 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8463 && lookup_attribute ("dllimport", attributes))
8464 error ("%qD was declared %qs which implies default visibility",
8465 decl, "dllimport");
8466 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8467 && lookup_attribute ("dllexport", attributes))
8468 error ("%qD was declared %qs which implies default visibility",
8469 decl, "dllexport");
8470 }
4a2849cb 8471
8472 DECL_VISIBILITY (decl) = vis;
b212f378 8473 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8474
4a2849cb 8475 /* Go ahead and attach the attribute to the node as well. This is needed
8476 so we can determine whether we have VISIBILITY_DEFAULT because the
8477 visibility was not specified, or because it was explicitly overridden
8478 from the containing scope. */
9c40570a 8479
f8e93a2e 8480 return NULL_TREE;
8481}
8482
3aa0c315 8483/* Determine the ELF symbol visibility for DECL, which is either a
8484 variable or a function. It is an error to use this function if a
8485 definition of DECL is not available in this translation unit.
8486 Returns true if the final visibility has been determined by this
8487 function; false if the caller is free to make additional
8488 modifications. */
8489
8490bool
8491c_determine_visibility (tree decl)
8492{
b443c459 8493 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
3aa0c315 8494
8495 /* If the user explicitly specified the visibility with an
8496 attribute, honor that. DECL_VISIBILITY will have been set during
920f5a70 8497 the processing of the attribute. We check for an explicit
8498 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8499 to distinguish the use of an attribute from the use of a "#pragma
8500 GCC visibility push(...)"; in the latter case we still want other
8501 considerations to be able to overrule the #pragma. */
8502 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8503 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8504 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8505 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
3aa0c315 8506 return true;
8507
4a2849cb 8508 /* Set default visibility to whatever the user supplied with
8509 visibility_specified depending on #pragma GCC visibility. */
8510 if (!DECL_VISIBILITY_SPECIFIED (decl))
8511 {
2d9d8740 8512 if (visibility_options.inpragma
8513 || DECL_VISIBILITY (decl) != default_visibility)
8514 {
8515 DECL_VISIBILITY (decl) = default_visibility;
8516 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8517 /* If visibility changed and DECL already has DECL_RTL, ensure
8518 symbol flags are updated. */
f48c7f4a 8519 if (((VAR_P (decl) && TREE_STATIC (decl))
2d9d8740 8520 || TREE_CODE (decl) == FUNCTION_DECL)
8521 && DECL_RTL_SET_P (decl))
8522 make_decl_rtl (decl);
8523 }
4a2849cb 8524 }
3aa0c315 8525 return false;
8526}
8527
24dfead4 8528/* Handle an "tls_model" attribute; arguments as in
8529 struct attribute_spec.handler. */
8530
8531static tree
1cae46be 8532handle_tls_model_attribute (tree *node, tree name, tree args,
9a03a746 8533 int ARG_UNUSED (flags), bool *no_add_attrs)
24dfead4 8534{
1b53eb20 8535 tree id;
24dfead4 8536 tree decl = *node;
1b53eb20 8537 enum tls_model kind;
24dfead4 8538
1b53eb20 8539 *no_add_attrs = true;
8540
f48c7f4a 8541 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
24dfead4 8542 {
9b2d6d13 8543 warning (OPT_Wattributes, "%qE attribute ignored", name);
1b53eb20 8544 return NULL_TREE;
24dfead4 8545 }
24dfead4 8546
1b53eb20 8547 kind = DECL_TLS_MODEL (decl);
8548 id = TREE_VALUE (args);
8549 if (TREE_CODE (id) != STRING_CST)
8550 {
8551 error ("tls_model argument not a string");
8552 return NULL_TREE;
24dfead4 8553 }
8554
1b53eb20 8555 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8556 kind = TLS_MODEL_LOCAL_EXEC;
8557 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8558 kind = TLS_MODEL_INITIAL_EXEC;
8559 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8560 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8561 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8562 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8563 else
8564 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8565
5e68df57 8566 set_decl_tls_model (decl, kind);
24dfead4 8567 return NULL_TREE;
8568}
8569
f8e93a2e 8570/* Handle a "no_instrument_function" attribute; arguments as in
8571 struct attribute_spec.handler. */
8572
8573static tree
1cae46be 8574handle_no_instrument_function_attribute (tree *node, tree name,
9a03a746 8575 tree ARG_UNUSED (args),
8576 int ARG_UNUSED (flags),
09347743 8577 bool *no_add_attrs)
f8e93a2e 8578{
8579 tree decl = *node;
8580
8581 if (TREE_CODE (decl) != FUNCTION_DECL)
8582 {
712d2297 8583 error_at (DECL_SOURCE_LOCATION (decl),
8584 "%qE attribute applies only to functions", name);
f8e93a2e 8585 *no_add_attrs = true;
8586 }
f8e93a2e 8587 else
8588 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8589
8590 return NULL_TREE;
8591}
8592
8593/* Handle a "malloc" attribute; arguments as in
8594 struct attribute_spec.handler. */
8595
8596static tree
9a03a746 8597handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8598 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8599{
a5147fca 8600 if (TREE_CODE (*node) == FUNCTION_DECL
8601 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
f8e93a2e 8602 DECL_IS_MALLOC (*node) = 1;
f8e93a2e 8603 else
8604 {
9b2d6d13 8605 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8606 *no_add_attrs = true;
8607 }
8608
8609 return NULL_TREE;
8610}
8611
4a29c97c 8612/* Handle a "alloc_size" attribute; arguments as in
8613 struct attribute_spec.handler. */
8614
8615static tree
8616handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8617 int ARG_UNUSED (flags), bool *no_add_attrs)
8618{
2802826e 8619 unsigned arg_count = type_num_arguments (*node);
4a29c97c 8620 for (; args; args = TREE_CHAIN (args))
8621 {
8622 tree position = TREE_VALUE (args);
caf62483 8623 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8624 && TREE_CODE (position) != FUNCTION_DECL)
8625 position = default_conversion (position);
4a29c97c 8626
237e78b1 8627 if (!tree_fits_uhwi_p (position)
8628 || !arg_count
8629 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
4a29c97c 8630 {
48e1416a 8631 warning (OPT_Wattributes,
4a29c97c 8632 "alloc_size parameter outside range");
8633 *no_add_attrs = true;
8634 return NULL_TREE;
8635 }
8636 }
8637 return NULL_TREE;
8638}
8639
237e78b1 8640/* Handle a "alloc_align" attribute; arguments as in
8641 struct attribute_spec.handler. */
8642
8643static tree
8644handle_alloc_align_attribute (tree *node, tree, tree args, int,
8645 bool *no_add_attrs)
8646{
8647 unsigned arg_count = type_num_arguments (*node);
8648 tree position = TREE_VALUE (args);
8649 if (position && TREE_CODE (position) != IDENTIFIER_NODE)
8650 position = default_conversion (position);
8651
8652 if (!tree_fits_uhwi_p (position)
8653 || !arg_count
8654 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8655 {
8656 warning (OPT_Wattributes,
8657 "alloc_align parameter outside range");
8658 *no_add_attrs = true;
8659 return NULL_TREE;
8660 }
8661 return NULL_TREE;
8662}
8663
8664/* Handle a "assume_aligned" attribute; arguments as in
8665 struct attribute_spec.handler. */
8666
8667static tree
8668handle_assume_aligned_attribute (tree *, tree, tree args, int,
8669 bool *no_add_attrs)
8670{
8671 for (; args; args = TREE_CHAIN (args))
8672 {
8673 tree position = TREE_VALUE (args);
8674 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8675 && TREE_CODE (position) != FUNCTION_DECL)
8676 position = default_conversion (position);
8677
8678 if (TREE_CODE (position) != INTEGER_CST)
8679 {
8680 warning (OPT_Wattributes,
8681 "assume_aligned parameter not integer constant");
8682 *no_add_attrs = true;
8683 return NULL_TREE;
8684 }
8685 }
8686 return NULL_TREE;
8687}
8688
8ce86007 8689/* Handle a "fn spec" attribute; arguments as in
8690 struct attribute_spec.handler. */
8691
8692static tree
8693handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8694 tree args, int ARG_UNUSED (flags),
8695 bool *no_add_attrs ATTRIBUTE_UNUSED)
8696{
8697 gcc_assert (args
8698 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8699 && !TREE_CHAIN (args));
8700 return NULL_TREE;
8701}
8702
058a1b7a 8703/* Handle a "bnd_variable_size" attribute; arguments as in
8704 struct attribute_spec.handler. */
8705
8706static tree
8707handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8708 int ARG_UNUSED (flags), bool *no_add_attrs)
8709{
8710 if (TREE_CODE (*node) != FIELD_DECL)
8711 {
8712 warning (OPT_Wattributes, "%qE attribute ignored", name);
8713 *no_add_attrs = true;
8714 }
8715
8716 return NULL_TREE;
8717}
8718
8719/* Handle a "bnd_legacy" attribute; arguments as in
8720 struct attribute_spec.handler. */
8721
8722static tree
8723handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8724 int ARG_UNUSED (flags), bool *no_add_attrs)
8725{
8726 if (TREE_CODE (*node) != FUNCTION_DECL)
8727 {
8728 warning (OPT_Wattributes, "%qE attribute ignored", name);
8729 *no_add_attrs = true;
8730 }
8731
8732 return NULL_TREE;
8733}
8734
8735/* Handle a "bnd_instrument" attribute; arguments as in
8736 struct attribute_spec.handler. */
8737
8738static tree
8739handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8740 int ARG_UNUSED (flags), bool *no_add_attrs)
8741{
8742 if (TREE_CODE (*node) != FUNCTION_DECL)
8743 {
8744 warning (OPT_Wattributes, "%qE attribute ignored", name);
8745 *no_add_attrs = true;
8746 }
8747
8748 return NULL_TREE;
8749}
8750
a96c3cc1 8751/* Handle a "warn_unused" attribute; arguments as in
8752 struct attribute_spec.handler. */
8753
8754static tree
8755handle_warn_unused_attribute (tree *node, tree name,
8756 tree args ATTRIBUTE_UNUSED,
8757 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8758{
8759 if (TYPE_P (*node))
8760 /* Do nothing else, just set the attribute. We'll get at
8761 it later with lookup_attribute. */
8762 ;
8763 else
8764 {
8765 warning (OPT_Wattributes, "%qE attribute ignored", name);
8766 *no_add_attrs = true;
8767 }
8768
8769 return NULL_TREE;
8770}
8771
bc7bff74 8772/* Handle an "omp declare simd" attribute; arguments as in
8773 struct attribute_spec.handler. */
8774
8775static tree
8776handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8777{
8778 return NULL_TREE;
8779}
8780
8781/* Handle an "omp declare target" attribute; arguments as in
8782 struct attribute_spec.handler. */
8783
8784static tree
8785handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8786{
8787 return NULL_TREE;
8788}
8789
26d1c5ff 8790/* Handle a "returns_twice" attribute; arguments as in
8791 struct attribute_spec.handler. */
8792
8793static tree
8794handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8795 int ARG_UNUSED (flags), bool *no_add_attrs)
8796{
8797 if (TREE_CODE (*node) == FUNCTION_DECL)
8798 DECL_IS_RETURNS_TWICE (*node) = 1;
8799 else
8800 {
9b2d6d13 8801 warning (OPT_Wattributes, "%qE attribute ignored", name);
26d1c5ff 8802 *no_add_attrs = true;
8803 }
8804
8805 return NULL_TREE;
8806}
8807
f8e93a2e 8808/* Handle a "no_limit_stack" attribute; arguments as in
8809 struct attribute_spec.handler. */
8810
8811static tree
1cae46be 8812handle_no_limit_stack_attribute (tree *node, tree name,
9a03a746 8813 tree ARG_UNUSED (args),
8814 int ARG_UNUSED (flags),
09347743 8815 bool *no_add_attrs)
f8e93a2e 8816{
8817 tree decl = *node;
8818
8819 if (TREE_CODE (decl) != FUNCTION_DECL)
8820 {
712d2297 8821 error_at (DECL_SOURCE_LOCATION (decl),
8822 "%qE attribute applies only to functions", name);
f8e93a2e 8823 *no_add_attrs = true;
8824 }
8825 else if (DECL_INITIAL (decl))
8826 {
712d2297 8827 error_at (DECL_SOURCE_LOCATION (decl),
8828 "can%'t set %qE attribute after definition", name);
f8e93a2e 8829 *no_add_attrs = true;
8830 }
8831 else
8832 DECL_NO_LIMIT_STACK (decl) = 1;
8833
8834 return NULL_TREE;
8835}
8836
8837/* Handle a "pure" attribute; arguments as in
8838 struct attribute_spec.handler. */
8839
8840static tree
9a03a746 8841handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8842 int ARG_UNUSED (flags), bool *no_add_attrs)
f8e93a2e 8843{
8844 if (TREE_CODE (*node) == FUNCTION_DECL)
9c2a0c05 8845 DECL_PURE_P (*node) = 1;
f8e93a2e 8846 /* ??? TODO: Support types. */
8847 else
8848 {
9b2d6d13 8849 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 8850 *no_add_attrs = true;
8851 }
8852
8853 return NULL_TREE;
8854}
8855
4c0315d0 8856/* Digest an attribute list destined for a transactional memory statement.
8857 ALLOWED is the set of attributes that are allowed for this statement;
8858 return the attribute we parsed. Multiple attributes are never allowed. */
8859
8860int
8861parse_tm_stmt_attr (tree attrs, int allowed)
8862{
8863 tree a_seen = NULL;
8864 int m_seen = 0;
8865
8866 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8867 {
8868 tree a = TREE_PURPOSE (attrs);
8869 int m = 0;
8870
8871 if (is_attribute_p ("outer", a))
8872 m = TM_STMT_ATTR_OUTER;
8873
8874 if ((m & allowed) == 0)
8875 {
8876 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8877 continue;
8878 }
8879
8880 if (m_seen == 0)
8881 {
8882 a_seen = a;
8883 m_seen = m;
8884 }
8885 else if (m_seen == m)
8886 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8887 else
8888 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8889 }
8890
8891 return m_seen;
8892}
8893
8894/* Transform a TM attribute name into a maskable integer and back.
8895 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8896 to how the lack of an attribute is treated. */
8897
8898int
8899tm_attr_to_mask (tree attr)
8900{
8901 if (attr == NULL)
8902 return 0;
8903 if (is_attribute_p ("transaction_safe", attr))
8904 return TM_ATTR_SAFE;
8905 if (is_attribute_p ("transaction_callable", attr))
8906 return TM_ATTR_CALLABLE;
8907 if (is_attribute_p ("transaction_pure", attr))
8908 return TM_ATTR_PURE;
8909 if (is_attribute_p ("transaction_unsafe", attr))
8910 return TM_ATTR_IRREVOCABLE;
8911 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8912 return TM_ATTR_MAY_CANCEL_OUTER;
8913 return 0;
8914}
8915
8916tree
8917tm_mask_to_attr (int mask)
8918{
8919 const char *str;
8920 switch (mask)
8921 {
8922 case TM_ATTR_SAFE:
8923 str = "transaction_safe";
8924 break;
8925 case TM_ATTR_CALLABLE:
8926 str = "transaction_callable";
8927 break;
8928 case TM_ATTR_PURE:
8929 str = "transaction_pure";
8930 break;
8931 case TM_ATTR_IRREVOCABLE:
8932 str = "transaction_unsafe";
8933 break;
8934 case TM_ATTR_MAY_CANCEL_OUTER:
8935 str = "transaction_may_cancel_outer";
8936 break;
8937 default:
8938 gcc_unreachable ();
8939 }
8940 return get_identifier (str);
8941}
8942
8943/* Return the first TM attribute seen in LIST. */
8944
8945tree
8946find_tm_attribute (tree list)
8947{
8948 for (; list ; list = TREE_CHAIN (list))
8949 {
8950 tree name = TREE_PURPOSE (list);
8951 if (tm_attr_to_mask (name) != 0)
8952 return name;
8953 }
8954 return NULL_TREE;
8955}
8956
8957/* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8958 Here we accept only function types, and verify that none of the other
8959 function TM attributes are also applied. */
8960/* ??? We need to accept class types for C++, but not C. This greatly
8961 complicates this function, since we can no longer rely on the extra
8962 processing given by function_type_required. */
8963
8964static tree
8965handle_tm_attribute (tree *node, tree name, tree args,
8966 int flags, bool *no_add_attrs)
8967{
8968 /* Only one path adds the attribute; others don't. */
8969 *no_add_attrs = true;
8970
8971 switch (TREE_CODE (*node))
8972 {
8973 case RECORD_TYPE:
8974 case UNION_TYPE:
8975 /* Only tm_callable and tm_safe apply to classes. */
8976 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8977 goto ignored;
8978 /* FALLTHRU */
8979
8980 case FUNCTION_TYPE:
8981 case METHOD_TYPE:
8982 {
8983 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8984 if (old_name == name)
8985 ;
8986 else if (old_name != NULL_TREE)
8987 error ("type was previously declared %qE", old_name);
8988 else
8989 *no_add_attrs = false;
8990 }
8991 break;
8992
8993 case POINTER_TYPE:
8994 {
8995 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8996 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8997 {
8998 tree fn_tmp = TREE_TYPE (*node);
8999 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
9000 *node = build_pointer_type (fn_tmp);
9001 break;
9002 }
9003 }
9004 /* FALLTHRU */
9005
9006 default:
9007 /* If a function is next, pass it on to be tried next. */
9008 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
9009 return tree_cons (name, args, NULL);
9010
9011 ignored:
9012 warning (OPT_Wattributes, "%qE attribute ignored", name);
9013 break;
9014 }
9015
9016 return NULL_TREE;
9017}
9018
9019/* Handle the TM_WRAP attribute; arguments as in
9020 struct attribute_spec.handler. */
9021
9022static tree
9023handle_tm_wrap_attribute (tree *node, tree name, tree args,
9024 int ARG_UNUSED (flags), bool *no_add_attrs)
9025{
9026 tree decl = *node;
9027
9028 /* We don't need the attribute even on success, since we
9029 record the entry in an external table. */
9030 *no_add_attrs = true;
9031
9032 if (TREE_CODE (decl) != FUNCTION_DECL)
9033 warning (OPT_Wattributes, "%qE attribute ignored", name);
9034 else
9035 {
9036 tree wrap_decl = TREE_VALUE (args);
3e5a8b00 9037 if (error_operand_p (wrap_decl))
9038 ;
9039 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
b443c459 9040 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
4c0315d0 9041 error ("%qE argument not an identifier", name);
9042 else
9043 {
9044 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
9045 wrap_decl = lookup_name (wrap_decl);
9046 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
9047 {
9048 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
9049 TREE_TYPE (wrap_decl)))
9050 record_tm_replacement (wrap_decl, decl);
9051 else
9052 error ("%qD is not compatible with %qD", wrap_decl, decl);
9053 }
9054 else
cdf34fca 9055 error ("%qE argument is not a function", name);
4c0315d0 9056 }
9057 }
9058
9059 return NULL_TREE;
9060}
9061
9062/* Ignore the given attribute. Used when this attribute may be usefully
9063 overridden by the target, but is not used generically. */
9064
9065static tree
9066ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
9067 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9068 bool *no_add_attrs)
9069{
9070 *no_add_attrs = true;
9071 return NULL_TREE;
9072}
9073
fc09b200 9074/* Handle a "no vops" attribute; arguments as in
9075 struct attribute_spec.handler. */
9076
9077static tree
9078handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
9079 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9080 bool *ARG_UNUSED (no_add_attrs))
9081{
9082 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
9083 DECL_IS_NOVOPS (*node) = 1;
9084 return NULL_TREE;
9085}
9086
f8e93a2e 9087/* Handle a "deprecated" attribute; arguments as in
9088 struct attribute_spec.handler. */
1cae46be 9089
f8e93a2e 9090static tree
1cae46be 9091handle_deprecated_attribute (tree *node, tree name,
45c4e798 9092 tree args, int flags,
09347743 9093 bool *no_add_attrs)
f8e93a2e 9094{
9095 tree type = NULL_TREE;
9096 int warn = 0;
782858b8 9097 tree what = NULL_TREE;
1cae46be 9098
45c4e798 9099 if (!args)
9100 *no_add_attrs = true;
9101 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
9102 {
9103 error ("deprecated message is not a string");
9104 *no_add_attrs = true;
9105 }
9106
f8e93a2e 9107 if (DECL_P (*node))
9108 {
9109 tree decl = *node;
9110 type = TREE_TYPE (decl);
1cae46be 9111
f8e93a2e 9112 if (TREE_CODE (decl) == TYPE_DECL
9113 || TREE_CODE (decl) == PARM_DECL
b443c459 9114 || VAR_OR_FUNCTION_DECL_P (decl)
40c8d1dd 9115 || TREE_CODE (decl) == FIELD_DECL
5a4c69dd 9116 || TREE_CODE (decl) == CONST_DECL
40c8d1dd 9117 || objc_method_decl (TREE_CODE (decl)))
f8e93a2e 9118 TREE_DEPRECATED (decl) = 1;
9119 else
9120 warn = 1;
9121 }
9122 else if (TYPE_P (*node))
9123 {
9124 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
e086912e 9125 *node = build_variant_type_copy (*node);
f8e93a2e 9126 TREE_DEPRECATED (*node) = 1;
9127 type = *node;
9128 }
9129 else
9130 warn = 1;
1cae46be 9131
f8e93a2e 9132 if (warn)
9133 {
9134 *no_add_attrs = true;
9135 if (type && TYPE_NAME (type))
9136 {
9137 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
782858b8 9138 what = TYPE_NAME (*node);
f8e93a2e 9139 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9140 && DECL_NAME (TYPE_NAME (type)))
782858b8 9141 what = DECL_NAME (TYPE_NAME (type));
f8e93a2e 9142 }
9143 if (what)
9b2d6d13 9144 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
f8e93a2e 9145 else
9b2d6d13 9146 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 9147 }
9148
9149 return NULL_TREE;
9150}
9151
f8e93a2e 9152/* Handle a "vector_size" attribute; arguments as in
9153 struct attribute_spec.handler. */
9154
9155static tree
1cae46be 9156handle_vector_size_attribute (tree *node, tree name, tree args,
9a03a746 9157 int ARG_UNUSED (flags),
09347743 9158 bool *no_add_attrs)
f8e93a2e 9159{
9160 unsigned HOST_WIDE_INT vecsize, nunits;
3754d046 9161 machine_mode orig_mode;
4917c376 9162 tree type = *node, new_type, size;
f8e93a2e 9163
9164 *no_add_attrs = true;
9165
4917c376 9166 size = TREE_VALUE (args);
3e5a8b00 9167 if (size && TREE_CODE (size) != IDENTIFIER_NODE
9168 && TREE_CODE (size) != FUNCTION_DECL)
caf62483 9169 size = default_conversion (size);
4917c376 9170
e913b5cd 9171 if (!tree_fits_uhwi_p (size))
f8e93a2e 9172 {
9b2d6d13 9173 warning (OPT_Wattributes, "%qE attribute ignored", name);
f8e93a2e 9174 return NULL_TREE;
9175 }
9176
9177 /* Get the vector size (in bytes). */
e913b5cd 9178 vecsize = tree_to_uhwi (size);
f8e93a2e 9179
9180 /* We need to provide for vector pointers, vector arrays, and
9181 functions returning vectors. For example:
9182
9183 __attribute__((vector_size(16))) short *foo;
9184
9185 In this case, the mode is SI, but the type being modified is
9186 HI, so we need to look further. */
9187
9188 while (POINTER_TYPE_P (type)
9189 || TREE_CODE (type) == FUNCTION_TYPE
5bfb0742 9190 || TREE_CODE (type) == METHOD_TYPE
2cb0e5d9 9191 || TREE_CODE (type) == ARRAY_TYPE
9192 || TREE_CODE (type) == OFFSET_TYPE)
f8e93a2e 9193 type = TREE_TYPE (type);
9194
9195 /* Get the mode of the type being modified. */
9196 orig_mode = TYPE_MODE (type);
9197
2cb0e5d9 9198 if ((!INTEGRAL_TYPE_P (type)
9199 && !SCALAR_FLOAT_TYPE_P (type)
9200 && !FIXED_POINT_TYPE_P (type))
cee7491d 9201 || (!SCALAR_FLOAT_MODE_P (orig_mode)
9421ebb9 9202 && GET_MODE_CLASS (orig_mode) != MODE_INT
9203 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
e913b5cd 9204 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
7ec31215 9205 || TREE_CODE (type) == BOOLEAN_TYPE)
f8e93a2e 9206 {
782858b8 9207 error ("invalid vector type for attribute %qE", name);
f8e93a2e 9208 return NULL_TREE;
9209 }
9210
e913b5cd 9211 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
39cc3e6d 9212 {
9213 error ("vector size not an integral multiple of component size");
9214 return NULL;
9215 }
9216
9217 if (vecsize == 0)
9218 {
9219 error ("zero vector size");
9220 return NULL;
9221 }
9222
f8e93a2e 9223 /* Calculate how many units fit in the vector. */
e913b5cd 9224 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
83e2a11b 9225 if (nunits & (nunits - 1))
f8e93a2e 9226 {
83e2a11b 9227 error ("number of components of the vector not a power of two");
f8e93a2e 9228 return NULL_TREE;
9229 }
9230
83e2a11b 9231 new_type = build_vector_type (type, nunits);
f8e93a2e 9232
9233 /* Build back pointers if needed. */
d991e6e8 9234 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
f8e93a2e 9235
9236 return NULL_TREE;
9237}
9238
dbf6c367 9239/* Handle the "nonnull" attribute. */
9240static tree
9a03a746 9241handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9242 tree args, int ARG_UNUSED (flags),
09347743 9243 bool *no_add_attrs)
dbf6c367 9244{
9245 tree type = *node;
9246 unsigned HOST_WIDE_INT attr_arg_num;
9247
9248 /* If no arguments are specified, all pointer arguments should be
d716ce75 9249 non-null. Verify a full prototype is given so that the arguments
dbf6c367 9250 will have the correct types when we actually check them later. */
84166705 9251 if (!args)
dbf6c367 9252 {
a36cf284 9253 if (!prototype_p (type))
dbf6c367 9254 {
9255 error ("nonnull attribute without arguments on a non-prototype");
4ee9c684 9256 *no_add_attrs = true;
dbf6c367 9257 }
9258 return NULL_TREE;
9259 }
9260
9261 /* Argument list specified. Verify that each argument number references
9262 a pointer argument. */
caf62483 9263 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
dbf6c367 9264 {
4ee9c684 9265 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
dbf6c367 9266
caf62483 9267 tree arg = TREE_VALUE (args);
9268 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9269 && TREE_CODE (arg) != FUNCTION_DECL)
9270 arg = default_conversion (arg);
9271
9272 if (!get_nonnull_operand (arg, &arg_num))
dbf6c367 9273 {
07e3a3d2 9274 error ("nonnull argument has invalid operand number (argument %lu)",
dbf6c367 9275 (unsigned long) attr_arg_num);
9276 *no_add_attrs = true;
9277 return NULL_TREE;
9278 }
9279
d0af78c5 9280 if (prototype_p (type))
dbf6c367 9281 {
d0af78c5 9282 function_args_iterator iter;
9283 tree argument;
9284
9285 function_args_iter_init (&iter, type);
9286 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
dbf6c367 9287 {
d0af78c5 9288 argument = function_args_iter_cond (&iter);
9289 if (argument == NULL_TREE || ck_num == arg_num)
dbf6c367 9290 break;
dbf6c367 9291 }
9292
84166705 9293 if (!argument
d0af78c5 9294 || TREE_CODE (argument) == VOID_TYPE)
dbf6c367 9295 {
07e3a3d2 9296 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
dbf6c367 9297 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9298 *no_add_attrs = true;
9299 return NULL_TREE;
9300 }
9301
d0af78c5 9302 if (TREE_CODE (argument) != POINTER_TYPE)
dbf6c367 9303 {
07e3a3d2 9304 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
dbf6c367 9305 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9306 *no_add_attrs = true;
9307 return NULL_TREE;
9308 }
9309 }
9310 }
9311
9312 return NULL_TREE;
9313}
9314
9315/* Check the argument list of a function call for null in argument slots
d01f58f9 9316 that are marked as requiring a non-null pointer argument. The NARGS
9317 arguments are passed in the array ARGARRAY.
9318*/
dbf6c367 9319
9320static void
d01f58f9 9321check_function_nonnull (tree attrs, int nargs, tree *argarray)
dbf6c367 9322{
9ca77b08 9323 tree a;
d01f58f9 9324 int i;
dbf6c367 9325
9ca77b08 9326 attrs = lookup_attribute ("nonnull", attrs);
9327 if (attrs == NULL_TREE)
9328 return;
9329
9330 a = attrs;
9331 /* See if any of the nonnull attributes has no arguments. If so,
9332 then every pointer argument is checked (in which case the check
9333 for pointer type is done in check_nonnull_arg). */
9334 if (TREE_VALUE (a) != NULL_TREE)
9335 do
9336 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9337 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9338
9339 if (a != NULL_TREE)
9340 for (i = 0; i < nargs; i++)
9341 check_function_arguments_recurse (check_nonnull_arg, NULL, argarray[i],
9342 i + 1);
9343 else
dbf6c367 9344 {
9ca77b08 9345 /* Walk the argument list. If we encounter an argument number we
9346 should check for non-null, do it. */
9347 for (i = 0; i < nargs; i++)
dbf6c367 9348 {
9ca77b08 9349 for (a = attrs; ; a = TREE_CHAIN (a))
4ee9c684 9350 {
9ca77b08 9351 a = lookup_attribute ("nonnull", a);
9352 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9353 break;
4ee9c684 9354 }
9ca77b08 9355
9356 if (a != NULL_TREE)
9357 check_function_arguments_recurse (check_nonnull_arg, NULL,
9358 argarray[i], i + 1);
dbf6c367 9359 }
9360 }
9361}
9362
50ca527f 9363/* Check that the Nth argument of a function call (counting backwards
d01f58f9 9364 from the end) is a (pointer)0. The NARGS arguments are passed in the
9365 array ARGARRAY. */
bf6c8de0 9366
9367static void
774e9d58 9368check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
bf6c8de0 9369{
774e9d58 9370 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
bf6c8de0 9371
9372 if (attr)
9373 {
d01f58f9 9374 int len = 0;
9375 int pos = 0;
9376 tree sentinel;
774e9d58 9377 function_args_iterator iter;
9378 tree t;
a0c938f0 9379
d01f58f9 9380 /* Skip over the named arguments. */
774e9d58 9381 FOREACH_FUNCTION_ARGS (fntype, t, iter)
a0c938f0 9382 {
774e9d58 9383 if (len == nargs)
9384 break;
d01f58f9 9385 len++;
9386 }
50ca527f 9387
d01f58f9 9388 if (TREE_VALUE (attr))
9389 {
9390 tree p = TREE_VALUE (TREE_VALUE (attr));
f9ae6f95 9391 pos = TREE_INT_CST_LOW (p);
d01f58f9 9392 }
50ca527f 9393
d01f58f9 9394 /* The sentinel must be one of the varargs, i.e.
9395 in position >= the number of fixed arguments. */
9396 if ((nargs - 1 - pos) < len)
9397 {
77a357e3 9398 warning (OPT_Wformat_,
d01f58f9 9399 "not enough variable arguments to fit a sentinel");
9400 return;
bf6c8de0 9401 }
d01f58f9 9402
9403 /* Validate the sentinel. */
9404 sentinel = argarray[nargs - 1 - pos];
9405 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9406 || !integer_zerop (sentinel))
9407 /* Although __null (in C++) is only an integer we allow it
9408 nevertheless, as we are guaranteed that it's exactly
9409 as wide as a pointer, and we don't want to force
9410 users to cast the NULL they have written there.
9411 We warn with -Wstrict-null-sentinel, though. */
9412 && (warn_strict_null_sentinel || null_node != sentinel))
77a357e3 9413 warning (OPT_Wformat_, "missing sentinel in function call");
bf6c8de0 9414 }
9415}
9416
dbf6c367 9417/* Helper for check_function_nonnull; given a list of operands which
9418 must be non-null in ARGS, determine if operand PARAM_NUM should be
9419 checked. */
9420
9421static bool
1cae46be 9422nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
dbf6c367 9423{
4ee9c684 9424 unsigned HOST_WIDE_INT arg_num = 0;
dbf6c367 9425
9426 for (; args; args = TREE_CHAIN (args))
9427 {
231bd014 9428 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9429
9430 gcc_assert (found);
dbf6c367 9431
9432 if (arg_num == param_num)
9433 return true;
9434 }
9435 return false;
9436}
9437
9438/* Check that the function argument PARAM (which is operand number
9439 PARAM_NUM) is non-null. This is called by check_function_nonnull
9440 via check_function_arguments_recurse. */
9441
9442static void
9a03a746 9443check_nonnull_arg (void * ARG_UNUSED (ctx), tree param,
1cae46be 9444 unsigned HOST_WIDE_INT param_num)
dbf6c367 9445{
9446 /* Just skip checking the argument if it's not a pointer. This can
9447 happen if the "nonnull" attribute was given without an operand
9448 list (which means to check every pointer argument). */
9449
9450 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9451 return;
9452
9453 if (integer_zerop (param))
155b601b 9454 warning (OPT_Wnonnull, "null argument where non-null required "
9455 "(argument %lu)", (unsigned long) param_num);
dbf6c367 9456}
9457
9458/* Helper for nonnull attribute handling; fetch the operand number
9459 from the attribute argument list. */
9460
9461static bool
1cae46be 9462get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
dbf6c367 9463{
e913b5cd 9464 /* Verify the arg number is a small constant. */
e1d65c9f 9465 if (tree_fits_uhwi_p (arg_num_expr))
e913b5cd 9466 {
f9ae6f95 9467 *valp = TREE_INT_CST_LOW (arg_num_expr);
e913b5cd 9468 return true;
9469 }
9470 else
dbf6c367 9471 return false;
dbf6c367 9472}
fa987697 9473
9474/* Handle a "nothrow" attribute; arguments as in
9475 struct attribute_spec.handler. */
9476
9477static tree
9a03a746 9478handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9479 int ARG_UNUSED (flags), bool *no_add_attrs)
fa987697 9480{
9481 if (TREE_CODE (*node) == FUNCTION_DECL)
9482 TREE_NOTHROW (*node) = 1;
9483 /* ??? TODO: Support types. */
9484 else
9485 {
9b2d6d13 9486 warning (OPT_Wattributes, "%qE attribute ignored", name);
fa987697 9487 *no_add_attrs = true;
9488 }
9489
9490 return NULL_TREE;
9491}
7acb29a3 9492
9493/* Handle a "cleanup" attribute; arguments as in
9494 struct attribute_spec.handler. */
9495
9496static tree
1cae46be 9497handle_cleanup_attribute (tree *node, tree name, tree args,
9a03a746 9498 int ARG_UNUSED (flags), bool *no_add_attrs)
7acb29a3 9499{
9500 tree decl = *node;
9501 tree cleanup_id, cleanup_decl;
9502
9503 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9504 for global destructors in C++. This requires infrastructure that
9505 we don't have generically at the moment. It's also not a feature
9506 we'd be missing too much, since we do have attribute constructor. */
f48c7f4a 9507 if (!VAR_P (decl) || TREE_STATIC (decl))
7acb29a3 9508 {
9b2d6d13 9509 warning (OPT_Wattributes, "%qE attribute ignored", name);
7acb29a3 9510 *no_add_attrs = true;
9511 return NULL_TREE;
9512 }
9513
9514 /* Verify that the argument is a function in scope. */
9515 /* ??? We could support pointers to functions here as well, if
9516 that was considered desirable. */
9517 cleanup_id = TREE_VALUE (args);
9518 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9519 {
07e3a3d2 9520 error ("cleanup argument not an identifier");
7acb29a3 9521 *no_add_attrs = true;
9522 return NULL_TREE;
9523 }
d1c41717 9524 cleanup_decl = lookup_name (cleanup_id);
7acb29a3 9525 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9526 {
07e3a3d2 9527 error ("cleanup argument not a function");
7acb29a3 9528 *no_add_attrs = true;
9529 return NULL_TREE;
9530 }
9531
1cae46be 9532 /* That the function has proper type is checked with the
7acb29a3 9533 eventual call to build_function_call. */
9534
9535 return NULL_TREE;
9536}
8a8cdb8d 9537
9538/* Handle a "warn_unused_result" attribute. No special handling. */
9539
9540static tree
9541handle_warn_unused_result_attribute (tree *node, tree name,
9a03a746 9542 tree ARG_UNUSED (args),
9543 int ARG_UNUSED (flags), bool *no_add_attrs)
8a8cdb8d 9544{
9545 /* Ignore the attribute for functions not returning any value. */
9546 if (VOID_TYPE_P (TREE_TYPE (*node)))
9547 {
9b2d6d13 9548 warning (OPT_Wattributes, "%qE attribute ignored", name);
8a8cdb8d 9549 *no_add_attrs = true;
9550 }
9551
9552 return NULL_TREE;
9553}
bf6c8de0 9554
9555/* Handle a "sentinel" attribute. */
9556
9557static tree
50ca527f 9558handle_sentinel_attribute (tree *node, tree name, tree args,
bf6c8de0 9559 int ARG_UNUSED (flags), bool *no_add_attrs)
9560{
a36cf284 9561 if (!prototype_p (*node))
bf6c8de0 9562 {
9b2d6d13 9563 warning (OPT_Wattributes,
9564 "%qE attribute requires prototypes with named arguments", name);
bf6c8de0 9565 *no_add_attrs = true;
bf6c8de0 9566 }
50ca527f 9567 else
9568 {
c33080b9 9569 if (!stdarg_p (*node))
a0c938f0 9570 {
9b2d6d13 9571 warning (OPT_Wattributes,
9572 "%qE attribute only applies to variadic functions", name);
50ca527f 9573 *no_add_attrs = true;
9574 }
9575 }
a0c938f0 9576
50ca527f 9577 if (args)
bf6c8de0 9578 {
50ca527f 9579 tree position = TREE_VALUE (args);
3e5a8b00 9580 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9581 && TREE_CODE (position) != FUNCTION_DECL)
9582 position = default_conversion (position);
50ca527f 9583
3e5a8b00 9584 if (TREE_CODE (position) != INTEGER_CST
9585 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
a0c938f0 9586 {
48e1416a 9587 warning (OPT_Wattributes,
01b54db5 9588 "requested position is not an integer constant");
50ca527f 9589 *no_add_attrs = true;
9590 }
9591 else
a0c938f0 9592 {
50ca527f 9593 if (tree_int_cst_lt (position, integer_zero_node))
9594 {
01b54db5 9595 warning (OPT_Wattributes,
9596 "requested position is less than zero");
50ca527f 9597 *no_add_attrs = true;
9598 }
9599 }
bf6c8de0 9600 }
a0c938f0 9601
bf6c8de0 9602 return NULL_TREE;
9603}
b5c26b42 9604
9605/* Handle a "type_generic" attribute. */
9606
9607static tree
9608handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9609 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9610 bool * ARG_UNUSED (no_add_attrs))
9611{
19fbe3a4 9612 /* Ensure we have a function type. */
9613 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
48e1416a 9614
19fbe3a4 9615 /* Ensure we have a variadic function. */
c33080b9 9616 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
b5c26b42 9617
9618 return NULL_TREE;
9619}
46f8e3b0 9620
24470055 9621/* Handle a "target" attribute. */
46f8e3b0 9622
9623static tree
24470055 9624handle_target_attribute (tree *node, tree name, tree args, int flags,
46f8e3b0 9625 bool *no_add_attrs)
9626{
9627 /* Ensure we have a function type. */
9628 if (TREE_CODE (*node) != FUNCTION_DECL)
9629 {
9630 warning (OPT_Wattributes, "%qE attribute ignored", name);
9631 *no_add_attrs = true;
9632 }
46f8e3b0 9633 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
ae0c3984 9634 flags))
46f8e3b0 9635 *no_add_attrs = true;
9636
9637 return NULL_TREE;
9638}
9639
9640/* Arguments being collected for optimization. */
9641typedef const char *const_char_p; /* For DEF_VEC_P. */
f1f41a6c 9642static GTY(()) vec<const_char_p, va_gc> *optimize_args;
46f8e3b0 9643
9644
9645/* Inner function to convert a TREE_LIST to argv string to parse the optimize
9646 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9647 false for #pragma GCC optimize. */
9648
9649bool
9650parse_optimize_options (tree args, bool attr_p)
9651{
9652 bool ret = true;
9653 unsigned opt_argc;
9654 unsigned i;
2becf397 9655 int saved_flag_strict_aliasing;
46f8e3b0 9656 const char **opt_argv;
615ef0bb 9657 struct cl_decoded_option *decoded_options;
9658 unsigned int decoded_options_count;
46f8e3b0 9659 tree ap;
9660
9661 /* Build up argv vector. Just in case the string is stored away, use garbage
9662 collected strings. */
f1f41a6c 9663 vec_safe_truncate (optimize_args, 0);
9664 vec_safe_push (optimize_args, (const char *) NULL);
46f8e3b0 9665
9666 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9667 {
9668 tree value = TREE_VALUE (ap);
9669
9670 if (TREE_CODE (value) == INTEGER_CST)
9671 {
9672 char buffer[20];
f9ae6f95 9673 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
f1f41a6c 9674 vec_safe_push (optimize_args, ggc_strdup (buffer));
46f8e3b0 9675 }
9676
9677 else if (TREE_CODE (value) == STRING_CST)
9678 {
9679 /* Split string into multiple substrings. */
9680 size_t len = TREE_STRING_LENGTH (value);
9681 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9682 char *end = p + len;
9683 char *comma;
9684 char *next_p = p;
9685
9686 while (next_p != NULL)
9687 {
9688 size_t len2;
9689 char *q, *r;
9690
9691 p = next_p;
9692 comma = strchr (p, ',');
9693 if (comma)
9694 {
9695 len2 = comma - p;
9696 *comma = '\0';
9697 next_p = comma+1;
9698 }
9699 else
9700 {
9701 len2 = end - p;
9702 next_p = NULL;
9703 }
9704
ba72912a 9705 r = q = (char *) ggc_alloc_atomic (len2 + 3);
46f8e3b0 9706
9707 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9708 options. */
9709 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9710 {
9711 ret = false;
9712 if (attr_p)
9713 warning (OPT_Wattributes,
ebd7c4c1 9714 "bad option %s to optimize attribute", p);
46f8e3b0 9715 else
9716 warning (OPT_Wpragmas,
e44b0a1f 9717 "bad option %s to pragma attribute", p);
46f8e3b0 9718 continue;
9719 }
9720
9721 if (*p != '-')
9722 {
9723 *r++ = '-';
9724
9725 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9726 itself is -Os, and any other switch begins with a -f. */
9727 if ((*p >= '0' && *p <= '9')
9728 || (p[0] == 's' && p[1] == '\0'))
9729 *r++ = 'O';
9730 else if (*p != 'O')
9731 *r++ = 'f';
9732 }
9733
9734 memcpy (r, p, len2);
9735 r[len2] = '\0';
f1f41a6c 9736 vec_safe_push (optimize_args, (const char *) q);
46f8e3b0 9737 }
9738
9739 }
9740 }
9741
f1f41a6c 9742 opt_argc = optimize_args->length ();
46f8e3b0 9743 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9744
9745 for (i = 1; i < opt_argc; i++)
f1f41a6c 9746 opt_argv[i] = (*optimize_args)[i];
46f8e3b0 9747
2becf397 9748 saved_flag_strict_aliasing = flag_strict_aliasing;
9749
46f8e3b0 9750 /* Now parse the options. */
f3f006ad 9751 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9752 &decoded_options,
9753 &decoded_options_count);
9754 decode_options (&global_options, &global_options_set,
3c6c0e40 9755 decoded_options, decoded_options_count,
9756 input_location, global_dc);
46f8e3b0 9757
4bec06b3 9758 targetm.override_options_after_change();
9759
2becf397 9760 /* Don't allow changing -fstrict-aliasing. */
9761 flag_strict_aliasing = saved_flag_strict_aliasing;
9762
f1f41a6c 9763 optimize_args->truncate (0);
46f8e3b0 9764 return ret;
9765}
9766
9767/* For handling "optimize" attribute. arguments as in
9768 struct attribute_spec.handler. */
9769
9770static tree
9771handle_optimize_attribute (tree *node, tree name, tree args,
9772 int ARG_UNUSED (flags), bool *no_add_attrs)
9773{
9774 /* Ensure we have a function type. */
9775 if (TREE_CODE (*node) != FUNCTION_DECL)
9776 {
9777 warning (OPT_Wattributes, "%qE attribute ignored", name);
9778 *no_add_attrs = true;
9779 }
9780 else
9781 {
9782 struct cl_optimization cur_opts;
9783 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9784
9785 /* Save current options. */
2c5d2e39 9786 cl_optimization_save (&cur_opts, &global_options);
46f8e3b0 9787
9788 /* If we previously had some optimization options, use them as the
9789 default. */
9790 if (old_opts)
2c5d2e39 9791 cl_optimization_restore (&global_options,
9792 TREE_OPTIMIZATION (old_opts));
46f8e3b0 9793
9794 /* Parse options, and update the vector. */
9795 parse_optimize_options (args, true);
9796 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
55310327 9797 = build_optimization_node (&global_options);
46f8e3b0 9798
9799 /* Restore current options. */
2c5d2e39 9800 cl_optimization_restore (&global_options, &cur_opts);
46f8e3b0 9801 }
9802
9803 return NULL_TREE;
9804}
48b14f50 9805
9806/* Handle a "no_split_stack" attribute. */
9807
9808static tree
9809handle_no_split_stack_attribute (tree *node, tree name,
9810 tree ARG_UNUSED (args),
9811 int ARG_UNUSED (flags),
9812 bool *no_add_attrs)
9813{
9814 tree decl = *node;
9815
9816 if (TREE_CODE (decl) != FUNCTION_DECL)
9817 {
9818 error_at (DECL_SOURCE_LOCATION (decl),
9819 "%qE attribute applies only to functions", name);
9820 *no_add_attrs = true;
9821 }
9822 else if (DECL_INITIAL (decl))
9823 {
9824 error_at (DECL_SOURCE_LOCATION (decl),
9825 "can%'t set %qE attribute after definition", name);
9826 *no_add_attrs = true;
9827 }
9828
9829 return NULL_TREE;
9830}
d7dcba40 9831
9832/* Handle a "returns_nonnull" attribute; arguments as in
9833 struct attribute_spec.handler. */
9834
9835static tree
9836handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9837 bool *no_add_attrs)
9838{
9839 // Even without a prototype we still have a return type we can check.
9840 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9841 {
9842 error ("returns_nonnull attribute on a function not returning a pointer");
9843 *no_add_attrs = true;
9844 }
9845 return NULL_TREE;
9846}
9847
74691f46 9848/* Handle a "designated_init" attribute; arguments as in
9849 struct attribute_spec.handler. */
9850
9851static tree
9852handle_designated_init_attribute (tree *node, tree name, tree, int,
9853 bool *no_add_attrs)
9854{
9855 if (TREE_CODE (*node) != RECORD_TYPE)
9856 {
9857 error ("%qE attribute is only valid on %<struct%> type", name);
9858 *no_add_attrs = true;
9859 }
9860 return NULL_TREE;
9861}
9862
dbf6c367 9863\f
774e9d58 9864/* Check for valid arguments being passed to a function with FNTYPE.
9865 There are NARGS arguments in the array ARGARRAY. */
dbf6c367 9866void
774e9d58 9867check_function_arguments (const_tree fntype, int nargs, tree *argarray)
dbf6c367 9868{
9869 /* Check for null being passed in a pointer argument that must be
9870 non-null. We also need to do this if format checking is enabled. */
9871
9872 if (warn_nonnull)
774e9d58 9873 check_function_nonnull (TYPE_ATTRIBUTES (fntype), nargs, argarray);
dbf6c367 9874
9875 /* Check for errors in format strings. */
9876
068bea1e 9877 if (warn_format || warn_suggest_attribute_format)
774e9d58 9878 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
95c90e04 9879
9880 if (warn_format)
774e9d58 9881 check_function_sentinel (fntype, nargs, argarray);
dbf6c367 9882}
9883
9884/* Generic argument checking recursion routine. PARAM is the argument to
9885 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9886 once the argument is resolved. CTX is context for the callback. */
9887void
1cae46be 9888check_function_arguments_recurse (void (*callback)
9889 (void *, tree, unsigned HOST_WIDE_INT),
9890 void *ctx, tree param,
9891 unsigned HOST_WIDE_INT param_num)
dbf6c367 9892{
72dd6141 9893 if (CONVERT_EXPR_P (param)
c44afe23 9894 && (TYPE_PRECISION (TREE_TYPE (param))
9895 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
dbf6c367 9896 {
9897 /* Strip coercion. */
9898 check_function_arguments_recurse (callback, ctx,
4ee9c684 9899 TREE_OPERAND (param, 0), param_num);
dbf6c367 9900 return;
9901 }
9902
9903 if (TREE_CODE (param) == CALL_EXPR)
9904 {
c2f47e15 9905 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
dbf6c367 9906 tree attrs;
9907 bool found_format_arg = false;
9908
9909 /* See if this is a call to a known internationalization function
9910 that modifies a format arg. Such a function may have multiple
9911 format_arg attributes (for example, ngettext). */
9912
9913 for (attrs = TYPE_ATTRIBUTES (type);
9914 attrs;
9915 attrs = TREE_CHAIN (attrs))
9916 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9917 {
c2f47e15 9918 tree inner_arg;
dbf6c367 9919 tree format_num_expr;
9920 int format_num;
9921 int i;
c2f47e15 9922 call_expr_arg_iterator iter;
dbf6c367 9923
9924 /* Extract the argument number, which was previously checked
9925 to be valid. */
9926 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
ddb1be65 9927
e913b5cd 9928 format_num = tree_to_uhwi (format_num_expr);
dbf6c367 9929
c2f47e15 9930 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9931 inner_arg != 0;
9932 inner_arg = next_call_expr_arg (&iter), i++)
dbf6c367 9933 if (i == format_num)
9934 {
9935 check_function_arguments_recurse (callback, ctx,
c2f47e15 9936 inner_arg, param_num);
dbf6c367 9937 found_format_arg = true;
9938 break;
9939 }
9940 }
9941
9942 /* If we found a format_arg attribute and did a recursive check,
9943 we are done with checking this argument. Otherwise, we continue
9944 and this will be considered a non-literal. */
9945 if (found_format_arg)
9946 return;
9947 }
9948
9949 if (TREE_CODE (param) == COND_EXPR)
9950 {
9951 /* Check both halves of the conditional expression. */
9952 check_function_arguments_recurse (callback, ctx,
4ee9c684 9953 TREE_OPERAND (param, 1), param_num);
dbf6c367 9954 check_function_arguments_recurse (callback, ctx,
4ee9c684 9955 TREE_OPERAND (param, 2), param_num);
dbf6c367 9956 return;
9957 }
9958
9959 (*callback) (ctx, param, param_num);
9960}
1f3233d1 9961
60cce472 9962/* Checks for a builtin function FNDECL that the number of arguments
9963 NARGS against the required number REQUIRED and issues an error if
9964 there is a mismatch. Returns true if the number of arguments is
9965 correct, otherwise false. */
d43cee80 9966
9967static bool
60cce472 9968builtin_function_validate_nargs (tree fndecl, int nargs, int required)
d43cee80 9969{
9970 if (nargs < required)
9971 {
60cce472 9972 error_at (input_location,
9973 "not enough arguments to function %qE", fndecl);
d43cee80 9974 return false;
9975 }
9976 else if (nargs > required)
9977 {
60cce472 9978 error_at (input_location,
9979 "too many arguments to function %qE", fndecl);
d43cee80 9980 return false;
9981 }
9982 return true;
9983}
9984
9985/* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
9986 Returns false if there was an error, otherwise true. */
9987
9988bool
9989check_builtin_function_arguments (tree fndecl, int nargs, tree *args)
9990{
9991 if (!DECL_BUILT_IN (fndecl)
9992 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9993 return true;
9994
9995 switch (DECL_FUNCTION_CODE (fndecl))
9996 {
9997 case BUILT_IN_CONSTANT_P:
60cce472 9998 return builtin_function_validate_nargs (fndecl, nargs, 1);
d43cee80 9999
10000 case BUILT_IN_ISFINITE:
10001 case BUILT_IN_ISINF:
c319d56a 10002 case BUILT_IN_ISINF_SIGN:
d43cee80 10003 case BUILT_IN_ISNAN:
10004 case BUILT_IN_ISNORMAL:
60cce472 10005 if (builtin_function_validate_nargs (fndecl, nargs, 1))
d43cee80 10006 {
10007 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
10008 {
10009 error ("non-floating-point argument in call to "
10010 "function %qE", fndecl);
10011 return false;
10012 }
10013 return true;
10014 }
10015 return false;
10016
10017 case BUILT_IN_ISGREATER:
10018 case BUILT_IN_ISGREATEREQUAL:
10019 case BUILT_IN_ISLESS:
10020 case BUILT_IN_ISLESSEQUAL:
10021 case BUILT_IN_ISLESSGREATER:
10022 case BUILT_IN_ISUNORDERED:
60cce472 10023 if (builtin_function_validate_nargs (fndecl, nargs, 2))
d43cee80 10024 {
10025 enum tree_code code0, code1;
10026 code0 = TREE_CODE (TREE_TYPE (args[0]));
10027 code1 = TREE_CODE (TREE_TYPE (args[1]));
10028 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
10029 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10030 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
10031 {
10032 error ("non-floating-point arguments in call to "
10033 "function %qE", fndecl);
10034 return false;
10035 }
10036 return true;
10037 }
10038 return false;
10039
19fbe3a4 10040 case BUILT_IN_FPCLASSIFY:
60cce472 10041 if (builtin_function_validate_nargs (fndecl, nargs, 6))
19fbe3a4 10042 {
10043 unsigned i;
48e1416a 10044
19fbe3a4 10045 for (i=0; i<5; i++)
10046 if (TREE_CODE (args[i]) != INTEGER_CST)
10047 {
10048 error ("non-const integer argument %u in call to function %qE",
10049 i+1, fndecl);
10050 return false;
10051 }
10052
10053 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
10054 {
10055 error ("non-floating-point argument in call to function %qE",
10056 fndecl);
10057 return false;
10058 }
10059 return true;
10060 }
10061 return false;
10062
fca0886c 10063 case BUILT_IN_ASSUME_ALIGNED:
10064 if (builtin_function_validate_nargs (fndecl, nargs, 2 + (nargs > 2)))
10065 {
10066 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
10067 {
10068 error ("non-integer argument 3 in call to function %qE", fndecl);
10069 return false;
10070 }
10071 return true;
10072 }
10073 return false;
10074
0c93c8a9 10075 case BUILT_IN_ADD_OVERFLOW:
10076 case BUILT_IN_SUB_OVERFLOW:
10077 case BUILT_IN_MUL_OVERFLOW:
10078 if (builtin_function_validate_nargs (fndecl, nargs, 3))
10079 {
10080 unsigned i;
10081 for (i = 0; i < 2; i++)
10082 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
10083 {
10084 error ("argument %u in call to function %qE does not have "
10085 "integral type", i + 1, fndecl);
10086 return false;
10087 }
10088 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
10089 || TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) != INTEGER_TYPE)
10090 {
10091 error ("argument 3 in call to function %qE does not have "
10092 "pointer to integer type", fndecl);
10093 return false;
10094 }
10095 return true;
10096 }
10097 return false;
10098
d43cee80 10099 default:
10100 return true;
10101 }
10102}
10103
860251be 10104/* Function to help qsort sort FIELD_DECLs by name order. */
10105
10106int
10107field_decl_cmp (const void *x_p, const void *y_p)
10108{
4fd61bc6 10109 const tree *const x = (const tree *const) x_p;
10110 const tree *const y = (const tree *const) y_p;
10111
860251be 10112 if (DECL_NAME (*x) == DECL_NAME (*y))
10113 /* A nontype is "greater" than a type. */
10114 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10115 if (DECL_NAME (*x) == NULL_TREE)
10116 return -1;
10117 if (DECL_NAME (*y) == NULL_TREE)
10118 return 1;
10119 if (DECL_NAME (*x) < DECL_NAME (*y))
10120 return -1;
10121 return 1;
10122}
10123
10124static struct {
10125 gt_pointer_operator new_value;
10126 void *cookie;
10127} resort_data;
10128
10129/* This routine compares two fields like field_decl_cmp but using the
10130pointer operator in resort_data. */
10131
10132static int
10133resort_field_decl_cmp (const void *x_p, const void *y_p)
10134{
4fd61bc6 10135 const tree *const x = (const tree *const) x_p;
10136 const tree *const y = (const tree *const) y_p;
860251be 10137
10138 if (DECL_NAME (*x) == DECL_NAME (*y))
10139 /* A nontype is "greater" than a type. */
10140 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10141 if (DECL_NAME (*x) == NULL_TREE)
10142 return -1;
10143 if (DECL_NAME (*y) == NULL_TREE)
10144 return 1;
10145 {
10146 tree d1 = DECL_NAME (*x);
10147 tree d2 = DECL_NAME (*y);
10148 resort_data.new_value (&d1, resort_data.cookie);
10149 resort_data.new_value (&d2, resort_data.cookie);
10150 if (d1 < d2)
10151 return -1;
10152 }
10153 return 1;
10154}
10155
10156/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10157
10158void
10159resort_sorted_fields (void *obj,
9a03a746 10160 void * ARG_UNUSED (orig_obj),
4ee9c684 10161 gt_pointer_operator new_value,
10162 void *cookie)
860251be 10163{
9a03a746 10164 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
860251be 10165 resort_data.new_value = new_value;
10166 resort_data.cookie = cookie;
10167 qsort (&sf->elts[0], sf->len, sizeof (tree),
4ee9c684 10168 resort_field_decl_cmp);
860251be 10169}
10170
209c9752 10171/* Subroutine of c_parse_error.
10172 Return the result of concatenating LHS and RHS. RHS is really
10173 a string literal, its first character is indicated by RHS_START and
cfee01e3 10174 RHS_SIZE is its length (including the terminating NUL character).
209c9752 10175
10176 The caller is responsible for deleting the returned pointer. */
10177
10178static char *
10179catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10180{
10181 const int lhs_size = strlen (lhs);
10182 char *result = XNEWVEC (char, lhs_size + rhs_size);
10183 strncpy (result, lhs, lhs_size);
10184 strncpy (result + lhs_size, rhs_start, rhs_size);
10185 return result;
10186}
10187
380c6697 10188/* Issue the error given by GMSGID, indicating that it occurred before
92b128ed 10189 TOKEN, which had the associated VALUE. */
10190
10191void
48e1416a 10192c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
ba99525e 10193 tree value, unsigned char token_flags)
92b128ed 10194{
209c9752 10195#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10196
10197 char *message = NULL;
92b128ed 10198
ba99525e 10199 if (token_type == CPP_EOF)
380c6697 10200 message = catenate_messages (gmsgid, " at end of input");
48e1416a 10201 else if (token_type == CPP_CHAR
10202 || token_type == CPP_WCHAR
ba99525e 10203 || token_type == CPP_CHAR16
10204 || token_type == CPP_CHAR32)
92b128ed 10205 {
f9ae6f95 10206 unsigned int val = TREE_INT_CST_LOW (value);
924bbf02 10207 const char *prefix;
10208
ba99525e 10209 switch (token_type)
924bbf02 10210 {
10211 default:
10212 prefix = "";
10213 break;
10214 case CPP_WCHAR:
10215 prefix = "L";
10216 break;
10217 case CPP_CHAR16:
10218 prefix = "u";
10219 break;
10220 case CPP_CHAR32:
10221 prefix = "U";
10222 break;
10223 }
10224
92b128ed 10225 if (val <= UCHAR_MAX && ISGRAPH (val))
a0c938f0 10226 message = catenate_messages (gmsgid, " before %s'%c'");
92b128ed 10227 else
a0c938f0 10228 message = catenate_messages (gmsgid, " before %s'\\x%x'");
209c9752 10229
924bbf02 10230 error (message, prefix, val);
209c9752 10231 free (message);
10232 message = NULL;
92b128ed 10233 }
1898176c 10234 else if (token_type == CPP_CHAR_USERDEF
10235 || token_type == CPP_WCHAR_USERDEF
10236 || token_type == CPP_CHAR16_USERDEF
10237 || token_type == CPP_CHAR32_USERDEF)
10238 message = catenate_messages (gmsgid,
10239 " before user-defined character literal");
10240 else if (token_type == CPP_STRING_USERDEF
10241 || token_type == CPP_WSTRING_USERDEF
10242 || token_type == CPP_STRING16_USERDEF
10243 || token_type == CPP_STRING32_USERDEF
10244 || token_type == CPP_UTF8STRING_USERDEF)
10245 message = catenate_messages (gmsgid, " before user-defined string literal");
48e1416a 10246 else if (token_type == CPP_STRING
10247 || token_type == CPP_WSTRING
ba99525e 10248 || token_type == CPP_STRING16
538ba11a 10249 || token_type == CPP_STRING32
10250 || token_type == CPP_UTF8STRING)
380c6697 10251 message = catenate_messages (gmsgid, " before string constant");
ba99525e 10252 else if (token_type == CPP_NUMBER)
380c6697 10253 message = catenate_messages (gmsgid, " before numeric constant");
ba99525e 10254 else if (token_type == CPP_NAME)
209c9752 10255 {
380c6697 10256 message = catenate_messages (gmsgid, " before %qE");
782858b8 10257 error (message, value);
209c9752 10258 free (message);
10259 message = NULL;
10260 }
ba99525e 10261 else if (token_type == CPP_PRAGMA)
b75b98aa 10262 message = catenate_messages (gmsgid, " before %<#pragma%>");
ba99525e 10263 else if (token_type == CPP_PRAGMA_EOL)
b75b98aa 10264 message = catenate_messages (gmsgid, " before end of line");
07b8f133 10265 else if (token_type == CPP_DECLTYPE)
10266 message = catenate_messages (gmsgid, " before %<decltype%>");
ba99525e 10267 else if (token_type < N_TTYPES)
209c9752 10268 {
380c6697 10269 message = catenate_messages (gmsgid, " before %qs token");
ba99525e 10270 error (message, cpp_type2name (token_type, token_flags));
209c9752 10271 free (message);
10272 message = NULL;
10273 }
92b128ed 10274 else
380c6697 10275 error (gmsgid);
209c9752 10276
10277 if (message)
10278 {
10279 error (message);
10280 free (message);
10281 }
a0c938f0 10282#undef catenate_messages
92b128ed 10283}
10284
3a79f5da 10285/* Return the gcc option code associated with the reason for a cpp
10286 message, or 0 if none. */
10287
10288static int
10289c_option_controlling_cpp_error (int reason)
10290{
7ff8db31 10291 const struct cpp_reason_option_codes_t *entry;
3a79f5da 10292
7ff8db31 10293 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
3a79f5da 10294 {
10295 if (entry->reason == reason)
10296 return entry->option_code;
10297 }
10298 return 0;
10299}
10300
7f5f3953 10301/* Callback from cpp_error for PFILE to print diagnostics from the
3a79f5da 10302 preprocessor. The diagnostic is of type LEVEL, with REASON set
10303 to the reason code if LEVEL is represents a warning, at location
7f5f3953 10304 LOCATION unless this is after lexing and the compiler's location
10305 should be used instead, with column number possibly overridden by
10306 COLUMN_OVERRIDE if not zero; MSG is the translated message and AP
10307 the arguments. Returns true if a diagnostic was emitted, false
10308 otherwise. */
10309
10310bool
3a79f5da 10311c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
7f5f3953 10312 location_t location, unsigned int column_override,
10313 const char *msg, va_list *ap)
10314{
10315 diagnostic_info diagnostic;
10316 diagnostic_t dlevel;
5ae82d58 10317 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
7f5f3953 10318 bool ret;
10319
10320 switch (level)
10321 {
10322 case CPP_DL_WARNING_SYSHDR:
10323 if (flag_no_output)
10324 return false;
5ae82d58 10325 global_dc->dc_warn_system_headers = 1;
7f5f3953 10326 /* Fall through. */
10327 case CPP_DL_WARNING:
10328 if (flag_no_output)
10329 return false;
10330 dlevel = DK_WARNING;
10331 break;
10332 case CPP_DL_PEDWARN:
10333 if (flag_no_output && !flag_pedantic_errors)
10334 return false;
10335 dlevel = DK_PEDWARN;
10336 break;
10337 case CPP_DL_ERROR:
10338 dlevel = DK_ERROR;
10339 break;
10340 case CPP_DL_ICE:
10341 dlevel = DK_ICE;
10342 break;
10343 case CPP_DL_NOTE:
10344 dlevel = DK_NOTE;
10345 break;
ff903809 10346 case CPP_DL_FATAL:
10347 dlevel = DK_FATAL;
10348 break;
7f5f3953 10349 default:
10350 gcc_unreachable ();
10351 }
10352 if (done_lexing)
10353 location = input_location;
10354 diagnostic_set_info_translated (&diagnostic, msg, ap,
10355 location, dlevel);
10356 if (column_override)
10357 diagnostic_override_column (&diagnostic, column_override);
3a79f5da 10358 diagnostic_override_option_index (&diagnostic,
10359 c_option_controlling_cpp_error (reason));
7f5f3953 10360 ret = report_diagnostic (&diagnostic);
10361 if (level == CPP_DL_WARNING_SYSHDR)
5ae82d58 10362 global_dc->dc_warn_system_headers = save_warn_system_headers;
7f5f3953 10363 return ret;
10364}
10365
624d37a6 10366/* Convert a character from the host to the target execution character
10367 set. cpplib handles this, mostly. */
10368
10369HOST_WIDE_INT
10370c_common_to_target_charset (HOST_WIDE_INT c)
10371{
10372 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10373 zero-extended under -fno-signed-char. cpplib insists that characters
10374 and character constants are always unsigned. Hence we must convert
10375 back and forth. */
10376 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10377
10378 uc = cpp_host_to_exec_charset (parse_in, uc);
10379
10380 if (flag_signed_char)
10381 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10382 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10383 else
10384 return uc;
10385}
10386
7549df0d 10387/* Fold an offsetof-like expression. EXPR is a nested sequence of component
10388 references with an INDIRECT_REF of a constant at the bottom; much like the
10389 traditional rendering of offsetof as a macro. Return the folded result. */
af28855b 10390
7549df0d 10391tree
10392fold_offsetof_1 (tree expr)
af28855b 10393{
af28855b 10394 tree base, off, t;
10395
10396 switch (TREE_CODE (expr))
10397 {
10398 case ERROR_MARK:
10399 return expr;
10400
6b11d2e3 10401 case VAR_DECL:
10402 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10403 return error_mark_node;
10404
d897f7c2 10405 case CALL_EXPR:
cf1a89a3 10406 case TARGET_EXPR:
d897f7c2 10407 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10408 return error_mark_node;
10409
d897f7c2 10410 case NOP_EXPR:
10411 case INDIRECT_REF:
7549df0d 10412 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
64ed018c 10413 {
10414 error ("cannot apply %<offsetof%> to a non constant address");
10415 return error_mark_node;
10416 }
7549df0d 10417 return TREE_OPERAND (expr, 0);
d897f7c2 10418
af28855b 10419 case COMPONENT_REF:
7549df0d 10420 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
af28855b 10421 if (base == error_mark_node)
10422 return base;
10423
10424 t = TREE_OPERAND (expr, 1);
10425 if (DECL_C_BIT_FIELD (t))
10426 {
10427 error ("attempt to take address of bit-field structure "
782858b8 10428 "member %qD", t);
af28855b 10429 return error_mark_node;
10430 }
389dd41b 10431 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
e913b5cd 10432 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
389dd41b 10433 / BITS_PER_UNIT));
af28855b 10434 break;
10435
10436 case ARRAY_REF:
7549df0d 10437 base = fold_offsetof_1 (TREE_OPERAND (expr, 0));
af28855b 10438 if (base == error_mark_node)
10439 return base;
10440
10441 t = TREE_OPERAND (expr, 1);
64ed018c 10442
10443 /* Check if the offset goes beyond the upper bound of the array. */
7549df0d 10444 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
e0559d69 10445 {
10446 tree upbound = array_ref_up_bound (expr);
10447 if (upbound != NULL_TREE
10448 && TREE_CODE (upbound) == INTEGER_CST
10449 && !tree_int_cst_equal (upbound,
10450 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10451 {
10452 upbound = size_binop (PLUS_EXPR, upbound,
10453 build_int_cst (TREE_TYPE (upbound), 1));
10454 if (tree_int_cst_lt (upbound, t))
10455 {
10456 tree v;
10457
10458 for (v = TREE_OPERAND (expr, 0);
10459 TREE_CODE (v) == COMPONENT_REF;
10460 v = TREE_OPERAND (v, 0))
10461 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10462 == RECORD_TYPE)
10463 {
1767a056 10464 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10465 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
e0559d69 10466 if (TREE_CODE (fld_chain) == FIELD_DECL)
10467 break;
10468
10469 if (fld_chain)
10470 break;
10471 }
10472 /* Don't warn if the array might be considered a poor
10473 man's flexible array member with a very permissive
10474 definition thereof. */
10475 if (TREE_CODE (v) == ARRAY_REF
10476 || TREE_CODE (v) == COMPONENT_REF)
10477 warning (OPT_Warray_bounds,
10478 "index %E denotes an offset "
10479 "greater than size of %qT",
10480 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10481 }
10482 }
10483 }
7549df0d 10484
10485 t = convert (sizetype, t);
10486 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
af28855b 10487 break;
10488
ede90cc2 10489 case COMPOUND_EXPR:
10490 /* Handle static members of volatile structs. */
10491 t = TREE_OPERAND (expr, 1);
f48c7f4a 10492 gcc_assert (VAR_P (t));
7549df0d 10493 return fold_offsetof_1 (t);
ede90cc2 10494
af28855b 10495 default:
231bd014 10496 gcc_unreachable ();
af28855b 10497 }
10498
7549df0d 10499 return fold_build_pointer_plus (base, off);
af28855b 10500}
10501
7549df0d 10502/* Likewise, but convert it to the return type of offsetof. */
10503
af28855b 10504tree
7549df0d 10505fold_offsetof (tree expr)
af28855b 10506{
7549df0d 10507 return convert (size_type_node, fold_offsetof_1 (expr));
af28855b 10508}
10509
b9bdfa0b 10510/* Warn for A ?: C expressions (with B omitted) where A is a boolean
10511 expression, because B will always be true. */
10512
10513void
10514warn_for_omitted_condop (location_t location, tree cond)
10515{
10516 if (truth_value_p (TREE_CODE (cond)))
10517 warning_at (location, OPT_Wparentheses,
10518 "the omitted middle operand in ?: will always be %<true%>, "
10519 "suggest explicit middle operand");
10520}
10521
a1f90215 10522/* Give an error for storing into ARG, which is 'const'. USE indicates
10523 how ARG was being used. */
10524
10525void
f2697631 10526readonly_error (location_t loc, tree arg, enum lvalue_use use)
a1f90215 10527{
10528 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10529 || use == lv_asm);
10530 /* Using this macro rather than (for example) arrays of messages
10531 ensures that all the format strings are checked at compile
10532 time. */
10533#define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10534 : (use == lv_increment ? (I) \
10535 : (use == lv_decrement ? (D) : (AS))))
10536 if (TREE_CODE (arg) == COMPONENT_REF)
10537 {
10538 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
f2697631 10539 error_at (loc, READONLY_MSG (G_("assignment of member "
10540 "%qD in read-only object"),
10541 G_("increment of member "
10542 "%qD in read-only object"),
10543 G_("decrement of member "
10544 "%qD in read-only object"),
10545 G_("member %qD in read-only object "
10546 "used as %<asm%> output")),
10547 TREE_OPERAND (arg, 1));
a1f90215 10548 else
f2697631 10549 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10550 G_("increment of read-only member %qD"),
10551 G_("decrement of read-only member %qD"),
10552 G_("read-only member %qD used as %<asm%> output")),
10553 TREE_OPERAND (arg, 1));
a1f90215 10554 }
f48c7f4a 10555 else if (VAR_P (arg))
f2697631 10556 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10557 G_("increment of read-only variable %qD"),
10558 G_("decrement of read-only variable %qD"),
10559 G_("read-only variable %qD used as %<asm%> output")),
10560 arg);
a1f90215 10561 else if (TREE_CODE (arg) == PARM_DECL)
f2697631 10562 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10563 G_("increment of read-only parameter %qD"),
10564 G_("decrement of read-only parameter %qD"),
10565 G_("read-only parameter %qD use as %<asm%> output")),
10566 arg);
a1f90215 10567 else if (TREE_CODE (arg) == RESULT_DECL)
10568 {
10569 gcc_assert (c_dialect_cxx ());
f2697631 10570 error_at (loc, READONLY_MSG (G_("assignment of "
10571 "read-only named return value %qD"),
10572 G_("increment of "
10573 "read-only named return value %qD"),
10574 G_("decrement of "
10575 "read-only named return value %qD"),
10576 G_("read-only named return value %qD "
10577 "used as %<asm%>output")),
10578 arg);
a1f90215 10579 }
10580 else if (TREE_CODE (arg) == FUNCTION_DECL)
f2697631 10581 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10582 G_("increment of function %qD"),
10583 G_("decrement of function %qD"),
10584 G_("function %qD used as %<asm%> output")),
10585 arg);
a1f90215 10586 else
f2697631 10587 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10588 G_("increment of read-only location %qE"),
10589 G_("decrement of read-only location %qE"),
10590 G_("read-only location %qE used as %<asm%> output")),
10591 arg);
a1f90215 10592}
10593
e35976b1 10594/* Print an error message for an invalid lvalue. USE says
fdd84b77 10595 how the lvalue is being used and so selects the error message. LOC
10596 is the location for the error. */
ab6bb714 10597
e35976b1 10598void
fdd84b77 10599lvalue_error (location_t loc, enum lvalue_use use)
ab6bb714 10600{
e35976b1 10601 switch (use)
ab6bb714 10602 {
e35976b1 10603 case lv_assign:
fdd84b77 10604 error_at (loc, "lvalue required as left operand of assignment");
e35976b1 10605 break;
10606 case lv_increment:
fdd84b77 10607 error_at (loc, "lvalue required as increment operand");
e35976b1 10608 break;
10609 case lv_decrement:
fdd84b77 10610 error_at (loc, "lvalue required as decrement operand");
e35976b1 10611 break;
10612 case lv_addressof:
fdd84b77 10613 error_at (loc, "lvalue required as unary %<&%> operand");
e35976b1 10614 break;
10615 case lv_asm:
fdd84b77 10616 error_at (loc, "lvalue required in asm statement");
e35976b1 10617 break;
10618 default:
10619 gcc_unreachable ();
ab6bb714 10620 }
ab6bb714 10621}
b1bbc8e5 10622
10623/* Print an error message for an invalid indirection of type TYPE.
10624 ERRSTRING is the name of the operator for the indirection. */
10625
10626void
10627invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10628{
10629 switch (errstring)
10630 {
10631 case RO_NULL:
10632 gcc_assert (c_dialect_cxx ());
10633 error_at (loc, "invalid type argument (have %qT)", type);
10634 break;
10635 case RO_ARRAY_INDEXING:
10636 error_at (loc,
10637 "invalid type argument of array indexing (have %qT)",
10638 type);
10639 break;
10640 case RO_UNARY_STAR:
10641 error_at (loc,
10642 "invalid type argument of unary %<*%> (have %qT)",
10643 type);
10644 break;
10645 case RO_ARROW:
10646 error_at (loc,
10647 "invalid type argument of %<->%> (have %qT)",
10648 type);
10649 break;
7354a89b 10650 case RO_ARROW_STAR:
10651 error_at (loc,
10652 "invalid type argument of %<->*%> (have %qT)",
10653 type);
10654 break;
b1bbc8e5 10655 case RO_IMPLICIT_CONVERSION:
10656 error_at (loc,
10657 "invalid type argument of implicit conversion (have %qT)",
10658 type);
10659 break;
10660 default:
10661 gcc_unreachable ();
10662 }
10663}
c271bdb2 10664\f
10665/* *PTYPE is an incomplete array. Complete it with a domain based on
10666 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10667 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10668 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10669
10670int
10671complete_array_type (tree *ptype, tree initial_value, bool do_default)
10672{
10673 tree maxindex, type, main_type, elt, unqual_elt;
10674 int failure = 0, quals;
6753bca0 10675 hashval_t hashcode = 0;
f5298614 10676 bool overflow_p = false;
c271bdb2 10677
10678 maxindex = size_zero_node;
10679 if (initial_value)
10680 {
10681 if (TREE_CODE (initial_value) == STRING_CST)
10682 {
10683 int eltsize
10684 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10685 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10686 }
10687 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10688 {
f1f41a6c 10689 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
c271bdb2 10690
f1f41a6c 10691 if (vec_safe_is_empty (v))
c271bdb2 10692 {
10693 if (pedantic)
10694 failure = 3;
7542c3b4 10695 maxindex = ssize_int (-1);
c271bdb2 10696 }
10697 else
10698 {
10699 tree curindex;
c75b4594 10700 unsigned HOST_WIDE_INT cnt;
10701 constructor_elt *ce;
cee43f7e 10702 bool fold_p = false;
c271bdb2 10703
f1f41a6c 10704 if ((*v)[0].index)
f5298614 10705 maxindex = (*v)[0].index, fold_p = true;
10706
c271bdb2 10707 curindex = maxindex;
10708
f1f41a6c 10709 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
c271bdb2 10710 {
cee43f7e 10711 bool curfold_p = false;
c75b4594 10712 if (ce->index)
cee43f7e 10713 curindex = ce->index, curfold_p = true;
c271bdb2 10714 else
cee43f7e 10715 {
10716 if (fold_p)
f5298614 10717 {
10718 /* Since we treat size types now as ordinary
10719 unsigned types, we need an explicit overflow
10720 check. */
10721 tree orig = curindex;
10722 curindex = fold_convert (sizetype, curindex);
10723 overflow_p |= tree_int_cst_lt (curindex, orig);
10724 }
389dd41b 10725 curindex = size_binop (PLUS_EXPR, curindex,
10726 size_one_node);
cee43f7e 10727 }
c271bdb2 10728 if (tree_int_cst_lt (maxindex, curindex))
cee43f7e 10729 maxindex = curindex, fold_p = curfold_p;
c271bdb2 10730 }
f5298614 10731 if (fold_p)
10732 {
10733 tree orig = maxindex;
10734 maxindex = fold_convert (sizetype, maxindex);
10735 overflow_p |= tree_int_cst_lt (maxindex, orig);
10736 }
c271bdb2 10737 }
10738 }
10739 else
10740 {
10741 /* Make an error message unless that happened already. */
10742 if (initial_value != error_mark_node)
10743 failure = 1;
10744 }
10745 }
10746 else
10747 {
10748 failure = 2;
10749 if (!do_default)
10750 return failure;
10751 }
10752
10753 type = *ptype;
10754 elt = TREE_TYPE (type);
10755 quals = TYPE_QUALS (strip_array_types (elt));
10756 if (quals == 0)
10757 unqual_elt = elt;
10758 else
6d5d708e 10759 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
c271bdb2 10760
10761 /* Using build_distinct_type_copy and modifying things afterward instead
10762 of using build_array_type to create a new type preserves all of the
10763 TYPE_LANG_FLAG_? bits that the front end may have set. */
10764 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10765 TREE_TYPE (main_type) = unqual_elt;
783bb57e 10766 TYPE_DOMAIN (main_type)
10767 = build_range_type (TREE_TYPE (maxindex),
10768 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
c271bdb2 10769 layout_type (main_type);
10770
6753bca0 10771 /* Make sure we have the canonical MAIN_TYPE. */
10772 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
48e1416a 10773 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
6753bca0 10774 hashcode);
10775 main_type = type_hash_canon (hashcode, main_type);
10776
796735dc 10777 /* Fix the canonical type. */
10778 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10779 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10780 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10781 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10782 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10783 != TYPE_DOMAIN (main_type)))
48e1416a 10784 TYPE_CANONICAL (main_type)
796735dc 10785 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10786 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10787 else
10788 TYPE_CANONICAL (main_type) = main_type;
10789
c271bdb2 10790 if (quals == 0)
10791 type = main_type;
10792 else
10793 type = c_build_qualified_type (main_type, quals);
10794
4f5b8f2a 10795 if (COMPLETE_TYPE_P (type)
10796 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
f5298614 10797 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
4f5b8f2a 10798 {
10799 error ("size of array is too large");
10800 /* If we proceed with the array type as it is, we'll eventually
08f817b3 10801 crash in tree_to_[su]hwi(). */
4f5b8f2a 10802 type = error_mark_node;
10803 }
10804
c271bdb2 10805 *ptype = type;
10806 return failure;
10807}
ab6bb714 10808
93426222 10809/* Like c_mark_addressable but don't check register qualifier. */
10810void
10811c_common_mark_addressable_vec (tree t)
10812{
10813 while (handled_component_p (t))
10814 t = TREE_OPERAND (t, 0);
f48c7f4a 10815 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
93426222 10816 return;
10817 TREE_ADDRESSABLE (t) = 1;
10818}
10819
10820
b6a5fc45 10821\f
10822/* Used to help initialize the builtin-types.def table. When a type of
10823 the correct size doesn't exist, use error_mark_node instead of NULL.
10824 The later results in segfaults even when a decl using the type doesn't
10825 get invoked. */
10826
10827tree
10828builtin_type_for_size (int size, bool unsignedp)
10829{
a51edb4c 10830 tree type = c_common_type_for_size (size, unsignedp);
b6a5fc45 10831 return type ? type : error_mark_node;
10832}
10833
10834/* A helper function for resolve_overloaded_builtin in resolving the
10835 overloaded __sync_ builtins. Returns a positive power of 2 if the
10836 first operand of PARAMS is a pointer to a supported data type.
10837 Returns 0 if an error is encountered. */
10838
10839static int
f1f41a6c 10840sync_resolve_size (tree function, vec<tree, va_gc> *params)
b6a5fc45 10841{
10842 tree type;
10843 int size;
10844
f1f41a6c 10845 if (!params)
b6a5fc45 10846 {
10847 error ("too few arguments to function %qE", function);
10848 return 0;
10849 }
10850
f1f41a6c 10851 type = TREE_TYPE ((*params)[0]);
0d284870 10852 if (TREE_CODE (type) == ARRAY_TYPE)
10853 {
10854 /* Force array-to-pointer decay for C++. */
10855 gcc_assert (c_dialect_cxx());
10856 (*params)[0] = default_conversion ((*params)[0]);
10857 type = TREE_TYPE ((*params)[0]);
10858 }
b6a5fc45 10859 if (TREE_CODE (type) != POINTER_TYPE)
10860 goto incompatible;
10861
10862 type = TREE_TYPE (type);
10863 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10864 goto incompatible;
10865
e913b5cd 10866 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
27213ba3 10867 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
b6a5fc45 10868 return size;
10869
10870 incompatible:
10871 error ("incompatible type for argument %d of %qE", 1, function);
10872 return 0;
10873}
10874
a0c938f0 10875/* A helper function for resolve_overloaded_builtin. Adds casts to
b6a5fc45 10876 PARAMS to make arguments match up with those of FUNCTION. Drops
10877 the variadic arguments at the end. Returns false if some error
10878 was encountered; true on success. */
10879
10880static bool
1cd6e20d 10881sync_resolve_params (location_t loc, tree orig_function, tree function,
f1f41a6c 10882 vec<tree, va_gc> *params, bool orig_format)
b6a5fc45 10883{
d0af78c5 10884 function_args_iterator iter;
b6a5fc45 10885 tree ptype;
b9c74b4d 10886 unsigned int parmnum;
b6a5fc45 10887
d0af78c5 10888 function_args_iter_init (&iter, TREE_TYPE (function));
b6a5fc45 10889 /* We've declared the implementation functions to use "volatile void *"
10890 as the pointer parameter, so we shouldn't get any complaints from the
10891 call to check_function_arguments what ever type the user used. */
d0af78c5 10892 function_args_iter_next (&iter);
f1f41a6c 10893 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
b560fabd 10894 ptype = TYPE_MAIN_VARIANT (ptype);
b6a5fc45 10895
10896 /* For the rest of the values, we need to cast these to FTYPE, so that we
10897 don't get warnings for passing pointer types, etc. */
b9c74b4d 10898 parmnum = 0;
d0af78c5 10899 while (1)
b6a5fc45 10900 {
d0af78c5 10901 tree val, arg_type;
10902
10903 arg_type = function_args_iter_cond (&iter);
10904 /* XXX void_type_node belies the abstraction. */
10905 if (arg_type == void_type_node)
10906 break;
b6a5fc45 10907
b9c74b4d 10908 ++parmnum;
f1f41a6c 10909 if (params->length () <= parmnum)
b6a5fc45 10910 {
1cd6e20d 10911 error_at (loc, "too few arguments to function %qE", orig_function);
b6a5fc45 10912 return false;
10913 }
10914
0f6a7cb7 10915 /* Only convert parameters if arg_type is unsigned integer type with
10916 new format sync routines, i.e. don't attempt to convert pointer
10917 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10918 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10919 kinds). */
10920 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
1cd6e20d 10921 {
10922 /* Ideally for the first conversion we'd use convert_for_assignment
10923 so that we get warnings for anything that doesn't match the pointer
10924 type. This isn't portable across the C and C++ front ends atm. */
f1f41a6c 10925 val = (*params)[parmnum];
1cd6e20d 10926 val = convert (ptype, val);
10927 val = convert (arg_type, val);
f1f41a6c 10928 (*params)[parmnum] = val;
1cd6e20d 10929 }
b6a5fc45 10930
d0af78c5 10931 function_args_iter_next (&iter);
b6a5fc45 10932 }
10933
1cd6e20d 10934 /* __atomic routines are not variadic. */
f1f41a6c 10935 if (!orig_format && params->length () != parmnum + 1)
1cd6e20d 10936 {
10937 error_at (loc, "too many arguments to function %qE", orig_function);
10938 return false;
10939 }
10940
b6a5fc45 10941 /* The definition of these primitives is variadic, with the remaining
10942 being "an optional list of variables protected by the memory barrier".
10943 No clue what that's supposed to mean, precisely, but we consider all
10944 call-clobbered variables to be protected so we're safe. */
f1f41a6c 10945 params->truncate (parmnum + 1);
b6a5fc45 10946
10947 return true;
10948}
10949
a0c938f0 10950/* A helper function for resolve_overloaded_builtin. Adds a cast to
b6a5fc45 10951 RESULT to make it match the type of the first pointer argument in
10952 PARAMS. */
10953
10954static tree
1cd6e20d 10955sync_resolve_return (tree first_param, tree result, bool orig_format)
b6a5fc45 10956{
b9c74b4d 10957 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
1cd6e20d 10958 tree rtype = TREE_TYPE (result);
10080eac 10959 ptype = TYPE_MAIN_VARIANT (ptype);
1cd6e20d 10960
10961 /* New format doesn't require casting unless the types are the same size. */
10962 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10963 return convert (ptype, result);
10964 else
10965 return result;
10966}
10967
10968/* This function verifies the PARAMS to generic atomic FUNCTION.
10969 It returns the size if all the parameters are the same size, otherwise
10970 0 is returned if the parameters are invalid. */
10971
10972static int
f1f41a6c 10973get_atomic_generic_size (location_t loc, tree function,
10974 vec<tree, va_gc> *params)
1cd6e20d 10975{
10976 unsigned int n_param;
10977 unsigned int n_model;
10978 unsigned int x;
10979 int size_0;
10980 tree type_0;
10981
10982 /* Determine the parameter makeup. */
10983 switch (DECL_FUNCTION_CODE (function))
10984 {
10985 case BUILT_IN_ATOMIC_EXCHANGE:
10986 n_param = 4;
10987 n_model = 1;
10988 break;
10989 case BUILT_IN_ATOMIC_LOAD:
10990 case BUILT_IN_ATOMIC_STORE:
10991 n_param = 3;
10992 n_model = 1;
10993 break;
10994 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
10995 n_param = 6;
10996 n_model = 2;
10997 break;
10998 default:
1d581089 10999 gcc_unreachable ();
1cd6e20d 11000 }
11001
f1f41a6c 11002 if (vec_safe_length (params) != n_param)
1cd6e20d 11003 {
11004 error_at (loc, "incorrect number of arguments to function %qE", function);
11005 return 0;
11006 }
11007
11008 /* Get type of first parameter, and determine its size. */
f1f41a6c 11009 type_0 = TREE_TYPE ((*params)[0]);
0d284870 11010 if (TREE_CODE (type_0) == ARRAY_TYPE)
11011 {
11012 /* Force array-to-pointer decay for C++. */
11013 gcc_assert (c_dialect_cxx());
11014 (*params)[0] = default_conversion ((*params)[0]);
11015 type_0 = TREE_TYPE ((*params)[0]);
11016 }
1d581089 11017 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
11018 {
11019 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
11020 function);
11021 return 0;
11022 }
11023
11024 /* Types must be compile time constant sizes. */
11025 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
1cd6e20d 11026 {
1d581089 11027 error_at (loc,
11028 "argument 1 of %qE must be a pointer to a constant size type",
11029 function);
1cd6e20d 11030 return 0;
11031 }
1d581089 11032
e913b5cd 11033 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
1cd6e20d 11034
1d581089 11035 /* Zero size objects are not allowed. */
11036 if (size_0 == 0)
11037 {
11038 error_at (loc,
11039 "argument 1 of %qE must be a pointer to a nonzero size object",
11040 function);
11041 return 0;
11042 }
11043
1cd6e20d 11044 /* Check each other parameter is a pointer and the same size. */
11045 for (x = 0; x < n_param - n_model; x++)
11046 {
11047 int size;
f1f41a6c 11048 tree type = TREE_TYPE ((*params)[x]);
a04e8d62 11049 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
1cd6e20d 11050 if (n_param == 6 && x == 3)
11051 continue;
11052 if (!POINTER_TYPE_P (type))
11053 {
11054 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
11055 function);
11056 return 0;
11057 }
1f6be080 11058 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
11059 size = type_size ? tree_to_uhwi (type_size) : 0;
1cd6e20d 11060 if (size != size_0)
11061 {
11062 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
11063 function);
11064 return 0;
11065 }
11066 }
11067
11068 /* Check memory model parameters for validity. */
11069 for (x = n_param - n_model ; x < n_param; x++)
11070 {
f1f41a6c 11071 tree p = (*params)[x];
1cd6e20d 11072 if (TREE_CODE (p) == INTEGER_CST)
11073 {
e913b5cd 11074 int i = tree_to_uhwi (p);
a372f7ca 11075 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
1cd6e20d 11076 {
11077 warning_at (loc, OPT_Winvalid_memory_model,
11078 "invalid memory model argument %d of %qE", x + 1,
11079 function);
1cd6e20d 11080 }
11081 }
11082 else
11083 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
11084 {
11085 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
11086 function);
11087 return 0;
11088 }
11089 }
11090
11091 return size_0;
11092}
11093
11094
11095/* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
11096 at the beginning of the parameter list PARAMS representing the size of the
11097 objects. This is to match the library ABI requirement. LOC is the location
11098 of the function call.
11099 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
11100 returned to allow the external call to be constructed. */
11101
11102static tree
11103add_atomic_size_parameter (unsigned n, location_t loc, tree function,
f1f41a6c 11104 vec<tree, va_gc> *params)
1cd6e20d 11105{
11106 tree size_node;
11107
11108 /* Insert a SIZE_T parameter as the first param. If there isn't
11109 enough space, allocate a new vector and recursively re-build with that. */
f1f41a6c 11110 if (!params->space (1))
1cd6e20d 11111 {
11112 unsigned int z, len;
f1f41a6c 11113 vec<tree, va_gc> *v;
1cd6e20d 11114 tree f;
11115
f1f41a6c 11116 len = params->length ();
11117 vec_alloc (v, len + 1);
5a672e62 11118 v->quick_push (build_int_cst (size_type_node, n));
1cd6e20d 11119 for (z = 0; z < len; z++)
f1f41a6c 11120 v->quick_push ((*params)[z]);
ec761d5a 11121 f = build_function_call_vec (loc, vNULL, function, v, NULL);
f1f41a6c 11122 vec_free (v);
1cd6e20d 11123 return f;
11124 }
11125
11126 /* Add the size parameter and leave as a function call for processing. */
11127 size_node = build_int_cst (size_type_node, n);
f1f41a6c 11128 params->quick_insert (0, size_node);
1cd6e20d 11129 return NULL_TREE;
11130}
11131
11132
a056826c 11133/* Return whether atomic operations for naturally aligned N-byte
11134 arguments are supported, whether inline or through libatomic. */
11135static bool
11136atomic_size_supported_p (int n)
11137{
11138 switch (n)
11139 {
11140 case 1:
11141 case 2:
11142 case 4:
11143 case 8:
11144 return true;
11145
11146 case 16:
11147 return targetm.scalar_mode_supported_p (TImode);
11148
11149 default:
11150 return false;
11151 }
11152}
11153
1cd6e20d 11154/* This will process an __atomic_exchange function call, determine whether it
11155 needs to be mapped to the _N variation, or turned into a library call.
11156 LOC is the location of the builtin call.
11157 FUNCTION is the DECL that has been invoked;
11158 PARAMS is the argument list for the call. The return value is non-null
11159 TRUE is returned if it is translated into the proper format for a call to the
11160 external library, and NEW_RETURN is set the tree for that function.
11161 FALSE is returned if processing for the _N variation is required, and
11162 NEW_RETURN is set to the the return value the result is copied into. */
11163static bool
11164resolve_overloaded_atomic_exchange (location_t loc, tree function,
f1f41a6c 11165 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11166{
11167 tree p0, p1, p2, p3;
11168 tree I_type, I_type_ptr;
11169 int n = get_atomic_generic_size (loc, function, params);
11170
1d581089 11171 /* Size of 0 is an error condition. */
11172 if (n == 0)
11173 {
11174 *new_return = error_mark_node;
11175 return true;
11176 }
11177
1cd6e20d 11178 /* If not a lock-free size, change to the library generic format. */
a056826c 11179 if (!atomic_size_supported_p (n))
1cd6e20d 11180 {
11181 *new_return = add_atomic_size_parameter (n, loc, function, params);
11182 return true;
11183 }
11184
11185 /* Otherwise there is a lockfree match, transform the call from:
11186 void fn(T* mem, T* desired, T* return, model)
11187 into
11188 *return = (T) (fn (In* mem, (In) *desired, model)) */
11189
f1f41a6c 11190 p0 = (*params)[0];
11191 p1 = (*params)[1];
11192 p2 = (*params)[2];
11193 p3 = (*params)[3];
1cd6e20d 11194
11195 /* Create pointer to appropriate size. */
11196 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11197 I_type_ptr = build_pointer_type (I_type);
11198
11199 /* Convert object pointer to required type. */
11200 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11201 (*params)[0] = p0;
1cd6e20d 11202 /* Convert new value to required type, and dereference it. */
11203 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11204 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 11205 (*params)[1] = p1;
1cd6e20d 11206
11207 /* Move memory model to the 3rd position, and end param list. */
f1f41a6c 11208 (*params)[2] = p3;
11209 params->truncate (3);
1cd6e20d 11210
11211 /* Convert return pointer and dereference it for later assignment. */
11212 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11213
11214 return false;
b6a5fc45 11215}
11216
1cd6e20d 11217
11218/* This will process an __atomic_compare_exchange function call, determine
11219 whether it needs to be mapped to the _N variation, or turned into a lib call.
11220 LOC is the location of the builtin call.
11221 FUNCTION is the DECL that has been invoked;
11222 PARAMS is the argument list for the call. The return value is non-null
11223 TRUE is returned if it is translated into the proper format for a call to the
11224 external library, and NEW_RETURN is set the tree for that function.
11225 FALSE is returned if processing for the _N variation is required. */
11226
11227static bool
11228resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
f1f41a6c 11229 vec<tree, va_gc> *params,
1cd6e20d 11230 tree *new_return)
11231{
11232 tree p0, p1, p2;
11233 tree I_type, I_type_ptr;
11234 int n = get_atomic_generic_size (loc, function, params);
11235
1d581089 11236 /* Size of 0 is an error condition. */
11237 if (n == 0)
11238 {
11239 *new_return = error_mark_node;
11240 return true;
11241 }
11242
1cd6e20d 11243 /* If not a lock-free size, change to the library generic format. */
a056826c 11244 if (!atomic_size_supported_p (n))
1cd6e20d 11245 {
11246 /* The library generic format does not have the weak parameter, so
11247 remove it from the param list. Since a parameter has been removed,
11248 we can be sure that there is room for the SIZE_T parameter, meaning
11249 there will not be a recursive rebuilding of the parameter list, so
11250 there is no danger this will be done twice. */
11251 if (n > 0)
11252 {
f1f41a6c 11253 (*params)[3] = (*params)[4];
11254 (*params)[4] = (*params)[5];
11255 params->truncate (5);
1cd6e20d 11256 }
11257 *new_return = add_atomic_size_parameter (n, loc, function, params);
11258 return true;
11259 }
11260
11261 /* Otherwise, there is a match, so the call needs to be transformed from:
11262 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11263 into
11264 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
ab2c1de8 11265
f1f41a6c 11266 p0 = (*params)[0];
11267 p1 = (*params)[1];
11268 p2 = (*params)[2];
1cd6e20d 11269
11270 /* Create pointer to appropriate size. */
11271 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11272 I_type_ptr = build_pointer_type (I_type);
11273
11274 /* Convert object pointer to required type. */
11275 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11276 (*params)[0] = p0;
1cd6e20d 11277
11278 /* Convert expected pointer to required type. */
11279 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
f1f41a6c 11280 (*params)[1] = p1;
1cd6e20d 11281
11282 /* Convert desired value to required type, and dereference it. */
11283 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11284 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
f1f41a6c 11285 (*params)[2] = p2;
1cd6e20d 11286
11287 /* The rest of the parameters are fine. NULL means no special return value
11288 processing.*/
11289 *new_return = NULL;
11290 return false;
11291}
11292
11293
11294/* This will process an __atomic_load function call, determine whether it
11295 needs to be mapped to the _N variation, or turned into a library call.
11296 LOC is the location of the builtin call.
11297 FUNCTION is the DECL that has been invoked;
11298 PARAMS is the argument list for the call. The return value is non-null
11299 TRUE is returned if it is translated into the proper format for a call to the
11300 external library, and NEW_RETURN is set the tree for that function.
11301 FALSE is returned if processing for the _N variation is required, and
11302 NEW_RETURN is set to the the return value the result is copied into. */
11303
11304static bool
11305resolve_overloaded_atomic_load (location_t loc, tree function,
f1f41a6c 11306 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11307{
11308 tree p0, p1, p2;
11309 tree I_type, I_type_ptr;
11310 int n = get_atomic_generic_size (loc, function, params);
11311
1d581089 11312 /* Size of 0 is an error condition. */
11313 if (n == 0)
11314 {
11315 *new_return = error_mark_node;
11316 return true;
11317 }
11318
1cd6e20d 11319 /* If not a lock-free size, change to the library generic format. */
a056826c 11320 if (!atomic_size_supported_p (n))
1cd6e20d 11321 {
11322 *new_return = add_atomic_size_parameter (n, loc, function, params);
11323 return true;
11324 }
11325
11326 /* Otherwise, there is a match, so the call needs to be transformed from:
11327 void fn(T* mem, T* return, model)
11328 into
11329 *return = (T) (fn ((In *) mem, model)) */
11330
f1f41a6c 11331 p0 = (*params)[0];
11332 p1 = (*params)[1];
11333 p2 = (*params)[2];
1cd6e20d 11334
11335 /* Create pointer to appropriate size. */
11336 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11337 I_type_ptr = build_pointer_type (I_type);
11338
11339 /* Convert object pointer to required type. */
11340 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11341 (*params)[0] = p0;
1cd6e20d 11342
11343 /* Move memory model to the 2nd position, and end param list. */
f1f41a6c 11344 (*params)[1] = p2;
11345 params->truncate (2);
1cd6e20d 11346
11347 /* Convert return pointer and dereference it for later assignment. */
11348 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11349
11350 return false;
11351}
11352
11353
11354/* This will process an __atomic_store function call, determine whether it
11355 needs to be mapped to the _N variation, or turned into a library call.
11356 LOC is the location of the builtin call.
11357 FUNCTION is the DECL that has been invoked;
11358 PARAMS is the argument list for the call. The return value is non-null
11359 TRUE is returned if it is translated into the proper format for a call to the
11360 external library, and NEW_RETURN is set the tree for that function.
11361 FALSE is returned if processing for the _N variation is required, and
11362 NEW_RETURN is set to the the return value the result is copied into. */
11363
11364static bool
11365resolve_overloaded_atomic_store (location_t loc, tree function,
f1f41a6c 11366 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 11367{
11368 tree p0, p1;
11369 tree I_type, I_type_ptr;
11370 int n = get_atomic_generic_size (loc, function, params);
11371
1d581089 11372 /* Size of 0 is an error condition. */
11373 if (n == 0)
11374 {
11375 *new_return = error_mark_node;
11376 return true;
11377 }
11378
1cd6e20d 11379 /* If not a lock-free size, change to the library generic format. */
a056826c 11380 if (!atomic_size_supported_p (n))
1cd6e20d 11381 {
11382 *new_return = add_atomic_size_parameter (n, loc, function, params);
11383 return true;
11384 }
11385
11386 /* Otherwise, there is a match, so the call needs to be transformed from:
11387 void fn(T* mem, T* value, model)
11388 into
11389 fn ((In *) mem, (In) *value, model) */
11390
f1f41a6c 11391 p0 = (*params)[0];
11392 p1 = (*params)[1];
1cd6e20d 11393
11394 /* Create pointer to appropriate size. */
11395 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11396 I_type_ptr = build_pointer_type (I_type);
11397
11398 /* Convert object pointer to required type. */
11399 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 11400 (*params)[0] = p0;
1cd6e20d 11401
11402 /* Convert new value to required type, and dereference it. */
11403 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11404 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 11405 (*params)[1] = p1;
1cd6e20d 11406
11407 /* The memory model is in the right spot already. Return is void. */
11408 *new_return = NULL_TREE;
11409
11410 return false;
11411}
11412
11413
b6a5fc45 11414/* Some builtin functions are placeholders for other expressions. This
11415 function should be called immediately after parsing the call expression
11416 before surrounding code has committed to the type of the expression.
11417
e60a6f7b 11418 LOC is the location of the builtin call.
11419
b6a5fc45 11420 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11421 PARAMS is the argument list for the call. The return value is non-null
11422 when expansion is complete, and null if normal processing should
11423 continue. */
11424
11425tree
f1f41a6c 11426resolve_overloaded_builtin (location_t loc, tree function,
11427 vec<tree, va_gc> *params)
b6a5fc45 11428{
11429 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
1cd6e20d 11430 bool orig_format = true;
11431 tree new_return = NULL_TREE;
11432
65441f6f 11433 switch (DECL_BUILT_IN_CLASS (function))
11434 {
11435 case BUILT_IN_NORMAL:
11436 break;
11437 case BUILT_IN_MD:
11438 if (targetm.resolve_overloaded_builtin)
e60a6f7b 11439 return targetm.resolve_overloaded_builtin (loc, function, params);
65441f6f 11440 else
a0c938f0 11441 return NULL_TREE;
65441f6f 11442 default:
11443 return NULL_TREE;
11444 }
a0c938f0 11445
65441f6f 11446 /* Handle BUILT_IN_NORMAL here. */
b6a5fc45 11447 switch (orig_code)
11448 {
1cd6e20d 11449 case BUILT_IN_ATOMIC_EXCHANGE:
11450 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11451 case BUILT_IN_ATOMIC_LOAD:
11452 case BUILT_IN_ATOMIC_STORE:
11453 {
11454 /* Handle these 4 together so that they can fall through to the next
11455 case if the call is transformed to an _N variant. */
11456 switch (orig_code)
11457 {
11458 case BUILT_IN_ATOMIC_EXCHANGE:
11459 {
11460 if (resolve_overloaded_atomic_exchange (loc, function, params,
11461 &new_return))
11462 return new_return;
11463 /* Change to the _N variant. */
11464 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11465 break;
11466 }
11467
11468 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11469 {
11470 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11471 params,
11472 &new_return))
11473 return new_return;
11474 /* Change to the _N variant. */
11475 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11476 break;
11477 }
11478 case BUILT_IN_ATOMIC_LOAD:
11479 {
11480 if (resolve_overloaded_atomic_load (loc, function, params,
11481 &new_return))
11482 return new_return;
11483 /* Change to the _N variant. */
11484 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11485 break;
11486 }
11487 case BUILT_IN_ATOMIC_STORE:
11488 {
11489 if (resolve_overloaded_atomic_store (loc, function, params,
11490 &new_return))
11491 return new_return;
11492 /* Change to the _N variant. */
11493 orig_code = BUILT_IN_ATOMIC_STORE_N;
11494 break;
11495 }
11496 default:
11497 gcc_unreachable ();
11498 }
11499 /* Fallthrough to the normal processing. */
11500 }
11501 case BUILT_IN_ATOMIC_EXCHANGE_N:
11502 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11503 case BUILT_IN_ATOMIC_LOAD_N:
11504 case BUILT_IN_ATOMIC_STORE_N:
11505 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11506 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11507 case BUILT_IN_ATOMIC_AND_FETCH_N:
11508 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11509 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11510 case BUILT_IN_ATOMIC_OR_FETCH_N:
11511 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11512 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11513 case BUILT_IN_ATOMIC_FETCH_AND_N:
11514 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11515 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11516 case BUILT_IN_ATOMIC_FETCH_OR_N:
11517 {
11518 orig_format = false;
11519 /* Fallthru for parameter processing. */
11520 }
2797f13a 11521 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11522 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11523 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11524 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11525 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11526 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11527 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11528 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11529 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11530 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11531 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11532 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11533 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11534 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11535 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11536 case BUILT_IN_SYNC_LOCK_RELEASE_N:
b6a5fc45 11537 {
11538 int n = sync_resolve_size (function, params);
b9c74b4d 11539 tree new_function, first_param, result;
b9a16870 11540 enum built_in_function fncode;
b6a5fc45 11541
11542 if (n == 0)
11543 return error_mark_node;
11544
b9a16870 11545 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11546 new_function = builtin_decl_explicit (fncode);
1cd6e20d 11547 if (!sync_resolve_params (loc, function, new_function, params,
11548 orig_format))
b6a5fc45 11549 return error_mark_node;
11550
f1f41a6c 11551 first_param = (*params)[0];
ec761d5a 11552 result = build_function_call_vec (loc, vNULL, new_function, params,
11553 NULL);
1cd6e20d 11554 if (result == error_mark_node)
11555 return result;
2797f13a 11556 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
1cd6e20d 11557 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
11558 && orig_code != BUILT_IN_ATOMIC_STORE_N)
11559 result = sync_resolve_return (first_param, result, orig_format);
b6a5fc45 11560
1cd6e20d 11561 /* If new_return is set, assign function to that expr and cast the
11562 result to void since the generic interface returned void. */
11563 if (new_return)
11564 {
11565 /* Cast function result from I{1,2,4,8,16} to the required type. */
11566 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11567 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11568 result);
11569 TREE_SIDE_EFFECTS (result) = 1;
11570 protected_set_expr_location (result, loc);
11571 result = convert (void_type_node, result);
11572 }
b6a5fc45 11573 return result;
11574 }
11575
11576 default:
65441f6f 11577 return NULL_TREE;
b6a5fc45 11578 }
11579}
11580
73437615 11581/* vector_types_compatible_elements_p is used in type checks of vectors
11582 values used as operands of binary operators. Where it returns true, and
11583 the other checks of the caller succeed (being vector types in he first
11584 place, and matching number of elements), we can just treat the types
11585 as essentially the same.
11586 Contrast with vector_targets_convertible_p, which is used for vector
11587 pointer types, and vector_types_convertible_p, which will allow
11588 language-specific matches under the control of flag_lax_vector_conversions,
11589 and might still require a conversion. */
11590/* True if vector types T1 and T2 can be inputs to the same binary
11591 operator without conversion.
11592 We don't check the overall vector size here because some of our callers
11593 want to give different error messages when the vectors are compatible
11594 except for the element count. */
11595
491255f5 11596bool
73437615 11597vector_types_compatible_elements_p (tree t1, tree t2)
491255f5 11598{
73437615 11599 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11600 t1 = TREE_TYPE (t1);
11601 t2 = TREE_TYPE (t2);
11602
491255f5 11603 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11604
9421ebb9 11605 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11606 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11607 || c2 == FIXED_POINT_TYPE));
491255f5 11608
73437615 11609 t1 = c_common_signed_type (t1);
11610 t2 = c_common_signed_type (t2);
491255f5 11611 /* Equality works here because c_common_signed_type uses
11612 TYPE_MAIN_VARIANT. */
73437615 11613 if (t1 == t2)
11614 return true;
11615 if (opaque && c1 == c2
11616 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11617 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11618 return true;
11619 return false;
491255f5 11620}
11621
be7350e7 11622/* Check for missing format attributes on function pointers. LTYPE is
11623 the new type or left-hand side type. RTYPE is the old type or
11624 right-hand side type. Returns TRUE if LTYPE is missing the desired
11625 attribute. */
11626
11627bool
11628check_missing_format_attribute (tree ltype, tree rtype)
11629{
11630 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11631 tree ra;
11632
11633 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11634 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11635 break;
11636 if (ra)
11637 {
11638 tree la;
11639 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11640 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11641 break;
11642 return !la;
11643 }
11644 else
11645 return false;
11646}
11647
2840aae4 11648/* Subscripting with type char is likely to lose on a machine where
11649 chars are signed. So warn on any machine, but optionally. Don't
11650 warn for unsigned char since that type is safe. Don't warn for
11651 signed char because anyone who uses that must have done so
11652 deliberately. Furthermore, we reduce the false positive load by
11653 warning only for non-constant value of type char. */
11654
11655void
92b63884 11656warn_array_subscript_with_type_char (location_t loc, tree index)
2840aae4 11657{
11658 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11659 && TREE_CODE (index) != INTEGER_CST)
92b63884 11660 warning_at (loc, OPT_Wchar_subscripts,
11661 "array subscript has type %<char%>");
2840aae4 11662}
11663
e534436e 11664/* Implement -Wparentheses for the unexpected C precedence rules, to
11665 cover cases like x + y << z which readers are likely to
11666 misinterpret. We have seen an expression in which CODE is a binary
82012ffe 11667 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11668 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11669 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11670 expression was not formed using a binary or unary operator, or it
11671 was enclosed in parentheses. */
e534436e 11672
11673void
b0e7825e 11674warn_about_parentheses (location_t loc, enum tree_code code,
269f7979 11675 enum tree_code code_left, tree arg_left,
82012ffe 11676 enum tree_code code_right, tree arg_right)
e534436e 11677{
11678 if (!warn_parentheses)
11679 return;
11680
82012ffe 11681 /* This macro tests that the expression ARG with original tree code
11682 CODE appears to be a boolean expression. or the result of folding a
11683 boolean expression. */
11684#define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11685 (truth_value_p (TREE_CODE (ARG)) \
11686 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11687 /* Folding may create 0 or 1 integers from other expressions. */ \
11688 || ((CODE) != INTEGER_CST \
11689 && (integer_onep (ARG) || integer_zerop (ARG))))
11690
48e1416a 11691 switch (code)
e534436e 11692 {
82012ffe 11693 case LSHIFT_EXPR:
b0e7825e 11694 if (code_left == PLUS_EXPR)
11695 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11696 "suggest parentheses around %<+%> inside %<<<%>");
11697 else if (code_right == PLUS_EXPR)
11698 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11699 "suggest parentheses around %<+%> inside %<<<%>");
11700 else if (code_left == MINUS_EXPR)
11701 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11702 "suggest parentheses around %<-%> inside %<<<%>");
11703 else if (code_right == MINUS_EXPR)
11704 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11705 "suggest parentheses around %<-%> inside %<<<%>");
82012ffe 11706 return;
e534436e 11707
82012ffe 11708 case RSHIFT_EXPR:
b0e7825e 11709 if (code_left == PLUS_EXPR)
11710 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11711 "suggest parentheses around %<+%> inside %<>>%>");
11712 else if (code_right == PLUS_EXPR)
11713 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11714 "suggest parentheses around %<+%> inside %<>>%>");
11715 else if (code_left == MINUS_EXPR)
11716 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11717 "suggest parentheses around %<-%> inside %<>>%>");
11718 else if (code_right == MINUS_EXPR)
11719 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11720 "suggest parentheses around %<-%> inside %<>>%>");
82012ffe 11721 return;
e534436e 11722
82012ffe 11723 case TRUTH_ORIF_EXPR:
b0e7825e 11724 if (code_left == TRUTH_ANDIF_EXPR)
11725 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11726 "suggest parentheses around %<&&%> within %<||%>");
11727 else if (code_right == TRUTH_ANDIF_EXPR)
11728 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11729 "suggest parentheses around %<&&%> within %<||%>");
82012ffe 11730 return;
11731
11732 case BIT_IOR_EXPR:
e534436e 11733 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
b0e7825e 11734 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11735 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11736 "suggest parentheses around arithmetic in operand of %<|%>");
11737 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11738 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11739 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11740 "suggest parentheses around arithmetic in operand of %<|%>");
e534436e 11741 /* Check cases like x|y==z */
b0e7825e 11742 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11743 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11744 "suggest parentheses around comparison in operand of %<|%>");
11745 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11746 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11747 "suggest parentheses around comparison in operand of %<|%>");
11748 /* Check cases like !x | y */
11749 else if (code_left == TRUTH_NOT_EXPR
11750 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 11751 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11752 "suggest parentheses around operand of "
11753 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
82012ffe 11754 return;
e534436e 11755
82012ffe 11756 case BIT_XOR_EXPR:
e534436e 11757 if (code_left == BIT_AND_EXPR
b0e7825e 11758 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11759 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11760 "suggest parentheses around arithmetic in operand of %<^%>");
11761 else if (code_right == BIT_AND_EXPR
11762 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11763 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11764 "suggest parentheses around arithmetic in operand of %<^%>");
e534436e 11765 /* Check cases like x^y==z */
b0e7825e 11766 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11767 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11768 "suggest parentheses around comparison in operand of %<^%>");
11769 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11770 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11771 "suggest parentheses around comparison in operand of %<^%>");
11772 return;
e534436e 11773
82012ffe 11774 case BIT_AND_EXPR:
b0e7825e 11775 if (code_left == PLUS_EXPR)
11776 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11777 "suggest parentheses around %<+%> in operand of %<&%>");
11778 else if (code_right == PLUS_EXPR)
11779 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11780 "suggest parentheses around %<+%> in operand of %<&%>");
b0e7825e 11781 else if (code_left == MINUS_EXPR)
11782 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11783 "suggest parentheses around %<-%> in operand of %<&%>");
11784 else if (code_right == MINUS_EXPR)
11785 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11786 "suggest parentheses around %<-%> in operand of %<&%>");
e534436e 11787 /* Check cases like x&y==z */
b0e7825e 11788 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11789 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11790 "suggest parentheses around comparison in operand of %<&%>");
11791 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11792 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11793 "suggest parentheses around comparison in operand of %<&%>");
11794 /* Check cases like !x & y */
11795 else if (code_left == TRUTH_NOT_EXPR
11796 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
b0e7825e 11797 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11798 "suggest parentheses around operand of "
11799 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
82012ffe 11800 return;
e534436e 11801
82012ffe 11802 case EQ_EXPR:
b0e7825e 11803 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11804 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11805 "suggest parentheses around comparison in operand of %<==%>");
11806 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11807 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11808 "suggest parentheses around comparison in operand of %<==%>");
11809 return;
11810 case NE_EXPR:
b0e7825e 11811 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11812 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11813 "suggest parentheses around comparison in operand of %<!=%>");
11814 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11815 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
82012ffe 11816 "suggest parentheses around comparison in operand of %<!=%>");
11817 return;
11818
11819 default:
b0e7825e 11820 if (TREE_CODE_CLASS (code) == tcc_comparison)
11821 {
11822 if (TREE_CODE_CLASS (code_left) == tcc_comparison
269f7979 11823 && code_left != NE_EXPR && code_left != EQ_EXPR
11824 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
b0e7825e 11825 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11826 "comparisons like %<X<=Y<=Z%> do not "
11827 "have their mathematical meaning");
11828 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
269f7979 11829 && code_right != NE_EXPR && code_right != EQ_EXPR
b0e7825e 11830 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11831 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11832 "comparisons like %<X<=Y<=Z%> do not "
11833 "have their mathematical meaning");
11834 }
82012ffe 11835 return;
6ce0c450 11836 }
82012ffe 11837#undef NOT_A_BOOLEAN_EXPR_P
e534436e 11838}
11839
92fccaaa 11840/* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11841
11842void
11843warn_for_unused_label (tree label)
11844{
11845 if (!TREE_USED (label))
11846 {
11847 if (DECL_INITIAL (label))
11848 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11849 else
11850 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11851 }
11852}
2840aae4 11853
b6889cb0 11854/* Warn for division by zero according to the value of DIVISOR. LOC
11855 is the location of the division operator. */
f092582b 11856
11857void
b6889cb0 11858warn_for_div_by_zero (location_t loc, tree divisor)
f092582b 11859{
9421ebb9 11860 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11861 about division by zero. Do not issue a warning if DIVISOR has a
f092582b 11862 floating-point type, since we consider 0.0/0.0 a valid way of
11863 generating a NaN. */
48d94ede 11864 if (c_inhibit_evaluation_warnings == 0
9421ebb9 11865 && (integer_zerop (divisor) || fixed_zerop (divisor)))
b6889cb0 11866 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
f092582b 11867}
11868
13869a99 11869/* Subroutine of build_binary_op. Give warnings for comparisons
11870 between signed and unsigned quantities that may fail. Do the
11871 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11872 so that casts will be considered, but default promotions won't
8e70fb09 11873 be.
11874
11875 LOCATION is the location of the comparison operator.
13869a99 11876
11877 The arguments of this function map directly to local variables
11878 of build_binary_op. */
11879
48e1416a 11880void
8e70fb09 11881warn_for_sign_compare (location_t location,
48e1416a 11882 tree orig_op0, tree orig_op1,
11883 tree op0, tree op1,
13869a99 11884 tree result_type, enum tree_code resultcode)
11885{
11886 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11887 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11888 int unsignedp0, unsignedp1;
48e1416a 11889
13869a99 11890 /* In C++, check for comparison of different enum types. */
11891 if (c_dialect_cxx()
11892 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11893 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11894 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
895b662f 11895 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
13869a99 11896 {
8e70fb09 11897 warning_at (location,
11898 OPT_Wsign_compare, "comparison between types %qT and %qT",
11899 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
13869a99 11900 }
11901
11902 /* Do not warn if the comparison is being done in a signed type,
11903 since the signed type will only be chosen if it can represent
11904 all the values of the unsigned type. */
11905 if (!TYPE_UNSIGNED (result_type))
11906 /* OK */;
11907 /* Do not warn if both operands are unsigned. */
11908 else if (op0_signed == op1_signed)
11909 /* OK */;
11910 else
11911 {
895b662f 11912 tree sop, uop, base_type;
13869a99 11913 bool ovf;
895b662f 11914
13869a99 11915 if (op0_signed)
11916 sop = orig_op0, uop = orig_op1;
48e1416a 11917 else
13869a99 11918 sop = orig_op1, uop = orig_op0;
11919
48e1416a 11920 STRIP_TYPE_NOPS (sop);
13869a99 11921 STRIP_TYPE_NOPS (uop);
895b662f 11922 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
11923 ? TREE_TYPE (result_type) : result_type);
13869a99 11924
11925 /* Do not warn if the signed quantity is an unsuffixed integer
11926 literal (or some static constant expression involving such
11927 literals or a conditional expression involving such literals)
11928 and it is non-negative. */
11929 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
11930 /* OK */;
11931 /* Do not warn if the comparison is an equality operation, the
11932 unsigned quantity is an integral constant, and it would fit
11933 in the result if the result were signed. */
11934 else if (TREE_CODE (uop) == INTEGER_CST
11935 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
895b662f 11936 && int_fits_type_p (uop, c_common_signed_type (base_type)))
13869a99 11937 /* OK */;
11938 /* In C, do not warn if the unsigned quantity is an enumeration
11939 constant and its maximum value would fit in the result if the
11940 result were signed. */
11941 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
11942 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
11943 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
895b662f 11944 c_common_signed_type (base_type)))
13869a99 11945 /* OK */;
48e1416a 11946 else
8e70fb09 11947 warning_at (location,
48e1416a 11948 OPT_Wsign_compare,
8e70fb09 11949 "comparison between signed and unsigned integer expressions");
13869a99 11950 }
48e1416a 11951
13869a99 11952 /* Warn if two unsigned values are being compared in a size larger
11953 than their original size, and one (and only one) is the result of
11954 a `~' operator. This comparison will always fail.
48e1416a 11955
13869a99 11956 Also warn if one operand is a constant, and the constant does not
11957 have all bits set that are set in the ~ operand when it is
11958 extended. */
11959
7f506bca 11960 op0 = c_common_get_narrower (op0, &unsignedp0);
11961 op1 = c_common_get_narrower (op1, &unsignedp1);
48e1416a 11962
13869a99 11963 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
11964 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
11965 {
11966 if (TREE_CODE (op0) == BIT_NOT_EXPR)
7f506bca 11967 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
13869a99 11968 if (TREE_CODE (op1) == BIT_NOT_EXPR)
7f506bca 11969 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
13869a99 11970
e913b5cd 11971 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
13869a99 11972 {
11973 tree primop;
11974 HOST_WIDE_INT constant, mask;
11975 int unsignedp;
11976 unsigned int bits;
48e1416a 11977
e913b5cd 11978 if (tree_fits_shwi_p (op0))
13869a99 11979 {
11980 primop = op1;
11981 unsignedp = unsignedp1;
e913b5cd 11982 constant = tree_to_shwi (op0);
13869a99 11983 }
11984 else
11985 {
11986 primop = op0;
11987 unsignedp = unsignedp0;
e913b5cd 11988 constant = tree_to_shwi (op1);
13869a99 11989 }
48e1416a 11990
13869a99 11991 bits = TYPE_PRECISION (TREE_TYPE (primop));
11992 if (bits < TYPE_PRECISION (result_type)
11993 && bits < HOST_BITS_PER_LONG && unsignedp)
11994 {
11995 mask = (~ (HOST_WIDE_INT) 0) << bits;
11996 if ((mask & constant) != mask)
11997 {
11998 if (constant == 0)
76fdceeb 11999 warning_at (location, OPT_Wsign_compare,
12000 "promoted ~unsigned is always non-zero");
13869a99 12001 else
48e1416a 12002 warning_at (location, OPT_Wsign_compare,
8e70fb09 12003 "comparison of promoted ~unsigned with constant");
13869a99 12004 }
12005 }
12006 }
12007 else if (unsignedp0 && unsignedp1
12008 && (TYPE_PRECISION (TREE_TYPE (op0))
12009 < TYPE_PRECISION (result_type))
12010 && (TYPE_PRECISION (TREE_TYPE (op1))
12011 < TYPE_PRECISION (result_type)))
8e70fb09 12012 warning_at (location, OPT_Wsign_compare,
13869a99 12013 "comparison of promoted ~unsigned with unsigned");
12014 }
12015}
12016
5ba33bf4 12017/* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
12018 type via c_common_type. If -Wdouble-promotion is in use, and the
12019 conditions for warning have been met, issue a warning. GMSGID is
12020 the warning message. It must have two %T specifiers for the type
12021 that was converted (generally "float") and the type to which it was
12022 converted (generally "double), respectively. LOC is the location
12023 to which the awrning should refer. */
12024
12025void
12026do_warn_double_promotion (tree result_type, tree type1, tree type2,
12027 const char *gmsgid, location_t loc)
12028{
12029 tree source_type;
12030
12031 if (!warn_double_promotion)
12032 return;
12033 /* If the conversion will not occur at run-time, there is no need to
12034 warn about it. */
12035 if (c_inhibit_evaluation_warnings)
12036 return;
12037 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
12038 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
12039 return;
12040 if (TYPE_MAIN_VARIANT (type1) == float_type_node
12041 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
12042 source_type = type1;
12043 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
12044 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
12045 source_type = type2;
12046 else
12047 return;
12048 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
12049}
12050
41771881 12051/* Setup a TYPE_DECL node as a typedef representation.
12052
12053 X is a TYPE_DECL for a typedef statement. Create a brand new
12054 ..._TYPE node (which will be just a variant of the existing
12055 ..._TYPE node with identical properties) and then install X
12056 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
12057
12058 The whole point here is to end up with a situation where each
12059 and every ..._TYPE node the compiler creates will be uniquely
12060 associated with AT MOST one node representing a typedef name.
12061 This way, even though the compiler substitutes corresponding
12062 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
12063 early on, later parts of the compiler can always do the reverse
12064 translation and get back the corresponding typedef name. For
12065 example, given:
ab2c1de8 12066
41771881 12067 typedef struct S MY_TYPE;
12068 MY_TYPE object;
12069
12070 Later parts of the compiler might only know that `object' was of
12071 type `struct S' if it were not for code just below. With this
12072 code however, later parts of the compiler see something like:
12073
12074 struct S' == struct S
12075 typedef struct S' MY_TYPE;
12076 struct S' object;
12077
12078 And they can then deduce (from the node for type struct S') that
12079 the original object declaration was:
12080
12081 MY_TYPE object;
12082
12083 Being able to do this is important for proper support of protoize,
12084 and also for generating precise symbolic debugging information
12085 which takes full account of the programmer's (typedef) vocabulary.
12086
12087 Obviously, we don't want to generate a duplicate ..._TYPE node if
12088 the TYPE_DECL node that we are now processing really represents a
12089 standard built-in type. */
12090
12091void
12092set_underlying_type (tree x)
12093{
12094 if (x == error_mark_node)
12095 return;
12096 if (DECL_IS_BUILTIN (x))
12097 {
12098 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12099 TYPE_NAME (TREE_TYPE (x)) = x;
12100 }
12101 else if (TREE_TYPE (x) != error_mark_node
12102 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12103 {
12104 tree tt = TREE_TYPE (x);
12105 DECL_ORIGINAL_TYPE (x) = tt;
12106 tt = build_variant_type_copy (tt);
12107 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12108 TYPE_NAME (tt) = x;
12109 TREE_USED (tt) = TREE_USED (x);
12110 TREE_TYPE (x) = tt;
12111 }
12112}
12113
1a4c44c5 12114/* Record the types used by the current global variable declaration
12115 being parsed, so that we can decide later to emit their debug info.
12116 Those types are in types_used_by_cur_var_decl, and we are going to
12117 store them in the types_used_by_vars_hash hash table.
12118 DECL is the declaration of the global variable that has been parsed. */
12119
12120void
12121record_types_used_by_current_var_decl (tree decl)
12122{
12123 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12124
f1f41a6c 12125 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
1a4c44c5 12126 {
f1f41a6c 12127 tree type = types_used_by_cur_var_decl->pop ();
aef48c9a 12128 types_used_by_var_decl_insert (type, decl);
1a4c44c5 12129 }
12130}
12131
a4e3ffad 12132/* If DECL is a typedef that is declared in the current function,
12133 record it for the purpose of -Wunused-local-typedefs. */
12134
12135void
12136record_locally_defined_typedef (tree decl)
12137{
12138 struct c_language_function *l;
12139
12140 if (!warn_unused_local_typedefs
12141 || cfun == NULL
12142 /* if this is not a locally defined typedef then we are not
12143 interested. */
12144 || !is_typedef_decl (decl)
12145 || !decl_function_context (decl))
12146 return;
12147
12148 l = (struct c_language_function *) cfun->language;
f1f41a6c 12149 vec_safe_push (l->local_typedefs, decl);
a4e3ffad 12150}
12151
12152/* If T is a TYPE_DECL declared locally, mark it as used. */
12153
12154void
12155maybe_record_typedef_use (tree t)
12156{
12157 if (!is_typedef_decl (t))
12158 return;
12159
12160 TREE_USED (t) = true;
12161}
12162
12163/* Warn if there are some unused locally defined typedefs in the
12164 current function. */
12165
12166void
12167maybe_warn_unused_local_typedefs (void)
12168{
12169 int i;
12170 tree decl;
12171 /* The number of times we have emitted -Wunused-local-typedefs
12172 warnings. If this is different from errorcount, that means some
12173 unrelated errors have been issued. In which case, we'll avoid
12174 emitting "unused-local-typedefs" warnings. */
12175 static int unused_local_typedefs_warn_count;
12176 struct c_language_function *l;
12177
12178 if (cfun == NULL)
12179 return;
12180
12181 if ((l = (struct c_language_function *) cfun->language) == NULL)
12182 return;
12183
12184 if (warn_unused_local_typedefs
12185 && errorcount == unused_local_typedefs_warn_count)
12186 {
f1f41a6c 12187 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
a4e3ffad 12188 if (!TREE_USED (decl))
12189 warning_at (DECL_SOURCE_LOCATION (decl),
12190 OPT_Wunused_local_typedefs,
12191 "typedef %qD locally defined but not used", decl);
12192 unused_local_typedefs_warn_count = errorcount;
12193 }
12194
f1f41a6c 12195 vec_free (l->local_typedefs);
a4e3ffad 12196}
12197
78bf4156 12198/* Warn about boolean expression compared with an integer value different
12199 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12200 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12201 are the operands of the comparison. The caller must ensure that
12202 either operand is a boolean expression. */
12203
12204void
12205maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12206 tree op1)
12207{
12208 if (TREE_CODE_CLASS (code) != tcc_comparison)
12209 return;
12210
12211 tree cst = (TREE_CODE (op0) == INTEGER_CST)
12212 ? op0 : (TREE_CODE (op1) == INTEGER_CST) ? op1 : NULL_TREE;
12213 if (!cst)
12214 return;
12215
12216 if (!integer_zerop (cst) && !integer_onep (cst))
12217 {
a720ab1c 12218 int sign = (TREE_CODE (op0) == INTEGER_CST
12219 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
78bf4156 12220 if (code == EQ_EXPR
12221 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12222 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12223 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12224 "with boolean expression is always false", cst);
12225 else
12226 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12227 "with boolean expression is always true", cst);
12228 }
a720ab1c 12229 else if (integer_zerop (cst) || integer_onep (cst))
12230 {
14744a16 12231 /* If the non-constant operand isn't of a boolean type, we
12232 don't want to warn here. */
12233 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12234 /* Handle booleans promoted to integers. */
12235 if (CONVERT_EXPR_P (noncst)
12236 && TREE_TYPE (noncst) == integer_type_node
12237 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
12238 /* Warn. */;
12239 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12240 && !truth_value_p (TREE_CODE (noncst)))
12241 return;
a720ab1c 12242 /* Do some magic to get the right diagnostics. */
12243 bool flag = TREE_CODE (op0) == INTEGER_CST;
12244 flag = integer_zerop (cst) ? flag : !flag;
12245 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12246 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12247 "with boolean expression is always true", cst);
12248 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12249 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12250 "with boolean expression is always false", cst);
12251 }
78bf4156 12252}
12253
f352a3fb 12254/* The C and C++ parsers both use vectors to hold function arguments.
12255 For efficiency, we keep a cache of unused vectors. This is the
12256 cache. */
12257
f1f41a6c 12258typedef vec<tree, va_gc> *tree_gc_vec;
12259static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
f352a3fb 12260
12261/* Return a new vector from the cache. If the cache is empty,
12262 allocate a new vector. These vectors are GC'ed, so it is OK if the
12263 pointer is not released.. */
12264
f1f41a6c 12265vec<tree, va_gc> *
f352a3fb 12266make_tree_vector (void)
12267{
f1f41a6c 12268 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12269 return tree_vector_cache->pop ();
f352a3fb 12270 else
12271 {
f1f41a6c 12272 /* Passing 0 to vec::alloc returns NULL, and our callers require
f352a3fb 12273 that we always return a non-NULL value. The vector code uses
12274 4 when growing a NULL vector, so we do too. */
f1f41a6c 12275 vec<tree, va_gc> *v;
12276 vec_alloc (v, 4);
12277 return v;
f352a3fb 12278 }
12279}
12280
12281/* Release a vector of trees back to the cache. */
12282
12283void
f1f41a6c 12284release_tree_vector (vec<tree, va_gc> *vec)
f352a3fb 12285{
12286 if (vec != NULL)
12287 {
f1f41a6c 12288 vec->truncate (0);
12289 vec_safe_push (tree_vector_cache, vec);
f352a3fb 12290 }
12291}
12292
12293/* Get a new tree vector holding a single tree. */
12294
f1f41a6c 12295vec<tree, va_gc> *
f352a3fb 12296make_tree_vector_single (tree t)
12297{
f1f41a6c 12298 vec<tree, va_gc> *ret = make_tree_vector ();
12299 ret->quick_push (t);
f352a3fb 12300 return ret;
12301}
12302
c66c81be 12303/* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12304
f1f41a6c 12305vec<tree, va_gc> *
c66c81be 12306make_tree_vector_from_list (tree list)
12307{
f1f41a6c 12308 vec<tree, va_gc> *ret = make_tree_vector ();
c66c81be 12309 for (; list; list = TREE_CHAIN (list))
f1f41a6c 12310 vec_safe_push (ret, TREE_VALUE (list));
c66c81be 12311 return ret;
12312}
12313
f352a3fb 12314/* Get a new tree vector which is a copy of an existing one. */
12315
f1f41a6c 12316vec<tree, va_gc> *
12317make_tree_vector_copy (const vec<tree, va_gc> *orig)
f352a3fb 12318{
f1f41a6c 12319 vec<tree, va_gc> *ret;
f352a3fb 12320 unsigned int ix;
12321 tree t;
12322
12323 ret = make_tree_vector ();
f1f41a6c 12324 vec_safe_reserve (ret, vec_safe_length (orig));
12325 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12326 ret->quick_push (t);
f352a3fb 12327 return ret;
12328}
12329
a9ffdd35 12330/* Return true if KEYWORD starts a type specifier. */
12331
12332bool
12333keyword_begins_type_specifier (enum rid keyword)
12334{
12335 switch (keyword)
12336 {
4fba5eb9 12337 case RID_AUTO_TYPE:
a9ffdd35 12338 case RID_INT:
12339 case RID_CHAR:
12340 case RID_FLOAT:
12341 case RID_DOUBLE:
12342 case RID_VOID:
a9ffdd35 12343 case RID_UNSIGNED:
12344 case RID_LONG:
12345 case RID_SHORT:
12346 case RID_SIGNED:
12347 case RID_DFLOAT32:
12348 case RID_DFLOAT64:
12349 case RID_DFLOAT128:
12350 case RID_FRACT:
12351 case RID_ACCUM:
12352 case RID_BOOL:
12353 case RID_WCHAR:
12354 case RID_CHAR16:
12355 case RID_CHAR32:
12356 case RID_SAT:
12357 case RID_COMPLEX:
12358 case RID_TYPEOF:
12359 case RID_STRUCT:
12360 case RID_CLASS:
12361 case RID_UNION:
12362 case RID_ENUM:
12363 return true;
12364 default:
9f75f026 12365 if (keyword >= RID_FIRST_INT_N
12366 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12367 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12368 return true;
a9ffdd35 12369 return false;
12370 }
12371}
12372
12373/* Return true if KEYWORD names a type qualifier. */
12374
12375bool
12376keyword_is_type_qualifier (enum rid keyword)
12377{
12378 switch (keyword)
12379 {
12380 case RID_CONST:
12381 case RID_VOLATILE:
12382 case RID_RESTRICT:
b560fabd 12383 case RID_ATOMIC:
a9ffdd35 12384 return true;
12385 default:
12386 return false;
12387 }
12388}
12389
12390/* Return true if KEYWORD names a storage class specifier.
12391
12392 RID_TYPEDEF is not included in this list despite `typedef' being
12393 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12394 such for syntactic convenience only. */
12395
12396bool
12397keyword_is_storage_class_specifier (enum rid keyword)
12398{
12399 switch (keyword)
12400 {
12401 case RID_STATIC:
12402 case RID_EXTERN:
12403 case RID_REGISTER:
12404 case RID_AUTO:
12405 case RID_MUTABLE:
12406 case RID_THREAD:
12407 return true;
12408 default:
12409 return false;
12410 }
12411}
12412
fad3f658 12413/* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12414
12415static bool
12416keyword_is_function_specifier (enum rid keyword)
12417{
12418 switch (keyword)
12419 {
12420 case RID_INLINE:
985c6e3a 12421 case RID_NORETURN:
fad3f658 12422 case RID_VIRTUAL:
12423 case RID_EXPLICIT:
12424 return true;
12425 default:
12426 return false;
12427 }
12428}
12429
12430/* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12431 declaration-specifier (C99 6.7). */
12432
12433bool
12434keyword_is_decl_specifier (enum rid keyword)
12435{
12436 if (keyword_is_storage_class_specifier (keyword)
12437 || keyword_is_type_qualifier (keyword)
12438 || keyword_is_function_specifier (keyword))
12439 return true;
12440
12441 switch (keyword)
12442 {
12443 case RID_TYPEDEF:
12444 case RID_FRIEND:
12445 case RID_CONSTEXPR:
12446 return true;
12447 default:
12448 return false;
12449 }
12450}
12451
9b88d08d 12452/* Initialize language-specific-bits of tree_contains_struct. */
12453
12454void
12455c_common_init_ts (void)
12456{
12457 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12458 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
3c6d4197 12459 MARK_TS_TYPED (ARRAY_NOTATION_REF);
9b88d08d 12460}
12461
244db24d 12462/* Build a user-defined numeric literal out of an integer constant type VALUE
12463 with identifier SUFFIX. */
12464
12465tree
324ca377 12466build_userdef_literal (tree suffix_id, tree value,
12467 enum overflow_type overflow, tree num_string)
244db24d 12468{
12469 tree literal = make_node (USERDEF_LITERAL);
12470 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12471 USERDEF_LITERAL_VALUE (literal) = value;
324ca377 12472 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
244db24d 12473 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12474 return literal;
12475}
12476
7059d45d 12477/* For vector[index], convert the vector to a
c61ef207 12478 pointer of the underlying type. Return true if the resulting
12479 ARRAY_REF should not be an lvalue. */
12480
12481bool
7059d45d 12482convert_vector_to_pointer_for_subscript (location_t loc,
c61ef207 12483 tree *vecp, tree index)
7059d45d 12484{
c61ef207 12485 bool ret = false;
7059d45d 12486 if (TREE_CODE (TREE_TYPE (*vecp)) == VECTOR_TYPE)
12487 {
12488 tree type = TREE_TYPE (*vecp);
12489 tree type1;
12490
c61ef207 12491 ret = !lvalue_p (*vecp);
7059d45d 12492 if (TREE_CODE (index) == INTEGER_CST)
e913b5cd 12493 if (!tree_fits_uhwi_p (index)
aa59f000 12494 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7059d45d 12495 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12496
c61ef207 12497 if (ret)
12498 {
f9e245b2 12499 tree tmp = create_tmp_var_raw (type);
c61ef207 12500 DECL_SOURCE_LOCATION (tmp) = loc;
12501 *vecp = c_save_expr (*vecp);
12502 if (TREE_CODE (*vecp) == C_MAYBE_CONST_EXPR)
12503 {
12504 bool non_const = C_MAYBE_CONST_EXPR_NON_CONST (*vecp);
12505 *vecp = C_MAYBE_CONST_EXPR_EXPR (*vecp);
12506 *vecp
12507 = c_wrap_maybe_const (build4 (TARGET_EXPR, type, tmp,
12508 *vecp, NULL_TREE, NULL_TREE),
12509 non_const);
12510 }
12511 else
12512 *vecp = build4 (TARGET_EXPR, type, tmp, *vecp,
12513 NULL_TREE, NULL_TREE);
12514 SET_EXPR_LOCATION (*vecp, loc);
12515 c_common_mark_addressable_vec (tmp);
12516 }
12517 else
12518 c_common_mark_addressable_vec (*vecp);
7059d45d 12519 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
7059d45d 12520 type1 = build_pointer_type (TREE_TYPE (*vecp));
b2ca6510 12521 bool ref_all = TYPE_REF_CAN_ALIAS_ALL (type1);
12522 if (!ref_all
12523 && !DECL_P (*vecp))
12524 {
12525 /* If the original vector isn't declared may_alias and it
12526 isn't a bare vector look if the subscripting would
12527 alias the vector we subscript, and if not, force ref-all. */
12528 alias_set_type vecset = get_alias_set (*vecp);
12529 alias_set_type sset = get_alias_set (type);
12530 if (!alias_sets_must_conflict_p (sset, vecset)
12531 && !alias_set_subset_of (sset, vecset))
12532 ref_all = true;
12533 }
12534 type = build_pointer_type_for_mode (type, ptr_mode, ref_all);
7059d45d 12535 *vecp = build1 (ADDR_EXPR, type1, *vecp);
12536 *vecp = convert (type, *vecp);
12537 }
c61ef207 12538 return ret;
7059d45d 12539}
12540
41ed701a 12541/* Determine which of the operands, if any, is a scalar that needs to be
12542 converted to a vector, for the range of operations. */
12543enum stv_conv
12544scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12545 bool complain)
12546{
12547 tree type0 = TREE_TYPE (op0);
12548 tree type1 = TREE_TYPE (op1);
12549 bool integer_only_op = false;
12550 enum stv_conv ret = stv_firstarg;
12551
12552 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
12553 || TREE_CODE (type1) == VECTOR_TYPE);
12554 switch (code)
12555 {
12556 /* Most GENERIC binary expressions require homogeneous arguments.
12557 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12558 argument that is a vector and a second one that is a scalar, so
12559 we never return stv_secondarg for them. */
12560 case RSHIFT_EXPR:
12561 case LSHIFT_EXPR:
12562 if (TREE_CODE (type0) == INTEGER_TYPE
12563 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12564 {
22a75734 12565 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12566 {
12567 if (complain)
12568 error_at (loc, "conversion of scalar %qT to vector %qT "
12569 "involves truncation", type0, type1);
12570 return stv_error;
12571 }
12572 else
12573 return stv_firstarg;
12574 }
12575 break;
12576
12577 case BIT_IOR_EXPR:
12578 case BIT_XOR_EXPR:
12579 case BIT_AND_EXPR:
12580 integer_only_op = true;
12581 /* ... fall through ... */
12582
7b463b19 12583 case VEC_COND_EXPR:
12584
41ed701a 12585 case PLUS_EXPR:
12586 case MINUS_EXPR:
12587 case MULT_EXPR:
12588 case TRUNC_DIV_EXPR:
12589 case CEIL_DIV_EXPR:
12590 case FLOOR_DIV_EXPR:
12591 case ROUND_DIV_EXPR:
12592 case EXACT_DIV_EXPR:
12593 case TRUNC_MOD_EXPR:
12594 case FLOOR_MOD_EXPR:
12595 case RDIV_EXPR:
12596 case EQ_EXPR:
12597 case NE_EXPR:
12598 case LE_EXPR:
12599 case GE_EXPR:
12600 case LT_EXPR:
12601 case GT_EXPR:
12602 /* What about UNLT_EXPR? */
12603 if (TREE_CODE (type0) == VECTOR_TYPE)
12604 {
41ed701a 12605 ret = stv_secondarg;
a4f59596 12606 std::swap (type0, type1);
12607 std::swap (op0, op1);
41ed701a 12608 }
12609
12610 if (TREE_CODE (type0) == INTEGER_TYPE
12611 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12612 {
22a75734 12613 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12614 {
12615 if (complain)
12616 error_at (loc, "conversion of scalar %qT to vector %qT "
12617 "involves truncation", type0, type1);
12618 return stv_error;
12619 }
12620 return ret;
12621 }
12622 else if (!integer_only_op
12623 /* Allow integer --> real conversion if safe. */
12624 && (TREE_CODE (type0) == REAL_TYPE
12625 || TREE_CODE (type0) == INTEGER_TYPE)
12626 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12627 {
22a75734 12628 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
41ed701a 12629 {
12630 if (complain)
12631 error_at (loc, "conversion of scalar %qT to vector %qT "
12632 "involves truncation", type0, type1);
12633 return stv_error;
12634 }
12635 return ret;
12636 }
12637 default:
12638 break;
12639 }
12640
12641 return stv_nothing;
12642}
12643
ffcdbf9c 12644/* Return true iff ALIGN is an integral constant that is a fundamental
12645 alignment, as defined by [basic.align] in the c++-11
12646 specifications.
12647
12648 That is:
12649
12650 [A fundamental alignment is represented by an alignment less than or
12651 equal to the greatest alignment supported by the implementation
12652 in all contexts, which is equal to
12653 alignof(max_align_t)]. */
12654
12655bool
12656cxx_fundamental_alignment_p (unsigned align)
12657{
12658 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12659 TYPE_ALIGN (long_double_type_node)));
12660}
12661
46da3601 12662/* Return true if T is a pointer to a zero-sized aggregate. */
12663
12664bool
12665pointer_to_zero_sized_aggr_p (tree t)
12666{
12667 if (!POINTER_TYPE_P (t))
12668 return false;
12669 t = TREE_TYPE (t);
12670 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12671}
12672
7bedc3a0 12673#include "gt-c-family-c-common.h"