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