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