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