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