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