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

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