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