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 : :
60 : : cpp_reader *parse_in; /* Declared in c-pragma.h. */
61 : :
62 : : /* Mode used to build pointers (VOIDmode means ptr_mode). */
63 : :
64 : : machine_mode c_default_pointer_mode = VOIDmode;
65 : :
66 : : /* The following symbols are subsumed in the c_global_trees array, and
67 : : listed here individually for documentation purposes.
68 : :
69 : : INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
70 : :
71 : : tree short_integer_type_node;
72 : : tree long_integer_type_node;
73 : : tree long_long_integer_type_node;
74 : :
75 : : tree short_unsigned_type_node;
76 : : tree long_unsigned_type_node;
77 : : tree long_long_unsigned_type_node;
78 : :
79 : : tree truthvalue_type_node;
80 : : tree truthvalue_false_node;
81 : : tree truthvalue_true_node;
82 : :
83 : : tree ptrdiff_type_node;
84 : :
85 : : tree unsigned_char_type_node;
86 : : tree signed_char_type_node;
87 : : tree wchar_type_node;
88 : :
89 : : tree char8_type_node;
90 : : tree char16_type_node;
91 : : tree char32_type_node;
92 : :
93 : : tree float_type_node;
94 : : tree double_type_node;
95 : : tree long_double_type_node;
96 : :
97 : : tree complex_integer_type_node;
98 : : tree complex_float_type_node;
99 : : tree complex_double_type_node;
100 : : tree complex_long_double_type_node;
101 : :
102 : : tree dfloat32_type_node;
103 : : tree dfloat64_type_node;
104 : : tree dfloat128_type_node;
105 : : tree dfloat64x_type_node;
106 : :
107 : : tree intQI_type_node;
108 : : tree intHI_type_node;
109 : : tree intSI_type_node;
110 : : tree intDI_type_node;
111 : : tree intTI_type_node;
112 : :
113 : : tree unsigned_intQI_type_node;
114 : : tree unsigned_intHI_type_node;
115 : : tree unsigned_intSI_type_node;
116 : : tree unsigned_intDI_type_node;
117 : : tree unsigned_intTI_type_node;
118 : :
119 : : tree widest_integer_literal_type_node;
120 : : tree widest_unsigned_literal_type_node;
121 : :
122 : : Nodes for types `void *' and `const void *'.
123 : :
124 : : tree ptr_type_node, const_ptr_type_node;
125 : :
126 : : Nodes for types `char *' and `const char *'.
127 : :
128 : : tree string_type_node, const_string_type_node;
129 : :
130 : : Type `char[SOMENUMBER]'.
131 : : Used when an array of char is needed and the size is irrelevant.
132 : :
133 : : tree char_array_type_node;
134 : :
135 : : Type `wchar_t[SOMENUMBER]' or something like it.
136 : : Used when a wide string literal is created.
137 : :
138 : : tree wchar_array_type_node;
139 : :
140 : : Type `char8_t[SOMENUMBER]' or something like it.
141 : : Used when a UTF-8 string literal is created.
142 : :
143 : : tree char8_array_type_node;
144 : :
145 : : Type `char16_t[SOMENUMBER]' or something like it.
146 : : Used when a UTF-16 string literal is created.
147 : :
148 : : tree char16_array_type_node;
149 : :
150 : : Type `char32_t[SOMENUMBER]' or something like it.
151 : : Used when a UTF-32 string literal is created.
152 : :
153 : : tree char32_array_type_node;
154 : :
155 : : Type `int ()' -- used for implicit declaration of functions.
156 : :
157 : : tree default_function_type;
158 : :
159 : : A VOID_TYPE node, packaged in a TREE_LIST.
160 : :
161 : : tree void_list_node;
162 : :
163 : : The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
164 : : and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
165 : : VAR_DECLS, but C++ does.)
166 : :
167 : : tree function_name_decl_node;
168 : : tree pretty_function_name_decl_node;
169 : : tree c99_function_name_decl_node;
170 : :
171 : : Stack of nested function name VAR_DECLs.
172 : :
173 : : tree saved_function_name_decls;
174 : :
175 : : */
176 : :
177 : : tree c_global_trees[CTI_MAX];
178 : :
179 : : /* Switches common to the C front ends. */
180 : :
181 : : /* Nonzero means don't output line number information. */
182 : :
183 : : char flag_no_line_commands;
184 : :
185 : : /* Nonzero causes -E output not to be done, but directives such as
186 : : #define that have side effects are still obeyed. */
187 : :
188 : : char flag_no_output;
189 : :
190 : : /* Nonzero means dump macros in some fashion. */
191 : :
192 : : char flag_dump_macros;
193 : :
194 : : /* Nonzero means pass #include lines through to the output. */
195 : :
196 : : char flag_dump_includes;
197 : :
198 : : /* Nonzero means process PCH files while preprocessing. */
199 : :
200 : : bool flag_pch_preprocess;
201 : :
202 : : /* The file name to which we should write a precompiled header, or
203 : : NULL if no header will be written in this compile. */
204 : :
205 : : const char *pch_file;
206 : :
207 : : /* Nonzero if an ISO standard was selected. It rejects macros in the
208 : : user's namespace. */
209 : : int flag_iso;
210 : :
211 : : /* C/ObjC language option variables. */
212 : :
213 : :
214 : : /* Nonzero means allow type mismatches in conditional expressions;
215 : : just make their values `void'. */
216 : :
217 : : int flag_cond_mismatch;
218 : :
219 : : /* Nonzero means enable C89 Amendment 1 features. */
220 : :
221 : : int flag_isoc94;
222 : :
223 : : /* Nonzero means use the ISO C99 (or later) dialect of C. */
224 : :
225 : : int flag_isoc99;
226 : :
227 : : /* Nonzero means use the ISO C11 (or later) dialect of C. */
228 : :
229 : : int flag_isoc11;
230 : :
231 : : /* Nonzero means use the ISO C23 (or later) dialect of C. */
232 : :
233 : : int flag_isoc23;
234 : :
235 : : /* Nonzero means use the ISO C2Y (or later) dialect of C. */
236 : :
237 : : int flag_isoc2y;
238 : :
239 : : /* Nonzero means that we have builtin functions, and main is an int. */
240 : :
241 : : int flag_hosted = 1;
242 : :
243 : :
244 : : /* ObjC language option variables. */
245 : :
246 : :
247 : : /* Tells the compiler that this is a special run. Do not perform any
248 : : compiling, instead we are to test some platform dependent features
249 : : and output a C header file with appropriate definitions. */
250 : :
251 : : int print_struct_values;
252 : :
253 : : /* Tells the compiler what is the constant string class for ObjC. */
254 : :
255 : : const char *constant_string_class_name;
256 : :
257 : :
258 : : /* C++ language option variables. */
259 : :
260 : : /* The reference version of the ABI for -Wabi. */
261 : :
262 : : int warn_abi_version = -1;
263 : :
264 : : /* The C++ dialect being used. Default set in c_common_post_options. */
265 : :
266 : : enum cxx_dialect cxx_dialect = cxx_unset;
267 : :
268 : : /* Maximum template instantiation depth. This limit exists to limit the
269 : : time it takes to notice excessively recursive template instantiations.
270 : :
271 : : The default is lower than the 1024 recommended by the C++0x standard
272 : : because G++ runs out of stack before 1024 with highly recursive template
273 : : argument deduction substitution (g++.dg/cpp0x/enum11.C). */
274 : :
275 : : int max_tinst_depth = 900;
276 : :
277 : : /* The elements of `ridpointers' are identifier nodes for the reserved
278 : : type names and storage classes. It is indexed by a RID_... value. */
279 : : tree *ridpointers;
280 : :
281 : : tree (*make_fname_decl) (location_t, tree, int);
282 : :
283 : : /* Nonzero means don't warn about problems that occur when the code is
284 : : executed. */
285 : : int c_inhibit_evaluation_warnings;
286 : :
287 : : /* Whether we are building a boolean conversion inside
288 : : convert_for_assignment, or some other late binary operation. If
289 : : build_binary_op is called for C (from code shared by C and C++) in
290 : : this case, then the operands have already been folded and the
291 : : result will not be folded again, so C_MAYBE_CONST_EXPR should not
292 : : be generated. */
293 : : bool in_late_binary_op;
294 : :
295 : : /* Depending on which phase of processing we are in, we may need
296 : : to prefer input_location to libcpp's locations. (Specifically,
297 : : after the C++ lexer is done lexing tokens, but prior to calling
298 : : cpp_finish (), we need to do so. */
299 : : bool override_libcpp_locations;
300 : :
301 : : /* Information about how a function name is generated. */
302 : : struct fname_var_t
303 : : {
304 : : tree *const decl; /* pointer to the VAR_DECL. */
305 : : const unsigned rid; /* RID number for the identifier. */
306 : : const int pretty; /* How pretty is it? */
307 : : };
308 : :
309 : : /* The three ways of getting then name of the current function. */
310 : :
311 : : const struct fname_var_t fname_vars[] =
312 : : {
313 : : /* C99 compliant __func__, must be first. */
314 : : {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
315 : : /* GCC __FUNCTION__ compliant. */
316 : : {&function_name_decl_node, RID_FUNCTION_NAME, 0},
317 : : /* GCC __PRETTY_FUNCTION__ compliant. */
318 : : {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
319 : : {NULL, 0, 0},
320 : : };
321 : :
322 : : /* Flags to restrict availability of generic features that
323 : : are known to __has_{feature,extension}. */
324 : :
325 : : enum
326 : : {
327 : : HF_FLAG_NONE = 0,
328 : : HF_FLAG_EXT = 1, /* Available only as an extension. */
329 : : HF_FLAG_SANITIZE = 2, /* Availability depends on sanitizer flags. */
330 : : };
331 : :
332 : : /* Info for generic features which can be queried through
333 : : __has_{feature,extension}. */
334 : :
335 : : struct hf_feature_info
336 : : {
337 : : const char *ident;
338 : : unsigned flags;
339 : : unsigned mask;
340 : : };
341 : :
342 : : /* Table of generic features which can be queried through
343 : : __has_{feature,extension}. */
344 : :
345 : : static constexpr hf_feature_info has_feature_table[] =
346 : : {
347 : : { "address_sanitizer", HF_FLAG_SANITIZE, SANITIZE_ADDRESS },
348 : : { "thread_sanitizer", HF_FLAG_SANITIZE, SANITIZE_THREAD },
349 : : { "leak_sanitizer", HF_FLAG_SANITIZE, SANITIZE_LEAK },
350 : : { "hwaddress_sanitizer", HF_FLAG_SANITIZE, SANITIZE_HWADDRESS },
351 : : { "undefined_behavior_sanitizer", HF_FLAG_SANITIZE, SANITIZE_UNDEFINED },
352 : : { "attribute_deprecated_with_message", HF_FLAG_NONE, 0 },
353 : : { "attribute_unavailable_with_message", HF_FLAG_NONE, 0 },
354 : : { "enumerator_attributes", HF_FLAG_NONE, 0 },
355 : : { "tls", HF_FLAG_NONE, 0 },
356 : : { "gnu_asm_goto_with_outputs", HF_FLAG_EXT, 0 },
357 : : { "gnu_asm_goto_with_outputs_full", HF_FLAG_EXT, 0 }
358 : : };
359 : :
360 : : /* Global visibility options. */
361 : : struct visibility_flags visibility_options;
362 : :
363 : : static tree check_case_value (location_t, tree);
364 : :
365 : :
366 : : static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
367 : : static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
368 : :
369 : : /* Reserved words. The third field is a mask: keywords are disabled
370 : : if they match the mask.
371 : :
372 : : Masks for languages:
373 : : C --std=c89: D_C99 | D_C23 | D_CXXONLY | D_OBJC | D_CXX_OBJC
374 : : C --std=c99: D_C23 | D_CXXONLY | D_OBJC
375 : : C --std=c17: D_C23 | D_CXXONLY | D_OBJC
376 : : C --std=c23: D_CXXONLY | D_OBJC
377 : : ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
378 : : C++ --std=c++98: D_CONLY | D_CXX11 | D_CXX20 | D_OBJC
379 : : C++ --std=c++11: D_CONLY | D_CXX20 | D_OBJC
380 : : C++ --std=c++20: D_CONLY | D_OBJC
381 : : ObjC++ is like C++ except that D_OBJC is not set
382 : :
383 : : If -fno-asm is used, D_ASM is added to the mask. If
384 : : -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
385 : : C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
386 : : In C with -Wc++-compat, we warn if D_CXXWARN is set.
387 : :
388 : : Note the complication of the D_CXX_OBJC keywords. These are
389 : : reserved words such as 'class'. In C++, 'class' is a reserved
390 : : word. In Objective-C++ it is too. In Objective-C, it is a
391 : : reserved word too, but only if it follows an '@' sign.
392 : : */
393 : : const struct c_common_resword c_common_reswords[] =
394 : : {
395 : : { "_Alignas", RID_ALIGNAS, D_CONLY },
396 : : { "_Alignof", RID_ALIGNOF, D_CONLY },
397 : : { "_Countof", RID_COUNTOF, D_CONLY },
398 : : { "_Atomic", RID_ATOMIC, D_CONLY },
399 : : { "_BitInt", RID_BITINT, D_CONLY },
400 : : { "_Bool", RID_BOOL, D_CONLY },
401 : : { "_Complex", RID_COMPLEX, 0 },
402 : : { "_Imaginary", RID_IMAGINARY, D_CONLY },
403 : : { "_Float16", RID_FLOAT16, 0 },
404 : : { "_Float32", RID_FLOAT32, 0 },
405 : : { "_Float64", RID_FLOAT64, 0 },
406 : : { "_Float128", RID_FLOAT128, 0 },
407 : : { "_Float32x", RID_FLOAT32X, 0 },
408 : : { "_Float64x", RID_FLOAT64X, 0 },
409 : : { "_Float128x", RID_FLOAT128X, 0 },
410 : : { "_Decimal32", RID_DFLOAT32, D_CONLY },
411 : : { "_Decimal64", RID_DFLOAT64, D_CONLY },
412 : : { "_Decimal128", RID_DFLOAT128, D_CONLY },
413 : : { "_Decimal64x", RID_DFLOAT64X, D_CONLY },
414 : : { "_Fract", RID_FRACT, D_CONLY | D_EXT },
415 : : { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
416 : : { "_Sat", RID_SAT, D_CONLY | D_EXT },
417 : : { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
418 : : { "_Noreturn", RID_NORETURN, D_CONLY },
419 : : { "_Generic", RID_GENERIC, D_CONLY },
420 : : { "_Thread_local", RID_THREAD, D_CONLY },
421 : : { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
422 : : { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
423 : : { "__alignof", RID_ALIGNOF, 0 },
424 : : { "__alignof__", RID_ALIGNOF, 0 },
425 : : { "__asm", RID_ASM, 0 },
426 : : { "__asm__", RID_ASM, 0 },
427 : : { "__attribute", RID_ATTRIBUTE, 0 },
428 : : { "__attribute__", RID_ATTRIBUTE, 0 },
429 : : { "__auto_type", RID_AUTO_TYPE, D_CONLY },
430 : : { "__builtin_addressof", RID_ADDRESSOF, D_CXXONLY },
431 : : { "__builtin_assoc_barrier", RID_BUILTIN_ASSOC_BARRIER, 0 },
432 : : { "__builtin_bit_cast", RID_BUILTIN_BIT_CAST, D_CXXONLY },
433 : : { "__builtin_call_with_static_chain",
434 : : RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
435 : : { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
436 : : { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
437 : : { "__builtin_convertvector", RID_BUILTIN_CONVERTVECTOR, 0 },
438 : : { "__builtin_counted_by_ref", RID_BUILTIN_COUNTED_BY_REF, D_CONLY },
439 : : { "__builtin_has_attribute", RID_BUILTIN_HAS_ATTRIBUTE, 0 },
440 : : { "__builtin_launder", RID_BUILTIN_LAUNDER, D_CXXONLY },
441 : : { "__builtin_operator_new", RID_BUILTIN_OPERATOR_NEW, D_CXXONLY },
442 : : { "__builtin_operator_delete", RID_BUILTIN_OPERATOR_DELETE, D_CXXONLY },
443 : : { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
444 : : { "__builtin_shufflevector", RID_BUILTIN_SHUFFLEVECTOR, 0 },
445 : : { "__builtin_stdc_bit_ceil", RID_BUILTIN_STDC, D_CONLY },
446 : : { "__builtin_stdc_bit_floor", RID_BUILTIN_STDC, D_CONLY },
447 : : { "__builtin_stdc_bit_width", RID_BUILTIN_STDC, D_CONLY },
448 : : { "__builtin_stdc_count_ones", RID_BUILTIN_STDC, D_CONLY },
449 : : { "__builtin_stdc_count_zeros", RID_BUILTIN_STDC, D_CONLY },
450 : : { "__builtin_stdc_first_leading_one", RID_BUILTIN_STDC, D_CONLY },
451 : : { "__builtin_stdc_first_leading_zero", RID_BUILTIN_STDC, D_CONLY },
452 : : { "__builtin_stdc_first_trailing_one", RID_BUILTIN_STDC, D_CONLY },
453 : : { "__builtin_stdc_first_trailing_zero", RID_BUILTIN_STDC, D_CONLY },
454 : : { "__builtin_stdc_has_single_bit", RID_BUILTIN_STDC, D_CONLY },
455 : : { "__builtin_stdc_leading_ones", RID_BUILTIN_STDC, D_CONLY },
456 : : { "__builtin_stdc_leading_zeros", RID_BUILTIN_STDC, D_CONLY },
457 : : { "__builtin_stdc_rotate_left", RID_BUILTIN_STDC, D_CONLY },
458 : : { "__builtin_stdc_rotate_right", RID_BUILTIN_STDC, D_CONLY },
459 : : { "__builtin_stdc_trailing_ones", RID_BUILTIN_STDC, D_CONLY },
460 : : { "__builtin_stdc_trailing_zeros", RID_BUILTIN_STDC, D_CONLY },
461 : : { "__builtin_tgmath", RID_BUILTIN_TGMATH, D_CONLY },
462 : : { "__builtin_offsetof", RID_OFFSETOF, 0 },
463 : : { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
464 : : { "__builtin_c23_va_start", RID_C23_VA_START, D_C23 },
465 : : { "__builtin_va_arg", RID_VA_ARG, 0 },
466 : : { "__complex", RID_COMPLEX, 0 },
467 : : { "__complex__", RID_COMPLEX, 0 },
468 : : { "__const", RID_CONST, 0 },
469 : : { "__const__", RID_CONST, 0 },
470 : : { "__constinit", RID_CONSTINIT, D_CXXONLY },
471 : : { "__decltype", RID_DECLTYPE, D_CXXONLY },
472 : : { "__extension__", RID_EXTENSION, 0 },
473 : : { "__func__", RID_C99_FUNCTION_NAME, 0 },
474 : : { "__imag", RID_IMAGPART, 0 },
475 : : { "__imag__", RID_IMAGPART, 0 },
476 : : { "__inline", RID_INLINE, 0 },
477 : : { "__inline__", RID_INLINE, 0 },
478 : : { "__label__", RID_LABEL, 0 },
479 : : { "__null", RID_NULL, 0 },
480 : : { "__real", RID_REALPART, 0 },
481 : : { "__real__", RID_REALPART, 0 },
482 : : { "__restrict", RID_RESTRICT, 0 },
483 : : { "__restrict__", RID_RESTRICT, 0 },
484 : : { "__signed", RID_SIGNED, 0 },
485 : : { "__signed__", RID_SIGNED, 0 },
486 : : { "__thread", RID_THREAD, 0 },
487 : : { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
488 : : { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
489 : : { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
490 : : { "__typeof", RID_TYPEOF, 0 },
491 : : { "__typeof__", RID_TYPEOF, 0 },
492 : : { "__typeof_unqual", RID_TYPEOF_UNQUAL, D_CONLY },
493 : : { "__typeof_unqual__", RID_TYPEOF_UNQUAL, D_CONLY },
494 : : { "__volatile", RID_VOLATILE, 0 },
495 : : { "__volatile__", RID_VOLATILE, 0 },
496 : : { "__GIMPLE", RID_GIMPLE, D_CONLY },
497 : : { "__PHI", RID_PHI, D_CONLY },
498 : : { "__RTL", RID_RTL, D_CONLY },
499 : : { "alignas", RID_ALIGNAS, D_C23 | D_CXX11 | D_CXXWARN },
500 : : { "alignof", RID_ALIGNOF, D_C23 | D_CXX11 | D_CXXWARN },
501 : : { "asm", RID_ASM, D_ASM },
502 : : { "auto", RID_AUTO, 0 },
503 : : { "bool", RID_BOOL, D_C23 | D_CXXWARN },
504 : : { "break", RID_BREAK, 0 },
505 : : { "case", RID_CASE, 0 },
506 : : { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
507 : : { "char", RID_CHAR, 0 },
508 : : { "char8_t", RID_CHAR8, D_CXX_CHAR8_T_FLAGS | D_CXXWARN },
509 : : { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
510 : : { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
511 : : { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
512 : : { "const", RID_CONST, 0 },
513 : : { "consteval", RID_CONSTEVAL, D_CXXONLY | D_CXX20 | D_CXXWARN },
514 : : { "constexpr", RID_CONSTEXPR, D_C23 | D_CXX11 | D_CXXWARN },
515 : : { "constinit", RID_CONSTINIT, D_CXXONLY | D_CXX20 | D_CXXWARN },
516 : : { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
517 : : { "continue", RID_CONTINUE, 0 },
518 : : { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
519 : : { "default", RID_DEFAULT, 0 },
520 : : { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
521 : : { "do", RID_DO, 0 },
522 : : { "double", RID_DOUBLE, 0 },
523 : : { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
524 : : { "else", RID_ELSE, 0 },
525 : : { "enum", RID_ENUM, 0 },
526 : : { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
527 : : { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
528 : : { "extern", RID_EXTERN, 0 },
529 : : { "false", RID_FALSE, D_C23 | D_CXXWARN },
530 : : { "float", RID_FLOAT, 0 },
531 : : { "for", RID_FOR, 0 },
532 : : { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
533 : : { "goto", RID_GOTO, 0 },
534 : : { "if", RID_IF, 0 },
535 : : { "inline", RID_INLINE, D_EXT89 },
536 : : { "int", RID_INT, 0 },
537 : : { "long", RID_LONG, 0 },
538 : : { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
539 : : { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
540 : : { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
541 : : { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
542 : : { "nullptr", RID_NULLPTR, D_C23 | D_CXX11 | D_CXXWARN },
543 : : { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
544 : : { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
545 : : { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
546 : : { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
547 : : { "register", RID_REGISTER, 0 },
548 : : { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
549 : : { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
550 : : { "return", RID_RETURN, 0 },
551 : : { "short", RID_SHORT, 0 },
552 : : { "signed", RID_SIGNED, 0 },
553 : : { "sizeof", RID_SIZEOF, 0 },
554 : : { "static", RID_STATIC, 0 },
555 : : { "static_assert", RID_STATIC_ASSERT, D_C23 | D_CXX11 | D_CXXWARN },
556 : : { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
557 : : { "struct", RID_STRUCT, 0 },
558 : : { "switch", RID_SWITCH, 0 },
559 : : { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
560 : : { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
561 : : { "thread_local", RID_THREAD, D_C23 | D_CXX11 | D_CXXWARN },
562 : : { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
563 : : { "true", RID_TRUE, D_C23 | D_CXXWARN },
564 : : { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
565 : : { "typedef", RID_TYPEDEF, 0 },
566 : : { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
567 : : { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
568 : : { "typeof", RID_TYPEOF, D_EXT11 },
569 : : { "typeof_unqual", RID_TYPEOF_UNQUAL, D_CONLY | D_C23 },
570 : : { "union", RID_UNION, 0 },
571 : : { "unsigned", RID_UNSIGNED, 0 },
572 : : { "using", RID_USING, D_CXXONLY | D_CXXWARN },
573 : : { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
574 : : { "void", RID_VOID, 0 },
575 : : { "volatile", RID_VOLATILE, 0 },
576 : : { "wchar_t", RID_WCHAR, D_CXXONLY },
577 : : { "while", RID_WHILE, 0 },
578 : :
579 : : /* C++ transactional memory. */
580 : : { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
581 : : { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
582 : : { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
583 : : { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
584 : :
585 : : /* Concepts-related keywords */
586 : : { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
587 : : { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
588 : :
589 : : /* Modules-related keywords, these are internal unspellable tokens,
590 : : created by the preprocessor. */
591 : : { "module ", RID__MODULE, D_CXX_MODULES_FLAGS | D_CXXWARN },
592 : : { "import ", RID__IMPORT, D_CXX_MODULES_FLAGS | D_CXXWARN },
593 : : { "export ", RID__EXPORT, D_CXX_MODULES_FLAGS | D_CXXWARN },
594 : :
595 : : /* Coroutines-related keywords */
596 : : { "co_await", RID_CO_AWAIT, D_CXX_COROUTINES_FLAGS | D_CXXWARN },
597 : : { "co_yield", RID_CO_YIELD, D_CXX_COROUTINES_FLAGS | D_CXXWARN },
598 : : { "co_return", RID_CO_RETURN, D_CXX_COROUTINES_FLAGS | D_CXXWARN },
599 : :
600 : : /* These Objective-C keywords are recognized only immediately after
601 : : an '@'. */
602 : : { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
603 : : { "defs", RID_AT_DEFS, D_OBJC },
604 : : { "encode", RID_AT_ENCODE, D_OBJC },
605 : : { "end", RID_AT_END, D_OBJC },
606 : : { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
607 : : { "interface", RID_AT_INTERFACE, D_OBJC },
608 : : { "protocol", RID_AT_PROTOCOL, D_OBJC },
609 : : { "selector", RID_AT_SELECTOR, D_OBJC },
610 : : { "finally", RID_AT_FINALLY, D_OBJC },
611 : : { "optional", RID_AT_OPTIONAL, D_OBJC },
612 : : { "required", RID_AT_REQUIRED, D_OBJC },
613 : : { "property", RID_AT_PROPERTY, D_OBJC },
614 : : { "package", RID_AT_PACKAGE, D_OBJC },
615 : : { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
616 : : { "dynamic", RID_AT_DYNAMIC, D_OBJC },
617 : : /* These are recognized only in protocol-qualifier context
618 : : (see above) */
619 : : { "bycopy", RID_BYCOPY, D_OBJC },
620 : : { "byref", RID_BYREF, D_OBJC },
621 : : { "in", RID_IN, D_OBJC },
622 : : { "inout", RID_INOUT, D_OBJC },
623 : : { "oneway", RID_ONEWAY, D_OBJC },
624 : : { "out", RID_OUT, D_OBJC },
625 : : /* These are recognized inside a property attribute list */
626 : : { "assign", RID_ASSIGN, D_OBJC },
627 : : { "atomic", RID_PROPATOMIC, D_OBJC },
628 : : { "copy", RID_COPY, D_OBJC },
629 : : { "getter", RID_GETTER, D_OBJC },
630 : : { "nonatomic", RID_NONATOMIC, D_OBJC },
631 : : { "readonly", RID_READONLY, D_OBJC },
632 : : { "readwrite", RID_READWRITE, D_OBJC },
633 : : { "retain", RID_RETAIN, D_OBJC },
634 : : { "setter", RID_SETTER, D_OBJC },
635 : : /* These are Objective C implementation of nullability, accepted only in
636 : : specific contexts. */
637 : : { "null_unspecified", RID_NULL_UNSPECIFIED, D_OBJC },
638 : : { "nullable", RID_NULLABLE, D_OBJC },
639 : : { "nonnull", RID_NONNULL, D_OBJC },
640 : : { "null_resettable", RID_NULL_RESETTABLE, D_OBJC },
641 : : };
642 : :
643 : : const unsigned int num_c_common_reswords = ARRAY_SIZE (c_common_reswords);
644 : :
645 : : /* Return identifier for address space AS. */
646 : :
647 : : const char *
648 : 3 : c_addr_space_name (addr_space_t as)
649 : : {
650 : 3 : int rid = RID_FIRST_ADDR_SPACE + as;
651 : 3 : gcc_assert (ridpointers [rid]);
652 : 3 : return IDENTIFIER_POINTER (ridpointers [rid]);
653 : : }
654 : :
655 : : /* Push current bindings for the function name VAR_DECLS. */
656 : :
657 : : void
658 : 175005215 : start_fname_decls (void)
659 : : {
660 : 175005215 : unsigned ix;
661 : 175005215 : tree saved = NULL_TREE;
662 : :
663 : 700020860 : for (ix = 0; fname_vars[ix].decl; ix++)
664 : : {
665 : 525015645 : tree decl = *fname_vars[ix].decl;
666 : :
667 : 525015645 : if (decl)
668 : : {
669 : 46535 : saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
670 : : saved);
671 : 46535 : *fname_vars[ix].decl = NULL_TREE;
672 : : }
673 : : }
674 : 175005215 : if (saved || saved_function_name_decls)
675 : : /* Normally they'll have been NULL, so only push if we've got a
676 : : stack, or they are non-NULL. */
677 : 63830 : saved_function_name_decls = tree_cons (saved, NULL_TREE,
678 : : saved_function_name_decls);
679 : 175005215 : }
680 : :
681 : : /* Finish up the current bindings, adding them into the current function's
682 : : statement tree. This must be done _before_ finish_stmt_tree is called.
683 : : If there is no current function, we must be at file scope and no statements
684 : : are involved. Pop the previous bindings. */
685 : :
686 : : void
687 : 174892650 : finish_fname_decls (void)
688 : : {
689 : 174892650 : unsigned ix;
690 : 174892650 : tree stmts = NULL_TREE;
691 : 174892650 : tree stack = saved_function_name_decls;
692 : :
693 : 175149030 : for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
694 : 96275 : append_to_statement_list (TREE_VALUE (stack), &stmts);
695 : :
696 : 174892650 : if (stmts)
697 : : {
698 : 96170 : tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
699 : :
700 : 96170 : if (TREE_CODE (*bodyp) == BIND_EXPR)
701 : 91843 : bodyp = &BIND_EXPR_BODY (*bodyp);
702 : :
703 : 96170 : append_to_statement_list_force (*bodyp, &stmts);
704 : 96170 : *bodyp = stmts;
705 : : }
706 : :
707 : 699570600 : for (ix = 0; fname_vars[ix].decl; ix++)
708 : 524677950 : *fname_vars[ix].decl = NULL_TREE;
709 : :
710 : 174892650 : if (stack)
711 : : {
712 : : /* We had saved values, restore them. */
713 : 63830 : tree saved;
714 : :
715 : 110365 : for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
716 : : {
717 : 46535 : tree decl = TREE_PURPOSE (saved);
718 : 46535 : unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
719 : :
720 : 46535 : *fname_vars[ix].decl = decl;
721 : : }
722 : 63830 : stack = TREE_CHAIN (stack);
723 : : }
724 : 174892650 : saved_function_name_decls = stack;
725 : 174892650 : }
726 : :
727 : : /* Return the text name of the current function, suitably prettified
728 : : by PRETTY_P. Return string must be freed by caller. */
729 : :
730 : : const char *
731 : 3340 : fname_as_string (int pretty_p)
732 : : {
733 : 3340 : const char *name = "top level";
734 : 3340 : char *namep;
735 : 3340 : int vrb = 2, len;
736 : 3340 : cpp_string cstr = { 0, 0 }, strname;
737 : :
738 : 3340 : if (!pretty_p)
739 : : {
740 : 1867 : name = "";
741 : 1867 : vrb = 0;
742 : : }
743 : :
744 : 3340 : if (current_function_decl)
745 : 3333 : name = lang_hooks.decl_printable_name (current_function_decl, vrb);
746 : :
747 : 3340 : len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
748 : :
749 : 3340 : namep = XNEWVEC (char, len);
750 : 3340 : snprintf (namep, len, "\"%s\"", name);
751 : 3340 : strname.text = (unsigned char *) namep;
752 : 3340 : strname.len = len - 1;
753 : :
754 : 3340 : if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
755 : : {
756 : 3340 : XDELETEVEC (namep);
757 : 3340 : return (const char *) cstr.text;
758 : : }
759 : :
760 : : return namep;
761 : : }
762 : :
763 : : /* Return the VAR_DECL for a const char array naming the current
764 : : function. If the VAR_DECL has not yet been created, create it
765 : : now. RID indicates how it should be formatted and IDENTIFIER_NODE
766 : : ID is its name (unfortunately C and C++ hold the RID values of
767 : : keywords in different places, so we can't derive RID from ID in
768 : : this language independent code. LOC is the location of the
769 : : function. */
770 : :
771 : : tree
772 : 192549 : fname_decl (location_t loc, unsigned int rid, tree id)
773 : : {
774 : 192549 : unsigned ix;
775 : 192549 : tree decl = NULL_TREE;
776 : :
777 : 557133 : for (ix = 0; fname_vars[ix].decl; ix++)
778 : 557133 : if (fname_vars[ix].rid == rid)
779 : : break;
780 : :
781 : 192549 : decl = *fname_vars[ix].decl;
782 : 192549 : if (!decl)
783 : : {
784 : : /* If a tree is built here, it would normally have the lineno of
785 : : the current statement. Later this tree will be moved to the
786 : : beginning of the function and this line number will be wrong.
787 : : To avoid this problem set the lineno to 0 here; that prevents
788 : : it from appearing in the RTL. */
789 : 96275 : tree stmts;
790 : 96275 : location_t saved_location = input_location;
791 : 96275 : input_location = UNKNOWN_LOCATION;
792 : :
793 : 96275 : stmts = push_stmt_list ();
794 : 96275 : decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
795 : 96275 : stmts = pop_stmt_list (stmts);
796 : 96275 : if (!IS_EMPTY_STMT (stmts))
797 : 96275 : saved_function_name_decls
798 : 96275 : = tree_cons (decl, stmts, saved_function_name_decls);
799 : 96275 : *fname_vars[ix].decl = decl;
800 : 96275 : input_location = saved_location;
801 : : }
802 : 192549 : if (!ix && !current_function_decl)
803 : 4 : pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
804 : :
805 : 192549 : return decl;
806 : : }
807 : :
808 : : /* Given a STRING_CST, give it a suitable array-of-chars data type. */
809 : :
810 : : tree
811 : 24829122 : fix_string_type (tree value)
812 : : {
813 : 24829122 : int length = TREE_STRING_LENGTH (value);
814 : 24829122 : int nchars, charsz;
815 : 24829122 : tree e_type, i_type, a_type;
816 : :
817 : : /* Compute the number of elements, for the array type. */
818 : 24829122 : if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
819 : : {
820 : 24631594 : charsz = 1;
821 : 24631594 : e_type = char_type_node;
822 : : }
823 : 197528 : else if (flag_char8_t && TREE_TYPE (value) == char8_array_type_node)
824 : : {
825 : 341 : charsz = TYPE_PRECISION (char8_type_node) / BITS_PER_UNIT;
826 : 341 : e_type = char8_type_node;
827 : : }
828 : 197187 : else if (TREE_TYPE (value) == char16_array_type_node)
829 : : {
830 : 890 : charsz = TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT;
831 : 890 : e_type = char16_type_node;
832 : : }
833 : 196297 : else if (TREE_TYPE (value) == char32_array_type_node)
834 : : {
835 : 2979 : charsz = TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT;
836 : 2979 : e_type = char32_type_node;
837 : : }
838 : : else
839 : : {
840 : 193318 : charsz = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
841 : 193318 : e_type = wchar_type_node;
842 : : }
843 : :
844 : : /* This matters only for targets where ssizetype has smaller precision
845 : : than 32 bits. */
846 : 24829122 : if (wi::lts_p (wi::to_wide (TYPE_MAX_VALUE (ssizetype)), length))
847 : : {
848 : 0 : error ("size of string literal is too large");
849 : 0 : length = tree_to_shwi (TYPE_MAX_VALUE (ssizetype)) / charsz * charsz;
850 : 0 : char *str = CONST_CAST (char *, TREE_STRING_POINTER (value));
851 : 0 : memset (str + length, '\0',
852 : 0 : MIN (TREE_STRING_LENGTH (value) - length, charsz));
853 : 0 : TREE_STRING_LENGTH (value) = length;
854 : : }
855 : 24829122 : nchars = length / charsz;
856 : :
857 : : /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
858 : : limit in C++98 Annex B is very large (65536) and is not normative,
859 : : so we do not diagnose it (warn_overlength_strings is forced off
860 : : in c_common_post_options). */
861 : 24829122 : if (warn_overlength_strings)
862 : : {
863 : 14925 : const int nchars_max = flag_isoc99 ? 4095 : 509;
864 : 14925 : const int relevant_std = flag_isoc99 ? 99 : 90;
865 : 14925 : if (nchars - 1 > nchars_max)
866 : : /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
867 : : separate the %d from the 'C'. 'ISO' should not be
868 : : translated, but it may be moved after 'C%d' in languages
869 : : where modifiers follow nouns. */
870 : 5 : pedwarn (input_location, OPT_Woverlength_strings,
871 : : "string length %qd is greater than the length %qd "
872 : : "ISO C%d compilers are required to support",
873 : : nchars - 1, nchars_max, relevant_std);
874 : : }
875 : :
876 : : /* Create the array type for the string constant. The ISO C++
877 : : standard says that a string literal has type `const char[N]' or
878 : : `const wchar_t[N]'. We use the same logic when invoked as a C
879 : : front-end with -Wwrite-strings.
880 : : ??? We should change the type of an expression depending on the
881 : : state of a warning flag. We should just be warning -- see how
882 : : this is handled in the C++ front-end for the deprecated implicit
883 : : conversion from string literals to `char*' or `wchar_t*'.
884 : :
885 : : The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
886 : : array type being the unqualified version of that type.
887 : : Therefore, if we are constructing an array of const char, we must
888 : : construct the matching unqualified array type first. The C front
889 : : end does not require this, but it does no harm, so we do it
890 : : unconditionally. */
891 : 24829122 : i_type = build_index_type (size_int (nchars - 1));
892 : 24829122 : a_type = build_array_type (e_type, i_type);
893 : 24829122 : if (c_dialect_cxx() || warn_write_strings)
894 : 22481785 : a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
895 : :
896 : 24829122 : TREE_TYPE (value) = a_type;
897 : 24829122 : TREE_CONSTANT (value) = 1;
898 : 24829122 : TREE_READONLY (value) = 1;
899 : 24829122 : TREE_STATIC (value) = 1;
900 : 24829122 : return value;
901 : : }
902 : :
903 : : /* Given a string of type STRING_TYPE, determine what kind of string
904 : : token would give an equivalent execution encoding: CPP_STRING,
905 : : CPP_STRING16, or CPP_STRING32. Return CPP_OTHER in case of error.
906 : : This may not be exactly the string token type that initially created
907 : : the string, since CPP_WSTRING is indistinguishable from the 16/32 bit
908 : : string type, and CPP_UTF8STRING is indistinguishable from CPP_STRING
909 : : at this point.
910 : :
911 : : This effectively reverses part of the logic in lex_string and
912 : : fix_string_type. */
913 : :
914 : : static enum cpp_ttype
915 : 11254 : get_cpp_ttype_from_string_type (tree string_type)
916 : : {
917 : 11254 : gcc_assert (string_type);
918 : 11254 : if (TREE_CODE (string_type) == POINTER_TYPE)
919 : 5967 : string_type = TREE_TYPE (string_type);
920 : :
921 : 11254 : if (TREE_CODE (string_type) != ARRAY_TYPE)
922 : : return CPP_OTHER;
923 : :
924 : 11254 : tree element_type = TREE_TYPE (string_type);
925 : 11254 : if (TREE_CODE (element_type) != INTEGER_TYPE)
926 : : return CPP_OTHER;
927 : :
928 : 11254 : int bits_per_character = TYPE_PRECISION (element_type);
929 : 11254 : switch (bits_per_character)
930 : : {
931 : : case 8:
932 : : return CPP_STRING; /* It could have also been CPP_UTF8STRING. */
933 : 2 : case 16:
934 : 2 : return CPP_STRING16;
935 : 4 : case 32:
936 : 4 : return CPP_STRING32;
937 : : }
938 : :
939 : : return CPP_OTHER;
940 : : }
941 : :
942 : : /* The global record of string concatentations, for use in
943 : : extracting locations within string literals. */
944 : :
945 : : GTY(()) string_concat_db *g_string_concat_db;
946 : :
947 : : /* Implementation of LANG_HOOKS_GET_SUBSTRING_LOCATION. */
948 : :
949 : : const char *
950 : 11254 : c_get_substring_location (const substring_loc &substr_loc,
951 : : location_t *out_loc)
952 : : {
953 : 11254 : enum cpp_ttype tok_type
954 : 11254 : = get_cpp_ttype_from_string_type (substr_loc.get_string_type ());
955 : 11254 : if (tok_type == CPP_OTHER)
956 : : return "unrecognized string type";
957 : :
958 : 11254 : return get_location_within_string (parse_in,
959 : : global_dc->get_file_cache (),
960 : : g_string_concat_db,
961 : : substr_loc.get_fmt_string_loc (),
962 : : tok_type,
963 : : substr_loc.get_caret_idx (),
964 : : substr_loc.get_start_idx (),
965 : : substr_loc.get_end_idx (),
966 : 11254 : out_loc);
967 : : }
968 : :
969 : :
970 : : /* Return true iff T is a boolean promoted to int. */
971 : :
972 : : bool
973 : 74438 : bool_promoted_to_int_p (tree t)
974 : : {
975 : 57306 : return (CONVERT_EXPR_P (t)
976 : 17232 : && TREE_TYPE (t) == integer_type_node
977 : 74538 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == BOOLEAN_TYPE);
978 : : }
979 : :
980 : : /* vector_targets_convertible_p is used for vector pointer types. The
981 : : callers perform various checks that the qualifiers are satisfactory,
982 : : while OTOH vector_targets_convertible_p ignores the number of elements
983 : : in the vectors. That's fine with vector pointers as we can consider,
984 : : say, a vector of 8 elements as two consecutive vectors of 4 elements,
985 : : and that does not require and conversion of the pointer values.
986 : : In contrast, vector_types_convertible_p and
987 : : vector_types_compatible_elements_p are used for vector value types. */
988 : : /* True if pointers to distinct types T1 and T2 can be converted to
989 : : each other without an explicit cast. Only returns true for opaque
990 : : vector types. */
991 : : bool
992 : 1993221 : vector_targets_convertible_p (const_tree t1, const_tree t2)
993 : : {
994 : 953474 : if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
995 : 953298 : && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
996 : 1993221 : && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
997 : : return true;
998 : :
999 : : return false;
1000 : : }
1001 : :
1002 : : /* vector_types_convertible_p is used for vector value types.
1003 : : It could in principle call vector_targets_convertible_p as a subroutine,
1004 : : but then the check for vector type would be duplicated with its callers,
1005 : : and also the purpose of vector_targets_convertible_p would become
1006 : : muddled.
1007 : : Where vector_types_convertible_p returns true, a conversion might still be
1008 : : needed to make the types match.
1009 : : In contrast, vector_targets_convertible_p is used for vector pointer
1010 : : values, and vector_types_compatible_elements_p is used specifically
1011 : : in the context for binary operators, as a check if use is possible without
1012 : : conversion. */
1013 : : /* True if vector types T1 and T2 can be converted to each other
1014 : : without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
1015 : : can only be converted with -flax-vector-conversions yet that is not
1016 : : in effect, emit a note telling the user about that option if such
1017 : : a note has not previously been emitted. */
1018 : : bool
1019 : 17768028 : vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
1020 : : {
1021 : 17768028 : static bool emitted_lax_note = false;
1022 : 17768028 : bool convertible_lax;
1023 : :
1024 : 35531584 : if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
1025 : 17769534 : && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
1026 : : return true;
1027 : :
1028 : 35524216 : convertible_lax =
1029 : 17762108 : (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
1030 : 17758618 : && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE
1031 : 10463136 : || known_eq (TYPE_VECTOR_SUBPARTS (t1),
1032 : : TYPE_VECTOR_SUBPARTS (t2)))
1033 : 53223042 : && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
1034 : 17730467 : == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
1035 : :
1036 : 17762108 : if (!convertible_lax || flag_lax_vector_conversions)
1037 : 64321 : return convertible_lax;
1038 : :
1039 : 17697787 : if (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
1040 : 17697787 : && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
1041 : 16355441 : return true;
1042 : :
1043 : 1342346 : if (emit_lax_note && !emitted_lax_note)
1044 : : {
1045 : 10 : emitted_lax_note = true;
1046 : 10 : inform (input_location, "use %<-flax-vector-conversions%> to permit "
1047 : : "conversions between vectors with differing "
1048 : : "element types or numbers of subparts");
1049 : : }
1050 : :
1051 : : return false;
1052 : : }
1053 : :
1054 : : /* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
1055 : : and have vector types, V0 has the same type as V1, and the number of
1056 : : elements of V0, V1, MASK is the same.
1057 : :
1058 : : In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
1059 : : called with two arguments. In this case implementation passes the
1060 : : first argument twice in order to share the same tree code. This fact
1061 : : could enable the mask-values being twice the vector length. This is
1062 : : an implementation accident and this semantics is not guaranteed to
1063 : : the user. */
1064 : : tree
1065 : 474290 : c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
1066 : : bool complain)
1067 : : {
1068 : 474290 : tree ret;
1069 : 474290 : bool wrap = true;
1070 : 474290 : bool maybe_const = false;
1071 : 474290 : bool two_arguments = false;
1072 : :
1073 : 474290 : if (v1 == NULL_TREE)
1074 : : {
1075 : 432229 : two_arguments = true;
1076 : 432229 : v1 = v0;
1077 : : }
1078 : :
1079 : 474290 : if (v0 == error_mark_node || v1 == error_mark_node
1080 : 474290 : || mask == error_mark_node)
1081 : : return error_mark_node;
1082 : :
1083 : 474290 : if (!gnu_vector_type_p (TREE_TYPE (mask))
1084 : 474290 : || !VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
1085 : : {
1086 : 3 : if (complain)
1087 : 0 : error_at (loc, "%<__builtin_shuffle%> last argument must "
1088 : : "be an integer vector");
1089 : 3 : return error_mark_node;
1090 : : }
1091 : :
1092 : 474287 : if (!gnu_vector_type_p (TREE_TYPE (v0))
1093 : 474287 : || !gnu_vector_type_p (TREE_TYPE (v1)))
1094 : : {
1095 : 0 : if (complain)
1096 : 0 : error_at (loc, "%<__builtin_shuffle%> arguments must be vectors");
1097 : 0 : return error_mark_node;
1098 : : }
1099 : :
1100 : 474287 : if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
1101 : : {
1102 : 0 : if (complain)
1103 : 0 : error_at (loc, "%<__builtin_shuffle%> argument vectors must be of "
1104 : : "the same type");
1105 : 0 : return error_mark_node;
1106 : : }
1107 : :
1108 : 474287 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)),
1109 : 474287 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
1110 : 474287 : && maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)),
1111 : 0 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))))
1112 : : {
1113 : 0 : if (complain)
1114 : 0 : error_at (loc, "%<__builtin_shuffle%> number of elements of the "
1115 : : "argument vector(s) and the mask vector should "
1116 : : "be the same");
1117 : 0 : return error_mark_node;
1118 : : }
1119 : :
1120 : 474287 : if (GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
1121 : 948574 : != GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
1122 : : {
1123 : 0 : if (complain)
1124 : 0 : error_at (loc, "%<__builtin_shuffle%> argument vector(s) inner type "
1125 : : "must have the same size as inner type of the mask");
1126 : 0 : return error_mark_node;
1127 : : }
1128 : :
1129 : 474287 : if (!c_dialect_cxx ())
1130 : : {
1131 : : /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
1132 : 459654 : v0 = c_fully_fold (v0, false, &maybe_const);
1133 : 459654 : wrap &= maybe_const;
1134 : :
1135 : 459654 : if (two_arguments)
1136 : 418400 : v1 = v0 = save_expr (v0);
1137 : : else
1138 : : {
1139 : 41254 : v1 = c_fully_fold (v1, false, &maybe_const);
1140 : 41254 : wrap &= maybe_const;
1141 : : }
1142 : :
1143 : 459654 : mask = c_fully_fold (mask, false, &maybe_const);
1144 : 459654 : wrap &= maybe_const;
1145 : : }
1146 : 14633 : else if (two_arguments)
1147 : 13826 : v1 = v0 = save_expr (v0);
1148 : :
1149 : 474287 : ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
1150 : :
1151 : 474287 : if (!c_dialect_cxx () && !wrap)
1152 : 459654 : ret = c_wrap_maybe_const (ret, true);
1153 : :
1154 : : return ret;
1155 : : }
1156 : :
1157 : : /* Build a VEC_PERM_EXPR if V0, V1 are not error_mark_nodes
1158 : : and have vector types, V0 has the same element type as V1, and the
1159 : : number of elements the result is that of MASK. */
1160 : : tree
1161 : 1086209 : c_build_shufflevector (location_t loc, tree v0, tree v1,
1162 : : const vec<tree> &mask, bool complain)
1163 : : {
1164 : 1086209 : tree ret;
1165 : 1086209 : bool wrap = true;
1166 : 1086209 : bool maybe_const = false;
1167 : :
1168 : 1086209 : if (v0 == error_mark_node || v1 == error_mark_node)
1169 : : return error_mark_node;
1170 : :
1171 : 1086209 : if (!gnu_vector_type_p (TREE_TYPE (v0))
1172 : 1086209 : || !gnu_vector_type_p (TREE_TYPE (v1)))
1173 : : {
1174 : 4 : if (complain)
1175 : 4 : error_at (loc, "%<__builtin_shufflevector%> arguments must be vectors");
1176 : 4 : return error_mark_node;
1177 : : }
1178 : :
1179 : : /* ??? In principle one could select a constant part of a variable size
1180 : : vector but things get a bit awkward with trying to support this here. */
1181 : 1086205 : unsigned HOST_WIDE_INT v0n, v1n;
1182 : 1086205 : if (!TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)).is_constant (&v0n)
1183 : 1086205 : || !TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)).is_constant (&v1n))
1184 : : {
1185 : : if (complain)
1186 : : error_at (loc, "%<__builtin_shufflevector%> arguments must be constant"
1187 : : " size vectors");
1188 : : return error_mark_node;
1189 : : }
1190 : :
1191 : 1086205 : if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (v0)))
1192 : 1086205 : != TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (v1))))
1193 : : {
1194 : 4 : if (complain)
1195 : 4 : error_at (loc, "%<__builtin_shufflevector%> argument vectors must "
1196 : : "have the same element type");
1197 : 4 : return error_mark_node;
1198 : : }
1199 : :
1200 : 1086201 : if (!pow2p_hwi (mask.length ()))
1201 : : {
1202 : 4 : if (complain)
1203 : 4 : error_at (loc, "%<__builtin_shufflevector%> must specify a result "
1204 : : "with a power of two number of elements");
1205 : 4 : return error_mark_node;
1206 : : }
1207 : :
1208 : 1086197 : if (!c_dialect_cxx ())
1209 : : {
1210 : : /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
1211 : 1051997 : v0 = c_fully_fold (v0, false, &maybe_const);
1212 : 1051997 : wrap &= maybe_const;
1213 : :
1214 : 1051997 : v1 = c_fully_fold (v1, false, &maybe_const);
1215 : 1051997 : wrap &= maybe_const;
1216 : : }
1217 : :
1218 : 2172439 : unsigned HOST_WIDE_INT maskl = MAX (mask.length (), MAX (v0n, v1n));
1219 : 1086197 : unsigned HOST_WIDE_INT pad = (v0n < maskl ? maskl - v0n : 0);
1220 : 1086197 : vec_perm_builder sel (maskl, maskl, 1);
1221 : 1086197 : unsigned i;
1222 : 29481178 : for (i = 0; i < mask.length (); ++i)
1223 : : {
1224 : 13654407 : tree idx = mask[i];
1225 : 13654407 : if (!tree_fits_shwi_p (idx))
1226 : : {
1227 : 4 : if (complain)
1228 : 4 : error_at (loc, "invalid element index %qE to "
1229 : : "%<__builtin_shufflevector%>", idx);
1230 : 1086197 : return error_mark_node;
1231 : : }
1232 : 13654403 : HOST_WIDE_INT iidx = tree_to_shwi (idx);
1233 : 13654403 : if (iidx < -1
1234 : 13654399 : || (iidx != -1
1235 : 13654319 : && (unsigned HOST_WIDE_INT) iidx >= v0n + v1n))
1236 : : {
1237 : 11 : if (complain)
1238 : 11 : error_at (loc, "invalid element index %qE to "
1239 : : "%<__builtin_shufflevector%>", idx);
1240 : 11 : return error_mark_node;
1241 : : }
1242 : : /* ??? Our VEC_PERM_EXPR does not allow for -1 yet. */
1243 : 13654392 : if (iidx == -1)
1244 : 80 : iidx = i;
1245 : : /* ??? Our VEC_PERM_EXPR does not allow different sized inputs,
1246 : : so pad out a smaller v0. */
1247 : 13654312 : else if ((unsigned HOST_WIDE_INT) iidx >= v0n)
1248 : 1159 : iidx += pad;
1249 : 13654392 : sel.quick_push (iidx);
1250 : : }
1251 : : /* ??? VEC_PERM_EXPR does not support a result that is smaller than
1252 : : the inputs, so we have to pad id out. */
1253 : 1087310 : for (; i < maskl; ++i)
1254 : 1128 : sel.quick_push (i);
1255 : :
1256 : 1086182 : vec_perm_indices indices (sel, 2, maskl);
1257 : :
1258 : 1086182 : tree ret_type = build_vector_type (TREE_TYPE (TREE_TYPE (v0)), maskl);
1259 : 2172364 : tree mask_type = build_vector_type (build_nonstandard_integer_type
1260 : 1086182 : (TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (ret_type))), 1),
1261 : 1086182 : maskl);
1262 : : /* Pad out arguments to the common vector size. */
1263 : 1086182 : if (v0n < maskl)
1264 : : {
1265 : 125 : constructor_elt elt = { NULL_TREE, build_zero_cst (TREE_TYPE (v0)) };
1266 : 125 : v0 = build_constructor_single (ret_type, NULL_TREE, v0);
1267 : 571 : for (i = 1; i < maskl / v0n; ++i)
1268 : 321 : vec_safe_push (CONSTRUCTOR_ELTS (v0), elt);
1269 : : }
1270 : 1086182 : if (v1n < maskl)
1271 : : {
1272 : 95 : constructor_elt elt = { NULL_TREE, build_zero_cst (TREE_TYPE (v1)) };
1273 : 95 : v1 = build_constructor_single (ret_type, NULL_TREE, v1);
1274 : 351 : for (i = 1; i < maskl / v1n; ++i)
1275 : 161 : vec_safe_push (CONSTRUCTOR_ELTS (v1), elt);
1276 : : }
1277 : 1086182 : ret = build3_loc (loc, VEC_PERM_EXPR, ret_type, v0, v1,
1278 : : vec_perm_indices_to_tree (mask_type, indices));
1279 : : /* Get the lowpart we are interested in. */
1280 : 2172364 : if (mask.length () < maskl)
1281 : : {
1282 : 159 : tree lpartt = build_vector_type (TREE_TYPE (ret_type), mask.length ());
1283 : 159 : ret = build3_loc (loc, BIT_FIELD_REF,
1284 : 159 : lpartt, ret, TYPE_SIZE (lpartt), bitsize_zero_node);
1285 : : /* Wrap the lowpart operation in a TARGET_EXPR so it gets a separate
1286 : : temporary during gimplification. See PR101530 for cases where
1287 : : we'd otherwise end up with non-toplevel BIT_FIELD_REFs. */
1288 : 159 : tree tem = create_tmp_var_raw (lpartt);
1289 : 159 : DECL_CONTEXT (tem) = current_function_decl;
1290 : 159 : ret = build4 (TARGET_EXPR, lpartt, tem, ret, NULL_TREE, NULL_TREE);
1291 : 159 : TREE_SIDE_EFFECTS (ret) = 1;
1292 : : }
1293 : :
1294 : 1086182 : if (!c_dialect_cxx () && !wrap)
1295 : 1051994 : ret = c_wrap_maybe_const (ret, true);
1296 : :
1297 : 1086182 : return ret;
1298 : 2172379 : }
1299 : :
1300 : : /* Build a VEC_CONVERT ifn for __builtin_convertvector builtin. */
1301 : :
1302 : : tree
1303 : 490 : c_build_vec_convert (location_t loc1, tree expr, location_t loc2, tree type,
1304 : : bool complain)
1305 : : {
1306 : 490 : if (error_operand_p (type))
1307 : 0 : return error_mark_node;
1308 : 490 : if (error_operand_p (expr))
1309 : 0 : return error_mark_node;
1310 : :
1311 : 490 : if (!gnu_vector_type_p (TREE_TYPE (expr))
1312 : 490 : || (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (expr))
1313 : 166 : && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (expr))))
1314 : : {
1315 : 4 : if (complain)
1316 : 4 : error_at (loc1, "%<__builtin_convertvector%> first argument must "
1317 : : "be an integer or floating vector");
1318 : 4 : return error_mark_node;
1319 : : }
1320 : :
1321 : 486 : if (!gnu_vector_type_p (type)
1322 : 486 : || (!VECTOR_INTEGER_TYPE_P (type) && !VECTOR_FLOAT_TYPE_P (type)))
1323 : : {
1324 : 4 : if (complain)
1325 : 4 : error_at (loc2, "%<__builtin_convertvector%> second argument must "
1326 : : "be an integer or floating vector type");
1327 : 4 : return error_mark_node;
1328 : : }
1329 : :
1330 : 482 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr)),
1331 : 964 : TYPE_VECTOR_SUBPARTS (type)))
1332 : : {
1333 : 8 : if (complain)
1334 : 8 : error_at (loc1, "%<__builtin_convertvector%> number of elements "
1335 : : "of the first argument vector and the second argument "
1336 : : "vector type should be the same");
1337 : 8 : return error_mark_node;
1338 : : }
1339 : :
1340 : 474 : if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (expr)))
1341 : 474 : == TYPE_MAIN_VARIANT (TREE_TYPE (type)))
1342 : 474 : || (VECTOR_INTEGER_TYPE_P (TREE_TYPE (expr))
1343 : 303 : && VECTOR_INTEGER_TYPE_P (type)
1344 : 155 : && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (expr)))
1345 : 155 : == TYPE_PRECISION (TREE_TYPE (type)))))
1346 : 29 : return build1_loc (loc1, VIEW_CONVERT_EXPR, type, expr);
1347 : :
1348 : 445 : bool wrap = true;
1349 : 445 : bool maybe_const = false;
1350 : 445 : tree ret;
1351 : 445 : if (!c_dialect_cxx ())
1352 : : {
1353 : : /* Avoid C_MAYBE_CONST_EXPRs inside of VEC_CONVERT argument. */
1354 : 310 : expr = c_fully_fold (expr, false, &maybe_const);
1355 : 310 : wrap &= maybe_const;
1356 : : }
1357 : :
1358 : 445 : ret = build_call_expr_internal_loc (loc1, IFN_VEC_CONVERT, type, 1, expr);
1359 : :
1360 : 445 : if (!wrap)
1361 : 310 : ret = c_wrap_maybe_const (ret, true);
1362 : :
1363 : : return ret;
1364 : : }
1365 : :
1366 : : /* Like tree.cc:get_narrower, but retain conversion from C++0x scoped enum
1367 : : to integral type. */
1368 : :
1369 : : tree
1370 : 62932951 : c_common_get_narrower (tree op, int *unsignedp_ptr)
1371 : : {
1372 : 62932951 : op = get_narrower (op, unsignedp_ptr);
1373 : :
1374 : 62932951 : if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
1375 : 62932951 : && ENUM_IS_SCOPED (TREE_TYPE (op)))
1376 : : {
1377 : : /* C++0x scoped enumerations don't implicitly convert to integral
1378 : : type; if we stripped an explicit conversion to a larger type we
1379 : : need to replace it so common_type will still work. */
1380 : 2498520 : tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
1381 : 2498520 : TYPE_UNSIGNED (TREE_TYPE (op)));
1382 : 2498520 : op = fold_convert (type, op);
1383 : : }
1384 : 62932951 : return op;
1385 : : }
1386 : :
1387 : : /* This is a helper function of build_binary_op.
1388 : :
1389 : : For certain operations if both args were extended from the same
1390 : : smaller type, do the arithmetic in that type and then extend.
1391 : :
1392 : : BITWISE indicates a bitwise operation.
1393 : : For them, this optimization is safe only if
1394 : : both args are zero-extended or both are sign-extended.
1395 : : Otherwise, we might change the result.
1396 : : Eg, (short)-1 | (unsigned short)-1 is (int)-1
1397 : : but calculated in (unsigned short) it would be (unsigned short)-1.
1398 : : */
1399 : : tree
1400 : 5051484 : shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
1401 : : {
1402 : 5051484 : int unsigned0, unsigned1;
1403 : 5051484 : tree arg0, arg1;
1404 : 5051484 : int uns;
1405 : 5051484 : tree type;
1406 : :
1407 : : /* Do not shorten vector operations. */
1408 : 5051484 : if (VECTOR_TYPE_P (result_type))
1409 : : return result_type;
1410 : :
1411 : : /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
1412 : : excessive narrowing when we call get_narrower below. For
1413 : : example, suppose that OP0 is of unsigned int extended
1414 : : from signed char and that RESULT_TYPE is long long int.
1415 : : If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
1416 : : like
1417 : :
1418 : : (long long int) (unsigned int) signed_char
1419 : :
1420 : : which get_narrower would narrow down to
1421 : :
1422 : : (unsigned int) signed char
1423 : :
1424 : : If we do not cast OP0 first, get_narrower would return
1425 : : signed_char, which is inconsistent with the case of the
1426 : : explicit cast. */
1427 : 5033990 : op0 = convert (result_type, op0);
1428 : 5033990 : op1 = convert (result_type, op1);
1429 : :
1430 : 5033990 : arg0 = c_common_get_narrower (op0, &unsigned0);
1431 : 5033990 : arg1 = c_common_get_narrower (op1, &unsigned1);
1432 : :
1433 : : /* UNS is 1 if the operation to be done is an unsigned one. */
1434 : 5033990 : uns = TYPE_UNSIGNED (result_type);
1435 : :
1436 : : /* Handle the case that OP0 (or OP1) does not *contain* a conversion
1437 : : but it *requires* conversion to FINAL_TYPE. */
1438 : :
1439 : 5033990 : if ((TYPE_PRECISION (TREE_TYPE (op0))
1440 : 5033990 : == TYPE_PRECISION (TREE_TYPE (arg0)))
1441 : 5033990 : && TREE_TYPE (op0) != result_type)
1442 : 2309 : unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1443 : 5033990 : if ((TYPE_PRECISION (TREE_TYPE (op1))
1444 : 5033990 : == TYPE_PRECISION (TREE_TYPE (arg1)))
1445 : 5033990 : && TREE_TYPE (op1) != result_type)
1446 : 226536 : unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1447 : :
1448 : : /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
1449 : :
1450 : : /* For bitwise operations, signedness of nominal type
1451 : : does not matter. Consider only how operands were extended. */
1452 : 5033990 : if (bitwise)
1453 : 2448499 : uns = unsigned0;
1454 : :
1455 : : /* Note that in all three cases below we refrain from optimizing
1456 : : an unsigned operation on sign-extended args.
1457 : : That would not be valid. */
1458 : :
1459 : : /* Both args variable: if both extended in same way
1460 : : from same width, do it in that width.
1461 : : Do it unsigned if args were zero-extended. */
1462 : 5033990 : if ((TYPE_PRECISION (TREE_TYPE (arg0))
1463 : 5033990 : < TYPE_PRECISION (result_type))
1464 : 229672 : && (TYPE_PRECISION (TREE_TYPE (arg1))
1465 : 229672 : == TYPE_PRECISION (TREE_TYPE (arg0)))
1466 : 142483 : && unsigned0 == unsigned1
1467 : 5176287 : && (unsigned0 || !uns))
1468 : : {
1469 : 142279 : tree ctype = common_type (TREE_TYPE (arg0), TREE_TYPE (arg1));
1470 : 142279 : if (ctype != error_mark_node)
1471 : 142279 : return c_common_signed_or_unsigned_type (unsigned0, ctype);
1472 : : }
1473 : :
1474 : 4891711 : else if (TREE_CODE (arg0) == INTEGER_CST
1475 : 1722181 : && (unsigned1 || !uns)
1476 : 1466457 : && (TYPE_PRECISION (TREE_TYPE (arg1))
1477 : 1466457 : < TYPE_PRECISION (result_type))
1478 : 280 : && (type
1479 : 280 : = c_common_signed_or_unsigned_type (unsigned1,
1480 : 280 : TREE_TYPE (arg1)))
1481 : 280 : && !POINTER_TYPE_P (type)
1482 : 4891991 : && int_fits_type_p (arg0, type))
1483 : : return type;
1484 : :
1485 : 4891481 : else if (TREE_CODE (arg1) == INTEGER_CST
1486 : 3170269 : && (unsigned0 || !uns)
1487 : 2413452 : && (TYPE_PRECISION (TREE_TYPE (arg0))
1488 : 2413452 : < TYPE_PRECISION (result_type))
1489 : 66352 : && (type
1490 : 66352 : = c_common_signed_or_unsigned_type (unsigned0,
1491 : 66352 : TREE_TYPE (arg0)))
1492 : 66352 : && !POINTER_TYPE_P (type)
1493 : 4957833 : && int_fits_type_p (arg1, type))
1494 : : return type;
1495 : :
1496 : : return result_type;
1497 : : }
1498 : :
1499 : : /* Returns true iff any integer value of type FROM_TYPE can be represented as
1500 : : real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
1501 : :
1502 : : static bool
1503 : 101 : int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
1504 : : {
1505 : 101 : tree type_low_bound = TYPE_MIN_VALUE (from_type);
1506 : 101 : tree type_high_bound = TYPE_MAX_VALUE (from_type);
1507 : 101 : REAL_VALUE_TYPE real_low_bound =
1508 : 101 : real_value_from_int_cst (0, type_low_bound);
1509 : 101 : REAL_VALUE_TYPE real_high_bound =
1510 : 101 : real_value_from_int_cst (0, type_high_bound);
1511 : :
1512 : 101 : return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
1513 : 101 : && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
1514 : : }
1515 : :
1516 : : /* Checks if expression EXPR of complex/real/integer type cannot be converted
1517 : : to the complex/real/integer type TYPE. Function returns non-zero when:
1518 : : * EXPR is a constant which cannot be exactly converted to TYPE.
1519 : : * EXPR is not a constant and size of EXPR's type > than size of TYPE,
1520 : : for EXPR type and TYPE being both integers or both real, or both
1521 : : complex.
1522 : : * EXPR is not a constant of complex type and TYPE is a real or
1523 : : an integer.
1524 : : * EXPR is not a constant of real type and TYPE is an integer.
1525 : : * EXPR is not a constant of integer type which cannot be
1526 : : exactly converted to real type.
1527 : :
1528 : : Function allows conversions between types of different signedness if
1529 : : CHECK_SIGN is false and can return SAFE_CONVERSION (zero) in that
1530 : : case. Function can return UNSAFE_SIGN if CHECK_SIGN is true.
1531 : :
1532 : : RESULT, when non-null is the result of the conversion. When constant
1533 : : it is included in the text of diagnostics.
1534 : :
1535 : : Function allows conversions from complex constants to non-complex types,
1536 : : provided that imaginary part is zero and real part can be safely converted
1537 : : to TYPE. */
1538 : :
1539 : : enum conversion_safety
1540 : 9373 : unsafe_conversion_p (tree type, tree expr, tree result, bool check_sign)
1541 : : {
1542 : 9373 : enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
1543 : 9373 : tree expr_type = TREE_TYPE (expr);
1544 : :
1545 : 9373 : expr = fold_for_warn (expr);
1546 : :
1547 : 9373 : if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
1548 : : {
1549 : : /* If type is complex, we are interested in compatibility with
1550 : : underlying type. */
1551 : 5380 : if (TREE_CODE (type) == COMPLEX_TYPE)
1552 : 114 : type = TREE_TYPE (type);
1553 : :
1554 : : /* Warn for real constant that is not an exact integer converted
1555 : : to integer type. */
1556 : 5380 : if (SCALAR_FLOAT_TYPE_P (expr_type)
1557 : 658 : && (TREE_CODE (type) == INTEGER_TYPE
1558 : 658 : || TREE_CODE (type) == BITINT_TYPE))
1559 : : {
1560 : 283 : if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
1561 : 275 : give_warning = UNSAFE_REAL;
1562 : : }
1563 : : /* Warn for an integer constant that does not fit into integer type. */
1564 : 5097 : else if ((TREE_CODE (expr_type) == INTEGER_TYPE
1565 : 5097 : || TREE_CODE (expr_type) == BITINT_TYPE)
1566 : 4712 : && (TREE_CODE (type) == INTEGER_TYPE
1567 : 4712 : || TREE_CODE (type) == BITINT_TYPE)
1568 : 3988 : && !int_fits_type_p (expr, type))
1569 : : {
1570 : 995 : if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
1571 : 961 : && tree_int_cst_sgn (expr) < 0)
1572 : : {
1573 : 264 : if (check_sign)
1574 : 1834 : give_warning = UNSAFE_SIGN;
1575 : : }
1576 : 432 : else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
1577 : : {
1578 : 335 : if (check_sign)
1579 : 1834 : give_warning = UNSAFE_SIGN;
1580 : : }
1581 : : else
1582 : : give_warning = UNSAFE_OTHER;
1583 : : }
1584 : 4401 : else if (SCALAR_FLOAT_TYPE_P (type))
1585 : : {
1586 : : /* Warn for an integer constant that does not fit into real type. */
1587 : 1009 : if (TREE_CODE (expr_type) == INTEGER_TYPE
1588 : : || TREE_CODE (expr_type) == BITINT_TYPE)
1589 : : {
1590 : 634 : REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
1591 : 634 : if (!exact_real_truncate (TYPE_MODE (type), &a))
1592 : 53 : give_warning = UNSAFE_REAL;
1593 : : }
1594 : : /* Warn for a real constant that does not fit into a smaller
1595 : : real type. */
1596 : 375 : else if (SCALAR_FLOAT_TYPE_P (expr_type)
1597 : 375 : && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1598 : : {
1599 : 227 : REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
1600 : 227 : if (!exact_real_truncate (TYPE_MODE (type), &a))
1601 : 65 : give_warning = UNSAFE_REAL;
1602 : : }
1603 : : }
1604 : : }
1605 : :
1606 : 3993 : else if (TREE_CODE (expr) == COMPLEX_CST)
1607 : : {
1608 : 69 : tree imag_part = TREE_IMAGPART (expr);
1609 : : /* Conversion from complex constant with zero imaginary part,
1610 : : perform check for conversion of real part. */
1611 : 69 : if ((TREE_CODE (imag_part) == REAL_CST
1612 : 29 : && real_zerop (imag_part))
1613 : 80 : || (TREE_CODE (imag_part) == INTEGER_CST
1614 : 40 : && integer_zerop (imag_part)))
1615 : : /* Note: in this branch we use recursive call to unsafe_conversion_p
1616 : : with different type of EXPR, but it is still safe, because when EXPR
1617 : : is a constant, it's type is not used in text of generated warnings
1618 : : (otherwise they could sound misleading). */
1619 : 30 : return unsafe_conversion_p (type, TREE_REALPART (expr), result,
1620 : 30 : check_sign);
1621 : : /* Conversion from complex constant with non-zero imaginary part. */
1622 : : else
1623 : : {
1624 : : /* Conversion to complex type.
1625 : : Perform checks for both real and imaginary parts. */
1626 : 39 : if (TREE_CODE (type) == COMPLEX_TYPE)
1627 : : {
1628 : 33 : enum conversion_safety re_safety =
1629 : 33 : unsafe_conversion_p (type, TREE_REALPART (expr),
1630 : : result, check_sign);
1631 : 33 : enum conversion_safety im_safety =
1632 : 33 : unsafe_conversion_p (type, imag_part, result, check_sign);
1633 : :
1634 : : /* Merge the results into appropriate single warning. */
1635 : :
1636 : : /* Note: this case includes SAFE_CONVERSION, i.e. success. */
1637 : 33 : if (re_safety == im_safety)
1638 : : give_warning = re_safety;
1639 : 9 : else if (!re_safety && im_safety)
1640 : : give_warning = im_safety;
1641 : 4 : else if (re_safety && !im_safety)
1642 : : give_warning = re_safety;
1643 : : else
1644 : 670 : give_warning = UNSAFE_OTHER;
1645 : : }
1646 : : /* Warn about conversion from complex to real or integer type. */
1647 : : else
1648 : : give_warning = UNSAFE_IMAGINARY;
1649 : : }
1650 : : }
1651 : :
1652 : : /* Checks for remaining case: EXPR is not constant. */
1653 : : else
1654 : : {
1655 : : /* Warn for real types converted to integer types. */
1656 : 3924 : if (SCALAR_FLOAT_TYPE_P (expr_type)
1657 : 213 : && (TREE_CODE (type) == INTEGER_TYPE
1658 : 213 : || TREE_CODE (type) == BITINT_TYPE))
1659 : : give_warning = UNSAFE_REAL;
1660 : :
1661 : 3895 : else if ((TREE_CODE (expr_type) == INTEGER_TYPE
1662 : 3895 : || TREE_CODE (expr_type) == BITINT_TYPE)
1663 : 3500 : && (TREE_CODE (type) == INTEGER_TYPE
1664 : 3500 : || TREE_CODE (type) == BITINT_TYPE))
1665 : : {
1666 : : /* Don't warn about unsigned char y = 0xff, x = (int) y; */
1667 : 3279 : expr = get_unwidened (expr, 0);
1668 : 3279 : expr_type = TREE_TYPE (expr);
1669 : :
1670 : : /* Don't warn for short y; short x = ((int)y & 0xff); */
1671 : 3279 : if (TREE_CODE (expr) == BIT_AND_EXPR
1672 : 3265 : || TREE_CODE (expr) == BIT_IOR_EXPR
1673 : 3224 : || TREE_CODE (expr) == BIT_XOR_EXPR)
1674 : : {
1675 : : /* If both args were extended from a shortest type,
1676 : : use that type if that is safe. */
1677 : 59 : expr_type = shorten_binary_op (expr_type,
1678 : 59 : TREE_OPERAND (expr, 0),
1679 : 59 : TREE_OPERAND (expr, 1),
1680 : : /* bitwise */1);
1681 : :
1682 : 59 : if (TREE_CODE (expr) == BIT_AND_EXPR)
1683 : : {
1684 : 14 : tree op0 = TREE_OPERAND (expr, 0);
1685 : 14 : tree op1 = TREE_OPERAND (expr, 1);
1686 : 14 : bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
1687 : 14 : bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
1688 : :
1689 : : /* If one of the operands is a non-negative constant
1690 : : that fits in the target type, then the type of the
1691 : : other operand does not matter. */
1692 : 14 : if ((TREE_CODE (op0) == INTEGER_CST
1693 : 0 : && int_fits_type_p (op0, c_common_signed_type (type))
1694 : 0 : && int_fits_type_p (op0, c_common_unsigned_type (type)))
1695 : 14 : || (TREE_CODE (op1) == INTEGER_CST
1696 : 1 : && int_fits_type_p (op1, c_common_signed_type (type))
1697 : 1 : && int_fits_type_p (op1,
1698 : 1 : c_common_unsigned_type (type))))
1699 : 1 : return SAFE_CONVERSION;
1700 : : /* If constant is unsigned and fits in the target
1701 : : type, then the result will also fit. */
1702 : 13 : else if ((TREE_CODE (op0) == INTEGER_CST
1703 : 0 : && unsigned0
1704 : 0 : && int_fits_type_p (op0, type))
1705 : 13 : || (TREE_CODE (op1) == INTEGER_CST
1706 : 0 : && unsigned1
1707 : 0 : && int_fits_type_p (op1, type)))
1708 : : return SAFE_CONVERSION;
1709 : : }
1710 : : }
1711 : : /* Warn for integer types converted to smaller integer types. */
1712 : 3278 : if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1713 : : give_warning = UNSAFE_OTHER;
1714 : :
1715 : : /* When they are the same width but different signedness,
1716 : : then the value may change. */
1717 : 2742 : else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
1718 : 2213 : && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
1719 : : /* Even when converted to a bigger type, if the type is
1720 : : unsigned but expr is signed, then negative values
1721 : : will be changed. */
1722 : 1481 : || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
1723 : 4054 : && check_sign)
1724 : : give_warning = UNSAFE_SIGN;
1725 : : }
1726 : :
1727 : : /* Warn for integer types converted to real types if and only if
1728 : : all the range of values of the integer type cannot be
1729 : : represented by the real type. */
1730 : 616 : else if ((TREE_CODE (expr_type) == INTEGER_TYPE
1731 : : || TREE_CODE (expr_type) == BITINT_TYPE)
1732 : 221 : && SCALAR_FLOAT_TYPE_P (type))
1733 : : {
1734 : : /* Don't warn about char y = 0xff; float x = (int) y; */
1735 : 101 : expr = get_unwidened (expr, 0);
1736 : 101 : expr_type = TREE_TYPE (expr);
1737 : :
1738 : 101 : if (!int_safely_convertible_to_real_p (expr_type, type))
1739 : 670 : give_warning = UNSAFE_OTHER;
1740 : : }
1741 : :
1742 : : /* Warn for real types converted to smaller real types. */
1743 : 515 : else if (SCALAR_FLOAT_TYPE_P (expr_type)
1744 : 184 : && SCALAR_FLOAT_TYPE_P (type)
1745 : 699 : && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
1746 : : give_warning = UNSAFE_REAL;
1747 : :
1748 : : /* Check conversion between two complex types. */
1749 : 463 : else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1750 : 32 : && TREE_CODE (type) == COMPLEX_TYPE)
1751 : : {
1752 : : /* Extract underlying types (i.e., type of real and imaginary
1753 : : parts) of expr_type and type. */
1754 : 10 : tree from_type = TREE_TYPE (expr_type);
1755 : 10 : tree to_type = TREE_TYPE (type);
1756 : :
1757 : : /* Warn for real types converted to integer types. */
1758 : 10 : if (SCALAR_FLOAT_TYPE_P (from_type)
1759 : 8 : && TREE_CODE (to_type) == INTEGER_TYPE)
1760 : : give_warning = UNSAFE_REAL;
1761 : :
1762 : : /* Warn for real types converted to smaller real types. */
1763 : 6 : else if (SCALAR_FLOAT_TYPE_P (from_type)
1764 : 4 : && SCALAR_FLOAT_TYPE_P (to_type)
1765 : 10 : && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1766 : : give_warning = UNSAFE_REAL;
1767 : :
1768 : : /* Check conversion for complex integer types. Here implementation
1769 : : is simpler than for real-domain integers because it does not
1770 : : involve sophisticated cases, such as bitmasks, casts, etc. */
1771 : 4 : else if (TREE_CODE (from_type) == INTEGER_TYPE
1772 : 2 : && TREE_CODE (to_type) == INTEGER_TYPE)
1773 : : {
1774 : : /* Warn for integer types converted to smaller integer types. */
1775 : 2 : if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
1776 : : give_warning = UNSAFE_OTHER;
1777 : :
1778 : : /* Check for different signedness, see case for real-domain
1779 : : integers (above) for a more detailed comment. */
1780 : 0 : else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
1781 : 0 : && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
1782 : 0 : || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
1783 : 0 : && check_sign)
1784 : : give_warning = UNSAFE_SIGN;
1785 : : }
1786 : 2 : else if (TREE_CODE (from_type) == INTEGER_TYPE
1787 : 0 : && SCALAR_FLOAT_TYPE_P (to_type)
1788 : 2 : && !int_safely_convertible_to_real_p (from_type, to_type))
1789 : : give_warning = UNSAFE_OTHER;
1790 : : }
1791 : :
1792 : : /* Warn for complex types converted to real or integer types. */
1793 : 453 : else if (TREE_CODE (expr_type) == COMPLEX_TYPE
1794 : 22 : && TREE_CODE (type) != COMPLEX_TYPE)
1795 : 28 : give_warning = UNSAFE_IMAGINARY;
1796 : : }
1797 : :
1798 : : return give_warning;
1799 : : }
1800 : :
1801 : :
1802 : : /* Convert EXPR to TYPE, warning about conversion problems with constants.
1803 : : Invoke this function on every expression that is converted implicitly,
1804 : : i.e. because of language rules and not because of an explicit cast.
1805 : : INIT_CONST is true if the conversion is for arithmetic types for a static
1806 : : initializer and folding must apply accordingly (discarding floating-point
1807 : : exceptions and assuming the default rounding mode is in effect). */
1808 : :
1809 : : tree
1810 : 12324496 : convert_and_check (location_t loc, tree type, tree expr, bool init_const)
1811 : : {
1812 : 12324496 : tree result;
1813 : 12324496 : tree expr_for_warning;
1814 : :
1815 : : /* Convert from a value with possible excess precision rather than
1816 : : via the semantic type, but do not warn about values not fitting
1817 : : exactly in the semantic type. */
1818 : 12324496 : if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
1819 : : {
1820 : 5963 : tree orig_type = TREE_TYPE (expr);
1821 : 5963 : expr = TREE_OPERAND (expr, 0);
1822 : 5963 : expr_for_warning = (init_const
1823 : 5963 : ? convert_init (orig_type, expr)
1824 : 5602 : : convert (orig_type, expr));
1825 : 5963 : if (orig_type == type)
1826 : : return expr_for_warning;
1827 : : }
1828 : : else
1829 : : expr_for_warning = expr;
1830 : :
1831 : 12319026 : if (TREE_TYPE (expr) == type)
1832 : : return expr;
1833 : :
1834 : 10907607 : result = init_const ? convert_init (type, expr) : convert (type, expr);
1835 : :
1836 : 10907607 : if (c_inhibit_evaluation_warnings == 0
1837 : 10581255 : && !TREE_OVERFLOW_P (expr)
1838 : 10581229 : && result != error_mark_node
1839 : 21488682 : && !c_hardbool_type_attr (type))
1840 : 10577435 : warnings_for_convert_and_check (loc, type, expr_for_warning, result);
1841 : :
1842 : : return result;
1843 : : }
1844 : :
1845 : : /* A node in a list that describes references to variables (EXPR), which are
1846 : : either read accesses if WRITER is zero, or write accesses, in which case
1847 : : WRITER is the parent of EXPR. */
1848 : : struct tlist
1849 : : {
1850 : : struct tlist *next;
1851 : : tree expr, writer;
1852 : : };
1853 : :
1854 : : /* Used to implement a cache the results of a call to verify_tree. We only
1855 : : use this for SAVE_EXPRs. */
1856 : : struct tlist_cache
1857 : : {
1858 : : struct tlist_cache *next;
1859 : : struct tlist *cache_before_sp;
1860 : : struct tlist *cache_after_sp;
1861 : : tree expr;
1862 : : };
1863 : :
1864 : : /* Obstack to use when allocating tlist structures, and corresponding
1865 : : firstobj. */
1866 : : static struct obstack tlist_obstack;
1867 : : static char *tlist_firstobj = 0;
1868 : :
1869 : : /* Keep track of the identifiers we've warned about, so we can avoid duplicate
1870 : : warnings. */
1871 : : static struct tlist *warned_ids;
1872 : : /* SAVE_EXPRs need special treatment. We process them only once and then
1873 : : cache the results. */
1874 : : static struct tlist_cache *save_expr_cache;
1875 : :
1876 : : static void add_tlist (struct tlist **, struct tlist *, tree, int);
1877 : : static void merge_tlist (struct tlist **, struct tlist *, int);
1878 : : static void verify_tree (tree, struct tlist **, struct tlist **, tree);
1879 : : static bool warning_candidate_p (tree);
1880 : : static bool candidate_equal_p (const_tree, const_tree);
1881 : : static void warn_for_collisions (struct tlist *);
1882 : : static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
1883 : : static struct tlist *new_tlist (struct tlist *, tree, tree);
1884 : :
1885 : : /* Create a new struct tlist and fill in its fields. */
1886 : : static struct tlist *
1887 : 34240607 : new_tlist (struct tlist *next, tree t, tree writer)
1888 : : {
1889 : 34240607 : struct tlist *l;
1890 : 34240607 : l = XOBNEW (&tlist_obstack, struct tlist);
1891 : 34240607 : l->next = next;
1892 : 34240607 : l->expr = t;
1893 : 34240607 : l->writer = writer;
1894 : 34240607 : return l;
1895 : : }
1896 : :
1897 : : /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1898 : : is nonnull, we ignore any node we find which has a writer equal to it. */
1899 : :
1900 : : static void
1901 : 60388157 : add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
1902 : : {
1903 : 131379399 : while (add)
1904 : : {
1905 : 70991242 : struct tlist *next = add->next;
1906 : 13178772 : if (!copy)
1907 : 1893666 : add->next = *to;
1908 : 13178772 : if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
1909 : 68922242 : *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1910 : : add = next;
1911 : : }
1912 : 11898553 : }
1913 : :
1914 : : /* Merge the nodes of ADD into TO. This merging process is done so that for
1915 : : each variable that already exists in TO, no new node is added; however if
1916 : : there is a write access recorded in ADD, and an occurrence on TO is only
1917 : : a read access, then the occurrence in TO will be modified to record the
1918 : : write. */
1919 : :
1920 : : static void
1921 : 42118968 : merge_tlist (struct tlist **to, struct tlist *add, int copy)
1922 : : {
1923 : 42118968 : struct tlist **end = to;
1924 : :
1925 : 78607542 : while (*end)
1926 : 36488574 : end = &(*end)->next;
1927 : :
1928 : 57885658 : while (add)
1929 : : {
1930 : 15766690 : int found = 0;
1931 : 15766690 : struct tlist *tmp2;
1932 : 15766690 : struct tlist *next = add->next;
1933 : :
1934 : 42970941 : for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1935 : 27204251 : if (candidate_equal_p (tmp2->expr, add->expr))
1936 : : {
1937 : 2687331 : found = 1;
1938 : 2687331 : if (!tmp2->writer)
1939 : 2681178 : tmp2->writer = add->writer;
1940 : : }
1941 : 15766690 : if (!found)
1942 : : {
1943 : 13092952 : *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
1944 : 13092952 : end = &(*end)->next;
1945 : 13092952 : *end = 0;
1946 : : }
1947 : : add = next;
1948 : : }
1949 : 42118968 : }
1950 : :
1951 : : /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1952 : : references in list LIST conflict with it, excluding reads if ONLY writers
1953 : : is nonzero. */
1954 : :
1955 : : static void
1956 : 5294912 : warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
1957 : : int only_writes)
1958 : : {
1959 : 5294912 : struct tlist *tmp;
1960 : :
1961 : : /* Avoid duplicate warnings. */
1962 : 5295075 : for (tmp = warned_ids; tmp; tmp = tmp->next)
1963 : 611 : if (candidate_equal_p (tmp->expr, written))
1964 : : return;
1965 : :
1966 : 18849162 : while (list)
1967 : : {
1968 : 13554698 : if (candidate_equal_p (list->expr, written)
1969 : 1561940 : && !candidate_equal_p (list->writer, writer)
1970 : 13693457 : && (!only_writes || list->writer))
1971 : : {
1972 : 562 : warned_ids = new_tlist (warned_ids, written, NULL_TREE);
1973 : 562 : warning_at (EXPR_LOC_OR_LOC (writer, input_location),
1974 : 562 : OPT_Wsequence_point, "operation on %qE may be undefined",
1975 : : list->expr);
1976 : : }
1977 : 13554698 : list = list->next;
1978 : : }
1979 : : }
1980 : :
1981 : : /* Given a list LIST of references to variables, find whether any of these
1982 : : can cause conflicts due to missing sequence points. */
1983 : :
1984 : : static void
1985 : 19889356 : warn_for_collisions (struct tlist *list)
1986 : : {
1987 : 19889356 : struct tlist *tmp;
1988 : :
1989 : 54726212 : for (tmp = list; tmp; tmp = tmp->next)
1990 : : {
1991 : 34836856 : if (tmp->writer)
1992 : 1361490 : warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1993 : : }
1994 : 19889356 : }
1995 : :
1996 : : /* Return nonzero if X is a tree that can be verified by the sequence point
1997 : : warnings. */
1998 : :
1999 : : static bool
2000 : 76078366 : warning_candidate_p (tree x)
2001 : : {
2002 : 76078366 : if (DECL_P (x) && DECL_ARTIFICIAL (x))
2003 : : return false;
2004 : :
2005 : 68454417 : if (TREE_CODE (x) == BLOCK)
2006 : : return false;
2007 : :
2008 : : /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.cc
2009 : : (lvalue_p) crash on TRY/CATCH. */
2010 : 68452286 : if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2011 : : return false;
2012 : :
2013 : 67543220 : if (!lvalue_p (x))
2014 : : return false;
2015 : :
2016 : : /* No point to track non-const calls, they will never satisfy
2017 : : operand_equal_p. */
2018 : 24313378 : if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2019 : : return false;
2020 : :
2021 : 24273743 : if (TREE_CODE (x) == STRING_CST)
2022 : : return false;
2023 : :
2024 : : return true;
2025 : : }
2026 : :
2027 : : /* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2028 : : static bool
2029 : 46162486 : candidate_equal_p (const_tree x, const_tree y)
2030 : : {
2031 : 46162486 : return (x == y) || (x && y && operand_equal_p (x, y, 0));
2032 : : }
2033 : :
2034 : : /* Walk the tree X, and record accesses to variables. If X is written by the
2035 : : parent tree, WRITER is the parent.
2036 : : We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2037 : : expression or its only operand forces a sequence point, then everything up
2038 : : to the sequence point is stored in PBEFORE_SP. Everything else gets stored
2039 : : in PNO_SP.
2040 : : Once we return, we will have emitted warnings if any subexpression before
2041 : : such a sequence point could be undefined. On a higher level, however, the
2042 : : sequence point may not be relevant, and we'll merge the two lists.
2043 : :
2044 : : Example: (b++, a) + b;
2045 : : The call that processes the COMPOUND_EXPR will store the increment of B
2046 : : in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
2047 : : processes the PLUS_EXPR will need to merge the two lists so that
2048 : : eventually, all accesses end up on the same list (and we'll warn about the
2049 : : unordered subexpressions b++ and b.
2050 : :
2051 : : A note on merging. If we modify the former example so that our expression
2052 : : becomes
2053 : : (b++, b) + a
2054 : : care must be taken not simply to add all three expressions into the final
2055 : : PNO_SP list. The function merge_tlist takes care of that by merging the
2056 : : before-SP list of the COMPOUND_EXPR into its after-SP list in a special
2057 : : way, so that no more than one access to B is recorded. */
2058 : :
2059 : : static void
2060 : 59783781 : verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
2061 : : tree writer)
2062 : : {
2063 : 73317617 : struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
2064 : 73317617 : enum tree_code code;
2065 : 73317617 : enum tree_code_class cl;
2066 : :
2067 : 73317617 : restart:
2068 : : /* X may be NULL if it is the operand of an empty statement expression
2069 : : ({ }). */
2070 : 73317617 : if (x == NULL)
2071 : : return;
2072 : :
2073 : 72144944 : code = TREE_CODE (x);
2074 : 72144944 : cl = TREE_CODE_CLASS (code);
2075 : :
2076 : 72144944 : if (warning_candidate_p (x))
2077 : 23011740 : *pno_sp = new_tlist (*pno_sp, x, writer);
2078 : :
2079 : 72144944 : switch (code)
2080 : : {
2081 : : case CONSTRUCTOR:
2082 : : case SIZEOF_EXPR:
2083 : : case PAREN_SIZEOF_EXPR:
2084 : : return;
2085 : :
2086 : 193691 : case COMPOUND_EXPR:
2087 : 193691 : case TRUTH_ANDIF_EXPR:
2088 : 193691 : case TRUTH_ORIF_EXPR:
2089 : 193691 : sequenced_binary:
2090 : 193691 : tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
2091 : 193691 : verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2092 : 193691 : warn_for_collisions (tmp_nosp);
2093 : 193691 : merge_tlist (pbefore_sp, tmp_before, 0);
2094 : 193691 : merge_tlist (pbefore_sp, tmp_nosp, 0);
2095 : 193691 : verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_list2, NULL_TREE);
2096 : 193691 : warn_for_collisions (tmp_list2);
2097 : 193691 : merge_tlist (pbefore_sp, tmp_list3, 0);
2098 : 193691 : merge_tlist (pno_sp, tmp_list2, 0);
2099 : 193691 : return;
2100 : :
2101 : 66407 : case COND_EXPR:
2102 : 66407 : tmp_before = tmp_list2 = 0;
2103 : 66407 : verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
2104 : 66407 : warn_for_collisions (tmp_list2);
2105 : 66407 : merge_tlist (pbefore_sp, tmp_before, 0);
2106 : 66407 : merge_tlist (pbefore_sp, tmp_list2, 0);
2107 : :
2108 : 66407 : tmp_list3 = tmp_nosp = 0;
2109 : 66407 : verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
2110 : 66407 : warn_for_collisions (tmp_nosp);
2111 : 66407 : merge_tlist (pbefore_sp, tmp_list3, 0);
2112 : :
2113 : 66407 : tmp_list3 = tmp_list2 = 0;
2114 : 66407 : verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
2115 : 66407 : warn_for_collisions (tmp_list2);
2116 : 66407 : merge_tlist (pbefore_sp, tmp_list3, 0);
2117 : : /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
2118 : : two first, to avoid warning for (a ? b++ : b++). */
2119 : 66407 : merge_tlist (&tmp_nosp, tmp_list2, 0);
2120 : 66407 : add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2121 : : return;
2122 : :
2123 : 266878 : case PREDECREMENT_EXPR:
2124 : 266878 : case PREINCREMENT_EXPR:
2125 : 266878 : case POSTDECREMENT_EXPR:
2126 : 266878 : case POSTINCREMENT_EXPR:
2127 : 266878 : verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
2128 : 266878 : return;
2129 : :
2130 : 3933422 : case MODIFY_EXPR:
2131 : 3933422 : tmp_before = tmp_nosp = tmp_list3 = 0;
2132 : 3933422 : verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
2133 : 3933422 : verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
2134 : : /* Expressions inside the LHS are not ordered wrt. the sequence points
2135 : : in the RHS. Example:
2136 : : *a = (a++, 2)
2137 : : Despite the fact that the modification of "a" is in the before_sp
2138 : : list (tmp_before), it conflicts with the use of "a" in the LHS.
2139 : : We can handle this by adding the contents of tmp_list3
2140 : : to those of tmp_before, and redoing the collision warnings for that
2141 : : list. */
2142 : 3933422 : add_tlist (&tmp_before, tmp_list3, x, 1);
2143 : 3933422 : warn_for_collisions (tmp_before);
2144 : : /* Exclude the LHS itself here; we first have to merge it into the
2145 : : tmp_nosp list. This is done to avoid warning for "a = a"; if we
2146 : : didn't exclude the LHS, we'd get it twice, once as a read and once
2147 : : as a write. */
2148 : 3933422 : add_tlist (pno_sp, tmp_list3, x, 0);
2149 : 3933422 : warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
2150 : :
2151 : 3933422 : merge_tlist (pbefore_sp, tmp_before, 0);
2152 : 3933422 : if (warning_candidate_p (TREE_OPERAND (x, 0)))
2153 : 971126 : merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
2154 : 3933422 : add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
2155 : 3933422 : return;
2156 : :
2157 : 6231097 : case CALL_EXPR:
2158 : : /* We need to warn about conflicts among arguments and conflicts between
2159 : : args and the function address. Side effects of the function address,
2160 : : however, are not ordered by the sequence point of the call. */
2161 : 6231097 : {
2162 : 6231097 : call_expr_arg_iterator iter;
2163 : 6231097 : tree arg;
2164 : 6231097 : tmp_before = tmp_nosp = 0;
2165 : 6231097 : verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
2166 : 27683446 : FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
2167 : : {
2168 : 15221252 : tmp_list2 = tmp_list3 = 0;
2169 : 15221252 : verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
2170 : 15221252 : merge_tlist (&tmp_list3, tmp_list2, 0);
2171 : 30442504 : add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
2172 : : }
2173 : 6231097 : add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
2174 : 6231097 : warn_for_collisions (tmp_before);
2175 : 6231097 : add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
2176 : 6231097 : return;
2177 : : }
2178 : :
2179 : : case TREE_LIST:
2180 : : /* Scan all the list, e.g. indices of multi dimensional array. */
2181 : 6 : while (x)
2182 : : {
2183 : 3 : tmp_before = tmp_nosp = 0;
2184 : 3 : verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
2185 : 3 : merge_tlist (&tmp_nosp, tmp_before, 0);
2186 : 3 : add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2187 : 3 : x = TREE_CHAIN (x);
2188 : : }
2189 : : return;
2190 : :
2191 : 98287 : case SAVE_EXPR:
2192 : 98287 : {
2193 : 98287 : struct tlist_cache *t;
2194 : 101985 : for (t = save_expr_cache; t; t = t->next)
2195 : 53654 : if (candidate_equal_p (t->expr, x))
2196 : : break;
2197 : :
2198 : 98287 : if (!t)
2199 : : {
2200 : 48331 : t = XOBNEW (&tlist_obstack, struct tlist_cache);
2201 : 48331 : t->next = save_expr_cache;
2202 : 48331 : t->expr = x;
2203 : 48331 : save_expr_cache = t;
2204 : :
2205 : 48331 : tmp_before = tmp_nosp = 0;
2206 : 48331 : verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
2207 : 48331 : warn_for_collisions (tmp_nosp);
2208 : :
2209 : 48331 : tmp_list3 = 0;
2210 : 48331 : merge_tlist (&tmp_list3, tmp_nosp, 0);
2211 : 48331 : t->cache_before_sp = tmp_before;
2212 : 48331 : t->cache_after_sp = tmp_list3;
2213 : : }
2214 : 98287 : merge_tlist (pbefore_sp, t->cache_before_sp, 1);
2215 : 98287 : add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
2216 : 98287 : return;
2217 : : }
2218 : :
2219 : 7063788 : case ADDR_EXPR:
2220 : 7063788 : x = TREE_OPERAND (x, 0);
2221 : 7063788 : if (DECL_P (x))
2222 : : return;
2223 : 492761 : writer = 0;
2224 : 492761 : goto restart;
2225 : :
2226 : 11974729 : case VIEW_CONVERT_EXPR:
2227 : 11974729 : if (location_wrapper_p (x))
2228 : : {
2229 : 2947853 : x = TREE_OPERAND (x, 0);
2230 : 2947853 : goto restart;
2231 : : }
2232 : 9026876 : goto do_default;
2233 : :
2234 : 623668 : case LSHIFT_EXPR:
2235 : 623668 : case RSHIFT_EXPR:
2236 : 623668 : case ARRAY_REF:
2237 : 623668 : if (cxx_dialect >= cxx17)
2238 : 47432 : goto sequenced_binary;
2239 : 576236 : goto do_default;
2240 : :
2241 : 1275324 : case COMPONENT_REF:
2242 : : /* Treat as unary, the other operands aren't evaluated. */
2243 : 1275324 : x = TREE_OPERAND (x, 0);
2244 : 1275324 : writer = 0;
2245 : 1275324 : goto restart;
2246 : :
2247 : 49592929 : default:
2248 : 49592929 : do_default:
2249 : : /* For other expressions, simply recurse on their operands.
2250 : : Manual tail recursion for unary expressions.
2251 : : Other non-expressions need not be processed. */
2252 : 49592929 : if (cl == tcc_unary)
2253 : : {
2254 : 8551020 : x = TREE_OPERAND (x, 0);
2255 : 8551020 : writer = 0;
2256 : 8551020 : goto restart;
2257 : : }
2258 : 41041909 : else if (IS_EXPR_CODE_CLASS (cl))
2259 : : {
2260 : 14953400 : int lp;
2261 : 14953400 : int max = TREE_OPERAND_LENGTH (x);
2262 : 50646548 : for (lp = 0; lp < max; lp++)
2263 : : {
2264 : 20739748 : tmp_before = tmp_nosp = 0;
2265 : 20739748 : verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
2266 : 20739748 : merge_tlist (&tmp_nosp, tmp_before, 0);
2267 : 41479496 : add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2268 : : }
2269 : : }
2270 : : return;
2271 : : }
2272 : : }
2273 : :
2274 : : static constexpr size_t verify_sequence_points_limit = 1024;
2275 : :
2276 : : /* Called from verify_sequence_points via walk_tree. */
2277 : :
2278 : : static tree
2279 : 89301667 : verify_tree_lim_r (tree *tp, int *walk_subtrees, void *data)
2280 : : {
2281 : 89301667 : if (++*((size_t *) data) > verify_sequence_points_limit)
2282 : 57 : return integer_zero_node;
2283 : :
2284 : 89301610 : if (TYPE_P (*tp))
2285 : 3348 : *walk_subtrees = 0;
2286 : :
2287 : : return NULL_TREE;
2288 : : }
2289 : :
2290 : : /* Try to warn for undefined behavior in EXPR due to missing sequence
2291 : : points. */
2292 : :
2293 : : void
2294 : 9089960 : verify_sequence_points (tree expr)
2295 : : {
2296 : 9089960 : tlist *before_sp = nullptr, *after_sp = nullptr;
2297 : :
2298 : : /* verify_tree is highly recursive, and merge_tlist is O(n^2),
2299 : : so we return early if the expression is too big. */
2300 : 9089960 : size_t n = 0;
2301 : 9089960 : if (walk_tree (&expr, verify_tree_lim_r, &n, nullptr))
2302 : 57 : return;
2303 : :
2304 : 9089903 : warned_ids = nullptr;
2305 : 9089903 : save_expr_cache = nullptr;
2306 : 9089903 : if (!tlist_firstobj)
2307 : : {
2308 : 9876 : gcc_obstack_init (&tlist_obstack);
2309 : 9876 : tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
2310 : : }
2311 : :
2312 : 9089903 : verify_tree (expr, &before_sp, &after_sp, NULL_TREE);
2313 : 9089903 : warn_for_collisions (after_sp);
2314 : 9089903 : obstack_free (&tlist_obstack, tlist_firstobj);
2315 : : }
2316 : :
2317 : : /* Validate the expression after `case' and apply default promotions. */
2318 : :
2319 : : static tree
2320 : 2689062 : check_case_value (location_t loc, tree value)
2321 : : {
2322 : 2689062 : if (value == NULL_TREE)
2323 : : return value;
2324 : :
2325 : 4504337 : if (INTEGRAL_TYPE_P (TREE_TYPE (value))
2326 : 4504234 : && TREE_CODE (value) == INTEGER_CST)
2327 : : /* Promote char or short to int. */
2328 : 2688911 : value = perform_integral_promotions (value);
2329 : 151 : else if (value != error_mark_node)
2330 : : {
2331 : 53 : error_at (loc, "case label does not reduce to an integer constant");
2332 : 53 : value = error_mark_node;
2333 : : }
2334 : :
2335 : 2689062 : constant_expression_warning (value);
2336 : :
2337 : 2689062 : return value;
2338 : : }
2339 : :
2340 : : /* Return an integer type with BITS bits of precision,
2341 : : that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
2342 : :
2343 : : tree
2344 : 28237084 : c_common_type_for_size (unsigned int bits, int unsignedp)
2345 : : {
2346 : 28237084 : int i;
2347 : :
2348 : 28237084 : if (bits == TYPE_PRECISION (integer_type_node))
2349 : 18888290 : return unsignedp ? unsigned_type_node : integer_type_node;
2350 : :
2351 : 9348794 : if (bits == TYPE_PRECISION (signed_char_type_node))
2352 : 2593025 : return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2353 : :
2354 : 6755769 : if (bits == TYPE_PRECISION (short_integer_type_node))
2355 : 370119 : return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2356 : :
2357 : 6385650 : if (bits == TYPE_PRECISION (long_integer_type_node))
2358 : 5728638 : return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2359 : :
2360 : 657012 : if (bits == TYPE_PRECISION (long_long_integer_type_node))
2361 : 8493 : return (unsignedp ? long_long_unsigned_type_node
2362 : 7770 : : long_long_integer_type_node);
2363 : :
2364 : 1014421 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
2365 : 648519 : if (int_n_enabled_p[i]
2366 : 642347 : && bits == int_n_data[i].bitsize)
2367 : 282617 : return (unsignedp ? int_n_trees[i].unsigned_type
2368 : 282617 : : int_n_trees[i].signed_type);
2369 : :
2370 : 365902 : if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
2371 : 0 : return (unsignedp ? widest_unsigned_literal_type_node
2372 : 0 : : widest_integer_literal_type_node);
2373 : :
2374 : 1447687 : for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
2375 : : {
2376 : 1081785 : tree type = TREE_VALUE (t);
2377 : 1081785 : if (TREE_CODE (type) == INTEGER_TYPE
2378 : 0 : && bits == TYPE_PRECISION (type)
2379 : 1081785 : && !!unsignedp == !!TYPE_UNSIGNED (type))
2380 : : return type;
2381 : : }
2382 : :
2383 : 365902 : if (bits <= TYPE_PRECISION (intQI_type_node))
2384 : 305183 : return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2385 : :
2386 : 60719 : if (bits <= TYPE_PRECISION (intHI_type_node))
2387 : 9624 : return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2388 : :
2389 : 51095 : if (bits <= TYPE_PRECISION (intSI_type_node))
2390 : 20469 : return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2391 : :
2392 : 30626 : if (bits <= TYPE_PRECISION (intDI_type_node))
2393 : 24561 : return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2394 : :
2395 : 6065 : if (bits <= TYPE_PRECISION (widest_integer_literal_type_node))
2396 : 31 : return (unsignedp ? widest_unsigned_literal_type_node
2397 : 14 : : widest_integer_literal_type_node);
2398 : :
2399 : : return NULL_TREE;
2400 : : }
2401 : :
2402 : : /* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
2403 : : that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
2404 : : and saturating if SATP is nonzero, otherwise not saturating. */
2405 : :
2406 : : tree
2407 : 0 : c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
2408 : : int unsignedp, int satp)
2409 : : {
2410 : 0 : enum mode_class mclass;
2411 : 0 : if (ibit == 0)
2412 : 0 : mclass = unsignedp ? MODE_UFRACT : MODE_FRACT;
2413 : : else
2414 : 0 : mclass = unsignedp ? MODE_UACCUM : MODE_ACCUM;
2415 : :
2416 : 0 : opt_scalar_mode opt_mode;
2417 : 0 : scalar_mode mode;
2418 : 0 : FOR_EACH_MODE_IN_CLASS (opt_mode, mclass)
2419 : : {
2420 : 0 : mode = opt_mode.require ();
2421 : 0 : if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
2422 : : break;
2423 : : }
2424 : :
2425 : 0 : if (!opt_mode.exists (&mode) || !targetm.scalar_mode_supported_p (mode))
2426 : : {
2427 : 0 : sorry ("GCC cannot support operators with integer types and "
2428 : : "fixed-point types that have too many integral and "
2429 : : "fractional bits together");
2430 : 0 : return NULL_TREE;
2431 : : }
2432 : :
2433 : 0 : return c_common_type_for_mode (mode, satp);
2434 : : }
2435 : :
2436 : : /* Used for communication between c_common_type_for_mode and
2437 : : c_register_builtin_type. */
2438 : : tree registered_builtin_types;
2439 : :
2440 : : /* Return a data type that has machine mode MODE.
2441 : : If the mode is an integer,
2442 : : then UNSIGNEDP selects between signed and unsigned types.
2443 : : If the mode is a fixed-point mode,
2444 : : then UNSIGNEDP selects between saturating and nonsaturating types. */
2445 : :
2446 : : tree
2447 : 12521217 : c_common_type_for_mode (machine_mode mode, int unsignedp)
2448 : : {
2449 : 12521217 : tree t;
2450 : 12521217 : int i;
2451 : :
2452 : 12521217 : if (mode == TYPE_MODE (integer_type_node))
2453 : 1736036 : return unsignedp ? unsigned_type_node : integer_type_node;
2454 : :
2455 : 10785181 : if (mode == TYPE_MODE (signed_char_type_node))
2456 : 2099677 : return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2457 : :
2458 : 8685504 : if (mode == TYPE_MODE (short_integer_type_node))
2459 : 117092 : return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2460 : :
2461 : 8568412 : if (mode == TYPE_MODE (long_integer_type_node))
2462 : 5458427 : return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2463 : :
2464 : 3109985 : if (mode == TYPE_MODE (long_long_integer_type_node))
2465 : 82297 : return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2466 : :
2467 : 4766046 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
2468 : 3027688 : if (int_n_enabled_p[i]
2469 : 3027688 : && mode == int_n_data[i].m)
2470 : 1289330 : return (unsignedp ? int_n_trees[i].unsigned_type
2471 : 1289330 : : int_n_trees[i].signed_type);
2472 : :
2473 : 1738358 : if (mode == QImode)
2474 : 0 : return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2475 : :
2476 : 1738358 : if (mode == HImode)
2477 : 0 : return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2478 : :
2479 : 1738358 : if (mode == SImode)
2480 : 0 : return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2481 : :
2482 : 1738358 : if (mode == DImode)
2483 : 0 : return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2484 : :
2485 : : #if HOST_BITS_PER_WIDE_INT >= 64
2486 : 1738358 : if (mode == TYPE_MODE (intTI_type_node))
2487 : 0 : return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2488 : : #endif
2489 : :
2490 : 1738358 : if (mode == TYPE_MODE (float_type_node))
2491 : 52581 : return float_type_node;
2492 : :
2493 : 1685777 : if (mode == TYPE_MODE (double_type_node))
2494 : 45701 : return double_type_node;
2495 : :
2496 : 1640076 : if (mode == TYPE_MODE (long_double_type_node))
2497 : 18771 : return long_double_type_node;
2498 : :
2499 : 12351468 : for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2500 : 10879680 : if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2501 : 10879680 : && mode == TYPE_MODE (FLOATN_NX_TYPE_NODE (i)))
2502 : 149517 : return FLOATN_NX_TYPE_NODE (i);
2503 : :
2504 : 1471788 : if (mode == TYPE_MODE (void_type_node))
2505 : 0 : return void_type_node;
2506 : :
2507 : 1471788 : if (mode == TYPE_MODE (build_pointer_type (char_type_node))
2508 : 1471788 : || mode == TYPE_MODE (build_pointer_type (integer_type_node)))
2509 : : {
2510 : 0 : unsigned int precision
2511 : 0 : = GET_MODE_PRECISION (as_a <scalar_int_mode> (mode));
2512 : 0 : return (unsignedp
2513 : 0 : ? make_unsigned_type (precision)
2514 : 0 : : make_signed_type (precision));
2515 : : }
2516 : :
2517 : 1471788 : if (COMPLEX_MODE_P (mode))
2518 : : {
2519 : 1239172 : machine_mode inner_mode;
2520 : 1239172 : tree inner_type;
2521 : :
2522 : 1239172 : if (mode == TYPE_MODE (complex_float_type_node))
2523 : 207283 : return complex_float_type_node;
2524 : 1031889 : if (mode == TYPE_MODE (complex_double_type_node))
2525 : 206341 : return complex_double_type_node;
2526 : 825548 : if (mode == TYPE_MODE (complex_long_double_type_node))
2527 : 206335 : return complex_long_double_type_node;
2528 : :
2529 : 2682056 : for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
2530 : 2475988 : if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE
2531 : 2475988 : && mode == TYPE_MODE (COMPLEX_FLOATN_NX_TYPE_NODE (i)))
2532 : 413145 : return COMPLEX_FLOATN_NX_TYPE_NODE (i);
2533 : :
2534 : 206068 : if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
2535 : 12 : return complex_integer_type_node;
2536 : :
2537 : 206056 : inner_mode = GET_MODE_INNER (mode);
2538 : 206056 : inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2539 : 206056 : if (inner_type != NULL_TREE)
2540 : 206056 : return build_complex_type (inner_type);
2541 : : }
2542 : 232616 : else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
2543 : 232616 : && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
2544 : : {
2545 : 0 : unsigned int elem_bits = vector_element_size (GET_MODE_PRECISION (mode),
2546 : : GET_MODE_NUNITS (mode));
2547 : 0 : tree bool_type = build_nonstandard_boolean_type (elem_bits);
2548 : 0 : return build_vector_type_for_mode (bool_type, mode);
2549 : : }
2550 : 220777 : else if (VECTOR_MODE_P (mode)
2551 : 246106 : && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
2552 : : {
2553 : 13490 : machine_mode inner_mode = GET_MODE_INNER (mode);
2554 : 13490 : tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2555 : 13490 : if (inner_type != NULL_TREE)
2556 : 13490 : return build_vector_type_for_mode (inner_type, mode);
2557 : : }
2558 : :
2559 : 219126 : if (dfloat32_type_node != NULL_TREE
2560 : 219126 : && mode == TYPE_MODE (dfloat32_type_node))
2561 : 2783 : return dfloat32_type_node;
2562 : 216343 : if (dfloat64_type_node != NULL_TREE
2563 : 216343 : && mode == TYPE_MODE (dfloat64_type_node))
2564 : 3130 : return dfloat64_type_node;
2565 : 213213 : if (dfloat128_type_node != NULL_TREE
2566 : 213213 : && mode == TYPE_MODE (dfloat128_type_node))
2567 : 2424 : return dfloat128_type_node;
2568 : :
2569 : 210789 : if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
2570 : : {
2571 : 0 : if (mode == TYPE_MODE (short_fract_type_node))
2572 : 0 : return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
2573 : 0 : if (mode == TYPE_MODE (fract_type_node))
2574 : 0 : return unsignedp ? sat_fract_type_node : fract_type_node;
2575 : 0 : if (mode == TYPE_MODE (long_fract_type_node))
2576 : 0 : return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
2577 : 0 : if (mode == TYPE_MODE (long_long_fract_type_node))
2578 : 0 : return unsignedp ? sat_long_long_fract_type_node
2579 : 0 : : long_long_fract_type_node;
2580 : :
2581 : 0 : if (mode == TYPE_MODE (unsigned_short_fract_type_node))
2582 : 0 : return unsignedp ? sat_unsigned_short_fract_type_node
2583 : 0 : : unsigned_short_fract_type_node;
2584 : 0 : if (mode == TYPE_MODE (unsigned_fract_type_node))
2585 : 0 : return unsignedp ? sat_unsigned_fract_type_node
2586 : 0 : : unsigned_fract_type_node;
2587 : 0 : if (mode == TYPE_MODE (unsigned_long_fract_type_node))
2588 : 0 : return unsignedp ? sat_unsigned_long_fract_type_node
2589 : 0 : : unsigned_long_fract_type_node;
2590 : 0 : if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
2591 : 0 : return unsignedp ? sat_unsigned_long_long_fract_type_node
2592 : 0 : : unsigned_long_long_fract_type_node;
2593 : :
2594 : 0 : if (mode == TYPE_MODE (short_accum_type_node))
2595 : 0 : return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
2596 : 0 : if (mode == TYPE_MODE (accum_type_node))
2597 : 0 : return unsignedp ? sat_accum_type_node : accum_type_node;
2598 : 0 : if (mode == TYPE_MODE (long_accum_type_node))
2599 : 0 : return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
2600 : 0 : if (mode == TYPE_MODE (long_long_accum_type_node))
2601 : 0 : return unsignedp ? sat_long_long_accum_type_node
2602 : 0 : : long_long_accum_type_node;
2603 : :
2604 : 0 : if (mode == TYPE_MODE (unsigned_short_accum_type_node))
2605 : 0 : return unsignedp ? sat_unsigned_short_accum_type_node
2606 : 0 : : unsigned_short_accum_type_node;
2607 : 0 : if (mode == TYPE_MODE (unsigned_accum_type_node))
2608 : 0 : return unsignedp ? sat_unsigned_accum_type_node
2609 : 0 : : unsigned_accum_type_node;
2610 : 0 : if (mode == TYPE_MODE (unsigned_long_accum_type_node))
2611 : 0 : return unsignedp ? sat_unsigned_long_accum_type_node
2612 : 0 : : unsigned_long_accum_type_node;
2613 : 0 : if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
2614 : 0 : return unsignedp ? sat_unsigned_long_long_accum_type_node
2615 : 0 : : unsigned_long_long_accum_type_node;
2616 : :
2617 : 0 : if (mode == QQmode)
2618 : 0 : return unsignedp ? sat_qq_type_node : qq_type_node;
2619 : : if (mode == HQmode)
2620 : 0 : return unsignedp ? sat_hq_type_node : hq_type_node;
2621 : : if (mode == SQmode)
2622 : 0 : return unsignedp ? sat_sq_type_node : sq_type_node;
2623 : : if (mode == DQmode)
2624 : 0 : return unsignedp ? sat_dq_type_node : dq_type_node;
2625 : : if (mode == TQmode)
2626 : 0 : return unsignedp ? sat_tq_type_node : tq_type_node;
2627 : :
2628 : : if (mode == UQQmode)
2629 : 0 : return unsignedp ? sat_uqq_type_node : uqq_type_node;
2630 : : if (mode == UHQmode)
2631 : 0 : return unsignedp ? sat_uhq_type_node : uhq_type_node;
2632 : : if (mode == USQmode)
2633 : 0 : return unsignedp ? sat_usq_type_node : usq_type_node;
2634 : : if (mode == UDQmode)
2635 : 0 : return unsignedp ? sat_udq_type_node : udq_type_node;
2636 : : if (mode == UTQmode)
2637 : 0 : return unsignedp ? sat_utq_type_node : utq_type_node;
2638 : :
2639 : : if (mode == HAmode)
2640 : 0 : return unsignedp ? sat_ha_type_node : ha_type_node;
2641 : : if (mode == SAmode)
2642 : 0 : return unsignedp ? sat_sa_type_node : sa_type_node;
2643 : : if (mode == DAmode)
2644 : 0 : return unsignedp ? sat_da_type_node : da_type_node;
2645 : : if (mode == TAmode)
2646 : 0 : return unsignedp ? sat_ta_type_node : ta_type_node;
2647 : :
2648 : : if (mode == UHAmode)
2649 : 0 : return unsignedp ? sat_uha_type_node : uha_type_node;
2650 : : if (mode == USAmode)
2651 : 0 : return unsignedp ? sat_usa_type_node : usa_type_node;
2652 : : if (mode == UDAmode)
2653 : 0 : return unsignedp ? sat_uda_type_node : uda_type_node;
2654 : : if (mode == UTAmode)
2655 : 0 : return unsignedp ? sat_uta_type_node : uta_type_node;
2656 : : }
2657 : :
2658 : 214025 : for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
2659 : : {
2660 : 212961 : tree type = TREE_VALUE (t);
2661 : 212961 : if (TYPE_MODE (type) == mode
2662 : 209828 : && VECTOR_TYPE_P (type) == VECTOR_MODE_P (mode)
2663 : 422789 : && !!unsignedp == !!TYPE_UNSIGNED (type))
2664 : : return type;
2665 : : }
2666 : : return NULL_TREE;
2667 : : }
2668 : :
2669 : : tree
2670 : 1251190 : c_common_unsigned_type (tree type)
2671 : : {
2672 : 1251190 : return c_common_signed_or_unsigned_type (1, type);
2673 : : }
2674 : :
2675 : : /* Return a signed type the same as TYPE in other respects. */
2676 : :
2677 : : tree
2678 : 58810558 : c_common_signed_type (tree type)
2679 : : {
2680 : 58810558 : return c_common_signed_or_unsigned_type (0, type);
2681 : : }
2682 : :
2683 : : /* Return a type the same as TYPE except unsigned or
2684 : : signed according to UNSIGNEDP. */
2685 : :
2686 : : tree
2687 : 62883127 : c_common_signed_or_unsigned_type (int unsignedp, tree type)
2688 : : {
2689 : 62883127 : tree type1;
2690 : 62883127 : int i;
2691 : :
2692 : : /* This block of code emulates the behavior of the old
2693 : : c_common_unsigned_type. In particular, it returns
2694 : : long_unsigned_type_node if passed a long, even when a int would
2695 : : have the same size. This is necessary for warnings to work
2696 : : correctly in archs where sizeof(int) == sizeof(long) */
2697 : :
2698 : 62883127 : type1 = TYPE_MAIN_VARIANT (type);
2699 : 62883127 : if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
2700 : 2668068 : return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2701 : 60215059 : if (type1 == integer_type_node || type1 == unsigned_type_node)
2702 : 17654320 : return unsignedp ? unsigned_type_node : integer_type_node;
2703 : 42560739 : if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
2704 : 5808827 : return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2705 : 36751912 : if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
2706 : 22714612 : return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2707 : 14037300 : if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
2708 : 2461531 : return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
2709 : :
2710 : 22583634 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
2711 : 11575769 : if (int_n_enabled_p[i]
2712 : 10126384 : && (type1 == int_n_trees[i].unsigned_type
2713 : 9559182 : || type1 == int_n_trees[i].signed_type))
2714 : 567904 : return (unsignedp ? int_n_trees[i].unsigned_type
2715 : 567904 : : int_n_trees[i].signed_type);
2716 : :
2717 : : #if HOST_BITS_PER_WIDE_INT >= 64
2718 : 11007865 : if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
2719 : 0 : return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2720 : : #endif
2721 : 11007865 : if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
2722 : 0 : return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2723 : 11007865 : if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
2724 : 0 : return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2725 : 11007865 : if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
2726 : 0 : return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2727 : 11007865 : if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
2728 : 0 : return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2729 : :
2730 : : #define C_COMMON_FIXED_TYPES(NAME) \
2731 : : if (type1 == short_ ## NAME ## _type_node \
2732 : : || type1 == unsigned_short_ ## NAME ## _type_node) \
2733 : : return unsignedp ? unsigned_short_ ## NAME ## _type_node \
2734 : : : short_ ## NAME ## _type_node; \
2735 : : if (type1 == NAME ## _type_node \
2736 : : || type1 == unsigned_ ## NAME ## _type_node) \
2737 : : return unsignedp ? unsigned_ ## NAME ## _type_node \
2738 : : : NAME ## _type_node; \
2739 : : if (type1 == long_ ## NAME ## _type_node \
2740 : : || type1 == unsigned_long_ ## NAME ## _type_node) \
2741 : : return unsignedp ? unsigned_long_ ## NAME ## _type_node \
2742 : : : long_ ## NAME ## _type_node; \
2743 : : if (type1 == long_long_ ## NAME ## _type_node \
2744 : : || type1 == unsigned_long_long_ ## NAME ## _type_node) \
2745 : : return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
2746 : : : long_long_ ## NAME ## _type_node;
2747 : :
2748 : : #define C_COMMON_FIXED_MODE_TYPES(NAME) \
2749 : : if (type1 == NAME ## _type_node \
2750 : : || type1 == u ## NAME ## _type_node) \
2751 : : return unsignedp ? u ## NAME ## _type_node \
2752 : : : NAME ## _type_node;
2753 : :
2754 : : #define C_COMMON_FIXED_TYPES_SAT(NAME) \
2755 : : if (type1 == sat_ ## short_ ## NAME ## _type_node \
2756 : : || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
2757 : : return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
2758 : : : sat_ ## short_ ## NAME ## _type_node; \
2759 : : if (type1 == sat_ ## NAME ## _type_node \
2760 : : || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
2761 : : return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
2762 : : : sat_ ## NAME ## _type_node; \
2763 : : if (type1 == sat_ ## long_ ## NAME ## _type_node \
2764 : : || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
2765 : : return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
2766 : : : sat_ ## long_ ## NAME ## _type_node; \
2767 : : if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
2768 : : || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
2769 : : return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
2770 : : : sat_ ## long_long_ ## NAME ## _type_node;
2771 : :
2772 : : #define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
2773 : : if (type1 == sat_ ## NAME ## _type_node \
2774 : : || type1 == sat_ ## u ## NAME ## _type_node) \
2775 : : return unsignedp ? sat_ ## u ## NAME ## _type_node \
2776 : : : sat_ ## NAME ## _type_node;
2777 : :
2778 : 11007865 : C_COMMON_FIXED_TYPES (fract);
2779 : 11007865 : C_COMMON_FIXED_TYPES_SAT (fract);
2780 : 11007865 : C_COMMON_FIXED_TYPES (accum);
2781 : 11007865 : C_COMMON_FIXED_TYPES_SAT (accum);
2782 : :
2783 : 11007865 : C_COMMON_FIXED_MODE_TYPES (qq);
2784 : 11007865 : C_COMMON_FIXED_MODE_TYPES (hq);
2785 : 11007865 : C_COMMON_FIXED_MODE_TYPES (sq);
2786 : 11007865 : C_COMMON_FIXED_MODE_TYPES (dq);
2787 : 11007865 : C_COMMON_FIXED_MODE_TYPES (tq);
2788 : 11007865 : C_COMMON_FIXED_MODE_TYPES_SAT (qq);
2789 : 11007865 : C_COMMON_FIXED_MODE_TYPES_SAT (hq);
2790 : 11007865 : C_COMMON_FIXED_MODE_TYPES_SAT (sq);
2791 : 11007865 : C_COMMON_FIXED_MODE_TYPES_SAT (dq);
2792 : 11007865 : C_COMMON_FIXED_MODE_TYPES_SAT (tq);
2793 : 11007865 : C_COMMON_FIXED_MODE_TYPES (ha);
2794 : 11007865 : C_COMMON_FIXED_MODE_TYPES (sa);
2795 : 11007865 : C_COMMON_FIXED_MODE_TYPES (da);
2796 : 11007865 : C_COMMON_FIXED_MODE_TYPES (ta);
2797 : 11007865 : C_COMMON_FIXED_MODE_TYPES_SAT (ha);
2798 : 11007865 : C_COMMON_FIXED_MODE_TYPES_SAT (sa);
2799 : 11007865 : C_COMMON_FIXED_MODE_TYPES_SAT (da);
2800 : 11007865 : C_COMMON_FIXED_MODE_TYPES_SAT (ta);
2801 : :
2802 : : /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
2803 : : the precision; they have precision set to match their range, but
2804 : : may use a wider mode to match an ABI. If we change modes, we may
2805 : : wind up with bad conversions. For INTEGER_TYPEs in C, must check
2806 : : the precision as well, so as to yield correct results for
2807 : : bit-field types. C++ does not have these separate bit-field
2808 : : types, and producing a signed or unsigned variant of an
2809 : : ENUMERAL_TYPE may cause other problems as well. */
2810 : :
2811 : 11007865 : if (!INTEGRAL_TYPE_P (type)
2812 : 11007865 : || TYPE_UNSIGNED (type) == unsignedp)
2813 : : return type;
2814 : :
2815 : 10052038 : if (TREE_CODE (type) == BITINT_TYPE
2816 : : /* signed _BitInt(1) is invalid, avoid creating that. */
2817 : 10052038 : && (unsignedp || TYPE_PRECISION (type) > 1))
2818 : 1454131 : return build_bitint_type (TYPE_PRECISION (type), unsignedp);
2819 : :
2820 : : #define TYPE_OK(node) \
2821 : : (TYPE_MODE (type) == TYPE_MODE (node) \
2822 : : && TYPE_PRECISION (type) == TYPE_PRECISION (node))
2823 : 8597907 : if (TYPE_OK (signed_char_type_node))
2824 : 545925 : return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2825 : 8051982 : if (TYPE_OK (integer_type_node))
2826 : 3991843 : return unsignedp ? unsigned_type_node : integer_type_node;
2827 : 4060139 : if (TYPE_OK (short_integer_type_node))
2828 : 847492 : return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2829 : 3212647 : if (TYPE_OK (long_integer_type_node))
2830 : 2777255 : return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2831 : 435392 : if (TYPE_OK (long_long_integer_type_node))
2832 : 114571 : return (unsignedp ? long_long_unsigned_type_node
2833 : 0 : : long_long_integer_type_node);
2834 : :
2835 : 623056 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
2836 : 320821 : if (int_n_enabled_p[i]
2837 : 308741 : && TYPE_MODE (type) == int_n_data[i].m
2838 : 339471 : && TYPE_PRECISION (type) == int_n_data[i].bitsize)
2839 : 18586 : return (unsignedp ? int_n_trees[i].unsigned_type
2840 : 18586 : : int_n_trees[i].signed_type);
2841 : :
2842 : : #if HOST_BITS_PER_WIDE_INT >= 64
2843 : 302235 : if (TYPE_OK (intTI_type_node))
2844 : 0 : return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
2845 : : #endif
2846 : 302235 : if (TYPE_OK (intDI_type_node))
2847 : 0 : return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
2848 : 302235 : if (TYPE_OK (intSI_type_node))
2849 : 0 : return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
2850 : 302235 : if (TYPE_OK (intHI_type_node))
2851 : 0 : return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
2852 : 302235 : if (TYPE_OK (intQI_type_node))
2853 : 0 : return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
2854 : : #undef TYPE_OK
2855 : :
2856 : 302235 : return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
2857 : : }
2858 : :
2859 : : /* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
2860 : :
2861 : : tree
2862 : 316354 : c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
2863 : : {
2864 : 316354 : int i;
2865 : :
2866 : : /* Extended integer types of the same width as a standard type have
2867 : : lesser rank, so those of the same width as int promote to int or
2868 : : unsigned int and are valid for printf formats expecting int or
2869 : : unsigned int. To avoid such special cases, avoid creating
2870 : : extended integer types for bit-fields if a standard integer type
2871 : : is available. */
2872 : 316354 : if (width == TYPE_PRECISION (integer_type_node))
2873 : 138 : return unsignedp ? unsigned_type_node : integer_type_node;
2874 : 316216 : if (width == TYPE_PRECISION (signed_char_type_node))
2875 : 6749 : return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2876 : 309467 : if (width == TYPE_PRECISION (short_integer_type_node))
2877 : 1426 : return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2878 : 308041 : if (width == TYPE_PRECISION (long_integer_type_node))
2879 : 0 : return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2880 : 308041 : if (width == TYPE_PRECISION (long_long_integer_type_node))
2881 : 0 : return (unsignedp ? long_long_unsigned_type_node
2882 : 0 : : long_long_integer_type_node);
2883 : 616082 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
2884 : 308041 : if (int_n_enabled_p[i]
2885 : 301152 : && width == int_n_data[i].bitsize)
2886 : 0 : return (unsignedp ? int_n_trees[i].unsigned_type
2887 : 0 : : int_n_trees[i].signed_type);
2888 : 308041 : return build_nonstandard_integer_type (width, unsignedp);
2889 : : }
2890 : :
2891 : : /* The C version of the register_builtin_type langhook. */
2892 : :
2893 : : void
2894 : 618078 : c_register_builtin_type (tree type, const char* name)
2895 : : {
2896 : 618078 : tree decl;
2897 : :
2898 : 618078 : decl = build_decl (UNKNOWN_LOCATION,
2899 : : TYPE_DECL, get_identifier (name), type);
2900 : 618078 : DECL_ARTIFICIAL (decl) = 1;
2901 : 618078 : if (!TYPE_NAME (type))
2902 : 301084 : TYPE_NAME (type) = decl;
2903 : 618078 : lang_hooks.decls.pushdecl (decl);
2904 : :
2905 : 618078 : registered_builtin_types = tree_cons (0, type, registered_builtin_types);
2906 : 618078 : }
2907 : :
2908 : : /* Print an error message for invalid operands to arith operation
2909 : : CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
2910 : : RICHLOC is a rich location for the message, containing either
2911 : : three separate locations for each of the operator and operands
2912 : :
2913 : : lhs op rhs
2914 : : ~~~ ^~ ~~~
2915 : :
2916 : : (C FE), or one location ranging over all over them
2917 : :
2918 : : lhs op rhs
2919 : : ~~~~^~~~~~
2920 : :
2921 : : (C++ FE). */
2922 : :
2923 : : void
2924 : 558 : binary_op_error (rich_location *richloc, enum tree_code code,
2925 : : tree type0, tree type1)
2926 : : {
2927 : 558 : const char *opname;
2928 : :
2929 : 558 : switch (code)
2930 : : {
2931 : : case PLUS_EXPR:
2932 : : opname = "+"; break;
2933 : 14 : case MINUS_EXPR:
2934 : 14 : opname = "-"; break;
2935 : 378 : case MULT_EXPR:
2936 : 378 : opname = "*"; break;
2937 : 0 : case MAX_EXPR:
2938 : 0 : opname = "max"; break;
2939 : 0 : case MIN_EXPR:
2940 : 0 : opname = "min"; break;
2941 : 2 : case EQ_EXPR:
2942 : 2 : opname = "=="; break;
2943 : 2 : case NE_EXPR:
2944 : 2 : opname = "!="; break;
2945 : 0 : case LE_EXPR:
2946 : 0 : opname = "<="; break;
2947 : 0 : case GE_EXPR:
2948 : 0 : opname = ">="; break;
2949 : 1 : case LT_EXPR:
2950 : 1 : opname = "<"; break;
2951 : 1 : case GT_EXPR:
2952 : 1 : opname = ">"; break;
2953 : 21 : case LSHIFT_EXPR:
2954 : 21 : opname = "<<"; break;
2955 : 16 : case RSHIFT_EXPR:
2956 : 16 : opname = ">>"; break;
2957 : 3 : case TRUNC_MOD_EXPR:
2958 : 3 : case FLOOR_MOD_EXPR:
2959 : 3 : opname = "%"; break;
2960 : 6 : case TRUNC_DIV_EXPR:
2961 : 6 : case FLOOR_DIV_EXPR:
2962 : 6 : opname = "/"; break;
2963 : 40 : case BIT_AND_EXPR:
2964 : 40 : opname = "&"; break;
2965 : 21 : case BIT_IOR_EXPR:
2966 : 21 : opname = "|"; break;
2967 : 0 : case TRUTH_ANDIF_EXPR:
2968 : 0 : opname = "&&"; break;
2969 : 0 : case TRUTH_ORIF_EXPR:
2970 : 0 : opname = "||"; break;
2971 : 22 : case BIT_XOR_EXPR:
2972 : 22 : opname = "^"; break;
2973 : 0 : default:
2974 : 0 : gcc_unreachable ();
2975 : : }
2976 : 558 : pp_markup::element_quoted_type element_0 (type0, highlight_colors::lhs);
2977 : 1116 : pp_markup::element_quoted_type element_1 (type1, highlight_colors::rhs);
2978 : 558 : error_at (richloc,
2979 : : "invalid operands to binary %s (have %e and %e)",
2980 : : opname, &element_0, &element_1);
2981 : 558 : }
2982 : :
2983 : : /* Given an expression as a tree, return its original type. Do this
2984 : : by stripping any conversion that preserves the sign and precision. */
2985 : : static tree
2986 : 54557 : expr_original_type (tree expr)
2987 : : {
2988 : 54557 : STRIP_SIGN_NOPS (expr);
2989 : 54557 : return TREE_TYPE (expr);
2990 : : }
2991 : :
2992 : : /* Subroutine of build_binary_op, used for comparison operations.
2993 : : See if the operands have both been converted from subword integer types
2994 : : and, if so, perhaps change them both back to their original type.
2995 : : This function is also responsible for converting the two operands
2996 : : to the proper common type for comparison.
2997 : :
2998 : : The arguments of this function are all pointers to local variables
2999 : : of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3000 : : RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3001 : :
3002 : : LOC is the location of the comparison.
3003 : :
3004 : : If this function returns non-NULL_TREE, it means that the comparison has
3005 : : a constant value. What this function returns is an expression for
3006 : : that value. */
3007 : :
3008 : : tree
3009 : 25772914 : shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
3010 : : tree *restype_ptr, enum tree_code *rescode_ptr)
3011 : : {
3012 : 25772914 : tree type;
3013 : 25772914 : tree op0 = *op0_ptr;
3014 : 25772914 : tree op1 = *op1_ptr;
3015 : 25772914 : int unsignedp0, unsignedp1;
3016 : 25772914 : int real1, real2;
3017 : 25772914 : tree primop0, primop1;
3018 : 25772914 : enum tree_code code = *rescode_ptr;
3019 : :
3020 : : /* Throw away any conversions to wider types
3021 : : already present in the operands. */
3022 : :
3023 : 25772914 : primop0 = c_common_get_narrower (op0, &unsignedp0);
3024 : 25772914 : primop1 = c_common_get_narrower (op1, &unsignedp1);
3025 : :
3026 : : /* If primopN is first sign-extended from primopN's precision to opN's
3027 : : precision, then zero-extended from opN's precision to
3028 : : *restype_ptr precision, shortenings might be invalid. */
3029 : 25772914 : if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3030 : 1488711 : && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3031 : 5512 : && !unsignedp0
3032 : 25778168 : && TYPE_UNSIGNED (TREE_TYPE (op0)))
3033 : : primop0 = op0;
3034 : 25772914 : if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3035 : 910469 : && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3036 : 16454 : && !unsignedp1
3037 : 25784100 : && TYPE_UNSIGNED (TREE_TYPE (op1)))
3038 : : primop1 = op1;
3039 : :
3040 : : /* Handle the case that OP0 does not *contain* a conversion
3041 : : but it *requires* conversion to FINAL_TYPE. */
3042 : :
3043 : 25772914 : if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
3044 : 433340 : unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
3045 : 25772914 : if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
3046 : 6742985 : unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
3047 : :
3048 : : /* If one of the operands must be floated, we cannot optimize. */
3049 : 25772914 : real1 = SCALAR_FLOAT_TYPE_P (TREE_TYPE (primop0));
3050 : 25772914 : real2 = SCALAR_FLOAT_TYPE_P (TREE_TYPE (primop1));
3051 : :
3052 : : /* If first arg is constant, swap the args (changing operation
3053 : : so value is preserved), for canonicalization. Don't do this if
3054 : : the second arg is 0. */
3055 : :
3056 : 25772914 : if (TREE_CONSTANT (primop0)
3057 : 5355979 : && !integer_zerop (primop1) && !real_zerop (primop1)
3058 : 29715414 : && !fixed_zerop (primop1))
3059 : : {
3060 : 3942500 : std::swap (primop0, primop1);
3061 : 3942500 : std::swap (op0, op1);
3062 : 3942500 : *op0_ptr = op0;
3063 : 3942500 : *op1_ptr = op1;
3064 : 3942500 : std::swap (unsignedp0, unsignedp1);
3065 : 3942500 : std::swap (real1, real2);
3066 : :
3067 : 3942500 : switch (code)
3068 : : {
3069 : : case LT_EXPR:
3070 : : code = GT_EXPR;
3071 : : break;
3072 : : case GT_EXPR:
3073 : : code = LT_EXPR;
3074 : : break;
3075 : : case LE_EXPR:
3076 : : code = GE_EXPR;
3077 : : break;
3078 : : case GE_EXPR:
3079 : : code = LE_EXPR;
3080 : : break;
3081 : : default:
3082 : : break;
3083 : : }
3084 : 3942500 : *rescode_ptr = code;
3085 : : }
3086 : :
3087 : : /* If comparing an integer against a constant more bits wide,
3088 : : maybe we can deduce a value of 1 or 0 independent of the data.
3089 : : Or else truncate the constant now
3090 : : rather than extend the variable at run time.
3091 : :
3092 : : This is only interesting if the constant is the wider arg.
3093 : : Also, it is not safe if the constant is unsigned and the
3094 : : variable arg is signed, since in this case the variable
3095 : : would be sign-extended and then regarded as unsigned.
3096 : : Our technique fails in this case because the lowest/highest
3097 : : possible unsigned results don't follow naturally from the
3098 : : lowest/highest possible values of the variable operand.
3099 : : For just EQ_EXPR and NE_EXPR there is another technique that
3100 : : could be used: see if the constant can be faithfully represented
3101 : : in the other operand's type, by truncating it and reextending it
3102 : : and see if that preserves the constant's value. */
3103 : :
3104 : 21294421 : if (!real1 && !real2
3105 : 21270060 : && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
3106 : 21270060 : && TREE_CODE (primop1) == INTEGER_CST
3107 : 39638623 : && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
3108 : : {
3109 : 1546327 : int min_gt, max_gt, min_lt, max_lt;
3110 : 1546327 : tree maxval, minval;
3111 : : /* 1 if comparison is nominally unsigned. */
3112 : 1546327 : int unsignedp = TYPE_UNSIGNED (*restype_ptr);
3113 : 1546327 : tree val;
3114 : :
3115 : 1546327 : type = c_common_signed_or_unsigned_type (unsignedp0,
3116 : 1546327 : TREE_TYPE (primop0));
3117 : :
3118 : 1546327 : maxval = TYPE_MAX_VALUE (type);
3119 : 1546327 : minval = TYPE_MIN_VALUE (type);
3120 : :
3121 : 1546327 : if (unsignedp && !unsignedp0)
3122 : 409438 : *restype_ptr = c_common_signed_type (*restype_ptr);
3123 : :
3124 : 1546327 : if (TREE_TYPE (primop1) != *restype_ptr)
3125 : : {
3126 : : /* Convert primop1 to target type, but do not introduce
3127 : : additional overflow. We know primop1 is an int_cst. */
3128 : 409742 : primop1 = force_fit_type (*restype_ptr,
3129 : : wi::to_wide
3130 : 819484 : (primop1,
3131 : 409742 : TYPE_PRECISION (*restype_ptr)),
3132 : 409742 : 0, TREE_OVERFLOW (primop1));
3133 : : }
3134 : 1546327 : if (type != *restype_ptr)
3135 : : {
3136 : 1546327 : minval = convert (*restype_ptr, minval);
3137 : 1546327 : maxval = convert (*restype_ptr, maxval);
3138 : : }
3139 : :
3140 : 1546327 : min_gt = tree_int_cst_lt (primop1, minval);
3141 : 1546327 : max_gt = tree_int_cst_lt (primop1, maxval);
3142 : 1546327 : min_lt = tree_int_cst_lt (minval, primop1);
3143 : 1546327 : max_lt = tree_int_cst_lt (maxval, primop1);
3144 : :
3145 : 1546327 : val = 0;
3146 : : /* This used to be a switch, but Genix compiler can't handle that. */
3147 : 1546327 : if (code == NE_EXPR)
3148 : : {
3149 : 356612 : if (max_lt || min_gt)
3150 : 23 : val = truthvalue_true_node;
3151 : : }
3152 : : else if (code == EQ_EXPR)
3153 : : {
3154 : 622187 : if (max_lt || min_gt)
3155 : 27 : val = truthvalue_false_node;
3156 : : }
3157 : : else if (code == LT_EXPR)
3158 : : {
3159 : 313924 : if (max_lt)
3160 : 18 : val = truthvalue_true_node;
3161 : 313924 : if (!min_lt)
3162 : 7468 : val = truthvalue_false_node;
3163 : : }
3164 : : else if (code == GT_EXPR)
3165 : : {
3166 : 117922 : if (min_gt)
3167 : 8 : val = truthvalue_true_node;
3168 : 117922 : if (!max_gt)
3169 : 6 : val = truthvalue_false_node;
3170 : : }
3171 : : else if (code == LE_EXPR)
3172 : : {
3173 : 56111 : if (!max_gt)
3174 : 74 : val = truthvalue_true_node;
3175 : 56111 : if (min_gt)
3176 : 7 : val = truthvalue_false_node;
3177 : : }
3178 : : else if (code == GE_EXPR)
3179 : : {
3180 : 79569 : if (!min_lt)
3181 : 178 : val = truthvalue_true_node;
3182 : 79569 : if (max_lt)
3183 : 14970 : val = truthvalue_false_node;
3184 : : }
3185 : :
3186 : : /* If primop0 was sign-extended and unsigned comparison specd,
3187 : : we did a signed comparison above using the signed type bounds.
3188 : : But the comparison we output must be unsigned.
3189 : :
3190 : : Also, for inequalities, VAL is no good; but if the signed
3191 : : comparison had *any* fixed result, it follows that the
3192 : : unsigned comparison just tests the sign in reverse
3193 : : (positive values are LE, negative ones GE).
3194 : : So we can generate an unsigned comparison
3195 : : against an extreme value of the signed type. */
3196 : :
3197 : 1546327 : if (unsignedp && !unsignedp0)
3198 : : {
3199 : 409438 : if (val != 0)
3200 : 112 : switch (code)
3201 : : {
3202 : 81 : case LT_EXPR:
3203 : 81 : case GE_EXPR:
3204 : 81 : primop1 = TYPE_MIN_VALUE (type);
3205 : 81 : val = 0;
3206 : 81 : break;
3207 : :
3208 : 30 : case LE_EXPR:
3209 : 30 : case GT_EXPR:
3210 : 30 : primop1 = TYPE_MAX_VALUE (type);
3211 : 30 : val = 0;
3212 : 30 : break;
3213 : :
3214 : : default:
3215 : : break;
3216 : : }
3217 : 409438 : type = c_common_unsigned_type (type);
3218 : : }
3219 : :
3220 : 1546327 : if (TREE_CODE (primop0) != INTEGER_CST
3221 : : /* Don't warn if it's from a (non-system) macro. */
3222 : 1546327 : && !(from_macro_expansion_at
3223 : 1066698 : (expansion_point_location_if_in_system_header
3224 : 1066698 : (EXPR_LOCATION (primop0)))))
3225 : : {
3226 : 1027760 : if (val == truthvalue_false_node)
3227 : 7502 : warning_at (loc, OPT_Wtype_limits,
3228 : : "comparison is always false due to limited range of data type");
3229 : 1027760 : if (val == truthvalue_true_node)
3230 : 187 : warning_at (loc, OPT_Wtype_limits,
3231 : : "comparison is always true due to limited range of data type");
3232 : : }
3233 : :
3234 : 1546327 : if (val != 0)
3235 : : {
3236 : : /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3237 : 22668 : if (TREE_SIDE_EFFECTS (primop0))
3238 : 21 : return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
3239 : : return val;
3240 : : }
3241 : :
3242 : : /* Value is not predetermined, but do the comparison
3243 : : in the type of the operand that is not constant.
3244 : : TYPE is already properly set. */
3245 : : }
3246 : :
3247 : : /* If either arg is decimal float and the other is float, find the
3248 : : proper common type to use for comparison. */
3249 : 4478493 : else if (real1 && real2
3250 : 4249859 : && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
3251 : 24243135 : && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
3252 : 16547 : type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
3253 : :
3254 : : /* If either arg is decimal float and the other is float, fail. */
3255 : 4461946 : else if (real1 && real2
3256 : 28443352 : && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
3257 : 4233311 : || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
3258 : : {
3259 : 2 : type = *restype_ptr;
3260 : 2 : primop0 = op0;
3261 : 2 : primop1 = op1;
3262 : : }
3263 : :
3264 : 4461944 : else if (real1 && real2
3265 : 28443348 : && (TYPE_PRECISION (TREE_TYPE (primop0))
3266 : 4233310 : == TYPE_PRECISION (TREE_TYPE (primop1))))
3267 : 4138484 : type = TREE_TYPE (primop0);
3268 : :
3269 : : /* If args' natural types are both narrower than nominal type
3270 : : and both extend in the same manner, compare them
3271 : : in the type of the wider arg.
3272 : : Otherwise must actually extend both to the nominal
3273 : : common type lest different ways of extending
3274 : : alter the result.
3275 : : (eg, (short)-1 == (unsigned short)-1 should be 0.) */
3276 : :
3277 : 17518309 : else if (unsignedp0 == unsignedp1 && real1 == real2
3278 : 17278119 : && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
3279 : 503306 : && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr)
3280 : 20424990 : && (type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1)))
3281 : 353436 : != error_mark_node)
3282 : : {
3283 : 353436 : type = c_common_signed_or_unsigned_type (unsignedp0
3284 : 353436 : || TYPE_UNSIGNED (*restype_ptr),
3285 : : type);
3286 : : /* Make sure shorter operand is extended the right way
3287 : : to match the longer operand. */
3288 : 353436 : primop0
3289 : 353436 : = convert (c_common_signed_or_unsigned_type (unsignedp0,
3290 : 353436 : TREE_TYPE (primop0)),
3291 : : primop0);
3292 : 353436 : primop1
3293 : 353436 : = convert (c_common_signed_or_unsigned_type (unsignedp1,
3294 : 353436 : TREE_TYPE (primop1)),
3295 : : primop1);
3296 : : }
3297 : : else
3298 : : {
3299 : : /* Here we must do the comparison on the nominal type
3300 : : using the args exactly as we received them. */
3301 : 19718118 : type = *restype_ptr;
3302 : 19718118 : primop0 = op0;
3303 : 19718118 : primop1 = op1;
3304 : :
3305 : : /* We want to fold unsigned comparisons of >= and < against zero.
3306 : : For these, we may also issue a warning if we have a non-constant
3307 : : compared against zero, where the zero was spelled as "0" (rather
3308 : : than merely folding to it).
3309 : : If we have at least one constant, then op1 is constant
3310 : : and we may have a non-constant expression as op0. */
3311 : 19394658 : if (!real1 && !real2 && integer_zerop (primop1)
3312 : 27830377 : && TYPE_UNSIGNED (*restype_ptr))
3313 : : {
3314 : 2695479 : tree value = NULL_TREE;
3315 : : /* All unsigned values are >= 0, so we warn. However,
3316 : : if OP0 is a constant that is >= 0, the signedness of
3317 : : the comparison isn't an issue, so suppress the
3318 : : warning. */
3319 : 2695479 : tree folded_op0 = fold_for_warn (op0);
3320 : 2695479 : bool warn =
3321 : 70031 : warn_type_limits && !in_system_header_at (loc)
3322 : 63833 : && !(TREE_CODE (folded_op0) == INTEGER_CST
3323 : 9276 : && !TREE_OVERFLOW (convert (c_common_signed_type (type),
3324 : : folded_op0)))
3325 : : /* Do not warn for enumeration types. */
3326 : 2750036 : && (TREE_CODE (expr_original_type (folded_op0)) != ENUMERAL_TYPE);
3327 : :
3328 : 2695479 : switch (code)
3329 : : {
3330 : 11921 : case GE_EXPR:
3331 : 11921 : if (warn)
3332 : 56 : warning_at (loc, OPT_Wtype_limits,
3333 : : "comparison of unsigned expression in %<>= 0%> "
3334 : : "is always true");
3335 : 11921 : value = truthvalue_true_node;
3336 : 11921 : break;
3337 : :
3338 : 286377 : case LT_EXPR:
3339 : 286377 : if (warn)
3340 : 55 : warning_at (loc, OPT_Wtype_limits,
3341 : : "comparison of unsigned expression in %<< 0%> "
3342 : : "is always false");
3343 : 286377 : value = truthvalue_false_node;
3344 : 286377 : break;
3345 : :
3346 : : default:
3347 : : break;
3348 : : }
3349 : :
3350 : 298298 : if (value != NULL_TREE)
3351 : : {
3352 : : /* Don't forget to evaluate PRIMOP0 if it has side effects. */
3353 : 298298 : if (TREE_SIDE_EFFECTS (primop0))
3354 : 45 : return build2 (COMPOUND_EXPR, TREE_TYPE (value),
3355 : 45 : primop0, value);
3356 : : return value;
3357 : : }
3358 : : }
3359 : : }
3360 : :
3361 : 25451948 : *op0_ptr = convert (type, primop0);
3362 : 25451948 : *op1_ptr = convert (type, primop1);
3363 : :
3364 : 25451948 : *restype_ptr = truthvalue_type_node;
3365 : :
3366 : 25451948 : return NULL_TREE;
3367 : : }
3368 : :
3369 : : /* Return a tree for the sum or difference (RESULTCODE says which)
3370 : : of pointer PTROP and integer INTOP. */
3371 : :
3372 : : tree
3373 : 5391102 : pointer_int_sum (location_t loc, enum tree_code resultcode,
3374 : : tree ptrop, tree intop, bool complain)
3375 : : {
3376 : 5391102 : tree size_exp, ret;
3377 : :
3378 : : /* The result is a pointer of the same type that is being added. */
3379 : 5391102 : tree result_type = TREE_TYPE (ptrop);
3380 : :
3381 : 5391102 : if (VOID_TYPE_P (TREE_TYPE (result_type)))
3382 : : {
3383 : 895 : if (complain && warn_pointer_arith)
3384 : 61 : pedwarn (loc, OPT_Wpointer_arith,
3385 : : "pointer of type %<void *%> used in arithmetic");
3386 : : else if (!complain)
3387 : 0 : return error_mark_node;
3388 : 895 : size_exp = integer_one_node;
3389 : : }
3390 : 5390207 : else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
3391 : : {
3392 : 118 : if (complain && warn_pointer_arith)
3393 : 49 : pedwarn (loc, OPT_Wpointer_arith,
3394 : : "pointer to a function used in arithmetic");
3395 : : else if (!complain)
3396 : 0 : return error_mark_node;
3397 : 118 : size_exp = integer_one_node;
3398 : : }
3399 : 5390089 : else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
3400 : 5390089 : TREE_TYPE (result_type)))
3401 : 0 : size_exp = integer_one_node;
3402 : : else
3403 : : {
3404 : 5390089 : if (!complain && !COMPLETE_TYPE_P (TREE_TYPE (result_type)))
3405 : 108 : return error_mark_node;
3406 : 5389981 : size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type));
3407 : : /* Wrap the pointer expression in a SAVE_EXPR to make sure it
3408 : : is evaluated first when the size expression may depend
3409 : : on it for VM types. */
3410 : 5389981 : if (TREE_SIDE_EFFECTS (size_exp)
3411 : 1935 : && TREE_SIDE_EFFECTS (ptrop)
3412 : 5390000 : && variably_modified_type_p (TREE_TYPE (ptrop), NULL))
3413 : : {
3414 : 19 : ptrop = save_expr (ptrop);
3415 : 19 : size_exp = build2 (COMPOUND_EXPR, TREE_TYPE (intop), ptrop, size_exp);
3416 : : }
3417 : : }
3418 : :
3419 : : /* We are manipulating pointer values, so we don't need to warn
3420 : : about relying on undefined signed overflow. We disable the
3421 : : warning here because we use integer types so fold won't know that
3422 : : they are really pointers. */
3423 : 5390994 : fold_defer_overflow_warnings ();
3424 : :
3425 : : /* If what we are about to multiply by the size of the elements
3426 : : contains a constant term, apply distributive law
3427 : : and multiply that constant term separately.
3428 : : This helps produce common subexpressions. */
3429 : 5390994 : if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3430 : 172033 : && !TREE_CONSTANT (intop)
3431 : 171945 : && TREE_CONSTANT (TREE_OPERAND (intop, 1))
3432 : 137386 : && TREE_CONSTANT (size_exp)
3433 : : /* If the constant comes from pointer subtraction,
3434 : : skip this optimization--it would cause an error. */
3435 : 137385 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
3436 : : /* If the constant is unsigned, and smaller than the pointer size,
3437 : : then we must skip this optimization. This is because it could cause
3438 : : an overflow error if the constant is negative but INTOP is not. */
3439 : 5528379 : && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (intop))
3440 : 117574 : || (TYPE_PRECISION (TREE_TYPE (intop))
3441 : 117574 : == TYPE_PRECISION (TREE_TYPE (ptrop)))))
3442 : : {
3443 : 34243 : enum tree_code subcode = resultcode;
3444 : 34243 : tree int_type = TREE_TYPE (intop);
3445 : 34243 : if (TREE_CODE (intop) == MINUS_EXPR)
3446 : 14543 : subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
3447 : : /* Convert both subexpression types to the type of intop,
3448 : : because weird cases involving pointer arithmetic
3449 : : can result in a sum or difference with different type args. */
3450 : 34243 : ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
3451 : : subcode, ptrop,
3452 : 34243 : convert (int_type, TREE_OPERAND (intop, 1)),
3453 : : true);
3454 : 34243 : intop = convert (int_type, TREE_OPERAND (intop, 0));
3455 : : }
3456 : :
3457 : : /* Convert the integer argument to a type the same size as sizetype
3458 : : so the multiply won't overflow spuriously. */
3459 : 5390994 : if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
3460 : 5390994 : || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
3461 : 3730134 : intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
3462 : 3730134 : TYPE_UNSIGNED (sizetype)), intop);
3463 : :
3464 : : /* Replace the integer argument with a suitable product by the object size.
3465 : : Do this multiplication as signed, then convert to the appropriate type
3466 : : for the pointer operation and disregard an overflow that occurred only
3467 : : because of the sign-extension change in the latter conversion. */
3468 : 5390994 : {
3469 : 5390994 : tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
3470 : 5390994 : convert (TREE_TYPE (intop), size_exp));
3471 : 5390994 : intop = convert (sizetype, t);
3472 : 5390994 : if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
3473 : 0 : intop = wide_int_to_tree (TREE_TYPE (intop), wi::to_wide (intop));
3474 : : }
3475 : :
3476 : : /* Create the sum or difference. */
3477 : 5390994 : if (resultcode == MINUS_EXPR)
3478 : 181922 : intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
3479 : :
3480 : 5390994 : ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
3481 : :
3482 : 5390994 : fold_undefer_and_ignore_overflow_warnings ();
3483 : :
3484 : 5390994 : return ret;
3485 : : }
3486 : :
3487 : : /* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
3488 : : and if NON_CONST is known not to be permitted in an evaluated part
3489 : : of a constant expression. */
3490 : :
3491 : : tree
3492 : 4373918 : c_wrap_maybe_const (tree expr, bool non_const)
3493 : : {
3494 : 4373918 : location_t loc = EXPR_LOCATION (expr);
3495 : :
3496 : : /* This should never be called for C++. */
3497 : 4373918 : if (c_dialect_cxx ())
3498 : 0 : gcc_unreachable ();
3499 : :
3500 : : /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
3501 : 4374664 : STRIP_TYPE_NOPS (expr);
3502 : 4373918 : expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
3503 : 4373918 : C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
3504 : 4373918 : protected_set_expr_location (expr, loc);
3505 : :
3506 : 4373918 : return expr;
3507 : : }
3508 : :
3509 : : /* Return whether EXPR is a declaration whose address can never be NULL.
3510 : : The address of the first struct member could be NULL only if it were
3511 : : accessed through a NULL pointer, and such an access would be invalid.
3512 : : The address of a weak symbol may be null unless it has a definition. */
3513 : :
3514 : : bool
3515 : 1410 : decl_with_nonnull_addr_p (const_tree expr)
3516 : : {
3517 : 1410 : if (!DECL_P (expr))
3518 : : return false;
3519 : :
3520 : 1156 : if (TREE_CODE (expr) == FIELD_DECL
3521 : 1079 : || TREE_CODE (expr) == PARM_DECL
3522 : 1045 : || TREE_CODE (expr) == LABEL_DECL)
3523 : : return true;
3524 : :
3525 : 1020 : if (!VAR_OR_FUNCTION_DECL_P (expr))
3526 : : return false;
3527 : :
3528 : 1020 : if (!DECL_WEAK (expr))
3529 : : /* Ordinary (non-weak) symbols have nonnull addresses. */
3530 : : return true;
3531 : :
3532 : 529 : if (DECL_INITIAL (expr) && DECL_INITIAL (expr) != error_mark_node)
3533 : : /* Initialized weak symbols have nonnull addresses. */
3534 : : return true;
3535 : :
3536 : 476 : if (DECL_EXTERNAL (expr) || !TREE_STATIC (expr))
3537 : : /* Uninitialized extern weak symbols and weak symbols with no
3538 : : allocated storage might have a null address. */
3539 : : return false;
3540 : :
3541 : 43 : tree attribs = DECL_ATTRIBUTES (expr);
3542 : 43 : if (lookup_attribute ("weakref", attribs))
3543 : : /* Weakref symbols might have a null address unless their referent
3544 : : is known not to. Don't bother following weakref targets here. */
3545 : : return false;
3546 : :
3547 : : return true;
3548 : : }
3549 : :
3550 : : /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
3551 : : or for an `if' or `while' statement or ?..: exp. It should already
3552 : : have been validated to be of suitable type; otherwise, a bad
3553 : : diagnostic may result.
3554 : :
3555 : : The EXPR is located at LOCATION.
3556 : :
3557 : : This preparation consists of taking the ordinary
3558 : : representation of an expression expr and producing a valid tree
3559 : : boolean expression describing whether expr is nonzero. We could
3560 : : simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
3561 : : but we optimize comparisons, &&, ||, and !.
3562 : :
3563 : : The resulting type should always be `truthvalue_type_node'. */
3564 : :
3565 : : tree
3566 : 10368408 : c_common_truthvalue_conversion (location_t location, tree expr)
3567 : : {
3568 : 11164638 : STRIP_ANY_LOCATION_WRAPPER (expr);
3569 : 11164638 : switch (TREE_CODE (expr))
3570 : : {
3571 : 2439384 : case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
3572 : 2439384 : case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
3573 : 2439384 : case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
3574 : 2439384 : case ORDERED_EXPR: case UNORDERED_EXPR:
3575 : 2439384 : if (TREE_TYPE (expr) == truthvalue_type_node)
3576 : : return expr;
3577 : 392446 : expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3578 : 392446 : TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
3579 : 392446 : goto ret;
3580 : :
3581 : 587658 : case TRUTH_ANDIF_EXPR:
3582 : 587658 : case TRUTH_ORIF_EXPR:
3583 : 587658 : case TRUTH_AND_EXPR:
3584 : 587658 : case TRUTH_OR_EXPR:
3585 : 587658 : case TRUTH_XOR_EXPR:
3586 : 587658 : if (TREE_TYPE (expr) == truthvalue_type_node)
3587 : : return expr;
3588 : 74 : expr = build2 (TREE_CODE (expr), truthvalue_type_node,
3589 : : c_common_truthvalue_conversion (location,
3590 : 37 : TREE_OPERAND (expr, 0)),
3591 : : c_common_truthvalue_conversion (location,
3592 : 37 : TREE_OPERAND (expr, 1)));
3593 : 37 : goto ret;
3594 : :
3595 : 318292 : case TRUTH_NOT_EXPR:
3596 : 318292 : if (TREE_TYPE (expr) == truthvalue_type_node)
3597 : : return expr;
3598 : 315353 : expr = build1 (TREE_CODE (expr), truthvalue_type_node,
3599 : : c_common_truthvalue_conversion (location,
3600 : 315353 : TREE_OPERAND (expr, 0)));
3601 : 315353 : goto ret;
3602 : :
3603 : : case ERROR_MARK:
3604 : : return expr;
3605 : :
3606 : 2569747 : case INTEGER_CST:
3607 : 2569747 : if (TREE_CODE (TREE_TYPE (expr)) == ENUMERAL_TYPE
3608 : 751757 : && !integer_zerop (expr)
3609 : 3043048 : && !integer_onep (expr))
3610 : 890 : warning_at (location, OPT_Wint_in_bool_context,
3611 : : "enum constant in boolean context");
3612 : 2569747 : return integer_zerop (expr) ? truthvalue_false_node
3613 : 2569747 : : truthvalue_true_node;
3614 : :
3615 : 1288 : case REAL_CST:
3616 : 1288 : return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
3617 : 1288 : ? truthvalue_true_node
3618 : 1288 : : truthvalue_false_node;
3619 : :
3620 : 0 : case FIXED_CST:
3621 : 0 : return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
3622 : 0 : &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
3623 : 0 : ? truthvalue_true_node
3624 : 0 : : truthvalue_false_node;
3625 : :
3626 : 0 : case FUNCTION_DECL:
3627 : 0 : expr = build_unary_op (location, ADDR_EXPR, expr, false);
3628 : : /* Fall through. */
3629 : :
3630 : 579 : case ADDR_EXPR:
3631 : 579 : {
3632 : 579 : tree inner = TREE_OPERAND (expr, 0);
3633 : 579 : if (decl_with_nonnull_addr_p (inner)
3634 : : /* Check both EXPR and INNER for suppression. */
3635 : 127 : && !warning_suppressed_p (expr, OPT_Waddress)
3636 : 692 : && !warning_suppressed_p (inner, OPT_Waddress))
3637 : : {
3638 : : /* Common Ada programmer's mistake. */
3639 : 107 : warning_at (location,
3640 : 107 : OPT_Waddress,
3641 : : "the address of %qD will always evaluate as %<true%>",
3642 : : inner);
3643 : 107 : suppress_warning (inner, OPT_Waddress);
3644 : 107 : return truthvalue_true_node;
3645 : : }
3646 : : break;
3647 : : }
3648 : :
3649 : 578 : case COMPLEX_EXPR:
3650 : 1734 : expr = build_binary_op (EXPR_LOCATION (expr),
3651 : 578 : (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3652 : : ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3653 : : c_common_truthvalue_conversion (location,
3654 : 578 : TREE_OPERAND (expr, 0)),
3655 : : c_common_truthvalue_conversion (location,
3656 : 578 : TREE_OPERAND (expr, 1)),
3657 : : false);
3658 : 578 : goto ret;
3659 : :
3660 : 74 : case NEGATE_EXPR:
3661 : 74 : case ABS_EXPR:
3662 : 74 : case ABSU_EXPR:
3663 : 74 : case FLOAT_EXPR:
3664 : 74 : case EXCESS_PRECISION_EXPR:
3665 : : /* These don't change whether an object is nonzero or zero. */
3666 : 74 : return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
3667 : :
3668 : 0 : case LROTATE_EXPR:
3669 : 0 : case RROTATE_EXPR:
3670 : : /* These don't change whether an object is zero or nonzero, but
3671 : : we can't ignore them if their second arg has side-effects. */
3672 : 0 : if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
3673 : : {
3674 : 0 : expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
3675 : 0 : TREE_OPERAND (expr, 1),
3676 : : c_common_truthvalue_conversion
3677 : 0 : (location, TREE_OPERAND (expr, 0)));
3678 : 0 : goto ret;
3679 : : }
3680 : : else
3681 : 0 : return c_common_truthvalue_conversion (location,
3682 : 0 : TREE_OPERAND (expr, 0));
3683 : :
3684 : 502 : case MULT_EXPR:
3685 : 502 : warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3686 : : "%<*%> in boolean context, suggest %<&&%> instead");
3687 : 502 : break;
3688 : :
3689 : 166 : case LSHIFT_EXPR:
3690 : : /* We will only warn on signed shifts here, because the majority of
3691 : : false positive warnings happen in code where unsigned arithmetic
3692 : : was used in anticipation of a possible overflow.
3693 : : Furthermore, if we see an unsigned type here we know that the
3694 : : result of the shift is not subject to integer promotion rules. */
3695 : 166 : if ((TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
3696 : 1 : || TREE_CODE (TREE_TYPE (expr)) == BITINT_TYPE)
3697 : 167 : && !TYPE_UNSIGNED (TREE_TYPE (expr)))
3698 : 150 : warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3699 : : "%<<<%> in boolean context, did you mean %<<%>?");
3700 : : break;
3701 : :
3702 : 34637 : case COND_EXPR:
3703 : 34637 : if (warn_int_in_bool_context
3704 : 36113 : && !from_macro_definition_at (EXPR_LOCATION (expr)))
3705 : : {
3706 : 725 : tree val1 = fold_for_warn (TREE_OPERAND (expr, 1));
3707 : 725 : tree val2 = fold_for_warn (TREE_OPERAND (expr, 2));
3708 : 725 : if (TREE_CODE (val1) == INTEGER_CST
3709 : 78 : && TREE_CODE (val2) == INTEGER_CST
3710 : 78 : && !integer_zerop (val1)
3711 : 70 : && !integer_zerop (val2)
3712 : 741 : && (!integer_onep (val1)
3713 : 12 : || !integer_onep (val2)))
3714 : 12 : warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3715 : : "%<?:%> using integer constants in boolean context, "
3716 : : "the expression will always evaluate to %<true%>");
3717 : 713 : else if ((TREE_CODE (val1) == INTEGER_CST
3718 : 66 : && !integer_zerop (val1)
3719 : 58 : && !integer_onep (val1))
3720 : 779 : || (TREE_CODE (val2) == INTEGER_CST
3721 : 316 : && !integer_zerop (val2)
3722 : 258 : && !integer_onep (val2)))
3723 : 4 : warning_at (EXPR_LOCATION (expr), OPT_Wint_in_bool_context,
3724 : : "%<?:%> using integer constants in boolean context");
3725 : : }
3726 : : /* Distribute the conversion into the arms of a COND_EXPR. */
3727 : 34637 : if (c_dialect_cxx ())
3728 : : /* Avoid premature folding. */
3729 : : break;
3730 : : else
3731 : : {
3732 : 10066 : int w = warn_int_in_bool_context;
3733 : 10066 : warn_int_in_bool_context = 0;
3734 : : /* Folding will happen later for C. */
3735 : 30198 : expr = build3 (COND_EXPR, truthvalue_type_node,
3736 : 10066 : TREE_OPERAND (expr, 0),
3737 : : c_common_truthvalue_conversion (location,
3738 : 10066 : TREE_OPERAND (expr, 1)),
3739 : : c_common_truthvalue_conversion (location,
3740 : 10066 : TREE_OPERAND (expr, 2)));
3741 : 10066 : warn_int_in_bool_context = w;
3742 : 10066 : goto ret;
3743 : : }
3744 : :
3745 : 582805 : CASE_CONVERT:
3746 : 582805 : {
3747 : 582805 : tree totype = TREE_TYPE (expr);
3748 : 582805 : tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
3749 : :
3750 : 582805 : if (POINTER_TYPE_P (totype)
3751 : 2016 : && !c_inhibit_evaluation_warnings
3752 : 1848 : && TREE_CODE (fromtype) == REFERENCE_TYPE)
3753 : : {
3754 : 0 : tree inner = expr;
3755 : 0 : STRIP_NOPS (inner);
3756 : :
3757 : 0 : if (DECL_P (inner))
3758 : 0 : warning_at (location,
3759 : 0 : OPT_Waddress,
3760 : : "the compiler can assume that the address of "
3761 : : "%qD will always evaluate to %<true%>",
3762 : : inner);
3763 : : }
3764 : :
3765 : : /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
3766 : : since that affects how `default_conversion' will behave. */
3767 : 582805 : if (TREE_CODE (totype) == REFERENCE_TYPE
3768 : 582805 : || TREE_CODE (fromtype) == REFERENCE_TYPE)
3769 : : break;
3770 : : /* Don't strip a conversion from C++0x scoped enum, since they
3771 : : don't implicitly convert to other types. */
3772 : 582805 : if (TREE_CODE (fromtype) == ENUMERAL_TYPE
3773 : 582805 : && ENUM_IS_SCOPED (fromtype))
3774 : : break;
3775 : : /* If this isn't narrowing the argument, we can ignore it. */
3776 : 582767 : if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
3777 : : {
3778 : 565451 : tree op0 = TREE_OPERAND (expr, 0);
3779 : 565451 : if ((TREE_CODE (fromtype) == POINTER_TYPE
3780 : 1932 : && (TREE_CODE (totype) == INTEGER_TYPE
3781 : 1932 : || TREE_CODE (totype) == BITINT_TYPE))
3782 : 567351 : || warning_suppressed_p (expr, OPT_Waddress))
3783 : : /* Suppress -Waddress for casts to intptr_t, propagating
3784 : : any suppression from the enclosing expression to its
3785 : : operand. */
3786 : 44 : suppress_warning (op0, OPT_Waddress);
3787 : 565451 : return c_common_truthvalue_conversion (location, op0);
3788 : : }
3789 : : }
3790 : : break;
3791 : :
3792 : 8847 : case MODIFY_EXPR:
3793 : 8847 : if (!warning_suppressed_p (expr, OPT_Wparentheses)
3794 : 292 : && warn_parentheses
3795 : 9139 : && warning_at (location, OPT_Wparentheses,
3796 : : "suggest parentheses around assignment used as "
3797 : : "truth value"))
3798 : 14 : suppress_warning (expr, OPT_Wparentheses);
3799 : : break;
3800 : :
3801 : 230722 : case CONST_DECL:
3802 : 230722 : {
3803 : 230722 : tree folded_expr = fold_for_warn (expr);
3804 : 230722 : if (folded_expr != expr)
3805 : : return c_common_truthvalue_conversion (location, folded_expr);
3806 : : }
3807 : : break;
3808 : :
3809 : : default:
3810 : : break;
3811 : : }
3812 : :
3813 : 4441065 : if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
3814 : : {
3815 : 405 : tree t = save_expr (expr);
3816 : 405 : expr = (build_binary_op
3817 : 810 : (EXPR_LOCATION (expr),
3818 : 405 : (TREE_SIDE_EFFECTS (expr)
3819 : : ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
3820 : : c_common_truthvalue_conversion
3821 : : (location,
3822 : : build_unary_op (location, REALPART_EXPR, t, false)),
3823 : : c_common_truthvalue_conversion
3824 : : (location,
3825 : : build_unary_op (location, IMAGPART_EXPR, t, false)),
3826 : : false));
3827 : 405 : goto ret;
3828 : : }
3829 : :
3830 : 4440660 : if (FIXED_POINT_TYPE_P (TREE_TYPE (expr)))
3831 : : {
3832 : 0 : tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
3833 : 0 : FCONST0 (TYPE_MODE
3834 : : (TREE_TYPE (expr))));
3835 : 0 : return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, true);
3836 : : }
3837 : : else
3838 : 4440660 : return build_binary_op (location, NE_EXPR, expr, integer_zero_node, true);
3839 : :
3840 : 718885 : ret:
3841 : 718885 : protected_set_expr_location (expr, location);
3842 : 718885 : return expr;
3843 : : }
3844 : :
3845 : : static void def_builtin_1 (enum built_in_function fncode,
3846 : : const char *name,
3847 : : enum built_in_class fnclass,
3848 : : tree fntype, tree libtype,
3849 : : bool both_p, bool fallback_p, bool nonansi_p,
3850 : : tree fnattrs, bool implicit_p);
3851 : :
3852 : :
3853 : : /* Apply the TYPE_QUALS to the new DECL. */
3854 : :
3855 : : void
3856 : 931694751 : c_apply_type_quals_to_decl (int type_quals, tree decl)
3857 : : {
3858 : 931694751 : tree type = TREE_TYPE (decl);
3859 : :
3860 : 931694751 : if (type == error_mark_node)
3861 : : return;
3862 : :
3863 : 931694528 : if ((type_quals & TYPE_QUAL_CONST)
3864 : 601872566 : || (type && TREE_CODE (type) == REFERENCE_TYPE))
3865 : : /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
3866 : : constructor can produce constant init, so rely on cp_finish_decl to
3867 : : clear TREE_READONLY if the variable has non-constant init. */
3868 : 417205541 : TREE_READONLY (decl) = 1;
3869 : 931694528 : if (type_quals & TYPE_QUAL_VOLATILE)
3870 : : {
3871 : 134456 : TREE_SIDE_EFFECTS (decl) = 1;
3872 : 134456 : TREE_THIS_VOLATILE (decl) = 1;
3873 : : }
3874 : 931694528 : if (type_quals & TYPE_QUAL_RESTRICT)
3875 : : {
3876 : 10690664 : while (type && TREE_CODE (type) == ARRAY_TYPE)
3877 : : /* Allow 'restrict' on arrays of pointers.
3878 : : FIXME currently we just ignore it. */
3879 : 7 : type = TREE_TYPE (type);
3880 : 10690657 : if (!type
3881 : 10690657 : || !POINTER_TYPE_P (type)
3882 : 21381312 : || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
3883 : 3 : error ("invalid use of %<restrict%>");
3884 : : }
3885 : : }
3886 : :
3887 : : /* Return the typed-based alias set for T, which may be an expression
3888 : : or a type. Return -1 if we don't do anything special. */
3889 : :
3890 : : alias_set_type
3891 : 638326514 : c_common_get_alias_set (tree t)
3892 : : {
3893 : : /* For VLAs, use the alias set of the element type rather than the
3894 : : default of alias set 0 for types compared structurally. */
3895 : 638326514 : if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
3896 : : {
3897 : 765838 : if (TREE_CODE (t) == ARRAY_TYPE)
3898 : 428257 : return get_alias_set (TREE_TYPE (t));
3899 : : return -1;
3900 : : }
3901 : :
3902 : : /* That's all the expressions we handle specially. */
3903 : 637560676 : if (!TYPE_P (t))
3904 : : return -1;
3905 : :
3906 : : /* Unlike char, char8_t doesn't alias in C++. (In C, char8_t is not
3907 : : a distinct type.) */
3908 : 104349842 : if (flag_char8_t && t == char8_type_node && c_dialect_cxx ())
3909 : : return -1;
3910 : :
3911 : : /* The C standard guarantees that any object may be accessed via an
3912 : : lvalue that has narrow character type. */
3913 : 104349773 : if (t == char_type_node
3914 : 70976349 : || t == signed_char_type_node
3915 : 66965742 : || t == unsigned_char_type_node)
3916 : : return 0;
3917 : :
3918 : : /* The C standard specifically allows aliasing between signed and
3919 : : unsigned variants of the same type. We treat the signed
3920 : : variant as canonical. */
3921 : 55369308 : if ((TREE_CODE (t) == INTEGER_TYPE || TREE_CODE (t) == BITINT_TYPE)
3922 : 55369308 : && TYPE_UNSIGNED (t))
3923 : : {
3924 : 53768138 : tree t1 = c_common_signed_type (t);
3925 : :
3926 : : /* t1 == t can happen for boolean nodes which are always unsigned. */
3927 : 53768138 : if (t1 != t)
3928 : 53768138 : return get_alias_set (t1);
3929 : : }
3930 : :
3931 : : return -1;
3932 : : }
3933 : :
3934 : : /* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
3935 : : the IS_SIZEOF parameter indicates which operator is being applied.
3936 : : The COMPLAIN flag controls whether we should diagnose possibly
3937 : : ill-formed constructs or not. LOC is the location of the SIZEOF or
3938 : : TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
3939 : : a type in any context should be returned, rather than the normal
3940 : : alignment for that type. */
3941 : :
3942 : : tree
3943 : 18968257 : c_sizeof_or_alignof_type (location_t loc,
3944 : : tree type, bool is_sizeof, bool min_alignof,
3945 : : int complain)
3946 : : {
3947 : 18968257 : const char *op_name;
3948 : 18968257 : tree value = NULL;
3949 : 18968257 : enum tree_code type_code = TREE_CODE (type);
3950 : :
3951 : 18968257 : op_name = is_sizeof ? "sizeof" : "__alignof__";
3952 : :
3953 : 18968257 : if (type_code == FUNCTION_TYPE)
3954 : : {
3955 : 296 : if (is_sizeof)
3956 : : {
3957 : 290 : if (complain && warn_pointer_arith)
3958 : 28 : pedwarn (loc, OPT_Wpointer_arith,
3959 : : "invalid application of %<sizeof%> to a function type");
3960 : : else if (!complain)
3961 : 252 : return error_mark_node;
3962 : 38 : value = size_one_node;
3963 : : }
3964 : : else
3965 : : {
3966 : 6 : if (complain)
3967 : : {
3968 : 6 : if (c_dialect_cxx ())
3969 : 4 : pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
3970 : : "%<alignof%> applied to a function type");
3971 : : else
3972 : 2 : pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
3973 : : "%<_Alignof%> applied to a function type");
3974 : : }
3975 : 6 : value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
3976 : : }
3977 : : }
3978 : 18967961 : else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
3979 : : {
3980 : 1670 : if (type_code == VOID_TYPE
3981 : 1670 : && complain && warn_pointer_arith)
3982 : 19 : pedwarn (loc, OPT_Wpointer_arith,
3983 : : "invalid application of %qs to a void type", op_name);
3984 : 1651 : else if (!complain)
3985 : 1621 : return error_mark_node;
3986 : 49 : value = size_one_node;
3987 : : }
3988 : 18966291 : else if (!COMPLETE_TYPE_P (type)
3989 : 18966291 : && ((!c_dialect_cxx () && !flag_isoc2y)
3990 : : || is_sizeof
3991 : 581 : || type_code != ARRAY_TYPE))
3992 : : {
3993 : 708 : if (complain)
3994 : 194 : error_at (loc, "invalid application of %qs to incomplete type %qT",
3995 : : op_name, type);
3996 : 708 : return error_mark_node;
3997 : : }
3998 : 18287795 : else if (c_dialect_cxx () && type_code == ARRAY_TYPE
3999 : 19406591 : && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4000 : : {
4001 : 0 : if (complain)
4002 : 0 : error_at (loc, "invalid application of %qs to array type %qT of "
4003 : : "incomplete element type", op_name, type);
4004 : 0 : return error_mark_node;
4005 : : }
4006 : 19890745 : else if (!verify_type_context (loc, is_sizeof ? TCTX_SIZEOF : TCTX_ALIGNOF,
4007 : : type, !complain))
4008 : : {
4009 : 0 : if (!complain)
4010 : 0 : return error_mark_node;
4011 : 0 : value = size_one_node;
4012 : : }
4013 : : else
4014 : : {
4015 : 18965583 : if (is_sizeof)
4016 : : /* Convert in case a char is more than one unit. */
4017 : 18040421 : value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4018 : 18040421 : size_int (TYPE_PRECISION (char_type_node)
4019 : : / BITS_PER_UNIT));
4020 : 925162 : else if (min_alignof)
4021 : 719406 : value = size_int (min_align_of_type (type));
4022 : : else
4023 : 205756 : value = size_int (TYPE_ALIGN_UNIT (type));
4024 : : }
4025 : :
4026 : : /* VALUE will have the middle-end integer type sizetype.
4027 : : However, we should really return a value of type `size_t',
4028 : : which is just a typedef for an ordinary integer type. */
4029 : 18965676 : value = fold_convert_loc (loc, size_type_node, value);
4030 : :
4031 : 18965676 : return value;
4032 : : }
4033 : :
4034 : : /* Implement the __alignof keyword: Return the minimum required
4035 : : alignment of EXPR, measured in bytes. For VAR_DECLs,
4036 : : FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
4037 : : from an "aligned" __attribute__ specification). LOC is the
4038 : : location of the ALIGNOF operator. */
4039 : :
4040 : : tree
4041 : 40791 : c_alignof_expr (location_t loc, tree expr)
4042 : : {
4043 : 40791 : tree t;
4044 : :
4045 : 40791 : if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (expr)))
4046 : 0 : t = size_one_node;
4047 : :
4048 : 40791 : else if (VAR_OR_FUNCTION_DECL_P (expr))
4049 : 7245 : t = size_int (DECL_ALIGN_UNIT (expr));
4050 : :
4051 : 33546 : else if (TREE_CODE (expr) == COMPONENT_REF
4052 : 33546 : && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
4053 : : {
4054 : 1 : error_at (loc, "%<__alignof%> applied to a bit-field");
4055 : 1 : t = size_one_node;
4056 : : }
4057 : 33545 : else if (TREE_CODE (expr) == COMPONENT_REF
4058 : 33545 : && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
4059 : 17569 : t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
4060 : :
4061 : 15976 : else if (INDIRECT_REF_P (expr))
4062 : : {
4063 : 55 : tree t = TREE_OPERAND (expr, 0);
4064 : 55 : tree best = t;
4065 : 55 : int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4066 : :
4067 : 55 : while (CONVERT_EXPR_P (t)
4068 : 55 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
4069 : : {
4070 : 0 : int thisalign;
4071 : :
4072 : 0 : t = TREE_OPERAND (t, 0);
4073 : 0 : thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
4074 : 0 : if (thisalign > bestalign)
4075 : 0 : best = t, bestalign = thisalign;
4076 : : }
4077 : 55 : return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
4078 : : }
4079 : : else
4080 : 15921 : return c_alignof (loc, TREE_TYPE (expr));
4081 : :
4082 : 24815 : return fold_convert_loc (loc, size_type_node, t);
4083 : : }
4084 : :
4085 : : /* Implement the _Countof keyword:
4086 : : Return the number of elements of an array. */
4087 : :
4088 : : tree
4089 : 70 : c_countof_type (location_t loc, tree type)
4090 : : {
4091 : 70 : enum tree_code type_code;
4092 : :
4093 : 70 : type_code = TREE_CODE (type);
4094 : 70 : if (type_code != ARRAY_TYPE)
4095 : : {
4096 : 11 : error_at (loc, "invalid application of %<_Countof%> to type %qT", type);
4097 : 11 : return error_mark_node;
4098 : : }
4099 : 59 : if (!COMPLETE_TYPE_P (type))
4100 : : {
4101 : 2 : error_at (loc,
4102 : : "invalid application of %<_Countof%> to incomplete type %qT",
4103 : : type);
4104 : 2 : return error_mark_node;
4105 : : }
4106 : :
4107 : 57 : return array_type_nelts_top (type);
4108 : : }
4109 : :
4110 : : /* Handle C and C++ default attributes. */
4111 : :
4112 : : enum built_in_attribute
4113 : : {
4114 : : #define DEF_ATTR_NULL_TREE(ENUM) ENUM,
4115 : : #define DEF_ATTR_INT(ENUM, VALUE) ENUM,
4116 : : #define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
4117 : : #define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
4118 : : #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
4119 : : #include "builtin-attrs.def"
4120 : : #undef DEF_ATTR_NULL_TREE
4121 : : #undef DEF_ATTR_INT
4122 : : #undef DEF_ATTR_STRING
4123 : : #undef DEF_ATTR_IDENT
4124 : : #undef DEF_ATTR_TREE_LIST
4125 : : ATTR_LAST
4126 : : };
4127 : :
4128 : : static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
4129 : :
4130 : : static void c_init_attributes (void);
4131 : :
4132 : : enum c_builtin_type
4133 : : {
4134 : : #define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
4135 : : #define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
4136 : : #define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
4137 : : #define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
4138 : : #define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4139 : : #define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4140 : : #define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
4141 : : #define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4142 : : ARG6) NAME,
4143 : : #define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4144 : : ARG6, ARG7) NAME,
4145 : : #define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4146 : : ARG6, ARG7, ARG8) NAME,
4147 : : #define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4148 : : ARG6, ARG7, ARG8, ARG9) NAME,
4149 : : #define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4150 : : ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
4151 : : #define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4152 : : ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
4153 : : #define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
4154 : : #define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
4155 : : #define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
4156 : : #define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
4157 : : #define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
4158 : : #define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4159 : : NAME,
4160 : : #define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4161 : : ARG6) NAME,
4162 : : #define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4163 : : ARG6, ARG7) NAME,
4164 : : #define DEF_POINTER_TYPE(NAME, TYPE) NAME,
4165 : : #include "builtin-types.def"
4166 : : #undef DEF_PRIMITIVE_TYPE
4167 : : #undef DEF_FUNCTION_TYPE_0
4168 : : #undef DEF_FUNCTION_TYPE_1
4169 : : #undef DEF_FUNCTION_TYPE_2
4170 : : #undef DEF_FUNCTION_TYPE_3
4171 : : #undef DEF_FUNCTION_TYPE_4
4172 : : #undef DEF_FUNCTION_TYPE_5
4173 : : #undef DEF_FUNCTION_TYPE_6
4174 : : #undef DEF_FUNCTION_TYPE_7
4175 : : #undef DEF_FUNCTION_TYPE_8
4176 : : #undef DEF_FUNCTION_TYPE_9
4177 : : #undef DEF_FUNCTION_TYPE_10
4178 : : #undef DEF_FUNCTION_TYPE_11
4179 : : #undef DEF_FUNCTION_TYPE_VAR_0
4180 : : #undef DEF_FUNCTION_TYPE_VAR_1
4181 : : #undef DEF_FUNCTION_TYPE_VAR_2
4182 : : #undef DEF_FUNCTION_TYPE_VAR_3
4183 : : #undef DEF_FUNCTION_TYPE_VAR_4
4184 : : #undef DEF_FUNCTION_TYPE_VAR_5
4185 : : #undef DEF_FUNCTION_TYPE_VAR_6
4186 : : #undef DEF_FUNCTION_TYPE_VAR_7
4187 : : #undef DEF_POINTER_TYPE
4188 : : BT_LAST
4189 : : };
4190 : :
4191 : : typedef enum c_builtin_type builtin_type;
4192 : :
4193 : : /* A temporary array for c_common_nodes_and_builtins. Used in
4194 : : communication with def_fn_type. */
4195 : : static tree builtin_types[(int) BT_LAST + 1];
4196 : :
4197 : : /* A helper function for c_common_nodes_and_builtins. Build function type
4198 : : for DEF with return type RET and N arguments. If VAR is true, then the
4199 : : function should be variadic after those N arguments, or, if N is zero,
4200 : : unprototyped.
4201 : :
4202 : : Takes special care not to ICE if any of the types involved are
4203 : : error_mark_node, which indicates that said type is not in fact available
4204 : : (see builtin_type_for_size). In which case the function type as a whole
4205 : : should be error_mark_node. */
4206 : :
4207 : : static void
4208 : 103837104 : def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
4209 : : {
4210 : 103837104 : tree t;
4211 : 103837104 : tree *args = XALLOCAVEC (tree, n);
4212 : 103837104 : va_list list;
4213 : 103837104 : int i;
4214 : :
4215 : 103837104 : va_start (list, n);
4216 : 328927996 : for (i = 0; i < n; ++i)
4217 : : {
4218 : 228023019 : builtin_type a = (builtin_type) va_arg (list, int);
4219 : 228023019 : t = builtin_types[a];
4220 : 228023019 : if (t == error_mark_node)
4221 : 2932127 : goto egress;
4222 : 225090892 : args[i] = t;
4223 : : }
4224 : :
4225 : 100904977 : t = builtin_types[ret];
4226 : 100904977 : if (t == error_mark_node)
4227 : 417359 : goto egress;
4228 : 100487618 : if (var)
4229 : 6175473 : if (n == 0)
4230 : 824104 : t = build_function_type (t, NULL_TREE);
4231 : : else
4232 : 5351369 : t = build_varargs_function_type_array (t, n, args);
4233 : : else
4234 : 94312145 : t = build_function_type_array (t, n, args);
4235 : :
4236 : 103837104 : egress:
4237 : 103837104 : builtin_types[def] = t;
4238 : 103837104 : va_end (list);
4239 : 103837104 : }
4240 : :
4241 : : /* Build builtin functions common to both C and C++ language
4242 : : frontends. */
4243 : :
4244 : : static void
4245 : 206026 : c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
4246 : : {
4247 : : #define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
4248 : : builtin_types[ENUM] = VALUE;
4249 : : #define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
4250 : : def_fn_type (ENUM, RETURN, 0, 0);
4251 : : #define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
4252 : : def_fn_type (ENUM, RETURN, 0, 1, ARG1);
4253 : : #define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
4254 : : def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
4255 : : #define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4256 : : def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
4257 : : #define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4258 : : def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
4259 : : #define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4260 : : def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4261 : : #define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4262 : : ARG6) \
4263 : : def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
4264 : : #define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4265 : : ARG6, ARG7) \
4266 : : def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
4267 : : #define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4268 : : ARG6, ARG7, ARG8) \
4269 : : def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
4270 : : ARG7, ARG8);
4271 : : #define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4272 : : ARG6, ARG7, ARG8, ARG9) \
4273 : : def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
4274 : : ARG7, ARG8, ARG9);
4275 : : #define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4276 : : ARG6, ARG7, ARG8, ARG9, ARG10) \
4277 : : def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
4278 : : ARG7, ARG8, ARG9, ARG10);
4279 : : #define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4280 : : ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
4281 : : def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
4282 : : ARG7, ARG8, ARG9, ARG10, ARG11);
4283 : : #define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
4284 : : def_fn_type (ENUM, RETURN, 1, 0);
4285 : : #define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
4286 : : def_fn_type (ENUM, RETURN, 1, 1, ARG1);
4287 : : #define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
4288 : : def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
4289 : : #define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
4290 : : def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
4291 : : #define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
4292 : : def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
4293 : : #define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
4294 : : def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
4295 : : #define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4296 : : ARG6) \
4297 : : def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
4298 : : #define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
4299 : : ARG6, ARG7) \
4300 : : def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
4301 : : #define DEF_POINTER_TYPE(ENUM, TYPE) \
4302 : : builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
4303 : :
4304 : : #include "builtin-types.def"
4305 : :
4306 : : #undef DEF_PRIMITIVE_TYPE
4307 : : #undef DEF_FUNCTION_TYPE_0
4308 : : #undef DEF_FUNCTION_TYPE_1
4309 : : #undef DEF_FUNCTION_TYPE_2
4310 : : #undef DEF_FUNCTION_TYPE_3
4311 : : #undef DEF_FUNCTION_TYPE_4
4312 : : #undef DEF_FUNCTION_TYPE_5
4313 : : #undef DEF_FUNCTION_TYPE_6
4314 : : #undef DEF_FUNCTION_TYPE_7
4315 : : #undef DEF_FUNCTION_TYPE_8
4316 : : #undef DEF_FUNCTION_TYPE_9
4317 : : #undef DEF_FUNCTION_TYPE_10
4318 : : #undef DEF_FUNCTION_TYPE_11
4319 : : #undef DEF_FUNCTION_TYPE_VAR_0
4320 : : #undef DEF_FUNCTION_TYPE_VAR_1
4321 : : #undef DEF_FUNCTION_TYPE_VAR_2
4322 : : #undef DEF_FUNCTION_TYPE_VAR_3
4323 : : #undef DEF_FUNCTION_TYPE_VAR_4
4324 : : #undef DEF_FUNCTION_TYPE_VAR_5
4325 : : #undef DEF_FUNCTION_TYPE_VAR_6
4326 : : #undef DEF_FUNCTION_TYPE_VAR_7
4327 : : #undef DEF_POINTER_TYPE
4328 : 206026 : builtin_types[(int) BT_LAST] = NULL_TREE;
4329 : :
4330 : 206026 : c_init_attributes ();
4331 : :
4332 : : #define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
4333 : : NONANSI_P, ATTRS, IMPLICIT, COND) \
4334 : : if (NAME && COND) \
4335 : : def_builtin_1 (ENUM, NAME, CLASS, \
4336 : : builtin_types[(int) TYPE], \
4337 : : builtin_types[(int) LIBTYPE], \
4338 : : BOTH_P, FALLBACK_P, NONANSI_P, \
4339 : : built_in_attributes[(int) ATTRS], IMPLICIT);
4340 : : #include "builtins.def"
4341 : :
4342 : 206026 : targetm.init_builtins ();
4343 : :
4344 : 206026 : build_common_builtin_nodes ();
4345 : 206026 : }
4346 : :
4347 : : /* Like get_identifier, but avoid warnings about null arguments when
4348 : : the argument may be NULL for targets where GCC lacks stdint.h type
4349 : : information. */
4350 : :
4351 : : static inline tree
4352 : 5562702 : c_get_ident (const char *id)
4353 : : {
4354 : 2472312 : return get_identifier (id);
4355 : : }
4356 : :
4357 : : /* Build tree nodes and builtin functions common to both C and C++ language
4358 : : frontends. */
4359 : :
4360 : : void
4361 : 206026 : c_common_nodes_and_builtins (void)
4362 : : {
4363 : 206026 : int char8_type_size;
4364 : 206026 : int char16_type_size;
4365 : 206026 : int char32_type_size;
4366 : 206026 : int wchar_type_size;
4367 : 206026 : tree array_domain_type;
4368 : 206026 : tree va_list_ref_type_node;
4369 : 206026 : tree va_list_arg_type_node;
4370 : 206026 : int i;
4371 : :
4372 : 206026 : build_common_tree_nodes (flag_signed_char);
4373 : :
4374 : : /* Define `int' and `char' first so that dbx will output them first. */
4375 : 206026 : record_builtin_type (RID_INT, NULL, integer_type_node);
4376 : 206026 : record_builtin_type (RID_CHAR, "char", char_type_node);
4377 : :
4378 : : /* `signed' is the same as `int'. FIXME: the declarations of "signed",
4379 : : "unsigned long", "long long unsigned" and "unsigned short" were in C++
4380 : : but not C. Are the conditionals here needed? */
4381 : 206026 : if (c_dialect_cxx ())
4382 : 95036 : record_builtin_type (RID_SIGNED, NULL, integer_type_node);
4383 : 206026 : record_builtin_type (RID_LONG, "long int", long_integer_type_node);
4384 : 206026 : record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
4385 : 206026 : record_builtin_type (RID_MAX, "long unsigned int",
4386 : : long_unsigned_type_node);
4387 : :
4388 : 412052 : for (i = 0; i < NUM_INT_N_ENTS; i ++)
4389 : : {
4390 : 206026 : char name[25];
4391 : :
4392 : 206026 : sprintf (name, "__int%d", int_n_data[i].bitsize);
4393 : 206026 : record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
4394 : : int_n_trees[i].signed_type);
4395 : 206026 : sprintf (name, "__int%d__", int_n_data[i].bitsize);
4396 : 206026 : record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
4397 : : int_n_trees[i].signed_type);
4398 : 412052 : ridpointers[RID_FIRST_INT_N + i]
4399 : 206026 : = DECL_NAME (TYPE_NAME (int_n_trees[i].signed_type));
4400 : :
4401 : 206026 : sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
4402 : 206026 : record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
4403 : 206026 : sprintf (name, "__int%d__ unsigned", int_n_data[i].bitsize);
4404 : 206026 : record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
4405 : : }
4406 : :
4407 : 206026 : if (c_dialect_cxx ())
4408 : 95036 : record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
4409 : 206026 : record_builtin_type (RID_MAX, "long long int",
4410 : : long_long_integer_type_node);
4411 : 206026 : record_builtin_type (RID_MAX, "long long unsigned int",
4412 : : long_long_unsigned_type_node);
4413 : 206026 : if (c_dialect_cxx ())
4414 : 95036 : record_builtin_type (RID_MAX, "long long unsigned",
4415 : : long_long_unsigned_type_node);
4416 : 206026 : record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
4417 : 206026 : record_builtin_type (RID_MAX, "short unsigned int",
4418 : : short_unsigned_type_node);
4419 : 206026 : if (c_dialect_cxx ())
4420 : 95036 : record_builtin_type (RID_MAX, "unsigned short",
4421 : : short_unsigned_type_node);
4422 : :
4423 : : /* Define both `signed char' and `unsigned char'. */
4424 : 206026 : record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
4425 : 206026 : record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
4426 : :
4427 : : /* These are types that c_common_type_for_size and
4428 : : c_common_type_for_mode use. */
4429 : 206026 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4430 : : TYPE_DECL, NULL_TREE,
4431 : : intQI_type_node));
4432 : 206026 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4433 : : TYPE_DECL, NULL_TREE,
4434 : : intHI_type_node));
4435 : 206026 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4436 : : TYPE_DECL, NULL_TREE,
4437 : : intSI_type_node));
4438 : 206026 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4439 : : TYPE_DECL, NULL_TREE,
4440 : : intDI_type_node));
4441 : : #if HOST_BITS_PER_WIDE_INT >= 64
4442 : : /* Note that this is different than the __int128 type that's part of
4443 : : the generic __intN support. */
4444 : 206026 : if (targetm.scalar_mode_supported_p (TImode))
4445 : 200719 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4446 : : TYPE_DECL,
4447 : : get_identifier ("__int128_t"),
4448 : : intTI_type_node));
4449 : : #endif
4450 : 206026 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4451 : : TYPE_DECL, NULL_TREE,
4452 : : unsigned_intQI_type_node));
4453 : 206026 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4454 : : TYPE_DECL, NULL_TREE,
4455 : : unsigned_intHI_type_node));
4456 : 206026 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4457 : : TYPE_DECL, NULL_TREE,
4458 : : unsigned_intSI_type_node));
4459 : 206026 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4460 : : TYPE_DECL, NULL_TREE,
4461 : : unsigned_intDI_type_node));
4462 : : #if HOST_BITS_PER_WIDE_INT >= 64
4463 : 206026 : if (targetm.scalar_mode_supported_p (TImode))
4464 : 200719 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4465 : : TYPE_DECL,
4466 : : get_identifier ("__uint128_t"),
4467 : : unsigned_intTI_type_node));
4468 : : #endif
4469 : :
4470 : : /* Create the widest literal types. */
4471 : 206026 : if (targetm.scalar_mode_supported_p (TImode))
4472 : : {
4473 : 200719 : widest_integer_literal_type_node = intTI_type_node;
4474 : 200719 : widest_unsigned_literal_type_node = unsigned_intTI_type_node;
4475 : : }
4476 : : else
4477 : : {
4478 : 5307 : widest_integer_literal_type_node = intDI_type_node;
4479 : 5307 : widest_unsigned_literal_type_node = unsigned_intDI_type_node;
4480 : : }
4481 : :
4482 : 206026 : signed_size_type_node = c_common_signed_type (size_type_node);
4483 : :
4484 : 412052 : pid_type_node =
4485 : 206026 : TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
4486 : :
4487 : 206026 : record_builtin_type (RID_FLOAT, NULL, float_type_node);
4488 : 206026 : record_builtin_type (RID_DOUBLE, NULL, double_type_node);
4489 : 206026 : record_builtin_type (RID_MAX, "long double", long_double_type_node);
4490 : :
4491 : 1854234 : for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4492 : : {
4493 : 1442182 : if (FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4494 : 1236156 : record_builtin_type ((enum rid) (RID_FLOATN_NX_FIRST + i), NULL,
4495 : : FLOATN_NX_TYPE_NODE (i));
4496 : : }
4497 : :
4498 : : /* For C, let float128t_type_node (__float128 in some backends) be the
4499 : : same type as float128_type_node (_Float128), for C++ let those
4500 : : be distinct types that mangle and behave differently. */
4501 : 206026 : if (c_dialect_cxx ())
4502 : 95036 : float128t_type_node = NULL_TREE;
4503 : :
4504 : : /* Only supported decimal floating point extension if the target
4505 : : actually supports underlying modes. */
4506 : 206026 : if (targetm.scalar_mode_supported_p (SDmode)
4507 : 206026 : && targetm.scalar_mode_supported_p (DDmode)
4508 : 412052 : && targetm.scalar_mode_supported_p (TDmode))
4509 : : {
4510 : 206026 : record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
4511 : 206026 : record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
4512 : 206026 : record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
4513 : 206026 : record_builtin_type (RID_DFLOAT64X, NULL, dfloat64x_type_node);
4514 : : }
4515 : :
4516 : 206026 : if (targetm.fixed_point_supported_p ())
4517 : : {
4518 : 0 : record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
4519 : 0 : record_builtin_type (RID_FRACT, NULL, fract_type_node);
4520 : 0 : record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
4521 : 0 : record_builtin_type (RID_MAX, "long long _Fract",
4522 : : long_long_fract_type_node);
4523 : 0 : record_builtin_type (RID_MAX, "unsigned short _Fract",
4524 : : unsigned_short_fract_type_node);
4525 : 0 : record_builtin_type (RID_MAX, "unsigned _Fract",
4526 : : unsigned_fract_type_node);
4527 : 0 : record_builtin_type (RID_MAX, "unsigned long _Fract",
4528 : : unsigned_long_fract_type_node);
4529 : 0 : record_builtin_type (RID_MAX, "unsigned long long _Fract",
4530 : : unsigned_long_long_fract_type_node);
4531 : 0 : record_builtin_type (RID_MAX, "_Sat short _Fract",
4532 : : sat_short_fract_type_node);
4533 : 0 : record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
4534 : 0 : record_builtin_type (RID_MAX, "_Sat long _Fract",
4535 : : sat_long_fract_type_node);
4536 : 0 : record_builtin_type (RID_MAX, "_Sat long long _Fract",
4537 : : sat_long_long_fract_type_node);
4538 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
4539 : : sat_unsigned_short_fract_type_node);
4540 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
4541 : : sat_unsigned_fract_type_node);
4542 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
4543 : : sat_unsigned_long_fract_type_node);
4544 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
4545 : : sat_unsigned_long_long_fract_type_node);
4546 : 0 : record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
4547 : 0 : record_builtin_type (RID_ACCUM, NULL, accum_type_node);
4548 : 0 : record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
4549 : 0 : record_builtin_type (RID_MAX, "long long _Accum",
4550 : : long_long_accum_type_node);
4551 : 0 : record_builtin_type (RID_MAX, "unsigned short _Accum",
4552 : : unsigned_short_accum_type_node);
4553 : 0 : record_builtin_type (RID_MAX, "unsigned _Accum",
4554 : : unsigned_accum_type_node);
4555 : 0 : record_builtin_type (RID_MAX, "unsigned long _Accum",
4556 : : unsigned_long_accum_type_node);
4557 : 0 : record_builtin_type (RID_MAX, "unsigned long long _Accum",
4558 : : unsigned_long_long_accum_type_node);
4559 : 0 : record_builtin_type (RID_MAX, "_Sat short _Accum",
4560 : : sat_short_accum_type_node);
4561 : 0 : record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
4562 : 0 : record_builtin_type (RID_MAX, "_Sat long _Accum",
4563 : : sat_long_accum_type_node);
4564 : 0 : record_builtin_type (RID_MAX, "_Sat long long _Accum",
4565 : : sat_long_long_accum_type_node);
4566 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
4567 : : sat_unsigned_short_accum_type_node);
4568 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
4569 : : sat_unsigned_accum_type_node);
4570 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
4571 : : sat_unsigned_long_accum_type_node);
4572 : 0 : record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
4573 : : sat_unsigned_long_long_accum_type_node);
4574 : :
4575 : : }
4576 : :
4577 : 206026 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4578 : : TYPE_DECL,
4579 : : get_identifier ("complex int"),
4580 : : complex_integer_type_node));
4581 : 206026 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4582 : : TYPE_DECL,
4583 : : get_identifier ("complex float"),
4584 : : complex_float_type_node));
4585 : 206026 : lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
4586 : : TYPE_DECL,
4587 : : get_identifier ("complex double"),
4588 : : complex_double_type_node));
4589 : 206026 : lang_hooks.decls.pushdecl
4590 : 206026 : (build_decl (UNKNOWN_LOCATION,
4591 : : TYPE_DECL, get_identifier ("complex long double"),
4592 : : complex_long_double_type_node));
4593 : :
4594 : 206026 : if (!c_dialect_cxx ())
4595 : 887920 : for (i = 0; i < NUM_FLOATN_NX_TYPES; i++)
4596 : 776930 : if (COMPLEX_FLOATN_NX_TYPE_NODE (i) != NULL_TREE)
4597 : : {
4598 : 665940 : char buf[30];
4599 : 1331880 : sprintf (buf, "complex _Float%d%s", floatn_nx_types[i].n,
4600 : 665940 : floatn_nx_types[i].extended ? "x" : "");
4601 : 665940 : lang_hooks.decls.pushdecl
4602 : 665940 : (build_decl (UNKNOWN_LOCATION,
4603 : : TYPE_DECL,
4604 : : get_identifier (buf),
4605 : : COMPLEX_FLOATN_NX_TYPE_NODE (i)));
4606 : : }
4607 : :
4608 : : /* Make fileptr_type_node a distinct void * type until
4609 : : FILE type is defined. Likewise for const struct tm*. */
4610 : 1442182 : for (unsigned i = 0; i < ARRAY_SIZE (builtin_structptr_types); ++i)
4611 : 1236156 : builtin_structptr_types[i].node
4612 : 1236156 : = build_variant_type_copy (builtin_structptr_types[i].base);
4613 : :
4614 : 206026 : record_builtin_type (RID_VOID, NULL, void_type_node);
4615 : :
4616 : : /* Set the TYPE_NAME for any variants that were built before
4617 : : record_builtin_type gave names to the built-in types. */
4618 : 206026 : {
4619 : 206026 : tree void_name = TYPE_NAME (void_type_node);
4620 : 206026 : TYPE_NAME (void_type_node) = NULL_TREE;
4621 : 206026 : TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
4622 : 206026 : = void_name;
4623 : 206026 : TYPE_NAME (void_type_node) = void_name;
4624 : : }
4625 : :
4626 : : /* Make a type to be the domain of a few array types
4627 : : whose domains don't really matter.
4628 : : 200 is small enough that it always fits in size_t
4629 : : and large enough that it can hold most function names for the
4630 : : initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
4631 : 206026 : array_domain_type = build_index_type (size_int (200));
4632 : :
4633 : : /* Make a type for arrays of characters.
4634 : : With luck nothing will ever really depend on the length of this
4635 : : array type. */
4636 : 206026 : char_array_type_node
4637 : 206026 : = build_array_type (char_type_node, array_domain_type);
4638 : :
4639 : 206026 : string_type_node = build_pointer_type (char_type_node);
4640 : 206026 : const_string_type_node
4641 : 206026 : = build_pointer_type (build_qualified_type
4642 : : (char_type_node, TYPE_QUAL_CONST));
4643 : :
4644 : : /* This is special for C++ so functions can be overloaded. */
4645 : 211429 : wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
4646 : 206026 : wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
4647 : 206026 : wchar_type_size = TYPE_PRECISION (wchar_type_node);
4648 : 206026 : underlying_wchar_type_node = wchar_type_node;
4649 : 206026 : if (c_dialect_cxx ())
4650 : : {
4651 : 95036 : if (TYPE_UNSIGNED (wchar_type_node))
4652 : 7 : wchar_type_node = make_unsigned_type (wchar_type_size);
4653 : : else
4654 : 95029 : wchar_type_node = make_signed_type (wchar_type_size);
4655 : 95036 : record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
4656 : : }
4657 : :
4658 : : /* This is for wide string constants. */
4659 : 206026 : wchar_array_type_node
4660 : 206026 : = build_array_type (wchar_type_node, array_domain_type);
4661 : :
4662 : : /* Define 'char8_t'. */
4663 : 206026 : char8_type_node = get_identifier (CHAR8_TYPE);
4664 : 206026 : char8_type_node = TREE_TYPE (identifier_global_value (char8_type_node));
4665 : 206026 : char8_type_size = TYPE_PRECISION (char8_type_node);
4666 : 206026 : if (c_dialect_cxx ())
4667 : : {
4668 : 95036 : char8_type_node = make_unsigned_type (char8_type_size);
4669 : 95036 : TYPE_STRING_FLAG (char8_type_node) = true;
4670 : :
4671 : 95036 : if (flag_char8_t)
4672 : 29979 : record_builtin_type (RID_CHAR8, "char8_t", char8_type_node);
4673 : : }
4674 : :
4675 : : /* This is for UTF-8 string constants. */
4676 : 206026 : char8_array_type_node
4677 : 206026 : = build_array_type (char8_type_node, array_domain_type);
4678 : :
4679 : : /* Define 'char16_t'. */
4680 : 206026 : char16_type_node = get_identifier (CHAR16_TYPE);
4681 : 206026 : char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
4682 : 206026 : char16_type_size = TYPE_PRECISION (char16_type_node);
4683 : 206026 : if (c_dialect_cxx ())
4684 : : {
4685 : 95036 : char16_type_node = make_unsigned_type (char16_type_size);
4686 : :
4687 : 95036 : if (cxx_dialect >= cxx11)
4688 : 81122 : record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
4689 : : }
4690 : :
4691 : : /* This is for UTF-16 string constants. */
4692 : 206026 : char16_array_type_node
4693 : 206026 : = build_array_type (char16_type_node, array_domain_type);
4694 : :
4695 : : /* Define 'char32_t'. */
4696 : 206026 : char32_type_node = get_identifier (CHAR32_TYPE);
4697 : 206026 : char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
4698 : 206026 : char32_type_size = TYPE_PRECISION (char32_type_node);
4699 : 206026 : if (c_dialect_cxx ())
4700 : : {
4701 : 95036 : char32_type_node = make_unsigned_type (char32_type_size);
4702 : :
4703 : 95036 : if (cxx_dialect >= cxx11)
4704 : 81122 : record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
4705 : : }
4706 : :
4707 : : /* This is for UTF-32 string constants. */
4708 : 206026 : char32_array_type_node
4709 : 206026 : = build_array_type (char32_type_node, array_domain_type);
4710 : :
4711 : 206026 : if (strcmp (WINT_TYPE, "wchar_t") == 0)
4712 : : wint_type_node = wchar_type_node;
4713 : : else
4714 : 412052 : wint_type_node =
4715 : 206026 : TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
4716 : :
4717 : 412052 : intmax_type_node =
4718 : 211429 : TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
4719 : 412052 : uintmax_type_node =
4720 : 211429 : TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
4721 : :
4722 : 206026 : if (SIG_ATOMIC_TYPE)
4723 : 206026 : sig_atomic_type_node =
4724 : 206026 : TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
4725 : 206026 : if (INT8_TYPE)
4726 : 206026 : int8_type_node =
4727 : 206026 : TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
4728 : 206026 : if (INT16_TYPE)
4729 : 206026 : int16_type_node =
4730 : 206026 : TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
4731 : 206026 : if (INT32_TYPE)
4732 : 206026 : int32_type_node =
4733 : 206026 : TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
4734 : 206026 : if (INT64_TYPE)
4735 : 412052 : int64_type_node =
4736 : 412052 : TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
4737 : 206026 : if (UINT8_TYPE)
4738 : 206026 : uint8_type_node =
4739 : 206026 : TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
4740 : 206026 : if (UINT16_TYPE)
4741 : 206026 : c_uint16_type_node = uint16_type_node =
4742 : 206026 : TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
4743 : 206026 : if (UINT32_TYPE)
4744 : 206026 : c_uint32_type_node = uint32_type_node =
4745 : 206026 : TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
4746 : 206026 : if (UINT64_TYPE)
4747 : 412052 : c_uint64_type_node = uint64_type_node =
4748 : 412052 : TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
4749 : 206026 : if (INT_LEAST8_TYPE)
4750 : 206026 : int_least8_type_node =
4751 : 206026 : TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
4752 : 206026 : if (INT_LEAST16_TYPE)
4753 : 206026 : int_least16_type_node =
4754 : 206026 : TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
4755 : 206026 : if (INT_LEAST32_TYPE)
4756 : 206026 : int_least32_type_node =
4757 : 206026 : TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
4758 : 206026 : if (INT_LEAST64_TYPE)
4759 : 412052 : int_least64_type_node =
4760 : 412052 : TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
4761 : 206026 : if (UINT_LEAST8_TYPE)
4762 : 206026 : uint_least8_type_node =
4763 : 206026 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
4764 : 206026 : if (UINT_LEAST16_TYPE)
4765 : 206026 : uint_least16_type_node =
4766 : 206026 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
4767 : 206026 : if (UINT_LEAST32_TYPE)
4768 : 206026 : uint_least32_type_node =
4769 : 206026 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
4770 : 206026 : if (UINT_LEAST64_TYPE)
4771 : 412052 : uint_least64_type_node =
4772 : 412052 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
4773 : 206026 : if (INT_FAST8_TYPE)
4774 : 206026 : int_fast8_type_node =
4775 : 206026 : TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
4776 : 206026 : if (INT_FAST16_TYPE)
4777 : 412052 : int_fast16_type_node =
4778 : 412052 : TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
4779 : 206026 : if (INT_FAST32_TYPE)
4780 : 412052 : int_fast32_type_node =
4781 : 412052 : TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
4782 : 206026 : if (INT_FAST64_TYPE)
4783 : 412052 : int_fast64_type_node =
4784 : 412052 : TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
4785 : 206026 : if (UINT_FAST8_TYPE)
4786 : 206026 : uint_fast8_type_node =
4787 : 206026 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
4788 : 206026 : if (UINT_FAST16_TYPE)
4789 : 412052 : uint_fast16_type_node =
4790 : 412052 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
4791 : 206026 : if (UINT_FAST32_TYPE)
4792 : 412052 : uint_fast32_type_node =
4793 : 412052 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
4794 : 206026 : if (UINT_FAST64_TYPE)
4795 : 412052 : uint_fast64_type_node =
4796 : 412052 : TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
4797 : 206026 : if (INTPTR_TYPE)
4798 : 412052 : intptr_type_node =
4799 : 412052 : TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
4800 : 206026 : if (UINTPTR_TYPE)
4801 : 412052 : uintptr_type_node =
4802 : 412052 : TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
4803 : :
4804 : 206026 : default_function_type = build_function_type (integer_type_node, NULL_TREE);
4805 : 206026 : unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
4806 : :
4807 : 206026 : lang_hooks.decls.pushdecl
4808 : 206026 : (build_decl (UNKNOWN_LOCATION,
4809 : : TYPE_DECL, get_identifier ("__builtin_va_list"),
4810 : : va_list_type_node));
4811 : 206026 : if (targetm.enum_va_list_p)
4812 : : {
4813 : : int l;
4814 : : const char *pname;
4815 : : tree ptype;
4816 : :
4817 : 607464 : for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
4818 : : {
4819 : 401438 : lang_hooks.decls.pushdecl
4820 : 401438 : (build_decl (UNKNOWN_LOCATION,
4821 : : TYPE_DECL, get_identifier (pname),
4822 : : ptype));
4823 : :
4824 : : }
4825 : : }
4826 : :
4827 : 206026 : if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
4828 : : {
4829 : 401354 : va_list_arg_type_node = va_list_ref_type_node =
4830 : 200677 : build_pointer_type (TREE_TYPE (va_list_type_node));
4831 : : }
4832 : : else
4833 : : {
4834 : 5349 : va_list_arg_type_node = va_list_type_node;
4835 : 5349 : va_list_ref_type_node = build_reference_type (va_list_type_node);
4836 : : }
4837 : :
4838 : 206026 : c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
4839 : :
4840 : 206026 : main_identifier_node = get_identifier ("main");
4841 : :
4842 : : /* Create the built-in __null node. It is important that this is
4843 : : not shared. */
4844 : 206026 : null_node = make_int_cst (1, 1);
4845 : 211429 : TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
4846 : :
4847 : : /* Create the built-in nullptr node. This part of its initialization is
4848 : : common to C and C++. The front ends can further adjust its definition
4849 : : in {c,cxx}_init_decl_processing. In particular, we aren't setting the
4850 : : alignment here for C++ backward ABI bug compatibility. */
4851 : 206026 : nullptr_type_node = make_node (NULLPTR_TYPE);
4852 : 412052 : TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode));
4853 : 412052 : TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode));
4854 : 206026 : TYPE_UNSIGNED (nullptr_type_node) = 1;
4855 : 412052 : TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode);
4856 : 206026 : SET_TYPE_MODE (nullptr_type_node, ptr_mode);
4857 : 206026 : nullptr_node = build_int_cst (nullptr_type_node, 0);
4858 : :
4859 : : /* Since builtin_types isn't gc'ed, don't export these nodes. */
4860 : 206026 : memset (builtin_types, 0, sizeof (builtin_types));
4861 : 206026 : }
4862 : :
4863 : : /* The number of named compound-literals generated thus far. */
4864 : : static GTY(()) int compound_literal_number;
4865 : :
4866 : : /* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
4867 : :
4868 : : void
4869 : 273 : set_compound_literal_name (tree decl)
4870 : : {
4871 : 273 : char *name;
4872 : 273 : ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
4873 : : compound_literal_number);
4874 : 273 : compound_literal_number++;
4875 : 273 : DECL_NAME (decl) = get_identifier (name);
4876 : 273 : }
4877 : :
4878 : : /* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
4879 : : TYPE and operand OP. */
4880 : :
4881 : : static tree
4882 : 50780 : build_va_arg_1 (location_t loc, tree type, tree op)
4883 : : {
4884 : 50780 : tree expr = build1 (VA_ARG_EXPR, type, op);
4885 : 50780 : SET_EXPR_LOCATION (expr, loc);
4886 : 50780 : return expr;
4887 : : }
4888 : :
4889 : : /* Return a VA_ARG_EXPR corresponding to a source-level expression
4890 : : va_arg (EXPR, TYPE) at source location LOC. */
4891 : :
4892 : : tree
4893 : 50780 : build_va_arg (location_t loc, tree expr, tree type)
4894 : : {
4895 : 50780 : tree va_type = TREE_TYPE (expr);
4896 : 50780 : tree canon_va_type = (va_type == error_mark_node
4897 : 50780 : ? error_mark_node
4898 : 50770 : : targetm.canonical_va_list_type (va_type));
4899 : :
4900 : 50780 : if (va_type == error_mark_node
4901 : 50770 : || canon_va_type == NULL_TREE)
4902 : : {
4903 : 10 : if (canon_va_type == NULL_TREE)
4904 : 12 : error_at (loc, "first argument to %<va_arg%> not of type %<va_list%>");
4905 : :
4906 : : /* Let's handle things neutrally, if expr:
4907 : : - has undeclared type, or
4908 : : - is not an va_list type. */
4909 : 22 : return build_va_arg_1 (loc, type, error_mark_node);
4910 : : }
4911 : :
4912 : 50758 : if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
4913 : : {
4914 : : /* Case 1: Not an array type. */
4915 : :
4916 : : /* Take the address, to get '&ap'. Note that &ap is not a va_list
4917 : : type. */
4918 : 258 : c_common_mark_addressable_vec (expr);
4919 : 258 : expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
4920 : :
4921 : 258 : return build_va_arg_1 (loc, type, expr);
4922 : : }
4923 : :
4924 : : /* Case 2: Array type.
4925 : :
4926 : : Background:
4927 : :
4928 : : For contrast, let's start with the simple case (case 1). If
4929 : : canon_va_type is not an array type, but say a char *, then when
4930 : : passing-by-value a va_list, the type of the va_list param decl is
4931 : : the same as for another va_list decl (all ap's are char *):
4932 : :
4933 : : f2_1 (char * ap)
4934 : : D.1815 = VA_ARG (&ap, 0B, 1);
4935 : : return D.1815;
4936 : :
4937 : : f2 (int i)
4938 : : char * ap.0;
4939 : : char * ap;
4940 : : __builtin_va_start (&ap, 0);
4941 : : ap.0 = ap;
4942 : : res = f2_1 (ap.0);
4943 : : __builtin_va_end (&ap);
4944 : : D.1812 = res;
4945 : : return D.1812;
4946 : :
4947 : : However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
4948 : : va_list the type of the va_list param decl (case 2b, struct * ap) is not
4949 : : the same as for another va_list decl (case 2a, struct ap[1]).
4950 : :
4951 : : f2_1 (struct * ap)
4952 : : D.1844 = VA_ARG (ap, 0B, 0);
4953 : : return D.1844;
4954 : :
4955 : : f2 (int i)
4956 : : struct ap[1];
4957 : : __builtin_va_start (&ap, 0);
4958 : : res = f2_1 (&ap);
4959 : : __builtin_va_end (&ap);
4960 : : D.1841 = res;
4961 : : return D.1841;
4962 : :
4963 : : Case 2b is different because:
4964 : : - on the callee side, the parm decl has declared type va_list, but
4965 : : grokdeclarator changes the type of the parm decl to a pointer to the
4966 : : array elem type.
4967 : : - on the caller side, the pass-by-value uses &ap.
4968 : :
4969 : : We unify these two cases (case 2a: va_list is array type,
4970 : : case 2b: va_list is pointer to array elem type), by adding '&' for the
4971 : : array type case, such that we have a pointer to array elem in both
4972 : : cases. */
4973 : :
4974 : 50500 : if (TREE_CODE (va_type) == ARRAY_TYPE)
4975 : : {
4976 : : /* Case 2a: va_list is array type. */
4977 : :
4978 : : /* Take the address, to get '&ap'. Make sure it's a pointer to array
4979 : : elem type. */
4980 : 49753 : c_common_mark_addressable_vec (expr);
4981 : 49753 : expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
4982 : : expr);
4983 : :
4984 : : /* Verify that &ap is still recognized as having va_list type. */
4985 : 49753 : tree canon_expr_type
4986 : 49753 : = targetm.canonical_va_list_type (TREE_TYPE (expr));
4987 : 49753 : gcc_assert (canon_expr_type != NULL_TREE);
4988 : : }
4989 : : else
4990 : : {
4991 : : /* Case 2b: va_list is pointer to array elem type. */
4992 : 747 : gcc_assert (POINTER_TYPE_P (va_type));
4993 : :
4994 : : /* Comparison as in std_canonical_va_list_type. */
4995 : 747 : gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (va_type))
4996 : : == TYPE_MAIN_VARIANT (TREE_TYPE (canon_va_type)));
4997 : :
4998 : : /* Don't take the address. We've already got '&ap'. */
4999 : 50500 : ;
5000 : : }
5001 : :
5002 : 50500 : return build_va_arg_1 (loc, type, expr);
5003 : : }
5004 : :
5005 : :
5006 : : /* Linked list of disabled built-in functions. */
5007 : :
5008 : : struct disabled_builtin
5009 : : {
5010 : : const char *name;
5011 : : struct disabled_builtin *next;
5012 : : };
5013 : : static disabled_builtin *disabled_builtins = NULL;
5014 : :
5015 : : static bool builtin_function_disabled_p (const char *);
5016 : :
5017 : : /* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5018 : : begins with "__builtin_", give an error. */
5019 : :
5020 : : void
5021 : 308 : disable_builtin_function (const char *name)
5022 : : {
5023 : 308 : if (startswith (name, "__builtin_"))
5024 : 0 : error ("cannot disable built-in function %qs", name);
5025 : : else
5026 : : {
5027 : 308 : disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5028 : 308 : new_disabled_builtin->name = name;
5029 : 308 : new_disabled_builtin->next = disabled_builtins;
5030 : 308 : disabled_builtins = new_disabled_builtin;
5031 : : }
5032 : 308 : }
5033 : :
5034 : :
5035 : : /* Return true if the built-in function NAME has been disabled, false
5036 : : otherwise. */
5037 : :
5038 : : static bool
5039 : 158730748 : builtin_function_disabled_p (const char *name)
5040 : : {
5041 : 158730748 : disabled_builtin *p;
5042 : 159015208 : for (p = disabled_builtins; p != NULL; p = p->next)
5043 : : {
5044 : 284768 : if (strcmp (name, p->name) == 0)
5045 : : return true;
5046 : : }
5047 : : return false;
5048 : : }
5049 : :
5050 : :
5051 : : /* Worker for DEF_BUILTIN.
5052 : : Possibly define a builtin function with one or two names.
5053 : : Does not declare a non-__builtin_ function if flag_no_builtin, or if
5054 : : nonansi_p and flag_no_nonansi_builtin. */
5055 : :
5056 : : static void
5057 : 311056585 : def_builtin_1 (enum built_in_function fncode,
5058 : : const char *name,
5059 : : enum built_in_class fnclass,
5060 : : tree fntype, tree libtype,
5061 : : bool both_p, bool fallback_p, bool nonansi_p,
5062 : : tree fnattrs, bool implicit_p)
5063 : : {
5064 : 311056585 : tree decl;
5065 : 311056585 : const char *libname;
5066 : :
5067 : 311056585 : if (fntype == error_mark_node)
5068 : : return;
5069 : :
5070 : 293163218 : gcc_assert ((!both_p && !fallback_p)
5071 : : || startswith (name, "__builtin_"));
5072 : :
5073 : 293163218 : libname = name + strlen ("__builtin_");
5074 : 293163218 : decl = add_builtin_function (name, fntype, fncode, fnclass,
5075 : : (fallback_p ? libname : NULL),
5076 : : fnattrs);
5077 : :
5078 : 293163218 : set_builtin_decl (fncode, decl, implicit_p);
5079 : :
5080 : 293163218 : if (both_p
5081 : 159153547 : && !flag_no_builtin && !builtin_function_disabled_p (libname)
5082 : 451893658 : && !(nonansi_p && flag_no_nonansi_builtin))
5083 : 143496055 : add_builtin_function (libname, libtype, fncode, fnclass,
5084 : : NULL, fnattrs);
5085 : : }
5086 : :
5087 : : /* Nonzero if the type T promotes to int. This is (nearly) the
5088 : : integral promotions defined in ISO C99 6.3.1.1/2. */
5089 : :
5090 : : bool
5091 : 484305061 : c_promoting_integer_type_p (const_tree t)
5092 : : {
5093 : 484305061 : switch (TREE_CODE (t))
5094 : : {
5095 : 374834384 : case INTEGER_TYPE:
5096 : 374834384 : return (TYPE_MAIN_VARIANT (t) == char_type_node
5097 : 369349017 : || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5098 : 368946985 : || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5099 : 354663121 : || TYPE_MAIN_VARIANT (t) == short_integer_type_node
5100 : 353644406 : || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5101 : 723156615 : || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
5102 : :
5103 : 657142 : case ENUMERAL_TYPE:
5104 : : /* ??? Technically all enumerations not larger than an int
5105 : : promote to an int. But this is used along code paths
5106 : : that only want to notice a size change. */
5107 : 657142 : return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5108 : :
5109 : : case BOOLEAN_TYPE:
5110 : : return true;
5111 : :
5112 : : default:
5113 : : return false;
5114 : : }
5115 : : }
5116 : :
5117 : : /* Return 1 if PARMS specifies a fixed number of parameters
5118 : : and none of their types is affected by default promotions. */
5119 : :
5120 : : bool
5121 : 21742 : self_promoting_args_p (const_tree parms)
5122 : : {
5123 : 21742 : const_tree t;
5124 : 64722 : for (t = parms; t; t = TREE_CHAIN (t))
5125 : : {
5126 : 43511 : tree type = TREE_VALUE (t);
5127 : :
5128 : 43511 : if (type == error_mark_node)
5129 : 2 : continue;
5130 : :
5131 : 43509 : if (TREE_CHAIN (t) == NULL_TREE && type != void_type_node)
5132 : : return false;
5133 : :
5134 : 43028 : if (type == NULL_TREE)
5135 : : return false;
5136 : :
5137 : 43028 : if (TYPE_MAIN_VARIANT (type) == float_type_node)
5138 : : return false;
5139 : :
5140 : 42983 : if (c_promoting_integer_type_p (type))
5141 : : return false;
5142 : : }
5143 : : return true;
5144 : : }
5145 : :
5146 : : /* Recursively remove any '*' or '&' operator from TYPE. */
5147 : : tree
5148 : 36448850 : strip_pointer_operator (tree t)
5149 : : {
5150 : 38574320 : while (POINTER_TYPE_P (t))
5151 : 2125470 : t = TREE_TYPE (t);
5152 : 36448850 : return t;
5153 : : }
5154 : :
5155 : : /* Recursively remove pointer or array type from TYPE. */
5156 : : tree
5157 : 10323545 : strip_pointer_or_array_types (tree t)
5158 : : {
5159 : 14972275 : while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
5160 : 4648730 : t = TREE_TYPE (t);
5161 : 10323545 : return t;
5162 : : }
5163 : :
5164 : : /* Used to compare case labels. K1 and K2 are actually tree nodes
5165 : : representing case labels, or NULL_TREE for a `default' label.
5166 : : Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
5167 : : K2, and 0 if K1 and K2 are equal. */
5168 : :
5169 : : int
5170 : 27444490 : case_compare (splay_tree_key k1, splay_tree_key k2)
5171 : : {
5172 : : /* Consider a NULL key (such as arises with a `default' label) to be
5173 : : smaller than anything else. */
5174 : 27444490 : if (!k1)
5175 : 3679177 : return k2 ? -1 : 0;
5176 : 24425497 : else if (!k2)
5177 : : return k1 ? 1 : 0;
5178 : :
5179 : 23634472 : return tree_int_cst_compare ((tree) k1, (tree) k2);
5180 : : }
5181 : :
5182 : : /* Process a case label, located at LOC, for the range LOW_VALUE
5183 : : ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
5184 : : then this case label is actually a `default' label. If only
5185 : : HIGH_VALUE is NULL_TREE, then case label was declared using the
5186 : : usual C/C++ syntax, rather than the GNU case range extension.
5187 : : CASES is a tree containing all the case ranges processed so far;
5188 : : COND is the condition for the switch-statement itself.
5189 : : Returns the CASE_LABEL_EXPR created, or ERROR_MARK_NODE if no
5190 : : CASE_LABEL_EXPR is created. ATTRS are the attributes to be applied
5191 : : to the label. */
5192 : :
5193 : : tree
5194 : 2985968 : c_add_case_label (location_t loc, splay_tree cases, tree cond,
5195 : : tree low_value, tree high_value, tree attrs)
5196 : : {
5197 : 2985968 : tree type;
5198 : 2985968 : tree label;
5199 : 2985968 : tree case_label;
5200 : 2985968 : splay_tree_node node;
5201 : :
5202 : : /* Create the LABEL_DECL itself. */
5203 : 2985968 : label = create_artificial_label (loc);
5204 : 2985968 : decl_attributes (&label, attrs, 0);
5205 : :
5206 : : /* If there was an error processing the switch condition, bail now
5207 : : before we get more confused. */
5208 : 2985968 : if (!cond || cond == error_mark_node)
5209 : 0 : goto error_out;
5210 : :
5211 : 2688624 : if ((low_value && TREE_TYPE (low_value)
5212 : 2688624 : && POINTER_TYPE_P (TREE_TYPE (low_value)))
5213 : 5674591 : || (high_value && TREE_TYPE (high_value)
5214 : 440 : && POINTER_TYPE_P (TREE_TYPE (high_value))))
5215 : : {
5216 : 1 : error_at (loc, "pointers are not permitted as case values");
5217 : 1 : goto error_out;
5218 : : }
5219 : :
5220 : : /* Case ranges are a GNU extension. */
5221 : 2985967 : if (high_value)
5222 : : {
5223 : 440 : if (c_dialect_cxx ())
5224 : 126 : pedwarn (loc, OPT_Wpedantic,
5225 : : "range expressions in switch statements are non-standard");
5226 : 314 : else if (warn_c23_c2y_compat > 0)
5227 : : {
5228 : 13 : if (pedantic && !flag_isoc2y)
5229 : 0 : pedwarn (loc, OPT_Wc23_c2y_compat,
5230 : : "ISO C does not support range expressions in switch "
5231 : : "statements before C2Y");
5232 : : else
5233 : 13 : warning_at (loc, OPT_Wc23_c2y_compat,
5234 : : "ISO C does not support range expressions in switch "
5235 : : "statements before C2Y");
5236 : : }
5237 : 301 : else if (warn_c23_c2y_compat && pedantic && !flag_isoc2y)
5238 : 28 : pedwarn (loc, OPT_Wpedantic,
5239 : : "ISO C does not support range expressions in switch "
5240 : : "statements before C2Y");
5241 : : }
5242 : :
5243 : 2985967 : type = TREE_TYPE (cond);
5244 : 2985967 : if (low_value)
5245 : : {
5246 : 2688623 : low_value = check_case_value (loc, low_value);
5247 : 2688623 : tree tem = NULL_TREE;
5248 : 2688623 : if (high_value
5249 : 440 : && !c_dialect_cxx ()
5250 : 314 : && low_value != error_mark_node
5251 : 313 : && !int_fits_type_p (low_value, type)
5252 : 2689063 : && pedwarn (loc, OPT_Wpedantic,
5253 : : "conversion of %qE to %qT in range expression changes "
5254 : : "value to %qE", low_value, type,
5255 : 20 : (tem = fold_convert (type, low_value))))
5256 : 20 : low_value = tem;
5257 : 2688623 : low_value = convert_and_check (loc, type, low_value);
5258 : 2688623 : low_value = fold (low_value);
5259 : 2688623 : if (low_value == error_mark_node)
5260 : 147 : goto error_out;
5261 : : }
5262 : 2985820 : if (high_value)
5263 : : {
5264 : 439 : high_value = check_case_value (loc, high_value);
5265 : 439 : tree tem = NULL_TREE;
5266 : 439 : if (!c_dialect_cxx ()
5267 : 313 : && high_value != error_mark_node
5268 : 312 : && !int_fits_type_p (high_value, type)
5269 : 752 : && pedwarn (loc, OPT_Wpedantic,
5270 : : "conversion of %qE to %qT in range expression changes "
5271 : : "value to %qE", high_value, type,
5272 : 20 : (tem = fold_convert (type, high_value))))
5273 : 20 : high_value = tem;
5274 : 439 : high_value = convert_and_check (loc, type, high_value);
5275 : 439 : high_value = fold (high_value);
5276 : 439 : if (high_value == error_mark_node)
5277 : 4 : goto error_out;
5278 : : }
5279 : :
5280 : 2985816 : if (low_value && high_value)
5281 : : {
5282 : : /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
5283 : : really a case range, even though it was written that way.
5284 : : Remove the HIGH_VALUE to simplify later processing. */
5285 : 435 : if (tree_int_cst_equal (low_value, high_value))
5286 : : high_value = NULL_TREE;
5287 : 416 : else if (!tree_int_cst_lt (low_value, high_value))
5288 : : {
5289 : 28 : warning_at (loc, 0, "empty range specified");
5290 : 28 : goto error_out;
5291 : : }
5292 : : }
5293 : :
5294 : : /* Look up the LOW_VALUE in the table of case labels we already
5295 : : have. */
5296 : 2985788 : node = splay_tree_lookup (cases, (splay_tree_key) low_value);
5297 : : /* If there was not an exact match, check for overlapping ranges.
5298 : : There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
5299 : : that's a `default' label and the only overlap is an exact match. */
5300 : 2985788 : if (!node && (low_value || high_value))
5301 : : {
5302 : 2688426 : splay_tree_node low_bound;
5303 : 2688426 : splay_tree_node high_bound;
5304 : :
5305 : : /* Even though there wasn't an exact match, there might be an
5306 : : overlap between this case range and another case range.
5307 : : Since we've (inductively) not allowed any overlapping case
5308 : : ranges, we simply need to find the greatest low case label
5309 : : that is smaller that LOW_VALUE, and the smallest low case
5310 : : label that is greater than LOW_VALUE. If there is an overlap
5311 : : it will occur in one of these two ranges. */
5312 : 2688426 : low_bound = splay_tree_predecessor (cases,
5313 : : (splay_tree_key) low_value);
5314 : 2688426 : high_bound = splay_tree_successor (cases,
5315 : : (splay_tree_key) low_value);
5316 : :
5317 : : /* Check to see if the LOW_BOUND overlaps. It is smaller than
5318 : : the LOW_VALUE, so there is no need to check unless the
5319 : : LOW_BOUND is in fact itself a case range. */
5320 : 2688426 : if (low_bound
5321 : 1824298 : && CASE_HIGH ((tree) low_bound->value)
5322 : 2688593 : && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
5323 : : low_value) >= 0)
5324 : : node = low_bound;
5325 : : /* Check to see if the HIGH_BOUND overlaps. The low end of that
5326 : : range is bigger than the low end of the current range, so we
5327 : : are only interested if the current range is a real range, and
5328 : : not an ordinary case label. */
5329 : 2688414 : else if (high_bound
5330 : 2688414 : && high_value
5331 : 2688414 : && (tree_int_cst_compare ((tree) high_bound->key,
5332 : : high_value)
5333 : : <= 0))
5334 : : node = high_bound;
5335 : : }
5336 : : /* If there was an overlap, issue an error. */
5337 : 2985769 : if (node)
5338 : : {
5339 : 37 : tree duplicate = CASE_LABEL ((tree) node->value);
5340 : :
5341 : 37 : if (high_value)
5342 : : {
5343 : 10 : error_at (loc, "duplicate (or overlapping) case value");
5344 : 10 : inform (DECL_SOURCE_LOCATION (duplicate),
5345 : : "this is the first entry overlapping that value");
5346 : : }
5347 : 27 : else if (low_value)
5348 : : {
5349 : 26 : error_at (loc, "duplicate case value") ;
5350 : 26 : inform (DECL_SOURCE_LOCATION (duplicate), "previously used here");
5351 : : }
5352 : : else
5353 : : {
5354 : 1 : error_at (loc, "multiple default labels in one switch");
5355 : 1 : inform (DECL_SOURCE_LOCATION (duplicate),
5356 : : "this is the first default label");
5357 : : }
5358 : 37 : goto error_out;
5359 : : }
5360 : :
5361 : : /* Add a CASE_LABEL to the statement-tree. */
5362 : 2985751 : case_label = add_stmt (build_case_label (low_value, high_value, label));
5363 : : /* Register this case label in the splay tree. */
5364 : 2985751 : splay_tree_insert (cases,
5365 : : (splay_tree_key) low_value,
5366 : : (splay_tree_value) case_label);
5367 : :
5368 : 2985751 : return case_label;
5369 : :
5370 : 217 : error_out:
5371 : : /* Add a label so that the back-end doesn't think that the beginning of
5372 : : the switch is unreachable. Note that we do not add a case label, as
5373 : : that just leads to duplicates and thence to failure later on. */
5374 : 217 : if (!cases->root)
5375 : : {
5376 : 153 : tree t = create_artificial_label (loc);
5377 : 153 : add_stmt (build_stmt (loc, LABEL_EXPR, t));
5378 : : }
5379 : 217 : return error_mark_node;
5380 : : }
5381 : :
5382 : : /* Subroutine of c_switch_covers_all_cases_p, called via
5383 : : splay_tree_foreach. Return 1 if it doesn't cover all the cases.
5384 : : ARGS[0] is initially NULL and after the first iteration is the
5385 : : so far highest case label. ARGS[1] is the minimum of SWITCH_COND's
5386 : : type. */
5387 : :
5388 : : static int
5389 : 132077 : c_switch_covers_all_cases_p_1 (splay_tree_node node, void *data)
5390 : : {
5391 : 132077 : tree label = (tree) node->value;
5392 : 132077 : tree *args = (tree *) data;
5393 : :
5394 : : /* If there is a default case, we shouldn't have called this. */
5395 : 132077 : gcc_assert (CASE_LOW (label));
5396 : :
5397 : 132077 : if (args[0] == NULL_TREE)
5398 : : {
5399 : 46285 : if (wi::to_widest (args[1]) < wi::to_widest (CASE_LOW (label)))
5400 : : return 1;
5401 : : }
5402 : 85792 : else if (wi::add (wi::to_widest (args[0]), 1)
5403 : 171584 : != wi::to_widest (CASE_LOW (label)))
5404 : : return 1;
5405 : 114050 : if (CASE_HIGH (label))
5406 : 109 : args[0] = CASE_HIGH (label);
5407 : : else
5408 : 113941 : args[0] = CASE_LOW (label);
5409 : : return 0;
5410 : : }
5411 : :
5412 : : /* Return true if switch with CASES and switch condition with type
5413 : : covers all possible values in the case labels. */
5414 : :
5415 : : bool
5416 : 79511 : c_switch_covers_all_cases_p (splay_tree cases, tree type)
5417 : : {
5418 : : /* If there is default:, this is always the case. */
5419 : 79511 : splay_tree_node default_node
5420 : 79511 : = splay_tree_lookup (cases, (splay_tree_key) NULL);
5421 : 79511 : if (default_node)
5422 : : return true;
5423 : :
5424 : 46817 : if (!INTEGRAL_TYPE_P (type))
5425 : : return false;
5426 : :
5427 : 46750 : tree args[2] = { NULL_TREE, TYPE_MIN_VALUE (type) };
5428 : 46750 : if (splay_tree_foreach (cases, c_switch_covers_all_cases_p_1, args))
5429 : : return false;
5430 : :
5431 : : /* If there are no cases at all, or if the highest case label
5432 : : is smaller than TYPE_MAX_VALUE, return false. */
5433 : 28723 : if (args[0] == NULL_TREE
5434 : 28723 : || wi::to_widest (args[0]) < wi::to_widest (TYPE_MAX_VALUE (type)))
5435 : 28677 : return false;
5436 : :
5437 : : return true;
5438 : : }
5439 : :
5440 : : /* Return true if stmt can fall through. Used by block_may_fallthru
5441 : : default case. */
5442 : :
5443 : : bool
5444 : 1702191 : c_block_may_fallthru (const_tree stmt)
5445 : : {
5446 : 1702191 : switch (TREE_CODE (stmt))
5447 : : {
5448 : 278 : case SWITCH_STMT:
5449 : 278 : return (!SWITCH_STMT_ALL_CASES_P (stmt)
5450 : 184 : || !SWITCH_STMT_NO_BREAK_P (stmt)
5451 : 306 : || block_may_fallthru (SWITCH_STMT_BODY (stmt)));
5452 : :
5453 : : default:
5454 : : return true;
5455 : : }
5456 : : }
5457 : :
5458 : : /* Finish an expression taking the address of LABEL (an
5459 : : IDENTIFIER_NODE). Returns an expression for the address.
5460 : :
5461 : : LOC is the location for the expression returned. */
5462 : :
5463 : : tree
5464 : 2117 : finish_label_address_expr (tree label, location_t loc)
5465 : : {
5466 : 2117 : tree result;
5467 : :
5468 : 2117 : pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
5469 : :
5470 : 2117 : if (label == error_mark_node)
5471 : : return error_mark_node;
5472 : :
5473 : 2111 : label = lookup_label (label);
5474 : 2111 : if (label == NULL_TREE)
5475 : 5 : result = null_pointer_node;
5476 : : else
5477 : : {
5478 : 2106 : TREE_USED (label) = 1;
5479 : 2106 : result = build1 (ADDR_EXPR, ptr_type_node, label);
5480 : : /* The current function is not necessarily uninlinable.
5481 : : Computed gotos are incompatible with inlining, but the value
5482 : : here could be used only in a diagnostic, for example. */
5483 : 2106 : protected_set_expr_location (result, loc);
5484 : : }
5485 : :
5486 : : return result;
5487 : : }
5488 : :
5489 : :
5490 : : /* Given a boolean expression ARG, return a tree representing an increment
5491 : : or decrement (as indicated by CODE) of ARG. The front end must check for
5492 : : invalid cases (e.g., decrement in C++). */
5493 : : tree
5494 : 85 : boolean_increment (enum tree_code code, tree arg)
5495 : : {
5496 : 85 : tree val;
5497 : 85 : tree true_res = build_int_cst (TREE_TYPE (arg), 1);
5498 : :
5499 : 85 : arg = stabilize_reference (arg);
5500 : 85 : switch (code)
5501 : : {
5502 : 28 : case PREINCREMENT_EXPR:
5503 : 28 : val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5504 : 28 : break;
5505 : 29 : case POSTINCREMENT_EXPR:
5506 : 29 : val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
5507 : 29 : arg = save_expr (arg);
5508 : 29 : val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5509 : 29 : val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5510 : 29 : break;
5511 : 14 : case PREDECREMENT_EXPR:
5512 : 14 : val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5513 : : invert_truthvalue_loc (input_location, arg));
5514 : 14 : break;
5515 : 14 : case POSTDECREMENT_EXPR:
5516 : 14 : val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
5517 : : invert_truthvalue_loc (input_location, arg));
5518 : 14 : arg = save_expr (arg);
5519 : 14 : val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
5520 : 14 : val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
5521 : 14 : break;
5522 : 0 : default:
5523 : 0 : gcc_unreachable ();
5524 : : }
5525 : 85 : TREE_SIDE_EFFECTS (val) = 1;
5526 : 85 : return val;
5527 : : }
5528 : :
5529 : : /* Built-in macros for stddef.h and stdint.h, that require macros
5530 : : defined in this file. */
5531 : : void
5532 : 205504 : c_stddef_cpp_builtins(void)
5533 : : {
5534 : 210907 : builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
5535 : 210907 : builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
5536 : 210907 : builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
5537 : : /* C++ has wchar_t as a builtin type, C doesn't, so if WINT_TYPE
5538 : : maps to wchar_t, define it to the underlying WCHAR_TYPE in C, and
5539 : : to wchar_t in C++, so the desired type equivalence holds. */
5540 : 205504 : if (!c_dialect_cxx ()
5541 : : && strcmp (WINT_TYPE, "wchar_t") == 0)
5542 : : builtin_define_with_value ("__WINT_TYPE__", WCHAR_TYPE, 0);
5543 : : else
5544 : 205504 : builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
5545 : 210907 : builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
5546 : 210907 : builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
5547 : 205504 : if (flag_char8_t)
5548 : 127977 : builtin_define_with_value ("__CHAR8_TYPE__", CHAR8_TYPE, 0);
5549 : 205504 : builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
5550 : 205504 : builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
5551 : 205504 : if (SIG_ATOMIC_TYPE)
5552 : 205504 : builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
5553 : 205504 : if (INT8_TYPE)
5554 : 205504 : builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
5555 : 205504 : if (INT16_TYPE)
5556 : 205504 : builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
5557 : 205504 : if (INT32_TYPE)
5558 : 205504 : builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
5559 : 205504 : if (INT64_TYPE)
5560 : 210907 : builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
5561 : 205504 : if (UINT8_TYPE)
5562 : 205504 : builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
5563 : 205504 : if (UINT16_TYPE)
5564 : 205504 : builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
5565 : 205504 : if (UINT32_TYPE)
5566 : 205504 : builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
5567 : 205504 : if (UINT64_TYPE)
5568 : 210907 : builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
5569 : 205504 : if (INT_LEAST8_TYPE)
5570 : 205504 : builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
5571 : 205504 : if (INT_LEAST16_TYPE)
5572 : 205504 : builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
5573 : 205504 : if (INT_LEAST32_TYPE)
5574 : 205504 : builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
5575 : 205504 : if (INT_LEAST64_TYPE)
5576 : 210907 : builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
5577 : 205504 : if (UINT_LEAST8_TYPE)
5578 : 205504 : builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
5579 : 205504 : if (UINT_LEAST16_TYPE)
5580 : 205504 : builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
5581 : 205504 : if (UINT_LEAST32_TYPE)
5582 : 205504 : builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
5583 : 205504 : if (UINT_LEAST64_TYPE)
5584 : 210907 : builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
5585 : 205504 : if (INT_FAST8_TYPE)
5586 : 205504 : builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
5587 : 205504 : if (INT_FAST16_TYPE)
5588 : 210907 : builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
5589 : 205504 : if (INT_FAST32_TYPE)
5590 : 210907 : builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
5591 : 205504 : if (INT_FAST64_TYPE)
5592 : 210907 : builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
5593 : 205504 : if (UINT_FAST8_TYPE)
5594 : 205504 : builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
5595 : 205504 : if (UINT_FAST16_TYPE)
5596 : 210907 : builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
5597 : 205504 : if (UINT_FAST32_TYPE)
5598 : 210907 : builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
5599 : 205504 : if (UINT_FAST64_TYPE)
5600 : 210907 : builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
5601 : 205504 : if (INTPTR_TYPE)
5602 : 210907 : builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
5603 : 205504 : if (UINTPTR_TYPE)
5604 : 210907 : builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
5605 : : /* GIMPLE FE testcases need access to the GCC internal 'sizetype'.
5606 : : Expose it as __SIZETYPE__. */
5607 : 205504 : if (flag_gimple)
5608 : 215 : builtin_define_with_value ("__SIZETYPE__", SIZETYPE, 0);
5609 : 205504 : }
5610 : :
5611 : : static void
5612 : 206026 : c_init_attributes (void)
5613 : : {
5614 : : /* Fill in the built_in_attributes array. */
5615 : : #define DEF_ATTR_NULL_TREE(ENUM) \
5616 : : built_in_attributes[(int) ENUM] = NULL_TREE;
5617 : : #define DEF_ATTR_INT(ENUM, VALUE) \
5618 : : built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
5619 : : #define DEF_ATTR_STRING(ENUM, VALUE) \
5620 : : built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
5621 : : #define DEF_ATTR_IDENT(ENUM, STRING) \
5622 : : built_in_attributes[(int) ENUM] = get_identifier (STRING);
5623 : : #define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
5624 : : built_in_attributes[(int) ENUM] \
5625 : : = tree_cons (built_in_attributes[(int) PURPOSE], \
5626 : : built_in_attributes[(int) VALUE], \
5627 : : built_in_attributes[(int) CHAIN]);
5628 : : #include "builtin-attrs.def"
5629 : : #undef DEF_ATTR_NULL_TREE
5630 : : #undef DEF_ATTR_INT
5631 : : #undef DEF_ATTR_IDENT
5632 : : #undef DEF_ATTR_TREE_LIST
5633 : 206026 : }
5634 : :
5635 : : /* Check whether the byte alignment ALIGN is a valid user-specified
5636 : : alignment less than the supported maximum. If so, return ALIGN's
5637 : : base-2 log; if not, output an error and return -1. If OBJFILE
5638 : : then reject alignments greater than MAX_OFILE_ALIGNMENT when
5639 : : converted to bits. Otherwise, consider valid only alignments
5640 : : that are less than HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT.
5641 : : Zero is not considered a valid argument (and results in -1 on
5642 : : return) but it only triggers a warning when WARN_ZERO is set. */
5643 : :
5644 : : int
5645 : 562011 : check_user_alignment (const_tree align, bool objfile, bool warn_zero)
5646 : : {
5647 : 562011 : if (error_operand_p (align))
5648 : : return -1;
5649 : :
5650 : 561988 : if (TREE_CODE (align) != INTEGER_CST
5651 : 561988 : || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
5652 : : {
5653 : 27 : error ("requested alignment is not an integer constant");
5654 : 27 : return -1;
5655 : : }
5656 : :
5657 : 561961 : if (integer_zerop (align))
5658 : : {
5659 : 75 : if (warn_zero)
5660 : 63 : warning (OPT_Wattributes,
5661 : : "requested alignment %qE is not a positive power of 2",
5662 : : align);
5663 : 75 : return -1;
5664 : : }
5665 : :
5666 : : /* Log2 of the byte alignment ALIGN. */
5667 : 561886 : int log2align;
5668 : 561886 : if (tree_int_cst_sgn (align) == -1
5669 : 561886 : || (log2align = tree_log2 (align)) == -1)
5670 : : {
5671 : 36 : error ("requested alignment %qE is not a positive power of 2",
5672 : : align);
5673 : 36 : return -1;
5674 : : }
5675 : :
5676 : 561850 : if (objfile)
5677 : : {
5678 : 15455 : unsigned maxalign = MAX_OFILE_ALIGNMENT / BITS_PER_UNIT;
5679 : 15455 : if (!tree_fits_uhwi_p (align) || tree_to_uhwi (align) > maxalign)
5680 : : {
5681 : 10 : error ("requested alignment %qE exceeds object file maximum %u",
5682 : : align, maxalign);
5683 : 10 : return -1;
5684 : : }
5685 : : }
5686 : :
5687 : 561840 : if (log2align >= HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT)
5688 : : {
5689 : 17 : error ("requested alignment %qE exceeds maximum %u",
5690 : : align, 1U << (HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT - 1));
5691 : 17 : return -1;
5692 : : }
5693 : :
5694 : : return log2align;
5695 : : }
5696 : :
5697 : : /* Determine the ELF symbol visibility for DECL, which is either a
5698 : : variable or a function. It is an error to use this function if a
5699 : : definition of DECL is not available in this translation unit.
5700 : : Returns true if the final visibility has been determined by this
5701 : : function; false if the caller is free to make additional
5702 : : modifications. */
5703 : :
5704 : : bool
5705 : 50374510 : c_determine_visibility (tree decl)
5706 : : {
5707 : 50374510 : gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
5708 : :
5709 : : /* If the user explicitly specified the visibility with an
5710 : : attribute, honor that. DECL_VISIBILITY will have been set during
5711 : : the processing of the attribute. We check for an explicit
5712 : : attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
5713 : : to distinguish the use of an attribute from the use of a "#pragma
5714 : : GCC visibility push(...)"; in the latter case we still want other
5715 : : considerations to be able to overrule the #pragma. */
5716 : 50374510 : if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
5717 : : || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
5718 : : && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
5719 : : || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
5720 : : return true;
5721 : :
5722 : : /* Set default visibility to whatever the user supplied with
5723 : : visibility_specified depending on #pragma GCC visibility. */
5724 : 49743005 : if (!DECL_VISIBILITY_SPECIFIED (decl))
5725 : : {
5726 : 49741767 : if (visibility_options.inpragma
5727 : 49741767 : || DECL_VISIBILITY (decl) != default_visibility)
5728 : : {
5729 : 210184 : DECL_VISIBILITY (decl) = default_visibility;
5730 : 210184 : DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
5731 : : /* If visibility changed and DECL already has DECL_RTL, ensure
5732 : : symbol flags are updated. */
5733 : 14204 : if (((VAR_P (decl) && TREE_STATIC (decl))
5734 : 210132 : || TREE_CODE (decl) == FUNCTION_DECL)
5735 : 406216 : && DECL_RTL_SET_P (decl))
5736 : 0 : make_decl_rtl (decl);
5737 : : }
5738 : : }
5739 : : return false;
5740 : : }
5741 : :
5742 : : /* Data to communicate through check_function_arguments_recurse between
5743 : : check_function_nonnull and check_nonnull_arg. */
5744 : :
5745 : : struct nonnull_arg_ctx
5746 : : {
5747 : : /* Location of the call. */
5748 : : location_t loc;
5749 : : /* The function whose arguments are being checked and its type (used
5750 : : for calls through function pointers). */
5751 : : const_tree fndecl, fntype;
5752 : : /* For nonnull_if_nonzero, index of the other argument. */
5753 : : unsigned HOST_WIDE_INT other;
5754 : : /* True if a warning has been issued. */
5755 : : bool warned_p;
5756 : : };
5757 : :
5758 : : /* Check the argument list of a function call to CTX.FNDECL of CTX.FNTYPE
5759 : : for null in argument slots that are marked as requiring a non-null
5760 : : pointer argument. The NARGS arguments are passed in the array ARGARRAY.
5761 : : Return true if we have warned. */
5762 : :
5763 : : static bool
5764 : 6366631 : check_function_nonnull (nonnull_arg_ctx &ctx, int nargs, tree *argarray)
5765 : : {
5766 : 6366631 : int firstarg = 0;
5767 : 6366631 : if (TREE_CODE (ctx.fntype) == METHOD_TYPE)
5768 : : {
5769 : 454834 : bool closure = false;
5770 : 454834 : if (ctx.fndecl)
5771 : : {
5772 : : /* For certain lambda expressions the C++ front end emits calls
5773 : : that pass a null this pointer as an argument named __closure
5774 : : to the member operator() of empty function. Detect those
5775 : : and avoid checking them, but proceed to check the remaining
5776 : : arguments. */
5777 : 454683 : tree arg0 = DECL_ARGUMENTS (ctx.fndecl);
5778 : 454683 : if (tree arg0name = DECL_NAME (arg0))
5779 : 454683 : closure = id_equal (arg0name, "__closure");
5780 : : }
5781 : :
5782 : : /* In calls to C++ non-static member functions check the this
5783 : : pointer regardless of whether the function is declared with
5784 : : attribute nonnull. */
5785 : 905400 : firstarg = 1;
5786 : 454683 : if (!closure)
5787 : 450717 : check_function_arguments_recurse (check_nonnull_arg, &ctx, argarray[0],
5788 : : firstarg, OPT_Wnonnull);
5789 : : }
5790 : :
5791 : 6366631 : tree attrs = lookup_attribute ("nonnull", TYPE_ATTRIBUTES (ctx.fntype));
5792 : :
5793 : 6366631 : tree a = attrs;
5794 : : /* See if any of the nonnull attributes has no arguments. If so,
5795 : : then every pointer argument is checked (in which case the check
5796 : : for pointer type is done in check_nonnull_arg). */
5797 : 12962266 : while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE)
5798 : 102094 : a = lookup_attribute ("nonnull", TREE_CHAIN (a));
5799 : :
5800 : 6366631 : if (a != NULL_TREE)
5801 : 66604 : for (int i = firstarg; i < nargs; i++)
5802 : 41788 : check_function_arguments_recurse (check_nonnull_arg, &ctx, argarray[i],
5803 : 41788 : i + 1, OPT_Wnonnull);
5804 : 6341815 : else if (attrs)
5805 : : {
5806 : : /* Walk the argument list. If we encounter an argument number we
5807 : : should check for non-null, do it. */
5808 : 343252 : for (int i = firstarg; i < nargs; i++)
5809 : : {
5810 : 146127 : for (a = attrs; ; a = TREE_CHAIN (a))
5811 : : {
5812 : 397784 : a = lookup_attribute ("nonnull", a);
5813 : 397784 : if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
5814 : : break;
5815 : : }
5816 : :
5817 : 251657 : if (a != NULL_TREE)
5818 : 122708 : check_function_arguments_recurse (check_nonnull_arg, &ctx,
5819 : 122708 : argarray[i], i + 1,
5820 : : OPT_Wnonnull);
5821 : : }
5822 : : }
5823 : 6366631 : if (a == NULL_TREE)
5824 : 6322342 : for (attrs = TYPE_ATTRIBUTES (ctx.fntype);
5825 : 6378300 : (attrs = lookup_attribute ("nonnull_if_nonzero", attrs));
5826 : 55958 : attrs = TREE_CHAIN (attrs))
5827 : : {
5828 : 55958 : tree args = TREE_VALUE (attrs);
5829 : 55958 : unsigned int idx = TREE_INT_CST_LOW (TREE_VALUE (args)) - 1;
5830 : 55958 : unsigned int idx2
5831 : 55958 : = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (args))) - 1;
5832 : 55958 : if (idx < (unsigned) nargs - firstarg
5833 : 55953 : && idx2 < (unsigned) nargs - firstarg
5834 : 55948 : && INTEGRAL_TYPE_P (TREE_TYPE (argarray[firstarg + idx2]))
5835 : 111833 : && integer_nonzerop (argarray[firstarg + idx2]))
5836 : : {
5837 : 31484 : ctx.other = firstarg + idx2 + 1;
5838 : 31484 : check_function_arguments_recurse (check_nonnull_arg, &ctx,
5839 : 31484 : argarray[firstarg + idx],
5840 : 31484 : firstarg + idx + 1,
5841 : : OPT_Wnonnull);
5842 : 31484 : ctx.other = 0;
5843 : : }
5844 : : }
5845 : 6366631 : return ctx.warned_p;
5846 : : }
5847 : :
5848 : : /* Check that the Nth argument of a function call (counting backwards
5849 : : from the end) is a (pointer)0. The NARGS arguments are passed in the
5850 : : array ARGARRAY. */
5851 : :
5852 : : static void
5853 : 6353294 : check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
5854 : : {
5855 : 6353294 : tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
5856 : :
5857 : 6353294 : if (attr)
5858 : : {
5859 : 112 : int len = 0;
5860 : 112 : int pos = 0;
5861 : 112 : tree sentinel;
5862 : 112 : function_args_iterator iter;
5863 : 112 : tree t;
5864 : :
5865 : : /* Skip over the named arguments. */
5866 : 237 : FOREACH_FUNCTION_ARGS (fntype, t, iter)
5867 : : {
5868 : 125 : if (len == nargs)
5869 : : break;
5870 : 125 : len++;
5871 : : }
5872 : :
5873 : 112 : if (TREE_VALUE (attr))
5874 : : {
5875 : 59 : tree p = TREE_VALUE (TREE_VALUE (attr));
5876 : 59 : pos = TREE_INT_CST_LOW (p);
5877 : : }
5878 : :
5879 : : /* The sentinel must be one of the varargs, i.e.
5880 : : in position >= the number of fixed arguments. */
5881 : 112 : if ((nargs - 1 - pos) < len)
5882 : : {
5883 : 22 : warning (OPT_Wformat_,
5884 : : "not enough variable arguments to fit a sentinel");
5885 : 22 : return;
5886 : : }
5887 : :
5888 : : /* Validate the sentinel. */
5889 : 90 : sentinel = fold_for_warn (argarray[nargs - 1 - pos]);
5890 : 138 : if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
5891 : 42 : || !integer_zerop (sentinel))
5892 : 56 : && TREE_CODE (TREE_TYPE (sentinel)) != NULLPTR_TYPE
5893 : : /* Although __null (in C++) is only an integer we allow it
5894 : : nevertheless, as we are guaranteed that it's exactly
5895 : : as wide as a pointer, and we don't want to force
5896 : : users to cast the NULL they have written there.
5897 : : We warn with -Wstrict-null-sentinel, though. */
5898 : 138 : && (warn_strict_null_sentinel || null_node != sentinel))
5899 : 45 : warning (OPT_Wformat_, "missing sentinel in function call");
5900 : : }
5901 : : }
5902 : :
5903 : : /* Check that the same argument isn't passed to two or more
5904 : : restrict-qualified formal and issue a -Wrestrict warning
5905 : : if it is. Return true if a warning has been issued. */
5906 : :
5907 : : static bool
5908 : 52032225 : check_function_restrict (const_tree fndecl, const_tree fntype,
5909 : : int nargs, tree *unfolded_argarray)
5910 : : {
5911 : 52032225 : int i;
5912 : 52032225 : tree parms = TYPE_ARG_TYPES (fntype);
5913 : :
5914 : : /* Call fold_for_warn on all of the arguments. */
5915 : 52032225 : auto_vec<tree> argarray (nargs);
5916 : 234582942 : for (i = 0; i < nargs; i++)
5917 : 130518492 : argarray.quick_push (fold_for_warn (unfolded_argarray[i]));
5918 : :
5919 : 52032225 : if (fndecl
5920 : 51512121 : && TREE_CODE (fndecl) == FUNCTION_DECL)
5921 : : {
5922 : : /* Avoid diagnosing calls built-ins with a zero size/bound
5923 : : here. They are checked in more detail elsewhere. */
5924 : 51512121 : if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
5925 : 1696648 : && nargs == 3
5926 : 322618 : && TREE_CODE (argarray[2]) == INTEGER_CST
5927 : 51681417 : && integer_zerop (argarray[2]))
5928 : : return false;
5929 : :
5930 : 51486519 : if (DECL_ARGUMENTS (fndecl))
5931 : 52006623 : parms = DECL_ARGUMENTS (fndecl);
5932 : : }
5933 : :
5934 : 182448309 : for (i = 0; i < nargs; i++)
5935 : 130441686 : TREE_VISITED (argarray[i]) = 0;
5936 : :
5937 : : bool warned = false;
5938 : :
5939 : 181571102 : for (i = 0; i < nargs && parms && parms != void_list_node; i++)
5940 : : {
5941 : 129564479 : tree type;
5942 : 129564479 : if (TREE_CODE (parms) == PARM_DECL)
5943 : : {
5944 : 15427668 : type = TREE_TYPE (parms);
5945 : 15427668 : parms = DECL_CHAIN (parms);
5946 : : }
5947 : : else
5948 : : {
5949 : 114136811 : type = TREE_VALUE (parms);
5950 : 114136811 : parms = TREE_CHAIN (parms);
5951 : : }
5952 : 129564479 : if (POINTER_TYPE_P (type)
5953 : 7789534 : && TYPE_RESTRICT (type)
5954 : 129811834 : && !TYPE_READONLY (TREE_TYPE (type)))
5955 : 247982 : warned |= warn_for_restrict (i, argarray.address (), nargs);
5956 : : }
5957 : :
5958 : 182448309 : for (i = 0; i < nargs; i++)
5959 : 130441686 : TREE_VISITED (argarray[i]) = 0;
5960 : :
5961 : : return warned;
5962 : 52032225 : }
5963 : :
5964 : : /* Helper for check_function_nonnull; given a list of operands which
5965 : : must be non-null in ARGS, determine if operand PARAM_NUM should be
5966 : : checked. */
5967 : :
5968 : : static bool
5969 : 268835 : nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
5970 : : {
5971 : 268835 : unsigned HOST_WIDE_INT arg_num = 0;
5972 : :
5973 : 714512 : for (; args; args = TREE_CHAIN (args))
5974 : : {
5975 : 568385 : bool found = get_attribute_operand (TREE_VALUE (args), &arg_num);
5976 : :
5977 : 568385 : gcc_assert (found);
5978 : :
5979 : 568385 : if (arg_num == param_num)
5980 : : return true;
5981 : : }
5982 : : return false;
5983 : : }
5984 : :
5985 : : /* Check that the function argument PARAM (which is operand number
5986 : : PARAM_NUM) is non-null. This is called by check_function_nonnull
5987 : : via check_function_arguments_recurse. */
5988 : :
5989 : : static void
5990 : 645653 : check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
5991 : : {
5992 : 645653 : struct nonnull_arg_ctx *pctx = (struct nonnull_arg_ctx *) ctx;
5993 : :
5994 : : /* Just skip checking the argument if it's not a pointer. This can
5995 : : happen if the "nonnull" attribute was given without an operand
5996 : : list (which means to check every pointer argument). */
5997 : :
5998 : 645653 : tree paramtype = TREE_TYPE (param);
5999 : 645653 : if (TREE_CODE (paramtype) != POINTER_TYPE
6000 : 57934 : && TREE_CODE (paramtype) != NULLPTR_TYPE)
6001 : 644922 : return;
6002 : :
6003 : : /* Diagnose the simple cases of null arguments. */
6004 : 587725 : if (!integer_zerop (fold_for_warn (param)))
6005 : : return;
6006 : :
6007 : 731 : auto_diagnostic_group adg;
6008 : :
6009 : 731 : const location_t loc = EXPR_LOC_OR_LOC (param, pctx->loc);
6010 : :
6011 : 731 : if (TREE_CODE (pctx->fntype) == METHOD_TYPE)
6012 : 109 : --param_num;
6013 : :
6014 : 731 : bool warned;
6015 : 731 : if (param_num == 0)
6016 : : {
6017 : 30 : warned = warning_at (loc, OPT_Wnonnull,
6018 : : "%qs pointer is null", "this");
6019 : 30 : if (warned && pctx->fndecl)
6020 : 27 : inform (DECL_SOURCE_LOCATION (pctx->fndecl),
6021 : : "in a call to non-static member function %qD",
6022 : : pctx->fndecl);
6023 : : }
6024 : : else
6025 : : {
6026 : 701 : if (pctx->other)
6027 : 50 : warned = warning_at (loc, OPT_Wnonnull,
6028 : : "argument %u null where non-null expected "
6029 : : "because argument %u is nonzero",
6030 : : (unsigned) param_num,
6031 : : TREE_CODE (pctx->fntype) == METHOD_TYPE
6032 : 0 : ? (unsigned) pctx->other - 1
6033 : : : (unsigned) pctx->other);
6034 : : else
6035 : 651 : warned = warning_at (loc, OPT_Wnonnull,
6036 : : "argument %u null where non-null expected",
6037 : : (unsigned) param_num);
6038 : 701 : if (warned && pctx->fndecl)
6039 : 670 : inform (DECL_SOURCE_LOCATION (pctx->fndecl),
6040 : : "in a call to function %qD declared %qs",
6041 : : pctx->fndecl,
6042 : 670 : pctx->other ? "nonnull_if_nonzero" : "nonnull");
6043 : : }
6044 : :
6045 : 728 : if (warned)
6046 : 728 : pctx->warned_p = true;
6047 : 731 : }
6048 : :
6049 : : /* Helper for attribute handling; fetch the operand number from
6050 : : the attribute argument list. */
6051 : :
6052 : : bool
6053 : 568385 : get_attribute_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
6054 : : {
6055 : : /* Verify the arg number is a small constant. */
6056 : 568385 : if (tree_fits_uhwi_p (arg_num_expr))
6057 : : {
6058 : 568385 : *valp = tree_to_uhwi (arg_num_expr);
6059 : 568385 : return true;
6060 : : }
6061 : : else
6062 : : return false;
6063 : : }
6064 : :
6065 : : /* Arguments being collected for optimization. */
6066 : : typedef const char *const_char_p; /* For DEF_VEC_P. */
6067 : : static GTY(()) vec<const_char_p, va_gc> *optimize_args;
6068 : :
6069 : :
6070 : : /* Inner function to convert a TREE_LIST to argv string to parse the optimize
6071 : : options in ARGS. ATTR_P is true if this is for attribute(optimize), and
6072 : : false for #pragma GCC optimize. */
6073 : :
6074 : : bool
6075 : 327628 : parse_optimize_options (tree args, bool attr_p)
6076 : : {
6077 : 327628 : bool ret = true;
6078 : 327628 : unsigned opt_argc;
6079 : 327628 : unsigned i;
6080 : 327628 : const char **opt_argv;
6081 : 327628 : struct cl_decoded_option *decoded_options;
6082 : 327628 : unsigned int decoded_options_count;
6083 : 327628 : tree ap;
6084 : :
6085 : : /* Build up argv vector. Just in case the string is stored away, use garbage
6086 : : collected strings. */
6087 : 327628 : vec_safe_truncate (optimize_args, 0);
6088 : 327628 : vec_safe_push (optimize_args, (const char *) NULL);
6089 : :
6090 : 673955 : for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
6091 : : {
6092 : 346327 : tree value = TREE_VALUE (ap);
6093 : :
6094 : 346327 : if (TREE_CODE (value) == INTEGER_CST)
6095 : : {
6096 : 303 : char buffer[HOST_BITS_PER_LONG / 3 + 4];
6097 : 303 : sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
6098 : 303 : vec_safe_push (optimize_args, ggc_strdup (buffer));
6099 : : }
6100 : :
6101 : 346024 : else if (TREE_CODE (value) == STRING_CST)
6102 : : {
6103 : : /* Split string into multiple substrings. */
6104 : 346020 : size_t len = TREE_STRING_LENGTH (value);
6105 : 346020 : char *p = ASTRDUP (TREE_STRING_POINTER (value));
6106 : 346020 : char *end = p + len;
6107 : 346020 : char *comma;
6108 : 346020 : char *next_p = p;
6109 : :
6110 : 692089 : while (next_p != NULL)
6111 : : {
6112 : 346069 : size_t len2;
6113 : 346069 : char *q, *r;
6114 : :
6115 : 346069 : p = next_p;
6116 : 346069 : comma = strchr (p, ',');
6117 : 346069 : if (comma)
6118 : : {
6119 : 49 : len2 = comma - p;
6120 : 49 : *comma = '\0';
6121 : 49 : next_p = comma+1;
6122 : : }
6123 : : else
6124 : : {
6125 : 346020 : len2 = end - p;
6126 : 346020 : next_p = NULL;
6127 : : }
6128 : :
6129 : : /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
6130 : : options. */
6131 : 346069 : if (*p == '-' && p[1] != 'O' && p[1] != 'f')
6132 : : {
6133 : 0 : ret = false;
6134 : 0 : if (attr_p)
6135 : : {
6136 : 0 : auto_urlify_attributes sentinel;
6137 : 0 : warning (OPT_Wattributes,
6138 : : "bad option %qs to attribute %<optimize%>", p);
6139 : 0 : }
6140 : : else
6141 : 0 : warning (OPT_Wpragmas,
6142 : : "bad option %qs to pragma %<optimize%>", p);
6143 : 0 : continue;
6144 : 0 : }
6145 : :
6146 : : /* Can't use GC memory here, see PR88007. */
6147 : 346069 : r = q = XOBNEWVEC (&opts_obstack, char, len2 + 3);
6148 : :
6149 : 346069 : if (*p != '-')
6150 : : {
6151 : 42751 : *r++ = '-';
6152 : :
6153 : : /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
6154 : : itself is -Os, and any other switch begins with a -f. */
6155 : 42751 : if ((*p >= '0' && *p <= '9')
6156 : 42594 : || (p[0] == 's' && p[1] == '\0'))
6157 : 157 : *r++ = 'O';
6158 : 42594 : else if (*p != 'O')
6159 : 23301 : *r++ = 'f';
6160 : : }
6161 : :
6162 : 346069 : memcpy (r, p, len2);
6163 : 346069 : r[len2] = '\0';
6164 : 346069 : vec_safe_push (optimize_args, (const char *) q);
6165 : : }
6166 : :
6167 : : }
6168 : : }
6169 : :
6170 : 327628 : opt_argc = optimize_args->length ();
6171 : 327628 : opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
6172 : :
6173 : 674000 : for (i = 1; i < opt_argc; i++)
6174 : 346372 : opt_argv[i] = (*optimize_args)[i];
6175 : :
6176 : : /* Now parse the options. */
6177 : 327628 : decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
6178 : : &decoded_options,
6179 : : &decoded_options_count);
6180 : : /* Drop non-Optimization options. */
6181 : 327628 : unsigned j = 1;
6182 : 674000 : for (i = 1; i < decoded_options_count; ++i)
6183 : : {
6184 : 346372 : if (! (cl_options[decoded_options[i].opt_index].flags & CL_OPTIMIZATION))
6185 : : {
6186 : 3 : ret = false;
6187 : 3 : if (attr_p)
6188 : : {
6189 : 2 : auto_urlify_attributes sentinel;
6190 : 2 : warning (OPT_Wattributes,
6191 : : "bad option %qs to attribute %<optimize%>",
6192 : 2 : decoded_options[i].orig_option_with_args_text);
6193 : 2 : }
6194 : : else
6195 : 1 : warning (OPT_Wpragmas,
6196 : : "bad option %qs to pragma %<optimize%>",
6197 : : decoded_options[i].orig_option_with_args_text);
6198 : 3 : continue;
6199 : 3 : }
6200 : 346369 : if (i != j)
6201 : 0 : decoded_options[j] = decoded_options[i];
6202 : 346369 : j++;
6203 : : }
6204 : 327628 : decoded_options_count = j;
6205 : :
6206 : : /* Merge the decoded options with save_decoded_options. */
6207 : 327628 : unsigned save_opt_count = save_opt_decoded_options->length ();
6208 : 327628 : unsigned merged_decoded_options_count
6209 : : = save_opt_count + decoded_options_count;
6210 : 327628 : cl_decoded_option *merged_decoded_options
6211 : 327628 : = XNEWVEC (cl_decoded_option, merged_decoded_options_count);
6212 : :
6213 : : /* Note the first decoded_options is used for the program name. */
6214 : 1874619 : for (unsigned i = 0; i < save_opt_count; ++i)
6215 : 1546991 : merged_decoded_options[i + 1] = (*save_opt_decoded_options)[i];
6216 : 673997 : for (unsigned i = 1; i < decoded_options_count; ++i)
6217 : 346369 : merged_decoded_options[save_opt_count + i] = decoded_options[i];
6218 : :
6219 : : /* And apply them. */
6220 : 327628 : decode_options (&global_options, &global_options_set,
6221 : : merged_decoded_options, merged_decoded_options_count,
6222 : : input_location, global_dc, NULL);
6223 : 327628 : free (decoded_options);
6224 : :
6225 : 327628 : targetm.override_options_after_change();
6226 : :
6227 : 327628 : optimize_args->truncate (0);
6228 : 327628 : return ret;
6229 : : }
6230 : :
6231 : : /* Check whether ATTR is a valid attribute fallthrough. */
6232 : :
6233 : : bool
6234 : 260892525 : attribute_fallthrough_p (tree attr)
6235 : : {
6236 : 260892525 : if (attr == error_mark_node)
6237 : : return false;
6238 : 260892507 : tree t = lookup_attribute ("", "fallthrough", attr);
6239 : 260892507 : if (t == NULL_TREE)
6240 : : return false;
6241 : 17464 : auto_urlify_attributes sentinel;
6242 : : /* It is no longer true that "this attribute shall appear at most once in
6243 : : each attribute-list", but we still give a warning. */
6244 : 17464 : if (lookup_attribute ("", "fallthrough", TREE_CHAIN (t)))
6245 : 9 : warning (OPT_Wattributes, "attribute %<fallthrough%> specified multiple "
6246 : : "times");
6247 : : /* No attribute-argument-clause shall be present. */
6248 : 17455 : else if (TREE_VALUE (t) != NULL_TREE)
6249 : 4 : warning (OPT_Wattributes, "%<fallthrough%> attribute specified with "
6250 : : "a parameter");
6251 : : /* Warn if other attributes are found. */
6252 : 34954 : for (t = attr; t != NULL_TREE; t = TREE_CHAIN (t))
6253 : : {
6254 : 17490 : tree name = get_attribute_name (t);
6255 : 17490 : if (!is_attribute_p ("fallthrough", name)
6256 : 17490 : || !is_attribute_namespace_p ("", t))
6257 : : {
6258 : 17 : if (!c_dialect_cxx () && get_attribute_namespace (t) == NULL_TREE)
6259 : : /* The specifications of standard attributes in C mean
6260 : : this is a constraint violation. */
6261 : 3 : pedwarn (input_location, OPT_Wattributes, "%qE attribute ignored",
6262 : : get_attribute_name (t));
6263 : : else
6264 : 14 : warning (OPT_Wattributes, "%qE attribute ignored", name);
6265 : : }
6266 : : }
6267 : 17464 : return true;
6268 : 17464 : }
6269 : :
6270 : :
6271 : : /* Check for valid arguments being passed to a function with FNTYPE.
6272 : : There are NARGS arguments in the array ARGARRAY. LOC should be used
6273 : : for diagnostics. Return true if either -Wnonnull or -Wrestrict has
6274 : : been issued.
6275 : :
6276 : : The arguments in ARGARRAY may not have been folded yet (e.g. for C++,
6277 : : to preserve location wrappers); checks that require folded arguments
6278 : : should call fold_for_warn on them.
6279 : :
6280 : : Use the frontend-supplied COMP_TYPES when determining if
6281 : : one type is a subclass of another. */
6282 : :
6283 : : bool
6284 : 52474173 : check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype,
6285 : : int nargs, tree *argarray, vec<location_t> *arglocs,
6286 : : bool (*comp_types) (tree, tree))
6287 : : {
6288 : 52474173 : bool warned_p = false;
6289 : :
6290 : 52474173 : if (c_inhibit_evaluation_warnings)
6291 : : return warned_p;
6292 : :
6293 : : /* Check for null being passed in a pointer argument that must be
6294 : : non-null. In C++, this includes the this pointer. We also need
6295 : : to do this if format checking is enabled. */
6296 : 52036952 : if (warn_nonnull)
6297 : : {
6298 : 6366631 : nonnull_arg_ctx ctx = { loc, fndecl, fntype, 0, false };
6299 : 6366631 : warned_p = check_function_nonnull (ctx, nargs, argarray);
6300 : : }
6301 : :
6302 : : /* Check for errors in format strings. */
6303 : :
6304 : 52036952 : if (warn_format || warn_suggest_attribute_format)
6305 : 6430157 : check_function_format (fndecl ? fndecl : fntype, TYPE_ATTRIBUTES (fntype), nargs,
6306 : : argarray, arglocs, comp_types);
6307 : :
6308 : 52036952 : if (warn_format)
6309 : 6353294 : check_function_sentinel (fntype, nargs, argarray);
6310 : :
6311 : 52036952 : if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
6312 : : {
6313 : 1701375 : switch (DECL_FUNCTION_CODE (fndecl))
6314 : : {
6315 : : case BUILT_IN_SPRINTF:
6316 : : case BUILT_IN_SPRINTF_CHK:
6317 : : case BUILT_IN_SNPRINTF:
6318 : : case BUILT_IN_SNPRINTF_CHK:
6319 : : /* Let the sprintf pass handle these. */
6320 : : return warned_p;
6321 : :
6322 : : default:
6323 : : break;
6324 : : }
6325 : : }
6326 : :
6327 : : /* check_function_restrict sets the DECL_READ_P for arguments
6328 : : so it must be called unconditionally. */
6329 : 52032225 : warned_p |= check_function_restrict (fndecl, fntype, nargs, argarray);
6330 : :
6331 : 52032225 : return warned_p;
6332 : : }
6333 : :
6334 : : /* Generic argument checking recursion routine. PARAM is the argument to
6335 : : be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
6336 : : once the argument is resolved. CTX is context for the callback.
6337 : : OPT is the warning for which this is done. */
6338 : : void
6339 : 701317 : check_function_arguments_recurse (void (*callback)
6340 : : (void *, tree, unsigned HOST_WIDE_INT),
6341 : : void *ctx, tree param,
6342 : : unsigned HOST_WIDE_INT param_num,
6343 : : opt_code opt)
6344 : : {
6345 : 1101411 : if (opt != OPT_Wformat_ && warning_suppressed_p (param))
6346 : : return;
6347 : :
6348 : 747670 : if (CONVERT_EXPR_P (param)
6349 : 1213871 : && (TYPE_PRECISION (TREE_TYPE (param))
6350 : 466201 : == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
6351 : : {
6352 : : /* Strip coercion. */
6353 : 798802 : check_function_arguments_recurse (callback, ctx,
6354 : 399401 : TREE_OPERAND (param, 0), param_num,
6355 : : opt);
6356 : 399401 : return;
6357 : : }
6358 : :
6359 : 700329 : if (TREE_CODE (param) == CALL_EXPR && CALL_EXPR_FN (param))
6360 : : {
6361 : 7853 : tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
6362 : 7853 : tree attrs;
6363 : 7853 : bool found_format_arg = false;
6364 : :
6365 : : /* See if this is a call to a known internationalization function
6366 : : that modifies a format arg. Such a function may have multiple
6367 : : format_arg attributes (for example, ngettext). */
6368 : :
6369 : 7853 : for (attrs = TYPE_ATTRIBUTES (type);
6370 : 8235 : attrs;
6371 : 382 : attrs = TREE_CHAIN (attrs))
6372 : 382 : if (is_attribute_p ("format_arg", get_attribute_name (attrs)))
6373 : : {
6374 : 226 : tree inner_arg;
6375 : 226 : tree format_num_expr;
6376 : 226 : int format_num;
6377 : 226 : int i;
6378 : 226 : call_expr_arg_iterator iter;
6379 : :
6380 : : /* Extract the argument number, which was previously checked
6381 : : to be valid. */
6382 : 226 : format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
6383 : :
6384 : 226 : format_num = tree_to_uhwi (format_num_expr);
6385 : :
6386 : 226 : for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
6387 : 320 : inner_arg != NULL_TREE;
6388 : 94 : inner_arg = next_call_expr_arg (&iter), i++)
6389 : 320 : if (i == format_num)
6390 : : {
6391 : 226 : check_function_arguments_recurse (callback, ctx,
6392 : : inner_arg, param_num,
6393 : : opt);
6394 : 226 : found_format_arg = true;
6395 : 226 : break;
6396 : : }
6397 : : }
6398 : :
6399 : : /* If we found a format_arg attribute and did a recursive check,
6400 : : we are done with checking this argument. Otherwise, we continue
6401 : : and this will be considered a non-literal. */
6402 : 7853 : if (found_format_arg)
6403 : : return;
6404 : : }
6405 : :
6406 : 700151 : if (TREE_CODE (param) == COND_EXPR)
6407 : : {
6408 : : /* Simplify to avoid warning for an impossible case. */
6409 : 711 : param = fold_for_warn (param);
6410 : 711 : if (TREE_CODE (param) == COND_EXPR)
6411 : : {
6412 : : /* Check both halves of the conditional expression. */
6413 : 693 : check_function_arguments_recurse (callback, ctx,
6414 : 693 : TREE_OPERAND (param, 1),
6415 : : param_num, opt);
6416 : 1386 : check_function_arguments_recurse (callback, ctx,
6417 : 693 : TREE_OPERAND (param, 2),
6418 : : param_num, opt);
6419 : 693 : return;
6420 : : }
6421 : : }
6422 : :
6423 : 699458 : (*callback) (ctx, param, param_num);
6424 : : }
6425 : :
6426 : : /* Checks for a builtin function FNDECL that the number of arguments
6427 : : NARGS against the required number REQUIRED and issues an error if
6428 : : there is a mismatch. Returns true if the number of arguments is
6429 : : correct, otherwise false. LOC is the location of FNDECL. */
6430 : :
6431 : : static bool
6432 : 753828 : builtin_function_validate_nargs (location_t loc, tree fndecl, int nargs,
6433 : : int required, bool complain)
6434 : : {
6435 : 753828 : if (nargs < required)
6436 : : {
6437 : 182 : if (complain)
6438 : 143 : error_at (loc, "too few arguments to function %qE", fndecl);
6439 : 182 : return false;
6440 : : }
6441 : 753646 : else if (nargs > required)
6442 : : {
6443 : 138 : if (complain)
6444 : 93 : error_at (loc, "too many arguments to function %qE", fndecl);
6445 : 138 : return false;
6446 : : }
6447 : : return true;
6448 : : }
6449 : :
6450 : : /* Helper macro for check_builtin_function_arguments. */
6451 : : #define ARG_LOCATION(N) \
6452 : : (arg_loc.is_empty () \
6453 : : ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
6454 : : : expansion_point_location (arg_loc[(N)]))
6455 : :
6456 : : /* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
6457 : : Returns false if there was an error, otherwise true. LOC is the
6458 : : location of the function; ARG_LOC is a vector of locations of the
6459 : : arguments. If FNDECL is the result of resolving an overloaded
6460 : : target built-in, ORIG_FNDECL is the original function decl,
6461 : : otherwise it is null. */
6462 : :
6463 : : bool
6464 : 39225690 : check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
6465 : : tree fndecl, tree orig_fndecl, int nargs,
6466 : : tree *args, bool complain)
6467 : : {
6468 : 39225690 : if (!fndecl_built_in_p (fndecl))
6469 : : return true;
6470 : :
6471 : 39225690 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_MD)
6472 : 32045416 : return (!targetm.check_builtin_call
6473 : 32045416 : || targetm.check_builtin_call (loc, arg_loc, fndecl, orig_fndecl,
6474 : : nargs, args, complain));
6475 : :
6476 : 7180274 : if (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_FRONTEND)
6477 : : return true;
6478 : :
6479 : 7155992 : gcc_assert (DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL);
6480 : 7155992 : switch (DECL_FUNCTION_CODE (fndecl))
6481 : : {
6482 : 2 : case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX:
6483 : 2 : if (!tree_fits_uhwi_p (args[2]))
6484 : : {
6485 : 0 : if (complain)
6486 : 0 : error_at (
6487 : 0 : ARG_LOCATION (2),
6488 : : "third argument to function %qE must be a constant integer",
6489 : : fndecl);
6490 : 0 : return false;
6491 : : }
6492 : : /* fall through */
6493 : :
6494 : 264 : case BUILT_IN_ALLOCA_WITH_ALIGN:
6495 : 264 : {
6496 : : /* Get the requested alignment (in bits) if it's a constant
6497 : : integer expression. */
6498 : 264 : unsigned HOST_WIDE_INT align
6499 : 264 : = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
6500 : :
6501 : : /* Determine if the requested alignment is a power of 2. */
6502 : 264 : if ((align & (align - 1)))
6503 : : align = 0;
6504 : :
6505 : : /* The maximum alignment in bits corresponding to the same
6506 : : maximum in bytes enforced in check_user_alignment(). */
6507 : 315 : unsigned maxalign = (UINT_MAX >> 1) + 1;
6508 : :
6509 : : /* Reject invalid alignments. */
6510 : 181 : if (align < BITS_PER_UNIT || maxalign < align)
6511 : : {
6512 : 134 : if (complain)
6513 : 169 : error_at (ARG_LOCATION (1),
6514 : : "second argument to function %qE must be a constant "
6515 : : "integer power of 2 between %qi and %qu bits",
6516 : : fndecl, BITS_PER_UNIT, maxalign);
6517 : 134 : return false;
6518 : : }
6519 : : return true;
6520 : : }
6521 : :
6522 : 92781 : case BUILT_IN_CONSTANT_P:
6523 : 92781 : return builtin_function_validate_nargs (loc, fndecl, nargs, 1, complain);
6524 : :
6525 : 267294 : case BUILT_IN_ISFINITE:
6526 : 267294 : case BUILT_IN_ISINF:
6527 : 267294 : case BUILT_IN_ISINF_SIGN:
6528 : 267294 : case BUILT_IN_ISNAN:
6529 : 267294 : case BUILT_IN_ISNORMAL:
6530 : 267294 : case BUILT_IN_ISSIGNALING:
6531 : 267294 : case BUILT_IN_SIGNBIT:
6532 : 267294 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 1, complain))
6533 : : {
6534 : 267259 : if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
6535 : : {
6536 : 37 : if (complain)
6537 : 28 : error_at (ARG_LOCATION (0),
6538 : : "non-floating-point argument in "
6539 : : "call to function %qE",
6540 : : fndecl);
6541 : 37 : return false;
6542 : : }
6543 : : return true;
6544 : : }
6545 : : return false;
6546 : :
6547 : 223472 : case BUILT_IN_ISGREATER:
6548 : 223472 : case BUILT_IN_ISGREATEREQUAL:
6549 : 223472 : case BUILT_IN_ISLESS:
6550 : 223472 : case BUILT_IN_ISLESSEQUAL:
6551 : 223472 : case BUILT_IN_ISLESSGREATER:
6552 : 223472 : case BUILT_IN_ISUNORDERED:
6553 : 223472 : case BUILT_IN_ISEQSIG:
6554 : 223472 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 2, complain))
6555 : : {
6556 : 223437 : enum tree_code code0, code1;
6557 : 223437 : code0 = TREE_CODE (TREE_TYPE (args[0]));
6558 : 223437 : code1 = TREE_CODE (TREE_TYPE (args[1]));
6559 : 223437 : if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
6560 : 147 : || (code0 == REAL_TYPE
6561 : 56 : && (code1 == INTEGER_TYPE || code1 == BITINT_TYPE))
6562 : 92 : || ((code0 == INTEGER_TYPE || code0 == BITINT_TYPE)
6563 : 81 : && code1 == REAL_TYPE)))
6564 : : {
6565 : 39 : if (complain)
6566 : 18 : error_at (loc,
6567 : : "non-floating-point arguments in call to "
6568 : : "function %qE",
6569 : : fndecl);
6570 : 39 : return false;
6571 : : }
6572 : : return true;
6573 : : }
6574 : : return false;
6575 : :
6576 : 37941 : case BUILT_IN_FPCLASSIFY:
6577 : 37941 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 6, complain))
6578 : : {
6579 : 227574 : for (unsigned int i = 0; i < 5; i++)
6580 : 189649 : if (TREE_CODE (args[i]) != INTEGER_CST)
6581 : : {
6582 : 12 : if (complain)
6583 : 12 : error_at (ARG_LOCATION (i),
6584 : : "non-const integer argument %u in "
6585 : : "call to function %qE",
6586 : : i + 1, fndecl);
6587 : 12 : return false;
6588 : : }
6589 : :
6590 : 37925 : if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
6591 : : {
6592 : 2 : if (complain)
6593 : 4 : error_at (ARG_LOCATION (5),
6594 : : "non-floating-point argument in "
6595 : : "call to function %qE",
6596 : : fndecl);
6597 : 2 : return false;
6598 : : }
6599 : : return true;
6600 : : }
6601 : : return false;
6602 : :
6603 : 414 : case BUILT_IN_ASSUME_ALIGNED:
6604 : 808 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 2 + (nargs > 2),
6605 : : complain))
6606 : : {
6607 : 412 : if (nargs >= 3
6608 : 18 : && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE
6609 : 419 : && TREE_CODE (TREE_TYPE (args[2])) != BITINT_TYPE)
6610 : : {
6611 : 6 : if (complain)
6612 : 6 : error_at (ARG_LOCATION (2),
6613 : : "non-integer argument 3 in call to "
6614 : : "function %qE",
6615 : : fndecl);
6616 : 6 : return false;
6617 : : }
6618 : : return true;
6619 : : }
6620 : : return false;
6621 : :
6622 : 73363 : case BUILT_IN_ADD_OVERFLOW:
6623 : 73363 : case BUILT_IN_SUB_OVERFLOW:
6624 : 73363 : case BUILT_IN_MUL_OVERFLOW:
6625 : 73363 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 3, complain))
6626 : : {
6627 : : unsigned i;
6628 : 219633 : for (i = 0; i < 2; i++)
6629 : 146448 : if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
6630 : : {
6631 : 55 : if (complain)
6632 : 77 : error_at (ARG_LOCATION (i),
6633 : : "argument %u in call to function "
6634 : : "%qE does not have integral type",
6635 : : i + 1, fndecl);
6636 : 55 : return false;
6637 : : }
6638 : 73185 : if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
6639 : 73185 : || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args[2]))))
6640 : : {
6641 : 60 : if (complain)
6642 : 75 : error_at (ARG_LOCATION (2),
6643 : : "argument 3 in call to function %qE "
6644 : : "does not have pointer to integral type",
6645 : : fndecl);
6646 : 60 : return false;
6647 : : }
6648 : 73125 : else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == ENUMERAL_TYPE)
6649 : : {
6650 : 16 : if (complain)
6651 : 11 : error_at (ARG_LOCATION (2),
6652 : : "argument 3 in call to function %qE "
6653 : : "has pointer to enumerated type",
6654 : : fndecl);
6655 : 16 : return false;
6656 : : }
6657 : 73109 : else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == BOOLEAN_TYPE)
6658 : : {
6659 : 21 : if (complain)
6660 : 16 : error_at (ARG_LOCATION (2),
6661 : : "argument 3 in call to function %qE "
6662 : : "has pointer to boolean type",
6663 : : fndecl);
6664 : 21 : return false;
6665 : : }
6666 : 73088 : else if (TYPE_READONLY (TREE_TYPE (TREE_TYPE (args[2]))))
6667 : : {
6668 : 30 : if (complain)
6669 : 30 : error_at (ARG_LOCATION (2),
6670 : : "argument %u in call to function %qE "
6671 : : "has pointer to %qs type (%qT)",
6672 : 21 : 3, fndecl, "const", TREE_TYPE (args[2]));
6673 : 30 : return false;
6674 : : }
6675 : 73058 : else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (args[2]))))
6676 : : {
6677 : 6 : if (complain)
6678 : 12 : error_at (ARG_LOCATION (2),
6679 : : "argument %u in call to function %qE "
6680 : : "has pointer to %qs type (%qT)",
6681 : 6 : 3, fndecl, "_Atomic", TREE_TYPE (args[2]));
6682 : 6 : return false;
6683 : : }
6684 : : return true;
6685 : : }
6686 : : return false;
6687 : :
6688 : 41854 : case BUILT_IN_ADD_OVERFLOW_P:
6689 : 41854 : case BUILT_IN_SUB_OVERFLOW_P:
6690 : 41854 : case BUILT_IN_MUL_OVERFLOW_P:
6691 : 41854 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 3, complain))
6692 : : {
6693 : : unsigned i;
6694 : 167205 : for (i = 0; i < 3; i++)
6695 : 125421 : if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
6696 : : {
6697 : 37 : if (complain)
6698 : 35 : error_at (ARG_LOCATION (i),
6699 : : "argument %u in call to function "
6700 : : "%qE does not have integral type",
6701 : : i + 1, fndecl);
6702 : 37 : return false;
6703 : : }
6704 : 41784 : if (TREE_CODE (TREE_TYPE (args[2])) == ENUMERAL_TYPE)
6705 : : {
6706 : 13 : if (complain)
6707 : 5 : error_at (ARG_LOCATION (2),
6708 : : "argument %u in call to function "
6709 : : "%qE has enumerated type",
6710 : : 3, fndecl);
6711 : 13 : return false;
6712 : : }
6713 : 41771 : else if (TREE_CODE (TREE_TYPE (args[2])) == BOOLEAN_TYPE)
6714 : : {
6715 : 13 : if (complain)
6716 : 5 : error_at (ARG_LOCATION (2),
6717 : : "argument %u in call to function "
6718 : : "%qE has boolean type",
6719 : : 3, fndecl);
6720 : 13 : return false;
6721 : : }
6722 : : return true;
6723 : : }
6724 : : return false;
6725 : :
6726 : 9694 : case BUILT_IN_CLEAR_PADDING:
6727 : 9694 : if (builtin_function_validate_nargs (loc, fndecl, nargs, 1, complain))
6728 : : {
6729 : 9679 : if (!POINTER_TYPE_P (TREE_TYPE (args[0])))
6730 : : {
6731 : 7 : if (complain)
6732 : 5 : error_at (ARG_LOCATION (0),
6733 : : "argument %u in call to function "
6734 : : "%qE does not have pointer type",
6735 : : 1, fndecl);
6736 : 7 : return false;
6737 : : }
6738 : 9672 : else if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (args[0]))))
6739 : : {
6740 : 11 : if (complain)
6741 : 10 : error_at (ARG_LOCATION (0),
6742 : : "argument %u in call to function "
6743 : : "%qE points to incomplete type",
6744 : : 1, fndecl);
6745 : 11 : return false;
6746 : : }
6747 : 9661 : else if (TYPE_READONLY (TREE_TYPE (TREE_TYPE (args[0]))))
6748 : : {
6749 : 7 : if (complain)
6750 : 5 : error_at (ARG_LOCATION (0),
6751 : : "argument %u in call to function %qE "
6752 : : "has pointer to %qs type (%qT)",
6753 : 4 : 1, fndecl, "const", TREE_TYPE (args[0]));
6754 : 7 : return false;
6755 : : }
6756 : 9654 : else if (TYPE_ATOMIC (TREE_TYPE (TREE_TYPE (args[0]))))
6757 : : {
6758 : 0 : if (complain)
6759 : 0 : error_at (ARG_LOCATION (0),
6760 : : "argument %u in call to function %qE "
6761 : : "has pointer to %qs type (%qT)",
6762 : 0 : 1, fndecl, "_Atomic", TREE_TYPE (args[0]));
6763 : 0 : return false;
6764 : : }
6765 : : return true;
6766 : : }
6767 : : return false;
6768 : :
6769 : 55223 : case BUILT_IN_CLZG:
6770 : 55223 : case BUILT_IN_CTZG:
6771 : 55223 : case BUILT_IN_CLRSBG:
6772 : 55223 : case BUILT_IN_FFSG:
6773 : 55223 : case BUILT_IN_PARITYG:
6774 : 55223 : case BUILT_IN_POPCOUNTG:
6775 : 55223 : if (nargs == 2
6776 : 55223 : && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CLZG
6777 : 14870 : || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CTZG))
6778 : : {
6779 : 48208 : if (!INTEGRAL_TYPE_P (TREE_TYPE (args[1])))
6780 : : {
6781 : 18 : if (complain)
6782 : 15 : error_at (ARG_LOCATION (1),
6783 : : "argument %u in call to function "
6784 : : "%qE does not have integral type",
6785 : : 2, fndecl);
6786 : 18 : return false;
6787 : : }
6788 : 48190 : if ((TYPE_PRECISION (TREE_TYPE (args[1]))
6789 : 48190 : > TYPE_PRECISION (integer_type_node))
6790 : 48190 : || (TYPE_PRECISION (TREE_TYPE (args[1]))
6791 : 48176 : == TYPE_PRECISION (integer_type_node)
6792 : 48170 : && TYPE_UNSIGNED (TREE_TYPE (args[1]))))
6793 : : {
6794 : 28 : if (complain)
6795 : 26 : error_at (ARG_LOCATION (1),
6796 : : "argument %u in call to function "
6797 : : "%qE does not have %<int%> type",
6798 : : 2, fndecl);
6799 : 28 : return false;
6800 : : }
6801 : : }
6802 : 7015 : else if (!builtin_function_validate_nargs (loc, fndecl, nargs, 1,
6803 : : complain))
6804 : : return false;
6805 : :
6806 : 55111 : if (!INTEGRAL_TYPE_P (TREE_TYPE (args[0])))
6807 : : {
6808 : 82 : if (complain)
6809 : 80 : error_at (ARG_LOCATION (0),
6810 : : "argument %u in call to function "
6811 : : "%qE does not have integral type",
6812 : : 1, fndecl);
6813 : 82 : return false;
6814 : : }
6815 : 55029 : if (TREE_CODE (TREE_TYPE (args[0])) == ENUMERAL_TYPE)
6816 : : {
6817 : 42 : if (complain)
6818 : 24 : error_at (ARG_LOCATION (0),
6819 : : "argument %u in call to function "
6820 : : "%qE has enumerated type",
6821 : : 1, fndecl);
6822 : 42 : return false;
6823 : : }
6824 : 54987 : if (TREE_CODE (TREE_TYPE (args[0])) == BOOLEAN_TYPE)
6825 : : {
6826 : 50 : if (complain)
6827 : 40 : error_at (ARG_LOCATION (0),
6828 : : "argument %u in call to function "
6829 : : "%qE has boolean type",
6830 : : 1, fndecl);
6831 : 50 : return false;
6832 : : }
6833 : 54937 : if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_FFSG
6834 : 54937 : || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CLRSBG)
6835 : : {
6836 : 288 : if (TYPE_UNSIGNED (TREE_TYPE (args[0])))
6837 : : {
6838 : 14 : if (complain)
6839 : 10 : error_at (ARG_LOCATION (0),
6840 : : "argument 1 in call to function "
6841 : : "%qE has unsigned type",
6842 : : fndecl);
6843 : 14 : return false;
6844 : : }
6845 : : }
6846 : 54649 : else if (!TYPE_UNSIGNED (TREE_TYPE (args[0])))
6847 : : {
6848 : 42 : if (complain)
6849 : 42 : error_at (ARG_LOCATION (0),
6850 : : "argument 1 in call to function "
6851 : : "%qE has signed type",
6852 : : fndecl);
6853 : 42 : return false;
6854 : : }
6855 : : return true;
6856 : :
6857 : : default:
6858 : : return true;
6859 : : }
6860 : : }
6861 : :
6862 : : /* Subroutine of c_parse_error.
6863 : : Return the result of concatenating LHS and RHS. RHS is really
6864 : : a string literal, its first character is indicated by RHS_START and
6865 : : RHS_SIZE is its length (including the terminating NUL character).
6866 : :
6867 : : The caller is responsible for deleting the returned pointer. */
6868 : :
6869 : : static char *
6870 : 8719 : catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
6871 : : {
6872 : 8719 : const size_t lhs_size = strlen (lhs);
6873 : 8719 : char *result = XNEWVEC (char, lhs_size + rhs_size);
6874 : 8719 : memcpy (result, lhs, lhs_size);
6875 : 8719 : memcpy (result + lhs_size, rhs_start, rhs_size);
6876 : 8719 : return result;
6877 : : }
6878 : :
6879 : : /* Issue the error given by GMSGID at RICHLOC, indicating that it occurred
6880 : : before TOKEN, which had the associated VALUE. */
6881 : :
6882 : : void
6883 : 8733 : c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
6884 : : tree value, unsigned char token_flags,
6885 : : rich_location *richloc)
6886 : : {
6887 : : #define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
6888 : :
6889 : 8733 : char *message = NULL;
6890 : :
6891 : 8733 : if (token_type == CPP_EOF)
6892 : 471 : message = catenate_messages (gmsgid, " at end of input");
6893 : 8262 : else if (token_type == CPP_CHAR
6894 : : || token_type == CPP_WCHAR
6895 : : || token_type == CPP_CHAR16
6896 : : || token_type == CPP_CHAR32
6897 : 8262 : || token_type == CPP_UTF8CHAR)
6898 : : {
6899 : 24 : unsigned int val = TREE_INT_CST_LOW (value);
6900 : 24 : const char *prefix;
6901 : :
6902 : 24 : switch (token_type)
6903 : : {
6904 : : default:
6905 : : prefix = "";
6906 : : break;
6907 : 0 : case CPP_WCHAR:
6908 : 0 : prefix = "L";
6909 : 0 : break;
6910 : 0 : case CPP_CHAR16:
6911 : 0 : prefix = "u";
6912 : 0 : break;
6913 : 0 : case CPP_CHAR32:
6914 : 0 : prefix = "U";
6915 : 0 : break;
6916 : 0 : case CPP_UTF8CHAR:
6917 : 0 : prefix = "u8";
6918 : 0 : break;
6919 : : }
6920 : :
6921 : 24 : if (val <= UCHAR_MAX && ISGRAPH (val))
6922 : 20 : message = catenate_messages (gmsgid, " before %s'%c'");
6923 : : else
6924 : 4 : message = catenate_messages (gmsgid, " before %s'\\x%x'");
6925 : :
6926 : 24 : error_at (richloc, message, prefix, val);
6927 : 24 : free (message);
6928 : 24 : message = NULL;
6929 : : }
6930 : 8238 : else if (token_type == CPP_CHAR_USERDEF
6931 : : || token_type == CPP_WCHAR_USERDEF
6932 : : || token_type == CPP_CHAR16_USERDEF
6933 : : || token_type == CPP_CHAR32_USERDEF
6934 : 8238 : || token_type == CPP_UTF8CHAR_USERDEF)
6935 : 12 : message = catenate_messages (gmsgid,
6936 : : " before user-defined character literal");
6937 : 8226 : else if (token_type == CPP_STRING_USERDEF
6938 : : || token_type == CPP_WSTRING_USERDEF
6939 : : || token_type == CPP_STRING16_USERDEF
6940 : : || token_type == CPP_STRING32_USERDEF
6941 : 8226 : || token_type == CPP_UTF8STRING_USERDEF)
6942 : 25 : message = catenate_messages (gmsgid, " before user-defined string literal");
6943 : 8201 : else if (token_type == CPP_STRING
6944 : : || token_type == CPP_WSTRING
6945 : : || token_type == CPP_STRING16
6946 : : || token_type == CPP_STRING32
6947 : 8201 : || token_type == CPP_UTF8STRING)
6948 : 44 : message = catenate_messages (gmsgid, " before string constant");
6949 : 8157 : else if (token_type == CPP_NUMBER)
6950 : 528 : message = catenate_messages (gmsgid, " before numeric constant");
6951 : 7629 : else if (token_type == CPP_NAME)
6952 : : {
6953 : 2080 : message = catenate_messages (gmsgid, " before %qE");
6954 : 2080 : error_at (richloc, message, value);
6955 : 2080 : free (message);
6956 : 2080 : message = NULL;
6957 : : }
6958 : 5549 : else if (token_type == CPP_PRAGMA)
6959 : 20 : message = catenate_messages (gmsgid, " before %<#pragma%>");
6960 : 5529 : else if (token_type == CPP_PRAGMA_EOL)
6961 : 466 : message = catenate_messages (gmsgid, " before end of line");
6962 : 5063 : else if (token_type == CPP_DECLTYPE)
6963 : 12 : message = catenate_messages (gmsgid, " before %<decltype%>");
6964 : 5051 : else if (token_type == CPP_EMBED)
6965 : 0 : message = catenate_messages (gmsgid, " before %<#embed%>");
6966 : 5051 : else if (token_type < N_TTYPES)
6967 : : {
6968 : 5037 : message = catenate_messages (gmsgid, " before %qs token");
6969 : 5037 : error_at (richloc, message, cpp_type2name (token_type, token_flags));
6970 : 5037 : free (message);
6971 : 5037 : message = NULL;
6972 : : }
6973 : : else
6974 : 14 : error_at (richloc, gmsgid);
6975 : :
6976 : 8733 : if (message)
6977 : : {
6978 : 1578 : error_at (richloc, message);
6979 : 1578 : free (message);
6980 : : }
6981 : : #undef catenate_messages
6982 : 8733 : }
6983 : :
6984 : : /* Return the gcc option code associated with the reason for a cpp
6985 : : message, or 0 if none. */
6986 : :
6987 : : static diagnostic_option_id
6988 : 142622 : c_option_controlling_cpp_diagnostic (enum cpp_warning_reason reason)
6989 : : {
6990 : 142622 : const struct cpp_reason_option_codes_t *entry;
6991 : :
6992 : 3974918 : for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
6993 : : {
6994 : 3968971 : if (entry->reason == reason)
6995 : 136675 : return entry->option_code;
6996 : : }
6997 : 0 : return 0;
6998 : : }
6999 : :
7000 : : /* Return TRUE if the given option index corresponds to a diagnostic
7001 : : issued by libcpp. Linear search seems fine for now. */
7002 : : bool
7003 : 1999221 : c_option_is_from_cpp_diagnostics (int option_index)
7004 : : {
7005 : 46273158 : for (auto entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE;
7006 : : ++entry)
7007 : : {
7008 : 45528341 : if (entry->option_code == option_index)
7009 : : return true;
7010 : : }
7011 : : return false;
7012 : : }
7013 : :
7014 : : /* Callback from cpp_diagnostic for PFILE to print diagnostics from the
7015 : : preprocessor. The diagnostic is of type LEVEL, with REASON set
7016 : : to the reason code if LEVEL is represents a warning, at location
7017 : : RICHLOC unless this is after lexing and the compiler's location
7018 : : should be used instead; MSG is the translated message and AP
7019 : : the arguments. Returns true if a diagnostic was emitted, false
7020 : : otherwise. */
7021 : :
7022 : : bool
7023 : 142632 : c_cpp_diagnostic (cpp_reader *pfile ATTRIBUTE_UNUSED,
7024 : : enum cpp_diagnostic_level level,
7025 : : enum cpp_warning_reason reason,
7026 : : rich_location *richloc,
7027 : : const char *msg, va_list *ap)
7028 : : {
7029 : 142632 : diagnostic_info diagnostic;
7030 : 142632 : diagnostic_t dlevel;
7031 : 142632 : bool save_warn_system_headers = global_dc->m_warn_system_headers;
7032 : 142632 : bool ret;
7033 : :
7034 : 142632 : switch (level)
7035 : : {
7036 : 99 : case CPP_DL_WARNING_SYSHDR:
7037 : 99 : if (flag_no_output)
7038 : : return false;
7039 : 98 : global_dc->m_warn_system_headers = 1;
7040 : : /* Fall through. */
7041 : 24168 : case CPP_DL_WARNING:
7042 : 24168 : if (flag_no_output)
7043 : : return false;
7044 : : dlevel = DK_WARNING;
7045 : : break;
7046 : 113738 : case CPP_DL_PEDWARN:
7047 : 113738 : if (flag_no_output && !flag_pedantic_errors)
7048 : : return false;
7049 : : dlevel = DK_PEDWARN;
7050 : : break;
7051 : : case CPP_DL_ERROR:
7052 : : dlevel = DK_ERROR;
7053 : : break;
7054 : 0 : case CPP_DL_ICE:
7055 : 0 : dlevel = DK_ICE;
7056 : 0 : break;
7057 : 318 : case CPP_DL_NOTE:
7058 : 318 : dlevel = DK_NOTE;
7059 : 318 : break;
7060 : 353 : case CPP_DL_FATAL:
7061 : 353 : dlevel = DK_FATAL;
7062 : 353 : break;
7063 : 0 : default:
7064 : 0 : gcc_unreachable ();
7065 : : }
7066 : 142622 : if (override_libcpp_locations)
7067 : 247 : richloc->set_range (0, input_location, SHOW_RANGE_WITH_CARET);
7068 : 142622 : diagnostic_set_info_translated (&diagnostic, msg, ap,
7069 : : richloc, dlevel);
7070 : 285244 : diagnostic_set_option_id (&diagnostic,
7071 : : c_option_controlling_cpp_diagnostic (reason));
7072 : 142622 : ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
7073 : 142269 : if (level == CPP_DL_WARNING_SYSHDR)
7074 : 98 : global_dc->m_warn_system_headers = save_warn_system_headers;
7075 : : return ret;
7076 : 142279 : }
7077 : :
7078 : : /* Convert a character from the host to the target execution character
7079 : : set. cpplib handles this, mostly. */
7080 : :
7081 : : HOST_WIDE_INT
7082 : 6470106 : c_common_to_target_charset (HOST_WIDE_INT c)
7083 : : {
7084 : : /* Character constants in GCC proper are sign-extended under -fsigned-char,
7085 : : zero-extended under -fno-signed-char. cpplib insists that characters
7086 : : and character constants are always unsigned. Hence we must convert
7087 : : back and forth. */
7088 : 6470106 : cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
7089 : :
7090 : 6470106 : uc = cpp_host_to_exec_charset (parse_in, uc);
7091 : :
7092 : 6470106 : if (flag_signed_char)
7093 : 6470009 : return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
7094 : 6470009 : >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
7095 : : else
7096 : 97 : return uc;
7097 : : }
7098 : :
7099 : : /* Fold an offsetof-like expression. EXPR is a nested sequence of component
7100 : : references with an INDIRECT_REF of a constant at the bottom; much like the
7101 : : traditional rendering of offsetof as a macro. TYPE is the desired type of
7102 : : the whole expression. Return the folded result. */
7103 : :
7104 : : tree
7105 : 15071 : fold_offsetof (tree expr, tree type, enum tree_code ctx)
7106 : : {
7107 : 15080 : tree base, off, t;
7108 : 15080 : tree_code code = TREE_CODE (expr);
7109 : 15080 : switch (code)
7110 : : {
7111 : : case ERROR_MARK:
7112 : : return expr;
7113 : :
7114 : 12 : case VAR_DECL:
7115 : 12 : error ("cannot apply %<offsetof%> to static data member %qD", expr);
7116 : 12 : return error_mark_node;
7117 : :
7118 : 9 : case CALL_EXPR:
7119 : 9 : case TARGET_EXPR:
7120 : 9 : error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
7121 : 9 : return error_mark_node;
7122 : :
7123 : 6621 : case NOP_EXPR:
7124 : 6621 : case INDIRECT_REF:
7125 : 6621 : if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
7126 : : {
7127 : 17 : error ("cannot apply %<offsetof%> to a non constant address");
7128 : 17 : return error_mark_node;
7129 : : }
7130 : 6604 : return convert (type, TREE_OPERAND (expr, 0));
7131 : :
7132 : 7206 : case COMPONENT_REF:
7133 : 7206 : base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
7134 : 7206 : if (base == error_mark_node)
7135 : : return base;
7136 : :
7137 : 7203 : t = TREE_OPERAND (expr, 1);
7138 : 7203 : if (DECL_C_BIT_FIELD (t))
7139 : : {
7140 : 1 : error ("attempt to take address of bit-field structure "
7141 : : "member %qD", t);
7142 : 1 : return error_mark_node;
7143 : : }
7144 : 7202 : off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
7145 : 7202 : size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
7146 : : / BITS_PER_UNIT));
7147 : 7202 : break;
7148 : :
7149 : 1222 : case ARRAY_REF:
7150 : 1222 : base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
7151 : 1222 : if (base == error_mark_node)
7152 : : return base;
7153 : :
7154 : 1219 : t = TREE_OPERAND (expr, 1);
7155 : 1219 : STRIP_ANY_LOCATION_WRAPPER (t);
7156 : :
7157 : : /* Check if the offset goes beyond the upper bound of the array. */
7158 : 1219 : if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
7159 : : {
7160 : 956 : tree upbound = array_ref_up_bound (expr);
7161 : 956 : if (upbound != NULL_TREE
7162 : 844 : && TREE_CODE (upbound) == INTEGER_CST
7163 : 1800 : && !tree_int_cst_equal (upbound,
7164 : 844 : TYPE_MAX_VALUE (TREE_TYPE (upbound))))
7165 : : {
7166 : 835 : if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
7167 : 433 : upbound = size_binop (PLUS_EXPR, upbound,
7168 : : build_int_cst (TREE_TYPE (upbound), 1));
7169 : 835 : if (tree_int_cst_lt (upbound, t))
7170 : : {
7171 : 244 : tree v;
7172 : :
7173 : 244 : for (v = TREE_OPERAND (expr, 0);
7174 : 420 : TREE_CODE (v) == COMPONENT_REF;
7175 : 176 : v = TREE_OPERAND (v, 0))
7176 : 280 : if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
7177 : : == RECORD_TYPE)
7178 : : {
7179 : 220 : tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
7180 : 307 : for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
7181 : 191 : if (TREE_CODE (fld_chain) == FIELD_DECL)
7182 : : break;
7183 : :
7184 : : if (fld_chain)
7185 : : break;
7186 : : }
7187 : : /* Don't warn if the array might be considered a poor
7188 : : man's flexible array member with a very permissive
7189 : : definition thereof. */
7190 : 244 : if (TREE_CODE (v) == ARRAY_REF
7191 : 172 : || TREE_CODE (v) == COMPONENT_REF)
7192 : 176 : warning (OPT_Warray_bounds_,
7193 : : "index %E denotes an offset "
7194 : : "greater than size of %qT",
7195 : 176 : t, TREE_TYPE (TREE_OPERAND (expr, 0)));
7196 : : }
7197 : : }
7198 : : }
7199 : :
7200 : 1219 : t = convert (sizetype, t);
7201 : 1219 : off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
7202 : 1219 : break;
7203 : :
7204 : 9 : case COMPOUND_EXPR:
7205 : : /* Handle static members of volatile structs. */
7206 : 9 : t = TREE_OPERAND (expr, 1);
7207 : 9 : gcc_checking_assert (VAR_P (get_base_address (t)));
7208 : : return fold_offsetof (t, type);
7209 : :
7210 : 0 : default:
7211 : 0 : gcc_unreachable ();
7212 : : }
7213 : :
7214 : 8421 : if (!POINTER_TYPE_P (type))
7215 : 7552 : return size_binop (PLUS_EXPR, base, convert (type, off));
7216 : 869 : return fold_build_pointer_plus (base, off);
7217 : : }
7218 : :
7219 : : /* *PTYPE is an incomplete array. Complete it with a domain based on
7220 : : INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
7221 : : is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
7222 : : 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
7223 : :
7224 : : int
7225 : 522329 : complete_array_type (tree *ptype, tree initial_value, bool do_default)
7226 : : {
7227 : 522329 : tree maxindex, type, main_type, elt, unqual_elt;
7228 : 522329 : int failure = 0, quals;
7229 : 522329 : bool overflow_p = false;
7230 : :
7231 : 522329 : maxindex = size_zero_node;
7232 : 522329 : if (initial_value)
7233 : : {
7234 : 514891 : STRIP_ANY_LOCATION_WRAPPER (initial_value);
7235 : :
7236 : 514891 : if (TREE_CODE (initial_value) == STRING_CST)
7237 : : {
7238 : 413905 : int eltsize
7239 : 413905 : = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
7240 : 413905 : maxindex = size_int (TREE_STRING_LENGTH (initial_value) / eltsize
7241 : : - 1);
7242 : : }
7243 : 100986 : else if (TREE_CODE (initial_value) == CONSTRUCTOR)
7244 : : {
7245 : 100084 : vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
7246 : :
7247 : 100084 : if (vec_safe_is_empty (v))
7248 : : {
7249 : 396 : if (pedantic)
7250 : 68 : failure = 3;
7251 : 396 : maxindex = ssize_int (-1);
7252 : : }
7253 : : else
7254 : : {
7255 : 99688 : tree curindex;
7256 : 99688 : unsigned HOST_WIDE_INT cnt = 1;
7257 : 99688 : constructor_elt *ce;
7258 : 99688 : bool fold_p = false;
7259 : :
7260 : 99688 : if ((*v)[0].index)
7261 : 87371 : maxindex = (*v)[0].index, fold_p = true;
7262 : 99688 : if (TREE_CODE ((*v)[0].value) == RAW_DATA_CST)
7263 : 4 : cnt = 0;
7264 : :
7265 : : curindex = maxindex;
7266 : :
7267 : 15751490 : for (; vec_safe_iterate (v, cnt, &ce); cnt++)
7268 : : {
7269 : 15651802 : bool curfold_p = false;
7270 : 15651802 : if (ce->index)
7271 : 14437954 : curindex = ce->index, curfold_p = true;
7272 : 15651802 : if (!ce->index || TREE_CODE (ce->value) == RAW_DATA_CST)
7273 : : {
7274 : 1214207 : if (fold_p || curfold_p)
7275 : : {
7276 : : /* Since we treat size types now as ordinary
7277 : : unsigned types, we need an explicit overflow
7278 : : check. */
7279 : 359 : tree orig = curindex;
7280 : 359 : curindex = fold_convert (sizetype, curindex);
7281 : 359 : overflow_p |= tree_int_cst_lt (curindex, orig);
7282 : 359 : curfold_p = false;
7283 : : }
7284 : 1214207 : if (TREE_CODE (ce->value) == RAW_DATA_CST)
7285 : 359 : curindex
7286 : 718 : = size_binop (PLUS_EXPR, curindex,
7287 : : size_int (RAW_DATA_LENGTH (ce->value)
7288 : : - ((ce->index || !cnt)
7289 : : ? 1 : 0)));
7290 : : else
7291 : 1213848 : curindex = size_binop (PLUS_EXPR, curindex,
7292 : : size_one_node);
7293 : : }
7294 : 15651802 : if (tree_int_cst_lt (maxindex, curindex))
7295 : 15651802 : maxindex = curindex, fold_p = curfold_p;
7296 : : }
7297 : 99688 : if (fold_p)
7298 : : {
7299 : 87367 : tree orig = maxindex;
7300 : 87367 : maxindex = fold_convert (sizetype, maxindex);
7301 : 87367 : overflow_p |= tree_int_cst_lt (maxindex, orig);
7302 : : }
7303 : : }
7304 : : }
7305 : : else
7306 : : {
7307 : : /* Make an error message unless that happened already. */
7308 : 902 : if (initial_value != error_mark_node)
7309 : 31 : failure = 1;
7310 : : }
7311 : : }
7312 : : else
7313 : : {
7314 : 7438 : failure = 2;
7315 : 7438 : if (!do_default)
7316 : : return failure;
7317 : : }
7318 : :
7319 : 514976 : type = *ptype;
7320 : 514976 : elt = TREE_TYPE (type);
7321 : 514976 : quals = TYPE_QUALS (strip_array_types (elt));
7322 : 514976 : if (quals == 0)
7323 : : unqual_elt = elt;
7324 : : else
7325 : 484527 : unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
7326 : :
7327 : : /* Using build_distinct_type_copy and modifying things afterward instead
7328 : : of using build_array_type to create a new type preserves all of the
7329 : : TYPE_LANG_FLAG_? bits that the front end may have set. */
7330 : 514976 : main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
7331 : 514976 : TREE_TYPE (main_type) = unqual_elt;
7332 : 1029952 : TYPE_DOMAIN (main_type)
7333 : 514976 : = build_range_type (TREE_TYPE (maxindex),
7334 : 514976 : build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
7335 : 514976 : TYPE_TYPELESS_STORAGE (main_type) = TYPE_TYPELESS_STORAGE (type);
7336 : 514976 : layout_type (main_type);
7337 : :
7338 : : /* Set TYPE_STRUCTURAL_EQUALITY_P early. */
7339 : 514976 : if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
7340 : 1029575 : || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
7341 : 377 : SET_TYPE_STRUCTURAL_EQUALITY (main_type);
7342 : : else
7343 : 514599 : TYPE_CANONICAL (main_type) = main_type;
7344 : :
7345 : : /* Make sure we have the canonical MAIN_TYPE. */
7346 : 514976 : hashval_t hashcode = type_hash_canon_hash (main_type);
7347 : 514976 : main_type = type_hash_canon (hashcode, main_type);
7348 : :
7349 : : /* Fix the canonical type. */
7350 : 514976 : if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
7351 : 1029575 : || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
7352 : 377 : gcc_assert (TYPE_STRUCTURAL_EQUALITY_P (main_type));
7353 : 514599 : else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
7354 : 514599 : || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
7355 : 498283 : != TYPE_DOMAIN (main_type)))
7356 : 16316 : TYPE_CANONICAL (main_type)
7357 : 32632 : = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
7358 : 16316 : TYPE_CANONICAL (TYPE_DOMAIN (main_type)),
7359 : 16316 : TYPE_TYPELESS_STORAGE (main_type));
7360 : :
7361 : 514976 : if (quals == 0)
7362 : : type = main_type;
7363 : : else
7364 : 484527 : type = c_build_qualified_type (main_type, quals);
7365 : :
7366 : 514976 : if (COMPLETE_TYPE_P (type)
7367 : 514967 : && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
7368 : 1029937 : && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
7369 : : {
7370 : 4 : error ("size of array is too large");
7371 : : /* If we proceed with the array type as it is, we'll eventually
7372 : : crash in tree_to_[su]hwi(). */
7373 : 4 : type = error_mark_node;
7374 : : }
7375 : :
7376 : 514976 : *ptype = type;
7377 : 514976 : return failure;
7378 : : }
7379 : :
7380 : : /* INIT is an constructor of a structure with a flexible array member.
7381 : : Complete the flexible array member with a domain based on it's value. */
7382 : : void
7383 : 8741291 : complete_flexible_array_elts (tree init)
7384 : : {
7385 : 8846259 : tree elt, type;
7386 : :
7387 : 8846259 : if (init == NULL_TREE || TREE_CODE (init) != CONSTRUCTOR)
7388 : : return;
7389 : :
7390 : 106468 : if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
7391 : : return;
7392 : :
7393 : 105221 : elt = CONSTRUCTOR_ELTS (init)->last ().value;
7394 : 105221 : type = TREE_TYPE (elt);
7395 : 105221 : if (TREE_CODE (type) == ARRAY_TYPE
7396 : 105221 : && TYPE_SIZE (type) == NULL_TREE)
7397 : 253 : complete_array_type (&TREE_TYPE (elt), elt, false);
7398 : : else
7399 : : complete_flexible_array_elts (elt);
7400 : : }
7401 : :
7402 : : /* Like c_mark_addressable but don't check register qualifier. */
7403 : : void
7404 : 1096310 : c_common_mark_addressable_vec (tree t)
7405 : : {
7406 : 1269163 : while (handled_component_p (t) || TREE_CODE (t) == C_MAYBE_CONST_EXPR)
7407 : : {
7408 : 172853 : if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
7409 : 26 : t = C_MAYBE_CONST_EXPR_EXPR (t);
7410 : : else
7411 : 172827 : t = TREE_OPERAND (t, 0);
7412 : : }
7413 : 1096310 : if (!VAR_P (t)
7414 : : && TREE_CODE (t) != PARM_DECL
7415 : : && TREE_CODE (t) != COMPOUND_LITERAL_EXPR
7416 : : && TREE_CODE (t) != TARGET_EXPR)
7417 : : return;
7418 : 1077063 : if (!VAR_P (t) || !DECL_HARD_REGISTER (t))
7419 : 1076994 : TREE_ADDRESSABLE (t) = 1;
7420 : 1077063 : if (TREE_CODE (t) == COMPOUND_LITERAL_EXPR)
7421 : 126 : TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (t)) = 1;
7422 : 1076937 : else if (TREE_CODE (t) == TARGET_EXPR)
7423 : 28 : TREE_ADDRESSABLE (TARGET_EXPR_SLOT (t)) = 1;
7424 : : }
7425 : :
7426 : :
7427 : :
7428 : : /* Used to help initialize the builtin-types.def table. When a type of
7429 : : the correct size doesn't exist, use error_mark_node instead of NULL.
7430 : : The later results in segfaults even when a decl using the type doesn't
7431 : : get invoked. */
7432 : :
7433 : : tree
7434 : 1107145 : builtin_type_for_size (int size, bool unsignedp)
7435 : : {
7436 : 1107145 : tree type = c_common_type_for_size (size, unsignedp);
7437 : 1107145 : return type ? type : error_mark_node;
7438 : : }
7439 : :
7440 : : /* Work out the size of the first argument of a call to
7441 : : __builtin_speculation_safe_value. Only pointers and integral types
7442 : : are permitted. Return -1 if the argument type is not supported or
7443 : : the size is too large; 0 if the argument type is a pointer or the
7444 : : size if it is integral. */
7445 : : static enum built_in_function
7446 : 154 : speculation_safe_value_resolve_call (tree function, vec<tree, va_gc> *params,
7447 : : bool complain)
7448 : : {
7449 : : /* Type of the argument. */
7450 : 154 : tree type;
7451 : 154 : int size;
7452 : :
7453 : 154 : if (vec_safe_is_empty (params))
7454 : : {
7455 : 10 : if (complain)
7456 : 10 : error ("too few arguments to function %qE", function);
7457 : 10 : return BUILT_IN_NONE;
7458 : : }
7459 : :
7460 : 144 : type = TREE_TYPE ((*params)[0]);
7461 : 144 : if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
7462 : : {
7463 : : /* Force array-to-pointer decay for C++. */
7464 : 0 : (*params)[0] = default_conversion ((*params)[0]);
7465 : 0 : type = TREE_TYPE ((*params)[0]);
7466 : : }
7467 : :
7468 : 144 : if (POINTER_TYPE_P (type))
7469 : : return BUILT_IN_SPECULATION_SAFE_VALUE_PTR;
7470 : :
7471 : 128 : if (!INTEGRAL_TYPE_P (type))
7472 : 42 : goto incompatible;
7473 : :
7474 : 86 : if (!COMPLETE_TYPE_P (type))
7475 : 0 : goto incompatible;
7476 : :
7477 : 86 : size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7478 : 86 : if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
7479 : 86 : return ((enum built_in_function)
7480 : 172 : ((int) BUILT_IN_SPECULATION_SAFE_VALUE_1 + exact_log2 (size)));
7481 : :
7482 : 0 : incompatible:
7483 : : /* Issue the diagnostic only if the argument is valid, otherwise
7484 : : it would be redundant at best and could be misleading. */
7485 : 42 : if (type != error_mark_node && complain)
7486 : 6 : error ("operand type %qT is incompatible with argument %d of %qE",
7487 : : type, 1, function);
7488 : :
7489 : : return BUILT_IN_NONE;
7490 : : }
7491 : :
7492 : : /* Validate and coerce PARAMS, the arguments to ORIG_FUNCTION to fit
7493 : : the prototype for FUNCTION. The first argument is mandatory, a second
7494 : : argument, if present, must be type compatible with the first. */
7495 : : static bool
7496 : 102 : speculation_safe_value_resolve_params (location_t loc, tree orig_function,
7497 : : vec<tree, va_gc> *params, bool complain)
7498 : : {
7499 : 102 : tree val;
7500 : :
7501 : 102 : if (params->length () == 0)
7502 : : {
7503 : 0 : if (complain)
7504 : 0 : error_at (loc, "too few arguments to function %qE", orig_function);
7505 : 0 : return false;
7506 : : }
7507 : :
7508 : 102 : else if (params->length () > 2)
7509 : : {
7510 : 21 : if (complain)
7511 : 0 : error_at (loc, "too many arguments to function %qE", orig_function);
7512 : 21 : return false;
7513 : : }
7514 : :
7515 : 81 : val = (*params)[0];
7516 : 81 : if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE)
7517 : 0 : val = default_conversion (val);
7518 : 81 : if (!(TREE_CODE (TREE_TYPE (val)) == POINTER_TYPE
7519 : 65 : || TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE))
7520 : : {
7521 : 0 : if (complain)
7522 : 0 : error_at (loc, "expecting argument of type pointer or of type integer "
7523 : : "for argument 1");
7524 : 0 : return false;
7525 : : }
7526 : 81 : (*params)[0] = val;
7527 : :
7528 : 81 : if (params->length () == 2)
7529 : : {
7530 : 32 : tree val2 = (*params)[1];
7531 : 32 : if (TREE_CODE (TREE_TYPE (val2)) == ARRAY_TYPE)
7532 : 0 : val2 = default_conversion (val2);
7533 : 32 : if (error_operand_p (val2))
7534 : : return false;
7535 : 40 : if (!(TREE_TYPE (val) == TREE_TYPE (val2)
7536 : 9 : || useless_type_conversion_p (TREE_TYPE (val), TREE_TYPE (val2))))
7537 : : {
7538 : 9 : if (complain)
7539 : 0 : error_at (loc, "both arguments must be compatible");
7540 : 9 : return false;
7541 : : }
7542 : 22 : (*params)[1] = val2;
7543 : : }
7544 : :
7545 : : return true;
7546 : : }
7547 : :
7548 : : /* Cast the result of the builtin back to the type of the first argument,
7549 : : preserving any qualifiers that it might have. */
7550 : : static tree
7551 : 71 : speculation_safe_value_resolve_return (tree first_param, tree result)
7552 : : {
7553 : 71 : tree ptype = TREE_TYPE (first_param);
7554 : 71 : tree rtype = TREE_TYPE (result);
7555 : 71 : ptype = TYPE_MAIN_VARIANT (ptype);
7556 : :
7557 : 71 : if (tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
7558 : 71 : return convert (ptype, result);
7559 : :
7560 : : return result;
7561 : : }
7562 : :
7563 : : /* A helper function for resolve_overloaded_builtin in resolving the
7564 : : overloaded __sync_ builtins. Returns a positive power of 2 if the
7565 : : first operand of PARAMS is a pointer to a supported data type.
7566 : : Returns 0 if an error is encountered. Return -1 for _BitInt
7567 : : __atomic*fetch* with unsupported type which should be handled by
7568 : : a cas loop.
7569 : : FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
7570 : : built-ins. ORIG_FORMAT is for __sync_* rather than __atomic_*
7571 : : built-ins. */
7572 : :
7573 : : static int
7574 : 318730 : sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch,
7575 : : bool orig_format, bool complain)
7576 : : {
7577 : : /* Type of the argument. */
7578 : 318730 : tree argtype;
7579 : : /* Type the argument points to. */
7580 : 318730 : tree type;
7581 : 318730 : int size;
7582 : :
7583 : 318730 : if (vec_safe_is_empty (params))
7584 : : {
7585 : 106 : if (complain)
7586 : 106 : error ("too few arguments to function %qE", function);
7587 : 106 : return 0;
7588 : : }
7589 : :
7590 : 318624 : argtype = type = TREE_TYPE ((*params)[0]);
7591 : 318624 : if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
7592 : : {
7593 : : /* Force array-to-pointer decay for C++. */
7594 : 3 : (*params)[0] = default_conversion ((*params)[0]);
7595 : 3 : type = TREE_TYPE ((*params)[0]);
7596 : : }
7597 : 318624 : if (TREE_CODE (type) != POINTER_TYPE)
7598 : 2127 : goto incompatible;
7599 : :
7600 : 316497 : type = TREE_TYPE (type);
7601 : 316497 : if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
7602 : 288 : goto incompatible;
7603 : :
7604 : 316209 : if (!COMPLETE_TYPE_P (type))
7605 : 2 : goto incompatible;
7606 : :
7607 : 316207 : if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
7608 : 84 : goto incompatible;
7609 : :
7610 : 316123 : size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
7611 : 316123 : if (size == 16
7612 : 7675 : && TREE_CODE (type) == BITINT_TYPE
7613 : 316649 : && !targetm.scalar_mode_supported_p (TImode))
7614 : : {
7615 : 0 : if (fetch && !orig_format)
7616 : : return -1;
7617 : 0 : goto incompatible;
7618 : : }
7619 : :
7620 : 316123 : if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
7621 : : return size;
7622 : :
7623 : 528 : if (fetch && !orig_format && TREE_CODE (type) == BITINT_TYPE)
7624 : : return -1;
7625 : :
7626 : 0 : incompatible:
7627 : : /* Issue the diagnostic only if the argument is valid, otherwise
7628 : : it would be redundant at best and could be misleading. */
7629 : 2501 : if (argtype != error_mark_node && complain)
7630 : 920 : error ("operand type %qT is incompatible with argument %d of %qE",
7631 : : argtype, 1, function);
7632 : : return 0;
7633 : : }
7634 : :
7635 : : /* A helper function for resolve_overloaded_builtin. Adds casts to
7636 : : PARAMS to make arguments match up with those of FUNCTION. Drops
7637 : : the variadic arguments at the end. Returns false if some error
7638 : : was encountered; true on success. */
7639 : :
7640 : : static bool
7641 : 315595 : sync_resolve_params (location_t loc, tree orig_function, tree function,
7642 : : vec<tree, va_gc> *params, bool orig_format, bool complain)
7643 : : {
7644 : 315595 : function_args_iterator iter;
7645 : 315595 : tree ptype;
7646 : 315595 : unsigned int parmnum;
7647 : :
7648 : 315595 : function_args_iter_init (&iter, TREE_TYPE (function));
7649 : : /* We've declared the implementation functions to use "volatile void *"
7650 : : as the pointer parameter, so we shouldn't get any complaints from the
7651 : : call to check_function_arguments what ever type the user used. */
7652 : 315595 : function_args_iter_next (&iter);
7653 : 315595 : ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
7654 : 315595 : ptype = TYPE_MAIN_VARIANT (ptype);
7655 : :
7656 : : /* For the rest of the values, we need to cast these to FTYPE, so that we
7657 : : don't get warnings for passing pointer types, etc. */
7658 : 315595 : parmnum = 0;
7659 : 1769899 : while (1)
7660 : : {
7661 : 1042747 : tree val, arg_type;
7662 : :
7663 : 1042747 : arg_type = function_args_iter_cond (&iter);
7664 : : /* XXX void_type_node belies the abstraction. */
7665 : 1042747 : if (arg_type == void_type_node)
7666 : : break;
7667 : :
7668 : 727210 : ++parmnum;
7669 : 727210 : if (params->length () <= parmnum)
7670 : : {
7671 : 58 : if (complain)
7672 : 7 : error_at (loc, "too few arguments to function %qE", orig_function);
7673 : 58 : return false;
7674 : : }
7675 : :
7676 : : /* Only convert parameters if arg_type is unsigned integer type with
7677 : : new format sync routines, i.e. don't attempt to convert pointer
7678 : : arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
7679 : : bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
7680 : : kinds). */
7681 : 727152 : if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
7682 : : {
7683 : : /* Ideally for the first conversion we'd use convert_for_assignment
7684 : : so that we get warnings for anything that doesn't match the pointer
7685 : : type. This isn't portable across the C and C++ front ends atm. */
7686 : 201873 : val = (*params)[parmnum];
7687 : 201873 : val = convert (ptype, val);
7688 : 201873 : val = convert (arg_type, val);
7689 : 201873 : (*params)[parmnum] = val;
7690 : : }
7691 : :
7692 : 727152 : function_args_iter_next (&iter);
7693 : 727152 : }
7694 : :
7695 : : /* __atomic routines are not variadic. */
7696 : 315537 : if (!orig_format && params->length () != parmnum + 1)
7697 : : {
7698 : 106 : if (complain)
7699 : 103 : error_at (loc, "too many arguments to function %qE", orig_function);
7700 : 106 : return false;
7701 : : }
7702 : :
7703 : : /* The definition of these primitives is variadic, with the remaining
7704 : : being "an optional list of variables protected by the memory barrier".
7705 : : No clue what that's supposed to mean, precisely, but we consider all
7706 : : call-clobbered variables to be protected so we're safe. */
7707 : 315431 : params->truncate (parmnum + 1);
7708 : :
7709 : 315431 : return true;
7710 : : }
7711 : :
7712 : : /* A helper function for resolve_overloaded_builtin. Adds a cast to
7713 : : RESULT to make it match the type of the first pointer argument in
7714 : : PARAMS. */
7715 : :
7716 : : static tree
7717 : 208672 : sync_resolve_return (tree first_param, tree result, bool orig_format)
7718 : : {
7719 : 208672 : tree ptype = TREE_TYPE (TREE_TYPE (first_param));
7720 : 208672 : tree rtype = TREE_TYPE (result);
7721 : 208672 : ptype = TYPE_MAIN_VARIANT (ptype);
7722 : :
7723 : : /* New format doesn't require casting unless the types are the same size. */
7724 : 208672 : if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
7725 : 208672 : return convert (ptype, result);
7726 : : else
7727 : : return result;
7728 : : }
7729 : :
7730 : : /* This function verifies the PARAMS to generic atomic FUNCTION.
7731 : : It returns the size if all the parameters are the same size, otherwise
7732 : : 0 is returned if the parameters are invalid. */
7733 : :
7734 : : static int
7735 : 81980 : get_atomic_generic_size (location_t loc, tree function,
7736 : : vec<tree, va_gc> *params, bool complain)
7737 : : {
7738 : 81980 : unsigned int n_param;
7739 : 81980 : unsigned int n_model;
7740 : 81980 : unsigned int outputs = 0; // bitset of output parameters
7741 : 81980 : unsigned int x;
7742 : 81980 : int size_0;
7743 : 81980 : tree type_0;
7744 : :
7745 : : /* Determine the parameter makeup. */
7746 : 81980 : switch (DECL_FUNCTION_CODE (function))
7747 : : {
7748 : : case BUILT_IN_ATOMIC_EXCHANGE:
7749 : : n_param = 4;
7750 : : n_model = 1;
7751 : : outputs = 5;
7752 : : break;
7753 : 49263 : case BUILT_IN_ATOMIC_LOAD:
7754 : 49263 : n_param = 3;
7755 : 49263 : n_model = 1;
7756 : 49263 : outputs = 2;
7757 : 49263 : break;
7758 : 11389 : case BUILT_IN_ATOMIC_STORE:
7759 : 11389 : n_param = 3;
7760 : 11389 : n_model = 1;
7761 : 11389 : outputs = 1;
7762 : 11389 : break;
7763 : 20696 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7764 : 20696 : n_param = 6;
7765 : 20696 : n_model = 2;
7766 : 20696 : outputs = 3;
7767 : 20696 : break;
7768 : 0 : default:
7769 : 0 : gcc_unreachable ();
7770 : : }
7771 : :
7772 : 81980 : if (vec_safe_length (params) != n_param)
7773 : : {
7774 : 466 : if (complain)
7775 : 247 : error_at (loc, "incorrect number of arguments to function %qE",
7776 : : function);
7777 : 466 : return 0;
7778 : : }
7779 : :
7780 : : /* Get type of first parameter, and determine its size. */
7781 : 81514 : type_0 = TREE_TYPE ((*params)[0]);
7782 : 81514 : if (TREE_CODE (type_0) == ARRAY_TYPE && c_dialect_cxx ())
7783 : : {
7784 : : /* Force array-to-pointer decay for C++. */
7785 : 15 : (*params)[0] = default_conversion ((*params)[0]);
7786 : 15 : type_0 = TREE_TYPE ((*params)[0]);
7787 : : }
7788 : 81514 : if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
7789 : : {
7790 : 152 : if (complain)
7791 : 41 : error_at (loc, "argument 1 of %qE must be a non-void pointer type",
7792 : : function);
7793 : 152 : return 0;
7794 : : }
7795 : :
7796 : 81362 : if (!COMPLETE_TYPE_P (TREE_TYPE (type_0)))
7797 : : {
7798 : 23 : if (complain)
7799 : 8 : error_at (loc, "argument 1 of %qE must be a pointer to a complete type",
7800 : : function);
7801 : 23 : return 0;
7802 : : }
7803 : :
7804 : : /* Types must be compile time constant sizes. */
7805 : 81339 : if (!tree_fits_uhwi_p ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))))
7806 : : {
7807 : 1 : if (complain)
7808 : 1 : error_at (loc,
7809 : : "argument 1 of %qE must be a pointer to a constant size type",
7810 : : function);
7811 : 1 : return 0;
7812 : : }
7813 : :
7814 : 81338 : size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
7815 : :
7816 : : /* Zero size objects are not allowed. */
7817 : 81338 : if (size_0 == 0)
7818 : : {
7819 : 22 : if (complain)
7820 : 7 : error_at (
7821 : : loc, "argument 1 of %qE must be a pointer to a nonzero size object",
7822 : : function);
7823 : 22 : return 0;
7824 : : }
7825 : :
7826 : : /* Check each other parameter is a pointer and the same size. */
7827 : 285285 : for (x = 0; x < n_param - n_model; x++)
7828 : : {
7829 : 204108 : int size;
7830 : 204108 : tree type = TREE_TYPE ((*params)[x]);
7831 : : /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
7832 : 204108 : if (n_param == 6 && x == 3)
7833 : 20518 : continue;
7834 : 183590 : if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
7835 : : {
7836 : : /* Force array-to-pointer decay for C++. */
7837 : 18 : (*params)[x] = default_conversion ((*params)[x]);
7838 : 18 : type = TREE_TYPE ((*params)[x]);
7839 : : }
7840 : 183590 : if (!POINTER_TYPE_P (type))
7841 : : {
7842 : 16 : if (complain)
7843 : 13 : error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
7844 : : function);
7845 : 16 : return 0;
7846 : : }
7847 : 183574 : else if (TYPE_SIZE_UNIT (TREE_TYPE (type))
7848 : 183574 : && TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type))))
7849 : : != INTEGER_CST)
7850 : : {
7851 : 1 : if (complain)
7852 : 1 : error_at (loc,
7853 : : "argument %d of %qE must be a pointer to a constant "
7854 : : "size type",
7855 : : x + 1, function);
7856 : 1 : return 0;
7857 : : }
7858 : 183573 : else if (FUNCTION_POINTER_TYPE_P (type))
7859 : : {
7860 : 10 : if (complain)
7861 : 7 : error_at (loc,
7862 : : "argument %d of %qE must not be a pointer to a "
7863 : : "function",
7864 : : x + 1, function);
7865 : 10 : return 0;
7866 : : }
7867 : 183563 : tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
7868 : 183563 : size = type_size ? tree_to_uhwi (type_size) : 0;
7869 : 183563 : if (size != size_0)
7870 : : {
7871 : 34 : if (complain)
7872 : 25 : error_at (loc, "size mismatch in argument %d of %qE", x + 1,
7873 : : function);
7874 : 34 : return 0;
7875 : : }
7876 : :
7877 : 183529 : {
7878 : 183529 : auto_diagnostic_group d;
7879 : 183529 : int quals = TYPE_QUALS (TREE_TYPE (type));
7880 : : /* Must not write to an argument of a const-qualified type. */
7881 : 183529 : if (outputs & (1 << x) && quals & TYPE_QUAL_CONST)
7882 : : {
7883 : 57 : if (c_dialect_cxx ())
7884 : : {
7885 : 45 : if (complain)
7886 : 42 : error_at (loc,
7887 : : "argument %d of %qE must not be a pointer to "
7888 : : "a %<const%> type",
7889 : : x + 1, function);
7890 : 45 : return 0;
7891 : : }
7892 : : else
7893 : 12 : pedwarn (loc, OPT_Wdiscarded_qualifiers, "argument %d "
7894 : : "of %qE discards %<const%> qualifier", x + 1,
7895 : : function);
7896 : : }
7897 : : /* Only the first argument is allowed to be volatile. */
7898 : 183484 : if (x > 0 && quals & TYPE_QUAL_VOLATILE)
7899 : : {
7900 : 44 : if (c_dialect_cxx ())
7901 : : {
7902 : 33 : if (complain)
7903 : 30 : error_at (loc,
7904 : : "argument %d of %qE must not be a pointer to "
7905 : : "a %<volatile%> type",
7906 : : x + 1, function);
7907 : 33 : return 0;
7908 : : }
7909 : : else
7910 : 11 : pedwarn (loc, OPT_Wdiscarded_qualifiers, "argument %d "
7911 : : "of %qE discards %<volatile%> qualifier", x + 1,
7912 : : function);
7913 : : }
7914 : 183529 : }
7915 : : }
7916 : :
7917 : : /* Check memory model parameters for validity. */
7918 : 182856 : for (x = n_param - n_model ; x < n_param; x++)
7919 : : {
7920 : 101695 : tree p = (*params)[x];
7921 : 101695 : if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
7922 : : {
7923 : 10 : if (complain)
7924 : 7 : error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
7925 : : function);
7926 : 10 : return 0;
7927 : : }
7928 : 101685 : p = fold_for_warn (p);
7929 : 101685 : if (TREE_CODE (p) == INTEGER_CST)
7930 : : {
7931 : : /* memmodel_base masks the low 16 bits, thus ignore any bits above
7932 : : it by using TREE_INT_CST_LOW instead of tree_to_*hwi. Those high
7933 : : bits will be checked later during expansion in target specific
7934 : : way. */
7935 : 78718 : if (memmodel_base (TREE_INT_CST_LOW (p)) >= MEMMODEL_LAST)
7936 : : {
7937 : 20 : if (complain)
7938 : 14 : warning_at (loc, OPT_Winvalid_memory_model,
7939 : : "invalid memory model argument %d of %qE", x + 1,
7940 : : function);
7941 : : else
7942 : : return 0;
7943 : : }
7944 : : }
7945 : : }
7946 : :
7947 : : return size_0;
7948 : : }
7949 : :
7950 : :
7951 : : /* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
7952 : : at the beginning of the parameter list PARAMS representing the size of the
7953 : : objects. This is to match the library ABI requirement. LOC is the location
7954 : : of the function call.
7955 : : The new function is returned if it needed rebuilding, otherwise NULL_TREE is
7956 : : returned to allow the external call to be constructed. */
7957 : :
7958 : : static tree
7959 : 4146 : add_atomic_size_parameter (unsigned n, location_t loc, tree function,
7960 : : vec<tree, va_gc> *params)
7961 : : {
7962 : 4146 : tree size_node;
7963 : :
7964 : : /* Insert a SIZE_T parameter as the first param. If there isn't
7965 : : enough space, allocate a new vector and recursively re-build with that. */
7966 : 4146 : if (!params->space (1))
7967 : : {
7968 : 33 : unsigned int z, len;
7969 : 33 : vec<tree, va_gc> *v;
7970 : 33 : tree f;
7971 : :
7972 : 33 : len = params->length ();
7973 : 33 : vec_alloc (v, len + 1);
7974 : 33 : v->quick_push (build_int_cst (size_type_node, n));
7975 : 165 : for (z = 0; z < len; z++)
7976 : 132 : v->quick_push ((*params)[z]);
7977 : 33 : f = build_function_call_vec (loc, vNULL, function, v, NULL);
7978 : 33 : vec_free (v);
7979 : 33 : return f;
7980 : : }
7981 : :
7982 : : /* Add the size parameter and leave as a function call for processing. */
7983 : 4113 : size_node = build_int_cst (size_type_node, n);
7984 : 4113 : params->quick_insert (0, size_node);
7985 : 4113 : return NULL_TREE;
7986 : : }
7987 : :
7988 : :
7989 : : /* Return whether atomic operations for naturally aligned N-byte
7990 : : arguments are supported, whether inline or through libatomic. */
7991 : : static bool
7992 : 81161 : atomic_size_supported_p (int n)
7993 : : {
7994 : 81161 : switch (n)
7995 : : {
7996 : : case 1:
7997 : : case 2:
7998 : : case 4:
7999 : : case 8:
8000 : : return true;
8001 : :
8002 : 6968 : case 16:
8003 : 6968 : return targetm.scalar_mode_supported_p (TImode);
8004 : :
8005 : 4146 : default:
8006 : 4146 : return false;
8007 : : }
8008 : : }
8009 : :
8010 : : /* This will process an __atomic_exchange function call, determine whether it
8011 : : needs to be mapped to the _N variation, or turned into a library call.
8012 : : LOC is the location of the builtin call.
8013 : : FUNCTION is the DECL that has been invoked;
8014 : : PARAMS is the argument list for the call. The return value is non-null
8015 : : TRUE is returned if it is translated into the proper format for a call to the
8016 : : external library, and NEW_RETURN is set the tree for that function.
8017 : : FALSE is returned if processing for the _N variation is required, and
8018 : : NEW_RETURN is set to the return value the result is copied into. */
8019 : : static bool
8020 : 632 : resolve_overloaded_atomic_exchange (location_t loc, tree function,
8021 : : vec<tree, va_gc> *params, tree *new_return,
8022 : : bool complain)
8023 : : {
8024 : 632 : tree p0, p1, p2, p3;
8025 : 632 : tree I_type, I_type_ptr;
8026 : 632 : int n = get_atomic_generic_size (loc, function, params, complain);
8027 : :
8028 : : /* Size of 0 is an error condition. */
8029 : 632 : if (n == 0)
8030 : : {
8031 : 195 : *new_return = error_mark_node;
8032 : 195 : return true;
8033 : : }
8034 : :
8035 : : /* If not a lock-free size, change to the library generic format. */
8036 : 437 : if (!atomic_size_supported_p (n))
8037 : : {
8038 : 39 : *new_return = add_atomic_size_parameter (n, loc, function, params);
8039 : 39 : return true;
8040 : : }
8041 : :
8042 : : /* Otherwise there is a lockfree match, transform the call from:
8043 : : void fn(T* mem, T* desired, T* return, model)
8044 : : into
8045 : : *return = (T) (fn (In* mem, (In) *desired, model)) */
8046 : :
8047 : 398 : p0 = (*params)[0];
8048 : 398 : p1 = (*params)[1];
8049 : 398 : p2 = (*params)[2];
8050 : 398 : p3 = (*params)[3];
8051 : :
8052 : : /* Create pointer to appropriate size. */
8053 : 398 : I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
8054 : 398 : I_type_ptr = build_pointer_type (I_type);
8055 : :
8056 : : /* Convert object pointer to required type. */
8057 : 398 : p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
8058 : 398 : (*params)[0] = p0;
8059 : : /* Convert new value to required type, and dereference it.
8060 : : If *p1 type can have padding or may involve floating point which
8061 : : could e.g. be promoted to wider precision and demoted afterwards,
8062 : : state of padding bits might not be preserved. */
8063 : 398 : build_indirect_ref (loc, p1, RO_UNARY_STAR);
8064 : 398 : p1 = build2_loc (loc, MEM_REF, I_type,
8065 : : build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1),
8066 : 398 : build_zero_cst (TREE_TYPE (p1)));
8067 : 398 : (*params)[1] = p1;
8068 : :
8069 : : /* Move memory model to the 3rd position, and end param list. */
8070 : 398 : (*params)[2] = p3;
8071 : 398 : params->truncate (3);
8072 : :
8073 : : /* Convert return pointer and dereference it for later assignment. */
8074 : 398 : *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
8075 : :
8076 : 398 : return false;
8077 : : }
8078 : :
8079 : : /* This will process an __atomic_compare_exchange function call, determine
8080 : : whether it needs to be mapped to the _N variation, or turned into a lib call.
8081 : : LOC is the location of the builtin call.
8082 : : FUNCTION is the DECL that has been invoked;
8083 : : PARAMS is the argument list for the call. The return value is non-null
8084 : : TRUE is returned if it is translated into the proper format for a call to the
8085 : : external library, and NEW_RETURN is set the tree for that function.
8086 : : FALSE is returned if processing for the _N variation is required. */
8087 : :
8088 : : static bool
8089 : 20696 : resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
8090 : : vec<tree, va_gc> *params,
8091 : : tree *new_return, bool complain)
8092 : : {
8093 : 20696 : tree p0, p1, p2;
8094 : 20696 : tree I_type, I_type_ptr;
8095 : 20696 : int n = get_atomic_generic_size (loc, function, params, complain);
8096 : :
8097 : : /* Size of 0 is an error condition. */
8098 : 20696 : if (n == 0)
8099 : : {
8100 : 178 : *new_return = error_mark_node;
8101 : 178 : return true;
8102 : : }
8103 : :
8104 : : /* If not a lock-free size, change to the library generic format. */
8105 : 20518 : if (!atomic_size_supported_p (n))
8106 : : {
8107 : : /* The library generic format does not have the weak parameter, so
8108 : : remove it from the param list. Since a parameter has been removed,
8109 : : we can be sure that there is room for the SIZE_T parameter, meaning
8110 : : there will not be a recursive rebuilding of the parameter list, so
8111 : : there is no danger this will be done twice. */
8112 : 1198 : if (n > 0)
8113 : : {
8114 : 1198 : (*params)[3] = (*params)[4];
8115 : 1198 : (*params)[4] = (*params)[5];
8116 : 1198 : params->truncate (5);
8117 : : }
8118 : 1198 : *new_return = add_atomic_size_parameter (n, loc, function, params);
8119 : 1198 : return true;
8120 : : }
8121 : :
8122 : : /* Otherwise, there is a match, so the call needs to be transformed from:
8123 : : bool fn(T* mem, T* desired, T* return, weak, success, failure)
8124 : : into
8125 : : bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
8126 : :
8127 : 19320 : p0 = (*params)[0];
8128 : 19320 : p1 = (*params)[1];
8129 : 19320 : p2 = (*params)[2];
8130 : :
8131 : : /* Create pointer to appropriate size. */
8132 : 19320 : I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
8133 : 19320 : I_type_ptr = build_pointer_type (I_type);
8134 : :
8135 : : /* Convert object pointer to required type. */
8136 : 19320 : p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
8137 : 19320 : (*params)[0] = p0;
8138 : :
8139 : : /* Convert expected pointer to required type. */
8140 : 19320 : p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
8141 : 19320 : (*params)[1] = p1;
8142 : :
8143 : : /* Convert desired value to required type, and dereference it.
8144 : : If *p2 type can have padding or may involve floating point which
8145 : : could e.g. be promoted to wider precision and demoted afterwards,
8146 : : state of padding bits might not be preserved. */
8147 : 19320 : build_indirect_ref (loc, p2, RO_UNARY_STAR);
8148 : 19320 : p2 = build2_loc (loc, MEM_REF, I_type,
8149 : : build1 (VIEW_CONVERT_EXPR, I_type_ptr, p2),
8150 : 19320 : build_zero_cst (TREE_TYPE (p2)));
8151 : 19320 : (*params)[2] = p2;
8152 : :
8153 : : /* The rest of the parameters are fine. NULL means no special return value
8154 : : processing.*/
8155 : 19320 : *new_return = NULL;
8156 : 19320 : return false;
8157 : : }
8158 : :
8159 : : /* This will process an __atomic_load function call, determine whether it
8160 : : needs to be mapped to the _N variation, or turned into a library call.
8161 : : LOC is the location of the builtin call.
8162 : : FUNCTION is the DECL that has been invoked;
8163 : : PARAMS is the argument list for the call. The return value is non-null
8164 : : TRUE is returned if it is translated into the proper format for a call to the
8165 : : external library, and NEW_RETURN is set the tree for that function.
8166 : : FALSE is returned if processing for the _N variation is required, and
8167 : : NEW_RETURN is set to the return value the result is copied into. */
8168 : :
8169 : : static bool
8170 : 49263 : resolve_overloaded_atomic_load (location_t loc, tree function,
8171 : : vec<tree, va_gc> *params, tree *new_return,
8172 : : bool complain)
8173 : : {
8174 : 49263 : tree p0, p1, p2;
8175 : 49263 : tree I_type, I_type_ptr;
8176 : 49263 : int n = get_atomic_generic_size (loc, function, params, complain);
8177 : :
8178 : : /* Size of 0 is an error condition. */
8179 : 49263 : if (n == 0)
8180 : : {
8181 : 278 : *new_return = error_mark_node;
8182 : 278 : return true;
8183 : : }
8184 : :
8185 : : /* If not a lock-free size, change to the library generic format. */
8186 : 48985 : if (!atomic_size_supported_p (n))
8187 : : {
8188 : 2499 : *new_return = add_atomic_size_parameter (n, loc, function, params);
8189 : 2499 : return true;
8190 : : }
8191 : :
8192 : : /* Otherwise, there is a match, so the call needs to be transformed from:
8193 : : void fn(T* mem, T* return, model)
8194 : : into
8195 : : *return = (T) (fn ((In *) mem, model)) */
8196 : :
8197 : 46486 : p0 = (*params)[0];
8198 : 46486 : p1 = (*params)[1];
8199 : 46486 : p2 = (*params)[2];
8200 : :
8201 : : /* Create pointer to appropriate size. */
8202 : 46486 : I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
8203 : 46486 : I_type_ptr = build_pointer_type (I_type);
8204 : :
8205 : : /* Convert object pointer to required type. */
8206 : 46486 : p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
8207 : 46486 : (*params)[0] = p0;
8208 : :
8209 : : /* Move memory model to the 2nd position, and end param list. */
8210 : 46486 : (*params)[1] = p2;
8211 : 46486 : params->truncate (2);
8212 : :
8213 : : /* Convert return pointer and dereference it for later assignment. */
8214 : 46486 : *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
8215 : :
8216 : 46486 : return false;
8217 : : }
8218 : :
8219 : : /* This will process an __atomic_store function call, determine whether it
8220 : : needs to be mapped to the _N variation, or turned into a library call.
8221 : : LOC is the location of the builtin call.
8222 : : FUNCTION is the DECL that has been invoked;
8223 : : PARAMS is the argument list for the call. The return value is non-null
8224 : : TRUE is returned if it is translated into the proper format for a call to the
8225 : : external library, and NEW_RETURN is set the tree for that function.
8226 : : FALSE is returned if processing for the _N variation is required, and
8227 : : NEW_RETURN is set to the return value the result is copied into. */
8228 : :
8229 : : static bool
8230 : 11389 : resolve_overloaded_atomic_store (location_t loc, tree function,
8231 : : vec<tree, va_gc> *params, tree *new_return,
8232 : : bool complain)
8233 : : {
8234 : 11389 : tree p0, p1;
8235 : 11389 : tree I_type, I_type_ptr;
8236 : 11389 : int n = get_atomic_generic_size (loc, function, params, complain);
8237 : :
8238 : : /* Size of 0 is an error condition. */
8239 : 11389 : if (n == 0)
8240 : : {
8241 : 168 : *new_return = error_mark_node;
8242 : 168 : return true;
8243 : : }
8244 : :
8245 : : /* If not a lock-free size, change to the library generic format. */
8246 : 11221 : if (!atomic_size_supported_p (n))
8247 : : {
8248 : 410 : *new_return = add_atomic_size_parameter (n, loc, function, params);
8249 : 410 : return true;
8250 : : }
8251 : :
8252 : : /* Otherwise, there is a match, so the call needs to be transformed from:
8253 : : void fn(T* mem, T* value, model)
8254 : : into
8255 : : fn ((In *) mem, (In) *value, model) */
8256 : :
8257 : 10811 : p0 = (*params)[0];
8258 : 10811 : p1 = (*params)[1];
8259 : :
8260 : : /* Create pointer to appropriate size. */
8261 : 10811 : I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
8262 : 10811 : I_type_ptr = build_pointer_type (I_type);
8263 : :
8264 : : /* Convert object pointer to required type. */
8265 : 10811 : p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
8266 : 10811 : (*params)[0] = p0;
8267 : :
8268 : : /* Convert new value to required type, and dereference it. */
8269 : 10811 : p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
8270 : 10811 : p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
8271 : 10811 : (*params)[1] = p1;
8272 : :
8273 : : /* The memory model is in the right spot already. Return is void. */
8274 : 10811 : *new_return = NULL_TREE;
8275 : :
8276 : 10811 : return false;
8277 : : }
8278 : :
8279 : : /* Emit __atomic*fetch* on _BitInt which doesn't have a size of
8280 : : 1, 2, 4, 8 or 16 bytes using __atomic_compare_exchange loop.
8281 : : ORIG_CODE is the DECL_FUNCTION_CODE of ORIG_FUNCTION and
8282 : : ORIG_PARAMS arguments of the call. */
8283 : :
8284 : : static tree
8285 : 528 : atomic_bitint_fetch_using_cas_loop (location_t loc,
8286 : : enum built_in_function orig_code,
8287 : : tree orig_function,
8288 : : vec<tree, va_gc> *orig_params)
8289 : : {
8290 : 528 : enum tree_code code = ERROR_MARK;
8291 : 528 : bool return_old_p = false;
8292 : 528 : switch (orig_code)
8293 : : {
8294 : 0 : case BUILT_IN_ATOMIC_ADD_FETCH_N:
8295 : 0 : code = PLUS_EXPR;
8296 : 0 : break;
8297 : 1 : case BUILT_IN_ATOMIC_SUB_FETCH_N:
8298 : 1 : code = MINUS_EXPR;
8299 : 1 : break;
8300 : 0 : case BUILT_IN_ATOMIC_AND_FETCH_N:
8301 : 0 : code = BIT_AND_EXPR;
8302 : 0 : break;
8303 : : case BUILT_IN_ATOMIC_NAND_FETCH_N:
8304 : : break;
8305 : 1 : case BUILT_IN_ATOMIC_XOR_FETCH_N:
8306 : 1 : code = BIT_XOR_EXPR;
8307 : 1 : break;
8308 : 0 : case BUILT_IN_ATOMIC_OR_FETCH_N:
8309 : 0 : code = BIT_IOR_EXPR;
8310 : 0 : break;
8311 : 106 : case BUILT_IN_ATOMIC_FETCH_ADD_N:
8312 : 106 : code = PLUS_EXPR;
8313 : 106 : return_old_p = true;
8314 : 106 : break;
8315 : 104 : case BUILT_IN_ATOMIC_FETCH_SUB_N:
8316 : 104 : code = MINUS_EXPR;
8317 : 104 : return_old_p = true;
8318 : 104 : break;
8319 : 105 : case BUILT_IN_ATOMIC_FETCH_AND_N:
8320 : 105 : code = BIT_AND_EXPR;
8321 : 105 : return_old_p = true;
8322 : 105 : break;
8323 : 0 : case BUILT_IN_ATOMIC_FETCH_NAND_N:
8324 : 0 : return_old_p = true;
8325 : 0 : break;
8326 : 104 : case BUILT_IN_ATOMIC_FETCH_XOR_N:
8327 : 104 : code = BIT_XOR_EXPR;
8328 : 104 : return_old_p = true;
8329 : 104 : break;
8330 : 106 : case BUILT_IN_ATOMIC_FETCH_OR_N:
8331 : 106 : code = BIT_IOR_EXPR;
8332 : 106 : return_old_p = true;
8333 : 106 : break;
8334 : 0 : default:
8335 : 0 : gcc_unreachable ();
8336 : : }
8337 : :
8338 : 528 : if (orig_params->length () != 3)
8339 : : {
8340 : 0 : if (orig_params->length () < 3)
8341 : 0 : error_at (loc, "too few arguments to function %qE", orig_function);
8342 : : else
8343 : 0 : error_at (loc, "too many arguments to function %qE", orig_function);
8344 : 0 : return error_mark_node;
8345 : : }
8346 : :
8347 : 528 : tree stmts = push_stmt_list ();
8348 : :
8349 : 528 : tree nonatomic_lhs_type = TREE_TYPE (TREE_TYPE ((*orig_params)[0]));
8350 : 528 : nonatomic_lhs_type = TYPE_MAIN_VARIANT (nonatomic_lhs_type);
8351 : 528 : gcc_assert (TREE_CODE (nonatomic_lhs_type) == BITINT_TYPE);
8352 : :
8353 : 528 : tree lhs_addr = (*orig_params)[0];
8354 : 528 : tree val = convert (nonatomic_lhs_type, (*orig_params)[1]);
8355 : 528 : tree model = convert (integer_type_node, (*orig_params)[2]);
8356 : 528 : if (!c_dialect_cxx ())
8357 : : {
8358 : 528 : lhs_addr = c_fully_fold (lhs_addr, false, NULL);
8359 : 528 : val = c_fully_fold (val, false, NULL);
8360 : 528 : model = c_fully_fold (model, false, NULL);
8361 : : }
8362 : 528 : if (TREE_SIDE_EFFECTS (lhs_addr))
8363 : : {
8364 : 1 : tree var = create_tmp_var_raw (TREE_TYPE (lhs_addr));
8365 : 1 : lhs_addr = build4 (TARGET_EXPR, TREE_TYPE (lhs_addr), var, lhs_addr,
8366 : : NULL_TREE, NULL_TREE);
8367 : 1 : add_stmt (lhs_addr);
8368 : : }
8369 : 528 : if (TREE_SIDE_EFFECTS (val))
8370 : : {
8371 : 1 : tree var = create_tmp_var_raw (nonatomic_lhs_type);
8372 : 1 : val = build4 (TARGET_EXPR, nonatomic_lhs_type, var, val, NULL_TREE,
8373 : : NULL_TREE);
8374 : 1 : add_stmt (val);
8375 : : }
8376 : 528 : if (TREE_SIDE_EFFECTS (model))
8377 : : {
8378 : 1 : tree var = create_tmp_var_raw (integer_type_node);
8379 : 1 : model = build4 (TARGET_EXPR, integer_type_node, var, model, NULL_TREE,
8380 : : NULL_TREE);
8381 : 1 : add_stmt (model);
8382 : : }
8383 : :
8384 : 528 : tree old = create_tmp_var_raw (nonatomic_lhs_type);
8385 : 528 : tree old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
8386 : 528 : TREE_ADDRESSABLE (old) = 1;
8387 : 528 : suppress_warning (old);
8388 : :
8389 : 528 : tree newval = create_tmp_var_raw (nonatomic_lhs_type);
8390 : 528 : tree newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
8391 : 528 : TREE_ADDRESSABLE (newval) = 1;
8392 : 528 : suppress_warning (newval);
8393 : :
8394 : 528 : tree loop_decl = create_artificial_label (loc);
8395 : 528 : tree loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
8396 : :
8397 : 528 : tree done_decl = create_artificial_label (loc);
8398 : 528 : tree done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
8399 : :
8400 : 528 : vec<tree, va_gc> *params;
8401 : 528 : vec_alloc (params, 6);
8402 : :
8403 : : /* __atomic_load (addr, &old, SEQ_CST). */
8404 : 528 : tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
8405 : 528 : params->quick_push (lhs_addr);
8406 : 528 : params->quick_push (old_addr);
8407 : 528 : params->quick_push (build_int_cst (integer_type_node, MEMMODEL_RELAXED));
8408 : 528 : tree func_call = resolve_overloaded_builtin (loc, fndecl, params);
8409 : 528 : if (func_call == NULL_TREE)
8410 : 528 : func_call = build_function_call_vec (loc, vNULL, fndecl, params, NULL);
8411 : 528 : old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
8412 : : NULL_TREE);
8413 : 528 : add_stmt (old);
8414 : 528 : params->truncate (0);
8415 : :
8416 : : /* loop: */
8417 : 528 : add_stmt (loop_label);
8418 : :
8419 : : /* newval = old + val; */
8420 : 528 : tree rhs;
8421 : 528 : switch (code)
8422 : : {
8423 : 211 : case PLUS_EXPR:
8424 : 211 : case MINUS_EXPR:
8425 : 211 : if (!TYPE_OVERFLOW_WRAPS (nonatomic_lhs_type))
8426 : : {
8427 : 211 : tree utype
8428 : 211 : = build_bitint_type (TYPE_PRECISION (nonatomic_lhs_type), 1);
8429 : 211 : rhs = convert (nonatomic_lhs_type,
8430 : : build2_loc (loc, code, utype,
8431 : : convert (utype, old),
8432 : : convert (utype, val)));
8433 : : }
8434 : : else
8435 : 0 : rhs = build2_loc (loc, code, nonatomic_lhs_type, old, val);
8436 : : break;
8437 : 316 : case BIT_AND_EXPR:
8438 : 316 : case BIT_IOR_EXPR:
8439 : 316 : case BIT_XOR_EXPR:
8440 : 316 : rhs = build2_loc (loc, code, nonatomic_lhs_type, old, val);
8441 : 316 : break;
8442 : 1 : case ERROR_MARK:
8443 : 1 : rhs = build2_loc (loc, BIT_AND_EXPR, nonatomic_lhs_type,
8444 : : build1_loc (loc, BIT_NOT_EXPR,
8445 : : nonatomic_lhs_type, old), val);
8446 : 1 : break;
8447 : 0 : default:
8448 : 0 : gcc_unreachable ();
8449 : : }
8450 : 528 : rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
8451 : : NULL_TREE);
8452 : 528 : SET_EXPR_LOCATION (rhs, loc);
8453 : 528 : add_stmt (rhs);
8454 : :
8455 : : /* if (__atomic_compare_exchange (addr, &old, &new, false, model, model))
8456 : : goto done; */
8457 : 528 : fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
8458 : 528 : params->quick_push (lhs_addr);
8459 : 528 : params->quick_push (old_addr);
8460 : 528 : params->quick_push (newval_addr);
8461 : 528 : params->quick_push (integer_zero_node);
8462 : 528 : params->quick_push (model);
8463 : 528 : if (tree_fits_uhwi_p (model)
8464 : 527 : && (tree_to_uhwi (model) == MEMMODEL_RELEASE
8465 : 447 : || tree_to_uhwi (model) == MEMMODEL_ACQ_REL))
8466 : 129 : params->quick_push (build_int_cst (integer_type_node, MEMMODEL_RELAXED));
8467 : : else
8468 : 399 : params->quick_push (model);
8469 : 528 : func_call = resolve_overloaded_builtin (loc, fndecl, params);
8470 : 528 : if (func_call == NULL_TREE)
8471 : 528 : func_call = build_function_call_vec (loc, vNULL, fndecl, params, NULL);
8472 : :
8473 : 528 : tree goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
8474 : 528 : SET_EXPR_LOCATION (goto_stmt, loc);
8475 : :
8476 : 528 : tree stmt
8477 : 528 : = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
8478 : 528 : SET_EXPR_LOCATION (stmt, loc);
8479 : 528 : add_stmt (stmt);
8480 : :
8481 : : /* goto loop; */
8482 : 528 : goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
8483 : 528 : SET_EXPR_LOCATION (goto_stmt, loc);
8484 : 528 : add_stmt (goto_stmt);
8485 : :
8486 : : /* done: */
8487 : 528 : add_stmt (done_label);
8488 : :
8489 : 528 : tree ret = create_tmp_var_raw (nonatomic_lhs_type);
8490 : 531 : stmt = build2_loc (loc, MODIFY_EXPR, void_type_node, ret,
8491 : : return_old_p ? old : newval);
8492 : 528 : add_stmt (stmt);
8493 : :
8494 : : /* Finish the compound statement. */
8495 : 528 : stmts = pop_stmt_list (stmts);
8496 : :
8497 : 528 : return build4 (TARGET_EXPR, nonatomic_lhs_type, ret, stmts, NULL_TREE,
8498 : 528 : NULL_TREE);
8499 : : }
8500 : :
8501 : :
8502 : : /* Some builtin functions are placeholders for other expressions. This
8503 : : function should be called immediately after parsing the call expression
8504 : : before surrounding code has committed to the type of the expression.
8505 : :
8506 : : LOC is the location of the builtin call.
8507 : :
8508 : : FUNCTION is the DECL that has been invoked; it is known to be a builtin.
8509 : : PARAMS is the argument list for the call. The return value is non-null
8510 : : when expansion is complete, and null if normal processing should
8511 : : continue. */
8512 : :
8513 : : tree
8514 : 56810619 : resolve_overloaded_builtin (location_t loc, tree function,
8515 : : vec<tree, va_gc> *params, bool complain)
8516 : : {
8517 : : /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
8518 : : Those are not valid to call with a pointer to _Bool (or C++ bool)
8519 : : and so must be rejected. */
8520 : 56810619 : bool fetch_op = true;
8521 : 56810619 : bool orig_format = true;
8522 : 56810619 : tree new_return = NULL_TREE;
8523 : :
8524 : 56810619 : switch (DECL_BUILT_IN_CLASS (function))
8525 : : {
8526 : 8634149 : case BUILT_IN_NORMAL:
8527 : 8634149 : break;
8528 : 32045489 : case BUILT_IN_MD:
8529 : 32045489 : if (targetm.resolve_overloaded_builtin)
8530 : 0 : return targetm.resolve_overloaded_builtin (loc, function, params,
8531 : 0 : complain);
8532 : : else
8533 : : return NULL_TREE;
8534 : : default:
8535 : : return NULL_TREE;
8536 : : }
8537 : :
8538 : : /* Handle BUILT_IN_NORMAL here. */
8539 : 8634149 : enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
8540 : 8634149 : switch (orig_code)
8541 : : {
8542 : 154 : case BUILT_IN_SPECULATION_SAFE_VALUE_N:
8543 : 154 : {
8544 : 154 : tree new_function, first_param, result;
8545 : 154 : enum built_in_function fncode
8546 : 154 : = speculation_safe_value_resolve_call (function, params, complain);
8547 : :
8548 : 154 : if (fncode == BUILT_IN_NONE)
8549 : 52 : return error_mark_node;
8550 : :
8551 : 102 : first_param = (*params)[0];
8552 : 102 : if (!speculation_safe_value_resolve_params (loc, function, params,
8553 : : complain))
8554 : 31 : return error_mark_node;
8555 : :
8556 : 71 : if (targetm.have_speculation_safe_value (true))
8557 : : {
8558 : 71 : new_function = builtin_decl_explicit (fncode);
8559 : 71 : result = build_function_call_vec (loc, vNULL, new_function, params,
8560 : : NULL);
8561 : :
8562 : 71 : if (result == error_mark_node)
8563 : : return result;
8564 : :
8565 : 71 : return speculation_safe_value_resolve_return (first_param, result);
8566 : : }
8567 : : else
8568 : : {
8569 : : /* This target doesn't have, or doesn't need, active mitigation
8570 : : against incorrect speculative execution. Simply return the
8571 : : first parameter to the builtin. */
8572 : 0 : if (!targetm.have_speculation_safe_value (false))
8573 : : {
8574 : 0 : if (complain)
8575 : : /* The user has invoked __builtin_speculation_safe_value
8576 : : even though __HAVE_SPECULATION_SAFE_VALUE is not
8577 : : defined: emit a warning. */
8578 : 0 : warning_at (
8579 : 0 : input_location, 0,
8580 : : "this target does not define a speculation barrier; "
8581 : : "your program will still execute correctly, "
8582 : : "but incorrect speculation may not be "
8583 : : "restricted");
8584 : : else
8585 : 0 : return error_mark_node;
8586 : : }
8587 : :
8588 : : /* If the optional second argument is present, handle any side
8589 : : effects now. */
8590 : 0 : if (params->length () == 2
8591 : 0 : && TREE_SIDE_EFFECTS ((*params)[1]))
8592 : 0 : return build2 (COMPOUND_EXPR, TREE_TYPE (first_param),
8593 : 0 : (*params)[1], first_param);
8594 : :
8595 : : return first_param;
8596 : : }
8597 : : }
8598 : :
8599 : 81980 : case BUILT_IN_ATOMIC_EXCHANGE:
8600 : 81980 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
8601 : 81980 : case BUILT_IN_ATOMIC_LOAD:
8602 : 81980 : case BUILT_IN_ATOMIC_STORE:
8603 : 81980 : {
8604 : : /* Handle these 4 together so that they can fall through to the next
8605 : : case if the call is transformed to an _N variant. */
8606 : 81980 : switch (orig_code)
8607 : : {
8608 : 632 : case BUILT_IN_ATOMIC_EXCHANGE:
8609 : 632 : {
8610 : 632 : if (resolve_overloaded_atomic_exchange (loc, function, params,
8611 : : &new_return, complain))
8612 : 234 : return new_return;
8613 : : /* Change to the _N variant. */
8614 : : orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
8615 : : break;
8616 : : }
8617 : :
8618 : 20696 : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
8619 : 20696 : {
8620 : 20696 : if (resolve_overloaded_atomic_compare_exchange (
8621 : : loc, function, params, &new_return, complain))
8622 : 1376 : return new_return;
8623 : : /* Change to the _N variant. */
8624 : : orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
8625 : : break;
8626 : : }
8627 : 49263 : case BUILT_IN_ATOMIC_LOAD:
8628 : 49263 : {
8629 : 49263 : if (resolve_overloaded_atomic_load (loc, function, params,
8630 : : &new_return, complain))
8631 : 2777 : return new_return;
8632 : : /* Change to the _N variant. */
8633 : : orig_code = BUILT_IN_ATOMIC_LOAD_N;
8634 : : break;
8635 : : }
8636 : 11389 : case BUILT_IN_ATOMIC_STORE:
8637 : 11389 : {
8638 : 11389 : if (resolve_overloaded_atomic_store (loc, function, params,
8639 : : &new_return, complain))
8640 : 578 : return new_return;
8641 : : /* Change to the _N variant. */
8642 : : orig_code = BUILT_IN_ATOMIC_STORE_N;
8643 : : break;
8644 : : }
8645 : 0 : default:
8646 : 0 : gcc_unreachable ();
8647 : : }
8648 : : }
8649 : : /* FALLTHRU */
8650 : : case BUILT_IN_ATOMIC_EXCHANGE_N:
8651 : : case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
8652 : : case BUILT_IN_ATOMIC_LOAD_N:
8653 : : case BUILT_IN_ATOMIC_STORE_N:
8654 : : fetch_op = false;
8655 : : /* FALLTHRU */
8656 : : case BUILT_IN_ATOMIC_ADD_FETCH_N:
8657 : : case BUILT_IN_ATOMIC_SUB_FETCH_N:
8658 : : case BUILT_IN_ATOMIC_AND_FETCH_N:
8659 : : case BUILT_IN_ATOMIC_NAND_FETCH_N:
8660 : : case BUILT_IN_ATOMIC_XOR_FETCH_N:
8661 : : case BUILT_IN_ATOMIC_OR_FETCH_N:
8662 : : case BUILT_IN_ATOMIC_FETCH_ADD_N:
8663 : : case BUILT_IN_ATOMIC_FETCH_SUB_N:
8664 : : case BUILT_IN_ATOMIC_FETCH_AND_N:
8665 : : case BUILT_IN_ATOMIC_FETCH_NAND_N:
8666 : : case BUILT_IN_ATOMIC_FETCH_XOR_N:
8667 : : case BUILT_IN_ATOMIC_FETCH_OR_N:
8668 : : orig_format = false;
8669 : : /* FALLTHRU */
8670 : : case BUILT_IN_SYNC_FETCH_AND_ADD_N:
8671 : : case BUILT_IN_SYNC_FETCH_AND_SUB_N:
8672 : : case BUILT_IN_SYNC_FETCH_AND_OR_N:
8673 : : case BUILT_IN_SYNC_FETCH_AND_AND_N:
8674 : : case BUILT_IN_SYNC_FETCH_AND_XOR_N:
8675 : : case BUILT_IN_SYNC_FETCH_AND_NAND_N:
8676 : : case BUILT_IN_SYNC_ADD_AND_FETCH_N:
8677 : : case BUILT_IN_SYNC_SUB_AND_FETCH_N:
8678 : : case BUILT_IN_SYNC_OR_AND_FETCH_N:
8679 : : case BUILT_IN_SYNC_AND_AND_FETCH_N:
8680 : : case BUILT_IN_SYNC_XOR_AND_FETCH_N:
8681 : : case BUILT_IN_SYNC_NAND_AND_FETCH_N:
8682 : : case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
8683 : : case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
8684 : : case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
8685 : : case BUILT_IN_SYNC_LOCK_RELEASE_N:
8686 : : {
8687 : : /* The following are not _FETCH_OPs and must be accepted with
8688 : : pointers to _Bool (or C++ bool). */
8689 : : if (fetch_op)
8690 : 67077 : fetch_op = (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
8691 : 67077 : && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
8692 : 66591 : && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
8693 : 67077 : && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
8694 : :
8695 : 318730 : int n = sync_resolve_size (function, params, fetch_op, orig_format,
8696 : : complain);
8697 : 318730 : tree new_function, first_param, result;
8698 : 318730 : enum built_in_function fncode;
8699 : :
8700 : 318730 : if (n == 0)
8701 : 2607 : return error_mark_node;
8702 : :
8703 : 316123 : if (n == -1)
8704 : : {
8705 : : /* complain is related to SFINAE context.
8706 : : _BitInt is not defined in C++, hence can't enter this clause
8707 : : with complain unset. Even if at the abstraction level
8708 : : this complain is unset that still makes sense (whether
8709 : : this function should report an error or not if anything is
8710 : : wrong).
8711 : : Since can't test avoiding an error when this value is false not
8712 : : writing the code and instead asserting value is not set. */
8713 : 528 : gcc_assert (complain);
8714 : 528 : return atomic_bitint_fetch_using_cas_loop (loc, orig_code, function,
8715 : 528 : params);
8716 : : }
8717 : :
8718 : 315595 : fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
8719 : 315595 : new_function = builtin_decl_explicit (fncode);
8720 : 315595 : if (!sync_resolve_params (loc, function, new_function, params,
8721 : : orig_format, complain))
8722 : 164 : return error_mark_node;
8723 : :
8724 : 315431 : first_param = (*params)[0];
8725 : 315431 : result = build_function_call_vec (loc, vNULL, new_function, params,
8726 : : NULL);
8727 : 315431 : if (result == error_mark_node)
8728 : : return result;
8729 : 315419 : if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
8730 : 315419 : && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
8731 : 315112 : && orig_code != BUILT_IN_ATOMIC_STORE_N
8732 : 315112 : && orig_code != BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N)
8733 : 208672 : result = sync_resolve_return (first_param, result, orig_format);
8734 : :
8735 : 315419 : if (fetch_op)
8736 : : /* Prevent -Wunused-value warning. */
8737 : 63572 : TREE_USED (result) = true;
8738 : :
8739 : : /* If new_return is set, assign function to that expr and cast the
8740 : : result to void since the generic interface returned void. */
8741 : 315419 : if (new_return)
8742 : : {
8743 : : /* Cast function result from I{1,2,4,8,16} to the required type. */
8744 : 46884 : if (TREE_CODE (TREE_TYPE (new_return)) == BITINT_TYPE)
8745 : : {
8746 : 595 : struct bitint_info info;
8747 : 595 : unsigned prec = TYPE_PRECISION (TREE_TYPE (new_return));
8748 : 595 : targetm.c.bitint_type_info (prec, &info);
8749 : 595 : if (!info.extended)
8750 : : /* For _BitInt which has the padding bits undefined
8751 : : convert to the _BitInt type rather than VCE to force
8752 : : zero or sign extension. */
8753 : 595 : result = build1 (NOP_EXPR, TREE_TYPE (new_return), result);
8754 : : }
8755 : 46884 : result
8756 : 46884 : = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
8757 : 46884 : result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
8758 : : result);
8759 : 46884 : TREE_SIDE_EFFECTS (result) = 1;
8760 : 46884 : protected_set_expr_location (result, loc);
8761 : 46884 : result = convert (void_type_node, result);
8762 : : }
8763 : : return result;
8764 : : }
8765 : :
8766 : : default:
8767 : : return NULL_TREE;
8768 : : }
8769 : : }
8770 : :
8771 : : /* vector_types_compatible_elements_p is used in type checks of vectors
8772 : : values used as operands of binary operators. Where it returns true, and
8773 : : the other checks of the caller succeed (being vector types in he first
8774 : : place, and matching number of elements), we can just treat the types
8775 : : as essentially the same.
8776 : : Contrast with vector_targets_convertible_p, which is used for vector
8777 : : pointer types, and vector_types_convertible_p, which will allow
8778 : : language-specific matches under the control of flag_lax_vector_conversions,
8779 : : and might still require a conversion. */
8780 : : /* True if vector types T1 and T2 can be inputs to the same binary
8781 : : operator without conversion.
8782 : : We don't check the overall vector size here because some of our callers
8783 : : want to give different error messages when the vectors are compatible
8784 : : except for the element count. */
8785 : :
8786 : : bool
8787 : 1590415 : vector_types_compatible_elements_p (tree t1, tree t2)
8788 : : {
8789 : 3180554 : bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
8790 : 1590415 : t1 = TREE_TYPE (t1);
8791 : 1590415 : t2 = TREE_TYPE (t2);
8792 : :
8793 : 1590415 : enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
8794 : :
8795 : 1590415 : gcc_assert ((INTEGRAL_TYPE_P (t1)
8796 : : || c1 == REAL_TYPE
8797 : : || c1 == FIXED_POINT_TYPE)
8798 : : && (INTEGRAL_TYPE_P (t2)
8799 : : || c2 == REAL_TYPE
8800 : : || c2 == FIXED_POINT_TYPE));
8801 : :
8802 : 1590415 : t1 = c_common_signed_type (t1);
8803 : 1590415 : t2 = c_common_signed_type (t2);
8804 : : /* Equality works here because c_common_signed_type uses
8805 : : TYPE_MAIN_VARIANT. */
8806 : 1590415 : if (t1 == t2)
8807 : : return true;
8808 : 31 : if (opaque && c1 == c2
8809 : 4 : && (INTEGRAL_TYPE_P (t1) || c1 == REAL_TYPE)
8810 : 35 : && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
8811 : 4 : return true;
8812 : : return false;
8813 : : }
8814 : :
8815 : : /* Check for missing format attributes on function pointers. LTYPE is
8816 : : the new type or left-hand side type. RTYPE is the old type or
8817 : : right-hand side type. Returns TRUE if LTYPE is missing the desired
8818 : : attribute. */
8819 : :
8820 : : bool
8821 : 9890 : check_missing_format_attribute (tree ltype, tree rtype)
8822 : : {
8823 : 9890 : tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
8824 : 9890 : tree ra;
8825 : :
8826 : 9899 : for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
8827 : 73 : if (is_attribute_p ("format", get_attribute_name (ra)))
8828 : : break;
8829 : 9890 : if (ra)
8830 : : {
8831 : 64 : tree la;
8832 : 64 : for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
8833 : 24 : if (is_attribute_p ("format", get_attribute_name (la)))
8834 : : break;
8835 : 64 : return !la;
8836 : : }
8837 : : else
8838 : : return false;
8839 : : }
8840 : :
8841 : : /* Setup a TYPE_DECL node as a typedef representation.
8842 : :
8843 : : X is a TYPE_DECL for a typedef statement. Create a brand new
8844 : : ..._TYPE node (which will be just a variant of the existing
8845 : : ..._TYPE node with identical properties) and then install X
8846 : : as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
8847 : :
8848 : : The whole point here is to end up with a situation where each
8849 : : and every ..._TYPE node the compiler creates will be uniquely
8850 : : associated with AT MOST one node representing a typedef name.
8851 : : This way, even though the compiler substitutes corresponding
8852 : : ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
8853 : : early on, later parts of the compiler can always do the reverse
8854 : : translation and get back the corresponding typedef name. For
8855 : : example, given:
8856 : :
8857 : : typedef struct S MY_TYPE;
8858 : : MY_TYPE object;
8859 : :
8860 : : Later parts of the compiler might only know that `object' was of
8861 : : type `struct S' if it were not for code just below. With this
8862 : : code however, later parts of the compiler see something like:
8863 : :
8864 : : struct S' == struct S
8865 : : typedef struct S' MY_TYPE;
8866 : : struct S' object;
8867 : :
8868 : : And they can then deduce (from the node for type struct S') that
8869 : : the original object declaration was:
8870 : :
8871 : : MY_TYPE object;
8872 : :
8873 : : Being able to do this is important for proper support of protoize,
8874 : : and also for generating precise symbolic debugging information
8875 : : which takes full account of the programmer's (typedef) vocabulary.
8876 : :
8877 : : Obviously, we don't want to generate a duplicate ..._TYPE node if
8878 : : the TYPE_DECL node that we are now processing really represents a
8879 : : standard built-in type. */
8880 : :
8881 : : void
8882 : 166045494 : set_underlying_type (tree x)
8883 : : {
8884 : 166045494 : if (x == error_mark_node || TREE_TYPE (x) == error_mark_node)
8885 : : return;
8886 : 166045466 : if (DECL_IS_UNDECLARED_BUILTIN (x) && TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
8887 : : {
8888 : 6251351 : if (TYPE_NAME (TREE_TYPE (x)) == 0)
8889 : 4993904 : TYPE_NAME (TREE_TYPE (x)) = x;
8890 : : }
8891 : 159794115 : else if (DECL_ORIGINAL_TYPE (x))
8892 : 162951 : gcc_checking_assert (TYPE_NAME (TREE_TYPE (x)) == x);
8893 : : else
8894 : : {
8895 : 159631164 : tree tt = TREE_TYPE (x);
8896 : 159631164 : DECL_ORIGINAL_TYPE (x) = tt;
8897 : 159631164 : tt = build_variant_type_copy (tt);
8898 : 159631164 : TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
8899 : 159631164 : TYPE_NAME (tt) = x;
8900 : :
8901 : : /* Mark the type as used only when its type decl is decorated
8902 : : with attribute unused. */
8903 : 159631164 : if (lookup_attribute ("unused", DECL_ATTRIBUTES (x)))
8904 : 250 : TREE_USED (tt) = 1;
8905 : :
8906 : 159631164 : TREE_TYPE (x) = tt;
8907 : : }
8908 : : }
8909 : :
8910 : : /* Return true if it is worth exposing the DECL_ORIGINAL_TYPE of TYPE to
8911 : : the user in diagnostics, false if it would be better to use TYPE itself.
8912 : : TYPE is known to satisfy typedef_variant_p. */
8913 : :
8914 : : bool
8915 : 31058 : user_facing_original_type_p (const_tree type)
8916 : : {
8917 : 31058 : gcc_assert (typedef_variant_p (type));
8918 : 31058 : tree decl = TYPE_NAME (type);
8919 : :
8920 : : /* Look through any typedef in "user" code. */
8921 : 31058 : if (!DECL_IN_SYSTEM_HEADER (decl) && !DECL_IS_UNDECLARED_BUILTIN (decl))
8922 : : return true;
8923 : :
8924 : : /* If the original type is also named and is in the user namespace,
8925 : : assume it too is a user-facing type. */
8926 : 1565 : tree orig_type = DECL_ORIGINAL_TYPE (decl);
8927 : 1565 : if (tree orig_id = TYPE_IDENTIFIER (orig_type))
8928 : 1437 : if (!name_reserved_for_implementation_p (IDENTIFIER_POINTER (orig_id)))
8929 : : return true;
8930 : :
8931 : 710 : switch (TREE_CODE (orig_type))
8932 : : {
8933 : : /* Don't look through to an anonymous vector type, since the syntax
8934 : : we use for them in diagnostics isn't real C or C++ syntax.
8935 : : And if ORIG_TYPE is named but in the implementation namespace,
8936 : : TYPE is likely to be more meaningful to the user. */
8937 : : case VECTOR_TYPE:
8938 : : return false;
8939 : :
8940 : : /* Don't expose anonymous tag types that are presumably meant to be
8941 : : known by their typedef name. Also don't expose tags that are in
8942 : : the implementation namespace, such as:
8943 : :
8944 : : typedef struct __foo foo; */
8945 : : case RECORD_TYPE:
8946 : : case UNION_TYPE:
8947 : : case ENUMERAL_TYPE:
8948 : : return false;
8949 : :
8950 : : /* Look through to anything else. */
8951 : : default:
8952 : : return true;
8953 : : }
8954 : : }
8955 : :
8956 : : /* Record the types used by the current global variable declaration
8957 : : being parsed, so that we can decide later to emit their debug info.
8958 : : Those types are in types_used_by_cur_var_decl, and we are going to
8959 : : store them in the types_used_by_vars_hash hash table.
8960 : : DECL is the declaration of the global variable that has been parsed. */
8961 : :
8962 : : void
8963 : 21379934 : record_types_used_by_current_var_decl (tree decl)
8964 : : {
8965 : 21379934 : gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
8966 : :
8967 : 41829589 : while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
8968 : : {
8969 : 20449655 : tree type = types_used_by_cur_var_decl->pop ();
8970 : 20449655 : types_used_by_var_decl_insert (type, decl);
8971 : : }
8972 : 21379934 : }
8973 : :
8974 : : /* The C and C++ parsers both use vectors to hold function arguments.
8975 : : For efficiency, we keep a cache of unused vectors. This is the
8976 : : cache. */
8977 : :
8978 : : typedef vec<tree, va_gc> *tree_gc_vec;
8979 : : static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
8980 : :
8981 : : /* Return a new vector from the cache. If the cache is empty,
8982 : : allocate a new vector. These vectors are GC'ed, so it is OK if the
8983 : : pointer is not released.. */
8984 : :
8985 : : vec<tree, va_gc> *
8986 : 992444594 : make_tree_vector (void)
8987 : : {
8988 : 992444594 : if (tree_vector_cache && !tree_vector_cache->is_empty ())
8989 : 965749213 : return tree_vector_cache->pop ();
8990 : : else
8991 : : {
8992 : : /* Passing 0 to vec::alloc returns NULL, and our callers require
8993 : : that we always return a non-NULL value. The vector code uses
8994 : : 4 when growing a NULL vector, so we do too. */
8995 : 26695381 : vec<tree, va_gc> *v;
8996 : 26695381 : vec_alloc (v, 4);
8997 : 26695381 : return v;
8998 : : }
8999 : : }
9000 : :
9001 : : /* Release a vector of trees back to the cache. */
9002 : :
9003 : : void
9004 : 1334698719 : release_tree_vector (vec<tree, va_gc> *vec)
9005 : : {
9006 : 1334698719 : if (vec != NULL)
9007 : : {
9008 : 967167572 : if (vec->allocated () >= 16)
9009 : : /* Don't cache vecs that have expanded more than once. On a p64
9010 : : target, vecs double in alloc size with each power of 2 elements, e.g
9011 : : at 16 elements the alloc increases from 128 to 256 bytes. */
9012 : 633583 : vec_free (vec);
9013 : : else
9014 : : {
9015 : 966533989 : vec->truncate (0);
9016 : 966533989 : vec_safe_push (tree_vector_cache, vec);
9017 : : }
9018 : : }
9019 : 1334698719 : }
9020 : :
9021 : : /* Get a new tree vector holding a single tree. */
9022 : :
9023 : : vec<tree, va_gc> *
9024 : 48502238 : make_tree_vector_single (tree t)
9025 : : {
9026 : 48502238 : vec<tree, va_gc> *ret = make_tree_vector ();
9027 : 48502238 : ret->quick_push (t);
9028 : 48502238 : return ret;
9029 : : }
9030 : :
9031 : : /* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
9032 : :
9033 : : vec<tree, va_gc> *
9034 : 4664 : make_tree_vector_from_list (tree list)
9035 : : {
9036 : 4664 : vec<tree, va_gc> *ret = make_tree_vector ();
9037 : 13124 : for (; list; list = TREE_CHAIN (list))
9038 : 8460 : vec_safe_push (ret, TREE_VALUE (list));
9039 : 4664 : return ret;
9040 : : }
9041 : :
9042 : : /* Append to a tree vector V the values of a CONSTRUCTOR CTOR
9043 : : and return the new possibly reallocated vector. */
9044 : :
9045 : : vec<tree, va_gc> *
9046 : 1340995 : append_ctor_to_tree_vector (vec<tree, va_gc> *v, tree ctor)
9047 : : {
9048 : 2528424 : unsigned nelts = vec_safe_length (v) + CONSTRUCTOR_NELTS (ctor);
9049 : 1340995 : vec_safe_reserve (v, CONSTRUCTOR_NELTS (ctor));
9050 : 3327207 : for (unsigned i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
9051 : 1986212 : if (TREE_CODE (CONSTRUCTOR_ELT (ctor, i)->value) == RAW_DATA_CST)
9052 : : {
9053 : 24 : tree raw_data = CONSTRUCTOR_ELT (ctor, i)->value;
9054 : 24 : nelts += RAW_DATA_LENGTH (raw_data) - 1;
9055 : 24 : vec_safe_reserve (v, nelts - v->length ());
9056 : 48 : if (TYPE_PRECISION (TREE_TYPE (raw_data)) > CHAR_BIT
9057 : 24 : || TYPE_UNSIGNED (TREE_TYPE (raw_data)))
9058 : 6264 : for (unsigned j = 0; j < (unsigned) RAW_DATA_LENGTH (raw_data); ++j)
9059 : 6240 : v->quick_push (build_int_cst (TREE_TYPE (raw_data),
9060 : 6240 : RAW_DATA_UCHAR_ELT (raw_data, j)));
9061 : : else
9062 : 0 : for (unsigned j = 0; j < (unsigned) RAW_DATA_LENGTH (raw_data); ++j)
9063 : 0 : v->quick_push (build_int_cst (TREE_TYPE (raw_data),
9064 : 0 : RAW_DATA_SCHAR_ELT (raw_data, j)));
9065 : : }
9066 : : else
9067 : 1986188 : v->quick_push (CONSTRUCTOR_ELT (ctor, i)->value);
9068 : 1340995 : return v;
9069 : : }
9070 : :
9071 : : /* Get a new tree vector of the values of a CONSTRUCTOR. */
9072 : :
9073 : : vec<tree, va_gc> *
9074 : 4676 : make_tree_vector_from_ctor (tree ctor)
9075 : : {
9076 : 4676 : vec<tree,va_gc> *ret
9077 : 4676 : = CONSTRUCTOR_NELTS (ctor) <= 16 ? make_tree_vector () : NULL;
9078 : 4676 : return append_ctor_to_tree_vector (ret, ctor);
9079 : : }
9080 : :
9081 : : /* Get a new tree vector which is a copy of an existing one. */
9082 : :
9083 : : vec<tree, va_gc> *
9084 : 27645417 : make_tree_vector_copy (const vec<tree, va_gc> *orig)
9085 : : {
9086 : 27645417 : vec<tree, va_gc> *ret;
9087 : 27645417 : unsigned int ix;
9088 : 27645417 : tree t;
9089 : :
9090 : 27645417 : ret = make_tree_vector ();
9091 : 27645417 : vec_safe_reserve (ret, vec_safe_length (orig));
9092 : 67166161 : FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
9093 : 11875327 : ret->quick_push (t);
9094 : 27645417 : return ret;
9095 : : }
9096 : :
9097 : : /* Return true if KEYWORD starts a type specifier. */
9098 : :
9099 : : bool
9100 : 16 : keyword_begins_type_specifier (enum rid keyword)
9101 : : {
9102 : 16 : switch (keyword)
9103 : : {
9104 : : case RID_AUTO_TYPE:
9105 : : case RID_INT:
9106 : : case RID_CHAR:
9107 : : case RID_FLOAT:
9108 : : case RID_DOUBLE:
9109 : : case RID_VOID:
9110 : : case RID_UNSIGNED:
9111 : : case RID_LONG:
9112 : : case RID_SHORT:
9113 : : case RID_SIGNED:
9114 : : CASE_RID_FLOATN_NX:
9115 : : case RID_DFLOAT32:
9116 : : case RID_DFLOAT64:
9117 : : case RID_DFLOAT128:
9118 : : case RID_FRACT:
9119 : : case RID_ACCUM:
9120 : : case RID_BOOL:
9121 : : case RID_BITINT:
9122 : : case RID_WCHAR:
9123 : : case RID_CHAR8:
9124 : : case RID_CHAR16:
9125 : : case RID_CHAR32:
9126 : : case RID_SAT:
9127 : : case RID_COMPLEX:
9128 : : case RID_TYPEOF:
9129 : : case RID_STRUCT:
9130 : : case RID_CLASS:
9131 : : case RID_UNION:
9132 : : case RID_ENUM:
9133 : : return true;
9134 : 1 : default:
9135 : 1 : if (keyword >= RID_FIRST_INT_N
9136 : : && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
9137 : 0 : && int_n_enabled_p[keyword-RID_FIRST_INT_N])
9138 : : return true;
9139 : : return false;
9140 : : }
9141 : : }
9142 : :
9143 : : /* Return true if KEYWORD names a type qualifier. */
9144 : :
9145 : : bool
9146 : 11539 : keyword_is_type_qualifier (enum rid keyword)
9147 : : {
9148 : 11539 : switch (keyword)
9149 : : {
9150 : : case RID_CONST:
9151 : : case RID_VOLATILE:
9152 : : case RID_RESTRICT:
9153 : : case RID_ATOMIC:
9154 : : return true;
9155 : 55 : default:
9156 : 55 : return false;
9157 : : }
9158 : : }
9159 : :
9160 : : /* Return true if KEYWORD names a storage class specifier.
9161 : :
9162 : : RID_TYPEDEF is not included in this list despite `typedef' being
9163 : : listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
9164 : : such for syntactic convenience only. */
9165 : :
9166 : : bool
9167 : 11565 : keyword_is_storage_class_specifier (enum rid keyword)
9168 : : {
9169 : 11565 : switch (keyword)
9170 : : {
9171 : : case RID_STATIC:
9172 : : case RID_EXTERN:
9173 : : case RID_REGISTER:
9174 : : case RID_AUTO:
9175 : : case RID_MUTABLE:
9176 : : case RID_THREAD:
9177 : : return true;
9178 : 11539 : default:
9179 : 11539 : return false;
9180 : : }
9181 : : }
9182 : :
9183 : : /* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
9184 : :
9185 : : static bool
9186 : 55 : keyword_is_function_specifier (enum rid keyword)
9187 : : {
9188 : 55 : switch (keyword)
9189 : : {
9190 : : case RID_INLINE:
9191 : : case RID_NORETURN:
9192 : : case RID_VIRTUAL:
9193 : : case RID_EXPLICIT:
9194 : : return true;
9195 : 53 : default:
9196 : 0 : return false;
9197 : : }
9198 : : }
9199 : :
9200 : : /* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
9201 : : declaration-specifier (C99 6.7). */
9202 : :
9203 : : bool
9204 : 11565 : keyword_is_decl_specifier (enum rid keyword)
9205 : : {
9206 : 11565 : if (keyword_is_storage_class_specifier (keyword)
9207 : 11539 : || keyword_is_type_qualifier (keyword)
9208 : 11565 : || keyword_is_function_specifier (keyword))
9209 : 11512 : return true;
9210 : :
9211 : 53 : switch (keyword)
9212 : : {
9213 : : case RID_TYPEDEF:
9214 : : case RID_FRIEND:
9215 : : case RID_CONSTEXPR:
9216 : : case RID_CONSTINIT:
9217 : : return true;
9218 : : default:
9219 : : return false;
9220 : : }
9221 : : }
9222 : :
9223 : : /* Initialize language-specific-bits of tree_contains_struct. */
9224 : :
9225 : : void
9226 : 206315 : c_common_init_ts (void)
9227 : : {
9228 : 206315 : MARK_TS_EXP (SIZEOF_EXPR);
9229 : 206315 : MARK_TS_EXP (PAREN_SIZEOF_EXPR);
9230 : 206315 : MARK_TS_EXP (C_MAYBE_CONST_EXPR);
9231 : 206315 : MARK_TS_EXP (EXCESS_PRECISION_EXPR);
9232 : 206315 : MARK_TS_EXP (BREAK_STMT);
9233 : 206315 : MARK_TS_EXP (CONTINUE_STMT);
9234 : 206315 : MARK_TS_EXP (DO_STMT);
9235 : 206315 : MARK_TS_EXP (FOR_STMT);
9236 : 206315 : MARK_TS_EXP (SWITCH_STMT);
9237 : 206315 : MARK_TS_EXP (WHILE_STMT);
9238 : :
9239 : 206315 : MARK_TS_DECL_COMMON (CONCEPT_DECL);
9240 : 206315 : }
9241 : :
9242 : : /* Build a user-defined numeric literal out of an integer constant type VALUE
9243 : : with identifier SUFFIX. */
9244 : :
9245 : : tree
9246 : 919559 : build_userdef_literal (tree suffix_id, tree value,
9247 : : enum overflow_type overflow, tree num_string)
9248 : : {
9249 : 919559 : tree literal = make_node (USERDEF_LITERAL);
9250 : 919559 : USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
9251 : 919559 : USERDEF_LITERAL_VALUE (literal) = value;
9252 : 919559 : USERDEF_LITERAL_OVERFLOW (literal) = overflow;
9253 : 919559 : USERDEF_LITERAL_NUM_STRING (literal) = num_string;
9254 : 919559 : return literal;
9255 : : }
9256 : :
9257 : : /* For vector[index], convert the vector to an array of the underlying type.
9258 : : Return true if the resulting ARRAY_REF should not be an lvalue. */
9259 : :
9260 : : bool
9261 : 8590964 : convert_vector_to_array_for_subscript (location_t loc,
9262 : : tree *vecp, tree index)
9263 : : {
9264 : 8590964 : bool ret = false;
9265 : 8590964 : if (gnu_vector_type_p (TREE_TYPE (*vecp)))
9266 : : {
9267 : 1036077 : tree type = TREE_TYPE (*vecp);
9268 : 1036077 : tree newitype;
9269 : :
9270 : 1036077 : ret = !lvalue_p (*vecp);
9271 : :
9272 : 1036077 : index = fold_for_warn (index);
9273 : : /* Warn out-of-bounds index for vectors only if known. */
9274 : 1036077 : if (poly_int_tree_p (index))
9275 : 1023273 : if (!tree_fits_poly_uint64_p (index)
9276 : 1023273 : || known_ge (tree_to_poly_uint64 (index),
9277 : : TYPE_VECTOR_SUBPARTS (type)))
9278 : 82 : warning_at (loc, OPT_Warray_bounds_, "index value is out of bound");
9279 : :
9280 : : /* We are building an ARRAY_REF so mark the vector as addressable
9281 : : to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
9282 : : for function parameters. */
9283 : 1036077 : c_common_mark_addressable_vec (*vecp);
9284 : :
9285 : : /* Make sure qualifiers are copied from the vector type to the new element
9286 : : of the array type. */
9287 : 1036077 : newitype = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
9288 : :
9289 : 1036077 : *vecp = build1 (VIEW_CONVERT_EXPR,
9290 : : build_array_type_nelts (newitype,
9291 : : TYPE_VECTOR_SUBPARTS (type)),
9292 : : *vecp);
9293 : : }
9294 : 8590964 : return ret;
9295 : : }
9296 : :
9297 : : /* Determine which of the operands, if any, is a scalar that needs to be
9298 : : converted to a vector, for the range of operations. */
9299 : : enum stv_conv
9300 : 3418 : scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
9301 : : bool complain)
9302 : : {
9303 : 3418 : tree type0 = TREE_TYPE (op0);
9304 : 3418 : tree type1 = TREE_TYPE (op1);
9305 : 3418 : bool integer_only_op = false;
9306 : 3418 : enum stv_conv ret = stv_firstarg;
9307 : :
9308 : 3418 : gcc_assert (gnu_vector_type_p (type0) || gnu_vector_type_p (type1));
9309 : 3418 : switch (code)
9310 : : {
9311 : : /* Most GENERIC binary expressions require homogeneous arguments.
9312 : : LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
9313 : : argument that is a vector and a second one that is a scalar, so
9314 : : we never return stv_secondarg for them. */
9315 : 948 : case RSHIFT_EXPR:
9316 : 948 : case LSHIFT_EXPR:
9317 : 948 : if (TREE_CODE (type0) == INTEGER_TYPE
9318 : 948 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9319 : : {
9320 : 50 : if (unsafe_conversion_p (TREE_TYPE (type1), op0,
9321 : : NULL_TREE, false))
9322 : : {
9323 : 0 : if (complain)
9324 : 0 : error_at (loc, "conversion of scalar %qT to vector %qT "
9325 : : "involves truncation", type0, type1);
9326 : 0 : return stv_error;
9327 : : }
9328 : : else
9329 : : return stv_firstarg;
9330 : : }
9331 : : break;
9332 : :
9333 : 311 : case BIT_IOR_EXPR:
9334 : 311 : case BIT_XOR_EXPR:
9335 : 311 : case BIT_AND_EXPR:
9336 : 311 : integer_only_op = true;
9337 : : /* fall through */
9338 : :
9339 : 2458 : case VEC_COND_EXPR:
9340 : :
9341 : 2458 : case PLUS_EXPR:
9342 : 2458 : case MINUS_EXPR:
9343 : 2458 : case MULT_EXPR:
9344 : 2458 : case TRUNC_DIV_EXPR:
9345 : 2458 : case CEIL_DIV_EXPR:
9346 : 2458 : case FLOOR_DIV_EXPR:
9347 : 2458 : case ROUND_DIV_EXPR:
9348 : 2458 : case EXACT_DIV_EXPR:
9349 : 2458 : case TRUNC_MOD_EXPR:
9350 : 2458 : case FLOOR_MOD_EXPR:
9351 : 2458 : case RDIV_EXPR:
9352 : 2458 : case EQ_EXPR:
9353 : 2458 : case NE_EXPR:
9354 : 2458 : case LE_EXPR:
9355 : 2458 : case GE_EXPR:
9356 : 2458 : case LT_EXPR:
9357 : 2458 : case GT_EXPR:
9358 : : /* What about UNLT_EXPR? */
9359 : 2458 : if (gnu_vector_type_p (type0))
9360 : : {
9361 : 1812 : ret = stv_secondarg;
9362 : 1812 : std::swap (type0, type1);
9363 : 1812 : std::swap (op0, op1);
9364 : : }
9365 : :
9366 : 2458 : if (TREE_CODE (type0) == INTEGER_TYPE
9367 : 2458 : && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9368 : : {
9369 : 1748 : if (unsafe_conversion_p (TREE_TYPE (type1), op0,
9370 : : NULL_TREE, false))
9371 : : {
9372 : 11 : if (complain)
9373 : 8 : error_at (loc, "conversion of scalar %qT to vector %qT "
9374 : : "involves truncation", type0, type1);
9375 : 11 : return stv_error;
9376 : : }
9377 : : return ret;
9378 : : }
9379 : 710 : else if (!integer_only_op
9380 : : /* Allow integer --> real conversion if safe. */
9381 : 710 : && (SCALAR_FLOAT_TYPE_P (type0)
9382 : 468 : || TREE_CODE (type0) == INTEGER_TYPE)
9383 : 1420 : && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
9384 : : {
9385 : 686 : if (unsafe_conversion_p (TREE_TYPE (type1), op0,
9386 : : NULL_TREE, false))
9387 : : {
9388 : 22 : if (complain)
9389 : 22 : error_at (loc, "conversion of scalar %qT to vector %qT "
9390 : : "involves truncation", type0, type1);
9391 : 22 : return stv_error;
9392 : : }
9393 : : return ret;
9394 : : }
9395 : : default:
9396 : : break;
9397 : : }
9398 : :
9399 : : return stv_nothing;
9400 : : }
9401 : :
9402 : : /* Return the alignment of std::max_align_t.
9403 : :
9404 : : [support.types.layout] The type max_align_t is a POD type whose alignment
9405 : : requirement is at least as great as that of every scalar type, and whose
9406 : : alignment requirement is supported in every context. */
9407 : :
9408 : : unsigned
9409 : 248824 : max_align_t_align ()
9410 : : {
9411 : 248824 : unsigned int max_align = MAX (TYPE_ALIGN (long_long_integer_type_node),
9412 : : TYPE_ALIGN (long_double_type_node));
9413 : 248824 : if (float128_type_node != NULL_TREE)
9414 : 248824 : max_align = MAX (max_align, TYPE_ALIGN (float128_type_node));
9415 : 248824 : return max_align;
9416 : : }
9417 : :
9418 : : /* Return true iff ALIGN is an integral constant that is a fundamental
9419 : : alignment, as defined by [basic.align] in the c++-11
9420 : : specifications.
9421 : :
9422 : : That is:
9423 : :
9424 : : [A fundamental alignment is represented by an alignment less than or
9425 : : equal to the greatest alignment supported by the implementation
9426 : : in all contexts, which is equal to alignof(max_align_t)]. */
9427 : :
9428 : : bool
9429 : 0 : cxx_fundamental_alignment_p (unsigned align)
9430 : : {
9431 : 0 : return (align <= max_align_t_align ());
9432 : : }
9433 : :
9434 : : /* Return true if T is a pointer to a zero-sized aggregate. */
9435 : :
9436 : : bool
9437 : 1237703 : pointer_to_zero_sized_aggr_p (tree t)
9438 : : {
9439 : 1237703 : if (!POINTER_TYPE_P (t))
9440 : : return false;
9441 : 1237703 : t = TREE_TYPE (t);
9442 : 1237703 : return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
9443 : : }
9444 : :
9445 : : /* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
9446 : : with no library fallback or for an ADDR_EXPR whose operand is such type
9447 : : issues an error pointing to the location LOC.
9448 : : Returns true when the expression has been diagnosed and false
9449 : : otherwise. */
9450 : :
9451 : : bool
9452 : 4257661805 : reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
9453 : : {
9454 : 4257661805 : if (TREE_CODE (expr) == ADDR_EXPR)
9455 : 101568337 : expr = TREE_OPERAND (expr, 0);
9456 : :
9457 : 4257661805 : STRIP_ANY_LOCATION_WRAPPER (expr);
9458 : :
9459 : 4257661805 : if (TREE_TYPE (expr)
9460 : 4257661663 : && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
9461 : 1980077 : && TREE_CODE (expr) == FUNCTION_DECL
9462 : : /* The intersection of DECL_BUILT_IN and DECL_IS_UNDECLARED_BUILTIN avoids
9463 : : false positives for user-declared built-ins such as abs or
9464 : : strlen, and for C++ operators new and delete.
9465 : : The c_decl_implicit() test avoids false positives for implicitly
9466 : : declared built-ins with library fallbacks (such as abs). */
9467 : 1954803 : && fndecl_built_in_p (expr)
9468 : 65926 : && DECL_IS_UNDECLARED_BUILTIN (expr)
9469 : 2372 : && !c_decl_implicit (expr)
9470 : 4257664172 : && !DECL_ASSEMBLER_NAME_SET_P (expr))
9471 : : {
9472 : 220 : if (loc == UNKNOWN_LOCATION)
9473 : 82 : loc = EXPR_LOC_OR_LOC (expr, input_location);
9474 : :
9475 : : /* Reject arguments that are built-in functions with
9476 : : no library fallback. */
9477 : 220 : error_at (loc, "built-in function %qE must be directly called", expr);
9478 : :
9479 : 220 : return true;
9480 : : }
9481 : :
9482 : : return false;
9483 : : }
9484 : :
9485 : : /* Issue an ERROR for an invalid SIZE of array NAME which is null
9486 : : for unnamed arrays. */
9487 : :
9488 : : void
9489 : 1408 : invalid_array_size_error (location_t loc, cst_size_error error,
9490 : : const_tree size, const_tree name)
9491 : : {
9492 : 1408 : tree maxsize = max_object_size ();
9493 : 1408 : switch (error)
9494 : : {
9495 : 0 : case cst_size_not_constant:
9496 : 0 : if (name)
9497 : 0 : error_at (loc, "size of array %qE is not a constant expression",
9498 : : name);
9499 : : else
9500 : 0 : error_at (loc, "size of array is not a constant expression");
9501 : : break;
9502 : 418 : case cst_size_negative:
9503 : 418 : if (name)
9504 : 127 : error_at (loc, "size %qE of array %qE is negative",
9505 : : size, name);
9506 : : else
9507 : 291 : error_at (loc, "size %qE of array is negative",
9508 : : size);
9509 : : break;
9510 : 930 : case cst_size_too_big:
9511 : 930 : if (name)
9512 : 103 : error_at (loc, "size %qE of array %qE exceeds maximum "
9513 : : "object size %qE", size, name, maxsize);
9514 : : else
9515 : 827 : error_at (loc, "size %qE of array exceeds maximum "
9516 : : "object size %qE", size, maxsize);
9517 : : break;
9518 : 60 : case cst_size_overflow:
9519 : 60 : if (name)
9520 : 39 : error_at (loc, "size of array %qE exceeds maximum "
9521 : : "object size %qE", name, maxsize);
9522 : : else
9523 : 21 : error_at (loc, "size of array exceeds maximum "
9524 : : "object size %qE", maxsize);
9525 : : break;
9526 : 0 : default:
9527 : 0 : gcc_unreachable ();
9528 : : }
9529 : 1408 : }
9530 : :
9531 : : /* Check if array size calculations overflow or if the array covers more
9532 : : than half of the address space. Return true if the size of the array
9533 : : is valid, false otherwise. T is either the type of the array or its
9534 : : size, and NAME is the name of the array, or null for unnamed arrays. */
9535 : :
9536 : : bool
9537 : 7198839 : valid_array_size_p (location_t loc, const_tree t, tree name, bool complain)
9538 : : {
9539 : 7198839 : if (t == error_mark_node)
9540 : : return true;
9541 : :
9542 : 7198698 : const_tree size;
9543 : 7198698 : if (TYPE_P (t))
9544 : : {
9545 : 5162591 : if (!COMPLETE_TYPE_P (t))
9546 : : return true;
9547 : 2736715 : size = TYPE_SIZE_UNIT (t);
9548 : : }
9549 : : else
9550 : : size = t;
9551 : :
9552 : 4772822 : if (TREE_CODE (size) != INTEGER_CST)
9553 : : return true;
9554 : :
9555 : 4748595 : cst_size_error error;
9556 : 4748595 : if (valid_constant_size_p (size, &error))
9557 : : return true;
9558 : :
9559 : 1099 : if (!complain)
9560 : : return false;
9561 : :
9562 : 1084 : if (TREE_CODE (TREE_TYPE (size)) == ENUMERAL_TYPE)
9563 : : /* Show the value of the enumerator rather than its name. */
9564 : 16 : size = convert (ssizetype, const_cast<tree> (size));
9565 : :
9566 : 1084 : invalid_array_size_error (loc, error, size, name);
9567 : 1084 : return false;
9568 : : }
9569 : :
9570 : : /* Read SOURCE_DATE_EPOCH from environment to have a deterministic
9571 : : timestamp to replace embedded current dates to get reproducible
9572 : : results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
9573 : :
9574 : : time_t
9575 : 2401 : cb_get_source_date_epoch (cpp_reader *pfile ATTRIBUTE_UNUSED)
9576 : : {
9577 : 2401 : char *source_date_epoch;
9578 : 2401 : int64_t epoch;
9579 : 2401 : char *endptr;
9580 : :
9581 : 2401 : source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
9582 : 2401 : if (!source_date_epoch)
9583 : : return (time_t) -1;
9584 : :
9585 : 3 : errno = 0;
9586 : : #if defined(INT64_T_IS_LONG)
9587 : 3 : epoch = strtol (source_date_epoch, &endptr, 10);
9588 : : #else
9589 : : epoch = strtoll (source_date_epoch, &endptr, 10);
9590 : : #endif
9591 : 3 : if (errno != 0 || endptr == source_date_epoch || *endptr != '\0'
9592 : 2 : || epoch < 0 || epoch > MAX_SOURCE_DATE_EPOCH)
9593 : : {
9594 : 1 : error_at (input_location, "environment variable %qs must "
9595 : : "expand to a non-negative integer less than or equal to %wd",
9596 : : "SOURCE_DATE_EPOCH", MAX_SOURCE_DATE_EPOCH);
9597 : 1 : return (time_t) -1;
9598 : : }
9599 : :
9600 : : return (time_t) epoch;
9601 : : }
9602 : :
9603 : : /* Callback for libcpp for offering spelling suggestions for misspelled
9604 : : directives. GOAL is an unrecognized string; CANDIDATES is a
9605 : : NULL-terminated array of candidate strings. Return the closest
9606 : : match to GOAL within CANDIDATES, or NULL if none are good
9607 : : suggestions. */
9608 : :
9609 : : const char *
9610 : 19 : cb_get_suggestion (cpp_reader *, const char *goal,
9611 : : const char *const *candidates)
9612 : : {
9613 : 19 : best_match<const char *, const char *> bm (goal);
9614 : 204 : while (*candidates)
9615 : 166 : bm.consider (*candidates++);
9616 : 19 : return bm.get_best_meaningful_candidate ();
9617 : : }
9618 : :
9619 : : /* Return the latice point which is the wider of the two FLT_EVAL_METHOD
9620 : : modes X, Y. This isn't just >, as the FLT_EVAL_METHOD values added
9621 : : by C TS 18661-3 for interchange types that are computed in their
9622 : : native precision are larger than the C11 values for evaluating in the
9623 : : precision of float/double/long double. If either mode is
9624 : : FLT_EVAL_METHOD_UNPREDICTABLE, return that. */
9625 : :
9626 : : enum flt_eval_method
9627 : 522392 : excess_precision_mode_join (enum flt_eval_method x,
9628 : : enum flt_eval_method y)
9629 : : {
9630 : 522392 : if (x == FLT_EVAL_METHOD_UNPREDICTABLE
9631 : 522392 : || y == FLT_EVAL_METHOD_UNPREDICTABLE)
9632 : : return FLT_EVAL_METHOD_UNPREDICTABLE;
9633 : :
9634 : : /* GCC only supports one interchange type right now, _Float16. If
9635 : : we're evaluating _Float16 in 16-bit precision, then flt_eval_method
9636 : : will be FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
9637 : 522068 : if (x == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
9638 : : return y;
9639 : 519951 : if (y == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
9640 : : return x;
9641 : :
9642 : : /* Other values for flt_eval_method are directly comparable, and we want
9643 : : the maximum. */
9644 : 519939 : return MAX (x, y);
9645 : : }
9646 : :
9647 : : /* Return the value that should be set for FLT_EVAL_METHOD in the
9648 : : context of ISO/IEC TS 18861-3.
9649 : :
9650 : : This relates to the effective excess precision seen by the user,
9651 : : which is the join point of the precision the target requests for
9652 : : -fexcess-precision={standard,fast,16} and the implicit excess precision
9653 : : the target uses. */
9654 : :
9655 : : static enum flt_eval_method
9656 : 411008 : c_ts18661_flt_eval_method (void)
9657 : : {
9658 : 411008 : enum flt_eval_method implicit
9659 : 411008 : = targetm.c.excess_precision (EXCESS_PRECISION_TYPE_IMPLICIT);
9660 : :
9661 : 300018 : enum excess_precision_type flag_type
9662 : 411008 : = (flag_excess_precision == EXCESS_PRECISION_STANDARD
9663 : 411008 : ? EXCESS_PRECISION_TYPE_STANDARD
9664 : : : (flag_excess_precision == EXCESS_PRECISION_FLOAT16
9665 : 300030 : ? EXCESS_PRECISION_TYPE_FLOAT16
9666 : : : EXCESS_PRECISION_TYPE_FAST));
9667 : :
9668 : 411008 : enum flt_eval_method requested
9669 : 411008 : = targetm.c.excess_precision (flag_type);
9670 : :
9671 : 411008 : return excess_precision_mode_join (implicit, requested);
9672 : : }
9673 : :
9674 : : /* As c_cpp_ts18661_flt_eval_method, but clamps the expected values to
9675 : : those that were permitted by C11. That is to say, eliminates
9676 : : FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
9677 : :
9678 : : static enum flt_eval_method
9679 : 100370 : c_c11_flt_eval_method (void)
9680 : : {
9681 : 100370 : return excess_precision_mode_join (c_ts18661_flt_eval_method (),
9682 : 100370 : FLT_EVAL_METHOD_PROMOTE_TO_FLOAT);
9683 : : }
9684 : :
9685 : : /* Return the value that should be set for FLT_EVAL_METHOD.
9686 : : MAYBE_C11_ONLY_P is TRUE if we should check
9687 : : FLAG_PERMITTED_EVAL_METHODS as to whether we should limit the possible
9688 : : values we can return to those from C99/C11, and FALSE otherwise.
9689 : : See the comments on c_ts18661_flt_eval_method for what value we choose
9690 : : to set here. */
9691 : :
9692 : : int
9693 : 411008 : c_flt_eval_method (bool maybe_c11_only_p)
9694 : : {
9695 : 411008 : if (maybe_c11_only_p
9696 : 205504 : && flag_permitted_flt_eval_methods
9697 : : == PERMITTED_FLT_EVAL_METHODS_C11)
9698 : 100370 : return c_c11_flt_eval_method ();
9699 : : else
9700 : 310638 : return c_ts18661_flt_eval_method ();
9701 : : }
9702 : :
9703 : : /* An enum for get_missing_token_insertion_kind for describing the best
9704 : : place to insert a missing token, if there is one. */
9705 : :
9706 : : enum missing_token_insertion_kind
9707 : : {
9708 : : MTIK_IMPOSSIBLE,
9709 : : MTIK_INSERT_BEFORE_NEXT,
9710 : : MTIK_INSERT_AFTER_PREV
9711 : : };
9712 : :
9713 : : /* Given a missing token of TYPE, determine if it is reasonable to
9714 : : emit a fix-it hint suggesting the insertion of the token, and,
9715 : : if so, where the token should be inserted relative to other tokens.
9716 : :
9717 : : It only makes sense to do this for values of TYPE that are symbols.
9718 : :
9719 : : Some symbols should go before the next token, e.g. in:
9720 : : if flag)
9721 : : we want to insert the missing '(' immediately before "flag",
9722 : : giving:
9723 : : if (flag)
9724 : : rather than:
9725 : : if( flag)
9726 : : These use MTIK_INSERT_BEFORE_NEXT.
9727 : :
9728 : : Other symbols should go after the previous token, e.g. in:
9729 : : if (flag
9730 : : do_something ();
9731 : : we want to insert the missing ')' immediately after the "flag",
9732 : : giving:
9733 : : if (flag)
9734 : : do_something ();
9735 : : rather than:
9736 : : if (flag
9737 : : )do_something ();
9738 : : These use MTIK_INSERT_AFTER_PREV. */
9739 : :
9740 : : static enum missing_token_insertion_kind
9741 : 3225 : get_missing_token_insertion_kind (enum cpp_ttype type)
9742 : : {
9743 : 3225 : switch (type)
9744 : : {
9745 : : /* Insert missing "opening" brackets immediately
9746 : : before the next token. */
9747 : : case CPP_OPEN_SQUARE:
9748 : : case CPP_OPEN_PAREN:
9749 : : return MTIK_INSERT_BEFORE_NEXT;
9750 : :
9751 : : /* Insert other missing symbols immediately after
9752 : : the previous token. */
9753 : 1954 : case CPP_CLOSE_PAREN:
9754 : 1954 : case CPP_CLOSE_SQUARE:
9755 : 1954 : case CPP_SEMICOLON:
9756 : 1954 : case CPP_COMMA:
9757 : 1954 : case CPP_COLON:
9758 : 1954 : return MTIK_INSERT_AFTER_PREV;
9759 : :
9760 : : /* Other kinds of token don't get fix-it hints. */
9761 : 987 : default:
9762 : 987 : return MTIK_IMPOSSIBLE;
9763 : : }
9764 : : }
9765 : :
9766 : : /* Given RICHLOC, a location for a diagnostic describing a missing token
9767 : : of kind TOKEN_TYPE, potentially add a fix-it hint suggesting the
9768 : : insertion of the token.
9769 : :
9770 : : The location of the attempted fix-it hint depends on TOKEN_TYPE:
9771 : : it will either be:
9772 : : (a) immediately after PREV_TOKEN_LOC, or
9773 : :
9774 : : (b) immediately before the primary location within RICHLOC (taken to
9775 : : be that of the token following where the token was expected).
9776 : :
9777 : : If we manage to add a fix-it hint, then the location of the
9778 : : fix-it hint is likely to be more useful as the primary location
9779 : : of the diagnostic than that of the following token, so we swap
9780 : : these locations.
9781 : :
9782 : : For example, given this bogus code:
9783 : : 123456789012345678901234567890
9784 : : 1 | int missing_semicolon (void)
9785 : : 2 | {
9786 : : 3 | return 42
9787 : : 4 | }
9788 : :
9789 : : we will emit:
9790 : :
9791 : : "expected ';' before '}'"
9792 : :
9793 : : RICHLOC's primary location is at the closing brace, so before "swapping"
9794 : : we would emit the error at line 4 column 1:
9795 : :
9796 : : 123456789012345678901234567890
9797 : : 3 | return 42 |< fix-it hint emitted for this line
9798 : : | ; |
9799 : : 4 | } |< "expected ';' before '}'" emitted at this line
9800 : : | ^ |
9801 : :
9802 : : It's more useful for the location of the diagnostic to be at the
9803 : : fix-it hint, so we swap the locations, so the primary location
9804 : : is at the fix-it hint, with the old primary location inserted
9805 : : as a secondary location, giving this, with the error at line 3
9806 : : column 12:
9807 : :
9808 : : 123456789012345678901234567890
9809 : : 3 | return 42 |< "expected ';' before '}'" emitted at this line,
9810 : : | ^ | with fix-it hint
9811 : : 4 | ; |
9812 : : | } |< secondary range emitted here
9813 : : | ~ |. */
9814 : :
9815 : : void
9816 : 3225 : maybe_suggest_missing_token_insertion (rich_location *richloc,
9817 : : enum cpp_ttype token_type,
9818 : : location_t prev_token_loc)
9819 : : {
9820 : 3225 : gcc_assert (richloc);
9821 : :
9822 : 3225 : enum missing_token_insertion_kind mtik
9823 : 3225 : = get_missing_token_insertion_kind (token_type);
9824 : :
9825 : 3225 : switch (mtik)
9826 : : {
9827 : : default:
9828 : : gcc_unreachable ();
9829 : : break;
9830 : :
9831 : : case MTIK_IMPOSSIBLE:
9832 : : return;
9833 : :
9834 : 284 : case MTIK_INSERT_BEFORE_NEXT:
9835 : : /* Attempt to add the fix-it hint before the primary location
9836 : : of RICHLOC. */
9837 : 284 : richloc->add_fixit_insert_before (cpp_type2name (token_type, 0));
9838 : 284 : break;
9839 : :
9840 : 1954 : case MTIK_INSERT_AFTER_PREV:
9841 : : /* Attempt to add the fix-it hint after PREV_TOKEN_LOC. */
9842 : 1954 : richloc->add_fixit_insert_after (prev_token_loc,
9843 : : cpp_type2name (token_type, 0));
9844 : 1954 : break;
9845 : : }
9846 : :
9847 : : /* If we were successful, use the fix-it hint's location as the
9848 : : primary location within RICHLOC, adding the old primary location
9849 : : back as a secondary location. */
9850 : 2238 : if (!richloc->seen_impossible_fixit_p ())
9851 : : {
9852 : 2234 : fixit_hint *hint = richloc->get_last_fixit_hint ();
9853 : 2234 : location_t hint_loc = hint->get_start_loc ();
9854 : 2234 : location_t old_loc = richloc->get_loc ();
9855 : :
9856 : 2234 : richloc->set_range (0, hint_loc, SHOW_RANGE_WITH_CARET);
9857 : 2234 : richloc->add_range (old_loc);
9858 : : }
9859 : : }
9860 : :
9861 : : /* Potentially emit a note about likely missing '&' or '*',
9862 : : depending on EXPR and EXPECTED_TYPE. */
9863 : :
9864 : : void
9865 : 2178 : maybe_emit_indirection_note (location_t loc,
9866 : : tree expr, tree expected_type)
9867 : : {
9868 : 2178 : gcc_assert (expr);
9869 : 2178 : gcc_assert (expected_type);
9870 : :
9871 : 2178 : tree actual_type = TREE_TYPE (expr);
9872 : :
9873 : : /* Missing '&'. */
9874 : 2178 : if (TREE_CODE (expected_type) == POINTER_TYPE
9875 : 1224 : && compatible_types_for_indirection_note_p (actual_type,
9876 : 1224 : TREE_TYPE (expected_type))
9877 : 2256 : && lvalue_p (expr))
9878 : : {
9879 : 43 : gcc_rich_location richloc (loc);
9880 : 43 : richloc.add_fixit_insert_before ("&");
9881 : 43 : inform (&richloc, "possible fix: take the address with %qs", "&");
9882 : 43 : }
9883 : :
9884 : : /* Missing '*'. */
9885 : 2178 : if (TREE_CODE (actual_type) == POINTER_TYPE
9886 : 2178 : && compatible_types_for_indirection_note_p (TREE_TYPE (actual_type),
9887 : : expected_type))
9888 : : {
9889 : 78 : gcc_rich_location richloc (loc);
9890 : 78 : richloc.add_fixit_insert_before ("*");
9891 : 78 : inform (&richloc, "possible fix: dereference with %qs", "*");
9892 : 78 : }
9893 : 2178 : }
9894 : :
9895 : : #if CHECKING_P
9896 : :
9897 : : namespace selftest {
9898 : :
9899 : : /* Verify that fold_for_warn on error_mark_node is safe. */
9900 : :
9901 : : static void
9902 : 3 : test_fold_for_warn ()
9903 : : {
9904 : 3 : ASSERT_EQ (error_mark_node, fold_for_warn (error_mark_node));
9905 : 3 : }
9906 : :
9907 : : /* Run all of the selftests within this file. */
9908 : :
9909 : : static void
9910 : 3 : c_common_cc_tests ()
9911 : : {
9912 : 0 : test_fold_for_warn ();
9913 : 0 : }
9914 : :
9915 : : /* Run all of the tests within c-family. */
9916 : :
9917 : : void
9918 : 3 : c_family_tests (void)
9919 : : {
9920 : 3 : c_common_cc_tests ();
9921 : 3 : c_format_cc_tests ();
9922 : 3 : c_indentation_cc_tests ();
9923 : 3 : c_pretty_print_cc_tests ();
9924 : 3 : c_spellcheck_cc_tests ();
9925 : 3 : c_diagnostic_cc_tests ();
9926 : 3 : c_opt_problem_cc_tests ();
9927 : 3 : }
9928 : :
9929 : : } // namespace selftest
9930 : :
9931 : : #endif /* #if CHECKING_P */
9932 : :
9933 : : /* Attempt to locate a suitable location within FILE for a
9934 : : #include directive to be inserted before.
9935 : : LOC is the location of the relevant diagnostic.
9936 : :
9937 : : Attempt to return the location within FILE immediately
9938 : : after the last #include within that file, or the start of
9939 : : that file if it has no #include directives.
9940 : :
9941 : : Return UNKNOWN_LOCATION if no suitable location is found,
9942 : : or if an error occurs. */
9943 : :
9944 : : static location_t
9945 : 426 : try_to_locate_new_include_insertion_point (const char *file, location_t loc)
9946 : : {
9947 : : /* Locate the last ordinary map within FILE that ended with a #include. */
9948 : 426 : const line_map_ordinary *last_include_ord_map = NULL;
9949 : :
9950 : : /* ...and the next ordinary map within FILE after that one. */
9951 : 426 : const line_map_ordinary *last_ord_map_after_include = NULL;
9952 : :
9953 : : /* ...and the first ordinary map within FILE. */
9954 : 426 : const line_map_ordinary *first_ord_map_in_file = NULL;
9955 : :
9956 : : /* Get ordinary map containing LOC (or its expansion). */
9957 : 426 : const line_map_ordinary *ord_map_for_loc = NULL;
9958 : 426 : linemap_resolve_location (line_table, loc, LRK_MACRO_EXPANSION_POINT,
9959 : : &ord_map_for_loc);
9960 : 426 : gcc_assert (ord_map_for_loc);
9961 : :
9962 : 11965 : for (unsigned int i = 0; i < LINEMAPS_ORDINARY_USED (line_table); i++)
9963 : : {
9964 : 11965 : const line_map_ordinary *ord_map
9965 : 11965 : = LINEMAPS_ORDINARY_MAP_AT (line_table, i);
9966 : :
9967 : 23930 : if (const line_map_ordinary *from
9968 : 11965 : = linemap_included_from_linemap (line_table, ord_map))
9969 : : /* We cannot use pointer equality, because with preprocessed
9970 : : input all filename strings are unique. */
9971 : 9596 : if (0 == strcmp (from->to_file, file))
9972 : : {
9973 : 11965 : last_include_ord_map = from;
9974 : 11965 : last_ord_map_after_include = NULL;
9975 : : }
9976 : :
9977 : : /* Likewise, use strcmp, and reject any line-zero introductory
9978 : : map. */
9979 : 11965 : if (ord_map->to_line && 0 == strcmp (ord_map->to_file, file))
9980 : : {
9981 : 668 : if (!first_ord_map_in_file)
9982 : 426 : first_ord_map_in_file = ord_map;
9983 : 668 : if (last_include_ord_map && !last_ord_map_after_include)
9984 : 166 : last_ord_map_after_include = ord_map;
9985 : : }
9986 : :
9987 : : /* Stop searching when reaching the ord_map containing LOC,
9988 : : as it makes no sense to provide fix-it hints that appear
9989 : : after the diagnostic in question. */
9990 : 11965 : if (ord_map == ord_map_for_loc)
9991 : : break;
9992 : : }
9993 : :
9994 : : /* Determine where to insert the #include. */
9995 : 426 : const line_map_ordinary *ord_map_for_insertion;
9996 : :
9997 : : /* We want the next ordmap in the file after the last one that's a
9998 : : #include, but failing that, the start of the file. */
9999 : 426 : if (last_ord_map_after_include)
10000 : : ord_map_for_insertion = last_ord_map_after_include;
10001 : : else
10002 : 262 : ord_map_for_insertion = first_ord_map_in_file;
10003 : :
10004 : 262 : if (!ord_map_for_insertion)
10005 : : return UNKNOWN_LOCATION;
10006 : :
10007 : : /* The "start_location" is column 0, meaning "the whole line".
10008 : : rich_location and edit_context can't cope with this, so use
10009 : : column 1 instead. */
10010 : 426 : location_t col_0 = ord_map_for_insertion->start_location;
10011 : 426 : return linemap_position_for_loc_and_offset (line_table, col_0, 1);
10012 : : }
10013 : :
10014 : : /* A map from filenames to sets of headers added to them, for
10015 : : ensuring idempotency within maybe_add_include_fixit. */
10016 : :
10017 : : /* The values within the map. We need string comparison as there's
10018 : : no guarantee that two different diagnostics that are recommending
10019 : : adding e.g. "<stdio.h>" are using the same buffer. */
10020 : :
10021 : : typedef hash_set <const char *, false, nofree_string_hash> per_file_includes_t;
10022 : :
10023 : : /* The map itself. We don't need string comparison for the filename keys,
10024 : : as they come from libcpp. */
10025 : :
10026 : : typedef hash_map <const char *, per_file_includes_t *> added_includes_t;
10027 : : static added_includes_t *added_includes;
10028 : :
10029 : : /* Attempt to add a fix-it hint to RICHLOC, adding "#include HEADER\n"
10030 : : in a suitable location within the file of RICHLOC's primary
10031 : : location.
10032 : :
10033 : : This function is idempotent: a header will be added at most once to
10034 : : any given file.
10035 : :
10036 : : If OVERRIDE_LOCATION is true, then if a fix-it is added and will be
10037 : : printed, then RICHLOC's primary location will be replaced by that of
10038 : : the fix-it hint (for use by "inform" notes where the location of the
10039 : : issue has already been reported). */
10040 : :
10041 : : void
10042 : 878 : maybe_add_include_fixit (rich_location *richloc, const char *header,
10043 : : bool override_location)
10044 : : {
10045 : 878 : location_t loc = richloc->get_loc ();
10046 : 878 : const char *file = LOCATION_FILE (loc);
10047 : 878 : if (!file)
10048 : 452 : return;
10049 : :
10050 : : /* Idempotency: don't add the same header more than once to a given file. */
10051 : 878 : if (!added_includes)
10052 : 343 : added_includes = new added_includes_t ();
10053 : 878 : per_file_includes_t *&set = added_includes->get_or_insert (file);
10054 : 878 : if (set)
10055 : 535 : if (set->contains (header))
10056 : : /* ...then we've already added HEADER to that file. */
10057 : : return;
10058 : 426 : if (!set)
10059 : 343 : set = new per_file_includes_t ();
10060 : 426 : set->add (header);
10061 : :
10062 : : /* Attempt to locate a suitable place for the new directive. */
10063 : 426 : location_t include_insert_loc
10064 : 426 : = try_to_locate_new_include_insertion_point (file, loc);
10065 : 426 : if (include_insert_loc == UNKNOWN_LOCATION)
10066 : : return;
10067 : :
10068 : 426 : char *text = xasprintf ("#include %s\n", header);
10069 : 426 : richloc->add_fixit_insert_before (include_insert_loc, text);
10070 : 426 : free (text);
10071 : :
10072 : 426 : if (override_location && global_dc->m_source_printing.enabled)
10073 : : {
10074 : : /* Replace the primary location with that of the insertion point for the
10075 : : fix-it hint.
10076 : :
10077 : : We use SHOW_LINES_WITHOUT_RANGE so that we don't meaningless print a
10078 : : caret for the insertion point (or colorize it).
10079 : :
10080 : : Hence we print e.g.:
10081 : :
10082 : : ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
10083 : : 73 | # include <debug/vector>
10084 : : +++ |+#include <vector>
10085 : : 74 | #endif
10086 : :
10087 : : rather than:
10088 : :
10089 : : ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
10090 : : 73 | # include <debug/vector>
10091 : : +++ |+#include <vector>
10092 : : 74 | #endif
10093 : : | ^
10094 : :
10095 : : avoiding the caret on the first column of line 74. */
10096 : 177 : richloc->set_range (0, include_insert_loc, SHOW_LINES_WITHOUT_RANGE);
10097 : : }
10098 : : }
10099 : :
10100 : : /* Attempt to convert a braced array initializer list CTOR for array
10101 : : TYPE into a STRING_CST for convenience and efficiency. Return
10102 : : the converted string on success or the original ctor on failure.
10103 : : Also, for non-convertable CTORs which contain RAW_DATA_CST values
10104 : : among the elts try to extend the range of RAW_DATA_CSTs. */
10105 : :
10106 : : static tree
10107 : 43895 : braced_list_to_string (tree type, tree ctor, bool member)
10108 : : {
10109 : : /* Ignore non-members with unknown size like arrays with unspecified
10110 : : bound. */
10111 : 43895 : tree typesize = TYPE_SIZE_UNIT (type);
10112 : 43895 : if (!member && !tree_fits_uhwi_p (typesize))
10113 : : return ctor;
10114 : :
10115 : : /* If the target char size differs from the host char size, we'd risk
10116 : : loosing data and getting object sizes wrong by converting to
10117 : : host chars. */
10118 : 43880 : if (TYPE_PRECISION (char_type_node) != CHAR_BIT)
10119 : : return ctor;
10120 : :
10121 : : /* STRING_CST doesn't support wide characters. */
10122 : 43880 : gcc_checking_assert (TYPE_PRECISION (TREE_TYPE (type)) == CHAR_BIT);
10123 : :
10124 : : /* If the array has an explicit bound, use it to constrain the size
10125 : : of the string. If it doesn't, be sure to create a string that's
10126 : : as long as implied by the index of the last zero specified via
10127 : : a designator, as in:
10128 : : const char a[] = { [7] = 0 }; */
10129 : 43880 : unsigned HOST_WIDE_INT maxelts;
10130 : 43880 : if (typesize)
10131 : : {
10132 : 43801 : maxelts = tree_to_uhwi (typesize);
10133 : 43801 : maxelts /= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
10134 : : }
10135 : : else
10136 : : maxelts = HOST_WIDE_INT_M1U;
10137 : :
10138 : : /* Avoid converting initializers for zero-length arrays (but do
10139 : : create them for flexible array members). */
10140 : 43801 : if (!maxelts)
10141 : : return ctor;
10142 : :
10143 : 43731 : unsigned HOST_WIDE_INT nelts = CONSTRUCTOR_NELTS (ctor);
10144 : :
10145 : 43731 : auto_vec<char> str;
10146 : 43731 : str.reserve (nelts + 1);
10147 : :
10148 : 43731 : unsigned HOST_WIDE_INT i, j = HOST_WIDE_INT_M1U;
10149 : 43731 : tree index, value;
10150 : 43731 : bool check_raw_data = false;
10151 : :
10152 : 623596 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, index, value)
10153 : : {
10154 : 579865 : if (check_raw_data)
10155 : : {
10156 : : /* The preprocessor always surrounds CPP_EMBED tokens in between
10157 : : CPP_NUMBER and CPP_COMMA tokens. Try to undo that here now that
10158 : : the whole initializer is parsed. E.g. if we have
10159 : : [0] = 'T', [1] = "his is a #embed tex", [20] = 't'
10160 : : where the middle value is RAW_DATA_CST and in its owner this is
10161 : : surrounded by 'T' and 't' characters, we can create from it just
10162 : : [0] = "This is a #embed text"
10163 : : Similarly if a RAW_DATA_CST needs to be split into two parts
10164 : : because of designated init store but the stored value is actually
10165 : : the same as in the RAW_DATA_OWNER's memory we can merge multiple
10166 : : RAW_DATA_CSTs. */
10167 : 1171 : if (TREE_CODE (value) == RAW_DATA_CST
10168 : 533 : && index
10169 : 533 : && tree_fits_uhwi_p (index))
10170 : : {
10171 : 533 : tree owner = RAW_DATA_OWNER (value);
10172 : 533 : unsigned int start, end, k;
10173 : 533 : if (TREE_CODE (owner) == STRING_CST)
10174 : : {
10175 : 207 : start
10176 : 207 : = RAW_DATA_POINTER (value) - TREE_STRING_POINTER (owner);
10177 : 207 : end = TREE_STRING_LENGTH (owner) - RAW_DATA_LENGTH (value);
10178 : : }
10179 : : else
10180 : : {
10181 : 326 : gcc_checking_assert (TREE_CODE (owner) == RAW_DATA_CST);
10182 : 326 : start
10183 : 326 : = RAW_DATA_POINTER (value) - RAW_DATA_POINTER (owner);
10184 : 326 : end = RAW_DATA_LENGTH (owner) - RAW_DATA_LENGTH (value);
10185 : : }
10186 : 533 : end -= start;
10187 : 533 : unsigned HOST_WIDE_INT l = j == HOST_WIDE_INT_M1U ? i : j;
10188 : 951 : for (k = 0; k < start && k < l; ++k)
10189 : : {
10190 : 444 : constructor_elt *elt = CONSTRUCTOR_ELT (ctor, l - k - 1);
10191 : 888 : if (elt->index == NULL_TREE
10192 : 444 : || !tree_fits_uhwi_p (elt->index)
10193 : 444 : || !tree_fits_shwi_p (elt->value)
10194 : 1327 : || wi::to_widest (index) != (wi::to_widest (elt->index)
10195 : 1322 : + (k + 1)))
10196 : : break;
10197 : 439 : if (TYPE_UNSIGNED (TREE_TYPE (value)))
10198 : : {
10199 : 363 : if (tree_to_shwi (elt->value)
10200 : 363 : != *((const unsigned char *)
10201 : 363 : RAW_DATA_POINTER (value) - k - 1))
10202 : : break;
10203 : : }
10204 : 76 : else if (tree_to_shwi (elt->value)
10205 : 76 : != *((const signed char *)
10206 : 76 : RAW_DATA_POINTER (value) - k - 1))
10207 : : break;
10208 : : }
10209 : 533 : start = k;
10210 : 533 : l = 0;
10211 : 1384 : for (k = 0; k < end && k + 1 < CONSTRUCTOR_NELTS (ctor) - i; ++k)
10212 : : {
10213 : 432 : constructor_elt *elt = CONSTRUCTOR_ELT (ctor, i + k + 1);
10214 : 864 : if (elt->index == NULL_TREE
10215 : 432 : || !tree_fits_uhwi_p (elt->index)
10216 : 1296 : || (wi::to_widest (elt->index)
10217 : 864 : != (wi::to_widest (index)
10218 : 1296 : + (RAW_DATA_LENGTH (value) + l))))
10219 : : break;
10220 : 432 : if (TREE_CODE (elt->value) == RAW_DATA_CST
10221 : 2 : && RAW_DATA_OWNER (elt->value) == RAW_DATA_OWNER (value)
10222 : 432 : && (RAW_DATA_POINTER (elt->value)
10223 : 2 : == RAW_DATA_POINTER (value) + l))
10224 : : {
10225 : 0 : l += RAW_DATA_LENGTH (elt->value);
10226 : 0 : end -= RAW_DATA_LENGTH (elt->value) - 1;
10227 : 0 : continue;
10228 : : }
10229 : 432 : if (!tree_fits_shwi_p (elt->value))
10230 : : break;
10231 : 429 : if (TYPE_UNSIGNED (TREE_TYPE (value)))
10232 : : {
10233 : 375 : if (tree_to_shwi (elt->value)
10234 : 375 : != *((const unsigned char *)
10235 : 375 : RAW_DATA_POINTER (value)
10236 : 375 : + RAW_DATA_LENGTH (value) + k))
10237 : : break;
10238 : : }
10239 : 54 : else if (tree_to_shwi (elt->value)
10240 : 54 : != *((const signed char *)
10241 : 54 : RAW_DATA_POINTER (value)
10242 : 54 : + RAW_DATA_LENGTH (value) + k))
10243 : : break;
10244 : 394 : ++l;
10245 : : }
10246 : 533 : end = k;
10247 : 533 : if (start != 0 || end != 0)
10248 : : {
10249 : 440 : if (j == HOST_WIDE_INT_M1U)
10250 : 402 : j = i - start;
10251 : : else
10252 : 38 : j -= start;
10253 : 440 : RAW_DATA_POINTER (value) -= start;
10254 : 440 : RAW_DATA_LENGTH (value) += start + end;
10255 : 440 : i += end;
10256 : 440 : if (start == 0)
10257 : 22 : CONSTRUCTOR_ELT (ctor, j)->index = index;
10258 : 440 : CONSTRUCTOR_ELT (ctor, j)->value = value;
10259 : 440 : ++j;
10260 : 440 : continue;
10261 : : }
10262 : : }
10263 : 731 : if (j != HOST_WIDE_INT_M1U)
10264 : : {
10265 : 175 : CONSTRUCTOR_ELT (ctor, j)->index = index;
10266 : 175 : CONSTRUCTOR_ELT (ctor, j)->value = value;
10267 : 175 : ++j;
10268 : : }
10269 : 731 : continue;
10270 : 731 : }
10271 : :
10272 : 578694 : unsigned HOST_WIDE_INT idx = i;
10273 : 578694 : if (index)
10274 : : {
10275 : 578694 : if (!tree_fits_uhwi_p (index))
10276 : : {
10277 : 22 : check_raw_data = true;
10278 : 22 : continue;
10279 : : }
10280 : 578672 : idx = tree_to_uhwi (index);
10281 : : }
10282 : :
10283 : : /* auto_vec is limited to UINT_MAX elements. */
10284 : 578672 : if (idx > UINT_MAX)
10285 : : {
10286 : 0 : check_raw_data = true;
10287 : 0 : continue;
10288 : : }
10289 : :
10290 : : /* Avoid non-constant initializers. */
10291 : 578672 : if (!tree_fits_shwi_p (value))
10292 : : {
10293 : 637 : check_raw_data = true;
10294 : 637 : --i;
10295 : 637 : continue;
10296 : : }
10297 : :
10298 : : /* Skip over embedded nuls except the last one (initializer
10299 : : elements are in ascending order of indices). */
10300 : 578035 : HOST_WIDE_INT val = tree_to_shwi (value);
10301 : 578035 : if (!val && i + 1 < nelts)
10302 : 5940 : continue;
10303 : :
10304 : 572095 : if (idx < str.length ())
10305 : : {
10306 : 0 : check_raw_data = true;
10307 : 0 : continue;
10308 : : }
10309 : :
10310 : : /* Bail if the CTOR has a block of more than 256 embedded nuls
10311 : : due to implicitly initialized elements. */
10312 : 572095 : unsigned nchars = (idx - str.length ()) + 1;
10313 : 572095 : if (nchars > 256)
10314 : : {
10315 : 3 : check_raw_data = true;
10316 : 3 : continue;
10317 : : }
10318 : :
10319 : 572092 : if (nchars > 1)
10320 : : {
10321 : 3644 : str.reserve (idx);
10322 : 3644 : str.quick_grow_cleared (idx);
10323 : : }
10324 : :
10325 : 572092 : if (idx >= maxelts)
10326 : : {
10327 : 0 : check_raw_data = true;
10328 : 0 : continue;
10329 : : }
10330 : :
10331 : 572092 : str.safe_insert (idx, val);
10332 : : }
10333 : :
10334 : 43731 : if (check_raw_data)
10335 : : {
10336 : 662 : if (j != HOST_WIDE_INT_M1U)
10337 : 402 : CONSTRUCTOR_ELTS (ctor)->truncate (j);
10338 : 662 : return ctor;
10339 : : }
10340 : :
10341 : : /* Append a nul string termination. */
10342 : 86061 : if (maxelts != HOST_WIDE_INT_M1U && str.length () < maxelts)
10343 : 16731 : str.safe_push (0);
10344 : :
10345 : : /* Build a STRING_CST with the same type as the array. */
10346 : 129207 : tree res = build_string (str.length (), str.begin ());
10347 : 43069 : TREE_TYPE (res) = type;
10348 : 43069 : return res;
10349 : 43731 : }
10350 : :
10351 : : /* Implementation of the two-argument braced_lists_to_string withe
10352 : : the same arguments plus MEMBER which is set for struct members
10353 : : to allow initializers for flexible member arrays. */
10354 : :
10355 : : static tree
10356 : 172358366 : braced_lists_to_strings (tree type, tree ctor, bool member)
10357 : : {
10358 : 172358366 : if (TREE_CODE (ctor) != CONSTRUCTOR)
10359 : : return ctor;
10360 : :
10361 : 4914685 : tree_code code = TREE_CODE (type);
10362 : :
10363 : 4914685 : tree ttp;
10364 : 4914685 : if (code == ARRAY_TYPE)
10365 : 196536 : ttp = TREE_TYPE (type);
10366 : 4718149 : else if (code == RECORD_TYPE)
10367 : : {
10368 : 4673487 : ttp = TREE_TYPE (ctor);
10369 : 4673487 : if (TREE_CODE (ttp) == ARRAY_TYPE)
10370 : : {
10371 : 212736 : type = ttp;
10372 : 212736 : ttp = TREE_TYPE (ttp);
10373 : : }
10374 : : }
10375 : : else
10376 : : return ctor;
10377 : :
10378 : 4868135 : if ((TREE_CODE (ttp) == ARRAY_TYPE || TREE_CODE (ttp) == INTEGER_TYPE)
10379 : 5134542 : && TYPE_STRING_FLAG (ttp))
10380 : 43895 : return braced_list_to_string (type, ctor, member);
10381 : :
10382 : 4826128 : code = TREE_CODE (ttp);
10383 : 4826128 : if (code == ARRAY_TYPE || RECORD_OR_UNION_TYPE_P (ttp))
10384 : : {
10385 : 4512111 : bool rec = RECORD_OR_UNION_TYPE_P (ttp);
10386 : :
10387 : : /* Handle array of arrays or struct member initializers. */
10388 : 4512111 : tree val;
10389 : 4512111 : unsigned HOST_WIDE_INT idx;
10390 : 14717127 : FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), idx, val)
10391 : : {
10392 : 10205016 : val = braced_lists_to_strings (ttp, val, rec);
10393 : 10205016 : CONSTRUCTOR_ELT (ctor, idx)->value = val;
10394 : : }
10395 : : }
10396 : :
10397 : : return ctor;
10398 : : }
10399 : :
10400 : : /* Attempt to convert a CTOR containing braced array initializer lists
10401 : : for array TYPE into one containing STRING_CSTs, for convenience and
10402 : : efficiency. Recurse for arrays of arrays and member initializers.
10403 : : Return the converted CTOR or STRING_CST on success or the original
10404 : : CTOR otherwise. */
10405 : :
10406 : : tree
10407 : 162153350 : braced_lists_to_strings (tree type, tree ctor)
10408 : : {
10409 : 162153350 : return braced_lists_to_strings (type, ctor, false);
10410 : : }
10411 : :
10412 : :
10413 : : /* Emit debug for functions before finalizing early debug. */
10414 : :
10415 : : void
10416 : 176900 : c_common_finalize_early_debug (void)
10417 : : {
10418 : : /* Emit early debug for reachable functions, and by consequence,
10419 : : locally scoped symbols. Also emit debug for extern declared
10420 : : functions that are still reachable at this point. */
10421 : 176900 : struct cgraph_node *cnode;
10422 : 8362526 : FOR_EACH_FUNCTION (cnode)
10423 : 3642530 : if (!cnode->alias && !cnode->thunk
10424 : 7643848 : && (cnode->has_gimple_body_p ()
10425 : 1264606 : || !DECL_IS_UNDECLARED_BUILTIN (cnode->decl)))
10426 : 3428278 : (*debug_hooks->early_global_decl) (cnode->decl);
10427 : 176900 : }
10428 : :
10429 : : /* Determine whether TYPE is an ISO C99 flexible array member type "[]". */
10430 : : bool
10431 : 7510281 : c_flexible_array_member_type_p (const_tree type)
10432 : : {
10433 : 7510281 : if (TREE_CODE (type) == ARRAY_TYPE
10434 : 2662273 : && TYPE_SIZE (type) == NULL_TREE
10435 : 324446 : && TYPE_DOMAIN (type) != NULL_TREE
10436 : 7834727 : && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE)
10437 : 324446 : return true;
10438 : :
10439 : : return false;
10440 : : }
10441 : :
10442 : : /* Get the LEVEL of the strict_flex_array for the ARRAY_FIELD based on the
10443 : : values of attribute strict_flex_array and the flag_strict_flex_arrays. */
10444 : : unsigned int
10445 : 537557 : c_strict_flex_array_level_of (tree array_field)
10446 : : {
10447 : 537557 : gcc_assert (TREE_CODE (array_field) == FIELD_DECL);
10448 : 537557 : unsigned int strict_flex_array_level = flag_strict_flex_arrays;
10449 : :
10450 : 537557 : tree attr_strict_flex_array
10451 : 537557 : = lookup_attribute ("strict_flex_array", DECL_ATTRIBUTES (array_field));
10452 : : /* If there is a strict_flex_array attribute attached to the field,
10453 : : override the flag_strict_flex_arrays. */
10454 : 537557 : if (attr_strict_flex_array)
10455 : : {
10456 : : /* Get the value of the level first from the attribute. */
10457 : 6 : unsigned HOST_WIDE_INT attr_strict_flex_array_level = 0;
10458 : 6 : gcc_assert (TREE_VALUE (attr_strict_flex_array) != NULL_TREE);
10459 : 6 : attr_strict_flex_array = TREE_VALUE (attr_strict_flex_array);
10460 : 6 : gcc_assert (TREE_VALUE (attr_strict_flex_array) != NULL_TREE);
10461 : 6 : attr_strict_flex_array = TREE_VALUE (attr_strict_flex_array);
10462 : 6 : gcc_assert (tree_fits_uhwi_p (attr_strict_flex_array));
10463 : 6 : attr_strict_flex_array_level = tree_to_uhwi (attr_strict_flex_array);
10464 : :
10465 : : /* The attribute has higher priority than flag_struct_flex_array. */
10466 : 6 : strict_flex_array_level = attr_strict_flex_array_level;
10467 : : }
10468 : 537557 : return strict_flex_array_level;
10469 : : }
10470 : :
10471 : : /* Map from identifiers to booleans. Value is true for features, and
10472 : : false for extensions. Used to implement __has_{feature,extension}. */
10473 : :
10474 : : using feature_map_t = hash_map <tree, bool>;
10475 : : static feature_map_t *feature_map;
10476 : :
10477 : : /* Register a feature for __has_{feature,extension}. FEATURE_P is true
10478 : : if the feature identified by NAME is a feature (as opposed to an
10479 : : extension). */
10480 : :
10481 : : void
10482 : 1392291 : c_common_register_feature (const char *name, bool feature_p)
10483 : : {
10484 : 1392291 : bool dup = feature_map->put (get_identifier (name), feature_p);
10485 : 1392291 : gcc_checking_assert (!dup);
10486 : 1392291 : }
10487 : :
10488 : : /* Lazily initialize hash table for __has_{feature,extension},
10489 : : dispatching to the appropriate front end to register language-specific
10490 : : features. */
10491 : :
10492 : : static void
10493 : 45623 : init_has_feature ()
10494 : : {
10495 : 45623 : gcc_checking_assert (!feature_map);
10496 : 45623 : feature_map = new feature_map_t;
10497 : :
10498 : 547476 : for (unsigned i = 0; i < ARRAY_SIZE (has_feature_table); i++)
10499 : : {
10500 : 501853 : const hf_feature_info *info = has_feature_table + i;
10501 : :
10502 : 501853 : if ((info->flags & HF_FLAG_SANITIZE) && !(flag_sanitize & info->mask))
10503 : 227278 : continue;
10504 : :
10505 : 274575 : const bool feature_p = !(info->flags & HF_FLAG_EXT);
10506 : 274575 : c_common_register_feature (info->ident, feature_p);
10507 : : }
10508 : :
10509 : : /* Register language-specific features. */
10510 : 45623 : c_family_register_lang_features ();
10511 : 45623 : }
10512 : :
10513 : : /* If STRICT_P is true, evaluate __has_feature (IDENT).
10514 : : Otherwise, evaluate __has_extension (IDENT). */
10515 : :
10516 : : bool
10517 : 123616 : has_feature_p (const char *ident, bool strict_p)
10518 : : {
10519 : 123616 : if (!feature_map)
10520 : 45623 : init_has_feature ();
10521 : :
10522 : 123616 : tree name = canonicalize_attr_name (get_identifier (ident));
10523 : 123616 : bool *feat_p = feature_map->get (name);
10524 : 123616 : if (!feat_p)
10525 : : return false;
10526 : :
10527 : 70926 : return !strict_p || *feat_p;
10528 : : }
10529 : :
10530 : : /* This is the slow path of c-common.h's c_hardbool_type_attr. */
10531 : :
10532 : : tree
10533 : 40675 : c_hardbool_type_attr_1 (tree type, tree *false_value, tree *true_value)
10534 : : {
10535 : 40675 : tree attr = lookup_attribute ("hardbool", TYPE_ATTRIBUTES (type));
10536 : 40675 : if (!attr)
10537 : : return attr;
10538 : :
10539 : 11932 : if (false_value)
10540 : 7729 : *false_value = TREE_VALUE (TYPE_VALUES (type));
10541 : :
10542 : 11932 : if (true_value)
10543 : 7729 : *true_value = TREE_VALUE (TREE_CHAIN (TYPE_VALUES (type)));
10544 : :
10545 : : return attr;
10546 : : }
10547 : :
10548 : : #include "gt-c-family-c-common.h"
|