Branch data Line data Source code
1 : : /* Subroutines shared by all languages that are variants of C.
2 : : Copyright (C) 1992-2025 Free Software Foundation, Inc.
3 : :
4 : : This file is part of GCC.
5 : :
6 : : GCC is free software; you can redistribute it and/or modify it under
7 : : the terms of the GNU General Public License as published by the Free
8 : : Software Foundation; either version 3, or (at your option) any later
9 : : version.
10 : :
11 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : : for more details.
15 : :
16 : : You should have received a copy of the GNU General Public License
17 : : along with GCC; see the file COPYING3. If not see
18 : : <http://www.gnu.org/licenses/>. */
19 : :
20 : : #define GCC_C_COMMON_C
21 : :
22 : : #include "config.h"
23 : : #include "system.h"
24 : : #include "coretypes.h"
25 : : #include "target.h"
26 : : #include "function.h"
27 : : #include "tree.h"
28 : : #include "memmodel.h"
29 : : #include "c-common.h"
30 : : #include "gimple-expr.h"
31 : : #include "tm_p.h"
32 : : #include "stringpool.h"
33 : : #include "cgraph.h"
34 : : #include "diagnostic.h"
35 : : #include "intl.h"
36 : : #include "stor-layout.h"
37 : : #include "calls.h"
38 : : #include "attribs.h"
39 : : #include "varasm.h"
40 : : #include "trans-mem.h"
41 : : #include "c-objc.h"
42 : : #include "common/common-target.h"
43 : : #include "langhooks.h"
44 : : #include "tree-inline.h"
45 : : #include "toplev.h"
46 : : #include "tree-iterator.h"
47 : : #include "opts.h"
48 : : #include "gimplify.h"
49 : : #include "substring-locations.h"
50 : : #include "spellcheck.h"
51 : : #include "c-spellcheck.h"
52 : : #include "selftest.h"
53 : : #include "debug.h"
54 : : #include "tree-vector-builder.h"
55 : : #include "vec-perm-indices.h"
56 : : #include "tree-pretty-print-markup.h"
57 : : #include "gcc-rich-location.h"
58 : : #include "gcc-urlifier.h"
59 : : #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_post_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 : : { "_Atomic", RID_ATOMIC, D_CONLY },
401 : : { "_BitInt", RID_BITINT, D_CONLY },
402 : : { "_Bool", RID_BOOL, D_CONLY },
403 : : { "_Complex", RID_COMPLEX, 0 },
404 : : { "_Imaginary", RID_IMAGINARY, D_CONLY },
405 : : { "_Float16", RID_FLOAT16, 0 },
406 : : { "_Float32", RID_FLOAT32, 0 },
407 : : { "_Float64", RID_FLOAT64, 0 },
408 : : { "_Float128", RID_FLOAT128, 0 },
409 : : { "_Float32x", RID_FLOAT32X, 0 },
410 : : { "_Float64x", RID_FLOAT64X, 0 },
411 : : { "_Float128x", RID_FLOAT128X, 0 },
412 : : { "_Decimal32", RID_DFLOAT32, D_CONLY },
413 : : { "_Decimal64", RID_DFLOAT64, D_CONLY },
414 : : { "_Decimal128", RID_DFLOAT128, D_CONLY },
415 : : { "_Decimal64x", RID_DFLOAT64X, D_CONLY },
416 : : { "_Fract", RID_FRACT, D_CONLY | D_EXT },
417 : : { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
418 : : { "_Sat", RID_SAT, D_CONLY | D_EXT },
419 : : { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
420 : : { "_Noreturn", RID_NORETURN, D_CONLY },
421 : : { "_Generic", RID_GENERIC, D_CONLY },
422 : : { "_Thread_local", RID_THREAD, D_CONLY },
423 : : { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
424 : : { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
425 : : { "__alignof", RID_ALIGNOF, 0 },
426 : : { "__alignof__", RID_ALIGNOF, 0 },
427 : : { "__asm", RID_ASM, 0 },
428 : : { "__asm__", RID_ASM, 0 },
429 : : { "__attribute", RID_ATTRIBUTE, 0 },
430 : : { "__attribute__", RID_ATTRIBUTE, 0 },
431 : : { "__auto_type", RID_AUTO_TYPE, D_CONLY },
432 : : { "__builtin_addressof", RID_ADDRESSOF, D_CXXONLY },
433 : : { "__builtin_assoc_barrier", RID_BUILTIN_ASSOC_BARRIER, 0 },
434 : : { "__builtin_bit_cast", RID_BUILTIN_BIT_CAST, D_CXXONLY },
435 : : { "__builtin_call_with_static_chain",
436 : : RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
437 : : { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
438 : : { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
439 : : { "__builtin_convertvector", RID_BUILTIN_CONVERTVECTOR, 0 },
440 : : { "__builtin_counted_by_ref", RID_BUILTIN_COUNTED_BY_REF, D_CONLY },
441 : : { "__builtin_has_attribute", RID_BUILTIN_HAS_ATTRIBUTE, 0 },
442 : : { "__builtin_launder", RID_BUILTIN_LAUNDER, D_CXXONLY },
443 : : { "__builtin_operator_new", RID_BUILTIN_OPERATOR_NEW, D_CXXONLY },
444 : : { "__builtin_operator_delete", RID_BUILTIN_OPERATOR_DELETE, D_CXXONLY },
445 : : { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
446 : : { "__builtin_shufflevector", RID_BUILTIN_SHUFFLEVECTOR, 0 },
447 : : { "__builtin_stdc_bit_ceil", RID_BUILTIN_STDC, D_CONLY },
448 : : { "__builtin_stdc_bit_floor", RID_BUILTIN_STDC, D_CONLY },
449 : : { "__builtin_stdc_bit_width", RID_BUILTIN_STDC, D_CONLY },
450 : : { "__builtin_stdc_count_ones", RID_BUILTIN_STDC, D_CONLY },
451 : : { "__builtin_stdc_count_zeros", RID_BUILTIN_STDC, D_CONLY },
452 : : { "__builtin_stdc_first_leading_one", RID_BUILTIN_STDC, D_CONLY },
453 : : { "__builtin_stdc_first_leading_zero", RID_BUILTIN_STDC, D_CONLY },
454 : : { "__builtin_stdc_first_trailing_one", RID_BUILTIN_STDC, D_CONLY },
455 : : { "__builtin_stdc_first_trailing_zero", RID_BUILTIN_STDC, D_CONLY },
456 : : { "__builtin_stdc_has_single_bit", RID_BUILTIN_STDC, D_CONLY },
457 : : { "__builtin_stdc_leading_ones", RID_BUILTIN_STDC, D_CONLY },
458 : : { "__builtin_stdc_leading_zeros", RID_BUILTIN_STDC, D_CONLY },
459 : : { "__builtin_stdc_rotate_left", RID_BUILTIN_STDC, D_CONLY },
460 : : { "__builtin_stdc_rotate_right", RID_BUILTIN_STDC, D_CONLY },
461 : : { "__builtin_stdc_trailing_ones", RID_BUILTIN_STDC, D_CONLY },
462 : : { "__builtin_stdc_trailing_zeros", RID_BUILTIN_STDC, D_CONLY },
463 : : { "__builtin_tgmath", RID_BUILTIN_TGMATH, D_CONLY },
464 : : { "__builtin_offsetof", RID_OFFSETOF, 0 },
465 : : { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
466 : : { "__builtin_c23_va_start", RID_C23_VA_START, D_C23 | D_CXX26 },
467 : : { "__builtin_va_arg", RID_VA_ARG, 0 },
468 : : { "__complex", RID_COMPLEX, 0 },
469 : : { "__complex__", RID_COMPLEX, 0 },
470 : : { "__const", RID_CONST, 0 },
471 : : { "__const__", RID_CONST, 0 },
472 : : { "__constinit", RID_CONSTINIT, D_CXXONLY },
473 : : { "__decltype", RID_DECLTYPE, D_CXXONLY },
474 : : { "__extension__", RID_EXTENSION, 0 },
475 : : { "__func__", RID_C99_FUNCTION_NAME, 0 },
476 : : { "__imag", RID_IMAGPART, 0 },
477 : : { "__imag__", RID_IMAGPART, 0 },
478 : : { "__inline", RID_INLINE, 0 },
479 : : { "__inline__", RID_INLINE, 0 },
480 : : { "__label__", RID_LABEL, 0 },
481 : : { "__null", RID_NULL, 0 },
482 : : { "__real", RID_REALPART, 0 },
483 : : { "__real__", RID_REALPART, 0 },
484 : : { "__restrict", RID_RESTRICT, 0 },
485 : : { "__restrict__", RID_RESTRICT, 0 },
486 : : { "__signed", RID_SIGNED, 0 },
487 : : { "__signed__", RID_SIGNED, 0 },
488 : : { "__thread", RID_THREAD, 0 },
489 : : { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
490 : : { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
491 : : { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
492 : : { "__typeof", RID_TYPEOF, 0 },
493 : : { "__typeof__", RID_TYPEOF, 0 },
494 : : { "__typeof_unqual", RID_TYPEOF_UNQUAL, D_CONLY },
495 : : { "__typeof_unqual__", RID_TYPEOF_UNQUAL, D_CONLY },
496 : : { "__volatile", RID_VOLATILE, 0 },
497 : : { "__volatile__", RID_VOLATILE, 0 },
498 : : { "__GIMPLE", RID_GIMPLE, D_CONLY },
499 : : { "__PHI", RID_PHI, D_CONLY },
500 : : { "__RTL", RID_RTL, D_CONLY },
501 : : { "alignas", RID_ALIGNAS, D_C23 | D_CXX11 | D_CXXWARN },
502 : : { "alignof", RID_ALIGNOF, D_C23 | D_CXX11 | D_CXXWARN },
503 : : { "asm", RID_ASM, D_ASM },
504 : : { "auto", RID_AUTO, 0 },
505 : : { "bool", RID_BOOL, D_C23 | D_CXXWARN },
506 : : { "break", RID_BREAK, 0 },
507 : : { "case", RID_CASE, 0 },
508 : : { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
509 : : { "char", RID_CHAR, 0 },
510 : : { "char8_t", RID_CHAR8, D_CXX_CHAR8_T_FLAGS | D_CXXWARN },
511 : : { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
512 : : { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
513 : : { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
514 : : { "const", RID_CONST, 0 },
515 : : { "consteval", RID_CONSTEVAL, D_CXXONLY | D_CXX20 | D_CXXWARN },
516 : : { "constexpr", RID_CONSTEXPR, D_C23 | D_CXX11 | D_CXXWARN },
517 : : { "constinit", RID_CONSTINIT, D_CXXONLY | D_CXX20 | D_CXXWARN },
518 : : { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
519 : : { "continue", RID_CONTINUE, 0 },
520 : : { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
521 : : { "default", RID_DEFAULT, 0 },
522 : : { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
523 : : { "do", RID_DO, 0 },
524 : : { "double", RID_DOUBLE, 0 },
525 : : { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
526 : : { "else", RID_ELSE, 0 },
527 : : { "enum", RID_ENUM, 0 },
528 : : { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
529 : : { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
530 : : { "extern", RID_EXTERN, 0 },
531 : : { "false", RID_FALSE, D_C23 | D_CXXWARN },
532 : : { "float", RID_FLOAT, 0 },
533 : : { "for", RID_FOR, 0 },
534 : : { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
535 : : { "goto", RID_GOTO, 0 },
536 : : { "if", RID_IF, 0 },
537 : : { "inline", RID_INLINE, D_EXT89 },
538 : : { "int", RID_INT, 0 },
539 : : { "long", RID_LONG, 0 },
540 : : { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
541 : : { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
542 : : { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
543 : : { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
544 : : { "nullptr", RID_NULLPTR, D_C23 | D_CXX11 | D_CXXWARN },
545 : : { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
546 : : { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
547 : : { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
548 : : { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
549 : : { "register", RID_REGISTER, 0 },
550 : : { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
551 : : { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
552 : : { "return", RID_RETURN, 0 },
553 : : { "short", RID_SHORT, 0 },
554 : : { "signed", RID_SIGNED, 0 },
555 : : { "sizeof", RID_SIZEOF, 0 },
556 : : { "static", RID_STATIC, 0 },
557 : : { "static_assert", RID_STATIC_ASSERT, D_C23 | D_CXX11 | D_CXXWARN },
558 : : { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
559 : : { "struct", RID_STRUCT, 0 },
560 : : { "switch", RID_SWITCH, 0 },
561 : : { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
562 : : { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
563 : : { "thread_local", RID_THREAD, D_C23 | D_CXX11 | D_CXXWARN },
564 : : { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
565 : : { "true", RID_TRUE, D_C23 | D_CXXWARN },
566 : : { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
567 : : { "typedef", RID_TYPEDEF, 0 },
568 : : { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
569 : : { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
570 : : { "typeof", RID_TYPEOF, D_EXT11 },
571 : : { "typeof_unqual", RID_TYPEOF_UNQUAL, D_CONLY | D_C23 },
572 : : { "union", RID_UNION, 0 },
573 : : { "unsigned", RID_UNSIGNED, 0 },
574 : : { "using", RID_USING, D_CXXONLY | D_CXXWARN },
575 : : { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
576 : : { "void", RID_VOID, 0 },
577 : : { "volatile", RID_VOLATILE, 0 },
578 : : { "wchar_t", RID_WCHAR, D_CXXONLY },
579 : : { "while", RID_WHILE, 0 },
580 : :
581 : : /* C++ transactional memory. */
582 : : { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
583 : : { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
584 : : { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
585 : : { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
586 : :
587 : : /* Concepts-related keywords */
588 : : { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
589 : : { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
590 : :
591 : : /* Modules-related keywords, these are internal unspellable tokens,
592 : : created by the preprocessor. */
593 : : { "module ", RID__MODULE, D_CXX_MODULES_FLAGS | D_CXXWARN },
594 : : { "import ", RID__IMPORT, D_CXX_MODULES_FLAGS | D_CXXWARN },
595 : : { "export ", RID__EXPORT, D_CXX_MODULES_FLAGS | D_CXXWARN },
596 : :
597 : : /* Coroutines-related keywords */
598 : : { "co_await", RID_CO_AWAIT, D_CXX_COROUTINES_FLAGS | D_CXXWARN },
599 : : { "co_yield", RID_CO_YIELD, D_CXX_COROUTINES_FLAGS | D_CXXWARN },
600 : : { "co_return", RID_CO_RETURN, D_CXX_COROUTINES_FLAGS | D_CXXWARN },
601 : :
602 : : /* These Objective-C keywords are recognized only immediately after
603 : : an '@'. */
604 : : { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
605 : : { "defs", RID_AT_DEFS, D_OBJC },
606 : : { "encode", RID_AT_ENCODE, D_OBJC },
607 : : { "end", RID_AT_END, D_OBJC },
608 : : { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
609 : : { "interface", RID_AT_INTERFACE, D_OBJC },
610 : : { "protocol", RID_AT_PROTOCOL, D_OBJC },
611 : : { "selector", RID_AT_SELECTOR, D_OBJC },
612 : : { "finally", RID_AT_FINALLY, D_OBJC },
613 : : { "optional", RID_AT_OPTIONAL, D_OBJC },
614 : : { "required", RID_AT_REQUIRED, D_OBJC },
615 : : { "property", RID_AT_PROPERTY, D_OBJC },
616 : : { "package", RID_AT_PACKAGE, D_OBJC },
617 : : { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
618 : : { "dynamic", RID_AT_DYNAMIC, D_OBJC },
619 : : /* These are recognized only in protocol-qualifier context
620 : : (see above) */
621 : : { "bycopy", RID_BYCOPY, D_OBJC },
622 : : { "byref", RID_BYREF, D_OBJC },
623 : : { "in", RID_IN, D_OBJC },
624 : : { "inout", RID_INOUT, D_OBJC },
625 : : { "oneway", RID_ONEWAY, D_OBJC },
626 : : { "out", RID_OUT, D_OBJC },
627 : : /* These are recognized inside a property attribute list */
628 : : { "assign", RID_ASSIGN, D_OBJC },
629 : : { "atomic", RID_PROPATOMIC, D_OBJC },
630 : : { "copy", RID_COPY, D_OBJC },
631 : : { "getter", RID_GETTER, D_OBJC },
632 : : { "nonatomic", RID_NONATOMIC, D_OBJC },
633 : : { "readonly", RID_READONLY, D_OBJC },
634 : : { "readwrite", RID_READWRITE, D_OBJC },
635 : : { "retain", RID_RETAIN, D_OBJC },
636 : : { "setter", RID_SETTER, D_OBJC },
637 : : /* These are Objective C implementation of nullability, accepted only in
638 : : specific contexts. */
639 : : { "null_unspecified", RID_NULL_UNSPECIFIED, D_OBJC },
640 : : { "nullable", RID_NULLABLE, D_OBJC },
641 : : { "nonnull", RID_NONNULL, D_OBJC },
642 : : { "null_resettable", RID_NULL_RESETTABLE, D_OBJC },
643 : : };
644 : :
645 : : const unsigned int num_c_common_reswords = ARRAY_SIZE (c_common_reswords);
646 : :
647 : : /* Return identifier for address space AS. */
648 : :
649 : : const char *
650 : 3 : c_addr_space_name (addr_space_t as)
651 : : {
652 : 3 : int rid = RID_FIRST_ADDR_SPACE + as;
653 : 3 : gcc_assert (ridpointers [rid]);
654 : 3 : return IDENTIFIER_POINTER (ridpointers [rid]);
655 : : }
656 : :
657 : : /* Push current bindings for the function name VAR_DECLS. */
658 : :
659 : : void
660 : 180332676 : start_fname_decls (void)
661 : : {
662 : 180332676 : unsigned ix;
663 : 180332676 : tree saved = NULL_TREE;
664 : :
665 : 721330704 : for (ix = 0; fname_vars[ix].decl; ix++)
666 : : {
667 : 540998028 : tree decl = *fname_vars[ix].decl;
668 : :
669 : 540998028 : if (decl)
670 : : {
671 : 52609 : saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
672 : : saved);
673 : 52609 : *fname_vars[ix].decl = NULL_TREE;
674 : : }
675 : : }
676 : 180332676 : if (saved || saved_function_name_decls)
677 : : /* Normally they'll have been NULL, so only push if we've got a
678 : : stack, or they are non-NULL. */
679 : 72163 : saved_function_name_decls = tree_cons (saved, NULL_TREE,
680 : : saved_function_name_decls);
681 : 180332676 : }
682 : :
683 : : /* Finish up the current bindings, adding them into the current function's
684 : : statement tree. This must be done _before_ finish_stmt_tree is called.
685 : : If there is no current function, we must be at file scope and no statements
686 : : are involved. Pop the previous bindings. */
687 : :
688 : : void
689 : 180217674 : finish_fname_decls (void)
690 : : {
691 : 180217674 : unsigned ix;
692 : 180217674 : tree stmts = NULL_TREE;
693 : 180217674 : tree stack = saved_function_name_decls;
694 : :
695 : 180485437 : for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
696 : 97800 : append_to_statement_list (TREE_VALUE (stack), &stmts);
697 : :
698 : 180217674 : if (stmts)
699 : : {
700 : 97695 : tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
701 : :
702 : 97695 : if (TREE_CODE (*bodyp) == BIND_EXPR)
703 : 93767 : bodyp = &BIND_EXPR_BODY (*bodyp);
704 : :
705 : 97695 : append_to_statement_list_force (*bodyp, &stmts);
706 : 97695 : *bodyp = stmts;
707 : : }
708 : :
709 : 720870696 : for (ix = 0; fname_vars[ix].decl; ix++)
710 : 540653022 : *fname_vars[ix].decl = NULL_TREE;
711 : :
712 : 180217674 : if (stack)
713 : : {
714 : : /* We had saved values, restore them. */
715 : 72163 : tree saved;
716 : :
717 : 124772 : for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
718 : : {
719 : 52609 : tree decl = TREE_PURPOSE (saved);
720 : 52609 : unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
721 : :
722 : 52609 : *fname_vars[ix].decl = decl;
723 : : }
724 : 72163 : stack = TREE_CHAIN (stack);
725 : : }
726 : 180217674 : saved_function_name_decls = stack;
727 : 180217674 : }
728 : :
729 : : /* Return the text name of the current function, suitably prettified
730 : : by PRETTY_P. Return string must be freed by caller. */
731 : :
732 : : const char *
733 : 3507 : fname_as_string (int pretty_p)
734 : : {
735 : 3507 : const char *name = "top level";
736 : 3507 : char *namep;
737 : 3507 : int vrb = 2, len;
738 : 3507 : cpp_string cstr = { 0, 0 }, strname;
739 : :
740 : 3507 : if (!pretty_p)
741 : : {
742 : 1896 : name = "";
743 : 1896 : vrb = 0;
744 : : }
745 : :
746 : 3507 : if (current_function_decl)
747 : 3500 : name = lang_hooks.decl_printable_name (current_function_decl, vrb);
748 : :
749 : 3507 : len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
750 : :
751 : 3507 : namep = XNEWVEC (char, len);
752 : 3507 : snprintf (namep, len, "\"%s\"", name);
753 : 3507 : strname.text = (unsigned char *) namep;
754 : 3507 : strname.len = len - 1;
755 : :
756 : 3507 : if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
757 : : {
758 : 3507 : XDELETEVEC (namep);
759 : 3507 : return (const char *) cstr.text;
760 : : }
761 : :
762 : : return namep;
763 : : }
764 : :
765 : : /* Return the VAR_DECL for a const char array naming the current
766 : : function. If the VAR_DECL has not yet been created, create it
767 : : now. RID indicates how it should be formatted and IDENTIFIER_NODE
768 : : ID is its name (unfortunately C and C++ hold the RID values of
769 : : keywords in different places, so we can't derive RID from ID in
770 : : this language independent code. LOC is the location of the
771 : : function. */
772 : :
773 : : tree
774 : 197880 : fname_decl (location_t loc, unsigned int rid, tree id)
775 : : {
776 : 197880 : unsigned ix;
777 : 197880 : tree decl = NULL_TREE;
778 : :
779 : 572847 : for (ix = 0; fname_vars[ix].decl; ix++)
780 : 572847 : if (fname_vars[ix].rid == rid)
781 : : break;
782 : :
783 : 197880 : decl = *fname_vars[ix].decl;
784 : 197880 : if (!decl)
785 : : {
786 : : /* If a tree is built here, it would normally have the lineno of
787 : : the current statement. Later this tree will be moved to the
788 : : beginning of the function and this line number will be wrong.
789 : : To avoid this problem set the lineno to 0 here; that prevents
790 : : it from appearing in the RTL. */
791 : 97800 : tree stmts;
792 : 97800 : location_t saved_location = input_location;
793 : 97800 : input_location = UNKNOWN_LOCATION;
794 : :
795 : 97800 : stmts = push_stmt_list ();
796 : 97800 : decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
797 : 97800 : stmts = pop_stmt_list (stmts);
798 : 97800 : if (!IS_EMPTY_STMT (stmts))
799 : 97800 : saved_function_name_decls
800 : 97800 : = tree_cons (decl, stmts, saved_function_name_decls);
801 : 97800 : *fname_vars[ix].decl = decl;
802 : 97800 : input_location = saved_location;
803 : : }
804 : 197880 : if (!ix && !current_function_decl)
805 : 4 : pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
806 : :
807 : 197880 : return decl;
808 : : }
809 : :
810 : : /* Given a STRING_CST, give it a suitable array-of-chars data type. */
811 : :
812 : : tree
813 : 26003235 : fix_string_type (tree value)
814 : : {
815 : 26003235 : int length = TREE_STRING_LENGTH (value);
816 : 26003235 : int nchars, charsz;
817 : 26003235 : tree e_type, i_type, a_type;
818 : :
819 : : /* Compute the number of elements, for the array type. */
820 : 26003235 : if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
821 : : {
822 : 25737595 : charsz = 1;
823 : 25737595 : e_type = char_type_node;
824 : : }
825 : 265640 : else if (flag_char8_t && TREE_TYPE (value) == char8_array_type_node)
826 : : {
827 : 389 : charsz = TYPE_PRECISION (char8_type_node) / BITS_PER_UNIT;
828 : 389 : e_type = char8_type_node;
829 : : }
830 : 265251 : else if (TREE_TYPE (value) == char16_array_type_node)
831 : : {
832 : 956 : charsz = TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT;
833 : 956 : e_type = char16_type_node;
834 : : }
835 : 264295 : else if (TREE_TYPE (value) == char32_array_type_node)
836 : : {
837 : 3044 : charsz = TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT;
838 : 3044 : e_type = char32_type_node;
839 : : }
840 : : else
841 : : {
842 : 261251 : charsz = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
843 : 261251 : e_type = wchar_type_node;
844 : : }
845 : :
846 : : /* This matters only for targets where ssizetype has smaller precision
847 : : than 32 bits. */
848 : 26003235 : if (wi::lts_p (wi::to_wide (TYPE_MAX_VALUE (ssizetype)), length))
849 : : {
850 : 0 : error ("size of string literal is too large");
851 : 0 : length = tree_to_shwi (TYPE_MAX_VALUE (ssizetype)) / charsz * charsz;
852 : 0 : char *str = CONST_CAST (char *, TREE_STRING_POINTER (value));
853 : 0 : memset (str + length, '\0',
854 : 0 : MIN (TREE_STRING_LENGTH (value) - length, charsz));
855 : 0 : TREE_STRING_LENGTH (value) = length;
856 : : }
857 : 26003235 : nchars = length / charsz;
858 : :
859 : : /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
860 : : limit in C++98 Annex B is very large (65536) and is not normative,
861 : : so we do not diagnose it (warn_overlength_strings is forced off
862 : : in c_common_post_options). */
863 : 26003235 : if (warn_overlength_strings)
864 : : {
865 : 15086 : const int nchars_max = flag_isoc99 ? 4095 : 509;
866 : 15086 : const int relevant_std = flag_isoc99 ? 99 : 90;
867 : 15086 : if (nchars - 1 > nchars_max)
868 : : /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
869 : : separate the %d from the 'C'. 'ISO' should not be
870 : : translated, but it may be moved after 'C%d' in languages
871 : : where modifiers follow nouns. */
872 : 5 : pedwarn (input_location, OPT_Woverlength_strings,
873 : : "string length %qd is greater than the length %qd "
874 : : "ISO C%d compilers are required to support",
875 : : nchars - 1, nchars_max, relevant_std);
876 : : }
877 : :
878 : : /* Create the array type for the string constant. The ISO C++
879 : : standard says that a string literal has type `const char[N]' or
880 : : `const wchar_t[N]'. We use the same logic when invoked as a C
881 : : front-end with -Wwrite-strings.
882 : : ??? We should change the type of an expression depending on the
883 : : state of a warning flag. We should just be warning -- see how
884 : : this is handled in the C++ front-end for the deprecated implicit
885 : : conversion from string literals to `char*' or `wchar_t*'.
886 : :
887 : : The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
888 : : array type being the unqualified version of that type.
889 : : Therefore, if we are constructing an array of const char, we must
890 : : construct the matching unqualified array type first. The C front
891 : : end does not require this, but it does no harm, so we do it
892 : : unconditionally. */
893 : 26003235 : i_type = build_index_type (size_int (nchars - 1));
894 : 26003235 : a_type = build_array_type (e_type, i_type);
895 : 26003235 : if (c_dialect_cxx() || warn_write_strings)
896 : 23642806 : a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
897 : :
898 : 26003235 : TREE_TYPE (value) = a_type;
899 : 26003235 : TREE_CONSTANT (value) = 1;
900 : 26003235 : TREE_READONLY (value) = 1;
901 : 26003235 : TREE_STATIC (value) = 1;
902 : 26003235 : return value;
903 : : }
904 : :
905 : : /* Given a string of type STRING_TYPE, determine what kind of string
906 : : token would give an equivalent execution encoding: CPP_STRING,
907 : : CPP_STRING16, or CPP_STRING32. Return CPP_OTHER in case of error.
908 : : This may not be exactly the string token type that initially created
909 : : the string, since CPP_WSTRING is indistinguishable from the 16/32 bit
910 : : string type, and CPP_UTF8STRING is indistinguishable from CPP_STRING
911 : : at this point.
912 : :
913 : : This effectively reverses part of the logic in lex_string and
914 : : fix_string_type. */
915 : :
916 : : static enum cpp_ttype
917 : 11302 : get_cpp_ttype_from_string_type (tree string_type)
918 : : {
919 : 11302 : gcc_assert (string_type);
920 : 11302 : if (TREE_CODE (string_type) == POINTER_TYPE)
921 : 5977 : string_type = TREE_TYPE (string_type);
922 : :
923 : 11302 : if (TREE_CODE (string_type) != ARRAY_TYPE)
924 : : return CPP_OTHER;
925 : :
926 : 11302 : tree element_type = TREE_TYPE (string_type);
927 : 11302 : if (TREE_CODE (element_type) != INTEGER_TYPE)
928 : : return CPP_OTHER;
929 : :
930 : 11302 : int bits_per_character = TYPE_PRECISION (element_type);
931 : 11302 : switch (bits_per_character)
932 : : {
933 : : case 8:
934 : : return CPP_STRING; /* It could have also been CPP_UTF8STRING. */
935 : 2 : case 16:
936 : 2 : return CPP_STRING16;
937 : 4 : case 32:
938 : 4 : return CPP_STRING32;
939 : : }
940 : :
941 : : return CPP_OTHER;
942 : : }
943 : :
944 : : /* The global record of string concatentations, for use in
945 : : extracting locations within string literals. */
946 : :
947 : : GTY(()) string_concat_db *g_string_concat_db;
948 : :
949 : : /* Implementation of LANG_HOOKS_GET_SUBSTRING_LOCATION. */
950 : :
951 : : const char *
952 : 11302 : c_get_substring_location (const substring_loc &substr_loc,
953 : : location_t *out_loc)
954 : : {
955 : 11302 : enum cpp_ttype tok_type
956 : 11302 : = get_cpp_ttype_from_string_type (substr_loc.get_string_type ());
957 : 11302 : if (tok_type == CPP_OTHER)
958 : : return "unrecognized string type";
959 : :
960 : 11302 : return get_location_within_string (parse_in,
961 : : global_dc->get_file_cache (),
962 : : g_string_concat_db,
963 : : substr_loc.get_fmt_string_loc (),
964 : : tok_type,
965 : : substr_loc.get_caret_idx (),
966 : : substr_loc.get_start_idx (),
967 : : substr_loc.get_end_idx (),
968 : 11302 : out_loc);
969 : : }
970 : :
971 : :
972 : : /* Return true iff T is a boolean promoted to int. */
973 : :
974 : : bool
975 : 79295 : bool_promoted_to_int_p (tree t)
976 : : {
977 : 62163 : return (CONVERT_EXPR_P (t)
978 : 17232 : && TREE_TYPE (t) == integer_type_node
979 : 79395 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == BOOLEAN_TYPE);
980 : : }
981 : :
982 : : /* vector_targets_convertible_p is used for vector pointer types. The
983 : : callers perform various checks that the qualifiers are satisfactory,
984 : : while OTOH vector_targets_convertible_p ignores the number of elements
985 : : in the vectors. That's fine with vector pointers as we can consider,
986 : : say, a vector of 8 elements as two consecutive vectors of 4 elements,
987 : : and that does not require and conversion of the pointer values.
988 : : In contrast, vector_types_convertible_p and
989 : : vector_types_compatible_elements_p are used for vector value types. */
990 : : /* True if pointers to distinct types T1 and T2 can be converted to
991 : : each other without an explicit cast. Only returns true for opaque
992 : : vector types. */
993 : : bool
994 : 2027514 : vector_targets_convertible_p (const_tree t1, const_tree t2)
995 : : {
996 : 976663 : if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
997 : 976487 : && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
998 : 2027514 : && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
999 : : return true;
1000 : :
1001 : : return false;
1002 : : }
1003 : :
1004 : : /* vector_types_convertible_p is used for vector value types.
1005 : : It could in principle call vector_targets_convertible_p as a subroutine,
1006 : : but then the check for vector type would be duplicated with its callers,
1007 : : and also the purpose of vector_targets_convertible_p would become
1008 : : muddled.
1009 : : Where vector_types_convertible_p returns true, a conversion might still be
1010 : : needed to make the types match.
1011 : : In contrast, vector_targets_convertible_p is used for vector pointer
1012 : : values, and vector_types_compatible_elements_p is used specifically
1013 : : in the context for binary operators, as a check if use is possible without
1014 : : conversion. */
1015 : : /* True if vector types T1 and T2 can be converted to each other
1016 : : without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
1017 : : can only be converted with -flax-vector-conversions yet that is not
1018 : : in effect, emit a note telling the user about that option if such
1019 : : a note has not previously been emitted. */
1020 : : bool
1021 : 18521649 : vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
1022 : : {
1023 : 18521649 : static bool emitted_lax_note = false;
1024 : 18521649 : bool convertible_lax;
1025 : :
1026 : 37038207 : if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1027 : 18523192 : && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1028 : : return true;
1029 : :
1030 : 37030146 : convertible_lax =
1031 : 18515073 : (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
1032 : 18511521 : && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE
1033 : 10761127 : || known_eq (TYPE_VECTOR_SUBPARTS (t1),
1034 : : TYPE_VECTOR_SUBPARTS (t2)))
1035 : 55470527 : && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
1036 : 18477727 : == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
1037 : :
1038 : 18515073 : if (!convertible_lax || flag_lax_vector_conversions)
1039 : 73609 : return convertible_lax;
1040 : :
1041 : 18441464 : if (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1042 : 18441464 : && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1043 : 16874980 : return true;
1044 : :
1045 : 1566484 : if (emit_lax_note && !emitted_lax_note)
1046 : : {
1047 : 10 : emitted_lax_note = true;
1048 : 10 : inform (input_location, "use %<-flax-vector-conversions%> to permit "
1049 : : "conversions between vectors with differing "
1050 : : "element types or numbers of subparts");
1051 : : }
1052 : :
1053 : : return false;
1054 : : }
1055 : :
1056 : : /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
1057 : : and have vector types, V0 has the same type as V1, and the number of
1058 : : elements of V0, V1, MASK is the same.
1059 : :
1060 : : In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
1061 : : called with two arguments. In this case implementation passes the
1062 : : first argument twice in order to share the same tree code. This fact
1063 : : could enable the mask-values being twice the vector length. This is
1064 : : an implementation accident and this semantics is not guaranteed to
1065 : : the user. */
1066 : : tree
1067 : 486425 : c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
1068 : : bool complain)
1069 : : {
1070 : 486425 : tree ret;
1071 : 486425 : bool wrap = true;
1072 : 486425 : bool maybe_const = false;
1073 : 486425 : bool two_arguments = false;
1074 : :
1075 : 486425 : if (v1 == NULL_TREE)
1076 : : {
1077 : 444089 : two_arguments = true;
1078 : 444089 : v1 = v0;
1079 : : }
1080 : :
1081 : 486425 : if (v0 == error_mark_node || v1 == error_mark_node
1082 : 486425 : || mask == error_mark_node)
1083 : : return error_mark_node;
1084 : :
1085 : 486425 : if (!gnu_vector_type_p (TREE_TYPE (mask))
1086 : 486425 : || !VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
1087 : : {
1088 : 3 : if (complain)
1089 : 0 : error_at (loc, "%<__builtin_shuffle%> last argument must "
1090 : : "be an integer vector");
1091 : 3 : return error_mark_node;
1092 : : }
1093 : :
1094 : 486422 : if (!gnu_vector_type_p (TREE_TYPE (v0))
1095 : 486422 : || !gnu_vector_type_p (TREE_TYPE (v1)))
1096 : : {
1097 : 0 : if (complain)
1098 : 0 : error_at (loc, "%<__builtin_shuffle%> arguments must be vectors");
1099 : 0 : return error_mark_node;
1100 : : }
1101 : :
1102 : 486422 : if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
1103 : : {
1104 : 0 : if (complain)
1105 : 0 : error_at (loc, "%<__builtin_shuffle%> argument vectors must be of "
1106 : : "the same type");
1107 : 0 : return error_mark_node;
1108 : : }
1109 : :
1110 : 486422 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)),
1111 : 486422 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
1112 : 486422 : && maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)),
1113 : 0 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))))
1114 : : {
1115 : 0 : if (complain)
1116 : 0 : error_at (loc, "%<__builtin_shuffle%> number of elements of the "
1117 : : "argument vector(s) and the mask vector should "
1118 : : "be the same");
1119 : 0 : return error_mark_node;
1120 : : }
1121 : :
1122 : 486422 : if (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
1123 : 972844 : != GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
1124 : : {
1125 : 0 : if (complain)
1126 : 0 : error_at (loc, "%<__builtin_shuffle%> argument vector(s) inner type "
1127 : : "must have the same size as inner type of the mask");
1128 : 0 : return error_mark_node;
1129 : : }
1130 : :
1131 : 486422 : if (!c_dialect_cxx ())
1132 : : {
1133 : : /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
1134 : 468937 : v0 = c_fully_fold (v0, false, &maybe_const);
1135 : 468937 : wrap &= maybe_const;
1136 : :
1137 : 468937 : if (two_arguments)
1138 : 427470 : v1 = v0 = save_expr (v0);
1139 : : else
1140 : : {
1141 : 41467 : v1 = c_fully_fold (v1, false, &maybe_const);
1142 : 41467 : wrap &= maybe_const;
1143 : : }
1144 : :
1145 : 468937 : mask = c_fully_fold (mask, false, &maybe_const);
1146 : 468937 : wrap &= maybe_const;
1147 : : }
1148 : 17485 : else if (two_arguments)
1149 : 16616 : v1 = v0 = save_expr (v0);
1150 : :
1151 : 486422 : ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
1152 : :
1153 : 486422 : if (!c_dialect_cxx () && !wrap)
1154 : 468937 : ret = c_wrap_maybe_const (ret, true);
1155 : :
1156 : : return ret;
1157 : : }
1158 : :
1159 : : /* Build a VEC_PERM_EXPR if V0, V1 are not error_mark_nodes
1160 : : and have vector types, V0 has the same element type as V1, and the
1161 : : number of elements the result is that of MASK. */
1162 : : tree
1163 : 1116679 : c_build_shufflevector (location_t loc, tree v0, tree v1,
1164 : : const vec<tree> &mask, bool complain)
1165 : : {
1166 : 1116679 : tree ret;
1167 : 1116679 : bool wrap = true;
1168 : 1116679 : bool maybe_const = false;
1169 : :
1170 : 1116679 : if (v0 == error_mark_node || v1 == error_mark_node)
1171 : : return error_mark_node;
1172 : :
1173 : 1116679 : if (!gnu_vector_type_p (TREE_TYPE (v0))
1174 : 1116679 : || !gnu_vector_type_p (TREE_TYPE (v1)))
1175 : : {
1176 : 4 : if (complain)
1177 : 4 : error_at (loc, "%<__builtin_shufflevector%> arguments must be vectors");
1178 : 4 : return error_mark_node;
1179 : : }
1180 : :
1181 : : /* ??? In principle one could select a constant part of a variable size
1182 : : vector but things get a bit awkward with trying to support this here. */
1183 : 1116675 : unsigned HOST_WIDE_INT v0n, v1n;
1184 : 1116675 : if (!TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)).is_constant (&v0n)
1185 : 1116675 : || !TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)).is_constant (&v1n))
1186 : : {
1187 : : if (complain)
1188 : : error_at (loc, "%<__builtin_shufflevector%> arguments must be constant"
1189 : : " size vectors");
1190 : : return error_mark_node;
1191 : : }
1192 : :
1193 : 1116675 : if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (v0)))
1194 : 1116675 : != TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (v1))))
1195 : : {
1196 : 4 : if (complain)
1197 : 4 : error_at (loc, "%<__builtin_shufflevector%> argument vectors must "
1198 : : "have the same element type");
1199 : 4 : return error_mark_node;
1200 : : }
1201 : :
1202 : 1116671 : if (!pow2p_hwi (mask.length ()))
1203 : : {
1204 : 4 : if (complain)
1205 : 4 : error_at (loc, "%<__builtin_shufflevector%> must specify a result "
1206 : : "with a power of two number of elements");
1207 : 4 : return error_mark_node;
1208 : : }
1209 : :
1210 : 1116667 : if (!c_dialect_cxx ())
1211 : : {
1212 : : /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
1213 : 1075523 : v0 = c_fully_fold (v0, false, &maybe_const);
1214 : 1075523 : wrap &= maybe_const;
1215 : :
1216 : 1075523 : v1 = c_fully_fold (v1, false, &maybe_const);
1217 : 1075523 : wrap &= maybe_const;
1218 : : }
1219 : :
1220 : 2233379 : unsigned HOST_WIDE_INT maskl = MAX (mask.length (), MAX (v0n, v1n));
1221 : 1116667 : unsigned HOST_WIDE_INT pad = (v0n < maskl ? maskl - v0n : 0);
1222 : 1116667 : vec_perm_builder sel (maskl, maskl, 1);
1223 : 1116667 : unsigned i;
1224 : 30308126 : for (i = 0; i < mask.length (); ++i)
1225 : : {
1226 : 14037411 : tree idx = mask[i];
1227 : 14037411 : if (!tree_fits_shwi_p (idx))
1228 : : {
1229 : 4 : if (complain)
1230 : 4 : error_at (loc, "invalid element index %qE to "
1231 : : "%<__builtin_shufflevector%>", idx);
1232 : 1116667 : return error_mark_node;
1233 : : }
1234 : 14037407 : HOST_WIDE_INT iidx = tree_to_shwi (idx);
1235 : 14037407 : if (iidx < -1
1236 : 14037403 : || (iidx != -1
1237 : 14037323 : && (unsigned HOST_WIDE_INT) iidx >= v0n + v1n))
1238 : : {
1239 : 11 : if (complain)
1240 : 11 : error_at (loc, "invalid element index %qE to "
1241 : : "%<__builtin_shufflevector%>", idx);
1242 : 11 : return error_mark_node;
1243 : : }
1244 : : /* ??? Our VEC_PERM_EXPR does not allow for -1 yet. */
1245 : 14037396 : if (iidx == -1)
1246 : 80 : iidx = i;
1247 : : /* ??? Our VEC_PERM_EXPR does not allow different sized inputs,
1248 : : so pad out a smaller v0. */
1249 : 14037316 : else if ((unsigned HOST_WIDE_INT) iidx >= v0n)
1250 : 1159 : iidx += pad;
1251 : 14037396 : sel.quick_push (iidx);
1252 : : }
1253 : : /* ??? VEC_PERM_EXPR does not support a result that is smaller than
1254 : : the inputs, so we have to pad id out. */
1255 : 1117812 : for (; i < maskl; ++i)
1256 : 1160 : sel.quick_push (i);
1257 : :
1258 : 1116652 : vec_perm_indices indices (sel, 2, maskl);
1259 : :
1260 : 1116652 : tree ret_type = build_vector_type (TREE_TYPE (TREE_TYPE (v0)), maskl);
1261 : 2233304 : tree mask_type = build_vector_type (build_nonstandard_integer_type
1262 : 1116652 : (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (ret_type))), 1),
1263 : 1116652 : maskl);
1264 : : /* Pad out arguments to the common vector size. */
1265 : 1116652 : if (v0n < maskl)
1266 : : {
1267 : 125 : constructor_elt elt = { NULL_TREE, build_zero_cst (TREE_TYPE (v0)) };
1268 : 125 : v0 = build_constructor_single (ret_type, NULL_TREE, v0);
1269 : 571 : for (i = 1; i < maskl / v0n; ++i)
1270 : 321 : vec_safe_push (CONSTRUCTOR_ELTS (v0), elt);
1271 : : }
1272 : 1116652 : if (v1n < maskl)
1273 : : {
1274 : 95 : constructor_elt elt = { NULL_TREE, build_zero_cst (TREE_TYPE (v1)) };
1275 : 95 : v1 = build_constructor_single (ret_type, NULL_TREE, v1);
1276 : 351 : for (i = 1; i < maskl / v1n; ++i)
1277 : 161 : vec_safe_push (CONSTRUCTOR_ELTS (v1), elt);
1278 : : }
1279 : 1116652 : ret = build3_loc (loc, VEC_PERM_EXPR, ret_type, v0, v1,
1280 : : vec_perm_indices_to_tree (mask_type, indices));
1281 : : /* Get the lowpart we are interested in. */
1282 : 2233304 : if (mask.length () < maskl)
1283 : : {
1284 : 163 : tree lpartt = build_vector_type (TREE_TYPE (ret_type), mask.length ());
1285 : 163 : ret = build3_loc (loc, BIT_FIELD_REF,
1286 : 163 : lpartt, ret, TYPE_SIZE (lpartt), bitsize_zero_node);
1287 : : /* Wrap the lowpart operation in a TARGET_EXPR so it gets a separate
1288 : : temporary during gimplification. See PR101530 for cases where
1289 : : we'd otherwise end up with non-toplevel BIT_FIELD_REFs. */
1290 : 163 : tree tem = create_tmp_var_raw (lpartt);
1291 : 163 : DECL_CONTEXT (tem) = current_function_decl;
1292 : 163 : ret = build4 (TARGET_EXPR, lpartt, tem, ret, NULL_TREE, NULL_TREE);
1293 : 163 : TREE_SIDE_EFFECTS (ret) = 1;
1294 : : }
1295 : :
1296 : 1116652 : if (!c_dialect_cxx () && !wrap)
1297 : 1075520 : ret = c_wrap_maybe_const (ret, true);
1298 : :
1299 : 1116652 : return ret;
1300 : 2233319 : }
1301 : :
1302 : : /* Build a VEC_CONVERT ifn for __builtin_convertvector builtin. */
1303 : :
1304 : : tree
1305 : 490 : c_build_vec_convert (location_t loc1, tree expr, location_t loc2, tree type,
1306 : : bool complain)
1307 : : {
1308 : 490 : if (error_operand_p (type))
1309 : 0 : return error_mark_node;
1310 : 490 : if (error_operand_p (expr))
1311 : 0 : return error_mark_node;
1312 : :
1313 : 490 : if (!gnu_vector_type_p (TREE_TYPE (expr))
1314 : 490 : || (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (expr))
1315 : 166 : && !VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr))
1316 : 166 : && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (expr))))
1317 : : {
1318 : 4 : if (complain)
1319 : 4 : error_at (loc1, "%<__builtin_convertvector%> first argument must "
1320 : : "be an integer or floating vector");
1321 : 4 : return error_mark_node;
1322 : : }
1323 : :
1324 : 486 : if (!gnu_vector_type_p (type)
1325 : 486 : || (!VECTOR_INTEGER_TYPE_P (type) && !VECTOR_FLOAT_TYPE_P (type)
1326 : 0 : && !VECTOR_BOOLEAN_TYPE_P (type)))
1327 : : {
1328 : 4 : if (complain)
1329 : 4 : error_at (loc2, "%<__builtin_convertvector%> second argument must "
1330 : : "be an integer or floating vector type");
1331 : 4 : return error_mark_node;
1332 : : }
1333 : :
1334 : 482 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr)),
1335 : 964 : TYPE_VECTOR_SUBPARTS (type)))
1336 : : {
1337 : 8 : if (complain)
1338 : 8 : error_at (loc1, "%<__builtin_convertvector%> number of elements "
1339 : : "of the first argument vector and the second argument "
1340 : : "vector type should be the same");
1341 : 8 : return error_mark_node;
1342 : : }
1343 : :
1344 : 474 : if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (expr)))
1345 : 474 : == TYPE_MAIN_VARIANT (TREE_TYPE (type)))
1346 : 474 : || (VECTOR_INTEGER_TYPE_P (TREE_TYPE (expr))
1347 : 303 : && VECTOR_INTEGER_TYPE_P (type)
1348 : 155 : && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (expr)))
1349 : 155 : == TYPE_PRECISION (TREE_TYPE (type)))))
1350 : 29 : return build1_loc (loc1, VIEW_CONVERT_EXPR, type, expr);
1351 : :
1352 : 445 : bool wrap = true;
1353 : 445 : bool maybe_const = false;
1354 : 445 : tree ret;
1355 : 445 : if (!c_dialect_cxx ())
1356 : : {
1357 : : /* Avoid C_MAYBE_CONST_EXPRs inside of VEC_CONVERT argument. */
1358 : 310 : expr = c_fully_fold (expr, false, &maybe_const);
1359 : 310 : wrap &= maybe_const;
1360 : : }
1361 : :
1362 : 445 : ret = build_call_expr_internal_loc (loc1, IFN_VEC_CONVERT, type, 1, expr);
1363 : :
1364 : 445 : if (!wrap)
1365 : 310 : ret = c_wrap_maybe_const (ret, true);
1366 : :
1367 : : return ret;
1368 : : }
1369 : :
1370 : : /* Like tree.cc:get_narrower, but retain conversion from C++0x scoped enum
1371 : : to integral type. */
1372 : :
1373 : : tree
1374 : 65480025 : c_common_get_narrower (tree op, int *unsignedp_ptr)
1375 : : {
1376 : 65480025 : op = get_narrower (op, unsignedp_ptr);
1377 : :
1378 : 65480025 : if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
1379 : 65480025 : && ENUM_IS_SCOPED (TREE_TYPE (op)))
1380 : : {
1381 : : /* C++0x scoped enumerations don't implicitly convert to integral
1382 : : type; if we stripped an explicit conversion to a larger type we
1383 : : need to replace it so common_type will still work. */
1384 : 2607699 : tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
1385 : 2607699 : TYPE_UNSIGNED (TREE_TYPE (op)));
1386 : 2607699 : op = fold_convert (type, op);
1387 : : }
1388 : 65480025 : return op;
1389 : : }
1390 : :
1391 : : /* This is a helper function of build_binary_op.
1392 : :
1393 : : For certain operations if both args were extended from the same
1394 : : smaller type, do the arithmetic in that type and then extend.
1395 : :
1396 : : BITWISE indicates a bitwise operation.
1397 : : For them, this optimization is safe only if
1398 : : both args are zero-extended or both are sign-extended.
1399 : : Otherwise, we might change the result.
1400 : : Eg, (short)-1 | (unsigned short)-1 is (int)-1
1401 : : but calculated in (unsigned short) it would be (unsigned short)-1.
1402 : : */
1403 : : tree
1404 : 5238586 : shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
1405 : : {
1406 : 5238586 : int unsigned0, unsigned1;
1407 : 5238586 : tree arg0, arg1;
1408 : 5238586 : int uns;
1409 : 5238586 : tree type;
1410 : :
1411 : : /* Do not shorten vector operations. */
1412 : 5238586 : if (VECTOR_TYPE_P (result_type))
1413 : : return result_type;
1414 : :
1415 : : /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1416 : : excessive narrowing when we call get_narrower below. For
1417 : : example, suppose that OP0 is of unsigned int extended
1418 : : from signed char and that RESULT_TYPE is long long int.
1419 : : If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1420 : : like
1421 : :
1422 : : (long long int) (unsigned int) signed_char
1423 : :
1424 : : which get_narrower would narrow down to
1425 : :
1426 : : (unsigned int) signed char
1427 : :
1428 : : If we do not cast OP0 first, get_narrower would return
1429 : : signed_char, which is inconsistent with the case of the
1430 : : explicit cast. */
1431 : 5217713 : op0 = convert (result_type, op0);
1432 : 5217713 : op1 = convert (result_type, op1);
1433 : :
1434 : 5217713 : arg0 = c_common_get_narrower (op0, &unsigned0);
1435 : 5217713 : arg1 = c_common_get_narrower (op1, &unsigned1);
1436 : :
1437 : : /* UNS is 1 if the operation to be done is an unsigned one. */
1438 : 5217713 : uns = TYPE_UNSIGNED (result_type);
1439 : :
1440 : : /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1441 : : but it *requires* conversion to FINAL_TYPE. */
1442 : :
1443 : 5217713 : if ((TYPE_PRECISION (TREE_TYPE (op0))
1444 : 5217713 : == TYPE_PRECISION (TREE_TYPE (arg0)))
1445 : 5217713 : && TREE_TYPE (op0) != result_type)
1446 : 2437 : unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1447 : 5217713 : if ((TYPE_PRECISION (TREE_TYPE (op1))
1448 : 5217713 : == TYPE_PRECISION (TREE_TYPE (arg1)))
1449 : 5217713 : && TREE_TYPE (op1) != result_type)
1450 : 251822 : unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1451 : :
1452 : : /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
1453 : :
1454 : : /* For bitwise operations, signedness of nominal type
1455 : : does not matter. Consider only how operands were extended. */
1456 : 5217713 : if (bitwise)
1457 : 2538175 : uns = unsigned0;
1458 : :
1459 : : /* Note that in all three cases below we refrain from optimizing
1460 : : an unsigned operation on sign-extended args.
1461 : : That would not be valid. */
1462 : :
1463 : : /* Both args variable: if both extended in same way
1464 : : from same width, do it in that width.
1465 : : Do it unsigned if args were zero-extended. */
1466 : 5217713 : if ((TYPE_PRECISION (TREE_TYPE (arg0))
1467 : 5217713 : < TYPE_PRECISION (result_type))
1468 : 270615 : && (TYPE_PRECISION (TREE_TYPE (arg1))
1469 : 270615 : == TYPE_PRECISION (TREE_TYPE (arg0)))
1470 : 186825 : && unsigned0 == unsigned1
1471 : 5404344 : && (unsigned0 || !uns))
1472 : : {
1473 : 186613 : tree ctype = common_type (TREE_TYPE (arg0), TREE_TYPE (arg1));
1474 : 186613 : if (ctype != error_mark_node)
1475 : 186613 : return c_common_signed_or_unsigned_type (unsigned0, ctype);
1476 : : }
1477 : :
1478 : 5031100 : else if (TREE_CODE (arg0) == INTEGER_CST
1479 : 1784560 : && (unsigned1 || !uns)
1480 : 1581984 : && (TYPE_PRECISION (TREE_TYPE (arg1))
1481 : 1581984 : < TYPE_PRECISION (result_type))
1482 : 299 : && (type
1483 : 299 : = c_common_signed_or_unsigned_type (unsigned1,
1484 : 299 : TREE_TYPE (arg1)))
1485 : 299 : && !POINTER_TYPE_P (type)
1486 : 5031399 : && int_fits_type_p (arg0, type))
1487 : : return type;
1488 : :
1489 : 5030853 : else if (TREE_CODE (arg1) == INTEGER_CST
1490 : 3213562 : && (unsigned0 || !uns)
1491 : 2491022 : && (TYPE_PRECISION (TREE_TYPE (arg0))
1492 : 2491022 : < TYPE_PRECISION (result_type))
1493 : 59942 : && (type
1494 : 59942 : = c_common_signed_or_unsigned_type (unsigned0,
1495 : 59942 : TREE_TYPE (arg0)))
1496 : 59942 : && !POINTER_TYPE_P (type)
1497 : 5090795 : && int_fits_type_p (arg1, type))
1498 : : return type;
1499 : :
1500 : : return result_type;
1501 : : }
1502 : :
1503 : : /* Returns true iff any integer value of type FROM_TYPE can be represented as
1504 : : real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
1505 : :
1506 : : static bool
1507 : 101 : int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
1508 : : {
1509 : 101 : tree type_low_bound = TYPE_MIN_VALUE (from_type);
1510 : 101 : tree type_high_bound = TYPE_MAX_VALUE (from_type);
1511 : 101 : REAL_VALUE_TYPE real_low_bound =
1512 : 101 : real_value_from_int_cst (0, type_low_bound);
1513 : 101 : REAL_VALUE_TYPE real_high_bound =
1514 : 101 : real_value_from_int_cst (0, type_high_bound);
1515 : :
1516 : 101 : return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
1517 : 101 : && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
1518 : : }
1519 : :
1520 : : /* Checks if expression EXPR of complex/real/integer type cannot be converted
1521 : : to the complex/real/integer type TYPE. Function returns non-zero when:
1522 : : * EXPR is a constant which cannot be exactly converted to TYPE.
1523 : : * EXPR is not a constant and size of EXPR's type > than size of TYPE,
1524 : : for EXPR type and TYPE being both integers or both real, or both
1525 : : complex.
1526 : : * EXPR is not a constant of complex type and TYPE is a real or
1527 : : an integer.
1528 : : * EXPR is not a constant of real type and TYPE is an integer.
1529 : : * EXPR is not a constant of integer type which cannot be
1530 : : exactly converted to real type.
1531 : :
1532 : : Function allows conversions between types of different signedness if
1533 : : CHECK_SIGN is false and can return SAFE_CONVERSION (zero) in that
1534 : : case. Function can return UNSAFE_SIGN if CHECK_SIGN is true.
1535 : :
1536 : : RESULT, when non-null is the result of the conversion. When constant
1537 : : it is included in the text of diagnostics.
1538 : :
1539 : : Function allows conversions from complex constants to non-complex types,
1540 : : provided that imaginary part is zero and real part can be safely converted
1541 : : to TYPE. */
1542 : :
1543 : : enum conversion_safety
1544 : 9420 : unsafe_conversion_p (tree type, tree expr, tree result, bool check_sign)
1545 : : {
1546 : 9420 : enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
1547 : 9420 : tree expr_type = TREE_TYPE (expr);
1548 : :
1549 : 9420 : expr = fold_for_warn (expr);
1550 : :
1551 : 9420 : if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
1552 : : {
1553 : : /* If type is complex, we are interested in compatibility with
1554 : : underlying type. */
1555 : 5427 : if (TREE_CODE (type) == COMPLEX_TYPE)
1556 : 114 : type = TREE_TYPE (type);
1557 : :
1558 : : /* Warn for real constant that is not an exact integer converted
1559 : : to integer type. */
1560 : 5427 : if (SCALAR_FLOAT_TYPE_P (expr_type)
1561 : 658 : && (TREE_CODE (type) == INTEGER_TYPE
1562 : 658 : || TREE_CODE (type) == BITINT_TYPE))
1563 : : {
1564 : 283 : if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
1565 : 275 : give_warning = UNSAFE_REAL;
1566 : : }
1567 : : /* Warn for an integer constant that does not fit into integer type. */
1568 : 5144 : else if ((TREE_CODE (expr_type) == INTEGER_TYPE
1569 : 5144 : || TREE_CODE (expr_type) == BITINT_TYPE)
1570 : 4759 : && (TREE_CODE (type) == INTEGER_TYPE
1571 : 4759 : || TREE_CODE (type) == BITINT_TYPE)
1572 : 4035 : && !int_fits_type_p (expr, type))
1573 : : {
1574 : 995 : if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
1575 : 961 : && tree_int_cst_sgn (expr) < 0)
1576 : : {
1577 : 264 : if (check_sign)
1578 : 1834 : give_warning = UNSAFE_SIGN;
1579 : : }
1580 : 432 : else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
1581 : : {
1582 : 335 : if (check_sign)
1583 : 1834 : give_warning = UNSAFE_SIGN;
1584 : : }
1585 : : else
1586 : : give_warning = UNSAFE_OTHER;
1587 : : }
1588 : 4448 : else if (SCALAR_FLOAT_TYPE_P (type))
1589 : : {
1590 : : /* Warn for an integer constant that does not fit into real type. */
1591 : 1009 : if (TREE_CODE (expr_type) == INTEGER_TYPE
1592 : : || TREE_CODE (expr_type) == BITINT_TYPE)
1593 : : {
1594 : 634 : REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
1595 : 634 : if (!exact_real_truncate (TYPE_MODE (type), &a))
1596 : 53 : give_warning = UNSAFE_REAL;
1597 : : }
1598 : : /* Warn for a real constant that does not fit into a smaller
1599 : : real type. */
1600 : 375 : else if (SCALAR_FLOAT_TYPE_P (expr_type)
1601 : 375 : && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1602 : : {
1603 : 227 : REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
1604 : 227 : if (!exact_real_truncate (TYPE_MODE (type), &a))
1605 : 65 : give_warning = UNSAFE_REAL;
1606 : : }
1607 : : }
1608 : : }
1609 : :
1610 : 3993 : else if (TREE_CODE (expr) == COMPLEX_CST)
1611 : : {
1612 : 69 : tree imag_part = TREE_IMAGPART (expr);
1613 : : /* Conversion from complex constant with zero imaginary part,
1614 : : perform check for conversion of real part. */
1615 : 69 : if ((TREE_CODE (imag_part) == REAL_CST
1616 : 29 : && real_zerop (imag_part))
1617 : 80 : || (TREE_CODE (imag_part) == INTEGER_CST
1618 : 40 : && integer_zerop (imag_part)))
1619 : : /* Note: in this branch we use recursive call to unsafe_conversion_p
1620 : : with different type of EXPR, but it is still safe, because when EXPR
1621 : : is a constant, it's type is not used in text of generated warnings
1622 : : (otherwise they could sound misleading). */
1623 : 30 : return unsafe_conversion_p (type, TREE_REALPART (expr), result,
1624 : 30 : check_sign);
1625 : : /* Conversion from complex constant with non-zero imaginary part. */
1626 : : else
1627 : : {
1628 : : /* Conversion to complex type.
1629 : : Perform checks for both real and imaginary parts. */
1630 : 39 : if (TREE_CODE (type) == COMPLEX_TYPE)
1631 : : {
1632 : 33 : enum conversion_safety re_safety =
1633 : 33 : unsafe_conversion_p (type, TREE_REALPART (expr),
1634 : : result, check_sign);
1635 : 33 : enum conversion_safety im_safety =
1636 : 33 : unsafe_conversion_p (type, imag_part, result, check_sign);
1637 : :
1638 : : /* Merge the results into appropriate single warning. */
1639 : :
1640 : : /* Note: this case includes SAFE_CONVERSION, i.e. success. */
1641 : 33 : if (re_safety == im_safety)
1642 : : give_warning = re_safety;
1643 : 9 : else if (!re_safety && im_safety)
1644 : : give_warning = im_safety;
1645 : 4 : else if (re_safety && !im_safety)
1646 : : give_warning = re_safety;
1647 : : else
1648 : 670 : give_warning = UNSAFE_OTHER;
1649 : : }
1650 : : /* Warn about conversion from complex to real or integer type. */
1651 : : else
1652 : : give_warning = UNSAFE_IMAGINARY;
1653 : : }
1654 : : }
1655 : :
1656 : : /* Checks for remaining case: EXPR is not constant. */
1657 : : else
1658 : : {
1659 : : /* Warn for real types converted to integer types. */
1660 : 3924 : if (SCALAR_FLOAT_TYPE_P (expr_type)
1661 : 213 : && (TREE_CODE (type) == INTEGER_TYPE
1662 : 213 : || TREE_CODE (type) == BITINT_TYPE))
1663 : : give_warning = UNSAFE_REAL;
1664 : :
1665 : 3895 : else if ((TREE_CODE (expr_type) == INTEGER_TYPE
1666 : 3895 : || TREE_CODE (expr_type) == BITINT_TYPE)
1667 : 3500 : && (TREE_CODE (type) == INTEGER_TYPE
1668 : 3500 : || TREE_CODE (type) == BITINT_TYPE))
1669 : : {
1670 : : /* Don't warn about unsigned char y = 0xff, x = (int) y; */
1671 : 3279 : expr = get_unwidened (expr, 0);
1672 : 3279 : expr_type = TREE_TYPE (expr);
1673 : :
1674 : : /* Don't warn for short y; short x = ((int)y & 0xff); */
1675 : 3279 : if (TREE_CODE (expr) == BIT_AND_EXPR
1676 : 3265 : || TREE_CODE (expr) == BIT_IOR_EXPR
1677 : 3224 : || TREE_CODE (expr) == BIT_XOR_EXPR)
1678 : : {
1679 : : /* If both args were extended from a shortest type,
1680 : : use that type if that is safe. */
1681 : 59 : expr_type = shorten_binary_op (expr_type,
1682 : 59 : TREE_OPERAND (expr, 0),
1683 : 59 : TREE_OPERAND (expr, 1),
1684 : : /* bitwise */1);
1685 : :
1686 : 59 : if (TREE_CODE (expr) == BIT_AND_EXPR)
1687 : : {
1688 : 14 : tree op0 = TREE_OPERAND (expr, 0);
1689 : 14 : tree op1 = TREE_OPERAND (expr, 1);
1690 : 14 : bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1691 : 14 : bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1692 : :
1693 : : /* If one of the operands is a non-negative constant
1694 : : that fits in the target type, then the type of the
1695 : : other operand does not matter. */
1696 : 14 : if ((TREE_CODE (op0) == INTEGER_CST
1697 : 0 : && int_fits_type_p (op0, c_common_signed_type (type))
1698 : 0 : && int_fits_type_p (op0, c_common_unsigned_type (type)))
1699 : 14 : || (TREE_CODE (op1) == INTEGER_CST
1700 : 1 : && int_fits_type_p (op1, c_common_signed_type (type))
1701 : 1 : && int_fits_type_p (op1,
1702 : 1 : c_common_unsigned_type (type))))
1703 : 1 : return SAFE_CONVERSION;
1704 : : /* If constant is unsigned and fits in the target
1705 : : type, then the result will also fit. */
1706 : 13 : else if ((TREE_CODE (op0) == INTEGER_CST
1707 : 0 : && unsigned0
1708 : 0 : && int_fits_type_p (op0, type))
1709 : 13 : || (TREE_CODE (op1) == INTEGER_CST
1710 : 0 : && unsigned1
1711 : 0 : && int_fits_type_p (op1, type)))
1712 : : return SAFE_CONVERSION;
1713 : : }
1714 : : }
1715 : : /* Warn for integer types converted to smaller integer types. */
1716 : 3278 : if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1717 : : give_warning = UNSAFE_OTHER;
1718 : :
1719 : : /* When they are the same width but different signedness,
1720 : : then the value may change. */
1721 : 2742 : else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
1722 : 2213 : && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
1723 : : /* Even when converted to a bigger type, if the type is
1724 : : unsigned but expr is signed, then negative values
1725 : : will be changed. */
1726 : 1481 : || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
1727 : 4054 : && check_sign)
1728 : : give_warning = UNSAFE_SIGN;
1729 : : }
1730 : :
1731 : : /* Warn for integer types converted to real types if and only if
1732 : : all the range of values of the integer type cannot be
1733 : : represented by the real type. */
1734 : 616 : else if ((TREE_CODE (expr_type) == INTEGER_TYPE
1735 : : || TREE_CODE (expr_type) == BITINT_TYPE)
1736 : 221 : && SCALAR_FLOAT_TYPE_P (type))
1737 : : {
1738 : : /* Don't warn about char y = 0xff; float x = (int) y; */
1739 : 101 : expr = get_unwidened (expr, 0);
1740 : 101 : expr_type = TREE_TYPE (expr);
1741 : :
1742 : 101 : if (!int_safely_convertible_to_real_p (expr_type, type))
1743 : 670 : give_warning = UNSAFE_OTHER;
1744 : : }
1745 : :
1746 : : /* Warn for real types converted to smaller real types. */
1747 : 515 : else if (SCALAR_FLOAT_TYPE_P (expr_type)
1748 : 184 : && SCALAR_FLOAT_TYPE_P (type)
1749 : 699 : && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1750 : : give_warning = UNSAFE_REAL;
1751 : :
1752 : : /* Check conversion between two complex types. */
1753 : 463 : else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1754 : 32 : && TREE_CODE (type) == COMPLEX_TYPE)
1755 : : {
1756 : : /* Extract underlying types (i.e., type of real and imaginary
1757 : : parts) of expr_type and type. */
1758 : 10 : tree from_type = TREE_TYPE (expr_type);
1759 : 10 : tree to_type = TREE_TYPE (type);
1760 : :
1761 : : /* Warn for real types converted to integer types. */
1762 : 10 : if (SCALAR_FLOAT_TYPE_P (from_type)
1763 : 8 : && TREE_CODE (to_type) == INTEGER_TYPE)
1764 : : give_warning = UNSAFE_REAL;
1765 : :
1766 : : /* Warn for real types converted to smaller real types. */
1767 : 6 : else if (SCALAR_FLOAT_TYPE_P (from_type)
1768 : 4 : && SCALAR_FLOAT_TYPE_P (to_type)
1769 : 10 : && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1770 : : give_warning = UNSAFE_REAL;
1771 : :
1772 : : /* Check conversion for complex integer types. Here implementation
1773 : : is simpler than for real-domain integers because it does not
1774 : : involve sophisticated cases, such as bitmasks, casts, etc. */
1775 : 4 : else if (TREE_CODE (from_type) == INTEGER_TYPE
1776 : 2 : && TREE_CODE (to_type) == INTEGER_TYPE)
1777 : : {
1778 : : /* Warn for integer types converted to smaller integer types. */
1779 : 2 : if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1780 : : give_warning = UNSAFE_OTHER;
1781 : :
1782 : : /* Check for different signedness, see case for real-domain
1783 : : integers (above) for a more detailed comment. */
1784 : 0 : else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
1785 : 0 : && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
1786 : 0 : || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
1787 : 0 : && check_sign)
1788 : : give_warning = UNSAFE_SIGN;
1789 : : }
1790 : 2 : else if (TREE_CODE (from_type) == INTEGER_TYPE
1791 : 0 : && SCALAR_FLOAT_TYPE_P (to_type)
1792 : 2 : && !int_safely_convertible_to_real_p (from_type, to_type))
1793 : : give_warning = UNSAFE_OTHER;
1794 : : }
1795 : :
1796 : : /* Warn for complex types converted to real or integer types. */
1797 : 453 : else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1798 : 22 : && TREE_CODE (type) != COMPLEX_TYPE)
1799 : 28 : give_warning = UNSAFE_IMAGINARY;
1800 : : }
1801 : :
1802 : : return give_warning;
1803 : : }
1804 : :
1805 : :
1806 : : /* Convert EXPR to TYPE, warning about conversion problems with constants.
1807 : : Invoke this function on every expression that is converted implicitly,
1808 : : i.e. because of language rules and not because of an explicit cast.
1809 : : INIT_CONST is true if the conversion is for arithmetic types for a static
1810 : : initializer and folding must apply accordingly (discarding floating-point
1811 : : exceptions and assuming the default rounding mode is in effect). */
1812 : :
1813 : : tree
1814 : 12548863 : convert_and_check (location_t loc, tree type, tree expr, bool init_const)
1815 : : {
1816 : 12548863 : tree result;
1817 : 12548863 : tree expr_for_warning;
1818 : :
1819 : : /* Convert from a value with possible excess precision rather than
1820 : : via the semantic type, but do not warn about values not fitting
1821 : : exactly in the semantic type. */
1822 : 12548863 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1823 : : {
1824 : 5967 : tree orig_type = TREE_TYPE (expr);
1825 : 5967 : expr = TREE_OPERAND (expr, 0);
1826 : 5967 : expr_for_warning = (init_const
1827 : 5967 : ? convert_init (orig_type, expr)
1828 : 5606 : : convert (orig_type, expr));
1829 : 5967 : if (orig_type == type)
1830 : : return expr_for_warning;
1831 : : }
1832 : : else
1833 : : expr_for_warning = expr;
1834 : :
1835 : 12543389 : if (TREE_TYPE (expr) == type)
1836 : : return expr;
1837 : :
1838 : 11105215 : result = init_const ? convert_init (type, expr) : convert (type, expr);
1839 : :
1840 : 11105215 : if (c_inhibit_evaluation_warnings == 0
1841 : 10778755 : && !TREE_OVERFLOW_P (expr)
1842 : 10778729 : && result != error_mark_node
1843 : 21883790 : && !c_hardbool_type_attr (type))
1844 : 10771232 : warnings_for_convert_and_check (loc, type, expr_for_warning, result);
1845 : :
1846 : : return result;
1847 : : }
1848 : :
1849 : : /* A node in a list that describes references to variables (EXPR), which are
1850 : : either read accesses if WRITER is zero, or write accesses, in which case
1851 : : WRITER is the parent of EXPR. */
1852 : : struct tlist
1853 : : {
1854 : : struct tlist *next;
1855 : : tree expr, writer;
1856 : : };
1857 : :
1858 : : /* Used to implement a cache the results of a call to verify_tree. We only
1859 : : use this for SAVE_EXPRs. */
1860 : : struct tlist_cache
1861 : : {
1862 : : struct tlist_cache *next;
1863 : : struct tlist *cache_before_sp;
1864 : : struct tlist *cache_after_sp;
1865 : : tree expr;
1866 : : };
1867 : :
1868 : : /* Obstack to use when allocating tlist structures, and corresponding
1869 : : firstobj. */
1870 : : static struct obstack tlist_obstack;
1871 : : static char *tlist_firstobj = 0;
1872 : :
1873 : : /* Keep track of the identifiers we've warned about, so we can avoid duplicate
1874 : : warnings. */
1875 : : static struct tlist *warned_ids;
1876 : : /* SAVE_EXPRs need special treatment. We process them only once and then
1877 : : cache the results. */
1878 : : static struct tlist_cache *save_expr_cache;
1879 : :
1880 : : static void add_tlist (struct tlist **, struct tlist *, tree, int);
1881 : : static void merge_tlist (struct tlist **, struct tlist *, int);
1882 : : static void verify_tree (tree, struct tlist **, struct tlist **, tree);
1883 : : static bool warning_candidate_p (tree);
1884 : : static bool candidate_equal_p (const_tree, const_tree);
1885 : : static void warn_for_collisions (struct tlist *);
1886 : : static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
1887 : : static struct tlist *new_tlist (struct tlist *, tree, tree);
1888 : :
1889 : : /* Create a new struct tlist and fill in its fields. */
1890 : : static struct tlist *
1891 : 34294464 : new_tlist (struct tlist *next, tree t, tree writer)
1892 : : {
1893 : 34294464 : struct tlist *l;
1894 : 34294464 : l = XOBNEW (&tlist_obstack, struct tlist);
1895 : 34294464 : l->next = next;
1896 : 34294464 : l->expr = t;
1897 : 34294464 : l->writer = writer;
1898 : 34294464 : return l;
1899 : : }
1900 : :
1901 : : /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1902 : : is nonnull, we ignore any node we find which has a writer equal to it. */
1903 : :
1904 : : static void
1905 : 60537481 : add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
1906 : : {
1907 : 131702878 : while (add)
1908 : : {
1909 : 71165397 : struct tlist *next = add->next;
1910 : 13104567 : if (!copy)
1911 : 1866653 : add->next = *to;
1912 : 13104567 : if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
1913 : 69156377 : *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1914 : : add = next;
1915 : : }
1916 : 11808110 : }
1917 : :
1918 : : /* Merge the nodes of ADD into TO. This merging process is done so that for
1919 : : each variable that already exists in TO, no new node is added; however if
1920 : : there is a write access recorded in ADD, and an occurrence on TO is only
1921 : : a read access, then the occurrence in TO will be modified to record the
1922 : : write. */
1923 : :
1924 : : static void
1925 : 42278015 : merge_tlist (struct tlist **to, struct tlist *add, int copy)
1926 : : {
1927 : 42278015 : struct tlist **end = to;
1928 : :
1929 : 78949911 : while (*end)
1930 : 36671896 : end = &(*end)->next;
1931 : :
1932 : 58091932 : while (add)
1933 : : {
1934 : 15813917 : int found = 0;
1935 : 15813917 : struct tlist *tmp2;
1936 : 15813917 : struct tlist *next = add->next;
1937 : :
1938 : 43147585 : for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1939 : 27333668 : if (candidate_equal_p (tmp2->expr, add->expr))
1940 : : {
1941 : 2706817 : found = 1;
1942 : 2706817 : if (!tmp2->writer)
1943 : 2700433 : tmp2->writer = add->writer;
1944 : : }
1945 : 15813917 : if (!found)
1946 : : {
1947 : 13120774 : *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
1948 : 13120774 : end = &(*end)->next;
1949 : 13120774 : *end = 0;
1950 : : }
1951 : : add = next;
1952 : : }
1953 : 42278015 : }
1954 : :
1955 : : /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1956 : : references in list LIST conflict with it, excluding reads if ONLY writers
1957 : : is nonzero. */
1958 : :
1959 : : static void
1960 : 5234286 : warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
1961 : : int only_writes)
1962 : : {
1963 : 5234286 : struct tlist *tmp;
1964 : :
1965 : : /* Avoid duplicate warnings. */
1966 : 5234449 : for (tmp = warned_ids; tmp; tmp = tmp->next)
1967 : 611 : if (candidate_equal_p (tmp->expr, written))
1968 : : return;
1969 : :
1970 : 18799476 : while (list)
1971 : : {
1972 : 13565638 : if (candidate_equal_p (list->expr, written)
1973 : 1535124 : && !candidate_equal_p (list->writer, writer)
1974 : 13705896 : && (!only_writes || list->writer))
1975 : : {
1976 : 562 : warned_ids = new_tlist (warned_ids, written, NULL_TREE);
1977 : 562 : warning_at (EXPR_LOC_OR_LOC (writer, input_location),
1978 : 562 : OPT_Wsequence_point, "operation on %qE may be undefined",
1979 : : list->expr);
1980 : : }
1981 : 13565638 : list = list->next;
1982 : : }
1983 : : }
1984 : :
1985 : : /* Given a list LIST of references to variables, find whether any of these
1986 : : can cause conflicts due to missing sequence points. */
1987 : :
1988 : : static void
1989 : 19926574 : warn_for_collisions (struct tlist *list)
1990 : : {
1991 : 19926574 : struct tlist *tmp;
1992 : :
1993 : 54908490 : for (tmp = list; tmp; tmp = tmp->next)
1994 : : {
1995 : 34981916 : if (tmp->writer)
1996 : 1331135 : warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1997 : : }
1998 : 19926574 : }
1999 : :
2000 : : /* Return nonzero if X is a tree that can be verified by the sequence point
2001 : : warnings. */
2002 : :
2003 : : static bool
2004 : 76319075 : warning_candidate_p (tree x)
2005 : : {
2006 : 76319075 : if (DECL_P (x) && DECL_ARTIFICIAL (x))
2007 : : return false;
2008 : :
2009 : 68683339 : if (TREE_CODE (x) == BLOCK)
2010 : : return false;
2011 : :
2012 : : /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.cc
2013 : : (lvalue_p) crash on TRY/CATCH. */
2014 : 68681208 : if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2015 : : return false;
2016 : :
2017 : 67794034 : if (!lvalue_p (x))
2018 : : return false;
2019 : :
2020 : : /* No point to track non-const calls, they will never satisfy
2021 : : operand_equal_p. */
2022 : 24389258 : if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2023 : : return false;
2024 : :
2025 : 24347052 : if (TREE_CODE (x) == STRING_CST)
2026 : : return false;
2027 : :
2028 : : return true;
2029 : : }
2030 : :
2031 : : /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2032 : : static bool
2033 : 46222157 : candidate_equal_p (const_tree x, const_tree y)
2034 : : {
2035 : 46222157 : return (x == y) || (x && y && operand_equal_p (x, y, 0));
2036 : : }
2037 : :
2038 : : /* Walk the tree X, and record accesses to variables. If X is written by the
2039 : : parent tree, WRITER is the parent.
2040 : : We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2041 : : expression or its only operand forces a sequence point, then everything up
2042 : : to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2043 : : in PNO_SP.
2044 : : Once we return, we will have emitted warnings if any subexpression before
2045 : : such a sequence point could be undefined. On a higher level, however, the
2046 : : sequence point may not be relevant, and we'll merge the two lists.
2047 : :
2048 : : Example: (b++, a) + b;
2049 : : The call that processes the COMPOUND_EXPR will store the increment of B
2050 : : in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2051 : : processes the PLUS_EXPR will need to merge the two lists so that
2052 : : eventually, all accesses end up on the same list (and we'll warn about the
2053 : : unordered subexpressions b++ and b.
2054 : :
2055 : : A note on merging. If we modify the former example so that our expression
2056 : : becomes
2057 : : (b++, b) + a
2058 : : care must be taken not simply to add all three expressions into the final
2059 : : PNO_SP list. The function merge_tlist takes care of that by merging the
2060 : : before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2061 : : way, so that no more than one access to B is recorded. */
2062 : :
2063 : : static void
2064 : 59968158 : verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2065 : : tree writer)
2066 : : {
2067 : 73608483 : struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2068 : 73608483 : enum tree_code code;
2069 : 73608483 : enum tree_code_class cl;
2070 : :
2071 : 73608483 : restart:
2072 : : /* X may be NULL if it is the operand of an empty statement expression
2073 : : ({ }). */
2074 : 73608483 : if (x == NULL)
2075 : : return;
2076 : :
2077 : 72415924 : code = TREE_CODE (x);
2078 : 72415924 : cl = TREE_CODE_CLASS (code);
2079 : :
2080 : 72415924 : if (warning_candidate_p (x))
2081 : 23115028 : *pno_sp = new_tlist (*pno_sp, x, writer);
2082 : :
2083 : 72415924 : switch (code)
2084 : : {
2085 : : case CONSTRUCTOR:
2086 : : case SIZEOF_EXPR:
2087 : : case PAREN_SIZEOF_EXPR:
2088 : : return;
2089 : :
2090 : 203860 : case COMPOUND_EXPR:
2091 : 203860 : case TRUTH_ANDIF_EXPR:
2092 : 203860 : case TRUTH_ORIF_EXPR:
2093 : 203860 : sequenced_binary:
2094 : 203860 : tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
2095 : 203860 : verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2096 : 203860 : warn_for_collisions (tmp_nosp);
2097 : 203860 : merge_tlist (pbefore_sp, tmp_before, 0);
2098 : 203860 : merge_tlist (pbefore_sp, tmp_nosp, 0);
2099 : 203860 : verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_list2, NULL_TREE);
2100 : 203860 : warn_for_collisions (tmp_list2);
2101 : 203860 : merge_tlist (pbefore_sp, tmp_list3, 0);
2102 : 203860 : merge_tlist (pno_sp, tmp_list2, 0);
2103 : 203860 : return;
2104 : :
2105 : 66986 : case COND_EXPR:
2106 : 66986 : tmp_before = tmp_list2 = 0;
2107 : 66986 : verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2108 : 66986 : warn_for_collisions (tmp_list2);
2109 : 66986 : merge_tlist (pbefore_sp, tmp_before, 0);
2110 : 66986 : merge_tlist (pbefore_sp, tmp_list2, 0);
2111 : :
2112 : 66986 : tmp_list3 = tmp_nosp = 0;
2113 : 66986 : verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
2114 : 66986 : warn_for_collisions (tmp_nosp);
2115 : 66986 : merge_tlist (pbefore_sp, tmp_list3, 0);
2116 : :
2117 : 66986 : tmp_list3 = tmp_list2 = 0;
2118 : 66986 : verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
2119 : 66986 : warn_for_collisions (tmp_list2);
2120 : 66986 : merge_tlist (pbefore_sp, tmp_list3, 0);
2121 : : /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
2122 : : two first, to avoid warning for (a ? b++ : b++). */
2123 : 66986 : merge_tlist (&tmp_nosp, tmp_list2, 0);
2124 : 66986 : add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2125 : : return;
2126 : :
2127 : 267793 : case PREDECREMENT_EXPR:
2128 : 267793 : case PREINCREMENT_EXPR:
2129 : 267793 : case POSTDECREMENT_EXPR:
2130 : 267793 : case POSTINCREMENT_EXPR:
2131 : 267793 : verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
2132 : 267793 : return;
2133 : :
2134 : 3903151 : case MODIFY_EXPR:
2135 : 3903151 : tmp_before = tmp_nosp = tmp_list3 = 0;
2136 : 3903151 : verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
2137 : 3903151 : verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
2138 : : /* Expressions inside the LHS are not ordered wrt. the sequence points
2139 : : in the RHS. Example:
2140 : : *a = (a++, 2)
2141 : : Despite the fact that the modification of "a" is in the before_sp
2142 : : list (tmp_before), it conflicts with the use of "a" in the LHS.
2143 : : We can handle this by adding the contents of tmp_list3
2144 : : to those of tmp_before, and redoing the collision warnings for that
2145 : : list. */
2146 : 3903151 : add_tlist (&tmp_before, tmp_list3, x, 1);
2147 : 3903151 : warn_for_collisions (tmp_before);
2148 : : /* Exclude the LHS itself here; we first have to merge it into the
2149 : : tmp_nosp list. This is done to avoid warning for "a = a"; if we
2150 : : didn't exclude the LHS, we'd get it twice, once as a read and once
2151 : : as a write. */
2152 : 3903151 : add_tlist (pno_sp, tmp_list3, x, 0);
2153 : 3903151 : warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
2154 : :
2155 : 3903151 : merge_tlist (pbefore_sp, tmp_before, 0);
2156 : 3903151 : if (warning_candidate_p (TREE_OPERAND (x, 0)))
2157 : 938833 : merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
2158 : 3903151 : add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
2159 : 3903151 : return;
2160 : :
2161 : 6261976 : case CALL_EXPR:
2162 : : /* We need to warn about conflicts among arguments and conflicts between
2163 : : args and the function address. Side effects of the function address,
2164 : : however, are not ordered by the sequence point of the call. */
2165 : 6261976 : {
2166 : 6261976 : call_expr_arg_iterator iter;
2167 : 6261976 : tree arg;
2168 : 6261976 : tmp_before = tmp_nosp = 0;
2169 : 6261976 : verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
2170 : 27794239 : FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
2171 : : {
2172 : 15270287 : tmp_list2 = tmp_list3 = 0;
2173 : 15270287 : verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
2174 : 15270287 : merge_tlist (&tmp_list3, tmp_list2, 0);
2175 : 30540574 : add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
2176 : : }
2177 : 6261976 : add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
2178 : 6261976 : warn_for_collisions (tmp_before);
2179 : 6261976 : add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
2180 : 6261976 : return;
2181 : : }
2182 : :
2183 : : case TREE_LIST:
2184 : : /* Scan all the list, e.g. indices of multi dimensional array. */
2185 : 6 : while (x)
2186 : : {
2187 : 3 : tmp_before = tmp_nosp = 0;
2188 : 3 : verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
2189 : 3 : merge_tlist (&tmp_nosp, tmp_before, 0);
2190 : 3 : add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2191 : 3 : x = TREE_CHAIN (x);
2192 : : }
2193 : : return;
2194 : :
2195 : 98657 : case SAVE_EXPR:
2196 : 98657 : {
2197 : 98657 : struct tlist_cache *t;
2198 : 102381 : for (t = save_expr_cache; t; t = t->next)
2199 : 53810 : if (candidate_equal_p (t->expr, x))
2200 : : break;
2201 : :
2202 : 98657 : if (!t)
2203 : : {
2204 : 48571 : t = XOBNEW (&tlist_obstack, struct tlist_cache);
2205 : 48571 : t->next = save_expr_cache;
2206 : 48571 : t->expr = x;
2207 : 48571 : save_expr_cache = t;
2208 : :
2209 : 48571 : tmp_before = tmp_nosp = 0;
2210 : 48571 : verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2211 : 48571 : warn_for_collisions (tmp_nosp);
2212 : :
2213 : 48571 : tmp_list3 = 0;
2214 : 48571 : merge_tlist (&tmp_list3, tmp_nosp, 0);
2215 : 48571 : t->cache_before_sp = tmp_before;
2216 : 48571 : t->cache_after_sp = tmp_list3;
2217 : : }
2218 : 98657 : merge_tlist (pbefore_sp, t->cache_before_sp, 1);
2219 : 98657 : add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
2220 : 98657 : return;
2221 : : }
2222 : :
2223 : 7115589 : case ADDR_EXPR:
2224 : 7115589 : x = TREE_OPERAND (x, 0);
2225 : 7115589 : if (DECL_P (x))
2226 : : return;
2227 : 502170 : writer = 0;
2228 : 502170 : goto restart;
2229 : :
2230 : 12006615 : case VIEW_CONVERT_EXPR:
2231 : 12006615 : if (location_wrapper_p (x))
2232 : : {
2233 : 2979438 : x = TREE_OPERAND (x, 0);
2234 : 2979438 : goto restart;
2235 : : }
2236 : 9027177 : goto do_default;
2237 : :
2238 : 625312 : case LSHIFT_EXPR:
2239 : 625312 : case RSHIFT_EXPR:
2240 : 625312 : case ARRAY_REF:
2241 : 625312 : if (cxx_dialect >= cxx17)
2242 : 48755 : goto sequenced_binary;
2243 : 576557 : goto do_default;
2244 : :
2245 : 1292154 : case COMPONENT_REF:
2246 : : /* Treat as unary, the other operands aren't evaluated. */
2247 : 1292154 : x = TREE_OPERAND (x, 0);
2248 : 1292154 : writer = 0;
2249 : 1292154 : goto restart;
2250 : :
2251 : 49789622 : default:
2252 : 49789622 : do_default:
2253 : : /* For other expressions, simply recurse on their operands.
2254 : : Manual tail recursion for unary expressions.
2255 : : Other non-expressions need not be processed. */
2256 : 49789622 : if (cl == tcc_unary)
2257 : : {
2258 : 8598770 : x = TREE_OPERAND (x, 0);
2259 : 8598770 : writer = 0;
2260 : 8598770 : goto restart;
2261 : : }
2262 : 41190852 : else if (IS_EXPR_CODE_CLASS (cl))
2263 : : {
2264 : 14994798 : int lp;
2265 : 14994798 : int max = TREE_OPERAND_LENGTH (x);
2266 : 50857739 : for (lp = 0; lp < max; lp++)
2267 : : {
2268 : 20868143 : tmp_before = tmp_nosp = 0;
2269 : 20868143 : verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
2270 : 20868143 : merge_tlist (&tmp_nosp, tmp_before, 0);
2271 : 41736286 : add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2272 : : }
2273 : : }
2274 : : return;
2275 : : }
2276 : : }
2277 : :
2278 : : static constexpr size_t verify_sequence_points_limit = 1024;
2279 : :
2280 : : /* Called from verify_sequence_points via walk_tree. */
2281 : :
2282 : : static tree
2283 : 89678284 : verify_tree_lim_r (tree *tp, int *walk_subtrees, void *data)
2284 : : {
2285 : 89678284 : if (++*((size_t *) data) > verify_sequence_points_limit)
2286 : 57 : return integer_zero_node;
2287 : :
2288 : 89678227 : if (TYPE_P (*tp))
2289 : 5997 : *walk_subtrees = 0;
2290 : :
2291 : : return NULL_TREE;
2292 : : }
2293 : :
2294 : : /* Try to warn for undefined behavior in EXPR due to missing sequence
2295 : : points. */
2296 : :
2297 : : void
2298 : 9104255 : verify_sequence_points (tree expr)
2299 : : {
2300 : 9104255 : tlist *before_sp = nullptr, *after_sp = nullptr;
2301 : :
2302 : : /* verify_tree is highly recursive, and merge_tlist is O(n^2),
2303 : : so we return early if the expression is too big. */
2304 : 9104255 : size_t n = 0;
2305 : 9104255 : if (walk_tree (&expr, verify_tree_lim_r, &n, nullptr))
2306 : 57 : return;
2307 : :
2308 : 9104198 : warned_ids = nullptr;
2309 : 9104198 : save_expr_cache = nullptr;
2310 : 9104198 : if (!tlist_firstobj)
2311 : : {
2312 : 9907 : gcc_obstack_init (&tlist_obstack);
2313 : 9907 : tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
2314 : : }
2315 : :
2316 : 9104198 : verify_tree (expr, &before_sp, &after_sp, NULL_TREE);
2317 : 9104198 : warn_for_collisions (after_sp);
2318 : 9104198 : obstack_free (&tlist_obstack, tlist_firstobj);
2319 : : }
2320 : :
2321 : : /* Validate the expression after `case' and apply default promotions. */
2322 : :
2323 : : static tree
2324 : 2741423 : check_case_value (location_t loc, tree value)
2325 : : {
2326 : 2741423 : if (value == NULL_TREE)
2327 : : return value;
2328 : :
2329 : 4565221 : if (INTEGRAL_TYPE_P (TREE_TYPE (value))
2330 : 4565118 : && TREE_CODE (value) == INTEGER_CST)
2331 : : /* Promote char or short to int. */
2332 : 2741272 : value = perform_integral_promotions (value);
2333 : 151 : else if (value != error_mark_node)
2334 : : {
2335 : 53 : error_at (loc, "case label does not reduce to an integer constant");
2336 : 53 : value = error_mark_node;
2337 : : }
2338 : :
2339 : 2741423 : constant_expression_warning (value);
2340 : :
2341 : 2741423 : return value;
2342 : : }
2343 : :
2344 : : /* Return an integer type with BITS bits of precision,
2345 : : that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2346 : :
2347 : : tree
2348 : 28383908 : c_common_type_for_size (unsigned int bits, int unsignedp)
2349 : : {
2350 : 28383908 : int i;
2351 : :
2352 : 28383908 : if (bits == TYPE_PRECISION (integer_type_node))
2353 : 18733208 : return unsignedp ? unsigned_type_node : integer_type_node;
2354 : :
2355 : 9650700 : if (bits == TYPE_PRECISION (signed_char_type_node))
2356 : 2618532 : return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2357 : :
2358 : 7032168 : if (bits == TYPE_PRECISION (short_integer_type_node))
2359 : 395802 : return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2360 : :
2361 : 6636366 : if (bits == TYPE_PRECISION (long_integer_type_node))
2362 : 5955755 : return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2363 : :
2364 : 680611 : if (bits == TYPE_PRECISION (long_long_integer_type_node))
2365 : 8670 : return (unsignedp ? long_long_unsigned_type_node
2366 : 7941 : : long_long_integer_type_node);
2367 : :
2368 : 1053859 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
2369 : 671941 : if (int_n_enabled_p[i]
2370 : 665353 : && bits == int_n_data[i].bitsize)
2371 : 290023 : return (unsignedp ? int_n_trees[i].unsigned_type
2372 : 290023 : : int_n_trees[i].signed_type);
2373 : :
2374 : 381918 : if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2375 : 0 : return (unsignedp ? widest_unsigned_literal_type_node
2376 : 0 : : widest_integer_literal_type_node);
2377 : :
2378 : 1511658 : for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
2379 : : {
2380 : 1129740 : tree type = TREE_VALUE (t);
2381 : 1129740 : if (TREE_CODE (type) == INTEGER_TYPE
2382 : 0 : && bits == TYPE_PRECISION (type)
2383 : 1129740 : && !!unsignedp == !!TYPE_UNSIGNED (type))
2384 : : return type;
2385 : : }
2386 : :
2387 : 381918 : if (bits <= TYPE_PRECISION (intQI_type_node))
2388 : 320458 : return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2389 : :
2390 : 61460 : if (bits <= TYPE_PRECISION (intHI_type_node))
2391 : 9651 : return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2392 : :
2393 : 51809 : if (bits <= TYPE_PRECISION (intSI_type_node))
2394 : 20750 : return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2395 : :
2396 : 31059 : if (bits <= TYPE_PRECISION (intDI_type_node))
2397 : 24962 : return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2398 : :
2399 : 6097 : if (bits <= TYPE_PRECISION (widest_integer_literal_type_node))
2400 : 31 : return (unsignedp ? widest_unsigned_literal_type_node
2401 : 14 : : widest_integer_literal_type_node);
2402 : :
2403 : : return NULL_TREE;
2404 : : }
2405 : :
2406 : : /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2407 : : that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2408 : : and saturating if SATP is nonzero, otherwise not saturating. */
2409 : :
2410 : : tree
2411 : 0 : c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2412 : : int unsignedp, int satp)
2413 : : {
2414 : 0 : enum mode_class mclass;
2415 : 0 : if (ibit == 0)
2416 : 0 : mclass = unsignedp ? MODE_UFRACT : MODE_FRACT;
2417 : : else
2418 : 0 : mclass = unsignedp ? MODE_UACCUM : MODE_ACCUM;
2419 : :
2420 : 0 : opt_scalar_mode opt_mode;
2421 : 0 : scalar_mode mode;
2422 : 0 : FOR_EACH_MODE_IN_CLASS (opt_mode, mclass)
2423 : : {
2424 : 0 : mode = opt_mode.require ();
2425 : 0 : if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2426 : : break;
2427 : : }
2428 : :
2429 : 0 : if (!opt_mode.exists (&mode) || !targetm.scalar_mode_supported_p (mode))
2430 : : {
2431 : 0 : sorry ("GCC cannot support operators with integer types and "
2432 : : "fixed-point types that have too many integral and "
2433 : : "fractional bits together");
2434 : 0 : return NULL_TREE;
2435 : : }
2436 : :
2437 : 0 : return c_common_type_for_mode (mode, satp);
2438 : : }
2439 : :
2440 : : /* Used for communication between c_common_type_for_mode and
2441 : : c_register_builtin_type. */
2442 : : tree registered_builtin_types;
2443 : :
2444 : : /* Return a data type that has machine mode MODE.
2445 : : If the mode is an integer,
2446 : : then UNSIGNEDP selects between signed and unsigned types.
2447 : : If the mode is a fixed-point mode,
2448 : : then UNSIGNEDP selects between saturating and nonsaturating types. */
2449 : :
2450 : : tree
2451 : 12869637 : c_common_type_for_mode (machine_mode mode, int unsignedp)
2452 : : {
2453 : 12869637 : tree t;
2454 : 12869637 : int i;
2455 : :
2456 : 12869637 : if (mode == TYPE_MODE (integer_type_node))
2457 : 1776952 : return unsignedp ? unsigned_type_node : integer_type_node;
2458 : :
2459 : 11092685 : if (mode == TYPE_MODE (signed_char_type_node))
2460 : 2177512 : return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2461 : :
2462 : 8915173 : if (mode == TYPE_MODE (short_integer_type_node))
2463 : 122306 : return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2464 : :
2465 : 8792867 : if (mode == TYPE_MODE (long_integer_type_node))
2466 : 5542291 : return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2467 : :
2468 : 3250576 : if (mode == TYPE_MODE (long_long_integer_type_node))
2469 : 82512 : return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2470 : :
2471 : 4940610 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
2472 : 3168064 : if (int_n_enabled_p[i]
2473 : 3168064 : && mode == int_n_data[i].m)
2474 : 1395518 : return (unsignedp ? int_n_trees[i].unsigned_type
2475 : 1395518 : : int_n_trees[i].signed_type);
2476 : :
2477 : 1772546 : if (mode == QImode)
2478 : 0 : return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2479 : :
2480 : 1772546 : if (mode == HImode)
2481 : 0 : return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2482 : :
2483 : 1772546 : if (mode == SImode)
2484 : 0 : return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2485 : :
2486 : 1772546 : if (mode == DImode)
2487 : 0 : return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2488 : :
2489 : : #if HOST_BITS_PER_WIDE_INT >= 64
2490 : 1772546 : if (mode == TYPE_MODE (intTI_type_node))
2491 : 0 : return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2492 : : #endif
2493 : :
2494 : 1772546 : if (mode == TYPE_MODE (float_type_node))
2495 : 53572 : return float_type_node;
2496 : :
2497 : 1718974 : if (mode == TYPE_MODE (double_type_node))
2498 : 47437 : return double_type_node;
2499 : :
2500 : 1671537 : if (mode == TYPE_MODE (long_double_type_node))
2501 : 17600 : return long_double_type_node;
2502 : :
2503 : 12592722 : for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2504 : 11093152 : if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2505 : 11093152 : && mode == TYPE_MODE (FLOATN_NX_TYPE_NODE (i)))
2506 : 154367 : return FLOATN_NX_TYPE_NODE (i);
2507 : :
2508 : 1499570 : if (mode == TYPE_MODE (void_type_node))
2509 : 0 : return void_type_node;
2510 : :
2511 : 1499570 : if (mode == TYPE_MODE (build_pointer_type (char_type_node))
2512 : 1499570 : || mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2513 : : {
2514 : 0 : unsigned int precision
2515 : 0 : = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode));
2516 : 0 : return (unsignedp
2517 : 0 : ? make_unsigned_type (precision)
2518 : 0 : : make_signed_type (precision));
2519 : : }
2520 : :
2521 : 1499570 : if (COMPLEX_MODE_P (mode))
2522 : : {
2523 : 1262404 : machine_mode inner_mode;
2524 : 1262404 : tree inner_type;
2525 : :
2526 : 1262404 : if (mode == TYPE_MODE (complex_float_type_node))
2527 : 211155 : return complex_float_type_node;
2528 : 1051249 : if (mode == TYPE_MODE (complex_double_type_node))
2529 : 210213 : return complex_double_type_node;
2530 : 841036 : if (mode == TYPE_MODE (complex_long_double_type_node))
2531 : 210207 : return complex_long_double_type_node;
2532 : :
2533 : 2732392 : for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2534 : 2522452 : if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2535 : 2522452 : && mode == TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i)))
2536 : 420889 : return COMPLEX_FLOATN_NX_TYPE_NODE (i);
2537 : :
2538 : 209940 : if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2539 : 12 : return complex_integer_type_node;
2540 : :
2541 : 209928 : inner_mode = GET_MODE_INNER (mode);
2542 : 209928 : inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2543 : 209928 : if (inner_type != NULL_TREE)
2544 : 209928 : return build_complex_type (inner_type);
2545 : : }
2546 : 237166 : else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
2547 : 237166 : && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
2548 : : {
2549 : 0 : unsigned int elem_bits = vector_element_size (GET_MODE_PRECISION (mode),
2550 : : GET_MODE_NUNITS (mode));
2551 : 0 : tree bool_type = build_nonstandard_boolean_type (elem_bits);
2552 : 0 : return build_vector_type_for_mode (bool_type, mode);
2553 : : }
2554 : 225159 : else if (VECTOR_MODE_P (mode)
2555 : 251363 : && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
2556 : : {
2557 : 14197 : machine_mode inner_mode = GET_MODE_INNER (mode);
2558 : 14197 : tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2559 : 14197 : if (inner_type != NULL_TREE)
2560 : 14197 : return build_vector_type_for_mode (inner_type, mode);
2561 : : }
2562 : :
2563 : 222969 : if (dfloat32_type_node != NULL_TREE
2564 : 222969 : && mode == TYPE_MODE (dfloat32_type_node))
2565 : 2794 : return dfloat32_type_node;
2566 : 220175 : if (dfloat64_type_node != NULL_TREE
2567 : 220175 : && mode == TYPE_MODE (dfloat64_type_node))
2568 : 3101 : return dfloat64_type_node;
2569 : 217074 : if (dfloat128_type_node != NULL_TREE
2570 : 217074 : && mode == TYPE_MODE (dfloat128_type_node))
2571 : 2425 : return dfloat128_type_node;
2572 : :
2573 : 214649 : if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
2574 : : {
2575 : 0 : if (mode == TYPE_MODE (short_fract_type_node))
2576 : 0 : return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
2577 : 0 : if (mode == TYPE_MODE (fract_type_node))
2578 : 0 : return unsignedp ? sat_fract_type_node : fract_type_node;
2579 : 0 : if (mode == TYPE_MODE (long_fract_type_node))
2580 : 0 : return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
2581 : 0 : if (mode == TYPE_MODE (long_long_fract_type_node))
2582 : 0 : return unsignedp ? sat_long_long_fract_type_node
2583 : 0 : : long_long_fract_type_node;
2584 : :
2585 : 0 : if (mode == TYPE_MODE (unsigned_short_fract_type_node))
2586 : 0 : return unsignedp ? sat_unsigned_short_fract_type_node
2587 : 0 : : unsigned_short_fract_type_node;
2588 : 0 : if (mode == TYPE_MODE (unsigned_fract_type_node))
2589 : 0 : return unsignedp ? sat_unsigned_fract_type_node
2590 : 0 : : unsigned_fract_type_node;
2591 : 0 : if (mode == TYPE_MODE (unsigned_long_fract_type_node))
2592 : 0 : return unsignedp ? sat_unsigned_long_fract_type_node
2593 : 0 : : unsigned_long_fract_type_node;
2594 : 0 : if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
2595 : 0 : return unsignedp ? sat_unsigned_long_long_fract_type_node
2596 : 0 : : unsigned_long_long_fract_type_node;
2597 : :
2598 : 0 : if (mode == TYPE_MODE (short_accum_type_node))
2599 : 0 : return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
2600 : 0 : if (mode == TYPE_MODE (accum_type_node))
2601 : 0 : return unsignedp ? sat_accum_type_node : accum_type_node;
2602 : 0 : if (mode == TYPE_MODE (long_accum_type_node))
2603 : 0 : return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
2604 : 0 : if (mode == TYPE_MODE (long_long_accum_type_node))
2605 : 0 : return unsignedp ? sat_long_long_accum_type_node
2606 : 0 : : long_long_accum_type_node;
2607 : :
2608 : 0 : if (mode == TYPE_MODE (unsigned_short_accum_type_node))
2609 : 0 : return unsignedp ? sat_unsigned_short_accum_type_node
2610 : 0 : : unsigned_short_accum_type_node;
2611 : 0 : if (mode == TYPE_MODE (unsigned_accum_type_node))
2612 : 0 : return unsignedp ? sat_unsigned_accum_type_node
2613 : 0 : : unsigned_accum_type_node;
2614 : 0 : if (mode == TYPE_MODE (unsigned_long_accum_type_node))
2615 : 0 : return unsignedp ? sat_unsigned_long_accum_type_node
2616 : 0 : : unsigned_long_accum_type_node;
2617 : 0 : if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
2618 : 0 : return unsignedp ? sat_unsigned_long_long_accum_type_node
2619 : 0 : : unsigned_long_long_accum_type_node;
2620 : :
2621 : 0 : if (mode == QQmode)
2622 : 0 : return unsignedp ? sat_qq_type_node : qq_type_node;
2623 : : if (mode == HQmode)
2624 : 0 : return unsignedp ? sat_hq_type_node : hq_type_node;
2625 : : if (mode == SQmode)
2626 : 0 : return unsignedp ? sat_sq_type_node : sq_type_node;
2627 : : if (mode == DQmode)
2628 : 0 : return unsignedp ? sat_dq_type_node : dq_type_node;
2629 : : if (mode == TQmode)
2630 : 0 : return unsignedp ? sat_tq_type_node : tq_type_node;
2631 : :
2632 : : if (mode == UQQmode)
2633 : 0 : return unsignedp ? sat_uqq_type_node : uqq_type_node;
2634 : : if (mode == UHQmode)
2635 : 0 : return unsignedp ? sat_uhq_type_node : uhq_type_node;
2636 : : if (mode == USQmode)
2637 : 0 : return unsignedp ? sat_usq_type_node : usq_type_node;
2638 : : if (mode == UDQmode)
2639 : 0 : return unsignedp ? sat_udq_type_node : udq_type_node;
2640 : : if (mode == UTQmode)
2641 : 0 : return unsignedp ? sat_utq_type_node : utq_type_node;
2642 : :
2643 : : if (mode == HAmode)
2644 : 0 : return unsignedp ? sat_ha_type_node : ha_type_node;
2645 : : if (mode == SAmode)
2646 : 0 : return unsignedp ? sat_sa_type_node : sa_type_node;
2647 : : if (mode == DAmode)
2648 : 0 : return unsignedp ? sat_da_type_node : da_type_node;
2649 : : if (mode == TAmode)
2650 : 0 : return unsignedp ? sat_ta_type_node : ta_type_node;
2651 : :
2652 : : if (mode == UHAmode)
2653 : 0 : return unsignedp ? sat_uha_type_node : uha_type_node;
2654 : : if (mode == USAmode)
2655 : 0 : return unsignedp ? sat_usa_type_node : usa_type_node;
2656 : : if (mode == UDAmode)
2657 : 0 : return unsignedp ? sat_uda_type_node : uda_type_node;
2658 : : if (mode == UTAmode)
2659 : 0 : return unsignedp ? sat_uta_type_node : uta_type_node;
2660 : : }
2661 : :
2662 : 217261 : for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
2663 : : {
2664 : 216405 : tree type = TREE_VALUE (t);
2665 : 216405 : if (TYPE_MODE (type) == mode
2666 : 213896 : && VECTOR_TYPE_P (type) == VECTOR_MODE_P (mode)
2667 : 430301 : && !!unsignedp == !!TYPE_UNSIGNED (type))
2668 : : return type;
2669 : : }
2670 : : return NULL_TREE;
2671 : : }
2672 : :
2673 : : tree
2674 : 1300407 : c_common_unsigned_type (tree type)
2675 : : {
2676 : 1300407 : return c_common_signed_or_unsigned_type (1, type);
2677 : : }
2678 : :
2679 : : /* Return a signed type the same as TYPE in other respects. */
2680 : :
2681 : : tree
2682 : 61494776 : c_common_signed_type (tree type)
2683 : : {
2684 : 61494776 : return c_common_signed_or_unsigned_type (0, type);
2685 : : }
2686 : :
2687 : : /* Return a type the same as TYPE except unsigned or
2688 : : signed according to UNSIGNEDP. */
2689 : :
2690 : : tree
2691 : 65747704 : c_common_signed_or_unsigned_type (int unsignedp, tree type)
2692 : : {
2693 : 65747704 : tree type1;
2694 : 65747704 : int i;
2695 : :
2696 : : /* This block of code emulates the behavior of the old
2697 : : c_common_unsigned_type. In particular, it returns
2698 : : long_unsigned_type_node if passed a long, even when a int would
2699 : : have the same size. This is necessary for warnings to work
2700 : : correctly in archs where sizeof(int) == sizeof(long) */
2701 : :
2702 : 65747704 : type1 = TYPE_MAIN_VARIANT (type);
2703 : 65747704 : if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
2704 : 2714292 : return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2705 : 63033412 : if (type1 == integer_type_node || type1 == unsigned_type_node)
2706 : 18191801 : return unsignedp ? unsigned_type_node : integer_type_node;
2707 : 44841611 : if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
2708 : 6188705 : return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2709 : 38652906 : if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
2710 : 24142166 : return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2711 : 14510740 : if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
2712 : 2563105 : return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2713 : :
2714 : 23209633 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
2715 : 11947635 : if (int_n_enabled_p[i]
2716 : 10477395 : && (type1 == int_n_trees[i].unsigned_type
2717 : 9792474 : || type1 == int_n_trees[i].signed_type))
2718 : 685637 : return (unsignedp ? int_n_trees[i].unsigned_type
2719 : 685637 : : int_n_trees[i].signed_type);
2720 : :
2721 : : #if HOST_BITS_PER_WIDE_INT >= 64
2722 : 11261998 : if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
2723 : 0 : return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2724 : : #endif
2725 : 11261998 : if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
2726 : 0 : return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2727 : 11261998 : if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
2728 : 0 : return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2729 : 11261998 : if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
2730 : 0 : return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2731 : 11261998 : if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
2732 : 0 : return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2733 : :
2734 : : #define C_COMMON_FIXED_TYPES(NAME) \
2735 : : if (type1 == short_ ## NAME ## _type_node \
2736 : : || type1 == unsigned_short_ ## NAME ## _type_node) \
2737 : : return unsignedp ? unsigned_short_ ## NAME ## _type_node \
2738 : : : short_ ## NAME ## _type_node; \
2739 : : if (type1 == NAME ## _type_node \
2740 : : || type1 == unsigned_ ## NAME ## _type_node) \
2741 : : return unsignedp ? unsigned_ ## NAME ## _type_node \
2742 : : : NAME ## _type_node; \
2743 : : if (type1 == long_ ## NAME ## _type_node \
2744 : : || type1 == unsigned_long_ ## NAME ## _type_node) \
2745 : : return unsignedp ? unsigned_long_ ## NAME ## _type_node \
2746 : : : long_ ## NAME ## _type_node; \
2747 : : if (type1 == long_long_ ## NAME ## _type_node \
2748 : : || type1 == unsigned_long_long_ ## NAME ## _type_node) \
2749 : : return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
2750 : : : long_long_ ## NAME ## _type_node;
2751 : :
2752 : : #define C_COMMON_FIXED_MODE_TYPES(NAME) \
2753 : : if (type1 == NAME ## _type_node \
2754 : : || type1 == u ## NAME ## _type_node) \
2755 : : return unsignedp ? u ## NAME ## _type_node \
2756 : : : NAME ## _type_node;
2757 : :
2758 : : #define C_COMMON_FIXED_TYPES_SAT(NAME) \
2759 : : if (type1 == sat_ ## short_ ## NAME ## _type_node \
2760 : : || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
2761 : : return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
2762 : : : sat_ ## short_ ## NAME ## _type_node; \
2763 : : if (type1 == sat_ ## NAME ## _type_node \
2764 : : || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
2765 : : return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
2766 : : : sat_ ## NAME ## _type_node; \
2767 : : if (type1 == sat_ ## long_ ## NAME ## _type_node \
2768 : : || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
2769 : : return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
2770 : : : sat_ ## long_ ## NAME ## _type_node; \
2771 : : if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
2772 : : || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
2773 : : return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
2774 : : : sat_ ## long_long_ ## NAME ## _type_node;
2775 : :
2776 : : #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
2777 : : if (type1 == sat_ ## NAME ## _type_node \
2778 : : || type1 == sat_ ## u ## NAME ## _type_node) \
2779 : : return unsignedp ? sat_ ## u ## NAME ## _type_node \
2780 : : : sat_ ## NAME ## _type_node;
2781 : :
2782 : 11261998 : C_COMMON_FIXED_TYPES (fract);
2783 : 11261998 : C_COMMON_FIXED_TYPES_SAT (fract);
2784 : 11261998 : C_COMMON_FIXED_TYPES (accum);
2785 : 11261998 : C_COMMON_FIXED_TYPES_SAT (accum);
2786 : :
2787 : 11261998 : C_COMMON_FIXED_MODE_TYPES (qq);
2788 : 11261998 : C_COMMON_FIXED_MODE_TYPES (hq);
2789 : 11261998 : C_COMMON_FIXED_MODE_TYPES (sq);
2790 : 11261998 : C_COMMON_FIXED_MODE_TYPES (dq);
2791 : 11261998 : C_COMMON_FIXED_MODE_TYPES (tq);
2792 : 11261998 : C_COMMON_FIXED_MODE_TYPES_SAT (qq);
2793 : 11261998 : C_COMMON_FIXED_MODE_TYPES_SAT (hq);
2794 : 11261998 : C_COMMON_FIXED_MODE_TYPES_SAT (sq);
2795 : 11261998 : C_COMMON_FIXED_MODE_TYPES_SAT (dq);
2796 : 11261998 : C_COMMON_FIXED_MODE_TYPES_SAT (tq);
2797 : 11261998 : C_COMMON_FIXED_MODE_TYPES (ha);
2798 : 11261998 : C_COMMON_FIXED_MODE_TYPES (sa);
2799 : 11261998 : C_COMMON_FIXED_MODE_TYPES (da);
2800 : 11261998 : C_COMMON_FIXED_MODE_TYPES (ta);
2801 : 11261998 : C_COMMON_FIXED_MODE_TYPES_SAT (ha);
2802 : 11261998 : C_COMMON_FIXED_MODE_TYPES_SAT (sa);
2803 : 11261998 : C_COMMON_FIXED_MODE_TYPES_SAT (da);
2804 : 11261998 : C_COMMON_FIXED_MODE_TYPES_SAT (ta);
2805 : :
2806 : : /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
2807 : : the precision; they have precision set to match their range, but
2808 : : may use a wider mode to match an ABI. If we change modes, we may
2809 : : wind up with bad conversions. For INTEGER_TYPEs in C, must check
2810 : : the precision as well, so as to yield correct results for
2811 : : bit-field types. C++ does not have these separate bit-field
2812 : : types, and producing a signed or unsigned variant of an
2813 : : ENUMERAL_TYPE may cause other problems as well. */
2814 : :
2815 : 11261998 : if (!INTEGRAL_TYPE_P (type)
2816 : 11261998 : || TYPE_UNSIGNED (type) == unsignedp)
2817 : : return type;
2818 : :
2819 : 10281373 : if (TREE_CODE (type) == BITINT_TYPE
2820 : : /* signed _BitInt(1) is invalid, avoid creating that. */
2821 : 10281373 : && (unsignedp || TYPE_PRECISION (type) > 1))
2822 : 1469580 : return build_bitint_type (TYPE_PRECISION (type), unsignedp);
2823 : :
2824 : : #define TYPE_OK(node) \
2825 : : (TYPE_MODE (type) == TYPE_MODE (node) \
2826 : : && TYPE_PRECISION (type) == TYPE_PRECISION (node))
2827 : 8811793 : if (TYPE_OK (signed_char_type_node))
2828 : 494258 : return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2829 : 8317535 : if (TYPE_OK (integer_type_node))
2830 : 4120205 : return unsignedp ? unsigned_type_node : integer_type_node;
2831 : 4197330 : if (TYPE_OK (short_integer_type_node))
2832 : 887099 : return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2833 : 3310231 : if (TYPE_OK (long_integer_type_node))
2834 : 2830625 : return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2835 : 479606 : if (TYPE_OK (long_long_integer_type_node))
2836 : 127810 : return (unsignedp ? long_long_unsigned_type_node
2837 : 0 : : long_long_integer_type_node);
2838 : :
2839 : 683327 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
2840 : 351796 : if (int_n_enabled_p[i]
2841 : 339716 : && TYPE_MODE (type) == int_n_data[i].m
2842 : 372125 : && TYPE_PRECISION (type) == int_n_data[i].bitsize)
2843 : 20265 : return (unsignedp ? int_n_trees[i].unsigned_type
2844 : 20265 : : int_n_trees[i].signed_type);
2845 : :
2846 : : #if HOST_BITS_PER_WIDE_INT >= 64
2847 : 331531 : if (TYPE_OK (intTI_type_node))
2848 : 0 : return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2849 : : #endif
2850 : 331531 : if (TYPE_OK (intDI_type_node))
2851 : 0 : return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2852 : 331531 : if (TYPE_OK (intSI_type_node))
2853 : 0 : return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2854 : 331531 : if (TYPE_OK (intHI_type_node))
2855 : 0 : return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2856 : 331531 : if (TYPE_OK (intQI_type_node))
2857 : 0 : return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2858 : : #undef TYPE_OK
2859 : :
2860 : 331531 : return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
2861 : : }
2862 : :
2863 : : /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
2864 : :
2865 : : tree
2866 : 325550 : c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
2867 : : {
2868 : 325550 : int i;
2869 : :
2870 : : /* Extended integer types of the same width as a standard type have
2871 : : lesser rank, so those of the same width as int promote to int or
2872 : : unsigned int and are valid for printf formats expecting int or
2873 : : unsigned int. To avoid such special cases, avoid creating
2874 : : extended integer types for bit-fields if a standard integer type
2875 : : is available. */
2876 : 325550 : if (width == TYPE_PRECISION (integer_type_node))
2877 : 138 : return unsignedp ? unsigned_type_node : integer_type_node;
2878 : 325412 : if (width == TYPE_PRECISION (signed_char_type_node))
2879 : 7011 : return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2880 : 318401 : if (width == TYPE_PRECISION (short_integer_type_node))
2881 : 1426 : return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2882 : 316975 : if (width == TYPE_PRECISION (long_integer_type_node))
2883 : 0 : return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2884 : 316975 : if (width == TYPE_PRECISION (long_long_integer_type_node))
2885 : 0 : return (unsignedp ? long_long_unsigned_type_node
2886 : 0 : : long_long_integer_type_node);
2887 : 633950 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
2888 : 316975 : if (int_n_enabled_p[i]
2889 : 309996 : && width == int_n_data[i].bitsize)
2890 : 0 : return (unsignedp ? int_n_trees[i].unsigned_type
2891 : 0 : : int_n_trees[i].signed_type);
2892 : 316975 : return build_nonstandard_integer_type (width, unsignedp);
2893 : : }
2894 : :
2895 : : /* The C version of the register_builtin_type langhook. */
2896 : :
2897 : : void
2898 : 629694 : c_register_builtin_type (tree type, const char* name)
2899 : : {
2900 : 629694 : tree decl;
2901 : :
2902 : 629694 : decl = build_decl (UNKNOWN_LOCATION,
2903 : : TYPE_DECL, get_identifier (name), type);
2904 : 629694 : DECL_ARTIFICIAL (decl) = 1;
2905 : 629694 : if (!TYPE_NAME (type))
2906 : 306554 : TYPE_NAME (type) = decl;
2907 : 629694 : lang_hooks.decls.pushdecl (decl);
2908 : :
2909 : 629694 : registered_builtin_types = tree_cons (0, type, registered_builtin_types);
2910 : 629694 : }
2911 : :
2912 : : /* Print an error message for invalid operands to arith operation
2913 : : CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
2914 : : RICHLOC is a rich location for the message, containing either
2915 : : three separate locations for each of the operator and operands
2916 : :
2917 : : lhs op rhs
2918 : : ~~~ ^~ ~~~
2919 : :
2920 : : (C FE), or one location ranging over all over them
2921 : :
2922 : : lhs op rhs
2923 : : ~~~~^~~~~~
2924 : :
2925 : : (C++ FE). */
2926 : :
2927 : : void
2928 : 558 : binary_op_error (rich_location *richloc, enum tree_code code,
2929 : : tree type0, tree type1)
2930 : : {
2931 : 558 : const char *opname;
2932 : :
2933 : 558 : switch (code)
2934 : : {
2935 : : case PLUS_EXPR:
2936 : : opname = "+"; break;
2937 : 14 : case MINUS_EXPR:
2938 : 14 : opname = "-"; break;
2939 : 378 : case MULT_EXPR:
2940 : 378 : opname = "*"; break;
2941 : 0 : case MAX_EXPR:
2942 : 0 : opname = "max"; break;
2943 : 0 : case MIN_EXPR:
2944 : 0 : opname = "min"; break;
2945 : 2 : case EQ_EXPR:
2946 : 2 : opname = "=="; break;
2947 : 2 : case NE_EXPR:
2948 : 2 : opname = "!="; break;
2949 : 0 : case LE_EXPR:
2950 : 0 : opname = "<="; break;
2951 : 0 : case GE_EXPR:
2952 : 0 : opname = ">="; break;
2953 : 1 : case LT_EXPR:
2954 : 1 : opname = "<"; break;
2955 : 1 : case GT_EXPR:
2956 : 1 : opname = ">"; break;
2957 : 21 : case LSHIFT_EXPR:
2958 : 21 : opname = "<<"; break;
2959 : 16 : case RSHIFT_EXPR:
2960 : 16 : opname = ">>"; break;
2961 : 3 : case TRUNC_MOD_EXPR:
2962 : 3 : case FLOOR_MOD_EXPR:
2963 : 3 : opname = "%"; break;
2964 : 6 : case TRUNC_DIV_EXPR:
2965 : 6 : case FLOOR_DIV_EXPR:
2966 : 6 : opname = "/"; break;
2967 : 40 : case BIT_AND_EXPR:
2968 : 40 : opname = "&"; break;
2969 : 21 : case BIT_IOR_EXPR:
2970 : 21 : opname = "|"; break;
2971 : 0 : case TRUTH_ANDIF_EXPR:
2972 : 0 : opname = "&&"; break;
2973 : 0 : case TRUTH_ORIF_EXPR:
2974 : 0 : opname = "||"; break;
2975 : 22 : case BIT_XOR_EXPR:
2976 : 22 : opname = "^"; break;
2977 : 0 : default:
2978 : 0 : gcc_unreachable ();
2979 : : }
2980 : 558 : pp_markup::element_quoted_type element_0 (type0, highlight_colors::lhs);
2981 : 1116 : pp_markup::element_quoted_type element_1 (type1, highlight_colors::rhs);
2982 : 558 : error_at (richloc,
2983 : : "invalid operands to binary %s (have %e and %e)",
2984 : : opname, &element_0, &element_1);
2985 : 558 : }
2986 : :
2987 : : /* Given an expression as a tree, return its original type. Do this
2988 : : by stripping any conversion that preserves the sign and precision. */
2989 : : static tree
2990 : 55545 : expr_original_type (tree expr)
2991 : : {
2992 : 55545 : STRIP_SIGN_NOPS (expr);
2993 : 55545 : return TREE_TYPE (expr);
2994 : : }
2995 : :
2996 : : /* Subroutine of build_binary_op, used for comparison operations.
2997 : : See if the operands have both been converted from subword integer types
2998 : : and, if so, perhaps change them both back to their original type.
2999 : : This function is also responsible for converting the two operands
3000 : : to the proper common type for comparison.
3001 : :
3002 : : The arguments of this function are all pointers to local variables
3003 : : of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3004 : : RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3005 : :
3006 : : LOC is the location of the comparison.
3007 : :
3008 : : If this function returns non-NULL_TREE, it means that the comparison has
3009 : : a constant value. What this function returns is an expression for
3010 : : that value. */
3011 : :
3012 : : tree
3013 : 26852971 : shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
3014 : : tree *restype_ptr, enum tree_code *rescode_ptr)
3015 : : {
3016 : 26852971 : tree type;
3017 : 26852971 : tree op0 = *op0_ptr;
3018 : 26852971 : tree op1 = *op1_ptr;
3019 : 26852971 : int unsignedp0, unsignedp1;
3020 : 26852971 : int real1, real2;
3021 : 26852971 : tree primop0, primop1;
3022 : 26852971 : enum tree_code code = *rescode_ptr;
3023 : :
3024 : : /* Throw away any conversions to wider types
3025 : : already present in the operands. */
3026 : :
3027 : 26852971 : primop0 = c_common_get_narrower (op0, &unsignedp0);
3028 : 26852971 : primop1 = c_common_get_narrower (op1, &unsignedp1);
3029 : :
3030 : : /* If primopN is first sign-extended from primopN's precision to opN's
3031 : : precision, then zero-extended from opN's precision to
3032 : : *restype_ptr precision, shortenings might be invalid. */
3033 : 26852971 : if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3034 : 1534879 : && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3035 : 5922 : && !unsignedp0
3036 : 26858622 : && TYPE_UNSIGNED (TREE_TYPE (op0)))
3037 : : primop0 = op0;
3038 : 26852971 : if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3039 : 925285 : && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3040 : 17289 : && !unsignedp1
3041 : 26864804 : && TYPE_UNSIGNED (TREE_TYPE (op1)))
3042 : : primop1 = op1;
3043 : :
3044 : : /* Handle the case that OP0 does not *contain* a conversion
3045 : : but it *requires* conversion to FINAL_TYPE. */
3046 : :
3047 : 26852971 : if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3048 : 442880 : unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3049 : 26852971 : if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3050 : 7099713 : unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3051 : :
3052 : : /* If one of the operands must be floated, we cannot optimize. */
3053 : 26852971 : real1 = SCALAR_FLOAT_TYPE_P (TREE_TYPE (primop0));
3054 : 26852971 : real2 = SCALAR_FLOAT_TYPE_P (TREE_TYPE (primop1));
3055 : :
3056 : : /* If first arg is constant, swap the args (changing operation
3057 : : so value is preserved), for canonicalization. Don't do this if
3058 : : the second arg is 0. */
3059 : :
3060 : 26852971 : if (TREE_CONSTANT (primop0)
3061 : 5716204 : && !integer_zerop (primop1) && !real_zerop (primop1)
3062 : 31018183 : && !fixed_zerop (primop1))
3063 : : {
3064 : 4165212 : std::swap (primop0, primop1);
3065 : 4165212 : std::swap (op0, op1);
3066 : 4165212 : *op0_ptr = op0;
3067 : 4165212 : *op1_ptr = op1;
3068 : 4165212 : std::swap (unsignedp0, unsignedp1);
3069 : 4165212 : std::swap (real1, real2);
3070 : :
3071 : 4165212 : switch (code)
3072 : : {
3073 : : case LT_EXPR:
3074 : : code = GT_EXPR;
3075 : : break;
3076 : : case GT_EXPR:
3077 : : code = LT_EXPR;
3078 : : break;
3079 : : case LE_EXPR:
3080 : : code = GE_EXPR;
3081 : : break;
3082 : : case GE_EXPR:
3083 : : code = LE_EXPR;
3084 : : break;
3085 : : default:
3086 : : break;
3087 : : }
3088 : 4165212 : *rescode_ptr = code;
3089 : : }
3090 : :
3091 : : /* If comparing an integer against a constant more bits wide,
3092 : : maybe we can deduce a value of 1 or 0 independent of the data.
3093 : : Or else truncate the constant now
3094 : : rather than extend the variable at run time.
3095 : :
3096 : : This is only interesting if the constant is the wider arg.
3097 : : Also, it is not safe if the constant is unsigned and the
3098 : : variable arg is signed, since in this case the variable
3099 : : would be sign-extended and then regarded as unsigned.
3100 : : Our technique fails in this case because the lowest/highest
3101 : : possible unsigned results don't follow naturally from the
3102 : : lowest/highest possible values of the variable operand.
3103 : : For just EQ_EXPR and NE_EXPR there is another technique that
3104 : : could be used: see if the constant can be faithfully represented
3105 : : in the other operand's type, by truncating it and reextending it
3106 : : and see if that preserves the constant's value. */
3107 : :
3108 : 22271007 : if (!real1 && !real2
3109 : 22246260 : && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
3110 : 22246260 : && TREE_CODE (primop1) == INTEGER_CST
3111 : 41355217 : && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3112 : : {
3113 : 1631908 : int min_gt, max_gt, min_lt, max_lt;
3114 : 1631908 : tree maxval, minval;
3115 : : /* 1 if comparison is nominally unsigned. */
3116 : 1631908 : int unsignedp = TYPE_UNSIGNED (*restype_ptr);
3117 : 1631908 : tree val;
3118 : :
3119 : 1631908 : type = c_common_signed_or_unsigned_type (unsignedp0,
3120 : 1631908 : TREE_TYPE (primop0));
3121 : :
3122 : 1631908 : maxval = TYPE_MAX_VALUE (type);
3123 : 1631908 : minval = TYPE_MIN_VALUE (type);
3124 : :
3125 : 1631908 : if (unsignedp && !unsignedp0)
3126 : 438815 : *restype_ptr = c_common_signed_type (*restype_ptr);
3127 : :
3128 : 1631908 : if (TREE_TYPE (primop1) != *restype_ptr)
3129 : : {
3130 : : /* Convert primop1 to target type, but do not introduce
3131 : : additional overflow. We know primop1 is an int_cst. */
3132 : 439119 : primop1 = force_fit_type (*restype_ptr,
3133 : : wi::to_wide
3134 : 878238 : (primop1,
3135 : 439119 : TYPE_PRECISION (*restype_ptr)),
3136 : 439119 : 0, TREE_OVERFLOW (primop1));
3137 : : }
3138 : 1631908 : if (type != *restype_ptr)
3139 : : {
3140 : 1631908 : minval = convert (*restype_ptr, minval);
3141 : 1631908 : maxval = convert (*restype_ptr, maxval);
3142 : : }
3143 : :
3144 : 1631908 : min_gt = tree_int_cst_lt (primop1, minval);
3145 : 1631908 : max_gt = tree_int_cst_lt (primop1, maxval);
3146 : 1631908 : min_lt = tree_int_cst_lt (minval, primop1);
3147 : 1631908 : max_lt = tree_int_cst_lt (maxval, primop1);
3148 : :
3149 : 1631908 : val = 0;
3150 : : /* This used to be a switch, but Genix compiler can't handle that. */
3151 : 1631908 : if (code == NE_EXPR)
3152 : : {
3153 : 359399 : if (max_lt || min_gt)
3154 : 23 : val = truthvalue_true_node;
3155 : : }
3156 : : else if (code == EQ_EXPR)
3157 : : {
3158 : 659288 : if (max_lt || min_gt)
3159 : 35 : val = truthvalue_false_node;
3160 : : }
3161 : : else if (code == LT_EXPR)
3162 : : {
3163 : 341574 : if (max_lt)
3164 : 18 : val = truthvalue_true_node;
3165 : 341574 : if (!min_lt)
3166 : 7858 : val = truthvalue_false_node;
3167 : : }
3168 : : else if (code == GT_EXPR)
3169 : : {
3170 : 121965 : if (min_gt)
3171 : 8 : val = truthvalue_true_node;
3172 : 121965 : if (!max_gt)
3173 : 6 : val = truthvalue_false_node;
3174 : : }
3175 : : else if (code == LE_EXPR)
3176 : : {
3177 : 50917 : if (!max_gt)
3178 : 74 : val = truthvalue_true_node;
3179 : 50917 : if (min_gt)
3180 : 7 : val = truthvalue_false_node;
3181 : : }
3182 : : else if (code == GE_EXPR)
3183 : : {
3184 : 98763 : if (!min_lt)
3185 : 183 : val = truthvalue_true_node;
3186 : 98763 : if (max_lt)
3187 : 14976 : val = truthvalue_false_node;
3188 : : }
3189 : :
3190 : : /* If primop0 was sign-extended and unsigned comparison specd,
3191 : : we did a signed comparison above using the signed type bounds.
3192 : : But the comparison we output must be unsigned.
3193 : :
3194 : : Also, for inequalities, VAL is no good; but if the signed
3195 : : comparison had *any* fixed result, it follows that the
3196 : : unsigned comparison just tests the sign in reverse
3197 : : (positive values are LE, negative ones GE).
3198 : : So we can generate an unsigned comparison
3199 : : against an extreme value of the signed type. */
3200 : :
3201 : 1631908 : if (unsignedp && !unsignedp0)
3202 : : {
3203 : 438815 : if (val != 0)
3204 : 112 : switch (code)
3205 : : {
3206 : 81 : case LT_EXPR:
3207 : 81 : case GE_EXPR:
3208 : 81 : primop1 = TYPE_MIN_VALUE (type);
3209 : 81 : val = 0;
3210 : 81 : break;
3211 : :
3212 : 30 : case LE_EXPR:
3213 : 30 : case GT_EXPR:
3214 : 30 : primop1 = TYPE_MAX_VALUE (type);
3215 : 30 : val = 0;
3216 : 30 : break;
3217 : :
3218 : : default:
3219 : : break;
3220 : : }
3221 : 438815 : type = c_common_unsigned_type (type);
3222 : : }
3223 : :
3224 : 1631908 : if (TREE_CODE (primop0) != INTEGER_CST
3225 : : /* Don't warn if it's from a (non-system) macro. */
3226 : 1631908 : && !(from_macro_expansion_at
3227 : 1108109 : (expansion_point_location_if_in_system_header
3228 : 1108109 : (EXPR_LOCATION (primop0)))))
3229 : : {
3230 : 1066879 : if (val == truthvalue_false_node)
3231 : 7898 : warning_at (loc, OPT_Wtype_limits,
3232 : : "comparison is always false due to limited range of data type");
3233 : 1066879 : if (val == truthvalue_true_node)
3234 : 191 : warning_at (loc, OPT_Wtype_limits,
3235 : : "comparison is always true due to limited range of data type");
3236 : : }
3237 : :
3238 : 1631908 : if (val != 0)
3239 : : {
3240 : : /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3241 : 23077 : if (TREE_SIDE_EFFECTS (primop0))
3242 : 11 : return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
3243 : : return val;
3244 : : }
3245 : :
3246 : : /* Value is not predetermined, but do the comparison
3247 : : in the type of the operand that is not constant.
3248 : : TYPE is already properly set. */
3249 : : }
3250 : :
3251 : : /* If either arg is decimal float and the other is float, find the
3252 : : proper common type to use for comparison. */
3253 : 4581964 : else if (real1 && real2
3254 : 4343498 : && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
3255 : 25237611 : && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
3256 : 16547 : type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3257 : :
3258 : : /* If either arg is decimal float and the other is float, fail. */
3259 : 4565417 : else if (real1 && real2
3260 : 29531467 : && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
3261 : 4326950 : || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
3262 : : {
3263 : 2 : type = *restype_ptr;
3264 : 2 : primop0 = op0;
3265 : 2 : primop1 = op1;
3266 : : }
3267 : :
3268 : 4565415 : else if (real1 && real2
3269 : 29531463 : && (TYPE_PRECISION (TREE_TYPE (primop0))
3270 : 4326949 : == TYPE_PRECISION (TREE_TYPE (primop1))))
3271 : 4228691 : type = TREE_TYPE (primop0);
3272 : :
3273 : : /* If args' natural types are both narrower than nominal type
3274 : : and both extend in the same manner, compare them
3275 : : in the type of the wider arg.
3276 : : Otherwise must actually extend both to the nominal
3277 : : common type lest different ways of extending
3278 : : alter the result.
3279 : : (eg, (short)-1 == (unsigned short)-1 should be 0.) */
3280 : :
3281 : 18339899 : else if (unsignedp0 == unsignedp1 && real1 == real2
3282 : 18089781 : && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
3283 : 520989 : && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr)
3284 : 21331610 : && (type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)))
3285 : 355787 : != error_mark_node)
3286 : : {
3287 : 355787 : type = c_common_signed_or_unsigned_type (unsignedp0
3288 : 355787 : || TYPE_UNSIGNED (*restype_ptr),
3289 : : type);
3290 : : /* Make sure shorter operand is extended the right way
3291 : : to match the longer operand. */
3292 : 355787 : primop0
3293 : 355787 : = convert (c_common_signed_or_unsigned_type (unsignedp0,
3294 : 355787 : TREE_TYPE (primop0)),
3295 : : primop0);
3296 : 355787 : primop1
3297 : 355787 : = convert (c_common_signed_or_unsigned_type (unsignedp1,
3298 : 355787 : TREE_TYPE (primop1)),
3299 : : primop1);
3300 : : }
3301 : : else
3302 : : {
3303 : : /* Here we must do the comparison on the nominal type
3304 : : using the args exactly as we received them. */
3305 : 20620036 : type = *restype_ptr;
3306 : 20620036 : primop0 = op0;
3307 : 20620036 : primop1 = op1;
3308 : :
3309 : : /* We want to fold unsigned comparisons of >= and < against zero.
3310 : : For these, we may also issue a warning if we have a non-constant
3311 : : compared against zero, where the zero was spelled as "0" (rather
3312 : : than merely folding to it).
3313 : : If we have at least one constant, then op1 is constant
3314 : : and we may have a non-constant expression as op0. */
3315 : 20283312 : if (!real1 && !real2 && integer_zerop (primop1)
3316 : 29081165 : && TYPE_UNSIGNED (*restype_ptr))
3317 : : {
3318 : 2827863 : tree value = NULL_TREE;
3319 : : /* All unsigned values are >= 0, so we warn. However,
3320 : : if OP0 is a constant that is >= 0, the signedness of
3321 : : the comparison isn't an issue, so suppress the
3322 : : warning. */
3323 : 2827863 : tree folded_op0 = fold_for_warn (op0);
3324 : 2827863 : bool warn =
3325 : 71203 : warn_type_limits && !in_system_header_at (loc)
3326 : 64983 : && !(TREE_CODE (folded_op0) == INTEGER_CST
3327 : 9438 : && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3328 : : folded_op0)))
3329 : : /* Do not warn for enumeration types. */
3330 : 2883408 : && (TREE_CODE (expr_original_type (folded_op0)) != ENUMERAL_TYPE);
3331 : :
3332 : 2827863 : switch (code)
3333 : : {
3334 : 12257 : case GE_EXPR:
3335 : 12257 : if (warn)
3336 : 56 : warning_at (loc, OPT_Wtype_limits,
3337 : : "comparison of unsigned expression in %<>= 0%> "
3338 : : "is always true");
3339 : 12257 : value = truthvalue_true_node;
3340 : 12257 : break;
3341 : :
3342 : 310867 : case LT_EXPR:
3343 : 310867 : if (warn)
3344 : 55 : warning_at (loc, OPT_Wtype_limits,
3345 : : "comparison of unsigned expression in %<< 0%> "
3346 : : "is always false");
3347 : 310867 : value = truthvalue_false_node;
3348 : 310867 : break;
3349 : :
3350 : : default:
3351 : : break;
3352 : : }
3353 : :
3354 : 323124 : if (value != NULL_TREE)
3355 : : {
3356 : : /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3357 : 323124 : if (TREE_SIDE_EFFECTS (primop0))
3358 : 39 : return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3359 : 39 : primop0, value);
3360 : : return value;
3361 : : }
3362 : : }
3363 : : }
3364 : :
3365 : 26506770 : *op0_ptr = convert (type, primop0);
3366 : 26506770 : *op1_ptr = convert (type, primop1);
3367 : :
3368 : 26506770 : *restype_ptr = truthvalue_type_node;
3369 : :
3370 : 26506770 : return NULL_TREE;
3371 : : }
3372 : :
3373 : : /* Return a tree for the sum or difference (RESULTCODE says which)
3374 : : of pointer PTROP and integer INTOP. */
3375 : :
3376 : : tree
3377 : 5618664 : pointer_int_sum (location_t loc, enum tree_code resultcode,
3378 : : tree ptrop, tree intop, bool complain)
3379 : : {
3380 : 5618664 : tree size_exp, ret;
3381 : :
3382 : : /* The result is a pointer of the same type that is being added. */
3383 : 5618664 : tree result_type = TREE_TYPE (ptrop);
3384 : :
3385 : 5618664 : if (VOID_TYPE_P (TREE_TYPE (result_type)))
3386 : : {
3387 : 928 : if (complain && warn_pointer_arith)
3388 : 61 : pedwarn (loc, OPT_Wpointer_arith,
3389 : : "pointer of type %<void *%> used in arithmetic");
3390 : : else if (!complain)
3391 : 0 : return error_mark_node;
3392 : 928 : size_exp = integer_one_node;
3393 : : }
3394 : 5617736 : else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3395 : : {
3396 : 118 : if (complain && warn_pointer_arith)
3397 : 49 : pedwarn (loc, OPT_Wpointer_arith,
3398 : : "pointer to a function used in arithmetic");
3399 : : else if (!complain)
3400 : 0 : return error_mark_node;
3401 : 118 : size_exp = integer_one_node;
3402 : : }
3403 : 5617618 : else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
3404 : 5617618 : TREE_TYPE (result_type)))
3405 : 0 : size_exp = integer_one_node;
3406 : : else
3407 : : {
3408 : 5617618 : if (!complain && !COMPLETE_TYPE_P (TREE_TYPE (result_type)))
3409 : 108 : return error_mark_node;
3410 : 5617510 : size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type));
3411 : : /* Wrap the pointer expression in a SAVE_EXPR to make sure it
3412 : : is evaluated first when the size expression may depend
3413 : : on it for VM types. */
3414 : 5617510 : if (TREE_SIDE_EFFECTS (size_exp)
3415 : 1935 : && TREE_SIDE_EFFECTS (ptrop)
3416 : 5617529 : && variably_modified_type_p (TREE_TYPE (ptrop), NULL))
3417 : : {
3418 : 19 : ptrop = save_expr (ptrop);
3419 : 19 : size_exp = build2 (COMPOUND_EXPR, TREE_TYPE (intop), ptrop, size_exp);
3420 : : }
3421 : : }
3422 : :
3423 : : /* We are manipulating pointer values, so we don't need to warn
3424 : : about relying on undefined signed overflow. We disable the
3425 : : warning here because we use integer types so fold won't know that
3426 : : they are really pointers. */
3427 : 5618556 : fold_defer_overflow_warnings ();
3428 : :
3429 : : /* If what we are about to multiply by the size of the elements
3430 : : contains a constant term, apply distributive law
3431 : : and multiply that constant term separately.
3432 : : This helps produce common subexpressions. */
3433 : 5618556 : if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3434 : 176321 : && !TREE_CONSTANT (intop)
3435 : 176239 : && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3436 : 141340 : && TREE_CONSTANT (size_exp)
3437 : : /* If the constant comes from pointer subtraction,
3438 : : skip this optimization--it would cause an error. */
3439 : 141339 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3440 : : /* If the constant is unsigned, and smaller than the pointer size,
3441 : : then we must skip this optimization. This is because it could cause
3442 : : an overflow error if the constant is negative but INTOP is not. */
3443 : 282678 : && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (intop))
3444 : 121241 : || (TYPE_PRECISION (TREE_TYPE (intop))
3445 : 121241 : == TYPE_PRECISION (TREE_TYPE (ptrop))))
3446 : 5795686 : && TYPE_PRECISION (TREE_TYPE (intop)) <= TYPE_PRECISION (sizetype))
3447 : : {
3448 : 35791 : tree intop0 = TREE_OPERAND (intop, 0);
3449 : 35791 : tree intop1 = TREE_OPERAND (intop, 1);
3450 : 35791 : if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3451 : 35791 : || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3452 : : {
3453 : 20098 : tree optype = c_common_type_for_size (TYPE_PRECISION (sizetype),
3454 : 20098 : TYPE_UNSIGNED (sizetype));
3455 : 20098 : intop0 = convert (optype, intop0);
3456 : 20098 : intop1 = convert (optype, intop1);
3457 : : }
3458 : 35791 : tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop0), intop0,
3459 : 35791 : convert (TREE_TYPE (intop0), size_exp));
3460 : 35791 : intop0 = convert (sizetype, t);
3461 : 35791 : if (TREE_OVERFLOW_P (intop0) && !TREE_OVERFLOW (t))
3462 : 0 : intop0 = wide_int_to_tree (TREE_TYPE (intop0), wi::to_wide (intop0));
3463 : 35791 : t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop1), intop1,
3464 : 35791 : convert (TREE_TYPE (intop1), size_exp));
3465 : 35791 : intop1 = convert (sizetype, t);
3466 : 35791 : if (TREE_OVERFLOW_P (intop1) && !TREE_OVERFLOW (t))
3467 : 0 : intop1 = wide_int_to_tree (TREE_TYPE (intop1), wi::to_wide (intop1));
3468 : 35791 : intop = build_binary_op (EXPR_LOCATION (intop), TREE_CODE (intop),
3469 : : intop0, intop1, true);
3470 : :
3471 : : /* Create the sum or difference. */
3472 : 35791 : if (resultcode == MINUS_EXPR)
3473 : 51 : intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3474 : :
3475 : 35791 : ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
3476 : :
3477 : 35791 : fold_undefer_and_ignore_overflow_warnings ();
3478 : :
3479 : 35791 : 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 : 5582765 : if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3485 : 5582765 : || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3486 : 3890386 : intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
3487 : 3890386 : 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 : 5582765 : {
3494 : 5582765 : tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
3495 : 5582765 : convert (TREE_TYPE (intop), size_exp));
3496 : 5582765 : intop = convert (sizetype, t);
3497 : 5582765 : 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 : 5582765 : if (resultcode == MINUS_EXPR)
3503 : 214880 : intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3504 : :
3505 : 5582765 : ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
3506 : :
3507 : 5582765 : fold_undefer_and_ignore_overflow_warnings ();
3508 : :
3509 : 5582765 : return ret;
3510 : : }
3511 : :
3512 : : /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3513 : : and if NON_CONST is known not to be permitted in an evaluated part
3514 : : of a constant expression. */
3515 : :
3516 : : tree
3517 : 4473539 : c_wrap_maybe_const (tree expr, bool non_const)
3518 : : {
3519 : 4473539 : location_t loc = EXPR_LOCATION (expr);
3520 : :
3521 : : /* This should never be called for C++. */
3522 : 4473539 : if (c_dialect_cxx ())
3523 : 0 : gcc_unreachable ();
3524 : :
3525 : : /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
3526 : 4474285 : STRIP_TYPE_NOPS (expr);
3527 : 4473539 : expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
3528 : 4473539 : C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
3529 : 4473539 : protected_set_expr_location (expr, loc);
3530 : :
3531 : 4473539 : return expr;
3532 : : }
3533 : :
3534 : : /* Return whether EXPR is a declaration whose address can never be NULL.
3535 : : The address of the first struct member could be NULL only if it were
3536 : : accessed through a NULL pointer, and such an access would be invalid.
3537 : : The address of a weak symbol may be null unless it has a definition. */
3538 : :
3539 : : bool
3540 : 1410 : decl_with_nonnull_addr_p (const_tree expr)
3541 : : {
3542 : 1410 : if (!DECL_P (expr))
3543 : : return false;
3544 : :
3545 : 1156 : if (TREE_CODE (expr) == FIELD_DECL
3546 : 1079 : || TREE_CODE (expr) == PARM_DECL
3547 : 1045 : || TREE_CODE (expr) == LABEL_DECL)
3548 : : return true;
3549 : :
3550 : 1020 : if (!VAR_OR_FUNCTION_DECL_P (expr))
3551 : : return false;
3552 : :
3553 : 1020 : if (!DECL_WEAK (expr))
3554 : : /* Ordinary (non-weak) symbols have nonnull addresses. */
3555 : : return true;
3556 : :
3557 : 529 : if (DECL_INITIAL (expr) && DECL_INITIAL (expr) != error_mark_node)
3558 : : /* Initialized weak symbols have nonnull addresses. */
3559 : : return true;
3560 : :
3561 : 476 : if (DECL_EXTERNAL (expr) || !TREE_STATIC (expr))
3562 : : /* Uninitialized extern weak symbols and weak symbols with no
3563 : : allocated storage might have a null address. */
3564 : : return false;
3565 : :
3566 : 43 : tree attribs = DECL_ATTRIBUTES (expr);
3567 : 43 : if (lookup_attribute ("weakref", attribs))
3568 : : /* Weakref symbols might have a null address unless their referent
3569 : : is known not to. Don't bother following weakref targets here. */
3570 : : return false;
3571 : :
3572 : : return true;
3573 : : }
3574 : :
3575 : : /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3576 : : or for an `if' or `while' statement or ?..: exp. It should already
3577 : : have been validated to be of suitable type; otherwise, a bad
3578 : : diagnostic may result.
3579 : :
3580 : : The EXPR is located at LOCATION.
3581 : :
3582 : : This preparation consists of taking the ordinary
3583 : : representation of an expression expr and producing a valid tree
3584 : : boolean expression describing whether expr is nonzero. We could
3585 : : simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3586 : : but we optimize comparisons, &&, ||, and !.
3587 : :
3588 : : The resulting type should always be `truthvalue_type_node'. */
3589 : :
3590 : : tree
3591 : 10698256 : c_common_truthvalue_conversion (location_t location, tree expr)
3592 : : {
3593 : 11512736 : STRIP_ANY_LOCATION_WRAPPER (expr);
3594 : 11512736 : switch (TREE_CODE (expr))
3595 : : {
3596 : 2494908 : case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
3597 : 2494908 : case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3598 : 2494908 : case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
3599 : 2494908 : case ORDERED_EXPR: case UNORDERED_EXPR:
3600 : 2494908 : if (TREE_TYPE (expr) == truthvalue_type_node)
3601 : : return expr;
3602 : 405960 : expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3603 : 405960 : TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
3604 : 405960 : goto ret;
3605 : :
3606 : 596890 : case TRUTH_ANDIF_EXPR:
3607 : 596890 : case TRUTH_ORIF_EXPR:
3608 : 596890 : case TRUTH_AND_EXPR:
3609 : 596890 : case TRUTH_OR_EXPR:
3610 : 596890 : case TRUTH_XOR_EXPR:
3611 : 596890 : if (TREE_TYPE (expr) == truthvalue_type_node)
3612 : : return expr;
3613 : 74 : expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3614 : : c_common_truthvalue_conversion (location,
3615 : 37 : TREE_OPERAND (expr, 0)),
3616 : : c_common_truthvalue_conversion (location,
3617 : 37 : TREE_OPERAND (expr, 1)));
3618 : 37 : goto ret;
3619 : :
3620 : 329507 : case TRUTH_NOT_EXPR:
3621 : 329507 : if (TREE_TYPE (expr) == truthvalue_type_node)
3622 : : return expr;
3623 : 326534 : expr = build1 (TREE_CODE (expr), truthvalue_type_node,
3624 : : c_common_truthvalue_conversion (location,
3625 : 326534 : TREE_OPERAND (expr, 0)));
3626 : 326534 : goto ret;
3627 : :
3628 : : case ERROR_MARK:
3629 : : return expr;
3630 : :
3631 : 2650568 : case INTEGER_CST:
3632 : 2650568 : if (TREE_CODE (TREE_TYPE (expr)) == ENUMERAL_TYPE
3633 : 774850 : && !integer_zerop (expr)
3634 : 3136567 : && !integer_onep (expr))
3635 : 913 : warning_at (location, OPT_Wint_in_bool_context,
3636 : : "enum constant in boolean context");
3637 : 2650568 : return integer_zerop (expr) ? truthvalue_false_node
3638 : 2650568 : : truthvalue_true_node;
3639 : :
3640 : 1288 : case REAL_CST:
3641 : 1288 : return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
3642 : 1288 : ? truthvalue_true_node
3643 : 1288 : : truthvalue_false_node;
3644 : :
3645 : 0 : case FIXED_CST:
3646 : 0 : return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
3647 : 0 : &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
3648 : 0 : ? truthvalue_true_node
3649 : 0 : : truthvalue_false_node;
3650 : :
3651 : 0 : case FUNCTION_DECL:
3652 : 0 : expr = build_unary_op (location, ADDR_EXPR, expr, false);
3653 : : /* Fall through. */
3654 : :
3655 : 579 : case ADDR_EXPR:
3656 : 579 : {
3657 : 579 : tree inner = TREE_OPERAND (expr, 0);
3658 : 579 : if (decl_with_nonnull_addr_p (inner)
3659 : : /* Check both EXPR and INNER for suppression. */
3660 : 127 : && !warning_suppressed_p (expr, OPT_Waddress)
3661 : 692 : && !warning_suppressed_p (inner, OPT_Waddress))
3662 : : {
3663 : : /* Common Ada programmer's mistake. */
3664 : 107 : warning_at (location,
3665 : 107 : OPT_Waddress,
3666 : : "the address of %qD will always evaluate as %<true%>",
3667 : : inner);
3668 : 107 : suppress_warning (inner, OPT_Waddress);
3669 : 107 : return truthvalue_true_node;
3670 : : }
3671 : : break;
3672 : : }
3673 : :
3674 : 578 : case COMPLEX_EXPR:
3675 : 1734 : expr = build_binary_op (EXPR_LOCATION (expr),
3676 : 578 : (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3677 : : ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3678 : : c_common_truthvalue_conversion (location,
3679 : 578 : TREE_OPERAND (expr, 0)),
3680 : : c_common_truthvalue_conversion (location,
3681 : 578 : TREE_OPERAND (expr, 1)),
3682 : : false);
3683 : 578 : goto ret;
3684 : :
3685 : 74 : case NEGATE_EXPR:
3686 : 74 : case ABS_EXPR:
3687 : 74 : case ABSU_EXPR:
3688 : 74 : case FLOAT_EXPR:
3689 : 74 : case EXCESS_PRECISION_EXPR:
3690 : : /* These don't change whether an object is nonzero or zero. */
3691 : 74 : return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
3692 : :
3693 : 0 : case LROTATE_EXPR:
3694 : 0 : case RROTATE_EXPR:
3695 : : /* These don't change whether an object is zero or nonzero, but
3696 : : we can't ignore them if their second arg has side-effects. */
3697 : 0 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
3698 : : {
3699 : 0 : expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
3700 : 0 : TREE_OPERAND (expr, 1),
3701 : : c_common_truthvalue_conversion
3702 : 0 : (location, TREE_OPERAND (expr, 0)));
3703 : 0 : goto ret;
3704 : : }
3705 : : else
3706 : 0 : return c_common_truthvalue_conversion (location,
3707 : 0 : TREE_OPERAND (expr, 0));
3708 : :
3709 : 502 : case MULT_EXPR:
3710 : 502 : warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3711 : : "%<*%> in boolean context, suggest %<&&%> instead");
3712 : 502 : break;
3713 : :
3714 : 166 : case LSHIFT_EXPR:
3715 : : /* We will only warn on signed shifts here, because the majority of
3716 : : false positive warnings happen in code where unsigned arithmetic
3717 : : was used in anticipation of a possible overflow.
3718 : : Furthermore, if we see an unsigned type here we know that the
3719 : : result of the shift is not subject to integer promotion rules. */
3720 : 166 : if ((TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
3721 : 1 : || TREE_CODE (TREE_TYPE (expr)) == BITINT_TYPE)
3722 : 167 : && !TYPE_UNSIGNED (TREE_TYPE (expr)))
3723 : 150 : warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3724 : : "%<<<%> in boolean context, did you mean %<<%>?");
3725 : : break;
3726 : :
3727 : 46771 : case COND_EXPR:
3728 : 46771 : if (warn_int_in_bool_context
3729 : 48279 : && !from_macro_definition_at (EXPR_LOCATION (expr)))
3730 : : {
3731 : 741 : tree val1 = fold_for_warn (TREE_OPERAND (expr, 1));
3732 : 741 : tree val2 = fold_for_warn (TREE_OPERAND (expr, 2));
3733 : 741 : if (TREE_CODE (val1) == INTEGER_CST
3734 : 78 : && TREE_CODE (val2) == INTEGER_CST
3735 : 78 : && !integer_zerop (val1)
3736 : 70 : && !integer_zerop (val2)
3737 : 757 : && (!integer_onep (val1)
3738 : 12 : || !integer_onep (val2)))
3739 : 12 : warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3740 : : "%<?:%> using integer constants in boolean context, "
3741 : : "the expression will always evaluate to %<true%>");
3742 : 729 : else if ((TREE_CODE (val1) == INTEGER_CST
3743 : 66 : && !integer_zerop (val1)
3744 : 58 : && !integer_onep (val1))
3745 : 795 : || (TREE_CODE (val2) == INTEGER_CST
3746 : 332 : && !integer_zerop (val2)
3747 : 274 : && !integer_onep (val2)))
3748 : 4 : warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3749 : : "%<?:%> using integer constants in boolean context");
3750 : : }
3751 : : /* Distribute the conversion into the arms of a COND_EXPR. */
3752 : 46771 : if (c_dialect_cxx ())
3753 : : /* Avoid premature folding. */
3754 : : break;
3755 : : else
3756 : : {
3757 : 21555 : int w = warn_int_in_bool_context;
3758 : 21555 : warn_int_in_bool_context = 0;
3759 : : /* Folding will happen later for C. */
3760 : 64665 : expr = build3 (COND_EXPR, truthvalue_type_node,
3761 : 21555 : TREE_OPERAND (expr, 0),
3762 : : c_common_truthvalue_conversion (location,
3763 : 21555 : TREE_OPERAND (expr, 1)),
3764 : : c_common_truthvalue_conversion (location,
3765 : 21555 : TREE_OPERAND (expr, 2)));
3766 : 21555 : warn_int_in_bool_context = w;
3767 : 21555 : goto ret;
3768 : : }
3769 : :
3770 : 593746 : CASE_CONVERT:
3771 : 593746 : {
3772 : 593746 : tree totype = TREE_TYPE (expr);
3773 : 593746 : tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
3774 : :
3775 : 593746 : if (POINTER_TYPE_P (totype)
3776 : 2016 : && !c_inhibit_evaluation_warnings
3777 : 1848 : && TREE_CODE (fromtype) == REFERENCE_TYPE)
3778 : : {
3779 : 0 : tree inner = expr;
3780 : 0 : STRIP_NOPS (inner);
3781 : :
3782 : 0 : if (DECL_P (inner))
3783 : 0 : warning_at (location,
3784 : 0 : OPT_Waddress,
3785 : : "the compiler can assume that the address of "
3786 : : "%qD will always evaluate to %<true%>",
3787 : : inner);
3788 : : }
3789 : :
3790 : : /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3791 : : since that affects how `default_conversion' will behave. */
3792 : 593746 : if (TREE_CODE (totype) == REFERENCE_TYPE
3793 : 593746 : || TREE_CODE (fromtype) == REFERENCE_TYPE)
3794 : : break;
3795 : : /* Don't strip a conversion from C++0x scoped enum, since they
3796 : : don't implicitly convert to other types. */
3797 : 593746 : if (TREE_CODE (fromtype) == ENUMERAL_TYPE
3798 : 593746 : && ENUM_IS_SCOPED (fromtype))
3799 : : break;
3800 : : /* If this isn't narrowing the argument, we can ignore it. */
3801 : 593707 : if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
3802 : : {
3803 : 576391 : tree op0 = TREE_OPERAND (expr, 0);
3804 : 576391 : if ((TREE_CODE (fromtype) == POINTER_TYPE
3805 : 1932 : && (TREE_CODE (totype) == INTEGER_TYPE
3806 : 1932 : || TREE_CODE (totype) == BITINT_TYPE))
3807 : 578291 : || warning_suppressed_p (expr, OPT_Waddress))
3808 : : /* Suppress -Waddress for casts to intptr_t, propagating
3809 : : any suppression from the enclosing expression to its
3810 : : operand. */
3811 : 44 : suppress_warning (op0, OPT_Waddress);
3812 : 576391 : return c_common_truthvalue_conversion (location, op0);
3813 : : }
3814 : : }
3815 : : break;
3816 : :
3817 : 8842 : case MODIFY_EXPR:
3818 : 8842 : if (!warning_suppressed_p (expr, OPT_Wparentheses)
3819 : 269 : && warn_parentheses
3820 : 9111 : && warning_at (location, OPT_Wparentheses,
3821 : : "suggest parentheses around assignment used as "
3822 : : "truth value"))
3823 : 14 : suppress_warning (expr, OPT_Wparentheses);
3824 : : break;
3825 : :
3826 : 238032 : case CONST_DECL:
3827 : 238032 : {
3828 : 238032 : tree folded_expr = fold_for_warn (expr);
3829 : 238032 : if (folded_expr != expr)
3830 : : return c_common_truthvalue_conversion (location, folded_expr);
3831 : : }
3832 : : break;
3833 : :
3834 : : default:
3835 : : break;
3836 : : }
3837 : :
3838 : 4602632 : if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
3839 : : {
3840 : 405 : tree t = save_expr (expr);
3841 : 405 : expr = (build_binary_op
3842 : 810 : (EXPR_LOCATION (expr),
3843 : 405 : (TREE_SIDE_EFFECTS (expr)
3844 : : ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3845 : : c_common_truthvalue_conversion
3846 : : (location,
3847 : : build_unary_op (location, REALPART_EXPR, t, false)),
3848 : : c_common_truthvalue_conversion
3849 : : (location,
3850 : : build_unary_op (location, IMAGPART_EXPR, t, false)),
3851 : : false));
3852 : 405 : goto ret;
3853 : : }
3854 : :
3855 : 4602227 : if (FIXED_POINT_TYPE_P (TREE_TYPE (expr)))
3856 : : {
3857 : 0 : tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
3858 : 0 : FCONST0 (TYPE_MODE
3859 : : (TREE_TYPE (expr))));
3860 : 0 : return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, true);
3861 : : }
3862 : : else
3863 : 4602227 : return build_binary_op (location, NE_EXPR, expr, integer_zero_node, true);
3864 : :
3865 : 755069 : ret:
3866 : 755069 : protected_set_expr_location (expr, location);
3867 : 755069 : return expr;
3868 : : }
3869 : :
3870 : : static void def_builtin_1 (enum built_in_function fncode,
3871 : : const char *name,
3872 : : enum built_in_class fnclass,
3873 : : tree fntype, tree libtype,
3874 : : bool both_p, bool fallback_p, bool nonansi_p,
3875 : : tree fnattrs, bool implicit_p);
3876 : :
3877 : :
3878 : : /* Apply the TYPE_QUALS to the new DECL. */
3879 : :
3880 : : void
3881 : 969135848 : c_apply_type_quals_to_decl (int type_quals, tree decl)
3882 : : {
3883 : 969135848 : tree type = TREE_TYPE (decl);
3884 : :
3885 : 969135848 : if (type == error_mark_node)
3886 : : return;
3887 : :
3888 : 969135619 : if ((type_quals & TYPE_QUAL_CONST)
3889 : 627235164 : || (type && TREE_CODE (type) == REFERENCE_TYPE))
3890 : : /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
3891 : : constructor can produce constant init, so rely on cp_finish_decl to
3892 : : clear TREE_READONLY if the variable has non-constant init. */
3893 : 435009489 : TREE_READONLY (decl) = 1;
3894 : 969135619 : if (type_quals & TYPE_QUAL_VOLATILE)
3895 : : {
3896 : 135074 : TREE_SIDE_EFFECTS (decl) = 1;
3897 : 135074 : TREE_THIS_VOLATILE (decl) = 1;
3898 : : }
3899 : 969135619 : if (type_quals & TYPE_QUAL_RESTRICT)
3900 : : {
3901 : 10899234 : while (type && TREE_CODE (type) == ARRAY_TYPE)
3902 : : /* Allow 'restrict' on arrays of pointers.
3903 : : FIXME currently we just ignore it. */
3904 : 7 : type = TREE_TYPE (type);
3905 : 10899227 : if (!type
3906 : 10899227 : || !POINTER_TYPE_P (type)
3907 : 21798451 : || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
3908 : 4 : error ("invalid use of %<restrict%>");
3909 : : }
3910 : : }
3911 : :
3912 : : /* Return the typed-based alias set for T, which may be an expression
3913 : : or a type. Return -1 if we don't do anything special. */
3914 : :
3915 : : alias_set_type
3916 : 658051403 : c_common_get_alias_set (tree t)
3917 : : {
3918 : : /* For VLAs, use the alias set of the element type rather than the
3919 : : default of alias set 0 for types compared structurally. */
3920 : 658051403 : if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
3921 : : {
3922 : 887495 : if (TREE_CODE (t) == ARRAY_TYPE)
3923 : 451762 : return get_alias_set (TREE_TYPE (t));
3924 : : return -1;
3925 : : }
3926 : :
3927 : : /* That's all the expressions we handle specially. */
3928 : 657163908 : if (!TYPE_P (t))
3929 : : return -1;
3930 : :
3931 : : /* Unlike char, char8_t doesn't alias in C++. (In C, char8_t is not
3932 : : a distinct type.) */
3933 : 110861946 : if (flag_char8_t && t == char8_type_node && c_dialect_cxx ())
3934 : : return -1;
3935 : :
3936 : : /* The C standard guarantees that any object may be accessed via an
3937 : : lvalue that has narrow character type. */
3938 : 110861871 : if (t == char_type_node
3939 : 73206593 : || t == signed_char_type_node
3940 : 69081038 : || t == unsigned_char_type_node)
3941 : : return 0;
3942 : :
3943 : : /* The C standard specifically allows aliasing between signed and
3944 : : unsigned variants of the same type. We treat the signed
3945 : : variant as canonical. */
3946 : 57931026 : if ((TREE_CODE (t) == INTEGER_TYPE || TREE_CODE (t) == BITINT_TYPE)
3947 : 57931026 : && TYPE_UNSIGNED (t))
3948 : : {
3949 : 56308858 : tree t1 = c_common_signed_type (t);
3950 : :
3951 : : /* t1 == t can happen for boolean nodes which are always unsigned. */
3952 : 56308858 : if (t1 != t)
3953 : 56308858 : return get_alias_set (t1);
3954 : : }
3955 : :
3956 : : return -1;
3957 : : }
3958 : :
3959 : : /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
3960 : : the IS_SIZEOF parameter indicates which operator is being applied.
3961 : : The COMPLAIN flag controls whether we should diagnose possibly
3962 : : ill-formed constructs or not. LOC is the location of the SIZEOF or
3963 : : TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
3964 : : a type in any context should be returned, rather than the normal
3965 : : alignment for that type. */
3966 : :
3967 : : tree
3968 : 19772208 : c_sizeof_or_alignof_type (location_t loc,
3969 : : tree type, bool is_sizeof, bool min_alignof,
3970 : : int complain)
3971 : : {
3972 : 19772208 : const char *op_name;
3973 : 19772208 : tree value = NULL;
3974 : 19772208 : enum tree_code type_code = TREE_CODE (type);
3975 : :
3976 : 19772208 : op_name = is_sizeof ? "sizeof" : "__alignof__";
3977 : :
3978 : 19772208 : if (type_code == FUNCTION_TYPE)
3979 : : {
3980 : 64 : if (is_sizeof)
3981 : : {
3982 : 58 : if (complain && warn_pointer_arith)
3983 : 28 : pedwarn (loc, OPT_Wpointer_arith,
3984 : : "invalid application of %<sizeof%> to a function type");
3985 : : else if (!complain)
3986 : 20 : return error_mark_node;
3987 : 38 : value = size_one_node;
3988 : : }
3989 : : else
3990 : : {
3991 : 6 : if (complain)
3992 : : {
3993 : 6 : if (c_dialect_cxx ())
3994 : 4 : pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
3995 : : "%<alignof%> applied to a function type");
3996 : : else
3997 : 2 : pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
3998 : : "%<_Alignof%> applied to a function type");
3999 : : }
4000 : 6 : value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4001 : : }
4002 : : }
4003 : 19772144 : else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4004 : : {
4005 : 88 : if (type_code == VOID_TYPE
4006 : 88 : && complain && warn_pointer_arith)
4007 : 19 : pedwarn (loc, OPT_Wpointer_arith,
4008 : : "invalid application of %qs to a void type", op_name);
4009 : 69 : else if (!complain)
4010 : 30 : return error_mark_node;
4011 : 58 : value = size_one_node;
4012 : : }
4013 : 19772056 : else if (!COMPLETE_TYPE_P (type)
4014 : 19772056 : && ((!c_dialect_cxx () && !flag_isoc2y)
4015 : : || is_sizeof
4016 : 144 : || type_code != ARRAY_TYPE))
4017 : : {
4018 : 271 : if (complain)
4019 : 194 : error_at (loc, "invalid application of %qs to incomplete type %qT",
4020 : : op_name, type);
4021 : 271 : return error_mark_node;
4022 : : }
4023 : 19080744 : else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4024 : 20229913 : && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4025 : : {
4026 : 0 : if (complain)
4027 : 0 : error_at (loc, "invalid application of %qs to array type %qT of "
4028 : : "incomplete element type", op_name, type);
4029 : 0 : return error_mark_node;
4030 : : }
4031 : 20742468 : else if (!verify_type_context (loc, is_sizeof ? TCTX_SIZEOF : TCTX_ALIGNOF,
4032 : : type, !complain))
4033 : : {
4034 : 0 : if (!complain)
4035 : 0 : return error_mark_node;
4036 : 0 : value = size_one_node;
4037 : : }
4038 : : else
4039 : : {
4040 : 19771785 : if (is_sizeof)
4041 : : /* Convert in case a char is more than one unit. */
4042 : 18801102 : value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4043 : 18801102 : size_int (TYPE_PRECISION (char_type_node)
4044 : : / BITS_PER_UNIT));
4045 : 970683 : else if (min_alignof)
4046 : 761323 : value = size_int (min_align_of_type (type));
4047 : : else
4048 : 209360 : value = size_int (TYPE_ALIGN_UNIT (type));
4049 : : }
4050 : :
4051 : : /* VALUE will have the middle-end integer type sizetype.
4052 : : However, we should really return a value of type `size_t',
4053 : : which is just a typedef for an ordinary integer type. */
4054 : 19771887 : value = fold_convert_loc (loc, size_type_node, value);
4055 : :
4056 : 19771887 : return value;
4057 : : }
4058 : :
4059 : : /* Implement the __alignof keyword: Return the minimum required
4060 : : alignment of EXPR, measured in bytes. For VAR_DECLs,
4061 : : FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4062 : : from an "aligned" __attribute__ specification). LOC is the
4063 : : location of the ALIGNOF operator. */
4064 : :
4065 : : tree
4066 : 40793 : c_alignof_expr (location_t loc, tree expr)
4067 : : {
4068 : 40793 : tree t;
4069 : :
4070 : 40793 : if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (expr)))
4071 : 0 : t = size_one_node;
4072 : :
4073 : 40793 : else if (VAR_OR_FUNCTION_DECL_P (expr))
4074 : 7245 : t = size_int (DECL_ALIGN_UNIT (expr));
4075 : :
4076 : 33548 : else if (TREE_CODE (expr) == COMPONENT_REF
4077 : 33548 : && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4078 : : {
4079 : 1 : error_at (loc, "%<__alignof%> applied to a bit-field");
4080 : 1 : t = size_one_node;
4081 : : }
4082 : 33547 : else if (TREE_CODE (expr) == COMPONENT_REF
4083 : 33547 : && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
4084 : 17569 : t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
4085 : :
4086 : 15978 : else if (INDIRECT_REF_P (expr))
4087 : : {
4088 : 57 : tree t = TREE_OPERAND (expr, 0);
4089 : 57 : tree best = t;
4090 : 57 : int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4091 : :
4092 : 57 : while (CONVERT_EXPR_P (t)
4093 : 57 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
4094 : : {
4095 : 0 : int thisalign;
4096 : :
4097 : 0 : t = TREE_OPERAND (t, 0);
4098 : 0 : thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4099 : 0 : if (thisalign > bestalign)
4100 : 0 : best = t, bestalign = thisalign;
4101 : : }
4102 : 57 : return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
4103 : : }
4104 : : else
4105 : 15921 : return c_alignof (loc, TREE_TYPE (expr));
4106 : :
4107 : 24815 : return fold_convert_loc (loc, size_type_node, t);
4108 : : }
4109 : :
4110 : : /* Implement the _Countof keyword:
4111 : : Return the number of elements of an array. */
4112 : :
4113 : : tree
4114 : 71 : c_countof_type (location_t loc, tree type)
4115 : : {
4116 : 71 : enum tree_code type_code;
4117 : 71 : tree value;
4118 : :
4119 : 71 : type_code = TREE_CODE (type);
4120 : 71 : if (type_code != ARRAY_TYPE)
4121 : : {
4122 : 11 : error_at (loc, "invalid application of %<_Countof%> to type %qT", type);
4123 : 11 : return error_mark_node;
4124 : : }
4125 : 60 : if (!COMPLETE_TYPE_P (type))
4126 : : {
4127 : 2 : error_at (loc,
4128 : : "invalid application of %<_Countof%> to incomplete type %qT",
4129 : : type);
4130 : 2 : return error_mark_node;
4131 : : }
4132 : :
4133 : 58 : value = array_type_nelts_top (type);
4134 : : /* VALUE will have the middle-end integer type sizetype.
4135 : : However, we should really return a value of type `size_t',
4136 : : which is just a typedef for an ordinary integer type. */
4137 : 58 : value = fold_convert_loc (loc, size_type_node, value);
4138 : 58 : return value;
4139 : : }
4140 : :
4141 : : /* Handle C and C++ default attributes. */
4142 : :
4143 : : enum built_in_attribute
4144 : : {
4145 : : #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4146 : : #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
4147 : : #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
4148 : : #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4149 : : #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
4150 : : #include "builtin-attrs.def"
4151 : : #undef DEF_ATTR_NULL_TREE
4152 : : #undef DEF_ATTR_INT
4153 : : #undef DEF_ATTR_STRING
4154 : : #undef DEF_ATTR_IDENT
4155 : : #undef DEF_ATTR_TREE_LIST
4156 : : ATTR_LAST
4157 : : };
4158 : :
4159 : : static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4160 : :
4161 : : static void c_init_attributes (void);
4162 : :
4163 : : enum c_builtin_type
4164 : : {
4165 : : #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
4166 : : #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
4167 : : #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
4168 : : #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
4169 : : #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4170 : : #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4171 : : #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
4172 : : #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4173 : : ARG6) NAME,
4174 : : #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4175 : : ARG6, ARG7) NAME,
4176 : : #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4177 : : ARG6, ARG7, ARG8) NAME,
4178 : : #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4179 : : ARG6, ARG7, ARG8, ARG9) NAME,
4180 : : #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4181 : : ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
4182 : : #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4183 : : ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
4184 : : #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
4185 : : #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
4186 : : #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
4187 : : #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4188 : : #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4189 : : #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4190 : : NAME,
4191 : : #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4192 : : ARG6) NAME,
4193 : : #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4194 : : ARG6, ARG7) NAME,
4195 : : #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
4196 : : #include "builtin-types.def"
4197 : : #undef DEF_PRIMITIVE_TYPE
4198 : : #undef DEF_FUNCTION_TYPE_0
4199 : : #undef DEF_FUNCTION_TYPE_1
4200 : : #undef DEF_FUNCTION_TYPE_2
4201 : : #undef DEF_FUNCTION_TYPE_3
4202 : : #undef DEF_FUNCTION_TYPE_4
4203 : : #undef DEF_FUNCTION_TYPE_5
4204 : : #undef DEF_FUNCTION_TYPE_6
4205 : : #undef DEF_FUNCTION_TYPE_7
4206 : : #undef DEF_FUNCTION_TYPE_8
4207 : : #undef DEF_FUNCTION_TYPE_9
4208 : : #undef DEF_FUNCTION_TYPE_10
4209 : : #undef DEF_FUNCTION_TYPE_11
4210 : : #undef DEF_FUNCTION_TYPE_VAR_0
4211 : : #undef DEF_FUNCTION_TYPE_VAR_1
4212 : : #undef DEF_FUNCTION_TYPE_VAR_2
4213 : : #undef DEF_FUNCTION_TYPE_VAR_3
4214 : : #undef DEF_FUNCTION_TYPE_VAR_4
4215 : : #undef DEF_FUNCTION_TYPE_VAR_5
4216 : : #undef DEF_FUNCTION_TYPE_VAR_6
4217 : : #undef DEF_FUNCTION_TYPE_VAR_7
4218 : : #undef DEF_POINTER_TYPE
4219 : : BT_LAST
4220 : : };
4221 : :
4222 : : typedef enum c_builtin_type builtin_type;
4223 : :
4224 : : /* A temporary array for c_common_nodes_and_builtins. Used in
4225 : : communication with def_fn_type. */
4226 : : static tree builtin_types[(int) BT_LAST + 1];
4227 : :
4228 : : /* A helper function for c_common_nodes_and_builtins. Build function type
4229 : : for DEF with return type RET and N arguments. If VAR is true, then the
4230 : : function should be variadic after those N arguments, or, if N is zero,
4231 : : unprototyped.
4232 : :
4233 : : Takes special care not to ICE if any of the types involved are
4234 : : error_mark_node, which indicates that said type is not in fact available
4235 : : (see builtin_type_for_size). In which case the function type as a whole
4236 : : should be error_mark_node. */
4237 : :
4238 : : static void
4239 : 105788592 : def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
4240 : : {
4241 : 105788592 : tree t;
4242 : 105788592 : tree *args = XALLOCAVEC (tree, n);
4243 : 105788592 : va_list list;
4244 : 105788592 : int i;
4245 : :
4246 : 105788592 : va_start (list, n);
4247 : 335111022 : for (i = 0; i < n; ++i)
4248 : : {
4249 : 232309044 : builtin_type a = (builtin_type) va_arg (list, int);
4250 : 232309044 : t = builtin_types[a];
4251 : 232309044 : if (t == error_mark_node)
4252 : 2986614 : goto egress;
4253 : 229322430 : args[i] = t;
4254 : : }
4255 : :
4256 : 102801978 : t = builtin_types[ret];
4257 : 102801978 : if (t == error_mark_node)
4258 : 425134 : goto egress;
4259 : 102376844 : if (var)
4260 : 6291602 : if (n == 0)
4261 : 839592 : t = build_function_type (t, NULL_TREE);
4262 : : else
4263 : 5452010 : t = build_varargs_function_type_array (t, n, args);
4264 : : else
4265 : 96085242 : t = build_function_type_array (t, n, args);
4266 : :
4267 : 105788592 : egress:
4268 : 105788592 : builtin_types[def] = t;
4269 : 105788592 : va_end (list);
4270 : 105788592 : }
4271 : :
4272 : : /* Build builtin functions common to both C and C++ language
4273 : : frontends. */
4274 : :
4275 : : static void
4276 : 209898 : c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
4277 : : {
4278 : : #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
4279 : : builtin_types[ENUM] = VALUE;
4280 : : #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
4281 : : def_fn_type (ENUM, RETURN, 0, 0);
4282 : : #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
4283 : : def_fn_type (ENUM, RETURN, 0, 1, ARG1);
4284 : : #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
4285 : : def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
4286 : : #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4287 : : def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
4288 : : #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4289 : : def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
4290 : : #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4291 : : def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4292 : : #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4293 : : ARG6) \
4294 : : def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
4295 : : #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4296 : : ARG6, ARG7) \
4297 : : def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
4298 : : #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4299 : : ARG6, ARG7, ARG8) \
4300 : : def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
4301 : : ARG7, ARG8);
4302 : : #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4303 : : ARG6, ARG7, ARG8, ARG9) \
4304 : : def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
4305 : : ARG7, ARG8, ARG9);
4306 : : #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4307 : : ARG6, ARG7, ARG8, ARG9, ARG10) \
4308 : : def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
4309 : : ARG7, ARG8, ARG9, ARG10);
4310 : : #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4311 : : ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
4312 : : def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
4313 : : ARG7, ARG8, ARG9, ARG10, ARG11);
4314 : : #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
4315 : : def_fn_type (ENUM, RETURN, 1, 0);
4316 : : #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
4317 : : def_fn_type (ENUM, RETURN, 1, 1, ARG1);
4318 : : #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
4319 : : def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
4320 : : #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4321 : : def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
4322 : : #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4323 : : def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
4324 : : #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4325 : : def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4326 : : #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4327 : : ARG6) \
4328 : : def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
4329 : : #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4330 : : ARG6, ARG7) \
4331 : : def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
4332 : : #define DEF_POINTER_TYPE(ENUM, TYPE) \
4333 : : builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
4334 : :
4335 : : #include "builtin-types.def"
4336 : :
4337 : : #undef DEF_PRIMITIVE_TYPE
4338 : : #undef DEF_FUNCTION_TYPE_0
4339 : : #undef DEF_FUNCTION_TYPE_1
4340 : : #undef DEF_FUNCTION_TYPE_2
4341 : : #undef DEF_FUNCTION_TYPE_3
4342 : : #undef DEF_FUNCTION_TYPE_4
4343 : : #undef DEF_FUNCTION_TYPE_5
4344 : : #undef DEF_FUNCTION_TYPE_6
4345 : : #undef DEF_FUNCTION_TYPE_7
4346 : : #undef DEF_FUNCTION_TYPE_8
4347 : : #undef DEF_FUNCTION_TYPE_9
4348 : : #undef DEF_FUNCTION_TYPE_10
4349 : : #undef DEF_FUNCTION_TYPE_11
4350 : : #undef DEF_FUNCTION_TYPE_VAR_0
4351 : : #undef DEF_FUNCTION_TYPE_VAR_1
4352 : : #undef DEF_FUNCTION_TYPE_VAR_2
4353 : : #undef DEF_FUNCTION_TYPE_VAR_3
4354 : : #undef DEF_FUNCTION_TYPE_VAR_4
4355 : : #undef DEF_FUNCTION_TYPE_VAR_5
4356 : : #undef DEF_FUNCTION_TYPE_VAR_6
4357 : : #undef DEF_FUNCTION_TYPE_VAR_7
4358 : : #undef DEF_POINTER_TYPE
4359 : 209898 : builtin_types[(int) BT_LAST] = NULL_TREE;
4360 : :
4361 : 209898 : c_init_attributes ();
4362 : :
4363 : : #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
4364 : : NONANSI_P, ATTRS, IMPLICIT, COND) \
4365 : : if (NAME && COND) \
4366 : : def_builtin_1 (ENUM, NAME, CLASS, \
4367 : : builtin_types[(int) TYPE], \
4368 : : builtin_types[(int) LIBTYPE], \
4369 : : BOTH_P, FALLBACK_P, NONANSI_P, \
4370 : : built_in_attributes[(int) ATTRS], IMPLICIT);
4371 : : #include "builtins.def"
4372 : :
4373 : 209898 : targetm.init_builtins ();
4374 : :
4375 : 209898 : build_common_builtin_nodes ();
4376 : 209898 : }
4377 : :
4378 : : /* Like get_identifier, but avoid warnings about null arguments when
4379 : : the argument may be NULL for targets where GCC lacks stdint.h type
4380 : : information. */
4381 : :
4382 : : static inline tree
4383 : 5667246 : c_get_ident (const char *id)
4384 : : {
4385 : 2518776 : return get_identifier (id);
4386 : : }
4387 : :
4388 : : /* Build tree nodes and builtin functions common to both C and C++ language
4389 : : frontends. */
4390 : :
4391 : : void
4392 : 209898 : c_common_nodes_and_builtins (void)
4393 : : {
4394 : 209898 : int char8_type_size;
4395 : 209898 : int char16_type_size;
4396 : 209898 : int char32_type_size;
4397 : 209898 : int wchar_type_size;
4398 : 209898 : tree array_domain_type;
4399 : 209898 : tree va_list_ref_type_node;
4400 : 209898 : tree va_list_arg_type_node;
4401 : 209898 : int i;
4402 : :
4403 : 209898 : build_common_tree_nodes (flag_signed_char);
4404 : :
4405 : : /* Define `int' and `char' first so that dbx will output them first. */
4406 : 209898 : record_builtin_type (RID_INT, NULL, integer_type_node);
4407 : 209898 : record_builtin_type (RID_CHAR, "char", char_type_node);
4408 : :
4409 : : /* `signed' is the same as `int'. FIXME: the declarations of "signed",
4410 : : "unsigned long", "long long unsigned" and "unsigned short" were in C++
4411 : : but not C. Are the conditionals here needed? */
4412 : 209898 : if (c_dialect_cxx ())
4413 : 96634 : record_builtin_type (RID_SIGNED, NULL, integer_type_node);
4414 : 209898 : record_builtin_type (RID_LONG, "long int", long_integer_type_node);
4415 : 209898 : record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
4416 : 209898 : record_builtin_type (RID_MAX, "long unsigned int",
4417 : : long_unsigned_type_node);
4418 : :
4419 : 419796 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
4420 : : {
4421 : 209898 : char name[25];
4422 : :
4423 : 209898 : sprintf (name, "__int%d", int_n_data[i].bitsize);
4424 : 209898 : record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
4425 : : int_n_trees[i].signed_type);
4426 : 209898 : sprintf (name, "__int%d__", int_n_data[i].bitsize);
4427 : 209898 : record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
4428 : : int_n_trees[i].signed_type);
4429 : 419796 : ridpointers[RID_FIRST_INT_N + i]
4430 : 209898 : = DECL_NAME (TYPE_NAME (int_n_trees[i].signed_type));
4431 : :
4432 : 209898 : sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
4433 : 209898 : record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
4434 : 209898 : sprintf (name, "__int%d__ unsigned", int_n_data[i].bitsize);
4435 : 209898 : record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
4436 : : }
4437 : :
4438 : 209898 : if (c_dialect_cxx ())
4439 : 96634 : record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
4440 : 209898 : record_builtin_type (RID_MAX, "long long int",
4441 : : long_long_integer_type_node);
4442 : 209898 : record_builtin_type (RID_MAX, "long long unsigned int",
4443 : : long_long_unsigned_type_node);
4444 : 209898 : if (c_dialect_cxx ())
4445 : 96634 : record_builtin_type (RID_MAX, "long long unsigned",
4446 : : long_long_unsigned_type_node);
4447 : 209898 : record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
4448 : 209898 : record_builtin_type (RID_MAX, "short unsigned int",
4449 : : short_unsigned_type_node);
4450 : 209898 : if (c_dialect_cxx ())
4451 : 96634 : record_builtin_type (RID_MAX, "unsigned short",
4452 : : short_unsigned_type_node);
4453 : :
4454 : : /* Define both `signed char' and `unsigned char'. */
4455 : 209898 : record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
4456 : 209898 : record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
4457 : :
4458 : : /* These are types that c_common_type_for_size and
4459 : : c_common_type_for_mode use. */
4460 : 209898 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4461 : : TYPE_DECL, NULL_TREE,
4462 : : intQI_type_node));
4463 : 209898 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4464 : : TYPE_DECL, NULL_TREE,
4465 : : intHI_type_node));
4466 : 209898 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4467 : : TYPE_DECL, NULL_TREE,
4468 : : intSI_type_node));
4469 : 209898 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4470 : : TYPE_DECL, NULL_TREE,
4471 : : intDI_type_node));
4472 : : #if HOST_BITS_PER_WIDE_INT >= 64
4473 : : /* Note that this is different than the __int128 type that's part of
4474 : : the generic __intN support. */
4475 : 209898 : if (targetm.scalar_mode_supported_p (TImode))
4476 : 204560 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4477 : : TYPE_DECL,
4478 : : get_identifier ("__int128_t"),
4479 : : intTI_type_node));
4480 : : #endif
4481 : 209898 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4482 : : TYPE_DECL, NULL_TREE,
4483 : : unsigned_intQI_type_node));
4484 : 209898 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4485 : : TYPE_DECL, NULL_TREE,
4486 : : unsigned_intHI_type_node));
4487 : 209898 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4488 : : TYPE_DECL, NULL_TREE,
4489 : : unsigned_intSI_type_node));
4490 : 209898 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4491 : : TYPE_DECL, NULL_TREE,
4492 : : unsigned_intDI_type_node));
4493 : : #if HOST_BITS_PER_WIDE_INT >= 64
4494 : 209898 : if (targetm.scalar_mode_supported_p (TImode))
4495 : 204560 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4496 : : TYPE_DECL,
4497 : : get_identifier ("__uint128_t"),
4498 : : unsigned_intTI_type_node));
4499 : : #endif
4500 : :
4501 : : /* Create the widest literal types. */
4502 : 209898 : if (targetm.scalar_mode_supported_p (TImode))
4503 : : {
4504 : 204560 : widest_integer_literal_type_node = intTI_type_node;
4505 : 204560 : widest_unsigned_literal_type_node = unsigned_intTI_type_node;
4506 : : }
4507 : : else
4508 : : {
4509 : 5338 : widest_integer_literal_type_node = intDI_type_node;
4510 : 5338 : widest_unsigned_literal_type_node = unsigned_intDI_type_node;
4511 : : }
4512 : :
4513 : 209898 : signed_size_type_node = c_common_signed_type (size_type_node);
4514 : :
4515 : 419796 : pid_type_node =
4516 : 209898 : TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
4517 : :
4518 : 209898 : record_builtin_type (RID_FLOAT, NULL, float_type_node);
4519 : 209898 : record_builtin_type (RID_DOUBLE, NULL, double_type_node);
4520 : 209898 : record_builtin_type (RID_MAX, "long double", long_double_type_node);
4521 : :
4522 : 1889082 : for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4523 : : {
4524 : 1469286 : if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4525 : 1259388 : record_builtin_type ((enum rid) (RID_FLOATN_NX_FIRST + i), NULL,
4526 : : FLOATN_NX_TYPE_NODE (i));
4527 : : }
4528 : :
4529 : : /* For C, let float128t_type_node (__float128 in some backends) be the
4530 : : same type as float128_type_node (_Float128), for C++ let those
4531 : : be distinct types that mangle and behave differently. */
4532 : 209898 : if (c_dialect_cxx ())
4533 : 96634 : float128t_type_node = NULL_TREE;
4534 : :
4535 : : /* Only supported decimal floating point extension if the target
4536 : : actually supports underlying modes. */
4537 : 209898 : if (targetm.scalar_mode_supported_p (SDmode)
4538 : 209898 : && targetm.scalar_mode_supported_p (DDmode)
4539 : 419796 : && targetm.scalar_mode_supported_p (TDmode))
4540 : : {
4541 : 209898 : record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
4542 : 209898 : record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
4543 : 209898 : record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
4544 : 209898 : record_builtin_type (RID_DFLOAT64X, NULL, dfloat64x_type_node);
4545 : : }
4546 : :
4547 : 209898 : if (targetm.fixed_point_supported_p ())
4548 : : {
4549 : 0 : record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
4550 : 0 : record_builtin_type (RID_FRACT, NULL, fract_type_node);
4551 : 0 : record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
4552 : 0 : record_builtin_type (RID_MAX, "long long _Fract",
4553 : : long_long_fract_type_node);
4554 : 0 : record_builtin_type (RID_MAX, "unsigned short _Fract",
4555 : : unsigned_short_fract_type_node);
4556 : 0 : record_builtin_type (RID_MAX, "unsigned _Fract",
4557 : : unsigned_fract_type_node);
4558 : 0 : record_builtin_type (RID_MAX, "unsigned long _Fract",
4559 : : unsigned_long_fract_type_node);
4560 : 0 : record_builtin_type (RID_MAX, "unsigned long long _Fract",
4561 : : unsigned_long_long_fract_type_node);
4562 : 0 : record_builtin_type (RID_MAX, "_Sat short _Fract",
4563 : : sat_short_fract_type_node);
4564 : 0 : record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
4565 : 0 : record_builtin_type (RID_MAX, "_Sat long _Fract",
4566 : : sat_long_fract_type_node);
4567 : 0 : record_builtin_type (RID_MAX, "_Sat long long _Fract",
4568 : : sat_long_long_fract_type_node);
4569 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
4570 : : sat_unsigned_short_fract_type_node);
4571 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
4572 : : sat_unsigned_fract_type_node);
4573 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
4574 : : sat_unsigned_long_fract_type_node);
4575 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
4576 : : sat_unsigned_long_long_fract_type_node);
4577 : 0 : record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
4578 : 0 : record_builtin_type (RID_ACCUM, NULL, accum_type_node);
4579 : 0 : record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
4580 : 0 : record_builtin_type (RID_MAX, "long long _Accum",
4581 : : long_long_accum_type_node);
4582 : 0 : record_builtin_type (RID_MAX, "unsigned short _Accum",
4583 : : unsigned_short_accum_type_node);
4584 : 0 : record_builtin_type (RID_MAX, "unsigned _Accum",
4585 : : unsigned_accum_type_node);
4586 : 0 : record_builtin_type (RID_MAX, "unsigned long _Accum",
4587 : : unsigned_long_accum_type_node);
4588 : 0 : record_builtin_type (RID_MAX, "unsigned long long _Accum",
4589 : : unsigned_long_long_accum_type_node);
4590 : 0 : record_builtin_type (RID_MAX, "_Sat short _Accum",
4591 : : sat_short_accum_type_node);
4592 : 0 : record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
4593 : 0 : record_builtin_type (RID_MAX, "_Sat long _Accum",
4594 : : sat_long_accum_type_node);
4595 : 0 : record_builtin_type (RID_MAX, "_Sat long long _Accum",
4596 : : sat_long_long_accum_type_node);
4597 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
4598 : : sat_unsigned_short_accum_type_node);
4599 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
4600 : : sat_unsigned_accum_type_node);
4601 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
4602 : : sat_unsigned_long_accum_type_node);
4603 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
4604 : : sat_unsigned_long_long_accum_type_node);
4605 : :
4606 : : }
4607 : :
4608 : 209898 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4609 : : TYPE_DECL,
4610 : : get_identifier ("complex int"),
4611 : : complex_integer_type_node));
4612 : 209898 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4613 : : TYPE_DECL,
4614 : : get_identifier ("complex float"),
4615 : : complex_float_type_node));
4616 : 209898 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4617 : : TYPE_DECL,
4618 : : get_identifier ("complex double"),
4619 : : complex_double_type_node));
4620 : 209898 : lang_hooks.decls.pushdecl
4621 : 209898 : (build_decl (UNKNOWN_LOCATION,
4622 : : TYPE_DECL, get_identifier ("complex long double"),
4623 : : complex_long_double_type_node));
4624 : :
4625 : 209898 : if (!c_dialect_cxx ())
4626 : 906112 : for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4627 : 792848 : if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4628 : : {
4629 : 679584 : char buf[30];
4630 : 1359168 : sprintf (buf, "complex _Float%d%s", floatn_nx_types[i].n,
4631 : 679584 : floatn_nx_types[i].extended ? "x" : "");
4632 : 679584 : lang_hooks.decls.pushdecl
4633 : 679584 : (build_decl (UNKNOWN_LOCATION,
4634 : : TYPE_DECL,
4635 : : get_identifier (buf),
4636 : : COMPLEX_FLOATN_NX_TYPE_NODE (i)));
4637 : : }
4638 : :
4639 : : /* Make fileptr_type_node a distinct void * type until
4640 : : FILE type is defined. Likewise for const struct tm*. */
4641 : 1469286 : for (unsigned i = 0; i < ARRAY_SIZE (builtin_structptr_types); ++i)
4642 : 1259388 : builtin_structptr_types[i].node
4643 : 1259388 : = build_variant_type_copy (builtin_structptr_types[i].base);
4644 : :
4645 : 209898 : record_builtin_type (RID_VOID, NULL, void_type_node);
4646 : :
4647 : : /* Set the TYPE_NAME for any variants that were built before
4648 : : record_builtin_type gave names to the built-in types. */
4649 : 209898 : {
4650 : 209898 : tree void_name = TYPE_NAME (void_type_node);
4651 : 209898 : TYPE_NAME (void_type_node) = NULL_TREE;
4652 : 209898 : TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
4653 : 209898 : = void_name;
4654 : 209898 : TYPE_NAME (void_type_node) = void_name;
4655 : : }
4656 : :
4657 : : /* Make a type to be the domain of a few array types
4658 : : whose domains don't really matter.
4659 : : 200 is small enough that it always fits in size_t
4660 : : and large enough that it can hold most function names for the
4661 : : initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4662 : 209898 : array_domain_type = build_index_type (size_int (200));
4663 : :
4664 : : /* Make a type for arrays of characters.
4665 : : With luck nothing will ever really depend on the length of this
4666 : : array type. */
4667 : 209898 : char_array_type_node
4668 : 209898 : = build_array_type (char_type_node, array_domain_type);
4669 : :
4670 : 209898 : string_type_node = build_pointer_type (char_type_node);
4671 : 209898 : const_string_type_node
4672 : 209898 : = build_pointer_type (build_qualified_type
4673 : : (char_type_node, TYPE_QUAL_CONST));
4674 : :
4675 : : /* This is special for C++ so functions can be overloaded. */
4676 : 215339 : wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
4677 : 209898 : wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
4678 : 209898 : wchar_type_size = TYPE_PRECISION (wchar_type_node);
4679 : 209898 : underlying_wchar_type_node = wchar_type_node;
4680 : 209898 : if (c_dialect_cxx ())
4681 : : {
4682 : 96634 : if (TYPE_UNSIGNED (wchar_type_node))
4683 : 7 : wchar_type_node = make_unsigned_type (wchar_type_size);
4684 : : else
4685 : 96627 : wchar_type_node = make_signed_type (wchar_type_size);
4686 : 96634 : record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
4687 : : }
4688 : :
4689 : : /* This is for wide string constants. */
4690 : 209898 : wchar_array_type_node
4691 : 209898 : = build_array_type (wchar_type_node, array_domain_type);
4692 : :
4693 : : /* Define 'char8_t'. */
4694 : 209898 : char8_type_node = get_identifier (CHAR8_TYPE);
4695 : 209898 : char8_type_node = TREE_TYPE (identifier_global_value (char8_type_node));
4696 : 209898 : char8_type_size = TYPE_PRECISION (char8_type_node);
4697 : 209898 : if (c_dialect_cxx ())
4698 : : {
4699 : 96634 : char8_type_node = make_unsigned_type (char8_type_size);
4700 : 96634 : TYPE_STRING_FLAG (char8_type_node) = true;
4701 : :
4702 : 96634 : if (flag_char8_t)
4703 : 30742 : record_builtin_type (RID_CHAR8, "char8_t", char8_type_node);
4704 : : }
4705 : :
4706 : : /* This is for UTF-8 string constants. */
4707 : 209898 : char8_array_type_node
4708 : 209898 : = build_array_type (char8_type_node, array_domain_type);
4709 : :
4710 : : /* Define 'char16_t'. */
4711 : 209898 : char16_type_node = get_identifier (CHAR16_TYPE);
4712 : 209898 : char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
4713 : 209898 : char16_type_size = TYPE_PRECISION (char16_type_node);
4714 : 209898 : if (c_dialect_cxx ())
4715 : : {
4716 : 96634 : char16_type_node = make_unsigned_type (char16_type_size);
4717 : :
4718 : 96634 : if (cxx_dialect >= cxx11)
4719 : 82611 : record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
4720 : : }
4721 : :
4722 : : /* This is for UTF-16 string constants. */
4723 : 209898 : char16_array_type_node
4724 : 209898 : = build_array_type (char16_type_node, array_domain_type);
4725 : :
4726 : : /* Define 'char32_t'. */
4727 : 209898 : char32_type_node = get_identifier (CHAR32_TYPE);
4728 : 209898 : char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
4729 : 209898 : char32_type_size = TYPE_PRECISION (char32_type_node);
4730 : 209898 : if (c_dialect_cxx ())
4731 : : {
4732 : 96634 : char32_type_node = make_unsigned_type (char32_type_size);
4733 : :
4734 : 96634 : if (cxx_dialect >= cxx11)
4735 : 82611 : record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
4736 : : }
4737 : :
4738 : : /* This is for UTF-32 string constants. */
4739 : 209898 : char32_array_type_node
4740 : 209898 : = build_array_type (char32_type_node, array_domain_type);
4741 : :
4742 : 209898 : if (strcmp (WINT_TYPE, "wchar_t") == 0)
4743 : : wint_type_node = wchar_type_node;
4744 : : else
4745 : 419796 : wint_type_node =
4746 : 209898 : TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
4747 : :
4748 : 419796 : intmax_type_node =
4749 : 215339 : TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
4750 : 419796 : uintmax_type_node =
4751 : 215339 : TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
4752 : :
4753 : 209898 : if (SIG_ATOMIC_TYPE)
4754 : 209898 : sig_atomic_type_node =
4755 : 209898 : TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
4756 : 209898 : if (INT8_TYPE)
4757 : 209898 : int8_type_node =
4758 : 209898 : TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
4759 : 209898 : if (INT16_TYPE)
4760 : 209898 : int16_type_node =
4761 : 209898 : TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
4762 : 209898 : if (INT32_TYPE)
4763 : 209898 : int32_type_node =
4764 : 209898 : TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
4765 : 209898 : if (INT64_TYPE)
4766 : 419796 : int64_type_node =
4767 : 419796 : TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
4768 : 209898 : if (UINT8_TYPE)
4769 : 209898 : uint8_type_node =
4770 : 209898 : TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
4771 : 209898 : if (UINT16_TYPE)
4772 : 209898 : c_uint16_type_node = uint16_type_node =
4773 : 209898 : TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
4774 : 209898 : if (UINT32_TYPE)
4775 : 209898 : c_uint32_type_node = uint32_type_node =
4776 : 209898 : TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
4777 : 209898 : if (UINT64_TYPE)
4778 : 419796 : c_uint64_type_node = uint64_type_node =
4779 : 419796 : TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
4780 : 209898 : if (INT_LEAST8_TYPE)
4781 : 209898 : int_least8_type_node =
4782 : 209898 : TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
4783 : 209898 : if (INT_LEAST16_TYPE)
4784 : 209898 : int_least16_type_node =
4785 : 209898 : TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
4786 : 209898 : if (INT_LEAST32_TYPE)
4787 : 209898 : int_least32_type_node =
4788 : 209898 : TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
4789 : 209898 : if (INT_LEAST64_TYPE)
4790 : 419796 : int_least64_type_node =
4791 : 419796 : TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
4792 : 209898 : if (UINT_LEAST8_TYPE)
4793 : 209898 : uint_least8_type_node =
4794 : 209898 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
4795 : 209898 : if (UINT_LEAST16_TYPE)
4796 : 209898 : uint_least16_type_node =
4797 : 209898 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
4798 : 209898 : if (UINT_LEAST32_TYPE)
4799 : 209898 : uint_least32_type_node =
4800 : 209898 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
4801 : 209898 : if (UINT_LEAST64_TYPE)
4802 : 419796 : uint_least64_type_node =
4803 : 419796 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
4804 : 209898 : if (INT_FAST8_TYPE)
4805 : 209898 : int_fast8_type_node =
4806 : 209898 : TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
4807 : 209898 : if (INT_FAST16_TYPE)
4808 : 419796 : int_fast16_type_node =
4809 : 419796 : TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
4810 : 209898 : if (INT_FAST32_TYPE)
4811 : 419796 : int_fast32_type_node =
4812 : 419796 : TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
4813 : 209898 : if (INT_FAST64_TYPE)
4814 : 419796 : int_fast64_type_node =
4815 : 419796 : TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
4816 : 209898 : if (UINT_FAST8_TYPE)
4817 : 209898 : uint_fast8_type_node =
4818 : 209898 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
4819 : 209898 : if (UINT_FAST16_TYPE)
4820 : 419796 : uint_fast16_type_node =
4821 : 419796 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
4822 : 209898 : if (UINT_FAST32_TYPE)
4823 : 419796 : uint_fast32_type_node =
4824 : 419796 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
4825 : 209898 : if (UINT_FAST64_TYPE)
4826 : 419796 : uint_fast64_type_node =
4827 : 419796 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
4828 : 209898 : if (INTPTR_TYPE)
4829 : 419796 : intptr_type_node =
4830 : 419796 : TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
4831 : 209898 : if (UINTPTR_TYPE)
4832 : 419796 : uintptr_type_node =
4833 : 419796 : TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
4834 : :
4835 : 209898 : default_function_type = build_function_type (integer_type_node, NULL_TREE);
4836 : 209898 : unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
4837 : :
4838 : 209898 : lang_hooks.decls.pushdecl
4839 : 209898 : (build_decl (UNKNOWN_LOCATION,
4840 : : TYPE_DECL, get_identifier ("__builtin_va_list"),
4841 : : va_list_type_node));
4842 : 209898 : if (targetm.enum_va_list_p)
4843 : : {
4844 : : int l;
4845 : : const char *pname;
4846 : : tree ptype;
4847 : :
4848 : 619018 : for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
4849 : : {
4850 : 409120 : lang_hooks.decls.pushdecl
4851 : 409120 : (build_decl (UNKNOWN_LOCATION,
4852 : : TYPE_DECL, get_identifier (pname),
4853 : : ptype));
4854 : :
4855 : : }
4856 : : }
4857 : :
4858 : 209898 : if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4859 : : {
4860 : 409036 : va_list_arg_type_node = va_list_ref_type_node =
4861 : 204518 : build_pointer_type (TREE_TYPE (va_list_type_node));
4862 : : }
4863 : : else
4864 : : {
4865 : 5380 : va_list_arg_type_node = va_list_type_node;
4866 : 5380 : va_list_ref_type_node = build_reference_type (va_list_type_node);
4867 : : }
4868 : :
4869 : 209898 : c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
4870 : :
4871 : 209898 : main_identifier_node = get_identifier ("main");
4872 : :
4873 : : /* Create the built-in __null node. It is important that this is
4874 : : not shared. */
4875 : 209898 : null_node = make_int_cst (1, 1);
4876 : 215339 : TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
4877 : :
4878 : : /* Create the built-in nullptr node. This part of its initialization is
4879 : : common to C and C++. The front ends can further adjust its definition
4880 : : in {c,cxx}_init_decl_processing. In particular, we aren't setting the
4881 : : alignment here for C++ backward ABI bug compatibility. */
4882 : 209898 : nullptr_type_node = make_node (NULLPTR_TYPE);
4883 : 419796 : TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4884 : 419796 : TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4885 : 209898 : TYPE_UNSIGNED (nullptr_type_node) = 1;
4886 : 419796 : TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4887 : 209898 : SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4888 : 209898 : nullptr_node = build_int_cst (nullptr_type_node, 0);
4889 : :
4890 : : /* Since builtin_types isn't gc'ed, don't export these nodes. */
4891 : 209898 : memset (builtin_types, 0, sizeof (builtin_types));
4892 : 209898 : }
4893 : :
4894 : : /* The number of named compound-literals generated thus far. */
4895 : : static GTY(()) int compound_literal_number;
4896 : :
4897 : : /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
4898 : :
4899 : : void
4900 : 279 : set_compound_literal_name (tree decl)
4901 : : {
4902 : 279 : char *name;
4903 : 279 : ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
4904 : : compound_literal_number);
4905 : 279 : compound_literal_number++;
4906 : 279 : DECL_NAME (decl) = get_identifier (name);
4907 : 279 : }
4908 : :
4909 : : /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
4910 : : TYPE and operand OP. */
4911 : :
4912 : : static tree
4913 : 50940 : build_va_arg_1 (location_t loc, tree type, tree op)
4914 : : {
4915 : 50940 : tree expr = build1 (VA_ARG_EXPR, type, op);
4916 : 50940 : SET_EXPR_LOCATION (expr, loc);
4917 : 50940 : return expr;
4918 : : }
4919 : :
4920 : : /* Return a VA_ARG_EXPR corresponding to a source-level expression
4921 : : va_arg (EXPR, TYPE) at source location LOC. */
4922 : :
4923 : : tree
4924 : 50940 : build_va_arg (location_t loc, tree expr, tree type)
4925 : : {
4926 : 50940 : tree va_type = TREE_TYPE (expr);
4927 : 50940 : tree canon_va_type = (va_type == error_mark_node
4928 : 50940 : ? error_mark_node
4929 : 50930 : : targetm.canonical_va_list_type (va_type));
4930 : :
4931 : 50940 : if (va_type == error_mark_node
4932 : 50930 : || canon_va_type == NULL_TREE)
4933 : : {
4934 : 10 : if (canon_va_type == NULL_TREE)
4935 : 12 : error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4936 : :
4937 : : /* Let's handle things neutrally, if expr:
4938 : : - has undeclared type, or
4939 : : - is not an va_list type. */
4940 : 22 : return build_va_arg_1 (loc, type, error_mark_node);
4941 : : }
4942 : :
4943 : 50918 : if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
4944 : : {
4945 : : /* Case 1: Not an array type. */
4946 : :
4947 : : /* Take the address, to get '&ap'. Note that &ap is not a va_list
4948 : : type. */
4949 : 258 : c_common_mark_addressable_vec (expr);
4950 : 258 : expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
4951 : :
4952 : 258 : return build_va_arg_1 (loc, type, expr);
4953 : : }
4954 : :
4955 : : /* Case 2: Array type.
4956 : :
4957 : : Background:
4958 : :
4959 : : For contrast, let's start with the simple case (case 1). If
4960 : : canon_va_type is not an array type, but say a char *, then when
4961 : : passing-by-value a va_list, the type of the va_list param decl is
4962 : : the same as for another va_list decl (all ap's are char *):
4963 : :
4964 : : f2_1 (char * ap)
4965 : : D.1815 = VA_ARG (&ap, 0B, 1);
4966 : : return D.1815;
4967 : :
4968 : : f2 (int i)
4969 : : char * ap.0;
4970 : : char * ap;
4971 : : __builtin_va_start (&ap, 0);
4972 : : ap.0 = ap;
4973 : : res = f2_1 (ap.0);
4974 : : __builtin_va_end (&ap);
4975 : : D.1812 = res;
4976 : : return D.1812;
4977 : :
4978 : : However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
4979 : : va_list the type of the va_list param decl (case 2b, struct * ap) is not
4980 : : the same as for another va_list decl (case 2a, struct ap[1]).
4981 : :
4982 : : f2_1 (struct * ap)
4983 : : D.1844 = VA_ARG (ap, 0B, 0);
4984 : : return D.1844;
4985 : :
4986 : : f2 (int i)
4987 : : struct ap[1];
4988 : : __builtin_va_start (&ap, 0);
4989 : : res = f2_1 (&ap);
4990 : : __builtin_va_end (&ap);
4991 : : D.1841 = res;
4992 : : return D.1841;
4993 : :
4994 : : Case 2b is different because:
4995 : : - on the callee side, the parm decl has declared type va_list, but
4996 : : grokdeclarator changes the type of the parm decl to a pointer to the
4997 : : array elem type.
4998 : : - on the caller side, the pass-by-value uses &ap.
4999 : :
5000 : : We unify these two cases (case 2a: va_list is array type,
5001 : : case 2b: va_list is pointer to array elem type), by adding '&' for the
5002 : : array type case, such that we have a pointer to array elem in both
5003 : : cases. */
5004 : :
5005 : 50660 : if (TREE_CODE (va_type) == ARRAY_TYPE)
5006 : : {
5007 : : /* Case 2a: va_list is array type. */
5008 : :
5009 : : /* Take the address, to get '&ap'. Make sure it's a pointer to array
5010 : : elem type. */
5011 : 49913 : c_common_mark_addressable_vec (expr);
5012 : 49913 : expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
5013 : : expr);
5014 : :
5015 : : /* Verify that &ap is still recognized as having va_list type. */
5016 : 49913 : tree canon_expr_type
5017 : 49913 : = targetm.canonical_va_list_type (TREE_TYPE (expr));
5018 : 49913 : gcc_assert (canon_expr_type != NULL_TREE);
5019 : : }
5020 : : else
5021 : : {
5022 : : /* Case 2b: va_list is pointer to array elem type. */
5023 : 747 : gcc_assert (POINTER_TYPE_P (va_type));
5024 : :
5025 : : /* Comparison as in std_canonical_va_list_type. */
5026 : 747 : gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (va_type))
5027 : : == TYPE_MAIN_VARIANT (TREE_TYPE (canon_va_type)));
5028 : :
5029 : : /* Don't take the address. We've already got '&ap'. */
5030 : 50660 : ;
5031 : : }
5032 : :
5033 : 50660 : return build_va_arg_1 (loc, type, expr);
5034 : : }
5035 : :
5036 : :
5037 : : /* Linked list of disabled built-in functions. */
5038 : :
5039 : : struct disabled_builtin
5040 : : {
5041 : : const char *name;
5042 : : struct disabled_builtin *next;
5043 : : };
5044 : : static disabled_builtin *disabled_builtins = NULL;
5045 : :
5046 : : static bool builtin_function_disabled_p (const char *);
5047 : :
5048 : : /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5049 : : begins with "__builtin_", give an error. */
5050 : :
5051 : : void
5052 : 308 : disable_builtin_function (const char *name)
5053 : : {
5054 : 308 : if (startswith (name, "__builtin_"))
5055 : 0 : error ("cannot disable built-in function %qs", name);
5056 : : else
5057 : : {
5058 : 308 : disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5059 : 308 : new_disabled_builtin->name = name;
5060 : 308 : new_disabled_builtin->next = disabled_builtins;
5061 : 308 : disabled_builtins = new_disabled_builtin;
5062 : : }
5063 : 308 : }
5064 : :
5065 : :
5066 : : /* Return true if the built-in function NAME has been disabled, false
5067 : : otherwise. */
5068 : :
5069 : : static bool
5070 : 161829504 : builtin_function_disabled_p (const char *name)
5071 : : {
5072 : 161829504 : disabled_builtin *p;
5073 : 162113964 : for (p = disabled_builtins; p != NULL; p = p->next)
5074 : : {
5075 : 284768 : if (strcmp (name, p->name) == 0)
5076 : : return true;
5077 : : }
5078 : : return false;
5079 : : }
5080 : :
5081 : :
5082 : : /* Worker for DEF_BUILTIN.
5083 : : Possibly define a builtin function with one or two names.
5084 : : Does not declare a non-__builtin_ function if flag_no_builtin, or if
5085 : : nonansi_p and flag_no_nonansi_builtin. */
5086 : :
5087 : : static void
5088 : 317113872 : def_builtin_1 (enum built_in_function fncode,
5089 : : const char *name,
5090 : : enum built_in_class fnclass,
5091 : : tree fntype, tree libtype,
5092 : : bool both_p, bool fallback_p, bool nonansi_p,
5093 : : tree fnattrs, bool implicit_p)
5094 : : {
5095 : 317113872 : tree decl;
5096 : 317113872 : const char *libname;
5097 : :
5098 : 317113872 : if (fntype == error_mark_node)
5099 : : return;
5100 : :
5101 : 298886490 : gcc_assert ((!both_p && !fallback_p)
5102 : : || startswith (name, "__builtin_"));
5103 : :
5104 : 298886490 : libname = name + strlen ("__builtin_");
5105 : 298886490 : decl = add_builtin_function (name, fntype, fncode, fnclass,
5106 : : (fallback_p ? libname : NULL),
5107 : : fnattrs);
5108 : :
5109 : 298886490 : set_builtin_decl (fncode, decl, implicit_p);
5110 : :
5111 : 298886490 : if (both_p
5112 : 162252303 : && !flag_no_builtin && !builtin_function_disabled_p (libname)
5113 : 460715686 : && !(nonansi_p && flag_no_nonansi_builtin))
5114 : 145918885 : add_builtin_function (libname, libtype, fncode, fnclass,
5115 : : NULL, fnattrs);
5116 : : }
5117 : :
5118 : : /* Nonzero if the type T promotes to int. This is (nearly) the
5119 : : integral promotions defined in ISO C99 6.3.1.1/2. */
5120 : :
5121 : : bool
5122 : 504881918 : c_promoting_integer_type_p (const_tree t)
5123 : : {
5124 : 504881918 : switch (TREE_CODE (t))
5125 : : {
5126 : 392355786 : case INTEGER_TYPE:
5127 : 392355786 : return (TYPE_MAIN_VARIANT (t) == char_type_node
5128 : 386625530 : || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5129 : 386234908 : || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5130 : 371559280 : || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5131 : 370511731 : || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5132 : 757346312 : || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5133 : :
5134 : 672941 : case ENUMERAL_TYPE:
5135 : : /* ??? Technically all enumerations not larger than an int
5136 : : promote to an int. But this is used along code paths
5137 : : that only want to notice a size change. */
5138 : 672941 : return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5139 : :
5140 : : case BOOLEAN_TYPE:
5141 : : return true;
5142 : :
5143 : : default:
5144 : : return false;
5145 : : }
5146 : : }
5147 : :
5148 : : /* Return 1 if PARMS specifies a fixed number of parameters
5149 : : and none of their types is affected by default promotions. */
5150 : :
5151 : : bool
5152 : 21849 : self_promoting_args_p (const_tree parms)
5153 : : {
5154 : 21849 : const_tree t;
5155 : 65045 : for (t = parms; t; t = TREE_CHAIN (t))
5156 : : {
5157 : 43729 : tree type = TREE_VALUE (t);
5158 : :
5159 : 43729 : if (type == error_mark_node)
5160 : 2 : continue;
5161 : :
5162 : 43727 : if (TREE_CHAIN (t) == NULL_TREE && type != void_type_node)
5163 : : return false;
5164 : :
5165 : 43244 : if (type == NULL_TREE)
5166 : : return false;
5167 : :
5168 : 43244 : if (TYPE_MAIN_VARIANT (type) == float_type_node)
5169 : : return false;
5170 : :
5171 : 43199 : if (c_promoting_integer_type_p (type))
5172 : : return false;
5173 : : }
5174 : : return true;
5175 : : }
5176 : :
5177 : : /* Recursively remove any '*' or '&' operator from TYPE. */
5178 : : tree
5179 : 38017129 : strip_pointer_operator (tree t)
5180 : : {
5181 : 40275227 : while (POINTER_TYPE_P (t))
5182 : 2258098 : t = TREE_TYPE (t);
5183 : 38017129 : return t;
5184 : : }
5185 : :
5186 : : /* Recursively remove pointer or array type from TYPE. */
5187 : : tree
5188 : 10644973 : strip_pointer_or_array_types (tree t)
5189 : : {
5190 : 15398902 : while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5191 : 4753929 : t = TREE_TYPE (t);
5192 : 10644973 : return t;
5193 : : }
5194 : :
5195 : : /* Used to compare case labels. K1 and K2 are actually tree nodes
5196 : : representing case labels, or NULL_TREE for a `default' label.
5197 : : Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5198 : : K2, and 0 if K1 and K2 are equal. */
5199 : :
5200 : : int
5201 : 27904405 : case_compare (splay_tree_key k1, splay_tree_key k2)
5202 : : {
5203 : : /* Consider a NULL key (such as arises with a `default' label) to be
5204 : : smaller than anything else. */
5205 : 27904405 : if (!k1)
5206 : 3780627 : return k2 ? -1 : 0;
5207 : 24805086 : else if (!k2)
5208 : : return k1 ? 1 : 0;
5209 : :
5210 : 23987529 : return tree_int_cst_compare ((tree) k1, (tree) k2);
5211 : : }
5212 : :
5213 : : /* Process a case label, located at LOC, for the range LOW_VALUE
5214 : : ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5215 : : then this case label is actually a `default' label. If only
5216 : : HIGH_VALUE is NULL_TREE, then case label was declared using the
5217 : : usual C/C++ syntax, rather than the GNU case range extension.
5218 : : CASES is a tree containing all the case ranges processed so far;
5219 : : COND is the condition for the switch-statement itself.
5220 : : Returns the CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no
5221 : : CASE_LABEL_EXPR is created. ATTRS are the attributes to be applied
5222 : : to the label. */
5223 : :
5224 : : tree
5225 : 3048346 : c_add_case_label (location_t loc, splay_tree cases, tree cond,
5226 : : tree low_value, tree high_value, tree attrs)
5227 : : {
5228 : 3048346 : tree type;
5229 : 3048346 : tree label;
5230 : 3048346 : tree case_label;
5231 : 3048346 : splay_tree_node node;
5232 : :
5233 : : /* Create the LABEL_DECL itself. */
5234 : 3048346 : label = create_artificial_label (loc);
5235 : 3048346 : decl_attributes (&label, attrs, 0);
5236 : :
5237 : : /* If there was an error processing the switch condition, bail now
5238 : : before we get more confused. */
5239 : 3048346 : if (!cond || cond == error_mark_node)
5240 : 0 : goto error_out;
5241 : :
5242 : 2740985 : if ((low_value && TREE_TYPE (low_value)
5243 : 2740985 : && POINTER_TYPE_P (TREE_TYPE (low_value)))
5244 : 5789330 : || (high_value && TREE_TYPE (high_value)
5245 : 440 : && POINTER_TYPE_P (TREE_TYPE (high_value))))
5246 : : {
5247 : 1 : error_at (loc, "pointers are not permitted as case values");
5248 : 1 : goto error_out;
5249 : : }
5250 : :
5251 : : /* Case ranges are a GNU extension. */
5252 : 3048345 : if (high_value)
5253 : : {
5254 : 440 : if (c_dialect_cxx ())
5255 : 126 : pedwarn (loc, OPT_Wpedantic,
5256 : : "range expressions in switch statements are non-standard");
5257 : 314 : else if (warn_c23_c2y_compat > 0)
5258 : : {
5259 : 13 : if (pedantic && !flag_isoc2y)
5260 : 0 : pedwarn (loc, OPT_Wc23_c2y_compat,
5261 : : "ISO C does not support range expressions in switch "
5262 : : "statements before C2Y");
5263 : : else
5264 : 13 : warning_at (loc, OPT_Wc23_c2y_compat,
5265 : : "ISO C does not support range expressions in switch "
5266 : : "statements before C2Y");
5267 : : }
5268 : 301 : else if (warn_c23_c2y_compat && pedantic && !flag_isoc2y)
5269 : 28 : pedwarn (loc, OPT_Wpedantic,
5270 : : "ISO C does not support range expressions in switch "
5271 : : "statements before C2Y");
5272 : : }
5273 : :
5274 : 3048345 : type = TREE_TYPE (cond);
5275 : 3048345 : if (low_value)
5276 : : {
5277 : 2740984 : low_value = check_case_value (loc, low_value);
5278 : 2740984 : tree tem = NULL_TREE;
5279 : 2740984 : if (high_value
5280 : 440 : && !c_dialect_cxx ()
5281 : 314 : && low_value != error_mark_node
5282 : 313 : && !int_fits_type_p (low_value, type)
5283 : 2741424 : && pedwarn (loc, OPT_Wpedantic,
5284 : : "conversion of %qE to %qT in range expression changes "
5285 : : "value to %qE", low_value, type,
5286 : 20 : (tem = fold_convert (type, low_value))))
5287 : 20 : low_value = tem;
5288 : 2740984 : low_value = convert_and_check (loc, type, low_value);
5289 : 2740984 : low_value = fold (low_value);
5290 : 2740984 : if (low_value == error_mark_node)
5291 : 147 : goto error_out;
5292 : : }
5293 : 3048198 : if (high_value)
5294 : : {
5295 : 439 : high_value = check_case_value (loc, high_value);
5296 : 439 : tree tem = NULL_TREE;
5297 : 439 : if (!c_dialect_cxx ()
5298 : 313 : && high_value != error_mark_node
5299 : 312 : && !int_fits_type_p (high_value, type)
5300 : 752 : && pedwarn (loc, OPT_Wpedantic,
5301 : : "conversion of %qE to %qT in range expression changes "
5302 : : "value to %qE", high_value, type,
5303 : 20 : (tem = fold_convert (type, high_value))))
5304 : 20 : high_value = tem;
5305 : 439 : high_value = convert_and_check (loc, type, high_value);
5306 : 439 : high_value = fold (high_value);
5307 : 439 : if (high_value == error_mark_node)
5308 : 4 : goto error_out;
5309 : : }
5310 : :
5311 : 3048194 : if (low_value && high_value)
5312 : : {
5313 : : /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5314 : : really a case range, even though it was written that way.
5315 : : Remove the HIGH_VALUE to simplify later processing. */
5316 : 435 : if (tree_int_cst_equal (low_value, high_value))
5317 : : high_value = NULL_TREE;
5318 : 416 : else if (!tree_int_cst_lt (low_value, high_value))
5319 : : {
5320 : 28 : warning_at (loc, 0, "empty range specified");
5321 : 28 : goto error_out;
5322 : : }
5323 : : }
5324 : :
5325 : : /* Look up the LOW_VALUE in the table of case labels we already
5326 : : have. */
5327 : 3048166 : node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5328 : : /* If there was not an exact match, check for overlapping ranges.
5329 : : There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5330 : : that's a `default' label and the only overlap is an exact match. */
5331 : 3048166 : if (!node && (low_value || high_value))
5332 : : {
5333 : 2740787 : splay_tree_node low_bound;
5334 : 2740787 : splay_tree_node high_bound;
5335 : :
5336 : : /* Even though there wasn't an exact match, there might be an
5337 : : overlap between this case range and another case range.
5338 : : Since we've (inductively) not allowed any overlapping case
5339 : : ranges, we simply need to find the greatest low case label
5340 : : that is smaller that LOW_VALUE, and the smallest low case
5341 : : label that is greater than LOW_VALUE. If there is an overlap
5342 : : it will occur in one of these two ranges. */
5343 : 2740787 : low_bound = splay_tree_predecessor (cases,
5344 : : (splay_tree_key) low_value);
5345 : 2740787 : high_bound = splay_tree_successor (cases,
5346 : : (splay_tree_key) low_value);
5347 : :
5348 : : /* Check to see if the LOW_BOUND overlaps. It is smaller than
5349 : : the LOW_VALUE, so there is no need to check unless the
5350 : : LOW_BOUND is in fact itself a case range. */
5351 : 2740787 : if (low_bound
5352 : 1848974 : && CASE_HIGH ((tree) low_bound->value)
5353 : 2740954 : && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5354 : : low_value) >= 0)
5355 : : node = low_bound;
5356 : : /* Check to see if the HIGH_BOUND overlaps. The low end of that
5357 : : range is bigger than the low end of the current range, so we
5358 : : are only interested if the current range is a real range, and
5359 : : not an ordinary case label. */
5360 : 2740775 : else if (high_bound
5361 : 2740775 : && high_value
5362 : 2740775 : && (tree_int_cst_compare ((tree) high_bound->key,
5363 : : high_value)
5364 : : <= 0))
5365 : : node = high_bound;
5366 : : }
5367 : : /* If there was an overlap, issue an error. */
5368 : 3048147 : if (node)
5369 : : {
5370 : 37 : tree duplicate = CASE_LABEL ((tree) node->value);
5371 : :
5372 : 37 : if (high_value)
5373 : : {
5374 : 10 : error_at (loc, "duplicate (or overlapping) case value");
5375 : 10 : inform (DECL_SOURCE_LOCATION (duplicate),
5376 : : "this is the first entry overlapping that value");
5377 : : }
5378 : 27 : else if (low_value)
5379 : : {
5380 : 26 : error_at (loc, "duplicate case value") ;
5381 : 26 : inform (DECL_SOURCE_LOCATION (duplicate), "previously used here");
5382 : : }
5383 : : else
5384 : : {
5385 : 1 : error_at (loc, "multiple default labels in one switch");
5386 : 1 : inform (DECL_SOURCE_LOCATION (duplicate),
5387 : : "this is the first default label");
5388 : : }
5389 : 37 : goto error_out;
5390 : : }
5391 : :
5392 : : /* Add a CASE_LABEL to the statement-tree. */
5393 : 3048129 : case_label = add_stmt (build_case_label (low_value, high_value, label));
5394 : : /* Register this case label in the splay tree. */
5395 : 3048129 : splay_tree_insert (cases,
5396 : : (splay_tree_key) low_value,
5397 : : (splay_tree_value) case_label);
5398 : :
5399 : 3048129 : return case_label;
5400 : :
5401 : 217 : error_out:
5402 : : /* Add a label so that the back-end doesn't think that the beginning of
5403 : : the switch is unreachable. Note that we do not add a case label, as
5404 : : that just leads to duplicates and thence to failure later on. */
5405 : 217 : if (!cases->root)
5406 : : {
5407 : 153 : tree t = create_artificial_label (loc);
5408 : 153 : add_stmt (build_stmt (loc, LABEL_EXPR, t));
5409 : : }
5410 : 217 : return error_mark_node;
5411 : : }
5412 : :
5413 : : /* Subroutine of c_switch_covers_all_cases_p, called via
5414 : : splay_tree_foreach. Return 1 if it doesn't cover all the cases.
5415 : : ARGS[0] is initially NULL and after the first iteration is the
5416 : : so far highest case label. ARGS[1] is the minimum of SWITCH_COND's
5417 : : type. */
5418 : :
5419 : : static int
5420 : 134668 : c_switch_covers_all_cases_p_1 (splay_tree_node node, void *data)
5421 : : {
5422 : 134668 : tree label = (tree) node->value;
5423 : 134668 : tree *args = (tree *) data;
5424 : :
5425 : : /* If there is a default case, we shouldn't have called this. */
5426 : 134668 : gcc_assert (CASE_LOW (label));
5427 : :
5428 : 134668 : if (args[0] == NULL_TREE)
5429 : : {
5430 : 47186 : if (wi::to_widest (args[1]) < wi::to_widest (CASE_LOW (label)))
5431 : : return 1;
5432 : : }
5433 : 87482 : else if (wi::add (wi::to_widest (args[0]), 1)
5434 : 174964 : != wi::to_widest (CASE_LOW (label)))
5435 : : return 1;
5436 : 116389 : if (CASE_HIGH (label))
5437 : 109 : args[0] = CASE_HIGH (label);
5438 : : else
5439 : 116280 : args[0] = CASE_LOW (label);
5440 : : return 0;
5441 : : }
5442 : :
5443 : : /* Return true if switch with CASES and switch condition with type
5444 : : covers all possible values in the case labels. */
5445 : :
5446 : : bool
5447 : 80971 : c_switch_covers_all_cases_p (splay_tree cases, tree type)
5448 : : {
5449 : : /* If there is default:, this is always the case. */
5450 : 80971 : splay_tree_node default_node
5451 : 80971 : = splay_tree_lookup (cases, (splay_tree_key) NULL);
5452 : 80971 : if (default_node)
5453 : : return true;
5454 : :
5455 : 47732 : if (!INTEGRAL_TYPE_P (type))
5456 : : return false;
5457 : :
5458 : 47665 : tree args[2] = { NULL_TREE, TYPE_MIN_VALUE (type) };
5459 : 47665 : if (splay_tree_foreach (cases, c_switch_covers_all_cases_p_1, args))
5460 : : return false;
5461 : :
5462 : : /* If there are no cases at all, or if the highest case label
5463 : : is smaller than TYPE_MAX_VALUE, return false. */
5464 : 29386 : if (args[0] == NULL_TREE
5465 : 29386 : || wi::to_widest (args[0]) < wi::to_widest (TYPE_MAX_VALUE (type)))
5466 : 29340 : return false;
5467 : :
5468 : : return true;
5469 : : }
5470 : :
5471 : : /* Return true if stmt can fall through. Used by block_may_fallthru
5472 : : default case. */
5473 : :
5474 : : bool
5475 : 1779803 : c_block_may_fallthru (const_tree stmt)
5476 : : {
5477 : 1779803 : switch (TREE_CODE (stmt))
5478 : : {
5479 : 277 : case SWITCH_STMT:
5480 : 277 : return (!SWITCH_STMT_ALL_CASES_P (stmt)
5481 : 184 : || !SWITCH_STMT_NO_BREAK_P (stmt)
5482 : 305 : || block_may_fallthru (SWITCH_STMT_BODY (stmt)));
5483 : :
5484 : : default:
5485 : : return true;
5486 : : }
5487 : : }
5488 : :
5489 : : /* Finish an expression taking the address of LABEL (an
5490 : : IDENTIFIER_NODE). Returns an expression for the address.
5491 : :
5492 : : LOC is the location for the expression returned. */
5493 : :
5494 : : tree
5495 : 2122 : finish_label_address_expr (tree label, location_t loc)
5496 : : {
5497 : 2122 : tree result;
5498 : :
5499 : 2122 : pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
5500 : :
5501 : 2122 : if (label == error_mark_node)
5502 : : return error_mark_node;
5503 : :
5504 : 2116 : label = lookup_label (label);
5505 : 2116 : if (label == NULL_TREE)
5506 : 5 : result = null_pointer_node;
5507 : : else
5508 : : {
5509 : 2111 : TREE_USED (label) = 1;
5510 : 2111 : result = build1 (ADDR_EXPR, ptr_type_node, label);
5511 : : /* The current function is not necessarily uninlinable.
5512 : : Computed gotos are incompatible with inlining, but the value
5513 : : here could be used only in a diagnostic, for example. */
5514 : 2111 : protected_set_expr_location (result, loc);
5515 : : }
5516 : :
5517 : : return result;
5518 : : }
5519 : :
5520 : :
5521 : : /* Given a boolean expression ARG, return a tree representing an increment
5522 : : or decrement (as indicated by CODE) of ARG. The front end must check for
5523 : : invalid cases (e.g., decrement in C++). */
5524 : : tree
5525 : 85 : boolean_increment (enum tree_code code, tree arg)
5526 : : {
5527 : 85 : tree val;
5528 : 85 : tree true_res = build_int_cst (TREE_TYPE (arg), 1);
5529 : :
5530 : 85 : arg = stabilize_reference (arg);
5531 : 85 : switch (code)
5532 : : {
5533 : 28 : case PREINCREMENT_EXPR:
5534 : 28 : val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5535 : 28 : break;
5536 : 29 : case POSTINCREMENT_EXPR:
5537 : 29 : val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5538 : 29 : arg = save_expr (arg);
5539 : 29 : val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5540 : 29 : val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5541 : 29 : break;
5542 : 14 : case PREDECREMENT_EXPR:
5543 : 14 : val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5544 : : invert_truthvalue_loc (input_location, arg));
5545 : 14 : break;
5546 : 14 : case POSTDECREMENT_EXPR:
5547 : 14 : val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5548 : : invert_truthvalue_loc (input_location, arg));
5549 : 14 : arg = save_expr (arg);
5550 : 14 : val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5551 : 14 : val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5552 : 14 : break;
5553 : 0 : default:
5554 : 0 : gcc_unreachable ();
5555 : : }
5556 : 85 : TREE_SIDE_EFFECTS (val) = 1;
5557 : 85 : return val;
5558 : : }
5559 : :
5560 : : /* Built-in macros for stddef.h and stdint.h, that require macros
5561 : : defined in this file. */
5562 : : void
5563 : 209370 : c_stddef_cpp_builtins(void)
5564 : : {
5565 : 214811 : builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
5566 : 214811 : builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
5567 : 214811 : builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
5568 : : /* C++ has wchar_t as a builtin type, C doesn't, so if WINT_TYPE
5569 : : maps to wchar_t, define it to the underlying WCHAR_TYPE in C, and
5570 : : to wchar_t in C++, so the desired type equivalence holds. */
5571 : 209370 : if (!c_dialect_cxx ()
5572 : : && strcmp (WINT_TYPE, "wchar_t") == 0)
5573 : : builtin_define_with_value ("__WINT_TYPE__", WCHAR_TYPE, 0);
5574 : : else
5575 : 209370 : builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
5576 : 214811 : builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
5577 : 214811 : builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
5578 : 209370 : if (flag_char8_t)
5579 : 130436 : builtin_define_with_value ("__CHAR8_TYPE__", CHAR8_TYPE, 0);
5580 : 209370 : builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
5581 : 209370 : builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
5582 : 209370 : if (SIG_ATOMIC_TYPE)
5583 : 209370 : builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
5584 : 209370 : if (INT8_TYPE)
5585 : 209370 : builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
5586 : 209370 : if (INT16_TYPE)
5587 : 209370 : builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
5588 : 209370 : if (INT32_TYPE)
5589 : 209370 : builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
5590 : 209370 : if (INT64_TYPE)
5591 : 214811 : builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
5592 : 209370 : if (UINT8_TYPE)
5593 : 209370 : builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
5594 : 209370 : if (UINT16_TYPE)
5595 : 209370 : builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
5596 : 209370 : if (UINT32_TYPE)
5597 : 209370 : builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
5598 : 209370 : if (UINT64_TYPE)
5599 : 214811 : builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
5600 : 209370 : if (INT_LEAST8_TYPE)
5601 : 209370 : builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
5602 : 209370 : if (INT_LEAST16_TYPE)
5603 : 209370 : builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
5604 : 209370 : if (INT_LEAST32_TYPE)
5605 : 209370 : builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
5606 : 209370 : if (INT_LEAST64_TYPE)
5607 : 214811 : builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
5608 : 209370 : if (UINT_LEAST8_TYPE)
5609 : 209370 : builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
5610 : 209370 : if (UINT_LEAST16_TYPE)
5611 : 209370 : builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
5612 : 209370 : if (UINT_LEAST32_TYPE)
5613 : 209370 : builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
5614 : 209370 : if (UINT_LEAST64_TYPE)
5615 : 214811 : builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
5616 : 209370 : if (INT_FAST8_TYPE)
5617 : 209370 : builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
5618 : 209370 : if (INT_FAST16_TYPE)
5619 : 214811 : builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
5620 : 209370 : if (INT_FAST32_TYPE)
5621 : 214811 : builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
5622 : 209370 : if (INT_FAST64_TYPE)
5623 : 214811 : builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
5624 : 209370 : if (UINT_FAST8_TYPE)
5625 : 209370 : builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
5626 : 209370 : if (UINT_FAST16_TYPE)
5627 : 214811 : builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
5628 : 209370 : if (UINT_FAST32_TYPE)
5629 : 214811 : builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
5630 : 209370 : if (UINT_FAST64_TYPE)
5631 : 214811 : builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
5632 : 209370 : if (INTPTR_TYPE)
5633 : 214811 : builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
5634 : 209370 : if (UINTPTR_TYPE)
5635 : 214811 : builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
5636 : : /* GIMPLE FE testcases need access to the GCC internal 'sizetype'.
5637 : : Expose it as __SIZETYPE__. */
5638 : 209370 : if (flag_gimple)
5639 : 293 : builtin_define_with_value ("__SIZETYPE__", SIZETYPE, 0);
5640 : 209370 : }
5641 : :
5642 : : static void
5643 : 209898 : c_init_attributes (void)
5644 : : {
5645 : : /* Fill in the built_in_attributes array. */
5646 : : #define DEF_ATTR_NULL_TREE(ENUM) \
5647 : : built_in_attributes[(int) ENUM] = NULL_TREE;
5648 : : #define DEF_ATTR_INT(ENUM, VALUE) \
5649 : : built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
5650 : : #define DEF_ATTR_STRING(ENUM, VALUE) \
5651 : : built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
5652 : : #define DEF_ATTR_IDENT(ENUM, STRING) \
5653 : : built_in_attributes[(int) ENUM] = get_identifier (STRING);
5654 : : #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5655 : : built_in_attributes[(int) ENUM] \
5656 : : = tree_cons (built_in_attributes[(int) PURPOSE], \
5657 : : built_in_attributes[(int) VALUE], \
5658 : : built_in_attributes[(int) CHAIN]);
5659 : : #include "builtin-attrs.def"
5660 : : #undef DEF_ATTR_NULL_TREE
5661 : : #undef DEF_ATTR_INT
5662 : : #undef DEF_ATTR_IDENT
5663 : : #undef DEF_ATTR_TREE_LIST
5664 : 209898 : }
5665 : :
5666 : : /* Check whether the byte alignment ALIGN is a valid user-specified
5667 : : alignment less than the supported maximum. If so, return ALIGN's
5668 : : base-2 log; if not, output an error and return -1. If OBJFILE
5669 : : then reject alignments greater than MAX_OFILE_ALIGNMENT when
5670 : : converted to bits. Otherwise, consider valid only alignments
5671 : : that are less than HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT.
5672 : : Zero is not considered a valid argument (and results in -1 on
5673 : : return) but it only triggers a warning when WARN_ZERO is set. */
5674 : :
5675 : : int
5676 : 576626 : check_user_alignment (const_tree align, bool objfile, bool warn_zero)
5677 : : {
5678 : 576626 : if (error_operand_p (align))
5679 : : return -1;
5680 : :
5681 : 576603 : if (TREE_CODE (align) != INTEGER_CST
5682 : 576603 : || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
5683 : : {
5684 : 27 : error ("requested alignment is not an integer constant");
5685 : 27 : return -1;
5686 : : }
5687 : :
5688 : 576576 : if (integer_zerop (align))
5689 : : {
5690 : 75 : if (warn_zero)
5691 : 63 : warning (OPT_Wattributes,
5692 : : "requested alignment %qE is not a positive power of 2",
5693 : : align);
5694 : 75 : return -1;
5695 : : }
5696 : :
5697 : : /* Log2 of the byte alignment ALIGN. */
5698 : 576501 : int log2align;
5699 : 576501 : if (tree_int_cst_sgn (align) == -1
5700 : 576501 : || (log2align = tree_log2 (align)) == -1)
5701 : : {
5702 : 36 : error ("requested alignment %qE is not a positive power of 2",
5703 : : align);
5704 : 36 : return -1;
5705 : : }
5706 : :
5707 : 576465 : if (objfile)
5708 : : {
5709 : 15653 : unsigned maxalign = MAX_OFILE_ALIGNMENT / BITS_PER_UNIT;
5710 : 15653 : if (!tree_fits_uhwi_p (align) || tree_to_uhwi (align) > maxalign)
5711 : : {
5712 : 10 : error ("requested alignment %qE exceeds object file maximum %u",
5713 : : align, maxalign);
5714 : 10 : return -1;
5715 : : }
5716 : : }
5717 : :
5718 : 576455 : if (log2align >= HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT)
5719 : : {
5720 : 17 : error ("requested alignment %qE exceeds maximum %u",
5721 : : align, 1U << (HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT - 1));
5722 : 17 : return -1;
5723 : : }
5724 : :
5725 : : return log2align;
5726 : : }
5727 : :
5728 : : /* Determine the ELF symbol visibility for DECL, which is either a
5729 : : variable or a function. It is an error to use this function if a
5730 : : definition of DECL is not available in this translation unit.
5731 : : Returns true if the final visibility has been determined by this
5732 : : function; false if the caller is free to make additional
5733 : : modifications. */
5734 : :
5735 : : bool
5736 : 51794681 : c_determine_visibility (tree decl)
5737 : : {
5738 : 51794681 : gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
5739 : :
5740 : : /* If the user explicitly specified the visibility with an
5741 : : attribute, honor that. DECL_VISIBILITY will have been set during
5742 : : the processing of the attribute. We check for an explicit
5743 : : attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
5744 : : to distinguish the use of an attribute from the use of a "#pragma
5745 : : GCC visibility push(...)"; in the latter case we still want other
5746 : : considerations to be able to overrule the #pragma. */
5747 : 51794681 : if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
5748 : : || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
5749 : : && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
5750 : : || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
5751 : : return true;
5752 : :
5753 : : /* Set default visibility to whatever the user supplied with
5754 : : visibility_specified depending on #pragma GCC visibility. */
5755 : 51162314 : if (!DECL_VISIBILITY_SPECIFIED (decl))
5756 : : {
5757 : 51161068 : if (visibility_options.inpragma
5758 : 51161068 : || DECL_VISIBILITY (decl) != default_visibility)
5759 : : {
5760 : 212390 : DECL_VISIBILITY (decl) = default_visibility;
5761 : 212390 : DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
5762 : : /* If visibility changed and DECL already has DECL_RTL, ensure
5763 : : symbol flags are updated. */
5764 : 14336 : if (((VAR_P (decl) && TREE_STATIC (decl))
5765 : 212338 : || TREE_CODE (decl) == FUNCTION_DECL)
5766 : 410496 : && DECL_RTL_SET_P (decl))
5767 : 0 : make_decl_rtl (decl);
5768 : : }
5769 : : }
5770 : : return false;
5771 : : }
5772 : :
5773 : : /* Data to communicate through check_function_arguments_recurse between
5774 : : check_function_nonnull and check_nonnull_arg. */
5775 : :
5776 : : struct nonnull_arg_ctx
5777 : : {
5778 : : /* Location of the call. */
5779 : : location_t loc;
5780 : : /* The function whose arguments are being checked and its type (used
5781 : : for calls through function pointers). */
5782 : : const_tree fndecl, fntype;
5783 : : /* For nonnull_if_nonzero, index of the other arguments. */
5784 : : unsigned HOST_WIDE_INT other1, other2;
5785 : : /* True if a warning has been issued. */
5786 : : bool warned_p;
5787 : : };
5788 : :
5789 : : /* Check the argument list of a function call to CTX.FNDECL of CTX.FNTYPE
5790 : : for null in argument slots that are marked as requiring a non-null
5791 : : pointer argument. The NARGS arguments are passed in the array ARGARRAY.
5792 : : Return true if we have warned. */
5793 : :
5794 : : static bool
5795 : 6404612 : check_function_nonnull (nonnull_arg_ctx &ctx, int nargs, tree *argarray)
5796 : : {
5797 : 6404612 : int firstarg = 0;
5798 : 6404612 : if (TREE_CODE (ctx.fntype) == METHOD_TYPE)
5799 : : {
5800 : 475603 : bool closure = false;
5801 : 475603 : if (ctx.fndecl)
5802 : : {
5803 : : /* For certain lambda expressions the C++ front end emits calls
5804 : : that pass a null this pointer as an argument named __closure
5805 : : to the member operator() of empty function. Detect those
5806 : : and avoid checking them, but proceed to check the remaining
5807 : : arguments. */
5808 : 475452 : tree arg0 = DECL_ARGUMENTS (ctx.fndecl);
5809 : 475452 : if (tree arg0name = DECL_NAME (arg0))
5810 : 475452 : closure = id_equal (arg0name, "__closure");
5811 : : }
5812 : :
5813 : : /* In calls to C++ non-static member functions check the this
5814 : : pointer regardless of whether the function is declared with
5815 : : attribute nonnull. */
5816 : 946404 : firstarg = 1;
5817 : 475452 : if (!closure)
5818 : 470952 : check_function_arguments_recurse (check_nonnull_arg, &ctx, argarray[0],
5819 : : firstarg, OPT_Wnonnull);
5820 : : }
5821 : :
5822 : 6404612 : tree attrs = lookup_attribute ("nonnull", TYPE_ATTRIBUTES (ctx.fntype));
5823 : :
5824 : 6404612 : tree a = attrs;
5825 : : /* See if any of the nonnull attributes has no arguments. If so,
5826 : : then every pointer argument is checked (in which case the check
5827 : : for pointer type is done in check_nonnull_arg). */
5828 : 13040362 : while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE)
5829 : 102690 : a = lookup_attribute ("nonnull", TREE_CHAIN (a));
5830 : :
5831 : 6404612 : if (a != NULL_TREE)
5832 : 68517 : for (int i = firstarg; i < nargs; i++)
5833 : 42759 : check_function_arguments_recurse (check_nonnull_arg, &ctx, argarray[i],
5834 : 42759 : i + 1, OPT_Wnonnull);
5835 : 6378854 : else if (attrs)
5836 : : {
5837 : : /* Walk the argument list. If we encounter an argument number we
5838 : : should check for non-null, do it. */
5839 : 345032 : for (int i = firstarg; i < nargs; i++)
5840 : : {
5841 : 146810 : for (a = attrs; ; a = TREE_CHAIN (a))
5842 : : {
5843 : 399736 : a = lookup_attribute ("nonnull", a);
5844 : 399736 : if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
5845 : : break;
5846 : : }
5847 : :
5848 : 252926 : if (a != NULL_TREE)
5849 : 123392 : check_function_arguments_recurse (check_nonnull_arg, &ctx,
5850 : 123392 : argarray[i], i + 1,
5851 : : OPT_Wnonnull);
5852 : 252926 : a = NULL_TREE;
5853 : : }
5854 : : }
5855 : 6404612 : if (a == NULL_TREE)
5856 : 6378854 : for (attrs = TYPE_ATTRIBUTES (ctx.fntype);
5857 : 6435878 : (attrs = lookup_attribute ("nonnull_if_nonzero", attrs));
5858 : 57024 : attrs = TREE_CHAIN (attrs))
5859 : : {
5860 : 57024 : tree args = TREE_VALUE (attrs);
5861 : 57024 : unsigned int idx = TREE_INT_CST_LOW (TREE_VALUE (args)) - 1;
5862 : 57024 : unsigned int idx2
5863 : 57024 : = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args))) - 1;
5864 : 57024 : unsigned int idx3 = idx2;
5865 : 57024 : if (tree chain2 = TREE_CHAIN (TREE_CHAIN (args)))
5866 : 479 : idx3 = TREE_INT_CST_LOW (TREE_VALUE (chain2)) - 1;
5867 : 57024 : if (idx < (unsigned) nargs - firstarg
5868 : 57019 : && idx2 < (unsigned) nargs - firstarg
5869 : 57014 : && idx3 < (unsigned) nargs - firstarg
5870 : 57014 : && INTEGRAL_TYPE_P (TREE_TYPE (argarray[firstarg + idx2]))
5871 : 56941 : && integer_nonzerop (argarray[firstarg + idx2])
5872 : 31732 : && INTEGRAL_TYPE_P (TREE_TYPE (argarray[firstarg + idx3]))
5873 : 88756 : && integer_nonzerop (argarray[firstarg + idx3]))
5874 : : {
5875 : 31640 : ctx.other1 = firstarg + idx2 + 1;
5876 : 31640 : ctx.other2 = firstarg + idx3 + 1;
5877 : 31640 : check_function_arguments_recurse (check_nonnull_arg, &ctx,
5878 : 31640 : argarray[firstarg + idx],
5879 : 31640 : firstarg + idx + 1,
5880 : : OPT_Wnonnull);
5881 : 31640 : ctx.other1 = 0;
5882 : 31640 : ctx.other2 = 0;
5883 : : }
5884 : : }
5885 : 6404612 : return ctx.warned_p;
5886 : : }
5887 : :
5888 : : /* Check that the Nth argument of a function call (counting backwards
5889 : : from the end) is a (pointer)0. The NARGS arguments are passed in the
5890 : : array ARGARRAY. */
5891 : :
5892 : : static void
5893 : 6390417 : check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
5894 : : {
5895 : 6390417 : tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
5896 : :
5897 : 6390417 : if (attr)
5898 : : {
5899 : 112 : int len = 0;
5900 : 112 : int pos = 0;
5901 : 112 : tree sentinel;
5902 : 112 : function_args_iterator iter;
5903 : 112 : tree t;
5904 : :
5905 : : /* Skip over the named arguments. */
5906 : 237 : FOREACH_FUNCTION_ARGS (fntype, t, iter)
5907 : : {
5908 : 125 : if (len == nargs)
5909 : : break;
5910 : 125 : len++;
5911 : : }
5912 : :
5913 : 112 : if (TREE_VALUE (attr))
5914 : : {
5915 : 59 : tree p = TREE_VALUE (TREE_VALUE (attr));
5916 : 59 : pos = TREE_INT_CST_LOW (p);
5917 : : }
5918 : :
5919 : : /* The sentinel must be one of the varargs, i.e.
5920 : : in position >= the number of fixed arguments. */
5921 : 112 : if ((nargs - 1 - pos) < len)
5922 : : {
5923 : 22 : warning (OPT_Wformat_,
5924 : : "not enough variable arguments to fit a sentinel");
5925 : 22 : return;
5926 : : }
5927 : :
5928 : : /* Validate the sentinel. */
5929 : 90 : sentinel = fold_for_warn (argarray[nargs - 1 - pos]);
5930 : 138 : if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
5931 : 42 : || !integer_zerop (sentinel))
5932 : 56 : && TREE_CODE (TREE_TYPE (sentinel)) != NULLPTR_TYPE
5933 : : /* Although __null (in C++) is only an integer we allow it
5934 : : nevertheless, as we are guaranteed that it's exactly
5935 : : as wide as a pointer, and we don't want to force
5936 : : users to cast the NULL they have written there.
5937 : : We warn with -Wstrict-null-sentinel, though. */
5938 : 138 : && (warn_strict_null_sentinel || null_node != sentinel))
5939 : 45 : warning (OPT_Wformat_, "missing sentinel in function call");
5940 : : }
5941 : : }
5942 : :
5943 : : /* Check that the same argument isn't passed to two or more
5944 : : restrict-qualified formal and issue a -Wrestrict warning
5945 : : if it is. Return true if a warning has been issued. */
5946 : :
5947 : : static bool
5948 : 53219783 : check_function_restrict (const_tree fndecl, const_tree fntype,
5949 : : int nargs, tree *unfolded_argarray)
5950 : : {
5951 : 53219783 : int i;
5952 : 53219783 : tree parms = TYPE_ARG_TYPES (fntype);
5953 : :
5954 : : /* Call fold_for_warn on all of the arguments. */
5955 : 53219783 : auto_vec<tree> argarray (nargs);
5956 : 239869720 : for (i = 0; i < nargs; i++)
5957 : 133430154 : argarray.quick_push (fold_for_warn (unfolded_argarray[i]));
5958 : :
5959 : 53219783 : if (fndecl
5960 : 52688405 : && TREE_CODE (fndecl) == FUNCTION_DECL)
5961 : : {
5962 : : /* Avoid diagnosing calls built-ins with a zero size/bound
5963 : : here. They are checked in more detail elsewhere. */
5964 : 52688405 : if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
5965 : 1715634 : && nargs == 3
5966 : 326620 : && TREE_CODE (argarray[2]) == INTEGER_CST
5967 : 52859133 : && integer_zerop (argarray[2]))
5968 : : return false;
5969 : :
5970 : 52662795 : if (DECL_ARGUMENTS (fndecl))
5971 : 53194173 : parms = DECL_ARGUMENTS (fndecl);
5972 : : }
5973 : :
5974 : 186547497 : for (i = 0; i < nargs; i++)
5975 : 133353324 : TREE_VISITED (argarray[i]) = 0;
5976 : :
5977 : : bool warned = false;
5978 : :
5979 : 185667669 : for (i = 0; i < nargs && parms && parms != void_list_node; i++)
5980 : : {
5981 : 132473496 : tree type;
5982 : 132473496 : if (TREE_CODE (parms) == PARM_DECL)
5983 : : {
5984 : 15764189 : type = TREE_TYPE (parms);
5985 : 15764189 : parms = DECL_CHAIN (parms);
5986 : : }
5987 : : else
5988 : : {
5989 : 116709307 : type = TREE_VALUE (parms);
5990 : 116709307 : parms = TREE_CHAIN (parms);
5991 : : }
5992 : 132473496 : if (POINTER_TYPE_P (type)
5993 : 8021217 : && TYPE_RESTRICT (type)
5994 : 132723166 : && !TYPE_READONLY (TREE_TYPE (type)))
5995 : 250338 : warned |= warn_for_restrict (i, argarray.address (), nargs);
5996 : : }
5997 : :
5998 : 186547497 : for (i = 0; i < nargs; i++)
5999 : 133353324 : TREE_VISITED (argarray[i]) = 0;
6000 : :
6001 : : return warned;
6002 : 53219783 : }
6003 : :
6004 : : /* Helper for check_function_nonnull; given a list of operands which
6005 : : must be non-null in ARGS, determine if operand PARAM_NUM should be
6006 : : checked. */
6007 : :
6008 : : static bool
6009 : 270202 : nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
6010 : : {
6011 : 270202 : unsigned HOST_WIDE_INT arg_num = 0;
6012 : :
6013 : 716804 : for (; args; args = TREE_CHAIN (args))
6014 : : {
6015 : 569994 : bool found = get_attribute_operand (TREE_VALUE (args), &arg_num);
6016 : :
6017 : 569994 : gcc_assert (found);
6018 : :
6019 : 569994 : if (arg_num == param_num)
6020 : : return true;
6021 : : }
6022 : : return false;
6023 : : }
6024 : :
6025 : : /* Check that the function argument PARAM (which is operand number
6026 : : PARAM_NUM) is non-null. This is called by check_function_nonnull
6027 : : via check_function_arguments_recurse. */
6028 : :
6029 : : static void
6030 : 667751 : check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
6031 : : {
6032 : 667751 : struct nonnull_arg_ctx *pctx = (struct nonnull_arg_ctx *) ctx;
6033 : :
6034 : : /* Just skip checking the argument if it's not a pointer. This can
6035 : : happen if the "nonnull" attribute was given without an operand
6036 : : list (which means to check every pointer argument). */
6037 : :
6038 : 667751 : tree paramtype = TREE_TYPE (param);
6039 : 667751 : if (TREE_CODE (paramtype) != POINTER_TYPE
6040 : 60670 : && TREE_CODE (paramtype) != NULLPTR_TYPE)
6041 : 667002 : return;
6042 : :
6043 : : /* Diagnose the simple cases of null arguments. */
6044 : 607087 : if (!integer_zerop (fold_for_warn (param)))
6045 : : return;
6046 : :
6047 : 749 : auto_diagnostic_group adg;
6048 : :
6049 : 749 : const location_t loc = EXPR_LOC_OR_LOC (param, pctx->loc);
6050 : :
6051 : 749 : if (TREE_CODE (pctx->fntype) == METHOD_TYPE)
6052 : 109 : --param_num;
6053 : :
6054 : 749 : bool warned;
6055 : 749 : if (param_num == 0)
6056 : : {
6057 : 30 : warned = warning_at (loc, OPT_Wnonnull,
6058 : : "%qs pointer is null", "this");
6059 : 30 : if (warned && pctx->fndecl)
6060 : 27 : inform (DECL_SOURCE_LOCATION (pctx->fndecl),
6061 : : "in a call to non-static member function %qD",
6062 : : pctx->fndecl);
6063 : : }
6064 : : else
6065 : : {
6066 : 719 : if (pctx->other1 && pctx->other2 != pctx->other1)
6067 : 14 : warned = warning_at (loc, OPT_Wnonnull,
6068 : : "argument %u null where non-null expected "
6069 : : "because arguments %u and %u are nonzero",
6070 : : (unsigned) param_num,
6071 : : TREE_CODE (pctx->fntype) == METHOD_TYPE
6072 : 0 : ? (unsigned) pctx->other1 - 1
6073 : : : (unsigned) pctx->other1,
6074 : : TREE_CODE (pctx->fntype) == METHOD_TYPE
6075 : 0 : ? (unsigned) pctx->other2 - 1
6076 : : : (unsigned) pctx->other2);
6077 : 705 : else if (pctx->other1)
6078 : 50 : warned = warning_at (loc, OPT_Wnonnull,
6079 : : "argument %u null where non-null expected "
6080 : : "because argument %u is nonzero",
6081 : : (unsigned) param_num,
6082 : : TREE_CODE (pctx->fntype) == METHOD_TYPE
6083 : 0 : ? (unsigned) pctx->other1 - 1
6084 : : : (unsigned) pctx->other1);
6085 : : else
6086 : 655 : warned = warning_at (loc, OPT_Wnonnull,
6087 : : "argument %u null where non-null expected",
6088 : : (unsigned) param_num);
6089 : 719 : if (warned && pctx->fndecl)
6090 : 688 : inform (DECL_SOURCE_LOCATION (pctx->fndecl),
6091 : : "in a call to function %qD declared %qs",
6092 : : pctx->fndecl,
6093 : 688 : pctx->other1 ? "nonnull_if_nonzero" : "nonnull");
6094 : : }
6095 : :
6096 : 746 : if (warned)
6097 : 746 : pctx->warned_p = true;
6098 : 749 : }
6099 : :
6100 : : /* Helper for attribute handling; fetch the operand number from
6101 : : the attribute argument list. */
6102 : :
6103 : : bool
6104 : 569994 : get_attribute_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
6105 : : {
6106 : : /* Verify the arg number is a small constant. */
6107 : 569994 : if (tree_fits_uhwi_p (arg_num_expr))
6108 : : {
6109 : 569994 : *valp = tree_to_uhwi (arg_num_expr);
6110 : 569994 : return true;
6111 : : }
6112 : : else
6113 : : return false;
6114 : : }
6115 : :
6116 : : /* Arguments being collected for optimization. */
6117 : : typedef const char *const_char_p; /* For DEF_VEC_P. */
6118 : : static GTY(()) vec<const_char_p, va_gc> *optimize_args;
6119 : :
6120 : :
6121 : : /* Inner function to convert a TREE_LIST to argv string to parse the optimize
6122 : : options in ARGS. ATTR_P is true if this is for attribute(optimize), and
6123 : : false for #pragma GCC optimize. */
6124 : :
6125 : : bool
6126 : 343970 : parse_optimize_options (tree args, bool attr_p)
6127 : : {
6128 : 343970 : bool ret = true;
6129 : 343970 : unsigned opt_argc;
6130 : 343970 : unsigned i;
6131 : 343970 : const char **opt_argv;
6132 : 343970 : struct cl_decoded_option *decoded_options;
6133 : 343970 : unsigned int decoded_options_count;
6134 : 343970 : tree ap;
6135 : :
6136 : : /* Build up argv vector. Just in case the string is stored away, use garbage
6137 : : collected strings. */
6138 : 343970 : vec_safe_truncate (optimize_args, 0);
6139 : 343970 : vec_safe_push (optimize_args, (const char *) NULL);
6140 : :
6141 : 706639 : for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
6142 : : {
6143 : 362669 : tree value = TREE_VALUE (ap);
6144 : :
6145 : 362669 : if (TREE_CODE (value) == INTEGER_CST)
6146 : : {
6147 : 305 : char buffer[HOST_BITS_PER_LONG / 3 + 4];
6148 : 305 : sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
6149 : 305 : vec_safe_push (optimize_args, ggc_strdup (buffer));
6150 : : }
6151 : :
6152 : 362364 : else if (TREE_CODE (value) == STRING_CST)
6153 : : {
6154 : : /* Split string into multiple substrings. */
6155 : 362360 : size_t len = TREE_STRING_LENGTH (value);
6156 : 362360 : char *p = ASTRDUP (TREE_STRING_POINTER (value));
6157 : 362360 : char *end = p + len;
6158 : 362360 : char *comma;
6159 : 362360 : char *next_p = p;
6160 : :
6161 : 724769 : while (next_p != NULL)
6162 : : {
6163 : 362409 : size_t len2;
6164 : 362409 : char *q, *r;
6165 : :
6166 : 362409 : p = next_p;
6167 : 362409 : comma = strchr (p, ',');
6168 : 362409 : if (comma)
6169 : : {
6170 : 49 : len2 = comma - p;
6171 : 49 : *comma = '\0';
6172 : 49 : next_p = comma+1;
6173 : : }
6174 : : else
6175 : : {
6176 : 362360 : len2 = end - p;
6177 : 362360 : next_p = NULL;
6178 : : }
6179 : :
6180 : : /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
6181 : : options. */
6182 : 362409 : if (*p == '-' && p[1] != 'O' && p[1] != 'f')
6183 : : {
6184 : 0 : ret = false;
6185 : 0 : if (attr_p)
6186 : : {
6187 : 0 : auto_urlify_attributes sentinel;
6188 : 0 : warning (OPT_Wattributes,
6189 : : "bad option %qs to attribute %<optimize%>", p);
6190 : 0 : }
6191 : : else
6192 : 0 : warning (OPT_Wpragmas,
6193 : : "bad option %qs to pragma %<optimize%>", p);
6194 : 0 : continue;
6195 : 0 : }
6196 : :
6197 : : /* Can't use GC memory here, see PR88007. */
6198 : 362409 : r = q = XOBNEWVEC (&opts_obstack, char, len2 + 3);
6199 : :
6200 : 362409 : if (*p != '-')
6201 : : {
6202 : 42772 : *r++ = '-';
6203 : :
6204 : : /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
6205 : : itself is -Os, and any other switch begins with a -f. */
6206 : 42772 : if ((*p >= '0' && *p <= '9')
6207 : 42615 : || (p[0] == 's' && p[1] == '\0'))
6208 : 157 : *r++ = 'O';
6209 : 42615 : else if (*p != 'O')
6210 : 23322 : *r++ = 'f';
6211 : : }
6212 : :
6213 : 362409 : memcpy (r, p, len2);
6214 : 362409 : r[len2] = '\0';
6215 : 362409 : vec_safe_push (optimize_args, (const char *) q);
6216 : : }
6217 : :
6218 : : }
6219 : : }
6220 : :
6221 : 343970 : opt_argc = optimize_args->length ();
6222 : 343970 : opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
6223 : :
6224 : 706684 : for (i = 1; i < opt_argc; i++)
6225 : 362714 : opt_argv[i] = (*optimize_args)[i];
6226 : :
6227 : : /* Now parse the options. */
6228 : 343970 : decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
6229 : : &decoded_options,
6230 : : &decoded_options_count);
6231 : : /* Drop non-Optimization options. */
6232 : 343970 : unsigned j = 1;
6233 : 706684 : for (i = 1; i < decoded_options_count; ++i)
6234 : : {
6235 : 362714 : if (! (cl_options[decoded_options[i].opt_index].flags & CL_OPTIMIZATION))
6236 : : {
6237 : 3 : ret = false;
6238 : 3 : if (attr_p)
6239 : : {
6240 : 2 : auto_urlify_attributes sentinel;
6241 : 2 : warning (OPT_Wattributes,
6242 : : "bad option %qs to attribute %<optimize%>",
6243 : 2 : decoded_options[i].orig_option_with_args_text);
6244 : 2 : }
6245 : : else
6246 : 1 : warning (OPT_Wpragmas,
6247 : : "bad option %qs to pragma %<optimize%>",
6248 : : decoded_options[i].orig_option_with_args_text);
6249 : 3 : continue;
6250 : 3 : }
6251 : 362711 : if (i != j)
6252 : 0 : decoded_options[j] = decoded_options[i];
6253 : 362711 : j++;
6254 : : }
6255 : 343970 : decoded_options_count = j;
6256 : :
6257 : : /* Merge the decoded options with save_decoded_options. */
6258 : 343970 : unsigned save_opt_count = save_opt_decoded_options->length ();
6259 : 343970 : unsigned merged_decoded_options_count
6260 : : = save_opt_count + decoded_options_count;
6261 : 343970 : cl_decoded_option *merged_decoded_options
6262 : 343970 : = XNEWVEC (cl_decoded_option, merged_decoded_options_count);
6263 : :
6264 : : /* Note the first decoded_options is used for the program name. */
6265 : 1972668 : for (unsigned i = 0; i < save_opt_count; ++i)
6266 : 1628698 : merged_decoded_options[i + 1] = (*save_opt_decoded_options)[i];
6267 : 706681 : for (unsigned i = 1; i < decoded_options_count; ++i)
6268 : 362711 : merged_decoded_options[save_opt_count + i] = decoded_options[i];
6269 : :
6270 : : /* And apply them. */
6271 : 343970 : decode_options (&global_options, &global_options_set,
6272 : : merged_decoded_options, merged_decoded_options_count,
6273 : : input_location, global_dc, NULL);
6274 : 343970 : free (decoded_options);
6275 : :
6276 : 343970 : targetm.override_options_after_change();
6277 : :
6278 : 343970 : optimize_args->truncate (0);
6279 : 343970 : return ret;
6280 : : }
6281 : :
6282 : : /* Check whether ATTR is a valid attribute fallthrough. */
6283 : :
6284 : : bool
6285 : 270346651 : attribute_fallthrough_p (tree attr)
6286 : : {
6287 : 270346651 : if (attr == error_mark_node)
6288 : : return false;
6289 : 270346633 : tree t = lookup_attribute ("", "fallthrough", attr);
6290 : 270346633 : if (t == NULL_TREE)
6291 : : return false;
6292 : 18391 : auto_urlify_attributes sentinel;
6293 : : /* It is no longer true that "this attribute shall appear at most once in
6294 : : each attribute-list", but we still give a warning. */
6295 : 18391 : if (lookup_attribute ("", "fallthrough", TREE_CHAIN (t)))
6296 : 9 : warning (OPT_Wattributes, "attribute %<fallthrough%> specified multiple "
6297 : : "times");
6298 : : /* No attribute-argument-clause shall be present. */
6299 : 18382 : else if (TREE_VALUE (t) != NULL_TREE)
6300 : 4 : warning (OPT_Wattributes, "%<fallthrough%> attribute specified with "
6301 : : "a parameter");
6302 : : /* Warn if other attributes are found. */
6303 : 36808 : for (t = attr; t != NULL_TREE; t = TREE_CHAIN (t))
6304 : : {
6305 : 18417 : tree name = get_attribute_name (t);
6306 : 18417 : if (!is_attribute_p ("fallthrough", name)
6307 : 18417 : || !is_attribute_namespace_p ("", t))
6308 : : {
6309 : 17 : if (!c_dialect_cxx () && get_attribute_namespace (t) == NULL_TREE)
6310 : : /* The specifications of standard attributes in C mean
6311 : : this is a constraint violation. */
6312 : 3 : pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
6313 : : get_attribute_name (t));
6314 : : else
6315 : 14 : warning (OPT_Wattributes, "%qE attribute ignored", name);
6316 : : }
6317 : : }
6318 : 18391 : return true;
6319 : 18391 : }
6320 : :
6321 : :
6322 : : /* Check for valid arguments being passed to a function with FNTYPE.
6323 : : There are NARGS arguments in the array ARGARRAY. LOC should be used
6324 : : for diagnostics. Return true if either -Wnonnull or -Wrestrict has
6325 : : been issued.
6326 : :
6327 : : The arguments in ARGARRAY may not have been folded yet (e.g. for C++,
6328 : : to preserve location wrappers); checks that require folded arguments
6329 : : should call fold_for_warn on them.
6330 : :
6331 : : Use the frontend-supplied COMP_TYPES when determining if
6332 : : one type is a subclass of another. */
6333 : :
6334 : : bool
6335 : 53851275 : check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype,
6336 : : int nargs, tree *argarray, vec<location_t> *arglocs,
6337 : : bool (*comp_types) (tree, tree))
6338 : : {
6339 : 53851275 : bool warned_p = false;
6340 : :
6341 : 53851275 : if (c_inhibit_evaluation_warnings)
6342 : : return warned_p;
6343 : :
6344 : : /* Check for null being passed in a pointer argument that must be
6345 : : non-null. In C++, this includes the this pointer. We also need
6346 : : to do this if format checking is enabled. */
6347 : 53224516 : if (warn_nonnull)
6348 : : {
6349 : 6404612 : nonnull_arg_ctx ctx = { loc, fndecl, fntype, 0, 0, false };
6350 : 6404612 : warned_p = check_function_nonnull (ctx, nargs, argarray);
6351 : : }
6352 : :
6353 : : /* Check for errors in format strings. */
6354 : :
6355 : 53224516 : if (warn_format || warn_suggest_attribute_format)
6356 : 6467486 : check_function_format (fndecl ? fndecl : fntype, TYPE_ATTRIBUTES (fntype), nargs,
6357 : : argarray, arglocs, comp_types);
6358 : :
6359 : 53224516 : if (warn_format)
6360 : 6390417 : check_function_sentinel (fntype, nargs, argarray);
6361 : :
6362 : 53224516 : if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
6363 : : {
6364 : 1720367 : switch (DECL_FUNCTION_CODE (fndecl))
6365 : : {
6366 : : case BUILT_IN_SPRINTF:
6367 : : case BUILT_IN_SPRINTF_CHK:
6368 : : case BUILT_IN_SNPRINTF:
6369 : : case BUILT_IN_SNPRINTF_CHK:
6370 : : /* Let the sprintf pass handle these. */
6371 : : return warned_p;
6372 : :
6373 : : default:
6374 : : break;
6375 : : }
6376 : : }
6377 : :
6378 : : /* check_function_restrict sets the DECL_READ_P for arguments
6379 : : so it must be called unconditionally. */
6380 : 53219783 : warned_p |= check_function_restrict (fndecl, fntype, nargs, argarray);
6381 : :
6382 : 53219783 : return warned_p;
6383 : : }
6384 : :
6385 : : /* Generic argument checking recursion routine. PARAM is the argument to
6386 : : be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
6387 : : once the argument is resolved. CTX is context for the callback.
6388 : : OPT is the warning for which this is done. */
6389 : : void
6390 : 723739 : check_function_arguments_recurse (void (*callback)
6391 : : (void *, tree, unsigned HOST_WIDE_INT),
6392 : : void *ctx, tree param,
6393 : : unsigned HOST_WIDE_INT param_num,
6394 : : opt_code opt)
6395 : : {
6396 : 1134109 : if (opt != OPT_Wformat_ && warning_suppressed_p (param))
6397 : : return;
6398 : :
6399 : 772873 : if (CONVERT_EXPR_P (param)
6400 : 1251155 : && (TYPE_PRECISION (TREE_TYPE (param))
6401 : 478282 : == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
6402 : : {
6403 : : /* Strip coercion. */
6404 : 819210 : check_function_arguments_recurse (callback, ctx,
6405 : 409605 : TREE_OPERAND (param, 0), param_num,
6406 : : opt);
6407 : 409605 : return;
6408 : : }
6409 : :
6410 : 722803 : if (TREE_CODE (param) == CALL_EXPR && CALL_EXPR_FN (param))
6411 : : {
6412 : 8283 : tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
6413 : 8283 : tree attrs;
6414 : 8283 : bool found_format_arg = false;
6415 : :
6416 : : /* See if this is a call to a known internationalization function
6417 : : that modifies a format arg. Such a function may have multiple
6418 : : format_arg attributes (for example, ngettext). */
6419 : :
6420 : 8283 : for (attrs = TYPE_ATTRIBUTES (type);
6421 : 8665 : attrs;
6422 : 382 : attrs = TREE_CHAIN (attrs))
6423 : 382 : if (is_attribute_p ("format_arg", get_attribute_name (attrs)))
6424 : : {
6425 : 226 : tree inner_arg;
6426 : 226 : tree format_num_expr;
6427 : 226 : int format_num;
6428 : 226 : int i;
6429 : 226 : call_expr_arg_iterator iter;
6430 : :
6431 : : /* Extract the argument number, which was previously checked
6432 : : to be valid. */
6433 : 226 : format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
6434 : :
6435 : 226 : format_num = tree_to_uhwi (format_num_expr);
6436 : :
6437 : 226 : for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
6438 : 320 : inner_arg != NULL_TREE;
6439 : 94 : inner_arg = next_call_expr_arg (&iter), i++)
6440 : 320 : if (i == format_num)
6441 : : {
6442 : 226 : check_function_arguments_recurse (callback, ctx,
6443 : : inner_arg, param_num,
6444 : : opt);
6445 : 226 : found_format_arg = true;
6446 : 226 : break;
6447 : : }
6448 : : }
6449 : :
6450 : : /* If we found a format_arg attribute and did a recursive check,
6451 : : we are done with checking this argument. Otherwise, we continue
6452 : : and this will be considered a non-literal. */
6453 : 8283 : if (found_format_arg)
6454 : : return;
6455 : : }
6456 : :
6457 : 722625 : if (TREE_CODE (param) == COND_EXPR)
6458 : : {
6459 : : /* Simplify to avoid warning for an impossible case. */
6460 : 783 : param = fold_for_warn (param);
6461 : 783 : if (TREE_CODE (param) == COND_EXPR)
6462 : : {
6463 : : /* Check both halves of the conditional expression. */
6464 : 765 : check_function_arguments_recurse (callback, ctx,
6465 : 765 : TREE_OPERAND (param, 1),
6466 : : param_num, opt);
6467 : 1530 : check_function_arguments_recurse (callback, ctx,
6468 : 765 : TREE_OPERAND (param, 2),
6469 : : param_num, opt);
6470 : 765 : return;
6471 : : }
6472 : : }
6473 : :
6474 : 721860 : (*callback) (ctx, param, param_num);
6475 : : }
6476 : :
6477 : : /* Checks for a builtin function FNDECL that the number of arguments
6478 : : NARGS against the required number REQUIRED and issues an error if
6479 : : there is a mismatch. Returns true if the number of arguments is
6480 : : correct, otherwise false. LOC is the location of FNDECL. */
6481 : :
6482 : : static bool
6483 : 783069 : builtin_function_validate_nargs (location_t loc, tree fndecl, int nargs,
6484 : : int required, bool complain)
6485 : : {
6486 : 783069 : if (nargs < required)
6487 : : {
6488 : 182 : if (complain)
6489 : 143 : error_at (loc, "too few arguments to function %qE", fndecl);
6490 : 182 : return false;
6491 : : }
6492 : 782887 : else if (nargs > required)
6493 : : {
6494 : 138 : if (complain)
6495 : 93 : error_at (loc, "too many arguments to function %qE", fndecl);
6496 : 138 : return false;
6497 : : }
6498 : : return true;
6499 : : }
6500 : :
6501 : : /* Helper macro for check_builtin_function_arguments. */
6502 : : #define ARG_LOCATION(N) \
6503 : : (arg_loc.is_empty () \
6504 : : ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
6505 : : : expansion_point_location (arg_loc[(N)]))
6506 : :
6507 : : /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
6508 : : Returns false if there was an error, otherwise true. LOC is the
6509 : : location of the function; ARG_LOC is a vector of locations of the
6510 : : arguments. If FNDECL is the result of resolving an overloaded
6511 : : target built-in, ORIG_FNDECL is the original function decl,
6512 : : otherwise it is null. */
6513 : :
6514 : : bool
6515 : 40361827 : check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
6516 : : tree fndecl, tree orig_fndecl, int nargs,
6517 : : tree *args, bool complain)
6518 : : {
6519 : 40361827 : if (!fndecl_built_in_p (fndecl))
6520 : : return true;
6521 : :
6522 : 40361827 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6523 : 32898605 : return (!targetm.check_builtin_call
6524 : 32898605 : || targetm.check_builtin_call (loc, arg_loc, fndecl, orig_fndecl,
6525 : : nargs, args, complain));
6526 : :
6527 : 7463222 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_FRONTEND)
6528 : : return true;
6529 : :
6530 : 7433603 : gcc_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL);
6531 : 7433603 : switch (DECL_FUNCTION_CODE (fndecl))
6532 : : {
6533 : 2 : case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX:
6534 : 2 : if (!tree_fits_uhwi_p (args[2]))
6535 : : {
6536 : 0 : if (complain)
6537 : 0 : error_at (
6538 : 0 : ARG_LOCATION (2),
6539 : : "third argument to function %qE must be a constant integer",
6540 : : fndecl);
6541 : 0 : return false;
6542 : : }
6543 : : /* fall through */
6544 : :
6545 : 264 : case BUILT_IN_ALLOCA_WITH_ALIGN:
6546 : 264 : {
6547 : : /* Get the requested alignment (in bits) if it's a constant
6548 : : integer expression. */
6549 : 264 : unsigned HOST_WIDE_INT align
6550 : 264 : = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
6551 : :
6552 : : /* Determine if the requested alignment is a power of 2. */
6553 : 264 : if ((align & (align - 1)))
6554 : : align = 0;
6555 : :
6556 : : /* The maximum alignment in bits corresponding to the same
6557 : : maximum in bytes enforced in check_user_alignment(). */
6558 : 315 : unsigned maxalign = (UINT_MAX >> 1) + 1;
6559 : :
6560 : : /* Reject invalid alignments. */
6561 : 181 : if (align < BITS_PER_UNIT || maxalign < align)
6562 : : {
6563 : 134 : if (complain)
6564 : 169 : error_at (ARG_LOCATION (1),
6565 : : "second argument to function %qE must be a constant "
6566 : : "integer power of 2 between %qi and %qu bits",
6567 : : fndecl, BITS_PER_UNIT, maxalign);
6568 : 134 : return false;
6569 : : }
6570 : : return true;
6571 : : }
6572 : :
6573 : 101462 : case BUILT_IN_CONSTANT_P:
6574 : 101462 : return builtin_function_validate_nargs (loc, fndecl, nargs, 1, complain);
6575 : :
6576 : 277377 : case BUILT_IN_ISFINITE:
6577 : 277377 : case BUILT_IN_ISINF:
6578 : 277377 : case BUILT_IN_ISINF_SIGN:
6579 : 277377 : case BUILT_IN_ISNAN:
6580 : 277377 : case BUILT_IN_ISNORMAL:
6581 : 277377 : case BUILT_IN_ISSIGNALING:
6582 : 277377 : case BUILT_IN_SIGNBIT:
6583 : 277377 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 1, complain))
6584 : : {
6585 : 277342 : if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
6586 : : {
6587 : 37 : if (complain)
6588 : 28 : error_at (ARG_LOCATION (0),
6589 : : "non-floating-point argument in "
6590 : : "call to function %qE",
6591 : : fndecl);
6592 : 37 : return false;
6593 : : }
6594 : : return true;
6595 : : }
6596 : : return false;
6597 : :
6598 : 230367 : case BUILT_IN_ISGREATER:
6599 : 230367 : case BUILT_IN_ISGREATEREQUAL:
6600 : 230367 : case BUILT_IN_ISLESS:
6601 : 230367 : case BUILT_IN_ISLESSEQUAL:
6602 : 230367 : case BUILT_IN_ISLESSGREATER:
6603 : 230367 : case BUILT_IN_ISUNORDERED:
6604 : 230367 : case BUILT_IN_ISEQSIG:
6605 : 230367 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 2, complain))
6606 : : {
6607 : 230332 : enum tree_code code0, code1;
6608 : 230332 : code0 = TREE_CODE (TREE_TYPE (args[0]));
6609 : 230332 : code1 = TREE_CODE (TREE_TYPE (args[1]));
6610 : 230332 : if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
6611 : 154 : || (code0 == REAL_TYPE
6612 : 56 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
6613 : 99 : || ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
6614 : 88 : && code1 == REAL_TYPE)))
6615 : : {
6616 : 39 : if (complain)
6617 : 18 : error_at (loc,
6618 : : "non-floating-point arguments in call to "
6619 : : "function %qE",
6620 : : fndecl);
6621 : 39 : return false;
6622 : : }
6623 : : return true;
6624 : : }
6625 : : return false;
6626 : :
6627 : 39089 : case BUILT_IN_FPCLASSIFY:
6628 : 39089 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 6, complain))
6629 : : {
6630 : 234462 : for (unsigned int i = 0; i < 5; i++)
6631 : 195389 : if (TREE_CODE (args[i]) != INTEGER_CST)
6632 : : {
6633 : 12 : if (complain)
6634 : 12 : error_at (ARG_LOCATION (i),
6635 : : "non-const integer argument %u in "
6636 : : "call to function %qE",
6637 : : i + 1, fndecl);
6638 : 12 : return false;
6639 : : }
6640 : :
6641 : 39073 : if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
6642 : : {
6643 : 2 : if (complain)
6644 : 4 : error_at (ARG_LOCATION (5),
6645 : : "non-floating-point argument in "
6646 : : "call to function %qE",
6647 : : fndecl);
6648 : 2 : return false;
6649 : : }
6650 : : return true;
6651 : : }
6652 : : return false;
6653 : :
6654 : 419 : case BUILT_IN_ASSUME_ALIGNED:
6655 : 818 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 2 + (nargs > 2),
6656 : : complain))
6657 : : {
6658 : 417 : if (nargs >= 3
6659 : 18 : && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE
6660 : 424 : && TREE_CODE (TREE_TYPE (args[2])) != BITINT_TYPE)
6661 : : {
6662 : 6 : if (complain)
6663 : 6 : error_at (ARG_LOCATION (2),
6664 : : "non-integer argument 3 in call to "
6665 : : "function %qE",
6666 : : fndecl);
6667 : 6 : return false;
6668 : : }
6669 : : return true;
6670 : : }
6671 : : return false;
6672 : :
6673 : 74601 : case BUILT_IN_ADD_OVERFLOW:
6674 : 74601 : case BUILT_IN_SUB_OVERFLOW:
6675 : 74601 : case BUILT_IN_MUL_OVERFLOW:
6676 : 74601 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 3, complain))
6677 : : {
6678 : : unsigned i;
6679 : 223347 : for (i = 0; i < 2; i++)
6680 : 148924 : if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
6681 : : {
6682 : 55 : if (complain)
6683 : 77 : error_at (ARG_LOCATION (i),
6684 : : "argument %u in call to function "
6685 : : "%qE does not have integral type",
6686 : : i + 1, fndecl);
6687 : 55 : return false;
6688 : : }
6689 : 74423 : if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
6690 : 74423 : || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args[2]))))
6691 : : {
6692 : 60 : if (complain)
6693 : 75 : error_at (ARG_LOCATION (2),
6694 : : "argument 3 in call to function %qE "
6695 : : "does not have pointer to integral type",
6696 : : fndecl);
6697 : 60 : return false;
6698 : : }
6699 : 74363 : else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == ENUMERAL_TYPE)
6700 : : {
6701 : 16 : if (complain)
6702 : 11 : error_at (ARG_LOCATION (2),
6703 : : "argument 3 in call to function %qE "
6704 : : "has pointer to enumerated type",
6705 : : fndecl);
6706 : 16 : return false;
6707 : : }
6708 : 74347 : else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == BOOLEAN_TYPE)
6709 : : {
6710 : 21 : if (complain)
6711 : 16 : error_at (ARG_LOCATION (2),
6712 : : "argument 3 in call to function %qE "
6713 : : "has pointer to boolean type",
6714 : : fndecl);
6715 : 21 : return false;
6716 : : }
6717 : 74326 : else if (TYPE_READONLY (TREE_TYPE (TREE_TYPE (args[2]))))
6718 : : {
6719 : 30 : if (complain)
6720 : 30 : error_at (ARG_LOCATION (2),
6721 : : "argument %u in call to function %qE "
6722 : : "has pointer to %qs type (%qT)",
6723 : 21 : 3, fndecl, "const", TREE_TYPE (args[2]));
6724 : 30 : return false;
6725 : : }
6726 : 74296 : else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (args[2]))))
6727 : : {
6728 : 6 : if (complain)
6729 : 12 : error_at (ARG_LOCATION (2),
6730 : : "argument %u in call to function %qE "
6731 : : "has pointer to %qs type (%qT)",
6732 : 6 : 3, fndecl, "_Atomic", TREE_TYPE (args[2]));
6733 : 6 : return false;
6734 : : }
6735 : : return true;
6736 : : }
6737 : : return false;
6738 : :
6739 : 41854 : case BUILT_IN_ADD_OVERFLOW_P:
6740 : 41854 : case BUILT_IN_SUB_OVERFLOW_P:
6741 : 41854 : case BUILT_IN_MUL_OVERFLOW_P:
6742 : 41854 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 3, complain))
6743 : : {
6744 : : unsigned i;
6745 : 167205 : for (i = 0; i < 3; i++)
6746 : 125421 : if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
6747 : : {
6748 : 37 : if (complain)
6749 : 35 : error_at (ARG_LOCATION (i),
6750 : : "argument %u in call to function "
6751 : : "%qE does not have integral type",
6752 : : i + 1, fndecl);
6753 : 37 : return false;
6754 : : }
6755 : 41784 : if (TREE_CODE (TREE_TYPE (args[2])) == ENUMERAL_TYPE)
6756 : : {
6757 : 13 : if (complain)
6758 : 5 : error_at (ARG_LOCATION (2),
6759 : : "argument %u in call to function "
6760 : : "%qE has enumerated type",
6761 : : 3, fndecl);
6762 : 13 : return false;
6763 : : }
6764 : 41771 : else if (TREE_CODE (TREE_TYPE (args[2])) == BOOLEAN_TYPE)
6765 : : {
6766 : 13 : if (complain)
6767 : 5 : error_at (ARG_LOCATION (2),
6768 : : "argument %u in call to function "
6769 : : "%qE has boolean type",
6770 : : 3, fndecl);
6771 : 13 : return false;
6772 : : }
6773 : : return true;
6774 : : }
6775 : : return false;
6776 : :
6777 : 10400 : case BUILT_IN_CLEAR_PADDING:
6778 : 10400 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 1, complain))
6779 : : {
6780 : 10385 : if (!POINTER_TYPE_P (TREE_TYPE (args[0])))
6781 : : {
6782 : 7 : if (complain)
6783 : 5 : error_at (ARG_LOCATION (0),
6784 : : "argument %u in call to function "
6785 : : "%qE does not have pointer type",
6786 : : 1, fndecl);
6787 : 7 : return false;
6788 : : }
6789 : 10378 : else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (args[0]))))
6790 : : {
6791 : 11 : if (complain)
6792 : 10 : error_at (ARG_LOCATION (0),
6793 : : "argument %u in call to function "
6794 : : "%qE points to incomplete type",
6795 : : 1, fndecl);
6796 : 11 : return false;
6797 : : }
6798 : 10367 : else if (TYPE_READONLY (TREE_TYPE (TREE_TYPE (args[0]))))
6799 : : {
6800 : 7 : if (complain)
6801 : 5 : error_at (ARG_LOCATION (0),
6802 : : "argument %u in call to function %qE "
6803 : : "has pointer to %qs type (%qT)",
6804 : 4 : 1, fndecl, "const", TREE_TYPE (args[0]));
6805 : 7 : return false;
6806 : : }
6807 : 10360 : else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (args[0]))))
6808 : : {
6809 : 0 : if (complain)
6810 : 0 : error_at (ARG_LOCATION (0),
6811 : : "argument %u in call to function %qE "
6812 : : "has pointer to %qs type (%qT)",
6813 : 0 : 1, fndecl, "_Atomic", TREE_TYPE (args[0]));
6814 : 0 : return false;
6815 : : }
6816 : : return true;
6817 : : }
6818 : : return false;
6819 : :
6820 : 58706 : case BUILT_IN_CLZG:
6821 : 58706 : case BUILT_IN_CTZG:
6822 : 58706 : case BUILT_IN_CLRSBG:
6823 : 58706 : case BUILT_IN_FFSG:
6824 : 58706 : case BUILT_IN_PARITYG:
6825 : 58706 : case BUILT_IN_POPCOUNTG:
6826 : 58706 : if (nargs == 2
6827 : 58706 : && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CLZG
6828 : 16555 : || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CTZG))
6829 : : {
6830 : 51206 : if (!INTEGRAL_TYPE_P (TREE_TYPE (args[1])))
6831 : : {
6832 : 18 : if (complain)
6833 : 15 : error_at (ARG_LOCATION (1),
6834 : : "argument %u in call to function "
6835 : : "%qE does not have integral type",
6836 : : 2, fndecl);
6837 : 18 : return false;
6838 : : }
6839 : 51188 : if ((TYPE_PRECISION (TREE_TYPE (args[1]))
6840 : 51188 : > TYPE_PRECISION (integer_type_node))
6841 : 51188 : || (TYPE_PRECISION (TREE_TYPE (args[1]))
6842 : 51174 : == TYPE_PRECISION (integer_type_node)
6843 : 51168 : && TYPE_UNSIGNED (TREE_TYPE (args[1]))))
6844 : : {
6845 : 28 : if (complain)
6846 : 26 : error_at (ARG_LOCATION (1),
6847 : : "argument %u in call to function "
6848 : : "%qE does not have %<int%> type",
6849 : : 2, fndecl);
6850 : 28 : return false;
6851 : : }
6852 : : }
6853 : 7500 : else if (!builtin_function_validate_nargs (loc, fndecl, nargs, 1,
6854 : : complain))
6855 : : return false;
6856 : :
6857 : 58594 : if (!INTEGRAL_TYPE_P (TREE_TYPE (args[0])))
6858 : : {
6859 : 82 : if (complain)
6860 : 80 : error_at (ARG_LOCATION (0),
6861 : : "argument %u in call to function "
6862 : : "%qE does not have integral type",
6863 : : 1, fndecl);
6864 : 82 : return false;
6865 : : }
6866 : 58512 : if (TREE_CODE (TREE_TYPE (args[0])) == ENUMERAL_TYPE)
6867 : : {
6868 : 42 : if (complain)
6869 : 24 : error_at (ARG_LOCATION (0),
6870 : : "argument %u in call to function "
6871 : : "%qE has enumerated type",
6872 : : 1, fndecl);
6873 : 42 : return false;
6874 : : }
6875 : 58470 : if (TREE_CODE (TREE_TYPE (args[0])) == BOOLEAN_TYPE)
6876 : : {
6877 : 50 : if (complain)
6878 : 40 : error_at (ARG_LOCATION (0),
6879 : : "argument %u in call to function "
6880 : : "%qE has boolean type",
6881 : : 1, fndecl);
6882 : 50 : return false;
6883 : : }
6884 : 58420 : if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FFSG
6885 : 58420 : || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CLRSBG)
6886 : : {
6887 : 288 : if (TYPE_UNSIGNED (TREE_TYPE (args[0])))
6888 : : {
6889 : 14 : if (complain)
6890 : 10 : error_at (ARG_LOCATION (0),
6891 : : "argument 1 in call to function "
6892 : : "%qE has unsigned type",
6893 : : fndecl);
6894 : 14 : return false;
6895 : : }
6896 : : }
6897 : 58132 : else if (!TYPE_UNSIGNED (TREE_TYPE (args[0])))
6898 : : {
6899 : 42 : if (complain)
6900 : 42 : error_at (ARG_LOCATION (0),
6901 : : "argument 1 in call to function "
6902 : : "%qE has signed type",
6903 : : fndecl);
6904 : 42 : return false;
6905 : : }
6906 : : return true;
6907 : :
6908 : : default:
6909 : : return true;
6910 : : }
6911 : : }
6912 : :
6913 : : /* Subroutine of c_parse_error.
6914 : : Return the result of concatenating LHS and RHS. RHS is really
6915 : : a string literal, its first character is indicated by RHS_START and
6916 : : RHS_SIZE is its length (including the terminating NUL character).
6917 : :
6918 : : The caller is responsible for deleting the returned pointer. */
6919 : :
6920 : : static char *
6921 : 8731 : catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
6922 : : {
6923 : 8731 : const size_t lhs_size = strlen (lhs);
6924 : 8731 : char *result = XNEWVEC (char, lhs_size + rhs_size);
6925 : 8731 : memcpy (result, lhs, lhs_size);
6926 : 8731 : memcpy (result + lhs_size, rhs_start, rhs_size);
6927 : 8731 : return result;
6928 : : }
6929 : :
6930 : : /* Issue the error given by GMSGID at RICHLOC, indicating that it occurred
6931 : : before TOKEN, which had the associated VALUE. */
6932 : :
6933 : : void
6934 : 8745 : c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
6935 : : tree value, unsigned char token_flags,
6936 : : rich_location *richloc)
6937 : : {
6938 : : #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
6939 : :
6940 : 8745 : char *message = NULL;
6941 : :
6942 : 8745 : if (token_type == CPP_EOF)
6943 : 472 : message = catenate_messages (gmsgid, " at end of input");
6944 : 8273 : else if (token_type == CPP_CHAR
6945 : : || token_type == CPP_WCHAR
6946 : : || token_type == CPP_CHAR16
6947 : : || token_type == CPP_CHAR32
6948 : 8273 : || token_type == CPP_UTF8CHAR)
6949 : : {
6950 : 24 : unsigned int val = TREE_INT_CST_LOW (value);
6951 : 24 : const char *prefix;
6952 : :
6953 : 24 : switch (token_type)
6954 : : {
6955 : : default:
6956 : : prefix = "";
6957 : : break;
6958 : 0 : case CPP_WCHAR:
6959 : 0 : prefix = "L";
6960 : 0 : break;
6961 : 0 : case CPP_CHAR16:
6962 : 0 : prefix = "u";
6963 : 0 : break;
6964 : 0 : case CPP_CHAR32:
6965 : 0 : prefix = "U";
6966 : 0 : break;
6967 : 0 : case CPP_UTF8CHAR:
6968 : 0 : prefix = "u8";
6969 : 0 : break;
6970 : : }
6971 : :
6972 : 24 : if (val <= UCHAR_MAX && ISGRAPH (val))
6973 : 20 : message = catenate_messages (gmsgid, " before %s'%c'");
6974 : : else
6975 : 4 : message = catenate_messages (gmsgid, " before %s'\\x%x'");
6976 : :
6977 : 24 : error_at (richloc, message, prefix, val);
6978 : 24 : free (message);
6979 : 24 : message = NULL;
6980 : : }
6981 : 8249 : else if (token_type == CPP_CHAR_USERDEF
6982 : : || token_type == CPP_WCHAR_USERDEF
6983 : : || token_type == CPP_CHAR16_USERDEF
6984 : : || token_type == CPP_CHAR32_USERDEF
6985 : 8249 : || token_type == CPP_UTF8CHAR_USERDEF)
6986 : 12 : message = catenate_messages (gmsgid,
6987 : : " before user-defined character literal");
6988 : 8237 : else if (token_type == CPP_STRING_USERDEF
6989 : : || token_type == CPP_WSTRING_USERDEF
6990 : : || token_type == CPP_STRING16_USERDEF
6991 : : || token_type == CPP_STRING32_USERDEF
6992 : 8237 : || token_type == CPP_UTF8STRING_USERDEF)
6993 : 25 : message = catenate_messages (gmsgid, " before user-defined string literal");
6994 : 8212 : else if (token_type == CPP_STRING
6995 : : || token_type == CPP_WSTRING
6996 : : || token_type == CPP_STRING16
6997 : : || token_type == CPP_STRING32
6998 : 8212 : || token_type == CPP_UTF8STRING)
6999 : 44 : message = catenate_messages (gmsgid, " before string constant");
7000 : 8168 : else if (token_type == CPP_NUMBER)
7001 : 528 : message = catenate_messages (gmsgid, " before numeric constant");
7002 : 7640 : else if (token_type == CPP_NAME)
7003 : : {
7004 : 2101 : message = catenate_messages (gmsgid, " before %qE");
7005 : 2101 : error_at (richloc, message, value);
7006 : 2101 : free (message);
7007 : 2101 : message = NULL;
7008 : : }
7009 : 5539 : else if (token_type == CPP_PRAGMA)
7010 : 20 : message = catenate_messages (gmsgid, " before %<#pragma%>");
7011 : 5519 : else if (token_type == CPP_PRAGMA_EOL)
7012 : 476 : message = catenate_messages (gmsgid, " before end of line");
7013 : 5043 : else if (token_type == CPP_DECLTYPE)
7014 : 12 : message = catenate_messages (gmsgid, " before %<decltype%>");
7015 : 5031 : else if (token_type == CPP_EMBED)
7016 : 0 : message = catenate_messages (gmsgid, " before %<#embed%>");
7017 : 5031 : else if (token_type < N_TTYPES)
7018 : : {
7019 : 5017 : message = catenate_messages (gmsgid, " before %qs token");
7020 : 5017 : error_at (richloc, message, cpp_type2name (token_type, token_flags));
7021 : 5017 : free (message);
7022 : 5017 : message = NULL;
7023 : : }
7024 : : else
7025 : 14 : error_at (richloc, gmsgid);
7026 : :
7027 : 8745 : if (message)
7028 : : {
7029 : 1589 : error_at (richloc, message);
7030 : 1589 : free (message);
7031 : : }
7032 : : #undef catenate_messages
7033 : 8745 : }
7034 : :
7035 : : /* Return the gcc option code associated with the reason for a cpp
7036 : : message, or 0 if none. */
7037 : :
7038 : : static diagnostics::option_id
7039 : 148172 : c_option_controlling_cpp_diagnostic (enum cpp_warning_reason reason)
7040 : : {
7041 : 148172 : const struct cpp_reason_option_codes_t *entry;
7042 : :
7043 : 4254485 : for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
7044 : : {
7045 : 4247506 : if (entry->reason == reason)
7046 : 141193 : return entry->option_code;
7047 : : }
7048 : 0 : return 0;
7049 : : }
7050 : :
7051 : : /* Return TRUE if the given option index corresponds to a diagnostic
7052 : : issued by libcpp. Linear search seems fine for now. */
7053 : : bool
7054 : 2165621 : c_option_is_from_cpp_diagnostics (int option_index)
7055 : : {
7056 : 51193475 : for (auto entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE;
7057 : : ++entry)
7058 : : {
7059 : 50376436 : if (entry->option_code == option_index)
7060 : : return true;
7061 : : }
7062 : : return false;
7063 : : }
7064 : :
7065 : : /* Callback from cpp_diagnostic for PFILE to print diagnostics from the
7066 : : preprocessor. The diagnostic is of type LEVEL, with REASON set
7067 : : to the reason code if LEVEL is represents a warning, at location
7068 : : RICHLOC unless this is after lexing and the compiler's location
7069 : : should be used instead; MSG is the translated message and AP
7070 : : the arguments. Returns true if a diagnostic was emitted, false
7071 : : otherwise. */
7072 : :
7073 : : bool
7074 : 148182 : c_cpp_diagnostic (cpp_reader *pfile ATTRIBUTE_UNUSED,
7075 : : enum cpp_diagnostic_level level,
7076 : : enum cpp_warning_reason reason,
7077 : : rich_location *richloc,
7078 : : const char *msg, va_list *ap)
7079 : : {
7080 : 148182 : diagnostics::diagnostic_info diagnostic;
7081 : 148182 : enum diagnostics::kind dlevel;
7082 : 148182 : bool save_warn_system_headers = global_dc->m_warn_system_headers;
7083 : 148182 : bool ret;
7084 : :
7085 : 148182 : switch (level)
7086 : : {
7087 : 90 : case CPP_DL_WARNING_SYSHDR:
7088 : 90 : if (flag_no_output)
7089 : : return false;
7090 : 89 : global_dc->m_warn_system_headers = 1;
7091 : : /* Fall through. */
7092 : 26152 : case CPP_DL_WARNING:
7093 : 26152 : if (flag_no_output)
7094 : : return false;
7095 : : dlevel = diagnostics::kind::warning;
7096 : : break;
7097 : 116907 : case CPP_DL_PEDWARN:
7098 : 116907 : if (flag_no_output && !flag_pedantic_errors)
7099 : : return false;
7100 : : dlevel = diagnostics::kind::pedwarn;
7101 : : break;
7102 : : case CPP_DL_ERROR:
7103 : : dlevel = diagnostics::kind::error;
7104 : : break;
7105 : 0 : case CPP_DL_ICE:
7106 : 0 : dlevel = diagnostics::kind::ice;
7107 : 0 : break;
7108 : 603 : case CPP_DL_NOTE:
7109 : 603 : dlevel = diagnostics::kind::note;
7110 : 603 : break;
7111 : 364 : case CPP_DL_FATAL:
7112 : 364 : dlevel = diagnostics::kind::fatal;
7113 : 364 : break;
7114 : 0 : default:
7115 : 0 : gcc_unreachable ();
7116 : : }
7117 : 148172 : if (override_libcpp_locations)
7118 : 247 : richloc->set_range (0, input_location, SHOW_RANGE_WITH_CARET);
7119 : 148172 : diagnostic_set_info_translated (&diagnostic, msg, ap,
7120 : : richloc, dlevel);
7121 : 296344 : diagnostic_set_option_id (&diagnostic,
7122 : : c_option_controlling_cpp_diagnostic (reason));
7123 : 148172 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
7124 : 147808 : if (level == CPP_DL_WARNING_SYSHDR)
7125 : 89 : global_dc->m_warn_system_headers = save_warn_system_headers;
7126 : : return ret;
7127 : 147818 : }
7128 : :
7129 : : /* Convert a character from the host to the target execution character
7130 : : set. cpplib handles this, mostly. */
7131 : :
7132 : : HOST_WIDE_INT
7133 : 6585010 : c_common_to_target_charset (HOST_WIDE_INT c)
7134 : : {
7135 : : /* Character constants in GCC proper are sign-extended under -fsigned-char,
7136 : : zero-extended under -fno-signed-char. cpplib insists that characters
7137 : : and character constants are always unsigned. Hence we must convert
7138 : : back and forth. */
7139 : 6585010 : cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
7140 : :
7141 : 6585010 : uc = cpp_host_to_exec_charset (parse_in, uc);
7142 : :
7143 : 6585010 : if (flag_signed_char)
7144 : 6584913 : return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
7145 : 6584913 : >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
7146 : : else
7147 : 97 : return uc;
7148 : : }
7149 : :
7150 : : /* Fold an offsetof-like expression. EXPR is a nested sequence of component
7151 : : references with an INDIRECT_REF of a constant at the bottom; much like the
7152 : : traditional rendering of offsetof as a macro. TYPE is the desired type of
7153 : : the whole expression. Return the folded result. */
7154 : :
7155 : : tree
7156 : 15128 : fold_offsetof (tree expr, tree type, enum tree_code ctx)
7157 : : {
7158 : 15137 : tree base, off, t;
7159 : 15137 : tree_code code = TREE_CODE (expr);
7160 : 15137 : switch (code)
7161 : : {
7162 : : case ERROR_MARK:
7163 : : return expr;
7164 : :
7165 : 12 : case VAR_DECL:
7166 : 12 : error ("cannot apply %<offsetof%> to static data member %qD", expr);
7167 : 12 : return error_mark_node;
7168 : :
7169 : 9 : case CALL_EXPR:
7170 : 9 : case TARGET_EXPR:
7171 : 9 : error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
7172 : 9 : return error_mark_node;
7173 : :
7174 : 6646 : case NOP_EXPR:
7175 : 6646 : case INDIRECT_REF:
7176 : 6646 : if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
7177 : : {
7178 : 17 : error ("cannot apply %<offsetof%> to a non constant address");
7179 : 17 : return error_mark_node;
7180 : : }
7181 : 6629 : return convert (type, TREE_OPERAND (expr, 0));
7182 : :
7183 : 7234 : case COMPONENT_REF:
7184 : 7234 : base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
7185 : 7234 : if (base == error_mark_node)
7186 : : return base;
7187 : :
7188 : 7231 : t = TREE_OPERAND (expr, 1);
7189 : 7231 : if (DECL_C_BIT_FIELD (t))
7190 : : {
7191 : 1 : error ("attempt to take address of bit-field structure "
7192 : : "member %qD", t);
7193 : 1 : return error_mark_node;
7194 : : }
7195 : 7230 : off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
7196 : 7230 : size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
7197 : : / BITS_PER_UNIT));
7198 : 7230 : break;
7199 : :
7200 : 1226 : case ARRAY_REF:
7201 : 1226 : base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
7202 : 1226 : if (base == error_mark_node)
7203 : : return base;
7204 : :
7205 : 1223 : t = TREE_OPERAND (expr, 1);
7206 : 1223 : STRIP_ANY_LOCATION_WRAPPER (t);
7207 : :
7208 : : /* Check if the offset goes beyond the upper bound of the array. */
7209 : 1223 : if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
7210 : : {
7211 : 956 : tree upbound = array_ref_up_bound (expr);
7212 : 956 : if (upbound != NULL_TREE
7213 : 844 : && TREE_CODE (upbound) == INTEGER_CST
7214 : 1800 : && !tree_int_cst_equal (upbound,
7215 : 844 : TYPE_MAX_VALUE (TREE_TYPE (upbound))))
7216 : : {
7217 : 835 : if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
7218 : 433 : upbound = size_binop (PLUS_EXPR, upbound,
7219 : : build_int_cst (TREE_TYPE (upbound), 1));
7220 : 835 : if (tree_int_cst_lt (upbound, t))
7221 : : {
7222 : 244 : tree v;
7223 : :
7224 : 244 : for (v = TREE_OPERAND (expr, 0);
7225 : 420 : TREE_CODE (v) == COMPONENT_REF;
7226 : 176 : v = TREE_OPERAND (v, 0))
7227 : 280 : if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
7228 : : == RECORD_TYPE)
7229 : : {
7230 : 220 : tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
7231 : 307 : for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
7232 : 191 : if (TREE_CODE (fld_chain) == FIELD_DECL)
7233 : : break;
7234 : :
7235 : : if (fld_chain)
7236 : : break;
7237 : : }
7238 : : /* Don't warn if the array might be considered a poor
7239 : : man's flexible array member with a very permissive
7240 : : definition thereof. */
7241 : 244 : if (TREE_CODE (v) == ARRAY_REF
7242 : 172 : || TREE_CODE (v) == COMPONENT_REF)
7243 : 176 : warning (OPT_Warray_bounds_,
7244 : : "index %E denotes an offset "
7245 : : "greater than size of %qT",
7246 : 176 : t, TREE_TYPE (TREE_OPERAND (expr, 0)));
7247 : : }
7248 : : }
7249 : : }
7250 : :
7251 : 1223 : t = convert (sizetype, t);
7252 : 1223 : off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
7253 : 1223 : break;
7254 : :
7255 : 9 : case COMPOUND_EXPR:
7256 : : /* Handle static members of volatile structs. */
7257 : 9 : t = TREE_OPERAND (expr, 1);
7258 : 9 : gcc_checking_assert (VAR_P (get_base_address (t)));
7259 : : return fold_offsetof (t, type);
7260 : :
7261 : 0 : default:
7262 : 0 : gcc_unreachable ();
7263 : : }
7264 : :
7265 : 8453 : if (!POINTER_TYPE_P (type))
7266 : 7584 : return size_binop (PLUS_EXPR, base, convert (type, off));
7267 : 869 : return fold_build_pointer_plus (base, off);
7268 : : }
7269 : :
7270 : : /* *PTYPE is an incomplete array. Complete it with a domain based on
7271 : : INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
7272 : : is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
7273 : : 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
7274 : :
7275 : : int
7276 : 549319 : complete_array_type (tree *ptype, tree initial_value, bool do_default)
7277 : : {
7278 : 549319 : tree maxindex, type, main_type, elt, unqual_elt;
7279 : 549319 : int failure = 0, quals;
7280 : 549319 : bool overflow_p = false;
7281 : :
7282 : 549319 : maxindex = size_zero_node;
7283 : 549319 : if (initial_value)
7284 : : {
7285 : 541847 : STRIP_ANY_LOCATION_WRAPPER (initial_value);
7286 : :
7287 : 541847 : if (TREE_CODE (initial_value) == STRING_CST)
7288 : : {
7289 : 437032 : int eltsize
7290 : 437032 : = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
7291 : 437032 : maxindex = size_int (TREE_STRING_LENGTH (initial_value) / eltsize
7292 : : - 1);
7293 : : }
7294 : 104815 : else if (TREE_CODE (initial_value) == CONSTRUCTOR)
7295 : : {
7296 : 103881 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
7297 : :
7298 : 103881 : if (vec_safe_is_empty (v))
7299 : : {
7300 : 404 : if (pedantic)
7301 : 68 : failure = 3;
7302 : 404 : maxindex = ssize_int (-1);
7303 : : }
7304 : : else
7305 : : {
7306 : 103477 : tree curindex;
7307 : 103477 : unsigned HOST_WIDE_INT cnt = 1;
7308 : 103477 : constructor_elt *ce;
7309 : 103477 : bool fold_p = false;
7310 : :
7311 : 103477 : if ((*v)[0].index)
7312 : 90818 : maxindex = (*v)[0].index, fold_p = true;
7313 : 103477 : if (TREE_CODE ((*v)[0].value) == RAW_DATA_CST)
7314 : 4 : cnt = 0;
7315 : :
7316 : : curindex = maxindex;
7317 : :
7318 : 17242531 : for (; vec_safe_iterate (v, cnt, &ce); cnt++)
7319 : : {
7320 : 17139054 : bool curfold_p = false;
7321 : 17139054 : if (ce->index)
7322 : 15901010 : curindex = ce->index, curfold_p = true;
7323 : 17139054 : if (!ce->index || TREE_CODE (ce->value) == RAW_DATA_CST)
7324 : : {
7325 : 1238406 : if (fold_p || curfold_p)
7326 : : {
7327 : : /* Since we treat size types now as ordinary
7328 : : unsigned types, we need an explicit overflow
7329 : : check. */
7330 : 362 : tree orig = curindex;
7331 : 362 : curindex = fold_convert (sizetype, curindex);
7332 : 362 : overflow_p |= tree_int_cst_lt (curindex, orig);
7333 : 362 : curfold_p = false;
7334 : : }
7335 : 1238406 : if (TREE_CODE (ce->value) == RAW_DATA_CST)
7336 : 362 : curindex
7337 : 724 : = size_binop (PLUS_EXPR, curindex,
7338 : : size_int (RAW_DATA_LENGTH (ce->value)
7339 : : - ((ce->index || !cnt)
7340 : : ? 1 : 0)));
7341 : : else
7342 : 1238044 : curindex = size_binop (PLUS_EXPR, curindex,
7343 : : size_one_node);
7344 : : }
7345 : 17139054 : if (tree_int_cst_lt (maxindex, curindex))
7346 : 17139054 : maxindex = curindex, fold_p = curfold_p;
7347 : : }
7348 : 103477 : if (fold_p)
7349 : : {
7350 : 90814 : tree orig = maxindex;
7351 : 90814 : maxindex = fold_convert (sizetype, maxindex);
7352 : 90814 : overflow_p |= tree_int_cst_lt (maxindex, orig);
7353 : : }
7354 : : }
7355 : : }
7356 : : else
7357 : : {
7358 : : /* Make an error message unless that happened already. */
7359 : 934 : if (initial_value != error_mark_node)
7360 : 31 : failure = 1;
7361 : : }
7362 : : }
7363 : : else
7364 : : {
7365 : 7472 : failure = 2;
7366 : 7472 : if (!do_default)
7367 : : return failure;
7368 : : }
7369 : :
7370 : 541942 : type = *ptype;
7371 : 541942 : elt = TREE_TYPE (type);
7372 : 541942 : quals = TYPE_QUALS (strip_array_types (elt));
7373 : 541942 : if (quals == 0)
7374 : : unqual_elt = elt;
7375 : : else
7376 : 510022 : unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
7377 : :
7378 : : /* Using build_distinct_type_copy and modifying things afterward instead
7379 : : of using build_array_type to create a new type preserves all of the
7380 : : TYPE_LANG_FLAG_? bits that the front end may have set. */
7381 : 541942 : main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7382 : 541942 : TREE_TYPE (main_type) = unqual_elt;
7383 : 1083884 : TYPE_DOMAIN (main_type)
7384 : 541942 : = build_range_type (TREE_TYPE (maxindex),
7385 : 541942 : build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
7386 : 541942 : TYPE_TYPELESS_STORAGE (main_type) = TYPE_TYPELESS_STORAGE (type);
7387 : 541942 : layout_type (main_type);
7388 : :
7389 : : /* Set TYPE_STRUCTURAL_EQUALITY_P early. */
7390 : 541942 : if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
7391 : 1083494 : || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
7392 : 390 : SET_TYPE_STRUCTURAL_EQUALITY (main_type);
7393 : : else
7394 : 541552 : TYPE_CANONICAL (main_type) = main_type;
7395 : :
7396 : : /* Make sure we have the canonical MAIN_TYPE. */
7397 : 541942 : hashval_t hashcode = type_hash_canon_hash (main_type);
7398 : 541942 : main_type = type_hash_canon (hashcode, main_type);
7399 : :
7400 : : /* Fix the canonical type. */
7401 : 541942 : if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
7402 : 1083494 : || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
7403 : 390 : gcc_assert (TYPE_STRUCTURAL_EQUALITY_P (main_type));
7404 : 541552 : else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
7405 : 541552 : || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
7406 : 524505 : != TYPE_DOMAIN (main_type)))
7407 : 17047 : TYPE_CANONICAL (main_type)
7408 : 34094 : = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
7409 : 17047 : TYPE_CANONICAL (TYPE_DOMAIN (main_type)),
7410 : 17047 : TYPE_TYPELESS_STORAGE (main_type));
7411 : :
7412 : 541942 : if (quals == 0)
7413 : : type = main_type;
7414 : : else
7415 : 510022 : type = c_build_qualified_type (main_type, quals);
7416 : :
7417 : 541942 : if (COMPLETE_TYPE_P (type)
7418 : 541933 : && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
7419 : 1083866 : && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
7420 : : {
7421 : 4 : error ("size of array is too large");
7422 : : /* If we proceed with the array type as it is, we'll eventually
7423 : : crash in tree_to_[su]hwi(). */
7424 : 4 : type = error_mark_node;
7425 : : }
7426 : :
7427 : 541942 : *ptype = type;
7428 : 541942 : return failure;
7429 : : }
7430 : :
7431 : : /* INIT is an constructor of a structure with a flexible array member.
7432 : : Complete the flexible array member with a domain based on it's value. */
7433 : : void
7434 : 8896798 : complete_flexible_array_elts (tree init)
7435 : : {
7436 : 9002957 : tree elt, type;
7437 : :
7438 : 9002957 : if (init == NULL_TREE || TREE_CODE (init) != CONSTRUCTOR)
7439 : : return;
7440 : :
7441 : 107686 : if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
7442 : : return;
7443 : :
7444 : 106412 : elt = CONSTRUCTOR_ELTS (init)->last ().value;
7445 : 106412 : type = TREE_TYPE (elt);
7446 : 106412 : if (TREE_CODE (type) == ARRAY_TYPE
7447 : 106412 : && TYPE_SIZE (type) == NULL_TREE)
7448 : 253 : complete_array_type (&TREE_TYPE (elt), elt, false);
7449 : : else
7450 : : complete_flexible_array_elts (elt);
7451 : : }
7452 : :
7453 : : /* Like c_mark_addressable but don't check register qualifier. */
7454 : : void
7455 : 1125016 : c_common_mark_addressable_vec (tree t)
7456 : : {
7457 : 1305900 : while (handled_component_p (t) || TREE_CODE (t) == C_MAYBE_CONST_EXPR)
7458 : : {
7459 : 180884 : if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
7460 : 26 : t = C_MAYBE_CONST_EXPR_EXPR (t);
7461 : : else
7462 : 180858 : t = TREE_OPERAND (t, 0);
7463 : : }
7464 : 1125016 : if (!VAR_P (t)
7465 : : && TREE_CODE (t) != PARM_DECL
7466 : : && TREE_CODE (t) != COMPOUND_LITERAL_EXPR
7467 : : && TREE_CODE (t) != TARGET_EXPR)
7468 : : return;
7469 : 1104901 : if (!VAR_P (t) || !DECL_HARD_REGISTER (t))
7470 : 1104832 : TREE_ADDRESSABLE (t) = 1;
7471 : 1104901 : if (TREE_CODE (t) == COMPOUND_LITERAL_EXPR)
7472 : 126 : TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (t)) = 1;
7473 : 1104775 : else if (TREE_CODE (t) == TARGET_EXPR)
7474 : 28 : TREE_ADDRESSABLE (TARGET_EXPR_SLOT (t)) = 1;
7475 : : }
7476 : :
7477 : :
7478 : :
7479 : : /* Used to help initialize the builtin-types.def table. When a type of
7480 : : the correct size doesn't exist, use error_mark_node instead of NULL.
7481 : : The later results in segfaults even when a decl using the type doesn't
7482 : : get invoked. */
7483 : :
7484 : : tree
7485 : 1128063 : builtin_type_for_size (int size, bool unsignedp)
7486 : : {
7487 : 1128063 : tree type = c_common_type_for_size (size, unsignedp);
7488 : 1128063 : return type ? type : error_mark_node;
7489 : : }
7490 : :
7491 : : /* Work out the size of the first argument of a call to
7492 : : __builtin_speculation_safe_value. Only pointers and integral types
7493 : : are permitted. Return -1 if the argument type is not supported or
7494 : : the size is too large; 0 if the argument type is a pointer or the
7495 : : size if it is integral. */
7496 : : static enum built_in_function
7497 : 154 : speculation_safe_value_resolve_call (tree function, vec<tree, va_gc> *params,
7498 : : bool complain)
7499 : : {
7500 : : /* Type of the argument. */
7501 : 154 : tree type;
7502 : 154 : int size;
7503 : :
7504 : 154 : if (vec_safe_is_empty (params))
7505 : : {
7506 : 10 : if (complain)
7507 : 10 : error ("too few arguments to function %qE", function);
7508 : 10 : return BUILT_IN_NONE;
7509 : : }
7510 : :
7511 : 144 : type = TREE_TYPE ((*params)[0]);
7512 : 144 : if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
7513 : : {
7514 : : /* Force array-to-pointer decay for C++. */
7515 : 0 : (*params)[0] = default_conversion ((*params)[0]);
7516 : 0 : type = TREE_TYPE ((*params)[0]);
7517 : : }
7518 : :
7519 : 144 : if (POINTER_TYPE_P (type))
7520 : : return BUILT_IN_SPECULATION_SAFE_VALUE_PTR;
7521 : :
7522 : 128 : if (!INTEGRAL_TYPE_P (type))
7523 : 42 : goto incompatible;
7524 : :
7525 : 86 : if (!COMPLETE_TYPE_P (type))
7526 : 0 : goto incompatible;
7527 : :
7528 : 86 : size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7529 : 86 : if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
7530 : 86 : return ((enum built_in_function)
7531 : 172 : ((int) BUILT_IN_SPECULATION_SAFE_VALUE_1 + exact_log2 (size)));
7532 : :
7533 : 0 : incompatible:
7534 : : /* Issue the diagnostic only if the argument is valid, otherwise
7535 : : it would be redundant at best and could be misleading. */
7536 : 42 : if (type != error_mark_node && complain)
7537 : 6 : error ("operand type %qT is incompatible with argument %d of %qE",
7538 : : type, 1, function);
7539 : :
7540 : : return BUILT_IN_NONE;
7541 : : }
7542 : :
7543 : : /* Validate and coerce PARAMS, the arguments to ORIG_FUNCTION to fit
7544 : : the prototype for FUNCTION. The first argument is mandatory, a second
7545 : : argument, if present, must be type compatible with the first. */
7546 : : static bool
7547 : 102 : speculation_safe_value_resolve_params (location_t loc, tree orig_function,
7548 : : vec<tree, va_gc> *params, bool complain)
7549 : : {
7550 : 102 : tree val;
7551 : :
7552 : 102 : if (params->length () == 0)
7553 : : {
7554 : 0 : if (complain)
7555 : 0 : error_at (loc, "too few arguments to function %qE", orig_function);
7556 : 0 : return false;
7557 : : }
7558 : :
7559 : 102 : else if (params->length () > 2)
7560 : : {
7561 : 21 : if (complain)
7562 : 0 : error_at (loc, "too many arguments to function %qE", orig_function);
7563 : 21 : return false;
7564 : : }
7565 : :
7566 : 81 : val = (*params)[0];
7567 : 81 : if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE)
7568 : 0 : val = default_conversion (val);
7569 : 81 : if (!(TREE_CODE (TREE_TYPE (val)) == POINTER_TYPE
7570 : 65 : || TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE))
7571 : : {
7572 : 0 : if (complain)
7573 : 0 : error_at (loc, "expecting argument of type pointer or of type integer "
7574 : : "for argument 1");
7575 : 0 : return false;
7576 : : }
7577 : 81 : (*params)[0] = val;
7578 : :
7579 : 81 : if (params->length () == 2)
7580 : : {
7581 : 32 : tree val2 = (*params)[1];
7582 : 32 : if (TREE_CODE (TREE_TYPE (val2)) == ARRAY_TYPE)
7583 : 0 : val2 = default_conversion (val2);
7584 : 32 : if (error_operand_p (val2))
7585 : : return false;
7586 : 40 : if (!(TREE_TYPE (val) == TREE_TYPE (val2)
7587 : 9 : || useless_type_conversion_p (TREE_TYPE (val), TREE_TYPE (val2))))
7588 : : {
7589 : 9 : if (complain)
7590 : 0 : error_at (loc, "both arguments must be compatible");
7591 : 9 : return false;
7592 : : }
7593 : 22 : (*params)[1] = val2;
7594 : : }
7595 : :
7596 : : return true;
7597 : : }
7598 : :
7599 : : /* Cast the result of the builtin back to the type of the first argument,
7600 : : preserving any qualifiers that it might have. */
7601 : : static tree
7602 : 71 : speculation_safe_value_resolve_return (tree first_param, tree result)
7603 : : {
7604 : 71 : tree ptype = TREE_TYPE (first_param);
7605 : 71 : tree rtype = TREE_TYPE (result);
7606 : 71 : ptype = TYPE_MAIN_VARIANT (ptype);
7607 : :
7608 : 71 : if (tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
7609 : 71 : return convert (ptype, result);
7610 : :
7611 : : return result;
7612 : : }
7613 : :
7614 : : /* A helper function for resolve_overloaded_builtin in resolving the
7615 : : overloaded __sync_ builtins. Returns a positive power of 2 if the
7616 : : first operand of PARAMS is a pointer to a supported data type.
7617 : : Returns 0 if an error is encountered. Return -1 for _BitInt
7618 : : __atomic*fetch* with unsupported type which should be handled by
7619 : : a cas loop.
7620 : : FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
7621 : : built-ins. ORIG_FORMAT is for __sync_* rather than __atomic_*
7622 : : built-ins. */
7623 : :
7624 : : static int
7625 : 325345 : sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch,
7626 : : bool orig_format, bool complain)
7627 : : {
7628 : : /* Type of the argument. */
7629 : 325345 : tree argtype;
7630 : : /* Type the argument points to. */
7631 : 325345 : tree type;
7632 : 325345 : int size;
7633 : :
7634 : 325345 : if (vec_safe_is_empty (params))
7635 : : {
7636 : 106 : if (complain)
7637 : 106 : error ("too few arguments to function %qE", function);
7638 : 106 : return 0;
7639 : : }
7640 : :
7641 : 325239 : argtype = type = TREE_TYPE ((*params)[0]);
7642 : 325239 : if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
7643 : : {
7644 : : /* Force array-to-pointer decay for C++. */
7645 : 3 : (*params)[0] = default_conversion ((*params)[0]);
7646 : 3 : type = TREE_TYPE ((*params)[0]);
7647 : : }
7648 : 325239 : if (TREE_CODE (type) != POINTER_TYPE)
7649 : 2127 : goto incompatible;
7650 : :
7651 : 323112 : type = TREE_TYPE (type);
7652 : 323112 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
7653 : 290 : goto incompatible;
7654 : :
7655 : 322822 : if (!COMPLETE_TYPE_P (type))
7656 : 2 : goto incompatible;
7657 : :
7658 : 322820 : if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
7659 : 84 : goto incompatible;
7660 : :
7661 : 322736 : size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7662 : 322736 : if (size == 16
7663 : 7679 : && TREE_CODE (type) == BITINT_TYPE
7664 : 323262 : && !targetm.scalar_mode_supported_p (TImode))
7665 : : {
7666 : 0 : if (fetch && !orig_format)
7667 : : return -1;
7668 : 0 : goto incompatible;
7669 : : }
7670 : :
7671 : 322736 : if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
7672 : : return size;
7673 : :
7674 : 528 : if (fetch && !orig_format && TREE_CODE (type) == BITINT_TYPE)
7675 : : return -1;
7676 : :
7677 : 0 : incompatible:
7678 : : /* Issue the diagnostic only if the argument is valid, otherwise
7679 : : it would be redundant at best and could be misleading. */
7680 : 2503 : if (argtype != error_mark_node && complain)
7681 : 920 : error ("operand type %qT is incompatible with argument %d of %qE",
7682 : : argtype, 1, function);
7683 : : return 0;
7684 : : }
7685 : :
7686 : : /* A helper function for resolve_overloaded_builtin. Adds casts to
7687 : : PARAMS to make arguments match up with those of FUNCTION. Drops
7688 : : the variadic arguments at the end. Returns false if some error
7689 : : was encountered; true on success. */
7690 : :
7691 : : static bool
7692 : 322208 : sync_resolve_params (location_t loc, tree orig_function, tree function,
7693 : : vec<tree, va_gc> *params, bool orig_format, bool complain)
7694 : : {
7695 : 322208 : function_args_iterator iter;
7696 : 322208 : tree ptype;
7697 : 322208 : unsigned int parmnum;
7698 : :
7699 : 322208 : function_args_iter_init (&iter, TREE_TYPE (function));
7700 : : /* We've declared the implementation functions to use "volatile void *"
7701 : : as the pointer parameter, so we shouldn't get any complaints from the
7702 : : call to check_function_arguments what ever type the user used. */
7703 : 322208 : function_args_iter_next (&iter);
7704 : 322208 : ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
7705 : 322208 : ptype = TYPE_MAIN_VARIANT (ptype);
7706 : :
7707 : : /* For the rest of the values, we need to cast these to FTYPE, so that we
7708 : : don't get warnings for passing pointer types, etc. */
7709 : 322208 : parmnum = 0;
7710 : 1807390 : while (1)
7711 : : {
7712 : 1064799 : tree val, arg_type;
7713 : :
7714 : 1064799 : arg_type = function_args_iter_cond (&iter);
7715 : : /* XXX void_type_node belies the abstraction. */
7716 : 1064799 : if (arg_type == void_type_node)
7717 : : break;
7718 : :
7719 : 742649 : ++parmnum;
7720 : 742649 : if (params->length () <= parmnum)
7721 : : {
7722 : 58 : if (complain)
7723 : 7 : error_at (loc, "too few arguments to function %qE", orig_function);
7724 : 58 : return false;
7725 : : }
7726 : :
7727 : : /* Only convert parameters if arg_type is unsigned integer type with
7728 : : new format sync routines, i.e. don't attempt to convert pointer
7729 : : arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
7730 : : bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
7731 : : kinds). */
7732 : 742591 : if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
7733 : : {
7734 : : /* Ideally for the first conversion we'd use convert_for_assignment
7735 : : so that we get warnings for anything that doesn't match the pointer
7736 : : type. This isn't portable across the C and C++ front ends atm. */
7737 : 206077 : val = (*params)[parmnum];
7738 : 206077 : val = convert (ptype, val);
7739 : 206077 : val = convert (arg_type, val);
7740 : 206077 : (*params)[parmnum] = val;
7741 : : }
7742 : :
7743 : 742591 : function_args_iter_next (&iter);
7744 : 742591 : }
7745 : :
7746 : : /* __atomic routines are not variadic. */
7747 : 322150 : if (!orig_format && params->length () != parmnum + 1)
7748 : : {
7749 : 106 : if (complain)
7750 : 103 : error_at (loc, "too many arguments to function %qE", orig_function);
7751 : 106 : return false;
7752 : : }
7753 : :
7754 : : /* The definition of these primitives is variadic, with the remaining
7755 : : being "an optional list of variables protected by the memory barrier".
7756 : : No clue what that's supposed to mean, precisely, but we consider all
7757 : : call-clobbered variables to be protected so we're safe. */
7758 : 322044 : params->truncate (parmnum + 1);
7759 : :
7760 : 322044 : return true;
7761 : : }
7762 : :
7763 : : /* A helper function for resolve_overloaded_builtin. Adds a cast to
7764 : : RESULT to make it match the type of the first pointer argument in
7765 : : PARAMS. */
7766 : :
7767 : : static tree
7768 : 213111 : sync_resolve_return (tree first_param, tree result, bool orig_format)
7769 : : {
7770 : 213111 : tree ptype = TREE_TYPE (TREE_TYPE (first_param));
7771 : 213111 : tree rtype = TREE_TYPE (result);
7772 : 213111 : ptype = TYPE_MAIN_VARIANT (ptype);
7773 : :
7774 : : /* New format doesn't require casting unless the types are the same size. */
7775 : 213111 : if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
7776 : 213111 : return convert (ptype, result);
7777 : : else
7778 : : return result;
7779 : : }
7780 : :
7781 : : /* This function verifies the PARAMS to generic atomic FUNCTION.
7782 : : It returns the size if all the parameters are the same size, otherwise
7783 : : 0 is returned if the parameters are invalid. */
7784 : :
7785 : : static int
7786 : 83538 : get_atomic_generic_size (location_t loc, tree function,
7787 : : vec<tree, va_gc> *params, bool complain)
7788 : : {
7789 : 83538 : unsigned int n_param;
7790 : 83538 : unsigned int n_model;
7791 : 83538 : unsigned int outputs = 0; // bitset of output parameters
7792 : 83538 : unsigned int x;
7793 : 83538 : int size_0;
7794 : 83538 : tree type_0;
7795 : :
7796 : : /* Determine the parameter makeup. */
7797 : 83538 : switch (DECL_FUNCTION_CODE (function))
7798 : : {
7799 : : case BUILT_IN_ATOMIC_EXCHANGE:
7800 : : n_param = 4;
7801 : : n_model = 1;
7802 : : outputs = 5;
7803 : : break;
7804 : 50235 : case BUILT_IN_ATOMIC_LOAD:
7805 : 50235 : n_param = 3;
7806 : 50235 : n_model = 1;
7807 : 50235 : outputs = 2;
7808 : 50235 : break;
7809 : 11501 : case BUILT_IN_ATOMIC_STORE:
7810 : 11501 : n_param = 3;
7811 : 11501 : n_model = 1;
7812 : 11501 : outputs = 1;
7813 : 11501 : break;
7814 : 21148 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7815 : 21148 : n_param = 6;
7816 : 21148 : n_model = 2;
7817 : 21148 : outputs = 3;
7818 : 21148 : break;
7819 : 0 : default:
7820 : 0 : gcc_unreachable ();
7821 : : }
7822 : :
7823 : 83538 : if (vec_safe_length (params) != n_param)
7824 : : {
7825 : 466 : if (complain)
7826 : 247 : error_at (loc, "incorrect number of arguments to function %qE",
7827 : : function);
7828 : 466 : return 0;
7829 : : }
7830 : :
7831 : : /* Get type of first parameter, and determine its size. */
7832 : 83072 : type_0 = TREE_TYPE ((*params)[0]);
7833 : 83072 : if (TREE_CODE (type_0) == ARRAY_TYPE && c_dialect_cxx ())
7834 : : {
7835 : : /* Force array-to-pointer decay for C++. */
7836 : 15 : (*params)[0] = default_conversion ((*params)[0]);
7837 : 15 : type_0 = TREE_TYPE ((*params)[0]);
7838 : : }
7839 : 83072 : if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
7840 : : {
7841 : 152 : if (complain)
7842 : 41 : error_at (loc, "argument 1 of %qE must be a non-void pointer type",
7843 : : function);
7844 : 152 : return 0;
7845 : : }
7846 : :
7847 : 82920 : if (!COMPLETE_TYPE_P (TREE_TYPE (type_0)))
7848 : : {
7849 : 23 : if (complain)
7850 : 8 : error_at (loc, "argument 1 of %qE must be a pointer to a complete type",
7851 : : function);
7852 : 23 : return 0;
7853 : : }
7854 : :
7855 : : /* Types must be compile time constant sizes. */
7856 : 82897 : if (!tree_fits_uhwi_p ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))))
7857 : : {
7858 : 1 : if (complain)
7859 : 1 : error_at (loc,
7860 : : "argument 1 of %qE must be a pointer to a constant size type",
7861 : : function);
7862 : 1 : return 0;
7863 : : }
7864 : :
7865 : 82896 : size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
7866 : :
7867 : : /* Zero size objects are not allowed. */
7868 : 82896 : if (size_0 == 0)
7869 : : {
7870 : 22 : if (complain)
7871 : 7 : error_at (
7872 : : loc, "argument 1 of %qE must be a pointer to a nonzero size object",
7873 : : function);
7874 : 22 : return 0;
7875 : : }
7876 : :
7877 : : /* Check each other parameter is a pointer and the same size. */
7878 : 290885 : for (x = 0; x < n_param - n_model; x++)
7879 : : {
7880 : 208150 : int size;
7881 : 208150 : tree type = TREE_TYPE ((*params)[x]);
7882 : : /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
7883 : 208150 : if (n_param == 6 && x == 3)
7884 : 20970 : continue;
7885 : 187180 : if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
7886 : : {
7887 : : /* Force array-to-pointer decay for C++. */
7888 : 18 : (*params)[x] = default_conversion ((*params)[x]);
7889 : 18 : type = TREE_TYPE ((*params)[x]);
7890 : : }
7891 : 187180 : if (!POINTER_TYPE_P (type))
7892 : : {
7893 : 16 : if (complain)
7894 : 13 : error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
7895 : : function);
7896 : 16 : return 0;
7897 : : }
7898 : 187164 : else if (TYPE_SIZE_UNIT (TREE_TYPE (type))
7899 : 187164 : && TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type))))
7900 : : != INTEGER_CST)
7901 : : {
7902 : 1 : if (complain)
7903 : 1 : error_at (loc,
7904 : : "argument %d of %qE must be a pointer to a constant "
7905 : : "size type",
7906 : : x + 1, function);
7907 : 1 : return 0;
7908 : : }
7909 : 187163 : else if (FUNCTION_POINTER_TYPE_P (type))
7910 : : {
7911 : 10 : if (complain)
7912 : 7 : error_at (loc,
7913 : : "argument %d of %qE must not be a pointer to a "
7914 : : "function",
7915 : : x + 1, function);
7916 : 10 : return 0;
7917 : : }
7918 : 187153 : tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
7919 : 187153 : size = type_size ? tree_to_uhwi (type_size) : 0;
7920 : 187153 : if (size != size_0)
7921 : : {
7922 : 34 : if (complain)
7923 : 25 : error_at (loc, "size mismatch in argument %d of %qE", x + 1,
7924 : : function);
7925 : 34 : return 0;
7926 : : }
7927 : :
7928 : 187119 : {
7929 : 187119 : auto_diagnostic_group d;
7930 : 187119 : int quals = TYPE_QUALS (TREE_TYPE (type));
7931 : : /* Must not write to an argument of a const-qualified type. */
7932 : 187119 : if (outputs & (1 << x) && quals & TYPE_QUAL_CONST)
7933 : : {
7934 : 57 : if (c_dialect_cxx ())
7935 : : {
7936 : 45 : if (complain)
7937 : 42 : error_at (loc,
7938 : : "argument %d of %qE must not be a pointer to "
7939 : : "a %<const%> type",
7940 : : x + 1, function);
7941 : 45 : return 0;
7942 : : }
7943 : : else
7944 : 12 : pedwarn (loc, OPT_Wdiscarded_qualifiers, "argument %d "
7945 : : "of %qE discards %<const%> qualifier", x + 1,
7946 : : function);
7947 : : }
7948 : : /* Only the first argument is allowed to be volatile. */
7949 : 187074 : if (x > 0 && quals & TYPE_QUAL_VOLATILE)
7950 : : {
7951 : 44 : if (c_dialect_cxx ())
7952 : : {
7953 : 33 : if (complain)
7954 : 30 : error_at (loc,
7955 : : "argument %d of %qE must not be a pointer to "
7956 : : "a %<volatile%> type",
7957 : : x + 1, function);
7958 : 33 : return 0;
7959 : : }
7960 : : else
7961 : 11 : pedwarn (loc, OPT_Wdiscarded_qualifiers, "argument %d "
7962 : : "of %qE discards %<volatile%> qualifier", x + 1,
7963 : : function);
7964 : : }
7965 : 187119 : }
7966 : : }
7967 : :
7968 : : /* Check memory model parameters for validity. */
7969 : 186424 : for (x = n_param - n_model ; x < n_param; x++)
7970 : : {
7971 : 103705 : tree p = (*params)[x];
7972 : 103705 : if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
7973 : : {
7974 : 10 : if (complain)
7975 : 7 : error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
7976 : : function);
7977 : 10 : return 0;
7978 : : }
7979 : 103695 : p = fold_for_warn (p);
7980 : 103695 : if (TREE_CODE (p) == INTEGER_CST)
7981 : : {
7982 : : /* memmodel_base masks the low 16 bits, thus ignore any bits above
7983 : : it by using TREE_INT_CST_LOW instead of tree_to_*hwi. Those high
7984 : : bits will be checked later during expansion in target specific
7985 : : way. */
7986 : 79316 : if (memmodel_base (TREE_INT_CST_LOW (p)) >= MEMMODEL_LAST)
7987 : : {
7988 : 20 : if (complain)
7989 : 14 : warning_at (loc, OPT_Winvalid_memory_model,
7990 : : "invalid memory model argument %d of %qE", x + 1,
7991 : : function);
7992 : : else
7993 : : return 0;
7994 : : }
7995 : : }
7996 : : }
7997 : :
7998 : : return size_0;
7999 : : }
8000 : :
8001 : :
8002 : : /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
8003 : : at the beginning of the parameter list PARAMS representing the size of the
8004 : : objects. This is to match the library ABI requirement. LOC is the location
8005 : : of the function call.
8006 : : The new function is returned if it needed rebuilding, otherwise NULL_TREE is
8007 : : returned to allow the external call to be constructed. */
8008 : :
8009 : : static tree
8010 : 4146 : add_atomic_size_parameter (unsigned n, location_t loc, tree function,
8011 : : vec<tree, va_gc> *params)
8012 : : {
8013 : 4146 : tree size_node;
8014 : :
8015 : : /* Insert a SIZE_T parameter as the first param. If there isn't
8016 : : enough space, allocate a new vector and recursively re-build with that. */
8017 : 4146 : if (!params->space (1))
8018 : : {
8019 : 33 : unsigned int z, len;
8020 : 33 : vec<tree, va_gc> *v;
8021 : 33 : tree f;
8022 : :
8023 : 33 : len = params->length ();
8024 : 33 : vec_alloc (v, len + 1);
8025 : 33 : v->quick_push (build_int_cst (size_type_node, n));
8026 : 165 : for (z = 0; z < len; z++)
8027 : 132 : v->quick_push ((*params)[z]);
8028 : 33 : f = build_function_call_vec (loc, vNULL, function, v, NULL);
8029 : 33 : vec_free (v);
8030 : 33 : return f;
8031 : : }
8032 : :
8033 : : /* Add the size parameter and leave as a function call for processing. */
8034 : 4113 : size_node = build_int_cst (size_type_node, n);
8035 : 4113 : params->quick_insert (0, size_node);
8036 : 4113 : return NULL_TREE;
8037 : : }
8038 : :
8039 : :
8040 : : /* Return whether atomic operations for naturally aligned N-byte
8041 : : arguments are supported, whether inline or through libatomic. */
8042 : : static bool
8043 : 82719 : atomic_size_supported_p (int n)
8044 : : {
8045 : 82719 : switch (n)
8046 : : {
8047 : : case 1:
8048 : : case 2:
8049 : : case 4:
8050 : : case 8:
8051 : : return true;
8052 : :
8053 : 6972 : case 16:
8054 : 6972 : return targetm.scalar_mode_supported_p (TImode);
8055 : :
8056 : 4146 : default:
8057 : 4146 : return false;
8058 : : }
8059 : : }
8060 : :
8061 : : /* This will process an __atomic_exchange function call, determine whether it
8062 : : needs to be mapped to the _N variation, or turned into a library call.
8063 : : LOC is the location of the builtin call.
8064 : : FUNCTION is the DECL that has been invoked;
8065 : : PARAMS is the argument list for the call. The return value is non-null
8066 : : TRUE is returned if it is translated into the proper format for a call to the
8067 : : external library, and NEW_RETURN is set the tree for that function.
8068 : : FALSE is returned if processing for the _N variation is required, and
8069 : : NEW_RETURN is set to the return value the result is copied into. */
8070 : : static bool
8071 : 654 : resolve_overloaded_atomic_exchange (location_t loc, tree function,
8072 : : vec<tree, va_gc> *params, tree *new_return,
8073 : : bool complain)
8074 : : {
8075 : 654 : tree p0, p1, p2, p3;
8076 : 654 : tree I_type, I_type_ptr;
8077 : 654 : int n = get_atomic_generic_size (loc, function, params, complain);
8078 : :
8079 : : /* Size of 0 is an error condition. */
8080 : 654 : if (n == 0)
8081 : : {
8082 : 195 : *new_return = error_mark_node;
8083 : 195 : return true;
8084 : : }
8085 : :
8086 : : /* If not a lock-free size, change to the library generic format. */
8087 : 459 : if (!atomic_size_supported_p (n))
8088 : : {
8089 : 39 : *new_return = add_atomic_size_parameter (n, loc, function, params);
8090 : 39 : return true;
8091 : : }
8092 : :
8093 : : /* Otherwise there is a lockfree match, transform the call from:
8094 : : void fn(T* mem, T* desired, T* return, model)
8095 : : into
8096 : : *return = (T) (fn (In* mem, (In) *desired, model)) */
8097 : :
8098 : 420 : p0 = (*params)[0];
8099 : 420 : p1 = (*params)[1];
8100 : 420 : p2 = (*params)[2];
8101 : 420 : p3 = (*params)[3];
8102 : :
8103 : : /* Create pointer to appropriate size. */
8104 : 420 : I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
8105 : 420 : I_type_ptr = build_pointer_type (I_type);
8106 : :
8107 : : /* Convert object pointer to required type. */
8108 : 420 : p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
8109 : 420 : (*params)[0] = p0;
8110 : : /* Convert new value to required type, and dereference it.
8111 : : If *p1 type can have padding or may involve floating point which
8112 : : could e.g. be promoted to wider precision and demoted afterwards,
8113 : : state of padding bits might not be preserved. */
8114 : 420 : build_indirect_ref (loc, p1, RO_UNARY_STAR);
8115 : 420 : p1 = build2_loc (loc, MEM_REF, I_type,
8116 : : build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1),
8117 : 420 : build_zero_cst (TREE_TYPE (p1)));
8118 : 420 : (*params)[1] = p1;
8119 : :
8120 : : /* Move memory model to the 3rd position, and end param list. */
8121 : 420 : (*params)[2] = p3;
8122 : 420 : params->truncate (3);
8123 : :
8124 : : /* Convert return pointer and dereference it for later assignment. */
8125 : 420 : *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
8126 : :
8127 : 420 : return false;
8128 : : }
8129 : :
8130 : : /* This will process an __atomic_compare_exchange function call, determine
8131 : : whether it needs to be mapped to the _N variation, or turned into a lib call.
8132 : : LOC is the location of the builtin call.
8133 : : FUNCTION is the DECL that has been invoked;
8134 : : PARAMS is the argument list for the call. The return value is non-null
8135 : : TRUE is returned if it is translated into the proper format for a call to the
8136 : : external library, and NEW_RETURN is set the tree for that function.
8137 : : FALSE is returned if processing for the _N variation is required. */
8138 : :
8139 : : static bool
8140 : 21148 : resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
8141 : : vec<tree, va_gc> *params,
8142 : : tree *new_return, bool complain)
8143 : : {
8144 : 21148 : tree p0, p1, p2;
8145 : 21148 : tree I_type, I_type_ptr;
8146 : 21148 : int n = get_atomic_generic_size (loc, function, params, complain);
8147 : :
8148 : : /* Size of 0 is an error condition. */
8149 : 21148 : if (n == 0)
8150 : : {
8151 : 178 : *new_return = error_mark_node;
8152 : 178 : return true;
8153 : : }
8154 : :
8155 : : /* If not a lock-free size, change to the library generic format. */
8156 : 20970 : if (!atomic_size_supported_p (n))
8157 : : {
8158 : : /* The library generic format does not have the weak parameter, so
8159 : : remove it from the param list. Since a parameter has been removed,
8160 : : we can be sure that there is room for the SIZE_T parameter, meaning
8161 : : there will not be a recursive rebuilding of the parameter list, so
8162 : : there is no danger this will be done twice. */
8163 : 1198 : if (n > 0)
8164 : : {
8165 : 1198 : (*params)[3] = (*params)[4];
8166 : 1198 : (*params)[4] = (*params)[5];
8167 : 1198 : params->truncate (5);
8168 : : }
8169 : 1198 : *new_return = add_atomic_size_parameter (n, loc, function, params);
8170 : 1198 : return true;
8171 : : }
8172 : :
8173 : : /* Otherwise, there is a match, so the call needs to be transformed from:
8174 : : bool fn(T* mem, T* desired, T* return, weak, success, failure)
8175 : : into
8176 : : bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
8177 : :
8178 : 19772 : p0 = (*params)[0];
8179 : 19772 : p1 = (*params)[1];
8180 : 19772 : p2 = (*params)[2];
8181 : :
8182 : : /* Create pointer to appropriate size. */
8183 : 19772 : I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
8184 : 19772 : I_type_ptr = build_pointer_type (I_type);
8185 : :
8186 : : /* Convert object pointer to required type. */
8187 : 19772 : p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
8188 : 19772 : (*params)[0] = p0;
8189 : :
8190 : : /* Convert expected pointer to required type. */
8191 : 19772 : p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
8192 : 19772 : (*params)[1] = p1;
8193 : :
8194 : : /* Convert desired value to required type, and dereference it.
8195 : : If *p2 type can have padding or may involve floating point which
8196 : : could e.g. be promoted to wider precision and demoted afterwards,
8197 : : state of padding bits might not be preserved. */
8198 : 19772 : build_indirect_ref (loc, p2, RO_UNARY_STAR);
8199 : 19772 : p2 = build2_loc (loc, MEM_REF, I_type,
8200 : : build1 (VIEW_CONVERT_EXPR, I_type_ptr, p2),
8201 : 19772 : build_zero_cst (TREE_TYPE (p2)));
8202 : 19772 : (*params)[2] = p2;
8203 : :
8204 : : /* The rest of the parameters are fine. NULL means no special return value
8205 : : processing.*/
8206 : 19772 : *new_return = NULL;
8207 : 19772 : return false;
8208 : : }
8209 : :
8210 : : /* This will process an __atomic_load function call, determine whether it
8211 : : needs to be mapped to the _N variation, or turned into a library call.
8212 : : LOC is the location of the builtin call.
8213 : : FUNCTION is the DECL that has been invoked;
8214 : : PARAMS is the argument list for the call. The return value is non-null
8215 : : TRUE is returned if it is translated into the proper format for a call to the
8216 : : external library, and NEW_RETURN is set the tree for that function.
8217 : : FALSE is returned if processing for the _N variation is required, and
8218 : : NEW_RETURN is set to the return value the result is copied into. */
8219 : :
8220 : : static bool
8221 : 50235 : resolve_overloaded_atomic_load (location_t loc, tree function,
8222 : : vec<tree, va_gc> *params, tree *new_return,
8223 : : bool complain)
8224 : : {
8225 : 50235 : tree p0, p1, p2;
8226 : 50235 : tree I_type, I_type_ptr;
8227 : 50235 : int n = get_atomic_generic_size (loc, function, params, complain);
8228 : :
8229 : : /* Size of 0 is an error condition. */
8230 : 50235 : if (n == 0)
8231 : : {
8232 : 278 : *new_return = error_mark_node;
8233 : 278 : return true;
8234 : : }
8235 : :
8236 : : /* If not a lock-free size, change to the library generic format. */
8237 : 49957 : if (!atomic_size_supported_p (n))
8238 : : {
8239 : 2499 : *new_return = add_atomic_size_parameter (n, loc, function, params);
8240 : 2499 : return true;
8241 : : }
8242 : :
8243 : : /* Otherwise, there is a match, so the call needs to be transformed from:
8244 : : void fn(T* mem, T* return, model)
8245 : : into
8246 : : *return = (T) (fn ((In *) mem, model)) */
8247 : :
8248 : 47458 : p0 = (*params)[0];
8249 : 47458 : p1 = (*params)[1];
8250 : 47458 : p2 = (*params)[2];
8251 : :
8252 : : /* Create pointer to appropriate size. */
8253 : 47458 : I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
8254 : 47458 : I_type_ptr = build_pointer_type (I_type);
8255 : :
8256 : : /* Convert object pointer to required type. */
8257 : 47458 : p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
8258 : 47458 : (*params)[0] = p0;
8259 : :
8260 : : /* Move memory model to the 2nd position, and end param list. */
8261 : 47458 : (*params)[1] = p2;
8262 : 47458 : params->truncate (2);
8263 : :
8264 : : /* Convert return pointer and dereference it for later assignment. */
8265 : 47458 : *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
8266 : :
8267 : 47458 : return false;
8268 : : }
8269 : :
8270 : : /* This will process an __atomic_store function call, determine whether it
8271 : : needs to be mapped to the _N variation, or turned into a library call.
8272 : : LOC is the location of the builtin call.
8273 : : FUNCTION is the DECL that has been invoked;
8274 : : PARAMS is the argument list for the call. The return value is non-null
8275 : : TRUE is returned if it is translated into the proper format for a call to the
8276 : : external library, and NEW_RETURN is set the tree for that function.
8277 : : FALSE is returned if processing for the _N variation is required, and
8278 : : NEW_RETURN is set to the return value the result is copied into. */
8279 : :
8280 : : static bool
8281 : 11501 : resolve_overloaded_atomic_store (location_t loc, tree function,
8282 : : vec<tree, va_gc> *params, tree *new_return,
8283 : : bool complain)
8284 : : {
8285 : 11501 : tree p0, p1;
8286 : 11501 : tree I_type, I_type_ptr;
8287 : 11501 : int n = get_atomic_generic_size (loc, function, params, complain);
8288 : :
8289 : : /* Size of 0 is an error condition. */
8290 : 11501 : if (n == 0)
8291 : : {
8292 : 168 : *new_return = error_mark_node;
8293 : 168 : return true;
8294 : : }
8295 : :
8296 : : /* If not a lock-free size, change to the library generic format. */
8297 : 11333 : if (!atomic_size_supported_p (n))
8298 : : {
8299 : 410 : *new_return = add_atomic_size_parameter (n, loc, function, params);
8300 : 410 : return true;
8301 : : }
8302 : :
8303 : : /* Otherwise, there is a match, so the call needs to be transformed from:
8304 : : void fn(T* mem, T* value, model)
8305 : : into
8306 : : fn ((In *) mem, (In) *value, model) */
8307 : :
8308 : 10923 : p0 = (*params)[0];
8309 : 10923 : p1 = (*params)[1];
8310 : :
8311 : : /* Create pointer to appropriate size. */
8312 : 10923 : I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
8313 : 10923 : I_type_ptr = build_pointer_type (I_type);
8314 : :
8315 : : /* Convert object pointer to required type. */
8316 : 10923 : p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
8317 : 10923 : (*params)[0] = p0;
8318 : :
8319 : : /* Convert new value to required type, and dereference it. */
8320 : 10923 : p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
8321 : 10923 : p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
8322 : 10923 : (*params)[1] = p1;
8323 : :
8324 : : /* The memory model is in the right spot already. Return is void. */
8325 : 10923 : *new_return = NULL_TREE;
8326 : :
8327 : 10923 : return false;
8328 : : }
8329 : :
8330 : : /* Emit __atomic*fetch* on _BitInt which doesn't have a size of
8331 : : 1, 2, 4, 8 or 16 bytes using __atomic_compare_exchange loop.
8332 : : ORIG_CODE is the DECL_FUNCTION_CODE of ORIG_FUNCTION and
8333 : : ORIG_PARAMS arguments of the call. */
8334 : :
8335 : : static tree
8336 : 528 : atomic_bitint_fetch_using_cas_loop (location_t loc,
8337 : : enum built_in_function orig_code,
8338 : : tree orig_function,
8339 : : vec<tree, va_gc> *orig_params)
8340 : : {
8341 : 528 : enum tree_code code = ERROR_MARK;
8342 : 528 : bool return_old_p = false;
8343 : 528 : switch (orig_code)
8344 : : {
8345 : 0 : case BUILT_IN_ATOMIC_ADD_FETCH_N:
8346 : 0 : code = PLUS_EXPR;
8347 : 0 : break;
8348 : 1 : case BUILT_IN_ATOMIC_SUB_FETCH_N:
8349 : 1 : code = MINUS_EXPR;
8350 : 1 : break;
8351 : 0 : case BUILT_IN_ATOMIC_AND_FETCH_N:
8352 : 0 : code = BIT_AND_EXPR;
8353 : 0 : break;
8354 : : case BUILT_IN_ATOMIC_NAND_FETCH_N:
8355 : : break;
8356 : 1 : case BUILT_IN_ATOMIC_XOR_FETCH_N:
8357 : 1 : code = BIT_XOR_EXPR;
8358 : 1 : break;
8359 : 0 : case BUILT_IN_ATOMIC_OR_FETCH_N:
8360 : 0 : code = BIT_IOR_EXPR;
8361 : 0 : break;
8362 : 106 : case BUILT_IN_ATOMIC_FETCH_ADD_N:
8363 : 106 : code = PLUS_EXPR;
8364 : 106 : return_old_p = true;
8365 : 106 : break;
8366 : 104 : case BUILT_IN_ATOMIC_FETCH_SUB_N:
8367 : 104 : code = MINUS_EXPR;
8368 : 104 : return_old_p = true;
8369 : 104 : break;
8370 : 105 : case BUILT_IN_ATOMIC_FETCH_AND_N:
8371 : 105 : code = BIT_AND_EXPR;
8372 : 105 : return_old_p = true;
8373 : 105 : break;
8374 : 0 : case BUILT_IN_ATOMIC_FETCH_NAND_N:
8375 : 0 : return_old_p = true;
8376 : 0 : break;
8377 : 104 : case BUILT_IN_ATOMIC_FETCH_XOR_N:
8378 : 104 : code = BIT_XOR_EXPR;
8379 : 104 : return_old_p = true;
8380 : 104 : break;
8381 : 106 : case BUILT_IN_ATOMIC_FETCH_OR_N:
8382 : 106 : code = BIT_IOR_EXPR;
8383 : 106 : return_old_p = true;
8384 : 106 : break;
8385 : 0 : default:
8386 : 0 : gcc_unreachable ();
8387 : : }
8388 : :
8389 : 528 : if (orig_params->length () != 3)
8390 : : {
8391 : 0 : if (orig_params->length () < 3)
8392 : 0 : error_at (loc, "too few arguments to function %qE", orig_function);
8393 : : else
8394 : 0 : error_at (loc, "too many arguments to function %qE", orig_function);
8395 : 0 : return error_mark_node;
8396 : : }
8397 : :
8398 : 528 : tree stmts = push_stmt_list ();
8399 : :
8400 : 528 : tree nonatomic_lhs_type = TREE_TYPE (TREE_TYPE ((*orig_params)[0]));
8401 : 528 : nonatomic_lhs_type = TYPE_MAIN_VARIANT (nonatomic_lhs_type);
8402 : 528 : gcc_assert (TREE_CODE (nonatomic_lhs_type) == BITINT_TYPE);
8403 : :
8404 : 528 : tree lhs_addr = (*orig_params)[0];
8405 : 528 : tree val = convert (nonatomic_lhs_type, (*orig_params)[1]);
8406 : 528 : tree model = convert (integer_type_node, (*orig_params)[2]);
8407 : 528 : if (!c_dialect_cxx ())
8408 : : {
8409 : 528 : lhs_addr = c_fully_fold (lhs_addr, false, NULL);
8410 : 528 : val = c_fully_fold (val, false, NULL);
8411 : 528 : model = c_fully_fold (model, false, NULL);
8412 : : }
8413 : 528 : if (TREE_SIDE_EFFECTS (lhs_addr))
8414 : : {
8415 : 1 : tree var = create_tmp_var_raw (TREE_TYPE (lhs_addr));
8416 : 1 : lhs_addr = build4 (TARGET_EXPR, TREE_TYPE (lhs_addr), var, lhs_addr,
8417 : : NULL_TREE, NULL_TREE);
8418 : 1 : add_stmt (lhs_addr);
8419 : : }
8420 : 528 : if (TREE_SIDE_EFFECTS (val))
8421 : : {
8422 : 1 : tree var = create_tmp_var_raw (nonatomic_lhs_type);
8423 : 1 : val = build4 (TARGET_EXPR, nonatomic_lhs_type, var, val, NULL_TREE,
8424 : : NULL_TREE);
8425 : 1 : add_stmt (val);
8426 : : }
8427 : 528 : if (TREE_SIDE_EFFECTS (model))
8428 : : {
8429 : 1 : tree var = create_tmp_var_raw (integer_type_node);
8430 : 1 : model = build4 (TARGET_EXPR, integer_type_node, var, model, NULL_TREE,
8431 : : NULL_TREE);
8432 : 1 : add_stmt (model);
8433 : : }
8434 : :
8435 : 528 : tree old = create_tmp_var_raw (nonatomic_lhs_type);
8436 : 528 : tree old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
8437 : 528 : TREE_ADDRESSABLE (old) = 1;
8438 : 528 : suppress_warning (old);
8439 : :
8440 : 528 : tree newval = create_tmp_var_raw (nonatomic_lhs_type);
8441 : 528 : tree newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
8442 : 528 : TREE_ADDRESSABLE (newval) = 1;
8443 : 528 : suppress_warning (newval);
8444 : :
8445 : 528 : tree loop_decl = create_artificial_label (loc);
8446 : 528 : tree loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
8447 : :
8448 : 528 : tree done_decl = create_artificial_label (loc);
8449 : 528 : tree done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
8450 : :
8451 : 528 : vec<tree, va_gc> *params;
8452 : 528 : vec_alloc (params, 6);
8453 : :
8454 : : /* __atomic_load (addr, &old, SEQ_CST). */
8455 : 528 : tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
8456 : 528 : params->quick_push (lhs_addr);
8457 : 528 : params->quick_push (old_addr);
8458 : 528 : params->quick_push (build_int_cst (integer_type_node, MEMMODEL_RELAXED));
8459 : 528 : tree func_call = resolve_overloaded_builtin (loc, fndecl, params);
8460 : 528 : if (func_call == NULL_TREE)
8461 : 528 : func_call = build_function_call_vec (loc, vNULL, fndecl, params, NULL);
8462 : 528 : old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
8463 : : NULL_TREE);
8464 : 528 : add_stmt (old);
8465 : 528 : params->truncate (0);
8466 : :
8467 : : /* loop: */
8468 : 528 : add_stmt (loop_label);
8469 : :
8470 : : /* newval = old + val; */
8471 : 528 : tree rhs;
8472 : 528 : switch (code)
8473 : : {
8474 : 211 : case PLUS_EXPR:
8475 : 211 : case MINUS_EXPR:
8476 : 211 : if (!TYPE_OVERFLOW_WRAPS (nonatomic_lhs_type))
8477 : : {
8478 : 211 : tree utype
8479 : 211 : = build_bitint_type (TYPE_PRECISION (nonatomic_lhs_type), 1);
8480 : 211 : rhs = convert (nonatomic_lhs_type,
8481 : : build2_loc (loc, code, utype,
8482 : : convert (utype, old),
8483 : : convert (utype, val)));
8484 : : }
8485 : : else
8486 : 0 : rhs = build2_loc (loc, code, nonatomic_lhs_type, old, val);
8487 : : break;
8488 : 316 : case BIT_AND_EXPR:
8489 : 316 : case BIT_IOR_EXPR:
8490 : 316 : case BIT_XOR_EXPR:
8491 : 316 : rhs = build2_loc (loc, code, nonatomic_lhs_type, old, val);
8492 : 316 : break;
8493 : 1 : case ERROR_MARK:
8494 : 1 : rhs = build2_loc (loc, BIT_AND_EXPR, nonatomic_lhs_type,
8495 : : build1_loc (loc, BIT_NOT_EXPR,
8496 : : nonatomic_lhs_type, old), val);
8497 : 1 : break;
8498 : 0 : default:
8499 : 0 : gcc_unreachable ();
8500 : : }
8501 : 528 : rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
8502 : : NULL_TREE);
8503 : 528 : SET_EXPR_LOCATION (rhs, loc);
8504 : 528 : add_stmt (rhs);
8505 : :
8506 : : /* if (__atomic_compare_exchange (addr, &old, &new, false, model, model))
8507 : : goto done; */
8508 : 528 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
8509 : 528 : params->quick_push (lhs_addr);
8510 : 528 : params->quick_push (old_addr);
8511 : 528 : params->quick_push (newval_addr);
8512 : 528 : params->quick_push (integer_zero_node);
8513 : 528 : params->quick_push (model);
8514 : 528 : if (tree_fits_uhwi_p (model)
8515 : 527 : && (tree_to_uhwi (model) == MEMMODEL_RELEASE
8516 : 447 : || tree_to_uhwi (model) == MEMMODEL_ACQ_REL))
8517 : 129 : params->quick_push (build_int_cst (integer_type_node, MEMMODEL_RELAXED));
8518 : : else
8519 : 399 : params->quick_push (model);
8520 : 528 : func_call = resolve_overloaded_builtin (loc, fndecl, params);
8521 : 528 : if (func_call == NULL_TREE)
8522 : 528 : func_call = build_function_call_vec (loc, vNULL, fndecl, params, NULL);
8523 : :
8524 : 528 : tree goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
8525 : 528 : SET_EXPR_LOCATION (goto_stmt, loc);
8526 : :
8527 : 528 : tree stmt
8528 : 528 : = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
8529 : 528 : SET_EXPR_LOCATION (stmt, loc);
8530 : 528 : add_stmt (stmt);
8531 : :
8532 : : /* goto loop; */
8533 : 528 : goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
8534 : 528 : SET_EXPR_LOCATION (goto_stmt, loc);
8535 : 528 : add_stmt (goto_stmt);
8536 : :
8537 : : /* done: */
8538 : 528 : add_stmt (done_label);
8539 : :
8540 : 528 : tree ret = create_tmp_var_raw (nonatomic_lhs_type);
8541 : 531 : stmt = build2_loc (loc, MODIFY_EXPR, void_type_node, ret,
8542 : : return_old_p ? old : newval);
8543 : 528 : add_stmt (stmt);
8544 : :
8545 : : /* Finish the compound statement. */
8546 : 528 : stmts = pop_stmt_list (stmts);
8547 : :
8548 : 528 : return build4 (TARGET_EXPR, nonatomic_lhs_type, ret, stmts, NULL_TREE,
8549 : 528 : NULL_TREE);
8550 : : }
8551 : :
8552 : :
8553 : : /* Some builtin functions are placeholders for other expressions. This
8554 : : function should be called immediately after parsing the call expression
8555 : : before surrounding code has committed to the type of the expression.
8556 : :
8557 : : LOC is the location of the builtin call.
8558 : :
8559 : : FUNCTION is the DECL that has been invoked; it is known to be a builtin.
8560 : : PARAMS is the argument list for the call. The return value is non-null
8561 : : when expansion is complete, and null if normal processing should
8562 : : continue. */
8563 : :
8564 : : tree
8565 : 58362896 : resolve_overloaded_builtin (location_t loc, tree function,
8566 : : vec<tree, va_gc> *params, bool complain)
8567 : : {
8568 : : /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
8569 : : Those are not valid to call with a pointer to _Bool (or C++ bool)
8570 : : and so must be rejected. */
8571 : 58362896 : bool fetch_op = true;
8572 : 58362896 : bool orig_format = true;
8573 : 58362896 : tree new_return = NULL_TREE;
8574 : :
8575 : 58362896 : switch (DECL_BUILT_IN_CLASS (function))
8576 : : {
8577 : 8982522 : case BUILT_IN_NORMAL:
8578 : 8982522 : break;
8579 : 32898678 : case BUILT_IN_MD:
8580 : 32898678 : if (targetm.resolve_overloaded_builtin)
8581 : 0 : return targetm.resolve_overloaded_builtin (loc, function, params,
8582 : 0 : complain);
8583 : : else
8584 : : return NULL_TREE;
8585 : : default:
8586 : : return NULL_TREE;
8587 : : }
8588 : :
8589 : : /* Handle BUILT_IN_NORMAL here. */
8590 : 8982522 : enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
8591 : 8982522 : switch (orig_code)
8592 : : {
8593 : 154 : case BUILT_IN_SPECULATION_SAFE_VALUE_N:
8594 : 154 : {
8595 : 154 : tree new_function, first_param, result;
8596 : 154 : enum built_in_function fncode
8597 : 154 : = speculation_safe_value_resolve_call (function, params, complain);
8598 : :
8599 : 154 : if (fncode == BUILT_IN_NONE)
8600 : 52 : return error_mark_node;
8601 : :
8602 : 102 : first_param = (*params)[0];
8603 : 102 : if (!speculation_safe_value_resolve_params (loc, function, params,
8604 : : complain))
8605 : 31 : return error_mark_node;
8606 : :
8607 : 71 : if (targetm.have_speculation_safe_value (true))
8608 : : {
8609 : 71 : new_function = builtin_decl_explicit (fncode);
8610 : 71 : result = build_function_call_vec (loc, vNULL, new_function, params,
8611 : : NULL);
8612 : :
8613 : 71 : if (result == error_mark_node)
8614 : : return result;
8615 : :
8616 : 71 : return speculation_safe_value_resolve_return (first_param, result);
8617 : : }
8618 : : else
8619 : : {
8620 : : /* This target doesn't have, or doesn't need, active mitigation
8621 : : against incorrect speculative execution. Simply return the
8622 : : first parameter to the builtin. */
8623 : 0 : if (!targetm.have_speculation_safe_value (false))
8624 : : {
8625 : 0 : if (complain)
8626 : : /* The user has invoked __builtin_speculation_safe_value
8627 : : even though __HAVE_SPECULATION_SAFE_VALUE is not
8628 : : defined: emit a warning. */
8629 : 0 : warning_at (
8630 : 0 : input_location, 0,
8631 : : "this target does not define a speculation barrier; "
8632 : : "your program will still execute correctly, "
8633 : : "but incorrect speculation may not be "
8634 : : "restricted");
8635 : : else
8636 : 0 : return error_mark_node;
8637 : : }
8638 : :
8639 : : /* If the optional second argument is present, handle any side
8640 : : effects now. */
8641 : 0 : if (params->length () == 2
8642 : 0 : && TREE_SIDE_EFFECTS ((*params)[1]))
8643 : 0 : return build2 (COMPOUND_EXPR, TREE_TYPE (first_param),
8644 : 0 : (*params)[1], first_param);
8645 : :
8646 : : return first_param;
8647 : : }
8648 : : }
8649 : :
8650 : 83538 : case BUILT_IN_ATOMIC_EXCHANGE:
8651 : 83538 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
8652 : 83538 : case BUILT_IN_ATOMIC_LOAD:
8653 : 83538 : case BUILT_IN_ATOMIC_STORE:
8654 : 83538 : {
8655 : : /* Handle these 4 together so that they can fall through to the next
8656 : : case if the call is transformed to an _N variant. */
8657 : 83538 : switch (orig_code)
8658 : : {
8659 : 654 : case BUILT_IN_ATOMIC_EXCHANGE:
8660 : 654 : {
8661 : 654 : if (resolve_overloaded_atomic_exchange (loc, function, params,
8662 : : &new_return, complain))
8663 : 234 : return new_return;
8664 : : /* Change to the _N variant. */
8665 : : orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
8666 : : break;
8667 : : }
8668 : :
8669 : 21148 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
8670 : 21148 : {
8671 : 21148 : if (resolve_overloaded_atomic_compare_exchange (
8672 : : loc, function, params, &new_return, complain))
8673 : 1376 : return new_return;
8674 : : /* Change to the _N variant. */
8675 : : orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
8676 : : break;
8677 : : }
8678 : 50235 : case BUILT_IN_ATOMIC_LOAD:
8679 : 50235 : {
8680 : 50235 : if (resolve_overloaded_atomic_load (loc, function, params,
8681 : : &new_return, complain))
8682 : 2777 : return new_return;
8683 : : /* Change to the _N variant. */
8684 : : orig_code = BUILT_IN_ATOMIC_LOAD_N;
8685 : : break;
8686 : : }
8687 : 11501 : case BUILT_IN_ATOMIC_STORE:
8688 : 11501 : {
8689 : 11501 : if (resolve_overloaded_atomic_store (loc, function, params,
8690 : : &new_return, complain))
8691 : 578 : return new_return;
8692 : : /* Change to the _N variant. */
8693 : : orig_code = BUILT_IN_ATOMIC_STORE_N;
8694 : : break;
8695 : : }
8696 : 0 : default:
8697 : 0 : gcc_unreachable ();
8698 : : }
8699 : : }
8700 : : /* FALLTHRU */
8701 : : case BUILT_IN_ATOMIC_EXCHANGE_N:
8702 : : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
8703 : : case BUILT_IN_ATOMIC_LOAD_N:
8704 : : case BUILT_IN_ATOMIC_STORE_N:
8705 : : fetch_op = false;
8706 : : /* FALLTHRU */
8707 : : case BUILT_IN_ATOMIC_ADD_FETCH_N:
8708 : : case BUILT_IN_ATOMIC_SUB_FETCH_N:
8709 : : case BUILT_IN_ATOMIC_AND_FETCH_N:
8710 : : case BUILT_IN_ATOMIC_NAND_FETCH_N:
8711 : : case BUILT_IN_ATOMIC_XOR_FETCH_N:
8712 : : case BUILT_IN_ATOMIC_OR_FETCH_N:
8713 : : case BUILT_IN_ATOMIC_FETCH_ADD_N:
8714 : : case BUILT_IN_ATOMIC_FETCH_SUB_N:
8715 : : case BUILT_IN_ATOMIC_FETCH_AND_N:
8716 : : case BUILT_IN_ATOMIC_FETCH_NAND_N:
8717 : : case BUILT_IN_ATOMIC_FETCH_XOR_N:
8718 : : case BUILT_IN_ATOMIC_FETCH_OR_N:
8719 : : orig_format = false;
8720 : : /* FALLTHRU */
8721 : : case BUILT_IN_SYNC_FETCH_AND_ADD_N:
8722 : : case BUILT_IN_SYNC_FETCH_AND_SUB_N:
8723 : : case BUILT_IN_SYNC_FETCH_AND_OR_N:
8724 : : case BUILT_IN_SYNC_FETCH_AND_AND_N:
8725 : : case BUILT_IN_SYNC_FETCH_AND_XOR_N:
8726 : : case BUILT_IN_SYNC_FETCH_AND_NAND_N:
8727 : : case BUILT_IN_SYNC_ADD_AND_FETCH_N:
8728 : : case BUILT_IN_SYNC_SUB_AND_FETCH_N:
8729 : : case BUILT_IN_SYNC_OR_AND_FETCH_N:
8730 : : case BUILT_IN_SYNC_AND_AND_FETCH_N:
8731 : : case BUILT_IN_SYNC_XOR_AND_FETCH_N:
8732 : : case BUILT_IN_SYNC_NAND_AND_FETCH_N:
8733 : : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
8734 : : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
8735 : : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
8736 : : case BUILT_IN_SYNC_LOCK_RELEASE_N:
8737 : : {
8738 : : /* The following are not _FETCH_OPs and must be accepted with
8739 : : pointers to _Bool (or C++ bool). */
8740 : : if (fetch_op)
8741 : 68509 : fetch_op = (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
8742 : 68509 : && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
8743 : 68023 : && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
8744 : 68509 : && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
8745 : :
8746 : 325345 : int n = sync_resolve_size (function, params, fetch_op, orig_format,
8747 : : complain);
8748 : 325345 : tree new_function, first_param, result;
8749 : 325345 : enum built_in_function fncode;
8750 : :
8751 : 325345 : if (n == 0)
8752 : 2609 : return error_mark_node;
8753 : :
8754 : 322736 : if (n == -1)
8755 : : {
8756 : : /* complain is related to SFINAE context.
8757 : : _BitInt is not defined in C++, hence can't enter this clause
8758 : : with complain unset. Even if at the abstraction level
8759 : : this complain is unset that still makes sense (whether
8760 : : this function should report an error or not if anything is
8761 : : wrong).
8762 : : Since can't test avoiding an error when this value is false not
8763 : : writing the code and instead asserting value is not set. */
8764 : 528 : gcc_assert (complain);
8765 : 528 : return atomic_bitint_fetch_using_cas_loop (loc, orig_code, function,
8766 : 528 : params);
8767 : : }
8768 : :
8769 : 322208 : fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
8770 : 322208 : new_function = builtin_decl_explicit (fncode);
8771 : 322208 : if (!sync_resolve_params (loc, function, new_function, params,
8772 : : orig_format, complain))
8773 : 164 : return error_mark_node;
8774 : :
8775 : 322044 : first_param = (*params)[0];
8776 : 322044 : result = build_function_call_vec (loc, vNULL, new_function, params,
8777 : : NULL);
8778 : 322044 : if (result == error_mark_node)
8779 : : return result;
8780 : 322032 : if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
8781 : 322032 : && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
8782 : 321725 : && orig_code != BUILT_IN_ATOMIC_STORE_N
8783 : 321725 : && orig_code != BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N)
8784 : 213111 : result = sync_resolve_return (first_param, result, orig_format);
8785 : :
8786 : 322032 : if (fetch_op)
8787 : : /* Prevent -Wunused-value warning. */
8788 : 65002 : TREE_USED (result) = true;
8789 : :
8790 : : /* If new_return is set, assign function to that expr and cast the
8791 : : result to void since the generic interface returned void. */
8792 : 322032 : if (new_return)
8793 : : {
8794 : : /* Cast function result from I{1,2,4,8,16} to the required type. */
8795 : 47878 : if (TREE_CODE (TREE_TYPE (new_return)) == BITINT_TYPE)
8796 : : {
8797 : 599 : struct bitint_info info;
8798 : 599 : unsigned prec = TYPE_PRECISION (TREE_TYPE (new_return));
8799 : 599 : targetm.c.bitint_type_info (prec, &info);
8800 : 599 : if (!info.extended)
8801 : : /* For _BitInt which has the padding bits undefined
8802 : : convert to the _BitInt type rather than VCE to force
8803 : : zero or sign extension. */
8804 : 599 : result = build1 (NOP_EXPR, TREE_TYPE (new_return), result);
8805 : : }
8806 : 47878 : result
8807 : 47878 : = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
8808 : 47878 : result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
8809 : : result);
8810 : 47878 : TREE_SIDE_EFFECTS (result) = 1;
8811 : 47878 : protected_set_expr_location (result, loc);
8812 : 47878 : result = convert (void_type_node, result);
8813 : : }
8814 : : return result;
8815 : : }
8816 : :
8817 : : default:
8818 : : return NULL_TREE;
8819 : : }
8820 : : }
8821 : :
8822 : : /* vector_types_compatible_elements_p is used in type checks of vectors
8823 : : values used as operands of binary operators. Where it returns true, and
8824 : : the other checks of the caller succeed (being vector types in he first
8825 : : place, and matching number of elements), we can just treat the types
8826 : : as essentially the same.
8827 : : Contrast with vector_targets_convertible_p, which is used for vector
8828 : : pointer types, and vector_types_convertible_p, which will allow
8829 : : language-specific matches under the control of flag_lax_vector_conversions,
8830 : : and might still require a conversion. */
8831 : : /* True if vector types T1 and T2 can be inputs to the same binary
8832 : : operator without conversion.
8833 : : We don't check the overall vector size here because some of our callers
8834 : : want to give different error messages when the vectors are compatible
8835 : : except for the element count. */
8836 : :
8837 : : bool
8838 : 1633457 : vector_types_compatible_elements_p (tree t1, tree t2)
8839 : : {
8840 : 3266638 : bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
8841 : 1633457 : t1 = TREE_TYPE (t1);
8842 : 1633457 : t2 = TREE_TYPE (t2);
8843 : :
8844 : 1633457 : enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
8845 : :
8846 : 1633457 : gcc_assert ((INTEGRAL_TYPE_P (t1)
8847 : : || c1 == REAL_TYPE
8848 : : || c1 == FIXED_POINT_TYPE)
8849 : : && (INTEGRAL_TYPE_P (t2)
8850 : : || c2 == REAL_TYPE
8851 : : || c2 == FIXED_POINT_TYPE));
8852 : :
8853 : 1633457 : t1 = c_common_signed_type (t1);
8854 : 1633457 : t2 = c_common_signed_type (t2);
8855 : : /* Equality works here because c_common_signed_type uses
8856 : : TYPE_MAIN_VARIANT. */
8857 : 1633457 : if (t1 == t2)
8858 : : return true;
8859 : 31 : if (opaque && c1 == c2
8860 : 4 : && (INTEGRAL_TYPE_P (t1) || c1 == REAL_TYPE)
8861 : 35 : && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
8862 : 4 : return true;
8863 : : return false;
8864 : : }
8865 : :
8866 : : /* Check for missing format attributes on function pointers. LTYPE is
8867 : : the new type or left-hand side type. RTYPE is the old type or
8868 : : right-hand side type. Returns TRUE if LTYPE is missing the desired
8869 : : attribute. */
8870 : :
8871 : : bool
8872 : 9930 : check_missing_format_attribute (tree ltype, tree rtype)
8873 : : {
8874 : 9930 : tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
8875 : 9930 : tree ra;
8876 : :
8877 : 9939 : for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
8878 : 73 : if (is_attribute_p ("format", get_attribute_name (ra)))
8879 : : break;
8880 : 9930 : if (ra)
8881 : : {
8882 : 64 : tree la;
8883 : 64 : for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
8884 : 24 : if (is_attribute_p ("format", get_attribute_name (la)))
8885 : : break;
8886 : 64 : return !la;
8887 : : }
8888 : : else
8889 : : return false;
8890 : : }
8891 : :
8892 : : /* Setup a TYPE_DECL node as a typedef representation.
8893 : :
8894 : : X is a TYPE_DECL for a typedef statement. Create a brand new
8895 : : ..._TYPE node (which will be just a variant of the existing
8896 : : ..._TYPE node with identical properties) and then install X
8897 : : as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
8898 : :
8899 : : The whole point here is to end up with a situation where each
8900 : : and every ..._TYPE node the compiler creates will be uniquely
8901 : : associated with AT MOST one node representing a typedef name.
8902 : : This way, even though the compiler substitutes corresponding
8903 : : ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
8904 : : early on, later parts of the compiler can always do the reverse
8905 : : translation and get back the corresponding typedef name. For
8906 : : example, given:
8907 : :
8908 : : typedef struct S MY_TYPE;
8909 : : MY_TYPE object;
8910 : :
8911 : : Later parts of the compiler might only know that `object' was of
8912 : : type `struct S' if it were not for code just below. With this
8913 : : code however, later parts of the compiler see something like:
8914 : :
8915 : : struct S' == struct S
8916 : : typedef struct S' MY_TYPE;
8917 : : struct S' object;
8918 : :
8919 : : And they can then deduce (from the node for type struct S') that
8920 : : the original object declaration was:
8921 : :
8922 : : MY_TYPE object;
8923 : :
8924 : : Being able to do this is important for proper support of protoize,
8925 : : and also for generating precise symbolic debugging information
8926 : : which takes full account of the programmer's (typedef) vocabulary.
8927 : :
8928 : : Obviously, we don't want to generate a duplicate ..._TYPE node if
8929 : : the TYPE_DECL node that we are now processing really represents a
8930 : : standard built-in type. */
8931 : :
8932 : : void
8933 : 174732502 : set_underlying_type (tree x)
8934 : : {
8935 : 174732502 : if (x == error_mark_node || TREE_TYPE (x) == error_mark_node)
8936 : : return;
8937 : 174732474 : if (DECL_IS_UNDECLARED_BUILTIN (x) && TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
8938 : : {
8939 : 6375745 : if (TYPE_NAME (TREE_TYPE (x)) == 0)
8940 : 5093776 : TYPE_NAME (TREE_TYPE (x)) = x;
8941 : : }
8942 : 168356729 : else if (DECL_ORIGINAL_TYPE (x))
8943 : 165787 : gcc_checking_assert (TYPE_NAME (TREE_TYPE (x)) == x);
8944 : : else
8945 : : {
8946 : 168190942 : tree tt = TREE_TYPE (x);
8947 : 168190942 : DECL_ORIGINAL_TYPE (x) = tt;
8948 : 168190942 : tt = build_variant_type_copy (tt);
8949 : 168190942 : TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
8950 : 168190942 : TYPE_NAME (tt) = x;
8951 : :
8952 : : /* Mark the type as used only when its type decl is decorated
8953 : : with attribute unused. */
8954 : 168190942 : if (lookup_attribute ("unused", DECL_ATTRIBUTES (x)))
8955 : 254 : TREE_USED (tt) = 1;
8956 : :
8957 : 168190942 : TREE_TYPE (x) = tt;
8958 : : }
8959 : : }
8960 : :
8961 : : /* Return true if it is worth exposing the DECL_ORIGINAL_TYPE of TYPE to
8962 : : the user in diagnostics, false if it would be better to use TYPE itself.
8963 : : TYPE is known to satisfy typedef_variant_p. */
8964 : :
8965 : : bool
8966 : 31501 : user_facing_original_type_p (const_tree type)
8967 : : {
8968 : 31501 : gcc_assert (typedef_variant_p (type));
8969 : 31501 : tree decl = TYPE_NAME (type);
8970 : :
8971 : : /* Look through any typedef in "user" code. */
8972 : 31501 : if (!DECL_IN_SYSTEM_HEADER (decl) && !DECL_IS_UNDECLARED_BUILTIN (decl))
8973 : : return true;
8974 : :
8975 : : /* If the original type is also named and is in the user namespace,
8976 : : assume it too is a user-facing type. */
8977 : 1576 : tree orig_type = DECL_ORIGINAL_TYPE (decl);
8978 : 1576 : if (tree orig_id = TYPE_IDENTIFIER (orig_type))
8979 : 1448 : if (!name_reserved_for_implementation_p (IDENTIFIER_POINTER (orig_id)))
8980 : : return true;
8981 : :
8982 : 698 : switch (TREE_CODE (orig_type))
8983 : : {
8984 : : /* Don't look through to an anonymous vector type, since the syntax
8985 : : we use for them in diagnostics isn't real C or C++ syntax.
8986 : : And if ORIG_TYPE is named but in the implementation namespace,
8987 : : TYPE is likely to be more meaningful to the user. */
8988 : : case VECTOR_TYPE:
8989 : : return false;
8990 : :
8991 : : /* Don't expose anonymous tag types that are presumably meant to be
8992 : : known by their typedef name. Also don't expose tags that are in
8993 : : the implementation namespace, such as:
8994 : :
8995 : : typedef struct __foo foo; */
8996 : : case RECORD_TYPE:
8997 : : case UNION_TYPE:
8998 : : case ENUMERAL_TYPE:
8999 : : return false;
9000 : :
9001 : : /* Look through to anything else. */
9002 : : default:
9003 : : return true;
9004 : : }
9005 : : }
9006 : :
9007 : : /* Record the types used by the current global variable declaration
9008 : : being parsed, so that we can decide later to emit their debug info.
9009 : : Those types are in types_used_by_cur_var_decl, and we are going to
9010 : : store them in the types_used_by_vars_hash hash table.
9011 : : DECL is the declaration of the global variable that has been parsed. */
9012 : :
9013 : : void
9014 : 23690272 : record_types_used_by_current_var_decl (tree decl)
9015 : : {
9016 : 23690272 : gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
9017 : :
9018 : 45508062 : while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
9019 : : {
9020 : 21817790 : tree type = types_used_by_cur_var_decl->pop ();
9021 : 21817790 : types_used_by_var_decl_insert (type, decl);
9022 : : }
9023 : 23690272 : }
9024 : :
9025 : : /* The C and C++ parsers both use vectors to hold function arguments.
9026 : : For efficiency, we keep a cache of unused vectors. This is the
9027 : : cache. */
9028 : :
9029 : : typedef vec<tree, va_gc> *tree_gc_vec;
9030 : : static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
9031 : :
9032 : : /* Return a new vector from the cache. If the cache is empty,
9033 : : allocate a new vector. These vectors are GC'ed, so it is OK if the
9034 : : pointer is not released.. */
9035 : :
9036 : : vec<tree, va_gc> *
9037 : 1042580626 : make_tree_vector (void)
9038 : : {
9039 : 1042580626 : if (tree_vector_cache && !tree_vector_cache->is_empty ())
9040 : 1013476176 : return tree_vector_cache->pop ();
9041 : : else
9042 : : {
9043 : : /* Passing 0 to vec::alloc returns NULL, and our callers require
9044 : : that we always return a non-NULL value. The vector code uses
9045 : : 4 when growing a NULL vector, so we do too. */
9046 : 29104450 : vec<tree, va_gc> *v;
9047 : 29104450 : vec_alloc (v, 4);
9048 : 29104450 : return v;
9049 : : }
9050 : : }
9051 : :
9052 : : /* Release a vector of trees back to the cache. */
9053 : :
9054 : : void
9055 : 1413618382 : release_tree_vector (vec<tree, va_gc> *vec)
9056 : : {
9057 : 1413618382 : if (vec != NULL)
9058 : : {
9059 : 1015075028 : if (vec->allocated () >= 16)
9060 : : /* Don't cache vecs that have expanded more than once. On a p64
9061 : : target, vecs double in alloc size with each power of 2 elements, e.g
9062 : : at 16 elements the alloc increases from 128 to 256 bytes. */
9063 : 785305 : vec_free (vec);
9064 : : else
9065 : : {
9066 : 1014289723 : vec->truncate (0);
9067 : 1014289723 : vec_safe_push (tree_vector_cache, vec);
9068 : : }
9069 : : }
9070 : 1413618382 : }
9071 : :
9072 : : /* Get a new tree vector holding a single tree. */
9073 : :
9074 : : vec<tree, va_gc> *
9075 : 50896849 : make_tree_vector_single (tree t)
9076 : : {
9077 : 50896849 : vec<tree, va_gc> *ret = make_tree_vector ();
9078 : 50896849 : ret->quick_push (t);
9079 : 50896849 : return ret;
9080 : : }
9081 : :
9082 : : /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
9083 : :
9084 : : vec<tree, va_gc> *
9085 : 6896 : make_tree_vector_from_list (tree list)
9086 : : {
9087 : 6896 : vec<tree, va_gc> *ret = make_tree_vector ();
9088 : 18411 : for (; list; list = TREE_CHAIN (list))
9089 : 11515 : vec_safe_push (ret, TREE_VALUE (list));
9090 : 6896 : return ret;
9091 : : }
9092 : :
9093 : : /* Append to a tree vector V the values of a CONSTRUCTOR CTOR
9094 : : and return the new possibly reallocated vector. */
9095 : :
9096 : : vec<tree, va_gc> *
9097 : 1400784 : append_ctor_to_tree_vector (vec<tree, va_gc> *v, tree ctor)
9098 : : {
9099 : 2632103 : unsigned nelts = vec_safe_length (v) + CONSTRUCTOR_NELTS (ctor);
9100 : 1400784 : vec_safe_reserve (v, CONSTRUCTOR_NELTS (ctor));
9101 : 3458955 : for (unsigned i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
9102 : 2058171 : if (TREE_CODE (CONSTRUCTOR_ELT (ctor, i)->value) == RAW_DATA_CST)
9103 : : {
9104 : 24 : tree raw_data = CONSTRUCTOR_ELT (ctor, i)->value;
9105 : 24 : nelts += RAW_DATA_LENGTH (raw_data) - 1;
9106 : 24 : vec_safe_reserve (v, nelts - v->length ());
9107 : 48 : if (TYPE_PRECISION (TREE_TYPE (raw_data)) > CHAR_BIT
9108 : 24 : || TYPE_UNSIGNED (TREE_TYPE (raw_data)))
9109 : 6264 : for (unsigned j = 0; j < (unsigned) RAW_DATA_LENGTH (raw_data); ++j)
9110 : 6240 : v->quick_push (build_int_cst (TREE_TYPE (raw_data),
9111 : 6240 : RAW_DATA_UCHAR_ELT (raw_data, j)));
9112 : : else
9113 : 0 : for (unsigned j = 0; j < (unsigned) RAW_DATA_LENGTH (raw_data); ++j)
9114 : 0 : v->quick_push (build_int_cst (TREE_TYPE (raw_data),
9115 : 0 : RAW_DATA_SCHAR_ELT (raw_data, j)));
9116 : : }
9117 : : else
9118 : 2058147 : v->quick_push (CONSTRUCTOR_ELT (ctor, i)->value);
9119 : 1400784 : return v;
9120 : : }
9121 : :
9122 : : /* Get a new tree vector of the values of a CONSTRUCTOR. */
9123 : :
9124 : : vec<tree, va_gc> *
9125 : 5168 : make_tree_vector_from_ctor (tree ctor)
9126 : : {
9127 : 5168 : vec<tree,va_gc> *ret
9128 : 5168 : = CONSTRUCTOR_NELTS (ctor) <= 16 ? make_tree_vector () : NULL;
9129 : 5168 : return append_ctor_to_tree_vector (ret, ctor);
9130 : : }
9131 : :
9132 : : /* Get a new tree vector which is a copy of an existing one. */
9133 : :
9134 : : vec<tree, va_gc> *
9135 : 28539935 : make_tree_vector_copy (const vec<tree, va_gc> *orig)
9136 : : {
9137 : 28539935 : vec<tree, va_gc> *ret;
9138 : 28539935 : unsigned int ix;
9139 : 28539935 : tree t;
9140 : :
9141 : 28539935 : ret = make_tree_vector ();
9142 : 28539935 : vec_safe_reserve (ret, vec_safe_length (orig));
9143 : 69398834 : FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
9144 : 12318964 : ret->quick_push (t);
9145 : 28539935 : return ret;
9146 : : }
9147 : :
9148 : : /* Return true if KEYWORD starts a type specifier. */
9149 : :
9150 : : bool
9151 : 16 : keyword_begins_type_specifier (enum rid keyword)
9152 : : {
9153 : 16 : switch (keyword)
9154 : : {
9155 : : case RID_AUTO_TYPE:
9156 : : case RID_INT:
9157 : : case RID_CHAR:
9158 : : case RID_FLOAT:
9159 : : case RID_DOUBLE:
9160 : : case RID_VOID:
9161 : : case RID_UNSIGNED:
9162 : : case RID_LONG:
9163 : : case RID_SHORT:
9164 : : case RID_SIGNED:
9165 : : CASE_RID_FLOATN_NX:
9166 : : case RID_DFLOAT32:
9167 : : case RID_DFLOAT64:
9168 : : case RID_DFLOAT128:
9169 : : case RID_FRACT:
9170 : : case RID_ACCUM:
9171 : : case RID_BOOL:
9172 : : case RID_BITINT:
9173 : : case RID_WCHAR:
9174 : : case RID_CHAR8:
9175 : : case RID_CHAR16:
9176 : : case RID_CHAR32:
9177 : : case RID_SAT:
9178 : : case RID_COMPLEX:
9179 : : case RID_TYPEOF:
9180 : : case RID_STRUCT:
9181 : : case RID_CLASS:
9182 : : case RID_UNION:
9183 : : case RID_ENUM:
9184 : : return true;
9185 : 1 : default:
9186 : 1 : if (keyword >= RID_FIRST_INT_N
9187 : : && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
9188 : 0 : && int_n_enabled_p[keyword-RID_FIRST_INT_N])
9189 : : return true;
9190 : : return false;
9191 : : }
9192 : : }
9193 : :
9194 : : /* Return true if KEYWORD names a type qualifier. */
9195 : :
9196 : : bool
9197 : 11841 : keyword_is_type_qualifier (enum rid keyword)
9198 : : {
9199 : 11841 : switch (keyword)
9200 : : {
9201 : : case RID_CONST:
9202 : : case RID_VOLATILE:
9203 : : case RID_RESTRICT:
9204 : : case RID_ATOMIC:
9205 : : return true;
9206 : 58 : default:
9207 : 58 : return false;
9208 : : }
9209 : : }
9210 : :
9211 : : /* Return true if KEYWORD names a storage class specifier.
9212 : :
9213 : : RID_TYPEDEF is not included in this list despite `typedef' being
9214 : : listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
9215 : : such for syntactic convenience only. */
9216 : :
9217 : : bool
9218 : 11867 : keyword_is_storage_class_specifier (enum rid keyword)
9219 : : {
9220 : 11867 : switch (keyword)
9221 : : {
9222 : : case RID_STATIC:
9223 : : case RID_EXTERN:
9224 : : case RID_REGISTER:
9225 : : case RID_AUTO:
9226 : : case RID_MUTABLE:
9227 : : case RID_THREAD:
9228 : : return true;
9229 : 11841 : default:
9230 : 11841 : return false;
9231 : : }
9232 : : }
9233 : :
9234 : : /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
9235 : :
9236 : : static bool
9237 : 58 : keyword_is_function_specifier (enum rid keyword)
9238 : : {
9239 : 58 : switch (keyword)
9240 : : {
9241 : : case RID_INLINE:
9242 : : case RID_NORETURN:
9243 : : case RID_VIRTUAL:
9244 : : case RID_EXPLICIT:
9245 : : return true;
9246 : 56 : default:
9247 : 0 : return false;
9248 : : }
9249 : : }
9250 : :
9251 : : /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
9252 : : declaration-specifier (C99 6.7). */
9253 : :
9254 : : bool
9255 : 11867 : keyword_is_decl_specifier (enum rid keyword)
9256 : : {
9257 : 11867 : if (keyword_is_storage_class_specifier (keyword)
9258 : 11841 : || keyword_is_type_qualifier (keyword)
9259 : 11867 : || keyword_is_function_specifier (keyword))
9260 : 11811 : return true;
9261 : :
9262 : 56 : switch (keyword)
9263 : : {
9264 : : case RID_TYPEDEF:
9265 : : case RID_FRIEND:
9266 : : case RID_CONSTEXPR:
9267 : : case RID_CONSTINIT:
9268 : : return true;
9269 : : default:
9270 : : return false;
9271 : : }
9272 : : }
9273 : :
9274 : : /* Initialize language-specific-bits of tree_contains_struct. */
9275 : :
9276 : : void
9277 : 210211 : c_common_init_ts (void)
9278 : : {
9279 : 210211 : MARK_TS_EXP (SIZEOF_EXPR);
9280 : 210211 : MARK_TS_EXP (PAREN_SIZEOF_EXPR);
9281 : 210211 : MARK_TS_EXP (C_MAYBE_CONST_EXPR);
9282 : 210211 : MARK_TS_EXP (EXCESS_PRECISION_EXPR);
9283 : 210211 : MARK_TS_EXP (BREAK_STMT);
9284 : 210211 : MARK_TS_EXP (CONTINUE_STMT);
9285 : 210211 : MARK_TS_EXP (DO_STMT);
9286 : 210211 : MARK_TS_EXP (FOR_STMT);
9287 : 210211 : MARK_TS_EXP (SWITCH_STMT);
9288 : 210211 : MARK_TS_EXP (WHILE_STMT);
9289 : :
9290 : 210211 : MARK_TS_DECL_COMMON (CONCEPT_DECL);
9291 : 210211 : }
9292 : :
9293 : : /* Build a user-defined numeric literal out of an integer constant type VALUE
9294 : : with identifier SUFFIX. */
9295 : :
9296 : : tree
9297 : 938826 : build_userdef_literal (tree suffix_id, tree value,
9298 : : enum overflow_type overflow, tree num_string)
9299 : : {
9300 : 938826 : tree literal = make_node (USERDEF_LITERAL);
9301 : 938826 : USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
9302 : 938826 : USERDEF_LITERAL_VALUE (literal) = value;
9303 : 938826 : USERDEF_LITERAL_OVERFLOW (literal) = overflow;
9304 : 938826 : USERDEF_LITERAL_NUM_STRING (literal) = num_string;
9305 : 938826 : return literal;
9306 : : }
9307 : :
9308 : : /* For vector[index], convert the vector to an array of the underlying type.
9309 : : Return true if the resulting ARRAY_REF should not be an lvalue. */
9310 : :
9311 : : bool
9312 : 8781575 : convert_vector_to_array_for_subscript (location_t loc,
9313 : : tree *vecp, tree index)
9314 : : {
9315 : 8781575 : bool ret = false;
9316 : 8781575 : if (gnu_vector_type_p (TREE_TYPE (*vecp)))
9317 : : {
9318 : 1064599 : tree type = TREE_TYPE (*vecp);
9319 : 1064599 : tree newitype;
9320 : :
9321 : 1064599 : ret = !lvalue_p (*vecp);
9322 : :
9323 : 1064599 : index = fold_for_warn (index);
9324 : : /* Warn out-of-bounds index for vectors only if known. */
9325 : 1064599 : if (poly_int_tree_p (index))
9326 : 1051750 : if (!tree_fits_poly_uint64_p (index)
9327 : 1051750 : || known_ge (tree_to_poly_uint64 (index),
9328 : : TYPE_VECTOR_SUBPARTS (type)))
9329 : 84 : warning_at (loc, OPT_Warray_bounds_, "index value is out of bound");
9330 : :
9331 : : /* We are building an ARRAY_REF so mark the vector as addressable
9332 : : to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
9333 : : for function parameters. */
9334 : 1064599 : c_common_mark_addressable_vec (*vecp);
9335 : :
9336 : : /* Make sure qualifiers are copied from the vector type to the new element
9337 : : of the array type. */
9338 : 1064599 : newitype = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
9339 : :
9340 : 1064599 : *vecp = build1 (VIEW_CONVERT_EXPR,
9341 : : build_array_type_nelts (newitype,
9342 : : TYPE_VECTOR_SUBPARTS (type)),
9343 : : *vecp);
9344 : : }
9345 : 8781575 : return ret;
9346 : : }
9347 : :
9348 : : /* Determine which of the operands, if any, is a scalar that needs to be
9349 : : converted to a vector, for the range of operations. */
9350 : : enum stv_conv
9351 : 3468 : scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
9352 : : bool complain)
9353 : : {
9354 : 3468 : tree type0 = TREE_TYPE (op0);
9355 : 3468 : tree type1 = TREE_TYPE (op1);
9356 : 3468 : bool integer_only_op = false;
9357 : 3468 : enum stv_conv ret = stv_firstarg;
9358 : :
9359 : 3468 : gcc_assert (gnu_vector_type_p (type0) || gnu_vector_type_p (type1));
9360 : 3468 : switch (code)
9361 : : {
9362 : : /* Most GENERIC binary expressions require homogeneous arguments.
9363 : : LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
9364 : : argument that is a vector and a second one that is a scalar, so
9365 : : we never return stv_secondarg for them. */
9366 : 951 : case RSHIFT_EXPR:
9367 : 951 : case LSHIFT_EXPR:
9368 : 951 : if (TREE_CODE (type0) == INTEGER_TYPE
9369 : 951 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9370 : : {
9371 : 50 : if (unsafe_conversion_p (TREE_TYPE (type1), op0,
9372 : : NULL_TREE, false))
9373 : : {
9374 : 0 : if (complain)
9375 : 0 : error_at (loc, "conversion of scalar %qT to vector %qT "
9376 : : "involves truncation", type0, type1);
9377 : 0 : return stv_error;
9378 : : }
9379 : : else
9380 : : return stv_firstarg;
9381 : : }
9382 : : break;
9383 : :
9384 : 311 : case BIT_IOR_EXPR:
9385 : 311 : case BIT_XOR_EXPR:
9386 : 311 : case BIT_AND_EXPR:
9387 : 311 : integer_only_op = true;
9388 : : /* fall through */
9389 : :
9390 : 2505 : case VEC_COND_EXPR:
9391 : :
9392 : 2505 : case PLUS_EXPR:
9393 : 2505 : case MINUS_EXPR:
9394 : 2505 : case MULT_EXPR:
9395 : 2505 : case TRUNC_DIV_EXPR:
9396 : 2505 : case CEIL_DIV_EXPR:
9397 : 2505 : case FLOOR_DIV_EXPR:
9398 : 2505 : case ROUND_DIV_EXPR:
9399 : 2505 : case EXACT_DIV_EXPR:
9400 : 2505 : case TRUNC_MOD_EXPR:
9401 : 2505 : case FLOOR_MOD_EXPR:
9402 : 2505 : case RDIV_EXPR:
9403 : 2505 : case EQ_EXPR:
9404 : 2505 : case NE_EXPR:
9405 : 2505 : case LE_EXPR:
9406 : 2505 : case GE_EXPR:
9407 : 2505 : case LT_EXPR:
9408 : 2505 : case GT_EXPR:
9409 : : /* What about UNLT_EXPR? */
9410 : 2505 : if (gnu_vector_type_p (type0))
9411 : : {
9412 : 1852 : ret = stv_secondarg;
9413 : 1852 : std::swap (type0, type1);
9414 : 1852 : std::swap (op0, op1);
9415 : : }
9416 : :
9417 : 2505 : if (TREE_CODE (type0) == INTEGER_TYPE
9418 : 2505 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9419 : : {
9420 : 1795 : if (unsafe_conversion_p (TREE_TYPE (type1), op0,
9421 : : NULL_TREE, false))
9422 : : {
9423 : 11 : if (complain)
9424 : 8 : error_at (loc, "conversion of scalar %qT to vector %qT "
9425 : : "involves truncation", type0, type1);
9426 : 11 : return stv_error;
9427 : : }
9428 : : return ret;
9429 : : }
9430 : 710 : else if (!integer_only_op
9431 : : /* Allow integer --> real conversion if safe. */
9432 : 710 : && (SCALAR_FLOAT_TYPE_P (type0)
9433 : 468 : || TREE_CODE (type0) == INTEGER_TYPE)
9434 : 1420 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
9435 : : {
9436 : 686 : if (unsafe_conversion_p (TREE_TYPE (type1), op0,
9437 : : NULL_TREE, false))
9438 : : {
9439 : 22 : if (complain)
9440 : 22 : error_at (loc, "conversion of scalar %qT to vector %qT "
9441 : : "involves truncation", type0, type1);
9442 : 22 : return stv_error;
9443 : : }
9444 : : return ret;
9445 : : }
9446 : : default:
9447 : : break;
9448 : : }
9449 : :
9450 : : return stv_nothing;
9451 : : }
9452 : :
9453 : : /* Return the alignment of std::max_align_t.
9454 : :
9455 : : [support.types.layout] The type max_align_t is a POD type whose alignment
9456 : : requirement is at least as great as that of every scalar type, and whose
9457 : : alignment requirement is supported in every context. */
9458 : :
9459 : : unsigned
9460 : 253198 : max_align_t_align ()
9461 : : {
9462 : 253198 : unsigned int max_align = MAX (TYPE_ALIGN (long_long_integer_type_node),
9463 : : TYPE_ALIGN (long_double_type_node));
9464 : 253198 : if (float128_type_node != NULL_TREE)
9465 : 253198 : max_align = MAX (max_align, TYPE_ALIGN (float128_type_node));
9466 : 253198 : return max_align;
9467 : : }
9468 : :
9469 : : /* Return true iff ALIGN is an integral constant that is a fundamental
9470 : : alignment, as defined by [basic.align] in the c++-11
9471 : : specifications.
9472 : :
9473 : : That is:
9474 : :
9475 : : [A fundamental alignment is represented by an alignment less than or
9476 : : equal to the greatest alignment supported by the implementation
9477 : : in all contexts, which is equal to alignof(max_align_t)]. */
9478 : :
9479 : : bool
9480 : 0 : cxx_fundamental_alignment_p (unsigned align)
9481 : : {
9482 : 0 : return (align <= max_align_t_align ());
9483 : : }
9484 : :
9485 : : /* Return true if T is a pointer to a zero-sized aggregate. */
9486 : :
9487 : : bool
9488 : 1304539 : pointer_to_zero_sized_aggr_p (tree t)
9489 : : {
9490 : 1304539 : if (!POINTER_TYPE_P (t))
9491 : : return false;
9492 : 1304539 : t = TREE_TYPE (t);
9493 : 1304539 : return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
9494 : : }
9495 : :
9496 : : /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
9497 : : with no library fallback or for an ADDR_EXPR whose operand is such type
9498 : : issues an error pointing to the location LOC.
9499 : : Returns true when the expression has been diagnosed and false
9500 : : otherwise. */
9501 : :
9502 : : bool
9503 : 4332200027 : reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
9504 : : {
9505 : 4332200027 : if (TREE_CODE (expr) == ADDR_EXPR)
9506 : 105751972 : expr = TREE_OPERAND (expr, 0);
9507 : :
9508 : 4332200027 : STRIP_ANY_LOCATION_WRAPPER (expr);
9509 : :
9510 : 4332200027 : if (TREE_TYPE (expr)
9511 : 4332199873 : && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
9512 : 2017389 : && TREE_CODE (expr) == FUNCTION_DECL
9513 : : /* The intersection of DECL_BUILT_IN and DECL_IS_UNDECLARED_BUILTIN avoids
9514 : : false positives for user-declared built-ins such as abs or
9515 : : strlen, and for C++ operators new and delete.
9516 : : The c_decl_implicit() test avoids false positives for implicitly
9517 : : declared built-ins with library fallbacks (such as abs). */
9518 : 1990473 : && fndecl_built_in_p (expr)
9519 : 67135 : && DECL_IS_UNDECLARED_BUILTIN (expr)
9520 : 2372 : && !c_decl_implicit (expr)
9521 : 4332202394 : && !DECL_ASSEMBLER_NAME_SET_P (expr))
9522 : : {
9523 : 220 : if (loc == UNKNOWN_LOCATION)
9524 : 82 : loc = EXPR_LOC_OR_LOC (expr, input_location);
9525 : :
9526 : : /* Reject arguments that are built-in functions with
9527 : : no library fallback. */
9528 : 220 : error_at (loc, "built-in function %qE must be directly called", expr);
9529 : :
9530 : 220 : return true;
9531 : : }
9532 : :
9533 : : return false;
9534 : : }
9535 : :
9536 : : /* Issue an ERROR for an invalid SIZE of array NAME which is null
9537 : : for unnamed arrays. */
9538 : :
9539 : : void
9540 : 1405 : invalid_array_size_error (location_t loc, cst_size_error error,
9541 : : const_tree size, const_tree name)
9542 : : {
9543 : 1405 : tree maxsize = max_object_size ();
9544 : 1405 : switch (error)
9545 : : {
9546 : 0 : case cst_size_not_constant:
9547 : 0 : if (name)
9548 : 0 : error_at (loc, "size of array %qE is not a constant expression",
9549 : : name);
9550 : : else
9551 : 0 : error_at (loc, "size of array is not a constant expression");
9552 : : break;
9553 : 415 : case cst_size_negative:
9554 : 415 : if (name)
9555 : 124 : error_at (loc, "size %qE of array %qE is negative",
9556 : : size, name);
9557 : : else
9558 : 291 : error_at (loc, "size %qE of array is negative",
9559 : : size);
9560 : : break;
9561 : 930 : case cst_size_too_big:
9562 : 930 : if (name)
9563 : 103 : error_at (loc, "size %qE of array %qE exceeds maximum "
9564 : : "object size %qE", size, name, maxsize);
9565 : : else
9566 : 827 : error_at (loc, "size %qE of array exceeds maximum "
9567 : : "object size %qE", size, maxsize);
9568 : : break;
9569 : 60 : case cst_size_overflow:
9570 : 60 : if (name)
9571 : 39 : error_at (loc, "size of array %qE exceeds maximum "
9572 : : "object size %qE", name, maxsize);
9573 : : else
9574 : 21 : error_at (loc, "size of array exceeds maximum "
9575 : : "object size %qE", maxsize);
9576 : : break;
9577 : 0 : default:
9578 : 0 : gcc_unreachable ();
9579 : : }
9580 : 1405 : }
9581 : :
9582 : : /* Check if array size calculations overflow or if the array covers more
9583 : : than half of the address space. Return true if the size of the array
9584 : : is valid, false otherwise. T is either the type of the array or its
9585 : : size, and NAME is the name of the array, or null for unnamed arrays. */
9586 : :
9587 : : bool
9588 : 7462775 : valid_array_size_p (location_t loc, const_tree t, tree name, bool complain)
9589 : : {
9590 : 7462775 : if (t == error_mark_node)
9591 : : return true;
9592 : :
9593 : 7462634 : const_tree size;
9594 : 7462634 : if (TYPE_P (t))
9595 : : {
9596 : 5367536 : if (!COMPLETE_TYPE_P (t))
9597 : : return true;
9598 : 2799581 : size = TYPE_SIZE_UNIT (t);
9599 : : }
9600 : : else
9601 : : size = t;
9602 : :
9603 : 4894679 : if (TREE_CODE (size) != INTEGER_CST)
9604 : : return true;
9605 : :
9606 : 4870246 : cst_size_error error;
9607 : 4870246 : if (valid_constant_size_p (size, &error))
9608 : : return true;
9609 : :
9610 : 1096 : if (!complain)
9611 : : return false;
9612 : :
9613 : 1081 : if (TREE_CODE (TREE_TYPE (size)) == ENUMERAL_TYPE)
9614 : : /* Show the value of the enumerator rather than its name. */
9615 : 16 : size = convert (ssizetype, const_cast<tree> (size));
9616 : :
9617 : 1081 : invalid_array_size_error (loc, error, size, name);
9618 : 1081 : return false;
9619 : : }
9620 : :
9621 : : /* Read SOURCE_DATE_EPOCH from environment to have a deterministic
9622 : : timestamp to replace embedded current dates to get reproducible
9623 : : results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
9624 : :
9625 : : time_t
9626 : 2564 : cb_get_source_date_epoch (cpp_reader *pfile ATTRIBUTE_UNUSED)
9627 : : {
9628 : 2564 : char *source_date_epoch;
9629 : 2564 : int64_t epoch;
9630 : 2564 : char *endptr;
9631 : :
9632 : 2564 : source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
9633 : 2564 : if (!source_date_epoch)
9634 : : return (time_t) -1;
9635 : :
9636 : 3 : errno = 0;
9637 : : #if defined(INT64_T_IS_LONG)
9638 : 3 : epoch = strtol (source_date_epoch, &endptr, 10);
9639 : : #else
9640 : : epoch = strtoll (source_date_epoch, &endptr, 10);
9641 : : #endif
9642 : 3 : if (errno != 0 || endptr == source_date_epoch || *endptr != '\0'
9643 : 2 : || epoch < 0 || epoch > MAX_SOURCE_DATE_EPOCH)
9644 : : {
9645 : 1 : error_at (input_location, "environment variable %qs must "
9646 : : "expand to a non-negative integer less than or equal to %wd",
9647 : : "SOURCE_DATE_EPOCH", MAX_SOURCE_DATE_EPOCH);
9648 : 1 : return (time_t) -1;
9649 : : }
9650 : :
9651 : : return (time_t) epoch;
9652 : : }
9653 : :
9654 : : /* Callback for libcpp for offering spelling suggestions for misspelled
9655 : : directives. GOAL is an unrecognized string; CANDIDATES is a
9656 : : NULL-terminated array of candidate strings. Return the closest
9657 : : match to GOAL within CANDIDATES, or NULL if none are good
9658 : : suggestions. */
9659 : :
9660 : : const char *
9661 : 19 : cb_get_suggestion (cpp_reader *, const char *goal,
9662 : : const char *const *candidates)
9663 : : {
9664 : 19 : best_match<const char *, const char *> bm (goal);
9665 : 204 : while (*candidates)
9666 : 166 : bm.consider (*candidates++);
9667 : 19 : return bm.get_best_meaningful_candidate ();
9668 : : }
9669 : :
9670 : : /* Return the latice point which is the wider of the two FLT_EVAL_METHOD
9671 : : modes X, Y. This isn't just >, as the FLT_EVAL_METHOD values added
9672 : : by C TS 18661-3 for interchange types that are computed in their
9673 : : native precision are larger than the C11 values for evaluating in the
9674 : : precision of float/double/long double. If either mode is
9675 : : FLT_EVAL_METHOD_UNPREDICTABLE, return that. */
9676 : :
9677 : : enum flt_eval_method
9678 : 533582 : excess_precision_mode_join (enum flt_eval_method x,
9679 : : enum flt_eval_method y)
9680 : : {
9681 : 533582 : if (x == FLT_EVAL_METHOD_UNPREDICTABLE
9682 : 533582 : || y == FLT_EVAL_METHOD_UNPREDICTABLE)
9683 : : return FLT_EVAL_METHOD_UNPREDICTABLE;
9684 : :
9685 : : /* GCC only supports one interchange type right now, _Float16. If
9686 : : we're evaluating _Float16 in 16-bit precision, then flt_eval_method
9687 : : will be FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
9688 : 533258 : if (x == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
9689 : : return y;
9690 : 530977 : if (y == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
9691 : : return x;
9692 : :
9693 : : /* Other values for flt_eval_method are directly comparable, and we want
9694 : : the maximum. */
9695 : 530965 : return MAX (x, y);
9696 : : }
9697 : :
9698 : : /* Return the value that should be set for FLT_EVAL_METHOD in the
9699 : : context of ISO/IEC TS 18861-3.
9700 : :
9701 : : This relates to the effective excess precision seen by the user,
9702 : : which is the join point of the precision the target requests for
9703 : : -fexcess-precision={standard,fast,16} and the implicit excess precision
9704 : : the target uses. */
9705 : :
9706 : : static enum flt_eval_method
9707 : 418740 : c_ts18661_flt_eval_method (void)
9708 : : {
9709 : 418740 : enum flt_eval_method implicit
9710 : 418740 : = targetm.c.excess_precision (EXCESS_PRECISION_TYPE_IMPLICIT);
9711 : :
9712 : 305126 : enum excess_precision_type flag_type
9713 : 418740 : = (flag_excess_precision == EXCESS_PRECISION_STANDARD
9714 : 418740 : ? EXCESS_PRECISION_TYPE_STANDARD
9715 : : : (flag_excess_precision == EXCESS_PRECISION_FLOAT16
9716 : 305138 : ? EXCESS_PRECISION_TYPE_FLOAT16
9717 : : : EXCESS_PRECISION_TYPE_FAST));
9718 : :
9719 : 418740 : enum flt_eval_method requested
9720 : 418740 : = targetm.c.excess_precision (flag_type);
9721 : :
9722 : 418740 : return excess_precision_mode_join (implicit, requested);
9723 : : }
9724 : :
9725 : : /* As c_cpp_ts18661_flt_eval_method, but clamps the expected values to
9726 : : those that were permitted by C11. That is to say, eliminates
9727 : : FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
9728 : :
9729 : : static enum flt_eval_method
9730 : 102588 : c_c11_flt_eval_method (void)
9731 : : {
9732 : 102588 : return excess_precision_mode_join (c_ts18661_flt_eval_method (),
9733 : 102588 : FLT_EVAL_METHOD_PROMOTE_TO_FLOAT);
9734 : : }
9735 : :
9736 : : /* Return the value that should be set for FLT_EVAL_METHOD.
9737 : : MAYBE_C11_ONLY_P is TRUE if we should check
9738 : : FLAG_PERMITTED_EVAL_METHODS as to whether we should limit the possible
9739 : : values we can return to those from C99/C11, and FALSE otherwise.
9740 : : See the comments on c_ts18661_flt_eval_method for what value we choose
9741 : : to set here. */
9742 : :
9743 : : int
9744 : 418740 : c_flt_eval_method (bool maybe_c11_only_p)
9745 : : {
9746 : 418740 : if (maybe_c11_only_p
9747 : 209370 : && flag_permitted_flt_eval_methods
9748 : : == PERMITTED_FLT_EVAL_METHODS_C11)
9749 : 102588 : return c_c11_flt_eval_method ();
9750 : : else
9751 : 316152 : return c_ts18661_flt_eval_method ();
9752 : : }
9753 : :
9754 : : /* An enum for get_missing_token_insertion_kind for describing the best
9755 : : place to insert a missing token, if there is one. */
9756 : :
9757 : : enum missing_token_insertion_kind
9758 : : {
9759 : : MTIK_IMPOSSIBLE,
9760 : : MTIK_INSERT_BEFORE_NEXT,
9761 : : MTIK_INSERT_AFTER_PREV
9762 : : };
9763 : :
9764 : : /* Given a missing token of TYPE, determine if it is reasonable to
9765 : : emit a fix-it hint suggesting the insertion of the token, and,
9766 : : if so, where the token should be inserted relative to other tokens.
9767 : :
9768 : : It only makes sense to do this for values of TYPE that are symbols.
9769 : :
9770 : : Some symbols should go before the next token, e.g. in:
9771 : : if flag)
9772 : : we want to insert the missing '(' immediately before "flag",
9773 : : giving:
9774 : : if (flag)
9775 : : rather than:
9776 : : if( flag)
9777 : : These use MTIK_INSERT_BEFORE_NEXT.
9778 : :
9779 : : Other symbols should go after the previous token, e.g. in:
9780 : : if (flag
9781 : : do_something ();
9782 : : we want to insert the missing ')' immediately after the "flag",
9783 : : giving:
9784 : : if (flag)
9785 : : do_something ();
9786 : : rather than:
9787 : : if (flag
9788 : : )do_something ();
9789 : : These use MTIK_INSERT_AFTER_PREV. */
9790 : :
9791 : : static enum missing_token_insertion_kind
9792 : 3208 : get_missing_token_insertion_kind (enum cpp_ttype type)
9793 : : {
9794 : 3208 : switch (type)
9795 : : {
9796 : : /* Insert missing "opening" brackets immediately
9797 : : before the next token. */
9798 : : case CPP_OPEN_SQUARE:
9799 : : case CPP_OPEN_PAREN:
9800 : : return MTIK_INSERT_BEFORE_NEXT;
9801 : :
9802 : : /* Insert other missing symbols immediately after
9803 : : the previous token. */
9804 : 1938 : case CPP_CLOSE_PAREN:
9805 : 1938 : case CPP_CLOSE_SQUARE:
9806 : 1938 : case CPP_SEMICOLON:
9807 : 1938 : case CPP_COMMA:
9808 : 1938 : case CPP_COLON:
9809 : 1938 : return MTIK_INSERT_AFTER_PREV;
9810 : :
9811 : : /* Other kinds of token don't get fix-it hints. */
9812 : 986 : default:
9813 : 986 : return MTIK_IMPOSSIBLE;
9814 : : }
9815 : : }
9816 : :
9817 : : /* Given RICHLOC, a location for a diagnostic describing a missing token
9818 : : of kind TOKEN_TYPE, potentially add a fix-it hint suggesting the
9819 : : insertion of the token.
9820 : :
9821 : : The location of the attempted fix-it hint depends on TOKEN_TYPE:
9822 : : it will either be:
9823 : : (a) immediately after PREV_TOKEN_LOC, or
9824 : :
9825 : : (b) immediately before the primary location within RICHLOC (taken to
9826 : : be that of the token following where the token was expected).
9827 : :
9828 : : If we manage to add a fix-it hint, then the location of the
9829 : : fix-it hint is likely to be more useful as the primary location
9830 : : of the diagnostic than that of the following token, so we swap
9831 : : these locations.
9832 : :
9833 : : For example, given this bogus code:
9834 : : 123456789012345678901234567890
9835 : : 1 | int missing_semicolon (void)
9836 : : 2 | {
9837 : : 3 | return 42
9838 : : 4 | }
9839 : :
9840 : : we will emit:
9841 : :
9842 : : "expected ';' before '}'"
9843 : :
9844 : : RICHLOC's primary location is at the closing brace, so before "swapping"
9845 : : we would emit the error at line 4 column 1:
9846 : :
9847 : : 123456789012345678901234567890
9848 : : 3 | return 42 |< fix-it hint emitted for this line
9849 : : | ; |
9850 : : 4 | } |< "expected ';' before '}'" emitted at this line
9851 : : | ^ |
9852 : :
9853 : : It's more useful for the location of the diagnostic to be at the
9854 : : fix-it hint, so we swap the locations, so the primary location
9855 : : is at the fix-it hint, with the old primary location inserted
9856 : : as a secondary location, giving this, with the error at line 3
9857 : : column 12:
9858 : :
9859 : : 123456789012345678901234567890
9860 : : 3 | return 42 |< "expected ';' before '}'" emitted at this line,
9861 : : | ^ | with fix-it hint
9862 : : 4 | ; |
9863 : : | } |< secondary range emitted here
9864 : : | ~ |. */
9865 : :
9866 : : void
9867 : 3208 : maybe_suggest_missing_token_insertion (rich_location *richloc,
9868 : : enum cpp_ttype token_type,
9869 : : location_t prev_token_loc)
9870 : : {
9871 : 3208 : gcc_assert (richloc);
9872 : :
9873 : 3208 : enum missing_token_insertion_kind mtik
9874 : 3208 : = get_missing_token_insertion_kind (token_type);
9875 : :
9876 : 3208 : switch (mtik)
9877 : : {
9878 : : default:
9879 : : gcc_unreachable ();
9880 : : break;
9881 : :
9882 : : case MTIK_IMPOSSIBLE:
9883 : : return;
9884 : :
9885 : 284 : case MTIK_INSERT_BEFORE_NEXT:
9886 : : /* Attempt to add the fix-it hint before the primary location
9887 : : of RICHLOC. */
9888 : 284 : richloc->add_fixit_insert_before (cpp_type2name (token_type, 0));
9889 : 284 : break;
9890 : :
9891 : 1938 : case MTIK_INSERT_AFTER_PREV:
9892 : : /* Attempt to add the fix-it hint after PREV_TOKEN_LOC. */
9893 : 1938 : richloc->add_fixit_insert_after (prev_token_loc,
9894 : : cpp_type2name (token_type, 0));
9895 : 1938 : break;
9896 : : }
9897 : :
9898 : : /* If we were successful, use the fix-it hint's location as the
9899 : : primary location within RICHLOC, adding the old primary location
9900 : : back as a secondary location. */
9901 : 2222 : if (!richloc->seen_impossible_fixit_p ())
9902 : : {
9903 : 2218 : fixit_hint *hint = richloc->get_last_fixit_hint ();
9904 : 2218 : location_t hint_loc = hint->get_start_loc ();
9905 : 2218 : location_t old_loc = richloc->get_loc ();
9906 : :
9907 : 2218 : richloc->set_range (0, hint_loc, SHOW_RANGE_WITH_CARET);
9908 : 2218 : richloc->add_range (old_loc);
9909 : : }
9910 : : }
9911 : :
9912 : : /* Potentially emit a note about likely missing '&' or '*',
9913 : : depending on EXPR and EXPECTED_TYPE. */
9914 : :
9915 : : void
9916 : 2202 : maybe_emit_indirection_note (location_t loc,
9917 : : tree expr, tree expected_type)
9918 : : {
9919 : 2202 : gcc_assert (expr);
9920 : 2202 : gcc_assert (expected_type);
9921 : :
9922 : 2202 : tree actual_type = TREE_TYPE (expr);
9923 : :
9924 : : /* Missing '&'. */
9925 : 2202 : if (TREE_CODE (expected_type) == POINTER_TYPE
9926 : 1224 : && compatible_types_for_indirection_note_p (actual_type,
9927 : 1224 : TREE_TYPE (expected_type))
9928 : 2280 : && lvalue_p (expr))
9929 : : {
9930 : 43 : gcc_rich_location richloc (loc);
9931 : 43 : richloc.add_fixit_insert_before ("&");
9932 : 43 : inform (&richloc, "possible fix: take the address with %qs", "&");
9933 : 43 : }
9934 : :
9935 : : /* Missing '*'. */
9936 : 2202 : if (TREE_CODE (actual_type) == POINTER_TYPE
9937 : 2202 : && compatible_types_for_indirection_note_p (TREE_TYPE (actual_type),
9938 : : expected_type))
9939 : : {
9940 : 78 : gcc_rich_location richloc (loc);
9941 : 78 : richloc.add_fixit_insert_before ("*");
9942 : 78 : inform (&richloc, "possible fix: dereference with %qs", "*");
9943 : 78 : }
9944 : 2202 : }
9945 : :
9946 : : #if CHECKING_P
9947 : :
9948 : : namespace selftest {
9949 : :
9950 : : /* Verify that fold_for_warn on error_mark_node is safe. */
9951 : :
9952 : : static void
9953 : 3 : test_fold_for_warn ()
9954 : : {
9955 : 3 : ASSERT_EQ (error_mark_node, fold_for_warn (error_mark_node));
9956 : 3 : }
9957 : :
9958 : : /* Run all of the selftests within this file. */
9959 : :
9960 : : static void
9961 : 3 : c_common_cc_tests ()
9962 : : {
9963 : 0 : test_fold_for_warn ();
9964 : 0 : }
9965 : :
9966 : : /* Run all of the tests within c-family. */
9967 : :
9968 : : void
9969 : 3 : c_family_tests (void)
9970 : : {
9971 : 3 : c_common_cc_tests ();
9972 : 3 : c_format_cc_tests ();
9973 : 3 : c_indentation_cc_tests ();
9974 : 3 : c_pretty_print_cc_tests ();
9975 : 3 : c_spellcheck_cc_tests ();
9976 : 3 : c_opt_problem_cc_tests ();
9977 : :
9978 : : /* According to https://gcc.gnu.org/pipermail/gcc/2021-November/237703.html
9979 : : this has some language-specific assumptions, so we run it here. */
9980 : 3 : diagnostics::selftest::context_cc_tests ();
9981 : 3 : }
9982 : :
9983 : : } // namespace selftest
9984 : :
9985 : : #endif /* #if CHECKING_P */
9986 : :
9987 : : /* Attempt to locate a suitable location within FILE for a
9988 : : #include directive to be inserted before.
9989 : : LOC is the location of the relevant diagnostic.
9990 : :
9991 : : Attempt to return the location within FILE immediately
9992 : : after the last #include within that file, or the start of
9993 : : that file if it has no #include directives.
9994 : :
9995 : : Return UNKNOWN_LOCATION if no suitable location is found,
9996 : : or if an error occurs. */
9997 : :
9998 : : static location_t
9999 : 427 : try_to_locate_new_include_insertion_point (const char *file, location_t loc)
10000 : : {
10001 : : /* Locate the last ordinary map within FILE that ended with a #include. */
10002 : 427 : const line_map_ordinary *last_include_ord_map = NULL;
10003 : :
10004 : : /* ...and the next ordinary map within FILE after that one. */
10005 : 427 : const line_map_ordinary *last_ord_map_after_include = NULL;
10006 : :
10007 : : /* ...and the first ordinary map within FILE. */
10008 : 427 : const line_map_ordinary *first_ord_map_in_file = NULL;
10009 : :
10010 : : /* Get ordinary map containing LOC (or its expansion). */
10011 : 427 : const line_map_ordinary *ord_map_for_loc = NULL;
10012 : 427 : linemap_resolve_location (line_table, loc, LRK_MACRO_EXPANSION_POINT,
10013 : : &ord_map_for_loc);
10014 : 427 : gcc_assert (ord_map_for_loc);
10015 : :
10016 : 11981 : for (unsigned int i = 0; i < LINEMAPS_ORDINARY_USED (line_table); i++)
10017 : : {
10018 : 11981 : const line_map_ordinary *ord_map
10019 : 11981 : = LINEMAPS_ORDINARY_MAP_AT (line_table, i);
10020 : :
10021 : 23962 : if (const line_map_ordinary *from
10022 : 11981 : = linemap_included_from_linemap (line_table, ord_map))
10023 : : /* We cannot use pointer equality, because with preprocessed
10024 : : input all filename strings are unique. */
10025 : 9606 : if (0 == strcmp (from->to_file, file))
10026 : : {
10027 : 11981 : last_include_ord_map = from;
10028 : 11981 : last_ord_map_after_include = NULL;
10029 : : }
10030 : :
10031 : : /* Likewise, use strcmp, and reject any line-zero introductory
10032 : : map. */
10033 : 11981 : if (ord_map->to_line && 0 == strcmp (ord_map->to_file, file))
10034 : : {
10035 : 669 : if (!first_ord_map_in_file)
10036 : 427 : first_ord_map_in_file = ord_map;
10037 : 669 : if (last_include_ord_map && !last_ord_map_after_include)
10038 : 166 : last_ord_map_after_include = ord_map;
10039 : : }
10040 : :
10041 : : /* Stop searching when reaching the ord_map containing LOC,
10042 : : as it makes no sense to provide fix-it hints that appear
10043 : : after the diagnostic in question. */
10044 : 11981 : if (ord_map == ord_map_for_loc)
10045 : : break;
10046 : : }
10047 : :
10048 : : /* Determine where to insert the #include. */
10049 : 427 : const line_map_ordinary *ord_map_for_insertion;
10050 : :
10051 : : /* We want the next ordmap in the file after the last one that's a
10052 : : #include, but failing that, the start of the file. */
10053 : 427 : if (last_ord_map_after_include)
10054 : : ord_map_for_insertion = last_ord_map_after_include;
10055 : : else
10056 : 263 : ord_map_for_insertion = first_ord_map_in_file;
10057 : :
10058 : 263 : if (!ord_map_for_insertion)
10059 : : return UNKNOWN_LOCATION;
10060 : :
10061 : : /* The "start_location" is column 0, meaning "the whole line".
10062 : : rich_location and diagnostics::changes can't cope with this, so use
10063 : : column 1 instead. */
10064 : 427 : location_t col_0 = ord_map_for_insertion->start_location;
10065 : 427 : return linemap_position_for_loc_and_offset (line_table, col_0, 1);
10066 : : }
10067 : :
10068 : : /* A map from filenames to sets of headers added to them, for
10069 : : ensuring idempotency within maybe_add_include_fixit. */
10070 : :
10071 : : /* The values within the map. We need string comparison as there's
10072 : : no guarantee that two different diagnostics that are recommending
10073 : : adding e.g. "<stdio.h>" are using the same buffer. */
10074 : :
10075 : : typedef hash_set <const char *, false, nofree_string_hash> per_file_includes_t;
10076 : :
10077 : : /* The map itself. We don't need string comparison for the filename keys,
10078 : : as they come from libcpp. */
10079 : :
10080 : : typedef hash_map <const char *, per_file_includes_t *> added_includes_t;
10081 : : static added_includes_t *added_includes;
10082 : :
10083 : : /* Attempt to add a fix-it hint to RICHLOC, adding "#include HEADER\n"
10084 : : in a suitable location within the file of RICHLOC's primary
10085 : : location.
10086 : :
10087 : : This function is idempotent: a header will be added at most once to
10088 : : any given file.
10089 : :
10090 : : If OVERRIDE_LOCATION is true, then if a fix-it is added and will be
10091 : : printed, then RICHLOC's primary location will be replaced by that of
10092 : : the fix-it hint (for use by "inform" notes where the location of the
10093 : : issue has already been reported). */
10094 : :
10095 : : void
10096 : 879 : maybe_add_include_fixit (rich_location *richloc, const char *header,
10097 : : bool override_location)
10098 : : {
10099 : 879 : location_t loc = richloc->get_loc ();
10100 : 879 : const char *file = LOCATION_FILE (loc);
10101 : 879 : if (!file)
10102 : 452 : return;
10103 : :
10104 : : /* Idempotency: don't add the same header more than once to a given file. */
10105 : 879 : if (!added_includes)
10106 : 344 : added_includes = new added_includes_t ();
10107 : 879 : per_file_includes_t *&set = added_includes->get_or_insert (file);
10108 : 879 : if (set)
10109 : 535 : if (set->contains (header))
10110 : : /* ...then we've already added HEADER to that file. */
10111 : : return;
10112 : 427 : if (!set)
10113 : 344 : set = new per_file_includes_t ();
10114 : 427 : set->add (header);
10115 : :
10116 : : /* Attempt to locate a suitable place for the new directive. */
10117 : 427 : location_t include_insert_loc
10118 : 427 : = try_to_locate_new_include_insertion_point (file, loc);
10119 : 427 : if (include_insert_loc == UNKNOWN_LOCATION)
10120 : : return;
10121 : :
10122 : 427 : char *text = xasprintf ("#include %s\n", header);
10123 : 427 : richloc->add_fixit_insert_before (include_insert_loc, text);
10124 : 427 : free (text);
10125 : :
10126 : 427 : if (override_location && global_dc->get_source_printing_options ().enabled)
10127 : : {
10128 : : /* Replace the primary location with that of the insertion point for the
10129 : : fix-it hint.
10130 : :
10131 : : We use SHOW_LINES_WITHOUT_RANGE so that we don't meaningless print a
10132 : : caret for the insertion point (or colorize it).
10133 : :
10134 : : Hence we print e.g.:
10135 : :
10136 : : ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
10137 : : 73 | # include <debug/vector>
10138 : : +++ |+#include <vector>
10139 : : 74 | #endif
10140 : :
10141 : : rather than:
10142 : :
10143 : : ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
10144 : : 73 | # include <debug/vector>
10145 : : +++ |+#include <vector>
10146 : : 74 | #endif
10147 : : | ^
10148 : :
10149 : : avoiding the caret on the first column of line 74. */
10150 : 177 : richloc->set_range (0, include_insert_loc, SHOW_LINES_WITHOUT_RANGE);
10151 : : }
10152 : : }
10153 : :
10154 : : /* Attempt to convert a braced array initializer list CTOR for array
10155 : : TYPE into a STRING_CST for convenience and efficiency. Return
10156 : : the converted string on success or the original ctor on failure.
10157 : : Also, for non-convertable CTORs which contain RAW_DATA_CST values
10158 : : among the elts try to extend the range of RAW_DATA_CSTs. */
10159 : :
10160 : : static tree
10161 : 44787 : braced_list_to_string (tree type, tree ctor, bool member)
10162 : : {
10163 : : /* Ignore non-members with unknown size like arrays with unspecified
10164 : : bound. */
10165 : 44787 : tree typesize = TYPE_SIZE_UNIT (type);
10166 : 44787 : if (!member && !tree_fits_uhwi_p (typesize))
10167 : : return ctor;
10168 : :
10169 : : /* If the target char size differs from the host char size, we'd risk
10170 : : loosing data and getting object sizes wrong by converting to
10171 : : host chars. */
10172 : 44771 : if (TYPE_PRECISION (char_type_node) != CHAR_BIT)
10173 : : return ctor;
10174 : :
10175 : : /* STRING_CST doesn't support wide characters. */
10176 : 44771 : gcc_checking_assert (TYPE_PRECISION (TREE_TYPE (type)) == CHAR_BIT);
10177 : :
10178 : : /* If the array has an explicit bound, use it to constrain the size
10179 : : of the string. If it doesn't, be sure to create a string that's
10180 : : as long as implied by the index of the last zero specified via
10181 : : a designator, as in:
10182 : : const char a[] = { [7] = 0 }; */
10183 : 44771 : unsigned HOST_WIDE_INT maxelts;
10184 : 44771 : if (typesize)
10185 : : {
10186 : 44692 : maxelts = tree_to_uhwi (typesize);
10187 : 44692 : maxelts /= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
10188 : : }
10189 : : else
10190 : : maxelts = HOST_WIDE_INT_M1U;
10191 : :
10192 : : /* Avoid converting initializers for zero-length arrays (but do
10193 : : create them for flexible array members). */
10194 : 44692 : if (!maxelts)
10195 : : return ctor;
10196 : :
10197 : 44622 : unsigned HOST_WIDE_INT nelts = CONSTRUCTOR_NELTS (ctor);
10198 : :
10199 : 44622 : auto_vec<char> str;
10200 : 44622 : str.reserve (nelts + 1);
10201 : :
10202 : 44622 : unsigned HOST_WIDE_INT i, j = HOST_WIDE_INT_M1U;
10203 : 44622 : tree index, value;
10204 : 44622 : bool check_raw_data = false;
10205 : :
10206 : 634606 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, index, value)
10207 : : {
10208 : 589984 : if (check_raw_data)
10209 : : {
10210 : : /* The preprocessor always surrounds CPP_EMBED tokens in between
10211 : : CPP_NUMBER and CPP_COMMA tokens. Try to undo that here now that
10212 : : the whole initializer is parsed. E.g. if we have
10213 : : [0] = 'T', [1] = "his is a #embed tex", [20] = 't'
10214 : : where the middle value is RAW_DATA_CST and in its owner this is
10215 : : surrounded by 'T' and 't' characters, we can create from it just
10216 : : [0] = "This is a #embed text"
10217 : : Similarly if a RAW_DATA_CST needs to be split into two parts
10218 : : because of designated init store but the stored value is actually
10219 : : the same as in the RAW_DATA_OWNER's memory we can merge multiple
10220 : : RAW_DATA_CSTs. */
10221 : 1229 : if (TREE_CODE (value) == RAW_DATA_CST
10222 : 551 : && index
10223 : 551 : && tree_fits_uhwi_p (index))
10224 : : {
10225 : 551 : tree owner = RAW_DATA_OWNER (value);
10226 : 551 : unsigned int start, end, k;
10227 : 551 : if (TREE_CODE (owner) == STRING_CST)
10228 : : {
10229 : 216 : start
10230 : 216 : = RAW_DATA_POINTER (value) - TREE_STRING_POINTER (owner);
10231 : 216 : end = TREE_STRING_LENGTH (owner) - RAW_DATA_LENGTH (value);
10232 : : }
10233 : : else
10234 : : {
10235 : 335 : gcc_checking_assert (TREE_CODE (owner) == RAW_DATA_CST);
10236 : 335 : start
10237 : 335 : = RAW_DATA_POINTER (value) - RAW_DATA_POINTER (owner);
10238 : 335 : end = RAW_DATA_LENGTH (owner) - RAW_DATA_LENGTH (value);
10239 : : }
10240 : 551 : end -= start;
10241 : 551 : unsigned HOST_WIDE_INT l = j == HOST_WIDE_INT_M1U ? i : j;
10242 : 987 : for (k = 0; k < start && k < l; ++k)
10243 : : {
10244 : 462 : constructor_elt *elt = CONSTRUCTOR_ELT (ctor, l - k - 1);
10245 : 924 : if (elt->index == NULL_TREE
10246 : 462 : || !tree_fits_uhwi_p (elt->index)
10247 : 462 : || !tree_fits_shwi_p (elt->value)
10248 : 1381 : || wi::to_widest (index) != (wi::to_widest (elt->index)
10249 : 1376 : + (k + 1)))
10250 : : break;
10251 : 457 : if (TYPE_UNSIGNED (TREE_TYPE (value)))
10252 : : {
10253 : 381 : if (tree_to_shwi (elt->value)
10254 : 381 : != *((const unsigned char *)
10255 : 381 : RAW_DATA_POINTER (value) - k - 1))
10256 : : break;
10257 : : }
10258 : 76 : else if (tree_to_shwi (elt->value)
10259 : 76 : != *((const signed char *)
10260 : 76 : RAW_DATA_POINTER (value) - k - 1))
10261 : : break;
10262 : : }
10263 : 551 : start = k;
10264 : 551 : l = 0;
10265 : 1436 : for (k = 0; k < end && k + 1 < CONSTRUCTOR_NELTS (ctor) - i; ++k)
10266 : : {
10267 : 449 : constructor_elt *elt = CONSTRUCTOR_ELT (ctor, i + k + 1);
10268 : 898 : if (elt->index == NULL_TREE
10269 : 449 : || !tree_fits_uhwi_p (elt->index)
10270 : 1347 : || (wi::to_widest (elt->index)
10271 : 898 : != (wi::to_widest (index)
10272 : 1347 : + (RAW_DATA_LENGTH (value) + l))))
10273 : : break;
10274 : 449 : if (TREE_CODE (elt->value) == RAW_DATA_CST
10275 : 2 : && RAW_DATA_OWNER (elt->value) == RAW_DATA_OWNER (value)
10276 : 449 : && (RAW_DATA_POINTER (elt->value)
10277 : 2 : == RAW_DATA_POINTER (value) + l))
10278 : : {
10279 : 0 : l += RAW_DATA_LENGTH (elt->value);
10280 : 0 : end -= RAW_DATA_LENGTH (elt->value) - 1;
10281 : 0 : continue;
10282 : : }
10283 : 449 : if (!tree_fits_shwi_p (elt->value))
10284 : : break;
10285 : 446 : if (TYPE_UNSIGNED (TREE_TYPE (value)))
10286 : : {
10287 : 392 : if (tree_to_shwi (elt->value)
10288 : 392 : != *((const unsigned char *)
10289 : 392 : RAW_DATA_POINTER (value)
10290 : 392 : + RAW_DATA_LENGTH (value) + k))
10291 : : break;
10292 : : }
10293 : 54 : else if (tree_to_shwi (elt->value)
10294 : 54 : != *((const signed char *)
10295 : 54 : RAW_DATA_POINTER (value)
10296 : 54 : + RAW_DATA_LENGTH (value) + k))
10297 : : break;
10298 : 411 : ++l;
10299 : : }
10300 : 551 : end = k;
10301 : 551 : if (start != 0 || end != 0)
10302 : : {
10303 : 458 : if (j == HOST_WIDE_INT_M1U)
10304 : 420 : j = i - start;
10305 : : else
10306 : 38 : j -= start;
10307 : 458 : value = copy_node (value);
10308 : 458 : RAW_DATA_POINTER (value) -= start;
10309 : 458 : RAW_DATA_LENGTH (value) += start + end;
10310 : 458 : i += end;
10311 : 458 : if (start == 0)
10312 : 22 : CONSTRUCTOR_ELT (ctor, j)->index = index;
10313 : 458 : CONSTRUCTOR_ELT (ctor, j)->value = value;
10314 : 458 : ++j;
10315 : 458 : continue;
10316 : : }
10317 : : }
10318 : 771 : if (j != HOST_WIDE_INT_M1U)
10319 : : {
10320 : 175 : CONSTRUCTOR_ELT (ctor, j)->index = index;
10321 : 175 : CONSTRUCTOR_ELT (ctor, j)->value = value;
10322 : 175 : ++j;
10323 : : }
10324 : 771 : continue;
10325 : 771 : }
10326 : :
10327 : 588755 : unsigned HOST_WIDE_INT idx = i;
10328 : 588755 : if (index)
10329 : : {
10330 : 588755 : if (!tree_fits_uhwi_p (index))
10331 : : {
10332 : 22 : check_raw_data = true;
10333 : 22 : continue;
10334 : : }
10335 : 588733 : idx = tree_to_uhwi (index);
10336 : : }
10337 : :
10338 : : /* auto_vec is limited to UINT_MAX elements. */
10339 : 588733 : if (idx > UINT_MAX)
10340 : : {
10341 : 0 : check_raw_data = true;
10342 : 0 : continue;
10343 : : }
10344 : :
10345 : : /* Avoid non-constant initializers. */
10346 : 588733 : if (!tree_fits_shwi_p (value))
10347 : : {
10348 : 675 : check_raw_data = true;
10349 : 675 : --i;
10350 : 675 : continue;
10351 : : }
10352 : :
10353 : : /* Skip over embedded nuls except the last one (initializer
10354 : : elements are in ascending order of indices). */
10355 : 588058 : HOST_WIDE_INT val = tree_to_shwi (value);
10356 : 588058 : if (!val && i + 1 < nelts)
10357 : 5938 : continue;
10358 : :
10359 : 582120 : if (idx < str.length ())
10360 : : {
10361 : 0 : check_raw_data = true;
10362 : 0 : continue;
10363 : : }
10364 : :
10365 : : /* Bail if the CTOR has a block of more than 256 embedded nuls
10366 : : due to implicitly initialized elements. */
10367 : 582120 : unsigned nchars = (idx - str.length ()) + 1;
10368 : 582120 : if (nchars > 256)
10369 : : {
10370 : 3 : check_raw_data = true;
10371 : 3 : continue;
10372 : : }
10373 : :
10374 : 582117 : if (nchars > 1)
10375 : : {
10376 : 3643 : str.reserve (idx);
10377 : 3643 : str.quick_grow_cleared (idx);
10378 : : }
10379 : :
10380 : 582117 : if (idx >= maxelts)
10381 : : {
10382 : 0 : check_raw_data = true;
10383 : 0 : continue;
10384 : : }
10385 : :
10386 : 582117 : str.safe_insert (idx, val);
10387 : : }
10388 : :
10389 : 44622 : if (check_raw_data)
10390 : : {
10391 : 700 : if (j != HOST_WIDE_INT_M1U)
10392 : 420 : CONSTRUCTOR_ELTS (ctor)->truncate (j);
10393 : 700 : return ctor;
10394 : : }
10395 : :
10396 : : /* Append a nul string termination. */
10397 : 87767 : if (maxelts != HOST_WIDE_INT_M1U && str.length () < maxelts)
10398 : 17197 : str.safe_push (0);
10399 : :
10400 : : /* Build a STRING_CST with the same type as the array. */
10401 : 131766 : tree res = build_string (str.length (), str.begin ());
10402 : 43922 : TREE_TYPE (res) = type;
10403 : 43922 : return res;
10404 : 44622 : }
10405 : :
10406 : : /* Implementation of the two-argument braced_lists_to_string withe
10407 : : the same arguments plus MEMBER which is set for struct members
10408 : : to allow initializers for flexible member arrays. */
10409 : :
10410 : : static tree
10411 : 179017963 : braced_lists_to_strings (tree type, tree ctor, bool member)
10412 : : {
10413 : 179017963 : if (TREE_CODE (ctor) != CONSTRUCTOR)
10414 : : return ctor;
10415 : :
10416 : 5149903 : tree_code code = TREE_CODE (type);
10417 : :
10418 : 5149903 : tree ttp;
10419 : 5149903 : if (code == ARRAY_TYPE)
10420 : 207517 : ttp = TREE_TYPE (type);
10421 : 4942386 : else if (code == RECORD_TYPE)
10422 : : {
10423 : 4896356 : ttp = TREE_TYPE (ctor);
10424 : 4896356 : if (TREE_CODE (ttp) == ARRAY_TYPE)
10425 : : {
10426 : 222045 : type = ttp;
10427 : 222045 : ttp = TREE_TYPE (ttp);
10428 : : }
10429 : : }
10430 : : else
10431 : : return ctor;
10432 : :
10433 : 5101985 : if ((TREE_CODE (ttp) == ARRAY_TYPE || TREE_CODE (ttp) == INTEGER_TYPE)
10434 : 5383443 : && TYPE_STRING_FLAG (ttp))
10435 : 44787 : return braced_list_to_string (type, ctor, member);
10436 : :
10437 : 5059086 : code = TREE_CODE (ttp);
10438 : 5059086 : if (code == ARRAY_TYPE || RECORD_OR_UNION_TYPE_P (ttp))
10439 : : {
10440 : 4728643 : bool rec = RECORD_OR_UNION_TYPE_P (ttp);
10441 : :
10442 : : /* Handle array of arrays or struct member initializers. */
10443 : 4728643 : tree val;
10444 : 4728643 : unsigned HOST_WIDE_INT idx;
10445 : 15441657 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), idx, val)
10446 : : {
10447 : 10713014 : val = braced_lists_to_strings (ttp, val, rec);
10448 : 10713014 : CONSTRUCTOR_ELT (ctor, idx)->value = val;
10449 : : }
10450 : : }
10451 : :
10452 : : return ctor;
10453 : : }
10454 : :
10455 : : /* Attempt to convert a CTOR containing braced array initializer lists
10456 : : for array TYPE into one containing STRING_CSTs, for convenience and
10457 : : efficiency. Recurse for arrays of arrays and member initializers.
10458 : : Return the converted CTOR or STRING_CST on success or the original
10459 : : CTOR otherwise. */
10460 : :
10461 : : tree
10462 : 168304949 : braced_lists_to_strings (tree type, tree ctor)
10463 : : {
10464 : 168304949 : return braced_lists_to_strings (type, ctor, false);
10465 : : }
10466 : :
10467 : :
10468 : : /* Emit debug for functions before finalizing early debug. */
10469 : :
10470 : : void
10471 : 179678 : c_common_finalize_early_debug (void)
10472 : : {
10473 : : /* Emit early debug for reachable functions, and by consequence,
10474 : : locally scoped symbols. Also emit debug for extern declared
10475 : : functions that are still reachable at this point. */
10476 : 179678 : struct cgraph_node *cnode;
10477 : 8470182 : FOR_EACH_FUNCTION (cnode)
10478 : 3684615 : if (!cnode->alias && !cnode->thunk
10479 : 7736967 : && (cnode->has_gimple_body_p ()
10480 : 1270907 : || !DECL_IS_UNDECLARED_BUILTIN (cnode->decl)))
10481 : 3466741 : (*debug_hooks->early_global_decl) (cnode->decl);
10482 : 179678 : }
10483 : :
10484 : : /* Determine whether TYPE is an ISO C99 flexible array member type "[]". */
10485 : : bool
10486 : 10456479 : c_flexible_array_member_type_p (const_tree type)
10487 : : {
10488 : 10456479 : if (TREE_CODE (type) == ARRAY_TYPE
10489 : 3587060 : && TYPE_SIZE (type) == NULL_TREE
10490 : 465673 : && TYPE_DOMAIN (type) != NULL_TREE
10491 : 10922152 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
10492 : 465673 : return true;
10493 : :
10494 : : return false;
10495 : : }
10496 : :
10497 : : /* Get the LEVEL of the strict_flex_array for the ARRAY_FIELD based on the
10498 : : values of attribute strict_flex_array and the flag_strict_flex_arrays. */
10499 : : unsigned int
10500 : 529283 : c_strict_flex_array_level_of (tree array_field)
10501 : : {
10502 : 529283 : gcc_assert (TREE_CODE (array_field) == FIELD_DECL);
10503 : 529283 : unsigned int strict_flex_array_level = flag_strict_flex_arrays;
10504 : :
10505 : 529283 : tree attr_strict_flex_array
10506 : 529283 : = lookup_attribute ("strict_flex_array", DECL_ATTRIBUTES (array_field));
10507 : : /* If there is a strict_flex_array attribute attached to the field,
10508 : : override the flag_strict_flex_arrays. */
10509 : 529283 : if (attr_strict_flex_array)
10510 : : {
10511 : : /* Get the value of the level first from the attribute. */
10512 : 6 : unsigned HOST_WIDE_INT attr_strict_flex_array_level = 0;
10513 : 6 : gcc_assert (TREE_VALUE (attr_strict_flex_array) != NULL_TREE);
10514 : 6 : attr_strict_flex_array = TREE_VALUE (attr_strict_flex_array);
10515 : 6 : gcc_assert (TREE_VALUE (attr_strict_flex_array) != NULL_TREE);
10516 : 6 : attr_strict_flex_array = TREE_VALUE (attr_strict_flex_array);
10517 : 6 : gcc_assert (tree_fits_uhwi_p (attr_strict_flex_array));
10518 : 6 : attr_strict_flex_array_level = tree_to_uhwi (attr_strict_flex_array);
10519 : :
10520 : : /* The attribute has higher priority than flag_struct_flex_array. */
10521 : 6 : strict_flex_array_level = attr_strict_flex_array_level;
10522 : : }
10523 : 529283 : return strict_flex_array_level;
10524 : : }
10525 : :
10526 : : /* Map from identifiers to booleans. Value is true for features, and
10527 : : false for extensions. Used to implement __has_{feature,extension}. */
10528 : :
10529 : : using feature_map_t = hash_map <tree, bool>;
10530 : : static feature_map_t *feature_map;
10531 : :
10532 : : /* Register a feature for __has_{feature,extension}. FEATURE_P is true
10533 : : if the feature identified by NAME is a feature (as opposed to an
10534 : : extension). */
10535 : :
10536 : : void
10537 : 1423561 : c_common_register_feature (const char *name, bool feature_p)
10538 : : {
10539 : 1423561 : bool dup = feature_map->put (get_identifier (name), feature_p);
10540 : 1423561 : gcc_checking_assert (!dup);
10541 : 1423561 : }
10542 : :
10543 : : /* Lazily initialize hash table for __has_{feature,extension},
10544 : : dispatching to the appropriate front end to register language-specific
10545 : : features. */
10546 : :
10547 : : static void
10548 : 46491 : init_has_feature ()
10549 : : {
10550 : 46491 : gcc_checking_assert (!feature_map);
10551 : 46491 : feature_map = new feature_map_t;
10552 : :
10553 : 557892 : for (unsigned i = 0; i < ARRAY_SIZE (has_feature_table); i++)
10554 : : {
10555 : 511401 : const hf_feature_info *info = has_feature_table + i;
10556 : :
10557 : 511401 : if ((info->flags & HF_FLAG_SANITIZE) && !(flag_sanitize & info->mask))
10558 : 231579 : continue;
10559 : :
10560 : 279822 : const bool feature_p = !(info->flags & HF_FLAG_EXT);
10561 : 279822 : c_common_register_feature (info->ident, feature_p);
10562 : : }
10563 : :
10564 : : /* Register language-specific features. */
10565 : 46491 : c_family_register_lang_features ();
10566 : 46491 : }
10567 : :
10568 : : /* If STRICT_P is true, evaluate __has_feature (IDENT).
10569 : : Otherwise, evaluate __has_extension (IDENT). */
10570 : :
10571 : : bool
10572 : 126200 : has_feature_p (const char *ident, bool strict_p)
10573 : : {
10574 : 126200 : if (!feature_map)
10575 : 46491 : init_has_feature ();
10576 : :
10577 : 126200 : tree name = canonicalize_attr_name (get_identifier (ident));
10578 : 126200 : bool *feat_p = feature_map->get (name);
10579 : 126200 : if (!feat_p)
10580 : : return false;
10581 : :
10582 : 72134 : return !strict_p || *feat_p;
10583 : : }
10584 : :
10585 : : /* This is the slow path of c-common.h's c_hardbool_type_attr. */
10586 : :
10587 : : tree
10588 : 66527 : c_hardbool_type_attr_1 (tree type, tree *false_value, tree *true_value)
10589 : : {
10590 : 66527 : tree attr = lookup_attribute ("hardbool", TYPE_ATTRIBUTES (type));
10591 : 66527 : if (!attr)
10592 : : return attr;
10593 : :
10594 : 35375 : if (false_value)
10595 : 18453 : *false_value = TREE_VALUE (TYPE_VALUES (type));
10596 : :
10597 : 35375 : if (true_value)
10598 : 18817 : *true_value = TREE_VALUE (TREE_CHAIN (TYPE_VALUES (type)));
10599 : :
10600 : : return attr;
10601 : : }
10602 : :
10603 : : #include "gt-c-family-c-common.h"
|