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