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