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