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