]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-common.c
Update .po files.
[thirdparty/gcc.git] / gcc / c-family / c-common.c
CommitLineData
b0fc3e72 1/* Subroutines shared by all languages that are variants of C.
fbd26352 2 Copyright (C) 1992-2019 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"
4cba6f60 25#include "target.h"
26#include "function.h"
4cba6f60 27#include "tree.h"
ea36272b 28#include "memmodel.h"
7ff8db31 29#include "c-common.h"
4cba6f60 30#include "gimple-expr.h"
31#include "tm_p.h"
32#include "stringpool.h"
33#include "cgraph.h"
34#include "diagnostic.h"
e48d0f41 35#include "intl.h"
9ed99284 36#include "stor-layout.h"
37#include "calls.h"
9ed99284 38#include "attribs.h"
39#include "varasm.h"
40#include "trans-mem.h"
6c536c4f 41#include "c-objc.h"
218e3e4e 42#include "common/common-target.h"
96554925 43#include "langhooks.h"
f3dde807 44#include "tree-inline.h"
69579044 45#include "toplev.h"
4ee9c684 46#include "tree-iterator.h"
e08bd2f4 47#include "opts.h"
a8783bee 48#include "gimplify.h"
d4166bdc 49#include "substring-locations.h"
5c8151fa 50#include "spellcheck.h"
d76863c8 51#include "selftest.h"
fd6f6435 52
90cc7820 53cpp_reader *parse_in; /* Declared in c-pragma.h. */
a654e028 54
de801c28 55/* Mode used to build pointers (VOIDmode means ptr_mode). */
56
57machine_mode c_default_pointer_mode = VOIDmode;
58
72040e7e 59/* The following symbols are subsumed in the c_global_trees array, and
44e9fa65 60 listed here individually for documentation purposes.
72040e7e 61
62 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
63
64 tree short_integer_type_node;
65 tree long_integer_type_node;
66 tree long_long_integer_type_node;
67
68 tree short_unsigned_type_node;
69 tree long_unsigned_type_node;
70 tree long_long_unsigned_type_node;
71
3c2239cf 72 tree truthvalue_type_node;
73 tree truthvalue_false_node;
74 tree truthvalue_true_node;
72040e7e 75
76 tree ptrdiff_type_node;
77
78 tree unsigned_char_type_node;
79 tree signed_char_type_node;
80 tree wchar_type_node;
72040e7e 81
82362779 82 tree char8_type_node;
924bbf02 83 tree char16_type_node;
84 tree char32_type_node;
85
72040e7e 86 tree float_type_node;
87 tree double_type_node;
88 tree long_double_type_node;
89
90 tree complex_integer_type_node;
91 tree complex_float_type_node;
92 tree complex_double_type_node;
93 tree complex_long_double_type_node;
94
c4503c0a 95 tree dfloat32_type_node;
96 tree dfloat64_type_node;
97 tree_dfloat128_type_node;
98
72040e7e 99 tree intQI_type_node;
100 tree intHI_type_node;
101 tree intSI_type_node;
102 tree intDI_type_node;
103 tree intTI_type_node;
104
105 tree unsigned_intQI_type_node;
106 tree unsigned_intHI_type_node;
107 tree unsigned_intSI_type_node;
108 tree unsigned_intDI_type_node;
109 tree unsigned_intTI_type_node;
110
111 tree widest_integer_literal_type_node;
112 tree widest_unsigned_literal_type_node;
113
114 Nodes for types `void *' and `const void *'.
115
116 tree ptr_type_node, const_ptr_type_node;
117
118 Nodes for types `char *' and `const char *'.
119
120 tree string_type_node, const_string_type_node;
121
122 Type `char[SOMENUMBER]'.
123 Used when an array of char is needed and the size is irrelevant.
124
125 tree char_array_type_node;
126
72040e7e 127 Type `wchar_t[SOMENUMBER]' or something like it.
128 Used when a wide string literal is created.
129
130 tree wchar_array_type_node;
131
82362779 132 Type `char8_t[SOMENUMBER]' or something like it.
133 Used when a UTF-8 string literal is created.
134
135 tree char8_array_type_node;
136
924bbf02 137 Type `char16_t[SOMENUMBER]' or something like it.
138 Used when a UTF-16 string literal is created.
139
140 tree char16_array_type_node;
141
142 Type `char32_t[SOMENUMBER]' or something like it.
143 Used when a UTF-32 string literal is created.
144
145 tree char32_array_type_node;
146
72040e7e 147 Type `int ()' -- used for implicit declaration of functions.
148
149 tree default_function_type;
150
72040e7e 151 A VOID_TYPE node, packaged in a TREE_LIST.
152
153 tree void_list_node;
154
734c98be 155 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
65b7f83f 156 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
157 VAR_DECLS, but C++ does.)
71d9fc9b 158
65b7f83f 159 tree function_name_decl_node;
734c98be 160 tree pretty_function_name_decl_node;
65b7f83f 161 tree c99_function_name_decl_node;
162
163 Stack of nested function name VAR_DECLs.
1cae46be 164
65b7f83f 165 tree saved_function_name_decls;
71d9fc9b 166
72040e7e 167*/
168
169tree c_global_trees[CTI_MAX];
573aba85 170\f
574a6990 171/* Switches common to the C front ends. */
172
f7070933 173/* Nonzero means don't output line number information. */
174
175char flag_no_line_commands;
176
177/* Nonzero causes -E output not to be done, but directives such as
178 #define that have side effects are still obeyed. */
179
180char flag_no_output;
181
182/* Nonzero means dump macros in some fashion. */
183
184char flag_dump_macros;
185
186/* Nonzero means pass #include lines through to the output. */
187
188char flag_dump_includes;
189
d718b525 190/* Nonzero means process PCH files while preprocessing. */
191
192bool flag_pch_preprocess;
193
573aba85 194/* The file name to which we should write a precompiled header, or
195 NULL if no header will be written in this compile. */
196
197const char *pch_file;
198
1ed9d5f5 199/* Nonzero if an ISO standard was selected. It rejects macros in the
200 user's namespace. */
201int flag_iso;
202
574a6990 203/* C/ObjC language option variables. */
204
205
574a6990 206/* Nonzero means allow type mismatches in conditional expressions;
207 just make their values `void'. */
208
209int flag_cond_mismatch;
210
211/* Nonzero means enable C89 Amendment 1 features. */
212
213int flag_isoc94;
214
32074525 215/* Nonzero means use the ISO C99 (or C11) dialect of C. */
574a6990 216
217int flag_isoc99;
218
32074525 219/* Nonzero means use the ISO C11 dialect of C. */
39012afb 220
32074525 221int flag_isoc11;
39012afb 222
fcbe1d64 223/* Nonzero means use the ISO C2X dialect of C. */
224
225int flag_isoc2x;
226
8b332087 227/* Nonzero means that we have builtin functions, and main is an int. */
574a6990 228
229int flag_hosted = 1;
230
574a6990 231
232/* ObjC language option variables. */
233
234
574a6990 235/* Tells the compiler that this is a special run. Do not perform any
236 compiling, instead we are to test some platform dependent features
237 and output a C header file with appropriate definitions. */
238
239int print_struct_values;
240
f0b5f617 241/* Tells the compiler what is the constant string class for ObjC. */
574a6990 242
243const char *constant_string_class_name;
244
574a6990 245
246/* C++ language option variables. */
247
229a58b1 248/* The reference version of the ABI for -Wabi. */
249
250int warn_abi_version = -1;
574a6990 251
574a6990 252/* Nonzero means generate separate instantiation control files and
253 juggle them at link time. */
254
255int flag_use_repository;
256
d875b9d2 257/* The C++ dialect being used. Default set in c_common_post_options. */
0fe6eeac 258
d875b9d2 259enum cxx_dialect cxx_dialect = cxx_unset;
0fe6eeac 260
9dcd0d49 261/* Maximum template instantiation depth. This limit exists to limit the
8ce59854 262 time it takes to notice excessively recursive template instantiations.
574a6990 263
8ce59854 264 The default is lower than the 1024 recommended by the C++0x standard
265 because G++ runs out of stack before 1024 with highly recursive template
266 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
267
268int max_tinst_depth = 900;
574a6990 269
988fc1d1 270/* The elements of `ridpointers' are identifier nodes for the reserved
271 type names and storage classes. It is indexed by a RID_... value. */
272tree *ridpointers;
273
e60a6f7b 274tree (*make_fname_decl) (location_t, tree, int);
9e5a737d 275
48d94ede 276/* Nonzero means don't warn about problems that occur when the code is
277 executed. */
278int c_inhibit_evaluation_warnings;
e78703c1 279
93be21c0 280/* Whether we are building a boolean conversion inside
281 convert_for_assignment, or some other late binary operation. If
282 build_binary_op is called for C (from code shared by C and C++) in
283 this case, then the operands have already been folded and the
284 result will not be folded again, so C_MAYBE_CONST_EXPR should not
285 be generated. */
286bool in_late_binary_op;
287
7f5f3953 288/* Whether lexing has been completed, so subsequent preprocessor
289 errors should use the compiler's input_location. */
290bool done_lexing = false;
291
2c0e001b 292/* Information about how a function name is generated. */
65b7f83f 293struct fname_var_t
294{
e99c3a1d 295 tree *const decl; /* pointer to the VAR_DECL. */
296 const unsigned rid; /* RID number for the identifier. */
297 const int pretty; /* How pretty is it? */
65b7f83f 298};
299
2c0e001b 300/* The three ways of getting then name of the current function. */
65b7f83f 301
302const struct fname_var_t fname_vars[] =
303{
2c0e001b 304 /* C99 compliant __func__, must be first. */
65b7f83f 305 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
2c0e001b 306 /* GCC __FUNCTION__ compliant. */
65b7f83f 307 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
2c0e001b 308 /* GCC __PRETTY_FUNCTION__ compliant. */
65b7f83f 309 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
310 {NULL, 0, 0},
311};
312
79396169 313/* Global visibility options. */
314struct visibility_flags visibility_options;
315
2d2f6a15 316static tree check_case_value (location_t, tree);
be23b16f 317static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
318 bool *);
be43ff5a 319
1cae46be 320
1cae46be 321static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
322static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
dbf6c367 323
5c6e5756 324/* Reserved words. The third field is a mask: keywords are disabled
325 if they match the mask.
326
327 Masks for languages:
328 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
329 C --std=c99: D_CXXONLY | D_OBJC
330 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
5c176ebe 331 C++ --std=c++98: D_CONLY | D_CXX11 | D_OBJC
332 C++ --std=c++11: D_CONLY | D_OBJC
5c6e5756 333 ObjC++ is like C++ except that D_OBJC is not set
334
335 If -fno-asm is used, D_ASM is added to the mask. If
336 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
337 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
b27e241e 338 In C with -Wc++-compat, we warn if D_CXXWARN is set.
5c6e5756 339
b27e241e 340 Note the complication of the D_CXX_OBJC keywords. These are
341 reserved words such as 'class'. In C++, 'class' is a reserved
342 word. In Objective-C++ it is too. In Objective-C, it is a
343 reserved word too, but only if it follows an '@' sign.
344*/
5c6e5756 345const struct c_common_resword c_common_reswords[] =
346{
83e25171 347 { "_Alignas", RID_ALIGNAS, D_CONLY },
348 { "_Alignof", RID_ALIGNOF, D_CONLY },
b560fabd 349 { "_Atomic", RID_ATOMIC, D_CONLY },
5c6e5756 350 { "_Bool", RID_BOOL, D_CONLY },
351 { "_Complex", RID_COMPLEX, 0 },
c1800156 352 { "_Imaginary", RID_IMAGINARY, D_CONLY },
82c85aba 353 { "_Float16", RID_FLOAT16, D_CONLY },
354 { "_Float32", RID_FLOAT32, D_CONLY },
355 { "_Float64", RID_FLOAT64, D_CONLY },
356 { "_Float128", RID_FLOAT128, D_CONLY },
357 { "_Float32x", RID_FLOAT32X, D_CONLY },
358 { "_Float64x", RID_FLOAT64X, D_CONLY },
359 { "_Float128x", RID_FLOAT128X, D_CONLY },
5c6e5756 360 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
361 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
362 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
363 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
364 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
365 { "_Sat", RID_SAT, D_CONLY | D_EXT },
f80e7755 366 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
985c6e3a 367 { "_Noreturn", RID_NORETURN, D_CONLY },
7aa04c8d 368 { "_Generic", RID_GENERIC, D_CONLY },
d184e0c0 369 { "_Thread_local", RID_THREAD, D_CONLY },
5c6e5756 370 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
371 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
372 { "__alignof", RID_ALIGNOF, 0 },
373 { "__alignof__", RID_ALIGNOF, 0 },
374 { "__asm", RID_ASM, 0 },
375 { "__asm__", RID_ASM, 0 },
376 { "__attribute", RID_ATTRIBUTE, 0 },
377 { "__attribute__", RID_ATTRIBUTE, 0 },
4fba5eb9 378 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
e6014a82 379 { "__bases", RID_BASES, D_CXXONLY },
cd45162d 380 { "__builtin_addressof", RID_ADDRESSOF, D_CXXONLY },
6f58cf06 381 { "__builtin_call_with_static_chain",
382 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
5c6e5756 383 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
bff4ad11 384 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
59409f09 385 { "__builtin_convertvector", RID_BUILTIN_CONVERTVECTOR, 0 },
818af1aa 386 { "__builtin_has_attribute", RID_BUILTIN_HAS_ATTRIBUTE, 0 },
6e1b2ffb 387 { "__builtin_launder", RID_BUILTIN_LAUNDER, D_CXXONLY },
bf0cb017 388 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
aac64699 389 { "__builtin_tgmath", RID_BUILTIN_TGMATH, D_CONLY },
5c6e5756 390 { "__builtin_offsetof", RID_OFFSETOF, 0 },
391 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
392 { "__builtin_va_arg", RID_VA_ARG, 0 },
393 { "__complex", RID_COMPLEX, 0 },
394 { "__complex__", RID_COMPLEX, 0 },
395 { "__const", RID_CONST, 0 },
396 { "__const__", RID_CONST, 0 },
397 { "__decltype", RID_DECLTYPE, D_CXXONLY },
e6014a82 398 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
5c6e5756 399 { "__extension__", RID_EXTENSION, 0 },
400 { "__func__", RID_C99_FUNCTION_NAME, 0 },
401 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
402 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
403 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
404 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
405 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
406 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
407 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
adeca879 408 { "__has_unique_object_representations", RID_HAS_UNIQUE_OBJ_REPRESENTATIONS,
409 D_CXXONLY },
5c6e5756 410 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
23407dc9 411 { "__imag", RID_IMAGPART, 0 },
412 { "__imag__", RID_IMAGPART, 0 },
413 { "__inline", RID_INLINE, 0 },
414 { "__inline__", RID_INLINE, 0 },
5c6e5756 415 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
ca2af7df 416 { "__is_aggregate", RID_IS_AGGREGATE, D_CXXONLY },
5c6e5756 417 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
418 { "__is_class", RID_IS_CLASS, D_CXXONLY },
5c6e5756 419 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
420 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
aa4313eb 421 { "__is_final", RID_IS_FINAL, D_CXXONLY },
23407dc9 422 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
5c6e5756 423 { "__is_pod", RID_IS_POD, D_CXXONLY },
424 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
56c12fd4 425 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
c1c67b4f 426 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
427 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
f76a9aa8 428 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
429 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
717e52f9 430 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
5c6e5756 431 { "__is_union", RID_IS_UNION, D_CXXONLY },
5c6e5756 432 { "__label__", RID_LABEL, 0 },
433 { "__null", RID_NULL, 0 },
434 { "__real", RID_REALPART, 0 },
435 { "__real__", RID_REALPART, 0 },
436 { "__restrict", RID_RESTRICT, 0 },
437 { "__restrict__", RID_RESTRICT, 0 },
438 { "__signed", RID_SIGNED, 0 },
439 { "__signed__", RID_SIGNED, 0 },
440 { "__thread", RID_THREAD, 0 },
4c0315d0 441 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
442 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
443 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
5c6e5756 444 { "__typeof", RID_TYPEOF, 0 },
445 { "__typeof__", RID_TYPEOF, 0 },
23407dc9 446 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
5c6e5756 447 { "__volatile", RID_VOLATILE, 0 },
448 { "__volatile__", RID_VOLATILE, 0 },
b1f04d34 449 { "__GIMPLE", RID_GIMPLE, D_CONLY },
450 { "__PHI", RID_PHI, D_CONLY },
175e0d6b 451 { "__RTL", RID_RTL, D_CONLY },
fa769cc5 452 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
453 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 454 { "asm", RID_ASM, D_ASM },
455 { "auto", RID_AUTO, 0 },
dbd982c9 456 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
5c6e5756 457 { "break", RID_BREAK, 0 },
458 { "case", RID_CASE, 0 },
51030405 459 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
5c6e5756 460 { "char", RID_CHAR, 0 },
82362779 461 { "char8_t", RID_CHAR8, D_CXX_CHAR8_T_FLAGS | D_CXXWARN },
fa769cc5 462 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
463 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
51030405 464 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
5c6e5756 465 { "const", RID_CONST, 0 },
fa769cc5 466 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 467 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
468 { "continue", RID_CONTINUE, 0 },
fa769cc5 469 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 470 { "default", RID_DEFAULT, 0 },
51030405 471 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
5c6e5756 472 { "do", RID_DO, 0 },
473 { "double", RID_DOUBLE, 0 },
474 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
475 { "else", RID_ELSE, 0 },
476 { "enum", RID_ENUM, 0 },
51030405 477 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
478 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
5c6e5756 479 { "extern", RID_EXTERN, 0 },
51030405 480 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
5c6e5756 481 { "float", RID_FLOAT, 0 },
482 { "for", RID_FOR, 0 },
51030405 483 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
5c6e5756 484 { "goto", RID_GOTO, 0 },
485 { "if", RID_IF, 0 },
486 { "inline", RID_INLINE, D_EXT89 },
487 { "int", RID_INT, 0 },
488 { "long", RID_LONG, 0 },
489 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
51030405 490 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
491 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
fa769cc5 492 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
493 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
51030405 494 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
495 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
496 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
497 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
5c6e5756 498 { "register", RID_REGISTER, 0 },
499 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
500 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
501 { "return", RID_RETURN, 0 },
502 { "short", RID_SHORT, 0 },
503 { "signed", RID_SIGNED, 0 },
504 { "sizeof", RID_SIZEOF, 0 },
505 { "static", RID_STATIC, 0 },
fa769cc5 506 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
5c6e5756 507 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
508 { "struct", RID_STRUCT, 0 },
509 { "switch", RID_SWITCH, 0 },
51030405 510 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
511 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
fa769cc5 512 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
51030405 513 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
514 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
515 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
5c6e5756 516 { "typedef", RID_TYPEDEF, 0 },
51030405 517 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
518 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
5c6e5756 519 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
520 { "union", RID_UNION, 0 },
521 { "unsigned", RID_UNSIGNED, 0 },
51030405 522 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
523 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
5c6e5756 524 { "void", RID_VOID, 0 },
525 { "volatile", RID_VOLATILE, 0 },
526 { "wchar_t", RID_WCHAR, D_CXXONLY },
527 { "while", RID_WHILE, 0 },
b4d90ee2 528 { "__is_assignable", RID_IS_ASSIGNABLE, D_CXXONLY },
529 { "__is_constructible", RID_IS_CONSTRUCTIBLE, D_CXXONLY },
56c12fd4 530
6d02e6b2 531 /* C++ transactional memory. */
532 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
533 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
534 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
535 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
536
56c12fd4 537 /* Concepts-related keywords */
538 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
539 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
540
5c6e5756 541 /* These Objective-C keywords are recognized only immediately after
542 an '@'. */
543 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
544 { "defs", RID_AT_DEFS, D_OBJC },
545 { "encode", RID_AT_ENCODE, D_OBJC },
546 { "end", RID_AT_END, D_OBJC },
547 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
548 { "interface", RID_AT_INTERFACE, D_OBJC },
549 { "protocol", RID_AT_PROTOCOL, D_OBJC },
550 { "selector", RID_AT_SELECTOR, D_OBJC },
551 { "finally", RID_AT_FINALLY, D_OBJC },
069761fb 552 { "optional", RID_AT_OPTIONAL, D_OBJC },
553 { "required", RID_AT_REQUIRED, D_OBJC },
86c110ac 554 { "property", RID_AT_PROPERTY, D_OBJC },
4a8875ed 555 { "package", RID_AT_PACKAGE, D_OBJC },
e1f293c0 556 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
557 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
5c6e5756 558 /* These are recognized only in protocol-qualifier context
559 (see above) */
560 { "bycopy", RID_BYCOPY, D_OBJC },
561 { "byref", RID_BYREF, D_OBJC },
562 { "in", RID_IN, D_OBJC },
563 { "inout", RID_INOUT, D_OBJC },
564 { "oneway", RID_ONEWAY, D_OBJC },
565 { "out", RID_OUT, D_OBJC },
86c110ac 566 /* These are recognized inside a property attribute list */
7590f0e5 567 { "assign", RID_ASSIGN, D_OBJC },
568 { "copy", RID_COPY, D_OBJC },
86c110ac 569 { "getter", RID_GETTER, D_OBJC },
7590f0e5 570 { "nonatomic", RID_NONATOMIC, D_OBJC },
571 { "readonly", RID_READONLY, D_OBJC },
572 { "readwrite", RID_READWRITE, D_OBJC },
573 { "retain", RID_RETAIN, D_OBJC },
86c110ac 574 { "setter", RID_SETTER, D_OBJC },
5c6e5756 575};
576
577const unsigned int num_c_common_reswords =
578 sizeof c_common_reswords / sizeof (struct c_common_resword);
579
6d5d708e 580/* Return identifier for address space AS. */
34208e18 581
6d5d708e 582const char *
583c_addr_space_name (addr_space_t as)
584{
34208e18 585 int rid = RID_FIRST_ADDR_SPACE + as;
586 gcc_assert (ridpointers [rid]);
587 return IDENTIFIER_POINTER (ridpointers [rid]);
6d5d708e 588}
589
2c0e001b 590/* Push current bindings for the function name VAR_DECLS. */
f4e3c278 591
592void
1cae46be 593start_fname_decls (void)
f4e3c278 594{
65b7f83f 595 unsigned ix;
596 tree saved = NULL_TREE;
1cae46be 597
65b7f83f 598 for (ix = 0; fname_vars[ix].decl; ix++)
599 {
600 tree decl = *fname_vars[ix].decl;
f4e3c278 601
65b7f83f 602 if (decl)
603 {
ceb7b692 604 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
605 saved);
65b7f83f 606 *fname_vars[ix].decl = NULL_TREE;
607 }
608 }
609 if (saved || saved_function_name_decls)
610 /* Normally they'll have been NULL, so only push if we've got a
611 stack, or they are non-NULL. */
612 saved_function_name_decls = tree_cons (saved, NULL_TREE,
613 saved_function_name_decls);
614}
615
2363ef00 616/* Finish up the current bindings, adding them into the current function's
617 statement tree. This must be done _before_ finish_stmt_tree is called.
618 If there is no current function, we must be at file scope and no statements
619 are involved. Pop the previous bindings. */
65b7f83f 620
621void
1cae46be 622finish_fname_decls (void)
65b7f83f 623{
624 unsigned ix;
2363ef00 625 tree stmts = NULL_TREE;
65b7f83f 626 tree stack = saved_function_name_decls;
627
628 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
2363ef00 629 append_to_statement_list (TREE_VALUE (stack), &stmts);
1cae46be 630
2363ef00 631 if (stmts)
65b7f83f 632 {
2363ef00 633 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
5c423bd6 634
2363ef00 635 if (TREE_CODE (*bodyp) == BIND_EXPR)
636 bodyp = &BIND_EXPR_BODY (*bodyp);
81010c97 637
bc2b76e0 638 append_to_statement_list_force (*bodyp, &stmts);
2363ef00 639 *bodyp = stmts;
65b7f83f 640 }
1cae46be 641
65b7f83f 642 for (ix = 0; fname_vars[ix].decl; ix++)
643 *fname_vars[ix].decl = NULL_TREE;
1cae46be 644
65b7f83f 645 if (stack)
f4e3c278 646 {
2c0e001b 647 /* We had saved values, restore them. */
65b7f83f 648 tree saved;
649
650 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
651 {
652 tree decl = TREE_PURPOSE (saved);
f9ae6f95 653 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
1cae46be 654
65b7f83f 655 *fname_vars[ix].decl = decl;
656 }
657 stack = TREE_CHAIN (stack);
f4e3c278 658 }
65b7f83f 659 saved_function_name_decls = stack;
660}
661
81010c97 662/* Return the text name of the current function, suitably prettified
5fc7fa69 663 by PRETTY_P. Return string must be freed by caller. */
65b7f83f 664
665const char *
1cae46be 666fname_as_string (int pretty_p)
65b7f83f 667{
9ad4bb1e 668 const char *name = "top level";
5fc7fa69 669 char *namep;
8115b8be 670 int vrb = 2, len;
671 cpp_string cstr = { 0, 0 }, strname;
9ad4bb1e 672
84166705 673 if (!pretty_p)
9ad4bb1e 674 {
675 name = "";
676 vrb = 0;
677 }
678
679 if (current_function_decl)
dc24ddbd 680 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
9ad4bb1e 681
8115b8be 682 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
5fc7fa69 683
8115b8be 684 namep = XNEWVEC (char, len);
685 snprintf (namep, len, "\"%s\"", name);
686 strname.text = (unsigned char *) namep;
687 strname.len = len - 1;
5fc7fa69 688
924bbf02 689 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
8115b8be 690 {
691 XDELETEVEC (namep);
692 return (const char *) cstr.text;
5fc7fa69 693 }
5fc7fa69 694
695 return namep;
65b7f83f 696}
697
65b7f83f 698/* Return the VAR_DECL for a const char array naming the current
699 function. If the VAR_DECL has not yet been created, create it
700 now. RID indicates how it should be formatted and IDENTIFIER_NODE
701 ID is its name (unfortunately C and C++ hold the RID values of
702 keywords in different places, so we can't derive RID from ID in
e3b80d49 703 this language independent code. LOC is the location of the
704 function. */
65b7f83f 705
706tree
e3b80d49 707fname_decl (location_t loc, unsigned int rid, tree id)
65b7f83f 708{
709 unsigned ix;
710 tree decl = NULL_TREE;
711
712 for (ix = 0; fname_vars[ix].decl; ix++)
713 if (fname_vars[ix].rid == rid)
714 break;
715
716 decl = *fname_vars[ix].decl;
717 if (!decl)
f4e3c278 718 {
2222b3c6 719 /* If a tree is built here, it would normally have the lineno of
720 the current statement. Later this tree will be moved to the
721 beginning of the function and this line number will be wrong.
722 To avoid this problem set the lineno to 0 here; that prevents
7299020b 723 it from appearing in the RTL. */
2363ef00 724 tree stmts;
9a6486a6 725 location_t saved_location = input_location;
9a6486a6 726 input_location = UNKNOWN_LOCATION;
1cae46be 727
2363ef00 728 stmts = push_stmt_list ();
e60a6f7b 729 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
2363ef00 730 stmts = pop_stmt_list (stmts);
731 if (!IS_EMPTY_STMT (stmts))
732 saved_function_name_decls
733 = tree_cons (decl, stmts, saved_function_name_decls);
65b7f83f 734 *fname_vars[ix].decl = decl;
9a6486a6 735 input_location = saved_location;
f4e3c278 736 }
65b7f83f 737 if (!ix && !current_function_decl)
e3b80d49 738 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
81010c97 739
65b7f83f 740 return decl;
f4e3c278 741}
742
070236f0 743/* Given a STRING_CST, give it a suitable array-of-chars data type. */
b0fc3e72 744
745tree
1cae46be 746fix_string_type (tree value)
b0fc3e72 747{
070236f0 748 int length = TREE_STRING_LENGTH (value);
bee3d99f 749 int nchars, charsz;
00d26680 750 tree e_type, i_type, a_type;
751
73be5127 752 /* Compute the number of elements, for the array type. */
924bbf02 753 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
754 {
bee3d99f 755 charsz = 1;
924bbf02 756 e_type = char_type_node;
757 }
82362779 758 else if (flag_char8_t && TREE_TYPE (value) == char8_array_type_node)
759 {
760 charsz = TYPE_PRECISION (char8_type_node) / BITS_PER_UNIT;
761 e_type = char8_type_node;
762 }
924bbf02 763 else if (TREE_TYPE (value) == char16_array_type_node)
764 {
bee3d99f 765 charsz = TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT;
924bbf02 766 e_type = char16_type_node;
767 }
768 else if (TREE_TYPE (value) == char32_array_type_node)
769 {
bee3d99f 770 charsz = TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT;
924bbf02 771 e_type = char32_type_node;
772 }
773 else
774 {
bee3d99f 775 charsz = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
924bbf02 776 e_type = wchar_type_node;
777 }
b0fc3e72 778
bee3d99f 779 /* This matters only for targets where ssizetype has smaller precision
780 than 32 bits. */
781 if (wi::lts_p (wi::to_wide (TYPE_MAX_VALUE (ssizetype)), length))
782 {
783 error ("size of string literal is too large");
784 length = tree_to_shwi (TYPE_MAX_VALUE (ssizetype)) / charsz * charsz;
785 char *str = CONST_CAST (char *, TREE_STRING_POINTER (value));
786 memset (str + length, '\0',
787 MIN (TREE_STRING_LENGTH (value) - length, charsz));
788 TREE_STRING_LENGTH (value) = length;
789 }
790 nchars = length / charsz;
791
1d752508 792 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
793 limit in C++98 Annex B is very large (65536) and is not normative,
794 so we do not diagnose it (warn_overlength_strings is forced off
795 in c_common_post_options). */
796 if (warn_overlength_strings)
797 {
798 const int nchars_max = flag_isoc99 ? 4095 : 509;
799 const int relevant_std = flag_isoc99 ? 99 : 90;
800 if (nchars - 1 > nchars_max)
801 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
802 separate the %d from the 'C'. 'ISO' should not be
803 translated, but it may be moved after 'C%d' in languages
804 where modifiers follow nouns. */
21ca8540 805 pedwarn (input_location, OPT_Woverlength_strings,
8864917d 806 "string length %qd is greater than the length %qd "
1d752508 807 "ISO C%d compilers are required to support",
808 nchars - 1, nchars_max, relevant_std);
809 }
82cfc7f7 810
390be14e 811 /* Create the array type for the string constant. The ISO C++
812 standard says that a string literal has type `const char[N]' or
813 `const wchar_t[N]'. We use the same logic when invoked as a C
814 front-end with -Wwrite-strings.
815 ??? We should change the type of an expression depending on the
816 state of a warning flag. We should just be warning -- see how
817 this is handled in the C++ front-end for the deprecated implicit
818 conversion from string literals to `char*' or `wchar_t*'.
00d26680 819
820 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
821 array type being the unqualified version of that type.
822 Therefore, if we are constructing an array of const char, we must
823 construct the matching unqualified array type first. The C front
824 end does not require this, but it does no harm, so we do it
825 unconditionally. */
ceb7b692 826 i_type = build_index_type (size_int (nchars - 1));
00d26680 827 a_type = build_array_type (e_type, i_type);
390be14e 828 if (c_dialect_cxx() || warn_write_strings)
aebc8537 829 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
3a10ba35 830
00d26680 831 TREE_TYPE (value) = a_type;
b8e3b7ad 832 TREE_CONSTANT (value) = 1;
a814bad5 833 TREE_READONLY (value) = 1;
b0fc3e72 834 TREE_STATIC (value) = 1;
835 return value;
836}
d4166bdc 837
838/* Given a string of type STRING_TYPE, determine what kind of string
839 token would give an equivalent execution encoding: CPP_STRING,
840 CPP_STRING16, or CPP_STRING32. Return CPP_OTHER in case of error.
841 This may not be exactly the string token type that initially created
842 the string, since CPP_WSTRING is indistinguishable from the 16/32 bit
82362779 843 string type, and CPP_UTF8STRING is indistinguishable from CPP_STRING
844 at this point.
d4166bdc 845
846 This effectively reverses part of the logic in lex_string and
847 fix_string_type. */
848
849static enum cpp_ttype
850get_cpp_ttype_from_string_type (tree string_type)
851{
852 gcc_assert (string_type);
3da97ff7 853 if (TREE_CODE (string_type) == POINTER_TYPE)
854 string_type = TREE_TYPE (string_type);
855
d4166bdc 856 if (TREE_CODE (string_type) != ARRAY_TYPE)
857 return CPP_OTHER;
858
859 tree element_type = TREE_TYPE (string_type);
860 if (TREE_CODE (element_type) != INTEGER_TYPE)
861 return CPP_OTHER;
862
863 int bits_per_character = TYPE_PRECISION (element_type);
864 switch (bits_per_character)
865 {
866 case 8:
867 return CPP_STRING; /* It could have also been CPP_UTF8STRING. */
868 case 16:
869 return CPP_STRING16;
870 case 32:
871 return CPP_STRING32;
872 }
873
874 return CPP_OTHER;
875}
876
877/* The global record of string concatentations, for use in
878 extracting locations within string literals. */
879
880GTY(()) string_concat_db *g_string_concat_db;
881
3da97ff7 882/* Implementation of LANG_HOOKS_GET_SUBSTRING_LOCATION. */
d4166bdc 883
884const char *
3da97ff7 885c_get_substring_location (const substring_loc &substr_loc,
886 location_t *out_loc)
d4166bdc 887{
3da97ff7 888 enum cpp_ttype tok_type
889 = get_cpp_ttype_from_string_type (substr_loc.get_string_type ());
d4166bdc 890 if (tok_type == CPP_OTHER)
891 return "unrecognized string type";
892
be1e7283 893 return get_location_within_string (parse_in, g_string_concat_db,
894 substr_loc.get_fmt_string_loc (),
895 tok_type,
896 substr_loc.get_caret_idx (),
897 substr_loc.get_start_idx (),
898 substr_loc.get_end_idx (),
899 out_loc);
d4166bdc 900}
901
b0fc3e72 902\f
8f8828ba 903/* Return true iff T is a boolean promoted to int. */
904
0f4cea33 905bool
8f8828ba 906bool_promoted_to_int_p (tree t)
907{
908 return (CONVERT_EXPR_P (t)
909 && TREE_TYPE (t) == integer_type_node
910 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == BOOLEAN_TYPE);
911}
912
73437615 913/* vector_targets_convertible_p is used for vector pointer types. The
914 callers perform various checks that the qualifiers are satisfactory,
915 while OTOH vector_targets_convertible_p ignores the number of elements
916 in the vectors. That's fine with vector pointers as we can consider,
917 say, a vector of 8 elements as two consecutive vectors of 4 elements,
918 and that does not require and conversion of the pointer values.
919 In contrast, vector_types_convertible_p and
920 vector_types_compatible_elements_p are used for vector value types. */
ed7c4e62 921/* True if pointers to distinct types T1 and T2 can be converted to
922 each other without an explicit cast. Only returns true for opaque
923 vector types. */
924bool
925vector_targets_convertible_p (const_tree t1, const_tree t2)
926{
6290f0db 927 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
8d125f7d 928 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ed7c4e62 929 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
930 return true;
931
932 return false;
933}
934
73437615 935/* vector_types_convertible_p is used for vector value types.
936 It could in principle call vector_targets_convertible_p as a subroutine,
937 but then the check for vector type would be duplicated with its callers,
938 and also the purpose of vector_targets_convertible_p would become
939 muddled.
940 Where vector_types_convertible_p returns true, a conversion might still be
941 needed to make the types match.
942 In contrast, vector_targets_convertible_p is used for vector pointer
943 values, and vector_types_compatible_elements_p is used specifically
944 in the context for binary operators, as a check if use is possible without
945 conversion. */
546c4794 946/* True if vector types T1 and T2 can be converted to each other
947 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
948 can only be converted with -flax-vector-conversions yet that is not
949 in effect, emit a note telling the user about that option if such
950 a note has not previously been emitted. */
951bool
9f627b1a 952vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
8b4b9810 953{
546c4794 954 static bool emitted_lax_note = false;
ae6db8ab 955 bool convertible_lax;
956
8d125f7d 957 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
ae6db8ab 958 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
959 return true;
960
961 convertible_lax =
962 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
f08ee65f 963 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE
964 || known_eq (TYPE_VECTOR_SUBPARTS (t1),
965 TYPE_VECTOR_SUBPARTS (t2)))
ae6db8ab 966 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
967 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
546c4794 968
969 if (!convertible_lax || flag_lax_vector_conversions)
970 return convertible_lax;
971
f08ee65f 972 if (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
29f51994 973 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
546c4794 974 return true;
975
976 if (emit_lax_note && !emitted_lax_note)
977 {
978 emitted_lax_note = true;
2f6d557f 979 inform (input_location, "use %<-flax-vector-conversions%> to permit "
546c4794 980 "conversions between vectors with differing "
981 "element types or numbers of subparts");
982 }
983
984 return false;
8b4b9810 985}
986
bf0cb017 987/* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
988 and have vector types, V0 has the same type as V1, and the number of
989 elements of V0, V1, MASK is the same.
990
991 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
992 called with two arguments. In this case implementation passes the
993 first argument twice in order to share the same tree code. This fact
994 could enable the mask-values being twice the vector length. This is
995 an implementation accident and this semantics is not guaranteed to
996 the user. */
997tree
68ea4406 998c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
999 bool complain)
bf0cb017 1000{
1001 tree ret;
1002 bool wrap = true;
1003 bool maybe_const = false;
1004 bool two_arguments = false;
1005
1006 if (v1 == NULL_TREE)
1007 {
1008 two_arguments = true;
1009 v1 = v0;
1010 }
1011
1012 if (v0 == error_mark_node || v1 == error_mark_node
1013 || mask == error_mark_node)
1014 return error_mark_node;
1015
6290f0db 1016 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
bf0cb017 1017 {
68ea4406 1018 if (complain)
2f6d557f 1019 error_at (loc, "%<__builtin_shuffle%> last argument must "
68ea4406 1020 "be an integer vector");
bf0cb017 1021 return error_mark_node;
1022 }
1023
6290f0db 1024 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
1025 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
bf0cb017 1026 {
68ea4406 1027 if (complain)
2f6d557f 1028 error_at (loc, "%<__builtin_shuffle%> arguments must be vectors");
bf0cb017 1029 return error_mark_node;
1030 }
1031
1032 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
1033 {
68ea4406 1034 if (complain)
2f6d557f 1035 error_at (loc, "%<__builtin_shuffle%> argument vectors must be of "
68ea4406 1036 "the same type");
bf0cb017 1037 return error_mark_node;
1038 }
1039
f08ee65f 1040 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)),
1041 TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
1042 && maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)),
1043 TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))))
bf0cb017 1044 {
68ea4406 1045 if (complain)
2f6d557f 1046 error_at (loc, "%<__builtin_shuffle%> number of elements of the "
68ea4406 1047 "argument vector(s) and the mask vector should "
1048 "be the same");
bf0cb017 1049 return error_mark_node;
1050 }
1051
3d2b0034 1052 if (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
1053 != GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
bf0cb017 1054 {
68ea4406 1055 if (complain)
2f6d557f 1056 error_at (loc, "%<__builtin_shuffle%> argument vector(s) inner type "
68ea4406 1057 "must have the same size as inner type of the mask");
bf0cb017 1058 return error_mark_node;
1059 }
1060
1061 if (!c_dialect_cxx ())
1062 {
1063 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
1064 v0 = c_fully_fold (v0, false, &maybe_const);
1065 wrap &= maybe_const;
1066
1067 if (two_arguments)
1068 v1 = v0 = save_expr (v0);
1069 else
1070 {
1071 v1 = c_fully_fold (v1, false, &maybe_const);
1072 wrap &= maybe_const;
1073 }
1074
1075 mask = c_fully_fold (mask, false, &maybe_const);
1076 wrap &= maybe_const;
1077 }
68ea4406 1078 else if (two_arguments)
1079 v1 = v0 = save_expr (v0);
bf0cb017 1080
1081 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
1082
1083 if (!c_dialect_cxx () && !wrap)
1084 ret = c_wrap_maybe_const (ret, true);
1085
1086 return ret;
1087}
1088
59409f09 1089/* Build a VEC_CONVERT ifn for __builtin_convertvector builtin. */
1090
1091tree
1092c_build_vec_convert (location_t loc1, tree expr, location_t loc2, tree type,
1093 bool complain)
1094{
1095 if (error_operand_p (type))
1096 return error_mark_node;
1097 if (error_operand_p (expr))
1098 return error_mark_node;
1099
1100 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (expr))
1101 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (expr)))
1102 {
1103 if (complain)
1104 error_at (loc1, "%<__builtin_convertvector%> first argument must "
1105 "be an integer or floating vector");
1106 return error_mark_node;
1107 }
1108
1109 if (!VECTOR_INTEGER_TYPE_P (type) && !VECTOR_FLOAT_TYPE_P (type))
1110 {
1111 if (complain)
1112 error_at (loc2, "%<__builtin_convertvector%> second argument must "
1113 "be an integer or floating vector type");
1114 return error_mark_node;
1115 }
1116
1117 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr)),
1118 TYPE_VECTOR_SUBPARTS (type)))
1119 {
1120 if (complain)
1121 error_at (loc1, "%<__builtin_convertvector%> number of elements "
1122 "of the first argument vector and the second argument "
1123 "vector type should be the same");
1124 return error_mark_node;
1125 }
1126
1127 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (expr)))
1128 == TYPE_MAIN_VARIANT (TREE_TYPE (type)))
1129 || (VECTOR_INTEGER_TYPE_P (TREE_TYPE (expr))
1130 && VECTOR_INTEGER_TYPE_P (type)
1131 && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (expr)))
1132 == TYPE_PRECISION (TREE_TYPE (type)))))
1133 return build1_loc (loc1, VIEW_CONVERT_EXPR, type, expr);
1134
1135 bool wrap = true;
1136 bool maybe_const = false;
1137 tree ret;
1138 if (!c_dialect_cxx ())
1139 {
1140 /* Avoid C_MAYBE_CONST_EXPRs inside of VEC_CONVERT argument. */
1141 expr = c_fully_fold (expr, false, &maybe_const);
1142 wrap &= maybe_const;
1143 }
1144
1145 ret = build_call_expr_internal_loc (loc1, IFN_VEC_CONVERT, type, 1, expr);
1146
1147 if (!wrap)
1148 ret = c_wrap_maybe_const (ret, true);
1149
1150 return ret;
1151}
1152
7f506bca 1153/* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
1154 to integral type. */
1155
0f4cea33 1156tree
7f506bca 1157c_common_get_narrower (tree op, int *unsignedp_ptr)
1158{
1159 op = get_narrower (op, unsignedp_ptr);
1160
1161 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
1162 && ENUM_IS_SCOPED (TREE_TYPE (op)))
1163 {
1164 /* C++0x scoped enumerations don't implicitly convert to integral
1165 type; if we stripped an explicit conversion to a larger type we
1166 need to replace it so common_type will still work. */
a51edb4c 1167 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
1168 TYPE_UNSIGNED (TREE_TYPE (op)));
7f506bca 1169 op = fold_convert (type, op);
1170 }
1171 return op;
1172}
1173
2561cea2 1174/* This is a helper function of build_binary_op.
1175
1176 For certain operations if both args were extended from the same
1177 smaller type, do the arithmetic in that type and then extend.
1178
1179 BITWISE indicates a bitwise operation.
1180 For them, this optimization is safe only if
1181 both args are zero-extended or both are sign-extended.
1182 Otherwise, we might change the result.
1183 Eg, (short)-1 | (unsigned short)-1 is (int)-1
48e1416a 1184 but calculated in (unsigned short) it would be (unsigned short)-1.
2561cea2 1185*/
7f506bca 1186tree
1187shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
2561cea2 1188{
1189 int unsigned0, unsigned1;
1190 tree arg0, arg1;
1191 int uns;
1192 tree type;
1193
1194 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1195 excessive narrowing when we call get_narrower below. For
1196 example, suppose that OP0 is of unsigned int extended
1197 from signed char and that RESULT_TYPE is long long int.
1198 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1199 like
48e1416a 1200
2561cea2 1201 (long long int) (unsigned int) signed_char
1202
1203 which get_narrower would narrow down to
48e1416a 1204
2561cea2 1205 (unsigned int) signed char
48e1416a 1206
2561cea2 1207 If we do not cast OP0 first, get_narrower would return
1208 signed_char, which is inconsistent with the case of the
1209 explicit cast. */
1210 op0 = convert (result_type, op0);
1211 op1 = convert (result_type, op1);
1212
7f506bca 1213 arg0 = c_common_get_narrower (op0, &unsigned0);
1214 arg1 = c_common_get_narrower (op1, &unsigned1);
ab2c1de8 1215
2561cea2 1216 /* UNS is 1 if the operation to be done is an unsigned one. */
1217 uns = TYPE_UNSIGNED (result_type);
1218
1219 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1220 but it *requires* conversion to FINAL_TYPE. */
48e1416a 1221
2561cea2 1222 if ((TYPE_PRECISION (TREE_TYPE (op0))
1223 == TYPE_PRECISION (TREE_TYPE (arg0)))
1224 && TREE_TYPE (op0) != result_type)
1225 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1226 if ((TYPE_PRECISION (TREE_TYPE (op1))
1227 == TYPE_PRECISION (TREE_TYPE (arg1)))
1228 && TREE_TYPE (op1) != result_type)
1229 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
48e1416a 1230
2561cea2 1231 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
48e1416a 1232
2561cea2 1233 /* For bitwise operations, signedness of nominal type
1234 does not matter. Consider only how operands were extended. */
1235 if (bitwise)
1236 uns = unsigned0;
48e1416a 1237
2561cea2 1238 /* Note that in all three cases below we refrain from optimizing
1239 an unsigned operation on sign-extended args.
1240 That would not be valid. */
48e1416a 1241
2561cea2 1242 /* Both args variable: if both extended in same way
1243 from same width, do it in that width.
1244 Do it unsigned if args were zero-extended. */
1245 if ((TYPE_PRECISION (TREE_TYPE (arg0))
1246 < TYPE_PRECISION (result_type))
1247 && (TYPE_PRECISION (TREE_TYPE (arg1))
1248 == TYPE_PRECISION (TREE_TYPE (arg0)))
1249 && unsigned0 == unsigned1
1250 && (unsigned0 || !uns))
1251 return c_common_signed_or_unsigned_type
1252 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
1253
1254 else if (TREE_CODE (arg0) == INTEGER_CST
1255 && (unsigned1 || !uns)
1256 && (TYPE_PRECISION (TREE_TYPE (arg1))
1257 < TYPE_PRECISION (result_type))
1258 && (type
1259 = c_common_signed_or_unsigned_type (unsigned1,
1260 TREE_TYPE (arg1)))
1261 && !POINTER_TYPE_P (type)
1262 && int_fits_type_p (arg0, type))
1263 return type;
1264
1265 else if (TREE_CODE (arg1) == INTEGER_CST
1266 && (unsigned0 || !uns)
1267 && (TYPE_PRECISION (TREE_TYPE (arg0))
1268 < TYPE_PRECISION (result_type))
1269 && (type
1270 = c_common_signed_or_unsigned_type (unsigned0,
1271 TREE_TYPE (arg0)))
1272 && !POINTER_TYPE_P (type)
1273 && int_fits_type_p (arg1, type))
1274 return type;
1275
1276 return result_type;
1277}
1278
e53013a8 1279/* Returns true iff any integer value of type FROM_TYPE can be represented as
1280 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
1281
1282static bool
1283int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
1284{
1285 tree type_low_bound = TYPE_MIN_VALUE (from_type);
1286 tree type_high_bound = TYPE_MAX_VALUE (from_type);
1287 REAL_VALUE_TYPE real_low_bound =
1288 real_value_from_int_cst (0, type_low_bound);
1289 REAL_VALUE_TYPE real_high_bound =
1290 real_value_from_int_cst (0, type_high_bound);
1291
1292 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
1293 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
1294}
1295
1296/* Checks if expression EXPR of complex/real/integer type cannot be converted
1297 to the complex/real/integer type TYPE. Function returns non-zero when:
22a75734 1298 * EXPR is a constant which cannot be exactly converted to TYPE.
1299 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
e53013a8 1300 for EXPR type and TYPE being both integers or both real, or both
1301 complex.
1302 * EXPR is not a constant of complex type and TYPE is a real or
1303 an integer.
22a75734 1304 * EXPR is not a constant of real type and TYPE is an integer.
1305 * EXPR is not a constant of integer type which cannot be
1306 exactly converted to real type.
e53013a8 1307
7dfa155b 1308 Function allows conversions between types of different signedness and
ca9d7d74 1309 can return SAFE_CONVERSION (zero) in that case. Function can produce
e53013a8 1310 signedness warnings if PRODUCE_WARNS is true.
1311
645b0f8d 1312 RESULT, when non-null is the result of the conversion. When constant
1313 it is included in the text of diagnostics.
1314
e53013a8 1315 Function allows conversions from complex constants to non-complex types,
1316 provided that imaginary part is zero and real part can be safely converted
1317 to TYPE. */
22a75734 1318
ca9d7d74 1319enum conversion_safety
645b0f8d 1320unsafe_conversion_p (location_t loc, tree type, tree expr, tree result,
1321 bool produce_warns)
d31d55f0 1322{
ca9d7d74 1323 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
27259707 1324 tree expr_type = TREE_TYPE (expr);
645b0f8d 1325
1326 bool cstresult = (result
1327 && TREE_CODE_CLASS (TREE_CODE (result)) == tcc_constant);
1328
1329 loc = expansion_point_location_if_in_system_header (loc);
d31d55f0 1330
d582d140 1331 expr = fold_for_warn (expr);
1332
7dfa155b 1333 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
5b16c152 1334 {
e53013a8 1335 /* If type is complex, we are interested in compatibility with
1336 underlying type. */
1337 if (TREE_CODE (type) == COMPLEX_TYPE)
1338 type = TREE_TYPE (type);
1339
d31d55f0 1340 /* Warn for real constant that is not an exact integer converted
7dfa155b 1341 to integer type. */
27259707 1342 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 1343 && TREE_CODE (type) == INTEGER_TYPE)
1344 {
1345 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
ca9d7d74 1346 give_warning = UNSAFE_REAL;
7dfa155b 1347 }
da1fb07b 1348 /* Warn for an integer constant that does not fit into integer type. */
27259707 1349 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 1350 && TREE_CODE (type) == INTEGER_TYPE
1351 && !int_fits_type_p (expr, type))
1352 {
1353 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
27259707 1354 && tree_int_cst_sgn (expr) < 0)
7dfa155b 1355 {
1356 if (produce_warns)
645b0f8d 1357 {
1358 if (cstresult)
1359 warning_at (loc, OPT_Wsign_conversion,
1360 "unsigned conversion from %qT to %qT "
1361 "changes value from %qE to %qE",
1362 expr_type, type, expr, result);
1363 else
1364 warning_at (loc, OPT_Wsign_conversion,
1365 "unsigned conversion from %qT to %qT "
1366 "changes the value of %qE",
1367 expr_type, type, expr);
1368 }
7dfa155b 1369 }
1370 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
1371 {
645b0f8d 1372 if (cstresult)
1373 warning_at (loc, OPT_Wsign_conversion,
1374 "signed conversion from %qT to %qT changes "
1375 "value from %qE to %qE",
1376 expr_type, type, expr, result);
1377 else
1378 warning_at (loc, OPT_Wsign_conversion,
1379 "signed conversion from %qT to %qT changes "
1380 "the value of %qE",
1381 expr_type, type, expr);
7dfa155b 1382 }
7ee0d227 1383 else
ca9d7d74 1384 give_warning = UNSAFE_OTHER;
7dfa155b 1385 }
d31d55f0 1386 else if (TREE_CODE (type) == REAL_TYPE)
7dfa155b 1387 {
1388 /* Warn for an integer constant that does not fit into real type. */
1389 if (TREE_CODE (expr_type) == INTEGER_TYPE)
1390 {
1391 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
1392 if (!exact_real_truncate (TYPE_MODE (type), &a))
ca9d7d74 1393 give_warning = UNSAFE_REAL;
7dfa155b 1394 }
1395 /* Warn for a real constant that does not fit into a smaller
1396 real type. */
1397 else if (TREE_CODE (expr_type) == REAL_TYPE
1398 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1399 {
1400 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
1401 if (!exact_real_truncate (TYPE_MODE (type), &a))
ca9d7d74 1402 give_warning = UNSAFE_REAL;
7dfa155b 1403 }
1404 }
1405 }
e53013a8 1406
1407 else if (TREE_CODE (expr) == COMPLEX_CST)
1408 {
1409 tree imag_part = TREE_IMAGPART (expr);
1410 /* Conversion from complex constant with zero imaginary part,
1411 perform check for conversion of real part. */
1412 if ((TREE_CODE (imag_part) == REAL_CST
1413 && real_zerop (imag_part))
1414 || (TREE_CODE (imag_part) == INTEGER_CST
1415 && integer_zerop (imag_part)))
1416 /* Note: in this branch we use recursive call to unsafe_conversion_p
1417 with different type of EXPR, but it is still safe, because when EXPR
1418 is a constant, it's type is not used in text of generated warnings
1419 (otherwise they could sound misleading). */
645b0f8d 1420 return unsafe_conversion_p (loc, type, TREE_REALPART (expr), result,
e53013a8 1421 produce_warns);
1422 /* Conversion from complex constant with non-zero imaginary part. */
1423 else
1424 {
1425 /* Conversion to complex type.
1426 Perform checks for both real and imaginary parts. */
1427 if (TREE_CODE (type) == COMPLEX_TYPE)
1428 {
1429 /* Unfortunately, produce_warns must be false in two subsequent
1430 calls of unsafe_conversion_p, because otherwise we could
1431 produce strange "double" warnings, if both real and imaginary
1432 parts have conversion problems related to signedness.
1433
1434 For example:
1435 int32_t _Complex a = 0x80000000 + 0x80000000i;
1436
1437 Possible solution: add a separate function for checking
1438 constants and combine result of two calls appropriately. */
1439 enum conversion_safety re_safety =
645b0f8d 1440 unsafe_conversion_p (loc, type, TREE_REALPART (expr),
1441 result, false);
e53013a8 1442 enum conversion_safety im_safety =
645b0f8d 1443 unsafe_conversion_p (loc, type, imag_part, result, false);
e53013a8 1444
1445 /* Merge the results into appropriate single warning. */
1446
1447 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
1448 if (re_safety == im_safety)
1449 give_warning = re_safety;
1450 else if (!re_safety && im_safety)
1451 give_warning = im_safety;
1452 else if (re_safety && !im_safety)
1453 give_warning = re_safety;
1454 else
1455 give_warning = UNSAFE_OTHER;
1456 }
1457 /* Warn about conversion from complex to real or integer type. */
1458 else
1459 give_warning = UNSAFE_IMAGINARY;
1460 }
1461 }
1462
1463 /* Checks for remaining case: EXPR is not constant. */
7dfa155b 1464 else
1465 {
d31d55f0 1466 /* Warn for real types converted to integer types. */
2561cea2 1467 if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 1468 && TREE_CODE (type) == INTEGER_TYPE)
ca9d7d74 1469 give_warning = UNSAFE_REAL;
d31d55f0 1470
2561cea2 1471 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 1472 && TREE_CODE (type) == INTEGER_TYPE)
1473 {
69609004 1474 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
f9d856a4 1475 expr = get_unwidened (expr, 0);
2561cea2 1476 expr_type = TREE_TYPE (expr);
69609004 1477
2561cea2 1478 /* Don't warn for short y; short x = ((int)y & 0xff); */
48e1416a 1479 if (TREE_CODE (expr) == BIT_AND_EXPR
7dfa155b 1480 || TREE_CODE (expr) == BIT_IOR_EXPR
2561cea2 1481 || TREE_CODE (expr) == BIT_XOR_EXPR)
1482 {
27259707 1483 /* If both args were extended from a shortest type,
1484 use that type if that is safe. */
48e1416a 1485 expr_type = shorten_binary_op (expr_type,
1486 TREE_OPERAND (expr, 0),
1487 TREE_OPERAND (expr, 1),
2561cea2 1488 /* bitwise */1);
1489
2561cea2 1490 if (TREE_CODE (expr) == BIT_AND_EXPR)
1491 {
1492 tree op0 = TREE_OPERAND (expr, 0);
1493 tree op1 = TREE_OPERAND (expr, 1);
30de145b 1494 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1495 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1496
1497 /* If one of the operands is a non-negative constant
1498 that fits in the target type, then the type of the
1499 other operand does not matter. */
2561cea2 1500 if ((TREE_CODE (op0) == INTEGER_CST
1501 && int_fits_type_p (op0, c_common_signed_type (type))
1502 && int_fits_type_p (op0, c_common_unsigned_type (type)))
1503 || (TREE_CODE (op1) == INTEGER_CST
27259707 1504 && int_fits_type_p (op1, c_common_signed_type (type))
48e1416a 1505 && int_fits_type_p (op1,
27259707 1506 c_common_unsigned_type (type))))
ca9d7d74 1507 return SAFE_CONVERSION;
30de145b 1508 /* If constant is unsigned and fits in the target
1509 type, then the result will also fit. */
1510 else if ((TREE_CODE (op0) == INTEGER_CST
48e1416a 1511 && unsigned0
30de145b 1512 && int_fits_type_p (op0, type))
1513 || (TREE_CODE (op1) == INTEGER_CST
1514 && unsigned1
1515 && int_fits_type_p (op1, type)))
ca9d7d74 1516 return SAFE_CONVERSION;
2561cea2 1517 }
1518 }
7dfa155b 1519 /* Warn for integer types converted to smaller integer types. */
48e1416a 1520 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
ca9d7d74 1521 give_warning = UNSAFE_OTHER;
7ee0d227 1522
1523 /* When they are the same width but different signedness,
1524 then the value may change. */
7dfa155b 1525 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
2561cea2 1526 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
7ee0d227 1527 /* Even when converted to a bigger type, if the type is
1528 unsigned but expr is signed, then negative values
1529 will be changed. */
7dfa155b 1530 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
1531 && produce_warns)
200dd99c 1532 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
1533 "may change the sign of the result",
1534 type, expr_type);
7dfa155b 1535 }
d31d55f0 1536
1537 /* Warn for integer types converted to real types if and only if
7dfa155b 1538 all the range of values of the integer type cannot be
1539 represented by the real type. */
2561cea2 1540 else if (TREE_CODE (expr_type) == INTEGER_TYPE
7dfa155b 1541 && TREE_CODE (type) == REAL_TYPE)
1542 {
4c2cfa81 1543 /* Don't warn about char y = 0xff; float x = (int) y; */
1544 expr = get_unwidened (expr, 0);
1545 expr_type = TREE_TYPE (expr);
1546
e53013a8 1547 if (!int_safely_convertible_to_real_p (expr_type, type))
ca9d7d74 1548 give_warning = UNSAFE_OTHER;
7dfa155b 1549 }
d31d55f0 1550
1551 /* Warn for real types converted to smaller real types. */
2561cea2 1552 else if (TREE_CODE (expr_type) == REAL_TYPE
7dfa155b 1553 && TREE_CODE (type) == REAL_TYPE
1554 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
ca9d7d74 1555 give_warning = UNSAFE_REAL;
e53013a8 1556
1557 /* Check conversion between two complex types. */
1558 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1559 && TREE_CODE (type) == COMPLEX_TYPE)
1560 {
1561 /* Extract underlying types (i.e., type of real and imaginary
1562 parts) of expr_type and type. */
1563 tree from_type = TREE_TYPE (expr_type);
1564 tree to_type = TREE_TYPE (type);
1565
1566 /* Warn for real types converted to integer types. */
1567 if (TREE_CODE (from_type) == REAL_TYPE
1568 && TREE_CODE (to_type) == INTEGER_TYPE)
1569 give_warning = UNSAFE_REAL;
1570
1571 /* Warn for real types converted to smaller real types. */
1572 else if (TREE_CODE (from_type) == REAL_TYPE
1573 && TREE_CODE (to_type) == REAL_TYPE
1574 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1575 give_warning = UNSAFE_REAL;
1576
1577 /* Check conversion for complex integer types. Here implementation
1578 is simpler than for real-domain integers because it does not
1579 involve sophisticated cases, such as bitmasks, casts, etc. */
1580 else if (TREE_CODE (from_type) == INTEGER_TYPE
1581 && TREE_CODE (to_type) == INTEGER_TYPE)
1582 {
1583 /* Warn for integer types converted to smaller integer types. */
1584 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1585 give_warning = UNSAFE_OTHER;
1586
1587 /* Check for different signedness, see case for real-domain
1588 integers (above) for a more detailed comment. */
1589 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
1590 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
1591 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
1592 && produce_warns)
1593 warning_at (loc, OPT_Wsign_conversion,
1594 "conversion to %qT from %qT "
1595 "may change the sign of the result",
1596 type, expr_type);
1597 }
1598 else if (TREE_CODE (from_type) == INTEGER_TYPE
1599 && TREE_CODE (to_type) == REAL_TYPE
1600 && !int_safely_convertible_to_real_p (from_type, to_type))
1601 give_warning = UNSAFE_OTHER;
1602 }
1603
1604 /* Warn for complex types converted to real or integer types. */
1605 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1606 && TREE_CODE (type) != COMPLEX_TYPE)
1607 give_warning = UNSAFE_IMAGINARY;
7dfa155b 1608 }
1609
1610 return give_warning;
1611}
1612
59dd8856 1613
1614/* Convert EXPR to TYPE, warning about conversion problems with constants.
1615 Invoke this function on every expression that is converted implicitly,
1616 i.e. because of language rules and not because of an explicit cast. */
1617
1618tree
22a75734 1619convert_and_check (location_t loc, tree type, tree expr)
59dd8856 1620{
1621 tree result;
c6418a4e 1622 tree expr_for_warning;
1623
1624 /* Convert from a value with possible excess precision rather than
1625 via the semantic type, but do not warn about values not fitting
1626 exactly in the semantic type. */
1627 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1628 {
1629 tree orig_type = TREE_TYPE (expr);
1630 expr = TREE_OPERAND (expr, 0);
1631 expr_for_warning = convert (orig_type, expr);
1632 if (orig_type == type)
1633 return expr_for_warning;
1634 }
1635 else
1636 expr_for_warning = expr;
59dd8856 1637
1638 if (TREE_TYPE (expr) == type)
1639 return expr;
48e1416a 1640
59dd8856 1641 result = convert (type, expr);
1642
48d94ede 1643 if (c_inhibit_evaluation_warnings == 0
1644 && !TREE_OVERFLOW_P (expr)
1645 && result != error_mark_node)
22a75734 1646 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
59dd8856 1647
da1fb07b 1648 return result;
b2806639 1649}
1650\f
4e91a871 1651/* A node in a list that describes references to variables (EXPR), which are
1652 either read accesses if WRITER is zero, or write accesses, in which case
1653 WRITER is the parent of EXPR. */
1654struct tlist
1655{
1656 struct tlist *next;
1657 tree expr, writer;
1658};
1659
1660/* Used to implement a cache the results of a call to verify_tree. We only
1661 use this for SAVE_EXPRs. */
1662struct tlist_cache
1663{
1664 struct tlist_cache *next;
1665 struct tlist *cache_before_sp;
1666 struct tlist *cache_after_sp;
1667 tree expr;
481c6ce6 1668};
1669
4e91a871 1670/* Obstack to use when allocating tlist structures, and corresponding
1671 firstobj. */
1672static struct obstack tlist_obstack;
1673static char *tlist_firstobj = 0;
1674
1675/* Keep track of the identifiers we've warned about, so we can avoid duplicate
1676 warnings. */
1677static struct tlist *warned_ids;
1678/* SAVE_EXPRs need special treatment. We process them only once and then
1679 cache the results. */
1680static struct tlist_cache *save_expr_cache;
1681
1cae46be 1682static void add_tlist (struct tlist **, struct tlist *, tree, int);
1683static void merge_tlist (struct tlist **, struct tlist *, int);
1684static void verify_tree (tree, struct tlist **, struct tlist **, tree);
424b2d7d 1685static bool warning_candidate_p (tree);
79973b57 1686static bool candidate_equal_p (const_tree, const_tree);
1cae46be 1687static void warn_for_collisions (struct tlist *);
1688static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
1689static struct tlist *new_tlist (struct tlist *, tree, tree);
481c6ce6 1690
4e91a871 1691/* Create a new struct tlist and fill in its fields. */
1692static struct tlist *
1cae46be 1693new_tlist (struct tlist *next, tree t, tree writer)
4e91a871 1694{
1695 struct tlist *l;
9318f22c 1696 l = XOBNEW (&tlist_obstack, struct tlist);
4e91a871 1697 l->next = next;
1698 l->expr = t;
1699 l->writer = writer;
1700 return l;
1701}
1702
1703/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1704 is nonnull, we ignore any node we find which has a writer equal to it. */
1705
1706static void
1cae46be 1707add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
4e91a871 1708{
1709 while (add)
1710 {
1711 struct tlist *next = add->next;
84166705 1712 if (!copy)
4e91a871 1713 add->next = *to;
79973b57 1714 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
4e91a871 1715 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1716 add = next;
1717 }
1718}
1719
1720/* Merge the nodes of ADD into TO. This merging process is done so that for
1721 each variable that already exists in TO, no new node is added; however if
1722 there is a write access recorded in ADD, and an occurrence on TO is only
1723 a read access, then the occurrence in TO will be modified to record the
1724 write. */
481c6ce6 1725
1726static void
1cae46be 1727merge_tlist (struct tlist **to, struct tlist *add, int copy)
4e91a871 1728{
1729 struct tlist **end = to;
1730
1731 while (*end)
1732 end = &(*end)->next;
1733
1734 while (add)
1735 {
1736 int found = 0;
1737 struct tlist *tmp2;
1738 struct tlist *next = add->next;
1739
1740 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
79973b57 1741 if (candidate_equal_p (tmp2->expr, add->expr))
4e91a871 1742 {
1743 found = 1;
84166705 1744 if (!tmp2->writer)
4e91a871 1745 tmp2->writer = add->writer;
1746 }
84166705 1747 if (!found)
4e91a871 1748 {
312243bb 1749 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
4e91a871 1750 end = &(*end)->next;
1751 *end = 0;
1752 }
1753 add = next;
1754 }
1755}
1756
1757/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1758 references in list LIST conflict with it, excluding reads if ONLY writers
1759 is nonzero. */
1760
1761static void
1cae46be 1762warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
1763 int only_writes)
4e91a871 1764{
1765 struct tlist *tmp;
1766
1767 /* Avoid duplicate warnings. */
1768 for (tmp = warned_ids; tmp; tmp = tmp->next)
79973b57 1769 if (candidate_equal_p (tmp->expr, written))
4e91a871 1770 return;
1771
1772 while (list)
1773 {
79973b57 1774 if (candidate_equal_p (list->expr, written)
1775 && !candidate_equal_p (list->writer, writer)
1776 && (!only_writes || list->writer))
4e91a871 1777 {
1778 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
3df42822 1779 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
6513b50d 1780 OPT_Wsequence_point, "operation on %qE may be undefined",
1781 list->expr);
4e91a871 1782 }
1783 list = list->next;
1784 }
1785}
1786
1787/* Given a list LIST of references to variables, find whether any of these
1788 can cause conflicts due to missing sequence points. */
1789
1790static void
1cae46be 1791warn_for_collisions (struct tlist *list)
4e91a871 1792{
1793 struct tlist *tmp;
1cae46be 1794
4e91a871 1795 for (tmp = list; tmp; tmp = tmp->next)
1796 {
1797 if (tmp->writer)
1798 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1799 }
1800}
1801
734c98be 1802/* Return nonzero if X is a tree that can be verified by the sequence point
4e91a871 1803 warnings. */
424b2d7d 1804
1805static bool
1cae46be 1806warning_candidate_p (tree x)
481c6ce6 1807{
6ef8d12f 1808 if (DECL_P (x) && DECL_ARTIFICIAL (x))
424b2d7d 1809 return false;
6ef8d12f 1810
027fc6ef 1811 if (TREE_CODE (x) == BLOCK)
424b2d7d 1812 return false;
027fc6ef 1813
6ef8d12f 1814 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
79973b57 1815 (lvalue_p) crash on TRY/CATCH. */
6ef8d12f 1816 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
424b2d7d 1817 return false;
6ef8d12f 1818
1819 if (!lvalue_p (x))
424b2d7d 1820 return false;
6ef8d12f 1821
1822 /* No point to track non-const calls, they will never satisfy
1823 operand_equal_p. */
1824 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
424b2d7d 1825 return false;
6ef8d12f 1826
1827 if (TREE_CODE (x) == STRING_CST)
424b2d7d 1828 return false;
6ef8d12f 1829
424b2d7d 1830 return true;
79973b57 1831}
1832
1833/* Return nonzero if X and Y appear to be the same candidate (or NULL) */
1834static bool
1835candidate_equal_p (const_tree x, const_tree y)
1836{
1837 return (x == y) || (x && y && operand_equal_p (x, y, 0));
4e91a871 1838}
481c6ce6 1839
4e91a871 1840/* Walk the tree X, and record accesses to variables. If X is written by the
1841 parent tree, WRITER is the parent.
1842 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
1843 expression or its only operand forces a sequence point, then everything up
1844 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
1845 in PNO_SP.
1846 Once we return, we will have emitted warnings if any subexpression before
1847 such a sequence point could be undefined. On a higher level, however, the
1848 sequence point may not be relevant, and we'll merge the two lists.
1849
1850 Example: (b++, a) + b;
1851 The call that processes the COMPOUND_EXPR will store the increment of B
1852 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
1853 processes the PLUS_EXPR will need to merge the two lists so that
1854 eventually, all accesses end up on the same list (and we'll warn about the
1855 unordered subexpressions b++ and b.
1856
1857 A note on merging. If we modify the former example so that our expression
1858 becomes
1859 (b++, b) + a
1860 care must be taken not simply to add all three expressions into the final
1861 PNO_SP list. The function merge_tlist takes care of that by merging the
1862 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
1863 way, so that no more than one access to B is recorded. */
481c6ce6 1864
4e91a871 1865static void
1cae46be 1866verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
1867 tree writer)
4e91a871 1868{
1869 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
1870 enum tree_code code;
ce45a448 1871 enum tree_code_class cl;
481c6ce6 1872
e5b75768 1873 /* X may be NULL if it is the operand of an empty statement expression
1874 ({ }). */
1875 if (x == NULL)
1876 return;
1877
4e91a871 1878 restart:
1879 code = TREE_CODE (x);
e916c70c 1880 cl = TREE_CODE_CLASS (code);
481c6ce6 1881
4e91a871 1882 if (warning_candidate_p (x))
79973b57 1883 *pno_sp = new_tlist (*pno_sp, x, writer);
4e91a871 1884
1885 switch (code)
1886 {
67b28e3e 1887 case CONSTRUCTOR:
8e71dad2 1888 case SIZEOF_EXPR:
67b28e3e 1889 return;
1890
4e91a871 1891 case COMPOUND_EXPR:
1892 case TRUTH_ANDIF_EXPR:
1893 case TRUTH_ORIF_EXPR:
176aa551 1894 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
4e91a871 1895 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1896 warn_for_collisions (tmp_nosp);
1897 merge_tlist (pbefore_sp, tmp_before, 0);
1898 merge_tlist (pbefore_sp, tmp_nosp, 0);
176aa551 1899 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_list2, NULL_TREE);
1900 warn_for_collisions (tmp_list2);
4e91a871 1901 merge_tlist (pbefore_sp, tmp_list3, 0);
176aa551 1902 merge_tlist (pno_sp, tmp_list2, 0);
4e91a871 1903 return;
1904
1905 case COND_EXPR:
1906 tmp_before = tmp_list2 = 0;
1907 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
1908 warn_for_collisions (tmp_list2);
1909 merge_tlist (pbefore_sp, tmp_before, 0);
312243bb 1910 merge_tlist (pbefore_sp, tmp_list2, 0);
4e91a871 1911
1912 tmp_list3 = tmp_nosp = 0;
1913 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
1914 warn_for_collisions (tmp_nosp);
1915 merge_tlist (pbefore_sp, tmp_list3, 0);
1916
1917 tmp_list3 = tmp_list2 = 0;
1918 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
1919 warn_for_collisions (tmp_list2);
1920 merge_tlist (pbefore_sp, tmp_list3, 0);
1921 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1922 two first, to avoid warning for (a ? b++ : b++). */
1923 merge_tlist (&tmp_nosp, tmp_list2, 0);
1924 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1925 return;
1926
481c6ce6 1927 case PREDECREMENT_EXPR:
1928 case PREINCREMENT_EXPR:
1929 case POSTDECREMENT_EXPR:
1930 case POSTINCREMENT_EXPR:
4e91a871 1931 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
1932 return;
1933
1934 case MODIFY_EXPR:
1935 tmp_before = tmp_nosp = tmp_list3 = 0;
1936 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
1937 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
1938 /* Expressions inside the LHS are not ordered wrt. the sequence points
1939 in the RHS. Example:
1940 *a = (a++, 2)
1941 Despite the fact that the modification of "a" is in the before_sp
1942 list (tmp_before), it conflicts with the use of "a" in the LHS.
1943 We can handle this by adding the contents of tmp_list3
1944 to those of tmp_before, and redoing the collision warnings for that
1945 list. */
1946 add_tlist (&tmp_before, tmp_list3, x, 1);
1947 warn_for_collisions (tmp_before);
1948 /* Exclude the LHS itself here; we first have to merge it into the
1949 tmp_nosp list. This is done to avoid warning for "a = a"; if we
1950 didn't exclude the LHS, we'd get it twice, once as a read and once
1951 as a write. */
1952 add_tlist (pno_sp, tmp_list3, x, 0);
1953 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
1954
1955 merge_tlist (pbefore_sp, tmp_before, 0);
1956 if (warning_candidate_p (TREE_OPERAND (x, 0)))
1957 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
1958 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
1959 return;
481c6ce6 1960
1961 case CALL_EXPR:
4e91a871 1962 /* We need to warn about conflicts among arguments and conflicts between
1963 args and the function address. Side effects of the function address,
1964 however, are not ordered by the sequence point of the call. */
c2f47e15 1965 {
1966 call_expr_arg_iterator iter;
1967 tree arg;
48e1416a 1968 tmp_before = tmp_nosp = 0;
c2f47e15 1969 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
1970 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
1971 {
1972 tmp_list2 = tmp_list3 = 0;
1973 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
1974 merge_tlist (&tmp_list3, tmp_list2, 0);
1975 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
1976 }
1977 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
1978 warn_for_collisions (tmp_before);
1979 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
1980 return;
1981 }
481c6ce6 1982
1983 case TREE_LIST:
1984 /* Scan all the list, e.g. indices of multi dimensional array. */
1985 while (x)
1986 {
4e91a871 1987 tmp_before = tmp_nosp = 0;
1988 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
1989 merge_tlist (&tmp_nosp, tmp_before, 0);
1990 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
481c6ce6 1991 x = TREE_CHAIN (x);
1992 }
4e91a871 1993 return;
481c6ce6 1994
4e91a871 1995 case SAVE_EXPR:
1996 {
1997 struct tlist_cache *t;
1998 for (t = save_expr_cache; t; t = t->next)
79973b57 1999 if (candidate_equal_p (t->expr, x))
4e91a871 2000 break;
481c6ce6 2001
84166705 2002 if (!t)
481c6ce6 2003 {
9318f22c 2004 t = XOBNEW (&tlist_obstack, struct tlist_cache);
4e91a871 2005 t->next = save_expr_cache;
2006 t->expr = x;
2007 save_expr_cache = t;
2008
2009 tmp_before = tmp_nosp = 0;
2010 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2011 warn_for_collisions (tmp_nosp);
2012
2013 tmp_list3 = 0;
312243bb 2014 merge_tlist (&tmp_list3, tmp_nosp, 0);
4e91a871 2015 t->cache_before_sp = tmp_before;
2016 t->cache_after_sp = tmp_list3;
481c6ce6 2017 }
4e91a871 2018 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
2019 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
2020 return;
2021 }
481c6ce6 2022
012916cb 2023 case ADDR_EXPR:
2024 x = TREE_OPERAND (x, 0);
2025 if (DECL_P (x))
2026 return;
2027 writer = 0;
2028 goto restart;
2029
d582d140 2030 case VIEW_CONVERT_EXPR:
2031 if (location_wrapper_p (x))
2032 {
2033 x = TREE_OPERAND (x, 0);
2034 goto restart;
2035 }
2036 gcc_fallthrough ();
ce45a448 2037 default:
2038 /* For other expressions, simply recurse on their operands.
a0c938f0 2039 Manual tail recursion for unary expressions.
ce45a448 2040 Other non-expressions need not be processed. */
2041 if (cl == tcc_unary)
2042 {
ce45a448 2043 x = TREE_OPERAND (x, 0);
2044 writer = 0;
2045 goto restart;
2046 }
2047 else if (IS_EXPR_CODE_CLASS (cl))
2048 {
2049 int lp;
c2f47e15 2050 int max = TREE_OPERAND_LENGTH (x);
ce45a448 2051 for (lp = 0; lp < max; lp++)
2052 {
2053 tmp_before = tmp_nosp = 0;
2054 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
2055 merge_tlist (&tmp_nosp, tmp_before, 0);
2056 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2057 }
2058 }
2059 return;
481c6ce6 2060 }
481c6ce6 2061}
2062
974e2c0c 2063/* Try to warn for undefined behavior in EXPR due to missing sequence
481c6ce6 2064 points. */
2065
4b987fac 2066DEBUG_FUNCTION void
1cae46be 2067verify_sequence_points (tree expr)
481c6ce6 2068{
4e91a871 2069 struct tlist *before_sp = 0, *after_sp = 0;
481c6ce6 2070
4e91a871 2071 warned_ids = 0;
2072 save_expr_cache = 0;
2073 if (tlist_firstobj == 0)
481c6ce6 2074 {
4e91a871 2075 gcc_obstack_init (&tlist_obstack);
4fd61bc6 2076 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
481c6ce6 2077 }
2078
4e91a871 2079 verify_tree (expr, &before_sp, &after_sp, 0);
2080 warn_for_collisions (after_sp);
2081 obstack_free (&tlist_obstack, tlist_firstobj);
481c6ce6 2082}
b0fc3e72 2083\f
2084/* Validate the expression after `case' and apply default promotions. */
2085
2ca392fd 2086static tree
2d2f6a15 2087check_case_value (location_t loc, tree value)
b0fc3e72 2088{
2089 if (value == NULL_TREE)
2090 return value;
2091
b96dc121 2092 if (TREE_CODE (value) == INTEGER_CST)
2093 /* Promote char or short to int. */
2094 value = perform_integral_promotions (value);
2095 else if (value != error_mark_node)
b0fc3e72 2096 {
2d2f6a15 2097 error_at (loc, "case label does not reduce to an integer constant");
b0fc3e72 2098 value = error_mark_node;
2099 }
b0fc3e72 2100
6433f1c2 2101 constant_expression_warning (value);
2102
b0fc3e72 2103 return value;
2104}
2105\f
2ca392fd 2106/* See if the case values LOW and HIGH are in the range of the original
5c9dae64 2107 type (i.e. before the default conversion to int) of the switch testing
2ca392fd 2108 expression.
2109 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
91275768 2110 the type before promoting it. CASE_LOW_P is a pointer to the lower
2ca392fd 2111 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
2112 if the case is not a case range.
2113 The caller has to make sure that we are not called with NULL for
be23b16f 2114 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
2115 was a case value that doesn't fit into the range of the ORIG_TYPE.
442e3cb9 2116 Returns true if the case label is in range of ORIG_TYPE (saturated or
2ca392fd 2117 untouched) or false if the label is out of range. */
2118
2119static bool
f61a9bc2 2120check_case_bounds (location_t loc, tree type, tree orig_type,
be23b16f 2121 tree *case_low_p, tree *case_high_p,
2122 bool *outside_range_p)
2ca392fd 2123{
2124 tree min_value, max_value;
2125 tree case_low = *case_low_p;
2126 tree case_high = case_high_p ? *case_high_p : case_low;
2127
2128 /* If there was a problem with the original type, do nothing. */
2129 if (orig_type == error_mark_node)
2130 return true;
2131
2132 min_value = TYPE_MIN_VALUE (orig_type);
2133 max_value = TYPE_MAX_VALUE (orig_type);
2134
73f67931 2135 /* We'll really need integer constants here. */
2136 case_low = fold (case_low);
2137 case_high = fold (case_high);
2138
2ca392fd 2139 /* Case label is less than minimum for type. */
2140 if (tree_int_cst_compare (case_low, min_value) < 0
2141 && tree_int_cst_compare (case_high, min_value) < 0)
2142 {
f61a9bc2 2143 warning_at (loc, 0, "case label value is less than minimum value "
2144 "for type");
be23b16f 2145 *outside_range_p = true;
2ca392fd 2146 return false;
2147 }
b27ac6b5 2148
2ca392fd 2149 /* Case value is greater than maximum for type. */
2150 if (tree_int_cst_compare (case_low, max_value) > 0
2151 && tree_int_cst_compare (case_high, max_value) > 0)
2152 {
f61a9bc2 2153 warning_at (loc, 0, "case label value exceeds maximum value for type");
be23b16f 2154 *outside_range_p = true;
2ca392fd 2155 return false;
2156 }
2157
2158 /* Saturate lower case label value to minimum. */
2159 if (tree_int_cst_compare (case_high, min_value) >= 0
2160 && tree_int_cst_compare (case_low, min_value) < 0)
2161 {
f61a9bc2 2162 warning_at (loc, 0, "lower value in case label range"
2163 " less than minimum value for type");
be23b16f 2164 *outside_range_p = true;
2ca392fd 2165 case_low = min_value;
2166 }
b27ac6b5 2167
2ca392fd 2168 /* Saturate upper case label value to maximum. */
2169 if (tree_int_cst_compare (case_low, max_value) <= 0
2170 && tree_int_cst_compare (case_high, max_value) > 0)
2171 {
f61a9bc2 2172 warning_at (loc, 0, "upper value in case label range"
2173 " exceeds maximum value for type");
be23b16f 2174 *outside_range_p = true;
2ca392fd 2175 case_high = max_value;
2176 }
2177
2178 if (*case_low_p != case_low)
2179 *case_low_p = convert (type, case_low);
2180 if (case_high_p && *case_high_p != case_high)
2181 *case_high_p = convert (type, case_high);
2182
2183 return true;
2184}
2185\f
b0fc3e72 2186/* Return an integer type with BITS bits of precision,
2187 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2188
2189tree
1cae46be 2190c_common_type_for_size (unsigned int bits, int unsignedp)
b0fc3e72 2191{
9f75f026 2192 int i;
2193
46375237 2194 if (bits == TYPE_PRECISION (integer_type_node))
2195 return unsignedp ? unsigned_type_node : integer_type_node;
2196
bacde65a 2197 if (bits == TYPE_PRECISION (signed_char_type_node))
b0fc3e72 2198 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2199
bacde65a 2200 if (bits == TYPE_PRECISION (short_integer_type_node))
b0fc3e72 2201 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2202
bacde65a 2203 if (bits == TYPE_PRECISION (long_integer_type_node))
b0fc3e72 2204 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2205
bacde65a 2206 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b0fc3e72 2207 return (unsignedp ? long_long_unsigned_type_node
2208 : long_long_integer_type_node);
2209
9f75f026 2210 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2211 if (int_n_enabled_p[i]
2212 && bits == int_n_data[i].bitsize)
2213 return (unsignedp ? int_n_trees[i].unsigned_type
2214 : int_n_trees[i].signed_type);
6388cfe2 2215
f57fa2ea 2216 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2217 return (unsignedp ? widest_unsigned_literal_type_node
2218 : widest_integer_literal_type_node);
2219
bacde65a 2220 if (bits <= TYPE_PRECISION (intQI_type_node))
2221 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2222
2223 if (bits <= TYPE_PRECISION (intHI_type_node))
2224 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2225
2226 if (bits <= TYPE_PRECISION (intSI_type_node))
2227 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2228
2229 if (bits <= TYPE_PRECISION (intDI_type_node))
2230 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2231
78173203 2232 return NULL_TREE;
b0fc3e72 2233}
2234
9421ebb9 2235/* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2236 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2237 and saturating if SATP is nonzero, otherwise not saturating. */
2238
2239tree
2240c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2241 int unsignedp, int satp)
2242{
19a4dce4 2243 enum mode_class mclass;
9421ebb9 2244 if (ibit == 0)
19a4dce4 2245 mclass = unsignedp ? MODE_UFRACT : MODE_FRACT;
9421ebb9 2246 else
19a4dce4 2247 mclass = unsignedp ? MODE_UACCUM : MODE_ACCUM;
9421ebb9 2248
2b8f5b8a 2249 opt_scalar_mode opt_mode;
2250 scalar_mode mode;
2251 FOR_EACH_MODE_IN_CLASS (opt_mode, mclass)
2252 {
2253 mode = opt_mode.require ();
2254 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2255 break;
2256 }
9421ebb9 2257
2b8f5b8a 2258 if (!opt_mode.exists (&mode) || !targetm.scalar_mode_supported_p (mode))
9421ebb9 2259 {
2260 sorry ("GCC cannot support operators with integer types and "
2261 "fixed-point types that have too many integral and "
2262 "fractional bits together");
78173203 2263 return NULL_TREE;
9421ebb9 2264 }
2265
2266 return c_common_type_for_mode (mode, satp);
2267}
2268
5b247e9f 2269/* Used for communication between c_common_type_for_mode and
2270 c_register_builtin_type. */
c1917557 2271tree registered_builtin_types;
5b247e9f 2272
b0fc3e72 2273/* Return a data type that has machine mode MODE.
2274 If the mode is an integer,
9421ebb9 2275 then UNSIGNEDP selects between signed and unsigned types.
2276 If the mode is a fixed-point mode,
2277 then UNSIGNEDP selects between saturating and nonsaturating types. */
b0fc3e72 2278
2279tree
3754d046 2280c_common_type_for_mode (machine_mode mode, int unsignedp)
b0fc3e72 2281{
5b247e9f 2282 tree t;
9f75f026 2283 int i;
5b247e9f 2284
46375237 2285 if (mode == TYPE_MODE (integer_type_node))
2286 return unsignedp ? unsigned_type_node : integer_type_node;
2287
b0fc3e72 2288 if (mode == TYPE_MODE (signed_char_type_node))
2289 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2290
2291 if (mode == TYPE_MODE (short_integer_type_node))
2292 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2293
b0fc3e72 2294 if (mode == TYPE_MODE (long_integer_type_node))
2295 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2296
2297 if (mode == TYPE_MODE (long_long_integer_type_node))
2298 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2299
9f75f026 2300 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2301 if (int_n_enabled_p[i]
2302 && mode == int_n_data[i].m)
2303 return (unsignedp ? int_n_trees[i].unsigned_type
2304 : int_n_trees[i].signed_type);
6388cfe2 2305
88ae7f04 2306 if (mode == QImode)
bacde65a 2307 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2308
88ae7f04 2309 if (mode == HImode)
bacde65a 2310 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2311
88ae7f04 2312 if (mode == SImode)
bacde65a 2313 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2314
88ae7f04 2315 if (mode == DImode)
bacde65a 2316 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
ab2c1de8 2317
cc1cc1c7 2318#if HOST_BITS_PER_WIDE_INT >= 64
6274009c 2319 if (mode == TYPE_MODE (intTI_type_node))
2320 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
cc1cc1c7 2321#endif
6274009c 2322
b0fc3e72 2323 if (mode == TYPE_MODE (float_type_node))
2324 return float_type_node;
2325
2326 if (mode == TYPE_MODE (double_type_node))
2327 return double_type_node;
2328
2329 if (mode == TYPE_MODE (long_double_type_node))
2330 return long_double_type_node;
2331
82c85aba 2332 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2333 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2334 && mode == TYPE_MODE (FLOATN_NX_TYPE_NODE (i)))
2335 return FLOATN_NX_TYPE_NODE (i);
2336
545c2bde 2337 if (mode == TYPE_MODE (void_type_node))
2338 return void_type_node;
b27ac6b5 2339
7a6aeeed 2340 if (mode == TYPE_MODE (build_pointer_type (char_type_node))
2341 || mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2342 {
2343 unsigned int precision
2344 = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode));
2345 return (unsignedp
2346 ? make_unsigned_type (precision)
2347 : make_signed_type (precision));
2348 }
b0fc3e72 2349
0dfc45b5 2350 if (COMPLEX_MODE_P (mode))
2351 {
3754d046 2352 machine_mode inner_mode;
0dfc45b5 2353 tree inner_type;
2354
2355 if (mode == TYPE_MODE (complex_float_type_node))
2356 return complex_float_type_node;
2357 if (mode == TYPE_MODE (complex_double_type_node))
2358 return complex_double_type_node;
2359 if (mode == TYPE_MODE (complex_long_double_type_node))
2360 return complex_long_double_type_node;
2361
82c85aba 2362 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2363 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2364 && mode == TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i)))
2365 return COMPLEX_FLOATN_NX_TYPE_NODE (i);
2366
0dfc45b5 2367 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2368 return complex_integer_type_node;
2369
2370 inner_mode = GET_MODE_INNER (mode);
2371 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2372 if (inner_type != NULL_TREE)
2373 return build_complex_type (inner_type);
2374 }
8464736b 2375 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
2376 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
2377 {
2378 unsigned int elem_bits = vector_element_size (GET_MODE_BITSIZE (mode),
2379 GET_MODE_NUNITS (mode));
2380 tree bool_type = build_nonstandard_boolean_type (elem_bits);
2381 return build_vector_type_for_mode (bool_type, mode);
2382 }
f08ee65f 2383 else if (VECTOR_MODE_P (mode)
2384 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
4917c376 2385 {
3754d046 2386 machine_mode inner_mode = GET_MODE_INNER (mode);
4917c376 2387 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2388 if (inner_type != NULL_TREE)
2389 return build_vector_type_for_mode (inner_type, mode);
88ae7f04 2390 }
e2ea7e3a 2391
c4503c0a 2392 if (mode == TYPE_MODE (dfloat32_type_node))
2393 return dfloat32_type_node;
2394 if (mode == TYPE_MODE (dfloat64_type_node))
2395 return dfloat64_type_node;
2396 if (mode == TYPE_MODE (dfloat128_type_node))
2397 return dfloat128_type_node;
2398
9421ebb9 2399 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
2400 {
2401 if (mode == TYPE_MODE (short_fract_type_node))
2402 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
2403 if (mode == TYPE_MODE (fract_type_node))
2404 return unsignedp ? sat_fract_type_node : fract_type_node;
2405 if (mode == TYPE_MODE (long_fract_type_node))
2406 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
2407 if (mode == TYPE_MODE (long_long_fract_type_node))
2408 return unsignedp ? sat_long_long_fract_type_node
2409 : long_long_fract_type_node;
2410
2411 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
2412 return unsignedp ? sat_unsigned_short_fract_type_node
2413 : unsigned_short_fract_type_node;
2414 if (mode == TYPE_MODE (unsigned_fract_type_node))
2415 return unsignedp ? sat_unsigned_fract_type_node
2416 : unsigned_fract_type_node;
2417 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
2418 return unsignedp ? sat_unsigned_long_fract_type_node
2419 : unsigned_long_fract_type_node;
2420 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
2421 return unsignedp ? sat_unsigned_long_long_fract_type_node
2422 : unsigned_long_long_fract_type_node;
2423
2424 if (mode == TYPE_MODE (short_accum_type_node))
2425 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
2426 if (mode == TYPE_MODE (accum_type_node))
2427 return unsignedp ? sat_accum_type_node : accum_type_node;
2428 if (mode == TYPE_MODE (long_accum_type_node))
2429 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
2430 if (mode == TYPE_MODE (long_long_accum_type_node))
2431 return unsignedp ? sat_long_long_accum_type_node
2432 : long_long_accum_type_node;
2433
2434 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
2435 return unsignedp ? sat_unsigned_short_accum_type_node
2436 : unsigned_short_accum_type_node;
2437 if (mode == TYPE_MODE (unsigned_accum_type_node))
2438 return unsignedp ? sat_unsigned_accum_type_node
2439 : unsigned_accum_type_node;
2440 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
2441 return unsignedp ? sat_unsigned_long_accum_type_node
2442 : unsigned_long_accum_type_node;
2443 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
2444 return unsignedp ? sat_unsigned_long_long_accum_type_node
2445 : unsigned_long_long_accum_type_node;
2446
2447 if (mode == QQmode)
2448 return unsignedp ? sat_qq_type_node : qq_type_node;
2449 if (mode == HQmode)
2450 return unsignedp ? sat_hq_type_node : hq_type_node;
2451 if (mode == SQmode)
2452 return unsignedp ? sat_sq_type_node : sq_type_node;
2453 if (mode == DQmode)
2454 return unsignedp ? sat_dq_type_node : dq_type_node;
2455 if (mode == TQmode)
2456 return unsignedp ? sat_tq_type_node : tq_type_node;
2457
2458 if (mode == UQQmode)
2459 return unsignedp ? sat_uqq_type_node : uqq_type_node;
2460 if (mode == UHQmode)
2461 return unsignedp ? sat_uhq_type_node : uhq_type_node;
2462 if (mode == USQmode)
2463 return unsignedp ? sat_usq_type_node : usq_type_node;
2464 if (mode == UDQmode)
2465 return unsignedp ? sat_udq_type_node : udq_type_node;
2466 if (mode == UTQmode)
2467 return unsignedp ? sat_utq_type_node : utq_type_node;
2468
2469 if (mode == HAmode)
2470 return unsignedp ? sat_ha_type_node : ha_type_node;
2471 if (mode == SAmode)
2472 return unsignedp ? sat_sa_type_node : sa_type_node;
2473 if (mode == DAmode)
2474 return unsignedp ? sat_da_type_node : da_type_node;
2475 if (mode == TAmode)
2476 return unsignedp ? sat_ta_type_node : ta_type_node;
2477
2478 if (mode == UHAmode)
2479 return unsignedp ? sat_uha_type_node : uha_type_node;
2480 if (mode == USAmode)
2481 return unsignedp ? sat_usa_type_node : usa_type_node;
2482 if (mode == UDAmode)
2483 return unsignedp ? sat_uda_type_node : uda_type_node;
2484 if (mode == UTAmode)
2485 return unsignedp ? sat_uta_type_node : uta_type_node;
2486 }
2487
5b247e9f 2488 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
baec58e1 2489 if (TYPE_MODE (TREE_VALUE (t)) == mode
2490 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
5b247e9f 2491 return TREE_VALUE (t);
2492
78173203 2493 return NULL_TREE;
b0fc3e72 2494}
20d39783 2495
11773141 2496tree
2497c_common_unsigned_type (tree type)
2498{
2499 return c_common_signed_or_unsigned_type (1, type);
2500}
2501
20d39783 2502/* Return a signed type the same as TYPE in other respects. */
2503
2504tree
1cae46be 2505c_common_signed_type (tree type)
20d39783 2506{
4070745f 2507 return c_common_signed_or_unsigned_type (0, type);
20d39783 2508}
2509
2510/* Return a type the same as TYPE except unsigned or
2511 signed according to UNSIGNEDP. */
2512
2513tree
1cae46be 2514c_common_signed_or_unsigned_type (int unsignedp, tree type)
20d39783 2515{
7a91101f 2516 tree type1;
9f75f026 2517 int i;
20d39783 2518
7a91101f 2519 /* This block of code emulates the behavior of the old
2520 c_common_unsigned_type. In particular, it returns
2521 long_unsigned_type_node if passed a long, even when a int would
2522 have the same size. This is necessary for warnings to work
2523 correctly in archs where sizeof(int) == sizeof(long) */
2524
2525 type1 = TYPE_MAIN_VARIANT (type);
2526 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
2527 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2528 if (type1 == integer_type_node || type1 == unsigned_type_node)
2529 return unsignedp ? unsigned_type_node : integer_type_node;
2530 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
2531 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2532 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
2533 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2534 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
2535 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
9f75f026 2536
2537 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2538 if (int_n_enabled_p[i]
2539 && (type1 == int_n_trees[i].unsigned_type
2540 || type1 == int_n_trees[i].signed_type))
2541 return (unsignedp ? int_n_trees[i].unsigned_type
2542 : int_n_trees[i].signed_type);
2543
7a91101f 2544#if HOST_BITS_PER_WIDE_INT >= 64
2545 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
2546 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2547#endif
2548 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
2549 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2550 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
2551 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2552 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
2553 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2554 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
2555 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2556
9f92e1a2 2557#define C_COMMON_FIXED_TYPES(NAME) \
2558 if (type1 == short_ ## NAME ## _type_node \
2559 || type1 == unsigned_short_ ## NAME ## _type_node) \
2560 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
2561 : short_ ## NAME ## _type_node; \
2562 if (type1 == NAME ## _type_node \
2563 || type1 == unsigned_ ## NAME ## _type_node) \
2564 return unsignedp ? unsigned_ ## NAME ## _type_node \
2565 : NAME ## _type_node; \
2566 if (type1 == long_ ## NAME ## _type_node \
2567 || type1 == unsigned_long_ ## NAME ## _type_node) \
2568 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
2569 : long_ ## NAME ## _type_node; \
2570 if (type1 == long_long_ ## NAME ## _type_node \
2571 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
2572 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
2573 : long_long_ ## NAME ## _type_node;
2574
2575#define C_COMMON_FIXED_MODE_TYPES(NAME) \
2576 if (type1 == NAME ## _type_node \
2577 || type1 == u ## NAME ## _type_node) \
2578 return unsignedp ? u ## NAME ## _type_node \
2579 : NAME ## _type_node;
2580
2581#define C_COMMON_FIXED_TYPES_SAT(NAME) \
2582 if (type1 == sat_ ## short_ ## NAME ## _type_node \
2583 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
2584 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
2585 : sat_ ## short_ ## NAME ## _type_node; \
2586 if (type1 == sat_ ## NAME ## _type_node \
2587 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
2588 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
2589 : sat_ ## NAME ## _type_node; \
2590 if (type1 == sat_ ## long_ ## NAME ## _type_node \
2591 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
2592 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
2593 : sat_ ## long_ ## NAME ## _type_node; \
2594 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
2595 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
2596 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
2597 : sat_ ## long_long_ ## NAME ## _type_node;
2598
2599#define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
2600 if (type1 == sat_ ## NAME ## _type_node \
2601 || type1 == sat_ ## u ## NAME ## _type_node) \
2602 return unsignedp ? sat_ ## u ## NAME ## _type_node \
2603 : sat_ ## NAME ## _type_node;
2604
2605 C_COMMON_FIXED_TYPES (fract);
2606 C_COMMON_FIXED_TYPES_SAT (fract);
2607 C_COMMON_FIXED_TYPES (accum);
2608 C_COMMON_FIXED_TYPES_SAT (accum);
2609
2610 C_COMMON_FIXED_MODE_TYPES (qq);
2611 C_COMMON_FIXED_MODE_TYPES (hq);
2612 C_COMMON_FIXED_MODE_TYPES (sq);
2613 C_COMMON_FIXED_MODE_TYPES (dq);
2614 C_COMMON_FIXED_MODE_TYPES (tq);
2615 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
2616 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
2617 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
2618 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
2619 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
2620 C_COMMON_FIXED_MODE_TYPES (ha);
2621 C_COMMON_FIXED_MODE_TYPES (sa);
2622 C_COMMON_FIXED_MODE_TYPES (da);
2623 C_COMMON_FIXED_MODE_TYPES (ta);
2624 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
2625 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
2626 C_COMMON_FIXED_MODE_TYPES_SAT (da);
2627 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
9421ebb9 2628
4f7f7efd 2629 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
2630 the precision; they have precision set to match their range, but
2631 may use a wider mode to match an ABI. If we change modes, we may
2632 wind up with bad conversions. For INTEGER_TYPEs in C, must check
2633 the precision as well, so as to yield correct results for
2634 bit-field types. C++ does not have these separate bit-field
2635 types, and producing a signed or unsigned variant of an
2636 ENUMERAL_TYPE may cause other problems as well. */
2637
ac265864 2638 if (!INTEGRAL_TYPE_P (type)
2639 || TYPE_UNSIGNED (type) == unsignedp)
2640 return type;
2641
4f7f7efd 2642#define TYPE_OK(node) \
2643 (TYPE_MODE (type) == TYPE_MODE (node) \
0c4abe5b 2644 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
4f7f7efd 2645 if (TYPE_OK (signed_char_type_node))
20d39783 2646 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
4f7f7efd 2647 if (TYPE_OK (integer_type_node))
20d39783 2648 return unsignedp ? unsigned_type_node : integer_type_node;
4f7f7efd 2649 if (TYPE_OK (short_integer_type_node))
20d39783 2650 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
4f7f7efd 2651 if (TYPE_OK (long_integer_type_node))
20d39783 2652 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
4f7f7efd 2653 if (TYPE_OK (long_long_integer_type_node))
20d39783 2654 return (unsignedp ? long_long_unsigned_type_node
2655 : long_long_integer_type_node);
9f75f026 2656
2657 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2658 if (int_n_enabled_p[i]
2659 && TYPE_MODE (type) == int_n_data[i].m
2660 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
2661 return (unsignedp ? int_n_trees[i].unsigned_type
2662 : int_n_trees[i].signed_type);
2663
ef11801e 2664#if HOST_BITS_PER_WIDE_INT >= 64
4f7f7efd 2665 if (TYPE_OK (intTI_type_node))
ef11801e 2666 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2667#endif
4f7f7efd 2668 if (TYPE_OK (intDI_type_node))
ef11801e 2669 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
4f7f7efd 2670 if (TYPE_OK (intSI_type_node))
ef11801e 2671 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
4f7f7efd 2672 if (TYPE_OK (intHI_type_node))
ef11801e 2673 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
4f7f7efd 2674 if (TYPE_OK (intQI_type_node))
ef11801e 2675 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
4f7f7efd 2676#undef TYPE_OK
ef11801e 2677
0c4abe5b 2678 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
20d39783 2679}
b268e47e 2680
c0e47fd4 2681/* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
2682
2683tree
2684c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
2685{
9f75f026 2686 int i;
2687
c0e47fd4 2688 /* Extended integer types of the same width as a standard type have
2689 lesser rank, so those of the same width as int promote to int or
2690 unsigned int and are valid for printf formats expecting int or
2691 unsigned int. To avoid such special cases, avoid creating
2692 extended integer types for bit-fields if a standard integer type
2693 is available. */
2694 if (width == TYPE_PRECISION (integer_type_node))
2695 return unsignedp ? unsigned_type_node : integer_type_node;
2696 if (width == TYPE_PRECISION (signed_char_type_node))
2697 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2698 if (width == TYPE_PRECISION (short_integer_type_node))
2699 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2700 if (width == TYPE_PRECISION (long_integer_type_node))
2701 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2702 if (width == TYPE_PRECISION (long_long_integer_type_node))
2703 return (unsignedp ? long_long_unsigned_type_node
2704 : long_long_integer_type_node);
9f75f026 2705 for (i = 0; i < NUM_INT_N_ENTS; i ++)
2706 if (int_n_enabled_p[i]
2707 && width == int_n_data[i].bitsize)
2708 return (unsignedp ? int_n_trees[i].unsigned_type
2709 : int_n_trees[i].signed_type);
c0e47fd4 2710 return build_nonstandard_integer_type (width, unsignedp);
2711}
2712
b268e47e 2713/* The C version of the register_builtin_type langhook. */
2714
2715void
2716c_register_builtin_type (tree type, const char* name)
2717{
2718 tree decl;
2719
e60a6f7b 2720 decl = build_decl (UNKNOWN_LOCATION,
2721 TYPE_DECL, get_identifier (name), type);
b268e47e 2722 DECL_ARTIFICIAL (decl) = 1;
2723 if (!TYPE_NAME (type))
2724 TYPE_NAME (type) = decl;
cdf05a3f 2725 lang_hooks.decls.pushdecl (decl);
5b247e9f 2726
2727 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
b268e47e 2728}
a9b9d10c 2729\f
aff9e656 2730/* Print an error message for invalid operands to arith operation
8e70fb09 2731 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
c0bf500c 2732 RICHLOC is a rich location for the message, containing either
2733 three separate locations for each of the operator and operands
2734
2735 lhs op rhs
2736 ~~~ ^~ ~~~
2737
2738 (C FE), or one location ranging over all over them
2739
2740 lhs op rhs
2741 ~~~~^~~~~~
2742
2743 (C++ FE). */
b0fc3e72 2744
2745void
c0bf500c 2746binary_op_error (rich_location *richloc, enum tree_code code,
8e70fb09 2747 tree type0, tree type1)
b0fc3e72 2748{
19cb6b50 2749 const char *opname;
f03946e4 2750
b0fc3e72 2751 switch (code)
2752 {
b0fc3e72 2753 case PLUS_EXPR:
2754 opname = "+"; break;
2755 case MINUS_EXPR:
2756 opname = "-"; break;
2757 case MULT_EXPR:
2758 opname = "*"; break;
2759 case MAX_EXPR:
2760 opname = "max"; break;
2761 case MIN_EXPR:
2762 opname = "min"; break;
2763 case EQ_EXPR:
2764 opname = "=="; break;
2765 case NE_EXPR:
2766 opname = "!="; break;
2767 case LE_EXPR:
2768 opname = "<="; break;
2769 case GE_EXPR:
2770 opname = ">="; break;
2771 case LT_EXPR:
2772 opname = "<"; break;
2773 case GT_EXPR:
2774 opname = ">"; break;
2775 case LSHIFT_EXPR:
2776 opname = "<<"; break;
2777 case RSHIFT_EXPR:
2778 opname = ">>"; break;
2779 case TRUNC_MOD_EXPR:
66618a1e 2780 case FLOOR_MOD_EXPR:
b0fc3e72 2781 opname = "%"; break;
2782 case TRUNC_DIV_EXPR:
66618a1e 2783 case FLOOR_DIV_EXPR:
b0fc3e72 2784 opname = "/"; break;
2785 case BIT_AND_EXPR:
2786 opname = "&"; break;
2787 case BIT_IOR_EXPR:
2788 opname = "|"; break;
2789 case TRUTH_ANDIF_EXPR:
2790 opname = "&&"; break;
2791 case TRUTH_ORIF_EXPR:
2792 opname = "||"; break;
2793 case BIT_XOR_EXPR:
2794 opname = "^"; break;
31f820d2 2795 default:
315ba355 2796 gcc_unreachable ();
b0fc3e72 2797 }
3b6578b3 2798 error_at (richloc,
2799 "invalid operands to binary %s (have %qT and %qT)",
2800 opname, type0, type1);
b0fc3e72 2801}
2802\f
03fe1dc2 2803/* Given an expression as a tree, return its original type. Do this
2804 by stripping any conversion that preserves the sign and precision. */
2805static tree
2806expr_original_type (tree expr)
2807{
2808 STRIP_SIGN_NOPS (expr);
2809 return TREE_TYPE (expr);
2810}
2811
b0fc3e72 2812/* Subroutine of build_binary_op, used for comparison operations.
2813 See if the operands have both been converted from subword integer types
2814 and, if so, perhaps change them both back to their original type.
5b511807 2815 This function is also responsible for converting the two operands
2816 to the proper common type for comparison.
b0fc3e72 2817
2818 The arguments of this function are all pointers to local variables
2819 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2820 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2821
2623625f 2822 LOC is the location of the comparison.
2823
78173203 2824 If this function returns non-NULL_TREE, it means that the comparison has
b0fc3e72 2825 a constant value. What this function returns is an expression for
2826 that value. */
2827
2828tree
2623625f 2829shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
2830 tree *restype_ptr, enum tree_code *rescode_ptr)
b0fc3e72 2831{
19cb6b50 2832 tree type;
b0fc3e72 2833 tree op0 = *op0_ptr;
2834 tree op1 = *op1_ptr;
2835 int unsignedp0, unsignedp1;
2836 int real1, real2;
2837 tree primop0, primop1;
2838 enum tree_code code = *rescode_ptr;
2839
2840 /* Throw away any conversions to wider types
2841 already present in the operands. */
2842
7f506bca 2843 primop0 = c_common_get_narrower (op0, &unsignedp0);
2844 primop1 = c_common_get_narrower (op1, &unsignedp1);
b0fc3e72 2845
119d06b2 2846 /* If primopN is first sign-extended from primopN's precision to opN's
2847 precision, then zero-extended from opN's precision to
2848 *restype_ptr precision, shortenings might be invalid. */
2849 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
2850 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
2851 && !unsignedp0
2852 && TYPE_UNSIGNED (TREE_TYPE (op0)))
2853 primop0 = op0;
2854 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
2855 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
2856 && !unsignedp1
2857 && TYPE_UNSIGNED (TREE_TYPE (op1)))
2858 primop1 = op1;
2859
b0fc3e72 2860 /* Handle the case that OP0 does not *contain* a conversion
2861 but it *requires* conversion to FINAL_TYPE. */
2862
2863 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
78a8ed03 2864 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
b0fc3e72 2865 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
78a8ed03 2866 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b0fc3e72 2867
2868 /* If one of the operands must be floated, we cannot optimize. */
2869 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2870 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
ab2c1de8 2871
b0fc3e72 2872 /* If first arg is constant, swap the args (changing operation
2bd278cc 2873 so value is preserved), for canonicalization. Don't do this if
2874 the second arg is 0. */
b0fc3e72 2875
2bd278cc 2876 if (TREE_CONSTANT (primop0)
9421ebb9 2877 && !integer_zerop (primop1) && !real_zerop (primop1)
2878 && !fixed_zerop (primop1))
b0fc3e72 2879 {
dfcf26a5 2880 std::swap (primop0, primop1);
2881 std::swap (op0, op1);
b0fc3e72 2882 *op0_ptr = op0;
2883 *op1_ptr = op1;
dfcf26a5 2884 std::swap (unsignedp0, unsignedp1);
2885 std::swap (real1, real2);
b0fc3e72 2886
2887 switch (code)
2888 {
2889 case LT_EXPR:
2890 code = GT_EXPR;
2891 break;
2892 case GT_EXPR:
2893 code = LT_EXPR;
2894 break;
2895 case LE_EXPR:
2896 code = GE_EXPR;
2897 break;
2898 case GE_EXPR:
2899 code = LE_EXPR;
2900 break;
31f820d2 2901 default:
2902 break;
b0fc3e72 2903 }
2904 *rescode_ptr = code;
2905 }
2906
2907 /* If comparing an integer against a constant more bits wide,
2908 maybe we can deduce a value of 1 or 0 independent of the data.
2909 Or else truncate the constant now
2910 rather than extend the variable at run time.
2911
2912 This is only interesting if the constant is the wider arg.
2913 Also, it is not safe if the constant is unsigned and the
2914 variable arg is signed, since in this case the variable
2915 would be sign-extended and then regarded as unsigned.
2916 Our technique fails in this case because the lowest/highest
2917 possible unsigned results don't follow naturally from the
2918 lowest/highest possible values of the variable operand.
2919 For just EQ_EXPR and NE_EXPR there is another technique that
2920 could be used: see if the constant can be faithfully represented
2921 in the other operand's type, by truncating it and reextending it
2922 and see if that preserves the constant's value. */
2923
2924 if (!real1 && !real2
9421ebb9 2925 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
b0fc3e72 2926 && TREE_CODE (primop1) == INTEGER_CST
2927 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2928 {
2929 int min_gt, max_gt, min_lt, max_lt;
2930 tree maxval, minval;
2931 /* 1 if comparison is nominally unsigned. */
78a8ed03 2932 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
b0fc3e72 2933 tree val;
2934
4070745f 2935 type = c_common_signed_or_unsigned_type (unsignedp0,
2936 TREE_TYPE (primop0));
cda09c61 2937
b0fc3e72 2938 maxval = TYPE_MAX_VALUE (type);
2939 minval = TYPE_MIN_VALUE (type);
2940
2941 if (unsignedp && !unsignedp0)
4070745f 2942 *restype_ptr = c_common_signed_type (*restype_ptr);
b0fc3e72 2943
2944 if (TREE_TYPE (primop1) != *restype_ptr)
18dbec6f 2945 {
9a5e8086 2946 /* Convert primop1 to target type, but do not introduce
2947 additional overflow. We know primop1 is an int_cst. */
e913b5cd 2948 primop1 = force_fit_type (*restype_ptr,
ee31dd3d 2949 wi::to_wide
2950 (primop1,
2951 TYPE_PRECISION (*restype_ptr)),
e913b5cd 2952 0, TREE_OVERFLOW (primop1));
18dbec6f 2953 }
b0fc3e72 2954 if (type != *restype_ptr)
2955 {
2956 minval = convert (*restype_ptr, minval);
2957 maxval = convert (*restype_ptr, maxval);
2958 }
2959
d99d10ca 2960 min_gt = tree_int_cst_lt (primop1, minval);
2961 max_gt = tree_int_cst_lt (primop1, maxval);
2962 min_lt = tree_int_cst_lt (minval, primop1);
2963 max_lt = tree_int_cst_lt (maxval, primop1);
b0fc3e72 2964
2965 val = 0;
2966 /* This used to be a switch, but Genix compiler can't handle that. */
2967 if (code == NE_EXPR)
2968 {
2969 if (max_lt || min_gt)
3c2239cf 2970 val = truthvalue_true_node;
b0fc3e72 2971 }
2972 else if (code == EQ_EXPR)
2973 {
2974 if (max_lt || min_gt)
3c2239cf 2975 val = truthvalue_false_node;
b0fc3e72 2976 }
2977 else if (code == LT_EXPR)
2978 {
2979 if (max_lt)
3c2239cf 2980 val = truthvalue_true_node;
b0fc3e72 2981 if (!min_lt)
3c2239cf 2982 val = truthvalue_false_node;
b0fc3e72 2983 }
2984 else if (code == GT_EXPR)
2985 {
2986 if (min_gt)
3c2239cf 2987 val = truthvalue_true_node;
b0fc3e72 2988 if (!max_gt)
3c2239cf 2989 val = truthvalue_false_node;
b0fc3e72 2990 }
2991 else if (code == LE_EXPR)
2992 {
2993 if (!max_gt)
3c2239cf 2994 val = truthvalue_true_node;
b0fc3e72 2995 if (min_gt)
3c2239cf 2996 val = truthvalue_false_node;
b0fc3e72 2997 }
2998 else if (code == GE_EXPR)
2999 {
3000 if (!min_lt)
3c2239cf 3001 val = truthvalue_true_node;
b0fc3e72 3002 if (max_lt)
3c2239cf 3003 val = truthvalue_false_node;
b0fc3e72 3004 }
3005
3006 /* If primop0 was sign-extended and unsigned comparison specd,
3007 we did a signed comparison above using the signed type bounds.
3008 But the comparison we output must be unsigned.
3009
3010 Also, for inequalities, VAL is no good; but if the signed
3011 comparison had *any* fixed result, it follows that the
3012 unsigned comparison just tests the sign in reverse
3013 (positive values are LE, negative ones GE).
3014 So we can generate an unsigned comparison
3015 against an extreme value of the signed type. */
3016
3017 if (unsignedp && !unsignedp0)
3018 {
3019 if (val != 0)
3020 switch (code)
3021 {
3022 case LT_EXPR:
3023 case GE_EXPR:
3024 primop1 = TYPE_MIN_VALUE (type);
3025 val = 0;
3026 break;
3027
3028 case LE_EXPR:
3029 case GT_EXPR:
3030 primop1 = TYPE_MAX_VALUE (type);
3031 val = 0;
3032 break;
31f820d2 3033
3034 default:
3035 break;
b0fc3e72 3036 }
11773141 3037 type = c_common_unsigned_type (type);
b0fc3e72 3038 }
3039
2d3d3af7 3040 if (TREE_CODE (primop0) != INTEGER_CST
01f11119 3041 /* Don't warn if it's from a (non-system) macro. */
3042 && !(from_macro_expansion_at
3043 (expansion_point_location_if_in_system_header
3044 (EXPR_LOCATION (primop0)))))
b0fc3e72 3045 {
3c2239cf 3046 if (val == truthvalue_false_node)
03fe1dc2 3047 warning_at (loc, OPT_Wtype_limits,
3048 "comparison is always false due to limited range of data type");
3c2239cf 3049 if (val == truthvalue_true_node)
03fe1dc2 3050 warning_at (loc, OPT_Wtype_limits,
3051 "comparison is always true due to limited range of data type");
b0fc3e72 3052 }
3053
3054 if (val != 0)
3055 {
3056 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3057 if (TREE_SIDE_EFFECTS (primop0))
14ae0310 3058 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
b0fc3e72 3059 return val;
3060 }
3061
3062 /* Value is not predetermined, but do the comparison
3063 in the type of the operand that is not constant.
3064 TYPE is already properly set. */
3065 }
c4503c0a 3066
3067 /* If either arg is decimal float and the other is float, find the
3068 proper common type to use for comparison. */
7fd22aae 3069 else if (real1 && real2
3070 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
3071 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
3072 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3073
3074 /* If either arg is decimal float and the other is float, fail. */
c4503c0a 3075 else if (real1 && real2
3076 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
3077 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
78173203 3078 return NULL_TREE;
c4503c0a 3079
b0fc3e72 3080 else if (real1 && real2
2203bd5c 3081 && (TYPE_PRECISION (TREE_TYPE (primop0))
3082 == TYPE_PRECISION (TREE_TYPE (primop1))))
b0fc3e72 3083 type = TREE_TYPE (primop0);
3084
3085 /* If args' natural types are both narrower than nominal type
3086 and both extend in the same manner, compare them
3087 in the type of the wider arg.
3088 Otherwise must actually extend both to the nominal
3089 common type lest different ways of extending
3090 alter the result.
3091 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
3092
3093 else if (unsignedp0 == unsignedp1 && real1 == real2
3094 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
3095 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
3096 {
3097 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4070745f 3098 type = c_common_signed_or_unsigned_type (unsignedp0
78a8ed03 3099 || TYPE_UNSIGNED (*restype_ptr),
4070745f 3100 type);
b0fc3e72 3101 /* Make sure shorter operand is extended the right way
3102 to match the longer operand. */
4070745f 3103 primop0
3104 = convert (c_common_signed_or_unsigned_type (unsignedp0,
3105 TREE_TYPE (primop0)),
3106 primop0);
3107 primop1
3108 = convert (c_common_signed_or_unsigned_type (unsignedp1,
3109 TREE_TYPE (primop1)),
3110 primop1);
b0fc3e72 3111 }
3112 else
3113 {
3114 /* Here we must do the comparison on the nominal type
3115 using the args exactly as we received them. */
3116 type = *restype_ptr;
3117 primop0 = op0;
3118 primop1 = op1;
3119
3bb711c4 3120 /* We want to fold unsigned comparisons of >= and < against zero.
3121 For these, we may also issue a warning if we have a non-constant
3122 compared against zero, where the zero was spelled as "0" (rather
3123 than merely folding to it).
3124 If we have at least one constant, then op1 is constant
3125 and we may have a non-constant expression as op0. */
b0fc3e72 3126 if (!real1 && !real2 && integer_zerop (primop1)
78a8ed03 3127 && TYPE_UNSIGNED (*restype_ptr))
b0fc3e72 3128 {
72749341 3129 tree value = NULL_TREE;
03fe1dc2 3130 /* All unsigned values are >= 0, so we warn. However,
3131 if OP0 is a constant that is >= 0, the signedness of
3132 the comparison isn't an issue, so suppress the
3133 warning. */
3bb711c4 3134 tree folded_op0 = fold_for_warn (op0);
03fe1dc2 3135 bool warn =
3df42822 3136 warn_type_limits && !in_system_header_at (loc)
3bb711c4 3137 && !(TREE_CODE (folded_op0) == INTEGER_CST
03fe1dc2 3138 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3bb711c4 3139 folded_op0)))
03fe1dc2 3140 /* Do not warn for enumeration types. */
3bb711c4 3141 && (TREE_CODE (expr_original_type (folded_op0)) != ENUMERAL_TYPE);
03fe1dc2 3142
b0fc3e72 3143 switch (code)
3144 {
3145 case GE_EXPR:
03fe1dc2 3146 if (warn)
3147 warning_at (loc, OPT_Wtype_limits,
3148 "comparison of unsigned expression >= 0 is always true");
3c2239cf 3149 value = truthvalue_true_node;
b0fc3e72 3150 break;
3151
3152 case LT_EXPR:
03fe1dc2 3153 if (warn)
3154 warning_at (loc, OPT_Wtype_limits,
3155 "comparison of unsigned expression < 0 is always false");
3c2239cf 3156 value = truthvalue_false_node;
31f820d2 3157 break;
3158
3159 default:
3160 break;
b0fc3e72 3161 }
3162
72749341 3163 if (value != NULL_TREE)
b0fc3e72 3164 {
3165 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3166 if (TREE_SIDE_EFFECTS (primop0))
14ae0310 3167 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3168 primop0, value);
b0fc3e72 3169 return value;
3170 }
3171 }
3172 }
3173
3174 *op0_ptr = convert (type, primop0);
3175 *op1_ptr = convert (type, primop1);
3176
3c2239cf 3177 *restype_ptr = truthvalue_type_node;
b0fc3e72 3178
78173203 3179 return NULL_TREE;
b0fc3e72 3180}
3181\f
1c26100f 3182/* Return a tree for the sum or difference (RESULTCODE says which)
3183 of pointer PTROP and integer INTOP. */
3184
3185tree
389dd41b 3186pointer_int_sum (location_t loc, enum tree_code resultcode,
42f9a786 3187 tree ptrop, tree intop, bool complain)
1c26100f 3188{
add6ee5e 3189 tree size_exp, ret;
1c26100f 3190
1c26100f 3191 /* The result is a pointer of the same type that is being added. */
1c26100f 3192 tree result_type = TREE_TYPE (ptrop);
3193
3194 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
3195 {
42f9a786 3196 if (complain && warn_pointer_arith)
3197 pedwarn (loc, OPT_Wpointer_arith,
3198 "pointer of type %<void *%> used in arithmetic");
3199 else if (!complain)
3200 return error_mark_node;
1c26100f 3201 size_exp = integer_one_node;
3202 }
3203 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3204 {
42f9a786 3205 if (complain && warn_pointer_arith)
3206 pedwarn (loc, OPT_Wpointer_arith,
3207 "pointer to a function used in arithmetic");
3208 else if (!complain)
3209 return error_mark_node;
1c26100f 3210 size_exp = integer_one_node;
3211 }
1c26100f 3212 else
22a3f7bd 3213 size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type));
1c26100f 3214
add6ee5e 3215 /* We are manipulating pointer values, so we don't need to warn
3216 about relying on undefined signed overflow. We disable the
3217 warning here because we use integer types so fold won't know that
3218 they are really pointers. */
3219 fold_defer_overflow_warnings ();
3220
1c26100f 3221 /* If what we are about to multiply by the size of the elements
3222 contains a constant term, apply distributive law
3223 and multiply that constant term separately.
3224 This helps produce common subexpressions. */
1c26100f 3225 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
84166705 3226 && !TREE_CONSTANT (intop)
1c26100f 3227 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3228 && TREE_CONSTANT (size_exp)
3229 /* If the constant comes from pointer subtraction,
3230 skip this optimization--it would cause an error. */
3231 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3232 /* If the constant is unsigned, and smaller than the pointer size,
3233 then we must skip this optimization. This is because it could cause
3234 an overflow error if the constant is negative but INTOP is not. */
84166705 3235 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
1c26100f 3236 || (TYPE_PRECISION (TREE_TYPE (intop))
3237 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
3238 {
3239 enum tree_code subcode = resultcode;
3240 tree int_type = TREE_TYPE (intop);
3241 if (TREE_CODE (intop) == MINUS_EXPR)
3242 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3243 /* Convert both subexpression types to the type of intop,
3244 because weird cases involving pointer arithmetic
3245 can result in a sum or difference with different type args. */
8e70fb09 3246 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
3247 subcode, ptrop,
623ee358 3248 convert (int_type, TREE_OPERAND (intop, 1)),
3249 true);
1c26100f 3250 intop = convert (int_type, TREE_OPERAND (intop, 0));
3251 }
3252
3253 /* Convert the integer argument to a type the same size as sizetype
3254 so the multiply won't overflow spuriously. */
1c26100f 3255 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
78a8ed03 3256 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
1cae46be 3257 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
78a8ed03 3258 TYPE_UNSIGNED (sizetype)), intop);
1c26100f 3259
3260 /* Replace the integer argument with a suitable product by the object size.
c6feb9f1 3261 Do this multiplication as signed, then convert to the appropriate type
8032877c 3262 for the pointer operation and disregard an overflow that occurred only
c6feb9f1 3263 because of the sign-extension change in the latter conversion. */
3264 {
f576a2e4 3265 tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
3266 convert (TREE_TYPE (intop), size_exp));
c6feb9f1 3267 intop = convert (sizetype, t);
3268 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
e3d0f65c 3269 intop = wide_int_to_tree (TREE_TYPE (intop), wi::to_wide (intop));
c6feb9f1 3270 }
0de36bdb 3271
499e523f 3272 /* Create the sum or difference. */
0de36bdb 3273 if (resultcode == MINUS_EXPR)
389dd41b 3274 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
1c26100f 3275
2cc66f2a 3276 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
add6ee5e 3277
3278 fold_undefer_and_ignore_overflow_warnings ();
3279
3280 return ret;
1c26100f 3281}
3282\f
f59e3889 3283/* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3284 and if NON_CONST is known not to be permitted in an evaluated part
3285 of a constant expression. */
3286
3287tree
3288c_wrap_maybe_const (tree expr, bool non_const)
3289{
3290 bool nowarning = TREE_NO_WARNING (expr);
3291 location_t loc = EXPR_LOCATION (expr);
3292
3293 /* This should never be called for C++. */
3294 if (c_dialect_cxx ())
3295 gcc_unreachable ();
3296
3297 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
3298 STRIP_TYPE_NOPS (expr);
3299 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
3300 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
3301 if (nowarning)
3302 TREE_NO_WARNING (expr) = 1;
3303 protected_set_expr_location (expr, loc);
3304
3305 return expr;
3306}
3307
6b68e71a 3308/* Return whether EXPR is a declaration whose address can never be
3309 NULL. */
3310
3311bool
9f627b1a 3312decl_with_nonnull_addr_p (const_tree expr)
6b68e71a 3313{
3314 return (DECL_P (expr)
3315 && (TREE_CODE (expr) == PARM_DECL
3316 || TREE_CODE (expr) == LABEL_DECL
3317 || !DECL_WEAK (expr)));
3318}
3319
b0fc3e72 3320/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
45a78cc0 3321 or for an `if' or `while' statement or ?..: exp. It should already
3322 have been validated to be of suitable type; otherwise, a bad
3323 diagnostic may result.
b0fc3e72 3324
8e70fb09 3325 The EXPR is located at LOCATION.
3326
b0fc3e72 3327 This preparation consists of taking the ordinary
3328 representation of an expression expr and producing a valid tree
3329 boolean expression describing whether expr is nonzero. We could
3c2239cf 3330 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
b0fc3e72 3331 but we optimize comparisons, &&, ||, and !.
3332
3c2239cf 3333 The resulting type should always be `truthvalue_type_node'. */
b0fc3e72 3334
3335tree
8e70fb09 3336c_common_truthvalue_conversion (location_t location, tree expr)
b0fc3e72 3337{
d582d140 3338 STRIP_ANY_LOCATION_WRAPPER (expr);
b0fc3e72 3339 switch (TREE_CODE (expr))
3340 {
318a728f 3341 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
e1232ce2 3342 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3343 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
3344 case ORDERED_EXPR: case UNORDERED_EXPR:
ce04dcdc 3345 if (TREE_TYPE (expr) == truthvalue_type_node)
3346 return expr;
e60a6f7b 3347 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
ce04dcdc 3348 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
e60a6f7b 3349 goto ret;
ce04dcdc 3350
b0fc3e72 3351 case TRUTH_ANDIF_EXPR:
3352 case TRUTH_ORIF_EXPR:
3353 case TRUTH_AND_EXPR:
3354 case TRUTH_OR_EXPR:
31f6e93c 3355 case TRUTH_XOR_EXPR:
ce04dcdc 3356 if (TREE_TYPE (expr) == truthvalue_type_node)
3357 return expr;
e60a6f7b 3358 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
48e1416a 3359 c_common_truthvalue_conversion (location,
e60a6f7b 3360 TREE_OPERAND (expr, 0)),
3361 c_common_truthvalue_conversion (location,
3362 TREE_OPERAND (expr, 1)));
3363 goto ret;
3e851b85 3364
f6e28f72 3365 case TRUTH_NOT_EXPR:
ce04dcdc 3366 if (TREE_TYPE (expr) == truthvalue_type_node)
3367 return expr;
e60a6f7b 3368 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
3369 c_common_truthvalue_conversion (location,
3370 TREE_OPERAND (expr, 0)));
3371 goto ret;
f6e28f72 3372
b0fc3e72 3373 case ERROR_MARK:
3374 return expr;
ab2c1de8 3375
b0fc3e72 3376 case INTEGER_CST:
0365bfa8 3377 if (TREE_CODE (TREE_TYPE (expr)) == ENUMERAL_TYPE
3378 && !integer_zerop (expr)
3379 && !integer_onep (expr))
3380 warning_at (location, OPT_Wint_in_bool_context,
3381 "enum constant in boolean context");
eddad94a 3382 return integer_zerop (expr) ? truthvalue_false_node
3383 : truthvalue_true_node;
b0fc3e72 3384
3385 case REAL_CST:
5000e21c 3386 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
3387 ? truthvalue_true_node
3388 : truthvalue_false_node;
b0fc3e72 3389
9421ebb9 3390 case FIXED_CST:
3391 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
3392 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
3393 ? truthvalue_true_node
3394 : truthvalue_false_node;
3395
ce04dcdc 3396 case FUNCTION_DECL:
b99cc6da 3397 expr = build_unary_op (location, ADDR_EXPR, expr, false);
ce04dcdc 3398 /* Fall through. */
3399
b0fc3e72 3400 case ADDR_EXPR:
ce871053 3401 {
ee7d4d6a 3402 tree inner = TREE_OPERAND (expr, 0);
6b68e71a 3403 if (decl_with_nonnull_addr_p (inner))
ce871053 3404 {
6c77f4a4 3405 /* Common Ada programmer's mistake. */
8e70fb09 3406 warning_at (location,
3407 OPT_Waddress,
3408 "the address of %qD will always evaluate as %<true%>",
3409 inner);
ce871053 3410 return truthvalue_true_node;
3411 }
d473d901 3412 break;
ce871053 3413 }
b0fc3e72 3414
2203bd5c 3415 case COMPLEX_EXPR:
e60a6f7b 3416 expr = build_binary_op (EXPR_LOCATION (expr),
8e70fb09 3417 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
95809de4 3418 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
8e70fb09 3419 c_common_truthvalue_conversion (location,
3420 TREE_OPERAND (expr, 0)),
3421 c_common_truthvalue_conversion (location,
3422 TREE_OPERAND (expr, 1)),
623ee358 3423 false);
e60a6f7b 3424 goto ret;
2203bd5c 3425
b0fc3e72 3426 case NEGATE_EXPR:
3427 case ABS_EXPR:
1c67942e 3428 case ABSU_EXPR:
b0fc3e72 3429 case FLOAT_EXPR:
c6418a4e 3430 case EXCESS_PRECISION_EXPR:
d10cfa8d 3431 /* These don't change whether an object is nonzero or zero. */
8e70fb09 3432 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
b0fc3e72 3433
3434 case LROTATE_EXPR:
3435 case RROTATE_EXPR:
d10cfa8d 3436 /* These don't change whether an object is zero or nonzero, but
b0fc3e72 3437 we can't ignore them if their second arg has side-effects. */
3438 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
e60a6f7b 3439 {
3440 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
3441 TREE_OPERAND (expr, 1),
48e1416a 3442 c_common_truthvalue_conversion
e60a6f7b 3443 (location, TREE_OPERAND (expr, 0)));
3444 goto ret;
3445 }
b0fc3e72 3446 else
8e70fb09 3447 return c_common_truthvalue_conversion (location,
3448 TREE_OPERAND (expr, 0));
73be5127 3449
5f64e688 3450 case MULT_EXPR:
3451 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3452 "%<*%> in boolean context, suggest %<&&%> instead");
3453 break;
3454
255beb07 3455 case LSHIFT_EXPR:
3645e924 3456 /* We will only warn on signed shifts here, because the majority of
5c2ba578 3457 false positive warnings happen in code where unsigned arithmetic
3458 was used in anticipation of a possible overflow.
3459 Furthermore, if we see an unsigned type here we know that the
3460 result of the shift is not subject to integer promotion rules. */
3461 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
3462 && !TYPE_UNSIGNED (TREE_TYPE (expr)))
3463 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
5f64e688 3464 "%<<<%> in boolean context, did you mean %<<%> ?");
255beb07 3465 break;
3466
b0fc3e72 3467 case COND_EXPR:
a5fe0b37 3468 if (warn_int_in_bool_context
3469 && !from_macro_definition_at (EXPR_LOCATION (expr)))
bed03df1 3470 {
3471 tree val1 = fold_for_warn (TREE_OPERAND (expr, 1));
3472 tree val2 = fold_for_warn (TREE_OPERAND (expr, 2));
3473 if (TREE_CODE (val1) == INTEGER_CST
3474 && TREE_CODE (val2) == INTEGER_CST
3475 && !integer_zerop (val1)
3476 && !integer_zerop (val2)
3477 && (!integer_onep (val1)
3478 || !integer_onep (val2)))
3479 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
a5fe0b37 3480 "?: using integer constants in boolean context, "
3481 "the expression will always evaluate to %<true%>");
2b6903a5 3482 else if ((TREE_CODE (val1) == INTEGER_CST
3483 && !integer_zerop (val1)
3484 && !integer_onep (val1))
3485 || (TREE_CODE (val2) == INTEGER_CST
3486 && !integer_zerop (val2)
3487 && !integer_onep (val2)))
3488 warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3489 "?: using integer constants in boolean context");
bed03df1 3490 }
b0fc3e72 3491 /* Distribute the conversion into the arms of a COND_EXPR. */
a75b1c71 3492 if (c_dialect_cxx ())
7a7ca07c 3493 /* Avoid premature folding. */
3494 break;
a75b1c71 3495 else
e60a6f7b 3496 {
255beb07 3497 int w = warn_int_in_bool_context;
3498 warn_int_in_bool_context = 0;
e60a6f7b 3499 /* Folding will happen later for C. */
3500 expr = build3 (COND_EXPR, truthvalue_type_node,
3501 TREE_OPERAND (expr, 0),
3502 c_common_truthvalue_conversion (location,
3503 TREE_OPERAND (expr, 1)),
3504 c_common_truthvalue_conversion (location,
3505 TREE_OPERAND (expr, 2)));
255beb07 3506 warn_int_in_bool_context = w;
e60a6f7b 3507 goto ret;
3508 }
b0fc3e72 3509
72dd6141 3510 CASE_CONVERT:
f8913d47 3511 {
3512 tree totype = TREE_TYPE (expr);
3513 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
3514
db3d1ffc 3515 if (POINTER_TYPE_P (totype)
0c0f63cb 3516 && !c_inhibit_evaluation_warnings
db3d1ffc 3517 && TREE_CODE (fromtype) == REFERENCE_TYPE)
3518 {
3519 tree inner = expr;
3520 STRIP_NOPS (inner);
3521
3522 if (DECL_P (inner))
3523 warning_at (location,
3524 OPT_Waddress,
3525 "the compiler can assume that the address of "
3526 "%qD will always evaluate to %<true%>",
3527 inner);
3528 }
3529
f8913d47 3530 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3531 since that affects how `default_conversion' will behave. */
3532 if (TREE_CODE (totype) == REFERENCE_TYPE
3533 || TREE_CODE (fromtype) == REFERENCE_TYPE)
3534 break;
3535 /* Don't strip a conversion from C++0x scoped enum, since they
3536 don't implicitly convert to other types. */
3537 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
3538 && ENUM_IS_SCOPED (fromtype))
3539 break;
3540 /* If this isn't narrowing the argument, we can ignore it. */
3541 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
3542 return c_common_truthvalue_conversion (location,
3543 TREE_OPERAND (expr, 0));
3544 }
b0fc3e72 3545 break;
3546
16837b18 3547 case MODIFY_EXPR:
60a0513e 3548 if (!TREE_NO_WARNING (expr)
94a62c5a 3549 && warn_parentheses
3550 && warning_at (location, OPT_Wparentheses,
3551 "suggest parentheses around assignment used as "
3552 "truth value"))
3553 TREE_NO_WARNING (expr) = 1;
16837b18 3554 break;
73be5127 3555
d582d140 3556 case CONST_DECL:
3557 {
3558 tree folded_expr = fold_for_warn (expr);
3559 if (folded_expr != expr)
3560 return c_common_truthvalue_conversion (location, folded_expr);
3561 }
3562 break;
3563
31f820d2 3564 default:
3565 break;
b0fc3e72 3566 }
3567
2ba726d2 3568 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
a0748b7d 3569 {
d0869ea4 3570 tree t = save_expr (expr);
e60a6f7b 3571 expr = (build_binary_op
8e70fb09 3572 (EXPR_LOCATION (expr),
3573 (TREE_SIDE_EFFECTS (expr)
a0748b7d 3574 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
b6889cb0 3575 c_common_truthvalue_conversion
3576 (location,
b99cc6da 3577 build_unary_op (location, REALPART_EXPR, t, false)),
b6889cb0 3578 c_common_truthvalue_conversion
3579 (location,
b99cc6da 3580 build_unary_op (location, IMAGPART_EXPR, t, false)),
623ee358 3581 false));
e60a6f7b 3582 goto ret;
a0748b7d 3583 }
2ba726d2 3584
9421ebb9 3585 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
3586 {
3587 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
3588 FCONST0 (TYPE_MODE
3589 (TREE_TYPE (expr))));
623ee358 3590 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, true);
9421ebb9 3591 }
e60a6f7b 3592 else
623ee358 3593 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, true);
9421ebb9 3594
e60a6f7b 3595 ret:
3596 protected_set_expr_location (expr, location);
3597 return expr;
b0fc3e72 3598}
3599\f
3237155d 3600static void def_builtin_1 (enum built_in_function fncode,
3601 const char *name,
3602 enum built_in_class fnclass,
3603 tree fntype, tree libtype,
3604 bool both_p, bool fallback_p, bool nonansi_p,
3605 tree fnattrs, bool implicit_p);
0d4238dc 3606
a5b1863e 3607
3608/* Apply the TYPE_QUALS to the new DECL. */
3609
3610void
1cae46be 3611c_apply_type_quals_to_decl (int type_quals, tree decl)
a5b1863e 3612{
adfb367f 3613 tree type = TREE_TYPE (decl);
b27ac6b5 3614
e4eabbe4 3615 if (type == error_mark_node)
3616 return;
adfb367f 3617
98a33d9f 3618 if ((type_quals & TYPE_QUAL_CONST)
3619 || (type && TREE_CODE (type) == REFERENCE_TYPE))
3620 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
3621 constructor can produce constant init, so rely on cp_finish_decl to
3622 clear TREE_READONLY if the variable has non-constant init. */
a5b1863e 3623 TREE_READONLY (decl) = 1;
3624 if (type_quals & TYPE_QUAL_VOLATILE)
3625 {
3626 TREE_SIDE_EFFECTS (decl) = 1;
3627 TREE_THIS_VOLATILE (decl) = 1;
3628 }
d91a20bc 3629 if (type_quals & TYPE_QUAL_RESTRICT)
a5b1863e 3630 {
adfb367f 3631 while (type && TREE_CODE (type) == ARRAY_TYPE)
3632 /* Allow 'restrict' on arrays of pointers.
3633 FIXME currently we just ignore it. */
3634 type = TREE_TYPE (type);
3635 if (!type
3636 || !POINTER_TYPE_P (type)
3637 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
b0b1af64 3638 error ("invalid use of %<restrict%>");
a5b1863e 3639 }
3640}
3641
b5ba9f3a 3642/* Return the typed-based alias set for T, which may be an expression
f7c44134 3643 or a type. Return -1 if we don't do anything special. */
ab2c1de8 3644
32c2fdea 3645alias_set_type
1cae46be 3646c_common_get_alias_set (tree t)
b5ba9f3a 3647{
e58c17e7 3648 /* For VLAs, use the alias set of the element type rather than the
3649 default of alias set 0 for types compared structurally. */
3650 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
3651 {
3652 if (TREE_CODE (t) == ARRAY_TYPE)
3653 return get_alias_set (TREE_TYPE (t));
3654 return -1;
3655 }
3656
9fcc3e54 3657 /* That's all the expressions we handle specially. */
84166705 3658 if (!TYPE_P (t))
9fcc3e54 3659 return -1;
3660
82362779 3661 /* Unlike char, char8_t doesn't alias. */
3662 if (flag_char8_t && t == char8_type_node)
3663 return -1;
3664
d716ce75 3665 /* The C standard guarantees that any object may be accessed via an
82362779 3666 lvalue that has narrow character type (except char8_t). */
9fcc3e54 3667 if (t == char_type_node
3668 || t == signed_char_type_node
3669 || t == unsigned_char_type_node)
f7c44134 3670 return 0;
a5b1863e 3671
1607663f 3672 /* The C standard specifically allows aliasing between signed and
3673 unsigned variants of the same type. We treat the signed
3674 variant as canonical. */
78a8ed03 3675 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
a8868e19 3676 {
4070745f 3677 tree t1 = c_common_signed_type (t);
1607663f 3678
a8868e19 3679 /* t1 == t can happen for boolean nodes which are always unsigned. */
3680 if (t1 != t)
3681 return get_alias_set (t1);
3682 }
1e2513d9 3683
f7c44134 3684 return -1;
b5ba9f3a 3685}
902b4e01 3686\f
e60a6f7b 3687/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
a179a7dc 3688 the IS_SIZEOF parameter indicates which operator is being applied.
e60a6f7b 3689 The COMPLAIN flag controls whether we should diagnose possibly
3690 ill-formed constructs or not. LOC is the location of the SIZEOF or
a179a7dc 3691 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
3692 a type in any context should be returned, rather than the normal
3693 alignment for that type. */
908c697e 3694
902b4e01 3695tree
e60a6f7b 3696c_sizeof_or_alignof_type (location_t loc,
a179a7dc 3697 tree type, bool is_sizeof, bool min_alignof,
3698 int complain)
902b4e01 3699{
d4c4d95c 3700 const char *op_name;
3701 tree value = NULL;
3702 enum tree_code type_code = TREE_CODE (type);
1cae46be 3703
908c697e 3704 op_name = is_sizeof ? "sizeof" : "__alignof__";
1cae46be 3705
d4c4d95c 3706 if (type_code == FUNCTION_TYPE)
902b4e01 3707 {
908c697e 3708 if (is_sizeof)
d4c4d95c 3709 {
9205a6cc 3710 if (complain && warn_pointer_arith)
3711 pedwarn (loc, OPT_Wpointer_arith,
8864917d 3712 "invalid application of %<sizeof%> to a function type");
ebd21de4 3713 else if (!complain)
3714 return error_mark_node;
d4c4d95c 3715 value = size_one_node;
3716 }
3717 else
83e25171 3718 {
3719 if (complain)
3720 {
3721 if (c_dialect_cxx ())
29438999 3722 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
83e25171 3723 "%<alignof%> applied to a function type");
3724 else
29438999 3725 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
83e25171 3726 "%<_Alignof%> applied to a function type");
3727 }
3728 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
3729 }
d4c4d95c 3730 }
3731 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
3732 {
1cae46be 3733 if (type_code == VOID_TYPE
9205a6cc 3734 && complain && warn_pointer_arith)
3735 pedwarn (loc, OPT_Wpointer_arith,
8864917d 3736 "invalid application of %qs to a void type", op_name);
ebd21de4 3737 else if (!complain)
3738 return error_mark_node;
d4c4d95c 3739 value = size_one_node;
902b4e01 3740 }
3df19e1b 3741 else if (!COMPLETE_TYPE_P (type)
3742 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
902b4e01 3743 {
d3a4d008 3744 if (complain)
3df19e1b 3745 error_at (loc, "invalid application of %qs to incomplete type %qT",
e60a6f7b 3746 op_name, type);
9c719c74 3747 return error_mark_node;
902b4e01 3748 }
3df19e1b 3749 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
3750 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
3751 {
3752 if (complain)
3753 error_at (loc, "invalid application of %qs to array type %qT of "
3754 "incomplete element type", op_name, type);
3755 return error_mark_node;
3756 }
902b4e01 3757 else
d4c4d95c 3758 {
908c697e 3759 if (is_sizeof)
d4c4d95c 3760 /* Convert in case a char is more than one unit. */
389dd41b 3761 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
3762 size_int (TYPE_PRECISION (char_type_node)
3763 / BITS_PER_UNIT));
a179a7dc 3764 else if (min_alignof)
c2c4ae8d 3765 value = size_int (min_align_of_type (type));
d4c4d95c 3766 else
d37625c0 3767 value = size_int (TYPE_ALIGN_UNIT (type));
d4c4d95c 3768 }
902b4e01 3769
5a1fe2db 3770 /* VALUE will have the middle-end integer type sizetype.
3771 However, we should really return a value of type `size_t',
3772 which is just a typedef for an ordinary integer type. */
389dd41b 3773 value = fold_convert_loc (loc, size_type_node, value);
1cae46be 3774
d4c4d95c 3775 return value;
902b4e01 3776}
3777
3778/* Implement the __alignof keyword: Return the minimum required
097b5c8b 3779 alignment of EXPR, measured in bytes. For VAR_DECLs,
3780 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
e60a6f7b 3781 from an "aligned" __attribute__ specification). LOC is the
3782 location of the ALIGNOF operator. */
72040e7e 3783
902b4e01 3784tree
e60a6f7b 3785c_alignof_expr (location_t loc, tree expr)
902b4e01 3786{
3787 tree t;
3788
097b5c8b 3789 if (VAR_OR_FUNCTION_DECL_P (expr))
d37625c0 3790 t = size_int (DECL_ALIGN_UNIT (expr));
1cae46be 3791
902b4e01 3792 else if (TREE_CODE (expr) == COMPONENT_REF
3793 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
3794 {
e60a6f7b 3795 error_at (loc, "%<__alignof%> applied to a bit-field");
902b4e01 3796 t = size_one_node;
3797 }
3798 else if (TREE_CODE (expr) == COMPONENT_REF
7cc7e163 3799 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
d37625c0 3800 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
1cae46be 3801
aa3e402e 3802 else if (INDIRECT_REF_P (expr))
902b4e01 3803 {
3804 tree t = TREE_OPERAND (expr, 0);
3805 tree best = t;
3806 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
1cae46be 3807
72dd6141 3808 while (CONVERT_EXPR_P (t)
7cc7e163 3809 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
902b4e01 3810 {
3811 int thisalign;
3812
3813 t = TREE_OPERAND (t, 0);
3814 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
3815 if (thisalign > bestalign)
3816 best = t, bestalign = thisalign;
3817 }
e60a6f7b 3818 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
902b4e01 3819 }
3820 else
e60a6f7b 3821 return c_alignof (loc, TREE_TYPE (expr));
902b4e01 3822
389dd41b 3823 return fold_convert_loc (loc, size_type_node, t);
902b4e01 3824}
3825\f
8fe4a266 3826/* Handle C and C++ default attributes. */
3827
3828enum built_in_attribute
3829{
3830#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
3831#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
c8010b80 3832#define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
8fe4a266 3833#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
3834#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
8fe4a266 3835#include "builtin-attrs.def"
3836#undef DEF_ATTR_NULL_TREE
3837#undef DEF_ATTR_INT
c8010b80 3838#undef DEF_ATTR_STRING
8fe4a266 3839#undef DEF_ATTR_IDENT
3840#undef DEF_ATTR_TREE_LIST
8fe4a266 3841 ATTR_LAST
3842};
3843
3844static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
3845
1cae46be 3846static void c_init_attributes (void);
8fe4a266 3847
27213ba3 3848enum c_builtin_type
72040e7e 3849{
d2d4bdde 3850#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
3851#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
3852#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
3853#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
3854#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
3855#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
0a39fd54 3856#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
3c77ca67 3857#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3858 ARG6) NAME,
3859#define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3860 ARG6, ARG7) NAME,
3861#define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3862 ARG6, ARG7, ARG8) NAME,
43895be5 3863#define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3864 ARG6, ARG7, ARG8, ARG9) NAME,
3865#define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3866 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
3867#define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3868 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
d2d4bdde 3869#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
3870#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
3871#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
03901330 3872#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
0a39fd54 3873#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
3c77ca67 3874#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
6349b8cc 3875 NAME,
e561d5e1 3876#define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3877 ARG6) NAME,
6349b8cc 3878#define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3879 ARG6, ARG7) NAME,
d2d4bdde 3880#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
3881#include "builtin-types.def"
3882#undef DEF_PRIMITIVE_TYPE
3883#undef DEF_FUNCTION_TYPE_0
3884#undef DEF_FUNCTION_TYPE_1
3885#undef DEF_FUNCTION_TYPE_2
3886#undef DEF_FUNCTION_TYPE_3
3887#undef DEF_FUNCTION_TYPE_4
0a39fd54 3888#undef DEF_FUNCTION_TYPE_5
3889#undef DEF_FUNCTION_TYPE_6
27213ba3 3890#undef DEF_FUNCTION_TYPE_7
bc7bff74 3891#undef DEF_FUNCTION_TYPE_8
43895be5 3892#undef DEF_FUNCTION_TYPE_9
3893#undef DEF_FUNCTION_TYPE_10
3894#undef DEF_FUNCTION_TYPE_11
d2d4bdde 3895#undef DEF_FUNCTION_TYPE_VAR_0
3896#undef DEF_FUNCTION_TYPE_VAR_1
3897#undef DEF_FUNCTION_TYPE_VAR_2
03901330 3898#undef DEF_FUNCTION_TYPE_VAR_3
0a39fd54 3899#undef DEF_FUNCTION_TYPE_VAR_4
3900#undef DEF_FUNCTION_TYPE_VAR_5
e561d5e1 3901#undef DEF_FUNCTION_TYPE_VAR_6
6349b8cc 3902#undef DEF_FUNCTION_TYPE_VAR_7
d2d4bdde 3903#undef DEF_POINTER_TYPE
27213ba3 3904 BT_LAST
3905};
3906
3907typedef enum c_builtin_type builtin_type;
d2d4bdde 3908
27213ba3 3909/* A temporary array for c_common_nodes_and_builtins. Used in
3910 communication with def_fn_type. */
3911static tree builtin_types[(int) BT_LAST + 1];
d2d4bdde 3912
27213ba3 3913/* A helper function for c_common_nodes_and_builtins. Build function type
3914 for DEF with return type RET and N arguments. If VAR is true, then the
3915 function should be variadic after those N arguments.
3916
3917 Takes special care not to ICE if any of the types involved are
3918 error_mark_node, which indicates that said type is not in fact available
3919 (see builtin_type_for_size). In which case the function type as a whole
3920 should be error_mark_node. */
3921
3922static void
3923def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
3924{
3a939d12 3925 tree t;
3926 tree *args = XALLOCAVEC (tree, n);
27213ba3 3927 va_list list;
3928 int i;
3929
3930 va_start (list, n);
3931 for (i = 0; i < n; ++i)
3932 {
7d339f93 3933 builtin_type a = (builtin_type) va_arg (list, int);
27213ba3 3934 t = builtin_types[a];
3935 if (t == error_mark_node)
3936 goto egress;
3a939d12 3937 args[i] = t;
27213ba3 3938 }
27213ba3 3939
27213ba3 3940 t = builtin_types[ret];
3941 if (t == error_mark_node)
3942 goto egress;
3a939d12 3943 if (var)
3944 t = build_varargs_function_type_array (t, n, args);
3945 else
3946 t = build_function_type_array (t, n, args);
27213ba3 3947
3948 egress:
3949 builtin_types[def] = t;
451c8e2f 3950 va_end (list);
27213ba3 3951}
3952
dce22712 3953/* Build builtin functions common to both C and C++ language
3954 frontends. */
3955
3956static void
3957c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
3958{
3959#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
3960 builtin_types[ENUM] = VALUE;
3961#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
3962 def_fn_type (ENUM, RETURN, 0, 0);
3963#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
3964 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
3965#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
3966 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
3967#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
3968 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
3969#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
3970 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
3971#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
3972 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3973#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3974 ARG6) \
3975 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
3976#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3977 ARG6, ARG7) \
3978 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
bc7bff74 3979#define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3980 ARG6, ARG7, ARG8) \
3981 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3982 ARG7, ARG8);
43895be5 3983#define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3984 ARG6, ARG7, ARG8, ARG9) \
3985 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3986 ARG7, ARG8, ARG9);
3987#define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3988 ARG6, ARG7, ARG8, ARG9, ARG10) \
3989 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3990 ARG7, ARG8, ARG9, ARG10);
3991#define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
3992 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
3993 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
3994 ARG7, ARG8, ARG9, ARG10, ARG11);
dce22712 3995#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
3996 def_fn_type (ENUM, RETURN, 1, 0);
3997#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
3998 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
3999#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
4000 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
4001#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4002 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
4003#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4004 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
4005#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4006 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
e561d5e1 4007#define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4008 ARG6) \
4009 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
6349b8cc 4010#define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4011 ARG6, ARG7) \
4012 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
dce22712 4013#define DEF_POINTER_TYPE(ENUM, TYPE) \
4014 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
4015
4016#include "builtin-types.def"
4017
4018#undef DEF_PRIMITIVE_TYPE
3c77ca67 4019#undef DEF_FUNCTION_TYPE_0
dce22712 4020#undef DEF_FUNCTION_TYPE_1
4021#undef DEF_FUNCTION_TYPE_2
4022#undef DEF_FUNCTION_TYPE_3
4023#undef DEF_FUNCTION_TYPE_4
4024#undef DEF_FUNCTION_TYPE_5
4025#undef DEF_FUNCTION_TYPE_6
3c77ca67 4026#undef DEF_FUNCTION_TYPE_7
4027#undef DEF_FUNCTION_TYPE_8
43895be5 4028#undef DEF_FUNCTION_TYPE_9
4029#undef DEF_FUNCTION_TYPE_10
4030#undef DEF_FUNCTION_TYPE_11
dce22712 4031#undef DEF_FUNCTION_TYPE_VAR_0
4032#undef DEF_FUNCTION_TYPE_VAR_1
4033#undef DEF_FUNCTION_TYPE_VAR_2
4034#undef DEF_FUNCTION_TYPE_VAR_3
4035#undef DEF_FUNCTION_TYPE_VAR_4
4036#undef DEF_FUNCTION_TYPE_VAR_5
e561d5e1 4037#undef DEF_FUNCTION_TYPE_VAR_6
6349b8cc 4038#undef DEF_FUNCTION_TYPE_VAR_7
dce22712 4039#undef DEF_POINTER_TYPE
4040 builtin_types[(int) BT_LAST] = NULL_TREE;
4041
4042 c_init_attributes ();
4043
4044#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
4045 NONANSI_P, ATTRS, IMPLICIT, COND) \
4046 if (NAME && COND) \
4047 def_builtin_1 (ENUM, NAME, CLASS, \
4048 builtin_types[(int) TYPE], \
4049 builtin_types[(int) LIBTYPE], \
4050 BOTH_P, FALLBACK_P, NONANSI_P, \
4051 built_in_attributes[(int) ATTRS], IMPLICIT);
4052#include "builtins.def"
dce22712 4053
87eb1c28 4054 targetm.init_builtins ();
4055
471eff36 4056 build_common_builtin_nodes ();
dce22712 4057}
4058
9e6687c8 4059/* Like get_identifier, but avoid warnings about null arguments when
4060 the argument may be NULL for targets where GCC lacks stdint.h type
4061 information. */
4062
4063static inline tree
4064c_get_ident (const char *id)
4065{
4066 return get_identifier (id);
4067}
4068
27213ba3 4069/* Build tree nodes and builtin functions common to both C and C++ language
4070 frontends. */
4071
4072void
4073c_common_nodes_and_builtins (void)
4074{
82362779 4075 int char8_type_size;
924bbf02 4076 int char16_type_size;
4077 int char32_type_size;
174fcc61 4078 int wchar_type_size;
4079 tree array_domain_type;
2d47cc32 4080 tree va_list_ref_type_node;
8a15c04a 4081 tree va_list_arg_type_node;
9f75f026 4082 int i;
a66c9326 4083
600695e0 4084 build_common_tree_nodes (flag_signed_char);
e593356b 4085
174fcc61 4086 /* Define `int' and `char' first so that dbx will output them first. */
d946ea19 4087 record_builtin_type (RID_INT, NULL, integer_type_node);
174fcc61 4088 record_builtin_type (RID_CHAR, "char", char_type_node);
4089
4090 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
4091 "unsigned long", "long long unsigned" and "unsigned short" were in C++
4092 but not C. Are the conditionals here needed? */
c0f19401 4093 if (c_dialect_cxx ())
d946ea19 4094 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
174fcc61 4095 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
4096 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
4097 record_builtin_type (RID_MAX, "long unsigned int",
4098 long_unsigned_type_node);
9f75f026 4099
4100 for (i = 0; i < NUM_INT_N_ENTS; i ++)
6388cfe2 4101 {
9f75f026 4102 char name[25];
4103
4104 sprintf (name, "__int%d", int_n_data[i].bitsize);
76738f56 4105 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
9f75f026 4106 int_n_trees[i].signed_type);
4107 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
76738f56 4108 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
6388cfe2 4109 }
9f75f026 4110
c0f19401 4111 if (c_dialect_cxx ())
174fcc61 4112 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
4113 record_builtin_type (RID_MAX, "long long int",
4114 long_long_integer_type_node);
4115 record_builtin_type (RID_MAX, "long long unsigned int",
4116 long_long_unsigned_type_node);
c0f19401 4117 if (c_dialect_cxx ())
174fcc61 4118 record_builtin_type (RID_MAX, "long long unsigned",
4119 long_long_unsigned_type_node);
4120 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
4121 record_builtin_type (RID_MAX, "short unsigned int",
4122 short_unsigned_type_node);
c0f19401 4123 if (c_dialect_cxx ())
174fcc61 4124 record_builtin_type (RID_MAX, "unsigned short",
4125 short_unsigned_type_node);
4126
4127 /* Define both `signed char' and `unsigned char'. */
4128 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
4129 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
4130
771d21fa 4131 /* These are types that c_common_type_for_size and
4132 c_common_type_for_mode use. */
e60a6f7b 4133 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4134 TYPE_DECL, NULL_TREE,
dc24ddbd 4135 intQI_type_node));
e60a6f7b 4136 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4137 TYPE_DECL, NULL_TREE,
dc24ddbd 4138 intHI_type_node));
e60a6f7b 4139 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4140 TYPE_DECL, NULL_TREE,
dc24ddbd 4141 intSI_type_node));
e60a6f7b 4142 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4143 TYPE_DECL, NULL_TREE,
dc24ddbd 4144 intDI_type_node));
174fcc61 4145#if HOST_BITS_PER_WIDE_INT >= 64
9f75f026 4146 /* Note that this is different than the __int128 type that's part of
4147 the generic __intN support. */
f1515a39 4148 if (targetm.scalar_mode_supported_p (TImode))
e60a6f7b 4149 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4150 TYPE_DECL,
f1515a39 4151 get_identifier ("__int128_t"),
4152 intTI_type_node));
174fcc61 4153#endif
e60a6f7b 4154 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4155 TYPE_DECL, NULL_TREE,
dc24ddbd 4156 unsigned_intQI_type_node));
e60a6f7b 4157 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4158 TYPE_DECL, NULL_TREE,
dc24ddbd 4159 unsigned_intHI_type_node));
e60a6f7b 4160 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4161 TYPE_DECL, NULL_TREE,
dc24ddbd 4162 unsigned_intSI_type_node));
e60a6f7b 4163 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4164 TYPE_DECL, NULL_TREE,
dc24ddbd 4165 unsigned_intDI_type_node));
174fcc61 4166#if HOST_BITS_PER_WIDE_INT >= 64
f1515a39 4167 if (targetm.scalar_mode_supported_p (TImode))
e60a6f7b 4168 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4169 TYPE_DECL,
f1515a39 4170 get_identifier ("__uint128_t"),
4171 unsigned_intTI_type_node));
174fcc61 4172#endif
4173
4174 /* Create the widest literal types. */
7ec2cbc9 4175 if (targetm.scalar_mode_supported_p (TImode))
4176 {
4177 widest_integer_literal_type_node = intTI_type_node;
4178 widest_unsigned_literal_type_node = unsigned_intTI_type_node;
4179 }
4180 else
4181 {
4182 widest_integer_literal_type_node = intDI_type_node;
4183 widest_unsigned_literal_type_node = unsigned_intDI_type_node;
4184 }
174fcc61 4185
654ef926 4186 signed_size_type_node = c_common_signed_type (size_type_node);
174fcc61 4187
73673831 4188 pid_type_node =
4189 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
4190
d946ea19 4191 record_builtin_type (RID_FLOAT, NULL, float_type_node);
4192 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
174fcc61 4193 record_builtin_type (RID_MAX, "long double", long_double_type_node);
4194
82c85aba 4195 if (!c_dialect_cxx ())
4196 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4197 if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4198 record_builtin_type ((enum rid) (RID_FLOATN_NX_FIRST + i), NULL,
4199 FLOATN_NX_TYPE_NODE (i));
4200
c4503c0a 4201 /* Only supported decimal floating point extension if the target
4202 actually supports underlying modes. */
48e1416a 4203 if (targetm.scalar_mode_supported_p (SDmode)
c4503c0a 4204 && targetm.scalar_mode_supported_p (DDmode)
4205 && targetm.scalar_mode_supported_p (TDmode))
4206 {
4207 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
4208 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
4209 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
4210 }
4211
9421ebb9 4212 if (targetm.fixed_point_supported_p ())
4213 {
4214 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
4215 record_builtin_type (RID_FRACT, NULL, fract_type_node);
4216 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
4217 record_builtin_type (RID_MAX, "long long _Fract",
4218 long_long_fract_type_node);
4219 record_builtin_type (RID_MAX, "unsigned short _Fract",
4220 unsigned_short_fract_type_node);
4221 record_builtin_type (RID_MAX, "unsigned _Fract",
4222 unsigned_fract_type_node);
4223 record_builtin_type (RID_MAX, "unsigned long _Fract",
4224 unsigned_long_fract_type_node);
4225 record_builtin_type (RID_MAX, "unsigned long long _Fract",
4226 unsigned_long_long_fract_type_node);
4227 record_builtin_type (RID_MAX, "_Sat short _Fract",
4228 sat_short_fract_type_node);
4229 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
4230 record_builtin_type (RID_MAX, "_Sat long _Fract",
4231 sat_long_fract_type_node);
4232 record_builtin_type (RID_MAX, "_Sat long long _Fract",
4233 sat_long_long_fract_type_node);
4234 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
4235 sat_unsigned_short_fract_type_node);
4236 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
4237 sat_unsigned_fract_type_node);
4238 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
4239 sat_unsigned_long_fract_type_node);
4240 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
4241 sat_unsigned_long_long_fract_type_node);
4242 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
4243 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
4244 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
4245 record_builtin_type (RID_MAX, "long long _Accum",
4246 long_long_accum_type_node);
4247 record_builtin_type (RID_MAX, "unsigned short _Accum",
4248 unsigned_short_accum_type_node);
4249 record_builtin_type (RID_MAX, "unsigned _Accum",
4250 unsigned_accum_type_node);
4251 record_builtin_type (RID_MAX, "unsigned long _Accum",
4252 unsigned_long_accum_type_node);
4253 record_builtin_type (RID_MAX, "unsigned long long _Accum",
4254 unsigned_long_long_accum_type_node);
4255 record_builtin_type (RID_MAX, "_Sat short _Accum",
4256 sat_short_accum_type_node);
4257 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
4258 record_builtin_type (RID_MAX, "_Sat long _Accum",
4259 sat_long_accum_type_node);
4260 record_builtin_type (RID_MAX, "_Sat long long _Accum",
4261 sat_long_long_accum_type_node);
4262 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
4263 sat_unsigned_short_accum_type_node);
4264 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
4265 sat_unsigned_accum_type_node);
4266 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
4267 sat_unsigned_long_accum_type_node);
4268 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
4269 sat_unsigned_long_long_accum_type_node);
4270
4271 }
4272
e60a6f7b 4273 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4274 TYPE_DECL,
dc24ddbd 4275 get_identifier ("complex int"),
4276 complex_integer_type_node));
e60a6f7b 4277 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4278 TYPE_DECL,
dc24ddbd 4279 get_identifier ("complex float"),
4280 complex_float_type_node));
e60a6f7b 4281 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4282 TYPE_DECL,
dc24ddbd 4283 get_identifier ("complex double"),
4284 complex_double_type_node));
4285 lang_hooks.decls.pushdecl
e60a6f7b 4286 (build_decl (UNKNOWN_LOCATION,
4287 TYPE_DECL, get_identifier ("complex long double"),
20325f61 4288 complex_long_double_type_node));
174fcc61 4289
82c85aba 4290 if (!c_dialect_cxx ())
4291 for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4292 if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4293 {
4294 char buf[30];
4295 sprintf (buf, "complex _Float%d%s", floatn_nx_types[i].n,
4296 floatn_nx_types[i].extended ? "x" : "");
4297 lang_hooks.decls.pushdecl
4298 (build_decl (UNKNOWN_LOCATION,
4299 TYPE_DECL,
4300 get_identifier (buf),
4301 COMPLEX_FLOATN_NX_TYPE_NODE (i)));
4302 }
4303
a456bf31 4304 /* Make fileptr_type_node a distinct void * type until
4305 FILE type is defined. Likewise for const struct tm*. */
4306 for (unsigned i = 0;
4307 i < sizeof (builtin_structptr_types) / sizeof (builtin_structptr_type);
4308 ++i)
4309 builtin_structptr_types[i].node
4310 = build_variant_type_copy (builtin_structptr_types[i].base);
e256d445 4311
d946ea19 4312 record_builtin_type (RID_VOID, NULL, void_type_node);
174fcc61 4313
6753bca0 4314 /* Set the TYPE_NAME for any variants that were built before
4315 record_builtin_type gave names to the built-in types. */
4316 {
4317 tree void_name = TYPE_NAME (void_type_node);
4318 TYPE_NAME (void_type_node) = NULL_TREE;
4319 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
4320 = void_name;
4321 TYPE_NAME (void_type_node) = void_name;
4322 }
4323
174fcc61 4324 void_list_node = build_void_list_node ();
4325
4326 /* Make a type to be the domain of a few array types
4327 whose domains don't really matter.
4328 200 is small enough that it always fits in size_t
4329 and large enough that it can hold most function names for the
4330 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4331 array_domain_type = build_index_type (size_int (200));
4332
4333 /* Make a type for arrays of characters.
4334 With luck nothing will ever really depend on the length of this
4335 array type. */
4336 char_array_type_node
4337 = build_array_type (char_type_node, array_domain_type);
4338
d2d4bdde 4339 string_type_node = build_pointer_type (char_type_node);
4340 const_string_type_node
4341 = build_pointer_type (build_qualified_type
4342 (char_type_node, TYPE_QUAL_CONST));
4343
174fcc61 4344 /* This is special for C++ so functions can be overloaded. */
18ef7ac2 4345 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
174fcc61 4346 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
4347 wchar_type_size = TYPE_PRECISION (wchar_type_node);
f3449a3c 4348 underlying_wchar_type_node = wchar_type_node;
c0f19401 4349 if (c_dialect_cxx ())
174fcc61 4350 {
78a8ed03 4351 if (TYPE_UNSIGNED (wchar_type_node))
174fcc61 4352 wchar_type_node = make_unsigned_type (wchar_type_size);
4353 else
4354 wchar_type_node = make_signed_type (wchar_type_size);
4355 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
4356 }
174fcc61 4357
4358 /* This is for wide string constants. */
4359 wchar_array_type_node
4360 = build_array_type (wchar_type_node, array_domain_type);
4361
82362779 4362 /* Define 'char8_t'. */
4363 char8_type_node = get_identifier (CHAR8_TYPE);
4364 char8_type_node = TREE_TYPE (identifier_global_value (char8_type_node));
4365 char8_type_size = TYPE_PRECISION (char8_type_node);
4366 if (c_dialect_cxx ())
4367 {
4368 char8_type_node = make_unsigned_type (char8_type_size);
4369
4370 if (flag_char8_t)
4371 record_builtin_type (RID_CHAR8, "char8_t", char8_type_node);
4372 }
4373
4374 /* This is for UTF-8 string constants. */
4375 char8_array_type_node
4376 = build_array_type (char8_type_node, array_domain_type);
4377
924bbf02 4378 /* Define 'char16_t'. */
4379 char16_type_node = get_identifier (CHAR16_TYPE);
4380 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
4381 char16_type_size = TYPE_PRECISION (char16_type_node);
4382 if (c_dialect_cxx ())
4383 {
4384 char16_type_node = make_unsigned_type (char16_type_size);
4385
60777f69 4386 if (cxx_dialect >= cxx11)
924bbf02 4387 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
4388 }
4389
4390 /* This is for UTF-16 string constants. */
4391 char16_array_type_node
4392 = build_array_type (char16_type_node, array_domain_type);
4393
4394 /* Define 'char32_t'. */
4395 char32_type_node = get_identifier (CHAR32_TYPE);
4396 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
4397 char32_type_size = TYPE_PRECISION (char32_type_node);
4398 if (c_dialect_cxx ())
4399 {
4400 char32_type_node = make_unsigned_type (char32_type_size);
4401
60777f69 4402 if (cxx_dialect >= cxx11)
924bbf02 4403 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
4404 }
4405
4406 /* This is for UTF-32 string constants. */
4407 char32_array_type_node
4408 = build_array_type (char32_type_node, array_domain_type);
4409
6bf5ed8d 4410 wint_type_node =
4411 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
4412
4413 intmax_type_node =
4414 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
4415 uintmax_type_node =
4416 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
4417
f3449a3c 4418 if (SIG_ATOMIC_TYPE)
4419 sig_atomic_type_node =
9e6687c8 4420 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
f3449a3c 4421 if (INT8_TYPE)
4422 int8_type_node =
9e6687c8 4423 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
f3449a3c 4424 if (INT16_TYPE)
4425 int16_type_node =
9e6687c8 4426 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
f3449a3c 4427 if (INT32_TYPE)
4428 int32_type_node =
9e6687c8 4429 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
f3449a3c 4430 if (INT64_TYPE)
4431 int64_type_node =
9e6687c8 4432 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
f3449a3c 4433 if (UINT8_TYPE)
4434 uint8_type_node =
9e6687c8 4435 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
f3449a3c 4436 if (UINT16_TYPE)
d1081017 4437 c_uint16_type_node = uint16_type_node =
9e6687c8 4438 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
f3449a3c 4439 if (UINT32_TYPE)
d1081017 4440 c_uint32_type_node = uint32_type_node =
9e6687c8 4441 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
f3449a3c 4442 if (UINT64_TYPE)
d1081017 4443 c_uint64_type_node = uint64_type_node =
9e6687c8 4444 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
f3449a3c 4445 if (INT_LEAST8_TYPE)
4446 int_least8_type_node =
9e6687c8 4447 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
f3449a3c 4448 if (INT_LEAST16_TYPE)
4449 int_least16_type_node =
9e6687c8 4450 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
f3449a3c 4451 if (INT_LEAST32_TYPE)
4452 int_least32_type_node =
9e6687c8 4453 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
f3449a3c 4454 if (INT_LEAST64_TYPE)
4455 int_least64_type_node =
9e6687c8 4456 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
f3449a3c 4457 if (UINT_LEAST8_TYPE)
4458 uint_least8_type_node =
9e6687c8 4459 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
f3449a3c 4460 if (UINT_LEAST16_TYPE)
4461 uint_least16_type_node =
9e6687c8 4462 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
f3449a3c 4463 if (UINT_LEAST32_TYPE)
4464 uint_least32_type_node =
9e6687c8 4465 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
f3449a3c 4466 if (UINT_LEAST64_TYPE)
4467 uint_least64_type_node =
9e6687c8 4468 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
f3449a3c 4469 if (INT_FAST8_TYPE)
4470 int_fast8_type_node =
9e6687c8 4471 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
f3449a3c 4472 if (INT_FAST16_TYPE)
4473 int_fast16_type_node =
9e6687c8 4474 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
f3449a3c 4475 if (INT_FAST32_TYPE)
4476 int_fast32_type_node =
9e6687c8 4477 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
f3449a3c 4478 if (INT_FAST64_TYPE)
4479 int_fast64_type_node =
9e6687c8 4480 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
f3449a3c 4481 if (UINT_FAST8_TYPE)
4482 uint_fast8_type_node =
9e6687c8 4483 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
f3449a3c 4484 if (UINT_FAST16_TYPE)
4485 uint_fast16_type_node =
9e6687c8 4486 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
f3449a3c 4487 if (UINT_FAST32_TYPE)
4488 uint_fast32_type_node =
9e6687c8 4489 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
f3449a3c 4490 if (UINT_FAST64_TYPE)
4491 uint_fast64_type_node =
9e6687c8 4492 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
f3449a3c 4493 if (INTPTR_TYPE)
4494 intptr_type_node =
9e6687c8 4495 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
f3449a3c 4496 if (UINTPTR_TYPE)
4497 uintptr_type_node =
9e6687c8 4498 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
f3449a3c 4499
3a939d12 4500 default_function_type
4501 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
11773141 4502 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
6bf5ed8d 4503
dc24ddbd 4504 lang_hooks.decls.pushdecl
e60a6f7b 4505 (build_decl (UNKNOWN_LOCATION,
4506 TYPE_DECL, get_identifier ("__builtin_va_list"),
20325f61 4507 va_list_type_node));
202d6e5f 4508 if (targetm.enum_va_list_p)
acd6f472 4509 {
4510 int l;
4511 const char *pname;
4512 tree ptype;
5f57a8b1 4513
202d6e5f 4514 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
acd6f472 4515 {
4516 lang_hooks.decls.pushdecl
4517 (build_decl (UNKNOWN_LOCATION,
4518 TYPE_DECL, get_identifier (pname),
4519 ptype));
ab2c1de8 4520
acd6f472 4521 }
4522 }
8a15c04a 4523
8a15c04a 4524 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
2d47cc32 4525 {
4526 va_list_arg_type_node = va_list_ref_type_node =
4527 build_pointer_type (TREE_TYPE (va_list_type_node));
4528 }
8a15c04a 4529 else
2d47cc32 4530 {
4531 va_list_arg_type_node = va_list_type_node;
4532 va_list_ref_type_node = build_reference_type (va_list_type_node);
4533 }
1cae46be 4534
dce22712 4535 if (!flag_preprocess_only)
4536 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
ffa8918b 4537
5c62f199 4538 main_identifier_node = get_identifier ("main");
ae84079f 4539
4540 /* Create the built-in __null node. It is important that this is
4541 not shared. */
271e739a 4542 null_node = make_int_cst (1, 1);
ae84079f 4543 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
27213ba3 4544
4545 /* Since builtin_types isn't gc'ed, don't export these nodes. */
4546 memset (builtin_types, 0, sizeof (builtin_types));
72040e7e 4547}
a66c9326 4548
79b01846 4549/* The number of named compound-literals generated thus far. */
4550static GTY(()) int compound_literal_number;
4551
4552/* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
4553
4554void
4555set_compound_literal_name (tree decl)
4556{
4557 char *name;
4558 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
4559 compound_literal_number);
4560 compound_literal_number++;
4561 DECL_NAME (decl) = get_identifier (name);
4562}
4563
2e474820 4564/* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
4565 TYPE and operand OP. */
4566
4567static tree
4568build_va_arg_1 (location_t loc, tree type, tree op)
4569{
4570 tree expr = build1 (VA_ARG_EXPR, type, op);
4571 SET_EXPR_LOCATION (expr, loc);
4572 return expr;
4573}
4574
4575/* Return a VA_ARG_EXPR corresponding to a source-level expression
4576 va_arg (EXPR, TYPE) at source location LOC. */
4577
a66c9326 4578tree
e60a6f7b 4579build_va_arg (location_t loc, tree expr, tree type)
a66c9326 4580{
c37be9ec 4581 tree va_type = TREE_TYPE (expr);
4582 tree canon_va_type = (va_type == error_mark_node
ea2ec8f6 4583 ? error_mark_node
c37be9ec 4584 : targetm.canonical_va_list_type (va_type));
4585
2e474820 4586 if (va_type == error_mark_node
4587 || canon_va_type == NULL_TREE)
c37be9ec 4588 {
ea2ec8f6 4589 if (canon_va_type == NULL_TREE)
4590 error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4591
2e474820 4592 /* Let's handle things neutrallly, if expr:
4593 - has undeclared type, or
4594 - is not an va_list type. */
ea2ec8f6 4595 return build_va_arg_1 (loc, type, error_mark_node);
c37be9ec 4596 }
f7fec1c7 4597
2e474820 4598 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
4599 {
4600 /* Case 1: Not an array type. */
4601
3bb45f76 4602 /* Take the address, to get '&ap'. Note that &ap is not a va_list
4603 type. */
2e474820 4604 mark_addressable (expr);
4605 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
4606
2e474820 4607 return build_va_arg_1 (loc, type, expr);
4608 }
4609
4610 /* Case 2: Array type.
4611
4612 Background:
4613
4614 For contrast, let's start with the simple case (case 1). If
4615 canon_va_type is not an array type, but say a char *, then when
4616 passing-by-value a va_list, the type of the va_list param decl is
4617 the same as for another va_list decl (all ap's are char *):
4618
4619 f2_1 (char * ap)
4620 D.1815 = VA_ARG (&ap, 0B, 1);
4621 return D.1815;
4622
4623 f2 (int i)
4624 char * ap.0;
4625 char * ap;
4626 __builtin_va_start (&ap, 0);
4627 ap.0 = ap;
4628 res = f2_1 (ap.0);
4629 __builtin_va_end (&ap);
4630 D.1812 = res;
4631 return D.1812;
4632
4633 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
4634 va_list the type of the va_list param decl (case 2b, struct * ap) is not
4635 the same as for another va_list decl (case 2a, struct ap[1]).
4636
4637 f2_1 (struct * ap)
4638 D.1844 = VA_ARG (ap, 0B, 0);
4639 return D.1844;
4640
4641 f2 (int i)
4642 struct ap[1];
4643 __builtin_va_start (&ap, 0);
4644 res = f2_1 (&ap);
4645 __builtin_va_end (&ap);
4646 D.1841 = res;
4647 return D.1841;
4648
4649 Case 2b is different because:
4650 - on the callee side, the parm decl has declared type va_list, but
4651 grokdeclarator changes the type of the parm decl to a pointer to the
4652 array elem type.
4653 - on the caller side, the pass-by-value uses &ap.
4654
4655 We unify these two cases (case 2a: va_list is array type,
4656 case 2b: va_list is pointer to array elem type), by adding '&' for the
4657 array type case, such that we have a pointer to array elem in both
4658 cases. */
4659
4660 if (TREE_CODE (va_type) == ARRAY_TYPE)
4661 {
4662 /* Case 2a: va_list is array type. */
4663
4664 /* Take the address, to get '&ap'. Make sure it's a pointer to array
4665 elem type. */
4666 mark_addressable (expr);
4667 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
4668 expr);
4669
4670 /* Verify that &ap is still recognized as having va_list type. */
4671 tree canon_expr_type
4672 = targetm.canonical_va_list_type (TREE_TYPE (expr));
3bb45f76 4673 gcc_assert (canon_expr_type != NULL_TREE);
2e474820 4674 }
4675 else
4676 {
4677 /* Case 2b: va_list is pointer to array elem type. */
4678 gcc_assert (POINTER_TYPE_P (va_type));
839e4d28 4679
4680 /* Comparison as in std_canonical_va_list_type. */
4681 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (va_type))
4682 == TYPE_MAIN_VARIANT (TREE_TYPE (canon_va_type)));
2e474820 4683
4684 /* Don't take the address. We've already got '&ap'. */
4685 ;
4686 }
4687
4688 return build_va_arg_1 (loc, type, expr);
a66c9326 4689}
0d4238dc 4690
4691
dd878098 4692/* Linked list of disabled built-in functions. */
4693
6dc50383 4694struct disabled_builtin
dd878098 4695{
4696 const char *name;
4697 struct disabled_builtin *next;
6dc50383 4698};
dd878098 4699static disabled_builtin *disabled_builtins = NULL;
4700
1cae46be 4701static bool builtin_function_disabled_p (const char *);
dd878098 4702
4703/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
4704 begins with "__builtin_", give an error. */
4705
4706void
1cae46be 4707disable_builtin_function (const char *name)
dd878098 4708{
4709 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
b0b1af64 4710 error ("cannot disable built-in function %qs", name);
dd878098 4711 else
4712 {
e85905e5 4713 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
4714 new_disabled_builtin->name = name;
4715 new_disabled_builtin->next = disabled_builtins;
4716 disabled_builtins = new_disabled_builtin;
dd878098 4717 }
4718}
4719
4720
4721/* Return true if the built-in function NAME has been disabled, false
4722 otherwise. */
4723
4724static bool
1cae46be 4725builtin_function_disabled_p (const char *name)
dd878098 4726{
4727 disabled_builtin *p;
4728 for (p = disabled_builtins; p != NULL; p = p->next)
4729 {
4730 if (strcmp (name, p->name) == 0)
4731 return true;
4732 }
4733 return false;
4734}
4735
4736
3237155d 4737/* Worker for DEF_BUILTIN.
4738 Possibly define a builtin function with one or two names.
4739 Does not declare a non-__builtin_ function if flag_no_builtin, or if
4740 nonansi_p and flag_no_nonansi_builtin. */
0d4238dc 4741
3237155d 4742static void
4743def_builtin_1 (enum built_in_function fncode,
4744 const char *name,
4745 enum built_in_class fnclass,
4746 tree fntype, tree libtype,
4747 bool both_p, bool fallback_p, bool nonansi_p,
4748 tree fnattrs, bool implicit_p)
0d4238dc 4749{
3237155d 4750 tree decl;
4751 const char *libname;
4752
27213ba3 4753 if (fntype == error_mark_node)
4754 return;
4755
3237155d 4756 gcc_assert ((!both_p && !fallback_p)
4757 || !strncmp (name, "__builtin_",
4758 strlen ("__builtin_")));
4759
4760 libname = name + strlen ("__builtin_");
54be5d7e 4761 decl = add_builtin_function (name, fntype, fncode, fnclass,
4762 (fallback_p ? libname : NULL),
4763 fnattrs);
b9a16870 4764
4765 set_builtin_decl (fncode, decl, implicit_p);
4766
3237155d 4767 if (both_p
4768 && !flag_no_builtin && !builtin_function_disabled_p (libname)
dd878098 4769 && !(nonansi_p && flag_no_nonansi_builtin))
54be5d7e 4770 add_builtin_function (libname, libtype, fncode, fnclass,
4771 NULL, fnattrs);
0d4238dc 4772}
e94026da 4773\f
d7aeef06 4774/* Nonzero if the type T promotes to int. This is (nearly) the
4775 integral promotions defined in ISO C99 6.3.1.1/2. */
4776
4777bool
9f627b1a 4778c_promoting_integer_type_p (const_tree t)
d7aeef06 4779{
4780 switch (TREE_CODE (t))
4781 {
4782 case INTEGER_TYPE:
4783 return (TYPE_MAIN_VARIANT (t) == char_type_node
4784 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
4785 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
4786 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
7aa1e6eb 4787 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
4788 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d7aeef06 4789
4790 case ENUMERAL_TYPE:
4791 /* ??? Technically all enumerations not larger than an int
4792 promote to an int. But this is used along code paths
4793 that only want to notice a size change. */
4794 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
4795
4796 case BOOLEAN_TYPE:
78173203 4797 return true;
d7aeef06 4798
4799 default:
78173203 4800 return false;
d7aeef06 4801 }
4802}
4803
e94026da 4804/* Return 1 if PARMS specifies a fixed number of parameters
4805 and none of their types is affected by default promotions. */
4806
2823920b 4807bool
9f627b1a 4808self_promoting_args_p (const_tree parms)
e94026da 4809{
9f627b1a 4810 const_tree t;
e94026da 4811 for (t = parms; t; t = TREE_CHAIN (t))
4812 {
19cb6b50 4813 tree type = TREE_VALUE (t);
43f74bc4 4814
e1d8e198 4815 if (type == error_mark_node)
4816 continue;
4817
72749341 4818 if (TREE_CHAIN (t) == NULL_TREE && type != void_type_node)
2823920b 4819 return false;
e94026da 4820
72749341 4821 if (type == NULL_TREE)
2823920b 4822 return false;
e94026da 4823
4824 if (TYPE_MAIN_VARIANT (type) == float_type_node)
2823920b 4825 return false;
e94026da 4826
d7aeef06 4827 if (c_promoting_integer_type_p (type))
2823920b 4828 return false;
e94026da 4829 }
2823920b 4830 return true;
e94026da 4831}
605fb01e 4832
c10de5e7 4833/* Recursively remove any '*' or '&' operator from TYPE. */
4834tree
4835strip_pointer_operator (tree t)
4836{
4837 while (POINTER_TYPE_P (t))
4838 t = TREE_TYPE (t);
4839 return t;
4840}
4841
57a0ed23 4842/* Recursively remove pointer or array type from TYPE. */
4843tree
4844strip_pointer_or_array_types (tree t)
4845{
4846 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
4847 t = TREE_TYPE (t);
4848 return t;
4849}
4850
e41f0d80 4851/* Used to compare case labels. K1 and K2 are actually tree nodes
4852 representing case labels, or NULL_TREE for a `default' label.
4853 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
4854 K2, and 0 if K1 and K2 are equal. */
4855
4856int
1cae46be 4857case_compare (splay_tree_key k1, splay_tree_key k2)
e41f0d80 4858{
4859 /* Consider a NULL key (such as arises with a `default' label) to be
4860 smaller than anything else. */
4861 if (!k1)
4862 return k2 ? -1 : 0;
4863 else if (!k2)
4864 return k1 ? 1 : 0;
4865
4866 return tree_int_cst_compare ((tree) k1, (tree) k2);
4867}
4868
e60a6f7b 4869/* Process a case label, located at LOC, for the range LOW_VALUE
4870 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
4871 then this case label is actually a `default' label. If only
4872 HIGH_VALUE is NULL_TREE, then case label was declared using the
4873 usual C/C++ syntax, rather than the GNU case range extension.
4874 CASES is a tree containing all the case ranges processed so far;
be23b16f 4875 COND is the condition for the switch-statement itself.
4876 OUTSIDE_RANGE_P says whether there was a case value that doesn't
4877 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
4878 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
e41f0d80 4879
4880tree
e60a6f7b 4881c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
be23b16f 4882 tree low_value, tree high_value, bool *outside_range_p)
e41f0d80 4883{
4884 tree type;
4885 tree label;
4886 tree case_label;
4887 splay_tree_node node;
4888
4889 /* Create the LABEL_DECL itself. */
e60a6f7b 4890 label = create_artificial_label (loc);
e41f0d80 4891
4892 /* If there was an error processing the switch condition, bail now
4893 before we get more confused. */
4894 if (!cond || cond == error_mark_node)
4ee9c684 4895 goto error_out;
e41f0d80 4896
1cae46be 4897 if ((low_value && TREE_TYPE (low_value)
4898 && POINTER_TYPE_P (TREE_TYPE (low_value)))
e41f0d80 4899 || (high_value && TREE_TYPE (high_value)
4900 && POINTER_TYPE_P (TREE_TYPE (high_value))))
b96dc121 4901 {
e60a6f7b 4902 error_at (loc, "pointers are not permitted as case values");
b96dc121 4903 goto error_out;
4904 }
e41f0d80 4905
4906 /* Case ranges are a GNU extension. */
8864917d 4907 if (high_value)
29438999 4908 pedwarn (loc, OPT_Wpedantic,
8864917d 4909 "range expressions in switch statements are non-standard");
e41f0d80 4910
4911 type = TREE_TYPE (cond);
4912 if (low_value)
4913 {
2d2f6a15 4914 low_value = check_case_value (loc, low_value);
22a75734 4915 low_value = convert_and_check (loc, type, low_value);
96722196 4916 if (low_value == error_mark_node)
4917 goto error_out;
e41f0d80 4918 }
4919 if (high_value)
4920 {
2d2f6a15 4921 high_value = check_case_value (loc, high_value);
22a75734 4922 high_value = convert_and_check (loc, type, high_value);
96722196 4923 if (high_value == error_mark_node)
4924 goto error_out;
e41f0d80 4925 }
4926
96722196 4927 if (low_value && high_value)
4928 {
4929 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
a0c938f0 4930 really a case range, even though it was written that way.
4931 Remove the HIGH_VALUE to simplify later processing. */
96722196 4932 if (tree_int_cst_equal (low_value, high_value))
4933 high_value = NULL_TREE;
4934 else if (!tree_int_cst_lt (low_value, high_value))
e60a6f7b 4935 warning_at (loc, 0, "empty range specified");
96722196 4936 }
e41f0d80 4937
2ca392fd 4938 /* See if the case is in range of the type of the original testing
4939 expression. If both low_value and high_value are out of range,
4940 don't insert the case label and return NULL_TREE. */
4941 if (low_value
f61a9bc2 4942 && !check_case_bounds (loc, type, orig_type,
be23b16f 4943 &low_value, high_value ? &high_value : NULL,
4944 outside_range_p))
2ca392fd 4945 return NULL_TREE;
4946
e41f0d80 4947 /* Look up the LOW_VALUE in the table of case labels we already
4948 have. */
4949 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
4950 /* If there was not an exact match, check for overlapping ranges.
4951 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
4952 that's a `default' label and the only overlap is an exact match. */
4953 if (!node && (low_value || high_value))
4954 {
4955 splay_tree_node low_bound;
4956 splay_tree_node high_bound;
4957
4958 /* Even though there wasn't an exact match, there might be an
4959 overlap between this case range and another case range.
4960 Since we've (inductively) not allowed any overlapping case
4961 ranges, we simply need to find the greatest low case label
4962 that is smaller that LOW_VALUE, and the smallest low case
4963 label that is greater than LOW_VALUE. If there is an overlap
4964 it will occur in one of these two ranges. */
4965 low_bound = splay_tree_predecessor (cases,
4966 (splay_tree_key) low_value);
4967 high_bound = splay_tree_successor (cases,
4968 (splay_tree_key) low_value);
4969
4970 /* Check to see if the LOW_BOUND overlaps. It is smaller than
4971 the LOW_VALUE, so there is no need to check unless the
4972 LOW_BOUND is in fact itself a case range. */
4973 if (low_bound
4974 && CASE_HIGH ((tree) low_bound->value)
4975 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
4976 low_value) >= 0)
4977 node = low_bound;
4978 /* Check to see if the HIGH_BOUND overlaps. The low end of that
4979 range is bigger than the low end of the current range, so we
4980 are only interested if the current range is a real range, and
4981 not an ordinary case label. */
1cae46be 4982 else if (high_bound
e41f0d80 4983 && high_value
4984 && (tree_int_cst_compare ((tree) high_bound->key,
4985 high_value)
4986 <= 0))
4987 node = high_bound;
4988 }
4989 /* If there was an overlap, issue an error. */
4990 if (node)
4991 {
eaae3b75 4992 tree duplicate = CASE_LABEL ((tree) node->value);
e41f0d80 4993
4994 if (high_value)
4995 {
e60a6f7b 4996 error_at (loc, "duplicate (or overlapping) case value");
6c1f90ee 4997 inform (DECL_SOURCE_LOCATION (duplicate),
4998 "this is the first entry overlapping that value");
e41f0d80 4999 }
5000 else if (low_value)
5001 {
e60a6f7b 5002 error_at (loc, "duplicate case value") ;
6c1f90ee 5003 inform (DECL_SOURCE_LOCATION (duplicate), "previously used here");
e41f0d80 5004 }
5005 else
5006 {
0f4cea33 5007 error_at (loc, "multiple default labels in one switch");
6c1f90ee 5008 inform (DECL_SOURCE_LOCATION (duplicate),
5009 "this is the first default label");
4ee9c684 5010 }
0f4cea33 5011 goto error_out;
4ee9c684 5012 }
359d87c6 5013
0f4cea33 5014 /* Add a CASE_LABEL to the statement-tree. */
5015 case_label = add_stmt (build_case_label (low_value, high_value, label));
5016 /* Register this case label in the splay tree. */
5017 splay_tree_insert (cases,
5018 (splay_tree_key) low_value,
5019 (splay_tree_value) case_label);
359d87c6 5020
0f4cea33 5021 return case_label;
6cbbbc89 5022
0f4cea33 5023 error_out:
5024 /* Add a label so that the back-end doesn't think that the beginning of
5025 the switch is unreachable. Note that we do not add a case label, as
5026 that just leads to duplicates and thence to failure later on. */
5027 if (!cases->root)
5028 {
5029 tree t = create_artificial_label (loc);
5030 add_stmt (build_stmt (loc, LABEL_EXPR, t));
5031 }
5032 return error_mark_node;
e41f0d80 5033}
5034
dfa10f0d 5035/* Subroutine of c_switch_covers_all_cases_p, called via
5036 splay_tree_foreach. Return 1 if it doesn't cover all the cases.
5037 ARGS[0] is initially NULL and after the first iteration is the
5038 so far highest case label. ARGS[1] is the minimum of SWITCH_COND's
5039 type. */
5040
5041static int
5042c_switch_covers_all_cases_p_1 (splay_tree_node node, void *data)
5043{
5044 tree label = (tree) node->value;
5045 tree *args = (tree *) data;
5046
5047 /* If there is a default case, we shouldn't have called this. */
5048 gcc_assert (CASE_LOW (label));
5049
5050 if (args[0] == NULL_TREE)
5051 {
5052 if (wi::to_widest (args[1]) < wi::to_widest (CASE_LOW (label)))
5053 return 1;
5054 }
5055 else if (wi::add (wi::to_widest (args[0]), 1)
5056 != wi::to_widest (CASE_LOW (label)))
5057 return 1;
5058 if (CASE_HIGH (label))
5059 args[0] = CASE_HIGH (label);
5060 else
5061 args[0] = CASE_LOW (label);
5062 return 0;
5063}
5064
5065/* Return true if switch with CASES and switch condition with type
5066 covers all possible values in the case labels. */
5067
5068bool
5069c_switch_covers_all_cases_p (splay_tree cases, tree type)
5070{
5071 /* If there is default:, this is always the case. */
5072 splay_tree_node default_node
5073 = splay_tree_lookup (cases, (splay_tree_key) NULL);
5074 if (default_node)
5075 return true;
5076
5077 if (!INTEGRAL_TYPE_P (type))
5078 return false;
5079
5080 tree args[2] = { NULL_TREE, TYPE_MIN_VALUE (type) };
5081 if (splay_tree_foreach (cases, c_switch_covers_all_cases_p_1, args))
5082 return false;
5083
5084 /* If there are no cases at all, or if the highest case label
5085 is smaller than TYPE_MAX_VALUE, return false. */
5086 if (args[0] == NULL_TREE
5087 || wi::to_widest (args[0]) < wi::to_widest (TYPE_MAX_VALUE (type)))
5088 return false;
5089
5090 return true;
5091}
5092
9dd48740 5093/* Finish an expression taking the address of LABEL (an
dda49785 5094 IDENTIFIER_NODE). Returns an expression for the address.
5095
5096 LOC is the location for the expression returned. */
d0a47c8d 5097
1cae46be 5098tree
dda49785 5099finish_label_address_expr (tree label, location_t loc)
d0a47c8d 5100{
5101 tree result;
5102
29438999 5103 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
d0a47c8d 5104
9dd48740 5105 if (label == error_mark_node)
5106 return error_mark_node;
5107
d0a47c8d 5108 label = lookup_label (label);
5109 if (label == NULL_TREE)
5110 result = null_pointer_node;
5111 else
5112 {
5113 TREE_USED (label) = 1;
5114 result = build1 (ADDR_EXPR, ptr_type_node, label);
344cd9b2 5115 /* The current function is not necessarily uninlinable.
d0a47c8d 5116 Computed gotos are incompatible with inlining, but the value
5117 here could be used only in a diagnostic, for example. */
dda49785 5118 protected_set_expr_location (result, loc);
d0a47c8d 5119 }
5120
5121 return result;
5122}
4f9a1c9b 5123\f
5124
5125/* Given a boolean expression ARG, return a tree representing an increment
5126 or decrement (as indicated by CODE) of ARG. The front end must check for
5127 invalid cases (e.g., decrement in C++). */
5128tree
1cae46be 5129boolean_increment (enum tree_code code, tree arg)
4f9a1c9b 5130{
5131 tree val;
69db191c 5132 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
c0f19401 5133
4f9a1c9b 5134 arg = stabilize_reference (arg);
5135 switch (code)
5136 {
5137 case PREINCREMENT_EXPR:
14ae0310 5138 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 5139 break;
5140 case POSTINCREMENT_EXPR:
14ae0310 5141 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4f9a1c9b 5142 arg = save_expr (arg);
14ae0310 5143 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5144 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 5145 break;
5146 case PREDECREMENT_EXPR:
14ae0310 5147 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 5148 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 5149 break;
5150 case POSTDECREMENT_EXPR:
14ae0310 5151 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
389dd41b 5152 invert_truthvalue_loc (input_location, arg));
4f9a1c9b 5153 arg = save_expr (arg);
14ae0310 5154 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5155 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4f9a1c9b 5156 break;
5157 default:
231bd014 5158 gcc_unreachable ();
4f9a1c9b 5159 }
5160 TREE_SIDE_EFFECTS (val) = 1;
5161 return val;
5162}
76a6e674 5163\f
f3449a3c 5164/* Built-in macros for stddef.h and stdint.h, that require macros
5165 defined in this file. */
79cf3ec1 5166void
1cae46be 5167c_stddef_cpp_builtins(void)
1ed9d5f5 5168{
63994318 5169 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
5170 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
5171 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
5172 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
36bccbfc 5173 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
5174 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
82362779 5175 if (flag_char8_t)
5176 builtin_define_with_value ("__CHAR8_TYPE__", CHAR8_TYPE, 0);
b0726616 5177 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
5178 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
f3449a3c 5179 if (SIG_ATOMIC_TYPE)
5180 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
5181 if (INT8_TYPE)
5182 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
5183 if (INT16_TYPE)
5184 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
5185 if (INT32_TYPE)
5186 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
5187 if (INT64_TYPE)
5188 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
5189 if (UINT8_TYPE)
5190 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
5191 if (UINT16_TYPE)
5192 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
5193 if (UINT32_TYPE)
5194 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
5195 if (UINT64_TYPE)
5196 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
5197 if (INT_LEAST8_TYPE)
5198 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
5199 if (INT_LEAST16_TYPE)
5200 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
5201 if (INT_LEAST32_TYPE)
5202 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
5203 if (INT_LEAST64_TYPE)
5204 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
5205 if (UINT_LEAST8_TYPE)
5206 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
5207 if (UINT_LEAST16_TYPE)
5208 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
5209 if (UINT_LEAST32_TYPE)
5210 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
5211 if (UINT_LEAST64_TYPE)
5212 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
5213 if (INT_FAST8_TYPE)
5214 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
5215 if (INT_FAST16_TYPE)
5216 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
5217 if (INT_FAST32_TYPE)
5218 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
5219 if (INT_FAST64_TYPE)
5220 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
5221 if (UINT_FAST8_TYPE)
5222 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
5223 if (UINT_FAST16_TYPE)
5224 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
5225 if (UINT_FAST32_TYPE)
5226 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
5227 if (UINT_FAST64_TYPE)
5228 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
5229 if (INTPTR_TYPE)
5230 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
5231 if (UINTPTR_TYPE)
5232 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
574006c3 5233}
5234
7d3b509a 5235static void
1cae46be 5236c_init_attributes (void)
7d3b509a 5237{
5238 /* Fill in the built_in_attributes array. */
7c446c95 5239#define DEF_ATTR_NULL_TREE(ENUM) \
7d3b509a 5240 built_in_attributes[(int) ENUM] = NULL_TREE;
7c446c95 5241#define DEF_ATTR_INT(ENUM, VALUE) \
ceb7b692 5242 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
c8010b80 5243#define DEF_ATTR_STRING(ENUM, VALUE) \
5244 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
7d3b509a 5245#define DEF_ATTR_IDENT(ENUM, STRING) \
5246 built_in_attributes[(int) ENUM] = get_identifier (STRING);
5247#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5248 built_in_attributes[(int) ENUM] \
5249 = tree_cons (built_in_attributes[(int) PURPOSE], \
5250 built_in_attributes[(int) VALUE], \
5251 built_in_attributes[(int) CHAIN]);
7d3b509a 5252#include "builtin-attrs.def"
5253#undef DEF_ATTR_NULL_TREE
5254#undef DEF_ATTR_INT
5255#undef DEF_ATTR_IDENT
5256#undef DEF_ATTR_TREE_LIST
76a6e674 5257}
5f3cead1 5258
ff31e352 5259/* Check whether the byte alignment ALIGN is a valid user-specified
5260 alignment less than the supported maximum. If so, return ALIGN's
5261 base-2 log; if not, output an error and return -1. If OBJFILE
5262 then reject alignments greater than MAX_OFILE_ALIGNMENT when
5263 converted to bits. Otherwise, consider valid only alignments
5264 that are less than HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT.
85e8db42 5265 Zero is not considered a valid argument (and results in -1 on
5266 return) but it only triggers a warning when WARN_ZERO is set. */
ff31e352 5267
b0c98c23 5268int
85e8db42 5269check_user_alignment (const_tree align, bool objfile, bool warn_zero)
33199a81 5270{
b0c98c23 5271 if (error_operand_p (align))
5272 return -1;
ff31e352 5273
b0c98c23 5274 if (TREE_CODE (align) != INTEGER_CST
5275 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
f8e93a2e 5276 {
b0c98c23 5277 error ("requested alignment is not an integer constant");
5278 return -1;
f8e93a2e 5279 }
ff31e352 5280
85e8db42 5281 if (integer_zerop (align))
5282 {
5283 if (warn_zero)
5284 warning (OPT_Wattributes,
5285 "requested alignment %qE is not a positive power of 2",
5286 align);
5287 return -1;
5288 }
ff31e352 5289
5290 int log2bitalign;
5291 if (tree_int_cst_sgn (align) == -1
5292 || (log2bitalign = tree_log2 (align)) == -1)
c2ab04f9 5293 {
ff31e352 5294 error ("requested alignment %qE is not a positive power of 2",
5295 align);
b0c98c23 5296 return -1;
c2ab04f9 5297 }
ff31e352 5298
5299 if (objfile)
f8e93a2e 5300 {
ff31e352 5301 unsigned maxalign = MAX_OFILE_ALIGNMENT / BITS_PER_UNIT;
5302 if (tree_to_shwi (align) > maxalign)
5303 {
5304 error ("requested alignment %qE exceeds object file maximum %u",
5305 align, maxalign);
5306 return -1;
5307 }
5308 }
5309
5310 if (log2bitalign >= HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT)
5311 {
5312 error ("requested alignment %qE exceeds maximum %u",
5313 align, 1U << (HOST_BITS_PER_INT - 1));
b0c98c23 5314 return -1;
f8e93a2e 5315 }
ff31e352 5316
5317 return log2bitalign;
f8e93a2e 5318}
5319
b0c98c23 5320/* Determine the ELF symbol visibility for DECL, which is either a
5321 variable or a function. It is an error to use this function if a
5322 definition of DECL is not available in this translation unit.
5323 Returns true if the final visibility has been determined by this
5324 function; false if the caller is free to make additional
5325 modifications. */
f8e93a2e 5326
b0c98c23 5327bool
5328c_determine_visibility (tree decl)
f8e93a2e 5329{
b0c98c23 5330 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
f8e93a2e 5331
b0c98c23 5332 /* If the user explicitly specified the visibility with an
5333 attribute, honor that. DECL_VISIBILITY will have been set during
5334 the processing of the attribute. We check for an explicit
5335 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
5336 to distinguish the use of an attribute from the use of a "#pragma
5337 GCC visibility push(...)"; in the latter case we still want other
5338 considerations to be able to overrule the #pragma. */
5339 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
5340 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
5341 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
5342 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
5343 return true;
f8e93a2e 5344
b0c98c23 5345 /* Set default visibility to whatever the user supplied with
5346 visibility_specified depending on #pragma GCC visibility. */
5347 if (!DECL_VISIBILITY_SPECIFIED (decl))
f8e93a2e 5348 {
b0c98c23 5349 if (visibility_options.inpragma
5350 || DECL_VISIBILITY (decl) != default_visibility)
5351 {
5352 DECL_VISIBILITY (decl) = default_visibility;
5353 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
5354 /* If visibility changed and DECL already has DECL_RTL, ensure
5355 symbol flags are updated. */
5356 if (((VAR_P (decl) && TREE_STATIC (decl))
5357 || TREE_CODE (decl) == FUNCTION_DECL)
5358 && DECL_RTL_SET_P (decl))
5359 make_decl_rtl (decl);
5360 }
f8e93a2e 5361 }
b0c98c23 5362 return false;
f8e93a2e 5363}
5364
184fac50 5365/* Data to communicate through check_function_arguments_recurse between
5366 check_function_nonnull and check_nonnull_arg. */
5367
5368struct nonnull_arg_ctx
5369{
5370 location_t loc;
5371 bool warned_p;
5372};
5373
b0c98c23 5374/* Check the argument list of a function call for null in argument slots
5375 that are marked as requiring a non-null pointer argument. The NARGS
184fac50 5376 arguments are passed in the array ARGARRAY. Return true if we have
5377 warned. */
f8e93a2e 5378
184fac50 5379static bool
b0c98c23 5380check_function_nonnull (location_t loc, tree attrs, int nargs, tree *argarray)
f8e93a2e 5381{
b0c98c23 5382 tree a;
5383 int i;
f8e93a2e 5384
b0c98c23 5385 attrs = lookup_attribute ("nonnull", attrs);
5386 if (attrs == NULL_TREE)
184fac50 5387 return false;
f8e93a2e 5388
b0c98c23 5389 a = attrs;
5390 /* See if any of the nonnull attributes has no arguments. If so,
5391 then every pointer argument is checked (in which case the check
5392 for pointer type is done in check_nonnull_arg). */
5393 if (TREE_VALUE (a) != NULL_TREE)
5394 do
5395 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
5396 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
5de92639 5397
184fac50 5398 struct nonnull_arg_ctx ctx = { loc, false };
b0c98c23 5399 if (a != NULL_TREE)
5400 for (i = 0; i < nargs; i++)
184fac50 5401 check_function_arguments_recurse (check_nonnull_arg, &ctx, argarray[i],
b0c98c23 5402 i + 1);
5403 else
5de92639 5404 {
b0c98c23 5405 /* Walk the argument list. If we encounter an argument number we
5406 should check for non-null, do it. */
5407 for (i = 0; i < nargs; i++)
5de92639 5408 {
b0c98c23 5409 for (a = attrs; ; a = TREE_CHAIN (a))
5410 {
5411 a = lookup_attribute ("nonnull", a);
5412 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
5413 break;
5414 }
5415
5416 if (a != NULL_TREE)
184fac50 5417 check_function_arguments_recurse (check_nonnull_arg, &ctx,
b0c98c23 5418 argarray[i], i + 1);
5de92639 5419 }
5de92639 5420 }
184fac50 5421 return ctx.warned_p;
5de92639 5422}
758a38ab 5423
b0c98c23 5424/* Check that the Nth argument of a function call (counting backwards
5425 from the end) is a (pointer)0. The NARGS arguments are passed in the
5426 array ARGARRAY. */
5de92639 5427
b0c98c23 5428static void
5429check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
5de92639 5430{
b0c98c23 5431 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
5432
5433 if (attr)
5de92639 5434 {
b0c98c23 5435 int len = 0;
5436 int pos = 0;
5437 tree sentinel;
5438 function_args_iterator iter;
5439 tree t;
5440
5441 /* Skip over the named arguments. */
5442 FOREACH_FUNCTION_ARGS (fntype, t, iter)
5de92639 5443 {
b0c98c23 5444 if (len == nargs)
5445 break;
5446 len++;
5de92639 5447 }
5de92639 5448
b0c98c23 5449 if (TREE_VALUE (attr))
5450 {
5451 tree p = TREE_VALUE (TREE_VALUE (attr));
5452 pos = TREE_INT_CST_LOW (p);
5453 }
5de92639 5454
b0c98c23 5455 /* The sentinel must be one of the varargs, i.e.
5456 in position >= the number of fixed arguments. */
5457 if ((nargs - 1 - pos) < len)
5458 {
5459 warning (OPT_Wformat_,
5460 "not enough variable arguments to fit a sentinel");
5461 return;
5462 }
d413ffdd 5463
b0c98c23 5464 /* Validate the sentinel. */
8e415b30 5465 sentinel = fold_for_warn (argarray[nargs - 1 - pos]);
b0c98c23 5466 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
5467 || !integer_zerop (sentinel))
5468 /* Although __null (in C++) is only an integer we allow it
5469 nevertheless, as we are guaranteed that it's exactly
5470 as wide as a pointer, and we don't want to force
5471 users to cast the NULL they have written there.
5472 We warn with -Wstrict-null-sentinel, though. */
5473 && (warn_strict_null_sentinel || null_node != sentinel))
5474 warning (OPT_Wformat_, "missing sentinel in function call");
d413ffdd 5475 }
d413ffdd 5476}
5477
75e9093e 5478/* Check that the same argument isn't passed to two or more
5479 restrict-qualified formal and issue a -Wrestrict warning
5480 if it is. Return true if a warning has been issued. */
bbd5521e 5481
75e9093e 5482static bool
bbd5521e 5483check_function_restrict (const_tree fndecl, const_tree fntype,
8e415b30 5484 int nargs, tree *unfolded_argarray)
bbd5521e 5485{
5486 int i;
e6a18b5a 5487 tree parms = TYPE_ARG_TYPES (fntype);
bbd5521e 5488
8e415b30 5489 /* Call fold_for_warn on all of the arguments. */
5490 auto_vec<tree> argarray (nargs);
5491 for (i = 0; i < nargs; i++)
5492 argarray.quick_push (fold_for_warn (unfolded_argarray[i]));
5493
bbd5521e 5494 if (fndecl
e6a18b5a 5495 && TREE_CODE (fndecl) == FUNCTION_DECL)
5496 {
75e9093e 5497 /* Avoid diagnosing calls built-ins with a zero size/bound
5498 here. They are checked in more detail elsewhere. */
a0e9bfbb 5499 if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
75e9093e 5500 && nargs == 3
5501 && TREE_CODE (argarray[2]) == INTEGER_CST
5502 && integer_zerop (argarray[2]))
5503 return false;
e6a18b5a 5504
5505 if (DECL_ARGUMENTS (fndecl))
5506 parms = DECL_ARGUMENTS (fndecl);
5507 }
bbd5521e 5508
5509 for (i = 0; i < nargs; i++)
5510 TREE_VISITED (argarray[i]) = 0;
5511
75e9093e 5512 bool warned = false;
5513
bbd5521e 5514 for (i = 0; i < nargs && parms && parms != void_list_node; i++)
5515 {
5516 tree type;
5517 if (TREE_CODE (parms) == PARM_DECL)
5518 {
5519 type = TREE_TYPE (parms);
5520 parms = DECL_CHAIN (parms);
5521 }
5522 else
5523 {
5524 type = TREE_VALUE (parms);
5525 parms = TREE_CHAIN (parms);
5526 }
5527 if (POINTER_TYPE_P (type)
5528 && TYPE_RESTRICT (type)
5529 && !TYPE_READONLY (TREE_TYPE (type)))
8e415b30 5530 warned |= warn_for_restrict (i, argarray.address (), nargs);
bbd5521e 5531 }
5532
5533 for (i = 0; i < nargs; i++)
5534 TREE_VISITED (argarray[i]) = 0;
75e9093e 5535
5536 return warned;
bbd5521e 5537}
5538
b0c98c23 5539/* Helper for check_function_nonnull; given a list of operands which
5540 must be non-null in ARGS, determine if operand PARAM_NUM should be
5541 checked. */
a9196da9 5542
b0c98c23 5543static bool
5544nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
a9196da9 5545{
b0c98c23 5546 unsigned HOST_WIDE_INT arg_num = 0;
05f893e1 5547
b0c98c23 5548 for (; args; args = TREE_CHAIN (args))
05f893e1 5549 {
b0c98c23 5550 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
05f893e1 5551
b0c98c23 5552 gcc_assert (found);
05f893e1 5553
b0c98c23 5554 if (arg_num == param_num)
5555 return true;
947aa916 5556 }
b0c98c23 5557 return false;
947aa916 5558}
5559
b0c98c23 5560/* Check that the function argument PARAM (which is operand number
5561 PARAM_NUM) is non-null. This is called by check_function_nonnull
5562 via check_function_arguments_recurse. */
f8e93a2e 5563
b0c98c23 5564static void
5565check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
f8e93a2e 5566{
184fac50 5567 struct nonnull_arg_ctx *pctx = (struct nonnull_arg_ctx *) ctx;
f8e93a2e 5568
b0c98c23 5569 /* Just skip checking the argument if it's not a pointer. This can
5570 happen if the "nonnull" attribute was given without an operand
5571 list (which means to check every pointer argument). */
bdb1f0d1 5572
b0c98c23 5573 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
5574 return;
bdb1f0d1 5575
4210c535 5576 /* Diagnose the simple cases of null arguments. */
5577 if (integer_zerop (fold_for_warn (param)))
184fac50 5578 {
5579 warning_at (pctx->loc, OPT_Wnonnull, "null argument where non-null "
5580 "required (argument %lu)", (unsigned long) param_num);
5581 pctx->warned_p = true;
5582 }
bdb1f0d1 5583}
5584
b0c98c23 5585/* Helper for nonnull attribute handling; fetch the operand number
5586 from the attribute argument list. */
85fbea97 5587
b0c98c23 5588bool
5589get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
85fbea97 5590{
b0c98c23 5591 /* Verify the arg number is a small constant. */
5592 if (tree_fits_uhwi_p (arg_num_expr))
85fbea97 5593 {
ab2a527a 5594 *valp = tree_to_uhwi (arg_num_expr);
b0c98c23 5595 return true;
85fbea97 5596 }
b0c98c23 5597 else
5598 return false;
ab50af2a 5599}
5600
46f8e3b0 5601/* Arguments being collected for optimization. */
5602typedef const char *const_char_p; /* For DEF_VEC_P. */
f1f41a6c 5603static GTY(()) vec<const_char_p, va_gc> *optimize_args;
46f8e3b0 5604
5605
5606/* Inner function to convert a TREE_LIST to argv string to parse the optimize
5607 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
5608 false for #pragma GCC optimize. */
5609
5610bool
5611parse_optimize_options (tree args, bool attr_p)
5612{
5613 bool ret = true;
5614 unsigned opt_argc;
5615 unsigned i;
5616 const char **opt_argv;
615ef0bb 5617 struct cl_decoded_option *decoded_options;
5618 unsigned int decoded_options_count;
46f8e3b0 5619 tree ap;
5620
5621 /* Build up argv vector. Just in case the string is stored away, use garbage
5622 collected strings. */
f1f41a6c 5623 vec_safe_truncate (optimize_args, 0);
5624 vec_safe_push (optimize_args, (const char *) NULL);
46f8e3b0 5625
5626 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
5627 {
5628 tree value = TREE_VALUE (ap);
5629
5630 if (TREE_CODE (value) == INTEGER_CST)
5631 {
5632 char buffer[20];
f9ae6f95 5633 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
f1f41a6c 5634 vec_safe_push (optimize_args, ggc_strdup (buffer));
46f8e3b0 5635 }
5636
5637 else if (TREE_CODE (value) == STRING_CST)
5638 {
5639 /* Split string into multiple substrings. */
5640 size_t len = TREE_STRING_LENGTH (value);
5641 char *p = ASTRDUP (TREE_STRING_POINTER (value));
5642 char *end = p + len;
5643 char *comma;
5644 char *next_p = p;
5645
5646 while (next_p != NULL)
5647 {
5648 size_t len2;
5649 char *q, *r;
5650
5651 p = next_p;
5652 comma = strchr (p, ',');
5653 if (comma)
5654 {
5655 len2 = comma - p;
5656 *comma = '\0';
5657 next_p = comma+1;
5658 }
5659 else
5660 {
5661 len2 = end - p;
5662 next_p = NULL;
5663 }
5664
46f8e3b0 5665 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
5666 options. */
5667 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
5668 {
5669 ret = false;
5670 if (attr_p)
5671 warning (OPT_Wattributes,
86a5f91c 5672 "bad option %qs to attribute %<optimize%>", p);
46f8e3b0 5673 else
5674 warning (OPT_Wpragmas,
86a5f91c 5675 "bad option %qs to pragma %<optimize%>", p);
46f8e3b0 5676 continue;
5677 }
5678
f6742350 5679 /* Can't use GC memory here, see PR88007. */
5680 r = q = XOBNEWVEC (&opts_obstack, char, len2 + 3);
5681
46f8e3b0 5682 if (*p != '-')
5683 {
5684 *r++ = '-';
5685
5686 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
5687 itself is -Os, and any other switch begins with a -f. */
5688 if ((*p >= '0' && *p <= '9')
5689 || (p[0] == 's' && p[1] == '\0'))
5690 *r++ = 'O';
5691 else if (*p != 'O')
5692 *r++ = 'f';
5693 }
5694
5695 memcpy (r, p, len2);
5696 r[len2] = '\0';
f1f41a6c 5697 vec_safe_push (optimize_args, (const char *) q);
46f8e3b0 5698 }
5699
5700 }
5701 }
5702
f1f41a6c 5703 opt_argc = optimize_args->length ();
46f8e3b0 5704 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
5705
5706 for (i = 1; i < opt_argc; i++)
f1f41a6c 5707 opt_argv[i] = (*optimize_args)[i];
46f8e3b0 5708
5709 /* Now parse the options. */
f3f006ad 5710 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
5711 &decoded_options,
5712 &decoded_options_count);
b032c4dd 5713 /* Drop non-Optimization options. */
5714 unsigned j = 1;
5715 for (i = 1; i < decoded_options_count; ++i)
5716 {
5717 if (! (cl_options[decoded_options[i].opt_index].flags & CL_OPTIMIZATION))
5718 {
5719 ret = false;
5720 if (attr_p)
5721 warning (OPT_Wattributes,
86a5f91c 5722 "bad option %qs to attribute %<optimize%>",
b032c4dd 5723 decoded_options[i].orig_option_with_args_text);
5724 else
5725 warning (OPT_Wpragmas,
86a5f91c 5726 "bad option %qs to pragma %<optimize%>",
b032c4dd 5727 decoded_options[i].orig_option_with_args_text);
5728 continue;
5729 }
5730 if (i != j)
5731 decoded_options[j] = decoded_options[i];
5732 j++;
5733 }
5734 decoded_options_count = j;
5735 /* And apply them. */
f3f006ad 5736 decode_options (&global_options, &global_options_set,
3c6c0e40 5737 decoded_options, decoded_options_count,
98102386 5738 input_location, global_dc, NULL);
46f8e3b0 5739
4bec06b3 5740 targetm.override_options_after_change();
5741
f1f41a6c 5742 optimize_args->truncate (0);
46f8e3b0 5743 return ret;
5744}
5745
3c77f69c 5746/* Check whether ATTR is a valid attribute fallthrough. */
5747
5748bool
5749attribute_fallthrough_p (tree attr)
5750{
bbdce47f 5751 if (attr == error_mark_node)
5752 return false;
3c77f69c 5753 tree t = lookup_attribute ("fallthrough", attr);
5754 if (t == NULL_TREE)
5755 return false;
5756 /* This attribute shall appear at most once in each attribute-list. */
5757 if (lookup_attribute ("fallthrough", TREE_CHAIN (t)))
5758 warning (OPT_Wattributes, "%<fallthrough%> attribute specified multiple "
5759 "times");
5760 /* No attribute-argument-clause shall be present. */
5761 else if (TREE_VALUE (t) != NULL_TREE)
5762 warning (OPT_Wattributes, "%<fallthrough%> attribute specified with "
5763 "a parameter");
5764 /* Warn if other attributes are found. */
5765 for (t = attr; t != NULL_TREE; t = TREE_CHAIN (t))
5766 {
5767 tree name = get_attribute_name (t);
5768 if (!is_attribute_p ("fallthrough", name))
5769 warning (OPT_Wattributes, "%qE attribute ignored", name);
5770 }
5771 return true;
5772}
5773
dbf6c367 5774\f
774e9d58 5775/* Check for valid arguments being passed to a function with FNTYPE.
75e9093e 5776 There are NARGS arguments in the array ARGARRAY. LOC should be used
5777 for diagnostics. Return true if either -Wnonnull or -Wrestrict has
8e415b30 5778 been issued.
5779
5780 The arguments in ARGARRAY may not have been folded yet (e.g. for C++,
5781 to preserve location wrappers); checks that require folded arguments
5782 should call fold_for_warn on them. */
75e9093e 5783
184fac50 5784bool
bbd5521e 5785check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype,
a115c319 5786 int nargs, tree *argarray, vec<location_t> *arglocs)
dbf6c367 5787{
184fac50 5788 bool warned_p = false;
5789
dbf6c367 5790 /* Check for null being passed in a pointer argument that must be
5791 non-null. We also need to do this if format checking is enabled. */
5792
5793 if (warn_nonnull)
184fac50 5794 warned_p = check_function_nonnull (loc, TYPE_ATTRIBUTES (fntype),
5795 nargs, argarray);
dbf6c367 5796
5797 /* Check for errors in format strings. */
5798
068bea1e 5799 if (warn_format || warn_suggest_attribute_format)
249e7a17 5800 check_function_format (fntype, TYPE_ATTRIBUTES (fntype), nargs, argarray,
5801 arglocs);
95c90e04 5802
5803 if (warn_format)
774e9d58 5804 check_function_sentinel (fntype, nargs, argarray);
bbd5521e 5805
5806 if (warn_restrict)
75e9093e 5807 warned_p |= check_function_restrict (fndecl, fntype, nargs, argarray);
184fac50 5808 return warned_p;
dbf6c367 5809}
5810
5811/* Generic argument checking recursion routine. PARAM is the argument to
5812 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
5813 once the argument is resolved. CTX is context for the callback. */
5814void
1cae46be 5815check_function_arguments_recurse (void (*callback)
5816 (void *, tree, unsigned HOST_WIDE_INT),
5817 void *ctx, tree param,
5818 unsigned HOST_WIDE_INT param_num)
dbf6c367 5819{
72dd6141 5820 if (CONVERT_EXPR_P (param)
c44afe23 5821 && (TYPE_PRECISION (TREE_TYPE (param))
5822 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
dbf6c367 5823 {
5824 /* Strip coercion. */
5825 check_function_arguments_recurse (callback, ctx,
4ee9c684 5826 TREE_OPERAND (param, 0), param_num);
dbf6c367 5827 return;
5828 }
5829
5830 if (TREE_CODE (param) == CALL_EXPR)
5831 {
c2f47e15 5832 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
dbf6c367 5833 tree attrs;
5834 bool found_format_arg = false;
5835
5836 /* See if this is a call to a known internationalization function
5837 that modifies a format arg. Such a function may have multiple
5838 format_arg attributes (for example, ngettext). */
5839
5840 for (attrs = TYPE_ATTRIBUTES (type);
5841 attrs;
5842 attrs = TREE_CHAIN (attrs))
5843 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
5844 {
c2f47e15 5845 tree inner_arg;
dbf6c367 5846 tree format_num_expr;
5847 int format_num;
5848 int i;
c2f47e15 5849 call_expr_arg_iterator iter;
dbf6c367 5850
5851 /* Extract the argument number, which was previously checked
5852 to be valid. */
5853 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
ddb1be65 5854
e913b5cd 5855 format_num = tree_to_uhwi (format_num_expr);
dbf6c367 5856
c2f47e15 5857 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
72749341 5858 inner_arg != NULL_TREE;
c2f47e15 5859 inner_arg = next_call_expr_arg (&iter), i++)
dbf6c367 5860 if (i == format_num)
5861 {
5862 check_function_arguments_recurse (callback, ctx,
c2f47e15 5863 inner_arg, param_num);
dbf6c367 5864 found_format_arg = true;
5865 break;
5866 }
5867 }
5868
5869 /* If we found a format_arg attribute and did a recursive check,
5870 we are done with checking this argument. Otherwise, we continue
5871 and this will be considered a non-literal. */
5872 if (found_format_arg)
5873 return;
5874 }
5875
5876 if (TREE_CODE (param) == COND_EXPR)
5877 {
f6dfb86a 5878 /* Simplify to avoid warning for an impossible case. */
5879 param = fold_for_warn (param);
5880 if (TREE_CODE (param) == COND_EXPR)
5881 {
5882 /* Check both halves of the conditional expression. */
5883 check_function_arguments_recurse (callback, ctx,
5884 TREE_OPERAND (param, 1),
5885 param_num);
5886 check_function_arguments_recurse (callback, ctx,
5887 TREE_OPERAND (param, 2),
5888 param_num);
5889 return;
5890 }
dbf6c367 5891 }
5892
5893 (*callback) (ctx, param, param_num);
5894}
1f3233d1 5895
60cce472 5896/* Checks for a builtin function FNDECL that the number of arguments
5897 NARGS against the required number REQUIRED and issues an error if
5898 there is a mismatch. Returns true if the number of arguments is
5d4db8ef 5899 correct, otherwise false. LOC is the location of FNDECL. */
d43cee80 5900
5901static bool
5d4db8ef 5902builtin_function_validate_nargs (location_t loc, tree fndecl, int nargs,
5903 int required)
d43cee80 5904{
5905 if (nargs < required)
5906 {
23b101c5 5907 error_at (loc, "too few arguments to function %qE", fndecl);
d43cee80 5908 return false;
5909 }
5910 else if (nargs > required)
5911 {
5d4db8ef 5912 error_at (loc, "too many arguments to function %qE", fndecl);
d43cee80 5913 return false;
5914 }
5915 return true;
5916}
5917
5d4db8ef 5918/* Helper macro for check_builtin_function_arguments. */
5919#define ARG_LOCATION(N) \
5920 (arg_loc.is_empty () \
5921 ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
5922 : expansion_point_location (arg_loc[(N)]))
5923
d43cee80 5924/* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
5d4db8ef 5925 Returns false if there was an error, otherwise true. LOC is the
5926 location of the function; ARG_LOC is a vector of locations of the
5927 arguments. */
d43cee80 5928
5929bool
5d4db8ef 5930check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
5931 tree fndecl, int nargs, tree *args)
d43cee80 5932{
a0e9bfbb 5933 if (!fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
d43cee80 5934 return true;
5935
5936 switch (DECL_FUNCTION_CODE (fndecl))
5937 {
2b34677f 5938 case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX:
5939 if (!tree_fits_uhwi_p (args[2]))
5940 {
5941 error_at (ARG_LOCATION (2),
5942 "third argument to function %qE must be a constant integer",
5943 fndecl);
5944 return false;
5945 }
5946 /* fall through */
5947
8c38d887 5948 case BUILT_IN_ALLOCA_WITH_ALIGN:
5949 {
5950 /* Get the requested alignment (in bits) if it's a constant
5951 integer expression. */
5952 unsigned HOST_WIDE_INT align
5953 = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
5954
5955 /* Determine if the requested alignment is a power of 2. */
5956 if ((align & (align - 1)))
5957 align = 0;
5958
5959 /* The maximum alignment in bits corresponding to the same
5960 maximum in bytes enforced in check_user_alignment(). */
5961 unsigned maxalign = (UINT_MAX >> 1) + 1;
5d4db8ef 5962
8c38d887 5963 /* Reject invalid alignments. */
5964 if (align < BITS_PER_UNIT || maxalign < align)
5965 {
5d4db8ef 5966 error_at (ARG_LOCATION (1),
8c38d887 5967 "second argument to function %qE must be a constant "
5968 "integer power of 2 between %qi and %qu bits",
5969 fndecl, BITS_PER_UNIT, maxalign);
5970 return false;
5971 }
5d4db8ef 5972 return true;
8c38d887 5973 }
5974
d43cee80 5975 case BUILT_IN_CONSTANT_P:
5d4db8ef 5976 return builtin_function_validate_nargs (loc, fndecl, nargs, 1);
d43cee80 5977
5978 case BUILT_IN_ISFINITE:
5979 case BUILT_IN_ISINF:
c319d56a 5980 case BUILT_IN_ISINF_SIGN:
d43cee80 5981 case BUILT_IN_ISNAN:
5982 case BUILT_IN_ISNORMAL:
10902624 5983 case BUILT_IN_SIGNBIT:
5d4db8ef 5984 if (builtin_function_validate_nargs (loc, fndecl, nargs, 1))
d43cee80 5985 {
5986 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
5987 {
5d4db8ef 5988 error_at (ARG_LOCATION (0), "non-floating-point argument in "
5989 "call to function %qE", fndecl);
d43cee80 5990 return false;
5991 }
5992 return true;
5993 }
5994 return false;
5995
5996 case BUILT_IN_ISGREATER:
5997 case BUILT_IN_ISGREATEREQUAL:
5998 case BUILT_IN_ISLESS:
5999 case BUILT_IN_ISLESSEQUAL:
6000 case BUILT_IN_ISLESSGREATER:
6001 case BUILT_IN_ISUNORDERED:
5d4db8ef 6002 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2))
d43cee80 6003 {
6004 enum tree_code code0, code1;
6005 code0 = TREE_CODE (TREE_TYPE (args[0]));
6006 code1 = TREE_CODE (TREE_TYPE (args[1]));
6007 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
6008 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
6009 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
6010 {
5d4db8ef 6011 error_at (loc, "non-floating-point arguments in call to "
6012 "function %qE", fndecl);
d43cee80 6013 return false;
6014 }
6015 return true;
6016 }
6017 return false;
6018
19fbe3a4 6019 case BUILT_IN_FPCLASSIFY:
5d4db8ef 6020 if (builtin_function_validate_nargs (loc, fndecl, nargs, 6))
19fbe3a4 6021 {
5d4db8ef 6022 for (unsigned int i = 0; i < 5; i++)
19fbe3a4 6023 if (TREE_CODE (args[i]) != INTEGER_CST)
6024 {
5d4db8ef 6025 error_at (ARG_LOCATION (i), "non-const integer argument %u in "
6026 "call to function %qE", i + 1, fndecl);
19fbe3a4 6027 return false;
6028 }
6029
6030 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
6031 {
5d4db8ef 6032 error_at (ARG_LOCATION (5), "non-floating-point argument in "
6033 "call to function %qE", fndecl);
19fbe3a4 6034 return false;
6035 }
6036 return true;
6037 }
6038 return false;
6039
fca0886c 6040 case BUILT_IN_ASSUME_ALIGNED:
5d4db8ef 6041 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2 + (nargs > 2)))
fca0886c 6042 {
6043 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
6044 {
5d4db8ef 6045 error_at (ARG_LOCATION (2), "non-integer argument 3 in call to "
6046 "function %qE", fndecl);
fca0886c 6047 return false;
6048 }
6049 return true;
6050 }
6051 return false;
6052
0c93c8a9 6053 case BUILT_IN_ADD_OVERFLOW:
6054 case BUILT_IN_SUB_OVERFLOW:
6055 case BUILT_IN_MUL_OVERFLOW:
5d4db8ef 6056 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
0c93c8a9 6057 {
6058 unsigned i;
6059 for (i = 0; i < 2; i++)
6060 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
6061 {
5d4db8ef 6062 error_at (ARG_LOCATION (i), "argument %u in call to function "
6063 "%qE does not have integral type", i + 1, fndecl);
0c93c8a9 6064 return false;
6065 }
6066 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
77104764 6067 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args[2]))))
0c93c8a9 6068 {
5d4db8ef 6069 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
77104764 6070 "does not have pointer to integral type", fndecl);
6071 return false;
6072 }
6073 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == ENUMERAL_TYPE)
6074 {
6075 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
6076 "has pointer to enumerated type", fndecl);
6077 return false;
6078 }
6079 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == BOOLEAN_TYPE)
6080 {
6081 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
6082 "has pointer to boolean type", fndecl);
0c93c8a9 6083 return false;
6084 }
6085 return true;
6086 }
6087 return false;
6088
732905bb 6089 case BUILT_IN_ADD_OVERFLOW_P:
6090 case BUILT_IN_SUB_OVERFLOW_P:
6091 case BUILT_IN_MUL_OVERFLOW_P:
6092 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
6093 {
6094 unsigned i;
6095 for (i = 0; i < 3; i++)
6096 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
6097 {
6098 error_at (ARG_LOCATION (i), "argument %u in call to function "
6099 "%qE does not have integral type", i + 1, fndecl);
6100 return false;
6101 }
77104764 6102 if (TREE_CODE (TREE_TYPE (args[2])) == ENUMERAL_TYPE)
6103 {
6104 error_at (ARG_LOCATION (2), "argument 3 in call to function "
6105 "%qE has enumerated type", fndecl);
6106 return false;
6107 }
6108 else if (TREE_CODE (TREE_TYPE (args[2])) == BOOLEAN_TYPE)
6109 {
6110 error_at (ARG_LOCATION (2), "argument 3 in call to function "
6111 "%qE has boolean type", fndecl);
6112 return false;
6113 }
732905bb 6114 return true;
6115 }
6116 return false;
6117
d43cee80 6118 default:
6119 return true;
6120 }
6121}
6122
209c9752 6123/* Subroutine of c_parse_error.
6124 Return the result of concatenating LHS and RHS. RHS is really
6125 a string literal, its first character is indicated by RHS_START and
cfee01e3 6126 RHS_SIZE is its length (including the terminating NUL character).
209c9752 6127
6128 The caller is responsible for deleting the returned pointer. */
6129
6130static char *
6131catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
6132{
d8aad786 6133 const size_t lhs_size = strlen (lhs);
209c9752 6134 char *result = XNEWVEC (char, lhs_size + rhs_size);
d8aad786 6135 memcpy (result, lhs, lhs_size);
6136 memcpy (result + lhs_size, rhs_start, rhs_size);
209c9752 6137 return result;
6138}
6139
3fe34694 6140/* Issue the error given by GMSGID at RICHLOC, indicating that it occurred
6141 before TOKEN, which had the associated VALUE. */
92b128ed 6142
6143void
48e1416a 6144c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
3fe34694 6145 tree value, unsigned char token_flags,
6146 rich_location *richloc)
92b128ed 6147{
209c9752 6148#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
6149
6150 char *message = NULL;
92b128ed 6151
ba99525e 6152 if (token_type == CPP_EOF)
380c6697 6153 message = catenate_messages (gmsgid, " at end of input");
48e1416a 6154 else if (token_type == CPP_CHAR
6155 || token_type == CPP_WCHAR
ba99525e 6156 || token_type == CPP_CHAR16
30b1ba42 6157 || token_type == CPP_CHAR32
6158 || token_type == CPP_UTF8CHAR)
92b128ed 6159 {
f9ae6f95 6160 unsigned int val = TREE_INT_CST_LOW (value);
924bbf02 6161 const char *prefix;
6162
ba99525e 6163 switch (token_type)
924bbf02 6164 {
6165 default:
6166 prefix = "";
6167 break;
6168 case CPP_WCHAR:
6169 prefix = "L";
6170 break;
6171 case CPP_CHAR16:
6172 prefix = "u";
6173 break;
6174 case CPP_CHAR32:
6175 prefix = "U";
6176 break;
30b1ba42 6177 case CPP_UTF8CHAR:
6178 prefix = "u8";
6179 break;
924bbf02 6180 }
6181
92b128ed 6182 if (val <= UCHAR_MAX && ISGRAPH (val))
a0c938f0 6183 message = catenate_messages (gmsgid, " before %s'%c'");
92b128ed 6184 else
a0c938f0 6185 message = catenate_messages (gmsgid, " before %s'\\x%x'");
209c9752 6186
3b6578b3 6187 error_at (richloc, message, prefix, val);
209c9752 6188 free (message);
6189 message = NULL;
92b128ed 6190 }
1898176c 6191 else if (token_type == CPP_CHAR_USERDEF
6192 || token_type == CPP_WCHAR_USERDEF
6193 || token_type == CPP_CHAR16_USERDEF
30b1ba42 6194 || token_type == CPP_CHAR32_USERDEF
6195 || token_type == CPP_UTF8CHAR_USERDEF)
1898176c 6196 message = catenate_messages (gmsgid,
6197 " before user-defined character literal");
6198 else if (token_type == CPP_STRING_USERDEF
6199 || token_type == CPP_WSTRING_USERDEF
6200 || token_type == CPP_STRING16_USERDEF
6201 || token_type == CPP_STRING32_USERDEF
6202 || token_type == CPP_UTF8STRING_USERDEF)
6203 message = catenate_messages (gmsgid, " before user-defined string literal");
48e1416a 6204 else if (token_type == CPP_STRING
6205 || token_type == CPP_WSTRING
ba99525e 6206 || token_type == CPP_STRING16
538ba11a 6207 || token_type == CPP_STRING32
6208 || token_type == CPP_UTF8STRING)
380c6697 6209 message = catenate_messages (gmsgid, " before string constant");
ba99525e 6210 else if (token_type == CPP_NUMBER)
380c6697 6211 message = catenate_messages (gmsgid, " before numeric constant");
ba99525e 6212 else if (token_type == CPP_NAME)
209c9752 6213 {
380c6697 6214 message = catenate_messages (gmsgid, " before %qE");
3b6578b3 6215 error_at (richloc, message, value);
209c9752 6216 free (message);
6217 message = NULL;
6218 }
ba99525e 6219 else if (token_type == CPP_PRAGMA)
b75b98aa 6220 message = catenate_messages (gmsgid, " before %<#pragma%>");
ba99525e 6221 else if (token_type == CPP_PRAGMA_EOL)
b75b98aa 6222 message = catenate_messages (gmsgid, " before end of line");
07b8f133 6223 else if (token_type == CPP_DECLTYPE)
6224 message = catenate_messages (gmsgid, " before %<decltype%>");
ba99525e 6225 else if (token_type < N_TTYPES)
209c9752 6226 {
380c6697 6227 message = catenate_messages (gmsgid, " before %qs token");
3b6578b3 6228 error_at (richloc, message, cpp_type2name (token_type, token_flags));
209c9752 6229 free (message);
6230 message = NULL;
6231 }
92b128ed 6232 else
3b6578b3 6233 error_at (richloc, gmsgid);
209c9752 6234
6235 if (message)
6236 {
3b6578b3 6237 error_at (richloc, message);
209c9752 6238 free (message);
6239 }
a0c938f0 6240#undef catenate_messages
92b128ed 6241}
6242
3a79f5da 6243/* Return the gcc option code associated with the reason for a cpp
6244 message, or 0 if none. */
6245
6246static int
fb225cf1 6247c_option_controlling_cpp_diagnostic (enum cpp_warning_reason reason)
3a79f5da 6248{
7ff8db31 6249 const struct cpp_reason_option_codes_t *entry;
3a79f5da 6250
7ff8db31 6251 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
3a79f5da 6252 {
6253 if (entry->reason == reason)
6254 return entry->option_code;
6255 }
6256 return 0;
6257}
6258
fb225cf1 6259/* Callback from cpp_diagnostic for PFILE to print diagnostics from the
3a79f5da 6260 preprocessor. The diagnostic is of type LEVEL, with REASON set
6261 to the reason code if LEVEL is represents a warning, at location
f0479000 6262 RICHLOC unless this is after lexing and the compiler's location
6263 should be used instead; MSG is the translated message and AP
7f5f3953 6264 the arguments. Returns true if a diagnostic was emitted, false
6265 otherwise. */
6266
6267bool
fb225cf1 6268c_cpp_diagnostic (cpp_reader *pfile ATTRIBUTE_UNUSED,
6269 enum cpp_diagnostic_level level,
6270 enum cpp_warning_reason reason,
6271 rich_location *richloc,
6272 const char *msg, va_list *ap)
7f5f3953 6273{
6274 diagnostic_info diagnostic;
6275 diagnostic_t dlevel;
5ae82d58 6276 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
7f5f3953 6277 bool ret;
6278
6279 switch (level)
6280 {
6281 case CPP_DL_WARNING_SYSHDR:
6282 if (flag_no_output)
6283 return false;
5ae82d58 6284 global_dc->dc_warn_system_headers = 1;
7f5f3953 6285 /* Fall through. */
6286 case CPP_DL_WARNING:
6287 if (flag_no_output)
6288 return false;
6289 dlevel = DK_WARNING;
6290 break;
6291 case CPP_DL_PEDWARN:
6292 if (flag_no_output && !flag_pedantic_errors)
6293 return false;
6294 dlevel = DK_PEDWARN;
6295 break;
6296 case CPP_DL_ERROR:
6297 dlevel = DK_ERROR;
6298 break;
6299 case CPP_DL_ICE:
6300 dlevel = DK_ICE;
6301 break;
6302 case CPP_DL_NOTE:
6303 dlevel = DK_NOTE;
6304 break;
ff903809 6305 case CPP_DL_FATAL:
6306 dlevel = DK_FATAL;
6307 break;
7f5f3953 6308 default:
6309 gcc_unreachable ();
6310 }
6311 if (done_lexing)
5fe20025 6312 richloc->set_range (0, input_location, SHOW_RANGE_WITH_CARET);
7f5f3953 6313 diagnostic_set_info_translated (&diagnostic, msg, ap,
f0479000 6314 richloc, dlevel);
fb225cf1 6315 diagnostic_override_option_index
6316 (&diagnostic,
6317 c_option_controlling_cpp_diagnostic (reason));
56b8400f 6318 ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
7f5f3953 6319 if (level == CPP_DL_WARNING_SYSHDR)
5ae82d58 6320 global_dc->dc_warn_system_headers = save_warn_system_headers;
7f5f3953 6321 return ret;
6322}
6323
624d37a6 6324/* Convert a character from the host to the target execution character
6325 set. cpplib handles this, mostly. */
6326
6327HOST_WIDE_INT
6328c_common_to_target_charset (HOST_WIDE_INT c)
6329{
6330 /* Character constants in GCC proper are sign-extended under -fsigned-char,
6331 zero-extended under -fno-signed-char. cpplib insists that characters
6332 and character constants are always unsigned. Hence we must convert
6333 back and forth. */
6334 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
6335
6336 uc = cpp_host_to_exec_charset (parse_in, uc);
6337
6338 if (flag_signed_char)
6339 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
6340 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
6341 else
6342 return uc;
6343}
6344
7549df0d 6345/* Fold an offsetof-like expression. EXPR is a nested sequence of component
6346 references with an INDIRECT_REF of a constant at the bottom; much like the
3c43ed34 6347 traditional rendering of offsetof as a macro. TYPE is the desired type of
6348 the whole expression. Return the folded result. */
af28855b 6349
7549df0d 6350tree
3c43ed34 6351fold_offsetof (tree expr, tree type, enum tree_code ctx)
af28855b 6352{
af28855b 6353 tree base, off, t;
aeaccb75 6354 tree_code code = TREE_CODE (expr);
6355 switch (code)
af28855b 6356 {
6357 case ERROR_MARK:
6358 return expr;
6359
6b11d2e3 6360 case VAR_DECL:
6361 error ("cannot apply %<offsetof%> to static data member %qD", expr);
6362 return error_mark_node;
6363
d897f7c2 6364 case CALL_EXPR:
cf1a89a3 6365 case TARGET_EXPR:
d897f7c2 6366 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
6367 return error_mark_node;
6368
d897f7c2 6369 case NOP_EXPR:
6370 case INDIRECT_REF:
7549df0d 6371 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
64ed018c 6372 {
6373 error ("cannot apply %<offsetof%> to a non constant address");
6374 return error_mark_node;
6375 }
3c43ed34 6376 return convert (type, TREE_OPERAND (expr, 0));
d897f7c2 6377
af28855b 6378 case COMPONENT_REF:
3c43ed34 6379 base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
af28855b 6380 if (base == error_mark_node)
6381 return base;
6382
6383 t = TREE_OPERAND (expr, 1);
6384 if (DECL_C_BIT_FIELD (t))
6385 {
6386 error ("attempt to take address of bit-field structure "
782858b8 6387 "member %qD", t);
af28855b 6388 return error_mark_node;
6389 }
389dd41b 6390 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
e913b5cd 6391 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
389dd41b 6392 / BITS_PER_UNIT));
af28855b 6393 break;
6394
6395 case ARRAY_REF:
3c43ed34 6396 base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
af28855b 6397 if (base == error_mark_node)
6398 return base;
6399
6400 t = TREE_OPERAND (expr, 1);
d582d140 6401 STRIP_ANY_LOCATION_WRAPPER (t);
64ed018c 6402
6403 /* Check if the offset goes beyond the upper bound of the array. */
7549df0d 6404 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
e0559d69 6405 {
6406 tree upbound = array_ref_up_bound (expr);
6407 if (upbound != NULL_TREE
6408 && TREE_CODE (upbound) == INTEGER_CST
6409 && !tree_int_cst_equal (upbound,
6410 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
6411 {
aeaccb75 6412 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
6413 upbound = size_binop (PLUS_EXPR, upbound,
6414 build_int_cst (TREE_TYPE (upbound), 1));
e0559d69 6415 if (tree_int_cst_lt (upbound, t))
6416 {
6417 tree v;
6418
6419 for (v = TREE_OPERAND (expr, 0);
6420 TREE_CODE (v) == COMPONENT_REF;
6421 v = TREE_OPERAND (v, 0))
6422 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
6423 == RECORD_TYPE)
6424 {
1767a056 6425 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
6426 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
e0559d69 6427 if (TREE_CODE (fld_chain) == FIELD_DECL)
6428 break;
6429
6430 if (fld_chain)
6431 break;
6432 }
6433 /* Don't warn if the array might be considered a poor
6434 man's flexible array member with a very permissive
6435 definition thereof. */
6436 if (TREE_CODE (v) == ARRAY_REF
6437 || TREE_CODE (v) == COMPONENT_REF)
6438 warning (OPT_Warray_bounds,
6439 "index %E denotes an offset "
6440 "greater than size of %qT",
6441 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
6442 }
6443 }
6444 }
7549df0d 6445
6446 t = convert (sizetype, t);
6447 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
af28855b 6448 break;
6449
ede90cc2 6450 case COMPOUND_EXPR:
6451 /* Handle static members of volatile structs. */
6452 t = TREE_OPERAND (expr, 1);
45685ee6 6453 gcc_checking_assert (VAR_P (get_base_address (t)));
3c43ed34 6454 return fold_offsetof (t, type);
ede90cc2 6455
af28855b 6456 default:
231bd014 6457 gcc_unreachable ();
af28855b 6458 }
6459
3c43ed34 6460 if (!POINTER_TYPE_P (type))
6461 return size_binop (PLUS_EXPR, base, convert (type, off));
7549df0d 6462 return fold_build_pointer_plus (base, off);
af28855b 6463}
c271bdb2 6464\f
6465/* *PTYPE is an incomplete array. Complete it with a domain based on
6466 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
6467 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6468 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
6469
6470int
6471complete_array_type (tree *ptype, tree initial_value, bool do_default)
6472{
6473 tree maxindex, type, main_type, elt, unqual_elt;
6474 int failure = 0, quals;
f5298614 6475 bool overflow_p = false;
c271bdb2 6476
6477 maxindex = size_zero_node;
6478 if (initial_value)
6479 {
d582d140 6480 STRIP_ANY_LOCATION_WRAPPER (initial_value);
6481
c271bdb2 6482 if (TREE_CODE (initial_value) == STRING_CST)
6483 {
6484 int eltsize
6485 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
6486 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
6487 }
6488 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
6489 {
f1f41a6c 6490 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
c271bdb2 6491
f1f41a6c 6492 if (vec_safe_is_empty (v))
c271bdb2 6493 {
6494 if (pedantic)
6495 failure = 3;
7542c3b4 6496 maxindex = ssize_int (-1);
c271bdb2 6497 }
6498 else
6499 {
6500 tree curindex;
c75b4594 6501 unsigned HOST_WIDE_INT cnt;
6502 constructor_elt *ce;
cee43f7e 6503 bool fold_p = false;
c271bdb2 6504
f1f41a6c 6505 if ((*v)[0].index)
f5298614 6506 maxindex = (*v)[0].index, fold_p = true;
6507
c271bdb2 6508 curindex = maxindex;
6509
f1f41a6c 6510 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
c271bdb2 6511 {
cee43f7e 6512 bool curfold_p = false;
c75b4594 6513 if (ce->index)
cee43f7e 6514 curindex = ce->index, curfold_p = true;
c271bdb2 6515 else
cee43f7e 6516 {
6517 if (fold_p)
f5298614 6518 {
6519 /* Since we treat size types now as ordinary
6520 unsigned types, we need an explicit overflow
6521 check. */
6522 tree orig = curindex;
6523 curindex = fold_convert (sizetype, curindex);
6524 overflow_p |= tree_int_cst_lt (curindex, orig);
6525 }
389dd41b 6526 curindex = size_binop (PLUS_EXPR, curindex,
6527 size_one_node);
cee43f7e 6528 }
c271bdb2 6529 if (tree_int_cst_lt (maxindex, curindex))
cee43f7e 6530 maxindex = curindex, fold_p = curfold_p;
c271bdb2 6531 }
f5298614 6532 if (fold_p)
6533 {
6534 tree orig = maxindex;
6535 maxindex = fold_convert (sizetype, maxindex);
6536 overflow_p |= tree_int_cst_lt (maxindex, orig);
6537 }
c271bdb2 6538 }
6539 }
6540 else
6541 {
6542 /* Make an error message unless that happened already. */
6543 if (initial_value != error_mark_node)
6544 failure = 1;
6545 }
6546 }
6547 else
6548 {
6549 failure = 2;
6550 if (!do_default)
6551 return failure;
6552 }
6553
6554 type = *ptype;
6555 elt = TREE_TYPE (type);
6556 quals = TYPE_QUALS (strip_array_types (elt));
6557 if (quals == 0)
6558 unqual_elt = elt;
6559 else
6d5d708e 6560 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
c271bdb2 6561
6562 /* Using build_distinct_type_copy and modifying things afterward instead
6563 of using build_array_type to create a new type preserves all of the
6564 TYPE_LANG_FLAG_? bits that the front end may have set. */
6565 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6566 TREE_TYPE (main_type) = unqual_elt;
783bb57e 6567 TYPE_DOMAIN (main_type)
6568 = build_range_type (TREE_TYPE (maxindex),
6569 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
6bac87c3 6570 TYPE_TYPELESS_STORAGE (main_type) = TYPE_TYPELESS_STORAGE (type);
c271bdb2 6571 layout_type (main_type);
6572
6753bca0 6573 /* Make sure we have the canonical MAIN_TYPE. */
a06321ef 6574 hashval_t hashcode = type_hash_canon_hash (main_type);
6575 main_type = type_hash_canon (hashcode, main_type);
6753bca0 6576
796735dc 6577 /* Fix the canonical type. */
6578 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
6579 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
6580 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
6581 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
6582 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
6583 != TYPE_DOMAIN (main_type)))
48e1416a 6584 TYPE_CANONICAL (main_type)
796735dc 6585 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
6bac87c3 6586 TYPE_CANONICAL (TYPE_DOMAIN (main_type)),
6587 TYPE_TYPELESS_STORAGE (main_type));
796735dc 6588 else
6589 TYPE_CANONICAL (main_type) = main_type;
6590
c271bdb2 6591 if (quals == 0)
6592 type = main_type;
6593 else
6594 type = c_build_qualified_type (main_type, quals);
6595
4f5b8f2a 6596 if (COMPLETE_TYPE_P (type)
6597 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
f5298614 6598 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
4f5b8f2a 6599 {
6600 error ("size of array is too large");
6601 /* If we proceed with the array type as it is, we'll eventually
08f817b3 6602 crash in tree_to_[su]hwi(). */
4f5b8f2a 6603 type = error_mark_node;
6604 }
6605
c271bdb2 6606 *ptype = type;
6607 return failure;
6608}
ab6bb714 6609
df06d247 6610/* INIT is an constructor of a structure with a flexible array member.
6611 Complete the flexible array member with a domain based on it's value. */
6612void
6613complete_flexible_array_elts (tree init)
6614{
6615 tree elt, type;
6616
6617 if (init == NULL_TREE || TREE_CODE (init) != CONSTRUCTOR)
6618 return;
6619
6620 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
6621 return;
6622
6623 elt = CONSTRUCTOR_ELTS (init)->last ().value;
6624 type = TREE_TYPE (elt);
6625 if (TREE_CODE (type) == ARRAY_TYPE
6626 && TYPE_SIZE (type) == NULL_TREE)
6627 complete_array_type (&TREE_TYPE (elt), elt, false);
6628 else
6629 complete_flexible_array_elts (elt);
6630}
6631
93426222 6632/* Like c_mark_addressable but don't check register qualifier. */
6633void
6634c_common_mark_addressable_vec (tree t)
6635{
b977a7e3 6636 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
6637 t = C_MAYBE_CONST_EXPR_EXPR (t);
93426222 6638 while (handled_component_p (t))
6639 t = TREE_OPERAND (t, 0);
4c04bcce 6640 if (!VAR_P (t)
6641 && TREE_CODE (t) != PARM_DECL
6642 && TREE_CODE (t) != COMPOUND_LITERAL_EXPR)
93426222 6643 return;
3385a573 6644 if (!VAR_P (t) || !DECL_HARD_REGISTER (t))
6645 TREE_ADDRESSABLE (t) = 1;
93426222 6646}
6647
6648
b6a5fc45 6649\f
6650/* Used to help initialize the builtin-types.def table. When a type of
6651 the correct size doesn't exist, use error_mark_node instead of NULL.
6652 The later results in segfaults even when a decl using the type doesn't
6653 get invoked. */
6654
6655tree
6656builtin_type_for_size (int size, bool unsignedp)
6657{
a51edb4c 6658 tree type = c_common_type_for_size (size, unsignedp);
b6a5fc45 6659 return type ? type : error_mark_node;
6660}
6661
123081ef 6662/* Work out the size of the first argument of a call to
6663 __builtin_speculation_safe_value. Only pointers and integral types
6664 are permitted. Return -1 if the argument type is not supported or
6665 the size is too large; 0 if the argument type is a pointer or the
6666 size if it is integral. */
6667static enum built_in_function
6668speculation_safe_value_resolve_call (tree function, vec<tree, va_gc> *params)
6669{
6670 /* Type of the argument. */
6671 tree type;
6672 int size;
6673
6674 if (vec_safe_is_empty (params))
6675 {
6676 error ("too few arguments to function %qE", function);
6677 return BUILT_IN_NONE;
6678 }
6679
6680 type = TREE_TYPE ((*params)[0]);
6681 if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
6682 {
6683 /* Force array-to-pointer decay for C++. */
6684 (*params)[0] = default_conversion ((*params)[0]);
6685 type = TREE_TYPE ((*params)[0]);
6686 }
6687
6688 if (POINTER_TYPE_P (type))
6689 return BUILT_IN_SPECULATION_SAFE_VALUE_PTR;
6690
6691 if (!INTEGRAL_TYPE_P (type))
6692 goto incompatible;
6693
6694 if (!COMPLETE_TYPE_P (type))
6695 goto incompatible;
6696
6697 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
6698 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
6699 return ((enum built_in_function)
6700 ((int) BUILT_IN_SPECULATION_SAFE_VALUE_1 + exact_log2 (size)));
6701
6702 incompatible:
6703 /* Issue the diagnostic only if the argument is valid, otherwise
6704 it would be redundant at best and could be misleading. */
6705 if (type != error_mark_node)
6706 error ("operand type %qT is incompatible with argument %d of %qE",
6707 type, 1, function);
6708
6709 return BUILT_IN_NONE;
6710}
6711
6712/* Validate and coerce PARAMS, the arguments to ORIG_FUNCTION to fit
6713 the prototype for FUNCTION. The first argument is mandatory, a second
6714 argument, if present, must be type compatible with the first. */
6715static bool
6716speculation_safe_value_resolve_params (location_t loc, tree orig_function,
6717 vec<tree, va_gc> *params)
6718{
6719 tree val;
6720
6721 if (params->length () == 0)
6722 {
6723 error_at (loc, "too few arguments to function %qE", orig_function);
6724 return false;
6725 }
6726
6727 else if (params->length () > 2)
6728 {
6729 error_at (loc, "too many arguments to function %qE", orig_function);
6730 return false;
6731 }
6732
6733 val = (*params)[0];
6734 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE)
6735 val = default_conversion (val);
6736 if (!(TREE_CODE (TREE_TYPE (val)) == POINTER_TYPE
6737 || TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE))
6738 {
6739 error_at (loc,
6740 "expecting argument of type pointer or of type integer "
6741 "for argument 1");
6742 return false;
6743 }
6744 (*params)[0] = val;
6745
6746 if (params->length () == 2)
6747 {
6748 tree val2 = (*params)[1];
6749 if (TREE_CODE (TREE_TYPE (val2)) == ARRAY_TYPE)
6750 val2 = default_conversion (val2);
6751 if (!(TREE_TYPE (val) == TREE_TYPE (val2)
6752 || useless_type_conversion_p (TREE_TYPE (val), TREE_TYPE (val2))))
6753 {
6754 error_at (loc, "both arguments must be compatible");
6755 return false;
6756 }
6757 (*params)[1] = val2;
6758 }
6759
6760 return true;
6761}
6762
6763/* Cast the result of the builtin back to the type of the first argument,
6764 preserving any qualifiers that it might have. */
6765static tree
6766speculation_safe_value_resolve_return (tree first_param, tree result)
6767{
6768 tree ptype = TREE_TYPE (first_param);
6769 tree rtype = TREE_TYPE (result);
6770 ptype = TYPE_MAIN_VARIANT (ptype);
6771
6772 if (tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
6773 return convert (ptype, result);
6774
6775 return result;
6776}
6777
b6a5fc45 6778/* A helper function for resolve_overloaded_builtin in resolving the
6779 overloaded __sync_ builtins. Returns a positive power of 2 if the
6780 first operand of PARAMS is a pointer to a supported data type.
517906ff 6781 Returns 0 if an error is encountered.
6782 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
6783 built-ins. */
b6a5fc45 6784
6785static int
517906ff 6786sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch)
b6a5fc45 6787{
517906ff 6788 /* Type of the argument. */
6789 tree argtype;
6790 /* Type the argument points to. */
b6a5fc45 6791 tree type;
6792 int size;
6793
77454e6e 6794 if (vec_safe_is_empty (params))
b6a5fc45 6795 {
6796 error ("too few arguments to function %qE", function);
6797 return 0;
6798 }
6799
517906ff 6800 argtype = type = TREE_TYPE ((*params)[0]);
e69fccfa 6801 if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
0d284870 6802 {
6803 /* Force array-to-pointer decay for C++. */
0d284870 6804 (*params)[0] = default_conversion ((*params)[0]);
6805 type = TREE_TYPE ((*params)[0]);
6806 }
b6a5fc45 6807 if (TREE_CODE (type) != POINTER_TYPE)
6808 goto incompatible;
6809
6810 type = TREE_TYPE (type);
6811 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
6812 goto incompatible;
6813
c18b3f9b 6814 if (!COMPLETE_TYPE_P (type))
6815 goto incompatible;
6816
517906ff 6817 if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
6818 goto incompatible;
6819
e913b5cd 6820 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
27213ba3 6821 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
b6a5fc45 6822 return size;
6823
6824 incompatible:
bbbbe8ab 6825 /* Issue the diagnostic only if the argument is valid, otherwise
6826 it would be redundant at best and could be misleading. */
6827 if (argtype != error_mark_node)
6828 error ("operand type %qT is incompatible with argument %d of %qE",
6829 argtype, 1, function);
b6a5fc45 6830 return 0;
6831}
6832
a0c938f0 6833/* A helper function for resolve_overloaded_builtin. Adds casts to
b6a5fc45 6834 PARAMS to make arguments match up with those of FUNCTION. Drops
6835 the variadic arguments at the end. Returns false if some error
6836 was encountered; true on success. */
6837
6838static bool
1cd6e20d 6839sync_resolve_params (location_t loc, tree orig_function, tree function,
f1f41a6c 6840 vec<tree, va_gc> *params, bool orig_format)
b6a5fc45 6841{
d0af78c5 6842 function_args_iterator iter;
b6a5fc45 6843 tree ptype;
b9c74b4d 6844 unsigned int parmnum;
b6a5fc45 6845
d0af78c5 6846 function_args_iter_init (&iter, TREE_TYPE (function));
b6a5fc45 6847 /* We've declared the implementation functions to use "volatile void *"
6848 as the pointer parameter, so we shouldn't get any complaints from the
6849 call to check_function_arguments what ever type the user used. */
d0af78c5 6850 function_args_iter_next (&iter);
f1f41a6c 6851 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
b560fabd 6852 ptype = TYPE_MAIN_VARIANT (ptype);
b6a5fc45 6853
6854 /* For the rest of the values, we need to cast these to FTYPE, so that we
6855 don't get warnings for passing pointer types, etc. */
b9c74b4d 6856 parmnum = 0;
d0af78c5 6857 while (1)
b6a5fc45 6858 {
d0af78c5 6859 tree val, arg_type;
6860
6861 arg_type = function_args_iter_cond (&iter);
6862 /* XXX void_type_node belies the abstraction. */
6863 if (arg_type == void_type_node)
6864 break;
b6a5fc45 6865
b9c74b4d 6866 ++parmnum;
f1f41a6c 6867 if (params->length () <= parmnum)
b6a5fc45 6868 {
1cd6e20d 6869 error_at (loc, "too few arguments to function %qE", orig_function);
b6a5fc45 6870 return false;
6871 }
6872
0f6a7cb7 6873 /* Only convert parameters if arg_type is unsigned integer type with
6874 new format sync routines, i.e. don't attempt to convert pointer
6875 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
6876 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
6877 kinds). */
6878 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
1cd6e20d 6879 {
6880 /* Ideally for the first conversion we'd use convert_for_assignment
6881 so that we get warnings for anything that doesn't match the pointer
6882 type. This isn't portable across the C and C++ front ends atm. */
f1f41a6c 6883 val = (*params)[parmnum];
1cd6e20d 6884 val = convert (ptype, val);
6885 val = convert (arg_type, val);
f1f41a6c 6886 (*params)[parmnum] = val;
1cd6e20d 6887 }
b6a5fc45 6888
d0af78c5 6889 function_args_iter_next (&iter);
b6a5fc45 6890 }
6891
1cd6e20d 6892 /* __atomic routines are not variadic. */
f1f41a6c 6893 if (!orig_format && params->length () != parmnum + 1)
1cd6e20d 6894 {
6895 error_at (loc, "too many arguments to function %qE", orig_function);
6896 return false;
6897 }
6898
b6a5fc45 6899 /* The definition of these primitives is variadic, with the remaining
6900 being "an optional list of variables protected by the memory barrier".
6901 No clue what that's supposed to mean, precisely, but we consider all
6902 call-clobbered variables to be protected so we're safe. */
f1f41a6c 6903 params->truncate (parmnum + 1);
b6a5fc45 6904
6905 return true;
6906}
6907
a0c938f0 6908/* A helper function for resolve_overloaded_builtin. Adds a cast to
b6a5fc45 6909 RESULT to make it match the type of the first pointer argument in
6910 PARAMS. */
6911
6912static tree
1cd6e20d 6913sync_resolve_return (tree first_param, tree result, bool orig_format)
b6a5fc45 6914{
b9c74b4d 6915 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
1cd6e20d 6916 tree rtype = TREE_TYPE (result);
10080eac 6917 ptype = TYPE_MAIN_VARIANT (ptype);
1cd6e20d 6918
6919 /* New format doesn't require casting unless the types are the same size. */
6920 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
6921 return convert (ptype, result);
6922 else
6923 return result;
6924}
6925
6926/* This function verifies the PARAMS to generic atomic FUNCTION.
6927 It returns the size if all the parameters are the same size, otherwise
6928 0 is returned if the parameters are invalid. */
6929
6930static int
f1f41a6c 6931get_atomic_generic_size (location_t loc, tree function,
6932 vec<tree, va_gc> *params)
1cd6e20d 6933{
6934 unsigned int n_param;
6935 unsigned int n_model;
6936 unsigned int x;
6937 int size_0;
6938 tree type_0;
6939
6940 /* Determine the parameter makeup. */
6941 switch (DECL_FUNCTION_CODE (function))
6942 {
6943 case BUILT_IN_ATOMIC_EXCHANGE:
6944 n_param = 4;
6945 n_model = 1;
6946 break;
6947 case BUILT_IN_ATOMIC_LOAD:
6948 case BUILT_IN_ATOMIC_STORE:
6949 n_param = 3;
6950 n_model = 1;
6951 break;
6952 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
6953 n_param = 6;
6954 n_model = 2;
6955 break;
6956 default:
1d581089 6957 gcc_unreachable ();
1cd6e20d 6958 }
6959
f1f41a6c 6960 if (vec_safe_length (params) != n_param)
1cd6e20d 6961 {
6962 error_at (loc, "incorrect number of arguments to function %qE", function);
6963 return 0;
6964 }
6965
6966 /* Get type of first parameter, and determine its size. */
f1f41a6c 6967 type_0 = TREE_TYPE ((*params)[0]);
e69fccfa 6968 if (TREE_CODE (type_0) == ARRAY_TYPE && c_dialect_cxx ())
0d284870 6969 {
6970 /* Force array-to-pointer decay for C++. */
0d284870 6971 (*params)[0] = default_conversion ((*params)[0]);
6972 type_0 = TREE_TYPE ((*params)[0]);
6973 }
1d581089 6974 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
6975 {
6976 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
6977 function);
6978 return 0;
6979 }
6980
6981 /* Types must be compile time constant sizes. */
6982 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
1cd6e20d 6983 {
1d581089 6984 error_at (loc,
6985 "argument 1 of %qE must be a pointer to a constant size type",
6986 function);
1cd6e20d 6987 return 0;
6988 }
1d581089 6989
e913b5cd 6990 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
1cd6e20d 6991
1d581089 6992 /* Zero size objects are not allowed. */
6993 if (size_0 == 0)
6994 {
6995 error_at (loc,
6996 "argument 1 of %qE must be a pointer to a nonzero size object",
6997 function);
6998 return 0;
6999 }
7000
1cd6e20d 7001 /* Check each other parameter is a pointer and the same size. */
7002 for (x = 0; x < n_param - n_model; x++)
7003 {
7004 int size;
f1f41a6c 7005 tree type = TREE_TYPE ((*params)[x]);
a04e8d62 7006 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
1cd6e20d 7007 if (n_param == 6 && x == 3)
7008 continue;
e69fccfa 7009 if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
7010 {
7011 /* Force array-to-pointer decay for C++. */
7012 (*params)[x] = default_conversion ((*params)[x]);
7013 type = TREE_TYPE ((*params)[x]);
7014 }
1cd6e20d 7015 if (!POINTER_TYPE_P (type))
7016 {
7017 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
7018 function);
7019 return 0;
7020 }
26040f06 7021 else if (TYPE_SIZE_UNIT (TREE_TYPE (type))
7022 && TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type))))
7023 != INTEGER_CST)
7024 {
7025 error_at (loc, "argument %d of %qE must be a pointer to a constant "
7026 "size type", x + 1, function);
7027 return 0;
7028 }
7029 else if (FUNCTION_POINTER_TYPE_P (type))
7030 {
7031 error_at (loc, "argument %d of %qE must not be a pointer to a "
7032 "function", x + 1, function);
7033 return 0;
7034 }
1f6be080 7035 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
7036 size = type_size ? tree_to_uhwi (type_size) : 0;
1cd6e20d 7037 if (size != size_0)
7038 {
7039 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
7040 function);
7041 return 0;
7042 }
7043 }
7044
7045 /* Check memory model parameters for validity. */
7046 for (x = n_param - n_model ; x < n_param; x++)
7047 {
f1f41a6c 7048 tree p = (*params)[x];
d76863c8 7049 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
7050 {
7051 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
7052 function);
7053 return 0;
7054 }
7055 p = fold_for_warn (p);
1cd6e20d 7056 if (TREE_CODE (p) == INTEGER_CST)
d76863c8 7057 {
72f46c23 7058 /* memmodel_base masks the low 16 bits, thus ignore any bits above
7059 it by using TREE_INT_CST_LOW instead of tree_to_*hwi. Those high
7060 bits will be checked later during expansion in target specific
7061 way. */
7062 if (memmodel_base (TREE_INT_CST_LOW (p)) >= MEMMODEL_LAST)
7063 warning_at (loc, OPT_Winvalid_memory_model,
7064 "invalid memory model argument %d of %qE", x + 1,
7065 function);
1cd6e20d 7066 }
d76863c8 7067 }
1cd6e20d 7068
7069 return size_0;
7070}
7071
7072
7073/* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
7074 at the beginning of the parameter list PARAMS representing the size of the
7075 objects. This is to match the library ABI requirement. LOC is the location
7076 of the function call.
7077 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
7078 returned to allow the external call to be constructed. */
7079
7080static tree
7081add_atomic_size_parameter (unsigned n, location_t loc, tree function,
f1f41a6c 7082 vec<tree, va_gc> *params)
1cd6e20d 7083{
7084 tree size_node;
7085
7086 /* Insert a SIZE_T parameter as the first param. If there isn't
7087 enough space, allocate a new vector and recursively re-build with that. */
f1f41a6c 7088 if (!params->space (1))
1cd6e20d 7089 {
7090 unsigned int z, len;
f1f41a6c 7091 vec<tree, va_gc> *v;
1cd6e20d 7092 tree f;
7093
f1f41a6c 7094 len = params->length ();
7095 vec_alloc (v, len + 1);
5a672e62 7096 v->quick_push (build_int_cst (size_type_node, n));
1cd6e20d 7097 for (z = 0; z < len; z++)
f1f41a6c 7098 v->quick_push ((*params)[z]);
ec761d5a 7099 f = build_function_call_vec (loc, vNULL, function, v, NULL);
f1f41a6c 7100 vec_free (v);
1cd6e20d 7101 return f;
7102 }
7103
7104 /* Add the size parameter and leave as a function call for processing. */
7105 size_node = build_int_cst (size_type_node, n);
f1f41a6c 7106 params->quick_insert (0, size_node);
1cd6e20d 7107 return NULL_TREE;
7108}
7109
7110
a056826c 7111/* Return whether atomic operations for naturally aligned N-byte
7112 arguments are supported, whether inline or through libatomic. */
7113static bool
7114atomic_size_supported_p (int n)
7115{
7116 switch (n)
7117 {
7118 case 1:
7119 case 2:
7120 case 4:
7121 case 8:
7122 return true;
7123
7124 case 16:
7125 return targetm.scalar_mode_supported_p (TImode);
7126
7127 default:
7128 return false;
7129 }
7130}
7131
1cd6e20d 7132/* This will process an __atomic_exchange function call, determine whether it
7133 needs to be mapped to the _N variation, or turned into a library call.
7134 LOC is the location of the builtin call.
7135 FUNCTION is the DECL that has been invoked;
7136 PARAMS is the argument list for the call. The return value is non-null
7137 TRUE is returned if it is translated into the proper format for a call to the
7138 external library, and NEW_RETURN is set the tree for that function.
7139 FALSE is returned if processing for the _N variation is required, and
47ae02b7 7140 NEW_RETURN is set to the return value the result is copied into. */
1cd6e20d 7141static bool
7142resolve_overloaded_atomic_exchange (location_t loc, tree function,
f1f41a6c 7143 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 7144{
7145 tree p0, p1, p2, p3;
7146 tree I_type, I_type_ptr;
7147 int n = get_atomic_generic_size (loc, function, params);
7148
1d581089 7149 /* Size of 0 is an error condition. */
7150 if (n == 0)
7151 {
7152 *new_return = error_mark_node;
7153 return true;
7154 }
7155
1cd6e20d 7156 /* If not a lock-free size, change to the library generic format. */
a056826c 7157 if (!atomic_size_supported_p (n))
1cd6e20d 7158 {
7159 *new_return = add_atomic_size_parameter (n, loc, function, params);
7160 return true;
7161 }
7162
7163 /* Otherwise there is a lockfree match, transform the call from:
7164 void fn(T* mem, T* desired, T* return, model)
7165 into
7166 *return = (T) (fn (In* mem, (In) *desired, model)) */
7167
f1f41a6c 7168 p0 = (*params)[0];
7169 p1 = (*params)[1];
7170 p2 = (*params)[2];
7171 p3 = (*params)[3];
1cd6e20d 7172
7173 /* Create pointer to appropriate size. */
7174 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7175 I_type_ptr = build_pointer_type (I_type);
7176
7177 /* Convert object pointer to required type. */
7178 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 7179 (*params)[0] = p0;
1cd6e20d 7180 /* Convert new value to required type, and dereference it. */
7181 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7182 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 7183 (*params)[1] = p1;
1cd6e20d 7184
7185 /* Move memory model to the 3rd position, and end param list. */
f1f41a6c 7186 (*params)[2] = p3;
7187 params->truncate (3);
1cd6e20d 7188
7189 /* Convert return pointer and dereference it for later assignment. */
7190 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
7191
7192 return false;
b6a5fc45 7193}
7194
1cd6e20d 7195
7196/* This will process an __atomic_compare_exchange function call, determine
7197 whether it needs to be mapped to the _N variation, or turned into a lib call.
7198 LOC is the location of the builtin call.
7199 FUNCTION is the DECL that has been invoked;
7200 PARAMS is the argument list for the call. The return value is non-null
7201 TRUE is returned if it is translated into the proper format for a call to the
7202 external library, and NEW_RETURN is set the tree for that function.
7203 FALSE is returned if processing for the _N variation is required. */
7204
7205static bool
7206resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
f1f41a6c 7207 vec<tree, va_gc> *params,
1cd6e20d 7208 tree *new_return)
7209{
7210 tree p0, p1, p2;
7211 tree I_type, I_type_ptr;
7212 int n = get_atomic_generic_size (loc, function, params);
7213
1d581089 7214 /* Size of 0 is an error condition. */
7215 if (n == 0)
7216 {
7217 *new_return = error_mark_node;
7218 return true;
7219 }
7220
1cd6e20d 7221 /* If not a lock-free size, change to the library generic format. */
a056826c 7222 if (!atomic_size_supported_p (n))
1cd6e20d 7223 {
7224 /* The library generic format does not have the weak parameter, so
7225 remove it from the param list. Since a parameter has been removed,
7226 we can be sure that there is room for the SIZE_T parameter, meaning
7227 there will not be a recursive rebuilding of the parameter list, so
7228 there is no danger this will be done twice. */
7229 if (n > 0)
7230 {
f1f41a6c 7231 (*params)[3] = (*params)[4];
7232 (*params)[4] = (*params)[5];
7233 params->truncate (5);
1cd6e20d 7234 }
7235 *new_return = add_atomic_size_parameter (n, loc, function, params);
7236 return true;
7237 }
7238
7239 /* Otherwise, there is a match, so the call needs to be transformed from:
7240 bool fn(T* mem, T* desired, T* return, weak, success, failure)
7241 into
7242 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
ab2c1de8 7243
f1f41a6c 7244 p0 = (*params)[0];
7245 p1 = (*params)[1];
7246 p2 = (*params)[2];
1cd6e20d 7247
7248 /* Create pointer to appropriate size. */
7249 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7250 I_type_ptr = build_pointer_type (I_type);
7251
7252 /* Convert object pointer to required type. */
7253 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 7254 (*params)[0] = p0;
1cd6e20d 7255
7256 /* Convert expected pointer to required type. */
7257 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
f1f41a6c 7258 (*params)[1] = p1;
1cd6e20d 7259
7260 /* Convert desired value to required type, and dereference it. */
7261 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
7262 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
f1f41a6c 7263 (*params)[2] = p2;
1cd6e20d 7264
7265 /* The rest of the parameters are fine. NULL means no special return value
7266 processing.*/
7267 *new_return = NULL;
7268 return false;
7269}
7270
7271
7272/* This will process an __atomic_load function call, determine whether it
7273 needs to be mapped to the _N variation, or turned into a library call.
7274 LOC is the location of the builtin call.
7275 FUNCTION is the DECL that has been invoked;
7276 PARAMS is the argument list for the call. The return value is non-null
7277 TRUE is returned if it is translated into the proper format for a call to the
7278 external library, and NEW_RETURN is set the tree for that function.
7279 FALSE is returned if processing for the _N variation is required, and
47ae02b7 7280 NEW_RETURN is set to the return value the result is copied into. */
1cd6e20d 7281
7282static bool
7283resolve_overloaded_atomic_load (location_t loc, tree function,
f1f41a6c 7284 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 7285{
7286 tree p0, p1, p2;
7287 tree I_type, I_type_ptr;
7288 int n = get_atomic_generic_size (loc, function, params);
7289
1d581089 7290 /* Size of 0 is an error condition. */
7291 if (n == 0)
7292 {
7293 *new_return = error_mark_node;
7294 return true;
7295 }
7296
1cd6e20d 7297 /* If not a lock-free size, change to the library generic format. */
a056826c 7298 if (!atomic_size_supported_p (n))
1cd6e20d 7299 {
7300 *new_return = add_atomic_size_parameter (n, loc, function, params);
7301 return true;
7302 }
7303
7304 /* Otherwise, there is a match, so the call needs to be transformed from:
7305 void fn(T* mem, T* return, model)
7306 into
7307 *return = (T) (fn ((In *) mem, model)) */
7308
f1f41a6c 7309 p0 = (*params)[0];
7310 p1 = (*params)[1];
7311 p2 = (*params)[2];
1cd6e20d 7312
7313 /* Create pointer to appropriate size. */
7314 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7315 I_type_ptr = build_pointer_type (I_type);
7316
7317 /* Convert object pointer to required type. */
7318 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 7319 (*params)[0] = p0;
1cd6e20d 7320
7321 /* Move memory model to the 2nd position, and end param list. */
f1f41a6c 7322 (*params)[1] = p2;
7323 params->truncate (2);
1cd6e20d 7324
7325 /* Convert return pointer and dereference it for later assignment. */
7326 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7327
7328 return false;
7329}
7330
7331
7332/* This will process an __atomic_store function call, determine whether it
7333 needs to be mapped to the _N variation, or turned into a library call.
7334 LOC is the location of the builtin call.
7335 FUNCTION is the DECL that has been invoked;
7336 PARAMS is the argument list for the call. The return value is non-null
7337 TRUE is returned if it is translated into the proper format for a call to the
7338 external library, and NEW_RETURN is set the tree for that function.
7339 FALSE is returned if processing for the _N variation is required, and
47ae02b7 7340 NEW_RETURN is set to the return value the result is copied into. */
1cd6e20d 7341
7342static bool
7343resolve_overloaded_atomic_store (location_t loc, tree function,
f1f41a6c 7344 vec<tree, va_gc> *params, tree *new_return)
1cd6e20d 7345{
7346 tree p0, p1;
7347 tree I_type, I_type_ptr;
7348 int n = get_atomic_generic_size (loc, function, params);
7349
1d581089 7350 /* Size of 0 is an error condition. */
7351 if (n == 0)
7352 {
7353 *new_return = error_mark_node;
7354 return true;
7355 }
7356
1cd6e20d 7357 /* If not a lock-free size, change to the library generic format. */
a056826c 7358 if (!atomic_size_supported_p (n))
1cd6e20d 7359 {
7360 *new_return = add_atomic_size_parameter (n, loc, function, params);
7361 return true;
7362 }
7363
7364 /* Otherwise, there is a match, so the call needs to be transformed from:
7365 void fn(T* mem, T* value, model)
7366 into
7367 fn ((In *) mem, (In) *value, model) */
7368
f1f41a6c 7369 p0 = (*params)[0];
7370 p1 = (*params)[1];
1cd6e20d 7371
7372 /* Create pointer to appropriate size. */
7373 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7374 I_type_ptr = build_pointer_type (I_type);
7375
7376 /* Convert object pointer to required type. */
7377 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
f1f41a6c 7378 (*params)[0] = p0;
1cd6e20d 7379
7380 /* Convert new value to required type, and dereference it. */
7381 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7382 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
f1f41a6c 7383 (*params)[1] = p1;
1cd6e20d 7384
7385 /* The memory model is in the right spot already. Return is void. */
7386 *new_return = NULL_TREE;
7387
7388 return false;
7389}
7390
7391
b6a5fc45 7392/* Some builtin functions are placeholders for other expressions. This
7393 function should be called immediately after parsing the call expression
7394 before surrounding code has committed to the type of the expression.
7395
e60a6f7b 7396 LOC is the location of the builtin call.
7397
b6a5fc45 7398 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
7399 PARAMS is the argument list for the call. The return value is non-null
7400 when expansion is complete, and null if normal processing should
7401 continue. */
7402
7403tree
f1f41a6c 7404resolve_overloaded_builtin (location_t loc, tree function,
7405 vec<tree, va_gc> *params)
b6a5fc45 7406{
7407 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
517906ff 7408
7409 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
7410 Those are not valid to call with a pointer to _Bool (or C++ bool)
7411 and so must be rejected. */
7412 bool fetch_op = true;
1cd6e20d 7413 bool orig_format = true;
7414 tree new_return = NULL_TREE;
7415
65441f6f 7416 switch (DECL_BUILT_IN_CLASS (function))
7417 {
7418 case BUILT_IN_NORMAL:
7419 break;
7420 case BUILT_IN_MD:
7421 if (targetm.resolve_overloaded_builtin)
e60a6f7b 7422 return targetm.resolve_overloaded_builtin (loc, function, params);
65441f6f 7423 else
a0c938f0 7424 return NULL_TREE;
65441f6f 7425 default:
7426 return NULL_TREE;
7427 }
a0c938f0 7428
65441f6f 7429 /* Handle BUILT_IN_NORMAL here. */
b6a5fc45 7430 switch (orig_code)
7431 {
123081ef 7432 case BUILT_IN_SPECULATION_SAFE_VALUE_N:
7433 {
7434 tree new_function, first_param, result;
7435 enum built_in_function fncode
7436 = speculation_safe_value_resolve_call (function, params);;
7437
7438 first_param = (*params)[0];
7439 if (fncode == BUILT_IN_NONE
7440 || !speculation_safe_value_resolve_params (loc, function, params))
7441 return error_mark_node;
7442
7443 if (targetm.have_speculation_safe_value (true))
7444 {
7445 new_function = builtin_decl_explicit (fncode);
7446 result = build_function_call_vec (loc, vNULL, new_function, params,
7447 NULL);
7448
7449 if (result == error_mark_node)
7450 return result;
7451
7452 return speculation_safe_value_resolve_return (first_param, result);
7453 }
7454 else
7455 {
7456 /* This target doesn't have, or doesn't need, active mitigation
7457 against incorrect speculative execution. Simply return the
7458 first parameter to the builtin. */
7459 if (!targetm.have_speculation_safe_value (false))
7460 /* The user has invoked __builtin_speculation_safe_value
7461 even though __HAVE_SPECULATION_SAFE_VALUE is not
7462 defined: emit a warning. */
7463 warning_at (input_location, 0,
7464 "this target does not define a speculation barrier; "
7465 "your program will still execute correctly, "
7466 "but incorrect speculation may not be be "
7467 "restricted");
7468
7469 /* If the optional second argument is present, handle any side
7470 effects now. */
7471 if (params->length () == 2
7472 && TREE_SIDE_EFFECTS ((*params)[1]))
7473 return build2 (COMPOUND_EXPR, TREE_TYPE (first_param),
7474 (*params)[1], first_param);
7475
7476 return first_param;
7477 }
7478 }
7479
1cd6e20d 7480 case BUILT_IN_ATOMIC_EXCHANGE:
7481 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7482 case BUILT_IN_ATOMIC_LOAD:
7483 case BUILT_IN_ATOMIC_STORE:
7484 {
7485 /* Handle these 4 together so that they can fall through to the next
7486 case if the call is transformed to an _N variant. */
7487 switch (orig_code)
c7afb782 7488 {
1cd6e20d 7489 case BUILT_IN_ATOMIC_EXCHANGE:
7490 {
7491 if (resolve_overloaded_atomic_exchange (loc, function, params,
7492 &new_return))
7493 return new_return;
7494 /* Change to the _N variant. */
7495 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
7496 break;
7497 }
7498
7499 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7500 {
7501 if (resolve_overloaded_atomic_compare_exchange (loc, function,
7502 params,
7503 &new_return))
7504 return new_return;
7505 /* Change to the _N variant. */
7506 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
7507 break;
7508 }
7509 case BUILT_IN_ATOMIC_LOAD:
7510 {
7511 if (resolve_overloaded_atomic_load (loc, function, params,
7512 &new_return))
7513 return new_return;
7514 /* Change to the _N variant. */
7515 orig_code = BUILT_IN_ATOMIC_LOAD_N;
7516 break;
7517 }
7518 case BUILT_IN_ATOMIC_STORE:
7519 {
7520 if (resolve_overloaded_atomic_store (loc, function, params,
7521 &new_return))
7522 return new_return;
7523 /* Change to the _N variant. */
7524 orig_code = BUILT_IN_ATOMIC_STORE_N;
7525 break;
7526 }
7527 default:
7528 gcc_unreachable ();
c7afb782 7529 }
1cd6e20d 7530 }
c7afb782 7531 /* FALLTHRU */
1cd6e20d 7532 case BUILT_IN_ATOMIC_EXCHANGE_N:
7533 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
7534 case BUILT_IN_ATOMIC_LOAD_N:
7535 case BUILT_IN_ATOMIC_STORE_N:
c7afb782 7536 fetch_op = false;
7537 /* FALLTHRU */
1cd6e20d 7538 case BUILT_IN_ATOMIC_ADD_FETCH_N:
7539 case BUILT_IN_ATOMIC_SUB_FETCH_N:
7540 case BUILT_IN_ATOMIC_AND_FETCH_N:
7541 case BUILT_IN_ATOMIC_NAND_FETCH_N:
7542 case BUILT_IN_ATOMIC_XOR_FETCH_N:
7543 case BUILT_IN_ATOMIC_OR_FETCH_N:
7544 case BUILT_IN_ATOMIC_FETCH_ADD_N:
7545 case BUILT_IN_ATOMIC_FETCH_SUB_N:
7546 case BUILT_IN_ATOMIC_FETCH_AND_N:
7547 case BUILT_IN_ATOMIC_FETCH_NAND_N:
7548 case BUILT_IN_ATOMIC_FETCH_XOR_N:
7549 case BUILT_IN_ATOMIC_FETCH_OR_N:
c7afb782 7550 orig_format = false;
7551 /* FALLTHRU */
2797f13a 7552 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
7553 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
7554 case BUILT_IN_SYNC_FETCH_AND_OR_N:
7555 case BUILT_IN_SYNC_FETCH_AND_AND_N:
7556 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
7557 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
7558 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
7559 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
7560 case BUILT_IN_SYNC_OR_AND_FETCH_N:
7561 case BUILT_IN_SYNC_AND_AND_FETCH_N:
7562 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
7563 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
7564 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
7565 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
7566 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
7567 case BUILT_IN_SYNC_LOCK_RELEASE_N:
b6a5fc45 7568 {
517906ff 7569 /* The following are not _FETCH_OPs and must be accepted with
7570 pointers to _Bool (or C++ bool). */
7571 if (fetch_op)
7572 fetch_op =
7573 (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7574 && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
7575 && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
7576 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
7577
7578 int n = sync_resolve_size (function, params, fetch_op);
b9c74b4d 7579 tree new_function, first_param, result;
b9a16870 7580 enum built_in_function fncode;
b6a5fc45 7581
7582 if (n == 0)
7583 return error_mark_node;
7584
b9a16870 7585 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
7586 new_function = builtin_decl_explicit (fncode);
1cd6e20d 7587 if (!sync_resolve_params (loc, function, new_function, params,
7588 orig_format))
b6a5fc45 7589 return error_mark_node;
7590
f1f41a6c 7591 first_param = (*params)[0];
ec761d5a 7592 result = build_function_call_vec (loc, vNULL, new_function, params,
7593 NULL);
1cd6e20d 7594 if (result == error_mark_node)
7595 return result;
2797f13a 7596 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
1cd6e20d 7597 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
efa8e86e 7598 && orig_code != BUILT_IN_ATOMIC_STORE_N
7599 && orig_code != BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N)
1cd6e20d 7600 result = sync_resolve_return (first_param, result, orig_format);
b6a5fc45 7601
92e7ab1e 7602 if (fetch_op)
7603 /* Prevent -Wunused-value warning. */
7604 TREE_USED (result) = true;
7605
1cd6e20d 7606 /* If new_return is set, assign function to that expr and cast the
7607 result to void since the generic interface returned void. */
7608 if (new_return)
7609 {
7610 /* Cast function result from I{1,2,4,8,16} to the required type. */
7611 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
7612 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
7613 result);
7614 TREE_SIDE_EFFECTS (result) = 1;
7615 protected_set_expr_location (result, loc);
7616 result = convert (void_type_node, result);
7617 }
b6a5fc45 7618 return result;
7619 }
7620
7621 default:
65441f6f 7622 return NULL_TREE;
b6a5fc45 7623 }
7624}
7625
73437615 7626/* vector_types_compatible_elements_p is used in type checks of vectors
7627 values used as operands of binary operators. Where it returns true, and
7628 the other checks of the caller succeed (being vector types in he first
7629 place, and matching number of elements), we can just treat the types
7630 as essentially the same.
7631 Contrast with vector_targets_convertible_p, which is used for vector
7632 pointer types, and vector_types_convertible_p, which will allow
7633 language-specific matches under the control of flag_lax_vector_conversions,
7634 and might still require a conversion. */
7635/* True if vector types T1 and T2 can be inputs to the same binary
7636 operator without conversion.
7637 We don't check the overall vector size here because some of our callers
7638 want to give different error messages when the vectors are compatible
7639 except for the element count. */
7640
491255f5 7641bool
73437615 7642vector_types_compatible_elements_p (tree t1, tree t2)
491255f5 7643{
73437615 7644 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
7645 t1 = TREE_TYPE (t1);
7646 t2 = TREE_TYPE (t2);
7647
491255f5 7648 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
7649
d32a4fbd 7650 gcc_assert ((INTEGRAL_TYPE_P (t1)
7651 || c1 == REAL_TYPE
7652 || c1 == FIXED_POINT_TYPE)
7653 && (INTEGRAL_TYPE_P (t2)
7654 || c2 == REAL_TYPE
9421ebb9 7655 || c2 == FIXED_POINT_TYPE));
491255f5 7656
73437615 7657 t1 = c_common_signed_type (t1);
7658 t2 = c_common_signed_type (t2);
491255f5 7659 /* Equality works here because c_common_signed_type uses
7660 TYPE_MAIN_VARIANT. */
73437615 7661 if (t1 == t2)
7662 return true;
7663 if (opaque && c1 == c2
d32a4fbd 7664 && (INTEGRAL_TYPE_P (t1) || c1 == REAL_TYPE)
73437615 7665 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
7666 return true;
7667 return false;
491255f5 7668}
7669
be7350e7 7670/* Check for missing format attributes on function pointers. LTYPE is
7671 the new type or left-hand side type. RTYPE is the old type or
7672 right-hand side type. Returns TRUE if LTYPE is missing the desired
7673 attribute. */
7674
7675bool
7676check_missing_format_attribute (tree ltype, tree rtype)
7677{
7678 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
7679 tree ra;
7680
7681 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
7682 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
7683 break;
7684 if (ra)
7685 {
7686 tree la;
7687 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
7688 if (is_attribute_p ("format", TREE_PURPOSE (la)))
7689 break;
7690 return !la;
7691 }
7692 else
7693 return false;
7694}
7695
41771881 7696/* Setup a TYPE_DECL node as a typedef representation.
7697
7698 X is a TYPE_DECL for a typedef statement. Create a brand new
7699 ..._TYPE node (which will be just a variant of the existing
7700 ..._TYPE node with identical properties) and then install X
7701 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
7702
7703 The whole point here is to end up with a situation where each
7704 and every ..._TYPE node the compiler creates will be uniquely
7705 associated with AT MOST one node representing a typedef name.
7706 This way, even though the compiler substitutes corresponding
7707 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
7708 early on, later parts of the compiler can always do the reverse
7709 translation and get back the corresponding typedef name. For
7710 example, given:
ab2c1de8 7711
41771881 7712 typedef struct S MY_TYPE;
7713 MY_TYPE object;
7714
7715 Later parts of the compiler might only know that `object' was of
7716 type `struct S' if it were not for code just below. With this
7717 code however, later parts of the compiler see something like:
7718
7719 struct S' == struct S
7720 typedef struct S' MY_TYPE;
7721 struct S' object;
7722
7723 And they can then deduce (from the node for type struct S') that
7724 the original object declaration was:
7725
7726 MY_TYPE object;
7727
7728 Being able to do this is important for proper support of protoize,
7729 and also for generating precise symbolic debugging information
7730 which takes full account of the programmer's (typedef) vocabulary.
7731
7732 Obviously, we don't want to generate a duplicate ..._TYPE node if
7733 the TYPE_DECL node that we are now processing really represents a
7734 standard built-in type. */
7735
7736void
7737set_underlying_type (tree x)
7738{
7739 if (x == error_mark_node)
7740 return;
2c24fd5e 7741 if (DECL_IS_BUILTIN (x) && TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
41771881 7742 {
7743 if (TYPE_NAME (TREE_TYPE (x)) == 0)
7744 TYPE_NAME (TREE_TYPE (x)) = x;
7745 }
ba5a0dd7 7746 else if (TREE_TYPE (x) != error_mark_node
7747 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
41771881 7748 {
7749 tree tt = TREE_TYPE (x);
7750 DECL_ORIGINAL_TYPE (x) = tt;
ba5a0dd7 7751 tt = build_variant_type_copy (tt);
7752 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
7753 TYPE_NAME (tt) = x;
da7a26fc 7754
7755 /* Mark the type as used only when its type decl is decorated
7756 with attribute unused. */
7757 if (lookup_attribute ("unused", DECL_ATTRIBUTES (x)))
7758 TREE_USED (tt) = 1;
7759
ba5a0dd7 7760 TREE_TYPE (x) = tt;
41771881 7761 }
7762}
7763
1a4c44c5 7764/* Record the types used by the current global variable declaration
7765 being parsed, so that we can decide later to emit their debug info.
7766 Those types are in types_used_by_cur_var_decl, and we are going to
7767 store them in the types_used_by_vars_hash hash table.
7768 DECL is the declaration of the global variable that has been parsed. */
7769
7770void
7771record_types_used_by_current_var_decl (tree decl)
7772{
7773 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
7774
f1f41a6c 7775 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
1a4c44c5 7776 {
f1f41a6c 7777 tree type = types_used_by_cur_var_decl->pop ();
aef48c9a 7778 types_used_by_var_decl_insert (type, decl);
1a4c44c5 7779 }
7780}
7781
f352a3fb 7782/* The C and C++ parsers both use vectors to hold function arguments.
7783 For efficiency, we keep a cache of unused vectors. This is the
7784 cache. */
7785
f1f41a6c 7786typedef vec<tree, va_gc> *tree_gc_vec;
7787static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
f352a3fb 7788
7789/* Return a new vector from the cache. If the cache is empty,
7790 allocate a new vector. These vectors are GC'ed, so it is OK if the
7791 pointer is not released.. */
7792
f1f41a6c 7793vec<tree, va_gc> *
f352a3fb 7794make_tree_vector (void)
7795{
f1f41a6c 7796 if (tree_vector_cache && !tree_vector_cache->is_empty ())
7797 return tree_vector_cache->pop ();
f352a3fb 7798 else
7799 {
f1f41a6c 7800 /* Passing 0 to vec::alloc returns NULL, and our callers require
f352a3fb 7801 that we always return a non-NULL value. The vector code uses
7802 4 when growing a NULL vector, so we do too. */
f1f41a6c 7803 vec<tree, va_gc> *v;
7804 vec_alloc (v, 4);
7805 return v;
f352a3fb 7806 }
7807}
7808
7809/* Release a vector of trees back to the cache. */
7810
7811void
f1f41a6c 7812release_tree_vector (vec<tree, va_gc> *vec)
f352a3fb 7813{
7814 if (vec != NULL)
7815 {
f1f41a6c 7816 vec->truncate (0);
7817 vec_safe_push (tree_vector_cache, vec);
f352a3fb 7818 }
7819}
7820
7821/* Get a new tree vector holding a single tree. */
7822
f1f41a6c 7823vec<tree, va_gc> *
f352a3fb 7824make_tree_vector_single (tree t)
7825{
f1f41a6c 7826 vec<tree, va_gc> *ret = make_tree_vector ();
7827 ret->quick_push (t);
f352a3fb 7828 return ret;
7829}
7830
c66c81be 7831/* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
7832
f1f41a6c 7833vec<tree, va_gc> *
c66c81be 7834make_tree_vector_from_list (tree list)
7835{
f1f41a6c 7836 vec<tree, va_gc> *ret = make_tree_vector ();
c66c81be 7837 for (; list; list = TREE_CHAIN (list))
f1f41a6c 7838 vec_safe_push (ret, TREE_VALUE (list));
c66c81be 7839 return ret;
7840}
7841
492ab670 7842/* Get a new tree vector of the values of a CONSTRUCTOR. */
7843
7844vec<tree, va_gc> *
7845make_tree_vector_from_ctor (tree ctor)
7846{
7847 vec<tree,va_gc> *ret = make_tree_vector ();
7848 vec_safe_reserve (ret, CONSTRUCTOR_NELTS (ctor));
7849 for (unsigned i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
7850 ret->quick_push (CONSTRUCTOR_ELT (ctor, i)->value);
7851 return ret;
7852}
7853
f352a3fb 7854/* Get a new tree vector which is a copy of an existing one. */
7855
f1f41a6c 7856vec<tree, va_gc> *
7857make_tree_vector_copy (const vec<tree, va_gc> *orig)
f352a3fb 7858{
f1f41a6c 7859 vec<tree, va_gc> *ret;
f352a3fb 7860 unsigned int ix;
7861 tree t;
7862
7863 ret = make_tree_vector ();
f1f41a6c 7864 vec_safe_reserve (ret, vec_safe_length (orig));
7865 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
7866 ret->quick_push (t);
f352a3fb 7867 return ret;
7868}
7869
a9ffdd35 7870/* Return true if KEYWORD starts a type specifier. */
7871
7872bool
7873keyword_begins_type_specifier (enum rid keyword)
7874{
7875 switch (keyword)
7876 {
4fba5eb9 7877 case RID_AUTO_TYPE:
a9ffdd35 7878 case RID_INT:
7879 case RID_CHAR:
7880 case RID_FLOAT:
7881 case RID_DOUBLE:
7882 case RID_VOID:
a9ffdd35 7883 case RID_UNSIGNED:
7884 case RID_LONG:
7885 case RID_SHORT:
7886 case RID_SIGNED:
82c85aba 7887 CASE_RID_FLOATN_NX:
a9ffdd35 7888 case RID_DFLOAT32:
7889 case RID_DFLOAT64:
7890 case RID_DFLOAT128:
7891 case RID_FRACT:
7892 case RID_ACCUM:
7893 case RID_BOOL:
7894 case RID_WCHAR:
82362779 7895 case RID_CHAR8:
a9ffdd35 7896 case RID_CHAR16:
7897 case RID_CHAR32:
7898 case RID_SAT:
7899 case RID_COMPLEX:
7900 case RID_TYPEOF:
7901 case RID_STRUCT:
7902 case RID_CLASS:
7903 case RID_UNION:
7904 case RID_ENUM:
7905 return true;
7906 default:
9f75f026 7907 if (keyword >= RID_FIRST_INT_N
7908 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
7909 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
7910 return true;
a9ffdd35 7911 return false;
7912 }
7913}
7914
7915/* Return true if KEYWORD names a type qualifier. */
7916
7917bool
7918keyword_is_type_qualifier (enum rid keyword)
7919{
7920 switch (keyword)
7921 {
7922 case RID_CONST:
7923 case RID_VOLATILE:
7924 case RID_RESTRICT:
b560fabd 7925 case RID_ATOMIC:
a9ffdd35 7926 return true;
7927 default:
7928 return false;
7929 }
7930}
7931
7932/* Return true if KEYWORD names a storage class specifier.
7933
7934 RID_TYPEDEF is not included in this list despite `typedef' being
7935 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
7936 such for syntactic convenience only. */
7937
7938bool
7939keyword_is_storage_class_specifier (enum rid keyword)
7940{
7941 switch (keyword)
7942 {
7943 case RID_STATIC:
7944 case RID_EXTERN:
7945 case RID_REGISTER:
7946 case RID_AUTO:
7947 case RID_MUTABLE:
7948 case RID_THREAD:
7949 return true;
7950 default:
7951 return false;
7952 }
7953}
7954
fad3f658 7955/* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
7956
7957static bool
7958keyword_is_function_specifier (enum rid keyword)
7959{
7960 switch (keyword)
7961 {
7962 case RID_INLINE:
985c6e3a 7963 case RID_NORETURN:
fad3f658 7964 case RID_VIRTUAL:
7965 case RID_EXPLICIT:
7966 return true;
7967 default:
7968 return false;
7969 }
7970}
7971
7972/* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
7973 declaration-specifier (C99 6.7). */
7974
7975bool
7976keyword_is_decl_specifier (enum rid keyword)
7977{
7978 if (keyword_is_storage_class_specifier (keyword)
7979 || keyword_is_type_qualifier (keyword)
7980 || keyword_is_function_specifier (keyword))
7981 return true;
7982
7983 switch (keyword)
7984 {
7985 case RID_TYPEDEF:
7986 case RID_FRIEND:
7987 case RID_CONSTEXPR:
7988 return true;
7989 default:
7990 return false;
7991 }
7992}
7993
9b88d08d 7994/* Initialize language-specific-bits of tree_contains_struct. */
7995
7996void
7997c_common_init_ts (void)
7998{
7999 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
8000 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
8001}
8002
244db24d 8003/* Build a user-defined numeric literal out of an integer constant type VALUE
8004 with identifier SUFFIX. */
8005
8006tree
324ca377 8007build_userdef_literal (tree suffix_id, tree value,
8008 enum overflow_type overflow, tree num_string)
244db24d 8009{
8010 tree literal = make_node (USERDEF_LITERAL);
8011 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
8012 USERDEF_LITERAL_VALUE (literal) = value;
324ca377 8013 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
244db24d 8014 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
8015 return literal;
8016}
8017
7345b977 8018/* For vector[index], convert the vector to an array of the underlying type.
8019 Return true if the resulting ARRAY_REF should not be an lvalue. */
c61ef207 8020
8021bool
7345b977 8022convert_vector_to_array_for_subscript (location_t loc,
8023 tree *vecp, tree index)
7059d45d 8024{
c61ef207 8025 bool ret = false;
6290f0db 8026 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
7059d45d 8027 {
8028 tree type = TREE_TYPE (*vecp);
7059d45d 8029
c61ef207 8030 ret = !lvalue_p (*vecp);
7345b977 8031
d582d140 8032 index = fold_for_warn (index);
7059d45d 8033 if (TREE_CODE (index) == INTEGER_CST)
e913b5cd 8034 if (!tree_fits_uhwi_p (index)
f08ee65f 8035 || maybe_ge (tree_to_uhwi (index), TYPE_VECTOR_SUBPARTS (type)))
7059d45d 8036 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
8037
7345b977 8038 /* We are building an ARRAY_REF so mark the vector as addressable
8039 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
8040 for function parameters. */
8041 c_common_mark_addressable_vec (*vecp);
8042
8043 *vecp = build1 (VIEW_CONVERT_EXPR,
8044 build_array_type_nelts (TREE_TYPE (type),
8045 TYPE_VECTOR_SUBPARTS (type)),
8046 *vecp);
7059d45d 8047 }
c61ef207 8048 return ret;
7059d45d 8049}
8050
41ed701a 8051/* Determine which of the operands, if any, is a scalar that needs to be
8052 converted to a vector, for the range of operations. */
8053enum stv_conv
8054scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
8055 bool complain)
8056{
8057 tree type0 = TREE_TYPE (op0);
8058 tree type1 = TREE_TYPE (op1);
8059 bool integer_only_op = false;
8060 enum stv_conv ret = stv_firstarg;
8061
6290f0db 8062 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
41ed701a 8063 switch (code)
8064 {
8065 /* Most GENERIC binary expressions require homogeneous arguments.
8066 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
8067 argument that is a vector and a second one that is a scalar, so
8068 we never return stv_secondarg for them. */
8069 case RSHIFT_EXPR:
8070 case LSHIFT_EXPR:
8071 if (TREE_CODE (type0) == INTEGER_TYPE
8072 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
8073 {
645b0f8d 8074 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0,
8075 NULL_TREE, false))
41ed701a 8076 {
8077 if (complain)
8078 error_at (loc, "conversion of scalar %qT to vector %qT "
8079 "involves truncation", type0, type1);
8080 return stv_error;
8081 }
8082 else
8083 return stv_firstarg;
8084 }
8085 break;
8086
8087 case BIT_IOR_EXPR:
8088 case BIT_XOR_EXPR:
8089 case BIT_AND_EXPR:
8090 integer_only_op = true;
e3533433 8091 /* fall through */
41ed701a 8092
7b463b19 8093 case VEC_COND_EXPR:
8094
41ed701a 8095 case PLUS_EXPR:
8096 case MINUS_EXPR:
8097 case MULT_EXPR:
8098 case TRUNC_DIV_EXPR:
8099 case CEIL_DIV_EXPR:
8100 case FLOOR_DIV_EXPR:
8101 case ROUND_DIV_EXPR:
8102 case EXACT_DIV_EXPR:
8103 case TRUNC_MOD_EXPR:
8104 case FLOOR_MOD_EXPR:
8105 case RDIV_EXPR:
8106 case EQ_EXPR:
8107 case NE_EXPR:
8108 case LE_EXPR:
8109 case GE_EXPR:
8110 case LT_EXPR:
8111 case GT_EXPR:
8112 /* What about UNLT_EXPR? */
6290f0db 8113 if (VECTOR_TYPE_P (type0))
41ed701a 8114 {
41ed701a 8115 ret = stv_secondarg;
a4f59596 8116 std::swap (type0, type1);
8117 std::swap (op0, op1);
41ed701a 8118 }
8119
8120 if (TREE_CODE (type0) == INTEGER_TYPE
8121 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
8122 {
645b0f8d 8123 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0,
8124 NULL_TREE, false))
41ed701a 8125 {
8126 if (complain)
8127 error_at (loc, "conversion of scalar %qT to vector %qT "
8128 "involves truncation", type0, type1);
8129 return stv_error;
8130 }
8131 return ret;
8132 }
8133 else if (!integer_only_op
8134 /* Allow integer --> real conversion if safe. */
8135 && (TREE_CODE (type0) == REAL_TYPE
8136 || TREE_CODE (type0) == INTEGER_TYPE)
8137 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
8138 {
645b0f8d 8139 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0,
8140 NULL_TREE, false))
41ed701a 8141 {
8142 if (complain)
8143 error_at (loc, "conversion of scalar %qT to vector %qT "
8144 "involves truncation", type0, type1);
8145 return stv_error;
8146 }
8147 return ret;
8148 }
8149 default:
8150 break;
8151 }
8152
8153 return stv_nothing;
8154}
8155
db8ffb40 8156/* Return the alignment of std::max_align_t.
8157
8158 [support.types.layout] The type max_align_t is a POD type whose alignment
8159 requirement is at least as great as that of every scalar type, and whose
8160 alignment requirement is supported in every context. */
8161
8162unsigned
8163max_align_t_align ()
8164{
9b5c49ef 8165 unsigned int max_align = MAX (TYPE_ALIGN (long_long_integer_type_node),
8166 TYPE_ALIGN (long_double_type_node));
8167 if (float128_type_node != NULL_TREE)
8168 max_align = MAX (max_align, TYPE_ALIGN (float128_type_node));
8169 return max_align;
db8ffb40 8170}
8171
ffcdbf9c 8172/* Return true iff ALIGN is an integral constant that is a fundamental
8173 alignment, as defined by [basic.align] in the c++-11
8174 specifications.
8175
8176 That is:
8177
8178 [A fundamental alignment is represented by an alignment less than or
8179 equal to the greatest alignment supported by the implementation
db8ffb40 8180 in all contexts, which is equal to alignof(max_align_t)]. */
ffcdbf9c 8181
8182bool
db8ffb40 8183cxx_fundamental_alignment_p (unsigned align)
ffcdbf9c 8184{
db8ffb40 8185 return (align <= max_align_t_align ());
ffcdbf9c 8186}
8187
46da3601 8188/* Return true if T is a pointer to a zero-sized aggregate. */
8189
8190bool
8191pointer_to_zero_sized_aggr_p (tree t)
8192{
8193 if (!POINTER_TYPE_P (t))
8194 return false;
8195 t = TREE_TYPE (t);
8196 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
8197}
8198
547c6b1f 8199/* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
8200 with no library fallback or for an ADDR_EXPR whose operand is such type
8201 issues an error pointing to the location LOC.
8202 Returns true when the expression has been diagnosed and false
8203 otherwise. */
0f4cea33 8204
547c6b1f 8205bool
8206reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
8207{
8208 if (TREE_CODE (expr) == ADDR_EXPR)
8209 expr = TREE_OPERAND (expr, 0);
8210
d76863c8 8211 STRIP_ANY_LOCATION_WRAPPER (expr);
8212
547c6b1f 8213 if (TREE_TYPE (expr)
8214 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
12cc1225 8215 && TREE_CODE (expr) == FUNCTION_DECL
547c6b1f 8216 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
8217 false positives for user-declared built-ins such as abs or
8218 strlen, and for C++ operators new and delete.
8219 The c_decl_implicit() test avoids false positives for implicitly
8220 declared built-ins with library fallbacks (such as abs). */
a0e9bfbb 8221 && fndecl_built_in_p (expr)
547c6b1f 8222 && DECL_IS_BUILTIN (expr)
8223 && !c_decl_implicit (expr)
8224 && !DECL_ASSEMBLER_NAME_SET_P (expr))
8225 {
8226 if (loc == UNKNOWN_LOCATION)
8227 loc = EXPR_LOC_OR_LOC (expr, input_location);
8228
8229 /* Reject arguments that are built-in functions with
8230 no library fallback. */
8231 error_at (loc, "built-in function %qE must be directly called", expr);
8232
8233 return true;
8234 }
8235
8236 return false;
8237}
8238
08acf739 8239/* Issue an ERROR for an invalid SIZE of array NAME which is null
8240 for unnamed arrays. */
8241
8242void
8243invalid_array_size_error (location_t loc, cst_size_error error,
8244 const_tree size, const_tree name)
8245{
8246 tree maxsize = max_object_size ();
8247 switch (error)
8248 {
ec425a89 8249 case cst_size_not_constant:
8250 if (name)
8251 error_at (loc, "size of array %qE is not a constant expression",
8252 name);
8253 else
8254 error_at (loc, "size of array is not a constant expression");
8255 break;
08acf739 8256 case cst_size_negative:
8257 if (name)
8258 error_at (loc, "size %qE of array %qE is negative",
8259 size, name);
8260 else
8261 error_at (loc, "size %qE of array is negative",
8262 size);
8263 break;
8264 case cst_size_too_big:
8265 if (name)
8266 error_at (loc, "size %qE of array %qE exceeds maximum "
8267 "object size %qE", size, name, maxsize);
8268 else
8269 error_at (loc, "size %qE of array exceeds maximum "
8270 "object size %qE", size, maxsize);
8271 break;
8272 case cst_size_overflow:
8273 if (name)
8274 error_at (loc, "size of array %qE exceeds maximum "
8275 "object size %qE", name, maxsize);
8276 else
8277 error_at (loc, "size of array exceeds maximum "
8278 "object size %qE", maxsize);
8279 break;
8280 default:
8281 gcc_unreachable ();
8282 }
8283}
8284
81b1b2a8 8285/* Check if array size calculations overflow or if the array covers more
8286 than half of the address space. Return true if the size of the array
08acf739 8287 is valid, false otherwise. T is either the type of the array or its
8288 size, and NAME is the name of the array, or null for unnamed arrays. */
81b1b2a8 8289
8290bool
08acf739 8291valid_array_size_p (location_t loc, const_tree t, tree name, bool complain)
81b1b2a8 8292{
08acf739 8293 if (t == error_mark_node)
8294 return true;
8295
8296 const_tree size;
8297 if (TYPE_P (t))
81b1b2a8 8298 {
08acf739 8299 if (!COMPLETE_TYPE_P (t))
8300 return true;
8301 size = TYPE_SIZE_UNIT (t);
81b1b2a8 8302 }
08acf739 8303 else
8304 size = t;
8305
8306 if (TREE_CODE (size) != INTEGER_CST)
8307 return true;
8308
8309 cst_size_error error;
8310 if (valid_constant_size_p (size, &error))
8311 return true;
8312
8313 if (!complain)
8314 return false;
8315
8316 if (TREE_CODE (TREE_TYPE (size)) == ENUMERAL_TYPE)
8317 /* Show the value of the enumerator rather than its name. */
8318 size = convert (ssizetype, const_cast<tree> (size));
8319
8320 invalid_array_size_error (loc, error, size, name);
8321 return false;
81b1b2a8 8322}
8323
e3e8c48c 8324/* Read SOURCE_DATE_EPOCH from environment to have a deterministic
8325 timestamp to replace embedded current dates to get reproducible
8326 results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
dfa5c0d3 8327
e3e8c48c 8328time_t
dfa5c0d3 8329cb_get_source_date_epoch (cpp_reader *pfile ATTRIBUTE_UNUSED)
e3e8c48c 8330{
8331 char *source_date_epoch;
bd08c370 8332 int64_t epoch;
e3e8c48c 8333 char *endptr;
8334
8335 source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
8336 if (!source_date_epoch)
8337 return (time_t) -1;
8338
8339 errno = 0;
bd08c370 8340#if defined(INT64_T_IS_LONG)
8341 epoch = strtol (source_date_epoch, &endptr, 10);
8342#else
e3e8c48c 8343 epoch = strtoll (source_date_epoch, &endptr, 10);
bd08c370 8344#endif
dfa5c0d3 8345 if (errno != 0 || endptr == source_date_epoch || *endptr != '\0'
8346 || epoch < 0 || epoch > MAX_SOURCE_DATE_EPOCH)
8347 {
8348 error_at (input_location, "environment variable SOURCE_DATE_EPOCH must "
8349 "expand to a non-negative integer less than or equal to %wd",
8350 MAX_SOURCE_DATE_EPOCH);
8351 return (time_t) -1;
8352 }
e3e8c48c 8353
8354 return (time_t) epoch;
8355}
8356
5c8151fa 8357/* Callback for libcpp for offering spelling suggestions for misspelled
8358 directives. GOAL is an unrecognized string; CANDIDATES is a
8359 NULL-terminated array of candidate strings. Return the closest
8360 match to GOAL within CANDIDATES, or NULL if none are good
8361 suggestions. */
8362
8363const char *
8364cb_get_suggestion (cpp_reader *, const char *goal,
8365 const char *const *candidates)
8366{
8367 best_match<const char *, const char *> bm (goal);
8368 while (*candidates)
8369 bm.consider (*candidates++);
8370 return bm.get_best_meaningful_candidate ();
8371}
8372
c39beb8a 8373/* Return the latice point which is the wider of the two FLT_EVAL_METHOD
8374 modes X, Y. This isn't just >, as the FLT_EVAL_METHOD values added
8375 by C TS 18661-3 for interchange types that are computed in their
8376 native precision are larger than the C11 values for evaluating in the
8377 precision of float/double/long double. If either mode is
8378 FLT_EVAL_METHOD_UNPREDICTABLE, return that. */
8379
8380enum flt_eval_method
8381excess_precision_mode_join (enum flt_eval_method x,
8382 enum flt_eval_method y)
8383{
8384 if (x == FLT_EVAL_METHOD_UNPREDICTABLE
8385 || y == FLT_EVAL_METHOD_UNPREDICTABLE)
8386 return FLT_EVAL_METHOD_UNPREDICTABLE;
8387
8388 /* GCC only supports one interchange type right now, _Float16. If
8389 we're evaluating _Float16 in 16-bit precision, then flt_eval_method
8390 will be FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
8391 if (x == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
8392 return y;
8393 if (y == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
8394 return x;
8395
8396 /* Other values for flt_eval_method are directly comparable, and we want
8397 the maximum. */
8398 return MAX (x, y);
8399}
8400
8401/* Return the value that should be set for FLT_EVAL_METHOD in the
8402 context of ISO/IEC TS 18861-3.
8403
8404 This relates to the effective excess precision seen by the user,
8405 which is the join point of the precision the target requests for
8406 -fexcess-precision={standard,fast} and the implicit excess precision
8407 the target uses. */
8408
8409static enum flt_eval_method
8410c_ts18661_flt_eval_method (void)
8411{
8412 enum flt_eval_method implicit
8413 = targetm.c.excess_precision (EXCESS_PRECISION_TYPE_IMPLICIT);
8414
8415 enum excess_precision_type flag_type
8416 = (flag_excess_precision_cmdline == EXCESS_PRECISION_STANDARD
8417 ? EXCESS_PRECISION_TYPE_STANDARD
8418 : EXCESS_PRECISION_TYPE_FAST);
8419
8420 enum flt_eval_method requested
8421 = targetm.c.excess_precision (flag_type);
8422
8423 return excess_precision_mode_join (implicit, requested);
8424}
8425
8426/* As c_cpp_ts18661_flt_eval_method, but clamps the expected values to
8427 those that were permitted by C11. That is to say, eliminates
8428 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
8429
8430static enum flt_eval_method
8431c_c11_flt_eval_method (void)
8432{
8433 return excess_precision_mode_join (c_ts18661_flt_eval_method (),
8434 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT);
8435}
8436
8437/* Return the value that should be set for FLT_EVAL_METHOD.
8438 MAYBE_C11_ONLY_P is TRUE if we should check
8439 FLAG_PERMITTED_EVAL_METHODS as to whether we should limit the possible
8440 values we can return to those from C99/C11, and FALSE otherwise.
8441 See the comments on c_ts18661_flt_eval_method for what value we choose
8442 to set here. */
8443
8444int
8445c_flt_eval_method (bool maybe_c11_only_p)
8446{
8447 if (maybe_c11_only_p
8448 && flag_permitted_flt_eval_methods
8449 == PERMITTED_FLT_EVAL_METHODS_C11)
8450 return c_c11_flt_eval_method ();
8451 else
8452 return c_ts18661_flt_eval_method ();
8453}
8454
74578794 8455/* An enum for get_missing_token_insertion_kind for describing the best
8456 place to insert a missing token, if there is one. */
8457
8458enum missing_token_insertion_kind
8459{
8460 MTIK_IMPOSSIBLE,
8461 MTIK_INSERT_BEFORE_NEXT,
8462 MTIK_INSERT_AFTER_PREV
8463};
8464
8465/* Given a missing token of TYPE, determine if it is reasonable to
8466 emit a fix-it hint suggesting the insertion of the token, and,
8467 if so, where the token should be inserted relative to other tokens.
8468
8469 It only makes sense to do this for values of TYPE that are symbols.
8470
8471 Some symbols should go before the next token, e.g. in:
8472 if flag)
8473 we want to insert the missing '(' immediately before "flag",
8474 giving:
8475 if (flag)
8476 rather than:
8477 if( flag)
8478 These use MTIK_INSERT_BEFORE_NEXT.
8479
8480 Other symbols should go after the previous token, e.g. in:
8481 if (flag
8482 do_something ();
8483 we want to insert the missing ')' immediately after the "flag",
8484 giving:
8485 if (flag)
8486 do_something ();
8487 rather than:
8488 if (flag
8489 )do_something ();
8490 These use MTIK_INSERT_AFTER_PREV. */
8491
8492static enum missing_token_insertion_kind
8493get_missing_token_insertion_kind (enum cpp_ttype type)
8494{
8495 switch (type)
8496 {
8497 /* Insert missing "opening" brackets immediately
8498 before the next token. */
8499 case CPP_OPEN_SQUARE:
8500 case CPP_OPEN_PAREN:
8501 return MTIK_INSERT_BEFORE_NEXT;
8502
8503 /* Insert other missing symbols immediately after
8504 the previous token. */
8505 case CPP_CLOSE_PAREN:
8506 case CPP_CLOSE_SQUARE:
8507 case CPP_SEMICOLON:
8508 case CPP_COMMA:
8509 case CPP_COLON:
8510 return MTIK_INSERT_AFTER_PREV;
8511
8512 /* Other kinds of token don't get fix-it hints. */
8513 default:
8514 return MTIK_IMPOSSIBLE;
8515 }
8516}
8517
8518/* Given RICHLOC, a location for a diagnostic describing a missing token
8519 of kind TOKEN_TYPE, potentially add a fix-it hint suggesting the
8520 insertion of the token.
8521
8522 The location of the attempted fix-it hint depends on TOKEN_TYPE:
8523 it will either be:
8524 (a) immediately after PREV_TOKEN_LOC, or
8525
8526 (b) immediately before the primary location within RICHLOC (taken to
8527 be that of the token following where the token was expected).
8528
8529 If we manage to add a fix-it hint, then the location of the
8530 fix-it hint is likely to be more useful as the primary location
8531 of the diagnostic than that of the following token, so we swap
8532 these locations.
8533
8534 For example, given this bogus code:
8535 123456789012345678901234567890
8536 1 | int missing_semicolon (void)
8537 2 | {
8538 3 | return 42
8539 4 | }
8540
8541 we will emit:
8542
8543 "expected ';' before '}'"
8544
8545 RICHLOC's primary location is at the closing brace, so before "swapping"
8546 we would emit the error at line 4 column 1:
8547
8548 123456789012345678901234567890
8549 3 | return 42 |< fix-it hint emitted for this line
8550 | ; |
8551 4 | } |< "expected ';' before '}'" emitted at this line
8552 | ^ |
8553
8554 It's more useful for the location of the diagnostic to be at the
8555 fix-it hint, so we swap the locations, so the primary location
8556 is at the fix-it hint, with the old primary location inserted
8557 as a secondary location, giving this, with the error at line 3
8558 column 12:
8559
8560 123456789012345678901234567890
8561 3 | return 42 |< "expected ';' before '}'" emitted at this line,
8562 | ^ | with fix-it hint
8563 4 | ; |
8564 | } |< secondary range emitted here
8565 | ~ |. */
8566
8567void
8568maybe_suggest_missing_token_insertion (rich_location *richloc,
8569 enum cpp_ttype token_type,
8570 location_t prev_token_loc)
8571{
8572 gcc_assert (richloc);
8573
8574 enum missing_token_insertion_kind mtik
8575 = get_missing_token_insertion_kind (token_type);
8576
8577 switch (mtik)
8578 {
8579 default:
8580 gcc_unreachable ();
8581 break;
8582
8583 case MTIK_IMPOSSIBLE:
8584 return;
8585
8586 case MTIK_INSERT_BEFORE_NEXT:
8587 /* Attempt to add the fix-it hint before the primary location
8588 of RICHLOC. */
8589 richloc->add_fixit_insert_before (cpp_type2name (token_type, 0));
8590 break;
8591
8592 case MTIK_INSERT_AFTER_PREV:
8593 /* Attempt to add the fix-it hint after PREV_TOKEN_LOC. */
8594 richloc->add_fixit_insert_after (prev_token_loc,
8595 cpp_type2name (token_type, 0));
8596 break;
8597 }
8598
8599 /* If we were successful, use the fix-it hint's location as the
8600 primary location within RICHLOC, adding the old primary location
8601 back as a secondary location. */
8602 if (!richloc->seen_impossible_fixit_p ())
8603 {
8604 fixit_hint *hint = richloc->get_last_fixit_hint ();
8605 location_t hint_loc = hint->get_start_loc ();
8606 location_t old_loc = richloc->get_loc ();
8607
5fe20025 8608 richloc->set_range (0, hint_loc, SHOW_RANGE_WITH_CARET);
8609 richloc->add_range (old_loc);
74578794 8610 }
8611}
8612
7051d239 8613#if CHECKING_P
8614
8615namespace selftest {
8616
d76863c8 8617/* Verify that fold_for_warn on error_mark_node is safe. */
8618
8619static void
8620test_fold_for_warn ()
8621{
8622 ASSERT_EQ (error_mark_node, fold_for_warn (error_mark_node));
8623}
8624
8625/* Run all of the selftests within this file. */
8626
8627static void
8628c_common_c_tests ()
8629{
8630 test_fold_for_warn ();
8631}
8632
7051d239 8633/* Run all of the tests within c-family. */
8634
8635void
8636c_family_tests (void)
8637{
d76863c8 8638 c_common_c_tests ();
7051d239 8639 c_format_c_tests ();
6564acaf 8640 c_indentation_c_tests ();
d76863c8 8641 c_pretty_print_c_tests ();
08233401 8642 c_spellcheck_cc_tests ();
7051d239 8643}
8644
8645} // namespace selftest
8646
8647#endif /* #if CHECKING_P */
8648
7f9c8569 8649/* Attempt to locate a suitable location within FILE for a
8650 #include directive to be inserted before. FILE should
8651 be a string from libcpp (pointer equality is used).
8652 LOC is the location of the relevant diagnostic.
8653
8654 Attempt to return the location within FILE immediately
8655 after the last #include within that file, or the start of
8656 that file if it has no #include directives.
8657
8658 Return UNKNOWN_LOCATION if no suitable location is found,
8659 or if an error occurs. */
8660
8661static location_t
8662try_to_locate_new_include_insertion_point (const char *file, location_t loc)
8663{
8664 /* Locate the last ordinary map within FILE that ended with a #include. */
8665 const line_map_ordinary *last_include_ord_map = NULL;
8666
8667 /* ...and the next ordinary map within FILE after that one. */
8668 const line_map_ordinary *last_ord_map_after_include = NULL;
8669
8670 /* ...and the first ordinary map within FILE. */
8671 const line_map_ordinary *first_ord_map_in_file = NULL;
8672
8673 /* Get ordinary map containing LOC (or its expansion). */
8674 const line_map_ordinary *ord_map_for_loc = NULL;
8675 loc = linemap_resolve_location (line_table, loc, LRK_MACRO_EXPANSION_POINT,
8676 &ord_map_for_loc);
8677 gcc_assert (ord_map_for_loc);
8678
8679 for (unsigned int i = 0; i < LINEMAPS_ORDINARY_USED (line_table); i++)
8680 {
8681 const line_map_ordinary *ord_map
8682 = LINEMAPS_ORDINARY_MAP_AT (line_table, i);
8683
03410c5e 8684 if (const line_map_ordinary *from
8685 = linemap_included_from_linemap (line_table, ord_map))
7f9c8569 8686 if (from->to_file == file)
8687 {
8688 last_include_ord_map = from;
8689 last_ord_map_after_include = NULL;
8690 }
8691
8692 if (ord_map->to_file == file)
8693 {
8694 if (!first_ord_map_in_file)
8695 first_ord_map_in_file = ord_map;
8696 if (last_include_ord_map && !last_ord_map_after_include)
8697 last_ord_map_after_include = ord_map;
8698 }
8699
8700 /* Stop searching when reaching the ord_map containing LOC,
8701 as it makes no sense to provide fix-it hints that appear
8702 after the diagnostic in question. */
8703 if (ord_map == ord_map_for_loc)
8704 break;
8705 }
8706
8707 /* Determine where to insert the #include. */
8708 const line_map_ordinary *ord_map_for_insertion;
8709
8710 /* We want the next ordmap in the file after the last one that's a
8711 #include, but failing that, the start of the file. */
8712 if (last_ord_map_after_include)
8713 ord_map_for_insertion = last_ord_map_after_include;
8714 else
8715 ord_map_for_insertion = first_ord_map_in_file;
8716
8717 if (!ord_map_for_insertion)
8718 return UNKNOWN_LOCATION;
8719
8720 /* The "start_location" is column 0, meaning "the whole line".
8721 rich_location and edit_context can't cope with this, so use
8722 column 1 instead. */
8723 location_t col_0 = ord_map_for_insertion->start_location;
8724 return linemap_position_for_loc_and_offset (line_table, col_0, 1);
8725}
8726
8727/* A map from filenames to sets of headers added to them, for
8728 ensuring idempotency within maybe_add_include_fixit. */
8729
8730/* The values within the map. We need string comparison as there's
8731 no guarantee that two different diagnostics that are recommending
8732 adding e.g. "<stdio.h>" are using the same buffer. */
8733
067e9a50 8734typedef hash_set <const char *, false, nofree_string_hash> per_file_includes_t;
7f9c8569 8735
8736/* The map itself. We don't need string comparison for the filename keys,
8737 as they come from libcpp. */
8738
8739typedef hash_map <const char *, per_file_includes_t *> added_includes_t;
8740static added_includes_t *added_includes;
8741
8742/* Attempt to add a fix-it hint to RICHLOC, adding "#include HEADER\n"
8743 in a suitable location within the file of RICHLOC's primary
8744 location.
8745
8746 This function is idempotent: a header will be added at most once to
5fe20025 8747 any given file.
8748
8749 If OVERRIDE_LOCATION is true, then if a fix-it is added and will be
8750 printed, then RICHLOC's primary location will be replaced by that of
8751 the fix-it hint (for use by "inform" notes where the location of the
8752 issue has already been reported). */
7f9c8569 8753
8754void
5fe20025 8755maybe_add_include_fixit (rich_location *richloc, const char *header,
8756 bool override_location)
7f9c8569 8757{
8758 location_t loc = richloc->get_loc ();
8759 const char *file = LOCATION_FILE (loc);
8760 if (!file)
8761 return;
8762
8763 /* Idempotency: don't add the same header more than once to a given file. */
8764 if (!added_includes)
8765 added_includes = new added_includes_t ();
8766 per_file_includes_t *&set = added_includes->get_or_insert (file);
8767 if (set)
8768 if (set->contains (header))
8769 /* ...then we've already added HEADER to that file. */
8770 return;
8771 if (!set)
8772 set = new per_file_includes_t ();
8773 set->add (header);
8774
8775 /* Attempt to locate a suitable place for the new directive. */
8776 location_t include_insert_loc
8777 = try_to_locate_new_include_insertion_point (file, loc);
8778 if (include_insert_loc == UNKNOWN_LOCATION)
8779 return;
8780
8781 char *text = xasprintf ("#include %s\n", header);
8782 richloc->add_fixit_insert_before (include_insert_loc, text);
8783 free (text);
5fe20025 8784
8785 if (override_location && global_dc->show_caret)
8786 {
8787 /* Replace the primary location with that of the insertion point for the
8788 fix-it hint.
8789
8790 We use SHOW_LINES_WITHOUT_RANGE so that we don't meaningless print a
8791 caret for the insertion point (or colorize it).
8792
8793 Hence we print e.g.:
8794
8795 ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
8796 73 | # include <debug/vector>
8797 +++ |+#include <vector>
8798 74 | #endif
8799
8800 rather than:
8801
8802 ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
8803 73 | # include <debug/vector>
8804 +++ |+#include <vector>
8805 74 | #endif
8806 | ^
8807
8808 avoiding the caret on the first column of line 74. */
8809 richloc->set_range (0, include_insert_loc, SHOW_LINES_WITHOUT_RANGE);
8810 }
7f9c8569 8811}
8812
b2fc0527 8813/* Attempt to convert a braced array initializer list CTOR for array
d839099f 8814 TYPE into a STRING_CST for convenience and efficiency. Return
8815 the converted string on success or the original ctor on failure. */
b2fc0527 8816
dc0cf270 8817static tree
d839099f 8818braced_list_to_string (tree type, tree ctor)
b2fc0527 8819{
d839099f 8820 if (!tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
8821 return ctor;
b2fc0527 8822
8823 /* If the array has an explicit bound, use it to constrain the size
8824 of the string. If it doesn't, be sure to create a string that's
8825 as long as implied by the index of the last zero specified via
8826 a designator, as in:
8827 const char a[] = { [7] = 0 }; */
d839099f 8828 unsigned HOST_WIDE_INT maxelts = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8829 maxelts /= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
b2fc0527 8830
d839099f 8831 /* Avoid converting initializers for zero-length arrays. */
8832 if (!maxelts)
8833 return ctor;
b2fc0527 8834
d839099f 8835 unsigned HOST_WIDE_INT nelts = CONSTRUCTOR_NELTS (ctor);
b2fc0527 8836
8837 auto_vec<char> str;
8838 str.reserve (nelts + 1);
8839
8840 unsigned HOST_WIDE_INT i;
8841 tree index, value;
8842
8843 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, index, value)
8844 {
d839099f 8845 unsigned HOST_WIDE_INT idx = i;
8846 if (index)
8847 {
8848 if (!tree_fits_uhwi_p (index))
8849 return ctor;
8850 idx = tree_to_uhwi (index);
8851 }
b2fc0527 8852
8853 /* auto_vec is limited to UINT_MAX elements. */
8854 if (idx > UINT_MAX)
d839099f 8855 return ctor;
b2fc0527 8856
d839099f 8857 /* Avoid non-constant initializers. */
b2fc0527 8858 if (!tree_fits_shwi_p (value))
d839099f 8859 return ctor;
b2fc0527 8860
8861 /* Skip over embedded nuls except the last one (initializer
8862 elements are in ascending order of indices). */
8863 HOST_WIDE_INT val = tree_to_shwi (value);
8864 if (!val && i + 1 < nelts)
8865 continue;
8866
d839099f 8867 if (idx < str.length())
8868 return ctor;
8869
b2fc0527 8870 /* Bail if the CTOR has a block of more than 256 embedded nuls
8871 due to implicitly initialized elements. */
8872 unsigned nchars = (idx - str.length ()) + 1;
8873 if (nchars > 256)
d839099f 8874 return ctor;
b2fc0527 8875
8876 if (nchars > 1)
8877 {
8878 str.reserve (idx);
8879 str.quick_grow_cleared (idx);
8880 }
8881
d839099f 8882 if (idx >= maxelts)
8883 return ctor;
b2fc0527 8884
8885 str.safe_insert (idx, val);
8886 }
8887
d839099f 8888 /* Append a nul string termination. */
8889 if (str.length () < maxelts)
b2fc0527 8890 str.safe_push (0);
8891
d839099f 8892 /* Build a STRING_CST with the same type as the array. */
b2fc0527 8893 tree res = build_string (str.length (), str.begin ());
8894 TREE_TYPE (res) = type;
8895 return res;
8896}
8897
dc0cf270 8898/* Attempt to convert a CTOR containing braced array initializer lists
8899 for array TYPE into one containing STRING_CSTs, for convenience and
8900 efficiency. Recurse for arrays of arrays and member initializers.
8901 Return the converted CTOR or STRING_CST on success or the original
8902 CTOR otherwise. */
8903
8904tree
8905braced_lists_to_strings (tree type, tree ctor)
8906{
8907 if (TREE_CODE (ctor) != CONSTRUCTOR)
8908 return ctor;
8909
8910 tree_code code = TREE_CODE (type);
8911
8912 tree ttp;
8913 if (code == ARRAY_TYPE)
8914 ttp = TREE_TYPE (type);
8915 else if (code == RECORD_TYPE)
8916 {
8917 ttp = TREE_TYPE (ctor);
8918 if (TREE_CODE (ttp) == ARRAY_TYPE)
8919 {
8920 type = ttp;
8921 ttp = TREE_TYPE (ttp);
8922 }
8923 }
8924 else
8925 return ctor;
8926
8927 if (TYPE_STRING_FLAG (ttp))
8928 return braced_list_to_string (type, ctor);
8929
8930 code = TREE_CODE (ttp);
8931 if (code == ARRAY_TYPE || code == RECORD_TYPE)
8932 {
8933 /* Handle array of arrays or struct member initializers. */
8934 tree val;
8935 unsigned HOST_WIDE_INT idx;
8936 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), idx, val)
8937 {
8938 val = braced_lists_to_strings (ttp, val);
8939 CONSTRUCTOR_ELT (ctor, idx)->value = val;
8940 }
8941 }
8942
8943 return ctor;
8944}
8945
7bedc3a0 8946#include "gt-c-family-c-common.h"