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