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

source code of gcc/c-family/c-common.cc