]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-common.c
Eliminate source_location in favor of location_t
[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
620e594b
DM
865 return get_location_within_string (parse_in, g_string_concat_db,
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 (),
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
4c7bd361
MS
5126/* Check whether the byte alignment ALIGN is a valid user-specified
5127 alignment less than the supported maximum. If so, return ALIGN's
5128 base-2 log; if not, output an error and return -1. If OBJFILE
5129 then reject alignments greater than MAX_OFILE_ALIGNMENT when
5130 converted to bits. Otherwise, consider valid only alignments
5131 that are less than HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT.
5132 If ALLOW_ZERO then 0 is valid and should result in
5133 a return of -1 with no error. */
5134
b12b1915 5135int
4c7bd361 5136check_user_alignment (const_tree align, bool objfile, bool allow_zero)
943f82e7 5137{
b12b1915
MP
5138 if (error_operand_p (align))
5139 return -1;
4c7bd361 5140
b12b1915
MP
5141 if (TREE_CODE (align) != INTEGER_CST
5142 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
349ae713 5143 {
b12b1915
MP
5144 error ("requested alignment is not an integer constant");
5145 return -1;
349ae713 5146 }
4c7bd361
MS
5147
5148 if (allow_zero && integer_zerop (align))
b12b1915 5149 return -1;
4c7bd361
MS
5150
5151 int log2bitalign;
5152 if (tree_int_cst_sgn (align) == -1
5153 || (log2bitalign = tree_log2 (align)) == -1)
646c0835 5154 {
4c7bd361
MS
5155 error ("requested alignment %qE is not a positive power of 2",
5156 align);
b12b1915 5157 return -1;
646c0835 5158 }
4c7bd361
MS
5159
5160 if (objfile)
349ae713 5161 {
4c7bd361
MS
5162 unsigned maxalign = MAX_OFILE_ALIGNMENT / BITS_PER_UNIT;
5163 if (tree_to_shwi (align) > maxalign)
5164 {
5165 error ("requested alignment %qE exceeds object file maximum %u",
5166 align, maxalign);
5167 return -1;
5168 }
5169 }
5170
5171 if (log2bitalign >= HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT)
5172 {
5173 error ("requested alignment %qE exceeds maximum %u",
5174 align, 1U << (HOST_BITS_PER_INT - 1));
b12b1915 5175 return -1;
349ae713 5176 }
4c7bd361
MS
5177
5178 return log2bitalign;
349ae713
NB
5179}
5180
b12b1915
MP
5181/* Determine the ELF symbol visibility for DECL, which is either a
5182 variable or a function. It is an error to use this function if a
5183 definition of DECL is not available in this translation unit.
5184 Returns true if the final visibility has been determined by this
5185 function; false if the caller is free to make additional
5186 modifications. */
349ae713 5187
b12b1915
MP
5188bool
5189c_determine_visibility (tree decl)
349ae713 5190{
b12b1915 5191 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
349ae713 5192
b12b1915
MP
5193 /* If the user explicitly specified the visibility with an
5194 attribute, honor that. DECL_VISIBILITY will have been set during
5195 the processing of the attribute. We check for an explicit
5196 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
5197 to distinguish the use of an attribute from the use of a "#pragma
5198 GCC visibility push(...)"; in the latter case we still want other
5199 considerations to be able to overrule the #pragma. */
5200 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
5201 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
5202 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
5203 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
5204 return true;
349ae713 5205
b12b1915
MP
5206 /* Set default visibility to whatever the user supplied with
5207 visibility_specified depending on #pragma GCC visibility. */
5208 if (!DECL_VISIBILITY_SPECIFIED (decl))
349ae713 5209 {
b12b1915
MP
5210 if (visibility_options.inpragma
5211 || DECL_VISIBILITY (decl) != default_visibility)
5212 {
5213 DECL_VISIBILITY (decl) = default_visibility;
5214 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
5215 /* If visibility changed and DECL already has DECL_RTL, ensure
5216 symbol flags are updated. */
5217 if (((VAR_P (decl) && TREE_STATIC (decl))
5218 || TREE_CODE (decl) == FUNCTION_DECL)
5219 && DECL_RTL_SET_P (decl))
5220 make_decl_rtl (decl);
5221 }
349ae713 5222 }
b12b1915 5223 return false;
349ae713
NB
5224}
5225
0dba7960
JJ
5226/* Data to communicate through check_function_arguments_recurse between
5227 check_function_nonnull and check_nonnull_arg. */
5228
5229struct nonnull_arg_ctx
5230{
5231 location_t loc;
5232 bool warned_p;
5233};
5234
b12b1915
MP
5235/* Check the argument list of a function call for null in argument slots
5236 that are marked as requiring a non-null pointer argument. The NARGS
0dba7960
JJ
5237 arguments are passed in the array ARGARRAY. Return true if we have
5238 warned. */
349ae713 5239
0dba7960 5240static bool
b12b1915 5241check_function_nonnull (location_t loc, tree attrs, int nargs, tree *argarray)
349ae713 5242{
b12b1915
MP
5243 tree a;
5244 int i;
349ae713 5245
b12b1915
MP
5246 attrs = lookup_attribute ("nonnull", attrs);
5247 if (attrs == NULL_TREE)
0dba7960 5248 return false;
349ae713 5249
b12b1915
MP
5250 a = attrs;
5251 /* See if any of the nonnull attributes has no arguments. If so,
5252 then every pointer argument is checked (in which case the check
5253 for pointer type is done in check_nonnull_arg). */
5254 if (TREE_VALUE (a) != NULL_TREE)
5255 do
5256 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
5257 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
52bf96d2 5258
0dba7960 5259 struct nonnull_arg_ctx ctx = { loc, false };
b12b1915
MP
5260 if (a != NULL_TREE)
5261 for (i = 0; i < nargs; i++)
0dba7960 5262 check_function_arguments_recurse (check_nonnull_arg, &ctx, argarray[i],
b12b1915
MP
5263 i + 1);
5264 else
52bf96d2 5265 {
b12b1915
MP
5266 /* Walk the argument list. If we encounter an argument number we
5267 should check for non-null, do it. */
5268 for (i = 0; i < nargs; i++)
52bf96d2 5269 {
b12b1915
MP
5270 for (a = attrs; ; a = TREE_CHAIN (a))
5271 {
5272 a = lookup_attribute ("nonnull", a);
5273 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
5274 break;
5275 }
5276
5277 if (a != NULL_TREE)
0dba7960 5278 check_function_arguments_recurse (check_nonnull_arg, &ctx,
b12b1915 5279 argarray[i], i + 1);
52bf96d2 5280 }
52bf96d2 5281 }
0dba7960 5282 return ctx.warned_p;
52bf96d2 5283}
e45abe1f 5284
b12b1915
MP
5285/* Check that the Nth argument of a function call (counting backwards
5286 from the end) is a (pointer)0. The NARGS arguments are passed in the
5287 array ARGARRAY. */
52bf96d2 5288
b12b1915
MP
5289static void
5290check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
52bf96d2 5291{
b12b1915
MP
5292 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
5293
5294 if (attr)
52bf96d2 5295 {
b12b1915
MP
5296 int len = 0;
5297 int pos = 0;
5298 tree sentinel;
5299 function_args_iterator iter;
5300 tree t;
5301
5302 /* Skip over the named arguments. */
5303 FOREACH_FUNCTION_ARGS (fntype, t, iter)
52bf96d2 5304 {
b12b1915
MP
5305 if (len == nargs)
5306 break;
5307 len++;
52bf96d2 5308 }
52bf96d2 5309
b12b1915
MP
5310 if (TREE_VALUE (attr))
5311 {
5312 tree p = TREE_VALUE (TREE_VALUE (attr));
5313 pos = TREE_INT_CST_LOW (p);
5314 }
52bf96d2 5315
b12b1915
MP
5316 /* The sentinel must be one of the varargs, i.e.
5317 in position >= the number of fixed arguments. */
5318 if ((nargs - 1 - pos) < len)
5319 {
5320 warning (OPT_Wformat_,
5321 "not enough variable arguments to fit a sentinel");
5322 return;
5323 }
77bc5132 5324
b12b1915 5325 /* Validate the sentinel. */
b2bf438c 5326 sentinel = fold_for_warn (argarray[nargs - 1 - pos]);
b12b1915
MP
5327 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
5328 || !integer_zerop (sentinel))
5329 /* Although __null (in C++) is only an integer we allow it
5330 nevertheless, as we are guaranteed that it's exactly
5331 as wide as a pointer, and we don't want to force
5332 users to cast the NULL they have written there.
5333 We warn with -Wstrict-null-sentinel, though. */
5334 && (warn_strict_null_sentinel || null_node != sentinel))
5335 warning (OPT_Wformat_, "missing sentinel in function call");
77bc5132 5336 }
77bc5132
JJ
5337}
5338
e9b9fa4c
MS
5339/* Check that the same argument isn't passed to two or more
5340 restrict-qualified formal and issue a -Wrestrict warning
5341 if it is. Return true if a warning has been issued. */
4227c9ad 5342
e9b9fa4c 5343static bool
4227c9ad 5344check_function_restrict (const_tree fndecl, const_tree fntype,
b2bf438c 5345 int nargs, tree *unfolded_argarray)
4227c9ad
JJ
5346{
5347 int i;
cc8bea0a 5348 tree parms = TYPE_ARG_TYPES (fntype);
4227c9ad 5349
b2bf438c
DM
5350 /* Call fold_for_warn on all of the arguments. */
5351 auto_vec<tree> argarray (nargs);
5352 for (i = 0; i < nargs; i++)
5353 argarray.quick_push (fold_for_warn (unfolded_argarray[i]));
5354
4227c9ad 5355 if (fndecl
cc8bea0a
MS
5356 && TREE_CODE (fndecl) == FUNCTION_DECL)
5357 {
e9b9fa4c
MS
5358 /* Avoid diagnosing calls built-ins with a zero size/bound
5359 here. They are checked in more detail elsewhere. */
3d78e008 5360 if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
e9b9fa4c
MS
5361 && nargs == 3
5362 && TREE_CODE (argarray[2]) == INTEGER_CST
5363 && integer_zerop (argarray[2]))
5364 return false;
cc8bea0a
MS
5365
5366 if (DECL_ARGUMENTS (fndecl))
5367 parms = DECL_ARGUMENTS (fndecl);
5368 }
4227c9ad
JJ
5369
5370 for (i = 0; i < nargs; i++)
5371 TREE_VISITED (argarray[i]) = 0;
5372
e9b9fa4c
MS
5373 bool warned = false;
5374
4227c9ad
JJ
5375 for (i = 0; i < nargs && parms && parms != void_list_node; i++)
5376 {
5377 tree type;
5378 if (TREE_CODE (parms) == PARM_DECL)
5379 {
5380 type = TREE_TYPE (parms);
5381 parms = DECL_CHAIN (parms);
5382 }
5383 else
5384 {
5385 type = TREE_VALUE (parms);
5386 parms = TREE_CHAIN (parms);
5387 }
5388 if (POINTER_TYPE_P (type)
5389 && TYPE_RESTRICT (type)
5390 && !TYPE_READONLY (TREE_TYPE (type)))
b2bf438c 5391 warned |= warn_for_restrict (i, argarray.address (), nargs);
4227c9ad
JJ
5392 }
5393
5394 for (i = 0; i < nargs; i++)
5395 TREE_VISITED (argarray[i]) = 0;
e9b9fa4c
MS
5396
5397 return warned;
4227c9ad
JJ
5398}
5399
b12b1915
MP
5400/* Helper for check_function_nonnull; given a list of operands which
5401 must be non-null in ARGS, determine if operand PARAM_NUM should be
5402 checked. */
e664c61c 5403
b12b1915
MP
5404static bool
5405nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
e664c61c 5406{
b12b1915 5407 unsigned HOST_WIDE_INT arg_num = 0;
ce6923c5 5408
b12b1915 5409 for (; args; args = TREE_CHAIN (args))
ce6923c5 5410 {
b12b1915 5411 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
ce6923c5 5412
b12b1915 5413 gcc_assert (found);
ce6923c5 5414
b12b1915
MP
5415 if (arg_num == param_num)
5416 return true;
5434dc07 5417 }
b12b1915 5418 return false;
5434dc07
MD
5419}
5420
b12b1915
MP
5421/* Check that the function argument PARAM (which is operand number
5422 PARAM_NUM) is non-null. This is called by check_function_nonnull
5423 via check_function_arguments_recurse. */
349ae713 5424
b12b1915
MP
5425static void
5426check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
349ae713 5427{
0dba7960 5428 struct nonnull_arg_ctx *pctx = (struct nonnull_arg_ctx *) ctx;
349ae713 5429
b12b1915
MP
5430 /* Just skip checking the argument if it's not a pointer. This can
5431 happen if the "nonnull" attribute was given without an operand
5432 list (which means to check every pointer argument). */
86631ea3 5433
b12b1915
MP
5434 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
5435 return;
86631ea3 5436
f41b7612
JJ
5437 /* Diagnose the simple cases of null arguments. */
5438 if (integer_zerop (fold_for_warn (param)))
0dba7960
JJ
5439 {
5440 warning_at (pctx->loc, OPT_Wnonnull, "null argument where non-null "
5441 "required (argument %lu)", (unsigned long) param_num);
5442 pctx->warned_p = true;
5443 }
86631ea3
MJ
5444}
5445
b12b1915
MP
5446/* Helper for nonnull attribute handling; fetch the operand number
5447 from the attribute argument list. */
185c9e56 5448
b12b1915
MP
5449bool
5450get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
185c9e56 5451{
b12b1915
MP
5452 /* Verify the arg number is a small constant. */
5453 if (tree_fits_uhwi_p (arg_num_expr))
185c9e56 5454 {
7d2f0f9b 5455 *valp = tree_to_uhwi (arg_num_expr);
b12b1915 5456 return true;
185c9e56 5457 }
b12b1915
MP
5458 else
5459 return false;
3b1661a9
ES
5460}
5461
ab442df7
MM
5462/* Arguments being collected for optimization. */
5463typedef const char *const_char_p; /* For DEF_VEC_P. */
9771b263 5464static GTY(()) vec<const_char_p, va_gc> *optimize_args;
ab442df7
MM
5465
5466
5467/* Inner function to convert a TREE_LIST to argv string to parse the optimize
5468 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
5469 false for #pragma GCC optimize. */
5470
5471bool
5472parse_optimize_options (tree args, bool attr_p)
5473{
5474 bool ret = true;
5475 unsigned opt_argc;
5476 unsigned i;
5477 const char **opt_argv;
6e2f1956
JM
5478 struct cl_decoded_option *decoded_options;
5479 unsigned int decoded_options_count;
ab442df7
MM
5480 tree ap;
5481
5482 /* Build up argv vector. Just in case the string is stored away, use garbage
5483 collected strings. */
9771b263
DN
5484 vec_safe_truncate (optimize_args, 0);
5485 vec_safe_push (optimize_args, (const char *) NULL);
ab442df7
MM
5486
5487 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
5488 {
5489 tree value = TREE_VALUE (ap);
5490
5491 if (TREE_CODE (value) == INTEGER_CST)
5492 {
5493 char buffer[20];
5494 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9771b263 5495 vec_safe_push (optimize_args, ggc_strdup (buffer));
ab442df7
MM
5496 }
5497
5498 else if (TREE_CODE (value) == STRING_CST)
5499 {
5500 /* Split string into multiple substrings. */
5501 size_t len = TREE_STRING_LENGTH (value);
5502 char *p = ASTRDUP (TREE_STRING_POINTER (value));
5503 char *end = p + len;
5504 char *comma;
5505 char *next_p = p;
5506
5507 while (next_p != NULL)
5508 {
5509 size_t len2;
5510 char *q, *r;
5511
5512 p = next_p;
5513 comma = strchr (p, ',');
5514 if (comma)
5515 {
5516 len2 = comma - p;
5517 *comma = '\0';
5518 next_p = comma+1;
5519 }
5520 else
5521 {
5522 len2 = end - p;
5523 next_p = NULL;
5524 }
5525
a9429e29 5526 r = q = (char *) ggc_alloc_atomic (len2 + 3);
ab442df7
MM
5527
5528 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
5529 options. */
5530 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
5531 {
5532 ret = false;
5533 if (attr_p)
5534 warning (OPT_Wattributes,
a80a7051 5535 "bad option %qs to attribute %<optimize%>", p);
ab442df7
MM
5536 else
5537 warning (OPT_Wpragmas,
a80a7051 5538 "bad option %qs to pragma %<optimize%>", p);
ab442df7
MM
5539 continue;
5540 }
5541
5542 if (*p != '-')
5543 {
5544 *r++ = '-';
5545
5546 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
5547 itself is -Os, and any other switch begins with a -f. */
5548 if ((*p >= '0' && *p <= '9')
5549 || (p[0] == 's' && p[1] == '\0'))
5550 *r++ = 'O';
5551 else if (*p != 'O')
5552 *r++ = 'f';
5553 }
5554
5555 memcpy (r, p, len2);
5556 r[len2] = '\0';
9771b263 5557 vec_safe_push (optimize_args, (const char *) q);
ab442df7
MM
5558 }
5559
5560 }
5561 }
5562
9771b263 5563 opt_argc = optimize_args->length ();
ab442df7
MM
5564 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
5565
5566 for (i = 1; i < opt_argc; i++)
9771b263 5567 opt_argv[i] = (*optimize_args)[i];
ab442df7
MM
5568
5569 /* Now parse the options. */
a75bfaa6
JM
5570 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
5571 &decoded_options,
5572 &decoded_options_count);
bfd67b47
RB
5573 /* Drop non-Optimization options. */
5574 unsigned j = 1;
5575 for (i = 1; i < decoded_options_count; ++i)
5576 {
5577 if (! (cl_options[decoded_options[i].opt_index].flags & CL_OPTIMIZATION))
5578 {
5579 ret = false;
5580 if (attr_p)
5581 warning (OPT_Wattributes,
a80a7051 5582 "bad option %qs to attribute %<optimize%>",
bfd67b47
RB
5583 decoded_options[i].orig_option_with_args_text);
5584 else
5585 warning (OPT_Wpragmas,
a80a7051 5586 "bad option %qs to pragma %<optimize%>",
bfd67b47
RB
5587 decoded_options[i].orig_option_with_args_text);
5588 continue;
5589 }
5590 if (i != j)
5591 decoded_options[j] = decoded_options[i];
5592 j++;
5593 }
5594 decoded_options_count = j;
5595 /* And apply them. */
a75bfaa6 5596 decode_options (&global_options, &global_options_set,
a4d8c676 5597 decoded_options, decoded_options_count,
130fcab0 5598 input_location, global_dc, NULL);
ab442df7 5599
2b7e2984
SE
5600 targetm.override_options_after_change();
5601
9771b263 5602 optimize_args->truncate (0);
ab442df7
MM
5603 return ret;
5604}
5605
81fea426
MP
5606/* Check whether ATTR is a valid attribute fallthrough. */
5607
5608bool
5609attribute_fallthrough_p (tree attr)
5610{
fe366b87
MP
5611 if (attr == error_mark_node)
5612 return false;
81fea426
MP
5613 tree t = lookup_attribute ("fallthrough", attr);
5614 if (t == NULL_TREE)
5615 return false;
5616 /* This attribute shall appear at most once in each attribute-list. */
5617 if (lookup_attribute ("fallthrough", TREE_CHAIN (t)))
5618 warning (OPT_Wattributes, "%<fallthrough%> attribute specified multiple "
5619 "times");
5620 /* No attribute-argument-clause shall be present. */
5621 else if (TREE_VALUE (t) != NULL_TREE)
5622 warning (OPT_Wattributes, "%<fallthrough%> attribute specified with "
5623 "a parameter");
5624 /* Warn if other attributes are found. */
5625 for (t = attr; t != NULL_TREE; t = TREE_CHAIN (t))
5626 {
5627 tree name = get_attribute_name (t);
5628 if (!is_attribute_p ("fallthrough", name))
5629 warning (OPT_Wattributes, "%qE attribute ignored", name);
5630 }
5631 return true;
5632}
5633
b34c7881 5634\f
dde05067 5635/* Check for valid arguments being passed to a function with FNTYPE.
e9b9fa4c
MS
5636 There are NARGS arguments in the array ARGARRAY. LOC should be used
5637 for diagnostics. Return true if either -Wnonnull or -Wrestrict has
b2bf438c
DM
5638 been issued.
5639
5640 The arguments in ARGARRAY may not have been folded yet (e.g. for C++,
5641 to preserve location wrappers); checks that require folded arguments
5642 should call fold_for_warn on them. */
e9b9fa4c 5643
0dba7960 5644bool
4227c9ad 5645check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype,
2f687306 5646 int nargs, tree *argarray, vec<location_t> *arglocs)
b34c7881 5647{
0dba7960
JJ
5648 bool warned_p = false;
5649
b34c7881
JT
5650 /* Check for null being passed in a pointer argument that must be
5651 non-null. We also need to do this if format checking is enabled. */
5652
5653 if (warn_nonnull)
0dba7960
JJ
5654 warned_p = check_function_nonnull (loc, TYPE_ATTRIBUTES (fntype),
5655 nargs, argarray);
b34c7881
JT
5656
5657 /* Check for errors in format strings. */
5658
e6c69da0 5659 if (warn_format || warn_suggest_attribute_format)
2f687306 5660 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray, arglocs);
7876a414
KG
5661
5662 if (warn_format)
dde05067 5663 check_function_sentinel (fntype, nargs, argarray);
4227c9ad
JJ
5664
5665 if (warn_restrict)
e9b9fa4c 5666 warned_p |= check_function_restrict (fndecl, fntype, nargs, argarray);
0dba7960 5667 return warned_p;
b34c7881
JT
5668}
5669
5670/* Generic argument checking recursion routine. PARAM is the argument to
5671 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
5672 once the argument is resolved. CTX is context for the callback. */
5673void
35b1a6fa
AJ
5674check_function_arguments_recurse (void (*callback)
5675 (void *, tree, unsigned HOST_WIDE_INT),
5676 void *ctx, tree param,
5677 unsigned HOST_WIDE_INT param_num)
b34c7881 5678{
1043771b 5679 if (CONVERT_EXPR_P (param)
1344f9a3
JM
5680 && (TYPE_PRECISION (TREE_TYPE (param))
5681 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
b34c7881
JT
5682 {
5683 /* Strip coercion. */
5684 check_function_arguments_recurse (callback, ctx,
6de9cd9a 5685 TREE_OPERAND (param, 0), param_num);
b34c7881
JT
5686 return;
5687 }
5688
5689 if (TREE_CODE (param) == CALL_EXPR)
5690 {
5039610b 5691 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
b34c7881
JT
5692 tree attrs;
5693 bool found_format_arg = false;
5694
5695 /* See if this is a call to a known internationalization function
5696 that modifies a format arg. Such a function may have multiple
5697 format_arg attributes (for example, ngettext). */
5698
5699 for (attrs = TYPE_ATTRIBUTES (type);
5700 attrs;
5701 attrs = TREE_CHAIN (attrs))
5702 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
5703 {
5039610b 5704 tree inner_arg;
b34c7881
JT
5705 tree format_num_expr;
5706 int format_num;
5707 int i;
5039610b 5708 call_expr_arg_iterator iter;
b34c7881
JT
5709
5710 /* Extract the argument number, which was previously checked
5711 to be valid. */
5712 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
b34c7881 5713
6b3b8c27 5714 format_num = tree_to_uhwi (format_num_expr);
b34c7881 5715
5039610b 5716 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
3fa8871b 5717 inner_arg != NULL_TREE;
5039610b 5718 inner_arg = next_call_expr_arg (&iter), i++)
b34c7881
JT
5719 if (i == format_num)
5720 {
5721 check_function_arguments_recurse (callback, ctx,
5039610b 5722 inner_arg, param_num);
b34c7881
JT
5723 found_format_arg = true;
5724 break;
5725 }
5726 }
5727
5728 /* If we found a format_arg attribute and did a recursive check,
5729 we are done with checking this argument. Otherwise, we continue
5730 and this will be considered a non-literal. */
5731 if (found_format_arg)
5732 return;
5733 }
5734
5735 if (TREE_CODE (param) == COND_EXPR)
5736 {
7f26f7df
JM
5737 /* Simplify to avoid warning for an impossible case. */
5738 param = fold_for_warn (param);
5739 if (TREE_CODE (param) == COND_EXPR)
5740 {
5741 /* Check both halves of the conditional expression. */
5742 check_function_arguments_recurse (callback, ctx,
5743 TREE_OPERAND (param, 1),
5744 param_num);
5745 check_function_arguments_recurse (callback, ctx,
5746 TREE_OPERAND (param, 2),
5747 param_num);
5748 return;
5749 }
b34c7881
JT
5750 }
5751
5752 (*callback) (ctx, param, param_num);
5753}
e2500fed 5754
a98c2819
MLI
5755/* Checks for a builtin function FNDECL that the number of arguments
5756 NARGS against the required number REQUIRED and issues an error if
5757 there is a mismatch. Returns true if the number of arguments is
79ce98bc 5758 correct, otherwise false. LOC is the location of FNDECL. */
83322951
RG
5759
5760static bool
79ce98bc
MP
5761builtin_function_validate_nargs (location_t loc, tree fndecl, int nargs,
5762 int required)
83322951
RG
5763{
5764 if (nargs < required)
5765 {
e01b4e16 5766 error_at (loc, "too few arguments to function %qE", fndecl);
83322951
RG
5767 return false;
5768 }
5769 else if (nargs > required)
5770 {
79ce98bc 5771 error_at (loc, "too many arguments to function %qE", fndecl);
83322951
RG
5772 return false;
5773 }
5774 return true;
5775}
5776
79ce98bc
MP
5777/* Helper macro for check_builtin_function_arguments. */
5778#define ARG_LOCATION(N) \
5779 (arg_loc.is_empty () \
5780 ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
5781 : expansion_point_location (arg_loc[(N)]))
5782
83322951 5783/* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
79ce98bc
MP
5784 Returns false if there was an error, otherwise true. LOC is the
5785 location of the function; ARG_LOC is a vector of locations of the
5786 arguments. */
83322951
RG
5787
5788bool
79ce98bc
MP
5789check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
5790 tree fndecl, int nargs, tree *args)
83322951 5791{
3d78e008 5792 if (!fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
83322951
RG
5793 return true;
5794
5795 switch (DECL_FUNCTION_CODE (fndecl))
5796 {
9e878cf1
EB
5797 case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX:
5798 if (!tree_fits_uhwi_p (args[2]))
5799 {
5800 error_at (ARG_LOCATION (2),
5801 "third argument to function %qE must be a constant integer",
5802 fndecl);
5803 return false;
5804 }
5805 /* fall through */
5806
35886f0b
MS
5807 case BUILT_IN_ALLOCA_WITH_ALIGN:
5808 {
5809 /* Get the requested alignment (in bits) if it's a constant
5810 integer expression. */
5811 unsigned HOST_WIDE_INT align
5812 = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
5813
5814 /* Determine if the requested alignment is a power of 2. */
5815 if ((align & (align - 1)))
5816 align = 0;
5817
5818 /* The maximum alignment in bits corresponding to the same
5819 maximum in bytes enforced in check_user_alignment(). */
5820 unsigned maxalign = (UINT_MAX >> 1) + 1;
79ce98bc 5821
35886f0b
MS
5822 /* Reject invalid alignments. */
5823 if (align < BITS_PER_UNIT || maxalign < align)
5824 {
79ce98bc 5825 error_at (ARG_LOCATION (1),
35886f0b
MS
5826 "second argument to function %qE must be a constant "
5827 "integer power of 2 between %qi and %qu bits",
5828 fndecl, BITS_PER_UNIT, maxalign);
5829 return false;
5830 }
79ce98bc 5831 return true;
35886f0b
MS
5832 }
5833
83322951 5834 case BUILT_IN_CONSTANT_P:
79ce98bc 5835 return builtin_function_validate_nargs (loc, fndecl, nargs, 1);
83322951
RG
5836
5837 case BUILT_IN_ISFINITE:
5838 case BUILT_IN_ISINF:
05f41289 5839 case BUILT_IN_ISINF_SIGN:
83322951
RG
5840 case BUILT_IN_ISNAN:
5841 case BUILT_IN_ISNORMAL:
61717a45 5842 case BUILT_IN_SIGNBIT:
79ce98bc 5843 if (builtin_function_validate_nargs (loc, fndecl, nargs, 1))
83322951
RG
5844 {
5845 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
5846 {
79ce98bc
MP
5847 error_at (ARG_LOCATION (0), "non-floating-point argument in "
5848 "call to function %qE", fndecl);
83322951
RG
5849 return false;
5850 }
5851 return true;
5852 }
5853 return false;
5854
5855 case BUILT_IN_ISGREATER:
5856 case BUILT_IN_ISGREATEREQUAL:
5857 case BUILT_IN_ISLESS:
5858 case BUILT_IN_ISLESSEQUAL:
5859 case BUILT_IN_ISLESSGREATER:
5860 case BUILT_IN_ISUNORDERED:
79ce98bc 5861 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2))
83322951
RG
5862 {
5863 enum tree_code code0, code1;
5864 code0 = TREE_CODE (TREE_TYPE (args[0]));
5865 code1 = TREE_CODE (TREE_TYPE (args[1]));
5866 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
5867 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
5868 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
5869 {
79ce98bc
MP
5870 error_at (loc, "non-floating-point arguments in call to "
5871 "function %qE", fndecl);
83322951
RG
5872 return false;
5873 }
5874 return true;
5875 }
5876 return false;
5877
3bf5906b 5878 case BUILT_IN_FPCLASSIFY:
79ce98bc 5879 if (builtin_function_validate_nargs (loc, fndecl, nargs, 6))
3bf5906b 5880 {
79ce98bc 5881 for (unsigned int i = 0; i < 5; i++)
3bf5906b
KG
5882 if (TREE_CODE (args[i]) != INTEGER_CST)
5883 {
79ce98bc
MP
5884 error_at (ARG_LOCATION (i), "non-const integer argument %u in "
5885 "call to function %qE", i + 1, fndecl);
3bf5906b
KG
5886 return false;
5887 }
5888
5889 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
5890 {
79ce98bc
MP
5891 error_at (ARG_LOCATION (5), "non-floating-point argument in "
5892 "call to function %qE", fndecl);
3bf5906b
KG
5893 return false;
5894 }
5895 return true;
5896 }
5897 return false;
5898
45d439ac 5899 case BUILT_IN_ASSUME_ALIGNED:
79ce98bc 5900 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2 + (nargs > 2)))
45d439ac
JJ
5901 {
5902 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
5903 {
79ce98bc
MP
5904 error_at (ARG_LOCATION (2), "non-integer argument 3 in call to "
5905 "function %qE", fndecl);
45d439ac
JJ
5906 return false;
5907 }
5908 return true;
5909 }
5910 return false;
5911
1304953e
JJ
5912 case BUILT_IN_ADD_OVERFLOW:
5913 case BUILT_IN_SUB_OVERFLOW:
5914 case BUILT_IN_MUL_OVERFLOW:
79ce98bc 5915 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
1304953e
JJ
5916 {
5917 unsigned i;
5918 for (i = 0; i < 2; i++)
5919 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
5920 {
79ce98bc
MP
5921 error_at (ARG_LOCATION (i), "argument %u in call to function "
5922 "%qE does not have integral type", i + 1, fndecl);
1304953e
JJ
5923 return false;
5924 }
5925 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
a86451b9 5926 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args[2]))))
1304953e 5927 {
79ce98bc 5928 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
a86451b9
JJ
5929 "does not have pointer to integral type", fndecl);
5930 return false;
5931 }
5932 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == ENUMERAL_TYPE)
5933 {
5934 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5935 "has pointer to enumerated type", fndecl);
5936 return false;
5937 }
5938 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == BOOLEAN_TYPE)
5939 {
5940 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
5941 "has pointer to boolean type", fndecl);
1304953e
JJ
5942 return false;
5943 }
5944 return true;
5945 }
5946 return false;
5947
44a845ca
MS
5948 case BUILT_IN_ADD_OVERFLOW_P:
5949 case BUILT_IN_SUB_OVERFLOW_P:
5950 case BUILT_IN_MUL_OVERFLOW_P:
5951 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
5952 {
5953 unsigned i;
5954 for (i = 0; i < 3; i++)
5955 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
5956 {
5957 error_at (ARG_LOCATION (i), "argument %u in call to function "
5958 "%qE does not have integral type", i + 1, fndecl);
5959 return false;
5960 }
a86451b9
JJ
5961 if (TREE_CODE (TREE_TYPE (args[2])) == ENUMERAL_TYPE)
5962 {
5963 error_at (ARG_LOCATION (2), "argument 3 in call to function "
5964 "%qE has enumerated type", fndecl);
5965 return false;
5966 }
5967 else if (TREE_CODE (TREE_TYPE (args[2])) == BOOLEAN_TYPE)
5968 {
5969 error_at (ARG_LOCATION (2), "argument 3 in call to function "
5970 "%qE has boolean type", fndecl);
5971 return false;
5972 }
44a845ca
MS
5973 return true;
5974 }
5975 return false;
5976
83322951
RG
5977 default:
5978 return true;
5979 }
5980}
5981
0a3ee0fd
GDR
5982/* Subroutine of c_parse_error.
5983 Return the result of concatenating LHS and RHS. RHS is really
5984 a string literal, its first character is indicated by RHS_START and
3292fb42 5985 RHS_SIZE is its length (including the terminating NUL character).
0a3ee0fd
GDR
5986
5987 The caller is responsible for deleting the returned pointer. */
5988
5989static char *
5990catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
5991{
025d57f0 5992 const size_t lhs_size = strlen (lhs);
0a3ee0fd 5993 char *result = XNEWVEC (char, lhs_size + rhs_size);
025d57f0
MS
5994 memcpy (result, lhs, lhs_size);
5995 memcpy (result + lhs_size, rhs_start, rhs_size);
0a3ee0fd
GDR
5996 return result;
5997}
5998
32129a17
DM
5999/* Issue the error given by GMSGID at RICHLOC, indicating that it occurred
6000 before TOKEN, which had the associated VALUE. */
4bb8ca28
MM
6001
6002void
b8698a0f 6003c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
32129a17
DM
6004 tree value, unsigned char token_flags,
6005 rich_location *richloc)
4bb8ca28 6006{
0a3ee0fd
GDR
6007#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
6008
6009 char *message = NULL;
4bb8ca28 6010
cfc93532 6011 if (token_type == CPP_EOF)
4b794eaf 6012 message = catenate_messages (gmsgid, " at end of input");
b8698a0f
L
6013 else if (token_type == CPP_CHAR
6014 || token_type == CPP_WCHAR
cfc93532 6015 || token_type == CPP_CHAR16
fe95b036
ESR
6016 || token_type == CPP_CHAR32
6017 || token_type == CPP_UTF8CHAR)
4bb8ca28
MM
6018 {
6019 unsigned int val = TREE_INT_CST_LOW (value);
b6baa67d
KVH
6020 const char *prefix;
6021
cfc93532 6022 switch (token_type)
b6baa67d
KVH
6023 {
6024 default:
6025 prefix = "";
6026 break;
6027 case CPP_WCHAR:
6028 prefix = "L";
6029 break;
6030 case CPP_CHAR16:
6031 prefix = "u";
6032 break;
6033 case CPP_CHAR32:
6034 prefix = "U";
6035 break;
fe95b036
ESR
6036 case CPP_UTF8CHAR:
6037 prefix = "u8";
6038 break;
b6baa67d
KVH
6039 }
6040
4bb8ca28 6041 if (val <= UCHAR_MAX && ISGRAPH (val))
c22cacf3 6042 message = catenate_messages (gmsgid, " before %s'%c'");
4bb8ca28 6043 else
c22cacf3 6044 message = catenate_messages (gmsgid, " before %s'\\x%x'");
0a3ee0fd 6045
64a5912c 6046 error_at (richloc, message, prefix, val);
0a3ee0fd
GDR
6047 free (message);
6048 message = NULL;
4bb8ca28 6049 }
65e5a578
ESR
6050 else if (token_type == CPP_CHAR_USERDEF
6051 || token_type == CPP_WCHAR_USERDEF
6052 || token_type == CPP_CHAR16_USERDEF
fe95b036
ESR
6053 || token_type == CPP_CHAR32_USERDEF
6054 || token_type == CPP_UTF8CHAR_USERDEF)
65e5a578
ESR
6055 message = catenate_messages (gmsgid,
6056 " before user-defined character literal");
6057 else if (token_type == CPP_STRING_USERDEF
6058 || token_type == CPP_WSTRING_USERDEF
6059 || token_type == CPP_STRING16_USERDEF
6060 || token_type == CPP_STRING32_USERDEF
6061 || token_type == CPP_UTF8STRING_USERDEF)
6062 message = catenate_messages (gmsgid, " before user-defined string literal");
b8698a0f
L
6063 else if (token_type == CPP_STRING
6064 || token_type == CPP_WSTRING
cfc93532 6065 || token_type == CPP_STRING16
2c6e3f55
JJ
6066 || token_type == CPP_STRING32
6067 || token_type == CPP_UTF8STRING)
4b794eaf 6068 message = catenate_messages (gmsgid, " before string constant");
cfc93532 6069 else if (token_type == CPP_NUMBER)
4b794eaf 6070 message = catenate_messages (gmsgid, " before numeric constant");
cfc93532 6071 else if (token_type == CPP_NAME)
0a3ee0fd 6072 {
4b794eaf 6073 message = catenate_messages (gmsgid, " before %qE");
64a5912c 6074 error_at (richloc, message, value);
0a3ee0fd
GDR
6075 free (message);
6076 message = NULL;
6077 }
cfc93532 6078 else if (token_type == CPP_PRAGMA)
bc4071dd 6079 message = catenate_messages (gmsgid, " before %<#pragma%>");
cfc93532 6080 else if (token_type == CPP_PRAGMA_EOL)
bc4071dd 6081 message = catenate_messages (gmsgid, " before end of line");
34429675
JM
6082 else if (token_type == CPP_DECLTYPE)
6083 message = catenate_messages (gmsgid, " before %<decltype%>");
cfc93532 6084 else if (token_type < N_TTYPES)
0a3ee0fd 6085 {
4b794eaf 6086 message = catenate_messages (gmsgid, " before %qs token");
64a5912c 6087 error_at (richloc, message, cpp_type2name (token_type, token_flags));
0a3ee0fd
GDR
6088 free (message);
6089 message = NULL;
6090 }
4bb8ca28 6091 else
64a5912c 6092 error_at (richloc, gmsgid);
0a3ee0fd
GDR
6093
6094 if (message)
6095 {
64a5912c 6096 error_at (richloc, message);
0a3ee0fd
GDR
6097 free (message);
6098 }
c22cacf3 6099#undef catenate_messages
4bb8ca28
MM
6100}
6101
87cf0651
SB
6102/* Return the gcc option code associated with the reason for a cpp
6103 message, or 0 if none. */
6104
6105static int
c24300ba 6106c_option_controlling_cpp_diagnostic (enum cpp_warning_reason reason)
87cf0651 6107{
b559c810 6108 const struct cpp_reason_option_codes_t *entry;
87cf0651 6109
b559c810 6110 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
87cf0651
SB
6111 {
6112 if (entry->reason == reason)
6113 return entry->option_code;
6114 }
6115 return 0;
6116}
6117
c24300ba 6118/* Callback from cpp_diagnostic for PFILE to print diagnostics from the
87cf0651
SB
6119 preprocessor. The diagnostic is of type LEVEL, with REASON set
6120 to the reason code if LEVEL is represents a warning, at location
8a645150
DM
6121 RICHLOC unless this is after lexing and the compiler's location
6122 should be used instead; MSG is the translated message and AP
148e4216
JM
6123 the arguments. Returns true if a diagnostic was emitted, false
6124 otherwise. */
6125
6126bool
c24300ba
DM
6127c_cpp_diagnostic (cpp_reader *pfile ATTRIBUTE_UNUSED,
6128 enum cpp_diagnostic_level level,
6129 enum cpp_warning_reason reason,
6130 rich_location *richloc,
6131 const char *msg, va_list *ap)
148e4216
JM
6132{
6133 diagnostic_info diagnostic;
6134 diagnostic_t dlevel;
e3339d0f 6135 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
148e4216
JM
6136 bool ret;
6137
6138 switch (level)
6139 {
6140 case CPP_DL_WARNING_SYSHDR:
6141 if (flag_no_output)
6142 return false;
e3339d0f 6143 global_dc->dc_warn_system_headers = 1;
148e4216
JM
6144 /* Fall through. */
6145 case CPP_DL_WARNING:
6146 if (flag_no_output)
6147 return false;
6148 dlevel = DK_WARNING;
6149 break;
6150 case CPP_DL_PEDWARN:
6151 if (flag_no_output && !flag_pedantic_errors)
6152 return false;
6153 dlevel = DK_PEDWARN;
6154 break;
6155 case CPP_DL_ERROR:
6156 dlevel = DK_ERROR;
6157 break;
6158 case CPP_DL_ICE:
6159 dlevel = DK_ICE;
6160 break;
6161 case CPP_DL_NOTE:
6162 dlevel = DK_NOTE;
6163 break;
47580d22
JM
6164 case CPP_DL_FATAL:
6165 dlevel = DK_FATAL;
6166 break;
148e4216
JM
6167 default:
6168 gcc_unreachable ();
6169 }
6170 if (done_lexing)
85204e23 6171 richloc->set_range (0, input_location, SHOW_RANGE_WITH_CARET);
148e4216 6172 diagnostic_set_info_translated (&diagnostic, msg, ap,
8a645150 6173 richloc, dlevel);
c24300ba
DM
6174 diagnostic_override_option_index
6175 (&diagnostic,
6176 c_option_controlling_cpp_diagnostic (reason));
56d35585 6177 ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
148e4216 6178 if (level == CPP_DL_WARNING_SYSHDR)
e3339d0f 6179 global_dc->dc_warn_system_headers = save_warn_system_headers;
148e4216
JM
6180 return ret;
6181}
6182
c5ff069d
ZW
6183/* Convert a character from the host to the target execution character
6184 set. cpplib handles this, mostly. */
6185
6186HOST_WIDE_INT
6187c_common_to_target_charset (HOST_WIDE_INT c)
6188{
6189 /* Character constants in GCC proper are sign-extended under -fsigned-char,
6190 zero-extended under -fno-signed-char. cpplib insists that characters
6191 and character constants are always unsigned. Hence we must convert
6192 back and forth. */
6193 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
6194
6195 uc = cpp_host_to_exec_charset (parse_in, uc);
6196
6197 if (flag_signed_char)
6198 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
6199 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
6200 else
6201 return uc;
6202}
6203
cf9e9959
EB
6204/* Fold an offsetof-like expression. EXPR is a nested sequence of component
6205 references with an INDIRECT_REF of a constant at the bottom; much like the
79e7b1fe
JJ
6206 traditional rendering of offsetof as a macro. TYPE is the desired type of
6207 the whole expression. Return the folded result. */
ee8a6a3e 6208
cf9e9959 6209tree
79e7b1fe 6210fold_offsetof (tree expr, tree type, enum tree_code ctx)
ee8a6a3e 6211{
ee8a6a3e 6212 tree base, off, t;
c85158de
MS
6213 tree_code code = TREE_CODE (expr);
6214 switch (code)
ee8a6a3e
RH
6215 {
6216 case ERROR_MARK:
6217 return expr;
6218
545b7d8c
VR
6219 case VAR_DECL:
6220 error ("cannot apply %<offsetof%> to static data member %qD", expr);
6221 return error_mark_node;
6222
6d4d7b0e 6223 case CALL_EXPR:
8d5f60ac 6224 case TARGET_EXPR:
6d4d7b0e
PB
6225 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
6226 return error_mark_node;
6227
6d4d7b0e
PB
6228 case NOP_EXPR:
6229 case INDIRECT_REF:
cf9e9959 6230 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
61c3c490
DS
6231 {
6232 error ("cannot apply %<offsetof%> to a non constant address");
6233 return error_mark_node;
6234 }
79e7b1fe 6235 return convert (type, TREE_OPERAND (expr, 0));
6d4d7b0e 6236
ee8a6a3e 6237 case COMPONENT_REF:
79e7b1fe 6238 base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
ee8a6a3e
RH
6239 if (base == error_mark_node)
6240 return base;
6241
6242 t = TREE_OPERAND (expr, 1);
6243 if (DECL_C_BIT_FIELD (t))
6244 {
6245 error ("attempt to take address of bit-field structure "
c51a1ba9 6246 "member %qD", t);
ee8a6a3e
RH
6247 return error_mark_node;
6248 }
db3927fb 6249 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
386b1f1f 6250 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
db3927fb 6251 / BITS_PER_UNIT));
ee8a6a3e
RH
6252 break;
6253
6254 case ARRAY_REF:
79e7b1fe 6255 base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
ee8a6a3e
RH
6256 if (base == error_mark_node)
6257 return base;
6258
6259 t = TREE_OPERAND (expr, 1);
61c3c490
DS
6260
6261 /* Check if the offset goes beyond the upper bound of the array. */
cf9e9959 6262 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
d32599a6
JJ
6263 {
6264 tree upbound = array_ref_up_bound (expr);
6265 if (upbound != NULL_TREE
6266 && TREE_CODE (upbound) == INTEGER_CST
6267 && !tree_int_cst_equal (upbound,
6268 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
6269 {
c85158de
MS
6270 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
6271 upbound = size_binop (PLUS_EXPR, upbound,
6272 build_int_cst (TREE_TYPE (upbound), 1));
d32599a6
JJ
6273 if (tree_int_cst_lt (upbound, t))
6274 {
6275 tree v;
6276
6277 for (v = TREE_OPERAND (expr, 0);
6278 TREE_CODE (v) == COMPONENT_REF;
6279 v = TREE_OPERAND (v, 0))
6280 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
6281 == RECORD_TYPE)
6282 {
910ad8de
NF
6283 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
6284 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
d32599a6
JJ
6285 if (TREE_CODE (fld_chain) == FIELD_DECL)
6286 break;
6287
6288 if (fld_chain)
6289 break;
6290 }
6291 /* Don't warn if the array might be considered a poor
6292 man's flexible array member with a very permissive
6293 definition thereof. */
6294 if (TREE_CODE (v) == ARRAY_REF
6295 || TREE_CODE (v) == COMPONENT_REF)
6296 warning (OPT_Warray_bounds,
6297 "index %E denotes an offset "
6298 "greater than size of %qT",
6299 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
6300 }
6301 }
6302 }
cf9e9959
EB
6303
6304 t = convert (sizetype, t);
6305 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
ee8a6a3e
RH
6306 break;
6307
1916c916
VR
6308 case COMPOUND_EXPR:
6309 /* Handle static members of volatile structs. */
6310 t = TREE_OPERAND (expr, 1);
a7dea617 6311 gcc_checking_assert (VAR_P (get_base_address (t)));
79e7b1fe 6312 return fold_offsetof (t, type);
1916c916 6313
ee8a6a3e 6314 default:
366de0ce 6315 gcc_unreachable ();
ee8a6a3e
RH
6316 }
6317
79e7b1fe
JJ
6318 if (!POINTER_TYPE_P (type))
6319 return size_binop (PLUS_EXPR, base, convert (type, off));
cf9e9959 6320 return fold_build_pointer_plus (base, off);
ee8a6a3e 6321}
aab038d5
RH
6322\f
6323/* *PTYPE is an incomplete array. Complete it with a domain based on
6324 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
6325 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6326 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
6327
6328int
6329complete_array_type (tree *ptype, tree initial_value, bool do_default)
6330{
6331 tree maxindex, type, main_type, elt, unqual_elt;
6332 int failure = 0, quals;
40d3d530 6333 bool overflow_p = false;
aab038d5
RH
6334
6335 maxindex = size_zero_node;
6336 if (initial_value)
6337 {
6338 if (TREE_CODE (initial_value) == STRING_CST)
6339 {
6340 int eltsize
6341 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
6342 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
6343 }
6344 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
6345 {
9771b263 6346 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
aab038d5 6347
9771b263 6348 if (vec_safe_is_empty (v))
aab038d5
RH
6349 {
6350 if (pedantic)
6351 failure = 3;
830c740f 6352 maxindex = ssize_int (-1);
aab038d5
RH
6353 }
6354 else
6355 {
6356 tree curindex;
4038c495
GB
6357 unsigned HOST_WIDE_INT cnt;
6358 constructor_elt *ce;
cff7525f 6359 bool fold_p = false;
aab038d5 6360
9771b263 6361 if ((*v)[0].index)
40d3d530
JR
6362 maxindex = (*v)[0].index, fold_p = true;
6363
aab038d5
RH
6364 curindex = maxindex;
6365
9771b263 6366 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
aab038d5 6367 {
cff7525f 6368 bool curfold_p = false;
4038c495 6369 if (ce->index)
cff7525f 6370 curindex = ce->index, curfold_p = true;
aab038d5 6371 else
cff7525f
JH
6372 {
6373 if (fold_p)
40d3d530
JR
6374 {
6375 /* Since we treat size types now as ordinary
6376 unsigned types, we need an explicit overflow
6377 check. */
6378 tree orig = curindex;
6379 curindex = fold_convert (sizetype, curindex);
6380 overflow_p |= tree_int_cst_lt (curindex, orig);
6381 }
db3927fb
AH
6382 curindex = size_binop (PLUS_EXPR, curindex,
6383 size_one_node);
cff7525f 6384 }
aab038d5 6385 if (tree_int_cst_lt (maxindex, curindex))
cff7525f 6386 maxindex = curindex, fold_p = curfold_p;
aab038d5 6387 }
40d3d530
JR
6388 if (fold_p)
6389 {
6390 tree orig = maxindex;
6391 maxindex = fold_convert (sizetype, maxindex);
6392 overflow_p |= tree_int_cst_lt (maxindex, orig);
6393 }
aab038d5
RH
6394 }
6395 }
6396 else
6397 {
6398 /* Make an error message unless that happened already. */
6399 if (initial_value != error_mark_node)
6400 failure = 1;
6401 }
6402 }
6403 else
6404 {
6405 failure = 2;
6406 if (!do_default)
6407 return failure;
6408 }
6409
6410 type = *ptype;
6411 elt = TREE_TYPE (type);
6412 quals = TYPE_QUALS (strip_array_types (elt));
6413 if (quals == 0)
6414 unqual_elt = elt;
6415 else
36c5e70a 6416 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
aab038d5
RH
6417
6418 /* Using build_distinct_type_copy and modifying things afterward instead
6419 of using build_array_type to create a new type preserves all of the
6420 TYPE_LANG_FLAG_? bits that the front end may have set. */
6421 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
6422 TREE_TYPE (main_type) = unqual_elt;
e6313a78
RG
6423 TYPE_DOMAIN (main_type)
6424 = build_range_type (TREE_TYPE (maxindex),
6425 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
8a59d466 6426 TYPE_TYPELESS_STORAGE (main_type) = TYPE_TYPELESS_STORAGE (type);
aab038d5
RH
6427 layout_type (main_type);
6428
06d40de8 6429 /* Make sure we have the canonical MAIN_TYPE. */
6fe63fb4
NS
6430 hashval_t hashcode = type_hash_canon_hash (main_type);
6431 main_type = type_hash_canon (hashcode, main_type);
06d40de8 6432
9ae165a0
DG
6433 /* Fix the canonical type. */
6434 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
6435 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
6436 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
6437 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
6438 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
6439 != TYPE_DOMAIN (main_type)))
b8698a0f 6440 TYPE_CANONICAL (main_type)
9ae165a0 6441 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
8a59d466
JJ
6442 TYPE_CANONICAL (TYPE_DOMAIN (main_type)),
6443 TYPE_TYPELESS_STORAGE (main_type));
9ae165a0
DG
6444 else
6445 TYPE_CANONICAL (main_type) = main_type;
6446
aab038d5
RH
6447 if (quals == 0)
6448 type = main_type;
6449 else
6450 type = c_build_qualified_type (main_type, quals);
6451
7bfcb402
JM
6452 if (COMPLETE_TYPE_P (type)
6453 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
40d3d530 6454 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
7bfcb402
JM
6455 {
6456 error ("size of array is too large");
6457 /* If we proceed with the array type as it is, we'll eventually
386b1f1f 6458 crash in tree_to_[su]hwi(). */
7bfcb402
JM
6459 type = error_mark_node;
6460 }
6461
aab038d5
RH
6462 *ptype = type;
6463 return failure;
6464}
5ae9ba3e 6465
6d900107
BE
6466/* INIT is an constructor of a structure with a flexible array member.
6467 Complete the flexible array member with a domain based on it's value. */
6468void
6469complete_flexible_array_elts (tree init)
6470{
6471 tree elt, type;
6472
6473 if (init == NULL_TREE || TREE_CODE (init) != CONSTRUCTOR)
6474 return;
6475
6476 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
6477 return;
6478
6479 elt = CONSTRUCTOR_ELTS (init)->last ().value;
6480 type = TREE_TYPE (elt);
6481 if (TREE_CODE (type) == ARRAY_TYPE
6482 && TYPE_SIZE (type) == NULL_TREE)
6483 complete_array_type (&TREE_TYPE (elt), elt, false);
6484 else
6485 complete_flexible_array_elts (elt);
6486}
6487
30cd1c5d
AS
6488/* Like c_mark_addressable but don't check register qualifier. */
6489void
6490c_common_mark_addressable_vec (tree t)
6491{
eb0e7c34
RB
6492 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
6493 t = C_MAYBE_CONST_EXPR_EXPR (t);
30cd1c5d
AS
6494 while (handled_component_p (t))
6495 t = TREE_OPERAND (t, 0);
b772a565
RB
6496 if (!VAR_P (t)
6497 && TREE_CODE (t) != PARM_DECL
6498 && TREE_CODE (t) != COMPOUND_LITERAL_EXPR)
30cd1c5d 6499 return;
a9e4a1a5
JJ
6500 if (!VAR_P (t) || !DECL_HARD_REGISTER (t))
6501 TREE_ADDRESSABLE (t) = 1;
30cd1c5d
AS
6502}
6503
6504
48ae6c13
RH
6505\f
6506/* Used to help initialize the builtin-types.def table. When a type of
6507 the correct size doesn't exist, use error_mark_node instead of NULL.
6508 The later results in segfaults even when a decl using the type doesn't
6509 get invoked. */
6510
6511tree
6512builtin_type_for_size (int size, bool unsignedp)
6513{
21fa2faf 6514 tree type = c_common_type_for_size (size, unsignedp);
48ae6c13
RH
6515 return type ? type : error_mark_node;
6516}
6517
425fc685
RE
6518/* Work out the size of the first argument of a call to
6519 __builtin_speculation_safe_value. Only pointers and integral types
6520 are permitted. Return -1 if the argument type is not supported or
6521 the size is too large; 0 if the argument type is a pointer or the
6522 size if it is integral. */
6523static enum built_in_function
6524speculation_safe_value_resolve_call (tree function, vec<tree, va_gc> *params)
6525{
6526 /* Type of the argument. */
6527 tree type;
6528 int size;
6529
6530 if (vec_safe_is_empty (params))
6531 {
6532 error ("too few arguments to function %qE", function);
6533 return BUILT_IN_NONE;
6534 }
6535
6536 type = TREE_TYPE ((*params)[0]);
6537 if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
6538 {
6539 /* Force array-to-pointer decay for C++. */
6540 (*params)[0] = default_conversion ((*params)[0]);
6541 type = TREE_TYPE ((*params)[0]);
6542 }
6543
6544 if (POINTER_TYPE_P (type))
6545 return BUILT_IN_SPECULATION_SAFE_VALUE_PTR;
6546
6547 if (!INTEGRAL_TYPE_P (type))
6548 goto incompatible;
6549
6550 if (!COMPLETE_TYPE_P (type))
6551 goto incompatible;
6552
6553 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
6554 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
6555 return ((enum built_in_function)
6556 ((int) BUILT_IN_SPECULATION_SAFE_VALUE_1 + exact_log2 (size)));
6557
6558 incompatible:
6559 /* Issue the diagnostic only if the argument is valid, otherwise
6560 it would be redundant at best and could be misleading. */
6561 if (type != error_mark_node)
6562 error ("operand type %qT is incompatible with argument %d of %qE",
6563 type, 1, function);
6564
6565 return BUILT_IN_NONE;
6566}
6567
6568/* Validate and coerce PARAMS, the arguments to ORIG_FUNCTION to fit
6569 the prototype for FUNCTION. The first argument is mandatory, a second
6570 argument, if present, must be type compatible with the first. */
6571static bool
6572speculation_safe_value_resolve_params (location_t loc, tree orig_function,
6573 vec<tree, va_gc> *params)
6574{
6575 tree val;
6576
6577 if (params->length () == 0)
6578 {
6579 error_at (loc, "too few arguments to function %qE", orig_function);
6580 return false;
6581 }
6582
6583 else if (params->length () > 2)
6584 {
6585 error_at (loc, "too many arguments to function %qE", orig_function);
6586 return false;
6587 }
6588
6589 val = (*params)[0];
6590 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE)
6591 val = default_conversion (val);
6592 if (!(TREE_CODE (TREE_TYPE (val)) == POINTER_TYPE
6593 || TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE))
6594 {
6595 error_at (loc,
6596 "expecting argument of type pointer or of type integer "
6597 "for argument 1");
6598 return false;
6599 }
6600 (*params)[0] = val;
6601
6602 if (params->length () == 2)
6603 {
6604 tree val2 = (*params)[1];
6605 if (TREE_CODE (TREE_TYPE (val2)) == ARRAY_TYPE)
6606 val2 = default_conversion (val2);
6607 if (!(TREE_TYPE (val) == TREE_TYPE (val2)
6608 || useless_type_conversion_p (TREE_TYPE (val), TREE_TYPE (val2))))
6609 {
6610 error_at (loc, "both arguments must be compatible");
6611 return false;
6612 }
6613 (*params)[1] = val2;
6614 }
6615
6616 return true;
6617}
6618
6619/* Cast the result of the builtin back to the type of the first argument,
6620 preserving any qualifiers that it might have. */
6621static tree
6622speculation_safe_value_resolve_return (tree first_param, tree result)
6623{
6624 tree ptype = TREE_TYPE (first_param);
6625 tree rtype = TREE_TYPE (result);
6626 ptype = TYPE_MAIN_VARIANT (ptype);
6627
6628 if (tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
6629 return convert (ptype, result);
6630
6631 return result;
6632}
6633
48ae6c13
RH
6634/* A helper function for resolve_overloaded_builtin in resolving the
6635 overloaded __sync_ builtins. Returns a positive power of 2 if the
6636 first operand of PARAMS is a pointer to a supported data type.
7a127fa7
MS
6637 Returns 0 if an error is encountered.
6638 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
6639 built-ins. */
48ae6c13
RH
6640
6641static int
7a127fa7 6642sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch)
48ae6c13 6643{
7a127fa7
MS
6644 /* Type of the argument. */
6645 tree argtype;
6646 /* Type the argument points to. */
48ae6c13
RH
6647 tree type;
6648 int size;
6649
ba6b3795 6650 if (vec_safe_is_empty (params))
48ae6c13
RH
6651 {
6652 error ("too few arguments to function %qE", function);
6653 return 0;
6654 }
6655
7a127fa7 6656 argtype = type = TREE_TYPE ((*params)[0]);
ec33aebb 6657 if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
6415bd5d
JM
6658 {
6659 /* Force array-to-pointer decay for C++. */
6415bd5d
JM
6660 (*params)[0] = default_conversion ((*params)[0]);
6661 type = TREE_TYPE ((*params)[0]);
6662 }
48ae6c13
RH
6663 if (TREE_CODE (type) != POINTER_TYPE)
6664 goto incompatible;
6665
6666 type = TREE_TYPE (type);
6667 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
6668 goto incompatible;
6669
efbf55b0
MP
6670 if (!COMPLETE_TYPE_P (type))
6671 goto incompatible;
6672
7a127fa7
MS
6673 if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
6674 goto incompatible;
6675
ae7e9ddd 6676 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
a0274e3e 6677 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
48ae6c13
RH
6678 return size;
6679
6680 incompatible:
9f04a53e
MS
6681 /* Issue the diagnostic only if the argument is valid, otherwise
6682 it would be redundant at best and could be misleading. */
6683 if (argtype != error_mark_node)
6684 error ("operand type %qT is incompatible with argument %d of %qE",
6685 argtype, 1, function);
48ae6c13
RH
6686 return 0;
6687}
6688
c22cacf3 6689/* A helper function for resolve_overloaded_builtin. Adds casts to
48ae6c13
RH
6690 PARAMS to make arguments match up with those of FUNCTION. Drops
6691 the variadic arguments at the end. Returns false if some error
6692 was encountered; true on success. */
6693
6694static bool
86951993 6695sync_resolve_params (location_t loc, tree orig_function, tree function,
9771b263 6696 vec<tree, va_gc> *params, bool orig_format)
48ae6c13 6697{
e19a18d4 6698 function_args_iterator iter;
48ae6c13 6699 tree ptype;
bbbbb16a 6700 unsigned int parmnum;
48ae6c13 6701
e19a18d4 6702 function_args_iter_init (&iter, TREE_TYPE (function));
48ae6c13
RH
6703 /* We've declared the implementation functions to use "volatile void *"
6704 as the pointer parameter, so we shouldn't get any complaints from the
6705 call to check_function_arguments what ever type the user used. */
e19a18d4 6706 function_args_iter_next (&iter);
9771b263 6707 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
267bac10 6708 ptype = TYPE_MAIN_VARIANT (ptype);
48ae6c13
RH
6709
6710 /* For the rest of the values, we need to cast these to FTYPE, so that we
6711 don't get warnings for passing pointer types, etc. */
bbbbb16a 6712 parmnum = 0;
e19a18d4 6713 while (1)
48ae6c13 6714 {
e19a18d4
NF
6715 tree val, arg_type;
6716
6717 arg_type = function_args_iter_cond (&iter);
6718 /* XXX void_type_node belies the abstraction. */
6719 if (arg_type == void_type_node)
6720 break;
48ae6c13 6721
bbbbb16a 6722 ++parmnum;
9771b263 6723 if (params->length () <= parmnum)
48ae6c13 6724 {
86951993 6725 error_at (loc, "too few arguments to function %qE", orig_function);
48ae6c13
RH
6726 return false;
6727 }
6728
e3793c6f
JJ
6729 /* Only convert parameters if arg_type is unsigned integer type with
6730 new format sync routines, i.e. don't attempt to convert pointer
6731 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
6732 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
6733 kinds). */
6734 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
86951993
AM
6735 {
6736 /* Ideally for the first conversion we'd use convert_for_assignment
6737 so that we get warnings for anything that doesn't match the pointer
6738 type. This isn't portable across the C and C++ front ends atm. */
9771b263 6739 val = (*params)[parmnum];
86951993
AM
6740 val = convert (ptype, val);
6741 val = convert (arg_type, val);
9771b263 6742 (*params)[parmnum] = val;
86951993 6743 }
48ae6c13 6744
e19a18d4 6745 function_args_iter_next (&iter);
48ae6c13
RH
6746 }
6747
86951993 6748 /* __atomic routines are not variadic. */
9771b263 6749 if (!orig_format && params->length () != parmnum + 1)
86951993
AM
6750 {
6751 error_at (loc, "too many arguments to function %qE", orig_function);
6752 return false;
6753 }
6754
48ae6c13
RH
6755 /* The definition of these primitives is variadic, with the remaining
6756 being "an optional list of variables protected by the memory barrier".
6757 No clue what that's supposed to mean, precisely, but we consider all
6758 call-clobbered variables to be protected so we're safe. */
9771b263 6759 params->truncate (parmnum + 1);
48ae6c13
RH
6760
6761 return true;
6762}
6763
c22cacf3 6764/* A helper function for resolve_overloaded_builtin. Adds a cast to
48ae6c13
RH
6765 RESULT to make it match the type of the first pointer argument in
6766 PARAMS. */
6767
6768static tree
86951993 6769sync_resolve_return (tree first_param, tree result, bool orig_format)
48ae6c13 6770{
bbbbb16a 6771 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
86951993 6772 tree rtype = TREE_TYPE (result);
99db1ef0 6773 ptype = TYPE_MAIN_VARIANT (ptype);
86951993
AM
6774
6775 /* New format doesn't require casting unless the types are the same size. */
6776 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
6777 return convert (ptype, result);
6778 else
6779 return result;
6780}
6781
6782/* This function verifies the PARAMS to generic atomic FUNCTION.
6783 It returns the size if all the parameters are the same size, otherwise
6784 0 is returned if the parameters are invalid. */
6785
6786static int
9771b263
DN
6787get_atomic_generic_size (location_t loc, tree function,
6788 vec<tree, va_gc> *params)
86951993
AM
6789{
6790 unsigned int n_param;
6791 unsigned int n_model;
6792 unsigned int x;
6793 int size_0;
6794 tree type_0;
6795
6796 /* Determine the parameter makeup. */
6797 switch (DECL_FUNCTION_CODE (function))
6798 {
6799 case BUILT_IN_ATOMIC_EXCHANGE:
6800 n_param = 4;
6801 n_model = 1;
6802 break;
6803 case BUILT_IN_ATOMIC_LOAD:
6804 case BUILT_IN_ATOMIC_STORE:
6805 n_param = 3;
6806 n_model = 1;
6807 break;
6808 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
6809 n_param = 6;
6810 n_model = 2;
6811 break;
6812 default:
c466c4ff 6813 gcc_unreachable ();
86951993
AM
6814 }
6815
9771b263 6816 if (vec_safe_length (params) != n_param)
86951993
AM
6817 {
6818 error_at (loc, "incorrect number of arguments to function %qE", function);
6819 return 0;
6820 }
6821
6822 /* Get type of first parameter, and determine its size. */
9771b263 6823 type_0 = TREE_TYPE ((*params)[0]);
ec33aebb 6824 if (TREE_CODE (type_0) == ARRAY_TYPE && c_dialect_cxx ())
6415bd5d
JM
6825 {
6826 /* Force array-to-pointer decay for C++. */
6415bd5d
JM
6827 (*params)[0] = default_conversion ((*params)[0]);
6828 type_0 = TREE_TYPE ((*params)[0]);
6829 }
c466c4ff
AM
6830 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
6831 {
6832 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
6833 function);
6834 return 0;
6835 }
6836
6837 /* Types must be compile time constant sizes. */
6838 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
86951993 6839 {
c466c4ff
AM
6840 error_at (loc,
6841 "argument 1 of %qE must be a pointer to a constant size type",
6842 function);
86951993
AM
6843 return 0;
6844 }
c466c4ff 6845
ae7e9ddd 6846 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
86951993 6847
c466c4ff
AM
6848 /* Zero size objects are not allowed. */
6849 if (size_0 == 0)
6850 {
6851 error_at (loc,
6852 "argument 1 of %qE must be a pointer to a nonzero size object",
6853 function);
6854 return 0;
6855 }
6856
86951993
AM
6857 /* Check each other parameter is a pointer and the same size. */
6858 for (x = 0; x < n_param - n_model; x++)
6859 {
6860 int size;
9771b263 6861 tree type = TREE_TYPE ((*params)[x]);
688010ba 6862 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
86951993
AM
6863 if (n_param == 6 && x == 3)
6864 continue;
ec33aebb
JJ
6865 if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
6866 {
6867 /* Force array-to-pointer decay for C++. */
6868 (*params)[x] = default_conversion ((*params)[x]);
6869 type = TREE_TYPE ((*params)[x]);
6870 }
86951993
AM
6871 if (!POINTER_TYPE_P (type))
6872 {
6873 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
6874 function);
6875 return 0;
6876 }
a76989dc
MP
6877 else if (TYPE_SIZE_UNIT (TREE_TYPE (type))
6878 && TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type))))
6879 != INTEGER_CST)
6880 {
6881 error_at (loc, "argument %d of %qE must be a pointer to a constant "
6882 "size type", x + 1, function);
6883 return 0;
6884 }
6885 else if (FUNCTION_POINTER_TYPE_P (type))
6886 {
6887 error_at (loc, "argument %d of %qE must not be a pointer to a "
6888 "function", x + 1, function);
6889 return 0;
6890 }
7b56b2f8
MP
6891 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
6892 size = type_size ? tree_to_uhwi (type_size) : 0;
86951993
AM
6893 if (size != size_0)
6894 {
6895 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
6896 function);
6897 return 0;
6898 }
6899 }
6900
6901 /* Check memory model parameters for validity. */
6902 for (x = n_param - n_model ; x < n_param; x++)
6903 {
9771b263 6904 tree p = (*params)[x];
9a004410
DM
6905 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
6906 {
6907 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
6908 function);
6909 return 0;
6910 }
6911 p = fold_for_warn (p);
86951993 6912 if (TREE_CODE (p) == INTEGER_CST)
9a004410 6913 {
d44ed508
JJ
6914 /* memmodel_base masks the low 16 bits, thus ignore any bits above
6915 it by using TREE_INT_CST_LOW instead of tree_to_*hwi. Those high
6916 bits will be checked later during expansion in target specific
6917 way. */
6918 if (memmodel_base (TREE_INT_CST_LOW (p)) >= MEMMODEL_LAST)
6919 warning_at (loc, OPT_Winvalid_memory_model,
6920 "invalid memory model argument %d of %qE", x + 1,
6921 function);
86951993 6922 }
9a004410 6923 }
86951993
AM
6924
6925 return size_0;
6926}
6927
6928
6929/* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
6930 at the beginning of the parameter list PARAMS representing the size of the
6931 objects. This is to match the library ABI requirement. LOC is the location
6932 of the function call.
6933 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
6934 returned to allow the external call to be constructed. */
6935
6936static tree
6937add_atomic_size_parameter (unsigned n, location_t loc, tree function,
9771b263 6938 vec<tree, va_gc> *params)
86951993
AM
6939{
6940 tree size_node;
6941
6942 /* Insert a SIZE_T parameter as the first param. If there isn't
6943 enough space, allocate a new vector and recursively re-build with that. */
9771b263 6944 if (!params->space (1))
86951993
AM
6945 {
6946 unsigned int z, len;
9771b263 6947 vec<tree, va_gc> *v;
86951993
AM
6948 tree f;
6949
9771b263
DN
6950 len = params->length ();
6951 vec_alloc (v, len + 1);
8edbfaa6 6952 v->quick_push (build_int_cst (size_type_node, n));
86951993 6953 for (z = 0; z < len; z++)
9771b263 6954 v->quick_push ((*params)[z]);
81e5eca8 6955 f = build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 6956 vec_free (v);
86951993
AM
6957 return f;
6958 }
6959
6960 /* Add the size parameter and leave as a function call for processing. */
6961 size_node = build_int_cst (size_type_node, n);
9771b263 6962 params->quick_insert (0, size_node);
86951993
AM
6963 return NULL_TREE;
6964}
6965
6966
6b28e197
JM
6967/* Return whether atomic operations for naturally aligned N-byte
6968 arguments are supported, whether inline or through libatomic. */
6969static bool
6970atomic_size_supported_p (int n)
6971{
6972 switch (n)
6973 {
6974 case 1:
6975 case 2:
6976 case 4:
6977 case 8:
6978 return true;
6979
6980 case 16:
6981 return targetm.scalar_mode_supported_p (TImode);
6982
6983 default:
6984 return false;
6985 }
6986}
6987
86951993
AM
6988/* This will process an __atomic_exchange function call, determine whether it
6989 needs to be mapped to the _N variation, or turned into a library call.
6990 LOC is the location of the builtin call.
6991 FUNCTION is the DECL that has been invoked;
6992 PARAMS is the argument list for the call. The return value is non-null
6993 TRUE is returned if it is translated into the proper format for a call to the
6994 external library, and NEW_RETURN is set the tree for that function.
6995 FALSE is returned if processing for the _N variation is required, and
026c3cfd 6996 NEW_RETURN is set to the return value the result is copied into. */
86951993
AM
6997static bool
6998resolve_overloaded_atomic_exchange (location_t loc, tree function,
9771b263 6999 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
7000{
7001 tree p0, p1, p2, p3;
7002 tree I_type, I_type_ptr;
7003 int n = get_atomic_generic_size (loc, function, params);
7004
c466c4ff
AM
7005 /* Size of 0 is an error condition. */
7006 if (n == 0)
7007 {
7008 *new_return = error_mark_node;
7009 return true;
7010 }
7011
86951993 7012 /* If not a lock-free size, change to the library generic format. */
6b28e197 7013 if (!atomic_size_supported_p (n))
86951993
AM
7014 {
7015 *new_return = add_atomic_size_parameter (n, loc, function, params);
7016 return true;
7017 }
7018
7019 /* Otherwise there is a lockfree match, transform the call from:
7020 void fn(T* mem, T* desired, T* return, model)
7021 into
7022 *return = (T) (fn (In* mem, (In) *desired, model)) */
7023
9771b263
DN
7024 p0 = (*params)[0];
7025 p1 = (*params)[1];
7026 p2 = (*params)[2];
7027 p3 = (*params)[3];
86951993
AM
7028
7029 /* Create pointer to appropriate size. */
7030 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7031 I_type_ptr = build_pointer_type (I_type);
7032
7033 /* Convert object pointer to required type. */
7034 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 7035 (*params)[0] = p0;
86951993
AM
7036 /* Convert new value to required type, and dereference it. */
7037 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7038 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9771b263 7039 (*params)[1] = p1;
86951993
AM
7040
7041 /* Move memory model to the 3rd position, and end param list. */
9771b263
DN
7042 (*params)[2] = p3;
7043 params->truncate (3);
86951993
AM
7044
7045 /* Convert return pointer and dereference it for later assignment. */
7046 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
7047
7048 return false;
48ae6c13
RH
7049}
7050
86951993
AM
7051
7052/* This will process an __atomic_compare_exchange function call, determine
7053 whether it needs to be mapped to the _N variation, or turned into a lib call.
7054 LOC is the location of the builtin call.
7055 FUNCTION is the DECL that has been invoked;
7056 PARAMS is the argument list for the call. The return value is non-null
7057 TRUE is returned if it is translated into the proper format for a call to the
7058 external library, and NEW_RETURN is set the tree for that function.
7059 FALSE is returned if processing for the _N variation is required. */
7060
7061static bool
7062resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
9771b263 7063 vec<tree, va_gc> *params,
86951993
AM
7064 tree *new_return)
7065{
7066 tree p0, p1, p2;
7067 tree I_type, I_type_ptr;
7068 int n = get_atomic_generic_size (loc, function, params);
7069
c466c4ff
AM
7070 /* Size of 0 is an error condition. */
7071 if (n == 0)
7072 {
7073 *new_return = error_mark_node;
7074 return true;
7075 }
7076
86951993 7077 /* If not a lock-free size, change to the library generic format. */
6b28e197 7078 if (!atomic_size_supported_p (n))
86951993
AM
7079 {
7080 /* The library generic format does not have the weak parameter, so
7081 remove it from the param list. Since a parameter has been removed,
7082 we can be sure that there is room for the SIZE_T parameter, meaning
7083 there will not be a recursive rebuilding of the parameter list, so
7084 there is no danger this will be done twice. */
7085 if (n > 0)
7086 {
9771b263
DN
7087 (*params)[3] = (*params)[4];
7088 (*params)[4] = (*params)[5];
7089 params->truncate (5);
86951993
AM
7090 }
7091 *new_return = add_atomic_size_parameter (n, loc, function, params);
7092 return true;
7093 }
7094
7095 /* Otherwise, there is a match, so the call needs to be transformed from:
7096 bool fn(T* mem, T* desired, T* return, weak, success, failure)
7097 into
7098 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
7099
9771b263
DN
7100 p0 = (*params)[0];
7101 p1 = (*params)[1];
7102 p2 = (*params)[2];
86951993
AM
7103
7104 /* Create pointer to appropriate size. */
7105 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7106 I_type_ptr = build_pointer_type (I_type);
7107
7108 /* Convert object pointer to required type. */
7109 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 7110 (*params)[0] = p0;
86951993
AM
7111
7112 /* Convert expected pointer to required type. */
7113 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
9771b263 7114 (*params)[1] = p1;
86951993
AM
7115
7116 /* Convert desired value to required type, and dereference it. */
7117 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
7118 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
9771b263 7119 (*params)[2] = p2;
86951993
AM
7120
7121 /* The rest of the parameters are fine. NULL means no special return value
7122 processing.*/
7123 *new_return = NULL;
7124 return false;
7125}
7126
7127
7128/* This will process an __atomic_load function call, determine whether it
7129 needs to be mapped to the _N variation, or turned into a library call.
7130 LOC is the location of the builtin call.
7131 FUNCTION is the DECL that has been invoked;
7132 PARAMS is the argument list for the call. The return value is non-null
7133 TRUE is returned if it is translated into the proper format for a call to the
7134 external library, and NEW_RETURN is set the tree for that function.
7135 FALSE is returned if processing for the _N variation is required, and
026c3cfd 7136 NEW_RETURN is set to the return value the result is copied into. */
86951993
AM
7137
7138static bool
7139resolve_overloaded_atomic_load (location_t loc, tree function,
9771b263 7140 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
7141{
7142 tree p0, p1, p2;
7143 tree I_type, I_type_ptr;
7144 int n = get_atomic_generic_size (loc, function, params);
7145
c466c4ff
AM
7146 /* Size of 0 is an error condition. */
7147 if (n == 0)
7148 {
7149 *new_return = error_mark_node;
7150 return true;
7151 }
7152
86951993 7153 /* If not a lock-free size, change to the library generic format. */
6b28e197 7154 if (!atomic_size_supported_p (n))
86951993
AM
7155 {
7156 *new_return = add_atomic_size_parameter (n, loc, function, params);
7157 return true;
7158 }
7159
7160 /* Otherwise, there is a match, so the call needs to be transformed from:
7161 void fn(T* mem, T* return, model)
7162 into
7163 *return = (T) (fn ((In *) mem, model)) */
7164
9771b263
DN
7165 p0 = (*params)[0];
7166 p1 = (*params)[1];
7167 p2 = (*params)[2];
86951993
AM
7168
7169 /* Create pointer to appropriate size. */
7170 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7171 I_type_ptr = build_pointer_type (I_type);
7172
7173 /* Convert object pointer to required type. */
7174 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 7175 (*params)[0] = p0;
86951993
AM
7176
7177 /* Move memory model to the 2nd position, and end param list. */
9771b263
DN
7178 (*params)[1] = p2;
7179 params->truncate (2);
86951993
AM
7180
7181 /* Convert return pointer and dereference it for later assignment. */
7182 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7183
7184 return false;
7185}
7186
7187
7188/* This will process an __atomic_store function call, determine whether it
7189 needs to be mapped to the _N variation, or turned into a library call.
7190 LOC is the location of the builtin call.
7191 FUNCTION is the DECL that has been invoked;
7192 PARAMS is the argument list for the call. The return value is non-null
7193 TRUE is returned if it is translated into the proper format for a call to the
7194 external library, and NEW_RETURN is set the tree for that function.
7195 FALSE is returned if processing for the _N variation is required, and
026c3cfd 7196 NEW_RETURN is set to the return value the result is copied into. */
86951993
AM
7197
7198static bool
7199resolve_overloaded_atomic_store (location_t loc, tree function,
9771b263 7200 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
7201{
7202 tree p0, p1;
7203 tree I_type, I_type_ptr;
7204 int n = get_atomic_generic_size (loc, function, params);
7205
c466c4ff
AM
7206 /* Size of 0 is an error condition. */
7207 if (n == 0)
7208 {
7209 *new_return = error_mark_node;
7210 return true;
7211 }
7212
86951993 7213 /* If not a lock-free size, change to the library generic format. */
6b28e197 7214 if (!atomic_size_supported_p (n))
86951993
AM
7215 {
7216 *new_return = add_atomic_size_parameter (n, loc, function, params);
7217 return true;
7218 }
7219
7220 /* Otherwise, there is a match, so the call needs to be transformed from:
7221 void fn(T* mem, T* value, model)
7222 into
7223 fn ((In *) mem, (In) *value, model) */
7224
9771b263
DN
7225 p0 = (*params)[0];
7226 p1 = (*params)[1];
86951993
AM
7227
7228 /* Create pointer to appropriate size. */
7229 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
7230 I_type_ptr = build_pointer_type (I_type);
7231
7232 /* Convert object pointer to required type. */
7233 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 7234 (*params)[0] = p0;
86951993
AM
7235
7236 /* Convert new value to required type, and dereference it. */
7237 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
7238 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9771b263 7239 (*params)[1] = p1;
86951993
AM
7240
7241 /* The memory model is in the right spot already. Return is void. */
7242 *new_return = NULL_TREE;
7243
7244 return false;
7245}
7246
7247
48ae6c13
RH
7248/* Some builtin functions are placeholders for other expressions. This
7249 function should be called immediately after parsing the call expression
7250 before surrounding code has committed to the type of the expression.
7251
c2255bc4
AH
7252 LOC is the location of the builtin call.
7253
48ae6c13
RH
7254 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
7255 PARAMS is the argument list for the call. The return value is non-null
7256 when expansion is complete, and null if normal processing should
7257 continue. */
7258
7259tree
9771b263
DN
7260resolve_overloaded_builtin (location_t loc, tree function,
7261 vec<tree, va_gc> *params)
48ae6c13
RH
7262{
7263 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
7a127fa7
MS
7264
7265 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
7266 Those are not valid to call with a pointer to _Bool (or C++ bool)
7267 and so must be rejected. */
7268 bool fetch_op = true;
86951993
AM
7269 bool orig_format = true;
7270 tree new_return = NULL_TREE;
7271
58646b77
PB
7272 switch (DECL_BUILT_IN_CLASS (function))
7273 {
7274 case BUILT_IN_NORMAL:
7275 break;
7276 case BUILT_IN_MD:
7277 if (targetm.resolve_overloaded_builtin)
c2255bc4 7278 return targetm.resolve_overloaded_builtin (loc, function, params);
58646b77 7279 else
c22cacf3 7280 return NULL_TREE;
58646b77
PB
7281 default:
7282 return NULL_TREE;
7283 }
c22cacf3 7284
58646b77 7285 /* Handle BUILT_IN_NORMAL here. */
48ae6c13
RH
7286 switch (orig_code)
7287 {
425fc685
RE
7288 case BUILT_IN_SPECULATION_SAFE_VALUE_N:
7289 {
7290 tree new_function, first_param, result;
7291 enum built_in_function fncode
7292 = speculation_safe_value_resolve_call (function, params);;
7293
7294 first_param = (*params)[0];
7295 if (fncode == BUILT_IN_NONE
7296 || !speculation_safe_value_resolve_params (loc, function, params))
7297 return error_mark_node;
7298
7299 if (targetm.have_speculation_safe_value (true))
7300 {
7301 new_function = builtin_decl_explicit (fncode);
7302 result = build_function_call_vec (loc, vNULL, new_function, params,
7303 NULL);
7304
7305 if (result == error_mark_node)
7306 return result;
7307
7308 return speculation_safe_value_resolve_return (first_param, result);
7309 }
7310 else
7311 {
7312 /* This target doesn't have, or doesn't need, active mitigation
7313 against incorrect speculative execution. Simply return the
7314 first parameter to the builtin. */
7315 if (!targetm.have_speculation_safe_value (false))
7316 /* The user has invoked __builtin_speculation_safe_value
7317 even though __HAVE_SPECULATION_SAFE_VALUE is not
7318 defined: emit a warning. */
7319 warning_at (input_location, 0,
7320 "this target does not define a speculation barrier; "
7321 "your program will still execute correctly, "
7322 "but incorrect speculation may not be be "
7323 "restricted");
7324
7325 /* If the optional second argument is present, handle any side
7326 effects now. */
7327 if (params->length () == 2
7328 && TREE_SIDE_EFFECTS ((*params)[1]))
7329 return build2 (COMPOUND_EXPR, TREE_TYPE (first_param),
7330 (*params)[1], first_param);
7331
7332 return first_param;
7333 }
7334 }
7335
86951993
AM
7336 case BUILT_IN_ATOMIC_EXCHANGE:
7337 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7338 case BUILT_IN_ATOMIC_LOAD:
7339 case BUILT_IN_ATOMIC_STORE:
7340 {
7341 /* Handle these 4 together so that they can fall through to the next
7342 case if the call is transformed to an _N variant. */
7343 switch (orig_code)
295844f6 7344 {
86951993
AM
7345 case BUILT_IN_ATOMIC_EXCHANGE:
7346 {
7347 if (resolve_overloaded_atomic_exchange (loc, function, params,
7348 &new_return))
7349 return new_return;
7350 /* Change to the _N variant. */
7351 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
7352 break;
7353 }
7354
7355 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7356 {
7357 if (resolve_overloaded_atomic_compare_exchange (loc, function,
7358 params,
7359 &new_return))
7360 return new_return;
7361 /* Change to the _N variant. */
7362 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
7363 break;
7364 }
7365 case BUILT_IN_ATOMIC_LOAD:
7366 {
7367 if (resolve_overloaded_atomic_load (loc, function, params,
7368 &new_return))
7369 return new_return;
7370 /* Change to the _N variant. */
7371 orig_code = BUILT_IN_ATOMIC_LOAD_N;
7372 break;
7373 }
7374 case BUILT_IN_ATOMIC_STORE:
7375 {
7376 if (resolve_overloaded_atomic_store (loc, function, params,
7377 &new_return))
7378 return new_return;
7379 /* Change to the _N variant. */
7380 orig_code = BUILT_IN_ATOMIC_STORE_N;
7381 break;
7382 }
7383 default:
7384 gcc_unreachable ();
295844f6 7385 }
86951993 7386 }
295844f6 7387 /* FALLTHRU */
86951993
AM
7388 case BUILT_IN_ATOMIC_EXCHANGE_N:
7389 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
7390 case BUILT_IN_ATOMIC_LOAD_N:
7391 case BUILT_IN_ATOMIC_STORE_N:
295844f6
MP
7392 fetch_op = false;
7393 /* FALLTHRU */
86951993
AM
7394 case BUILT_IN_ATOMIC_ADD_FETCH_N:
7395 case BUILT_IN_ATOMIC_SUB_FETCH_N:
7396 case BUILT_IN_ATOMIC_AND_FETCH_N:
7397 case BUILT_IN_ATOMIC_NAND_FETCH_N:
7398 case BUILT_IN_ATOMIC_XOR_FETCH_N:
7399 case BUILT_IN_ATOMIC_OR_FETCH_N:
7400 case BUILT_IN_ATOMIC_FETCH_ADD_N:
7401 case BUILT_IN_ATOMIC_FETCH_SUB_N:
7402 case BUILT_IN_ATOMIC_FETCH_AND_N:
7403 case BUILT_IN_ATOMIC_FETCH_NAND_N:
7404 case BUILT_IN_ATOMIC_FETCH_XOR_N:
7405 case BUILT_IN_ATOMIC_FETCH_OR_N:
295844f6
MP
7406 orig_format = false;
7407 /* FALLTHRU */
e0a8ecf2
AM
7408 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
7409 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
7410 case BUILT_IN_SYNC_FETCH_AND_OR_N:
7411 case BUILT_IN_SYNC_FETCH_AND_AND_N:
7412 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
7413 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
7414 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
7415 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
7416 case BUILT_IN_SYNC_OR_AND_FETCH_N:
7417 case BUILT_IN_SYNC_AND_AND_FETCH_N:
7418 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
7419 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
7420 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
7421 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
7422 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
7423 case BUILT_IN_SYNC_LOCK_RELEASE_N:
48ae6c13 7424 {
7a127fa7
MS
7425 /* The following are not _FETCH_OPs and must be accepted with
7426 pointers to _Bool (or C++ bool). */
7427 if (fetch_op)
7428 fetch_op =
7429 (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
7430 && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
7431 && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
7432 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
7433
7434 int n = sync_resolve_size (function, params, fetch_op);
bbbbb16a 7435 tree new_function, first_param, result;
e79983f4 7436 enum built_in_function fncode;
48ae6c13
RH
7437
7438 if (n == 0)
7439 return error_mark_node;
7440
e79983f4
MM
7441 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
7442 new_function = builtin_decl_explicit (fncode);
86951993
AM
7443 if (!sync_resolve_params (loc, function, new_function, params,
7444 orig_format))
48ae6c13
RH
7445 return error_mark_node;
7446
9771b263 7447 first_param = (*params)[0];
81e5eca8
MP
7448 result = build_function_call_vec (loc, vNULL, new_function, params,
7449 NULL);
86951993
AM
7450 if (result == error_mark_node)
7451 return result;
e0a8ecf2 7452 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
86951993 7453 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
2e6b45af
MS
7454 && orig_code != BUILT_IN_ATOMIC_STORE_N
7455 && orig_code != BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N)
86951993 7456 result = sync_resolve_return (first_param, result, orig_format);
48ae6c13 7457
14ba7b28
MP
7458 if (fetch_op)
7459 /* Prevent -Wunused-value warning. */
7460 TREE_USED (result) = true;
7461
86951993
AM
7462 /* If new_return is set, assign function to that expr and cast the
7463 result to void since the generic interface returned void. */
7464 if (new_return)
7465 {
7466 /* Cast function result from I{1,2,4,8,16} to the required type. */
7467 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
7468 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
7469 result);
7470 TREE_SIDE_EFFECTS (result) = 1;
7471 protected_set_expr_location (result, loc);
7472 result = convert (void_type_node, result);
7473 }
48ae6c13
RH
7474 return result;
7475 }
7476
7477 default:
58646b77 7478 return NULL_TREE;
48ae6c13
RH
7479 }
7480}
7481
0af94e6f
JR
7482/* vector_types_compatible_elements_p is used in type checks of vectors
7483 values used as operands of binary operators. Where it returns true, and
7484 the other checks of the caller succeed (being vector types in he first
7485 place, and matching number of elements), we can just treat the types
7486 as essentially the same.
7487 Contrast with vector_targets_convertible_p, which is used for vector
7488 pointer types, and vector_types_convertible_p, which will allow
7489 language-specific matches under the control of flag_lax_vector_conversions,
7490 and might still require a conversion. */
7491/* True if vector types T1 and T2 can be inputs to the same binary
7492 operator without conversion.
7493 We don't check the overall vector size here because some of our callers
7494 want to give different error messages when the vectors are compatible
7495 except for the element count. */
7496
5bed876a 7497bool
0af94e6f 7498vector_types_compatible_elements_p (tree t1, tree t2)
5bed876a 7499{
0af94e6f
JR
7500 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
7501 t1 = TREE_TYPE (t1);
7502 t2 = TREE_TYPE (t2);
7503
5bed876a
AH
7504 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
7505
8656dafa
RS
7506 gcc_assert ((INTEGRAL_TYPE_P (t1)
7507 || c1 == REAL_TYPE
7508 || c1 == FIXED_POINT_TYPE)
7509 && (INTEGRAL_TYPE_P (t2)
7510 || c2 == REAL_TYPE
ab22c1fa 7511 || c2 == FIXED_POINT_TYPE));
5bed876a 7512
0af94e6f
JR
7513 t1 = c_common_signed_type (t1);
7514 t2 = c_common_signed_type (t2);
5bed876a
AH
7515 /* Equality works here because c_common_signed_type uses
7516 TYPE_MAIN_VARIANT. */
0af94e6f
JR
7517 if (t1 == t2)
7518 return true;
7519 if (opaque && c1 == c2
8656dafa 7520 && (INTEGRAL_TYPE_P (t1) || c1 == REAL_TYPE)
0af94e6f
JR
7521 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
7522 return true;
7523 return false;
5bed876a
AH
7524}
7525
104f8784
KG
7526/* Check for missing format attributes on function pointers. LTYPE is
7527 the new type or left-hand side type. RTYPE is the old type or
7528 right-hand side type. Returns TRUE if LTYPE is missing the desired
7529 attribute. */
7530
7531bool
7532check_missing_format_attribute (tree ltype, tree rtype)
7533{
7534 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
7535 tree ra;
7536
7537 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
7538 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
7539 break;
7540 if (ra)
7541 {
7542 tree la;
7543 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
7544 if (is_attribute_p ("format", TREE_PURPOSE (la)))
7545 break;
7546 return !la;
7547 }
7548 else
7549 return false;
7550}
7551
d0940d56
DS
7552/* Setup a TYPE_DECL node as a typedef representation.
7553
7554 X is a TYPE_DECL for a typedef statement. Create a brand new
7555 ..._TYPE node (which will be just a variant of the existing
7556 ..._TYPE node with identical properties) and then install X
7557 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
7558
7559 The whole point here is to end up with a situation where each
7560 and every ..._TYPE node the compiler creates will be uniquely
7561 associated with AT MOST one node representing a typedef name.
7562 This way, even though the compiler substitutes corresponding
7563 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
7564 early on, later parts of the compiler can always do the reverse
7565 translation and get back the corresponding typedef name. For
7566 example, given:
7567
7568 typedef struct S MY_TYPE;
7569 MY_TYPE object;
7570
7571 Later parts of the compiler might only know that `object' was of
7572 type `struct S' if it were not for code just below. With this
7573 code however, later parts of the compiler see something like:
7574
7575 struct S' == struct S
7576 typedef struct S' MY_TYPE;
7577 struct S' object;
7578
7579 And they can then deduce (from the node for type struct S') that
7580 the original object declaration was:
7581
7582 MY_TYPE object;
7583
7584 Being able to do this is important for proper support of protoize,
7585 and also for generating precise symbolic debugging information
7586 which takes full account of the programmer's (typedef) vocabulary.
7587
7588 Obviously, we don't want to generate a duplicate ..._TYPE node if
7589 the TYPE_DECL node that we are now processing really represents a
7590 standard built-in type. */
7591
7592void
7593set_underlying_type (tree x)
7594{
7595 if (x == error_mark_node)
7596 return;
42763690 7597 if (DECL_IS_BUILTIN (x) && TREE_CODE (TREE_TYPE (x)) != ARRAY_TYPE)
d0940d56
DS
7598 {
7599 if (TYPE_NAME (TREE_TYPE (x)) == 0)
7600 TYPE_NAME (TREE_TYPE (x)) = x;
7601 }
1ddca3f3
PC
7602 else if (TREE_TYPE (x) != error_mark_node
7603 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
d0940d56
DS
7604 {
7605 tree tt = TREE_TYPE (x);
7606 DECL_ORIGINAL_TYPE (x) = tt;
1ddca3f3
PC
7607 tt = build_variant_type_copy (tt);
7608 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
7609 TYPE_NAME (tt) = x;
3f01b620
MS
7610
7611 /* Mark the type as used only when its type decl is decorated
7612 with attribute unused. */
7613 if (lookup_attribute ("unused", DECL_ATTRIBUTES (x)))
7614 TREE_USED (tt) = 1;
7615
1ddca3f3 7616 TREE_TYPE (x) = tt;
d0940d56
DS
7617 }
7618}
7619
b646ba3f
DS
7620/* Record the types used by the current global variable declaration
7621 being parsed, so that we can decide later to emit their debug info.
7622 Those types are in types_used_by_cur_var_decl, and we are going to
7623 store them in the types_used_by_vars_hash hash table.
7624 DECL is the declaration of the global variable that has been parsed. */
7625
7626void
7627record_types_used_by_current_var_decl (tree decl)
7628{
7629 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
7630
9771b263 7631 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
b646ba3f 7632 {
9771b263 7633 tree type = types_used_by_cur_var_decl->pop ();
bc87224e 7634 types_used_by_var_decl_insert (type, decl);
b646ba3f
DS
7635 }
7636}
7637
c166b898
ILT
7638/* The C and C++ parsers both use vectors to hold function arguments.
7639 For efficiency, we keep a cache of unused vectors. This is the
7640 cache. */
7641
9771b263
DN
7642typedef vec<tree, va_gc> *tree_gc_vec;
7643static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
c166b898
ILT
7644
7645/* Return a new vector from the cache. If the cache is empty,
7646 allocate a new vector. These vectors are GC'ed, so it is OK if the
7647 pointer is not released.. */
7648
9771b263 7649vec<tree, va_gc> *
c166b898
ILT
7650make_tree_vector (void)
7651{
9771b263
DN
7652 if (tree_vector_cache && !tree_vector_cache->is_empty ())
7653 return tree_vector_cache->pop ();
c166b898
ILT
7654 else
7655 {
9771b263 7656 /* Passing 0 to vec::alloc returns NULL, and our callers require
c166b898
ILT
7657 that we always return a non-NULL value. The vector code uses
7658 4 when growing a NULL vector, so we do too. */
9771b263
DN
7659 vec<tree, va_gc> *v;
7660 vec_alloc (v, 4);
7661 return v;
c166b898
ILT
7662 }
7663}
7664
7665/* Release a vector of trees back to the cache. */
7666
7667void
9771b263 7668release_tree_vector (vec<tree, va_gc> *vec)
c166b898
ILT
7669{
7670 if (vec != NULL)
7671 {
9771b263
DN
7672 vec->truncate (0);
7673 vec_safe_push (tree_vector_cache, vec);
c166b898
ILT
7674 }
7675}
7676
7677/* Get a new tree vector holding a single tree. */
7678
9771b263 7679vec<tree, va_gc> *
c166b898
ILT
7680make_tree_vector_single (tree t)
7681{
9771b263
DN
7682 vec<tree, va_gc> *ret = make_tree_vector ();
7683 ret->quick_push (t);
c166b898
ILT
7684 return ret;
7685}
7686
c12ff9d8
JM
7687/* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
7688
9771b263 7689vec<tree, va_gc> *
c12ff9d8
JM
7690make_tree_vector_from_list (tree list)
7691{
9771b263 7692 vec<tree, va_gc> *ret = make_tree_vector ();
c12ff9d8 7693 for (; list; list = TREE_CHAIN (list))
9771b263 7694 vec_safe_push (ret, TREE_VALUE (list));
c12ff9d8
JM
7695 return ret;
7696}
7697
f1644724
JM
7698/* Get a new tree vector of the values of a CONSTRUCTOR. */
7699
7700vec<tree, va_gc> *
7701make_tree_vector_from_ctor (tree ctor)
7702{
7703 vec<tree,va_gc> *ret = make_tree_vector ();
7704 vec_safe_reserve (ret, CONSTRUCTOR_NELTS (ctor));
7705 for (unsigned i = 0; i < CONSTRUCTOR_NELTS (ctor); ++i)
7706 ret->quick_push (CONSTRUCTOR_ELT (ctor, i)->value);
7707 return ret;
7708}
7709
c166b898
ILT
7710/* Get a new tree vector which is a copy of an existing one. */
7711
9771b263
DN
7712vec<tree, va_gc> *
7713make_tree_vector_copy (const vec<tree, va_gc> *orig)
c166b898 7714{
9771b263 7715 vec<tree, va_gc> *ret;
c166b898
ILT
7716 unsigned int ix;
7717 tree t;
7718
7719 ret = make_tree_vector ();
9771b263
DN
7720 vec_safe_reserve (ret, vec_safe_length (orig));
7721 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
7722 ret->quick_push (t);
c166b898
ILT
7723 return ret;
7724}
7725
a9aa2c3a
NF
7726/* Return true if KEYWORD starts a type specifier. */
7727
7728bool
7729keyword_begins_type_specifier (enum rid keyword)
7730{
7731 switch (keyword)
7732 {
38b7bc7f 7733 case RID_AUTO_TYPE:
a9aa2c3a
NF
7734 case RID_INT:
7735 case RID_CHAR:
7736 case RID_FLOAT:
7737 case RID_DOUBLE:
7738 case RID_VOID:
a9aa2c3a
NF
7739 case RID_UNSIGNED:
7740 case RID_LONG:
7741 case RID_SHORT:
7742 case RID_SIGNED:
c65699ef 7743 CASE_RID_FLOATN_NX:
a9aa2c3a
NF
7744 case RID_DFLOAT32:
7745 case RID_DFLOAT64:
7746 case RID_DFLOAT128:
7747 case RID_FRACT:
7748 case RID_ACCUM:
7749 case RID_BOOL:
7750 case RID_WCHAR:
7751 case RID_CHAR16:
7752 case RID_CHAR32:
7753 case RID_SAT:
7754 case RID_COMPLEX:
7755 case RID_TYPEOF:
7756 case RID_STRUCT:
7757 case RID_CLASS:
7758 case RID_UNION:
7759 case RID_ENUM:
7760 return true;
7761 default:
78a7c317
DD
7762 if (keyword >= RID_FIRST_INT_N
7763 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
7764 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
7765 return true;
a9aa2c3a
NF
7766 return false;
7767 }
7768}
7769
7770/* Return true if KEYWORD names a type qualifier. */
7771
7772bool
7773keyword_is_type_qualifier (enum rid keyword)
7774{
7775 switch (keyword)
7776 {
7777 case RID_CONST:
7778 case RID_VOLATILE:
7779 case RID_RESTRICT:
267bac10 7780 case RID_ATOMIC:
a9aa2c3a
NF
7781 return true;
7782 default:
7783 return false;
7784 }
7785}
7786
7787/* Return true if KEYWORD names a storage class specifier.
7788
7789 RID_TYPEDEF is not included in this list despite `typedef' being
7790 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
7791 such for syntactic convenience only. */
7792
7793bool
7794keyword_is_storage_class_specifier (enum rid keyword)
7795{
7796 switch (keyword)
7797 {
7798 case RID_STATIC:
7799 case RID_EXTERN:
7800 case RID_REGISTER:
7801 case RID_AUTO:
7802 case RID_MUTABLE:
7803 case RID_THREAD:
7804 return true;
7805 default:
7806 return false;
7807 }
7808}
7809
ba9e6dd5
NF
7810/* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
7811
7812static bool
7813keyword_is_function_specifier (enum rid keyword)
7814{
7815 switch (keyword)
7816 {
7817 case RID_INLINE:
bbceee64 7818 case RID_NORETURN:
ba9e6dd5
NF
7819 case RID_VIRTUAL:
7820 case RID_EXPLICIT:
7821 return true;
7822 default:
7823 return false;
7824 }
7825}
7826
7827/* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
7828 declaration-specifier (C99 6.7). */
7829
7830bool
7831keyword_is_decl_specifier (enum rid keyword)
7832{
7833 if (keyword_is_storage_class_specifier (keyword)
7834 || keyword_is_type_qualifier (keyword)
7835 || keyword_is_function_specifier (keyword))
7836 return true;
7837
7838 switch (keyword)
7839 {
7840 case RID_TYPEDEF:
7841 case RID_FRIEND:
7842 case RID_CONSTEXPR:
7843 return true;
7844 default:
7845 return false;
7846 }
7847}
7848
81f653d6
NF
7849/* Initialize language-specific-bits of tree_contains_struct. */
7850
7851void
7852c_common_init_ts (void)
7853{
7854 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
7855 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
7856}
7857
3ce4f9e4
ESR
7858/* Build a user-defined numeric literal out of an integer constant type VALUE
7859 with identifier SUFFIX. */
7860
7861tree
2d7aa578
ESR
7862build_userdef_literal (tree suffix_id, tree value,
7863 enum overflow_type overflow, tree num_string)
3ce4f9e4
ESR
7864{
7865 tree literal = make_node (USERDEF_LITERAL);
7866 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
7867 USERDEF_LITERAL_VALUE (literal) = value;
2d7aa578 7868 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
3ce4f9e4
ESR
7869 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
7870 return literal;
7871}
7872
f17a223d
RB
7873/* For vector[index], convert the vector to an array of the underlying type.
7874 Return true if the resulting ARRAY_REF should not be an lvalue. */
aa7da51a
JJ
7875
7876bool
f17a223d
RB
7877convert_vector_to_array_for_subscript (location_t loc,
7878 tree *vecp, tree index)
7edaa4d2 7879{
aa7da51a 7880 bool ret = false;
31521951 7881 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
7edaa4d2
MG
7882 {
7883 tree type = TREE_TYPE (*vecp);
7edaa4d2 7884
aa7da51a 7885 ret = !lvalue_p (*vecp);
f17a223d 7886
7edaa4d2 7887 if (TREE_CODE (index) == INTEGER_CST)
cc269bb6 7888 if (!tree_fits_uhwi_p (index)
928686b1 7889 || maybe_ge (tree_to_uhwi (index), TYPE_VECTOR_SUBPARTS (type)))
7edaa4d2
MG
7890 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
7891
f17a223d
RB
7892 /* We are building an ARRAY_REF so mark the vector as addressable
7893 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
7894 for function parameters. */
7895 c_common_mark_addressable_vec (*vecp);
7896
7897 *vecp = build1 (VIEW_CONVERT_EXPR,
7898 build_array_type_nelts (TREE_TYPE (type),
7899 TYPE_VECTOR_SUBPARTS (type)),
7900 *vecp);
7edaa4d2 7901 }
aa7da51a 7902 return ret;
7edaa4d2
MG
7903}
7904
a212e43f
MG
7905/* Determine which of the operands, if any, is a scalar that needs to be
7906 converted to a vector, for the range of operations. */
7907enum stv_conv
7908scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
7909 bool complain)
7910{
7911 tree type0 = TREE_TYPE (op0);
7912 tree type1 = TREE_TYPE (op1);
7913 bool integer_only_op = false;
7914 enum stv_conv ret = stv_firstarg;
7915
31521951 7916 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
a212e43f
MG
7917 switch (code)
7918 {
7919 /* Most GENERIC binary expressions require homogeneous arguments.
7920 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
7921 argument that is a vector and a second one that is a scalar, so
7922 we never return stv_secondarg for them. */
7923 case RSHIFT_EXPR:
7924 case LSHIFT_EXPR:
7925 if (TREE_CODE (type0) == INTEGER_TYPE
7926 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
7927 {
3cd211af
MS
7928 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0,
7929 NULL_TREE, false))
a212e43f
MG
7930 {
7931 if (complain)
7932 error_at (loc, "conversion of scalar %qT to vector %qT "
7933 "involves truncation", type0, type1);
7934 return stv_error;
7935 }
7936 else
7937 return stv_firstarg;
7938 }
7939 break;
7940
7941 case BIT_IOR_EXPR:
7942 case BIT_XOR_EXPR:
7943 case BIT_AND_EXPR:
7944 integer_only_op = true;
191816a3 7945 /* fall through */
a212e43f 7946
93100c6b
MG
7947 case VEC_COND_EXPR:
7948
a212e43f
MG
7949 case PLUS_EXPR:
7950 case MINUS_EXPR:
7951 case MULT_EXPR:
7952 case TRUNC_DIV_EXPR:
7953 case CEIL_DIV_EXPR:
7954 case FLOOR_DIV_EXPR:
7955 case ROUND_DIV_EXPR:
7956 case EXACT_DIV_EXPR:
7957 case TRUNC_MOD_EXPR:
7958 case FLOOR_MOD_EXPR:
7959 case RDIV_EXPR:
7960 case EQ_EXPR:
7961 case NE_EXPR:
7962 case LE_EXPR:
7963 case GE_EXPR:
7964 case LT_EXPR:
7965 case GT_EXPR:
7966 /* What about UNLT_EXPR? */
31521951 7967 if (VECTOR_TYPE_P (type0))
a212e43f 7968 {
a212e43f 7969 ret = stv_secondarg;
6b4db501
MM
7970 std::swap (type0, type1);
7971 std::swap (op0, op1);
a212e43f
MG
7972 }
7973
7974 if (TREE_CODE (type0) == INTEGER_TYPE
7975 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
7976 {
3cd211af
MS
7977 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0,
7978 NULL_TREE, false))
a212e43f
MG
7979 {
7980 if (complain)
7981 error_at (loc, "conversion of scalar %qT to vector %qT "
7982 "involves truncation", type0, type1);
7983 return stv_error;
7984 }
7985 return ret;
7986 }
7987 else if (!integer_only_op
7988 /* Allow integer --> real conversion if safe. */
7989 && (TREE_CODE (type0) == REAL_TYPE
7990 || TREE_CODE (type0) == INTEGER_TYPE)
7991 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
7992 {
3cd211af
MS
7993 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0,
7994 NULL_TREE, false))
a212e43f
MG
7995 {
7996 if (complain)
7997 error_at (loc, "conversion of scalar %qT to vector %qT "
7998 "involves truncation", type0, type1);
7999 return stv_error;
8000 }
8001 return ret;
8002 }
8003 default:
8004 break;
8005 }
8006
8007 return stv_nothing;
8008}
8009
af63ba4b
JM
8010/* Return the alignment of std::max_align_t.
8011
8012 [support.types.layout] The type max_align_t is a POD type whose alignment
8013 requirement is at least as great as that of every scalar type, and whose
8014 alignment requirement is supported in every context. */
8015
8016unsigned
8017max_align_t_align ()
8018{
63012d9a
JM
8019 unsigned int max_align = MAX (TYPE_ALIGN (long_long_integer_type_node),
8020 TYPE_ALIGN (long_double_type_node));
8021 if (float128_type_node != NULL_TREE)
8022 max_align = MAX (max_align, TYPE_ALIGN (float128_type_node));
8023 return max_align;
af63ba4b
JM
8024}
8025
e28d52cf
DS
8026/* Return true iff ALIGN is an integral constant that is a fundamental
8027 alignment, as defined by [basic.align] in the c++-11
8028 specifications.
8029
8030 That is:
8031
8032 [A fundamental alignment is represented by an alignment less than or
8033 equal to the greatest alignment supported by the implementation
af63ba4b 8034 in all contexts, which is equal to alignof(max_align_t)]. */
e28d52cf
DS
8035
8036bool
af63ba4b 8037cxx_fundamental_alignment_p (unsigned align)
e28d52cf 8038{
af63ba4b 8039 return (align <= max_align_t_align ());
e28d52cf
DS
8040}
8041
f04dda30
MP
8042/* Return true if T is a pointer to a zero-sized aggregate. */
8043
8044bool
8045pointer_to_zero_sized_aggr_p (tree t)
8046{
8047 if (!POINTER_TYPE_P (t))
8048 return false;
8049 t = TREE_TYPE (t);
8050 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
8051}
8052
1807ffc1
MS
8053/* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
8054 with no library fallback or for an ADDR_EXPR whose operand is such type
8055 issues an error pointing to the location LOC.
8056 Returns true when the expression has been diagnosed and false
8057 otherwise. */
038b5cc0 8058
1807ffc1
MS
8059bool
8060reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
8061{
8062 if (TREE_CODE (expr) == ADDR_EXPR)
8063 expr = TREE_OPERAND (expr, 0);
8064
9a004410
DM
8065 STRIP_ANY_LOCATION_WRAPPER (expr);
8066
1807ffc1
MS
8067 if (TREE_TYPE (expr)
8068 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
1be56bc5 8069 && TREE_CODE (expr) == FUNCTION_DECL
1807ffc1
MS
8070 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
8071 false positives for user-declared built-ins such as abs or
8072 strlen, and for C++ operators new and delete.
8073 The c_decl_implicit() test avoids false positives for implicitly
8074 declared built-ins with library fallbacks (such as abs). */
3d78e008 8075 && fndecl_built_in_p (expr)
1807ffc1
MS
8076 && DECL_IS_BUILTIN (expr)
8077 && !c_decl_implicit (expr)
8078 && !DECL_ASSEMBLER_NAME_SET_P (expr))
8079 {
8080 if (loc == UNKNOWN_LOCATION)
8081 loc = EXPR_LOC_OR_LOC (expr, input_location);
8082
8083 /* Reject arguments that are built-in functions with
8084 no library fallback. */
8085 error_at (loc, "built-in function %qE must be directly called", expr);
8086
8087 return true;
8088 }
8089
8090 return false;
8091}
8092
e78bede6
MP
8093/* Check if array size calculations overflow or if the array covers more
8094 than half of the address space. Return true if the size of the array
8095 is valid, false otherwise. TYPE is the type of the array and NAME is
8096 the name of the array, or NULL_TREE for unnamed arrays. */
8097
8098bool
403962ea 8099valid_array_size_p (location_t loc, tree type, tree name, bool complain)
e78bede6
MP
8100{
8101 if (type != error_mark_node
8102 && COMPLETE_TYPE_P (type)
8103 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
8104 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
8105 {
403962ea
JM
8106 if (complain)
8107 {
8108 if (name)
8109 error_at (loc, "size of array %qE is too large", name);
8110 else
8111 error_at (loc, "size of unnamed array is too large");
8112 }
e78bede6
MP
8113 return false;
8114 }
8115 return true;
8116}
8117
174f6622
ES
8118/* Read SOURCE_DATE_EPOCH from environment to have a deterministic
8119 timestamp to replace embedded current dates to get reproducible
8120 results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
15c98b2e 8121
174f6622 8122time_t
15c98b2e 8123cb_get_source_date_epoch (cpp_reader *pfile ATTRIBUTE_UNUSED)
174f6622
ES
8124{
8125 char *source_date_epoch;
fe55692c 8126 int64_t epoch;
174f6622
ES
8127 char *endptr;
8128
8129 source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
8130 if (!source_date_epoch)
8131 return (time_t) -1;
8132
8133 errno = 0;
fe55692c
JDA
8134#if defined(INT64_T_IS_LONG)
8135 epoch = strtol (source_date_epoch, &endptr, 10);
8136#else
174f6622 8137 epoch = strtoll (source_date_epoch, &endptr, 10);
fe55692c 8138#endif
15c98b2e
ES
8139 if (errno != 0 || endptr == source_date_epoch || *endptr != '\0'
8140 || epoch < 0 || epoch > MAX_SOURCE_DATE_EPOCH)
8141 {
8142 error_at (input_location, "environment variable SOURCE_DATE_EPOCH must "
8143 "expand to a non-negative integer less than or equal to %wd",
8144 MAX_SOURCE_DATE_EPOCH);
8145 return (time_t) -1;
8146 }
174f6622
ES
8147
8148 return (time_t) epoch;
8149}
8150
cb18fd07
DM
8151/* Callback for libcpp for offering spelling suggestions for misspelled
8152 directives. GOAL is an unrecognized string; CANDIDATES is a
8153 NULL-terminated array of candidate strings. Return the closest
8154 match to GOAL within CANDIDATES, or NULL if none are good
8155 suggestions. */
8156
8157const char *
8158cb_get_suggestion (cpp_reader *, const char *goal,
8159 const char *const *candidates)
8160{
8161 best_match<const char *, const char *> bm (goal);
8162 while (*candidates)
8163 bm.consider (*candidates++);
8164 return bm.get_best_meaningful_candidate ();
8165}
8166
56d8ffc1
JG
8167/* Return the latice point which is the wider of the two FLT_EVAL_METHOD
8168 modes X, Y. This isn't just >, as the FLT_EVAL_METHOD values added
8169 by C TS 18661-3 for interchange types that are computed in their
8170 native precision are larger than the C11 values for evaluating in the
8171 precision of float/double/long double. If either mode is
8172 FLT_EVAL_METHOD_UNPREDICTABLE, return that. */
8173
8174enum flt_eval_method
8175excess_precision_mode_join (enum flt_eval_method x,
8176 enum flt_eval_method y)
8177{
8178 if (x == FLT_EVAL_METHOD_UNPREDICTABLE
8179 || y == FLT_EVAL_METHOD_UNPREDICTABLE)
8180 return FLT_EVAL_METHOD_UNPREDICTABLE;
8181
8182 /* GCC only supports one interchange type right now, _Float16. If
8183 we're evaluating _Float16 in 16-bit precision, then flt_eval_method
8184 will be FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
8185 if (x == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
8186 return y;
8187 if (y == FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16)
8188 return x;
8189
8190 /* Other values for flt_eval_method are directly comparable, and we want
8191 the maximum. */
8192 return MAX (x, y);
8193}
8194
8195/* Return the value that should be set for FLT_EVAL_METHOD in the
8196 context of ISO/IEC TS 18861-3.
8197
8198 This relates to the effective excess precision seen by the user,
8199 which is the join point of the precision the target requests for
8200 -fexcess-precision={standard,fast} and the implicit excess precision
8201 the target uses. */
8202
8203static enum flt_eval_method
8204c_ts18661_flt_eval_method (void)
8205{
8206 enum flt_eval_method implicit
8207 = targetm.c.excess_precision (EXCESS_PRECISION_TYPE_IMPLICIT);
8208
8209 enum excess_precision_type flag_type
8210 = (flag_excess_precision_cmdline == EXCESS_PRECISION_STANDARD
8211 ? EXCESS_PRECISION_TYPE_STANDARD
8212 : EXCESS_PRECISION_TYPE_FAST);
8213
8214 enum flt_eval_method requested
8215 = targetm.c.excess_precision (flag_type);
8216
8217 return excess_precision_mode_join (implicit, requested);
8218}
8219
8220/* As c_cpp_ts18661_flt_eval_method, but clamps the expected values to
8221 those that were permitted by C11. That is to say, eliminates
8222 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT16. */
8223
8224static enum flt_eval_method
8225c_c11_flt_eval_method (void)
8226{
8227 return excess_precision_mode_join (c_ts18661_flt_eval_method (),
8228 FLT_EVAL_METHOD_PROMOTE_TO_FLOAT);
8229}
8230
8231/* Return the value that should be set for FLT_EVAL_METHOD.
8232 MAYBE_C11_ONLY_P is TRUE if we should check
8233 FLAG_PERMITTED_EVAL_METHODS as to whether we should limit the possible
8234 values we can return to those from C99/C11, and FALSE otherwise.
8235 See the comments on c_ts18661_flt_eval_method for what value we choose
8236 to set here. */
8237
8238int
8239c_flt_eval_method (bool maybe_c11_only_p)
8240{
8241 if (maybe_c11_only_p
8242 && flag_permitted_flt_eval_methods
8243 == PERMITTED_FLT_EVAL_METHODS_C11)
8244 return c_c11_flt_eval_method ();
8245 else
8246 return c_ts18661_flt_eval_method ();
8247}
8248
62e1c678
DM
8249/* An enum for get_missing_token_insertion_kind for describing the best
8250 place to insert a missing token, if there is one. */
8251
8252enum missing_token_insertion_kind
8253{
8254 MTIK_IMPOSSIBLE,
8255 MTIK_INSERT_BEFORE_NEXT,
8256 MTIK_INSERT_AFTER_PREV
8257};
8258
8259/* Given a missing token of TYPE, determine if it is reasonable to
8260 emit a fix-it hint suggesting the insertion of the token, and,
8261 if so, where the token should be inserted relative to other tokens.
8262
8263 It only makes sense to do this for values of TYPE that are symbols.
8264
8265 Some symbols should go before the next token, e.g. in:
8266 if flag)
8267 we want to insert the missing '(' immediately before "flag",
8268 giving:
8269 if (flag)
8270 rather than:
8271 if( flag)
8272 These use MTIK_INSERT_BEFORE_NEXT.
8273
8274 Other symbols should go after the previous token, e.g. in:
8275 if (flag
8276 do_something ();
8277 we want to insert the missing ')' immediately after the "flag",
8278 giving:
8279 if (flag)
8280 do_something ();
8281 rather than:
8282 if (flag
8283 )do_something ();
8284 These use MTIK_INSERT_AFTER_PREV. */
8285
8286static enum missing_token_insertion_kind
8287get_missing_token_insertion_kind (enum cpp_ttype type)
8288{
8289 switch (type)
8290 {
8291 /* Insert missing "opening" brackets immediately
8292 before the next token. */
8293 case CPP_OPEN_SQUARE:
8294 case CPP_OPEN_PAREN:
8295 return MTIK_INSERT_BEFORE_NEXT;
8296
8297 /* Insert other missing symbols immediately after
8298 the previous token. */
8299 case CPP_CLOSE_PAREN:
8300 case CPP_CLOSE_SQUARE:
8301 case CPP_SEMICOLON:
8302 case CPP_COMMA:
8303 case CPP_COLON:
8304 return MTIK_INSERT_AFTER_PREV;
8305
8306 /* Other kinds of token don't get fix-it hints. */
8307 default:
8308 return MTIK_IMPOSSIBLE;
8309 }
8310}
8311
8312/* Given RICHLOC, a location for a diagnostic describing a missing token
8313 of kind TOKEN_TYPE, potentially add a fix-it hint suggesting the
8314 insertion of the token.
8315
8316 The location of the attempted fix-it hint depends on TOKEN_TYPE:
8317 it will either be:
8318 (a) immediately after PREV_TOKEN_LOC, or
8319
8320 (b) immediately before the primary location within RICHLOC (taken to
8321 be that of the token following where the token was expected).
8322
8323 If we manage to add a fix-it hint, then the location of the
8324 fix-it hint is likely to be more useful as the primary location
8325 of the diagnostic than that of the following token, so we swap
8326 these locations.
8327
8328 For example, given this bogus code:
8329 123456789012345678901234567890
8330 1 | int missing_semicolon (void)
8331 2 | {
8332 3 | return 42
8333 4 | }
8334
8335 we will emit:
8336
8337 "expected ';' before '}'"
8338
8339 RICHLOC's primary location is at the closing brace, so before "swapping"
8340 we would emit the error at line 4 column 1:
8341
8342 123456789012345678901234567890
8343 3 | return 42 |< fix-it hint emitted for this line
8344 | ; |
8345 4 | } |< "expected ';' before '}'" emitted at this line
8346 | ^ |
8347
8348 It's more useful for the location of the diagnostic to be at the
8349 fix-it hint, so we swap the locations, so the primary location
8350 is at the fix-it hint, with the old primary location inserted
8351 as a secondary location, giving this, with the error at line 3
8352 column 12:
8353
8354 123456789012345678901234567890
8355 3 | return 42 |< "expected ';' before '}'" emitted at this line,
8356 | ^ | with fix-it hint
8357 4 | ; |
8358 | } |< secondary range emitted here
8359 | ~ |. */
8360
8361void
8362maybe_suggest_missing_token_insertion (rich_location *richloc,
8363 enum cpp_ttype token_type,
8364 location_t prev_token_loc)
8365{
8366 gcc_assert (richloc);
8367
8368 enum missing_token_insertion_kind mtik
8369 = get_missing_token_insertion_kind (token_type);
8370
8371 switch (mtik)
8372 {
8373 default:
8374 gcc_unreachable ();
8375 break;
8376
8377 case MTIK_IMPOSSIBLE:
8378 return;
8379
8380 case MTIK_INSERT_BEFORE_NEXT:
8381 /* Attempt to add the fix-it hint before the primary location
8382 of RICHLOC. */
8383 richloc->add_fixit_insert_before (cpp_type2name (token_type, 0));
8384 break;
8385
8386 case MTIK_INSERT_AFTER_PREV:
8387 /* Attempt to add the fix-it hint after PREV_TOKEN_LOC. */
8388 richloc->add_fixit_insert_after (prev_token_loc,
8389 cpp_type2name (token_type, 0));
8390 break;
8391 }
8392
8393 /* If we were successful, use the fix-it hint's location as the
8394 primary location within RICHLOC, adding the old primary location
8395 back as a secondary location. */
8396 if (!richloc->seen_impossible_fixit_p ())
8397 {
8398 fixit_hint *hint = richloc->get_last_fixit_hint ();
8399 location_t hint_loc = hint->get_start_loc ();
8400 location_t old_loc = richloc->get_loc ();
8401
85204e23
DM
8402 richloc->set_range (0, hint_loc, SHOW_RANGE_WITH_CARET);
8403 richloc->add_range (old_loc);
62e1c678
DM
8404 }
8405}
8406
b6f43128
DM
8407#if CHECKING_P
8408
8409namespace selftest {
8410
9a004410
DM
8411/* Verify that fold_for_warn on error_mark_node is safe. */
8412
8413static void
8414test_fold_for_warn ()
8415{
8416 ASSERT_EQ (error_mark_node, fold_for_warn (error_mark_node));
8417}
8418
8419/* Run all of the selftests within this file. */
8420
8421static void
8422c_common_c_tests ()
8423{
8424 test_fold_for_warn ();
8425}
8426
b6f43128
DM
8427/* Run all of the tests within c-family. */
8428
8429void
8430c_family_tests (void)
8431{
9a004410 8432 c_common_c_tests ();
b6f43128 8433 c_format_c_tests ();
10fcc142 8434 c_indentation_c_tests ();
9a004410 8435 c_pretty_print_c_tests ();
c79144f8 8436 c_spellcheck_cc_tests ();
b6f43128
DM
8437}
8438
8439} // namespace selftest
8440
8441#endif /* #if CHECKING_P */
8442
eea77d1f
DM
8443/* Attempt to locate a suitable location within FILE for a
8444 #include directive to be inserted before. FILE should
8445 be a string from libcpp (pointer equality is used).
8446 LOC is the location of the relevant diagnostic.
8447
8448 Attempt to return the location within FILE immediately
8449 after the last #include within that file, or the start of
8450 that file if it has no #include directives.
8451
8452 Return UNKNOWN_LOCATION if no suitable location is found,
8453 or if an error occurs. */
8454
8455static location_t
8456try_to_locate_new_include_insertion_point (const char *file, location_t loc)
8457{
8458 /* Locate the last ordinary map within FILE that ended with a #include. */
8459 const line_map_ordinary *last_include_ord_map = NULL;
8460
8461 /* ...and the next ordinary map within FILE after that one. */
8462 const line_map_ordinary *last_ord_map_after_include = NULL;
8463
8464 /* ...and the first ordinary map within FILE. */
8465 const line_map_ordinary *first_ord_map_in_file = NULL;
8466
8467 /* Get ordinary map containing LOC (or its expansion). */
8468 const line_map_ordinary *ord_map_for_loc = NULL;
8469 loc = linemap_resolve_location (line_table, loc, LRK_MACRO_EXPANSION_POINT,
8470 &ord_map_for_loc);
8471 gcc_assert (ord_map_for_loc);
8472
8473 for (unsigned int i = 0; i < LINEMAPS_ORDINARY_USED (line_table); i++)
8474 {
8475 const line_map_ordinary *ord_map
8476 = LINEMAPS_ORDINARY_MAP_AT (line_table, i);
8477
f10a9135
NS
8478 if (const line_map_ordinary *from
8479 = linemap_included_from_linemap (line_table, ord_map))
eea77d1f
DM
8480 if (from->to_file == file)
8481 {
8482 last_include_ord_map = from;
8483 last_ord_map_after_include = NULL;
8484 }
8485
8486 if (ord_map->to_file == file)
8487 {
8488 if (!first_ord_map_in_file)
8489 first_ord_map_in_file = ord_map;
8490 if (last_include_ord_map && !last_ord_map_after_include)
8491 last_ord_map_after_include = ord_map;
8492 }
8493
8494 /* Stop searching when reaching the ord_map containing LOC,
8495 as it makes no sense to provide fix-it hints that appear
8496 after the diagnostic in question. */
8497 if (ord_map == ord_map_for_loc)
8498 break;
8499 }
8500
8501 /* Determine where to insert the #include. */
8502 const line_map_ordinary *ord_map_for_insertion;
8503
8504 /* We want the next ordmap in the file after the last one that's a
8505 #include, but failing that, the start of the file. */
8506 if (last_ord_map_after_include)
8507 ord_map_for_insertion = last_ord_map_after_include;
8508 else
8509 ord_map_for_insertion = first_ord_map_in_file;
8510
8511 if (!ord_map_for_insertion)
8512 return UNKNOWN_LOCATION;
8513
8514 /* The "start_location" is column 0, meaning "the whole line".
8515 rich_location and edit_context can't cope with this, so use
8516 column 1 instead. */
8517 location_t col_0 = ord_map_for_insertion->start_location;
8518 return linemap_position_for_loc_and_offset (line_table, col_0, 1);
8519}
8520
8521/* A map from filenames to sets of headers added to them, for
8522 ensuring idempotency within maybe_add_include_fixit. */
8523
8524/* The values within the map. We need string comparison as there's
8525 no guarantee that two different diagnostics that are recommending
8526 adding e.g. "<stdio.h>" are using the same buffer. */
8527
8528typedef hash_set <const char *, nofree_string_hash> per_file_includes_t;
8529
8530/* The map itself. We don't need string comparison for the filename keys,
8531 as they come from libcpp. */
8532
8533typedef hash_map <const char *, per_file_includes_t *> added_includes_t;
8534static added_includes_t *added_includes;
8535
8536/* Attempt to add a fix-it hint to RICHLOC, adding "#include HEADER\n"
8537 in a suitable location within the file of RICHLOC's primary
8538 location.
8539
8540 This function is idempotent: a header will be added at most once to
85204e23
DM
8541 any given file.
8542
8543 If OVERRIDE_LOCATION is true, then if a fix-it is added and will be
8544 printed, then RICHLOC's primary location will be replaced by that of
8545 the fix-it hint (for use by "inform" notes where the location of the
8546 issue has already been reported). */
eea77d1f
DM
8547
8548void
85204e23
DM
8549maybe_add_include_fixit (rich_location *richloc, const char *header,
8550 bool override_location)
eea77d1f
DM
8551{
8552 location_t loc = richloc->get_loc ();
8553 const char *file = LOCATION_FILE (loc);
8554 if (!file)
8555 return;
8556
8557 /* Idempotency: don't add the same header more than once to a given file. */
8558 if (!added_includes)
8559 added_includes = new added_includes_t ();
8560 per_file_includes_t *&set = added_includes->get_or_insert (file);
8561 if (set)
8562 if (set->contains (header))
8563 /* ...then we've already added HEADER to that file. */
8564 return;
8565 if (!set)
8566 set = new per_file_includes_t ();
8567 set->add (header);
8568
8569 /* Attempt to locate a suitable place for the new directive. */
8570 location_t include_insert_loc
8571 = try_to_locate_new_include_insertion_point (file, loc);
8572 if (include_insert_loc == UNKNOWN_LOCATION)
8573 return;
8574
8575 char *text = xasprintf ("#include %s\n", header);
8576 richloc->add_fixit_insert_before (include_insert_loc, text);
8577 free (text);
85204e23
DM
8578
8579 if (override_location && global_dc->show_caret)
8580 {
8581 /* Replace the primary location with that of the insertion point for the
8582 fix-it hint.
8583
8584 We use SHOW_LINES_WITHOUT_RANGE so that we don't meaningless print a
8585 caret for the insertion point (or colorize it).
8586
8587 Hence we print e.g.:
8588
8589 ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
8590 73 | # include <debug/vector>
8591 +++ |+#include <vector>
8592 74 | #endif
8593
8594 rather than:
8595
8596 ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
8597 73 | # include <debug/vector>
8598 +++ |+#include <vector>
8599 74 | #endif
8600 | ^
8601
8602 avoiding the caret on the first column of line 74. */
8603 richloc->set_range (0, include_insert_loc, SHOW_LINES_WITHOUT_RANGE);
8604 }
eea77d1f
DM
8605}
8606
23aa9f7c 8607/* Attempt to convert a braced array initializer list CTOR for array
b5764229
BE
8608 TYPE into a STRING_CST for convenience and efficiency. Return
8609 the converted string on success or the original ctor on failure. */
23aa9f7c
MS
8610
8611tree
b5764229 8612braced_list_to_string (tree type, tree ctor)
23aa9f7c 8613{
b5764229
BE
8614 if (!tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
8615 return ctor;
23aa9f7c
MS
8616
8617 /* If the array has an explicit bound, use it to constrain the size
8618 of the string. If it doesn't, be sure to create a string that's
8619 as long as implied by the index of the last zero specified via
8620 a designator, as in:
8621 const char a[] = { [7] = 0 }; */
b5764229
BE
8622 unsigned HOST_WIDE_INT maxelts = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8623 maxelts /= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
23aa9f7c 8624
b5764229
BE
8625 /* Avoid converting initializers for zero-length arrays. */
8626 if (!maxelts)
8627 return ctor;
23aa9f7c 8628
b5764229 8629 unsigned HOST_WIDE_INT nelts = CONSTRUCTOR_NELTS (ctor);
23aa9f7c
MS
8630
8631 auto_vec<char> str;
8632 str.reserve (nelts + 1);
8633
8634 unsigned HOST_WIDE_INT i;
8635 tree index, value;
8636
8637 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, index, value)
8638 {
b5764229
BE
8639 unsigned HOST_WIDE_INT idx = i;
8640 if (index)
8641 {
8642 if (!tree_fits_uhwi_p (index))
8643 return ctor;
8644 idx = tree_to_uhwi (index);
8645 }
23aa9f7c
MS
8646
8647 /* auto_vec is limited to UINT_MAX elements. */
8648 if (idx > UINT_MAX)
b5764229 8649 return ctor;
23aa9f7c 8650
b5764229 8651 /* Avoid non-constant initializers. */
23aa9f7c 8652 if (!tree_fits_shwi_p (value))
b5764229 8653 return ctor;
23aa9f7c
MS
8654
8655 /* Skip over embedded nuls except the last one (initializer
8656 elements are in ascending order of indices). */
8657 HOST_WIDE_INT val = tree_to_shwi (value);
8658 if (!val && i + 1 < nelts)
8659 continue;
8660
b5764229
BE
8661 if (idx < str.length())
8662 return ctor;
8663
23aa9f7c
MS
8664 /* Bail if the CTOR has a block of more than 256 embedded nuls
8665 due to implicitly initialized elements. */
8666 unsigned nchars = (idx - str.length ()) + 1;
8667 if (nchars > 256)
b5764229 8668 return ctor;
23aa9f7c
MS
8669
8670 if (nchars > 1)
8671 {
8672 str.reserve (idx);
8673 str.quick_grow_cleared (idx);
8674 }
8675
b5764229
BE
8676 if (idx >= maxelts)
8677 return ctor;
23aa9f7c
MS
8678
8679 str.safe_insert (idx, val);
8680 }
8681
b5764229
BE
8682 /* Append a nul string termination. */
8683 if (str.length () < maxelts)
23aa9f7c
MS
8684 str.safe_push (0);
8685
b5764229 8686 /* Build a STRING_CST with the same type as the array. */
23aa9f7c
MS
8687 tree res = build_string (str.length (), str.begin ());
8688 TREE_TYPE (res) = type;
8689 return res;
8690}
8691
39dabefd 8692#include "gt-c-family-c-common.h"