]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-family/c-common.c
Move class temp_source_file from input.c to selftest.c/h
[thirdparty/gcc.git] / gcc / c-family / c-common.c
CommitLineData
b30f223b 1/* Subroutines shared by all languages that are variants of C.
818ab71a 2 Copyright (C) 1992-2016 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"
b559c810 28#include "c-common.h"
2adfab87
AM
29#include "gimple-expr.h"
30#include "tm_p.h"
31#include "stringpool.h"
32#include "cgraph.h"
33#include "diagnostic.h"
d9b2742a 34#include "intl.h"
d8a2d370
DN
35#include "stor-layout.h"
36#include "calls.h"
d8a2d370
DN
37#include "attribs.h"
38#include "varasm.h"
39#include "trans-mem.h"
61d3ce20 40#include "c-objc.h"
677f3fa8 41#include "common/common-target.h"
7afff7cf 42#include "langhooks.h"
d57a4b98 43#include "tree-inline.h"
5f1989e6 44#include "toplev.h"
6de9cd9a 45#include "tree-iterator.h"
d974312d 46#include "opts.h"
45b0be94 47#include "gimplify.h"
88fa5555 48#include "substring-locations.h"
cb60f38d 49
81a75f0f 50cpp_reader *parse_in; /* Declared in c-pragma.h. */
c8724862 51
8fba1830
BRF
52/* Mode used to build pointers (VOIDmode means ptr_mode). */
53
54machine_mode c_default_pointer_mode = VOIDmode;
55
7f4edbcb 56/* The following symbols are subsumed in the c_global_trees array, and
d125d268 57 listed here individually for documentation purposes.
7f4edbcb
BS
58
59 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
60
61 tree short_integer_type_node;
62 tree long_integer_type_node;
63 tree long_long_integer_type_node;
64
65 tree short_unsigned_type_node;
66 tree long_unsigned_type_node;
67 tree long_long_unsigned_type_node;
68
de7df9eb
JM
69 tree truthvalue_type_node;
70 tree truthvalue_false_node;
71 tree truthvalue_true_node;
7f4edbcb
BS
72
73 tree ptrdiff_type_node;
74
75 tree unsigned_char_type_node;
76 tree signed_char_type_node;
77 tree wchar_type_node;
7f4edbcb 78
b6baa67d
KVH
79 tree char16_type_node;
80 tree char32_type_node;
81
7f4edbcb
BS
82 tree float_type_node;
83 tree double_type_node;
84 tree long_double_type_node;
85
86 tree complex_integer_type_node;
87 tree complex_float_type_node;
88 tree complex_double_type_node;
89 tree complex_long_double_type_node;
90
9a8ce21f
JG
91 tree dfloat32_type_node;
92 tree dfloat64_type_node;
93 tree_dfloat128_type_node;
94
7f4edbcb
BS
95 tree intQI_type_node;
96 tree intHI_type_node;
97 tree intSI_type_node;
98 tree intDI_type_node;
99 tree intTI_type_node;
100
101 tree unsigned_intQI_type_node;
102 tree unsigned_intHI_type_node;
103 tree unsigned_intSI_type_node;
104 tree unsigned_intDI_type_node;
105 tree unsigned_intTI_type_node;
106
107 tree widest_integer_literal_type_node;
108 tree widest_unsigned_literal_type_node;
109
110 Nodes for types `void *' and `const void *'.
111
112 tree ptr_type_node, const_ptr_type_node;
113
114 Nodes for types `char *' and `const char *'.
115
116 tree string_type_node, const_string_type_node;
117
118 Type `char[SOMENUMBER]'.
119 Used when an array of char is needed and the size is irrelevant.
120
121 tree char_array_type_node;
122
7f4edbcb
BS
123 Type `wchar_t[SOMENUMBER]' or something like it.
124 Used when a wide string literal is created.
125
126 tree wchar_array_type_node;
127
b6baa67d
KVH
128 Type `char16_t[SOMENUMBER]' or something like it.
129 Used when a UTF-16 string literal is created.
130
131 tree char16_array_type_node;
132
133 Type `char32_t[SOMENUMBER]' or something like it.
134 Used when a UTF-32 string literal is created.
135
136 tree char32_array_type_node;
137
7f4edbcb
BS
138 Type `int ()' -- used for implicit declaration of functions.
139
140 tree default_function_type;
141
7f4edbcb
BS
142 A VOID_TYPE node, packaged in a TREE_LIST.
143
144 tree void_list_node;
145
684d9f3b 146 The lazily created VAR_DECLs for __FUNCTION__, __PRETTY_FUNCTION__,
0ba8a114
NS
147 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
148 VAR_DECLS, but C++ does.)
63ad61ed 149
0ba8a114 150 tree function_name_decl_node;
684d9f3b 151 tree pretty_function_name_decl_node;
0ba8a114
NS
152 tree c99_function_name_decl_node;
153
154 Stack of nested function name VAR_DECLs.
35b1a6fa 155
0ba8a114 156 tree saved_function_name_decls;
63ad61ed 157
7f4edbcb
BS
158*/
159
160tree c_global_trees[CTI_MAX];
17211ab5 161\f
4078b403
NB
162/* Switches common to the C front ends. */
163
63973df3
NB
164/* Nonzero means don't output line number information. */
165
166char flag_no_line_commands;
167
168/* Nonzero causes -E output not to be done, but directives such as
169 #define that have side effects are still obeyed. */
170
171char flag_no_output;
172
173/* Nonzero means dump macros in some fashion. */
174
175char flag_dump_macros;
176
177/* Nonzero means pass #include lines through to the output. */
178
179char flag_dump_includes;
180
c0d578e6
GK
181/* Nonzero means process PCH files while preprocessing. */
182
183bool flag_pch_preprocess;
184
17211ab5
GK
185/* The file name to which we should write a precompiled header, or
186 NULL if no header will be written in this compile. */
187
188const char *pch_file;
189
3df89291
NB
190/* Nonzero if an ISO standard was selected. It rejects macros in the
191 user's namespace. */
192int flag_iso;
193
4078b403
NB
194/* C/ObjC language option variables. */
195
196
4078b403
NB
197/* Nonzero means allow type mismatches in conditional expressions;
198 just make their values `void'. */
199
200int flag_cond_mismatch;
201
202/* Nonzero means enable C89 Amendment 1 features. */
203
204int flag_isoc94;
205
48b0b196 206/* Nonzero means use the ISO C99 (or C11) dialect of C. */
4078b403
NB
207
208int flag_isoc99;
209
48b0b196 210/* Nonzero means use the ISO C11 dialect of C. */
2778d766 211
48b0b196 212int flag_isoc11;
2778d766 213
6614fd40 214/* Nonzero means that we have builtin functions, and main is an int. */
4078b403
NB
215
216int flag_hosted = 1;
217
4078b403
NB
218
219/* ObjC language option variables. */
220
221
4078b403
NB
222/* Tells the compiler that this is a special run. Do not perform any
223 compiling, instead we are to test some platform dependent features
224 and output a C header file with appropriate definitions. */
225
226int print_struct_values;
227
fa10beec 228/* Tells the compiler what is the constant string class for ObjC. */
4078b403
NB
229
230const char *constant_string_class_name;
231
4078b403
NB
232
233/* C++ language option variables. */
234
a75f1574
JM
235/* The reference version of the ABI for -Wabi. */
236
237int warn_abi_version = -1;
4078b403 238
4078b403
NB
239/* Nonzero means generate separate instantiation control files and
240 juggle them at link time. */
241
242int flag_use_repository;
243
129211bc 244/* The C++ dialect being used. Default set in c_common_post_options. */
966541e3 245
129211bc 246enum cxx_dialect cxx_dialect = cxx_unset;
966541e3 247
4afe7ad7 248/* Maximum template instantiation depth. This limit exists to limit the
3ff60975 249 time it takes to notice excessively recursive template instantiations.
4078b403 250
3ff60975
JM
251 The default is lower than the 1024 recommended by the C++0x standard
252 because G++ runs out of stack before 1024 with highly recursive template
253 argument deduction substitution (g++.dg/cpp0x/enum11.C). */
254
255int max_tinst_depth = 900;
4078b403 256
f09f1de5
MM
257/* The elements of `ridpointers' are identifier nodes for the reserved
258 type names and storage classes. It is indexed by a RID_... value. */
259tree *ridpointers;
260
c2255bc4 261tree (*make_fname_decl) (location_t, tree, int);
2ce07e2d 262
7d882b83
ILT
263/* Nonzero means don't warn about problems that occur when the code is
264 executed. */
265int c_inhibit_evaluation_warnings;
e78a3b42 266
5386338c
JM
267/* Whether we are building a boolean conversion inside
268 convert_for_assignment, or some other late binary operation. If
269 build_binary_op is called for C (from code shared by C and C++) in
270 this case, then the operands have already been folded and the
271 result will not be folded again, so C_MAYBE_CONST_EXPR should not
272 be generated. */
273bool in_late_binary_op;
274
148e4216
JM
275/* Whether lexing has been completed, so subsequent preprocessor
276 errors should use the compiler's input_location. */
277bool done_lexing = false;
278
ec5c56db 279/* Information about how a function name is generated. */
0ba8a114
NS
280struct fname_var_t
281{
8b60264b
KG
282 tree *const decl; /* pointer to the VAR_DECL. */
283 const unsigned rid; /* RID number for the identifier. */
284 const int pretty; /* How pretty is it? */
0ba8a114
NS
285};
286
ec5c56db 287/* The three ways of getting then name of the current function. */
0ba8a114
NS
288
289const struct fname_var_t fname_vars[] =
290{
ec5c56db 291 /* C99 compliant __func__, must be first. */
0ba8a114 292 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
ec5c56db 293 /* GCC __FUNCTION__ compliant. */
0ba8a114 294 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
ec5c56db 295 /* GCC __PRETTY_FUNCTION__ compliant. */
0ba8a114
NS
296 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
297 {NULL, 0, 0},
298};
299
c98cd5bf
JM
300/* Global visibility options. */
301struct visibility_flags visibility_options;
302
62e4eb35 303static tree check_case_value (location_t, tree);
b155cfd9
MP
304static bool check_case_bounds (location_t, tree, tree, tree *, tree *,
305 bool *);
4724b3de 306
35b1a6fa
AJ
307static tree handle_packed_attribute (tree *, tree, tree, int, bool *);
308static tree handle_nocommon_attribute (tree *, tree, tree, int, bool *);
309static tree handle_common_attribute (tree *, tree, tree, int, bool *);
310static tree handle_noreturn_attribute (tree *, tree, tree, int, bool *);
52bf96d2
JH
311static tree handle_hot_attribute (tree *, tree, tree, int, bool *);
312static tree handle_cold_attribute (tree *, tree, tree, int, bool *);
e664c61c
KS
313static tree handle_no_sanitize_address_attribute (tree *, tree, tree,
314 int, bool *);
77bc5132
JJ
315static tree handle_no_address_safety_analysis_attribute (tree *, tree, tree,
316 int, bool *);
ce6923c5
MP
317static tree handle_no_sanitize_undefined_attribute (tree *, tree, tree, int,
318 bool *);
5434dc07 319static tree handle_stack_protect_attribute (tree *, tree, tree, int, bool *);
35b1a6fa 320static tree handle_noinline_attribute (tree *, tree, tree, int, bool *);
86631ea3 321static tree handle_noclone_attribute (tree *, tree, tree, int, bool *);
185c9e56 322static tree handle_noicf_attribute (tree *, tree, tree, int, bool *);
46a4da10 323static tree handle_leaf_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
324static tree handle_always_inline_attribute (tree *, tree, tree, int,
325 bool *);
d752cfdb
JJ
326static tree handle_gnu_inline_attribute (tree *, tree, tree, int, bool *);
327static tree handle_artificial_attribute (tree *, tree, tree, int, bool *);
0691d1d4 328static tree handle_flatten_attribute (tree *, tree, tree, int, bool *);
d2af6a68 329static tree handle_error_attribute (tree *, tree, tree, int, bool *);
35b1a6fa 330static tree handle_used_attribute (tree *, tree, tree, int, bool *);
ce91e74c
JH
331static tree handle_externally_visible_attribute (tree *, tree, tree, int,
332 bool *);
7861b648
AK
333static tree handle_no_reorder_attribute (tree *, tree, tree, int,
334 bool *);
35b1a6fa
AJ
335static tree handle_const_attribute (tree *, tree, tree, int, bool *);
336static tree handle_transparent_union_attribute (tree *, tree, tree,
337 int, bool *);
ee45a32d
EB
338static tree handle_scalar_storage_order_attribute (tree *, tree, tree,
339 int, bool *);
35b1a6fa
AJ
340static tree handle_constructor_attribute (tree *, tree, tree, int, bool *);
341static tree handle_destructor_attribute (tree *, tree, tree, int, bool *);
342static tree handle_mode_attribute (tree *, tree, tree, int, bool *);
343static tree handle_section_attribute (tree *, tree, tree, int, bool *);
344static tree handle_aligned_attribute (tree *, tree, tree, int, bool *);
345static tree handle_weak_attribute (tree *, tree, tree, int, bool *) ;
4bb794e2 346static tree handle_noplt_attribute (tree *, tree, tree, int, bool *) ;
ba885ec5
NS
347static tree handle_alias_ifunc_attribute (bool, tree *, tree, tree, bool *);
348static tree handle_ifunc_attribute (tree *, tree, tree, int, bool *);
35b1a6fa 349static tree handle_alias_attribute (tree *, tree, tree, int, bool *);
a0203ca7 350static tree handle_weakref_attribute (tree *, tree, tree, int, bool *) ;
35b1a6fa
AJ
351static tree handle_visibility_attribute (tree *, tree, tree, int,
352 bool *);
353static tree handle_tls_model_attribute (tree *, tree, tree, int,
354 bool *);
355static tree handle_no_instrument_function_attribute (tree *, tree,
356 tree, int, bool *);
1225d6b1
ML
357static tree handle_no_profile_instrument_function_attribute (tree *, tree,
358 tree, int, bool *);
35b1a6fa 359static tree handle_malloc_attribute (tree *, tree, tree, int, bool *);
6e9a3221 360static tree handle_returns_twice_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
361static tree handle_no_limit_stack_attribute (tree *, tree, tree, int,
362 bool *);
363static tree handle_pure_attribute (tree *, tree, tree, int, bool *);
0a35513e
AH
364static tree handle_tm_attribute (tree *, tree, tree, int, bool *);
365static tree handle_tm_wrap_attribute (tree *, tree, tree, int, bool *);
dcd6de6d 366static tree handle_novops_attribute (tree *, tree, tree, int, bool *);
35b1a6fa
AJ
367static tree handle_deprecated_attribute (tree *, tree, tree, int,
368 bool *);
369static tree handle_vector_size_attribute (tree *, tree, tree, int,
370 bool *);
371static tree handle_nonnull_attribute (tree *, tree, tree, int, bool *);
372static tree handle_nothrow_attribute (tree *, tree, tree, int, bool *);
373static tree handle_cleanup_attribute (tree *, tree, tree, int, bool *);
72954a4f
JM
374static tree handle_warn_unused_result_attribute (tree *, tree, tree, int,
375 bool *);
3d091dac 376static tree handle_sentinel_attribute (tree *, tree, tree, int, bool *);
b5d32c25 377static tree handle_type_generic_attribute (tree *, tree, tree, int, bool *);
51bc54a6 378static tree handle_alloc_size_attribute (tree *, tree, tree, int, bool *);
8fcbce72
JJ
379static tree handle_alloc_align_attribute (tree *, tree, tree, int, bool *);
380static tree handle_assume_aligned_attribute (tree *, tree, tree, int, bool *);
5779e713 381static tree handle_target_attribute (tree *, tree, tree, int, bool *);
3b1661a9 382static tree handle_target_clones_attribute (tree *, tree, tree, int, bool *);
ab442df7 383static tree handle_optimize_attribute (tree *, tree, tree, int, bool *);
0a35513e 384static tree ignore_attribute (tree *, tree, tree, int, bool *);
7458026b 385static tree handle_no_split_stack_attribute (tree *, tree, tree, int, bool *);
0b7b376d 386static tree handle_fnspec_attribute (tree *, tree, tree, int, bool *);
2a99e5e6 387static tree handle_warn_unused_attribute (tree *, tree, tree, int, bool *);
826cacfe 388static tree handle_returns_nonnull_attribute (tree *, tree, tree, int, bool *);
acf0174b
JJ
389static tree handle_omp_declare_simd_attribute (tree *, tree, tree, int,
390 bool *);
fff77217 391static tree handle_simd_attribute (tree *, tree, tree, int, bool *);
acf0174b
JJ
392static tree handle_omp_declare_target_attribute (tree *, tree, tree, int,
393 bool *);
976d5a22 394static tree handle_designated_init_attribute (tree *, tree, tree, int, bool *);
d5e254e1
IE
395static tree handle_bnd_variable_size_attribute (tree *, tree, tree, int, bool *);
396static tree handle_bnd_legacy (tree *, tree, tree, int, bool *);
397static tree handle_bnd_instrument (tree *, tree, tree, int, bool *);
35b1a6fa 398
35b1a6fa
AJ
399static void check_nonnull_arg (void *, tree, unsigned HOST_WIDE_INT);
400static bool nonnull_check_p (tree, unsigned HOST_WIDE_INT);
401static bool get_nonnull_operand (tree, unsigned HOST_WIDE_INT *);
d07605f5 402static int resort_field_decl_cmp (const void *, const void *);
b34c7881 403
eea1139b
ILT
404/* Reserved words. The third field is a mask: keywords are disabled
405 if they match the mask.
406
407 Masks for languages:
408 C --std=c89: D_C99 | D_CXXONLY | D_OBJC | D_CXX_OBJC
409 C --std=c99: D_CXXONLY | D_OBJC
410 ObjC is like C except that D_OBJC and D_CXX_OBJC are not set
411 C++ --std=c98: D_CONLY | D_CXXOX | D_OBJC
412 C++ --std=c0x: D_CONLY | D_OBJC
413 ObjC++ is like C++ except that D_OBJC is not set
414
415 If -fno-asm is used, D_ASM is added to the mask. If
416 -fno-gnu-keywords is used, D_EXT is added. If -fno-asm and C in
417 C89 mode, D_EXT89 is added for both -fno-asm and -fno-gnu-keywords.
1973201f 418 In C with -Wc++-compat, we warn if D_CXXWARN is set.
eea1139b 419
1973201f
NP
420 Note the complication of the D_CXX_OBJC keywords. These are
421 reserved words such as 'class'. In C++, 'class' is a reserved
422 word. In Objective-C++ it is too. In Objective-C, it is a
423 reserved word too, but only if it follows an '@' sign.
424*/
eea1139b
ILT
425const struct c_common_resword c_common_reswords[] =
426{
d19fa6b5
JM
427 { "_Alignas", RID_ALIGNAS, D_CONLY },
428 { "_Alignof", RID_ALIGNOF, D_CONLY },
267bac10 429 { "_Atomic", RID_ATOMIC, D_CONLY },
eea1139b
ILT
430 { "_Bool", RID_BOOL, D_CONLY },
431 { "_Complex", RID_COMPLEX, 0 },
939b37da
BI
432 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
433 { "_Cilk_sync", RID_CILK_SYNC, 0 },
9a771876 434 { "_Cilk_for", RID_CILK_FOR, 0 },
fa5da7de 435 { "_Imaginary", RID_IMAGINARY, D_CONLY },
eea1139b
ILT
436 { "_Decimal32", RID_DFLOAT32, D_CONLY | D_EXT },
437 { "_Decimal64", RID_DFLOAT64, D_CONLY | D_EXT },
438 { "_Decimal128", RID_DFLOAT128, D_CONLY | D_EXT },
439 { "_Fract", RID_FRACT, D_CONLY | D_EXT },
440 { "_Accum", RID_ACCUM, D_CONLY | D_EXT },
441 { "_Sat", RID_SAT, D_CONLY | D_EXT },
32912286 442 { "_Static_assert", RID_STATIC_ASSERT, D_CONLY },
bbceee64 443 { "_Noreturn", RID_NORETURN, D_CONLY },
433cc7b0 444 { "_Generic", RID_GENERIC, D_CONLY },
582d9b50 445 { "_Thread_local", RID_THREAD, D_CONLY },
eea1139b
ILT
446 { "__FUNCTION__", RID_FUNCTION_NAME, 0 },
447 { "__PRETTY_FUNCTION__", RID_PRETTY_FUNCTION_NAME, 0 },
448 { "__alignof", RID_ALIGNOF, 0 },
449 { "__alignof__", RID_ALIGNOF, 0 },
450 { "__asm", RID_ASM, 0 },
451 { "__asm__", RID_ASM, 0 },
452 { "__attribute", RID_ATTRIBUTE, 0 },
453 { "__attribute__", RID_ATTRIBUTE, 0 },
38b7bc7f 454 { "__auto_type", RID_AUTO_TYPE, D_CONLY },
4daba884 455 { "__bases", RID_BASES, D_CXXONLY },
74893f25
RH
456 { "__builtin_call_with_static_chain",
457 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
eea1139b 458 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
d4a83c10 459 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
9e1a8dd1 460 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
eea1139b
ILT
461 { "__builtin_offsetof", RID_OFFSETOF, 0 },
462 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
463 { "__builtin_va_arg", RID_VA_ARG, 0 },
464 { "__complex", RID_COMPLEX, 0 },
465 { "__complex__", RID_COMPLEX, 0 },
466 { "__const", RID_CONST, 0 },
467 { "__const__", RID_CONST, 0 },
468 { "__decltype", RID_DECLTYPE, D_CXXONLY },
4daba884 469 { "__direct_bases", RID_DIRECT_BASES, D_CXXONLY },
eea1139b
ILT
470 { "__extension__", RID_EXTENSION, 0 },
471 { "__func__", RID_C99_FUNCTION_NAME, 0 },
472 { "__has_nothrow_assign", RID_HAS_NOTHROW_ASSIGN, D_CXXONLY },
473 { "__has_nothrow_constructor", RID_HAS_NOTHROW_CONSTRUCTOR, D_CXXONLY },
474 { "__has_nothrow_copy", RID_HAS_NOTHROW_COPY, D_CXXONLY },
475 { "__has_trivial_assign", RID_HAS_TRIVIAL_ASSIGN, D_CXXONLY },
476 { "__has_trivial_constructor", RID_HAS_TRIVIAL_CONSTRUCTOR, D_CXXONLY },
477 { "__has_trivial_copy", RID_HAS_TRIVIAL_COPY, D_CXXONLY },
478 { "__has_trivial_destructor", RID_HAS_TRIVIAL_DESTRUCTOR, D_CXXONLY },
479 { "__has_virtual_destructor", RID_HAS_VIRTUAL_DESTRUCTOR, D_CXXONLY },
3c0d13bf
PC
480 { "__imag", RID_IMAGPART, 0 },
481 { "__imag__", RID_IMAGPART, 0 },
482 { "__inline", RID_INLINE, 0 },
483 { "__inline__", RID_INLINE, 0 },
eea1139b
ILT
484 { "__is_abstract", RID_IS_ABSTRACT, D_CXXONLY },
485 { "__is_base_of", RID_IS_BASE_OF, D_CXXONLY },
486 { "__is_class", RID_IS_CLASS, D_CXXONLY },
eea1139b
ILT
487 { "__is_empty", RID_IS_EMPTY, D_CXXONLY },
488 { "__is_enum", RID_IS_ENUM, D_CXXONLY },
b3908fcc 489 { "__is_final", RID_IS_FINAL, D_CXXONLY },
3c0d13bf 490 { "__is_literal_type", RID_IS_LITERAL_TYPE, D_CXXONLY },
eea1139b
ILT
491 { "__is_pod", RID_IS_POD, D_CXXONLY },
492 { "__is_polymorphic", RID_IS_POLYMORPHIC, D_CXXONLY },
971e17ff 493 { "__is_same_as", RID_IS_SAME_AS, D_CXXONLY },
c32097d8
JM
494 { "__is_standard_layout", RID_IS_STD_LAYOUT, D_CXXONLY },
495 { "__is_trivial", RID_IS_TRIVIAL, D_CXXONLY },
dd5d5481
JM
496 { "__is_trivially_assignable", RID_IS_TRIVIALLY_ASSIGNABLE, D_CXXONLY },
497 { "__is_trivially_constructible", RID_IS_TRIVIALLY_CONSTRUCTIBLE, D_CXXONLY },
b752325e 498 { "__is_trivially_copyable", RID_IS_TRIVIALLY_COPYABLE, D_CXXONLY },
eea1139b 499 { "__is_union", RID_IS_UNION, D_CXXONLY },
eea1139b
ILT
500 { "__label__", RID_LABEL, 0 },
501 { "__null", RID_NULL, 0 },
502 { "__real", RID_REALPART, 0 },
503 { "__real__", RID_REALPART, 0 },
504 { "__restrict", RID_RESTRICT, 0 },
505 { "__restrict__", RID_RESTRICT, 0 },
506 { "__signed", RID_SIGNED, 0 },
507 { "__signed__", RID_SIGNED, 0 },
508 { "__thread", RID_THREAD, 0 },
0a35513e
AH
509 { "__transaction_atomic", RID_TRANSACTION_ATOMIC, 0 },
510 { "__transaction_relaxed", RID_TRANSACTION_RELAXED, 0 },
511 { "__transaction_cancel", RID_TRANSACTION_CANCEL, 0 },
eea1139b
ILT
512 { "__typeof", RID_TYPEOF, 0 },
513 { "__typeof__", RID_TYPEOF, 0 },
3c0d13bf 514 { "__underlying_type", RID_UNDERLYING_TYPE, D_CXXONLY },
eea1139b
ILT
515 { "__volatile", RID_VOLATILE, 0 },
516 { "__volatile__", RID_VOLATILE, 0 },
36a85135
JM
517 { "alignas", RID_ALIGNAS, D_CXXONLY | D_CXX11 | D_CXXWARN },
518 { "alignof", RID_ALIGNOF, D_CXXONLY | D_CXX11 | D_CXXWARN },
eea1139b
ILT
519 { "asm", RID_ASM, D_ASM },
520 { "auto", RID_AUTO, 0 },
03c3034e 521 { "bool", RID_BOOL, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
522 { "break", RID_BREAK, 0 },
523 { "case", RID_CASE, 0 },
2696a995 524 { "catch", RID_CATCH, D_CXX_OBJC | D_CXXWARN },
eea1139b 525 { "char", RID_CHAR, 0 },
36a85135
JM
526 { "char16_t", RID_CHAR16, D_CXXONLY | D_CXX11 | D_CXXWARN },
527 { "char32_t", RID_CHAR32, D_CXXONLY | D_CXX11 | D_CXXWARN },
2696a995 528 { "class", RID_CLASS, D_CXX_OBJC | D_CXXWARN },
eea1139b 529 { "const", RID_CONST, 0 },
36a85135 530 { "constexpr", RID_CONSTEXPR, D_CXXONLY | D_CXX11 | D_CXXWARN },
eea1139b
ILT
531 { "const_cast", RID_CONSTCAST, D_CXXONLY | D_CXXWARN },
532 { "continue", RID_CONTINUE, 0 },
36a85135 533 { "decltype", RID_DECLTYPE, D_CXXONLY | D_CXX11 | D_CXXWARN },
eea1139b 534 { "default", RID_DEFAULT, 0 },
2696a995 535 { "delete", RID_DELETE, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
536 { "do", RID_DO, 0 },
537 { "double", RID_DOUBLE, 0 },
538 { "dynamic_cast", RID_DYNCAST, D_CXXONLY | D_CXXWARN },
539 { "else", RID_ELSE, 0 },
540 { "enum", RID_ENUM, 0 },
2696a995
KG
541 { "explicit", RID_EXPLICIT, D_CXXONLY | D_CXXWARN },
542 { "export", RID_EXPORT, D_CXXONLY | D_CXXWARN },
eea1139b 543 { "extern", RID_EXTERN, 0 },
2696a995 544 { "false", RID_FALSE, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
545 { "float", RID_FLOAT, 0 },
546 { "for", RID_FOR, 0 },
2696a995 547 { "friend", RID_FRIEND, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
548 { "goto", RID_GOTO, 0 },
549 { "if", RID_IF, 0 },
550 { "inline", RID_INLINE, D_EXT89 },
551 { "int", RID_INT, 0 },
552 { "long", RID_LONG, 0 },
553 { "mutable", RID_MUTABLE, D_CXXONLY | D_CXXWARN },
2696a995
KG
554 { "namespace", RID_NAMESPACE, D_CXXONLY | D_CXXWARN },
555 { "new", RID_NEW, D_CXXONLY | D_CXXWARN },
36a85135
JM
556 { "noexcept", RID_NOEXCEPT, D_CXXONLY | D_CXX11 | D_CXXWARN },
557 { "nullptr", RID_NULLPTR, D_CXXONLY | D_CXX11 | D_CXXWARN },
2696a995
KG
558 { "operator", RID_OPERATOR, D_CXXONLY | D_CXXWARN },
559 { "private", RID_PRIVATE, D_CXX_OBJC | D_CXXWARN },
560 { "protected", RID_PROTECTED, D_CXX_OBJC | D_CXXWARN },
561 { "public", RID_PUBLIC, D_CXX_OBJC | D_CXXWARN },
eea1139b
ILT
562 { "register", RID_REGISTER, 0 },
563 { "reinterpret_cast", RID_REINTCAST, D_CXXONLY | D_CXXWARN },
564 { "restrict", RID_RESTRICT, D_CONLY | D_C99 },
565 { "return", RID_RETURN, 0 },
566 { "short", RID_SHORT, 0 },
567 { "signed", RID_SIGNED, 0 },
568 { "sizeof", RID_SIZEOF, 0 },
569 { "static", RID_STATIC, 0 },
36a85135 570 { "static_assert", RID_STATIC_ASSERT, D_CXXONLY | D_CXX11 | D_CXXWARN },
eea1139b
ILT
571 { "static_cast", RID_STATCAST, D_CXXONLY | D_CXXWARN },
572 { "struct", RID_STRUCT, 0 },
573 { "switch", RID_SWITCH, 0 },
2696a995
KG
574 { "template", RID_TEMPLATE, D_CXXONLY | D_CXXWARN },
575 { "this", RID_THIS, D_CXXONLY | D_CXXWARN },
36a85135 576 { "thread_local", RID_THREAD, D_CXXONLY | D_CXX11 | D_CXXWARN },
2696a995
KG
577 { "throw", RID_THROW, D_CXX_OBJC | D_CXXWARN },
578 { "true", RID_TRUE, D_CXXONLY | D_CXXWARN },
579 { "try", RID_TRY, D_CXX_OBJC | D_CXXWARN },
eea1139b 580 { "typedef", RID_TYPEDEF, 0 },
2696a995
KG
581 { "typename", RID_TYPENAME, D_CXXONLY | D_CXXWARN },
582 { "typeid", RID_TYPEID, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
583 { "typeof", RID_TYPEOF, D_ASM | D_EXT },
584 { "union", RID_UNION, 0 },
585 { "unsigned", RID_UNSIGNED, 0 },
2696a995
KG
586 { "using", RID_USING, D_CXXONLY | D_CXXWARN },
587 { "virtual", RID_VIRTUAL, D_CXXONLY | D_CXXWARN },
eea1139b
ILT
588 { "void", RID_VOID, 0 },
589 { "volatile", RID_VOLATILE, 0 },
590 { "wchar_t", RID_WCHAR, D_CXXONLY },
591 { "while", RID_WHILE, 0 },
971e17ff 592
b8fd7909
JM
593 /* C++ transactional memory. */
594 { "synchronized", RID_SYNCHRONIZED, D_CXX_OBJC | D_TRANSMEM },
595 { "atomic_noexcept", RID_ATOMIC_NOEXCEPT, D_CXXONLY | D_TRANSMEM },
596 { "atomic_cancel", RID_ATOMIC_CANCEL, D_CXXONLY | D_TRANSMEM },
597 { "atomic_commit", RID_TRANSACTION_ATOMIC, D_CXXONLY | D_TRANSMEM },
598
971e17ff
AS
599 /* Concepts-related keywords */
600 { "concept", RID_CONCEPT, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
601 { "requires", RID_REQUIRES, D_CXX_CONCEPTS_FLAGS | D_CXXWARN },
602
eea1139b
ILT
603 /* These Objective-C keywords are recognized only immediately after
604 an '@'. */
605 { "compatibility_alias", RID_AT_ALIAS, D_OBJC },
606 { "defs", RID_AT_DEFS, D_OBJC },
607 { "encode", RID_AT_ENCODE, D_OBJC },
608 { "end", RID_AT_END, D_OBJC },
609 { "implementation", RID_AT_IMPLEMENTATION, D_OBJC },
610 { "interface", RID_AT_INTERFACE, D_OBJC },
611 { "protocol", RID_AT_PROTOCOL, D_OBJC },
612 { "selector", RID_AT_SELECTOR, D_OBJC },
613 { "finally", RID_AT_FINALLY, D_OBJC },
92902b1b
IS
614 { "optional", RID_AT_OPTIONAL, D_OBJC },
615 { "required", RID_AT_REQUIRED, D_OBJC },
668ea4b1 616 { "property", RID_AT_PROPERTY, D_OBJC },
c37d8c30 617 { "package", RID_AT_PACKAGE, D_OBJC },
da57d1b9
NP
618 { "synthesize", RID_AT_SYNTHESIZE, D_OBJC },
619 { "dynamic", RID_AT_DYNAMIC, D_OBJC },
eea1139b
ILT
620 /* These are recognized only in protocol-qualifier context
621 (see above) */
622 { "bycopy", RID_BYCOPY, D_OBJC },
623 { "byref", RID_BYREF, D_OBJC },
624 { "in", RID_IN, D_OBJC },
625 { "inout", RID_INOUT, D_OBJC },
626 { "oneway", RID_ONEWAY, D_OBJC },
627 { "out", RID_OUT, D_OBJC },
668ea4b1 628 /* These are recognized inside a property attribute list */
200290f2
NP
629 { "assign", RID_ASSIGN, D_OBJC },
630 { "copy", RID_COPY, D_OBJC },
668ea4b1 631 { "getter", RID_GETTER, D_OBJC },
200290f2
NP
632 { "nonatomic", RID_NONATOMIC, D_OBJC },
633 { "readonly", RID_READONLY, D_OBJC },
634 { "readwrite", RID_READWRITE, D_OBJC },
635 { "retain", RID_RETAIN, D_OBJC },
668ea4b1 636 { "setter", RID_SETTER, D_OBJC },
eea1139b
ILT
637};
638
639const unsigned int num_c_common_reswords =
640 sizeof c_common_reswords / sizeof (struct c_common_resword);
641
d5e254e1
IE
642/* Table of machine-independent attributes common to all C-like languages.
643
644 All attributes referencing arguments should be additionally processed
645 in chkp_copy_function_type_adding_bounds for correct instrumentation
646 by Pointer Bounds Checker.
647 Current list of processed common attributes: nonnull. */
349ae713
NB
648const struct attribute_spec c_common_attribute_table[] =
649{
62d784f7
KT
650 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
651 affects_type_identity } */
349ae713 652 { "packed", 0, 0, false, false, false,
62d784f7 653 handle_packed_attribute , false},
349ae713 654 { "nocommon", 0, 0, true, false, false,
62d784f7 655 handle_nocommon_attribute, false},
349ae713 656 { "common", 0, 0, true, false, false,
62d784f7 657 handle_common_attribute, false },
349ae713
NB
658 /* FIXME: logically, noreturn attributes should be listed as
659 "false, true, true" and apply to function types. But implementing this
660 would require all the places in the compiler that use TREE_THIS_VOLATILE
661 on a decl to identify non-returning functions to be located and fixed
662 to check the function type instead. */
663 { "noreturn", 0, 0, true, false, false,
62d784f7 664 handle_noreturn_attribute, false },
349ae713 665 { "volatile", 0, 0, true, false, false,
62d784f7 666 handle_noreturn_attribute, false },
5434dc07
MD
667 { "stack_protect", 0, 0, true, false, false,
668 handle_stack_protect_attribute, false },
349ae713 669 { "noinline", 0, 0, true, false, false,
62d784f7 670 handle_noinline_attribute, false },
86631ea3 671 { "noclone", 0, 0, true, false, false,
62d784f7 672 handle_noclone_attribute, false },
185c9e56
ML
673 { "no_icf", 0, 0, true, false, false,
674 handle_noicf_attribute, false },
46a4da10 675 { "leaf", 0, 0, true, false, false,
62d784f7 676 handle_leaf_attribute, false },
349ae713 677 { "always_inline", 0, 0, true, false, false,
62d784f7 678 handle_always_inline_attribute, false },
4eb7fd83 679 { "gnu_inline", 0, 0, true, false, false,
62d784f7 680 handle_gnu_inline_attribute, false },
d752cfdb 681 { "artificial", 0, 0, true, false, false,
62d784f7 682 handle_artificial_attribute, false },
0691d1d4 683 { "flatten", 0, 0, true, false, false,
62d784f7 684 handle_flatten_attribute, false },
349ae713 685 { "used", 0, 0, true, false, false,
62d784f7 686 handle_used_attribute, false },
349ae713 687 { "unused", 0, 0, false, false, false,
62d784f7 688 handle_unused_attribute, false },
ce91e74c 689 { "externally_visible", 0, 0, true, false, false,
62d784f7 690 handle_externally_visible_attribute, false },
7861b648
AK
691 { "no_reorder", 0, 0, true, false, false,
692 handle_no_reorder_attribute, false },
349ae713
NB
693 /* The same comments as for noreturn attributes apply to const ones. */
694 { "const", 0, 0, true, false, false,
62d784f7 695 handle_const_attribute, false },
ee45a32d
EB
696 { "scalar_storage_order", 1, 1, false, false, false,
697 handle_scalar_storage_order_attribute, false },
349ae713 698 { "transparent_union", 0, 0, false, false, false,
62d784f7 699 handle_transparent_union_attribute, false },
fc8600f9 700 { "constructor", 0, 1, true, false, false,
62d784f7 701 handle_constructor_attribute, false },
fc8600f9 702 { "destructor", 0, 1, true, false, false,
62d784f7 703 handle_destructor_attribute, false },
349ae713 704 { "mode", 1, 1, false, true, false,
62d784f7 705 handle_mode_attribute, false },
349ae713 706 { "section", 1, 1, true, false, false,
62d784f7 707 handle_section_attribute, false },
349ae713 708 { "aligned", 0, 1, false, false, false,
62d784f7 709 handle_aligned_attribute, false },
349ae713 710 { "weak", 0, 0, true, false, false,
62d784f7 711 handle_weak_attribute, false },
4bb794e2
ST
712 { "noplt", 0, 0, true, false, false,
713 handle_noplt_attribute, false },
ba885ec5 714 { "ifunc", 1, 1, true, false, false,
62d784f7 715 handle_ifunc_attribute, false },
349ae713 716 { "alias", 1, 1, true, false, false,
62d784f7 717 handle_alias_attribute, false },
a0203ca7 718 { "weakref", 0, 1, true, false, false,
62d784f7 719 handle_weakref_attribute, false },
349ae713 720 { "no_instrument_function", 0, 0, true, false, false,
62d784f7
KT
721 handle_no_instrument_function_attribute,
722 false },
1225d6b1
ML
723 { "no_profile_instrument_function", 0, 0, true, false, false,
724 handle_no_profile_instrument_function_attribute,
725 false },
349ae713 726 { "malloc", 0, 0, true, false, false,
62d784f7 727 handle_malloc_attribute, false },
6e9a3221 728 { "returns_twice", 0, 0, true, false, false,
62d784f7 729 handle_returns_twice_attribute, false },
349ae713 730 { "no_stack_limit", 0, 0, true, false, false,
62d784f7 731 handle_no_limit_stack_attribute, false },
349ae713 732 { "pure", 0, 0, true, false, false,
62d784f7 733 handle_pure_attribute, false },
0a35513e
AH
734 { "transaction_callable", 0, 0, false, true, false,
735 handle_tm_attribute, false },
736 { "transaction_unsafe", 0, 0, false, true, false,
b8fd7909 737 handle_tm_attribute, true },
0a35513e 738 { "transaction_safe", 0, 0, false, true, false,
b8fd7909
JM
739 handle_tm_attribute, true },
740 { "transaction_safe_dynamic", 0, 0, true, false, false,
0a35513e
AH
741 handle_tm_attribute, false },
742 { "transaction_may_cancel_outer", 0, 0, false, true, false,
743 handle_tm_attribute, false },
744 /* ??? These two attributes didn't make the transition from the
745 Intel language document to the multi-vendor language document. */
746 { "transaction_pure", 0, 0, false, true, false,
747 handle_tm_attribute, false },
748 { "transaction_wrap", 1, 1, true, false, false,
749 handle_tm_wrap_attribute, false },
dcd6de6d
ZD
750 /* For internal use (marking of builtins) only. The name contains space
751 to prevent its usage in source code. */
752 { "no vops", 0, 0, true, false, false,
62d784f7 753 handle_novops_attribute, false },
9b86d6bb 754 { "deprecated", 0, 1, false, false, false,
62d784f7 755 handle_deprecated_attribute, false },
349ae713 756 { "vector_size", 1, 1, false, true, false,
f3365c12 757 handle_vector_size_attribute, true },
d7afec4b 758 { "visibility", 1, 1, false, false, false,
62d784f7 759 handle_visibility_attribute, false },
dce81a1a 760 { "tls_model", 1, 1, true, false, false,
62d784f7 761 handle_tls_model_attribute, false },
b34c7881 762 { "nonnull", 0, -1, false, true, true,
62d784f7 763 handle_nonnull_attribute, false },
39f2f3c8 764 { "nothrow", 0, 0, true, false, false,
62d784f7
KT
765 handle_nothrow_attribute, false },
766 { "may_alias", 0, 0, false, true, false, NULL, false },
0bfa5f65 767 { "cleanup", 1, 1, true, false, false,
62d784f7 768 handle_cleanup_attribute, false },
72954a4f 769 { "warn_unused_result", 0, 0, false, true, true,
62d784f7 770 handle_warn_unused_result_attribute, false },
254986c7 771 { "sentinel", 0, 1, false, true, true,
62d784f7 772 handle_sentinel_attribute, false },
b5d32c25
KG
773 /* For internal use (marking of builtins) only. The name contains space
774 to prevent its usage in source code. */
775 { "type generic", 0, 0, false, true, true,
62d784f7 776 handle_type_generic_attribute, false },
51bc54a6 777 { "alloc_size", 1, 2, false, true, true,
62d784f7 778 handle_alloc_size_attribute, false },
52bf96d2 779 { "cold", 0, 0, true, false, false,
62d784f7 780 handle_cold_attribute, false },
52bf96d2 781 { "hot", 0, 0, true, false, false,
62d784f7 782 handle_hot_attribute, false },
77bc5132
JJ
783 { "no_address_safety_analysis",
784 0, 0, true, false, false,
785 handle_no_address_safety_analysis_attribute,
786 false },
e664c61c
KS
787 { "no_sanitize_address", 0, 0, true, false, false,
788 handle_no_sanitize_address_attribute,
789 false },
de35aa66
MS
790 { "no_sanitize_thread", 0, 0, true, false, false,
791 handle_no_sanitize_address_attribute,
792 false },
ce6923c5
MP
793 { "no_sanitize_undefined", 0, 0, true, false, false,
794 handle_no_sanitize_undefined_attribute,
795 false },
d2af6a68 796 { "warning", 1, 1, true, false, false,
62d784f7 797 handle_error_attribute, false },
d2af6a68 798 { "error", 1, 1, true, false, false,
62d784f7 799 handle_error_attribute, false },
5779e713 800 { "target", 1, -1, true, false, false,
62d784f7 801 handle_target_attribute, false },
3b1661a9
ES
802 { "target_clones", 1, -1, true, false, false,
803 handle_target_clones_attribute, false },
ab442df7 804 { "optimize", 1, -1, true, false, false,
62d784f7 805 handle_optimize_attribute, false },
0a35513e
AH
806 /* For internal use only. The leading '*' both prevents its usage in
807 source code and signals that it may be overridden by machine tables. */
808 { "*tm regparm", 0, 0, false, true, true,
809 ignore_attribute, false },
7458026b 810 { "no_split_stack", 0, 0, true, false, false,
62d784f7 811 handle_no_split_stack_attribute, false },
0b7b376d
RG
812 /* For internal use (marking of builtins and runtime functions) only.
813 The name contains space to prevent its usage in source code. */
814 { "fn spec", 1, 1, false, true, true,
62d784f7 815 handle_fnspec_attribute, false },
2a99e5e6
LL
816 { "warn_unused", 0, 0, false, false, false,
817 handle_warn_unused_attribute, false },
826cacfe
MG
818 { "returns_nonnull", 0, 0, false, true, true,
819 handle_returns_nonnull_attribute, false },
acf0174b
JJ
820 { "omp declare simd", 0, -1, true, false, false,
821 handle_omp_declare_simd_attribute, false },
41958c28
BI
822 { "cilk simd function", 0, -1, true, false, false,
823 handle_omp_declare_simd_attribute, false },
e7b69085 824 { "simd", 0, 1, true, false, false,
fff77217 825 handle_simd_attribute, false },
acf0174b
JJ
826 { "omp declare target", 0, 0, true, false, false,
827 handle_omp_declare_target_attribute, false },
4a38b02b
IV
828 { "omp declare target link", 0, 0, true, false, false,
829 handle_omp_declare_target_attribute, false },
8fcbce72
JJ
830 { "alloc_align", 1, 1, false, true, true,
831 handle_alloc_align_attribute, false },
832 { "assume_aligned", 1, 2, false, true, true,
833 handle_assume_aligned_attribute, false },
976d5a22
TT
834 { "designated_init", 0, 0, false, true, false,
835 handle_designated_init_attribute, false },
d5e254e1
IE
836 { "bnd_variable_size", 0, 0, true, false, false,
837 handle_bnd_variable_size_attribute, false },
838 { "bnd_legacy", 0, 0, true, false, false,
839 handle_bnd_legacy, false },
840 { "bnd_instrument", 0, 0, true, false, false,
841 handle_bnd_instrument, false },
62d784f7 842 { NULL, 0, 0, false, false, false, NULL, false }
349ae713
NB
843};
844
845/* Give the specifications for the format attributes, used by C and all
d5e254e1 846 descendants.
349ae713 847
d5e254e1
IE
848 All attributes referencing arguments should be additionally processed
849 in chkp_copy_function_type_adding_bounds for correct instrumentation
850 by Pointer Bounds Checker.
851 Current list of processed format attributes: format, format_arg. */
349ae713
NB
852const struct attribute_spec c_common_format_attribute_table[] =
853{
62d784f7
KT
854 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
855 affects_type_identity } */
349ae713 856 { "format", 3, 3, false, true, true,
62d784f7 857 handle_format_attribute, false },
349ae713 858 { "format_arg", 1, 1, false, true, true,
62d784f7
KT
859 handle_format_arg_attribute, false },
860 { NULL, 0, 0, false, false, false, NULL, false }
349ae713
NB
861};
862
36c5e70a 863/* Return identifier for address space AS. */
3ef0694c 864
36c5e70a
BE
865const char *
866c_addr_space_name (addr_space_t as)
867{
3ef0694c
UW
868 int rid = RID_FIRST_ADDR_SPACE + as;
869 gcc_assert (ridpointers [rid]);
870 return IDENTIFIER_POINTER (ridpointers [rid]);
36c5e70a
BE
871}
872
ec5c56db 873/* Push current bindings for the function name VAR_DECLS. */
7da551a2
RS
874
875void
35b1a6fa 876start_fname_decls (void)
7da551a2 877{
0ba8a114
NS
878 unsigned ix;
879 tree saved = NULL_TREE;
35b1a6fa 880
0ba8a114
NS
881 for (ix = 0; fname_vars[ix].decl; ix++)
882 {
883 tree decl = *fname_vars[ix].decl;
7da551a2 884
0ba8a114
NS
885 if (decl)
886 {
c62c040f
RG
887 saved = tree_cons (decl, build_int_cst (integer_type_node, ix),
888 saved);
0ba8a114
NS
889 *fname_vars[ix].decl = NULL_TREE;
890 }
891 }
892 if (saved || saved_function_name_decls)
893 /* Normally they'll have been NULL, so only push if we've got a
894 stack, or they are non-NULL. */
895 saved_function_name_decls = tree_cons (saved, NULL_TREE,
896 saved_function_name_decls);
897}
898
325c3691
RH
899/* Finish up the current bindings, adding them into the current function's
900 statement tree. This must be done _before_ finish_stmt_tree is called.
901 If there is no current function, we must be at file scope and no statements
902 are involved. Pop the previous bindings. */
0ba8a114
NS
903
904void
35b1a6fa 905finish_fname_decls (void)
0ba8a114
NS
906{
907 unsigned ix;
325c3691 908 tree stmts = NULL_TREE;
0ba8a114
NS
909 tree stack = saved_function_name_decls;
910
911 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
325c3691 912 append_to_statement_list (TREE_VALUE (stack), &stmts);
35b1a6fa 913
325c3691 914 if (stmts)
0ba8a114 915 {
325c3691 916 tree *bodyp = &DECL_SAVED_TREE (current_function_decl);
bfaba7a9 917
325c3691
RH
918 if (TREE_CODE (*bodyp) == BIND_EXPR)
919 bodyp = &BIND_EXPR_BODY (*bodyp);
6cce57b0 920
86ad3aa9 921 append_to_statement_list_force (*bodyp, &stmts);
325c3691 922 *bodyp = stmts;
0ba8a114 923 }
35b1a6fa 924
0ba8a114
NS
925 for (ix = 0; fname_vars[ix].decl; ix++)
926 *fname_vars[ix].decl = NULL_TREE;
35b1a6fa 927
0ba8a114 928 if (stack)
7da551a2 929 {
ec5c56db 930 /* We had saved values, restore them. */
0ba8a114
NS
931 tree saved;
932
933 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
934 {
935 tree decl = TREE_PURPOSE (saved);
936 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
35b1a6fa 937
0ba8a114
NS
938 *fname_vars[ix].decl = decl;
939 }
940 stack = TREE_CHAIN (stack);
7da551a2 941 }
0ba8a114
NS
942 saved_function_name_decls = stack;
943}
944
6cce57b0 945/* Return the text name of the current function, suitably prettified
0d0bc036 946 by PRETTY_P. Return string must be freed by caller. */
0ba8a114
NS
947
948const char *
35b1a6fa 949fname_as_string (int pretty_p)
0ba8a114 950{
47ab33b2 951 const char *name = "top level";
0d0bc036 952 char *namep;
46c2514e
TT
953 int vrb = 2, len;
954 cpp_string cstr = { 0, 0 }, strname;
47ab33b2 955
3f75a254 956 if (!pretty_p)
47ab33b2
MA
957 {
958 name = "";
959 vrb = 0;
960 }
961
962 if (current_function_decl)
ae2bcd98 963 name = lang_hooks.decl_printable_name (current_function_decl, vrb);
47ab33b2 964
46c2514e 965 len = strlen (name) + 3; /* Two for '"'s. One for NULL. */
0d0bc036 966
46c2514e
TT
967 namep = XNEWVEC (char, len);
968 snprintf (namep, len, "\"%s\"", name);
969 strname.text = (unsigned char *) namep;
970 strname.len = len - 1;
0d0bc036 971
b6baa67d 972 if (cpp_interpret_string (parse_in, &strname, 1, &cstr, CPP_STRING))
46c2514e
TT
973 {
974 XDELETEVEC (namep);
975 return (const char *) cstr.text;
0d0bc036 976 }
0d0bc036
AH
977
978 return namep;
0ba8a114
NS
979}
980
0ba8a114
NS
981/* Return the VAR_DECL for a const char array naming the current
982 function. If the VAR_DECL has not yet been created, create it
983 now. RID indicates how it should be formatted and IDENTIFIER_NODE
984 ID is its name (unfortunately C and C++ hold the RID values of
985 keywords in different places, so we can't derive RID from ID in
3ba09659
AH
986 this language independent code. LOC is the location of the
987 function. */
0ba8a114
NS
988
989tree
3ba09659 990fname_decl (location_t loc, unsigned int rid, tree id)
0ba8a114
NS
991{
992 unsigned ix;
993 tree decl = NULL_TREE;
994
995 for (ix = 0; fname_vars[ix].decl; ix++)
996 if (fname_vars[ix].rid == rid)
997 break;
998
999 decl = *fname_vars[ix].decl;
1000 if (!decl)
7da551a2 1001 {
8d3e27d1
DJ
1002 /* If a tree is built here, it would normally have the lineno of
1003 the current statement. Later this tree will be moved to the
1004 beginning of the function and this line number will be wrong.
1005 To avoid this problem set the lineno to 0 here; that prevents
4b7e68e7 1006 it from appearing in the RTL. */
325c3691 1007 tree stmts;
3c20847b 1008 location_t saved_location = input_location;
3c20847b 1009 input_location = UNKNOWN_LOCATION;
35b1a6fa 1010
325c3691 1011 stmts = push_stmt_list ();
c2255bc4 1012 decl = (*make_fname_decl) (loc, id, fname_vars[ix].pretty);
325c3691
RH
1013 stmts = pop_stmt_list (stmts);
1014 if (!IS_EMPTY_STMT (stmts))
1015 saved_function_name_decls
1016 = tree_cons (decl, stmts, saved_function_name_decls);
0ba8a114 1017 *fname_vars[ix].decl = decl;
3c20847b 1018 input_location = saved_location;
7da551a2 1019 }
0ba8a114 1020 if (!ix && !current_function_decl)
3ba09659 1021 pedwarn (loc, 0, "%qD is not defined outside of function scope", decl);
6cce57b0 1022
0ba8a114 1023 return decl;
7da551a2
RS
1024}
1025
b84a3874 1026/* Given a STRING_CST, give it a suitable array-of-chars data type. */
b30f223b
RS
1027
1028tree
35b1a6fa 1029fix_string_type (tree value)
b30f223b 1030{
b84a3874
RH
1031 int length = TREE_STRING_LENGTH (value);
1032 int nchars;
c162c75e
MA
1033 tree e_type, i_type, a_type;
1034
b57062ca 1035 /* Compute the number of elements, for the array type. */
b6baa67d
KVH
1036 if (TREE_TYPE (value) == char_array_type_node || !TREE_TYPE (value))
1037 {
1038 nchars = length;
1039 e_type = char_type_node;
1040 }
1041 else if (TREE_TYPE (value) == char16_array_type_node)
1042 {
1043 nchars = length / (TYPE_PRECISION (char16_type_node) / BITS_PER_UNIT);
1044 e_type = char16_type_node;
1045 }
1046 else if (TREE_TYPE (value) == char32_array_type_node)
1047 {
1048 nchars = length / (TYPE_PRECISION (char32_type_node) / BITS_PER_UNIT);
1049 e_type = char32_type_node;
1050 }
1051 else
1052 {
1053 nchars = length / (TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT);
1054 e_type = wchar_type_node;
1055 }
b30f223b 1056
89a42ac8
ZW
1057 /* C89 2.2.4.1, C99 5.2.4.1 (Translation limits). The analogous
1058 limit in C++98 Annex B is very large (65536) and is not normative,
1059 so we do not diagnose it (warn_overlength_strings is forced off
1060 in c_common_post_options). */
1061 if (warn_overlength_strings)
1062 {
1063 const int nchars_max = flag_isoc99 ? 4095 : 509;
1064 const int relevant_std = flag_isoc99 ? 99 : 90;
1065 if (nchars - 1 > nchars_max)
1066 /* Translators: The %d after 'ISO C' will be 90 or 99. Do not
1067 separate the %d from the 'C'. 'ISO' should not be
1068 translated, but it may be moved after 'C%d' in languages
1069 where modifiers follow nouns. */
509c9d60 1070 pedwarn (input_location, OPT_Woverlength_strings,
fcf73884 1071 "string length %qd is greater than the length %qd "
89a42ac8
ZW
1072 "ISO C%d compilers are required to support",
1073 nchars - 1, nchars_max, relevant_std);
1074 }
1326a48b 1075
cfb10bd3
GDR
1076 /* Create the array type for the string constant. The ISO C++
1077 standard says that a string literal has type `const char[N]' or
1078 `const wchar_t[N]'. We use the same logic when invoked as a C
1079 front-end with -Wwrite-strings.
1080 ??? We should change the type of an expression depending on the
1081 state of a warning flag. We should just be warning -- see how
1082 this is handled in the C++ front-end for the deprecated implicit
1083 conversion from string literals to `char*' or `wchar_t*'.
c162c75e
MA
1084
1085 The C++ front end relies on TYPE_MAIN_VARIANT of a cv-qualified
1086 array type being the unqualified version of that type.
1087 Therefore, if we are constructing an array of const char, we must
1088 construct the matching unqualified array type first. The C front
1089 end does not require this, but it does no harm, so we do it
1090 unconditionally. */
c62c040f 1091 i_type = build_index_type (size_int (nchars - 1));
c162c75e 1092 a_type = build_array_type (e_type, i_type);
cfb10bd3 1093 if (c_dialect_cxx() || warn_write_strings)
46df2823 1094 a_type = c_build_qualified_type (a_type, TYPE_QUAL_CONST);
d9cf7c82 1095
c162c75e 1096 TREE_TYPE (value) = a_type;
ccd4c832 1097 TREE_CONSTANT (value) = 1;
3521b33c 1098 TREE_READONLY (value) = 1;
b30f223b
RS
1099 TREE_STATIC (value) = 1;
1100 return value;
1101}
88fa5555
DM
1102
1103/* Given a string of type STRING_TYPE, determine what kind of string
1104 token would give an equivalent execution encoding: CPP_STRING,
1105 CPP_STRING16, or CPP_STRING32. Return CPP_OTHER in case of error.
1106 This may not be exactly the string token type that initially created
1107 the string, since CPP_WSTRING is indistinguishable from the 16/32 bit
1108 string type at this point.
1109
1110 This effectively reverses part of the logic in lex_string and
1111 fix_string_type. */
1112
1113static enum cpp_ttype
1114get_cpp_ttype_from_string_type (tree string_type)
1115{
1116 gcc_assert (string_type);
1117 if (TREE_CODE (string_type) != ARRAY_TYPE)
1118 return CPP_OTHER;
1119
1120 tree element_type = TREE_TYPE (string_type);
1121 if (TREE_CODE (element_type) != INTEGER_TYPE)
1122 return CPP_OTHER;
1123
1124 int bits_per_character = TYPE_PRECISION (element_type);
1125 switch (bits_per_character)
1126 {
1127 case 8:
1128 return CPP_STRING; /* It could have also been CPP_UTF8STRING. */
1129 case 16:
1130 return CPP_STRING16;
1131 case 32:
1132 return CPP_STRING32;
1133 }
1134
1135 return CPP_OTHER;
1136}
1137
1138/* The global record of string concatentations, for use in
1139 extracting locations within string literals. */
1140
1141GTY(()) string_concat_db *g_string_concat_db;
1142
1143/* Attempt to determine the source range of the substring.
1144 If successful, return NULL and write the source range to *OUT_RANGE.
1145 Otherwise return an error message. Error messages are intended
1146 for GCC developers (to help debugging) rather than for end-users. */
1147
1148const char *
1149substring_loc::get_range (source_range *out_range) const
1150{
1151 gcc_assert (out_range);
1152
1153 enum cpp_ttype tok_type = get_cpp_ttype_from_string_type (m_string_type);
1154 if (tok_type == CPP_OTHER)
1155 return "unrecognized string type";
1156
1157 return get_source_range_for_substring (parse_in, g_string_concat_db,
1158 m_fmt_string_loc, tok_type,
1159 m_start_idx, m_end_idx,
1160 out_range);
1161}
1162
b30f223b 1163\f
f479b43d
JM
1164/* Fold X for consideration by one of the warning functions when checking
1165 whether an expression has a constant value. */
1166
1167static tree
1168fold_for_warn (tree x)
1169{
1170 if (c_dialect_cxx ())
1171 return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
1172 else
1173 /* The C front-end has already folded X appropriately. */
1174 return x;
1175}
1176
d74154d5
RS
1177/* Print a warning if a constant expression had overflow in folding.
1178 Invoke this function on every expression that the language
1179 requires to be a constant expression.
1180 Note the ANSI C standard says it is erroneous for a
1181 constant expression to overflow. */
96571883
BK
1182
1183void
35b1a6fa 1184constant_expression_warning (tree value)
393eda6a 1185{
b8698a0f 1186 if (warn_overflow && pedantic
393eda6a
MLI
1187 && (TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1188 || TREE_CODE (value) == FIXED_CST
1189 || TREE_CODE (value) == VECTOR_CST
1190 || TREE_CODE (value) == COMPLEX_CST)
1191 && TREE_OVERFLOW (value))
509c9d60 1192 pedwarn (input_location, OPT_Woverflow, "overflow in constant expression");
393eda6a
MLI
1193}
1194
1195/* The same as above but print an unconditional error. */
1196void
1197constant_expression_error (tree value)
96571883 1198{
c05f751c 1199 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
ab22c1fa 1200 || TREE_CODE (value) == FIXED_CST
69ef87e2 1201 || TREE_CODE (value) == VECTOR_CST
c05f751c 1202 || TREE_CODE (value) == COMPLEX_CST)
393eda6a
MLI
1203 && TREE_OVERFLOW (value))
1204 error ("overflow in constant expression");
d74154d5
RS
1205}
1206
59c0753d
MLI
1207/* Print a warning if an expression had overflow in folding and its
1208 operands hadn't.
1209
d74154d5
RS
1210 Invoke this function on every expression that
1211 (1) appears in the source code, and
59c0753d 1212 (2) is a constant expression that overflowed, and
d74154d5 1213 (3) is not already checked by convert_and_check;
59c0753d
MLI
1214 however, do not invoke this function on operands of explicit casts
1215 or when the expression is the result of an operator and any operand
1216 already overflowed. */
d74154d5
RS
1217
1218void
c2255bc4 1219overflow_warning (location_t loc, tree value)
d74154d5 1220{
7d882b83
ILT
1221 if (c_inhibit_evaluation_warnings != 0)
1222 return;
59c0753d
MLI
1223
1224 switch (TREE_CODE (value))
69ef87e2 1225 {
59c0753d 1226 case INTEGER_CST:
c2255bc4 1227 warning_at (loc, OPT_Woverflow, "integer overflow in expression");
59c0753d 1228 break;
b8698a0f 1229
59c0753d 1230 case REAL_CST:
c2255bc4
AH
1231 warning_at (loc, OPT_Woverflow,
1232 "floating point overflow in expression");
59c0753d 1233 break;
b8698a0f 1234
ab22c1fa 1235 case FIXED_CST:
c2255bc4 1236 warning_at (loc, OPT_Woverflow, "fixed-point overflow in expression");
ab22c1fa
CF
1237 break;
1238
59c0753d 1239 case VECTOR_CST:
c2255bc4 1240 warning_at (loc, OPT_Woverflow, "vector overflow in expression");
59c0753d 1241 break;
b8698a0f 1242
59c0753d
MLI
1243 case COMPLEX_CST:
1244 if (TREE_CODE (TREE_REALPART (value)) == INTEGER_CST)
c2255bc4
AH
1245 warning_at (loc, OPT_Woverflow,
1246 "complex integer overflow in expression");
59c0753d 1247 else if (TREE_CODE (TREE_REALPART (value)) == REAL_CST)
c2255bc4
AH
1248 warning_at (loc, OPT_Woverflow,
1249 "complex floating point overflow in expression");
59c0753d
MLI
1250 break;
1251
1252 default:
1253 break;
69ef87e2 1254 }
d74154d5
RS
1255}
1256
ca409efd
MLI
1257/* Warn about uses of logical || / && operator in a context where it
1258 is likely that the bitwise equivalent was intended by the
1259 programmer. We have seen an expression in which CODE is a binary
a243fb4a
MLI
1260 operator used to combine expressions OP_LEFT and OP_RIGHT, which before folding
1261 had CODE_LEFT and CODE_RIGHT, into an expression of type TYPE. */
63a08740 1262void
a243fb4a 1263warn_logical_operator (location_t location, enum tree_code code, tree type,
b8698a0f 1264 enum tree_code code_left, tree op_left,
ca409efd 1265 enum tree_code ARG_UNUSED (code_right), tree op_right)
63a08740 1266{
a243fb4a
MLI
1267 int or_op = (code == TRUTH_ORIF_EXPR || code == TRUTH_OR_EXPR);
1268 int in0_p, in1_p, in_p;
1269 tree low0, low1, low, high0, high1, high, lhs, rhs, tem;
1270 bool strict_overflow_p = false;
1271
ca409efd
MLI
1272 if (code != TRUTH_ANDIF_EXPR
1273 && code != TRUTH_AND_EXPR
1274 && code != TRUTH_ORIF_EXPR
1275 && code != TRUTH_OR_EXPR)
1276 return;
1277
b8787813
MP
1278 /* We don't want to warn if either operand comes from a macro
1279 expansion. ??? This doesn't work with e.g. NEGATE_EXPR yet;
1280 see PR61534. */
1281 if (from_macro_expansion_at (EXPR_LOCATION (op_left))
1282 || from_macro_expansion_at (EXPR_LOCATION (op_right)))
1283 return;
1284
ca409efd
MLI
1285 /* Warn if &&/|| are being used in a context where it is
1286 likely that the bitwise equivalent was intended by the
1287 programmer. That is, an expression such as op && MASK
1288 where op should not be any boolean expression, nor a
1289 constant, and mask seems to be a non-boolean integer constant. */
f479b43d
JM
1290 if (TREE_CODE (op_right) == CONST_DECL)
1291 /* An enumerator counts as a constant. */
1292 op_right = DECL_INITIAL (op_right);
ca409efd
MLI
1293 if (!truth_value_p (code_left)
1294 && INTEGRAL_TYPE_P (TREE_TYPE (op_left))
1295 && !CONSTANT_CLASS_P (op_left)
1296 && !TREE_NO_WARNING (op_left)
1297 && TREE_CODE (op_right) == INTEGER_CST
1298 && !integer_zerop (op_right)
1299 && !integer_onep (op_right))
63a08740 1300 {
a243fb4a 1301 if (or_op)
ca409efd
MLI
1302 warning_at (location, OPT_Wlogical_op, "logical %<or%>"
1303 " applied to non-boolean constant");
1304 else
1305 warning_at (location, OPT_Wlogical_op, "logical %<and%>"
1306 " applied to non-boolean constant");
1307 TREE_NO_WARNING (op_left) = true;
a243fb4a
MLI
1308 return;
1309 }
1310
1311 /* We do not warn for constants because they are typical of macro
1312 expansions that test for features. */
f479b43d
JM
1313 if (CONSTANT_CLASS_P (fold_for_warn (op_left))
1314 || CONSTANT_CLASS_P (fold_for_warn (op_right)))
a243fb4a
MLI
1315 return;
1316
1317 /* This warning only makes sense with logical operands. */
1318 if (!(truth_value_p (TREE_CODE (op_left))
1319 || INTEGRAL_TYPE_P (TREE_TYPE (op_left)))
1320 || !(truth_value_p (TREE_CODE (op_right))
1321 || INTEGRAL_TYPE_P (TREE_TYPE (op_right))))
1322 return;
1323
3c9aabbd
MG
1324 /* The range computations only work with scalars. */
1325 if (VECTOR_TYPE_P (TREE_TYPE (op_left))
1326 || VECTOR_TYPE_P (TREE_TYPE (op_right)))
1327 return;
a243fb4a 1328
f2c4a785
MLI
1329 /* We first test whether either side separately is trivially true
1330 (with OR) or trivially false (with AND). If so, do not warn.
1331 This is a common idiom for testing ranges of data types in
1332 portable code. */
1333 lhs = make_range (op_left, &in0_p, &low0, &high0, &strict_overflow_p);
1334 if (!lhs)
1335 return;
1336 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
a243fb4a
MLI
1337 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
1338
f2c4a785
MLI
1339 /* If this is an OR operation, invert both sides; now, the result
1340 should be always false to get a warning. */
1341 if (or_op)
1342 in0_p = !in0_p;
1343
1344 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in0_p, low0, high0);
1e537948 1345 if (tem && integer_zerop (tem))
f2c4a785
MLI
1346 return;
1347
1348 rhs = make_range (op_right, &in1_p, &low1, &high1, &strict_overflow_p);
1349 if (!rhs)
1350 return;
1351 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
a243fb4a 1352 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
b8698a0f 1353
f2c4a785
MLI
1354 /* If this is an OR operation, invert both sides; now, the result
1355 should be always false to get a warning. */
a243fb4a 1356 if (or_op)
f2c4a785 1357 in1_p = !in1_p;
b8698a0f 1358
f2c4a785 1359 tem = build_range_check (UNKNOWN_LOCATION, type, rhs, in1_p, low1, high1);
1e537948 1360 if (tem && integer_zerop (tem))
f2c4a785
MLI
1361 return;
1362
1363 /* If both expressions have the same operand, if we can merge the
8c2b7f79 1364 ranges, ... */
f2c4a785 1365 if (operand_equal_p (lhs, rhs, 0)
a243fb4a 1366 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
8c2b7f79 1367 in1_p, low1, high1))
a243fb4a 1368 {
8c2b7f79
MP
1369 tem = build_range_check (UNKNOWN_LOCATION, type, lhs, in_p, low, high);
1370 /* ... and if the range test is always false, then warn. */
1371 if (tem && integer_zerop (tem))
1372 {
1373 if (or_op)
1374 warning_at (location, OPT_Wlogical_op,
1375 "logical %<or%> of collectively exhaustive tests is "
1376 "always true");
1377 else
1378 warning_at (location, OPT_Wlogical_op,
1379 "logical %<and%> of mutually exclusive tests is "
1380 "always false");
1381 }
1382 /* Or warn if the operands have exactly the same range, e.g.
1383 A > 0 && A > 0. */
a68ae2e1
MP
1384 else if (tree_int_cst_equal (low0, low1)
1385 && tree_int_cst_equal (high0, high1))
8c2b7f79
MP
1386 {
1387 if (or_op)
1388 warning_at (location, OPT_Wlogical_op,
1389 "logical %<or%> of equal expressions");
1390 else
1391 warning_at (location, OPT_Wlogical_op,
1392 "logical %<and%> of equal expressions");
1393 }
63a08740
DM
1394 }
1395}
1396
05b28fd6
MP
1397/* Helper function for warn_tautological_cmp. Look for ARRAY_REFs
1398 with constant indices. */
1399
1400static tree
1401find_array_ref_with_const_idx_r (tree *expr_p, int *walk_subtrees, void *data)
1402{
1403 tree expr = *expr_p;
1404
1405 if ((TREE_CODE (expr) == ARRAY_REF
1406 || TREE_CODE (expr) == ARRAY_RANGE_REF)
1407 && TREE_CODE (TREE_OPERAND (expr, 1)) == INTEGER_CST)
1408 {
1409 *(bool *) data = true;
1410 *walk_subtrees = 0;
1411 }
1412
1413 return NULL_TREE;
1414}
1415
1416/* Warn if a self-comparison always evaluates to true or false. LOC
1417 is the location of the comparison with code CODE, LHS and RHS are
1418 operands of the comparison. */
1419
1420void
1421warn_tautological_cmp (location_t loc, enum tree_code code, tree lhs, tree rhs)
1422{
1423 if (TREE_CODE_CLASS (code) != tcc_comparison)
1424 return;
1425
f2afe6dd
MP
1426 /* Don't warn for various macro expansions. */
1427 if (from_macro_expansion_at (loc)
1428 || from_macro_expansion_at (EXPR_LOCATION (lhs))
1429 || from_macro_expansion_at (EXPR_LOCATION (rhs)))
1430 return;
1431
05b28fd6
MP
1432 /* We do not warn for constants because they are typical of macro
1433 expansions that test for features, sizeof, and similar. */
f479b43d
JM
1434 if (CONSTANT_CLASS_P (fold_for_warn (lhs))
1435 || CONSTANT_CLASS_P (fold_for_warn (rhs)))
05b28fd6
MP
1436 return;
1437
1438 /* Don't warn for e.g.
1439 HOST_WIDE_INT n;
1440 ...
1441 if (n == (long) n) ...
1442 */
1443 if ((CONVERT_EXPR_P (lhs) || TREE_CODE (lhs) == NON_LVALUE_EXPR)
1444 || (CONVERT_EXPR_P (rhs) || TREE_CODE (rhs) == NON_LVALUE_EXPR))
1445 return;
1446
173864e8
MP
1447 /* Don't warn if either LHS or RHS has an IEEE floating-point type.
1448 It could be a NaN, and NaN never compares equal to anything, even
1449 itself. */
1450 if (FLOAT_TYPE_P (TREE_TYPE (lhs)) || FLOAT_TYPE_P (TREE_TYPE (rhs)))
1451 return;
1452
05b28fd6
MP
1453 if (operand_equal_p (lhs, rhs, 0))
1454 {
1455 /* Don't warn about array references with constant indices;
1456 these are likely to come from a macro. */
1457 bool found = false;
1458 walk_tree_without_duplicates (&lhs, find_array_ref_with_const_idx_r,
1459 &found);
1460 if (found)
1461 return;
1462 const bool always_true = (code == EQ_EXPR || code == LE_EXPR
1463 || code == GE_EXPR || code == UNLE_EXPR
1464 || code == UNGE_EXPR || code == UNEQ_EXPR);
1465 if (always_true)
1466 warning_at (loc, OPT_Wtautological_compare,
1467 "self-comparison always evaluates to true");
1468 else
1469 warning_at (loc, OPT_Wtautological_compare,
1470 "self-comparison always evaluates to false");
1471 }
1472}
1473
742938c9
MP
1474/* Warn about logical not used on the left hand side operand of a comparison.
1475 This function assumes that the LHS is inside of TRUTH_NOT_EXPR.
59ea0364 1476 Do not warn if RHS is of a boolean type. */
742938c9
MP
1477
1478void
1479warn_logical_not_parentheses (location_t location, enum tree_code code,
59ea0364 1480 tree rhs)
742938c9 1481{
59ea0364
MP
1482 if (TREE_CODE_CLASS (code) != tcc_comparison
1483 || TREE_TYPE (rhs) == NULL_TREE
1484 || TREE_CODE (TREE_TYPE (rhs)) == BOOLEAN_TYPE)
742938c9
MP
1485 return;
1486
7ccb1a11
JJ
1487 /* Don't warn for !x == 0 or !y != 0, those are equivalent to
1488 !(x == 0) or !(y != 0). */
1489 if ((code == EQ_EXPR || code == NE_EXPR)
1490 && integer_zerop (rhs))
1491 return;
1492
742938c9
MP
1493 warning_at (location, OPT_Wlogical_not_parentheses,
1494 "logical not is only applied to the left hand side of "
1495 "comparison");
1496}
63a08740 1497
fd4116f4
MLI
1498/* Warn if EXP contains any computations whose results are not used.
1499 Return true if a warning is printed; false otherwise. LOCUS is the
1500 (potential) location of the expression. */
1501
1502bool
1503warn_if_unused_value (const_tree exp, location_t locus)
1504{
1505 restart:
1506 if (TREE_USED (exp) || TREE_NO_WARNING (exp))
1507 return false;
1508
1509 /* Don't warn about void constructs. This includes casting to void,
1510 void function calls, and statement expressions with a final cast
1511 to void. */
1512 if (VOID_TYPE_P (TREE_TYPE (exp)))
1513 return false;
1514
1515 if (EXPR_HAS_LOCATION (exp))
1516 locus = EXPR_LOCATION (exp);
1517
1518 switch (TREE_CODE (exp))
1519 {
1520 case PREINCREMENT_EXPR:
1521 case POSTINCREMENT_EXPR:
1522 case PREDECREMENT_EXPR:
1523 case POSTDECREMENT_EXPR:
1524 case MODIFY_EXPR:
1525 case INIT_EXPR:
1526 case TARGET_EXPR:
1527 case CALL_EXPR:
1528 case TRY_CATCH_EXPR:
1529 case WITH_CLEANUP_EXPR:
1530 case EXIT_EXPR:
1531 case VA_ARG_EXPR:
1532 return false;
1533
1534 case BIND_EXPR:
1535 /* For a binding, warn if no side effect within it. */
1536 exp = BIND_EXPR_BODY (exp);
1537 goto restart;
1538
1539 case SAVE_EXPR:
1540 case NON_LVALUE_EXPR:
007a787d 1541 case NOP_EXPR:
fd4116f4
MLI
1542 exp = TREE_OPERAND (exp, 0);
1543 goto restart;
1544
1545 case TRUTH_ORIF_EXPR:
1546 case TRUTH_ANDIF_EXPR:
1547 /* In && or ||, warn if 2nd operand has no side effect. */
1548 exp = TREE_OPERAND (exp, 1);
1549 goto restart;
1550
1551 case COMPOUND_EXPR:
1552 if (warn_if_unused_value (TREE_OPERAND (exp, 0), locus))
1553 return true;
1554 /* Let people do `(foo (), 0)' without a warning. */
1555 if (TREE_CONSTANT (TREE_OPERAND (exp, 1)))
1556 return false;
1557 exp = TREE_OPERAND (exp, 1);
1558 goto restart;
1559
1560 case COND_EXPR:
1561 /* If this is an expression with side effects, don't warn; this
1562 case commonly appears in macro expansions. */
1563 if (TREE_SIDE_EFFECTS (exp))
1564 return false;
1565 goto warn;
1566
1567 case INDIRECT_REF:
1568 /* Don't warn about automatic dereferencing of references, since
1569 the user cannot control it. */
1570 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
1571 {
1572 exp = TREE_OPERAND (exp, 0);
1573 goto restart;
1574 }
1575 /* Fall through. */
1576
1577 default:
1578 /* Referencing a volatile value is a side effect, so don't warn. */
1579 if ((DECL_P (exp) || REFERENCE_CLASS_P (exp))
1580 && TREE_THIS_VOLATILE (exp))
1581 return false;
1582
1583 /* If this is an expression which has no operands, there is no value
1584 to be unused. There are no such language-independent codes,
1585 but front ends may define such. */
1586 if (EXPRESSION_CLASS_P (exp) && TREE_OPERAND_LENGTH (exp) == 0)
1587 return false;
1588
1589 warn:
1590 return warning_at (locus, OPT_Wunused_value, "value computed is not used");
1591 }
1592}
1593
1594
de9c56a4
RG
1595/* Print a warning about casts that might indicate violation
1596 of strict aliasing rules if -Wstrict-aliasing is used and
3f0a2a47
DM
1597 strict aliasing mode is in effect. OTYPE is the original
1598 TREE_TYPE of EXPR, and TYPE the type we're casting to. */
de9c56a4 1599
79bedddc 1600bool
3f0a2a47 1601strict_aliasing_warning (tree otype, tree type, tree expr)
de9c56a4 1602{
255d3827
RG
1603 /* Strip pointer conversion chains and get to the correct original type. */
1604 STRIP_NOPS (expr);
1605 otype = TREE_TYPE (expr);
1606
ac7ee6ad
RG
1607 if (!(flag_strict_aliasing
1608 && POINTER_TYPE_P (type)
1609 && POINTER_TYPE_P (otype)
1610 && !VOID_TYPE_P (TREE_TYPE (type)))
1611 /* If the type we are casting to is a ref-all pointer
1612 dereferencing it is always valid. */
1613 || TYPE_REF_CAN_ALIAS_ALL (type))
79bedddc
SR
1614 return false;
1615
1616 if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR
de9c56a4 1617 && (DECL_P (TREE_OPERAND (expr, 0))
79bedddc 1618 || handled_component_p (TREE_OPERAND (expr, 0))))
de9c56a4
RG
1619 {
1620 /* Casting the address of an object to non void pointer. Warn
1621 if the cast breaks type based aliasing. */
79bedddc
SR
1622 if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2)
1623 {
1624 warning (OPT_Wstrict_aliasing, "type-punning to incomplete type "
1625 "might break strict-aliasing rules");
1626 return true;
1627 }
de9c56a4
RG
1628 else
1629 {
b8698a0f 1630 /* warn_strict_aliasing >= 3. This includes the default (3).
79bedddc 1631 Only warn if the cast is dereferenced immediately. */
4862826d 1632 alias_set_type set1 =
79bedddc 1633 get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0)));
4862826d 1634 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
de9c56a4 1635
4653cae5 1636 if (set1 != set2 && set2 != 0
c06d25bb
RB
1637 && (set1 == 0
1638 || (!alias_set_subset_of (set2, set1)
1639 && !alias_sets_conflict_p (set1, set2))))
79bedddc
SR
1640 {
1641 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1642 "pointer will break strict-aliasing rules");
1643 return true;
1644 }
1645 else if (warn_strict_aliasing == 2
836f7794 1646 && !alias_sets_must_conflict_p (set1, set2))
79bedddc
SR
1647 {
1648 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1649 "pointer might break strict-aliasing rules");
1650 return true;
1651 }
de9c56a4
RG
1652 }
1653 }
79bedddc
SR
1654 else
1655 if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype)))
1656 {
1657 /* At this level, warn for any conversions, even if an address is
1658 not taken in the same statement. This will likely produce many
1659 false positives, but could be useful to pinpoint problems that
1660 are not revealed at higher levels. */
4862826d
ILT
1661 alias_set_type set1 = get_alias_set (TREE_TYPE (otype));
1662 alias_set_type set2 = get_alias_set (TREE_TYPE (type));
1663 if (!COMPLETE_TYPE_P (type)
836f7794 1664 || !alias_sets_must_conflict_p (set1, set2))
79bedddc
SR
1665 {
1666 warning (OPT_Wstrict_aliasing, "dereferencing type-punned "
1667 "pointer might break strict-aliasing rules");
1668 return true;
1669 }
1670 }
1671
1672 return false;
de9c56a4
RG
1673}
1674
1a4049e7
JJ
1675/* Warn about memset (&a, 0, sizeof (&a)); and similar mistakes with
1676 sizeof as last operand of certain builtins. */
1677
1678void
3a785c97 1679sizeof_pointer_memaccess_warning (location_t *sizeof_arg_loc, tree callee,
9771b263 1680 vec<tree, va_gc> *params, tree *sizeof_arg,
1a4049e7
JJ
1681 bool (*comp_types) (tree, tree))
1682{
1683 tree type, dest = NULL_TREE, src = NULL_TREE, tem;
3a785c97
JJ
1684 bool strop = false, cmp = false;
1685 unsigned int idx = ~0;
1686 location_t loc;
1a4049e7
JJ
1687
1688 if (TREE_CODE (callee) != FUNCTION_DECL
1689 || DECL_BUILT_IN_CLASS (callee) != BUILT_IN_NORMAL
9771b263 1690 || vec_safe_length (params) <= 1)
1a4049e7
JJ
1691 return;
1692
1a4049e7
JJ
1693 switch (DECL_FUNCTION_CODE (callee))
1694 {
1695 case BUILT_IN_STRNCMP:
1696 case BUILT_IN_STRNCASECMP:
3a785c97
JJ
1697 cmp = true;
1698 /* FALLTHRU */
1a4049e7 1699 case BUILT_IN_STRNCPY:
3a785c97 1700 case BUILT_IN_STRNCPY_CHK:
1a4049e7 1701 case BUILT_IN_STRNCAT:
3a785c97
JJ
1702 case BUILT_IN_STRNCAT_CHK:
1703 case BUILT_IN_STPNCPY:
1704 case BUILT_IN_STPNCPY_CHK:
1a4049e7
JJ
1705 strop = true;
1706 /* FALLTHRU */
1707 case BUILT_IN_MEMCPY:
3a785c97 1708 case BUILT_IN_MEMCPY_CHK:
1a4049e7 1709 case BUILT_IN_MEMMOVE:
3a785c97 1710 case BUILT_IN_MEMMOVE_CHK:
9771b263 1711 if (params->length () < 3)
3a785c97 1712 return;
9771b263
DN
1713 src = (*params)[1];
1714 dest = (*params)[0];
3a785c97
JJ
1715 idx = 2;
1716 break;
1717 case BUILT_IN_BCOPY:
9771b263 1718 if (params->length () < 3)
3a785c97 1719 return;
9771b263
DN
1720 src = (*params)[0];
1721 dest = (*params)[1];
3a785c97
JJ
1722 idx = 2;
1723 break;
1a4049e7 1724 case BUILT_IN_MEMCMP:
3a785c97 1725 case BUILT_IN_BCMP:
9771b263 1726 if (params->length () < 3)
1a4049e7 1727 return;
9771b263
DN
1728 src = (*params)[1];
1729 dest = (*params)[0];
3a785c97
JJ
1730 idx = 2;
1731 cmp = true;
1a4049e7
JJ
1732 break;
1733 case BUILT_IN_MEMSET:
3a785c97 1734 case BUILT_IN_MEMSET_CHK:
9771b263 1735 if (params->length () < 3)
1a4049e7 1736 return;
9771b263 1737 dest = (*params)[0];
3a785c97
JJ
1738 idx = 2;
1739 break;
1740 case BUILT_IN_BZERO:
9771b263 1741 dest = (*params)[0];
3a785c97 1742 idx = 1;
1a4049e7
JJ
1743 break;
1744 case BUILT_IN_STRNDUP:
9771b263 1745 src = (*params)[0];
1a4049e7 1746 strop = true;
3a785c97
JJ
1747 idx = 1;
1748 break;
1749 case BUILT_IN_MEMCHR:
9771b263 1750 if (params->length () < 3)
3a785c97 1751 return;
9771b263 1752 src = (*params)[0];
3a785c97
JJ
1753 idx = 2;
1754 break;
1755 case BUILT_IN_SNPRINTF:
1756 case BUILT_IN_SNPRINTF_CHK:
1757 case BUILT_IN_VSNPRINTF:
1758 case BUILT_IN_VSNPRINTF_CHK:
9771b263 1759 dest = (*params)[0];
3a785c97
JJ
1760 idx = 1;
1761 strop = true;
1a4049e7
JJ
1762 break;
1763 default:
1764 break;
1765 }
1766
3a785c97
JJ
1767 if (idx >= 3)
1768 return;
1769
1770 if (sizeof_arg[idx] == NULL || sizeof_arg[idx] == error_mark_node)
1771 return;
1772
1773 type = TYPE_P (sizeof_arg[idx])
1774 ? sizeof_arg[idx] : TREE_TYPE (sizeof_arg[idx]);
1775 if (!POINTER_TYPE_P (type))
1776 return;
1777
1a4049e7
JJ
1778 if (dest
1779 && (tem = tree_strip_nop_conversions (dest))
1780 && POINTER_TYPE_P (TREE_TYPE (tem))
1781 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1782 return;
1783
1784 if (src
1785 && (tem = tree_strip_nop_conversions (src))
1786 && POINTER_TYPE_P (TREE_TYPE (tem))
1787 && comp_types (TREE_TYPE (TREE_TYPE (tem)), type))
1788 return;
1789
3a785c97
JJ
1790 loc = sizeof_arg_loc[idx];
1791
1792 if (dest && !cmp)
1a4049e7 1793 {
3a785c97
JJ
1794 if (!TYPE_P (sizeof_arg[idx])
1795 && operand_equal_p (dest, sizeof_arg[idx], 0)
1a4049e7
JJ
1796 && comp_types (TREE_TYPE (dest), type))
1797 {
3a785c97 1798 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1a4049e7
JJ
1799 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1800 "argument to %<sizeof%> in %qD call is the same "
1801 "expression as the destination; did you mean to "
1802 "remove the addressof?", callee);
1803 else if ((TYPE_PRECISION (TREE_TYPE (type))
1804 == TYPE_PRECISION (char_type_node))
1805 || strop)
1806 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1807 "argument to %<sizeof%> in %qD call is the same "
1808 "expression as the destination; did you mean to "
1809 "provide an explicit length?", callee);
1810 else
1811 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1812 "argument to %<sizeof%> in %qD call is the same "
1813 "expression as the destination; did you mean to "
1814 "dereference it?", callee);
1815 return;
1816 }
1817
1818 if (POINTER_TYPE_P (TREE_TYPE (dest))
1819 && !strop
1820 && comp_types (TREE_TYPE (dest), type)
1821 && !VOID_TYPE_P (TREE_TYPE (type)))
1822 {
1823 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1824 "argument to %<sizeof%> in %qD call is the same "
1825 "pointer type %qT as the destination; expected %qT "
1826 "or an explicit length", callee, TREE_TYPE (dest),
1827 TREE_TYPE (TREE_TYPE (dest)));
1828 return;
1829 }
1830 }
1831
3a785c97 1832 if (src && !cmp)
1a4049e7 1833 {
3a785c97
JJ
1834 if (!TYPE_P (sizeof_arg[idx])
1835 && operand_equal_p (src, sizeof_arg[idx], 0)
1a4049e7
JJ
1836 && comp_types (TREE_TYPE (src), type))
1837 {
3a785c97 1838 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1a4049e7
JJ
1839 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1840 "argument to %<sizeof%> in %qD call is the same "
1841 "expression as the source; did you mean to "
1842 "remove the addressof?", callee);
1843 else if ((TYPE_PRECISION (TREE_TYPE (type))
1844 == TYPE_PRECISION (char_type_node))
1845 || strop)
1846 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1847 "argument to %<sizeof%> in %qD call is the same "
1848 "expression as the source; did you mean to "
1849 "provide an explicit length?", callee);
1850 else
1851 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1852 "argument to %<sizeof%> in %qD call is the same "
1853 "expression as the source; did you mean to "
1854 "dereference it?", callee);
1855 return;
1856 }
1857
1858 if (POINTER_TYPE_P (TREE_TYPE (src))
1859 && !strop
1860 && comp_types (TREE_TYPE (src), type)
1861 && !VOID_TYPE_P (TREE_TYPE (type)))
1862 {
1863 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1864 "argument to %<sizeof%> in %qD call is the same "
1865 "pointer type %qT as the source; expected %qT "
1866 "or an explicit length", callee, TREE_TYPE (src),
1867 TREE_TYPE (TREE_TYPE (src)));
1868 return;
1869 }
1870 }
3a785c97
JJ
1871
1872 if (dest)
1873 {
1874 if (!TYPE_P (sizeof_arg[idx])
1875 && operand_equal_p (dest, sizeof_arg[idx], 0)
1876 && comp_types (TREE_TYPE (dest), type))
1877 {
1878 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1879 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1880 "argument to %<sizeof%> in %qD call is the same "
1881 "expression as the first source; did you mean to "
1882 "remove the addressof?", callee);
1883 else if ((TYPE_PRECISION (TREE_TYPE (type))
1884 == TYPE_PRECISION (char_type_node))
1885 || strop)
1886 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1887 "argument to %<sizeof%> in %qD call is the same "
1888 "expression as the first source; did you mean to "
1889 "provide an explicit length?", callee);
1890 else
1891 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1892 "argument to %<sizeof%> in %qD call is the same "
1893 "expression as the first source; did you mean to "
1894 "dereference it?", callee);
1895 return;
1896 }
1897
1898 if (POINTER_TYPE_P (TREE_TYPE (dest))
1899 && !strop
1900 && comp_types (TREE_TYPE (dest), type)
1901 && !VOID_TYPE_P (TREE_TYPE (type)))
1902 {
1903 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1904 "argument to %<sizeof%> in %qD call is the same "
1905 "pointer type %qT as the first source; expected %qT "
1906 "or an explicit length", callee, TREE_TYPE (dest),
1907 TREE_TYPE (TREE_TYPE (dest)));
1908 return;
1909 }
1910 }
1911
1912 if (src)
1913 {
1914 if (!TYPE_P (sizeof_arg[idx])
1915 && operand_equal_p (src, sizeof_arg[idx], 0)
1916 && comp_types (TREE_TYPE (src), type))
1917 {
1918 if (TREE_CODE (sizeof_arg[idx]) == ADDR_EXPR && !strop)
1919 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1920 "argument to %<sizeof%> in %qD call is the same "
1921 "expression as the second source; did you mean to "
1922 "remove the addressof?", callee);
1923 else if ((TYPE_PRECISION (TREE_TYPE (type))
1924 == TYPE_PRECISION (char_type_node))
1925 || strop)
1926 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1927 "argument to %<sizeof%> in %qD call is the same "
1928 "expression as the second source; did you mean to "
1929 "provide an explicit length?", callee);
1930 else
1931 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1932 "argument to %<sizeof%> in %qD call is the same "
1933 "expression as the second source; did you mean to "
1934 "dereference it?", callee);
1935 return;
1936 }
1937
1938 if (POINTER_TYPE_P (TREE_TYPE (src))
1939 && !strop
1940 && comp_types (TREE_TYPE (src), type)
1941 && !VOID_TYPE_P (TREE_TYPE (type)))
1942 {
1943 warning_at (loc, OPT_Wsizeof_pointer_memaccess,
1944 "argument to %<sizeof%> in %qD call is the same "
1945 "pointer type %qT as the second source; expected %qT "
1946 "or an explicit length", callee, TREE_TYPE (src),
1947 TREE_TYPE (TREE_TYPE (src)));
1948 return;
1949 }
1950 }
1951
1a4049e7
JJ
1952}
1953
a1e45ff0
DM
1954/* Warn for unlikely, improbable, or stupid DECL declarations
1955 of `main'. */
1956
1957void
1958check_main_parameter_types (tree decl)
1959{
e19a18d4
NF
1960 function_args_iterator iter;
1961 tree type;
a1e45ff0
DM
1962 int argct = 0;
1963
e19a18d4
NF
1964 FOREACH_FUNCTION_ARGS (TREE_TYPE (decl), type, iter)
1965 {
1966 /* XXX void_type_node belies the abstraction. */
1967 if (type == void_type_node || type == error_mark_node )
1968 break;
1969
f827930a
MP
1970 tree t = type;
1971 if (TYPE_ATOMIC (t))
1972 pedwarn (input_location, OPT_Wmain,
1973 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1974 type, decl);
1975 while (POINTER_TYPE_P (t))
1976 {
1977 t = TREE_TYPE (t);
1978 if (TYPE_ATOMIC (t))
1979 pedwarn (input_location, OPT_Wmain,
1980 "%<_Atomic%>-qualified parameter type %qT of %q+D",
1981 type, decl);
1982 }
1983
e19a18d4
NF
1984 ++argct;
1985 switch (argct)
1986 {
1987 case 1:
1988 if (TYPE_MAIN_VARIANT (type) != integer_type_node)
1989 pedwarn (input_location, OPT_Wmain,
1990 "first argument of %q+D should be %<int%>", decl);
1991 break;
1992
1993 case 2:
1994 if (TREE_CODE (type) != POINTER_TYPE
1995 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
1996 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
1997 != char_type_node))
1998 pedwarn (input_location, OPT_Wmain,
1999 "second argument of %q+D should be %<char **%>", decl);
2000 break;
2001
2002 case 3:
2003 if (TREE_CODE (type) != POINTER_TYPE
2004 || TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE
2005 || (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (type)))
2006 != char_type_node))
2007 pedwarn (input_location, OPT_Wmain,
2008 "third argument of %q+D should probably be "
2009 "%<char **%>", decl);
2010 break;
2011 }
2012 }
a1e45ff0
DM
2013
2014 /* It is intentional that this message does not mention the third
2015 argument because it's only mentioned in an appendix of the
2016 standard. */
2017 if (argct > 0 && (argct < 2 || argct > 3))
e19a18d4
NF
2018 pedwarn (input_location, OPT_Wmain,
2019 "%q+D takes only zero or two arguments", decl);
38e514c0
MP
2020
2021 if (stdarg_p (TREE_TYPE (decl)))
2022 pedwarn (input_location, OPT_Wmain,
2023 "%q+D declared as variadic function", decl);
a1e45ff0
DM
2024}
2025
0af94e6f
JR
2026/* vector_targets_convertible_p is used for vector pointer types. The
2027 callers perform various checks that the qualifiers are satisfactory,
2028 while OTOH vector_targets_convertible_p ignores the number of elements
2029 in the vectors. That's fine with vector pointers as we can consider,
2030 say, a vector of 8 elements as two consecutive vectors of 4 elements,
2031 and that does not require and conversion of the pointer values.
2032 In contrast, vector_types_convertible_p and
2033 vector_types_compatible_elements_p are used for vector value types. */
f83c7f63
DJ
2034/* True if pointers to distinct types T1 and T2 can be converted to
2035 each other without an explicit cast. Only returns true for opaque
2036 vector types. */
2037bool
2038vector_targets_convertible_p (const_tree t1, const_tree t2)
2039{
31521951 2040 if (VECTOR_TYPE_P (t1) && VECTOR_TYPE_P (t2)
b6fc2cdb 2041 && (TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
f83c7f63
DJ
2042 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2043 return true;
2044
2045 return false;
2046}
2047
0af94e6f
JR
2048/* vector_types_convertible_p is used for vector value types.
2049 It could in principle call vector_targets_convertible_p as a subroutine,
2050 but then the check for vector type would be duplicated with its callers,
2051 and also the purpose of vector_targets_convertible_p would become
2052 muddled.
2053 Where vector_types_convertible_p returns true, a conversion might still be
2054 needed to make the types match.
2055 In contrast, vector_targets_convertible_p is used for vector pointer
2056 values, and vector_types_compatible_elements_p is used specifically
2057 in the context for binary operators, as a check if use is possible without
2058 conversion. */
00c8e9f6
MS
2059/* True if vector types T1 and T2 can be converted to each other
2060 without an explicit cast. If EMIT_LAX_NOTE is true, and T1 and T2
2061 can only be converted with -flax-vector-conversions yet that is not
2062 in effect, emit a note telling the user about that option if such
2063 a note has not previously been emitted. */
2064bool
58f9752a 2065vector_types_convertible_p (const_tree t1, const_tree t2, bool emit_lax_note)
cc27e657 2066{
00c8e9f6 2067 static bool emitted_lax_note = false;
14e765da
JM
2068 bool convertible_lax;
2069
b6fc2cdb 2070 if ((TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2))
14e765da
JM
2071 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
2072 return true;
2073
2074 convertible_lax =
2075 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
2076 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE ||
a5e0cd1d 2077 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2))
14e765da
JM
2078 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
2079 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
00c8e9f6
MS
2080
2081 if (!convertible_lax || flag_lax_vector_conversions)
2082 return convertible_lax;
2083
2084 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
cf7bc668 2085 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
00c8e9f6
MS
2086 return true;
2087
2088 if (emit_lax_note && !emitted_lax_note)
2089 {
2090 emitted_lax_note = true;
1f5b3869 2091 inform (input_location, "use -flax-vector-conversions to permit "
00c8e9f6
MS
2092 "conversions between vectors with differing "
2093 "element types or numbers of subparts");
2094 }
2095
2096 return false;
cc27e657
PB
2097}
2098
9e1a8dd1
RR
2099/* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
2100 and have vector types, V0 has the same type as V1, and the number of
2101 elements of V0, V1, MASK is the same.
2102
2103 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2104 called with two arguments. In this case implementation passes the
2105 first argument twice in order to share the same tree code. This fact
2106 could enable the mask-values being twice the vector length. This is
2107 an implementation accident and this semantics is not guaranteed to
2108 the user. */
2109tree
bedc293e
MG
2110c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask,
2111 bool complain)
9e1a8dd1
RR
2112{
2113 tree ret;
2114 bool wrap = true;
2115 bool maybe_const = false;
2116 bool two_arguments = false;
2117
2118 if (v1 == NULL_TREE)
2119 {
2120 two_arguments = true;
2121 v1 = v0;
2122 }
2123
2124 if (v0 == error_mark_node || v1 == error_mark_node
2125 || mask == error_mark_node)
2126 return error_mark_node;
2127
31521951 2128 if (!VECTOR_INTEGER_TYPE_P (TREE_TYPE (mask)))
9e1a8dd1 2129 {
bedc293e
MG
2130 if (complain)
2131 error_at (loc, "__builtin_shuffle last argument must "
2132 "be an integer vector");
9e1a8dd1
RR
2133 return error_mark_node;
2134 }
2135
31521951
MP
2136 if (!VECTOR_TYPE_P (TREE_TYPE (v0))
2137 || !VECTOR_TYPE_P (TREE_TYPE (v1)))
9e1a8dd1 2138 {
bedc293e
MG
2139 if (complain)
2140 error_at (loc, "__builtin_shuffle arguments must be vectors");
9e1a8dd1
RR
2141 return error_mark_node;
2142 }
2143
2144 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2145 {
bedc293e
MG
2146 if (complain)
2147 error_at (loc, "__builtin_shuffle argument vectors must be of "
2148 "the same type");
9e1a8dd1
RR
2149 return error_mark_node;
2150 }
2151
2152 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2153 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2154 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2155 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2156 {
bedc293e
MG
2157 if (complain)
2158 error_at (loc, "__builtin_shuffle number of elements of the "
2159 "argument vector(s) and the mask vector should "
2160 "be the same");
9e1a8dd1
RR
2161 return error_mark_node;
2162 }
2163
2164 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2165 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2166 {
bedc293e
MG
2167 if (complain)
2168 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2169 "must have the same size as inner type of the mask");
9e1a8dd1
RR
2170 return error_mark_node;
2171 }
2172
2173 if (!c_dialect_cxx ())
2174 {
2175 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
2176 v0 = c_fully_fold (v0, false, &maybe_const);
2177 wrap &= maybe_const;
2178
2179 if (two_arguments)
2180 v1 = v0 = save_expr (v0);
2181 else
2182 {
2183 v1 = c_fully_fold (v1, false, &maybe_const);
2184 wrap &= maybe_const;
2185 }
2186
2187 mask = c_fully_fold (mask, false, &maybe_const);
2188 wrap &= maybe_const;
2189 }
bedc293e
MG
2190 else if (two_arguments)
2191 v1 = v0 = save_expr (v0);
9e1a8dd1
RR
2192
2193 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
2194
2195 if (!c_dialect_cxx () && !wrap)
2196 ret = c_wrap_maybe_const (ret, true);
2197
2198 return ret;
2199}
2200
828fb3ba
JM
2201/* Like tree.c:get_narrower, but retain conversion from C++0x scoped enum
2202 to integral type. */
2203
2204static tree
2205c_common_get_narrower (tree op, int *unsignedp_ptr)
2206{
2207 op = get_narrower (op, unsignedp_ptr);
2208
2209 if (TREE_CODE (TREE_TYPE (op)) == ENUMERAL_TYPE
2210 && ENUM_IS_SCOPED (TREE_TYPE (op)))
2211 {
2212 /* C++0x scoped enumerations don't implicitly convert to integral
2213 type; if we stripped an explicit conversion to a larger type we
2214 need to replace it so common_type will still work. */
21fa2faf
RG
2215 tree type = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op)),
2216 TYPE_UNSIGNED (TREE_TYPE (op)));
828fb3ba
JM
2217 op = fold_convert (type, op);
2218 }
2219 return op;
2220}
2221
6715192c
MLI
2222/* This is a helper function of build_binary_op.
2223
2224 For certain operations if both args were extended from the same
2225 smaller type, do the arithmetic in that type and then extend.
2226
2227 BITWISE indicates a bitwise operation.
2228 For them, this optimization is safe only if
2229 both args are zero-extended or both are sign-extended.
2230 Otherwise, we might change the result.
2231 Eg, (short)-1 | (unsigned short)-1 is (int)-1
b8698a0f 2232 but calculated in (unsigned short) it would be (unsigned short)-1.
6715192c 2233*/
828fb3ba
JM
2234tree
2235shorten_binary_op (tree result_type, tree op0, tree op1, bool bitwise)
6715192c
MLI
2236{
2237 int unsigned0, unsigned1;
2238 tree arg0, arg1;
2239 int uns;
2240 tree type;
2241
2242 /* Cast OP0 and OP1 to RESULT_TYPE. Doing so prevents
2243 excessive narrowing when we call get_narrower below. For
2244 example, suppose that OP0 is of unsigned int extended
2245 from signed char and that RESULT_TYPE is long long int.
2246 If we explicitly cast OP0 to RESULT_TYPE, OP0 would look
2247 like
b8698a0f 2248
6715192c
MLI
2249 (long long int) (unsigned int) signed_char
2250
2251 which get_narrower would narrow down to
b8698a0f 2252
6715192c 2253 (unsigned int) signed char
b8698a0f 2254
6715192c
MLI
2255 If we do not cast OP0 first, get_narrower would return
2256 signed_char, which is inconsistent with the case of the
2257 explicit cast. */
2258 op0 = convert (result_type, op0);
2259 op1 = convert (result_type, op1);
2260
828fb3ba
JM
2261 arg0 = c_common_get_narrower (op0, &unsigned0);
2262 arg1 = c_common_get_narrower (op1, &unsigned1);
6715192c
MLI
2263
2264 /* UNS is 1 if the operation to be done is an unsigned one. */
2265 uns = TYPE_UNSIGNED (result_type);
2266
2267 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
2268 but it *requires* conversion to FINAL_TYPE. */
b8698a0f 2269
6715192c
MLI
2270 if ((TYPE_PRECISION (TREE_TYPE (op0))
2271 == TYPE_PRECISION (TREE_TYPE (arg0)))
2272 && TREE_TYPE (op0) != result_type)
2273 unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2274 if ((TYPE_PRECISION (TREE_TYPE (op1))
2275 == TYPE_PRECISION (TREE_TYPE (arg1)))
2276 && TREE_TYPE (op1) != result_type)
2277 unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b8698a0f 2278
6715192c 2279 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
b8698a0f 2280
6715192c
MLI
2281 /* For bitwise operations, signedness of nominal type
2282 does not matter. Consider only how operands were extended. */
2283 if (bitwise)
2284 uns = unsigned0;
b8698a0f 2285
6715192c
MLI
2286 /* Note that in all three cases below we refrain from optimizing
2287 an unsigned operation on sign-extended args.
2288 That would not be valid. */
b8698a0f 2289
6715192c
MLI
2290 /* Both args variable: if both extended in same way
2291 from same width, do it in that width.
2292 Do it unsigned if args were zero-extended. */
2293 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2294 < TYPE_PRECISION (result_type))
2295 && (TYPE_PRECISION (TREE_TYPE (arg1))
2296 == TYPE_PRECISION (TREE_TYPE (arg0)))
2297 && unsigned0 == unsigned1
2298 && (unsigned0 || !uns))
2299 return c_common_signed_or_unsigned_type
2300 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
2301
2302 else if (TREE_CODE (arg0) == INTEGER_CST
2303 && (unsigned1 || !uns)
2304 && (TYPE_PRECISION (TREE_TYPE (arg1))
2305 < TYPE_PRECISION (result_type))
2306 && (type
2307 = c_common_signed_or_unsigned_type (unsigned1,
2308 TREE_TYPE (arg1)))
2309 && !POINTER_TYPE_P (type)
2310 && int_fits_type_p (arg0, type))
2311 return type;
2312
2313 else if (TREE_CODE (arg1) == INTEGER_CST
2314 && (unsigned0 || !uns)
2315 && (TYPE_PRECISION (TREE_TYPE (arg0))
2316 < TYPE_PRECISION (result_type))
2317 && (type
2318 = c_common_signed_or_unsigned_type (unsigned0,
2319 TREE_TYPE (arg0)))
2320 && !POINTER_TYPE_P (type)
2321 && int_fits_type_p (arg1, type))
2322 return type;
2323
2324 return result_type;
2325}
2326
7a37fa90
MM
2327/* Returns true iff any integer value of type FROM_TYPE can be represented as
2328 real of type TO_TYPE. This is a helper function for unsafe_conversion_p. */
2329
2330static bool
2331int_safely_convertible_to_real_p (const_tree from_type, const_tree to_type)
2332{
2333 tree type_low_bound = TYPE_MIN_VALUE (from_type);
2334 tree type_high_bound = TYPE_MAX_VALUE (from_type);
2335 REAL_VALUE_TYPE real_low_bound =
2336 real_value_from_int_cst (0, type_low_bound);
2337 REAL_VALUE_TYPE real_high_bound =
2338 real_value_from_int_cst (0, type_high_bound);
2339
2340 return exact_real_truncate (TYPE_MODE (to_type), &real_low_bound)
2341 && exact_real_truncate (TYPE_MODE (to_type), &real_high_bound);
2342}
2343
2344/* Checks if expression EXPR of complex/real/integer type cannot be converted
2345 to the complex/real/integer type TYPE. Function returns non-zero when:
68fca595
MP
2346 * EXPR is a constant which cannot be exactly converted to TYPE.
2347 * EXPR is not a constant and size of EXPR's type > than size of TYPE,
7a37fa90
MM
2348 for EXPR type and TYPE being both integers or both real, or both
2349 complex.
2350 * EXPR is not a constant of complex type and TYPE is a real or
2351 an integer.
68fca595
MP
2352 * EXPR is not a constant of real type and TYPE is an integer.
2353 * EXPR is not a constant of integer type which cannot be
2354 exactly converted to real type.
7a37fa90 2355
0e3a99ae 2356 Function allows conversions between types of different signedness and
49b0aa18 2357 can return SAFE_CONVERSION (zero) in that case. Function can produce
7a37fa90
MM
2358 signedness warnings if PRODUCE_WARNS is true.
2359
2360 Function allows conversions from complex constants to non-complex types,
2361 provided that imaginary part is zero and real part can be safely converted
2362 to TYPE. */
68fca595 2363
49b0aa18 2364enum conversion_safety
68fca595 2365unsafe_conversion_p (location_t loc, tree type, tree expr, bool produce_warns)
422c3a54 2366{
49b0aa18 2367 enum conversion_safety give_warning = SAFE_CONVERSION; /* is 0 or false */
374035cb 2368 tree expr_type = TREE_TYPE (expr);
68fca595 2369 loc = expansion_point_location_if_in_system_header (loc);
422c3a54 2370
0e3a99ae 2371 if (TREE_CODE (expr) == REAL_CST || TREE_CODE (expr) == INTEGER_CST)
0011dedb 2372 {
7a37fa90
MM
2373 /* If type is complex, we are interested in compatibility with
2374 underlying type. */
2375 if (TREE_CODE (type) == COMPLEX_TYPE)
2376 type = TREE_TYPE (type);
2377
422c3a54 2378 /* Warn for real constant that is not an exact integer converted
0e3a99ae 2379 to integer type. */
374035cb 2380 if (TREE_CODE (expr_type) == REAL_TYPE
0e3a99ae
AS
2381 && TREE_CODE (type) == INTEGER_TYPE)
2382 {
2383 if (!real_isinteger (TREE_REAL_CST_PTR (expr), TYPE_MODE (expr_type)))
49b0aa18 2384 give_warning = UNSAFE_REAL;
0e3a99ae 2385 }
91c41804 2386 /* Warn for an integer constant that does not fit into integer type. */
374035cb 2387 else if (TREE_CODE (expr_type) == INTEGER_TYPE
0e3a99ae
AS
2388 && TREE_CODE (type) == INTEGER_TYPE
2389 && !int_fits_type_p (expr, type))
2390 {
2391 if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)
374035cb 2392 && tree_int_cst_sgn (expr) < 0)
0e3a99ae
AS
2393 {
2394 if (produce_warns)
2395 warning_at (loc, OPT_Wsign_conversion, "negative integer"
2396 " implicitly converted to unsigned type");
2397 }
2398 else if (!TYPE_UNSIGNED (type) && TYPE_UNSIGNED (expr_type))
2399 {
2400 if (produce_warns)
2401 warning_at (loc, OPT_Wsign_conversion, "conversion of unsigned"
2402 " constant value to negative integer");
2403 }
7060db96 2404 else
49b0aa18 2405 give_warning = UNSAFE_OTHER;
0e3a99ae 2406 }
422c3a54 2407 else if (TREE_CODE (type) == REAL_TYPE)
0e3a99ae
AS
2408 {
2409 /* Warn for an integer constant that does not fit into real type. */
2410 if (TREE_CODE (expr_type) == INTEGER_TYPE)
2411 {
2412 REAL_VALUE_TYPE a = real_value_from_int_cst (0, expr);
2413 if (!exact_real_truncate (TYPE_MODE (type), &a))
49b0aa18 2414 give_warning = UNSAFE_REAL;
0e3a99ae
AS
2415 }
2416 /* Warn for a real constant that does not fit into a smaller
2417 real type. */
2418 else if (TREE_CODE (expr_type) == REAL_TYPE
2419 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
2420 {
2421 REAL_VALUE_TYPE a = TREE_REAL_CST (expr);
2422 if (!exact_real_truncate (TYPE_MODE (type), &a))
49b0aa18 2423 give_warning = UNSAFE_REAL;
0e3a99ae
AS
2424 }
2425 }
2426 }
7a37fa90
MM
2427
2428 else if (TREE_CODE (expr) == COMPLEX_CST)
2429 {
2430 tree imag_part = TREE_IMAGPART (expr);
2431 /* Conversion from complex constant with zero imaginary part,
2432 perform check for conversion of real part. */
2433 if ((TREE_CODE (imag_part) == REAL_CST
2434 && real_zerop (imag_part))
2435 || (TREE_CODE (imag_part) == INTEGER_CST
2436 && integer_zerop (imag_part)))
2437 /* Note: in this branch we use recursive call to unsafe_conversion_p
2438 with different type of EXPR, but it is still safe, because when EXPR
2439 is a constant, it's type is not used in text of generated warnings
2440 (otherwise they could sound misleading). */
2441 return unsafe_conversion_p (loc, type, TREE_REALPART (expr),
2442 produce_warns);
2443 /* Conversion from complex constant with non-zero imaginary part. */
2444 else
2445 {
2446 /* Conversion to complex type.
2447 Perform checks for both real and imaginary parts. */
2448 if (TREE_CODE (type) == COMPLEX_TYPE)
2449 {
2450 /* Unfortunately, produce_warns must be false in two subsequent
2451 calls of unsafe_conversion_p, because otherwise we could
2452 produce strange "double" warnings, if both real and imaginary
2453 parts have conversion problems related to signedness.
2454
2455 For example:
2456 int32_t _Complex a = 0x80000000 + 0x80000000i;
2457
2458 Possible solution: add a separate function for checking
2459 constants and combine result of two calls appropriately. */
2460 enum conversion_safety re_safety =
2461 unsafe_conversion_p (loc, type, TREE_REALPART (expr), false);
2462 enum conversion_safety im_safety =
2463 unsafe_conversion_p (loc, type, imag_part, false);
2464
2465 /* Merge the results into appropriate single warning. */
2466
2467 /* Note: this case includes SAFE_CONVERSION, i.e. success. */
2468 if (re_safety == im_safety)
2469 give_warning = re_safety;
2470 else if (!re_safety && im_safety)
2471 give_warning = im_safety;
2472 else if (re_safety && !im_safety)
2473 give_warning = re_safety;
2474 else
2475 give_warning = UNSAFE_OTHER;
2476 }
2477 /* Warn about conversion from complex to real or integer type. */
2478 else
2479 give_warning = UNSAFE_IMAGINARY;
2480 }
2481 }
2482
2483 /* Checks for remaining case: EXPR is not constant. */
0e3a99ae
AS
2484 else
2485 {
422c3a54 2486 /* Warn for real types converted to integer types. */
6715192c 2487 if (TREE_CODE (expr_type) == REAL_TYPE
0e3a99ae 2488 && TREE_CODE (type) == INTEGER_TYPE)
49b0aa18 2489 give_warning = UNSAFE_REAL;
422c3a54 2490
6715192c 2491 else if (TREE_CODE (expr_type) == INTEGER_TYPE
0e3a99ae
AS
2492 && TREE_CODE (type) == INTEGER_TYPE)
2493 {
cfdaefec 2494 /* Don't warn about unsigned char y = 0xff, x = (int) y; */
c00e8b06 2495 expr = get_unwidened (expr, 0);
6715192c 2496 expr_type = TREE_TYPE (expr);
cfdaefec 2497
6715192c 2498 /* Don't warn for short y; short x = ((int)y & 0xff); */
b8698a0f 2499 if (TREE_CODE (expr) == BIT_AND_EXPR
0e3a99ae 2500 || TREE_CODE (expr) == BIT_IOR_EXPR
6715192c
MLI
2501 || TREE_CODE (expr) == BIT_XOR_EXPR)
2502 {
374035cb
MLI
2503 /* If both args were extended from a shortest type,
2504 use that type if that is safe. */
b8698a0f
L
2505 expr_type = shorten_binary_op (expr_type,
2506 TREE_OPERAND (expr, 0),
2507 TREE_OPERAND (expr, 1),
6715192c
MLI
2508 /* bitwise */1);
2509
6715192c
MLI
2510 if (TREE_CODE (expr) == BIT_AND_EXPR)
2511 {
2512 tree op0 = TREE_OPERAND (expr, 0);
2513 tree op1 = TREE_OPERAND (expr, 1);
9c591bd0
MLI
2514 bool unsigned0 = TYPE_UNSIGNED (TREE_TYPE (op0));
2515 bool unsigned1 = TYPE_UNSIGNED (TREE_TYPE (op1));
2516
2517 /* If one of the operands is a non-negative constant
2518 that fits in the target type, then the type of the
2519 other operand does not matter. */
6715192c
MLI
2520 if ((TREE_CODE (op0) == INTEGER_CST
2521 && int_fits_type_p (op0, c_common_signed_type (type))
2522 && int_fits_type_p (op0, c_common_unsigned_type (type)))
2523 || (TREE_CODE (op1) == INTEGER_CST
374035cb 2524 && int_fits_type_p (op1, c_common_signed_type (type))
b8698a0f 2525 && int_fits_type_p (op1,
374035cb 2526 c_common_unsigned_type (type))))
49b0aa18 2527 return SAFE_CONVERSION;
9c591bd0
MLI
2528 /* If constant is unsigned and fits in the target
2529 type, then the result will also fit. */
2530 else if ((TREE_CODE (op0) == INTEGER_CST
b8698a0f 2531 && unsigned0
9c591bd0
MLI
2532 && int_fits_type_p (op0, type))
2533 || (TREE_CODE (op1) == INTEGER_CST
2534 && unsigned1
2535 && int_fits_type_p (op1, type)))
49b0aa18 2536 return SAFE_CONVERSION;
6715192c
MLI
2537 }
2538 }
0e3a99ae 2539 /* Warn for integer types converted to smaller integer types. */
b8698a0f 2540 if (TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
49b0aa18 2541 give_warning = UNSAFE_OTHER;
7060db96
MLI
2542
2543 /* When they are the same width but different signedness,
2544 then the value may change. */
0e3a99ae 2545 else if (((TYPE_PRECISION (type) == TYPE_PRECISION (expr_type)
6715192c 2546 && TYPE_UNSIGNED (expr_type) != TYPE_UNSIGNED (type))
7060db96
MLI
2547 /* Even when converted to a bigger type, if the type is
2548 unsigned but expr is signed, then negative values
2549 will be changed. */
0e3a99ae
AS
2550 || (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (expr_type)))
2551 && produce_warns)
6312e84d
MLI
2552 warning_at (loc, OPT_Wsign_conversion, "conversion to %qT from %qT "
2553 "may change the sign of the result",
2554 type, expr_type);
0e3a99ae 2555 }
422c3a54
MLI
2556
2557 /* Warn for integer types converted to real types if and only if
0e3a99ae
AS
2558 all the range of values of the integer type cannot be
2559 represented by the real type. */
6715192c 2560 else if (TREE_CODE (expr_type) == INTEGER_TYPE
0e3a99ae
AS
2561 && TREE_CODE (type) == REAL_TYPE)
2562 {
58076e21
MLI
2563 /* Don't warn about char y = 0xff; float x = (int) y; */
2564 expr = get_unwidened (expr, 0);
2565 expr_type = TREE_TYPE (expr);
2566
7a37fa90 2567 if (!int_safely_convertible_to_real_p (expr_type, type))
49b0aa18 2568 give_warning = UNSAFE_OTHER;
0e3a99ae 2569 }
422c3a54
MLI
2570
2571 /* Warn for real types converted to smaller real types. */
6715192c 2572 else if (TREE_CODE (expr_type) == REAL_TYPE
0e3a99ae
AS
2573 && TREE_CODE (type) == REAL_TYPE
2574 && TYPE_PRECISION (type) < TYPE_PRECISION (expr_type))
49b0aa18 2575 give_warning = UNSAFE_REAL;
7a37fa90
MM
2576
2577 /* Check conversion between two complex types. */
2578 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2579 && TREE_CODE (type) == COMPLEX_TYPE)
2580 {
2581 /* Extract underlying types (i.e., type of real and imaginary
2582 parts) of expr_type and type. */
2583 tree from_type = TREE_TYPE (expr_type);
2584 tree to_type = TREE_TYPE (type);
2585
2586 /* Warn for real types converted to integer types. */
2587 if (TREE_CODE (from_type) == REAL_TYPE
2588 && TREE_CODE (to_type) == INTEGER_TYPE)
2589 give_warning = UNSAFE_REAL;
2590
2591 /* Warn for real types converted to smaller real types. */
2592 else if (TREE_CODE (from_type) == REAL_TYPE
2593 && TREE_CODE (to_type) == REAL_TYPE
2594 && TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2595 give_warning = UNSAFE_REAL;
2596
2597 /* Check conversion for complex integer types. Here implementation
2598 is simpler than for real-domain integers because it does not
2599 involve sophisticated cases, such as bitmasks, casts, etc. */
2600 else if (TREE_CODE (from_type) == INTEGER_TYPE
2601 && TREE_CODE (to_type) == INTEGER_TYPE)
2602 {
2603 /* Warn for integer types converted to smaller integer types. */
2604 if (TYPE_PRECISION (to_type) < TYPE_PRECISION (from_type))
2605 give_warning = UNSAFE_OTHER;
2606
2607 /* Check for different signedness, see case for real-domain
2608 integers (above) for a more detailed comment. */
2609 else if (((TYPE_PRECISION (to_type) == TYPE_PRECISION (from_type)
2610 && TYPE_UNSIGNED (to_type) != TYPE_UNSIGNED (from_type))
2611 || (TYPE_UNSIGNED (to_type) && !TYPE_UNSIGNED (from_type)))
2612 && produce_warns)
2613 warning_at (loc, OPT_Wsign_conversion,
2614 "conversion to %qT from %qT "
2615 "may change the sign of the result",
2616 type, expr_type);
2617 }
2618 else if (TREE_CODE (from_type) == INTEGER_TYPE
2619 && TREE_CODE (to_type) == REAL_TYPE
2620 && !int_safely_convertible_to_real_p (from_type, to_type))
2621 give_warning = UNSAFE_OTHER;
2622 }
2623
2624 /* Warn for complex types converted to real or integer types. */
2625 else if (TREE_CODE (expr_type) == COMPLEX_TYPE
2626 && TREE_CODE (type) != COMPLEX_TYPE)
2627 give_warning = UNSAFE_IMAGINARY;
0e3a99ae
AS
2628 }
2629
2630 return give_warning;
2631}
2632
2633/* Warns if the conversion of EXPR to TYPE may alter a value.
2634 This is a helper function for warnings_for_convert_and_check. */
2635
2636static void
68fca595 2637conversion_warning (location_t loc, tree type, tree expr)
0e3a99ae 2638{
0e3a99ae 2639 tree expr_type = TREE_TYPE (expr);
49b0aa18 2640 enum conversion_safety conversion_kind;
422c3a54 2641
49b0aa18 2642 if (!warn_conversion && !warn_sign_conversion && !warn_float_conversion)
0e3a99ae 2643 return;
422c3a54 2644
66f20604
MP
2645 /* This may happen, because for LHS op= RHS we preevaluate
2646 RHS and create C_MAYBE_CONST_EXPR <SAVE_EXPR <RHS>>, which
2647 means we could no longer see the code of the EXPR. */
2648 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
2649 expr = C_MAYBE_CONST_EXPR_EXPR (expr);
2650 if (TREE_CODE (expr) == SAVE_EXPR)
2651 expr = TREE_OPERAND (expr, 0);
2652
0e3a99ae
AS
2653 switch (TREE_CODE (expr))
2654 {
2655 case EQ_EXPR:
2656 case NE_EXPR:
2657 case LE_EXPR:
2658 case GE_EXPR:
2659 case LT_EXPR:
2660 case GT_EXPR:
2661 case TRUTH_ANDIF_EXPR:
2662 case TRUTH_ORIF_EXPR:
2663 case TRUTH_AND_EXPR:
2664 case TRUTH_OR_EXPR:
2665 case TRUTH_XOR_EXPR:
2666 case TRUTH_NOT_EXPR:
2667 /* Conversion from boolean to a signed:1 bit-field (which only
2668 can hold the values 0 and -1) doesn't lose information - but
2669 it does change the value. */
2670 if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
2671 warning_at (loc, OPT_Wconversion,
2672 "conversion to %qT from boolean expression", type);
2673 return;
2674
2675 case REAL_CST:
2676 case INTEGER_CST:
7a37fa90 2677 case COMPLEX_CST:
68fca595 2678 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
49b0aa18
JC
2679 if (conversion_kind == UNSAFE_REAL)
2680 warning_at (loc, OPT_Wfloat_conversion,
2681 "conversion to %qT alters %qT constant value",
2682 type, expr_type);
2683 else if (conversion_kind)
0e3a99ae
AS
2684 warning_at (loc, OPT_Wconversion,
2685 "conversion to %qT alters %qT constant value",
2686 type, expr_type);
2687 return;
2688
2689 case COND_EXPR:
2690 {
3f46d6a5
MLI
2691 /* In case of COND_EXPR, we do not care about the type of
2692 COND_EXPR, only about the conversion of each operand. */
2693 tree op1 = TREE_OPERAND (expr, 1);
2694 tree op2 = TREE_OPERAND (expr, 2);
2695
68fca595
MP
2696 conversion_warning (loc, type, op1);
2697 conversion_warning (loc, type, op2);
3f46d6a5 2698 return;
0e3a99ae
AS
2699 }
2700
2701 default: /* 'expr' is not a constant. */
68fca595 2702 conversion_kind = unsafe_conversion_p (loc, type, expr, true);
49b0aa18
JC
2703 if (conversion_kind == UNSAFE_REAL)
2704 warning_at (loc, OPT_Wfloat_conversion,
2705 "conversion to %qT from %qT may alter its value",
2706 type, expr_type);
7a37fa90
MM
2707 else if (conversion_kind == UNSAFE_IMAGINARY)
2708 warning_at (loc, OPT_Wconversion,
2709 "conversion to %qT from %qT discards imaginary component",
2710 type, expr_type);
49b0aa18 2711 else if (conversion_kind)
0e3a99ae 2712 warning_at (loc, OPT_Wconversion,
6312e84d
MLI
2713 "conversion to %qT from %qT may alter its value",
2714 type, expr_type);
422c3a54
MLI
2715 }
2716}
2717
07231d4f
MLI
2718/* Produce warnings after a conversion. RESULT is the result of
2719 converting EXPR to TYPE. This is a helper function for
2720 convert_and_check and cp_convert_and_check. */
d74154d5 2721
07231d4f 2722void
68fca595
MP
2723warnings_for_convert_and_check (location_t loc, tree type, tree expr,
2724 tree result)
d74154d5 2725{
68fca595 2726 loc = expansion_point_location_if_in_system_header (loc);
5a3c9cf2 2727
91c41804
RS
2728 if (TREE_CODE (expr) == INTEGER_CST
2729 && (TREE_CODE (type) == INTEGER_TYPE
2730 || TREE_CODE (type) == ENUMERAL_TYPE)
2731 && !int_fits_type_p (expr, type))
2732 {
422c3a54
MLI
2733 /* Do not diagnose overflow in a constant expression merely
2734 because a conversion overflowed. */
91c41804 2735 if (TREE_OVERFLOW (result))
d95787e6
RS
2736 TREE_OVERFLOW (result) = TREE_OVERFLOW (expr);
2737
91c41804 2738 if (TYPE_UNSIGNED (type))
422c3a54 2739 {
91c41804
RS
2740 /* This detects cases like converting -129 or 256 to
2741 unsigned char. */
2742 if (!int_fits_type_p (expr, c_common_signed_type (type)))
5a3c9cf2
PC
2743 warning_at (loc, OPT_Woverflow,
2744 "large integer implicitly truncated to unsigned type");
7060db96 2745 else
68fca595 2746 conversion_warning (loc, type, expr);
91c41804 2747 }
b8698a0f 2748 else if (!int_fits_type_p (expr, c_common_unsigned_type (type)))
68fca595 2749 warning_at (loc, OPT_Woverflow,
f73fe417
MLI
2750 "overflow in implicit constant conversion");
2751 /* No warning for converting 0x80000000 to int. */
2752 else if (pedantic
2753 && (TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE
2754 || TYPE_PRECISION (TREE_TYPE (expr))
2755 != TYPE_PRECISION (type)))
5a3c9cf2
PC
2756 warning_at (loc, OPT_Woverflow,
2757 "overflow in implicit constant conversion");
f73fe417 2758
7060db96 2759 else
68fca595 2760 conversion_warning (loc, type, expr);
d74154d5 2761 }
ab22c1fa
CF
2762 else if ((TREE_CODE (result) == INTEGER_CST
2763 || TREE_CODE (result) == FIXED_CST) && TREE_OVERFLOW (result))
5a3c9cf2
PC
2764 warning_at (loc, OPT_Woverflow,
2765 "overflow in implicit constant conversion");
7060db96 2766 else
68fca595 2767 conversion_warning (loc, type, expr);
07231d4f
MLI
2768}
2769
2770
2771/* Convert EXPR to TYPE, warning about conversion problems with constants.
2772 Invoke this function on every expression that is converted implicitly,
2773 i.e. because of language rules and not because of an explicit cast. */
2774
2775tree
68fca595 2776convert_and_check (location_t loc, tree type, tree expr)
07231d4f
MLI
2777{
2778 tree result;
8ce94e44
JM
2779 tree expr_for_warning;
2780
2781 /* Convert from a value with possible excess precision rather than
2782 via the semantic type, but do not warn about values not fitting
2783 exactly in the semantic type. */
2784 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
2785 {
2786 tree orig_type = TREE_TYPE (expr);
2787 expr = TREE_OPERAND (expr, 0);
2788 expr_for_warning = convert (orig_type, expr);
2789 if (orig_type == type)
2790 return expr_for_warning;
2791 }
2792 else
2793 expr_for_warning = expr;
07231d4f
MLI
2794
2795 if (TREE_TYPE (expr) == type)
2796 return expr;
b8698a0f 2797
07231d4f
MLI
2798 result = convert (type, expr);
2799
7d882b83
ILT
2800 if (c_inhibit_evaluation_warnings == 0
2801 && !TREE_OVERFLOW_P (expr)
2802 && result != error_mark_node)
68fca595 2803 warnings_for_convert_and_check (loc, type, expr_for_warning, result);
07231d4f 2804
91c41804 2805 return result;
96571883
BK
2806}
2807\f
235cfbc4
BS
2808/* A node in a list that describes references to variables (EXPR), which are
2809 either read accesses if WRITER is zero, or write accesses, in which case
2810 WRITER is the parent of EXPR. */
2811struct tlist
2812{
2813 struct tlist *next;
2814 tree expr, writer;
2815};
2816
2817/* Used to implement a cache the results of a call to verify_tree. We only
2818 use this for SAVE_EXPRs. */
2819struct tlist_cache
2820{
2821 struct tlist_cache *next;
2822 struct tlist *cache_before_sp;
2823 struct tlist *cache_after_sp;
2824 tree expr;
2683ed8d
BS
2825};
2826
235cfbc4
BS
2827/* Obstack to use when allocating tlist structures, and corresponding
2828 firstobj. */
2829static struct obstack tlist_obstack;
2830static char *tlist_firstobj = 0;
2831
2832/* Keep track of the identifiers we've warned about, so we can avoid duplicate
2833 warnings. */
2834static struct tlist *warned_ids;
2835/* SAVE_EXPRs need special treatment. We process them only once and then
2836 cache the results. */
2837static struct tlist_cache *save_expr_cache;
2838
35b1a6fa
AJ
2839static void add_tlist (struct tlist **, struct tlist *, tree, int);
2840static void merge_tlist (struct tlist **, struct tlist *, int);
2841static void verify_tree (tree, struct tlist **, struct tlist **, tree);
2842static int warning_candidate_p (tree);
1e4ae551 2843static bool candidate_equal_p (const_tree, const_tree);
35b1a6fa
AJ
2844static void warn_for_collisions (struct tlist *);
2845static void warn_for_collisions_1 (tree, tree, struct tlist *, int);
2846static struct tlist *new_tlist (struct tlist *, tree, tree);
2683ed8d 2847
235cfbc4
BS
2848/* Create a new struct tlist and fill in its fields. */
2849static struct tlist *
35b1a6fa 2850new_tlist (struct tlist *next, tree t, tree writer)
235cfbc4
BS
2851{
2852 struct tlist *l;
5d038c4c 2853 l = XOBNEW (&tlist_obstack, struct tlist);
235cfbc4
BS
2854 l->next = next;
2855 l->expr = t;
2856 l->writer = writer;
2857 return l;
2858}
2859
2860/* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
2861 is nonnull, we ignore any node we find which has a writer equal to it. */
2862
2863static void
35b1a6fa 2864add_tlist (struct tlist **to, struct tlist *add, tree exclude_writer, int copy)
235cfbc4
BS
2865{
2866 while (add)
2867 {
2868 struct tlist *next = add->next;
3f75a254 2869 if (!copy)
235cfbc4 2870 add->next = *to;
1e4ae551 2871 if (!exclude_writer || !candidate_equal_p (add->writer, exclude_writer))
235cfbc4
BS
2872 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
2873 add = next;
2874 }
2875}
2876
2877/* Merge the nodes of ADD into TO. This merging process is done so that for
2878 each variable that already exists in TO, no new node is added; however if
2879 there is a write access recorded in ADD, and an occurrence on TO is only
2880 a read access, then the occurrence in TO will be modified to record the
2881 write. */
2683ed8d
BS
2882
2883static void
35b1a6fa 2884merge_tlist (struct tlist **to, struct tlist *add, int copy)
235cfbc4
BS
2885{
2886 struct tlist **end = to;
2887
2888 while (*end)
2889 end = &(*end)->next;
2890
2891 while (add)
2892 {
2893 int found = 0;
2894 struct tlist *tmp2;
2895 struct tlist *next = add->next;
2896
2897 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1e4ae551 2898 if (candidate_equal_p (tmp2->expr, add->expr))
235cfbc4
BS
2899 {
2900 found = 1;
3f75a254 2901 if (!tmp2->writer)
235cfbc4
BS
2902 tmp2->writer = add->writer;
2903 }
3f75a254 2904 if (!found)
235cfbc4 2905 {
c2bf53a1 2906 *end = copy ? new_tlist (NULL, add->expr, add->writer) : add;
235cfbc4
BS
2907 end = &(*end)->next;
2908 *end = 0;
2909 }
2910 add = next;
2911 }
2912}
2913
2914/* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
2915 references in list LIST conflict with it, excluding reads if ONLY writers
2916 is nonzero. */
2917
2918static void
35b1a6fa
AJ
2919warn_for_collisions_1 (tree written, tree writer, struct tlist *list,
2920 int only_writes)
235cfbc4
BS
2921{
2922 struct tlist *tmp;
2923
2924 /* Avoid duplicate warnings. */
2925 for (tmp = warned_ids; tmp; tmp = tmp->next)
1e4ae551 2926 if (candidate_equal_p (tmp->expr, written))
235cfbc4
BS
2927 return;
2928
2929 while (list)
2930 {
1e4ae551
MLI
2931 if (candidate_equal_p (list->expr, written)
2932 && !candidate_equal_p (list->writer, writer)
2933 && (!only_writes || list->writer))
235cfbc4
BS
2934 {
2935 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
8400e75e 2936 warning_at (EXPR_LOC_OR_LOC (writer, input_location),
ca085fd7
MLI
2937 OPT_Wsequence_point, "operation on %qE may be undefined",
2938 list->expr);
235cfbc4
BS
2939 }
2940 list = list->next;
2941 }
2942}
2943
2944/* Given a list LIST of references to variables, find whether any of these
2945 can cause conflicts due to missing sequence points. */
2946
2947static void
35b1a6fa 2948warn_for_collisions (struct tlist *list)
235cfbc4
BS
2949{
2950 struct tlist *tmp;
35b1a6fa 2951
235cfbc4
BS
2952 for (tmp = list; tmp; tmp = tmp->next)
2953 {
2954 if (tmp->writer)
2955 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
2956 }
2957}
2958
684d9f3b 2959/* Return nonzero if X is a tree that can be verified by the sequence point
235cfbc4
BS
2960 warnings. */
2961static int
35b1a6fa 2962warning_candidate_p (tree x)
2683ed8d 2963{
07078664
JJ
2964 if (DECL_P (x) && DECL_ARTIFICIAL (x))
2965 return 0;
2966
92e948a8
NF
2967 if (TREE_CODE (x) == BLOCK)
2968 return 0;
2969
07078664 2970 /* VOID_TYPE_P (TREE_TYPE (x)) is workaround for cp/tree.c
1e4ae551 2971 (lvalue_p) crash on TRY/CATCH. */
07078664
JJ
2972 if (TREE_TYPE (x) == NULL_TREE || VOID_TYPE_P (TREE_TYPE (x)))
2973 return 0;
2974
2975 if (!lvalue_p (x))
2976 return 0;
2977
2978 /* No point to track non-const calls, they will never satisfy
2979 operand_equal_p. */
2980 if (TREE_CODE (x) == CALL_EXPR && (call_expr_flags (x) & ECF_CONST) == 0)
2981 return 0;
2982
2983 if (TREE_CODE (x) == STRING_CST)
2984 return 0;
2985
2986 return 1;
1e4ae551
MLI
2987}
2988
2989/* Return nonzero if X and Y appear to be the same candidate (or NULL) */
2990static bool
2991candidate_equal_p (const_tree x, const_tree y)
2992{
2993 return (x == y) || (x && y && operand_equal_p (x, y, 0));
235cfbc4 2994}
2683ed8d 2995
235cfbc4
BS
2996/* Walk the tree X, and record accesses to variables. If X is written by the
2997 parent tree, WRITER is the parent.
2998 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
2999 expression or its only operand forces a sequence point, then everything up
3000 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
3001 in PNO_SP.
3002 Once we return, we will have emitted warnings if any subexpression before
3003 such a sequence point could be undefined. On a higher level, however, the
3004 sequence point may not be relevant, and we'll merge the two lists.
3005
3006 Example: (b++, a) + b;
3007 The call that processes the COMPOUND_EXPR will store the increment of B
3008 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
3009 processes the PLUS_EXPR will need to merge the two lists so that
3010 eventually, all accesses end up on the same list (and we'll warn about the
3011 unordered subexpressions b++ and b.
3012
3013 A note on merging. If we modify the former example so that our expression
3014 becomes
3015 (b++, b) + a
3016 care must be taken not simply to add all three expressions into the final
3017 PNO_SP list. The function merge_tlist takes care of that by merging the
3018 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
3019 way, so that no more than one access to B is recorded. */
2683ed8d 3020
235cfbc4 3021static void
35b1a6fa
AJ
3022verify_tree (tree x, struct tlist **pbefore_sp, struct tlist **pno_sp,
3023 tree writer)
235cfbc4
BS
3024{
3025 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
3026 enum tree_code code;
6615c446 3027 enum tree_code_class cl;
2683ed8d 3028
f9e1917e
JM
3029 /* X may be NULL if it is the operand of an empty statement expression
3030 ({ }). */
3031 if (x == NULL)
3032 return;
3033
235cfbc4
BS
3034 restart:
3035 code = TREE_CODE (x);
e3a64162 3036 cl = TREE_CODE_CLASS (code);
2683ed8d 3037
235cfbc4 3038 if (warning_candidate_p (x))
1e4ae551 3039 *pno_sp = new_tlist (*pno_sp, x, writer);
235cfbc4
BS
3040
3041 switch (code)
3042 {
52a84e42 3043 case CONSTRUCTOR:
f7716d57 3044 case SIZEOF_EXPR:
52a84e42
BS
3045 return;
3046
235cfbc4
BS
3047 case COMPOUND_EXPR:
3048 case TRUTH_ANDIF_EXPR:
3049 case TRUTH_ORIF_EXPR:
a25bd9e6 3050 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
235cfbc4
BS
3051 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3052 warn_for_collisions (tmp_nosp);
3053 merge_tlist (pbefore_sp, tmp_before, 0);
3054 merge_tlist (pbefore_sp, tmp_nosp, 0);
a25bd9e6
JM
3055 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_list2, NULL_TREE);
3056 warn_for_collisions (tmp_list2);
235cfbc4 3057 merge_tlist (pbefore_sp, tmp_list3, 0);
a25bd9e6 3058 merge_tlist (pno_sp, tmp_list2, 0);
235cfbc4
BS
3059 return;
3060
3061 case COND_EXPR:
3062 tmp_before = tmp_list2 = 0;
3063 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
3064 warn_for_collisions (tmp_list2);
3065 merge_tlist (pbefore_sp, tmp_before, 0);
c2bf53a1 3066 merge_tlist (pbefore_sp, tmp_list2, 0);
235cfbc4
BS
3067
3068 tmp_list3 = tmp_nosp = 0;
3069 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
3070 warn_for_collisions (tmp_nosp);
3071 merge_tlist (pbefore_sp, tmp_list3, 0);
3072
3073 tmp_list3 = tmp_list2 = 0;
3074 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
3075 warn_for_collisions (tmp_list2);
3076 merge_tlist (pbefore_sp, tmp_list3, 0);
3077 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
3078 two first, to avoid warning for (a ? b++ : b++). */
3079 merge_tlist (&tmp_nosp, tmp_list2, 0);
3080 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3081 return;
3082
2683ed8d
BS
3083 case PREDECREMENT_EXPR:
3084 case PREINCREMENT_EXPR:
3085 case POSTDECREMENT_EXPR:
3086 case POSTINCREMENT_EXPR:
235cfbc4
BS
3087 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
3088 return;
3089
3090 case MODIFY_EXPR:
3091 tmp_before = tmp_nosp = tmp_list3 = 0;
3092 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
3093 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
3094 /* Expressions inside the LHS are not ordered wrt. the sequence points
3095 in the RHS. Example:
3096 *a = (a++, 2)
3097 Despite the fact that the modification of "a" is in the before_sp
3098 list (tmp_before), it conflicts with the use of "a" in the LHS.
3099 We can handle this by adding the contents of tmp_list3
3100 to those of tmp_before, and redoing the collision warnings for that
3101 list. */
3102 add_tlist (&tmp_before, tmp_list3, x, 1);
3103 warn_for_collisions (tmp_before);
3104 /* Exclude the LHS itself here; we first have to merge it into the
3105 tmp_nosp list. This is done to avoid warning for "a = a"; if we
3106 didn't exclude the LHS, we'd get it twice, once as a read and once
3107 as a write. */
3108 add_tlist (pno_sp, tmp_list3, x, 0);
3109 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
3110
3111 merge_tlist (pbefore_sp, tmp_before, 0);
3112 if (warning_candidate_p (TREE_OPERAND (x, 0)))
3113 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
3114 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
3115 return;
2683ed8d
BS
3116
3117 case CALL_EXPR:
235cfbc4
BS
3118 /* We need to warn about conflicts among arguments and conflicts between
3119 args and the function address. Side effects of the function address,
3120 however, are not ordered by the sequence point of the call. */
5039610b
SL
3121 {
3122 call_expr_arg_iterator iter;
3123 tree arg;
b8698a0f 3124 tmp_before = tmp_nosp = 0;
5039610b
SL
3125 verify_tree (CALL_EXPR_FN (x), &tmp_before, &tmp_nosp, NULL_TREE);
3126 FOR_EACH_CALL_EXPR_ARG (arg, iter, x)
3127 {
3128 tmp_list2 = tmp_list3 = 0;
3129 verify_tree (arg, &tmp_list2, &tmp_list3, NULL_TREE);
3130 merge_tlist (&tmp_list3, tmp_list2, 0);
3131 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
3132 }
3133 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
3134 warn_for_collisions (tmp_before);
3135 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
3136 return;
3137 }
2683ed8d
BS
3138
3139 case TREE_LIST:
3140 /* Scan all the list, e.g. indices of multi dimensional array. */
3141 while (x)
3142 {
235cfbc4
BS
3143 tmp_before = tmp_nosp = 0;
3144 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
3145 merge_tlist (&tmp_nosp, tmp_before, 0);
3146 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
2683ed8d
BS
3147 x = TREE_CHAIN (x);
3148 }
235cfbc4 3149 return;
2683ed8d 3150
235cfbc4
BS
3151 case SAVE_EXPR:
3152 {
3153 struct tlist_cache *t;
3154 for (t = save_expr_cache; t; t = t->next)
1e4ae551 3155 if (candidate_equal_p (t->expr, x))
235cfbc4 3156 break;
2683ed8d 3157
3f75a254 3158 if (!t)
2683ed8d 3159 {
5d038c4c 3160 t = XOBNEW (&tlist_obstack, struct tlist_cache);
235cfbc4
BS
3161 t->next = save_expr_cache;
3162 t->expr = x;
3163 save_expr_cache = t;
3164
3165 tmp_before = tmp_nosp = 0;
3166 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
3167 warn_for_collisions (tmp_nosp);
3168
3169 tmp_list3 = 0;
c2bf53a1 3170 merge_tlist (&tmp_list3, tmp_nosp, 0);
235cfbc4
BS
3171 t->cache_before_sp = tmp_before;
3172 t->cache_after_sp = tmp_list3;
2683ed8d 3173 }
235cfbc4
BS
3174 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
3175 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
3176 return;
3177 }
2683ed8d 3178
528c22f4
MLI
3179 case ADDR_EXPR:
3180 x = TREE_OPERAND (x, 0);
3181 if (DECL_P (x))
3182 return;
3183 writer = 0;
3184 goto restart;
3185
6615c446
JO
3186 default:
3187 /* For other expressions, simply recurse on their operands.
c22cacf3 3188 Manual tail recursion for unary expressions.
6615c446
JO
3189 Other non-expressions need not be processed. */
3190 if (cl == tcc_unary)
3191 {
6615c446
JO
3192 x = TREE_OPERAND (x, 0);
3193 writer = 0;
3194 goto restart;
3195 }
3196 else if (IS_EXPR_CODE_CLASS (cl))
3197 {
3198 int lp;
5039610b 3199 int max = TREE_OPERAND_LENGTH (x);
6615c446
JO
3200 for (lp = 0; lp < max; lp++)
3201 {
3202 tmp_before = tmp_nosp = 0;
3203 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, 0);
3204 merge_tlist (&tmp_nosp, tmp_before, 0);
3205 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
3206 }
3207 }
3208 return;
2683ed8d 3209 }
2683ed8d
BS
3210}
3211
8d9afc4e 3212/* Try to warn for undefined behavior in EXPR due to missing sequence
2683ed8d
BS
3213 points. */
3214
24e47c76 3215DEBUG_FUNCTION void
35b1a6fa 3216verify_sequence_points (tree expr)
2683ed8d 3217{
235cfbc4 3218 struct tlist *before_sp = 0, *after_sp = 0;
2683ed8d 3219
235cfbc4
BS
3220 warned_ids = 0;
3221 save_expr_cache = 0;
3222 if (tlist_firstobj == 0)
2683ed8d 3223 {
235cfbc4 3224 gcc_obstack_init (&tlist_obstack);
28dab132 3225 tlist_firstobj = (char *) obstack_alloc (&tlist_obstack, 0);
2683ed8d
BS
3226 }
3227
235cfbc4
BS
3228 verify_tree (expr, &before_sp, &after_sp, 0);
3229 warn_for_collisions (after_sp);
3230 obstack_free (&tlist_obstack, tlist_firstobj);
2683ed8d 3231}
b30f223b
RS
3232\f
3233/* Validate the expression after `case' and apply default promotions. */
3234
a6c0a76c 3235static tree
62e4eb35 3236check_case_value (location_t loc, tree value)
b30f223b
RS
3237{
3238 if (value == NULL_TREE)
3239 return value;
3240
522ddfa2
JM
3241 if (TREE_CODE (value) == INTEGER_CST)
3242 /* Promote char or short to int. */
3243 value = perform_integral_promotions (value);
3244 else if (value != error_mark_node)
b30f223b 3245 {
62e4eb35 3246 error_at (loc, "case label does not reduce to an integer constant");
b30f223b
RS
3247 value = error_mark_node;
3248 }
b30f223b 3249
bc690db1
RS
3250 constant_expression_warning (value);
3251
b30f223b
RS
3252 return value;
3253}
3254\f
a6c0a76c 3255/* See if the case values LOW and HIGH are in the range of the original
89dbed81 3256 type (i.e. before the default conversion to int) of the switch testing
a6c0a76c
SB
3257 expression.
3258 TYPE is the promoted type of the testing expression, and ORIG_TYPE is
2a7e31df 3259 the type before promoting it. CASE_LOW_P is a pointer to the lower
a6c0a76c
SB
3260 bound of the case label, and CASE_HIGH_P is the upper bound or NULL
3261 if the case is not a case range.
3262 The caller has to make sure that we are not called with NULL for
b155cfd9
MP
3263 CASE_LOW_P (i.e. the default case). OUTSIDE_RANGE_P says whether there
3264 was a case value that doesn't fit into the range of the ORIG_TYPE.
0fa2e4df 3265 Returns true if the case label is in range of ORIG_TYPE (saturated or
a6c0a76c
SB
3266 untouched) or false if the label is out of range. */
3267
3268static bool
9d548dfb 3269check_case_bounds (location_t loc, tree type, tree orig_type,
b155cfd9
MP
3270 tree *case_low_p, tree *case_high_p,
3271 bool *outside_range_p)
a6c0a76c
SB
3272{
3273 tree min_value, max_value;
3274 tree case_low = *case_low_p;
3275 tree case_high = case_high_p ? *case_high_p : case_low;
3276
3277 /* If there was a problem with the original type, do nothing. */
3278 if (orig_type == error_mark_node)
3279 return true;
3280
3281 min_value = TYPE_MIN_VALUE (orig_type);
3282 max_value = TYPE_MAX_VALUE (orig_type);
3283
0f62c7a0
MP
3284 /* We'll really need integer constants here. */
3285 case_low = fold (case_low);
3286 case_high = fold (case_high);
3287
a6c0a76c
SB
3288 /* Case label is less than minimum for type. */
3289 if (tree_int_cst_compare (case_low, min_value) < 0
3290 && tree_int_cst_compare (case_high, min_value) < 0)
3291 {
9d548dfb
MP
3292 warning_at (loc, 0, "case label value is less than minimum value "
3293 "for type");
b155cfd9 3294 *outside_range_p = true;
a6c0a76c
SB
3295 return false;
3296 }
9f63daea 3297
a6c0a76c
SB
3298 /* Case value is greater than maximum for type. */
3299 if (tree_int_cst_compare (case_low, max_value) > 0
3300 && tree_int_cst_compare (case_high, max_value) > 0)
3301 {
9d548dfb 3302 warning_at (loc, 0, "case label value exceeds maximum value for type");
b155cfd9 3303 *outside_range_p = true;
a6c0a76c
SB
3304 return false;
3305 }
3306
3307 /* Saturate lower case label value to minimum. */
3308 if (tree_int_cst_compare (case_high, min_value) >= 0
3309 && tree_int_cst_compare (case_low, min_value) < 0)
3310 {
9d548dfb
MP
3311 warning_at (loc, 0, "lower value in case label range"
3312 " less than minimum value for type");
b155cfd9 3313 *outside_range_p = true;
a6c0a76c
SB
3314 case_low = min_value;
3315 }
9f63daea 3316
a6c0a76c
SB
3317 /* Saturate upper case label value to maximum. */
3318 if (tree_int_cst_compare (case_low, max_value) <= 0
3319 && tree_int_cst_compare (case_high, max_value) > 0)
3320 {
9d548dfb
MP
3321 warning_at (loc, 0, "upper value in case label range"
3322 " exceeds maximum value for type");
b155cfd9 3323 *outside_range_p = true;
a6c0a76c
SB
3324 case_high = max_value;
3325 }
3326
3327 if (*case_low_p != case_low)
3328 *case_low_p = convert (type, case_low);
3329 if (case_high_p && *case_high_p != case_high)
3330 *case_high_p = convert (type, case_high);
3331
3332 return true;
3333}
3334\f
b30f223b
RS
3335/* Return an integer type with BITS bits of precision,
3336 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
3337
3338tree
35b1a6fa 3339c_common_type_for_size (unsigned int bits, int unsignedp)
b30f223b 3340{
78a7c317
DD
3341 int i;
3342
a311b52c
JM
3343 if (bits == TYPE_PRECISION (integer_type_node))
3344 return unsignedp ? unsigned_type_node : integer_type_node;
3345
3fc7e390 3346 if (bits == TYPE_PRECISION (signed_char_type_node))
b30f223b
RS
3347 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3348
3fc7e390 3349 if (bits == TYPE_PRECISION (short_integer_type_node))
b30f223b
RS
3350 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3351
3fc7e390 3352 if (bits == TYPE_PRECISION (long_integer_type_node))
b30f223b
RS
3353 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3354
3fc7e390 3355 if (bits == TYPE_PRECISION (long_long_integer_type_node))
b30f223b
RS
3356 return (unsignedp ? long_long_unsigned_type_node
3357 : long_long_integer_type_node);
3358
78a7c317
DD
3359 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3360 if (int_n_enabled_p[i]
3361 && bits == int_n_data[i].bitsize)
3362 return (unsignedp ? int_n_trees[i].unsigned_type
3363 : int_n_trees[i].signed_type);
a6766312 3364
835f9b4d
GRK
3365 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
3366 return (unsignedp ? widest_unsigned_literal_type_node
3367 : widest_integer_literal_type_node);
3368
3fc7e390
RS
3369 if (bits <= TYPE_PRECISION (intQI_type_node))
3370 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3371
3372 if (bits <= TYPE_PRECISION (intHI_type_node))
3373 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3374
3375 if (bits <= TYPE_PRECISION (intSI_type_node))
3376 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3377
3378 if (bits <= TYPE_PRECISION (intDI_type_node))
3379 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3380
b30f223b
RS
3381 return 0;
3382}
3383
ab22c1fa
CF
3384/* Return a fixed-point type that has at least IBIT ibits and FBIT fbits
3385 that is unsigned if UNSIGNEDP is nonzero, otherwise signed;
3386 and saturating if SATP is nonzero, otherwise not saturating. */
3387
3388tree
3389c_common_fixed_point_type_for_size (unsigned int ibit, unsigned int fbit,
3390 int unsignedp, int satp)
3391{
ef4bddc2 3392 machine_mode mode;
ab22c1fa
CF
3393 if (ibit == 0)
3394 mode = unsignedp ? UQQmode : QQmode;
3395 else
3396 mode = unsignedp ? UHAmode : HAmode;
3397
3398 for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3399 if (GET_MODE_IBIT (mode) >= ibit && GET_MODE_FBIT (mode) >= fbit)
3400 break;
3401
3402 if (mode == VOIDmode || !targetm.scalar_mode_supported_p (mode))
3403 {
3404 sorry ("GCC cannot support operators with integer types and "
3405 "fixed-point types that have too many integral and "
3406 "fractional bits together");
3407 return 0;
3408 }
3409
3410 return c_common_type_for_mode (mode, satp);
3411}
3412
d1d3865f
ZW
3413/* Used for communication between c_common_type_for_mode and
3414 c_register_builtin_type. */
793c625f 3415tree registered_builtin_types;
d1d3865f 3416
b30f223b
RS
3417/* Return a data type that has machine mode MODE.
3418 If the mode is an integer,
ab22c1fa
CF
3419 then UNSIGNEDP selects between signed and unsigned types.
3420 If the mode is a fixed-point mode,
3421 then UNSIGNEDP selects between saturating and nonsaturating types. */
b30f223b
RS
3422
3423tree
ef4bddc2 3424c_common_type_for_mode (machine_mode mode, int unsignedp)
b30f223b 3425{
d1d3865f 3426 tree t;
78a7c317 3427 int i;
d1d3865f 3428
a311b52c
JM
3429 if (mode == TYPE_MODE (integer_type_node))
3430 return unsignedp ? unsigned_type_node : integer_type_node;
3431
b30f223b
RS
3432 if (mode == TYPE_MODE (signed_char_type_node))
3433 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3434
3435 if (mode == TYPE_MODE (short_integer_type_node))
3436 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3437
b30f223b
RS
3438 if (mode == TYPE_MODE (long_integer_type_node))
3439 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3440
3441 if (mode == TYPE_MODE (long_long_integer_type_node))
3442 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
3443
78a7c317
DD
3444 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3445 if (int_n_enabled_p[i]
3446 && mode == int_n_data[i].m)
3447 return (unsignedp ? int_n_trees[i].unsigned_type
3448 : int_n_trees[i].signed_type);
a6766312 3449
835f9b4d 3450 if (mode == TYPE_MODE (widest_integer_literal_type_node))
d125d268 3451 return unsignedp ? widest_unsigned_literal_type_node
6de9cd9a 3452 : widest_integer_literal_type_node;
835f9b4d 3453
0afeef64 3454 if (mode == QImode)
3fc7e390
RS
3455 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3456
0afeef64 3457 if (mode == HImode)
3fc7e390
RS
3458 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3459
0afeef64 3460 if (mode == SImode)
3fc7e390
RS
3461 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3462
0afeef64 3463 if (mode == DImode)
3fc7e390
RS
3464 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3465
21a9616b 3466#if HOST_BITS_PER_WIDE_INT >= 64
a6d7e156
JL
3467 if (mode == TYPE_MODE (intTI_type_node))
3468 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
21a9616b 3469#endif
a6d7e156 3470
b30f223b
RS
3471 if (mode == TYPE_MODE (float_type_node))
3472 return float_type_node;
3473
3474 if (mode == TYPE_MODE (double_type_node))
3475 return double_type_node;
3476
3477 if (mode == TYPE_MODE (long_double_type_node))
3478 return long_double_type_node;
3479
ff42324e
NS
3480 if (mode == TYPE_MODE (void_type_node))
3481 return void_type_node;
9f63daea 3482
b30f223b 3483 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
19b3ffbc
DD
3484 return (unsignedp
3485 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3486 : make_signed_type (GET_MODE_PRECISION (mode)));
b30f223b
RS
3487
3488 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
19b3ffbc
DD
3489 return (unsignedp
3490 ? make_unsigned_type (GET_MODE_PRECISION (mode))
3491 : make_signed_type (GET_MODE_PRECISION (mode)));
b30f223b 3492
7e7e470f
RH
3493 if (COMPLEX_MODE_P (mode))
3494 {
ef4bddc2 3495 machine_mode inner_mode;
7e7e470f
RH
3496 tree inner_type;
3497
3498 if (mode == TYPE_MODE (complex_float_type_node))
3499 return complex_float_type_node;
3500 if (mode == TYPE_MODE (complex_double_type_node))
3501 return complex_double_type_node;
3502 if (mode == TYPE_MODE (complex_long_double_type_node))
3503 return complex_long_double_type_node;
3504
3505 if (mode == TYPE_MODE (complex_integer_type_node) && !unsignedp)
3506 return complex_integer_type_node;
3507
3508 inner_mode = GET_MODE_INNER (mode);
3509 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3510 if (inner_type != NULL_TREE)
3511 return build_complex_type (inner_type);
3512 }
3513 else if (VECTOR_MODE_P (mode))
4a5eab38 3514 {
ef4bddc2 3515 machine_mode inner_mode = GET_MODE_INNER (mode);
4a5eab38
PB
3516 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
3517 if (inner_type != NULL_TREE)
3518 return build_vector_type_for_mode (inner_type, mode);
0afeef64 3519 }
4061f623 3520
9a8ce21f
JG
3521 if (mode == TYPE_MODE (dfloat32_type_node))
3522 return dfloat32_type_node;
3523 if (mode == TYPE_MODE (dfloat64_type_node))
3524 return dfloat64_type_node;
3525 if (mode == TYPE_MODE (dfloat128_type_node))
3526 return dfloat128_type_node;
3527
ab22c1fa
CF
3528 if (ALL_SCALAR_FIXED_POINT_MODE_P (mode))
3529 {
3530 if (mode == TYPE_MODE (short_fract_type_node))
3531 return unsignedp ? sat_short_fract_type_node : short_fract_type_node;
3532 if (mode == TYPE_MODE (fract_type_node))
3533 return unsignedp ? sat_fract_type_node : fract_type_node;
3534 if (mode == TYPE_MODE (long_fract_type_node))
3535 return unsignedp ? sat_long_fract_type_node : long_fract_type_node;
3536 if (mode == TYPE_MODE (long_long_fract_type_node))
3537 return unsignedp ? sat_long_long_fract_type_node
3538 : long_long_fract_type_node;
3539
3540 if (mode == TYPE_MODE (unsigned_short_fract_type_node))
3541 return unsignedp ? sat_unsigned_short_fract_type_node
3542 : unsigned_short_fract_type_node;
3543 if (mode == TYPE_MODE (unsigned_fract_type_node))
3544 return unsignedp ? sat_unsigned_fract_type_node
3545 : unsigned_fract_type_node;
3546 if (mode == TYPE_MODE (unsigned_long_fract_type_node))
3547 return unsignedp ? sat_unsigned_long_fract_type_node
3548 : unsigned_long_fract_type_node;
3549 if (mode == TYPE_MODE (unsigned_long_long_fract_type_node))
3550 return unsignedp ? sat_unsigned_long_long_fract_type_node
3551 : unsigned_long_long_fract_type_node;
3552
3553 if (mode == TYPE_MODE (short_accum_type_node))
3554 return unsignedp ? sat_short_accum_type_node : short_accum_type_node;
3555 if (mode == TYPE_MODE (accum_type_node))
3556 return unsignedp ? sat_accum_type_node : accum_type_node;
3557 if (mode == TYPE_MODE (long_accum_type_node))
3558 return unsignedp ? sat_long_accum_type_node : long_accum_type_node;
3559 if (mode == TYPE_MODE (long_long_accum_type_node))
3560 return unsignedp ? sat_long_long_accum_type_node
3561 : long_long_accum_type_node;
3562
3563 if (mode == TYPE_MODE (unsigned_short_accum_type_node))
3564 return unsignedp ? sat_unsigned_short_accum_type_node
3565 : unsigned_short_accum_type_node;
3566 if (mode == TYPE_MODE (unsigned_accum_type_node))
3567 return unsignedp ? sat_unsigned_accum_type_node
3568 : unsigned_accum_type_node;
3569 if (mode == TYPE_MODE (unsigned_long_accum_type_node))
3570 return unsignedp ? sat_unsigned_long_accum_type_node
3571 : unsigned_long_accum_type_node;
3572 if (mode == TYPE_MODE (unsigned_long_long_accum_type_node))
3573 return unsignedp ? sat_unsigned_long_long_accum_type_node
3574 : unsigned_long_long_accum_type_node;
3575
3576 if (mode == QQmode)
3577 return unsignedp ? sat_qq_type_node : qq_type_node;
3578 if (mode == HQmode)
3579 return unsignedp ? sat_hq_type_node : hq_type_node;
3580 if (mode == SQmode)
3581 return unsignedp ? sat_sq_type_node : sq_type_node;
3582 if (mode == DQmode)
3583 return unsignedp ? sat_dq_type_node : dq_type_node;
3584 if (mode == TQmode)
3585 return unsignedp ? sat_tq_type_node : tq_type_node;
3586
3587 if (mode == UQQmode)
3588 return unsignedp ? sat_uqq_type_node : uqq_type_node;
3589 if (mode == UHQmode)
3590 return unsignedp ? sat_uhq_type_node : uhq_type_node;
3591 if (mode == USQmode)
3592 return unsignedp ? sat_usq_type_node : usq_type_node;
3593 if (mode == UDQmode)
3594 return unsignedp ? sat_udq_type_node : udq_type_node;
3595 if (mode == UTQmode)
3596 return unsignedp ? sat_utq_type_node : utq_type_node;
3597
3598 if (mode == HAmode)
3599 return unsignedp ? sat_ha_type_node : ha_type_node;
3600 if (mode == SAmode)
3601 return unsignedp ? sat_sa_type_node : sa_type_node;
3602 if (mode == DAmode)
3603 return unsignedp ? sat_da_type_node : da_type_node;
3604 if (mode == TAmode)
3605 return unsignedp ? sat_ta_type_node : ta_type_node;
3606
3607 if (mode == UHAmode)
3608 return unsignedp ? sat_uha_type_node : uha_type_node;
3609 if (mode == USAmode)
3610 return unsignedp ? sat_usa_type_node : usa_type_node;
3611 if (mode == UDAmode)
3612 return unsignedp ? sat_uda_type_node : uda_type_node;
3613 if (mode == UTAmode)
3614 return unsignedp ? sat_uta_type_node : uta_type_node;
3615 }
3616
d1d3865f 3617 for (t = registered_builtin_types; t; t = TREE_CHAIN (t))
7a421706
MS
3618 if (TYPE_MODE (TREE_VALUE (t)) == mode
3619 && !!unsignedp == !!TYPE_UNSIGNED (TREE_VALUE (t)))
d1d3865f
ZW
3620 return TREE_VALUE (t);
3621
b30f223b
RS
3622 return 0;
3623}
693a6128 3624
12753674
RE
3625tree
3626c_common_unsigned_type (tree type)
3627{
3628 return c_common_signed_or_unsigned_type (1, type);
3629}
3630
693a6128
GRK
3631/* Return a signed type the same as TYPE in other respects. */
3632
3633tree
35b1a6fa 3634c_common_signed_type (tree type)
693a6128 3635{
ceef8ce4 3636 return c_common_signed_or_unsigned_type (0, type);
693a6128
GRK
3637}
3638
3639/* Return a type the same as TYPE except unsigned or
3640 signed according to UNSIGNEDP. */
3641
3642tree
35b1a6fa 3643c_common_signed_or_unsigned_type (int unsignedp, tree type)
693a6128 3644{
c74a03d2 3645 tree type1;
78a7c317 3646 int i;
693a6128 3647
c74a03d2
RAE
3648 /* This block of code emulates the behavior of the old
3649 c_common_unsigned_type. In particular, it returns
3650 long_unsigned_type_node if passed a long, even when a int would
3651 have the same size. This is necessary for warnings to work
3652 correctly in archs where sizeof(int) == sizeof(long) */
3653
3654 type1 = TYPE_MAIN_VARIANT (type);
3655 if (type1 == signed_char_type_node || type1 == char_type_node || type1 == unsigned_char_type_node)
3656 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3657 if (type1 == integer_type_node || type1 == unsigned_type_node)
3658 return unsignedp ? unsigned_type_node : integer_type_node;
3659 if (type1 == short_integer_type_node || type1 == short_unsigned_type_node)
3660 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3661 if (type1 == long_integer_type_node || type1 == long_unsigned_type_node)
3662 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3663 if (type1 == long_long_integer_type_node || type1 == long_long_unsigned_type_node)
3664 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
78a7c317
DD
3665
3666 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3667 if (int_n_enabled_p[i]
3668 && (type1 == int_n_trees[i].unsigned_type
3669 || type1 == int_n_trees[i].signed_type))
3670 return (unsignedp ? int_n_trees[i].unsigned_type
3671 : int_n_trees[i].signed_type);
3672
c74a03d2
RAE
3673 if (type1 == widest_integer_literal_type_node || type1 == widest_unsigned_literal_type_node)
3674 return unsignedp ? widest_unsigned_literal_type_node : widest_integer_literal_type_node;
3675#if HOST_BITS_PER_WIDE_INT >= 64
3676 if (type1 == intTI_type_node || type1 == unsigned_intTI_type_node)
3677 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3678#endif
3679 if (type1 == intDI_type_node || type1 == unsigned_intDI_type_node)
3680 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
3681 if (type1 == intSI_type_node || type1 == unsigned_intSI_type_node)
3682 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
3683 if (type1 == intHI_type_node || type1 == unsigned_intHI_type_node)
3684 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
3685 if (type1 == intQI_type_node || type1 == unsigned_intQI_type_node)
3686 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
3687
70d3fcab
AH
3688#define C_COMMON_FIXED_TYPES(NAME) \
3689 if (type1 == short_ ## NAME ## _type_node \
3690 || type1 == unsigned_short_ ## NAME ## _type_node) \
3691 return unsignedp ? unsigned_short_ ## NAME ## _type_node \
3692 : short_ ## NAME ## _type_node; \
3693 if (type1 == NAME ## _type_node \
3694 || type1 == unsigned_ ## NAME ## _type_node) \
3695 return unsignedp ? unsigned_ ## NAME ## _type_node \
3696 : NAME ## _type_node; \
3697 if (type1 == long_ ## NAME ## _type_node \
3698 || type1 == unsigned_long_ ## NAME ## _type_node) \
3699 return unsignedp ? unsigned_long_ ## NAME ## _type_node \
3700 : long_ ## NAME ## _type_node; \
3701 if (type1 == long_long_ ## NAME ## _type_node \
3702 || type1 == unsigned_long_long_ ## NAME ## _type_node) \
3703 return unsignedp ? unsigned_long_long_ ## NAME ## _type_node \
3704 : long_long_ ## NAME ## _type_node;
3705
3706#define C_COMMON_FIXED_MODE_TYPES(NAME) \
3707 if (type1 == NAME ## _type_node \
3708 || type1 == u ## NAME ## _type_node) \
3709 return unsignedp ? u ## NAME ## _type_node \
3710 : NAME ## _type_node;
3711
3712#define C_COMMON_FIXED_TYPES_SAT(NAME) \
3713 if (type1 == sat_ ## short_ ## NAME ## _type_node \
3714 || type1 == sat_ ## unsigned_short_ ## NAME ## _type_node) \
3715 return unsignedp ? sat_ ## unsigned_short_ ## NAME ## _type_node \
3716 : sat_ ## short_ ## NAME ## _type_node; \
3717 if (type1 == sat_ ## NAME ## _type_node \
3718 || type1 == sat_ ## unsigned_ ## NAME ## _type_node) \
3719 return unsignedp ? sat_ ## unsigned_ ## NAME ## _type_node \
3720 : sat_ ## NAME ## _type_node; \
3721 if (type1 == sat_ ## long_ ## NAME ## _type_node \
3722 || type1 == sat_ ## unsigned_long_ ## NAME ## _type_node) \
3723 return unsignedp ? sat_ ## unsigned_long_ ## NAME ## _type_node \
3724 : sat_ ## long_ ## NAME ## _type_node; \
3725 if (type1 == sat_ ## long_long_ ## NAME ## _type_node \
3726 || type1 == sat_ ## unsigned_long_long_ ## NAME ## _type_node) \
3727 return unsignedp ? sat_ ## unsigned_long_long_ ## NAME ## _type_node \
3728 : sat_ ## long_long_ ## NAME ## _type_node;
3729
3730#define C_COMMON_FIXED_MODE_TYPES_SAT(NAME) \
3731 if (type1 == sat_ ## NAME ## _type_node \
3732 || type1 == sat_ ## u ## NAME ## _type_node) \
3733 return unsignedp ? sat_ ## u ## NAME ## _type_node \
3734 : sat_ ## NAME ## _type_node;
3735
3736 C_COMMON_FIXED_TYPES (fract);
3737 C_COMMON_FIXED_TYPES_SAT (fract);
3738 C_COMMON_FIXED_TYPES (accum);
3739 C_COMMON_FIXED_TYPES_SAT (accum);
3740
3741 C_COMMON_FIXED_MODE_TYPES (qq);
3742 C_COMMON_FIXED_MODE_TYPES (hq);
3743 C_COMMON_FIXED_MODE_TYPES (sq);
3744 C_COMMON_FIXED_MODE_TYPES (dq);
3745 C_COMMON_FIXED_MODE_TYPES (tq);
3746 C_COMMON_FIXED_MODE_TYPES_SAT (qq);
3747 C_COMMON_FIXED_MODE_TYPES_SAT (hq);
3748 C_COMMON_FIXED_MODE_TYPES_SAT (sq);
3749 C_COMMON_FIXED_MODE_TYPES_SAT (dq);
3750 C_COMMON_FIXED_MODE_TYPES_SAT (tq);
3751 C_COMMON_FIXED_MODE_TYPES (ha);
3752 C_COMMON_FIXED_MODE_TYPES (sa);
3753 C_COMMON_FIXED_MODE_TYPES (da);
3754 C_COMMON_FIXED_MODE_TYPES (ta);
3755 C_COMMON_FIXED_MODE_TYPES_SAT (ha);
3756 C_COMMON_FIXED_MODE_TYPES_SAT (sa);
3757 C_COMMON_FIXED_MODE_TYPES_SAT (da);
3758 C_COMMON_FIXED_MODE_TYPES_SAT (ta);
ab22c1fa 3759
bc15d0ef
JM
3760 /* For ENUMERAL_TYPEs in C++, must check the mode of the types, not
3761 the precision; they have precision set to match their range, but
3762 may use a wider mode to match an ABI. If we change modes, we may
3763 wind up with bad conversions. For INTEGER_TYPEs in C, must check
3764 the precision as well, so as to yield correct results for
3765 bit-field types. C++ does not have these separate bit-field
3766 types, and producing a signed or unsigned variant of an
3767 ENUMERAL_TYPE may cause other problems as well. */
3768
1e204133
RAE
3769 if (!INTEGRAL_TYPE_P (type)
3770 || TYPE_UNSIGNED (type) == unsignedp)
3771 return type;
3772
bc15d0ef
JM
3773#define TYPE_OK(node) \
3774 (TYPE_MODE (type) == TYPE_MODE (node) \
41b81065 3775 && TYPE_PRECISION (type) == TYPE_PRECISION (node))
bc15d0ef 3776 if (TYPE_OK (signed_char_type_node))
693a6128 3777 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
bc15d0ef 3778 if (TYPE_OK (integer_type_node))
693a6128 3779 return unsignedp ? unsigned_type_node : integer_type_node;
bc15d0ef 3780 if (TYPE_OK (short_integer_type_node))
693a6128 3781 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
bc15d0ef 3782 if (TYPE_OK (long_integer_type_node))
693a6128 3783 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
bc15d0ef 3784 if (TYPE_OK (long_long_integer_type_node))
693a6128
GRK
3785 return (unsignedp ? long_long_unsigned_type_node
3786 : long_long_integer_type_node);
78a7c317
DD
3787
3788 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3789 if (int_n_enabled_p[i]
3790 && TYPE_MODE (type) == int_n_data[i].m
3791 && TYPE_PRECISION (type) == int_n_data[i].bitsize)
3792 return (unsignedp ? int_n_trees[i].unsigned_type
3793 : int_n_trees[i].signed_type);
3794
bc15d0ef 3795 if (TYPE_OK (widest_integer_literal_type_node))
693a6128
GRK
3796 return (unsignedp ? widest_unsigned_literal_type_node
3797 : widest_integer_literal_type_node);
4a063bec
RH
3798
3799#if HOST_BITS_PER_WIDE_INT >= 64
bc15d0ef 3800 if (TYPE_OK (intTI_type_node))
4a063bec
RH
3801 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
3802#endif
bc15d0ef 3803 if (TYPE_OK (intDI_type_node))
4a063bec 3804 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
bc15d0ef 3805 if (TYPE_OK (intSI_type_node))
4a063bec 3806 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
bc15d0ef 3807 if (TYPE_OK (intHI_type_node))
4a063bec 3808 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
bc15d0ef 3809 if (TYPE_OK (intQI_type_node))
4a063bec 3810 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
bc15d0ef 3811#undef TYPE_OK
4a063bec 3812
41b81065 3813 return build_nonstandard_integer_type (TYPE_PRECISION (type), unsignedp);
693a6128 3814}
9649812a 3815
38a4afee
MM
3816/* Build a bit-field integer type for the given WIDTH and UNSIGNEDP. */
3817
3818tree
3819c_build_bitfield_integer_type (unsigned HOST_WIDE_INT width, int unsignedp)
3820{
78a7c317
DD
3821 int i;
3822
38a4afee
MM
3823 /* Extended integer types of the same width as a standard type have
3824 lesser rank, so those of the same width as int promote to int or
3825 unsigned int and are valid for printf formats expecting int or
3826 unsigned int. To avoid such special cases, avoid creating
3827 extended integer types for bit-fields if a standard integer type
3828 is available. */
3829 if (width == TYPE_PRECISION (integer_type_node))
3830 return unsignedp ? unsigned_type_node : integer_type_node;
3831 if (width == TYPE_PRECISION (signed_char_type_node))
3832 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
3833 if (width == TYPE_PRECISION (short_integer_type_node))
3834 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
3835 if (width == TYPE_PRECISION (long_integer_type_node))
3836 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
3837 if (width == TYPE_PRECISION (long_long_integer_type_node))
3838 return (unsignedp ? long_long_unsigned_type_node
3839 : long_long_integer_type_node);
78a7c317
DD
3840 for (i = 0; i < NUM_INT_N_ENTS; i ++)
3841 if (int_n_enabled_p[i]
3842 && width == int_n_data[i].bitsize)
3843 return (unsignedp ? int_n_trees[i].unsigned_type
3844 : int_n_trees[i].signed_type);
38a4afee
MM
3845 return build_nonstandard_integer_type (width, unsignedp);
3846}
3847
9649812a
MM
3848/* The C version of the register_builtin_type langhook. */
3849
3850void
3851c_register_builtin_type (tree type, const char* name)
3852{
3853 tree decl;
3854
c2255bc4
AH
3855 decl = build_decl (UNKNOWN_LOCATION,
3856 TYPE_DECL, get_identifier (name), type);
9649812a
MM
3857 DECL_ARTIFICIAL (decl) = 1;
3858 if (!TYPE_NAME (type))
3859 TYPE_NAME (type) = decl;
3860 pushdecl (decl);
d1d3865f
ZW
3861
3862 registered_builtin_types = tree_cons (0, type, registered_builtin_types);
9649812a 3863}
6acfe908 3864\f
78ef5b89 3865/* Print an error message for invalid operands to arith operation
ba47d38d 3866 CODE with TYPE0 for operand 0, and TYPE1 for operand 1.
745e411d
DM
3867 RICHLOC is a rich location for the message, containing either
3868 three separate locations for each of the operator and operands
3869
3870 lhs op rhs
3871 ~~~ ^~ ~~~
3872
3873 (C FE), or one location ranging over all over them
3874
3875 lhs op rhs
3876 ~~~~^~~~~~
3877
3878 (C++ FE). */
b30f223b
RS
3879
3880void
745e411d 3881binary_op_error (rich_location *richloc, enum tree_code code,
ba47d38d 3882 tree type0, tree type1)
b30f223b 3883{
b3694847 3884 const char *opname;
89c78d7d 3885
b30f223b
RS
3886 switch (code)
3887 {
b30f223b
RS
3888 case PLUS_EXPR:
3889 opname = "+"; break;
3890 case MINUS_EXPR:
3891 opname = "-"; break;
3892 case MULT_EXPR:
3893 opname = "*"; break;
3894 case MAX_EXPR:
3895 opname = "max"; break;
3896 case MIN_EXPR:
3897 opname = "min"; break;
3898 case EQ_EXPR:
3899 opname = "=="; break;
3900 case NE_EXPR:
3901 opname = "!="; break;
3902 case LE_EXPR:
3903 opname = "<="; break;
3904 case GE_EXPR:
3905 opname = ">="; break;
3906 case LT_EXPR:
3907 opname = "<"; break;
3908 case GT_EXPR:
3909 opname = ">"; break;
3910 case LSHIFT_EXPR:
3911 opname = "<<"; break;
3912 case RSHIFT_EXPR:
3913 opname = ">>"; break;
3914 case TRUNC_MOD_EXPR:
047de90b 3915 case FLOOR_MOD_EXPR:
b30f223b
RS
3916 opname = "%"; break;
3917 case TRUNC_DIV_EXPR:
047de90b 3918 case FLOOR_DIV_EXPR:
b30f223b
RS
3919 opname = "/"; break;
3920 case BIT_AND_EXPR:
3921 opname = "&"; break;
3922 case BIT_IOR_EXPR:
3923 opname = "|"; break;
3924 case TRUTH_ANDIF_EXPR:
3925 opname = "&&"; break;
3926 case TRUTH_ORIF_EXPR:
3927 opname = "||"; break;
3928 case BIT_XOR_EXPR:
3929 opname = "^"; break;
6d819282 3930 default:
37b2f290 3931 gcc_unreachable ();
b30f223b 3932 }
745e411d
DM
3933 error_at_rich_loc (richloc,
3934 "invalid operands to binary %s (have %qT and %qT)",
3935 opname, type0, type1);
b30f223b
RS
3936}
3937\f
50f305ca
MLI
3938/* Given an expression as a tree, return its original type. Do this
3939 by stripping any conversion that preserves the sign and precision. */
3940static tree
3941expr_original_type (tree expr)
3942{
3943 STRIP_SIGN_NOPS (expr);
3944 return TREE_TYPE (expr);
3945}
3946
b30f223b
RS
3947/* Subroutine of build_binary_op, used for comparison operations.
3948 See if the operands have both been converted from subword integer types
3949 and, if so, perhaps change them both back to their original type.
94dccd9d
RS
3950 This function is also responsible for converting the two operands
3951 to the proper common type for comparison.
b30f223b
RS
3952
3953 The arguments of this function are all pointers to local variables
3954 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
3955 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
3956
393e8e8b
MP
3957 LOC is the location of the comparison.
3958
b30f223b
RS
3959 If this function returns nonzero, it means that the comparison has
3960 a constant value. What this function returns is an expression for
3961 that value. */
3962
3963tree
393e8e8b
MP
3964shorten_compare (location_t loc, tree *op0_ptr, tree *op1_ptr,
3965 tree *restype_ptr, enum tree_code *rescode_ptr)
b30f223b 3966{
b3694847 3967 tree type;
b30f223b
RS
3968 tree op0 = *op0_ptr;
3969 tree op1 = *op1_ptr;
3970 int unsignedp0, unsignedp1;
3971 int real1, real2;
3972 tree primop0, primop1;
3973 enum tree_code code = *rescode_ptr;
3974
3975 /* Throw away any conversions to wider types
3976 already present in the operands. */
3977
828fb3ba
JM
3978 primop0 = c_common_get_narrower (op0, &unsignedp0);
3979 primop1 = c_common_get_narrower (op1, &unsignedp1);
b30f223b 3980
126e6609
JJ
3981 /* If primopN is first sign-extended from primopN's precision to opN's
3982 precision, then zero-extended from opN's precision to
3983 *restype_ptr precision, shortenings might be invalid. */
3984 if (TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (TREE_TYPE (op0))
3985 && TYPE_PRECISION (TREE_TYPE (op0)) < TYPE_PRECISION (*restype_ptr)
3986 && !unsignedp0
3987 && TYPE_UNSIGNED (TREE_TYPE (op0)))
3988 primop0 = op0;
3989 if (TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (TREE_TYPE (op1))
3990 && TYPE_PRECISION (TREE_TYPE (op1)) < TYPE_PRECISION (*restype_ptr)
3991 && !unsignedp1
3992 && TYPE_UNSIGNED (TREE_TYPE (op1)))
3993 primop1 = op1;
3994
b30f223b
RS
3995 /* Handle the case that OP0 does not *contain* a conversion
3996 but it *requires* conversion to FINAL_TYPE. */
3997
3998 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
8df83eae 3999 unsignedp0 = TYPE_UNSIGNED (TREE_TYPE (op0));
b30f223b 4000 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
8df83eae 4001 unsignedp1 = TYPE_UNSIGNED (TREE_TYPE (op1));
b30f223b
RS
4002
4003 /* If one of the operands must be floated, we cannot optimize. */
4004 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
4005 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
4006
4007 /* If first arg is constant, swap the args (changing operation
5af6001b
RK
4008 so value is preserved), for canonicalization. Don't do this if
4009 the second arg is 0. */
b30f223b 4010
5af6001b 4011 if (TREE_CONSTANT (primop0)
ab22c1fa
CF
4012 && !integer_zerop (primop1) && !real_zerop (primop1)
4013 && !fixed_zerop (primop1))
b30f223b 4014 {
fab27f52
MM
4015 std::swap (primop0, primop1);
4016 std::swap (op0, op1);
b30f223b
RS
4017 *op0_ptr = op0;
4018 *op1_ptr = op1;
fab27f52
MM
4019 std::swap (unsignedp0, unsignedp1);
4020 std::swap (real1, real2);
b30f223b
RS
4021
4022 switch (code)
4023 {
4024 case LT_EXPR:
4025 code = GT_EXPR;
4026 break;
4027 case GT_EXPR:
4028 code = LT_EXPR;
4029 break;
4030 case LE_EXPR:
4031 code = GE_EXPR;
4032 break;
4033 case GE_EXPR:
4034 code = LE_EXPR;
4035 break;
6d819282
MK
4036 default:
4037 break;
b30f223b
RS
4038 }
4039 *rescode_ptr = code;
4040 }
4041
4042 /* If comparing an integer against a constant more bits wide,
4043 maybe we can deduce a value of 1 or 0 independent of the data.
4044 Or else truncate the constant now
4045 rather than extend the variable at run time.
4046
4047 This is only interesting if the constant is the wider arg.
4048 Also, it is not safe if the constant is unsigned and the
4049 variable arg is signed, since in this case the variable
4050 would be sign-extended and then regarded as unsigned.
4051 Our technique fails in this case because the lowest/highest
4052 possible unsigned results don't follow naturally from the
4053 lowest/highest possible values of the variable operand.
4054 For just EQ_EXPR and NE_EXPR there is another technique that
4055 could be used: see if the constant can be faithfully represented
4056 in the other operand's type, by truncating it and reextending it
4057 and see if that preserves the constant's value. */
4058
4059 if (!real1 && !real2
ab22c1fa 4060 && TREE_CODE (TREE_TYPE (primop0)) != FIXED_POINT_TYPE
b30f223b
RS
4061 && TREE_CODE (primop1) == INTEGER_CST
4062 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
4063 {
4064 int min_gt, max_gt, min_lt, max_lt;
4065 tree maxval, minval;
4066 /* 1 if comparison is nominally unsigned. */
8df83eae 4067 int unsignedp = TYPE_UNSIGNED (*restype_ptr);
b30f223b
RS
4068 tree val;
4069
ceef8ce4
NB
4070 type = c_common_signed_or_unsigned_type (unsignedp0,
4071 TREE_TYPE (primop0));
8bbd5685 4072
b30f223b
RS
4073 maxval = TYPE_MAX_VALUE (type);
4074 minval = TYPE_MIN_VALUE (type);
4075
4076 if (unsignedp && !unsignedp0)
ceef8ce4 4077 *restype_ptr = c_common_signed_type (*restype_ptr);
b30f223b
RS
4078
4079 if (TREE_TYPE (primop1) != *restype_ptr)
fae1b38d 4080 {
af9c6659
NS
4081 /* Convert primop1 to target type, but do not introduce
4082 additional overflow. We know primop1 is an int_cst. */
807e902e 4083 primop1 = force_fit_type (*restype_ptr,
697e0b28
RS
4084 wi::to_wide
4085 (primop1,
4086 TYPE_PRECISION (*restype_ptr)),
807e902e 4087 0, TREE_OVERFLOW (primop1));
fae1b38d 4088 }
b30f223b
RS
4089 if (type != *restype_ptr)
4090 {
4091 minval = convert (*restype_ptr, minval);
4092 maxval = convert (*restype_ptr, maxval);
4093 }
4094
807e902e
KZ
4095 min_gt = tree_int_cst_lt (primop1, minval);
4096 max_gt = tree_int_cst_lt (primop1, maxval);
4097 min_lt = tree_int_cst_lt (minval, primop1);
4098 max_lt = tree_int_cst_lt (maxval, primop1);
b30f223b
RS
4099
4100 val = 0;
4101 /* This used to be a switch, but Genix compiler can't handle that. */
4102 if (code == NE_EXPR)
4103 {
4104 if (max_lt || min_gt)
de7df9eb 4105 val = truthvalue_true_node;
b30f223b
RS
4106 }
4107 else if (code == EQ_EXPR)
4108 {
4109 if (max_lt || min_gt)
de7df9eb 4110 val = truthvalue_false_node;
b30f223b
RS
4111 }
4112 else if (code == LT_EXPR)
4113 {
4114 if (max_lt)
de7df9eb 4115 val = truthvalue_true_node;
b30f223b 4116 if (!min_lt)
de7df9eb 4117 val = truthvalue_false_node;
b30f223b
RS
4118 }
4119 else if (code == GT_EXPR)
4120 {
4121 if (min_gt)
de7df9eb 4122 val = truthvalue_true_node;
b30f223b 4123 if (!max_gt)
de7df9eb 4124 val = truthvalue_false_node;
b30f223b
RS
4125 }
4126 else if (code == LE_EXPR)
4127 {
4128 if (!max_gt)
de7df9eb 4129 val = truthvalue_true_node;
b30f223b 4130 if (min_gt)
de7df9eb 4131 val = truthvalue_false_node;
b30f223b
RS
4132 }
4133 else if (code == GE_EXPR)
4134 {
4135 if (!min_lt)
de7df9eb 4136 val = truthvalue_true_node;
b30f223b 4137 if (max_lt)
de7df9eb 4138 val = truthvalue_false_node;
b30f223b
RS
4139 }
4140
4141 /* If primop0 was sign-extended and unsigned comparison specd,
4142 we did a signed comparison above using the signed type bounds.
4143 But the comparison we output must be unsigned.
4144
4145 Also, for inequalities, VAL is no good; but if the signed
4146 comparison had *any* fixed result, it follows that the
4147 unsigned comparison just tests the sign in reverse
4148 (positive values are LE, negative ones GE).
4149 So we can generate an unsigned comparison
4150 against an extreme value of the signed type. */
4151
4152 if (unsignedp && !unsignedp0)
4153 {
4154 if (val != 0)
4155 switch (code)
4156 {
4157 case LT_EXPR:
4158 case GE_EXPR:
4159 primop1 = TYPE_MIN_VALUE (type);
4160 val = 0;
4161 break;
4162
4163 case LE_EXPR:
4164 case GT_EXPR:
4165 primop1 = TYPE_MAX_VALUE (type);
4166 val = 0;
4167 break;
6d819282
MK
4168
4169 default:
4170 break;
b30f223b 4171 }
12753674 4172 type = c_common_unsigned_type (type);
b30f223b
RS
4173 }
4174
d0eccfcd 4175 if (TREE_CODE (primop0) != INTEGER_CST
269adb9d
JM
4176 /* Don't warn if it's from a (non-system) macro. */
4177 && !(from_macro_expansion_at
4178 (expansion_point_location_if_in_system_header
4179 (EXPR_LOCATION (primop0)))))
b30f223b 4180 {
de7df9eb 4181 if (val == truthvalue_false_node)
50f305ca
MLI
4182 warning_at (loc, OPT_Wtype_limits,
4183 "comparison is always false due to limited range of data type");
de7df9eb 4184 if (val == truthvalue_true_node)
50f305ca
MLI
4185 warning_at (loc, OPT_Wtype_limits,
4186 "comparison is always true due to limited range of data type");
b30f223b
RS
4187 }
4188
4189 if (val != 0)
4190 {
4191 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4192 if (TREE_SIDE_EFFECTS (primop0))
53fb4de3 4193 return build2 (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
b30f223b
RS
4194 return val;
4195 }
4196
4197 /* Value is not predetermined, but do the comparison
4198 in the type of the operand that is not constant.
4199 TYPE is already properly set. */
4200 }
9a8ce21f
JG
4201
4202 /* If either arg is decimal float and the other is float, find the
4203 proper common type to use for comparison. */
6f450181
RB
4204 else if (real1 && real2
4205 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4206 && DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1))))
4207 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
4208
4209 /* If either arg is decimal float and the other is float, fail. */
9a8ce21f
JG
4210 else if (real1 && real2
4211 && (DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop0)))
4212 || DECIMAL_FLOAT_MODE_P (TYPE_MODE (TREE_TYPE (primop1)))))
6f450181 4213 return 0;
9a8ce21f 4214
b30f223b 4215 else if (real1 && real2
766f6c30
RS
4216 && (TYPE_PRECISION (TREE_TYPE (primop0))
4217 == TYPE_PRECISION (TREE_TYPE (primop1))))
b30f223b
RS
4218 type = TREE_TYPE (primop0);
4219
4220 /* If args' natural types are both narrower than nominal type
4221 and both extend in the same manner, compare them
4222 in the type of the wider arg.
4223 Otherwise must actually extend both to the nominal
4224 common type lest different ways of extending
4225 alter the result.
4226 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
4227
4228 else if (unsignedp0 == unsignedp1 && real1 == real2
4229 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
4230 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
4231 {
4232 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
ceef8ce4 4233 type = c_common_signed_or_unsigned_type (unsignedp0
8df83eae 4234 || TYPE_UNSIGNED (*restype_ptr),
ceef8ce4 4235 type);
b30f223b
RS
4236 /* Make sure shorter operand is extended the right way
4237 to match the longer operand. */
ceef8ce4
NB
4238 primop0
4239 = convert (c_common_signed_or_unsigned_type (unsignedp0,
4240 TREE_TYPE (primop0)),
4241 primop0);
4242 primop1
4243 = convert (c_common_signed_or_unsigned_type (unsignedp1,
4244 TREE_TYPE (primop1)),
4245 primop1);
b30f223b
RS
4246 }
4247 else
4248 {
4249 /* Here we must do the comparison on the nominal type
4250 using the args exactly as we received them. */
4251 type = *restype_ptr;
4252 primop0 = op0;
4253 primop1 = op1;
4254
4255 if (!real1 && !real2 && integer_zerop (primop1)
8df83eae 4256 && TYPE_UNSIGNED (*restype_ptr))
b30f223b
RS
4257 {
4258 tree value = 0;
50f305ca
MLI
4259 /* All unsigned values are >= 0, so we warn. However,
4260 if OP0 is a constant that is >= 0, the signedness of
4261 the comparison isn't an issue, so suppress the
4262 warning. */
4263 bool warn =
8400e75e 4264 warn_type_limits && !in_system_header_at (loc)
50f305ca
MLI
4265 && !(TREE_CODE (primop0) == INTEGER_CST
4266 && !TREE_OVERFLOW (convert (c_common_signed_type (type),
4267 primop0)))
4268 /* Do not warn for enumeration types. */
4269 && (TREE_CODE (expr_original_type (primop0)) != ENUMERAL_TYPE);
4270
b30f223b
RS
4271 switch (code)
4272 {
4273 case GE_EXPR:
50f305ca
MLI
4274 if (warn)
4275 warning_at (loc, OPT_Wtype_limits,
4276 "comparison of unsigned expression >= 0 is always true");
de7df9eb 4277 value = truthvalue_true_node;
b30f223b
RS
4278 break;
4279
4280 case LT_EXPR:
50f305ca
MLI
4281 if (warn)
4282 warning_at (loc, OPT_Wtype_limits,
4283 "comparison of unsigned expression < 0 is always false");
de7df9eb 4284 value = truthvalue_false_node;
6d819282
MK
4285 break;
4286
4287 default:
4288 break;
b30f223b
RS
4289 }
4290
4291 if (value != 0)
4292 {
4293 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
4294 if (TREE_SIDE_EFFECTS (primop0))
53fb4de3
RS
4295 return build2 (COMPOUND_EXPR, TREE_TYPE (value),
4296 primop0, value);
b30f223b
RS
4297 return value;
4298 }
4299 }
4300 }
4301
4302 *op0_ptr = convert (type, primop0);
4303 *op1_ptr = convert (type, primop1);
4304
de7df9eb 4305 *restype_ptr = truthvalue_type_node;
b30f223b
RS
4306
4307 return 0;
4308}
4309\f
7552da58
JJ
4310/* Return a tree for the sum or difference (RESULTCODE says which)
4311 of pointer PTROP and integer INTOP. */
4312
4313tree
db3927fb 4314pointer_int_sum (location_t loc, enum tree_code resultcode,
fd9b0f32 4315 tree ptrop, tree intop, bool complain)
7552da58 4316{
6ac01510 4317 tree size_exp, ret;
7552da58 4318
7552da58 4319 /* The result is a pointer of the same type that is being added. */
7552da58
JJ
4320 tree result_type = TREE_TYPE (ptrop);
4321
4322 if (TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE)
4323 {
fd9b0f32
PC
4324 if (complain && warn_pointer_arith)
4325 pedwarn (loc, OPT_Wpointer_arith,
4326 "pointer of type %<void *%> used in arithmetic");
4327 else if (!complain)
4328 return error_mark_node;
7552da58
JJ
4329 size_exp = integer_one_node;
4330 }
4331 else if (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE)
4332 {
fd9b0f32
PC
4333 if (complain && warn_pointer_arith)
4334 pedwarn (loc, OPT_Wpointer_arith,
4335 "pointer to a function used in arithmetic");
4336 else if (!complain)
4337 return error_mark_node;
7552da58
JJ
4338 size_exp = integer_one_node;
4339 }
7552da58 4340 else
4f2e1536 4341 size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type));
7552da58 4342
6ac01510
ILT
4343 /* We are manipulating pointer values, so we don't need to warn
4344 about relying on undefined signed overflow. We disable the
4345 warning here because we use integer types so fold won't know that
4346 they are really pointers. */
4347 fold_defer_overflow_warnings ();
4348
7552da58
JJ
4349 /* If what we are about to multiply by the size of the elements
4350 contains a constant term, apply distributive law
4351 and multiply that constant term separately.
4352 This helps produce common subexpressions. */
7552da58 4353 if ((TREE_CODE (intop) == PLUS_EXPR || TREE_CODE (intop) == MINUS_EXPR)
3f75a254 4354 && !TREE_CONSTANT (intop)
7552da58
JJ
4355 && TREE_CONSTANT (TREE_OPERAND (intop, 1))
4356 && TREE_CONSTANT (size_exp)
4357 /* If the constant comes from pointer subtraction,
4358 skip this optimization--it would cause an error. */
4359 && TREE_CODE (TREE_TYPE (TREE_OPERAND (intop, 0))) == INTEGER_TYPE
4360 /* If the constant is unsigned, and smaller than the pointer size,
4361 then we must skip this optimization. This is because it could cause
4362 an overflow error if the constant is negative but INTOP is not. */
3f75a254 4363 && (!TYPE_UNSIGNED (TREE_TYPE (intop))
7552da58
JJ
4364 || (TYPE_PRECISION (TREE_TYPE (intop))
4365 == TYPE_PRECISION (TREE_TYPE (ptrop)))))
4366 {
4367 enum tree_code subcode = resultcode;
4368 tree int_type = TREE_TYPE (intop);
4369 if (TREE_CODE (intop) == MINUS_EXPR)
4370 subcode = (subcode == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
4371 /* Convert both subexpression types to the type of intop,
4372 because weird cases involving pointer arithmetic
4373 can result in a sum or difference with different type args. */
ba47d38d
AH
4374 ptrop = build_binary_op (EXPR_LOCATION (TREE_OPERAND (intop, 1)),
4375 subcode, ptrop,
7552da58
JJ
4376 convert (int_type, TREE_OPERAND (intop, 1)), 1);
4377 intop = convert (int_type, TREE_OPERAND (intop, 0));
4378 }
4379
4380 /* Convert the integer argument to a type the same size as sizetype
4381 so the multiply won't overflow spuriously. */
7552da58 4382 if (TYPE_PRECISION (TREE_TYPE (intop)) != TYPE_PRECISION (sizetype)
8df83eae 4383 || TYPE_UNSIGNED (TREE_TYPE (intop)) != TYPE_UNSIGNED (sizetype))
35b1a6fa 4384 intop = convert (c_common_type_for_size (TYPE_PRECISION (sizetype),
8df83eae 4385 TYPE_UNSIGNED (sizetype)), intop);
7552da58
JJ
4386
4387 /* Replace the integer argument with a suitable product by the object size.
9e9ef331 4388 Do this multiplication as signed, then convert to the appropriate type
65de6659 4389 for the pointer operation and disregard an overflow that occurred only
9e9ef331
EB
4390 because of the sign-extension change in the latter conversion. */
4391 {
d90ec4f2
JM
4392 tree t = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (intop), intop,
4393 convert (TREE_TYPE (intop), size_exp));
9e9ef331
EB
4394 intop = convert (sizetype, t);
4395 if (TREE_OVERFLOW_P (intop) && !TREE_OVERFLOW (t))
807e902e 4396 intop = wide_int_to_tree (TREE_TYPE (intop), intop);
9e9ef331 4397 }
5be014d5 4398
280f1ffa 4399 /* Create the sum or difference. */
5be014d5 4400 if (resultcode == MINUS_EXPR)
db3927fb 4401 intop = fold_build1_loc (loc, NEGATE_EXPR, sizetype, intop);
7552da58 4402
5d49b6a7 4403 ret = fold_build_pointer_plus_loc (loc, ptrop, intop);
6ac01510
ILT
4404
4405 fold_undefer_and_ignore_overflow_warnings ();
4406
4407 return ret;
7552da58
JJ
4408}
4409\f
e5a94231
JM
4410/* Wrap a C_MAYBE_CONST_EXPR around an expression that is fully folded
4411 and if NON_CONST is known not to be permitted in an evaluated part
4412 of a constant expression. */
4413
4414tree
4415c_wrap_maybe_const (tree expr, bool non_const)
4416{
4417 bool nowarning = TREE_NO_WARNING (expr);
4418 location_t loc = EXPR_LOCATION (expr);
4419
4420 /* This should never be called for C++. */
4421 if (c_dialect_cxx ())
4422 gcc_unreachable ();
4423
4424 /* The result of folding may have a NOP_EXPR to set TREE_NO_WARNING. */
4425 STRIP_TYPE_NOPS (expr);
4426 expr = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL, expr);
4427 C_MAYBE_CONST_EXPR_NON_CONST (expr) = non_const;
4428 if (nowarning)
4429 TREE_NO_WARNING (expr) = 1;
4430 protected_set_expr_location (expr, loc);
4431
4432 return expr;
4433}
4434
928c19bb
JM
4435/* Wrap a SAVE_EXPR around EXPR, if appropriate. Like save_expr, but
4436 for C folds the inside expression and wraps a C_MAYBE_CONST_EXPR
4437 around the SAVE_EXPR if needed so that c_fully_fold does not need
4438 to look inside SAVE_EXPRs. */
4439
4440tree
4441c_save_expr (tree expr)
4442{
4443 bool maybe_const = true;
4444 if (c_dialect_cxx ())
4445 return save_expr (expr);
4446 expr = c_fully_fold (expr, false, &maybe_const);
4447 expr = save_expr (expr);
4448 if (!maybe_const)
e5a94231 4449 expr = c_wrap_maybe_const (expr, true);
928c19bb
JM
4450 return expr;
4451}
4452
b3c6d2ea
ILT
4453/* Return whether EXPR is a declaration whose address can never be
4454 NULL. */
4455
4456bool
58f9752a 4457decl_with_nonnull_addr_p (const_tree expr)
b3c6d2ea
ILT
4458{
4459 return (DECL_P (expr)
4460 && (TREE_CODE (expr) == PARM_DECL
4461 || TREE_CODE (expr) == LABEL_DECL
4462 || !DECL_WEAK (expr)));
4463}
4464
b30f223b 4465/* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
85498824
JM
4466 or for an `if' or `while' statement or ?..: exp. It should already
4467 have been validated to be of suitable type; otherwise, a bad
4468 diagnostic may result.
b30f223b 4469
ba47d38d
AH
4470 The EXPR is located at LOCATION.
4471
b30f223b
RS
4472 This preparation consists of taking the ordinary
4473 representation of an expression expr and producing a valid tree
4474 boolean expression describing whether expr is nonzero. We could
de7df9eb 4475 simply always do build_binary_op (NE_EXPR, expr, truthvalue_false_node, 1),
b30f223b
RS
4476 but we optimize comparisons, &&, ||, and !.
4477
de7df9eb 4478 The resulting type should always be `truthvalue_type_node'. */
b30f223b
RS
4479
4480tree
ba47d38d 4481c_common_truthvalue_conversion (location_t location, tree expr)
b30f223b 4482{
b30f223b
RS
4483 switch (TREE_CODE (expr))
4484 {
d1a7edaf 4485 case EQ_EXPR: case NE_EXPR: case UNEQ_EXPR: case LTGT_EXPR:
6f312d18
ZW
4486 case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
4487 case UNLE_EXPR: case UNGE_EXPR: case UNLT_EXPR: case UNGT_EXPR:
4488 case ORDERED_EXPR: case UNORDERED_EXPR:
90ec750d
RS
4489 if (TREE_TYPE (expr) == truthvalue_type_node)
4490 return expr;
c2255bc4 4491 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
90ec750d 4492 TREE_OPERAND (expr, 0), TREE_OPERAND (expr, 1));
c2255bc4 4493 goto ret;
90ec750d 4494
b30f223b
RS
4495 case TRUTH_ANDIF_EXPR:
4496 case TRUTH_ORIF_EXPR:
4497 case TRUTH_AND_EXPR:
4498 case TRUTH_OR_EXPR:
9379fac9 4499 case TRUTH_XOR_EXPR:
90ec750d
RS
4500 if (TREE_TYPE (expr) == truthvalue_type_node)
4501 return expr;
c2255bc4 4502 expr = build2 (TREE_CODE (expr), truthvalue_type_node,
b8698a0f 4503 c_common_truthvalue_conversion (location,
c2255bc4
AH
4504 TREE_OPERAND (expr, 0)),
4505 c_common_truthvalue_conversion (location,
4506 TREE_OPERAND (expr, 1)));
4507 goto ret;
18c0f675 4508
18d00205 4509 case TRUTH_NOT_EXPR:
90ec750d
RS
4510 if (TREE_TYPE (expr) == truthvalue_type_node)
4511 return expr;
c2255bc4
AH
4512 expr = build1 (TREE_CODE (expr), truthvalue_type_node,
4513 c_common_truthvalue_conversion (location,
4514 TREE_OPERAND (expr, 0)));
4515 goto ret;
18d00205 4516
b30f223b
RS
4517 case ERROR_MARK:
4518 return expr;
4519
4520 case INTEGER_CST:
d95787e6
RS
4521 return integer_zerop (expr) ? truthvalue_false_node
4522 : truthvalue_true_node;
b30f223b
RS
4523
4524 case REAL_CST:
010c4d9c
RS
4525 return real_compare (NE_EXPR, &TREE_REAL_CST (expr), &dconst0)
4526 ? truthvalue_true_node
4527 : truthvalue_false_node;
b30f223b 4528
ab22c1fa
CF
4529 case FIXED_CST:
4530 return fixed_compare (NE_EXPR, &TREE_FIXED_CST (expr),
4531 &FCONST0 (TYPE_MODE (TREE_TYPE (expr))))
4532 ? truthvalue_true_node
4533 : truthvalue_false_node;
4534
90ec750d 4535 case FUNCTION_DECL:
c9f9eb5d 4536 expr = build_unary_op (location, ADDR_EXPR, expr, 0);
90ec750d
RS
4537 /* Fall through. */
4538
b30f223b 4539 case ADDR_EXPR:
1998463c 4540 {
f6f08360 4541 tree inner = TREE_OPERAND (expr, 0);
b3c6d2ea 4542 if (decl_with_nonnull_addr_p (inner))
1998463c 4543 {
b3c6d2ea 4544 /* Common Ada/Pascal programmer's mistake. */
ba47d38d
AH
4545 warning_at (location,
4546 OPT_Waddress,
4547 "the address of %qD will always evaluate as %<true%>",
4548 inner);
1998463c
SB
4549 return truthvalue_true_node;
4550 }
33766b66 4551 break;
1998463c 4552 }
b30f223b 4553
766f6c30 4554 case COMPLEX_EXPR:
c2255bc4 4555 expr = build_binary_op (EXPR_LOCATION (expr),
ba47d38d 4556 (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
b839fb3f 4557 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
ba47d38d
AH
4558 c_common_truthvalue_conversion (location,
4559 TREE_OPERAND (expr, 0)),
4560 c_common_truthvalue_conversion (location,
4561 TREE_OPERAND (expr, 1)),
766f6c30 4562 0);
c2255bc4 4563 goto ret;
766f6c30 4564
b30f223b
RS
4565 case NEGATE_EXPR:
4566 case ABS_EXPR:
4567 case FLOAT_EXPR:
8ce94e44 4568 case EXCESS_PRECISION_EXPR:
da7d8304 4569 /* These don't change whether an object is nonzero or zero. */
ba47d38d 4570 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
b30f223b
RS
4571
4572 case LROTATE_EXPR:
4573 case RROTATE_EXPR:
da7d8304 4574 /* These don't change whether an object is zero or nonzero, but
b30f223b
RS
4575 we can't ignore them if their second arg has side-effects. */
4576 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
c2255bc4
AH
4577 {
4578 expr = build2 (COMPOUND_EXPR, truthvalue_type_node,
4579 TREE_OPERAND (expr, 1),
b8698a0f 4580 c_common_truthvalue_conversion
c2255bc4
AH
4581 (location, TREE_OPERAND (expr, 0)));
4582 goto ret;
4583 }
b30f223b 4584 else
ba47d38d
AH
4585 return c_common_truthvalue_conversion (location,
4586 TREE_OPERAND (expr, 0));
b57062ca 4587
b30f223b
RS
4588 case COND_EXPR:
4589 /* Distribute the conversion into the arms of a COND_EXPR. */
928c19bb 4590 if (c_dialect_cxx ())
c2255bc4 4591 {
4cc4f2f4
JJ
4592 tree op1 = TREE_OPERAND (expr, 1);
4593 tree op2 = TREE_OPERAND (expr, 2);
4594 /* In C++ one of the arms might have void type if it is throw. */
4595 if (!VOID_TYPE_P (TREE_TYPE (op1)))
4596 op1 = c_common_truthvalue_conversion (location, op1);
4597 if (!VOID_TYPE_P (TREE_TYPE (op2)))
4598 op2 = c_common_truthvalue_conversion (location, op2);
db3927fb 4599 expr = fold_build3_loc (location, COND_EXPR, truthvalue_type_node,
4cc4f2f4 4600 TREE_OPERAND (expr, 0), op1, op2);
c2255bc4
AH
4601 goto ret;
4602 }
928c19bb 4603 else
c2255bc4
AH
4604 {
4605 /* Folding will happen later for C. */
4606 expr = build3 (COND_EXPR, truthvalue_type_node,
4607 TREE_OPERAND (expr, 0),
4608 c_common_truthvalue_conversion (location,
4609 TREE_OPERAND (expr, 1)),
4610 c_common_truthvalue_conversion (location,
4611 TREE_OPERAND (expr, 2)));
4612 goto ret;
4613 }
b30f223b 4614
1043771b 4615 CASE_CONVERT:
1ee44b26
JM
4616 {
4617 tree totype = TREE_TYPE (expr);
4618 tree fromtype = TREE_TYPE (TREE_OPERAND (expr, 0));
4619
076fecad 4620 if (POINTER_TYPE_P (totype)
451dcc66 4621 && !c_inhibit_evaluation_warnings
076fecad
PP
4622 && TREE_CODE (fromtype) == REFERENCE_TYPE)
4623 {
4624 tree inner = expr;
4625 STRIP_NOPS (inner);
4626
4627 if (DECL_P (inner))
4628 warning_at (location,
4629 OPT_Waddress,
4630 "the compiler can assume that the address of "
4631 "%qD will always evaluate to %<true%>",
4632 inner);
4633 }
4634
1ee44b26
JM
4635 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
4636 since that affects how `default_conversion' will behave. */
4637 if (TREE_CODE (totype) == REFERENCE_TYPE
4638 || TREE_CODE (fromtype) == REFERENCE_TYPE)
4639 break;
4640 /* Don't strip a conversion from C++0x scoped enum, since they
4641 don't implicitly convert to other types. */
4642 if (TREE_CODE (fromtype) == ENUMERAL_TYPE
4643 && ENUM_IS_SCOPED (fromtype))
4644 break;
4645 /* If this isn't narrowing the argument, we can ignore it. */
4646 if (TYPE_PRECISION (totype) >= TYPE_PRECISION (fromtype))
4647 return c_common_truthvalue_conversion (location,
4648 TREE_OPERAND (expr, 0));
4649 }
b30f223b
RS
4650 break;
4651
e2aab13d 4652 case MODIFY_EXPR:
fbc8d2d3
ILT
4653 if (!TREE_NO_WARNING (expr)
4654 && warn_parentheses)
4655 {
638fc14f
MP
4656 warning_at (location, OPT_Wparentheses,
4657 "suggest parentheses around assignment used as "
4658 "truth value");
fbc8d2d3
ILT
4659 TREE_NO_WARNING (expr) = 1;
4660 }
e2aab13d 4661 break;
b57062ca 4662
6d819282
MK
4663 default:
4664 break;
b30f223b
RS
4665 }
4666
f0b996c5 4667 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
f0b8d9aa 4668 {
5386338c 4669 tree t = (in_late_binary_op ? save_expr (expr) : c_save_expr (expr));
c2255bc4 4670 expr = (build_binary_op
ba47d38d
AH
4671 (EXPR_LOCATION (expr),
4672 (TREE_SIDE_EFFECTS (expr)
f0b8d9aa 4673 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
c9f9eb5d
AH
4674 c_common_truthvalue_conversion
4675 (location,
4676 build_unary_op (location, REALPART_EXPR, t, 0)),
4677 c_common_truthvalue_conversion
4678 (location,
4679 build_unary_op (location, IMAGPART_EXPR, t, 0)),
f0b8d9aa 4680 0));
c2255bc4 4681 goto ret;
f0b8d9aa 4682 }
f0b996c5 4683
ab22c1fa
CF
4684 if (TREE_CODE (TREE_TYPE (expr)) == FIXED_POINT_TYPE)
4685 {
4686 tree fixed_zero_node = build_fixed (TREE_TYPE (expr),
4687 FCONST0 (TYPE_MODE
4688 (TREE_TYPE (expr))));
ca80e52b 4689 return build_binary_op (location, NE_EXPR, expr, fixed_zero_node, 1);
ab22c1fa 4690 }
c2255bc4
AH
4691 else
4692 return build_binary_op (location, NE_EXPR, expr, integer_zero_node, 1);
ab22c1fa 4693
c2255bc4
AH
4694 ret:
4695 protected_set_expr_location (expr, location);
4696 return expr;
b30f223b
RS
4697}
4698\f
9bc15050
RG
4699static void def_builtin_1 (enum built_in_function fncode,
4700 const char *name,
4701 enum built_in_class fnclass,
4702 tree fntype, tree libtype,
4703 bool both_p, bool fallback_p, bool nonansi_p,
4704 tree fnattrs, bool implicit_p);
fc2aaf30 4705
3932261a
MM
4706
4707/* Apply the TYPE_QUALS to the new DECL. */
4708
4709void
35b1a6fa 4710c_apply_type_quals_to_decl (int type_quals, tree decl)
3932261a 4711{
4b011bbf 4712 tree type = TREE_TYPE (decl);
9f63daea 4713
5a6159dd
AP
4714 if (type == error_mark_node)
4715 return;
4b011bbf 4716
329af3c7
JM
4717 if ((type_quals & TYPE_QUAL_CONST)
4718 || (type && TREE_CODE (type) == REFERENCE_TYPE))
4719 /* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
4720 constructor can produce constant init, so rely on cp_finish_decl to
4721 clear TREE_READONLY if the variable has non-constant init. */
3932261a
MM
4722 TREE_READONLY (decl) = 1;
4723 if (type_quals & TYPE_QUAL_VOLATILE)
4724 {
4725 TREE_SIDE_EFFECTS (decl) = 1;
4726 TREE_THIS_VOLATILE (decl) = 1;
4727 }
6946bc60 4728 if (type_quals & TYPE_QUAL_RESTRICT)
3932261a 4729 {
4b011bbf
JM
4730 while (type && TREE_CODE (type) == ARRAY_TYPE)
4731 /* Allow 'restrict' on arrays of pointers.
4732 FIXME currently we just ignore it. */
4733 type = TREE_TYPE (type);
4734 if (!type
4735 || !POINTER_TYPE_P (type)
4736 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type)))
bda67431 4737 error ("invalid use of %<restrict%>");
3932261a
MM
4738 }
4739}
4740
ca752f39 4741struct c_type_hasher : ggc_ptr_hash<tree_node>
2a22f99c
TS
4742{
4743 static hashval_t hash (tree);
4744 static bool equal (tree, tree);
4745};
4746
6de9cd9a
DN
4747/* Hash function for the problem of multiple type definitions in
4748 different files. This must hash all types that will compare
4749 equal via comptypes to the same value. In practice it hashes
9cf737f8 4750 on some of the simple stuff and leaves the details to comptypes. */
6de9cd9a 4751
2a22f99c
TS
4752hashval_t
4753c_type_hasher::hash (tree t)
6de9cd9a 4754{
a19e4d44 4755 int n_elements;
6de9cd9a 4756 int shift, size;
6de9cd9a
DN
4757 tree t2;
4758 switch (TREE_CODE (t))
4759 {
8c27b7d4 4760 /* For pointers, hash on pointee type plus some swizzling. */
325c3691 4761 case POINTER_TYPE:
2a22f99c 4762 return hash (TREE_TYPE (t)) ^ 0x3003003;
325c3691
RH
4763 /* Hash on number of elements and total size. */
4764 case ENUMERAL_TYPE:
4765 shift = 3;
4766 t2 = TYPE_VALUES (t);
4767 break;
4768 case RECORD_TYPE:
4769 shift = 0;
4770 t2 = TYPE_FIELDS (t);
4771 break;
4772 case QUAL_UNION_TYPE:
4773 shift = 1;
4774 t2 = TYPE_FIELDS (t);
4775 break;
4776 case UNION_TYPE:
4777 shift = 2;
4778 t2 = TYPE_FIELDS (t);
4779 break;
4780 default:
366de0ce 4781 gcc_unreachable ();
6de9cd9a 4782 }
a19e4d44
NF
4783 /* FIXME: We want to use a DECL_CHAIN iteration method here, but
4784 TYPE_VALUES of ENUMERAL_TYPEs is stored as a TREE_LIST. */
4785 n_elements = list_length (t2);
6fc3c3c0
TT
4786 /* We might have a VLA here. */
4787 if (TREE_CODE (TYPE_SIZE (t)) != INTEGER_CST)
4788 size = 0;
4789 else
4790 size = TREE_INT_CST_LOW (TYPE_SIZE (t));
a19e4d44 4791 return ((size << 24) | (n_elements << shift));
6de9cd9a
DN
4792}
4793
2a22f99c
TS
4794bool
4795c_type_hasher::equal (tree t1, tree t2)
4796{
4797 return lang_hooks.types_compatible_p (t1, t2);
4798}
4799
4800static GTY(()) hash_table<c_type_hasher> *type_hash_table;
4fe52ce9 4801
41472af8 4802/* Return the typed-based alias set for T, which may be an expression
3bdf5ad1 4803 or a type. Return -1 if we don't do anything special. */
41472af8 4804
4862826d 4805alias_set_type
35b1a6fa 4806c_common_get_alias_set (tree t)
41472af8 4807{
cb9c2485
JM
4808 /* For VLAs, use the alias set of the element type rather than the
4809 default of alias set 0 for types compared structurally. */
4810 if (TYPE_P (t) && TYPE_STRUCTURAL_EQUALITY_P (t))
4811 {
4812 if (TREE_CODE (t) == ARRAY_TYPE)
4813 return get_alias_set (TREE_TYPE (t));
4814 return -1;
4815 }
4816
74d86f4f 4817 /* That's all the expressions we handle specially. */
3f75a254 4818 if (!TYPE_P (t))
74d86f4f
RH
4819 return -1;
4820
95bd1dd7 4821 /* The C standard guarantees that any object may be accessed via an
74d86f4f
RH
4822 lvalue that has character type. */
4823 if (t == char_type_node
4824 || t == signed_char_type_node
4825 || t == unsigned_char_type_node)
3bdf5ad1 4826 return 0;
3932261a 4827
f824e5c3
RK
4828 /* The C standard specifically allows aliasing between signed and
4829 unsigned variants of the same type. We treat the signed
4830 variant as canonical. */
8df83eae 4831 if (TREE_CODE (t) == INTEGER_TYPE && TYPE_UNSIGNED (t))
8f215dce 4832 {
ceef8ce4 4833 tree t1 = c_common_signed_type (t);
f824e5c3 4834
8f215dce
JJ
4835 /* t1 == t can happen for boolean nodes which are always unsigned. */
4836 if (t1 != t)
4837 return get_alias_set (t1);
4838 }
ece32014 4839
6de9cd9a
DN
4840 /* Handle the case of multiple type nodes referring to "the same" type,
4841 which occurs with IMA. These share an alias set. FIXME: Currently only
4842 C90 is handled. (In C99 type compatibility is not transitive, which
4843 complicates things mightily. The alias set splay trees can theoretically
4844 represent this, but insertion is tricky when you consider all the
4845 different orders things might arrive in.) */
4846
4847 if (c_language != clk_c || flag_isoc99)
4848 return -1;
4849
9cf737f8 4850 /* Save time if there's only one input file. */
d974312d 4851 if (num_in_fnames == 1)
6de9cd9a
DN
4852 return -1;
4853
4854 /* Pointers need special handling if they point to any type that
4855 needs special handling (below). */
4856 if (TREE_CODE (t) == POINTER_TYPE)
4857 {
4858 tree t2;
4859 /* Find bottom type under any nested POINTERs. */
9f63daea 4860 for (t2 = TREE_TYPE (t);
762f7d9d
TT
4861 TREE_CODE (t2) == POINTER_TYPE;
4862 t2 = TREE_TYPE (t2))
4863 ;
a868811e
MP
4864 if (!RECORD_OR_UNION_TYPE_P (t2)
4865 && TREE_CODE (t2) != ENUMERAL_TYPE)
762f7d9d 4866 return -1;
6de9cd9a 4867 if (TYPE_SIZE (t2) == 0)
762f7d9d 4868 return -1;
6de9cd9a
DN
4869 }
4870 /* These are the only cases that need special handling. */
a868811e 4871 if (!RECORD_OR_UNION_TYPE_P (t)
6de9cd9a 4872 && TREE_CODE (t) != ENUMERAL_TYPE
6de9cd9a
DN
4873 && TREE_CODE (t) != POINTER_TYPE)
4874 return -1;
4875 /* Undefined? */
4876 if (TYPE_SIZE (t) == 0)
4877 return -1;
4878
9f63daea 4879 /* Look up t in hash table. Only one of the compatible types within each
6de9cd9a
DN
4880 alias set is recorded in the table. */
4881 if (!type_hash_table)
2a22f99c
TS
4882 type_hash_table = hash_table<c_type_hasher>::create_ggc (1021);
4883 tree *slot = type_hash_table->find_slot (t, INSERT);
6de9cd9a 4884 if (*slot != NULL)
6a3203c8
AP
4885 {
4886 TYPE_ALIAS_SET (t) = TYPE_ALIAS_SET ((tree)*slot);
4887 return TYPE_ALIAS_SET ((tree)*slot);
4888 }
6de9cd9a
DN
4889 else
4890 /* Our caller will assign and record (in t) a new alias set; all we need
4891 to do is remember t in the hash table. */
4892 *slot = t;
4893
3bdf5ad1 4894 return -1;
41472af8 4895}
0213a355 4896\f
c2255bc4 4897/* Compute the value of 'sizeof (TYPE)' or '__alignof__ (TYPE)', where
296674db 4898 the IS_SIZEOF parameter indicates which operator is being applied.
c2255bc4
AH
4899 The COMPLAIN flag controls whether we should diagnose possibly
4900 ill-formed constructs or not. LOC is the location of the SIZEOF or
296674db
JM
4901 TYPEOF operator. If MIN_ALIGNOF, the least alignment required for
4902 a type in any context should be returned, rather than the normal
4903 alignment for that type. */
03a08664 4904
0213a355 4905tree
c2255bc4 4906c_sizeof_or_alignof_type (location_t loc,
296674db
JM
4907 tree type, bool is_sizeof, bool min_alignof,
4908 int complain)
0213a355 4909{
fa72b064
GDR
4910 const char *op_name;
4911 tree value = NULL;
4912 enum tree_code type_code = TREE_CODE (type);
35b1a6fa 4913
03a08664 4914 op_name = is_sizeof ? "sizeof" : "__alignof__";
35b1a6fa 4915
fa72b064 4916 if (type_code == FUNCTION_TYPE)
0213a355 4917 {
03a08664 4918 if (is_sizeof)
fa72b064 4919 {
44d90fe1
PC
4920 if (complain && warn_pointer_arith)
4921 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 4922 "invalid application of %<sizeof%> to a function type");
5ade1ed2
DG
4923 else if (!complain)
4924 return error_mark_node;
fa72b064
GDR
4925 value = size_one_node;
4926 }
4927 else
d19fa6b5
JM
4928 {
4929 if (complain)
4930 {
4931 if (c_dialect_cxx ())
c1771a20 4932 pedwarn (loc, OPT_Wpedantic, "ISO C++ does not permit "
d19fa6b5
JM
4933 "%<alignof%> applied to a function type");
4934 else
c1771a20 4935 pedwarn (loc, OPT_Wpedantic, "ISO C does not permit "
d19fa6b5
JM
4936 "%<_Alignof%> applied to a function type");
4937 }
4938 value = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
4939 }
fa72b064
GDR
4940 }
4941 else if (type_code == VOID_TYPE || type_code == ERROR_MARK)
4942 {
35b1a6fa 4943 if (type_code == VOID_TYPE
44d90fe1
PC
4944 && complain && warn_pointer_arith)
4945 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 4946 "invalid application of %qs to a void type", op_name);
5ade1ed2
DG
4947 else if (!complain)
4948 return error_mark_node;
fa72b064 4949 value = size_one_node;
0213a355 4950 }
73ac190a
PC
4951 else if (!COMPLETE_TYPE_P (type)
4952 && (!c_dialect_cxx () || is_sizeof || type_code != ARRAY_TYPE))
0213a355 4953 {
ea793912 4954 if (complain)
73ac190a 4955 error_at (loc, "invalid application of %qs to incomplete type %qT",
c2255bc4 4956 op_name, type);
cb6addf4 4957 return error_mark_node;
0213a355 4958 }
73ac190a
PC
4959 else if (c_dialect_cxx () && type_code == ARRAY_TYPE
4960 && !COMPLETE_TYPE_P (TREE_TYPE (type)))
4961 {
4962 if (complain)
4963 error_at (loc, "invalid application of %qs to array type %qT of "
4964 "incomplete element type", op_name, type);
4965 return error_mark_node;
4966 }
0213a355 4967 else
fa72b064 4968 {
03a08664 4969 if (is_sizeof)
fa72b064 4970 /* Convert in case a char is more than one unit. */
db3927fb
AH
4971 value = size_binop_loc (loc, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
4972 size_int (TYPE_PRECISION (char_type_node)
4973 / BITS_PER_UNIT));
296674db 4974 else if (min_alignof)
2793eeab 4975 value = size_int (min_align_of_type (type));
fa72b064 4976 else
a4e9ffe5 4977 value = size_int (TYPE_ALIGN_UNIT (type));
fa72b064 4978 }
0213a355 4979
3ac8781c
RG
4980 /* VALUE will have the middle-end integer type sizetype.
4981 However, we should really return a value of type `size_t',
4982 which is just a typedef for an ordinary integer type. */
db3927fb 4983 value = fold_convert_loc (loc, size_type_node, value);
35b1a6fa 4984
fa72b064 4985 return value;
0213a355
JM
4986}
4987
4988/* Implement the __alignof keyword: Return the minimum required
837edd5f
GK
4989 alignment of EXPR, measured in bytes. For VAR_DECLs,
4990 FUNCTION_DECLs and FIELD_DECLs return DECL_ALIGN (which can be set
c2255bc4
AH
4991 from an "aligned" __attribute__ specification). LOC is the
4992 location of the ALIGNOF operator. */
7f4edbcb 4993
0213a355 4994tree
c2255bc4 4995c_alignof_expr (location_t loc, tree expr)
0213a355
JM
4996{
4997 tree t;
4998
837edd5f 4999 if (VAR_OR_FUNCTION_DECL_P (expr))
a4e9ffe5 5000 t = size_int (DECL_ALIGN_UNIT (expr));
35b1a6fa 5001
0213a355
JM
5002 else if (TREE_CODE (expr) == COMPONENT_REF
5003 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
5004 {
c2255bc4 5005 error_at (loc, "%<__alignof%> applied to a bit-field");
0213a355
JM
5006 t = size_one_node;
5007 }
5008 else if (TREE_CODE (expr) == COMPONENT_REF
173bf5be 5009 && TREE_CODE (TREE_OPERAND (expr, 1)) == FIELD_DECL)
a4e9ffe5 5010 t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (expr, 1)));
35b1a6fa 5011
22d03525 5012 else if (INDIRECT_REF_P (expr))
0213a355
JM
5013 {
5014 tree t = TREE_OPERAND (expr, 0);
5015 tree best = t;
5016 int bestalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
35b1a6fa 5017
1043771b 5018 while (CONVERT_EXPR_P (t)
173bf5be 5019 && TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == POINTER_TYPE)
0213a355
JM
5020 {
5021 int thisalign;
5022
5023 t = TREE_OPERAND (t, 0);
5024 thisalign = TYPE_ALIGN (TREE_TYPE (TREE_TYPE (t)));
5025 if (thisalign > bestalign)
5026 best = t, bestalign = thisalign;
5027 }
c2255bc4 5028 return c_alignof (loc, TREE_TYPE (TREE_TYPE (best)));
0213a355
JM
5029 }
5030 else
c2255bc4 5031 return c_alignof (loc, TREE_TYPE (expr));
0213a355 5032
db3927fb 5033 return fold_convert_loc (loc, size_type_node, t);
0213a355
JM
5034}
5035\f
df061a43
RS
5036/* Handle C and C++ default attributes. */
5037
5038enum built_in_attribute
5039{
5040#define DEF_ATTR_NULL_TREE(ENUM) ENUM,
5041#define DEF_ATTR_INT(ENUM, VALUE) ENUM,
e384e6b5 5042#define DEF_ATTR_STRING(ENUM, VALUE) ENUM,
df061a43
RS
5043#define DEF_ATTR_IDENT(ENUM, STRING) ENUM,
5044#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) ENUM,
df061a43
RS
5045#include "builtin-attrs.def"
5046#undef DEF_ATTR_NULL_TREE
5047#undef DEF_ATTR_INT
e384e6b5 5048#undef DEF_ATTR_STRING
df061a43
RS
5049#undef DEF_ATTR_IDENT
5050#undef DEF_ATTR_TREE_LIST
df061a43
RS
5051 ATTR_LAST
5052};
5053
5054static GTY(()) tree built_in_attributes[(int) ATTR_LAST];
5055
35b1a6fa 5056static void c_init_attributes (void);
df061a43 5057
a0274e3e 5058enum c_builtin_type
7f4edbcb 5059{
10841285
MM
5060#define DEF_PRIMITIVE_TYPE(NAME, VALUE) NAME,
5061#define DEF_FUNCTION_TYPE_0(NAME, RETURN) NAME,
5062#define DEF_FUNCTION_TYPE_1(NAME, RETURN, ARG1) NAME,
5063#define DEF_FUNCTION_TYPE_2(NAME, RETURN, ARG1, ARG2) NAME,
5064#define DEF_FUNCTION_TYPE_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
5065#define DEF_FUNCTION_TYPE_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
10a0d495 5066#define DEF_FUNCTION_TYPE_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) NAME,
f6a7cffc
TS
5067#define DEF_FUNCTION_TYPE_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5068 ARG6) NAME,
5069#define DEF_FUNCTION_TYPE_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5070 ARG6, ARG7) NAME,
5071#define DEF_FUNCTION_TYPE_8(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5072 ARG6, ARG7, ARG8) NAME,
d9a6bd32
JJ
5073#define DEF_FUNCTION_TYPE_9(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5074 ARG6, ARG7, ARG8, ARG9) NAME,
5075#define DEF_FUNCTION_TYPE_10(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5076 ARG6, ARG7, ARG8, ARG9, ARG10) NAME,
5077#define DEF_FUNCTION_TYPE_11(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5078 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) NAME,
10841285
MM
5079#define DEF_FUNCTION_TYPE_VAR_0(NAME, RETURN) NAME,
5080#define DEF_FUNCTION_TYPE_VAR_1(NAME, RETURN, ARG1) NAME,
5081#define DEF_FUNCTION_TYPE_VAR_2(NAME, RETURN, ARG1, ARG2) NAME,
08291658 5082#define DEF_FUNCTION_TYPE_VAR_3(NAME, RETURN, ARG1, ARG2, ARG3) NAME,
10a0d495 5083#define DEF_FUNCTION_TYPE_VAR_4(NAME, RETURN, ARG1, ARG2, ARG3, ARG4) NAME,
f6a7cffc 5084#define DEF_FUNCTION_TYPE_VAR_5(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
56a9f6bc 5085 NAME,
3e32ee19
NS
5086#define DEF_FUNCTION_TYPE_VAR_6(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5087 ARG6) NAME,
56a9f6bc
TS
5088#define DEF_FUNCTION_TYPE_VAR_7(NAME, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5089 ARG6, ARG7) NAME,
10841285
MM
5090#define DEF_POINTER_TYPE(NAME, TYPE) NAME,
5091#include "builtin-types.def"
5092#undef DEF_PRIMITIVE_TYPE
5093#undef DEF_FUNCTION_TYPE_0
5094#undef DEF_FUNCTION_TYPE_1
5095#undef DEF_FUNCTION_TYPE_2
5096#undef DEF_FUNCTION_TYPE_3
5097#undef DEF_FUNCTION_TYPE_4
10a0d495
JJ
5098#undef DEF_FUNCTION_TYPE_5
5099#undef DEF_FUNCTION_TYPE_6
a0274e3e 5100#undef DEF_FUNCTION_TYPE_7
acf0174b 5101#undef DEF_FUNCTION_TYPE_8
d9a6bd32
JJ
5102#undef DEF_FUNCTION_TYPE_9
5103#undef DEF_FUNCTION_TYPE_10
5104#undef DEF_FUNCTION_TYPE_11
10841285
MM
5105#undef DEF_FUNCTION_TYPE_VAR_0
5106#undef DEF_FUNCTION_TYPE_VAR_1
5107#undef DEF_FUNCTION_TYPE_VAR_2
08291658 5108#undef DEF_FUNCTION_TYPE_VAR_3
10a0d495
JJ
5109#undef DEF_FUNCTION_TYPE_VAR_4
5110#undef DEF_FUNCTION_TYPE_VAR_5
3e32ee19 5111#undef DEF_FUNCTION_TYPE_VAR_6
56a9f6bc 5112#undef DEF_FUNCTION_TYPE_VAR_7
10841285 5113#undef DEF_POINTER_TYPE
a0274e3e
JJ
5114 BT_LAST
5115};
5116
5117typedef enum c_builtin_type builtin_type;
10841285 5118
a0274e3e
JJ
5119/* A temporary array for c_common_nodes_and_builtins. Used in
5120 communication with def_fn_type. */
5121static tree builtin_types[(int) BT_LAST + 1];
10841285 5122
a0274e3e
JJ
5123/* A helper function for c_common_nodes_and_builtins. Build function type
5124 for DEF with return type RET and N arguments. If VAR is true, then the
5125 function should be variadic after those N arguments.
5126
5127 Takes special care not to ICE if any of the types involved are
5128 error_mark_node, which indicates that said type is not in fact available
5129 (see builtin_type_for_size). In which case the function type as a whole
5130 should be error_mark_node. */
5131
5132static void
5133def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...)
5134{
8242dd04
NF
5135 tree t;
5136 tree *args = XALLOCAVEC (tree, n);
a0274e3e
JJ
5137 va_list list;
5138 int i;
5139
5140 va_start (list, n);
5141 for (i = 0; i < n; ++i)
5142 {
d75d71e0 5143 builtin_type a = (builtin_type) va_arg (list, int);
a0274e3e
JJ
5144 t = builtin_types[a];
5145 if (t == error_mark_node)
5146 goto egress;
8242dd04 5147 args[i] = t;
a0274e3e 5148 }
a0274e3e 5149
a0274e3e
JJ
5150 t = builtin_types[ret];
5151 if (t == error_mark_node)
5152 goto egress;
8242dd04
NF
5153 if (var)
5154 t = build_varargs_function_type_array (t, n, args);
5155 else
5156 t = build_function_type_array (t, n, args);
a0274e3e
JJ
5157
5158 egress:
5159 builtin_types[def] = t;
0edf1bb2 5160 va_end (list);
a0274e3e
JJ
5161}
5162
c6d86fce
ILT
5163/* Build builtin functions common to both C and C++ language
5164 frontends. */
5165
5166static void
5167c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node)
5168{
5169#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \
5170 builtin_types[ENUM] = VALUE;
5171#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \
5172 def_fn_type (ENUM, RETURN, 0, 0);
5173#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \
5174 def_fn_type (ENUM, RETURN, 0, 1, ARG1);
5175#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \
5176 def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2);
5177#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5178 def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3);
5179#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5180 def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4);
5181#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5182 def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
5183#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5184 ARG6) \
5185 def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
5186#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5187 ARG6, ARG7) \
5188 def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
acf0174b
JJ
5189#define DEF_FUNCTION_TYPE_8(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5190 ARG6, ARG7, ARG8) \
5191 def_fn_type (ENUM, RETURN, 0, 8, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5192 ARG7, ARG8);
d9a6bd32
JJ
5193#define DEF_FUNCTION_TYPE_9(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5194 ARG6, ARG7, ARG8, ARG9) \
5195 def_fn_type (ENUM, RETURN, 0, 9, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5196 ARG7, ARG8, ARG9);
5197#define DEF_FUNCTION_TYPE_10(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5198 ARG6, ARG7, ARG8, ARG9, ARG10) \
5199 def_fn_type (ENUM, RETURN, 0, 10, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5200 ARG7, ARG8, ARG9, ARG10);
5201#define DEF_FUNCTION_TYPE_11(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5202 ARG6, ARG7, ARG8, ARG9, ARG10, ARG11) \
5203 def_fn_type (ENUM, RETURN, 0, 11, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, \
5204 ARG7, ARG8, ARG9, ARG10, ARG11);
c6d86fce
ILT
5205#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \
5206 def_fn_type (ENUM, RETURN, 1, 0);
5207#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \
5208 def_fn_type (ENUM, RETURN, 1, 1, ARG1);
5209#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \
5210 def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2);
5211#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \
5212 def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3);
5213#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \
5214 def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4);
5215#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \
5216 def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5);
3e32ee19
NS
5217#define DEF_FUNCTION_TYPE_VAR_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5218 ARG6) \
5219 def_fn_type (ENUM, RETURN, 1, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6);
56a9f6bc
TS
5220#define DEF_FUNCTION_TYPE_VAR_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \
5221 ARG6, ARG7) \
5222 def_fn_type (ENUM, RETURN, 1, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7);
c6d86fce
ILT
5223#define DEF_POINTER_TYPE(ENUM, TYPE) \
5224 builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]);
5225
5226#include "builtin-types.def"
5227
5228#undef DEF_PRIMITIVE_TYPE
f6a7cffc 5229#undef DEF_FUNCTION_TYPE_0
c6d86fce
ILT
5230#undef DEF_FUNCTION_TYPE_1
5231#undef DEF_FUNCTION_TYPE_2
5232#undef DEF_FUNCTION_TYPE_3
5233#undef DEF_FUNCTION_TYPE_4
5234#undef DEF_FUNCTION_TYPE_5
5235#undef DEF_FUNCTION_TYPE_6
f6a7cffc
TS
5236#undef DEF_FUNCTION_TYPE_7
5237#undef DEF_FUNCTION_TYPE_8
d9a6bd32
JJ
5238#undef DEF_FUNCTION_TYPE_9
5239#undef DEF_FUNCTION_TYPE_10
5240#undef DEF_FUNCTION_TYPE_11
c6d86fce
ILT
5241#undef DEF_FUNCTION_TYPE_VAR_0
5242#undef DEF_FUNCTION_TYPE_VAR_1
5243#undef DEF_FUNCTION_TYPE_VAR_2
5244#undef DEF_FUNCTION_TYPE_VAR_3
5245#undef DEF_FUNCTION_TYPE_VAR_4
5246#undef DEF_FUNCTION_TYPE_VAR_5
3e32ee19 5247#undef DEF_FUNCTION_TYPE_VAR_6
56a9f6bc 5248#undef DEF_FUNCTION_TYPE_VAR_7
c6d86fce
ILT
5249#undef DEF_POINTER_TYPE
5250 builtin_types[(int) BT_LAST] = NULL_TREE;
5251
5252 c_init_attributes ();
5253
5254#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \
5255 NONANSI_P, ATTRS, IMPLICIT, COND) \
5256 if (NAME && COND) \
5257 def_builtin_1 (ENUM, NAME, CLASS, \
5258 builtin_types[(int) TYPE], \
5259 builtin_types[(int) LIBTYPE], \
5260 BOTH_P, FALLBACK_P, NONANSI_P, \
5261 built_in_attributes[(int) ATTRS], IMPLICIT);
5262#include "builtins.def"
c6d86fce 5263
8de7ef2a
UB
5264 targetm.init_builtins ();
5265
384c400a 5266 build_common_builtin_nodes ();
939b37da 5267
b72271b9 5268 if (flag_cilkplus)
939b37da 5269 cilk_init_builtins ();
c6d86fce
ILT
5270}
5271
c1b61fca
JM
5272/* Like get_identifier, but avoid warnings about null arguments when
5273 the argument may be NULL for targets where GCC lacks stdint.h type
5274 information. */
5275
5276static inline tree
5277c_get_ident (const char *id)
5278{
5279 return get_identifier (id);
5280}
5281
a0274e3e
JJ
5282/* Build tree nodes and builtin functions common to both C and C++ language
5283 frontends. */
5284
5285void
5286c_common_nodes_and_builtins (void)
5287{
b6baa67d
KVH
5288 int char16_type_size;
5289 int char32_type_size;
eaa7c03f
JM
5290 int wchar_type_size;
5291 tree array_domain_type;
9f720c3e 5292 tree va_list_ref_type_node;
daf68dd7 5293 tree va_list_arg_type_node;
78a7c317 5294 int i;
d3707adb 5295
a011cd92 5296 build_common_tree_nodes (flag_signed_char);
fce5dddd 5297
eaa7c03f 5298 /* Define `int' and `char' first so that dbx will output them first. */
6496a589 5299 record_builtin_type (RID_INT, NULL, integer_type_node);
eaa7c03f
JM
5300 record_builtin_type (RID_CHAR, "char", char_type_node);
5301
5302 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
5303 "unsigned long", "long long unsigned" and "unsigned short" were in C++
5304 but not C. Are the conditionals here needed? */
37fa72e9 5305 if (c_dialect_cxx ())
6496a589 5306 record_builtin_type (RID_SIGNED, NULL, integer_type_node);
eaa7c03f
JM
5307 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
5308 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
5309 record_builtin_type (RID_MAX, "long unsigned int",
5310 long_unsigned_type_node);
78a7c317
DD
5311
5312 for (i = 0; i < NUM_INT_N_ENTS; i ++)
a6766312 5313 {
78a7c317
DD
5314 char name[25];
5315
5316 sprintf (name, "__int%d", int_n_data[i].bitsize);
17958621 5317 record_builtin_type ((enum rid)(RID_FIRST_INT_N + i), name,
78a7c317
DD
5318 int_n_trees[i].signed_type);
5319 sprintf (name, "__int%d unsigned", int_n_data[i].bitsize);
17958621 5320 record_builtin_type (RID_MAX, name, int_n_trees[i].unsigned_type);
a6766312 5321 }
78a7c317 5322
37fa72e9 5323 if (c_dialect_cxx ())
eaa7c03f
JM
5324 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
5325 record_builtin_type (RID_MAX, "long long int",
5326 long_long_integer_type_node);
5327 record_builtin_type (RID_MAX, "long long unsigned int",
5328 long_long_unsigned_type_node);
37fa72e9 5329 if (c_dialect_cxx ())
eaa7c03f
JM
5330 record_builtin_type (RID_MAX, "long long unsigned",
5331 long_long_unsigned_type_node);
5332 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
5333 record_builtin_type (RID_MAX, "short unsigned int",
5334 short_unsigned_type_node);
37fa72e9 5335 if (c_dialect_cxx ())
eaa7c03f
JM
5336 record_builtin_type (RID_MAX, "unsigned short",
5337 short_unsigned_type_node);
5338
5339 /* Define both `signed char' and `unsigned char'. */
5340 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
5341 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
5342
b0c48229
NB
5343 /* These are types that c_common_type_for_size and
5344 c_common_type_for_mode use. */
c2255bc4
AH
5345 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5346 TYPE_DECL, NULL_TREE,
ae2bcd98 5347 intQI_type_node));
c2255bc4
AH
5348 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5349 TYPE_DECL, NULL_TREE,
ae2bcd98 5350 intHI_type_node));
c2255bc4
AH
5351 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5352 TYPE_DECL, NULL_TREE,
ae2bcd98 5353 intSI_type_node));
c2255bc4
AH
5354 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5355 TYPE_DECL, NULL_TREE,
ae2bcd98 5356 intDI_type_node));
eaa7c03f 5357#if HOST_BITS_PER_WIDE_INT >= 64
78a7c317
DD
5358 /* Note that this is different than the __int128 type that's part of
5359 the generic __intN support. */
1e1b8649 5360 if (targetm.scalar_mode_supported_p (TImode))
c2255bc4
AH
5361 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5362 TYPE_DECL,
1e1b8649
AP
5363 get_identifier ("__int128_t"),
5364 intTI_type_node));
eaa7c03f 5365#endif
c2255bc4
AH
5366 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5367 TYPE_DECL, NULL_TREE,
ae2bcd98 5368 unsigned_intQI_type_node));
c2255bc4
AH
5369 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5370 TYPE_DECL, NULL_TREE,
ae2bcd98 5371 unsigned_intHI_type_node));
c2255bc4
AH
5372 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5373 TYPE_DECL, NULL_TREE,
ae2bcd98 5374 unsigned_intSI_type_node));
c2255bc4
AH
5375 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5376 TYPE_DECL, NULL_TREE,
ae2bcd98 5377 unsigned_intDI_type_node));
eaa7c03f 5378#if HOST_BITS_PER_WIDE_INT >= 64
1e1b8649 5379 if (targetm.scalar_mode_supported_p (TImode))
c2255bc4
AH
5380 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5381 TYPE_DECL,
1e1b8649
AP
5382 get_identifier ("__uint128_t"),
5383 unsigned_intTI_type_node));
eaa7c03f
JM
5384#endif
5385
5386 /* Create the widest literal types. */
5387 widest_integer_literal_type_node
5388 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
c2255bc4
AH
5389 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5390 TYPE_DECL, NULL_TREE,
ae2bcd98 5391 widest_integer_literal_type_node));
eaa7c03f
JM
5392
5393 widest_unsigned_literal_type_node
5394 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
c2255bc4
AH
5395 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5396 TYPE_DECL, NULL_TREE,
ae2bcd98 5397 widest_unsigned_literal_type_node));
eaa7c03f 5398
c9f8536c 5399 signed_size_type_node = c_common_signed_type (size_type_node);
eaa7c03f 5400
d1c38823
ZD
5401 pid_type_node =
5402 TREE_TYPE (identifier_global_value (get_identifier (PID_TYPE)));
5403
6496a589
KG
5404 record_builtin_type (RID_FLOAT, NULL, float_type_node);
5405 record_builtin_type (RID_DOUBLE, NULL, double_type_node);
eaa7c03f
JM
5406 record_builtin_type (RID_MAX, "long double", long_double_type_node);
5407
9a8ce21f
JG
5408 /* Only supported decimal floating point extension if the target
5409 actually supports underlying modes. */
b8698a0f 5410 if (targetm.scalar_mode_supported_p (SDmode)
9a8ce21f
JG
5411 && targetm.scalar_mode_supported_p (DDmode)
5412 && targetm.scalar_mode_supported_p (TDmode))
5413 {
5414 record_builtin_type (RID_DFLOAT32, NULL, dfloat32_type_node);
5415 record_builtin_type (RID_DFLOAT64, NULL, dfloat64_type_node);
5416 record_builtin_type (RID_DFLOAT128, NULL, dfloat128_type_node);
5417 }
5418
ab22c1fa
CF
5419 if (targetm.fixed_point_supported_p ())
5420 {
5421 record_builtin_type (RID_MAX, "short _Fract", short_fract_type_node);
5422 record_builtin_type (RID_FRACT, NULL, fract_type_node);
5423 record_builtin_type (RID_MAX, "long _Fract", long_fract_type_node);
5424 record_builtin_type (RID_MAX, "long long _Fract",
5425 long_long_fract_type_node);
5426 record_builtin_type (RID_MAX, "unsigned short _Fract",
5427 unsigned_short_fract_type_node);
5428 record_builtin_type (RID_MAX, "unsigned _Fract",
5429 unsigned_fract_type_node);
5430 record_builtin_type (RID_MAX, "unsigned long _Fract",
5431 unsigned_long_fract_type_node);
5432 record_builtin_type (RID_MAX, "unsigned long long _Fract",
5433 unsigned_long_long_fract_type_node);
5434 record_builtin_type (RID_MAX, "_Sat short _Fract",
5435 sat_short_fract_type_node);
5436 record_builtin_type (RID_MAX, "_Sat _Fract", sat_fract_type_node);
5437 record_builtin_type (RID_MAX, "_Sat long _Fract",
5438 sat_long_fract_type_node);
5439 record_builtin_type (RID_MAX, "_Sat long long _Fract",
5440 sat_long_long_fract_type_node);
5441 record_builtin_type (RID_MAX, "_Sat unsigned short _Fract",
5442 sat_unsigned_short_fract_type_node);
5443 record_builtin_type (RID_MAX, "_Sat unsigned _Fract",
5444 sat_unsigned_fract_type_node);
5445 record_builtin_type (RID_MAX, "_Sat unsigned long _Fract",
5446 sat_unsigned_long_fract_type_node);
5447 record_builtin_type (RID_MAX, "_Sat unsigned long long _Fract",
5448 sat_unsigned_long_long_fract_type_node);
5449 record_builtin_type (RID_MAX, "short _Accum", short_accum_type_node);
5450 record_builtin_type (RID_ACCUM, NULL, accum_type_node);
5451 record_builtin_type (RID_MAX, "long _Accum", long_accum_type_node);
5452 record_builtin_type (RID_MAX, "long long _Accum",
5453 long_long_accum_type_node);
5454 record_builtin_type (RID_MAX, "unsigned short _Accum",
5455 unsigned_short_accum_type_node);
5456 record_builtin_type (RID_MAX, "unsigned _Accum",
5457 unsigned_accum_type_node);
5458 record_builtin_type (RID_MAX, "unsigned long _Accum",
5459 unsigned_long_accum_type_node);
5460 record_builtin_type (RID_MAX, "unsigned long long _Accum",
5461 unsigned_long_long_accum_type_node);
5462 record_builtin_type (RID_MAX, "_Sat short _Accum",
5463 sat_short_accum_type_node);
5464 record_builtin_type (RID_MAX, "_Sat _Accum", sat_accum_type_node);
5465 record_builtin_type (RID_MAX, "_Sat long _Accum",
5466 sat_long_accum_type_node);
5467 record_builtin_type (RID_MAX, "_Sat long long _Accum",
5468 sat_long_long_accum_type_node);
5469 record_builtin_type (RID_MAX, "_Sat unsigned short _Accum",
5470 sat_unsigned_short_accum_type_node);
5471 record_builtin_type (RID_MAX, "_Sat unsigned _Accum",
5472 sat_unsigned_accum_type_node);
5473 record_builtin_type (RID_MAX, "_Sat unsigned long _Accum",
5474 sat_unsigned_long_accum_type_node);
5475 record_builtin_type (RID_MAX, "_Sat unsigned long long _Accum",
5476 sat_unsigned_long_long_accum_type_node);
5477
5478 }
5479
c2255bc4
AH
5480 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5481 TYPE_DECL,
ae2bcd98
RS
5482 get_identifier ("complex int"),
5483 complex_integer_type_node));
c2255bc4
AH
5484 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5485 TYPE_DECL,
ae2bcd98
RS
5486 get_identifier ("complex float"),
5487 complex_float_type_node));
c2255bc4
AH
5488 lang_hooks.decls.pushdecl (build_decl (UNKNOWN_LOCATION,
5489 TYPE_DECL,
ae2bcd98
RS
5490 get_identifier ("complex double"),
5491 complex_double_type_node));
5492 lang_hooks.decls.pushdecl
c2255bc4
AH
5493 (build_decl (UNKNOWN_LOCATION,
5494 TYPE_DECL, get_identifier ("complex long double"),
43577e6b 5495 complex_long_double_type_node));
eaa7c03f 5496
498c0f27
JJ
5497 if (c_dialect_cxx ())
5498 /* For C++, make fileptr_type_node a distinct void * type until
5499 FILE type is defined. */
8dd16ecc 5500 fileptr_type_node = build_variant_type_copy (ptr_type_node);
498c0f27 5501
6496a589 5502 record_builtin_type (RID_VOID, NULL, void_type_node);
eaa7c03f 5503
06d40de8
DG
5504 /* Set the TYPE_NAME for any variants that were built before
5505 record_builtin_type gave names to the built-in types. */
5506 {
5507 tree void_name = TYPE_NAME (void_type_node);
5508 TYPE_NAME (void_type_node) = NULL_TREE;
5509 TYPE_NAME (build_qualified_type (void_type_node, TYPE_QUAL_CONST))
5510 = void_name;
5511 TYPE_NAME (void_type_node) = void_name;
5512 }
5513
eaa7c03f
JM
5514 void_list_node = build_void_list_node ();
5515
5516 /* Make a type to be the domain of a few array types
5517 whose domains don't really matter.
5518 200 is small enough that it always fits in size_t
5519 and large enough that it can hold most function names for the
5520 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
5521 array_domain_type = build_index_type (size_int (200));
5522
5523 /* Make a type for arrays of characters.
5524 With luck nothing will ever really depend on the length of this
5525 array type. */
5526 char_array_type_node
5527 = build_array_type (char_type_node, array_domain_type);
5528
10841285
MM
5529 string_type_node = build_pointer_type (char_type_node);
5530 const_string_type_node
5531 = build_pointer_type (build_qualified_type
5532 (char_type_node, TYPE_QUAL_CONST));
5533
eaa7c03f 5534 /* This is special for C++ so functions can be overloaded. */
a11eba95 5535 wchar_type_node = get_identifier (MODIFIED_WCHAR_TYPE);
eaa7c03f
JM
5536 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
5537 wchar_type_size = TYPE_PRECISION (wchar_type_node);
207bf79d 5538 underlying_wchar_type_node = wchar_type_node;
37fa72e9 5539 if (c_dialect_cxx ())
eaa7c03f 5540 {
8df83eae 5541 if (TYPE_UNSIGNED (wchar_type_node))
eaa7c03f
JM
5542 wchar_type_node = make_unsigned_type (wchar_type_size);
5543 else
5544 wchar_type_node = make_signed_type (wchar_type_size);
5545 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
5546 }
eaa7c03f
JM
5547
5548 /* This is for wide string constants. */
5549 wchar_array_type_node
5550 = build_array_type (wchar_type_node, array_domain_type);
5551
b6baa67d
KVH
5552 /* Define 'char16_t'. */
5553 char16_type_node = get_identifier (CHAR16_TYPE);
5554 char16_type_node = TREE_TYPE (identifier_global_value (char16_type_node));
5555 char16_type_size = TYPE_PRECISION (char16_type_node);
5556 if (c_dialect_cxx ())
5557 {
5558 char16_type_node = make_unsigned_type (char16_type_size);
5559
604b2bfc 5560 if (cxx_dialect >= cxx11)
b6baa67d
KVH
5561 record_builtin_type (RID_CHAR16, "char16_t", char16_type_node);
5562 }
5563
5564 /* This is for UTF-16 string constants. */
5565 char16_array_type_node
5566 = build_array_type (char16_type_node, array_domain_type);
5567
5568 /* Define 'char32_t'. */
5569 char32_type_node = get_identifier (CHAR32_TYPE);
5570 char32_type_node = TREE_TYPE (identifier_global_value (char32_type_node));
5571 char32_type_size = TYPE_PRECISION (char32_type_node);
5572 if (c_dialect_cxx ())
5573 {
5574 char32_type_node = make_unsigned_type (char32_type_size);
5575
604b2bfc 5576 if (cxx_dialect >= cxx11)
b6baa67d
KVH
5577 record_builtin_type (RID_CHAR32, "char32_t", char32_type_node);
5578 }
5579
5580 /* This is for UTF-32 string constants. */
5581 char32_array_type_node
5582 = build_array_type (char32_type_node, array_domain_type);
5583
5fd8e536
JM
5584 wint_type_node =
5585 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
5586
5587 intmax_type_node =
5588 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
5589 uintmax_type_node =
5590 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
5591
207bf79d
JM
5592 if (SIG_ATOMIC_TYPE)
5593 sig_atomic_type_node =
c1b61fca 5594 TREE_TYPE (identifier_global_value (c_get_ident (SIG_ATOMIC_TYPE)));
207bf79d
JM
5595 if (INT8_TYPE)
5596 int8_type_node =
c1b61fca 5597 TREE_TYPE (identifier_global_value (c_get_ident (INT8_TYPE)));
207bf79d
JM
5598 if (INT16_TYPE)
5599 int16_type_node =
c1b61fca 5600 TREE_TYPE (identifier_global_value (c_get_ident (INT16_TYPE)));
207bf79d
JM
5601 if (INT32_TYPE)
5602 int32_type_node =
c1b61fca 5603 TREE_TYPE (identifier_global_value (c_get_ident (INT32_TYPE)));
207bf79d
JM
5604 if (INT64_TYPE)
5605 int64_type_node =
c1b61fca 5606 TREE_TYPE (identifier_global_value (c_get_ident (INT64_TYPE)));
207bf79d
JM
5607 if (UINT8_TYPE)
5608 uint8_type_node =
c1b61fca 5609 TREE_TYPE (identifier_global_value (c_get_ident (UINT8_TYPE)));
207bf79d 5610 if (UINT16_TYPE)
cca615af 5611 c_uint16_type_node = uint16_type_node =
c1b61fca 5612 TREE_TYPE (identifier_global_value (c_get_ident (UINT16_TYPE)));
207bf79d 5613 if (UINT32_TYPE)
cca615af 5614 c_uint32_type_node = uint32_type_node =
c1b61fca 5615 TREE_TYPE (identifier_global_value (c_get_ident (UINT32_TYPE)));
207bf79d 5616 if (UINT64_TYPE)
cca615af 5617 c_uint64_type_node = uint64_type_node =
c1b61fca 5618 TREE_TYPE (identifier_global_value (c_get_ident (UINT64_TYPE)));
207bf79d
JM
5619 if (INT_LEAST8_TYPE)
5620 int_least8_type_node =
c1b61fca 5621 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST8_TYPE)));
207bf79d
JM
5622 if (INT_LEAST16_TYPE)
5623 int_least16_type_node =
c1b61fca 5624 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST16_TYPE)));
207bf79d
JM
5625 if (INT_LEAST32_TYPE)
5626 int_least32_type_node =
c1b61fca 5627 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST32_TYPE)));
207bf79d
JM
5628 if (INT_LEAST64_TYPE)
5629 int_least64_type_node =
c1b61fca 5630 TREE_TYPE (identifier_global_value (c_get_ident (INT_LEAST64_TYPE)));
207bf79d
JM
5631 if (UINT_LEAST8_TYPE)
5632 uint_least8_type_node =
c1b61fca 5633 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST8_TYPE)));
207bf79d
JM
5634 if (UINT_LEAST16_TYPE)
5635 uint_least16_type_node =
c1b61fca 5636 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST16_TYPE)));
207bf79d
JM
5637 if (UINT_LEAST32_TYPE)
5638 uint_least32_type_node =
c1b61fca 5639 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST32_TYPE)));
207bf79d
JM
5640 if (UINT_LEAST64_TYPE)
5641 uint_least64_type_node =
c1b61fca 5642 TREE_TYPE (identifier_global_value (c_get_ident (UINT_LEAST64_TYPE)));
207bf79d
JM
5643 if (INT_FAST8_TYPE)
5644 int_fast8_type_node =
c1b61fca 5645 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST8_TYPE)));
207bf79d
JM
5646 if (INT_FAST16_TYPE)
5647 int_fast16_type_node =
c1b61fca 5648 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST16_TYPE)));
207bf79d
JM
5649 if (INT_FAST32_TYPE)
5650 int_fast32_type_node =
c1b61fca 5651 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST32_TYPE)));
207bf79d
JM
5652 if (INT_FAST64_TYPE)
5653 int_fast64_type_node =
c1b61fca 5654 TREE_TYPE (identifier_global_value (c_get_ident (INT_FAST64_TYPE)));
207bf79d
JM
5655 if (UINT_FAST8_TYPE)
5656 uint_fast8_type_node =
c1b61fca 5657 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST8_TYPE)));
207bf79d
JM
5658 if (UINT_FAST16_TYPE)
5659 uint_fast16_type_node =
c1b61fca 5660 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST16_TYPE)));
207bf79d
JM
5661 if (UINT_FAST32_TYPE)
5662 uint_fast32_type_node =
c1b61fca 5663 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST32_TYPE)));
207bf79d
JM
5664 if (UINT_FAST64_TYPE)
5665 uint_fast64_type_node =
c1b61fca 5666 TREE_TYPE (identifier_global_value (c_get_ident (UINT_FAST64_TYPE)));
207bf79d
JM
5667 if (INTPTR_TYPE)
5668 intptr_type_node =
c1b61fca 5669 TREE_TYPE (identifier_global_value (c_get_ident (INTPTR_TYPE)));
207bf79d
JM
5670 if (UINTPTR_TYPE)
5671 uintptr_type_node =
c1b61fca 5672 TREE_TYPE (identifier_global_value (c_get_ident (UINTPTR_TYPE)));
207bf79d 5673
8242dd04
NF
5674 default_function_type
5675 = build_varargs_function_type_list (integer_type_node, NULL_TREE);
5fd8e536
JM
5676 ptrdiff_type_node
5677 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
12753674 5678 unsigned_ptrdiff_type_node = c_common_unsigned_type (ptrdiff_type_node);
5fd8e536 5679
ae2bcd98 5680 lang_hooks.decls.pushdecl
c2255bc4
AH
5681 (build_decl (UNKNOWN_LOCATION,
5682 TYPE_DECL, get_identifier ("__builtin_va_list"),
43577e6b 5683 va_list_type_node));
38f8b050 5684 if (targetm.enum_va_list_p)
d4048208
KT
5685 {
5686 int l;
5687 const char *pname;
5688 tree ptype;
35cbb299 5689
38f8b050 5690 for (l = 0; targetm.enum_va_list_p (l, &pname, &ptype); ++l)
d4048208
KT
5691 {
5692 lang_hooks.decls.pushdecl
5693 (build_decl (UNKNOWN_LOCATION,
5694 TYPE_DECL, get_identifier (pname),
5695 ptype));
5696
5697 }
5698 }
daf68dd7 5699
daf68dd7 5700 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
9f720c3e
GK
5701 {
5702 va_list_arg_type_node = va_list_ref_type_node =
5703 build_pointer_type (TREE_TYPE (va_list_type_node));
5704 }
daf68dd7 5705 else
9f720c3e
GK
5706 {
5707 va_list_arg_type_node = va_list_type_node;
5708 va_list_ref_type_node = build_reference_type (va_list_type_node);
5709 }
35b1a6fa 5710
c6d86fce
ILT
5711 if (!flag_preprocess_only)
5712 c_define_builtins (va_list_ref_type_node, va_list_arg_type_node);
4677862a 5713
5b47282c 5714 main_identifier_node = get_identifier ("main");
b2f97e4a
MM
5715
5716 /* Create the built-in __null node. It is important that this is
5717 not shared. */
807e902e 5718 null_node = make_int_cst (1, 1);
b2f97e4a 5719 TREE_TYPE (null_node) = c_common_type_for_size (POINTER_SIZE, 0);
a0274e3e
JJ
5720
5721 /* Since builtin_types isn't gc'ed, don't export these nodes. */
5722 memset (builtin_types, 0, sizeof (builtin_types));
7f4edbcb 5723}
d3707adb 5724
3b2db49f
MM
5725/* The number of named compound-literals generated thus far. */
5726static GTY(()) int compound_literal_number;
5727
5728/* Set DECL_NAME for DECL, a VAR_DECL for a compound-literal. */
5729
5730void
5731set_compound_literal_name (tree decl)
5732{
5733 char *name;
5734 ASM_FORMAT_PRIVATE_NAME (name, "__compound_literal",
5735 compound_literal_number);
5736 compound_literal_number++;
5737 DECL_NAME (decl) = get_identifier (name);
5738}
5739
2fe1d762
TV
5740/* build_va_arg helper function. Return a VA_ARG_EXPR with location LOC, type
5741 TYPE and operand OP. */
5742
5743static tree
5744build_va_arg_1 (location_t loc, tree type, tree op)
5745{
5746 tree expr = build1 (VA_ARG_EXPR, type, op);
5747 SET_EXPR_LOCATION (expr, loc);
5748 return expr;
5749}
5750
5751/* Return a VA_ARG_EXPR corresponding to a source-level expression
5752 va_arg (EXPR, TYPE) at source location LOC. */
5753
d3707adb 5754tree
c2255bc4 5755build_va_arg (location_t loc, tree expr, tree type)
d3707adb 5756{
c7b38fd5
TV
5757 tree va_type = TREE_TYPE (expr);
5758 tree canon_va_type = (va_type == error_mark_node
5759 ? NULL_TREE
5760 : targetm.canonical_va_list_type (va_type));
5761
2fe1d762
TV
5762 if (va_type == error_mark_node
5763 || canon_va_type == NULL_TREE)
c7b38fd5 5764 {
2fe1d762
TV
5765 /* Let's handle things neutrallly, if expr:
5766 - has undeclared type, or
5767 - is not an va_list type. */
5768 return build_va_arg_1 (loc, type, expr);
c7b38fd5 5769 }
ecd0e562 5770
2fe1d762
TV
5771 if (TREE_CODE (canon_va_type) != ARRAY_TYPE)
5772 {
5773 /* Case 1: Not an array type. */
5774
5775 /* Take the address, to get '&ap'. */
5776 mark_addressable (expr);
5777 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (expr)), expr);
5778
5779 /* Verify that &ap is still recognized as having va_list type. */
5780 tree canon_expr_type
5781 = targetm.canonical_va_list_type (TREE_TYPE (expr));
1e77281b
MP
5782 if (canon_expr_type == NULL_TREE)
5783 {
5784 error_at (loc,
5785 "first argument to %<va_arg%> not of type %<va_list%>");
5786 return error_mark_node;
5787 }
2fe1d762
TV
5788
5789 return build_va_arg_1 (loc, type, expr);
5790 }
5791
5792 /* Case 2: Array type.
5793
5794 Background:
5795
5796 For contrast, let's start with the simple case (case 1). If
5797 canon_va_type is not an array type, but say a char *, then when
5798 passing-by-value a va_list, the type of the va_list param decl is
5799 the same as for another va_list decl (all ap's are char *):
5800
5801 f2_1 (char * ap)
5802 D.1815 = VA_ARG (&ap, 0B, 1);
5803 return D.1815;
5804
5805 f2 (int i)
5806 char * ap.0;
5807 char * ap;
5808 __builtin_va_start (&ap, 0);
5809 ap.0 = ap;
5810 res = f2_1 (ap.0);
5811 __builtin_va_end (&ap);
5812 D.1812 = res;
5813 return D.1812;
5814
5815 However, if canon_va_type is ARRAY_TYPE, then when passing-by-value a
5816 va_list the type of the va_list param decl (case 2b, struct * ap) is not
5817 the same as for another va_list decl (case 2a, struct ap[1]).
5818
5819 f2_1 (struct * ap)
5820 D.1844 = VA_ARG (ap, 0B, 0);
5821 return D.1844;
5822
5823 f2 (int i)
5824 struct ap[1];
5825 __builtin_va_start (&ap, 0);
5826 res = f2_1 (&ap);
5827 __builtin_va_end (&ap);
5828 D.1841 = res;
5829 return D.1841;
5830
5831 Case 2b is different because:
5832 - on the callee side, the parm decl has declared type va_list, but
5833 grokdeclarator changes the type of the parm decl to a pointer to the
5834 array elem type.
5835 - on the caller side, the pass-by-value uses &ap.
5836
5837 We unify these two cases (case 2a: va_list is array type,
5838 case 2b: va_list is pointer to array elem type), by adding '&' for the
5839 array type case, such that we have a pointer to array elem in both
5840 cases. */
5841
5842 if (TREE_CODE (va_type) == ARRAY_TYPE)
5843 {
5844 /* Case 2a: va_list is array type. */
5845
5846 /* Take the address, to get '&ap'. Make sure it's a pointer to array
5847 elem type. */
5848 mark_addressable (expr);
5849 expr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (canon_va_type)),
5850 expr);
5851
5852 /* Verify that &ap is still recognized as having va_list type. */
5853 tree canon_expr_type
5854 = targetm.canonical_va_list_type (TREE_TYPE (expr));
1e77281b
MP
5855 if (canon_expr_type == NULL_TREE)
5856 {
5857 error_at (loc,
5858 "first argument to %<va_arg%> not of type %<va_list%>");
5859 return error_mark_node;
5860 }
2fe1d762
TV
5861 }
5862 else
5863 {
5864 /* Case 2b: va_list is pointer to array elem type. */
5865 gcc_assert (POINTER_TYPE_P (va_type));
5866 gcc_assert (TREE_TYPE (va_type) == TREE_TYPE (canon_va_type));
5867
5868 /* Don't take the address. We've already got '&ap'. */
5869 ;
5870 }
5871
5872 return build_va_arg_1 (loc, type, expr);
d3707adb 5873}
fc2aaf30
JM
5874
5875
7d14c755
JM
5876/* Linked list of disabled built-in functions. */
5877
a79683d5 5878struct disabled_builtin
7d14c755
JM
5879{
5880 const char *name;
5881 struct disabled_builtin *next;
a79683d5 5882};
7d14c755
JM
5883static disabled_builtin *disabled_builtins = NULL;
5884
35b1a6fa 5885static bool builtin_function_disabled_p (const char *);
7d14c755
JM
5886
5887/* Disable a built-in function specified by -fno-builtin-NAME. If NAME
5888 begins with "__builtin_", give an error. */
5889
5890void
35b1a6fa 5891disable_builtin_function (const char *name)
7d14c755
JM
5892{
5893 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
bda67431 5894 error ("cannot disable built-in function %qs", name);
7d14c755
JM
5895 else
5896 {
1ad463f4
BI
5897 disabled_builtin *new_disabled_builtin = XNEW (disabled_builtin);
5898 new_disabled_builtin->name = name;
5899 new_disabled_builtin->next = disabled_builtins;
5900 disabled_builtins = new_disabled_builtin;
7d14c755
JM
5901 }
5902}
5903
5904
5905/* Return true if the built-in function NAME has been disabled, false
5906 otherwise. */
5907
5908static bool
35b1a6fa 5909builtin_function_disabled_p (const char *name)
7d14c755
JM
5910{
5911 disabled_builtin *p;
5912 for (p = disabled_builtins; p != NULL; p = p->next)
5913 {
5914 if (strcmp (name, p->name) == 0)
5915 return true;
5916 }
5917 return false;
5918}
5919
5920
9bc15050
RG
5921/* Worker for DEF_BUILTIN.
5922 Possibly define a builtin function with one or two names.
5923 Does not declare a non-__builtin_ function if flag_no_builtin, or if
5924 nonansi_p and flag_no_nonansi_builtin. */
fc2aaf30 5925
9bc15050
RG
5926static void
5927def_builtin_1 (enum built_in_function fncode,
5928 const char *name,
5929 enum built_in_class fnclass,
5930 tree fntype, tree libtype,
5931 bool both_p, bool fallback_p, bool nonansi_p,
5932 tree fnattrs, bool implicit_p)
fc2aaf30 5933{
9bc15050
RG
5934 tree decl;
5935 const char *libname;
5936
a0274e3e
JJ
5937 if (fntype == error_mark_node)
5938 return;
5939
9bc15050
RG
5940 gcc_assert ((!both_p && !fallback_p)
5941 || !strncmp (name, "__builtin_",
5942 strlen ("__builtin_")));
5943
5944 libname = name + strlen ("__builtin_");
c79efc4d
RÁE
5945 decl = add_builtin_function (name, fntype, fncode, fnclass,
5946 (fallback_p ? libname : NULL),
5947 fnattrs);
e79983f4
MM
5948
5949 set_builtin_decl (fncode, decl, implicit_p);
5950
9bc15050
RG
5951 if (both_p
5952 && !flag_no_builtin && !builtin_function_disabled_p (libname)
7d14c755 5953 && !(nonansi_p && flag_no_nonansi_builtin))
c79efc4d
RÁE
5954 add_builtin_function (libname, libtype, fncode, fnclass,
5955 NULL, fnattrs);
fc2aaf30 5956}
c530479e 5957\f
d72040f5
RH
5958/* Nonzero if the type T promotes to int. This is (nearly) the
5959 integral promotions defined in ISO C99 6.3.1.1/2. */
5960
5961bool
58f9752a 5962c_promoting_integer_type_p (const_tree t)
d72040f5
RH
5963{
5964 switch (TREE_CODE (t))
5965 {
5966 case INTEGER_TYPE:
5967 return (TYPE_MAIN_VARIANT (t) == char_type_node
5968 || TYPE_MAIN_VARIANT (t) == signed_char_type_node
5969 || TYPE_MAIN_VARIANT (t) == unsigned_char_type_node
5970 || TYPE_MAIN_VARIANT (t) == short_integer_type_node
c6c04fca
RL
5971 || TYPE_MAIN_VARIANT (t) == short_unsigned_type_node
5972 || TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node));
d72040f5
RH
5973
5974 case ENUMERAL_TYPE:
5975 /* ??? Technically all enumerations not larger than an int
5976 promote to an int. But this is used along code paths
5977 that only want to notice a size change. */
5978 return TYPE_PRECISION (t) < TYPE_PRECISION (integer_type_node);
5979
5980 case BOOLEAN_TYPE:
5981 return 1;
5982
5983 default:
5984 return 0;
5985 }
5986}
5987
c530479e
RH
5988/* Return 1 if PARMS specifies a fixed number of parameters
5989 and none of their types is affected by default promotions. */
5990
5991int
58f9752a 5992self_promoting_args_p (const_tree parms)
c530479e 5993{
58f9752a 5994 const_tree t;
c530479e
RH
5995 for (t = parms; t; t = TREE_CHAIN (t))
5996 {
b3694847 5997 tree type = TREE_VALUE (t);
7e8176d7 5998
694fea20
VR
5999 if (type == error_mark_node)
6000 continue;
6001
c530479e
RH
6002 if (TREE_CHAIN (t) == 0 && type != void_type_node)
6003 return 0;
6004
6005 if (type == 0)
6006 return 0;
6007
6008 if (TYPE_MAIN_VARIANT (type) == float_type_node)
6009 return 0;
6010
d72040f5 6011 if (c_promoting_integer_type_p (type))
c530479e
RH
6012 return 0;
6013 }
6014 return 1;
6015}
5eda3d66 6016
12ea3302
GDR
6017/* Recursively remove any '*' or '&' operator from TYPE. */
6018tree
6019strip_pointer_operator (tree t)
6020{
6021 while (POINTER_TYPE_P (t))
6022 t = TREE_TYPE (t);
6023 return t;
6024}
6025
ba992967
SP
6026/* Recursively remove pointer or array type from TYPE. */
6027tree
6028strip_pointer_or_array_types (tree t)
6029{
6030 while (TREE_CODE (t) == ARRAY_TYPE || POINTER_TYPE_P (t))
6031 t = TREE_TYPE (t);
6032 return t;
6033}
6034
8f17b5c5
MM
6035/* Used to compare case labels. K1 and K2 are actually tree nodes
6036 representing case labels, or NULL_TREE for a `default' label.
6037 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
6038 K2, and 0 if K1 and K2 are equal. */
6039
6040int
35b1a6fa 6041case_compare (splay_tree_key k1, splay_tree_key k2)
8f17b5c5
MM
6042{
6043 /* Consider a NULL key (such as arises with a `default' label) to be
6044 smaller than anything else. */
6045 if (!k1)
6046 return k2 ? -1 : 0;
6047 else if (!k2)
6048 return k1 ? 1 : 0;
6049
6050 return tree_int_cst_compare ((tree) k1, (tree) k2);
6051}
6052
c2255bc4
AH
6053/* Process a case label, located at LOC, for the range LOW_VALUE
6054 ... HIGH_VALUE. If LOW_VALUE and HIGH_VALUE are both NULL_TREE
6055 then this case label is actually a `default' label. If only
6056 HIGH_VALUE is NULL_TREE, then case label was declared using the
6057 usual C/C++ syntax, rather than the GNU case range extension.
6058 CASES is a tree containing all the case ranges processed so far;
b155cfd9
MP
6059 COND is the condition for the switch-statement itself.
6060 OUTSIDE_RANGE_P says whether there was a case value that doesn't
6061 fit into the range of the ORIG_TYPE. Returns the CASE_LABEL_EXPR
6062 created, or ERROR_MARK_NODE if no CASE_LABEL_EXPR is created. */
8f17b5c5
MM
6063
6064tree
c2255bc4 6065c_add_case_label (location_t loc, splay_tree cases, tree cond, tree orig_type,
b155cfd9 6066 tree low_value, tree high_value, bool *outside_range_p)
8f17b5c5
MM
6067{
6068 tree type;
6069 tree label;
6070 tree case_label;
6071 splay_tree_node node;
6072
6073 /* Create the LABEL_DECL itself. */
c2255bc4 6074 label = create_artificial_label (loc);
8f17b5c5
MM
6075
6076 /* If there was an error processing the switch condition, bail now
6077 before we get more confused. */
6078 if (!cond || cond == error_mark_node)
6de9cd9a 6079 goto error_out;
8f17b5c5 6080
35b1a6fa
AJ
6081 if ((low_value && TREE_TYPE (low_value)
6082 && POINTER_TYPE_P (TREE_TYPE (low_value)))
8f17b5c5
MM
6083 || (high_value && TREE_TYPE (high_value)
6084 && POINTER_TYPE_P (TREE_TYPE (high_value))))
522ddfa2 6085 {
c2255bc4 6086 error_at (loc, "pointers are not permitted as case values");
522ddfa2
JM
6087 goto error_out;
6088 }
8f17b5c5
MM
6089
6090 /* Case ranges are a GNU extension. */
fcf73884 6091 if (high_value)
c1771a20 6092 pedwarn (loc, OPT_Wpedantic,
fcf73884 6093 "range expressions in switch statements are non-standard");
8f17b5c5
MM
6094
6095 type = TREE_TYPE (cond);
6096 if (low_value)
6097 {
62e4eb35 6098 low_value = check_case_value (loc, low_value);
68fca595 6099 low_value = convert_and_check (loc, type, low_value);
c0e22534
NS
6100 if (low_value == error_mark_node)
6101 goto error_out;
8f17b5c5
MM
6102 }
6103 if (high_value)
6104 {
62e4eb35 6105 high_value = check_case_value (loc, high_value);
68fca595 6106 high_value = convert_and_check (loc, type, high_value);
c0e22534
NS
6107 if (high_value == error_mark_node)
6108 goto error_out;
8f17b5c5
MM
6109 }
6110
c0e22534
NS
6111 if (low_value && high_value)
6112 {
6113 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
c22cacf3
MS
6114 really a case range, even though it was written that way.
6115 Remove the HIGH_VALUE to simplify later processing. */
c0e22534
NS
6116 if (tree_int_cst_equal (low_value, high_value))
6117 high_value = NULL_TREE;
6118 else if (!tree_int_cst_lt (low_value, high_value))
c2255bc4 6119 warning_at (loc, 0, "empty range specified");
c0e22534 6120 }
8f17b5c5 6121
a6c0a76c
SB
6122 /* See if the case is in range of the type of the original testing
6123 expression. If both low_value and high_value are out of range,
6124 don't insert the case label and return NULL_TREE. */
6125 if (low_value
9d548dfb 6126 && !check_case_bounds (loc, type, orig_type,
b155cfd9
MP
6127 &low_value, high_value ? &high_value : NULL,
6128 outside_range_p))
a6c0a76c
SB
6129 return NULL_TREE;
6130
8f17b5c5
MM
6131 /* Look up the LOW_VALUE in the table of case labels we already
6132 have. */
6133 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
6134 /* If there was not an exact match, check for overlapping ranges.
6135 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
6136 that's a `default' label and the only overlap is an exact match. */
6137 if (!node && (low_value || high_value))
6138 {
6139 splay_tree_node low_bound;
6140 splay_tree_node high_bound;
6141
6142 /* Even though there wasn't an exact match, there might be an
6143 overlap between this case range and another case range.
6144 Since we've (inductively) not allowed any overlapping case
6145 ranges, we simply need to find the greatest low case label
6146 that is smaller that LOW_VALUE, and the smallest low case
6147 label that is greater than LOW_VALUE. If there is an overlap
6148 it will occur in one of these two ranges. */
6149 low_bound = splay_tree_predecessor (cases,
6150 (splay_tree_key) low_value);
6151 high_bound = splay_tree_successor (cases,
6152 (splay_tree_key) low_value);
6153
6154 /* Check to see if the LOW_BOUND overlaps. It is smaller than
6155 the LOW_VALUE, so there is no need to check unless the
6156 LOW_BOUND is in fact itself a case range. */
6157 if (low_bound
6158 && CASE_HIGH ((tree) low_bound->value)
6159 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
6160 low_value) >= 0)
6161 node = low_bound;
6162 /* Check to see if the HIGH_BOUND overlaps. The low end of that
6163 range is bigger than the low end of the current range, so we
6164 are only interested if the current range is a real range, and
6165 not an ordinary case label. */
35b1a6fa 6166 else if (high_bound
8f17b5c5
MM
6167 && high_value
6168 && (tree_int_cst_compare ((tree) high_bound->key,
6169 high_value)
6170 <= 0))
6171 node = high_bound;
6172 }
6173 /* If there was an overlap, issue an error. */
6174 if (node)
6175 {
8c161995 6176 tree duplicate = CASE_LABEL ((tree) node->value);
8f17b5c5
MM
6177
6178 if (high_value)
6179 {
c2255bc4
AH
6180 error_at (loc, "duplicate (or overlapping) case value");
6181 error_at (DECL_SOURCE_LOCATION (duplicate),
6182 "this is the first entry overlapping that value");
8f17b5c5
MM
6183 }
6184 else if (low_value)
6185 {
c2255bc4
AH
6186 error_at (loc, "duplicate case value") ;
6187 error_at (DECL_SOURCE_LOCATION (duplicate), "previously used here");
8f17b5c5
MM
6188 }
6189 else
6190 {
c2255bc4
AH
6191 error_at (loc, "multiple default labels in one switch");
6192 error_at (DECL_SOURCE_LOCATION (duplicate),
6193 "this is the first default label");
8f17b5c5 6194 }
6de9cd9a 6195 goto error_out;
8f17b5c5
MM
6196 }
6197
6198 /* Add a CASE_LABEL to the statement-tree. */
3d528853 6199 case_label = add_stmt (build_case_label (low_value, high_value, label));
8f17b5c5 6200 /* Register this case label in the splay tree. */
35b1a6fa 6201 splay_tree_insert (cases,
8f17b5c5
MM
6202 (splay_tree_key) low_value,
6203 (splay_tree_value) case_label);
6204
6205 return case_label;
6de9cd9a
DN
6206
6207 error_out:
9e14e18f 6208 /* Add a label so that the back-end doesn't think that the beginning of
6de9cd9a 6209 the switch is unreachable. Note that we do not add a case label, as
41806d92 6210 that just leads to duplicates and thence to failure later on. */
6de9cd9a
DN
6211 if (!cases->root)
6212 {
c2255bc4
AH
6213 tree t = create_artificial_label (loc);
6214 add_stmt (build_stmt (loc, LABEL_EXPR, t));
6de9cd9a
DN
6215 }
6216 return error_mark_node;
6217}
6218
6219/* Subroutines of c_do_switch_warnings, called via splay_tree_foreach.
6220 Used to verify that case values match up with enumerator values. */
6221
6222static void
6223match_case_to_enum_1 (tree key, tree type, tree label)
6224{
807e902e
KZ
6225 char buf[WIDE_INT_PRINT_BUFFER_SIZE];
6226
6227 if (tree_fits_uhwi_p (key))
6228 print_dec (key, buf, UNSIGNED);
6229 else if (tree_fits_shwi_p (key))
6230 print_dec (key, buf, SIGNED);
6de9cd9a 6231 else
807e902e 6232 print_hex (key, buf);
6de9cd9a
DN
6233
6234 if (TYPE_NAME (type) == 0)
c5d75364
MLI
6235 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6236 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6237 "case value %qs not in enumerated type",
6238 buf);
6de9cd9a 6239 else
c5d75364
MLI
6240 warning_at (DECL_SOURCE_LOCATION (CASE_LABEL (label)),
6241 warn_switch ? OPT_Wswitch : OPT_Wswitch_enum,
6242 "case value %qs not in enumerated type %qT",
6243 buf, type);
6de9cd9a
DN
6244}
6245
c782c2fe
RS
6246/* Subroutine of c_do_switch_warnings, called via splay_tree_foreach.
6247 Used to verify that case values match up with enumerator values. */
6248
6de9cd9a
DN
6249static int
6250match_case_to_enum (splay_tree_node node, void *data)
6251{
6252 tree label = (tree) node->value;
28dab132 6253 tree type = (tree) data;
6de9cd9a
DN
6254
6255 /* Skip default case. */
6256 if (!CASE_LOW (label))
6257 return 0;
6258
c782c2fe 6259 /* If CASE_LOW_SEEN is not set, that means CASE_LOW did not appear
6de9cd9a 6260 when we did our enum->case scan. Reset our scratch bit after. */
c782c2fe 6261 if (!CASE_LOW_SEEN (label))
6de9cd9a
DN
6262 match_case_to_enum_1 (CASE_LOW (label), type, label);
6263 else
c782c2fe 6264 CASE_LOW_SEEN (label) = 0;
6de9cd9a 6265
c782c2fe
RS
6266 /* If CASE_HIGH is non-null, we have a range. If CASE_HIGH_SEEN is
6267 not set, that means that CASE_HIGH did not appear when we did our
6268 enum->case scan. Reset our scratch bit after. */
6de9cd9a
DN
6269 if (CASE_HIGH (label))
6270 {
c782c2fe
RS
6271 if (!CASE_HIGH_SEEN (label))
6272 match_case_to_enum_1 (CASE_HIGH (label), type, label);
6273 else
6274 CASE_HIGH_SEEN (label) = 0;
6de9cd9a
DN
6275 }
6276
6277 return 0;
6278}
6279
fbc315db
ILT
6280/* Handle -Wswitch*. Called from the front end after parsing the
6281 switch construct. */
6282/* ??? Should probably be somewhere generic, since other languages
6283 besides C and C++ would want this. At the moment, however, C/C++
6284 are the only tree-ssa languages that support enumerations at all,
6285 so the point is moot. */
6de9cd9a 6286
fbc315db
ILT
6287void
6288c_do_switch_warnings (splay_tree cases, location_t switch_location,
b155cfd9
MP
6289 tree type, tree cond, bool bool_cond_p,
6290 bool outside_range_p)
6de9cd9a 6291{
9f63daea 6292 splay_tree_node default_node;
c782c2fe
RS
6293 splay_tree_node node;
6294 tree chain;
6de9cd9a 6295
b155cfd9
MP
6296 if (!warn_switch && !warn_switch_enum && !warn_switch_default
6297 && !warn_switch_bool)
6de9cd9a
DN
6298 return;
6299
6de9cd9a 6300 default_node = splay_tree_lookup (cases, (splay_tree_key) NULL);
44c21c7f 6301 if (!default_node)
fab922b1
MLI
6302 warning_at (switch_location, OPT_Wswitch_default,
6303 "switch missing default case");
6de9cd9a 6304
b155cfd9
MP
6305 /* There are certain cases where -Wswitch-bool warnings aren't
6306 desirable, such as
6307 switch (boolean)
6308 {
6309 case true: ...
6310 case false: ...
6311 }
6312 so be careful here. */
6313 if (warn_switch_bool && bool_cond_p)
6314 {
6315 splay_tree_node min_node;
6316 /* If there's a default node, it's also the value with the minimal
6317 key. So look at the penultimate key (if any). */
6318 if (default_node)
6319 min_node = splay_tree_successor (cases, (splay_tree_key) NULL);
6320 else
6321 min_node = splay_tree_min (cases);
6322 tree min = min_node ? (tree) min_node->key : NULL_TREE;
6323
6324 splay_tree_node max_node = splay_tree_max (cases);
6325 /* This might be a case range, so look at the value with the
6326 maximal key and then check CASE_HIGH. */
6327 tree max = max_node ? (tree) max_node->value : NULL_TREE;
6328 if (max)
6329 max = CASE_HIGH (max) ? CASE_HIGH (max) : CASE_LOW (max);
6330
6331 /* If there's a case value > 1 or < 0, that is outside bool
6332 range, warn. */
6333 if (outside_range_p
6334 || (max && wi::gts_p (max, 1))
6335 || (min && wi::lts_p (min, 0))
6336 /* And handle the
6337 switch (boolean)
6338 {
6339 case true: ...
6340 case false: ...
6341 default: ...
6342 }
6343 case, where we want to warn. */
6344 || (default_node
6345 && max && wi::eq_p (max, 1)
6346 && min && wi::eq_p (min, 0)))
6347 warning_at (switch_location, OPT_Wswitch_bool,
6348 "switch condition has boolean value");
6349 }
6350
026c3cfd 6351 /* From here on, we only care about enumerated types. */
c782c2fe
RS
6352 if (!type || TREE_CODE (type) != ENUMERAL_TYPE)
6353 return;
6354
cdb88468
ILT
6355 /* From here on, we only care about -Wswitch and -Wswitch-enum. */
6356 if (!warn_switch_enum && !warn_switch)
c782c2fe
RS
6357 return;
6358
cdb88468
ILT
6359 /* Check the cases. Warn about case values which are not members of
6360 the enumerated type. For -Wswitch-enum, or for -Wswitch when
6361 there is no default case, check that exactly all enumeration
6362 literals are covered by the cases. */
6363
c782c2fe
RS
6364 /* Clearing COND if it is not an integer constant simplifies
6365 the tests inside the loop below. */
6366 if (TREE_CODE (cond) != INTEGER_CST)
6367 cond = NULL_TREE;
6368
6369 /* The time complexity here is O(N*lg(N)) worst case, but for the
6370 common case of monotonically increasing enumerators, it is
6371 O(N), since the nature of the splay tree will keep the next
6372 element adjacent to the root at all times. */
6de9cd9a 6373
c782c2fe
RS
6374 for (chain = TYPE_VALUES (type); chain; chain = TREE_CHAIN (chain))
6375 {
6376 tree value = TREE_VALUE (chain);
adf2edec
DG
6377 if (TREE_CODE (value) == CONST_DECL)
6378 value = DECL_INITIAL (value);
c782c2fe
RS
6379 node = splay_tree_lookup (cases, (splay_tree_key) value);
6380 if (node)
6de9cd9a 6381 {
c782c2fe
RS
6382 /* Mark the CASE_LOW part of the case entry as seen. */
6383 tree label = (tree) node->value;
6384 CASE_LOW_SEEN (label) = 1;
6385 continue;
6386 }
6387
6388 /* Even though there wasn't an exact match, there might be a
fa10beec 6389 case range which includes the enumerator's value. */
c782c2fe
RS
6390 node = splay_tree_predecessor (cases, (splay_tree_key) value);
6391 if (node && CASE_HIGH ((tree) node->value))
6392 {
6393 tree label = (tree) node->value;
6394 int cmp = tree_int_cst_compare (CASE_HIGH (label), value);
6395 if (cmp >= 0)
6de9cd9a 6396 {
c782c2fe
RS
6397 /* If we match the upper bound exactly, mark the CASE_HIGH
6398 part of the case entry as seen. */
6399 if (cmp == 0)
6400 CASE_HIGH_SEEN (label) = 1;
6401 continue;
6de9cd9a
DN
6402 }
6403 }
6404
c782c2fe
RS
6405 /* We've now determined that this enumerated literal isn't
6406 handled by the case labels of the switch statement. */
6de9cd9a 6407
c782c2fe
RS
6408 /* If the switch expression is a constant, we only really care
6409 about whether that constant is handled by the switch. */
6410 if (cond && tree_int_cst_compare (cond, value))
6411 continue;
6de9cd9a 6412
683d6ff9 6413 /* If there is a default_node, the only relevant option is
cdb88468 6414 Wswitch-enum. Otherwise, if both are enabled then we prefer
683d6ff9
MLI
6415 to warn using -Wswitch because -Wswitch is enabled by -Wall
6416 while -Wswitch-enum is explicit. */
cdb88468
ILT
6417 warning_at (switch_location,
6418 (default_node || !warn_switch
6419 ? OPT_Wswitch_enum
6420 : OPT_Wswitch),
6421 "enumeration value %qE not handled in switch",
6422 TREE_PURPOSE (chain));
6de9cd9a 6423 }
c782c2fe
RS
6424
6425 /* Warn if there are case expressions that don't correspond to
6426 enumerators. This can occur since C and C++ don't enforce
6427 type-checking of assignments to enumeration variables.
6428
6429 The time complexity here is now always O(N) worst case, since
6430 we should have marked both the lower bound and upper bound of
6431 every disjoint case label, with CASE_LOW_SEEN and CASE_HIGH_SEEN
6432 above. This scan also resets those fields. */
683d6ff9 6433
c782c2fe 6434 splay_tree_foreach (cases, match_case_to_enum, type);
8f17b5c5
MM
6435}
6436
6b665219 6437/* Finish an expression taking the address of LABEL (an
6a3799eb
AH
6438 IDENTIFIER_NODE). Returns an expression for the address.
6439
6440 LOC is the location for the expression returned. */
15b732b2 6441
35b1a6fa 6442tree
6a3799eb 6443finish_label_address_expr (tree label, location_t loc)
15b732b2
NB
6444{
6445 tree result;
6446
c1771a20 6447 pedwarn (input_location, OPT_Wpedantic, "taking the address of a label is non-standard");
15b732b2 6448
6b665219
MM
6449 if (label == error_mark_node)
6450 return error_mark_node;
6451
15b732b2
NB
6452 label = lookup_label (label);
6453 if (label == NULL_TREE)
6454 result = null_pointer_node;
6455 else
6456 {
6457 TREE_USED (label) = 1;
6458 result = build1 (ADDR_EXPR, ptr_type_node, label);
5805e48d 6459 /* The current function is not necessarily uninlinable.
15b732b2
NB
6460 Computed gotos are incompatible with inlining, but the value
6461 here could be used only in a diagnostic, for example. */
6a3799eb 6462 protected_set_expr_location (result, loc);
15b732b2
NB
6463 }
6464
6465 return result;
6466}
19552aa5
JM
6467\f
6468
6469/* Given a boolean expression ARG, return a tree representing an increment
6470 or decrement (as indicated by CODE) of ARG. The front end must check for
6471 invalid cases (e.g., decrement in C++). */
6472tree
35b1a6fa 6473boolean_increment (enum tree_code code, tree arg)
19552aa5
JM
6474{
6475 tree val;
b5119fa1 6476 tree true_res = build_int_cst (TREE_TYPE (arg), 1);
37fa72e9 6477
19552aa5
JM
6478 arg = stabilize_reference (arg);
6479 switch (code)
6480 {
6481 case PREINCREMENT_EXPR:
53fb4de3 6482 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
19552aa5
JM
6483 break;
6484 case POSTINCREMENT_EXPR:
53fb4de3 6485 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
19552aa5 6486 arg = save_expr (arg);
53fb4de3
RS
6487 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6488 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
19552aa5
JM
6489 break;
6490 case PREDECREMENT_EXPR:
53fb4de3 6491 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
db3927fb 6492 invert_truthvalue_loc (input_location, arg));
19552aa5
JM
6493 break;
6494 case POSTDECREMENT_EXPR:
53fb4de3 6495 val = build2 (MODIFY_EXPR, TREE_TYPE (arg), arg,
db3927fb 6496 invert_truthvalue_loc (input_location, arg));
19552aa5 6497 arg = save_expr (arg);
53fb4de3
RS
6498 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
6499 val = build2 (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
19552aa5
JM
6500 break;
6501 default:
366de0ce 6502 gcc_unreachable ();
19552aa5
JM
6503 }
6504 TREE_SIDE_EFFECTS (val) = 1;
6505 return val;
6506}
03dc0325 6507\f
207bf79d
JM
6508/* Built-in macros for stddef.h and stdint.h, that require macros
6509 defined in this file. */
460bd0e3 6510void
35b1a6fa 6511c_stddef_cpp_builtins(void)
3df89291 6512{
5279d739
ZW
6513 builtin_define_with_value ("__SIZE_TYPE__", SIZE_TYPE, 0);
6514 builtin_define_with_value ("__PTRDIFF_TYPE__", PTRDIFF_TYPE, 0);
6515 builtin_define_with_value ("__WCHAR_TYPE__", MODIFIED_WCHAR_TYPE, 0);
6516 builtin_define_with_value ("__WINT_TYPE__", WINT_TYPE, 0);
85291069
JM
6517 builtin_define_with_value ("__INTMAX_TYPE__", INTMAX_TYPE, 0);
6518 builtin_define_with_value ("__UINTMAX_TYPE__", UINTMAX_TYPE, 0);
c466b2cd
KVH
6519 builtin_define_with_value ("__CHAR16_TYPE__", CHAR16_TYPE, 0);
6520 builtin_define_with_value ("__CHAR32_TYPE__", CHAR32_TYPE, 0);
207bf79d
JM
6521 if (SIG_ATOMIC_TYPE)
6522 builtin_define_with_value ("__SIG_ATOMIC_TYPE__", SIG_ATOMIC_TYPE, 0);
6523 if (INT8_TYPE)
6524 builtin_define_with_value ("__INT8_TYPE__", INT8_TYPE, 0);
6525 if (INT16_TYPE)
6526 builtin_define_with_value ("__INT16_TYPE__", INT16_TYPE, 0);
6527 if (INT32_TYPE)
6528 builtin_define_with_value ("__INT32_TYPE__", INT32_TYPE, 0);
6529 if (INT64_TYPE)
6530 builtin_define_with_value ("__INT64_TYPE__", INT64_TYPE, 0);
6531 if (UINT8_TYPE)
6532 builtin_define_with_value ("__UINT8_TYPE__", UINT8_TYPE, 0);
6533 if (UINT16_TYPE)
6534 builtin_define_with_value ("__UINT16_TYPE__", UINT16_TYPE, 0);
6535 if (UINT32_TYPE)
6536 builtin_define_with_value ("__UINT32_TYPE__", UINT32_TYPE, 0);
6537 if (UINT64_TYPE)
6538 builtin_define_with_value ("__UINT64_TYPE__", UINT64_TYPE, 0);
6539 if (INT_LEAST8_TYPE)
6540 builtin_define_with_value ("__INT_LEAST8_TYPE__", INT_LEAST8_TYPE, 0);
6541 if (INT_LEAST16_TYPE)
6542 builtin_define_with_value ("__INT_LEAST16_TYPE__", INT_LEAST16_TYPE, 0);
6543 if (INT_LEAST32_TYPE)
6544 builtin_define_with_value ("__INT_LEAST32_TYPE__", INT_LEAST32_TYPE, 0);
6545 if (INT_LEAST64_TYPE)
6546 builtin_define_with_value ("__INT_LEAST64_TYPE__", INT_LEAST64_TYPE, 0);
6547 if (UINT_LEAST8_TYPE)
6548 builtin_define_with_value ("__UINT_LEAST8_TYPE__", UINT_LEAST8_TYPE, 0);
6549 if (UINT_LEAST16_TYPE)
6550 builtin_define_with_value ("__UINT_LEAST16_TYPE__", UINT_LEAST16_TYPE, 0);
6551 if (UINT_LEAST32_TYPE)
6552 builtin_define_with_value ("__UINT_LEAST32_TYPE__", UINT_LEAST32_TYPE, 0);
6553 if (UINT_LEAST64_TYPE)
6554 builtin_define_with_value ("__UINT_LEAST64_TYPE__", UINT_LEAST64_TYPE, 0);
6555 if (INT_FAST8_TYPE)
6556 builtin_define_with_value ("__INT_FAST8_TYPE__", INT_FAST8_TYPE, 0);
6557 if (INT_FAST16_TYPE)
6558 builtin_define_with_value ("__INT_FAST16_TYPE__", INT_FAST16_TYPE, 0);
6559 if (INT_FAST32_TYPE)
6560 builtin_define_with_value ("__INT_FAST32_TYPE__", INT_FAST32_TYPE, 0);
6561 if (INT_FAST64_TYPE)
6562 builtin_define_with_value ("__INT_FAST64_TYPE__", INT_FAST64_TYPE, 0);
6563 if (UINT_FAST8_TYPE)
6564 builtin_define_with_value ("__UINT_FAST8_TYPE__", UINT_FAST8_TYPE, 0);
6565 if (UINT_FAST16_TYPE)
6566 builtin_define_with_value ("__UINT_FAST16_TYPE__", UINT_FAST16_TYPE, 0);
6567 if (UINT_FAST32_TYPE)
6568 builtin_define_with_value ("__UINT_FAST32_TYPE__", UINT_FAST32_TYPE, 0);
6569 if (UINT_FAST64_TYPE)
6570 builtin_define_with_value ("__UINT_FAST64_TYPE__", UINT_FAST64_TYPE, 0);
6571 if (INTPTR_TYPE)
6572 builtin_define_with_value ("__INTPTR_TYPE__", INTPTR_TYPE, 0);
6573 if (UINTPTR_TYPE)
6574 builtin_define_with_value ("__UINTPTR_TYPE__", UINTPTR_TYPE, 0);
676997cf
RH
6575}
6576
6431177a 6577static void
35b1a6fa 6578c_init_attributes (void)
6431177a
JM
6579{
6580 /* Fill in the built_in_attributes array. */
4a90aeeb 6581#define DEF_ATTR_NULL_TREE(ENUM) \
6431177a 6582 built_in_attributes[(int) ENUM] = NULL_TREE;
4a90aeeb 6583#define DEF_ATTR_INT(ENUM, VALUE) \
c62c040f 6584 built_in_attributes[(int) ENUM] = build_int_cst (integer_type_node, VALUE);
e384e6b5
BS
6585#define DEF_ATTR_STRING(ENUM, VALUE) \
6586 built_in_attributes[(int) ENUM] = build_string (strlen (VALUE), VALUE);
6431177a
JM
6587#define DEF_ATTR_IDENT(ENUM, STRING) \
6588 built_in_attributes[(int) ENUM] = get_identifier (STRING);
6589#define DEF_ATTR_TREE_LIST(ENUM, PURPOSE, VALUE, CHAIN) \
6590 built_in_attributes[(int) ENUM] \
6591 = tree_cons (built_in_attributes[(int) PURPOSE], \
6592 built_in_attributes[(int) VALUE], \
6593 built_in_attributes[(int) CHAIN]);
6431177a
JM
6594#include "builtin-attrs.def"
6595#undef DEF_ATTR_NULL_TREE
6596#undef DEF_ATTR_INT
6597#undef DEF_ATTR_IDENT
6598#undef DEF_ATTR_TREE_LIST
03dc0325 6599}
26f943fd 6600
943f82e7
JM
6601/* Returns TRUE iff the attribute indicated by ATTR_ID takes a plain
6602 identifier as an argument, so the front end shouldn't look it up. */
6603
6604bool
564a129d 6605attribute_takes_identifier_p (const_tree attr_id)
943f82e7 6606{
f231b5ff 6607 const struct attribute_spec *spec = lookup_attribute_spec (attr_id);
1b9b91a6
JM
6608 if (spec == NULL)
6609 /* Unknown attribute that we'll end up ignoring, return true so we
6610 don't complain about an identifier argument. */
6611 return true;
6612 else if (!strcmp ("mode", spec->name)
6613 || !strcmp ("format", spec->name)
6614 || !strcmp ("cleanup", spec->name))
564a129d
JM
6615 return true;
6616 else
6617 return targetm.attribute_takes_identifier_p (attr_id);
943f82e7
JM
6618}
6619
349ae713
NB
6620/* Attribute handlers common to C front ends. */
6621
6622/* Handle a "packed" attribute; arguments as in
6623 struct attribute_spec.handler. */
6624
6625static tree
e18476eb 6626handle_packed_attribute (tree *node, tree name, tree ARG_UNUSED (args),
a742c759 6627 int flags, bool *no_add_attrs)
349ae713 6628{
c6e4cc53 6629 if (TYPE_P (*node))
349ae713
NB
6630 {
6631 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 6632 *node = build_variant_type_copy (*node);
c6e4cc53 6633 TYPE_PACKED (*node) = 1;
349ae713
NB
6634 }
6635 else if (TREE_CODE (*node) == FIELD_DECL)
646c0835 6636 {
2cd36c22
AN
6637 if (TYPE_ALIGN (TREE_TYPE (*node)) <= BITS_PER_UNIT
6638 /* Still pack bitfields. */
6639 && ! DECL_INITIAL (*node))
646c0835
NS
6640 warning (OPT_Wattributes,
6641 "%qE attribute ignored for field of type %qT",
6642 name, TREE_TYPE (*node));
6643 else
6644 DECL_PACKED (*node) = 1;
6645 }
349ae713 6646 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
c6e4cc53
NS
6647 used for DECL_REGISTER. It wouldn't mean anything anyway.
6648 We can't set DECL_PACKED on the type of a TYPE_DECL, because
6649 that changes what the typedef is typing. */
349ae713
NB
6650 else
6651 {
5c498b10 6652 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6653 *no_add_attrs = true;
6654 }
6655
6656 return NULL_TREE;
6657}
6658
6659/* Handle a "nocommon" attribute; arguments as in
6660 struct attribute_spec.handler. */
6661
6662static tree
35b1a6fa 6663handle_nocommon_attribute (tree *node, tree name,
e18476eb
BI
6664 tree ARG_UNUSED (args),
6665 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 6666{
0ae9bd27 6667 if (VAR_P (*node))
349ae713
NB
6668 DECL_COMMON (*node) = 0;
6669 else
6670 {
5c498b10 6671 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6672 *no_add_attrs = true;
6673 }
6674
6675 return NULL_TREE;
6676}
6677
6678/* Handle a "common" attribute; arguments as in
6679 struct attribute_spec.handler. */
6680
6681static tree
e18476eb
BI
6682handle_common_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6683 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 6684{
0ae9bd27 6685 if (VAR_P (*node))
349ae713
NB
6686 DECL_COMMON (*node) = 1;
6687 else
6688 {
5c498b10 6689 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6690 *no_add_attrs = true;
6691 }
6692
6693 return NULL_TREE;
6694}
6695
6696/* Handle a "noreturn" attribute; arguments as in
6697 struct attribute_spec.handler. */
6698
6699static tree
e18476eb
BI
6700handle_noreturn_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6701 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
6702{
6703 tree type = TREE_TYPE (*node);
6704
6705 /* See FIXME comment in c_common_attribute_table. */
2debdb4f
NP
6706 if (TREE_CODE (*node) == FUNCTION_DECL
6707 || objc_method_decl (TREE_CODE (*node)))
349ae713
NB
6708 TREE_THIS_VOLATILE (*node) = 1;
6709 else if (TREE_CODE (type) == POINTER_TYPE
6710 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
6711 TREE_TYPE (*node)
6e7ceb17
PC
6712 = (build_qualified_type
6713 (build_pointer_type
6714 (build_type_variant (TREE_TYPE (type),
6715 TYPE_READONLY (TREE_TYPE (type)), 1)),
6716 TYPE_QUALS (type)));
349ae713
NB
6717 else
6718 {
5c498b10 6719 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6720 *no_add_attrs = true;
6721 }
6722
6723 return NULL_TREE;
6724}
6725
52bf96d2
JH
6726/* Handle a "hot" and attribute; arguments as in
6727 struct attribute_spec.handler. */
6728
6729static tree
6730handle_hot_attribute (tree *node, tree name, tree ARG_UNUSED (args),
ab442df7 6731 int ARG_UNUSED (flags), bool *no_add_attrs)
52bf96d2 6732{
e45abe1f
RH
6733 if (TREE_CODE (*node) == FUNCTION_DECL
6734 || TREE_CODE (*node) == LABEL_DECL)
52bf96d2
JH
6735 {
6736 if (lookup_attribute ("cold", DECL_ATTRIBUTES (*node)) != NULL)
6737 {
45484dcf
MP
6738 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6739 "with attribute %qs", name, "cold");
52bf96d2
JH
6740 *no_add_attrs = true;
6741 }
5779e713
MM
6742 /* Most of the rest of the hot processing is done later with
6743 lookup_attribute. */
52bf96d2
JH
6744 }
6745 else
6746 {
6747 warning (OPT_Wattributes, "%qE attribute ignored", name);
6748 *no_add_attrs = true;
6749 }
6750
6751 return NULL_TREE;
6752}
e45abe1f 6753
52bf96d2
JH
6754/* Handle a "cold" and attribute; arguments as in
6755 struct attribute_spec.handler. */
6756
6757static tree
6758handle_cold_attribute (tree *node, tree name, tree ARG_UNUSED (args),
6759 int ARG_UNUSED (flags), bool *no_add_attrs)
6760{
e45abe1f
RH
6761 if (TREE_CODE (*node) == FUNCTION_DECL
6762 || TREE_CODE (*node) == LABEL_DECL)
52bf96d2
JH
6763 {
6764 if (lookup_attribute ("hot", DECL_ATTRIBUTES (*node)) != NULL)
6765 {
45484dcf
MP
6766 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6767 "with attribute %qs", name, "hot");
52bf96d2
JH
6768 *no_add_attrs = true;
6769 }
5779e713
MM
6770 /* Most of the rest of the cold processing is done later with
6771 lookup_attribute. */
52bf96d2
JH
6772 }
6773 else
6774 {
6775 warning (OPT_Wattributes, "%qE attribute ignored", name);
6776 *no_add_attrs = true;
6777 }
6778
6779 return NULL_TREE;
6780}
6781
e664c61c 6782/* Handle a "no_sanitize_address" attribute; arguments as in
77bc5132
JJ
6783 struct attribute_spec.handler. */
6784
6785static tree
e664c61c
KS
6786handle_no_sanitize_address_attribute (tree *node, tree name, tree, int,
6787 bool *no_add_attrs)
77bc5132
JJ
6788{
6789 if (TREE_CODE (*node) != FUNCTION_DECL)
6790 {
6791 warning (OPT_Wattributes, "%qE attribute ignored", name);
6792 *no_add_attrs = true;
6793 }
6794
6795 return NULL_TREE;
6796}
6797
e664c61c
KS
6798/* Handle a "no_address_safety_analysis" attribute; arguments as in
6799 struct attribute_spec.handler. */
6800
6801static tree
6802handle_no_address_safety_analysis_attribute (tree *node, tree name, tree, int,
6803 bool *no_add_attrs)
6804{
6805 if (TREE_CODE (*node) != FUNCTION_DECL)
6806 warning (OPT_Wattributes, "%qE attribute ignored", name);
6807 else if (!lookup_attribute ("no_sanitize_address", DECL_ATTRIBUTES (*node)))
6808 DECL_ATTRIBUTES (*node)
6809 = tree_cons (get_identifier ("no_sanitize_address"),
6810 NULL_TREE, DECL_ATTRIBUTES (*node));
6811 *no_add_attrs = true;
6812 return NULL_TREE;
6813}
6814
ce6923c5
MP
6815/* Handle a "no_sanitize_undefined" attribute; arguments as in
6816 struct attribute_spec.handler. */
6817
6818static tree
6819handle_no_sanitize_undefined_attribute (tree *node, tree name, tree, int,
6820 bool *no_add_attrs)
6821{
6822 if (TREE_CODE (*node) != FUNCTION_DECL)
6823 {
6824 warning (OPT_Wattributes, "%qE attribute ignored", name);
6825 *no_add_attrs = true;
6826 }
6827
6828 return NULL_TREE;
6829}
6830
5434dc07
MD
6831/* Handle a "stack_protect" attribute; arguments as in
6832 struct attribute_spec.handler. */
6833static tree
6834handle_stack_protect_attribute (tree *node, tree name, tree, int,
6835 bool *no_add_attrs)
6836{
6837 if (TREE_CODE (*node) != FUNCTION_DECL)
6838 {
6839 warning (OPT_Wattributes, "%qE attribute ignored", name);
6840 *no_add_attrs = true;
6841 }
6842 else
6843 DECL_ATTRIBUTES (*node)
6844 = tree_cons (get_identifier ("stack_protect"),
6845 NULL_TREE, DECL_ATTRIBUTES (*node));
6846
6847 return NULL_TREE;
6848}
6849
349ae713
NB
6850/* Handle a "noinline" attribute; arguments as in
6851 struct attribute_spec.handler. */
6852
6853static tree
35b1a6fa 6854handle_noinline_attribute (tree *node, tree name,
e18476eb
BI
6855 tree ARG_UNUSED (args),
6856 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
6857{
6858 if (TREE_CODE (*node) == FUNCTION_DECL)
45484dcf
MP
6859 {
6860 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
6861 {
6862 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6863 "with attribute %qs", name, "always_inline");
6864 *no_add_attrs = true;
6865 }
6866 else
6867 DECL_UNINLINABLE (*node) = 1;
6868 }
349ae713
NB
6869 else
6870 {
5c498b10 6871 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
6872 *no_add_attrs = true;
6873 }
6874
6875 return NULL_TREE;
6876}
6877
86631ea3
MJ
6878/* Handle a "noclone" attribute; arguments as in
6879 struct attribute_spec.handler. */
6880
6881static tree
6882handle_noclone_attribute (tree *node, tree name,
6883 tree ARG_UNUSED (args),
6884 int ARG_UNUSED (flags), bool *no_add_attrs)
6885{
6886 if (TREE_CODE (*node) != FUNCTION_DECL)
6887 {
6888 warning (OPT_Wattributes, "%qE attribute ignored", name);
6889 *no_add_attrs = true;
6890 }
6891
6892 return NULL_TREE;
6893}
6894
185c9e56
ML
6895/* Handle a "no_icf" attribute; arguments as in
6896 struct attribute_spec.handler. */
6897
6898static tree
6899handle_noicf_attribute (tree *node, tree name,
6900 tree ARG_UNUSED (args),
6901 int ARG_UNUSED (flags), bool *no_add_attrs)
6902{
6903 if (TREE_CODE (*node) != FUNCTION_DECL)
6904 {
6905 warning (OPT_Wattributes, "%qE attribute ignored", name);
6906 *no_add_attrs = true;
6907 }
6908
6909 return NULL_TREE;
6910}
6911
6912
349ae713
NB
6913/* Handle a "always_inline" attribute; arguments as in
6914 struct attribute_spec.handler. */
6915
6916static tree
35b1a6fa 6917handle_always_inline_attribute (tree *node, tree name,
e18476eb
BI
6918 tree ARG_UNUSED (args),
6919 int ARG_UNUSED (flags),
a742c759 6920 bool *no_add_attrs)
349ae713
NB
6921{
6922 if (TREE_CODE (*node) == FUNCTION_DECL)
6923 {
45484dcf
MP
6924 if (lookup_attribute ("noinline", DECL_ATTRIBUTES (*node)))
6925 {
6926 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6927 "with %qs attribute", name, "noinline");
6928 *no_add_attrs = true;
6929 }
3b1661a9
ES
6930 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
6931 {
6932 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
6933 "with %qs attribute", name, "target_clones");
6934 *no_add_attrs = true;
6935 }
45484dcf
MP
6936 else
6937 /* Set the attribute and mark it for disregarding inline
6938 limits. */
6939 DECL_DISREGARD_INLINE_LIMITS (*node) = 1;
349ae713
NB
6940 }
6941 else
6942 {
5c498b10 6943 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713 6944 *no_add_attrs = true;
4eb7fd83
JJ
6945 }
6946
6947 return NULL_TREE;
6948}
6949
6950/* Handle a "gnu_inline" attribute; arguments as in
6951 struct attribute_spec.handler. */
6952
6953static tree
6954handle_gnu_inline_attribute (tree *node, tree name,
6955 tree ARG_UNUSED (args),
6956 int ARG_UNUSED (flags),
6957 bool *no_add_attrs)
6958{
6959 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
6960 {
6961 /* Do nothing else, just set the attribute. We'll get at
6962 it later with lookup_attribute. */
6963 }
6964 else
6965 {
6966 warning (OPT_Wattributes, "%qE attribute ignored", name);
6967 *no_add_attrs = true;
46a4da10
JH
6968 }
6969
6970 return NULL_TREE;
6971}
6972
6973/* Handle a "leaf" attribute; arguments as in
6974 struct attribute_spec.handler. */
6975
6976static tree
6977handle_leaf_attribute (tree *node, tree name,
6978 tree ARG_UNUSED (args),
6979 int ARG_UNUSED (flags), bool *no_add_attrs)
6980{
6981 if (TREE_CODE (*node) != FUNCTION_DECL)
6982 {
6983 warning (OPT_Wattributes, "%qE attribute ignored", name);
6984 *no_add_attrs = true;
6985 }
6986 if (!TREE_PUBLIC (*node))
6987 {
6988 warning (OPT_Wattributes, "%qE attribute has no effect on unit local functions", name);
6989 *no_add_attrs = true;
349ae713
NB
6990 }
6991
6992 return NULL_TREE;
6993}
6994
d752cfdb
JJ
6995/* Handle an "artificial" attribute; arguments as in
6996 struct attribute_spec.handler. */
6997
6998static tree
6999handle_artificial_attribute (tree *node, tree name,
7000 tree ARG_UNUSED (args),
7001 int ARG_UNUSED (flags),
7002 bool *no_add_attrs)
7003{
7004 if (TREE_CODE (*node) == FUNCTION_DECL && DECL_DECLARED_INLINE_P (*node))
7005 {
7006 /* Do nothing else, just set the attribute. We'll get at
7007 it later with lookup_attribute. */
7008 }
7009 else
7010 {
7011 warning (OPT_Wattributes, "%qE attribute ignored", name);
7012 *no_add_attrs = true;
7013 }
7014
7015 return NULL_TREE;
7016}
7017
0691d1d4
RG
7018/* Handle a "flatten" attribute; arguments as in
7019 struct attribute_spec.handler. */
7020
7021static tree
7022handle_flatten_attribute (tree *node, tree name,
c22cacf3
MS
7023 tree args ATTRIBUTE_UNUSED,
7024 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
0691d1d4
RG
7025{
7026 if (TREE_CODE (*node) == FUNCTION_DECL)
7027 /* Do nothing else, just set the attribute. We'll get at
7028 it later with lookup_attribute. */
7029 ;
7030 else
7031 {
7032 warning (OPT_Wattributes, "%qE attribute ignored", name);
7033 *no_add_attrs = true;
7034 }
7035
7036 return NULL_TREE;
7037}
7038
d2af6a68
JJ
7039/* Handle a "warning" or "error" attribute; arguments as in
7040 struct attribute_spec.handler. */
7041
7042static tree
7043handle_error_attribute (tree *node, tree name, tree args,
7044 int ARG_UNUSED (flags), bool *no_add_attrs)
7045{
7046 if (TREE_CODE (*node) == FUNCTION_DECL
cf35e2b1 7047 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
d2af6a68
JJ
7048 /* Do nothing else, just set the attribute. We'll get at
7049 it later with lookup_attribute. */
7050 ;
7051 else
7052 {
7053 warning (OPT_Wattributes, "%qE attribute ignored", name);
7054 *no_add_attrs = true;
7055 }
7056
7057 return NULL_TREE;
7058}
0691d1d4 7059
349ae713
NB
7060/* Handle a "used" attribute; arguments as in
7061 struct attribute_spec.handler. */
7062
7063static tree
e18476eb
BI
7064handle_used_attribute (tree *pnode, tree name, tree ARG_UNUSED (args),
7065 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 7066{
d7ddbe24
RH
7067 tree node = *pnode;
7068
7069 if (TREE_CODE (node) == FUNCTION_DECL
0ae9bd27 7070 || (VAR_P (node) && TREE_STATIC (node))
3797cb21 7071 || (TREE_CODE (node) == TYPE_DECL))
4d7d0451 7072 {
4d7d0451 7073 TREE_USED (node) = 1;
8e3e233b 7074 DECL_PRESERVE_P (node) = 1;
0ae9bd27 7075 if (VAR_P (node))
ebfbbdc5 7076 DECL_READ_P (node) = 1;
4d7d0451 7077 }
349ae713
NB
7078 else
7079 {
5c498b10 7080 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7081 *no_add_attrs = true;
7082 }
7083
7084 return NULL_TREE;
7085}
7086
7087/* Handle a "unused" attribute; arguments as in
7088 struct attribute_spec.handler. */
7089
d067e05f 7090tree
e18476eb
BI
7091handle_unused_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7092 int flags, bool *no_add_attrs)
349ae713
NB
7093{
7094 if (DECL_P (*node))
7095 {
7096 tree decl = *node;
7097
7098 if (TREE_CODE (decl) == PARM_DECL
af05e6e5 7099 || VAR_OR_FUNCTION_DECL_P (decl)
349ae713 7100 || TREE_CODE (decl) == LABEL_DECL
d067e05f 7101 || TREE_CODE (decl) == CONST_DECL
349ae713 7102 || TREE_CODE (decl) == TYPE_DECL)
ebfbbdc5
JJ
7103 {
7104 TREE_USED (decl) = 1;
0ae9bd27 7105 if (VAR_P (decl) || TREE_CODE (decl) == PARM_DECL)
ebfbbdc5
JJ
7106 DECL_READ_P (decl) = 1;
7107 }
349ae713
NB
7108 else
7109 {
5c498b10 7110 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7111 *no_add_attrs = true;
7112 }
7113 }
7114 else
7115 {
7116 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 7117 *node = build_variant_type_copy (*node);
349ae713
NB
7118 TREE_USED (*node) = 1;
7119 }
7120
7121 return NULL_TREE;
7122}
7123
ce91e74c
JH
7124/* Handle a "externally_visible" attribute; arguments as in
7125 struct attribute_spec.handler. */
7126
7127static tree
7128handle_externally_visible_attribute (tree *pnode, tree name,
7129 tree ARG_UNUSED (args),
7130 int ARG_UNUSED (flags),
7131 bool *no_add_attrs)
7132{
7133 tree node = *pnode;
7134
21b634ae 7135 if (VAR_OR_FUNCTION_DECL_P (node))
ce91e74c 7136 {
343d4b27
JJ
7137 if ((!TREE_STATIC (node) && TREE_CODE (node) != FUNCTION_DECL
7138 && !DECL_EXTERNAL (node)) || !TREE_PUBLIC (node))
7139 {
7140 warning (OPT_Wattributes,
7141 "%qE attribute have effect only on public objects", name);
7142 *no_add_attrs = true;
7143 }
ce91e74c 7144 }
ce91e74c
JH
7145 else
7146 {
7147 warning (OPT_Wattributes, "%qE attribute ignored", name);
7148 *no_add_attrs = true;
7149 }
7150
7151 return NULL_TREE;
7152}
7153
7861b648
AK
7154/* Handle the "no_reorder" attribute. Arguments as in
7155 struct attribute_spec.handler. */
7156
7157static tree
7158handle_no_reorder_attribute (tree *pnode,
7159 tree name,
7160 tree,
7161 int,
7162 bool *no_add_attrs)
7163{
7164 tree node = *pnode;
7165
21b634ae 7166 if (!VAR_OR_FUNCTION_DECL_P (node)
7861b648
AK
7167 && !(TREE_STATIC (node) || DECL_EXTERNAL (node)))
7168 {
7169 warning (OPT_Wattributes,
7170 "%qE attribute only affects top level objects",
7171 name);
7172 *no_add_attrs = true;
7173 }
7174
7175 return NULL_TREE;
7176}
7177
349ae713
NB
7178/* Handle a "const" attribute; arguments as in
7179 struct attribute_spec.handler. */
7180
7181static tree
e18476eb
BI
7182handle_const_attribute (tree *node, tree name, tree ARG_UNUSED (args),
7183 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7184{
7185 tree type = TREE_TYPE (*node);
7186
7187 /* See FIXME comment on noreturn in c_common_attribute_table. */
7188 if (TREE_CODE (*node) == FUNCTION_DECL)
7189 TREE_READONLY (*node) = 1;
7190 else if (TREE_CODE (type) == POINTER_TYPE
7191 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
7192 TREE_TYPE (*node)
6e7ceb17
PC
7193 = (build_qualified_type
7194 (build_pointer_type
7195 (build_type_variant (TREE_TYPE (type), 1,
7196 TREE_THIS_VOLATILE (TREE_TYPE (type)))),
7197 TYPE_QUALS (type)));
349ae713
NB
7198 else
7199 {
5c498b10 7200 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7201 *no_add_attrs = true;
7202 }
7203
7204 return NULL_TREE;
7205}
7206
ee45a32d
EB
7207/* Handle a "scalar_storage_order" attribute; arguments as in
7208 struct attribute_spec.handler. */
7209
7210static tree
7211handle_scalar_storage_order_attribute (tree *node, tree name, tree args,
7212 int flags, bool *no_add_attrs)
7213{
7214 tree id = TREE_VALUE (args);
7215 tree type;
7216
7217 if (TREE_CODE (*node) == TYPE_DECL
7218 && ! (flags & ATTR_FLAG_CXX11))
7219 node = &TREE_TYPE (*node);
7220 type = *node;
7221
7222 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
7223 {
7224 error ("scalar_storage_order is not supported because endianness "
7225 "is not uniform");
7226 return NULL_TREE;
7227 }
7228
7229 if (RECORD_OR_UNION_TYPE_P (type) && !c_dialect_cxx ())
7230 {
7231 bool reverse = false;
7232
7233 if (TREE_CODE (id) == STRING_CST
7234 && strcmp (TREE_STRING_POINTER (id), "big-endian") == 0)
7235 reverse = !BYTES_BIG_ENDIAN;
7236 else if (TREE_CODE (id) == STRING_CST
7237 && strcmp (TREE_STRING_POINTER (id), "little-endian") == 0)
7238 reverse = BYTES_BIG_ENDIAN;
7239 else
7240 {
7241 error ("scalar_storage_order argument must be one of \"big-endian\""
7242 " or \"little-endian\"");
7243 return NULL_TREE;
7244 }
7245
7246 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7247 {
7248 if (reverse)
7249 /* A type variant isn't good enough, since we don't want a cast
7250 to such a type to be removed as a no-op. */
7251 *node = type = build_duplicate_type (type);
7252 }
7253
7254 TYPE_REVERSE_STORAGE_ORDER (type) = reverse;
7255 return NULL_TREE;
7256 }
7257
7258 warning (OPT_Wattributes, "%qE attribute ignored", name);
7259 *no_add_attrs = true;
7260 return NULL_TREE;
7261}
7262
349ae713
NB
7263/* Handle a "transparent_union" attribute; arguments as in
7264 struct attribute_spec.handler. */
7265
7266static tree
35b1a6fa 7267handle_transparent_union_attribute (tree *node, tree name,
e18476eb 7268 tree ARG_UNUSED (args), int flags,
a742c759 7269 bool *no_add_attrs)
349ae713 7270{
4009f2e7 7271 tree type;
52dd234b
RH
7272
7273 *no_add_attrs = true;
349ae713 7274
e28d52cf
DS
7275 if (TREE_CODE (*node) == TYPE_DECL
7276 && ! (flags & ATTR_FLAG_CXX11))
4009f2e7
JM
7277 node = &TREE_TYPE (*node);
7278 type = *node;
349ae713 7279
52dd234b 7280 if (TREE_CODE (type) == UNION_TYPE)
349ae713 7281 {
d58d6eb5
JM
7282 /* Make sure that the first field will work for a transparent union.
7283 If the type isn't complete yet, leave the check to the code in
7284 finish_struct. */
7285 if (TYPE_SIZE (type))
7286 {
7287 tree first = first_field (type);
7288 if (first == NULL_TREE
7289 || DECL_ARTIFICIAL (first)
7290 || TYPE_MODE (type) != DECL_MODE (first))
7291 goto ignored;
7292 }
7293
349ae713 7294 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
52dd234b 7295 {
d58d6eb5
JM
7296 /* If the type isn't complete yet, setting the flag
7297 on a variant wouldn't ever be checked. */
7298 if (!TYPE_SIZE (type))
7299 goto ignored;
7300
7301 /* build_duplicate_type doesn't work for C++. */
7302 if (c_dialect_cxx ())
52dd234b
RH
7303 goto ignored;
7304
ee45a32d
EB
7305 /* A type variant isn't good enough, since we don't want a cast
7306 to such a type to be removed as a no-op. */
52dd234b
RH
7307 *node = type = build_duplicate_type (type);
7308 }
7309
75c8aac3
JH
7310 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
7311 TYPE_TRANSPARENT_AGGR (t) = 1;
52dd234b 7312 return NULL_TREE;
349ae713
NB
7313 }
7314
52dd234b
RH
7315 ignored:
7316 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7317 return NULL_TREE;
7318}
7319
fc8600f9
MM
7320/* Subroutine of handle_{con,de}structor_attribute. Evaluate ARGS to
7321 get the requested priority for a constructor or destructor,
7322 possibly issuing diagnostics for invalid or reserved
7323 priorities. */
7324
7325static priority_type
7326get_priority (tree args, bool is_destructor)
7327{
7328 HOST_WIDE_INT pri;
b2f4bed8 7329 tree arg;
fc8600f9
MM
7330
7331 if (!args)
7332 return DEFAULT_INIT_PRIORITY;
b8698a0f 7333
f6fc5c86
MM
7334 if (!SUPPORTS_INIT_PRIORITY)
7335 {
7336 if (is_destructor)
7337 error ("destructor priorities are not supported");
7338 else
7339 error ("constructor priorities are not supported");
7340 return DEFAULT_INIT_PRIORITY;
7341 }
7342
b2f4bed8 7343 arg = TREE_VALUE (args);
fad7652e
JJ
7344 if (TREE_CODE (arg) == IDENTIFIER_NODE)
7345 goto invalid;
7346 if (arg == error_mark_node)
7347 return DEFAULT_INIT_PRIORITY;
8d0d1915 7348 arg = default_conversion (arg);
9541ffee 7349 if (!tree_fits_shwi_p (arg)
b2f4bed8 7350 || !INTEGRAL_TYPE_P (TREE_TYPE (arg)))
fc8600f9
MM
7351 goto invalid;
7352
9439e9a1 7353 pri = tree_to_shwi (arg);
fc8600f9
MM
7354 if (pri < 0 || pri > MAX_INIT_PRIORITY)
7355 goto invalid;
7356
7357 if (pri <= MAX_RESERVED_INIT_PRIORITY)
7358 {
7359 if (is_destructor)
7360 warning (0,
7361 "destructor priorities from 0 to %d are reserved "
b8698a0f 7362 "for the implementation",
fc8600f9
MM
7363 MAX_RESERVED_INIT_PRIORITY);
7364 else
7365 warning (0,
7366 "constructor priorities from 0 to %d are reserved "
b8698a0f 7367 "for the implementation",
fc8600f9
MM
7368 MAX_RESERVED_INIT_PRIORITY);
7369 }
7370 return pri;
7371
7372 invalid:
7373 if (is_destructor)
7374 error ("destructor priorities must be integers from 0 to %d inclusive",
7375 MAX_INIT_PRIORITY);
7376 else
7377 error ("constructor priorities must be integers from 0 to %d inclusive",
7378 MAX_INIT_PRIORITY);
7379 return DEFAULT_INIT_PRIORITY;
7380}
7381
349ae713
NB
7382/* Handle a "constructor" attribute; arguments as in
7383 struct attribute_spec.handler. */
7384
7385static tree
fc8600f9 7386handle_constructor_attribute (tree *node, tree name, tree args,
e18476eb 7387 int ARG_UNUSED (flags),
a742c759 7388 bool *no_add_attrs)
349ae713
NB
7389{
7390 tree decl = *node;
7391 tree type = TREE_TYPE (decl);
7392
7393 if (TREE_CODE (decl) == FUNCTION_DECL
7394 && TREE_CODE (type) == FUNCTION_TYPE
7395 && decl_function_context (decl) == 0)
7396 {
fc8600f9 7397 priority_type priority;
349ae713 7398 DECL_STATIC_CONSTRUCTOR (decl) = 1;
fc8600f9
MM
7399 priority = get_priority (args, /*is_destructor=*/false);
7400 SET_DECL_INIT_PRIORITY (decl, priority);
349ae713
NB
7401 TREE_USED (decl) = 1;
7402 }
7403 else
7404 {
5c498b10 7405 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7406 *no_add_attrs = true;
7407 }
7408
7409 return NULL_TREE;
7410}
7411
7412/* Handle a "destructor" attribute; arguments as in
7413 struct attribute_spec.handler. */
7414
7415static tree
fc8600f9 7416handle_destructor_attribute (tree *node, tree name, tree args,
e18476eb 7417 int ARG_UNUSED (flags),
a742c759 7418 bool *no_add_attrs)
349ae713
NB
7419{
7420 tree decl = *node;
7421 tree type = TREE_TYPE (decl);
7422
7423 if (TREE_CODE (decl) == FUNCTION_DECL
7424 && TREE_CODE (type) == FUNCTION_TYPE
7425 && decl_function_context (decl) == 0)
7426 {
fc8600f9 7427 priority_type priority;
349ae713 7428 DECL_STATIC_DESTRUCTOR (decl) = 1;
fc8600f9
MM
7429 priority = get_priority (args, /*is_destructor=*/true);
7430 SET_DECL_FINI_PRIORITY (decl, priority);
349ae713
NB
7431 TREE_USED (decl) = 1;
7432 }
7433 else
7434 {
5c498b10 7435 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7436 *no_add_attrs = true;
7437 }
7438
7439 return NULL_TREE;
7440}
7441
4d451982
MLI
7442/* Nonzero if the mode is a valid vector mode for this architecture.
7443 This returns nonzero even if there is no hardware support for the
7444 vector mode, but we can emulate with narrower modes. */
7445
7446static int
ef4bddc2 7447vector_mode_valid_p (machine_mode mode)
4d451982
MLI
7448{
7449 enum mode_class mclass = GET_MODE_CLASS (mode);
ef4bddc2 7450 machine_mode innermode;
4d451982
MLI
7451
7452 /* Doh! What's going on? */
7453 if (mclass != MODE_VECTOR_INT
7454 && mclass != MODE_VECTOR_FLOAT
7455 && mclass != MODE_VECTOR_FRACT
7456 && mclass != MODE_VECTOR_UFRACT
7457 && mclass != MODE_VECTOR_ACCUM
7458 && mclass != MODE_VECTOR_UACCUM)
7459 return 0;
7460
7461 /* Hardware support. Woo hoo! */
7462 if (targetm.vector_mode_supported_p (mode))
7463 return 1;
7464
7465 innermode = GET_MODE_INNER (mode);
7466
7467 /* We should probably return 1 if requesting V4DI and we have no DI,
7468 but we have V2DI, but this is probably very unlikely. */
7469
7470 /* If we have support for the inner mode, we can safely emulate it.
7471 We may not have V2DI, but me can emulate with a pair of DIs. */
7472 return targetm.scalar_mode_supported_p (innermode);
7473}
7474
7475
349ae713
NB
7476/* Handle a "mode" attribute; arguments as in
7477 struct attribute_spec.handler. */
7478
7479static tree
e18476eb
BI
7480handle_mode_attribute (tree *node, tree name, tree args,
7481 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7482{
7483 tree type = *node;
88388a52 7484 tree ident = TREE_VALUE (args);
349ae713
NB
7485
7486 *no_add_attrs = true;
7487
88388a52 7488 if (TREE_CODE (ident) != IDENTIFIER_NODE)
5c498b10 7489 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7490 else
7491 {
7492 int j;
88388a52 7493 const char *p = IDENTIFIER_POINTER (ident);
349ae713 7494 int len = strlen (p);
ef4bddc2 7495 machine_mode mode = VOIDmode;
349ae713 7496 tree typefm;
6dd53648 7497 bool valid_mode;
349ae713
NB
7498
7499 if (len > 4 && p[0] == '_' && p[1] == '_'
7500 && p[len - 1] == '_' && p[len - 2] == '_')
7501 {
28dab132 7502 char *newp = (char *) alloca (len - 1);
349ae713
NB
7503
7504 strcpy (newp, &p[2]);
7505 newp[len - 4] = '\0';
7506 p = newp;
7507 }
7508
7509 /* Change this type to have a type with the specified mode.
7510 First check for the special modes. */
3f75a254 7511 if (!strcmp (p, "byte"))
349ae713
NB
7512 mode = byte_mode;
7513 else if (!strcmp (p, "word"))
7514 mode = word_mode;
3f75a254 7515 else if (!strcmp (p, "pointer"))
349ae713 7516 mode = ptr_mode;
c7ff6e7a
AK
7517 else if (!strcmp (p, "libgcc_cmp_return"))
7518 mode = targetm.libgcc_cmp_return_mode ();
7519 else if (!strcmp (p, "libgcc_shift_count"))
7520 mode = targetm.libgcc_shift_count_mode ();
7b0518e3
UW
7521 else if (!strcmp (p, "unwind_word"))
7522 mode = targetm.unwind_word_mode ();
349ae713
NB
7523 else
7524 for (j = 0; j < NUM_MACHINE_MODES; j++)
7525 if (!strcmp (p, GET_MODE_NAME (j)))
61f03aba 7526 {
ef4bddc2 7527 mode = (machine_mode) j;
61f03aba
RH
7528 break;
7529 }
349ae713
NB
7530
7531 if (mode == VOIDmode)
4a5eab38 7532 {
88388a52 7533 error ("unknown machine mode %qE", ident);
4a5eab38
PB
7534 return NULL_TREE;
7535 }
7536
6dd53648
RH
7537 valid_mode = false;
7538 switch (GET_MODE_CLASS (mode))
4a5eab38 7539 {
6dd53648
RH
7540 case MODE_INT:
7541 case MODE_PARTIAL_INT:
7542 case MODE_FLOAT:
9a8ce21f 7543 case MODE_DECIMAL_FLOAT:
ab22c1fa
CF
7544 case MODE_FRACT:
7545 case MODE_UFRACT:
7546 case MODE_ACCUM:
7547 case MODE_UACCUM:
6dd53648
RH
7548 valid_mode = targetm.scalar_mode_supported_p (mode);
7549 break;
7550
7551 case MODE_COMPLEX_INT:
7552 case MODE_COMPLEX_FLOAT:
7553 valid_mode = targetm.scalar_mode_supported_p (GET_MODE_INNER (mode));
7554 break;
7555
7556 case MODE_VECTOR_INT:
7557 case MODE_VECTOR_FLOAT:
ab22c1fa
CF
7558 case MODE_VECTOR_FRACT:
7559 case MODE_VECTOR_UFRACT:
7560 case MODE_VECTOR_ACCUM:
7561 case MODE_VECTOR_UACCUM:
5c498b10
DD
7562 warning (OPT_Wattributes, "specifying vector types with "
7563 "__attribute__ ((mode)) is deprecated");
7564 warning (OPT_Wattributes,
7565 "use __attribute__ ((vector_size)) instead");
6dd53648
RH
7566 valid_mode = vector_mode_valid_p (mode);
7567 break;
4a5eab38 7568
6dd53648
RH
7569 default:
7570 break;
7571 }
7572 if (!valid_mode)
7573 {
9e637a26 7574 error ("unable to emulate %qs", p);
6dd53648
RH
7575 return NULL_TREE;
7576 }
4a5eab38 7577
6dd53648 7578 if (POINTER_TYPE_P (type))
cb2a532e 7579 {
36c5e70a 7580 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (type));
ef4bddc2 7581 tree (*fn)(tree, machine_mode, bool);
6dd53648 7582
36c5e70a 7583 if (!targetm.addr_space.valid_pointer_mode (mode, as))
cb2a532e 7584 {
9e637a26 7585 error ("invalid pointer mode %qs", p);
cb2a532e
AH
7586 return NULL_TREE;
7587 }
7588
c22cacf3 7589 if (TREE_CODE (type) == POINTER_TYPE)
6dd53648 7590 fn = build_pointer_type_for_mode;
4977bab6 7591 else
6dd53648
RH
7592 fn = build_reference_type_for_mode;
7593 typefm = fn (TREE_TYPE (type), mode, false);
cb2a532e 7594 }
6dd53648 7595 else
ab22c1fa
CF
7596 {
7597 /* For fixed-point modes, we need to test if the signness of type
7598 and the machine mode are consistent. */
7599 if (ALL_FIXED_POINT_MODE_P (mode)
7600 && TYPE_UNSIGNED (type) != UNSIGNED_FIXED_POINT_MODE_P (mode))
7601 {
d8a07487 7602 error ("signedness of type and machine mode %qs don%'t match", p);
ab22c1fa
CF
7603 return NULL_TREE;
7604 }
7605 /* For fixed-point modes, we need to pass saturating info. */
7606 typefm = lang_hooks.types.type_for_mode (mode,
7607 ALL_FIXED_POINT_MODE_P (mode) ? TYPE_SATURATING (type)
7608 : TYPE_UNSIGNED (type));
7609 }
ec8465a5 7610
6dd53648
RH
7611 if (typefm == NULL_TREE)
7612 {
61f03aba 7613 error ("no data type for mode %qs", p);
6dd53648
RH
7614 return NULL_TREE;
7615 }
ec8465a5
RK
7616 else if (TREE_CODE (type) == ENUMERAL_TYPE)
7617 {
7618 /* For enumeral types, copy the precision from the integer
7619 type returned above. If not an INTEGER_TYPE, we can't use
7620 this mode for this type. */
7621 if (TREE_CODE (typefm) != INTEGER_TYPE)
7622 {
61f03aba 7623 error ("cannot use mode %qs for enumeral types", p);
ec8465a5
RK
7624 return NULL_TREE;
7625 }
7626
99db1ef0
RH
7627 if (flags & ATTR_FLAG_TYPE_IN_PLACE)
7628 {
7629 TYPE_PRECISION (type) = TYPE_PRECISION (typefm);
7630 typefm = type;
7631 }
7632 else
7633 {
7634 /* We cannot build a type variant, as there's code that assumes
7635 that TYPE_MAIN_VARIANT has the same mode. This includes the
7636 debug generators. Instead, create a subrange type. This
7637 results in all of the enumeral values being emitted only once
7638 in the original, and the subtype gets them by reference. */
7639 if (TYPE_UNSIGNED (type))
7640 typefm = make_unsigned_type (TYPE_PRECISION (typefm));
7641 else
7642 typefm = make_signed_type (TYPE_PRECISION (typefm));
7643 TREE_TYPE (typefm) = type;
7644 }
ec8465a5 7645 }
a2d36602
RH
7646 else if (VECTOR_MODE_P (mode)
7647 ? TREE_CODE (type) != TREE_CODE (TREE_TYPE (typefm))
7648 : TREE_CODE (type) != TREE_CODE (typefm))
61f03aba
RH
7649 {
7650 error ("mode %qs applied to inappropriate type", p);
7651 return NULL_TREE;
7652 }
7653
6dd53648 7654 *node = typefm;
349ae713
NB
7655 }
7656
7657 return NULL_TREE;
7658}
7659
7660/* Handle a "section" attribute; arguments as in
7661 struct attribute_spec.handler. */
7662
7663static tree
e18476eb
BI
7664handle_section_attribute (tree *node, tree ARG_UNUSED (name), tree args,
7665 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
7666{
7667 tree decl = *node;
7668
0373796b 7669 if (!targetm_common.have_named_sections)
349ae713 7670 {
0373796b
JT
7671 error_at (DECL_SOURCE_LOCATION (*node),
7672 "section attributes are not supported for this target");
7673 goto fail;
7674 }
9fb32434 7675
0373796b 7676 user_defined_section_attribute = true;
349ae713 7677
21b634ae 7678 if (!VAR_OR_FUNCTION_DECL_P (decl))
0373796b
JT
7679 {
7680 error ("section attribute not allowed for %q+D", *node);
7681 goto fail;
349ae713 7682 }
0373796b
JT
7683
7684 if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
349ae713 7685 {
0373796b
JT
7686 error ("section attribute argument not a string constant");
7687 goto fail;
349ae713
NB
7688 }
7689
0ae9bd27 7690 if (VAR_P (decl)
0373796b
JT
7691 && current_function_decl != NULL_TREE
7692 && !TREE_STATIC (decl))
7693 {
7694 error_at (DECL_SOURCE_LOCATION (decl),
7695 "section attribute cannot be specified for local variables");
7696 goto fail;
7697 }
7698
7699 /* The decl may have already been given a section attribute
7700 from a previous declaration. Ensure they match. */
7701 if (DECL_SECTION_NAME (decl) != NULL
7702 && strcmp (DECL_SECTION_NAME (decl),
7703 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
7704 {
7705 error ("section of %q+D conflicts with previous declaration", *node);
7706 goto fail;
7707 }
7708
0ae9bd27 7709 if (VAR_P (decl)
0373796b
JT
7710 && !targetm.have_tls && targetm.emutls.tmpl_section
7711 && DECL_THREAD_LOCAL_P (decl))
7712 {
7713 error ("section of %q+D cannot be overridden", *node);
7714 goto fail;
7715 }
7716
7717 set_decl_section_name (decl, TREE_STRING_POINTER (TREE_VALUE (args)));
7718 return NULL_TREE;
7719
7720fail:
7721 *no_add_attrs = true;
349ae713
NB
7722 return NULL_TREE;
7723}
7724
d19fa6b5
JM
7725/* Check whether ALIGN is a valid user-specified alignment. If so,
7726 return its base-2 log; if not, output an error and return -1. If
7727 ALLOW_ZERO then 0 is valid and should result in a return of -1 with
7728 no error. */
7729int
7730check_user_alignment (const_tree align, bool allow_zero)
7731{
7732 int i;
7733
661a0813
MP
7734 if (error_operand_p (align))
7735 return -1;
a859517f
MP
7736 if (TREE_CODE (align) != INTEGER_CST
7737 || !INTEGRAL_TYPE_P (TREE_TYPE (align)))
d19fa6b5
JM
7738 {
7739 error ("requested alignment is not an integer constant");
7740 return -1;
7741 }
7742 else if (allow_zero && integer_zerop (align))
7743 return -1;
3f12f6e9
SKS
7744 else if (tree_int_cst_sgn (align) == -1
7745 || (i = tree_log2 (align)) == -1)
d19fa6b5 7746 {
3f12f6e9 7747 error ("requested alignment is not a positive power of 2");
d19fa6b5
JM
7748 return -1;
7749 }
ec1e2a40 7750 else if (i >= HOST_BITS_PER_INT - LOG2_BITS_PER_UNIT)
d19fa6b5
JM
7751 {
7752 error ("requested alignment is too large");
7753 return -1;
7754 }
7755 return i;
7756}
7757
e28d52cf
DS
7758/*
7759 If in c++-11, check if the c++-11 alignment constraint with respect
7760 to fundamental alignment (in [dcl.align]) are satisfied. If not in
7761 c++-11 mode, does nothing.
7762
7763 [dcl.align]2/ says:
7764
7765 [* if the constant expression evaluates to a fundamental alignment,
7766 the alignment requirement of the declared entity shall be the
7767 specified fundamental alignment.
7768
7769 * if the constant expression evaluates to an extended alignment
7770 and the implementation supports that alignment in the context
7771 of the declaration, the alignment of the declared entity shall
7772 be that alignment
7773
7774 * if the constant expression evaluates to an extended alignment
7775 and the implementation does not support that alignment in the
7776 context of the declaration, the program is ill-formed]. */
7777
7778static bool
7779check_cxx_fundamental_alignment_constraints (tree node,
7780 unsigned align_log,
7781 int flags)
7782{
7783 bool alignment_too_large_p = false;
7784 unsigned requested_alignment = 1U << align_log;
7785 unsigned max_align = 0;
7786
7787 if ((!(flags & ATTR_FLAG_CXX11) && !warn_cxx_compat)
7788 || (node == NULL_TREE || node == error_mark_node))
7789 return true;
7790
7791 if (cxx_fundamental_alignment_p (requested_alignment))
7792 return true;
7793
7794 if (DECL_P (node))
7795 {
7796 if (TREE_STATIC (node))
7797 {
7798 /* For file scope variables and static members, the target
7799 supports alignments that are at most
7800 MAX_OFILE_ALIGNMENT. */
7801 if (requested_alignment > (max_align = MAX_OFILE_ALIGNMENT))
7802 alignment_too_large_p = true;
7803 }
7804 else
7805 {
7806#ifdef BIGGEST_FIELD_ALIGNMENT
7807#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_FIELD_ALIGNMENT
7808#else
7809#define MAX_TARGET_FIELD_ALIGNMENT BIGGEST_ALIGNMENT
7810#endif
7811 /* For non-static members, the target supports either
7812 alignments that at most either BIGGEST_FIELD_ALIGNMENT
7813 if it is defined or BIGGEST_ALIGNMENT. */
7814 max_align = MAX_TARGET_FIELD_ALIGNMENT;
7815 if (TREE_CODE (node) == FIELD_DECL
7816 && requested_alignment > (max_align = MAX_TARGET_FIELD_ALIGNMENT))
7817 alignment_too_large_p = true;
7818#undef MAX_TARGET_FIELD_ALIGNMENT
7819 /* For stack variables, the target supports at most
7820 MAX_STACK_ALIGNMENT. */
7821 else if (decl_function_context (node) != NULL
7822 && requested_alignment > (max_align = MAX_STACK_ALIGNMENT))
7823 alignment_too_large_p = true;
7824 }
7825 }
7826 else if (TYPE_P (node))
7827 {
7828 /* Let's be liberal for types. */
7829 if (requested_alignment > (max_align = BIGGEST_ALIGNMENT))
7830 alignment_too_large_p = true;
7831 }
7832
7833 if (alignment_too_large_p)
7834 pedwarn (input_location, OPT_Wattributes,
7835 "requested alignment %d is larger than %d",
7836 requested_alignment, max_align);
7837
7838 return !alignment_too_large_p;
7839}
7840
349ae713
NB
7841/* Handle a "aligned" attribute; arguments as in
7842 struct attribute_spec.handler. */
7843
7844static tree
e18476eb 7845handle_aligned_attribute (tree *node, tree ARG_UNUSED (name), tree args,
a742c759 7846 int flags, bool *no_add_attrs)
349ae713
NB
7847{
7848 tree decl = NULL_TREE;
7849 tree *type = NULL;
7850 int is_type = 0;
5d77fb19 7851 tree align_expr;
349ae713
NB
7852 int i;
7853
5d77fb19
MG
7854 if (args)
7855 {
7856 align_expr = TREE_VALUE (args);
661a0813
MP
7857 if (align_expr && TREE_CODE (align_expr) != IDENTIFIER_NODE
7858 && TREE_CODE (align_expr) != FUNCTION_DECL)
5d77fb19
MG
7859 align_expr = default_conversion (align_expr);
7860 }
7861 else
7862 align_expr = size_int (ATTRIBUTE_ALIGNED_VALUE / BITS_PER_UNIT);
7863
349ae713
NB
7864 if (DECL_P (*node))
7865 {
7866 decl = *node;
7867 type = &TREE_TYPE (decl);
7868 is_type = TREE_CODE (*node) == TYPE_DECL;
7869 }
7870 else if (TYPE_P (*node))
7871 type = node, is_type = 1;
7872
509063eb 7873 if ((i = check_user_alignment (align_expr, true)) == -1
e28d52cf 7874 || !check_cxx_fundamental_alignment_constraints (*node, i, flags))
d19fa6b5 7875 *no_add_attrs = true;
349ae713
NB
7876 else if (is_type)
7877 {
0f559c16
JM
7878 if ((flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
7879 /* OK, modify the type in place. */;
349ae713
NB
7880 /* If we have a TYPE_DECL, then copy the type, so that we
7881 don't accidentally modify a builtin type. See pushdecl. */
0f559c16
JM
7882 else if (decl && TREE_TYPE (decl) != error_mark_node
7883 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
349ae713
NB
7884 {
7885 tree tt = TREE_TYPE (decl);
8dd16ecc 7886 *type = build_variant_type_copy (*type);
349ae713
NB
7887 DECL_ORIGINAL_TYPE (decl) = tt;
7888 TYPE_NAME (*type) = decl;
7889 TREE_USED (*type) = TREE_USED (decl);
7890 TREE_TYPE (decl) = *type;
7891 }
0f559c16 7892 else
8dd16ecc 7893 *type = build_variant_type_copy (*type);
349ae713 7894
fe37c7af 7895 SET_TYPE_ALIGN (*type, (1U << i) * BITS_PER_UNIT);
349ae713
NB
7896 TYPE_USER_ALIGN (*type) = 1;
7897 }
837edd5f 7898 else if (! VAR_OR_FUNCTION_DECL_P (decl)
349ae713
NB
7899 && TREE_CODE (decl) != FIELD_DECL)
7900 {
dee15844 7901 error ("alignment may not be specified for %q+D", decl);
349ae713
NB
7902 *no_add_attrs = true;
7903 }
e28d52cf
DS
7904 else if (DECL_USER_ALIGN (decl)
7905 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
7906 /* C++-11 [dcl.align/4]:
7907
7908 When multiple alignment-specifiers are specified for an
7909 entity, the alignment requirement shall be set to the
7910 strictest specified alignment.
7911
7912 This formally comes from the c++11 specification but we are
7913 doing it for the GNU attribute syntax as well. */
7914 *no_add_attrs = true;
837edd5f 7915 else if (TREE_CODE (decl) == FUNCTION_DECL
d9223014 7916 && DECL_ALIGN (decl) > (1U << i) * BITS_PER_UNIT)
837edd5f
GK
7917 {
7918 if (DECL_USER_ALIGN (decl))
7919 error ("alignment for %q+D was previously specified as %d "
7920 "and may not be decreased", decl,
7921 DECL_ALIGN (decl) / BITS_PER_UNIT);
7922 else
7923 error ("alignment for %q+D must be at least %d", decl,
7924 DECL_ALIGN (decl) / BITS_PER_UNIT);
7925 *no_add_attrs = true;
7926 }
349ae713
NB
7927 else
7928 {
fe37c7af 7929 SET_DECL_ALIGN (decl, (1U << i) * BITS_PER_UNIT);
349ae713
NB
7930 DECL_USER_ALIGN (decl) = 1;
7931 }
7932
7933 return NULL_TREE;
7934}
7935
7936/* Handle a "weak" attribute; arguments as in
7937 struct attribute_spec.handler. */
7938
7939static tree
55af93a8 7940handle_weak_attribute (tree *node, tree name,
e18476eb
BI
7941 tree ARG_UNUSED (args),
7942 int ARG_UNUSED (flags),
7943 bool * ARG_UNUSED (no_add_attrs))
349ae713 7944{
55af93a8 7945 if (TREE_CODE (*node) == FUNCTION_DECL
6b4e94bc
RG
7946 && DECL_DECLARED_INLINE_P (*node))
7947 {
2aa9c6ae 7948 warning (OPT_Wattributes, "inline function %q+D declared weak", *node);
6b4e94bc
RG
7949 *no_add_attrs = true;
7950 }
ba885ec5
NS
7951 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
7952 {
7953 error ("indirect function %q+D cannot be declared weak", *node);
7954 *no_add_attrs = true;
7955 return NULL_TREE;
7956 }
21b634ae 7957 else if (VAR_OR_FUNCTION_DECL_P (*node))
c316b5e4 7958 declare_weak (*node);
55af93a8
DS
7959 else
7960 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
7961
7962 return NULL_TREE;
7963}
7964
4bb794e2
ST
7965/* Handle a "noplt" attribute; arguments as in
7966 struct attribute_spec.handler. */
7967
7968static tree
7969handle_noplt_attribute (tree *node, tree name,
7970 tree ARG_UNUSED (args),
7971 int ARG_UNUSED (flags),
7972 bool * ARG_UNUSED (no_add_attrs))
7973{
7974 if (TREE_CODE (*node) != FUNCTION_DECL)
7975 {
7976 warning (OPT_Wattributes,
7977 "%qE attribute is only applicable on functions", name);
7978 *no_add_attrs = true;
7979 return NULL_TREE;
7980 }
7981 return NULL_TREE;
7982}
7983
ba885ec5
NS
7984/* Handle an "alias" or "ifunc" attribute; arguments as in
7985 struct attribute_spec.handler, except that IS_ALIAS tells us
7986 whether this is an alias as opposed to ifunc attribute. */
349ae713
NB
7987
7988static tree
ba885ec5
NS
7989handle_alias_ifunc_attribute (bool is_alias, tree *node, tree name, tree args,
7990 bool *no_add_attrs)
349ae713
NB
7991{
7992 tree decl = *node;
7993
ba885ec5 7994 if (TREE_CODE (decl) != FUNCTION_DECL
0ae9bd27 7995 && (!is_alias || !VAR_P (decl)))
feab5a67
JM
7996 {
7997 warning (OPT_Wattributes, "%qE attribute ignored", name);
7998 *no_add_attrs = true;
7999 }
8000 else if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
b8698a0f 8001 || (TREE_CODE (decl) != FUNCTION_DECL
a9b0b825
GK
8002 && TREE_PUBLIC (decl) && !DECL_EXTERNAL (decl))
8003 /* A static variable declaration is always a tentative definition,
8004 but the alias is a non-tentative definition which overrides. */
b8698a0f 8005 || (TREE_CODE (decl) != FUNCTION_DECL
a9b0b825 8006 && ! TREE_PUBLIC (decl) && DECL_INITIAL (decl)))
349ae713 8007 {
ba885ec5 8008 error ("%q+D defined both normally and as %qE attribute", decl, name);
349ae713 8009 *no_add_attrs = true;
ba885ec5 8010 return NULL_TREE;
349ae713 8011 }
ba885ec5
NS
8012 else if (!is_alias
8013 && (lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
8014 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))))
8015 {
8016 error ("weak %q+D cannot be defined %qE", decl, name);
8017 *no_add_attrs = true;
8018 return NULL_TREE;
8019 }
f6a76b9f
RH
8020
8021 /* Note that the very first time we process a nested declaration,
8022 decl_function_context will not be set. Indeed, *would* never
8023 be set except for the DECL_INITIAL/DECL_EXTERNAL frobbery that
8024 we do below. After such frobbery, pushdecl would set the context.
8025 In any case, this is never what we want. */
8026 else if (decl_function_context (decl) == 0 && current_function_decl == NULL)
349ae713
NB
8027 {
8028 tree id;
8029
8030 id = TREE_VALUE (args);
8031 if (TREE_CODE (id) != STRING_CST)
8032 {
ba885ec5 8033 error ("attribute %qE argument not a string", name);
349ae713
NB
8034 *no_add_attrs = true;
8035 return NULL_TREE;
8036 }
8037 id = get_identifier (TREE_STRING_POINTER (id));
8038 /* This counts as a use of the object pointed to. */
8039 TREE_USED (id) = 1;
8040
8041 if (TREE_CODE (decl) == FUNCTION_DECL)
8042 DECL_INITIAL (decl) = error_mark_node;
8043 else
08346abd 8044 TREE_STATIC (decl) = 1;
ba885ec5
NS
8045
8046 if (!is_alias)
8047 /* ifuncs are also aliases, so set that attribute too. */
8048 DECL_ATTRIBUTES (decl)
8049 = tree_cons (get_identifier ("alias"), args, DECL_ATTRIBUTES (decl));
349ae713
NB
8050 }
8051 else
8052 {
5c498b10 8053 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8054 *no_add_attrs = true;
8055 }
8056
f7217cde
JH
8057 if (decl_in_symtab_p (*node))
8058 {
8059 struct symtab_node *n = symtab_node::get (decl);
8060 if (n && n->refuse_visibility_changes)
8061 {
8062 if (is_alias)
8063 error ("%+D declared alias after being used", decl);
8064 else
8065 error ("%+D declared ifunc after being used", decl);
8066 }
8067 }
8068
8069
349ae713
NB
8070 return NULL_TREE;
8071}
8072
ba885ec5
NS
8073/* Handle an "alias" or "ifunc" attribute; arguments as in
8074 struct attribute_spec.handler. */
8075
8076static tree
8077handle_ifunc_attribute (tree *node, tree name, tree args,
8078 int ARG_UNUSED (flags), bool *no_add_attrs)
8079{
8080 return handle_alias_ifunc_attribute (false, node, name, args, no_add_attrs);
8081}
8082
8083/* Handle an "alias" or "ifunc" attribute; arguments as in
8084 struct attribute_spec.handler. */
8085
8086static tree
8087handle_alias_attribute (tree *node, tree name, tree args,
8088 int ARG_UNUSED (flags), bool *no_add_attrs)
8089{
8090 return handle_alias_ifunc_attribute (true, node, name, args, no_add_attrs);
8091}
8092
a0203ca7
AO
8093/* Handle a "weakref" attribute; arguments as in struct
8094 attribute_spec.handler. */
8095
8096static tree
8097handle_weakref_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8098 int flags, bool *no_add_attrs)
8099{
8100 tree attr = NULL_TREE;
8101
e1cf56b1
AO
8102 /* We must ignore the attribute when it is associated with
8103 local-scoped decls, since attribute alias is ignored and many
8104 such symbols do not even have a DECL_WEAK field. */
e7b012c0
JJ
8105 if (decl_function_context (*node)
8106 || current_function_decl
21b634ae 8107 || !VAR_OR_FUNCTION_DECL_P (*node))
e1cf56b1
AO
8108 {
8109 warning (OPT_Wattributes, "%qE attribute ignored", name);
8110 *no_add_attrs = true;
8111 return NULL_TREE;
8112 }
8113
ba885ec5
NS
8114 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (*node)))
8115 {
8116 error ("indirect function %q+D cannot be declared weakref", *node);
8117 *no_add_attrs = true;
8118 return NULL_TREE;
8119 }
8120
a0203ca7
AO
8121 /* The idea here is that `weakref("name")' mutates into `weakref,
8122 alias("name")', and weakref without arguments, in turn,
8123 implicitly adds weak. */
8124
8125 if (args)
8126 {
8127 attr = tree_cons (get_identifier ("alias"), args, attr);
8128 attr = tree_cons (get_identifier ("weakref"), NULL_TREE, attr);
8129
8130 *no_add_attrs = true;
a9b0b825
GK
8131
8132 decl_attributes (node, attr, flags);
a0203ca7
AO
8133 }
8134 else
8135 {
8136 if (lookup_attribute ("alias", DECL_ATTRIBUTES (*node)))
c5d75364
MLI
8137 error_at (DECL_SOURCE_LOCATION (*node),
8138 "weakref attribute must appear before alias attribute");
a0203ca7 8139
a9b0b825
GK
8140 /* Can't call declare_weak because it wants this to be TREE_PUBLIC,
8141 and that isn't supported; and because it wants to add it to
8142 the list of weak decls, which isn't helpful. */
8143 DECL_WEAK (*node) = 1;
a0203ca7
AO
8144 }
8145
f7217cde
JH
8146 if (decl_in_symtab_p (*node))
8147 {
8148 struct symtab_node *n = symtab_node::get (*node);
8149 if (n && n->refuse_visibility_changes)
8150 error ("%+D declared weakref after being used", *node);
8151 }
8152
a0203ca7
AO
8153 return NULL_TREE;
8154}
8155
349ae713
NB
8156/* Handle an "visibility" attribute; arguments as in
8157 struct attribute_spec.handler. */
8158
8159static tree
35b1a6fa 8160handle_visibility_attribute (tree *node, tree name, tree args,
e18476eb 8161 int ARG_UNUSED (flags),
b9e75696 8162 bool *ARG_UNUSED (no_add_attrs))
349ae713
NB
8163{
8164 tree decl = *node;
968b41a1 8165 tree id = TREE_VALUE (args);
b9e75696 8166 enum symbol_visibility vis;
349ae713 8167
d7afec4b
ND
8168 if (TYPE_P (*node))
8169 {
b9e75696
JM
8170 if (TREE_CODE (*node) == ENUMERAL_TYPE)
8171 /* OK */;
a868811e 8172 else if (!RECORD_OR_UNION_TYPE_P (*node))
b9e75696
JM
8173 {
8174 warning (OPT_Wattributes, "%qE attribute ignored on non-class types",
8175 name);
8176 return NULL_TREE;
8177 }
8178 else if (TYPE_FIELDS (*node))
8179 {
8180 error ("%qE attribute ignored because %qT is already defined",
8181 name, *node);
8182 return NULL_TREE;
8183 }
d7afec4b 8184 }
3f75a254 8185 else if (decl_function_context (decl) != 0 || !TREE_PUBLIC (decl))
349ae713 8186 {
5c498b10 8187 warning (OPT_Wattributes, "%qE attribute ignored", name);
968b41a1 8188 return NULL_TREE;
349ae713 8189 }
349ae713 8190
968b41a1
MA
8191 if (TREE_CODE (id) != STRING_CST)
8192 {
40b97a2e 8193 error ("visibility argument not a string");
968b41a1 8194 return NULL_TREE;
349ae713 8195 }
9f63daea 8196
d7afec4b
ND
8197 /* If this is a type, set the visibility on the type decl. */
8198 if (TYPE_P (decl))
8199 {
8200 decl = TYPE_NAME (decl);
3f75a254 8201 if (!decl)
c22cacf3 8202 return NULL_TREE;
e8233ac2
AP
8203 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8204 {
5c498b10 8205 warning (OPT_Wattributes, "%qE attribute ignored on types",
e8233ac2
AP
8206 name);
8207 return NULL_TREE;
8208 }
d7afec4b 8209 }
349ae713 8210
968b41a1 8211 if (strcmp (TREE_STRING_POINTER (id), "default") == 0)
b9e75696 8212 vis = VISIBILITY_DEFAULT;
968b41a1 8213 else if (strcmp (TREE_STRING_POINTER (id), "internal") == 0)
b9e75696 8214 vis = VISIBILITY_INTERNAL;
968b41a1 8215 else if (strcmp (TREE_STRING_POINTER (id), "hidden") == 0)
b9e75696 8216 vis = VISIBILITY_HIDDEN;
968b41a1 8217 else if (strcmp (TREE_STRING_POINTER (id), "protected") == 0)
b9e75696 8218 vis = VISIBILITY_PROTECTED;
968b41a1 8219 else
b9e75696
JM
8220 {
8221 error ("visibility argument must be one of \"default\", \"hidden\", \"protected\" or \"internal\"");
8222 vis = VISIBILITY_DEFAULT;
8223 }
8224
8225 if (DECL_VISIBILITY_SPECIFIED (decl)
3a687f8b
MM
8226 && vis != DECL_VISIBILITY (decl))
8227 {
8228 tree attributes = (TYPE_P (*node)
8229 ? TYPE_ATTRIBUTES (*node)
8230 : DECL_ATTRIBUTES (decl));
8231 if (lookup_attribute ("visibility", attributes))
8232 error ("%qD redeclared with different visibility", decl);
8233 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8234 && lookup_attribute ("dllimport", attributes))
8235 error ("%qD was declared %qs which implies default visibility",
8236 decl, "dllimport");
8237 else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8238 && lookup_attribute ("dllexport", attributes))
8239 error ("%qD was declared %qs which implies default visibility",
8240 decl, "dllexport");
8241 }
b9e75696
JM
8242
8243 DECL_VISIBILITY (decl) = vis;
d7afec4b
ND
8244 DECL_VISIBILITY_SPECIFIED (decl) = 1;
8245
b9e75696
JM
8246 /* Go ahead and attach the attribute to the node as well. This is needed
8247 so we can determine whether we have VISIBILITY_DEFAULT because the
8248 visibility was not specified, or because it was explicitly overridden
8249 from the containing scope. */
968b41a1 8250
349ae713
NB
8251 return NULL_TREE;
8252}
8253
b2ca3702
MM
8254/* Determine the ELF symbol visibility for DECL, which is either a
8255 variable or a function. It is an error to use this function if a
8256 definition of DECL is not available in this translation unit.
8257 Returns true if the final visibility has been determined by this
8258 function; false if the caller is free to make additional
8259 modifications. */
8260
8261bool
8262c_determine_visibility (tree decl)
8263{
21b634ae 8264 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
b2ca3702
MM
8265
8266 /* If the user explicitly specified the visibility with an
8267 attribute, honor that. DECL_VISIBILITY will have been set during
6d87092d
JM
8268 the processing of the attribute. We check for an explicit
8269 attribute, rather than just checking DECL_VISIBILITY_SPECIFIED,
8270 to distinguish the use of an attribute from the use of a "#pragma
8271 GCC visibility push(...)"; in the latter case we still want other
8272 considerations to be able to overrule the #pragma. */
8273 if (lookup_attribute ("visibility", DECL_ATTRIBUTES (decl))
8274 || (TARGET_DLLIMPORT_DECL_ATTRIBUTES
8275 && (lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
8276 || lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)))))
b2ca3702
MM
8277 return true;
8278
b9e75696
JM
8279 /* Set default visibility to whatever the user supplied with
8280 visibility_specified depending on #pragma GCC visibility. */
8281 if (!DECL_VISIBILITY_SPECIFIED (decl))
8282 {
09812622
JJ
8283 if (visibility_options.inpragma
8284 || DECL_VISIBILITY (decl) != default_visibility)
8285 {
8286 DECL_VISIBILITY (decl) = default_visibility;
8287 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma;
8288 /* If visibility changed and DECL already has DECL_RTL, ensure
8289 symbol flags are updated. */
0ae9bd27 8290 if (((VAR_P (decl) && TREE_STATIC (decl))
09812622
JJ
8291 || TREE_CODE (decl) == FUNCTION_DECL)
8292 && DECL_RTL_SET_P (decl))
8293 make_decl_rtl (decl);
8294 }
b9e75696 8295 }
b2ca3702
MM
8296 return false;
8297}
8298
dce81a1a
JJ
8299/* Handle an "tls_model" attribute; arguments as in
8300 struct attribute_spec.handler. */
8301
8302static tree
35b1a6fa 8303handle_tls_model_attribute (tree *node, tree name, tree args,
e18476eb 8304 int ARG_UNUSED (flags), bool *no_add_attrs)
dce81a1a 8305{
c2f7fa15 8306 tree id;
dce81a1a 8307 tree decl = *node;
c2f7fa15 8308 enum tls_model kind;
dce81a1a 8309
c2f7fa15
SB
8310 *no_add_attrs = true;
8311
0ae9bd27 8312 if (!VAR_P (decl) || !DECL_THREAD_LOCAL_P (decl))
dce81a1a 8313 {
5c498b10 8314 warning (OPT_Wattributes, "%qE attribute ignored", name);
c2f7fa15 8315 return NULL_TREE;
dce81a1a 8316 }
dce81a1a 8317
c2f7fa15
SB
8318 kind = DECL_TLS_MODEL (decl);
8319 id = TREE_VALUE (args);
8320 if (TREE_CODE (id) != STRING_CST)
8321 {
8322 error ("tls_model argument not a string");
8323 return NULL_TREE;
dce81a1a
JJ
8324 }
8325
c2f7fa15
SB
8326 if (!strcmp (TREE_STRING_POINTER (id), "local-exec"))
8327 kind = TLS_MODEL_LOCAL_EXEC;
8328 else if (!strcmp (TREE_STRING_POINTER (id), "initial-exec"))
8329 kind = TLS_MODEL_INITIAL_EXEC;
8330 else if (!strcmp (TREE_STRING_POINTER (id), "local-dynamic"))
8331 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
8332 else if (!strcmp (TREE_STRING_POINTER (id), "global-dynamic"))
8333 kind = TLS_MODEL_GLOBAL_DYNAMIC;
8334 else
8335 error ("tls_model argument must be one of \"local-exec\", \"initial-exec\", \"local-dynamic\" or \"global-dynamic\"");
8336
56363ffd 8337 set_decl_tls_model (decl, kind);
dce81a1a
JJ
8338 return NULL_TREE;
8339}
8340
349ae713
NB
8341/* Handle a "no_instrument_function" attribute; arguments as in
8342 struct attribute_spec.handler. */
8343
8344static tree
35b1a6fa 8345handle_no_instrument_function_attribute (tree *node, tree name,
e18476eb
BI
8346 tree ARG_UNUSED (args),
8347 int ARG_UNUSED (flags),
a742c759 8348 bool *no_add_attrs)
349ae713
NB
8349{
8350 tree decl = *node;
8351
8352 if (TREE_CODE (decl) != FUNCTION_DECL)
8353 {
c5d75364
MLI
8354 error_at (DECL_SOURCE_LOCATION (decl),
8355 "%qE attribute applies only to functions", name);
349ae713
NB
8356 *no_add_attrs = true;
8357 }
349ae713
NB
8358 else
8359 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
8360
8361 return NULL_TREE;
8362}
8363
1225d6b1
ML
8364/* Handle a "no_profile_instrument_function" attribute; arguments as in
8365 struct attribute_spec.handler. */
8366
8367static tree
8368handle_no_profile_instrument_function_attribute (tree *node, tree name, tree,
8369 int, bool *no_add_attrs)
8370{
8371 if (TREE_CODE (*node) != FUNCTION_DECL)
8372 {
8373 warning (OPT_Wattributes, "%qE attribute ignored", name);
8374 *no_add_attrs = true;
8375 }
8376
8377 return NULL_TREE;
8378}
8379
349ae713
NB
8380/* Handle a "malloc" attribute; arguments as in
8381 struct attribute_spec.handler. */
8382
8383static tree
e18476eb
BI
8384handle_malloc_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8385 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713 8386{
3425638a
JM
8387 if (TREE_CODE (*node) == FUNCTION_DECL
8388 && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (*node))))
349ae713 8389 DECL_IS_MALLOC (*node) = 1;
349ae713
NB
8390 else
8391 {
5c498b10 8392 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8393 *no_add_attrs = true;
8394 }
8395
8396 return NULL_TREE;
8397}
8398
51bc54a6
DM
8399/* Handle a "alloc_size" attribute; arguments as in
8400 struct attribute_spec.handler. */
8401
8402static tree
8403handle_alloc_size_attribute (tree *node, tree ARG_UNUSED (name), tree args,
8404 int ARG_UNUSED (flags), bool *no_add_attrs)
8405{
f3f75f69 8406 unsigned arg_count = type_num_arguments (*node);
51bc54a6
DM
8407 for (; args; args = TREE_CHAIN (args))
8408 {
8409 tree position = TREE_VALUE (args);
5d77fb19
MG
8410 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8411 && TREE_CODE (position) != FUNCTION_DECL)
8412 position = default_conversion (position);
51bc54a6 8413
8fcbce72
JJ
8414 if (!tree_fits_uhwi_p (position)
8415 || !arg_count
8416 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
51bc54a6 8417 {
b8698a0f 8418 warning (OPT_Wattributes,
51bc54a6
DM
8419 "alloc_size parameter outside range");
8420 *no_add_attrs = true;
8421 return NULL_TREE;
8422 }
8423 }
8424 return NULL_TREE;
8425}
8426
8fcbce72
JJ
8427/* Handle a "alloc_align" attribute; arguments as in
8428 struct attribute_spec.handler. */
8429
8430static tree
8431handle_alloc_align_attribute (tree *node, tree, tree args, int,
8432 bool *no_add_attrs)
8433{
8434 unsigned arg_count = type_num_arguments (*node);
8435 tree position = TREE_VALUE (args);
78169471
MP
8436 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8437 && TREE_CODE (position) != FUNCTION_DECL)
8fcbce72
JJ
8438 position = default_conversion (position);
8439
8440 if (!tree_fits_uhwi_p (position)
8441 || !arg_count
8442 || !IN_RANGE (tree_to_uhwi (position), 1, arg_count))
8443 {
8444 warning (OPT_Wattributes,
8445 "alloc_align parameter outside range");
8446 *no_add_attrs = true;
8447 return NULL_TREE;
8448 }
8449 return NULL_TREE;
8450}
8451
8452/* Handle a "assume_aligned" attribute; arguments as in
8453 struct attribute_spec.handler. */
8454
8455static tree
8456handle_assume_aligned_attribute (tree *, tree, tree args, int,
8457 bool *no_add_attrs)
8458{
8459 for (; args; args = TREE_CHAIN (args))
8460 {
8461 tree position = TREE_VALUE (args);
8462 if (position && TREE_CODE (position) != IDENTIFIER_NODE
8463 && TREE_CODE (position) != FUNCTION_DECL)
8464 position = default_conversion (position);
8465
8466 if (TREE_CODE (position) != INTEGER_CST)
8467 {
8468 warning (OPT_Wattributes,
8469 "assume_aligned parameter not integer constant");
8470 *no_add_attrs = true;
8471 return NULL_TREE;
8472 }
8473 }
8474 return NULL_TREE;
8475}
8476
0b7b376d
RG
8477/* Handle a "fn spec" attribute; arguments as in
8478 struct attribute_spec.handler. */
8479
8480static tree
8481handle_fnspec_attribute (tree *node ATTRIBUTE_UNUSED, tree ARG_UNUSED (name),
8482 tree args, int ARG_UNUSED (flags),
8483 bool *no_add_attrs ATTRIBUTE_UNUSED)
8484{
8485 gcc_assert (args
8486 && TREE_CODE (TREE_VALUE (args)) == STRING_CST
8487 && !TREE_CHAIN (args));
8488 return NULL_TREE;
8489}
8490
d5e254e1
IE
8491/* Handle a "bnd_variable_size" attribute; arguments as in
8492 struct attribute_spec.handler. */
8493
8494static tree
8495handle_bnd_variable_size_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8496 int ARG_UNUSED (flags), bool *no_add_attrs)
8497{
8498 if (TREE_CODE (*node) != FIELD_DECL)
8499 {
8500 warning (OPT_Wattributes, "%qE attribute ignored", name);
8501 *no_add_attrs = true;
8502 }
8503
8504 return NULL_TREE;
8505}
8506
8507/* Handle a "bnd_legacy" attribute; arguments as in
8508 struct attribute_spec.handler. */
8509
8510static tree
8511handle_bnd_legacy (tree *node, tree name, tree ARG_UNUSED (args),
8512 int ARG_UNUSED (flags), bool *no_add_attrs)
8513{
8514 if (TREE_CODE (*node) != FUNCTION_DECL)
8515 {
8516 warning (OPT_Wattributes, "%qE attribute ignored", name);
8517 *no_add_attrs = true;
8518 }
8519
8520 return NULL_TREE;
8521}
8522
8523/* Handle a "bnd_instrument" attribute; arguments as in
8524 struct attribute_spec.handler. */
8525
8526static tree
8527handle_bnd_instrument (tree *node, tree name, tree ARG_UNUSED (args),
8528 int ARG_UNUSED (flags), bool *no_add_attrs)
8529{
8530 if (TREE_CODE (*node) != FUNCTION_DECL)
8531 {
8532 warning (OPT_Wattributes, "%qE attribute ignored", name);
8533 *no_add_attrs = true;
8534 }
8535
8536 return NULL_TREE;
8537}
8538
2a99e5e6
LL
8539/* Handle a "warn_unused" attribute; arguments as in
8540 struct attribute_spec.handler. */
8541
8542static tree
8543handle_warn_unused_attribute (tree *node, tree name,
8544 tree args ATTRIBUTE_UNUSED,
8545 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
8546{
8547 if (TYPE_P (*node))
8548 /* Do nothing else, just set the attribute. We'll get at
8549 it later with lookup_attribute. */
8550 ;
8551 else
8552 {
8553 warning (OPT_Wattributes, "%qE attribute ignored", name);
8554 *no_add_attrs = true;
8555 }
8556
8557 return NULL_TREE;
8558}
8559
acf0174b
JJ
8560/* Handle an "omp declare simd" attribute; arguments as in
8561 struct attribute_spec.handler. */
8562
8563static tree
8564handle_omp_declare_simd_attribute (tree *, tree, tree, int, bool *)
8565{
8566 return NULL_TREE;
8567}
8568
fff77217
KY
8569/* Handle a "simd" attribute. */
8570
8571static tree
e7b69085 8572handle_simd_attribute (tree *node, tree name, tree args, int, bool *no_add_attrs)
fff77217
KY
8573{
8574 if (TREE_CODE (*node) == FUNCTION_DECL)
8575 {
8576 if (lookup_attribute ("cilk simd function",
8577 DECL_ATTRIBUTES (*node)) != NULL)
8578 {
8579 error_at (DECL_SOURCE_LOCATION (*node),
8580 "%<__simd__%> attribute cannot be used in the same "
8581 "function marked as a Cilk Plus SIMD-enabled function");
8582 *no_add_attrs = true;
8583 }
8584 else
e7b69085
KY
8585 {
8586 tree t = get_identifier ("omp declare simd");
8587 tree attr = NULL_TREE;
8588 if (args)
8589 {
8590 tree id = TREE_VALUE (args);
8591
8592 if (TREE_CODE (id) != STRING_CST)
8593 {
8594 error ("attribute %qE argument not a string", name);
8595 *no_add_attrs = true;
8596 return NULL_TREE;
8597 }
8598
8599 if (strcmp (TREE_STRING_POINTER (id), "notinbranch") == 0)
8600 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8601 OMP_CLAUSE_NOTINBRANCH);
8602 else
8603 if (strcmp (TREE_STRING_POINTER (id), "inbranch") == 0)
8604 attr = build_omp_clause (DECL_SOURCE_LOCATION (*node),
8605 OMP_CLAUSE_INBRANCH);
8606 else
8607 {
8608 error ("only %<inbranch%> and %<notinbranch%> flags are "
8609 "allowed for %<__simd__%> attribute");
8610 *no_add_attrs = true;
8611 return NULL_TREE;
8612 }
8613 }
8614
8615 DECL_ATTRIBUTES (*node) = tree_cons (t,
8616 build_tree_list (NULL_TREE,
8617 attr),
8618 DECL_ATTRIBUTES (*node));
8619 }
fff77217
KY
8620 }
8621 else
8622 {
8623 warning (OPT_Wattributes, "%qE attribute ignored", name);
8624 *no_add_attrs = true;
8625 }
8626
8627 return NULL_TREE;
8628}
8629
acf0174b
JJ
8630/* Handle an "omp declare target" attribute; arguments as in
8631 struct attribute_spec.handler. */
8632
8633static tree
8634handle_omp_declare_target_attribute (tree *, tree, tree, int, bool *)
8635{
8636 return NULL_TREE;
8637}
8638
6e9a3221
AN
8639/* Handle a "returns_twice" attribute; arguments as in
8640 struct attribute_spec.handler. */
8641
8642static tree
8643handle_returns_twice_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8644 int ARG_UNUSED (flags), bool *no_add_attrs)
8645{
8646 if (TREE_CODE (*node) == FUNCTION_DECL)
8647 DECL_IS_RETURNS_TWICE (*node) = 1;
8648 else
8649 {
5c498b10 8650 warning (OPT_Wattributes, "%qE attribute ignored", name);
6e9a3221
AN
8651 *no_add_attrs = true;
8652 }
8653
8654 return NULL_TREE;
8655}
8656
349ae713
NB
8657/* Handle a "no_limit_stack" attribute; arguments as in
8658 struct attribute_spec.handler. */
8659
8660static tree
35b1a6fa 8661handle_no_limit_stack_attribute (tree *node, tree name,
e18476eb
BI
8662 tree ARG_UNUSED (args),
8663 int ARG_UNUSED (flags),
a742c759 8664 bool *no_add_attrs)
349ae713
NB
8665{
8666 tree decl = *node;
8667
8668 if (TREE_CODE (decl) != FUNCTION_DECL)
8669 {
c5d75364
MLI
8670 error_at (DECL_SOURCE_LOCATION (decl),
8671 "%qE attribute applies only to functions", name);
349ae713
NB
8672 *no_add_attrs = true;
8673 }
8674 else if (DECL_INITIAL (decl))
8675 {
c5d75364
MLI
8676 error_at (DECL_SOURCE_LOCATION (decl),
8677 "can%'t set %qE attribute after definition", name);
349ae713
NB
8678 *no_add_attrs = true;
8679 }
8680 else
8681 DECL_NO_LIMIT_STACK (decl) = 1;
8682
8683 return NULL_TREE;
8684}
8685
8686/* Handle a "pure" attribute; arguments as in
8687 struct attribute_spec.handler. */
8688
8689static tree
e18476eb
BI
8690handle_pure_attribute (tree *node, tree name, tree ARG_UNUSED (args),
8691 int ARG_UNUSED (flags), bool *no_add_attrs)
349ae713
NB
8692{
8693 if (TREE_CODE (*node) == FUNCTION_DECL)
becfd6e5 8694 DECL_PURE_P (*node) = 1;
349ae713
NB
8695 /* ??? TODO: Support types. */
8696 else
8697 {
5c498b10 8698 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
8699 *no_add_attrs = true;
8700 }
8701
8702 return NULL_TREE;
8703}
8704
0a35513e
AH
8705/* Digest an attribute list destined for a transactional memory statement.
8706 ALLOWED is the set of attributes that are allowed for this statement;
8707 return the attribute we parsed. Multiple attributes are never allowed. */
8708
8709int
8710parse_tm_stmt_attr (tree attrs, int allowed)
8711{
8712 tree a_seen = NULL;
8713 int m_seen = 0;
8714
8715 for ( ; attrs ; attrs = TREE_CHAIN (attrs))
8716 {
8717 tree a = TREE_PURPOSE (attrs);
8718 int m = 0;
8719
8720 if (is_attribute_p ("outer", a))
8721 m = TM_STMT_ATTR_OUTER;
8722
8723 if ((m & allowed) == 0)
8724 {
8725 warning (OPT_Wattributes, "%qE attribute directive ignored", a);
8726 continue;
8727 }
8728
8729 if (m_seen == 0)
8730 {
8731 a_seen = a;
8732 m_seen = m;
8733 }
8734 else if (m_seen == m)
8735 warning (OPT_Wattributes, "%qE attribute duplicated", a);
8736 else
8737 warning (OPT_Wattributes, "%qE attribute follows %qE", a, a_seen);
8738 }
8739
8740 return m_seen;
8741}
8742
8743/* Transform a TM attribute name into a maskable integer and back.
8744 Note that NULL (i.e. no attribute) is mapped to UNKNOWN, corresponding
8745 to how the lack of an attribute is treated. */
8746
8747int
8748tm_attr_to_mask (tree attr)
8749{
8750 if (attr == NULL)
8751 return 0;
8752 if (is_attribute_p ("transaction_safe", attr))
8753 return TM_ATTR_SAFE;
8754 if (is_attribute_p ("transaction_callable", attr))
8755 return TM_ATTR_CALLABLE;
8756 if (is_attribute_p ("transaction_pure", attr))
8757 return TM_ATTR_PURE;
8758 if (is_attribute_p ("transaction_unsafe", attr))
8759 return TM_ATTR_IRREVOCABLE;
8760 if (is_attribute_p ("transaction_may_cancel_outer", attr))
8761 return TM_ATTR_MAY_CANCEL_OUTER;
8762 return 0;
8763}
8764
8765tree
8766tm_mask_to_attr (int mask)
8767{
8768 const char *str;
8769 switch (mask)
8770 {
8771 case TM_ATTR_SAFE:
8772 str = "transaction_safe";
8773 break;
8774 case TM_ATTR_CALLABLE:
8775 str = "transaction_callable";
8776 break;
8777 case TM_ATTR_PURE:
8778 str = "transaction_pure";
8779 break;
8780 case TM_ATTR_IRREVOCABLE:
8781 str = "transaction_unsafe";
8782 break;
8783 case TM_ATTR_MAY_CANCEL_OUTER:
8784 str = "transaction_may_cancel_outer";
8785 break;
8786 default:
8787 gcc_unreachable ();
8788 }
8789 return get_identifier (str);
8790}
8791
8792/* Return the first TM attribute seen in LIST. */
8793
8794tree
8795find_tm_attribute (tree list)
8796{
8797 for (; list ; list = TREE_CHAIN (list))
8798 {
8799 tree name = TREE_PURPOSE (list);
8800 if (tm_attr_to_mask (name) != 0)
8801 return name;
8802 }
8803 return NULL_TREE;
8804}
8805
8806/* Handle the TM attributes; arguments as in struct attribute_spec.handler.
8807 Here we accept only function types, and verify that none of the other
8808 function TM attributes are also applied. */
8809/* ??? We need to accept class types for C++, but not C. This greatly
8810 complicates this function, since we can no longer rely on the extra
8811 processing given by function_type_required. */
8812
8813static tree
8814handle_tm_attribute (tree *node, tree name, tree args,
8815 int flags, bool *no_add_attrs)
8816{
8817 /* Only one path adds the attribute; others don't. */
8818 *no_add_attrs = true;
8819
8820 switch (TREE_CODE (*node))
8821 {
8822 case RECORD_TYPE:
8823 case UNION_TYPE:
8824 /* Only tm_callable and tm_safe apply to classes. */
8825 if (tm_attr_to_mask (name) & ~(TM_ATTR_SAFE | TM_ATTR_CALLABLE))
8826 goto ignored;
8827 /* FALLTHRU */
8828
8829 case FUNCTION_TYPE:
8830 case METHOD_TYPE:
8831 {
8832 tree old_name = find_tm_attribute (TYPE_ATTRIBUTES (*node));
8833 if (old_name == name)
8834 ;
8835 else if (old_name != NULL_TREE)
8836 error ("type was previously declared %qE", old_name);
8837 else
8838 *no_add_attrs = false;
8839 }
8840 break;
8841
b8fd7909
JM
8842 case FUNCTION_DECL:
8843 {
8844 /* transaction_safe_dynamic goes on the FUNCTION_DECL, but we also
8845 want to set transaction_safe on the type. */
8846 gcc_assert (is_attribute_p ("transaction_safe_dynamic", name));
8847 if (!TYPE_P (DECL_CONTEXT (*node)))
8848 error_at (DECL_SOURCE_LOCATION (*node),
8849 "%<transaction_safe_dynamic%> may only be specified for "
8850 "a virtual function");
8851 *no_add_attrs = false;
8852 decl_attributes (&TREE_TYPE (*node),
8853 build_tree_list (get_identifier ("transaction_safe"),
8854 NULL_TREE),
8855 0);
8856 break;
8857 }
8858
0a35513e
AH
8859 case POINTER_TYPE:
8860 {
8861 enum tree_code subcode = TREE_CODE (TREE_TYPE (*node));
8862 if (subcode == FUNCTION_TYPE || subcode == METHOD_TYPE)
8863 {
8864 tree fn_tmp = TREE_TYPE (*node);
8865 decl_attributes (&fn_tmp, tree_cons (name, args, NULL), 0);
8866 *node = build_pointer_type (fn_tmp);
8867 break;
8868 }
8869 }
8870 /* FALLTHRU */
8871
8872 default:
8873 /* If a function is next, pass it on to be tried next. */
8874 if (flags & (int) ATTR_FLAG_FUNCTION_NEXT)
8875 return tree_cons (name, args, NULL);
8876
8877 ignored:
8878 warning (OPT_Wattributes, "%qE attribute ignored", name);
8879 break;
8880 }
8881
8882 return NULL_TREE;
8883}
8884
8885/* Handle the TM_WRAP attribute; arguments as in
8886 struct attribute_spec.handler. */
8887
8888static tree
8889handle_tm_wrap_attribute (tree *node, tree name, tree args,
8890 int ARG_UNUSED (flags), bool *no_add_attrs)
8891{
8892 tree decl = *node;
8893
8894 /* We don't need the attribute even on success, since we
8895 record the entry in an external table. */
8896 *no_add_attrs = true;
8897
8898 if (TREE_CODE (decl) != FUNCTION_DECL)
8899 warning (OPT_Wattributes, "%qE attribute ignored", name);
8900 else
8901 {
8902 tree wrap_decl = TREE_VALUE (args);
661a0813
MP
8903 if (error_operand_p (wrap_decl))
8904 ;
8905 else if (TREE_CODE (wrap_decl) != IDENTIFIER_NODE
21b634ae 8906 && !VAR_OR_FUNCTION_DECL_P (wrap_decl))
0a35513e
AH
8907 error ("%qE argument not an identifier", name);
8908 else
8909 {
8910 if (TREE_CODE (wrap_decl) == IDENTIFIER_NODE)
8911 wrap_decl = lookup_name (wrap_decl);
8912 if (wrap_decl && TREE_CODE (wrap_decl) == FUNCTION_DECL)
8913 {
8914 if (lang_hooks.types_compatible_p (TREE_TYPE (decl),
8915 TREE_TYPE (wrap_decl)))
8916 record_tm_replacement (wrap_decl, decl);
8917 else
8918 error ("%qD is not compatible with %qD", wrap_decl, decl);
8919 }
8920 else
c07d7c02 8921 error ("%qE argument is not a function", name);
0a35513e
AH
8922 }
8923 }
8924
8925 return NULL_TREE;
8926}
8927
8928/* Ignore the given attribute. Used when this attribute may be usefully
8929 overridden by the target, but is not used generically. */
8930
8931static tree
8932ignore_attribute (tree * ARG_UNUSED (node), tree ARG_UNUSED (name),
8933 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8934 bool *no_add_attrs)
8935{
8936 *no_add_attrs = true;
8937 return NULL_TREE;
8938}
8939
dcd6de6d
ZD
8940/* Handle a "no vops" attribute; arguments as in
8941 struct attribute_spec.handler. */
8942
8943static tree
8944handle_novops_attribute (tree *node, tree ARG_UNUSED (name),
8945 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
8946 bool *ARG_UNUSED (no_add_attrs))
8947{
8948 gcc_assert (TREE_CODE (*node) == FUNCTION_DECL);
8949 DECL_IS_NOVOPS (*node) = 1;
8950 return NULL_TREE;
8951}
8952
349ae713
NB
8953/* Handle a "deprecated" attribute; arguments as in
8954 struct attribute_spec.handler. */
35b1a6fa 8955
349ae713 8956static tree
35b1a6fa 8957handle_deprecated_attribute (tree *node, tree name,
9b86d6bb 8958 tree args, int flags,
a742c759 8959 bool *no_add_attrs)
349ae713
NB
8960{
8961 tree type = NULL_TREE;
8962 int warn = 0;
c51a1ba9 8963 tree what = NULL_TREE;
35b1a6fa 8964
9b86d6bb
L
8965 if (!args)
8966 *no_add_attrs = true;
8967 else if (TREE_CODE (TREE_VALUE (args)) != STRING_CST)
8968 {
8969 error ("deprecated message is not a string");
8970 *no_add_attrs = true;
8971 }
8972
349ae713
NB
8973 if (DECL_P (*node))
8974 {
8975 tree decl = *node;
8976 type = TREE_TYPE (decl);
35b1a6fa 8977
349ae713
NB
8978 if (TREE_CODE (decl) == TYPE_DECL
8979 || TREE_CODE (decl) == PARM_DECL
21b634ae 8980 || VAR_OR_FUNCTION_DECL_P (decl)
a1178b30 8981 || TREE_CODE (decl) == FIELD_DECL
fd5c817a 8982 || TREE_CODE (decl) == CONST_DECL
a1178b30 8983 || objc_method_decl (TREE_CODE (decl)))
349ae713
NB
8984 TREE_DEPRECATED (decl) = 1;
8985 else
8986 warn = 1;
8987 }
8988 else if (TYPE_P (*node))
8989 {
8990 if (!(flags & (int) ATTR_FLAG_TYPE_IN_PLACE))
8dd16ecc 8991 *node = build_variant_type_copy (*node);
349ae713
NB
8992 TREE_DEPRECATED (*node) = 1;
8993 type = *node;
8994 }
8995 else
8996 warn = 1;
35b1a6fa 8997
349ae713
NB
8998 if (warn)
8999 {
9000 *no_add_attrs = true;
9001 if (type && TYPE_NAME (type))
9002 {
9003 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9 9004 what = TYPE_NAME (*node);
349ae713
NB
9005 else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9006 && DECL_NAME (TYPE_NAME (type)))
c51a1ba9 9007 what = DECL_NAME (TYPE_NAME (type));
349ae713
NB
9008 }
9009 if (what)
5c498b10 9010 warning (OPT_Wattributes, "%qE attribute ignored for %qE", name, what);
349ae713 9011 else
5c498b10 9012 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
9013 }
9014
9015 return NULL_TREE;
9016}
9017
349ae713
NB
9018/* Handle a "vector_size" attribute; arguments as in
9019 struct attribute_spec.handler. */
9020
9021static tree
35b1a6fa 9022handle_vector_size_attribute (tree *node, tree name, tree args,
e18476eb 9023 int ARG_UNUSED (flags),
a742c759 9024 bool *no_add_attrs)
349ae713
NB
9025{
9026 unsigned HOST_WIDE_INT vecsize, nunits;
ef4bddc2 9027 machine_mode orig_mode;
4a5eab38 9028 tree type = *node, new_type, size;
349ae713
NB
9029
9030 *no_add_attrs = true;
9031
4a5eab38 9032 size = TREE_VALUE (args);
661a0813
MP
9033 if (size && TREE_CODE (size) != IDENTIFIER_NODE
9034 && TREE_CODE (size) != FUNCTION_DECL)
5d77fb19 9035 size = default_conversion (size);
4a5eab38 9036
cc269bb6 9037 if (!tree_fits_uhwi_p (size))
349ae713 9038 {
5c498b10 9039 warning (OPT_Wattributes, "%qE attribute ignored", name);
349ae713
NB
9040 return NULL_TREE;
9041 }
9042
9043 /* Get the vector size (in bytes). */
ae7e9ddd 9044 vecsize = tree_to_uhwi (size);
349ae713
NB
9045
9046 /* We need to provide for vector pointers, vector arrays, and
9047 functions returning vectors. For example:
9048
9049 __attribute__((vector_size(16))) short *foo;
9050
9051 In this case, the mode is SI, but the type being modified is
9052 HI, so we need to look further. */
9053
9054 while (POINTER_TYPE_P (type)
9055 || TREE_CODE (type) == FUNCTION_TYPE
43dc123f 9056 || TREE_CODE (type) == METHOD_TYPE
270e749d
JJ
9057 || TREE_CODE (type) == ARRAY_TYPE
9058 || TREE_CODE (type) == OFFSET_TYPE)
349ae713
NB
9059 type = TREE_TYPE (type);
9060
9061 /* Get the mode of the type being modified. */
9062 orig_mode = TYPE_MODE (type);
9063
270e749d
JJ
9064 if ((!INTEGRAL_TYPE_P (type)
9065 && !SCALAR_FLOAT_TYPE_P (type)
9066 && !FIXED_POINT_TYPE_P (type))
3d8bf70f 9067 || (!SCALAR_FLOAT_MODE_P (orig_mode)
ab22c1fa
CF
9068 && GET_MODE_CLASS (orig_mode) != MODE_INT
9069 && !ALL_SCALAR_FIXED_POINT_MODE_P (orig_mode))
cc269bb6 9070 || !tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))
e4e5261f 9071 || TREE_CODE (type) == BOOLEAN_TYPE)
349ae713 9072 {
c51a1ba9 9073 error ("invalid vector type for attribute %qE", name);
349ae713
NB
9074 return NULL_TREE;
9075 }
9076
ae7e9ddd 9077 if (vecsize % tree_to_uhwi (TYPE_SIZE_UNIT (type)))
ee8960e5
JJ
9078 {
9079 error ("vector size not an integral multiple of component size");
9080 return NULL;
9081 }
9082
9083 if (vecsize == 0)
9084 {
9085 error ("zero vector size");
9086 return NULL;
9087 }
9088
349ae713 9089 /* Calculate how many units fit in the vector. */
ae7e9ddd 9090 nunits = vecsize / tree_to_uhwi (TYPE_SIZE_UNIT (type));
26277d41 9091 if (nunits & (nunits - 1))
349ae713 9092 {
26277d41 9093 error ("number of components of the vector not a power of two");
349ae713
NB
9094 return NULL_TREE;
9095 }
9096
26277d41 9097 new_type = build_vector_type (type, nunits);
349ae713
NB
9098
9099 /* Build back pointers if needed. */
5dc11954 9100 *node = lang_hooks.types.reconstruct_complex_type (*node, new_type);
349ae713
NB
9101
9102 return NULL_TREE;
9103}
9104
b34c7881
JT
9105/* Handle the "nonnull" attribute. */
9106static tree
e18476eb
BI
9107handle_nonnull_attribute (tree *node, tree ARG_UNUSED (name),
9108 tree args, int ARG_UNUSED (flags),
a742c759 9109 bool *no_add_attrs)
b34c7881
JT
9110{
9111 tree type = *node;
9112 unsigned HOST_WIDE_INT attr_arg_num;
9113
9114 /* If no arguments are specified, all pointer arguments should be
95bd1dd7 9115 non-null. Verify a full prototype is given so that the arguments
4d926e34
MS
9116 will have the correct types when we actually check them later.
9117 Avoid diagnosing type-generic built-ins since those have no
9118 prototype. */
3f75a254 9119 if (!args)
b34c7881 9120 {
4d926e34
MS
9121 if (!prototype_p (type)
9122 && (!TYPE_ATTRIBUTES (type)
9123 || !lookup_attribute ("type generic", TYPE_ATTRIBUTES (type))))
b34c7881
JT
9124 {
9125 error ("nonnull attribute without arguments on a non-prototype");
6de9cd9a 9126 *no_add_attrs = true;
b34c7881
JT
9127 }
9128 return NULL_TREE;
9129 }
9130
9131 /* Argument list specified. Verify that each argument number references
9132 a pointer argument. */
5d77fb19 9133 for (attr_arg_num = 1; args; attr_arg_num++, args = TREE_CHAIN (args))
b34c7881 9134 {
6de9cd9a 9135 unsigned HOST_WIDE_INT arg_num = 0, ck_num;
b34c7881 9136
5d77fb19
MG
9137 tree arg = TREE_VALUE (args);
9138 if (arg && TREE_CODE (arg) != IDENTIFIER_NODE
9139 && TREE_CODE (arg) != FUNCTION_DECL)
9140 arg = default_conversion (arg);
9141
9142 if (!get_nonnull_operand (arg, &arg_num))
b34c7881 9143 {
40b97a2e 9144 error ("nonnull argument has invalid operand number (argument %lu)",
b34c7881
JT
9145 (unsigned long) attr_arg_num);
9146 *no_add_attrs = true;
9147 return NULL_TREE;
9148 }
9149
e19a18d4 9150 if (prototype_p (type))
b34c7881 9151 {
e19a18d4
NF
9152 function_args_iterator iter;
9153 tree argument;
9154
9155 function_args_iter_init (&iter, type);
9156 for (ck_num = 1; ; ck_num++, function_args_iter_next (&iter))
b34c7881 9157 {
e19a18d4
NF
9158 argument = function_args_iter_cond (&iter);
9159 if (argument == NULL_TREE || ck_num == arg_num)
b34c7881 9160 break;
b34c7881
JT
9161 }
9162
3f75a254 9163 if (!argument
e19a18d4 9164 || TREE_CODE (argument) == VOID_TYPE)
b34c7881 9165 {
40b97a2e 9166 error ("nonnull argument with out-of-range operand number (argument %lu, operand %lu)",
b34c7881
JT
9167 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9168 *no_add_attrs = true;
9169 return NULL_TREE;
9170 }
9171
e19a18d4 9172 if (TREE_CODE (argument) != POINTER_TYPE)
b34c7881 9173 {
40b97a2e 9174 error ("nonnull argument references non-pointer operand (argument %lu, operand %lu)",
b34c7881
JT
9175 (unsigned long) attr_arg_num, (unsigned long) arg_num);
9176 *no_add_attrs = true;
9177 return NULL_TREE;
9178 }
9179 }
9180 }
9181
9182 return NULL_TREE;
9183}
9184
9185/* Check the argument list of a function call for null in argument slots
94a0dd7b 9186 that are marked as requiring a non-null pointer argument. The NARGS
3342fd71 9187 arguments are passed in the array ARGARRAY. */
b34c7881
JT
9188
9189static void
3342fd71 9190check_function_nonnull (location_t loc, tree attrs, int nargs, tree *argarray)
b34c7881 9191{
332f1d24 9192 tree a;
94a0dd7b 9193 int i;
b34c7881 9194
332f1d24
JJ
9195 attrs = lookup_attribute ("nonnull", attrs);
9196 if (attrs == NULL_TREE)
9197 return;
9198
9199 a = attrs;
9200 /* See if any of the nonnull attributes has no arguments. If so,
9201 then every pointer argument is checked (in which case the check
9202 for pointer type is done in check_nonnull_arg). */
9203 if (TREE_VALUE (a) != NULL_TREE)
9204 do
9205 a = lookup_attribute ("nonnull", TREE_CHAIN (a));
9206 while (a != NULL_TREE && TREE_VALUE (a) != NULL_TREE);
9207
9208 if (a != NULL_TREE)
9209 for (i = 0; i < nargs; i++)
3342fd71 9210 check_function_arguments_recurse (check_nonnull_arg, &loc, argarray[i],
332f1d24
JJ
9211 i + 1);
9212 else
b34c7881 9213 {
332f1d24
JJ
9214 /* Walk the argument list. If we encounter an argument number we
9215 should check for non-null, do it. */
9216 for (i = 0; i < nargs; i++)
b34c7881 9217 {
332f1d24 9218 for (a = attrs; ; a = TREE_CHAIN (a))
6de9cd9a 9219 {
332f1d24
JJ
9220 a = lookup_attribute ("nonnull", a);
9221 if (a == NULL_TREE || nonnull_check_p (TREE_VALUE (a), i + 1))
9222 break;
6de9cd9a 9223 }
332f1d24
JJ
9224
9225 if (a != NULL_TREE)
3342fd71 9226 check_function_arguments_recurse (check_nonnull_arg, &loc,
332f1d24 9227 argarray[i], i + 1);
b34c7881
JT
9228 }
9229 }
9230}
9231
254986c7 9232/* Check that the Nth argument of a function call (counting backwards
94a0dd7b
SL
9233 from the end) is a (pointer)0. The NARGS arguments are passed in the
9234 array ARGARRAY. */
3d091dac
KG
9235
9236static void
dde05067 9237check_function_sentinel (const_tree fntype, int nargs, tree *argarray)
3d091dac 9238{
dde05067 9239 tree attr = lookup_attribute ("sentinel", TYPE_ATTRIBUTES (fntype));
3d091dac
KG
9240
9241 if (attr)
9242 {
94a0dd7b
SL
9243 int len = 0;
9244 int pos = 0;
9245 tree sentinel;
dde05067
NF
9246 function_args_iterator iter;
9247 tree t;
c22cacf3 9248
94a0dd7b 9249 /* Skip over the named arguments. */
dde05067 9250 FOREACH_FUNCTION_ARGS (fntype, t, iter)
c22cacf3 9251 {
dde05067
NF
9252 if (len == nargs)
9253 break;
94a0dd7b
SL
9254 len++;
9255 }
254986c7 9256
94a0dd7b
SL
9257 if (TREE_VALUE (attr))
9258 {
9259 tree p = TREE_VALUE (TREE_VALUE (attr));
9260 pos = TREE_INT_CST_LOW (p);
9261 }
254986c7 9262
94a0dd7b
SL
9263 /* The sentinel must be one of the varargs, i.e.
9264 in position >= the number of fixed arguments. */
9265 if ((nargs - 1 - pos) < len)
9266 {
7332899a 9267 warning (OPT_Wformat_,
94a0dd7b
SL
9268 "not enough variable arguments to fit a sentinel");
9269 return;
3d091dac 9270 }
94a0dd7b
SL
9271
9272 /* Validate the sentinel. */
9273 sentinel = argarray[nargs - 1 - pos];
9274 if ((!POINTER_TYPE_P (TREE_TYPE (sentinel))
9275 || !integer_zerop (sentinel))
9276 /* Although __null (in C++) is only an integer we allow it
9277 nevertheless, as we are guaranteed that it's exactly
9278 as wide as a pointer, and we don't want to force
9279 users to cast the NULL they have written there.
9280 We warn with -Wstrict-null-sentinel, though. */
9281 && (warn_strict_null_sentinel || null_node != sentinel))
7332899a 9282 warning (OPT_Wformat_, "missing sentinel in function call");
3d091dac
KG
9283 }
9284}
9285
b34c7881
JT
9286/* Helper for check_function_nonnull; given a list of operands which
9287 must be non-null in ARGS, determine if operand PARAM_NUM should be
9288 checked. */
9289
9290static bool
35b1a6fa 9291nonnull_check_p (tree args, unsigned HOST_WIDE_INT param_num)
b34c7881 9292{
6de9cd9a 9293 unsigned HOST_WIDE_INT arg_num = 0;
b34c7881
JT
9294
9295 for (; args; args = TREE_CHAIN (args))
9296 {
366de0ce
NS
9297 bool found = get_nonnull_operand (TREE_VALUE (args), &arg_num);
9298
9299 gcc_assert (found);
b34c7881
JT
9300
9301 if (arg_num == param_num)
9302 return true;
9303 }
9304 return false;
9305}
9306
9307/* Check that the function argument PARAM (which is operand number
9308 PARAM_NUM) is non-null. This is called by check_function_nonnull
9309 via check_function_arguments_recurse. */
9310
9311static void
3342fd71 9312check_nonnull_arg (void *ctx, tree param, unsigned HOST_WIDE_INT param_num)
b34c7881 9313{
3342fd71
BS
9314 location_t *ploc = (location_t *) ctx;
9315
b34c7881
JT
9316 /* Just skip checking the argument if it's not a pointer. This can
9317 happen if the "nonnull" attribute was given without an operand
9318 list (which means to check every pointer argument). */
9319
9320 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
9321 return;
9322
9323 if (integer_zerop (param))
3342fd71
BS
9324 warning_at (*ploc, OPT_Wnonnull, "null argument where non-null required "
9325 "(argument %lu)", (unsigned long) param_num);
b34c7881
JT
9326}
9327
9328/* Helper for nonnull attribute handling; fetch the operand number
9329 from the attribute argument list. */
9330
9331static bool
35b1a6fa 9332get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
b34c7881 9333{
807e902e
KZ
9334 /* Verify the arg number is a small constant. */
9335 if (tree_fits_uhwi_p (arg_num_expr))
9336 {
9337 *valp = TREE_INT_CST_LOW (arg_num_expr);
9338 return true;
9339 }
9340 else
b34c7881 9341 return false;
b34c7881 9342}
39f2f3c8
RS
9343
9344/* Handle a "nothrow" attribute; arguments as in
9345 struct attribute_spec.handler. */
9346
9347static tree
e18476eb
BI
9348handle_nothrow_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9349 int ARG_UNUSED (flags), bool *no_add_attrs)
39f2f3c8
RS
9350{
9351 if (TREE_CODE (*node) == FUNCTION_DECL)
9352 TREE_NOTHROW (*node) = 1;
9353 /* ??? TODO: Support types. */
9354 else
9355 {
5c498b10 9356 warning (OPT_Wattributes, "%qE attribute ignored", name);
39f2f3c8
RS
9357 *no_add_attrs = true;
9358 }
9359
9360 return NULL_TREE;
9361}
0bfa5f65
RH
9362
9363/* Handle a "cleanup" attribute; arguments as in
9364 struct attribute_spec.handler. */
9365
9366static tree
35b1a6fa 9367handle_cleanup_attribute (tree *node, tree name, tree args,
e18476eb 9368 int ARG_UNUSED (flags), bool *no_add_attrs)
0bfa5f65
RH
9369{
9370 tree decl = *node;
9371 tree cleanup_id, cleanup_decl;
9372
9373 /* ??? Could perhaps support cleanups on TREE_STATIC, much like we do
9374 for global destructors in C++. This requires infrastructure that
9375 we don't have generically at the moment. It's also not a feature
9376 we'd be missing too much, since we do have attribute constructor. */
0ae9bd27 9377 if (!VAR_P (decl) || TREE_STATIC (decl))
0bfa5f65 9378 {
5c498b10 9379 warning (OPT_Wattributes, "%qE attribute ignored", name);
0bfa5f65
RH
9380 *no_add_attrs = true;
9381 return NULL_TREE;
9382 }
9383
9384 /* Verify that the argument is a function in scope. */
9385 /* ??? We could support pointers to functions here as well, if
9386 that was considered desirable. */
9387 cleanup_id = TREE_VALUE (args);
9388 if (TREE_CODE (cleanup_id) != IDENTIFIER_NODE)
9389 {
40b97a2e 9390 error ("cleanup argument not an identifier");
0bfa5f65
RH
9391 *no_add_attrs = true;
9392 return NULL_TREE;
9393 }
10e6657a 9394 cleanup_decl = lookup_name (cleanup_id);
0bfa5f65
RH
9395 if (!cleanup_decl || TREE_CODE (cleanup_decl) != FUNCTION_DECL)
9396 {
40b97a2e 9397 error ("cleanup argument not a function");
0bfa5f65
RH
9398 *no_add_attrs = true;
9399 return NULL_TREE;
9400 }
9401
35b1a6fa 9402 /* That the function has proper type is checked with the
0bfa5f65
RH
9403 eventual call to build_function_call. */
9404
9405 return NULL_TREE;
9406}
72954a4f
JM
9407
9408/* Handle a "warn_unused_result" attribute. No special handling. */
9409
9410static tree
9411handle_warn_unused_result_attribute (tree *node, tree name,
e18476eb
BI
9412 tree ARG_UNUSED (args),
9413 int ARG_UNUSED (flags), bool *no_add_attrs)
72954a4f
JM
9414{
9415 /* Ignore the attribute for functions not returning any value. */
9416 if (VOID_TYPE_P (TREE_TYPE (*node)))
9417 {
5c498b10 9418 warning (OPT_Wattributes, "%qE attribute ignored", name);
72954a4f
JM
9419 *no_add_attrs = true;
9420 }
9421
9422 return NULL_TREE;
9423}
3d091dac
KG
9424
9425/* Handle a "sentinel" attribute. */
9426
9427static tree
254986c7 9428handle_sentinel_attribute (tree *node, tree name, tree args,
3d091dac
KG
9429 int ARG_UNUSED (flags), bool *no_add_attrs)
9430{
f4da8dce 9431 if (!prototype_p (*node))
3d091dac 9432 {
5c498b10
DD
9433 warning (OPT_Wattributes,
9434 "%qE attribute requires prototypes with named arguments", name);
3d091dac 9435 *no_add_attrs = true;
3d091dac 9436 }
254986c7
KG
9437 else
9438 {
dcf0c47e 9439 if (!stdarg_p (*node))
c22cacf3 9440 {
5c498b10
DD
9441 warning (OPT_Wattributes,
9442 "%qE attribute only applies to variadic functions", name);
254986c7
KG
9443 *no_add_attrs = true;
9444 }
9445 }
c22cacf3 9446
254986c7 9447 if (args)
3d091dac 9448 {
254986c7 9449 tree position = TREE_VALUE (args);
661a0813
MP
9450 if (position && TREE_CODE (position) != IDENTIFIER_NODE
9451 && TREE_CODE (position) != FUNCTION_DECL)
9452 position = default_conversion (position);
254986c7 9453
661a0813
MP
9454 if (TREE_CODE (position) != INTEGER_CST
9455 || !INTEGRAL_TYPE_P (TREE_TYPE (position)))
c22cacf3 9456 {
b8698a0f 9457 warning (OPT_Wattributes,
aa86a51b 9458 "requested position is not an integer constant");
254986c7
KG
9459 *no_add_attrs = true;
9460 }
9461 else
c22cacf3 9462 {
254986c7
KG
9463 if (tree_int_cst_lt (position, integer_zero_node))
9464 {
aa86a51b
DM
9465 warning (OPT_Wattributes,
9466 "requested position is less than zero");
254986c7
KG
9467 *no_add_attrs = true;
9468 }
9469 }
3d091dac 9470 }
c22cacf3 9471
3d091dac
KG
9472 return NULL_TREE;
9473}
b5d32c25
KG
9474
9475/* Handle a "type_generic" attribute. */
9476
9477static tree
9478handle_type_generic_attribute (tree *node, tree ARG_UNUSED (name),
9479 tree ARG_UNUSED (args), int ARG_UNUSED (flags),
9480 bool * ARG_UNUSED (no_add_attrs))
9481{
3bf5906b
KG
9482 /* Ensure we have a function type. */
9483 gcc_assert (TREE_CODE (*node) == FUNCTION_TYPE);
b8698a0f 9484
3bf5906b 9485 /* Ensure we have a variadic function. */
dcf0c47e 9486 gcc_assert (!prototype_p (*node) || stdarg_p (*node));
b5d32c25
KG
9487
9488 return NULL_TREE;
9489}
ab442df7 9490
5779e713 9491/* Handle a "target" attribute. */
ab442df7
MM
9492
9493static tree
5779e713 9494handle_target_attribute (tree *node, tree name, tree args, int flags,
ab442df7
MM
9495 bool *no_add_attrs)
9496{
9497 /* Ensure we have a function type. */
9498 if (TREE_CODE (*node) != FUNCTION_DECL)
9499 {
9500 warning (OPT_Wattributes, "%qE attribute ignored", name);
9501 *no_add_attrs = true;
9502 }
3b1661a9
ES
9503 else if (lookup_attribute ("target_clones", DECL_ATTRIBUTES (*node)))
9504 {
9505 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9506 "with %qs attribute", name, "target_clones");
9507 *no_add_attrs = true;
9508 }
ab442df7 9509 else if (! targetm.target_option.valid_attribute_p (*node, name, args,
32887460 9510 flags))
ab442df7
MM
9511 *no_add_attrs = true;
9512
9513 return NULL_TREE;
9514}
9515
3b1661a9
ES
9516/* Handle a "target_clones" attribute. */
9517
9518static tree
9519handle_target_clones_attribute (tree *node, tree name, tree ARG_UNUSED (args),
9520 int ARG_UNUSED (flags), bool *no_add_attrs)
9521{
9522 /* Ensure we have a function type. */
9523 if (TREE_CODE (*node) == FUNCTION_DECL)
9524 {
9525 if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (*node)))
9526 {
9527 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9528 "with %qs attribute", name, "always_inline");
9529 *no_add_attrs = true;
9530 }
9531 else if (lookup_attribute ("target", DECL_ATTRIBUTES (*node)))
9532 {
9533 warning (OPT_Wattributes, "%qE attribute ignored due to conflict "
9534 "with %qs attribute", name, "target");
9535 *no_add_attrs = true;
9536 }
9537 else
9538 /* Do not inline functions with multiple clone targets. */
9539 DECL_UNINLINABLE (*node) = 1;
9540 }
9541 else
9542 {
9543 warning (OPT_Wattributes, "%qE attribute ignored", name);
9544 *no_add_attrs = true;
9545 }
9546 return NULL_TREE;
9547}
9548
ab442df7
MM
9549/* Arguments being collected for optimization. */
9550typedef const char *const_char_p; /* For DEF_VEC_P. */
9771b263 9551static GTY(()) vec<const_char_p, va_gc> *optimize_args;
ab442df7
MM
9552
9553
9554/* Inner function to convert a TREE_LIST to argv string to parse the optimize
9555 options in ARGS. ATTR_P is true if this is for attribute(optimize), and
9556 false for #pragma GCC optimize. */
9557
9558bool
9559parse_optimize_options (tree args, bool attr_p)
9560{
9561 bool ret = true;
9562 unsigned opt_argc;
9563 unsigned i;
9564 const char **opt_argv;
6e2f1956
JM
9565 struct cl_decoded_option *decoded_options;
9566 unsigned int decoded_options_count;
ab442df7
MM
9567 tree ap;
9568
9569 /* Build up argv vector. Just in case the string is stored away, use garbage
9570 collected strings. */
9771b263
DN
9571 vec_safe_truncate (optimize_args, 0);
9572 vec_safe_push (optimize_args, (const char *) NULL);
ab442df7
MM
9573
9574 for (ap = args; ap != NULL_TREE; ap = TREE_CHAIN (ap))
9575 {
9576 tree value = TREE_VALUE (ap);
9577
9578 if (TREE_CODE (value) == INTEGER_CST)
9579 {
9580 char buffer[20];
9581 sprintf (buffer, "-O%ld", (long) TREE_INT_CST_LOW (value));
9771b263 9582 vec_safe_push (optimize_args, ggc_strdup (buffer));
ab442df7
MM
9583 }
9584
9585 else if (TREE_CODE (value) == STRING_CST)
9586 {
9587 /* Split string into multiple substrings. */
9588 size_t len = TREE_STRING_LENGTH (value);
9589 char *p = ASTRDUP (TREE_STRING_POINTER (value));
9590 char *end = p + len;
9591 char *comma;
9592 char *next_p = p;
9593
9594 while (next_p != NULL)
9595 {
9596 size_t len2;
9597 char *q, *r;
9598
9599 p = next_p;
9600 comma = strchr (p, ',');
9601 if (comma)
9602 {
9603 len2 = comma - p;
9604 *comma = '\0';
9605 next_p = comma+1;
9606 }
9607 else
9608 {
9609 len2 = end - p;
9610 next_p = NULL;
9611 }
9612
a9429e29 9613 r = q = (char *) ggc_alloc_atomic (len2 + 3);
ab442df7
MM
9614
9615 /* If the user supplied -Oxxx or -fxxx, only allow -Oxxx or -fxxx
9616 options. */
9617 if (*p == '-' && p[1] != 'O' && p[1] != 'f')
9618 {
9619 ret = false;
9620 if (attr_p)
9621 warning (OPT_Wattributes,
a80a7051 9622 "bad option %qs to attribute %<optimize%>", p);
ab442df7
MM
9623 else
9624 warning (OPT_Wpragmas,
a80a7051 9625 "bad option %qs to pragma %<optimize%>", p);
ab442df7
MM
9626 continue;
9627 }
9628
9629 if (*p != '-')
9630 {
9631 *r++ = '-';
9632
9633 /* Assume that Ox is -Ox, a numeric value is -Ox, a s by
9634 itself is -Os, and any other switch begins with a -f. */
9635 if ((*p >= '0' && *p <= '9')
9636 || (p[0] == 's' && p[1] == '\0'))
9637 *r++ = 'O';
9638 else if (*p != 'O')
9639 *r++ = 'f';
9640 }
9641
9642 memcpy (r, p, len2);
9643 r[len2] = '\0';
9771b263 9644 vec_safe_push (optimize_args, (const char *) q);
ab442df7
MM
9645 }
9646
9647 }
9648 }
9649
9771b263 9650 opt_argc = optimize_args->length ();
ab442df7
MM
9651 opt_argv = (const char **) alloca (sizeof (char *) * (opt_argc + 1));
9652
9653 for (i = 1; i < opt_argc; i++)
9771b263 9654 opt_argv[i] = (*optimize_args)[i];
ab442df7
MM
9655
9656 /* Now parse the options. */
a75bfaa6
JM
9657 decode_cmdline_options_to_array_default_mask (opt_argc, opt_argv,
9658 &decoded_options,
9659 &decoded_options_count);
bfd67b47
RB
9660 /* Drop non-Optimization options. */
9661 unsigned j = 1;
9662 for (i = 1; i < decoded_options_count; ++i)
9663 {
9664 if (! (cl_options[decoded_options[i].opt_index].flags & CL_OPTIMIZATION))
9665 {
9666 ret = false;
9667 if (attr_p)
9668 warning (OPT_Wattributes,
a80a7051 9669 "bad option %qs to attribute %<optimize%>",
bfd67b47
RB
9670 decoded_options[i].orig_option_with_args_text);
9671 else
9672 warning (OPT_Wpragmas,
a80a7051 9673 "bad option %qs to pragma %<optimize%>",
bfd67b47
RB
9674 decoded_options[i].orig_option_with_args_text);
9675 continue;
9676 }
9677 if (i != j)
9678 decoded_options[j] = decoded_options[i];
9679 j++;
9680 }
9681 decoded_options_count = j;
9682 /* And apply them. */
a75bfaa6 9683 decode_options (&global_options, &global_options_set,
a4d8c676
JM
9684 decoded_options, decoded_options_count,
9685 input_location, global_dc);
ab442df7 9686
2b7e2984
SE
9687 targetm.override_options_after_change();
9688
9771b263 9689 optimize_args->truncate (0);
ab442df7
MM
9690 return ret;
9691}
9692
9693/* For handling "optimize" attribute. arguments as in
9694 struct attribute_spec.handler. */
9695
9696static tree
9697handle_optimize_attribute (tree *node, tree name, tree args,
9698 int ARG_UNUSED (flags), bool *no_add_attrs)
9699{
9700 /* Ensure we have a function type. */
9701 if (TREE_CODE (*node) != FUNCTION_DECL)
9702 {
9703 warning (OPT_Wattributes, "%qE attribute ignored", name);
9704 *no_add_attrs = true;
9705 }
9706 else
9707 {
9708 struct cl_optimization cur_opts;
9709 tree old_opts = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node);
9710
9711 /* Save current options. */
46625112 9712 cl_optimization_save (&cur_opts, &global_options);
ab442df7
MM
9713
9714 /* If we previously had some optimization options, use them as the
9715 default. */
9716 if (old_opts)
46625112
JM
9717 cl_optimization_restore (&global_options,
9718 TREE_OPTIMIZATION (old_opts));
ab442df7
MM
9719
9720 /* Parse options, and update the vector. */
9721 parse_optimize_options (args, true);
9722 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (*node)
bf7b5747 9723 = build_optimization_node (&global_options);
ab442df7
MM
9724
9725 /* Restore current options. */
46625112 9726 cl_optimization_restore (&global_options, &cur_opts);
ab442df7
MM
9727 }
9728
9729 return NULL_TREE;
9730}
7458026b
ILT
9731
9732/* Handle a "no_split_stack" attribute. */
9733
9734static tree
9735handle_no_split_stack_attribute (tree *node, tree name,
9736 tree ARG_UNUSED (args),
9737 int ARG_UNUSED (flags),
9738 bool *no_add_attrs)
9739{
9740 tree decl = *node;
9741
9742 if (TREE_CODE (decl) != FUNCTION_DECL)
9743 {
9744 error_at (DECL_SOURCE_LOCATION (decl),
9745 "%qE attribute applies only to functions", name);
9746 *no_add_attrs = true;
9747 }
9748 else if (DECL_INITIAL (decl))
9749 {
9750 error_at (DECL_SOURCE_LOCATION (decl),
9751 "can%'t set %qE attribute after definition", name);
9752 *no_add_attrs = true;
9753 }
9754
9755 return NULL_TREE;
9756}
826cacfe
MG
9757
9758/* Handle a "returns_nonnull" attribute; arguments as in
9759 struct attribute_spec.handler. */
9760
9761static tree
9762handle_returns_nonnull_attribute (tree *node, tree, tree, int,
9763 bool *no_add_attrs)
9764{
9765 // Even without a prototype we still have a return type we can check.
9766 if (TREE_CODE (TREE_TYPE (*node)) != POINTER_TYPE)
9767 {
9768 error ("returns_nonnull attribute on a function not returning a pointer");
9769 *no_add_attrs = true;
9770 }
9771 return NULL_TREE;
9772}
9773
976d5a22
TT
9774/* Handle a "designated_init" attribute; arguments as in
9775 struct attribute_spec.handler. */
9776
9777static tree
9778handle_designated_init_attribute (tree *node, tree name, tree, int,
9779 bool *no_add_attrs)
9780{
9781 if (TREE_CODE (*node) != RECORD_TYPE)
9782 {
9783 error ("%qE attribute is only valid on %<struct%> type", name);
9784 *no_add_attrs = true;
9785 }
9786 return NULL_TREE;
9787}
9788
b34c7881 9789\f
dde05067 9790/* Check for valid arguments being passed to a function with FNTYPE.
3342fd71
BS
9791 There are NARGS arguments in the array ARGARRAY. LOC should be used for
9792 diagnostics. */
b34c7881 9793void
3342fd71
BS
9794check_function_arguments (location_t loc, const_tree fntype, int nargs,
9795 tree *argarray)
b34c7881
JT
9796{
9797 /* Check for null being passed in a pointer argument that must be
9798 non-null. We also need to do this if format checking is enabled. */
9799
9800 if (warn_nonnull)
3342fd71 9801 check_function_nonnull (loc, TYPE_ATTRIBUTES (fntype), nargs, argarray);
b34c7881
JT
9802
9803 /* Check for errors in format strings. */
9804
e6c69da0 9805 if (warn_format || warn_suggest_attribute_format)
dde05067 9806 check_function_format (TYPE_ATTRIBUTES (fntype), nargs, argarray);
7876a414
KG
9807
9808 if (warn_format)
dde05067 9809 check_function_sentinel (fntype, nargs, argarray);
b34c7881
JT
9810}
9811
9812/* Generic argument checking recursion routine. PARAM is the argument to
9813 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
9814 once the argument is resolved. CTX is context for the callback. */
9815void
35b1a6fa
AJ
9816check_function_arguments_recurse (void (*callback)
9817 (void *, tree, unsigned HOST_WIDE_INT),
9818 void *ctx, tree param,
9819 unsigned HOST_WIDE_INT param_num)
b34c7881 9820{
1043771b 9821 if (CONVERT_EXPR_P (param)
1344f9a3
JM
9822 && (TYPE_PRECISION (TREE_TYPE (param))
9823 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (param, 0)))))
b34c7881
JT
9824 {
9825 /* Strip coercion. */
9826 check_function_arguments_recurse (callback, ctx,
6de9cd9a 9827 TREE_OPERAND (param, 0), param_num);
b34c7881
JT
9828 return;
9829 }
9830
9831 if (TREE_CODE (param) == CALL_EXPR)
9832 {
5039610b 9833 tree type = TREE_TYPE (TREE_TYPE (CALL_EXPR_FN (param)));
b34c7881
JT
9834 tree attrs;
9835 bool found_format_arg = false;
9836
9837 /* See if this is a call to a known internationalization function
9838 that modifies a format arg. Such a function may have multiple
9839 format_arg attributes (for example, ngettext). */
9840
9841 for (attrs = TYPE_ATTRIBUTES (type);
9842 attrs;
9843 attrs = TREE_CHAIN (attrs))
9844 if (is_attribute_p ("format_arg", TREE_PURPOSE (attrs)))
9845 {
5039610b 9846 tree inner_arg;
b34c7881
JT
9847 tree format_num_expr;
9848 int format_num;
9849 int i;
5039610b 9850 call_expr_arg_iterator iter;
b34c7881
JT
9851
9852 /* Extract the argument number, which was previously checked
9853 to be valid. */
9854 format_num_expr = TREE_VALUE (TREE_VALUE (attrs));
b34c7881 9855
6b3b8c27 9856 format_num = tree_to_uhwi (format_num_expr);
b34c7881 9857
5039610b
SL
9858 for (inner_arg = first_call_expr_arg (param, &iter), i = 1;
9859 inner_arg != 0;
9860 inner_arg = next_call_expr_arg (&iter), i++)
b34c7881
JT
9861 if (i == format_num)
9862 {
9863 check_function_arguments_recurse (callback, ctx,
5039610b 9864 inner_arg, param_num);
b34c7881
JT
9865 found_format_arg = true;
9866 break;
9867 }
9868 }
9869
9870 /* If we found a format_arg attribute and did a recursive check,
9871 we are done with checking this argument. Otherwise, we continue
9872 and this will be considered a non-literal. */
9873 if (found_format_arg)
9874 return;
9875 }
9876
9877 if (TREE_CODE (param) == COND_EXPR)
9878 {
7f26f7df
JM
9879 /* Simplify to avoid warning for an impossible case. */
9880 param = fold_for_warn (param);
9881 if (TREE_CODE (param) == COND_EXPR)
9882 {
9883 /* Check both halves of the conditional expression. */
9884 check_function_arguments_recurse (callback, ctx,
9885 TREE_OPERAND (param, 1),
9886 param_num);
9887 check_function_arguments_recurse (callback, ctx,
9888 TREE_OPERAND (param, 2),
9889 param_num);
9890 return;
9891 }
b34c7881
JT
9892 }
9893
9894 (*callback) (ctx, param, param_num);
9895}
e2500fed 9896
a98c2819
MLI
9897/* Checks for a builtin function FNDECL that the number of arguments
9898 NARGS against the required number REQUIRED and issues an error if
9899 there is a mismatch. Returns true if the number of arguments is
79ce98bc 9900 correct, otherwise false. LOC is the location of FNDECL. */
83322951
RG
9901
9902static bool
79ce98bc
MP
9903builtin_function_validate_nargs (location_t loc, tree fndecl, int nargs,
9904 int required)
83322951
RG
9905{
9906 if (nargs < required)
9907 {
e01b4e16 9908 error_at (loc, "too few arguments to function %qE", fndecl);
83322951
RG
9909 return false;
9910 }
9911 else if (nargs > required)
9912 {
79ce98bc 9913 error_at (loc, "too many arguments to function %qE", fndecl);
83322951
RG
9914 return false;
9915 }
9916 return true;
9917}
9918
79ce98bc
MP
9919/* Helper macro for check_builtin_function_arguments. */
9920#define ARG_LOCATION(N) \
9921 (arg_loc.is_empty () \
9922 ? EXPR_LOC_OR_LOC (args[(N)], input_location) \
9923 : expansion_point_location (arg_loc[(N)]))
9924
83322951 9925/* Verifies the NARGS arguments ARGS to the builtin function FNDECL.
79ce98bc
MP
9926 Returns false if there was an error, otherwise true. LOC is the
9927 location of the function; ARG_LOC is a vector of locations of the
9928 arguments. */
83322951
RG
9929
9930bool
79ce98bc
MP
9931check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
9932 tree fndecl, int nargs, tree *args)
83322951
RG
9933{
9934 if (!DECL_BUILT_IN (fndecl)
9935 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
9936 return true;
9937
9938 switch (DECL_FUNCTION_CODE (fndecl))
9939 {
35886f0b
MS
9940 case BUILT_IN_ALLOCA_WITH_ALIGN:
9941 {
9942 /* Get the requested alignment (in bits) if it's a constant
9943 integer expression. */
9944 unsigned HOST_WIDE_INT align
9945 = tree_fits_uhwi_p (args[1]) ? tree_to_uhwi (args[1]) : 0;
9946
9947 /* Determine if the requested alignment is a power of 2. */
9948 if ((align & (align - 1)))
9949 align = 0;
9950
9951 /* The maximum alignment in bits corresponding to the same
9952 maximum in bytes enforced in check_user_alignment(). */
9953 unsigned maxalign = (UINT_MAX >> 1) + 1;
79ce98bc 9954
35886f0b
MS
9955 /* Reject invalid alignments. */
9956 if (align < BITS_PER_UNIT || maxalign < align)
9957 {
79ce98bc 9958 error_at (ARG_LOCATION (1),
35886f0b
MS
9959 "second argument to function %qE must be a constant "
9960 "integer power of 2 between %qi and %qu bits",
9961 fndecl, BITS_PER_UNIT, maxalign);
9962 return false;
9963 }
79ce98bc 9964 return true;
35886f0b
MS
9965 }
9966
83322951 9967 case BUILT_IN_CONSTANT_P:
79ce98bc 9968 return builtin_function_validate_nargs (loc, fndecl, nargs, 1);
83322951
RG
9969
9970 case BUILT_IN_ISFINITE:
9971 case BUILT_IN_ISINF:
05f41289 9972 case BUILT_IN_ISINF_SIGN:
83322951
RG
9973 case BUILT_IN_ISNAN:
9974 case BUILT_IN_ISNORMAL:
61717a45 9975 case BUILT_IN_SIGNBIT:
79ce98bc 9976 if (builtin_function_validate_nargs (loc, fndecl, nargs, 1))
83322951
RG
9977 {
9978 if (TREE_CODE (TREE_TYPE (args[0])) != REAL_TYPE)
9979 {
79ce98bc
MP
9980 error_at (ARG_LOCATION (0), "non-floating-point argument in "
9981 "call to function %qE", fndecl);
83322951
RG
9982 return false;
9983 }
9984 return true;
9985 }
9986 return false;
9987
9988 case BUILT_IN_ISGREATER:
9989 case BUILT_IN_ISGREATEREQUAL:
9990 case BUILT_IN_ISLESS:
9991 case BUILT_IN_ISLESSEQUAL:
9992 case BUILT_IN_ISLESSGREATER:
9993 case BUILT_IN_ISUNORDERED:
79ce98bc 9994 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2))
83322951
RG
9995 {
9996 enum tree_code code0, code1;
9997 code0 = TREE_CODE (TREE_TYPE (args[0]));
9998 code1 = TREE_CODE (TREE_TYPE (args[1]));
9999 if (!((code0 == REAL_TYPE && code1 == REAL_TYPE)
10000 || (code0 == REAL_TYPE && code1 == INTEGER_TYPE)
10001 || (code0 == INTEGER_TYPE && code1 == REAL_TYPE)))
10002 {
79ce98bc
MP
10003 error_at (loc, "non-floating-point arguments in call to "
10004 "function %qE", fndecl);
83322951
RG
10005 return false;
10006 }
10007 return true;
10008 }
10009 return false;
10010
3bf5906b 10011 case BUILT_IN_FPCLASSIFY:
79ce98bc 10012 if (builtin_function_validate_nargs (loc, fndecl, nargs, 6))
3bf5906b 10013 {
79ce98bc 10014 for (unsigned int i = 0; i < 5; i++)
3bf5906b
KG
10015 if (TREE_CODE (args[i]) != INTEGER_CST)
10016 {
79ce98bc
MP
10017 error_at (ARG_LOCATION (i), "non-const integer argument %u in "
10018 "call to function %qE", i + 1, fndecl);
3bf5906b
KG
10019 return false;
10020 }
10021
10022 if (TREE_CODE (TREE_TYPE (args[5])) != REAL_TYPE)
10023 {
79ce98bc
MP
10024 error_at (ARG_LOCATION (5), "non-floating-point argument in "
10025 "call to function %qE", fndecl);
3bf5906b
KG
10026 return false;
10027 }
10028 return true;
10029 }
10030 return false;
10031
45d439ac 10032 case BUILT_IN_ASSUME_ALIGNED:
79ce98bc 10033 if (builtin_function_validate_nargs (loc, fndecl, nargs, 2 + (nargs > 2)))
45d439ac
JJ
10034 {
10035 if (nargs >= 3 && TREE_CODE (TREE_TYPE (args[2])) != INTEGER_TYPE)
10036 {
79ce98bc
MP
10037 error_at (ARG_LOCATION (2), "non-integer argument 3 in call to "
10038 "function %qE", fndecl);
45d439ac
JJ
10039 return false;
10040 }
10041 return true;
10042 }
10043 return false;
10044
1304953e
JJ
10045 case BUILT_IN_ADD_OVERFLOW:
10046 case BUILT_IN_SUB_OVERFLOW:
10047 case BUILT_IN_MUL_OVERFLOW:
79ce98bc 10048 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
1304953e
JJ
10049 {
10050 unsigned i;
10051 for (i = 0; i < 2; i++)
10052 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
10053 {
79ce98bc
MP
10054 error_at (ARG_LOCATION (i), "argument %u in call to function "
10055 "%qE does not have integral type", i + 1, fndecl);
1304953e
JJ
10056 return false;
10057 }
10058 if (TREE_CODE (TREE_TYPE (args[2])) != POINTER_TYPE
a86451b9 10059 || !INTEGRAL_TYPE_P (TREE_TYPE (TREE_TYPE (args[2]))))
1304953e 10060 {
79ce98bc 10061 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
a86451b9
JJ
10062 "does not have pointer to integral type", fndecl);
10063 return false;
10064 }
10065 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == ENUMERAL_TYPE)
10066 {
10067 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
10068 "has pointer to enumerated type", fndecl);
10069 return false;
10070 }
10071 else if (TREE_CODE (TREE_TYPE (TREE_TYPE (args[2]))) == BOOLEAN_TYPE)
10072 {
10073 error_at (ARG_LOCATION (2), "argument 3 in call to function %qE "
10074 "has pointer to boolean type", fndecl);
1304953e
JJ
10075 return false;
10076 }
10077 return true;
10078 }
10079 return false;
10080
44a845ca
MS
10081 case BUILT_IN_ADD_OVERFLOW_P:
10082 case BUILT_IN_SUB_OVERFLOW_P:
10083 case BUILT_IN_MUL_OVERFLOW_P:
10084 if (builtin_function_validate_nargs (loc, fndecl, nargs, 3))
10085 {
10086 unsigned i;
10087 for (i = 0; i < 3; i++)
10088 if (!INTEGRAL_TYPE_P (TREE_TYPE (args[i])))
10089 {
10090 error_at (ARG_LOCATION (i), "argument %u in call to function "
10091 "%qE does not have integral type", i + 1, fndecl);
10092 return false;
10093 }
a86451b9
JJ
10094 if (TREE_CODE (TREE_TYPE (args[2])) == ENUMERAL_TYPE)
10095 {
10096 error_at (ARG_LOCATION (2), "argument 3 in call to function "
10097 "%qE has enumerated type", fndecl);
10098 return false;
10099 }
10100 else if (TREE_CODE (TREE_TYPE (args[2])) == BOOLEAN_TYPE)
10101 {
10102 error_at (ARG_LOCATION (2), "argument 3 in call to function "
10103 "%qE has boolean type", fndecl);
10104 return false;
10105 }
44a845ca
MS
10106 return true;
10107 }
10108 return false;
10109
83322951
RG
10110 default:
10111 return true;
10112 }
10113}
10114
d07605f5
AP
10115/* Function to help qsort sort FIELD_DECLs by name order. */
10116
10117int
10118field_decl_cmp (const void *x_p, const void *y_p)
10119{
28dab132
BI
10120 const tree *const x = (const tree *const) x_p;
10121 const tree *const y = (const tree *const) y_p;
10122
d07605f5
AP
10123 if (DECL_NAME (*x) == DECL_NAME (*y))
10124 /* A nontype is "greater" than a type. */
10125 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10126 if (DECL_NAME (*x) == NULL_TREE)
10127 return -1;
10128 if (DECL_NAME (*y) == NULL_TREE)
10129 return 1;
10130 if (DECL_NAME (*x) < DECL_NAME (*y))
10131 return -1;
10132 return 1;
10133}
10134
10135static struct {
10136 gt_pointer_operator new_value;
10137 void *cookie;
10138} resort_data;
10139
10140/* This routine compares two fields like field_decl_cmp but using the
10141pointer operator in resort_data. */
10142
10143static int
10144resort_field_decl_cmp (const void *x_p, const void *y_p)
10145{
28dab132
BI
10146 const tree *const x = (const tree *const) x_p;
10147 const tree *const y = (const tree *const) y_p;
d07605f5
AP
10148
10149 if (DECL_NAME (*x) == DECL_NAME (*y))
10150 /* A nontype is "greater" than a type. */
10151 return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
10152 if (DECL_NAME (*x) == NULL_TREE)
10153 return -1;
10154 if (DECL_NAME (*y) == NULL_TREE)
10155 return 1;
10156 {
10157 tree d1 = DECL_NAME (*x);
10158 tree d2 = DECL_NAME (*y);
10159 resort_data.new_value (&d1, resort_data.cookie);
10160 resort_data.new_value (&d2, resort_data.cookie);
10161 if (d1 < d2)
10162 return -1;
10163 }
10164 return 1;
10165}
10166
10167/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
10168
10169void
10170resort_sorted_fields (void *obj,
e18476eb 10171 void * ARG_UNUSED (orig_obj),
6de9cd9a
DN
10172 gt_pointer_operator new_value,
10173 void *cookie)
d07605f5 10174{
e18476eb 10175 struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
d07605f5
AP
10176 resort_data.new_value = new_value;
10177 resort_data.cookie = cookie;
10178 qsort (&sf->elts[0], sf->len, sizeof (tree),
6de9cd9a 10179 resort_field_decl_cmp);
d07605f5
AP
10180}
10181
0a3ee0fd
GDR
10182/* Subroutine of c_parse_error.
10183 Return the result of concatenating LHS and RHS. RHS is really
10184 a string literal, its first character is indicated by RHS_START and
3292fb42 10185 RHS_SIZE is its length (including the terminating NUL character).
0a3ee0fd
GDR
10186
10187 The caller is responsible for deleting the returned pointer. */
10188
10189static char *
10190catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
10191{
10192 const int lhs_size = strlen (lhs);
10193 char *result = XNEWVEC (char, lhs_size + rhs_size);
10194 strncpy (result, lhs, lhs_size);
10195 strncpy (result + lhs_size, rhs_start, rhs_size);
10196 return result;
10197}
10198
4b794eaf 10199/* Issue the error given by GMSGID, indicating that it occurred before
4bb8ca28
MM
10200 TOKEN, which had the associated VALUE. */
10201
10202void
b8698a0f 10203c_parse_error (const char *gmsgid, enum cpp_ttype token_type,
cfc93532 10204 tree value, unsigned char token_flags)
4bb8ca28 10205{
0a3ee0fd
GDR
10206#define catenate_messages(M1, M2) catenate_strings ((M1), (M2), sizeof (M2))
10207
10208 char *message = NULL;
4bb8ca28 10209
cfc93532 10210 if (token_type == CPP_EOF)
4b794eaf 10211 message = catenate_messages (gmsgid, " at end of input");
b8698a0f
L
10212 else if (token_type == CPP_CHAR
10213 || token_type == CPP_WCHAR
cfc93532 10214 || token_type == CPP_CHAR16
fe95b036
ESR
10215 || token_type == CPP_CHAR32
10216 || token_type == CPP_UTF8CHAR)
4bb8ca28
MM
10217 {
10218 unsigned int val = TREE_INT_CST_LOW (value);
b6baa67d
KVH
10219 const char *prefix;
10220
cfc93532 10221 switch (token_type)
b6baa67d
KVH
10222 {
10223 default:
10224 prefix = "";
10225 break;
10226 case CPP_WCHAR:
10227 prefix = "L";
10228 break;
10229 case CPP_CHAR16:
10230 prefix = "u";
10231 break;
10232 case CPP_CHAR32:
10233 prefix = "U";
10234 break;
fe95b036
ESR
10235 case CPP_UTF8CHAR:
10236 prefix = "u8";
10237 break;
b6baa67d
KVH
10238 }
10239
4bb8ca28 10240 if (val <= UCHAR_MAX && ISGRAPH (val))
c22cacf3 10241 message = catenate_messages (gmsgid, " before %s'%c'");
4bb8ca28 10242 else
c22cacf3 10243 message = catenate_messages (gmsgid, " before %s'\\x%x'");
0a3ee0fd 10244
b6baa67d 10245 error (message, prefix, val);
0a3ee0fd
GDR
10246 free (message);
10247 message = NULL;
4bb8ca28 10248 }
65e5a578
ESR
10249 else if (token_type == CPP_CHAR_USERDEF
10250 || token_type == CPP_WCHAR_USERDEF
10251 || token_type == CPP_CHAR16_USERDEF
fe95b036
ESR
10252 || token_type == CPP_CHAR32_USERDEF
10253 || token_type == CPP_UTF8CHAR_USERDEF)
65e5a578
ESR
10254 message = catenate_messages (gmsgid,
10255 " before user-defined character literal");
10256 else if (token_type == CPP_STRING_USERDEF
10257 || token_type == CPP_WSTRING_USERDEF
10258 || token_type == CPP_STRING16_USERDEF
10259 || token_type == CPP_STRING32_USERDEF
10260 || token_type == CPP_UTF8STRING_USERDEF)
10261 message = catenate_messages (gmsgid, " before user-defined string literal");
b8698a0f
L
10262 else if (token_type == CPP_STRING
10263 || token_type == CPP_WSTRING
cfc93532 10264 || token_type == CPP_STRING16
2c6e3f55
JJ
10265 || token_type == CPP_STRING32
10266 || token_type == CPP_UTF8STRING)
4b794eaf 10267 message = catenate_messages (gmsgid, " before string constant");
cfc93532 10268 else if (token_type == CPP_NUMBER)
4b794eaf 10269 message = catenate_messages (gmsgid, " before numeric constant");
cfc93532 10270 else if (token_type == CPP_NAME)
0a3ee0fd 10271 {
4b794eaf 10272 message = catenate_messages (gmsgid, " before %qE");
c51a1ba9 10273 error (message, value);
0a3ee0fd
GDR
10274 free (message);
10275 message = NULL;
10276 }
cfc93532 10277 else if (token_type == CPP_PRAGMA)
bc4071dd 10278 message = catenate_messages (gmsgid, " before %<#pragma%>");
cfc93532 10279 else if (token_type == CPP_PRAGMA_EOL)
bc4071dd 10280 message = catenate_messages (gmsgid, " before end of line");
34429675
JM
10281 else if (token_type == CPP_DECLTYPE)
10282 message = catenate_messages (gmsgid, " before %<decltype%>");
cfc93532 10283 else if (token_type < N_TTYPES)
0a3ee0fd 10284 {
4b794eaf 10285 message = catenate_messages (gmsgid, " before %qs token");
cfc93532 10286 error (message, cpp_type2name (token_type, token_flags));
0a3ee0fd
GDR
10287 free (message);
10288 message = NULL;
10289 }
4bb8ca28 10290 else
4b794eaf 10291 error (gmsgid);
0a3ee0fd
GDR
10292
10293 if (message)
10294 {
10295 error (message);
10296 free (message);
10297 }
c22cacf3 10298#undef catenate_messages
4bb8ca28
MM
10299}
10300
87cf0651
SB
10301/* Return the gcc option code associated with the reason for a cpp
10302 message, or 0 if none. */
10303
10304static int
10305c_option_controlling_cpp_error (int reason)
10306{
b559c810 10307 const struct cpp_reason_option_codes_t *entry;
87cf0651 10308
b559c810 10309 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
87cf0651
SB
10310 {
10311 if (entry->reason == reason)
10312 return entry->option_code;
10313 }
10314 return 0;
10315}
10316
148e4216 10317/* Callback from cpp_error for PFILE to print diagnostics from the
87cf0651
SB
10318 preprocessor. The diagnostic is of type LEVEL, with REASON set
10319 to the reason code if LEVEL is represents a warning, at location
8a645150
DM
10320 RICHLOC unless this is after lexing and the compiler's location
10321 should be used instead; MSG is the translated message and AP
148e4216
JM
10322 the arguments. Returns true if a diagnostic was emitted, false
10323 otherwise. */
10324
10325bool
87cf0651 10326c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason,
8a645150 10327 rich_location *richloc,
148e4216
JM
10328 const char *msg, va_list *ap)
10329{
10330 diagnostic_info diagnostic;
10331 diagnostic_t dlevel;
e3339d0f 10332 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
148e4216
JM
10333 bool ret;
10334
10335 switch (level)
10336 {
10337 case CPP_DL_WARNING_SYSHDR:
10338 if (flag_no_output)
10339 return false;
e3339d0f 10340 global_dc->dc_warn_system_headers = 1;
148e4216
JM
10341 /* Fall through. */
10342 case CPP_DL_WARNING:
10343 if (flag_no_output)
10344 return false;
10345 dlevel = DK_WARNING;
10346 break;
10347 case CPP_DL_PEDWARN:
10348 if (flag_no_output && !flag_pedantic_errors)
10349 return false;
10350 dlevel = DK_PEDWARN;
10351 break;
10352 case CPP_DL_ERROR:
10353 dlevel = DK_ERROR;
10354 break;
10355 case CPP_DL_ICE:
10356 dlevel = DK_ICE;
10357 break;
10358 case CPP_DL_NOTE:
10359 dlevel = DK_NOTE;
10360 break;
47580d22
JM
10361 case CPP_DL_FATAL:
10362 dlevel = DK_FATAL;
10363 break;
148e4216
JM
10364 default:
10365 gcc_unreachable ();
10366 }
10367 if (done_lexing)
f79520bb 10368 richloc->set_range (line_table, 0, input_location, true);
148e4216 10369 diagnostic_set_info_translated (&diagnostic, msg, ap,
8a645150 10370 richloc, dlevel);
87cf0651
SB
10371 diagnostic_override_option_index (&diagnostic,
10372 c_option_controlling_cpp_error (reason));
148e4216
JM
10373 ret = report_diagnostic (&diagnostic);
10374 if (level == CPP_DL_WARNING_SYSHDR)
e3339d0f 10375 global_dc->dc_warn_system_headers = save_warn_system_headers;
148e4216
JM
10376 return ret;
10377}
10378
c5ff069d
ZW
10379/* Convert a character from the host to the target execution character
10380 set. cpplib handles this, mostly. */
10381
10382HOST_WIDE_INT
10383c_common_to_target_charset (HOST_WIDE_INT c)
10384{
10385 /* Character constants in GCC proper are sign-extended under -fsigned-char,
10386 zero-extended under -fno-signed-char. cpplib insists that characters
10387 and character constants are always unsigned. Hence we must convert
10388 back and forth. */
10389 cppchar_t uc = ((cppchar_t)c) & ((((cppchar_t)1) << CHAR_BIT)-1);
10390
10391 uc = cpp_host_to_exec_charset (parse_in, uc);
10392
10393 if (flag_signed_char)
10394 return ((HOST_WIDE_INT)uc) << (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE)
10395 >> (HOST_BITS_PER_WIDE_INT - CHAR_TYPE_SIZE);
10396 else
10397 return uc;
10398}
10399
cf9e9959
EB
10400/* Fold an offsetof-like expression. EXPR is a nested sequence of component
10401 references with an INDIRECT_REF of a constant at the bottom; much like the
10402 traditional rendering of offsetof as a macro. Return the folded result. */
ee8a6a3e 10403
cf9e9959 10404tree
c85158de 10405fold_offsetof_1 (tree expr, enum tree_code ctx)
ee8a6a3e 10406{
ee8a6a3e 10407 tree base, off, t;
c85158de
MS
10408 tree_code code = TREE_CODE (expr);
10409 switch (code)
ee8a6a3e
RH
10410 {
10411 case ERROR_MARK:
10412 return expr;
10413
545b7d8c
VR
10414 case VAR_DECL:
10415 error ("cannot apply %<offsetof%> to static data member %qD", expr);
10416 return error_mark_node;
10417
6d4d7b0e 10418 case CALL_EXPR:
8d5f60ac 10419 case TARGET_EXPR:
6d4d7b0e
PB
10420 error ("cannot apply %<offsetof%> when %<operator[]%> is overloaded");
10421 return error_mark_node;
10422
6d4d7b0e
PB
10423 case NOP_EXPR:
10424 case INDIRECT_REF:
cf9e9959 10425 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
61c3c490
DS
10426 {
10427 error ("cannot apply %<offsetof%> to a non constant address");
10428 return error_mark_node;
10429 }
cf9e9959 10430 return TREE_OPERAND (expr, 0);
6d4d7b0e 10431
ee8a6a3e 10432 case COMPONENT_REF:
c85158de 10433 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
ee8a6a3e
RH
10434 if (base == error_mark_node)
10435 return base;
10436
10437 t = TREE_OPERAND (expr, 1);
10438 if (DECL_C_BIT_FIELD (t))
10439 {
10440 error ("attempt to take address of bit-field structure "
c51a1ba9 10441 "member %qD", t);
ee8a6a3e
RH
10442 return error_mark_node;
10443 }
db3927fb 10444 off = size_binop_loc (input_location, PLUS_EXPR, DECL_FIELD_OFFSET (t),
386b1f1f 10445 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
db3927fb 10446 / BITS_PER_UNIT));
ee8a6a3e
RH
10447 break;
10448
10449 case ARRAY_REF:
c85158de 10450 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code);
ee8a6a3e
RH
10451 if (base == error_mark_node)
10452 return base;
10453
10454 t = TREE_OPERAND (expr, 1);
61c3c490
DS
10455
10456 /* Check if the offset goes beyond the upper bound of the array. */
cf9e9959 10457 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) >= 0)
d32599a6
JJ
10458 {
10459 tree upbound = array_ref_up_bound (expr);
10460 if (upbound != NULL_TREE
10461 && TREE_CODE (upbound) == INTEGER_CST
10462 && !tree_int_cst_equal (upbound,
10463 TYPE_MAX_VALUE (TREE_TYPE (upbound))))
10464 {
c85158de
MS
10465 if (ctx != ARRAY_REF && ctx != COMPONENT_REF)
10466 upbound = size_binop (PLUS_EXPR, upbound,
10467 build_int_cst (TREE_TYPE (upbound), 1));
d32599a6
JJ
10468 if (tree_int_cst_lt (upbound, t))
10469 {
10470 tree v;
10471
10472 for (v = TREE_OPERAND (expr, 0);
10473 TREE_CODE (v) == COMPONENT_REF;
10474 v = TREE_OPERAND (v, 0))
10475 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
10476 == RECORD_TYPE)
10477 {
910ad8de
NF
10478 tree fld_chain = DECL_CHAIN (TREE_OPERAND (v, 1));
10479 for (; fld_chain; fld_chain = DECL_CHAIN (fld_chain))
d32599a6
JJ
10480 if (TREE_CODE (fld_chain) == FIELD_DECL)
10481 break;
10482
10483 if (fld_chain)
10484 break;
10485 }
10486 /* Don't warn if the array might be considered a poor
10487 man's flexible array member with a very permissive
10488 definition thereof. */
10489 if (TREE_CODE (v) == ARRAY_REF
10490 || TREE_CODE (v) == COMPONENT_REF)
10491 warning (OPT_Warray_bounds,
10492 "index %E denotes an offset "
10493 "greater than size of %qT",
10494 t, TREE_TYPE (TREE_OPERAND (expr, 0)));
10495 }
10496 }
10497 }
cf9e9959
EB
10498
10499 t = convert (sizetype, t);
10500 off = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (TREE_TYPE (expr)), t);
ee8a6a3e
RH
10501 break;
10502
1916c916
VR
10503 case COMPOUND_EXPR:
10504 /* Handle static members of volatile structs. */
10505 t = TREE_OPERAND (expr, 1);
0ae9bd27 10506 gcc_assert (VAR_P (t));
cf9e9959 10507 return fold_offsetof_1 (t);
1916c916 10508
ee8a6a3e 10509 default:
366de0ce 10510 gcc_unreachable ();
ee8a6a3e
RH
10511 }
10512
cf9e9959 10513 return fold_build_pointer_plus (base, off);
ee8a6a3e
RH
10514}
10515
cf9e9959
EB
10516/* Likewise, but convert it to the return type of offsetof. */
10517
ee8a6a3e 10518tree
cf9e9959 10519fold_offsetof (tree expr)
ee8a6a3e 10520{
cf9e9959 10521 return convert (size_type_node, fold_offsetof_1 (expr));
ee8a6a3e
RH
10522}
10523
d166d4c3
AK
10524/* Warn for A ?: C expressions (with B omitted) where A is a boolean
10525 expression, because B will always be true. */
10526
10527void
10528warn_for_omitted_condop (location_t location, tree cond)
10529{
10530 if (truth_value_p (TREE_CODE (cond)))
10531 warning_at (location, OPT_Wparentheses,
10532 "the omitted middle operand in ?: will always be %<true%>, "
10533 "suggest explicit middle operand");
10534}
10535
4816c593
NF
10536/* Give an error for storing into ARG, which is 'const'. USE indicates
10537 how ARG was being used. */
10538
10539void
c02065fc 10540readonly_error (location_t loc, tree arg, enum lvalue_use use)
4816c593
NF
10541{
10542 gcc_assert (use == lv_assign || use == lv_increment || use == lv_decrement
10543 || use == lv_asm);
10544 /* Using this macro rather than (for example) arrays of messages
10545 ensures that all the format strings are checked at compile
10546 time. */
10547#define READONLY_MSG(A, I, D, AS) (use == lv_assign ? (A) \
10548 : (use == lv_increment ? (I) \
10549 : (use == lv_decrement ? (D) : (AS))))
10550 if (TREE_CODE (arg) == COMPONENT_REF)
10551 {
10552 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
c02065fc
AH
10553 error_at (loc, READONLY_MSG (G_("assignment of member "
10554 "%qD in read-only object"),
10555 G_("increment of member "
10556 "%qD in read-only object"),
10557 G_("decrement of member "
10558 "%qD in read-only object"),
10559 G_("member %qD in read-only object "
10560 "used as %<asm%> output")),
10561 TREE_OPERAND (arg, 1));
4816c593 10562 else
c02065fc
AH
10563 error_at (loc, READONLY_MSG (G_("assignment of read-only member %qD"),
10564 G_("increment of read-only member %qD"),
10565 G_("decrement of read-only member %qD"),
10566 G_("read-only member %qD used as %<asm%> output")),
10567 TREE_OPERAND (arg, 1));
4816c593 10568 }
0ae9bd27 10569 else if (VAR_P (arg))
c02065fc
AH
10570 error_at (loc, READONLY_MSG (G_("assignment of read-only variable %qD"),
10571 G_("increment of read-only variable %qD"),
10572 G_("decrement of read-only variable %qD"),
10573 G_("read-only variable %qD used as %<asm%> output")),
10574 arg);
4816c593 10575 else if (TREE_CODE (arg) == PARM_DECL)
c02065fc
AH
10576 error_at (loc, READONLY_MSG (G_("assignment of read-only parameter %qD"),
10577 G_("increment of read-only parameter %qD"),
10578 G_("decrement of read-only parameter %qD"),
10579 G_("read-only parameter %qD use as %<asm%> output")),
10580 arg);
4816c593
NF
10581 else if (TREE_CODE (arg) == RESULT_DECL)
10582 {
10583 gcc_assert (c_dialect_cxx ());
c02065fc
AH
10584 error_at (loc, READONLY_MSG (G_("assignment of "
10585 "read-only named return value %qD"),
10586 G_("increment of "
10587 "read-only named return value %qD"),
10588 G_("decrement of "
10589 "read-only named return value %qD"),
10590 G_("read-only named return value %qD "
10591 "used as %<asm%>output")),
10592 arg);
4816c593
NF
10593 }
10594 else if (TREE_CODE (arg) == FUNCTION_DECL)
c02065fc
AH
10595 error_at (loc, READONLY_MSG (G_("assignment of function %qD"),
10596 G_("increment of function %qD"),
10597 G_("decrement of function %qD"),
10598 G_("function %qD used as %<asm%> output")),
10599 arg);
4816c593 10600 else
c02065fc
AH
10601 error_at (loc, READONLY_MSG (G_("assignment of read-only location %qE"),
10602 G_("increment of read-only location %qE"),
10603 G_("decrement of read-only location %qE"),
10604 G_("read-only location %qE used as %<asm%> output")),
10605 arg);
4816c593
NF
10606}
10607
37dc0d8d 10608/* Print an error message for an invalid lvalue. USE says
7bd11157
TT
10609 how the lvalue is being used and so selects the error message. LOC
10610 is the location for the error. */
5ae9ba3e 10611
37dc0d8d 10612void
7bd11157 10613lvalue_error (location_t loc, enum lvalue_use use)
5ae9ba3e 10614{
37dc0d8d 10615 switch (use)
5ae9ba3e 10616 {
37dc0d8d 10617 case lv_assign:
7bd11157 10618 error_at (loc, "lvalue required as left operand of assignment");
37dc0d8d
JM
10619 break;
10620 case lv_increment:
7bd11157 10621 error_at (loc, "lvalue required as increment operand");
37dc0d8d
JM
10622 break;
10623 case lv_decrement:
7bd11157 10624 error_at (loc, "lvalue required as decrement operand");
37dc0d8d
JM
10625 break;
10626 case lv_addressof:
7bd11157 10627 error_at (loc, "lvalue required as unary %<&%> operand");
37dc0d8d
JM
10628 break;
10629 case lv_asm:
7bd11157 10630 error_at (loc, "lvalue required in asm statement");
37dc0d8d
JM
10631 break;
10632 default:
10633 gcc_unreachable ();
5ae9ba3e 10634 }
5ae9ba3e 10635}
7a6daeb0
NF
10636
10637/* Print an error message for an invalid indirection of type TYPE.
10638 ERRSTRING is the name of the operator for the indirection. */
10639
10640void
10641invalid_indirection_error (location_t loc, tree type, ref_operator errstring)
10642{
10643 switch (errstring)
10644 {
10645 case RO_NULL:
10646 gcc_assert (c_dialect_cxx ());
10647 error_at (loc, "invalid type argument (have %qT)", type);
10648 break;
10649 case RO_ARRAY_INDEXING:
10650 error_at (loc,
10651 "invalid type argument of array indexing (have %qT)",
10652 type);
10653 break;
10654 case RO_UNARY_STAR:
10655 error_at (loc,
10656 "invalid type argument of unary %<*%> (have %qT)",
10657 type);
10658 break;
10659 case RO_ARROW:
10660 error_at (loc,
10661 "invalid type argument of %<->%> (have %qT)",
10662 type);
10663 break;
55a7f02f
MP
10664 case RO_ARROW_STAR:
10665 error_at (loc,
10666 "invalid type argument of %<->*%> (have %qT)",
10667 type);
10668 break;
7a6daeb0
NF
10669 case RO_IMPLICIT_CONVERSION:
10670 error_at (loc,
10671 "invalid type argument of implicit conversion (have %qT)",
10672 type);
10673 break;
10674 default:
10675 gcc_unreachable ();
10676 }
10677}
aab038d5
RH
10678\f
10679/* *PTYPE is an incomplete array. Complete it with a domain based on
10680 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
10681 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
10682 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
10683
10684int
10685complete_array_type (tree *ptype, tree initial_value, bool do_default)
10686{
10687 tree maxindex, type, main_type, elt, unqual_elt;
10688 int failure = 0, quals;
06d40de8 10689 hashval_t hashcode = 0;
40d3d530 10690 bool overflow_p = false;
aab038d5
RH
10691
10692 maxindex = size_zero_node;
10693 if (initial_value)
10694 {
10695 if (TREE_CODE (initial_value) == STRING_CST)
10696 {
10697 int eltsize
10698 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
10699 maxindex = size_int (TREE_STRING_LENGTH (initial_value)/eltsize - 1);
10700 }
10701 else if (TREE_CODE (initial_value) == CONSTRUCTOR)
10702 {
9771b263 10703 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
aab038d5 10704
9771b263 10705 if (vec_safe_is_empty (v))
aab038d5
RH
10706 {
10707 if (pedantic)
10708 failure = 3;
830c740f 10709 maxindex = ssize_int (-1);
aab038d5
RH
10710 }
10711 else
10712 {
10713 tree curindex;
4038c495
GB
10714 unsigned HOST_WIDE_INT cnt;
10715 constructor_elt *ce;
cff7525f 10716 bool fold_p = false;
aab038d5 10717
9771b263 10718 if ((*v)[0].index)
40d3d530
JR
10719 maxindex = (*v)[0].index, fold_p = true;
10720
aab038d5
RH
10721 curindex = maxindex;
10722
9771b263 10723 for (cnt = 1; vec_safe_iterate (v, cnt, &ce); cnt++)
aab038d5 10724 {
cff7525f 10725 bool curfold_p = false;
4038c495 10726 if (ce->index)
cff7525f 10727 curindex = ce->index, curfold_p = true;
aab038d5 10728 else
cff7525f
JH
10729 {
10730 if (fold_p)
40d3d530
JR
10731 {
10732 /* Since we treat size types now as ordinary
10733 unsigned types, we need an explicit overflow
10734 check. */
10735 tree orig = curindex;
10736 curindex = fold_convert (sizetype, curindex);
10737 overflow_p |= tree_int_cst_lt (curindex, orig);
10738 }
db3927fb
AH
10739 curindex = size_binop (PLUS_EXPR, curindex,
10740 size_one_node);
cff7525f 10741 }
aab038d5 10742 if (tree_int_cst_lt (maxindex, curindex))
cff7525f 10743 maxindex = curindex, fold_p = curfold_p;
aab038d5 10744 }
40d3d530
JR
10745 if (fold_p)
10746 {
10747 tree orig = maxindex;
10748 maxindex = fold_convert (sizetype, maxindex);
10749 overflow_p |= tree_int_cst_lt (maxindex, orig);
10750 }
aab038d5
RH
10751 }
10752 }
10753 else
10754 {
10755 /* Make an error message unless that happened already. */
10756 if (initial_value != error_mark_node)
10757 failure = 1;
10758 }
10759 }
10760 else
10761 {
10762 failure = 2;
10763 if (!do_default)
10764 return failure;
10765 }
10766
10767 type = *ptype;
10768 elt = TREE_TYPE (type);
10769 quals = TYPE_QUALS (strip_array_types (elt));
10770 if (quals == 0)
10771 unqual_elt = elt;
10772 else
36c5e70a 10773 unqual_elt = c_build_qualified_type (elt, KEEP_QUAL_ADDR_SPACE (quals));
aab038d5
RH
10774
10775 /* Using build_distinct_type_copy and modifying things afterward instead
10776 of using build_array_type to create a new type preserves all of the
10777 TYPE_LANG_FLAG_? bits that the front end may have set. */
10778 main_type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
10779 TREE_TYPE (main_type) = unqual_elt;
e6313a78
RG
10780 TYPE_DOMAIN (main_type)
10781 = build_range_type (TREE_TYPE (maxindex),
10782 build_int_cst (TREE_TYPE (maxindex), 0), maxindex);
aab038d5
RH
10783 layout_type (main_type);
10784
06d40de8
DG
10785 /* Make sure we have the canonical MAIN_TYPE. */
10786 hashcode = iterative_hash_object (TYPE_HASH (unqual_elt), hashcode);
b8698a0f 10787 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (main_type)),
06d40de8
DG
10788 hashcode);
10789 main_type = type_hash_canon (hashcode, main_type);
10790
9ae165a0
DG
10791 /* Fix the canonical type. */
10792 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (main_type))
10793 || TYPE_STRUCTURAL_EQUALITY_P (TYPE_DOMAIN (main_type)))
10794 SET_TYPE_STRUCTURAL_EQUALITY (main_type);
10795 else if (TYPE_CANONICAL (TREE_TYPE (main_type)) != TREE_TYPE (main_type)
10796 || (TYPE_CANONICAL (TYPE_DOMAIN (main_type))
10797 != TYPE_DOMAIN (main_type)))
b8698a0f 10798 TYPE_CANONICAL (main_type)
9ae165a0
DG
10799 = build_array_type (TYPE_CANONICAL (TREE_TYPE (main_type)),
10800 TYPE_CANONICAL (TYPE_DOMAIN (main_type)));
10801 else
10802 TYPE_CANONICAL (main_type) = main_type;
10803
aab038d5
RH
10804 if (quals == 0)
10805 type = main_type;
10806 else
10807 type = c_build_qualified_type (main_type, quals);
10808
7bfcb402
JM
10809 if (COMPLETE_TYPE_P (type)
10810 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
40d3d530 10811 && (overflow_p || TREE_OVERFLOW (TYPE_SIZE_UNIT (type))))
7bfcb402
JM
10812 {
10813 error ("size of array is too large");
10814 /* If we proceed with the array type as it is, we'll eventually
386b1f1f 10815 crash in tree_to_[su]hwi(). */
7bfcb402
JM
10816 type = error_mark_node;
10817 }
10818
aab038d5
RH
10819 *ptype = type;
10820 return failure;
10821}
5ae9ba3e 10822
30cd1c5d
AS
10823/* Like c_mark_addressable but don't check register qualifier. */
10824void
10825c_common_mark_addressable_vec (tree t)
10826{
10827 while (handled_component_p (t))
10828 t = TREE_OPERAND (t, 0);
0ae9bd27 10829 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
30cd1c5d
AS
10830 return;
10831 TREE_ADDRESSABLE (t) = 1;
10832}
10833
10834
48ae6c13
RH
10835\f
10836/* Used to help initialize the builtin-types.def table. When a type of
10837 the correct size doesn't exist, use error_mark_node instead of NULL.
10838 The later results in segfaults even when a decl using the type doesn't
10839 get invoked. */
10840
10841tree
10842builtin_type_for_size (int size, bool unsignedp)
10843{
21fa2faf 10844 tree type = c_common_type_for_size (size, unsignedp);
48ae6c13
RH
10845 return type ? type : error_mark_node;
10846}
10847
10848/* A helper function for resolve_overloaded_builtin in resolving the
10849 overloaded __sync_ builtins. Returns a positive power of 2 if the
10850 first operand of PARAMS is a pointer to a supported data type.
7a127fa7
MS
10851 Returns 0 if an error is encountered.
10852 FETCH is true when FUNCTION is one of the _FETCH_OP_ or _OP_FETCH_
10853 built-ins. */
48ae6c13
RH
10854
10855static int
7a127fa7 10856sync_resolve_size (tree function, vec<tree, va_gc> *params, bool fetch)
48ae6c13 10857{
7a127fa7
MS
10858 /* Type of the argument. */
10859 tree argtype;
10860 /* Type the argument points to. */
48ae6c13
RH
10861 tree type;
10862 int size;
10863
ba6b3795 10864 if (vec_safe_is_empty (params))
48ae6c13
RH
10865 {
10866 error ("too few arguments to function %qE", function);
10867 return 0;
10868 }
10869
7a127fa7 10870 argtype = type = TREE_TYPE ((*params)[0]);
6415bd5d
JM
10871 if (TREE_CODE (type) == ARRAY_TYPE)
10872 {
10873 /* Force array-to-pointer decay for C++. */
10874 gcc_assert (c_dialect_cxx());
10875 (*params)[0] = default_conversion ((*params)[0]);
10876 type = TREE_TYPE ((*params)[0]);
10877 }
48ae6c13
RH
10878 if (TREE_CODE (type) != POINTER_TYPE)
10879 goto incompatible;
10880
10881 type = TREE_TYPE (type);
10882 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10883 goto incompatible;
10884
7a127fa7
MS
10885 if (fetch && TREE_CODE (type) == BOOLEAN_TYPE)
10886 goto incompatible;
10887
ae7e9ddd 10888 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
a0274e3e 10889 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
48ae6c13
RH
10890 return size;
10891
10892 incompatible:
9f04a53e
MS
10893 /* Issue the diagnostic only if the argument is valid, otherwise
10894 it would be redundant at best and could be misleading. */
10895 if (argtype != error_mark_node)
10896 error ("operand type %qT is incompatible with argument %d of %qE",
10897 argtype, 1, function);
48ae6c13
RH
10898 return 0;
10899}
10900
c22cacf3 10901/* A helper function for resolve_overloaded_builtin. Adds casts to
48ae6c13
RH
10902 PARAMS to make arguments match up with those of FUNCTION. Drops
10903 the variadic arguments at the end. Returns false if some error
10904 was encountered; true on success. */
10905
10906static bool
86951993 10907sync_resolve_params (location_t loc, tree orig_function, tree function,
9771b263 10908 vec<tree, va_gc> *params, bool orig_format)
48ae6c13 10909{
e19a18d4 10910 function_args_iterator iter;
48ae6c13 10911 tree ptype;
bbbbb16a 10912 unsigned int parmnum;
48ae6c13 10913
e19a18d4 10914 function_args_iter_init (&iter, TREE_TYPE (function));
48ae6c13
RH
10915 /* We've declared the implementation functions to use "volatile void *"
10916 as the pointer parameter, so we shouldn't get any complaints from the
10917 call to check_function_arguments what ever type the user used. */
e19a18d4 10918 function_args_iter_next (&iter);
9771b263 10919 ptype = TREE_TYPE (TREE_TYPE ((*params)[0]));
267bac10 10920 ptype = TYPE_MAIN_VARIANT (ptype);
48ae6c13
RH
10921
10922 /* For the rest of the values, we need to cast these to FTYPE, so that we
10923 don't get warnings for passing pointer types, etc. */
bbbbb16a 10924 parmnum = 0;
e19a18d4 10925 while (1)
48ae6c13 10926 {
e19a18d4
NF
10927 tree val, arg_type;
10928
10929 arg_type = function_args_iter_cond (&iter);
10930 /* XXX void_type_node belies the abstraction. */
10931 if (arg_type == void_type_node)
10932 break;
48ae6c13 10933
bbbbb16a 10934 ++parmnum;
9771b263 10935 if (params->length () <= parmnum)
48ae6c13 10936 {
86951993 10937 error_at (loc, "too few arguments to function %qE", orig_function);
48ae6c13
RH
10938 return false;
10939 }
10940
e3793c6f
JJ
10941 /* Only convert parameters if arg_type is unsigned integer type with
10942 new format sync routines, i.e. don't attempt to convert pointer
10943 arguments (e.g. EXPECTED argument of __atomic_compare_exchange_n),
10944 bool arguments (e.g. WEAK argument) or signed int arguments (memmodel
10945 kinds). */
10946 if (TREE_CODE (arg_type) == INTEGER_TYPE && TYPE_UNSIGNED (arg_type))
86951993
AM
10947 {
10948 /* Ideally for the first conversion we'd use convert_for_assignment
10949 so that we get warnings for anything that doesn't match the pointer
10950 type. This isn't portable across the C and C++ front ends atm. */
9771b263 10951 val = (*params)[parmnum];
86951993
AM
10952 val = convert (ptype, val);
10953 val = convert (arg_type, val);
9771b263 10954 (*params)[parmnum] = val;
86951993 10955 }
48ae6c13 10956
e19a18d4 10957 function_args_iter_next (&iter);
48ae6c13
RH
10958 }
10959
86951993 10960 /* __atomic routines are not variadic. */
9771b263 10961 if (!orig_format && params->length () != parmnum + 1)
86951993
AM
10962 {
10963 error_at (loc, "too many arguments to function %qE", orig_function);
10964 return false;
10965 }
10966
48ae6c13
RH
10967 /* The definition of these primitives is variadic, with the remaining
10968 being "an optional list of variables protected by the memory barrier".
10969 No clue what that's supposed to mean, precisely, but we consider all
10970 call-clobbered variables to be protected so we're safe. */
9771b263 10971 params->truncate (parmnum + 1);
48ae6c13
RH
10972
10973 return true;
10974}
10975
c22cacf3 10976/* A helper function for resolve_overloaded_builtin. Adds a cast to
48ae6c13
RH
10977 RESULT to make it match the type of the first pointer argument in
10978 PARAMS. */
10979
10980static tree
86951993 10981sync_resolve_return (tree first_param, tree result, bool orig_format)
48ae6c13 10982{
bbbbb16a 10983 tree ptype = TREE_TYPE (TREE_TYPE (first_param));
86951993 10984 tree rtype = TREE_TYPE (result);
99db1ef0 10985 ptype = TYPE_MAIN_VARIANT (ptype);
86951993
AM
10986
10987 /* New format doesn't require casting unless the types are the same size. */
10988 if (orig_format || tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
10989 return convert (ptype, result);
10990 else
10991 return result;
10992}
10993
10994/* This function verifies the PARAMS to generic atomic FUNCTION.
10995 It returns the size if all the parameters are the same size, otherwise
10996 0 is returned if the parameters are invalid. */
10997
10998static int
9771b263
DN
10999get_atomic_generic_size (location_t loc, tree function,
11000 vec<tree, va_gc> *params)
86951993
AM
11001{
11002 unsigned int n_param;
11003 unsigned int n_model;
11004 unsigned int x;
11005 int size_0;
11006 tree type_0;
11007
11008 /* Determine the parameter makeup. */
11009 switch (DECL_FUNCTION_CODE (function))
11010 {
11011 case BUILT_IN_ATOMIC_EXCHANGE:
11012 n_param = 4;
11013 n_model = 1;
11014 break;
11015 case BUILT_IN_ATOMIC_LOAD:
11016 case BUILT_IN_ATOMIC_STORE:
11017 n_param = 3;
11018 n_model = 1;
11019 break;
11020 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11021 n_param = 6;
11022 n_model = 2;
11023 break;
11024 default:
c466c4ff 11025 gcc_unreachable ();
86951993
AM
11026 }
11027
9771b263 11028 if (vec_safe_length (params) != n_param)
86951993
AM
11029 {
11030 error_at (loc, "incorrect number of arguments to function %qE", function);
11031 return 0;
11032 }
11033
11034 /* Get type of first parameter, and determine its size. */
9771b263 11035 type_0 = TREE_TYPE ((*params)[0]);
6415bd5d
JM
11036 if (TREE_CODE (type_0) == ARRAY_TYPE)
11037 {
11038 /* Force array-to-pointer decay for C++. */
11039 gcc_assert (c_dialect_cxx());
11040 (*params)[0] = default_conversion ((*params)[0]);
11041 type_0 = TREE_TYPE ((*params)[0]);
11042 }
c466c4ff
AM
11043 if (TREE_CODE (type_0) != POINTER_TYPE || VOID_TYPE_P (TREE_TYPE (type_0)))
11044 {
11045 error_at (loc, "argument 1 of %qE must be a non-void pointer type",
11046 function);
11047 return 0;
11048 }
11049
11050 /* Types must be compile time constant sizes. */
11051 if (TREE_CODE ((TYPE_SIZE_UNIT (TREE_TYPE (type_0)))) != INTEGER_CST)
86951993 11052 {
c466c4ff
AM
11053 error_at (loc,
11054 "argument 1 of %qE must be a pointer to a constant size type",
11055 function);
86951993
AM
11056 return 0;
11057 }
c466c4ff 11058
ae7e9ddd 11059 size_0 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type_0)));
86951993 11060
c466c4ff
AM
11061 /* Zero size objects are not allowed. */
11062 if (size_0 == 0)
11063 {
11064 error_at (loc,
11065 "argument 1 of %qE must be a pointer to a nonzero size object",
11066 function);
11067 return 0;
11068 }
11069
86951993
AM
11070 /* Check each other parameter is a pointer and the same size. */
11071 for (x = 0; x < n_param - n_model; x++)
11072 {
11073 int size;
9771b263 11074 tree type = TREE_TYPE ((*params)[x]);
688010ba 11075 /* __atomic_compare_exchange has a bool in the 4th position, skip it. */
86951993
AM
11076 if (n_param == 6 && x == 3)
11077 continue;
11078 if (!POINTER_TYPE_P (type))
11079 {
11080 error_at (loc, "argument %d of %qE must be a pointer type", x + 1,
11081 function);
11082 return 0;
11083 }
7b56b2f8
MP
11084 tree type_size = TYPE_SIZE_UNIT (TREE_TYPE (type));
11085 size = type_size ? tree_to_uhwi (type_size) : 0;
86951993
AM
11086 if (size != size_0)
11087 {
11088 error_at (loc, "size mismatch in argument %d of %qE", x + 1,
11089 function);
11090 return 0;
11091 }
11092 }
11093
11094 /* Check memory model parameters for validity. */
11095 for (x = n_param - n_model ; x < n_param; x++)
11096 {
9771b263 11097 tree p = (*params)[x];
86951993
AM
11098 if (TREE_CODE (p) == INTEGER_CST)
11099 {
ae7e9ddd 11100 int i = tree_to_uhwi (p);
46b35980 11101 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST))
86951993
AM
11102 {
11103 warning_at (loc, OPT_Winvalid_memory_model,
11104 "invalid memory model argument %d of %qE", x + 1,
11105 function);
86951993
AM
11106 }
11107 }
11108 else
11109 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
11110 {
11111 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
11112 function);
11113 return 0;
11114 }
11115 }
11116
11117 return size_0;
11118}
11119
11120
11121/* This will take an __atomic_ generic FUNCTION call, and add a size parameter N
11122 at the beginning of the parameter list PARAMS representing the size of the
11123 objects. This is to match the library ABI requirement. LOC is the location
11124 of the function call.
11125 The new function is returned if it needed rebuilding, otherwise NULL_TREE is
11126 returned to allow the external call to be constructed. */
11127
11128static tree
11129add_atomic_size_parameter (unsigned n, location_t loc, tree function,
9771b263 11130 vec<tree, va_gc> *params)
86951993
AM
11131{
11132 tree size_node;
11133
11134 /* Insert a SIZE_T parameter as the first param. If there isn't
11135 enough space, allocate a new vector and recursively re-build with that. */
9771b263 11136 if (!params->space (1))
86951993
AM
11137 {
11138 unsigned int z, len;
9771b263 11139 vec<tree, va_gc> *v;
86951993
AM
11140 tree f;
11141
9771b263
DN
11142 len = params->length ();
11143 vec_alloc (v, len + 1);
8edbfaa6 11144 v->quick_push (build_int_cst (size_type_node, n));
86951993 11145 for (z = 0; z < len; z++)
9771b263 11146 v->quick_push ((*params)[z]);
81e5eca8 11147 f = build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 11148 vec_free (v);
86951993
AM
11149 return f;
11150 }
11151
11152 /* Add the size parameter and leave as a function call for processing. */
11153 size_node = build_int_cst (size_type_node, n);
9771b263 11154 params->quick_insert (0, size_node);
86951993
AM
11155 return NULL_TREE;
11156}
11157
11158
6b28e197
JM
11159/* Return whether atomic operations for naturally aligned N-byte
11160 arguments are supported, whether inline or through libatomic. */
11161static bool
11162atomic_size_supported_p (int n)
11163{
11164 switch (n)
11165 {
11166 case 1:
11167 case 2:
11168 case 4:
11169 case 8:
11170 return true;
11171
11172 case 16:
11173 return targetm.scalar_mode_supported_p (TImode);
11174
11175 default:
11176 return false;
11177 }
11178}
11179
86951993
AM
11180/* This will process an __atomic_exchange function call, determine whether it
11181 needs to be mapped to the _N variation, or turned into a library call.
11182 LOC is the location of the builtin call.
11183 FUNCTION is the DECL that has been invoked;
11184 PARAMS is the argument list for the call. The return value is non-null
11185 TRUE is returned if it is translated into the proper format for a call to the
11186 external library, and NEW_RETURN is set the tree for that function.
11187 FALSE is returned if processing for the _N variation is required, and
026c3cfd 11188 NEW_RETURN is set to the return value the result is copied into. */
86951993
AM
11189static bool
11190resolve_overloaded_atomic_exchange (location_t loc, tree function,
9771b263 11191 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
11192{
11193 tree p0, p1, p2, p3;
11194 tree I_type, I_type_ptr;
11195 int n = get_atomic_generic_size (loc, function, params);
11196
c466c4ff
AM
11197 /* Size of 0 is an error condition. */
11198 if (n == 0)
11199 {
11200 *new_return = error_mark_node;
11201 return true;
11202 }
11203
86951993 11204 /* If not a lock-free size, change to the library generic format. */
6b28e197 11205 if (!atomic_size_supported_p (n))
86951993
AM
11206 {
11207 *new_return = add_atomic_size_parameter (n, loc, function, params);
11208 return true;
11209 }
11210
11211 /* Otherwise there is a lockfree match, transform the call from:
11212 void fn(T* mem, T* desired, T* return, model)
11213 into
11214 *return = (T) (fn (In* mem, (In) *desired, model)) */
11215
9771b263
DN
11216 p0 = (*params)[0];
11217 p1 = (*params)[1];
11218 p2 = (*params)[2];
11219 p3 = (*params)[3];
86951993
AM
11220
11221 /* Create pointer to appropriate size. */
11222 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11223 I_type_ptr = build_pointer_type (I_type);
11224
11225 /* Convert object pointer to required type. */
11226 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11227 (*params)[0] = p0;
86951993
AM
11228 /* Convert new value to required type, and dereference it. */
11229 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11230 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9771b263 11231 (*params)[1] = p1;
86951993
AM
11232
11233 /* Move memory model to the 3rd position, and end param list. */
9771b263
DN
11234 (*params)[2] = p3;
11235 params->truncate (3);
86951993
AM
11236
11237 /* Convert return pointer and dereference it for later assignment. */
11238 *new_return = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11239
11240 return false;
48ae6c13
RH
11241}
11242
86951993
AM
11243
11244/* This will process an __atomic_compare_exchange function call, determine
11245 whether it needs to be mapped to the _N variation, or turned into a lib call.
11246 LOC is the location of the builtin call.
11247 FUNCTION is the DECL that has been invoked;
11248 PARAMS is the argument list for the call. The return value is non-null
11249 TRUE is returned if it is translated into the proper format for a call to the
11250 external library, and NEW_RETURN is set the tree for that function.
11251 FALSE is returned if processing for the _N variation is required. */
11252
11253static bool
11254resolve_overloaded_atomic_compare_exchange (location_t loc, tree function,
9771b263 11255 vec<tree, va_gc> *params,
86951993
AM
11256 tree *new_return)
11257{
11258 tree p0, p1, p2;
11259 tree I_type, I_type_ptr;
11260 int n = get_atomic_generic_size (loc, function, params);
11261
c466c4ff
AM
11262 /* Size of 0 is an error condition. */
11263 if (n == 0)
11264 {
11265 *new_return = error_mark_node;
11266 return true;
11267 }
11268
86951993 11269 /* If not a lock-free size, change to the library generic format. */
6b28e197 11270 if (!atomic_size_supported_p (n))
86951993
AM
11271 {
11272 /* The library generic format does not have the weak parameter, so
11273 remove it from the param list. Since a parameter has been removed,
11274 we can be sure that there is room for the SIZE_T parameter, meaning
11275 there will not be a recursive rebuilding of the parameter list, so
11276 there is no danger this will be done twice. */
11277 if (n > 0)
11278 {
9771b263
DN
11279 (*params)[3] = (*params)[4];
11280 (*params)[4] = (*params)[5];
11281 params->truncate (5);
86951993
AM
11282 }
11283 *new_return = add_atomic_size_parameter (n, loc, function, params);
11284 return true;
11285 }
11286
11287 /* Otherwise, there is a match, so the call needs to be transformed from:
11288 bool fn(T* mem, T* desired, T* return, weak, success, failure)
11289 into
11290 bool fn ((In *)mem, (In *)expected, (In) *desired, weak, succ, fail) */
11291
9771b263
DN
11292 p0 = (*params)[0];
11293 p1 = (*params)[1];
11294 p2 = (*params)[2];
86951993
AM
11295
11296 /* Create pointer to appropriate size. */
11297 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11298 I_type_ptr = build_pointer_type (I_type);
11299
11300 /* Convert object pointer to required type. */
11301 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11302 (*params)[0] = p0;
86951993
AM
11303
11304 /* Convert expected pointer to required type. */
11305 p1 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p1);
9771b263 11306 (*params)[1] = p1;
86951993
AM
11307
11308 /* Convert desired value to required type, and dereference it. */
11309 p2 = build_indirect_ref (loc, p2, RO_UNARY_STAR);
11310 p2 = build1 (VIEW_CONVERT_EXPR, I_type, p2);
9771b263 11311 (*params)[2] = p2;
86951993
AM
11312
11313 /* The rest of the parameters are fine. NULL means no special return value
11314 processing.*/
11315 *new_return = NULL;
11316 return false;
11317}
11318
11319
11320/* This will process an __atomic_load function call, determine whether it
11321 needs to be mapped to the _N variation, or turned into a library call.
11322 LOC is the location of the builtin call.
11323 FUNCTION is the DECL that has been invoked;
11324 PARAMS is the argument list for the call. The return value is non-null
11325 TRUE is returned if it is translated into the proper format for a call to the
11326 external library, and NEW_RETURN is set the tree for that function.
11327 FALSE is returned if processing for the _N variation is required, and
026c3cfd 11328 NEW_RETURN is set to the return value the result is copied into. */
86951993
AM
11329
11330static bool
11331resolve_overloaded_atomic_load (location_t loc, tree function,
9771b263 11332 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
11333{
11334 tree p0, p1, p2;
11335 tree I_type, I_type_ptr;
11336 int n = get_atomic_generic_size (loc, function, params);
11337
c466c4ff
AM
11338 /* Size of 0 is an error condition. */
11339 if (n == 0)
11340 {
11341 *new_return = error_mark_node;
11342 return true;
11343 }
11344
86951993 11345 /* If not a lock-free size, change to the library generic format. */
6b28e197 11346 if (!atomic_size_supported_p (n))
86951993
AM
11347 {
11348 *new_return = add_atomic_size_parameter (n, loc, function, params);
11349 return true;
11350 }
11351
11352 /* Otherwise, there is a match, so the call needs to be transformed from:
11353 void fn(T* mem, T* return, model)
11354 into
11355 *return = (T) (fn ((In *) mem, model)) */
11356
9771b263
DN
11357 p0 = (*params)[0];
11358 p1 = (*params)[1];
11359 p2 = (*params)[2];
86951993
AM
11360
11361 /* Create pointer to appropriate size. */
11362 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11363 I_type_ptr = build_pointer_type (I_type);
11364
11365 /* Convert object pointer to required type. */
11366 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11367 (*params)[0] = p0;
86951993
AM
11368
11369 /* Move memory model to the 2nd position, and end param list. */
9771b263
DN
11370 (*params)[1] = p2;
11371 params->truncate (2);
86951993
AM
11372
11373 /* Convert return pointer and dereference it for later assignment. */
11374 *new_return = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11375
11376 return false;
11377}
11378
11379
11380/* This will process an __atomic_store function call, determine whether it
11381 needs to be mapped to the _N variation, or turned into a library call.
11382 LOC is the location of the builtin call.
11383 FUNCTION is the DECL that has been invoked;
11384 PARAMS is the argument list for the call. The return value is non-null
11385 TRUE is returned if it is translated into the proper format for a call to the
11386 external library, and NEW_RETURN is set the tree for that function.
11387 FALSE is returned if processing for the _N variation is required, and
026c3cfd 11388 NEW_RETURN is set to the return value the result is copied into. */
86951993
AM
11389
11390static bool
11391resolve_overloaded_atomic_store (location_t loc, tree function,
9771b263 11392 vec<tree, va_gc> *params, tree *new_return)
86951993
AM
11393{
11394 tree p0, p1;
11395 tree I_type, I_type_ptr;
11396 int n = get_atomic_generic_size (loc, function, params);
11397
c466c4ff
AM
11398 /* Size of 0 is an error condition. */
11399 if (n == 0)
11400 {
11401 *new_return = error_mark_node;
11402 return true;
11403 }
11404
86951993 11405 /* If not a lock-free size, change to the library generic format. */
6b28e197 11406 if (!atomic_size_supported_p (n))
86951993
AM
11407 {
11408 *new_return = add_atomic_size_parameter (n, loc, function, params);
11409 return true;
11410 }
11411
11412 /* Otherwise, there is a match, so the call needs to be transformed from:
11413 void fn(T* mem, T* value, model)
11414 into
11415 fn ((In *) mem, (In) *value, model) */
11416
9771b263
DN
11417 p0 = (*params)[0];
11418 p1 = (*params)[1];
86951993
AM
11419
11420 /* Create pointer to appropriate size. */
11421 I_type = builtin_type_for_size (BITS_PER_UNIT * n, 1);
11422 I_type_ptr = build_pointer_type (I_type);
11423
11424 /* Convert object pointer to required type. */
11425 p0 = build1 (VIEW_CONVERT_EXPR, I_type_ptr, p0);
9771b263 11426 (*params)[0] = p0;
86951993
AM
11427
11428 /* Convert new value to required type, and dereference it. */
11429 p1 = build_indirect_ref (loc, p1, RO_UNARY_STAR);
11430 p1 = build1 (VIEW_CONVERT_EXPR, I_type, p1);
9771b263 11431 (*params)[1] = p1;
86951993
AM
11432
11433 /* The memory model is in the right spot already. Return is void. */
11434 *new_return = NULL_TREE;
11435
11436 return false;
11437}
11438
11439
48ae6c13
RH
11440/* Some builtin functions are placeholders for other expressions. This
11441 function should be called immediately after parsing the call expression
11442 before surrounding code has committed to the type of the expression.
11443
c2255bc4
AH
11444 LOC is the location of the builtin call.
11445
48ae6c13
RH
11446 FUNCTION is the DECL that has been invoked; it is known to be a builtin.
11447 PARAMS is the argument list for the call. The return value is non-null
11448 when expansion is complete, and null if normal processing should
11449 continue. */
11450
11451tree
9771b263
DN
11452resolve_overloaded_builtin (location_t loc, tree function,
11453 vec<tree, va_gc> *params)
48ae6c13
RH
11454{
11455 enum built_in_function orig_code = DECL_FUNCTION_CODE (function);
7a127fa7
MS
11456
11457 /* Is function one of the _FETCH_OP_ or _OP_FETCH_ built-ins?
11458 Those are not valid to call with a pointer to _Bool (or C++ bool)
11459 and so must be rejected. */
11460 bool fetch_op = true;
86951993
AM
11461 bool orig_format = true;
11462 tree new_return = NULL_TREE;
11463
58646b77
PB
11464 switch (DECL_BUILT_IN_CLASS (function))
11465 {
11466 case BUILT_IN_NORMAL:
11467 break;
11468 case BUILT_IN_MD:
11469 if (targetm.resolve_overloaded_builtin)
c2255bc4 11470 return targetm.resolve_overloaded_builtin (loc, function, params);
58646b77 11471 else
c22cacf3 11472 return NULL_TREE;
58646b77
PB
11473 default:
11474 return NULL_TREE;
11475 }
c22cacf3 11476
58646b77 11477 /* Handle BUILT_IN_NORMAL here. */
48ae6c13
RH
11478 switch (orig_code)
11479 {
86951993
AM
11480 case BUILT_IN_ATOMIC_EXCHANGE:
11481 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11482 case BUILT_IN_ATOMIC_LOAD:
11483 case BUILT_IN_ATOMIC_STORE:
11484 {
11485 /* Handle these 4 together so that they can fall through to the next
11486 case if the call is transformed to an _N variant. */
11487 switch (orig_code)
11488 {
11489 case BUILT_IN_ATOMIC_EXCHANGE:
11490 {
11491 if (resolve_overloaded_atomic_exchange (loc, function, params,
11492 &new_return))
11493 return new_return;
11494 /* Change to the _N variant. */
11495 orig_code = BUILT_IN_ATOMIC_EXCHANGE_N;
11496 break;
11497 }
11498
11499 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
11500 {
11501 if (resolve_overloaded_atomic_compare_exchange (loc, function,
11502 params,
11503 &new_return))
11504 return new_return;
11505 /* Change to the _N variant. */
11506 orig_code = BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N;
11507 break;
11508 }
11509 case BUILT_IN_ATOMIC_LOAD:
11510 {
11511 if (resolve_overloaded_atomic_load (loc, function, params,
11512 &new_return))
11513 return new_return;
11514 /* Change to the _N variant. */
11515 orig_code = BUILT_IN_ATOMIC_LOAD_N;
11516 break;
11517 }
11518 case BUILT_IN_ATOMIC_STORE:
11519 {
11520 if (resolve_overloaded_atomic_store (loc, function, params,
11521 &new_return))
11522 return new_return;
11523 /* Change to the _N variant. */
11524 orig_code = BUILT_IN_ATOMIC_STORE_N;
11525 break;
11526 }
11527 default:
11528 gcc_unreachable ();
11529 }
11530 /* Fallthrough to the normal processing. */
11531 }
11532 case BUILT_IN_ATOMIC_EXCHANGE_N:
11533 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N:
11534 case BUILT_IN_ATOMIC_LOAD_N:
11535 case BUILT_IN_ATOMIC_STORE_N:
7a127fa7
MS
11536 {
11537 fetch_op = false;
11538 /* Fallthrough to further processing. */
11539 }
86951993
AM
11540 case BUILT_IN_ATOMIC_ADD_FETCH_N:
11541 case BUILT_IN_ATOMIC_SUB_FETCH_N:
11542 case BUILT_IN_ATOMIC_AND_FETCH_N:
11543 case BUILT_IN_ATOMIC_NAND_FETCH_N:
11544 case BUILT_IN_ATOMIC_XOR_FETCH_N:
11545 case BUILT_IN_ATOMIC_OR_FETCH_N:
11546 case BUILT_IN_ATOMIC_FETCH_ADD_N:
11547 case BUILT_IN_ATOMIC_FETCH_SUB_N:
11548 case BUILT_IN_ATOMIC_FETCH_AND_N:
11549 case BUILT_IN_ATOMIC_FETCH_NAND_N:
11550 case BUILT_IN_ATOMIC_FETCH_XOR_N:
11551 case BUILT_IN_ATOMIC_FETCH_OR_N:
11552 {
11553 orig_format = false;
11554 /* Fallthru for parameter processing. */
11555 }
e0a8ecf2
AM
11556 case BUILT_IN_SYNC_FETCH_AND_ADD_N:
11557 case BUILT_IN_SYNC_FETCH_AND_SUB_N:
11558 case BUILT_IN_SYNC_FETCH_AND_OR_N:
11559 case BUILT_IN_SYNC_FETCH_AND_AND_N:
11560 case BUILT_IN_SYNC_FETCH_AND_XOR_N:
11561 case BUILT_IN_SYNC_FETCH_AND_NAND_N:
11562 case BUILT_IN_SYNC_ADD_AND_FETCH_N:
11563 case BUILT_IN_SYNC_SUB_AND_FETCH_N:
11564 case BUILT_IN_SYNC_OR_AND_FETCH_N:
11565 case BUILT_IN_SYNC_AND_AND_FETCH_N:
11566 case BUILT_IN_SYNC_XOR_AND_FETCH_N:
11567 case BUILT_IN_SYNC_NAND_AND_FETCH_N:
11568 case BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N:
11569 case BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N:
11570 case BUILT_IN_SYNC_LOCK_TEST_AND_SET_N:
11571 case BUILT_IN_SYNC_LOCK_RELEASE_N:
48ae6c13 11572 {
7a127fa7
MS
11573 /* The following are not _FETCH_OPs and must be accepted with
11574 pointers to _Bool (or C++ bool). */
11575 if (fetch_op)
11576 fetch_op =
11577 (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
11578 && orig_code != BUILT_IN_SYNC_VAL_COMPARE_AND_SWAP_N
11579 && orig_code != BUILT_IN_SYNC_LOCK_TEST_AND_SET_N
11580 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N);
11581
11582 int n = sync_resolve_size (function, params, fetch_op);
bbbbb16a 11583 tree new_function, first_param, result;
e79983f4 11584 enum built_in_function fncode;
48ae6c13
RH
11585
11586 if (n == 0)
11587 return error_mark_node;
11588
e79983f4
MM
11589 fncode = (enum built_in_function)((int)orig_code + exact_log2 (n) + 1);
11590 new_function = builtin_decl_explicit (fncode);
86951993
AM
11591 if (!sync_resolve_params (loc, function, new_function, params,
11592 orig_format))
48ae6c13
RH
11593 return error_mark_node;
11594
9771b263 11595 first_param = (*params)[0];
81e5eca8
MP
11596 result = build_function_call_vec (loc, vNULL, new_function, params,
11597 NULL);
86951993
AM
11598 if (result == error_mark_node)
11599 return result;
e0a8ecf2 11600 if (orig_code != BUILT_IN_SYNC_BOOL_COMPARE_AND_SWAP_N
86951993 11601 && orig_code != BUILT_IN_SYNC_LOCK_RELEASE_N
2e6b45af
MS
11602 && orig_code != BUILT_IN_ATOMIC_STORE_N
11603 && orig_code != BUILT_IN_ATOMIC_COMPARE_EXCHANGE_N)
86951993 11604 result = sync_resolve_return (first_param, result, orig_format);
48ae6c13 11605
14ba7b28
MP
11606 if (fetch_op)
11607 /* Prevent -Wunused-value warning. */
11608 TREE_USED (result) = true;
11609
86951993
AM
11610 /* If new_return is set, assign function to that expr and cast the
11611 result to void since the generic interface returned void. */
11612 if (new_return)
11613 {
11614 /* Cast function result from I{1,2,4,8,16} to the required type. */
11615 result = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (new_return), result);
11616 result = build2 (MODIFY_EXPR, TREE_TYPE (new_return), new_return,
11617 result);
11618 TREE_SIDE_EFFECTS (result) = 1;
11619 protected_set_expr_location (result, loc);
11620 result = convert (void_type_node, result);
11621 }
48ae6c13
RH
11622 return result;
11623 }
11624
11625 default:
58646b77 11626 return NULL_TREE;
48ae6c13
RH
11627 }
11628}
11629
0af94e6f
JR
11630/* vector_types_compatible_elements_p is used in type checks of vectors
11631 values used as operands of binary operators. Where it returns true, and
11632 the other checks of the caller succeed (being vector types in he first
11633 place, and matching number of elements), we can just treat the types
11634 as essentially the same.
11635 Contrast with vector_targets_convertible_p, which is used for vector
11636 pointer types, and vector_types_convertible_p, which will allow
11637 language-specific matches under the control of flag_lax_vector_conversions,
11638 and might still require a conversion. */
11639/* True if vector types T1 and T2 can be inputs to the same binary
11640 operator without conversion.
11641 We don't check the overall vector size here because some of our callers
11642 want to give different error messages when the vectors are compatible
11643 except for the element count. */
11644
5bed876a 11645bool
0af94e6f 11646vector_types_compatible_elements_p (tree t1, tree t2)
5bed876a 11647{
0af94e6f
JR
11648 bool opaque = TYPE_VECTOR_OPAQUE (t1) || TYPE_VECTOR_OPAQUE (t2);
11649 t1 = TREE_TYPE (t1);
11650 t2 = TREE_TYPE (t2);
11651
5bed876a
AH
11652 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
11653
ab22c1fa
CF
11654 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE)
11655 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE
11656 || c2 == FIXED_POINT_TYPE));
5bed876a 11657
0af94e6f
JR
11658 t1 = c_common_signed_type (t1);
11659 t2 = c_common_signed_type (t2);
5bed876a
AH
11660 /* Equality works here because c_common_signed_type uses
11661 TYPE_MAIN_VARIANT. */
0af94e6f
JR
11662 if (t1 == t2)
11663 return true;
11664 if (opaque && c1 == c2
11665 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE)
11666 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
11667 return true;
11668 return false;
5bed876a
AH
11669}
11670
104f8784
KG
11671/* Check for missing format attributes on function pointers. LTYPE is
11672 the new type or left-hand side type. RTYPE is the old type or
11673 right-hand side type. Returns TRUE if LTYPE is missing the desired
11674 attribute. */
11675
11676bool
11677check_missing_format_attribute (tree ltype, tree rtype)
11678{
11679 tree const ttr = TREE_TYPE (rtype), ttl = TREE_TYPE (ltype);
11680 tree ra;
11681
11682 for (ra = TYPE_ATTRIBUTES (ttr); ra; ra = TREE_CHAIN (ra))
11683 if (is_attribute_p ("format", TREE_PURPOSE (ra)))
11684 break;
11685 if (ra)
11686 {
11687 tree la;
11688 for (la = TYPE_ATTRIBUTES (ttl); la; la = TREE_CHAIN (la))
11689 if (is_attribute_p ("format", TREE_PURPOSE (la)))
11690 break;
11691 return !la;
11692 }
11693 else
11694 return false;
11695}
11696
ff6b6641
GDR
11697/* Subscripting with type char is likely to lose on a machine where
11698 chars are signed. So warn on any machine, but optionally. Don't
11699 warn for unsigned char since that type is safe. Don't warn for
11700 signed char because anyone who uses that must have done so
11701 deliberately. Furthermore, we reduce the false positive load by
11702 warning only for non-constant value of type char. */
11703
11704void
5bd012f8 11705warn_array_subscript_with_type_char (location_t loc, tree index)
ff6b6641
GDR
11706{
11707 if (TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node
11708 && TREE_CODE (index) != INTEGER_CST)
5bd012f8
MP
11709 warning_at (loc, OPT_Wchar_subscripts,
11710 "array subscript has type %<char%>");
ff6b6641
GDR
11711}
11712
2a67bec2
ILT
11713/* Implement -Wparentheses for the unexpected C precedence rules, to
11714 cover cases like x + y << z which readers are likely to
11715 misinterpret. We have seen an expression in which CODE is a binary
100d537d
MLI
11716 operator used to combine expressions ARG_LEFT and ARG_RIGHT, which
11717 before folding had CODE_LEFT and CODE_RIGHT. CODE_LEFT and
11718 CODE_RIGHT may be ERROR_MARK, which means that that side of the
11719 expression was not formed using a binary or unary operator, or it
11720 was enclosed in parentheses. */
2a67bec2
ILT
11721
11722void
5d9de0d0 11723warn_about_parentheses (location_t loc, enum tree_code code,
fb3e178a 11724 enum tree_code code_left, tree arg_left,
100d537d 11725 enum tree_code code_right, tree arg_right)
2a67bec2
ILT
11726{
11727 if (!warn_parentheses)
11728 return;
11729
100d537d
MLI
11730 /* This macro tests that the expression ARG with original tree code
11731 CODE appears to be a boolean expression. or the result of folding a
11732 boolean expression. */
11733#define APPEARS_TO_BE_BOOLEAN_EXPR_P(CODE, ARG) \
11734 (truth_value_p (TREE_CODE (ARG)) \
11735 || TREE_CODE (TREE_TYPE (ARG)) == BOOLEAN_TYPE \
11736 /* Folding may create 0 or 1 integers from other expressions. */ \
11737 || ((CODE) != INTEGER_CST \
11738 && (integer_onep (ARG) || integer_zerop (ARG))))
11739
b8698a0f 11740 switch (code)
2a67bec2 11741 {
100d537d 11742 case LSHIFT_EXPR:
5d9de0d0
PC
11743 if (code_left == PLUS_EXPR)
11744 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11745 "suggest parentheses around %<+%> inside %<<<%>");
11746 else if (code_right == PLUS_EXPR)
11747 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11748 "suggest parentheses around %<+%> inside %<<<%>");
11749 else if (code_left == MINUS_EXPR)
11750 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11751 "suggest parentheses around %<-%> inside %<<<%>");
11752 else if (code_right == MINUS_EXPR)
11753 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11754 "suggest parentheses around %<-%> inside %<<<%>");
100d537d 11755 return;
2a67bec2 11756
100d537d 11757 case RSHIFT_EXPR:
5d9de0d0
PC
11758 if (code_left == PLUS_EXPR)
11759 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11760 "suggest parentheses around %<+%> inside %<>>%>");
11761 else if (code_right == PLUS_EXPR)
11762 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11763 "suggest parentheses around %<+%> inside %<>>%>");
11764 else if (code_left == MINUS_EXPR)
11765 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11766 "suggest parentheses around %<-%> inside %<>>%>");
11767 else if (code_right == MINUS_EXPR)
11768 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11769 "suggest parentheses around %<-%> inside %<>>%>");
100d537d 11770 return;
2a67bec2 11771
100d537d 11772 case TRUTH_ORIF_EXPR:
5d9de0d0
PC
11773 if (code_left == TRUTH_ANDIF_EXPR)
11774 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11775 "suggest parentheses around %<&&%> within %<||%>");
11776 else if (code_right == TRUTH_ANDIF_EXPR)
11777 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11778 "suggest parentheses around %<&&%> within %<||%>");
100d537d
MLI
11779 return;
11780
11781 case BIT_IOR_EXPR:
2a67bec2 11782 if (code_left == BIT_AND_EXPR || code_left == BIT_XOR_EXPR
5d9de0d0
PC
11783 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11784 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11785 "suggest parentheses around arithmetic in operand of %<|%>");
11786 else if (code_right == BIT_AND_EXPR || code_right == BIT_XOR_EXPR
11787 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11788 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11789 "suggest parentheses around arithmetic in operand of %<|%>");
2a67bec2 11790 /* Check cases like x|y==z */
5d9de0d0
PC
11791 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11792 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11793 "suggest parentheses around comparison in operand of %<|%>");
11794 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11795 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11796 "suggest parentheses around comparison in operand of %<|%>");
11797 /* Check cases like !x | y */
11798 else if (code_left == TRUTH_NOT_EXPR
11799 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
5d9de0d0
PC
11800 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11801 "suggest parentheses around operand of "
11802 "%<!%> or change %<|%> to %<||%> or %<!%> to %<~%>");
100d537d 11803 return;
2a67bec2 11804
100d537d 11805 case BIT_XOR_EXPR:
2a67bec2 11806 if (code_left == BIT_AND_EXPR
5d9de0d0
PC
11807 || code_left == PLUS_EXPR || code_left == MINUS_EXPR)
11808 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11809 "suggest parentheses around arithmetic in operand of %<^%>");
11810 else if (code_right == BIT_AND_EXPR
11811 || code_right == PLUS_EXPR || code_right == MINUS_EXPR)
11812 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11813 "suggest parentheses around arithmetic in operand of %<^%>");
2a67bec2 11814 /* Check cases like x^y==z */
5d9de0d0
PC
11815 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11816 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11817 "suggest parentheses around comparison in operand of %<^%>");
11818 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11819 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11820 "suggest parentheses around comparison in operand of %<^%>");
11821 return;
2a67bec2 11822
100d537d 11823 case BIT_AND_EXPR:
5d9de0d0
PC
11824 if (code_left == PLUS_EXPR)
11825 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11826 "suggest parentheses around %<+%> in operand of %<&%>");
11827 else if (code_right == PLUS_EXPR)
11828 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11829 "suggest parentheses around %<+%> in operand of %<&%>");
5d9de0d0
PC
11830 else if (code_left == MINUS_EXPR)
11831 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11832 "suggest parentheses around %<-%> in operand of %<&%>");
11833 else if (code_right == MINUS_EXPR)
11834 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d 11835 "suggest parentheses around %<-%> in operand of %<&%>");
2a67bec2 11836 /* Check cases like x&y==z */
5d9de0d0
PC
11837 else if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11838 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11839 "suggest parentheses around comparison in operand of %<&%>");
11840 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11841 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11842 "suggest parentheses around comparison in operand of %<&%>");
11843 /* Check cases like !x & y */
11844 else if (code_left == TRUTH_NOT_EXPR
11845 && !APPEARS_TO_BE_BOOLEAN_EXPR_P (code_right, arg_right))
5d9de0d0
PC
11846 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11847 "suggest parentheses around operand of "
11848 "%<!%> or change %<&%> to %<&&%> or %<!%> to %<~%>");
100d537d 11849 return;
2a67bec2 11850
100d537d 11851 case EQ_EXPR:
5d9de0d0
PC
11852 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11853 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11854 "suggest parentheses around comparison in operand of %<==%>");
11855 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11856 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11857 "suggest parentheses around comparison in operand of %<==%>");
11858 return;
11859 case NE_EXPR:
5d9de0d0
PC
11860 if (TREE_CODE_CLASS (code_left) == tcc_comparison)
11861 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11862 "suggest parentheses around comparison in operand of %<!=%>");
11863 else if (TREE_CODE_CLASS (code_right) == tcc_comparison)
11864 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
100d537d
MLI
11865 "suggest parentheses around comparison in operand of %<!=%>");
11866 return;
11867
11868 default:
5d9de0d0
PC
11869 if (TREE_CODE_CLASS (code) == tcc_comparison)
11870 {
11871 if (TREE_CODE_CLASS (code_left) == tcc_comparison
fb3e178a
JJ
11872 && code_left != NE_EXPR && code_left != EQ_EXPR
11873 && INTEGRAL_TYPE_P (TREE_TYPE (arg_left)))
5d9de0d0
PC
11874 warning_at (EXPR_LOC_OR_LOC (arg_left, loc), OPT_Wparentheses,
11875 "comparisons like %<X<=Y<=Z%> do not "
11876 "have their mathematical meaning");
11877 else if (TREE_CODE_CLASS (code_right) == tcc_comparison
fb3e178a 11878 && code_right != NE_EXPR && code_right != EQ_EXPR
5d9de0d0
PC
11879 && INTEGRAL_TYPE_P (TREE_TYPE (arg_right)))
11880 warning_at (EXPR_LOC_OR_LOC (arg_right, loc), OPT_Wparentheses,
11881 "comparisons like %<X<=Y<=Z%> do not "
11882 "have their mathematical meaning");
11883 }
100d537d 11884 return;
e7917d06 11885 }
100d537d 11886#undef NOT_A_BOOLEAN_EXPR_P
2a67bec2
ILT
11887}
11888
c616e51b
MLI
11889/* If LABEL (a LABEL_DECL) has not been used, issue a warning. */
11890
11891void
11892warn_for_unused_label (tree label)
11893{
11894 if (!TREE_USED (label))
11895 {
11896 if (DECL_INITIAL (label))
11897 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
11898 else
11899 warning (OPT_Wunused_label, "label %q+D declared but not defined", label);
11900 }
11901}
ff6b6641 11902
c9f9eb5d
AH
11903/* Warn for division by zero according to the value of DIVISOR. LOC
11904 is the location of the division operator. */
2e9cb75e
MLI
11905
11906void
c9f9eb5d 11907warn_for_div_by_zero (location_t loc, tree divisor)
2e9cb75e 11908{
ab22c1fa
CF
11909 /* If DIVISOR is zero, and has integral or fixed-point type, issue a warning
11910 about division by zero. Do not issue a warning if DIVISOR has a
2e9cb75e
MLI
11911 floating-point type, since we consider 0.0/0.0 a valid way of
11912 generating a NaN. */
7d882b83 11913 if (c_inhibit_evaluation_warnings == 0
ab22c1fa 11914 && (integer_zerop (divisor) || fixed_zerop (divisor)))
c9f9eb5d 11915 warning_at (loc, OPT_Wdiv_by_zero, "division by zero");
2e9cb75e
MLI
11916}
11917
c1e1f433
BS
11918/* Warn for patterns where memset appears to be used incorrectly. The
11919 warning location should be LOC. ARG0, and ARG2 are the first and
11920 last arguments to the call, while LITERAL_ZERO_MASK has a 1 bit for
11921 each argument that was a literal zero. */
11922
11923void
11924warn_for_memset (location_t loc, tree arg0, tree arg2,
11925 int literal_zero_mask)
11926{
11927 if (warn_memset_transposed_args
11928 && integer_zerop (arg2)
11929 && (literal_zero_mask & (1 << 2)) != 0
11930 && (literal_zero_mask & (1 << 1)) == 0)
11931 warning_at (loc, OPT_Wmemset_transposed_args,
11932 "%<memset%> used with constant zero length "
11933 "parameter; this could be due to transposed "
11934 "parameters");
11935
11936 if (warn_memset_elt_size && TREE_CODE (arg2) == INTEGER_CST)
11937 {
11938 STRIP_NOPS (arg0);
11939 if (TREE_CODE (arg0) == ADDR_EXPR)
11940 arg0 = TREE_OPERAND (arg0, 0);
11941 tree type = TREE_TYPE (arg0);
11942 if (TREE_CODE (type) == ARRAY_TYPE)
11943 {
11944 tree elt_type = TREE_TYPE (type);
11945 tree domain = TYPE_DOMAIN (type);
11946 if (!integer_onep (TYPE_SIZE_UNIT (elt_type))
2fff3db8 11947 && domain != NULL_TREE
c1e1f433
BS
11948 && TYPE_MAXVAL (domain)
11949 && TYPE_MINVAL (domain)
11950 && integer_zerop (TYPE_MINVAL (domain))
11951 && integer_onep (fold_build2 (MINUS_EXPR, domain,
11952 arg2,
11953 TYPE_MAXVAL (domain))))
11954 warning_at (loc, OPT_Wmemset_elt_size,
11955 "%<memset%> used with length equal to "
11956 "number of elements without multiplication "
11957 "by element size");
11958 }
11959 }
11960}
11961
2d12797c
MLI
11962/* Subroutine of build_binary_op. Give warnings for comparisons
11963 between signed and unsigned quantities that may fail. Do the
11964 checking based on the original operand trees ORIG_OP0 and ORIG_OP1,
11965 so that casts will be considered, but default promotions won't
ba47d38d
AH
11966 be.
11967
11968 LOCATION is the location of the comparison operator.
2d12797c
MLI
11969
11970 The arguments of this function map directly to local variables
11971 of build_binary_op. */
11972
b8698a0f 11973void
ba47d38d 11974warn_for_sign_compare (location_t location,
b8698a0f
L
11975 tree orig_op0, tree orig_op1,
11976 tree op0, tree op1,
2d12797c
MLI
11977 tree result_type, enum tree_code resultcode)
11978{
11979 int op0_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op0));
11980 int op1_signed = !TYPE_UNSIGNED (TREE_TYPE (orig_op1));
11981 int unsignedp0, unsignedp1;
b8698a0f 11982
2d12797c
MLI
11983 /* In C++, check for comparison of different enum types. */
11984 if (c_dialect_cxx()
11985 && TREE_CODE (TREE_TYPE (orig_op0)) == ENUMERAL_TYPE
11986 && TREE_CODE (TREE_TYPE (orig_op1)) == ENUMERAL_TYPE
11987 && TYPE_MAIN_VARIANT (TREE_TYPE (orig_op0))
ead51d36 11988 != TYPE_MAIN_VARIANT (TREE_TYPE (orig_op1)))
2d12797c 11989 {
ba47d38d
AH
11990 warning_at (location,
11991 OPT_Wsign_compare, "comparison between types %qT and %qT",
11992 TREE_TYPE (orig_op0), TREE_TYPE (orig_op1));
2d12797c
MLI
11993 }
11994
11995 /* Do not warn if the comparison is being done in a signed type,
11996 since the signed type will only be chosen if it can represent
11997 all the values of the unsigned type. */
11998 if (!TYPE_UNSIGNED (result_type))
11999 /* OK */;
12000 /* Do not warn if both operands are unsigned. */
12001 else if (op0_signed == op1_signed)
12002 /* OK */;
12003 else
12004 {
ead51d36 12005 tree sop, uop, base_type;
2d12797c 12006 bool ovf;
ead51d36 12007
2d12797c
MLI
12008 if (op0_signed)
12009 sop = orig_op0, uop = orig_op1;
b8698a0f 12010 else
2d12797c
MLI
12011 sop = orig_op1, uop = orig_op0;
12012
b8698a0f 12013 STRIP_TYPE_NOPS (sop);
2d12797c 12014 STRIP_TYPE_NOPS (uop);
ead51d36
JJ
12015 base_type = (TREE_CODE (result_type) == COMPLEX_TYPE
12016 ? TREE_TYPE (result_type) : result_type);
2d12797c
MLI
12017
12018 /* Do not warn if the signed quantity is an unsuffixed integer
12019 literal (or some static constant expression involving such
12020 literals or a conditional expression involving such literals)
12021 and it is non-negative. */
12022 if (tree_expr_nonnegative_warnv_p (sop, &ovf))
12023 /* OK */;
12024 /* Do not warn if the comparison is an equality operation, the
12025 unsigned quantity is an integral constant, and it would fit
12026 in the result if the result were signed. */
12027 else if (TREE_CODE (uop) == INTEGER_CST
12028 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
ead51d36 12029 && int_fits_type_p (uop, c_common_signed_type (base_type)))
2d12797c
MLI
12030 /* OK */;
12031 /* In C, do not warn if the unsigned quantity is an enumeration
12032 constant and its maximum value would fit in the result if the
12033 result were signed. */
12034 else if (!c_dialect_cxx() && TREE_CODE (uop) == INTEGER_CST
12035 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
12036 && int_fits_type_p (TYPE_MAX_VALUE (TREE_TYPE (uop)),
ead51d36 12037 c_common_signed_type (base_type)))
2d12797c 12038 /* OK */;
b8698a0f 12039 else
ba47d38d 12040 warning_at (location,
b8698a0f 12041 OPT_Wsign_compare,
ba47d38d 12042 "comparison between signed and unsigned integer expressions");
2d12797c 12043 }
b8698a0f 12044
2d12797c
MLI
12045 /* Warn if two unsigned values are being compared in a size larger
12046 than their original size, and one (and only one) is the result of
12047 a `~' operator. This comparison will always fail.
b8698a0f 12048
2d12797c
MLI
12049 Also warn if one operand is a constant, and the constant does not
12050 have all bits set that are set in the ~ operand when it is
12051 extended. */
12052
828fb3ba
JM
12053 op0 = c_common_get_narrower (op0, &unsignedp0);
12054 op1 = c_common_get_narrower (op1, &unsignedp1);
b8698a0f 12055
2d12797c
MLI
12056 if ((TREE_CODE (op0) == BIT_NOT_EXPR)
12057 ^ (TREE_CODE (op1) == BIT_NOT_EXPR))
12058 {
12059 if (TREE_CODE (op0) == BIT_NOT_EXPR)
828fb3ba 12060 op0 = c_common_get_narrower (TREE_OPERAND (op0, 0), &unsignedp0);
2d12797c 12061 if (TREE_CODE (op1) == BIT_NOT_EXPR)
828fb3ba 12062 op1 = c_common_get_narrower (TREE_OPERAND (op1, 0), &unsignedp1);
2d12797c 12063
9541ffee 12064 if (tree_fits_shwi_p (op0) || tree_fits_shwi_p (op1))
2d12797c
MLI
12065 {
12066 tree primop;
12067 HOST_WIDE_INT constant, mask;
12068 int unsignedp;
12069 unsigned int bits;
b8698a0f 12070
9541ffee 12071 if (tree_fits_shwi_p (op0))
2d12797c
MLI
12072 {
12073 primop = op1;
12074 unsignedp = unsignedp1;
9439e9a1 12075 constant = tree_to_shwi (op0);
2d12797c
MLI
12076 }
12077 else
12078 {
12079 primop = op0;
12080 unsignedp = unsignedp0;
9439e9a1 12081 constant = tree_to_shwi (op1);
2d12797c 12082 }
b8698a0f 12083
2d12797c
MLI
12084 bits = TYPE_PRECISION (TREE_TYPE (primop));
12085 if (bits < TYPE_PRECISION (result_type)
12086 && bits < HOST_BITS_PER_LONG && unsignedp)
12087 {
e3fe09c1 12088 mask = HOST_WIDE_INT_M1U << bits;
2d12797c
MLI
12089 if ((mask & constant) != mask)
12090 {
12091 if (constant == 0)
0a756a3f
MP
12092 warning_at (location, OPT_Wsign_compare,
12093 "promoted ~unsigned is always non-zero");
2d12797c 12094 else
b8698a0f 12095 warning_at (location, OPT_Wsign_compare,
ba47d38d 12096 "comparison of promoted ~unsigned with constant");
2d12797c
MLI
12097 }
12098 }
12099 }
12100 else if (unsignedp0 && unsignedp1
12101 && (TYPE_PRECISION (TREE_TYPE (op0))
12102 < TYPE_PRECISION (result_type))
12103 && (TYPE_PRECISION (TREE_TYPE (op1))
12104 < TYPE_PRECISION (result_type)))
ba47d38d 12105 warning_at (location, OPT_Wsign_compare,
2d12797c
MLI
12106 "comparison of promoted ~unsigned with unsigned");
12107 }
12108}
12109
c5ee1358
MM
12110/* RESULT_TYPE is the result of converting TYPE1 and TYPE2 to a common
12111 type via c_common_type. If -Wdouble-promotion is in use, and the
12112 conditions for warning have been met, issue a warning. GMSGID is
12113 the warning message. It must have two %T specifiers for the type
12114 that was converted (generally "float") and the type to which it was
12115 converted (generally "double), respectively. LOC is the location
12116 to which the awrning should refer. */
12117
12118void
12119do_warn_double_promotion (tree result_type, tree type1, tree type2,
12120 const char *gmsgid, location_t loc)
12121{
12122 tree source_type;
12123
12124 if (!warn_double_promotion)
12125 return;
12126 /* If the conversion will not occur at run-time, there is no need to
12127 warn about it. */
12128 if (c_inhibit_evaluation_warnings)
12129 return;
12130 if (TYPE_MAIN_VARIANT (result_type) != double_type_node
12131 && TYPE_MAIN_VARIANT (result_type) != complex_double_type_node)
12132 return;
12133 if (TYPE_MAIN_VARIANT (type1) == float_type_node
12134 || TYPE_MAIN_VARIANT (type1) == complex_float_type_node)
12135 source_type = type1;
12136 else if (TYPE_MAIN_VARIANT (type2) == float_type_node
12137 || TYPE_MAIN_VARIANT (type2) == complex_float_type_node)
12138 source_type = type2;
12139 else
12140 return;
12141 warning_at (loc, OPT_Wdouble_promotion, gmsgid, source_type, result_type);
12142}
12143
da2e71c9
MLI
12144/* Possibly warn about unused parameters. */
12145
12146void
12147do_warn_unused_parameter (tree fn)
12148{
12149 tree decl;
12150
12151 for (decl = DECL_ARGUMENTS (fn);
12152 decl; decl = DECL_CHAIN (decl))
12153 if (!TREE_USED (decl) && TREE_CODE (decl) == PARM_DECL
12154 && DECL_NAME (decl) && !DECL_ARTIFICIAL (decl)
12155 && !TREE_NO_WARNING (decl))
12156 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wunused_parameter,
12157 "unused parameter %qD", decl);
12158}
12159
12160
d0940d56
DS
12161/* Setup a TYPE_DECL node as a typedef representation.
12162
12163 X is a TYPE_DECL for a typedef statement. Create a brand new
12164 ..._TYPE node (which will be just a variant of the existing
12165 ..._TYPE node with identical properties) and then install X
12166 as the TYPE_NAME of this brand new (duplicate) ..._TYPE node.
12167
12168 The whole point here is to end up with a situation where each
12169 and every ..._TYPE node the compiler creates will be uniquely
12170 associated with AT MOST one node representing a typedef name.
12171 This way, even though the compiler substitutes corresponding
12172 ..._TYPE nodes for TYPE_DECL (i.e. "typedef name") nodes very
12173 early on, later parts of the compiler can always do the reverse
12174 translation and get back the corresponding typedef name. For
12175 example, given:
12176
12177 typedef struct S MY_TYPE;
12178 MY_TYPE object;
12179
12180 Later parts of the compiler might only know that `object' was of
12181 type `struct S' if it were not for code just below. With this
12182 code however, later parts of the compiler see something like:
12183
12184 struct S' == struct S
12185 typedef struct S' MY_TYPE;
12186 struct S' object;
12187
12188 And they can then deduce (from the node for type struct S') that
12189 the original object declaration was:
12190
12191 MY_TYPE object;
12192
12193 Being able to do this is important for proper support of protoize,
12194 and also for generating precise symbolic debugging information
12195 which takes full account of the programmer's (typedef) vocabulary.
12196
12197 Obviously, we don't want to generate a duplicate ..._TYPE node if
12198 the TYPE_DECL node that we are now processing really represents a
12199 standard built-in type. */
12200
12201void
12202set_underlying_type (tree x)
12203{
12204 if (x == error_mark_node)
12205 return;
12206 if (DECL_IS_BUILTIN (x))
12207 {
12208 if (TYPE_NAME (TREE_TYPE (x)) == 0)
12209 TYPE_NAME (TREE_TYPE (x)) = x;
12210 }
12211 else if (TREE_TYPE (x) != error_mark_node
12212 && DECL_ORIGINAL_TYPE (x) == NULL_TREE)
12213 {
12214 tree tt = TREE_TYPE (x);
12215 DECL_ORIGINAL_TYPE (x) = tt;
12216 tt = build_variant_type_copy (tt);
12217 TYPE_STUB_DECL (tt) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
12218 TYPE_NAME (tt) = x;
12219 TREE_USED (tt) = TREE_USED (x);
12220 TREE_TYPE (x) = tt;
12221 }
12222}
12223
b646ba3f
DS
12224/* Record the types used by the current global variable declaration
12225 being parsed, so that we can decide later to emit their debug info.
12226 Those types are in types_used_by_cur_var_decl, and we are going to
12227 store them in the types_used_by_vars_hash hash table.
12228 DECL is the declaration of the global variable that has been parsed. */
12229
12230void
12231record_types_used_by_current_var_decl (tree decl)
12232{
12233 gcc_assert (decl && DECL_P (decl) && TREE_STATIC (decl));
12234
9771b263 12235 while (types_used_by_cur_var_decl && !types_used_by_cur_var_decl->is_empty ())
b646ba3f 12236 {
9771b263 12237 tree type = types_used_by_cur_var_decl->pop ();
bc87224e 12238 types_used_by_var_decl_insert (type, decl);
b646ba3f
DS
12239 }
12240}
12241
3797cb21
DS
12242/* If DECL is a typedef that is declared in the current function,
12243 record it for the purpose of -Wunused-local-typedefs. */
12244
12245void
12246record_locally_defined_typedef (tree decl)
12247{
12248 struct c_language_function *l;
12249
12250 if (!warn_unused_local_typedefs
12251 || cfun == NULL
12252 /* if this is not a locally defined typedef then we are not
12253 interested. */
12254 || !is_typedef_decl (decl)
12255 || !decl_function_context (decl))
12256 return;
12257
12258 l = (struct c_language_function *) cfun->language;
9771b263 12259 vec_safe_push (l->local_typedefs, decl);
3797cb21
DS
12260}
12261
12262/* If T is a TYPE_DECL declared locally, mark it as used. */
12263
12264void
12265maybe_record_typedef_use (tree t)
12266{
12267 if (!is_typedef_decl (t))
12268 return;
12269
12270 TREE_USED (t) = true;
12271}
12272
12273/* Warn if there are some unused locally defined typedefs in the
12274 current function. */
12275
12276void
12277maybe_warn_unused_local_typedefs (void)
12278{
12279 int i;
12280 tree decl;
12281 /* The number of times we have emitted -Wunused-local-typedefs
12282 warnings. If this is different from errorcount, that means some
12283 unrelated errors have been issued. In which case, we'll avoid
12284 emitting "unused-local-typedefs" warnings. */
12285 static int unused_local_typedefs_warn_count;
12286 struct c_language_function *l;
12287
12288 if (cfun == NULL)
12289 return;
12290
12291 if ((l = (struct c_language_function *) cfun->language) == NULL)
12292 return;
12293
12294 if (warn_unused_local_typedefs
12295 && errorcount == unused_local_typedefs_warn_count)
12296 {
9771b263 12297 FOR_EACH_VEC_SAFE_ELT (l->local_typedefs, i, decl)
3797cb21
DS
12298 if (!TREE_USED (decl))
12299 warning_at (DECL_SOURCE_LOCATION (decl),
12300 OPT_Wunused_local_typedefs,
12301 "typedef %qD locally defined but not used", decl);
12302 unused_local_typedefs_warn_count = errorcount;
12303 }
12304
9771b263 12305 vec_free (l->local_typedefs);
3797cb21
DS
12306}
12307
04159acf
MP
12308/* Warn about boolean expression compared with an integer value different
12309 from true/false. Warns also e.g. about "(i1 == i2) == 2".
12310 LOC is the location of the comparison, CODE is its code, OP0 and OP1
12311 are the operands of the comparison. The caller must ensure that
12312 either operand is a boolean expression. */
12313
12314void
12315maybe_warn_bool_compare (location_t loc, enum tree_code code, tree op0,
12316 tree op1)
12317{
12318 if (TREE_CODE_CLASS (code) != tcc_comparison)
12319 return;
12320
f479b43d
JM
12321 tree f, cst;
12322 if (f = fold_for_warn (op0),
12323 TREE_CODE (f) == INTEGER_CST)
12324 cst = op0 = f;
12325 else if (f = fold_for_warn (op1),
12326 TREE_CODE (f) == INTEGER_CST)
12327 cst = op1 = f;
12328 else
04159acf
MP
12329 return;
12330
12331 if (!integer_zerop (cst) && !integer_onep (cst))
12332 {
716c0ba6
MP
12333 int sign = (TREE_CODE (op0) == INTEGER_CST
12334 ? tree_int_cst_sgn (cst) : -tree_int_cst_sgn (cst));
04159acf
MP
12335 if (code == EQ_EXPR
12336 || ((code == GT_EXPR || code == GE_EXPR) && sign < 0)
12337 || ((code == LT_EXPR || code == LE_EXPR) && sign > 0))
12338 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12339 "with boolean expression is always false", cst);
12340 else
12341 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12342 "with boolean expression is always true", cst);
12343 }
716c0ba6
MP
12344 else if (integer_zerop (cst) || integer_onep (cst))
12345 {
577cd070
MP
12346 /* If the non-constant operand isn't of a boolean type, we
12347 don't want to warn here. */
12348 tree noncst = TREE_CODE (op0) == INTEGER_CST ? op1 : op0;
12349 /* Handle booleans promoted to integers. */
12350 if (CONVERT_EXPR_P (noncst)
12351 && TREE_TYPE (noncst) == integer_type_node
12352 && TREE_CODE (TREE_TYPE (TREE_OPERAND (noncst, 0))) == BOOLEAN_TYPE)
12353 /* Warn. */;
12354 else if (TREE_CODE (TREE_TYPE (noncst)) != BOOLEAN_TYPE
12355 && !truth_value_p (TREE_CODE (noncst)))
12356 return;
716c0ba6
MP
12357 /* Do some magic to get the right diagnostics. */
12358 bool flag = TREE_CODE (op0) == INTEGER_CST;
12359 flag = integer_zerop (cst) ? flag : !flag;
12360 if ((code == GE_EXPR && !flag) || (code == LE_EXPR && flag))
12361 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12362 "with boolean expression is always true", cst);
12363 else if ((code == LT_EXPR && !flag) || (code == GT_EXPR && flag))
12364 warning_at (loc, OPT_Wbool_compare, "comparison of constant %qE "
12365 "with boolean expression is always false", cst);
12366 }
04159acf
MP
12367}
12368
451b5e48
MP
12369/* Warn if signed left shift overflows. We don't warn
12370 about left-shifting 1 into the sign bit in C++14; cf.
12371 <http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3367.html#1457>
12372 LOC is a location of the shift; OP0 and OP1 are the operands.
12373 Return true if an overflow is detected, false otherwise. */
12374
12375bool
12376maybe_warn_shift_overflow (location_t loc, tree op0, tree op1)
12377{
12378 if (TREE_CODE (op0) != INTEGER_CST
12379 || TREE_CODE (op1) != INTEGER_CST)
12380 return false;
12381
12382 tree type0 = TREE_TYPE (op0);
12383 unsigned int prec0 = TYPE_PRECISION (type0);
12384
12385 /* Left-hand operand must be signed. */
12386 if (TYPE_UNSIGNED (type0))
12387 return false;
12388
b893e375
MP
12389 unsigned int min_prec = (wi::min_precision (op0, SIGNED)
12390 + TREE_INT_CST_LOW (op1));
c5404f1c
PB
12391 /* Handle the case of left-shifting 1 into the sign bit.
12392 * However, shifting 1 _out_ of the sign bit, as in
12393 * INT_MIN << 1, is considered an overflow.
12394 */
12395 if (!tree_int_cst_sign_bit(op0) && min_prec == prec0 + 1)
451b5e48
MP
12396 {
12397 /* Never warn for C++14 onwards. */
12398 if (cxx_dialect >= cxx14)
12399 return false;
12400 /* Otherwise only if -Wshift-overflow=2. But return
12401 true to signal an overflow for the sake of integer
12402 constant expressions. */
12403 if (warn_shift_overflow < 2)
12404 return true;
12405 }
12406
451b5e48
MP
12407 bool overflowed = min_prec > prec0;
12408 if (overflowed && c_inhibit_evaluation_warnings == 0)
12409 warning_at (loc, OPT_Wshift_overflow_,
12410 "result of %qE requires %u bits to represent, "
12411 "but %qT only has %u bits",
12412 build2_loc (loc, LSHIFT_EXPR, type0, op0, op1),
12413 min_prec, type0, prec0);
12414
12415 return overflowed;
12416}
12417
c166b898
ILT
12418/* The C and C++ parsers both use vectors to hold function arguments.
12419 For efficiency, we keep a cache of unused vectors. This is the
12420 cache. */
12421
9771b263
DN
12422typedef vec<tree, va_gc> *tree_gc_vec;
12423static GTY((deletable)) vec<tree_gc_vec, va_gc> *tree_vector_cache;
c166b898
ILT
12424
12425/* Return a new vector from the cache. If the cache is empty,
12426 allocate a new vector. These vectors are GC'ed, so it is OK if the
12427 pointer is not released.. */
12428
9771b263 12429vec<tree, va_gc> *
c166b898
ILT
12430make_tree_vector (void)
12431{
9771b263
DN
12432 if (tree_vector_cache && !tree_vector_cache->is_empty ())
12433 return tree_vector_cache->pop ();
c166b898
ILT
12434 else
12435 {
9771b263 12436 /* Passing 0 to vec::alloc returns NULL, and our callers require
c166b898
ILT
12437 that we always return a non-NULL value. The vector code uses
12438 4 when growing a NULL vector, so we do too. */
9771b263
DN
12439 vec<tree, va_gc> *v;
12440 vec_alloc (v, 4);
12441 return v;
c166b898
ILT
12442 }
12443}
12444
12445/* Release a vector of trees back to the cache. */
12446
12447void
9771b263 12448release_tree_vector (vec<tree, va_gc> *vec)
c166b898
ILT
12449{
12450 if (vec != NULL)
12451 {
9771b263
DN
12452 vec->truncate (0);
12453 vec_safe_push (tree_vector_cache, vec);
c166b898
ILT
12454 }
12455}
12456
12457/* Get a new tree vector holding a single tree. */
12458
9771b263 12459vec<tree, va_gc> *
c166b898
ILT
12460make_tree_vector_single (tree t)
12461{
9771b263
DN
12462 vec<tree, va_gc> *ret = make_tree_vector ();
12463 ret->quick_push (t);
c166b898
ILT
12464 return ret;
12465}
12466
c12ff9d8
JM
12467/* Get a new tree vector of the TREE_VALUEs of a TREE_LIST chain. */
12468
9771b263 12469vec<tree, va_gc> *
c12ff9d8
JM
12470make_tree_vector_from_list (tree list)
12471{
9771b263 12472 vec<tree, va_gc> *ret = make_tree_vector ();
c12ff9d8 12473 for (; list; list = TREE_CHAIN (list))
9771b263 12474 vec_safe_push (ret, TREE_VALUE (list));
c12ff9d8
JM
12475 return ret;
12476}
12477
c166b898
ILT
12478/* Get a new tree vector which is a copy of an existing one. */
12479
9771b263
DN
12480vec<tree, va_gc> *
12481make_tree_vector_copy (const vec<tree, va_gc> *orig)
c166b898 12482{
9771b263 12483 vec<tree, va_gc> *ret;
c166b898
ILT
12484 unsigned int ix;
12485 tree t;
12486
12487 ret = make_tree_vector ();
9771b263
DN
12488 vec_safe_reserve (ret, vec_safe_length (orig));
12489 FOR_EACH_VEC_SAFE_ELT (orig, ix, t)
12490 ret->quick_push (t);
c166b898
ILT
12491 return ret;
12492}
12493
a9aa2c3a
NF
12494/* Return true if KEYWORD starts a type specifier. */
12495
12496bool
12497keyword_begins_type_specifier (enum rid keyword)
12498{
12499 switch (keyword)
12500 {
38b7bc7f 12501 case RID_AUTO_TYPE:
a9aa2c3a
NF
12502 case RID_INT:
12503 case RID_CHAR:
12504 case RID_FLOAT:
12505 case RID_DOUBLE:
12506 case RID_VOID:
a9aa2c3a
NF
12507 case RID_UNSIGNED:
12508 case RID_LONG:
12509 case RID_SHORT:
12510 case RID_SIGNED:
12511 case RID_DFLOAT32:
12512 case RID_DFLOAT64:
12513 case RID_DFLOAT128:
12514 case RID_FRACT:
12515 case RID_ACCUM:
12516 case RID_BOOL:
12517 case RID_WCHAR:
12518 case RID_CHAR16:
12519 case RID_CHAR32:
12520 case RID_SAT:
12521 case RID_COMPLEX:
12522 case RID_TYPEOF:
12523 case RID_STRUCT:
12524 case RID_CLASS:
12525 case RID_UNION:
12526 case RID_ENUM:
12527 return true;
12528 default:
78a7c317
DD
12529 if (keyword >= RID_FIRST_INT_N
12530 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
12531 && int_n_enabled_p[keyword-RID_FIRST_INT_N])
12532 return true;
a9aa2c3a
NF
12533 return false;
12534 }
12535}
12536
12537/* Return true if KEYWORD names a type qualifier. */
12538
12539bool
12540keyword_is_type_qualifier (enum rid keyword)
12541{
12542 switch (keyword)
12543 {
12544 case RID_CONST:
12545 case RID_VOLATILE:
12546 case RID_RESTRICT:
267bac10 12547 case RID_ATOMIC:
a9aa2c3a
NF
12548 return true;
12549 default:
12550 return false;
12551 }
12552}
12553
12554/* Return true if KEYWORD names a storage class specifier.
12555
12556 RID_TYPEDEF is not included in this list despite `typedef' being
12557 listed in C99 6.7.1.1. 6.7.1.3 indicates that `typedef' is listed as
12558 such for syntactic convenience only. */
12559
12560bool
12561keyword_is_storage_class_specifier (enum rid keyword)
12562{
12563 switch (keyword)
12564 {
12565 case RID_STATIC:
12566 case RID_EXTERN:
12567 case RID_REGISTER:
12568 case RID_AUTO:
12569 case RID_MUTABLE:
12570 case RID_THREAD:
12571 return true;
12572 default:
12573 return false;
12574 }
12575}
12576
ba9e6dd5
NF
12577/* Return true if KEYWORD names a function-specifier [dcl.fct.spec]. */
12578
12579static bool
12580keyword_is_function_specifier (enum rid keyword)
12581{
12582 switch (keyword)
12583 {
12584 case RID_INLINE:
bbceee64 12585 case RID_NORETURN:
ba9e6dd5
NF
12586 case RID_VIRTUAL:
12587 case RID_EXPLICIT:
12588 return true;
12589 default:
12590 return false;
12591 }
12592}
12593
12594/* Return true if KEYWORD names a decl-specifier [dcl.spec] or a
12595 declaration-specifier (C99 6.7). */
12596
12597bool
12598keyword_is_decl_specifier (enum rid keyword)
12599{
12600 if (keyword_is_storage_class_specifier (keyword)
12601 || keyword_is_type_qualifier (keyword)
12602 || keyword_is_function_specifier (keyword))
12603 return true;
12604
12605 switch (keyword)
12606 {
12607 case RID_TYPEDEF:
12608 case RID_FRIEND:
12609 case RID_CONSTEXPR:
12610 return true;
12611 default:
12612 return false;
12613 }
12614}
12615
81f653d6
NF
12616/* Initialize language-specific-bits of tree_contains_struct. */
12617
12618void
12619c_common_init_ts (void)
12620{
12621 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
12622 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
36536d79 12623 MARK_TS_TYPED (ARRAY_NOTATION_REF);
81f653d6
NF
12624}
12625
3ce4f9e4
ESR
12626/* Build a user-defined numeric literal out of an integer constant type VALUE
12627 with identifier SUFFIX. */
12628
12629tree
2d7aa578
ESR
12630build_userdef_literal (tree suffix_id, tree value,
12631 enum overflow_type overflow, tree num_string)
3ce4f9e4
ESR
12632{
12633 tree literal = make_node (USERDEF_LITERAL);
12634 USERDEF_LITERAL_SUFFIX_ID (literal) = suffix_id;
12635 USERDEF_LITERAL_VALUE (literal) = value;
2d7aa578 12636 USERDEF_LITERAL_OVERFLOW (literal) = overflow;
3ce4f9e4
ESR
12637 USERDEF_LITERAL_NUM_STRING (literal) = num_string;
12638 return literal;
12639}
12640
f17a223d
RB
12641/* For vector[index], convert the vector to an array of the underlying type.
12642 Return true if the resulting ARRAY_REF should not be an lvalue. */
aa7da51a
JJ
12643
12644bool
f17a223d
RB
12645convert_vector_to_array_for_subscript (location_t loc,
12646 tree *vecp, tree index)
7edaa4d2 12647{
aa7da51a 12648 bool ret = false;
31521951 12649 if (VECTOR_TYPE_P (TREE_TYPE (*vecp)))
7edaa4d2
MG
12650 {
12651 tree type = TREE_TYPE (*vecp);
7edaa4d2 12652
aa7da51a 12653 ret = !lvalue_p (*vecp);
f17a223d 12654
7edaa4d2 12655 if (TREE_CODE (index) == INTEGER_CST)
cc269bb6 12656 if (!tree_fits_uhwi_p (index)
7d362f6c 12657 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type))
7edaa4d2
MG
12658 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
12659
f17a223d
RB
12660 /* We are building an ARRAY_REF so mark the vector as addressable
12661 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
12662 for function parameters. */
12663 c_common_mark_addressable_vec (*vecp);
12664
12665 *vecp = build1 (VIEW_CONVERT_EXPR,
12666 build_array_type_nelts (TREE_TYPE (type),
12667 TYPE_VECTOR_SUBPARTS (type)),
12668 *vecp);
7edaa4d2 12669 }
aa7da51a 12670 return ret;
7edaa4d2
MG
12671}
12672
a212e43f
MG
12673/* Determine which of the operands, if any, is a scalar that needs to be
12674 converted to a vector, for the range of operations. */
12675enum stv_conv
12676scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1,
12677 bool complain)
12678{
12679 tree type0 = TREE_TYPE (op0);
12680 tree type1 = TREE_TYPE (op1);
12681 bool integer_only_op = false;
12682 enum stv_conv ret = stv_firstarg;
12683
31521951 12684 gcc_assert (VECTOR_TYPE_P (type0) || VECTOR_TYPE_P (type1));
a212e43f
MG
12685 switch (code)
12686 {
12687 /* Most GENERIC binary expressions require homogeneous arguments.
12688 LSHIFT_EXPR and RSHIFT_EXPR are exceptions and accept a first
12689 argument that is a vector and a second one that is a scalar, so
12690 we never return stv_secondarg for them. */
12691 case RSHIFT_EXPR:
12692 case LSHIFT_EXPR:
12693 if (TREE_CODE (type0) == INTEGER_TYPE
12694 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12695 {
68fca595 12696 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
a212e43f
MG
12697 {
12698 if (complain)
12699 error_at (loc, "conversion of scalar %qT to vector %qT "
12700 "involves truncation", type0, type1);
12701 return stv_error;
12702 }
12703 else
12704 return stv_firstarg;
12705 }
12706 break;
12707
12708 case BIT_IOR_EXPR:
12709 case BIT_XOR_EXPR:
12710 case BIT_AND_EXPR:
12711 integer_only_op = true;
191816a3 12712 /* fall through */
a212e43f 12713
93100c6b
MG
12714 case VEC_COND_EXPR:
12715
a212e43f
MG
12716 case PLUS_EXPR:
12717 case MINUS_EXPR:
12718 case MULT_EXPR:
12719 case TRUNC_DIV_EXPR:
12720 case CEIL_DIV_EXPR:
12721 case FLOOR_DIV_EXPR:
12722 case ROUND_DIV_EXPR:
12723 case EXACT_DIV_EXPR:
12724 case TRUNC_MOD_EXPR:
12725 case FLOOR_MOD_EXPR:
12726 case RDIV_EXPR:
12727 case EQ_EXPR:
12728 case NE_EXPR:
12729 case LE_EXPR:
12730 case GE_EXPR:
12731 case LT_EXPR:
12732 case GT_EXPR:
12733 /* What about UNLT_EXPR? */
31521951 12734 if (VECTOR_TYPE_P (type0))
a212e43f 12735 {
a212e43f 12736 ret = stv_secondarg;
6b4db501
MM
12737 std::swap (type0, type1);
12738 std::swap (op0, op1);
a212e43f
MG
12739 }
12740
12741 if (TREE_CODE (type0) == INTEGER_TYPE
12742 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
12743 {
68fca595 12744 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
a212e43f
MG
12745 {
12746 if (complain)
12747 error_at (loc, "conversion of scalar %qT to vector %qT "
12748 "involves truncation", type0, type1);
12749 return stv_error;
12750 }
12751 return ret;
12752 }
12753 else if (!integer_only_op
12754 /* Allow integer --> real conversion if safe. */
12755 && (TREE_CODE (type0) == REAL_TYPE
12756 || TREE_CODE (type0) == INTEGER_TYPE)
12757 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
12758 {
68fca595 12759 if (unsafe_conversion_p (loc, TREE_TYPE (type1), op0, false))
a212e43f
MG
12760 {
12761 if (complain)
12762 error_at (loc, "conversion of scalar %qT to vector %qT "
12763 "involves truncation", type0, type1);
12764 return stv_error;
12765 }
12766 return ret;
12767 }
12768 default:
12769 break;
12770 }
12771
12772 return stv_nothing;
12773}
12774
e28d52cf
DS
12775/* Return true iff ALIGN is an integral constant that is a fundamental
12776 alignment, as defined by [basic.align] in the c++-11
12777 specifications.
12778
12779 That is:
12780
12781 [A fundamental alignment is represented by an alignment less than or
12782 equal to the greatest alignment supported by the implementation
12783 in all contexts, which is equal to
12784 alignof(max_align_t)]. */
12785
12786bool
12787cxx_fundamental_alignment_p (unsigned align)
12788{
12789 return (align <= MAX (TYPE_ALIGN (long_long_integer_type_node),
12790 TYPE_ALIGN (long_double_type_node)));
12791}
12792
f04dda30
MP
12793/* Return true if T is a pointer to a zero-sized aggregate. */
12794
12795bool
12796pointer_to_zero_sized_aggr_p (tree t)
12797{
12798 if (!POINTER_TYPE_P (t))
12799 return false;
12800 t = TREE_TYPE (t);
12801 return (TYPE_SIZE (t) && integer_zerop (TYPE_SIZE (t)));
12802}
12803
1807ffc1
MS
12804/* For an EXPR of a FUNCTION_TYPE that references a GCC built-in function
12805 with no library fallback or for an ADDR_EXPR whose operand is such type
12806 issues an error pointing to the location LOC.
12807 Returns true when the expression has been diagnosed and false
12808 otherwise. */
12809bool
12810reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
12811{
12812 if (TREE_CODE (expr) == ADDR_EXPR)
12813 expr = TREE_OPERAND (expr, 0);
12814
12815 if (TREE_TYPE (expr)
12816 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
1be56bc5 12817 && TREE_CODE (expr) == FUNCTION_DECL
1807ffc1
MS
12818 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
12819 false positives for user-declared built-ins such as abs or
12820 strlen, and for C++ operators new and delete.
12821 The c_decl_implicit() test avoids false positives for implicitly
12822 declared built-ins with library fallbacks (such as abs). */
12823 && DECL_BUILT_IN (expr)
12824 && DECL_IS_BUILTIN (expr)
12825 && !c_decl_implicit (expr)
12826 && !DECL_ASSEMBLER_NAME_SET_P (expr))
12827 {
12828 if (loc == UNKNOWN_LOCATION)
12829 loc = EXPR_LOC_OR_LOC (expr, input_location);
12830
12831 /* Reject arguments that are built-in functions with
12832 no library fallback. */
12833 error_at (loc, "built-in function %qE must be directly called", expr);
12834
12835 return true;
12836 }
12837
12838 return false;
12839}
12840
3e3b8d63
MP
12841/* If we're creating an if-else-if condition chain, first see if we
12842 already have this COND in the CHAIN. If so, warn and don't add COND
12843 into the vector, otherwise add the COND there. LOC is the location
12844 of COND. */
12845
12846void
12847warn_duplicated_cond_add_or_warn (location_t loc, tree cond, vec<tree> **chain)
12848{
12849 /* No chain has been created yet. Do nothing. */
12850 if (*chain == NULL)
12851 return;
12852
12853 if (TREE_SIDE_EFFECTS (cond))
12854 {
12855 /* Uh-oh! This condition has a side-effect, thus invalidates
12856 the whole chain. */
12857 delete *chain;
12858 *chain = NULL;
12859 return;
12860 }
12861
12862 unsigned int ix;
12863 tree t;
12864 bool found = false;
12865 FOR_EACH_VEC_ELT (**chain, ix, t)
12866 if (operand_equal_p (cond, t, 0))
12867 {
12868 if (warning_at (loc, OPT_Wduplicated_cond,
12869 "duplicated %<if%> condition"))
12870 inform (EXPR_LOCATION (t), "previously used here");
12871 found = true;
12872 break;
12873 }
12874
12875 if (!found
12876 && !CONSTANT_CLASS_P (cond)
12877 /* Don't infinitely grow the chain. */
12878 && (*chain)->length () < 512)
12879 (*chain)->safe_push (cond);
12880}
12881
e78bede6
MP
12882/* Check if array size calculations overflow or if the array covers more
12883 than half of the address space. Return true if the size of the array
12884 is valid, false otherwise. TYPE is the type of the array and NAME is
12885 the name of the array, or NULL_TREE for unnamed arrays. */
12886
12887bool
12888valid_array_size_p (location_t loc, tree type, tree name)
12889{
12890 if (type != error_mark_node
12891 && COMPLETE_TYPE_P (type)
12892 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
12893 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
12894 {
12895 if (name)
12896 error_at (loc, "size of array %qE is too large", name);
12897 else
12898 error_at (loc, "size of unnamed array is too large");
12899 return false;
12900 }
12901 return true;
12902}
12903
174f6622
ES
12904/* Read SOURCE_DATE_EPOCH from environment to have a deterministic
12905 timestamp to replace embedded current dates to get reproducible
12906 results. Returns -1 if SOURCE_DATE_EPOCH is not defined. */
15c98b2e 12907
174f6622 12908time_t
15c98b2e 12909cb_get_source_date_epoch (cpp_reader *pfile ATTRIBUTE_UNUSED)
174f6622
ES
12910{
12911 char *source_date_epoch;
fe55692c 12912 int64_t epoch;
174f6622
ES
12913 char *endptr;
12914
12915 source_date_epoch = getenv ("SOURCE_DATE_EPOCH");
12916 if (!source_date_epoch)
12917 return (time_t) -1;
12918
12919 errno = 0;
fe55692c
JDA
12920#if defined(INT64_T_IS_LONG)
12921 epoch = strtol (source_date_epoch, &endptr, 10);
12922#else
174f6622 12923 epoch = strtoll (source_date_epoch, &endptr, 10);
fe55692c 12924#endif
15c98b2e
ES
12925 if (errno != 0 || endptr == source_date_epoch || *endptr != '\0'
12926 || epoch < 0 || epoch > MAX_SOURCE_DATE_EPOCH)
12927 {
12928 error_at (input_location, "environment variable SOURCE_DATE_EPOCH must "
12929 "expand to a non-negative integer less than or equal to %wd",
12930 MAX_SOURCE_DATE_EPOCH);
12931 return (time_t) -1;
12932 }
174f6622
ES
12933
12934 return (time_t) epoch;
12935}
12936
5c3a10fb
MP
12937/* Check and possibly warn if two declarations have contradictory
12938 attributes, such as always_inline vs. noinline. */
12939
12940bool
12941diagnose_mismatched_attributes (tree olddecl, tree newdecl)
12942{
12943 bool warned = false;
12944
12945 tree a1 = lookup_attribute ("optimize", DECL_ATTRIBUTES (olddecl));
12946 tree a2 = lookup_attribute ("optimize", DECL_ATTRIBUTES (newdecl));
12947 /* An optimization attribute applied on a declaration after the
12948 definition is likely not what the user wanted. */
12949 if (a2 != NULL_TREE
12950 && DECL_SAVED_TREE (olddecl) != NULL_TREE
12951 && (a1 == NULL_TREE || !attribute_list_equal (a1, a2)))
12952 warned |= warning (OPT_Wattributes,
12953 "optimization attribute on %qD follows "
12954 "definition but the attribute doesn%'t match",
12955 newdecl);
12956
12957 /* Diagnose inline __attribute__ ((noinline)) which is silly. */
12958 if (DECL_DECLARED_INLINE_P (newdecl)
12959 && DECL_UNINLINABLE (olddecl)
12960 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
12961 warned |= warning (OPT_Wattributes, "inline declaration of %qD follows "
12962 "declaration with attribute noinline", newdecl);
12963 else if (DECL_DECLARED_INLINE_P (olddecl)
12964 && DECL_UNINLINABLE (newdecl)
12965 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
12966 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
12967 "noinline follows inline declaration ", newdecl);
12968 else if (lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl))
12969 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (olddecl)))
12970 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
12971 "%qs follows declaration with attribute %qs",
12972 newdecl, "noinline", "always_inline");
12973 else if (lookup_attribute ("always_inline", DECL_ATTRIBUTES (newdecl))
12974 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
12975 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
12976 "%qs follows declaration with attribute %qs",
12977 newdecl, "always_inline", "noinline");
12978 else if (lookup_attribute ("cold", DECL_ATTRIBUTES (newdecl))
12979 && lookup_attribute ("hot", DECL_ATTRIBUTES (olddecl)))
12980 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
12981 "%qs follows declaration with attribute %qs",
12982 newdecl, "cold", "hot");
12983 else if (lookup_attribute ("hot", DECL_ATTRIBUTES (newdecl))
12984 && lookup_attribute ("cold", DECL_ATTRIBUTES (olddecl)))
12985 warned |= warning (OPT_Wattributes, "declaration of %q+D with attribute "
12986 "%qs follows declaration with attribute %qs",
12987 newdecl, "hot", "cold");
12988 return warned;
12989}
12990
39dabefd 12991#include "gt-c-family-c-common.h"