Branch data Line data Source code
1 : : /* Subroutines shared by all languages that are variants of C.
2 : : Copyright (C) 1992-2025 Free Software Foundation, Inc.
3 : :
4 : : This file is part of GCC.
5 : :
6 : : GCC is free software; you can redistribute it and/or modify it under
7 : : the terms of the GNU General Public License as published by the Free
8 : : Software Foundation; either version 3, or (at your option) any later
9 : : version.
10 : :
11 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : : for more details.
15 : :
16 : : You should have received a copy of the GNU General Public License
17 : : along with GCC; see the file COPYING3. If not see
18 : : <http://www.gnu.org/licenses/>. */
19 : :
20 : : #define GCC_C_COMMON_C
21 : :
22 : : #include "config.h"
23 : : #include "system.h"
24 : : #include "coretypes.h"
25 : : #include "target.h"
26 : : #include "function.h"
27 : : #include "tree.h"
28 : : #include "memmodel.h"
29 : : #include "c-common.h"
30 : : #include "gimple-expr.h"
31 : : #include "tm_p.h"
32 : : #include "stringpool.h"
33 : : #include "cgraph.h"
34 : : #include "diagnostic.h"
35 : : #include "intl.h"
36 : : #include "stor-layout.h"
37 : : #include "calls.h"
38 : : #include "attribs.h"
39 : : #include "varasm.h"
40 : : #include "trans-mem.h"
41 : : #include "c-objc.h"
42 : : #include "common/common-target.h"
43 : : #include "langhooks.h"
44 : : #include "tree-inline.h"
45 : : #include "toplev.h"
46 : : #include "tree-iterator.h"
47 : : #include "opts.h"
48 : : #include "gimplify.h"
49 : : #include "substring-locations.h"
50 : : #include "spellcheck.h"
51 : : #include "c-spellcheck.h"
52 : : #include "selftest.h"
53 : : #include "debug.h"
54 : : #include "tree-vector-builder.h"
55 : : #include "vec-perm-indices.h"
56 : : #include "tree-pretty-print-markup.h"
57 : : #include "gcc-rich-location.h"
58 : : #include "gcc-urlifier.h"
59 : : #include "diagnostics/diagnostics-selftests.h"
60 : :
61 : : cpp_reader *parse_in; /* Declared in c-pragma.h. */
62 : :
63 : : /* Mode used to build pointers (VOIDmode means ptr_mode). */
64 : :
65 : : machine_mode c_default_pointer_mode = VOIDmode;
66 : :
67 : : /* The following symbols are subsumed in the c_global_trees array, and
68 : : listed here individually for documentation purposes.
69 : :
70 : : INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
71 : :
72 : : tree short_integer_type_node;
73 : : tree long_integer_type_node;
74 : : tree long_long_integer_type_node;
75 : :
76 : : tree short_unsigned_type_node;
77 : : tree long_unsigned_type_node;
78 : : tree long_long_unsigned_type_node;
79 : :
80 : : tree truthvalue_type_node;
81 : : tree truthvalue_false_node;
82 : : tree truthvalue_true_node;
83 : :
84 : : tree ptrdiff_type_node;
85 : :
86 : : tree unsigned_char_type_node;
87 : : tree signed_char_type_node;
88 : : tree wchar_type_node;
89 : :
90 : : tree char8_type_node;
91 : : tree char16_type_node;
92 : : tree char32_type_node;
93 : :
94 : : tree float_type_node;
95 : : tree double_type_node;
96 : : tree long_double_type_node;
97 : :
98 : : tree complex_integer_type_node;
99 : : tree complex_float_type_node;
100 : : tree complex_double_type_node;
101 : : tree complex_long_double_type_node;
102 : :
103 : : tree dfloat32_type_node;
104 : : tree dfloat64_type_node;
105 : : tree dfloat128_type_node;
106 : : tree dfloat64x_type_node;
107 : :
108 : : tree intQI_type_node;
109 : : tree intHI_type_node;
110 : : tree intSI_type_node;
111 : : tree intDI_type_node;
112 : : tree intTI_type_node;
113 : :
114 : : tree unsigned_intQI_type_node;
115 : : tree unsigned_intHI_type_node;
116 : : tree unsigned_intSI_type_node;
117 : : tree unsigned_intDI_type_node;
118 : : tree unsigned_intTI_type_node;
119 : :
120 : : tree widest_integer_literal_type_node;
121 : : tree widest_unsigned_literal_type_node;
122 : :
123 : : Nodes for types `void *' and `const void *'.
124 : :
125 : : tree ptr_type_node, const_ptr_type_node;
126 : :
127 : : Nodes for types `char *' and `const char *'.
128 : :
129 : : tree string_type_node, const_string_type_node;
130 : :
131 : : Type `char[SOMENUMBER]'.
132 : : Used when an array of char is needed and the size is irrelevant.
133 : :
134 : : tree char_array_type_node;
135 : :
136 : : Type `wchar_t[SOMENUMBER]' or something like it.
137 : : Used when a wide string literal is created.
138 : :
139 : : tree wchar_array_type_node;
140 : :
141 : : Type `char8_t[SOMENUMBER]' or something like it.
142 : : Used when a UTF-8 string literal is created.
143 : :
144 : : tree char8_array_type_node;
145 : :
146 : : Type `char16_t[SOMENUMBER]' or something like it.
147 : : Used when a UTF-16 string literal is created.
148 : :
149 : : tree char16_array_type_node;
150 : :
151 : : Type `char32_t[SOMENUMBER]' or something like it.
152 : : Used when a UTF-32 string literal is created.
153 : :
154 : : tree char32_array_type_node;
155 : :
156 : : Type `int ()' -- used for implicit declaration of functions.
157 : :
158 : : tree default_function_type;
159 : :
160 : : A VOID_TYPE node, packaged in a TREE_LIST.
161 : :
162 : : tree void_list_node;
163 : :
164 : : The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
165 : : and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
166 : : VAR_DECLS, but C++ does.)
167 : :
168 : : tree function_name_decl_node;
169 : : tree pretty_function_name_decl_node;
170 : : tree c99_function_name_decl_node;
171 : :
172 : : Stack of nested function name VAR_DECLs.
173 : :
174 : : tree saved_function_name_decls;
175 : :
176 : : */
177 : :
178 : : tree c_global_trees[CTI_MAX];
179 : :
180 : : /* Switches common to the C front ends. */
181 : :
182 : : /* Nonzero means don't output line number information. */
183 : :
184 : : char flag_no_line_commands;
185 : :
186 : : /* Nonzero causes -E output not to be done, but directives such as
187 : : #define that have side effects are still obeyed. */
188 : :
189 : : char flag_no_output;
190 : :
191 : : /* Nonzero means dump macros in some fashion. */
192 : :
193 : : char flag_dump_macros;
194 : :
195 : : /* Nonzero means pass #include lines through to the output. */
196 : :
197 : : char flag_dump_includes;
198 : :
199 : : /* Nonzero means process PCH files while preprocessing. */
200 : :
201 : : bool flag_pch_preprocess;
202 : :
203 : : /* The file name to which we should write a precompiled header, or
204 : : NULL if no header will be written in this compile. */
205 : :
206 : : const char *pch_file;
207 : :
208 : : /* Nonzero if an ISO standard was selected. It rejects macros in the
209 : : user's namespace. */
210 : : int flag_iso;
211 : :
212 : : /* C/ObjC language option variables. */
213 : :
214 : :
215 : : /* Nonzero means allow type mismatches in conditional expressions;
216 : : just make their values `void'. */
217 : :
218 : : int flag_cond_mismatch;
219 : :
220 : : /* Nonzero means enable C89 Amendment 1 features. */
221 : :
222 : : int flag_isoc94;
223 : :
224 : : /* Nonzero means use the ISO C99 (or later) dialect of C. */
225 : :
226 : : int flag_isoc99;
227 : :
228 : : /* Nonzero means use the ISO C11 (or later) dialect of C. */
229 : :
230 : : int flag_isoc11;
231 : :
232 : : /* Nonzero means use the ISO C23 (or later) dialect of C. */
233 : :
234 : : int flag_isoc23;
235 : :
236 : : /* Nonzero means use the ISO C2Y (or later) dialect of C. */
237 : :
238 : : int flag_isoc2y;
239 : :
240 : : /* Nonzero means that we have builtin functions, and main is an int. */
241 : :
242 : : int flag_hosted = 1;
243 : :
244 : :
245 : : /* ObjC language option variables. */
246 : :
247 : :
248 : : /* Tells the compiler that this is a special run. Do not perform any
249 : : compiling, instead we are to test some platform dependent features
250 : : and output a C header file with appropriate definitions. */
251 : :
252 : : int print_struct_values;
253 : :
254 : : /* Tells the compiler what is the constant string class for ObjC. */
255 : :
256 : : const char *constant_string_class_name;
257 : :
258 : :
259 : : /* C++ language option variables. */
260 : :
261 : : /* The reference version of the ABI for -Wabi. */
262 : :
263 : : int warn_abi_version = -1;
264 : :
265 : : /* The C++ dialect being used. Default set in c_common_post_options. */
266 : :
267 : : enum cxx_dialect cxx_dialect = cxx_unset;
268 : :
269 : : /* Maximum template instantiation depth. This limit exists to limit the
270 : : time it takes to notice excessively recursive template instantiations.
271 : :
272 : : The default is lower than the 1024 recommended by the C++0x standard
273 : : because G++ runs out of stack before 1024 with highly recursive template
274 : : argument deduction substitution (g++.dg/cpp0x/enum11.C). */
275 : :
276 : : int max_tinst_depth = 900;
277 : :
278 : : /* The elements of `ridpointers' are identifier nodes for the reserved
279 : : type names and storage classes. It is indexed by a RID_... value. */
280 : : tree *ridpointers;
281 : :
282 : : tree (*make_fname_decl) (location_t, tree, int);
283 : :
284 : : /* Nonzero means don't warn about problems that occur when the code is
285 : : executed. */
286 : : int c_inhibit_evaluation_warnings;
287 : :
288 : : /* Whether we are building a boolean conversion inside
289 : : convert_for_assignment, or some other late binary operation. If
290 : : build_binary_op is called for C (from code shared by C and C++) in
291 : : this case, then the operands have already been folded and the
292 : : result will not be folded again, so C_MAYBE_CONST_EXPR should not
293 : : be generated. */
294 : : bool in_late_binary_op;
295 : :
296 : : /* Depending on which phase of processing we are in, we may need
297 : : to prefer input_location to libcpp's locations. (Specifically,
298 : : after the C++ lexer is done lexing tokens, but prior to calling
299 : : cpp_finish (), we need to do so. */
300 : : bool override_libcpp_locations;
301 : :
302 : : /* Information about how a function name is generated. */
303 : : struct fname_var_t
304 : : {
305 : : tree *const decl; /* pointer to the VAR_DECL. */
306 : : const unsigned rid; /* RID number for the identifier. */
307 : : const int pretty; /* How pretty is it? */
308 : : };
309 : :
310 : : /* The three ways of getting then name of the current function. */
311 : :
312 : : const struct fname_var_t fname_vars[] =
313 : : {
314 : : /* C99 compliant __func__, must be first. */
315 : : {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
316 : : /* GCC __FUNCTION__ compliant. */
317 : : {&function_name_decl_node, RID_FUNCTION_NAME, 0},
318 : : /* GCC __PRETTY_FUNCTION__ compliant. */
319 : : {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
320 : : {NULL, 0, 0},
321 : : };
322 : :
323 : : /* Flags to restrict availability of generic features that
324 : : are known to __has_{feature,extension}. */
325 : :
326 : : enum
327 : : {
328 : : HF_FLAG_NONE = 0,
329 : : HF_FLAG_EXT = 1, /* Available only as an extension. */
330 : : HF_FLAG_SANITIZE = 2, /* Availability depends on sanitizer flags. */
331 : : };
332 : :
333 : : /* Info for generic features which can be queried through
334 : : __has_{feature,extension}. */
335 : :
336 : : struct hf_feature_info
337 : : {
338 : : const char *ident;
339 : : unsigned flags;
340 : : unsigned mask;
341 : : };
342 : :
343 : : /* Table of generic features which can be queried through
344 : : __has_{feature,extension}. */
345 : :
346 : : static constexpr hf_feature_info has_feature_table[] =
347 : : {
348 : : { "address_sanitizer", HF_FLAG_SANITIZE, SANITIZE_ADDRESS },
349 : : { "thread_sanitizer", HF_FLAG_SANITIZE, SANITIZE_THREAD },
350 : : { "leak_sanitizer", HF_FLAG_SANITIZE, SANITIZE_LEAK },
351 : : { "hwaddress_sanitizer", HF_FLAG_SANITIZE, SANITIZE_HWADDRESS },
352 : : { "undefined_behavior_sanitizer", HF_FLAG_SANITIZE, SANITIZE_UNDEFINED },
353 : : { "attribute_deprecated_with_message", HF_FLAG_NONE, 0 },
354 : : { "attribute_unavailable_with_message", HF_FLAG_NONE, 0 },
355 : : { "enumerator_attributes", HF_FLAG_NONE, 0 },
356 : : { "tls", HF_FLAG_NONE, 0 },
357 : : { "gnu_asm_goto_with_outputs", HF_FLAG_EXT, 0 },
358 : : { "gnu_asm_goto_with_outputs_full", HF_FLAG_EXT, 0 }
359 : : };
360 : :
361 : : /* Global visibility options. */
362 : : struct visibility_flags visibility_options;
363 : :
364 : : static tree check_case_value (location_t, tree);
365 : :
366 : :
367 : : static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
368 : : static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
369 : :
370 : : /* Reserved words. The third field is a mask: keywords are disabled
371 : : if they match the mask.
372 : :
373 : : Masks for languages:
374 : : C --std=c89: D_C99 | D_C23 | D_CXXONLY | D_OBJC | D_CXX_OBJC
375 : : C --std=c99: D_C23 | D_CXXONLY | D_OBJC
376 : : C --std=c17: D_C23 | D_CXXONLY | D_OBJC
377 : : C --std=c23: D_CXXONLY | D_OBJC
378 : : ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
379 : : C++ --std=c++98: D_CONLY | D_CXX11 | D_CXX20 | D_CXX26 | D_OBJC
380 : : C++ --std=c++11: D_CONLY | D_CXX20 | D_CXX26 | D_OBJC
381 : : C++ --std=c++20: D_CONLY | D_CXX26 | D_OBJC
382 : : C++ --std=c++26: D_CONLY | D_OBJC
383 : : ObjC++ is like C++ except that D_OBJC is not set
384 : :
385 : : If -fno-asm is used, D_ASM is added to the mask. If
386 : : -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
387 : : C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
388 : : In C with -Wc++-compat, we warn if D_CXXWARN is set.
389 : :
390 : : Note the complication of the D_CXX_OBJC keywords. These are
391 : : reserved words such as 'class'. In C++, 'class' is a reserved
392 : : word. In Objective-C++ it is too. In Objective-C, it is a
393 : : reserved word too, but only if it follows an '@' sign.
394 : : */
395 : : const struct c_common_resword c_common_reswords[] =
396 : : {
397 : : { "_Alignas", RID_ALIGNAS, D_CONLY },
398 : : { "_Alignof", RID_ALIGNOF, D_CONLY },
399 : : { "_Countof", RID_COUNTOF, D_CONLY },
400 : : { "_Atomic", RID_ATOMIC, D_CONLY },
401 : : { "_BitInt", RID_BITINT, D_CONLY },
402 : : { "_Bool", RID_BOOL, D_CONLY },
403 : : { "_Complex", RID_COMPLEX, 0 },
404 : : { "_Imaginary", RID_IMAGINARY, D_CONLY },
405 : : { "_Float16", RID_FLOAT16, 0 },
406 : : { "_Float32", RID_FLOAT32, 0 },
407 : : { "_Float64", RID_FLOAT64, 0 },
408 : : { "_Float128", RID_FLOAT128, 0 },
409 : : { "_Float32x", RID_FLOAT32X, 0 },
410 : : { "_Float64x", RID_FLOAT64X, 0 },
411 : : { "_Float128x", RID_FLOAT128X, 0 },
412 : : { "_Decimal32", RID_DFLOAT32, D_CONLY },
413 : : { "_Decimal64", RID_DFLOAT64, D_CONLY },
414 : : { "_Decimal128", RID_DFLOAT128, D_CONLY },
415 : : { "_Decimal64x", RID_DFLOAT64X, D_CONLY },
416 : : { "_Fract", RID_FRACT, D_CONLY | D_EXT },
417 : : { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
418 : : { "_Sat", RID_SAT, D_CONLY | D_EXT },
419 : : { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
420 : : { "_Noreturn", RID_NORETURN, D_CONLY },
421 : : { "_Generic", RID_GENERIC, D_CONLY },
422 : : { "_Thread_local", RID_THREAD, D_CONLY },
423 : : { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
424 : : { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
425 : : { "__alignof", RID_ALIGNOF, 0 },
426 : : { "__alignof__", RID_ALIGNOF, 0 },
427 : : { "__asm", RID_ASM, 0 },
428 : : { "__asm__", RID_ASM, 0 },
429 : : { "__attribute", RID_ATTRIBUTE, 0 },
430 : : { "__attribute__", RID_ATTRIBUTE, 0 },
431 : : { "__auto_type", RID_AUTO_TYPE, D_CONLY },
432 : : { "__builtin_addressof", RID_ADDRESSOF, D_CXXONLY },
433 : : { "__builtin_assoc_barrier", RID_BUILTIN_ASSOC_BARRIER, 0 },
434 : : { "__builtin_bit_cast", RID_BUILTIN_BIT_CAST, D_CXXONLY },
435 : : { "__builtin_call_with_static_chain",
436 : : RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
437 : : { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
438 : : { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
439 : : { "__builtin_convertvector", RID_BUILTIN_CONVERTVECTOR, 0 },
440 : : { "__builtin_counted_by_ref", RID_BUILTIN_COUNTED_BY_REF, D_CONLY },
441 : : { "__builtin_has_attribute", RID_BUILTIN_HAS_ATTRIBUTE, 0 },
442 : : { "__builtin_launder", RID_BUILTIN_LAUNDER, D_CXXONLY },
443 : : { "__builtin_operator_new", RID_BUILTIN_OPERATOR_NEW, D_CXXONLY },
444 : : { "__builtin_operator_delete", RID_BUILTIN_OPERATOR_DELETE, D_CXXONLY },
445 : : { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
446 : : { "__builtin_shufflevector", RID_BUILTIN_SHUFFLEVECTOR, 0 },
447 : : { "__builtin_stdc_bit_ceil", RID_BUILTIN_STDC, D_CONLY },
448 : : { "__builtin_stdc_bit_floor", RID_BUILTIN_STDC, D_CONLY },
449 : : { "__builtin_stdc_bit_width", RID_BUILTIN_STDC, D_CONLY },
450 : : { "__builtin_stdc_count_ones", RID_BUILTIN_STDC, D_CONLY },
451 : : { "__builtin_stdc_count_zeros", RID_BUILTIN_STDC, D_CONLY },
452 : : { "__builtin_stdc_first_leading_one", RID_BUILTIN_STDC, D_CONLY },
453 : : { "__builtin_stdc_first_leading_zero", RID_BUILTIN_STDC, D_CONLY },
454 : : { "__builtin_stdc_first_trailing_one", RID_BUILTIN_STDC, D_CONLY },
455 : : { "__builtin_stdc_first_trailing_zero", RID_BUILTIN_STDC, D_CONLY },
456 : : { "__builtin_stdc_has_single_bit", RID_BUILTIN_STDC, D_CONLY },
457 : : { "__builtin_stdc_leading_ones", RID_BUILTIN_STDC, D_CONLY },
458 : : { "__builtin_stdc_leading_zeros", RID_BUILTIN_STDC, D_CONLY },
459 : : { "__builtin_stdc_rotate_left", RID_BUILTIN_STDC, D_CONLY },
460 : : { "__builtin_stdc_rotate_right", RID_BUILTIN_STDC, D_CONLY },
461 : : { "__builtin_stdc_trailing_ones", RID_BUILTIN_STDC, D_CONLY },
462 : : { "__builtin_stdc_trailing_zeros", RID_BUILTIN_STDC, D_CONLY },
463 : : { "__builtin_tgmath", RID_BUILTIN_TGMATH, D_CONLY },
464 : : { "__builtin_offsetof", RID_OFFSETOF, 0 },
465 : : { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
466 : : { "__builtin_c23_va_start", RID_C23_VA_START, D_C23 | D_CXX26 },
467 : : { "__builtin_va_arg", RID_VA_ARG, 0 },
468 : : { "__complex", RID_COMPLEX, 0 },
469 : : { "__complex__", RID_COMPLEX, 0 },
470 : : { "__const", RID_CONST, 0 },
471 : : { "__const__", RID_CONST, 0 },
472 : : { "__constinit", RID_CONSTINIT, D_CXXONLY },
473 : : { "__decltype", RID_DECLTYPE, D_CXXONLY },
474 : : { "__extension__", RID_EXTENSION, 0 },
475 : : { "__func__", RID_C99_FUNCTION_NAME, 0 },
476 : : { "__imag", RID_IMAGPART, 0 },
477 : : { "__imag__", RID_IMAGPART, 0 },
478 : : { "__inline", RID_INLINE, 0 },
479 : : { "__inline__", RID_INLINE, 0 },
480 : : { "__label__", RID_LABEL, 0 },
481 : : { "__null", RID_NULL, 0 },
482 : : { "__real", RID_REALPART, 0 },
483 : : { "__real__", RID_REALPART, 0 },
484 : : { "__restrict", RID_RESTRICT, 0 },
485 : : { "__restrict__", RID_RESTRICT, 0 },
486 : : { "__signed", RID_SIGNED, 0 },
487 : : { "__signed__", RID_SIGNED, 0 },
488 : : { "__thread", RID_THREAD, 0 },
489 : : { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
490 : : { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
491 : : { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
492 : : { "__typeof", RID_TYPEOF, 0 },
493 : : { "__typeof__", RID_TYPEOF, 0 },
494 : : { "__typeof_unqual", RID_TYPEOF_UNQUAL, D_CONLY },
495 : : { "__typeof_unqual__", RID_TYPEOF_UNQUAL, D_CONLY },
496 : : { "__volatile", RID_VOLATILE, 0 },
497 : : { "__volatile__", RID_VOLATILE, 0 },
498 : : { "__GIMPLE", RID_GIMPLE, D_CONLY },
499 : : { "__PHI", RID_PHI, D_CONLY },
500 : : { "__RTL", RID_RTL, D_CONLY },
501 : : { "alignas", RID_ALIGNAS, D_C23 | D_CXX11 | D_CXXWARN },
502 : : { "alignof", RID_ALIGNOF, D_C23 | D_CXX11 | D_CXXWARN },
503 : : { "asm", RID_ASM, D_ASM },
504 : : { "auto", RID_AUTO, 0 },
505 : : { "bool", RID_BOOL, D_C23 | D_CXXWARN },
506 : : { "break", RID_BREAK, 0 },
507 : : { "case", RID_CASE, 0 },
508 : : { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
509 : : { "char", RID_CHAR, 0 },
510 : : { "char8_t", RID_CHAR8, D_CXX_CHAR8_T_FLAGS | D_CXXWARN },
511 : : { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
512 : : { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
513 : : { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
514 : : { "const", RID_CONST, 0 },
515 : : { "consteval", RID_CONSTEVAL, D_CXXONLY | D_CXX20 | D_CXXWARN },
516 : : { "constexpr", RID_CONSTEXPR, D_C23 | D_CXX11 | D_CXXWARN },
517 : : { "constinit", RID_CONSTINIT, D_CXXONLY | D_CXX20 | D_CXXWARN },
518 : : { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
519 : : { "continue", RID_CONTINUE, 0 },
520 : : { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
521 : : { "default", RID_DEFAULT, 0 },
522 : : { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
523 : : { "do", RID_DO, 0 },
524 : : { "double", RID_DOUBLE, 0 },
525 : : { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
526 : : { "else", RID_ELSE, 0 },
527 : : { "enum", RID_ENUM, 0 },
528 : : { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
529 : : { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
530 : : { "extern", RID_EXTERN, 0 },
531 : : { "false", RID_FALSE, D_C23 | D_CXXWARN },
532 : : { "float", RID_FLOAT, 0 },
533 : : { "for", RID_FOR, 0 },
534 : : { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
535 : : { "goto", RID_GOTO, 0 },
536 : : { "if", RID_IF, 0 },
537 : : { "inline", RID_INLINE, D_EXT89 },
538 : : { "int", RID_INT, 0 },
539 : : { "long", RID_LONG, 0 },
540 : : { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
541 : : { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
542 : : { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
543 : : { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
544 : : { "nullptr", RID_NULLPTR, D_C23 | D_CXX11 | D_CXXWARN },
545 : : { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
546 : : { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
547 : : { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
548 : : { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
549 : : { "register", RID_REGISTER, 0 },
550 : : { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
551 : : { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
552 : : { "return", RID_RETURN, 0 },
553 : : { "short", RID_SHORT, 0 },
554 : : { "signed", RID_SIGNED, 0 },
555 : : { "sizeof", RID_SIZEOF, 0 },
556 : : { "static", RID_STATIC, 0 },
557 : : { "static_assert", RID_STATIC_ASSERT, D_C23 | D_CXX11 | D_CXXWARN },
558 : : { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
559 : : { "struct", RID_STRUCT, 0 },
560 : : { "switch", RID_SWITCH, 0 },
561 : : { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
562 : : { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
563 : : { "thread_local", RID_THREAD, D_C23 | D_CXX11 | D_CXXWARN },
564 : : { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
565 : : { "true", RID_TRUE, D_C23 | D_CXXWARN },
566 : : { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
567 : : { "typedef", RID_TYPEDEF, 0 },
568 : : { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
569 : : { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
570 : : { "typeof", RID_TYPEOF, D_EXT11 },
571 : : { "typeof_unqual", RID_TYPEOF_UNQUAL, D_CONLY | D_C23 },
572 : : { "union", RID_UNION, 0 },
573 : : { "unsigned", RID_UNSIGNED, 0 },
574 : : { "using", RID_USING, D_CXXONLY | D_CXXWARN },
575 : : { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
576 : : { "void", RID_VOID, 0 },
577 : : { "volatile", RID_VOLATILE, 0 },
578 : : { "wchar_t", RID_WCHAR, D_CXXONLY },
579 : : { "while", RID_WHILE, 0 },
580 : :
581 : : /* C++ transactional memory. */
582 : : { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
583 : : { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
584 : : { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
585 : : { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
586 : :
587 : : /* Concepts-related keywords */
588 : : { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
589 : : { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
590 : :
591 : : /* Modules-related keywords, these are internal unspellable tokens,
592 : : created by the preprocessor. */
593 : : { "module ", RID__MODULE, D_CXX_MODULES_FLAGS | D_CXXWARN },
594 : : { "import ", RID__IMPORT, D_CXX_MODULES_FLAGS | D_CXXWARN },
595 : : { "export ", RID__EXPORT, D_CXX_MODULES_FLAGS | D_CXXWARN },
596 : :
597 : : /* Coroutines-related keywords */
598 : : { "co_await", RID_CO_AWAIT, D_CXX_COROUTINES_FLAGS | D_CXXWARN },
599 : : { "co_yield", RID_CO_YIELD, D_CXX_COROUTINES_FLAGS | D_CXXWARN },
600 : : { "co_return", RID_CO_RETURN, D_CXX_COROUTINES_FLAGS | D_CXXWARN },
601 : :
602 : : /* These Objective-C keywords are recognized only immediately after
603 : : an '@'. */
604 : : { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
605 : : { "defs", RID_AT_DEFS, D_OBJC },
606 : : { "encode", RID_AT_ENCODE, D_OBJC },
607 : : { "end", RID_AT_END, D_OBJC },
608 : : { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
609 : : { "interface", RID_AT_INTERFACE, D_OBJC },
610 : : { "protocol", RID_AT_PROTOCOL, D_OBJC },
611 : : { "selector", RID_AT_SELECTOR, D_OBJC },
612 : : { "finally", RID_AT_FINALLY, D_OBJC },
613 : : { "optional", RID_AT_OPTIONAL, D_OBJC },
614 : : { "required", RID_AT_REQUIRED, D_OBJC },
615 : : { "property", RID_AT_PROPERTY, D_OBJC },
616 : : { "package", RID_AT_PACKAGE, D_OBJC },
617 : : { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
618 : : { "dynamic", RID_AT_DYNAMIC, D_OBJC },
619 : : /* These are recognized only in protocol-qualifier context
620 : : (see above) */
621 : : { "bycopy", RID_BYCOPY, D_OBJC },
622 : : { "byref", RID_BYREF, D_OBJC },
623 : : { "in", RID_IN, D_OBJC },
624 : : { "inout", RID_INOUT, D_OBJC },
625 : : { "oneway", RID_ONEWAY, D_OBJC },
626 : : { "out", RID_OUT, D_OBJC },
627 : : /* These are recognized inside a property attribute list */
628 : : { "assign", RID_ASSIGN, D_OBJC },
629 : : { "atomic", RID_PROPATOMIC, D_OBJC },
630 : : { "copy", RID_COPY, D_OBJC },
631 : : { "getter", RID_GETTER, D_OBJC },
632 : : { "nonatomic", RID_NONATOMIC, D_OBJC },
633 : : { "readonly", RID_READONLY, D_OBJC },
634 : : { "readwrite", RID_READWRITE, D_OBJC },
635 : : { "retain", RID_RETAIN, D_OBJC },
636 : : { "setter", RID_SETTER, D_OBJC },
637 : : /* These are Objective C implementation of nullability, accepted only in
638 : : specific contexts. */
639 : : { "null_unspecified", RID_NULL_UNSPECIFIED, D_OBJC },
640 : : { "nullable", RID_NULLABLE, D_OBJC },
641 : : { "nonnull", RID_NONNULL, D_OBJC },
642 : : { "null_resettable", RID_NULL_RESETTABLE, D_OBJC },
643 : : };
644 : :
645 : : const unsigned int num_c_common_reswords = ARRAY_SIZE (c_common_reswords);
646 : :
647 : : /* Return identifier for address space AS. */
648 : :
649 : : const char *
650 : 3 : c_addr_space_name (addr_space_t as)
651 : : {
652 : 3 : int rid = RID_FIRST_ADDR_SPACE + as;
653 : 3 : gcc_assert (ridpointers [rid]);
654 : 3 : return IDENTIFIER_POINTER (ridpointers [rid]);
655 : : }
656 : :
657 : : /* Push current bindings for the function name VAR_DECLS. */
658 : :
659 : : void
660 : 178226898 : start_fname_decls (void)
661 : : {
662 : 178226898 : unsigned ix;
663 : 178226898 : tree saved = NULL_TREE;
664 : :
665 : 712907592 : for (ix = 0; fname_vars[ix].decl; ix++)
666 : : {
667 : 534680694 : tree decl = *fname_vars[ix].decl;
668 : :
669 : 534680694 : if (decl)
670 : : {
671 : 51989 : saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
672 : : saved);
673 : 51989 : *fname_vars[ix].decl = NULL_TREE;
674 : : }
675 : : }
676 : 178226898 : if (saved || saved_function_name_decls)
677 : : /* Normally they'll have been NULL, so only push if we've got a
678 : : stack, or they are non-NULL. */
679 : 71553 : saved_function_name_decls = tree_cons (saved, NULL_TREE,
680 : : saved_function_name_decls);
681 : 178226898 : }
682 : :
683 : : /* Finish up the current bindings, adding them into the current function's
684 : : statement tree. This must be done _before_ finish_stmt_tree is called.
685 : : If there is no current function, we must be at file scope and no statements
686 : : are involved. Pop the previous bindings. */
687 : :
688 : : void
689 : 178112113 : finish_fname_decls (void)
690 : : {
691 : 178112113 : unsigned ix;
692 : 178112113 : tree stmts = NULL_TREE;
693 : 178112113 : tree stack = saved_function_name_decls;
694 : :
695 : 178378008 : for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
696 : 97171 : append_to_statement_list (TREE_VALUE (stack), &stmts);
697 : :
698 : 178112113 : if (stmts)
699 : : {
700 : 97066 : tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
701 : :
702 : 97066 : if (TREE_CODE (*bodyp) == BIND_EXPR)
703 : 93147 : bodyp = &BIND_EXPR_BODY (*bodyp);
704 : :
705 : 97066 : append_to_statement_list_force (*bodyp, &stmts);
706 : 97066 : *bodyp = stmts;
707 : : }
708 : :
709 : 712448452 : for (ix = 0; fname_vars[ix].decl; ix++)
710 : 534336339 : *fname_vars[ix].decl = NULL_TREE;
711 : :
712 : 178112113 : if (stack)
713 : : {
714 : : /* We had saved values, restore them. */
715 : 71553 : tree saved;
716 : :
717 : 123542 : for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
718 : : {
719 : 51989 : tree decl = TREE_PURPOSE (saved);
720 : 51989 : unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
721 : :
722 : 51989 : *fname_vars[ix].decl = decl;
723 : : }
724 : 71553 : stack = TREE_CHAIN (stack);
725 : : }
726 : 178112113 : saved_function_name_decls = stack;
727 : 178112113 : }
728 : :
729 : : /* Return the text name of the current function, suitably prettified
730 : : by PRETTY_P. Return string must be freed by caller. */
731 : :
732 : : const char *
733 : 3485 : fname_as_string (int pretty_p)
734 : : {
735 : 3485 : const char *name = "top level";
736 : 3485 : char *namep;
737 : 3485 : int vrb = 2, len;
738 : 3485 : cpp_string cstr = { 0, 0 }, strname;
739 : :
740 : 3485 : if (!pretty_p)
741 : : {
742 : 1886 : name = "";
743 : 1886 : vrb = 0;
744 : : }
745 : :
746 : 3485 : if (current_function_decl)
747 : 3478 : name = lang_hooks.decl_printable_name (current_function_decl, vrb);
748 : :
749 : 3485 : len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
750 : :
751 : 3485 : namep = XNEWVEC (char, len);
752 : 3485 : snprintf (namep, len, "\"%s\"", name);
753 : 3485 : strname.text = (unsigned char *) namep;
754 : 3485 : strname.len = len - 1;
755 : :
756 : 3485 : if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
757 : : {
758 : 3485 : XDELETEVEC (namep);
759 : 3485 : return (const char *) cstr.text;
760 : : }
761 : :
762 : : return namep;
763 : : }
764 : :
765 : : /* Return the VAR_DECL for a const char array naming the current
766 : : function. If the VAR_DECL has not yet been created, create it
767 : : now. RID indicates how it should be formatted and IDENTIFIER_NODE
768 : : ID is its name (unfortunately C and C++ hold the RID values of
769 : : keywords in different places, so we can't derive RID from ID in
770 : : this language independent code. LOC is the location of the
771 : : function. */
772 : :
773 : : tree
774 : 196524 : fname_decl (location_t loc, unsigned int rid, tree id)
775 : : {
776 : 196524 : unsigned ix;
777 : 196524 : tree decl = NULL_TREE;
778 : :
779 : 569007 : for (ix = 0; fname_vars[ix].decl; ix++)
780 : 569007 : if (fname_vars[ix].rid == rid)
781 : : break;
782 : :
783 : 196524 : decl = *fname_vars[ix].decl;
784 : 196524 : if (!decl)
785 : : {
786 : : /* If a tree is built here, it would normally have the lineno of
787 : : the current statement. Later this tree will be moved to the
788 : : beginning of the function and this line number will be wrong.
789 : : To avoid this problem set the lineno to 0 here; that prevents
790 : : it from appearing in the RTL. */
791 : 97171 : tree stmts;
792 : 97171 : location_t saved_location = input_location;
793 : 97171 : input_location = UNKNOWN_LOCATION;
794 : :
795 : 97171 : stmts = push_stmt_list ();
796 : 97171 : decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
797 : 97171 : stmts = pop_stmt_list (stmts);
798 : 97171 : if (!IS_EMPTY_STMT (stmts))
799 : 97171 : saved_function_name_decls
800 : 97171 : = tree_cons (decl, stmts, saved_function_name_decls);
801 : 97171 : *fname_vars[ix].decl = decl;
802 : 97171 : input_location = saved_location;
803 : : }
804 : 196524 : if (!ix && !current_function_decl)
805 : 4 : pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
806 : :
807 : 196524 : return decl;
808 : : }
809 : :
810 : : /* Given a STRING_CST, give it a suitable array-of-chars data type. */
811 : :
812 : : tree
813 : 25782659 : fix_string_type (tree value)
814 : : {
815 : 25782659 : int length = TREE_STRING_LENGTH (value);
816 : 25782659 : int nchars, charsz;
817 : 25782659 : tree e_type, i_type, a_type;
818 : :
819 : : /* Compute the number of elements, for the array type. */
820 : 25782659 : if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
821 : : {
822 : 25518801 : charsz = 1;
823 : 25518801 : e_type = char_type_node;
824 : : }
825 : 263858 : else if (flag_char8_t && TREE_TYPE (value) == char8_array_type_node)
826 : : {
827 : 389 : charsz = TYPE_PRECISION (char8_type_node) / BITS_PER_UNIT;
828 : 389 : e_type = char8_type_node;
829 : : }
830 : 263469 : else if (TREE_TYPE (value) == char16_array_type_node)
831 : : {
832 : 956 : charsz = TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT;
833 : 956 : e_type = char16_type_node;
834 : : }
835 : 262513 : else if (TREE_TYPE (value) == char32_array_type_node)
836 : : {
837 : 3044 : charsz = TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT;
838 : 3044 : e_type = char32_type_node;
839 : : }
840 : : else
841 : : {
842 : 259469 : charsz = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
843 : 259469 : e_type = wchar_type_node;
844 : : }
845 : :
846 : : /* This matters only for targets where ssizetype has smaller precision
847 : : than 32 bits. */
848 : 25782659 : if (wi::lts_p (wi::to_wide (TYPE_MAX_VALUE (ssizetype)), length))
849 : : {
850 : 0 : error ("size of string literal is too large");
851 : 0 : length = tree_to_shwi (TYPE_MAX_VALUE (ssizetype)) / charsz * charsz;
852 : 0 : char *str = CONST_CAST (char *, TREE_STRING_POINTER (value));
853 : 0 : memset (str + length, '\0',
854 : 0 : MIN (TREE_STRING_LENGTH (value) - length, charsz));
855 : 0 : TREE_STRING_LENGTH (value) = length;
856 : : }
857 : 25782659 : nchars = length / charsz;
858 : :
859 : : /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
860 : : limit in C++98 Annex B is very large (65536) and is not normative,
861 : : so we do not diagnose it (warn_overlength_strings is forced off
862 : : in c_common_post_options). */
863 : 25782659 : if (warn_overlength_strings)
864 : : {
865 : 15000 : const int nchars_max = flag_isoc99 ? 4095 : 509;
866 : 15000 : const int relevant_std = flag_isoc99 ? 99 : 90;
867 : 15000 : if (nchars - 1 > nchars_max)
868 : : /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
869 : : separate the %d from the 'C'. 'ISO' should not be
870 : : translated, but it may be moved after 'C%d' in languages
871 : : where modifiers follow nouns. */
872 : 5 : pedwarn (input_location, OPT_Woverlength_strings,
873 : : "string length %qd is greater than the length %qd "
874 : : "ISO C%d compilers are required to support",
875 : : nchars - 1, nchars_max, relevant_std);
876 : : }
877 : :
878 : : /* Create the array type for the string constant. The ISO C++
879 : : standard says that a string literal has type `const char[N]' or
880 : : `const wchar_t[N]'. We use the same logic when invoked as a C
881 : : front-end with -Wwrite-strings.
882 : : ??? We should change the type of an expression depending on the
883 : : state of a warning flag. We should just be warning -- see how
884 : : this is handled in the C++ front-end for the deprecated implicit
885 : : conversion from string literals to `char*' or `wchar_t*'.
886 : :
887 : : The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
888 : : array type being the unqualified version of that type.
889 : : Therefore, if we are constructing an array of const char, we must
890 : : construct the matching unqualified array type first. The C front
891 : : end does not require this, but it does no harm, so we do it
892 : : unconditionally. */
893 : 25782659 : i_type = build_index_type (size_int (nchars - 1));
894 : 25782659 : a_type = build_array_type (e_type, i_type);
895 : 25782659 : if (c_dialect_cxx() || warn_write_strings)
896 : 23454735 : a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
897 : :
898 : 25782659 : TREE_TYPE (value) = a_type;
899 : 25782659 : TREE_CONSTANT (value) = 1;
900 : 25782659 : TREE_READONLY (value) = 1;
901 : 25782659 : TREE_STATIC (value) = 1;
902 : 25782659 : return value;
903 : : }
904 : :
905 : : /* Given a string of type STRING_TYPE, determine what kind of string
906 : : token would give an equivalent execution encoding: CPP_STRING,
907 : : CPP_STRING16, or CPP_STRING32. Return CPP_OTHER in case of error.
908 : : This may not be exactly the string token type that initially created
909 : : the string, since CPP_WSTRING is indistinguishable from the 16/32 bit
910 : : string type, and CPP_UTF8STRING is indistinguishable from CPP_STRING
911 : : at this point.
912 : :
913 : : This effectively reverses part of the logic in lex_string and
914 : : fix_string_type. */
915 : :
916 : : static enum cpp_ttype
917 : 11310 : get_cpp_ttype_from_string_type (tree string_type)
918 : : {
919 : 11310 : gcc_assert (string_type);
920 : 11310 : if (TREE_CODE (string_type) == POINTER_TYPE)
921 : 5985 : string_type = TREE_TYPE (string_type);
922 : :
923 : 11310 : if (TREE_CODE (string_type) != ARRAY_TYPE)
924 : : return CPP_OTHER;
925 : :
926 : 11310 : tree element_type = TREE_TYPE (string_type);
927 : 11310 : if (TREE_CODE (element_type) != INTEGER_TYPE)
928 : : return CPP_OTHER;
929 : :
930 : 11310 : int bits_per_character = TYPE_PRECISION (element_type);
931 : 11310 : switch (bits_per_character)
932 : : {
933 : : case 8:
934 : : return CPP_STRING; /* It could have also been CPP_UTF8STRING. */
935 : 2 : case 16:
936 : 2 : return CPP_STRING16;
937 : 4 : case 32:
938 : 4 : return CPP_STRING32;
939 : : }
940 : :
941 : : return CPP_OTHER;
942 : : }
943 : :
944 : : /* The global record of string concatentations, for use in
945 : : extracting locations within string literals. */
946 : :
947 : : GTY(()) string_concat_db *g_string_concat_db;
948 : :
949 : : /* Implementation of LANG_HOOKS_GET_SUBSTRING_LOCATION. */
950 : :
951 : : const char *
952 : 11310 : c_get_substring_location (const substring_loc &substr_loc,
953 : : location_t *out_loc)
954 : : {
955 : 11310 : enum cpp_ttype tok_type
956 : 11310 : = get_cpp_ttype_from_string_type (substr_loc.get_string_type ());
957 : 11310 : if (tok_type == CPP_OTHER)
958 : : return "unrecognized string type";
959 : :
960 : 11310 : return get_location_within_string (parse_in,
961 : : global_dc->get_file_cache (),
962 : : g_string_concat_db,
963 : : substr_loc.get_fmt_string_loc (),
964 : : tok_type,
965 : : substr_loc.get_caret_idx (),
966 : : substr_loc.get_start_idx (),
967 : : substr_loc.get_end_idx (),
968 : 11310 : out_loc);
969 : : }
970 : :
971 : :
972 : : /* Return true iff T is a boolean promoted to int. */
973 : :
974 : : bool
975 : 78894 : bool_promoted_to_int_p (tree t)
976 : : {
977 : 61762 : return (CONVERT_EXPR_P (t)
978 : 17232 : && TREE_TYPE (t) == integer_type_node
979 : 78994 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == BOOLEAN_TYPE);
980 : : }
981 : :
982 : : /* vector_targets_convertible_p is used for vector pointer types. The
983 : : callers perform various checks that the qualifiers are satisfactory,
984 : : while OTOH vector_targets_convertible_p ignores the number of elements
985 : : in the vectors. That's fine with vector pointers as we can consider,
986 : : say, a vector of 8 elements as two consecutive vectors of 4 elements,
987 : : and that does not require and conversion of the pointer values.
988 : : In contrast, vector_types_convertible_p and
989 : : vector_types_compatible_elements_p are used for vector value types. */
990 : : /* True if pointers to distinct types T1 and T2 can be converted to
991 : : each other without an explicit cast. Only returns true for opaque
992 : : vector types. */
993 : : bool
994 : 1999298 : vector_targets_convertible_p (const_tree t1, const_tree t2)
995 : : {
996 : 954145 : if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
997 : 953969 : && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
998 : 1999298 : && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
999 : : return true;
1000 : :
1001 : : return false;
1002 : : }
1003 : :
1004 : : /* vector_types_convertible_p is used for vector value types.
1005 : : It could in principle call vector_targets_convertible_p as a subroutine,
1006 : : but then the check for vector type would be duplicated with its callers,
1007 : : and also the purpose of vector_targets_convertible_p would become
1008 : : muddled.
1009 : : Where vector_types_convertible_p returns true, a conversion might still be
1010 : : needed to make the types match.
1011 : : In contrast, vector_targets_convertible_p is used for vector pointer
1012 : : values, and vector_types_compatible_elements_p is used specifically
1013 : : in the context for binary operators, as a check if use is possible without
1014 : : conversion. */
1015 : : /* True if vector types T1 and T2 can be converted to each other
1016 : : without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
1017 : : can only be converted with -flax-vector-conversions yet that is not
1018 : : in effect, emit a note telling the user about that option if such
1019 : : a note has not previously been emitted. */
1020 : : bool
1021 : 17794237 : vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
1022 : : {
1023 : 17794237 : static bool emitted_lax_note = false;
1024 : 17794237 : bool convertible_lax;
1025 : :
1026 : 35583953 : if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1027 : 17795780 : && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1028 : : return true;
1029 : :
1030 : 35576462 : convertible_lax =
1031 : 17788231 : (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
1032 : 17784739 : && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE
1033 : 10472811 : || known_eq (TYPE_VECTOR_SUBPARTS (t1),
1034 : : TYPE_VECTOR_SUBPARTS (t2)))
1035 : 53301041 : && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
1036 : 17756405 : == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
1037 : :
1038 : 17788231 : if (!convertible_lax || flag_lax_vector_conversions)
1039 : 64699 : return convertible_lax;
1040 : :
1041 : 17723532 : if (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1042 : 17723532 : && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1043 : 16372625 : return true;
1044 : :
1045 : 1350907 : if (emit_lax_note && !emitted_lax_note)
1046 : : {
1047 : 10 : emitted_lax_note = true;
1048 : 10 : inform (input_location, "use %<-flax-vector-conversions%> to permit "
1049 : : "conversions between vectors with differing "
1050 : : "element types or numbers of subparts");
1051 : : }
1052 : :
1053 : : return false;
1054 : : }
1055 : :
1056 : : /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
1057 : : and have vector types, V0 has the same type as V1, and the number of
1058 : : elements of V0, V1, MASK is the same.
1059 : :
1060 : : In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
1061 : : called with two arguments. In this case implementation passes the
1062 : : first argument twice in order to share the same tree code. This fact
1063 : : could enable the mask-values being twice the vector length. This is
1064 : : an implementation accident and this semantics is not guaranteed to
1065 : : the user. */
1066 : : tree
1067 : 474650 : c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
1068 : : bool complain)
1069 : : {
1070 : 474650 : tree ret;
1071 : 474650 : bool wrap = true;
1072 : 474650 : bool maybe_const = false;
1073 : 474650 : bool two_arguments = false;
1074 : :
1075 : 474650 : if (v1 == NULL_TREE)
1076 : : {
1077 : 432581 : two_arguments = true;
1078 : 432581 : v1 = v0;
1079 : : }
1080 : :
1081 : 474650 : if (v0 == error_mark_node || v1 == error_mark_node
1082 : 474650 : || mask == error_mark_node)
1083 : : return error_mark_node;
1084 : :
1085 : 474650 : if (!gnu_vector_type_p (TREE_TYPE (mask))
1086 : 474650 : || !VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
1087 : : {
1088 : 3 : if (complain)
1089 : 0 : error_at (loc, "%<__builtin_shuffle%> last argument must "
1090 : : "be an integer vector");
1091 : 3 : return error_mark_node;
1092 : : }
1093 : :
1094 : 474647 : if (!gnu_vector_type_p (TREE_TYPE (v0))
1095 : 474647 : || !gnu_vector_type_p (TREE_TYPE (v1)))
1096 : : {
1097 : 0 : if (complain)
1098 : 0 : error_at (loc, "%<__builtin_shuffle%> arguments must be vectors");
1099 : 0 : return error_mark_node;
1100 : : }
1101 : :
1102 : 474647 : if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
1103 : : {
1104 : 0 : if (complain)
1105 : 0 : error_at (loc, "%<__builtin_shuffle%> argument vectors must be of "
1106 : : "the same type");
1107 : 0 : return error_mark_node;
1108 : : }
1109 : :
1110 : 474647 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)),
1111 : 474647 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
1112 : 474647 : && maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)),
1113 : 0 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))))
1114 : : {
1115 : 0 : if (complain)
1116 : 0 : error_at (loc, "%<__builtin_shuffle%> number of elements of the "
1117 : : "argument vector(s) and the mask vector should "
1118 : : "be the same");
1119 : 0 : return error_mark_node;
1120 : : }
1121 : :
1122 : 474647 : if (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
1123 : 949294 : != GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
1124 : : {
1125 : 0 : if (complain)
1126 : 0 : error_at (loc, "%<__builtin_shuffle%> argument vector(s) inner type "
1127 : : "must have the same size as inner type of the mask");
1128 : 0 : return error_mark_node;
1129 : : }
1130 : :
1131 : 474647 : if (!c_dialect_cxx ())
1132 : : {
1133 : : /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
1134 : 459922 : v0 = c_fully_fold (v0, false, &maybe_const);
1135 : 459922 : wrap &= maybe_const;
1136 : :
1137 : 459922 : if (two_arguments)
1138 : 418662 : v1 = v0 = save_expr (v0);
1139 : : else
1140 : : {
1141 : 41260 : v1 = c_fully_fold (v1, false, &maybe_const);
1142 : 41260 : wrap &= maybe_const;
1143 : : }
1144 : :
1145 : 459922 : mask = c_fully_fold (mask, false, &maybe_const);
1146 : 459922 : wrap &= maybe_const;
1147 : : }
1148 : 14725 : else if (two_arguments)
1149 : 13916 : v1 = v0 = save_expr (v0);
1150 : :
1151 : 474647 : ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
1152 : :
1153 : 474647 : if (!c_dialect_cxx () && !wrap)
1154 : 459922 : ret = c_wrap_maybe_const (ret, true);
1155 : :
1156 : : return ret;
1157 : : }
1158 : :
1159 : : /* Build a VEC_PERM_EXPR if V0, V1 are not error_mark_nodes
1160 : : and have vector types, V0 has the same element type as V1, and the
1161 : : number of elements the result is that of MASK. */
1162 : : tree
1163 : 1087111 : c_build_shufflevector (location_t loc, tree v0, tree v1,
1164 : : const vec<tree> &mask, bool complain)
1165 : : {
1166 : 1087111 : tree ret;
1167 : 1087111 : bool wrap = true;
1168 : 1087111 : bool maybe_const = false;
1169 : :
1170 : 1087111 : if (v0 == error_mark_node || v1 == error_mark_node)
1171 : : return error_mark_node;
1172 : :
1173 : 1087111 : if (!gnu_vector_type_p (TREE_TYPE (v0))
1174 : 1087111 : || !gnu_vector_type_p (TREE_TYPE (v1)))
1175 : : {
1176 : 4 : if (complain)
1177 : 4 : error_at (loc, "%<__builtin_shufflevector%> arguments must be vectors");
1178 : 4 : return error_mark_node;
1179 : : }
1180 : :
1181 : : /* ??? In principle one could select a constant part of a variable size
1182 : : vector but things get a bit awkward with trying to support this here. */
1183 : 1087107 : unsigned HOST_WIDE_INT v0n, v1n;
1184 : 1087107 : if (!TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)).is_constant (&v0n)
1185 : 1087107 : || !TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)).is_constant (&v1n))
1186 : : {
1187 : : if (complain)
1188 : : error_at (loc, "%<__builtin_shufflevector%> arguments must be constant"
1189 : : " size vectors");
1190 : : return error_mark_node;
1191 : : }
1192 : :
1193 : 1087107 : if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (v0)))
1194 : 1087107 : != TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (v1))))
1195 : : {
1196 : 4 : if (complain)
1197 : 4 : error_at (loc, "%<__builtin_shufflevector%> argument vectors must "
1198 : : "have the same element type");
1199 : 4 : return error_mark_node;
1200 : : }
1201 : :
1202 : 1087103 : if (!pow2p_hwi (mask.length ()))
1203 : : {
1204 : 4 : if (complain)
1205 : 4 : error_at (loc, "%<__builtin_shufflevector%> must specify a result "
1206 : : "with a power of two number of elements");
1207 : 4 : return error_mark_node;
1208 : : }
1209 : :
1210 : 1087099 : if (!c_dialect_cxx ())
1211 : : {
1212 : : /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
1213 : 1052675 : v0 = c_fully_fold (v0, false, &maybe_const);
1214 : 1052675 : wrap &= maybe_const;
1215 : :
1216 : 1052675 : v1 = c_fully_fold (v1, false, &maybe_const);
1217 : 1052675 : wrap &= maybe_const;
1218 : : }
1219 : :
1220 : 2174243 : unsigned HOST_WIDE_INT maskl = MAX (mask.length (), MAX (v0n, v1n));
1221 : 1087099 : unsigned HOST_WIDE_INT pad = (v0n < maskl ? maskl - v0n : 0);
1222 : 1087099 : vec_perm_builder sel (maskl, maskl, 1);
1223 : 1087099 : unsigned i;
1224 : 29505566 : for (i = 0; i < mask.length (); ++i)
1225 : : {
1226 : 13665699 : tree idx = mask[i];
1227 : 13665699 : if (!tree_fits_shwi_p (idx))
1228 : : {
1229 : 4 : if (complain)
1230 : 4 : error_at (loc, "invalid element index %qE to "
1231 : : "%<__builtin_shufflevector%>", idx);
1232 : 1087099 : return error_mark_node;
1233 : : }
1234 : 13665695 : HOST_WIDE_INT iidx = tree_to_shwi (idx);
1235 : 13665695 : if (iidx < -1
1236 : 13665691 : || (iidx != -1
1237 : 13665611 : && (unsigned HOST_WIDE_INT) iidx >= v0n + v1n))
1238 : : {
1239 : 11 : if (complain)
1240 : 11 : error_at (loc, "invalid element index %qE to "
1241 : : "%<__builtin_shufflevector%>", idx);
1242 : 11 : return error_mark_node;
1243 : : }
1244 : : /* ??? Our VEC_PERM_EXPR does not allow for -1 yet. */
1245 : 13665684 : if (iidx == -1)
1246 : 80 : iidx = i;
1247 : : /* ??? Our VEC_PERM_EXPR does not allow different sized inputs,
1248 : : so pad out a smaller v0. */
1249 : 13665604 : else if ((unsigned HOST_WIDE_INT) iidx >= v0n)
1250 : 1159 : iidx += pad;
1251 : 13665684 : sel.quick_push (iidx);
1252 : : }
1253 : : /* ??? VEC_PERM_EXPR does not support a result that is smaller than
1254 : : the inputs, so we have to pad id out. */
1255 : 1088244 : for (; i < maskl; ++i)
1256 : 1160 : sel.quick_push (i);
1257 : :
1258 : 1087084 : vec_perm_indices indices (sel, 2, maskl);
1259 : :
1260 : 1087084 : tree ret_type = build_vector_type (TREE_TYPE (TREE_TYPE (v0)), maskl);
1261 : 2174168 : tree mask_type = build_vector_type (build_nonstandard_integer_type
1262 : 1087084 : (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (ret_type))), 1),
1263 : 1087084 : maskl);
1264 : : /* Pad out arguments to the common vector size. */
1265 : 1087084 : if (v0n < maskl)
1266 : : {
1267 : 125 : constructor_elt elt = { NULL_TREE, build_zero_cst (TREE_TYPE (v0)) };
1268 : 125 : v0 = build_constructor_single (ret_type, NULL_TREE, v0);
1269 : 571 : for (i = 1; i < maskl / v0n; ++i)
1270 : 321 : vec_safe_push (CONSTRUCTOR_ELTS (v0), elt);
1271 : : }
1272 : 1087084 : if (v1n < maskl)
1273 : : {
1274 : 95 : constructor_elt elt = { NULL_TREE, build_zero_cst (TREE_TYPE (v1)) };
1275 : 95 : v1 = build_constructor_single (ret_type, NULL_TREE, v1);
1276 : 351 : for (i = 1; i < maskl / v1n; ++i)
1277 : 161 : vec_safe_push (CONSTRUCTOR_ELTS (v1), elt);
1278 : : }
1279 : 1087084 : ret = build3_loc (loc, VEC_PERM_EXPR, ret_type, v0, v1,
1280 : : vec_perm_indices_to_tree (mask_type, indices));
1281 : : /* Get the lowpart we are interested in. */
1282 : 2174168 : if (mask.length () < maskl)
1283 : : {
1284 : 163 : tree lpartt = build_vector_type (TREE_TYPE (ret_type), mask.length ());
1285 : 163 : ret = build3_loc (loc, BIT_FIELD_REF,
1286 : 163 : lpartt, ret, TYPE_SIZE (lpartt), bitsize_zero_node);
1287 : : /* Wrap the lowpart operation in a TARGET_EXPR so it gets a separate
1288 : : temporary during gimplification. See PR101530 for cases where
1289 : : we'd otherwise end up with non-toplevel BIT_FIELD_REFs. */
1290 : 163 : tree tem = create_tmp_var_raw (lpartt);
1291 : 163 : DECL_CONTEXT (tem) = current_function_decl;
1292 : 163 : ret = build4 (TARGET_EXPR, lpartt, tem, ret, NULL_TREE, NULL_TREE);
1293 : 163 : TREE_SIDE_EFFECTS (ret) = 1;
1294 : : }
1295 : :
1296 : 1087084 : if (!c_dialect_cxx () && !wrap)
1297 : 1052672 : ret = c_wrap_maybe_const (ret, true);
1298 : :
1299 : 1087084 : return ret;
1300 : 2174183 : }
1301 : :
1302 : : /* Build a VEC_CONVERT ifn for __builtin_convertvector builtin. */
1303 : :
1304 : : tree
1305 : 490 : c_build_vec_convert (location_t loc1, tree expr, location_t loc2, tree type,
1306 : : bool complain)
1307 : : {
1308 : 490 : if (error_operand_p (type))
1309 : 0 : return error_mark_node;
1310 : 490 : if (error_operand_p (expr))
1311 : 0 : return error_mark_node;
1312 : :
1313 : 490 : if (!gnu_vector_type_p (TREE_TYPE (expr))
1314 : 490 : || (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (expr))
1315 : 166 : && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (expr))))
1316 : : {
1317 : 4 : if (complain)
1318 : 4 : error_at (loc1, "%<__builtin_convertvector%> first argument must "
1319 : : "be an integer or floating vector");
1320 : 4 : return error_mark_node;
1321 : : }
1322 : :
1323 : 486 : if (!gnu_vector_type_p (type)
1324 : 486 : || (!VECTOR_INTEGER_TYPE_P (type) && !VECTOR_FLOAT_TYPE_P (type)))
1325 : : {
1326 : 4 : if (complain)
1327 : 4 : error_at (loc2, "%<__builtin_convertvector%> second argument must "
1328 : : "be an integer or floating vector type");
1329 : 4 : return error_mark_node;
1330 : : }
1331 : :
1332 : 482 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr)),
1333 : 964 : TYPE_VECTOR_SUBPARTS (type)))
1334 : : {
1335 : 8 : if (complain)
1336 : 8 : error_at (loc1, "%<__builtin_convertvector%> number of elements "
1337 : : "of the first argument vector and the second argument "
1338 : : "vector type should be the same");
1339 : 8 : return error_mark_node;
1340 : : }
1341 : :
1342 : 474 : if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (expr)))
1343 : 474 : == TYPE_MAIN_VARIANT (TREE_TYPE (type)))
1344 : 474 : || (VECTOR_INTEGER_TYPE_P (TREE_TYPE (expr))
1345 : 303 : && VECTOR_INTEGER_TYPE_P (type)
1346 : 155 : && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (expr)))
1347 : 155 : == TYPE_PRECISION (TREE_TYPE (type)))))
1348 : 29 : return build1_loc (loc1, VIEW_CONVERT_EXPR, type, expr);
1349 : :
1350 : 445 : bool wrap = true;
1351 : 445 : bool maybe_const = false;
1352 : 445 : tree ret;
1353 : 445 : if (!c_dialect_cxx ())
1354 : : {
1355 : : /* Avoid C_MAYBE_CONST_EXPRs inside of VEC_CONVERT argument. */
1356 : 310 : expr = c_fully_fold (expr, false, &maybe_const);
1357 : 310 : wrap &= maybe_const;
1358 : : }
1359 : :
1360 : 445 : ret = build_call_expr_internal_loc (loc1, IFN_VEC_CONVERT, type, 1, expr);
1361 : :
1362 : 445 : if (!wrap)
1363 : 310 : ret = c_wrap_maybe_const (ret, true);
1364 : :
1365 : : return ret;
1366 : : }
1367 : :
1368 : : /* Like tree.cc:get_narrower, but retain conversion from C++0x scoped enum
1369 : : to integral type. */
1370 : :
1371 : : tree
1372 : 64712205 : c_common_get_narrower (tree op, int *unsignedp_ptr)
1373 : : {
1374 : 64712205 : op = get_narrower (op, unsignedp_ptr);
1375 : :
1376 : 64712205 : if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
1377 : 64712205 : && ENUM_IS_SCOPED (TREE_TYPE (op)))
1378 : : {
1379 : : /* C++0x scoped enumerations don't implicitly convert to integral
1380 : : type; if we stripped an explicit conversion to a larger type we
1381 : : need to replace it so common_type will still work. */
1382 : 2592828 : tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
1383 : 2592828 : TYPE_UNSIGNED (TREE_TYPE (op)));
1384 : 2592828 : op = fold_convert (type, op);
1385 : : }
1386 : 64712205 : return op;
1387 : : }
1388 : :
1389 : : /* This is a helper function of build_binary_op.
1390 : :
1391 : : For certain operations if both args were extended from the same
1392 : : smaller type, do the arithmetic in that type and then extend.
1393 : :
1394 : : BITWISE indicates a bitwise operation.
1395 : : For them, this optimization is safe only if
1396 : : both args are zero-extended or both are sign-extended.
1397 : : Otherwise, we might change the result.
1398 : : Eg, (short)-1 | (unsigned short)-1 is (int)-1
1399 : : but calculated in (unsigned short) it would be (unsigned short)-1.
1400 : : */
1401 : : tree
1402 : 5155470 : shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
1403 : : {
1404 : 5155470 : int unsigned0, unsigned1;
1405 : 5155470 : tree arg0, arg1;
1406 : 5155470 : int uns;
1407 : 5155470 : tree type;
1408 : :
1409 : : /* Do not shorten vector operations. */
1410 : 5155470 : if (VECTOR_TYPE_P (result_type))
1411 : : return result_type;
1412 : :
1413 : : /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1414 : : excessive narrowing when we call get_narrower below. For
1415 : : example, suppose that OP0 is of unsigned int extended
1416 : : from signed char and that RESULT_TYPE is long long int.
1417 : : If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1418 : : like
1419 : :
1420 : : (long long int) (unsigned int) signed_char
1421 : :
1422 : : which get_narrower would narrow down to
1423 : :
1424 : : (unsigned int) signed char
1425 : :
1426 : : If we do not cast OP0 first, get_narrower would return
1427 : : signed_char, which is inconsistent with the case of the
1428 : : explicit cast. */
1429 : 5137867 : op0 = convert (result_type, op0);
1430 : 5137867 : op1 = convert (result_type, op1);
1431 : :
1432 : 5137867 : arg0 = c_common_get_narrower (op0, &unsigned0);
1433 : 5137867 : arg1 = c_common_get_narrower (op1, &unsigned1);
1434 : :
1435 : : /* UNS is 1 if the operation to be done is an unsigned one. */
1436 : 5137867 : uns = TYPE_UNSIGNED (result_type);
1437 : :
1438 : : /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1439 : : but it *requires* conversion to FINAL_TYPE. */
1440 : :
1441 : 5137867 : if ((TYPE_PRECISION (TREE_TYPE (op0))
1442 : 5137867 : == TYPE_PRECISION (TREE_TYPE (arg0)))
1443 : 5137867 : && TREE_TYPE (op0) != result_type)
1444 : 2420 : unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1445 : 5137867 : if ((TYPE_PRECISION (TREE_TYPE (op1))
1446 : 5137867 : == TYPE_PRECISION (TREE_TYPE (arg1)))
1447 : 5137867 : && TREE_TYPE (op1) != result_type)
1448 : 231009 : unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1449 : :
1450 : : /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
1451 : :
1452 : : /* For bitwise operations, signedness of nominal type
1453 : : does not matter. Consider only how operands were extended. */
1454 : 5137867 : if (bitwise)
1455 : 2501265 : uns = unsigned0;
1456 : :
1457 : : /* Note that in all three cases below we refrain from optimizing
1458 : : an unsigned operation on sign-extended args.
1459 : : That would not be valid. */
1460 : :
1461 : : /* Both args variable: if both extended in same way
1462 : : from same width, do it in that width.
1463 : : Do it unsigned if args were zero-extended. */
1464 : 5137867 : if ((TYPE_PRECISION (TREE_TYPE (arg0))
1465 : 5137867 : < TYPE_PRECISION (result_type))
1466 : 266490 : && (TYPE_PRECISION (TREE_TYPE (arg1))
1467 : 266490 : == TYPE_PRECISION (TREE_TYPE (arg0)))
1468 : 185283 : && unsigned0 == unsigned1
1469 : 5322956 : && (unsigned0 || !uns))
1470 : : {
1471 : 185071 : tree ctype = common_type (TREE_TYPE (arg0), TREE_TYPE (arg1));
1472 : 185071 : if (ctype != error_mark_node)
1473 : 185071 : return c_common_signed_or_unsigned_type (unsigned0, ctype);
1474 : : }
1475 : :
1476 : 4952796 : else if (TREE_CODE (arg0) == INTEGER_CST
1477 : 1744715 : && (unsigned1 || !uns)
1478 : 1547722 : && (TYPE_PRECISION (TREE_TYPE (arg1))
1479 : 1547722 : < TYPE_PRECISION (result_type))
1480 : 282 : && (type
1481 : 282 : = c_common_signed_or_unsigned_type (unsigned1,
1482 : 282 : TREE_TYPE (arg1)))
1483 : 282 : && !POINTER_TYPE_P (type)
1484 : 4953078 : && int_fits_type_p (arg0, type))
1485 : : return type;
1486 : :
1487 : 4952566 : else if (TREE_CODE (arg1) == INTEGER_CST
1488 : 3177618 : && (unsigned0 || !uns)
1489 : 2463860 : && (TYPE_PRECISION (TREE_TYPE (arg0))
1490 : 2463860 : < TYPE_PRECISION (result_type))
1491 : 57673 : && (type
1492 : 57673 : = c_common_signed_or_unsigned_type (unsigned0,
1493 : 57673 : TREE_TYPE (arg0)))
1494 : 57673 : && !POINTER_TYPE_P (type)
1495 : 5010239 : && int_fits_type_p (arg1, type))
1496 : : return type;
1497 : :
1498 : : return result_type;
1499 : : }
1500 : :
1501 : : /* Returns true iff any integer value of type FROM_TYPE can be represented as
1502 : : real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
1503 : :
1504 : : static bool
1505 : 101 : int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
1506 : : {
1507 : 101 : tree type_low_bound = TYPE_MIN_VALUE (from_type);
1508 : 101 : tree type_high_bound = TYPE_MAX_VALUE (from_type);
1509 : 101 : REAL_VALUE_TYPE real_low_bound =
1510 : 101 : real_value_from_int_cst (0, type_low_bound);
1511 : 101 : REAL_VALUE_TYPE real_high_bound =
1512 : 101 : real_value_from_int_cst (0, type_high_bound);
1513 : :
1514 : 101 : return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
1515 : 101 : && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
1516 : : }
1517 : :
1518 : : /* Checks if expression EXPR of complex/real/integer type cannot be converted
1519 : : to the complex/real/integer type TYPE. Function returns non-zero when:
1520 : : * EXPR is a constant which cannot be exactly converted to TYPE.
1521 : : * EXPR is not a constant and size of EXPR's type > than size of TYPE,
1522 : : for EXPR type and TYPE being both integers or both real, or both
1523 : : complex.
1524 : : * EXPR is not a constant of complex type and TYPE is a real or
1525 : : an integer.
1526 : : * EXPR is not a constant of real type and TYPE is an integer.
1527 : : * EXPR is not a constant of integer type which cannot be
1528 : : exactly converted to real type.
1529 : :
1530 : : Function allows conversions between types of different signedness if
1531 : : CHECK_SIGN is false and can return SAFE_CONVERSION (zero) in that
1532 : : case. Function can return UNSAFE_SIGN if CHECK_SIGN is true.
1533 : :
1534 : : RESULT, when non-null is the result of the conversion. When constant
1535 : : it is included in the text of diagnostics.
1536 : :
1537 : : Function allows conversions from complex constants to non-complex types,
1538 : : provided that imaginary part is zero and real part can be safely converted
1539 : : to TYPE. */
1540 : :
1541 : : enum conversion_safety
1542 : 9419 : unsafe_conversion_p (tree type, tree expr, tree result, bool check_sign)
1543 : : {
1544 : 9419 : enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
1545 : 9419 : tree expr_type = TREE_TYPE (expr);
1546 : :
1547 : 9419 : expr = fold_for_warn (expr);
1548 : :
1549 : 9419 : if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
1550 : : {
1551 : : /* If type is complex, we are interested in compatibility with
1552 : : underlying type. */
1553 : 5426 : if (TREE_CODE (type) == COMPLEX_TYPE)
1554 : 114 : type = TREE_TYPE (type);
1555 : :
1556 : : /* Warn for real constant that is not an exact integer converted
1557 : : to integer type. */
1558 : 5426 : if (SCALAR_FLOAT_TYPE_P (expr_type)
1559 : 658 : && (TREE_CODE (type) == INTEGER_TYPE
1560 : 658 : || TREE_CODE (type) == BITINT_TYPE))
1561 : : {
1562 : 283 : if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
1563 : 275 : give_warning = UNSAFE_REAL;
1564 : : }
1565 : : /* Warn for an integer constant that does not fit into integer type. */
1566 : 5143 : else if ((TREE_CODE (expr_type) == INTEGER_TYPE
1567 : 5143 : || TREE_CODE (expr_type) == BITINT_TYPE)
1568 : 4758 : && (TREE_CODE (type) == INTEGER_TYPE
1569 : 4758 : || TREE_CODE (type) == BITINT_TYPE)
1570 : 4034 : && !int_fits_type_p (expr, type))
1571 : : {
1572 : 995 : if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
1573 : 961 : && tree_int_cst_sgn (expr) < 0)
1574 : : {
1575 : 264 : if (check_sign)
1576 : 1834 : give_warning = UNSAFE_SIGN;
1577 : : }
1578 : 432 : else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
1579 : : {
1580 : 335 : if (check_sign)
1581 : 1834 : give_warning = UNSAFE_SIGN;
1582 : : }
1583 : : else
1584 : : give_warning = UNSAFE_OTHER;
1585 : : }
1586 : 4447 : else if (SCALAR_FLOAT_TYPE_P (type))
1587 : : {
1588 : : /* Warn for an integer constant that does not fit into real type. */
1589 : 1009 : if (TREE_CODE (expr_type) == INTEGER_TYPE
1590 : : || TREE_CODE (expr_type) == BITINT_TYPE)
1591 : : {
1592 : 634 : REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
1593 : 634 : if (!exact_real_truncate (TYPE_MODE (type), &a))
1594 : 53 : give_warning = UNSAFE_REAL;
1595 : : }
1596 : : /* Warn for a real constant that does not fit into a smaller
1597 : : real type. */
1598 : 375 : else if (SCALAR_FLOAT_TYPE_P (expr_type)
1599 : 375 : && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1600 : : {
1601 : 227 : REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
1602 : 227 : if (!exact_real_truncate (TYPE_MODE (type), &a))
1603 : 65 : give_warning = UNSAFE_REAL;
1604 : : }
1605 : : }
1606 : : }
1607 : :
1608 : 3993 : else if (TREE_CODE (expr) == COMPLEX_CST)
1609 : : {
1610 : 69 : tree imag_part = TREE_IMAGPART (expr);
1611 : : /* Conversion from complex constant with zero imaginary part,
1612 : : perform check for conversion of real part. */
1613 : 69 : if ((TREE_CODE (imag_part) == REAL_CST
1614 : 29 : && real_zerop (imag_part))
1615 : 80 : || (TREE_CODE (imag_part) == INTEGER_CST
1616 : 40 : && integer_zerop (imag_part)))
1617 : : /* Note: in this branch we use recursive call to unsafe_conversion_p
1618 : : with different type of EXPR, but it is still safe, because when EXPR
1619 : : is a constant, it's type is not used in text of generated warnings
1620 : : (otherwise they could sound misleading). */
1621 : 30 : return unsafe_conversion_p (type, TREE_REALPART (expr), result,
1622 : 30 : check_sign);
1623 : : /* Conversion from complex constant with non-zero imaginary part. */
1624 : : else
1625 : : {
1626 : : /* Conversion to complex type.
1627 : : Perform checks for both real and imaginary parts. */
1628 : 39 : if (TREE_CODE (type) == COMPLEX_TYPE)
1629 : : {
1630 : 33 : enum conversion_safety re_safety =
1631 : 33 : unsafe_conversion_p (type, TREE_REALPART (expr),
1632 : : result, check_sign);
1633 : 33 : enum conversion_safety im_safety =
1634 : 33 : unsafe_conversion_p (type, imag_part, result, check_sign);
1635 : :
1636 : : /* Merge the results into appropriate single warning. */
1637 : :
1638 : : /* Note: this case includes SAFE_CONVERSION, i.e. success. */
1639 : 33 : if (re_safety == im_safety)
1640 : : give_warning = re_safety;
1641 : 9 : else if (!re_safety && im_safety)
1642 : : give_warning = im_safety;
1643 : 4 : else if (re_safety && !im_safety)
1644 : : give_warning = re_safety;
1645 : : else
1646 : 670 : give_warning = UNSAFE_OTHER;
1647 : : }
1648 : : /* Warn about conversion from complex to real or integer type. */
1649 : : else
1650 : : give_warning = UNSAFE_IMAGINARY;
1651 : : }
1652 : : }
1653 : :
1654 : : /* Checks for remaining case: EXPR is not constant. */
1655 : : else
1656 : : {
1657 : : /* Warn for real types converted to integer types. */
1658 : 3924 : if (SCALAR_FLOAT_TYPE_P (expr_type)
1659 : 213 : && (TREE_CODE (type) == INTEGER_TYPE
1660 : 213 : || TREE_CODE (type) == BITINT_TYPE))
1661 : : give_warning = UNSAFE_REAL;
1662 : :
1663 : 3895 : else if ((TREE_CODE (expr_type) == INTEGER_TYPE
1664 : 3895 : || TREE_CODE (expr_type) == BITINT_TYPE)
1665 : 3500 : && (TREE_CODE (type) == INTEGER_TYPE
1666 : 3500 : || TREE_CODE (type) == BITINT_TYPE))
1667 : : {
1668 : : /* Don't warn about unsigned char y = 0xff, x = (int) y; */
1669 : 3279 : expr = get_unwidened (expr, 0);
1670 : 3279 : expr_type = TREE_TYPE (expr);
1671 : :
1672 : : /* Don't warn for short y; short x = ((int)y & 0xff); */
1673 : 3279 : if (TREE_CODE (expr) == BIT_AND_EXPR
1674 : 3265 : || TREE_CODE (expr) == BIT_IOR_EXPR
1675 : 3224 : || TREE_CODE (expr) == BIT_XOR_EXPR)
1676 : : {
1677 : : /* If both args were extended from a shortest type,
1678 : : use that type if that is safe. */
1679 : 59 : expr_type = shorten_binary_op (expr_type,
1680 : 59 : TREE_OPERAND (expr, 0),
1681 : 59 : TREE_OPERAND (expr, 1),
1682 : : /* bitwise */1);
1683 : :
1684 : 59 : if (TREE_CODE (expr) == BIT_AND_EXPR)
1685 : : {
1686 : 14 : tree op0 = TREE_OPERAND (expr, 0);
1687 : 14 : tree op1 = TREE_OPERAND (expr, 1);
1688 : 14 : bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1689 : 14 : bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1690 : :
1691 : : /* If one of the operands is a non-negative constant
1692 : : that fits in the target type, then the type of the
1693 : : other operand does not matter. */
1694 : 14 : if ((TREE_CODE (op0) == INTEGER_CST
1695 : 0 : && int_fits_type_p (op0, c_common_signed_type (type))
1696 : 0 : && int_fits_type_p (op0, c_common_unsigned_type (type)))
1697 : 14 : || (TREE_CODE (op1) == INTEGER_CST
1698 : 1 : && int_fits_type_p (op1, c_common_signed_type (type))
1699 : 1 : && int_fits_type_p (op1,
1700 : 1 : c_common_unsigned_type (type))))
1701 : 1 : return SAFE_CONVERSION;
1702 : : /* If constant is unsigned and fits in the target
1703 : : type, then the result will also fit. */
1704 : 13 : else if ((TREE_CODE (op0) == INTEGER_CST
1705 : 0 : && unsigned0
1706 : 0 : && int_fits_type_p (op0, type))
1707 : 13 : || (TREE_CODE (op1) == INTEGER_CST
1708 : 0 : && unsigned1
1709 : 0 : && int_fits_type_p (op1, type)))
1710 : : return SAFE_CONVERSION;
1711 : : }
1712 : : }
1713 : : /* Warn for integer types converted to smaller integer types. */
1714 : 3278 : if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1715 : : give_warning = UNSAFE_OTHER;
1716 : :
1717 : : /* When they are the same width but different signedness,
1718 : : then the value may change. */
1719 : 2742 : else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
1720 : 2213 : && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
1721 : : /* Even when converted to a bigger type, if the type is
1722 : : unsigned but expr is signed, then negative values
1723 : : will be changed. */
1724 : 1481 : || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
1725 : 4054 : && check_sign)
1726 : : give_warning = UNSAFE_SIGN;
1727 : : }
1728 : :
1729 : : /* Warn for integer types converted to real types if and only if
1730 : : all the range of values of the integer type cannot be
1731 : : represented by the real type. */
1732 : 616 : else if ((TREE_CODE (expr_type) == INTEGER_TYPE
1733 : : || TREE_CODE (expr_type) == BITINT_TYPE)
1734 : 221 : && SCALAR_FLOAT_TYPE_P (type))
1735 : : {
1736 : : /* Don't warn about char y = 0xff; float x = (int) y; */
1737 : 101 : expr = get_unwidened (expr, 0);
1738 : 101 : expr_type = TREE_TYPE (expr);
1739 : :
1740 : 101 : if (!int_safely_convertible_to_real_p (expr_type, type))
1741 : 670 : give_warning = UNSAFE_OTHER;
1742 : : }
1743 : :
1744 : : /* Warn for real types converted to smaller real types. */
1745 : 515 : else if (SCALAR_FLOAT_TYPE_P (expr_type)
1746 : 184 : && SCALAR_FLOAT_TYPE_P (type)
1747 : 699 : && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1748 : : give_warning = UNSAFE_REAL;
1749 : :
1750 : : /* Check conversion between two complex types. */
1751 : 463 : else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1752 : 32 : && TREE_CODE (type) == COMPLEX_TYPE)
1753 : : {
1754 : : /* Extract underlying types (i.e., type of real and imaginary
1755 : : parts) of expr_type and type. */
1756 : 10 : tree from_type = TREE_TYPE (expr_type);
1757 : 10 : tree to_type = TREE_TYPE (type);
1758 : :
1759 : : /* Warn for real types converted to integer types. */
1760 : 10 : if (SCALAR_FLOAT_TYPE_P (from_type)
1761 : 8 : && TREE_CODE (to_type) == INTEGER_TYPE)
1762 : : give_warning = UNSAFE_REAL;
1763 : :
1764 : : /* Warn for real types converted to smaller real types. */
1765 : 6 : else if (SCALAR_FLOAT_TYPE_P (from_type)
1766 : 4 : && SCALAR_FLOAT_TYPE_P (to_type)
1767 : 10 : && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1768 : : give_warning = UNSAFE_REAL;
1769 : :
1770 : : /* Check conversion for complex integer types. Here implementation
1771 : : is simpler than for real-domain integers because it does not
1772 : : involve sophisticated cases, such as bitmasks, casts, etc. */
1773 : 4 : else if (TREE_CODE (from_type) == INTEGER_TYPE
1774 : 2 : && TREE_CODE (to_type) == INTEGER_TYPE)
1775 : : {
1776 : : /* Warn for integer types converted to smaller integer types. */
1777 : 2 : if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1778 : : give_warning = UNSAFE_OTHER;
1779 : :
1780 : : /* Check for different signedness, see case for real-domain
1781 : : integers (above) for a more detailed comment. */
1782 : 0 : else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
1783 : 0 : && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
1784 : 0 : || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
1785 : 0 : && check_sign)
1786 : : give_warning = UNSAFE_SIGN;
1787 : : }
1788 : 2 : else if (TREE_CODE (from_type) == INTEGER_TYPE
1789 : 0 : && SCALAR_FLOAT_TYPE_P (to_type)
1790 : 2 : && !int_safely_convertible_to_real_p (from_type, to_type))
1791 : : give_warning = UNSAFE_OTHER;
1792 : : }
1793 : :
1794 : : /* Warn for complex types converted to real or integer types. */
1795 : 453 : else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1796 : 22 : && TREE_CODE (type) != COMPLEX_TYPE)
1797 : 28 : give_warning = UNSAFE_IMAGINARY;
1798 : : }
1799 : :
1800 : : return give_warning;
1801 : : }
1802 : :
1803 : :
1804 : : /* Convert EXPR to TYPE, warning about conversion problems with constants.
1805 : : Invoke this function on every expression that is converted implicitly,
1806 : : i.e. because of language rules and not because of an explicit cast.
1807 : : INIT_CONST is true if the conversion is for arithmetic types for a static
1808 : : initializer and folding must apply accordingly (discarding floating-point
1809 : : exceptions and assuming the default rounding mode is in effect). */
1810 : :
1811 : : tree
1812 : 12377531 : convert_and_check (location_t loc, tree type, tree expr, bool init_const)
1813 : : {
1814 : 12377531 : tree result;
1815 : 12377531 : tree expr_for_warning;
1816 : :
1817 : : /* Convert from a value with possible excess precision rather than
1818 : : via the semantic type, but do not warn about values not fitting
1819 : : exactly in the semantic type. */
1820 : 12377531 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1821 : : {
1822 : 5963 : tree orig_type = TREE_TYPE (expr);
1823 : 5963 : expr = TREE_OPERAND (expr, 0);
1824 : 5963 : expr_for_warning = (init_const
1825 : 5963 : ? convert_init (orig_type, expr)
1826 : 5602 : : convert (orig_type, expr));
1827 : 5963 : if (orig_type == type)
1828 : : return expr_for_warning;
1829 : : }
1830 : : else
1831 : : expr_for_warning = expr;
1832 : :
1833 : 12372061 : if (TREE_TYPE (expr) == type)
1834 : : return expr;
1835 : :
1836 : 10943250 : result = init_const ? convert_init (type, expr) : convert (type, expr);
1837 : :
1838 : 10943250 : if (c_inhibit_evaluation_warnings == 0
1839 : 10616834 : && !TREE_OVERFLOW_P (expr)
1840 : 10616808 : && result != error_mark_node
1841 : 21559904 : && !c_hardbool_type_attr (type))
1842 : 10609311 : warnings_for_convert_and_check (loc, type, expr_for_warning, result);
1843 : :
1844 : : return result;
1845 : : }
1846 : :
1847 : : /* A node in a list that describes references to variables (EXPR), which are
1848 : : either read accesses if WRITER is zero, or write accesses, in which case
1849 : : WRITER is the parent of EXPR. */
1850 : : struct tlist
1851 : : {
1852 : : struct tlist *next;
1853 : : tree expr, writer;
1854 : : };
1855 : :
1856 : : /* Used to implement a cache the results of a call to verify_tree. We only
1857 : : use this for SAVE_EXPRs. */
1858 : : struct tlist_cache
1859 : : {
1860 : : struct tlist_cache *next;
1861 : : struct tlist *cache_before_sp;
1862 : : struct tlist *cache_after_sp;
1863 : : tree expr;
1864 : : };
1865 : :
1866 : : /* Obstack to use when allocating tlist structures, and corresponding
1867 : : firstobj. */
1868 : : static struct obstack tlist_obstack;
1869 : : static char *tlist_firstobj = 0;
1870 : :
1871 : : /* Keep track of the identifiers we've warned about, so we can avoid duplicate
1872 : : warnings. */
1873 : : static struct tlist *warned_ids;
1874 : : /* SAVE_EXPRs need special treatment. We process them only once and then
1875 : : cache the results. */
1876 : : static struct tlist_cache *save_expr_cache;
1877 : :
1878 : : static void add_tlist (struct tlist **, struct tlist *, tree, int);
1879 : : static void merge_tlist (struct tlist **, struct tlist *, int);
1880 : : static void verify_tree (tree, struct tlist **, struct tlist **, tree);
1881 : : static bool warning_candidate_p (tree);
1882 : : static bool candidate_equal_p (const_tree, const_tree);
1883 : : static void warn_for_collisions (struct tlist *);
1884 : : static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
1885 : : static struct tlist *new_tlist (struct tlist *, tree, tree);
1886 : :
1887 : : /* Create a new struct tlist and fill in its fields. */
1888 : : static struct tlist *
1889 : 34304854 : new_tlist (struct tlist *next, tree t, tree writer)
1890 : : {
1891 : 34304854 : struct tlist *l;
1892 : 34304854 : l = XOBNEW (&tlist_obstack, struct tlist);
1893 : 34304854 : l->next = next;
1894 : 34304854 : l->expr = t;
1895 : 34304854 : l->writer = writer;
1896 : 34304854 : return l;
1897 : : }
1898 : :
1899 : : /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1900 : : is nonnull, we ignore any node we find which has a writer equal to it. */
1901 : :
1902 : : static void
1903 : 60550238 : add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
1904 : : {
1905 : 131732214 : while (add)
1906 : : {
1907 : 71181976 : struct tlist *next = add->next;
1908 : 13106293 : if (!copy)
1909 : 1867321 : add->next = *to;
1910 : 13106293 : if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
1911 : 69171202 : *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1912 : : add = next;
1913 : : }
1914 : 11807866 : }
1915 : :
1916 : : /* Merge the nodes of ADD into TO. This merging process is done so that for
1917 : : each variable that already exists in TO, no new node is added; however if
1918 : : there is a write access recorded in ADD, and an occurrence on TO is only
1919 : : a read access, then the occurrence in TO will be modified to record the
1920 : : write. */
1921 : :
1922 : : static void
1923 : 42289340 : merge_tlist (struct tlist **to, struct tlist *add, int copy)
1924 : : {
1925 : 42289340 : struct tlist **end = to;
1926 : :
1927 : 78970388 : while (*end)
1928 : 36681048 : end = &(*end)->next;
1929 : :
1930 : 58107861 : while (add)
1931 : : {
1932 : 15818521 : int found = 0;
1933 : 15818521 : struct tlist *tmp2;
1934 : 15818521 : struct tlist *next = add->next;
1935 : :
1936 : 43157375 : for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1937 : 27338854 : if (candidate_equal_p (tmp2->expr, add->expr))
1938 : : {
1939 : 2709010 : found = 1;
1940 : 2709010 : if (!tmp2->writer)
1941 : 2702584 : tmp2->writer = add->writer;
1942 : : }
1943 : 15818521 : if (!found)
1944 : : {
1945 : 13123188 : *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
1946 : 13123188 : end = &(*end)->next;
1947 : 13123188 : *end = 0;
1948 : : }
1949 : : add = next;
1950 : : }
1951 : 42289340 : }
1952 : :
1953 : : /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1954 : : references in list LIST conflict with it, excluding reads if ONLY writers
1955 : : is nonzero. */
1956 : :
1957 : : static void
1958 : 5234849 : warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
1959 : : int only_writes)
1960 : : {
1961 : 5234849 : struct tlist *tmp;
1962 : :
1963 : : /* Avoid duplicate warnings. */
1964 : 5235012 : for (tmp = warned_ids; tmp; tmp = tmp->next)
1965 : 611 : if (candidate_equal_p (tmp->expr, written))
1966 : : return;
1967 : :
1968 : 18802662 : while (list)
1969 : : {
1970 : 13568261 : if (candidate_equal_p (list->expr, written)
1971 : 1536120 : && !candidate_equal_p (list->writer, writer)
1972 : 13708489 : && (!only_writes || list->writer))
1973 : : {
1974 : 562 : warned_ids = new_tlist (warned_ids, written, NULL_TREE);
1975 : 562 : warning_at (EXPR_LOC_OR_LOC (writer, input_location),
1976 : 562 : OPT_Wsequence_point, "operation on %qE may be undefined",
1977 : : list->expr);
1978 : : }
1979 : 13568261 : list = list->next;
1980 : : }
1981 : : }
1982 : :
1983 : : /* Given a list LIST of references to variables, find whether any of these
1984 : : can cause conflicts due to missing sequence points. */
1985 : :
1986 : : static void
1987 : 19930697 : warn_for_collisions (struct tlist *list)
1988 : : {
1989 : 19930697 : struct tlist *tmp;
1990 : :
1991 : 54924274 : for (tmp = list; tmp; tmp = tmp->next)
1992 : : {
1993 : 34993577 : if (tmp->writer)
1994 : 1331787 : warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1995 : : }
1996 : 19930697 : }
1997 : :
1998 : : /* Return nonzero if X is a tree that can be verified by the sequence point
1999 : : warnings. */
2000 : :
2001 : : static bool
2002 : 76343858 : warning_candidate_p (tree x)
2003 : : {
2004 : 76343858 : if (DECL_P (x) && DECL_ARTIFICIAL (x))
2005 : : return false;
2006 : :
2007 : 68704958 : if (TREE_CODE (x) == BLOCK)
2008 : : return false;
2009 : :
2010 : : /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.cc
2011 : : (lvalue_p) crash on TRY/CATCH. */
2012 : 68702827 : if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2013 : : return false;
2014 : :
2015 : 67815226 : if (!lvalue_p (x))
2016 : : return false;
2017 : :
2018 : : /* No point to track non-const calls, they will never satisfy
2019 : : operand_equal_p. */
2020 : 24399146 : if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2021 : : return false;
2022 : :
2023 : 24356708 : if (TREE_CODE (x) == STRING_CST)
2024 : : return false;
2025 : :
2026 : : return true;
2027 : : }
2028 : :
2029 : : /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2030 : : static bool
2031 : 46232310 : candidate_equal_p (const_tree x, const_tree y)
2032 : : {
2033 : 46232310 : return (x == y) || (x && y && operand_equal_p (x, y, 0));
2034 : : }
2035 : :
2036 : : /* Walk the tree X, and record accesses to variables. If X is written by the
2037 : : parent tree, WRITER is the parent.
2038 : : We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2039 : : expression or its only operand forces a sequence point, then everything up
2040 : : to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2041 : : in PNO_SP.
2042 : : Once we return, we will have emitted warnings if any subexpression before
2043 : : such a sequence point could be undefined. On a higher level, however, the
2044 : : sequence point may not be relevant, and we'll merge the two lists.
2045 : :
2046 : : Example: (b++, a) + b;
2047 : : The call that processes the COMPOUND_EXPR will store the increment of B
2048 : : in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2049 : : processes the PLUS_EXPR will need to merge the two lists so that
2050 : : eventually, all accesses end up on the same list (and we'll warn about the
2051 : : unordered subexpressions b++ and b.
2052 : :
2053 : : A note on merging. If we modify the former example so that our expression
2054 : : becomes
2055 : : (b++, b) + a
2056 : : care must be taken not simply to add all three expressions into the final
2057 : : PNO_SP list. The function merge_tlist takes care of that by merging the
2058 : : before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2059 : : way, so that no more than one access to B is recorded. */
2060 : :
2061 : : static void
2062 : 59981899 : verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2063 : : tree writer)
2064 : : {
2065 : 73634412 : struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2066 : 73634412 : enum tree_code code;
2067 : 73634412 : enum tree_code_class cl;
2068 : :
2069 : 73634412 : restart:
2070 : : /* X may be NULL if it is the operand of an empty statement expression
2071 : : ({ }). */
2072 : 73634412 : if (x == NULL)
2073 : : return;
2074 : :
2075 : 72440796 : code = TREE_CODE (x);
2076 : 72440796 : cl = TREE_CODE_CLASS (code);
2077 : :
2078 : 72440796 : if (warning_candidate_p (x))
2079 : 23124848 : *pno_sp = new_tlist (*pno_sp, x, writer);
2080 : :
2081 : 72440796 : switch (code)
2082 : : {
2083 : : case CONSTRUCTOR:
2084 : : case SIZEOF_EXPR:
2085 : : case PAREN_SIZEOF_EXPR:
2086 : : return;
2087 : :
2088 : 204126 : case COMPOUND_EXPR:
2089 : 204126 : case TRUTH_ANDIF_EXPR:
2090 : 204126 : case TRUTH_ORIF_EXPR:
2091 : 204126 : sequenced_binary:
2092 : 204126 : tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
2093 : 204126 : verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2094 : 204126 : warn_for_collisions (tmp_nosp);
2095 : 204126 : merge_tlist (pbefore_sp, tmp_before, 0);
2096 : 204126 : merge_tlist (pbefore_sp, tmp_nosp, 0);
2097 : 204126 : verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_list2, NULL_TREE);
2098 : 204126 : warn_for_collisions (tmp_list2);
2099 : 204126 : merge_tlist (pbefore_sp, tmp_list3, 0);
2100 : 204126 : merge_tlist (pno_sp, tmp_list2, 0);
2101 : 204126 : return;
2102 : :
2103 : 67030 : case COND_EXPR:
2104 : 67030 : tmp_before = tmp_list2 = 0;
2105 : 67030 : verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2106 : 67030 : warn_for_collisions (tmp_list2);
2107 : 67030 : merge_tlist (pbefore_sp, tmp_before, 0);
2108 : 67030 : merge_tlist (pbefore_sp, tmp_list2, 0);
2109 : :
2110 : 67030 : tmp_list3 = tmp_nosp = 0;
2111 : 67030 : verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
2112 : 67030 : warn_for_collisions (tmp_nosp);
2113 : 67030 : merge_tlist (pbefore_sp, tmp_list3, 0);
2114 : :
2115 : 67030 : tmp_list3 = tmp_list2 = 0;
2116 : 67030 : verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
2117 : 67030 : warn_for_collisions (tmp_list2);
2118 : 67030 : merge_tlist (pbefore_sp, tmp_list3, 0);
2119 : : /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
2120 : : two first, to avoid warning for (a ? b++ : b++). */
2121 : 67030 : merge_tlist (&tmp_nosp, tmp_list2, 0);
2122 : 67030 : add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2123 : : return;
2124 : :
2125 : 267891 : case PREDECREMENT_EXPR:
2126 : 267891 : case PREINCREMENT_EXPR:
2127 : 267891 : case POSTDECREMENT_EXPR:
2128 : 267891 : case POSTINCREMENT_EXPR:
2129 : 267891 : verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
2130 : 267891 : return;
2131 : :
2132 : 3903062 : case MODIFY_EXPR:
2133 : 3903062 : tmp_before = tmp_nosp = tmp_list3 = 0;
2134 : 3903062 : verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
2135 : 3903062 : verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
2136 : : /* Expressions inside the LHS are not ordered wrt. the sequence points
2137 : : in the RHS. Example:
2138 : : *a = (a++, 2)
2139 : : Despite the fact that the modification of "a" is in the before_sp
2140 : : list (tmp_before), it conflicts with the use of "a" in the LHS.
2141 : : We can handle this by adding the contents of tmp_list3
2142 : : to those of tmp_before, and redoing the collision warnings for that
2143 : : list. */
2144 : 3903062 : add_tlist (&tmp_before, tmp_list3, x, 1);
2145 : 3903062 : warn_for_collisions (tmp_before);
2146 : : /* Exclude the LHS itself here; we first have to merge it into the
2147 : : tmp_nosp list. This is done to avoid warning for "a = a"; if we
2148 : : didn't exclude the LHS, we'd get it twice, once as a read and once
2149 : : as a write. */
2150 : 3903062 : add_tlist (pno_sp, tmp_list3, x, 0);
2151 : 3903062 : warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
2152 : :
2153 : 3903062 : merge_tlist (pbefore_sp, tmp_before, 0);
2154 : 3903062 : if (warning_candidate_p (TREE_OPERAND (x, 0)))
2155 : 939222 : merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
2156 : 3903062 : add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
2157 : 3903062 : return;
2158 : :
2159 : 6263601 : case CALL_EXPR:
2160 : : /* We need to warn about conflicts among arguments and conflicts between
2161 : : args and the function address. Side effects of the function address,
2162 : : however, are not ordered by the sequence point of the call. */
2163 : 6263601 : {
2164 : 6263601 : call_expr_arg_iterator iter;
2165 : 6263601 : tree arg;
2166 : 6263601 : tmp_before = tmp_nosp = 0;
2167 : 6263601 : verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
2168 : 27798999 : FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
2169 : : {
2170 : 15271797 : tmp_list2 = tmp_list3 = 0;
2171 : 15271797 : verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
2172 : 15271797 : merge_tlist (&tmp_list3, tmp_list2, 0);
2173 : 30543594 : add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
2174 : : }
2175 : 6263601 : add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
2176 : 6263601 : warn_for_collisions (tmp_before);
2177 : 6263601 : add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
2178 : 6263601 : return;
2179 : : }
2180 : :
2181 : : case TREE_LIST:
2182 : : /* Scan all the list, e.g. indices of multi dimensional array. */
2183 : 6 : while (x)
2184 : : {
2185 : 3 : tmp_before = tmp_nosp = 0;
2186 : 3 : verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
2187 : 3 : merge_tlist (&tmp_nosp, tmp_before, 0);
2188 : 3 : add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2189 : 3 : x = TREE_CHAIN (x);
2190 : : }
2191 : : return;
2192 : :
2193 : 98680 : case SAVE_EXPR:
2194 : 98680 : {
2195 : 98680 : struct tlist_cache *t;
2196 : 102404 : for (t = save_expr_cache; t; t = t->next)
2197 : 53822 : if (candidate_equal_p (t->expr, x))
2198 : : break;
2199 : :
2200 : 98680 : if (!t)
2201 : : {
2202 : 48582 : t = XOBNEW (&tlist_obstack, struct tlist_cache);
2203 : 48582 : t->next = save_expr_cache;
2204 : 48582 : t->expr = x;
2205 : 48582 : save_expr_cache = t;
2206 : :
2207 : 48582 : tmp_before = tmp_nosp = 0;
2208 : 48582 : verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2209 : 48582 : warn_for_collisions (tmp_nosp);
2210 : :
2211 : 48582 : tmp_list3 = 0;
2212 : 48582 : merge_tlist (&tmp_list3, tmp_nosp, 0);
2213 : 48582 : t->cache_before_sp = tmp_before;
2214 : 48582 : t->cache_after_sp = tmp_list3;
2215 : : }
2216 : 98680 : merge_tlist (pbefore_sp, t->cache_before_sp, 1);
2217 : 98680 : add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
2218 : 98680 : return;
2219 : : }
2220 : :
2221 : 7118306 : case ADDR_EXPR:
2222 : 7118306 : x = TREE_OPERAND (x, 0);
2223 : 7118306 : if (DECL_P (x))
2224 : : return;
2225 : 502232 : writer = 0;
2226 : 502232 : goto restart;
2227 : :
2228 : 12011283 : case VIEW_CONVERT_EXPR:
2229 : 12011283 : if (location_wrapper_p (x))
2230 : : {
2231 : 2984105 : x = TREE_OPERAND (x, 0);
2232 : 2984105 : goto restart;
2233 : : }
2234 : 9027178 : goto do_default;
2235 : :
2236 : 625227 : case LSHIFT_EXPR:
2237 : 625227 : case RSHIFT_EXPR:
2238 : 625227 : case ARRAY_REF:
2239 : 625227 : if (cxx_dialect >= cxx17)
2240 : 48787 : goto sequenced_binary;
2241 : 576440 : goto do_default;
2242 : :
2243 : 1292971 : case COMPONENT_REF:
2244 : : /* Treat as unary, the other operands aren't evaluated. */
2245 : 1292971 : x = TREE_OPERAND (x, 0);
2246 : 1292971 : writer = 0;
2247 : 1292971 : goto restart;
2248 : :
2249 : 49804184 : default:
2250 : 49804184 : do_default:
2251 : : /* For other expressions, simply recurse on their operands.
2252 : : Manual tail recursion for unary expressions.
2253 : : Other non-expressions need not be processed. */
2254 : 49804184 : if (cl == tcc_unary)
2255 : : {
2256 : 8605314 : x = TREE_OPERAND (x, 0);
2257 : 8605314 : writer = 0;
2258 : 8605314 : goto restart;
2259 : : }
2260 : 41198870 : else if (IS_EXPR_CODE_CLASS (cl))
2261 : : {
2262 : 14998611 : int lp;
2263 : 14998611 : int max = TREE_OPERAND_LENGTH (x);
2264 : 50873562 : for (lp = 0; lp < max; lp++)
2265 : : {
2266 : 20876340 : tmp_before = tmp_nosp = 0;
2267 : 20876340 : verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
2268 : 20876340 : merge_tlist (&tmp_nosp, tmp_before, 0);
2269 : 41752680 : add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2270 : : }
2271 : : }
2272 : : return;
2273 : : }
2274 : : }
2275 : :
2276 : : static constexpr size_t verify_sequence_points_limit = 1024;
2277 : :
2278 : : /* Called from verify_sequence_points via walk_tree. */
2279 : :
2280 : : static tree
2281 : 89710365 : verify_tree_lim_r (tree *tp, int *walk_subtrees, void *data)
2282 : : {
2283 : 89710365 : if (++*((size_t *) data) > verify_sequence_points_limit)
2284 : 57 : return integer_zero_node;
2285 : :
2286 : 89710308 : if (TYPE_P (*tp))
2287 : 5866 : *walk_subtrees = 0;
2288 : :
2289 : : return NULL_TREE;
2290 : : }
2291 : :
2292 : : /* Try to warn for undefined behavior in EXPR due to missing sequence
2293 : : points. */
2294 : :
2295 : : void
2296 : 9106167 : verify_sequence_points (tree expr)
2297 : : {
2298 : 9106167 : tlist *before_sp = nullptr, *after_sp = nullptr;
2299 : :
2300 : : /* verify_tree is highly recursive, and merge_tlist is O(n^2),
2301 : : so we return early if the expression is too big. */
2302 : 9106167 : size_t n = 0;
2303 : 9106167 : if (walk_tree (&expr, verify_tree_lim_r, &n, nullptr))
2304 : 57 : return;
2305 : :
2306 : 9106110 : warned_ids = nullptr;
2307 : 9106110 : save_expr_cache = nullptr;
2308 : 9106110 : if (!tlist_firstobj)
2309 : : {
2310 : 9891 : gcc_obstack_init (&tlist_obstack);
2311 : 9891 : tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
2312 : : }
2313 : :
2314 : 9106110 : verify_tree (expr, &before_sp, &after_sp, NULL_TREE);
2315 : 9106110 : warn_for_collisions (after_sp);
2316 : 9106110 : obstack_free (&tlist_obstack, tlist_firstobj);
2317 : : }
2318 : :
2319 : : /* Validate the expression after `case' and apply default promotions. */
2320 : :
2321 : : static tree
2322 : 2725210 : check_case_value (location_t loc, tree value)
2323 : : {
2324 : 2725210 : if (value == NULL_TREE)
2325 : : return value;
2326 : :
2327 : 4539253 : if (INTEGRAL_TYPE_P (TREE_TYPE (value))
2328 : 4539150 : && TREE_CODE (value) == INTEGER_CST)
2329 : : /* Promote char or short to int. */
2330 : 2725059 : value = perform_integral_promotions (value);
2331 : 151 : else if (value != error_mark_node)
2332 : : {
2333 : 53 : error_at (loc, "case label does not reduce to an integer constant");
2334 : 53 : value = error_mark_node;
2335 : : }
2336 : :
2337 : 2725210 : constant_expression_warning (value);
2338 : :
2339 : 2725210 : return value;
2340 : : }
2341 : :
2342 : : /* Return an integer type with BITS bits of precision,
2343 : : that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2344 : :
2345 : : tree
2346 : 28182253 : c_common_type_for_size (unsigned int bits, int unsignedp)
2347 : : {
2348 : 28182253 : int i;
2349 : :
2350 : 28182253 : if (bits == TYPE_PRECISION (integer_type_node))
2351 : 18590641 : return unsignedp ? unsigned_type_node : integer_type_node;
2352 : :
2353 : 9591612 : if (bits == TYPE_PRECISION (signed_char_type_node))
2354 : 2604136 : return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2355 : :
2356 : 6987476 : if (bits == TYPE_PRECISION (short_integer_type_node))
2357 : 391572 : return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2358 : :
2359 : 6595904 : if (bits == TYPE_PRECISION (long_integer_type_node))
2360 : 5918082 : return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2361 : :
2362 : 677822 : if (bits == TYPE_PRECISION (long_long_integer_type_node))
2363 : 8647 : return (unsignedp ? long_long_unsigned_type_node
2364 : 7918 : : long_long_integer_type_node);
2365 : :
2366 : 1049183 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
2367 : 669175 : if (int_n_enabled_p[i]
2368 : 662591 : && bits == int_n_data[i].bitsize)
2369 : 289167 : return (unsignedp ? int_n_trees[i].unsigned_type
2370 : 289167 : : int_n_trees[i].signed_type);
2371 : :
2372 : 380008 : if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2373 : 0 : return (unsignedp ? widest_unsigned_literal_type_node
2374 : 0 : : widest_integer_literal_type_node);
2375 : :
2376 : 1504030 : for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
2377 : : {
2378 : 1124022 : tree type = TREE_VALUE (t);
2379 : 1124022 : if (TREE_CODE (type) == INTEGER_TYPE
2380 : 0 : && bits == TYPE_PRECISION (type)
2381 : 1124022 : && !!unsignedp == !!TYPE_UNSIGNED (type))
2382 : : return type;
2383 : : }
2384 : :
2385 : 380008 : if (bits <= TYPE_PRECISION (intQI_type_node))
2386 : 318700 : return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2387 : :
2388 : 61308 : if (bits <= TYPE_PRECISION (intHI_type_node))
2389 : 9631 : return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2390 : :
2391 : 51677 : if (bits <= TYPE_PRECISION (intSI_type_node))
2392 : 20669 : return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2393 : :
2394 : 31008 : if (bits <= TYPE_PRECISION (intDI_type_node))
2395 : 24915 : return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2396 : :
2397 : 6093 : if (bits <= TYPE_PRECISION (widest_integer_literal_type_node))
2398 : 31 : return (unsignedp ? widest_unsigned_literal_type_node
2399 : 14 : : widest_integer_literal_type_node);
2400 : :
2401 : : return NULL_TREE;
2402 : : }
2403 : :
2404 : : /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2405 : : that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2406 : : and saturating if SATP is nonzero, otherwise not saturating. */
2407 : :
2408 : : tree
2409 : 0 : c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2410 : : int unsignedp, int satp)
2411 : : {
2412 : 0 : enum mode_class mclass;
2413 : 0 : if (ibit == 0)
2414 : 0 : mclass = unsignedp ? MODE_UFRACT : MODE_FRACT;
2415 : : else
2416 : 0 : mclass = unsignedp ? MODE_UACCUM : MODE_ACCUM;
2417 : :
2418 : 0 : opt_scalar_mode opt_mode;
2419 : 0 : scalar_mode mode;
2420 : 0 : FOR_EACH_MODE_IN_CLASS (opt_mode, mclass)
2421 : : {
2422 : 0 : mode = opt_mode.require ();
2423 : 0 : if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2424 : : break;
2425 : : }
2426 : :
2427 : 0 : if (!opt_mode.exists (&mode) || !targetm.scalar_mode_supported_p (mode))
2428 : : {
2429 : 0 : sorry ("GCC cannot support operators with integer types and "
2430 : : "fixed-point types that have too many integral and "
2431 : : "fractional bits together");
2432 : 0 : return NULL_TREE;
2433 : : }
2434 : :
2435 : 0 : return c_common_type_for_mode (mode, satp);
2436 : : }
2437 : :
2438 : : /* Used for communication between c_common_type_for_mode and
2439 : : c_register_builtin_type. */
2440 : : tree registered_builtin_types;
2441 : :
2442 : : /* Return a data type that has machine mode MODE.
2443 : : If the mode is an integer,
2444 : : then UNSIGNEDP selects between signed and unsigned types.
2445 : : If the mode is a fixed-point mode,
2446 : : then UNSIGNEDP selects between saturating and nonsaturating types. */
2447 : :
2448 : : tree
2449 : 12785153 : c_common_type_for_mode (machine_mode mode, int unsignedp)
2450 : : {
2451 : 12785153 : tree t;
2452 : 12785153 : int i;
2453 : :
2454 : 12785153 : if (mode == TYPE_MODE (integer_type_node))
2455 : 1766306 : return unsignedp ? unsigned_type_node : integer_type_node;
2456 : :
2457 : 11018847 : if (mode == TYPE_MODE (signed_char_type_node))
2458 : 2166158 : return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2459 : :
2460 : 8852689 : if (mode == TYPE_MODE (short_integer_type_node))
2461 : 121995 : return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2462 : :
2463 : 8730694 : if (mode == TYPE_MODE (long_integer_type_node))
2464 : 5497026 : return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2465 : :
2466 : 3233668 : if (mode == TYPE_MODE (long_long_integer_type_node))
2467 : 81687 : return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2468 : :
2469 : 4917156 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
2470 : 3151981 : if (int_n_enabled_p[i]
2471 : 3151981 : && mode == int_n_data[i].m)
2472 : 1386806 : return (unsignedp ? int_n_trees[i].unsigned_type
2473 : 1386806 : : int_n_trees[i].signed_type);
2474 : :
2475 : 1765175 : if (mode == QImode)
2476 : 0 : return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2477 : :
2478 : 1765175 : if (mode == HImode)
2479 : 0 : return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2480 : :
2481 : 1765175 : if (mode == SImode)
2482 : 0 : return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2483 : :
2484 : 1765175 : if (mode == DImode)
2485 : 0 : return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2486 : :
2487 : : #if HOST_BITS_PER_WIDE_INT >= 64
2488 : 1765175 : if (mode == TYPE_MODE (intTI_type_node))
2489 : 0 : return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2490 : : #endif
2491 : :
2492 : 1765175 : if (mode == TYPE_MODE (float_type_node))
2493 : 52161 : return float_type_node;
2494 : :
2495 : 1713014 : if (mode == TYPE_MODE (double_type_node))
2496 : 46531 : return double_type_node;
2497 : :
2498 : 1666483 : if (mode == TYPE_MODE (long_double_type_node))
2499 : 17610 : return long_double_type_node;
2500 : :
2501 : 12559796 : for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2502 : 11063452 : if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2503 : 11063452 : && mode == TYPE_MODE (FLOATN_NX_TYPE_NODE (i)))
2504 : 152529 : return FLOATN_NX_TYPE_NODE (i);
2505 : :
2506 : 1496344 : if (mode == TYPE_MODE (void_type_node))
2507 : 0 : return void_type_node;
2508 : :
2509 : 1496344 : if (mode == TYPE_MODE (build_pointer_type (char_type_node))
2510 : 1496344 : || mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2511 : : {
2512 : 0 : unsigned int precision
2513 : 0 : = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode));
2514 : 0 : return (unsignedp
2515 : 0 : ? make_unsigned_type (precision)
2516 : 0 : : make_signed_type (precision));
2517 : : }
2518 : :
2519 : 1496344 : if (COMPLEX_MODE_P (mode))
2520 : : {
2521 : 1260244 : machine_mode inner_mode;
2522 : 1260244 : tree inner_type;
2523 : :
2524 : 1260244 : if (mode == TYPE_MODE (complex_float_type_node))
2525 : 210795 : return complex_float_type_node;
2526 : 1049449 : if (mode == TYPE_MODE (complex_double_type_node))
2527 : 209853 : return complex_double_type_node;
2528 : 839596 : if (mode == TYPE_MODE (complex_long_double_type_node))
2529 : 209847 : return complex_long_double_type_node;
2530 : :
2531 : 2727712 : for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2532 : 2518132 : if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2533 : 2518132 : && mode == TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i)))
2534 : 420169 : return COMPLEX_FLOATN_NX_TYPE_NODE (i);
2535 : :
2536 : 209580 : if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2537 : 12 : return complex_integer_type_node;
2538 : :
2539 : 209568 : inner_mode = GET_MODE_INNER (mode);
2540 : 209568 : inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2541 : 209568 : if (inner_type != NULL_TREE)
2542 : 209568 : return build_complex_type (inner_type);
2543 : : }
2544 : 236100 : else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
2545 : 236100 : && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
2546 : : {
2547 : 0 : unsigned int elem_bits = vector_element_size (GET_MODE_PRECISION (mode),
2548 : : GET_MODE_NUNITS (mode));
2549 : 0 : tree bool_type = build_nonstandard_boolean_type (elem_bits);
2550 : 0 : return build_vector_type_for_mode (bool_type, mode);
2551 : : }
2552 : 224289 : else if (VECTOR_MODE_P (mode)
2553 : 249746 : && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
2554 : : {
2555 : 13646 : machine_mode inner_mode = GET_MODE_INNER (mode);
2556 : 13646 : tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2557 : 13646 : if (inner_type != NULL_TREE)
2558 : 13646 : return build_vector_type_for_mode (inner_type, mode);
2559 : : }
2560 : :
2561 : 222454 : if (dfloat32_type_node != NULL_TREE
2562 : 222454 : && mode == TYPE_MODE (dfloat32_type_node))
2563 : 2794 : return dfloat32_type_node;
2564 : 219660 : if (dfloat64_type_node != NULL_TREE
2565 : 219660 : && mode == TYPE_MODE (dfloat64_type_node))
2566 : 3146 : return dfloat64_type_node;
2567 : 216514 : if (dfloat128_type_node != NULL_TREE
2568 : 216514 : && mode == TYPE_MODE (dfloat128_type_node))
2569 : 2425 : return dfloat128_type_node;
2570 : :
2571 : 214089 : if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
2572 : : {
2573 : 0 : if (mode == TYPE_MODE (short_fract_type_node))
2574 : 0 : return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
2575 : 0 : if (mode == TYPE_MODE (fract_type_node))
2576 : 0 : return unsignedp ? sat_fract_type_node : fract_type_node;
2577 : 0 : if (mode == TYPE_MODE (long_fract_type_node))
2578 : 0 : return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
2579 : 0 : if (mode == TYPE_MODE (long_long_fract_type_node))
2580 : 0 : return unsignedp ? sat_long_long_fract_type_node
2581 : 0 : : long_long_fract_type_node;
2582 : :
2583 : 0 : if (mode == TYPE_MODE (unsigned_short_fract_type_node))
2584 : 0 : return unsignedp ? sat_unsigned_short_fract_type_node
2585 : 0 : : unsigned_short_fract_type_node;
2586 : 0 : if (mode == TYPE_MODE (unsigned_fract_type_node))
2587 : 0 : return unsignedp ? sat_unsigned_fract_type_node
2588 : 0 : : unsigned_fract_type_node;
2589 : 0 : if (mode == TYPE_MODE (unsigned_long_fract_type_node))
2590 : 0 : return unsignedp ? sat_unsigned_long_fract_type_node
2591 : 0 : : unsigned_long_fract_type_node;
2592 : 0 : if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
2593 : 0 : return unsignedp ? sat_unsigned_long_long_fract_type_node
2594 : 0 : : unsigned_long_long_fract_type_node;
2595 : :
2596 : 0 : if (mode == TYPE_MODE (short_accum_type_node))
2597 : 0 : return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
2598 : 0 : if (mode == TYPE_MODE (accum_type_node))
2599 : 0 : return unsignedp ? sat_accum_type_node : accum_type_node;
2600 : 0 : if (mode == TYPE_MODE (long_accum_type_node))
2601 : 0 : return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
2602 : 0 : if (mode == TYPE_MODE (long_long_accum_type_node))
2603 : 0 : return unsignedp ? sat_long_long_accum_type_node
2604 : 0 : : long_long_accum_type_node;
2605 : :
2606 : 0 : if (mode == TYPE_MODE (unsigned_short_accum_type_node))
2607 : 0 : return unsignedp ? sat_unsigned_short_accum_type_node
2608 : 0 : : unsigned_short_accum_type_node;
2609 : 0 : if (mode == TYPE_MODE (unsigned_accum_type_node))
2610 : 0 : return unsignedp ? sat_unsigned_accum_type_node
2611 : 0 : : unsigned_accum_type_node;
2612 : 0 : if (mode == TYPE_MODE (unsigned_long_accum_type_node))
2613 : 0 : return unsignedp ? sat_unsigned_long_accum_type_node
2614 : 0 : : unsigned_long_accum_type_node;
2615 : 0 : if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
2616 : 0 : return unsignedp ? sat_unsigned_long_long_accum_type_node
2617 : 0 : : unsigned_long_long_accum_type_node;
2618 : :
2619 : 0 : if (mode == QQmode)
2620 : 0 : return unsignedp ? sat_qq_type_node : qq_type_node;
2621 : : if (mode == HQmode)
2622 : 0 : return unsignedp ? sat_hq_type_node : hq_type_node;
2623 : : if (mode == SQmode)
2624 : 0 : return unsignedp ? sat_sq_type_node : sq_type_node;
2625 : : if (mode == DQmode)
2626 : 0 : return unsignedp ? sat_dq_type_node : dq_type_node;
2627 : : if (mode == TQmode)
2628 : 0 : return unsignedp ? sat_tq_type_node : tq_type_node;
2629 : :
2630 : : if (mode == UQQmode)
2631 : 0 : return unsignedp ? sat_uqq_type_node : uqq_type_node;
2632 : : if (mode == UHQmode)
2633 : 0 : return unsignedp ? sat_uhq_type_node : uhq_type_node;
2634 : : if (mode == USQmode)
2635 : 0 : return unsignedp ? sat_usq_type_node : usq_type_node;
2636 : : if (mode == UDQmode)
2637 : 0 : return unsignedp ? sat_udq_type_node : udq_type_node;
2638 : : if (mode == UTQmode)
2639 : 0 : return unsignedp ? sat_utq_type_node : utq_type_node;
2640 : :
2641 : : if (mode == HAmode)
2642 : 0 : return unsignedp ? sat_ha_type_node : ha_type_node;
2643 : : if (mode == SAmode)
2644 : 0 : return unsignedp ? sat_sa_type_node : sa_type_node;
2645 : : if (mode == DAmode)
2646 : 0 : return unsignedp ? sat_da_type_node : da_type_node;
2647 : : if (mode == TAmode)
2648 : 0 : return unsignedp ? sat_ta_type_node : ta_type_node;
2649 : :
2650 : : if (mode == UHAmode)
2651 : 0 : return unsignedp ? sat_uha_type_node : uha_type_node;
2652 : : if (mode == USAmode)
2653 : 0 : return unsignedp ? sat_usa_type_node : usa_type_node;
2654 : : if (mode == UDAmode)
2655 : 0 : return unsignedp ? sat_uda_type_node : uda_type_node;
2656 : : if (mode == UTAmode)
2657 : 0 : return unsignedp ? sat_uta_type_node : uta_type_node;
2658 : : }
2659 : :
2660 : 216605 : for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
2661 : : {
2662 : 215781 : tree type = TREE_VALUE (t);
2663 : 215781 : if (TYPE_MODE (type) == mode
2664 : 213368 : && VECTOR_TYPE_P (type) == VECTOR_MODE_P (mode)
2665 : 429149 : && !!unsignedp == !!TYPE_UNSIGNED (type))
2666 : : return type;
2667 : : }
2668 : : return NULL_TREE;
2669 : : }
2670 : :
2671 : : tree
2672 : 1268047 : c_common_unsigned_type (tree type)
2673 : : {
2674 : 1268047 : return c_common_signed_or_unsigned_type (1, type);
2675 : : }
2676 : :
2677 : : /* Return a signed type the same as TYPE in other respects. */
2678 : :
2679 : : tree
2680 : 60780028 : c_common_signed_type (tree type)
2681 : : {
2682 : 60780028 : return c_common_signed_or_unsigned_type (0, type);
2683 : : }
2684 : :
2685 : : /* Return a type the same as TYPE except unsigned or
2686 : : signed according to UNSIGNEDP. */
2687 : :
2688 : : tree
2689 : 64960421 : c_common_signed_or_unsigned_type (int unsignedp, tree type)
2690 : : {
2691 : 64960421 : tree type1;
2692 : 64960421 : int i;
2693 : :
2694 : : /* This block of code emulates the behavior of the old
2695 : : c_common_unsigned_type. In particular, it returns
2696 : : long_unsigned_type_node if passed a long, even when a int would
2697 : : have the same size. This is necessary for warnings to work
2698 : : correctly in archs where sizeof(int) == sizeof(long) */
2699 : :
2700 : 64960421 : type1 = TYPE_MAIN_VARIANT (type);
2701 : 64960421 : if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
2702 : 2671332 : return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2703 : 62289089 : if (type1 == integer_type_node || type1 == unsigned_type_node)
2704 : 18031747 : return unsignedp ? unsigned_type_node : integer_type_node;
2705 : 44257342 : if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
2706 : 6009268 : return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2707 : 38248074 : if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
2708 : 23846005 : return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2709 : 14402069 : if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
2710 : 2536498 : return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2711 : :
2712 : 23045750 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
2713 : 11865571 : if (int_n_enabled_p[i]
2714 : 10405297 : && (type1 == int_n_trees[i].unsigned_type
2715 : 9720621 : || type1 == int_n_trees[i].signed_type))
2716 : 685392 : return (unsignedp ? int_n_trees[i].unsigned_type
2717 : 685392 : : int_n_trees[i].signed_type);
2718 : :
2719 : : #if HOST_BITS_PER_WIDE_INT >= 64
2720 : 11180179 : if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
2721 : 0 : return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2722 : : #endif
2723 : 11180179 : if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
2724 : 0 : return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2725 : 11180179 : if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
2726 : 0 : return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2727 : 11180179 : if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
2728 : 0 : return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2729 : 11180179 : if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
2730 : 0 : return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2731 : :
2732 : : #define C_COMMON_FIXED_TYPES(NAME) \
2733 : : if (type1 == short_ ## NAME ## _type_node \
2734 : : || type1 == unsigned_short_ ## NAME ## _type_node) \
2735 : : return unsignedp ? unsigned_short_ ## NAME ## _type_node \
2736 : : : short_ ## NAME ## _type_node; \
2737 : : if (type1 == NAME ## _type_node \
2738 : : || type1 == unsigned_ ## NAME ## _type_node) \
2739 : : return unsignedp ? unsigned_ ## NAME ## _type_node \
2740 : : : NAME ## _type_node; \
2741 : : if (type1 == long_ ## NAME ## _type_node \
2742 : : || type1 == unsigned_long_ ## NAME ## _type_node) \
2743 : : return unsignedp ? unsigned_long_ ## NAME ## _type_node \
2744 : : : long_ ## NAME ## _type_node; \
2745 : : if (type1 == long_long_ ## NAME ## _type_node \
2746 : : || type1 == unsigned_long_long_ ## NAME ## _type_node) \
2747 : : return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
2748 : : : long_long_ ## NAME ## _type_node;
2749 : :
2750 : : #define C_COMMON_FIXED_MODE_TYPES(NAME) \
2751 : : if (type1 == NAME ## _type_node \
2752 : : || type1 == u ## NAME ## _type_node) \
2753 : : return unsignedp ? u ## NAME ## _type_node \
2754 : : : NAME ## _type_node;
2755 : :
2756 : : #define C_COMMON_FIXED_TYPES_SAT(NAME) \
2757 : : if (type1 == sat_ ## short_ ## NAME ## _type_node \
2758 : : || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
2759 : : return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
2760 : : : sat_ ## short_ ## NAME ## _type_node; \
2761 : : if (type1 == sat_ ## NAME ## _type_node \
2762 : : || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
2763 : : return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
2764 : : : sat_ ## NAME ## _type_node; \
2765 : : if (type1 == sat_ ## long_ ## NAME ## _type_node \
2766 : : || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
2767 : : return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
2768 : : : sat_ ## long_ ## NAME ## _type_node; \
2769 : : if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
2770 : : || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
2771 : : return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
2772 : : : sat_ ## long_long_ ## NAME ## _type_node;
2773 : :
2774 : : #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
2775 : : if (type1 == sat_ ## NAME ## _type_node \
2776 : : || type1 == sat_ ## u ## NAME ## _type_node) \
2777 : : return unsignedp ? sat_ ## u ## NAME ## _type_node \
2778 : : : sat_ ## NAME ## _type_node;
2779 : :
2780 : 11180179 : C_COMMON_FIXED_TYPES (fract);
2781 : 11180179 : C_COMMON_FIXED_TYPES_SAT (fract);
2782 : 11180179 : C_COMMON_FIXED_TYPES (accum);
2783 : 11180179 : C_COMMON_FIXED_TYPES_SAT (accum);
2784 : :
2785 : 11180179 : C_COMMON_FIXED_MODE_TYPES (qq);
2786 : 11180179 : C_COMMON_FIXED_MODE_TYPES (hq);
2787 : 11180179 : C_COMMON_FIXED_MODE_TYPES (sq);
2788 : 11180179 : C_COMMON_FIXED_MODE_TYPES (dq);
2789 : 11180179 : C_COMMON_FIXED_MODE_TYPES (tq);
2790 : 11180179 : C_COMMON_FIXED_MODE_TYPES_SAT (qq);
2791 : 11180179 : C_COMMON_FIXED_MODE_TYPES_SAT (hq);
2792 : 11180179 : C_COMMON_FIXED_MODE_TYPES_SAT (sq);
2793 : 11180179 : C_COMMON_FIXED_MODE_TYPES_SAT (dq);
2794 : 11180179 : C_COMMON_FIXED_MODE_TYPES_SAT (tq);
2795 : 11180179 : C_COMMON_FIXED_MODE_TYPES (ha);
2796 : 11180179 : C_COMMON_FIXED_MODE_TYPES (sa);
2797 : 11180179 : C_COMMON_FIXED_MODE_TYPES (da);
2798 : 11180179 : C_COMMON_FIXED_MODE_TYPES (ta);
2799 : 11180179 : C_COMMON_FIXED_MODE_TYPES_SAT (ha);
2800 : 11180179 : C_COMMON_FIXED_MODE_TYPES_SAT (sa);
2801 : 11180179 : C_COMMON_FIXED_MODE_TYPES_SAT (da);
2802 : 11180179 : C_COMMON_FIXED_MODE_TYPES_SAT (ta);
2803 : :
2804 : : /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
2805 : : the precision; they have precision set to match their range, but
2806 : : may use a wider mode to match an ABI. If we change modes, we may
2807 : : wind up with bad conversions. For INTEGER_TYPEs in C, must check
2808 : : the precision as well, so as to yield correct results for
2809 : : bit-field types. C++ does not have these separate bit-field
2810 : : types, and producing a signed or unsigned variant of an
2811 : : ENUMERAL_TYPE may cause other problems as well. */
2812 : :
2813 : 11180179 : if (!INTEGRAL_TYPE_P (type)
2814 : 11180179 : || TYPE_UNSIGNED (type) == unsignedp)
2815 : : return type;
2816 : :
2817 : 10220394 : if (TREE_CODE (type) == BITINT_TYPE
2818 : : /* signed _BitInt(1) is invalid, avoid creating that. */
2819 : 10220394 : && (unsignedp || TYPE_PRECISION (type) > 1))
2820 : 1470123 : return build_bitint_type (TYPE_PRECISION (type), unsignedp);
2821 : :
2822 : : #define TYPE_OK(node) \
2823 : : (TYPE_MODE (type) == TYPE_MODE (node) \
2824 : : && TYPE_PRECISION (type) == TYPE_PRECISION (node))
2825 : 8750271 : if (TYPE_OK (signed_char_type_node))
2826 : 477159 : return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2827 : 8273112 : if (TYPE_OK (integer_type_node))
2828 : 4117959 : return unsignedp ? unsigned_type_node : integer_type_node;
2829 : 4155153 : if (TYPE_OK (short_integer_type_node))
2830 : 895173 : return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2831 : 3259980 : if (TYPE_OK (long_integer_type_node))
2832 : 2795839 : return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2833 : 464141 : if (TYPE_OK (long_long_integer_type_node))
2834 : 115447 : return (unsignedp ? long_long_unsigned_type_node
2835 : 0 : : long_long_integer_type_node);
2836 : :
2837 : 679519 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
2838 : 348694 : if (int_n_enabled_p[i]
2839 : 336614 : && TYPE_MODE (type) == int_n_data[i].m
2840 : 366627 : && TYPE_PRECISION (type) == int_n_data[i].bitsize)
2841 : 17869 : return (unsignedp ? int_n_trees[i].unsigned_type
2842 : 17869 : : int_n_trees[i].signed_type);
2843 : :
2844 : : #if HOST_BITS_PER_WIDE_INT >= 64
2845 : 330825 : if (TYPE_OK (intTI_type_node))
2846 : 0 : return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2847 : : #endif
2848 : 330825 : if (TYPE_OK (intDI_type_node))
2849 : 0 : return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2850 : 330825 : if (TYPE_OK (intSI_type_node))
2851 : 0 : return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2852 : 330825 : if (TYPE_OK (intHI_type_node))
2853 : 0 : return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2854 : 330825 : if (TYPE_OK (intQI_type_node))
2855 : 0 : return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2856 : : #undef TYPE_OK
2857 : :
2858 : 330825 : return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
2859 : : }
2860 : :
2861 : : /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
2862 : :
2863 : : tree
2864 : 323489 : c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
2865 : : {
2866 : 323489 : int i;
2867 : :
2868 : : /* Extended integer types of the same width as a standard type have
2869 : : lesser rank, so those of the same width as int promote to int or
2870 : : unsigned int and are valid for printf formats expecting int or
2871 : : unsigned int. To avoid such special cases, avoid creating
2872 : : extended integer types for bit-fields if a standard integer type
2873 : : is available. */
2874 : 323489 : if (width == TYPE_PRECISION (integer_type_node))
2875 : 138 : return unsignedp ? unsigned_type_node : integer_type_node;
2876 : 323351 : if (width == TYPE_PRECISION (signed_char_type_node))
2877 : 6979 : return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2878 : 316372 : if (width == TYPE_PRECISION (short_integer_type_node))
2879 : 1426 : return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2880 : 314946 : if (width == TYPE_PRECISION (long_integer_type_node))
2881 : 0 : return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2882 : 314946 : if (width == TYPE_PRECISION (long_long_integer_type_node))
2883 : 0 : return (unsignedp ? long_long_unsigned_type_node
2884 : 0 : : long_long_integer_type_node);
2885 : 629892 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
2886 : 314946 : if (int_n_enabled_p[i]
2887 : 307969 : && width == int_n_data[i].bitsize)
2888 : 0 : return (unsignedp ? int_n_trees[i].unsigned_type
2889 : 0 : : int_n_trees[i].signed_type);
2890 : 314946 : return build_nonstandard_integer_type (width, unsignedp);
2891 : : }
2892 : :
2893 : : /* The C version of the register_builtin_type langhook. */
2894 : :
2895 : : void
2896 : 628614 : c_register_builtin_type (tree type, const char* name)
2897 : : {
2898 : 628614 : tree decl;
2899 : :
2900 : 628614 : decl = build_decl (UNKNOWN_LOCATION,
2901 : : TYPE_DECL, get_identifier (name), type);
2902 : 628614 : DECL_ARTIFICIAL (decl) = 1;
2903 : 628614 : if (!TYPE_NAME (type))
2904 : 306031 : TYPE_NAME (type) = decl;
2905 : 628614 : lang_hooks.decls.pushdecl (decl);
2906 : :
2907 : 628614 : registered_builtin_types = tree_cons (0, type, registered_builtin_types);
2908 : 628614 : }
2909 : :
2910 : : /* Print an error message for invalid operands to arith operation
2911 : : CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
2912 : : RICHLOC is a rich location for the message, containing either
2913 : : three separate locations for each of the operator and operands
2914 : :
2915 : : lhs op rhs
2916 : : ~~~ ^~ ~~~
2917 : :
2918 : : (C FE), or one location ranging over all over them
2919 : :
2920 : : lhs op rhs
2921 : : ~~~~^~~~~~
2922 : :
2923 : : (C++ FE). */
2924 : :
2925 : : void
2926 : 558 : binary_op_error (rich_location *richloc, enum tree_code code,
2927 : : tree type0, tree type1)
2928 : : {
2929 : 558 : const char *opname;
2930 : :
2931 : 558 : switch (code)
2932 : : {
2933 : : case PLUS_EXPR:
2934 : : opname = "+"; break;
2935 : 14 : case MINUS_EXPR:
2936 : 14 : opname = "-"; break;
2937 : 378 : case MULT_EXPR:
2938 : 378 : opname = "*"; break;
2939 : 0 : case MAX_EXPR:
2940 : 0 : opname = "max"; break;
2941 : 0 : case MIN_EXPR:
2942 : 0 : opname = "min"; break;
2943 : 2 : case EQ_EXPR:
2944 : 2 : opname = "=="; break;
2945 : 2 : case NE_EXPR:
2946 : 2 : opname = "!="; break;
2947 : 0 : case LE_EXPR:
2948 : 0 : opname = "<="; break;
2949 : 0 : case GE_EXPR:
2950 : 0 : opname = ">="; break;
2951 : 1 : case LT_EXPR:
2952 : 1 : opname = "<"; break;
2953 : 1 : case GT_EXPR:
2954 : 1 : opname = ">"; break;
2955 : 21 : case LSHIFT_EXPR:
2956 : 21 : opname = "<<"; break;
2957 : 16 : case RSHIFT_EXPR:
2958 : 16 : opname = ">>"; break;
2959 : 3 : case TRUNC_MOD_EXPR:
2960 : 3 : case FLOOR_MOD_EXPR:
2961 : 3 : opname = "%"; break;
2962 : 6 : case TRUNC_DIV_EXPR:
2963 : 6 : case FLOOR_DIV_EXPR:
2964 : 6 : opname = "/"; break;
2965 : 40 : case BIT_AND_EXPR:
2966 : 40 : opname = "&"; break;
2967 : 21 : case BIT_IOR_EXPR:
2968 : 21 : opname = "|"; break;
2969 : 0 : case TRUTH_ANDIF_EXPR:
2970 : 0 : opname = "&&"; break;
2971 : 0 : case TRUTH_ORIF_EXPR:
2972 : 0 : opname = "||"; break;
2973 : 22 : case BIT_XOR_EXPR:
2974 : 22 : opname = "^"; break;
2975 : 0 : default:
2976 : 0 : gcc_unreachable ();
2977 : : }
2978 : 558 : pp_markup::element_quoted_type element_0 (type0, highlight_colors::lhs);
2979 : 1116 : pp_markup::element_quoted_type element_1 (type1, highlight_colors::rhs);
2980 : 558 : error_at (richloc,
2981 : : "invalid operands to binary %s (have %e and %e)",
2982 : : opname, &element_0, &element_1);
2983 : 558 : }
2984 : :
2985 : : /* Given an expression as a tree, return its original type. Do this
2986 : : by stripping any conversion that preserves the sign and precision. */
2987 : : static tree
2988 : 55646 : expr_original_type (tree expr)
2989 : : {
2990 : 55646 : STRIP_SIGN_NOPS (expr);
2991 : 55646 : return TREE_TYPE (expr);
2992 : : }
2993 : :
2994 : : /* Subroutine of build_binary_op, used for comparison operations.
2995 : : See if the operands have both been converted from subword integer types
2996 : : and, if so, perhaps change them both back to their original type.
2997 : : This function is also responsible for converting the two operands
2998 : : to the proper common type for comparison.
2999 : :
3000 : : The arguments of this function are all pointers to local variables
3001 : : of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3002 : : RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3003 : :
3004 : : LOC is the location of the comparison.
3005 : :
3006 : : If this function returns non-NULL_TREE, it means that the comparison has
3007 : : a constant value. What this function returns is an expression for
3008 : : that value. */
3009 : :
3010 : : tree
3011 : 26548095 : shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
3012 : : tree *restype_ptr, enum tree_code *rescode_ptr)
3013 : : {
3014 : 26548095 : tree type;
3015 : 26548095 : tree op0 = *op0_ptr;
3016 : 26548095 : tree op1 = *op1_ptr;
3017 : 26548095 : int unsignedp0, unsignedp1;
3018 : 26548095 : int real1, real2;
3019 : 26548095 : tree primop0, primop1;
3020 : 26548095 : enum tree_code code = *rescode_ptr;
3021 : :
3022 : : /* Throw away any conversions to wider types
3023 : : already present in the operands. */
3024 : :
3025 : 26548095 : primop0 = c_common_get_narrower (op0, &unsignedp0);
3026 : 26548095 : primop1 = c_common_get_narrower (op1, &unsignedp1);
3027 : :
3028 : : /* If primopN is first sign-extended from primopN's precision to opN's
3029 : : precision, then zero-extended from opN's precision to
3030 : : *restype_ptr precision, shortenings might be invalid. */
3031 : 26548095 : if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3032 : 1522719 : && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3033 : 5890 : && !unsignedp0
3034 : 26553714 : && TYPE_UNSIGNED (TREE_TYPE (op0)))
3035 : : primop0 = op0;
3036 : 26548095 : if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3037 : 915965 : && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3038 : 17193 : && !unsignedp1
3039 : 26559864 : && TYPE_UNSIGNED (TREE_TYPE (op1)))
3040 : : primop1 = op1;
3041 : :
3042 : : /* Handle the case that OP0 does not *contain* a conversion
3043 : : but it *requires* conversion to FINAL_TYPE. */
3044 : :
3045 : 26548095 : if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3046 : 435529 : unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3047 : 26548095 : if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3048 : 6997769 : unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3049 : :
3050 : : /* If one of the operands must be floated, we cannot optimize. */
3051 : 26548095 : real1 = SCALAR_FLOAT_TYPE_P (TREE_TYPE (primop0));
3052 : 26548095 : real2 = SCALAR_FLOAT_TYPE_P (TREE_TYPE (primop1));
3053 : :
3054 : : /* If first arg is constant, swap the args (changing operation
3055 : : so value is preserved), for canonicalization. Don't do this if
3056 : : the second arg is 0. */
3057 : :
3058 : 26548095 : if (TREE_CONSTANT (primop0)
3059 : 5630216 : && !integer_zerop (primop1) && !real_zerop (primop1)
3060 : 30646499 : && !fixed_zerop (primop1))
3061 : : {
3062 : 4098404 : std::swap (primop0, primop1);
3063 : 4098404 : std::swap (op0, op1);
3064 : 4098404 : *op0_ptr = op0;
3065 : 4098404 : *op1_ptr = op1;
3066 : 4098404 : std::swap (unsignedp0, unsignedp1);
3067 : 4098404 : std::swap (real1, real2);
3068 : :
3069 : 4098404 : switch (code)
3070 : : {
3071 : : case LT_EXPR:
3072 : : code = GT_EXPR;
3073 : : break;
3074 : : case GT_EXPR:
3075 : : code = LT_EXPR;
3076 : : break;
3077 : : case LE_EXPR:
3078 : : code = GE_EXPR;
3079 : : break;
3080 : : case GE_EXPR:
3081 : : code = LE_EXPR;
3082 : : break;
3083 : : default:
3084 : : break;
3085 : : }
3086 : 4098404 : *rescode_ptr = code;
3087 : : }
3088 : :
3089 : : /* If comparing an integer against a constant more bits wide,
3090 : : maybe we can deduce a value of 1 or 0 independent of the data.
3091 : : Or else truncate the constant now
3092 : : rather than extend the variable at run time.
3093 : :
3094 : : This is only interesting if the constant is the wider arg.
3095 : : Also, it is not safe if the constant is unsigned and the
3096 : : variable arg is signed, since in this case the variable
3097 : : would be sign-extended and then regarded as unsigned.
3098 : : Our technique fails in this case because the lowest/highest
3099 : : possible unsigned results don't follow naturally from the
3100 : : lowest/highest possible values of the variable operand.
3101 : : For just EQ_EXPR and NE_EXPR there is another technique that
3102 : : could be used: see if the constant can be faithfully represented
3103 : : in the other operand's type, by truncating it and reextending it
3104 : : and see if that preserves the constant's value. */
3105 : :
3106 : 22001042 : if (!real1 && !real2
3107 : 21976397 : && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
3108 : 21976397 : && TREE_CODE (primop1) == INTEGER_CST
3109 : 40872961 : && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3110 : : {
3111 : 1606339 : int min_gt, max_gt, min_lt, max_lt;
3112 : 1606339 : tree maxval, minval;
3113 : : /* 1 if comparison is nominally unsigned. */
3114 : 1606339 : int unsignedp = TYPE_UNSIGNED (*restype_ptr);
3115 : 1606339 : tree val;
3116 : :
3117 : 1606339 : type = c_common_signed_or_unsigned_type (unsignedp0,
3118 : 1606339 : TREE_TYPE (primop0));
3119 : :
3120 : 1606339 : maxval = TYPE_MAX_VALUE (type);
3121 : 1606339 : minval = TYPE_MIN_VALUE (type);
3122 : :
3123 : 1606339 : if (unsignedp && !unsignedp0)
3124 : 421739 : *restype_ptr = c_common_signed_type (*restype_ptr);
3125 : :
3126 : 1606339 : if (TREE_TYPE (primop1) != *restype_ptr)
3127 : : {
3128 : : /* Convert primop1 to target type, but do not introduce
3129 : : additional overflow. We know primop1 is an int_cst. */
3130 : 422043 : primop1 = force_fit_type (*restype_ptr,
3131 : : wi::to_wide
3132 : 844086 : (primop1,
3133 : 422043 : TYPE_PRECISION (*restype_ptr)),
3134 : 422043 : 0, TREE_OVERFLOW (primop1));
3135 : : }
3136 : 1606339 : if (type != *restype_ptr)
3137 : : {
3138 : 1606339 : minval = convert (*restype_ptr, minval);
3139 : 1606339 : maxval = convert (*restype_ptr, maxval);
3140 : : }
3141 : :
3142 : 1606339 : min_gt = tree_int_cst_lt (primop1, minval);
3143 : 1606339 : max_gt = tree_int_cst_lt (primop1, maxval);
3144 : 1606339 : min_lt = tree_int_cst_lt (minval, primop1);
3145 : 1606339 : max_lt = tree_int_cst_lt (maxval, primop1);
3146 : :
3147 : 1606339 : val = 0;
3148 : : /* This used to be a switch, but Genix compiler can't handle that. */
3149 : 1606339 : if (code == NE_EXPR)
3150 : : {
3151 : 356584 : if (max_lt || min_gt)
3152 : 23 : val = truthvalue_true_node;
3153 : : }
3154 : : else if (code == EQ_EXPR)
3155 : : {
3156 : 654994 : if (max_lt || min_gt)
3157 : 35 : val = truthvalue_false_node;
3158 : : }
3159 : : else if (code == LT_EXPR)
3160 : : {
3161 : 324837 : if (max_lt)
3162 : 18 : val = truthvalue_true_node;
3163 : 324837 : if (!min_lt)
3164 : 7810 : val = truthvalue_false_node;
3165 : : }
3166 : : else if (code == GT_EXPR)
3167 : : {
3168 : 121233 : if (min_gt)
3169 : 8 : val = truthvalue_true_node;
3170 : 121233 : if (!max_gt)
3171 : 6 : val = truthvalue_false_node;
3172 : : }
3173 : : else if (code == LE_EXPR)
3174 : : {
3175 : 50549 : if (!max_gt)
3176 : 74 : val = truthvalue_true_node;
3177 : 50549 : if (min_gt)
3178 : 7 : val = truthvalue_false_node;
3179 : : }
3180 : : else if (code == GE_EXPR)
3181 : : {
3182 : 98140 : if (!min_lt)
3183 : 183 : val = truthvalue_true_node;
3184 : 98140 : if (max_lt)
3185 : 14976 : val = truthvalue_false_node;
3186 : : }
3187 : :
3188 : : /* If primop0 was sign-extended and unsigned comparison specd,
3189 : : we did a signed comparison above using the signed type bounds.
3190 : : But the comparison we output must be unsigned.
3191 : :
3192 : : Also, for inequalities, VAL is no good; but if the signed
3193 : : comparison had *any* fixed result, it follows that the
3194 : : unsigned comparison just tests the sign in reverse
3195 : : (positive values are LE, negative ones GE).
3196 : : So we can generate an unsigned comparison
3197 : : against an extreme value of the signed type. */
3198 : :
3199 : 1606339 : if (unsignedp && !unsignedp0)
3200 : : {
3201 : 421739 : if (val != 0)
3202 : 112 : switch (code)
3203 : : {
3204 : 81 : case LT_EXPR:
3205 : 81 : case GE_EXPR:
3206 : 81 : primop1 = TYPE_MIN_VALUE (type);
3207 : 81 : val = 0;
3208 : 81 : break;
3209 : :
3210 : 30 : case LE_EXPR:
3211 : 30 : case GT_EXPR:
3212 : 30 : primop1 = TYPE_MAX_VALUE (type);
3213 : 30 : val = 0;
3214 : 30 : break;
3215 : :
3216 : : default:
3217 : : break;
3218 : : }
3219 : 421739 : type = c_common_unsigned_type (type);
3220 : : }
3221 : :
3222 : 1606339 : if (TREE_CODE (primop0) != INTEGER_CST
3223 : : /* Don't warn if it's from a (non-system) macro. */
3224 : 1606339 : && !(from_macro_expansion_at
3225 : 1095859 : (expansion_point_location_if_in_system_header
3226 : 1095859 : (EXPR_LOCATION (primop0)))))
3227 : : {
3228 : 1054629 : if (val == truthvalue_false_node)
3229 : 7850 : warning_at (loc, OPT_Wtype_limits,
3230 : : "comparison is always false due to limited range of data type");
3231 : 1054629 : if (val == truthvalue_true_node)
3232 : 191 : warning_at (loc, OPT_Wtype_limits,
3233 : : "comparison is always true due to limited range of data type");
3234 : : }
3235 : :
3236 : 1606339 : if (val != 0)
3237 : : {
3238 : : /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3239 : 23029 : if (TREE_SIDE_EFFECTS (primop0))
3240 : 11 : return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
3241 : : return val;
3242 : : }
3243 : :
3244 : : /* Value is not predetermined, but do the comparison
3245 : : in the type of the operand that is not constant.
3246 : : TYPE is already properly set. */
3247 : : }
3248 : :
3249 : : /* If either arg is decimal float and the other is float, find the
3250 : : proper common type to use for comparison. */
3251 : 4547053 : else if (real1 && real2
3252 : 4311174 : && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
3253 : 24958304 : && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
3254 : 16547 : type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3255 : :
3256 : : /* If either arg is decimal float and the other is float, fail. */
3257 : 4530506 : else if (real1 && real2
3258 : 29219836 : && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
3259 : 4294626 : || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
3260 : : {
3261 : 2 : type = *restype_ptr;
3262 : 2 : primop0 = op0;
3263 : 2 : primop1 = op1;
3264 : : }
3265 : :
3266 : 4530504 : else if (real1 && real2
3267 : 29219832 : && (TYPE_PRECISION (TREE_TYPE (primop0))
3268 : 4294625 : == TYPE_PRECISION (TREE_TYPE (primop1))))
3269 : 4199604 : type = TREE_TYPE (primop0);
3270 : :
3271 : : /* If args' natural types are both narrower than nominal type
3272 : : and both extend in the same manner, compare them
3273 : : in the type of the wider arg.
3274 : : Otherwise must actually extend both to the nominal
3275 : : common type lest different ways of extending
3276 : : alter the result.
3277 : : (eg, (short)-1 == (unsigned short)-1 should be 0.) */
3278 : :
3279 : 18126052 : else if (unsignedp0 == unsignedp1 && real1 == real2
3280 : 17878530 : && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
3281 : 513432 : && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr)
3282 : 21077880 : && (type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)))
3283 : 352277 : != error_mark_node)
3284 : : {
3285 : 352277 : type = c_common_signed_or_unsigned_type (unsignedp0
3286 : 352277 : || TYPE_UNSIGNED (*restype_ptr),
3287 : : type);
3288 : : /* Make sure shorter operand is extended the right way
3289 : : to match the longer operand. */
3290 : 352277 : primop0
3291 : 352277 : = convert (c_common_signed_or_unsigned_type (unsignedp0,
3292 : 352277 : TREE_TYPE (primop0)),
3293 : : primop0);
3294 : 352277 : primop1
3295 : 352277 : = convert (c_common_signed_or_unsigned_type (unsignedp1,
3296 : 352277 : TREE_TYPE (primop1)),
3297 : : primop1);
3298 : : }
3299 : : else
3300 : : {
3301 : : /* Here we must do the comparison on the nominal type
3302 : : using the args exactly as we received them. */
3303 : 20373326 : type = *restype_ptr;
3304 : 20373326 : primop0 = op0;
3305 : 20373326 : primop1 = op1;
3306 : :
3307 : : /* We want to fold unsigned comparisons of >= and < against zero.
3308 : : For these, we may also issue a warning if we have a non-constant
3309 : : compared against zero, where the zero was spelled as "0" (rather
3310 : : than merely folding to it).
3311 : : If we have at least one constant, then op1 is constant
3312 : : and we may have a non-constant expression as op0. */
3313 : 20042426 : if (!real1 && !real2 && integer_zerop (primop1)
3314 : 28729576 : && TYPE_UNSIGNED (*restype_ptr))
3315 : : {
3316 : 2798135 : tree value = NULL_TREE;
3317 : : /* All unsigned values are >= 0, so we warn. However,
3318 : : if OP0 is a constant that is >= 0, the signedness of
3319 : : the comparison isn't an issue, so suppress the
3320 : : warning. */
3321 : 2798135 : tree folded_op0 = fold_for_warn (op0);
3322 : 2798135 : bool warn =
3323 : 71300 : warn_type_limits && !in_system_header_at (loc)
3324 : 65084 : && !(TREE_CODE (folded_op0) == INTEGER_CST
3325 : 9438 : && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3326 : : folded_op0)))
3327 : : /* Do not warn for enumeration types. */
3328 : 2853781 : && (TREE_CODE (expr_original_type (folded_op0)) != ENUMERAL_TYPE);
3329 : :
3330 : 2798135 : switch (code)
3331 : : {
3332 : 12120 : case GE_EXPR:
3333 : 12120 : if (warn)
3334 : 56 : warning_at (loc, OPT_Wtype_limits,
3335 : : "comparison of unsigned expression in %<>= 0%> "
3336 : : "is always true");
3337 : 12120 : value = truthvalue_true_node;
3338 : 12120 : break;
3339 : :
3340 : 308862 : case LT_EXPR:
3341 : 308862 : if (warn)
3342 : 55 : warning_at (loc, OPT_Wtype_limits,
3343 : : "comparison of unsigned expression in %<< 0%> "
3344 : : "is always false");
3345 : 308862 : value = truthvalue_false_node;
3346 : 308862 : break;
3347 : :
3348 : : default:
3349 : : break;
3350 : : }
3351 : :
3352 : 320982 : if (value != NULL_TREE)
3353 : : {
3354 : : /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3355 : 320982 : if (TREE_SIDE_EFFECTS (primop0))
3356 : 39 : return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3357 : 39 : primop0, value);
3358 : : return value;
3359 : : }
3360 : : }
3361 : : }
3362 : :
3363 : 26204084 : *op0_ptr = convert (type, primop0);
3364 : 26204084 : *op1_ptr = convert (type, primop1);
3365 : :
3366 : 26204084 : *restype_ptr = truthvalue_type_node;
3367 : :
3368 : 26204084 : return NULL_TREE;
3369 : : }
3370 : :
3371 : : /* Return a tree for the sum or difference (RESULTCODE says which)
3372 : : of pointer PTROP and integer INTOP. */
3373 : :
3374 : : tree
3375 : 5579241 : pointer_int_sum (location_t loc, enum tree_code resultcode,
3376 : : tree ptrop, tree intop, bool complain)
3377 : : {
3378 : 5579241 : tree size_exp, ret;
3379 : :
3380 : : /* The result is a pointer of the same type that is being added. */
3381 : 5579241 : tree result_type = TREE_TYPE (ptrop);
3382 : :
3383 : 5579241 : if (VOID_TYPE_P (TREE_TYPE (result_type)))
3384 : : {
3385 : 900 : if (complain && warn_pointer_arith)
3386 : 61 : pedwarn (loc, OPT_Wpointer_arith,
3387 : : "pointer of type %<void *%> used in arithmetic");
3388 : : else if (!complain)
3389 : 0 : return error_mark_node;
3390 : 900 : size_exp = integer_one_node;
3391 : : }
3392 : 5578341 : else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3393 : : {
3394 : 118 : if (complain && warn_pointer_arith)
3395 : 49 : pedwarn (loc, OPT_Wpointer_arith,
3396 : : "pointer to a function used in arithmetic");
3397 : : else if (!complain)
3398 : 0 : return error_mark_node;
3399 : 118 : size_exp = integer_one_node;
3400 : : }
3401 : 5578223 : else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
3402 : 5578223 : TREE_TYPE (result_type)))
3403 : 0 : size_exp = integer_one_node;
3404 : : else
3405 : : {
3406 : 5578223 : if (!complain && !COMPLETE_TYPE_P (TREE_TYPE (result_type)))
3407 : 108 : return error_mark_node;
3408 : 5578115 : size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type));
3409 : : /* Wrap the pointer expression in a SAVE_EXPR to make sure it
3410 : : is evaluated first when the size expression may depend
3411 : : on it for VM types. */
3412 : 5578115 : if (TREE_SIDE_EFFECTS (size_exp)
3413 : 1935 : && TREE_SIDE_EFFECTS (ptrop)
3414 : 5578134 : && variably_modified_type_p (TREE_TYPE (ptrop), NULL))
3415 : : {
3416 : 19 : ptrop = save_expr (ptrop);
3417 : 19 : size_exp = build2 (COMPOUND_EXPR, TREE_TYPE (intop), ptrop, size_exp);
3418 : : }
3419 : : }
3420 : :
3421 : : /* We are manipulating pointer values, so we don't need to warn
3422 : : about relying on undefined signed overflow. We disable the
3423 : : warning here because we use integer types so fold won't know that
3424 : : they are really pointers. */
3425 : 5579133 : fold_defer_overflow_warnings ();
3426 : :
3427 : : /* If what we are about to multiply by the size of the elements
3428 : : contains a constant term, apply distributive law
3429 : : and multiply that constant term separately.
3430 : : This helps produce common subexpressions. */
3431 : 5579133 : if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3432 : 175145 : && !TREE_CONSTANT (intop)
3433 : 175063 : && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3434 : 140388 : && TREE_CONSTANT (size_exp)
3435 : : /* If the constant comes from pointer subtraction,
3436 : : skip this optimization--it would cause an error. */
3437 : 140387 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3438 : : /* If the constant is unsigned, and smaller than the pointer size,
3439 : : then we must skip this optimization. This is because it could cause
3440 : : an overflow error if the constant is negative but INTOP is not. */
3441 : 280774 : && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (intop))
3442 : 120366 : || (TYPE_PRECISION (TREE_TYPE (intop))
3443 : 120366 : == TYPE_PRECISION (TREE_TYPE (ptrop))))
3444 : 5755097 : && TYPE_PRECISION (TREE_TYPE (intop)) <= TYPE_PRECISION (sizetype))
3445 : : {
3446 : 35577 : tree intop0 = TREE_OPERAND (intop, 0);
3447 : 35577 : tree intop1 = TREE_OPERAND (intop, 1);
3448 : 35577 : if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3449 : 35577 : || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3450 : : {
3451 : 20021 : tree optype = c_common_type_for_size (TYPE_PRECISION (sizetype),
3452 : 20021 : TYPE_UNSIGNED (sizetype));
3453 : 20021 : intop0 = convert (optype, intop0);
3454 : 20021 : intop1 = convert (optype, intop1);
3455 : : }
3456 : 35577 : tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop0), intop0,
3457 : 35577 : convert (TREE_TYPE (intop0), size_exp));
3458 : 35577 : intop0 = convert (sizetype, t);
3459 : 35577 : if (TREE_OVERFLOW_P (intop0) && !TREE_OVERFLOW (t))
3460 : 0 : intop0 = wide_int_to_tree (TREE_TYPE (intop0), wi::to_wide (intop0));
3461 : 35577 : t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop1), intop1,
3462 : 35577 : convert (TREE_TYPE (intop1), size_exp));
3463 : 35577 : intop1 = convert (sizetype, t);
3464 : 35577 : if (TREE_OVERFLOW_P (intop1) && !TREE_OVERFLOW (t))
3465 : 0 : intop1 = wide_int_to_tree (TREE_TYPE (intop1), wi::to_wide (intop1));
3466 : 35577 : intop = build_binary_op (EXPR_LOCATION (intop), TREE_CODE (intop),
3467 : : intop0, intop1, true);
3468 : :
3469 : : /* Create the sum or difference. */
3470 : 35577 : if (resultcode == MINUS_EXPR)
3471 : 51 : intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3472 : :
3473 : 35577 : ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
3474 : :
3475 : 35577 : fold_undefer_and_ignore_overflow_warnings ();
3476 : :
3477 : 35577 : return ret;
3478 : : }
3479 : :
3480 : : /* Convert the integer argument to a type the same size as sizetype
3481 : : so the multiply won't overflow spuriously. */
3482 : 5543556 : if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3483 : 5543556 : || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3484 : 3863843 : intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
3485 : 3863843 : TYPE_UNSIGNED (sizetype)), intop);
3486 : :
3487 : : /* Replace the integer argument with a suitable product by the object size.
3488 : : Do this multiplication as signed, then convert to the appropriate type
3489 : : for the pointer operation and disregard an overflow that occurred only
3490 : : because of the sign-extension change in the latter conversion. */
3491 : 5543556 : {
3492 : 5543556 : tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
3493 : 5543556 : convert (TREE_TYPE (intop), size_exp));
3494 : 5543556 : intop = convert (sizetype, t);
3495 : 5543556 : if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
3496 : 0 : intop = wide_int_to_tree (TREE_TYPE (intop), wi::to_wide (intop));
3497 : : }
3498 : :
3499 : : /* Create the sum or difference. */
3500 : 5543556 : if (resultcode == MINUS_EXPR)
3501 : 218508 : intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3502 : :
3503 : 5543556 : ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
3504 : :
3505 : 5543556 : fold_undefer_and_ignore_overflow_warnings ();
3506 : :
3507 : 5543556 : return ret;
3508 : : }
3509 : :
3510 : : /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3511 : : and if NON_CONST is known not to be permitted in an evaluated part
3512 : : of a constant expression. */
3513 : :
3514 : : tree
3515 : 4385924 : c_wrap_maybe_const (tree expr, bool non_const)
3516 : : {
3517 : 4385924 : location_t loc = EXPR_LOCATION (expr);
3518 : :
3519 : : /* This should never be called for C++. */
3520 : 4385924 : if (c_dialect_cxx ())
3521 : 0 : gcc_unreachable ();
3522 : :
3523 : : /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
3524 : 4386670 : STRIP_TYPE_NOPS (expr);
3525 : 4385924 : expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
3526 : 4385924 : C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
3527 : 4385924 : protected_set_expr_location (expr, loc);
3528 : :
3529 : 4385924 : return expr;
3530 : : }
3531 : :
3532 : : /* Return whether EXPR is a declaration whose address can never be NULL.
3533 : : The address of the first struct member could be NULL only if it were
3534 : : accessed through a NULL pointer, and such an access would be invalid.
3535 : : The address of a weak symbol may be null unless it has a definition. */
3536 : :
3537 : : bool
3538 : 1409 : decl_with_nonnull_addr_p (const_tree expr)
3539 : : {
3540 : 1409 : if (!DECL_P (expr))
3541 : : return false;
3542 : :
3543 : 1155 : if (TREE_CODE (expr) == FIELD_DECL
3544 : 1078 : || TREE_CODE (expr) == PARM_DECL
3545 : 1044 : || TREE_CODE (expr) == LABEL_DECL)
3546 : : return true;
3547 : :
3548 : 1019 : if (!VAR_OR_FUNCTION_DECL_P (expr))
3549 : : return false;
3550 : :
3551 : 1019 : if (!DECL_WEAK (expr))
3552 : : /* Ordinary (non-weak) symbols have nonnull addresses. */
3553 : : return true;
3554 : :
3555 : 528 : if (DECL_INITIAL (expr) && DECL_INITIAL (expr) != error_mark_node)
3556 : : /* Initialized weak symbols have nonnull addresses. */
3557 : : return true;
3558 : :
3559 : 475 : if (DECL_EXTERNAL (expr) || !TREE_STATIC (expr))
3560 : : /* Uninitialized extern weak symbols and weak symbols with no
3561 : : allocated storage might have a null address. */
3562 : : return false;
3563 : :
3564 : 43 : tree attribs = DECL_ATTRIBUTES (expr);
3565 : 43 : if (lookup_attribute ("weakref", attribs))
3566 : : /* Weakref symbols might have a null address unless their referent
3567 : : is known not to. Don't bother following weakref targets here. */
3568 : : return false;
3569 : :
3570 : : return true;
3571 : : }
3572 : :
3573 : : /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3574 : : or for an `if' or `while' statement or ?..: exp. It should already
3575 : : have been validated to be of suitable type; otherwise, a bad
3576 : : diagnostic may result.
3577 : :
3578 : : The EXPR is located at LOCATION.
3579 : :
3580 : : This preparation consists of taking the ordinary
3581 : : representation of an expression expr and producing a valid tree
3582 : : boolean expression describing whether expr is nonzero. We could
3583 : : simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3584 : : but we optimize comparisons, &&, ||, and !.
3585 : :
3586 : : The resulting type should always be `truthvalue_type_node'. */
3587 : :
3588 : : tree
3589 : 10578561 : c_common_truthvalue_conversion (location_t location, tree expr)
3590 : : {
3591 : 11387527 : STRIP_ANY_LOCATION_WRAPPER (expr);
3592 : 11387527 : switch (TREE_CODE (expr))
3593 : : {
3594 : 2457146 : case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
3595 : 2457146 : case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3596 : 2457146 : case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
3597 : 2457146 : case ORDERED_EXPR: case UNORDERED_EXPR:
3598 : 2457146 : if (TREE_TYPE (expr) == truthvalue_type_node)
3599 : : return expr;
3600 : 403519 : expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3601 : 403519 : TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
3602 : 403519 : goto ret;
3603 : :
3604 : 589913 : case TRUTH_ANDIF_EXPR:
3605 : 589913 : case TRUTH_ORIF_EXPR:
3606 : 589913 : case TRUTH_AND_EXPR:
3607 : 589913 : case TRUTH_OR_EXPR:
3608 : 589913 : case TRUTH_XOR_EXPR:
3609 : 589913 : if (TREE_TYPE (expr) == truthvalue_type_node)
3610 : : return expr;
3611 : 74 : expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3612 : : c_common_truthvalue_conversion (location,
3613 : 37 : TREE_OPERAND (expr, 0)),
3614 : : c_common_truthvalue_conversion (location,
3615 : 37 : TREE_OPERAND (expr, 1)));
3616 : 37 : goto ret;
3617 : :
3618 : 327491 : case TRUTH_NOT_EXPR:
3619 : 327491 : if (TREE_TYPE (expr) == truthvalue_type_node)
3620 : : return expr;
3621 : 324552 : expr = build1 (TREE_CODE (expr), truthvalue_type_node,
3622 : : c_common_truthvalue_conversion (location,
3623 : 324552 : TREE_OPERAND (expr, 0)));
3624 : 324552 : goto ret;
3625 : :
3626 : : case ERROR_MARK:
3627 : : return expr;
3628 : :
3629 : 2638088 : case INTEGER_CST:
3630 : 2638088 : if (TREE_CODE (TREE_TYPE (expr)) == ENUMERAL_TYPE
3631 : 767726 : && !integer_zerop (expr)
3632 : 3119275 : && !integer_onep (expr))
3633 : 889 : warning_at (location, OPT_Wint_in_bool_context,
3634 : : "enum constant in boolean context");
3635 : 2638088 : return integer_zerop (expr) ? truthvalue_false_node
3636 : 2638088 : : truthvalue_true_node;
3637 : :
3638 : 1288 : case REAL_CST:
3639 : 1288 : return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
3640 : 1288 : ? truthvalue_true_node
3641 : 1288 : : truthvalue_false_node;
3642 : :
3643 : 0 : case FIXED_CST:
3644 : 0 : return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
3645 : 0 : &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
3646 : 0 : ? truthvalue_true_node
3647 : 0 : : truthvalue_false_node;
3648 : :
3649 : 0 : case FUNCTION_DECL:
3650 : 0 : expr = build_unary_op (location, ADDR_EXPR, expr, false);
3651 : : /* Fall through. */
3652 : :
3653 : 578 : case ADDR_EXPR:
3654 : 578 : {
3655 : 578 : tree inner = TREE_OPERAND (expr, 0);
3656 : 578 : if (decl_with_nonnull_addr_p (inner)
3657 : : /* Check both EXPR and INNER for suppression. */
3658 : 127 : && !warning_suppressed_p (expr, OPT_Waddress)
3659 : 691 : && !warning_suppressed_p (inner, OPT_Waddress))
3660 : : {
3661 : : /* Common Ada programmer's mistake. */
3662 : 107 : warning_at (location,
3663 : 107 : OPT_Waddress,
3664 : : "the address of %qD will always evaluate as %<true%>",
3665 : : inner);
3666 : 107 : suppress_warning (inner, OPT_Waddress);
3667 : 107 : return truthvalue_true_node;
3668 : : }
3669 : : break;
3670 : : }
3671 : :
3672 : 578 : case COMPLEX_EXPR:
3673 : 1734 : expr = build_binary_op (EXPR_LOCATION (expr),
3674 : 578 : (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3675 : : ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3676 : : c_common_truthvalue_conversion (location,
3677 : 578 : TREE_OPERAND (expr, 0)),
3678 : : c_common_truthvalue_conversion (location,
3679 : 578 : TREE_OPERAND (expr, 1)),
3680 : : false);
3681 : 578 : goto ret;
3682 : :
3683 : 74 : case NEGATE_EXPR:
3684 : 74 : case ABS_EXPR:
3685 : 74 : case ABSU_EXPR:
3686 : 74 : case FLOAT_EXPR:
3687 : 74 : case EXCESS_PRECISION_EXPR:
3688 : : /* These don't change whether an object is nonzero or zero. */
3689 : 74 : return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
3690 : :
3691 : 0 : case LROTATE_EXPR:
3692 : 0 : case RROTATE_EXPR:
3693 : : /* These don't change whether an object is zero or nonzero, but
3694 : : we can't ignore them if their second arg has side-effects. */
3695 : 0 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
3696 : : {
3697 : 0 : expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
3698 : 0 : TREE_OPERAND (expr, 1),
3699 : : c_common_truthvalue_conversion
3700 : 0 : (location, TREE_OPERAND (expr, 0)));
3701 : 0 : goto ret;
3702 : : }
3703 : : else
3704 : 0 : return c_common_truthvalue_conversion (location,
3705 : 0 : TREE_OPERAND (expr, 0));
3706 : :
3707 : 502 : case MULT_EXPR:
3708 : 502 : warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3709 : : "%<*%> in boolean context, suggest %<&&%> instead");
3710 : 502 : break;
3711 : :
3712 : 166 : case LSHIFT_EXPR:
3713 : : /* We will only warn on signed shifts here, because the majority of
3714 : : false positive warnings happen in code where unsigned arithmetic
3715 : : was used in anticipation of a possible overflow.
3716 : : Furthermore, if we see an unsigned type here we know that the
3717 : : result of the shift is not subject to integer promotion rules. */
3718 : 166 : if ((TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
3719 : 1 : || TREE_CODE (TREE_TYPE (expr)) == BITINT_TYPE)
3720 : 167 : && !TYPE_UNSIGNED (TREE_TYPE (expr)))
3721 : 150 : warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3722 : : "%<<<%> in boolean context, did you mean %<<%>?");
3723 : : break;
3724 : :
3725 : 46638 : case COND_EXPR:
3726 : 46638 : if (warn_int_in_bool_context
3727 : 48146 : && !from_macro_definition_at (EXPR_LOCATION (expr)))
3728 : : {
3729 : 741 : tree val1 = fold_for_warn (TREE_OPERAND (expr, 1));
3730 : 741 : tree val2 = fold_for_warn (TREE_OPERAND (expr, 2));
3731 : 741 : if (TREE_CODE (val1) == INTEGER_CST
3732 : 78 : && TREE_CODE (val2) == INTEGER_CST
3733 : 78 : && !integer_zerop (val1)
3734 : 70 : && !integer_zerop (val2)
3735 : 757 : && (!integer_onep (val1)
3736 : 12 : || !integer_onep (val2)))
3737 : 12 : warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3738 : : "%<?:%> using integer constants in boolean context, "
3739 : : "the expression will always evaluate to %<true%>");
3740 : 729 : else if ((TREE_CODE (val1) == INTEGER_CST
3741 : 66 : && !integer_zerop (val1)
3742 : 58 : && !integer_onep (val1))
3743 : 795 : || (TREE_CODE (val2) == INTEGER_CST
3744 : 332 : && !integer_zerop (val2)
3745 : 274 : && !integer_onep (val2)))
3746 : 4 : warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3747 : : "%<?:%> using integer constants in boolean context");
3748 : : }
3749 : : /* Distribute the conversion into the arms of a COND_EXPR. */
3750 : 46638 : if (c_dialect_cxx ())
3751 : : /* Avoid premature folding. */
3752 : : break;
3753 : : else
3754 : : {
3755 : 21558 : int w = warn_int_in_bool_context;
3756 : 21558 : warn_int_in_bool_context = 0;
3757 : : /* Folding will happen later for C. */
3758 : 64674 : expr = build3 (COND_EXPR, truthvalue_type_node,
3759 : 21558 : TREE_OPERAND (expr, 0),
3760 : : c_common_truthvalue_conversion (location,
3761 : 21558 : TREE_OPERAND (expr, 1)),
3762 : : c_common_truthvalue_conversion (location,
3763 : 21558 : TREE_OPERAND (expr, 2)));
3764 : 21558 : warn_int_in_bool_context = w;
3765 : 21558 : goto ret;
3766 : : }
3767 : :
3768 : 590295 : CASE_CONVERT:
3769 : 590295 : {
3770 : 590295 : tree totype = TREE_TYPE (expr);
3771 : 590295 : tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
3772 : :
3773 : 590295 : if (POINTER_TYPE_P (totype)
3774 : 2016 : && !c_inhibit_evaluation_warnings
3775 : 1848 : && TREE_CODE (fromtype) == REFERENCE_TYPE)
3776 : : {
3777 : 0 : tree inner = expr;
3778 : 0 : STRIP_NOPS (inner);
3779 : :
3780 : 0 : if (DECL_P (inner))
3781 : 0 : warning_at (location,
3782 : 0 : OPT_Waddress,
3783 : : "the compiler can assume that the address of "
3784 : : "%qD will always evaluate to %<true%>",
3785 : : inner);
3786 : : }
3787 : :
3788 : : /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3789 : : since that affects how `default_conversion' will behave. */
3790 : 590295 : if (TREE_CODE (totype) == REFERENCE_TYPE
3791 : 590295 : || TREE_CODE (fromtype) == REFERENCE_TYPE)
3792 : : break;
3793 : : /* Don't strip a conversion from C++0x scoped enum, since they
3794 : : don't implicitly convert to other types. */
3795 : 590295 : if (TREE_CODE (fromtype) == ENUMERAL_TYPE
3796 : 590295 : && ENUM_IS_SCOPED (fromtype))
3797 : : break;
3798 : : /* If this isn't narrowing the argument, we can ignore it. */
3799 : 590256 : if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
3800 : : {
3801 : 572940 : tree op0 = TREE_OPERAND (expr, 0);
3802 : 572940 : if ((TREE_CODE (fromtype) == POINTER_TYPE
3803 : 1932 : && (TREE_CODE (totype) == INTEGER_TYPE
3804 : 1932 : || TREE_CODE (totype) == BITINT_TYPE))
3805 : 574840 : || warning_suppressed_p (expr, OPT_Waddress))
3806 : : /* Suppress -Waddress for casts to intptr_t, propagating
3807 : : any suppression from the enclosing expression to its
3808 : : operand. */
3809 : 44 : suppress_warning (op0, OPT_Waddress);
3810 : 572940 : return c_common_truthvalue_conversion (location, op0);
3811 : : }
3812 : : }
3813 : : break;
3814 : :
3815 : 8826 : case MODIFY_EXPR:
3816 : 8826 : if (!warning_suppressed_p (expr, OPT_Wparentheses)
3817 : 269 : && warn_parentheses
3818 : 9095 : && warning_at (location, OPT_Wparentheses,
3819 : : "suggest parentheses around assignment used as "
3820 : : "truth value"))
3821 : 14 : suppress_warning (expr, OPT_Wparentheses);
3822 : : break;
3823 : :
3824 : 235969 : case CONST_DECL:
3825 : 235969 : {
3826 : 235969 : tree folded_expr = fold_for_warn (expr);
3827 : 235969 : if (folded_expr != expr)
3828 : : return c_common_truthvalue_conversion (location, folded_expr);
3829 : : }
3830 : : break;
3831 : :
3832 : : default:
3833 : : break;
3834 : : }
3835 : :
3836 : 4542169 : if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
3837 : : {
3838 : 405 : tree t = save_expr (expr);
3839 : 405 : expr = (build_binary_op
3840 : 810 : (EXPR_LOCATION (expr),
3841 : 405 : (TREE_SIDE_EFFECTS (expr)
3842 : : ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3843 : : c_common_truthvalue_conversion
3844 : : (location,
3845 : : build_unary_op (location, REALPART_EXPR, t, false)),
3846 : : c_common_truthvalue_conversion
3847 : : (location,
3848 : : build_unary_op (location, IMAGPART_EXPR, t, false)),
3849 : : false));
3850 : 405 : goto ret;
3851 : : }
3852 : :
3853 : 4541764 : if (FIXED_POINT_TYPE_P (TREE_TYPE (expr)))
3854 : : {
3855 : 0 : tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
3856 : 0 : FCONST0 (TYPE_MODE
3857 : : (TREE_TYPE (expr))));
3858 : 0 : return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, true);
3859 : : }
3860 : : else
3861 : 4541764 : return build_binary_op (location, NE_EXPR, expr, integer_zero_node, true);
3862 : :
3863 : 750649 : ret:
3864 : 750649 : protected_set_expr_location (expr, location);
3865 : 750649 : return expr;
3866 : : }
3867 : :
3868 : : static void def_builtin_1 (enum built_in_function fncode,
3869 : : const char *name,
3870 : : enum built_in_class fnclass,
3871 : : tree fntype, tree libtype,
3872 : : bool both_p, bool fallback_p, bool nonansi_p,
3873 : : tree fnattrs, bool implicit_p);
3874 : :
3875 : :
3876 : : /* Apply the TYPE_QUALS to the new DECL. */
3877 : :
3878 : : void
3879 : 958097585 : c_apply_type_quals_to_decl (int type_quals, tree decl)
3880 : : {
3881 : 958097585 : tree type = TREE_TYPE (decl);
3882 : :
3883 : 958097585 : if (type == error_mark_node)
3884 : : return;
3885 : :
3886 : 958097360 : if ((type_quals & TYPE_QUAL_CONST)
3887 : 619510162 : || (type && TREE_CODE (type) == REFERENCE_TYPE))
3888 : : /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
3889 : : constructor can produce constant init, so rely on cp_finish_decl to
3890 : : clear TREE_READONLY if the variable has non-constant init. */
3891 : 430644519 : TREE_READONLY (decl) = 1;
3892 : 958097360 : if (type_quals & TYPE_QUAL_VOLATILE)
3893 : : {
3894 : 135061 : TREE_SIDE_EFFECTS (decl) = 1;
3895 : 135061 : TREE_THIS_VOLATILE (decl) = 1;
3896 : : }
3897 : 958097360 : if (type_quals & TYPE_QUAL_RESTRICT)
3898 : : {
3899 : 10805393 : while (type && TREE_CODE (type) == ARRAY_TYPE)
3900 : : /* Allow 'restrict' on arrays of pointers.
3901 : : FIXME currently we just ignore it. */
3902 : 7 : type = TREE_TYPE (type);
3903 : 10805386 : if (!type
3904 : 10805386 : || !POINTER_TYPE_P (type)
3905 : 21610769 : || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
3906 : 4 : error ("invalid use of %<restrict%>");
3907 : : }
3908 : : }
3909 : :
3910 : : /* Return the typed-based alias set for T, which may be an expression
3911 : : or a type. Return -1 if we don't do anything special. */
3912 : :
3913 : : alias_set_type
3914 : 649789634 : c_common_get_alias_set (tree t)
3915 : : {
3916 : : /* For VLAs, use the alias set of the element type rather than the
3917 : : default of alias set 0 for types compared structurally. */
3918 : 649789634 : if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
3919 : : {
3920 : 885587 : if (TREE_CODE (t) == ARRAY_TYPE)
3921 : 449602 : return get_alias_set (TREE_TYPE (t));
3922 : : return -1;
3923 : : }
3924 : :
3925 : : /* That's all the expressions we handle specially. */
3926 : 648904047 : if (!TYPE_P (t))
3927 : : return -1;
3928 : :
3929 : : /* Unlike char, char8_t doesn't alias in C++. (In C, char8_t is not
3930 : : a distinct type.) */
3931 : 109110797 : if (flag_char8_t && t == char8_type_node && c_dialect_cxx ())
3932 : : return -1;
3933 : :
3934 : : /* The C standard guarantees that any object may be accessed via an
3935 : : lvalue that has narrow character type. */
3936 : 109110722 : if (t == char_type_node
3937 : 72449767 : || t == signed_char_type_node
3938 : 68394805 : || t == unsigned_char_type_node)
3939 : : return 0;
3940 : :
3941 : : /* The C standard specifically allows aliasing between signed and
3942 : : unsigned variants of the same type. We treat the signed
3943 : : variant as canonical. */
3944 : 57322487 : if ((TREE_CODE (t) == INTEGER_TYPE || TREE_CODE (t) == BITINT_TYPE)
3945 : 57322487 : && TYPE_UNSIGNED (t))
3946 : : {
3947 : 55707492 : tree t1 = c_common_signed_type (t);
3948 : :
3949 : : /* t1 == t can happen for boolean nodes which are always unsigned. */
3950 : 55707492 : if (t1 != t)
3951 : 55707492 : return get_alias_set (t1);
3952 : : }
3953 : :
3954 : : return -1;
3955 : : }
3956 : :
3957 : : /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
3958 : : the IS_SIZEOF parameter indicates which operator is being applied.
3959 : : The COMPLAIN flag controls whether we should diagnose possibly
3960 : : ill-formed constructs or not. LOC is the location of the SIZEOF or
3961 : : TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
3962 : : a type in any context should be returned, rather than the normal
3963 : : alignment for that type. */
3964 : :
3965 : : tree
3966 : 19388209 : c_sizeof_or_alignof_type (location_t loc,
3967 : : tree type, bool is_sizeof, bool min_alignof,
3968 : : int complain)
3969 : : {
3970 : 19388209 : const char *op_name;
3971 : 19388209 : tree value = NULL;
3972 : 19388209 : enum tree_code type_code = TREE_CODE (type);
3973 : :
3974 : 19388209 : op_name = is_sizeof ? "sizeof" : "__alignof__";
3975 : :
3976 : 19388209 : if (type_code == FUNCTION_TYPE)
3977 : : {
3978 : 64 : if (is_sizeof)
3979 : : {
3980 : 58 : if (complain && warn_pointer_arith)
3981 : 28 : pedwarn (loc, OPT_Wpointer_arith,
3982 : : "invalid application of %<sizeof%> to a function type");
3983 : : else if (!complain)
3984 : 20 : return error_mark_node;
3985 : 38 : value = size_one_node;
3986 : : }
3987 : : else
3988 : : {
3989 : 6 : if (complain)
3990 : : {
3991 : 6 : if (c_dialect_cxx ())
3992 : 4 : pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
3993 : : "%<alignof%> applied to a function type");
3994 : : else
3995 : 2 : pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
3996 : : "%<_Alignof%> applied to a function type");
3997 : : }
3998 : 6 : value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
3999 : : }
4000 : : }
4001 : 19388145 : else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4002 : : {
4003 : 83 : if (type_code == VOID_TYPE
4004 : 83 : && complain && warn_pointer_arith)
4005 : 19 : pedwarn (loc, OPT_Wpointer_arith,
4006 : : "invalid application of %qs to a void type", op_name);
4007 : 64 : else if (!complain)
4008 : 30 : return error_mark_node;
4009 : 53 : value = size_one_node;
4010 : : }
4011 : 19388062 : else if (!COMPLETE_TYPE_P (type)
4012 : 19388062 : && ((!c_dialect_cxx () && !flag_isoc2y)
4013 : : || is_sizeof
4014 : 144 : || type_code != ARRAY_TYPE))
4015 : : {
4016 : 271 : if (complain)
4017 : 194 : error_at (loc, "invalid application of %qs to incomplete type %qT",
4018 : : op_name, type);
4019 : 271 : return error_mark_node;
4020 : : }
4021 : 18707741 : else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4022 : 19843558 : && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4023 : : {
4024 : 0 : if (complain)
4025 : 0 : error_at (loc, "invalid application of %qs to array type %qT of "
4026 : : "incomplete element type", op_name, type);
4027 : 0 : return error_mark_node;
4028 : : }
4029 : 20332995 : else if (!verify_type_context (loc, is_sizeof ? TCTX_SIZEOF : TCTX_ALIGNOF,
4030 : : type, !complain))
4031 : : {
4032 : 0 : if (!complain)
4033 : 0 : return error_mark_node;
4034 : 0 : value = size_one_node;
4035 : : }
4036 : : else
4037 : : {
4038 : 19387791 : if (is_sizeof)
4039 : : /* Convert in case a char is more than one unit. */
4040 : 18442587 : value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4041 : 18442587 : size_int (TYPE_PRECISION (char_type_node)
4042 : : / BITS_PER_UNIT));
4043 : 945204 : else if (min_alignof)
4044 : 736956 : value = size_int (min_align_of_type (type));
4045 : : else
4046 : 208248 : value = size_int (TYPE_ALIGN_UNIT (type));
4047 : : }
4048 : :
4049 : : /* VALUE will have the middle-end integer type sizetype.
4050 : : However, we should really return a value of type `size_t',
4051 : : which is just a typedef for an ordinary integer type. */
4052 : 19387888 : value = fold_convert_loc (loc, size_type_node, value);
4053 : :
4054 : 19387888 : return value;
4055 : : }
4056 : :
4057 : : /* Implement the __alignof keyword: Return the minimum required
4058 : : alignment of EXPR, measured in bytes. For VAR_DECLs,
4059 : : FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4060 : : from an "aligned" __attribute__ specification). LOC is the
4061 : : location of the ALIGNOF operator. */
4062 : :
4063 : : tree
4064 : 40793 : c_alignof_expr (location_t loc, tree expr)
4065 : : {
4066 : 40793 : tree t;
4067 : :
4068 : 40793 : if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (expr)))
4069 : 0 : t = size_one_node;
4070 : :
4071 : 40793 : else if (VAR_OR_FUNCTION_DECL_P (expr))
4072 : 7245 : t = size_int (DECL_ALIGN_UNIT (expr));
4073 : :
4074 : 33548 : else if (TREE_CODE (expr) == COMPONENT_REF
4075 : 33548 : && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4076 : : {
4077 : 1 : error_at (loc, "%<__alignof%> applied to a bit-field");
4078 : 1 : t = size_one_node;
4079 : : }
4080 : 33547 : else if (TREE_CODE (expr) == COMPONENT_REF
4081 : 33547 : && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
4082 : 17569 : t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
4083 : :
4084 : 15978 : else if (INDIRECT_REF_P (expr))
4085 : : {
4086 : 57 : tree t = TREE_OPERAND (expr, 0);
4087 : 57 : tree best = t;
4088 : 57 : int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4089 : :
4090 : 57 : while (CONVERT_EXPR_P (t)
4091 : 57 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
4092 : : {
4093 : 0 : int thisalign;
4094 : :
4095 : 0 : t = TREE_OPERAND (t, 0);
4096 : 0 : thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4097 : 0 : if (thisalign > bestalign)
4098 : 0 : best = t, bestalign = thisalign;
4099 : : }
4100 : 57 : return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
4101 : : }
4102 : : else
4103 : 15921 : return c_alignof (loc, TREE_TYPE (expr));
4104 : :
4105 : 24815 : return fold_convert_loc (loc, size_type_node, t);
4106 : : }
4107 : :
4108 : : /* Implement the _Countof keyword:
4109 : : Return the number of elements of an array. */
4110 : :
4111 : : tree
4112 : 70 : c_countof_type (location_t loc, tree type)
4113 : : {
4114 : 70 : enum tree_code type_code;
4115 : :
4116 : 70 : type_code = TREE_CODE (type);
4117 : 70 : if (type_code != ARRAY_TYPE)
4118 : : {
4119 : 11 : error_at (loc, "invalid application of %<_Countof%> to type %qT", type);
4120 : 11 : return error_mark_node;
4121 : : }
4122 : 59 : if (!COMPLETE_TYPE_P (type))
4123 : : {
4124 : 2 : error_at (loc,
4125 : : "invalid application of %<_Countof%> to incomplete type %qT",
4126 : : type);
4127 : 2 : return error_mark_node;
4128 : : }
4129 : :
4130 : 57 : return array_type_nelts_top (type);
4131 : : }
4132 : :
4133 : : /* Handle C and C++ default attributes. */
4134 : :
4135 : : enum built_in_attribute
4136 : : {
4137 : : #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4138 : : #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
4139 : : #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
4140 : : #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4141 : : #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
4142 : : #include "builtin-attrs.def"
4143 : : #undef DEF_ATTR_NULL_TREE
4144 : : #undef DEF_ATTR_INT
4145 : : #undef DEF_ATTR_STRING
4146 : : #undef DEF_ATTR_IDENT
4147 : : #undef DEF_ATTR_TREE_LIST
4148 : : ATTR_LAST
4149 : : };
4150 : :
4151 : : static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4152 : :
4153 : : static void c_init_attributes (void);
4154 : :
4155 : : enum c_builtin_type
4156 : : {
4157 : : #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
4158 : : #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
4159 : : #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
4160 : : #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
4161 : : #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4162 : : #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4163 : : #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
4164 : : #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4165 : : ARG6) NAME,
4166 : : #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4167 : : ARG6, ARG7) NAME,
4168 : : #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4169 : : ARG6, ARG7, ARG8) NAME,
4170 : : #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4171 : : ARG6, ARG7, ARG8, ARG9) NAME,
4172 : : #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4173 : : ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
4174 : : #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4175 : : ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
4176 : : #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
4177 : : #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
4178 : : #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
4179 : : #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4180 : : #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4181 : : #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4182 : : NAME,
4183 : : #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4184 : : ARG6) NAME,
4185 : : #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4186 : : ARG6, ARG7) NAME,
4187 : : #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
4188 : : #include "builtin-types.def"
4189 : : #undef DEF_PRIMITIVE_TYPE
4190 : : #undef DEF_FUNCTION_TYPE_0
4191 : : #undef DEF_FUNCTION_TYPE_1
4192 : : #undef DEF_FUNCTION_TYPE_2
4193 : : #undef DEF_FUNCTION_TYPE_3
4194 : : #undef DEF_FUNCTION_TYPE_4
4195 : : #undef DEF_FUNCTION_TYPE_5
4196 : : #undef DEF_FUNCTION_TYPE_6
4197 : : #undef DEF_FUNCTION_TYPE_7
4198 : : #undef DEF_FUNCTION_TYPE_8
4199 : : #undef DEF_FUNCTION_TYPE_9
4200 : : #undef DEF_FUNCTION_TYPE_10
4201 : : #undef DEF_FUNCTION_TYPE_11
4202 : : #undef DEF_FUNCTION_TYPE_VAR_0
4203 : : #undef DEF_FUNCTION_TYPE_VAR_1
4204 : : #undef DEF_FUNCTION_TYPE_VAR_2
4205 : : #undef DEF_FUNCTION_TYPE_VAR_3
4206 : : #undef DEF_FUNCTION_TYPE_VAR_4
4207 : : #undef DEF_FUNCTION_TYPE_VAR_5
4208 : : #undef DEF_FUNCTION_TYPE_VAR_6
4209 : : #undef DEF_FUNCTION_TYPE_VAR_7
4210 : : #undef DEF_POINTER_TYPE
4211 : : BT_LAST
4212 : : };
4213 : :
4214 : : typedef enum c_builtin_type builtin_type;
4215 : :
4216 : : /* A temporary array for c_common_nodes_and_builtins. Used in
4217 : : communication with def_fn_type. */
4218 : : static tree builtin_types[(int) BT_LAST + 1];
4219 : :
4220 : : /* A helper function for c_common_nodes_and_builtins. Build function type
4221 : : for DEF with return type RET and N arguments. If VAR is true, then the
4222 : : function should be variadic after those N arguments, or, if N is zero,
4223 : : unprototyped.
4224 : :
4225 : : Takes special care not to ICE if any of the types involved are
4226 : : error_mark_node, which indicates that said type is not in fact available
4227 : : (see builtin_type_for_size). In which case the function type as a whole
4228 : : should be error_mark_node. */
4229 : :
4230 : : static void
4231 : 105607152 : def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
4232 : : {
4233 : 105607152 : tree t;
4234 : 105607152 : tree *args = XALLOCAVEC (tree, n);
4235 : 105607152 : va_list list;
4236 : 105607152 : int i;
4237 : :
4238 : 105607152 : va_start (list, n);
4239 : 334536174 : for (i = 0; i < n; ++i)
4240 : : {
4241 : 231910560 : builtin_type a = (builtin_type) va_arg (list, int);
4242 : 231910560 : t = builtin_types[a];
4243 : 231910560 : if (t == error_mark_node)
4244 : 2981538 : goto egress;
4245 : 228929022 : args[i] = t;
4246 : : }
4247 : :
4248 : 102625614 : t = builtin_types[ret];
4249 : 102625614 : if (t == error_mark_node)
4250 : 424410 : goto egress;
4251 : 102201204 : if (var)
4252 : 6280806 : if (n == 0)
4253 : 838152 : t = build_function_type (t, NULL_TREE);
4254 : : else
4255 : 5442654 : t = build_varargs_function_type_array (t, n, args);
4256 : : else
4257 : 95920398 : t = build_function_type_array (t, n, args);
4258 : :
4259 : 105607152 : egress:
4260 : 105607152 : builtin_types[def] = t;
4261 : 105607152 : va_end (list);
4262 : 105607152 : }
4263 : :
4264 : : /* Build builtin functions common to both C and C++ language
4265 : : frontends. */
4266 : :
4267 : : static void
4268 : 209538 : c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
4269 : : {
4270 : : #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
4271 : : builtin_types[ENUM] = VALUE;
4272 : : #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
4273 : : def_fn_type (ENUM, RETURN, 0, 0);
4274 : : #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
4275 : : def_fn_type (ENUM, RETURN, 0, 1, ARG1);
4276 : : #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
4277 : : def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
4278 : : #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4279 : : def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
4280 : : #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4281 : : def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
4282 : : #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4283 : : def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4284 : : #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4285 : : ARG6) \
4286 : : def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
4287 : : #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4288 : : ARG6, ARG7) \
4289 : : def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
4290 : : #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4291 : : ARG6, ARG7, ARG8) \
4292 : : def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
4293 : : ARG7, ARG8);
4294 : : #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4295 : : ARG6, ARG7, ARG8, ARG9) \
4296 : : def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
4297 : : ARG7, ARG8, ARG9);
4298 : : #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4299 : : ARG6, ARG7, ARG8, ARG9, ARG10) \
4300 : : def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
4301 : : ARG7, ARG8, ARG9, ARG10);
4302 : : #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4303 : : ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
4304 : : def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
4305 : : ARG7, ARG8, ARG9, ARG10, ARG11);
4306 : : #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
4307 : : def_fn_type (ENUM, RETURN, 1, 0);
4308 : : #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
4309 : : def_fn_type (ENUM, RETURN, 1, 1, ARG1);
4310 : : #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
4311 : : def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
4312 : : #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4313 : : def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
4314 : : #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4315 : : def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
4316 : : #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4317 : : def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4318 : : #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4319 : : ARG6) \
4320 : : def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
4321 : : #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4322 : : ARG6, ARG7) \
4323 : : def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
4324 : : #define DEF_POINTER_TYPE(ENUM, TYPE) \
4325 : : builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
4326 : :
4327 : : #include "builtin-types.def"
4328 : :
4329 : : #undef DEF_PRIMITIVE_TYPE
4330 : : #undef DEF_FUNCTION_TYPE_0
4331 : : #undef DEF_FUNCTION_TYPE_1
4332 : : #undef DEF_FUNCTION_TYPE_2
4333 : : #undef DEF_FUNCTION_TYPE_3
4334 : : #undef DEF_FUNCTION_TYPE_4
4335 : : #undef DEF_FUNCTION_TYPE_5
4336 : : #undef DEF_FUNCTION_TYPE_6
4337 : : #undef DEF_FUNCTION_TYPE_7
4338 : : #undef DEF_FUNCTION_TYPE_8
4339 : : #undef DEF_FUNCTION_TYPE_9
4340 : : #undef DEF_FUNCTION_TYPE_10
4341 : : #undef DEF_FUNCTION_TYPE_11
4342 : : #undef DEF_FUNCTION_TYPE_VAR_0
4343 : : #undef DEF_FUNCTION_TYPE_VAR_1
4344 : : #undef DEF_FUNCTION_TYPE_VAR_2
4345 : : #undef DEF_FUNCTION_TYPE_VAR_3
4346 : : #undef DEF_FUNCTION_TYPE_VAR_4
4347 : : #undef DEF_FUNCTION_TYPE_VAR_5
4348 : : #undef DEF_FUNCTION_TYPE_VAR_6
4349 : : #undef DEF_FUNCTION_TYPE_VAR_7
4350 : : #undef DEF_POINTER_TYPE
4351 : 209538 : builtin_types[(int) BT_LAST] = NULL_TREE;
4352 : :
4353 : 209538 : c_init_attributes ();
4354 : :
4355 : : #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
4356 : : NONANSI_P, ATTRS, IMPLICIT, COND) \
4357 : : if (NAME && COND) \
4358 : : def_builtin_1 (ENUM, NAME, CLASS, \
4359 : : builtin_types[(int) TYPE], \
4360 : : builtin_types[(int) LIBTYPE], \
4361 : : BOTH_P, FALLBACK_P, NONANSI_P, \
4362 : : built_in_attributes[(int) ATTRS], IMPLICIT);
4363 : : #include "builtins.def"
4364 : :
4365 : 209538 : targetm.init_builtins ();
4366 : :
4367 : 209538 : build_common_builtin_nodes ();
4368 : 209538 : }
4369 : :
4370 : : /* Like get_identifier, but avoid warnings about null arguments when
4371 : : the argument may be NULL for targets where GCC lacks stdint.h type
4372 : : information. */
4373 : :
4374 : : static inline tree
4375 : 5657526 : c_get_ident (const char *id)
4376 : : {
4377 : 2514456 : return get_identifier (id);
4378 : : }
4379 : :
4380 : : /* Build tree nodes and builtin functions common to both C and C++ language
4381 : : frontends. */
4382 : :
4383 : : void
4384 : 209538 : c_common_nodes_and_builtins (void)
4385 : : {
4386 : 209538 : int char8_type_size;
4387 : 209538 : int char16_type_size;
4388 : 209538 : int char32_type_size;
4389 : 209538 : int wchar_type_size;
4390 : 209538 : tree array_domain_type;
4391 : 209538 : tree va_list_ref_type_node;
4392 : 209538 : tree va_list_arg_type_node;
4393 : 209538 : int i;
4394 : :
4395 : 209538 : build_common_tree_nodes (flag_signed_char);
4396 : :
4397 : : /* Define `int' and `char' first so that dbx will output them first. */
4398 : 209538 : record_builtin_type (RID_INT, NULL, integer_type_node);
4399 : 209538 : record_builtin_type (RID_CHAR, "char", char_type_node);
4400 : :
4401 : : /* `signed' is the same as `int'. FIXME: the declarations of "signed",
4402 : : "unsigned long", "long long unsigned" and "unsigned short" were in C++
4403 : : but not C. Are the conditionals here needed? */
4404 : 209538 : if (c_dialect_cxx ())
4405 : 96471 : record_builtin_type (RID_SIGNED, NULL, integer_type_node);
4406 : 209538 : record_builtin_type (RID_LONG, "long int", long_integer_type_node);
4407 : 209538 : record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
4408 : 209538 : record_builtin_type (RID_MAX, "long unsigned int",
4409 : : long_unsigned_type_node);
4410 : :
4411 : 419076 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
4412 : : {
4413 : 209538 : char name[25];
4414 : :
4415 : 209538 : sprintf (name, "__int%d", int_n_data[i].bitsize);
4416 : 209538 : record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
4417 : : int_n_trees[i].signed_type);
4418 : 209538 : sprintf (name, "__int%d__", int_n_data[i].bitsize);
4419 : 209538 : record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
4420 : : int_n_trees[i].signed_type);
4421 : 419076 : ridpointers[RID_FIRST_INT_N + i]
4422 : 209538 : = DECL_NAME (TYPE_NAME (int_n_trees[i].signed_type));
4423 : :
4424 : 209538 : sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
4425 : 209538 : record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
4426 : 209538 : sprintf (name, "__int%d__ unsigned", int_n_data[i].bitsize);
4427 : 209538 : record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
4428 : : }
4429 : :
4430 : 209538 : if (c_dialect_cxx ())
4431 : 96471 : record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
4432 : 209538 : record_builtin_type (RID_MAX, "long long int",
4433 : : long_long_integer_type_node);
4434 : 209538 : record_builtin_type (RID_MAX, "long long unsigned int",
4435 : : long_long_unsigned_type_node);
4436 : 209538 : if (c_dialect_cxx ())
4437 : 96471 : record_builtin_type (RID_MAX, "long long unsigned",
4438 : : long_long_unsigned_type_node);
4439 : 209538 : record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
4440 : 209538 : record_builtin_type (RID_MAX, "short unsigned int",
4441 : : short_unsigned_type_node);
4442 : 209538 : if (c_dialect_cxx ())
4443 : 96471 : record_builtin_type (RID_MAX, "unsigned short",
4444 : : short_unsigned_type_node);
4445 : :
4446 : : /* Define both `signed char' and `unsigned char'. */
4447 : 209538 : record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
4448 : 209538 : record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
4449 : :
4450 : : /* These are types that c_common_type_for_size and
4451 : : c_common_type_for_mode use. */
4452 : 209538 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4453 : : TYPE_DECL, NULL_TREE,
4454 : : intQI_type_node));
4455 : 209538 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4456 : : TYPE_DECL, NULL_TREE,
4457 : : intHI_type_node));
4458 : 209538 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4459 : : TYPE_DECL, NULL_TREE,
4460 : : intSI_type_node));
4461 : 209538 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4462 : : TYPE_DECL, NULL_TREE,
4463 : : intDI_type_node));
4464 : : #if HOST_BITS_PER_WIDE_INT >= 64
4465 : : /* Note that this is different than the __int128 type that's part of
4466 : : the generic __intN support. */
4467 : 209538 : if (targetm.scalar_mode_supported_p (TImode))
4468 : 204204 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4469 : : TYPE_DECL,
4470 : : get_identifier ("__int128_t"),
4471 : : intTI_type_node));
4472 : : #endif
4473 : 209538 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4474 : : TYPE_DECL, NULL_TREE,
4475 : : unsigned_intQI_type_node));
4476 : 209538 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4477 : : TYPE_DECL, NULL_TREE,
4478 : : unsigned_intHI_type_node));
4479 : 209538 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4480 : : TYPE_DECL, NULL_TREE,
4481 : : unsigned_intSI_type_node));
4482 : 209538 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4483 : : TYPE_DECL, NULL_TREE,
4484 : : unsigned_intDI_type_node));
4485 : : #if HOST_BITS_PER_WIDE_INT >= 64
4486 : 209538 : if (targetm.scalar_mode_supported_p (TImode))
4487 : 204204 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4488 : : TYPE_DECL,
4489 : : get_identifier ("__uint128_t"),
4490 : : unsigned_intTI_type_node));
4491 : : #endif
4492 : :
4493 : : /* Create the widest literal types. */
4494 : 209538 : if (targetm.scalar_mode_supported_p (TImode))
4495 : : {
4496 : 204204 : widest_integer_literal_type_node = intTI_type_node;
4497 : 204204 : widest_unsigned_literal_type_node = unsigned_intTI_type_node;
4498 : : }
4499 : : else
4500 : : {
4501 : 5334 : widest_integer_literal_type_node = intDI_type_node;
4502 : 5334 : widest_unsigned_literal_type_node = unsigned_intDI_type_node;
4503 : : }
4504 : :
4505 : 209538 : signed_size_type_node = c_common_signed_type (size_type_node);
4506 : :
4507 : 419076 : pid_type_node =
4508 : 209538 : TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
4509 : :
4510 : 209538 : record_builtin_type (RID_FLOAT, NULL, float_type_node);
4511 : 209538 : record_builtin_type (RID_DOUBLE, NULL, double_type_node);
4512 : 209538 : record_builtin_type (RID_MAX, "long double", long_double_type_node);
4513 : :
4514 : 1885842 : for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4515 : : {
4516 : 1466766 : if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4517 : 1257228 : record_builtin_type ((enum rid) (RID_FLOATN_NX_FIRST + i), NULL,
4518 : : FLOATN_NX_TYPE_NODE (i));
4519 : : }
4520 : :
4521 : : /* For C, let float128t_type_node (__float128 in some backends) be the
4522 : : same type as float128_type_node (_Float128), for C++ let those
4523 : : be distinct types that mangle and behave differently. */
4524 : 209538 : if (c_dialect_cxx ())
4525 : 96471 : float128t_type_node = NULL_TREE;
4526 : :
4527 : : /* Only supported decimal floating point extension if the target
4528 : : actually supports underlying modes. */
4529 : 209538 : if (targetm.scalar_mode_supported_p (SDmode)
4530 : 209538 : && targetm.scalar_mode_supported_p (DDmode)
4531 : 419076 : && targetm.scalar_mode_supported_p (TDmode))
4532 : : {
4533 : 209538 : record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
4534 : 209538 : record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
4535 : 209538 : record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
4536 : 209538 : record_builtin_type (RID_DFLOAT64X, NULL, dfloat64x_type_node);
4537 : : }
4538 : :
4539 : 209538 : if (targetm.fixed_point_supported_p ())
4540 : : {
4541 : 0 : record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
4542 : 0 : record_builtin_type (RID_FRACT, NULL, fract_type_node);
4543 : 0 : record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
4544 : 0 : record_builtin_type (RID_MAX, "long long _Fract",
4545 : : long_long_fract_type_node);
4546 : 0 : record_builtin_type (RID_MAX, "unsigned short _Fract",
4547 : : unsigned_short_fract_type_node);
4548 : 0 : record_builtin_type (RID_MAX, "unsigned _Fract",
4549 : : unsigned_fract_type_node);
4550 : 0 : record_builtin_type (RID_MAX, "unsigned long _Fract",
4551 : : unsigned_long_fract_type_node);
4552 : 0 : record_builtin_type (RID_MAX, "unsigned long long _Fract",
4553 : : unsigned_long_long_fract_type_node);
4554 : 0 : record_builtin_type (RID_MAX, "_Sat short _Fract",
4555 : : sat_short_fract_type_node);
4556 : 0 : record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
4557 : 0 : record_builtin_type (RID_MAX, "_Sat long _Fract",
4558 : : sat_long_fract_type_node);
4559 : 0 : record_builtin_type (RID_MAX, "_Sat long long _Fract",
4560 : : sat_long_long_fract_type_node);
4561 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
4562 : : sat_unsigned_short_fract_type_node);
4563 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
4564 : : sat_unsigned_fract_type_node);
4565 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
4566 : : sat_unsigned_long_fract_type_node);
4567 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
4568 : : sat_unsigned_long_long_fract_type_node);
4569 : 0 : record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
4570 : 0 : record_builtin_type (RID_ACCUM, NULL, accum_type_node);
4571 : 0 : record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
4572 : 0 : record_builtin_type (RID_MAX, "long long _Accum",
4573 : : long_long_accum_type_node);
4574 : 0 : record_builtin_type (RID_MAX, "unsigned short _Accum",
4575 : : unsigned_short_accum_type_node);
4576 : 0 : record_builtin_type (RID_MAX, "unsigned _Accum",
4577 : : unsigned_accum_type_node);
4578 : 0 : record_builtin_type (RID_MAX, "unsigned long _Accum",
4579 : : unsigned_long_accum_type_node);
4580 : 0 : record_builtin_type (RID_MAX, "unsigned long long _Accum",
4581 : : unsigned_long_long_accum_type_node);
4582 : 0 : record_builtin_type (RID_MAX, "_Sat short _Accum",
4583 : : sat_short_accum_type_node);
4584 : 0 : record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
4585 : 0 : record_builtin_type (RID_MAX, "_Sat long _Accum",
4586 : : sat_long_accum_type_node);
4587 : 0 : record_builtin_type (RID_MAX, "_Sat long long _Accum",
4588 : : sat_long_long_accum_type_node);
4589 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
4590 : : sat_unsigned_short_accum_type_node);
4591 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
4592 : : sat_unsigned_accum_type_node);
4593 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
4594 : : sat_unsigned_long_accum_type_node);
4595 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
4596 : : sat_unsigned_long_long_accum_type_node);
4597 : :
4598 : : }
4599 : :
4600 : 209538 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4601 : : TYPE_DECL,
4602 : : get_identifier ("complex int"),
4603 : : complex_integer_type_node));
4604 : 209538 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4605 : : TYPE_DECL,
4606 : : get_identifier ("complex float"),
4607 : : complex_float_type_node));
4608 : 209538 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4609 : : TYPE_DECL,
4610 : : get_identifier ("complex double"),
4611 : : complex_double_type_node));
4612 : 209538 : lang_hooks.decls.pushdecl
4613 : 209538 : (build_decl (UNKNOWN_LOCATION,
4614 : : TYPE_DECL, get_identifier ("complex long double"),
4615 : : complex_long_double_type_node));
4616 : :
4617 : 209538 : if (!c_dialect_cxx ())
4618 : 904536 : for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4619 : 791469 : if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4620 : : {
4621 : 678402 : char buf[30];
4622 : 1356804 : sprintf (buf, "complex _Float%d%s", floatn_nx_types[i].n,
4623 : 678402 : floatn_nx_types[i].extended ? "x" : "");
4624 : 678402 : lang_hooks.decls.pushdecl
4625 : 678402 : (build_decl (UNKNOWN_LOCATION,
4626 : : TYPE_DECL,
4627 : : get_identifier (buf),
4628 : : COMPLEX_FLOATN_NX_TYPE_NODE (i)));
4629 : : }
4630 : :
4631 : : /* Make fileptr_type_node a distinct void * type until
4632 : : FILE type is defined. Likewise for const struct tm*. */
4633 : 1466766 : for (unsigned i = 0; i < ARRAY_SIZE (builtin_structptr_types); ++i)
4634 : 1257228 : builtin_structptr_types[i].node
4635 : 1257228 : = build_variant_type_copy (builtin_structptr_types[i].base);
4636 : :
4637 : 209538 : record_builtin_type (RID_VOID, NULL, void_type_node);
4638 : :
4639 : : /* Set the TYPE_NAME for any variants that were built before
4640 : : record_builtin_type gave names to the built-in types. */
4641 : 209538 : {
4642 : 209538 : tree void_name = TYPE_NAME (void_type_node);
4643 : 209538 : TYPE_NAME (void_type_node) = NULL_TREE;
4644 : 209538 : TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
4645 : 209538 : = void_name;
4646 : 209538 : TYPE_NAME (void_type_node) = void_name;
4647 : : }
4648 : :
4649 : : /* Make a type to be the domain of a few array types
4650 : : whose domains don't really matter.
4651 : : 200 is small enough that it always fits in size_t
4652 : : and large enough that it can hold most function names for the
4653 : : initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4654 : 209538 : array_domain_type = build_index_type (size_int (200));
4655 : :
4656 : : /* Make a type for arrays of characters.
4657 : : With luck nothing will ever really depend on the length of this
4658 : : array type. */
4659 : 209538 : char_array_type_node
4660 : 209538 : = build_array_type (char_type_node, array_domain_type);
4661 : :
4662 : 209538 : string_type_node = build_pointer_type (char_type_node);
4663 : 209538 : const_string_type_node
4664 : 209538 : = build_pointer_type (build_qualified_type
4665 : : (char_type_node, TYPE_QUAL_CONST));
4666 : :
4667 : : /* This is special for C++ so functions can be overloaded. */
4668 : 214972 : wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
4669 : 209538 : wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
4670 : 209538 : wchar_type_size = TYPE_PRECISION (wchar_type_node);
4671 : 209538 : underlying_wchar_type_node = wchar_type_node;
4672 : 209538 : if (c_dialect_cxx ())
4673 : : {
4674 : 96471 : if (TYPE_UNSIGNED (wchar_type_node))
4675 : 7 : wchar_type_node = make_unsigned_type (wchar_type_size);
4676 : : else
4677 : 96464 : wchar_type_node = make_signed_type (wchar_type_size);
4678 : 96471 : record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
4679 : : }
4680 : :
4681 : : /* This is for wide string constants. */
4682 : 209538 : wchar_array_type_node
4683 : 209538 : = build_array_type (wchar_type_node, array_domain_type);
4684 : :
4685 : : /* Define 'char8_t'. */
4686 : 209538 : char8_type_node = get_identifier (CHAR8_TYPE);
4687 : 209538 : char8_type_node = TREE_TYPE (identifier_global_value (char8_type_node));
4688 : 209538 : char8_type_size = TYPE_PRECISION (char8_type_node);
4689 : 209538 : if (c_dialect_cxx ())
4690 : : {
4691 : 96471 : char8_type_node = make_unsigned_type (char8_type_size);
4692 : 96471 : TYPE_STRING_FLAG (char8_type_node) = true;
4693 : :
4694 : 96471 : if (flag_char8_t)
4695 : 30604 : record_builtin_type (RID_CHAR8, "char8_t", char8_type_node);
4696 : : }
4697 : :
4698 : : /* This is for UTF-8 string constants. */
4699 : 209538 : char8_array_type_node
4700 : 209538 : = build_array_type (char8_type_node, array_domain_type);
4701 : :
4702 : : /* Define 'char16_t'. */
4703 : 209538 : char16_type_node = get_identifier (CHAR16_TYPE);
4704 : 209538 : char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
4705 : 209538 : char16_type_size = TYPE_PRECISION (char16_type_node);
4706 : 209538 : if (c_dialect_cxx ())
4707 : : {
4708 : 96471 : char16_type_node = make_unsigned_type (char16_type_size);
4709 : :
4710 : 96471 : if (cxx_dialect >= cxx11)
4711 : 82482 : record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
4712 : : }
4713 : :
4714 : : /* This is for UTF-16 string constants. */
4715 : 209538 : char16_array_type_node
4716 : 209538 : = build_array_type (char16_type_node, array_domain_type);
4717 : :
4718 : : /* Define 'char32_t'. */
4719 : 209538 : char32_type_node = get_identifier (CHAR32_TYPE);
4720 : 209538 : char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
4721 : 209538 : char32_type_size = TYPE_PRECISION (char32_type_node);
4722 : 209538 : if (c_dialect_cxx ())
4723 : : {
4724 : 96471 : char32_type_node = make_unsigned_type (char32_type_size);
4725 : :
4726 : 96471 : if (cxx_dialect >= cxx11)
4727 : 82482 : record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
4728 : : }
4729 : :
4730 : : /* This is for UTF-32 string constants. */
4731 : 209538 : char32_array_type_node
4732 : 209538 : = build_array_type (char32_type_node, array_domain_type);
4733 : :
4734 : 209538 : if (strcmp (WINT_TYPE, "wchar_t") == 0)
4735 : : wint_type_node = wchar_type_node;
4736 : : else
4737 : 419076 : wint_type_node =
4738 : 209538 : TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
4739 : :
4740 : 419076 : intmax_type_node =
4741 : 214972 : TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
4742 : 419076 : uintmax_type_node =
4743 : 214972 : TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
4744 : :
4745 : 209538 : if (SIG_ATOMIC_TYPE)
4746 : 209538 : sig_atomic_type_node =
4747 : 209538 : TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
4748 : 209538 : if (INT8_TYPE)
4749 : 209538 : int8_type_node =
4750 : 209538 : TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
4751 : 209538 : if (INT16_TYPE)
4752 : 209538 : int16_type_node =
4753 : 209538 : TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
4754 : 209538 : if (INT32_TYPE)
4755 : 209538 : int32_type_node =
4756 : 209538 : TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
4757 : 209538 : if (INT64_TYPE)
4758 : 419076 : int64_type_node =
4759 : 419076 : TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
4760 : 209538 : if (UINT8_TYPE)
4761 : 209538 : uint8_type_node =
4762 : 209538 : TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
4763 : 209538 : if (UINT16_TYPE)
4764 : 209538 : c_uint16_type_node = uint16_type_node =
4765 : 209538 : TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
4766 : 209538 : if (UINT32_TYPE)
4767 : 209538 : c_uint32_type_node = uint32_type_node =
4768 : 209538 : TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
4769 : 209538 : if (UINT64_TYPE)
4770 : 419076 : c_uint64_type_node = uint64_type_node =
4771 : 419076 : TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
4772 : 209538 : if (INT_LEAST8_TYPE)
4773 : 209538 : int_least8_type_node =
4774 : 209538 : TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
4775 : 209538 : if (INT_LEAST16_TYPE)
4776 : 209538 : int_least16_type_node =
4777 : 209538 : TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
4778 : 209538 : if (INT_LEAST32_TYPE)
4779 : 209538 : int_least32_type_node =
4780 : 209538 : TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
4781 : 209538 : if (INT_LEAST64_TYPE)
4782 : 419076 : int_least64_type_node =
4783 : 419076 : TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
4784 : 209538 : if (UINT_LEAST8_TYPE)
4785 : 209538 : uint_least8_type_node =
4786 : 209538 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
4787 : 209538 : if (UINT_LEAST16_TYPE)
4788 : 209538 : uint_least16_type_node =
4789 : 209538 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
4790 : 209538 : if (UINT_LEAST32_TYPE)
4791 : 209538 : uint_least32_type_node =
4792 : 209538 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
4793 : 209538 : if (UINT_LEAST64_TYPE)
4794 : 419076 : uint_least64_type_node =
4795 : 419076 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
4796 : 209538 : if (INT_FAST8_TYPE)
4797 : 209538 : int_fast8_type_node =
4798 : 209538 : TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
4799 : 209538 : if (INT_FAST16_TYPE)
4800 : 419076 : int_fast16_type_node =
4801 : 419076 : TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
4802 : 209538 : if (INT_FAST32_TYPE)
4803 : 419076 : int_fast32_type_node =
4804 : 419076 : TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
4805 : 209538 : if (INT_FAST64_TYPE)
4806 : 419076 : int_fast64_type_node =
4807 : 419076 : TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
4808 : 209538 : if (UINT_FAST8_TYPE)
4809 : 209538 : uint_fast8_type_node =
4810 : 209538 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
4811 : 209538 : if (UINT_FAST16_TYPE)
4812 : 419076 : uint_fast16_type_node =
4813 : 419076 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
4814 : 209538 : if (UINT_FAST32_TYPE)
4815 : 419076 : uint_fast32_type_node =
4816 : 419076 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
4817 : 209538 : if (UINT_FAST64_TYPE)
4818 : 419076 : uint_fast64_type_node =
4819 : 419076 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
4820 : 209538 : if (INTPTR_TYPE)
4821 : 419076 : intptr_type_node =
4822 : 419076 : TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
4823 : 209538 : if (UINTPTR_TYPE)
4824 : 419076 : uintptr_type_node =
4825 : 419076 : TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
4826 : :
4827 : 209538 : default_function_type = build_function_type (integer_type_node, NULL_TREE);
4828 : 209538 : unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
4829 : :
4830 : 209538 : lang_hooks.decls.pushdecl
4831 : 209538 : (build_decl (UNKNOWN_LOCATION,
4832 : : TYPE_DECL, get_identifier ("__builtin_va_list"),
4833 : : va_list_type_node));
4834 : 209538 : if (targetm.enum_va_list_p)
4835 : : {
4836 : : int l;
4837 : : const char *pname;
4838 : : tree ptype;
4839 : :
4840 : 617946 : for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
4841 : : {
4842 : 408408 : lang_hooks.decls.pushdecl
4843 : 408408 : (build_decl (UNKNOWN_LOCATION,
4844 : : TYPE_DECL, get_identifier (pname),
4845 : : ptype));
4846 : :
4847 : : }
4848 : : }
4849 : :
4850 : 209538 : if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4851 : : {
4852 : 408324 : va_list_arg_type_node = va_list_ref_type_node =
4853 : 204162 : build_pointer_type (TREE_TYPE (va_list_type_node));
4854 : : }
4855 : : else
4856 : : {
4857 : 5376 : va_list_arg_type_node = va_list_type_node;
4858 : 5376 : va_list_ref_type_node = build_reference_type (va_list_type_node);
4859 : : }
4860 : :
4861 : 209538 : c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
4862 : :
4863 : 209538 : main_identifier_node = get_identifier ("main");
4864 : :
4865 : : /* Create the built-in __null node. It is important that this is
4866 : : not shared. */
4867 : 209538 : null_node = make_int_cst (1, 1);
4868 : 214972 : TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
4869 : :
4870 : : /* Create the built-in nullptr node. This part of its initialization is
4871 : : common to C and C++. The front ends can further adjust its definition
4872 : : in {c,cxx}_init_decl_processing. In particular, we aren't setting the
4873 : : alignment here for C++ backward ABI bug compatibility. */
4874 : 209538 : nullptr_type_node = make_node (NULLPTR_TYPE);
4875 : 419076 : TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4876 : 419076 : TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4877 : 209538 : TYPE_UNSIGNED (nullptr_type_node) = 1;
4878 : 419076 : TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4879 : 209538 : SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4880 : 209538 : nullptr_node = build_int_cst (nullptr_type_node, 0);
4881 : :
4882 : : /* Since builtin_types isn't gc'ed, don't export these nodes. */
4883 : 209538 : memset (builtin_types, 0, sizeof (builtin_types));
4884 : 209538 : }
4885 : :
4886 : : /* The number of named compound-literals generated thus far. */
4887 : : static GTY(()) int compound_literal_number;
4888 : :
4889 : : /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
4890 : :
4891 : : void
4892 : 277 : set_compound_literal_name (tree decl)
4893 : : {
4894 : 277 : char *name;
4895 : 277 : ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
4896 : : compound_literal_number);
4897 : 277 : compound_literal_number++;
4898 : 277 : DECL_NAME (decl) = get_identifier (name);
4899 : 277 : }
4900 : :
4901 : : /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
4902 : : TYPE and operand OP. */
4903 : :
4904 : : static tree
4905 : 50942 : build_va_arg_1 (location_t loc, tree type, tree op)
4906 : : {
4907 : 50942 : tree expr = build1 (VA_ARG_EXPR, type, op);
4908 : 50942 : SET_EXPR_LOCATION (expr, loc);
4909 : 50942 : return expr;
4910 : : }
4911 : :
4912 : : /* Return a VA_ARG_EXPR corresponding to a source-level expression
4913 : : va_arg (EXPR, TYPE) at source location LOC. */
4914 : :
4915 : : tree
4916 : 50942 : build_va_arg (location_t loc, tree expr, tree type)
4917 : : {
4918 : 50942 : tree va_type = TREE_TYPE (expr);
4919 : 50942 : tree canon_va_type = (va_type == error_mark_node
4920 : 50942 : ? error_mark_node
4921 : 50932 : : targetm.canonical_va_list_type (va_type));
4922 : :
4923 : 50942 : if (va_type == error_mark_node
4924 : 50932 : || canon_va_type == NULL_TREE)
4925 : : {
4926 : 10 : if (canon_va_type == NULL_TREE)
4927 : 12 : error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4928 : :
4929 : : /* Let's handle things neutrally, if expr:
4930 : : - has undeclared type, or
4931 : : - is not an va_list type. */
4932 : 22 : return build_va_arg_1 (loc, type, error_mark_node);
4933 : : }
4934 : :
4935 : 50920 : if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
4936 : : {
4937 : : /* Case 1: Not an array type. */
4938 : :
4939 : : /* Take the address, to get '&ap'. Note that &ap is not a va_list
4940 : : type. */
4941 : 258 : c_common_mark_addressable_vec (expr);
4942 : 258 : expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
4943 : :
4944 : 258 : return build_va_arg_1 (loc, type, expr);
4945 : : }
4946 : :
4947 : : /* Case 2: Array type.
4948 : :
4949 : : Background:
4950 : :
4951 : : For contrast, let's start with the simple case (case 1). If
4952 : : canon_va_type is not an array type, but say a char *, then when
4953 : : passing-by-value a va_list, the type of the va_list param decl is
4954 : : the same as for another va_list decl (all ap's are char *):
4955 : :
4956 : : f2_1 (char * ap)
4957 : : D.1815 = VA_ARG (&ap, 0B, 1);
4958 : : return D.1815;
4959 : :
4960 : : f2 (int i)
4961 : : char * ap.0;
4962 : : char * ap;
4963 : : __builtin_va_start (&ap, 0);
4964 : : ap.0 = ap;
4965 : : res = f2_1 (ap.0);
4966 : : __builtin_va_end (&ap);
4967 : : D.1812 = res;
4968 : : return D.1812;
4969 : :
4970 : : However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
4971 : : va_list the type of the va_list param decl (case 2b, struct * ap) is not
4972 : : the same as for another va_list decl (case 2a, struct ap[1]).
4973 : :
4974 : : f2_1 (struct * ap)
4975 : : D.1844 = VA_ARG (ap, 0B, 0);
4976 : : return D.1844;
4977 : :
4978 : : f2 (int i)
4979 : : struct ap[1];
4980 : : __builtin_va_start (&ap, 0);
4981 : : res = f2_1 (&ap);
4982 : : __builtin_va_end (&ap);
4983 : : D.1841 = res;
4984 : : return D.1841;
4985 : :
4986 : : Case 2b is different because:
4987 : : - on the callee side, the parm decl has declared type va_list, but
4988 : : grokdeclarator changes the type of the parm decl to a pointer to the
4989 : : array elem type.
4990 : : - on the caller side, the pass-by-value uses &ap.
4991 : :
4992 : : We unify these two cases (case 2a: va_list is array type,
4993 : : case 2b: va_list is pointer to array elem type), by adding '&' for the
4994 : : array type case, such that we have a pointer to array elem in both
4995 : : cases. */
4996 : :
4997 : 50662 : if (TREE_CODE (va_type) == ARRAY_TYPE)
4998 : : {
4999 : : /* Case 2a: va_list is array type. */
5000 : :
5001 : : /* Take the address, to get '&ap'. Make sure it's a pointer to array
5002 : : elem type. */
5003 : 49915 : c_common_mark_addressable_vec (expr);
5004 : 49915 : expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
5005 : : expr);
5006 : :
5007 : : /* Verify that &ap is still recognized as having va_list type. */
5008 : 49915 : tree canon_expr_type
5009 : 49915 : = targetm.canonical_va_list_type (TREE_TYPE (expr));
5010 : 49915 : gcc_assert (canon_expr_type != NULL_TREE);
5011 : : }
5012 : : else
5013 : : {
5014 : : /* Case 2b: va_list is pointer to array elem type. */
5015 : 747 : gcc_assert (POINTER_TYPE_P (va_type));
5016 : :
5017 : : /* Comparison as in std_canonical_va_list_type. */
5018 : 747 : gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (va_type))
5019 : : == TYPE_MAIN_VARIANT (TREE_TYPE (canon_va_type)));
5020 : :
5021 : : /* Don't take the address. We've already got '&ap'. */
5022 : 50662 : ;
5023 : : }
5024 : :
5025 : 50662 : return build_va_arg_1 (loc, type, expr);
5026 : : }
5027 : :
5028 : :
5029 : : /* Linked list of disabled built-in functions. */
5030 : :
5031 : : struct disabled_builtin
5032 : : {
5033 : : const char *name;
5034 : : struct disabled_builtin *next;
5035 : : };
5036 : : static disabled_builtin *disabled_builtins = NULL;
5037 : :
5038 : : static bool builtin_function_disabled_p (const char *);
5039 : :
5040 : : /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5041 : : begins with "__builtin_", give an error. */
5042 : :
5043 : : void
5044 : 308 : disable_builtin_function (const char *name)
5045 : : {
5046 : 308 : if (startswith (name, "__builtin_"))
5047 : 0 : error ("cannot disable built-in function %qs", name);
5048 : : else
5049 : : {
5050 : 308 : disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5051 : 308 : new_disabled_builtin->name = name;
5052 : 308 : new_disabled_builtin->next = disabled_builtins;
5053 : 308 : disabled_builtins = new_disabled_builtin;
5054 : : }
5055 : 308 : }
5056 : :
5057 : :
5058 : : /* Return true if the built-in function NAME has been disabled, false
5059 : : otherwise. */
5060 : :
5061 : : static bool
5062 : 161573039 : builtin_function_disabled_p (const char *name)
5063 : : {
5064 : 161573039 : disabled_builtin *p;
5065 : 161857499 : for (p = disabled_builtins; p != NULL; p = p->next)
5066 : : {
5067 : 284768 : if (strcmp (name, p->name) == 0)
5068 : : return true;
5069 : : }
5070 : : return false;
5071 : : }
5072 : :
5073 : :
5074 : : /* Worker for DEF_BUILTIN.
5075 : : Possibly define a builtin function with one or two names.
5076 : : Does not declare a non-__builtin_ function if flag_no_builtin, or if
5077 : : nonansi_p and flag_no_nonansi_builtin. */
5078 : :
5079 : : static void
5080 : 316379505 : def_builtin_1 (enum built_in_function fncode,
5081 : : const char *name,
5082 : : enum built_in_class fnclass,
5083 : : tree fntype, tree libtype,
5084 : : bool both_p, bool fallback_p, bool nonansi_p,
5085 : : tree fnattrs, bool implicit_p)
5086 : : {
5087 : 316379505 : tree decl;
5088 : 316379505 : const char *libname;
5089 : :
5090 : 316379505 : if (fntype == error_mark_node)
5091 : : return;
5092 : :
5093 : 298183215 : gcc_assert ((!both_p && !fallback_p)
5094 : : || startswith (name, "__builtin_"));
5095 : :
5096 : 298183215 : libname = name + strlen ("__builtin_");
5097 : 298183215 : decl = add_builtin_function (name, fntype, fncode, fnclass,
5098 : : (fallback_p ? libname : NULL),
5099 : : fnattrs);
5100 : :
5101 : 298183215 : set_builtin_decl (fncode, decl, implicit_p);
5102 : :
5103 : 298183215 : if (both_p
5104 : 161990865 : && !flag_no_builtin && !builtin_function_disabled_p (libname)
5105 : 459755946 : && !(nonansi_p && flag_no_nonansi_builtin))
5106 : 145688968 : add_builtin_function (libname, libtype, fncode, fnclass,
5107 : : NULL, fnattrs);
5108 : : }
5109 : :
5110 : : /* Nonzero if the type T promotes to int. This is (nearly) the
5111 : : integral promotions defined in ISO C99 6.3.1.1/2. */
5112 : :
5113 : : bool
5114 : 499898097 : c_promoting_integer_type_p (const_tree t)
5115 : : {
5116 : 499898097 : switch (TREE_CODE (t))
5117 : : {
5118 : 389049965 : case INTEGER_TYPE:
5119 : 389049965 : return (TYPE_MAIN_VARIANT (t) == char_type_node
5120 : 383444973 : || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5121 : 383056163 : || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5122 : 368661763 : || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5123 : 367632903 : || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5124 : 751270963 : || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5125 : :
5126 : 658047 : case ENUMERAL_TYPE:
5127 : : /* ??? Technically all enumerations not larger than an int
5128 : : promote to an int. But this is used along code paths
5129 : : that only want to notice a size change. */
5130 : 658047 : return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5131 : :
5132 : : case BOOLEAN_TYPE:
5133 : : return true;
5134 : :
5135 : : default:
5136 : : return false;
5137 : : }
5138 : : }
5139 : :
5140 : : /* Return 1 if PARMS specifies a fixed number of parameters
5141 : : and none of their types is affected by default promotions. */
5142 : :
5143 : : bool
5144 : 21751 : self_promoting_args_p (const_tree parms)
5145 : : {
5146 : 21751 : const_tree t;
5147 : 64755 : for (t = parms; t; t = TREE_CHAIN (t))
5148 : : {
5149 : 43535 : tree type = TREE_VALUE (t);
5150 : :
5151 : 43535 : if (type == error_mark_node)
5152 : 2 : continue;
5153 : :
5154 : 43533 : if (TREE_CHAIN (t) == NULL_TREE && type != void_type_node)
5155 : : return false;
5156 : :
5157 : 43052 : if (type == NULL_TREE)
5158 : : return false;
5159 : :
5160 : 43052 : if (TYPE_MAIN_VARIANT (type) == float_type_node)
5161 : : return false;
5162 : :
5163 : 43007 : if (c_promoting_integer_type_p (type))
5164 : : return false;
5165 : : }
5166 : : return true;
5167 : : }
5168 : :
5169 : : /* Recursively remove any '*' or '&' operator from TYPE. */
5170 : : tree
5171 : 36652237 : strip_pointer_operator (tree t)
5172 : : {
5173 : 38912682 : while (POINTER_TYPE_P (t))
5174 : 2260445 : t = TREE_TYPE (t);
5175 : 36652237 : return t;
5176 : : }
5177 : :
5178 : : /* Recursively remove pointer or array type from TYPE. */
5179 : : tree
5180 : 10548771 : strip_pointer_or_array_types (tree t)
5181 : : {
5182 : 15290521 : while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5183 : 4741750 : t = TREE_TYPE (t);
5184 : 10548771 : return t;
5185 : : }
5186 : :
5187 : : /* Used to compare case labels. K1 and K2 are actually tree nodes
5188 : : representing case labels, or NULL_TREE for a `default' label.
5189 : : Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5190 : : K2, and 0 if K1 and K2 are equal. */
5191 : :
5192 : : int
5193 : 27725162 : case_compare (splay_tree_key k1, splay_tree_key k2)
5194 : : {
5195 : : /* Consider a NULL key (such as arises with a `default' label) to be
5196 : : smaller than anything else. */
5197 : 27725162 : if (!k1)
5198 : 3756430 : return k2 ? -1 : 0;
5199 : 24644528 : else if (!k2)
5200 : : return k1 ? 1 : 0;
5201 : :
5202 : 23832582 : return tree_int_cst_compare ((tree) k1, (tree) k2);
5203 : : }
5204 : :
5205 : : /* Process a case label, located at LOC, for the range LOW_VALUE
5206 : : ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5207 : : then this case label is actually a `default' label. If only
5208 : : HIGH_VALUE is NULL_TREE, then case label was declared using the
5209 : : usual C/C++ syntax, rather than the GNU case range extension.
5210 : : CASES is a tree containing all the case ranges processed so far;
5211 : : COND is the condition for the switch-statement itself.
5212 : : Returns the CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no
5213 : : CASE_LABEL_EXPR is created. ATTRS are the attributes to be applied
5214 : : to the label. */
5215 : :
5216 : : tree
5217 : 3029890 : c_add_case_label (location_t loc, splay_tree cases, tree cond,
5218 : : tree low_value, tree high_value, tree attrs)
5219 : : {
5220 : 3029890 : tree type;
5221 : 3029890 : tree label;
5222 : 3029890 : tree case_label;
5223 : 3029890 : splay_tree_node node;
5224 : :
5225 : : /* Create the LABEL_DECL itself. */
5226 : 3029890 : label = create_artificial_label (loc);
5227 : 3029890 : decl_attributes (&label, attrs, 0);
5228 : :
5229 : : /* If there was an error processing the switch condition, bail now
5230 : : before we get more confused. */
5231 : 3029890 : if (!cond || cond == error_mark_node)
5232 : 0 : goto error_out;
5233 : :
5234 : 2724772 : if ((low_value && TREE_TYPE (low_value)
5235 : 2724772 : && POINTER_TYPE_P (TREE_TYPE (low_value)))
5236 : 5754661 : || (high_value && TREE_TYPE (high_value)
5237 : 440 : && POINTER_TYPE_P (TREE_TYPE (high_value))))
5238 : : {
5239 : 1 : error_at (loc, "pointers are not permitted as case values");
5240 : 1 : goto error_out;
5241 : : }
5242 : :
5243 : : /* Case ranges are a GNU extension. */
5244 : 3029889 : if (high_value)
5245 : : {
5246 : 440 : if (c_dialect_cxx ())
5247 : 126 : pedwarn (loc, OPT_Wpedantic,
5248 : : "range expressions in switch statements are non-standard");
5249 : 314 : else if (warn_c23_c2y_compat > 0)
5250 : : {
5251 : 13 : if (pedantic && !flag_isoc2y)
5252 : 0 : pedwarn (loc, OPT_Wc23_c2y_compat,
5253 : : "ISO C does not support range expressions in switch "
5254 : : "statements before C2Y");
5255 : : else
5256 : 13 : warning_at (loc, OPT_Wc23_c2y_compat,
5257 : : "ISO C does not support range expressions in switch "
5258 : : "statements before C2Y");
5259 : : }
5260 : 301 : else if (warn_c23_c2y_compat && pedantic && !flag_isoc2y)
5261 : 28 : pedwarn (loc, OPT_Wpedantic,
5262 : : "ISO C does not support range expressions in switch "
5263 : : "statements before C2Y");
5264 : : }
5265 : :
5266 : 3029889 : type = TREE_TYPE (cond);
5267 : 3029889 : if (low_value)
5268 : : {
5269 : 2724771 : low_value = check_case_value (loc, low_value);
5270 : 2724771 : tree tem = NULL_TREE;
5271 : 2724771 : if (high_value
5272 : 440 : && !c_dialect_cxx ()
5273 : 314 : && low_value != error_mark_node
5274 : 313 : && !int_fits_type_p (low_value, type)
5275 : 2725211 : && pedwarn (loc, OPT_Wpedantic,
5276 : : "conversion of %qE to %qT in range expression changes "
5277 : : "value to %qE", low_value, type,
5278 : 20 : (tem = fold_convert (type, low_value))))
5279 : 20 : low_value = tem;
5280 : 2724771 : low_value = convert_and_check (loc, type, low_value);
5281 : 2724771 : low_value = fold (low_value);
5282 : 2724771 : if (low_value == error_mark_node)
5283 : 147 : goto error_out;
5284 : : }
5285 : 3029742 : if (high_value)
5286 : : {
5287 : 439 : high_value = check_case_value (loc, high_value);
5288 : 439 : tree tem = NULL_TREE;
5289 : 439 : if (!c_dialect_cxx ()
5290 : 313 : && high_value != error_mark_node
5291 : 312 : && !int_fits_type_p (high_value, type)
5292 : 752 : && pedwarn (loc, OPT_Wpedantic,
5293 : : "conversion of %qE to %qT in range expression changes "
5294 : : "value to %qE", high_value, type,
5295 : 20 : (tem = fold_convert (type, high_value))))
5296 : 20 : high_value = tem;
5297 : 439 : high_value = convert_and_check (loc, type, high_value);
5298 : 439 : high_value = fold (high_value);
5299 : 439 : if (high_value == error_mark_node)
5300 : 4 : goto error_out;
5301 : : }
5302 : :
5303 : 3029738 : if (low_value && high_value)
5304 : : {
5305 : : /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5306 : : really a case range, even though it was written that way.
5307 : : Remove the HIGH_VALUE to simplify later processing. */
5308 : 435 : if (tree_int_cst_equal (low_value, high_value))
5309 : : high_value = NULL_TREE;
5310 : 416 : else if (!tree_int_cst_lt (low_value, high_value))
5311 : : {
5312 : 28 : warning_at (loc, 0, "empty range specified");
5313 : 28 : goto error_out;
5314 : : }
5315 : : }
5316 : :
5317 : : /* Look up the LOW_VALUE in the table of case labels we already
5318 : : have. */
5319 : 3029710 : node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5320 : : /* If there was not an exact match, check for overlapping ranges.
5321 : : There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5322 : : that's a `default' label and the only overlap is an exact match. */
5323 : 3029710 : if (!node && (low_value || high_value))
5324 : : {
5325 : 2724574 : splay_tree_node low_bound;
5326 : 2724574 : splay_tree_node high_bound;
5327 : :
5328 : : /* Even though there wasn't an exact match, there might be an
5329 : : overlap between this case range and another case range.
5330 : : Since we've (inductively) not allowed any overlapping case
5331 : : ranges, we simply need to find the greatest low case label
5332 : : that is smaller that LOW_VALUE, and the smallest low case
5333 : : label that is greater than LOW_VALUE. If there is an overlap
5334 : : it will occur in one of these two ranges. */
5335 : 2724574 : low_bound = splay_tree_predecessor (cases,
5336 : : (splay_tree_key) low_value);
5337 : 2724574 : high_bound = splay_tree_successor (cases,
5338 : : (splay_tree_key) low_value);
5339 : :
5340 : : /* Check to see if the LOW_BOUND overlaps. It is smaller than
5341 : : the LOW_VALUE, so there is no need to check unless the
5342 : : LOW_BOUND is in fact itself a case range. */
5343 : 2724574 : if (low_bound
5344 : 1838289 : && CASE_HIGH ((tree) low_bound->value)
5345 : 2724741 : && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5346 : : low_value) >= 0)
5347 : : node = low_bound;
5348 : : /* Check to see if the HIGH_BOUND overlaps. The low end of that
5349 : : range is bigger than the low end of the current range, so we
5350 : : are only interested if the current range is a real range, and
5351 : : not an ordinary case label. */
5352 : 2724562 : else if (high_bound
5353 : 2724562 : && high_value
5354 : 2724562 : && (tree_int_cst_compare ((tree) high_bound->key,
5355 : : high_value)
5356 : : <= 0))
5357 : : node = high_bound;
5358 : : }
5359 : : /* If there was an overlap, issue an error. */
5360 : 3029691 : if (node)
5361 : : {
5362 : 37 : tree duplicate = CASE_LABEL ((tree) node->value);
5363 : :
5364 : 37 : if (high_value)
5365 : : {
5366 : 10 : error_at (loc, "duplicate (or overlapping) case value");
5367 : 10 : inform (DECL_SOURCE_LOCATION (duplicate),
5368 : : "this is the first entry overlapping that value");
5369 : : }
5370 : 27 : else if (low_value)
5371 : : {
5372 : 26 : error_at (loc, "duplicate case value") ;
5373 : 26 : inform (DECL_SOURCE_LOCATION (duplicate), "previously used here");
5374 : : }
5375 : : else
5376 : : {
5377 : 1 : error_at (loc, "multiple default labels in one switch");
5378 : 1 : inform (DECL_SOURCE_LOCATION (duplicate),
5379 : : "this is the first default label");
5380 : : }
5381 : 37 : goto error_out;
5382 : : }
5383 : :
5384 : : /* Add a CASE_LABEL to the statement-tree. */
5385 : 3029673 : case_label = add_stmt (build_case_label (low_value, high_value, label));
5386 : : /* Register this case label in the splay tree. */
5387 : 3029673 : splay_tree_insert (cases,
5388 : : (splay_tree_key) low_value,
5389 : : (splay_tree_value) case_label);
5390 : :
5391 : 3029673 : return case_label;
5392 : :
5393 : 217 : error_out:
5394 : : /* Add a label so that the back-end doesn't think that the beginning of
5395 : : the switch is unreachable. Note that we do not add a case label, as
5396 : : that just leads to duplicates and thence to failure later on. */
5397 : 217 : if (!cases->root)
5398 : : {
5399 : 153 : tree t = create_artificial_label (loc);
5400 : 153 : add_stmt (build_stmt (loc, LABEL_EXPR, t));
5401 : : }
5402 : 217 : return error_mark_node;
5403 : : }
5404 : :
5405 : : /* Subroutine of c_switch_covers_all_cases_p, called via
5406 : : splay_tree_foreach. Return 1 if it doesn't cover all the cases.
5407 : : ARGS[0] is initially NULL and after the first iteration is the
5408 : : so far highest case label. ARGS[1] is the minimum of SWITCH_COND's
5409 : : type. */
5410 : :
5411 : : static int
5412 : 134069 : c_switch_covers_all_cases_p_1 (splay_tree_node node, void *data)
5413 : : {
5414 : 134069 : tree label = (tree) node->value;
5415 : 134069 : tree *args = (tree *) data;
5416 : :
5417 : : /* If there is a default case, we shouldn't have called this. */
5418 : 134069 : gcc_assert (CASE_LOW (label));
5419 : :
5420 : 134069 : if (args[0] == NULL_TREE)
5421 : : {
5422 : 46959 : if (wi::to_widest (args[1]) < wi::to_widest (CASE_LOW (label)))
5423 : : return 1;
5424 : : }
5425 : 87110 : else if (wi::add (wi::to_widest (args[0]), 1)
5426 : 174220 : != wi::to_widest (CASE_LOW (label)))
5427 : : return 1;
5428 : 115880 : if (CASE_HIGH (label))
5429 : 109 : args[0] = CASE_HIGH (label);
5430 : : else
5431 : 115771 : args[0] = CASE_LOW (label);
5432 : : return 0;
5433 : : }
5434 : :
5435 : : /* Return true if switch with CASES and switch condition with type
5436 : : covers all possible values in the case labels. */
5437 : :
5438 : : bool
5439 : 80217 : c_switch_covers_all_cases_p (splay_tree cases, tree type)
5440 : : {
5441 : : /* If there is default:, this is always the case. */
5442 : 80217 : splay_tree_node default_node
5443 : 80217 : = splay_tree_lookup (cases, (splay_tree_key) NULL);
5444 : 80217 : if (default_node)
5445 : : return true;
5446 : :
5447 : 47491 : if (!INTEGRAL_TYPE_P (type))
5448 : : return false;
5449 : :
5450 : 47424 : tree args[2] = { NULL_TREE, TYPE_MIN_VALUE (type) };
5451 : 47424 : if (splay_tree_foreach (cases, c_switch_covers_all_cases_p_1, args))
5452 : : return false;
5453 : :
5454 : : /* If there are no cases at all, or if the highest case label
5455 : : is smaller than TYPE_MAX_VALUE, return false. */
5456 : 29235 : if (args[0] == NULL_TREE
5457 : 29235 : || wi::to_widest (args[0]) < wi::to_widest (TYPE_MAX_VALUE (type)))
5458 : 29189 : return false;
5459 : :
5460 : : return true;
5461 : : }
5462 : :
5463 : : /* Return true if stmt can fall through. Used by block_may_fallthru
5464 : : default case. */
5465 : :
5466 : : bool
5467 : 1766697 : c_block_may_fallthru (const_tree stmt)
5468 : : {
5469 : 1766697 : switch (TREE_CODE (stmt))
5470 : : {
5471 : 278 : case SWITCH_STMT:
5472 : 278 : return (!SWITCH_STMT_ALL_CASES_P (stmt)
5473 : 184 : || !SWITCH_STMT_NO_BREAK_P (stmt)
5474 : 306 : || block_may_fallthru (SWITCH_STMT_BODY (stmt)));
5475 : :
5476 : : default:
5477 : : return true;
5478 : : }
5479 : : }
5480 : :
5481 : : /* Finish an expression taking the address of LABEL (an
5482 : : IDENTIFIER_NODE). Returns an expression for the address.
5483 : :
5484 : : LOC is the location for the expression returned. */
5485 : :
5486 : : tree
5487 : 2119 : finish_label_address_expr (tree label, location_t loc)
5488 : : {
5489 : 2119 : tree result;
5490 : :
5491 : 2119 : pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
5492 : :
5493 : 2119 : if (label == error_mark_node)
5494 : : return error_mark_node;
5495 : :
5496 : 2113 : label = lookup_label (label);
5497 : 2113 : if (label == NULL_TREE)
5498 : 5 : result = null_pointer_node;
5499 : : else
5500 : : {
5501 : 2108 : TREE_USED (label) = 1;
5502 : 2108 : result = build1 (ADDR_EXPR, ptr_type_node, label);
5503 : : /* The current function is not necessarily uninlinable.
5504 : : Computed gotos are incompatible with inlining, but the value
5505 : : here could be used only in a diagnostic, for example. */
5506 : 2108 : protected_set_expr_location (result, loc);
5507 : : }
5508 : :
5509 : : return result;
5510 : : }
5511 : :
5512 : :
5513 : : /* Given a boolean expression ARG, return a tree representing an increment
5514 : : or decrement (as indicated by CODE) of ARG. The front end must check for
5515 : : invalid cases (e.g., decrement in C++). */
5516 : : tree
5517 : 85 : boolean_increment (enum tree_code code, tree arg)
5518 : : {
5519 : 85 : tree val;
5520 : 85 : tree true_res = build_int_cst (TREE_TYPE (arg), 1);
5521 : :
5522 : 85 : arg = stabilize_reference (arg);
5523 : 85 : switch (code)
5524 : : {
5525 : 28 : case PREINCREMENT_EXPR:
5526 : 28 : val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5527 : 28 : break;
5528 : 29 : case POSTINCREMENT_EXPR:
5529 : 29 : val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5530 : 29 : arg = save_expr (arg);
5531 : 29 : val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5532 : 29 : val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5533 : 29 : break;
5534 : 14 : case PREDECREMENT_EXPR:
5535 : 14 : val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5536 : : invert_truthvalue_loc (input_location, arg));
5537 : 14 : break;
5538 : 14 : case POSTDECREMENT_EXPR:
5539 : 14 : val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5540 : : invert_truthvalue_loc (input_location, arg));
5541 : 14 : arg = save_expr (arg);
5542 : 14 : val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5543 : 14 : val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5544 : 14 : break;
5545 : 0 : default:
5546 : 0 : gcc_unreachable ();
5547 : : }
5548 : 85 : TREE_SIDE_EFFECTS (val) = 1;
5549 : 85 : return val;
5550 : : }
5551 : :
5552 : : /* Built-in macros for stddef.h and stdint.h, that require macros
5553 : : defined in this file. */
5554 : : void
5555 : 209010 : c_stddef_cpp_builtins(void)
5556 : : {
5557 : 214444 : builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
5558 : 214444 : builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
5559 : 214444 : builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
5560 : : /* C++ has wchar_t as a builtin type, C doesn't, so if WINT_TYPE
5561 : : maps to wchar_t, define it to the underlying WCHAR_TYPE in C, and
5562 : : to wchar_t in C++, so the desired type equivalence holds. */
5563 : 209010 : if (!c_dialect_cxx ()
5564 : : && strcmp (WINT_TYPE, "wchar_t") == 0)
5565 : : builtin_define_with_value ("__WINT_TYPE__", WCHAR_TYPE, 0);
5566 : : else
5567 : 209010 : builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
5568 : 214444 : builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
5569 : 214444 : builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
5570 : 209010 : if (flag_char8_t)
5571 : 130114 : builtin_define_with_value ("__CHAR8_TYPE__", CHAR8_TYPE, 0);
5572 : 209010 : builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
5573 : 209010 : builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
5574 : 209010 : if (SIG_ATOMIC_TYPE)
5575 : 209010 : builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
5576 : 209010 : if (INT8_TYPE)
5577 : 209010 : builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
5578 : 209010 : if (INT16_TYPE)
5579 : 209010 : builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
5580 : 209010 : if (INT32_TYPE)
5581 : 209010 : builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
5582 : 209010 : if (INT64_TYPE)
5583 : 214444 : builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
5584 : 209010 : if (UINT8_TYPE)
5585 : 209010 : builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
5586 : 209010 : if (UINT16_TYPE)
5587 : 209010 : builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
5588 : 209010 : if (UINT32_TYPE)
5589 : 209010 : builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
5590 : 209010 : if (UINT64_TYPE)
5591 : 214444 : builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
5592 : 209010 : if (INT_LEAST8_TYPE)
5593 : 209010 : builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
5594 : 209010 : if (INT_LEAST16_TYPE)
5595 : 209010 : builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
5596 : 209010 : if (INT_LEAST32_TYPE)
5597 : 209010 : builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
5598 : 209010 : if (INT_LEAST64_TYPE)
5599 : 214444 : builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
5600 : 209010 : if (UINT_LEAST8_TYPE)
5601 : 209010 : builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
5602 : 209010 : if (UINT_LEAST16_TYPE)
5603 : 209010 : builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
5604 : 209010 : if (UINT_LEAST32_TYPE)
5605 : 209010 : builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
5606 : 209010 : if (UINT_LEAST64_TYPE)
5607 : 214444 : builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
5608 : 209010 : if (INT_FAST8_TYPE)
5609 : 209010 : builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
5610 : 209010 : if (INT_FAST16_TYPE)
5611 : 214444 : builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
5612 : 209010 : if (INT_FAST32_TYPE)
5613 : 214444 : builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
5614 : 209010 : if (INT_FAST64_TYPE)
5615 : 214444 : builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
5616 : 209010 : if (UINT_FAST8_TYPE)
5617 : 209010 : builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
5618 : 209010 : if (UINT_FAST16_TYPE)
5619 : 214444 : builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
5620 : 209010 : if (UINT_FAST32_TYPE)
5621 : 214444 : builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
5622 : 209010 : if (UINT_FAST64_TYPE)
5623 : 214444 : builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
5624 : 209010 : if (INTPTR_TYPE)
5625 : 214444 : builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
5626 : 209010 : if (UINTPTR_TYPE)
5627 : 214444 : builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
5628 : : /* GIMPLE FE testcases need access to the GCC internal 'sizetype'.
5629 : : Expose it as __SIZETYPE__. */
5630 : 209010 : if (flag_gimple)
5631 : 288 : builtin_define_with_value ("__SIZETYPE__", SIZETYPE, 0);
5632 : 209010 : }
5633 : :
5634 : : static void
5635 : 209538 : c_init_attributes (void)
5636 : : {
5637 : : /* Fill in the built_in_attributes array. */
5638 : : #define DEF_ATTR_NULL_TREE(ENUM) \
5639 : : built_in_attributes[(int) ENUM] = NULL_TREE;
5640 : : #define DEF_ATTR_INT(ENUM, VALUE) \
5641 : : built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
5642 : : #define DEF_ATTR_STRING(ENUM, VALUE) \
5643 : : built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
5644 : : #define DEF_ATTR_IDENT(ENUM, STRING) \
5645 : : built_in_attributes[(int) ENUM] = get_identifier (STRING);
5646 : : #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5647 : : built_in_attributes[(int) ENUM] \
5648 : : = tree_cons (built_in_attributes[(int) PURPOSE], \
5649 : : built_in_attributes[(int) VALUE], \
5650 : : built_in_attributes[(int) CHAIN]);
5651 : : #include "builtin-attrs.def"
5652 : : #undef DEF_ATTR_NULL_TREE
5653 : : #undef DEF_ATTR_INT
5654 : : #undef DEF_ATTR_IDENT
5655 : : #undef DEF_ATTR_TREE_LIST
5656 : 209538 : }
5657 : :
5658 : : /* Check whether the byte alignment ALIGN is a valid user-specified
5659 : : alignment less than the supported maximum. If so, return ALIGN's
5660 : : base-2 log; if not, output an error and return -1. If OBJFILE
5661 : : then reject alignments greater than MAX_OFILE_ALIGNMENT when
5662 : : converted to bits. Otherwise, consider valid only alignments
5663 : : that are less than HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT.
5664 : : Zero is not considered a valid argument (and results in -1 on
5665 : : return) but it only triggers a warning when WARN_ZERO is set. */
5666 : :
5667 : : int
5668 : 570630 : check_user_alignment (const_tree align, bool objfile, bool warn_zero)
5669 : : {
5670 : 570630 : if (error_operand_p (align))
5671 : : return -1;
5672 : :
5673 : 570607 : if (TREE_CODE (align) != INTEGER_CST
5674 : 570607 : || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
5675 : : {
5676 : 27 : error ("requested alignment is not an integer constant");
5677 : 27 : return -1;
5678 : : }
5679 : :
5680 : 570580 : if (integer_zerop (align))
5681 : : {
5682 : 75 : if (warn_zero)
5683 : 63 : warning (OPT_Wattributes,
5684 : : "requested alignment %qE is not a positive power of 2",
5685 : : align);
5686 : 75 : return -1;
5687 : : }
5688 : :
5689 : : /* Log2 of the byte alignment ALIGN. */
5690 : 570505 : int log2align;
5691 : 570505 : if (tree_int_cst_sgn (align) == -1
5692 : 570505 : || (log2align = tree_log2 (align)) == -1)
5693 : : {
5694 : 36 : error ("requested alignment %qE is not a positive power of 2",
5695 : : align);
5696 : 36 : return -1;
5697 : : }
5698 : :
5699 : 570469 : if (objfile)
5700 : : {
5701 : 15603 : unsigned maxalign = MAX_OFILE_ALIGNMENT / BITS_PER_UNIT;
5702 : 15603 : if (!tree_fits_uhwi_p (align) || tree_to_uhwi (align) > maxalign)
5703 : : {
5704 : 10 : error ("requested alignment %qE exceeds object file maximum %u",
5705 : : align, maxalign);
5706 : 10 : return -1;
5707 : : }
5708 : : }
5709 : :
5710 : 570459 : if (log2align >= HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT)
5711 : : {
5712 : 17 : error ("requested alignment %qE exceeds maximum %u",
5713 : : align, 1U << (HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT - 1));
5714 : 17 : return -1;
5715 : : }
5716 : :
5717 : : return log2align;
5718 : : }
5719 : :
5720 : : /* Determine the ELF symbol visibility for DECL, which is either a
5721 : : variable or a function. It is an error to use this function if a
5722 : : definition of DECL is not available in this translation unit.
5723 : : Returns true if the final visibility has been determined by this
5724 : : function; false if the caller is free to make additional
5725 : : modifications. */
5726 : :
5727 : : bool
5728 : 50976018 : c_determine_visibility (tree decl)
5729 : : {
5730 : 50976018 : gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
5731 : :
5732 : : /* If the user explicitly specified the visibility with an
5733 : : attribute, honor that. DECL_VISIBILITY will have been set during
5734 : : the processing of the attribute. We check for an explicit
5735 : : attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
5736 : : to distinguish the use of an attribute from the use of a "#pragma
5737 : : GCC visibility push(...)"; in the latter case we still want other
5738 : : considerations to be able to overrule the #pragma. */
5739 : 50976018 : if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
5740 : : || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
5741 : : && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
5742 : : || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
5743 : : return true;
5744 : :
5745 : : /* Set default visibility to whatever the user supplied with
5746 : : visibility_specified depending on #pragma GCC visibility. */
5747 : 50344503 : if (!DECL_VISIBILITY_SPECIFIED (decl))
5748 : : {
5749 : 50343265 : if (visibility_options.inpragma
5750 : 50343265 : || DECL_VISIBILITY (decl) != default_visibility)
5751 : : {
5752 : 210826 : DECL_VISIBILITY (decl) = default_visibility;
5753 : 210826 : DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
5754 : : /* If visibility changed and DECL already has DECL_RTL, ensure
5755 : : symbol flags are updated. */
5756 : 14204 : if (((VAR_P (decl) && TREE_STATIC (decl))
5757 : 210774 : || TREE_CODE (decl) == FUNCTION_DECL)
5758 : 407500 : && DECL_RTL_SET_P (decl))
5759 : 0 : make_decl_rtl (decl);
5760 : : }
5761 : : }
5762 : : return false;
5763 : : }
5764 : :
5765 : : /* Data to communicate through check_function_arguments_recurse between
5766 : : check_function_nonnull and check_nonnull_arg. */
5767 : :
5768 : : struct nonnull_arg_ctx
5769 : : {
5770 : : /* Location of the call. */
5771 : : location_t loc;
5772 : : /* The function whose arguments are being checked and its type (used
5773 : : for calls through function pointers). */
5774 : : const_tree fndecl, fntype;
5775 : : /* For nonnull_if_nonzero, index of the other arguments. */
5776 : : unsigned HOST_WIDE_INT other1, other2;
5777 : : /* True if a warning has been issued. */
5778 : : bool warned_p;
5779 : : };
5780 : :
5781 : : /* Check the argument list of a function call to CTX.FNDECL of CTX.FNTYPE
5782 : : for null in argument slots that are marked as requiring a non-null
5783 : : pointer argument. The NARGS arguments are passed in the array ARGARRAY.
5784 : : Return true if we have warned. */
5785 : :
5786 : : static bool
5787 : 6406201 : check_function_nonnull (nonnull_arg_ctx &ctx, int nargs, tree *argarray)
5788 : : {
5789 : 6406201 : int firstarg = 0;
5790 : 6406201 : if (TREE_CODE (ctx.fntype) == METHOD_TYPE)
5791 : : {
5792 : 478290 : bool closure = false;
5793 : 478290 : if (ctx.fndecl)
5794 : : {
5795 : : /* For certain lambda expressions the C++ front end emits calls
5796 : : that pass a null this pointer as an argument named __closure
5797 : : to the member operator() of empty function. Detect those
5798 : : and avoid checking them, but proceed to check the remaining
5799 : : arguments. */
5800 : 478139 : tree arg0 = DECL_ARGUMENTS (ctx.fndecl);
5801 : 478139 : if (tree arg0name = DECL_NAME (arg0))
5802 : 478139 : closure = id_equal (arg0name, "__closure");
5803 : : }
5804 : :
5805 : : /* In calls to C++ non-static member functions check the this
5806 : : pointer regardless of whether the function is declared with
5807 : : attribute nonnull. */
5808 : 951610 : firstarg = 1;
5809 : 478139 : if (!closure)
5810 : 473471 : check_function_arguments_recurse (check_nonnull_arg, &ctx, argarray[0],
5811 : : firstarg, OPT_Wnonnull);
5812 : : }
5813 : :
5814 : 6406201 : tree attrs = lookup_attribute ("nonnull", TYPE_ATTRIBUTES (ctx.fntype));
5815 : :
5816 : 6406201 : tree a = attrs;
5817 : : /* See if any of the nonnull attributes has no arguments. If so,
5818 : : then every pointer argument is checked (in which case the check
5819 : : for pointer type is done in check_nonnull_arg). */
5820 : 13042965 : while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE)
5821 : 102387 : a = lookup_attribute ("nonnull", TREE_CHAIN (a));
5822 : :
5823 : 6406201 : if (a != NULL_TREE)
5824 : 68569 : for (int i = firstarg; i < nargs; i++)
5825 : 42780 : check_function_arguments_recurse (check_nonnull_arg, &ctx, argarray[i],
5826 : 42780 : i + 1, OPT_Wnonnull);
5827 : 6380412 : else if (attrs)
5828 : : {
5829 : : /* Walk the argument list. If we encounter an argument number we
5830 : : should check for non-null, do it. */
5831 : 344204 : for (int i = firstarg; i < nargs; i++)
5832 : : {
5833 : 146483 : for (a = attrs; ; a = TREE_CHAIN (a))
5834 : : {
5835 : 398818 : a = lookup_attribute ("nonnull", a);
5836 : 398818 : if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
5837 : : break;
5838 : : }
5839 : :
5840 : 252335 : if (a != NULL_TREE)
5841 : 123058 : check_function_arguments_recurse (check_nonnull_arg, &ctx,
5842 : 123058 : argarray[i], i + 1,
5843 : : OPT_Wnonnull);
5844 : 252335 : a = NULL_TREE;
5845 : : }
5846 : : }
5847 : 6406201 : if (a == NULL_TREE)
5848 : 6380412 : for (attrs = TYPE_ATTRIBUTES (ctx.fntype);
5849 : 6437432 : (attrs = lookup_attribute ("nonnull_if_nonzero", attrs));
5850 : 57020 : attrs = TREE_CHAIN (attrs))
5851 : : {
5852 : 57020 : tree args = TREE_VALUE (attrs);
5853 : 57020 : unsigned int idx = TREE_INT_CST_LOW (TREE_VALUE (args)) - 1;
5854 : 57020 : unsigned int idx2
5855 : 57020 : = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args))) - 1;
5856 : 57020 : unsigned int idx3 = idx2;
5857 : 57020 : if (tree chain2 = TREE_CHAIN (TREE_CHAIN (args)))
5858 : 479 : idx3 = TREE_INT_CST_LOW (TREE_VALUE (chain2)) - 1;
5859 : 57020 : if (idx < (unsigned) nargs - firstarg
5860 : 57015 : && idx2 < (unsigned) nargs - firstarg
5861 : 57010 : && idx3 < (unsigned) nargs - firstarg
5862 : 57010 : && INTEGRAL_TYPE_P (TREE_TYPE (argarray[firstarg + idx2]))
5863 : 56937 : && integer_nonzerop (argarray[firstarg + idx2])
5864 : 31710 : && INTEGRAL_TYPE_P (TREE_TYPE (argarray[firstarg + idx3]))
5865 : 88730 : && integer_nonzerop (argarray[firstarg + idx3]))
5866 : : {
5867 : 31618 : ctx.other1 = firstarg + idx2 + 1;
5868 : 31618 : ctx.other2 = firstarg + idx3 + 1;
5869 : 31618 : check_function_arguments_recurse (check_nonnull_arg, &ctx,
5870 : 31618 : argarray[firstarg + idx],
5871 : 31618 : firstarg + idx + 1,
5872 : : OPT_Wnonnull);
5873 : 31618 : ctx.other1 = 0;
5874 : 31618 : ctx.other2 = 0;
5875 : : }
5876 : : }
5877 : 6406201 : return ctx.warned_p;
5878 : : }
5879 : :
5880 : : /* Check that the Nth argument of a function call (counting backwards
5881 : : from the end) is a (pointer)0. The NARGS arguments are passed in the
5882 : : array ARGARRAY. */
5883 : :
5884 : : static void
5885 : 6392006 : check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
5886 : : {
5887 : 6392006 : tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
5888 : :
5889 : 6392006 : if (attr)
5890 : : {
5891 : 112 : int len = 0;
5892 : 112 : int pos = 0;
5893 : 112 : tree sentinel;
5894 : 112 : function_args_iterator iter;
5895 : 112 : tree t;
5896 : :
5897 : : /* Skip over the named arguments. */
5898 : 237 : FOREACH_FUNCTION_ARGS (fntype, t, iter)
5899 : : {
5900 : 125 : if (len == nargs)
5901 : : break;
5902 : 125 : len++;
5903 : : }
5904 : :
5905 : 112 : if (TREE_VALUE (attr))
5906 : : {
5907 : 59 : tree p = TREE_VALUE (TREE_VALUE (attr));
5908 : 59 : pos = TREE_INT_CST_LOW (p);
5909 : : }
5910 : :
5911 : : /* The sentinel must be one of the varargs, i.e.
5912 : : in position >= the number of fixed arguments. */
5913 : 112 : if ((nargs - 1 - pos) < len)
5914 : : {
5915 : 22 : warning (OPT_Wformat_,
5916 : : "not enough variable arguments to fit a sentinel");
5917 : 22 : return;
5918 : : }
5919 : :
5920 : : /* Validate the sentinel. */
5921 : 90 : sentinel = fold_for_warn (argarray[nargs - 1 - pos]);
5922 : 138 : if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
5923 : 42 : || !integer_zerop (sentinel))
5924 : 56 : && TREE_CODE (TREE_TYPE (sentinel)) != NULLPTR_TYPE
5925 : : /* Although __null (in C++) is only an integer we allow it
5926 : : nevertheless, as we are guaranteed that it's exactly
5927 : : as wide as a pointer, and we don't want to force
5928 : : users to cast the NULL they have written there.
5929 : : We warn with -Wstrict-null-sentinel, though. */
5930 : 138 : && (warn_strict_null_sentinel || null_node != sentinel))
5931 : 45 : warning (OPT_Wformat_, "missing sentinel in function call");
5932 : : }
5933 : : }
5934 : :
5935 : : /* Check that the same argument isn't passed to two or more
5936 : : restrict-qualified formal and issue a -Wrestrict warning
5937 : : if it is. Return true if a warning has been issued. */
5938 : :
5939 : : static bool
5940 : 52202193 : check_function_restrict (const_tree fndecl, const_tree fntype,
5941 : : int nargs, tree *unfolded_argarray)
5942 : : {
5943 : 52202193 : int i;
5944 : 52202193 : tree parms = TYPE_ARG_TYPES (fntype);
5945 : :
5946 : : /* Call fold_for_warn on all of the arguments. */
5947 : 52202193 : auto_vec<tree> argarray (nargs);
5948 : 235223676 : for (i = 0; i < nargs; i++)
5949 : 130819290 : argarray.quick_push (fold_for_warn (unfolded_argarray[i]));
5950 : :
5951 : 52202193 : if (fndecl
5952 : 51674468 : && TREE_CODE (fndecl) == FUNCTION_DECL)
5953 : : {
5954 : : /* Avoid diagnosing calls built-ins with a zero size/bound
5955 : : here. They are checked in more detail elsewhere. */
5956 : 51674468 : if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
5957 : 1707575 : && nargs == 3
5958 : 325488 : && TREE_CODE (argarray[2]) == INTEGER_CST
5959 : 51844661 : && integer_zerop (argarray[2]))
5960 : : return false;
5961 : :
5962 : 51648866 : if (DECL_ARGUMENTS (fndecl))
5963 : 52176591 : parms = DECL_ARGUMENTS (fndecl);
5964 : : }
5965 : :
5966 : 182919075 : for (i = 0; i < nargs; i++)
5967 : 130742484 : TREE_VISITED (argarray[i]) = 0;
5968 : :
5969 : : bool warned = false;
5970 : :
5971 : 182040539 : for (i = 0; i < nargs && parms && parms != void_list_node; i++)
5972 : : {
5973 : 129863948 : tree type;
5974 : 129863948 : if (TREE_CODE (parms) == PARM_DECL)
5975 : : {
5976 : 15531889 : type = TREE_TYPE (parms);
5977 : 15531889 : parms = DECL_CHAIN (parms);
5978 : : }
5979 : : else
5980 : : {
5981 : 114332059 : type = TREE_VALUE (parms);
5982 : 114332059 : parms = TREE_CHAIN (parms);
5983 : : }
5984 : 129863948 : if (POINTER_TYPE_P (type)
5985 : 7945972 : && TYPE_RESTRICT (type)
5986 : 130112199 : && !TYPE_READONLY (TREE_TYPE (type)))
5987 : 248920 : warned |= warn_for_restrict (i, argarray.address (), nargs);
5988 : : }
5989 : :
5990 : 182919075 : for (i = 0; i < nargs; i++)
5991 : 130742484 : TREE_VISITED (argarray[i]) = 0;
5992 : :
5993 : : return warned;
5994 : 52202193 : }
5995 : :
5996 : : /* Helper for check_function_nonnull; given a list of operands which
5997 : : must be non-null in ARGS, determine if operand PARAM_NUM should be
5998 : : checked. */
5999 : :
6000 : : static bool
6001 : 269541 : nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
6002 : : {
6003 : 269541 : unsigned HOST_WIDE_INT arg_num = 0;
6004 : :
6005 : 715688 : for (; args; args = TREE_CHAIN (args))
6006 : : {
6007 : 569205 : bool found = get_attribute_operand (TREE_VALUE (args), &arg_num);
6008 : :
6009 : 569205 : gcc_assert (found);
6010 : :
6011 : 569205 : if (arg_num == param_num)
6012 : : return true;
6013 : : }
6014 : : return false;
6015 : : }
6016 : :
6017 : : /* Check that the function argument PARAM (which is operand number
6018 : : PARAM_NUM) is non-null. This is called by check_function_nonnull
6019 : : via check_function_arguments_recurse. */
6020 : :
6021 : : static void
6022 : 669955 : check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
6023 : : {
6024 : 669955 : struct nonnull_arg_ctx *pctx = (struct nonnull_arg_ctx *) ctx;
6025 : :
6026 : : /* Just skip checking the argument if it's not a pointer. This can
6027 : : happen if the "nonnull" attribute was given without an operand
6028 : : list (which means to check every pointer argument). */
6029 : :
6030 : 669955 : tree paramtype = TREE_TYPE (param);
6031 : 669955 : if (TREE_CODE (paramtype) != POINTER_TYPE
6032 : 60848 : && TREE_CODE (paramtype) != NULLPTR_TYPE)
6033 : 669206 : return;
6034 : :
6035 : : /* Diagnose the simple cases of null arguments. */
6036 : 609113 : if (!integer_zerop (fold_for_warn (param)))
6037 : : return;
6038 : :
6039 : 749 : auto_diagnostic_group adg;
6040 : :
6041 : 749 : const location_t loc = EXPR_LOC_OR_LOC (param, pctx->loc);
6042 : :
6043 : 749 : if (TREE_CODE (pctx->fntype) == METHOD_TYPE)
6044 : 109 : --param_num;
6045 : :
6046 : 749 : bool warned;
6047 : 749 : if (param_num == 0)
6048 : : {
6049 : 30 : warned = warning_at (loc, OPT_Wnonnull,
6050 : : "%qs pointer is null", "this");
6051 : 30 : if (warned && pctx->fndecl)
6052 : 27 : inform (DECL_SOURCE_LOCATION (pctx->fndecl),
6053 : : "in a call to non-static member function %qD",
6054 : : pctx->fndecl);
6055 : : }
6056 : : else
6057 : : {
6058 : 719 : if (pctx->other1 && pctx->other2 != pctx->other1)
6059 : 14 : warned = warning_at (loc, OPT_Wnonnull,
6060 : : "argument %u null where non-null expected "
6061 : : "because arguments %u and %u are nonzero",
6062 : : (unsigned) param_num,
6063 : : TREE_CODE (pctx->fntype) == METHOD_TYPE
6064 : 0 : ? (unsigned) pctx->other1 - 1
6065 : : : (unsigned) pctx->other1,
6066 : : TREE_CODE (pctx->fntype) == METHOD_TYPE
6067 : 0 : ? (unsigned) pctx->other2 - 1
6068 : : : (unsigned) pctx->other2);
6069 : 705 : else if (pctx->other1)
6070 : 50 : warned = warning_at (loc, OPT_Wnonnull,
6071 : : "argument %u null where non-null expected "
6072 : : "because argument %u is nonzero",
6073 : : (unsigned) param_num,
6074 : : TREE_CODE (pctx->fntype) == METHOD_TYPE
6075 : 0 : ? (unsigned) pctx->other1 - 1
6076 : : : (unsigned) pctx->other1);
6077 : : else
6078 : 655 : warned = warning_at (loc, OPT_Wnonnull,
6079 : : "argument %u null where non-null expected",
6080 : : (unsigned) param_num);
6081 : 719 : if (warned && pctx->fndecl)
6082 : 688 : inform (DECL_SOURCE_LOCATION (pctx->fndecl),
6083 : : "in a call to function %qD declared %qs",
6084 : : pctx->fndecl,
6085 : 688 : pctx->other1 ? "nonnull_if_nonzero" : "nonnull");
6086 : : }
6087 : :
6088 : 746 : if (warned)
6089 : 746 : pctx->warned_p = true;
6090 : 749 : }
6091 : :
6092 : : /* Helper for attribute handling; fetch the operand number from
6093 : : the attribute argument list. */
6094 : :
6095 : : bool
6096 : 569205 : get_attribute_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
6097 : : {
6098 : : /* Verify the arg number is a small constant. */
6099 : 569205 : if (tree_fits_uhwi_p (arg_num_expr))
6100 : : {
6101 : 569205 : *valp = tree_to_uhwi (arg_num_expr);
6102 : 569205 : return true;
6103 : : }
6104 : : else
6105 : : return false;
6106 : : }
6107 : :
6108 : : /* Arguments being collected for optimization. */
6109 : : typedef const char *const_char_p; /* For DEF_VEC_P. */
6110 : : static GTY(()) vec<const_char_p, va_gc> *optimize_args;
6111 : :
6112 : :
6113 : : /* Inner function to convert a TREE_LIST to argv string to parse the optimize
6114 : : options in ARGS. ATTR_P is true if this is for attribute(optimize), and
6115 : : false for #pragma GCC optimize. */
6116 : :
6117 : : bool
6118 : 344075 : parse_optimize_options (tree args, bool attr_p)
6119 : : {
6120 : 344075 : bool ret = true;
6121 : 344075 : unsigned opt_argc;
6122 : 344075 : unsigned i;
6123 : 344075 : const char **opt_argv;
6124 : 344075 : struct cl_decoded_option *decoded_options;
6125 : 344075 : unsigned int decoded_options_count;
6126 : 344075 : tree ap;
6127 : :
6128 : : /* Build up argv vector. Just in case the string is stored away, use garbage
6129 : : collected strings. */
6130 : 344075 : vec_safe_truncate (optimize_args, 0);
6131 : 344075 : vec_safe_push (optimize_args, (const char *) NULL);
6132 : :
6133 : 706849 : for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
6134 : : {
6135 : 362774 : tree value = TREE_VALUE (ap);
6136 : :
6137 : 362774 : if (TREE_CODE (value) == INTEGER_CST)
6138 : : {
6139 : 305 : char buffer[HOST_BITS_PER_LONG / 3 + 4];
6140 : 305 : sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
6141 : 305 : vec_safe_push (optimize_args, ggc_strdup (buffer));
6142 : : }
6143 : :
6144 : 362469 : else if (TREE_CODE (value) == STRING_CST)
6145 : : {
6146 : : /* Split string into multiple substrings. */
6147 : 362465 : size_t len = TREE_STRING_LENGTH (value);
6148 : 362465 : char *p = ASTRDUP (TREE_STRING_POINTER (value));
6149 : 362465 : char *end = p + len;
6150 : 362465 : char *comma;
6151 : 362465 : char *next_p = p;
6152 : :
6153 : 724979 : while (next_p != NULL)
6154 : : {
6155 : 362514 : size_t len2;
6156 : 362514 : char *q, *r;
6157 : :
6158 : 362514 : p = next_p;
6159 : 362514 : comma = strchr (p, ',');
6160 : 362514 : if (comma)
6161 : : {
6162 : 49 : len2 = comma - p;
6163 : 49 : *comma = '\0';
6164 : 49 : next_p = comma+1;
6165 : : }
6166 : : else
6167 : : {
6168 : 362465 : len2 = end - p;
6169 : 362465 : next_p = NULL;
6170 : : }
6171 : :
6172 : : /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
6173 : : options. */
6174 : 362514 : if (*p == '-' && p[1] != 'O' && p[1] != 'f')
6175 : : {
6176 : 0 : ret = false;
6177 : 0 : if (attr_p)
6178 : : {
6179 : 0 : auto_urlify_attributes sentinel;
6180 : 0 : warning (OPT_Wattributes,
6181 : : "bad option %qs to attribute %<optimize%>", p);
6182 : 0 : }
6183 : : else
6184 : 0 : warning (OPT_Wpragmas,
6185 : : "bad option %qs to pragma %<optimize%>", p);
6186 : 0 : continue;
6187 : 0 : }
6188 : :
6189 : : /* Can't use GC memory here, see PR88007. */
6190 : 362514 : r = q = XOBNEWVEC (&opts_obstack, char, len2 + 3);
6191 : :
6192 : 362514 : if (*p != '-')
6193 : : {
6194 : 42877 : *r++ = '-';
6195 : :
6196 : : /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
6197 : : itself is -Os, and any other switch begins with a -f. */
6198 : 42877 : if ((*p >= '0' && *p <= '9')
6199 : 42720 : || (p[0] == 's' && p[1] == '\0'))
6200 : 157 : *r++ = 'O';
6201 : 42720 : else if (*p != 'O')
6202 : 23307 : *r++ = 'f';
6203 : : }
6204 : :
6205 : 362514 : memcpy (r, p, len2);
6206 : 362514 : r[len2] = '\0';
6207 : 362514 : vec_safe_push (optimize_args, (const char *) q);
6208 : : }
6209 : :
6210 : : }
6211 : : }
6212 : :
6213 : 344075 : opt_argc = optimize_args->length ();
6214 : 344075 : opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
6215 : :
6216 : 706894 : for (i = 1; i < opt_argc; i++)
6217 : 362819 : opt_argv[i] = (*optimize_args)[i];
6218 : :
6219 : : /* Now parse the options. */
6220 : 344075 : decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
6221 : : &decoded_options,
6222 : : &decoded_options_count);
6223 : : /* Drop non-Optimization options. */
6224 : 344075 : unsigned j = 1;
6225 : 706894 : for (i = 1; i < decoded_options_count; ++i)
6226 : : {
6227 : 362819 : if (! (cl_options[decoded_options[i].opt_index].flags & CL_OPTIMIZATION))
6228 : : {
6229 : 3 : ret = false;
6230 : 3 : if (attr_p)
6231 : : {
6232 : 2 : auto_urlify_attributes sentinel;
6233 : 2 : warning (OPT_Wattributes,
6234 : : "bad option %qs to attribute %<optimize%>",
6235 : 2 : decoded_options[i].orig_option_with_args_text);
6236 : 2 : }
6237 : : else
6238 : 1 : warning (OPT_Wpragmas,
6239 : : "bad option %qs to pragma %<optimize%>",
6240 : : decoded_options[i].orig_option_with_args_text);
6241 : 3 : continue;
6242 : 3 : }
6243 : 362816 : if (i != j)
6244 : 0 : decoded_options[j] = decoded_options[i];
6245 : 362816 : j++;
6246 : : }
6247 : 344075 : decoded_options_count = j;
6248 : :
6249 : : /* Merge the decoded options with save_decoded_options. */
6250 : 344075 : unsigned save_opt_count = save_opt_decoded_options->length ();
6251 : 344075 : unsigned merged_decoded_options_count
6252 : : = save_opt_count + decoded_options_count;
6253 : 344075 : cl_decoded_option *merged_decoded_options
6254 : 344075 : = XNEWVEC (cl_decoded_option, merged_decoded_options_count);
6255 : :
6256 : : /* Note the first decoded_options is used for the program name. */
6257 : 1973238 : for (unsigned i = 0; i < save_opt_count; ++i)
6258 : 1629163 : merged_decoded_options[i + 1] = (*save_opt_decoded_options)[i];
6259 : 706891 : for (unsigned i = 1; i < decoded_options_count; ++i)
6260 : 362816 : merged_decoded_options[save_opt_count + i] = decoded_options[i];
6261 : :
6262 : : /* And apply them. */
6263 : 344075 : decode_options (&global_options, &global_options_set,
6264 : : merged_decoded_options, merged_decoded_options_count,
6265 : : input_location, global_dc, NULL);
6266 : 344075 : free (decoded_options);
6267 : :
6268 : 344075 : targetm.override_options_after_change();
6269 : :
6270 : 344075 : optimize_args->truncate (0);
6271 : 344075 : return ret;
6272 : : }
6273 : :
6274 : : /* Check whether ATTR is a valid attribute fallthrough. */
6275 : :
6276 : : bool
6277 : 267918125 : attribute_fallthrough_p (tree attr)
6278 : : {
6279 : 267918125 : if (attr == error_mark_node)
6280 : : return false;
6281 : 267918107 : tree t = lookup_attribute ("", "fallthrough", attr);
6282 : 267918107 : if (t == NULL_TREE)
6283 : : return false;
6284 : 18277 : auto_urlify_attributes sentinel;
6285 : : /* It is no longer true that "this attribute shall appear at most once in
6286 : : each attribute-list", but we still give a warning. */
6287 : 18277 : if (lookup_attribute ("", "fallthrough", TREE_CHAIN (t)))
6288 : 9 : warning (OPT_Wattributes, "attribute %<fallthrough%> specified multiple "
6289 : : "times");
6290 : : /* No attribute-argument-clause shall be present. */
6291 : 18268 : else if (TREE_VALUE (t) != NULL_TREE)
6292 : 4 : warning (OPT_Wattributes, "%<fallthrough%> attribute specified with "
6293 : : "a parameter");
6294 : : /* Warn if other attributes are found. */
6295 : 36580 : for (t = attr; t != NULL_TREE; t = TREE_CHAIN (t))
6296 : : {
6297 : 18303 : tree name = get_attribute_name (t);
6298 : 18303 : if (!is_attribute_p ("fallthrough", name)
6299 : 18303 : || !is_attribute_namespace_p ("", t))
6300 : : {
6301 : 17 : if (!c_dialect_cxx () && get_attribute_namespace (t) == NULL_TREE)
6302 : : /* The specifications of standard attributes in C mean
6303 : : this is a constraint violation. */
6304 : 3 : pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
6305 : : get_attribute_name (t));
6306 : : else
6307 : 14 : warning (OPT_Wattributes, "%qE attribute ignored", name);
6308 : : }
6309 : : }
6310 : 18277 : return true;
6311 : 18277 : }
6312 : :
6313 : :
6314 : : /* Check for valid arguments being passed to a function with FNTYPE.
6315 : : There are NARGS arguments in the array ARGARRAY. LOC should be used
6316 : : for diagnostics. Return true if either -Wnonnull or -Wrestrict has
6317 : : been issued.
6318 : :
6319 : : The arguments in ARGARRAY may not have been folded yet (e.g. for C++,
6320 : : to preserve location wrappers); checks that require folded arguments
6321 : : should call fold_for_warn on them.
6322 : :
6323 : : Use the frontend-supplied COMP_TYPES when determining if
6324 : : one type is a subclass of another. */
6325 : :
6326 : : bool
6327 : 52805619 : check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype,
6328 : : int nargs, tree *argarray, vec<location_t> *arglocs,
6329 : : bool (*comp_types) (tree, tree))
6330 : : {
6331 : 52805619 : bool warned_p = false;
6332 : :
6333 : 52805619 : if (c_inhibit_evaluation_warnings)
6334 : : return warned_p;
6335 : :
6336 : : /* Check for null being passed in a pointer argument that must be
6337 : : non-null. In C++, this includes the this pointer. We also need
6338 : : to do this if format checking is enabled. */
6339 : 52206922 : if (warn_nonnull)
6340 : : {
6341 : 6406201 : nonnull_arg_ctx ctx = { loc, fndecl, fntype, 0, 0, false };
6342 : 6406201 : warned_p = check_function_nonnull (ctx, nargs, argarray);
6343 : : }
6344 : :
6345 : : /* Check for errors in format strings. */
6346 : :
6347 : 52206922 : if (warn_format || warn_suggest_attribute_format)
6348 : 6469034 : check_function_format (fndecl ? fndecl : fntype, TYPE_ATTRIBUTES (fntype), nargs,
6349 : : argarray, arglocs, comp_types);
6350 : :
6351 : 52206922 : if (warn_format)
6352 : 6392006 : check_function_sentinel (fntype, nargs, argarray);
6353 : :
6354 : 52206922 : if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
6355 : : {
6356 : 1712304 : switch (DECL_FUNCTION_CODE (fndecl))
6357 : : {
6358 : : case BUILT_IN_SPRINTF:
6359 : : case BUILT_IN_SPRINTF_CHK:
6360 : : case BUILT_IN_SNPRINTF:
6361 : : case BUILT_IN_SNPRINTF_CHK:
6362 : : /* Let the sprintf pass handle these. */
6363 : : return warned_p;
6364 : :
6365 : : default:
6366 : : break;
6367 : : }
6368 : : }
6369 : :
6370 : : /* check_function_restrict sets the DECL_READ_P for arguments
6371 : : so it must be called unconditionally. */
6372 : 52202193 : warned_p |= check_function_restrict (fndecl, fntype, nargs, argarray);
6373 : :
6374 : 52202193 : return warned_p;
6375 : : }
6376 : :
6377 : : /* Generic argument checking recursion routine. PARAM is the argument to
6378 : : be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
6379 : : once the argument is resolved. CTX is context for the callback.
6380 : : OPT is the warning for which this is done. */
6381 : : void
6382 : 725779 : check_function_arguments_recurse (void (*callback)
6383 : : (void *, tree, unsigned HOST_WIDE_INT),
6384 : : void *ctx, tree param,
6385 : : unsigned HOST_WIDE_INT param_num,
6386 : : opt_code opt)
6387 : : {
6388 : 1136626 : if (opt != OPT_Wformat_ && warning_suppressed_p (param))
6389 : : return;
6390 : :
6391 : 775111 : if (CONVERT_EXPR_P (param)
6392 : 1253953 : && (TYPE_PRECISION (TREE_TYPE (param))
6393 : 478842 : == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
6394 : : {
6395 : : /* Strip coercion. */
6396 : 820108 : check_function_arguments_recurse (callback, ctx,
6397 : 410054 : TREE_OPERAND (param, 0), param_num,
6398 : : opt);
6399 : 410054 : return;
6400 : : }
6401 : :
6402 : 724863 : if (TREE_CODE (param) == CALL_EXPR && CALL_EXPR_FN (param))
6403 : : {
6404 : 8292 : tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
6405 : 8292 : tree attrs;
6406 : 8292 : bool found_format_arg = false;
6407 : :
6408 : : /* See if this is a call to a known internationalization function
6409 : : that modifies a format arg. Such a function may have multiple
6410 : : format_arg attributes (for example, ngettext). */
6411 : :
6412 : 8292 : for (attrs = TYPE_ATTRIBUTES (type);
6413 : 8674 : attrs;
6414 : 382 : attrs = TREE_CHAIN (attrs))
6415 : 382 : if (is_attribute_p ("format_arg", get_attribute_name (attrs)))
6416 : : {
6417 : 226 : tree inner_arg;
6418 : 226 : tree format_num_expr;
6419 : 226 : int format_num;
6420 : 226 : int i;
6421 : 226 : call_expr_arg_iterator iter;
6422 : :
6423 : : /* Extract the argument number, which was previously checked
6424 : : to be valid. */
6425 : 226 : format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
6426 : :
6427 : 226 : format_num = tree_to_uhwi (format_num_expr);
6428 : :
6429 : 226 : for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
6430 : 320 : inner_arg != NULL_TREE;
6431 : 94 : inner_arg = next_call_expr_arg (&iter), i++)
6432 : 320 : if (i == format_num)
6433 : : {
6434 : 226 : check_function_arguments_recurse (callback, ctx,
6435 : : inner_arg, param_num,
6436 : : opt);
6437 : 226 : found_format_arg = true;
6438 : 226 : break;
6439 : : }
6440 : : }
6441 : :
6442 : : /* If we found a format_arg attribute and did a recursive check,
6443 : : we are done with checking this argument. Otherwise, we continue
6444 : : and this will be considered a non-literal. */
6445 : 8292 : if (found_format_arg)
6446 : : return;
6447 : : }
6448 : :
6449 : 724685 : if (TREE_CODE (param) == COND_EXPR)
6450 : : {
6451 : : /* Simplify to avoid warning for an impossible case. */
6452 : 811 : param = fold_for_warn (param);
6453 : 811 : if (TREE_CODE (param) == COND_EXPR)
6454 : : {
6455 : : /* Check both halves of the conditional expression. */
6456 : 793 : check_function_arguments_recurse (callback, ctx,
6457 : 793 : TREE_OPERAND (param, 1),
6458 : : param_num, opt);
6459 : 1586 : check_function_arguments_recurse (callback, ctx,
6460 : 793 : TREE_OPERAND (param, 2),
6461 : : param_num, opt);
6462 : 793 : return;
6463 : : }
6464 : : }
6465 : :
6466 : 723892 : (*callback) (ctx, param, param_num);
6467 : : }
6468 : :
6469 : : /* Checks for a builtin function FNDECL that the number of arguments
6470 : : NARGS against the required number REQUIRED and issues an error if
6471 : : there is a mismatch. Returns true if the number of arguments is
6472 : : correct, otherwise false. LOC is the location of FNDECL. */
6473 : :
6474 : : static bool
6475 : 773113 : builtin_function_validate_nargs (location_t loc, tree fndecl, int nargs,
6476 : : int required, bool complain)
6477 : : {
6478 : 773113 : if (nargs < required)
6479 : : {
6480 : 182 : if (complain)
6481 : 143 : error_at (loc, "too few arguments to function %qE", fndecl);
6482 : 182 : return false;
6483 : : }
6484 : 772931 : else if (nargs > required)
6485 : : {
6486 : 138 : if (complain)
6487 : 93 : error_at (loc, "too many arguments to function %qE", fndecl);
6488 : 138 : return false;
6489 : : }
6490 : : return true;
6491 : : }
6492 : :
6493 : : /* Helper macro for check_builtin_function_arguments. */
6494 : : #define ARG_LOCATION(N) \
6495 : : (arg_loc.is_empty () \
6496 : : ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
6497 : : : expansion_point_location (arg_loc[(N)]))
6498 : :
6499 : : /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
6500 : : Returns false if there was an error, otherwise true. LOC is the
6501 : : location of the function; ARG_LOC is a vector of locations of the
6502 : : arguments. If FNDECL is the result of resolving an overloaded
6503 : : target built-in, ORIG_FNDECL is the original function decl,
6504 : : otherwise it is null. */
6505 : :
6506 : : bool
6507 : 39471657 : check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
6508 : : tree fndecl, tree orig_fndecl, int nargs,
6509 : : tree *args, bool complain)
6510 : : {
6511 : 39471657 : if (!fndecl_built_in_p (fndecl))
6512 : : return true;
6513 : :
6514 : 39471657 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6515 : 32072878 : return (!targetm.check_builtin_call
6516 : 32072878 : || targetm.check_builtin_call (loc, arg_loc, fndecl, orig_fndecl,
6517 : : nargs, args, complain));
6518 : :
6519 : 7398779 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_FRONTEND)
6520 : : return true;
6521 : :
6522 : 7369387 : gcc_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL);
6523 : 7369387 : switch (DECL_FUNCTION_CODE (fndecl))
6524 : : {
6525 : 2 : case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX:
6526 : 2 : if (!tree_fits_uhwi_p (args[2]))
6527 : : {
6528 : 0 : if (complain)
6529 : 0 : error_at (
6530 : 0 : ARG_LOCATION (2),
6531 : : "third argument to function %qE must be a constant integer",
6532 : : fndecl);
6533 : 0 : return false;
6534 : : }
6535 : : /* fall through */
6536 : :
6537 : 264 : case BUILT_IN_ALLOCA_WITH_ALIGN:
6538 : 264 : {
6539 : : /* Get the requested alignment (in bits) if it's a constant
6540 : : integer expression. */
6541 : 264 : unsigned HOST_WIDE_INT align
6542 : 264 : = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
6543 : :
6544 : : /* Determine if the requested alignment is a power of 2. */
6545 : 264 : if ((align & (align - 1)))
6546 : : align = 0;
6547 : :
6548 : : /* The maximum alignment in bits corresponding to the same
6549 : : maximum in bytes enforced in check_user_alignment(). */
6550 : 315 : unsigned maxalign = (UINT_MAX >> 1) + 1;
6551 : :
6552 : : /* Reject invalid alignments. */
6553 : 181 : if (align < BITS_PER_UNIT || maxalign < align)
6554 : : {
6555 : 134 : if (complain)
6556 : 169 : error_at (ARG_LOCATION (1),
6557 : : "second argument to function %qE must be a constant "
6558 : : "integer power of 2 between %qi and %qu bits",
6559 : : fndecl, BITS_PER_UNIT, maxalign);
6560 : 134 : return false;
6561 : : }
6562 : : return true;
6563 : : }
6564 : :
6565 : 95163 : case BUILT_IN_CONSTANT_P:
6566 : 95163 : return builtin_function_validate_nargs (loc, fndecl, nargs, 1, complain);
6567 : :
6568 : 275648 : case BUILT_IN_ISFINITE:
6569 : 275648 : case BUILT_IN_ISINF:
6570 : 275648 : case BUILT_IN_ISINF_SIGN:
6571 : 275648 : case BUILT_IN_ISNAN:
6572 : 275648 : case BUILT_IN_ISNORMAL:
6573 : 275648 : case BUILT_IN_ISSIGNALING:
6574 : 275648 : case BUILT_IN_SIGNBIT:
6575 : 275648 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 1, complain))
6576 : : {
6577 : 275613 : if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
6578 : : {
6579 : 37 : if (complain)
6580 : 28 : error_at (ARG_LOCATION (0),
6581 : : "non-floating-point argument in "
6582 : : "call to function %qE",
6583 : : fndecl);
6584 : 37 : return false;
6585 : : }
6586 : : return true;
6587 : : }
6588 : : return false;
6589 : :
6590 : 228992 : case BUILT_IN_ISGREATER:
6591 : 228992 : case BUILT_IN_ISGREATEREQUAL:
6592 : 228992 : case BUILT_IN_ISLESS:
6593 : 228992 : case BUILT_IN_ISLESSEQUAL:
6594 : 228992 : case BUILT_IN_ISLESSGREATER:
6595 : 228992 : case BUILT_IN_ISUNORDERED:
6596 : 228992 : case BUILT_IN_ISEQSIG:
6597 : 228992 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 2, complain))
6598 : : {
6599 : 228957 : enum tree_code code0, code1;
6600 : 228957 : code0 = TREE_CODE (TREE_TYPE (args[0]));
6601 : 228957 : code1 = TREE_CODE (TREE_TYPE (args[1]));
6602 : 228957 : if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
6603 : 147 : || (code0 == REAL_TYPE
6604 : 56 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
6605 : 92 : || ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
6606 : 81 : && code1 == REAL_TYPE)))
6607 : : {
6608 : 39 : if (complain)
6609 : 18 : error_at (loc,
6610 : : "non-floating-point arguments in call to "
6611 : : "function %qE",
6612 : : fndecl);
6613 : 39 : return false;
6614 : : }
6615 : : return true;
6616 : : }
6617 : : return false;
6618 : :
6619 : 38861 : case BUILT_IN_FPCLASSIFY:
6620 : 38861 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 6, complain))
6621 : : {
6622 : 233094 : for (unsigned int i = 0; i < 5; i++)
6623 : 194249 : if (TREE_CODE (args[i]) != INTEGER_CST)
6624 : : {
6625 : 12 : if (complain)
6626 : 12 : error_at (ARG_LOCATION (i),
6627 : : "non-const integer argument %u in "
6628 : : "call to function %qE",
6629 : : i + 1, fndecl);
6630 : 12 : return false;
6631 : : }
6632 : :
6633 : 38845 : if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
6634 : : {
6635 : 2 : if (complain)
6636 : 4 : error_at (ARG_LOCATION (5),
6637 : : "non-floating-point argument in "
6638 : : "call to function %qE",
6639 : : fndecl);
6640 : 2 : return false;
6641 : : }
6642 : : return true;
6643 : : }
6644 : : return false;
6645 : :
6646 : 415 : case BUILT_IN_ASSUME_ALIGNED:
6647 : 810 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 2 + (nargs > 2),
6648 : : complain))
6649 : : {
6650 : 413 : if (nargs >= 3
6651 : 18 : && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE
6652 : 420 : && TREE_CODE (TREE_TYPE (args[2])) != BITINT_TYPE)
6653 : : {
6654 : 6 : if (complain)
6655 : 6 : error_at (ARG_LOCATION (2),
6656 : : "non-integer argument 3 in call to "
6657 : : "function %qE",
6658 : : fndecl);
6659 : 6 : return false;
6660 : : }
6661 : : return true;
6662 : : }
6663 : : return false;
6664 : :
6665 : 74437 : case BUILT_IN_ADD_OVERFLOW:
6666 : 74437 : case BUILT_IN_SUB_OVERFLOW:
6667 : 74437 : case BUILT_IN_MUL_OVERFLOW:
6668 : 74437 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 3, complain))
6669 : : {
6670 : : unsigned i;
6671 : 222855 : for (i = 0; i < 2; i++)
6672 : 148596 : if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
6673 : : {
6674 : 55 : if (complain)
6675 : 77 : error_at (ARG_LOCATION (i),
6676 : : "argument %u in call to function "
6677 : : "%qE does not have integral type",
6678 : : i + 1, fndecl);
6679 : 55 : return false;
6680 : : }
6681 : 74259 : if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
6682 : 74259 : || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args[2]))))
6683 : : {
6684 : 60 : if (complain)
6685 : 75 : error_at (ARG_LOCATION (2),
6686 : : "argument 3 in call to function %qE "
6687 : : "does not have pointer to integral type",
6688 : : fndecl);
6689 : 60 : return false;
6690 : : }
6691 : 74199 : else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == ENUMERAL_TYPE)
6692 : : {
6693 : 16 : if (complain)
6694 : 11 : error_at (ARG_LOCATION (2),
6695 : : "argument 3 in call to function %qE "
6696 : : "has pointer to enumerated type",
6697 : : fndecl);
6698 : 16 : return false;
6699 : : }
6700 : 74183 : else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == BOOLEAN_TYPE)
6701 : : {
6702 : 21 : if (complain)
6703 : 16 : error_at (ARG_LOCATION (2),
6704 : : "argument 3 in call to function %qE "
6705 : : "has pointer to boolean type",
6706 : : fndecl);
6707 : 21 : return false;
6708 : : }
6709 : 74162 : else if (TYPE_READONLY (TREE_TYPE (TREE_TYPE (args[2]))))
6710 : : {
6711 : 30 : if (complain)
6712 : 30 : error_at (ARG_LOCATION (2),
6713 : : "argument %u in call to function %qE "
6714 : : "has pointer to %qs type (%qT)",
6715 : 21 : 3, fndecl, "const", TREE_TYPE (args[2]));
6716 : 30 : return false;
6717 : : }
6718 : 74132 : else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (args[2]))))
6719 : : {
6720 : 6 : if (complain)
6721 : 12 : error_at (ARG_LOCATION (2),
6722 : : "argument %u in call to function %qE "
6723 : : "has pointer to %qs type (%qT)",
6724 : 6 : 3, fndecl, "_Atomic", TREE_TYPE (args[2]));
6725 : 6 : return false;
6726 : : }
6727 : : return true;
6728 : : }
6729 : : return false;
6730 : :
6731 : 41854 : case BUILT_IN_ADD_OVERFLOW_P:
6732 : 41854 : case BUILT_IN_SUB_OVERFLOW_P:
6733 : 41854 : case BUILT_IN_MUL_OVERFLOW_P:
6734 : 41854 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 3, complain))
6735 : : {
6736 : : unsigned i;
6737 : 167205 : for (i = 0; i < 3; i++)
6738 : 125421 : if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
6739 : : {
6740 : 37 : if (complain)
6741 : 35 : error_at (ARG_LOCATION (i),
6742 : : "argument %u in call to function "
6743 : : "%qE does not have integral type",
6744 : : i + 1, fndecl);
6745 : 37 : return false;
6746 : : }
6747 : 41784 : if (TREE_CODE (TREE_TYPE (args[2])) == ENUMERAL_TYPE)
6748 : : {
6749 : 13 : if (complain)
6750 : 5 : error_at (ARG_LOCATION (2),
6751 : : "argument %u in call to function "
6752 : : "%qE has enumerated type",
6753 : : 3, fndecl);
6754 : 13 : return false;
6755 : : }
6756 : 41771 : else if (TREE_CODE (TREE_TYPE (args[2])) == BOOLEAN_TYPE)
6757 : : {
6758 : 13 : if (complain)
6759 : 5 : error_at (ARG_LOCATION (2),
6760 : : "argument %u in call to function "
6761 : : "%qE has boolean type",
6762 : : 3, fndecl);
6763 : 13 : return false;
6764 : : }
6765 : : return true;
6766 : : }
6767 : : return false;
6768 : :
6769 : 10308 : case BUILT_IN_CLEAR_PADDING:
6770 : 10308 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 1, complain))
6771 : : {
6772 : 10293 : if (!POINTER_TYPE_P (TREE_TYPE (args[0])))
6773 : : {
6774 : 7 : if (complain)
6775 : 5 : error_at (ARG_LOCATION (0),
6776 : : "argument %u in call to function "
6777 : : "%qE does not have pointer type",
6778 : : 1, fndecl);
6779 : 7 : return false;
6780 : : }
6781 : 10286 : else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (args[0]))))
6782 : : {
6783 : 11 : if (complain)
6784 : 10 : error_at (ARG_LOCATION (0),
6785 : : "argument %u in call to function "
6786 : : "%qE points to incomplete type",
6787 : : 1, fndecl);
6788 : 11 : return false;
6789 : : }
6790 : 10275 : else if (TYPE_READONLY (TREE_TYPE (TREE_TYPE (args[0]))))
6791 : : {
6792 : 7 : if (complain)
6793 : 5 : error_at (ARG_LOCATION (0),
6794 : : "argument %u in call to function %qE "
6795 : : "has pointer to %qs type (%qT)",
6796 : 4 : 1, fndecl, "const", TREE_TYPE (args[0]));
6797 : 7 : return false;
6798 : : }
6799 : 10268 : else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (args[0]))))
6800 : : {
6801 : 0 : if (complain)
6802 : 0 : error_at (ARG_LOCATION (0),
6803 : : "argument %u in call to function %qE "
6804 : : "has pointer to %qs type (%qT)",
6805 : 0 : 1, fndecl, "_Atomic", TREE_TYPE (args[0]));
6806 : 0 : return false;
6807 : : }
6808 : : return true;
6809 : : }
6810 : : return false;
6811 : :
6812 : 58341 : case BUILT_IN_CLZG:
6813 : 58341 : case BUILT_IN_CTZG:
6814 : 58341 : case BUILT_IN_CLRSBG:
6815 : 58341 : case BUILT_IN_FFSG:
6816 : 58341 : case BUILT_IN_PARITYG:
6817 : 58341 : case BUILT_IN_POPCOUNTG:
6818 : 58341 : if (nargs == 2
6819 : 58341 : && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CLZG
6820 : 16448 : || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CTZG))
6821 : : {
6822 : 50906 : if (!INTEGRAL_TYPE_P (TREE_TYPE (args[1])))
6823 : : {
6824 : 18 : if (complain)
6825 : 15 : error_at (ARG_LOCATION (1),
6826 : : "argument %u in call to function "
6827 : : "%qE does not have integral type",
6828 : : 2, fndecl);
6829 : 18 : return false;
6830 : : }
6831 : 50888 : if ((TYPE_PRECISION (TREE_TYPE (args[1]))
6832 : 50888 : > TYPE_PRECISION (integer_type_node))
6833 : 50888 : || (TYPE_PRECISION (TREE_TYPE (args[1]))
6834 : 50874 : == TYPE_PRECISION (integer_type_node)
6835 : 50868 : && TYPE_UNSIGNED (TREE_TYPE (args[1]))))
6836 : : {
6837 : 28 : if (complain)
6838 : 26 : error_at (ARG_LOCATION (1),
6839 : : "argument %u in call to function "
6840 : : "%qE does not have %<int%> type",
6841 : : 2, fndecl);
6842 : 28 : return false;
6843 : : }
6844 : : }
6845 : 7435 : else if (!builtin_function_validate_nargs (loc, fndecl, nargs, 1,
6846 : : complain))
6847 : : return false;
6848 : :
6849 : 58229 : if (!INTEGRAL_TYPE_P (TREE_TYPE (args[0])))
6850 : : {
6851 : 82 : if (complain)
6852 : 80 : error_at (ARG_LOCATION (0),
6853 : : "argument %u in call to function "
6854 : : "%qE does not have integral type",
6855 : : 1, fndecl);
6856 : 82 : return false;
6857 : : }
6858 : 58147 : if (TREE_CODE (TREE_TYPE (args[0])) == ENUMERAL_TYPE)
6859 : : {
6860 : 42 : if (complain)
6861 : 24 : error_at (ARG_LOCATION (0),
6862 : : "argument %u in call to function "
6863 : : "%qE has enumerated type",
6864 : : 1, fndecl);
6865 : 42 : return false;
6866 : : }
6867 : 58105 : if (TREE_CODE (TREE_TYPE (args[0])) == BOOLEAN_TYPE)
6868 : : {
6869 : 50 : if (complain)
6870 : 40 : error_at (ARG_LOCATION (0),
6871 : : "argument %u in call to function "
6872 : : "%qE has boolean type",
6873 : : 1, fndecl);
6874 : 50 : return false;
6875 : : }
6876 : 58055 : if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FFSG
6877 : 58055 : || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CLRSBG)
6878 : : {
6879 : 288 : if (TYPE_UNSIGNED (TREE_TYPE (args[0])))
6880 : : {
6881 : 14 : if (complain)
6882 : 10 : error_at (ARG_LOCATION (0),
6883 : : "argument 1 in call to function "
6884 : : "%qE has unsigned type",
6885 : : fndecl);
6886 : 14 : return false;
6887 : : }
6888 : : }
6889 : 57767 : else if (!TYPE_UNSIGNED (TREE_TYPE (args[0])))
6890 : : {
6891 : 42 : if (complain)
6892 : 42 : error_at (ARG_LOCATION (0),
6893 : : "argument 1 in call to function "
6894 : : "%qE has signed type",
6895 : : fndecl);
6896 : 42 : return false;
6897 : : }
6898 : : return true;
6899 : :
6900 : : default:
6901 : : return true;
6902 : : }
6903 : : }
6904 : :
6905 : : /* Subroutine of c_parse_error.
6906 : : Return the result of concatenating LHS and RHS. RHS is really
6907 : : a string literal, its first character is indicated by RHS_START and
6908 : : RHS_SIZE is its length (including the terminating NUL character).
6909 : :
6910 : : The caller is responsible for deleting the returned pointer. */
6911 : :
6912 : : static char *
6913 : 8755 : catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
6914 : : {
6915 : 8755 : const size_t lhs_size = strlen (lhs);
6916 : 8755 : char *result = XNEWVEC (char, lhs_size + rhs_size);
6917 : 8755 : memcpy (result, lhs, lhs_size);
6918 : 8755 : memcpy (result + lhs_size, rhs_start, rhs_size);
6919 : 8755 : return result;
6920 : : }
6921 : :
6922 : : /* Issue the error given by GMSGID at RICHLOC, indicating that it occurred
6923 : : before TOKEN, which had the associated VALUE. */
6924 : :
6925 : : void
6926 : 8769 : c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
6927 : : tree value, unsigned char token_flags,
6928 : : rich_location *richloc)
6929 : : {
6930 : : #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
6931 : :
6932 : 8769 : char *message = NULL;
6933 : :
6934 : 8769 : if (token_type == CPP_EOF)
6935 : 472 : message = catenate_messages (gmsgid, " at end of input");
6936 : 8297 : else if (token_type == CPP_CHAR
6937 : : || token_type == CPP_WCHAR
6938 : : || token_type == CPP_CHAR16
6939 : : || token_type == CPP_CHAR32
6940 : 8297 : || token_type == CPP_UTF8CHAR)
6941 : : {
6942 : 24 : unsigned int val = TREE_INT_CST_LOW (value);
6943 : 24 : const char *prefix;
6944 : :
6945 : 24 : switch (token_type)
6946 : : {
6947 : : default:
6948 : : prefix = "";
6949 : : break;
6950 : 0 : case CPP_WCHAR:
6951 : 0 : prefix = "L";
6952 : 0 : break;
6953 : 0 : case CPP_CHAR16:
6954 : 0 : prefix = "u";
6955 : 0 : break;
6956 : 0 : case CPP_CHAR32:
6957 : 0 : prefix = "U";
6958 : 0 : break;
6959 : 0 : case CPP_UTF8CHAR:
6960 : 0 : prefix = "u8";
6961 : 0 : break;
6962 : : }
6963 : :
6964 : 24 : if (val <= UCHAR_MAX && ISGRAPH (val))
6965 : 20 : message = catenate_messages (gmsgid, " before %s'%c'");
6966 : : else
6967 : 4 : message = catenate_messages (gmsgid, " before %s'\\x%x'");
6968 : :
6969 : 24 : error_at (richloc, message, prefix, val);
6970 : 24 : free (message);
6971 : 24 : message = NULL;
6972 : : }
6973 : 8273 : else if (token_type == CPP_CHAR_USERDEF
6974 : : || token_type == CPP_WCHAR_USERDEF
6975 : : || token_type == CPP_CHAR16_USERDEF
6976 : : || token_type == CPP_CHAR32_USERDEF
6977 : 8273 : || token_type == CPP_UTF8CHAR_USERDEF)
6978 : 12 : message = catenate_messages (gmsgid,
6979 : : " before user-defined character literal");
6980 : 8261 : else if (token_type == CPP_STRING_USERDEF
6981 : : || token_type == CPP_WSTRING_USERDEF
6982 : : || token_type == CPP_STRING16_USERDEF
6983 : : || token_type == CPP_STRING32_USERDEF
6984 : 8261 : || token_type == CPP_UTF8STRING_USERDEF)
6985 : 25 : message = catenate_messages (gmsgid, " before user-defined string literal");
6986 : 8236 : else if (token_type == CPP_STRING
6987 : : || token_type == CPP_WSTRING
6988 : : || token_type == CPP_STRING16
6989 : : || token_type == CPP_STRING32
6990 : 8236 : || token_type == CPP_UTF8STRING)
6991 : 44 : message = catenate_messages (gmsgid, " before string constant");
6992 : 8192 : else if (token_type == CPP_NUMBER)
6993 : 528 : message = catenate_messages (gmsgid, " before numeric constant");
6994 : 7664 : else if (token_type == CPP_NAME)
6995 : : {
6996 : 2107 : message = catenate_messages (gmsgid, " before %qE");
6997 : 2107 : error_at (richloc, message, value);
6998 : 2107 : free (message);
6999 : 2107 : message = NULL;
7000 : : }
7001 : 5557 : else if (token_type == CPP_PRAGMA)
7002 : 20 : message = catenate_messages (gmsgid, " before %<#pragma%>");
7003 : 5537 : else if (token_type == CPP_PRAGMA_EOL)
7004 : 476 : message = catenate_messages (gmsgid, " before end of line");
7005 : 5061 : else if (token_type == CPP_DECLTYPE)
7006 : 12 : message = catenate_messages (gmsgid, " before %<decltype%>");
7007 : 5049 : else if (token_type == CPP_EMBED)
7008 : 0 : message = catenate_messages (gmsgid, " before %<#embed%>");
7009 : 5049 : else if (token_type < N_TTYPES)
7010 : : {
7011 : 5035 : message = catenate_messages (gmsgid, " before %qs token");
7012 : 5035 : error_at (richloc, message, cpp_type2name (token_type, token_flags));
7013 : 5035 : free (message);
7014 : 5035 : message = NULL;
7015 : : }
7016 : : else
7017 : 14 : error_at (richloc, gmsgid);
7018 : :
7019 : 8769 : if (message)
7020 : : {
7021 : 1589 : error_at (richloc, message);
7022 : 1589 : free (message);
7023 : : }
7024 : : #undef catenate_messages
7025 : 8769 : }
7026 : :
7027 : : /* Return the gcc option code associated with the reason for a cpp
7028 : : message, or 0 if none. */
7029 : :
7030 : : static diagnostics::option_id
7031 : 146908 : c_option_controlling_cpp_diagnostic (enum cpp_warning_reason reason)
7032 : : {
7033 : 146908 : const struct cpp_reason_option_codes_t *entry;
7034 : :
7035 : 4217335 : for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
7036 : : {
7037 : 4210351 : if (entry->reason == reason)
7038 : 139924 : return entry->option_code;
7039 : : }
7040 : 0 : return 0;
7041 : : }
7042 : :
7043 : : /* Return TRUE if the given option index corresponds to a diagnostic
7044 : : issued by libcpp. Linear search seems fine for now. */
7045 : : bool
7046 : 2137002 : c_option_is_from_cpp_diagnostics (int option_index)
7047 : : {
7048 : 50669549 : for (auto entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE;
7049 : : ++entry)
7050 : : {
7051 : 49859974 : if (entry->option_code == option_index)
7052 : : return true;
7053 : : }
7054 : : return false;
7055 : : }
7056 : :
7057 : : /* Callback from cpp_diagnostic for PFILE to print diagnostics from the
7058 : : preprocessor. The diagnostic is of type LEVEL, with REASON set
7059 : : to the reason code if LEVEL is represents a warning, at location
7060 : : RICHLOC unless this is after lexing and the compiler's location
7061 : : should be used instead; MSG is the translated message and AP
7062 : : the arguments. Returns true if a diagnostic was emitted, false
7063 : : otherwise. */
7064 : :
7065 : : bool
7066 : 146918 : c_cpp_diagnostic (cpp_reader *pfile ATTRIBUTE_UNUSED,
7067 : : enum cpp_diagnostic_level level,
7068 : : enum cpp_warning_reason reason,
7069 : : rich_location *richloc,
7070 : : const char *msg, va_list *ap)
7071 : : {
7072 : 146918 : diagnostics::diagnostic_info diagnostic;
7073 : 146918 : enum diagnostics::kind dlevel;
7074 : 146918 : bool save_warn_system_headers = global_dc->m_warn_system_headers;
7075 : 146918 : bool ret;
7076 : :
7077 : 146918 : switch (level)
7078 : : {
7079 : 90 : case CPP_DL_WARNING_SYSHDR:
7080 : 90 : if (flag_no_output)
7081 : : return false;
7082 : 89 : global_dc->m_warn_system_headers = 1;
7083 : : /* Fall through. */
7084 : 26065 : case CPP_DL_WARNING:
7085 : 26065 : if (flag_no_output)
7086 : : return false;
7087 : : dlevel = diagnostics::kind::warning;
7088 : : break;
7089 : 115727 : case CPP_DL_PEDWARN:
7090 : 115727 : if (flag_no_output && !flag_pedantic_errors)
7091 : : return false;
7092 : : dlevel = diagnostics::kind::pedwarn;
7093 : : break;
7094 : : case CPP_DL_ERROR:
7095 : : dlevel = diagnostics::kind::error;
7096 : : break;
7097 : 0 : case CPP_DL_ICE:
7098 : 0 : dlevel = diagnostics::kind::ice;
7099 : 0 : break;
7100 : 604 : case CPP_DL_NOTE:
7101 : 604 : dlevel = diagnostics::kind::note;
7102 : 604 : break;
7103 : 360 : case CPP_DL_FATAL:
7104 : 360 : dlevel = diagnostics::kind::fatal;
7105 : 360 : break;
7106 : 0 : default:
7107 : 0 : gcc_unreachable ();
7108 : : }
7109 : 146908 : if (override_libcpp_locations)
7110 : 247 : richloc->set_range (0, input_location, SHOW_RANGE_WITH_CARET);
7111 : 146908 : diagnostic_set_info_translated (&diagnostic, msg, ap,
7112 : : richloc, dlevel);
7113 : 293816 : diagnostic_set_option_id (&diagnostic,
7114 : : c_option_controlling_cpp_diagnostic (reason));
7115 : 146908 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
7116 : 146548 : if (level == CPP_DL_WARNING_SYSHDR)
7117 : 89 : global_dc->m_warn_system_headers = save_warn_system_headers;
7118 : : return ret;
7119 : 146558 : }
7120 : :
7121 : : /* Convert a character from the host to the target execution character
7122 : : set. cpplib handles this, mostly. */
7123 : :
7124 : : HOST_WIDE_INT
7125 : 6550227 : c_common_to_target_charset (HOST_WIDE_INT c)
7126 : : {
7127 : : /* Character constants in GCC proper are sign-extended under -fsigned-char,
7128 : : zero-extended under -fno-signed-char. cpplib insists that characters
7129 : : and character constants are always unsigned. Hence we must convert
7130 : : back and forth. */
7131 : 6550227 : cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
7132 : :
7133 : 6550227 : uc = cpp_host_to_exec_charset (parse_in, uc);
7134 : :
7135 : 6550227 : if (flag_signed_char)
7136 : 6550130 : return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
7137 : 6550130 : >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
7138 : : else
7139 : 97 : return uc;
7140 : : }
7141 : :
7142 : : /* Fold an offsetof-like expression. EXPR is a nested sequence of component
7143 : : references with an INDIRECT_REF of a constant at the bottom; much like the
7144 : : traditional rendering of offsetof as a macro. TYPE is the desired type of
7145 : : the whole expression. Return the folded result. */
7146 : :
7147 : : tree
7148 : 15092 : fold_offsetof (tree expr, tree type, enum tree_code ctx)
7149 : : {
7150 : 15101 : tree base, off, t;
7151 : 15101 : tree_code code = TREE_CODE (expr);
7152 : 15101 : switch (code)
7153 : : {
7154 : : case ERROR_MARK:
7155 : : return expr;
7156 : :
7157 : 12 : case VAR_DECL:
7158 : 12 : error ("cannot apply %<offsetof%> to static data member %qD", expr);
7159 : 12 : return error_mark_node;
7160 : :
7161 : 9 : case CALL_EXPR:
7162 : 9 : case TARGET_EXPR:
7163 : 9 : error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
7164 : 9 : return error_mark_node;
7165 : :
7166 : 6630 : case NOP_EXPR:
7167 : 6630 : case INDIRECT_REF:
7168 : 6630 : if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
7169 : : {
7170 : 17 : error ("cannot apply %<offsetof%> to a non constant address");
7171 : 17 : return error_mark_node;
7172 : : }
7173 : 6613 : return convert (type, TREE_OPERAND (expr, 0));
7174 : :
7175 : 7218 : case COMPONENT_REF:
7176 : 7218 : base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
7177 : 7218 : if (base == error_mark_node)
7178 : : return base;
7179 : :
7180 : 7215 : t = TREE_OPERAND (expr, 1);
7181 : 7215 : if (DECL_C_BIT_FIELD (t))
7182 : : {
7183 : 1 : error ("attempt to take address of bit-field structure "
7184 : : "member %qD", t);
7185 : 1 : return error_mark_node;
7186 : : }
7187 : 7214 : off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
7188 : 7214 : size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
7189 : : / BITS_PER_UNIT));
7190 : 7214 : break;
7191 : :
7192 : 1222 : case ARRAY_REF:
7193 : 1222 : base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
7194 : 1222 : if (base == error_mark_node)
7195 : : return base;
7196 : :
7197 : 1219 : t = TREE_OPERAND (expr, 1);
7198 : 1219 : STRIP_ANY_LOCATION_WRAPPER (t);
7199 : :
7200 : : /* Check if the offset goes beyond the upper bound of the array. */
7201 : 1219 : if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
7202 : : {
7203 : 956 : tree upbound = array_ref_up_bound (expr);
7204 : 956 : if (upbound != NULL_TREE
7205 : 844 : && TREE_CODE (upbound) == INTEGER_CST
7206 : 1800 : && !tree_int_cst_equal (upbound,
7207 : 844 : TYPE_MAX_VALUE (TREE_TYPE (upbound))))
7208 : : {
7209 : 835 : if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
7210 : 433 : upbound = size_binop (PLUS_EXPR, upbound,
7211 : : build_int_cst (TREE_TYPE (upbound), 1));
7212 : 835 : if (tree_int_cst_lt (upbound, t))
7213 : : {
7214 : 244 : tree v;
7215 : :
7216 : 244 : for (v = TREE_OPERAND (expr, 0);
7217 : 420 : TREE_CODE (v) == COMPONENT_REF;
7218 : 176 : v = TREE_OPERAND (v, 0))
7219 : 280 : if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
7220 : : == RECORD_TYPE)
7221 : : {
7222 : 220 : tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
7223 : 307 : for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
7224 : 191 : if (TREE_CODE (fld_chain) == FIELD_DECL)
7225 : : break;
7226 : :
7227 : : if (fld_chain)
7228 : : break;
7229 : : }
7230 : : /* Don't warn if the array might be considered a poor
7231 : : man's flexible array member with a very permissive
7232 : : definition thereof. */
7233 : 244 : if (TREE_CODE (v) == ARRAY_REF
7234 : 172 : || TREE_CODE (v) == COMPONENT_REF)
7235 : 176 : warning (OPT_Warray_bounds_,
7236 : : "index %E denotes an offset "
7237 : : "greater than size of %qT",
7238 : 176 : t, TREE_TYPE (TREE_OPERAND (expr, 0)));
7239 : : }
7240 : : }
7241 : : }
7242 : :
7243 : 1219 : t = convert (sizetype, t);
7244 : 1219 : off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
7245 : 1219 : break;
7246 : :
7247 : 9 : case COMPOUND_EXPR:
7248 : : /* Handle static members of volatile structs. */
7249 : 9 : t = TREE_OPERAND (expr, 1);
7250 : 9 : gcc_checking_assert (VAR_P (get_base_address (t)));
7251 : : return fold_offsetof (t, type);
7252 : :
7253 : 0 : default:
7254 : 0 : gcc_unreachable ();
7255 : : }
7256 : :
7257 : 8433 : if (!POINTER_TYPE_P (type))
7258 : 7564 : return size_binop (PLUS_EXPR, base, convert (type, off));
7259 : 869 : return fold_build_pointer_plus (base, off);
7260 : : }
7261 : :
7262 : : /* *PTYPE is an incomplete array. Complete it with a domain based on
7263 : : INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
7264 : : is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
7265 : : 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
7266 : :
7267 : : int
7268 : 546404 : complete_array_type (tree *ptype, tree initial_value, bool do_default)
7269 : : {
7270 : 546404 : tree maxindex, type, main_type, elt, unqual_elt;
7271 : 546404 : int failure = 0, quals;
7272 : 546404 : bool overflow_p = false;
7273 : :
7274 : 546404 : maxindex = size_zero_node;
7275 : 546404 : if (initial_value)
7276 : : {
7277 : 538933 : STRIP_ANY_LOCATION_WRAPPER (initial_value);
7278 : :
7279 : 538933 : if (TREE_CODE (initial_value) == STRING_CST)
7280 : : {
7281 : 434631 : int eltsize
7282 : 434631 : = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
7283 : 434631 : maxindex = size_int (TREE_STRING_LENGTH (initial_value) / eltsize
7284 : : - 1);
7285 : : }
7286 : 104302 : else if (TREE_CODE (initial_value) == CONSTRUCTOR)
7287 : : {
7288 : 103368 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
7289 : :
7290 : 103368 : if (vec_safe_is_empty (v))
7291 : : {
7292 : 404 : if (pedantic)
7293 : 68 : failure = 3;
7294 : 404 : maxindex = ssize_int (-1);
7295 : : }
7296 : : else
7297 : : {
7298 : 102964 : tree curindex;
7299 : 102964 : unsigned HOST_WIDE_INT cnt = 1;
7300 : 102964 : constructor_elt *ce;
7301 : 102964 : bool fold_p = false;
7302 : :
7303 : 102964 : if ((*v)[0].index)
7304 : 90373 : maxindex = (*v)[0].index, fold_p = true;
7305 : 102964 : if (TREE_CODE ((*v)[0].value) == RAW_DATA_CST)
7306 : 4 : cnt = 0;
7307 : :
7308 : : curindex = maxindex;
7309 : :
7310 : 17133759 : for (; vec_safe_iterate (v, cnt, &ce); cnt++)
7311 : : {
7312 : 17030795 : bool curfold_p = false;
7313 : 17030795 : if (ce->index)
7314 : 15798996 : curindex = ce->index, curfold_p = true;
7315 : 17030795 : if (!ce->index || TREE_CODE (ce->value) == RAW_DATA_CST)
7316 : : {
7317 : 1232158 : if (fold_p || curfold_p)
7318 : : {
7319 : : /* Since we treat size types now as ordinary
7320 : : unsigned types, we need an explicit overflow
7321 : : check. */
7322 : 359 : tree orig = curindex;
7323 : 359 : curindex = fold_convert (sizetype, curindex);
7324 : 359 : overflow_p |= tree_int_cst_lt (curindex, orig);
7325 : 359 : curfold_p = false;
7326 : : }
7327 : 1232158 : if (TREE_CODE (ce->value) == RAW_DATA_CST)
7328 : 359 : curindex
7329 : 718 : = size_binop (PLUS_EXPR, curindex,
7330 : : size_int (RAW_DATA_LENGTH (ce->value)
7331 : : - ((ce->index || !cnt)
7332 : : ? 1 : 0)));
7333 : : else
7334 : 1231799 : curindex = size_binop (PLUS_EXPR, curindex,
7335 : : size_one_node);
7336 : : }
7337 : 17030795 : if (tree_int_cst_lt (maxindex, curindex))
7338 : 17030795 : maxindex = curindex, fold_p = curfold_p;
7339 : : }
7340 : 102964 : if (fold_p)
7341 : : {
7342 : 90369 : tree orig = maxindex;
7343 : 90369 : maxindex = fold_convert (sizetype, maxindex);
7344 : 90369 : overflow_p |= tree_int_cst_lt (maxindex, orig);
7345 : : }
7346 : : }
7347 : : }
7348 : : else
7349 : : {
7350 : : /* Make an error message unless that happened already. */
7351 : 934 : if (initial_value != error_mark_node)
7352 : 31 : failure = 1;
7353 : : }
7354 : : }
7355 : : else
7356 : : {
7357 : 7471 : failure = 2;
7358 : 7471 : if (!do_default)
7359 : : return failure;
7360 : : }
7361 : :
7362 : 539028 : type = *ptype;
7363 : 539028 : elt = TREE_TYPE (type);
7364 : 539028 : quals = TYPE_QUALS (strip_array_types (elt));
7365 : 539028 : if (quals == 0)
7366 : : unqual_elt = elt;
7367 : : else
7368 : 507219 : unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
7369 : :
7370 : : /* Using build_distinct_type_copy and modifying things afterward instead
7371 : : of using build_array_type to create a new type preserves all of the
7372 : : TYPE_LANG_FLAG_? bits that the front end may have set. */
7373 : 539028 : main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7374 : 539028 : TREE_TYPE (main_type) = unqual_elt;
7375 : 1078056 : TYPE_DOMAIN (main_type)
7376 : 539028 : = build_range_type (TREE_TYPE (maxindex),
7377 : 539028 : build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
7378 : 539028 : TYPE_TYPELESS_STORAGE (main_type) = TYPE_TYPELESS_STORAGE (type);
7379 : 539028 : layout_type (main_type);
7380 : :
7381 : : /* Set TYPE_STRUCTURAL_EQUALITY_P early. */
7382 : 539028 : if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
7383 : 1077675 : || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
7384 : 381 : SET_TYPE_STRUCTURAL_EQUALITY (main_type);
7385 : : else
7386 : 538647 : TYPE_CANONICAL (main_type) = main_type;
7387 : :
7388 : : /* Make sure we have the canonical MAIN_TYPE. */
7389 : 539028 : hashval_t hashcode = type_hash_canon_hash (main_type);
7390 : 539028 : main_type = type_hash_canon (hashcode, main_type);
7391 : :
7392 : : /* Fix the canonical type. */
7393 : 539028 : if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
7394 : 1077675 : || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
7395 : 381 : gcc_assert (TYPE_STRUCTURAL_EQUALITY_P (main_type));
7396 : 538647 : else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
7397 : 538647 : || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
7398 : 521672 : != TYPE_DOMAIN (main_type)))
7399 : 16975 : TYPE_CANONICAL (main_type)
7400 : 33950 : = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
7401 : 16975 : TYPE_CANONICAL (TYPE_DOMAIN (main_type)),
7402 : 16975 : TYPE_TYPELESS_STORAGE (main_type));
7403 : :
7404 : 539028 : if (quals == 0)
7405 : : type = main_type;
7406 : : else
7407 : 507219 : type = c_build_qualified_type (main_type, quals);
7408 : :
7409 : 539028 : if (COMPLETE_TYPE_P (type)
7410 : 539019 : && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
7411 : 1078041 : && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
7412 : : {
7413 : 4 : error ("size of array is too large");
7414 : : /* If we proceed with the array type as it is, we'll eventually
7415 : : crash in tree_to_[su]hwi(). */
7416 : 4 : type = error_mark_node;
7417 : : }
7418 : :
7419 : 539028 : *ptype = type;
7420 : 539028 : return failure;
7421 : : }
7422 : :
7423 : : /* INIT is an constructor of a structure with a flexible array member.
7424 : : Complete the flexible array member with a domain based on it's value. */
7425 : : void
7426 : 8753206 : complete_flexible_array_elts (tree init)
7427 : : {
7428 : 8858349 : tree elt, type;
7429 : :
7430 : 8858349 : if (init == NULL_TREE || TREE_CODE (init) != CONSTRUCTOR)
7431 : : return;
7432 : :
7433 : 106669 : if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
7434 : : return;
7435 : :
7436 : 105396 : elt = CONSTRUCTOR_ELTS (init)->last ().value;
7437 : 105396 : type = TREE_TYPE (elt);
7438 : 105396 : if (TREE_CODE (type) == ARRAY_TYPE
7439 : 105396 : && TYPE_SIZE (type) == NULL_TREE)
7440 : 253 : complete_array_type (&TREE_TYPE (elt), elt, false);
7441 : : else
7442 : : complete_flexible_array_elts (elt);
7443 : : }
7444 : :
7445 : : /* Like c_mark_addressable but don't check register qualifier. */
7446 : : void
7447 : 1097828 : c_common_mark_addressable_vec (tree t)
7448 : : {
7449 : 1271072 : while (handled_component_p (t) || TREE_CODE (t) == C_MAYBE_CONST_EXPR)
7450 : : {
7451 : 173244 : if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
7452 : 26 : t = C_MAYBE_CONST_EXPR_EXPR (t);
7453 : : else
7454 : 173218 : t = TREE_OPERAND (t, 0);
7455 : : }
7456 : 1097828 : if (!VAR_P (t)
7457 : : && TREE_CODE (t) != PARM_DECL
7458 : : && TREE_CODE (t) != COMPOUND_LITERAL_EXPR
7459 : : && TREE_CODE (t) != TARGET_EXPR)
7460 : : return;
7461 : 1078547 : if (!VAR_P (t) || !DECL_HARD_REGISTER (t))
7462 : 1078478 : TREE_ADDRESSABLE (t) = 1;
7463 : 1078547 : if (TREE_CODE (t) == COMPOUND_LITERAL_EXPR)
7464 : 126 : TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (t)) = 1;
7465 : 1078421 : else if (TREE_CODE (t) == TARGET_EXPR)
7466 : 28 : TREE_ADDRESSABLE (TARGET_EXPR_SLOT (t)) = 1;
7467 : : }
7468 : :
7469 : :
7470 : :
7471 : : /* Used to help initialize the builtin-types.def table. When a type of
7472 : : the correct size doesn't exist, use error_mark_node instead of NULL.
7473 : : The later results in segfaults even when a decl using the type doesn't
7474 : : get invoked. */
7475 : :
7476 : : tree
7477 : 1126143 : builtin_type_for_size (int size, bool unsignedp)
7478 : : {
7479 : 1126143 : tree type = c_common_type_for_size (size, unsignedp);
7480 : 1126143 : return type ? type : error_mark_node;
7481 : : }
7482 : :
7483 : : /* Work out the size of the first argument of a call to
7484 : : __builtin_speculation_safe_value. Only pointers and integral types
7485 : : are permitted. Return -1 if the argument type is not supported or
7486 : : the size is too large; 0 if the argument type is a pointer or the
7487 : : size if it is integral. */
7488 : : static enum built_in_function
7489 : 154 : speculation_safe_value_resolve_call (tree function, vec<tree, va_gc> *params,
7490 : : bool complain)
7491 : : {
7492 : : /* Type of the argument. */
7493 : 154 : tree type;
7494 : 154 : int size;
7495 : :
7496 : 154 : if (vec_safe_is_empty (params))
7497 : : {
7498 : 10 : if (complain)
7499 : 10 : error ("too few arguments to function %qE", function);
7500 : 10 : return BUILT_IN_NONE;
7501 : : }
7502 : :
7503 : 144 : type = TREE_TYPE ((*params)[0]);
7504 : 144 : if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
7505 : : {
7506 : : /* Force array-to-pointer decay for C++. */
7507 : 0 : (*params)[0] = default_conversion ((*params)[0]);
7508 : 0 : type = TREE_TYPE ((*params)[0]);
7509 : : }
7510 : :
7511 : 144 : if (POINTER_TYPE_P (type))
7512 : : return BUILT_IN_SPECULATION_SAFE_VALUE_PTR;
7513 : :
7514 : 128 : if (!INTEGRAL_TYPE_P (type))
7515 : 42 : goto incompatible;
7516 : :
7517 : 86 : if (!COMPLETE_TYPE_P (type))
7518 : 0 : goto incompatible;
7519 : :
7520 : 86 : size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7521 : 86 : if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
7522 : 86 : return ((enum built_in_function)
7523 : 172 : ((int) BUILT_IN_SPECULATION_SAFE_VALUE_1 + exact_log2 (size)));
7524 : :
7525 : 0 : incompatible:
7526 : : /* Issue the diagnostic only if the argument is valid, otherwise
7527 : : it would be redundant at best and could be misleading. */
7528 : 42 : if (type != error_mark_node && complain)
7529 : 6 : error ("operand type %qT is incompatible with argument %d of %qE",
7530 : : type, 1, function);
7531 : :
7532 : : return BUILT_IN_NONE;
7533 : : }
7534 : :
7535 : : /* Validate and coerce PARAMS, the arguments to ORIG_FUNCTION to fit
7536 : : the prototype for FUNCTION. The first argument is mandatory, a second
7537 : : argument, if present, must be type compatible with the first. */
7538 : : static bool
7539 : 102 : speculation_safe_value_resolve_params (location_t loc, tree orig_function,
7540 : : vec<tree, va_gc> *params, bool complain)
7541 : : {
7542 : 102 : tree val;
7543 : :
7544 : 102 : if (params->length () == 0)
7545 : : {
7546 : 0 : if (complain)
7547 : 0 : error_at (loc, "too few arguments to function %qE", orig_function);
7548 : 0 : return false;
7549 : : }
7550 : :
7551 : 102 : else if (params->length () > 2)
7552 : : {
7553 : 21 : if (complain)
7554 : 0 : error_at (loc, "too many arguments to function %qE", orig_function);
7555 : 21 : return false;
7556 : : }
7557 : :
7558 : 81 : val = (*params)[0];
7559 : 81 : if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE)
7560 : 0 : val = default_conversion (val);
7561 : 81 : if (!(TREE_CODE (TREE_TYPE (val)) == POINTER_TYPE
7562 : 65 : || TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE))
7563 : : {
7564 : 0 : if (complain)
7565 : 0 : error_at (loc, "expecting argument of type pointer or of type integer "
7566 : : "for argument 1");
7567 : 0 : return false;
7568 : : }
7569 : 81 : (*params)[0] = val;
7570 : :
7571 : 81 : if (params->length () == 2)
7572 : : {
7573 : 32 : tree val2 = (*params)[1];
7574 : 32 : if (TREE_CODE (TREE_TYPE (val2)) == ARRAY_TYPE)
7575 : 0 : val2 = default_conversion (val2);
7576 : 32 : if (error_operand_p (val2))
7577 : : return false;
7578 : 40 : if (!(TREE_TYPE (val) == TREE_TYPE (val2)
7579 : 9 : || useless_type_conversion_p (TREE_TYPE (val), TREE_TYPE (val2))))
7580 : : {
7581 : 9 : if (complain)
7582 : 0 : error_at (loc, "both arguments must be compatible");
7583 : 9 : return false;
7584 : : }
7585 : 22 : (*params)[1] = val2;
7586 : : }
7587 : :
7588 : : return true;
7589 : : }
7590 : :
7591 : : /* Cast the result of the builtin back to the type of the first argument,
7592 : : preserving any qualifiers that it might have. */
7593 : : static tree
7594 : 71 : speculation_safe_value_resolve_return (tree first_param, tree result)
7595 : : {
7596 : 71 : tree ptype = TREE_TYPE (first_param);
7597 : 71 : tree rtype = TREE_TYPE (result);
7598 : 71 : ptype = TYPE_MAIN_VARIANT (ptype);
7599 : :
7600 : 71 : if (tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
7601 : 71 : return convert (ptype, result);
7602 : :
7603 : : return result;
7604 : : }
7605 : :
7606 : : /* A helper function for resolve_overloaded_builtin in resolving the
7607 : : overloaded __sync_ builtins. Returns a positive power of 2 if the
7608 : : first operand of PARAMS is a pointer to a supported data type.
7609 : : Returns 0 if an error is encountered. Return -1 for _BitInt
7610 : : __atomic*fetch* with unsupported type which should be handled by
7611 : : a cas loop.
7612 : : FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
7613 : : built-ins. ORIG_FORMAT is for __sync_* rather than __atomic_*
7614 : : built-ins. */
7615 : :
7616 : : static int
7617 : 323887 : sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch,
7618 : : bool orig_format, bool complain)
7619 : : {
7620 : : /* Type of the argument. */
7621 : 323887 : tree argtype;
7622 : : /* Type the argument points to. */
7623 : 323887 : tree type;
7624 : 323887 : int size;
7625 : :
7626 : 323887 : if (vec_safe_is_empty (params))
7627 : : {
7628 : 106 : if (complain)
7629 : 106 : error ("too few arguments to function %qE", function);
7630 : 106 : return 0;
7631 : : }
7632 : :
7633 : 323781 : argtype = type = TREE_TYPE ((*params)[0]);
7634 : 323781 : if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
7635 : : {
7636 : : /* Force array-to-pointer decay for C++. */
7637 : 3 : (*params)[0] = default_conversion ((*params)[0]);
7638 : 3 : type = TREE_TYPE ((*params)[0]);
7639 : : }
7640 : 323781 : if (TREE_CODE (type) != POINTER_TYPE)
7641 : 2127 : goto incompatible;
7642 : :
7643 : 321654 : type = TREE_TYPE (type);
7644 : 321654 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
7645 : 290 : goto incompatible;
7646 : :
7647 : 321364 : if (!COMPLETE_TYPE_P (type))
7648 : 2 : goto incompatible;
7649 : :
7650 : 321362 : if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
7651 : 84 : goto incompatible;
7652 : :
7653 : 321278 : size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7654 : 321278 : if (size == 16
7655 : 7679 : && TREE_CODE (type) == BITINT_TYPE
7656 : 321804 : && !targetm.scalar_mode_supported_p (TImode))
7657 : : {
7658 : 0 : if (fetch && !orig_format)
7659 : : return -1;
7660 : 0 : goto incompatible;
7661 : : }
7662 : :
7663 : 321278 : if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
7664 : : return size;
7665 : :
7666 : 528 : if (fetch && !orig_format && TREE_CODE (type) == BITINT_TYPE)
7667 : : return -1;
7668 : :
7669 : 0 : incompatible:
7670 : : /* Issue the diagnostic only if the argument is valid, otherwise
7671 : : it would be redundant at best and could be misleading. */
7672 : 2503 : if (argtype != error_mark_node && complain)
7673 : 920 : error ("operand type %qT is incompatible with argument %d of %qE",
7674 : : argtype, 1, function);
7675 : : return 0;
7676 : : }
7677 : :
7678 : : /* A helper function for resolve_overloaded_builtin. Adds casts to
7679 : : PARAMS to make arguments match up with those of FUNCTION. Drops
7680 : : the variadic arguments at the end. Returns false if some error
7681 : : was encountered; true on success. */
7682 : :
7683 : : static bool
7684 : 320750 : sync_resolve_params (location_t loc, tree orig_function, tree function,
7685 : : vec<tree, va_gc> *params, bool orig_format, bool complain)
7686 : : {
7687 : 320750 : function_args_iterator iter;
7688 : 320750 : tree ptype;
7689 : 320750 : unsigned int parmnum;
7690 : :
7691 : 320750 : function_args_iter_init (&iter, TREE_TYPE (function));
7692 : : /* We've declared the implementation functions to use "volatile void *"
7693 : : as the pointer parameter, so we shouldn't get any complaints from the
7694 : : call to check_function_arguments what ever type the user used. */
7695 : 320750 : function_args_iter_next (&iter);
7696 : 320750 : ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
7697 : 320750 : ptype = TYPE_MAIN_VARIANT (ptype);
7698 : :
7699 : : /* For the rest of the values, we need to cast these to FTYPE, so that we
7700 : : don't get warnings for passing pointer types, etc. */
7701 : 320750 : parmnum = 0;
7702 : 1799024 : while (1)
7703 : : {
7704 : 1059887 : tree val, arg_type;
7705 : :
7706 : 1059887 : arg_type = function_args_iter_cond (&iter);
7707 : : /* XXX void_type_node belies the abstraction. */
7708 : 1059887 : if (arg_type == void_type_node)
7709 : : break;
7710 : :
7711 : 739195 : ++parmnum;
7712 : 739195 : if (params->length () <= parmnum)
7713 : : {
7714 : 58 : if (complain)
7715 : 7 : error_at (loc, "too few arguments to function %qE", orig_function);
7716 : 58 : return false;
7717 : : }
7718 : :
7719 : : /* Only convert parameters if arg_type is unsigned integer type with
7720 : : new format sync routines, i.e. don't attempt to convert pointer
7721 : : arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
7722 : : bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
7723 : : kinds). */
7724 : 739137 : if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
7725 : : {
7726 : : /* Ideally for the first conversion we'd use convert_for_assignment
7727 : : so that we get warnings for anything that doesn't match the pointer
7728 : : type. This isn't portable across the C and C++ front ends atm. */
7729 : 205061 : val = (*params)[parmnum];
7730 : 205061 : val = convert (ptype, val);
7731 : 205061 : val = convert (arg_type, val);
7732 : 205061 : (*params)[parmnum] = val;
7733 : : }
7734 : :
7735 : 739137 : function_args_iter_next (&iter);
7736 : 739137 : }
7737 : :
7738 : : /* __atomic routines are not variadic. */
7739 : 320692 : if (!orig_format && params->length () != parmnum + 1)
7740 : : {
7741 : 106 : if (complain)
7742 : 103 : error_at (loc, "too many arguments to function %qE", orig_function);
7743 : 106 : return false;
7744 : : }
7745 : :
7746 : : /* The definition of these primitives is variadic, with the remaining
7747 : : being "an optional list of variables protected by the memory barrier".
7748 : : No clue what that's supposed to mean, precisely, but we consider all
7749 : : call-clobbered variables to be protected so we're safe. */
7750 : 320586 : params->truncate (parmnum + 1);
7751 : :
7752 : 320586 : return true;
7753 : : }
7754 : :
7755 : : /* A helper function for resolve_overloaded_builtin. Adds a cast to
7756 : : RESULT to make it match the type of the first pointer argument in
7757 : : PARAMS. */
7758 : :
7759 : : static tree
7760 : 212105 : sync_resolve_return (tree first_param, tree result, bool orig_format)
7761 : : {
7762 : 212105 : tree ptype = TREE_TYPE (TREE_TYPE (first_param));
7763 : 212105 : tree rtype = TREE_TYPE (result);
7764 : 212105 : ptype = TYPE_MAIN_VARIANT (ptype);
7765 : :
7766 : : /* New format doesn't require casting unless the types are the same size. */
7767 : 212105 : if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
7768 : 212105 : return convert (ptype, result);
7769 : : else
7770 : : return result;
7771 : : }
7772 : :
7773 : : /* This function verifies the PARAMS to generic atomic FUNCTION.
7774 : : It returns the size if all the parameters are the same size, otherwise
7775 : : 0 is returned if the parameters are invalid. */
7776 : :
7777 : : static int
7778 : 83418 : get_atomic_generic_size (location_t loc, tree function,
7779 : : vec<tree, va_gc> *params, bool complain)
7780 : : {
7781 : 83418 : unsigned int n_param;
7782 : 83418 : unsigned int n_model;
7783 : 83418 : unsigned int outputs = 0; // bitset of output parameters
7784 : 83418 : unsigned int x;
7785 : 83418 : int size_0;
7786 : 83418 : tree type_0;
7787 : :
7788 : : /* Determine the parameter makeup. */
7789 : 83418 : switch (DECL_FUNCTION_CODE (function))
7790 : : {
7791 : : case BUILT_IN_ATOMIC_EXCHANGE:
7792 : : n_param = 4;
7793 : : n_model = 1;
7794 : : outputs = 5;
7795 : : break;
7796 : 50165 : case BUILT_IN_ATOMIC_LOAD:
7797 : 50165 : n_param = 3;
7798 : 50165 : n_model = 1;
7799 : 50165 : outputs = 2;
7800 : 50165 : break;
7801 : 11501 : case BUILT_IN_ATOMIC_STORE:
7802 : 11501 : n_param = 3;
7803 : 11501 : n_model = 1;
7804 : 11501 : outputs = 1;
7805 : 11501 : break;
7806 : 21098 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7807 : 21098 : n_param = 6;
7808 : 21098 : n_model = 2;
7809 : 21098 : outputs = 3;
7810 : 21098 : break;
7811 : 0 : default:
7812 : 0 : gcc_unreachable ();
7813 : : }
7814 : :
7815 : 83418 : if (vec_safe_length (params) != n_param)
7816 : : {
7817 : 466 : if (complain)
7818 : 247 : error_at (loc, "incorrect number of arguments to function %qE",
7819 : : function);
7820 : 466 : return 0;
7821 : : }
7822 : :
7823 : : /* Get type of first parameter, and determine its size. */
7824 : 82952 : type_0 = TREE_TYPE ((*params)[0]);
7825 : 82952 : if (TREE_CODE (type_0) == ARRAY_TYPE && c_dialect_cxx ())
7826 : : {
7827 : : /* Force array-to-pointer decay for C++. */
7828 : 15 : (*params)[0] = default_conversion ((*params)[0]);
7829 : 15 : type_0 = TREE_TYPE ((*params)[0]);
7830 : : }
7831 : 82952 : if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
7832 : : {
7833 : 152 : if (complain)
7834 : 41 : error_at (loc, "argument 1 of %qE must be a non-void pointer type",
7835 : : function);
7836 : 152 : return 0;
7837 : : }
7838 : :
7839 : 82800 : if (!COMPLETE_TYPE_P (TREE_TYPE (type_0)))
7840 : : {
7841 : 23 : if (complain)
7842 : 8 : error_at (loc, "argument 1 of %qE must be a pointer to a complete type",
7843 : : function);
7844 : 23 : return 0;
7845 : : }
7846 : :
7847 : : /* Types must be compile time constant sizes. */
7848 : 82777 : if (!tree_fits_uhwi_p ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))))
7849 : : {
7850 : 1 : if (complain)
7851 : 1 : error_at (loc,
7852 : : "argument 1 of %qE must be a pointer to a constant size type",
7853 : : function);
7854 : 1 : return 0;
7855 : : }
7856 : :
7857 : 82776 : size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
7858 : :
7859 : : /* Zero size objects are not allowed. */
7860 : 82776 : if (size_0 == 0)
7861 : : {
7862 : 22 : if (complain)
7863 : 7 : error_at (
7864 : : loc, "argument 1 of %qE must be a pointer to a nonzero size object",
7865 : : function);
7866 : 22 : return 0;
7867 : : }
7868 : :
7869 : : /* Check each other parameter is a pointer and the same size. */
7870 : 290425 : for (x = 0; x < n_param - n_model; x++)
7871 : : {
7872 : 207810 : int size;
7873 : 207810 : tree type = TREE_TYPE ((*params)[x]);
7874 : : /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
7875 : 207810 : if (n_param == 6 && x == 3)
7876 : 20920 : continue;
7877 : 186890 : if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
7878 : : {
7879 : : /* Force array-to-pointer decay for C++. */
7880 : 18 : (*params)[x] = default_conversion ((*params)[x]);
7881 : 18 : type = TREE_TYPE ((*params)[x]);
7882 : : }
7883 : 186890 : if (!POINTER_TYPE_P (type))
7884 : : {
7885 : 16 : if (complain)
7886 : 13 : error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
7887 : : function);
7888 : 16 : return 0;
7889 : : }
7890 : 186874 : else if (TYPE_SIZE_UNIT (TREE_TYPE (type))
7891 : 186874 : && TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type))))
7892 : : != INTEGER_CST)
7893 : : {
7894 : 1 : if (complain)
7895 : 1 : error_at (loc,
7896 : : "argument %d of %qE must be a pointer to a constant "
7897 : : "size type",
7898 : : x + 1, function);
7899 : 1 : return 0;
7900 : : }
7901 : 186873 : else if (FUNCTION_POINTER_TYPE_P (type))
7902 : : {
7903 : 10 : if (complain)
7904 : 7 : error_at (loc,
7905 : : "argument %d of %qE must not be a pointer to a "
7906 : : "function",
7907 : : x + 1, function);
7908 : 10 : return 0;
7909 : : }
7910 : 186863 : tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
7911 : 186863 : size = type_size ? tree_to_uhwi (type_size) : 0;
7912 : 186863 : if (size != size_0)
7913 : : {
7914 : 34 : if (complain)
7915 : 25 : error_at (loc, "size mismatch in argument %d of %qE", x + 1,
7916 : : function);
7917 : 34 : return 0;
7918 : : }
7919 : :
7920 : 186829 : {
7921 : 186829 : auto_diagnostic_group d;
7922 : 186829 : int quals = TYPE_QUALS (TREE_TYPE (type));
7923 : : /* Must not write to an argument of a const-qualified type. */
7924 : 186829 : if (outputs & (1 << x) && quals & TYPE_QUAL_CONST)
7925 : : {
7926 : 57 : if (c_dialect_cxx ())
7927 : : {
7928 : 45 : if (complain)
7929 : 42 : error_at (loc,
7930 : : "argument %d of %qE must not be a pointer to "
7931 : : "a %<const%> type",
7932 : : x + 1, function);
7933 : 45 : return 0;
7934 : : }
7935 : : else
7936 : 12 : pedwarn (loc, OPT_Wdiscarded_qualifiers, "argument %d "
7937 : : "of %qE discards %<const%> qualifier", x + 1,
7938 : : function);
7939 : : }
7940 : : /* Only the first argument is allowed to be volatile. */
7941 : 186784 : if (x > 0 && quals & TYPE_QUAL_VOLATILE)
7942 : : {
7943 : 44 : if (c_dialect_cxx ())
7944 : : {
7945 : 33 : if (complain)
7946 : 30 : error_at (loc,
7947 : : "argument %d of %qE must not be a pointer to "
7948 : : "a %<volatile%> type",
7949 : : x + 1, function);
7950 : 33 : return 0;
7951 : : }
7952 : : else
7953 : 11 : pedwarn (loc, OPT_Wdiscarded_qualifiers, "argument %d "
7954 : : "of %qE discards %<volatile%> qualifier", x + 1,
7955 : : function);
7956 : : }
7957 : 186829 : }
7958 : : }
7959 : :
7960 : : /* Check memory model parameters for validity. */
7961 : 186134 : for (x = n_param - n_model ; x < n_param; x++)
7962 : : {
7963 : 103535 : tree p = (*params)[x];
7964 : 103535 : if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
7965 : : {
7966 : 10 : if (complain)
7967 : 7 : error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
7968 : : function);
7969 : 10 : return 0;
7970 : : }
7971 : 103525 : p = fold_for_warn (p);
7972 : 103525 : if (TREE_CODE (p) == INTEGER_CST)
7973 : : {
7974 : : /* memmodel_base masks the low 16 bits, thus ignore any bits above
7975 : : it by using TREE_INT_CST_LOW instead of tree_to_*hwi. Those high
7976 : : bits will be checked later during expansion in target specific
7977 : : way. */
7978 : 79318 : if (memmodel_base (TREE_INT_CST_LOW (p)) >= MEMMODEL_LAST)
7979 : : {
7980 : 20 : if (complain)
7981 : 14 : warning_at (loc, OPT_Winvalid_memory_model,
7982 : : "invalid memory model argument %d of %qE", x + 1,
7983 : : function);
7984 : : else
7985 : : return 0;
7986 : : }
7987 : : }
7988 : : }
7989 : :
7990 : : return size_0;
7991 : : }
7992 : :
7993 : :
7994 : : /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
7995 : : at the beginning of the parameter list PARAMS representing the size of the
7996 : : objects. This is to match the library ABI requirement. LOC is the location
7997 : : of the function call.
7998 : : The new function is returned if it needed rebuilding, otherwise NULL_TREE is
7999 : : returned to allow the external call to be constructed. */
8000 : :
8001 : : static tree
8002 : 4146 : add_atomic_size_parameter (unsigned n, location_t loc, tree function,
8003 : : vec<tree, va_gc> *params)
8004 : : {
8005 : 4146 : tree size_node;
8006 : :
8007 : : /* Insert a SIZE_T parameter as the first param. If there isn't
8008 : : enough space, allocate a new vector and recursively re-build with that. */
8009 : 4146 : if (!params->space (1))
8010 : : {
8011 : 33 : unsigned int z, len;
8012 : 33 : vec<tree, va_gc> *v;
8013 : 33 : tree f;
8014 : :
8015 : 33 : len = params->length ();
8016 : 33 : vec_alloc (v, len + 1);
8017 : 33 : v->quick_push (build_int_cst (size_type_node, n));
8018 : 165 : for (z = 0; z < len; z++)
8019 : 132 : v->quick_push ((*params)[z]);
8020 : 33 : f = build_function_call_vec (loc, vNULL, function, v, NULL);
8021 : 33 : vec_free (v);
8022 : 33 : return f;
8023 : : }
8024 : :
8025 : : /* Add the size parameter and leave as a function call for processing. */
8026 : 4113 : size_node = build_int_cst (size_type_node, n);
8027 : 4113 : params->quick_insert (0, size_node);
8028 : 4113 : return NULL_TREE;
8029 : : }
8030 : :
8031 : :
8032 : : /* Return whether atomic operations for naturally aligned N-byte
8033 : : arguments are supported, whether inline or through libatomic. */
8034 : : static bool
8035 : 82599 : atomic_size_supported_p (int n)
8036 : : {
8037 : 82599 : switch (n)
8038 : : {
8039 : : case 1:
8040 : : case 2:
8041 : : case 4:
8042 : : case 8:
8043 : : return true;
8044 : :
8045 : 6972 : case 16:
8046 : 6972 : return targetm.scalar_mode_supported_p (TImode);
8047 : :
8048 : 4146 : default:
8049 : 4146 : return false;
8050 : : }
8051 : : }
8052 : :
8053 : : /* This will process an __atomic_exchange function call, determine whether it
8054 : : needs to be mapped to the _N variation, or turned into a library call.
8055 : : LOC is the location of the builtin call.
8056 : : FUNCTION is the DECL that has been invoked;
8057 : : PARAMS is the argument list for the call. The return value is non-null
8058 : : TRUE is returned if it is translated into the proper format for a call to the
8059 : : external library, and NEW_RETURN is set the tree for that function.
8060 : : FALSE is returned if processing for the _N variation is required, and
8061 : : NEW_RETURN is set to the return value the result is copied into. */
8062 : : static bool
8063 : 654 : resolve_overloaded_atomic_exchange (location_t loc, tree function,
8064 : : vec<tree, va_gc> *params, tree *new_return,
8065 : : bool complain)
8066 : : {
8067 : 654 : tree p0, p1, p2, p3;
8068 : 654 : tree I_type, I_type_ptr;
8069 : 654 : int n = get_atomic_generic_size (loc, function, params, complain);
8070 : :
8071 : : /* Size of 0 is an error condition. */
8072 : 654 : if (n == 0)
8073 : : {
8074 : 195 : *new_return = error_mark_node;
8075 : 195 : return true;
8076 : : }
8077 : :
8078 : : /* If not a lock-free size, change to the library generic format. */
8079 : 459 : if (!atomic_size_supported_p (n))
8080 : : {
8081 : 39 : *new_return = add_atomic_size_parameter (n, loc, function, params);
8082 : 39 : return true;
8083 : : }
8084 : :
8085 : : /* Otherwise there is a lockfree match, transform the call from:
8086 : : void fn(T* mem, T* desired, T* return, model)
8087 : : into
8088 : : *return = (T) (fn (In* mem, (In) *desired, model)) */
8089 : :
8090 : 420 : p0 = (*params)[0];
8091 : 420 : p1 = (*params)[1];
8092 : 420 : p2 = (*params)[2];
8093 : 420 : p3 = (*params)[3];
8094 : :
8095 : : /* Create pointer to appropriate size. */
8096 : 420 : I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
8097 : 420 : I_type_ptr = build_pointer_type (I_type);
8098 : :
8099 : : /* Convert object pointer to required type. */
8100 : 420 : p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
8101 : 420 : (*params)[0] = p0;
8102 : : /* Convert new value to required type, and dereference it.
8103 : : If *p1 type can have padding or may involve floating point which
8104 : : could e.g. be promoted to wider precision and demoted afterwards,
8105 : : state of padding bits might not be preserved. */
8106 : 420 : build_indirect_ref (loc, p1, RO_UNARY_STAR);
8107 : 420 : p1 = build2_loc (loc, MEM_REF, I_type,
8108 : : build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1),
8109 : 420 : build_zero_cst (TREE_TYPE (p1)));
8110 : 420 : (*params)[1] = p1;
8111 : :
8112 : : /* Move memory model to the 3rd position, and end param list. */
8113 : 420 : (*params)[2] = p3;
8114 : 420 : params->truncate (3);
8115 : :
8116 : : /* Convert return pointer and dereference it for later assignment. */
8117 : 420 : *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
8118 : :
8119 : 420 : return false;
8120 : : }
8121 : :
8122 : : /* This will process an __atomic_compare_exchange function call, determine
8123 : : whether it needs to be mapped to the _N variation, or turned into a lib call.
8124 : : LOC is the location of the builtin call.
8125 : : FUNCTION is the DECL that has been invoked;
8126 : : PARAMS is the argument list for the call. The return value is non-null
8127 : : TRUE is returned if it is translated into the proper format for a call to the
8128 : : external library, and NEW_RETURN is set the tree for that function.
8129 : : FALSE is returned if processing for the _N variation is required. */
8130 : :
8131 : : static bool
8132 : 21098 : resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
8133 : : vec<tree, va_gc> *params,
8134 : : tree *new_return, bool complain)
8135 : : {
8136 : 21098 : tree p0, p1, p2;
8137 : 21098 : tree I_type, I_type_ptr;
8138 : 21098 : int n = get_atomic_generic_size (loc, function, params, complain);
8139 : :
8140 : : /* Size of 0 is an error condition. */
8141 : 21098 : if (n == 0)
8142 : : {
8143 : 178 : *new_return = error_mark_node;
8144 : 178 : return true;
8145 : : }
8146 : :
8147 : : /* If not a lock-free size, change to the library generic format. */
8148 : 20920 : if (!atomic_size_supported_p (n))
8149 : : {
8150 : : /* The library generic format does not have the weak parameter, so
8151 : : remove it from the param list. Since a parameter has been removed,
8152 : : we can be sure that there is room for the SIZE_T parameter, meaning
8153 : : there will not be a recursive rebuilding of the parameter list, so
8154 : : there is no danger this will be done twice. */
8155 : 1198 : if (n > 0)
8156 : : {
8157 : 1198 : (*params)[3] = (*params)[4];
8158 : 1198 : (*params)[4] = (*params)[5];
8159 : 1198 : params->truncate (5);
8160 : : }
8161 : 1198 : *new_return = add_atomic_size_parameter (n, loc, function, params);
8162 : 1198 : return true;
8163 : : }
8164 : :
8165 : : /* Otherwise, there is a match, so the call needs to be transformed from:
8166 : : bool fn(T* mem, T* desired, T* return, weak, success, failure)
8167 : : into
8168 : : bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
8169 : :
8170 : 19722 : p0 = (*params)[0];
8171 : 19722 : p1 = (*params)[1];
8172 : 19722 : p2 = (*params)[2];
8173 : :
8174 : : /* Create pointer to appropriate size. */
8175 : 19722 : I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
8176 : 19722 : I_type_ptr = build_pointer_type (I_type);
8177 : :
8178 : : /* Convert object pointer to required type. */
8179 : 19722 : p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
8180 : 19722 : (*params)[0] = p0;
8181 : :
8182 : : /* Convert expected pointer to required type. */
8183 : 19722 : p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
8184 : 19722 : (*params)[1] = p1;
8185 : :
8186 : : /* Convert desired value to required type, and dereference it.
8187 : : If *p2 type can have padding or may involve floating point which
8188 : : could e.g. be promoted to wider precision and demoted afterwards,
8189 : : state of padding bits might not be preserved. */
8190 : 19722 : build_indirect_ref (loc, p2, RO_UNARY_STAR);
8191 : 19722 : p2 = build2_loc (loc, MEM_REF, I_type,
8192 : : build1 (VIEW_CONVERT_EXPR, I_type_ptr, p2),
8193 : 19722 : build_zero_cst (TREE_TYPE (p2)));
8194 : 19722 : (*params)[2] = p2;
8195 : :
8196 : : /* The rest of the parameters are fine. NULL means no special return value
8197 : : processing.*/
8198 : 19722 : *new_return = NULL;
8199 : 19722 : return false;
8200 : : }
8201 : :
8202 : : /* This will process an __atomic_load function call, determine whether it
8203 : : needs to be mapped to the _N variation, or turned into a library call.
8204 : : LOC is the location of the builtin call.
8205 : : FUNCTION is the DECL that has been invoked;
8206 : : PARAMS is the argument list for the call. The return value is non-null
8207 : : TRUE is returned if it is translated into the proper format for a call to the
8208 : : external library, and NEW_RETURN is set the tree for that function.
8209 : : FALSE is returned if processing for the _N variation is required, and
8210 : : NEW_RETURN is set to the return value the result is copied into. */
8211 : :
8212 : : static bool
8213 : 50165 : resolve_overloaded_atomic_load (location_t loc, tree function,
8214 : : vec<tree, va_gc> *params, tree *new_return,
8215 : : bool complain)
8216 : : {
8217 : 50165 : tree p0, p1, p2;
8218 : 50165 : tree I_type, I_type_ptr;
8219 : 50165 : int n = get_atomic_generic_size (loc, function, params, complain);
8220 : :
8221 : : /* Size of 0 is an error condition. */
8222 : 50165 : if (n == 0)
8223 : : {
8224 : 278 : *new_return = error_mark_node;
8225 : 278 : return true;
8226 : : }
8227 : :
8228 : : /* If not a lock-free size, change to the library generic format. */
8229 : 49887 : if (!atomic_size_supported_p (n))
8230 : : {
8231 : 2499 : *new_return = add_atomic_size_parameter (n, loc, function, params);
8232 : 2499 : return true;
8233 : : }
8234 : :
8235 : : /* Otherwise, there is a match, so the call needs to be transformed from:
8236 : : void fn(T* mem, T* return, model)
8237 : : into
8238 : : *return = (T) (fn ((In *) mem, model)) */
8239 : :
8240 : 47388 : p0 = (*params)[0];
8241 : 47388 : p1 = (*params)[1];
8242 : 47388 : p2 = (*params)[2];
8243 : :
8244 : : /* Create pointer to appropriate size. */
8245 : 47388 : I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
8246 : 47388 : I_type_ptr = build_pointer_type (I_type);
8247 : :
8248 : : /* Convert object pointer to required type. */
8249 : 47388 : p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
8250 : 47388 : (*params)[0] = p0;
8251 : :
8252 : : /* Move memory model to the 2nd position, and end param list. */
8253 : 47388 : (*params)[1] = p2;
8254 : 47388 : params->truncate (2);
8255 : :
8256 : : /* Convert return pointer and dereference it for later assignment. */
8257 : 47388 : *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
8258 : :
8259 : 47388 : return false;
8260 : : }
8261 : :
8262 : : /* This will process an __atomic_store function call, determine whether it
8263 : : needs to be mapped to the _N variation, or turned into a library call.
8264 : : LOC is the location of the builtin call.
8265 : : FUNCTION is the DECL that has been invoked;
8266 : : PARAMS is the argument list for the call. The return value is non-null
8267 : : TRUE is returned if it is translated into the proper format for a call to the
8268 : : external library, and NEW_RETURN is set the tree for that function.
8269 : : FALSE is returned if processing for the _N variation is required, and
8270 : : NEW_RETURN is set to the return value the result is copied into. */
8271 : :
8272 : : static bool
8273 : 11501 : resolve_overloaded_atomic_store (location_t loc, tree function,
8274 : : vec<tree, va_gc> *params, tree *new_return,
8275 : : bool complain)
8276 : : {
8277 : 11501 : tree p0, p1;
8278 : 11501 : tree I_type, I_type_ptr;
8279 : 11501 : int n = get_atomic_generic_size (loc, function, params, complain);
8280 : :
8281 : : /* Size of 0 is an error condition. */
8282 : 11501 : if (n == 0)
8283 : : {
8284 : 168 : *new_return = error_mark_node;
8285 : 168 : return true;
8286 : : }
8287 : :
8288 : : /* If not a lock-free size, change to the library generic format. */
8289 : 11333 : if (!atomic_size_supported_p (n))
8290 : : {
8291 : 410 : *new_return = add_atomic_size_parameter (n, loc, function, params);
8292 : 410 : return true;
8293 : : }
8294 : :
8295 : : /* Otherwise, there is a match, so the call needs to be transformed from:
8296 : : void fn(T* mem, T* value, model)
8297 : : into
8298 : : fn ((In *) mem, (In) *value, model) */
8299 : :
8300 : 10923 : p0 = (*params)[0];
8301 : 10923 : p1 = (*params)[1];
8302 : :
8303 : : /* Create pointer to appropriate size. */
8304 : 10923 : I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
8305 : 10923 : I_type_ptr = build_pointer_type (I_type);
8306 : :
8307 : : /* Convert object pointer to required type. */
8308 : 10923 : p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
8309 : 10923 : (*params)[0] = p0;
8310 : :
8311 : : /* Convert new value to required type, and dereference it. */
8312 : 10923 : p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
8313 : 10923 : p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
8314 : 10923 : (*params)[1] = p1;
8315 : :
8316 : : /* The memory model is in the right spot already. Return is void. */
8317 : 10923 : *new_return = NULL_TREE;
8318 : :
8319 : 10923 : return false;
8320 : : }
8321 : :
8322 : : /* Emit __atomic*fetch* on _BitInt which doesn't have a size of
8323 : : 1, 2, 4, 8 or 16 bytes using __atomic_compare_exchange loop.
8324 : : ORIG_CODE is the DECL_FUNCTION_CODE of ORIG_FUNCTION and
8325 : : ORIG_PARAMS arguments of the call. */
8326 : :
8327 : : static tree
8328 : 528 : atomic_bitint_fetch_using_cas_loop (location_t loc,
8329 : : enum built_in_function orig_code,
8330 : : tree orig_function,
8331 : : vec<tree, va_gc> *orig_params)
8332 : : {
8333 : 528 : enum tree_code code = ERROR_MARK;
8334 : 528 : bool return_old_p = false;
8335 : 528 : switch (orig_code)
8336 : : {
8337 : 0 : case BUILT_IN_ATOMIC_ADD_FETCH_N:
8338 : 0 : code = PLUS_EXPR;
8339 : 0 : break;
8340 : 1 : case BUILT_IN_ATOMIC_SUB_FETCH_N:
8341 : 1 : code = MINUS_EXPR;
8342 : 1 : break;
8343 : 0 : case BUILT_IN_ATOMIC_AND_FETCH_N:
8344 : 0 : code = BIT_AND_EXPR;
8345 : 0 : break;
8346 : : case BUILT_IN_ATOMIC_NAND_FETCH_N:
8347 : : break;
8348 : 1 : case BUILT_IN_ATOMIC_XOR_FETCH_N:
8349 : 1 : code = BIT_XOR_EXPR;
8350 : 1 : break;
8351 : 0 : case BUILT_IN_ATOMIC_OR_FETCH_N:
8352 : 0 : code = BIT_IOR_EXPR;
8353 : 0 : break;
8354 : 106 : case BUILT_IN_ATOMIC_FETCH_ADD_N:
8355 : 106 : code = PLUS_EXPR;
8356 : 106 : return_old_p = true;
8357 : 106 : break;
8358 : 104 : case BUILT_IN_ATOMIC_FETCH_SUB_N:
8359 : 104 : code = MINUS_EXPR;
8360 : 104 : return_old_p = true;
8361 : 104 : break;
8362 : 105 : case BUILT_IN_ATOMIC_FETCH_AND_N:
8363 : 105 : code = BIT_AND_EXPR;
8364 : 105 : return_old_p = true;
8365 : 105 : break;
8366 : 0 : case BUILT_IN_ATOMIC_FETCH_NAND_N:
8367 : 0 : return_old_p = true;
8368 : 0 : break;
8369 : 104 : case BUILT_IN_ATOMIC_FETCH_XOR_N:
8370 : 104 : code = BIT_XOR_EXPR;
8371 : 104 : return_old_p = true;
8372 : 104 : break;
8373 : 106 : case BUILT_IN_ATOMIC_FETCH_OR_N:
8374 : 106 : code = BIT_IOR_EXPR;
8375 : 106 : return_old_p = true;
8376 : 106 : break;
8377 : 0 : default:
8378 : 0 : gcc_unreachable ();
8379 : : }
8380 : :
8381 : 528 : if (orig_params->length () != 3)
8382 : : {
8383 : 0 : if (orig_params->length () < 3)
8384 : 0 : error_at (loc, "too few arguments to function %qE", orig_function);
8385 : : else
8386 : 0 : error_at (loc, "too many arguments to function %qE", orig_function);
8387 : 0 : return error_mark_node;
8388 : : }
8389 : :
8390 : 528 : tree stmts = push_stmt_list ();
8391 : :
8392 : 528 : tree nonatomic_lhs_type = TREE_TYPE (TREE_TYPE ((*orig_params)[0]));
8393 : 528 : nonatomic_lhs_type = TYPE_MAIN_VARIANT (nonatomic_lhs_type);
8394 : 528 : gcc_assert (TREE_CODE (nonatomic_lhs_type) == BITINT_TYPE);
8395 : :
8396 : 528 : tree lhs_addr = (*orig_params)[0];
8397 : 528 : tree val = convert (nonatomic_lhs_type, (*orig_params)[1]);
8398 : 528 : tree model = convert (integer_type_node, (*orig_params)[2]);
8399 : 528 : if (!c_dialect_cxx ())
8400 : : {
8401 : 528 : lhs_addr = c_fully_fold (lhs_addr, false, NULL);
8402 : 528 : val = c_fully_fold (val, false, NULL);
8403 : 528 : model = c_fully_fold (model, false, NULL);
8404 : : }
8405 : 528 : if (TREE_SIDE_EFFECTS (lhs_addr))
8406 : : {
8407 : 1 : tree var = create_tmp_var_raw (TREE_TYPE (lhs_addr));
8408 : 1 : lhs_addr = build4 (TARGET_EXPR, TREE_TYPE (lhs_addr), var, lhs_addr,
8409 : : NULL_TREE, NULL_TREE);
8410 : 1 : add_stmt (lhs_addr);
8411 : : }
8412 : 528 : if (TREE_SIDE_EFFECTS (val))
8413 : : {
8414 : 1 : tree var = create_tmp_var_raw (nonatomic_lhs_type);
8415 : 1 : val = build4 (TARGET_EXPR, nonatomic_lhs_type, var, val, NULL_TREE,
8416 : : NULL_TREE);
8417 : 1 : add_stmt (val);
8418 : : }
8419 : 528 : if (TREE_SIDE_EFFECTS (model))
8420 : : {
8421 : 1 : tree var = create_tmp_var_raw (integer_type_node);
8422 : 1 : model = build4 (TARGET_EXPR, integer_type_node, var, model, NULL_TREE,
8423 : : NULL_TREE);
8424 : 1 : add_stmt (model);
8425 : : }
8426 : :
8427 : 528 : tree old = create_tmp_var_raw (nonatomic_lhs_type);
8428 : 528 : tree old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
8429 : 528 : TREE_ADDRESSABLE (old) = 1;
8430 : 528 : suppress_warning (old);
8431 : :
8432 : 528 : tree newval = create_tmp_var_raw (nonatomic_lhs_type);
8433 : 528 : tree newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
8434 : 528 : TREE_ADDRESSABLE (newval) = 1;
8435 : 528 : suppress_warning (newval);
8436 : :
8437 : 528 : tree loop_decl = create_artificial_label (loc);
8438 : 528 : tree loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
8439 : :
8440 : 528 : tree done_decl = create_artificial_label (loc);
8441 : 528 : tree done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
8442 : :
8443 : 528 : vec<tree, va_gc> *params;
8444 : 528 : vec_alloc (params, 6);
8445 : :
8446 : : /* __atomic_load (addr, &old, SEQ_CST). */
8447 : 528 : tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
8448 : 528 : params->quick_push (lhs_addr);
8449 : 528 : params->quick_push (old_addr);
8450 : 528 : params->quick_push (build_int_cst (integer_type_node, MEMMODEL_RELAXED));
8451 : 528 : tree func_call = resolve_overloaded_builtin (loc, fndecl, params);
8452 : 528 : if (func_call == NULL_TREE)
8453 : 528 : func_call = build_function_call_vec (loc, vNULL, fndecl, params, NULL);
8454 : 528 : old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
8455 : : NULL_TREE);
8456 : 528 : add_stmt (old);
8457 : 528 : params->truncate (0);
8458 : :
8459 : : /* loop: */
8460 : 528 : add_stmt (loop_label);
8461 : :
8462 : : /* newval = old + val; */
8463 : 528 : tree rhs;
8464 : 528 : switch (code)
8465 : : {
8466 : 211 : case PLUS_EXPR:
8467 : 211 : case MINUS_EXPR:
8468 : 211 : if (!TYPE_OVERFLOW_WRAPS (nonatomic_lhs_type))
8469 : : {
8470 : 211 : tree utype
8471 : 211 : = build_bitint_type (TYPE_PRECISION (nonatomic_lhs_type), 1);
8472 : 211 : rhs = convert (nonatomic_lhs_type,
8473 : : build2_loc (loc, code, utype,
8474 : : convert (utype, old),
8475 : : convert (utype, val)));
8476 : : }
8477 : : else
8478 : 0 : rhs = build2_loc (loc, code, nonatomic_lhs_type, old, val);
8479 : : break;
8480 : 316 : case BIT_AND_EXPR:
8481 : 316 : case BIT_IOR_EXPR:
8482 : 316 : case BIT_XOR_EXPR:
8483 : 316 : rhs = build2_loc (loc, code, nonatomic_lhs_type, old, val);
8484 : 316 : break;
8485 : 1 : case ERROR_MARK:
8486 : 1 : rhs = build2_loc (loc, BIT_AND_EXPR, nonatomic_lhs_type,
8487 : : build1_loc (loc, BIT_NOT_EXPR,
8488 : : nonatomic_lhs_type, old), val);
8489 : 1 : break;
8490 : 0 : default:
8491 : 0 : gcc_unreachable ();
8492 : : }
8493 : 528 : rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
8494 : : NULL_TREE);
8495 : 528 : SET_EXPR_LOCATION (rhs, loc);
8496 : 528 : add_stmt (rhs);
8497 : :
8498 : : /* if (__atomic_compare_exchange (addr, &old, &new, false, model, model))
8499 : : goto done; */
8500 : 528 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
8501 : 528 : params->quick_push (lhs_addr);
8502 : 528 : params->quick_push (old_addr);
8503 : 528 : params->quick_push (newval_addr);
8504 : 528 : params->quick_push (integer_zero_node);
8505 : 528 : params->quick_push (model);
8506 : 528 : if (tree_fits_uhwi_p (model)
8507 : 527 : && (tree_to_uhwi (model) == MEMMODEL_RELEASE
8508 : 447 : || tree_to_uhwi (model) == MEMMODEL_ACQ_REL))
8509 : 129 : params->quick_push (build_int_cst (integer_type_node, MEMMODEL_RELAXED));
8510 : : else
8511 : 399 : params->quick_push (model);
8512 : 528 : func_call = resolve_overloaded_builtin (loc, fndecl, params);
8513 : 528 : if (func_call == NULL_TREE)
8514 : 528 : func_call = build_function_call_vec (loc, vNULL, fndecl, params, NULL);
8515 : :
8516 : 528 : tree goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
8517 : 528 : SET_EXPR_LOCATION (goto_stmt, loc);
8518 : :
8519 : 528 : tree stmt
8520 : 528 : = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
8521 : 528 : SET_EXPR_LOCATION (stmt, loc);
8522 : 528 : add_stmt (stmt);
8523 : :
8524 : : /* goto loop; */
8525 : 528 : goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
8526 : 528 : SET_EXPR_LOCATION (goto_stmt, loc);
8527 : 528 : add_stmt (goto_stmt);
8528 : :
8529 : : /* done: */
8530 : 528 : add_stmt (done_label);
8531 : :
8532 : 528 : tree ret = create_tmp_var_raw (nonatomic_lhs_type);
8533 : 531 : stmt = build2_loc (loc, MODIFY_EXPR, void_type_node, ret,
8534 : : return_old_p ? old : newval);
8535 : 528 : add_stmt (stmt);
8536 : :
8537 : : /* Finish the compound statement. */
8538 : 528 : stmts = pop_stmt_list (stmts);
8539 : :
8540 : 528 : return build4 (TARGET_EXPR, nonatomic_lhs_type, ret, stmts, NULL_TREE,
8541 : 528 : NULL_TREE);
8542 : : }
8543 : :
8544 : :
8545 : : /* Some builtin functions are placeholders for other expressions. This
8546 : : function should be called immediately after parsing the call expression
8547 : : before surrounding code has committed to the type of the expression.
8548 : :
8549 : : LOC is the location of the builtin call.
8550 : :
8551 : : FUNCTION is the DECL that has been invoked; it is known to be a builtin.
8552 : : PARAMS is the argument list for the call. The return value is non-null
8553 : : when expansion is complete, and null if normal processing should
8554 : : continue. */
8555 : :
8556 : : tree
8557 : 57128542 : resolve_overloaded_builtin (location_t loc, tree function,
8558 : : vec<tree, va_gc> *params, bool complain)
8559 : : {
8560 : : /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
8561 : : Those are not valid to call with a pointer to _Bool (or C++ bool)
8562 : : and so must be rejected. */
8563 : 57128542 : bool fetch_op = true;
8564 : 57128542 : bool orig_format = true;
8565 : 57128542 : tree new_return = NULL_TREE;
8566 : :
8567 : 57128542 : switch (DECL_BUILT_IN_CLASS (function))
8568 : : {
8569 : 8907266 : case BUILT_IN_NORMAL:
8570 : 8907266 : break;
8571 : 32072951 : case BUILT_IN_MD:
8572 : 32072951 : if (targetm.resolve_overloaded_builtin)
8573 : 0 : return targetm.resolve_overloaded_builtin (loc, function, params,
8574 : 0 : complain);
8575 : : else
8576 : : return NULL_TREE;
8577 : : default:
8578 : : return NULL_TREE;
8579 : : }
8580 : :
8581 : : /* Handle BUILT_IN_NORMAL here. */
8582 : 8907266 : enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
8583 : 8907266 : switch (orig_code)
8584 : : {
8585 : 154 : case BUILT_IN_SPECULATION_SAFE_VALUE_N:
8586 : 154 : {
8587 : 154 : tree new_function, first_param, result;
8588 : 154 : enum built_in_function fncode
8589 : 154 : = speculation_safe_value_resolve_call (function, params, complain);
8590 : :
8591 : 154 : if (fncode == BUILT_IN_NONE)
8592 : 52 : return error_mark_node;
8593 : :
8594 : 102 : first_param = (*params)[0];
8595 : 102 : if (!speculation_safe_value_resolve_params (loc, function, params,
8596 : : complain))
8597 : 31 : return error_mark_node;
8598 : :
8599 : 71 : if (targetm.have_speculation_safe_value (true))
8600 : : {
8601 : 71 : new_function = builtin_decl_explicit (fncode);
8602 : 71 : result = build_function_call_vec (loc, vNULL, new_function, params,
8603 : : NULL);
8604 : :
8605 : 71 : if (result == error_mark_node)
8606 : : return result;
8607 : :
8608 : 71 : return speculation_safe_value_resolve_return (first_param, result);
8609 : : }
8610 : : else
8611 : : {
8612 : : /* This target doesn't have, or doesn't need, active mitigation
8613 : : against incorrect speculative execution. Simply return the
8614 : : first parameter to the builtin. */
8615 : 0 : if (!targetm.have_speculation_safe_value (false))
8616 : : {
8617 : 0 : if (complain)
8618 : : /* The user has invoked __builtin_speculation_safe_value
8619 : : even though __HAVE_SPECULATION_SAFE_VALUE is not
8620 : : defined: emit a warning. */
8621 : 0 : warning_at (
8622 : 0 : input_location, 0,
8623 : : "this target does not define a speculation barrier; "
8624 : : "your program will still execute correctly, "
8625 : : "but incorrect speculation may not be "
8626 : : "restricted");
8627 : : else
8628 : 0 : return error_mark_node;
8629 : : }
8630 : :
8631 : : /* If the optional second argument is present, handle any side
8632 : : effects now. */
8633 : 0 : if (params->length () == 2
8634 : 0 : && TREE_SIDE_EFFECTS ((*params)[1]))
8635 : 0 : return build2 (COMPOUND_EXPR, TREE_TYPE (first_param),
8636 : 0 : (*params)[1], first_param);
8637 : :
8638 : : return first_param;
8639 : : }
8640 : : }
8641 : :
8642 : 83418 : case BUILT_IN_ATOMIC_EXCHANGE:
8643 : 83418 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
8644 : 83418 : case BUILT_IN_ATOMIC_LOAD:
8645 : 83418 : case BUILT_IN_ATOMIC_STORE:
8646 : 83418 : {
8647 : : /* Handle these 4 together so that they can fall through to the next
8648 : : case if the call is transformed to an _N variant. */
8649 : 83418 : switch (orig_code)
8650 : : {
8651 : 654 : case BUILT_IN_ATOMIC_EXCHANGE:
8652 : 654 : {
8653 : 654 : if (resolve_overloaded_atomic_exchange (loc, function, params,
8654 : : &new_return, complain))
8655 : 234 : return new_return;
8656 : : /* Change to the _N variant. */
8657 : : orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
8658 : : break;
8659 : : }
8660 : :
8661 : 21098 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
8662 : 21098 : {
8663 : 21098 : if (resolve_overloaded_atomic_compare_exchange (
8664 : : loc, function, params, &new_return, complain))
8665 : 1376 : return new_return;
8666 : : /* Change to the _N variant. */
8667 : : orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
8668 : : break;
8669 : : }
8670 : 50165 : case BUILT_IN_ATOMIC_LOAD:
8671 : 50165 : {
8672 : 50165 : if (resolve_overloaded_atomic_load (loc, function, params,
8673 : : &new_return, complain))
8674 : 2777 : return new_return;
8675 : : /* Change to the _N variant. */
8676 : : orig_code = BUILT_IN_ATOMIC_LOAD_N;
8677 : : break;
8678 : : }
8679 : 11501 : case BUILT_IN_ATOMIC_STORE:
8680 : 11501 : {
8681 : 11501 : if (resolve_overloaded_atomic_store (loc, function, params,
8682 : : &new_return, complain))
8683 : 578 : return new_return;
8684 : : /* Change to the _N variant. */
8685 : : orig_code = BUILT_IN_ATOMIC_STORE_N;
8686 : : break;
8687 : : }
8688 : 0 : default:
8689 : 0 : gcc_unreachable ();
8690 : : }
8691 : : }
8692 : : /* FALLTHRU */
8693 : : case BUILT_IN_ATOMIC_EXCHANGE_N:
8694 : : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
8695 : : case BUILT_IN_ATOMIC_LOAD_N:
8696 : : case BUILT_IN_ATOMIC_STORE_N:
8697 : : fetch_op = false;
8698 : : /* FALLTHRU */
8699 : : case BUILT_IN_ATOMIC_ADD_FETCH_N:
8700 : : case BUILT_IN_ATOMIC_SUB_FETCH_N:
8701 : : case BUILT_IN_ATOMIC_AND_FETCH_N:
8702 : : case BUILT_IN_ATOMIC_NAND_FETCH_N:
8703 : : case BUILT_IN_ATOMIC_XOR_FETCH_N:
8704 : : case BUILT_IN_ATOMIC_OR_FETCH_N:
8705 : : case BUILT_IN_ATOMIC_FETCH_ADD_N:
8706 : : case BUILT_IN_ATOMIC_FETCH_SUB_N:
8707 : : case BUILT_IN_ATOMIC_FETCH_AND_N:
8708 : : case BUILT_IN_ATOMIC_FETCH_NAND_N:
8709 : : case BUILT_IN_ATOMIC_FETCH_XOR_N:
8710 : : case BUILT_IN_ATOMIC_FETCH_OR_N:
8711 : : orig_format = false;
8712 : : /* FALLTHRU */
8713 : : case BUILT_IN_SYNC_FETCH_AND_ADD_N:
8714 : : case BUILT_IN_SYNC_FETCH_AND_SUB_N:
8715 : : case BUILT_IN_SYNC_FETCH_AND_OR_N:
8716 : : case BUILT_IN_SYNC_FETCH_AND_AND_N:
8717 : : case BUILT_IN_SYNC_FETCH_AND_XOR_N:
8718 : : case BUILT_IN_SYNC_FETCH_AND_NAND_N:
8719 : : case BUILT_IN_SYNC_ADD_AND_FETCH_N:
8720 : : case BUILT_IN_SYNC_SUB_AND_FETCH_N:
8721 : : case BUILT_IN_SYNC_OR_AND_FETCH_N:
8722 : : case BUILT_IN_SYNC_AND_AND_FETCH_N:
8723 : : case BUILT_IN_SYNC_XOR_AND_FETCH_N:
8724 : : case BUILT_IN_SYNC_NAND_AND_FETCH_N:
8725 : : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
8726 : : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
8727 : : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
8728 : : case BUILT_IN_SYNC_LOCK_RELEASE_N:
8729 : : {
8730 : : /* The following are not _FETCH_OPs and must be accepted with
8731 : : pointers to _Bool (or C++ bool). */
8732 : : if (fetch_op)
8733 : 68108 : fetch_op = (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
8734 : 68108 : && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
8735 : 67622 : && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
8736 : 68108 : && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
8737 : :
8738 : 323887 : int n = sync_resolve_size (function, params, fetch_op, orig_format,
8739 : : complain);
8740 : 323887 : tree new_function, first_param, result;
8741 : 323887 : enum built_in_function fncode;
8742 : :
8743 : 323887 : if (n == 0)
8744 : 2609 : return error_mark_node;
8745 : :
8746 : 321278 : if (n == -1)
8747 : : {
8748 : : /* complain is related to SFINAE context.
8749 : : _BitInt is not defined in C++, hence can't enter this clause
8750 : : with complain unset. Even if at the abstraction level
8751 : : this complain is unset that still makes sense (whether
8752 : : this function should report an error or not if anything is
8753 : : wrong).
8754 : : Since can't test avoiding an error when this value is false not
8755 : : writing the code and instead asserting value is not set. */
8756 : 528 : gcc_assert (complain);
8757 : 528 : return atomic_bitint_fetch_using_cas_loop (loc, orig_code, function,
8758 : 528 : params);
8759 : : }
8760 : :
8761 : 320750 : fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
8762 : 320750 : new_function = builtin_decl_explicit (fncode);
8763 : 320750 : if (!sync_resolve_params (loc, function, new_function, params,
8764 : : orig_format, complain))
8765 : 164 : return error_mark_node;
8766 : :
8767 : 320586 : first_param = (*params)[0];
8768 : 320586 : result = build_function_call_vec (loc, vNULL, new_function, params,
8769 : : NULL);
8770 : 320586 : if (result == error_mark_node)
8771 : : return result;
8772 : 320574 : if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
8773 : 320574 : && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
8774 : 320267 : && orig_code != BUILT_IN_ATOMIC_STORE_N
8775 : 320267 : && orig_code != BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N)
8776 : 212105 : result = sync_resolve_return (first_param, result, orig_format);
8777 : :
8778 : 320574 : if (fetch_op)
8779 : : /* Prevent -Wunused-value warning. */
8780 : 64601 : TREE_USED (result) = true;
8781 : :
8782 : : /* If new_return is set, assign function to that expr and cast the
8783 : : result to void since the generic interface returned void. */
8784 : 320574 : if (new_return)
8785 : : {
8786 : : /* Cast function result from I{1,2,4,8,16} to the required type. */
8787 : 47808 : if (TREE_CODE (TREE_TYPE (new_return)) == BITINT_TYPE)
8788 : : {
8789 : 599 : struct bitint_info info;
8790 : 599 : unsigned prec = TYPE_PRECISION (TREE_TYPE (new_return));
8791 : 599 : targetm.c.bitint_type_info (prec, &info);
8792 : 599 : if (!info.extended)
8793 : : /* For _BitInt which has the padding bits undefined
8794 : : convert to the _BitInt type rather than VCE to force
8795 : : zero or sign extension. */
8796 : 599 : result = build1 (NOP_EXPR, TREE_TYPE (new_return), result);
8797 : : }
8798 : 47808 : result
8799 : 47808 : = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
8800 : 47808 : result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
8801 : : result);
8802 : 47808 : TREE_SIDE_EFFECTS (result) = 1;
8803 : 47808 : protected_set_expr_location (result, loc);
8804 : 47808 : result = convert (void_type_node, result);
8805 : : }
8806 : : return result;
8807 : : }
8808 : :
8809 : : default:
8810 : : return NULL_TREE;
8811 : : }
8812 : : }
8813 : :
8814 : : /* vector_types_compatible_elements_p is used in type checks of vectors
8815 : : values used as operands of binary operators. Where it returns true, and
8816 : : the other checks of the caller succeed (being vector types in he first
8817 : : place, and matching number of elements), we can just treat the types
8818 : : as essentially the same.
8819 : : Contrast with vector_targets_convertible_p, which is used for vector
8820 : : pointer types, and vector_types_convertible_p, which will allow
8821 : : language-specific matches under the control of flag_lax_vector_conversions,
8822 : : and might still require a conversion. */
8823 : : /* True if vector types T1 and T2 can be inputs to the same binary
8824 : : operator without conversion.
8825 : : We don't check the overall vector size here because some of our callers
8826 : : want to give different error messages when the vectors are compatible
8827 : : except for the element count. */
8828 : :
8829 : : bool
8830 : 1591783 : vector_types_compatible_elements_p (tree t1, tree t2)
8831 : : {
8832 : 3183290 : bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
8833 : 1591783 : t1 = TREE_TYPE (t1);
8834 : 1591783 : t2 = TREE_TYPE (t2);
8835 : :
8836 : 1591783 : enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
8837 : :
8838 : 1591783 : gcc_assert ((INTEGRAL_TYPE_P (t1)
8839 : : || c1 == REAL_TYPE
8840 : : || c1 == FIXED_POINT_TYPE)
8841 : : && (INTEGRAL_TYPE_P (t2)
8842 : : || c2 == REAL_TYPE
8843 : : || c2 == FIXED_POINT_TYPE));
8844 : :
8845 : 1591783 : t1 = c_common_signed_type (t1);
8846 : 1591783 : t2 = c_common_signed_type (t2);
8847 : : /* Equality works here because c_common_signed_type uses
8848 : : TYPE_MAIN_VARIANT. */
8849 : 1591783 : if (t1 == t2)
8850 : : return true;
8851 : 31 : if (opaque && c1 == c2
8852 : 4 : && (INTEGRAL_TYPE_P (t1) || c1 == REAL_TYPE)
8853 : 35 : && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
8854 : 4 : return true;
8855 : : return false;
8856 : : }
8857 : :
8858 : : /* Check for missing format attributes on function pointers. LTYPE is
8859 : : the new type or left-hand side type. RTYPE is the old type or
8860 : : right-hand side type. Returns TRUE if LTYPE is missing the desired
8861 : : attribute. */
8862 : :
8863 : : bool
8864 : 9930 : check_missing_format_attribute (tree ltype, tree rtype)
8865 : : {
8866 : 9930 : tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
8867 : 9930 : tree ra;
8868 : :
8869 : 9939 : for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
8870 : 73 : if (is_attribute_p ("format", get_attribute_name (ra)))
8871 : : break;
8872 : 9930 : if (ra)
8873 : : {
8874 : 64 : tree la;
8875 : 64 : for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
8876 : 24 : if (is_attribute_p ("format", get_attribute_name (la)))
8877 : : break;
8878 : 64 : return !la;
8879 : : }
8880 : : else
8881 : : return false;
8882 : : }
8883 : :
8884 : : /* Setup a TYPE_DECL node as a typedef representation.
8885 : :
8886 : : X is a TYPE_DECL for a typedef statement. Create a brand new
8887 : : ..._TYPE node (which will be just a variant of the existing
8888 : : ..._TYPE node with identical properties) and then install X
8889 : : as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
8890 : :
8891 : : The whole point here is to end up with a situation where each
8892 : : and every ..._TYPE node the compiler creates will be uniquely
8893 : : associated with AT MOST one node representing a typedef name.
8894 : : This way, even though the compiler substitutes corresponding
8895 : : ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
8896 : : early on, later parts of the compiler can always do the reverse
8897 : : translation and get back the corresponding typedef name. For
8898 : : example, given:
8899 : :
8900 : : typedef struct S MY_TYPE;
8901 : : MY_TYPE object;
8902 : :
8903 : : Later parts of the compiler might only know that `object' was of
8904 : : type `struct S' if it were not for code just below. With this
8905 : : code however, later parts of the compiler see something like:
8906 : :
8907 : : struct S' == struct S
8908 : : typedef struct S' MY_TYPE;
8909 : : struct S' object;
8910 : :
8911 : : And they can then deduce (from the node for type struct S') that
8912 : : the original object declaration was:
8913 : :
8914 : : MY_TYPE object;
8915 : :
8916 : : Being able to do this is important for proper support of protoize,
8917 : : and also for generating precise symbolic debugging information
8918 : : which takes full account of the programmer's (typedef) vocabulary.
8919 : :
8920 : : Obviously, we don't want to generate a duplicate ..._TYPE node if
8921 : : the TYPE_DECL node that we are now processing really represents a
8922 : : standard built-in type. */
8923 : :
8924 : : void
8925 : 172517639 : set_underlying_type (tree x)
8926 : : {
8927 : 172517639 : if (x == error_mark_node || TREE_TYPE (x) == error_mark_node)
8928 : : return;
8929 : 172517611 : if (DECL_IS_UNDECLARED_BUILTIN (x) && TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
8930 : : {
8931 : 6364701 : if (TYPE_NAME (TREE_TYPE (x)) == 0)
8932 : 5084952 : TYPE_NAME (TREE_TYPE (x)) = x;
8933 : : }
8934 : 166152910 : else if (DECL_ORIGINAL_TYPE (x))
8935 : 163039 : gcc_checking_assert (TYPE_NAME (TREE_TYPE (x)) == x);
8936 : : else
8937 : : {
8938 : 165989871 : tree tt = TREE_TYPE (x);
8939 : 165989871 : DECL_ORIGINAL_TYPE (x) = tt;
8940 : 165989871 : tt = build_variant_type_copy (tt);
8941 : 165989871 : TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
8942 : 165989871 : TYPE_NAME (tt) = x;
8943 : :
8944 : : /* Mark the type as used only when its type decl is decorated
8945 : : with attribute unused. */
8946 : 165989871 : if (lookup_attribute ("unused", DECL_ATTRIBUTES (x)))
8947 : 254 : TREE_USED (tt) = 1;
8948 : :
8949 : 165989871 : TREE_TYPE (x) = tt;
8950 : : }
8951 : : }
8952 : :
8953 : : /* Return true if it is worth exposing the DECL_ORIGINAL_TYPE of TYPE to
8954 : : the user in diagnostics, false if it would be better to use TYPE itself.
8955 : : TYPE is known to satisfy typedef_variant_p. */
8956 : :
8957 : : bool
8958 : 31366 : user_facing_original_type_p (const_tree type)
8959 : : {
8960 : 31366 : gcc_assert (typedef_variant_p (type));
8961 : 31366 : tree decl = TYPE_NAME (type);
8962 : :
8963 : : /* Look through any typedef in "user" code. */
8964 : 31366 : if (!DECL_IN_SYSTEM_HEADER (decl) && !DECL_IS_UNDECLARED_BUILTIN (decl))
8965 : : return true;
8966 : :
8967 : : /* If the original type is also named and is in the user namespace,
8968 : : assume it too is a user-facing type. */
8969 : 1576 : tree orig_type = DECL_ORIGINAL_TYPE (decl);
8970 : 1576 : if (tree orig_id = TYPE_IDENTIFIER (orig_type))
8971 : 1448 : if (!name_reserved_for_implementation_p (IDENTIFIER_POINTER (orig_id)))
8972 : : return true;
8973 : :
8974 : 698 : switch (TREE_CODE (orig_type))
8975 : : {
8976 : : /* Don't look through to an anonymous vector type, since the syntax
8977 : : we use for them in diagnostics isn't real C or C++ syntax.
8978 : : And if ORIG_TYPE is named but in the implementation namespace,
8979 : : TYPE is likely to be more meaningful to the user. */
8980 : : case VECTOR_TYPE:
8981 : : return false;
8982 : :
8983 : : /* Don't expose anonymous tag types that are presumably meant to be
8984 : : known by their typedef name. Also don't expose tags that are in
8985 : : the implementation namespace, such as:
8986 : :
8987 : : typedef struct __foo foo; */
8988 : : case RECORD_TYPE:
8989 : : case UNION_TYPE:
8990 : : case ENUMERAL_TYPE:
8991 : : return false;
8992 : :
8993 : : /* Look through to anything else. */
8994 : : default:
8995 : : return true;
8996 : : }
8997 : : }
8998 : :
8999 : : /* Record the types used by the current global variable declaration
9000 : : being parsed, so that we can decide later to emit their debug info.
9001 : : Those types are in types_used_by_cur_var_decl, and we are going to
9002 : : store them in the types_used_by_vars_hash hash table.
9003 : : DECL is the declaration of the global variable that has been parsed. */
9004 : :
9005 : : void
9006 : 23329064 : record_types_used_by_current_var_decl (tree decl)
9007 : : {
9008 : 23329064 : gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
9009 : :
9010 : 44785365 : while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
9011 : : {
9012 : 21456301 : tree type = types_used_by_cur_var_decl->pop ();
9013 : 21456301 : types_used_by_var_decl_insert (type, decl);
9014 : : }
9015 : 23329064 : }
9016 : :
9017 : : /* The C and C++ parsers both use vectors to hold function arguments.
9018 : : For efficiency, we keep a cache of unused vectors. This is the
9019 : : cache. */
9020 : :
9021 : : typedef vec<tree, va_gc> *tree_gc_vec;
9022 : : static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
9023 : :
9024 : : /* Return a new vector from the cache. If the cache is empty,
9025 : : allocate a new vector. These vectors are GC'ed, so it is OK if the
9026 : : pointer is not released.. */
9027 : :
9028 : : vec<tree, va_gc> *
9029 : 1030971076 : make_tree_vector (void)
9030 : : {
9031 : 1030971076 : if (tree_vector_cache && !tree_vector_cache->is_empty ())
9032 : 1002602160 : return tree_vector_cache->pop ();
9033 : : else
9034 : : {
9035 : : /* Passing 0 to vec::alloc returns NULL, and our callers require
9036 : : that we always return a non-NULL value. The vector code uses
9037 : : 4 when growing a NULL vector, so we do too. */
9038 : 28368916 : vec<tree, va_gc> *v;
9039 : 28368916 : vec_alloc (v, 4);
9040 : 28368916 : return v;
9041 : : }
9042 : : }
9043 : :
9044 : : /* Release a vector of trees back to the cache. */
9045 : :
9046 : : void
9047 : 1395094748 : release_tree_vector (vec<tree, va_gc> *vec)
9048 : : {
9049 : 1395094748 : if (vec != NULL)
9050 : : {
9051 : 1004188795 : if (vec->allocated () >= 16)
9052 : : /* Don't cache vecs that have expanded more than once. On a p64
9053 : : target, vecs double in alloc size with each power of 2 elements, e.g
9054 : : at 16 elements the alloc increases from 128 to 256 bytes. */
9055 : 779699 : vec_free (vec);
9056 : : else
9057 : : {
9058 : 1003409096 : vec->truncate (0);
9059 : 1003409096 : vec_safe_push (tree_vector_cache, vec);
9060 : : }
9061 : : }
9062 : 1395094748 : }
9063 : :
9064 : : /* Get a new tree vector holding a single tree. */
9065 : :
9066 : : vec<tree, va_gc> *
9067 : 49598649 : make_tree_vector_single (tree t)
9068 : : {
9069 : 49598649 : vec<tree, va_gc> *ret = make_tree_vector ();
9070 : 49598649 : ret->quick_push (t);
9071 : 49598649 : return ret;
9072 : : }
9073 : :
9074 : : /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
9075 : :
9076 : : vec<tree, va_gc> *
9077 : 6806 : make_tree_vector_from_list (tree list)
9078 : : {
9079 : 6806 : vec<tree, va_gc> *ret = make_tree_vector ();
9080 : 18186 : for (; list; list = TREE_CHAIN (list))
9081 : 11380 : vec_safe_push (ret, TREE_VALUE (list));
9082 : 6806 : return ret;
9083 : : }
9084 : :
9085 : : /* Append to a tree vector V the values of a CONSTRUCTOR CTOR
9086 : : and return the new possibly reallocated vector. */
9087 : :
9088 : : vec<tree, va_gc> *
9089 : 1389597 : append_ctor_to_tree_vector (vec<tree, va_gc> *v, tree ctor)
9090 : : {
9091 : 2613496 : unsigned nelts = vec_safe_length (v) + CONSTRUCTOR_NELTS (ctor);
9092 : 1389597 : vec_safe_reserve (v, CONSTRUCTOR_NELTS (ctor));
9093 : 3435704 : for (unsigned i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
9094 : 2046107 : if (TREE_CODE (CONSTRUCTOR_ELT (ctor, i)->value) == RAW_DATA_CST)
9095 : : {
9096 : 24 : tree raw_data = CONSTRUCTOR_ELT (ctor, i)->value;
9097 : 24 : nelts += RAW_DATA_LENGTH (raw_data) - 1;
9098 : 24 : vec_safe_reserve (v, nelts - v->length ());
9099 : 48 : if (TYPE_PRECISION (TREE_TYPE (raw_data)) > CHAR_BIT
9100 : 24 : || TYPE_UNSIGNED (TREE_TYPE (raw_data)))
9101 : 6264 : for (unsigned j = 0; j < (unsigned) RAW_DATA_LENGTH (raw_data); ++j)
9102 : 6240 : v->quick_push (build_int_cst (TREE_TYPE (raw_data),
9103 : 6240 : RAW_DATA_UCHAR_ELT (raw_data, j)));
9104 : : else
9105 : 0 : for (unsigned j = 0; j < (unsigned) RAW_DATA_LENGTH (raw_data); ++j)
9106 : 0 : v->quick_push (build_int_cst (TREE_TYPE (raw_data),
9107 : 0 : RAW_DATA_SCHAR_ELT (raw_data, j)));
9108 : : }
9109 : : else
9110 : 2046083 : v->quick_push (CONSTRUCTOR_ELT (ctor, i)->value);
9111 : 1389597 : return v;
9112 : : }
9113 : :
9114 : : /* Get a new tree vector of the values of a CONSTRUCTOR. */
9115 : :
9116 : : vec<tree, va_gc> *
9117 : 5152 : make_tree_vector_from_ctor (tree ctor)
9118 : : {
9119 : 5152 : vec<tree,va_gc> *ret
9120 : 5152 : = CONSTRUCTOR_NELTS (ctor) <= 16 ? make_tree_vector () : NULL;
9121 : 5152 : return append_ctor_to_tree_vector (ret, ctor);
9122 : : }
9123 : :
9124 : : /* Get a new tree vector which is a copy of an existing one. */
9125 : :
9126 : : vec<tree, va_gc> *
9127 : 28325435 : make_tree_vector_copy (const vec<tree, va_gc> *orig)
9128 : : {
9129 : 28325435 : vec<tree, va_gc> *ret;
9130 : 28325435 : unsigned int ix;
9131 : 28325435 : tree t;
9132 : :
9133 : 28325435 : ret = make_tree_vector ();
9134 : 28325435 : vec_safe_reserve (ret, vec_safe_length (orig));
9135 : 68850496 : FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
9136 : 12199626 : ret->quick_push (t);
9137 : 28325435 : return ret;
9138 : : }
9139 : :
9140 : : /* Return true if KEYWORD starts a type specifier. */
9141 : :
9142 : : bool
9143 : 16 : keyword_begins_type_specifier (enum rid keyword)
9144 : : {
9145 : 16 : switch (keyword)
9146 : : {
9147 : : case RID_AUTO_TYPE:
9148 : : case RID_INT:
9149 : : case RID_CHAR:
9150 : : case RID_FLOAT:
9151 : : case RID_DOUBLE:
9152 : : case RID_VOID:
9153 : : case RID_UNSIGNED:
9154 : : case RID_LONG:
9155 : : case RID_SHORT:
9156 : : case RID_SIGNED:
9157 : : CASE_RID_FLOATN_NX:
9158 : : case RID_DFLOAT32:
9159 : : case RID_DFLOAT64:
9160 : : case RID_DFLOAT128:
9161 : : case RID_FRACT:
9162 : : case RID_ACCUM:
9163 : : case RID_BOOL:
9164 : : case RID_BITINT:
9165 : : case RID_WCHAR:
9166 : : case RID_CHAR8:
9167 : : case RID_CHAR16:
9168 : : case RID_CHAR32:
9169 : : case RID_SAT:
9170 : : case RID_COMPLEX:
9171 : : case RID_TYPEOF:
9172 : : case RID_STRUCT:
9173 : : case RID_CLASS:
9174 : : case RID_UNION:
9175 : : case RID_ENUM:
9176 : : return true;
9177 : 1 : default:
9178 : 1 : if (keyword >= RID_FIRST_INT_N
9179 : : && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
9180 : 0 : && int_n_enabled_p[keyword-RID_FIRST_INT_N])
9181 : : return true;
9182 : : return false;
9183 : : }
9184 : : }
9185 : :
9186 : : /* Return true if KEYWORD names a type qualifier. */
9187 : :
9188 : : bool
9189 : 11714 : keyword_is_type_qualifier (enum rid keyword)
9190 : : {
9191 : 11714 : switch (keyword)
9192 : : {
9193 : : case RID_CONST:
9194 : : case RID_VOLATILE:
9195 : : case RID_RESTRICT:
9196 : : case RID_ATOMIC:
9197 : : return true;
9198 : 58 : default:
9199 : 58 : return false;
9200 : : }
9201 : : }
9202 : :
9203 : : /* Return true if KEYWORD names a storage class specifier.
9204 : :
9205 : : RID_TYPEDEF is not included in this list despite `typedef' being
9206 : : listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
9207 : : such for syntactic convenience only. */
9208 : :
9209 : : bool
9210 : 11740 : keyword_is_storage_class_specifier (enum rid keyword)
9211 : : {
9212 : 11740 : switch (keyword)
9213 : : {
9214 : : case RID_STATIC:
9215 : : case RID_EXTERN:
9216 : : case RID_REGISTER:
9217 : : case RID_AUTO:
9218 : : case RID_MUTABLE:
9219 : : case RID_THREAD:
9220 : : return true;
9221 : 11714 : default:
9222 : 11714 : return false;
9223 : : }
9224 : : }
9225 : :
9226 : : /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
9227 : :
9228 : : static bool
9229 : 58 : keyword_is_function_specifier (enum rid keyword)
9230 : : {
9231 : 58 : switch (keyword)
9232 : : {
9233 : : case RID_INLINE:
9234 : : case RID_NORETURN:
9235 : : case RID_VIRTUAL:
9236 : : case RID_EXPLICIT:
9237 : : return true;
9238 : 56 : default:
9239 : 0 : return false;
9240 : : }
9241 : : }
9242 : :
9243 : : /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
9244 : : declaration-specifier (C99 6.7). */
9245 : :
9246 : : bool
9247 : 11740 : keyword_is_decl_specifier (enum rid keyword)
9248 : : {
9249 : 11740 : if (keyword_is_storage_class_specifier (keyword)
9250 : 11714 : || keyword_is_type_qualifier (keyword)
9251 : 11740 : || keyword_is_function_specifier (keyword))
9252 : 11684 : return true;
9253 : :
9254 : 56 : switch (keyword)
9255 : : {
9256 : : case RID_TYPEDEF:
9257 : : case RID_FRIEND:
9258 : : case RID_CONSTEXPR:
9259 : : case RID_CONSTINIT:
9260 : : return true;
9261 : : default:
9262 : : return false;
9263 : : }
9264 : : }
9265 : :
9266 : : /* Initialize language-specific-bits of tree_contains_struct. */
9267 : :
9268 : : void
9269 : 209847 : c_common_init_ts (void)
9270 : : {
9271 : 209847 : MARK_TS_EXP (SIZEOF_EXPR);
9272 : 209847 : MARK_TS_EXP (PAREN_SIZEOF_EXPR);
9273 : 209847 : MARK_TS_EXP (C_MAYBE_CONST_EXPR);
9274 : 209847 : MARK_TS_EXP (EXCESS_PRECISION_EXPR);
9275 : 209847 : MARK_TS_EXP (BREAK_STMT);
9276 : 209847 : MARK_TS_EXP (CONTINUE_STMT);
9277 : 209847 : MARK_TS_EXP (DO_STMT);
9278 : 209847 : MARK_TS_EXP (FOR_STMT);
9279 : 209847 : MARK_TS_EXP (SWITCH_STMT);
9280 : 209847 : MARK_TS_EXP (WHILE_STMT);
9281 : :
9282 : 209847 : MARK_TS_DECL_COMMON (CONCEPT_DECL);
9283 : 209847 : }
9284 : :
9285 : : /* Build a user-defined numeric literal out of an integer constant type VALUE
9286 : : with identifier SUFFIX. */
9287 : :
9288 : : tree
9289 : 932127 : build_userdef_literal (tree suffix_id, tree value,
9290 : : enum overflow_type overflow, tree num_string)
9291 : : {
9292 : 932127 : tree literal = make_node (USERDEF_LITERAL);
9293 : 932127 : USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
9294 : 932127 : USERDEF_LITERAL_VALUE (literal) = value;
9295 : 932127 : USERDEF_LITERAL_OVERFLOW (literal) = overflow;
9296 : 932127 : USERDEF_LITERAL_NUM_STRING (literal) = num_string;
9297 : 932127 : return literal;
9298 : : }
9299 : :
9300 : : /* For vector[index], convert the vector to an array of the underlying type.
9301 : : Return true if the resulting ARRAY_REF should not be an lvalue. */
9302 : :
9303 : : bool
9304 : 8678737 : convert_vector_to_array_for_subscript (location_t loc,
9305 : : tree *vecp, tree index)
9306 : : {
9307 : 8678737 : bool ret = false;
9308 : 8678737 : if (gnu_vector_type_p (TREE_TYPE (*vecp)))
9309 : : {
9310 : 1037409 : tree type = TREE_TYPE (*vecp);
9311 : 1037409 : tree newitype;
9312 : :
9313 : 1037409 : ret = !lvalue_p (*vecp);
9314 : :
9315 : 1037409 : index = fold_for_warn (index);
9316 : : /* Warn out-of-bounds index for vectors only if known. */
9317 : 1037409 : if (poly_int_tree_p (index))
9318 : 1024612 : if (!tree_fits_poly_uint64_p (index)
9319 : 1024612 : || known_ge (tree_to_poly_uint64 (index),
9320 : : TYPE_VECTOR_SUBPARTS (type)))
9321 : 82 : warning_at (loc, OPT_Warray_bounds_, "index value is out of bound");
9322 : :
9323 : : /* We are building an ARRAY_REF so mark the vector as addressable
9324 : : to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
9325 : : for function parameters. */
9326 : 1037409 : c_common_mark_addressable_vec (*vecp);
9327 : :
9328 : : /* Make sure qualifiers are copied from the vector type to the new element
9329 : : of the array type. */
9330 : 1037409 : newitype = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
9331 : :
9332 : 1037409 : *vecp = build1 (VIEW_CONVERT_EXPR,
9333 : : build_array_type_nelts (newitype,
9334 : : TYPE_VECTOR_SUBPARTS (type)),
9335 : : *vecp);
9336 : : }
9337 : 8678737 : return ret;
9338 : : }
9339 : :
9340 : : /* Determine which of the operands, if any, is a scalar that needs to be
9341 : : converted to a vector, for the range of operations. */
9342 : : enum stv_conv
9343 : 3467 : scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
9344 : : bool complain)
9345 : : {
9346 : 3467 : tree type0 = TREE_TYPE (op0);
9347 : 3467 : tree type1 = TREE_TYPE (op1);
9348 : 3467 : bool integer_only_op = false;
9349 : 3467 : enum stv_conv ret = stv_firstarg;
9350 : :
9351 : 3467 : gcc_assert (gnu_vector_type_p (type0) || gnu_vector_type_p (type1));
9352 : 3467 : switch (code)
9353 : : {
9354 : : /* Most GENERIC binary expressions require homogeneous arguments.
9355 : : LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
9356 : : argument that is a vector and a second one that is a scalar, so
9357 : : we never return stv_secondarg for them. */
9358 : 951 : case RSHIFT_EXPR:
9359 : 951 : case LSHIFT_EXPR:
9360 : 951 : if (TREE_CODE (type0) == INTEGER_TYPE
9361 : 951 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9362 : : {
9363 : 50 : if (unsafe_conversion_p (TREE_TYPE (type1), op0,
9364 : : NULL_TREE, false))
9365 : : {
9366 : 0 : if (complain)
9367 : 0 : error_at (loc, "conversion of scalar %qT to vector %qT "
9368 : : "involves truncation", type0, type1);
9369 : 0 : return stv_error;
9370 : : }
9371 : : else
9372 : : return stv_firstarg;
9373 : : }
9374 : : break;
9375 : :
9376 : 311 : case BIT_IOR_EXPR:
9377 : 311 : case BIT_XOR_EXPR:
9378 : 311 : case BIT_AND_EXPR:
9379 : 311 : integer_only_op = true;
9380 : : /* fall through */
9381 : :
9382 : 2504 : case VEC_COND_EXPR:
9383 : :
9384 : 2504 : case PLUS_EXPR:
9385 : 2504 : case MINUS_EXPR:
9386 : 2504 : case MULT_EXPR:
9387 : 2504 : case TRUNC_DIV_EXPR:
9388 : 2504 : case CEIL_DIV_EXPR:
9389 : 2504 : case FLOOR_DIV_EXPR:
9390 : 2504 : case ROUND_DIV_EXPR:
9391 : 2504 : case EXACT_DIV_EXPR:
9392 : 2504 : case TRUNC_MOD_EXPR:
9393 : 2504 : case FLOOR_MOD_EXPR:
9394 : 2504 : case RDIV_EXPR:
9395 : 2504 : case EQ_EXPR:
9396 : 2504 : case NE_EXPR:
9397 : 2504 : case LE_EXPR:
9398 : 2504 : case GE_EXPR:
9399 : 2504 : case LT_EXPR:
9400 : 2504 : case GT_EXPR:
9401 : : /* What about UNLT_EXPR? */
9402 : 2504 : if (gnu_vector_type_p (type0))
9403 : : {
9404 : 1851 : ret = stv_secondarg;
9405 : 1851 : std::swap (type0, type1);
9406 : 1851 : std::swap (op0, op1);
9407 : : }
9408 : :
9409 : 2504 : if (TREE_CODE (type0) == INTEGER_TYPE
9410 : 2504 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9411 : : {
9412 : 1794 : if (unsafe_conversion_p (TREE_TYPE (type1), op0,
9413 : : NULL_TREE, false))
9414 : : {
9415 : 11 : if (complain)
9416 : 8 : error_at (loc, "conversion of scalar %qT to vector %qT "
9417 : : "involves truncation", type0, type1);
9418 : 11 : return stv_error;
9419 : : }
9420 : : return ret;
9421 : : }
9422 : 710 : else if (!integer_only_op
9423 : : /* Allow integer --> real conversion if safe. */
9424 : 710 : && (SCALAR_FLOAT_TYPE_P (type0)
9425 : 468 : || TREE_CODE (type0) == INTEGER_TYPE)
9426 : 1420 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
9427 : : {
9428 : 686 : if (unsafe_conversion_p (TREE_TYPE (type1), op0,
9429 : : NULL_TREE, false))
9430 : : {
9431 : 22 : if (complain)
9432 : 22 : error_at (loc, "conversion of scalar %qT to vector %qT "
9433 : : "involves truncation", type0, type1);
9434 : 22 : return stv_error;
9435 : : }
9436 : : return ret;
9437 : : }
9438 : : default:
9439 : : break;
9440 : : }
9441 : :
9442 : : return stv_nothing;
9443 : : }
9444 : :
9445 : : /* Return the alignment of std::max_align_t.
9446 : :
9447 : : [support.types.layout] The type max_align_t is a POD type whose alignment
9448 : : requirement is at least as great as that of every scalar type, and whose
9449 : : alignment requirement is supported in every context. */
9450 : :
9451 : : unsigned
9452 : 252777 : max_align_t_align ()
9453 : : {
9454 : 252777 : unsigned int max_align = MAX (TYPE_ALIGN (long_long_integer_type_node),
9455 : : TYPE_ALIGN (long_double_type_node));
9456 : 252777 : if (float128_type_node != NULL_TREE)
9457 : 252777 : max_align = MAX (max_align, TYPE_ALIGN (float128_type_node));
9458 : 252777 : return max_align;
9459 : : }
9460 : :
9461 : : /* Return true iff ALIGN is an integral constant that is a fundamental
9462 : : alignment, as defined by [basic.align] in the c++-11
9463 : : specifications.
9464 : :
9465 : : That is:
9466 : :
9467 : : [A fundamental alignment is represented by an alignment less than or
9468 : : equal to the greatest alignment supported by the implementation
9469 : : in all contexts, which is equal to alignof(max_align_t)]. */
9470 : :
9471 : : bool
9472 : 0 : cxx_fundamental_alignment_p (unsigned align)
9473 : : {
9474 : 0 : return (align <= max_align_t_align ());
9475 : : }
9476 : :
9477 : : /* Return true if T is a pointer to a zero-sized aggregate. */
9478 : :
9479 : : bool
9480 : 1299920 : pointer_to_zero_sized_aggr_p (tree t)
9481 : : {
9482 : 1299920 : if (!POINTER_TYPE_P (t))
9483 : : return false;
9484 : 1299920 : t = TREE_TYPE (t);
9485 : 1299920 : return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
9486 : : }
9487 : :
9488 : : /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
9489 : : with no library fallback or for an ADDR_EXPR whose operand is such type
9490 : : issues an error pointing to the location LOC.
9491 : : Returns true when the expression has been diagnosed and false
9492 : : otherwise. */
9493 : :
9494 : : bool
9495 : 4278434029 : reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
9496 : : {
9497 : 4278434029 : if (TREE_CODE (expr) == ADDR_EXPR)
9498 : 104567185 : expr = TREE_OPERAND (expr, 0);
9499 : :
9500 : 4278434029 : STRIP_ANY_LOCATION_WRAPPER (expr);
9501 : :
9502 : 4278434029 : if (TREE_TYPE (expr)
9503 : 4278433875 : && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
9504 : 2008635 : && TREE_CODE (expr) == FUNCTION_DECL
9505 : : /* The intersection of DECL_BUILT_IN and DECL_IS_UNDECLARED_BUILTIN avoids
9506 : : false positives for user-declared built-ins such as abs or
9507 : : strlen, and for C++ operators new and delete.
9508 : : The c_decl_implicit() test avoids false positives for implicitly
9509 : : declared built-ins with library fallbacks (such as abs). */
9510 : 1981953 : && fndecl_built_in_p (expr)
9511 : 66463 : && DECL_IS_UNDECLARED_BUILTIN (expr)
9512 : 2372 : && !c_decl_implicit (expr)
9513 : 4278436396 : && !DECL_ASSEMBLER_NAME_SET_P (expr))
9514 : : {
9515 : 220 : if (loc == UNKNOWN_LOCATION)
9516 : 82 : loc = EXPR_LOC_OR_LOC (expr, input_location);
9517 : :
9518 : : /* Reject arguments that are built-in functions with
9519 : : no library fallback. */
9520 : 220 : error_at (loc, "built-in function %qE must be directly called", expr);
9521 : :
9522 : 220 : return true;
9523 : : }
9524 : :
9525 : : return false;
9526 : : }
9527 : :
9528 : : /* Issue an ERROR for an invalid SIZE of array NAME which is null
9529 : : for unnamed arrays. */
9530 : :
9531 : : void
9532 : 1416 : invalid_array_size_error (location_t loc, cst_size_error error,
9533 : : const_tree size, const_tree name)
9534 : : {
9535 : 1416 : tree maxsize = max_object_size ();
9536 : 1416 : switch (error)
9537 : : {
9538 : 0 : case cst_size_not_constant:
9539 : 0 : if (name)
9540 : 0 : error_at (loc, "size of array %qE is not a constant expression",
9541 : : name);
9542 : : else
9543 : 0 : error_at (loc, "size of array is not a constant expression");
9544 : : break;
9545 : 426 : case cst_size_negative:
9546 : 426 : if (name)
9547 : 135 : error_at (loc, "size %qE of array %qE is negative",
9548 : : size, name);
9549 : : else
9550 : 291 : error_at (loc, "size %qE of array is negative",
9551 : : size);
9552 : : break;
9553 : 930 : case cst_size_too_big:
9554 : 930 : if (name)
9555 : 103 : error_at (loc, "size %qE of array %qE exceeds maximum "
9556 : : "object size %qE", size, name, maxsize);
9557 : : else
9558 : 827 : error_at (loc, "size %qE of array exceeds maximum "
9559 : : "object size %qE", size, maxsize);
9560 : : break;
9561 : 60 : case cst_size_overflow:
9562 : 60 : if (name)
9563 : 39 : error_at (loc, "size of array %qE exceeds maximum "
9564 : : "object size %qE", name, maxsize);
9565 : : else
9566 : 21 : error_at (loc, "size of array exceeds maximum "
9567 : : "object size %qE", maxsize);
9568 : : break;
9569 : 0 : default:
9570 : 0 : gcc_unreachable ();
9571 : : }
9572 : 1416 : }
9573 : :
9574 : : /* Check if array size calculations overflow or if the array covers more
9575 : : than half of the address space. Return true if the size of the array
9576 : : is valid, false otherwise. T is either the type of the array or its
9577 : : size, and NAME is the name of the array, or null for unnamed arrays. */
9578 : :
9579 : : bool
9580 : 7382829 : valid_array_size_p (location_t loc, const_tree t, tree name, bool complain)
9581 : : {
9582 : 7382829 : if (t == error_mark_node)
9583 : : return true;
9584 : :
9585 : 7382688 : const_tree size;
9586 : 7382688 : if (TYPE_P (t))
9587 : : {
9588 : 5304104 : if (!COMPLETE_TYPE_P (t))
9589 : : return true;
9590 : 2775105 : size = TYPE_SIZE_UNIT (t);
9591 : : }
9592 : : else
9593 : : size = t;
9594 : :
9595 : 4853689 : if (TREE_CODE (size) != INTEGER_CST)
9596 : : return true;
9597 : :
9598 : 4829277 : cst_size_error error;
9599 : 4829277 : if (valid_constant_size_p (size, &error))
9600 : : return true;
9601 : :
9602 : 1107 : if (!complain)
9603 : : return false;
9604 : :
9605 : 1092 : if (TREE_CODE (TREE_TYPE (size)) == ENUMERAL_TYPE)
9606 : : /* Show the value of the enumerator rather than its name. */
9607 : 16 : size = convert (ssizetype, const_cast<tree> (size));
9608 : :
9609 : 1092 : invalid_array_size_error (loc, error, size, name);
9610 : 1092 : return false;
9611 : : }
9612 : :
9613 : : /* Read SOURCE_DATE_EPOCH from environment to have a deterministic
9614 : : timestamp to replace embedded current dates to get reproducible
9615 : : results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
9616 : :
9617 : : time_t
9618 : 2504 : cb_get_source_date_epoch (cpp_reader *pfile ATTRIBUTE_UNUSED)
9619 : : {
9620 : 2504 : char *source_date_epoch;
9621 : 2504 : int64_t epoch;
9622 : 2504 : char *endptr;
9623 : :
9624 : 2504 : source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
9625 : 2504 : if (!source_date_epoch)
9626 : : return (time_t) -1;
9627 : :
9628 : 3 : errno = 0;
9629 : : #if defined(INT64_T_IS_LONG)
9630 : 3 : epoch = strtol (source_date_epoch, &endptr, 10);
9631 : : #else
9632 : : epoch = strtoll (source_date_epoch, &endptr, 10);
9633 : : #endif
9634 : 3 : if (errno != 0 || endptr == source_date_epoch || *endptr != '\0'
9635 : 2 : || epoch < 0 || epoch > MAX_SOURCE_DATE_EPOCH)
9636 : : {
9637 : 1 : error_at (input_location, "environment variable %qs must "
9638 : : "expand to a non-negative integer less than or equal to %wd",
9639 : : "SOURCE_DATE_EPOCH", MAX_SOURCE_DATE_EPOCH);
9640 : 1 : return (time_t) -1;
9641 : : }
9642 : :
9643 : : return (time_t) epoch;
9644 : : }
9645 : :
9646 : : /* Callback for libcpp for offering spelling suggestions for misspelled
9647 : : directives. GOAL is an unrecognized string; CANDIDATES is a
9648 : : NULL-terminated array of candidate strings. Return the closest
9649 : : match to GOAL within CANDIDATES, or NULL if none are good
9650 : : suggestions. */
9651 : :
9652 : : const char *
9653 : 19 : cb_get_suggestion (cpp_reader *, const char *goal,
9654 : : const char *const *candidates)
9655 : : {
9656 : 19 : best_match<const char *, const char *> bm (goal);
9657 : 204 : while (*candidates)
9658 : 166 : bm.consider (*candidates++);
9659 : 19 : return bm.get_best_meaningful_candidate ();
9660 : : }
9661 : :
9662 : : /* Return the latice point which is the wider of the two FLT_EVAL_METHOD
9663 : : modes X, Y. This isn't just >, as the FLT_EVAL_METHOD values added
9664 : : by C TS 18661-3 for interchange types that are computed in their
9665 : : native precision are larger than the C11 values for evaluating in the
9666 : : precision of float/double/long double. If either mode is
9667 : : FLT_EVAL_METHOD_UNPREDICTABLE, return that. */
9668 : :
9669 : : enum flt_eval_method
9670 : 532678 : excess_precision_mode_join (enum flt_eval_method x,
9671 : : enum flt_eval_method y)
9672 : : {
9673 : 532678 : if (x == FLT_EVAL_METHOD_UNPREDICTABLE
9674 : 532678 : || y == FLT_EVAL_METHOD_UNPREDICTABLE)
9675 : : return FLT_EVAL_METHOD_UNPREDICTABLE;
9676 : :
9677 : : /* GCC only supports one interchange type right now, _Float16. If
9678 : : we're evaluating _Float16 in 16-bit precision, then flt_eval_method
9679 : : will be FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
9680 : 532354 : if (x == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
9681 : : return y;
9682 : 530247 : if (y == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
9683 : : return x;
9684 : :
9685 : : /* Other values for flt_eval_method are directly comparable, and we want
9686 : : the maximum. */
9687 : 530235 : return MAX (x, y);
9688 : : }
9689 : :
9690 : : /* Return the value that should be set for FLT_EVAL_METHOD in the
9691 : : context of ISO/IEC TS 18861-3.
9692 : :
9693 : : This relates to the effective excess precision seen by the user,
9694 : : which is the join point of the precision the target requests for
9695 : : -fexcess-precision={standard,fast,16} and the implicit excess precision
9696 : : the target uses. */
9697 : :
9698 : : static enum flt_eval_method
9699 : 418020 : c_ts18661_flt_eval_method (void)
9700 : : {
9701 : 418020 : enum flt_eval_method implicit
9702 : 418020 : = targetm.c.excess_precision (EXCESS_PRECISION_TYPE_IMPLICIT);
9703 : :
9704 : 304662 : enum excess_precision_type flag_type
9705 : 418020 : = (flag_excess_precision == EXCESS_PRECISION_STANDARD
9706 : 418020 : ? EXCESS_PRECISION_TYPE_STANDARD
9707 : : : (flag_excess_precision == EXCESS_PRECISION_FLOAT16
9708 : 304674 : ? EXCESS_PRECISION_TYPE_FLOAT16
9709 : : : EXCESS_PRECISION_TYPE_FAST));
9710 : :
9711 : 418020 : enum flt_eval_method requested
9712 : 418020 : = targetm.c.excess_precision (flag_type);
9713 : :
9714 : 418020 : return excess_precision_mode_join (implicit, requested);
9715 : : }
9716 : :
9717 : : /* As c_cpp_ts18661_flt_eval_method, but clamps the expected values to
9718 : : those that were permitted by C11. That is to say, eliminates
9719 : : FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
9720 : :
9721 : : static enum flt_eval_method
9722 : 102418 : c_c11_flt_eval_method (void)
9723 : : {
9724 : 102418 : return excess_precision_mode_join (c_ts18661_flt_eval_method (),
9725 : 102418 : FLT_EVAL_METHOD_PROMOTE_TO_FLOAT);
9726 : : }
9727 : :
9728 : : /* Return the value that should be set for FLT_EVAL_METHOD.
9729 : : MAYBE_C11_ONLY_P is TRUE if we should check
9730 : : FLAG_PERMITTED_EVAL_METHODS as to whether we should limit the possible
9731 : : values we can return to those from C99/C11, and FALSE otherwise.
9732 : : See the comments on c_ts18661_flt_eval_method for what value we choose
9733 : : to set here. */
9734 : :
9735 : : int
9736 : 418020 : c_flt_eval_method (bool maybe_c11_only_p)
9737 : : {
9738 : 418020 : if (maybe_c11_only_p
9739 : 209010 : && flag_permitted_flt_eval_methods
9740 : : == PERMITTED_FLT_EVAL_METHODS_C11)
9741 : 102418 : return c_c11_flt_eval_method ();
9742 : : else
9743 : 315602 : return c_ts18661_flt_eval_method ();
9744 : : }
9745 : :
9746 : : /* An enum for get_missing_token_insertion_kind for describing the best
9747 : : place to insert a missing token, if there is one. */
9748 : :
9749 : : enum missing_token_insertion_kind
9750 : : {
9751 : : MTIK_IMPOSSIBLE,
9752 : : MTIK_INSERT_BEFORE_NEXT,
9753 : : MTIK_INSERT_AFTER_PREV
9754 : : };
9755 : :
9756 : : /* Given a missing token of TYPE, determine if it is reasonable to
9757 : : emit a fix-it hint suggesting the insertion of the token, and,
9758 : : if so, where the token should be inserted relative to other tokens.
9759 : :
9760 : : It only makes sense to do this for values of TYPE that are symbols.
9761 : :
9762 : : Some symbols should go before the next token, e.g. in:
9763 : : if flag)
9764 : : we want to insert the missing '(' immediately before "flag",
9765 : : giving:
9766 : : if (flag)
9767 : : rather than:
9768 : : if( flag)
9769 : : These use MTIK_INSERT_BEFORE_NEXT.
9770 : :
9771 : : Other symbols should go after the previous token, e.g. in:
9772 : : if (flag
9773 : : do_something ();
9774 : : we want to insert the missing ')' immediately after the "flag",
9775 : : giving:
9776 : : if (flag)
9777 : : do_something ();
9778 : : rather than:
9779 : : if (flag
9780 : : )do_something ();
9781 : : These use MTIK_INSERT_AFTER_PREV. */
9782 : :
9783 : : static enum missing_token_insertion_kind
9784 : 3229 : get_missing_token_insertion_kind (enum cpp_ttype type)
9785 : : {
9786 : 3229 : switch (type)
9787 : : {
9788 : : /* Insert missing "opening" brackets immediately
9789 : : before the next token. */
9790 : : case CPP_OPEN_SQUARE:
9791 : : case CPP_OPEN_PAREN:
9792 : : return MTIK_INSERT_BEFORE_NEXT;
9793 : :
9794 : : /* Insert other missing symbols immediately after
9795 : : the previous token. */
9796 : 1958 : case CPP_CLOSE_PAREN:
9797 : 1958 : case CPP_CLOSE_SQUARE:
9798 : 1958 : case CPP_SEMICOLON:
9799 : 1958 : case CPP_COMMA:
9800 : 1958 : case CPP_COLON:
9801 : 1958 : return MTIK_INSERT_AFTER_PREV;
9802 : :
9803 : : /* Other kinds of token don't get fix-it hints. */
9804 : 987 : default:
9805 : 987 : return MTIK_IMPOSSIBLE;
9806 : : }
9807 : : }
9808 : :
9809 : : /* Given RICHLOC, a location for a diagnostic describing a missing token
9810 : : of kind TOKEN_TYPE, potentially add a fix-it hint suggesting the
9811 : : insertion of the token.
9812 : :
9813 : : The location of the attempted fix-it hint depends on TOKEN_TYPE:
9814 : : it will either be:
9815 : : (a) immediately after PREV_TOKEN_LOC, or
9816 : :
9817 : : (b) immediately before the primary location within RICHLOC (taken to
9818 : : be that of the token following where the token was expected).
9819 : :
9820 : : If we manage to add a fix-it hint, then the location of the
9821 : : fix-it hint is likely to be more useful as the primary location
9822 : : of the diagnostic than that of the following token, so we swap
9823 : : these locations.
9824 : :
9825 : : For example, given this bogus code:
9826 : : 123456789012345678901234567890
9827 : : 1 | int missing_semicolon (void)
9828 : : 2 | {
9829 : : 3 | return 42
9830 : : 4 | }
9831 : :
9832 : : we will emit:
9833 : :
9834 : : "expected ';' before '}'"
9835 : :
9836 : : RICHLOC's primary location is at the closing brace, so before "swapping"
9837 : : we would emit the error at line 4 column 1:
9838 : :
9839 : : 123456789012345678901234567890
9840 : : 3 | return 42 |< fix-it hint emitted for this line
9841 : : | ; |
9842 : : 4 | } |< "expected ';' before '}'" emitted at this line
9843 : : | ^ |
9844 : :
9845 : : It's more useful for the location of the diagnostic to be at the
9846 : : fix-it hint, so we swap the locations, so the primary location
9847 : : is at the fix-it hint, with the old primary location inserted
9848 : : as a secondary location, giving this, with the error at line 3
9849 : : column 12:
9850 : :
9851 : : 123456789012345678901234567890
9852 : : 3 | return 42 |< "expected ';' before '}'" emitted at this line,
9853 : : | ^ | with fix-it hint
9854 : : 4 | ; |
9855 : : | } |< secondary range emitted here
9856 : : | ~ |. */
9857 : :
9858 : : void
9859 : 3229 : maybe_suggest_missing_token_insertion (rich_location *richloc,
9860 : : enum cpp_ttype token_type,
9861 : : location_t prev_token_loc)
9862 : : {
9863 : 3229 : gcc_assert (richloc);
9864 : :
9865 : 3229 : enum missing_token_insertion_kind mtik
9866 : 3229 : = get_missing_token_insertion_kind (token_type);
9867 : :
9868 : 3229 : switch (mtik)
9869 : : {
9870 : : default:
9871 : : gcc_unreachable ();
9872 : : break;
9873 : :
9874 : : case MTIK_IMPOSSIBLE:
9875 : : return;
9876 : :
9877 : 284 : case MTIK_INSERT_BEFORE_NEXT:
9878 : : /* Attempt to add the fix-it hint before the primary location
9879 : : of RICHLOC. */
9880 : 284 : richloc->add_fixit_insert_before (cpp_type2name (token_type, 0));
9881 : 284 : break;
9882 : :
9883 : 1958 : case MTIK_INSERT_AFTER_PREV:
9884 : : /* Attempt to add the fix-it hint after PREV_TOKEN_LOC. */
9885 : 1958 : richloc->add_fixit_insert_after (prev_token_loc,
9886 : : cpp_type2name (token_type, 0));
9887 : 1958 : break;
9888 : : }
9889 : :
9890 : : /* If we were successful, use the fix-it hint's location as the
9891 : : primary location within RICHLOC, adding the old primary location
9892 : : back as a secondary location. */
9893 : 2242 : if (!richloc->seen_impossible_fixit_p ())
9894 : : {
9895 : 2238 : fixit_hint *hint = richloc->get_last_fixit_hint ();
9896 : 2238 : location_t hint_loc = hint->get_start_loc ();
9897 : 2238 : location_t old_loc = richloc->get_loc ();
9898 : :
9899 : 2238 : richloc->set_range (0, hint_loc, SHOW_RANGE_WITH_CARET);
9900 : 2238 : richloc->add_range (old_loc);
9901 : : }
9902 : : }
9903 : :
9904 : : /* Potentially emit a note about likely missing '&' or '*',
9905 : : depending on EXPR and EXPECTED_TYPE. */
9906 : :
9907 : : void
9908 : 2202 : maybe_emit_indirection_note (location_t loc,
9909 : : tree expr, tree expected_type)
9910 : : {
9911 : 2202 : gcc_assert (expr);
9912 : 2202 : gcc_assert (expected_type);
9913 : :
9914 : 2202 : tree actual_type = TREE_TYPE (expr);
9915 : :
9916 : : /* Missing '&'. */
9917 : 2202 : if (TREE_CODE (expected_type) == POINTER_TYPE
9918 : 1224 : && compatible_types_for_indirection_note_p (actual_type,
9919 : 1224 : TREE_TYPE (expected_type))
9920 : 2280 : && lvalue_p (expr))
9921 : : {
9922 : 43 : gcc_rich_location richloc (loc);
9923 : 43 : richloc.add_fixit_insert_before ("&");
9924 : 43 : inform (&richloc, "possible fix: take the address with %qs", "&");
9925 : 43 : }
9926 : :
9927 : : /* Missing '*'. */
9928 : 2202 : if (TREE_CODE (actual_type) == POINTER_TYPE
9929 : 2202 : && compatible_types_for_indirection_note_p (TREE_TYPE (actual_type),
9930 : : expected_type))
9931 : : {
9932 : 78 : gcc_rich_location richloc (loc);
9933 : 78 : richloc.add_fixit_insert_before ("*");
9934 : 78 : inform (&richloc, "possible fix: dereference with %qs", "*");
9935 : 78 : }
9936 : 2202 : }
9937 : :
9938 : : #if CHECKING_P
9939 : :
9940 : : namespace selftest {
9941 : :
9942 : : /* Verify that fold_for_warn on error_mark_node is safe. */
9943 : :
9944 : : static void
9945 : 3 : test_fold_for_warn ()
9946 : : {
9947 : 3 : ASSERT_EQ (error_mark_node, fold_for_warn (error_mark_node));
9948 : 3 : }
9949 : :
9950 : : /* Run all of the selftests within this file. */
9951 : :
9952 : : static void
9953 : 3 : c_common_cc_tests ()
9954 : : {
9955 : 0 : test_fold_for_warn ();
9956 : 0 : }
9957 : :
9958 : : /* Run all of the tests within c-family. */
9959 : :
9960 : : void
9961 : 3 : c_family_tests (void)
9962 : : {
9963 : 3 : c_common_cc_tests ();
9964 : 3 : c_format_cc_tests ();
9965 : 3 : c_indentation_cc_tests ();
9966 : 3 : c_pretty_print_cc_tests ();
9967 : 3 : c_spellcheck_cc_tests ();
9968 : 3 : c_opt_problem_cc_tests ();
9969 : :
9970 : : /* According to https://gcc.gnu.org/pipermail/gcc/2021-November/237703.html
9971 : : this has some language-specific assumptions, so we run it here. */
9972 : 3 : diagnostics::selftest::context_cc_tests ();
9973 : 3 : }
9974 : :
9975 : : } // namespace selftest
9976 : :
9977 : : #endif /* #if CHECKING_P */
9978 : :
9979 : : /* Attempt to locate a suitable location within FILE for a
9980 : : #include directive to be inserted before.
9981 : : LOC is the location of the relevant diagnostic.
9982 : :
9983 : : Attempt to return the location within FILE immediately
9984 : : after the last #include within that file, or the start of
9985 : : that file if it has no #include directives.
9986 : :
9987 : : Return UNKNOWN_LOCATION if no suitable location is found,
9988 : : or if an error occurs. */
9989 : :
9990 : : static location_t
9991 : 427 : try_to_locate_new_include_insertion_point (const char *file, location_t loc)
9992 : : {
9993 : : /* Locate the last ordinary map within FILE that ended with a #include. */
9994 : 427 : const line_map_ordinary *last_include_ord_map = NULL;
9995 : :
9996 : : /* ...and the next ordinary map within FILE after that one. */
9997 : 427 : const line_map_ordinary *last_ord_map_after_include = NULL;
9998 : :
9999 : : /* ...and the first ordinary map within FILE. */
10000 : 427 : const line_map_ordinary *first_ord_map_in_file = NULL;
10001 : :
10002 : : /* Get ordinary map containing LOC (or its expansion). */
10003 : 427 : const line_map_ordinary *ord_map_for_loc = NULL;
10004 : 427 : linemap_resolve_location (line_table, loc, LRK_MACRO_EXPANSION_POINT,
10005 : : &ord_map_for_loc);
10006 : 427 : gcc_assert (ord_map_for_loc);
10007 : :
10008 : 11987 : for (unsigned int i = 0; i < LINEMAPS_ORDINARY_USED (line_table); i++)
10009 : : {
10010 : 11987 : const line_map_ordinary *ord_map
10011 : 11987 : = LINEMAPS_ORDINARY_MAP_AT (line_table, i);
10012 : :
10013 : 23974 : if (const line_map_ordinary *from
10014 : 11987 : = linemap_included_from_linemap (line_table, ord_map))
10015 : : /* We cannot use pointer equality, because with preprocessed
10016 : : input all filename strings are unique. */
10017 : 9613 : if (0 == strcmp (from->to_file, file))
10018 : : {
10019 : 11987 : last_include_ord_map = from;
10020 : 11987 : last_ord_map_after_include = NULL;
10021 : : }
10022 : :
10023 : : /* Likewise, use strcmp, and reject any line-zero introductory
10024 : : map. */
10025 : 11987 : if (ord_map->to_line && 0 == strcmp (ord_map->to_file, file))
10026 : : {
10027 : 669 : if (!first_ord_map_in_file)
10028 : 427 : first_ord_map_in_file = ord_map;
10029 : 669 : if (last_include_ord_map && !last_ord_map_after_include)
10030 : 166 : last_ord_map_after_include = ord_map;
10031 : : }
10032 : :
10033 : : /* Stop searching when reaching the ord_map containing LOC,
10034 : : as it makes no sense to provide fix-it hints that appear
10035 : : after the diagnostic in question. */
10036 : 11987 : if (ord_map == ord_map_for_loc)
10037 : : break;
10038 : : }
10039 : :
10040 : : /* Determine where to insert the #include. */
10041 : 427 : const line_map_ordinary *ord_map_for_insertion;
10042 : :
10043 : : /* We want the next ordmap in the file after the last one that's a
10044 : : #include, but failing that, the start of the file. */
10045 : 427 : if (last_ord_map_after_include)
10046 : : ord_map_for_insertion = last_ord_map_after_include;
10047 : : else
10048 : 263 : ord_map_for_insertion = first_ord_map_in_file;
10049 : :
10050 : 263 : if (!ord_map_for_insertion)
10051 : : return UNKNOWN_LOCATION;
10052 : :
10053 : : /* The "start_location" is column 0, meaning "the whole line".
10054 : : rich_location and diagnostics::changes can't cope with this, so use
10055 : : column 1 instead. */
10056 : 427 : location_t col_0 = ord_map_for_insertion->start_location;
10057 : 427 : return linemap_position_for_loc_and_offset (line_table, col_0, 1);
10058 : : }
10059 : :
10060 : : /* A map from filenames to sets of headers added to them, for
10061 : : ensuring idempotency within maybe_add_include_fixit. */
10062 : :
10063 : : /* The values within the map. We need string comparison as there's
10064 : : no guarantee that two different diagnostics that are recommending
10065 : : adding e.g. "<stdio.h>" are using the same buffer. */
10066 : :
10067 : : typedef hash_set <const char *, false, nofree_string_hash> per_file_includes_t;
10068 : :
10069 : : /* The map itself. We don't need string comparison for the filename keys,
10070 : : as they come from libcpp. */
10071 : :
10072 : : typedef hash_map <const char *, per_file_includes_t *> added_includes_t;
10073 : : static added_includes_t *added_includes;
10074 : :
10075 : : /* Attempt to add a fix-it hint to RICHLOC, adding "#include HEADER\n"
10076 : : in a suitable location within the file of RICHLOC's primary
10077 : : location.
10078 : :
10079 : : This function is idempotent: a header will be added at most once to
10080 : : any given file.
10081 : :
10082 : : If OVERRIDE_LOCATION is true, then if a fix-it is added and will be
10083 : : printed, then RICHLOC's primary location will be replaced by that of
10084 : : the fix-it hint (for use by "inform" notes where the location of the
10085 : : issue has already been reported). */
10086 : :
10087 : : void
10088 : 879 : maybe_add_include_fixit (rich_location *richloc, const char *header,
10089 : : bool override_location)
10090 : : {
10091 : 879 : location_t loc = richloc->get_loc ();
10092 : 879 : const char *file = LOCATION_FILE (loc);
10093 : 879 : if (!file)
10094 : 452 : return;
10095 : :
10096 : : /* Idempotency: don't add the same header more than once to a given file. */
10097 : 879 : if (!added_includes)
10098 : 344 : added_includes = new added_includes_t ();
10099 : 879 : per_file_includes_t *&set = added_includes->get_or_insert (file);
10100 : 879 : if (set)
10101 : 535 : if (set->contains (header))
10102 : : /* ...then we've already added HEADER to that file. */
10103 : : return;
10104 : 427 : if (!set)
10105 : 344 : set = new per_file_includes_t ();
10106 : 427 : set->add (header);
10107 : :
10108 : : /* Attempt to locate a suitable place for the new directive. */
10109 : 427 : location_t include_insert_loc
10110 : 427 : = try_to_locate_new_include_insertion_point (file, loc);
10111 : 427 : if (include_insert_loc == UNKNOWN_LOCATION)
10112 : : return;
10113 : :
10114 : 427 : char *text = xasprintf ("#include %s\n", header);
10115 : 427 : richloc->add_fixit_insert_before (include_insert_loc, text);
10116 : 427 : free (text);
10117 : :
10118 : 427 : if (override_location && global_dc->get_source_printing_options ().enabled)
10119 : : {
10120 : : /* Replace the primary location with that of the insertion point for the
10121 : : fix-it hint.
10122 : :
10123 : : We use SHOW_LINES_WITHOUT_RANGE so that we don't meaningless print a
10124 : : caret for the insertion point (or colorize it).
10125 : :
10126 : : Hence we print e.g.:
10127 : :
10128 : : ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
10129 : : 73 | # include <debug/vector>
10130 : : +++ |+#include <vector>
10131 : : 74 | #endif
10132 : :
10133 : : rather than:
10134 : :
10135 : : ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
10136 : : 73 | # include <debug/vector>
10137 : : +++ |+#include <vector>
10138 : : 74 | #endif
10139 : : | ^
10140 : :
10141 : : avoiding the caret on the first column of line 74. */
10142 : 177 : richloc->set_range (0, include_insert_loc, SHOW_LINES_WITHOUT_RANGE);
10143 : : }
10144 : : }
10145 : :
10146 : : /* Attempt to convert a braced array initializer list CTOR for array
10147 : : TYPE into a STRING_CST for convenience and efficiency. Return
10148 : : the converted string on success or the original ctor on failure.
10149 : : Also, for non-convertable CTORs which contain RAW_DATA_CST values
10150 : : among the elts try to extend the range of RAW_DATA_CSTs. */
10151 : :
10152 : : static tree
10153 : 44586 : braced_list_to_string (tree type, tree ctor, bool member)
10154 : : {
10155 : : /* Ignore non-members with unknown size like arrays with unspecified
10156 : : bound. */
10157 : 44586 : tree typesize = TYPE_SIZE_UNIT (type);
10158 : 44586 : if (!member && !tree_fits_uhwi_p (typesize))
10159 : : return ctor;
10160 : :
10161 : : /* If the target char size differs from the host char size, we'd risk
10162 : : loosing data and getting object sizes wrong by converting to
10163 : : host chars. */
10164 : 44570 : if (TYPE_PRECISION (char_type_node) != CHAR_BIT)
10165 : : return ctor;
10166 : :
10167 : : /* STRING_CST doesn't support wide characters. */
10168 : 44570 : gcc_checking_assert (TYPE_PRECISION (TREE_TYPE (type)) == CHAR_BIT);
10169 : :
10170 : : /* If the array has an explicit bound, use it to constrain the size
10171 : : of the string. If it doesn't, be sure to create a string that's
10172 : : as long as implied by the index of the last zero specified via
10173 : : a designator, as in:
10174 : : const char a[] = { [7] = 0 }; */
10175 : 44570 : unsigned HOST_WIDE_INT maxelts;
10176 : 44570 : if (typesize)
10177 : : {
10178 : 44491 : maxelts = tree_to_uhwi (typesize);
10179 : 44491 : maxelts /= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
10180 : : }
10181 : : else
10182 : : maxelts = HOST_WIDE_INT_M1U;
10183 : :
10184 : : /* Avoid converting initializers for zero-length arrays (but do
10185 : : create them for flexible array members). */
10186 : 44491 : if (!maxelts)
10187 : : return ctor;
10188 : :
10189 : 44421 : unsigned HOST_WIDE_INT nelts = CONSTRUCTOR_NELTS (ctor);
10190 : :
10191 : 44421 : auto_vec<char> str;
10192 : 44421 : str.reserve (nelts + 1);
10193 : :
10194 : 44421 : unsigned HOST_WIDE_INT i, j = HOST_WIDE_INT_M1U;
10195 : 44421 : tree index, value;
10196 : 44421 : bool check_raw_data = false;
10197 : :
10198 : 631726 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, index, value)
10199 : : {
10200 : 587305 : if (check_raw_data)
10201 : : {
10202 : : /* The preprocessor always surrounds CPP_EMBED tokens in between
10203 : : CPP_NUMBER and CPP_COMMA tokens. Try to undo that here now that
10204 : : the whole initializer is parsed. E.g. if we have
10205 : : [0] = 'T', [1] = "his is a #embed tex", [20] = 't'
10206 : : where the middle value is RAW_DATA_CST and in its owner this is
10207 : : surrounded by 'T' and 't' characters, we can create from it just
10208 : : [0] = "This is a #embed text"
10209 : : Similarly if a RAW_DATA_CST needs to be split into two parts
10210 : : because of designated init store but the stored value is actually
10211 : : the same as in the RAW_DATA_OWNER's memory we can merge multiple
10212 : : RAW_DATA_CSTs. */
10213 : 1213 : if (TREE_CODE (value) == RAW_DATA_CST
10214 : 535 : && index
10215 : 535 : && tree_fits_uhwi_p (index))
10216 : : {
10217 : 535 : tree owner = RAW_DATA_OWNER (value);
10218 : 535 : unsigned int start, end, k;
10219 : 535 : if (TREE_CODE (owner) == STRING_CST)
10220 : : {
10221 : 209 : start
10222 : 209 : = RAW_DATA_POINTER (value) - TREE_STRING_POINTER (owner);
10223 : 209 : end = TREE_STRING_LENGTH (owner) - RAW_DATA_LENGTH (value);
10224 : : }
10225 : : else
10226 : : {
10227 : 326 : gcc_checking_assert (TREE_CODE (owner) == RAW_DATA_CST);
10228 : 326 : start
10229 : 326 : = RAW_DATA_POINTER (value) - RAW_DATA_POINTER (owner);
10230 : 326 : end = RAW_DATA_LENGTH (owner) - RAW_DATA_LENGTH (value);
10231 : : }
10232 : 535 : end -= start;
10233 : 535 : unsigned HOST_WIDE_INT l = j == HOST_WIDE_INT_M1U ? i : j;
10234 : 955 : for (k = 0; k < start && k < l; ++k)
10235 : : {
10236 : 446 : constructor_elt *elt = CONSTRUCTOR_ELT (ctor, l - k - 1);
10237 : 892 : if (elt->index == NULL_TREE
10238 : 446 : || !tree_fits_uhwi_p (elt->index)
10239 : 446 : || !tree_fits_shwi_p (elt->value)
10240 : 1333 : || wi::to_widest (index) != (wi::to_widest (elt->index)
10241 : 1328 : + (k + 1)))
10242 : : break;
10243 : 441 : if (TYPE_UNSIGNED (TREE_TYPE (value)))
10244 : : {
10245 : 365 : if (tree_to_shwi (elt->value)
10246 : 365 : != *((const unsigned char *)
10247 : 365 : RAW_DATA_POINTER (value) - k - 1))
10248 : : break;
10249 : : }
10250 : 76 : else if (tree_to_shwi (elt->value)
10251 : 76 : != *((const signed char *)
10252 : 76 : RAW_DATA_POINTER (value) - k - 1))
10253 : : break;
10254 : : }
10255 : 535 : start = k;
10256 : 535 : l = 0;
10257 : 1390 : for (k = 0; k < end && k + 1 < CONSTRUCTOR_NELTS (ctor) - i; ++k)
10258 : : {
10259 : 434 : constructor_elt *elt = CONSTRUCTOR_ELT (ctor, i + k + 1);
10260 : 868 : if (elt->index == NULL_TREE
10261 : 434 : || !tree_fits_uhwi_p (elt->index)
10262 : 1302 : || (wi::to_widest (elt->index)
10263 : 868 : != (wi::to_widest (index)
10264 : 1302 : + (RAW_DATA_LENGTH (value) + l))))
10265 : : break;
10266 : 434 : if (TREE_CODE (elt->value) == RAW_DATA_CST
10267 : 2 : && RAW_DATA_OWNER (elt->value) == RAW_DATA_OWNER (value)
10268 : 434 : && (RAW_DATA_POINTER (elt->value)
10269 : 2 : == RAW_DATA_POINTER (value) + l))
10270 : : {
10271 : 0 : l += RAW_DATA_LENGTH (elt->value);
10272 : 0 : end -= RAW_DATA_LENGTH (elt->value) - 1;
10273 : 0 : continue;
10274 : : }
10275 : 434 : if (!tree_fits_shwi_p (elt->value))
10276 : : break;
10277 : 431 : if (TYPE_UNSIGNED (TREE_TYPE (value)))
10278 : : {
10279 : 377 : if (tree_to_shwi (elt->value)
10280 : 377 : != *((const unsigned char *)
10281 : 377 : RAW_DATA_POINTER (value)
10282 : 377 : + RAW_DATA_LENGTH (value) + k))
10283 : : break;
10284 : : }
10285 : 54 : else if (tree_to_shwi (elt->value)
10286 : 54 : != *((const signed char *)
10287 : 54 : RAW_DATA_POINTER (value)
10288 : 54 : + RAW_DATA_LENGTH (value) + k))
10289 : : break;
10290 : 396 : ++l;
10291 : : }
10292 : 535 : end = k;
10293 : 535 : if (start != 0 || end != 0)
10294 : : {
10295 : 442 : if (j == HOST_WIDE_INT_M1U)
10296 : 404 : j = i - start;
10297 : : else
10298 : 38 : j -= start;
10299 : 442 : RAW_DATA_POINTER (value) -= start;
10300 : 442 : RAW_DATA_LENGTH (value) += start + end;
10301 : 442 : i += end;
10302 : 442 : if (start == 0)
10303 : 22 : CONSTRUCTOR_ELT (ctor, j)->index = index;
10304 : 442 : CONSTRUCTOR_ELT (ctor, j)->value = value;
10305 : 442 : ++j;
10306 : 442 : continue;
10307 : : }
10308 : : }
10309 : 771 : if (j != HOST_WIDE_INT_M1U)
10310 : : {
10311 : 175 : CONSTRUCTOR_ELT (ctor, j)->index = index;
10312 : 175 : CONSTRUCTOR_ELT (ctor, j)->value = value;
10313 : 175 : ++j;
10314 : : }
10315 : 771 : continue;
10316 : 771 : }
10317 : :
10318 : 586092 : unsigned HOST_WIDE_INT idx = i;
10319 : 586092 : if (index)
10320 : : {
10321 : 586092 : if (!tree_fits_uhwi_p (index))
10322 : : {
10323 : 22 : check_raw_data = true;
10324 : 22 : continue;
10325 : : }
10326 : 586070 : idx = tree_to_uhwi (index);
10327 : : }
10328 : :
10329 : : /* auto_vec is limited to UINT_MAX elements. */
10330 : 586070 : if (idx > UINT_MAX)
10331 : : {
10332 : 0 : check_raw_data = true;
10333 : 0 : continue;
10334 : : }
10335 : :
10336 : : /* Avoid non-constant initializers. */
10337 : 586070 : if (!tree_fits_shwi_p (value))
10338 : : {
10339 : 659 : check_raw_data = true;
10340 : 659 : --i;
10341 : 659 : continue;
10342 : : }
10343 : :
10344 : : /* Skip over embedded nuls except the last one (initializer
10345 : : elements are in ascending order of indices). */
10346 : 585411 : HOST_WIDE_INT val = tree_to_shwi (value);
10347 : 585411 : if (!val && i + 1 < nelts)
10348 : 5940 : continue;
10349 : :
10350 : 579471 : if (idx < str.length ())
10351 : : {
10352 : 0 : check_raw_data = true;
10353 : 0 : continue;
10354 : : }
10355 : :
10356 : : /* Bail if the CTOR has a block of more than 256 embedded nuls
10357 : : due to implicitly initialized elements. */
10358 : 579471 : unsigned nchars = (idx - str.length ()) + 1;
10359 : 579471 : if (nchars > 256)
10360 : : {
10361 : 3 : check_raw_data = true;
10362 : 3 : continue;
10363 : : }
10364 : :
10365 : 579468 : if (nchars > 1)
10366 : : {
10367 : 3644 : str.reserve (idx);
10368 : 3644 : str.quick_grow_cleared (idx);
10369 : : }
10370 : :
10371 : 579468 : if (idx >= maxelts)
10372 : : {
10373 : 0 : check_raw_data = true;
10374 : 0 : continue;
10375 : : }
10376 : :
10377 : 579468 : str.safe_insert (idx, val);
10378 : : }
10379 : :
10380 : 44421 : if (check_raw_data)
10381 : : {
10382 : 684 : if (j != HOST_WIDE_INT_M1U)
10383 : 404 : CONSTRUCTOR_ELTS (ctor)->truncate (j);
10384 : 684 : return ctor;
10385 : : }
10386 : :
10387 : : /* Append a nul string termination. */
10388 : 87397 : if (maxelts != HOST_WIDE_INT_M1U && str.length () < maxelts)
10389 : 17113 : str.safe_push (0);
10390 : :
10391 : : /* Build a STRING_CST with the same type as the array. */
10392 : 131211 : tree res = build_string (str.length (), str.begin ());
10393 : 43737 : TREE_TYPE (res) = type;
10394 : 43737 : return res;
10395 : 44421 : }
10396 : :
10397 : : /* Implementation of the two-argument braced_lists_to_string withe
10398 : : the same arguments plus MEMBER which is set for struct members
10399 : : to allow initializers for flexible member arrays. */
10400 : :
10401 : : static tree
10402 : 176109685 : braced_lists_to_strings (tree type, tree ctor, bool member)
10403 : : {
10404 : 176109685 : if (TREE_CODE (ctor) != CONSTRUCTOR)
10405 : : return ctor;
10406 : :
10407 : 5118129 : tree_code code = TREE_CODE (type);
10408 : :
10409 : 5118129 : tree ttp;
10410 : 5118129 : if (code == ARRAY_TYPE)
10411 : 207209 : ttp = TREE_TYPE (type);
10412 : 4910920 : else if (code == RECORD_TYPE)
10413 : : {
10414 : 4866110 : ttp = TREE_TYPE (ctor);
10415 : 4866110 : if (TREE_CODE (ttp) == ARRAY_TYPE)
10416 : : {
10417 : 222309 : type = ttp;
10418 : 222309 : ttp = TREE_TYPE (ttp);
10419 : : }
10420 : : }
10421 : : else
10422 : : return ctor;
10423 : :
10424 : 5071431 : if ((TREE_CODE (ttp) == ARRAY_TYPE || TREE_CODE (ttp) == INTEGER_TYPE)
10425 : 5353629 : && TYPE_STRING_FLAG (ttp))
10426 : 44586 : return braced_list_to_string (type, ctor, member);
10427 : :
10428 : 5028733 : code = TREE_CODE (ttp);
10429 : 5028733 : if (code == ARRAY_TYPE || RECORD_OR_UNION_TYPE_P (ttp))
10430 : : {
10431 : 4697764 : bool rec = RECORD_OR_UNION_TYPE_P (ttp);
10432 : :
10433 : : /* Handle array of arrays or struct member initializers. */
10434 : 4697764 : tree val;
10435 : 4697764 : unsigned HOST_WIDE_INT idx;
10436 : 15346375 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), idx, val)
10437 : : {
10438 : 10648611 : val = braced_lists_to_strings (ttp, val, rec);
10439 : 10648611 : CONSTRUCTOR_ELT (ctor, idx)->value = val;
10440 : : }
10441 : : }
10442 : :
10443 : : return ctor;
10444 : : }
10445 : :
10446 : : /* Attempt to convert a CTOR containing braced array initializer lists
10447 : : for array TYPE into one containing STRING_CSTs, for convenience and
10448 : : efficiency. Recurse for arrays of arrays and member initializers.
10449 : : Return the converted CTOR or STRING_CST on success or the original
10450 : : CTOR otherwise. */
10451 : :
10452 : : tree
10453 : 165461074 : braced_lists_to_strings (tree type, tree ctor)
10454 : : {
10455 : 165461074 : return braced_lists_to_strings (type, ctor, false);
10456 : : }
10457 : :
10458 : :
10459 : : /* Emit debug for functions before finalizing early debug. */
10460 : :
10461 : : void
10462 : 179332 : c_common_finalize_early_debug (void)
10463 : : {
10464 : : /* Emit early debug for reachable functions, and by consequence,
10465 : : locally scoped symbols. Also emit debug for extern declared
10466 : : functions that are still reachable at this point. */
10467 : 179332 : struct cgraph_node *cnode;
10468 : 8425942 : FOR_EACH_FUNCTION (cnode)
10469 : 3665830 : if (!cnode->alias && !cnode->thunk
10470 : 7696406 : && (cnode->has_gimple_body_p ()
10471 : 1269735 : || !DECL_IS_UNDECLARED_BUILTIN (cnode->decl)))
10472 : 3449527 : (*debug_hooks->early_global_decl) (cnode->decl);
10473 : 179332 : }
10474 : :
10475 : : /* Determine whether TYPE is an ISO C99 flexible array member type "[]". */
10476 : : bool
10477 : 10341822 : c_flexible_array_member_type_p (const_tree type)
10478 : : {
10479 : 10341822 : if (TREE_CODE (type) == ARRAY_TYPE
10480 : 3531888 : && TYPE_SIZE (type) == NULL_TREE
10481 : 465166 : && TYPE_DOMAIN (type) != NULL_TREE
10482 : 10806988 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
10483 : 465166 : return true;
10484 : :
10485 : : return false;
10486 : : }
10487 : :
10488 : : /* Get the LEVEL of the strict_flex_array for the ARRAY_FIELD based on the
10489 : : values of attribute strict_flex_array and the flag_strict_flex_arrays. */
10490 : : unsigned int
10491 : 522856 : c_strict_flex_array_level_of (tree array_field)
10492 : : {
10493 : 522856 : gcc_assert (TREE_CODE (array_field) == FIELD_DECL);
10494 : 522856 : unsigned int strict_flex_array_level = flag_strict_flex_arrays;
10495 : :
10496 : 522856 : tree attr_strict_flex_array
10497 : 522856 : = lookup_attribute ("strict_flex_array", DECL_ATTRIBUTES (array_field));
10498 : : /* If there is a strict_flex_array attribute attached to the field,
10499 : : override the flag_strict_flex_arrays. */
10500 : 522856 : if (attr_strict_flex_array)
10501 : : {
10502 : : /* Get the value of the level first from the attribute. */
10503 : 6 : unsigned HOST_WIDE_INT attr_strict_flex_array_level = 0;
10504 : 6 : gcc_assert (TREE_VALUE (attr_strict_flex_array) != NULL_TREE);
10505 : 6 : attr_strict_flex_array = TREE_VALUE (attr_strict_flex_array);
10506 : 6 : gcc_assert (TREE_VALUE (attr_strict_flex_array) != NULL_TREE);
10507 : 6 : attr_strict_flex_array = TREE_VALUE (attr_strict_flex_array);
10508 : 6 : gcc_assert (tree_fits_uhwi_p (attr_strict_flex_array));
10509 : 6 : attr_strict_flex_array_level = tree_to_uhwi (attr_strict_flex_array);
10510 : :
10511 : : /* The attribute has higher priority than flag_struct_flex_array. */
10512 : 6 : strict_flex_array_level = attr_strict_flex_array_level;
10513 : : }
10514 : 522856 : return strict_flex_array_level;
10515 : : }
10516 : :
10517 : : /* Map from identifiers to booleans. Value is true for features, and
10518 : : false for extensions. Used to implement __has_{feature,extension}. */
10519 : :
10520 : : using feature_map_t = hash_map <tree, bool>;
10521 : : static feature_map_t *feature_map;
10522 : :
10523 : : /* Register a feature for __has_{feature,extension}. FEATURE_P is true
10524 : : if the feature identified by NAME is a feature (as opposed to an
10525 : : extension). */
10526 : :
10527 : : void
10528 : 1412692 : c_common_register_feature (const char *name, bool feature_p)
10529 : : {
10530 : 1412692 : bool dup = feature_map->put (get_identifier (name), feature_p);
10531 : 1412692 : gcc_checking_assert (!dup);
10532 : 1412692 : }
10533 : :
10534 : : /* Lazily initialize hash table for __has_{feature,extension},
10535 : : dispatching to the appropriate front end to register language-specific
10536 : : features. */
10537 : :
10538 : : static void
10539 : 46169 : init_has_feature ()
10540 : : {
10541 : 46169 : gcc_checking_assert (!feature_map);
10542 : 46169 : feature_map = new feature_map_t;
10543 : :
10544 : 554028 : for (unsigned i = 0; i < ARRAY_SIZE (has_feature_table); i++)
10545 : : {
10546 : 507859 : const hf_feature_info *info = has_feature_table + i;
10547 : :
10548 : 507859 : if ((info->flags & HF_FLAG_SANITIZE) && !(flag_sanitize & info->mask))
10549 : 229969 : continue;
10550 : :
10551 : 277890 : const bool feature_p = !(info->flags & HF_FLAG_EXT);
10552 : 277890 : c_common_register_feature (info->ident, feature_p);
10553 : : }
10554 : :
10555 : : /* Register language-specific features. */
10556 : 46169 : c_family_register_lang_features ();
10557 : 46169 : }
10558 : :
10559 : : /* If STRICT_P is true, evaluate __has_feature (IDENT).
10560 : : Otherwise, evaluate __has_extension (IDENT). */
10561 : :
10562 : : bool
10563 : 125209 : has_feature_p (const char *ident, bool strict_p)
10564 : : {
10565 : 125209 : if (!feature_map)
10566 : 46169 : init_has_feature ();
10567 : :
10568 : 125209 : tree name = canonicalize_attr_name (get_identifier (ident));
10569 : 125209 : bool *feat_p = feature_map->get (name);
10570 : 125209 : if (!feat_p)
10571 : : return false;
10572 : :
10573 : 71680 : return !strict_p || *feat_p;
10574 : : }
10575 : :
10576 : : /* This is the slow path of c-common.h's c_hardbool_type_attr. */
10577 : :
10578 : : tree
10579 : 64241 : c_hardbool_type_attr_1 (tree type, tree *false_value, tree *true_value)
10580 : : {
10581 : 64241 : tree attr = lookup_attribute ("hardbool", TYPE_ATTRIBUTES (type));
10582 : 64241 : if (!attr)
10583 : : return attr;
10584 : :
10585 : 35375 : if (false_value)
10586 : 18453 : *false_value = TREE_VALUE (TYPE_VALUES (type));
10587 : :
10588 : 35375 : if (true_value)
10589 : 18817 : *true_value = TREE_VALUE (TREE_CHAIN (TYPE_VALUES (type)));
10590 : :
10591 : : return attr;
10592 : : }
10593 : :
10594 : : #include "gt-c-family-c-common.h"
|