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 : : { "_Maxof", RID_MAXOF, D_CONLY },
401 : : { "_Minof", RID_MINOF, D_CONLY },
402 : : { "_Atomic", RID_ATOMIC, D_CONLY },
403 : : { "_BitInt", RID_BITINT, D_CONLY },
404 : : { "_Bool", RID_BOOL, D_CONLY },
405 : : { "_Complex", RID_COMPLEX, 0 },
406 : : { "_Imaginary", RID_IMAGINARY, D_CONLY },
407 : : { "_Float16", RID_FLOAT16, 0 },
408 : : { "_Float32", RID_FLOAT32, 0 },
409 : : { "_Float64", RID_FLOAT64, 0 },
410 : : { "_Float128", RID_FLOAT128, 0 },
411 : : { "_Float32x", RID_FLOAT32X, 0 },
412 : : { "_Float64x", RID_FLOAT64X, 0 },
413 : : { "_Float128x", RID_FLOAT128X, 0 },
414 : : { "_Decimal32", RID_DFLOAT32, D_CONLY },
415 : : { "_Decimal64", RID_DFLOAT64, D_CONLY },
416 : : { "_Decimal128", RID_DFLOAT128, D_CONLY },
417 : : { "_Decimal64x", RID_DFLOAT64X, D_CONLY },
418 : : { "_Fract", RID_FRACT, D_CONLY | D_EXT },
419 : : { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
420 : : { "_Sat", RID_SAT, D_CONLY | D_EXT },
421 : : { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
422 : : { "_Noreturn", RID_NORETURN, D_CONLY },
423 : : { "_Generic", RID_GENERIC, D_CONLY },
424 : : { "_Thread_local", RID_THREAD, D_CONLY },
425 : : { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
426 : : { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
427 : : { "__alignof", RID_ALIGNOF, 0 },
428 : : { "__alignof__", RID_ALIGNOF, 0 },
429 : : { "__asm", RID_ASM, 0 },
430 : : { "__asm__", RID_ASM, 0 },
431 : : { "__attribute", RID_ATTRIBUTE, 0 },
432 : : { "__attribute__", RID_ATTRIBUTE, 0 },
433 : : { "__auto_type", RID_AUTO_TYPE, D_CONLY },
434 : : { "__builtin_addressof", RID_ADDRESSOF, D_CXXONLY },
435 : : { "__builtin_assoc_barrier", RID_BUILTIN_ASSOC_BARRIER, 0 },
436 : : { "__builtin_bit_cast", RID_BUILTIN_BIT_CAST, D_CXXONLY },
437 : : { "__builtin_call_with_static_chain",
438 : : RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
439 : : { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
440 : : { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
441 : : { "__builtin_convertvector", RID_BUILTIN_CONVERTVECTOR, 0 },
442 : : { "__builtin_counted_by_ref", RID_BUILTIN_COUNTED_BY_REF, D_CONLY },
443 : : { "__builtin_has_attribute", RID_BUILTIN_HAS_ATTRIBUTE, 0 },
444 : : { "__builtin_launder", RID_BUILTIN_LAUNDER, D_CXXONLY },
445 : : { "__builtin_operator_new", RID_BUILTIN_OPERATOR_NEW, D_CXXONLY },
446 : : { "__builtin_operator_delete", RID_BUILTIN_OPERATOR_DELETE, D_CXXONLY },
447 : : { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
448 : : { "__builtin_shufflevector", RID_BUILTIN_SHUFFLEVECTOR, 0 },
449 : : { "__builtin_stdc_bit_ceil", RID_BUILTIN_STDC, D_CONLY },
450 : : { "__builtin_stdc_bit_floor", RID_BUILTIN_STDC, D_CONLY },
451 : : { "__builtin_stdc_bit_width", RID_BUILTIN_STDC, D_CONLY },
452 : : { "__builtin_stdc_count_ones", RID_BUILTIN_STDC, D_CONLY },
453 : : { "__builtin_stdc_count_zeros", RID_BUILTIN_STDC, D_CONLY },
454 : : { "__builtin_stdc_first_leading_one", RID_BUILTIN_STDC, D_CONLY },
455 : : { "__builtin_stdc_first_leading_zero", RID_BUILTIN_STDC, D_CONLY },
456 : : { "__builtin_stdc_first_trailing_one", RID_BUILTIN_STDC, D_CONLY },
457 : : { "__builtin_stdc_first_trailing_zero", RID_BUILTIN_STDC, D_CONLY },
458 : : { "__builtin_stdc_has_single_bit", RID_BUILTIN_STDC, D_CONLY },
459 : : { "__builtin_stdc_leading_ones", RID_BUILTIN_STDC, D_CONLY },
460 : : { "__builtin_stdc_leading_zeros", RID_BUILTIN_STDC, D_CONLY },
461 : : { "__builtin_stdc_rotate_left", RID_BUILTIN_STDC, D_CONLY },
462 : : { "__builtin_stdc_rotate_right", RID_BUILTIN_STDC, D_CONLY },
463 : : { "__builtin_stdc_trailing_ones", RID_BUILTIN_STDC, D_CONLY },
464 : : { "__builtin_stdc_trailing_zeros", RID_BUILTIN_STDC, D_CONLY },
465 : : { "__builtin_tgmath", RID_BUILTIN_TGMATH, D_CONLY },
466 : : { "__builtin_offsetof", RID_OFFSETOF, 0 },
467 : : { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
468 : : { "__builtin_c23_va_start", RID_C23_VA_START, D_C23 | D_CXX26 },
469 : : { "__builtin_va_arg", RID_VA_ARG, 0 },
470 : : { "__complex", RID_COMPLEX, 0 },
471 : : { "__complex__", RID_COMPLEX, 0 },
472 : : { "__const", RID_CONST, 0 },
473 : : { "__const__", RID_CONST, 0 },
474 : : { "__constinit", RID_CONSTINIT, D_CXXONLY },
475 : : { "__decltype", RID_DECLTYPE, D_CXXONLY },
476 : : { "__extension__", RID_EXTENSION, 0 },
477 : : { "__func__", RID_C99_FUNCTION_NAME, 0 },
478 : : { "__imag", RID_IMAGPART, 0 },
479 : : { "__imag__", RID_IMAGPART, 0 },
480 : : { "__inline", RID_INLINE, 0 },
481 : : { "__inline__", RID_INLINE, 0 },
482 : : { "__label__", RID_LABEL, 0 },
483 : : { "__null", RID_NULL, 0 },
484 : : { "__real", RID_REALPART, 0 },
485 : : { "__real__", RID_REALPART, 0 },
486 : : { "__restrict", RID_RESTRICT, 0 },
487 : : { "__restrict__", RID_RESTRICT, 0 },
488 : : { "__signed", RID_SIGNED, 0 },
489 : : { "__signed__", RID_SIGNED, 0 },
490 : : { "__thread", RID_THREAD, 0 },
491 : : { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
492 : : { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
493 : : { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
494 : : { "__typeof", RID_TYPEOF, 0 },
495 : : { "__typeof__", RID_TYPEOF, 0 },
496 : : { "__typeof_unqual", RID_TYPEOF_UNQUAL, D_CONLY },
497 : : { "__typeof_unqual__", RID_TYPEOF_UNQUAL, D_CONLY },
498 : : { "__volatile", RID_VOLATILE, 0 },
499 : : { "__volatile__", RID_VOLATILE, 0 },
500 : : { "__GIMPLE", RID_GIMPLE, D_CONLY },
501 : : { "__PHI", RID_PHI, D_CONLY },
502 : : { "__RTL", RID_RTL, D_CONLY },
503 : : { "alignas", RID_ALIGNAS, D_C23 | D_CXX11 | D_CXXWARN },
504 : : { "alignof", RID_ALIGNOF, D_C23 | D_CXX11 | D_CXXWARN },
505 : : { "asm", RID_ASM, D_ASM },
506 : : { "auto", RID_AUTO, 0 },
507 : : { "bool", RID_BOOL, D_C23 | D_CXXWARN },
508 : : { "break", RID_BREAK, 0 },
509 : : { "case", RID_CASE, 0 },
510 : : { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
511 : : { "char", RID_CHAR, 0 },
512 : : { "char8_t", RID_CHAR8, D_CXX_CHAR8_T_FLAGS | D_CXXWARN },
513 : : { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
514 : : { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
515 : : { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
516 : : { "const", RID_CONST, 0 },
517 : : { "consteval", RID_CONSTEVAL, D_CXXONLY | D_CXX20 | D_CXXWARN },
518 : : { "constexpr", RID_CONSTEXPR, D_C23 | D_CXX11 | D_CXXWARN },
519 : : { "constinit", RID_CONSTINIT, D_CXXONLY | D_CXX20 | D_CXXWARN },
520 : : { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
521 : : { "continue", RID_CONTINUE, 0 },
522 : : { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
523 : : { "default", RID_DEFAULT, 0 },
524 : : { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
525 : : { "do", RID_DO, 0 },
526 : : { "double", RID_DOUBLE, 0 },
527 : : { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
528 : : { "else", RID_ELSE, 0 },
529 : : { "enum", RID_ENUM, 0 },
530 : : { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
531 : : { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
532 : : { "extern", RID_EXTERN, 0 },
533 : : { "false", RID_FALSE, D_C23 | D_CXXWARN },
534 : : { "float", RID_FLOAT, 0 },
535 : : { "for", RID_FOR, 0 },
536 : : { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
537 : : { "goto", RID_GOTO, 0 },
538 : : { "if", RID_IF, 0 },
539 : : { "inline", RID_INLINE, D_EXT89 },
540 : : { "int", RID_INT, 0 },
541 : : { "long", RID_LONG, 0 },
542 : : { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
543 : : { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
544 : : { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
545 : : { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
546 : : { "nullptr", RID_NULLPTR, D_C23 | D_CXX11 | D_CXXWARN },
547 : : { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
548 : : { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
549 : : { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
550 : : { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
551 : : { "register", RID_REGISTER, 0 },
552 : : { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
553 : : { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
554 : : { "return", RID_RETURN, 0 },
555 : : { "short", RID_SHORT, 0 },
556 : : { "signed", RID_SIGNED, 0 },
557 : : { "sizeof", RID_SIZEOF, 0 },
558 : : { "static", RID_STATIC, 0 },
559 : : { "static_assert", RID_STATIC_ASSERT, D_C23 | D_CXX11 | D_CXXWARN },
560 : : { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
561 : : { "struct", RID_STRUCT, 0 },
562 : : { "switch", RID_SWITCH, 0 },
563 : : { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
564 : : { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
565 : : { "thread_local", RID_THREAD, D_C23 | D_CXX11 | D_CXXWARN },
566 : : { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
567 : : { "true", RID_TRUE, D_C23 | D_CXXWARN },
568 : : { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
569 : : { "typedef", RID_TYPEDEF, 0 },
570 : : { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
571 : : { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
572 : : { "typeof", RID_TYPEOF, D_EXT11 },
573 : : { "typeof_unqual", RID_TYPEOF_UNQUAL, D_CONLY | D_C23 },
574 : : { "union", RID_UNION, 0 },
575 : : { "unsigned", RID_UNSIGNED, 0 },
576 : : { "using", RID_USING, D_CXXONLY | D_CXXWARN },
577 : : { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
578 : : { "void", RID_VOID, 0 },
579 : : { "volatile", RID_VOLATILE, 0 },
580 : : { "wchar_t", RID_WCHAR, D_CXXONLY },
581 : : { "while", RID_WHILE, 0 },
582 : :
583 : : /* C++ transactional memory. */
584 : : { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
585 : : { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
586 : : { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
587 : : { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
588 : :
589 : : /* Concepts-related keywords */
590 : : { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
591 : : { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
592 : :
593 : : /* Modules-related keywords, these are internal unspellable tokens,
594 : : created by the preprocessor. */
595 : : { "module ", RID__MODULE, D_CXX_MODULES_FLAGS | D_CXXWARN },
596 : : { "import ", RID__IMPORT, D_CXX_MODULES_FLAGS | D_CXXWARN },
597 : : { "export ", RID__EXPORT, D_CXX_MODULES_FLAGS | D_CXXWARN },
598 : :
599 : : /* Coroutines-related keywords */
600 : : { "co_await", RID_CO_AWAIT, D_CXX_COROUTINES_FLAGS | D_CXXWARN },
601 : : { "co_yield", RID_CO_YIELD, D_CXX_COROUTINES_FLAGS | D_CXXWARN },
602 : : { "co_return", RID_CO_RETURN, D_CXX_COROUTINES_FLAGS | D_CXXWARN },
603 : :
604 : : /* These Objective-C keywords are recognized only immediately after
605 : : an '@'. */
606 : : { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
607 : : { "defs", RID_AT_DEFS, D_OBJC },
608 : : { "encode", RID_AT_ENCODE, D_OBJC },
609 : : { "end", RID_AT_END, D_OBJC },
610 : : { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
611 : : { "interface", RID_AT_INTERFACE, D_OBJC },
612 : : { "protocol", RID_AT_PROTOCOL, D_OBJC },
613 : : { "selector", RID_AT_SELECTOR, D_OBJC },
614 : : { "finally", RID_AT_FINALLY, D_OBJC },
615 : : { "optional", RID_AT_OPTIONAL, D_OBJC },
616 : : { "required", RID_AT_REQUIRED, D_OBJC },
617 : : { "property", RID_AT_PROPERTY, D_OBJC },
618 : : { "package", RID_AT_PACKAGE, D_OBJC },
619 : : { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
620 : : { "dynamic", RID_AT_DYNAMIC, D_OBJC },
621 : : /* These are recognized only in protocol-qualifier context
622 : : (see above) */
623 : : { "bycopy", RID_BYCOPY, D_OBJC },
624 : : { "byref", RID_BYREF, D_OBJC },
625 : : { "in", RID_IN, D_OBJC },
626 : : { "inout", RID_INOUT, D_OBJC },
627 : : { "oneway", RID_ONEWAY, D_OBJC },
628 : : { "out", RID_OUT, D_OBJC },
629 : : /* These are recognized inside a property attribute list */
630 : : { "assign", RID_ASSIGN, D_OBJC },
631 : : { "atomic", RID_PROPATOMIC, D_OBJC },
632 : : { "copy", RID_COPY, D_OBJC },
633 : : { "getter", RID_GETTER, D_OBJC },
634 : : { "nonatomic", RID_NONATOMIC, D_OBJC },
635 : : { "readonly", RID_READONLY, D_OBJC },
636 : : { "readwrite", RID_READWRITE, D_OBJC },
637 : : { "retain", RID_RETAIN, D_OBJC },
638 : : { "setter", RID_SETTER, D_OBJC },
639 : : /* These are Objective C implementation of nullability, accepted only in
640 : : specific contexts. */
641 : : { "null_unspecified", RID_NULL_UNSPECIFIED, D_OBJC },
642 : : { "nullable", RID_NULLABLE, D_OBJC },
643 : : { "nonnull", RID_NONNULL, D_OBJC },
644 : : { "null_resettable", RID_NULL_RESETTABLE, D_OBJC },
645 : : };
646 : :
647 : : const unsigned int num_c_common_reswords = ARRAY_SIZE (c_common_reswords);
648 : :
649 : : /* Return identifier for address space AS. */
650 : :
651 : : const char *
652 : 3 : c_addr_space_name (addr_space_t as)
653 : : {
654 : 3 : int rid = RID_FIRST_ADDR_SPACE + as;
655 : 3 : gcc_assert (ridpointers [rid]);
656 : 3 : return IDENTIFIER_POINTER (ridpointers [rid]);
657 : : }
658 : :
659 : : /* Push current bindings for the function name VAR_DECLS. */
660 : :
661 : : void
662 : 184276380 : start_fname_decls (void)
663 : : {
664 : 184276380 : unsigned ix;
665 : 184276380 : tree saved = NULL_TREE;
666 : :
667 : 737105520 : for (ix = 0; fname_vars[ix].decl; ix++)
668 : : {
669 : 552829140 : tree decl = *fname_vars[ix].decl;
670 : :
671 : 552829140 : if (decl)
672 : : {
673 : 55028 : saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
674 : : saved);
675 : 55028 : *fname_vars[ix].decl = NULL_TREE;
676 : : }
677 : : }
678 : 184276380 : if (saved || saved_function_name_decls)
679 : : /* Normally they'll have been NULL, so only push if we've got a
680 : : stack, or they are non-NULL. */
681 : 74832 : saved_function_name_decls = tree_cons (saved, NULL_TREE,
682 : : saved_function_name_decls);
683 : 184276380 : }
684 : :
685 : : /* Finish up the current bindings, adding them into the current function's
686 : : statement tree. This must be done _before_ finish_stmt_tree is called.
687 : : If there is no current function, we must be at file scope and no statements
688 : : are involved. Pop the previous bindings. */
689 : :
690 : : void
691 : 184160830 : finish_fname_decls (void)
692 : : {
693 : 184160830 : unsigned ix;
694 : 184160830 : tree stmts = NULL_TREE;
695 : 184160830 : tree stack = saved_function_name_decls;
696 : :
697 : 184456932 : for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
698 : 110635 : append_to_statement_list (TREE_VALUE (stack), &stmts);
699 : :
700 : 184160830 : if (stmts)
701 : : {
702 : 110530 : tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
703 : :
704 : 110530 : if (TREE_CODE (*bodyp) == BIND_EXPR)
705 : 105223 : bodyp = &BIND_EXPR_BODY (*bodyp);
706 : :
707 : 110530 : append_to_statement_list_force (*bodyp, &stmts);
708 : 110530 : *bodyp = stmts;
709 : : }
710 : :
711 : 736643320 : for (ix = 0; fname_vars[ix].decl; ix++)
712 : 552482490 : *fname_vars[ix].decl = NULL_TREE;
713 : :
714 : 184160830 : if (stack)
715 : : {
716 : : /* We had saved values, restore them. */
717 : 74832 : tree saved;
718 : :
719 : 129860 : for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
720 : : {
721 : 55028 : tree decl = TREE_PURPOSE (saved);
722 : 55028 : unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
723 : :
724 : 55028 : *fname_vars[ix].decl = decl;
725 : : }
726 : 74832 : stack = TREE_CHAIN (stack);
727 : : }
728 : 184160830 : saved_function_name_decls = stack;
729 : 184160830 : }
730 : :
731 : : /* Return the text name of the current function, suitably prettified
732 : : by PRETTY_P. Return string must be freed by caller. */
733 : :
734 : : const char *
735 : 3507 : fname_as_string (int pretty_p)
736 : : {
737 : 3507 : const char *name = "top level";
738 : 3507 : char *namep;
739 : 3507 : int vrb = 2, len;
740 : 3507 : cpp_string cstr = { 0, 0 }, strname;
741 : :
742 : 3507 : if (!pretty_p)
743 : : {
744 : 1896 : name = "";
745 : 1896 : vrb = 0;
746 : : }
747 : :
748 : 3507 : if (current_function_decl)
749 : 3500 : name = lang_hooks.decl_printable_name (current_function_decl, vrb);
750 : :
751 : 3507 : len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
752 : :
753 : 3507 : namep = XNEWVEC (char, len);
754 : 3507 : snprintf (namep, len, "\"%s\"", name);
755 : 3507 : strname.text = (unsigned char *) namep;
756 : 3507 : strname.len = len - 1;
757 : :
758 : 3507 : if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
759 : : {
760 : 3507 : XDELETEVEC (namep);
761 : 3507 : return (const char *) cstr.text;
762 : : }
763 : :
764 : : return namep;
765 : : }
766 : :
767 : : /* Return the VAR_DECL for a const char array naming the current
768 : : function. If the VAR_DECL has not yet been created, create it
769 : : now. RID indicates how it should be formatted and IDENTIFIER_NODE
770 : : ID is its name (unfortunately C and C++ hold the RID values of
771 : : keywords in different places, so we can't derive RID from ID in
772 : : this language independent code. LOC is the location of the
773 : : function. */
774 : :
775 : : tree
776 : 213021 : fname_decl (location_t loc, unsigned int rid, tree id)
777 : : {
778 : 213021 : unsigned ix;
779 : 213021 : tree decl = NULL_TREE;
780 : :
781 : 618270 : for (ix = 0; fname_vars[ix].decl; ix++)
782 : 618270 : if (fname_vars[ix].rid == rid)
783 : : break;
784 : :
785 : 213021 : decl = *fname_vars[ix].decl;
786 : 213021 : if (!decl)
787 : : {
788 : : /* If a tree is built here, it would normally have the lineno of
789 : : the current statement. Later this tree will be moved to the
790 : : beginning of the function and this line number will be wrong.
791 : : To avoid this problem set the lineno to 0 here; that prevents
792 : : it from appearing in the RTL. */
793 : 110635 : tree stmts;
794 : 110635 : location_t saved_location = input_location;
795 : 110635 : input_location = UNKNOWN_LOCATION;
796 : :
797 : 110635 : stmts = push_stmt_list ();
798 : 110635 : decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
799 : 110635 : stmts = pop_stmt_list (stmts);
800 : 110635 : if (!IS_EMPTY_STMT (stmts))
801 : 110635 : saved_function_name_decls
802 : 110635 : = tree_cons (decl, stmts, saved_function_name_decls);
803 : 110635 : *fname_vars[ix].decl = decl;
804 : 110635 : input_location = saved_location;
805 : : }
806 : 213021 : if (!ix && !current_function_decl)
807 : 4 : pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
808 : :
809 : 213021 : return decl;
810 : : }
811 : :
812 : : /* Given a STRING_CST, give it a suitable array-of-chars data type. */
813 : :
814 : : tree
815 : 26696086 : fix_string_type (tree value)
816 : : {
817 : 26696086 : int length = TREE_STRING_LENGTH (value);
818 : 26696086 : int nchars, charsz;
819 : 26696086 : tree e_type, i_type, a_type;
820 : :
821 : : /* Compute the number of elements, for the array type. */
822 : 26696086 : if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
823 : : {
824 : 26406390 : charsz = 1;
825 : 26406390 : e_type = char_type_node;
826 : : }
827 : 289696 : else if (flag_char8_t && TREE_TYPE (value) == char8_array_type_node)
828 : : {
829 : 577 : charsz = TYPE_PRECISION (char8_type_node) / BITS_PER_UNIT;
830 : 577 : e_type = char8_type_node;
831 : : }
832 : 289119 : else if (TREE_TYPE (value) == char16_array_type_node)
833 : : {
834 : 956 : charsz = TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT;
835 : 956 : e_type = char16_type_node;
836 : : }
837 : 288163 : else if (TREE_TYPE (value) == char32_array_type_node)
838 : : {
839 : 3044 : charsz = TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT;
840 : 3044 : e_type = char32_type_node;
841 : : }
842 : : else
843 : : {
844 : 285119 : charsz = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
845 : 285119 : e_type = wchar_type_node;
846 : : }
847 : :
848 : : /* This matters only for targets where ssizetype has smaller precision
849 : : than 32 bits. */
850 : 26696086 : if (wi::lts_p (wi::to_wide (TYPE_MAX_VALUE (ssizetype)), length))
851 : : {
852 : 0 : error ("size of string literal is too large");
853 : 0 : length = tree_to_shwi (TYPE_MAX_VALUE (ssizetype)) / charsz * charsz;
854 : 0 : char *str = CONST_CAST (char *, TREE_STRING_POINTER (value));
855 : 0 : memset (str + length, '\0',
856 : 0 : MIN (TREE_STRING_LENGTH (value) - length, charsz));
857 : 0 : TREE_STRING_LENGTH (value) = length;
858 : : }
859 : 26696086 : nchars = length / charsz;
860 : :
861 : : /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
862 : : limit in C++98 Annex B is very large (65536) and is not normative,
863 : : so we do not diagnose it (warn_overlength_strings is forced off
864 : : in c_common_post_options). */
865 : 26696086 : if (warn_overlength_strings)
866 : : {
867 : 15086 : const int nchars_max = flag_isoc99 ? 4095 : 509;
868 : 15086 : const int relevant_std = flag_isoc99 ? 99 : 90;
869 : 15086 : if (nchars - 1 > nchars_max)
870 : : /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
871 : : separate the %d from the 'C'. 'ISO' should not be
872 : : translated, but it may be moved after 'C%d' in languages
873 : : where modifiers follow nouns. */
874 : 5 : pedwarn (input_location, OPT_Woverlength_strings,
875 : : "string length %qd is greater than the length %qd "
876 : : "ISO C%d compilers are required to support",
877 : : nchars - 1, nchars_max, relevant_std);
878 : : }
879 : :
880 : : /* Create the array type for the string constant. The ISO C++
881 : : standard says that a string literal has type `const char[N]' or
882 : : `const wchar_t[N]'. We use the same logic when invoked as a C
883 : : front-end with -Wwrite-strings.
884 : : ??? We should change the type of an expression depending on the
885 : : state of a warning flag. We should just be warning -- see how
886 : : this is handled in the C++ front-end for the deprecated implicit
887 : : conversion from string literals to `char*' or `wchar_t*'.
888 : :
889 : : The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
890 : : array type being the unqualified version of that type.
891 : : Therefore, if we are constructing an array of const char, we must
892 : : construct the matching unqualified array type first. The C front
893 : : end does not require this, but it does no harm, so we do it
894 : : unconditionally. */
895 : 26696086 : i_type = build_index_type (size_int (nchars - 1));
896 : 26696086 : a_type = build_array_type (e_type, i_type);
897 : 26696086 : if (c_dialect_cxx() || warn_write_strings)
898 : 24334560 : a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
899 : :
900 : 26696086 : TREE_TYPE (value) = a_type;
901 : 26696086 : TREE_CONSTANT (value) = 1;
902 : 26696086 : TREE_READONLY (value) = 1;
903 : 26696086 : TREE_STATIC (value) = 1;
904 : 26696086 : return value;
905 : : }
906 : :
907 : : /* Given a string of type STRING_TYPE, determine what kind of string
908 : : token would give an equivalent execution encoding: CPP_STRING,
909 : : CPP_STRING16, or CPP_STRING32. Return CPP_OTHER in case of error.
910 : : This may not be exactly the string token type that initially created
911 : : the string, since CPP_WSTRING is indistinguishable from the 16/32 bit
912 : : string type, and CPP_UTF8STRING is indistinguishable from CPP_STRING
913 : : at this point.
914 : :
915 : : This effectively reverses part of the logic in lex_string and
916 : : fix_string_type. */
917 : :
918 : : static enum cpp_ttype
919 : 11120 : get_cpp_ttype_from_string_type (tree string_type)
920 : : {
921 : 11120 : gcc_assert (string_type);
922 : 11120 : if (TREE_CODE (string_type) == POINTER_TYPE)
923 : 5981 : string_type = TREE_TYPE (string_type);
924 : :
925 : 11120 : if (TREE_CODE (string_type) != ARRAY_TYPE)
926 : : return CPP_OTHER;
927 : :
928 : 11120 : tree element_type = TREE_TYPE (string_type);
929 : 11120 : if (TREE_CODE (element_type) != INTEGER_TYPE)
930 : : return CPP_OTHER;
931 : :
932 : 11120 : int bits_per_character = TYPE_PRECISION (element_type);
933 : 11120 : switch (bits_per_character)
934 : : {
935 : : case 8:
936 : : return CPP_STRING; /* It could have also been CPP_UTF8STRING. */
937 : 2 : case 16:
938 : 2 : return CPP_STRING16;
939 : 4 : case 32:
940 : 4 : return CPP_STRING32;
941 : : }
942 : :
943 : : return CPP_OTHER;
944 : : }
945 : :
946 : : /* The global record of string concatentations, for use in
947 : : extracting locations within string literals. */
948 : :
949 : : GTY(()) string_concat_db *g_string_concat_db;
950 : :
951 : : /* Implementation of LANG_HOOKS_GET_SUBSTRING_LOCATION. */
952 : :
953 : : const char *
954 : 11120 : c_get_substring_location (const substring_loc &substr_loc,
955 : : location_t *out_loc)
956 : : {
957 : 11120 : enum cpp_ttype tok_type
958 : 11120 : = get_cpp_ttype_from_string_type (substr_loc.get_string_type ());
959 : 11120 : if (tok_type == CPP_OTHER)
960 : : return "unrecognized string type";
961 : :
962 : 11120 : return get_location_within_string (parse_in,
963 : : global_dc->get_file_cache (),
964 : : g_string_concat_db,
965 : : substr_loc.get_fmt_string_loc (),
966 : : tok_type,
967 : : substr_loc.get_caret_idx (),
968 : : substr_loc.get_start_idx (),
969 : : substr_loc.get_end_idx (),
970 : 11120 : out_loc);
971 : : }
972 : :
973 : :
974 : : /* Return true iff T is a boolean promoted to int. */
975 : :
976 : : bool
977 : 79842 : bool_promoted_to_int_p (tree t)
978 : : {
979 : 62710 : return (CONVERT_EXPR_P (t)
980 : 17232 : && TREE_TYPE (t) == integer_type_node
981 : 79942 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == BOOLEAN_TYPE);
982 : : }
983 : :
984 : : /* vector_targets_convertible_p is used for vector pointer types. The
985 : : callers perform various checks that the qualifiers are satisfactory,
986 : : while OTOH vector_targets_convertible_p ignores the number of elements
987 : : in the vectors. That's fine with vector pointers as we can consider,
988 : : say, a vector of 8 elements as two consecutive vectors of 4 elements,
989 : : and that does not require and conversion of the pointer values.
990 : : In contrast, vector_types_convertible_p and
991 : : vector_types_compatible_elements_p are used for vector value types. */
992 : : /* True if pointers to distinct types T1 and T2 can be converted to
993 : : each other without an explicit cast. Only returns true for opaque
994 : : vector types. */
995 : : bool
996 : 2027633 : vector_targets_convertible_p (const_tree t1, const_tree t2)
997 : : {
998 : 976663 : if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
999 : 976487 : && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1000 : 2027633 : && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1001 : : return true;
1002 : :
1003 : : return false;
1004 : : }
1005 : :
1006 : : /* vector_types_convertible_p is used for vector value types.
1007 : : It could in principle call vector_targets_convertible_p as a subroutine,
1008 : : but then the check for vector type would be duplicated with its callers,
1009 : : and also the purpose of vector_targets_convertible_p would become
1010 : : muddled.
1011 : : Where vector_types_convertible_p returns true, a conversion might still be
1012 : : needed to make the types match.
1013 : : In contrast, vector_targets_convertible_p is used for vector pointer
1014 : : values, and vector_types_compatible_elements_p is used specifically
1015 : : in the context for binary operators, as a check if use is possible without
1016 : : conversion. */
1017 : : /* True if vector types T1 and T2 can be converted to each other
1018 : : without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
1019 : : can only be converted with -flax-vector-conversions yet that is not
1020 : : in effect, emit a note telling the user about that option if such
1021 : : a note has not previously been emitted. */
1022 : : bool
1023 : 18521651 : vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
1024 : : {
1025 : 18521651 : static bool emitted_lax_note = false;
1026 : 18521651 : bool convertible_lax;
1027 : :
1028 : 37038211 : if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1029 : 18523194 : && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1030 : : return true;
1031 : :
1032 : 37030150 : convertible_lax =
1033 : 18515075 : (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
1034 : 18511523 : && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE
1035 : 10761124 : || known_eq (TYPE_VECTOR_SUBPARTS (t1),
1036 : : TYPE_VECTOR_SUBPARTS (t2)))
1037 : 55470533 : && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
1038 : 18477729 : == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
1039 : :
1040 : 18515075 : if (!convertible_lax || flag_lax_vector_conversions)
1041 : 73609 : return convertible_lax;
1042 : :
1043 : 18441466 : if (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1044 : 18441466 : && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1045 : 16874982 : return true;
1046 : :
1047 : 1566484 : if (emit_lax_note && !emitted_lax_note)
1048 : : {
1049 : 10 : emitted_lax_note = true;
1050 : 10 : inform (input_location, "use %<-flax-vector-conversions%> to permit "
1051 : : "conversions between vectors with differing "
1052 : : "element types or numbers of subparts");
1053 : : }
1054 : :
1055 : : return false;
1056 : : }
1057 : :
1058 : : /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
1059 : : and have vector types, V0 has the same type as V1, and the number of
1060 : : elements of V0, V1, MASK is the same.
1061 : :
1062 : : In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
1063 : : called with two arguments. In this case implementation passes the
1064 : : first argument twice in order to share the same tree code. This fact
1065 : : could enable the mask-values being twice the vector length. This is
1066 : : an implementation accident and this semantics is not guaranteed to
1067 : : the user. */
1068 : : tree
1069 : 486425 : c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
1070 : : bool complain)
1071 : : {
1072 : 486425 : tree ret;
1073 : 486425 : bool wrap = true;
1074 : 486425 : bool maybe_const = false;
1075 : 486425 : bool two_arguments = false;
1076 : :
1077 : 486425 : if (v1 == NULL_TREE)
1078 : : {
1079 : 444089 : two_arguments = true;
1080 : 444089 : v1 = v0;
1081 : : }
1082 : :
1083 : 486425 : if (v0 == error_mark_node || v1 == error_mark_node
1084 : 486425 : || mask == error_mark_node)
1085 : : return error_mark_node;
1086 : :
1087 : 486425 : if (!gnu_vector_type_p (TREE_TYPE (mask))
1088 : 486425 : || !VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
1089 : : {
1090 : 3 : if (complain)
1091 : 0 : error_at (loc, "%<__builtin_shuffle%> last argument must "
1092 : : "be an integer vector");
1093 : 3 : return error_mark_node;
1094 : : }
1095 : :
1096 : 486422 : if (!gnu_vector_type_p (TREE_TYPE (v0))
1097 : 486422 : || !gnu_vector_type_p (TREE_TYPE (v1)))
1098 : : {
1099 : 0 : if (complain)
1100 : 0 : error_at (loc, "%<__builtin_shuffle%> arguments must be vectors");
1101 : 0 : return error_mark_node;
1102 : : }
1103 : :
1104 : 486422 : if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
1105 : : {
1106 : 0 : if (complain)
1107 : 0 : error_at (loc, "%<__builtin_shuffle%> argument vectors must be of "
1108 : : "the same type");
1109 : 0 : return error_mark_node;
1110 : : }
1111 : :
1112 : 486422 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)),
1113 : 486422 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
1114 : 486422 : && maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)),
1115 : 0 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))))
1116 : : {
1117 : 0 : if (complain)
1118 : 0 : error_at (loc, "%<__builtin_shuffle%> number of elements of the "
1119 : : "argument vector(s) and the mask vector should "
1120 : : "be the same");
1121 : 0 : return error_mark_node;
1122 : : }
1123 : :
1124 : 486422 : if (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
1125 : 972844 : != GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
1126 : : {
1127 : 0 : if (complain)
1128 : 0 : error_at (loc, "%<__builtin_shuffle%> argument vector(s) inner type "
1129 : : "must have the same size as inner type of the mask");
1130 : 0 : return error_mark_node;
1131 : : }
1132 : :
1133 : 486422 : if (!c_dialect_cxx ())
1134 : : {
1135 : : /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
1136 : 468937 : v0 = c_fully_fold (v0, false, &maybe_const);
1137 : 468937 : wrap &= maybe_const;
1138 : :
1139 : 468937 : if (two_arguments)
1140 : 427470 : v1 = v0 = save_expr (v0);
1141 : : else
1142 : : {
1143 : 41467 : v1 = c_fully_fold (v1, false, &maybe_const);
1144 : 41467 : wrap &= maybe_const;
1145 : : }
1146 : :
1147 : 468937 : mask = c_fully_fold (mask, false, &maybe_const);
1148 : 468937 : wrap &= maybe_const;
1149 : : }
1150 : 17485 : else if (two_arguments)
1151 : 16616 : v1 = v0 = save_expr (v0);
1152 : :
1153 : 486422 : ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
1154 : :
1155 : 486422 : if (!c_dialect_cxx () && !wrap)
1156 : 468937 : ret = c_wrap_maybe_const (ret, true);
1157 : :
1158 : : return ret;
1159 : : }
1160 : :
1161 : : /* Build a VEC_PERM_EXPR if V0, V1 are not error_mark_nodes
1162 : : and have vector types, V0 has the same element type as V1, and the
1163 : : number of elements the result is that of MASK. */
1164 : : tree
1165 : 1116679 : c_build_shufflevector (location_t loc, tree v0, tree v1,
1166 : : const vec<tree> &mask, bool complain)
1167 : : {
1168 : 1116679 : tree ret;
1169 : 1116679 : bool wrap = true;
1170 : 1116679 : bool maybe_const = false;
1171 : :
1172 : 1116679 : if (v0 == error_mark_node || v1 == error_mark_node)
1173 : : return error_mark_node;
1174 : :
1175 : 1116679 : if (!gnu_vector_type_p (TREE_TYPE (v0))
1176 : 1116679 : || !gnu_vector_type_p (TREE_TYPE (v1)))
1177 : : {
1178 : 4 : if (complain)
1179 : 4 : error_at (loc, "%<__builtin_shufflevector%> arguments must be vectors");
1180 : 4 : return error_mark_node;
1181 : : }
1182 : :
1183 : : /* ??? In principle one could select a constant part of a variable size
1184 : : vector but things get a bit awkward with trying to support this here. */
1185 : 1116675 : unsigned HOST_WIDE_INT v0n, v1n;
1186 : 1116675 : if (!TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)).is_constant (&v0n)
1187 : 1116675 : || !TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)).is_constant (&v1n))
1188 : : {
1189 : : if (complain)
1190 : : error_at (loc, "%<__builtin_shufflevector%> arguments must be constant"
1191 : : " size vectors");
1192 : : return error_mark_node;
1193 : : }
1194 : :
1195 : 1116675 : if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (v0)))
1196 : 1116675 : != TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (v1))))
1197 : : {
1198 : 4 : if (complain)
1199 : 4 : error_at (loc, "%<__builtin_shufflevector%> argument vectors must "
1200 : : "have the same element type");
1201 : 4 : return error_mark_node;
1202 : : }
1203 : :
1204 : 1116671 : if (!pow2p_hwi (mask.length ()))
1205 : : {
1206 : 4 : if (complain)
1207 : 4 : error_at (loc, "%<__builtin_shufflevector%> must specify a result "
1208 : : "with a power of two number of elements");
1209 : 4 : return error_mark_node;
1210 : : }
1211 : :
1212 : 1116667 : if (!c_dialect_cxx ())
1213 : : {
1214 : : /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
1215 : 1075523 : v0 = c_fully_fold (v0, false, &maybe_const);
1216 : 1075523 : wrap &= maybe_const;
1217 : :
1218 : 1075523 : v1 = c_fully_fold (v1, false, &maybe_const);
1219 : 1075523 : wrap &= maybe_const;
1220 : : }
1221 : :
1222 : 2233379 : unsigned HOST_WIDE_INT maskl = MAX (mask.length (), MAX (v0n, v1n));
1223 : 1116667 : unsigned HOST_WIDE_INT pad = (v0n < maskl ? maskl - v0n : 0);
1224 : 1116667 : vec_perm_builder sel (maskl, maskl, 1);
1225 : 1116667 : unsigned i;
1226 : 30308126 : for (i = 0; i < mask.length (); ++i)
1227 : : {
1228 : 14037411 : tree idx = mask[i];
1229 : 14037411 : if (!tree_fits_shwi_p (idx))
1230 : : {
1231 : 4 : if (complain)
1232 : 4 : error_at (loc, "invalid element index %qE to "
1233 : : "%<__builtin_shufflevector%>", idx);
1234 : 1116667 : return error_mark_node;
1235 : : }
1236 : 14037407 : HOST_WIDE_INT iidx = tree_to_shwi (idx);
1237 : 14037407 : if (iidx < -1
1238 : 14037403 : || (iidx != -1
1239 : 14037323 : && (unsigned HOST_WIDE_INT) iidx >= v0n + v1n))
1240 : : {
1241 : 11 : if (complain)
1242 : 11 : error_at (loc, "invalid element index %qE to "
1243 : : "%<__builtin_shufflevector%>", idx);
1244 : 11 : return error_mark_node;
1245 : : }
1246 : : /* ??? Our VEC_PERM_EXPR does not allow for -1 yet. */
1247 : 14037396 : if (iidx == -1)
1248 : 80 : iidx = i;
1249 : : /* ??? Our VEC_PERM_EXPR does not allow different sized inputs,
1250 : : so pad out a smaller v0. */
1251 : 14037316 : else if ((unsigned HOST_WIDE_INT) iidx >= v0n)
1252 : 1159 : iidx += pad;
1253 : 14037396 : sel.quick_push (iidx);
1254 : : }
1255 : : /* ??? VEC_PERM_EXPR does not support a result that is smaller than
1256 : : the inputs, so we have to pad id out. */
1257 : 1117812 : for (; i < maskl; ++i)
1258 : 1160 : sel.quick_push (i);
1259 : :
1260 : 1116652 : vec_perm_indices indices (sel, 2, maskl);
1261 : :
1262 : 1116652 : tree ret_type = build_vector_type (TREE_TYPE (TREE_TYPE (v0)), maskl);
1263 : 2233304 : tree mask_type = build_vector_type (build_nonstandard_integer_type
1264 : 1116652 : (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (ret_type))), 1),
1265 : 1116652 : maskl);
1266 : : /* Pad out arguments to the common vector size. */
1267 : 1116652 : if (v0n < maskl)
1268 : : {
1269 : 125 : constructor_elt elt = { NULL_TREE, build_zero_cst (TREE_TYPE (v0)) };
1270 : 125 : v0 = build_constructor_single (ret_type, NULL_TREE, v0);
1271 : 571 : for (i = 1; i < maskl / v0n; ++i)
1272 : 321 : vec_safe_push (CONSTRUCTOR_ELTS (v0), elt);
1273 : : }
1274 : 1116652 : if (v1n < maskl)
1275 : : {
1276 : 95 : constructor_elt elt = { NULL_TREE, build_zero_cst (TREE_TYPE (v1)) };
1277 : 95 : v1 = build_constructor_single (ret_type, NULL_TREE, v1);
1278 : 351 : for (i = 1; i < maskl / v1n; ++i)
1279 : 161 : vec_safe_push (CONSTRUCTOR_ELTS (v1), elt);
1280 : : }
1281 : 1116652 : ret = build3_loc (loc, VEC_PERM_EXPR, ret_type, v0, v1,
1282 : : vec_perm_indices_to_tree (mask_type, indices));
1283 : : /* Get the lowpart we are interested in. */
1284 : 2233304 : if (mask.length () < maskl)
1285 : : {
1286 : 163 : tree lpartt = build_vector_type (TREE_TYPE (ret_type), mask.length ());
1287 : 163 : ret = build3_loc (loc, BIT_FIELD_REF,
1288 : 163 : lpartt, ret, TYPE_SIZE (lpartt), bitsize_zero_node);
1289 : : /* Wrap the lowpart operation in a TARGET_EXPR so it gets a separate
1290 : : temporary during gimplification. See PR101530 for cases where
1291 : : we'd otherwise end up with non-toplevel BIT_FIELD_REFs. */
1292 : 163 : tree tem = create_tmp_var_raw (lpartt);
1293 : 163 : DECL_CONTEXT (tem) = current_function_decl;
1294 : 163 : ret = build4 (TARGET_EXPR, lpartt, tem, ret, NULL_TREE, NULL_TREE);
1295 : 163 : TREE_SIDE_EFFECTS (ret) = 1;
1296 : : }
1297 : :
1298 : 1116652 : if (!c_dialect_cxx () && !wrap)
1299 : 1075520 : ret = c_wrap_maybe_const (ret, true);
1300 : :
1301 : 1116652 : return ret;
1302 : 2233319 : }
1303 : :
1304 : : /* Build a VEC_CONVERT ifn for __builtin_convertvector builtin. */
1305 : :
1306 : : tree
1307 : 490 : c_build_vec_convert (location_t loc1, tree expr, location_t loc2, tree type,
1308 : : bool complain)
1309 : : {
1310 : 490 : if (error_operand_p (type))
1311 : 0 : return error_mark_node;
1312 : 490 : if (error_operand_p (expr))
1313 : 0 : return error_mark_node;
1314 : :
1315 : 490 : if (!gnu_vector_type_p (TREE_TYPE (expr))
1316 : 490 : || (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (expr))
1317 : 166 : && !VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (expr))
1318 : 166 : && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (expr))))
1319 : : {
1320 : 4 : if (complain)
1321 : 4 : error_at (loc1, "%<__builtin_convertvector%> first argument must "
1322 : : "be an integer or floating vector");
1323 : 4 : return error_mark_node;
1324 : : }
1325 : :
1326 : 486 : if (!gnu_vector_type_p (type)
1327 : 486 : || (!VECTOR_INTEGER_TYPE_P (type) && !VECTOR_FLOAT_TYPE_P (type)
1328 : 0 : && !VECTOR_BOOLEAN_TYPE_P (type)))
1329 : : {
1330 : 4 : if (complain)
1331 : 4 : error_at (loc2, "%<__builtin_convertvector%> second argument must "
1332 : : "be an integer or floating vector type");
1333 : 4 : return error_mark_node;
1334 : : }
1335 : :
1336 : 482 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr)),
1337 : 964 : TYPE_VECTOR_SUBPARTS (type)))
1338 : : {
1339 : 8 : if (complain)
1340 : 8 : error_at (loc1, "%<__builtin_convertvector%> number of elements "
1341 : : "of the first argument vector and the second argument "
1342 : : "vector type should be the same");
1343 : 8 : return error_mark_node;
1344 : : }
1345 : :
1346 : 474 : if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (expr)))
1347 : 474 : == TYPE_MAIN_VARIANT (TREE_TYPE (type)))
1348 : 474 : || (VECTOR_INTEGER_TYPE_P (TREE_TYPE (expr))
1349 : 303 : && VECTOR_INTEGER_TYPE_P (type)
1350 : 155 : && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (expr)))
1351 : 155 : == TYPE_PRECISION (TREE_TYPE (type)))))
1352 : 29 : return build1_loc (loc1, VIEW_CONVERT_EXPR, type, expr);
1353 : :
1354 : 445 : bool wrap = true;
1355 : 445 : bool maybe_const = false;
1356 : 445 : tree ret;
1357 : 445 : if (!c_dialect_cxx ())
1358 : : {
1359 : : /* Avoid C_MAYBE_CONST_EXPRs inside of VEC_CONVERT argument. */
1360 : 310 : expr = c_fully_fold (expr, false, &maybe_const);
1361 : 310 : wrap &= maybe_const;
1362 : : }
1363 : :
1364 : 445 : ret = build_call_expr_internal_loc (loc1, IFN_VEC_CONVERT, type, 1, expr);
1365 : :
1366 : 445 : if (!wrap)
1367 : 310 : ret = c_wrap_maybe_const (ret, true);
1368 : :
1369 : : return ret;
1370 : : }
1371 : :
1372 : : /* Like tree.cc:get_narrower, but retain conversion from C++0x scoped enum
1373 : : to integral type. */
1374 : :
1375 : : tree
1376 : 68202017 : c_common_get_narrower (tree op, int *unsignedp_ptr)
1377 : : {
1378 : 68202017 : op = get_narrower (op, unsignedp_ptr);
1379 : :
1380 : 68202017 : if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
1381 : 68202017 : && ENUM_IS_SCOPED (TREE_TYPE (op)))
1382 : : {
1383 : : /* C++0x scoped enumerations don't implicitly convert to integral
1384 : : type; if we stripped an explicit conversion to a larger type we
1385 : : need to replace it so common_type will still work. */
1386 : 2744627 : tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
1387 : 2744627 : TYPE_UNSIGNED (TREE_TYPE (op)));
1388 : 2744627 : op = fold_convert (type, op);
1389 : : }
1390 : 68202017 : return op;
1391 : : }
1392 : :
1393 : : /* This is a helper function of build_binary_op.
1394 : :
1395 : : For certain operations if both args were extended from the same
1396 : : smaller type, do the arithmetic in that type and then extend.
1397 : :
1398 : : BITWISE indicates a bitwise operation.
1399 : : For them, this optimization is safe only if
1400 : : both args are zero-extended or both are sign-extended.
1401 : : Otherwise, we might change the result.
1402 : : Eg, (short)-1 | (unsigned short)-1 is (int)-1
1403 : : but calculated in (unsigned short) it would be (unsigned short)-1.
1404 : : */
1405 : : tree
1406 : 5457398 : shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
1407 : : {
1408 : 5457398 : int unsigned0, unsigned1;
1409 : 5457398 : tree arg0, arg1;
1410 : 5457398 : int uns;
1411 : 5457398 : tree type;
1412 : :
1413 : : /* Do not shorten vector operations. */
1414 : 5457398 : if (VECTOR_TYPE_P (result_type))
1415 : : return result_type;
1416 : :
1417 : : /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1418 : : excessive narrowing when we call get_narrower below. For
1419 : : example, suppose that OP0 is of unsigned int extended
1420 : : from signed char and that RESULT_TYPE is long long int.
1421 : : If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1422 : : like
1423 : :
1424 : : (long long int) (unsigned int) signed_char
1425 : :
1426 : : which get_narrower would narrow down to
1427 : :
1428 : : (unsigned int) signed char
1429 : :
1430 : : If we do not cast OP0 first, get_narrower would return
1431 : : signed_char, which is inconsistent with the case of the
1432 : : explicit cast. */
1433 : 5436525 : op0 = convert (result_type, op0);
1434 : 5436525 : op1 = convert (result_type, op1);
1435 : :
1436 : 5436525 : arg0 = c_common_get_narrower (op0, &unsigned0);
1437 : 5436525 : arg1 = c_common_get_narrower (op1, &unsigned1);
1438 : :
1439 : : /* UNS is 1 if the operation to be done is an unsigned one. */
1440 : 5436525 : uns = TYPE_UNSIGNED (result_type);
1441 : :
1442 : : /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1443 : : but it *requires* conversion to FINAL_TYPE. */
1444 : :
1445 : 5436525 : if ((TYPE_PRECISION (TREE_TYPE (op0))
1446 : 5436525 : == TYPE_PRECISION (TREE_TYPE (arg0)))
1447 : 5436525 : && TREE_TYPE (op0) != result_type)
1448 : 2663 : unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1449 : 5436525 : if ((TYPE_PRECISION (TREE_TYPE (op1))
1450 : 5436525 : == TYPE_PRECISION (TREE_TYPE (arg1)))
1451 : 5436525 : && TREE_TYPE (op1) != result_type)
1452 : 269559 : unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1453 : :
1454 : : /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
1455 : :
1456 : : /* For bitwise operations, signedness of nominal type
1457 : : does not matter. Consider only how operands were extended. */
1458 : 5436525 : if (bitwise)
1459 : 2607382 : uns = unsigned0;
1460 : :
1461 : : /* Note that in all three cases below we refrain from optimizing
1462 : : an unsigned operation on sign-extended args.
1463 : : That would not be valid. */
1464 : :
1465 : : /* Both args variable: if both extended in same way
1466 : : from same width, do it in that width.
1467 : : Do it unsigned if args were zero-extended. */
1468 : 5436525 : if ((TYPE_PRECISION (TREE_TYPE (arg0))
1469 : 5436525 : < TYPE_PRECISION (result_type))
1470 : 284693 : && (TYPE_PRECISION (TREE_TYPE (arg1))
1471 : 284693 : == TYPE_PRECISION (TREE_TYPE (arg0)))
1472 : 196851 : && unsigned0 == unsigned1
1473 : 5633182 : && (unsigned0 || !uns))
1474 : : {
1475 : 196639 : tree ctype = common_type (TREE_TYPE (arg0), TREE_TYPE (arg1));
1476 : 196639 : if (ctype != error_mark_node)
1477 : 196639 : return c_common_signed_or_unsigned_type (unsigned0, ctype);
1478 : : }
1479 : :
1480 : 5239886 : else if (TREE_CODE (arg0) == INTEGER_CST
1481 : 1877770 : && (unsigned1 || !uns)
1482 : 1669587 : && (TYPE_PRECISION (TREE_TYPE (arg1))
1483 : 1669587 : < TYPE_PRECISION (result_type))
1484 : 299 : && (type
1485 : 299 : = c_common_signed_or_unsigned_type (unsigned1,
1486 : 299 : TREE_TYPE (arg1)))
1487 : 299 : && !POINTER_TYPE_P (type)
1488 : 5240185 : && int_fits_type_p (arg0, type))
1489 : : return type;
1490 : :
1491 : 5239639 : else if (TREE_CODE (arg1) == INTEGER_CST
1492 : 3354535 : && (unsigned0 || !uns)
1493 : 2598117 : && (TYPE_PRECISION (TREE_TYPE (arg0))
1494 : 2598117 : < TYPE_PRECISION (result_type))
1495 : 63060 : && (type
1496 : 63060 : = c_common_signed_or_unsigned_type (unsigned0,
1497 : 63060 : TREE_TYPE (arg0)))
1498 : 63060 : && !POINTER_TYPE_P (type)
1499 : 5302699 : && int_fits_type_p (arg1, type))
1500 : : return type;
1501 : :
1502 : : return result_type;
1503 : : }
1504 : :
1505 : : /* Returns true iff any integer value of type FROM_TYPE can be represented as
1506 : : real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
1507 : :
1508 : : static bool
1509 : 101 : int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
1510 : : {
1511 : 101 : tree type_low_bound = TYPE_MIN_VALUE (from_type);
1512 : 101 : tree type_high_bound = TYPE_MAX_VALUE (from_type);
1513 : 101 : REAL_VALUE_TYPE real_low_bound =
1514 : 101 : real_value_from_int_cst (0, type_low_bound);
1515 : 101 : REAL_VALUE_TYPE real_high_bound =
1516 : 101 : real_value_from_int_cst (0, type_high_bound);
1517 : :
1518 : 101 : return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
1519 : 101 : && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
1520 : : }
1521 : :
1522 : : /* Checks if expression EXPR of complex/real/integer type cannot be converted
1523 : : to the complex/real/integer type TYPE. Function returns non-zero when:
1524 : : * EXPR is a constant which cannot be exactly converted to TYPE.
1525 : : * EXPR is not a constant and size of EXPR's type > than size of TYPE,
1526 : : for EXPR type and TYPE being both integers or both real, or both
1527 : : complex.
1528 : : * EXPR is not a constant of complex type and TYPE is a real or
1529 : : an integer.
1530 : : * EXPR is not a constant of real type and TYPE is an integer.
1531 : : * EXPR is not a constant of integer type which cannot be
1532 : : exactly converted to real type.
1533 : :
1534 : : Function allows conversions between types of different signedness if
1535 : : CHECK_SIGN is false and can return SAFE_CONVERSION (zero) in that
1536 : : case. Function can return UNSAFE_SIGN if CHECK_SIGN is true.
1537 : :
1538 : : RESULT, when non-null is the result of the conversion. When constant
1539 : : it is included in the text of diagnostics.
1540 : :
1541 : : Function allows conversions from complex constants to non-complex types,
1542 : : provided that imaginary part is zero and real part can be safely converted
1543 : : to TYPE. */
1544 : :
1545 : : enum conversion_safety
1546 : 9427 : unsafe_conversion_p (tree type, tree expr, tree result, bool check_sign)
1547 : : {
1548 : 9427 : enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
1549 : 9427 : tree expr_type = TREE_TYPE (expr);
1550 : :
1551 : 9427 : expr = fold_for_warn (expr);
1552 : :
1553 : 9427 : if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
1554 : : {
1555 : : /* If type is complex, we are interested in compatibility with
1556 : : underlying type. */
1557 : 5434 : if (TREE_CODE (type) == COMPLEX_TYPE)
1558 : 114 : type = TREE_TYPE (type);
1559 : :
1560 : : /* Warn for real constant that is not an exact integer converted
1561 : : to integer type. */
1562 : 5434 : if (SCALAR_FLOAT_TYPE_P (expr_type)
1563 : 658 : && (TREE_CODE (type) == INTEGER_TYPE
1564 : 658 : || TREE_CODE (type) == BITINT_TYPE))
1565 : : {
1566 : 283 : if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
1567 : 275 : give_warning = UNSAFE_REAL;
1568 : : }
1569 : : /* Warn for an integer constant that does not fit into integer type. */
1570 : 5151 : else if ((TREE_CODE (expr_type) == INTEGER_TYPE
1571 : 5151 : || TREE_CODE (expr_type) == BITINT_TYPE)
1572 : 4766 : && (TREE_CODE (type) == INTEGER_TYPE
1573 : 4766 : || TREE_CODE (type) == BITINT_TYPE)
1574 : 4042 : && !int_fits_type_p (expr, type))
1575 : : {
1576 : 995 : if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
1577 : 961 : && tree_int_cst_sgn (expr) < 0)
1578 : : {
1579 : 264 : if (check_sign)
1580 : 1834 : give_warning = UNSAFE_SIGN;
1581 : : }
1582 : 432 : else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
1583 : : {
1584 : 335 : if (check_sign)
1585 : 1834 : give_warning = UNSAFE_SIGN;
1586 : : }
1587 : : else
1588 : : give_warning = UNSAFE_OTHER;
1589 : : }
1590 : 4455 : else if (SCALAR_FLOAT_TYPE_P (type))
1591 : : {
1592 : : /* Warn for an integer constant that does not fit into real type. */
1593 : 1009 : if (TREE_CODE (expr_type) == INTEGER_TYPE
1594 : : || TREE_CODE (expr_type) == BITINT_TYPE)
1595 : : {
1596 : 634 : REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
1597 : 634 : if (!exact_real_truncate (TYPE_MODE (type), &a))
1598 : 53 : give_warning = UNSAFE_REAL;
1599 : : }
1600 : : /* Warn for a real constant that does not fit into a smaller
1601 : : real type. */
1602 : 375 : else if (SCALAR_FLOAT_TYPE_P (expr_type)
1603 : 375 : && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1604 : : {
1605 : 227 : REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
1606 : 227 : if (!exact_real_truncate (TYPE_MODE (type), &a))
1607 : 65 : give_warning = UNSAFE_REAL;
1608 : : }
1609 : : }
1610 : : }
1611 : :
1612 : 3993 : else if (TREE_CODE (expr) == COMPLEX_CST)
1613 : : {
1614 : 69 : tree imag_part = TREE_IMAGPART (expr);
1615 : : /* Conversion from complex constant with zero imaginary part,
1616 : : perform check for conversion of real part. */
1617 : 69 : if ((TREE_CODE (imag_part) == REAL_CST
1618 : 29 : && real_zerop (imag_part))
1619 : 80 : || (TREE_CODE (imag_part) == INTEGER_CST
1620 : 40 : && integer_zerop (imag_part)))
1621 : : /* Note: in this branch we use recursive call to unsafe_conversion_p
1622 : : with different type of EXPR, but it is still safe, because when EXPR
1623 : : is a constant, it's type is not used in text of generated warnings
1624 : : (otherwise they could sound misleading). */
1625 : 30 : return unsafe_conversion_p (type, TREE_REALPART (expr), result,
1626 : 30 : check_sign);
1627 : : /* Conversion from complex constant with non-zero imaginary part. */
1628 : : else
1629 : : {
1630 : : /* Conversion to complex type.
1631 : : Perform checks for both real and imaginary parts. */
1632 : 39 : if (TREE_CODE (type) == COMPLEX_TYPE)
1633 : : {
1634 : 33 : enum conversion_safety re_safety =
1635 : 33 : unsafe_conversion_p (type, TREE_REALPART (expr),
1636 : : result, check_sign);
1637 : 33 : enum conversion_safety im_safety =
1638 : 33 : unsafe_conversion_p (type, imag_part, result, check_sign);
1639 : :
1640 : : /* Merge the results into appropriate single warning. */
1641 : :
1642 : : /* Note: this case includes SAFE_CONVERSION, i.e. success. */
1643 : 33 : if (re_safety == im_safety)
1644 : : give_warning = re_safety;
1645 : 9 : else if (!re_safety && im_safety)
1646 : : give_warning = im_safety;
1647 : 4 : else if (re_safety && !im_safety)
1648 : : give_warning = re_safety;
1649 : : else
1650 : 670 : give_warning = UNSAFE_OTHER;
1651 : : }
1652 : : /* Warn about conversion from complex to real or integer type. */
1653 : : else
1654 : : give_warning = UNSAFE_IMAGINARY;
1655 : : }
1656 : : }
1657 : :
1658 : : /* Checks for remaining case: EXPR is not constant. */
1659 : : else
1660 : : {
1661 : : /* Warn for real types converted to integer types. */
1662 : 3924 : if (SCALAR_FLOAT_TYPE_P (expr_type)
1663 : 213 : && (TREE_CODE (type) == INTEGER_TYPE
1664 : 213 : || TREE_CODE (type) == BITINT_TYPE))
1665 : : give_warning = UNSAFE_REAL;
1666 : :
1667 : 3895 : else if ((TREE_CODE (expr_type) == INTEGER_TYPE
1668 : 3895 : || TREE_CODE (expr_type) == BITINT_TYPE)
1669 : 3500 : && (TREE_CODE (type) == INTEGER_TYPE
1670 : 3500 : || TREE_CODE (type) == BITINT_TYPE))
1671 : : {
1672 : : /* Don't warn about unsigned char y = 0xff, x = (int) y; */
1673 : 3279 : expr = get_unwidened (expr, 0);
1674 : 3279 : expr_type = TREE_TYPE (expr);
1675 : :
1676 : : /* Don't warn for short y; short x = ((int)y & 0xff); */
1677 : 3279 : if (TREE_CODE (expr) == BIT_AND_EXPR
1678 : 3265 : || TREE_CODE (expr) == BIT_IOR_EXPR
1679 : 3224 : || TREE_CODE (expr) == BIT_XOR_EXPR)
1680 : : {
1681 : : /* If both args were extended from a shortest type,
1682 : : use that type if that is safe. */
1683 : 59 : expr_type = shorten_binary_op (expr_type,
1684 : 59 : TREE_OPERAND (expr, 0),
1685 : 59 : TREE_OPERAND (expr, 1),
1686 : : /* bitwise */1);
1687 : :
1688 : 59 : if (TREE_CODE (expr) == BIT_AND_EXPR)
1689 : : {
1690 : 14 : tree op0 = TREE_OPERAND (expr, 0);
1691 : 14 : tree op1 = TREE_OPERAND (expr, 1);
1692 : 14 : bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1693 : 14 : bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1694 : :
1695 : : /* If one of the operands is a non-negative constant
1696 : : that fits in the target type, then the type of the
1697 : : other operand does not matter. */
1698 : 14 : if ((TREE_CODE (op0) == INTEGER_CST
1699 : 0 : && int_fits_type_p (op0, c_common_signed_type (type))
1700 : 0 : && int_fits_type_p (op0, c_common_unsigned_type (type)))
1701 : 14 : || (TREE_CODE (op1) == INTEGER_CST
1702 : 1 : && int_fits_type_p (op1, c_common_signed_type (type))
1703 : 1 : && int_fits_type_p (op1,
1704 : 1 : c_common_unsigned_type (type))))
1705 : 1 : return SAFE_CONVERSION;
1706 : : /* If constant is unsigned and fits in the target
1707 : : type, then the result will also fit. */
1708 : 13 : else if ((TREE_CODE (op0) == INTEGER_CST
1709 : 0 : && unsigned0
1710 : 0 : && int_fits_type_p (op0, type))
1711 : 13 : || (TREE_CODE (op1) == INTEGER_CST
1712 : 0 : && unsigned1
1713 : 0 : && int_fits_type_p (op1, type)))
1714 : : return SAFE_CONVERSION;
1715 : : }
1716 : : }
1717 : : /* Warn for integer types converted to smaller integer types. */
1718 : 3278 : if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1719 : : give_warning = UNSAFE_OTHER;
1720 : :
1721 : : /* When they are the same width but different signedness,
1722 : : then the value may change. */
1723 : 2742 : else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
1724 : 2213 : && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
1725 : : /* Even when converted to a bigger type, if the type is
1726 : : unsigned but expr is signed, then negative values
1727 : : will be changed. */
1728 : 1481 : || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
1729 : 4054 : && check_sign)
1730 : : give_warning = UNSAFE_SIGN;
1731 : : }
1732 : :
1733 : : /* Warn for integer types converted to real types if and only if
1734 : : all the range of values of the integer type cannot be
1735 : : represented by the real type. */
1736 : 616 : else if ((TREE_CODE (expr_type) == INTEGER_TYPE
1737 : : || TREE_CODE (expr_type) == BITINT_TYPE)
1738 : 221 : && SCALAR_FLOAT_TYPE_P (type))
1739 : : {
1740 : : /* Don't warn about char y = 0xff; float x = (int) y; */
1741 : 101 : expr = get_unwidened (expr, 0);
1742 : 101 : expr_type = TREE_TYPE (expr);
1743 : :
1744 : 101 : if (!int_safely_convertible_to_real_p (expr_type, type))
1745 : 670 : give_warning = UNSAFE_OTHER;
1746 : : }
1747 : :
1748 : : /* Warn for real types converted to smaller real types. */
1749 : 515 : else if (SCALAR_FLOAT_TYPE_P (expr_type)
1750 : 184 : && SCALAR_FLOAT_TYPE_P (type)
1751 : 699 : && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1752 : : give_warning = UNSAFE_REAL;
1753 : :
1754 : : /* Check conversion between two complex types. */
1755 : 463 : else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1756 : 32 : && TREE_CODE (type) == COMPLEX_TYPE)
1757 : : {
1758 : : /* Extract underlying types (i.e., type of real and imaginary
1759 : : parts) of expr_type and type. */
1760 : 10 : tree from_type = TREE_TYPE (expr_type);
1761 : 10 : tree to_type = TREE_TYPE (type);
1762 : :
1763 : : /* Warn for real types converted to integer types. */
1764 : 10 : if (SCALAR_FLOAT_TYPE_P (from_type)
1765 : 8 : && TREE_CODE (to_type) == INTEGER_TYPE)
1766 : : give_warning = UNSAFE_REAL;
1767 : :
1768 : : /* Warn for real types converted to smaller real types. */
1769 : 6 : else if (SCALAR_FLOAT_TYPE_P (from_type)
1770 : 4 : && SCALAR_FLOAT_TYPE_P (to_type)
1771 : 10 : && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1772 : : give_warning = UNSAFE_REAL;
1773 : :
1774 : : /* Check conversion for complex integer types. Here implementation
1775 : : is simpler than for real-domain integers because it does not
1776 : : involve sophisticated cases, such as bitmasks, casts, etc. */
1777 : 4 : else if (TREE_CODE (from_type) == INTEGER_TYPE
1778 : 2 : && TREE_CODE (to_type) == INTEGER_TYPE)
1779 : : {
1780 : : /* Warn for integer types converted to smaller integer types. */
1781 : 2 : if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1782 : : give_warning = UNSAFE_OTHER;
1783 : :
1784 : : /* Check for different signedness, see case for real-domain
1785 : : integers (above) for a more detailed comment. */
1786 : 0 : else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
1787 : 0 : && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
1788 : 0 : || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
1789 : 0 : && check_sign)
1790 : : give_warning = UNSAFE_SIGN;
1791 : : }
1792 : 2 : else if (TREE_CODE (from_type) == INTEGER_TYPE
1793 : 0 : && SCALAR_FLOAT_TYPE_P (to_type)
1794 : 2 : && !int_safely_convertible_to_real_p (from_type, to_type))
1795 : : give_warning = UNSAFE_OTHER;
1796 : : }
1797 : :
1798 : : /* Warn for complex types converted to real or integer types. */
1799 : 453 : else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1800 : 22 : && TREE_CODE (type) != COMPLEX_TYPE)
1801 : 28 : give_warning = UNSAFE_IMAGINARY;
1802 : : }
1803 : :
1804 : : return give_warning;
1805 : : }
1806 : :
1807 : :
1808 : : /* Convert EXPR to TYPE, warning about conversion problems with constants.
1809 : : Invoke this function on every expression that is converted implicitly,
1810 : : i.e. because of language rules and not because of an explicit cast.
1811 : : INIT_CONST is true if the conversion is for arithmetic types for a static
1812 : : initializer and folding must apply accordingly (discarding floating-point
1813 : : exceptions and assuming the default rounding mode is in effect). */
1814 : :
1815 : : tree
1816 : 12645187 : convert_and_check (location_t loc, tree type, tree expr, bool init_const)
1817 : : {
1818 : 12645187 : tree result;
1819 : 12645187 : tree expr_for_warning;
1820 : :
1821 : : /* Convert from a value with possible excess precision rather than
1822 : : via the semantic type, but do not warn about values not fitting
1823 : : exactly in the semantic type. */
1824 : 12645187 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1825 : : {
1826 : 5967 : tree orig_type = TREE_TYPE (expr);
1827 : 5967 : expr = TREE_OPERAND (expr, 0);
1828 : 5967 : expr_for_warning = (init_const
1829 : 5967 : ? convert_init (orig_type, expr)
1830 : 5606 : : convert (orig_type, expr));
1831 : 5967 : if (orig_type == type)
1832 : : return expr_for_warning;
1833 : : }
1834 : : else
1835 : : expr_for_warning = expr;
1836 : :
1837 : 12639713 : if (TREE_TYPE (expr) == type)
1838 : : return expr;
1839 : :
1840 : 11145015 : result = init_const ? convert_init (type, expr) : convert (type, expr);
1841 : :
1842 : 11145015 : if (c_inhibit_evaluation_warnings == 0
1843 : 10818534 : && !TREE_OVERFLOW_P (expr)
1844 : 10818508 : && result != error_mark_node
1845 : 21963369 : && !c_hardbool_type_attr (type))
1846 : 10811011 : warnings_for_convert_and_check (loc, type, expr_for_warning, result);
1847 : :
1848 : : return result;
1849 : : }
1850 : :
1851 : : /* A node in a list that describes references to variables (EXPR), which are
1852 : : either read accesses if WRITER is zero, or write accesses, in which case
1853 : : WRITER is the parent of EXPR. */
1854 : : struct tlist
1855 : : {
1856 : : struct tlist *next;
1857 : : tree expr, writer;
1858 : : };
1859 : :
1860 : : /* Used to implement a cache the results of a call to verify_tree. We only
1861 : : use this for SAVE_EXPRs. */
1862 : : struct tlist_cache
1863 : : {
1864 : : struct tlist_cache *next;
1865 : : struct tlist *cache_before_sp;
1866 : : struct tlist *cache_after_sp;
1867 : : tree expr;
1868 : : };
1869 : :
1870 : : /* Obstack to use when allocating tlist structures, and corresponding
1871 : : firstobj. */
1872 : : static struct obstack tlist_obstack;
1873 : : static char *tlist_firstobj = 0;
1874 : :
1875 : : /* Keep track of the identifiers we've warned about, so we can avoid duplicate
1876 : : warnings. */
1877 : : static struct tlist *warned_ids;
1878 : : /* SAVE_EXPRs need special treatment. We process them only once and then
1879 : : cache the results. */
1880 : : static struct tlist_cache *save_expr_cache;
1881 : :
1882 : : static void add_tlist (struct tlist **, struct tlist *, tree, int);
1883 : : static void merge_tlist (struct tlist **, struct tlist *, int);
1884 : : static void verify_tree (tree, struct tlist **, struct tlist **, tree);
1885 : : static bool warning_candidate_p (tree);
1886 : : static bool candidate_equal_p (const_tree, const_tree);
1887 : : static void warn_for_collisions (struct tlist *);
1888 : : static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
1889 : : static struct tlist *new_tlist (struct tlist *, tree, tree);
1890 : :
1891 : : /* Create a new struct tlist and fill in its fields. */
1892 : : static struct tlist *
1893 : 34471138 : new_tlist (struct tlist *next, tree t, tree writer)
1894 : : {
1895 : 34471138 : struct tlist *l;
1896 : 34471138 : l = XOBNEW (&tlist_obstack, struct tlist);
1897 : 34471138 : l->next = next;
1898 : 34471138 : l->expr = t;
1899 : 34471138 : l->writer = writer;
1900 : 34471138 : return l;
1901 : : }
1902 : :
1903 : : /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1904 : : is nonnull, we ignore any node we find which has a writer equal to it. */
1905 : :
1906 : : static void
1907 : 60853825 : add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
1908 : : {
1909 : 132325232 : while (add)
1910 : : {
1911 : 71471407 : struct tlist *next = add->next;
1912 : 13146292 : if (!copy)
1913 : 1881071 : add->next = *to;
1914 : 13146292 : if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
1915 : 69451133 : *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1916 : : add = next;
1917 : : }
1918 : 11826232 : }
1919 : :
1920 : : /* Merge the nodes of ADD into TO. This merging process is done so that for
1921 : : each variable that already exists in TO, no new node is added; however if
1922 : : there is a write access recorded in ADD, and an occurrence on TO is only
1923 : : a read access, then the occurrence in TO will be modified to record the
1924 : : write. */
1925 : :
1926 : : static void
1927 : 42563120 : merge_tlist (struct tlist **to, struct tlist *add, int copy)
1928 : : {
1929 : 42563120 : struct tlist **end = to;
1930 : :
1931 : 79429416 : while (*end)
1932 : 36866296 : end = &(*end)->next;
1933 : :
1934 : 58461403 : while (add)
1935 : : {
1936 : 15898283 : int found = 0;
1937 : 15898283 : struct tlist *tmp2;
1938 : 15898283 : struct tlist *next = add->next;
1939 : :
1940 : 43345549 : for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1941 : 27447266 : if (candidate_equal_p (tmp2->expr, add->expr))
1942 : : {
1943 : 2727391 : found = 1;
1944 : 2727391 : if (!tmp2->writer)
1945 : 2720859 : tmp2->writer = add->writer;
1946 : : }
1947 : 15898283 : if (!found)
1948 : : {
1949 : 13184719 : *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
1950 : 13184719 : end = &(*end)->next;
1951 : 13184719 : *end = 0;
1952 : : }
1953 : : add = next;
1954 : : }
1955 : 42563120 : }
1956 : :
1957 : : /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1958 : : references in list LIST conflict with it, excluding reads if ONLY writers
1959 : : is nonzero. */
1960 : :
1961 : : static void
1962 : 5247856 : warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
1963 : : int only_writes)
1964 : : {
1965 : 5247856 : struct tlist *tmp;
1966 : :
1967 : : /* Avoid duplicate warnings. */
1968 : 5248019 : for (tmp = warned_ids; tmp; tmp = tmp->next)
1969 : 611 : if (candidate_equal_p (tmp->expr, written))
1970 : : return;
1971 : :
1972 : 18855111 : while (list)
1973 : : {
1974 : 13607703 : if (candidate_equal_p (list->expr, written)
1975 : 1545401 : && !candidate_equal_p (list->writer, writer)
1976 : 13748689 : && (!only_writes || list->writer))
1977 : : {
1978 : 562 : warned_ids = new_tlist (warned_ids, written, NULL_TREE);
1979 : 562 : warning_at (EXPR_LOC_OR_LOC (writer, input_location),
1980 : 562 : OPT_Wsequence_point, "operation on %qE may be undefined",
1981 : : list->expr);
1982 : : }
1983 : 13607703 : list = list->next;
1984 : : }
1985 : : }
1986 : :
1987 : : /* Given a list LIST of references to variables, find whether any of these
1988 : : can cause conflicts due to missing sequence points. */
1989 : :
1990 : : static void
1991 : 20042930 : warn_for_collisions (struct tlist *list)
1992 : : {
1993 : 20042930 : struct tlist *tmp;
1994 : :
1995 : 55220126 : for (tmp = list; tmp; tmp = tmp->next)
1996 : : {
1997 : 35177196 : if (tmp->writer)
1998 : 1338640 : warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1999 : : }
2000 : 20042930 : }
2001 : :
2002 : : /* Return nonzero if X is a tree that can be verified by the sequence point
2003 : : warnings. */
2004 : :
2005 : : static bool
2006 : 76821850 : warning_candidate_p (tree x)
2007 : : {
2008 : 76821850 : if (DECL_P (x) && DECL_ARTIFICIAL (x))
2009 : : return false;
2010 : :
2011 : 69130434 : if (TREE_CODE (x) == BLOCK)
2012 : : return false;
2013 : :
2014 : : /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.cc
2015 : : (lvalue_p) crash on TRY/CATCH. */
2016 : 69128269 : if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2017 : : return false;
2018 : :
2019 : 68218598 : if (!lvalue_p (x))
2020 : : return false;
2021 : :
2022 : : /* No point to track non-const calls, they will never satisfy
2023 : : operand_equal_p. */
2024 : 24550432 : if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2025 : : return false;
2026 : :
2027 : 24505760 : if (TREE_CODE (x) == STRING_CST)
2028 : : return false;
2029 : :
2030 : : return true;
2031 : : }
2032 : :
2033 : : /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2034 : : static bool
2035 : 46416842 : candidate_equal_p (const_tree x, const_tree y)
2036 : : {
2037 : 46416842 : return (x == y) || (x && y && operand_equal_p (x, y, 0));
2038 : : }
2039 : :
2040 : : /* Walk the tree X, and record accesses to variables. If X is written by the
2041 : : parent tree, WRITER is the parent.
2042 : : We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2043 : : expression or its only operand forces a sequence point, then everything up
2044 : : to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2045 : : in PNO_SP.
2046 : : Once we return, we will have emitted warnings if any subexpression before
2047 : : such a sequence point could be undefined. On a higher level, however, the
2048 : : sequence point may not be relevant, and we'll merge the two lists.
2049 : :
2050 : : Example: (b++, a) + b;
2051 : : The call that processes the COMPOUND_EXPR will store the increment of B
2052 : : in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2053 : : processes the PLUS_EXPR will need to merge the two lists so that
2054 : : eventually, all accesses end up on the same list (and we'll warn about the
2055 : : unordered subexpressions b++ and b.
2056 : :
2057 : : A note on merging. If we modify the former example so that our expression
2058 : : becomes
2059 : : (b++, b) + a
2060 : : care must be taken not simply to add all three expressions into the final
2061 : : PNO_SP list. The function merge_tlist takes care of that by merging the
2062 : : before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2063 : : way, so that no more than one access to B is recorded. */
2064 : :
2065 : : static void
2066 : 60323326 : verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2067 : : tree writer)
2068 : : {
2069 : 74126250 : struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2070 : 74126250 : enum tree_code code;
2071 : 74126250 : enum tree_code_class cl;
2072 : :
2073 : 74126250 : restart:
2074 : : /* X may be NULL if it is the operand of an empty statement expression
2075 : : ({ }). */
2076 : 74126250 : if (x == NULL)
2077 : : return;
2078 : :
2079 : 72912634 : code = TREE_CODE (x);
2080 : 72912634 : cl = TREE_CODE_CLASS (code);
2081 : :
2082 : 72912634 : if (warning_candidate_p (x))
2083 : 23264985 : *pno_sp = new_tlist (*pno_sp, x, writer);
2084 : :
2085 : 72912634 : switch (code)
2086 : : {
2087 : : case CONSTRUCTOR:
2088 : : case SIZEOF_EXPR:
2089 : : case PAREN_SIZEOF_EXPR:
2090 : : return;
2091 : :
2092 : 212279 : case COMPOUND_EXPR:
2093 : 212279 : case TRUTH_ANDIF_EXPR:
2094 : 212279 : case TRUTH_ORIF_EXPR:
2095 : 212279 : sequenced_binary:
2096 : 212279 : tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
2097 : 212279 : verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2098 : 212279 : warn_for_collisions (tmp_nosp);
2099 : 212279 : merge_tlist (pbefore_sp, tmp_before, 0);
2100 : 212279 : merge_tlist (pbefore_sp, tmp_nosp, 0);
2101 : 212279 : verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_list2, NULL_TREE);
2102 : 212279 : warn_for_collisions (tmp_list2);
2103 : 212279 : merge_tlist (pbefore_sp, tmp_list3, 0);
2104 : 212279 : merge_tlist (pno_sp, tmp_list2, 0);
2105 : 212279 : return;
2106 : :
2107 : 68493 : case COND_EXPR:
2108 : 68493 : tmp_before = tmp_list2 = 0;
2109 : 68493 : verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2110 : 68493 : warn_for_collisions (tmp_list2);
2111 : 68493 : merge_tlist (pbefore_sp, tmp_before, 0);
2112 : 68493 : merge_tlist (pbefore_sp, tmp_list2, 0);
2113 : :
2114 : 68493 : tmp_list3 = tmp_nosp = 0;
2115 : 68493 : verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
2116 : 68493 : warn_for_collisions (tmp_nosp);
2117 : 68493 : merge_tlist (pbefore_sp, tmp_list3, 0);
2118 : :
2119 : 68493 : tmp_list3 = tmp_list2 = 0;
2120 : 68493 : verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
2121 : 68493 : warn_for_collisions (tmp_list2);
2122 : 68493 : merge_tlist (pbefore_sp, tmp_list3, 0);
2123 : : /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
2124 : : two first, to avoid warning for (a ? b++ : b++). */
2125 : 68493 : merge_tlist (&tmp_nosp, tmp_list2, 0);
2126 : 68493 : add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2127 : : return;
2128 : :
2129 : 268837 : case PREDECREMENT_EXPR:
2130 : 268837 : case PREINCREMENT_EXPR:
2131 : 268837 : case POSTDECREMENT_EXPR:
2132 : 268837 : case POSTINCREMENT_EXPR:
2133 : 268837 : verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
2134 : 268837 : return;
2135 : :
2136 : 3909216 : case MODIFY_EXPR:
2137 : 3909216 : tmp_before = tmp_nosp = tmp_list3 = 0;
2138 : 3909216 : verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
2139 : 3909216 : verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
2140 : : /* Expressions inside the LHS are not ordered wrt. the sequence points
2141 : : in the RHS. Example:
2142 : : *a = (a++, 2)
2143 : : Despite the fact that the modification of "a" is in the before_sp
2144 : : list (tmp_before), it conflicts with the use of "a" in the LHS.
2145 : : We can handle this by adding the contents of tmp_list3
2146 : : to those of tmp_before, and redoing the collision warnings for that
2147 : : list. */
2148 : 3909216 : add_tlist (&tmp_before, tmp_list3, x, 1);
2149 : 3909216 : warn_for_collisions (tmp_before);
2150 : : /* Exclude the LHS itself here; we first have to merge it into the
2151 : : tmp_nosp list. This is done to avoid warning for "a = a"; if we
2152 : : didn't exclude the LHS, we'd get it twice, once as a read and once
2153 : : as a write. */
2154 : 3909216 : add_tlist (pno_sp, tmp_list3, x, 0);
2155 : 3909216 : warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
2156 : :
2157 : 3909216 : merge_tlist (pbefore_sp, tmp_before, 0);
2158 : 3909216 : if (warning_candidate_p (TREE_OPERAND (x, 0)))
2159 : 944000 : merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
2160 : 3909216 : add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
2161 : 3909216 : return;
2162 : :
2163 : 6293960 : case CALL_EXPR:
2164 : : /* We need to warn about conflicts among arguments and conflicts between
2165 : : args and the function address. Side effects of the function address,
2166 : : however, are not ordered by the sequence point of the call. */
2167 : 6293960 : {
2168 : 6293960 : call_expr_arg_iterator iter;
2169 : 6293960 : tree arg;
2170 : 6293960 : tmp_before = tmp_nosp = 0;
2171 : 6293960 : verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
2172 : 27910789 : FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
2173 : : {
2174 : 15322869 : tmp_list2 = tmp_list3 = 0;
2175 : 15322869 : verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
2176 : 15322869 : merge_tlist (&tmp_list3, tmp_list2, 0);
2177 : 30645738 : add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
2178 : : }
2179 : 6293960 : add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
2180 : 6293960 : warn_for_collisions (tmp_before);
2181 : 6293960 : add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
2182 : 6293960 : return;
2183 : : }
2184 : :
2185 : : case TREE_LIST:
2186 : : /* Scan all the list, e.g. indices of multi dimensional array. */
2187 : 6 : while (x)
2188 : : {
2189 : 3 : tmp_before = tmp_nosp = 0;
2190 : 3 : verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
2191 : 3 : merge_tlist (&tmp_nosp, tmp_before, 0);
2192 : 3 : add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2193 : 3 : x = TREE_CHAIN (x);
2194 : : }
2195 : : return;
2196 : :
2197 : 98584 : case SAVE_EXPR:
2198 : 98584 : {
2199 : 98584 : struct tlist_cache *t;
2200 : 102278 : for (t = save_expr_cache; t; t = t->next)
2201 : 53719 : if (candidate_equal_p (t->expr, x))
2202 : : break;
2203 : :
2204 : 98584 : if (!t)
2205 : : {
2206 : 48559 : t = XOBNEW (&tlist_obstack, struct tlist_cache);
2207 : 48559 : t->next = save_expr_cache;
2208 : 48559 : t->expr = x;
2209 : 48559 : save_expr_cache = t;
2210 : :
2211 : 48559 : tmp_before = tmp_nosp = 0;
2212 : 48559 : verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2213 : 48559 : warn_for_collisions (tmp_nosp);
2214 : :
2215 : 48559 : tmp_list3 = 0;
2216 : 48559 : merge_tlist (&tmp_list3, tmp_nosp, 0);
2217 : 48559 : t->cache_before_sp = tmp_before;
2218 : 48559 : t->cache_after_sp = tmp_list3;
2219 : : }
2220 : 98584 : merge_tlist (pbefore_sp, t->cache_before_sp, 1);
2221 : 98584 : add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
2222 : 98584 : return;
2223 : : }
2224 : :
2225 : 7167372 : case ADDR_EXPR:
2226 : 7167372 : x = TREE_OPERAND (x, 0);
2227 : 7167372 : if (DECL_P (x))
2228 : : return;
2229 : 510870 : writer = 0;
2230 : 510870 : goto restart;
2231 : :
2232 : 12049930 : case VIEW_CONVERT_EXPR:
2233 : 12049930 : if (location_wrapper_p (x))
2234 : : {
2235 : 3022417 : x = TREE_OPERAND (x, 0);
2236 : 3022417 : goto restart;
2237 : : }
2238 : 9027513 : goto do_default;
2239 : :
2240 : 627126 : case LSHIFT_EXPR:
2241 : 627126 : case RSHIFT_EXPR:
2242 : 627126 : case ARRAY_REF:
2243 : 627126 : if (cxx_dialect >= cxx17)
2244 : 50570 : goto sequenced_binary;
2245 : 576556 : goto do_default;
2246 : :
2247 : 1308088 : case COMPONENT_REF:
2248 : : /* Treat as unary, the other operands aren't evaluated. */
2249 : 1308088 : x = TREE_OPERAND (x, 0);
2250 : 1308088 : writer = 0;
2251 : 1308088 : goto restart;
2252 : :
2253 : 50123375 : default:
2254 : 50123375 : do_default:
2255 : : /* For other expressions, simply recurse on their operands.
2256 : : Manual tail recursion for unary expressions.
2257 : : Other non-expressions need not be processed. */
2258 : 50123375 : if (cl == tcc_unary)
2259 : : {
2260 : 8692712 : x = TREE_OPERAND (x, 0);
2261 : 8692712 : writer = 0;
2262 : 8692712 : goto restart;
2263 : : }
2264 : 41430663 : else if (IS_EXPR_CODE_CLASS (cl))
2265 : : {
2266 : 15079002 : int lp;
2267 : 15079002 : int max = TREE_OPERAND_LENGTH (x);
2268 : 51206312 : for (lp = 0; lp < max; lp++)
2269 : : {
2270 : 21048308 : tmp_before = tmp_nosp = 0;
2271 : 21048308 : verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
2272 : 21048308 : merge_tlist (&tmp_nosp, tmp_before, 0);
2273 : 42096616 : add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2274 : : }
2275 : : }
2276 : : return;
2277 : : }
2278 : : }
2279 : :
2280 : : static constexpr size_t verify_sequence_points_limit = 1024;
2281 : :
2282 : : /* Called from verify_sequence_points via walk_tree. */
2283 : :
2284 : : static tree
2285 : 90274078 : verify_tree_lim_r (tree *tp, int *walk_subtrees, void *data)
2286 : : {
2287 : 90274078 : if (++*((size_t *) data) > verify_sequence_points_limit)
2288 : 57 : return integer_zero_node;
2289 : :
2290 : 90274021 : if (TYPE_P (*tp))
2291 : 7093 : *walk_subtrees = 0;
2292 : :
2293 : : return NULL_TREE;
2294 : : }
2295 : :
2296 : : /* Try to warn for undefined behavior in EXPR due to missing sequence
2297 : : points. */
2298 : :
2299 : : void
2300 : 9161215 : verify_sequence_points (tree expr)
2301 : : {
2302 : 9161215 : tlist *before_sp = nullptr, *after_sp = nullptr;
2303 : :
2304 : : /* verify_tree is highly recursive, and merge_tlist is O(n^2),
2305 : : so we return early if the expression is too big. */
2306 : 9161215 : size_t n = 0;
2307 : 9161215 : if (walk_tree (&expr, verify_tree_lim_r, &n, nullptr))
2308 : 57 : return;
2309 : :
2310 : 9161158 : warned_ids = nullptr;
2311 : 9161158 : save_expr_cache = nullptr;
2312 : 9161158 : if (!tlist_firstobj)
2313 : : {
2314 : 9907 : gcc_obstack_init (&tlist_obstack);
2315 : 9907 : tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
2316 : : }
2317 : :
2318 : 9161158 : verify_tree (expr, &before_sp, &after_sp, NULL_TREE);
2319 : 9161158 : warn_for_collisions (after_sp);
2320 : 9161158 : obstack_free (&tlist_obstack, tlist_firstobj);
2321 : : }
2322 : :
2323 : : /* Validate the expression after `case' and apply default promotions. */
2324 : :
2325 : : static tree
2326 : 2835605 : check_case_value (location_t loc, tree value)
2327 : : {
2328 : 2835605 : if (value == NULL_TREE)
2329 : : return value;
2330 : :
2331 : 4687997 : if (INTEGRAL_TYPE_P (TREE_TYPE (value))
2332 : 4687894 : && TREE_CODE (value) == INTEGER_CST)
2333 : : /* Promote char or short to int. */
2334 : 2835454 : value = perform_integral_promotions (value);
2335 : 151 : else if (value != error_mark_node)
2336 : : {
2337 : 53 : error_at (loc, "case label does not reduce to an integer constant");
2338 : 53 : value = error_mark_node;
2339 : : }
2340 : :
2341 : 2835605 : constant_expression_warning (value);
2342 : :
2343 : 2835605 : return value;
2344 : : }
2345 : :
2346 : : /* Return an integer type with BITS bits of precision,
2347 : : that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2348 : :
2349 : : tree
2350 : 29356565 : c_common_type_for_size (unsigned int bits, int unsignedp)
2351 : : {
2352 : 29356565 : int i;
2353 : :
2354 : 29356565 : if (bits == TYPE_PRECISION (integer_type_node))
2355 : 19305398 : return unsignedp ? unsigned_type_node : integer_type_node;
2356 : :
2357 : 10051167 : if (bits == TYPE_PRECISION (signed_char_type_node))
2358 : 2742482 : return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2359 : :
2360 : 7308685 : if (bits == TYPE_PRECISION (short_integer_type_node))
2361 : 400929 : return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2362 : :
2363 : 6907756 : if (bits == TYPE_PRECISION (long_integer_type_node))
2364 : 6151624 : return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2365 : :
2366 : 756132 : if (bits == TYPE_PRECISION (long_long_integer_type_node))
2367 : 9039 : return (unsignedp ? long_long_unsigned_type_node
2368 : 8284 : : long_long_integer_type_node);
2369 : :
2370 : 1163412 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
2371 : 747093 : if (int_n_enabled_p[i]
2372 : 740432 : && bits == int_n_data[i].bitsize)
2373 : 330774 : return (unsignedp ? int_n_trees[i].unsigned_type
2374 : 330774 : : int_n_trees[i].signed_type);
2375 : :
2376 : 416319 : if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2377 : 0 : return (unsignedp ? widest_unsigned_literal_type_node
2378 : 0 : : widest_integer_literal_type_node);
2379 : :
2380 : 1649250 : for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
2381 : : {
2382 : 1232931 : tree type = TREE_VALUE (t);
2383 : 1232931 : if (TREE_CODE (type) == INTEGER_TYPE
2384 : 0 : && bits == TYPE_PRECISION (type)
2385 : 1232931 : && !!unsignedp == !!TYPE_UNSIGNED (type))
2386 : : return type;
2387 : : }
2388 : :
2389 : 416319 : if (bits <= TYPE_PRECISION (intQI_type_node))
2390 : 354086 : return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2391 : :
2392 : 62233 : if (bits <= TYPE_PRECISION (intHI_type_node))
2393 : 9651 : return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2394 : :
2395 : 52582 : if (bits <= TYPE_PRECISION (intSI_type_node))
2396 : 20844 : return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2397 : :
2398 : 31738 : if (bits <= TYPE_PRECISION (intDI_type_node))
2399 : 25637 : return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2400 : :
2401 : 6101 : if (bits <= TYPE_PRECISION (widest_integer_literal_type_node))
2402 : 31 : return (unsignedp ? widest_unsigned_literal_type_node
2403 : 14 : : widest_integer_literal_type_node);
2404 : :
2405 : : return NULL_TREE;
2406 : : }
2407 : :
2408 : : /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2409 : : that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2410 : : and saturating if SATP is nonzero, otherwise not saturating. */
2411 : :
2412 : : tree
2413 : 0 : c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2414 : : int unsignedp, int satp)
2415 : : {
2416 : 0 : enum mode_class mclass;
2417 : 0 : if (ibit == 0)
2418 : 0 : mclass = unsignedp ? MODE_UFRACT : MODE_FRACT;
2419 : : else
2420 : 0 : mclass = unsignedp ? MODE_UACCUM : MODE_ACCUM;
2421 : :
2422 : 0 : opt_scalar_mode opt_mode;
2423 : 0 : scalar_mode mode;
2424 : 0 : FOR_EACH_MODE_IN_CLASS (opt_mode, mclass)
2425 : : {
2426 : 0 : mode = opt_mode.require ();
2427 : 0 : if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2428 : : break;
2429 : : }
2430 : :
2431 : 0 : if (!opt_mode.exists (&mode) || !targetm.scalar_mode_supported_p (mode))
2432 : : {
2433 : 0 : sorry ("GCC cannot support operators with integer types and "
2434 : : "fixed-point types that have too many integral and "
2435 : : "fractional bits together");
2436 : 0 : return NULL_TREE;
2437 : : }
2438 : :
2439 : 0 : return c_common_type_for_mode (mode, satp);
2440 : : }
2441 : :
2442 : : /* Used for communication between c_common_type_for_mode and
2443 : : c_register_builtin_type. */
2444 : : tree registered_builtin_types;
2445 : :
2446 : : /* Return a data type that has machine mode MODE.
2447 : : If the mode is an integer,
2448 : : then UNSIGNEDP selects between signed and unsigned types.
2449 : : If the mode is a fixed-point mode,
2450 : : then UNSIGNEDP selects between saturating and nonsaturating types. */
2451 : :
2452 : : tree
2453 : 13311975 : c_common_type_for_mode (machine_mode mode, int unsignedp)
2454 : : {
2455 : 13311975 : tree t;
2456 : 13311975 : int i;
2457 : :
2458 : 13311975 : if (mode == TYPE_MODE (integer_type_node))
2459 : 1802248 : return unsignedp ? unsigned_type_node : integer_type_node;
2460 : :
2461 : 11509727 : if (mode == TYPE_MODE (signed_char_type_node))
2462 : 2422243 : return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2463 : :
2464 : 9087484 : if (mode == TYPE_MODE (short_integer_type_node))
2465 : 123577 : return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2466 : :
2467 : 8963907 : if (mode == TYPE_MODE (long_integer_type_node))
2468 : 5558021 : return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2469 : :
2470 : 3405886 : if (mode == TYPE_MODE (long_long_integer_type_node))
2471 : 83848 : return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2472 : :
2473 : 5123898 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
2474 : 3322038 : if (int_n_enabled_p[i]
2475 : 3322038 : && mode == int_n_data[i].m)
2476 : 1520178 : return (unsignedp ? int_n_trees[i].unsigned_type
2477 : 1520178 : : int_n_trees[i].signed_type);
2478 : :
2479 : 1801860 : if (mode == QImode)
2480 : 0 : return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2481 : :
2482 : 1801860 : if (mode == HImode)
2483 : 0 : return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2484 : :
2485 : 1801860 : if (mode == SImode)
2486 : 0 : return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2487 : :
2488 : 1801860 : if (mode == DImode)
2489 : 0 : return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2490 : :
2491 : : #if HOST_BITS_PER_WIDE_INT >= 64
2492 : 1801860 : if (mode == TYPE_MODE (intTI_type_node))
2493 : 0 : return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2494 : : #endif
2495 : :
2496 : 1801860 : if (mode == TYPE_MODE (float_type_node))
2497 : 53594 : return float_type_node;
2498 : :
2499 : 1748266 : if (mode == TYPE_MODE (double_type_node))
2500 : 47468 : return double_type_node;
2501 : :
2502 : 1700798 : if (mode == TYPE_MODE (long_double_type_node))
2503 : 17599 : return long_double_type_node;
2504 : :
2505 : 12740644 : for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2506 : 11233357 : if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2507 : 11233357 : && mode == TYPE_MODE (FLOATN_NX_TYPE_NODE (i)))
2508 : 175912 : return FLOATN_NX_TYPE_NODE (i);
2509 : :
2510 : 1507287 : if (mode == TYPE_MODE (void_type_node))
2511 : 0 : return void_type_node;
2512 : :
2513 : 1507287 : if (mode == TYPE_MODE (build_pointer_type (char_type_node))
2514 : 1507287 : || mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2515 : : {
2516 : 0 : unsigned int precision
2517 : 0 : = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode));
2518 : 0 : return (unsignedp
2519 : 0 : ? make_unsigned_type (precision)
2520 : 0 : : make_signed_type (precision));
2521 : : }
2522 : :
2523 : 1507287 : if (COMPLEX_MODE_P (mode))
2524 : : {
2525 : 1268980 : machine_mode inner_mode;
2526 : 1268980 : tree inner_type;
2527 : :
2528 : 1268980 : if (mode == TYPE_MODE (complex_float_type_node))
2529 : 212251 : return complex_float_type_node;
2530 : 1056729 : if (mode == TYPE_MODE (complex_double_type_node))
2531 : 211309 : return complex_double_type_node;
2532 : 845420 : if (mode == TYPE_MODE (complex_long_double_type_node))
2533 : 211303 : return complex_long_double_type_node;
2534 : :
2535 : 2746640 : for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2536 : 2535604 : if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2537 : 2535604 : && mode == TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i)))
2538 : 423081 : return COMPLEX_FLOATN_NX_TYPE_NODE (i);
2539 : :
2540 : 211036 : if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2541 : 12 : return complex_integer_type_node;
2542 : :
2543 : 211024 : inner_mode = GET_MODE_INNER (mode);
2544 : 211024 : inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2545 : 211024 : if (inner_type != NULL_TREE)
2546 : 211024 : return build_complex_type (inner_type);
2547 : : }
2548 : 238307 : else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
2549 : 238307 : && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
2550 : : {
2551 : 0 : unsigned int elem_bits = vector_element_size (GET_MODE_PRECISION (mode),
2552 : : GET_MODE_NUNITS (mode));
2553 : 0 : tree bool_type = build_nonstandard_boolean_type (elem_bits);
2554 : 0 : return build_vector_type_for_mode (bool_type, mode);
2555 : : }
2556 : 226280 : else if (VECTOR_MODE_P (mode)
2557 : 252524 : && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
2558 : : {
2559 : 14217 : machine_mode inner_mode = GET_MODE_INNER (mode);
2560 : 14217 : tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2561 : 14217 : if (inner_type != NULL_TREE)
2562 : 14217 : return build_vector_type_for_mode (inner_type, mode);
2563 : : }
2564 : :
2565 : 224090 : if (dfloat32_type_node != NULL_TREE
2566 : 224090 : && mode == TYPE_MODE (dfloat32_type_node))
2567 : 2794 : return dfloat32_type_node;
2568 : 221296 : if (dfloat64_type_node != NULL_TREE
2569 : 221296 : && mode == TYPE_MODE (dfloat64_type_node))
2570 : 3138 : return dfloat64_type_node;
2571 : 218158 : if (dfloat128_type_node != NULL_TREE
2572 : 218158 : && mode == TYPE_MODE (dfloat128_type_node))
2573 : 2425 : return dfloat128_type_node;
2574 : :
2575 : 215733 : if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
2576 : : {
2577 : 0 : if (mode == TYPE_MODE (short_fract_type_node))
2578 : 0 : return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
2579 : 0 : if (mode == TYPE_MODE (fract_type_node))
2580 : 0 : return unsignedp ? sat_fract_type_node : fract_type_node;
2581 : 0 : if (mode == TYPE_MODE (long_fract_type_node))
2582 : 0 : return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
2583 : 0 : if (mode == TYPE_MODE (long_long_fract_type_node))
2584 : 0 : return unsignedp ? sat_long_long_fract_type_node
2585 : 0 : : long_long_fract_type_node;
2586 : :
2587 : 0 : if (mode == TYPE_MODE (unsigned_short_fract_type_node))
2588 : 0 : return unsignedp ? sat_unsigned_short_fract_type_node
2589 : 0 : : unsigned_short_fract_type_node;
2590 : 0 : if (mode == TYPE_MODE (unsigned_fract_type_node))
2591 : 0 : return unsignedp ? sat_unsigned_fract_type_node
2592 : 0 : : unsigned_fract_type_node;
2593 : 0 : if (mode == TYPE_MODE (unsigned_long_fract_type_node))
2594 : 0 : return unsignedp ? sat_unsigned_long_fract_type_node
2595 : 0 : : unsigned_long_fract_type_node;
2596 : 0 : if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
2597 : 0 : return unsignedp ? sat_unsigned_long_long_fract_type_node
2598 : 0 : : unsigned_long_long_fract_type_node;
2599 : :
2600 : 0 : if (mode == TYPE_MODE (short_accum_type_node))
2601 : 0 : return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
2602 : 0 : if (mode == TYPE_MODE (accum_type_node))
2603 : 0 : return unsignedp ? sat_accum_type_node : accum_type_node;
2604 : 0 : if (mode == TYPE_MODE (long_accum_type_node))
2605 : 0 : return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
2606 : 0 : if (mode == TYPE_MODE (long_long_accum_type_node))
2607 : 0 : return unsignedp ? sat_long_long_accum_type_node
2608 : 0 : : long_long_accum_type_node;
2609 : :
2610 : 0 : if (mode == TYPE_MODE (unsigned_short_accum_type_node))
2611 : 0 : return unsignedp ? sat_unsigned_short_accum_type_node
2612 : 0 : : unsigned_short_accum_type_node;
2613 : 0 : if (mode == TYPE_MODE (unsigned_accum_type_node))
2614 : 0 : return unsignedp ? sat_unsigned_accum_type_node
2615 : 0 : : unsigned_accum_type_node;
2616 : 0 : if (mode == TYPE_MODE (unsigned_long_accum_type_node))
2617 : 0 : return unsignedp ? sat_unsigned_long_accum_type_node
2618 : 0 : : unsigned_long_accum_type_node;
2619 : 0 : if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
2620 : 0 : return unsignedp ? sat_unsigned_long_long_accum_type_node
2621 : 0 : : unsigned_long_long_accum_type_node;
2622 : :
2623 : 0 : if (mode == QQmode)
2624 : 0 : return unsignedp ? sat_qq_type_node : qq_type_node;
2625 : : if (mode == HQmode)
2626 : 0 : return unsignedp ? sat_hq_type_node : hq_type_node;
2627 : : if (mode == SQmode)
2628 : 0 : return unsignedp ? sat_sq_type_node : sq_type_node;
2629 : : if (mode == DQmode)
2630 : 0 : return unsignedp ? sat_dq_type_node : dq_type_node;
2631 : : if (mode == TQmode)
2632 : 0 : return unsignedp ? sat_tq_type_node : tq_type_node;
2633 : :
2634 : : if (mode == UQQmode)
2635 : 0 : return unsignedp ? sat_uqq_type_node : uqq_type_node;
2636 : : if (mode == UHQmode)
2637 : 0 : return unsignedp ? sat_uhq_type_node : uhq_type_node;
2638 : : if (mode == USQmode)
2639 : 0 : return unsignedp ? sat_usq_type_node : usq_type_node;
2640 : : if (mode == UDQmode)
2641 : 0 : return unsignedp ? sat_udq_type_node : udq_type_node;
2642 : : if (mode == UTQmode)
2643 : 0 : return unsignedp ? sat_utq_type_node : utq_type_node;
2644 : :
2645 : : if (mode == HAmode)
2646 : 0 : return unsignedp ? sat_ha_type_node : ha_type_node;
2647 : : if (mode == SAmode)
2648 : 0 : return unsignedp ? sat_sa_type_node : sa_type_node;
2649 : : if (mode == DAmode)
2650 : 0 : return unsignedp ? sat_da_type_node : da_type_node;
2651 : : if (mode == TAmode)
2652 : 0 : return unsignedp ? sat_ta_type_node : ta_type_node;
2653 : :
2654 : : if (mode == UHAmode)
2655 : 0 : return unsignedp ? sat_uha_type_node : uha_type_node;
2656 : : if (mode == USAmode)
2657 : 0 : return unsignedp ? sat_usa_type_node : usa_type_node;
2658 : : if (mode == UDAmode)
2659 : 0 : return unsignedp ? sat_uda_type_node : uda_type_node;
2660 : : if (mode == UTAmode)
2661 : 0 : return unsignedp ? sat_uta_type_node : uta_type_node;
2662 : : }
2663 : :
2664 : 218333 : for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
2665 : : {
2666 : 217481 : tree type = TREE_VALUE (t);
2667 : 217481 : if (TYPE_MODE (type) == mode
2668 : 214984 : && VECTOR_TYPE_P (type) == VECTOR_MODE_P (mode)
2669 : 432465 : && !!unsignedp == !!TYPE_UNSIGNED (type))
2670 : : return type;
2671 : : }
2672 : : return NULL_TREE;
2673 : : }
2674 : :
2675 : : tree
2676 : 1613197 : c_common_unsigned_type (tree type)
2677 : : {
2678 : 1613197 : return c_common_signed_or_unsigned_type (1, type);
2679 : : }
2680 : :
2681 : : /* Return a signed type the same as TYPE in other respects. */
2682 : :
2683 : : tree
2684 : 5430042 : c_common_signed_type (tree type)
2685 : : {
2686 : 5430042 : return c_common_signed_or_unsigned_type (0, type);
2687 : : }
2688 : :
2689 : : /* Return a type the same as TYPE except unsigned or
2690 : : signed according to UNSIGNEDP. */
2691 : :
2692 : : tree
2693 : 10196664 : c_common_signed_or_unsigned_type (int unsignedp, tree type)
2694 : : {
2695 : 10196664 : tree type1;
2696 : 10196664 : int i;
2697 : :
2698 : : /* This block of code emulates the behavior of the old
2699 : : c_common_unsigned_type. In particular, it returns
2700 : : long_unsigned_type_node if passed a long, even when a int would
2701 : : have the same size. This is necessary for warnings to work
2702 : : correctly in archs where sizeof(int) == sizeof(long) */
2703 : :
2704 : 10196664 : type1 = TYPE_MAIN_VARIANT (type);
2705 : 10196664 : if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
2706 : 2818882 : return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2707 : 7377782 : if (type1 == integer_type_node || type1 == unsigned_type_node)
2708 : 1989485 : return unsignedp ? unsigned_type_node : integer_type_node;
2709 : 5388297 : if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
2710 : 1584149 : return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2711 : 3804148 : if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
2712 : 1027961 : return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2713 : 2776187 : if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
2714 : 525429 : return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2715 : :
2716 : 4442758 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
2717 : 2250758 : if (int_n_enabled_p[i]
2718 : 2071034 : && (type1 == int_n_trees[i].unsigned_type
2719 : 2041869 : || type1 == int_n_trees[i].signed_type))
2720 : 58758 : return (unsignedp ? int_n_trees[i].unsigned_type
2721 : 58758 : : int_n_trees[i].signed_type);
2722 : :
2723 : : #if HOST_BITS_PER_WIDE_INT >= 64
2724 : 2192000 : if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
2725 : 0 : return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2726 : : #endif
2727 : 2192000 : if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
2728 : 0 : return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2729 : 2192000 : if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
2730 : 0 : return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2731 : 2192000 : if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
2732 : 0 : return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2733 : 2192000 : if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
2734 : 0 : return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2735 : :
2736 : : #define C_COMMON_FIXED_TYPES(NAME) \
2737 : : if (type1 == short_ ## NAME ## _type_node \
2738 : : || type1 == unsigned_short_ ## NAME ## _type_node) \
2739 : : return unsignedp ? unsigned_short_ ## NAME ## _type_node \
2740 : : : short_ ## NAME ## _type_node; \
2741 : : if (type1 == NAME ## _type_node \
2742 : : || type1 == unsigned_ ## NAME ## _type_node) \
2743 : : return unsignedp ? unsigned_ ## NAME ## _type_node \
2744 : : : NAME ## _type_node; \
2745 : : if (type1 == long_ ## NAME ## _type_node \
2746 : : || type1 == unsigned_long_ ## NAME ## _type_node) \
2747 : : return unsignedp ? unsigned_long_ ## NAME ## _type_node \
2748 : : : long_ ## NAME ## _type_node; \
2749 : : if (type1 == long_long_ ## NAME ## _type_node \
2750 : : || type1 == unsigned_long_long_ ## NAME ## _type_node) \
2751 : : return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
2752 : : : long_long_ ## NAME ## _type_node;
2753 : :
2754 : : #define C_COMMON_FIXED_MODE_TYPES(NAME) \
2755 : : if (type1 == NAME ## _type_node \
2756 : : || type1 == u ## NAME ## _type_node) \
2757 : : return unsignedp ? u ## NAME ## _type_node \
2758 : : : NAME ## _type_node;
2759 : :
2760 : : #define C_COMMON_FIXED_TYPES_SAT(NAME) \
2761 : : if (type1 == sat_ ## short_ ## NAME ## _type_node \
2762 : : || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
2763 : : return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
2764 : : : sat_ ## short_ ## NAME ## _type_node; \
2765 : : if (type1 == sat_ ## NAME ## _type_node \
2766 : : || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
2767 : : return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
2768 : : : sat_ ## NAME ## _type_node; \
2769 : : if (type1 == sat_ ## long_ ## NAME ## _type_node \
2770 : : || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
2771 : : return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
2772 : : : sat_ ## long_ ## NAME ## _type_node; \
2773 : : if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
2774 : : || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
2775 : : return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
2776 : : : sat_ ## long_long_ ## NAME ## _type_node;
2777 : :
2778 : : #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
2779 : : if (type1 == sat_ ## NAME ## _type_node \
2780 : : || type1 == sat_ ## u ## NAME ## _type_node) \
2781 : : return unsignedp ? sat_ ## u ## NAME ## _type_node \
2782 : : : sat_ ## NAME ## _type_node;
2783 : :
2784 : 2192000 : C_COMMON_FIXED_TYPES (fract);
2785 : 2192000 : C_COMMON_FIXED_TYPES_SAT (fract);
2786 : 2192000 : C_COMMON_FIXED_TYPES (accum);
2787 : 2192000 : C_COMMON_FIXED_TYPES_SAT (accum);
2788 : :
2789 : 2192000 : C_COMMON_FIXED_MODE_TYPES (qq);
2790 : 2192000 : C_COMMON_FIXED_MODE_TYPES (hq);
2791 : 2192000 : C_COMMON_FIXED_MODE_TYPES (sq);
2792 : 2192000 : C_COMMON_FIXED_MODE_TYPES (dq);
2793 : 2192000 : C_COMMON_FIXED_MODE_TYPES (tq);
2794 : 2192000 : C_COMMON_FIXED_MODE_TYPES_SAT (qq);
2795 : 2192000 : C_COMMON_FIXED_MODE_TYPES_SAT (hq);
2796 : 2192000 : C_COMMON_FIXED_MODE_TYPES_SAT (sq);
2797 : 2192000 : C_COMMON_FIXED_MODE_TYPES_SAT (dq);
2798 : 2192000 : C_COMMON_FIXED_MODE_TYPES_SAT (tq);
2799 : 2192000 : C_COMMON_FIXED_MODE_TYPES (ha);
2800 : 2192000 : C_COMMON_FIXED_MODE_TYPES (sa);
2801 : 2192000 : C_COMMON_FIXED_MODE_TYPES (da);
2802 : 2192000 : C_COMMON_FIXED_MODE_TYPES (ta);
2803 : 2192000 : C_COMMON_FIXED_MODE_TYPES_SAT (ha);
2804 : 2192000 : C_COMMON_FIXED_MODE_TYPES_SAT (sa);
2805 : 2192000 : C_COMMON_FIXED_MODE_TYPES_SAT (da);
2806 : 2192000 : C_COMMON_FIXED_MODE_TYPES_SAT (ta);
2807 : :
2808 : : /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
2809 : : the precision; they have precision set to match their range, but
2810 : : may use a wider mode to match an ABI. If we change modes, we may
2811 : : wind up with bad conversions. For INTEGER_TYPEs in C, must check
2812 : : the precision as well, so as to yield correct results for
2813 : : bit-field types. C++ does not have these separate bit-field
2814 : : types, and producing a signed or unsigned variant of an
2815 : : ENUMERAL_TYPE may cause other problems as well. */
2816 : :
2817 : 2192000 : if (!INTEGRAL_TYPE_P (type)
2818 : 2192000 : || TYPE_UNSIGNED (type) == unsignedp)
2819 : : return type;
2820 : :
2821 : 1186781 : if (TREE_CODE (type) == BITINT_TYPE
2822 : : /* signed _BitInt(1) is invalid, avoid creating that. */
2823 : 1186781 : && (unsignedp || TYPE_PRECISION (type) > 1))
2824 : 1913 : return build_bitint_type (TYPE_PRECISION (type), unsignedp);
2825 : :
2826 : : #define TYPE_OK(node) \
2827 : : (TYPE_MODE (type) == TYPE_MODE (node) \
2828 : : && TYPE_PRECISION (type) == TYPE_PRECISION (node))
2829 : 1184868 : if (TYPE_OK (signed_char_type_node))
2830 : 8011 : return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2831 : 1176857 : if (TYPE_OK (integer_type_node))
2832 : 176935 : return unsignedp ? unsigned_type_node : integer_type_node;
2833 : 999922 : if (TYPE_OK (short_integer_type_node))
2834 : 13483 : return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2835 : 986439 : if (TYPE_OK (long_integer_type_node))
2836 : 978781 : return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2837 : 7658 : if (TYPE_OK (long_long_integer_type_node))
2838 : 43 : return (unsignedp ? long_long_unsigned_type_node
2839 : 4 : : long_long_integer_type_node);
2840 : :
2841 : 14981 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
2842 : 7615 : if (int_n_enabled_p[i]
2843 : 7131 : && TYPE_MODE (type) == int_n_data[i].m
2844 : 7896 : && TYPE_PRECISION (type) == int_n_data[i].bitsize)
2845 : 249 : return (unsignedp ? int_n_trees[i].unsigned_type
2846 : 249 : : int_n_trees[i].signed_type);
2847 : :
2848 : : #if HOST_BITS_PER_WIDE_INT >= 64
2849 : 7366 : if (TYPE_OK (intTI_type_node))
2850 : 0 : return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2851 : : #endif
2852 : 7366 : if (TYPE_OK (intDI_type_node))
2853 : 0 : return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2854 : 7366 : if (TYPE_OK (intSI_type_node))
2855 : 0 : return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2856 : 7366 : if (TYPE_OK (intHI_type_node))
2857 : 0 : return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2858 : 7366 : if (TYPE_OK (intQI_type_node))
2859 : 0 : return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2860 : : #undef TYPE_OK
2861 : :
2862 : 7366 : return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
2863 : : }
2864 : :
2865 : : /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
2866 : :
2867 : : tree
2868 : 335746 : c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
2869 : : {
2870 : 335746 : int i;
2871 : :
2872 : : /* Extended integer types of the same width as a standard type have
2873 : : lesser rank, so those of the same width as int promote to int or
2874 : : unsigned int and are valid for printf formats expecting int or
2875 : : unsigned int. To avoid such special cases, avoid creating
2876 : : extended integer types for bit-fields if a standard integer type
2877 : : is available. */
2878 : 335746 : if (width == TYPE_PRECISION (integer_type_node))
2879 : 138 : return unsignedp ? unsigned_type_node : integer_type_node;
2880 : 335608 : if (width == TYPE_PRECISION (signed_char_type_node))
2881 : 7471 : return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2882 : 328137 : if (width == TYPE_PRECISION (short_integer_type_node))
2883 : 1426 : return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2884 : 326711 : if (width == TYPE_PRECISION (long_integer_type_node))
2885 : 0 : return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2886 : 326711 : if (width == TYPE_PRECISION (long_long_integer_type_node))
2887 : 0 : return (unsignedp ? long_long_unsigned_type_node
2888 : 0 : : long_long_integer_type_node);
2889 : 653422 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
2890 : 326711 : if (int_n_enabled_p[i]
2891 : 319640 : && width == int_n_data[i].bitsize)
2892 : 0 : return (unsignedp ? int_n_trees[i].unsigned_type
2893 : 0 : : int_n_trees[i].signed_type);
2894 : 326711 : return build_nonstandard_integer_type (width, unsignedp);
2895 : : }
2896 : :
2897 : : /* The C version of the register_builtin_type langhook. */
2898 : :
2899 : : void
2900 : 632982 : c_register_builtin_type (tree type, const char* name)
2901 : : {
2902 : 632982 : tree decl;
2903 : :
2904 : 632982 : decl = build_decl (UNKNOWN_LOCATION,
2905 : : TYPE_DECL, get_identifier (name), type);
2906 : 632982 : DECL_ARTIFICIAL (decl) = 1;
2907 : 632982 : if (!TYPE_NAME (type))
2908 : 308211 : TYPE_NAME (type) = decl;
2909 : 632982 : lang_hooks.decls.pushdecl (decl);
2910 : :
2911 : 632982 : registered_builtin_types = tree_cons (0, type, registered_builtin_types);
2912 : 632982 : }
2913 : :
2914 : : /* Print an error message for invalid operands to arith operation
2915 : : CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
2916 : : RICHLOC is a rich location for the message, containing either
2917 : : three separate locations for each of the operator and operands
2918 : :
2919 : : lhs op rhs
2920 : : ~~~ ^~ ~~~
2921 : :
2922 : : (C FE), or one location ranging over all over them
2923 : :
2924 : : lhs op rhs
2925 : : ~~~~^~~~~~
2926 : :
2927 : : (C++ FE). */
2928 : :
2929 : : void
2930 : 558 : binary_op_error (rich_location *richloc, enum tree_code code,
2931 : : tree type0, tree type1)
2932 : : {
2933 : 558 : const char *opname;
2934 : :
2935 : 558 : switch (code)
2936 : : {
2937 : : case PLUS_EXPR:
2938 : : opname = "+"; break;
2939 : 14 : case MINUS_EXPR:
2940 : 14 : opname = "-"; break;
2941 : 378 : case MULT_EXPR:
2942 : 378 : opname = "*"; break;
2943 : 0 : case MAX_EXPR:
2944 : 0 : opname = "max"; break;
2945 : 0 : case MIN_EXPR:
2946 : 0 : opname = "min"; break;
2947 : 2 : case EQ_EXPR:
2948 : 2 : opname = "=="; break;
2949 : 2 : case NE_EXPR:
2950 : 2 : opname = "!="; break;
2951 : 0 : case LE_EXPR:
2952 : 0 : opname = "<="; break;
2953 : 0 : case GE_EXPR:
2954 : 0 : opname = ">="; break;
2955 : 1 : case LT_EXPR:
2956 : 1 : opname = "<"; break;
2957 : 1 : case GT_EXPR:
2958 : 1 : opname = ">"; break;
2959 : 21 : case LSHIFT_EXPR:
2960 : 21 : opname = "<<"; break;
2961 : 16 : case RSHIFT_EXPR:
2962 : 16 : opname = ">>"; break;
2963 : 3 : case TRUNC_MOD_EXPR:
2964 : 3 : case FLOOR_MOD_EXPR:
2965 : 3 : opname = "%"; break;
2966 : 6 : case TRUNC_DIV_EXPR:
2967 : 6 : case FLOOR_DIV_EXPR:
2968 : 6 : opname = "/"; break;
2969 : 40 : case BIT_AND_EXPR:
2970 : 40 : opname = "&"; break;
2971 : 21 : case BIT_IOR_EXPR:
2972 : 21 : opname = "|"; break;
2973 : 0 : case TRUTH_ANDIF_EXPR:
2974 : 0 : opname = "&&"; break;
2975 : 0 : case TRUTH_ORIF_EXPR:
2976 : 0 : opname = "||"; break;
2977 : 22 : case BIT_XOR_EXPR:
2978 : 22 : opname = "^"; break;
2979 : 0 : default:
2980 : 0 : gcc_unreachable ();
2981 : : }
2982 : 558 : pp_markup::element_quoted_type element_0 (type0, highlight_colors::lhs);
2983 : 1116 : pp_markup::element_quoted_type element_1 (type1, highlight_colors::rhs);
2984 : 558 : error_at (richloc,
2985 : : "invalid operands to binary %s (have %e and %e)",
2986 : : opname, &element_0, &element_1);
2987 : 558 : }
2988 : :
2989 : : /* Given an expression as a tree, return its original type. Do this
2990 : : by stripping any conversion that preserves the sign and precision. */
2991 : : static tree
2992 : 55821 : expr_original_type (tree expr)
2993 : : {
2994 : 55821 : STRIP_SIGN_NOPS (expr);
2995 : 55821 : return TREE_TYPE (expr);
2996 : : }
2997 : :
2998 : : /* Subroutine of build_binary_op, used for comparison operations.
2999 : : See if the operands have both been converted from subword integer types
3000 : : and, if so, perhaps change them both back to their original type.
3001 : : This function is also responsible for converting the two operands
3002 : : to the proper common type for comparison.
3003 : :
3004 : : The arguments of this function are all pointers to local variables
3005 : : of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3006 : : RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3007 : :
3008 : : LOC is the location of the comparison.
3009 : :
3010 : : If this function returns non-NULL_TREE, it means that the comparison has
3011 : : a constant value. What this function returns is an expression for
3012 : : that value. */
3013 : :
3014 : : tree
3015 : 27981479 : shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
3016 : : tree *restype_ptr, enum tree_code *rescode_ptr)
3017 : : {
3018 : 27981479 : tree type;
3019 : 27981479 : tree op0 = *op0_ptr;
3020 : 27981479 : tree op1 = *op1_ptr;
3021 : 27981479 : int unsignedp0, unsignedp1;
3022 : 27981479 : int real1, real2;
3023 : 27981479 : tree primop0, primop1;
3024 : 27981479 : enum tree_code code = *rescode_ptr;
3025 : :
3026 : : /* Throw away any conversions to wider types
3027 : : already present in the operands. */
3028 : :
3029 : 27981479 : primop0 = c_common_get_narrower (op0, &unsignedp0);
3030 : 27981479 : primop1 = c_common_get_narrower (op1, &unsignedp1);
3031 : :
3032 : : /* If primopN is first sign-extended from primopN's precision to opN's
3033 : : precision, then zero-extended from opN's precision to
3034 : : *restype_ptr precision, shortenings might be invalid. */
3035 : 27981479 : if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3036 : 1639999 : && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3037 : 6379 : && !unsignedp0
3038 : 27987580 : && TYPE_UNSIGNED (TREE_TYPE (op0)))
3039 : : primop0 = op0;
3040 : 27981479 : if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3041 : 978265 : && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3042 : 18659 : && !unsignedp1
3043 : 27994219 : && TYPE_UNSIGNED (TREE_TYPE (op1)))
3044 : : primop1 = op1;
3045 : :
3046 : : /* Handle the case that OP0 does not *contain* a conversion
3047 : : but it *requires* conversion to FINAL_TYPE. */
3048 : :
3049 : 27981479 : if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3050 : 444619 : unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3051 : 27981479 : if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3052 : 7492649 : unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3053 : :
3054 : : /* If one of the operands must be floated, we cannot optimize. */
3055 : 27981479 : real1 = SCALAR_FLOAT_TYPE_P (TREE_TYPE (primop0));
3056 : 27981479 : real2 = SCALAR_FLOAT_TYPE_P (TREE_TYPE (primop1));
3057 : :
3058 : : /* If first arg is constant, swap the args (changing operation
3059 : : so value is preserved), for canonicalization. Don't do this if
3060 : : the second arg is 0. */
3061 : :
3062 : 27981479 : if (TREE_CONSTANT (primop0)
3063 : 6077982 : && !integer_zerop (primop1) && !real_zerop (primop1)
3064 : 32387924 : && !fixed_zerop (primop1))
3065 : : {
3066 : 4406445 : std::swap (primop0, primop1);
3067 : 4406445 : std::swap (op0, op1);
3068 : 4406445 : *op0_ptr = op0;
3069 : 4406445 : *op1_ptr = op1;
3070 : 4406445 : std::swap (unsignedp0, unsignedp1);
3071 : 4406445 : std::swap (real1, real2);
3072 : :
3073 : 4406445 : switch (code)
3074 : : {
3075 : : case LT_EXPR:
3076 : : code = GT_EXPR;
3077 : : break;
3078 : : case GT_EXPR:
3079 : : code = LT_EXPR;
3080 : : break;
3081 : : case LE_EXPR:
3082 : : code = GE_EXPR;
3083 : : break;
3084 : : case GE_EXPR:
3085 : : code = LE_EXPR;
3086 : : break;
3087 : : default:
3088 : : break;
3089 : : }
3090 : 4406445 : *rescode_ptr = code;
3091 : : }
3092 : :
3093 : : /* If comparing an integer against a constant more bits wide,
3094 : : maybe we can deduce a value of 1 or 0 independent of the data.
3095 : : Or else truncate the constant now
3096 : : rather than extend the variable at run time.
3097 : :
3098 : : This is only interesting if the constant is the wider arg.
3099 : : Also, it is not safe if the constant is unsigned and the
3100 : : variable arg is signed, since in this case the variable
3101 : : would be sign-extended and then regarded as unsigned.
3102 : : Our technique fails in this case because the lowest/highest
3103 : : possible unsigned results don't follow naturally from the
3104 : : lowest/highest possible values of the variable operand.
3105 : : For just EQ_EXPR and NE_EXPR there is another technique that
3106 : : could be used: see if the constant can be faithfully represented
3107 : : in the other operand's type, by truncating it and reextending it
3108 : : and see if that preserves the constant's value. */
3109 : :
3110 : 23357535 : if (!real1 && !real2
3111 : 23332654 : && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
3112 : 23332654 : && TREE_CODE (primop1) == INTEGER_CST
3113 : 43194210 : && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3114 : : {
3115 : 1770471 : int min_gt, max_gt, min_lt, max_lt;
3116 : 1770471 : tree maxval, minval;
3117 : : /* 1 if comparison is nominally unsigned. */
3118 : 1770471 : int unsignedp = TYPE_UNSIGNED (*restype_ptr);
3119 : 1770471 : tree val;
3120 : :
3121 : 1770471 : type = c_common_signed_or_unsigned_type (unsignedp0,
3122 : 1770471 : TREE_TYPE (primop0));
3123 : :
3124 : 1770471 : maxval = TYPE_MAX_VALUE (type);
3125 : 1770471 : minval = TYPE_MIN_VALUE (type);
3126 : :
3127 : 1770471 : if (unsignedp && !unsignedp0)
3128 : 478593 : *restype_ptr = c_common_signed_type (*restype_ptr);
3129 : :
3130 : 1770471 : if (TREE_TYPE (primop1) != *restype_ptr)
3131 : : {
3132 : : /* Convert primop1 to target type, but do not introduce
3133 : : additional overflow. We know primop1 is an int_cst. */
3134 : 481513 : primop1 = force_fit_type (*restype_ptr,
3135 : : wi::to_wide
3136 : 963026 : (primop1,
3137 : 481513 : TYPE_PRECISION (*restype_ptr)),
3138 : 481513 : 0, TREE_OVERFLOW (primop1));
3139 : : }
3140 : 1770471 : if (type != *restype_ptr)
3141 : : {
3142 : 1770471 : minval = convert (*restype_ptr, minval);
3143 : 1770471 : maxval = convert (*restype_ptr, maxval);
3144 : : }
3145 : :
3146 : 1770471 : min_gt = tree_int_cst_lt (primop1, minval);
3147 : 1770471 : max_gt = tree_int_cst_lt (primop1, maxval);
3148 : 1770471 : min_lt = tree_int_cst_lt (minval, primop1);
3149 : 1770471 : max_lt = tree_int_cst_lt (maxval, primop1);
3150 : :
3151 : 1770471 : val = 0;
3152 : : /* This used to be a switch, but Genix compiler can't handle that. */
3153 : 1770471 : if (code == NE_EXPR)
3154 : : {
3155 : 366536 : if (max_lt || min_gt)
3156 : 23 : val = truthvalue_true_node;
3157 : : }
3158 : : else if (code == EQ_EXPR)
3159 : : {
3160 : 728004 : if (max_lt || min_gt)
3161 : 35 : val = truthvalue_false_node;
3162 : : }
3163 : : else if (code == LT_EXPR)
3164 : : {
3165 : 362462 : if (max_lt)
3166 : 18 : val = truthvalue_true_node;
3167 : 362462 : if (!min_lt)
3168 : 8530 : val = truthvalue_false_node;
3169 : : }
3170 : : else if (code == GT_EXPR)
3171 : : {
3172 : 131050 : if (min_gt)
3173 : 8 : val = truthvalue_true_node;
3174 : 131050 : if (!max_gt)
3175 : 6 : val = truthvalue_false_node;
3176 : : }
3177 : : else if (code == LE_EXPR)
3178 : : {
3179 : 67465 : if (!max_gt)
3180 : 74 : val = truthvalue_true_node;
3181 : 67465 : if (min_gt)
3182 : 7 : val = truthvalue_false_node;
3183 : : }
3184 : : else if (code == GE_EXPR)
3185 : : {
3186 : 114952 : if (!min_lt)
3187 : 200 : val = truthvalue_true_node;
3188 : 114952 : if (max_lt)
3189 : 14976 : val = truthvalue_false_node;
3190 : : }
3191 : :
3192 : : /* If primop0 was sign-extended and unsigned comparison specd,
3193 : : we did a signed comparison above using the signed type bounds.
3194 : : But the comparison we output must be unsigned.
3195 : :
3196 : : Also, for inequalities, VAL is no good; but if the signed
3197 : : comparison had *any* fixed result, it follows that the
3198 : : unsigned comparison just tests the sign in reverse
3199 : : (positive values are LE, negative ones GE).
3200 : : So we can generate an unsigned comparison
3201 : : against an extreme value of the signed type. */
3202 : :
3203 : 1770471 : if (unsignedp && !unsignedp0)
3204 : : {
3205 : 478593 : if (val != 0)
3206 : 112 : switch (code)
3207 : : {
3208 : 81 : case LT_EXPR:
3209 : 81 : case GE_EXPR:
3210 : 81 : primop1 = TYPE_MIN_VALUE (type);
3211 : 81 : val = 0;
3212 : 81 : break;
3213 : :
3214 : 30 : case LE_EXPR:
3215 : 30 : case GT_EXPR:
3216 : 30 : primop1 = TYPE_MAX_VALUE (type);
3217 : 30 : val = 0;
3218 : 30 : break;
3219 : :
3220 : : default:
3221 : : break;
3222 : : }
3223 : 478593 : type = c_common_unsigned_type (type);
3224 : : }
3225 : :
3226 : 1770471 : if (TREE_CODE (primop0) != INTEGER_CST
3227 : : /* Don't warn if it's from a (non-system) macro. */
3228 : 1770471 : && !(from_macro_expansion_at
3229 : 1190708 : (expansion_point_location_if_in_system_header
3230 : 1190708 : (EXPR_LOCATION (primop0)))))
3231 : : {
3232 : 1149310 : if (val == truthvalue_false_node)
3233 : 8570 : warning_at (loc, OPT_Wtype_limits,
3234 : : "comparison is always false due to limited range of data type");
3235 : 1149310 : if (val == truthvalue_true_node)
3236 : 208 : warning_at (loc, OPT_Wtype_limits,
3237 : : "comparison is always true due to limited range of data type");
3238 : : }
3239 : :
3240 : 1770471 : if (val != 0)
3241 : : {
3242 : : /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3243 : 23766 : if (TREE_SIDE_EFFECTS (primop0))
3244 : 11 : return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
3245 : : return val;
3246 : : }
3247 : :
3248 : : /* Value is not predetermined, but do the comparison
3249 : : in the type of the operand that is not constant.
3250 : : TYPE is already properly set. */
3251 : : }
3252 : :
3253 : : /* If either arg is decimal float and the other is float, find the
3254 : : proper common type to use for comparison. */
3255 : 4623944 : else if (real1 && real2
3256 : 4377254 : && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
3257 : 26227556 : && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
3258 : 16547 : type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3259 : :
3260 : : /* If either arg is decimal float and the other is float, fail. */
3261 : 4607397 : else if (real1 && real2
3262 : 30555168 : && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
3263 : 4360706 : || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
3264 : : {
3265 : 2 : type = *restype_ptr;
3266 : 2 : primop0 = op0;
3267 : 2 : primop1 = op1;
3268 : : }
3269 : :
3270 : 4607395 : else if (real1 && real2
3271 : 30555164 : && (TYPE_PRECISION (TREE_TYPE (primop0))
3272 : 4360705 : == TYPE_PRECISION (TREE_TYPE (primop1))))
3273 : 4262373 : type = TREE_TYPE (primop0);
3274 : :
3275 : : /* If args' natural types are both narrower than nominal type
3276 : : and both extend in the same manner, compare them
3277 : : in the type of the wider arg.
3278 : : Otherwise must actually extend both to the nominal
3279 : : common type lest different ways of extending
3280 : : alter the result.
3281 : : (eg, (short)-1 == (unsigned short)-1 should be 0.) */
3282 : :
3283 : 19153676 : else if (unsignedp0 == unsignedp1 && real1 == real2
3284 : 18895279 : && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
3285 : 540255 : && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr)
3286 : 22304232 : && (type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)))
3287 : 372146 : != error_mark_node)
3288 : : {
3289 : 372146 : type = c_common_signed_or_unsigned_type (unsignedp0
3290 : 372146 : || TYPE_UNSIGNED (*restype_ptr),
3291 : : type);
3292 : : /* Make sure shorter operand is extended the right way
3293 : : to match the longer operand. */
3294 : 372146 : primop0
3295 : 372146 : = convert (c_common_signed_or_unsigned_type (unsignedp0,
3296 : 372146 : TREE_TYPE (primop0)),
3297 : : primop0);
3298 : 372146 : primop1
3299 : 372146 : = convert (c_common_signed_or_unsigned_type (unsignedp1,
3300 : 372146 : TREE_TYPE (primop1)),
3301 : : primop1);
3302 : : }
3303 : : else
3304 : : {
3305 : : /* Here we must do the comparison on the nominal type
3306 : : using the args exactly as we received them. */
3307 : 21559940 : type = *restype_ptr;
3308 : 21559940 : primop0 = op0;
3309 : 21559940 : primop1 = op1;
3310 : :
3311 : : /* We want to fold unsigned comparisons of >= and < against zero.
3312 : : For these, we may also issue a warning if we have a non-constant
3313 : : compared against zero, where the zero was spelled as "0" (rather
3314 : : than merely folding to it).
3315 : : If we have at least one constant, then op1 is constant
3316 : : and we may have a non-constant expression as op0. */
3317 : 21214918 : if (!real1 && !real2 && integer_zerop (primop1)
3318 : 30428859 : && TYPE_UNSIGNED (*restype_ptr))
3319 : : {
3320 : 2988542 : tree value = NULL_TREE;
3321 : : /* All unsigned values are >= 0, so we warn. However,
3322 : : if OP0 is a constant that is >= 0, the signedness of
3323 : : the comparison isn't an issue, so suppress the
3324 : : warning. */
3325 : 2988542 : tree folded_op0 = fold_for_warn (op0);
3326 : 2988542 : bool warn =
3327 : 71669 : warn_type_limits && !in_system_header_at (loc)
3328 : 65449 : && !(TREE_CODE (folded_op0) == INTEGER_CST
3329 : 9628 : && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3330 : : folded_op0)))
3331 : : /* Do not warn for enumeration types. */
3332 : 3044363 : && (TREE_CODE (expr_original_type (folded_op0)) != ENUMERAL_TYPE);
3333 : :
3334 : 2988542 : switch (code)
3335 : : {
3336 : 12514 : case GE_EXPR:
3337 : 12514 : if (warn)
3338 : 62 : warning_at (loc, OPT_Wtype_limits,
3339 : : "comparison of unsigned expression in %<>= 0%> "
3340 : : "is always true");
3341 : 12514 : value = truthvalue_true_node;
3342 : 12514 : break;
3343 : :
3344 : 340970 : case LT_EXPR:
3345 : 340970 : if (warn)
3346 : 55 : warning_at (loc, OPT_Wtype_limits,
3347 : : "comparison of unsigned expression in %<< 0%> "
3348 : : "is always false");
3349 : 340970 : value = truthvalue_false_node;
3350 : 340970 : break;
3351 : :
3352 : : default:
3353 : : break;
3354 : : }
3355 : :
3356 : 353484 : if (value != NULL_TREE)
3357 : : {
3358 : : /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3359 : 353484 : if (TREE_SIDE_EFFECTS (primop0))
3360 : 45 : return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3361 : 45 : primop0, value);
3362 : : return value;
3363 : : }
3364 : : }
3365 : : }
3366 : :
3367 : 27604229 : *op0_ptr = convert (type, primop0);
3368 : 27604229 : *op1_ptr = convert (type, primop1);
3369 : :
3370 : 27604229 : *restype_ptr = truthvalue_type_node;
3371 : :
3372 : 27604229 : return NULL_TREE;
3373 : : }
3374 : :
3375 : : /* Return a tree for the sum or difference (RESULTCODE says which)
3376 : : of pointer PTROP and integer INTOP. */
3377 : :
3378 : : tree
3379 : 5913933 : pointer_int_sum (location_t loc, enum tree_code resultcode,
3380 : : tree ptrop, tree intop, bool complain)
3381 : : {
3382 : 5913933 : tree size_exp, ret;
3383 : :
3384 : : /* The result is a pointer of the same type that is being added. */
3385 : 5913933 : tree result_type = TREE_TYPE (ptrop);
3386 : :
3387 : 5913933 : if (VOID_TYPE_P (TREE_TYPE (result_type)))
3388 : : {
3389 : 928 : if (complain && warn_pointer_arith)
3390 : 61 : pedwarn (loc, OPT_Wpointer_arith,
3391 : : "pointer of type %<void *%> used in arithmetic");
3392 : : else if (!complain)
3393 : 0 : return error_mark_node;
3394 : 928 : size_exp = integer_one_node;
3395 : : }
3396 : 5913005 : else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3397 : : {
3398 : 118 : if (complain && warn_pointer_arith)
3399 : 49 : pedwarn (loc, OPT_Wpointer_arith,
3400 : : "pointer to a function used in arithmetic");
3401 : : else if (!complain)
3402 : 0 : return error_mark_node;
3403 : 118 : size_exp = integer_one_node;
3404 : : }
3405 : 5912887 : else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
3406 : 5912887 : TREE_TYPE (result_type)))
3407 : 0 : size_exp = integer_one_node;
3408 : : else
3409 : : {
3410 : 5912887 : if (!complain && !COMPLETE_TYPE_P (TREE_TYPE (result_type)))
3411 : 108 : return error_mark_node;
3412 : 5912779 : size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type));
3413 : : /* Wrap the pointer expression in a SAVE_EXPR to make sure it
3414 : : is evaluated first when the size expression may depend
3415 : : on it for VM types. */
3416 : 5912779 : if (TREE_SIDE_EFFECTS (size_exp)
3417 : 1935 : && TREE_SIDE_EFFECTS (ptrop)
3418 : 5912798 : && variably_modified_type_p (TREE_TYPE (ptrop), NULL))
3419 : : {
3420 : 19 : ptrop = save_expr (ptrop);
3421 : 19 : size_exp = build2 (COMPOUND_EXPR, TREE_TYPE (intop), ptrop, size_exp);
3422 : : }
3423 : : }
3424 : :
3425 : : /* We are manipulating pointer values, so we don't need to warn
3426 : : about relying on undefined signed overflow. We disable the
3427 : : warning here because we use integer types so fold won't know that
3428 : : they are really pointers. */
3429 : 5913825 : fold_defer_overflow_warnings ();
3430 : :
3431 : : /* If what we are about to multiply by the size of the elements
3432 : : contains a constant term, apply distributive law
3433 : : and multiply that constant term separately.
3434 : : This helps produce common subexpressions. */
3435 : 5913825 : if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3436 : 179093 : && !TREE_CONSTANT (intop)
3437 : 179011 : && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3438 : 144064 : && TREE_CONSTANT (size_exp)
3439 : : /* If the constant comes from pointer subtraction,
3440 : : skip this optimization--it would cause an error. */
3441 : 144063 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3442 : : /* If the constant is unsigned, and smaller than the pointer size,
3443 : : then we must skip this optimization. This is because it could cause
3444 : : an overflow error if the constant is negative but INTOP is not. */
3445 : 288126 : && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (intop))
3446 : 123897 : || (TYPE_PRECISION (TREE_TYPE (intop))
3447 : 123897 : == TYPE_PRECISION (TREE_TYPE (ptrop))))
3448 : 6093916 : && TYPE_PRECISION (TREE_TYPE (intop)) <= TYPE_PRECISION (sizetype))
3449 : : {
3450 : 36028 : tree intop0 = TREE_OPERAND (intop, 0);
3451 : 36028 : tree intop1 = TREE_OPERAND (intop, 1);
3452 : 36028 : if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3453 : 36028 : || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3454 : : {
3455 : 20166 : tree optype = c_common_type_for_size (TYPE_PRECISION (sizetype),
3456 : 20166 : TYPE_UNSIGNED (sizetype));
3457 : 20166 : intop0 = convert (optype, intop0);
3458 : 20166 : intop1 = convert (optype, intop1);
3459 : : }
3460 : 36028 : tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop0), intop0,
3461 : 36028 : convert (TREE_TYPE (intop0), size_exp));
3462 : 36028 : intop0 = convert (sizetype, t);
3463 : 36028 : if (TREE_OVERFLOW_P (intop0) && !TREE_OVERFLOW (t))
3464 : 0 : intop0 = wide_int_to_tree (TREE_TYPE (intop0), wi::to_wide (intop0));
3465 : 36028 : t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop1), intop1,
3466 : 36028 : convert (TREE_TYPE (intop1), size_exp));
3467 : 36028 : intop1 = convert (sizetype, t);
3468 : 36028 : if (TREE_OVERFLOW_P (intop1) && !TREE_OVERFLOW (t))
3469 : 0 : intop1 = wide_int_to_tree (TREE_TYPE (intop1), wi::to_wide (intop1));
3470 : 36028 : intop = build_binary_op (EXPR_LOCATION (intop), TREE_CODE (intop),
3471 : : intop0, intop1, true);
3472 : :
3473 : : /* Create the sum or difference. */
3474 : 36028 : if (resultcode == MINUS_EXPR)
3475 : 51 : intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3476 : :
3477 : 36028 : ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
3478 : :
3479 : 36028 : fold_undefer_and_ignore_overflow_warnings ();
3480 : :
3481 : 36028 : return ret;
3482 : : }
3483 : :
3484 : : /* Convert the integer argument to a type the same size as sizetype
3485 : : so the multiply won't overflow spuriously. */
3486 : 5877797 : if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3487 : 5877797 : || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3488 : 4041118 : intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
3489 : 4041118 : TYPE_UNSIGNED (sizetype)), intop);
3490 : :
3491 : : /* Replace the integer argument with a suitable product by the object size.
3492 : : Do this multiplication as signed, then convert to the appropriate type
3493 : : for the pointer operation and disregard an overflow that occurred only
3494 : : because of the sign-extension change in the latter conversion. */
3495 : 5877797 : {
3496 : 5877797 : tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
3497 : 5877797 : convert (TREE_TYPE (intop), size_exp));
3498 : 5877797 : intop = convert (sizetype, t);
3499 : 5877797 : if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
3500 : 0 : intop = wide_int_to_tree (TREE_TYPE (intop), wi::to_wide (intop));
3501 : : }
3502 : :
3503 : : /* Create the sum or difference. */
3504 : 5877797 : if (resultcode == MINUS_EXPR)
3505 : 247681 : intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3506 : :
3507 : 5877797 : ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
3508 : :
3509 : 5877797 : fold_undefer_and_ignore_overflow_warnings ();
3510 : :
3511 : 5877797 : return ret;
3512 : : }
3513 : :
3514 : : /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3515 : : and if NON_CONST is known not to be permitted in an evaluated part
3516 : : of a constant expression. */
3517 : :
3518 : : tree
3519 : 4475352 : c_wrap_maybe_const (tree expr, bool non_const)
3520 : : {
3521 : 4475352 : location_t loc = EXPR_LOCATION (expr);
3522 : :
3523 : : /* This should never be called for C++. */
3524 : 4475352 : if (c_dialect_cxx ())
3525 : 0 : gcc_unreachable ();
3526 : :
3527 : : /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
3528 : 4476098 : STRIP_TYPE_NOPS (expr);
3529 : 4475352 : expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
3530 : 4475352 : C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
3531 : 4475352 : protected_set_expr_location (expr, loc);
3532 : :
3533 : 4475352 : return expr;
3534 : : }
3535 : :
3536 : : /* Return whether EXPR is a declaration whose address can never be NULL.
3537 : : The address of the first struct member could be NULL only if it were
3538 : : accessed through a NULL pointer, and such an access would be invalid.
3539 : : The address of a weak symbol may be null unless it has a definition. */
3540 : :
3541 : : bool
3542 : 1413 : decl_with_nonnull_addr_p (const_tree expr)
3543 : : {
3544 : 1413 : if (!DECL_P (expr))
3545 : : return false;
3546 : :
3547 : 1159 : if (TREE_CODE (expr) == FIELD_DECL
3548 : 1082 : || TREE_CODE (expr) == PARM_DECL
3549 : 1048 : || TREE_CODE (expr) == LABEL_DECL)
3550 : : return true;
3551 : :
3552 : 1023 : if (!VAR_OR_FUNCTION_DECL_P (expr))
3553 : : return false;
3554 : :
3555 : 1023 : if (!DECL_WEAK (expr))
3556 : : /* Ordinary (non-weak) symbols have nonnull addresses. */
3557 : : return true;
3558 : :
3559 : 532 : if (DECL_INITIAL (expr) && DECL_INITIAL (expr) != error_mark_node)
3560 : : /* Initialized weak symbols have nonnull addresses. */
3561 : : return true;
3562 : :
3563 : 479 : if (DECL_EXTERNAL (expr) || !TREE_STATIC (expr))
3564 : : /* Uninitialized extern weak symbols and weak symbols with no
3565 : : allocated storage might have a null address. */
3566 : : return false;
3567 : :
3568 : 43 : tree attribs = DECL_ATTRIBUTES (expr);
3569 : 43 : if (lookup_attribute ("weakref", attribs))
3570 : : /* Weakref symbols might have a null address unless their referent
3571 : : is known not to. Don't bother following weakref targets here. */
3572 : : return false;
3573 : :
3574 : : return true;
3575 : : }
3576 : :
3577 : : /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3578 : : or for an `if' or `while' statement or ?..: exp. It should already
3579 : : have been validated to be of suitable type; otherwise, a bad
3580 : : diagnostic may result.
3581 : :
3582 : : The EXPR is located at LOCATION.
3583 : :
3584 : : This preparation consists of taking the ordinary
3585 : : representation of an expression expr and producing a valid tree
3586 : : boolean expression describing whether expr is nonzero. We could
3587 : : simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3588 : : but we optimize comparisons, &&, ||, and !.
3589 : :
3590 : : The resulting type should always be `truthvalue_type_node'. */
3591 : :
3592 : : tree
3593 : 10970149 : c_common_truthvalue_conversion (location_t location, tree expr)
3594 : : {
3595 : 11844274 : STRIP_ANY_LOCATION_WRAPPER (expr);
3596 : 11844274 : switch (TREE_CODE (expr))
3597 : : {
3598 : 2505391 : case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
3599 : 2505391 : case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3600 : 2505391 : case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
3601 : 2505391 : case ORDERED_EXPR: case UNORDERED_EXPR:
3602 : 2505391 : if (TREE_TYPE (expr) == truthvalue_type_node)
3603 : : return expr;
3604 : 415287 : expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3605 : 415287 : TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
3606 : 415287 : goto ret;
3607 : :
3608 : 598406 : case TRUTH_ANDIF_EXPR:
3609 : 598406 : case TRUTH_ORIF_EXPR:
3610 : 598406 : case TRUTH_AND_EXPR:
3611 : 598406 : case TRUTH_OR_EXPR:
3612 : 598406 : case TRUTH_XOR_EXPR:
3613 : 598406 : if (TREE_TYPE (expr) == truthvalue_type_node)
3614 : : return expr;
3615 : 74 : expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3616 : : c_common_truthvalue_conversion (location,
3617 : 37 : TREE_OPERAND (expr, 0)),
3618 : : c_common_truthvalue_conversion (location,
3619 : 37 : TREE_OPERAND (expr, 1)));
3620 : 37 : goto ret;
3621 : :
3622 : 337955 : case TRUTH_NOT_EXPR:
3623 : 337955 : if (TREE_TYPE (expr) == truthvalue_type_node)
3624 : : return expr;
3625 : 334982 : expr = build1 (TREE_CODE (expr), truthvalue_type_node,
3626 : : c_common_truthvalue_conversion (location,
3627 : 334982 : TREE_OPERAND (expr, 0)));
3628 : 334982 : goto ret;
3629 : :
3630 : : case ERROR_MARK:
3631 : : return expr;
3632 : :
3633 : 2710292 : case INTEGER_CST:
3634 : 2710292 : if (TREE_CODE (TREE_TYPE (expr)) == ENUMERAL_TYPE
3635 : 782303 : && !integer_zerop (expr)
3636 : 3194968 : && !integer_onep (expr))
3637 : 937 : warning_at (location, OPT_Wint_in_bool_context,
3638 : : "enum constant in boolean context");
3639 : 2710292 : return integer_zerop (expr) ? truthvalue_false_node
3640 : 2710292 : : truthvalue_true_node;
3641 : :
3642 : 1288 : case REAL_CST:
3643 : 1288 : return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
3644 : 1288 : ? truthvalue_true_node
3645 : 1288 : : truthvalue_false_node;
3646 : :
3647 : 0 : case FIXED_CST:
3648 : 0 : return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
3649 : 0 : &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
3650 : 0 : ? truthvalue_true_node
3651 : 0 : : truthvalue_false_node;
3652 : :
3653 : 0 : case FUNCTION_DECL:
3654 : 0 : expr = build_unary_op (location, ADDR_EXPR, expr, false);
3655 : : /* Fall through. */
3656 : :
3657 : 582 : case ADDR_EXPR:
3658 : 582 : {
3659 : 582 : tree inner = TREE_OPERAND (expr, 0);
3660 : 582 : if (decl_with_nonnull_addr_p (inner)
3661 : : /* Check both EXPR and INNER for suppression. */
3662 : 127 : && !warning_suppressed_p (expr, OPT_Waddress)
3663 : 695 : && !warning_suppressed_p (inner, OPT_Waddress))
3664 : : {
3665 : : /* Common Ada programmer's mistake. */
3666 : 107 : warning_at (location,
3667 : 107 : OPT_Waddress,
3668 : : "the address of %qD will always evaluate as %<true%>",
3669 : : inner);
3670 : 107 : suppress_warning (inner, OPT_Waddress);
3671 : 107 : return truthvalue_true_node;
3672 : : }
3673 : : break;
3674 : : }
3675 : :
3676 : 578 : case COMPLEX_EXPR:
3677 : 1734 : expr = build_binary_op (EXPR_LOCATION (expr),
3678 : 578 : (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3679 : : ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3680 : : c_common_truthvalue_conversion (location,
3681 : 578 : TREE_OPERAND (expr, 0)),
3682 : : c_common_truthvalue_conversion (location,
3683 : 578 : TREE_OPERAND (expr, 1)),
3684 : : false);
3685 : 578 : goto ret;
3686 : :
3687 : 74 : case NEGATE_EXPR:
3688 : 74 : case ABS_EXPR:
3689 : 74 : case ABSU_EXPR:
3690 : 74 : case FLOAT_EXPR:
3691 : 74 : case EXCESS_PRECISION_EXPR:
3692 : : /* These don't change whether an object is nonzero or zero. */
3693 : 74 : return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
3694 : :
3695 : 0 : case LROTATE_EXPR:
3696 : 0 : case RROTATE_EXPR:
3697 : : /* These don't change whether an object is zero or nonzero, but
3698 : : we can't ignore them if their second arg has side-effects. */
3699 : 0 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
3700 : : {
3701 : 0 : expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
3702 : 0 : TREE_OPERAND (expr, 1),
3703 : : c_common_truthvalue_conversion
3704 : 0 : (location, TREE_OPERAND (expr, 0)));
3705 : 0 : goto ret;
3706 : : }
3707 : : else
3708 : 0 : return c_common_truthvalue_conversion (location,
3709 : 0 : TREE_OPERAND (expr, 0));
3710 : :
3711 : 502 : case MULT_EXPR:
3712 : 502 : warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3713 : : "%<*%> in boolean context, suggest %<&&%> instead");
3714 : 502 : break;
3715 : :
3716 : 166 : case LSHIFT_EXPR:
3717 : : /* We will only warn on signed shifts here, because the majority of
3718 : : false positive warnings happen in code where unsigned arithmetic
3719 : : was used in anticipation of a possible overflow.
3720 : : Furthermore, if we see an unsigned type here we know that the
3721 : : result of the shift is not subject to integer promotion rules. */
3722 : 166 : if ((TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
3723 : 1 : || TREE_CODE (TREE_TYPE (expr)) == BITINT_TYPE)
3724 : 167 : && !TYPE_UNSIGNED (TREE_TYPE (expr)))
3725 : 150 : warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3726 : : "%<<<%> in boolean context, did you mean %<<%>?");
3727 : : break;
3728 : :
3729 : 47461 : case COND_EXPR:
3730 : 47461 : if (warn_int_in_bool_context
3731 : 48971 : && !from_macro_definition_at (EXPR_LOCATION (expr)))
3732 : : {
3733 : 743 : tree val1 = fold_for_warn (TREE_OPERAND (expr, 1));
3734 : 743 : tree val2 = fold_for_warn (TREE_OPERAND (expr, 2));
3735 : 743 : if (TREE_CODE (val1) == INTEGER_CST
3736 : 80 : && TREE_CODE (val2) == INTEGER_CST
3737 : 80 : && !integer_zerop (val1)
3738 : 72 : && !integer_zerop (val2)
3739 : 759 : && (!integer_onep (val1)
3740 : 12 : || !integer_onep (val2)))
3741 : 12 : warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3742 : : "%<?:%> using integer constants in boolean context, "
3743 : : "the expression will always evaluate to %<true%>");
3744 : 731 : else if ((TREE_CODE (val1) == INTEGER_CST
3745 : 68 : && !integer_zerop (val1)
3746 : 60 : && !integer_onep (val1))
3747 : 799 : || (TREE_CODE (val2) == INTEGER_CST
3748 : 334 : && !integer_zerop (val2)
3749 : 274 : && !integer_onep (val2)))
3750 : 4 : warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3751 : : "%<?:%> using integer constants in boolean context");
3752 : : }
3753 : : /* Distribute the conversion into the arms of a COND_EXPR. */
3754 : 47461 : if (c_dialect_cxx ())
3755 : : /* Avoid premature folding. */
3756 : : break;
3757 : : else
3758 : : {
3759 : 21555 : int w = warn_int_in_bool_context;
3760 : 21555 : warn_int_in_bool_context = 0;
3761 : : /* Folding will happen later for C. */
3762 : 64665 : expr = build3 (COND_EXPR, truthvalue_type_node,
3763 : 21555 : TREE_OPERAND (expr, 0),
3764 : : c_common_truthvalue_conversion (location,
3765 : 21555 : TREE_OPERAND (expr, 1)),
3766 : : c_common_truthvalue_conversion (location,
3767 : 21555 : TREE_OPERAND (expr, 2)));
3768 : 21555 : warn_int_in_bool_context = w;
3769 : 21555 : goto ret;
3770 : : }
3771 : :
3772 : 652821 : CASE_CONVERT:
3773 : 652821 : {
3774 : 652821 : tree totype = TREE_TYPE (expr);
3775 : 652821 : tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
3776 : :
3777 : 652821 : if (POINTER_TYPE_P (totype)
3778 : 2016 : && !c_inhibit_evaluation_warnings
3779 : 1848 : && TREE_CODE (fromtype) == REFERENCE_TYPE)
3780 : : {
3781 : 0 : tree inner = expr;
3782 : 0 : STRIP_NOPS (inner);
3783 : :
3784 : 0 : if (DECL_P (inner))
3785 : 0 : warning_at (location,
3786 : 0 : OPT_Waddress,
3787 : : "the compiler can assume that the address of "
3788 : : "%qD will always evaluate to %<true%>",
3789 : : inner);
3790 : : }
3791 : :
3792 : : /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3793 : : since that affects how `default_conversion' will behave. */
3794 : 652821 : if (TREE_CODE (totype) == REFERENCE_TYPE
3795 : 652821 : || TREE_CODE (fromtype) == REFERENCE_TYPE)
3796 : : break;
3797 : : /* Don't strip a conversion from C++0x scoped enum, since they
3798 : : don't implicitly convert to other types. */
3799 : 652821 : if (TREE_CODE (fromtype) == ENUMERAL_TYPE
3800 : 652821 : && ENUM_IS_SCOPED (fromtype))
3801 : : break;
3802 : : /* If this isn't narrowing the argument, we can ignore it. */
3803 : 652782 : if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
3804 : : {
3805 : 635466 : tree op0 = TREE_OPERAND (expr, 0);
3806 : 635466 : if ((TREE_CODE (fromtype) == POINTER_TYPE
3807 : 1932 : && (TREE_CODE (totype) == INTEGER_TYPE
3808 : 1932 : || TREE_CODE (totype) == BITINT_TYPE))
3809 : 637366 : || warning_suppressed_p (expr, OPT_Waddress))
3810 : : /* Suppress -Waddress for casts to intptr_t, propagating
3811 : : any suppression from the enclosing expression to its
3812 : : operand. */
3813 : 44 : suppress_warning (op0, OPT_Waddress);
3814 : 635466 : return c_common_truthvalue_conversion (location, op0);
3815 : : }
3816 : : }
3817 : : break;
3818 : :
3819 : 8994 : case MODIFY_EXPR:
3820 : 8994 : if (!warning_suppressed_p (expr, OPT_Wparentheses)
3821 : 269 : && warn_parentheses
3822 : 9263 : && warning_at (location, OPT_Wparentheses,
3823 : : "suggest parentheses around assignment used as "
3824 : : "truth value"))
3825 : 14 : suppress_warning (expr, OPT_Wparentheses);
3826 : : break;
3827 : :
3828 : 238605 : case CONST_DECL:
3829 : 238605 : {
3830 : 238605 : tree folded_expr = fold_for_warn (expr);
3831 : 238605 : if (folded_expr != expr)
3832 : : return c_common_truthvalue_conversion (location, folded_expr);
3833 : : }
3834 : : break;
3835 : :
3836 : : default:
3837 : : break;
3838 : : }
3839 : :
3840 : 4794354 : if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
3841 : : {
3842 : 405 : tree t = save_expr (expr);
3843 : 405 : expr = (build_binary_op
3844 : 810 : (EXPR_LOCATION (expr),
3845 : 405 : (TREE_SIDE_EFFECTS (expr)
3846 : : ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3847 : : c_common_truthvalue_conversion
3848 : : (location,
3849 : : build_unary_op (location, REALPART_EXPR, t, false)),
3850 : : c_common_truthvalue_conversion
3851 : : (location,
3852 : : build_unary_op (location, IMAGPART_EXPR, t, false)),
3853 : : false));
3854 : 405 : goto ret;
3855 : : }
3856 : :
3857 : 4793949 : if (FIXED_POINT_TYPE_P (TREE_TYPE (expr)))
3858 : : {
3859 : 0 : tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
3860 : 0 : FCONST0 (TYPE_MODE
3861 : : (TREE_TYPE (expr))));
3862 : 0 : return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, true);
3863 : : }
3864 : : else
3865 : 4793949 : return build_binary_op (location, NE_EXPR, expr, integer_zero_node, true);
3866 : :
3867 : 772844 : ret:
3868 : 772844 : protected_set_expr_location (expr, location);
3869 : 772844 : return expr;
3870 : : }
3871 : :
3872 : : static void def_builtin_1 (enum built_in_function fncode,
3873 : : const char *name,
3874 : : enum built_in_class fnclass,
3875 : : tree fntype, tree libtype,
3876 : : bool both_p, bool fallback_p, bool nonansi_p,
3877 : : tree fnattrs, bool implicit_p);
3878 : :
3879 : :
3880 : : /* Apply the TYPE_QUALS to the new DECL. */
3881 : :
3882 : : void
3883 : 992062126 : c_apply_type_quals_to_decl (int type_quals, tree decl)
3884 : : {
3885 : 992062126 : tree type = TREE_TYPE (decl);
3886 : :
3887 : 992062126 : if (type == error_mark_node)
3888 : : return;
3889 : :
3890 : 992061897 : if ((type_quals & TYPE_QUAL_CONST)
3891 : 639796818 : || (type && TREE_CODE (type) == REFERENCE_TYPE))
3892 : : /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
3893 : : constructor can produce constant init, so rely on cp_finish_decl to
3894 : : clear TREE_READONLY if the variable has non-constant init. */
3895 : 448216565 : TREE_READONLY (decl) = 1;
3896 : 992061897 : if (type_quals & TYPE_QUAL_VOLATILE)
3897 : : {
3898 : 135122 : TREE_SIDE_EFFECTS (decl) = 1;
3899 : 135122 : TREE_THIS_VOLATILE (decl) = 1;
3900 : : }
3901 : 992061897 : if (type_quals & TYPE_QUAL_RESTRICT)
3902 : : {
3903 : 11022982 : while (type && TREE_CODE (type) == ARRAY_TYPE)
3904 : : /* Allow 'restrict' on arrays of pointers.
3905 : : FIXME currently we just ignore it. */
3906 : 7 : type = TREE_TYPE (type);
3907 : 11022975 : if (!type
3908 : 11022975 : || !POINTER_TYPE_P (type)
3909 : 22045947 : || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
3910 : 4 : error ("invalid use of %<restrict%>");
3911 : : }
3912 : : }
3913 : :
3914 : : /* Return the typed-based alias set for T, which may be an expression
3915 : : or a type. Return -1 if we don't do anything special. */
3916 : :
3917 : : alias_set_type
3918 : 605186345 : c_common_get_alias_set (tree t)
3919 : : {
3920 : : /* For VLAs, use the alias set of the element type rather than the
3921 : : default of alias set 0 for types compared structurally. */
3922 : 605186345 : if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
3923 : : {
3924 : 891548 : if (TREE_CODE (t) == ARRAY_TYPE)
3925 : 455749 : return get_alias_set (TREE_TYPE (t));
3926 : : return -1;
3927 : : }
3928 : :
3929 : : /* That's all the expressions we handle specially. */
3930 : 604294797 : if (!TYPE_P (t))
3931 : : return -1;
3932 : :
3933 : : /* Unlike char, char8_t doesn't alias in C++. (In C, char8_t is not
3934 : : a distinct type.) */
3935 : 54732867 : if (flag_char8_t && t == char8_type_node && c_dialect_cxx ())
3936 : : return -1;
3937 : :
3938 : : /* The C standard guarantees that any object may be accessed via an
3939 : : lvalue that has narrow character type. */
3940 : 54732778 : if (t == char_type_node
3941 : 16704317 : || t == signed_char_type_node
3942 : 13063698 : || t == unsigned_char_type_node)
3943 : : return 0;
3944 : :
3945 : : /* The C standard specifically allows aliasing between signed and
3946 : : unsigned variants of the same type. We treat the signed
3947 : : variant as canonical. */
3948 : 1847291 : if (TREE_CODE (t) == INTEGER_TYPE || TREE_CODE (t) == BITINT_TYPE)
3949 : : {
3950 : : /* For normal INTEGER_TYPEs (except ones built by
3951 : : build_nonstandard_integer_type), both signed and unsigned variants
3952 : : of the type are always reachable from GTY roots, so just calling
3953 : : get_alias_set on the signed type is ok. For BITINT_TYPE and
3954 : : non-standard INTEGER_TYPEs, only unsigned could be used and the
3955 : : corresponding signed type could be created on demand and garbage
3956 : : collected as unused, so the alias set of unsigned type could keep
3957 : : changing.
3958 : : Avoid that by remembering the signed type alias set in
3959 : : TYPE_ALIAS_SET and also when being asked about !TYPE_UNSIGNED
3960 : : check if there isn't a corresponding unsigned type with
3961 : : TYPE_ALIAS_SET_KNOWN_P. */
3962 : 470869 : if (TYPE_UNSIGNED (t))
3963 : : {
3964 : : /* There is no signed _BitInt(1). */
3965 : 198877 : if (TREE_CODE (t) == BITINT_TYPE && TYPE_PRECISION (t) == 1)
3966 : : return -1;
3967 : 198870 : tree t1 = c_common_signed_type (t);
3968 : 198870 : gcc_checking_assert (t != t1);
3969 : 198870 : TYPE_ALIAS_SET (t) = get_alias_set (t1);
3970 : 198870 : return TYPE_ALIAS_SET (t);
3971 : : }
3972 : : else
3973 : : {
3974 : 271992 : tree t1 = c_common_unsigned_type (t);
3975 : 271992 : gcc_checking_assert (t != t1);
3976 : 271992 : if (TYPE_ALIAS_SET_KNOWN_P (t1))
3977 : : {
3978 : 332 : TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET (t1);
3979 : 332 : return TYPE_ALIAS_SET (t);
3980 : : }
3981 : : }
3982 : : }
3983 : :
3984 : : return -1;
3985 : : }
3986 : :
3987 : : /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
3988 : : the IS_SIZEOF parameter indicates which operator is being applied.
3989 : : The COMPLAIN flag controls whether we should diagnose possibly
3990 : : ill-formed constructs or not. LOC is the location of the SIZEOF or
3991 : : TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
3992 : : a type in any context should be returned, rather than the normal
3993 : : alignment for that type. */
3994 : :
3995 : : tree
3996 : 21290929 : c_sizeof_or_alignof_type (location_t loc,
3997 : : tree type, bool is_sizeof, bool min_alignof,
3998 : : int complain)
3999 : : {
4000 : 21290929 : const char *op_name;
4001 : 21290929 : tree value = NULL;
4002 : 21290929 : enum tree_code type_code = TREE_CODE (type);
4003 : :
4004 : 21290929 : op_name = is_sizeof ? "sizeof" : "__alignof__";
4005 : :
4006 : 21290929 : if (type_code == FUNCTION_TYPE)
4007 : : {
4008 : 64 : if (is_sizeof)
4009 : : {
4010 : 58 : if (complain && warn_pointer_arith)
4011 : 28 : pedwarn (loc, OPT_Wpointer_arith,
4012 : : "invalid application of %<sizeof%> to a function type");
4013 : : else if (!complain)
4014 : 20 : return error_mark_node;
4015 : 38 : value = size_one_node;
4016 : : }
4017 : : else
4018 : : {
4019 : 6 : if (complain)
4020 : : {
4021 : 6 : if (c_dialect_cxx ())
4022 : 4 : pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
4023 : : "%<alignof%> applied to a function type");
4024 : : else
4025 : 2 : pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
4026 : : "%<_Alignof%> applied to a function type");
4027 : : }
4028 : 6 : value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4029 : : }
4030 : : }
4031 : 21290865 : else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4032 : : {
4033 : 108 : if (type_code == VOID_TYPE
4034 : 108 : && complain && warn_pointer_arith)
4035 : 24 : pedwarn (loc, OPT_Wpointer_arith,
4036 : : "invalid application of %qs to a void type", op_name);
4037 : 84 : else if (!complain)
4038 : 45 : return error_mark_node;
4039 : 63 : value = size_one_node;
4040 : : }
4041 : 21290757 : else if (!COMPLETE_TYPE_P (type)
4042 : 21290757 : && ((!c_dialect_cxx () && !flag_isoc2y)
4043 : : || is_sizeof
4044 : 144 : || type_code != ARRAY_TYPE))
4045 : : {
4046 : 271 : if (complain)
4047 : 194 : error_at (loc, "invalid application of %qs to incomplete type %qT",
4048 : : op_name, type);
4049 : 271 : return error_mark_node;
4050 : : }
4051 : 20599217 : else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4052 : 21775489 : && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4053 : : {
4054 : 0 : if (complain)
4055 : 0 : error_at (loc, "invalid application of %qs to array type %qT of "
4056 : : "incomplete element type", op_name, type);
4057 : 0 : return error_mark_node;
4058 : : }
4059 : 22288222 : else if (!verify_type_context (loc, is_sizeof ? TCTX_SIZEOF : TCTX_ALIGNOF,
4060 : : type, !complain))
4061 : : {
4062 : 0 : if (!complain)
4063 : 0 : return error_mark_node;
4064 : 0 : value = size_one_node;
4065 : : }
4066 : : else
4067 : : {
4068 : 21290486 : if (is_sizeof)
4069 : : /* Convert in case a char is more than one unit. */
4070 : 20292750 : value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4071 : 20292750 : size_int (TYPE_PRECISION (char_type_node)
4072 : : / BITS_PER_UNIT));
4073 : 997736 : else if (min_alignof)
4074 : 786509 : value = size_int (min_align_of_type (type));
4075 : : else
4076 : 211227 : value = size_int (TYPE_ALIGN_UNIT (type));
4077 : : }
4078 : :
4079 : : /* VALUE will have the middle-end integer type sizetype.
4080 : : However, we should really return a value of type `size_t',
4081 : : which is just a typedef for an ordinary integer type. */
4082 : 21290593 : value = fold_convert_loc (loc, size_type_node, value);
4083 : :
4084 : 21290593 : return value;
4085 : : }
4086 : :
4087 : : /* Implement the __alignof keyword: Return the minimum required
4088 : : alignment of EXPR, measured in bytes. For VAR_DECLs,
4089 : : FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4090 : : from an "aligned" __attribute__ specification). LOC is the
4091 : : location of the ALIGNOF operator. */
4092 : :
4093 : : tree
4094 : 40793 : c_alignof_expr (location_t loc, tree expr)
4095 : : {
4096 : 40793 : tree t;
4097 : :
4098 : 40793 : if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (expr)))
4099 : 0 : t = size_one_node;
4100 : :
4101 : 40793 : else if (VAR_OR_FUNCTION_DECL_P (expr))
4102 : 7245 : t = size_int (DECL_ALIGN_UNIT (expr));
4103 : :
4104 : 33548 : else if (TREE_CODE (expr) == COMPONENT_REF
4105 : 33548 : && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4106 : : {
4107 : 1 : error_at (loc, "%<__alignof%> applied to a bit-field");
4108 : 1 : t = size_one_node;
4109 : : }
4110 : 33547 : else if (TREE_CODE (expr) == COMPONENT_REF
4111 : 33547 : && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
4112 : 17569 : t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
4113 : :
4114 : 15978 : else if (INDIRECT_REF_P (expr))
4115 : : {
4116 : 57 : tree t = TREE_OPERAND (expr, 0);
4117 : 57 : tree best = t;
4118 : 57 : int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4119 : :
4120 : 57 : while (CONVERT_EXPR_P (t)
4121 : 57 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
4122 : : {
4123 : 0 : int thisalign;
4124 : :
4125 : 0 : t = TREE_OPERAND (t, 0);
4126 : 0 : thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4127 : 0 : if (thisalign > bestalign)
4128 : 0 : best = t, bestalign = thisalign;
4129 : : }
4130 : 57 : return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
4131 : : }
4132 : : else
4133 : 15921 : return c_alignof (loc, TREE_TYPE (expr));
4134 : :
4135 : 24815 : return fold_convert_loc (loc, size_type_node, t);
4136 : : }
4137 : :
4138 : : /* Implement the _Countof keyword:
4139 : : Return the number of elements of an array. */
4140 : :
4141 : : tree
4142 : 71 : c_countof_type (location_t loc, tree type)
4143 : : {
4144 : 71 : enum tree_code type_code;
4145 : 71 : tree value;
4146 : :
4147 : 71 : type_code = TREE_CODE (type);
4148 : 71 : if (type_code != ARRAY_TYPE)
4149 : : {
4150 : 11 : error_at (loc, "invalid application of %<_Countof%> to type %qT", type);
4151 : 11 : return error_mark_node;
4152 : : }
4153 : 60 : if (!COMPLETE_TYPE_P (type))
4154 : : {
4155 : 2 : error_at (loc,
4156 : : "invalid application of %<_Countof%> to incomplete type %qT",
4157 : : type);
4158 : 2 : return error_mark_node;
4159 : : }
4160 : :
4161 : 58 : value = array_type_nelts_top (type);
4162 : : /* VALUE will have the middle-end integer type sizetype.
4163 : : However, we should really return a value of type `size_t',
4164 : : which is just a typedef for an ordinary integer type. */
4165 : 58 : value = fold_convert_loc (loc, size_type_node, value);
4166 : 58 : return value;
4167 : : }
4168 : :
4169 : : /* Implement the _Maxof operator:
4170 : : Return the maximum representable value of an integer type. */
4171 : :
4172 : : tree
4173 : 44 : c_maxof_type (location_t loc, tree type)
4174 : : {
4175 : 44 : if (!INTEGRAL_TYPE_P (type))
4176 : : {
4177 : 3 : error_at (loc, "invalid application of %<_Maxof%> to type %qT", type);
4178 : 3 : return error_mark_node;
4179 : : }
4180 : 41 : if (!COMPLETE_TYPE_P (type))
4181 : : {
4182 : 1 : error_at (loc, "invalid application of %<_Maxof%> to incomplete type %qT",
4183 : : type);
4184 : 1 : return error_mark_node;
4185 : : }
4186 : :
4187 : 40 : return TYPE_MAX_VALUE (type);
4188 : : }
4189 : :
4190 : : /* Implement the _Minof operator:
4191 : : Return the minimum representable value of an integer type. */
4192 : :
4193 : : tree
4194 : 44 : c_minof_type (location_t loc, tree type)
4195 : : {
4196 : 44 : if (!INTEGRAL_TYPE_P (type))
4197 : : {
4198 : 3 : error_at (loc, "invalid application of %<_Minof%> to type %qT", type);
4199 : 3 : return error_mark_node;
4200 : : }
4201 : 41 : if (!COMPLETE_TYPE_P (type))
4202 : : {
4203 : 1 : error_at (loc, "invalid application of %<_Minof%> to incomplete type %qT",
4204 : : type);
4205 : 1 : return error_mark_node;
4206 : : }
4207 : :
4208 : 40 : return TYPE_MIN_VALUE (type);
4209 : : }
4210 : :
4211 : : /* Handle C and C++ default attributes. */
4212 : :
4213 : : enum built_in_attribute
4214 : : {
4215 : : #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4216 : : #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
4217 : : #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
4218 : : #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4219 : : #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
4220 : : #include "builtin-attrs.def"
4221 : : #undef DEF_ATTR_NULL_TREE
4222 : : #undef DEF_ATTR_INT
4223 : : #undef DEF_ATTR_STRING
4224 : : #undef DEF_ATTR_IDENT
4225 : : #undef DEF_ATTR_TREE_LIST
4226 : : ATTR_LAST
4227 : : };
4228 : :
4229 : : static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4230 : :
4231 : : static void c_init_attributes (void);
4232 : :
4233 : : enum c_builtin_type
4234 : : {
4235 : : #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
4236 : : #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
4237 : : #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
4238 : : #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
4239 : : #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4240 : : #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4241 : : #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
4242 : : #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4243 : : ARG6) NAME,
4244 : : #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4245 : : ARG6, ARG7) NAME,
4246 : : #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4247 : : ARG6, ARG7, ARG8) NAME,
4248 : : #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4249 : : ARG6, ARG7, ARG8, ARG9) NAME,
4250 : : #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4251 : : ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
4252 : : #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4253 : : ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
4254 : : #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
4255 : : #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
4256 : : #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
4257 : : #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4258 : : #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4259 : : #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4260 : : NAME,
4261 : : #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4262 : : ARG6) NAME,
4263 : : #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4264 : : ARG6, ARG7) NAME,
4265 : : #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
4266 : : #include "builtin-types.def"
4267 : : #undef DEF_PRIMITIVE_TYPE
4268 : : #undef DEF_FUNCTION_TYPE_0
4269 : : #undef DEF_FUNCTION_TYPE_1
4270 : : #undef DEF_FUNCTION_TYPE_2
4271 : : #undef DEF_FUNCTION_TYPE_3
4272 : : #undef DEF_FUNCTION_TYPE_4
4273 : : #undef DEF_FUNCTION_TYPE_5
4274 : : #undef DEF_FUNCTION_TYPE_6
4275 : : #undef DEF_FUNCTION_TYPE_7
4276 : : #undef DEF_FUNCTION_TYPE_8
4277 : : #undef DEF_FUNCTION_TYPE_9
4278 : : #undef DEF_FUNCTION_TYPE_10
4279 : : #undef DEF_FUNCTION_TYPE_11
4280 : : #undef DEF_FUNCTION_TYPE_VAR_0
4281 : : #undef DEF_FUNCTION_TYPE_VAR_1
4282 : : #undef DEF_FUNCTION_TYPE_VAR_2
4283 : : #undef DEF_FUNCTION_TYPE_VAR_3
4284 : : #undef DEF_FUNCTION_TYPE_VAR_4
4285 : : #undef DEF_FUNCTION_TYPE_VAR_5
4286 : : #undef DEF_FUNCTION_TYPE_VAR_6
4287 : : #undef DEF_FUNCTION_TYPE_VAR_7
4288 : : #undef DEF_POINTER_TYPE
4289 : : BT_LAST
4290 : : };
4291 : :
4292 : : typedef enum c_builtin_type builtin_type;
4293 : :
4294 : : /* A temporary array for c_common_nodes_and_builtins. Used in
4295 : : communication with def_fn_type. */
4296 : : static tree builtin_types[(int) BT_LAST + 1];
4297 : :
4298 : : /* A helper function for c_common_nodes_and_builtins. Build function type
4299 : : for DEF with return type RET and N arguments. If VAR is true, then the
4300 : : function should be variadic after those N arguments, or, if N is zero,
4301 : : unprototyped.
4302 : :
4303 : : Takes special care not to ICE if any of the types involved are
4304 : : error_mark_node, which indicates that said type is not in fact available
4305 : : (see builtin_type_for_size). In which case the function type as a whole
4306 : : should be error_mark_node. */
4307 : :
4308 : : static void
4309 : 106340976 : def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
4310 : : {
4311 : 106340976 : tree t;
4312 : 106340976 : tree *args = XALLOCAVEC (tree, n);
4313 : 106340976 : va_list list;
4314 : 106340976 : int i;
4315 : :
4316 : 106340976 : va_start (list, n);
4317 : 336861262 : for (i = 0; i < n; ++i)
4318 : : {
4319 : 233522280 : builtin_type a = (builtin_type) va_arg (list, int);
4320 : 233522280 : t = builtin_types[a];
4321 : 233522280 : if (t == error_mark_node)
4322 : 3001994 : goto egress;
4323 : 230520286 : args[i] = t;
4324 : : }
4325 : :
4326 : 103338982 : t = builtin_types[ret];
4327 : 103338982 : if (t == error_mark_node)
4328 : 427330 : goto egress;
4329 : 102911652 : if (var)
4330 : 6324478 : if (n == 0)
4331 : 843976 : t = build_function_type (t, NULL_TREE);
4332 : : else
4333 : 5480502 : t = build_varargs_function_type_array (t, n, args);
4334 : : else
4335 : 96587174 : t = build_function_type_array (t, n, args);
4336 : :
4337 : 106340976 : egress:
4338 : 106340976 : builtin_types[def] = t;
4339 : 106340976 : va_end (list);
4340 : 106340976 : }
4341 : :
4342 : : /* Build builtin functions common to both C and C++ language
4343 : : frontends. */
4344 : :
4345 : : static void
4346 : 210994 : c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
4347 : : {
4348 : : #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
4349 : : builtin_types[ENUM] = VALUE;
4350 : : #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
4351 : : def_fn_type (ENUM, RETURN, 0, 0);
4352 : : #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
4353 : : def_fn_type (ENUM, RETURN, 0, 1, ARG1);
4354 : : #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
4355 : : def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
4356 : : #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4357 : : def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
4358 : : #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4359 : : def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
4360 : : #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4361 : : def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4362 : : #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4363 : : ARG6) \
4364 : : def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
4365 : : #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4366 : : ARG6, ARG7) \
4367 : : def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
4368 : : #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4369 : : ARG6, ARG7, ARG8) \
4370 : : def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
4371 : : ARG7, ARG8);
4372 : : #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4373 : : ARG6, ARG7, ARG8, ARG9) \
4374 : : def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
4375 : : ARG7, ARG8, ARG9);
4376 : : #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4377 : : ARG6, ARG7, ARG8, ARG9, ARG10) \
4378 : : def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
4379 : : ARG7, ARG8, ARG9, ARG10);
4380 : : #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4381 : : ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
4382 : : def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
4383 : : ARG7, ARG8, ARG9, ARG10, ARG11);
4384 : : #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
4385 : : def_fn_type (ENUM, RETURN, 1, 0);
4386 : : #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
4387 : : def_fn_type (ENUM, RETURN, 1, 1, ARG1);
4388 : : #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
4389 : : def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
4390 : : #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4391 : : def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
4392 : : #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4393 : : def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
4394 : : #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4395 : : def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4396 : : #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4397 : : ARG6) \
4398 : : def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
4399 : : #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4400 : : ARG6, ARG7) \
4401 : : def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
4402 : : #define DEF_POINTER_TYPE(ENUM, TYPE) \
4403 : : builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
4404 : :
4405 : : #include "builtin-types.def"
4406 : :
4407 : : #undef DEF_PRIMITIVE_TYPE
4408 : : #undef DEF_FUNCTION_TYPE_0
4409 : : #undef DEF_FUNCTION_TYPE_1
4410 : : #undef DEF_FUNCTION_TYPE_2
4411 : : #undef DEF_FUNCTION_TYPE_3
4412 : : #undef DEF_FUNCTION_TYPE_4
4413 : : #undef DEF_FUNCTION_TYPE_5
4414 : : #undef DEF_FUNCTION_TYPE_6
4415 : : #undef DEF_FUNCTION_TYPE_7
4416 : : #undef DEF_FUNCTION_TYPE_8
4417 : : #undef DEF_FUNCTION_TYPE_9
4418 : : #undef DEF_FUNCTION_TYPE_10
4419 : : #undef DEF_FUNCTION_TYPE_11
4420 : : #undef DEF_FUNCTION_TYPE_VAR_0
4421 : : #undef DEF_FUNCTION_TYPE_VAR_1
4422 : : #undef DEF_FUNCTION_TYPE_VAR_2
4423 : : #undef DEF_FUNCTION_TYPE_VAR_3
4424 : : #undef DEF_FUNCTION_TYPE_VAR_4
4425 : : #undef DEF_FUNCTION_TYPE_VAR_5
4426 : : #undef DEF_FUNCTION_TYPE_VAR_6
4427 : : #undef DEF_FUNCTION_TYPE_VAR_7
4428 : : #undef DEF_POINTER_TYPE
4429 : 210994 : builtin_types[(int) BT_LAST] = NULL_TREE;
4430 : :
4431 : 210994 : c_init_attributes ();
4432 : :
4433 : : #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
4434 : : NONANSI_P, ATTRS, IMPLICIT, COND) \
4435 : : if (NAME && COND) \
4436 : : def_builtin_1 (ENUM, NAME, CLASS, \
4437 : : builtin_types[(int) TYPE], \
4438 : : builtin_types[(int) LIBTYPE], \
4439 : : BOTH_P, FALLBACK_P, NONANSI_P, \
4440 : : built_in_attributes[(int) ATTRS], IMPLICIT);
4441 : : #include "builtins.def"
4442 : :
4443 : 210994 : targetm.init_builtins ();
4444 : :
4445 : 210994 : build_common_builtin_nodes ();
4446 : 210994 : }
4447 : :
4448 : : /* Like get_identifier, but avoid warnings about null arguments when
4449 : : the argument may be NULL for targets where GCC lacks stdint.h type
4450 : : information. */
4451 : :
4452 : : static inline tree
4453 : 5696838 : c_get_ident (const char *id)
4454 : : {
4455 : 2531928 : return get_identifier (id);
4456 : : }
4457 : :
4458 : : /* Build tree nodes and builtin functions common to both C and C++ language
4459 : : frontends. */
4460 : :
4461 : : void
4462 : 210994 : c_common_nodes_and_builtins (void)
4463 : : {
4464 : 210994 : int char8_type_size;
4465 : 210994 : int char16_type_size;
4466 : 210994 : int char32_type_size;
4467 : 210994 : int wchar_type_size;
4468 : 210994 : tree array_domain_type;
4469 : 210994 : tree va_list_ref_type_node;
4470 : 210994 : tree va_list_arg_type_node;
4471 : 210994 : int i;
4472 : :
4473 : 210994 : build_common_tree_nodes (flag_signed_char);
4474 : :
4475 : : /* Define `int' and `char' first so that dbx will output them first. */
4476 : 210994 : record_builtin_type (RID_INT, NULL, integer_type_node);
4477 : 210994 : record_builtin_type (RID_CHAR, "char", char_type_node);
4478 : :
4479 : : /* `signed' is the same as `int'. FIXME: the declarations of "signed",
4480 : : "unsigned long", "long long unsigned" and "unsigned short" were in C++
4481 : : but not C. Are the conditionals here needed? */
4482 : 210994 : if (c_dialect_cxx ())
4483 : 97195 : record_builtin_type (RID_SIGNED, NULL, integer_type_node);
4484 : 210994 : record_builtin_type (RID_LONG, "long int", long_integer_type_node);
4485 : 210994 : record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
4486 : 210994 : record_builtin_type (RID_MAX, "long unsigned int",
4487 : : long_unsigned_type_node);
4488 : :
4489 : 421988 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
4490 : : {
4491 : 210994 : char name[25];
4492 : :
4493 : 210994 : sprintf (name, "__int%d", int_n_data[i].bitsize);
4494 : 210994 : record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
4495 : : int_n_trees[i].signed_type);
4496 : 210994 : sprintf (name, "__int%d__", int_n_data[i].bitsize);
4497 : 210994 : record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
4498 : : int_n_trees[i].signed_type);
4499 : 421988 : ridpointers[RID_FIRST_INT_N + i]
4500 : 210994 : = DECL_NAME (TYPE_NAME (int_n_trees[i].signed_type));
4501 : :
4502 : 210994 : sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
4503 : 210994 : record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
4504 : 210994 : sprintf (name, "__int%d__ unsigned", int_n_data[i].bitsize);
4505 : 210994 : record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
4506 : : }
4507 : :
4508 : 210994 : if (c_dialect_cxx ())
4509 : 97195 : record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
4510 : 210994 : record_builtin_type (RID_MAX, "long long int",
4511 : : long_long_integer_type_node);
4512 : 210994 : record_builtin_type (RID_MAX, "long long unsigned int",
4513 : : long_long_unsigned_type_node);
4514 : 210994 : if (c_dialect_cxx ())
4515 : 97195 : record_builtin_type (RID_MAX, "long long unsigned",
4516 : : long_long_unsigned_type_node);
4517 : 210994 : record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
4518 : 210994 : record_builtin_type (RID_MAX, "short unsigned int",
4519 : : short_unsigned_type_node);
4520 : 210994 : if (c_dialect_cxx ())
4521 : 97195 : record_builtin_type (RID_MAX, "unsigned short",
4522 : : short_unsigned_type_node);
4523 : :
4524 : : /* Define both `signed char' and `unsigned char'. */
4525 : 210994 : record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
4526 : 210994 : record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
4527 : :
4528 : : /* These are types that c_common_type_for_size and
4529 : : c_common_type_for_mode use. */
4530 : 210994 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4531 : : TYPE_DECL, NULL_TREE,
4532 : : intQI_type_node));
4533 : 210994 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4534 : : TYPE_DECL, NULL_TREE,
4535 : : intHI_type_node));
4536 : 210994 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4537 : : TYPE_DECL, NULL_TREE,
4538 : : intSI_type_node));
4539 : 210994 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4540 : : TYPE_DECL, NULL_TREE,
4541 : : intDI_type_node));
4542 : : #if HOST_BITS_PER_WIDE_INT >= 64
4543 : : /* Note that this is different than the __int128 type that's part of
4544 : : the generic __intN support. */
4545 : 210994 : if (targetm.scalar_mode_supported_p (TImode))
4546 : 205652 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4547 : : TYPE_DECL,
4548 : : get_identifier ("__int128_t"),
4549 : : intTI_type_node));
4550 : : #endif
4551 : 210994 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4552 : : TYPE_DECL, NULL_TREE,
4553 : : unsigned_intQI_type_node));
4554 : 210994 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4555 : : TYPE_DECL, NULL_TREE,
4556 : : unsigned_intHI_type_node));
4557 : 210994 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4558 : : TYPE_DECL, NULL_TREE,
4559 : : unsigned_intSI_type_node));
4560 : 210994 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4561 : : TYPE_DECL, NULL_TREE,
4562 : : unsigned_intDI_type_node));
4563 : : #if HOST_BITS_PER_WIDE_INT >= 64
4564 : 210994 : if (targetm.scalar_mode_supported_p (TImode))
4565 : 205652 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4566 : : TYPE_DECL,
4567 : : get_identifier ("__uint128_t"),
4568 : : unsigned_intTI_type_node));
4569 : : #endif
4570 : :
4571 : : /* Create the widest literal types. */
4572 : 210994 : if (targetm.scalar_mode_supported_p (TImode))
4573 : : {
4574 : 205652 : widest_integer_literal_type_node = intTI_type_node;
4575 : 205652 : widest_unsigned_literal_type_node = unsigned_intTI_type_node;
4576 : : }
4577 : : else
4578 : : {
4579 : 5342 : widest_integer_literal_type_node = intDI_type_node;
4580 : 5342 : widest_unsigned_literal_type_node = unsigned_intDI_type_node;
4581 : : }
4582 : :
4583 : 210994 : signed_size_type_node = c_common_signed_type (size_type_node);
4584 : :
4585 : 421988 : pid_type_node =
4586 : 210994 : TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
4587 : :
4588 : 210994 : record_builtin_type (RID_FLOAT, NULL, float_type_node);
4589 : 210994 : record_builtin_type (RID_DOUBLE, NULL, double_type_node);
4590 : 210994 : record_builtin_type (RID_MAX, "long double", long_double_type_node);
4591 : :
4592 : 1898946 : for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4593 : : {
4594 : 1476958 : if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4595 : 1265964 : record_builtin_type ((enum rid) (RID_FLOATN_NX_FIRST + i), NULL,
4596 : : FLOATN_NX_TYPE_NODE (i));
4597 : : }
4598 : :
4599 : : /* For C, let float128t_type_node (__float128 in some backends) be the
4600 : : same type as float128_type_node (_Float128), for C++ let those
4601 : : be distinct types that mangle and behave differently. */
4602 : 210994 : if (c_dialect_cxx ())
4603 : 97195 : float128t_type_node = NULL_TREE;
4604 : :
4605 : : /* Only supported decimal floating point extension if the target
4606 : : actually supports underlying modes. */
4607 : 210994 : if (targetm.scalar_mode_supported_p (SDmode)
4608 : 210994 : && targetm.scalar_mode_supported_p (DDmode)
4609 : 421988 : && targetm.scalar_mode_supported_p (TDmode))
4610 : : {
4611 : 210994 : record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
4612 : 210994 : record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
4613 : 210994 : record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
4614 : 210994 : record_builtin_type (RID_DFLOAT64X, NULL, dfloat64x_type_node);
4615 : : }
4616 : :
4617 : 210994 : if (targetm.fixed_point_supported_p ())
4618 : : {
4619 : 0 : record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
4620 : 0 : record_builtin_type (RID_FRACT, NULL, fract_type_node);
4621 : 0 : record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
4622 : 0 : record_builtin_type (RID_MAX, "long long _Fract",
4623 : : long_long_fract_type_node);
4624 : 0 : record_builtin_type (RID_MAX, "unsigned short _Fract",
4625 : : unsigned_short_fract_type_node);
4626 : 0 : record_builtin_type (RID_MAX, "unsigned _Fract",
4627 : : unsigned_fract_type_node);
4628 : 0 : record_builtin_type (RID_MAX, "unsigned long _Fract",
4629 : : unsigned_long_fract_type_node);
4630 : 0 : record_builtin_type (RID_MAX, "unsigned long long _Fract",
4631 : : unsigned_long_long_fract_type_node);
4632 : 0 : record_builtin_type (RID_MAX, "_Sat short _Fract",
4633 : : sat_short_fract_type_node);
4634 : 0 : record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
4635 : 0 : record_builtin_type (RID_MAX, "_Sat long _Fract",
4636 : : sat_long_fract_type_node);
4637 : 0 : record_builtin_type (RID_MAX, "_Sat long long _Fract",
4638 : : sat_long_long_fract_type_node);
4639 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
4640 : : sat_unsigned_short_fract_type_node);
4641 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
4642 : : sat_unsigned_fract_type_node);
4643 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
4644 : : sat_unsigned_long_fract_type_node);
4645 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
4646 : : sat_unsigned_long_long_fract_type_node);
4647 : 0 : record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
4648 : 0 : record_builtin_type (RID_ACCUM, NULL, accum_type_node);
4649 : 0 : record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
4650 : 0 : record_builtin_type (RID_MAX, "long long _Accum",
4651 : : long_long_accum_type_node);
4652 : 0 : record_builtin_type (RID_MAX, "unsigned short _Accum",
4653 : : unsigned_short_accum_type_node);
4654 : 0 : record_builtin_type (RID_MAX, "unsigned _Accum",
4655 : : unsigned_accum_type_node);
4656 : 0 : record_builtin_type (RID_MAX, "unsigned long _Accum",
4657 : : unsigned_long_accum_type_node);
4658 : 0 : record_builtin_type (RID_MAX, "unsigned long long _Accum",
4659 : : unsigned_long_long_accum_type_node);
4660 : 0 : record_builtin_type (RID_MAX, "_Sat short _Accum",
4661 : : sat_short_accum_type_node);
4662 : 0 : record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
4663 : 0 : record_builtin_type (RID_MAX, "_Sat long _Accum",
4664 : : sat_long_accum_type_node);
4665 : 0 : record_builtin_type (RID_MAX, "_Sat long long _Accum",
4666 : : sat_long_long_accum_type_node);
4667 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
4668 : : sat_unsigned_short_accum_type_node);
4669 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
4670 : : sat_unsigned_accum_type_node);
4671 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
4672 : : sat_unsigned_long_accum_type_node);
4673 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
4674 : : sat_unsigned_long_long_accum_type_node);
4675 : :
4676 : : }
4677 : :
4678 : 210994 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4679 : : TYPE_DECL,
4680 : : get_identifier ("complex int"),
4681 : : complex_integer_type_node));
4682 : 210994 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4683 : : TYPE_DECL,
4684 : : get_identifier ("complex float"),
4685 : : complex_float_type_node));
4686 : 210994 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4687 : : TYPE_DECL,
4688 : : get_identifier ("complex double"),
4689 : : complex_double_type_node));
4690 : 210994 : lang_hooks.decls.pushdecl
4691 : 210994 : (build_decl (UNKNOWN_LOCATION,
4692 : : TYPE_DECL, get_identifier ("complex long double"),
4693 : : complex_long_double_type_node));
4694 : :
4695 : 210994 : if (!c_dialect_cxx ())
4696 : 910392 : for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4697 : 796593 : if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4698 : : {
4699 : 682794 : char buf[30];
4700 : 1365588 : sprintf (buf, "complex _Float%d%s", floatn_nx_types[i].n,
4701 : 682794 : floatn_nx_types[i].extended ? "x" : "");
4702 : 682794 : lang_hooks.decls.pushdecl
4703 : 682794 : (build_decl (UNKNOWN_LOCATION,
4704 : : TYPE_DECL,
4705 : : get_identifier (buf),
4706 : : COMPLEX_FLOATN_NX_TYPE_NODE (i)));
4707 : : }
4708 : :
4709 : : /* Make fileptr_type_node a distinct void * type until
4710 : : FILE type is defined. Likewise for const struct tm*. */
4711 : 1476958 : for (unsigned i = 0; i < ARRAY_SIZE (builtin_structptr_types); ++i)
4712 : 1265964 : builtin_structptr_types[i].node
4713 : 1265964 : = build_variant_type_copy (builtin_structptr_types[i].base);
4714 : :
4715 : 210994 : record_builtin_type (RID_VOID, NULL, void_type_node);
4716 : :
4717 : : /* Set the TYPE_NAME for any variants that were built before
4718 : : record_builtin_type gave names to the built-in types. */
4719 : 210994 : {
4720 : 210994 : tree void_name = TYPE_NAME (void_type_node);
4721 : 210994 : TYPE_NAME (void_type_node) = NULL_TREE;
4722 : 210994 : TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
4723 : 210994 : = void_name;
4724 : 210994 : TYPE_NAME (void_type_node) = void_name;
4725 : : }
4726 : :
4727 : : /* Make a type to be the domain of a few array types
4728 : : whose domains don't really matter.
4729 : : 200 is small enough that it always fits in size_t
4730 : : and large enough that it can hold most function names for the
4731 : : initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4732 : 210994 : array_domain_type = build_index_type (size_int (200));
4733 : :
4734 : : /* Make a type for arrays of characters.
4735 : : With luck nothing will ever really depend on the length of this
4736 : : array type. */
4737 : 210994 : char_array_type_node
4738 : 210994 : = build_array_type (char_type_node, array_domain_type);
4739 : :
4740 : 210994 : string_type_node = build_pointer_type (char_type_node);
4741 : 210994 : const_string_type_node
4742 : 210994 : = build_pointer_type (build_qualified_type
4743 : : (char_type_node, TYPE_QUAL_CONST));
4744 : :
4745 : : /* This is special for C++ so functions can be overloaded. */
4746 : 216439 : wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
4747 : 210994 : wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
4748 : 210994 : wchar_type_size = TYPE_PRECISION (wchar_type_node);
4749 : 210994 : underlying_wchar_type_node = wchar_type_node;
4750 : 210994 : if (c_dialect_cxx ())
4751 : : {
4752 : 97195 : if (TYPE_UNSIGNED (wchar_type_node))
4753 : 7 : wchar_type_node = make_unsigned_type (wchar_type_size);
4754 : : else
4755 : 97188 : wchar_type_node = make_signed_type (wchar_type_size);
4756 : 97195 : record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
4757 : : }
4758 : :
4759 : : /* This is for wide string constants. */
4760 : 210994 : wchar_array_type_node
4761 : 210994 : = build_array_type (wchar_type_node, array_domain_type);
4762 : :
4763 : : /* Define 'char8_t'. */
4764 : 210994 : char8_type_node = get_identifier (CHAR8_TYPE);
4765 : 210994 : char8_type_node = TREE_TYPE (identifier_global_value (char8_type_node));
4766 : 210994 : char8_type_size = TYPE_PRECISION (char8_type_node);
4767 : 210994 : if (c_dialect_cxx ())
4768 : : {
4769 : 97195 : char8_type_node = make_unsigned_type (char8_type_size);
4770 : 97195 : TYPE_STRING_FLAG (char8_type_node) = true;
4771 : :
4772 : 97195 : if (flag_char8_t)
4773 : 64363 : record_builtin_type (RID_CHAR8, "char8_t", char8_type_node);
4774 : : }
4775 : :
4776 : : /* This is for UTF-8 string constants. */
4777 : 210994 : char8_array_type_node
4778 : 210994 : = build_array_type (char8_type_node, array_domain_type);
4779 : :
4780 : : /* Define 'char16_t'. */
4781 : 210994 : char16_type_node = get_identifier (CHAR16_TYPE);
4782 : 210994 : char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
4783 : 210994 : char16_type_size = TYPE_PRECISION (char16_type_node);
4784 : 210994 : if (c_dialect_cxx ())
4785 : : {
4786 : 97195 : char16_type_node = make_unsigned_type (char16_type_size);
4787 : :
4788 : 97195 : if (cxx_dialect >= cxx11)
4789 : 83128 : record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
4790 : : }
4791 : :
4792 : : /* This is for UTF-16 string constants. */
4793 : 210994 : char16_array_type_node
4794 : 210994 : = build_array_type (char16_type_node, array_domain_type);
4795 : :
4796 : : /* Define 'char32_t'. */
4797 : 210994 : char32_type_node = get_identifier (CHAR32_TYPE);
4798 : 210994 : char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
4799 : 210994 : char32_type_size = TYPE_PRECISION (char32_type_node);
4800 : 210994 : if (c_dialect_cxx ())
4801 : : {
4802 : 97195 : char32_type_node = make_unsigned_type (char32_type_size);
4803 : :
4804 : 97195 : if (cxx_dialect >= cxx11)
4805 : 83128 : record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
4806 : : }
4807 : :
4808 : : /* This is for UTF-32 string constants. */
4809 : 210994 : char32_array_type_node
4810 : 210994 : = build_array_type (char32_type_node, array_domain_type);
4811 : :
4812 : 210994 : if (strcmp (WINT_TYPE, "wchar_t") == 0)
4813 : : wint_type_node = wchar_type_node;
4814 : : else
4815 : 421988 : wint_type_node =
4816 : 210994 : TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
4817 : :
4818 : 421988 : intmax_type_node =
4819 : 216439 : TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
4820 : 421988 : uintmax_type_node =
4821 : 216439 : TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
4822 : :
4823 : 210994 : if (SIG_ATOMIC_TYPE)
4824 : 210994 : sig_atomic_type_node =
4825 : 210994 : TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
4826 : 210994 : if (INT8_TYPE)
4827 : 210994 : int8_type_node =
4828 : 210994 : TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
4829 : 210994 : if (INT16_TYPE)
4830 : 210994 : int16_type_node =
4831 : 210994 : TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
4832 : 210994 : if (INT32_TYPE)
4833 : 210994 : int32_type_node =
4834 : 210994 : TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
4835 : 210994 : if (INT64_TYPE)
4836 : 421988 : int64_type_node =
4837 : 421988 : TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
4838 : 210994 : if (UINT8_TYPE)
4839 : 210994 : uint8_type_node =
4840 : 210994 : TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
4841 : 210994 : if (UINT16_TYPE)
4842 : 210994 : c_uint16_type_node = uint16_type_node =
4843 : 210994 : TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
4844 : 210994 : if (UINT32_TYPE)
4845 : 210994 : c_uint32_type_node = uint32_type_node =
4846 : 210994 : TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
4847 : 210994 : if (UINT64_TYPE)
4848 : 421988 : c_uint64_type_node = uint64_type_node =
4849 : 421988 : TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
4850 : 210994 : if (INT_LEAST8_TYPE)
4851 : 210994 : int_least8_type_node =
4852 : 210994 : TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
4853 : 210994 : if (INT_LEAST16_TYPE)
4854 : 210994 : int_least16_type_node =
4855 : 210994 : TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
4856 : 210994 : if (INT_LEAST32_TYPE)
4857 : 210994 : int_least32_type_node =
4858 : 210994 : TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
4859 : 210994 : if (INT_LEAST64_TYPE)
4860 : 421988 : int_least64_type_node =
4861 : 421988 : TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
4862 : 210994 : if (UINT_LEAST8_TYPE)
4863 : 210994 : uint_least8_type_node =
4864 : 210994 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
4865 : 210994 : if (UINT_LEAST16_TYPE)
4866 : 210994 : uint_least16_type_node =
4867 : 210994 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
4868 : 210994 : if (UINT_LEAST32_TYPE)
4869 : 210994 : uint_least32_type_node =
4870 : 210994 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
4871 : 210994 : if (UINT_LEAST64_TYPE)
4872 : 421988 : uint_least64_type_node =
4873 : 421988 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
4874 : 210994 : if (INT_FAST8_TYPE)
4875 : 210994 : int_fast8_type_node =
4876 : 210994 : TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
4877 : 210994 : if (INT_FAST16_TYPE)
4878 : 421988 : int_fast16_type_node =
4879 : 421988 : TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
4880 : 210994 : if (INT_FAST32_TYPE)
4881 : 421988 : int_fast32_type_node =
4882 : 421988 : TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
4883 : 210994 : if (INT_FAST64_TYPE)
4884 : 421988 : int_fast64_type_node =
4885 : 421988 : TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
4886 : 210994 : if (UINT_FAST8_TYPE)
4887 : 210994 : uint_fast8_type_node =
4888 : 210994 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
4889 : 210994 : if (UINT_FAST16_TYPE)
4890 : 421988 : uint_fast16_type_node =
4891 : 421988 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
4892 : 210994 : if (UINT_FAST32_TYPE)
4893 : 421988 : uint_fast32_type_node =
4894 : 421988 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
4895 : 210994 : if (UINT_FAST64_TYPE)
4896 : 421988 : uint_fast64_type_node =
4897 : 421988 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
4898 : 210994 : if (INTPTR_TYPE)
4899 : 421988 : intptr_type_node =
4900 : 421988 : TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
4901 : 210994 : if (UINTPTR_TYPE)
4902 : 421988 : uintptr_type_node =
4903 : 421988 : TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
4904 : :
4905 : 210994 : default_function_type = build_function_type (integer_type_node, NULL_TREE);
4906 : 210994 : unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
4907 : :
4908 : 210994 : lang_hooks.decls.pushdecl
4909 : 210994 : (build_decl (UNKNOWN_LOCATION,
4910 : : TYPE_DECL, get_identifier ("__builtin_va_list"),
4911 : : va_list_type_node));
4912 : 210994 : if (targetm.enum_va_list_p)
4913 : : {
4914 : : int l;
4915 : : const char *pname;
4916 : : tree ptype;
4917 : :
4918 : 622298 : for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
4919 : : {
4920 : 411304 : lang_hooks.decls.pushdecl
4921 : 411304 : (build_decl (UNKNOWN_LOCATION,
4922 : : TYPE_DECL, get_identifier (pname),
4923 : : ptype));
4924 : :
4925 : : }
4926 : : }
4927 : :
4928 : 210994 : if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4929 : : {
4930 : 411220 : va_list_arg_type_node = va_list_ref_type_node =
4931 : 205610 : build_pointer_type (TREE_TYPE (va_list_type_node));
4932 : : }
4933 : : else
4934 : : {
4935 : 5384 : va_list_arg_type_node = va_list_type_node;
4936 : 5384 : va_list_ref_type_node = build_reference_type (va_list_type_node);
4937 : : }
4938 : :
4939 : 210994 : c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
4940 : :
4941 : 210994 : main_identifier_node = get_identifier ("main");
4942 : :
4943 : : /* Create the built-in __null node. It is important that this is
4944 : : not shared. */
4945 : 210994 : null_node = make_int_cst (1, 1);
4946 : 216439 : TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
4947 : :
4948 : : /* Create the built-in nullptr node. This part of its initialization is
4949 : : common to C and C++. The front ends can further adjust its definition
4950 : : in {c,cxx}_init_decl_processing. In particular, we aren't setting the
4951 : : alignment here for C++ backward ABI bug compatibility. */
4952 : 210994 : nullptr_type_node = make_node (NULLPTR_TYPE);
4953 : 421988 : TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4954 : 421988 : TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4955 : 210994 : TYPE_UNSIGNED (nullptr_type_node) = 1;
4956 : 421988 : TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4957 : 210994 : SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4958 : 210994 : nullptr_node = build_int_cst (nullptr_type_node, 0);
4959 : :
4960 : : /* Since builtin_types isn't gc'ed, don't export these nodes. */
4961 : 210994 : memset (builtin_types, 0, sizeof (builtin_types));
4962 : 210994 : }
4963 : :
4964 : : /* The number of named compound-literals generated thus far. */
4965 : : static GTY(()) int compound_literal_number;
4966 : :
4967 : : /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
4968 : :
4969 : : void
4970 : 279 : set_compound_literal_name (tree decl)
4971 : : {
4972 : 279 : char *name;
4973 : 279 : ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
4974 : : compound_literal_number);
4975 : 279 : compound_literal_number++;
4976 : 279 : DECL_NAME (decl) = get_identifier (name);
4977 : 279 : }
4978 : :
4979 : : /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
4980 : : TYPE and operand OP. */
4981 : :
4982 : : static tree
4983 : 50940 : build_va_arg_1 (location_t loc, tree type, tree op)
4984 : : {
4985 : 50940 : tree expr = build1 (VA_ARG_EXPR, type, op);
4986 : 50940 : SET_EXPR_LOCATION (expr, loc);
4987 : 50940 : return expr;
4988 : : }
4989 : :
4990 : : /* Return a VA_ARG_EXPR corresponding to a source-level expression
4991 : : va_arg (EXPR, TYPE) at source location LOC. */
4992 : :
4993 : : tree
4994 : 50940 : build_va_arg (location_t loc, tree expr, tree type)
4995 : : {
4996 : 50940 : tree va_type = TREE_TYPE (expr);
4997 : 50940 : tree canon_va_type = (va_type == error_mark_node
4998 : 50940 : ? error_mark_node
4999 : 50930 : : targetm.canonical_va_list_type (va_type));
5000 : :
5001 : 50940 : if (va_type == error_mark_node
5002 : 50930 : || canon_va_type == NULL_TREE)
5003 : : {
5004 : 10 : if (canon_va_type == NULL_TREE)
5005 : 12 : error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
5006 : :
5007 : : /* Let's handle things neutrally, if expr:
5008 : : - has undeclared type, or
5009 : : - is not an va_list type. */
5010 : 22 : return build_va_arg_1 (loc, type, error_mark_node);
5011 : : }
5012 : :
5013 : 50918 : if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
5014 : : {
5015 : : /* Case 1: Not an array type. */
5016 : :
5017 : : /* Take the address, to get '&ap'. Note that &ap is not a va_list
5018 : : type. */
5019 : 258 : c_common_mark_addressable_vec (expr);
5020 : 258 : expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
5021 : :
5022 : 258 : return build_va_arg_1 (loc, type, expr);
5023 : : }
5024 : :
5025 : : /* Case 2: Array type.
5026 : :
5027 : : Background:
5028 : :
5029 : : For contrast, let's start with the simple case (case 1). If
5030 : : canon_va_type is not an array type, but say a char *, then when
5031 : : passing-by-value a va_list, the type of the va_list param decl is
5032 : : the same as for another va_list decl (all ap's are char *):
5033 : :
5034 : : f2_1 (char * ap)
5035 : : D.1815 = VA_ARG (&ap, 0B, 1);
5036 : : return D.1815;
5037 : :
5038 : : f2 (int i)
5039 : : char * ap.0;
5040 : : char * ap;
5041 : : __builtin_va_start (&ap, 0);
5042 : : ap.0 = ap;
5043 : : res = f2_1 (ap.0);
5044 : : __builtin_va_end (&ap);
5045 : : D.1812 = res;
5046 : : return D.1812;
5047 : :
5048 : : However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
5049 : : va_list the type of the va_list param decl (case 2b, struct * ap) is not
5050 : : the same as for another va_list decl (case 2a, struct ap[1]).
5051 : :
5052 : : f2_1 (struct * ap)
5053 : : D.1844 = VA_ARG (ap, 0B, 0);
5054 : : return D.1844;
5055 : :
5056 : : f2 (int i)
5057 : : struct ap[1];
5058 : : __builtin_va_start (&ap, 0);
5059 : : res = f2_1 (&ap);
5060 : : __builtin_va_end (&ap);
5061 : : D.1841 = res;
5062 : : return D.1841;
5063 : :
5064 : : Case 2b is different because:
5065 : : - on the callee side, the parm decl has declared type va_list, but
5066 : : grokdeclarator changes the type of the parm decl to a pointer to the
5067 : : array elem type.
5068 : : - on the caller side, the pass-by-value uses &ap.
5069 : :
5070 : : We unify these two cases (case 2a: va_list is array type,
5071 : : case 2b: va_list is pointer to array elem type), by adding '&' for the
5072 : : array type case, such that we have a pointer to array elem in both
5073 : : cases. */
5074 : :
5075 : 50660 : if (TREE_CODE (va_type) == ARRAY_TYPE)
5076 : : {
5077 : : /* Case 2a: va_list is array type. */
5078 : :
5079 : : /* Take the address, to get '&ap'. Make sure it's a pointer to array
5080 : : elem type. */
5081 : 49913 : c_common_mark_addressable_vec (expr);
5082 : 49913 : expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
5083 : : expr);
5084 : :
5085 : : /* Verify that &ap is still recognized as having va_list type. */
5086 : 49913 : tree canon_expr_type
5087 : 49913 : = targetm.canonical_va_list_type (TREE_TYPE (expr));
5088 : 49913 : gcc_assert (canon_expr_type != NULL_TREE);
5089 : : }
5090 : : else
5091 : : {
5092 : : /* Case 2b: va_list is pointer to array elem type. */
5093 : 747 : gcc_assert (POINTER_TYPE_P (va_type));
5094 : :
5095 : : /* Comparison as in std_canonical_va_list_type. */
5096 : 747 : gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (va_type))
5097 : : == TYPE_MAIN_VARIANT (TREE_TYPE (canon_va_type)));
5098 : :
5099 : : /* Don't take the address. We've already got '&ap'. */
5100 : 50660 : ;
5101 : : }
5102 : :
5103 : 50660 : return build_va_arg_1 (loc, type, expr);
5104 : : }
5105 : :
5106 : :
5107 : : /* Linked list of disabled built-in functions. */
5108 : :
5109 : : struct disabled_builtin
5110 : : {
5111 : : const char *name;
5112 : : struct disabled_builtin *next;
5113 : : };
5114 : : static disabled_builtin *disabled_builtins = NULL;
5115 : :
5116 : : static bool builtin_function_disabled_p (const char *);
5117 : :
5118 : : /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5119 : : begins with "__builtin_", give an error. */
5120 : :
5121 : : void
5122 : 308 : disable_builtin_function (const char *name)
5123 : : {
5124 : 308 : if (startswith (name, "__builtin_"))
5125 : 0 : error ("cannot disable built-in function %qs", name);
5126 : : else
5127 : : {
5128 : 308 : disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5129 : 308 : new_disabled_builtin->name = name;
5130 : 308 : new_disabled_builtin->next = disabled_builtins;
5131 : 308 : disabled_builtins = new_disabled_builtin;
5132 : : }
5133 : 308 : }
5134 : :
5135 : :
5136 : : /* Return true if the built-in function NAME has been disabled, false
5137 : : otherwise. */
5138 : :
5139 : : static bool
5140 : 162774590 : builtin_function_disabled_p (const char *name)
5141 : : {
5142 : 162774590 : disabled_builtin *p;
5143 : 163059606 : for (p = disabled_builtins; p != NULL; p = p->next)
5144 : : {
5145 : 285324 : if (strcmp (name, p->name) == 0)
5146 : : return true;
5147 : : }
5148 : : return false;
5149 : : }
5150 : :
5151 : :
5152 : : /* Worker for DEF_BUILTIN.
5153 : : Possibly define a builtin function with one or two names.
5154 : : Does not declare a non-__builtin_ function if flag_no_builtin, or if
5155 : : nonansi_p and flag_no_nonansi_builtin. */
5156 : :
5157 : : static void
5158 : 318918219 : def_builtin_1 (enum built_in_function fncode,
5159 : : const char *name,
5160 : : enum built_in_class fnclass,
5161 : : tree fntype, tree libtype,
5162 : : bool both_p, bool fallback_p, bool nonansi_p,
5163 : : tree fnattrs, bool implicit_p)
5164 : : {
5165 : 318918219 : tree decl;
5166 : 318918219 : const char *libname;
5167 : :
5168 : 318918219 : if (fntype == error_mark_node)
5169 : : return;
5170 : :
5171 : 300596449 : gcc_assert ((!both_p && !fallback_p)
5172 : : || startswith (name, "__builtin_"));
5173 : :
5174 : 300596449 : libname = name + strlen ("__builtin_");
5175 : 300596449 : decl = add_builtin_function (name, fntype, fncode, fnclass,
5176 : : (fallback_p ? libname : NULL),
5177 : : fnattrs);
5178 : :
5179 : 300596449 : set_builtin_decl (fncode, decl, implicit_p);
5180 : :
5181 : 300596449 : if (both_p
5182 : 163202867 : && !flag_no_builtin && !builtin_function_disabled_p (libname)
5183 : 463370731 : && !(nonansi_p && flag_no_nonansi_builtin))
5184 : 146756289 : add_builtin_function (libname, libtype, fncode, fnclass,
5185 : : NULL, fnattrs);
5186 : : }
5187 : :
5188 : : /* Nonzero if the type T promotes to int. This is (nearly) the
5189 : : integral promotions defined in ISO C99 6.3.1.1/2. */
5190 : :
5191 : : bool
5192 : 517036700 : c_promoting_integer_type_p (const_tree t)
5193 : : {
5194 : 517036700 : switch (TREE_CODE (t))
5195 : : {
5196 : 404039970 : case INTEGER_TYPE:
5197 : 404039970 : return (TYPE_MAIN_VARIANT (t) == char_type_node
5198 : 398122474 : || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5199 : 397672862 : || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5200 : 382795108 : || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5201 : 381738883 : || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5202 : 780211017 : || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5203 : :
5204 : 673045 : case ENUMERAL_TYPE:
5205 : : /* ??? Technically all enumerations not larger than an int
5206 : : promote to an int. But this is used along code paths
5207 : : that only want to notice a size change. */
5208 : 673045 : return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5209 : :
5210 : : case BOOLEAN_TYPE:
5211 : : return true;
5212 : :
5213 : : default:
5214 : : return false;
5215 : : }
5216 : : }
5217 : :
5218 : : /* Return 1 if PARMS specifies a fixed number of parameters
5219 : : and none of their types is affected by default promotions. */
5220 : :
5221 : : bool
5222 : 21857 : self_promoting_args_p (const_tree parms)
5223 : : {
5224 : 21857 : const_tree t;
5225 : 65069 : for (t = parms; t; t = TREE_CHAIN (t))
5226 : : {
5227 : 43745 : tree type = TREE_VALUE (t);
5228 : :
5229 : 43745 : if (type == error_mark_node)
5230 : 2 : continue;
5231 : :
5232 : 43743 : if (TREE_CHAIN (t) == NULL_TREE && type != void_type_node)
5233 : : return false;
5234 : :
5235 : 43260 : if (type == NULL_TREE)
5236 : : return false;
5237 : :
5238 : 43260 : if (TYPE_MAIN_VARIANT (type) == float_type_node)
5239 : : return false;
5240 : :
5241 : 43215 : if (c_promoting_integer_type_p (type))
5242 : : return false;
5243 : : }
5244 : : return true;
5245 : : }
5246 : :
5247 : : /* Recursively remove any '*' or '&' operator from TYPE. */
5248 : : tree
5249 : 38623428 : strip_pointer_operator (tree t)
5250 : : {
5251 : 40816127 : while (POINTER_TYPE_P (t))
5252 : 2192699 : t = TREE_TYPE (t);
5253 : 38623428 : return t;
5254 : : }
5255 : :
5256 : : /* Recursively remove pointer or array type from TYPE. */
5257 : : tree
5258 : 10942452 : strip_pointer_or_array_types (tree t)
5259 : : {
5260 : 15824183 : while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5261 : 4881731 : t = TREE_TYPE (t);
5262 : 10942452 : return t;
5263 : : }
5264 : :
5265 : : /* Used to compare case labels. K1 and K2 are actually tree nodes
5266 : : representing case labels, or NULL_TREE for a `default' label.
5267 : : Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5268 : : K2, and 0 if K1 and K2 are equal. */
5269 : :
5270 : : int
5271 : 28933187 : case_compare (splay_tree_key k1, splay_tree_key k2)
5272 : : {
5273 : : /* Consider a NULL key (such as arises with a `default' label) to be
5274 : : smaller than anything else. */
5275 : 28933187 : if (!k1)
5276 : 3914399 : return k2 ? -1 : 0;
5277 : 25724626 : else if (!k2)
5278 : : return k1 ? 1 : 0;
5279 : :
5280 : 24873245 : return tree_int_cst_compare ((tree) k1, (tree) k2);
5281 : : }
5282 : :
5283 : : /* Process a case label, located at LOC, for the range LOW_VALUE
5284 : : ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5285 : : then this case label is actually a `default' label. If only
5286 : : HIGH_VALUE is NULL_TREE, then case label was declared using the
5287 : : usual C/C++ syntax, rather than the GNU case range extension.
5288 : : CASES is a tree containing all the case ranges processed so far;
5289 : : COND is the condition for the switch-statement itself.
5290 : : Returns the CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no
5291 : : CASE_LABEL_EXPR is created. ATTRS are the attributes to be applied
5292 : : to the label. */
5293 : :
5294 : : tree
5295 : 3154758 : c_add_case_label (location_t loc, splay_tree cases, tree cond,
5296 : : tree low_value, tree high_value, tree attrs)
5297 : : {
5298 : 3154758 : tree type;
5299 : 3154758 : tree label;
5300 : 3154758 : tree case_label;
5301 : 3154758 : splay_tree_node node;
5302 : :
5303 : : /* Create the LABEL_DECL itself. */
5304 : 3154758 : label = create_artificial_label (loc);
5305 : 3154758 : decl_attributes (&label, attrs, 0);
5306 : :
5307 : : /* If there was an error processing the switch condition, bail now
5308 : : before we get more confused. */
5309 : 3154758 : if (!cond || cond == error_mark_node)
5310 : 0 : goto error_out;
5311 : :
5312 : 2835167 : if ((low_value && TREE_TYPE (low_value)
5313 : 2835167 : && POINTER_TYPE_P (TREE_TYPE (low_value)))
5314 : 5989924 : || (high_value && TREE_TYPE (high_value)
5315 : 440 : && POINTER_TYPE_P (TREE_TYPE (high_value))))
5316 : : {
5317 : 1 : error_at (loc, "pointers are not permitted as case values");
5318 : 1 : goto error_out;
5319 : : }
5320 : :
5321 : : /* Case ranges are a GNU extension. */
5322 : 3154757 : if (high_value)
5323 : : {
5324 : 440 : if (c_dialect_cxx ())
5325 : 126 : pedwarn (loc, OPT_Wpedantic,
5326 : : "range expressions in switch statements are non-standard");
5327 : 314 : else if (warn_c23_c2y_compat > 0)
5328 : : {
5329 : 13 : if (pedantic && !flag_isoc2y)
5330 : 0 : pedwarn (loc, OPT_Wc23_c2y_compat,
5331 : : "ISO C does not support range expressions in switch "
5332 : : "statements before C2Y");
5333 : : else
5334 : 13 : warning_at (loc, OPT_Wc23_c2y_compat,
5335 : : "ISO C does not support range expressions in switch "
5336 : : "statements before C2Y");
5337 : : }
5338 : 301 : else if (warn_c23_c2y_compat && pedantic && !flag_isoc2y)
5339 : 28 : pedwarn (loc, OPT_Wpedantic,
5340 : : "ISO C does not support range expressions in switch "
5341 : : "statements before C2Y");
5342 : : }
5343 : :
5344 : 3154757 : type = TREE_TYPE (cond);
5345 : 3154757 : if (low_value)
5346 : : {
5347 : 2835166 : low_value = check_case_value (loc, low_value);
5348 : 2835166 : tree tem = NULL_TREE;
5349 : 2835166 : if (high_value
5350 : 440 : && !c_dialect_cxx ()
5351 : 314 : && low_value != error_mark_node
5352 : 313 : && !int_fits_type_p (low_value, type)
5353 : 2835606 : && pedwarn (loc, OPT_Wpedantic,
5354 : : "conversion of %qE to %qT in range expression changes "
5355 : : "value to %qE", low_value, type,
5356 : 20 : (tem = fold_convert (type, low_value))))
5357 : 20 : low_value = tem;
5358 : 2835166 : low_value = convert_and_check (loc, type, low_value);
5359 : 2835166 : low_value = fold (low_value);
5360 : 2835166 : if (low_value == error_mark_node)
5361 : 147 : goto error_out;
5362 : : }
5363 : 3154610 : if (high_value)
5364 : : {
5365 : 439 : high_value = check_case_value (loc, high_value);
5366 : 439 : tree tem = NULL_TREE;
5367 : 439 : if (!c_dialect_cxx ()
5368 : 313 : && high_value != error_mark_node
5369 : 312 : && !int_fits_type_p (high_value, type)
5370 : 752 : && pedwarn (loc, OPT_Wpedantic,
5371 : : "conversion of %qE to %qT in range expression changes "
5372 : : "value to %qE", high_value, type,
5373 : 20 : (tem = fold_convert (type, high_value))))
5374 : 20 : high_value = tem;
5375 : 439 : high_value = convert_and_check (loc, type, high_value);
5376 : 439 : high_value = fold (high_value);
5377 : 439 : if (high_value == error_mark_node)
5378 : 4 : goto error_out;
5379 : : }
5380 : :
5381 : 3154606 : if (low_value && high_value)
5382 : : {
5383 : : /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5384 : : really a case range, even though it was written that way.
5385 : : Remove the HIGH_VALUE to simplify later processing. */
5386 : 435 : if (tree_int_cst_equal (low_value, high_value))
5387 : : high_value = NULL_TREE;
5388 : 416 : else if (!tree_int_cst_lt (low_value, high_value))
5389 : : {
5390 : 28 : warning_at (loc, 0, "empty range specified");
5391 : 28 : goto error_out;
5392 : : }
5393 : : }
5394 : :
5395 : : /* Look up the LOW_VALUE in the table of case labels we already
5396 : : have. */
5397 : 3154578 : node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5398 : : /* If there was not an exact match, check for overlapping ranges.
5399 : : There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5400 : : that's a `default' label and the only overlap is an exact match. */
5401 : 3154578 : if (!node && (low_value || high_value))
5402 : : {
5403 : 2834969 : splay_tree_node low_bound;
5404 : 2834969 : splay_tree_node high_bound;
5405 : :
5406 : : /* Even though there wasn't an exact match, there might be an
5407 : : overlap between this case range and another case range.
5408 : : Since we've (inductively) not allowed any overlapping case
5409 : : ranges, we simply need to find the greatest low case label
5410 : : that is smaller that LOW_VALUE, and the smallest low case
5411 : : label that is greater than LOW_VALUE. If there is an overlap
5412 : : it will occur in one of these two ranges. */
5413 : 2834969 : low_bound = splay_tree_predecessor (cases,
5414 : : (splay_tree_key) low_value);
5415 : 2834969 : high_bound = splay_tree_successor (cases,
5416 : : (splay_tree_key) low_value);
5417 : :
5418 : : /* Check to see if the LOW_BOUND overlaps. It is smaller than
5419 : : the LOW_VALUE, so there is no need to check unless the
5420 : : LOW_BOUND is in fact itself a case range. */
5421 : 2834969 : if (low_bound
5422 : 1908231 : && CASE_HIGH ((tree) low_bound->value)
5423 : 2835136 : && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5424 : : low_value) >= 0)
5425 : : node = low_bound;
5426 : : /* Check to see if the HIGH_BOUND overlaps. The low end of that
5427 : : range is bigger than the low end of the current range, so we
5428 : : are only interested if the current range is a real range, and
5429 : : not an ordinary case label. */
5430 : 2834957 : else if (high_bound
5431 : 2834957 : && high_value
5432 : 2834957 : && (tree_int_cst_compare ((tree) high_bound->key,
5433 : : high_value)
5434 : : <= 0))
5435 : : node = high_bound;
5436 : : }
5437 : : /* If there was an overlap, issue an error. */
5438 : 3154559 : if (node)
5439 : : {
5440 : 37 : tree duplicate = CASE_LABEL ((tree) node->value);
5441 : :
5442 : 37 : if (high_value)
5443 : : {
5444 : 10 : error_at (loc, "duplicate (or overlapping) case value");
5445 : 10 : inform (DECL_SOURCE_LOCATION (duplicate),
5446 : : "this is the first entry overlapping that value");
5447 : : }
5448 : 27 : else if (low_value)
5449 : : {
5450 : 26 : error_at (loc, "duplicate case value") ;
5451 : 26 : inform (DECL_SOURCE_LOCATION (duplicate), "previously used here");
5452 : : }
5453 : : else
5454 : : {
5455 : 1 : error_at (loc, "multiple default labels in one switch");
5456 : 1 : inform (DECL_SOURCE_LOCATION (duplicate),
5457 : : "this is the first default label");
5458 : : }
5459 : 37 : goto error_out;
5460 : : }
5461 : :
5462 : : /* Add a CASE_LABEL to the statement-tree. */
5463 : 3154541 : case_label = add_stmt (build_case_label (low_value, high_value, label));
5464 : : /* Register this case label in the splay tree. */
5465 : 3154541 : splay_tree_insert (cases,
5466 : : (splay_tree_key) low_value,
5467 : : (splay_tree_value) case_label);
5468 : :
5469 : 3154541 : return case_label;
5470 : :
5471 : 217 : error_out:
5472 : : /* Add a label so that the back-end doesn't think that the beginning of
5473 : : the switch is unreachable. Note that we do not add a case label, as
5474 : : that just leads to duplicates and thence to failure later on. */
5475 : 217 : if (!cases->root)
5476 : : {
5477 : 153 : tree t = create_artificial_label (loc);
5478 : 153 : add_stmt (build_stmt (loc, LABEL_EXPR, t));
5479 : : }
5480 : 217 : return error_mark_node;
5481 : : }
5482 : :
5483 : : /* Subroutine of c_switch_covers_all_cases_p, called via
5484 : : splay_tree_foreach. Return 1 if it doesn't cover all the cases.
5485 : : ARGS[0] is initially NULL and after the first iteration is the
5486 : : so far highest case label. ARGS[1] is the minimum of SWITCH_COND's
5487 : : type. */
5488 : :
5489 : : static int
5490 : 137970 : c_switch_covers_all_cases_p_1 (splay_tree_node node, void *data)
5491 : : {
5492 : 137970 : tree label = (tree) node->value;
5493 : 137970 : tree *args = (tree *) data;
5494 : :
5495 : : /* If there is a default case, we shouldn't have called this. */
5496 : 137970 : gcc_assert (CASE_LOW (label));
5497 : :
5498 : 137970 : if (args[0] == NULL_TREE)
5499 : : {
5500 : 48757 : if (wi::to_widest (args[1]) < wi::to_widest (CASE_LOW (label)))
5501 : : return 1;
5502 : : }
5503 : 89213 : else if (wi::add (wi::to_widest (args[0]), 1)
5504 : 178426 : != wi::to_widest (CASE_LOW (label)))
5505 : : return 1;
5506 : 118707 : if (CASE_HIGH (label))
5507 : 109 : args[0] = CASE_HIGH (label);
5508 : : else
5509 : 118598 : args[0] = CASE_LOW (label);
5510 : : return 0;
5511 : : }
5512 : :
5513 : : /* Return true if switch with CASES and switch condition with type
5514 : : covers all possible values in the case labels. */
5515 : :
5516 : : bool
5517 : 82577 : c_switch_covers_all_cases_p (splay_tree cases, tree type)
5518 : : {
5519 : : /* If there is default:, this is always the case. */
5520 : 82577 : splay_tree_node default_node
5521 : 82577 : = splay_tree_lookup (cases, (splay_tree_key) NULL);
5522 : 82577 : if (default_node)
5523 : : return true;
5524 : :
5525 : 49303 : if (!INTEGRAL_TYPE_P (type))
5526 : : return false;
5527 : :
5528 : 49236 : tree args[2] = { NULL_TREE, TYPE_MIN_VALUE (type) };
5529 : 49236 : if (splay_tree_foreach (cases, c_switch_covers_all_cases_p_1, args))
5530 : : return false;
5531 : :
5532 : : /* If there are no cases at all, or if the highest case label
5533 : : is smaller than TYPE_MAX_VALUE, return false. */
5534 : 29973 : if (args[0] == NULL_TREE
5535 : 29973 : || wi::to_widest (args[0]) < wi::to_widest (TYPE_MAX_VALUE (type)))
5536 : 29927 : return false;
5537 : :
5538 : : return true;
5539 : : }
5540 : :
5541 : : /* Return true if stmt can fall through. Used by block_may_fallthru
5542 : : default case. */
5543 : :
5544 : : bool
5545 : 1860923 : c_block_may_fallthru (const_tree stmt)
5546 : : {
5547 : 1860923 : switch (TREE_CODE (stmt))
5548 : : {
5549 : 277 : case SWITCH_STMT:
5550 : 277 : return (!SWITCH_STMT_ALL_CASES_P (stmt)
5551 : 184 : || !SWITCH_STMT_NO_BREAK_P (stmt)
5552 : 305 : || block_may_fallthru (SWITCH_STMT_BODY (stmt)));
5553 : :
5554 : : default:
5555 : : return true;
5556 : : }
5557 : : }
5558 : :
5559 : : /* Finish an expression taking the address of LABEL (an
5560 : : IDENTIFIER_NODE). Returns an expression for the address.
5561 : :
5562 : : LOC is the location for the expression returned. */
5563 : :
5564 : : tree
5565 : 2122 : finish_label_address_expr (tree label, location_t loc)
5566 : : {
5567 : 2122 : tree result;
5568 : :
5569 : 2122 : pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
5570 : :
5571 : 2122 : if (label == error_mark_node)
5572 : : return error_mark_node;
5573 : :
5574 : 2116 : label = lookup_label (label);
5575 : 2116 : if (label == NULL_TREE)
5576 : 5 : result = null_pointer_node;
5577 : : else
5578 : : {
5579 : 2111 : TREE_USED (label) = 1;
5580 : 2111 : result = build1 (ADDR_EXPR, ptr_type_node, label);
5581 : : /* The current function is not necessarily uninlinable.
5582 : : Computed gotos are incompatible with inlining, but the value
5583 : : here could be used only in a diagnostic, for example. */
5584 : 2111 : protected_set_expr_location (result, loc);
5585 : : }
5586 : :
5587 : : return result;
5588 : : }
5589 : :
5590 : :
5591 : : /* Given a boolean expression ARG, return a tree representing an increment
5592 : : or decrement (as indicated by CODE) of ARG. The front end must check for
5593 : : invalid cases (e.g., decrement in C++). */
5594 : : tree
5595 : 85 : boolean_increment (enum tree_code code, tree arg)
5596 : : {
5597 : 85 : tree val;
5598 : 85 : tree true_res = build_int_cst (TREE_TYPE (arg), 1);
5599 : :
5600 : 85 : arg = stabilize_reference (arg);
5601 : 85 : switch (code)
5602 : : {
5603 : 28 : case PREINCREMENT_EXPR:
5604 : 28 : val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5605 : 28 : break;
5606 : 29 : case POSTINCREMENT_EXPR:
5607 : 29 : val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5608 : 29 : arg = save_expr (arg);
5609 : 29 : val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5610 : 29 : val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5611 : 29 : break;
5612 : 14 : case PREDECREMENT_EXPR:
5613 : 14 : val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5614 : : invert_truthvalue_loc (input_location, arg));
5615 : 14 : break;
5616 : 14 : case POSTDECREMENT_EXPR:
5617 : 14 : val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5618 : : invert_truthvalue_loc (input_location, arg));
5619 : 14 : arg = save_expr (arg);
5620 : 14 : val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5621 : 14 : val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5622 : 14 : break;
5623 : 0 : default:
5624 : 0 : gcc_unreachable ();
5625 : : }
5626 : 85 : TREE_SIDE_EFFECTS (val) = 1;
5627 : 85 : return val;
5628 : : }
5629 : :
5630 : : /* Built-in macros for stddef.h and stdint.h, that require macros
5631 : : defined in this file. */
5632 : : void
5633 : 210466 : c_stddef_cpp_builtins(void)
5634 : : {
5635 : 215911 : builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
5636 : 215911 : builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
5637 : 215911 : builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
5638 : : /* C++ has wchar_t as a builtin type, C doesn't, so if WINT_TYPE
5639 : : maps to wchar_t, define it to the underlying WCHAR_TYPE in C, and
5640 : : to wchar_t in C++, so the desired type equivalence holds. */
5641 : 210466 : if (!c_dialect_cxx ()
5642 : : && strcmp (WINT_TYPE, "wchar_t") == 0)
5643 : : builtin_define_with_value ("__WINT_TYPE__", WCHAR_TYPE, 0);
5644 : : else
5645 : 210466 : builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
5646 : 215911 : builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
5647 : 215911 : builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
5648 : 210466 : if (flag_char8_t)
5649 : 164539 : builtin_define_with_value ("__CHAR8_TYPE__", CHAR8_TYPE, 0);
5650 : 210466 : builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
5651 : 210466 : builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
5652 : 210466 : if (SIG_ATOMIC_TYPE)
5653 : 210466 : builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
5654 : 210466 : if (INT8_TYPE)
5655 : 210466 : builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
5656 : 210466 : if (INT16_TYPE)
5657 : 210466 : builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
5658 : 210466 : if (INT32_TYPE)
5659 : 210466 : builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
5660 : 210466 : if (INT64_TYPE)
5661 : 215911 : builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
5662 : 210466 : if (UINT8_TYPE)
5663 : 210466 : builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
5664 : 210466 : if (UINT16_TYPE)
5665 : 210466 : builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
5666 : 210466 : if (UINT32_TYPE)
5667 : 210466 : builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
5668 : 210466 : if (UINT64_TYPE)
5669 : 215911 : builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
5670 : 210466 : if (INT_LEAST8_TYPE)
5671 : 210466 : builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
5672 : 210466 : if (INT_LEAST16_TYPE)
5673 : 210466 : builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
5674 : 210466 : if (INT_LEAST32_TYPE)
5675 : 210466 : builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
5676 : 210466 : if (INT_LEAST64_TYPE)
5677 : 215911 : builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
5678 : 210466 : if (UINT_LEAST8_TYPE)
5679 : 210466 : builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
5680 : 210466 : if (UINT_LEAST16_TYPE)
5681 : 210466 : builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
5682 : 210466 : if (UINT_LEAST32_TYPE)
5683 : 210466 : builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
5684 : 210466 : if (UINT_LEAST64_TYPE)
5685 : 215911 : builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
5686 : 210466 : if (INT_FAST8_TYPE)
5687 : 210466 : builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
5688 : 210466 : if (INT_FAST16_TYPE)
5689 : 215911 : builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
5690 : 210466 : if (INT_FAST32_TYPE)
5691 : 215911 : builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
5692 : 210466 : if (INT_FAST64_TYPE)
5693 : 215911 : builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
5694 : 210466 : if (UINT_FAST8_TYPE)
5695 : 210466 : builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
5696 : 210466 : if (UINT_FAST16_TYPE)
5697 : 215911 : builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
5698 : 210466 : if (UINT_FAST32_TYPE)
5699 : 215911 : builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
5700 : 210466 : if (UINT_FAST64_TYPE)
5701 : 215911 : builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
5702 : 210466 : if (INTPTR_TYPE)
5703 : 215911 : builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
5704 : 210466 : if (UINTPTR_TYPE)
5705 : 215911 : builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
5706 : : /* GIMPLE FE testcases need access to the GCC internal 'sizetype'.
5707 : : Expose it as __SIZETYPE__. */
5708 : 210466 : if (flag_gimple)
5709 : 294 : builtin_define_with_value ("__SIZETYPE__", SIZETYPE, 0);
5710 : 210466 : }
5711 : :
5712 : : static void
5713 : 210994 : c_init_attributes (void)
5714 : : {
5715 : : /* Fill in the built_in_attributes array. */
5716 : : #define DEF_ATTR_NULL_TREE(ENUM) \
5717 : : built_in_attributes[(int) ENUM] = NULL_TREE;
5718 : : #define DEF_ATTR_INT(ENUM, VALUE) \
5719 : : built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
5720 : : #define DEF_ATTR_STRING(ENUM, VALUE) \
5721 : : built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
5722 : : #define DEF_ATTR_IDENT(ENUM, STRING) \
5723 : : built_in_attributes[(int) ENUM] = get_identifier (STRING);
5724 : : #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5725 : : built_in_attributes[(int) ENUM] \
5726 : : = tree_cons (built_in_attributes[(int) PURPOSE], \
5727 : : built_in_attributes[(int) VALUE], \
5728 : : built_in_attributes[(int) CHAIN]);
5729 : : #include "builtin-attrs.def"
5730 : : #undef DEF_ATTR_NULL_TREE
5731 : : #undef DEF_ATTR_INT
5732 : : #undef DEF_ATTR_IDENT
5733 : : #undef DEF_ATTR_TREE_LIST
5734 : 210994 : }
5735 : :
5736 : : /* Check whether the byte alignment ALIGN is a valid user-specified
5737 : : alignment less than the supported maximum. If so, return ALIGN's
5738 : : base-2 log; if not, output an error and return -1. If OBJFILE
5739 : : then reject alignments greater than MAX_OFILE_ALIGNMENT when
5740 : : converted to bits. Otherwise, consider valid only alignments
5741 : : that are less than HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT.
5742 : : Zero is not considered a valid argument (and results in -1 on
5743 : : return) but it only triggers a warning when WARN_ZERO is set. */
5744 : :
5745 : : int
5746 : 582522 : check_user_alignment (const_tree align, bool objfile, bool warn_zero)
5747 : : {
5748 : 582522 : if (error_operand_p (align))
5749 : : return -1;
5750 : :
5751 : 582499 : if (TREE_CODE (align) != INTEGER_CST
5752 : 582499 : || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
5753 : : {
5754 : 27 : error ("requested alignment is not an integer constant");
5755 : 27 : return -1;
5756 : : }
5757 : :
5758 : 582472 : if (integer_zerop (align))
5759 : : {
5760 : 75 : if (warn_zero)
5761 : 63 : warning (OPT_Wattributes,
5762 : : "requested alignment %qE is not a positive power of 2",
5763 : : align);
5764 : 75 : return -1;
5765 : : }
5766 : :
5767 : : /* Log2 of the byte alignment ALIGN. */
5768 : 582397 : int log2align;
5769 : 582397 : if (tree_int_cst_sgn (align) == -1
5770 : 582397 : || (log2align = tree_log2 (align)) == -1)
5771 : : {
5772 : 36 : error ("requested alignment %qE is not a positive power of 2",
5773 : : align);
5774 : 36 : return -1;
5775 : : }
5776 : :
5777 : 582361 : if (objfile)
5778 : : {
5779 : 15722 : unsigned maxalign = MAX_OFILE_ALIGNMENT / BITS_PER_UNIT;
5780 : 15722 : if (!tree_fits_uhwi_p (align) || tree_to_uhwi (align) > maxalign)
5781 : : {
5782 : 10 : error ("requested alignment %qE exceeds object file maximum %u",
5783 : : align, maxalign);
5784 : 10 : return -1;
5785 : : }
5786 : : }
5787 : :
5788 : 582351 : if (log2align >= HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT)
5789 : : {
5790 : 17 : error ("requested alignment %qE exceeds maximum %u",
5791 : : align, 1U << (HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT - 1));
5792 : 17 : return -1;
5793 : : }
5794 : :
5795 : : return log2align;
5796 : : }
5797 : :
5798 : : /* Determine the ELF symbol visibility for DECL, which is either a
5799 : : variable or a function. It is an error to use this function if a
5800 : : definition of DECL is not available in this translation unit.
5801 : : Returns true if the final visibility has been determined by this
5802 : : function; false if the caller is free to make additional
5803 : : modifications. */
5804 : :
5805 : : bool
5806 : 51800920 : c_determine_visibility (tree decl)
5807 : : {
5808 : 51800920 : gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
5809 : :
5810 : : /* If the user explicitly specified the visibility with an
5811 : : attribute, honor that. DECL_VISIBILITY will have been set during
5812 : : the processing of the attribute. We check for an explicit
5813 : : attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
5814 : : to distinguish the use of an attribute from the use of a "#pragma
5815 : : GCC visibility push(...)"; in the latter case we still want other
5816 : : considerations to be able to overrule the #pragma. */
5817 : 51800920 : if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
5818 : : || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
5819 : : && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
5820 : : || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
5821 : : return true;
5822 : :
5823 : : /* Set default visibility to whatever the user supplied with
5824 : : visibility_specified depending on #pragma GCC visibility. */
5825 : 51168555 : if (!DECL_VISIBILITY_SPECIFIED (decl))
5826 : : {
5827 : 51167309 : if (visibility_options.inpragma
5828 : 51167309 : || DECL_VISIBILITY (decl) != default_visibility)
5829 : : {
5830 : 212390 : DECL_VISIBILITY (decl) = default_visibility;
5831 : 212390 : DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
5832 : : /* If visibility changed and DECL already has DECL_RTL, ensure
5833 : : symbol flags are updated. */
5834 : 14336 : if (((VAR_P (decl) && TREE_STATIC (decl))
5835 : 212338 : || TREE_CODE (decl) == FUNCTION_DECL)
5836 : 410496 : && DECL_RTL_SET_P (decl))
5837 : 0 : make_decl_rtl (decl);
5838 : : }
5839 : : }
5840 : : return false;
5841 : : }
5842 : :
5843 : : /* Data to communicate through check_function_arguments_recurse between
5844 : : check_function_nonnull and check_nonnull_arg. */
5845 : :
5846 : : struct nonnull_arg_ctx
5847 : : {
5848 : : /* Location of the call. */
5849 : : location_t loc;
5850 : : /* The function whose arguments are being checked and its type (used
5851 : : for calls through function pointers). */
5852 : : const_tree fndecl, fntype;
5853 : : /* For nonnull_if_nonzero, index of the other arguments. */
5854 : : unsigned HOST_WIDE_INT other1, other2;
5855 : : /* True if a warning has been issued. */
5856 : : bool warned_p;
5857 : : };
5858 : :
5859 : : /* Check the argument list of a function call to CTX.FNDECL of CTX.FNTYPE
5860 : : for null in argument slots that are marked as requiring a non-null
5861 : : pointer argument. The NARGS arguments are passed in the array ARGARRAY.
5862 : : Return true if we have warned. */
5863 : :
5864 : : static bool
5865 : 6449253 : check_function_nonnull (nonnull_arg_ctx &ctx, int nargs, tree *argarray)
5866 : : {
5867 : 6449253 : int firstarg = 0;
5868 : 6449253 : if (TREE_CODE (ctx.fntype) == METHOD_TYPE)
5869 : : {
5870 : 495505 : bool closure = false;
5871 : 495505 : if (ctx.fndecl)
5872 : : {
5873 : : /* For certain lambda expressions the C++ front end emits calls
5874 : : that pass a null this pointer as an argument named __closure
5875 : : to the member operator() of empty function. Detect those
5876 : : and avoid checking them, but proceed to check the remaining
5877 : : arguments. */
5878 : 495354 : tree arg0 = DECL_ARGUMENTS (ctx.fndecl);
5879 : 495354 : if (tree arg0name = DECL_NAME (arg0))
5880 : 495354 : closure = id_equal (arg0name, "__closure");
5881 : : }
5882 : :
5883 : : /* In calls to C++ non-static member functions check the this
5884 : : pointer regardless of whether the function is declared with
5885 : : attribute nonnull. */
5886 : 986065 : firstarg = 1;
5887 : 495354 : if (!closure)
5888 : 490711 : check_function_arguments_recurse (check_nonnull_arg, &ctx, argarray[0],
5889 : : firstarg, OPT_Wnonnull);
5890 : : }
5891 : :
5892 : 6449253 : tree attrs = lookup_attribute ("nonnull", TYPE_ATTRIBUTES (ctx.fntype));
5893 : :
5894 : 6449253 : tree a = attrs;
5895 : : /* See if any of the nonnull attributes has no arguments. If so,
5896 : : then every pointer argument is checked (in which case the check
5897 : : for pointer type is done in check_nonnull_arg). */
5898 : 13130431 : while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE)
5899 : 102742 : a = lookup_attribute ("nonnull", TREE_CHAIN (a));
5900 : :
5901 : 6449253 : if (a != NULL_TREE)
5902 : 70350 : for (int i = firstarg; i < nargs; i++)
5903 : 43909 : check_function_arguments_recurse (check_nonnull_arg, &ctx, argarray[i],
5904 : 43909 : i + 1, OPT_Wnonnull);
5905 : 6422812 : else if (attrs)
5906 : : {
5907 : : /* Walk the argument list. If we encounter an argument number we
5908 : : should check for non-null, do it. */
5909 : 345191 : for (int i = firstarg; i < nargs; i++)
5910 : : {
5911 : 146879 : for (a = attrs; ; a = TREE_CHAIN (a))
5912 : : {
5913 : 399920 : a = lookup_attribute ("nonnull", a);
5914 : 399920 : if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
5915 : : break;
5916 : : }
5917 : :
5918 : 253041 : if (a != NULL_TREE)
5919 : 123446 : check_function_arguments_recurse (check_nonnull_arg, &ctx,
5920 : 123446 : argarray[i], i + 1,
5921 : : OPT_Wnonnull);
5922 : 253041 : a = NULL_TREE;
5923 : : }
5924 : : }
5925 : 6449253 : if (a == NULL_TREE)
5926 : 6422812 : for (attrs = TYPE_ATTRIBUTES (ctx.fntype);
5927 : 6480950 : (attrs = lookup_attribute ("nonnull_if_nonzero", attrs));
5928 : 58138 : attrs = TREE_CHAIN (attrs))
5929 : : {
5930 : 58138 : tree args = TREE_VALUE (attrs);
5931 : 58138 : unsigned int idx = TREE_INT_CST_LOW (TREE_VALUE (args)) - 1;
5932 : 58138 : unsigned int idx2
5933 : 58138 : = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args))) - 1;
5934 : 58138 : unsigned int idx3 = idx2;
5935 : 58138 : if (tree chain2 = TREE_CHAIN (TREE_CHAIN (args)))
5936 : 479 : idx3 = TREE_INT_CST_LOW (TREE_VALUE (chain2)) - 1;
5937 : 58138 : if (idx < (unsigned) nargs - firstarg
5938 : 58133 : && idx2 < (unsigned) nargs - firstarg
5939 : 58128 : && idx3 < (unsigned) nargs - firstarg
5940 : 58128 : && INTEGRAL_TYPE_P (TREE_TYPE (argarray[firstarg + idx2]))
5941 : 58055 : && integer_nonzerop (argarray[firstarg + idx2])
5942 : 31856 : && INTEGRAL_TYPE_P (TREE_TYPE (argarray[firstarg + idx3]))
5943 : 89994 : && integer_nonzerop (argarray[firstarg + idx3]))
5944 : : {
5945 : 31764 : ctx.other1 = firstarg + idx2 + 1;
5946 : 31764 : ctx.other2 = firstarg + idx3 + 1;
5947 : 31764 : check_function_arguments_recurse (check_nonnull_arg, &ctx,
5948 : 31764 : argarray[firstarg + idx],
5949 : 31764 : firstarg + idx + 1,
5950 : : OPT_Wnonnull);
5951 : 31764 : ctx.other1 = 0;
5952 : 31764 : ctx.other2 = 0;
5953 : : }
5954 : : }
5955 : 6449253 : return ctx.warned_p;
5956 : : }
5957 : :
5958 : : /* Check that the Nth argument of a function call (counting backwards
5959 : : from the end) is a (pointer)0. The NARGS arguments are passed in the
5960 : : array ARGARRAY. */
5961 : :
5962 : : static void
5963 : 6430156 : check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
5964 : : {
5965 : 6430156 : tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
5966 : :
5967 : 6430156 : if (attr)
5968 : : {
5969 : 112 : int len = 0;
5970 : 112 : int pos = 0;
5971 : 112 : tree sentinel;
5972 : 112 : function_args_iterator iter;
5973 : 112 : tree t;
5974 : :
5975 : : /* Skip over the named arguments. */
5976 : 237 : FOREACH_FUNCTION_ARGS (fntype, t, iter)
5977 : : {
5978 : 125 : if (len == nargs)
5979 : : break;
5980 : 125 : len++;
5981 : : }
5982 : :
5983 : 112 : if (TREE_VALUE (attr))
5984 : : {
5985 : 59 : tree p = TREE_VALUE (TREE_VALUE (attr));
5986 : 59 : pos = TREE_INT_CST_LOW (p);
5987 : : }
5988 : :
5989 : : /* The sentinel must be one of the varargs, i.e.
5990 : : in position >= the number of fixed arguments. */
5991 : 112 : if ((nargs - 1 - pos) < len)
5992 : : {
5993 : 22 : warning (OPT_Wformat_,
5994 : : "not enough variable arguments to fit a sentinel");
5995 : 22 : return;
5996 : : }
5997 : :
5998 : : /* Validate the sentinel. */
5999 : 90 : sentinel = fold_for_warn (argarray[nargs - 1 - pos]);
6000 : 138 : if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
6001 : 42 : || !integer_zerop (sentinel))
6002 : 56 : && TREE_CODE (TREE_TYPE (sentinel)) != NULLPTR_TYPE
6003 : : /* Although __null (in C++) is only an integer we allow it
6004 : : nevertheless, as we are guaranteed that it's exactly
6005 : : as wide as a pointer, and we don't want to force
6006 : : users to cast the NULL they have written there.
6007 : : We warn with -Wstrict-null-sentinel, though. */
6008 : 138 : && (warn_strict_null_sentinel || null_node != sentinel))
6009 : 45 : warning (OPT_Wformat_, "missing sentinel in function call");
6010 : : }
6011 : : }
6012 : :
6013 : : /* Check that the same argument isn't passed to two or more
6014 : : restrict-qualified formal and issue a -Wrestrict warning
6015 : : if it is. Return true if a warning has been issued. */
6016 : :
6017 : : static bool
6018 : 53380301 : check_function_restrict (const_tree fndecl, const_tree fntype,
6019 : : int nargs, tree *unfolded_argarray)
6020 : : {
6021 : 53380301 : int i;
6022 : 53380301 : tree parms = TYPE_ARG_TYPES (fntype);
6023 : :
6024 : : /* Call fold_for_warn on all of the arguments. */
6025 : 53380301 : auto_vec<tree> argarray (nargs);
6026 : 240483544 : for (i = 0; i < nargs; i++)
6027 : 133722942 : argarray.quick_push (fold_for_warn (unfolded_argarray[i]));
6028 : :
6029 : 53380301 : if (fndecl
6030 : 52841331 : && TREE_CODE (fndecl) == FUNCTION_DECL)
6031 : : {
6032 : : /* Avoid diagnosing calls built-ins with a zero size/bound
6033 : : here. They are checked in more detail elsewhere. */
6034 : 52841331 : if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
6035 : 1725566 : && nargs == 3
6036 : 329420 : && TREE_CODE (argarray[2]) == INTEGER_CST
6037 : 53012339 : && integer_zerop (argarray[2]))
6038 : : return false;
6039 : :
6040 : 52815719 : if (DECL_ARGUMENTS (fndecl))
6041 : 53354689 : parms = DECL_ARGUMENTS (fndecl);
6042 : : }
6043 : :
6044 : 187000795 : for (i = 0; i < nargs; i++)
6045 : 133646106 : TREE_VISITED (argarray[i]) = 0;
6046 : :
6047 : : bool warned = false;
6048 : :
6049 : 186119815 : for (i = 0; i < nargs && parms && parms != void_list_node; i++)
6050 : : {
6051 : 132765126 : tree type;
6052 : 132765126 : if (TREE_CODE (parms) == PARM_DECL)
6053 : : {
6054 : 15906175 : type = TREE_TYPE (parms);
6055 : 15906175 : parms = DECL_CHAIN (parms);
6056 : : }
6057 : : else
6058 : : {
6059 : 116858951 : type = TREE_VALUE (parms);
6060 : 116858951 : parms = TREE_CHAIN (parms);
6061 : : }
6062 : 132765126 : if (POINTER_TYPE_P (type)
6063 : 8213595 : && TYPE_RESTRICT (type)
6064 : 133014981 : && !TYPE_READONLY (TREE_TYPE (type)))
6065 : 250568 : warned |= warn_for_restrict (i, argarray.address (), nargs);
6066 : : }
6067 : :
6068 : 187000795 : for (i = 0; i < nargs; i++)
6069 : 133646106 : TREE_VISITED (argarray[i]) = 0;
6070 : :
6071 : : return warned;
6072 : 53380301 : }
6073 : :
6074 : : /* Helper for check_function_nonnull; given a list of operands which
6075 : : must be non-null in ARGS, determine if operand PARAM_NUM should be
6076 : : checked. */
6077 : :
6078 : : static bool
6079 : 270325 : nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
6080 : : {
6081 : 270325 : unsigned HOST_WIDE_INT arg_num = 0;
6082 : :
6083 : 717013 : for (; args; args = TREE_CHAIN (args))
6084 : : {
6085 : 570134 : bool found = get_attribute_operand (TREE_VALUE (args), &arg_num);
6086 : :
6087 : 570134 : gcc_assert (found);
6088 : :
6089 : 570134 : if (arg_num == param_num)
6090 : : return true;
6091 : : }
6092 : : return false;
6093 : : }
6094 : :
6095 : : /* Check that the function argument PARAM (which is operand number
6096 : : PARAM_NUM) is non-null. This is called by check_function_nonnull
6097 : : via check_function_arguments_recurse. */
6098 : :
6099 : : static void
6100 : 688838 : check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
6101 : : {
6102 : 688838 : struct nonnull_arg_ctx *pctx = (struct nonnull_arg_ctx *) ctx;
6103 : :
6104 : : /* Just skip checking the argument if it's not a pointer. This can
6105 : : happen if the "nonnull" attribute was given without an operand
6106 : : list (which means to check every pointer argument). */
6107 : :
6108 : 688838 : tree paramtype = TREE_TYPE (param);
6109 : 688838 : if (TREE_CODE (paramtype) != POINTER_TYPE
6110 : 61395 : && TREE_CODE (paramtype) != NULLPTR_TYPE)
6111 : 688089 : return;
6112 : :
6113 : : /* Diagnose the simple cases of null arguments. */
6114 : 627449 : if (!integer_zerop (fold_for_warn (param)))
6115 : : return;
6116 : :
6117 : 749 : auto_diagnostic_group adg;
6118 : :
6119 : 749 : const location_t loc = EXPR_LOC_OR_LOC (param, pctx->loc);
6120 : :
6121 : 749 : if (TREE_CODE (pctx->fntype) == METHOD_TYPE)
6122 : 109 : --param_num;
6123 : :
6124 : 749 : bool warned;
6125 : 749 : if (param_num == 0)
6126 : : {
6127 : 30 : warned = warning_at (loc, OPT_Wnonnull,
6128 : : "%qs pointer is null", "this");
6129 : 30 : if (warned && pctx->fndecl)
6130 : 27 : inform (DECL_SOURCE_LOCATION (pctx->fndecl),
6131 : : "in a call to non-static member function %qD",
6132 : : pctx->fndecl);
6133 : : }
6134 : : else
6135 : : {
6136 : 719 : if (pctx->other1 && pctx->other2 != pctx->other1)
6137 : 14 : warned = warning_at (loc, OPT_Wnonnull,
6138 : : "argument %u null where non-null expected "
6139 : : "because arguments %u and %u are nonzero",
6140 : : (unsigned) param_num,
6141 : : TREE_CODE (pctx->fntype) == METHOD_TYPE
6142 : 0 : ? (unsigned) pctx->other1 - 1
6143 : : : (unsigned) pctx->other1,
6144 : : TREE_CODE (pctx->fntype) == METHOD_TYPE
6145 : 0 : ? (unsigned) pctx->other2 - 1
6146 : : : (unsigned) pctx->other2);
6147 : 705 : else if (pctx->other1)
6148 : 50 : warned = warning_at (loc, OPT_Wnonnull,
6149 : : "argument %u null where non-null expected "
6150 : : "because argument %u is nonzero",
6151 : : (unsigned) param_num,
6152 : : TREE_CODE (pctx->fntype) == METHOD_TYPE
6153 : 0 : ? (unsigned) pctx->other1 - 1
6154 : : : (unsigned) pctx->other1);
6155 : : else
6156 : 655 : warned = warning_at (loc, OPT_Wnonnull,
6157 : : "argument %u null where non-null expected",
6158 : : (unsigned) param_num);
6159 : 719 : if (warned && pctx->fndecl)
6160 : 688 : inform (DECL_SOURCE_LOCATION (pctx->fndecl),
6161 : : "in a call to function %qD declared %qs",
6162 : : pctx->fndecl,
6163 : 688 : pctx->other1 ? "nonnull_if_nonzero" : "nonnull");
6164 : : }
6165 : :
6166 : 746 : if (warned)
6167 : 746 : pctx->warned_p = true;
6168 : 749 : }
6169 : :
6170 : : /* Helper for attribute handling; fetch the operand number from
6171 : : the attribute argument list. */
6172 : :
6173 : : bool
6174 : 570134 : get_attribute_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
6175 : : {
6176 : : /* Verify the arg number is a small constant. */
6177 : 570134 : if (tree_fits_uhwi_p (arg_num_expr))
6178 : : {
6179 : 570134 : *valp = tree_to_uhwi (arg_num_expr);
6180 : 570134 : return true;
6181 : : }
6182 : : else
6183 : : return false;
6184 : : }
6185 : :
6186 : : /* Arguments being collected for optimization. */
6187 : : typedef const char *const_char_p; /* For DEF_VEC_P. */
6188 : : static GTY(()) vec<const_char_p, va_gc> *optimize_args;
6189 : :
6190 : :
6191 : : /* Inner function to convert a TREE_LIST to argv string to parse the optimize
6192 : : options in ARGS. ATTR_P is true if this is for attribute(optimize), and
6193 : : false for #pragma GCC optimize. */
6194 : :
6195 : : bool
6196 : 343970 : parse_optimize_options (tree args, bool attr_p)
6197 : : {
6198 : 343970 : bool ret = true;
6199 : 343970 : unsigned opt_argc;
6200 : 343970 : unsigned i;
6201 : 343970 : const char **opt_argv;
6202 : 343970 : struct cl_decoded_option *decoded_options;
6203 : 343970 : unsigned int decoded_options_count;
6204 : 343970 : tree ap;
6205 : :
6206 : : /* Build up argv vector. Just in case the string is stored away, use garbage
6207 : : collected strings. */
6208 : 343970 : vec_safe_truncate (optimize_args, 0);
6209 : 343970 : vec_safe_push (optimize_args, (const char *) NULL);
6210 : :
6211 : 706639 : for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
6212 : : {
6213 : 362669 : tree value = TREE_VALUE (ap);
6214 : :
6215 : 362669 : if (TREE_CODE (value) == INTEGER_CST)
6216 : : {
6217 : 305 : char buffer[HOST_BITS_PER_LONG / 3 + 4];
6218 : 305 : sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
6219 : 305 : vec_safe_push (optimize_args, ggc_strdup (buffer));
6220 : : }
6221 : :
6222 : 362364 : else if (TREE_CODE (value) == STRING_CST)
6223 : : {
6224 : : /* Split string into multiple substrings. */
6225 : 362360 : size_t len = TREE_STRING_LENGTH (value);
6226 : 362360 : char *p = ASTRDUP (TREE_STRING_POINTER (value));
6227 : 362360 : char *end = p + len;
6228 : 362360 : char *comma;
6229 : 362360 : char *next_p = p;
6230 : :
6231 : 724769 : while (next_p != NULL)
6232 : : {
6233 : 362409 : size_t len2;
6234 : 362409 : char *q, *r;
6235 : :
6236 : 362409 : p = next_p;
6237 : 362409 : comma = strchr (p, ',');
6238 : 362409 : if (comma)
6239 : : {
6240 : 49 : len2 = comma - p;
6241 : 49 : *comma = '\0';
6242 : 49 : next_p = comma+1;
6243 : : }
6244 : : else
6245 : : {
6246 : 362360 : len2 = end - p;
6247 : 362360 : next_p = NULL;
6248 : : }
6249 : :
6250 : : /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
6251 : : options. */
6252 : 362409 : if (*p == '-' && p[1] != 'O' && p[1] != 'f')
6253 : : {
6254 : 0 : ret = false;
6255 : 0 : if (attr_p)
6256 : : {
6257 : 0 : auto_urlify_attributes sentinel;
6258 : 0 : warning (OPT_Wattributes,
6259 : : "bad option %qs to attribute %<optimize%>", p);
6260 : 0 : }
6261 : : else
6262 : 0 : warning (OPT_Wpragmas,
6263 : : "bad option %qs to pragma %<optimize%>", p);
6264 : 0 : continue;
6265 : 0 : }
6266 : :
6267 : : /* Can't use GC memory here, see PR88007. */
6268 : 362409 : r = q = XOBNEWVEC (&opts_obstack, char, len2 + 3);
6269 : :
6270 : 362409 : if (*p != '-')
6271 : : {
6272 : 42772 : *r++ = '-';
6273 : :
6274 : : /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
6275 : : itself is -Os, and any other switch begins with a -f. */
6276 : 42772 : if ((*p >= '0' && *p <= '9')
6277 : 42615 : || (p[0] == 's' && p[1] == '\0'))
6278 : 157 : *r++ = 'O';
6279 : 42615 : else if (*p != 'O')
6280 : 23322 : *r++ = 'f';
6281 : : }
6282 : :
6283 : 362409 : memcpy (r, p, len2);
6284 : 362409 : r[len2] = '\0';
6285 : 362409 : vec_safe_push (optimize_args, (const char *) q);
6286 : : }
6287 : :
6288 : : }
6289 : : }
6290 : :
6291 : 343970 : opt_argc = optimize_args->length ();
6292 : 343970 : opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
6293 : :
6294 : 706684 : for (i = 1; i < opt_argc; i++)
6295 : 362714 : opt_argv[i] = (*optimize_args)[i];
6296 : :
6297 : : /* Now parse the options. */
6298 : 343970 : decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
6299 : : &decoded_options,
6300 : : &decoded_options_count);
6301 : : /* Drop non-Optimization options. */
6302 : 343970 : unsigned j = 1;
6303 : 706684 : for (i = 1; i < decoded_options_count; ++i)
6304 : : {
6305 : 362714 : if (! (cl_options[decoded_options[i].opt_index].flags & CL_OPTIMIZATION))
6306 : : {
6307 : 3 : ret = false;
6308 : 3 : if (attr_p)
6309 : : {
6310 : 2 : auto_urlify_attributes sentinel;
6311 : 2 : warning (OPT_Wattributes,
6312 : : "bad option %qs to attribute %<optimize%>",
6313 : 2 : decoded_options[i].orig_option_with_args_text);
6314 : 2 : }
6315 : : else
6316 : 1 : warning (OPT_Wpragmas,
6317 : : "bad option %qs to pragma %<optimize%>",
6318 : : decoded_options[i].orig_option_with_args_text);
6319 : 3 : continue;
6320 : 3 : }
6321 : 362711 : if (i != j)
6322 : 0 : decoded_options[j] = decoded_options[i];
6323 : 362711 : j++;
6324 : : }
6325 : 343970 : decoded_options_count = j;
6326 : :
6327 : : /* Merge the decoded options with save_decoded_options. */
6328 : 343970 : unsigned save_opt_count = save_opt_decoded_options->length ();
6329 : 343970 : unsigned merged_decoded_options_count
6330 : : = save_opt_count + decoded_options_count;
6331 : 343970 : cl_decoded_option *merged_decoded_options
6332 : 343970 : = XNEWVEC (cl_decoded_option, merged_decoded_options_count);
6333 : :
6334 : : /* Note the first decoded_options is used for the program name. */
6335 : 1972668 : for (unsigned i = 0; i < save_opt_count; ++i)
6336 : 1628698 : merged_decoded_options[i + 1] = (*save_opt_decoded_options)[i];
6337 : 706681 : for (unsigned i = 1; i < decoded_options_count; ++i)
6338 : 362711 : merged_decoded_options[save_opt_count + i] = decoded_options[i];
6339 : :
6340 : : /* And apply them. */
6341 : 343970 : decode_options (&global_options, &global_options_set,
6342 : : merged_decoded_options, merged_decoded_options_count,
6343 : : input_location, global_dc, NULL);
6344 : 343970 : free (decoded_options);
6345 : :
6346 : 343970 : targetm.override_options_after_change();
6347 : :
6348 : 343970 : optimize_args->truncate (0);
6349 : 343970 : return ret;
6350 : : }
6351 : :
6352 : : /* Check whether ATTR is a valid attribute fallthrough. */
6353 : :
6354 : : bool
6355 : 274578229 : attribute_fallthrough_p (tree attr)
6356 : : {
6357 : 274578229 : if (attr == error_mark_node)
6358 : : return false;
6359 : 274578211 : tree t = lookup_attribute ("", "fallthrough", attr);
6360 : 274578211 : if (t == NULL_TREE)
6361 : : return false;
6362 : 19989 : auto_urlify_attributes sentinel;
6363 : : /* It is no longer true that "this attribute shall appear at most once in
6364 : : each attribute-list", but we still give a warning. */
6365 : 19989 : if (lookup_attribute ("", "fallthrough", TREE_CHAIN (t)))
6366 : 9 : warning (OPT_Wattributes, "attribute %<fallthrough%> specified multiple "
6367 : : "times");
6368 : : /* No attribute-argument-clause shall be present. */
6369 : 19980 : else if (TREE_VALUE (t) != NULL_TREE)
6370 : 4 : warning (OPT_Wattributes, "%<fallthrough%> attribute specified with "
6371 : : "a parameter");
6372 : : /* Warn if other attributes are found. */
6373 : 40004 : for (t = attr; t != NULL_TREE; t = TREE_CHAIN (t))
6374 : : {
6375 : 20015 : tree name = get_attribute_name (t);
6376 : 20015 : if (!is_attribute_p ("fallthrough", name)
6377 : 20015 : || !is_attribute_namespace_p ("", t))
6378 : : {
6379 : 17 : if (!c_dialect_cxx () && get_attribute_namespace (t) == NULL_TREE)
6380 : : /* The specifications of standard attributes in C mean
6381 : : this is a constraint violation. */
6382 : 3 : pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
6383 : : get_attribute_name (t));
6384 : : else
6385 : 14 : warning (OPT_Wattributes, "%qE attribute ignored", name);
6386 : : }
6387 : : }
6388 : 19989 : return true;
6389 : 19989 : }
6390 : :
6391 : :
6392 : : /* Check for valid arguments being passed to a function with FNTYPE.
6393 : : There are NARGS arguments in the array ARGARRAY. LOC should be used
6394 : : for diagnostics. Return true if either -Wnonnull or -Wrestrict has
6395 : : been issued.
6396 : :
6397 : : The arguments in ARGARRAY may not have been folded yet (e.g. for C++,
6398 : : to preserve location wrappers); checks that require folded arguments
6399 : : should call fold_for_warn on them.
6400 : :
6401 : : Use the frontend-supplied COMP_TYPES when determining if
6402 : : one type is a subclass of another. */
6403 : :
6404 : : bool
6405 : 54064111 : check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype,
6406 : : int nargs, tree *argarray, vec<location_t> *arglocs,
6407 : : bool (*comp_types) (tree, tree))
6408 : : {
6409 : 54064111 : bool warned_p = false;
6410 : :
6411 : 54064111 : if (c_inhibit_evaluation_warnings)
6412 : : return warned_p;
6413 : :
6414 : : /* Check for null being passed in a pointer argument that must be
6415 : : non-null. In C++, this includes the this pointer. We also need
6416 : : to do this if format checking is enabled. */
6417 : 53385034 : if (warn_nonnull)
6418 : : {
6419 : 6449253 : nonnull_arg_ctx ctx = { loc, fndecl, fntype, 0, 0, false };
6420 : 6449253 : warned_p = check_function_nonnull (ctx, nargs, argarray);
6421 : : }
6422 : :
6423 : : /* Check for errors in format strings. */
6424 : :
6425 : 53385034 : if (warn_format || warn_suggest_attribute_format)
6426 : 6507517 : check_function_format (fndecl ? fndecl : fntype, TYPE_ATTRIBUTES (fntype), nargs,
6427 : : argarray, arglocs, comp_types);
6428 : :
6429 : 53385034 : if (warn_format)
6430 : 6430156 : check_function_sentinel (fntype, nargs, argarray);
6431 : :
6432 : 53385034 : if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
6433 : : {
6434 : 1730299 : switch (DECL_FUNCTION_CODE (fndecl))
6435 : : {
6436 : : case BUILT_IN_SPRINTF:
6437 : : case BUILT_IN_SPRINTF_CHK:
6438 : : case BUILT_IN_SNPRINTF:
6439 : : case BUILT_IN_SNPRINTF_CHK:
6440 : : /* Let the sprintf pass handle these. */
6441 : : return warned_p;
6442 : :
6443 : : default:
6444 : : break;
6445 : : }
6446 : : }
6447 : :
6448 : : /* check_function_restrict sets the DECL_READ_P for arguments
6449 : : so it must be called unconditionally. */
6450 : 53380301 : warned_p |= check_function_restrict (fndecl, fntype, nargs, argarray);
6451 : :
6452 : 53380301 : return warned_p;
6453 : : }
6454 : :
6455 : : /* Generic argument checking recursion routine. PARAM is the argument to
6456 : : be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
6457 : : once the argument is resolved. CTX is context for the callback.
6458 : : OPT is the warning for which this is done. */
6459 : : void
6460 : 744990 : check_function_arguments_recurse (void (*callback)
6461 : : (void *, tree, unsigned HOST_WIDE_INT),
6462 : : void *ctx, tree param,
6463 : : unsigned HOST_WIDE_INT param_num,
6464 : : opt_code opt)
6465 : : {
6466 : 1163365 : if (opt != OPT_Wformat_ && warning_suppressed_p (param))
6467 : : return;
6468 : :
6469 : 794405 : if (CONVERT_EXPR_P (param)
6470 : 1287263 : && (TYPE_PRECISION (TREE_TYPE (param))
6471 : 492858 : == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
6472 : : {
6473 : : /* Strip coercion. */
6474 : 835220 : check_function_arguments_recurse (callback, ctx,
6475 : 417610 : TREE_OPERAND (param, 0), param_num,
6476 : : opt);
6477 : 417610 : return;
6478 : : }
6479 : :
6480 : 744054 : if (TREE_CODE (param) == CALL_EXPR && CALL_EXPR_FN (param))
6481 : : {
6482 : 8195 : tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
6483 : 8195 : tree attrs;
6484 : 8195 : bool found_format_arg = false;
6485 : :
6486 : : /* See if this is a call to a known internationalization function
6487 : : that modifies a format arg. Such a function may have multiple
6488 : : format_arg attributes (for example, ngettext). */
6489 : :
6490 : 8195 : for (attrs = TYPE_ATTRIBUTES (type);
6491 : 8577 : attrs;
6492 : 382 : attrs = TREE_CHAIN (attrs))
6493 : 382 : if (is_attribute_p ("format_arg", get_attribute_name (attrs)))
6494 : : {
6495 : 226 : tree inner_arg;
6496 : 226 : tree format_num_expr;
6497 : 226 : int format_num;
6498 : 226 : int i;
6499 : 226 : call_expr_arg_iterator iter;
6500 : :
6501 : : /* Extract the argument number, which was previously checked
6502 : : to be valid. */
6503 : 226 : format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
6504 : :
6505 : 226 : format_num = tree_to_uhwi (format_num_expr);
6506 : :
6507 : 226 : for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
6508 : 320 : inner_arg != NULL_TREE;
6509 : 94 : inner_arg = next_call_expr_arg (&iter), i++)
6510 : 320 : if (i == format_num)
6511 : : {
6512 : 226 : check_function_arguments_recurse (callback, ctx,
6513 : : inner_arg, param_num,
6514 : : opt);
6515 : 226 : found_format_arg = true;
6516 : 226 : break;
6517 : : }
6518 : : }
6519 : :
6520 : : /* If we found a format_arg attribute and did a recursive check,
6521 : : we are done with checking this argument. Otherwise, we continue
6522 : : and this will be considered a non-literal. */
6523 : 8195 : if (found_format_arg)
6524 : : return;
6525 : : }
6526 : :
6527 : 743876 : if (TREE_CODE (param) == COND_EXPR)
6528 : : {
6529 : : /* Simplify to avoid warning for an impossible case. */
6530 : 783 : param = fold_for_warn (param);
6531 : 783 : if (TREE_CODE (param) == COND_EXPR)
6532 : : {
6533 : : /* Check both halves of the conditional expression. */
6534 : 765 : check_function_arguments_recurse (callback, ctx,
6535 : 765 : TREE_OPERAND (param, 1),
6536 : : param_num, opt);
6537 : 1530 : check_function_arguments_recurse (callback, ctx,
6538 : 765 : TREE_OPERAND (param, 2),
6539 : : param_num, opt);
6540 : 765 : return;
6541 : : }
6542 : : }
6543 : :
6544 : 743111 : (*callback) (ctx, param, param_num);
6545 : : }
6546 : :
6547 : : /* Checks for a builtin function FNDECL that the number of arguments
6548 : : NARGS against the required number REQUIRED and issues an error if
6549 : : there is a mismatch. Returns true if the number of arguments is
6550 : : correct, otherwise false. LOC is the location of FNDECL. */
6551 : :
6552 : : static bool
6553 : 827318 : builtin_function_validate_nargs (location_t loc, tree fndecl, int nargs,
6554 : : int required, bool complain)
6555 : : {
6556 : 827318 : if (nargs < required)
6557 : : {
6558 : 182 : if (complain)
6559 : 143 : error_at (loc, "too few arguments to function %qE", fndecl);
6560 : 182 : return false;
6561 : : }
6562 : 827136 : else if (nargs > required)
6563 : : {
6564 : 138 : if (complain)
6565 : 93 : error_at (loc, "too many arguments to function %qE", fndecl);
6566 : 138 : return false;
6567 : : }
6568 : : return true;
6569 : : }
6570 : :
6571 : : /* Helper macro for check_builtin_function_arguments. */
6572 : : #define ARG_LOCATION(N) \
6573 : : (arg_loc.is_empty () \
6574 : : ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
6575 : : : expansion_point_location (arg_loc[(N)]))
6576 : :
6577 : : /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
6578 : : Returns false if there was an error, otherwise true. LOC is the
6579 : : location of the function; ARG_LOC is a vector of locations of the
6580 : : arguments. If FNDECL is the result of resolving an overloaded
6581 : : target built-in, ORIG_FNDECL is the original function decl,
6582 : : otherwise it is null. */
6583 : :
6584 : : bool
6585 : 40573685 : check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
6586 : : tree fndecl, tree orig_fndecl, int nargs,
6587 : : tree *args, bool complain)
6588 : : {
6589 : 40573685 : if (!fndecl_built_in_p (fndecl))
6590 : : return true;
6591 : :
6592 : 40573685 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6593 : 32900488 : return (!targetm.check_builtin_call
6594 : 32900488 : || targetm.check_builtin_call (loc, arg_loc, fndecl, orig_fndecl,
6595 : : nargs, args, complain));
6596 : :
6597 : 7673197 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_FRONTEND)
6598 : : return true;
6599 : :
6600 : 7640347 : gcc_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL);
6601 : 7640347 : switch (DECL_FUNCTION_CODE (fndecl))
6602 : : {
6603 : 2 : case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX:
6604 : 2 : if (!tree_fits_uhwi_p (args[2]))
6605 : : {
6606 : 0 : if (complain)
6607 : 0 : error_at (
6608 : 0 : ARG_LOCATION (2),
6609 : : "third argument to function %qE must be a constant integer",
6610 : : fndecl);
6611 : 0 : return false;
6612 : : }
6613 : : /* fall through */
6614 : :
6615 : 264 : case BUILT_IN_ALLOCA_WITH_ALIGN:
6616 : 264 : {
6617 : : /* Get the requested alignment (in bits) if it's a constant
6618 : : integer expression. */
6619 : 264 : unsigned HOST_WIDE_INT align
6620 : 264 : = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
6621 : :
6622 : : /* Determine if the requested alignment is a power of 2. */
6623 : 264 : if ((align & (align - 1)))
6624 : : align = 0;
6625 : :
6626 : : /* The maximum alignment in bits corresponding to the same
6627 : : maximum in bytes enforced in check_user_alignment(). */
6628 : 315 : unsigned maxalign = (UINT_MAX >> 1) + 1;
6629 : :
6630 : : /* Reject invalid alignments. */
6631 : 181 : if (align < BITS_PER_UNIT || maxalign < align)
6632 : : {
6633 : 134 : if (complain)
6634 : 169 : error_at (ARG_LOCATION (1),
6635 : : "second argument to function %qE must be a constant "
6636 : : "integer power of 2 between %qi and %qu bits",
6637 : : fndecl, BITS_PER_UNIT, maxalign);
6638 : 134 : return false;
6639 : : }
6640 : : return true;
6641 : : }
6642 : :
6643 : 112461 : case BUILT_IN_CONSTANT_P:
6644 : 112461 : return builtin_function_validate_nargs (loc, fndecl, nargs, 1, complain);
6645 : :
6646 : 286742 : case BUILT_IN_ISFINITE:
6647 : 286742 : case BUILT_IN_ISINF:
6648 : 286742 : case BUILT_IN_ISINF_SIGN:
6649 : 286742 : case BUILT_IN_ISNAN:
6650 : 286742 : case BUILT_IN_ISNORMAL:
6651 : 286742 : case BUILT_IN_ISSIGNALING:
6652 : 286742 : case BUILT_IN_SIGNBIT:
6653 : 286742 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 1, complain))
6654 : : {
6655 : 286707 : if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
6656 : : {
6657 : 37 : if (complain)
6658 : 28 : error_at (ARG_LOCATION (0),
6659 : : "non-floating-point argument in "
6660 : : "call to function %qE",
6661 : : fndecl);
6662 : 37 : return false;
6663 : : }
6664 : : return true;
6665 : : }
6666 : : return false;
6667 : :
6668 : 233001 : case BUILT_IN_ISGREATER:
6669 : 233001 : case BUILT_IN_ISGREATEREQUAL:
6670 : 233001 : case BUILT_IN_ISLESS:
6671 : 233001 : case BUILT_IN_ISLESSEQUAL:
6672 : 233001 : case BUILT_IN_ISLESSGREATER:
6673 : 233001 : case BUILT_IN_ISUNORDERED:
6674 : 233001 : case BUILT_IN_ISEQSIG:
6675 : 233001 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 2, complain))
6676 : : {
6677 : 232966 : enum tree_code code0, code1;
6678 : 232966 : code0 = TREE_CODE (TREE_TYPE (args[0]));
6679 : 232966 : code1 = TREE_CODE (TREE_TYPE (args[1]));
6680 : 232966 : if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
6681 : 154 : || (code0 == REAL_TYPE
6682 : 56 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
6683 : 99 : || ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
6684 : 88 : && code1 == REAL_TYPE)))
6685 : : {
6686 : 39 : if (complain)
6687 : 18 : error_at (loc,
6688 : : "non-floating-point arguments in call to "
6689 : : "function %qE",
6690 : : fndecl);
6691 : 39 : return false;
6692 : : }
6693 : : return true;
6694 : : }
6695 : : return false;
6696 : :
6697 : 39528 : case BUILT_IN_FPCLASSIFY:
6698 : 39528 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 6, complain))
6699 : : {
6700 : 237096 : for (unsigned int i = 0; i < 5; i++)
6701 : 197584 : if (TREE_CODE (args[i]) != INTEGER_CST)
6702 : : {
6703 : 12 : if (complain)
6704 : 12 : error_at (ARG_LOCATION (i),
6705 : : "non-const integer argument %u in "
6706 : : "call to function %qE",
6707 : : i + 1, fndecl);
6708 : 12 : return false;
6709 : : }
6710 : :
6711 : 39512 : if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
6712 : : {
6713 : 2 : if (complain)
6714 : 4 : error_at (ARG_LOCATION (5),
6715 : : "non-floating-point argument in "
6716 : : "call to function %qE",
6717 : : fndecl);
6718 : 2 : return false;
6719 : : }
6720 : : return true;
6721 : : }
6722 : : return false;
6723 : :
6724 : 419 : case BUILT_IN_ASSUME_ALIGNED:
6725 : 818 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 2 + (nargs > 2),
6726 : : complain))
6727 : : {
6728 : 417 : if (nargs >= 3
6729 : 18 : && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE
6730 : 424 : && TREE_CODE (TREE_TYPE (args[2])) != BITINT_TYPE)
6731 : : {
6732 : 6 : if (complain)
6733 : 6 : error_at (ARG_LOCATION (2),
6734 : : "non-integer argument 3 in call to "
6735 : : "function %qE",
6736 : : fndecl);
6737 : 6 : return false;
6738 : : }
6739 : : return true;
6740 : : }
6741 : : return false;
6742 : :
6743 : 83801 : case BUILT_IN_ADD_OVERFLOW:
6744 : 83801 : case BUILT_IN_SUB_OVERFLOW:
6745 : 83801 : case BUILT_IN_MUL_OVERFLOW:
6746 : 83801 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 3, complain))
6747 : : {
6748 : : unsigned i;
6749 : 250947 : for (i = 0; i < 2; i++)
6750 : 167324 : if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
6751 : : {
6752 : 55 : if (complain)
6753 : 77 : error_at (ARG_LOCATION (i),
6754 : : "argument %u in call to function "
6755 : : "%qE does not have integral type",
6756 : : i + 1, fndecl);
6757 : 55 : return false;
6758 : : }
6759 : 83623 : if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
6760 : 83623 : || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args[2]))))
6761 : : {
6762 : 60 : if (complain)
6763 : 75 : error_at (ARG_LOCATION (2),
6764 : : "argument 3 in call to function %qE "
6765 : : "does not have pointer to integral type",
6766 : : fndecl);
6767 : 60 : return false;
6768 : : }
6769 : 83563 : else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == ENUMERAL_TYPE)
6770 : : {
6771 : 16 : if (complain)
6772 : 11 : error_at (ARG_LOCATION (2),
6773 : : "argument 3 in call to function %qE "
6774 : : "has pointer to enumerated type",
6775 : : fndecl);
6776 : 16 : return false;
6777 : : }
6778 : 83547 : else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == BOOLEAN_TYPE)
6779 : : {
6780 : 21 : if (complain)
6781 : 16 : error_at (ARG_LOCATION (2),
6782 : : "argument 3 in call to function %qE "
6783 : : "has pointer to boolean type",
6784 : : fndecl);
6785 : 21 : return false;
6786 : : }
6787 : 83526 : else if (TYPE_READONLY (TREE_TYPE (TREE_TYPE (args[2]))))
6788 : : {
6789 : 30 : if (complain)
6790 : 30 : error_at (ARG_LOCATION (2),
6791 : : "argument %u in call to function %qE "
6792 : : "has pointer to %qs type (%qT)",
6793 : 21 : 3, fndecl, "const", TREE_TYPE (args[2]));
6794 : 30 : return false;
6795 : : }
6796 : 83496 : else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (args[2]))))
6797 : : {
6798 : 6 : if (complain)
6799 : 12 : error_at (ARG_LOCATION (2),
6800 : : "argument %u in call to function %qE "
6801 : : "has pointer to %qs type (%qT)",
6802 : 6 : 3, fndecl, "_Atomic", TREE_TYPE (args[2]));
6803 : 6 : return false;
6804 : : }
6805 : : return true;
6806 : : }
6807 : : return false;
6808 : :
6809 : 41854 : case BUILT_IN_ADD_OVERFLOW_P:
6810 : 41854 : case BUILT_IN_SUB_OVERFLOW_P:
6811 : 41854 : case BUILT_IN_MUL_OVERFLOW_P:
6812 : 41854 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 3, complain))
6813 : : {
6814 : : unsigned i;
6815 : 167205 : for (i = 0; i < 3; i++)
6816 : 125421 : if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
6817 : : {
6818 : 37 : if (complain)
6819 : 35 : error_at (ARG_LOCATION (i),
6820 : : "argument %u in call to function "
6821 : : "%qE does not have integral type",
6822 : : i + 1, fndecl);
6823 : 37 : return false;
6824 : : }
6825 : 41784 : if (TREE_CODE (TREE_TYPE (args[2])) == ENUMERAL_TYPE)
6826 : : {
6827 : 13 : if (complain)
6828 : 5 : error_at (ARG_LOCATION (2),
6829 : : "argument %u in call to function "
6830 : : "%qE has enumerated type",
6831 : : 3, fndecl);
6832 : 13 : return false;
6833 : : }
6834 : 41771 : else if (TREE_CODE (TREE_TYPE (args[2])) == BOOLEAN_TYPE)
6835 : : {
6836 : 13 : if (complain)
6837 : 5 : error_at (ARG_LOCATION (2),
6838 : : "argument %u in call to function "
6839 : : "%qE has boolean type",
6840 : : 3, fndecl);
6841 : 13 : return false;
6842 : : }
6843 : : return true;
6844 : : }
6845 : : return false;
6846 : :
6847 : 10876 : case BUILT_IN_CLEAR_PADDING:
6848 : 10876 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 1, complain))
6849 : : {
6850 : 10861 : if (!POINTER_TYPE_P (TREE_TYPE (args[0])))
6851 : : {
6852 : 7 : if (complain)
6853 : 5 : error_at (ARG_LOCATION (0),
6854 : : "argument %u in call to function "
6855 : : "%qE does not have pointer type",
6856 : : 1, fndecl);
6857 : 7 : return false;
6858 : : }
6859 : 10854 : else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (args[0]))))
6860 : : {
6861 : 11 : if (complain)
6862 : 10 : error_at (ARG_LOCATION (0),
6863 : : "argument %u in call to function "
6864 : : "%qE points to incomplete type",
6865 : : 1, fndecl);
6866 : 11 : return false;
6867 : : }
6868 : 10843 : else if (TYPE_READONLY (TREE_TYPE (TREE_TYPE (args[0]))))
6869 : : {
6870 : 7 : if (complain)
6871 : 5 : error_at (ARG_LOCATION (0),
6872 : : "argument %u in call to function %qE "
6873 : : "has pointer to %qs type (%qT)",
6874 : 4 : 1, fndecl, "const", TREE_TYPE (args[0]));
6875 : 7 : return false;
6876 : : }
6877 : 10836 : else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (args[0]))))
6878 : : {
6879 : 0 : if (complain)
6880 : 0 : error_at (ARG_LOCATION (0),
6881 : : "argument %u in call to function %qE "
6882 : : "has pointer to %qs type (%qT)",
6883 : 0 : 1, fndecl, "_Atomic", TREE_TYPE (args[0]));
6884 : 0 : return false;
6885 : : }
6886 : : return true;
6887 : : }
6888 : : return false;
6889 : :
6890 : 72467 : case BUILT_IN_CLZG:
6891 : 72467 : case BUILT_IN_CTZG:
6892 : 72467 : case BUILT_IN_CLRSBG:
6893 : 72467 : case BUILT_IN_FFSG:
6894 : 72467 : case BUILT_IN_PARITYG:
6895 : 72467 : case BUILT_IN_POPCOUNTG:
6896 : 72467 : if (nargs == 2
6897 : 72467 : && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CLZG
6898 : 17157 : || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CTZG))
6899 : : {
6900 : 53831 : if (!INTEGRAL_TYPE_P (TREE_TYPE (args[1])))
6901 : : {
6902 : 18 : if (complain)
6903 : 15 : error_at (ARG_LOCATION (1),
6904 : : "argument %u in call to function "
6905 : : "%qE does not have integral type",
6906 : : 2, fndecl);
6907 : 18 : return false;
6908 : : }
6909 : 53813 : if ((TYPE_PRECISION (TREE_TYPE (args[1]))
6910 : 53813 : > TYPE_PRECISION (integer_type_node))
6911 : 53813 : || (TYPE_PRECISION (TREE_TYPE (args[1]))
6912 : 53799 : == TYPE_PRECISION (integer_type_node)
6913 : 53793 : && TYPE_UNSIGNED (TREE_TYPE (args[1]))))
6914 : : {
6915 : 28 : if (complain)
6916 : 26 : error_at (ARG_LOCATION (1),
6917 : : "argument %u in call to function "
6918 : : "%qE does not have %<int%> type",
6919 : : 2, fndecl);
6920 : 28 : return false;
6921 : : }
6922 : : }
6923 : 18636 : else if (!builtin_function_validate_nargs (loc, fndecl, nargs, 1,
6924 : : complain))
6925 : : return false;
6926 : :
6927 : 72355 : if (!INTEGRAL_TYPE_P (TREE_TYPE (args[0])))
6928 : : {
6929 : 82 : if (complain)
6930 : 80 : error_at (ARG_LOCATION (0),
6931 : : "argument %u in call to function "
6932 : : "%qE does not have integral type",
6933 : : 1, fndecl);
6934 : 82 : return false;
6935 : : }
6936 : 72273 : if (TREE_CODE (TREE_TYPE (args[0])) == ENUMERAL_TYPE)
6937 : : {
6938 : 42 : if (complain)
6939 : 24 : error_at (ARG_LOCATION (0),
6940 : : "argument %u in call to function "
6941 : : "%qE has enumerated type",
6942 : : 1, fndecl);
6943 : 42 : return false;
6944 : : }
6945 : 72231 : if (TREE_CODE (TREE_TYPE (args[0])) == BOOLEAN_TYPE)
6946 : : {
6947 : 50 : if (complain)
6948 : 40 : error_at (ARG_LOCATION (0),
6949 : : "argument %u in call to function "
6950 : : "%qE has boolean type",
6951 : : 1, fndecl);
6952 : 50 : return false;
6953 : : }
6954 : 72181 : if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FFSG
6955 : 72181 : || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CLRSBG)
6956 : : {
6957 : 288 : if (TYPE_UNSIGNED (TREE_TYPE (args[0])))
6958 : : {
6959 : 14 : if (complain)
6960 : 10 : error_at (ARG_LOCATION (0),
6961 : : "argument 1 in call to function "
6962 : : "%qE has unsigned type",
6963 : : fndecl);
6964 : 14 : return false;
6965 : : }
6966 : : }
6967 : 71893 : else if (!TYPE_UNSIGNED (TREE_TYPE (args[0])))
6968 : : {
6969 : 42 : if (complain)
6970 : 42 : error_at (ARG_LOCATION (0),
6971 : : "argument 1 in call to function "
6972 : : "%qE has signed type",
6973 : : fndecl);
6974 : 42 : return false;
6975 : : }
6976 : : return true;
6977 : :
6978 : : default:
6979 : : return true;
6980 : : }
6981 : : }
6982 : :
6983 : : /* Subroutine of c_parse_error.
6984 : : Return the result of concatenating LHS and RHS. RHS is really
6985 : : a string literal, its first character is indicated by RHS_START and
6986 : : RHS_SIZE is its length (including the terminating NUL character).
6987 : :
6988 : : The caller is responsible for deleting the returned pointer. */
6989 : :
6990 : : static char *
6991 : 8817 : catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
6992 : : {
6993 : 8817 : const size_t lhs_size = strlen (lhs);
6994 : 8817 : char *result = XNEWVEC (char, lhs_size + rhs_size);
6995 : 8817 : memcpy (result, lhs, lhs_size);
6996 : 8817 : memcpy (result + lhs_size, rhs_start, rhs_size);
6997 : 8817 : return result;
6998 : : }
6999 : :
7000 : : /* Issue the error given by GMSGID at RICHLOC, indicating that it occurred
7001 : : before TOKEN, which had the associated VALUE. */
7002 : :
7003 : : void
7004 : 8830 : c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
7005 : : tree value, unsigned char token_flags,
7006 : : rich_location *richloc)
7007 : : {
7008 : : #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
7009 : :
7010 : 8830 : char *message = NULL;
7011 : :
7012 : 8830 : if (token_type == CPP_EOF)
7013 : 472 : message = catenate_messages (gmsgid, " at end of input");
7014 : 8358 : else if (token_type == CPP_CHAR
7015 : : || token_type == CPP_WCHAR
7016 : : || token_type == CPP_CHAR16
7017 : : || token_type == CPP_CHAR32
7018 : 8358 : || token_type == CPP_UTF8CHAR)
7019 : : {
7020 : 24 : unsigned int val = TREE_INT_CST_LOW (value);
7021 : 24 : const char *prefix;
7022 : :
7023 : 24 : switch (token_type)
7024 : : {
7025 : : default:
7026 : : prefix = "";
7027 : : break;
7028 : 0 : case CPP_WCHAR:
7029 : 0 : prefix = "L";
7030 : 0 : break;
7031 : 0 : case CPP_CHAR16:
7032 : 0 : prefix = "u";
7033 : 0 : break;
7034 : 0 : case CPP_CHAR32:
7035 : 0 : prefix = "U";
7036 : 0 : break;
7037 : 0 : case CPP_UTF8CHAR:
7038 : 0 : prefix = "u8";
7039 : 0 : break;
7040 : : }
7041 : :
7042 : 24 : if (val <= UCHAR_MAX && ISGRAPH (val))
7043 : 20 : message = catenate_messages (gmsgid, " before %s'%c'");
7044 : : else
7045 : 4 : message = catenate_messages (gmsgid, " before %s'\\x%x'");
7046 : :
7047 : 24 : error_at (richloc, message, prefix, val);
7048 : 24 : free (message);
7049 : 24 : message = NULL;
7050 : : }
7051 : 8334 : else if (token_type == CPP_CHAR_USERDEF
7052 : : || token_type == CPP_WCHAR_USERDEF
7053 : : || token_type == CPP_CHAR16_USERDEF
7054 : : || token_type == CPP_CHAR32_USERDEF
7055 : 8334 : || token_type == CPP_UTF8CHAR_USERDEF)
7056 : 12 : message = catenate_messages (gmsgid,
7057 : : " before user-defined character literal");
7058 : 8322 : else if (token_type == CPP_STRING_USERDEF
7059 : : || token_type == CPP_WSTRING_USERDEF
7060 : : || token_type == CPP_STRING16_USERDEF
7061 : : || token_type == CPP_STRING32_USERDEF
7062 : 8322 : || token_type == CPP_UTF8STRING_USERDEF)
7063 : 25 : message = catenate_messages (gmsgid, " before user-defined string literal");
7064 : 8297 : else if (token_type == CPP_STRING
7065 : : || token_type == CPP_WSTRING
7066 : : || token_type == CPP_STRING16
7067 : : || token_type == CPP_STRING32
7068 : 8297 : || token_type == CPP_UTF8STRING)
7069 : 44 : message = catenate_messages (gmsgid, " before string constant");
7070 : 8253 : else if (token_type == CPP_NUMBER)
7071 : 532 : message = catenate_messages (gmsgid, " before numeric constant");
7072 : 7721 : else if (token_type == CPP_NAME)
7073 : : {
7074 : 2141 : message = catenate_messages (gmsgid, " before %qE");
7075 : 2141 : error_at (richloc, message, value);
7076 : 2141 : free (message);
7077 : 2141 : message = NULL;
7078 : : }
7079 : 5580 : else if (token_type == CPP_PRAGMA)
7080 : 20 : message = catenate_messages (gmsgid, " before %<#pragma%>");
7081 : 5560 : else if (token_type == CPP_PRAGMA_EOL)
7082 : 476 : message = catenate_messages (gmsgid, " before end of line");
7083 : 5084 : else if (token_type == CPP_DECLTYPE)
7084 : 12 : message = catenate_messages (gmsgid, " before %<decltype%>");
7085 : 5072 : else if (token_type == CPP_EMBED)
7086 : 0 : message = catenate_messages (gmsgid, " before %<#embed%>");
7087 : 5072 : else if (token_type < N_TTYPES)
7088 : : {
7089 : 5059 : message = catenate_messages (gmsgid, " before %qs token");
7090 : 5059 : error_at (richloc, message, cpp_type2name (token_type, token_flags));
7091 : 5059 : free (message);
7092 : 5059 : message = NULL;
7093 : : }
7094 : : else
7095 : 13 : error_at (richloc, gmsgid);
7096 : :
7097 : 8830 : if (message)
7098 : : {
7099 : 1593 : error_at (richloc, message);
7100 : 1593 : free (message);
7101 : : }
7102 : : #undef catenate_messages
7103 : 8830 : }
7104 : :
7105 : : /* Return the gcc option code associated with the reason for a cpp
7106 : : message, or 0 if none. */
7107 : :
7108 : : static diagnostics::option_id
7109 : 149247 : c_option_controlling_cpp_diagnostic (enum cpp_warning_reason reason)
7110 : : {
7111 : 149247 : const struct cpp_reason_option_codes_t *entry;
7112 : :
7113 : 4286660 : for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
7114 : : {
7115 : 4279623 : if (entry->reason == reason)
7116 : 142210 : return entry->option_code;
7117 : : }
7118 : 0 : return 0;
7119 : : }
7120 : :
7121 : : /* Return TRUE if the given option index corresponds to a diagnostic
7122 : : issued by libcpp. Linear search seems fine for now. */
7123 : : bool
7124 : 2247577 : c_option_is_from_cpp_diagnostics (int option_index)
7125 : : {
7126 : 52359448 : for (auto entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE;
7127 : : ++entry)
7128 : : {
7129 : 51532738 : if (entry->option_code == option_index)
7130 : : return true;
7131 : : }
7132 : : return false;
7133 : : }
7134 : :
7135 : : /* Callback from cpp_diagnostic for PFILE to print diagnostics from the
7136 : : preprocessor. The diagnostic is of type LEVEL, with REASON set
7137 : : to the reason code if LEVEL is represents a warning, at location
7138 : : RICHLOC unless this is after lexing and the compiler's location
7139 : : should be used instead; MSG is the translated message and AP
7140 : : the arguments. Returns true if a diagnostic was emitted, false
7141 : : otherwise. */
7142 : :
7143 : : bool
7144 : 149257 : c_cpp_diagnostic (cpp_reader *pfile ATTRIBUTE_UNUSED,
7145 : : enum cpp_diagnostic_level level,
7146 : : enum cpp_warning_reason reason,
7147 : : rich_location *richloc,
7148 : : const char *msg, va_list *ap)
7149 : : {
7150 : 149257 : diagnostics::diagnostic_info diagnostic;
7151 : 149257 : enum diagnostics::kind dlevel;
7152 : 149257 : bool save_warn_system_headers = global_dc->m_warn_system_headers;
7153 : 149257 : bool ret;
7154 : :
7155 : 149257 : switch (level)
7156 : : {
7157 : 90 : case CPP_DL_WARNING_SYSHDR:
7158 : 90 : if (flag_no_output)
7159 : : return false;
7160 : 89 : global_dc->m_warn_system_headers = 1;
7161 : : /* Fall through. */
7162 : 26278 : case CPP_DL_WARNING:
7163 : 26278 : if (flag_no_output)
7164 : : return false;
7165 : : dlevel = diagnostics::kind::warning;
7166 : : break;
7167 : 117793 : case CPP_DL_PEDWARN:
7168 : 117793 : if (flag_no_output && !flag_pedantic_errors)
7169 : : return false;
7170 : : dlevel = diagnostics::kind::pedwarn;
7171 : : break;
7172 : : case CPP_DL_ERROR:
7173 : : dlevel = diagnostics::kind::error;
7174 : : break;
7175 : 0 : case CPP_DL_ICE:
7176 : 0 : dlevel = diagnostics::kind::ice;
7177 : 0 : break;
7178 : 603 : case CPP_DL_NOTE:
7179 : 603 : dlevel = diagnostics::kind::note;
7180 : 603 : break;
7181 : 369 : case CPP_DL_FATAL:
7182 : 369 : dlevel = diagnostics::kind::fatal;
7183 : 369 : break;
7184 : 0 : default:
7185 : 0 : gcc_unreachable ();
7186 : : }
7187 : 149247 : if (override_libcpp_locations)
7188 : 239 : richloc->set_range (0, input_location, SHOW_RANGE_WITH_CARET);
7189 : 149247 : diagnostic_set_info_translated (&diagnostic, msg, ap,
7190 : : richloc, dlevel);
7191 : 298494 : diagnostic_set_option_id (&diagnostic,
7192 : : c_option_controlling_cpp_diagnostic (reason));
7193 : 149247 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
7194 : 148878 : if (level == CPP_DL_WARNING_SYSHDR)
7195 : 89 : global_dc->m_warn_system_headers = save_warn_system_headers;
7196 : : return ret;
7197 : 148888 : }
7198 : :
7199 : : /* Convert a character from the host to the target execution character
7200 : : set. cpplib handles this, mostly. */
7201 : :
7202 : : HOST_WIDE_INT
7203 : 6607720 : c_common_to_target_charset (HOST_WIDE_INT c)
7204 : : {
7205 : : /* Character constants in GCC proper are sign-extended under -fsigned-char,
7206 : : zero-extended under -fno-signed-char. cpplib insists that characters
7207 : : and character constants are always unsigned. Hence we must convert
7208 : : back and forth. */
7209 : 6607720 : cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
7210 : :
7211 : 6607720 : uc = cpp_host_to_exec_charset (parse_in, uc);
7212 : :
7213 : 6607720 : if (flag_signed_char)
7214 : 6607623 : return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
7215 : 6607623 : >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
7216 : : else
7217 : 97 : return uc;
7218 : : }
7219 : :
7220 : : /* Fold an offsetof-like expression. EXPR is a nested sequence of component
7221 : : references with an INDIRECT_REF of a constant at the bottom; much like the
7222 : : traditional rendering of offsetof as a macro. TYPE is the desired type of
7223 : : the whole expression. Return the folded result. */
7224 : :
7225 : : tree
7226 : 15128 : fold_offsetof (tree expr, tree type, enum tree_code ctx)
7227 : : {
7228 : 15137 : tree base, off, t;
7229 : 15137 : tree_code code = TREE_CODE (expr);
7230 : 15137 : switch (code)
7231 : : {
7232 : : case ERROR_MARK:
7233 : : return expr;
7234 : :
7235 : 12 : case VAR_DECL:
7236 : 12 : error ("cannot apply %<offsetof%> to static data member %qD", expr);
7237 : 12 : return error_mark_node;
7238 : :
7239 : 9 : case CALL_EXPR:
7240 : 9 : case TARGET_EXPR:
7241 : 9 : error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
7242 : 9 : return error_mark_node;
7243 : :
7244 : 6646 : case NOP_EXPR:
7245 : 6646 : case INDIRECT_REF:
7246 : 6646 : if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
7247 : : {
7248 : 17 : error ("cannot apply %<offsetof%> to a non constant address");
7249 : 17 : return error_mark_node;
7250 : : }
7251 : 6629 : return convert (type, TREE_OPERAND (expr, 0));
7252 : :
7253 : 7234 : case COMPONENT_REF:
7254 : 7234 : base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
7255 : 7234 : if (base == error_mark_node)
7256 : : return base;
7257 : :
7258 : 7231 : t = TREE_OPERAND (expr, 1);
7259 : 7231 : if (DECL_C_BIT_FIELD (t))
7260 : : {
7261 : 1 : error ("attempt to take address of bit-field structure "
7262 : : "member %qD", t);
7263 : 1 : return error_mark_node;
7264 : : }
7265 : 7230 : off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
7266 : 7230 : size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
7267 : : / BITS_PER_UNIT));
7268 : 7230 : break;
7269 : :
7270 : 1226 : case ARRAY_REF:
7271 : 1226 : base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
7272 : 1226 : if (base == error_mark_node)
7273 : : return base;
7274 : :
7275 : 1223 : t = TREE_OPERAND (expr, 1);
7276 : 1223 : STRIP_ANY_LOCATION_WRAPPER (t);
7277 : :
7278 : : /* Check if the offset goes beyond the upper bound of the array. */
7279 : 1223 : if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
7280 : : {
7281 : 956 : tree upbound = array_ref_up_bound (expr);
7282 : 956 : if (upbound != NULL_TREE
7283 : 844 : && TREE_CODE (upbound) == INTEGER_CST
7284 : 1800 : && !tree_int_cst_equal (upbound,
7285 : 844 : TYPE_MAX_VALUE (TREE_TYPE (upbound))))
7286 : : {
7287 : 835 : if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
7288 : 433 : upbound = size_binop (PLUS_EXPR, upbound,
7289 : : build_int_cst (TREE_TYPE (upbound), 1));
7290 : 835 : if (tree_int_cst_lt (upbound, t))
7291 : : {
7292 : 244 : tree v;
7293 : :
7294 : 244 : for (v = TREE_OPERAND (expr, 0);
7295 : 420 : TREE_CODE (v) == COMPONENT_REF;
7296 : 176 : v = TREE_OPERAND (v, 0))
7297 : 280 : if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
7298 : : == RECORD_TYPE)
7299 : : {
7300 : 220 : tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
7301 : 307 : for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
7302 : 191 : if (TREE_CODE (fld_chain) == FIELD_DECL)
7303 : : break;
7304 : :
7305 : : if (fld_chain)
7306 : : break;
7307 : : }
7308 : : /* Don't warn if the array might be considered a poor
7309 : : man's flexible array member with a very permissive
7310 : : definition thereof. */
7311 : 244 : if (TREE_CODE (v) == ARRAY_REF
7312 : 172 : || TREE_CODE (v) == COMPONENT_REF)
7313 : 176 : warning (OPT_Warray_bounds_,
7314 : : "index %E denotes an offset "
7315 : : "greater than size of %qT",
7316 : 176 : t, TREE_TYPE (TREE_OPERAND (expr, 0)));
7317 : : }
7318 : : }
7319 : : }
7320 : :
7321 : 1223 : t = convert (sizetype, t);
7322 : 1223 : off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
7323 : 1223 : break;
7324 : :
7325 : 9 : case COMPOUND_EXPR:
7326 : : /* Handle static members of volatile structs. */
7327 : 9 : t = TREE_OPERAND (expr, 1);
7328 : 9 : gcc_checking_assert (VAR_P (get_base_address (t)));
7329 : : return fold_offsetof (t, type);
7330 : :
7331 : 0 : default:
7332 : 0 : gcc_unreachable ();
7333 : : }
7334 : :
7335 : 8453 : if (!POINTER_TYPE_P (type))
7336 : 7584 : return size_binop (PLUS_EXPR, base, convert (type, off));
7337 : 869 : return fold_build_pointer_plus (base, off);
7338 : : }
7339 : :
7340 : : /* *PTYPE is an incomplete array. Complete it with a domain based on
7341 : : INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
7342 : : is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
7343 : : 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
7344 : :
7345 : : int
7346 : 578099 : complete_array_type (tree *ptype, tree initial_value, bool do_default)
7347 : : {
7348 : 578099 : tree maxindex, type, main_type, elt, unqual_elt;
7349 : 578099 : int failure = 0, quals;
7350 : 578099 : bool overflow_p = false;
7351 : :
7352 : 578099 : maxindex = size_zero_node;
7353 : 578099 : if (initial_value)
7354 : : {
7355 : 570627 : STRIP_ANY_LOCATION_WRAPPER (initial_value);
7356 : :
7357 : 570627 : if (TREE_CODE (initial_value) == STRING_CST)
7358 : : {
7359 : 462954 : int eltsize
7360 : 462954 : = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
7361 : 462954 : maxindex = size_int (TREE_STRING_LENGTH (initial_value) / eltsize
7362 : : - 1);
7363 : : }
7364 : 107673 : else if (TREE_CODE (initial_value) == CONSTRUCTOR)
7365 : : {
7366 : 106741 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
7367 : :
7368 : 106741 : if (vec_safe_is_empty (v))
7369 : : {
7370 : 421 : if (pedantic)
7371 : 71 : failure = 3;
7372 : 421 : maxindex = ssize_int (-1);
7373 : : }
7374 : : else
7375 : : {
7376 : 106320 : tree curindex;
7377 : 106320 : unsigned HOST_WIDE_INT cnt = 1;
7378 : 106320 : constructor_elt *ce;
7379 : 106320 : bool fold_p = false;
7380 : :
7381 : 106320 : if ((*v)[0].index)
7382 : 93344 : maxindex = (*v)[0].index, fold_p = true;
7383 : 106320 : if (TREE_CODE ((*v)[0].value) == RAW_DATA_CST)
7384 : 4 : cnt = 0;
7385 : :
7386 : : curindex = maxindex;
7387 : :
7388 : 18372570 : for (; vec_safe_iterate (v, cnt, &ce); cnt++)
7389 : : {
7390 : 18266250 : bool curfold_p = false;
7391 : 18266250 : if (ce->index)
7392 : 17019410 : curindex = ce->index, curfold_p = true;
7393 : 18266250 : if (!ce->index || TREE_CODE (ce->value) == RAW_DATA_CST)
7394 : : {
7395 : 1247202 : if (fold_p || curfold_p)
7396 : : {
7397 : : /* Since we treat size types now as ordinary
7398 : : unsigned types, we need an explicit overflow
7399 : : check. */
7400 : 362 : tree orig = curindex;
7401 : 362 : curindex = fold_convert (sizetype, curindex);
7402 : 362 : overflow_p |= tree_int_cst_lt (curindex, orig);
7403 : 362 : curfold_p = false;
7404 : : }
7405 : 1247202 : if (TREE_CODE (ce->value) == RAW_DATA_CST)
7406 : 362 : curindex
7407 : 724 : = size_binop (PLUS_EXPR, curindex,
7408 : : size_int (RAW_DATA_LENGTH (ce->value)
7409 : : - ((ce->index || !cnt)
7410 : : ? 1 : 0)));
7411 : : else
7412 : 1246840 : curindex = size_binop (PLUS_EXPR, curindex,
7413 : : size_one_node);
7414 : : }
7415 : 18266250 : if (tree_int_cst_lt (maxindex, curindex))
7416 : 18266250 : maxindex = curindex, fold_p = curfold_p;
7417 : : }
7418 : 106320 : if (fold_p)
7419 : : {
7420 : 93340 : tree orig = maxindex;
7421 : 93340 : maxindex = fold_convert (sizetype, maxindex);
7422 : 93340 : overflow_p |= tree_int_cst_lt (maxindex, orig);
7423 : : }
7424 : : }
7425 : : }
7426 : : else
7427 : : {
7428 : : /* Make an error message unless that happened already. */
7429 : 932 : if (initial_value != error_mark_node)
7430 : 29 : failure = 1;
7431 : : }
7432 : : }
7433 : : else
7434 : : {
7435 : 7472 : failure = 2;
7436 : 7472 : if (!do_default)
7437 : : return failure;
7438 : : }
7439 : :
7440 : 570722 : type = *ptype;
7441 : 570722 : elt = TREE_TYPE (type);
7442 : 570722 : quals = TYPE_QUALS (strip_array_types (elt));
7443 : 570722 : if (quals == 0)
7444 : : unqual_elt = elt;
7445 : : else
7446 : 538278 : unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
7447 : :
7448 : : /* Using build_distinct_type_copy and modifying things afterward instead
7449 : : of using build_array_type to create a new type preserves all of the
7450 : : TYPE_LANG_FLAG_? bits that the front end may have set. */
7451 : 570722 : main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7452 : 570722 : TREE_TYPE (main_type) = unqual_elt;
7453 : 1141444 : TYPE_DOMAIN (main_type)
7454 : 570722 : = build_range_type (TREE_TYPE (maxindex),
7455 : 570722 : build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
7456 : 570722 : TYPE_TYPELESS_STORAGE (main_type) = TYPE_TYPELESS_STORAGE (type);
7457 : 570722 : layout_type (main_type);
7458 : :
7459 : : /* Set TYPE_STRUCTURAL_EQUALITY_P early. */
7460 : 570722 : if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
7461 : 1141054 : || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
7462 : 390 : SET_TYPE_STRUCTURAL_EQUALITY (main_type);
7463 : : else
7464 : 570332 : TYPE_CANONICAL (main_type) = main_type;
7465 : :
7466 : : /* Make sure we have the canonical MAIN_TYPE. */
7467 : 570722 : hashval_t hashcode = type_hash_canon_hash (main_type);
7468 : 570722 : main_type = type_hash_canon (hashcode, main_type);
7469 : :
7470 : : /* Fix the canonical type. */
7471 : 570722 : if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
7472 : 1141054 : || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
7473 : 390 : gcc_assert (TYPE_STRUCTURAL_EQUALITY_P (main_type));
7474 : 570332 : else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
7475 : 570332 : || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
7476 : 552359 : != TYPE_DOMAIN (main_type)))
7477 : 17973 : TYPE_CANONICAL (main_type)
7478 : 35946 : = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
7479 : 17973 : TYPE_CANONICAL (TYPE_DOMAIN (main_type)),
7480 : 17973 : TYPE_TYPELESS_STORAGE (main_type));
7481 : :
7482 : 570722 : if (quals == 0)
7483 : : type = main_type;
7484 : : else
7485 : 538278 : type = c_build_qualified_type (main_type, quals);
7486 : :
7487 : 570722 : if (COMPLETE_TYPE_P (type)
7488 : 570713 : && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
7489 : 1141426 : && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
7490 : : {
7491 : 4 : error ("size of array is too large");
7492 : : /* If we proceed with the array type as it is, we'll eventually
7493 : : crash in tree_to_[su]hwi(). */
7494 : 4 : type = error_mark_node;
7495 : : }
7496 : :
7497 : 570722 : *ptype = type;
7498 : 570722 : return failure;
7499 : : }
7500 : :
7501 : : /* INIT is an constructor of a structure with a flexible array member.
7502 : : Complete the flexible array member with a domain based on it's value. */
7503 : : void
7504 : 8898369 : complete_flexible_array_elts (tree init)
7505 : : {
7506 : 9004565 : tree elt, type;
7507 : :
7508 : 9004565 : if (init == NULL_TREE || TREE_CODE (init) != CONSTRUCTOR)
7509 : : return;
7510 : :
7511 : 107726 : if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
7512 : : return;
7513 : :
7514 : 106449 : elt = CONSTRUCTOR_ELTS (init)->last ().value;
7515 : 106449 : type = TREE_TYPE (elt);
7516 : 106449 : if (TREE_CODE (type) == ARRAY_TYPE
7517 : 106449 : && TYPE_SIZE (type) == NULL_TREE)
7518 : 253 : complete_array_type (&TREE_TYPE (elt), elt, false);
7519 : : else
7520 : : complete_flexible_array_elts (elt);
7521 : : }
7522 : :
7523 : : /* Like c_mark_addressable but don't check register qualifier. */
7524 : : void
7525 : 1125018 : c_common_mark_addressable_vec (tree t)
7526 : : {
7527 : 1305902 : while (handled_component_p (t) || TREE_CODE (t) == C_MAYBE_CONST_EXPR)
7528 : : {
7529 : 180884 : if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
7530 : 26 : t = C_MAYBE_CONST_EXPR_EXPR (t);
7531 : : else
7532 : 180858 : t = TREE_OPERAND (t, 0);
7533 : : }
7534 : 1125018 : if (!VAR_P (t)
7535 : : && TREE_CODE (t) != PARM_DECL
7536 : : && TREE_CODE (t) != COMPOUND_LITERAL_EXPR
7537 : : && TREE_CODE (t) != TARGET_EXPR)
7538 : : return;
7539 : 1104903 : if (!VAR_P (t) || !DECL_HARD_REGISTER (t))
7540 : 1104834 : TREE_ADDRESSABLE (t) = 1;
7541 : 1104903 : if (TREE_CODE (t) == COMPOUND_LITERAL_EXPR)
7542 : 126 : TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (t)) = 1;
7543 : 1104777 : else if (TREE_CODE (t) == TARGET_EXPR)
7544 : 28 : TREE_ADDRESSABLE (TARGET_EXPR_SLOT (t)) = 1;
7545 : : }
7546 : :
7547 : :
7548 : :
7549 : : /* Used to help initialize the builtin-types.def table. When a type of
7550 : : the correct size doesn't exist, use error_mark_node instead of NULL.
7551 : : The later results in segfaults even when a decl using the type doesn't
7552 : : get invoked. */
7553 : :
7554 : : tree
7555 : 1135204 : builtin_type_for_size (int size, bool unsignedp)
7556 : : {
7557 : 1135204 : tree type = c_common_type_for_size (size, unsignedp);
7558 : 1135204 : return type ? type : error_mark_node;
7559 : : }
7560 : :
7561 : : /* Work out the size of the first argument of a call to
7562 : : __builtin_speculation_safe_value. Only pointers and integral types
7563 : : are permitted. Return -1 if the argument type is not supported or
7564 : : the size is too large; 0 if the argument type is a pointer or the
7565 : : size if it is integral. */
7566 : : static enum built_in_function
7567 : 154 : speculation_safe_value_resolve_call (tree function, vec<tree, va_gc> *params,
7568 : : bool complain)
7569 : : {
7570 : : /* Type of the argument. */
7571 : 154 : tree type;
7572 : 154 : int size;
7573 : :
7574 : 154 : if (vec_safe_is_empty (params))
7575 : : {
7576 : 10 : if (complain)
7577 : 10 : error ("too few arguments to function %qE", function);
7578 : 10 : return BUILT_IN_NONE;
7579 : : }
7580 : :
7581 : 144 : type = TREE_TYPE ((*params)[0]);
7582 : 144 : if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
7583 : : {
7584 : : /* Force array-to-pointer decay for C++. */
7585 : 0 : (*params)[0] = default_conversion ((*params)[0]);
7586 : 0 : type = TREE_TYPE ((*params)[0]);
7587 : : }
7588 : :
7589 : 144 : if (POINTER_TYPE_P (type))
7590 : : return BUILT_IN_SPECULATION_SAFE_VALUE_PTR;
7591 : :
7592 : 128 : if (!INTEGRAL_TYPE_P (type))
7593 : 42 : goto incompatible;
7594 : :
7595 : 86 : if (!COMPLETE_TYPE_P (type))
7596 : 0 : goto incompatible;
7597 : :
7598 : 86 : size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7599 : 86 : if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
7600 : 86 : return ((enum built_in_function)
7601 : 172 : ((int) BUILT_IN_SPECULATION_SAFE_VALUE_1 + exact_log2 (size)));
7602 : :
7603 : 0 : incompatible:
7604 : : /* Issue the diagnostic only if the argument is valid, otherwise
7605 : : it would be redundant at best and could be misleading. */
7606 : 42 : if (type != error_mark_node && complain)
7607 : 6 : error ("operand type %qT is incompatible with argument %d of %qE",
7608 : : type, 1, function);
7609 : :
7610 : : return BUILT_IN_NONE;
7611 : : }
7612 : :
7613 : : /* Validate and coerce PARAMS, the arguments to ORIG_FUNCTION to fit
7614 : : the prototype for FUNCTION. The first argument is mandatory, a second
7615 : : argument, if present, must be type compatible with the first. */
7616 : : static bool
7617 : 102 : speculation_safe_value_resolve_params (location_t loc, tree orig_function,
7618 : : vec<tree, va_gc> *params, bool complain)
7619 : : {
7620 : 102 : tree val;
7621 : :
7622 : 102 : if (params->length () == 0)
7623 : : {
7624 : 0 : if (complain)
7625 : 0 : error_at (loc, "too few arguments to function %qE", orig_function);
7626 : 0 : return false;
7627 : : }
7628 : :
7629 : 102 : else if (params->length () > 2)
7630 : : {
7631 : 21 : if (complain)
7632 : 0 : error_at (loc, "too many arguments to function %qE", orig_function);
7633 : 21 : return false;
7634 : : }
7635 : :
7636 : 81 : val = (*params)[0];
7637 : 81 : if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE)
7638 : 0 : val = default_conversion (val);
7639 : 81 : if (!(TREE_CODE (TREE_TYPE (val)) == POINTER_TYPE
7640 : 65 : || TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE))
7641 : : {
7642 : 0 : if (complain)
7643 : 0 : error_at (loc, "expecting argument of type pointer or of type integer "
7644 : : "for argument 1");
7645 : 0 : return false;
7646 : : }
7647 : 81 : (*params)[0] = val;
7648 : :
7649 : 81 : if (params->length () == 2)
7650 : : {
7651 : 32 : tree val2 = (*params)[1];
7652 : 32 : if (TREE_CODE (TREE_TYPE (val2)) == ARRAY_TYPE)
7653 : 0 : val2 = default_conversion (val2);
7654 : 32 : if (error_operand_p (val2))
7655 : : return false;
7656 : 40 : if (!(TREE_TYPE (val) == TREE_TYPE (val2)
7657 : 9 : || useless_type_conversion_p (TREE_TYPE (val), TREE_TYPE (val2))))
7658 : : {
7659 : 9 : if (complain)
7660 : 0 : error_at (loc, "both arguments must be compatible");
7661 : 9 : return false;
7662 : : }
7663 : 22 : (*params)[1] = val2;
7664 : : }
7665 : :
7666 : : return true;
7667 : : }
7668 : :
7669 : : /* Cast the result of the builtin back to the type of the first argument,
7670 : : preserving any qualifiers that it might have. */
7671 : : static tree
7672 : 71 : speculation_safe_value_resolve_return (tree first_param, tree result)
7673 : : {
7674 : 71 : tree ptype = TREE_TYPE (first_param);
7675 : 71 : tree rtype = TREE_TYPE (result);
7676 : 71 : ptype = TYPE_MAIN_VARIANT (ptype);
7677 : :
7678 : 71 : if (tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
7679 : 71 : return convert (ptype, result);
7680 : :
7681 : : return result;
7682 : : }
7683 : :
7684 : : /* A helper function for resolve_overloaded_builtin in resolving the
7685 : : overloaded __sync_ builtins. Returns a positive power of 2 if the
7686 : : first operand of PARAMS is a pointer to a supported data type.
7687 : : Returns 0 if an error is encountered. Return -1 for _BitInt
7688 : : __atomic*fetch* with unsupported type which should be handled by
7689 : : a cas loop.
7690 : : FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
7691 : : built-ins. ORIG_FORMAT is for __sync_* rather than __atomic_*
7692 : : built-ins. */
7693 : :
7694 : : static int
7695 : 330425 : sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch,
7696 : : bool orig_format, bool complain)
7697 : : {
7698 : : /* Type of the argument. */
7699 : 330425 : tree argtype;
7700 : : /* Type the argument points to. */
7701 : 330425 : tree type;
7702 : 330425 : int size;
7703 : :
7704 : 330425 : if (vec_safe_is_empty (params))
7705 : : {
7706 : 106 : if (complain)
7707 : 106 : error ("too few arguments to function %qE", function);
7708 : 106 : return 0;
7709 : : }
7710 : :
7711 : 330319 : argtype = type = TREE_TYPE ((*params)[0]);
7712 : 330319 : if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
7713 : : {
7714 : : /* Force array-to-pointer decay for C++. */
7715 : 3 : (*params)[0] = default_conversion ((*params)[0]);
7716 : 3 : type = TREE_TYPE ((*params)[0]);
7717 : : }
7718 : 330319 : if (TREE_CODE (type) != POINTER_TYPE)
7719 : 2127 : goto incompatible;
7720 : :
7721 : 328192 : type = TREE_TYPE (type);
7722 : 328192 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
7723 : 290 : goto incompatible;
7724 : :
7725 : 327902 : if (!COMPLETE_TYPE_P (type))
7726 : 2 : goto incompatible;
7727 : :
7728 : 327900 : if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
7729 : 84 : goto incompatible;
7730 : :
7731 : 327816 : size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7732 : 327816 : if (size == 16
7733 : 7679 : && TREE_CODE (type) == BITINT_TYPE
7734 : 328342 : && !targetm.scalar_mode_supported_p (TImode))
7735 : : {
7736 : 0 : if (fetch && !orig_format)
7737 : : return -1;
7738 : 0 : goto incompatible;
7739 : : }
7740 : :
7741 : 327816 : if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
7742 : : return size;
7743 : :
7744 : 528 : if (fetch && !orig_format && TREE_CODE (type) == BITINT_TYPE)
7745 : : return -1;
7746 : :
7747 : 0 : incompatible:
7748 : : /* Issue the diagnostic only if the argument is valid, otherwise
7749 : : it would be redundant at best and could be misleading. */
7750 : 2503 : if (argtype != error_mark_node && complain)
7751 : 920 : error ("operand type %qT is incompatible with argument %d of %qE",
7752 : : argtype, 1, function);
7753 : : return 0;
7754 : : }
7755 : :
7756 : : /* A helper function for resolve_overloaded_builtin. Adds casts to
7757 : : PARAMS to make arguments match up with those of FUNCTION. Drops
7758 : : the variadic arguments at the end. Returns false if some error
7759 : : was encountered; true on success. */
7760 : :
7761 : : static bool
7762 : 327288 : sync_resolve_params (location_t loc, tree orig_function, tree function,
7763 : : vec<tree, va_gc> *params, bool orig_format, bool complain)
7764 : : {
7765 : 327288 : function_args_iterator iter;
7766 : 327288 : tree ptype;
7767 : 327288 : unsigned int parmnum;
7768 : :
7769 : 327288 : function_args_iter_init (&iter, TREE_TYPE (function));
7770 : : /* We've declared the implementation functions to use "volatile void *"
7771 : : as the pointer parameter, so we shouldn't get any complaints from the
7772 : : call to check_function_arguments what ever type the user used. */
7773 : 327288 : function_args_iter_next (&iter);
7774 : 327288 : ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
7775 : 327288 : ptype = TYPE_MAIN_VARIANT (ptype);
7776 : :
7777 : : /* For the rest of the values, we need to cast these to FTYPE, so that we
7778 : : don't get warnings for passing pointer types, etc. */
7779 : 327288 : parmnum = 0;
7780 : 1836342 : while (1)
7781 : : {
7782 : 1081815 : tree val, arg_type;
7783 : :
7784 : 1081815 : arg_type = function_args_iter_cond (&iter);
7785 : : /* XXX void_type_node belies the abstraction. */
7786 : 1081815 : if (arg_type == void_type_node)
7787 : : break;
7788 : :
7789 : 754585 : ++parmnum;
7790 : 754585 : if (params->length () <= parmnum)
7791 : : {
7792 : 58 : if (complain)
7793 : 7 : error_at (loc, "too few arguments to function %qE", orig_function);
7794 : 58 : return false;
7795 : : }
7796 : :
7797 : : /* Only convert parameters if arg_type is unsigned integer type with
7798 : : new format sync routines, i.e. don't attempt to convert pointer
7799 : : arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
7800 : : bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
7801 : : kinds). */
7802 : 754527 : if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
7803 : : {
7804 : : /* Ideally for the first conversion we'd use convert_for_assignment
7805 : : so that we get warnings for anything that doesn't match the pointer
7806 : : type. This isn't portable across the C and C++ front ends atm. */
7807 : 209252 : val = (*params)[parmnum];
7808 : 209252 : val = convert (ptype, val);
7809 : 209252 : val = convert (arg_type, val);
7810 : 209252 : (*params)[parmnum] = val;
7811 : : }
7812 : :
7813 : 754527 : function_args_iter_next (&iter);
7814 : 754527 : }
7815 : :
7816 : : /* __atomic routines are not variadic. */
7817 : 327230 : if (!orig_format && params->length () != parmnum + 1)
7818 : : {
7819 : 106 : if (complain)
7820 : 103 : error_at (loc, "too many arguments to function %qE", orig_function);
7821 : 106 : return false;
7822 : : }
7823 : :
7824 : : /* The definition of these primitives is variadic, with the remaining
7825 : : being "an optional list of variables protected by the memory barrier".
7826 : : No clue what that's supposed to mean, precisely, but we consider all
7827 : : call-clobbered variables to be protected so we're safe. */
7828 : 327124 : params->truncate (parmnum + 1);
7829 : :
7830 : 327124 : return true;
7831 : : }
7832 : :
7833 : : /* A helper function for resolve_overloaded_builtin. Adds a cast to
7834 : : RESULT to make it match the type of the first pointer argument in
7835 : : PARAMS. */
7836 : :
7837 : : static tree
7838 : 216609 : sync_resolve_return (tree first_param, tree result, bool orig_format)
7839 : : {
7840 : 216609 : tree ptype = TREE_TYPE (TREE_TYPE (first_param));
7841 : 216609 : tree rtype = TREE_TYPE (result);
7842 : 216609 : ptype = TYPE_MAIN_VARIANT (ptype);
7843 : :
7844 : : /* New format doesn't require casting unless the types are the same size. */
7845 : 216609 : if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
7846 : 216609 : return convert (ptype, result);
7847 : : else
7848 : : return result;
7849 : : }
7850 : :
7851 : : /* This function verifies the PARAMS to generic atomic FUNCTION.
7852 : : It returns the size if all the parameters are the same size, otherwise
7853 : : 0 is returned if the parameters are invalid. */
7854 : :
7855 : : static int
7856 : 85199 : get_atomic_generic_size (location_t loc, tree function,
7857 : : vec<tree, va_gc> *params, bool complain)
7858 : : {
7859 : 85199 : unsigned int n_param;
7860 : 85199 : unsigned int n_model;
7861 : 85199 : unsigned int outputs = 0; // bitset of output parameters
7862 : 85199 : unsigned int x;
7863 : 85199 : int size_0;
7864 : 85199 : tree type_0;
7865 : :
7866 : : /* Determine the parameter makeup. */
7867 : 85199 : switch (DECL_FUNCTION_CODE (function))
7868 : : {
7869 : : case BUILT_IN_ATOMIC_EXCHANGE:
7870 : : n_param = 4;
7871 : : n_model = 1;
7872 : : outputs = 5;
7873 : : break;
7874 : 51229 : case BUILT_IN_ATOMIC_LOAD:
7875 : 51229 : n_param = 3;
7876 : 51229 : n_model = 1;
7877 : 51229 : outputs = 2;
7878 : 51229 : break;
7879 : 11501 : case BUILT_IN_ATOMIC_STORE:
7880 : 11501 : n_param = 3;
7881 : 11501 : n_model = 1;
7882 : 11501 : outputs = 1;
7883 : 11501 : break;
7884 : 21815 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7885 : 21815 : n_param = 6;
7886 : 21815 : n_model = 2;
7887 : 21815 : outputs = 3;
7888 : 21815 : break;
7889 : 0 : default:
7890 : 0 : gcc_unreachable ();
7891 : : }
7892 : :
7893 : 85199 : if (vec_safe_length (params) != n_param)
7894 : : {
7895 : 466 : if (complain)
7896 : 247 : error_at (loc, "incorrect number of arguments to function %qE",
7897 : : function);
7898 : 466 : return 0;
7899 : : }
7900 : :
7901 : : /* Get type of first parameter, and determine its size. */
7902 : 84733 : type_0 = TREE_TYPE ((*params)[0]);
7903 : 84733 : if (TREE_CODE (type_0) == ARRAY_TYPE && c_dialect_cxx ())
7904 : : {
7905 : : /* Force array-to-pointer decay for C++. */
7906 : 15 : (*params)[0] = default_conversion ((*params)[0]);
7907 : 15 : type_0 = TREE_TYPE ((*params)[0]);
7908 : : }
7909 : 84733 : if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
7910 : : {
7911 : 152 : if (complain)
7912 : 41 : error_at (loc, "argument 1 of %qE must be a non-void pointer type",
7913 : : function);
7914 : 152 : return 0;
7915 : : }
7916 : :
7917 : 84581 : if (!COMPLETE_TYPE_P (TREE_TYPE (type_0)))
7918 : : {
7919 : 23 : if (complain)
7920 : 8 : error_at (loc, "argument 1 of %qE must be a pointer to a complete type",
7921 : : function);
7922 : 23 : return 0;
7923 : : }
7924 : :
7925 : : /* Types must be compile time constant sizes. */
7926 : 84558 : if (!tree_fits_uhwi_p ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))))
7927 : : {
7928 : 1 : if (complain)
7929 : 1 : error_at (loc,
7930 : : "argument 1 of %qE must be a pointer to a constant size type",
7931 : : function);
7932 : 1 : return 0;
7933 : : }
7934 : :
7935 : 84557 : size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
7936 : :
7937 : : /* Zero size objects are not allowed. */
7938 : 84557 : if (size_0 == 0)
7939 : : {
7940 : 22 : if (complain)
7941 : 7 : error_at (
7942 : : loc, "argument 1 of %qE must be a pointer to a nonzero size object",
7943 : : function);
7944 : 22 : return 0;
7945 : : }
7946 : :
7947 : : /* Check each other parameter is a pointer and the same size. */
7948 : 297202 : for (x = 0; x < n_param - n_model; x++)
7949 : : {
7950 : 212806 : int size;
7951 : 212806 : tree type = TREE_TYPE ((*params)[x]);
7952 : : /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
7953 : 212806 : if (n_param == 6 && x == 3)
7954 : 21637 : continue;
7955 : 191169 : if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
7956 : : {
7957 : : /* Force array-to-pointer decay for C++. */
7958 : 18 : (*params)[x] = default_conversion ((*params)[x]);
7959 : 18 : type = TREE_TYPE ((*params)[x]);
7960 : : }
7961 : 191169 : if (!POINTER_TYPE_P (type))
7962 : : {
7963 : 16 : if (complain)
7964 : 13 : error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
7965 : : function);
7966 : 16 : return 0;
7967 : : }
7968 : 191153 : else if (TYPE_SIZE_UNIT (TREE_TYPE (type))
7969 : 191153 : && TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type))))
7970 : : != INTEGER_CST)
7971 : : {
7972 : 1 : if (complain)
7973 : 1 : error_at (loc,
7974 : : "argument %d of %qE must be a pointer to a constant "
7975 : : "size type",
7976 : : x + 1, function);
7977 : 1 : return 0;
7978 : : }
7979 : 191152 : else if (FUNCTION_POINTER_TYPE_P (type))
7980 : : {
7981 : 10 : if (complain)
7982 : 7 : error_at (loc,
7983 : : "argument %d of %qE must not be a pointer to a "
7984 : : "function",
7985 : : x + 1, function);
7986 : 10 : return 0;
7987 : : }
7988 : 191142 : tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
7989 : 191142 : size = type_size ? tree_to_uhwi (type_size) : 0;
7990 : 191142 : if (size != size_0)
7991 : : {
7992 : 34 : if (complain)
7993 : 25 : error_at (loc, "size mismatch in argument %d of %qE", x + 1,
7994 : : function);
7995 : 34 : return 0;
7996 : : }
7997 : :
7998 : 191108 : {
7999 : 191108 : auto_diagnostic_group d;
8000 : 191108 : int quals = TYPE_QUALS (TREE_TYPE (type));
8001 : : /* Must not write to an argument of a const-qualified type. */
8002 : 191108 : if (outputs & (1 << x) && quals & TYPE_QUAL_CONST)
8003 : : {
8004 : 57 : if (c_dialect_cxx ())
8005 : : {
8006 : 45 : if (complain)
8007 : 42 : error_at (loc,
8008 : : "argument %d of %qE must not be a pointer to "
8009 : : "a %<const%> type",
8010 : : x + 1, function);
8011 : 45 : return 0;
8012 : : }
8013 : : else
8014 : 12 : pedwarn (loc, OPT_Wdiscarded_qualifiers, "argument %d "
8015 : : "of %qE discards %<const%> qualifier", x + 1,
8016 : : function);
8017 : : }
8018 : : /* Only the first argument is allowed to be volatile. */
8019 : 191063 : if (x > 0 && quals & TYPE_QUAL_VOLATILE)
8020 : : {
8021 : 44 : if (c_dialect_cxx ())
8022 : : {
8023 : 33 : if (complain)
8024 : 30 : error_at (loc,
8025 : : "argument %d of %qE must not be a pointer to "
8026 : : "a %<volatile%> type",
8027 : : x + 1, function);
8028 : 33 : return 0;
8029 : : }
8030 : : else
8031 : 11 : pedwarn (loc, OPT_Wdiscarded_qualifiers, "argument %d "
8032 : : "of %qE discards %<volatile%> qualifier", x + 1,
8033 : : function);
8034 : : }
8035 : 191108 : }
8036 : : }
8037 : :
8038 : : /* Check memory model parameters for validity. */
8039 : 190413 : for (x = n_param - n_model ; x < n_param; x++)
8040 : : {
8041 : 106033 : tree p = (*params)[x];
8042 : 106033 : if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
8043 : : {
8044 : 10 : if (complain)
8045 : 7 : error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
8046 : : function);
8047 : 10 : return 0;
8048 : : }
8049 : 106023 : p = fold_for_warn (p);
8050 : 106023 : if (TREE_CODE (p) == INTEGER_CST)
8051 : : {
8052 : : /* memmodel_base masks the low 16 bits, thus ignore any bits above
8053 : : it by using TREE_INT_CST_LOW instead of tree_to_*hwi. Those high
8054 : : bits will be checked later during expansion in target specific
8055 : : way. */
8056 : 79314 : if (memmodel_base (TREE_INT_CST_LOW (p)) >= MEMMODEL_LAST)
8057 : : {
8058 : 20 : if (complain)
8059 : 14 : warning_at (loc, OPT_Winvalid_memory_model,
8060 : : "invalid memory model argument %d of %qE", x + 1,
8061 : : function);
8062 : : else
8063 : : return 0;
8064 : : }
8065 : : }
8066 : : }
8067 : :
8068 : : return size_0;
8069 : : }
8070 : :
8071 : :
8072 : : /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
8073 : : at the beginning of the parameter list PARAMS representing the size of the
8074 : : objects. This is to match the library ABI requirement. LOC is the location
8075 : : of the function call.
8076 : : The new function is returned if it needed rebuilding, otherwise NULL_TREE is
8077 : : returned to allow the external call to be constructed. */
8078 : :
8079 : : static tree
8080 : 4146 : add_atomic_size_parameter (unsigned n, location_t loc, tree function,
8081 : : vec<tree, va_gc> *params)
8082 : : {
8083 : 4146 : tree size_node;
8084 : :
8085 : : /* Insert a SIZE_T parameter as the first param. If there isn't
8086 : : enough space, allocate a new vector and recursively re-build with that. */
8087 : 4146 : if (!params->space (1))
8088 : : {
8089 : 33 : unsigned int z, len;
8090 : 33 : vec<tree, va_gc> *v;
8091 : 33 : tree f;
8092 : :
8093 : 33 : len = params->length ();
8094 : 33 : vec_alloc (v, len + 1);
8095 : 33 : v->quick_push (build_int_cst (size_type_node, n));
8096 : 165 : for (z = 0; z < len; z++)
8097 : 132 : v->quick_push ((*params)[z]);
8098 : 33 : f = build_function_call_vec (loc, vNULL, function, v, NULL);
8099 : 33 : vec_free (v);
8100 : 33 : return f;
8101 : : }
8102 : :
8103 : : /* Add the size parameter and leave as a function call for processing. */
8104 : 4113 : size_node = build_int_cst (size_type_node, n);
8105 : 4113 : params->quick_insert (0, size_node);
8106 : 4113 : return NULL_TREE;
8107 : : }
8108 : :
8109 : :
8110 : : /* Return whether atomic operations for naturally aligned N-byte
8111 : : arguments are supported, whether inline or through libatomic. */
8112 : : static bool
8113 : 84380 : atomic_size_supported_p (int n)
8114 : : {
8115 : 84380 : switch (n)
8116 : : {
8117 : : case 1:
8118 : : case 2:
8119 : : case 4:
8120 : : case 8:
8121 : : return true;
8122 : :
8123 : 6972 : case 16:
8124 : 6972 : return targetm.scalar_mode_supported_p (TImode);
8125 : :
8126 : 4146 : default:
8127 : 4146 : return false;
8128 : : }
8129 : : }
8130 : :
8131 : : /* This will process an __atomic_exchange function call, determine whether it
8132 : : needs to be mapped to the _N variation, or turned into a library call.
8133 : : LOC is the location of the builtin call.
8134 : : FUNCTION is the DECL that has been invoked;
8135 : : PARAMS is the argument list for the call. The return value is non-null
8136 : : TRUE is returned if it is translated into the proper format for a call to the
8137 : : external library, and NEW_RETURN is set the tree for that function.
8138 : : FALSE is returned if processing for the _N variation is required, and
8139 : : NEW_RETURN is set to the return value the result is copied into. */
8140 : : static bool
8141 : 654 : resolve_overloaded_atomic_exchange (location_t loc, tree function,
8142 : : vec<tree, va_gc> *params, tree *new_return,
8143 : : bool complain)
8144 : : {
8145 : 654 : tree p0, p1, p2, p3;
8146 : 654 : tree I_type, I_type_ptr;
8147 : 654 : int n = get_atomic_generic_size (loc, function, params, complain);
8148 : :
8149 : : /* Size of 0 is an error condition. */
8150 : 654 : if (n == 0)
8151 : : {
8152 : 195 : *new_return = error_mark_node;
8153 : 195 : return true;
8154 : : }
8155 : :
8156 : : /* If not a lock-free size, change to the library generic format. */
8157 : 459 : if (!atomic_size_supported_p (n))
8158 : : {
8159 : 39 : *new_return = add_atomic_size_parameter (n, loc, function, params);
8160 : 39 : return true;
8161 : : }
8162 : :
8163 : : /* Otherwise there is a lockfree match, transform the call from:
8164 : : void fn(T* mem, T* desired, T* return, model)
8165 : : into
8166 : : *return = (T) (fn (In* mem, (In) *desired, model)) */
8167 : :
8168 : 420 : p0 = (*params)[0];
8169 : 420 : p1 = (*params)[1];
8170 : 420 : p2 = (*params)[2];
8171 : 420 : p3 = (*params)[3];
8172 : :
8173 : : /* Create pointer to appropriate size. */
8174 : 420 : I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
8175 : 420 : I_type_ptr = build_pointer_type (I_type);
8176 : :
8177 : : /* Convert object pointer to required type. */
8178 : 420 : p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
8179 : 420 : (*params)[0] = p0;
8180 : : /* Convert new value to required type, and dereference it.
8181 : : If *p1 type can have padding or may involve floating point which
8182 : : could e.g. be promoted to wider precision and demoted afterwards,
8183 : : state of padding bits might not be preserved. */
8184 : 420 : build_indirect_ref (loc, p1, RO_UNARY_STAR);
8185 : 420 : p1 = build2_loc (loc, MEM_REF, I_type,
8186 : : build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1),
8187 : 420 : build_zero_cst (TREE_TYPE (p1)));
8188 : 420 : (*params)[1] = p1;
8189 : :
8190 : : /* Move memory model to the 3rd position, and end param list. */
8191 : 420 : (*params)[2] = p3;
8192 : 420 : params->truncate (3);
8193 : :
8194 : : /* Convert return pointer and dereference it for later assignment. */
8195 : 420 : *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
8196 : :
8197 : 420 : return false;
8198 : : }
8199 : :
8200 : : /* This will process an __atomic_compare_exchange function call, determine
8201 : : whether it needs to be mapped to the _N variation, or turned into a lib call.
8202 : : LOC is the location of the builtin call.
8203 : : FUNCTION is the DECL that has been invoked;
8204 : : PARAMS is the argument list for the call. The return value is non-null
8205 : : TRUE is returned if it is translated into the proper format for a call to the
8206 : : external library, and NEW_RETURN is set the tree for that function.
8207 : : FALSE is returned if processing for the _N variation is required. */
8208 : :
8209 : : static bool
8210 : 21815 : resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
8211 : : vec<tree, va_gc> *params,
8212 : : tree *new_return, bool complain)
8213 : : {
8214 : 21815 : tree p0, p1, p2;
8215 : 21815 : tree I_type, I_type_ptr;
8216 : 21815 : int n = get_atomic_generic_size (loc, function, params, complain);
8217 : :
8218 : : /* Size of 0 is an error condition. */
8219 : 21815 : if (n == 0)
8220 : : {
8221 : 178 : *new_return = error_mark_node;
8222 : 178 : return true;
8223 : : }
8224 : :
8225 : : /* If not a lock-free size, change to the library generic format. */
8226 : 21637 : if (!atomic_size_supported_p (n))
8227 : : {
8228 : : /* The library generic format does not have the weak parameter, so
8229 : : remove it from the param list. Since a parameter has been removed,
8230 : : we can be sure that there is room for the SIZE_T parameter, meaning
8231 : : there will not be a recursive rebuilding of the parameter list, so
8232 : : there is no danger this will be done twice. */
8233 : 1198 : if (n > 0)
8234 : : {
8235 : 1198 : (*params)[3] = (*params)[4];
8236 : 1198 : (*params)[4] = (*params)[5];
8237 : 1198 : params->truncate (5);
8238 : : }
8239 : 1198 : *new_return = add_atomic_size_parameter (n, loc, function, params);
8240 : 1198 : return true;
8241 : : }
8242 : :
8243 : : /* Otherwise, there is a match, so the call needs to be transformed from:
8244 : : bool fn(T* mem, T* desired, T* return, weak, success, failure)
8245 : : into
8246 : : bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
8247 : :
8248 : 20439 : p0 = (*params)[0];
8249 : 20439 : p1 = (*params)[1];
8250 : 20439 : p2 = (*params)[2];
8251 : :
8252 : : /* Create pointer to appropriate size. */
8253 : 20439 : I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
8254 : 20439 : I_type_ptr = build_pointer_type (I_type);
8255 : :
8256 : : /* Convert object pointer to required type. */
8257 : 20439 : p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
8258 : 20439 : (*params)[0] = p0;
8259 : :
8260 : : /* Convert expected pointer to required type. */
8261 : 20439 : p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
8262 : 20439 : (*params)[1] = p1;
8263 : :
8264 : : /* Convert desired value to required type, and dereference it.
8265 : : If *p2 type can have padding or may involve floating point which
8266 : : could e.g. be promoted to wider precision and demoted afterwards,
8267 : : state of padding bits might not be preserved. */
8268 : 20439 : build_indirect_ref (loc, p2, RO_UNARY_STAR);
8269 : 20439 : p2 = build2_loc (loc, MEM_REF, I_type,
8270 : : build1 (VIEW_CONVERT_EXPR, I_type_ptr, p2),
8271 : 20439 : build_zero_cst (TREE_TYPE (p2)));
8272 : 20439 : (*params)[2] = p2;
8273 : :
8274 : : /* The rest of the parameters are fine. NULL means no special return value
8275 : : processing.*/
8276 : 20439 : *new_return = NULL;
8277 : 20439 : return false;
8278 : : }
8279 : :
8280 : : /* This will process an __atomic_load function call, determine whether it
8281 : : needs to be mapped to the _N variation, or turned into a library call.
8282 : : LOC is the location of the builtin call.
8283 : : FUNCTION is the DECL that has been invoked;
8284 : : PARAMS is the argument list for the call. The return value is non-null
8285 : : TRUE is returned if it is translated into the proper format for a call to the
8286 : : external library, and NEW_RETURN is set the tree for that function.
8287 : : FALSE is returned if processing for the _N variation is required, and
8288 : : NEW_RETURN is set to the return value the result is copied into. */
8289 : :
8290 : : static bool
8291 : 51229 : resolve_overloaded_atomic_load (location_t loc, tree function,
8292 : : vec<tree, va_gc> *params, tree *new_return,
8293 : : bool complain)
8294 : : {
8295 : 51229 : tree p0, p1, p2;
8296 : 51229 : tree I_type, I_type_ptr;
8297 : 51229 : int n = get_atomic_generic_size (loc, function, params, complain);
8298 : :
8299 : : /* Size of 0 is an error condition. */
8300 : 51229 : if (n == 0)
8301 : : {
8302 : 278 : *new_return = error_mark_node;
8303 : 278 : return true;
8304 : : }
8305 : :
8306 : : /* If not a lock-free size, change to the library generic format. */
8307 : 50951 : if (!atomic_size_supported_p (n))
8308 : : {
8309 : 2499 : *new_return = add_atomic_size_parameter (n, loc, function, params);
8310 : 2499 : return true;
8311 : : }
8312 : :
8313 : : /* Otherwise, there is a match, so the call needs to be transformed from:
8314 : : void fn(T* mem, T* return, model)
8315 : : into
8316 : : *return = (T) (fn ((In *) mem, model)) */
8317 : :
8318 : 48452 : p0 = (*params)[0];
8319 : 48452 : p1 = (*params)[1];
8320 : 48452 : p2 = (*params)[2];
8321 : :
8322 : : /* Create pointer to appropriate size. */
8323 : 48452 : I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
8324 : 48452 : I_type_ptr = build_pointer_type (I_type);
8325 : :
8326 : : /* Convert object pointer to required type. */
8327 : 48452 : p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
8328 : 48452 : (*params)[0] = p0;
8329 : :
8330 : : /* Move memory model to the 2nd position, and end param list. */
8331 : 48452 : (*params)[1] = p2;
8332 : 48452 : params->truncate (2);
8333 : :
8334 : : /* Convert return pointer and dereference it for later assignment. */
8335 : 48452 : *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
8336 : :
8337 : 48452 : return false;
8338 : : }
8339 : :
8340 : : /* This will process an __atomic_store function call, determine whether it
8341 : : needs to be mapped to the _N variation, or turned into a library call.
8342 : : LOC is the location of the builtin call.
8343 : : FUNCTION is the DECL that has been invoked;
8344 : : PARAMS is the argument list for the call. The return value is non-null
8345 : : TRUE is returned if it is translated into the proper format for a call to the
8346 : : external library, and NEW_RETURN is set the tree for that function.
8347 : : FALSE is returned if processing for the _N variation is required, and
8348 : : NEW_RETURN is set to the return value the result is copied into. */
8349 : :
8350 : : static bool
8351 : 11501 : resolve_overloaded_atomic_store (location_t loc, tree function,
8352 : : vec<tree, va_gc> *params, tree *new_return,
8353 : : bool complain)
8354 : : {
8355 : 11501 : tree p0, p1;
8356 : 11501 : tree I_type, I_type_ptr;
8357 : 11501 : int n = get_atomic_generic_size (loc, function, params, complain);
8358 : :
8359 : : /* Size of 0 is an error condition. */
8360 : 11501 : if (n == 0)
8361 : : {
8362 : 168 : *new_return = error_mark_node;
8363 : 168 : return true;
8364 : : }
8365 : :
8366 : : /* If not a lock-free size, change to the library generic format. */
8367 : 11333 : if (!atomic_size_supported_p (n))
8368 : : {
8369 : 410 : *new_return = add_atomic_size_parameter (n, loc, function, params);
8370 : 410 : return true;
8371 : : }
8372 : :
8373 : : /* Otherwise, there is a match, so the call needs to be transformed from:
8374 : : void fn(T* mem, T* value, model)
8375 : : into
8376 : : fn ((In *) mem, (In) *value, model) */
8377 : :
8378 : 10923 : p0 = (*params)[0];
8379 : 10923 : p1 = (*params)[1];
8380 : :
8381 : : /* Create pointer to appropriate size. */
8382 : 10923 : I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
8383 : 10923 : I_type_ptr = build_pointer_type (I_type);
8384 : :
8385 : : /* Convert object pointer to required type. */
8386 : 10923 : p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
8387 : 10923 : (*params)[0] = p0;
8388 : :
8389 : : /* Convert new value to required type, and dereference it. */
8390 : 10923 : p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
8391 : 10923 : p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
8392 : 10923 : (*params)[1] = p1;
8393 : :
8394 : : /* The memory model is in the right spot already. Return is void. */
8395 : 10923 : *new_return = NULL_TREE;
8396 : :
8397 : 10923 : return false;
8398 : : }
8399 : :
8400 : : /* Emit __atomic*fetch* on _BitInt which doesn't have a size of
8401 : : 1, 2, 4, 8 or 16 bytes using __atomic_compare_exchange loop.
8402 : : ORIG_CODE is the DECL_FUNCTION_CODE of ORIG_FUNCTION and
8403 : : ORIG_PARAMS arguments of the call. */
8404 : :
8405 : : static tree
8406 : 528 : atomic_bitint_fetch_using_cas_loop (location_t loc,
8407 : : enum built_in_function orig_code,
8408 : : tree orig_function,
8409 : : vec<tree, va_gc> *orig_params)
8410 : : {
8411 : 528 : enum tree_code code = ERROR_MARK;
8412 : 528 : bool return_old_p = false;
8413 : 528 : switch (orig_code)
8414 : : {
8415 : 0 : case BUILT_IN_ATOMIC_ADD_FETCH_N:
8416 : 0 : code = PLUS_EXPR;
8417 : 0 : break;
8418 : 1 : case BUILT_IN_ATOMIC_SUB_FETCH_N:
8419 : 1 : code = MINUS_EXPR;
8420 : 1 : break;
8421 : 0 : case BUILT_IN_ATOMIC_AND_FETCH_N:
8422 : 0 : code = BIT_AND_EXPR;
8423 : 0 : break;
8424 : : case BUILT_IN_ATOMIC_NAND_FETCH_N:
8425 : : break;
8426 : 1 : case BUILT_IN_ATOMIC_XOR_FETCH_N:
8427 : 1 : code = BIT_XOR_EXPR;
8428 : 1 : break;
8429 : 0 : case BUILT_IN_ATOMIC_OR_FETCH_N:
8430 : 0 : code = BIT_IOR_EXPR;
8431 : 0 : break;
8432 : 106 : case BUILT_IN_ATOMIC_FETCH_ADD_N:
8433 : 106 : code = PLUS_EXPR;
8434 : 106 : return_old_p = true;
8435 : 106 : break;
8436 : 104 : case BUILT_IN_ATOMIC_FETCH_SUB_N:
8437 : 104 : code = MINUS_EXPR;
8438 : 104 : return_old_p = true;
8439 : 104 : break;
8440 : 105 : case BUILT_IN_ATOMIC_FETCH_AND_N:
8441 : 105 : code = BIT_AND_EXPR;
8442 : 105 : return_old_p = true;
8443 : 105 : break;
8444 : 0 : case BUILT_IN_ATOMIC_FETCH_NAND_N:
8445 : 0 : return_old_p = true;
8446 : 0 : break;
8447 : 104 : case BUILT_IN_ATOMIC_FETCH_XOR_N:
8448 : 104 : code = BIT_XOR_EXPR;
8449 : 104 : return_old_p = true;
8450 : 104 : break;
8451 : 106 : case BUILT_IN_ATOMIC_FETCH_OR_N:
8452 : 106 : code = BIT_IOR_EXPR;
8453 : 106 : return_old_p = true;
8454 : 106 : break;
8455 : 0 : default:
8456 : 0 : gcc_unreachable ();
8457 : : }
8458 : :
8459 : 528 : if (orig_params->length () != 3)
8460 : : {
8461 : 0 : if (orig_params->length () < 3)
8462 : 0 : error_at (loc, "too few arguments to function %qE", orig_function);
8463 : : else
8464 : 0 : error_at (loc, "too many arguments to function %qE", orig_function);
8465 : 0 : return error_mark_node;
8466 : : }
8467 : :
8468 : 528 : tree stmts = push_stmt_list ();
8469 : :
8470 : 528 : tree nonatomic_lhs_type = TREE_TYPE (TREE_TYPE ((*orig_params)[0]));
8471 : 528 : nonatomic_lhs_type = TYPE_MAIN_VARIANT (nonatomic_lhs_type);
8472 : 528 : gcc_assert (TREE_CODE (nonatomic_lhs_type) == BITINT_TYPE);
8473 : :
8474 : 528 : tree lhs_addr = (*orig_params)[0];
8475 : 528 : tree val = convert (nonatomic_lhs_type, (*orig_params)[1]);
8476 : 528 : tree model = convert (integer_type_node, (*orig_params)[2]);
8477 : 528 : if (!c_dialect_cxx ())
8478 : : {
8479 : 528 : lhs_addr = c_fully_fold (lhs_addr, false, NULL);
8480 : 528 : val = c_fully_fold (val, false, NULL);
8481 : 528 : model = c_fully_fold (model, false, NULL);
8482 : : }
8483 : 528 : if (TREE_SIDE_EFFECTS (lhs_addr))
8484 : : {
8485 : 1 : tree var = create_tmp_var_raw (TREE_TYPE (lhs_addr));
8486 : 1 : lhs_addr = build4 (TARGET_EXPR, TREE_TYPE (lhs_addr), var, lhs_addr,
8487 : : NULL_TREE, NULL_TREE);
8488 : 1 : add_stmt (lhs_addr);
8489 : : }
8490 : 528 : if (TREE_SIDE_EFFECTS (val))
8491 : : {
8492 : 1 : tree var = create_tmp_var_raw (nonatomic_lhs_type);
8493 : 1 : val = build4 (TARGET_EXPR, nonatomic_lhs_type, var, val, NULL_TREE,
8494 : : NULL_TREE);
8495 : 1 : add_stmt (val);
8496 : : }
8497 : 528 : if (TREE_SIDE_EFFECTS (model))
8498 : : {
8499 : 1 : tree var = create_tmp_var_raw (integer_type_node);
8500 : 1 : model = build4 (TARGET_EXPR, integer_type_node, var, model, NULL_TREE,
8501 : : NULL_TREE);
8502 : 1 : add_stmt (model);
8503 : : }
8504 : :
8505 : 528 : tree old = create_tmp_var_raw (nonatomic_lhs_type);
8506 : 528 : tree old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
8507 : 528 : TREE_ADDRESSABLE (old) = 1;
8508 : 528 : suppress_warning (old);
8509 : :
8510 : 528 : tree newval = create_tmp_var_raw (nonatomic_lhs_type);
8511 : 528 : tree newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
8512 : 528 : TREE_ADDRESSABLE (newval) = 1;
8513 : 528 : suppress_warning (newval);
8514 : :
8515 : 528 : tree loop_decl = create_artificial_label (loc);
8516 : 528 : tree loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
8517 : :
8518 : 528 : tree done_decl = create_artificial_label (loc);
8519 : 528 : tree done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
8520 : :
8521 : 528 : vec<tree, va_gc> *params;
8522 : 528 : vec_alloc (params, 6);
8523 : :
8524 : : /* __atomic_load (addr, &old, SEQ_CST). */
8525 : 528 : tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
8526 : 528 : params->quick_push (lhs_addr);
8527 : 528 : params->quick_push (old_addr);
8528 : 528 : params->quick_push (build_int_cst (integer_type_node, MEMMODEL_RELAXED));
8529 : 528 : tree func_call = resolve_overloaded_builtin (loc, fndecl, params);
8530 : 528 : if (func_call == NULL_TREE)
8531 : 528 : func_call = build_function_call_vec (loc, vNULL, fndecl, params, NULL);
8532 : 528 : old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
8533 : : NULL_TREE);
8534 : 528 : add_stmt (old);
8535 : 528 : params->truncate (0);
8536 : :
8537 : : /* loop: */
8538 : 528 : add_stmt (loop_label);
8539 : :
8540 : : /* newval = old + val; */
8541 : 528 : tree rhs;
8542 : 528 : switch (code)
8543 : : {
8544 : 211 : case PLUS_EXPR:
8545 : 211 : case MINUS_EXPR:
8546 : 211 : if (!TYPE_OVERFLOW_WRAPS (nonatomic_lhs_type))
8547 : : {
8548 : 211 : tree utype
8549 : 211 : = build_bitint_type (TYPE_PRECISION (nonatomic_lhs_type), 1);
8550 : 211 : rhs = convert (nonatomic_lhs_type,
8551 : : build2_loc (loc, code, utype,
8552 : : convert (utype, old),
8553 : : convert (utype, val)));
8554 : : }
8555 : : else
8556 : 0 : rhs = build2_loc (loc, code, nonatomic_lhs_type, old, val);
8557 : : break;
8558 : 316 : case BIT_AND_EXPR:
8559 : 316 : case BIT_IOR_EXPR:
8560 : 316 : case BIT_XOR_EXPR:
8561 : 316 : rhs = build2_loc (loc, code, nonatomic_lhs_type, old, val);
8562 : 316 : break;
8563 : 1 : case ERROR_MARK:
8564 : 1 : rhs = build2_loc (loc, BIT_AND_EXPR, nonatomic_lhs_type,
8565 : : build1_loc (loc, BIT_NOT_EXPR,
8566 : : nonatomic_lhs_type, old), val);
8567 : 1 : break;
8568 : 0 : default:
8569 : 0 : gcc_unreachable ();
8570 : : }
8571 : 528 : rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
8572 : : NULL_TREE);
8573 : 528 : SET_EXPR_LOCATION (rhs, loc);
8574 : 528 : add_stmt (rhs);
8575 : :
8576 : : /* if (__atomic_compare_exchange (addr, &old, &new, false, model, model))
8577 : : goto done; */
8578 : 528 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
8579 : 528 : params->quick_push (lhs_addr);
8580 : 528 : params->quick_push (old_addr);
8581 : 528 : params->quick_push (newval_addr);
8582 : 528 : params->quick_push (integer_zero_node);
8583 : 528 : params->quick_push (model);
8584 : 528 : if (tree_fits_uhwi_p (model)
8585 : 527 : && (tree_to_uhwi (model) == MEMMODEL_RELEASE
8586 : 447 : || tree_to_uhwi (model) == MEMMODEL_ACQ_REL))
8587 : 129 : params->quick_push (build_int_cst (integer_type_node, MEMMODEL_RELAXED));
8588 : : else
8589 : 399 : params->quick_push (model);
8590 : 528 : func_call = resolve_overloaded_builtin (loc, fndecl, params);
8591 : 528 : if (func_call == NULL_TREE)
8592 : 528 : func_call = build_function_call_vec (loc, vNULL, fndecl, params, NULL);
8593 : :
8594 : 528 : tree goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
8595 : 528 : SET_EXPR_LOCATION (goto_stmt, loc);
8596 : :
8597 : 528 : tree stmt
8598 : 528 : = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
8599 : 528 : SET_EXPR_LOCATION (stmt, loc);
8600 : 528 : add_stmt (stmt);
8601 : :
8602 : : /* goto loop; */
8603 : 528 : goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
8604 : 528 : SET_EXPR_LOCATION (goto_stmt, loc);
8605 : 528 : add_stmt (goto_stmt);
8606 : :
8607 : : /* done: */
8608 : 528 : add_stmt (done_label);
8609 : :
8610 : 528 : tree ret = create_tmp_var_raw (nonatomic_lhs_type);
8611 : 531 : stmt = build2_loc (loc, MODIFY_EXPR, void_type_node, ret,
8612 : : return_old_p ? old : newval);
8613 : 528 : add_stmt (stmt);
8614 : :
8615 : : /* Finish the compound statement. */
8616 : 528 : stmts = pop_stmt_list (stmts);
8617 : :
8618 : 528 : return build4 (TARGET_EXPR, nonatomic_lhs_type, ret, stmts, NULL_TREE,
8619 : 528 : NULL_TREE);
8620 : : }
8621 : :
8622 : :
8623 : : /* Some builtin functions are placeholders for other expressions. This
8624 : : function should be called immediately after parsing the call expression
8625 : : before surrounding code has committed to the type of the expression.
8626 : :
8627 : : LOC is the location of the builtin call.
8628 : :
8629 : : FUNCTION is the DECL that has been invoked; it is known to be a builtin.
8630 : : PARAMS is the argument list for the call. The return value is non-null
8631 : : when expansion is complete, and null if normal processing should
8632 : : continue. */
8633 : :
8634 : : tree
8635 : 58625329 : resolve_overloaded_builtin (location_t loc, tree function,
8636 : : vec<tree, va_gc> *params, bool complain)
8637 : : {
8638 : : /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
8639 : : Those are not valid to call with a pointer to _Bool (or C++ bool)
8640 : : and so must be rejected. */
8641 : 58625329 : bool fetch_op = true;
8642 : 58625329 : bool orig_format = true;
8643 : 58625329 : tree new_return = NULL_TREE;
8644 : :
8645 : 58625329 : switch (DECL_BUILT_IN_CLASS (function))
8646 : : {
8647 : 9242513 : case BUILT_IN_NORMAL:
8648 : 9242513 : break;
8649 : 32900561 : case BUILT_IN_MD:
8650 : 32900561 : if (targetm.resolve_overloaded_builtin)
8651 : 0 : return targetm.resolve_overloaded_builtin (loc, function, params,
8652 : 0 : complain);
8653 : : else
8654 : : return NULL_TREE;
8655 : : default:
8656 : : return NULL_TREE;
8657 : : }
8658 : :
8659 : : /* Handle BUILT_IN_NORMAL here. */
8660 : 9242513 : enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
8661 : 9242513 : switch (orig_code)
8662 : : {
8663 : 154 : case BUILT_IN_SPECULATION_SAFE_VALUE_N:
8664 : 154 : {
8665 : 154 : tree new_function, first_param, result;
8666 : 154 : enum built_in_function fncode
8667 : 154 : = speculation_safe_value_resolve_call (function, params, complain);
8668 : :
8669 : 154 : if (fncode == BUILT_IN_NONE)
8670 : 52 : return error_mark_node;
8671 : :
8672 : 102 : first_param = (*params)[0];
8673 : 102 : if (!speculation_safe_value_resolve_params (loc, function, params,
8674 : : complain))
8675 : 31 : return error_mark_node;
8676 : :
8677 : 71 : if (targetm.have_speculation_safe_value (true))
8678 : : {
8679 : 71 : new_function = builtin_decl_explicit (fncode);
8680 : 71 : result = build_function_call_vec (loc, vNULL, new_function, params,
8681 : : NULL);
8682 : :
8683 : 71 : if (result == error_mark_node)
8684 : : return result;
8685 : :
8686 : 71 : return speculation_safe_value_resolve_return (first_param, result);
8687 : : }
8688 : : else
8689 : : {
8690 : : /* This target doesn't have, or doesn't need, active mitigation
8691 : : against incorrect speculative execution. Simply return the
8692 : : first parameter to the builtin. */
8693 : 0 : if (!targetm.have_speculation_safe_value (false))
8694 : : {
8695 : 0 : if (complain)
8696 : : /* The user has invoked __builtin_speculation_safe_value
8697 : : even though __HAVE_SPECULATION_SAFE_VALUE is not
8698 : : defined: emit a warning. */
8699 : 0 : warning_at (
8700 : 0 : input_location, 0,
8701 : : "this target does not define a speculation barrier; "
8702 : : "your program will still execute correctly, "
8703 : : "but incorrect speculation may not be "
8704 : : "restricted");
8705 : : else
8706 : 0 : return error_mark_node;
8707 : : }
8708 : :
8709 : : /* If the optional second argument is present, handle any side
8710 : : effects now. */
8711 : 0 : if (params->length () == 2
8712 : 0 : && TREE_SIDE_EFFECTS ((*params)[1]))
8713 : 0 : return build2 (COMPOUND_EXPR, TREE_TYPE (first_param),
8714 : 0 : (*params)[1], first_param);
8715 : :
8716 : : return first_param;
8717 : : }
8718 : : }
8719 : :
8720 : 85199 : case BUILT_IN_ATOMIC_EXCHANGE:
8721 : 85199 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
8722 : 85199 : case BUILT_IN_ATOMIC_LOAD:
8723 : 85199 : case BUILT_IN_ATOMIC_STORE:
8724 : 85199 : {
8725 : : /* Handle these 4 together so that they can fall through to the next
8726 : : case if the call is transformed to an _N variant. */
8727 : 85199 : switch (orig_code)
8728 : : {
8729 : 654 : case BUILT_IN_ATOMIC_EXCHANGE:
8730 : 654 : {
8731 : 654 : if (resolve_overloaded_atomic_exchange (loc, function, params,
8732 : : &new_return, complain))
8733 : 234 : return new_return;
8734 : : /* Change to the _N variant. */
8735 : : orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
8736 : : break;
8737 : : }
8738 : :
8739 : 21815 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
8740 : 21815 : {
8741 : 21815 : if (resolve_overloaded_atomic_compare_exchange (
8742 : : loc, function, params, &new_return, complain))
8743 : 1376 : return new_return;
8744 : : /* Change to the _N variant. */
8745 : : orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
8746 : : break;
8747 : : }
8748 : 51229 : case BUILT_IN_ATOMIC_LOAD:
8749 : 51229 : {
8750 : 51229 : if (resolve_overloaded_atomic_load (loc, function, params,
8751 : : &new_return, complain))
8752 : 2777 : return new_return;
8753 : : /* Change to the _N variant. */
8754 : : orig_code = BUILT_IN_ATOMIC_LOAD_N;
8755 : : break;
8756 : : }
8757 : 11501 : case BUILT_IN_ATOMIC_STORE:
8758 : 11501 : {
8759 : 11501 : if (resolve_overloaded_atomic_store (loc, function, params,
8760 : : &new_return, complain))
8761 : 578 : return new_return;
8762 : : /* Change to the _N variant. */
8763 : : orig_code = BUILT_IN_ATOMIC_STORE_N;
8764 : : break;
8765 : : }
8766 : 0 : default:
8767 : 0 : gcc_unreachable ();
8768 : : }
8769 : : }
8770 : : /* FALLTHRU */
8771 : : case BUILT_IN_ATOMIC_EXCHANGE_N:
8772 : : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
8773 : : case BUILT_IN_ATOMIC_LOAD_N:
8774 : : case BUILT_IN_ATOMIC_STORE_N:
8775 : : fetch_op = false;
8776 : : /* FALLTHRU */
8777 : : case BUILT_IN_ATOMIC_ADD_FETCH_N:
8778 : : case BUILT_IN_ATOMIC_SUB_FETCH_N:
8779 : : case BUILT_IN_ATOMIC_AND_FETCH_N:
8780 : : case BUILT_IN_ATOMIC_NAND_FETCH_N:
8781 : : case BUILT_IN_ATOMIC_XOR_FETCH_N:
8782 : : case BUILT_IN_ATOMIC_OR_FETCH_N:
8783 : : case BUILT_IN_ATOMIC_FETCH_ADD_N:
8784 : : case BUILT_IN_ATOMIC_FETCH_SUB_N:
8785 : : case BUILT_IN_ATOMIC_FETCH_AND_N:
8786 : : case BUILT_IN_ATOMIC_FETCH_NAND_N:
8787 : : case BUILT_IN_ATOMIC_FETCH_XOR_N:
8788 : : case BUILT_IN_ATOMIC_FETCH_OR_N:
8789 : : orig_format = false;
8790 : : /* FALLTHRU */
8791 : : case BUILT_IN_SYNC_FETCH_AND_ADD_N:
8792 : : case BUILT_IN_SYNC_FETCH_AND_SUB_N:
8793 : : case BUILT_IN_SYNC_FETCH_AND_OR_N:
8794 : : case BUILT_IN_SYNC_FETCH_AND_AND_N:
8795 : : case BUILT_IN_SYNC_FETCH_AND_XOR_N:
8796 : : case BUILT_IN_SYNC_FETCH_AND_NAND_N:
8797 : : case BUILT_IN_SYNC_ADD_AND_FETCH_N:
8798 : : case BUILT_IN_SYNC_SUB_AND_FETCH_N:
8799 : : case BUILT_IN_SYNC_OR_AND_FETCH_N:
8800 : : case BUILT_IN_SYNC_AND_AND_FETCH_N:
8801 : : case BUILT_IN_SYNC_XOR_AND_FETCH_N:
8802 : : case BUILT_IN_SYNC_NAND_AND_FETCH_N:
8803 : : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
8804 : : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
8805 : : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
8806 : : case BUILT_IN_SYNC_LOCK_RELEASE_N:
8807 : : {
8808 : : /* The following are not _FETCH_OPs and must be accepted with
8809 : : pointers to _Bool (or C++ bool). */
8810 : : if (fetch_op)
8811 : 69901 : fetch_op = (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
8812 : 69901 : && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
8813 : 69415 : && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
8814 : 69901 : && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
8815 : :
8816 : 330425 : int n = sync_resolve_size (function, params, fetch_op, orig_format,
8817 : : complain);
8818 : 330425 : tree new_function, first_param, result;
8819 : 330425 : enum built_in_function fncode;
8820 : :
8821 : 330425 : if (n == 0)
8822 : 2609 : return error_mark_node;
8823 : :
8824 : 327816 : if (n == -1)
8825 : : {
8826 : : /* complain is related to SFINAE context.
8827 : : _BitInt is not defined in C++, hence can't enter this clause
8828 : : with complain unset. Even if at the abstraction level
8829 : : this complain is unset that still makes sense (whether
8830 : : this function should report an error or not if anything is
8831 : : wrong).
8832 : : Since can't test avoiding an error when this value is false not
8833 : : writing the code and instead asserting value is not set. */
8834 : 528 : gcc_assert (complain);
8835 : 528 : return atomic_bitint_fetch_using_cas_loop (loc, orig_code, function,
8836 : 528 : params);
8837 : : }
8838 : :
8839 : 327288 : fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
8840 : 327288 : new_function = builtin_decl_explicit (fncode);
8841 : 327288 : if (!sync_resolve_params (loc, function, new_function, params,
8842 : : orig_format, complain))
8843 : 164 : return error_mark_node;
8844 : :
8845 : 327124 : first_param = (*params)[0];
8846 : 327124 : result = build_function_call_vec (loc, vNULL, new_function, params,
8847 : : NULL);
8848 : 327124 : if (result == error_mark_node)
8849 : : return result;
8850 : 327112 : if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
8851 : 327112 : && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
8852 : 326805 : && orig_code != BUILT_IN_ATOMIC_STORE_N
8853 : 326805 : && orig_code != BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N)
8854 : 216609 : result = sync_resolve_return (first_param, result, orig_format);
8855 : :
8856 : 327112 : if (fetch_op)
8857 : : /* Prevent -Wunused-value warning. */
8858 : 66394 : TREE_USED (result) = true;
8859 : :
8860 : : /* If new_return is set, assign function to that expr and cast the
8861 : : result to void since the generic interface returned void. */
8862 : 327112 : if (new_return)
8863 : : {
8864 : : /* Cast function result from I{1,2,4,8,16} to the required type. */
8865 : 48872 : if (TREE_CODE (TREE_TYPE (new_return)) == BITINT_TYPE)
8866 : : {
8867 : 599 : struct bitint_info info;
8868 : 599 : unsigned prec = TYPE_PRECISION (TREE_TYPE (new_return));
8869 : 599 : targetm.c.bitint_type_info (prec, &info);
8870 : 599 : if (!info.extended)
8871 : : /* For _BitInt which has the padding bits undefined
8872 : : convert to the _BitInt type rather than VCE to force
8873 : : zero or sign extension. */
8874 : 599 : result = build1 (NOP_EXPR, TREE_TYPE (new_return), result);
8875 : : }
8876 : 48872 : result
8877 : 48872 : = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
8878 : 48872 : result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
8879 : : result);
8880 : 48872 : TREE_SIDE_EFFECTS (result) = 1;
8881 : 48872 : protected_set_expr_location (result, loc);
8882 : 48872 : result = convert (void_type_node, result);
8883 : : }
8884 : : return result;
8885 : : }
8886 : :
8887 : : default:
8888 : : return NULL_TREE;
8889 : : }
8890 : : }
8891 : :
8892 : : /* vector_types_compatible_elements_p is used in type checks of vectors
8893 : : values used as operands of binary operators. Where it returns true, and
8894 : : the other checks of the caller succeed (being vector types in he first
8895 : : place, and matching number of elements), we can just treat the types
8896 : : as essentially the same.
8897 : : Contrast with vector_targets_convertible_p, which is used for vector
8898 : : pointer types, and vector_types_convertible_p, which will allow
8899 : : language-specific matches under the control of flag_lax_vector_conversions,
8900 : : and might still require a conversion. */
8901 : : /* True if vector types T1 and T2 can be inputs to the same binary
8902 : : operator without conversion.
8903 : : We don't check the overall vector size here because some of our callers
8904 : : want to give different error messages when the vectors are compatible
8905 : : except for the element count. */
8906 : :
8907 : : bool
8908 : 1633464 : vector_types_compatible_elements_p (tree t1, tree t2)
8909 : : {
8910 : 3266652 : bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
8911 : 1633464 : t1 = TREE_TYPE (t1);
8912 : 1633464 : t2 = TREE_TYPE (t2);
8913 : :
8914 : 1633464 : enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
8915 : :
8916 : 1633464 : gcc_assert ((INTEGRAL_TYPE_P (t1)
8917 : : || c1 == REAL_TYPE
8918 : : || c1 == FIXED_POINT_TYPE)
8919 : : && (INTEGRAL_TYPE_P (t2)
8920 : : || c2 == REAL_TYPE
8921 : : || c2 == FIXED_POINT_TYPE));
8922 : :
8923 : 1633464 : t1 = c_common_signed_type (t1);
8924 : 1633464 : t2 = c_common_signed_type (t2);
8925 : : /* Equality works here because c_common_signed_type uses
8926 : : TYPE_MAIN_VARIANT. */
8927 : 1633464 : if (t1 == t2)
8928 : : return true;
8929 : 31 : if (opaque && c1 == c2
8930 : 4 : && (INTEGRAL_TYPE_P (t1) || c1 == REAL_TYPE)
8931 : 35 : && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
8932 : 4 : return true;
8933 : : return false;
8934 : : }
8935 : :
8936 : : /* Check for missing format attributes on function pointers. LTYPE is
8937 : : the new type or left-hand side type. RTYPE is the old type or
8938 : : right-hand side type. Returns TRUE if LTYPE is missing the desired
8939 : : attribute. */
8940 : :
8941 : : bool
8942 : 9934 : check_missing_format_attribute (tree ltype, tree rtype)
8943 : : {
8944 : 9934 : tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
8945 : 9934 : tree ra;
8946 : :
8947 : 9943 : for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
8948 : 73 : if (is_attribute_p ("format", get_attribute_name (ra)))
8949 : : break;
8950 : 9934 : if (ra)
8951 : : {
8952 : 64 : tree la;
8953 : 64 : for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
8954 : 24 : if (is_attribute_p ("format", get_attribute_name (la)))
8955 : : break;
8956 : 64 : return !la;
8957 : : }
8958 : : else
8959 : : return false;
8960 : : }
8961 : :
8962 : : /* Setup a TYPE_DECL node as a typedef representation.
8963 : :
8964 : : X is a TYPE_DECL for a typedef statement. Create a brand new
8965 : : ..._TYPE node (which will be just a variant of the existing
8966 : : ..._TYPE node with identical properties) and then install X
8967 : : as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
8968 : :
8969 : : The whole point here is to end up with a situation where each
8970 : : and every ..._TYPE node the compiler creates will be uniquely
8971 : : associated with AT MOST one node representing a typedef name.
8972 : : This way, even though the compiler substitutes corresponding
8973 : : ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
8974 : : early on, later parts of the compiler can always do the reverse
8975 : : translation and get back the corresponding typedef name. For
8976 : : example, given:
8977 : :
8978 : : typedef struct S MY_TYPE;
8979 : : MY_TYPE object;
8980 : :
8981 : : Later parts of the compiler might only know that `object' was of
8982 : : type `struct S' if it were not for code just below. With this
8983 : : code however, later parts of the compiler see something like:
8984 : :
8985 : : struct S' == struct S
8986 : : typedef struct S' MY_TYPE;
8987 : : struct S' object;
8988 : :
8989 : : And they can then deduce (from the node for type struct S') that
8990 : : the original object declaration was:
8991 : :
8992 : : MY_TYPE object;
8993 : :
8994 : : Being able to do this is important for proper support of protoize,
8995 : : and also for generating precise symbolic debugging information
8996 : : which takes full account of the programmer's (typedef) vocabulary.
8997 : :
8998 : : Obviously, we don't want to generate a duplicate ..._TYPE node if
8999 : : the TYPE_DECL node that we are now processing really represents a
9000 : : standard built-in type. */
9001 : :
9002 : : void
9003 : 180904860 : set_underlying_type (tree x)
9004 : : {
9005 : 180904860 : if (x == error_mark_node || TREE_TYPE (x) == error_mark_node)
9006 : : return;
9007 : 180904832 : if (DECL_IS_UNDECLARED_BUILTIN (x) && TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
9008 : : {
9009 : 6407053 : if (TYPE_NAME (TREE_TYPE (x)) == 0)
9010 : 5118568 : TYPE_NAME (TREE_TYPE (x)) = x;
9011 : : }
9012 : 174497779 : else if (DECL_ORIGINAL_TYPE (x))
9013 : 165802 : gcc_checking_assert (TYPE_NAME (TREE_TYPE (x)) == x);
9014 : : else
9015 : : {
9016 : 174331977 : tree tt = TREE_TYPE (x);
9017 : 174331977 : DECL_ORIGINAL_TYPE (x) = tt;
9018 : 174331977 : tt = build_variant_type_copy (tt);
9019 : 174331977 : TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
9020 : 174331977 : TYPE_NAME (tt) = x;
9021 : :
9022 : : /* Mark the type as used only when its type decl is decorated
9023 : : with attribute unused. */
9024 : 174331977 : if (lookup_attribute ("unused", DECL_ATTRIBUTES (x)))
9025 : 258 : TREE_USED (tt) = 1;
9026 : :
9027 : 174331977 : TREE_TYPE (x) = tt;
9028 : : }
9029 : : }
9030 : :
9031 : : /* Return true if it is worth exposing the DECL_ORIGINAL_TYPE of TYPE to
9032 : : the user in diagnostics, false if it would be better to use TYPE itself.
9033 : : TYPE is known to satisfy typedef_variant_p. */
9034 : :
9035 : : bool
9036 : 32785 : user_facing_original_type_p (const_tree type)
9037 : : {
9038 : 32785 : gcc_assert (typedef_variant_p (type));
9039 : 32785 : tree decl = TYPE_NAME (type);
9040 : :
9041 : : /* Look through any typedef in "user" code. */
9042 : 32785 : if (!DECL_IN_SYSTEM_HEADER (decl) && !DECL_IS_UNDECLARED_BUILTIN (decl))
9043 : : return true;
9044 : :
9045 : : /* If the original type is also named and is in the user namespace,
9046 : : assume it too is a user-facing type. */
9047 : 1623 : tree orig_type = DECL_ORIGINAL_TYPE (decl);
9048 : 1623 : if (tree orig_id = TYPE_IDENTIFIER (orig_type))
9049 : 1495 : if (!name_reserved_for_implementation_p (IDENTIFIER_POINTER (orig_id)))
9050 : : return true;
9051 : :
9052 : 706 : switch (TREE_CODE (orig_type))
9053 : : {
9054 : : /* Don't look through to an anonymous vector type, since the syntax
9055 : : we use for them in diagnostics isn't real C or C++ syntax.
9056 : : And if ORIG_TYPE is named but in the implementation namespace,
9057 : : TYPE is likely to be more meaningful to the user. */
9058 : : case VECTOR_TYPE:
9059 : : return false;
9060 : :
9061 : : /* Don't expose anonymous tag types that are presumably meant to be
9062 : : known by their typedef name. Also don't expose tags that are in
9063 : : the implementation namespace, such as:
9064 : :
9065 : : typedef struct __foo foo; */
9066 : : case RECORD_TYPE:
9067 : : case UNION_TYPE:
9068 : : case ENUMERAL_TYPE:
9069 : : return false;
9070 : :
9071 : : /* Look through to anything else. */
9072 : : default:
9073 : : return true;
9074 : : }
9075 : : }
9076 : :
9077 : : /* Record the types used by the current global variable declaration
9078 : : being parsed, so that we can decide later to emit their debug info.
9079 : : Those types are in types_used_by_cur_var_decl, and we are going to
9080 : : store them in the types_used_by_vars_hash hash table.
9081 : : DECL is the declaration of the global variable that has been parsed. */
9082 : :
9083 : : void
9084 : 26234188 : record_types_used_by_current_var_decl (tree decl)
9085 : : {
9086 : 26234188 : gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
9087 : :
9088 : 48855593 : while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
9089 : : {
9090 : 22621405 : tree type = types_used_by_cur_var_decl->pop ();
9091 : 22621405 : types_used_by_var_decl_insert (type, decl);
9092 : : }
9093 : 26234188 : }
9094 : :
9095 : : /* The C and C++ parsers both use vectors to hold function arguments.
9096 : : For efficiency, we keep a cache of unused vectors. This is the
9097 : : cache. */
9098 : :
9099 : : typedef vec<tree, va_gc> *tree_gc_vec;
9100 : : static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
9101 : :
9102 : : /* Return a new vector from the cache. If the cache is empty,
9103 : : allocate a new vector. These vectors are GC'ed, so it is OK if the
9104 : : pointer is not released.. */
9105 : :
9106 : : vec<tree, va_gc> *
9107 : 1129187405 : make_tree_vector (void)
9108 : : {
9109 : 1129187405 : if (tree_vector_cache && !tree_vector_cache->is_empty ())
9110 : 1097254551 : return tree_vector_cache->pop ();
9111 : : else
9112 : : {
9113 : : /* Passing 0 to vec::alloc returns NULL, and our callers require
9114 : : that we always return a non-NULL value. The vector code uses
9115 : : 4 when growing a NULL vector, so we do too. */
9116 : 31932854 : vec<tree, va_gc> *v;
9117 : 31932854 : vec_alloc (v, 4);
9118 : 31932854 : return v;
9119 : : }
9120 : : }
9121 : :
9122 : : /* Release a vector of trees back to the cache. */
9123 : :
9124 : : void
9125 : 1523408412 : release_tree_vector (vec<tree, va_gc> *vec)
9126 : : {
9127 : 1523408412 : if (vec != NULL)
9128 : : {
9129 : 1099642627 : if (vec->allocated () >= 16)
9130 : : /* Don't cache vecs that have expanded more than once. On a p64
9131 : : target, vecs double in alloc size with each power of 2 elements, e.g
9132 : : at 16 elements the alloc increases from 128 to 256 bytes. */
9133 : 1560397 : vec_free (vec);
9134 : : else
9135 : : {
9136 : 1098082230 : vec->truncate (0);
9137 : 1098082230 : vec_safe_push (tree_vector_cache, vec);
9138 : : }
9139 : : }
9140 : 1523408412 : }
9141 : :
9142 : : /* Get a new tree vector holding a single tree. */
9143 : :
9144 : : vec<tree, va_gc> *
9145 : 54165668 : make_tree_vector_single (tree t)
9146 : : {
9147 : 54165668 : vec<tree, va_gc> *ret = make_tree_vector ();
9148 : 54165668 : ret->quick_push (t);
9149 : 54165668 : return ret;
9150 : : }
9151 : :
9152 : : /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
9153 : :
9154 : : vec<tree, va_gc> *
9155 : 9296 : make_tree_vector_from_list (tree list)
9156 : : {
9157 : 9296 : vec<tree, va_gc> *ret = make_tree_vector ();
9158 : 24670 : for (; list; list = TREE_CHAIN (list))
9159 : 15374 : vec_safe_push (ret, TREE_VALUE (list));
9160 : 9296 : return ret;
9161 : : }
9162 : :
9163 : : /* Append to a tree vector V the values of a CONSTRUCTOR CTOR
9164 : : and return the new possibly reallocated vector. */
9165 : :
9166 : : vec<tree, va_gc> *
9167 : 1464394 : append_ctor_to_tree_vector (vec<tree, va_gc> *v, tree ctor)
9168 : : {
9169 : 2750709 : unsigned nelts = vec_safe_length (v) + CONSTRUCTOR_NELTS (ctor);
9170 : 1464394 : vec_safe_reserve (v, CONSTRUCTOR_NELTS (ctor));
9171 : 3609362 : for (unsigned i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
9172 : 2144968 : if (TREE_CODE (CONSTRUCTOR_ELT (ctor, i)->value) == RAW_DATA_CST)
9173 : : {
9174 : 24 : tree raw_data = CONSTRUCTOR_ELT (ctor, i)->value;
9175 : 24 : nelts += RAW_DATA_LENGTH (raw_data) - 1;
9176 : 24 : vec_safe_reserve (v, nelts - v->length ());
9177 : 48 : if (TYPE_PRECISION (TREE_TYPE (raw_data)) > CHAR_BIT
9178 : 24 : || TYPE_UNSIGNED (TREE_TYPE (raw_data)))
9179 : 6264 : for (unsigned j = 0; j < (unsigned) RAW_DATA_LENGTH (raw_data); ++j)
9180 : 6240 : v->quick_push (build_int_cst (TREE_TYPE (raw_data),
9181 : 6240 : RAW_DATA_UCHAR_ELT (raw_data, j)));
9182 : : else
9183 : 0 : for (unsigned j = 0; j < (unsigned) RAW_DATA_LENGTH (raw_data); ++j)
9184 : 0 : v->quick_push (build_int_cst (TREE_TYPE (raw_data),
9185 : 0 : RAW_DATA_SCHAR_ELT (raw_data, j)));
9186 : : }
9187 : : else
9188 : 2144944 : v->quick_push (CONSTRUCTOR_ELT (ctor, i)->value);
9189 : 1464394 : return v;
9190 : : }
9191 : :
9192 : : /* Get a new tree vector of the values of a CONSTRUCTOR. */
9193 : :
9194 : : vec<tree, va_gc> *
9195 : 6551 : make_tree_vector_from_ctor (tree ctor)
9196 : : {
9197 : 6551 : vec<tree,va_gc> *ret
9198 : 6551 : = CONSTRUCTOR_NELTS (ctor) <= 16 ? make_tree_vector () : NULL;
9199 : 6551 : return append_ctor_to_tree_vector (ret, ctor);
9200 : : }
9201 : :
9202 : : /* Get a new tree vector which is a copy of an existing one. */
9203 : :
9204 : : vec<tree, va_gc> *
9205 : 29039460 : make_tree_vector_copy (const vec<tree, va_gc> *orig)
9206 : : {
9207 : 29039460 : vec<tree, va_gc> *ret;
9208 : 29039460 : unsigned int ix;
9209 : 29039460 : tree t;
9210 : :
9211 : 29039460 : ret = make_tree_vector ();
9212 : 29039460 : vec_safe_reserve (ret, vec_safe_length (orig));
9213 : 70659391 : FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
9214 : 12580471 : ret->quick_push (t);
9215 : 29039460 : return ret;
9216 : : }
9217 : :
9218 : : /* Return true if KEYWORD starts a type specifier. */
9219 : :
9220 : : bool
9221 : 16 : keyword_begins_type_specifier (enum rid keyword)
9222 : : {
9223 : 16 : switch (keyword)
9224 : : {
9225 : : case RID_AUTO_TYPE:
9226 : : case RID_INT:
9227 : : case RID_CHAR:
9228 : : case RID_FLOAT:
9229 : : case RID_DOUBLE:
9230 : : case RID_VOID:
9231 : : case RID_UNSIGNED:
9232 : : case RID_LONG:
9233 : : case RID_SHORT:
9234 : : case RID_SIGNED:
9235 : : CASE_RID_FLOATN_NX:
9236 : : case RID_DFLOAT32:
9237 : : case RID_DFLOAT64:
9238 : : case RID_DFLOAT128:
9239 : : case RID_FRACT:
9240 : : case RID_ACCUM:
9241 : : case RID_BOOL:
9242 : : case RID_BITINT:
9243 : : case RID_WCHAR:
9244 : : case RID_CHAR8:
9245 : : case RID_CHAR16:
9246 : : case RID_CHAR32:
9247 : : case RID_SAT:
9248 : : case RID_COMPLEX:
9249 : : case RID_TYPEOF:
9250 : : case RID_STRUCT:
9251 : : case RID_CLASS:
9252 : : case RID_UNION:
9253 : : case RID_ENUM:
9254 : : return true;
9255 : 1 : default:
9256 : 1 : if (keyword >= RID_FIRST_INT_N
9257 : : && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
9258 : 0 : && int_n_enabled_p[keyword-RID_FIRST_INT_N])
9259 : : return true;
9260 : : return false;
9261 : : }
9262 : : }
9263 : :
9264 : : /* Return true if KEYWORD names a type qualifier. */
9265 : :
9266 : : bool
9267 : 12127 : keyword_is_type_qualifier (enum rid keyword)
9268 : : {
9269 : 12127 : switch (keyword)
9270 : : {
9271 : : case RID_CONST:
9272 : : case RID_VOLATILE:
9273 : : case RID_RESTRICT:
9274 : : case RID_ATOMIC:
9275 : : return true;
9276 : 58 : default:
9277 : 58 : return false;
9278 : : }
9279 : : }
9280 : :
9281 : : /* Return true if KEYWORD names a storage class specifier.
9282 : :
9283 : : RID_TYPEDEF is not included in this list despite `typedef' being
9284 : : listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
9285 : : such for syntactic convenience only. */
9286 : :
9287 : : bool
9288 : 12154 : keyword_is_storage_class_specifier (enum rid keyword)
9289 : : {
9290 : 12154 : switch (keyword)
9291 : : {
9292 : : case RID_STATIC:
9293 : : case RID_EXTERN:
9294 : : case RID_REGISTER:
9295 : : case RID_AUTO:
9296 : : case RID_MUTABLE:
9297 : : case RID_THREAD:
9298 : : return true;
9299 : 12127 : default:
9300 : 12127 : return false;
9301 : : }
9302 : : }
9303 : :
9304 : : /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
9305 : :
9306 : : static bool
9307 : 58 : keyword_is_function_specifier (enum rid keyword)
9308 : : {
9309 : 58 : switch (keyword)
9310 : : {
9311 : : case RID_INLINE:
9312 : : case RID_NORETURN:
9313 : : case RID_VIRTUAL:
9314 : : case RID_EXPLICIT:
9315 : : return true;
9316 : 56 : default:
9317 : 0 : return false;
9318 : : }
9319 : : }
9320 : :
9321 : : /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
9322 : : declaration-specifier (C99 6.7). */
9323 : :
9324 : : bool
9325 : 12154 : keyword_is_decl_specifier (enum rid keyword)
9326 : : {
9327 : 12154 : if (keyword_is_storage_class_specifier (keyword)
9328 : 12127 : || keyword_is_type_qualifier (keyword)
9329 : 12154 : || keyword_is_function_specifier (keyword))
9330 : 12098 : return true;
9331 : :
9332 : 56 : switch (keyword)
9333 : : {
9334 : : case RID_TYPEDEF:
9335 : : case RID_FRIEND:
9336 : : case RID_CONSTEXPR:
9337 : : case RID_CONSTINIT:
9338 : : return true;
9339 : : default:
9340 : : return false;
9341 : : }
9342 : : }
9343 : :
9344 : : /* Initialize language-specific-bits of tree_contains_struct. */
9345 : :
9346 : : void
9347 : 211321 : c_common_init_ts (void)
9348 : : {
9349 : 211321 : MARK_TS_EXP (SIZEOF_EXPR);
9350 : 211321 : MARK_TS_EXP (PAREN_SIZEOF_EXPR);
9351 : 211321 : MARK_TS_EXP (C_MAYBE_CONST_EXPR);
9352 : 211321 : MARK_TS_EXP (EXCESS_PRECISION_EXPR);
9353 : 211321 : MARK_TS_EXP (BREAK_STMT);
9354 : 211321 : MARK_TS_EXP (CONTINUE_STMT);
9355 : 211321 : MARK_TS_EXP (DO_STMT);
9356 : 211321 : MARK_TS_EXP (FOR_STMT);
9357 : 211321 : MARK_TS_EXP (SWITCH_STMT);
9358 : 211321 : MARK_TS_EXP (WHILE_STMT);
9359 : :
9360 : 211321 : MARK_TS_DECL_COMMON (CONCEPT_DECL);
9361 : 211321 : }
9362 : :
9363 : : /* Build a user-defined numeric literal out of an integer constant type VALUE
9364 : : with identifier SUFFIX. */
9365 : :
9366 : : tree
9367 : 964487 : build_userdef_literal (tree suffix_id, tree value,
9368 : : enum overflow_type overflow, tree num_string)
9369 : : {
9370 : 964487 : tree literal = make_node (USERDEF_LITERAL);
9371 : 964487 : USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
9372 : 964487 : USERDEF_LITERAL_VALUE (literal) = value;
9373 : 964487 : USERDEF_LITERAL_OVERFLOW (literal) = overflow;
9374 : 964487 : USERDEF_LITERAL_NUM_STRING (literal) = num_string;
9375 : 964487 : return literal;
9376 : : }
9377 : :
9378 : : /* For vector[index], convert the vector to an array of the underlying type.
9379 : : Return true if the resulting ARRAY_REF should not be an lvalue. */
9380 : :
9381 : : bool
9382 : 8940289 : convert_vector_to_array_for_subscript (location_t loc,
9383 : : tree *vecp, tree index)
9384 : : {
9385 : 8940289 : bool ret = false;
9386 : 8940289 : if (gnu_vector_type_p (TREE_TYPE (*vecp)))
9387 : : {
9388 : 1064601 : tree type = TREE_TYPE (*vecp);
9389 : 1064601 : tree newitype;
9390 : :
9391 : 1064601 : ret = !lvalue_p (*vecp);
9392 : :
9393 : 1064601 : index = fold_for_warn (index);
9394 : : /* Warn out-of-bounds index for vectors only if known. */
9395 : 1064601 : if (poly_int_tree_p (index))
9396 : 1051752 : if (!tree_fits_poly_uint64_p (index)
9397 : 1051752 : || known_ge (tree_to_poly_uint64 (index),
9398 : : TYPE_VECTOR_SUBPARTS (type)))
9399 : 84 : warning_at (loc, OPT_Warray_bounds_, "index value is out of bound");
9400 : :
9401 : : /* We are building an ARRAY_REF so mark the vector as addressable
9402 : : to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
9403 : : for function parameters. */
9404 : 1064601 : c_common_mark_addressable_vec (*vecp);
9405 : :
9406 : : /* Make sure qualifiers are copied from the vector type to the new element
9407 : : of the array type. */
9408 : 1064601 : newitype = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
9409 : :
9410 : 1064601 : *vecp = build1 (VIEW_CONVERT_EXPR,
9411 : : build_array_type_nelts (newitype,
9412 : : TYPE_VECTOR_SUBPARTS (type)),
9413 : : *vecp);
9414 : : }
9415 : 8940289 : return ret;
9416 : : }
9417 : :
9418 : : /* Determine which of the operands, if any, is a scalar that needs to be
9419 : : converted to a vector, for the range of operations. */
9420 : : enum stv_conv
9421 : 3475 : scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
9422 : : bool complain)
9423 : : {
9424 : 3475 : tree type0 = TREE_TYPE (op0);
9425 : 3475 : tree type1 = TREE_TYPE (op1);
9426 : 3475 : bool integer_only_op = false;
9427 : 3475 : enum stv_conv ret = stv_firstarg;
9428 : :
9429 : 3475 : gcc_assert (gnu_vector_type_p (type0) || gnu_vector_type_p (type1));
9430 : 3475 : switch (code)
9431 : : {
9432 : : /* Most GENERIC binary expressions require homogeneous arguments.
9433 : : LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
9434 : : argument that is a vector and a second one that is a scalar, so
9435 : : we never return stv_secondarg for them. */
9436 : 951 : case RSHIFT_EXPR:
9437 : 951 : case LSHIFT_EXPR:
9438 : 951 : if (TREE_CODE (type0) == INTEGER_TYPE
9439 : 951 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9440 : : {
9441 : 50 : if (unsafe_conversion_p (TREE_TYPE (type1), op0,
9442 : : NULL_TREE, false))
9443 : : {
9444 : 0 : if (complain)
9445 : 0 : error_at (loc, "conversion of scalar %qT to vector %qT "
9446 : : "involves truncation", type0, type1);
9447 : 0 : return stv_error;
9448 : : }
9449 : : else
9450 : : return stv_firstarg;
9451 : : }
9452 : : break;
9453 : :
9454 : 311 : case BIT_IOR_EXPR:
9455 : 311 : case BIT_XOR_EXPR:
9456 : 311 : case BIT_AND_EXPR:
9457 : 311 : integer_only_op = true;
9458 : : /* fall through */
9459 : :
9460 : 2512 : case VEC_COND_EXPR:
9461 : :
9462 : 2512 : case PLUS_EXPR:
9463 : 2512 : case MINUS_EXPR:
9464 : 2512 : case MULT_EXPR:
9465 : 2512 : case TRUNC_DIV_EXPR:
9466 : 2512 : case CEIL_DIV_EXPR:
9467 : 2512 : case FLOOR_DIV_EXPR:
9468 : 2512 : case ROUND_DIV_EXPR:
9469 : 2512 : case EXACT_DIV_EXPR:
9470 : 2512 : case TRUNC_MOD_EXPR:
9471 : 2512 : case FLOOR_MOD_EXPR:
9472 : 2512 : case RDIV_EXPR:
9473 : 2512 : case EQ_EXPR:
9474 : 2512 : case NE_EXPR:
9475 : 2512 : case LE_EXPR:
9476 : 2512 : case GE_EXPR:
9477 : 2512 : case LT_EXPR:
9478 : 2512 : case GT_EXPR:
9479 : : /* What about UNLT_EXPR? */
9480 : 2512 : if (gnu_vector_type_p (type0))
9481 : : {
9482 : 1859 : ret = stv_secondarg;
9483 : 1859 : std::swap (type0, type1);
9484 : 1859 : std::swap (op0, op1);
9485 : : }
9486 : :
9487 : 2512 : if (TREE_CODE (type0) == INTEGER_TYPE
9488 : 2512 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9489 : : {
9490 : 1802 : if (unsafe_conversion_p (TREE_TYPE (type1), op0,
9491 : : NULL_TREE, false))
9492 : : {
9493 : 11 : if (complain)
9494 : 8 : error_at (loc, "conversion of scalar %qT to vector %qT "
9495 : : "involves truncation", type0, type1);
9496 : 11 : return stv_error;
9497 : : }
9498 : : return ret;
9499 : : }
9500 : 710 : else if (!integer_only_op
9501 : : /* Allow integer --> real conversion if safe. */
9502 : 710 : && (SCALAR_FLOAT_TYPE_P (type0)
9503 : 468 : || TREE_CODE (type0) == INTEGER_TYPE)
9504 : 1420 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
9505 : : {
9506 : 686 : if (unsafe_conversion_p (TREE_TYPE (type1), op0,
9507 : : NULL_TREE, false))
9508 : : {
9509 : 22 : if (complain)
9510 : 22 : error_at (loc, "conversion of scalar %qT to vector %qT "
9511 : : "involves truncation", type0, type1);
9512 : 22 : return stv_error;
9513 : : }
9514 : : return ret;
9515 : : }
9516 : : default:
9517 : : break;
9518 : : }
9519 : :
9520 : : return stv_nothing;
9521 : : }
9522 : :
9523 : : /* Return the alignment of std::max_align_t.
9524 : :
9525 : : [support.types.layout] The type max_align_t is a POD type whose alignment
9526 : : requirement is at least as great as that of every scalar type, and whose
9527 : : alignment requirement is supported in every context. */
9528 : :
9529 : : unsigned
9530 : 255059 : max_align_t_align ()
9531 : : {
9532 : 255059 : unsigned int max_align = MAX (TYPE_ALIGN (long_long_integer_type_node),
9533 : : TYPE_ALIGN (long_double_type_node));
9534 : 255059 : if (float128_type_node != NULL_TREE)
9535 : 255059 : max_align = MAX (max_align, TYPE_ALIGN (float128_type_node));
9536 : 255059 : return max_align;
9537 : : }
9538 : :
9539 : : /* Return true iff ALIGN is an integral constant that is a fundamental
9540 : : alignment, as defined by [basic.align] in the c++-11
9541 : : specifications.
9542 : :
9543 : : That is:
9544 : :
9545 : : [A fundamental alignment is represented by an alignment less than or
9546 : : equal to the greatest alignment supported by the implementation
9547 : : in all contexts, which is equal to alignof(max_align_t)]. */
9548 : :
9549 : : bool
9550 : 0 : cxx_fundamental_alignment_p (unsigned align)
9551 : : {
9552 : 0 : return (align <= max_align_t_align ());
9553 : : }
9554 : :
9555 : : /* Return true if T is a pointer to a zero-sized aggregate. */
9556 : :
9557 : : bool
9558 : 1370702 : pointer_to_zero_sized_aggr_p (tree t)
9559 : : {
9560 : 1370702 : if (!POINTER_TYPE_P (t))
9561 : : return false;
9562 : 1370702 : t = TREE_TYPE (t);
9563 : 1370702 : return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
9564 : : }
9565 : :
9566 : : /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
9567 : : with no library fallback or for an ADDR_EXPR whose operand is such type
9568 : : issues an error pointing to the location LOC.
9569 : : Returns true when the expression has been diagnosed and false
9570 : : otherwise. */
9571 : :
9572 : : bool
9573 : 4510226209 : reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
9574 : : {
9575 : 4510226209 : if (TREE_CODE (expr) == ADDR_EXPR)
9576 : 110338577 : expr = TREE_OPERAND (expr, 0);
9577 : :
9578 : 4510226209 : STRIP_ANY_LOCATION_WRAPPER (expr);
9579 : :
9580 : 4510226209 : if (TREE_TYPE (expr)
9581 : 4510226055 : && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
9582 : 2081736 : && TREE_CODE (expr) == FUNCTION_DECL
9583 : : /* The intersection of DECL_BUILT_IN and DECL_IS_UNDECLARED_BUILTIN avoids
9584 : : false positives for user-declared built-ins such as abs or
9585 : : strlen, and for C++ operators new and delete.
9586 : : The c_decl_implicit() test avoids false positives for implicitly
9587 : : declared built-ins with library fallbacks (such as abs). */
9588 : 2053916 : && fndecl_built_in_p (expr)
9589 : 67561 : && DECL_IS_UNDECLARED_BUILTIN (expr)
9590 : 2372 : && !c_decl_implicit (expr)
9591 : 4510228576 : && !DECL_ASSEMBLER_NAME_SET_P (expr))
9592 : : {
9593 : 220 : if (loc == UNKNOWN_LOCATION)
9594 : 83 : loc = EXPR_LOC_OR_LOC (expr, input_location);
9595 : :
9596 : : /* Reject arguments that are built-in functions with
9597 : : no library fallback. */
9598 : 220 : error_at (loc, "built-in function %qE must be directly called", expr);
9599 : :
9600 : 220 : return true;
9601 : : }
9602 : :
9603 : : return false;
9604 : : }
9605 : :
9606 : : /* Issue an ERROR for an invalid SIZE of array NAME which is null
9607 : : for unnamed arrays. */
9608 : :
9609 : : void
9610 : 1409 : invalid_array_size_error (location_t loc, cst_size_error error,
9611 : : const_tree size, const_tree name)
9612 : : {
9613 : 1409 : tree maxsize = max_object_size ();
9614 : 1409 : switch (error)
9615 : : {
9616 : 0 : case cst_size_not_constant:
9617 : 0 : if (name)
9618 : 0 : error_at (loc, "size of array %qE is not a constant expression",
9619 : : name);
9620 : : else
9621 : 0 : error_at (loc, "size of array is not a constant expression");
9622 : : break;
9623 : 419 : case cst_size_negative:
9624 : 419 : if (name)
9625 : 128 : error_at (loc, "size %qE of array %qE is negative",
9626 : : size, name);
9627 : : else
9628 : 291 : error_at (loc, "size %qE of array is negative",
9629 : : size);
9630 : : break;
9631 : 930 : case cst_size_too_big:
9632 : 930 : if (name)
9633 : 103 : error_at (loc, "size %qE of array %qE exceeds maximum "
9634 : : "object size %qE", size, name, maxsize);
9635 : : else
9636 : 827 : error_at (loc, "size %qE of array exceeds maximum "
9637 : : "object size %qE", size, maxsize);
9638 : : break;
9639 : 60 : case cst_size_overflow:
9640 : 60 : if (name)
9641 : 39 : error_at (loc, "size of array %qE exceeds maximum "
9642 : : "object size %qE", name, maxsize);
9643 : : else
9644 : 21 : error_at (loc, "size of array exceeds maximum "
9645 : : "object size %qE", maxsize);
9646 : : break;
9647 : 0 : default:
9648 : 0 : gcc_unreachable ();
9649 : : }
9650 : 1409 : }
9651 : :
9652 : : /* Check if array size calculations overflow or if the array covers more
9653 : : than half of the address space. Return true if the size of the array
9654 : : is valid, false otherwise. T is either the type of the array or its
9655 : : size, and NAME is the name of the array, or null for unnamed arrays. */
9656 : :
9657 : : bool
9658 : 7648564 : valid_array_size_p (location_t loc, const_tree t, tree name, bool complain)
9659 : : {
9660 : 7648564 : if (t == error_mark_node)
9661 : : return true;
9662 : :
9663 : 7648423 : const_tree size;
9664 : 7648423 : if (TYPE_P (t))
9665 : : {
9666 : 5508235 : if (!COMPLETE_TYPE_P (t))
9667 : : return true;
9668 : 2841545 : size = TYPE_SIZE_UNIT (t);
9669 : : }
9670 : : else
9671 : : size = t;
9672 : :
9673 : 4981733 : if (TREE_CODE (size) != INTEGER_CST)
9674 : : return true;
9675 : :
9676 : 4957253 : cst_size_error error;
9677 : 4957253 : if (valid_constant_size_p (size, &error))
9678 : : return true;
9679 : :
9680 : 1100 : if (!complain)
9681 : : return false;
9682 : :
9683 : 1085 : if (TREE_CODE (TREE_TYPE (size)) == ENUMERAL_TYPE)
9684 : : /* Show the value of the enumerator rather than its name. */
9685 : 16 : size = convert (ssizetype, const_cast<tree> (size));
9686 : :
9687 : 1085 : invalid_array_size_error (loc, error, size, name);
9688 : 1085 : return false;
9689 : : }
9690 : :
9691 : : /* Read SOURCE_DATE_EPOCH from environment to have a deterministic
9692 : : timestamp to replace embedded current dates to get reproducible
9693 : : results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
9694 : :
9695 : : time_t
9696 : 2600 : cb_get_source_date_epoch (cpp_reader *pfile ATTRIBUTE_UNUSED)
9697 : : {
9698 : 2600 : char *source_date_epoch;
9699 : 2600 : int64_t epoch;
9700 : 2600 : char *endptr;
9701 : :
9702 : 2600 : source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
9703 : 2600 : if (!source_date_epoch)
9704 : : return (time_t) -1;
9705 : :
9706 : 3 : errno = 0;
9707 : : #if defined(INT64_T_IS_LONG)
9708 : 3 : epoch = strtol (source_date_epoch, &endptr, 10);
9709 : : #else
9710 : : epoch = strtoll (source_date_epoch, &endptr, 10);
9711 : : #endif
9712 : 3 : if (errno != 0 || endptr == source_date_epoch || *endptr != '\0'
9713 : 2 : || epoch < 0 || epoch > MAX_SOURCE_DATE_EPOCH)
9714 : : {
9715 : 1 : error_at (input_location, "environment variable %qs must "
9716 : : "expand to a non-negative integer less than or equal to %wd",
9717 : : "SOURCE_DATE_EPOCH", MAX_SOURCE_DATE_EPOCH);
9718 : 1 : return (time_t) -1;
9719 : : }
9720 : :
9721 : : return (time_t) epoch;
9722 : : }
9723 : :
9724 : : /* Callback for libcpp for offering spelling suggestions for misspelled
9725 : : directives. GOAL is an unrecognized string; CANDIDATES is a
9726 : : NULL-terminated array of candidate strings. Return the closest
9727 : : match to GOAL within CANDIDATES, or NULL if none are good
9728 : : suggestions. */
9729 : :
9730 : : const char *
9731 : 19 : cb_get_suggestion (cpp_reader *, const char *goal,
9732 : : const char *const *candidates)
9733 : : {
9734 : 19 : best_match<const char *, const char *> bm (goal);
9735 : 204 : while (*candidates)
9736 : 166 : bm.consider (*candidates++);
9737 : 19 : return bm.get_best_meaningful_candidate ();
9738 : : }
9739 : :
9740 : : /* Return the latice point which is the wider of the two FLT_EVAL_METHOD
9741 : : modes X, Y. This isn't just >, as the FLT_EVAL_METHOD values added
9742 : : by C TS 18661-3 for interchange types that are computed in their
9743 : : native precision are larger than the C11 values for evaluating in the
9744 : : precision of float/double/long double. If either mode is
9745 : : FLT_EVAL_METHOD_UNPREDICTABLE, return that. */
9746 : :
9747 : : enum flt_eval_method
9748 : 536353 : excess_precision_mode_join (enum flt_eval_method x,
9749 : : enum flt_eval_method y)
9750 : : {
9751 : 536353 : if (x == FLT_EVAL_METHOD_UNPREDICTABLE
9752 : 536353 : || y == FLT_EVAL_METHOD_UNPREDICTABLE)
9753 : : return FLT_EVAL_METHOD_UNPREDICTABLE;
9754 : :
9755 : : /* GCC only supports one interchange type right now, _Float16. If
9756 : : we're evaluating _Float16 in 16-bit precision, then flt_eval_method
9757 : : will be FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
9758 : 536029 : if (x == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
9759 : : return y;
9760 : 533728 : if (y == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
9761 : : return x;
9762 : :
9763 : : /* Other values for flt_eval_method are directly comparable, and we want
9764 : : the maximum. */
9765 : 533716 : return MAX (x, y);
9766 : : }
9767 : :
9768 : : /* Return the value that should be set for FLT_EVAL_METHOD in the
9769 : : context of ISO/IEC TS 18861-3.
9770 : :
9771 : : This relates to the effective excess precision seen by the user,
9772 : : which is the join point of the precision the target requests for
9773 : : -fexcess-precision={standard,fast,16} and the implicit excess precision
9774 : : the target uses. */
9775 : :
9776 : : static enum flt_eval_method
9777 : 420932 : c_ts18661_flt_eval_method (void)
9778 : : {
9779 : 420932 : enum flt_eval_method implicit
9780 : 420932 : = targetm.c.excess_precision (EXCESS_PRECISION_TYPE_IMPLICIT);
9781 : :
9782 : 306770 : enum excess_precision_type flag_type
9783 : 420932 : = (flag_excess_precision == EXCESS_PRECISION_STANDARD
9784 : 420932 : ? EXCESS_PRECISION_TYPE_STANDARD
9785 : : : (flag_excess_precision == EXCESS_PRECISION_FLOAT16
9786 : 306782 : ? EXCESS_PRECISION_TYPE_FLOAT16
9787 : : : EXCESS_PRECISION_TYPE_FAST));
9788 : :
9789 : 420932 : enum flt_eval_method requested
9790 : 420932 : = targetm.c.excess_precision (flag_type);
9791 : :
9792 : 420932 : return excess_precision_mode_join (implicit, requested);
9793 : : }
9794 : :
9795 : : /* As c_cpp_ts18661_flt_eval_method, but clamps the expected values to
9796 : : those that were permitted by C11. That is to say, eliminates
9797 : : FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
9798 : :
9799 : : static enum flt_eval_method
9800 : 103155 : c_c11_flt_eval_method (void)
9801 : : {
9802 : 103155 : return excess_precision_mode_join (c_ts18661_flt_eval_method (),
9803 : 103155 : FLT_EVAL_METHOD_PROMOTE_TO_FLOAT);
9804 : : }
9805 : :
9806 : : /* Return the value that should be set for FLT_EVAL_METHOD.
9807 : : MAYBE_C11_ONLY_P is TRUE if we should check
9808 : : FLAG_PERMITTED_EVAL_METHODS as to whether we should limit the possible
9809 : : values we can return to those from C99/C11, and FALSE otherwise.
9810 : : See the comments on c_ts18661_flt_eval_method for what value we choose
9811 : : to set here. */
9812 : :
9813 : : int
9814 : 420932 : c_flt_eval_method (bool maybe_c11_only_p)
9815 : : {
9816 : 420932 : if (maybe_c11_only_p
9817 : 210466 : && flag_permitted_flt_eval_methods
9818 : : == PERMITTED_FLT_EVAL_METHODS_C11)
9819 : 103155 : return c_c11_flt_eval_method ();
9820 : : else
9821 : 317777 : return c_ts18661_flt_eval_method ();
9822 : : }
9823 : :
9824 : : /* An enum for get_missing_token_insertion_kind for describing the best
9825 : : place to insert a missing token, if there is one. */
9826 : :
9827 : : enum missing_token_insertion_kind
9828 : : {
9829 : : MTIK_IMPOSSIBLE,
9830 : : MTIK_INSERT_BEFORE_NEXT,
9831 : : MTIK_INSERT_AFTER_PREV
9832 : : };
9833 : :
9834 : : /* Given a missing token of TYPE, determine if it is reasonable to
9835 : : emit a fix-it hint suggesting the insertion of the token, and,
9836 : : if so, where the token should be inserted relative to other tokens.
9837 : :
9838 : : It only makes sense to do this for values of TYPE that are symbols.
9839 : :
9840 : : Some symbols should go before the next token, e.g. in:
9841 : : if flag)
9842 : : we want to insert the missing '(' immediately before "flag",
9843 : : giving:
9844 : : if (flag)
9845 : : rather than:
9846 : : if( flag)
9847 : : These use MTIK_INSERT_BEFORE_NEXT.
9848 : :
9849 : : Other symbols should go after the previous token, e.g. in:
9850 : : if (flag
9851 : : do_something ();
9852 : : we want to insert the missing ')' immediately after the "flag",
9853 : : giving:
9854 : : if (flag)
9855 : : do_something ();
9856 : : rather than:
9857 : : if (flag
9858 : : )do_something ();
9859 : : These use MTIK_INSERT_AFTER_PREV. */
9860 : :
9861 : : static enum missing_token_insertion_kind
9862 : 3259 : get_missing_token_insertion_kind (enum cpp_ttype type)
9863 : : {
9864 : 3259 : switch (type)
9865 : : {
9866 : : /* Insert missing "opening" brackets immediately
9867 : : before the next token. */
9868 : : case CPP_OPEN_SQUARE:
9869 : : case CPP_OPEN_PAREN:
9870 : : return MTIK_INSERT_BEFORE_NEXT;
9871 : :
9872 : : /* Insert other missing symbols immediately after
9873 : : the previous token. */
9874 : 1980 : case CPP_CLOSE_PAREN:
9875 : 1980 : case CPP_CLOSE_SQUARE:
9876 : 1980 : case CPP_SEMICOLON:
9877 : 1980 : case CPP_COMMA:
9878 : 1980 : case CPP_COLON:
9879 : 1980 : return MTIK_INSERT_AFTER_PREV;
9880 : :
9881 : : /* Other kinds of token don't get fix-it hints. */
9882 : 990 : default:
9883 : 990 : return MTIK_IMPOSSIBLE;
9884 : : }
9885 : : }
9886 : :
9887 : : /* Given RICHLOC, a location for a diagnostic describing a missing token
9888 : : of kind TOKEN_TYPE, potentially add a fix-it hint suggesting the
9889 : : insertion of the token.
9890 : :
9891 : : The location of the attempted fix-it hint depends on TOKEN_TYPE:
9892 : : it will either be:
9893 : : (a) immediately after PREV_TOKEN_LOC, or
9894 : :
9895 : : (b) immediately before the primary location within RICHLOC (taken to
9896 : : be that of the token following where the token was expected).
9897 : :
9898 : : If we manage to add a fix-it hint, then the location of the
9899 : : fix-it hint is likely to be more useful as the primary location
9900 : : of the diagnostic than that of the following token, so we swap
9901 : : these locations.
9902 : :
9903 : : For example, given this bogus code:
9904 : : 123456789012345678901234567890
9905 : : 1 | int missing_semicolon (void)
9906 : : 2 | {
9907 : : 3 | return 42
9908 : : 4 | }
9909 : :
9910 : : we will emit:
9911 : :
9912 : : "expected ';' before '}'"
9913 : :
9914 : : RICHLOC's primary location is at the closing brace, so before "swapping"
9915 : : we would emit the error at line 4 column 1:
9916 : :
9917 : : 123456789012345678901234567890
9918 : : 3 | return 42 |< fix-it hint emitted for this line
9919 : : | ; |
9920 : : 4 | } |< "expected ';' before '}'" emitted at this line
9921 : : | ^ |
9922 : :
9923 : : It's more useful for the location of the diagnostic to be at the
9924 : : fix-it hint, so we swap the locations, so the primary location
9925 : : is at the fix-it hint, with the old primary location inserted
9926 : : as a secondary location, giving this, with the error at line 3
9927 : : column 12:
9928 : :
9929 : : 123456789012345678901234567890
9930 : : 3 | return 42 |< "expected ';' before '}'" emitted at this line,
9931 : : | ^ | with fix-it hint
9932 : : 4 | ; |
9933 : : | } |< secondary range emitted here
9934 : : | ~ |. */
9935 : :
9936 : : void
9937 : 3259 : maybe_suggest_missing_token_insertion (rich_location *richloc,
9938 : : enum cpp_ttype token_type,
9939 : : location_t prev_token_loc)
9940 : : {
9941 : 3259 : gcc_assert (richloc);
9942 : :
9943 : 3259 : enum missing_token_insertion_kind mtik
9944 : 3259 : = get_missing_token_insertion_kind (token_type);
9945 : :
9946 : 3259 : switch (mtik)
9947 : : {
9948 : : default:
9949 : : gcc_unreachable ();
9950 : : break;
9951 : :
9952 : : case MTIK_IMPOSSIBLE:
9953 : : return;
9954 : :
9955 : 289 : case MTIK_INSERT_BEFORE_NEXT:
9956 : : /* Attempt to add the fix-it hint before the primary location
9957 : : of RICHLOC. */
9958 : 289 : richloc->add_fixit_insert_before (cpp_type2name (token_type, 0));
9959 : 289 : break;
9960 : :
9961 : 1980 : case MTIK_INSERT_AFTER_PREV:
9962 : : /* Attempt to add the fix-it hint after PREV_TOKEN_LOC. */
9963 : 1980 : richloc->add_fixit_insert_after (prev_token_loc,
9964 : : cpp_type2name (token_type, 0));
9965 : 1980 : break;
9966 : : }
9967 : :
9968 : : /* If we were successful, use the fix-it hint's location as the
9969 : : primary location within RICHLOC, adding the old primary location
9970 : : back as a secondary location. */
9971 : 2269 : if (!richloc->seen_impossible_fixit_p ())
9972 : : {
9973 : 2265 : fixit_hint *hint = richloc->get_last_fixit_hint ();
9974 : 2265 : location_t hint_loc = hint->get_start_loc ();
9975 : 2265 : location_t old_loc = richloc->get_loc ();
9976 : :
9977 : 2265 : richloc->set_range (0, hint_loc, SHOW_RANGE_WITH_CARET);
9978 : 2265 : richloc->add_range (old_loc);
9979 : : }
9980 : : }
9981 : :
9982 : : /* Potentially emit a note about likely missing '&' or '*',
9983 : : depending on EXPR and EXPECTED_TYPE. */
9984 : :
9985 : : void
9986 : 2205 : maybe_emit_indirection_note (location_t loc,
9987 : : tree expr, tree expected_type)
9988 : : {
9989 : 2205 : gcc_assert (expr);
9990 : 2205 : gcc_assert (expected_type);
9991 : :
9992 : 2205 : tree actual_type = TREE_TYPE (expr);
9993 : :
9994 : : /* Missing '&'. */
9995 : 2205 : if (TREE_CODE (expected_type) == POINTER_TYPE
9996 : 1224 : && compatible_types_for_indirection_note_p (actual_type,
9997 : 1224 : TREE_TYPE (expected_type))
9998 : 2283 : && lvalue_p (expr))
9999 : : {
10000 : 43 : gcc_rich_location richloc (loc);
10001 : 43 : richloc.add_fixit_insert_before ("&");
10002 : 43 : inform (&richloc, "possible fix: take the address with %qs", "&");
10003 : 43 : }
10004 : :
10005 : : /* Missing '*'. */
10006 : 2205 : if (TREE_CODE (actual_type) == POINTER_TYPE
10007 : 2205 : && compatible_types_for_indirection_note_p (TREE_TYPE (actual_type),
10008 : : expected_type))
10009 : : {
10010 : 78 : gcc_rich_location richloc (loc);
10011 : 78 : richloc.add_fixit_insert_before ("*");
10012 : 78 : inform (&richloc, "possible fix: dereference with %qs", "*");
10013 : 78 : }
10014 : 2205 : }
10015 : :
10016 : : #if CHECKING_P
10017 : :
10018 : : namespace selftest {
10019 : :
10020 : : /* Verify that fold_for_warn on error_mark_node is safe. */
10021 : :
10022 : : static void
10023 : 3 : test_fold_for_warn ()
10024 : : {
10025 : 3 : ASSERT_EQ (error_mark_node, fold_for_warn (error_mark_node));
10026 : 3 : }
10027 : :
10028 : : /* Run all of the selftests within this file. */
10029 : :
10030 : : static void
10031 : 3 : c_common_cc_tests ()
10032 : : {
10033 : 0 : test_fold_for_warn ();
10034 : 0 : }
10035 : :
10036 : : /* Run all of the tests within c-family. */
10037 : :
10038 : : void
10039 : 3 : c_family_tests (void)
10040 : : {
10041 : 3 : c_common_cc_tests ();
10042 : 3 : c_format_cc_tests ();
10043 : 3 : c_indentation_cc_tests ();
10044 : 3 : c_pretty_print_cc_tests ();
10045 : 3 : c_spellcheck_cc_tests ();
10046 : 3 : c_opt_problem_cc_tests ();
10047 : :
10048 : : /* According to https://gcc.gnu.org/pipermail/gcc/2021-November/237703.html
10049 : : this has some language-specific assumptions, so we run it here. */
10050 : 3 : diagnostics::selftest::context_cc_tests ();
10051 : 3 : }
10052 : :
10053 : : } // namespace selftest
10054 : :
10055 : : #endif /* #if CHECKING_P */
10056 : :
10057 : : /* Attempt to locate a suitable location within FILE for a
10058 : : #include directive to be inserted before.
10059 : : LOC is the location of the relevant diagnostic.
10060 : :
10061 : : Attempt to return the location within FILE immediately
10062 : : after the last #include within that file, or the start of
10063 : : that file if it has no #include directives.
10064 : :
10065 : : Return UNKNOWN_LOCATION if no suitable location is found,
10066 : : or if an error occurs. */
10067 : :
10068 : : static location_t
10069 : 428 : try_to_locate_new_include_insertion_point (const char *file, location_t loc)
10070 : : {
10071 : : /* Locate the last ordinary map within FILE that ended with a #include. */
10072 : 428 : const line_map_ordinary *last_include_ord_map = NULL;
10073 : :
10074 : : /* ...and the next ordinary map within FILE after that one. */
10075 : 428 : const line_map_ordinary *last_ord_map_after_include = NULL;
10076 : :
10077 : : /* ...and the first ordinary map within FILE. */
10078 : 428 : const line_map_ordinary *first_ord_map_in_file = NULL;
10079 : :
10080 : : /* Get ordinary map containing LOC (or its expansion). */
10081 : 428 : const line_map_ordinary *ord_map_for_loc = NULL;
10082 : 428 : linemap_resolve_location (line_table, loc, LRK_MACRO_EXPANSION_POINT,
10083 : : &ord_map_for_loc);
10084 : 428 : gcc_assert (ord_map_for_loc);
10085 : :
10086 : 12331 : for (unsigned int i = 0; i < LINEMAPS_ORDINARY_USED (line_table); i++)
10087 : : {
10088 : 12331 : const line_map_ordinary *ord_map
10089 : 12331 : = LINEMAPS_ORDINARY_MAP_AT (line_table, i);
10090 : :
10091 : 24662 : if (const line_map_ordinary *from
10092 : 12331 : = linemap_included_from_linemap (line_table, ord_map))
10093 : : /* We cannot use pointer equality, because with preprocessed
10094 : : input all filename strings are unique. */
10095 : 9951 : if (0 == strcmp (from->to_file, file))
10096 : : {
10097 : 12331 : last_include_ord_map = from;
10098 : 12331 : last_ord_map_after_include = NULL;
10099 : : }
10100 : :
10101 : : /* Likewise, use strcmp, and reject any line-zero introductory
10102 : : map. */
10103 : 12331 : if (ord_map->to_line && 0 == strcmp (ord_map->to_file, file))
10104 : : {
10105 : 670 : if (!first_ord_map_in_file)
10106 : 428 : first_ord_map_in_file = ord_map;
10107 : 670 : if (last_include_ord_map && !last_ord_map_after_include)
10108 : 166 : last_ord_map_after_include = ord_map;
10109 : : }
10110 : :
10111 : : /* Stop searching when reaching the ord_map containing LOC,
10112 : : as it makes no sense to provide fix-it hints that appear
10113 : : after the diagnostic in question. */
10114 : 12331 : if (ord_map == ord_map_for_loc)
10115 : : break;
10116 : : }
10117 : :
10118 : : /* Determine where to insert the #include. */
10119 : 428 : const line_map_ordinary *ord_map_for_insertion;
10120 : :
10121 : : /* We want the next ordmap in the file after the last one that's a
10122 : : #include, but failing that, the start of the file. */
10123 : 428 : if (last_ord_map_after_include)
10124 : : ord_map_for_insertion = last_ord_map_after_include;
10125 : : else
10126 : 264 : ord_map_for_insertion = first_ord_map_in_file;
10127 : :
10128 : 264 : if (!ord_map_for_insertion)
10129 : : return UNKNOWN_LOCATION;
10130 : :
10131 : : /* The "start_location" is column 0, meaning "the whole line".
10132 : : rich_location and diagnostics::changes can't cope with this, so use
10133 : : column 1 instead. */
10134 : 428 : location_t col_0 = ord_map_for_insertion->start_location;
10135 : 428 : return linemap_position_for_loc_and_offset (line_table, col_0, 1);
10136 : : }
10137 : :
10138 : : /* A map from filenames to sets of headers added to them, for
10139 : : ensuring idempotency within maybe_add_include_fixit. */
10140 : :
10141 : : /* The values within the map. We need string comparison as there's
10142 : : no guarantee that two different diagnostics that are recommending
10143 : : adding e.g. "<stdio.h>" are using the same buffer. */
10144 : :
10145 : : typedef hash_set <const char *, false, nofree_string_hash> per_file_includes_t;
10146 : :
10147 : : /* The map itself. We don't need string comparison for the filename keys,
10148 : : as they come from libcpp. */
10149 : :
10150 : : typedef hash_map <const char *, per_file_includes_t *> added_includes_t;
10151 : : static added_includes_t *added_includes;
10152 : :
10153 : : /* Attempt to add a fix-it hint to RICHLOC, adding "#include HEADER\n"
10154 : : in a suitable location within the file of RICHLOC's primary
10155 : : location.
10156 : :
10157 : : This function is idempotent: a header will be added at most once to
10158 : : any given file.
10159 : :
10160 : : If OVERRIDE_LOCATION is true, then if a fix-it is added and will be
10161 : : printed, then RICHLOC's primary location will be replaced by that of
10162 : : the fix-it hint (for use by "inform" notes where the location of the
10163 : : issue has already been reported). */
10164 : :
10165 : : void
10166 : 880 : maybe_add_include_fixit (rich_location *richloc, const char *header,
10167 : : bool override_location)
10168 : : {
10169 : 880 : location_t loc = richloc->get_loc ();
10170 : 880 : const char *file = LOCATION_FILE (loc);
10171 : 880 : if (!file)
10172 : 452 : return;
10173 : :
10174 : : /* Idempotency: don't add the same header more than once to a given file. */
10175 : 880 : if (!added_includes)
10176 : 345 : added_includes = new added_includes_t ();
10177 : 880 : per_file_includes_t *&set = added_includes->get_or_insert (file);
10178 : 880 : if (set)
10179 : 535 : if (set->contains (header))
10180 : : /* ...then we've already added HEADER to that file. */
10181 : : return;
10182 : 428 : if (!set)
10183 : 345 : set = new per_file_includes_t ();
10184 : 428 : set->add (header);
10185 : :
10186 : : /* Attempt to locate a suitable place for the new directive. */
10187 : 428 : location_t include_insert_loc
10188 : 428 : = try_to_locate_new_include_insertion_point (file, loc);
10189 : 428 : if (include_insert_loc == UNKNOWN_LOCATION)
10190 : : return;
10191 : :
10192 : 428 : char *text = xasprintf ("#include %s\n", header);
10193 : 428 : richloc->add_fixit_insert_before (include_insert_loc, text);
10194 : 428 : free (text);
10195 : :
10196 : 428 : if (override_location && global_dc->get_source_printing_options ().enabled)
10197 : : {
10198 : : /* Replace the primary location with that of the insertion point for the
10199 : : fix-it hint.
10200 : :
10201 : : We use SHOW_LINES_WITHOUT_RANGE so that we don't meaningless print a
10202 : : caret for the insertion point (or colorize it).
10203 : :
10204 : : Hence we print e.g.:
10205 : :
10206 : : ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
10207 : : 73 | # include <debug/vector>
10208 : : +++ |+#include <vector>
10209 : : 74 | #endif
10210 : :
10211 : : rather than:
10212 : :
10213 : : ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
10214 : : 73 | # include <debug/vector>
10215 : : +++ |+#include <vector>
10216 : : 74 | #endif
10217 : : | ^
10218 : :
10219 : : avoiding the caret on the first column of line 74. */
10220 : 177 : richloc->set_range (0, include_insert_loc, SHOW_LINES_WITHOUT_RANGE);
10221 : : }
10222 : : }
10223 : :
10224 : : /* Attempt to convert a braced array initializer list CTOR for array
10225 : : TYPE into a STRING_CST for convenience and efficiency. Return
10226 : : the converted string on success or the original ctor on failure.
10227 : : Also, for non-convertable CTORs which contain RAW_DATA_CST values
10228 : : among the elts try to extend the range of RAW_DATA_CSTs. */
10229 : :
10230 : : static tree
10231 : 45805 : braced_list_to_string (tree type, tree ctor, bool member)
10232 : : {
10233 : : /* Ignore non-members with unknown size like arrays with unspecified
10234 : : bound. */
10235 : 45805 : tree typesize = TYPE_SIZE_UNIT (type);
10236 : 45805 : if (!member && !tree_fits_uhwi_p (typesize))
10237 : : return ctor;
10238 : :
10239 : : /* If the target char size differs from the host char size, we'd risk
10240 : : loosing data and getting object sizes wrong by converting to
10241 : : host chars. */
10242 : 45789 : if (TYPE_PRECISION (char_type_node) != CHAR_BIT)
10243 : : return ctor;
10244 : :
10245 : : /* STRING_CST doesn't support wide characters. */
10246 : 45789 : gcc_checking_assert (TYPE_PRECISION (TREE_TYPE (type)) == CHAR_BIT);
10247 : :
10248 : : /* If the array has an explicit bound, use it to constrain the size
10249 : : of the string. If it doesn't, be sure to create a string that's
10250 : : as long as implied by the index of the last zero specified via
10251 : : a designator, as in:
10252 : : const char a[] = { [7] = 0 }; */
10253 : 45789 : unsigned HOST_WIDE_INT maxelts;
10254 : 45789 : if (typesize)
10255 : : {
10256 : 45710 : maxelts = tree_to_uhwi (typesize);
10257 : 45710 : maxelts /= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
10258 : : }
10259 : : else
10260 : : maxelts = HOST_WIDE_INT_M1U;
10261 : :
10262 : : /* Avoid converting initializers for zero-length arrays (but do
10263 : : create them for flexible array members). */
10264 : 45710 : if (!maxelts)
10265 : : return ctor;
10266 : :
10267 : 45640 : unsigned HOST_WIDE_INT nelts = CONSTRUCTOR_NELTS (ctor);
10268 : :
10269 : 45640 : auto_vec<char> str;
10270 : 45640 : str.reserve (nelts + 1);
10271 : :
10272 : 45640 : unsigned HOST_WIDE_INT i, j = HOST_WIDE_INT_M1U;
10273 : 45640 : tree index, value;
10274 : 45640 : bool check_raw_data = false;
10275 : :
10276 : 647932 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, index, value)
10277 : : {
10278 : 602292 : if (check_raw_data)
10279 : : {
10280 : : /* The preprocessor always surrounds CPP_EMBED tokens in between
10281 : : CPP_NUMBER and CPP_COMMA tokens. Try to undo that here now that
10282 : : the whole initializer is parsed. E.g. if we have
10283 : : [0] = 'T', [1] = "his is a #embed tex", [20] = 't'
10284 : : where the middle value is RAW_DATA_CST and in its owner this is
10285 : : surrounded by 'T' and 't' characters, we can create from it just
10286 : : [0] = "This is a #embed text"
10287 : : Similarly if a RAW_DATA_CST needs to be split into two parts
10288 : : because of designated init store but the stored value is actually
10289 : : the same as in the RAW_DATA_OWNER's memory we can merge multiple
10290 : : RAW_DATA_CSTs. */
10291 : 1229 : if (TREE_CODE (value) == RAW_DATA_CST
10292 : 551 : && index
10293 : 551 : && tree_fits_uhwi_p (index))
10294 : : {
10295 : 551 : tree owner = RAW_DATA_OWNER (value);
10296 : 551 : unsigned int start, end, k;
10297 : 551 : if (TREE_CODE (owner) == STRING_CST)
10298 : : {
10299 : 216 : start
10300 : 216 : = RAW_DATA_POINTER (value) - TREE_STRING_POINTER (owner);
10301 : 216 : end = TREE_STRING_LENGTH (owner) - RAW_DATA_LENGTH (value);
10302 : : }
10303 : : else
10304 : : {
10305 : 335 : gcc_checking_assert (TREE_CODE (owner) == RAW_DATA_CST);
10306 : 335 : start
10307 : 335 : = RAW_DATA_POINTER (value) - RAW_DATA_POINTER (owner);
10308 : 335 : end = RAW_DATA_LENGTH (owner) - RAW_DATA_LENGTH (value);
10309 : : }
10310 : 551 : end -= start;
10311 : 551 : unsigned HOST_WIDE_INT l = j == HOST_WIDE_INT_M1U ? i : j;
10312 : 987 : for (k = 0; k < start && k < l; ++k)
10313 : : {
10314 : 462 : constructor_elt *elt = CONSTRUCTOR_ELT (ctor, l - k - 1);
10315 : 924 : if (elt->index == NULL_TREE
10316 : 462 : || !tree_fits_uhwi_p (elt->index)
10317 : 462 : || !tree_fits_shwi_p (elt->value)
10318 : 1381 : || wi::to_widest (index) != (wi::to_widest (elt->index)
10319 : 1376 : + (k + 1)))
10320 : : break;
10321 : 457 : if (TYPE_UNSIGNED (TREE_TYPE (value)))
10322 : : {
10323 : 381 : if (tree_to_shwi (elt->value)
10324 : 381 : != *((const unsigned char *)
10325 : 381 : RAW_DATA_POINTER (value) - k - 1))
10326 : : break;
10327 : : }
10328 : 76 : else if (tree_to_shwi (elt->value)
10329 : 76 : != *((const signed char *)
10330 : 76 : RAW_DATA_POINTER (value) - k - 1))
10331 : : break;
10332 : : }
10333 : 551 : start = k;
10334 : 551 : l = 0;
10335 : 1436 : for (k = 0; k < end && k + 1 < CONSTRUCTOR_NELTS (ctor) - i; ++k)
10336 : : {
10337 : 449 : constructor_elt *elt = CONSTRUCTOR_ELT (ctor, i + k + 1);
10338 : 898 : if (elt->index == NULL_TREE
10339 : 449 : || !tree_fits_uhwi_p (elt->index)
10340 : 1347 : || (wi::to_widest (elt->index)
10341 : 898 : != (wi::to_widest (index)
10342 : 1347 : + (RAW_DATA_LENGTH (value) + l))))
10343 : : break;
10344 : 449 : if (TREE_CODE (elt->value) == RAW_DATA_CST
10345 : 2 : && RAW_DATA_OWNER (elt->value) == RAW_DATA_OWNER (value)
10346 : 449 : && (RAW_DATA_POINTER (elt->value)
10347 : 2 : == RAW_DATA_POINTER (value) + l))
10348 : : {
10349 : 0 : l += RAW_DATA_LENGTH (elt->value);
10350 : 0 : end -= RAW_DATA_LENGTH (elt->value) - 1;
10351 : 0 : continue;
10352 : : }
10353 : 449 : if (!tree_fits_shwi_p (elt->value))
10354 : : break;
10355 : 446 : if (TYPE_UNSIGNED (TREE_TYPE (value)))
10356 : : {
10357 : 392 : if (tree_to_shwi (elt->value)
10358 : 392 : != *((const unsigned char *)
10359 : 392 : RAW_DATA_POINTER (value)
10360 : 392 : + RAW_DATA_LENGTH (value) + k))
10361 : : break;
10362 : : }
10363 : 54 : else if (tree_to_shwi (elt->value)
10364 : 54 : != *((const signed char *)
10365 : 54 : RAW_DATA_POINTER (value)
10366 : 54 : + RAW_DATA_LENGTH (value) + k))
10367 : : break;
10368 : 411 : ++l;
10369 : : }
10370 : 551 : end = k;
10371 : 551 : if (start != 0 || end != 0)
10372 : : {
10373 : 458 : if (j == HOST_WIDE_INT_M1U)
10374 : 420 : j = i - start;
10375 : : else
10376 : 38 : j -= start;
10377 : 458 : value = copy_node (value);
10378 : 458 : RAW_DATA_POINTER (value) -= start;
10379 : 458 : RAW_DATA_LENGTH (value) += start + end;
10380 : 458 : i += end;
10381 : 458 : if (start == 0)
10382 : 22 : CONSTRUCTOR_ELT (ctor, j)->index = index;
10383 : 458 : CONSTRUCTOR_ELT (ctor, j)->value = value;
10384 : 458 : ++j;
10385 : 458 : continue;
10386 : : }
10387 : : }
10388 : 771 : if (j != HOST_WIDE_INT_M1U)
10389 : : {
10390 : 175 : CONSTRUCTOR_ELT (ctor, j)->index = index;
10391 : 175 : CONSTRUCTOR_ELT (ctor, j)->value = value;
10392 : 175 : ++j;
10393 : : }
10394 : 771 : continue;
10395 : 771 : }
10396 : :
10397 : 601063 : unsigned HOST_WIDE_INT idx = i;
10398 : 601063 : if (index)
10399 : : {
10400 : 601063 : if (!tree_fits_uhwi_p (index))
10401 : : {
10402 : 22 : check_raw_data = true;
10403 : 22 : continue;
10404 : : }
10405 : 601041 : idx = tree_to_uhwi (index);
10406 : : }
10407 : :
10408 : : /* auto_vec is limited to UINT_MAX elements. */
10409 : 601041 : if (idx > UINT_MAX)
10410 : : {
10411 : 0 : check_raw_data = true;
10412 : 0 : continue;
10413 : : }
10414 : :
10415 : : /* Avoid non-constant initializers. */
10416 : 601041 : if (!tree_fits_shwi_p (value))
10417 : : {
10418 : 675 : check_raw_data = true;
10419 : 675 : --i;
10420 : 675 : continue;
10421 : : }
10422 : :
10423 : : /* Skip over embedded nuls except the last one (initializer
10424 : : elements are in ascending order of indices). */
10425 : 600366 : HOST_WIDE_INT val = tree_to_shwi (value);
10426 : 600366 : if (!val && i + 1 < nelts)
10427 : 5960 : continue;
10428 : :
10429 : 594406 : if (idx < str.length ())
10430 : : {
10431 : 0 : check_raw_data = true;
10432 : 0 : continue;
10433 : : }
10434 : :
10435 : : /* Bail if the CTOR has a block of more than 256 embedded nuls
10436 : : due to implicitly initialized elements. */
10437 : 594406 : unsigned nchars = (idx - str.length ()) + 1;
10438 : 594406 : if (nchars > 256)
10439 : : {
10440 : 3 : check_raw_data = true;
10441 : 3 : continue;
10442 : : }
10443 : :
10444 : 594403 : if (nchars > 1)
10445 : : {
10446 : 3657 : str.reserve (idx);
10447 : 3657 : str.quick_grow_cleared (idx);
10448 : : }
10449 : :
10450 : 594403 : if (idx >= maxelts)
10451 : : {
10452 : 0 : check_raw_data = true;
10453 : 0 : continue;
10454 : : }
10455 : :
10456 : 594403 : str.safe_insert (idx, val);
10457 : : }
10458 : :
10459 : 45640 : if (check_raw_data)
10460 : : {
10461 : 700 : if (j != HOST_WIDE_INT_M1U)
10462 : 420 : CONSTRUCTOR_ELTS (ctor)->truncate (j);
10463 : 700 : return ctor;
10464 : : }
10465 : :
10466 : : /* Append a nul string termination. */
10467 : 89803 : if (maxelts != HOST_WIDE_INT_M1U && str.length () < maxelts)
10468 : 17735 : str.safe_push (0);
10469 : :
10470 : : /* Build a STRING_CST with the same type as the array. */
10471 : 134820 : tree res = build_string (str.length (), str.begin ());
10472 : 44940 : TREE_TYPE (res) = type;
10473 : 44940 : return res;
10474 : 45640 : }
10475 : :
10476 : : /* Implementation of the two-argument braced_lists_to_string withe
10477 : : the same arguments plus MEMBER which is set for struct members
10478 : : to allow initializers for flexible member arrays. */
10479 : :
10480 : : static tree
10481 : 182542497 : braced_lists_to_strings (tree type, tree ctor, bool member)
10482 : : {
10483 : 182542497 : if (TREE_CODE (ctor) != CONSTRUCTOR)
10484 : : return ctor;
10485 : :
10486 : 5455889 : tree_code code = TREE_CODE (type);
10487 : :
10488 : 5455889 : tree ttp;
10489 : 5455889 : if (code == ARRAY_TYPE)
10490 : 216195 : ttp = TREE_TYPE (type);
10491 : 5239694 : else if (code == RECORD_TYPE)
10492 : : {
10493 : 5193630 : ttp = TREE_TYPE (ctor);
10494 : 5193630 : if (TREE_CODE (ttp) == ARRAY_TYPE)
10495 : : {
10496 : 228891 : type = ttp;
10497 : 228891 : ttp = TREE_TYPE (ttp);
10498 : : }
10499 : : }
10500 : : else
10501 : : return ctor;
10502 : :
10503 : 5407929 : if ((TREE_CODE (ttp) == ARRAY_TYPE || TREE_CODE (ttp) == INTEGER_TYPE)
10504 : 5702175 : && TYPE_STRING_FLAG (ttp))
10505 : 45805 : return braced_list_to_string (type, ctor, member);
10506 : :
10507 : 5364020 : code = TREE_CODE (ttp);
10508 : 5364020 : if (code == ARRAY_TYPE || RECORD_OR_UNION_TYPE_P (ttp))
10509 : : {
10510 : 5020986 : bool rec = RECORD_OR_UNION_TYPE_P (ttp);
10511 : :
10512 : : /* Handle array of arrays or struct member initializers. */
10513 : 5020986 : tree val;
10514 : 5020986 : unsigned HOST_WIDE_INT idx;
10515 : 16186986 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), idx, val)
10516 : : {
10517 : 11166000 : val = braced_lists_to_strings (ttp, val, rec);
10518 : 11166000 : CONSTRUCTOR_ELT (ctor, idx)->value = val;
10519 : : }
10520 : : }
10521 : :
10522 : : return ctor;
10523 : : }
10524 : :
10525 : : /* Attempt to convert a CTOR containing braced array initializer lists
10526 : : for array TYPE into one containing STRING_CSTs, for convenience and
10527 : : efficiency. Recurse for arrays of arrays and member initializers.
10528 : : Return the converted CTOR or STRING_CST on success or the original
10529 : : CTOR otherwise. */
10530 : :
10531 : : tree
10532 : 171376497 : braced_lists_to_strings (tree type, tree ctor)
10533 : : {
10534 : 171376497 : return braced_lists_to_strings (type, ctor, false);
10535 : : }
10536 : :
10537 : :
10538 : : /* Emit debug for functions before finalizing early debug. */
10539 : :
10540 : : void
10541 : 180544 : c_common_finalize_early_debug (void)
10542 : : {
10543 : : /* Emit early debug for reachable functions, and by consequence,
10544 : : locally scoped symbols. Also emit debug for extern declared
10545 : : functions that are still reachable at this point. */
10546 : 180544 : struct cgraph_node *cnode;
10547 : 4271206 : FOR_EACH_FUNCTION (cnode)
10548 : 3717935 : if (!cnode->alias && !cnode->thunk
10549 : 7805536 : && (cnode->has_gimple_body_p ()
10550 : 1273809 : || !DECL_IS_UNDECLARED_BUILTIN (cnode->decl)))
10551 : 3498805 : (*debug_hooks->early_global_decl) (cnode->decl);
10552 : 180544 : }
10553 : :
10554 : : /* Determine whether TYPE is an ISO C99 flexible array member type "[]". */
10555 : : bool
10556 : 10460185 : c_flexible_array_member_type_p (const_tree type)
10557 : : {
10558 : 10460185 : if (TREE_CODE (type) == ARRAY_TYPE
10559 : 3588391 : && TYPE_SIZE (type) == NULL_TREE
10560 : 465667 : && TYPE_DOMAIN (type) != NULL_TREE
10561 : 10925852 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
10562 : 465667 : return true;
10563 : :
10564 : : return false;
10565 : : }
10566 : :
10567 : : /* Get the LEVEL of the strict_flex_array for the ARRAY_FIELD based on the
10568 : : values of attribute strict_flex_array and the flag_strict_flex_arrays. */
10569 : : unsigned int
10570 : 529658 : c_strict_flex_array_level_of (tree array_field)
10571 : : {
10572 : 529658 : gcc_assert (TREE_CODE (array_field) == FIELD_DECL);
10573 : 529658 : unsigned int strict_flex_array_level = flag_strict_flex_arrays;
10574 : :
10575 : 529658 : tree attr_strict_flex_array
10576 : 529658 : = lookup_attribute ("strict_flex_array", DECL_ATTRIBUTES (array_field));
10577 : : /* If there is a strict_flex_array attribute attached to the field,
10578 : : override the flag_strict_flex_arrays. */
10579 : 529658 : if (attr_strict_flex_array)
10580 : : {
10581 : : /* Get the value of the level first from the attribute. */
10582 : 6 : unsigned HOST_WIDE_INT attr_strict_flex_array_level = 0;
10583 : 6 : gcc_assert (TREE_VALUE (attr_strict_flex_array) != NULL_TREE);
10584 : 6 : attr_strict_flex_array = TREE_VALUE (attr_strict_flex_array);
10585 : 6 : gcc_assert (TREE_VALUE (attr_strict_flex_array) != NULL_TREE);
10586 : 6 : attr_strict_flex_array = TREE_VALUE (attr_strict_flex_array);
10587 : 6 : gcc_assert (tree_fits_uhwi_p (attr_strict_flex_array));
10588 : 6 : attr_strict_flex_array_level = tree_to_uhwi (attr_strict_flex_array);
10589 : :
10590 : : /* The attribute has higher priority than flag_struct_flex_array. */
10591 : 6 : strict_flex_array_level = attr_strict_flex_array_level;
10592 : : }
10593 : 529658 : return strict_flex_array_level;
10594 : : }
10595 : :
10596 : : /* Map from identifiers to booleans. Value is true for features, and
10597 : : false for extensions. Used to implement __has_{feature,extension}. */
10598 : :
10599 : : using feature_map_t = hash_map <tree, bool>;
10600 : : static feature_map_t *feature_map;
10601 : :
10602 : : /* Register a feature for __has_{feature,extension}. FEATURE_P is true
10603 : : if the feature identified by NAME is a feature (as opposed to an
10604 : : extension). */
10605 : :
10606 : : void
10607 : 1431758 : c_common_register_feature (const char *name, bool feature_p)
10608 : : {
10609 : 1431758 : bool dup = feature_map->put (get_identifier (name), feature_p);
10610 : 1431758 : gcc_checking_assert (!dup);
10611 : 1431758 : }
10612 : :
10613 : : /* Lazily initialize hash table for __has_{feature,extension},
10614 : : dispatching to the appropriate front end to register language-specific
10615 : : features. */
10616 : :
10617 : : static void
10618 : 46669 : init_has_feature ()
10619 : : {
10620 : 46669 : gcc_checking_assert (!feature_map);
10621 : 46669 : feature_map = new feature_map_t;
10622 : :
10623 : 560028 : for (unsigned i = 0; i < ARRAY_SIZE (has_feature_table); i++)
10624 : : {
10625 : 513359 : const hf_feature_info *info = has_feature_table + i;
10626 : :
10627 : 513359 : if ((info->flags & HF_FLAG_SANITIZE) && !(flag_sanitize & info->mask))
10628 : 232468 : continue;
10629 : :
10630 : 280891 : const bool feature_p = !(info->flags & HF_FLAG_EXT);
10631 : 280891 : c_common_register_feature (info->ident, feature_p);
10632 : : }
10633 : :
10634 : : /* Register language-specific features. */
10635 : 46669 : c_family_register_lang_features ();
10636 : 46669 : }
10637 : :
10638 : : /* If STRICT_P is true, evaluate __has_feature (IDENT).
10639 : : Otherwise, evaluate __has_extension (IDENT). */
10640 : :
10641 : : bool
10642 : 126884 : has_feature_p (const char *ident, bool strict_p)
10643 : : {
10644 : 126884 : if (!feature_map)
10645 : 46669 : init_has_feature ();
10646 : :
10647 : 126884 : tree name = canonicalize_attr_name (get_identifier (ident));
10648 : 126884 : bool *feat_p = feature_map->get (name);
10649 : 126884 : if (!feat_p)
10650 : : return false;
10651 : :
10652 : 72337 : return !strict_p || *feat_p;
10653 : : }
10654 : :
10655 : : /* This is the slow path of c-common.h's c_hardbool_type_attr. */
10656 : :
10657 : : tree
10658 : 66921 : c_hardbool_type_attr_1 (tree type, tree *false_value, tree *true_value)
10659 : : {
10660 : 66921 : tree attr = lookup_attribute ("hardbool", TYPE_ATTRIBUTES (type));
10661 : 66921 : if (!attr)
10662 : : return attr;
10663 : :
10664 : 35375 : if (false_value)
10665 : 18453 : *false_value = TREE_VALUE (TYPE_VALUES (type));
10666 : :
10667 : 35375 : if (true_value)
10668 : 18817 : *true_value = TREE_VALUE (TREE_CHAIN (TYPE_VALUES (type)));
10669 : :
10670 : : return attr;
10671 : : }
10672 : :
10673 : : #include "gt-c-family-c-common.h"
|