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