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