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