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