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