Debian Codegraph @

gcc/c-family/c-common.cc

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