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