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